@chimp-labs/keel-ai-kit 0.0.0-stage → 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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +33 -2
- package/dist/agent.d.ts +365 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +886 -0
- package/dist/agent.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/posix-path.d.ts +3 -0
- package/dist/internal/posix-path.d.ts.map +1 -0
- package/dist/internal/posix-path.js +13 -0
- package/dist/internal/posix-path.js.map +1 -0
- package/dist/llm/providers.d.ts +81 -0
- package/dist/llm/providers.d.ts.map +1 -0
- package/dist/llm/providers.js +854 -0
- package/dist/llm/providers.js.map +1 -0
- package/dist/llm.d.ts +133 -0
- package/dist/llm.d.ts.map +1 -0
- package/dist/llm.js +164 -0
- package/dist/llm.js.map +1 -0
- package/dist/sandbox/providers.d.ts +74 -0
- package/dist/sandbox/providers.d.ts.map +1 -0
- package/dist/sandbox/providers.js +655 -0
- package/dist/sandbox/providers.js.map +1 -0
- package/dist/sandbox.d.ts +390 -0
- package/dist/sandbox.d.ts.map +1 -0
- package/dist/sandbox.js +1000 -0
- package/dist/sandbox.js.map +1 -0
- package/package.json +85 -3
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
import { Cause, Duration, Effect, Layer, Queue, Stream } from "effect";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { joinPosixPath, normalizePosixPath } from "../internal/posix-path.js";
|
|
5
|
+
import { SandboxError, SandboxFileNotFoundError, SandboxNotFoundError, SandboxRuntime, SandboxTimeoutError, } from "../sandbox.js";
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
// Shared Helpers
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
const toTimeoutMs = (input) => input === undefined
|
|
10
|
+
? undefined
|
|
11
|
+
: Math.max(1, Math.floor(Duration.toMillis(Duration.fromInputUnsafe(input))));
|
|
12
|
+
const shellQuote = (s) => `'${s.replace(/'/g, `'\\''`)}'`;
|
|
13
|
+
const defaultDockerRunner = (dockerBin) => (args, options) => new Promise((resolve, reject) => {
|
|
14
|
+
const child = spawn(dockerBin, [...args], {
|
|
15
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
16
|
+
});
|
|
17
|
+
const stdoutChunks = [];
|
|
18
|
+
const stderrChunks = [];
|
|
19
|
+
let timedOut = false;
|
|
20
|
+
let timer;
|
|
21
|
+
if (options?.timeoutMs !== undefined && options.timeoutMs > 0) {
|
|
22
|
+
timer = setTimeout(() => {
|
|
23
|
+
timedOut = true;
|
|
24
|
+
child.kill("SIGKILL");
|
|
25
|
+
}, options.timeoutMs);
|
|
26
|
+
}
|
|
27
|
+
child.stdout.on("data", (chunk) => stdoutChunks.push(chunk));
|
|
28
|
+
child.stderr.on("data", (chunk) => stderrChunks.push(chunk));
|
|
29
|
+
child.on("error", (err) => {
|
|
30
|
+
if (timer)
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
reject(err);
|
|
33
|
+
});
|
|
34
|
+
child.on("close", (code) => {
|
|
35
|
+
if (timer)
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
resolve({
|
|
38
|
+
exitCode: code ?? (timedOut ? 137 : 1),
|
|
39
|
+
stdout: Buffer.concat(stdoutChunks).toString("utf8"),
|
|
40
|
+
stderr: Buffer.concat(stderrChunks).toString("utf8"),
|
|
41
|
+
timedOut,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
if (options?.stdin !== undefined) {
|
|
45
|
+
child.stdin.write(typeof options.stdin === "string"
|
|
46
|
+
? Buffer.from(options.stdin, "utf8")
|
|
47
|
+
: Buffer.from(options.stdin));
|
|
48
|
+
}
|
|
49
|
+
child.stdin.end();
|
|
50
|
+
});
|
|
51
|
+
/**
|
|
52
|
+
* Production & local-dev `SandboxRuntime` powered by Docker (`runc`) or gVisor
|
|
53
|
+
* (`runsc`) with zero npm dependencies.
|
|
54
|
+
*
|
|
55
|
+
* - Enforces `--network=none`, `--cpus`, `--memory`, `--runtime=runsc`, and
|
|
56
|
+
* working directory isolation.
|
|
57
|
+
* - Supports streaming `execStream`, binary-safe `readFile`/`writeFile`, and
|
|
58
|
+
* guaranteed `docker rm -f` cleanup on `Scope` close.
|
|
59
|
+
*/
|
|
60
|
+
export const SandboxDockerLive = (options) => {
|
|
61
|
+
const ociRuntime = options?.runtime ?? "runc";
|
|
62
|
+
const dockerBin = options?.dockerBin ?? "docker";
|
|
63
|
+
const containerPrefix = options?.containerPrefix ?? "keel-sb";
|
|
64
|
+
const runner = options?.commandRunner ?? defaultDockerRunner(dockerBin);
|
|
65
|
+
const isGvisor = ociRuntime === "runsc";
|
|
66
|
+
const caps = {
|
|
67
|
+
provider: isGvisor ? "docker-gvisor" : "docker",
|
|
68
|
+
maxIsolation: isGvisor ? "gvisor" : "container",
|
|
69
|
+
nativeSnapshot: false,
|
|
70
|
+
memoryPause: false,
|
|
71
|
+
gpu: options?.gpu ?? false,
|
|
72
|
+
egressFirewall: isGvisor,
|
|
73
|
+
streamingExec: true,
|
|
74
|
+
};
|
|
75
|
+
const runCli = (sandboxName, instanceId, args, runOpts) => Effect.tryPromise({
|
|
76
|
+
try: () => runner(args, runOpts),
|
|
77
|
+
catch: (cause) => new SandboxError({
|
|
78
|
+
sandbox: sandboxName,
|
|
79
|
+
...(instanceId !== undefined ? { instanceId } : {}),
|
|
80
|
+
cause,
|
|
81
|
+
}),
|
|
82
|
+
});
|
|
83
|
+
const buildExecArgs = (handle, command, execOpts) => {
|
|
84
|
+
const args = ["exec", "-i"];
|
|
85
|
+
const cwd = execOpts?.cwd ?? handle.workdir;
|
|
86
|
+
if (cwd)
|
|
87
|
+
args.push("-w", cwd);
|
|
88
|
+
for (const [k, v] of Object.entries(execOpts?.env ?? {})) {
|
|
89
|
+
args.push("-e", `${k}=${v}`);
|
|
90
|
+
}
|
|
91
|
+
args.push(handle.id);
|
|
92
|
+
if (typeof command === "string") {
|
|
93
|
+
args.push("sh", "-c", command);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
args.push(...command);
|
|
97
|
+
}
|
|
98
|
+
return args;
|
|
99
|
+
};
|
|
100
|
+
return Layer.succeed(SandboxRuntime, SandboxRuntime.of({
|
|
101
|
+
capabilitiesFor: () => caps,
|
|
102
|
+
create: (request) => Effect.gen(function* () {
|
|
103
|
+
const safeName = request.sandboxName
|
|
104
|
+
.toLowerCase()
|
|
105
|
+
.replace(/[^a-z0-9-]+/g, "-")
|
|
106
|
+
.replace(/^-|-$/g, "");
|
|
107
|
+
const containerName = `${containerPrefix}-${safeName || "sb"}-${randomUUID().slice(0, 8)}`;
|
|
108
|
+
const args = [
|
|
109
|
+
"run",
|
|
110
|
+
"-d",
|
|
111
|
+
"--init",
|
|
112
|
+
"--name",
|
|
113
|
+
containerName,
|
|
114
|
+
"--label",
|
|
115
|
+
`keel.sandbox=${request.sandboxName}`,
|
|
116
|
+
"-w",
|
|
117
|
+
request.workdir,
|
|
118
|
+
];
|
|
119
|
+
if (ociRuntime && ociRuntime !== "runc") {
|
|
120
|
+
args.push("--runtime", ociRuntime);
|
|
121
|
+
}
|
|
122
|
+
if (request.network === "none") {
|
|
123
|
+
args.push("--network", "none");
|
|
124
|
+
}
|
|
125
|
+
if (request.cpu !== undefined) {
|
|
126
|
+
args.push("--cpus", String(request.cpu));
|
|
127
|
+
}
|
|
128
|
+
if (request.memoryMb !== undefined) {
|
|
129
|
+
args.push("--memory", `${request.memoryMb}m`);
|
|
130
|
+
}
|
|
131
|
+
if (request.gpu !== undefined && request.gpu !== false) {
|
|
132
|
+
args.push("--gpus", "all");
|
|
133
|
+
}
|
|
134
|
+
for (const [k, v] of Object.entries(request.env)) {
|
|
135
|
+
args.push("-e", `${k}=${v}`);
|
|
136
|
+
}
|
|
137
|
+
if (options?.extraRunArgs) {
|
|
138
|
+
args.push(...options.extraRunArgs);
|
|
139
|
+
}
|
|
140
|
+
args.push(request.image, "sh", "-c", `mkdir -p ${shellQuote(request.workdir)} && trap 'exit 0' TERM INT; while true; do sleep 3600 & wait $!; done`);
|
|
141
|
+
const res = yield* runCli(request.sandboxName, undefined, args);
|
|
142
|
+
if (res.exitCode !== 0) {
|
|
143
|
+
return yield* new SandboxError({
|
|
144
|
+
sandbox: request.sandboxName,
|
|
145
|
+
cause: new Error(`docker run failed (exit ${res.exitCode}): ${res.stderr.trim() || res.stdout.trim()}`),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
const containerId = res.stdout.trim() || containerName;
|
|
149
|
+
return {
|
|
150
|
+
id: containerId,
|
|
151
|
+
sandboxName: request.sandboxName,
|
|
152
|
+
provider: caps.provider,
|
|
153
|
+
workdir: normalizePosixPath(request.workdir),
|
|
154
|
+
metadata: { containerName, runtime: ociRuntime },
|
|
155
|
+
};
|
|
156
|
+
}),
|
|
157
|
+
exec: (handle, command, execOpts) => Effect.gen(function* () {
|
|
158
|
+
const started = Date.now();
|
|
159
|
+
const timeoutMs = toTimeoutMs(execOpts?.timeout);
|
|
160
|
+
const args = buildExecArgs(handle, command, execOpts);
|
|
161
|
+
const res = yield* runCli(handle.sandboxName, handle.id, args, {
|
|
162
|
+
...(execOpts?.stdin !== undefined ? { stdin: execOpts.stdin } : {}),
|
|
163
|
+
...(timeoutMs !== undefined ? { timeoutMs } : {}),
|
|
164
|
+
});
|
|
165
|
+
if (res.timedOut) {
|
|
166
|
+
return yield* new SandboxTimeoutError({
|
|
167
|
+
sandbox: handle.sandboxName,
|
|
168
|
+
instanceId: handle.id,
|
|
169
|
+
command: typeof command === "string" ? command : command.join(" "),
|
|
170
|
+
timeoutMs: timeoutMs ?? 0,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
if (res.exitCode !== 0 &&
|
|
174
|
+
/No such container|is not running/i.test(res.stderr)) {
|
|
175
|
+
return yield* new SandboxNotFoundError({
|
|
176
|
+
sandbox: handle.sandboxName,
|
|
177
|
+
instanceId: handle.id,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
exitCode: res.exitCode,
|
|
182
|
+
stdout: res.stdout,
|
|
183
|
+
stderr: res.stderr,
|
|
184
|
+
durationMs: Math.max(0, Date.now() - started),
|
|
185
|
+
};
|
|
186
|
+
}),
|
|
187
|
+
execStream: (handle, command, execOpts) => {
|
|
188
|
+
if (options?.commandRunner) {
|
|
189
|
+
// When using a custom runner in tests, stream its captured stdout/stderr
|
|
190
|
+
const args = buildExecArgs(handle, command, execOpts);
|
|
191
|
+
return Stream.unwrap(runCli(handle.sandboxName, handle.id, args, {
|
|
192
|
+
...(execOpts?.stdin !== undefined ? { stdin: execOpts.stdin } : {}),
|
|
193
|
+
...(toTimeoutMs(execOpts?.timeout) !== undefined
|
|
194
|
+
? { timeoutMs: toTimeoutMs(execOpts?.timeout) }
|
|
195
|
+
: {}),
|
|
196
|
+
}).pipe(Effect.map((res) => {
|
|
197
|
+
const chunks = [];
|
|
198
|
+
if (res.stdout)
|
|
199
|
+
chunks.push({ stream: "stdout", data: res.stdout });
|
|
200
|
+
if (res.stderr)
|
|
201
|
+
chunks.push({ stream: "stderr", data: res.stderr });
|
|
202
|
+
return Stream.fromIterable(chunks);
|
|
203
|
+
})));
|
|
204
|
+
}
|
|
205
|
+
const args = buildExecArgs(handle, command, execOpts);
|
|
206
|
+
const timeoutMs = toTimeoutMs(execOpts?.timeout);
|
|
207
|
+
return Stream.callback((queue) => Effect.acquireRelease(Effect.sync(() => {
|
|
208
|
+
const child = spawn(dockerBin, [...args], {
|
|
209
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
210
|
+
});
|
|
211
|
+
let timedOut = false;
|
|
212
|
+
const timer = timeoutMs !== undefined
|
|
213
|
+
? setTimeout(() => {
|
|
214
|
+
timedOut = true;
|
|
215
|
+
child.kill("SIGKILL");
|
|
216
|
+
}, timeoutMs)
|
|
217
|
+
: undefined;
|
|
218
|
+
child.stdout.on("data", (chunk) => {
|
|
219
|
+
Queue.offerUnsafe(queue, {
|
|
220
|
+
stream: "stdout",
|
|
221
|
+
data: chunk.toString("utf8"),
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
child.stderr.on("data", (chunk) => {
|
|
225
|
+
Queue.offerUnsafe(queue, {
|
|
226
|
+
stream: "stderr",
|
|
227
|
+
data: chunk.toString("utf8"),
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
child.on("error", (cause) => {
|
|
231
|
+
if (timer)
|
|
232
|
+
clearTimeout(timer);
|
|
233
|
+
Queue.failCauseUnsafe(queue, Cause.fail(new SandboxError({
|
|
234
|
+
sandbox: handle.sandboxName,
|
|
235
|
+
instanceId: handle.id,
|
|
236
|
+
cause,
|
|
237
|
+
})));
|
|
238
|
+
});
|
|
239
|
+
child.on("close", () => {
|
|
240
|
+
if (timer)
|
|
241
|
+
clearTimeout(timer);
|
|
242
|
+
if (timedOut) {
|
|
243
|
+
Queue.failCauseUnsafe(queue, Cause.fail(new SandboxTimeoutError({
|
|
244
|
+
sandbox: handle.sandboxName,
|
|
245
|
+
instanceId: handle.id,
|
|
246
|
+
command: typeof command === "string"
|
|
247
|
+
? command
|
|
248
|
+
: command.join(" "),
|
|
249
|
+
timeoutMs: timeoutMs ?? 0,
|
|
250
|
+
})));
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
Queue.endUnsafe(queue);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
if (execOpts?.stdin !== undefined) {
|
|
257
|
+
child.stdin.write(typeof execOpts.stdin === "string"
|
|
258
|
+
? Buffer.from(execOpts.stdin, "utf8")
|
|
259
|
+
: Buffer.from(execOpts.stdin));
|
|
260
|
+
}
|
|
261
|
+
child.stdin.end();
|
|
262
|
+
return { child, timer };
|
|
263
|
+
}), ({ child, timer }) => Effect.sync(() => {
|
|
264
|
+
if (timer)
|
|
265
|
+
clearTimeout(timer);
|
|
266
|
+
if (!child.killed)
|
|
267
|
+
child.kill("SIGKILL");
|
|
268
|
+
})).pipe(Effect.asVoid));
|
|
269
|
+
},
|
|
270
|
+
readFile: (handle, path) => Effect.gen(function* () {
|
|
271
|
+
const full = joinPosixPath(handle.workdir, path);
|
|
272
|
+
const res = yield* runCli(handle.sandboxName, handle.id, [
|
|
273
|
+
"exec",
|
|
274
|
+
handle.id,
|
|
275
|
+
"sh",
|
|
276
|
+
"-c",
|
|
277
|
+
`if [ ! -f ${shellQuote(full)} ]; then exit 44; fi; base64 ${shellQuote(full)}`,
|
|
278
|
+
]);
|
|
279
|
+
if (res.exitCode === 44) {
|
|
280
|
+
return yield* new SandboxFileNotFoundError({
|
|
281
|
+
sandbox: handle.sandboxName,
|
|
282
|
+
instanceId: handle.id,
|
|
283
|
+
path: full,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
if (res.exitCode !== 0) {
|
|
287
|
+
if (/No such container|is not running/i.test(res.stderr)) {
|
|
288
|
+
return yield* new SandboxNotFoundError({
|
|
289
|
+
sandbox: handle.sandboxName,
|
|
290
|
+
instanceId: handle.id,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
return yield* new SandboxError({
|
|
294
|
+
sandbox: handle.sandboxName,
|
|
295
|
+
instanceId: handle.id,
|
|
296
|
+
cause: new Error(`Failed to read "${full}": ${res.stderr.trim()}`),
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
const cleanB64 = res.stdout.replace(/\s+/g, "");
|
|
300
|
+
return new Uint8Array(Buffer.from(cleanB64, "base64"));
|
|
301
|
+
}),
|
|
302
|
+
writeFile: (handle, path, content) => Effect.gen(function* () {
|
|
303
|
+
const full = joinPosixPath(handle.workdir, path);
|
|
304
|
+
const dir = full.slice(0, full.lastIndexOf("/")) || "/";
|
|
305
|
+
const bytes = typeof content === "string"
|
|
306
|
+
? Buffer.from(content, "utf8")
|
|
307
|
+
: Buffer.from(content);
|
|
308
|
+
const b64 = bytes.toString("base64");
|
|
309
|
+
const res = yield* runCli(handle.sandboxName, handle.id, [
|
|
310
|
+
"exec",
|
|
311
|
+
"-i",
|
|
312
|
+
handle.id,
|
|
313
|
+
"sh",
|
|
314
|
+
"-c",
|
|
315
|
+
`mkdir -p ${shellQuote(dir)} && base64 -d > ${shellQuote(full)}`,
|
|
316
|
+
], { stdin: b64 });
|
|
317
|
+
if (res.exitCode !== 0) {
|
|
318
|
+
if (/No such container|is not running/i.test(res.stderr)) {
|
|
319
|
+
return yield* new SandboxNotFoundError({
|
|
320
|
+
sandbox: handle.sandboxName,
|
|
321
|
+
instanceId: handle.id,
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
return yield* new SandboxError({
|
|
325
|
+
sandbox: handle.sandboxName,
|
|
326
|
+
instanceId: handle.id,
|
|
327
|
+
cause: new Error(`Failed to write "${full}": ${res.stderr.trim()}`),
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}),
|
|
331
|
+
listFiles: (handle, dirPath) => Effect.gen(function* () {
|
|
332
|
+
const base = normalizePosixPath(dirPath ?? handle.workdir);
|
|
333
|
+
const res = yield* runCli(handle.sandboxName, handle.id, [
|
|
334
|
+
"exec",
|
|
335
|
+
handle.id,
|
|
336
|
+
"sh",
|
|
337
|
+
"-c",
|
|
338
|
+
`if [ ! -d ${shellQuote(base)} ]; then exit 0; fi; find ${shellQuote(base)} -mindepth 1 -type f | while IFS= read -r f; do sz=$(wc -c < "$f" | tr -d ' '); printf '%s\t%s\n' "$sz" "$f"; done`,
|
|
339
|
+
]);
|
|
340
|
+
if (res.exitCode !== 0) {
|
|
341
|
+
if (/No such container|is not running/i.test(res.stderr)) {
|
|
342
|
+
return yield* new SandboxNotFoundError({
|
|
343
|
+
sandbox: handle.sandboxName,
|
|
344
|
+
instanceId: handle.id,
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
return yield* new SandboxError({
|
|
348
|
+
sandbox: handle.sandboxName,
|
|
349
|
+
instanceId: handle.id,
|
|
350
|
+
cause: new Error(`Failed to list "${base}": ${res.stderr.trim()}`),
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
const entries = [];
|
|
354
|
+
for (const line of res.stdout.split("\n")) {
|
|
355
|
+
const trimmed = line.trim();
|
|
356
|
+
if (!trimmed)
|
|
357
|
+
continue;
|
|
358
|
+
const tabIdx = trimmed.indexOf("\t");
|
|
359
|
+
if (tabIdx === -1)
|
|
360
|
+
continue;
|
|
361
|
+
const size = Number(trimmed.slice(0, tabIdx)) || 0;
|
|
362
|
+
const p = normalizePosixPath(trimmed.slice(tabIdx + 1));
|
|
363
|
+
entries.push({ path: p, isDirectory: false, size });
|
|
364
|
+
}
|
|
365
|
+
entries.sort((a, b) => a.path.localeCompare(b.path));
|
|
366
|
+
return entries;
|
|
367
|
+
}),
|
|
368
|
+
destroy: (handle) => runCli(handle.sandboxName, handle.id, ["rm", "-f", handle.id]).pipe(Effect.asVoid),
|
|
369
|
+
}));
|
|
370
|
+
};
|
|
371
|
+
const isFileNotFound = (cause) => {
|
|
372
|
+
if (!cause || typeof cause !== "object")
|
|
373
|
+
return false;
|
|
374
|
+
const c = cause;
|
|
375
|
+
if (c.code === "ENOENT" || c.code === "not_found")
|
|
376
|
+
return true;
|
|
377
|
+
if (typeof c.message === "string" && /ENOENT|not found|no such file/i.test(c.message)) {
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
return false;
|
|
381
|
+
};
|
|
382
|
+
/**
|
|
383
|
+
* Builds a `SandboxRuntime` layer from any structural `ExternalSandboxClientLike`
|
|
384
|
+
* and declared `SandboxCapabilityFlags`.
|
|
385
|
+
*/
|
|
386
|
+
export const makeExternalSandboxLayer = (client, capabilities) => {
|
|
387
|
+
const active = new Map();
|
|
388
|
+
const getActive = (handle) => {
|
|
389
|
+
const inst = active.get(handle.id);
|
|
390
|
+
if (!inst) {
|
|
391
|
+
return Effect.fail(new SandboxNotFoundError({
|
|
392
|
+
sandbox: handle.sandboxName,
|
|
393
|
+
instanceId: handle.id,
|
|
394
|
+
}));
|
|
395
|
+
}
|
|
396
|
+
return Effect.succeed(inst);
|
|
397
|
+
};
|
|
398
|
+
return Layer.succeed(SandboxRuntime, SandboxRuntime.of({
|
|
399
|
+
capabilitiesFor: () => capabilities,
|
|
400
|
+
create: (request) => Effect.tryPromise({
|
|
401
|
+
try: async () => {
|
|
402
|
+
const inst = await client.create(request);
|
|
403
|
+
active.set(inst.id, inst);
|
|
404
|
+
return {
|
|
405
|
+
id: inst.id,
|
|
406
|
+
sandboxName: request.sandboxName,
|
|
407
|
+
provider: capabilities.provider,
|
|
408
|
+
workdir: normalizePosixPath(request.workdir),
|
|
409
|
+
};
|
|
410
|
+
},
|
|
411
|
+
catch: (cause) => new SandboxError({ sandbox: request.sandboxName, cause }),
|
|
412
|
+
}),
|
|
413
|
+
exec: (handle, command, options) => Effect.gen(function* () {
|
|
414
|
+
const inst = yield* getActive(handle);
|
|
415
|
+
return yield* Effect.tryPromise({
|
|
416
|
+
try: () => inst.exec(command, options),
|
|
417
|
+
catch: (cause) => new SandboxError({
|
|
418
|
+
sandbox: handle.sandboxName,
|
|
419
|
+
instanceId: handle.id,
|
|
420
|
+
cause,
|
|
421
|
+
}),
|
|
422
|
+
});
|
|
423
|
+
}),
|
|
424
|
+
execStream: (handle, command, options) => Stream.unwrap(Effect.gen(function* () {
|
|
425
|
+
const inst = yield* getActive(handle);
|
|
426
|
+
if (inst.execStream) {
|
|
427
|
+
return Stream.fromAsyncIterable(inst.execStream(command, options), (cause) => new SandboxError({
|
|
428
|
+
sandbox: handle.sandboxName,
|
|
429
|
+
instanceId: handle.id,
|
|
430
|
+
cause,
|
|
431
|
+
}));
|
|
432
|
+
}
|
|
433
|
+
const res = yield* Effect.tryPromise({
|
|
434
|
+
try: () => inst.exec(command, options),
|
|
435
|
+
catch: (cause) => new SandboxError({
|
|
436
|
+
sandbox: handle.sandboxName,
|
|
437
|
+
instanceId: handle.id,
|
|
438
|
+
cause,
|
|
439
|
+
}),
|
|
440
|
+
});
|
|
441
|
+
const chunks = [];
|
|
442
|
+
if (res.stdout)
|
|
443
|
+
chunks.push({ stream: "stdout", data: res.stdout });
|
|
444
|
+
if (res.stderr)
|
|
445
|
+
chunks.push({ stream: "stderr", data: res.stderr });
|
|
446
|
+
return Stream.fromIterable(chunks);
|
|
447
|
+
})),
|
|
448
|
+
readFile: (handle, path) => Effect.gen(function* () {
|
|
449
|
+
const inst = yield* getActive(handle);
|
|
450
|
+
const full = joinPosixPath(handle.workdir, path);
|
|
451
|
+
const raw = yield* Effect.tryPromise({
|
|
452
|
+
try: () => inst.readFile(full),
|
|
453
|
+
catch: (cause) => isFileNotFound(cause)
|
|
454
|
+
? new SandboxFileNotFoundError({
|
|
455
|
+
sandbox: handle.sandboxName,
|
|
456
|
+
instanceId: handle.id,
|
|
457
|
+
path: full,
|
|
458
|
+
})
|
|
459
|
+
: new SandboxError({
|
|
460
|
+
sandbox: handle.sandboxName,
|
|
461
|
+
instanceId: handle.id,
|
|
462
|
+
cause,
|
|
463
|
+
}),
|
|
464
|
+
});
|
|
465
|
+
return typeof raw === "string"
|
|
466
|
+
? new TextEncoder().encode(raw)
|
|
467
|
+
: new Uint8Array(raw);
|
|
468
|
+
}),
|
|
469
|
+
writeFile: (handle, path, content) => Effect.gen(function* () {
|
|
470
|
+
const inst = yield* getActive(handle);
|
|
471
|
+
const full = joinPosixPath(handle.workdir, path);
|
|
472
|
+
yield* Effect.tryPromise({
|
|
473
|
+
try: () => inst.writeFile(full, content),
|
|
474
|
+
catch: (cause) => new SandboxError({
|
|
475
|
+
sandbox: handle.sandboxName,
|
|
476
|
+
instanceId: handle.id,
|
|
477
|
+
cause,
|
|
478
|
+
}),
|
|
479
|
+
});
|
|
480
|
+
}),
|
|
481
|
+
listFiles: (handle, dirPath) => Effect.gen(function* () {
|
|
482
|
+
const inst = yield* getActive(handle);
|
|
483
|
+
return yield* Effect.tryPromise({
|
|
484
|
+
try: () => inst.listFiles(dirPath ?? handle.workdir),
|
|
485
|
+
catch: (cause) => new SandboxError({
|
|
486
|
+
sandbox: handle.sandboxName,
|
|
487
|
+
instanceId: handle.id,
|
|
488
|
+
cause,
|
|
489
|
+
}),
|
|
490
|
+
});
|
|
491
|
+
}),
|
|
492
|
+
destroy: (handle) => Effect.gen(function* () {
|
|
493
|
+
const inst = active.get(handle.id);
|
|
494
|
+
active.delete(handle.id);
|
|
495
|
+
if (!inst)
|
|
496
|
+
return;
|
|
497
|
+
yield* Effect.tryPromise({
|
|
498
|
+
try: () => inst.destroy(),
|
|
499
|
+
catch: (cause) => new SandboxError({
|
|
500
|
+
sandbox: handle.sandboxName,
|
|
501
|
+
instanceId: handle.id,
|
|
502
|
+
cause,
|
|
503
|
+
}),
|
|
504
|
+
});
|
|
505
|
+
}),
|
|
506
|
+
...(capabilities.nativeSnapshot
|
|
507
|
+
? {
|
|
508
|
+
snapshot: (handle) => Effect.gen(function* () {
|
|
509
|
+
const inst = yield* getActive(handle);
|
|
510
|
+
if (!inst.snapshot) {
|
|
511
|
+
return yield* new SandboxError({
|
|
512
|
+
sandbox: handle.sandboxName,
|
|
513
|
+
instanceId: handle.id,
|
|
514
|
+
cause: new Error(`Provider "${capabilities.provider}" instance does not implement snapshot()`),
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
return yield* Effect.tryPromise({
|
|
518
|
+
try: () => inst.snapshot(),
|
|
519
|
+
catch: (cause) => new SandboxError({
|
|
520
|
+
sandbox: handle.sandboxName,
|
|
521
|
+
instanceId: handle.id,
|
|
522
|
+
cause,
|
|
523
|
+
}),
|
|
524
|
+
});
|
|
525
|
+
}),
|
|
526
|
+
restore: (request, snapshotId) => Effect.tryPromise({
|
|
527
|
+
try: async () => {
|
|
528
|
+
if (!client.restore) {
|
|
529
|
+
throw new Error(`Provider "${capabilities.provider}" client does not implement restore()`);
|
|
530
|
+
}
|
|
531
|
+
const inst = await client.restore(request, snapshotId);
|
|
532
|
+
active.set(inst.id, inst);
|
|
533
|
+
return {
|
|
534
|
+
id: inst.id,
|
|
535
|
+
sandboxName: request.sandboxName,
|
|
536
|
+
provider: capabilities.provider,
|
|
537
|
+
workdir: normalizePosixPath(request.workdir),
|
|
538
|
+
};
|
|
539
|
+
},
|
|
540
|
+
catch: (cause) => new SandboxError({ sandbox: request.sandboxName, cause }),
|
|
541
|
+
}),
|
|
542
|
+
}
|
|
543
|
+
: {}),
|
|
544
|
+
...(capabilities.memoryPause
|
|
545
|
+
? {
|
|
546
|
+
pause: (handle) => Effect.gen(function* () {
|
|
547
|
+
const inst = yield* getActive(handle);
|
|
548
|
+
if (!inst.pause) {
|
|
549
|
+
return yield* new SandboxError({
|
|
550
|
+
sandbox: handle.sandboxName,
|
|
551
|
+
instanceId: handle.id,
|
|
552
|
+
cause: new Error(`Provider "${capabilities.provider}" instance does not implement pause()`),
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
const cpId = yield* Effect.tryPromise({
|
|
556
|
+
try: () => inst.pause(),
|
|
557
|
+
catch: (cause) => new SandboxError({
|
|
558
|
+
sandbox: handle.sandboxName,
|
|
559
|
+
instanceId: handle.id,
|
|
560
|
+
cause,
|
|
561
|
+
}),
|
|
562
|
+
});
|
|
563
|
+
active.delete(handle.id);
|
|
564
|
+
return cpId;
|
|
565
|
+
}),
|
|
566
|
+
resume: (request, checkpointId) => Effect.tryPromise({
|
|
567
|
+
try: async () => {
|
|
568
|
+
if (!client.resume) {
|
|
569
|
+
throw new Error(`Provider "${capabilities.provider}" client does not implement resume()`);
|
|
570
|
+
}
|
|
571
|
+
const inst = await client.resume(request, checkpointId);
|
|
572
|
+
active.set(inst.id, inst);
|
|
573
|
+
return {
|
|
574
|
+
id: inst.id,
|
|
575
|
+
sandboxName: request.sandboxName,
|
|
576
|
+
provider: capabilities.provider,
|
|
577
|
+
workdir: normalizePosixPath(request.workdir),
|
|
578
|
+
};
|
|
579
|
+
},
|
|
580
|
+
catch: (cause) => new SandboxError({ sandbox: request.sandboxName, cause }),
|
|
581
|
+
}),
|
|
582
|
+
}
|
|
583
|
+
: {}),
|
|
584
|
+
}));
|
|
585
|
+
};
|
|
586
|
+
// ---------------------------------------------------------------------------
|
|
587
|
+
// Named Provider Presets with Accurate Capability Profiles
|
|
588
|
+
// ---------------------------------------------------------------------------
|
|
589
|
+
/** Vercel Sandbox (`@vercel/sandbox`) — Firecracker microVMs, native snapshots, egress firewall. */
|
|
590
|
+
export const SandboxVercelLive = (client, overrides) => makeExternalSandboxLayer(client, {
|
|
591
|
+
provider: "vercel",
|
|
592
|
+
maxIsolation: "microvm",
|
|
593
|
+
nativeSnapshot: true,
|
|
594
|
+
memoryPause: false,
|
|
595
|
+
gpu: false,
|
|
596
|
+
egressFirewall: true,
|
|
597
|
+
streamingExec: true,
|
|
598
|
+
...overrides,
|
|
599
|
+
});
|
|
600
|
+
/** E2B (`e2b`) — Firecracker microVMs, native snapshots, and full RAM+disk memory pause/resume. */
|
|
601
|
+
export const SandboxE2BLive = (client, overrides) => makeExternalSandboxLayer(client, {
|
|
602
|
+
provider: "e2b",
|
|
603
|
+
maxIsolation: "microvm",
|
|
604
|
+
nativeSnapshot: true,
|
|
605
|
+
memoryPause: true,
|
|
606
|
+
gpu: false,
|
|
607
|
+
egressFirewall: true,
|
|
608
|
+
streamingExec: true,
|
|
609
|
+
...overrides,
|
|
610
|
+
});
|
|
611
|
+
/** Cloudflare Sandbox (`@cloudflare/sandbox`) — Cloudflare Containers bound to Workers. */
|
|
612
|
+
export const SandboxCloudflareLive = (client, overrides) => makeExternalSandboxLayer(client, {
|
|
613
|
+
provider: "cloudflare",
|
|
614
|
+
maxIsolation: "container",
|
|
615
|
+
nativeSnapshot: true,
|
|
616
|
+
memoryPause: false,
|
|
617
|
+
gpu: false,
|
|
618
|
+
egressFirewall: false,
|
|
619
|
+
streamingExec: true,
|
|
620
|
+
...overrides,
|
|
621
|
+
});
|
|
622
|
+
/** Daytona (`@daytonaio/sdk`) — microVMs with native snapshots, GPU support, and egress firewall. */
|
|
623
|
+
export const SandboxDaytonaLive = (client, overrides) => makeExternalSandboxLayer(client, {
|
|
624
|
+
provider: "daytona",
|
|
625
|
+
maxIsolation: "microvm",
|
|
626
|
+
nativeSnapshot: true,
|
|
627
|
+
memoryPause: false,
|
|
628
|
+
gpu: true,
|
|
629
|
+
egressFirewall: true,
|
|
630
|
+
streamingExec: true,
|
|
631
|
+
...overrides,
|
|
632
|
+
});
|
|
633
|
+
/** Modal (`modal`) — microVMs with GPU acceleration (T4/L4/A10G/A100/H100) and native snapshots. */
|
|
634
|
+
export const SandboxModalLive = (client, overrides) => makeExternalSandboxLayer(client, {
|
|
635
|
+
provider: "modal",
|
|
636
|
+
maxIsolation: "microvm",
|
|
637
|
+
nativeSnapshot: true,
|
|
638
|
+
memoryPause: false,
|
|
639
|
+
gpu: true,
|
|
640
|
+
egressFirewall: false,
|
|
641
|
+
streamingExec: true,
|
|
642
|
+
...overrides,
|
|
643
|
+
});
|
|
644
|
+
/** Fly.io Sprites (`@fly/sprites`) — Firecracker microVMs with persistent snapshots. */
|
|
645
|
+
export const SandboxSpritesLive = (client, overrides) => makeExternalSandboxLayer(client, {
|
|
646
|
+
provider: "sprites",
|
|
647
|
+
maxIsolation: "microvm",
|
|
648
|
+
nativeSnapshot: true,
|
|
649
|
+
memoryPause: false,
|
|
650
|
+
gpu: false,
|
|
651
|
+
egressFirewall: false,
|
|
652
|
+
streamingExec: true,
|
|
653
|
+
...overrides,
|
|
654
|
+
});
|
|
655
|
+
//# sourceMappingURL=providers.js.map
|