mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-15 09:01:17 +00:00
refactor: make docker and e2b examples testable with exported setup functions
This commit is contained in:
parent
e434bc6d1e
commit
0ecf4cbe0d
8 changed files with 147 additions and 102 deletions
|
|
@ -14,6 +14,7 @@
|
|||
"devDependencies": {
|
||||
"@types/node": "latest",
|
||||
"tsx": "latest",
|
||||
"typescript": "latest"
|
||||
"typescript": "latest",
|
||||
"vitest": "^3.0.0"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { buildHeaders } from "../shared/sandbox-agent-client.ts";
|
||||
import { buildHeaders } from "@sandbox-agent/example-shared";
|
||||
import { setupE2BSandboxAgent } from "./e2b.ts";
|
||||
|
||||
const shouldRun = Boolean(process.env.E2B_API_KEY);
|
||||
|
|
|
|||
|
|
@ -1,52 +1,65 @@
|
|||
import { Sandbox } from "@e2b/code-interpreter";
|
||||
import { logInspectorUrl, runPrompt } from "@sandbox-agent/example-shared";
|
||||
import { logInspectorUrl, runPrompt, waitForHealth } from "@sandbox-agent/example-shared";
|
||||
|
||||
if (!process.env.E2B_API_KEY || (!process.env.OPENAI_API_KEY && !process.env.ANTHROPIC_API_KEY)) {
|
||||
throw new Error("E2B_API_KEY and (OPENAI_API_KEY or ANTHROPIC_API_KEY) required");
|
||||
export async function setupE2BSandboxAgent(): Promise<{
|
||||
baseUrl: string;
|
||||
token?: string;
|
||||
cleanup: () => Promise<void>;
|
||||
}> {
|
||||
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 = async (cmd: string) => {
|
||||
const result = await sandbox.commands.run(cmd);
|
||||
if (result.exitCode !== 0) throw new Error(`Command failed: ${cmd}\n${result.stderr}`);
|
||||
return result;
|
||||
};
|
||||
|
||||
console.log("Installing sandbox-agent...");
|
||||
await run("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.commands.run("sandbox-agent server --no-token --host 0.0.0.0 --port 3000", { background: true });
|
||||
|
||||
const baseUrl = `https://${sandbox.getHost(3000)}`;
|
||||
|
||||
// Wait for server to be ready
|
||||
console.log("Waiting for server...");
|
||||
await waitForHealth({ baseUrl });
|
||||
|
||||
const cleanup = async () => {
|
||||
console.log("Cleaning up...");
|
||||
await sandbox.kill();
|
||||
};
|
||||
|
||||
return { baseUrl, cleanup };
|
||||
}
|
||||
|
||||
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 = async (cmd: string) => {
|
||||
const result = await sandbox.commands.run(cmd);
|
||||
if (result.exitCode !== 0) throw new Error(`Command failed: ${cmd}\n${result.stderr}`);
|
||||
return result;
|
||||
};
|
||||
|
||||
console.log("Installing sandbox-agent...");
|
||||
await run("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.commands.run("sandbox-agent server --no-token --host 0.0.0.0 --port 3000", { background: true });
|
||||
|
||||
const baseUrl = `https://${sandbox.getHost(3000)}`;
|
||||
logInspectorUrl({ baseUrl });
|
||||
|
||||
// Wait for server to be ready
|
||||
console.log("Waiting for server...");
|
||||
for (let i = 0; i < 30; i++) {
|
||||
try {
|
||||
const res = await fetch(`${baseUrl}/v1/health`);
|
||||
if (res.ok) break;
|
||||
} catch {
|
||||
await new Promise((r) => setTimeout(r, 1000));
|
||||
// Run interactively if executed directly
|
||||
const isMainModule = import.meta.url === `file://${process.argv[1]}`;
|
||||
if (isMainModule) {
|
||||
if (!process.env.OPENAI_API_KEY && !process.env.ANTHROPIC_API_KEY) {
|
||||
throw new Error("E2B_API_KEY and (OPENAI_API_KEY or ANTHROPIC_API_KEY) required");
|
||||
}
|
||||
|
||||
const { baseUrl, cleanup } = await setupE2BSandboxAgent();
|
||||
logInspectorUrl({ baseUrl });
|
||||
|
||||
process.once("SIGINT", async () => {
|
||||
await cleanup();
|
||||
process.exit(0);
|
||||
});
|
||||
process.once("SIGTERM", async () => {
|
||||
await cleanup();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
await runPrompt({ baseUrl });
|
||||
await cleanup();
|
||||
}
|
||||
|
||||
const cleanup = async () => {
|
||||
console.log("Cleaning up...");
|
||||
await sandbox.kill();
|
||||
process.exit(0);
|
||||
};
|
||||
process.once("SIGINT", cleanup);
|
||||
process.once("SIGTERM", cleanup);
|
||||
|
||||
await runPrompt({ baseUrl });
|
||||
await cleanup();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue