mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-16 14:01:09 +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
34
examples/vercel/src/vercel-sandbox.test.ts
Normal file
34
examples/vercel/src/vercel-sandbox.test.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { buildHeaders } from "../shared/sandbox-agent-client.ts";
|
||||
import { setupVercelSandboxAgent } from "./vercel-sandbox.ts";
|
||||
|
||||
const hasOidc = Boolean(process.env.VERCEL_OIDC_TOKEN);
|
||||
const hasAccess = Boolean(
|
||||
process.env.VERCEL_TOKEN &&
|
||||
process.env.VERCEL_TEAM_ID &&
|
||||
process.env.VERCEL_PROJECT_ID
|
||||
);
|
||||
const shouldRun = hasOidc || hasAccess;
|
||||
const timeoutMs = Number.parseInt(process.env.SANDBOX_TEST_TIMEOUT_MS || "", 10) || 300_000;
|
||||
|
||||
const testFn = shouldRun ? it : it.skip;
|
||||
|
||||
describe("vercel sandbox example", () => {
|
||||
testFn(
|
||||
"starts sandbox-agent and responds to /v1/health",
|
||||
async () => {
|
||||
const { baseUrl, token, cleanup } = await setupVercelSandboxAgent();
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/v1/health`, {
|
||||
headers: buildHeaders({ token }),
|
||||
});
|
||||
expect(response.ok).toBe(true);
|
||||
const data = await response.json();
|
||||
expect(data.status).toBe("ok");
|
||||
} finally {
|
||||
await cleanup();
|
||||
}
|
||||
},
|
||||
timeoutMs
|
||||
);
|
||||
});
|
||||
105
examples/vercel/src/vercel-sandbox.ts
Normal file
105
examples/vercel/src/vercel-sandbox.ts
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import { Sandbox } from "@vercel/sandbox";
|
||||
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 VercelSandboxOptions = {
|
||||
runtime: string;
|
||||
ports: number[];
|
||||
token?: string;
|
||||
teamId?: string;
|
||||
projectId?: string;
|
||||
};
|
||||
|
||||
export async function setupVercelSandboxAgent(): 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 runtime = process.env.VERCEL_RUNTIME || "node24";
|
||||
|
||||
const createOptions: VercelSandboxOptions = {
|
||||
runtime,
|
||||
ports: [port],
|
||||
};
|
||||
|
||||
const accessToken = process.env.VERCEL_TOKEN;
|
||||
const teamId = process.env.VERCEL_TEAM_ID;
|
||||
const projectId = process.env.VERCEL_PROJECT_ID;
|
||||
if (accessToken && teamId && projectId) {
|
||||
createOptions.token = accessToken;
|
||||
createOptions.teamId = teamId;
|
||||
createOptions.projectId = projectId;
|
||||
}
|
||||
|
||||
const sandbox = await Sandbox.create(createOptions);
|
||||
|
||||
await sandbox.runCommand({
|
||||
cmd: "bash",
|
||||
args: ["-lc", INSTALL_SCRIPT],
|
||||
sudo: true,
|
||||
});
|
||||
|
||||
const tokenFlag = token ? "--token $SANDBOX_TOKEN" : "--no-token";
|
||||
await sandbox.runCommand({
|
||||
cmd: "bash",
|
||||
args: [
|
||||
"-lc",
|
||||
`SANDBOX_TOKEN=${token} sandbox-agent server ${tokenFlag} --host 0.0.0.0 --port ${port}`,
|
||||
],
|
||||
sudo: true,
|
||||
detached: true,
|
||||
});
|
||||
|
||||
const baseUrl = ensureUrl(sandbox.domain(port));
|
||||
await waitForHealth({ baseUrl, token });
|
||||
logInspectorUrl({ baseUrl, token });
|
||||
|
||||
const cleanup = async () => {
|
||||
try {
|
||||
await sandbox.stop();
|
||||
} catch {
|
||||
// ignore cleanup errors
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
baseUrl,
|
||||
token,
|
||||
cleanup,
|
||||
};
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const { baseUrl, token, cleanup } = await setupVercelSandboxAgent();
|
||||
|
||||
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