mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-17 13:05:09 +00:00
chore: add boxlite
This commit is contained in:
parent
a3fe0cc764
commit
c3a95c3611
20 changed files with 824 additions and 1 deletions
4
examples/boxlite-python/.gitignore
vendored
Normal file
4
examples/boxlite-python/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
__pycache__/
|
||||
*.pyc
|
||||
.venv/
|
||||
oci-image/
|
||||
5
examples/boxlite-python/Dockerfile
Normal file
5
examples/boxlite-python/Dockerfile
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
FROM node:22-bookworm-slim
|
||||
RUN apt-get update && apt-get install -y curl ca-certificates && rm -rf /var/lib/apt/lists/*
|
||||
RUN curl -fsSL https://releases.rivet.dev/sandbox-agent/0.2.x/install.sh | sh
|
||||
RUN sandbox-agent install-agent claude
|
||||
RUN sandbox-agent install-agent codex
|
||||
145
examples/boxlite-python/client.py
Normal file
145
examples/boxlite-python/client.py
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
"""Minimal JSON-RPC client for sandbox-agent's streamable HTTP transport."""
|
||||
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
class SandboxConnection:
|
||||
"""Connects to a sandbox-agent server via JSON-RPC over streamable HTTP.
|
||||
|
||||
Endpoints used:
|
||||
POST /v1/acp/{server_id}?agent=... (bootstrap + requests)
|
||||
GET /v1/acp/{server_id} (SSE event stream)
|
||||
DELETE /v1/acp/{server_id} (close)
|
||||
"""
|
||||
|
||||
def __init__(self, base_url: str, agent: str):
|
||||
self.base_url = base_url.rstrip("/")
|
||||
self.agent = agent
|
||||
self.server_id = f"py-{uuid.uuid4().hex[:8]}"
|
||||
self.url = f"{self.base_url}/v1/acp/{self.server_id}"
|
||||
self._next_id = 0
|
||||
self._events: list[dict] = []
|
||||
self._stop = threading.Event()
|
||||
self._sse_thread: threading.Thread | None = None
|
||||
|
||||
def _alloc_id(self) -> int:
|
||||
self._next_id += 1
|
||||
return self._next_id
|
||||
|
||||
def _post(self, method: str, params: dict | None = None, *, bootstrap: bool = False) -> dict:
|
||||
payload: dict = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": self._alloc_id(),
|
||||
"method": method,
|
||||
}
|
||||
if params is not None:
|
||||
payload["params"] = params
|
||||
|
||||
url = f"{self.url}?agent={self.agent}" if bootstrap else self.url
|
||||
r = httpx.post(url, json=payload, timeout=120)
|
||||
r.raise_for_status()
|
||||
body = r.text.strip()
|
||||
return json.loads(body) if body else {}
|
||||
|
||||
# -- Lifecycle -----------------------------------------------------------
|
||||
|
||||
def initialize(self) -> dict:
|
||||
result = self._post(
|
||||
"initialize",
|
||||
{
|
||||
"protocolVersion": 1,
|
||||
"clientInfo": {"name": "python-example", "version": "0.1.0"},
|
||||
},
|
||||
bootstrap=True,
|
||||
)
|
||||
self._start_sse()
|
||||
|
||||
# Auto-authenticate if the agent advertises env-var-based auth methods.
|
||||
auth_methods = result.get("result", {}).get("authMethods", [])
|
||||
env_ids = ("anthropic-api-key", "codex-api-key", "openai-api-key")
|
||||
for method in auth_methods:
|
||||
if method.get("id") not in env_ids:
|
||||
continue
|
||||
try:
|
||||
resp = self._post("authenticate", {"methodId": method["id"]})
|
||||
if "error" not in resp:
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
return result
|
||||
|
||||
def new_session(self, cwd: str = "/root") -> str:
|
||||
result = self._post("session/new", {"cwd": cwd, "mcpServers": []})
|
||||
if "error" in result:
|
||||
raise RuntimeError(f"session/new failed: {result['error'].get('message', result['error'])}")
|
||||
return result["result"]["sessionId"]
|
||||
|
||||
def prompt(self, session_id: str, text: str) -> dict:
|
||||
result = self._post(
|
||||
"session/prompt",
|
||||
{
|
||||
"sessionId": session_id,
|
||||
"prompt": [{"type": "text", "text": text}],
|
||||
},
|
||||
)
|
||||
return result
|
||||
|
||||
def close(self) -> None:
|
||||
self._stop.set()
|
||||
try:
|
||||
httpx.delete(self.url, timeout=2)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# -- SSE event stream (background thread) --------------------------------
|
||||
|
||||
@property
|
||||
def events(self) -> list[dict]:
|
||||
return list(self._events)
|
||||
|
||||
def _start_sse(self) -> None:
|
||||
self._sse_thread = threading.Thread(target=self._sse_loop, daemon=True)
|
||||
self._sse_thread.start()
|
||||
|
||||
def _sse_loop(self) -> None:
|
||||
while not self._stop.is_set():
|
||||
try:
|
||||
with httpx.stream(
|
||||
"GET",
|
||||
self.url,
|
||||
headers={"Accept": "text/event-stream"},
|
||||
timeout=httpx.Timeout(connect=5, read=None, write=5, pool=5),
|
||||
) as resp:
|
||||
buffer = ""
|
||||
for chunk in resp.iter_text():
|
||||
if self._stop.is_set():
|
||||
break
|
||||
buffer += chunk.replace("\r\n", "\n")
|
||||
while "\n\n" in buffer:
|
||||
event_chunk, buffer = buffer.split("\n\n", 1)
|
||||
self._process_sse_event(event_chunk)
|
||||
except Exception:
|
||||
if self._stop.is_set():
|
||||
return
|
||||
time.sleep(0.15)
|
||||
|
||||
def _process_sse_event(self, chunk: str) -> None:
|
||||
data_lines: list[str] = []
|
||||
for line in chunk.split("\n"):
|
||||
if line.startswith("data:"):
|
||||
data_lines.append(line[5:].lstrip())
|
||||
if not data_lines:
|
||||
return
|
||||
payload = "\n".join(data_lines).strip()
|
||||
if not payload:
|
||||
return
|
||||
try:
|
||||
self._events.append(json.loads(payload))
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
32
examples/boxlite-python/credentials.py
Normal file
32
examples/boxlite-python/credentials.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
"""Agent detection and credential helpers for sandbox-agent examples."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def detect_agent() -> str:
|
||||
"""Pick an agent based on env vars. Exits if no credentials are found."""
|
||||
if os.environ.get("SANDBOX_AGENT"):
|
||||
return os.environ["SANDBOX_AGENT"]
|
||||
has_claude = bool(
|
||||
os.environ.get("ANTHROPIC_API_KEY")
|
||||
or os.environ.get("CLAUDE_API_KEY")
|
||||
or os.environ.get("CLAUDE_CODE_OAUTH_TOKEN")
|
||||
)
|
||||
has_codex = (os.environ.get("OPENAI_API_KEY") or "").startswith("sk-")
|
||||
if has_codex:
|
||||
return "codex"
|
||||
if has_claude:
|
||||
return "claude"
|
||||
print("No API keys found. Set ANTHROPIC_API_KEY or OPENAI_API_KEY.")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def build_box_env() -> list[tuple[str, str]]:
|
||||
"""Collect credential env vars to forward into the BoxLite sandbox."""
|
||||
env: list[tuple[str, str]] = []
|
||||
for key in ("ANTHROPIC_API_KEY", "CLAUDE_API_KEY", "OPENAI_API_KEY", "CODEX_API_KEY"):
|
||||
val = os.environ.get(key)
|
||||
if val:
|
||||
env.append((key, val))
|
||||
return env
|
||||
110
examples/boxlite-python/main.py
Normal file
110
examples/boxlite-python/main.py
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
"""
|
||||
Sandbox Agent – Python + BoxLite example.
|
||||
|
||||
Builds a Docker image, exports it to OCI layout, runs it inside a BoxLite
|
||||
sandbox, connects to the sandbox-agent server, creates a session, and sends a prompt.
|
||||
|
||||
Usage:
|
||||
pip install -r requirements.txt
|
||||
python main.py
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import signal
|
||||
import time
|
||||
|
||||
import boxlite
|
||||
import httpx
|
||||
|
||||
from client import SandboxConnection
|
||||
from credentials import build_box_env, detect_agent
|
||||
from setup_image import OCI_DIR, setup_image
|
||||
|
||||
PORT = 3000
|
||||
|
||||
|
||||
def wait_for_health(base_url: str, timeout_s: float = 120) -> None:
|
||||
deadline = time.monotonic() + timeout_s
|
||||
last_err: str | None = None
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
r = httpx.get(f"{base_url}/v1/health", timeout=5)
|
||||
if r.status_code == 200 and r.json().get("status") == "ok":
|
||||
return
|
||||
last_err = f"health returned {r.status_code}"
|
||||
except Exception as exc:
|
||||
last_err = str(exc)
|
||||
time.sleep(0.5)
|
||||
raise RuntimeError(f"Timed out waiting for /v1/health: {last_err}")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
agent = detect_agent()
|
||||
print(f"Agent: {agent}")
|
||||
|
||||
setup_image()
|
||||
|
||||
env = build_box_env()
|
||||
|
||||
print("Creating BoxLite sandbox...")
|
||||
box = boxlite.SimpleBox(
|
||||
rootfs_path=OCI_DIR,
|
||||
env=env,
|
||||
ports=[(PORT, PORT, "tcp")],
|
||||
)
|
||||
|
||||
async with box:
|
||||
print("Starting server...")
|
||||
result = await box.exec(
|
||||
"sh", "-c",
|
||||
f"nohup sandbox-agent server --no-token --host 0.0.0.0 --port {PORT} "
|
||||
">/tmp/sandbox-agent.log 2>&1 &",
|
||||
)
|
||||
if result.exit_code != 0:
|
||||
raise RuntimeError(f"Failed to start server: {result.stderr}")
|
||||
|
||||
base_url = f"http://localhost:{PORT}"
|
||||
print("Waiting for server...")
|
||||
wait_for_health(base_url)
|
||||
print("Server ready.")
|
||||
print(f"Inspector: {base_url}/ui/")
|
||||
|
||||
# -- Session flow ----------------------------------------------------
|
||||
conn = SandboxConnection(base_url, agent)
|
||||
|
||||
print("Connecting...")
|
||||
init_result = conn.initialize()
|
||||
agent_info = init_result.get("result", {}).get("agentInfo", {})
|
||||
print(f"Connected to: {agent_info.get('title', agent)} {agent_info.get('version', '')}")
|
||||
|
||||
session_id = conn.new_session()
|
||||
print(f"Session: {session_id}")
|
||||
|
||||
prompt_text = "Say hello and tell me what you are. Be brief (one sentence)."
|
||||
print(f"\n> {prompt_text}")
|
||||
response = conn.prompt(session_id, prompt_text)
|
||||
|
||||
if "error" in response:
|
||||
err = response["error"]
|
||||
print(f"Error: {err.get('message', err)}")
|
||||
else:
|
||||
print(f"Stop reason: {response.get('result', {}).get('stopReason', 'unknown')}")
|
||||
|
||||
# Give SSE events a moment to arrive.
|
||||
time.sleep(1)
|
||||
|
||||
if conn.events:
|
||||
for ev in conn.events:
|
||||
if ev.get("method") == "session/update":
|
||||
content = ev.get("params", {}).get("update", {}).get("content", {})
|
||||
if content.get("text"):
|
||||
print(content["text"], end="")
|
||||
print()
|
||||
|
||||
conn.close()
|
||||
print("\nDone.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
2
examples/boxlite-python/requirements.txt
Normal file
2
examples/boxlite-python/requirements.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
boxlite>=0.5.0
|
||||
httpx>=0.27.0
|
||||
29
examples/boxlite-python/setup_image.py
Normal file
29
examples/boxlite-python/setup_image.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
"""Build the sandbox-agent Docker image and export it to OCI layout."""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
DOCKER_IMAGE = "sandbox-agent-boxlite"
|
||||
OCI_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "oci-image")
|
||||
|
||||
|
||||
def setup_image() -> None:
|
||||
dockerfile_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
print(f'Building image "{DOCKER_IMAGE}" (cached after first run)...')
|
||||
subprocess.run(
|
||||
["docker", "build", "-t", DOCKER_IMAGE, dockerfile_dir],
|
||||
check=True,
|
||||
)
|
||||
|
||||
if not os.path.exists(os.path.join(OCI_DIR, "oci-layout")):
|
||||
print("Exporting to OCI layout...")
|
||||
os.makedirs(OCI_DIR, exist_ok=True)
|
||||
subprocess.run(
|
||||
[
|
||||
"skopeo", "copy",
|
||||
f"docker-daemon:{DOCKER_IMAGE}:latest",
|
||||
f"oci:{OCI_DIR}:latest",
|
||||
],
|
||||
check=True,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue