sb = Aio(BASE_URL)# 1. A session fixes the directory and the environment for every command in it.session = sb.post("/v2/commands/sessions", cwd="/workspace/site", env={"PORT": "3000"})# 2. Start the server in it; async returns as soon as the process is spawned.started = sb.post("/v2/commands", command="python3 -u -m http.server $PORT", session=session["session_id"], mode="async")command_id = started["command_id"]# 3. Read what it printed; wait long-polls until output arrives.log = sb.get(f"/v2/commands/{command_id}", offset=0, stderr_offset=0, wait=True, wait_timeout=5)print(log["stdout"].strip(), "|", log["command"]["status"])# Serving HTTP on :: port 3000 (http://[::]:3000/) ... | running# 4. Stop the command, then drop the session.sb.post(f"/v2/commands/{command_id}/kill", signal="SIGTERM")sb.delete(f"/v2/commands/sessions/{session['session_id']}")
const sb = new Aio(BASE_URL);// 1. A session fixes the directory and the environment for every command in it.const session = await sb.post("/v2/commands/sessions", { cwd: "/workspace/site", env: { PORT: "3000" },});// 2. Start the server in it; async returns as soon as the process is spawned.const started = await sb.post("/v2/commands", { command: "python3 -u -m http.server $PORT", session: session.session_id, mode: "async",});// 3. Read what it printed; wait long-polls until output arrives.const log = await sb.get( `/v2/commands/${started.command_id}?offset=0&wait=true&wait_timeout=5`,);console.log(log.stdout.trim(), "|", log.command.status);// Serving HTTP on :: port 3000 (http://[::]:3000/) ... | running// 4. Stop the command, then drop the session.await sb.post(`/v2/commands/${started.command_id}/kill`, { signal: "SIGTERM" });await sb.delete(`/v2/commands/sessions/${session.session_id}`);
Python
TypeScript
from agent_sandbox import Sandboxclient = Sandbox(base_url=BASE_URL)# 1. A session fixes the working directory for every command in it.session = client.bash.create_session(exec_dir="/workspace/site").data# 2. Start the server in it; async_mode returns as soon as it is spawned.client.bash.exec( command="python3 -u -m http.server 3000", session_id=session.session_id, async_mode=True,)# 3. Read what it printed; wait long-polls until output arrives.log = client.bash.output( session_id=session.session_id, offset=0, stderr_offset=0, wait=True, wait_timeout=5,).dataprint(log.stdout.strip(), "|", log.command.status)# Serving HTTP on :: port 3000 (http://[::]:3000/) ... | running# 4. Stop the command, then close the session.client.bash.kill(session_id=session.session_id, signal="SIGTERM")client.bash.close_session(session.session_id)
import { SandboxClient } from "@agent-infra/sandbox";const client = new SandboxClient({ environment: BASE_URL });// 1. A session fixes the working directory for every command in it.const session = ( await client.bash.createSession({ exec_dir: "/workspace/site" })).body.data!;// 2. Start the server in it; async_mode returns as soon as it is spawned.await client.bash.exec({ command: "python3 -u -m http.server 3000", session_id: session.session_id, async_mode: true,});// 3. Read what it printed; wait long-polls until output arrives.const log = ( await client.bash.output({ session_id: session.session_id, offset: 0, stderr_offset: 0, wait: true, wait_timeout: 5, })).body.data!;console.log(log.stdout!.trim(), "|", log.command?.status);// Serving HTTP on :: port 3000 (http://[::]:3000/) ... | running// 4. Stop the command, then close the session.await client.bash.kill({ session_id: session.session_id, signal: "SIGTERM" });await client.bash.closeSession(session.session_id);
sb = Aio(BASE_URL)# 1. Start Python in interactive mode (-i: stdin is a pipe, not a terminal).repl = sb.post("/v2/commands", command="python3 -i", mode="async")command_id = repl["command_id"]# 2. The banner and the first prompt arrive on stderr; read them in order.banner = sb.get(f"/v2/commands/{command_id}", offset=0, stderr_offset=0, wait=True, wait_timeout=5)prompt = sb.get(f"/v2/commands/{command_id}", offset=banner["offset"], stderr_offset=banner["stderr_offset"], wait=True, wait_timeout=5)print(repr(prompt["stderr"])) # '>>> '# 3. Send a line, then read the result.sb.post(f"/v2/commands/{command_id}/stdin", input="1 + 1\n")out = sb.get(f"/v2/commands/{command_id}", offset=prompt["offset"], stderr_offset=prompt["stderr_offset"], wait=True, wait_timeout=5)print(repr(out["stdout"]), repr(out["stderr"])) # '2\n' '>>> '# 4. Leave: the process ends with exit_code 0.sb.post(f"/v2/commands/{command_id}/stdin", input="exit()\n")
const sb = new Aio(BASE_URL);// 1. Start Python in interactive mode (-i: stdin is a pipe, not a terminal).const repl = await sb.post("/v2/commands", { command: "python3 -i", mode: "async",});const read = (offset: number, stderrOffset: number) => sb.get( `/v2/commands/${repl.command_id}?offset=${offset}` + `&stderr_offset=${stderrOffset}&wait=true&wait_timeout=5`, );// 2. The banner and the first prompt arrive on stderr; read them in order.const banner = await read(0, 0);const prompt = await read(banner.offset, banner.stderr_offset);console.log(JSON.stringify(prompt.stderr));// ">>> "// 3. Send a line, then read the result.await sb.post(`/v2/commands/${repl.command_id}/stdin`, { input: "1 + 1\n" });const out = await read(prompt.offset, prompt.stderr_offset);console.log(JSON.stringify(out.stdout), JSON.stringify(out.stderr));// "2\n" ">>> "// 4. Leave: the process ends with exit_code 0.await sb.post(`/v2/commands/${repl.command_id}/stdin`, { input: "exit()\n" });
Python
TypeScript
# 1. Start Python in interactive mode (-i: stdin is a pipe, not a terminal).started = client.bash.exec(command="python3 -i", async_mode=True).datasession_id = started.session_id# 2. The banner and the first prompt arrive on stderr; read them in order.banner = client.bash.output( session_id=session_id, offset=0, stderr_offset=0, wait=True, wait_timeout=5).dataprompt = client.bash.output( session_id=session_id, offset=banner.offset, stderr_offset=banner.stderr_offset, wait=True, wait_timeout=5,).dataprint(repr(prompt.stderr)) # '>>> '# 3. Send a line, then read the result.client.bash.write(session_id=session_id, input="1 + 1\n")out = client.bash.output( session_id=session_id, offset=prompt.offset, stderr_offset=prompt.stderr_offset, wait=True, wait_timeout=5,).dataprint(repr(out.stdout), repr(out.stderr)) # '2\n' '>>> '# 4. Leave: the process ends with exit_code 0.client.bash.write(session_id=session_id, input="exit()\n")
// 1. Start Python in interactive mode (-i: stdin is a pipe, not a terminal).const started = ( await client.bash.exec({ command: "python3 -i", async_mode: true })).body.data!;const session_id = started.session_id;// 2. The banner and the first prompt arrive on stderr; read them in order.const banner = ( await client.bash.output({ session_id, offset: 0, stderr_offset: 0, wait: true, wait_timeout: 5, })).body.data!;const prompt = ( await client.bash.output({ session_id, offset: banner.offset, stderr_offset: banner.stderr_offset, wait: true, wait_timeout: 5, })).body.data!;console.log(JSON.stringify(prompt.stderr));// ">>> "// 3. Send a line, then read the result.await client.bash.write({ session_id, input: "1 + 1\n" });const out = ( await client.bash.output({ session_id, offset: prompt.offset, stderr_offset: prompt.stderr_offset, wait: true, wait_timeout: 5, })).body.data!;console.log(JSON.stringify(out.stdout), JSON.stringify(out.stderr));// "2\n" ">>> "// 4. Leave: the process ends with exit_code 0.await client.bash.write({ session_id, input: "exit()\n" });