mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-15 14:03:52 +00:00
- Add .github/actions/docker-setup composite action (from rivet) - Add docker/runtime/Dockerfile for Docker image builds - Update release.yaml to match rivet patterns: - Use corepack enable instead of pnpm/action-setup - Add reuse_engine_version input - Add Docker job with Depot runners - Use --no-frozen-lockfile for pnpm install - Add id-token permission for setup job
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import { Sandbox } from "@e2b/code-interpreter";
|
|
import { pathToFileURL } from "node:url";
|
|
import {
|
|
ensureUrl,
|
|
logInspectorUrl,
|
|
runPrompt,
|
|
waitForHealth,
|
|
} from "../shared/sandbox-agent-client.ts";
|
|
|
|
const INSTALL_SCRIPT = "curl -fsSL https://releases.rivet.dev/sandbox-agent/latest/install.sh | sh";
|
|
const DEFAULT_PORT = 2468;
|
|
|
|
type CommandRunner = (command: string, options?: Record<string, unknown>) => Promise<unknown>;
|
|
|
|
function resolveCommandRunner(sandbox: Sandbox): CommandRunner {
|
|
if (sandbox.commands?.run) {
|
|
return sandbox.commands.run.bind(sandbox.commands);
|
|
}
|
|
if (sandbox.commands?.exec) {
|
|
return sandbox.commands.exec.bind(sandbox.commands);
|
|
}
|
|
throw new Error("E2B SDK does not expose commands.run or commands.exec");
|
|
}
|
|
|
|
export async function setupE2BSandboxAgent(): Promise<{
|
|
baseUrl: string;
|
|
token: string;
|
|
cleanup: () => Promise<void>;
|
|
}> {
|
|
const token = process.env.SANDBOX_TOKEN || "";
|
|
const port = Number.parseInt(process.env.SANDBOX_PORT || "", 10) || DEFAULT_PORT;
|
|
|
|
const sandbox = await Sandbox.create({
|
|
allowInternetAccess: true,
|
|
envs: token ? { SANDBOX_TOKEN: token } : undefined,
|
|
});
|
|
|
|
const runCommand = resolveCommandRunner(sandbox);
|
|
|
|
await runCommand(`bash -lc "${INSTALL_SCRIPT}"`);
|
|
const tokenFlag = token ? "--token $SANDBOX_TOKEN" : "--no-token";
|
|
await runCommand(`bash -lc "sandbox-agent server ${tokenFlag} --host 0.0.0.0 --port ${port}"`, {
|
|
background: true,
|
|
envs: token ? { SANDBOX_TOKEN: token } : undefined,
|
|
});
|
|
|
|
const baseUrl = ensureUrl(sandbox.getHost(port));
|
|
await waitForHealth({ baseUrl, token });
|
|
logInspectorUrl({ baseUrl, token });
|
|
|
|
const cleanup = async () => {
|
|
try {
|
|
await sandbox.kill();
|
|
} catch {
|
|
// ignore cleanup errors
|
|
}
|
|
};
|
|
|
|
return {
|
|
baseUrl,
|
|
token,
|
|
cleanup,
|
|
};
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const { baseUrl, token, cleanup } = await setupE2BSandboxAgent();
|
|
|
|
const exitHandler = async () => {
|
|
await cleanup();
|
|
process.exit(0);
|
|
};
|
|
|
|
process.on("SIGINT", () => {
|
|
void exitHandler();
|
|
});
|
|
process.on("SIGTERM", () => {
|
|
void exitHandler();
|
|
});
|
|
|
|
await runPrompt({ baseUrl, token });
|
|
}
|
|
|
|
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|
|
}
|