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"
},
"dependencies": {
"dockerode": "latest",
"@sandbox-agent/example-shared": "workspace:*"
"@sandbox-agent/example-shared": "workspace:*",
"dockerode": "latest"
},
"devDependencies": {
"@types/dockerode": "latest",
"@types/node": "latest",
"tsx": "latest",
"typescript": "latest"
"typescript": "latest",
"vitest": "^3.0.0"
}
}

View file

@ -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 { setupDockerSandboxAgent } from "./docker.ts";
const shouldRun = process.env.RUN_DOCKER_EXAMPLES === "1";

View file

@ -1,20 +1,21 @@
import Docker from "dockerode";
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
const IMAGE = "alpine:latest";
const PORT = 3000;
const docker = new Docker({ socketPath: "/var/run/docker.sock" });
export async function setupDockerSandboxAgent(): Promise<{
baseUrl: string;
token?: string;
cleanup: () => Promise<void>;
}> {
const docker = new Docker({ socketPath: "/var/run/docker.sock" });
// Pull image if needed
try {
// Pull image if needed
try {
await docker.getImage(IMAGE).inspect();
} catch {
} catch {
console.log(`Pulling ${IMAGE}...`);
await new Promise<void>((resolve, reject) => {
docker.pull(IMAGE, (err: Error | null, stream: NodeJS.ReadableStream) => {
@ -22,10 +23,10 @@ try {
docker.modem.followProgress(stream, (err: Error | null) => err ? reject(err) : resolve());
});
});
}
}
console.log("Starting container...");
const container = await docker.createContainer({
console.log("Starting container...");
const container = await docker.createContainer({
Image: IMAGE,
Cmd: ["sh", "-c", [
// Install dependencies (Alpine uses apk, not apt-get)
@ -44,21 +45,40 @@ const container = await docker.createContainer({
AutoRemove: true,
PortBindings: { [`${PORT}/tcp`]: [{ HostPort: `${PORT}` }] },
},
});
await container.start();
});
await container.start();
const baseUrl = `http://127.0.0.1:${PORT}`;
await waitForHealth({ baseUrl });
logInspectorUrl({ baseUrl });
const baseUrl = `http://127.0.0.1:${PORT}`;
await waitForHealth({ baseUrl });
const cleanup = async () => {
const cleanup = async () => {
console.log("Cleaning up...");
try { await container.stop({ t: 5 }); } catch {}
try { await container.remove({ force: true }); } catch {}
process.exit(0);
};
process.once("SIGINT", cleanup);
process.once("SIGTERM", cleanup);
};
await runPrompt({ baseUrl });
await 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 cleanup();
}

View file

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

View file

@ -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);

View file

@ -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 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 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 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("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 });
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 });
const baseUrl = `https://${sandbox.getHost(3000)}`;
// 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));
}
}
// Wait for server to be ready
console.log("Waiting for server...");
await waitForHealth({ baseUrl });
const cleanup = async () => {
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();
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 cleanup();
}

View file

@ -11,6 +11,7 @@
"dev:docs": "cd docs/ && pnpm dlx mintlify dev"
},
"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:
specifier: ^2.4.0
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:
dependencies:
@ -52,6 +55,9 @@ importers:
typescript:
specifier: latest
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:
dependencies:
@ -74,6 +80,9 @@ importers:
typescript:
specifier: latest
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:
dependencies: