@meistrari/sandbox-sdk 0.1.4
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 +373 -0
- package/dist/index.cjs +381 -0
- package/dist/index.d.cts +195 -0
- package/dist/index.d.mts +195 -0
- package/dist/index.d.ts +195 -0
- package/dist/index.mjs +374 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
# @meistrari/sandbox-sdk
|
|
2
|
+
|
|
3
|
+
SDK for Sandbox - a secure code execution service.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @meistrari/sandbox-sdk
|
|
9
|
+
# or
|
|
10
|
+
bun add @meistrari/sandbox-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { sandbox, DataTokenAuthStrategy } from '@meistrari/sandbox-sdk'
|
|
17
|
+
|
|
18
|
+
// Create client
|
|
19
|
+
const client = sandbox({
|
|
20
|
+
apiUrl: 'https://sandbox.meistrari.com',
|
|
21
|
+
auth: new DataTokenAuthStrategy(dataToken),
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// Execute code
|
|
25
|
+
const run = await client.createRunAndWait({
|
|
26
|
+
code: 'console.log("Hello, World!")',
|
|
27
|
+
language: 'typescript',
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
console.log(run.stdout) // "Hello, World!"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
### Simple Code Execution
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
// One-shot execution (no persistent sandbox)
|
|
39
|
+
const run = await client.createRunAndWait({
|
|
40
|
+
code: `
|
|
41
|
+
const result = 1 + 1
|
|
42
|
+
console.log(result)
|
|
43
|
+
`,
|
|
44
|
+
language: 'typescript',
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
console.log(run.stdout) // "2"
|
|
48
|
+
console.log(run.exitCode) // 0
|
|
49
|
+
console.log(run.isSuccess) // true
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Persistent Sandbox
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { Sandbox } from '@meistrari/sandbox-sdk'
|
|
56
|
+
|
|
57
|
+
// Create a persistent sandbox
|
|
58
|
+
const sandbox = await Sandbox.create(client, {
|
|
59
|
+
memSize: 512,
|
|
60
|
+
cpuSize: 1.0,
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
// Execute multiple commands in the same sandbox
|
|
64
|
+
const result1 = await sandbox.runCode('const x = 42', { language: 'typescript' })
|
|
65
|
+
const result2 = await sandbox.runCode('console.log(x)', { language: 'typescript' })
|
|
66
|
+
|
|
67
|
+
console.log(result2.stdout) // "42"
|
|
68
|
+
|
|
69
|
+
// Cleanup when done
|
|
70
|
+
await sandbox.kill()
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### File Operations
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
const sandbox = await client.createSandbox()
|
|
77
|
+
|
|
78
|
+
// Write files
|
|
79
|
+
await sandbox.files.write('/home/user/config.json', '{"key": "value"}')
|
|
80
|
+
await sandbox.files.write('/home/user/data.bin', binaryData) // Uint8Array
|
|
81
|
+
|
|
82
|
+
// Read files
|
|
83
|
+
const text = await sandbox.files.read('/home/user/config.json')
|
|
84
|
+
const binary = await sandbox.files.read('/home/user/data.bin', { format: 'blob' })
|
|
85
|
+
|
|
86
|
+
// Check if file exists
|
|
87
|
+
const exists = await sandbox.files.exists('/home/user/config.json')
|
|
88
|
+
|
|
89
|
+
// List files
|
|
90
|
+
const files = await sandbox.files.list('/home/user')
|
|
91
|
+
|
|
92
|
+
// Remove files
|
|
93
|
+
await sandbox.files.remove('/home/user/config.json')
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Shell Commands
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const sandbox = await client.createSandbox()
|
|
100
|
+
|
|
101
|
+
// Run shell commands
|
|
102
|
+
const { stdout, stderr, exitCode } = await sandbox.commands.run('ls -la')
|
|
103
|
+
console.log(stdout)
|
|
104
|
+
|
|
105
|
+
// Install packages
|
|
106
|
+
await sandbox.commands.run('bun add lodash')
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Using Libraries
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
const sandbox = await client.createSandbox()
|
|
113
|
+
|
|
114
|
+
// Execute code with npm libraries (installed automatically)
|
|
115
|
+
const result = await sandbox.runCode(`
|
|
116
|
+
import _ from 'lodash'
|
|
117
|
+
import { z } from 'zod'
|
|
118
|
+
|
|
119
|
+
const schema = z.object({ name: z.string(), age: z.number() })
|
|
120
|
+
const data = { name: 'John', age: 30, extra: 'ignored' }
|
|
121
|
+
|
|
122
|
+
console.log(_.pick(data, ['name', 'age']))
|
|
123
|
+
console.log(schema.parse(data))
|
|
124
|
+
`, {
|
|
125
|
+
language: 'typescript',
|
|
126
|
+
libraries: ['lodash', 'zod'],
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
console.log(result.stdout)
|
|
130
|
+
// { name: 'John', age: 30 }
|
|
131
|
+
// { name: 'John', age: 30 }
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Using Environment Variables
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
const sandbox = await client.createSandbox()
|
|
138
|
+
|
|
139
|
+
const result = await sandbox.runCode(`
|
|
140
|
+
console.log('API Key:', process.env.API_KEY)
|
|
141
|
+
console.log('Environment:', process.env.NODE_ENV)
|
|
142
|
+
`, {
|
|
143
|
+
language: 'typescript',
|
|
144
|
+
envs: {
|
|
145
|
+
API_KEY: 'secret-key-123',
|
|
146
|
+
NODE_ENV: 'production',
|
|
147
|
+
},
|
|
148
|
+
})
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Using Input Files
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
const sandbox = await client.createSandbox()
|
|
155
|
+
|
|
156
|
+
// Execute code with input files provided inline
|
|
157
|
+
const result = await sandbox.runCode(`
|
|
158
|
+
import fs from 'fs'
|
|
159
|
+
|
|
160
|
+
const config = JSON.parse(fs.readFileSync('/workspace/user-code/config.json', 'utf8'))
|
|
161
|
+
const data = fs.readFileSync('/workspace/user-code/data.csv', 'utf8')
|
|
162
|
+
|
|
163
|
+
console.log('Config:', config)
|
|
164
|
+
console.log('Data lines:', data.split('\\n').length)
|
|
165
|
+
`, {
|
|
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
|
+
})
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Complete Example (Libraries + Envs + Files)
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
const sandbox = await client.createSandbox({ memSize: 512 })
|
|
186
|
+
|
|
187
|
+
const result = await sandbox.runCode(`
|
|
188
|
+
import Papa from 'papaparse'
|
|
189
|
+
import fs from 'fs'
|
|
190
|
+
|
|
191
|
+
const csvData = fs.readFileSync('/workspace/user-code/users.csv', 'utf8')
|
|
192
|
+
const parsed = Papa.parse(csvData, { header: true })
|
|
193
|
+
|
|
194
|
+
const apiKey = process.env.PARSER_API_KEY
|
|
195
|
+
console.log('Using API Key:', apiKey.substring(0, 4) + '...')
|
|
196
|
+
console.log('Parsed records:', parsed.data.length)
|
|
197
|
+
console.log('First record:', JSON.stringify(parsed.data[0]))
|
|
198
|
+
`, {
|
|
199
|
+
language: 'typescript',
|
|
200
|
+
libraries: ['papaparse'],
|
|
201
|
+
envs: {
|
|
202
|
+
PARSER_API_KEY: 'pk_live_abc123xyz',
|
|
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',
|
|
209
|
+
},
|
|
210
|
+
],
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
await sandbox.kill()
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Streaming Output
|
|
217
|
+
|
|
218
|
+
```typescript
|
|
219
|
+
const result = await sandbox.runCode(longRunningScript, {
|
|
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
|
+
})
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Connect to Existing Sandbox
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
// Reconnect to a previously created sandbox
|
|
231
|
+
const sandbox = await Sandbox.connect(client, existingSandboxId)
|
|
232
|
+
|
|
233
|
+
// Continue working
|
|
234
|
+
await sandbox.runCode('console.log("I am back!")')
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## API Reference
|
|
238
|
+
|
|
239
|
+
### `sandbox(config)`
|
|
240
|
+
|
|
241
|
+
Creates a new Sandbox client instance.
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
const client = sandbox({
|
|
245
|
+
apiUrl: string, // Sandbox API URL
|
|
246
|
+
auth: AuthStrategy, // Authentication strategy
|
|
247
|
+
timeout?: number, // Request timeout in ms (default: 300000)
|
|
248
|
+
})
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### `SandboxClient`
|
|
252
|
+
|
|
253
|
+
#### Methods
|
|
254
|
+
|
|
255
|
+
| Method | Description |
|
|
256
|
+
|--------|-------------|
|
|
257
|
+
| `createRun(request)` | Create a code execution run |
|
|
258
|
+
| `createRunAndWait(request)` | Create and wait for completion |
|
|
259
|
+
| `streamRun(request)` | Create run with SSE streaming |
|
|
260
|
+
| `getRun(runId)` | Get run by ID |
|
|
261
|
+
| `createSandbox(options)` | Create a persistent sandbox |
|
|
262
|
+
| `connectToSandbox(sandboxId)` | Connect to existing sandbox |
|
|
263
|
+
| `deleteSandbox(sandboxId)` | Delete a sandbox |
|
|
264
|
+
|
|
265
|
+
### `Sandbox`
|
|
266
|
+
|
|
267
|
+
#### Properties
|
|
268
|
+
|
|
269
|
+
| Property | Type | Description |
|
|
270
|
+
|----------|------|-------------|
|
|
271
|
+
| `sandboxId` | `string` | Unique sandbox identifier |
|
|
272
|
+
| `files` | `SandboxFiles` | File operations interface |
|
|
273
|
+
| `commands` | `SandboxCommands` | Command execution interface |
|
|
274
|
+
|
|
275
|
+
#### Methods
|
|
276
|
+
|
|
277
|
+
| Method | Description |
|
|
278
|
+
|--------|-------------|
|
|
279
|
+
| `runCode(code, options)` | Execute code in the sandbox |
|
|
280
|
+
| `kill()` | Terminate the sandbox |
|
|
281
|
+
| `setTimeout(ms)` | Set execution timeout |
|
|
282
|
+
|
|
283
|
+
### `SandboxRun`
|
|
284
|
+
|
|
285
|
+
#### Properties
|
|
286
|
+
|
|
287
|
+
| Property | Type | Description |
|
|
288
|
+
|----------|------|-------------|
|
|
289
|
+
| `id` | `string` | Run ID |
|
|
290
|
+
| `sandboxId` | `string?` | Associated sandbox ID |
|
|
291
|
+
| `status` | `Status` | Current status |
|
|
292
|
+
| `stdout` | `string?` | Standard output |
|
|
293
|
+
| `stderr` | `string?` | Standard error |
|
|
294
|
+
| `exitCode` | `number?` | Exit code |
|
|
295
|
+
| `isComplete` | `boolean` | Whether run has finished |
|
|
296
|
+
| `isSuccess` | `boolean` | Whether run succeeded |
|
|
297
|
+
|
|
298
|
+
### Types
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
type Status = 'pending' | 'running' | 'completed' | 'failed' | 'timeout' | 'cancelled'
|
|
302
|
+
|
|
303
|
+
interface CreateRunRequest {
|
|
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
|
+
}
|
|
315
|
+
|
|
316
|
+
interface RunCodeOptions {
|
|
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
|
+
}
|
|
326
|
+
|
|
327
|
+
interface FileInput {
|
|
328
|
+
name: string // filename
|
|
329
|
+
content: string // file content
|
|
330
|
+
encoding: 'utf8' | 'base64'
|
|
331
|
+
path?: string // optional custom path
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
## E2B Compatibility
|
|
336
|
+
|
|
337
|
+
This SDK is designed to be compatible with E2B's `SandboxLike` interface, making migration straightforward:
|
|
338
|
+
|
|
339
|
+
```typescript
|
|
340
|
+
// E2B
|
|
341
|
+
import { Sandbox } from '@e2b/code-interpreter'
|
|
342
|
+
const sandbox = await Sandbox.create(templateId, { apiKey })
|
|
343
|
+
|
|
344
|
+
// Sandbox (drop-in replacement)
|
|
345
|
+
import { Sandbox, sandbox, DataTokenAuthStrategy } from '@meistrari/sandbox-sdk'
|
|
346
|
+
const client = sandbox({ apiUrl, auth: new DataTokenAuthStrategy(token) })
|
|
347
|
+
const sandbox = await Sandbox.create(client)
|
|
348
|
+
|
|
349
|
+
// Same interface works for both
|
|
350
|
+
await sandbox.files.write('/path', content)
|
|
351
|
+
await sandbox.runCode(code, { onStdout: ({ line }) => console.log(line) })
|
|
352
|
+
await sandbox.kill()
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Error Handling
|
|
356
|
+
|
|
357
|
+
```typescript
|
|
358
|
+
import { FetchError } from '@meistrari/sandbox-sdk'
|
|
359
|
+
|
|
360
|
+
try {
|
|
361
|
+
const run = await client.createRunAndWait({ code: 'invalid(' })
|
|
362
|
+
} catch (error) {
|
|
363
|
+
if (error instanceof FetchError) {
|
|
364
|
+
console.error(`API Error: ${error.message}`)
|
|
365
|
+
console.error(`Status: ${error.status}`)
|
|
366
|
+
console.error(`URL: ${error.url}`)
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## License
|
|
372
|
+
|
|
373
|
+
MIT
|