mirror of
https://github.com/harivansh-afk/sandbox-agent.git
synced 2026-04-20 04:02:43 +00:00
Use prebuilt provider base images
This commit is contained in:
parent
5a3e185d1c
commit
c2e5dd6038
4 changed files with 209 additions and 79 deletions
|
|
@ -1,28 +1,67 @@
|
|||
import { Daytona } from "@daytonaio/sdk";
|
||||
import { Daytona, Image } from "@daytonaio/sdk";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { SandboxAgent } from "sandbox-agent";
|
||||
import { detectAgent, buildInspectorUrl, generateInstallCommand } from "@sandbox-agent/example-shared";
|
||||
import {
|
||||
SANDBOX_AGENT_IMAGE,
|
||||
SANDBOX_AGENT_INSTALL_VERSION,
|
||||
buildCredentialEnv,
|
||||
buildInspectorUrl,
|
||||
detectAgent,
|
||||
generateBaseImageDockerfile,
|
||||
getPreinstallComponents,
|
||||
} from "@sandbox-agent/example-shared";
|
||||
|
||||
const daytona = new Daytona();
|
||||
|
||||
const envVars: Record<string, string> = {};
|
||||
if (process.env.ANTHROPIC_API_KEY) envVars.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
|
||||
if (process.env.OPENAI_API_KEY) envVars.OPENAI_API_KEY = process.env.OPENAI_API_KEY;
|
||||
const envVars = buildCredentialEnv();
|
||||
const agent = detectAgent();
|
||||
const components = getPreinstallComponents(agent);
|
||||
const componentSuffix = components.length > 0 ? components.join("-") : "base";
|
||||
const baseImage = process.env.SANDBOX_AGENT_DAYTONA_IMAGE ?? SANDBOX_AGENT_IMAGE;
|
||||
const snapshotName = process.env.SANDBOX_AGENT_DAYTONA_SNAPSHOT ?? `sandbox-agent-${SANDBOX_AGENT_INSTALL_VERSION.replaceAll(".", "-")}-${componentSuffix}`;
|
||||
|
||||
// Use default image and install sandbox-agent at runtime (faster startup, no snapshot build)
|
||||
console.log("Creating Daytona sandbox...");
|
||||
const sandbox = await daytona.create({ envVars, autoStopInterval: 0 });
|
||||
async function ensureSnapshot(name: string) {
|
||||
try {
|
||||
return await daytona.snapshot.get(name);
|
||||
} catch {
|
||||
console.log(`Building Daytona snapshot ${name} from ${baseImage}...`);
|
||||
const dockerfileDir = fs.mkdtempSync(path.join(os.tmpdir(), "sandbox-agent-daytona-"));
|
||||
const dockerfilePath = path.join(dockerfileDir, "Dockerfile");
|
||||
fs.writeFileSync(
|
||||
dockerfilePath,
|
||||
generateBaseImageDockerfile({
|
||||
image: baseImage,
|
||||
components,
|
||||
}),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
// Install sandbox-agent and start server
|
||||
console.log("Installing sandbox-agent...");
|
||||
await sandbox.process.executeCommand(generateInstallCommand({ components: ["claude", "codex"] }));
|
||||
const image = Image.fromDockerfile(dockerfilePath)
|
||||
.workdir("/home/sandbox")
|
||||
.entrypoint(["sandbox-agent", "server", "--no-token", "--host", "0.0.0.0", "--port", "3000"]);
|
||||
|
||||
await sandbox.process.executeCommand("nohup sandbox-agent server --no-token --host 0.0.0.0 --port 3000 >/tmp/sandbox-agent.log 2>&1 &");
|
||||
try {
|
||||
const snapshot = await daytona.snapshot.create({ name, image }, { timeout: 180, onLogs: (line) => console.log(line) });
|
||||
|
||||
return await daytona.snapshot.activate(snapshot).catch(() => snapshot);
|
||||
} finally {
|
||||
fs.rmSync(dockerfileDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const snapshot = await ensureSnapshot(snapshotName);
|
||||
|
||||
console.log(`Creating Daytona sandbox from snapshot ${snapshot.name}...`);
|
||||
const sandbox = await daytona.create({ envVars, snapshot: snapshot.name, autoStopInterval: 0 }, { timeout: 180 });
|
||||
|
||||
const baseUrl = (await sandbox.getSignedPreviewUrl(3000, 4 * 60 * 60)).url;
|
||||
|
||||
console.log("Connecting to server...");
|
||||
const client = await SandboxAgent.connect({ baseUrl });
|
||||
const session = await client.createSession({ agent: detectAgent(), sessionInit: { cwd: "/home/daytona", mcpServers: [] } });
|
||||
const session = await client.createSession({ agent, sessionInit: { cwd: "/home/sandbox", mcpServers: [] } });
|
||||
const sessionId = session.id;
|
||||
|
||||
console.log(` UI: ${buildInspectorUrl({ baseUrl, sessionId })}`);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue