refactor: make docker and e2b examples testable with exported setup functions

This commit is contained in:
Nathan Flurry 2026-01-28 05:31:51 -08:00
parent e434bc6d1e
commit 0ecf4cbe0d
8 changed files with 147 additions and 102 deletions

View file

@ -7,13 +7,14 @@
"typecheck": "tsc --noEmit" "typecheck": "tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"dockerode": "latest", "@sandbox-agent/example-shared": "workspace:*",
"@sandbox-agent/example-shared": "workspace:*" "dockerode": "latest"
}, },
"devDependencies": { "devDependencies": {
"@types/dockerode": "latest", "@types/dockerode": "latest",
"@types/node": "latest", "@types/node": "latest",
"tsx": "latest", "tsx": "latest",
"typescript": "latest" "typescript": "latest",
"vitest": "^3.0.0"
} }
} }

View file

@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest"; import { describe, it, expect } from "vitest";
import { buildHeaders } from "../shared/sandbox-agent-client.ts"; import { buildHeaders } from "@sandbox-agent/example-shared";
import { setupDockerSandboxAgent } from "./docker.ts"; import { setupDockerSandboxAgent } from "./docker.ts";
const shouldRun = process.env.RUN_DOCKER_EXAMPLES === "1"; const shouldRun = process.env.RUN_DOCKER_EXAMPLES === "1";

View file

@ -1,14 +1,15 @@
import Docker from "dockerode"; import Docker from "dockerode";
import { logInspectorUrl, runPrompt, waitForHealth } from "@sandbox-agent/example-shared"; import { logInspectorUrl, runPrompt, waitForHealth } from "@sandbox-agent/example-shared";
if (!process.env.OPENAI_API_KEY && !process.env.ANTHROPIC_API_KEY) {
throw new Error("OPENAI_API_KEY or ANTHROPIC_API_KEY required");
}
// Alpine is required because Claude Code binary is built for musl libc // Alpine is required because Claude Code binary is built for musl libc
const IMAGE = "alpine:latest"; const IMAGE = "alpine:latest";
const PORT = 3000; const PORT = 3000;
export async function setupDockerSandboxAgent(): Promise<{
baseUrl: string;
token?: string;
cleanup: () => Promise<void>;
}> {
const docker = new Docker({ socketPath: "/var/run/docker.sock" }); const docker = new Docker({ socketPath: "/var/run/docker.sock" });
// Pull image if needed // Pull image if needed
@ -49,16 +50,35 @@ await container.start();
const baseUrl = `http://127.0.0.1:${PORT}`; const baseUrl = `http://127.0.0.1:${PORT}`;
await waitForHealth({ baseUrl }); await waitForHealth({ baseUrl });
logInspectorUrl({ baseUrl });
const cleanup = async () => { const cleanup = async () => {
console.log("Cleaning up..."); console.log("Cleaning up...");
try { await container.stop({ t: 5 }); } catch {} try { await container.stop({ t: 5 }); } catch {}
try { await container.remove({ force: true }); } catch {} try { await container.remove({ force: true }); } catch {}
process.exit(0);
}; };
process.once("SIGINT", cleanup);
process.once("SIGTERM", cleanup); return { baseUrl, cleanup };
}
// 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("OPENAI_API_KEY or ANTHROPIC_API_KEY required");
}
const { baseUrl, cleanup } = await setupDockerSandboxAgent();
logInspectorUrl({ baseUrl });
process.once("SIGINT", async () => {
await cleanup();
process.exit(0);
});
process.once("SIGTERM", async () => {
await cleanup();
process.exit(0);
});
await runPrompt({ baseUrl }); await runPrompt({ baseUrl });
await cleanup(); await cleanup();
}

View file

@ -14,6 +14,7 @@
"devDependencies": { "devDependencies": {
"@types/node": "latest", "@types/node": "latest",
"tsx": "latest", "tsx": "latest",
"typescript": "latest" "typescript": "latest",
"vitest": "^3.0.0"
} }
} }

View file

@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest"; 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"; import { setupE2BSandboxAgent } from "./e2b.ts";
const shouldRun = Boolean(process.env.E2B_API_KEY); const shouldRun = Boolean(process.env.E2B_API_KEY);

View file

@ -1,10 +1,11 @@
import { Sandbox } from "@e2b/code-interpreter"; 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> = {}; const envs: Record<string, string> = {};
if (process.env.ANTHROPIC_API_KEY) envs.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY; 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; if (process.env.OPENAI_API_KEY) envs.OPENAI_API_KEY = process.env.OPENAI_API_KEY;
@ -27,26 +28,38 @@ console.log("Starting server...");
await sandbox.commands.run("sandbox-agent server --no-token --host 0.0.0.0 --port 3000", { background: true }); await sandbox.commands.run("sandbox-agent server --no-token --host 0.0.0.0 --port 3000", { background: true });
const baseUrl = `https://${sandbox.getHost(3000)}`; const baseUrl = `https://${sandbox.getHost(3000)}`;
logInspectorUrl({ baseUrl });
// Wait for server to be ready // Wait for server to be ready
console.log("Waiting for server..."); console.log("Waiting for server...");
for (let i = 0; i < 30; i++) { await waitForHealth({ baseUrl });
try {
const res = await fetch(`${baseUrl}/v1/health`);
if (res.ok) break;
} catch {
await new Promise((r) => setTimeout(r, 1000));
}
}
const cleanup = async () => { const cleanup = async () => {
console.log("Cleaning up..."); console.log("Cleaning up...");
await sandbox.kill(); await sandbox.kill();
process.exit(0);
}; };
process.once("SIGINT", cleanup);
process.once("SIGTERM", cleanup); return { baseUrl, cleanup };
}
// 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 runPrompt({ baseUrl });
await cleanup(); await cleanup();
}

View file

@ -11,6 +11,7 @@
"dev:docs": "cd docs/ && pnpm dlx mintlify dev" "dev:docs": "cd docs/ && pnpm dlx mintlify dev"
}, },
"devDependencies": { "devDependencies": {
"turbo": "^2.4.0" "turbo": "^2.4.0",
"vitest": "^3.0.0"
} }
} }

9
pnpm-lock.yaml generated
View file

@ -11,6 +11,9 @@ importers:
turbo: turbo:
specifier: ^2.4.0 specifier: ^2.4.0
version: 2.7.6 version: 2.7.6
vitest:
specifier: ^3.0.0
version: 3.2.4(@types/debug@4.1.12)(@types/node@25.0.10)
examples/daytona: examples/daytona:
dependencies: dependencies:
@ -52,6 +55,9 @@ importers:
typescript: typescript:
specifier: latest specifier: latest
version: 5.9.3 version: 5.9.3
vitest:
specifier: ^3.0.0
version: 3.2.4(@types/debug@4.1.12)(@types/node@25.0.10)
examples/e2b: examples/e2b:
dependencies: dependencies:
@ -74,6 +80,9 @@ importers:
typescript: typescript:
specifier: latest specifier: latest
version: 5.9.3 version: 5.9.3
vitest:
specifier: ^3.0.0
version: 3.2.4(@types/debug@4.1.12)(@types/node@25.0.10)
examples/shared: examples/shared:
dependencies: dependencies: