fix(examples): pass API keys to E2B sandbox and improve session end handling

This commit is contained in:
Nathan Flurry 2026-01-28 02:33:04 -08:00
parent 8af152f0b3
commit fa89872d3b
2 changed files with 20 additions and 2 deletions

View file

@ -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"); 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<string, string> = {};
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); const run = (cmd: string) => sandbox.commands.run(cmd);

View file

@ -295,6 +295,7 @@ export async function runPrompt({
let isThinking = false; let isThinking = false;
let hasStartedOutput = false; let hasStartedOutput = false;
let turnResolve: (() => void) | null = null; let turnResolve: (() => void) | null = null;
let sessionEnded = false;
// Stream events in background using SDK // Stream events in background using SDK
const processEvents = async () => { const processEvents = async () => {
@ -332,9 +333,22 @@ export async function runPrompt({
turnResolve = null; 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 // Read user input and post messages
const rl = createInterface({ input: process.stdin, output: process.stdout }); const rl = createInterface({ input: process.stdin, output: process.stdout });