@meistrari/sandbox-sdk 1.2.2 → 1.4.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/README.md +17 -0
- package/dist/index.cjs +121 -18
- package/dist/index.d.cts +85 -14
- package/dist/index.d.mts +85 -14
- package/dist/index.d.ts +85 -14
- package/dist/index.mjs +115 -19
- package/package.json +26 -26
package/README.md
CHANGED
|
@@ -213,6 +213,20 @@ const result = await sandbox.runCode(`
|
|
|
213
213
|
await sandbox.kill()
|
|
214
214
|
```
|
|
215
215
|
|
|
216
|
+
### Selecting a Runtime
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
// Pick the backend that runs the sandbox. Only 'lambda-microvm' is supported
|
|
220
|
+
// today; omitting it uses the API's default runtime.
|
|
221
|
+
const run = await client.createRunAndWait({
|
|
222
|
+
code: 'console.log("Hello")',
|
|
223
|
+
language: 'typescript',
|
|
224
|
+
runtime: 'lambda-microvm',
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
const sandbox = await client.createSandbox({ runtime: 'lambda-microvm' })
|
|
228
|
+
```
|
|
229
|
+
|
|
216
230
|
### Streaming Output
|
|
217
231
|
|
|
218
232
|
```typescript
|
|
@@ -300,6 +314,8 @@ const client = sandbox({
|
|
|
300
314
|
```typescript
|
|
301
315
|
type Status = 'pending' | 'running' | 'completed' | 'failed' | 'timeout' | 'cancelled'
|
|
302
316
|
|
|
317
|
+
type SandboxRuntime = 'lambda-microvm'
|
|
318
|
+
|
|
303
319
|
interface CreateRunRequest {
|
|
304
320
|
code: string
|
|
305
321
|
language?: 'javascript' | 'typescript' | 'python' | 'bash'
|
|
@@ -311,6 +327,7 @@ interface CreateRunRequest {
|
|
|
311
327
|
timeoutMs?: number // ms (default: 300000)
|
|
312
328
|
sandboxId?: string // Use existing sandbox
|
|
313
329
|
persistent?: boolean // Keep sandbox after execution
|
|
330
|
+
runtime?: SandboxRuntime // Sandbox backend (default: API's configured runtime)
|
|
314
331
|
}
|
|
315
332
|
|
|
316
333
|
interface RunCodeOptions {
|
package/dist/index.cjs
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
const node_buffer = require('node:buffer');
|
|
4
4
|
const eventsourceParser = require('eventsource-parser');
|
|
5
5
|
|
|
6
|
+
const SANDBOX_RUNTIMES = ["lambda-microvm", "agent-sandbox", "e2b"];
|
|
7
|
+
|
|
6
8
|
class FetchError extends Error {
|
|
7
9
|
constructor(message, url, method, status, response) {
|
|
8
10
|
super(message);
|
|
@@ -70,14 +72,16 @@ var __publicField$2 = (obj, key, value) => {
|
|
|
70
72
|
return value;
|
|
71
73
|
};
|
|
72
74
|
class Sandbox {
|
|
73
|
-
constructor(client, sandboxId, auth) {
|
|
75
|
+
constructor(client, sandboxId, auth, info) {
|
|
74
76
|
this.client = client;
|
|
75
77
|
this.auth = auth;
|
|
76
78
|
__publicField$2(this, "sandboxId");
|
|
77
79
|
__publicField$2(this, "files");
|
|
78
80
|
__publicField$2(this, "commands");
|
|
79
81
|
__publicField$2(this, "timeoutMs", 3e5);
|
|
82
|
+
__publicField$2(this, "lastInfo");
|
|
80
83
|
this.sandboxId = sandboxId;
|
|
84
|
+
this.lastInfo = info;
|
|
81
85
|
const readFile = async (path, options) => {
|
|
82
86
|
return await this.client.readFile(sandboxId, path, options);
|
|
83
87
|
};
|
|
@@ -90,16 +94,12 @@ class Sandbox {
|
|
|
90
94
|
uploadBulk: async (files) => await this.client.uploadBulkFiles(sandboxId, files)
|
|
91
95
|
};
|
|
92
96
|
this.commands = {
|
|
93
|
-
run: async (command) =>
|
|
94
|
-
const result = await this.runCode(command, { language: "bash" });
|
|
95
|
-
return {
|
|
96
|
-
stdout: result.stdout,
|
|
97
|
-
stderr: result.stderr,
|
|
98
|
-
exitCode: result.exitCode
|
|
99
|
-
};
|
|
100
|
-
}
|
|
97
|
+
run: async (command, options) => await this.client.execCommand(sandboxId, command, options)
|
|
101
98
|
};
|
|
102
99
|
}
|
|
100
|
+
get info() {
|
|
101
|
+
return this.lastInfo;
|
|
102
|
+
}
|
|
103
103
|
// Factory methods (static)
|
|
104
104
|
static async create(client, options = {}) {
|
|
105
105
|
return await client.createSandbox(options);
|
|
@@ -170,7 +170,24 @@ class Sandbox {
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
}
|
|
173
|
+
async getInfo() {
|
|
174
|
+
this.lastInfo = await this.client.getSandbox(this.sandboxId);
|
|
175
|
+
return this.lastInfo;
|
|
176
|
+
}
|
|
177
|
+
async extend(ttlSeconds) {
|
|
178
|
+
this.lastInfo = await this.client.extendSandbox(this.sandboxId, ttlSeconds);
|
|
179
|
+
return this.lastInfo;
|
|
180
|
+
}
|
|
181
|
+
async suspend() {
|
|
182
|
+
this.lastInfo = await this.client.suspendSandbox(this.sandboxId);
|
|
183
|
+
return this.lastInfo;
|
|
184
|
+
}
|
|
185
|
+
async resume() {
|
|
186
|
+
this.lastInfo = await this.client.resumeSandbox(this.sandboxId);
|
|
187
|
+
return this.lastInfo;
|
|
188
|
+
}
|
|
173
189
|
async betaPause() {
|
|
190
|
+
await this.suspend();
|
|
174
191
|
}
|
|
175
192
|
setTimeout(ms) {
|
|
176
193
|
this.timeoutMs = ms;
|
|
@@ -237,6 +254,11 @@ var __publicField = (obj, key, value) => {
|
|
|
237
254
|
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
238
255
|
return value;
|
|
239
256
|
};
|
|
257
|
+
function assertSupportedRuntime(runtime) {
|
|
258
|
+
if (runtime !== void 0 && !SANDBOX_RUNTIMES.includes(runtime)) {
|
|
259
|
+
throw new Error(`Unsupported runtime "${runtime}". Supported runtimes: ${SANDBOX_RUNTIMES.join(", ")}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
240
262
|
class SandboxClient {
|
|
241
263
|
constructor(config) {
|
|
242
264
|
__publicField(this, "apiUrl");
|
|
@@ -262,6 +284,7 @@ class SandboxClient {
|
|
|
262
284
|
}
|
|
263
285
|
// === Runs ===
|
|
264
286
|
async createRun(request) {
|
|
287
|
+
assertSupportedRuntime(request.runtime);
|
|
265
288
|
const response = await this.fetch("/api/runs", {
|
|
266
289
|
method: "POST",
|
|
267
290
|
body: JSON.stringify(request)
|
|
@@ -273,6 +296,7 @@ class SandboxClient {
|
|
|
273
296
|
return await run.waitForCompletion();
|
|
274
297
|
}
|
|
275
298
|
async *streamRun(request) {
|
|
299
|
+
assertSupportedRuntime(request.runtime);
|
|
276
300
|
const url = `${this.apiUrl}/api/runs/stream`;
|
|
277
301
|
const headers = new Headers();
|
|
278
302
|
const authHeaders = this.auth.getHeaders();
|
|
@@ -382,28 +406,66 @@ class SandboxClient {
|
|
|
382
406
|
}
|
|
383
407
|
// === Sandboxes ===
|
|
384
408
|
async createSandbox(request = {}) {
|
|
409
|
+
assertSupportedRuntime(request.runtime);
|
|
385
410
|
const response = await this.fetch("/api/sandboxes", {
|
|
386
411
|
method: "POST",
|
|
387
412
|
body: JSON.stringify(request)
|
|
388
413
|
});
|
|
389
|
-
|
|
390
|
-
async () => await this.getSandboxStatus(response.data.sandboxId),
|
|
391
|
-
{ intervalMs: 200, maxAttempts: 150 }
|
|
392
|
-
// 30s max
|
|
393
|
-
);
|
|
394
|
-
return new Sandbox(this, response.data.sandboxId, this.auth);
|
|
414
|
+
return new Sandbox(this, response.data.sandboxId, this.auth, response.data);
|
|
395
415
|
}
|
|
396
|
-
async
|
|
416
|
+
async getSandbox(sandboxId) {
|
|
397
417
|
const response = await this.fetch(`/api/sandboxes/${sandboxId}`);
|
|
398
418
|
return response.data;
|
|
399
419
|
}
|
|
420
|
+
async getSandboxStatus(sandboxId) {
|
|
421
|
+
const { sandboxId: id, status } = await this.getSandbox(sandboxId);
|
|
422
|
+
return { sandboxId: id, status };
|
|
423
|
+
}
|
|
424
|
+
async listSandboxes(options = {}) {
|
|
425
|
+
const params = new URLSearchParams();
|
|
426
|
+
for (const [key, value] of Object.entries(options.metadata ?? {})) {
|
|
427
|
+
params.set(`metadata.${key}`, value);
|
|
428
|
+
}
|
|
429
|
+
if (options.states && options.states.length > 0) {
|
|
430
|
+
params.set("state", options.states.join(","));
|
|
431
|
+
}
|
|
432
|
+
const query = params.size > 0 ? `?${params.toString()}` : "";
|
|
433
|
+
const response = await this.fetch(`/api/sandboxes${query}`);
|
|
434
|
+
return response.data;
|
|
435
|
+
}
|
|
400
436
|
async connectToSandbox(sandboxId) {
|
|
401
|
-
await this.
|
|
402
|
-
return new Sandbox(this, sandboxId, this.auth);
|
|
437
|
+
const info = await this.getSandbox(sandboxId);
|
|
438
|
+
return new Sandbox(this, sandboxId, this.auth, info);
|
|
403
439
|
}
|
|
404
440
|
async deleteSandbox(sandboxId) {
|
|
405
441
|
await this.fetch(`/api/sandboxes/${sandboxId}`, { method: "DELETE" });
|
|
406
442
|
}
|
|
443
|
+
async execCommand(sandboxId, command, options = {}) {
|
|
444
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}/exec`, {
|
|
445
|
+
method: "POST",
|
|
446
|
+
body: JSON.stringify({ command, ...options })
|
|
447
|
+
});
|
|
448
|
+
return response.data;
|
|
449
|
+
}
|
|
450
|
+
async extendSandbox(sandboxId, ttlSeconds) {
|
|
451
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}/extend`, {
|
|
452
|
+
method: "POST",
|
|
453
|
+
body: JSON.stringify({ ttlSeconds })
|
|
454
|
+
});
|
|
455
|
+
return response.data;
|
|
456
|
+
}
|
|
457
|
+
async suspendSandbox(sandboxId) {
|
|
458
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}/suspend`, {
|
|
459
|
+
method: "POST"
|
|
460
|
+
});
|
|
461
|
+
return response.data;
|
|
462
|
+
}
|
|
463
|
+
async resumeSandbox(sandboxId) {
|
|
464
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}/resume`, {
|
|
465
|
+
method: "POST"
|
|
466
|
+
});
|
|
467
|
+
return response.data;
|
|
468
|
+
}
|
|
407
469
|
// === File Operations ===
|
|
408
470
|
async writeFile(sandboxId, path, content) {
|
|
409
471
|
const isBuffer = content instanceof Uint8Array;
|
|
@@ -480,13 +542,54 @@ class DataTokenAuthStrategy {
|
|
|
480
542
|
}
|
|
481
543
|
}
|
|
482
544
|
|
|
545
|
+
const SANDBOX_LIFECYCLE_TOPIC = "sandbox-lifecycle";
|
|
546
|
+
const SANDBOX_LIFECYCLE_ITEM = "lifecycle";
|
|
547
|
+
const SANDBOX_LIFECYCLE_EVENT_TYPES = [
|
|
548
|
+
"created",
|
|
549
|
+
"ready",
|
|
550
|
+
"suspended",
|
|
551
|
+
"resumed",
|
|
552
|
+
"terminated",
|
|
553
|
+
"failed",
|
|
554
|
+
"expired"
|
|
555
|
+
];
|
|
556
|
+
function sandboxLifecycleTopicEnv(binding = SANDBOX_LIFECYCLE_ITEM) {
|
|
557
|
+
return `KAFKA_${binding.toUpperCase().replaceAll("-", "_")}_TOPIC_${SANDBOX_LIFECYCLE_TOPIC.toUpperCase().replaceAll("-", "_")}`;
|
|
558
|
+
}
|
|
559
|
+
function isSandboxLifecycleEvent(value) {
|
|
560
|
+
if (typeof value !== "object" || value === null) {
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
const event = value;
|
|
564
|
+
return event.version === 1 && typeof event.eventId === "string" && SANDBOX_LIFECYCLE_EVENT_TYPES.includes(event.type) && typeof event.sandboxId === "string" && typeof event.workspaceId === "string" && typeof event.provider === "string" && typeof event.externalId === "string" && typeof event.state === "string" && typeof event.metadata === "object" && event.metadata !== null && typeof event.occurredAt === "string" && typeof event.source === "string";
|
|
565
|
+
}
|
|
566
|
+
function parseSandboxLifecycleEvent(raw) {
|
|
567
|
+
if (raw === null || raw === void 0) {
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
try {
|
|
571
|
+
const text = typeof raw === "string" ? raw : new TextDecoder().decode(raw);
|
|
572
|
+
const value = JSON.parse(text);
|
|
573
|
+
return isSandboxLifecycleEvent(value) ? value : null;
|
|
574
|
+
} catch {
|
|
575
|
+
return null;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
483
579
|
function sandbox(config) {
|
|
484
580
|
return new SandboxClient(config);
|
|
485
581
|
}
|
|
486
582
|
|
|
487
583
|
exports.DataTokenAuthStrategy = DataTokenAuthStrategy;
|
|
488
584
|
exports.FetchError = FetchError;
|
|
585
|
+
exports.SANDBOX_LIFECYCLE_EVENT_TYPES = SANDBOX_LIFECYCLE_EVENT_TYPES;
|
|
586
|
+
exports.SANDBOX_LIFECYCLE_ITEM = SANDBOX_LIFECYCLE_ITEM;
|
|
587
|
+
exports.SANDBOX_LIFECYCLE_TOPIC = SANDBOX_LIFECYCLE_TOPIC;
|
|
588
|
+
exports.SANDBOX_RUNTIMES = SANDBOX_RUNTIMES;
|
|
489
589
|
exports.Sandbox = Sandbox;
|
|
490
590
|
exports.SandboxClient = SandboxClient;
|
|
491
591
|
exports.SandboxRun = SandboxRun;
|
|
592
|
+
exports.isSandboxLifecycleEvent = isSandboxLifecycleEvent;
|
|
593
|
+
exports.parseSandboxLifecycleEvent = parseSandboxLifecycleEvent;
|
|
492
594
|
exports.sandbox = sandbox;
|
|
595
|
+
exports.sandboxLifecycleTopicEnv = sandboxLifecycleTopicEnv;
|
package/dist/index.d.cts
CHANGED
|
@@ -6,6 +6,41 @@ interface SandboxConfig {
|
|
|
6
6
|
interface AuthStrategy {
|
|
7
7
|
getHeaders: () => Headers;
|
|
8
8
|
}
|
|
9
|
+
declare const SANDBOX_RUNTIMES: readonly ["lambda-microvm", "agent-sandbox", "e2b"];
|
|
10
|
+
type SandboxRuntime = (typeof SANDBOX_RUNTIMES)[number];
|
|
11
|
+
type SandboxState = 'pending' | 'running' | 'suspended' | 'terminated' | 'failed';
|
|
12
|
+
type SandboxOnTimeout = 'suspend' | 'terminate';
|
|
13
|
+
interface SandboxInfo {
|
|
14
|
+
sandboxId: string;
|
|
15
|
+
status: Status;
|
|
16
|
+
provider: string;
|
|
17
|
+
externalId: string;
|
|
18
|
+
state: SandboxState;
|
|
19
|
+
image?: string;
|
|
20
|
+
metadata: Record<string, string>;
|
|
21
|
+
memSize: number;
|
|
22
|
+
cpuSize: number;
|
|
23
|
+
persistent: boolean;
|
|
24
|
+
createdAt: string;
|
|
25
|
+
lastUsedAt: string;
|
|
26
|
+
expiresAt?: string;
|
|
27
|
+
errorMessage?: string;
|
|
28
|
+
}
|
|
29
|
+
interface ListSandboxesOptions {
|
|
30
|
+
metadata?: Record<string, string>;
|
|
31
|
+
states?: SandboxState[];
|
|
32
|
+
}
|
|
33
|
+
interface ExecCommandOptions {
|
|
34
|
+
cwd?: string;
|
|
35
|
+
env?: Record<string, string>;
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
user?: string;
|
|
38
|
+
}
|
|
39
|
+
interface ExecCommandResult {
|
|
40
|
+
stdout: string;
|
|
41
|
+
stderr: string;
|
|
42
|
+
exitCode: number;
|
|
43
|
+
}
|
|
9
44
|
interface CreateRunRequest {
|
|
10
45
|
code: string;
|
|
11
46
|
language?: 'javascript' | 'typescript' | 'python' | 'bash' | 'ts' | 'js';
|
|
@@ -18,12 +53,18 @@ interface CreateRunRequest {
|
|
|
18
53
|
sandboxId?: string;
|
|
19
54
|
persistent?: boolean;
|
|
20
55
|
sandboxTtlSeconds?: number;
|
|
56
|
+
runtime?: SandboxRuntime;
|
|
21
57
|
}
|
|
22
58
|
interface CreateSandboxRequest {
|
|
23
59
|
cpuSize?: number;
|
|
24
60
|
memSize?: number;
|
|
25
61
|
sandboxTtlSeconds?: number;
|
|
26
62
|
files?: FileInput[];
|
|
63
|
+
runtime?: SandboxRuntime;
|
|
64
|
+
image?: string;
|
|
65
|
+
env?: Record<string, string>;
|
|
66
|
+
metadata?: Record<string, string>;
|
|
67
|
+
onTimeout?: SandboxOnTimeout;
|
|
27
68
|
}
|
|
28
69
|
type FileCategory = 'input' | 'skill' | 'settings' | 'context' | 'workspace' | 'output';
|
|
29
70
|
interface FileInput {
|
|
@@ -114,7 +155,7 @@ interface SandboxFiles {
|
|
|
114
155
|
uploadBulk: (files: BulkFileInput[]) => Promise<BulkUploadResult>;
|
|
115
156
|
}
|
|
116
157
|
interface SandboxCommands {
|
|
117
|
-
run: (command: string) => Promise<{
|
|
158
|
+
run: (command: string, options?: ExecCommandOptions) => Promise<{
|
|
118
159
|
stdout: string;
|
|
119
160
|
stderr: string;
|
|
120
161
|
exitCode?: number;
|
|
@@ -128,21 +169,18 @@ declare class Sandbox {
|
|
|
128
169
|
readonly files: SandboxFiles;
|
|
129
170
|
readonly commands: SandboxCommands;
|
|
130
171
|
private timeoutMs;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
files?: Array<{
|
|
136
|
-
name: string;
|
|
137
|
-
content: string;
|
|
138
|
-
encoding: 'utf8' | 'base64';
|
|
139
|
-
path?: string;
|
|
140
|
-
}>;
|
|
141
|
-
}): Promise<Sandbox>;
|
|
172
|
+
private lastInfo?;
|
|
173
|
+
constructor(client: SandboxClient, sandboxId: string, auth: AuthStrategy, info?: SandboxInfo);
|
|
174
|
+
get info(): SandboxInfo | undefined;
|
|
175
|
+
static create(client: SandboxClient, options?: CreateSandboxRequest): Promise<Sandbox>;
|
|
142
176
|
static connect(client: SandboxClient, sandboxId: string): Promise<Sandbox>;
|
|
143
177
|
private normalizeLanguage;
|
|
144
178
|
runCode(script: string, options?: RunCodeOptions): Promise<ExecutionResult>;
|
|
145
179
|
kill(): Promise<void>;
|
|
180
|
+
getInfo(): Promise<SandboxInfo>;
|
|
181
|
+
extend(ttlSeconds: number): Promise<SandboxInfo>;
|
|
182
|
+
suspend(): Promise<SandboxInfo>;
|
|
183
|
+
resume(): Promise<SandboxInfo>;
|
|
146
184
|
betaPause(): Promise<void>;
|
|
147
185
|
setTimeout(ms: number): void;
|
|
148
186
|
}
|
|
@@ -178,12 +216,18 @@ declare class SandboxClient {
|
|
|
178
216
|
streamRun(request: CreateRunRequest): AsyncGenerator<StreamEvent>;
|
|
179
217
|
getRun(runId: string): Promise<SandboxRun>;
|
|
180
218
|
createSandbox(request?: CreateSandboxRequest): Promise<Sandbox>;
|
|
219
|
+
getSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
181
220
|
getSandboxStatus(sandboxId: string): Promise<{
|
|
182
221
|
sandboxId: string;
|
|
183
222
|
status: Status;
|
|
184
223
|
}>;
|
|
224
|
+
listSandboxes(options?: ListSandboxesOptions): Promise<SandboxInfo[]>;
|
|
185
225
|
connectToSandbox(sandboxId: string): Promise<Sandbox>;
|
|
186
226
|
deleteSandbox(sandboxId: string): Promise<void>;
|
|
227
|
+
execCommand(sandboxId: string, command: string, options?: ExecCommandOptions): Promise<ExecCommandResult>;
|
|
228
|
+
extendSandbox(sandboxId: string, ttlSeconds: number): Promise<SandboxInfo>;
|
|
229
|
+
suspendSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
230
|
+
resumeSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
187
231
|
writeFile(sandboxId: string, path: string, content: string | Uint8Array): Promise<void>;
|
|
188
232
|
readFile(sandboxId: string, path: string, options?: FileReadOptions): Promise<string | Blob>;
|
|
189
233
|
fileExists(sandboxId: string, path: string): Promise<boolean>;
|
|
@@ -207,7 +251,34 @@ declare class FetchError extends Error {
|
|
|
207
251
|
static from(url: string, method: string, res: Response): Promise<FetchError>;
|
|
208
252
|
}
|
|
209
253
|
|
|
254
|
+
declare const SANDBOX_LIFECYCLE_TOPIC = "sandbox-lifecycle";
|
|
255
|
+
declare const SANDBOX_LIFECYCLE_ITEM = "lifecycle";
|
|
256
|
+
declare const SANDBOX_LIFECYCLE_EVENT_TYPES: readonly ["created", "ready", "suspended", "resumed", "terminated", "failed", "expired"];
|
|
257
|
+
type SandboxLifecycleEventType = (typeof SANDBOX_LIFECYCLE_EVENT_TYPES)[number];
|
|
258
|
+
type SandboxLifecycleSource = 'api' | 'provider-webhook' | 'provider-watch';
|
|
259
|
+
/**
|
|
260
|
+
* One sandbox transition, delivered at-least-once on the `sandbox-lifecycle`
|
|
261
|
+
* topic keyed by `sandboxId`. Consumers dedupe by `eventId`.
|
|
262
|
+
*/
|
|
263
|
+
interface SandboxLifecycleEvent {
|
|
264
|
+
version: 1;
|
|
265
|
+
eventId: string;
|
|
266
|
+
type: SandboxLifecycleEventType;
|
|
267
|
+
sandboxId: string;
|
|
268
|
+
workspaceId: string;
|
|
269
|
+
provider: string;
|
|
270
|
+
externalId: string;
|
|
271
|
+
image?: string;
|
|
272
|
+
state: SandboxState;
|
|
273
|
+
metadata: Record<string, string>;
|
|
274
|
+
occurredAt: string;
|
|
275
|
+
source: SandboxLifecycleSource;
|
|
276
|
+
}
|
|
277
|
+
declare function sandboxLifecycleTopicEnv(binding?: string): string;
|
|
278
|
+
declare function isSandboxLifecycleEvent(value: unknown): value is SandboxLifecycleEvent;
|
|
279
|
+
declare function parseSandboxLifecycleEvent(raw: string | Uint8Array | null | undefined): SandboxLifecycleEvent | null;
|
|
280
|
+
|
|
210
281
|
declare function sandbox(config: SandboxConfig): SandboxClient;
|
|
211
282
|
|
|
212
|
-
export { DataTokenAuthStrategy, FetchError, Sandbox, SandboxClient, SandboxRun, sandbox };
|
|
213
|
-
export type { AuthStrategy, CreateRunRequest, CreateSandboxRequest, ExecutionResult, FileInput, RunCodeOptions, RunResponse, SandboxCommands, SandboxConfig, SandboxFiles, Status, StreamCallbacks, StreamEvent };
|
|
283
|
+
export { DataTokenAuthStrategy, FetchError, SANDBOX_LIFECYCLE_EVENT_TYPES, SANDBOX_LIFECYCLE_ITEM, SANDBOX_LIFECYCLE_TOPIC, SANDBOX_RUNTIMES, Sandbox, SandboxClient, SandboxRun, isSandboxLifecycleEvent, parseSandboxLifecycleEvent, sandbox, sandboxLifecycleTopicEnv };
|
|
284
|
+
export type { AuthStrategy, CreateRunRequest, CreateSandboxRequest, ExecCommandOptions, ExecCommandResult, ExecutionResult, FileInput, ListSandboxesOptions, RunCodeOptions, RunResponse, SandboxCommands, SandboxConfig, SandboxFiles, SandboxInfo, SandboxLifecycleEvent, SandboxLifecycleEventType, SandboxLifecycleSource, SandboxOnTimeout, SandboxRuntime, SandboxState, Status, StreamCallbacks, StreamEvent };
|
package/dist/index.d.mts
CHANGED
|
@@ -6,6 +6,41 @@ interface SandboxConfig {
|
|
|
6
6
|
interface AuthStrategy {
|
|
7
7
|
getHeaders: () => Headers;
|
|
8
8
|
}
|
|
9
|
+
declare const SANDBOX_RUNTIMES: readonly ["lambda-microvm", "agent-sandbox", "e2b"];
|
|
10
|
+
type SandboxRuntime = (typeof SANDBOX_RUNTIMES)[number];
|
|
11
|
+
type SandboxState = 'pending' | 'running' | 'suspended' | 'terminated' | 'failed';
|
|
12
|
+
type SandboxOnTimeout = 'suspend' | 'terminate';
|
|
13
|
+
interface SandboxInfo {
|
|
14
|
+
sandboxId: string;
|
|
15
|
+
status: Status;
|
|
16
|
+
provider: string;
|
|
17
|
+
externalId: string;
|
|
18
|
+
state: SandboxState;
|
|
19
|
+
image?: string;
|
|
20
|
+
metadata: Record<string, string>;
|
|
21
|
+
memSize: number;
|
|
22
|
+
cpuSize: number;
|
|
23
|
+
persistent: boolean;
|
|
24
|
+
createdAt: string;
|
|
25
|
+
lastUsedAt: string;
|
|
26
|
+
expiresAt?: string;
|
|
27
|
+
errorMessage?: string;
|
|
28
|
+
}
|
|
29
|
+
interface ListSandboxesOptions {
|
|
30
|
+
metadata?: Record<string, string>;
|
|
31
|
+
states?: SandboxState[];
|
|
32
|
+
}
|
|
33
|
+
interface ExecCommandOptions {
|
|
34
|
+
cwd?: string;
|
|
35
|
+
env?: Record<string, string>;
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
user?: string;
|
|
38
|
+
}
|
|
39
|
+
interface ExecCommandResult {
|
|
40
|
+
stdout: string;
|
|
41
|
+
stderr: string;
|
|
42
|
+
exitCode: number;
|
|
43
|
+
}
|
|
9
44
|
interface CreateRunRequest {
|
|
10
45
|
code: string;
|
|
11
46
|
language?: 'javascript' | 'typescript' | 'python' | 'bash' | 'ts' | 'js';
|
|
@@ -18,12 +53,18 @@ interface CreateRunRequest {
|
|
|
18
53
|
sandboxId?: string;
|
|
19
54
|
persistent?: boolean;
|
|
20
55
|
sandboxTtlSeconds?: number;
|
|
56
|
+
runtime?: SandboxRuntime;
|
|
21
57
|
}
|
|
22
58
|
interface CreateSandboxRequest {
|
|
23
59
|
cpuSize?: number;
|
|
24
60
|
memSize?: number;
|
|
25
61
|
sandboxTtlSeconds?: number;
|
|
26
62
|
files?: FileInput[];
|
|
63
|
+
runtime?: SandboxRuntime;
|
|
64
|
+
image?: string;
|
|
65
|
+
env?: Record<string, string>;
|
|
66
|
+
metadata?: Record<string, string>;
|
|
67
|
+
onTimeout?: SandboxOnTimeout;
|
|
27
68
|
}
|
|
28
69
|
type FileCategory = 'input' | 'skill' | 'settings' | 'context' | 'workspace' | 'output';
|
|
29
70
|
interface FileInput {
|
|
@@ -114,7 +155,7 @@ interface SandboxFiles {
|
|
|
114
155
|
uploadBulk: (files: BulkFileInput[]) => Promise<BulkUploadResult>;
|
|
115
156
|
}
|
|
116
157
|
interface SandboxCommands {
|
|
117
|
-
run: (command: string) => Promise<{
|
|
158
|
+
run: (command: string, options?: ExecCommandOptions) => Promise<{
|
|
118
159
|
stdout: string;
|
|
119
160
|
stderr: string;
|
|
120
161
|
exitCode?: number;
|
|
@@ -128,21 +169,18 @@ declare class Sandbox {
|
|
|
128
169
|
readonly files: SandboxFiles;
|
|
129
170
|
readonly commands: SandboxCommands;
|
|
130
171
|
private timeoutMs;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
files?: Array<{
|
|
136
|
-
name: string;
|
|
137
|
-
content: string;
|
|
138
|
-
encoding: 'utf8' | 'base64';
|
|
139
|
-
path?: string;
|
|
140
|
-
}>;
|
|
141
|
-
}): Promise<Sandbox>;
|
|
172
|
+
private lastInfo?;
|
|
173
|
+
constructor(client: SandboxClient, sandboxId: string, auth: AuthStrategy, info?: SandboxInfo);
|
|
174
|
+
get info(): SandboxInfo | undefined;
|
|
175
|
+
static create(client: SandboxClient, options?: CreateSandboxRequest): Promise<Sandbox>;
|
|
142
176
|
static connect(client: SandboxClient, sandboxId: string): Promise<Sandbox>;
|
|
143
177
|
private normalizeLanguage;
|
|
144
178
|
runCode(script: string, options?: RunCodeOptions): Promise<ExecutionResult>;
|
|
145
179
|
kill(): Promise<void>;
|
|
180
|
+
getInfo(): Promise<SandboxInfo>;
|
|
181
|
+
extend(ttlSeconds: number): Promise<SandboxInfo>;
|
|
182
|
+
suspend(): Promise<SandboxInfo>;
|
|
183
|
+
resume(): Promise<SandboxInfo>;
|
|
146
184
|
betaPause(): Promise<void>;
|
|
147
185
|
setTimeout(ms: number): void;
|
|
148
186
|
}
|
|
@@ -178,12 +216,18 @@ declare class SandboxClient {
|
|
|
178
216
|
streamRun(request: CreateRunRequest): AsyncGenerator<StreamEvent>;
|
|
179
217
|
getRun(runId: string): Promise<SandboxRun>;
|
|
180
218
|
createSandbox(request?: CreateSandboxRequest): Promise<Sandbox>;
|
|
219
|
+
getSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
181
220
|
getSandboxStatus(sandboxId: string): Promise<{
|
|
182
221
|
sandboxId: string;
|
|
183
222
|
status: Status;
|
|
184
223
|
}>;
|
|
224
|
+
listSandboxes(options?: ListSandboxesOptions): Promise<SandboxInfo[]>;
|
|
185
225
|
connectToSandbox(sandboxId: string): Promise<Sandbox>;
|
|
186
226
|
deleteSandbox(sandboxId: string): Promise<void>;
|
|
227
|
+
execCommand(sandboxId: string, command: string, options?: ExecCommandOptions): Promise<ExecCommandResult>;
|
|
228
|
+
extendSandbox(sandboxId: string, ttlSeconds: number): Promise<SandboxInfo>;
|
|
229
|
+
suspendSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
230
|
+
resumeSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
187
231
|
writeFile(sandboxId: string, path: string, content: string | Uint8Array): Promise<void>;
|
|
188
232
|
readFile(sandboxId: string, path: string, options?: FileReadOptions): Promise<string | Blob>;
|
|
189
233
|
fileExists(sandboxId: string, path: string): Promise<boolean>;
|
|
@@ -207,7 +251,34 @@ declare class FetchError extends Error {
|
|
|
207
251
|
static from(url: string, method: string, res: Response): Promise<FetchError>;
|
|
208
252
|
}
|
|
209
253
|
|
|
254
|
+
declare const SANDBOX_LIFECYCLE_TOPIC = "sandbox-lifecycle";
|
|
255
|
+
declare const SANDBOX_LIFECYCLE_ITEM = "lifecycle";
|
|
256
|
+
declare const SANDBOX_LIFECYCLE_EVENT_TYPES: readonly ["created", "ready", "suspended", "resumed", "terminated", "failed", "expired"];
|
|
257
|
+
type SandboxLifecycleEventType = (typeof SANDBOX_LIFECYCLE_EVENT_TYPES)[number];
|
|
258
|
+
type SandboxLifecycleSource = 'api' | 'provider-webhook' | 'provider-watch';
|
|
259
|
+
/**
|
|
260
|
+
* One sandbox transition, delivered at-least-once on the `sandbox-lifecycle`
|
|
261
|
+
* topic keyed by `sandboxId`. Consumers dedupe by `eventId`.
|
|
262
|
+
*/
|
|
263
|
+
interface SandboxLifecycleEvent {
|
|
264
|
+
version: 1;
|
|
265
|
+
eventId: string;
|
|
266
|
+
type: SandboxLifecycleEventType;
|
|
267
|
+
sandboxId: string;
|
|
268
|
+
workspaceId: string;
|
|
269
|
+
provider: string;
|
|
270
|
+
externalId: string;
|
|
271
|
+
image?: string;
|
|
272
|
+
state: SandboxState;
|
|
273
|
+
metadata: Record<string, string>;
|
|
274
|
+
occurredAt: string;
|
|
275
|
+
source: SandboxLifecycleSource;
|
|
276
|
+
}
|
|
277
|
+
declare function sandboxLifecycleTopicEnv(binding?: string): string;
|
|
278
|
+
declare function isSandboxLifecycleEvent(value: unknown): value is SandboxLifecycleEvent;
|
|
279
|
+
declare function parseSandboxLifecycleEvent(raw: string | Uint8Array | null | undefined): SandboxLifecycleEvent | null;
|
|
280
|
+
|
|
210
281
|
declare function sandbox(config: SandboxConfig): SandboxClient;
|
|
211
282
|
|
|
212
|
-
export { DataTokenAuthStrategy, FetchError, Sandbox, SandboxClient, SandboxRun, sandbox };
|
|
213
|
-
export type { AuthStrategy, CreateRunRequest, CreateSandboxRequest, ExecutionResult, FileInput, RunCodeOptions, RunResponse, SandboxCommands, SandboxConfig, SandboxFiles, Status, StreamCallbacks, StreamEvent };
|
|
283
|
+
export { DataTokenAuthStrategy, FetchError, SANDBOX_LIFECYCLE_EVENT_TYPES, SANDBOX_LIFECYCLE_ITEM, SANDBOX_LIFECYCLE_TOPIC, SANDBOX_RUNTIMES, Sandbox, SandboxClient, SandboxRun, isSandboxLifecycleEvent, parseSandboxLifecycleEvent, sandbox, sandboxLifecycleTopicEnv };
|
|
284
|
+
export type { AuthStrategy, CreateRunRequest, CreateSandboxRequest, ExecCommandOptions, ExecCommandResult, ExecutionResult, FileInput, ListSandboxesOptions, RunCodeOptions, RunResponse, SandboxCommands, SandboxConfig, SandboxFiles, SandboxInfo, SandboxLifecycleEvent, SandboxLifecycleEventType, SandboxLifecycleSource, SandboxOnTimeout, SandboxRuntime, SandboxState, Status, StreamCallbacks, StreamEvent };
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,41 @@ interface SandboxConfig {
|
|
|
6
6
|
interface AuthStrategy {
|
|
7
7
|
getHeaders: () => Headers;
|
|
8
8
|
}
|
|
9
|
+
declare const SANDBOX_RUNTIMES: readonly ["lambda-microvm", "agent-sandbox", "e2b"];
|
|
10
|
+
type SandboxRuntime = (typeof SANDBOX_RUNTIMES)[number];
|
|
11
|
+
type SandboxState = 'pending' | 'running' | 'suspended' | 'terminated' | 'failed';
|
|
12
|
+
type SandboxOnTimeout = 'suspend' | 'terminate';
|
|
13
|
+
interface SandboxInfo {
|
|
14
|
+
sandboxId: string;
|
|
15
|
+
status: Status;
|
|
16
|
+
provider: string;
|
|
17
|
+
externalId: string;
|
|
18
|
+
state: SandboxState;
|
|
19
|
+
image?: string;
|
|
20
|
+
metadata: Record<string, string>;
|
|
21
|
+
memSize: number;
|
|
22
|
+
cpuSize: number;
|
|
23
|
+
persistent: boolean;
|
|
24
|
+
createdAt: string;
|
|
25
|
+
lastUsedAt: string;
|
|
26
|
+
expiresAt?: string;
|
|
27
|
+
errorMessage?: string;
|
|
28
|
+
}
|
|
29
|
+
interface ListSandboxesOptions {
|
|
30
|
+
metadata?: Record<string, string>;
|
|
31
|
+
states?: SandboxState[];
|
|
32
|
+
}
|
|
33
|
+
interface ExecCommandOptions {
|
|
34
|
+
cwd?: string;
|
|
35
|
+
env?: Record<string, string>;
|
|
36
|
+
timeoutMs?: number;
|
|
37
|
+
user?: string;
|
|
38
|
+
}
|
|
39
|
+
interface ExecCommandResult {
|
|
40
|
+
stdout: string;
|
|
41
|
+
stderr: string;
|
|
42
|
+
exitCode: number;
|
|
43
|
+
}
|
|
9
44
|
interface CreateRunRequest {
|
|
10
45
|
code: string;
|
|
11
46
|
language?: 'javascript' | 'typescript' | 'python' | 'bash' | 'ts' | 'js';
|
|
@@ -18,12 +53,18 @@ interface CreateRunRequest {
|
|
|
18
53
|
sandboxId?: string;
|
|
19
54
|
persistent?: boolean;
|
|
20
55
|
sandboxTtlSeconds?: number;
|
|
56
|
+
runtime?: SandboxRuntime;
|
|
21
57
|
}
|
|
22
58
|
interface CreateSandboxRequest {
|
|
23
59
|
cpuSize?: number;
|
|
24
60
|
memSize?: number;
|
|
25
61
|
sandboxTtlSeconds?: number;
|
|
26
62
|
files?: FileInput[];
|
|
63
|
+
runtime?: SandboxRuntime;
|
|
64
|
+
image?: string;
|
|
65
|
+
env?: Record<string, string>;
|
|
66
|
+
metadata?: Record<string, string>;
|
|
67
|
+
onTimeout?: SandboxOnTimeout;
|
|
27
68
|
}
|
|
28
69
|
type FileCategory = 'input' | 'skill' | 'settings' | 'context' | 'workspace' | 'output';
|
|
29
70
|
interface FileInput {
|
|
@@ -114,7 +155,7 @@ interface SandboxFiles {
|
|
|
114
155
|
uploadBulk: (files: BulkFileInput[]) => Promise<BulkUploadResult>;
|
|
115
156
|
}
|
|
116
157
|
interface SandboxCommands {
|
|
117
|
-
run: (command: string) => Promise<{
|
|
158
|
+
run: (command: string, options?: ExecCommandOptions) => Promise<{
|
|
118
159
|
stdout: string;
|
|
119
160
|
stderr: string;
|
|
120
161
|
exitCode?: number;
|
|
@@ -128,21 +169,18 @@ declare class Sandbox {
|
|
|
128
169
|
readonly files: SandboxFiles;
|
|
129
170
|
readonly commands: SandboxCommands;
|
|
130
171
|
private timeoutMs;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
files?: Array<{
|
|
136
|
-
name: string;
|
|
137
|
-
content: string;
|
|
138
|
-
encoding: 'utf8' | 'base64';
|
|
139
|
-
path?: string;
|
|
140
|
-
}>;
|
|
141
|
-
}): Promise<Sandbox>;
|
|
172
|
+
private lastInfo?;
|
|
173
|
+
constructor(client: SandboxClient, sandboxId: string, auth: AuthStrategy, info?: SandboxInfo);
|
|
174
|
+
get info(): SandboxInfo | undefined;
|
|
175
|
+
static create(client: SandboxClient, options?: CreateSandboxRequest): Promise<Sandbox>;
|
|
142
176
|
static connect(client: SandboxClient, sandboxId: string): Promise<Sandbox>;
|
|
143
177
|
private normalizeLanguage;
|
|
144
178
|
runCode(script: string, options?: RunCodeOptions): Promise<ExecutionResult>;
|
|
145
179
|
kill(): Promise<void>;
|
|
180
|
+
getInfo(): Promise<SandboxInfo>;
|
|
181
|
+
extend(ttlSeconds: number): Promise<SandboxInfo>;
|
|
182
|
+
suspend(): Promise<SandboxInfo>;
|
|
183
|
+
resume(): Promise<SandboxInfo>;
|
|
146
184
|
betaPause(): Promise<void>;
|
|
147
185
|
setTimeout(ms: number): void;
|
|
148
186
|
}
|
|
@@ -178,12 +216,18 @@ declare class SandboxClient {
|
|
|
178
216
|
streamRun(request: CreateRunRequest): AsyncGenerator<StreamEvent>;
|
|
179
217
|
getRun(runId: string): Promise<SandboxRun>;
|
|
180
218
|
createSandbox(request?: CreateSandboxRequest): Promise<Sandbox>;
|
|
219
|
+
getSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
181
220
|
getSandboxStatus(sandboxId: string): Promise<{
|
|
182
221
|
sandboxId: string;
|
|
183
222
|
status: Status;
|
|
184
223
|
}>;
|
|
224
|
+
listSandboxes(options?: ListSandboxesOptions): Promise<SandboxInfo[]>;
|
|
185
225
|
connectToSandbox(sandboxId: string): Promise<Sandbox>;
|
|
186
226
|
deleteSandbox(sandboxId: string): Promise<void>;
|
|
227
|
+
execCommand(sandboxId: string, command: string, options?: ExecCommandOptions): Promise<ExecCommandResult>;
|
|
228
|
+
extendSandbox(sandboxId: string, ttlSeconds: number): Promise<SandboxInfo>;
|
|
229
|
+
suspendSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
230
|
+
resumeSandbox(sandboxId: string): Promise<SandboxInfo>;
|
|
187
231
|
writeFile(sandboxId: string, path: string, content: string | Uint8Array): Promise<void>;
|
|
188
232
|
readFile(sandboxId: string, path: string, options?: FileReadOptions): Promise<string | Blob>;
|
|
189
233
|
fileExists(sandboxId: string, path: string): Promise<boolean>;
|
|
@@ -207,7 +251,34 @@ declare class FetchError extends Error {
|
|
|
207
251
|
static from(url: string, method: string, res: Response): Promise<FetchError>;
|
|
208
252
|
}
|
|
209
253
|
|
|
254
|
+
declare const SANDBOX_LIFECYCLE_TOPIC = "sandbox-lifecycle";
|
|
255
|
+
declare const SANDBOX_LIFECYCLE_ITEM = "lifecycle";
|
|
256
|
+
declare const SANDBOX_LIFECYCLE_EVENT_TYPES: readonly ["created", "ready", "suspended", "resumed", "terminated", "failed", "expired"];
|
|
257
|
+
type SandboxLifecycleEventType = (typeof SANDBOX_LIFECYCLE_EVENT_TYPES)[number];
|
|
258
|
+
type SandboxLifecycleSource = 'api' | 'provider-webhook' | 'provider-watch';
|
|
259
|
+
/**
|
|
260
|
+
* One sandbox transition, delivered at-least-once on the `sandbox-lifecycle`
|
|
261
|
+
* topic keyed by `sandboxId`. Consumers dedupe by `eventId`.
|
|
262
|
+
*/
|
|
263
|
+
interface SandboxLifecycleEvent {
|
|
264
|
+
version: 1;
|
|
265
|
+
eventId: string;
|
|
266
|
+
type: SandboxLifecycleEventType;
|
|
267
|
+
sandboxId: string;
|
|
268
|
+
workspaceId: string;
|
|
269
|
+
provider: string;
|
|
270
|
+
externalId: string;
|
|
271
|
+
image?: string;
|
|
272
|
+
state: SandboxState;
|
|
273
|
+
metadata: Record<string, string>;
|
|
274
|
+
occurredAt: string;
|
|
275
|
+
source: SandboxLifecycleSource;
|
|
276
|
+
}
|
|
277
|
+
declare function sandboxLifecycleTopicEnv(binding?: string): string;
|
|
278
|
+
declare function isSandboxLifecycleEvent(value: unknown): value is SandboxLifecycleEvent;
|
|
279
|
+
declare function parseSandboxLifecycleEvent(raw: string | Uint8Array | null | undefined): SandboxLifecycleEvent | null;
|
|
280
|
+
|
|
210
281
|
declare function sandbox(config: SandboxConfig): SandboxClient;
|
|
211
282
|
|
|
212
|
-
export { DataTokenAuthStrategy, FetchError, Sandbox, SandboxClient, SandboxRun, sandbox };
|
|
213
|
-
export type { AuthStrategy, CreateRunRequest, CreateSandboxRequest, ExecutionResult, FileInput, RunCodeOptions, RunResponse, SandboxCommands, SandboxConfig, SandboxFiles, Status, StreamCallbacks, StreamEvent };
|
|
283
|
+
export { DataTokenAuthStrategy, FetchError, SANDBOX_LIFECYCLE_EVENT_TYPES, SANDBOX_LIFECYCLE_ITEM, SANDBOX_LIFECYCLE_TOPIC, SANDBOX_RUNTIMES, Sandbox, SandboxClient, SandboxRun, isSandboxLifecycleEvent, parseSandboxLifecycleEvent, sandbox, sandboxLifecycleTopicEnv };
|
|
284
|
+
export type { AuthStrategy, CreateRunRequest, CreateSandboxRequest, ExecCommandOptions, ExecCommandResult, ExecutionResult, FileInput, ListSandboxesOptions, RunCodeOptions, RunResponse, SandboxCommands, SandboxConfig, SandboxFiles, SandboxInfo, SandboxLifecycleEvent, SandboxLifecycleEventType, SandboxLifecycleSource, SandboxOnTimeout, SandboxRuntime, SandboxState, Status, StreamCallbacks, StreamEvent };
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
2
|
import { createParser } from 'eventsource-parser';
|
|
3
3
|
|
|
4
|
+
const SANDBOX_RUNTIMES = ["lambda-microvm", "agent-sandbox", "e2b"];
|
|
5
|
+
|
|
4
6
|
class FetchError extends Error {
|
|
5
7
|
constructor(message, url, method, status, response) {
|
|
6
8
|
super(message);
|
|
@@ -68,14 +70,16 @@ var __publicField$2 = (obj, key, value) => {
|
|
|
68
70
|
return value;
|
|
69
71
|
};
|
|
70
72
|
class Sandbox {
|
|
71
|
-
constructor(client, sandboxId, auth) {
|
|
73
|
+
constructor(client, sandboxId, auth, info) {
|
|
72
74
|
this.client = client;
|
|
73
75
|
this.auth = auth;
|
|
74
76
|
__publicField$2(this, "sandboxId");
|
|
75
77
|
__publicField$2(this, "files");
|
|
76
78
|
__publicField$2(this, "commands");
|
|
77
79
|
__publicField$2(this, "timeoutMs", 3e5);
|
|
80
|
+
__publicField$2(this, "lastInfo");
|
|
78
81
|
this.sandboxId = sandboxId;
|
|
82
|
+
this.lastInfo = info;
|
|
79
83
|
const readFile = async (path, options) => {
|
|
80
84
|
return await this.client.readFile(sandboxId, path, options);
|
|
81
85
|
};
|
|
@@ -88,16 +92,12 @@ class Sandbox {
|
|
|
88
92
|
uploadBulk: async (files) => await this.client.uploadBulkFiles(sandboxId, files)
|
|
89
93
|
};
|
|
90
94
|
this.commands = {
|
|
91
|
-
run: async (command) =>
|
|
92
|
-
const result = await this.runCode(command, { language: "bash" });
|
|
93
|
-
return {
|
|
94
|
-
stdout: result.stdout,
|
|
95
|
-
stderr: result.stderr,
|
|
96
|
-
exitCode: result.exitCode
|
|
97
|
-
};
|
|
98
|
-
}
|
|
95
|
+
run: async (command, options) => await this.client.execCommand(sandboxId, command, options)
|
|
99
96
|
};
|
|
100
97
|
}
|
|
98
|
+
get info() {
|
|
99
|
+
return this.lastInfo;
|
|
100
|
+
}
|
|
101
101
|
// Factory methods (static)
|
|
102
102
|
static async create(client, options = {}) {
|
|
103
103
|
return await client.createSandbox(options);
|
|
@@ -168,7 +168,24 @@ class Sandbox {
|
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
|
+
async getInfo() {
|
|
172
|
+
this.lastInfo = await this.client.getSandbox(this.sandboxId);
|
|
173
|
+
return this.lastInfo;
|
|
174
|
+
}
|
|
175
|
+
async extend(ttlSeconds) {
|
|
176
|
+
this.lastInfo = await this.client.extendSandbox(this.sandboxId, ttlSeconds);
|
|
177
|
+
return this.lastInfo;
|
|
178
|
+
}
|
|
179
|
+
async suspend() {
|
|
180
|
+
this.lastInfo = await this.client.suspendSandbox(this.sandboxId);
|
|
181
|
+
return this.lastInfo;
|
|
182
|
+
}
|
|
183
|
+
async resume() {
|
|
184
|
+
this.lastInfo = await this.client.resumeSandbox(this.sandboxId);
|
|
185
|
+
return this.lastInfo;
|
|
186
|
+
}
|
|
171
187
|
async betaPause() {
|
|
188
|
+
await this.suspend();
|
|
172
189
|
}
|
|
173
190
|
setTimeout(ms) {
|
|
174
191
|
this.timeoutMs = ms;
|
|
@@ -235,6 +252,11 @@ var __publicField = (obj, key, value) => {
|
|
|
235
252
|
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
236
253
|
return value;
|
|
237
254
|
};
|
|
255
|
+
function assertSupportedRuntime(runtime) {
|
|
256
|
+
if (runtime !== void 0 && !SANDBOX_RUNTIMES.includes(runtime)) {
|
|
257
|
+
throw new Error(`Unsupported runtime "${runtime}". Supported runtimes: ${SANDBOX_RUNTIMES.join(", ")}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
238
260
|
class SandboxClient {
|
|
239
261
|
constructor(config) {
|
|
240
262
|
__publicField(this, "apiUrl");
|
|
@@ -260,6 +282,7 @@ class SandboxClient {
|
|
|
260
282
|
}
|
|
261
283
|
// === Runs ===
|
|
262
284
|
async createRun(request) {
|
|
285
|
+
assertSupportedRuntime(request.runtime);
|
|
263
286
|
const response = await this.fetch("/api/runs", {
|
|
264
287
|
method: "POST",
|
|
265
288
|
body: JSON.stringify(request)
|
|
@@ -271,6 +294,7 @@ class SandboxClient {
|
|
|
271
294
|
return await run.waitForCompletion();
|
|
272
295
|
}
|
|
273
296
|
async *streamRun(request) {
|
|
297
|
+
assertSupportedRuntime(request.runtime);
|
|
274
298
|
const url = `${this.apiUrl}/api/runs/stream`;
|
|
275
299
|
const headers = new Headers();
|
|
276
300
|
const authHeaders = this.auth.getHeaders();
|
|
@@ -380,28 +404,66 @@ class SandboxClient {
|
|
|
380
404
|
}
|
|
381
405
|
// === Sandboxes ===
|
|
382
406
|
async createSandbox(request = {}) {
|
|
407
|
+
assertSupportedRuntime(request.runtime);
|
|
383
408
|
const response = await this.fetch("/api/sandboxes", {
|
|
384
409
|
method: "POST",
|
|
385
410
|
body: JSON.stringify(request)
|
|
386
411
|
});
|
|
387
|
-
|
|
388
|
-
async () => await this.getSandboxStatus(response.data.sandboxId),
|
|
389
|
-
{ intervalMs: 200, maxAttempts: 150 }
|
|
390
|
-
// 30s max
|
|
391
|
-
);
|
|
392
|
-
return new Sandbox(this, response.data.sandboxId, this.auth);
|
|
412
|
+
return new Sandbox(this, response.data.sandboxId, this.auth, response.data);
|
|
393
413
|
}
|
|
394
|
-
async
|
|
414
|
+
async getSandbox(sandboxId) {
|
|
395
415
|
const response = await this.fetch(`/api/sandboxes/${sandboxId}`);
|
|
396
416
|
return response.data;
|
|
397
417
|
}
|
|
418
|
+
async getSandboxStatus(sandboxId) {
|
|
419
|
+
const { sandboxId: id, status } = await this.getSandbox(sandboxId);
|
|
420
|
+
return { sandboxId: id, status };
|
|
421
|
+
}
|
|
422
|
+
async listSandboxes(options = {}) {
|
|
423
|
+
const params = new URLSearchParams();
|
|
424
|
+
for (const [key, value] of Object.entries(options.metadata ?? {})) {
|
|
425
|
+
params.set(`metadata.${key}`, value);
|
|
426
|
+
}
|
|
427
|
+
if (options.states && options.states.length > 0) {
|
|
428
|
+
params.set("state", options.states.join(","));
|
|
429
|
+
}
|
|
430
|
+
const query = params.size > 0 ? `?${params.toString()}` : "";
|
|
431
|
+
const response = await this.fetch(`/api/sandboxes${query}`);
|
|
432
|
+
return response.data;
|
|
433
|
+
}
|
|
398
434
|
async connectToSandbox(sandboxId) {
|
|
399
|
-
await this.
|
|
400
|
-
return new Sandbox(this, sandboxId, this.auth);
|
|
435
|
+
const info = await this.getSandbox(sandboxId);
|
|
436
|
+
return new Sandbox(this, sandboxId, this.auth, info);
|
|
401
437
|
}
|
|
402
438
|
async deleteSandbox(sandboxId) {
|
|
403
439
|
await this.fetch(`/api/sandboxes/${sandboxId}`, { method: "DELETE" });
|
|
404
440
|
}
|
|
441
|
+
async execCommand(sandboxId, command, options = {}) {
|
|
442
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}/exec`, {
|
|
443
|
+
method: "POST",
|
|
444
|
+
body: JSON.stringify({ command, ...options })
|
|
445
|
+
});
|
|
446
|
+
return response.data;
|
|
447
|
+
}
|
|
448
|
+
async extendSandbox(sandboxId, ttlSeconds) {
|
|
449
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}/extend`, {
|
|
450
|
+
method: "POST",
|
|
451
|
+
body: JSON.stringify({ ttlSeconds })
|
|
452
|
+
});
|
|
453
|
+
return response.data;
|
|
454
|
+
}
|
|
455
|
+
async suspendSandbox(sandboxId) {
|
|
456
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}/suspend`, {
|
|
457
|
+
method: "POST"
|
|
458
|
+
});
|
|
459
|
+
return response.data;
|
|
460
|
+
}
|
|
461
|
+
async resumeSandbox(sandboxId) {
|
|
462
|
+
const response = await this.fetch(`/api/sandboxes/${sandboxId}/resume`, {
|
|
463
|
+
method: "POST"
|
|
464
|
+
});
|
|
465
|
+
return response.data;
|
|
466
|
+
}
|
|
405
467
|
// === File Operations ===
|
|
406
468
|
async writeFile(sandboxId, path, content) {
|
|
407
469
|
const isBuffer = content instanceof Uint8Array;
|
|
@@ -478,8 +540,42 @@ class DataTokenAuthStrategy {
|
|
|
478
540
|
}
|
|
479
541
|
}
|
|
480
542
|
|
|
543
|
+
const SANDBOX_LIFECYCLE_TOPIC = "sandbox-lifecycle";
|
|
544
|
+
const SANDBOX_LIFECYCLE_ITEM = "lifecycle";
|
|
545
|
+
const SANDBOX_LIFECYCLE_EVENT_TYPES = [
|
|
546
|
+
"created",
|
|
547
|
+
"ready",
|
|
548
|
+
"suspended",
|
|
549
|
+
"resumed",
|
|
550
|
+
"terminated",
|
|
551
|
+
"failed",
|
|
552
|
+
"expired"
|
|
553
|
+
];
|
|
554
|
+
function sandboxLifecycleTopicEnv(binding = SANDBOX_LIFECYCLE_ITEM) {
|
|
555
|
+
return `KAFKA_${binding.toUpperCase().replaceAll("-", "_")}_TOPIC_${SANDBOX_LIFECYCLE_TOPIC.toUpperCase().replaceAll("-", "_")}`;
|
|
556
|
+
}
|
|
557
|
+
function isSandboxLifecycleEvent(value) {
|
|
558
|
+
if (typeof value !== "object" || value === null) {
|
|
559
|
+
return false;
|
|
560
|
+
}
|
|
561
|
+
const event = value;
|
|
562
|
+
return event.version === 1 && typeof event.eventId === "string" && SANDBOX_LIFECYCLE_EVENT_TYPES.includes(event.type) && typeof event.sandboxId === "string" && typeof event.workspaceId === "string" && typeof event.provider === "string" && typeof event.externalId === "string" && typeof event.state === "string" && typeof event.metadata === "object" && event.metadata !== null && typeof event.occurredAt === "string" && typeof event.source === "string";
|
|
563
|
+
}
|
|
564
|
+
function parseSandboxLifecycleEvent(raw) {
|
|
565
|
+
if (raw === null || raw === void 0) {
|
|
566
|
+
return null;
|
|
567
|
+
}
|
|
568
|
+
try {
|
|
569
|
+
const text = typeof raw === "string" ? raw : new TextDecoder().decode(raw);
|
|
570
|
+
const value = JSON.parse(text);
|
|
571
|
+
return isSandboxLifecycleEvent(value) ? value : null;
|
|
572
|
+
} catch {
|
|
573
|
+
return null;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
481
577
|
function sandbox(config) {
|
|
482
578
|
return new SandboxClient(config);
|
|
483
579
|
}
|
|
484
580
|
|
|
485
|
-
export { DataTokenAuthStrategy, FetchError, Sandbox, SandboxClient, SandboxRun, sandbox };
|
|
581
|
+
export { DataTokenAuthStrategy, FetchError, SANDBOX_LIFECYCLE_EVENT_TYPES, SANDBOX_LIFECYCLE_ITEM, SANDBOX_LIFECYCLE_TOPIC, SANDBOX_RUNTIMES, Sandbox, SandboxClient, SandboxRun, isSandboxLifecycleEvent, parseSandboxLifecycleEvent, sandbox, sandboxLifecycleTopicEnv };
|
package/package.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
"name": "@meistrari/sandbox-sdk",
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.mjs",
|
|
9
|
+
"require": "./dist/index.cjs"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/index.mjs",
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "unbuild",
|
|
19
|
+
"test": "vitest"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"eventsource-parser": "^3.0.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"unbuild": "^2.0.0",
|
|
26
|
+
"vitest": "^2.0.0",
|
|
27
|
+
"typescript": "^5.0.0"
|
|
10
28
|
}
|
|
11
|
-
},
|
|
12
|
-
"main": "dist/index.mjs",
|
|
13
|
-
"types": "dist/index.d.ts",
|
|
14
|
-
"files": [
|
|
15
|
-
"dist"
|
|
16
|
-
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"build": "unbuild",
|
|
19
|
-
"test": "vitest"
|
|
20
|
-
},
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"eventsource-parser": "^3.0.0"
|
|
23
|
-
},
|
|
24
|
-
"devDependencies": {
|
|
25
|
-
"unbuild": "^2.0.0",
|
|
26
|
-
"vitest": "^2.0.0",
|
|
27
|
-
"typescript": "^5.0.0"
|
|
28
|
-
}
|
|
29
29
|
}
|