@meistrari/sandbox-sdk 0.1.4 → 1.0.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 +79 -77
- package/dist/index.cjs +51 -26
- package/dist/index.d.cts +19 -1
- package/dist/index.d.mts +19 -1
- package/dist/index.d.ts +19 -1
- package/dist/index.mjs +47 -22
- package/package.json +26 -26
package/README.md
CHANGED
|
@@ -17,14 +17,14 @@ import { sandbox, DataTokenAuthStrategy } from '@meistrari/sandbox-sdk'
|
|
|
17
17
|
|
|
18
18
|
// Create client
|
|
19
19
|
const client = sandbox({
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
apiUrl: 'https://sandbox.meistrari.com',
|
|
21
|
+
auth: new DataTokenAuthStrategy(dataToken),
|
|
22
22
|
})
|
|
23
23
|
|
|
24
24
|
// Execute code
|
|
25
25
|
const run = await client.createRunAndWait({
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
code: 'console.log("Hello, World!")',
|
|
27
|
+
language: 'typescript',
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
console.log(run.stdout) // "Hello, World!"
|
|
@@ -37,15 +37,15 @@ console.log(run.stdout) // "Hello, World!"
|
|
|
37
37
|
```typescript
|
|
38
38
|
// One-shot execution (no persistent sandbox)
|
|
39
39
|
const run = await client.createRunAndWait({
|
|
40
|
-
|
|
40
|
+
code: `
|
|
41
41
|
const result = 1 + 1
|
|
42
42
|
console.log(result)
|
|
43
43
|
`,
|
|
44
|
-
|
|
44
|
+
language: 'typescript',
|
|
45
45
|
})
|
|
46
46
|
|
|
47
|
-
console.log(run.stdout)
|
|
48
|
-
console.log(run.exitCode)
|
|
47
|
+
console.log(run.stdout) // "2"
|
|
48
|
+
console.log(run.exitCode) // 0
|
|
49
49
|
console.log(run.isSuccess) // true
|
|
50
50
|
```
|
|
51
51
|
|
|
@@ -56,8 +56,8 @@ import { Sandbox } from '@meistrari/sandbox-sdk'
|
|
|
56
56
|
|
|
57
57
|
// Create a persistent sandbox
|
|
58
58
|
const sandbox = await Sandbox.create(client, {
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
memSize: 512,
|
|
60
|
+
cpuSize: 1.0,
|
|
61
61
|
})
|
|
62
62
|
|
|
63
63
|
// Execute multiple commands in the same sandbox
|
|
@@ -122,8 +122,8 @@ const result = await sandbox.runCode(`
|
|
|
122
122
|
console.log(_.pick(data, ['name', 'age']))
|
|
123
123
|
console.log(schema.parse(data))
|
|
124
124
|
`, {
|
|
125
|
-
|
|
126
|
-
|
|
125
|
+
language: 'typescript',
|
|
126
|
+
libraries: ['lodash', 'zod'],
|
|
127
127
|
})
|
|
128
128
|
|
|
129
129
|
console.log(result.stdout)
|
|
@@ -140,11 +140,11 @@ const result = await sandbox.runCode(`
|
|
|
140
140
|
console.log('API Key:', process.env.API_KEY)
|
|
141
141
|
console.log('Environment:', process.env.NODE_ENV)
|
|
142
142
|
`, {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
143
|
+
language: 'typescript',
|
|
144
|
+
envs: {
|
|
145
|
+
API_KEY: 'secret-key-123',
|
|
146
|
+
NODE_ENV: 'production',
|
|
147
|
+
},
|
|
148
148
|
})
|
|
149
149
|
```
|
|
150
150
|
|
|
@@ -163,19 +163,19 @@ const result = await sandbox.runCode(`
|
|
|
163
163
|
console.log('Config:', config)
|
|
164
164
|
console.log('Data lines:', data.split('\\n').length)
|
|
165
165
|
`, {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
166
|
+
language: 'typescript',
|
|
167
|
+
files: [
|
|
168
|
+
{
|
|
169
|
+
name: 'config.json',
|
|
170
|
+
content: JSON.stringify({ apiUrl: 'https://api.example.com', timeout: 5000 }),
|
|
171
|
+
encoding: 'utf8',
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: 'data.csv',
|
|
175
|
+
content: 'name,age\\nJohn,30\\nJane,25',
|
|
176
|
+
encoding: 'utf8',
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
179
|
})
|
|
180
180
|
```
|
|
181
181
|
|
|
@@ -196,18 +196,18 @@ const result = await sandbox.runCode(`
|
|
|
196
196
|
console.log('Parsed records:', parsed.data.length)
|
|
197
197
|
console.log('First record:', JSON.stringify(parsed.data[0]))
|
|
198
198
|
`, {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
},
|
|
204
|
-
files: [
|
|
205
|
-
{
|
|
206
|
-
name: 'users.csv',
|
|
207
|
-
content: 'id,name,email\\n1,John,john@example.com\\n2,Jane,jane@example.com',
|
|
208
|
-
encoding: 'utf8',
|
|
199
|
+
language: 'typescript',
|
|
200
|
+
libraries: ['papaparse'],
|
|
201
|
+
envs: {
|
|
202
|
+
PARSER_API_KEY: 'pk_live_abc123xyz',
|
|
209
203
|
},
|
|
210
|
-
|
|
204
|
+
files: [
|
|
205
|
+
{
|
|
206
|
+
name: 'users.csv',
|
|
207
|
+
content: 'id,name,email\\n1,John,john@example.com\\n2,Jane,jane@example.com',
|
|
208
|
+
encoding: 'utf8',
|
|
209
|
+
},
|
|
210
|
+
],
|
|
211
211
|
})
|
|
212
212
|
|
|
213
213
|
await sandbox.kill()
|
|
@@ -217,10 +217,10 @@ await sandbox.kill()
|
|
|
217
217
|
|
|
218
218
|
```typescript
|
|
219
219
|
const result = await sandbox.runCode(longRunningScript, {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
220
|
+
language: 'typescript',
|
|
221
|
+
onStdout: ({ line }) => console.log('[stdout]', line),
|
|
222
|
+
onStderr: ({ line }) => console.log('[stderr]', line),
|
|
223
|
+
onError: error => console.error('[error]', error),
|
|
224
224
|
})
|
|
225
225
|
```
|
|
226
226
|
|
|
@@ -240,11 +240,11 @@ await sandbox.runCode('console.log("I am back!")')
|
|
|
240
240
|
|
|
241
241
|
Creates a new Sandbox client instance.
|
|
242
242
|
|
|
243
|
-
```
|
|
243
|
+
```javascript
|
|
244
244
|
const client = sandbox({
|
|
245
245
|
apiUrl: string, // Sandbox API URL
|
|
246
246
|
auth: AuthStrategy, // Authentication strategy
|
|
247
|
-
timeout
|
|
247
|
+
timeout: number, // Request timeout in ms (default: 300000, optional)
|
|
248
248
|
})
|
|
249
249
|
```
|
|
250
250
|
|
|
@@ -301,34 +301,34 @@ const client = sandbox({
|
|
|
301
301
|
type Status = 'pending' | 'running' | 'completed' | 'failed' | 'timeout' | 'cancelled'
|
|
302
302
|
|
|
303
303
|
interface CreateRunRequest {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
304
|
+
code: string
|
|
305
|
+
language?: 'javascript' | 'typescript' | 'python' | 'bash'
|
|
306
|
+
libraries?: string[]
|
|
307
|
+
env?: Record<string, string>
|
|
308
|
+
files?: FileInput[]
|
|
309
|
+
memSize?: number // MB (default: 512)
|
|
310
|
+
cpuSize?: number // cores (default: 1.0)
|
|
311
|
+
timeoutMs?: number // ms (default: 300000)
|
|
312
|
+
sandboxId?: string // Use existing sandbox
|
|
313
|
+
persistent?: boolean // Keep sandbox after execution
|
|
314
314
|
}
|
|
315
315
|
|
|
316
316
|
interface RunCodeOptions {
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
317
|
+
language?: 'typescript' | 'javascript' | 'python' | 'bash'
|
|
318
|
+
envs?: Record<string, string>
|
|
319
|
+
timeoutMs?: number
|
|
320
|
+
libraries?: string[] // npm packages to install
|
|
321
|
+
files?: FileInput[] // input files to provide
|
|
322
|
+
onStdout?: (data: { line: string }) => void
|
|
323
|
+
onStderr?: (data: { line: string }) => void
|
|
324
|
+
onError?: (error: Error) => void
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
interface FileInput {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
328
|
+
name: string // filename
|
|
329
|
+
content: string // file content
|
|
330
|
+
encoding: 'utf8' | 'base64'
|
|
331
|
+
path?: string // optional custom path
|
|
332
332
|
}
|
|
333
333
|
```
|
|
334
334
|
|
|
@@ -339,10 +339,11 @@ This SDK is designed to be compatible with E2B's `SandboxLike` interface, making
|
|
|
339
339
|
```typescript
|
|
340
340
|
// E2B
|
|
341
341
|
import { Sandbox } from '@e2b/code-interpreter'
|
|
342
|
-
const sandbox = await Sandbox.create(templateId, { apiKey })
|
|
343
342
|
|
|
344
343
|
// Sandbox (drop-in replacement)
|
|
345
344
|
import { Sandbox, sandbox, DataTokenAuthStrategy } from '@meistrari/sandbox-sdk'
|
|
345
|
+
|
|
346
|
+
const sandbox = await Sandbox.create(templateId, { apiKey })
|
|
346
347
|
const client = sandbox({ apiUrl, auth: new DataTokenAuthStrategy(token) })
|
|
347
348
|
const sandbox = await Sandbox.create(client)
|
|
348
349
|
|
|
@@ -358,13 +359,14 @@ await sandbox.kill()
|
|
|
358
359
|
import { FetchError } from '@meistrari/sandbox-sdk'
|
|
359
360
|
|
|
360
361
|
try {
|
|
361
|
-
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
362
|
+
const run = await client.createRunAndWait({ code: 'invalid(' })
|
|
363
|
+
}
|
|
364
|
+
catch (error) {
|
|
365
|
+
if (error instanceof FetchError) {
|
|
366
|
+
console.error(`API Error: ${error.message}`)
|
|
367
|
+
console.error(`Status: ${error.status}`)
|
|
368
|
+
console.error(`URL: ${error.url}`)
|
|
369
|
+
}
|
|
368
370
|
}
|
|
369
371
|
```
|
|
370
372
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const node_buffer = require('node:buffer');
|
|
3
4
|
const eventsourceParser = require('eventsource-parser');
|
|
4
5
|
|
|
5
6
|
class FetchError extends Error {
|
|
@@ -24,8 +25,7 @@ class FetchError extends Error {
|
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
async function* parseSSEStream(response) {
|
|
27
|
-
|
|
28
|
-
if (!reader)
|
|
28
|
+
if (!response.body)
|
|
29
29
|
throw new Error("No response body");
|
|
30
30
|
const decoder = new TextDecoder();
|
|
31
31
|
const events = [];
|
|
@@ -39,11 +39,8 @@ async function* parseSSEStream(response) {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
});
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (done)
|
|
45
|
-
break;
|
|
46
|
-
parser.feed(decoder.decode(value, { stream: true }));
|
|
42
|
+
for await (const chunk of response.body) {
|
|
43
|
+
parser.feed(decoder.decode(chunk, { stream: true }));
|
|
47
44
|
while (events.length > 0) {
|
|
48
45
|
yield events.shift();
|
|
49
46
|
}
|
|
@@ -81,15 +78,16 @@ class Sandbox {
|
|
|
81
78
|
__publicField$2(this, "commands");
|
|
82
79
|
__publicField$2(this, "timeoutMs", 3e5);
|
|
83
80
|
this.sandboxId = sandboxId;
|
|
84
|
-
const readFile = (path, options) => {
|
|
85
|
-
return this.client.readFile(sandboxId, path, options);
|
|
81
|
+
const readFile = async (path, options) => {
|
|
82
|
+
return await this.client.readFile(sandboxId, path, options);
|
|
86
83
|
};
|
|
87
84
|
this.files = {
|
|
88
|
-
write: (path, content) => this.client.writeFile(sandboxId, path, content),
|
|
85
|
+
write: async (path, content) => await this.client.writeFile(sandboxId, path, content),
|
|
89
86
|
read: readFile,
|
|
90
|
-
exists: (path) => this.client.fileExists(sandboxId, path),
|
|
91
|
-
remove: (path) => this.client.deleteFile(sandboxId, path),
|
|
92
|
-
list: (directory) => this.client.listFiles(sandboxId, directory)
|
|
87
|
+
exists: async (path) => await this.client.fileExists(sandboxId, path),
|
|
88
|
+
remove: async (path) => await this.client.deleteFile(sandboxId, path),
|
|
89
|
+
list: async (directory) => await this.client.listFiles(sandboxId, directory),
|
|
90
|
+
uploadBulk: async (files) => await this.client.uploadBulkFiles(sandboxId, files)
|
|
93
91
|
};
|
|
94
92
|
this.commands = {
|
|
95
93
|
run: async (command) => {
|
|
@@ -104,10 +102,10 @@ class Sandbox {
|
|
|
104
102
|
}
|
|
105
103
|
// Factory methods (static)
|
|
106
104
|
static async create(client, options = {}) {
|
|
107
|
-
return client.createSandbox(options);
|
|
105
|
+
return await client.createSandbox(options);
|
|
108
106
|
}
|
|
109
107
|
static async connect(client, sandboxId) {
|
|
110
|
-
return client.connectToSandbox(sandboxId);
|
|
108
|
+
return await client.connectToSandbox(sandboxId);
|
|
111
109
|
}
|
|
112
110
|
// Normalize language aliases ('ts' -> 'typescript', 'js' -> 'javascript')
|
|
113
111
|
normalizeLanguage(lang) {
|
|
@@ -115,7 +113,9 @@ class Sandbox {
|
|
|
115
113
|
return "typescript";
|
|
116
114
|
if (lang === "js")
|
|
117
115
|
return "javascript";
|
|
118
|
-
|
|
116
|
+
if (lang === "typescript" || lang === "javascript" || lang === "python" || lang === "bash")
|
|
117
|
+
return lang;
|
|
118
|
+
return "typescript";
|
|
119
119
|
}
|
|
120
120
|
// Main execution method (compatible with E2B and agent-api)
|
|
121
121
|
async runCode(script, options = {}) {
|
|
@@ -133,10 +133,12 @@ class Sandbox {
|
|
|
133
133
|
});
|
|
134
134
|
for await (const event of stream) {
|
|
135
135
|
if (event.type === "stdout" && event.content) {
|
|
136
|
-
stdout += event.content
|
|
136
|
+
stdout += `${event.content}
|
|
137
|
+
`;
|
|
137
138
|
await options.onStdout?.({ line: event.content });
|
|
138
139
|
} else if (event.type === "stderr" && event.content) {
|
|
139
|
-
stderr += event.content
|
|
140
|
+
stderr += `${event.content}
|
|
141
|
+
`;
|
|
140
142
|
await options.onStderr?.({ line: event.content });
|
|
141
143
|
} else if (event.type === "status" && event.metadata?.exitCode !== void 0) {
|
|
142
144
|
exitCode = event.metadata.exitCode;
|
|
@@ -149,8 +151,24 @@ class Sandbox {
|
|
|
149
151
|
return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode };
|
|
150
152
|
}
|
|
151
153
|
// Lifecycle methods
|
|
154
|
+
// Retries on 409 (active runs) to handle eventual consistency between
|
|
155
|
+
// the SSE stream completion and the DB status update
|
|
152
156
|
async kill() {
|
|
153
|
-
|
|
157
|
+
const maxAttempts = 5;
|
|
158
|
+
const baseDelayMs = 500;
|
|
159
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
160
|
+
try {
|
|
161
|
+
await this.client.deleteSandbox(this.sandboxId);
|
|
162
|
+
return;
|
|
163
|
+
} catch (error) {
|
|
164
|
+
const isConflict = error instanceof FetchError && error.status === 409;
|
|
165
|
+
if (isConflict && attempt < maxAttempts) {
|
|
166
|
+
await new Promise((resolve) => setTimeout(resolve, baseDelayMs * attempt));
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
154
172
|
}
|
|
155
173
|
async betaPause() {
|
|
156
174
|
}
|
|
@@ -240,7 +258,7 @@ class SandboxClient {
|
|
|
240
258
|
if (!res.ok) {
|
|
241
259
|
throw await FetchError.from(url, options.method ?? "GET", res);
|
|
242
260
|
}
|
|
243
|
-
return res.json();
|
|
261
|
+
return await res.json();
|
|
244
262
|
}
|
|
245
263
|
// === Runs ===
|
|
246
264
|
async createRun(request) {
|
|
@@ -252,7 +270,7 @@ class SandboxClient {
|
|
|
252
270
|
}
|
|
253
271
|
async createRunAndWait(request) {
|
|
254
272
|
const run = await this.createRun(request);
|
|
255
|
-
return run.waitForCompletion();
|
|
273
|
+
return await run.waitForCompletion();
|
|
256
274
|
}
|
|
257
275
|
async *streamRun(request) {
|
|
258
276
|
const url = `${this.apiUrl}/api/runs/stream`;
|
|
@@ -283,7 +301,7 @@ class SandboxClient {
|
|
|
283
301
|
body: JSON.stringify(request)
|
|
284
302
|
});
|
|
285
303
|
await pollUntilComplete(
|
|
286
|
-
() => this.getSandboxStatus(response.data.sandboxId),
|
|
304
|
+
async () => await this.getSandboxStatus(response.data.sandboxId),
|
|
287
305
|
{ intervalMs: 200, maxAttempts: 150 }
|
|
288
306
|
// 30s max
|
|
289
307
|
);
|
|
@@ -308,7 +326,7 @@ class SandboxClient {
|
|
|
308
326
|
body: JSON.stringify({
|
|
309
327
|
name: path.split("/").pop(),
|
|
310
328
|
path,
|
|
311
|
-
content: isBuffer ? Buffer.from(content).toString("base64") : content,
|
|
329
|
+
content: isBuffer ? node_buffer.Buffer.from(content).toString("base64") : content,
|
|
312
330
|
encoding: isBuffer ? "base64" : "utf8"
|
|
313
331
|
})
|
|
314
332
|
});
|
|
@@ -321,14 +339,14 @@ class SandboxClient {
|
|
|
321
339
|
const format = options?.format ?? "text";
|
|
322
340
|
if (format === "blob") {
|
|
323
341
|
if (file.encoding === "base64") {
|
|
324
|
-
const bytes2 = Uint8Array.from(Buffer.from(file.content, "base64"));
|
|
342
|
+
const bytes2 = Uint8Array.from(node_buffer.Buffer.from(file.content, "base64"));
|
|
325
343
|
return new Blob([bytes2]);
|
|
326
344
|
}
|
|
327
|
-
const bytes = Uint8Array.from(Buffer.from(file.content, "utf8"));
|
|
345
|
+
const bytes = Uint8Array.from(node_buffer.Buffer.from(file.content, "utf8"));
|
|
328
346
|
return new Blob([bytes]);
|
|
329
347
|
}
|
|
330
348
|
if (file.encoding === "base64") {
|
|
331
|
-
return Buffer.from(file.content, "base64").toString("utf8");
|
|
349
|
+
return node_buffer.Buffer.from(file.content, "base64").toString("utf8");
|
|
332
350
|
}
|
|
333
351
|
return file.content;
|
|
334
352
|
}
|
|
@@ -356,6 +374,13 @@ class SandboxClient {
|
|
|
356
374
|
body: JSON.stringify({ filePaths: [path] })
|
|
357
375
|
});
|
|
358
376
|
}
|
|
377
|
+
async uploadBulkFiles(sandboxId, files) {
|
|
378
|
+
const response = await this.fetch(
|
|
379
|
+
`/api/sandboxes/${sandboxId}/files/bulk`,
|
|
380
|
+
{ method: "POST", body: JSON.stringify({ files }) }
|
|
381
|
+
);
|
|
382
|
+
return response.data;
|
|
383
|
+
}
|
|
359
384
|
}
|
|
360
385
|
|
|
361
386
|
class DataTokenAuthStrategy {
|
package/dist/index.d.cts
CHANGED
|
@@ -4,7 +4,7 @@ interface SandboxConfig {
|
|
|
4
4
|
timeout?: number;
|
|
5
5
|
}
|
|
6
6
|
interface AuthStrategy {
|
|
7
|
-
getHeaders()
|
|
7
|
+
getHeaders: () => Headers;
|
|
8
8
|
}
|
|
9
9
|
interface CreateRunRequest {
|
|
10
10
|
code: string;
|
|
@@ -25,11 +25,27 @@ interface CreateSandboxRequest {
|
|
|
25
25
|
sandboxTtlSeconds?: number;
|
|
26
26
|
files?: FileInput[];
|
|
27
27
|
}
|
|
28
|
+
type FileCategory = 'input' | 'skill' | 'settings' | 'context' | 'workspace' | 'output';
|
|
28
29
|
interface FileInput {
|
|
29
30
|
name: string;
|
|
30
31
|
content: string;
|
|
31
32
|
encoding: 'base64' | 'utf8';
|
|
32
33
|
path?: string;
|
|
34
|
+
category?: FileCategory;
|
|
35
|
+
}
|
|
36
|
+
interface BulkFileInput {
|
|
37
|
+
path: string;
|
|
38
|
+
content: string;
|
|
39
|
+
encoding: 'base64' | 'utf8';
|
|
40
|
+
}
|
|
41
|
+
interface BulkUploadResult {
|
|
42
|
+
uploaded: Array<{
|
|
43
|
+
path: string;
|
|
44
|
+
}>;
|
|
45
|
+
errors?: Array<{
|
|
46
|
+
path: string;
|
|
47
|
+
error: string;
|
|
48
|
+
}>;
|
|
33
49
|
}
|
|
34
50
|
type Status = 'pending' | 'running' | 'completed' | 'failed' | 'timeout' | 'cancelled';
|
|
35
51
|
interface RunResponse {
|
|
@@ -95,6 +111,7 @@ interface SandboxFiles {
|
|
|
95
111
|
exists: (path: string) => Promise<boolean>;
|
|
96
112
|
remove: (path: string) => Promise<void>;
|
|
97
113
|
list: (directory?: string) => Promise<string[]>;
|
|
114
|
+
uploadBulk: (files: BulkFileInput[]) => Promise<BulkUploadResult>;
|
|
98
115
|
}
|
|
99
116
|
interface SandboxCommands {
|
|
100
117
|
run: (command: string) => Promise<{
|
|
@@ -172,6 +189,7 @@ declare class SandboxClient {
|
|
|
172
189
|
fileExists(sandboxId: string, path: string): Promise<boolean>;
|
|
173
190
|
listFiles(sandboxId: string, directory?: string): Promise<string[]>;
|
|
174
191
|
deleteFile(sandboxId: string, path: string): Promise<void>;
|
|
192
|
+
uploadBulkFiles(sandboxId: string, files: BulkFileInput[]): Promise<BulkUploadResult>;
|
|
175
193
|
}
|
|
176
194
|
|
|
177
195
|
declare class DataTokenAuthStrategy implements AuthStrategy {
|
package/dist/index.d.mts
CHANGED
|
@@ -4,7 +4,7 @@ interface SandboxConfig {
|
|
|
4
4
|
timeout?: number;
|
|
5
5
|
}
|
|
6
6
|
interface AuthStrategy {
|
|
7
|
-
getHeaders()
|
|
7
|
+
getHeaders: () => Headers;
|
|
8
8
|
}
|
|
9
9
|
interface CreateRunRequest {
|
|
10
10
|
code: string;
|
|
@@ -25,11 +25,27 @@ interface CreateSandboxRequest {
|
|
|
25
25
|
sandboxTtlSeconds?: number;
|
|
26
26
|
files?: FileInput[];
|
|
27
27
|
}
|
|
28
|
+
type FileCategory = 'input' | 'skill' | 'settings' | 'context' | 'workspace' | 'output';
|
|
28
29
|
interface FileInput {
|
|
29
30
|
name: string;
|
|
30
31
|
content: string;
|
|
31
32
|
encoding: 'base64' | 'utf8';
|
|
32
33
|
path?: string;
|
|
34
|
+
category?: FileCategory;
|
|
35
|
+
}
|
|
36
|
+
interface BulkFileInput {
|
|
37
|
+
path: string;
|
|
38
|
+
content: string;
|
|
39
|
+
encoding: 'base64' | 'utf8';
|
|
40
|
+
}
|
|
41
|
+
interface BulkUploadResult {
|
|
42
|
+
uploaded: Array<{
|
|
43
|
+
path: string;
|
|
44
|
+
}>;
|
|
45
|
+
errors?: Array<{
|
|
46
|
+
path: string;
|
|
47
|
+
error: string;
|
|
48
|
+
}>;
|
|
33
49
|
}
|
|
34
50
|
type Status = 'pending' | 'running' | 'completed' | 'failed' | 'timeout' | 'cancelled';
|
|
35
51
|
interface RunResponse {
|
|
@@ -95,6 +111,7 @@ interface SandboxFiles {
|
|
|
95
111
|
exists: (path: string) => Promise<boolean>;
|
|
96
112
|
remove: (path: string) => Promise<void>;
|
|
97
113
|
list: (directory?: string) => Promise<string[]>;
|
|
114
|
+
uploadBulk: (files: BulkFileInput[]) => Promise<BulkUploadResult>;
|
|
98
115
|
}
|
|
99
116
|
interface SandboxCommands {
|
|
100
117
|
run: (command: string) => Promise<{
|
|
@@ -172,6 +189,7 @@ declare class SandboxClient {
|
|
|
172
189
|
fileExists(sandboxId: string, path: string): Promise<boolean>;
|
|
173
190
|
listFiles(sandboxId: string, directory?: string): Promise<string[]>;
|
|
174
191
|
deleteFile(sandboxId: string, path: string): Promise<void>;
|
|
192
|
+
uploadBulkFiles(sandboxId: string, files: BulkFileInput[]): Promise<BulkUploadResult>;
|
|
175
193
|
}
|
|
176
194
|
|
|
177
195
|
declare class DataTokenAuthStrategy implements AuthStrategy {
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ interface SandboxConfig {
|
|
|
4
4
|
timeout?: number;
|
|
5
5
|
}
|
|
6
6
|
interface AuthStrategy {
|
|
7
|
-
getHeaders()
|
|
7
|
+
getHeaders: () => Headers;
|
|
8
8
|
}
|
|
9
9
|
interface CreateRunRequest {
|
|
10
10
|
code: string;
|
|
@@ -25,11 +25,27 @@ interface CreateSandboxRequest {
|
|
|
25
25
|
sandboxTtlSeconds?: number;
|
|
26
26
|
files?: FileInput[];
|
|
27
27
|
}
|
|
28
|
+
type FileCategory = 'input' | 'skill' | 'settings' | 'context' | 'workspace' | 'output';
|
|
28
29
|
interface FileInput {
|
|
29
30
|
name: string;
|
|
30
31
|
content: string;
|
|
31
32
|
encoding: 'base64' | 'utf8';
|
|
32
33
|
path?: string;
|
|
34
|
+
category?: FileCategory;
|
|
35
|
+
}
|
|
36
|
+
interface BulkFileInput {
|
|
37
|
+
path: string;
|
|
38
|
+
content: string;
|
|
39
|
+
encoding: 'base64' | 'utf8';
|
|
40
|
+
}
|
|
41
|
+
interface BulkUploadResult {
|
|
42
|
+
uploaded: Array<{
|
|
43
|
+
path: string;
|
|
44
|
+
}>;
|
|
45
|
+
errors?: Array<{
|
|
46
|
+
path: string;
|
|
47
|
+
error: string;
|
|
48
|
+
}>;
|
|
33
49
|
}
|
|
34
50
|
type Status = 'pending' | 'running' | 'completed' | 'failed' | 'timeout' | 'cancelled';
|
|
35
51
|
interface RunResponse {
|
|
@@ -95,6 +111,7 @@ interface SandboxFiles {
|
|
|
95
111
|
exists: (path: string) => Promise<boolean>;
|
|
96
112
|
remove: (path: string) => Promise<void>;
|
|
97
113
|
list: (directory?: string) => Promise<string[]>;
|
|
114
|
+
uploadBulk: (files: BulkFileInput[]) => Promise<BulkUploadResult>;
|
|
98
115
|
}
|
|
99
116
|
interface SandboxCommands {
|
|
100
117
|
run: (command: string) => Promise<{
|
|
@@ -172,6 +189,7 @@ declare class SandboxClient {
|
|
|
172
189
|
fileExists(sandboxId: string, path: string): Promise<boolean>;
|
|
173
190
|
listFiles(sandboxId: string, directory?: string): Promise<string[]>;
|
|
174
191
|
deleteFile(sandboxId: string, path: string): Promise<void>;
|
|
192
|
+
uploadBulkFiles(sandboxId: string, files: BulkFileInput[]): Promise<BulkUploadResult>;
|
|
175
193
|
}
|
|
176
194
|
|
|
177
195
|
declare class DataTokenAuthStrategy implements AuthStrategy {
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
1
2
|
import { createParser } from 'eventsource-parser';
|
|
2
3
|
|
|
3
4
|
class FetchError extends Error {
|
|
@@ -22,8 +23,7 @@ class FetchError extends Error {
|
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
async function* parseSSEStream(response) {
|
|
25
|
-
|
|
26
|
-
if (!reader)
|
|
26
|
+
if (!response.body)
|
|
27
27
|
throw new Error("No response body");
|
|
28
28
|
const decoder = new TextDecoder();
|
|
29
29
|
const events = [];
|
|
@@ -37,11 +37,8 @@ async function* parseSSEStream(response) {
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
});
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (done)
|
|
43
|
-
break;
|
|
44
|
-
parser.feed(decoder.decode(value, { stream: true }));
|
|
40
|
+
for await (const chunk of response.body) {
|
|
41
|
+
parser.feed(decoder.decode(chunk, { stream: true }));
|
|
45
42
|
while (events.length > 0) {
|
|
46
43
|
yield events.shift();
|
|
47
44
|
}
|
|
@@ -79,15 +76,16 @@ class Sandbox {
|
|
|
79
76
|
__publicField$2(this, "commands");
|
|
80
77
|
__publicField$2(this, "timeoutMs", 3e5);
|
|
81
78
|
this.sandboxId = sandboxId;
|
|
82
|
-
const readFile = (path, options) => {
|
|
83
|
-
return this.client.readFile(sandboxId, path, options);
|
|
79
|
+
const readFile = async (path, options) => {
|
|
80
|
+
return await this.client.readFile(sandboxId, path, options);
|
|
84
81
|
};
|
|
85
82
|
this.files = {
|
|
86
|
-
write: (path, content) => this.client.writeFile(sandboxId, path, content),
|
|
83
|
+
write: async (path, content) => await this.client.writeFile(sandboxId, path, content),
|
|
87
84
|
read: readFile,
|
|
88
|
-
exists: (path) => this.client.fileExists(sandboxId, path),
|
|
89
|
-
remove: (path) => this.client.deleteFile(sandboxId, path),
|
|
90
|
-
list: (directory) => this.client.listFiles(sandboxId, directory)
|
|
85
|
+
exists: async (path) => await this.client.fileExists(sandboxId, path),
|
|
86
|
+
remove: async (path) => await this.client.deleteFile(sandboxId, path),
|
|
87
|
+
list: async (directory) => await this.client.listFiles(sandboxId, directory),
|
|
88
|
+
uploadBulk: async (files) => await this.client.uploadBulkFiles(sandboxId, files)
|
|
91
89
|
};
|
|
92
90
|
this.commands = {
|
|
93
91
|
run: async (command) => {
|
|
@@ -102,10 +100,10 @@ class Sandbox {
|
|
|
102
100
|
}
|
|
103
101
|
// Factory methods (static)
|
|
104
102
|
static async create(client, options = {}) {
|
|
105
|
-
return client.createSandbox(options);
|
|
103
|
+
return await client.createSandbox(options);
|
|
106
104
|
}
|
|
107
105
|
static async connect(client, sandboxId) {
|
|
108
|
-
return client.connectToSandbox(sandboxId);
|
|
106
|
+
return await client.connectToSandbox(sandboxId);
|
|
109
107
|
}
|
|
110
108
|
// Normalize language aliases ('ts' -> 'typescript', 'js' -> 'javascript')
|
|
111
109
|
normalizeLanguage(lang) {
|
|
@@ -113,7 +111,9 @@ class Sandbox {
|
|
|
113
111
|
return "typescript";
|
|
114
112
|
if (lang === "js")
|
|
115
113
|
return "javascript";
|
|
116
|
-
|
|
114
|
+
if (lang === "typescript" || lang === "javascript" || lang === "python" || lang === "bash")
|
|
115
|
+
return lang;
|
|
116
|
+
return "typescript";
|
|
117
117
|
}
|
|
118
118
|
// Main execution method (compatible with E2B and agent-api)
|
|
119
119
|
async runCode(script, options = {}) {
|
|
@@ -131,10 +131,12 @@ class Sandbox {
|
|
|
131
131
|
});
|
|
132
132
|
for await (const event of stream) {
|
|
133
133
|
if (event.type === "stdout" && event.content) {
|
|
134
|
-
stdout += event.content
|
|
134
|
+
stdout += `${event.content}
|
|
135
|
+
`;
|
|
135
136
|
await options.onStdout?.({ line: event.content });
|
|
136
137
|
} else if (event.type === "stderr" && event.content) {
|
|
137
|
-
stderr += event.content
|
|
138
|
+
stderr += `${event.content}
|
|
139
|
+
`;
|
|
138
140
|
await options.onStderr?.({ line: event.content });
|
|
139
141
|
} else if (event.type === "status" && event.metadata?.exitCode !== void 0) {
|
|
140
142
|
exitCode = event.metadata.exitCode;
|
|
@@ -147,8 +149,24 @@ class Sandbox {
|
|
|
147
149
|
return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode };
|
|
148
150
|
}
|
|
149
151
|
// Lifecycle methods
|
|
152
|
+
// Retries on 409 (active runs) to handle eventual consistency between
|
|
153
|
+
// the SSE stream completion and the DB status update
|
|
150
154
|
async kill() {
|
|
151
|
-
|
|
155
|
+
const maxAttempts = 5;
|
|
156
|
+
const baseDelayMs = 500;
|
|
157
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
158
|
+
try {
|
|
159
|
+
await this.client.deleteSandbox(this.sandboxId);
|
|
160
|
+
return;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
const isConflict = error instanceof FetchError && error.status === 409;
|
|
163
|
+
if (isConflict && attempt < maxAttempts) {
|
|
164
|
+
await new Promise((resolve) => setTimeout(resolve, baseDelayMs * attempt));
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
152
170
|
}
|
|
153
171
|
async betaPause() {
|
|
154
172
|
}
|
|
@@ -238,7 +256,7 @@ class SandboxClient {
|
|
|
238
256
|
if (!res.ok) {
|
|
239
257
|
throw await FetchError.from(url, options.method ?? "GET", res);
|
|
240
258
|
}
|
|
241
|
-
return res.json();
|
|
259
|
+
return await res.json();
|
|
242
260
|
}
|
|
243
261
|
// === Runs ===
|
|
244
262
|
async createRun(request) {
|
|
@@ -250,7 +268,7 @@ class SandboxClient {
|
|
|
250
268
|
}
|
|
251
269
|
async createRunAndWait(request) {
|
|
252
270
|
const run = await this.createRun(request);
|
|
253
|
-
return run.waitForCompletion();
|
|
271
|
+
return await run.waitForCompletion();
|
|
254
272
|
}
|
|
255
273
|
async *streamRun(request) {
|
|
256
274
|
const url = `${this.apiUrl}/api/runs/stream`;
|
|
@@ -281,7 +299,7 @@ class SandboxClient {
|
|
|
281
299
|
body: JSON.stringify(request)
|
|
282
300
|
});
|
|
283
301
|
await pollUntilComplete(
|
|
284
|
-
() => this.getSandboxStatus(response.data.sandboxId),
|
|
302
|
+
async () => await this.getSandboxStatus(response.data.sandboxId),
|
|
285
303
|
{ intervalMs: 200, maxAttempts: 150 }
|
|
286
304
|
// 30s max
|
|
287
305
|
);
|
|
@@ -354,6 +372,13 @@ class SandboxClient {
|
|
|
354
372
|
body: JSON.stringify({ filePaths: [path] })
|
|
355
373
|
});
|
|
356
374
|
}
|
|
375
|
+
async uploadBulkFiles(sandboxId, files) {
|
|
376
|
+
const response = await this.fetch(
|
|
377
|
+
`/api/sandboxes/${sandboxId}/files/bulk`,
|
|
378
|
+
{ method: "POST", body: JSON.stringify({ files }) }
|
|
379
|
+
);
|
|
380
|
+
return response.data;
|
|
381
|
+
}
|
|
357
382
|
}
|
|
358
383
|
|
|
359
384
|
class DataTokenAuthStrategy {
|
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.0.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
|
}
|