import timesb = Aio(BASE_URL)session = sb.post("/v2/pty/sessions", cwd="/workspace")sid = session["session_id"]# 1. Start a program that asks a question; async returns while it waits.body = {"command": 'read -p "Name: " name; echo Hello $name', "async": True}sb.call("POST", f"/v2/pty/sessions/{sid}/exec", json=body)# 2. The prompt reaches the screen a moment after exec returns.time.sleep(0.3)screen = sb.get(f"/v2/pty/sessions/{sid}/screen")print(repr(screen["output"]), screen["status"])# 'Name: ' running# 3. Answer it, then read the screen once the command has ended.sb.post(f"/v2/pty/sessions/{sid}/input", input="Alice", press_enter=True)while sb.get(f"/v2/pty/sessions/{sid}/screen")["status"] == "running": time.sleep(0.2)screen = sb.get(f"/v2/pty/sessions/{sid}/screen")print(repr(screen["output"]), screen["status"], screen["exit_code"])# 'Name: Alice\nHello Alice' completed 0# 4. Close the terminal.sb.delete(f"/v2/pty/sessions/{sid}")
const sb = new Aio(BASE_URL);const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));const session = await sb.post("/v2/pty/sessions", { cwd: "/workspace" });const sid = session.session_id;// 1. Start a program that asks a question; async returns while it waits.await sb.post(`/v2/pty/sessions/${sid}/exec`, { command: 'read -p "Name: " name; echo Hello $name', async: true,});// 2. The prompt reaches the screen a moment after exec returns.await sleep(300);let screen = await sb.get(`/v2/pty/sessions/${sid}/screen`);console.log(JSON.stringify(screen.output), screen.status);// "Name: " running// 3. Answer it, then read the screen once the command has ended.await sb.post(`/v2/pty/sessions/${sid}/input`, { input: "Alice", press_enter: true,});while ((await sb.get(`/v2/pty/sessions/${sid}/screen`)).status === "running") { await sleep(200);}screen = await sb.get(`/v2/pty/sessions/${sid}/screen`);console.log(JSON.stringify(screen.output), screen.status, screen.exit_code);// "Name: Alice\nHello Alice" completed 0// 4. Close the terminal.await sb.delete(`/v2/pty/sessions/${sid}`);
Python
TypeScript
import timefrom agent_sandbox import Sandboxclient = Sandbox(base_url=BASE_URL)session = client.shell.create_session(exec_dir="/workspace").datasid = session.session_id# 1. Start a program that asks a question; async_mode returns while it waits.client.shell.exec_command( id=sid, command='read -p "Name: " name; echo Hello $name', async_mode=True)# 2. The prompt reaches the screen a moment after the call returns.time.sleep(0.3)screen = client.shell.view(id=sid).dataprint(repr(screen.output), screen.status)# 'Name: ' running# 3. Answer it, then wait for the command to end.client.shell.write_to_process(id=sid, input="Alice", press_enter=True)waited = client.shell.wait_for_process(id=sid, seconds=5).datascreen = client.shell.view(id=sid).dataprint(repr(screen.output), waited.status, screen.exit_code)# 'Name: Alice\nHello Alice' completed 0# 4. Close the terminal.client.shell.cleanup_session(sid)
import { SandboxClient } from "@agent-infra/sandbox";const client = new SandboxClient({ environment: BASE_URL });const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));const session = ( await client.shell.createSession({ exec_dir: "/workspace" })).body.data!;const id = session.session_id;// 1. Start a program that asks a question; async_mode returns while it waits.await client.shell.execCommand({ id, command: 'read -p "Name: " name; echo Hello $name', async_mode: true,});// 2. The prompt reaches the screen a moment after the call returns.await sleep(300);let screen = (await client.shell.view({ id })).body.data!;console.log(JSON.stringify(screen.output), screen.status);// "Name: " running// 3. Answer it, then wait for the command to end.await client.shell.writeToProcess({ id, input: "Alice", press_enter: true });const waited = ( await client.shell.waitForProcess({ id, seconds: 5 })).body.data!;screen = (await client.shell.view({ id })).body.data!;console.log(JSON.stringify(screen.output), waited.status, screen.exit_code);// "Name: Alice\nHello Alice" completed 0// 4. Close the terminal.await client.shell.cleanupSession(id);