@agentproto/sandbox-e2b 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +46 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.mjs +106 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 agentproto contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @agentproto/sandbox-e2b
|
|
2
|
+
|
|
3
|
+
e2b `SandboxProvider` for `@agentproto/sandbox`. Boots the pre-built
|
|
4
|
+
`agentproto-workstation` e2b template, starts the agentproto daemon inside
|
|
5
|
+
it, and exposes its MCP endpoint as a URL — so
|
|
6
|
+
`createSandboxAgentSessionHost` can hand it straight to
|
|
7
|
+
`connectDaemonAgentSessionHost` (`@agentproto/worktree`) and run any
|
|
8
|
+
`AgentStep` inside the sandbox, unchanged.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { createSandboxAgentSessionHost } from "@agentproto/sandbox"
|
|
14
|
+
import { e2bSandboxProvider } from "@agentproto/sandbox-e2b"
|
|
15
|
+
import { runWorkflow } from "@agentproto/workflow-runtime"
|
|
16
|
+
import { worktreeAgentWorkflow } from "@agentproto/worktree"
|
|
17
|
+
|
|
18
|
+
const host = await createSandboxAgentSessionHost({
|
|
19
|
+
provider: e2bSandboxProvider,
|
|
20
|
+
spec: { provider: "e2b", config: {} },
|
|
21
|
+
secrets: { slugs: ["OPENROUTER_API_KEY"] },
|
|
22
|
+
})
|
|
23
|
+
try {
|
|
24
|
+
await runWorkflow({ workflow: worktreeAgentWorkflow, input, agents: host })
|
|
25
|
+
} finally {
|
|
26
|
+
await host.stop()
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Requires `E2B_API_KEY` in the host process's environment.
|
|
31
|
+
|
|
32
|
+
## Notes
|
|
33
|
+
|
|
34
|
+
- The daemon's own origin allowlist defaults to `localhost:*`; this provider
|
|
35
|
+
opens it for the sandbox's own public host (`--allow-origin
|
|
36
|
+
https://<getHost>`) so the host process can reach it over
|
|
37
|
+
`https://<getHost>/mcp`.
|
|
38
|
+
- The pre-built `agentproto-workstation` template can lag behind the latest
|
|
39
|
+
`@agentproto/cli` release. This provider runs `npm i -g
|
|
40
|
+
@agentproto/cli@latest` before starting the daemon (set `updateCliOnBoot:
|
|
41
|
+
false` in `spec.config` to skip it — e.g. once the template is rebuilt
|
|
42
|
+
against a current release, which is the cleaner long-term fix).
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
MIT — see [LICENSE](./LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { SandboxProvider } from '@agentproto/sandbox';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* e2b `SandboxProvider` — boots the pre-built `agentproto-workstation`
|
|
5
|
+
* template, ensures the agentproto daemon is listening on it, and exposes
|
|
6
|
+
* the daemon's `/mcp` endpoint via `sandbox.getHost()` so
|
|
7
|
+
* `createSandboxAgentSessionHost` (`@agentproto/sandbox`) can hand it
|
|
8
|
+
* straight to `connectDaemonAgentSessionHost`.
|
|
9
|
+
*
|
|
10
|
+
* `getHost(port)` returns a publicly reachable hostname for that port
|
|
11
|
+
* directly — e2b's per-sandbox auth token (`SandboxOpts.secure`) gates the
|
|
12
|
+
* SDK's own control-plane (envd) traffic, not arbitrary listening ports the
|
|
13
|
+
* sandbox process opens, so no extra auth header is threaded through here.
|
|
14
|
+
* The daemon's OWN origin allowlist is a separate gate, though: it defaults
|
|
15
|
+
* to `localhost:*`, which rejects a connection from this host process
|
|
16
|
+
* against the sandbox's public `https://<getHost>` origin. `--allow-origin`
|
|
17
|
+
* is passed the sandbox's own getHost origin to open that gate.
|
|
18
|
+
*
|
|
19
|
+
* The workstation template MAY already autostart the daemon; since that
|
|
20
|
+
* can't be verified without a live template, this provider checks health
|
|
21
|
+
* first and only issues the start command when the daemon isn't already
|
|
22
|
+
* responding — correct either way. When it does start the daemon, it first
|
|
23
|
+
* updates the baked `@agentproto/cli` (the pre-built template can lag
|
|
24
|
+
* behind — verified stale against a live template) so callers aren't stuck
|
|
25
|
+
* on whatever agentproto version the template was last baked with.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/** Pre-built template that bakes @agentproto/cli + adapters + node + git. */
|
|
29
|
+
declare const DEFAULT_TEMPLATE = "53ybr99wdfgoebi9nee8";
|
|
30
|
+
/** `e2bSandboxProvider.boot`/`.connect` — see module docs. */
|
|
31
|
+
declare const e2bSandboxProvider: SandboxProvider;
|
|
32
|
+
|
|
33
|
+
export { DEFAULT_TEMPLATE, e2bSandboxProvider };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Sandbox } from 'e2b';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @agentproto/sandbox-e2b v0.1.0-alpha
|
|
5
|
+
* e2b SandboxProvider for @agentproto/sandbox.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
var DEFAULT_TEMPLATE = "53ybr99wdfgoebi9nee8";
|
|
9
|
+
var DEFAULT_PORT = 18790;
|
|
10
|
+
var DEFAULT_WORKSPACE = "/home/user";
|
|
11
|
+
var HEALTH_PROBE_TIMEOUT_MS = 3e3;
|
|
12
|
+
var DAEMON_READY_TIMEOUT_MS = 3e4;
|
|
13
|
+
var POLL_INTERVAL_MS = 500;
|
|
14
|
+
var UPDATE_CLI_TIMEOUT_MS = 12e4;
|
|
15
|
+
function readE2bConfig(spec) {
|
|
16
|
+
const config = spec.config ?? {};
|
|
17
|
+
return {
|
|
18
|
+
template: typeof config.template === "string" ? config.template : void 0,
|
|
19
|
+
port: typeof config.port === "number" ? config.port : void 0,
|
|
20
|
+
workspace: typeof config.workspace === "string" ? config.workspace : void 0,
|
|
21
|
+
timeoutMs: typeof config.timeoutMs === "number" ? config.timeoutMs : void 0,
|
|
22
|
+
healthProbeTimeoutMs: typeof config.healthProbeTimeoutMs === "number" ? config.healthProbeTimeoutMs : void 0,
|
|
23
|
+
daemonReadyTimeoutMs: typeof config.daemonReadyTimeoutMs === "number" ? config.daemonReadyTimeoutMs : void 0,
|
|
24
|
+
pollIntervalMs: typeof config.pollIntervalMs === "number" ? config.pollIntervalMs : void 0,
|
|
25
|
+
updateCliOnBoot: typeof config.updateCliOnBoot === "boolean" ? config.updateCliOnBoot : void 0,
|
|
26
|
+
updateCliTimeoutMs: typeof config.updateCliTimeoutMs === "number" ? config.updateCliTimeoutMs : void 0
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async function ensureDaemonHealthy(sandbox, host, port, workspace, config, env) {
|
|
30
|
+
const healthUrl = `https://${host}/health`;
|
|
31
|
+
const healthProbeTimeoutMs = config.healthProbeTimeoutMs ?? HEALTH_PROBE_TIMEOUT_MS;
|
|
32
|
+
const daemonReadyTimeoutMs = config.daemonReadyTimeoutMs ?? DAEMON_READY_TIMEOUT_MS;
|
|
33
|
+
const pollIntervalMs = config.pollIntervalMs ?? POLL_INTERVAL_MS;
|
|
34
|
+
const alreadyUp = await probeHealth(healthUrl, healthProbeTimeoutMs, pollIntervalMs);
|
|
35
|
+
if (alreadyUp) return;
|
|
36
|
+
if (config.updateCliOnBoot ?? true) {
|
|
37
|
+
await sandbox.commands.run("sudo npm i -g @agentproto/cli@latest", {
|
|
38
|
+
envs: env,
|
|
39
|
+
timeoutMs: config.updateCliTimeoutMs ?? UPDATE_CLI_TIMEOUT_MS
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
await sandbox.commands.run(
|
|
43
|
+
`agentproto serve --port ${port} --bind 0.0.0.0 --workspace ${workspace} --allow-origin https://${host}`,
|
|
44
|
+
{ background: true, envs: env }
|
|
45
|
+
);
|
|
46
|
+
const ready = await probeHealth(healthUrl, daemonReadyTimeoutMs, pollIntervalMs);
|
|
47
|
+
if (!ready) {
|
|
48
|
+
await sandbox.kill();
|
|
49
|
+
throw new Error(
|
|
50
|
+
`@agentproto/sandbox-e2b: agentproto daemon did not become healthy at ${healthUrl} within ${daemonReadyTimeoutMs}ms (sandbox ${sandbox.sandboxId}).`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function toBootedSandbox(sandbox, host) {
|
|
55
|
+
return {
|
|
56
|
+
mcpUrl: `https://${host}/mcp`,
|
|
57
|
+
sandboxId: sandbox.sandboxId,
|
|
58
|
+
async stop() {
|
|
59
|
+
await sandbox.kill();
|
|
60
|
+
},
|
|
61
|
+
async pause() {
|
|
62
|
+
await sandbox.pause({ keepMemory: true });
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
var e2bSandboxProvider = {
|
|
67
|
+
async boot(spec, opts) {
|
|
68
|
+
const config = readE2bConfig(spec);
|
|
69
|
+
const template = config.template ?? DEFAULT_TEMPLATE;
|
|
70
|
+
const port = config.port ?? DEFAULT_PORT;
|
|
71
|
+
const workspace = config.workspace ?? DEFAULT_WORKSPACE;
|
|
72
|
+
const sandbox = await Sandbox.create(template, {
|
|
73
|
+
apiKey: process.env.E2B_API_KEY,
|
|
74
|
+
envs: opts.env,
|
|
75
|
+
...config.timeoutMs !== void 0 ? { timeoutMs: config.timeoutMs } : {}
|
|
76
|
+
});
|
|
77
|
+
const host = sandbox.getHost(port);
|
|
78
|
+
await ensureDaemonHealthy(sandbox, host, port, workspace, config, opts.env);
|
|
79
|
+
return toBootedSandbox(sandbox, host);
|
|
80
|
+
},
|
|
81
|
+
async connect(sandboxId, spec, opts) {
|
|
82
|
+
const config = readE2bConfig(spec);
|
|
83
|
+
const port = config.port ?? DEFAULT_PORT;
|
|
84
|
+
const workspace = config.workspace ?? DEFAULT_WORKSPACE;
|
|
85
|
+
const sandbox = await Sandbox.connect(sandboxId, { apiKey: process.env.E2B_API_KEY });
|
|
86
|
+
const host = sandbox.getHost(port);
|
|
87
|
+
await ensureDaemonHealthy(sandbox, host, port, workspace, config, opts.env);
|
|
88
|
+
return toBootedSandbox(sandbox, host);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
async function probeHealth(url, timeoutMs, pollIntervalMs) {
|
|
92
|
+
const deadline = Date.now() + timeoutMs;
|
|
93
|
+
for (; ; ) {
|
|
94
|
+
try {
|
|
95
|
+
const res = await fetch(url, { signal: AbortSignal.timeout(pollIntervalMs) });
|
|
96
|
+
if (res.ok) return true;
|
|
97
|
+
} catch {
|
|
98
|
+
}
|
|
99
|
+
if (Date.now() >= deadline) return false;
|
|
100
|
+
await new Promise((resolve) => setTimeout(resolve, pollIntervalMs));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { DEFAULT_TEMPLATE, e2bSandboxProvider };
|
|
105
|
+
//# sourceMappingURL=index.mjs.map
|
|
106
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/provider.ts"],"names":[],"mappings":";;;;;;;AA6BO,IAAM,gBAAA,GAAmB;AAGhC,IAAM,YAAA,GAAe,KAAA;AACrB,IAAM,iBAAA,GAAoB,YAAA;AAC1B,IAAM,uBAAA,GAA0B,GAAA;AAChC,IAAM,uBAAA,GAA0B,GAAA;AAChC,IAAM,gBAAA,GAAmB,GAAA;AACzB,IAAM,qBAAA,GAAwB,IAAA;AA0B9B,SAAS,cAAc,IAAA,EAAqC;AAC1D,EAAA,MAAM,MAAA,GAAU,IAAA,CAAK,MAAA,IAAU,EAAC;AAChC,EAAA,OAAO;AAAA,IACL,UAAU,OAAO,MAAA,CAAO,QAAA,KAAa,QAAA,GAAW,OAAO,QAAA,GAAW,MAAA;AAAA,IAClE,MAAM,OAAO,MAAA,CAAO,IAAA,KAAS,QAAA,GAAW,OAAO,IAAA,GAAO,MAAA;AAAA,IACtD,WAAW,OAAO,MAAA,CAAO,SAAA,KAAc,QAAA,GAAW,OAAO,SAAA,GAAY,MAAA;AAAA,IACrE,WAAW,OAAO,MAAA,CAAO,SAAA,KAAc,QAAA,GAAW,OAAO,SAAA,GAAY,MAAA;AAAA,IACrE,sBACE,OAAO,MAAA,CAAO,oBAAA,KAAyB,QAAA,GAAW,OAAO,oBAAA,GAAuB,MAAA;AAAA,IAClF,sBACE,OAAO,MAAA,CAAO,oBAAA,KAAyB,QAAA,GAAW,OAAO,oBAAA,GAAuB,MAAA;AAAA,IAClF,gBAAgB,OAAO,MAAA,CAAO,cAAA,KAAmB,QAAA,GAA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EAAS,cAAc,CAAC,CAAA;AAAA,EAClE;AACF","file":"index.mjs","sourcesContent":["/**\n * e2b `SandboxProvider` — boots the pre-built `agentproto-workstation`\n * template, ensures the agentproto daemon is listening on it, and exposes\n * the daemon's `/mcp` endpoint via `sandbox.getHost()` so\n * `createSandboxAgentSessionHost` (`@agentproto/sandbox`) can hand it\n * straight to `connectDaemonAgentSessionHost`.\n *\n * `getHost(port)` returns a publicly reachable hostname for that port\n * directly — e2b's per-sandbox auth token (`SandboxOpts.secure`) gates the\n * SDK's own control-plane (envd) traffic, not arbitrary listening ports the\n * sandbox process opens, so no extra auth header is threaded through here.\n * The daemon's OWN origin allowlist is a separate gate, though: it defaults\n * to `localhost:*`, which rejects a connection from this host process\n * against the sandbox's public `https://<getHost>` origin. `--allow-origin`\n * is passed the sandbox's own getHost origin to open that gate.\n *\n * The workstation template MAY already autostart the daemon; since that\n * can't be verified without a live template, this provider checks health\n * first and only issues the start command when the daemon isn't already\n * responding — correct either way. When it does start the daemon, it first\n * updates the baked `@agentproto/cli` (the pre-built template can lag\n * behind — verified stale against a live template) so callers aren't stuck\n * on whatever agentproto version the template was last baked with.\n */\n\nimport { Sandbox } from \"e2b\"\nimport type { BootedSandbox, SandboxBootOpts, SandboxProvider, SandboxSpec } from \"@agentproto/sandbox\"\n\n/** Pre-built template that bakes @agentproto/cli + adapters + node + git. */\nexport const DEFAULT_TEMPLATE = \"53ybr99wdfgoebi9nee8\"\n\n/** `serve.ts`'s default port (`DEFAULT_MCP_URL` in `@agentproto/harness`). */\nconst DEFAULT_PORT = 18790\nconst DEFAULT_WORKSPACE = \"/home/user\"\nconst HEALTH_PROBE_TIMEOUT_MS = 3_000\nconst DAEMON_READY_TIMEOUT_MS = 30_000\nconst POLL_INTERVAL_MS = 500\nconst UPDATE_CLI_TIMEOUT_MS = 120_000\n\ninterface E2bSandboxConfig {\n /** Template name or id. Defaults to the `agentproto-workstation` template. */\n template?: string\n /** Port the agentproto daemon listens on inside the sandbox. Default 18790. */\n port?: number\n /** `--workspace` passed to `agentproto serve`. Default `/home/user`. */\n workspace?: string\n /** Sandbox lifetime cap, forwarded to `Sandbox.create`. */\n timeoutMs?: number\n /** How long to wait for the health probe before assuming the daemon isn't autostarted. */\n healthProbeTimeoutMs?: number\n /** How long to wait for the daemon to become healthy after starting it. */\n daemonReadyTimeoutMs?: number\n /** Delay between health-probe attempts. */\n pollIntervalMs?: number\n /** Run `npm i -g @agentproto/cli@latest` before starting the daemon, so a\n * stale template bake doesn't pin callers to an old agentproto version.\n * Default true. Only runs when this provider is the one starting the\n * daemon (skipped when the health probe finds it already autostarted). */\n updateCliOnBoot?: boolean\n /** Timeout for the `npm i -g` update command. Default 120s. */\n updateCliTimeoutMs?: number\n}\n\nfunction readE2bConfig(spec: SandboxSpec): E2bSandboxConfig {\n const config = (spec.config ?? {}) as Record<string, unknown>\n return {\n template: typeof config.template === \"string\" ? config.template : undefined,\n port: typeof config.port === \"number\" ? config.port : undefined,\n workspace: typeof config.workspace === \"string\" ? config.workspace : undefined,\n timeoutMs: typeof config.timeoutMs === \"number\" ? config.timeoutMs : undefined,\n healthProbeTimeoutMs:\n typeof config.healthProbeTimeoutMs === \"number\" ? config.healthProbeTimeoutMs : undefined,\n daemonReadyTimeoutMs:\n typeof config.daemonReadyTimeoutMs === \"number\" ? config.daemonReadyTimeoutMs : undefined,\n pollIntervalMs: typeof config.pollIntervalMs === \"number\" ? config.pollIntervalMs : undefined,\n updateCliOnBoot: typeof config.updateCliOnBoot === \"boolean\" ? config.updateCliOnBoot : undefined,\n updateCliTimeoutMs:\n typeof config.updateCliTimeoutMs === \"number\" ? config.updateCliTimeoutMs : undefined,\n }\n}\n\n/**\n * Probe health, and — if the daemon isn't already up — update the (possibly\n * stale) baked CLI and start it, then re-probe. Shared by `boot` (a fresh\n * template's autostart may not have fired yet) and `connect` (a resumed box\n * may wake with a dead daemon process — see PR3 risk \"stale daemon on\n * reconnect\"). Throws (after killing the sandbox) if the daemon never comes\n * up.\n */\nasync function ensureDaemonHealthy(\n sandbox: Sandbox,\n host: string,\n port: number,\n workspace: string,\n config: E2bSandboxConfig,\n env: Record<string, string>,\n): Promise<void> {\n const healthUrl = `https://${host}/health`\n const healthProbeTimeoutMs = config.healthProbeTimeoutMs ?? HEALTH_PROBE_TIMEOUT_MS\n const daemonReadyTimeoutMs = config.daemonReadyTimeoutMs ?? DAEMON_READY_TIMEOUT_MS\n const pollIntervalMs = config.pollIntervalMs ?? POLL_INTERVAL_MS\n\n const alreadyUp = await probeHealth(healthUrl, healthProbeTimeoutMs, pollIntervalMs)\n if (alreadyUp) return\n\n if (config.updateCliOnBoot ?? true) {\n await sandbox.commands.run(\"sudo npm i -g @agentproto/cli@latest\", {\n envs: env,\n timeoutMs: config.updateCliTimeoutMs ?? UPDATE_CLI_TIMEOUT_MS,\n })\n }\n await sandbox.commands.run(\n `agentproto serve --port ${port} --bind 0.0.0.0 --workspace ${workspace} --allow-origin https://${host}`,\n { background: true, envs: env },\n )\n const ready = await probeHealth(healthUrl, daemonReadyTimeoutMs, pollIntervalMs)\n if (!ready) {\n await sandbox.kill()\n throw new Error(\n `@agentproto/sandbox-e2b: agentproto daemon did not become healthy at ${healthUrl} ` +\n `within ${daemonReadyTimeoutMs}ms (sandbox ${sandbox.sandboxId}).`,\n )\n }\n}\n\n/** Wrap a healthy sandbox handle into the `BootedSandbox` shape common to\n * `boot` and `connect`. `pause()` always keeps the full memory snapshot\n * (`keepMemory: true`, the SDK default) — a filesystem-only pause would\n * cold-boot the box on resume, dropping the running agentproto daemon and\n * any open connections (PR3 risk \"e2b pause loses in-memory state\"). */\nfunction toBootedSandbox(sandbox: Awaited<ReturnType<typeof Sandbox.create>>, host: string): BootedSandbox {\n return {\n mcpUrl: `https://${host}/mcp`,\n sandboxId: sandbox.sandboxId,\n async stop(): Promise<void> {\n await sandbox.kill()\n },\n async pause(): Promise<void> {\n await sandbox.pause({ keepMemory: true })\n },\n }\n}\n\n/** `e2bSandboxProvider.boot`/`.connect` — see module docs. */\nexport const e2bSandboxProvider: SandboxProvider = {\n async boot(spec: SandboxSpec, opts: SandboxBootOpts): Promise<BootedSandbox> {\n const config = readE2bConfig(spec)\n const template = config.template ?? DEFAULT_TEMPLATE\n const port = config.port ?? DEFAULT_PORT\n const workspace = config.workspace ?? DEFAULT_WORKSPACE\n\n const sandbox = await Sandbox.create(template, {\n apiKey: process.env.E2B_API_KEY,\n envs: opts.env,\n ...(config.timeoutMs !== undefined ? { timeoutMs: config.timeoutMs } : {}),\n })\n\n const host = sandbox.getHost(port)\n await ensureDaemonHealthy(sandbox, host, port, workspace, config, opts.env)\n return toBootedSandbox(sandbox, host)\n },\n\n async connect(sandboxId: string, spec: SandboxSpec, opts: SandboxBootOpts): Promise<BootedSandbox> {\n const config = readE2bConfig(spec)\n const port = config.port ?? DEFAULT_PORT\n const workspace = config.workspace ?? DEFAULT_WORKSPACE\n\n const sandbox = await Sandbox.connect(sandboxId, { apiKey: process.env.E2B_API_KEY })\n\n const host = sandbox.getHost(port)\n await ensureDaemonHealthy(sandbox, host, port, workspace, config, opts.env)\n return toBootedSandbox(sandbox, host)\n },\n}\n\n/** Poll `url` until it responds OK, or return false once `timeoutMs` elapses. */\nasync function probeHealth(url: string, timeoutMs: number, pollIntervalMs: number): Promise<boolean> {\n const deadline = Date.now() + timeoutMs\n for (;;) {\n try {\n const res = await fetch(url, { signal: AbortSignal.timeout(pollIntervalMs) })\n if (res.ok) return true\n } catch {\n // not up yet — fall through to the deadline check\n }\n if (Date.now() >= deadline) return false\n await new Promise(resolve => setTimeout(resolve, pollIntervalMs))\n }\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agentproto/sandbox-e2b",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "@agentproto/sandbox-e2b — e2b `SandboxProvider` for @agentproto/sandbox. Boots the `agentproto-workstation` e2b template, starts the agentproto daemon inside it, and exposes its MCP endpoint as a URL so any AgentSessionHost consumer (worktreeAgentWorkflow, worktree-agent CLI) can run a coding-agent step inside the sandbox.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agentproto",
|
|
7
|
+
"aip-36",
|
|
8
|
+
"sandbox",
|
|
9
|
+
"e2b",
|
|
10
|
+
"open-standard",
|
|
11
|
+
"agentic"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://agentproto.sh/docs/aip-36",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/agentproto/ts",
|
|
17
|
+
"directory": "packages/sandbox-e2b"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/agentproto/ts/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "dist/index.mjs",
|
|
25
|
+
"module": "dist/index.mjs",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.mjs",
|
|
31
|
+
"default": "./dist/index.mjs"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"e2b": "^2.31.0",
|
|
45
|
+
"@agentproto/sandbox": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^25.6.2",
|
|
49
|
+
"tsup": "^8.5.1",
|
|
50
|
+
"typescript": "^5.9.3",
|
|
51
|
+
"vitest": "^3.2.4",
|
|
52
|
+
"@agentproto/tooling": "0.1.0-alpha.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"dev": "tsup --watch",
|
|
56
|
+
"build": "tsup",
|
|
57
|
+
"clean": "rm -rf dist",
|
|
58
|
+
"check-types": "tsc --noEmit",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"test:watch": "vitest"
|
|
61
|
+
}
|
|
62
|
+
}
|