diff --git a/examples/e2b/src/e2b.ts b/examples/e2b/src/e2b.ts index fa8bb1e..36fc584 100644 --- a/examples/e2b/src/e2b.ts +++ b/examples/e2b/src/e2b.ts @@ -6,7 +6,11 @@ if (!process.env.E2B_API_KEY || (!process.env.OPENAI_API_KEY && !process.env.ANT throw new Error("E2B_API_KEY and (OPENAI_API_KEY or ANTHROPIC_API_KEY) required"); } -const sandbox = await Sandbox.create({ allowInternetAccess: true }); +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; + +const sandbox = await Sandbox.create({ allowInternetAccess: true, envs }); const run = (cmd: string) => sandbox.commands.run(cmd); diff --git a/examples/shared/src/sandbox-agent-client.ts b/examples/shared/src/sandbox-agent-client.ts index 817d910..ccf18bb 100644 --- a/examples/shared/src/sandbox-agent-client.ts +++ b/examples/shared/src/sandbox-agent-client.ts @@ -295,6 +295,7 @@ export async function runPrompt({ let isThinking = false; let hasStartedOutput = false; let turnResolve: (() => void) | null = null; + let sessionEnded = false; // Stream events in background using SDK const processEvents = async () => { @@ -332,9 +333,22 @@ export async function runPrompt({ turnResolve = null; } } + + // Handle session ended + if (event.type === "session.ended") { + const data = event.data as any; + console.log(`Agent Process Exited${data?.reason ? `: ${data.reason}` : ""}`); + sessionEnded = true; + turnResolve?.(); + turnResolve = null; + } } }; - processEvents().catch(() => {}); + processEvents().catch((err) => { + if (!sessionEnded) { + console.error("Event stream error:", err instanceof Error ? err.message : err); + } + }); // Read user input and post messages const rl = createInterface({ input: process.stdin, output: process.stdout });