mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-15 07:04:48 +00:00
wip
This commit is contained in:
parent
34d4f3693e
commit
29b159ca20
28 changed files with 2138 additions and 395 deletions
28
examples/daytona/daytona.test.ts
Normal file
28
examples/daytona/daytona.test.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { describe, it, expect } from "vitest";
|
||||
import { buildHeaders } from "../shared/sandbox-agent-client.ts";
|
||||
import { setupDaytonaSandboxAgent } from "./daytona.ts";
|
||||
|
||||
const shouldRun = Boolean(process.env.DAYTONA_API_KEY);
|
||||
const timeoutMs = Number.parseInt(process.env.SANDBOX_TEST_TIMEOUT_MS || "", 10) || 300_000;
|
||||
|
||||
const testFn = shouldRun ? it : it.skip;
|
||||
|
||||
describe("daytona example", () => {
|
||||
testFn(
|
||||
"starts sandbox-agent and responds to /v1/health",
|
||||
async () => {
|
||||
const { baseUrl, token, extraHeaders, cleanup } = await setupDaytonaSandboxAgent();
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/v1/health`, {
|
||||
headers: buildHeaders({ token, extraHeaders }),
|
||||
});
|
||||
expect(response.ok).toBe(true);
|
||||
const data = await response.json();
|
||||
expect(data.status).toBe("ok");
|
||||
} finally {
|
||||
await cleanup();
|
||||
}
|
||||
},
|
||||
timeoutMs
|
||||
);
|
||||
});
|
||||
82
examples/daytona/daytona.ts
Normal file
82
examples/daytona/daytona.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import { Daytona } from "@daytonaio/sdk";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import {
|
||||
ensureUrl,
|
||||
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 = 3000;
|
||||
|
||||
export async function setupDaytonaSandboxAgent(): Promise<{
|
||||
baseUrl: string;
|
||||
token: string;
|
||||
extraHeaders: Record<string, string>;
|
||||
cleanup: () => Promise<void>;
|
||||
}> {
|
||||
const token = process.env.SANDBOX_TOKEN || "";
|
||||
const port = Number.parseInt(process.env.SANDBOX_PORT || "", 10) || DEFAULT_PORT;
|
||||
const language = process.env.DAYTONA_LANGUAGE || "typescript";
|
||||
|
||||
const daytona = new Daytona();
|
||||
const sandbox = await daytona.create({
|
||||
language,
|
||||
});
|
||||
|
||||
await sandbox.process.executeCommand(`bash -lc "${INSTALL_SCRIPT}"`);
|
||||
|
||||
const tokenFlag = token ? "--token $SANDBOX_TOKEN" : "--no-token";
|
||||
const serverCommand = `nohup sandbox-agent server ${tokenFlag} --host 0.0.0.0 --port ${port} >/tmp/sandbox-agent.log 2>&1 &`;
|
||||
await sandbox.process.executeCommand(`bash -lc "${serverCommand}"`);
|
||||
|
||||
const preview = await sandbox.getPreviewLink(port);
|
||||
const extraHeaders: Record<string, string> = {};
|
||||
if (preview.token) {
|
||||
extraHeaders["x-daytona-preview-token"] = preview.token;
|
||||
}
|
||||
extraHeaders["x-daytona-skip-preview-warning"] = "true";
|
||||
|
||||
const baseUrl = ensureUrl(preview.url);
|
||||
await waitForHealth({ baseUrl, token, extraHeaders });
|
||||
|
||||
const cleanup = async () => {
|
||||
try {
|
||||
await sandbox.delete(60);
|
||||
} catch {
|
||||
// ignore cleanup errors
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
baseUrl,
|
||||
token,
|
||||
extraHeaders,
|
||||
cleanup,
|
||||
};
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const { baseUrl, token, extraHeaders, cleanup } = await setupDaytonaSandboxAgent();
|
||||
|
||||
const exitHandler = async () => {
|
||||
await cleanup();
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
void exitHandler();
|
||||
});
|
||||
process.on("SIGTERM", () => {
|
||||
void exitHandler();
|
||||
});
|
||||
|
||||
await runPrompt({ baseUrl, token, extraHeaders });
|
||||
}
|
||||
|
||||
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