mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-16 21:03:46 +00:00
feat: move api cli commands under api subcommand
This commit is contained in:
parent
0ef3b998bb
commit
8a91b8e9aa
22 changed files with 351 additions and 246 deletions
89
examples/e2b/src/e2b.ts
Normal file
89
examples/e2b/src/e2b.ts
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import { Sandbox } from "@e2b/code-interpreter";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import {
|
||||
ensureUrl,
|
||||
logInspectorUrl,
|
||||
runPrompt,
|
||||
waitForHealth,
|
||||
} from "@sandbox-agent/example-shared";
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue