import { Sandbox } from "@vercel/sandbox"; import { runPrompt, waitForHealth } from "@sandbox-agent/example-shared"; const envs: Record = {}; if (process.env.ANTHROPIC_API_KEY) envs.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY; if (process.env.OPENAI_API_KEY) envs.OPENAI_API_KEY = process.env.OPENAI_API_KEY; console.log("Creating Vercel sandbox..."); const sandbox = await Sandbox.create({ runtime: "node24", ports: [3000], }); const run = async (cmd: string, args: string[] = []) => { const result = await sandbox.runCommand({ cmd, args, env: envs }); if (result.exitCode !== 0) { const stderr = await result.stderr(); throw new Error(`Command failed: ${cmd} ${args.join(" ")}\n${stderr}`); } return result; }; console.log("Installing sandbox-agent..."); await run("sh", ["-c", "curl -fsSL https://releases.rivet.dev/sandbox-agent/latest/install.sh | sh"]); console.log("Installing agents..."); await run("sandbox-agent", ["install-agent", "claude"]); await run("sandbox-agent", ["install-agent", "codex"]); console.log("Starting server..."); await sandbox.runCommand({ cmd: "sandbox-agent", args: ["server", "--no-token", "--host", "0.0.0.0", "--port", "3000"], env: envs, detached: true, }); const baseUrl = sandbox.domain(3000); console.log("Waiting for server..."); await waitForHealth({ baseUrl }); const cleanup = async () => { await sandbox.stop(); process.exit(0); }; process.once("SIGINT", cleanup); process.once("SIGTERM", cleanup); await runPrompt(baseUrl); await cleanup();