computesdk 2.6.0 → 4.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 +10 -325
- package/dist/index.d.mts +19 -3743
- package/dist/index.d.ts +19 -3743
- package/dist/index.js +9 -3667
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -3655
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,8 +28,8 @@ compute.setConfig({
|
|
|
28
28
|
const sandbox = await compute.sandbox.create();
|
|
29
29
|
|
|
30
30
|
// Execute code
|
|
31
|
-
const result = await sandbox.
|
|
32
|
-
console.log(result.
|
|
31
|
+
const result = await sandbox.runCommand('python -c "print(\"Hello World!\")"');
|
|
32
|
+
console.log(result.stdout); // "Hello World!"
|
|
33
33
|
|
|
34
34
|
// Clean up
|
|
35
35
|
await sandbox.destroy();
|
|
@@ -179,9 +179,9 @@ const sandbox = await compute.sandbox.create({
|
|
|
179
179
|
- `metadata?: Record<string, any>` - Custom metadata
|
|
180
180
|
- `envs?: Record<string, string>` - Environment variables
|
|
181
181
|
- `namespace?: string` - Namespace for organizing sandboxes
|
|
182
|
-
- `name?: string` -
|
|
183
|
-
|
|
184
|
-
|
|
182
|
+
- `name?: string` - Human-readable name for the sandbox
|
|
183
|
+
|
|
184
|
+
> **Note:** Not every provider honors every option. Support for fields like `name`, `metadata`, and `envs` depends on the underlying provider SDK — some pass them through, some map them to a different field, and some ignore them silently. Check your provider package's README for the exact set of options it respects.
|
|
185
185
|
|
|
186
186
|
#### `compute.sandbox.getById(sandboxId)`
|
|
187
187
|
|
|
@@ -191,40 +191,8 @@ Get an existing sandbox by ID.
|
|
|
191
191
|
const sandbox = await compute.sandbox.getById('sandbox-id');
|
|
192
192
|
```
|
|
193
193
|
|
|
194
|
-
#### `compute.sandbox.findOrCreate(options)`
|
|
195
|
-
|
|
196
|
-
Find an existing sandbox by namespace and name, or create a new one.
|
|
197
|
-
|
|
198
|
-
```typescript
|
|
199
|
-
const sandbox = await compute.sandbox.findOrCreate({
|
|
200
|
-
namespace: 'my-org',
|
|
201
|
-
name: 'my-project',
|
|
202
|
-
});
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
#### `compute.sandbox.find(options)`
|
|
206
|
-
|
|
207
|
-
Find an existing sandbox by namespace and name (returns null if not found).
|
|
208
|
-
|
|
209
|
-
```typescript
|
|
210
|
-
const sandbox = await compute.sandbox.find({
|
|
211
|
-
namespace: 'my-org',
|
|
212
|
-
name: 'my-project',
|
|
213
|
-
});
|
|
214
|
-
```
|
|
215
|
-
|
|
216
194
|
### Sandbox Operations
|
|
217
195
|
|
|
218
|
-
#### `sandbox.runCode(code, language?)`
|
|
219
|
-
|
|
220
|
-
Execute code in the sandbox.
|
|
221
|
-
|
|
222
|
-
```typescript
|
|
223
|
-
const result = await sandbox.runCode('print("Hello")', 'python');
|
|
224
|
-
console.log(result.output); // "Hello"
|
|
225
|
-
console.log(result.exitCode);
|
|
226
|
-
```
|
|
227
|
-
|
|
228
196
|
#### `sandbox.runCommand(command, options?)`
|
|
229
197
|
|
|
230
198
|
Run a shell command.
|
|
@@ -305,209 +273,6 @@ Remove a file or directory.
|
|
|
305
273
|
await sandbox.filesystem.remove('/tmp/hello.py');
|
|
306
274
|
```
|
|
307
275
|
|
|
308
|
-
### Terminal Operations
|
|
309
|
-
|
|
310
|
-
The sandbox provides terminal access in two modes: **PTY mode** (interactive shell) and **Exec mode** (command tracking).
|
|
311
|
-
|
|
312
|
-
#### `sandbox.terminal.create(options?)`
|
|
313
|
-
|
|
314
|
-
Create a new terminal session.
|
|
315
|
-
|
|
316
|
-
```typescript
|
|
317
|
-
// PTY mode - Interactive shell
|
|
318
|
-
const pty = await sandbox.terminal.create({ pty: true, shell: '/bin/bash' });
|
|
319
|
-
|
|
320
|
-
// Exec mode - Command tracking (default)
|
|
321
|
-
const exec = await sandbox.terminal.create({ pty: false });
|
|
322
|
-
```
|
|
323
|
-
|
|
324
|
-
**Options:**
|
|
325
|
-
- `pty?: boolean` - Terminal mode: `true` = PTY (interactive), `false` = exec (command tracking). Default: `false`
|
|
326
|
-
- `shell?: string` - Shell to use (e.g., '/bin/bash'). PTY mode only
|
|
327
|
-
- `encoding?: 'raw' | 'base64'` - Output encoding. Default: `'raw'`
|
|
328
|
-
|
|
329
|
-
**Returns:** `TerminalInstance`
|
|
330
|
-
|
|
331
|
-
#### `sandbox.terminal.list()`
|
|
332
|
-
|
|
333
|
-
List all active terminals.
|
|
334
|
-
|
|
335
|
-
```typescript
|
|
336
|
-
const terminals = await sandbox.terminal.list();
|
|
337
|
-
console.log(terminals); // [{ id: 'term-1', pty: true, status: 'running', ... }]
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
#### `sandbox.terminal.retrieve(id)`
|
|
341
|
-
|
|
342
|
-
Retrieve a specific terminal by ID.
|
|
343
|
-
|
|
344
|
-
```typescript
|
|
345
|
-
const terminal = await sandbox.terminal.retrieve('term-123');
|
|
346
|
-
console.log(terminal.id, terminal.status);
|
|
347
|
-
```
|
|
348
|
-
|
|
349
|
-
#### `sandbox.terminal.destroy(id)`
|
|
350
|
-
|
|
351
|
-
Destroy a terminal by ID.
|
|
352
|
-
|
|
353
|
-
```typescript
|
|
354
|
-
await sandbox.terminal.destroy('term-123');
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
### PTY Mode (Interactive Shell)
|
|
358
|
-
|
|
359
|
-
PTY mode creates an interactive shell session with real-time input/output over WebSocket.
|
|
360
|
-
|
|
361
|
-
#### `terminal.write(input)`
|
|
362
|
-
|
|
363
|
-
Send input to the terminal shell.
|
|
364
|
-
|
|
365
|
-
```typescript
|
|
366
|
-
const pty = await sandbox.terminal.create({ pty: true });
|
|
367
|
-
pty.on('output', (data) => console.log(data));
|
|
368
|
-
pty.write('ls -la\n');
|
|
369
|
-
pty.write('pwd\n');
|
|
370
|
-
await pty.destroy();
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
#### `terminal.resize(cols, rows)`
|
|
374
|
-
|
|
375
|
-
Resize the terminal window.
|
|
376
|
-
|
|
377
|
-
```typescript
|
|
378
|
-
pty.resize(120, 40);
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
#### `terminal.on(event, handler)`
|
|
382
|
-
|
|
383
|
-
Register an event handler. Events: `'output'`, `'error'`, `'destroyed'`.
|
|
384
|
-
|
|
385
|
-
```typescript
|
|
386
|
-
pty.on('output', (data) => console.log('Output:', data));
|
|
387
|
-
pty.on('error', (error) => console.error('Error:', error));
|
|
388
|
-
pty.on('destroyed', () => console.log('Terminal destroyed'));
|
|
389
|
-
```
|
|
390
|
-
|
|
391
|
-
#### `terminal.off(event, handler)`
|
|
392
|
-
|
|
393
|
-
Unregister an event handler.
|
|
394
|
-
|
|
395
|
-
```typescript
|
|
396
|
-
const handler = (data) => console.log(data);
|
|
397
|
-
pty.on('output', handler);
|
|
398
|
-
pty.off('output', handler);
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
#### Terminal Properties
|
|
402
|
-
|
|
403
|
-
```typescript
|
|
404
|
-
console.log(pty.id); // Terminal ID
|
|
405
|
-
console.log(pty.status); // 'running' | 'stopped' | 'active' | 'ready'
|
|
406
|
-
console.log(pty.channel); // WebSocket channel (PTY only)
|
|
407
|
-
console.log(pty.pty); // true for PTY mode
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
### Exec Mode (Command Tracking)
|
|
411
|
-
|
|
412
|
-
Exec mode executes commands with structured result tracking, suitable for automation.
|
|
413
|
-
|
|
414
|
-
#### `terminal.command.run(command, options?)`
|
|
415
|
-
|
|
416
|
-
Execute a command in the terminal.
|
|
417
|
-
|
|
418
|
-
```typescript
|
|
419
|
-
const exec = await sandbox.terminal.create({ pty: false });
|
|
420
|
-
|
|
421
|
-
// Foreground execution (waits for completion)
|
|
422
|
-
const cmd = await exec.command.run('npm test');
|
|
423
|
-
console.log(cmd.stdout);
|
|
424
|
-
console.log(cmd.stderr);
|
|
425
|
-
console.log(cmd.exitCode);
|
|
426
|
-
|
|
427
|
-
// Background execution (returns immediately)
|
|
428
|
-
const bgCmd = await exec.command.run('npm install', { background: true });
|
|
429
|
-
console.log(bgCmd.status); // 'running'
|
|
430
|
-
await bgCmd.wait(); // Wait for completion
|
|
431
|
-
console.log(bgCmd.exitCode);
|
|
432
|
-
```
|
|
433
|
-
|
|
434
|
-
**Parameters:**
|
|
435
|
-
- `command: string` - The command to execute
|
|
436
|
-
- `options?: { background?: boolean }` - Execution options
|
|
437
|
-
|
|
438
|
-
**Returns:** `Command` object
|
|
439
|
-
|
|
440
|
-
#### `terminal.command.list()`
|
|
441
|
-
|
|
442
|
-
List all commands executed in this terminal.
|
|
443
|
-
|
|
444
|
-
```typescript
|
|
445
|
-
const commands = await exec.command.list();
|
|
446
|
-
commands.forEach(cmd => {
|
|
447
|
-
console.log(cmd.id, cmd.command, cmd.status, cmd.exitCode);
|
|
448
|
-
});
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
#### `terminal.command.retrieve(cmdId)`
|
|
452
|
-
|
|
453
|
-
Retrieve a specific command by ID.
|
|
454
|
-
|
|
455
|
-
```typescript
|
|
456
|
-
const cmd = await exec.command.retrieve('cmd-123');
|
|
457
|
-
console.log(cmd.stdout);
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
### Command Object
|
|
461
|
-
|
|
462
|
-
The `Command` object represents a command execution result.
|
|
463
|
-
|
|
464
|
-
#### Command Properties
|
|
465
|
-
|
|
466
|
-
```typescript
|
|
467
|
-
console.log(cmd.id); // Command ID
|
|
468
|
-
console.log(cmd.terminalId); // Parent terminal ID
|
|
469
|
-
console.log(cmd.command); // Executed command
|
|
470
|
-
console.log(cmd.status); // 'running' | 'completed' | 'failed'
|
|
471
|
-
console.log(cmd.stdout); // Standard output
|
|
472
|
-
console.log(cmd.stderr); // Standard error
|
|
473
|
-
console.log(cmd.exitCode); // Exit code (undefined if running)
|
|
474
|
-
console.log(cmd.durationMs); // Execution time in milliseconds
|
|
475
|
-
console.log(cmd.startedAt); // Start timestamp
|
|
476
|
-
console.log(cmd.finishedAt); // Finish timestamp (undefined if running)
|
|
477
|
-
```
|
|
478
|
-
|
|
479
|
-
#### `command.wait(timeout?)`
|
|
480
|
-
|
|
481
|
-
Wait for a background command to complete.
|
|
482
|
-
|
|
483
|
-
```typescript
|
|
484
|
-
const cmd = await exec.command.run('sleep 5', { background: true });
|
|
485
|
-
await cmd.wait(); // Waits up to default timeout
|
|
486
|
-
console.log(cmd.exitCode);
|
|
487
|
-
|
|
488
|
-
// With custom timeout (in seconds, 0 = no timeout)
|
|
489
|
-
await cmd.wait(10);
|
|
490
|
-
```
|
|
491
|
-
|
|
492
|
-
#### `command.refresh()`
|
|
493
|
-
|
|
494
|
-
Refresh the command status from the server.
|
|
495
|
-
|
|
496
|
-
```typescript
|
|
497
|
-
const cmd = await exec.command.run('npm build', { background: true });
|
|
498
|
-
// ... later ...
|
|
499
|
-
await cmd.refresh();
|
|
500
|
-
console.log(cmd.status, cmd.exitCode);
|
|
501
|
-
```
|
|
502
|
-
|
|
503
|
-
#### `terminal.destroy()`
|
|
504
|
-
|
|
505
|
-
Destroy the terminal and clean up resources.
|
|
506
|
-
|
|
507
|
-
```typescript
|
|
508
|
-
await exec.destroy();
|
|
509
|
-
```
|
|
510
|
-
|
|
511
276
|
## Examples
|
|
512
277
|
|
|
513
278
|
### Multi-Step Build Process
|
|
@@ -547,93 +312,13 @@ const installResult = await sandbox.runCommand('npm install', { cwd: '/app' });
|
|
|
547
312
|
console.log('Install:', installResult.stdout);
|
|
548
313
|
|
|
549
314
|
// Run the app
|
|
550
|
-
const runResult = await sandbox.
|
|
551
|
-
const { spawn } = require('child_process');
|
|
552
|
-
const proc = spawn('node', ['src/index.js'], { cwd: '/app' });
|
|
553
|
-
proc.stdout.on('data', (data) => console.log(data.toString()));
|
|
554
|
-
`);
|
|
315
|
+
const runResult = await sandbox.runCommand('node src/index.js', { cwd: '/app' });
|
|
555
316
|
|
|
556
|
-
console.log(runResult.
|
|
317
|
+
console.log(runResult.stdout);
|
|
557
318
|
|
|
558
319
|
await sandbox.destroy();
|
|
559
320
|
```
|
|
560
321
|
|
|
561
|
-
### Terminal Command Execution
|
|
562
|
-
|
|
563
|
-
```typescript
|
|
564
|
-
import { compute } from 'computesdk';
|
|
565
|
-
|
|
566
|
-
const sandbox = await compute.sandbox.create();
|
|
567
|
-
|
|
568
|
-
// Create exec mode terminal for command tracking
|
|
569
|
-
const terminal = await sandbox.terminal.create({ pty: false });
|
|
570
|
-
|
|
571
|
-
// Run build commands with tracking
|
|
572
|
-
const install = await terminal.command.run('npm install');
|
|
573
|
-
console.log('Install exit code:', install.exitCode);
|
|
574
|
-
|
|
575
|
-
const build = await terminal.command.run('npm run build');
|
|
576
|
-
console.log('Build output:', build.stdout);
|
|
577
|
-
|
|
578
|
-
// Run tests in background
|
|
579
|
-
const tests = await terminal.command.run('npm test', { background: true });
|
|
580
|
-
console.log('Tests started:', tests.status);
|
|
581
|
-
|
|
582
|
-
// Wait for tests to complete
|
|
583
|
-
await tests.wait(60); // 60 second timeout
|
|
584
|
-
console.log('Tests completed:', tests.exitCode === 0 ? 'PASSED' : 'FAILED');
|
|
585
|
-
console.log('Test output:', tests.stdout);
|
|
586
|
-
|
|
587
|
-
// List all commands
|
|
588
|
-
const commands = await terminal.command.list();
|
|
589
|
-
console.log(`Executed ${commands.length} commands`);
|
|
590
|
-
|
|
591
|
-
await terminal.destroy();
|
|
592
|
-
await sandbox.destroy();
|
|
593
|
-
```
|
|
594
|
-
|
|
595
|
-
### Interactive Terminal Session
|
|
596
|
-
|
|
597
|
-
```typescript
|
|
598
|
-
import { compute } from 'computesdk';
|
|
599
|
-
|
|
600
|
-
const sandbox = await compute.sandbox.create();
|
|
601
|
-
|
|
602
|
-
// Create PTY terminal for interactive shell
|
|
603
|
-
const pty = await sandbox.terminal.create({
|
|
604
|
-
pty: true,
|
|
605
|
-
shell: '/bin/bash'
|
|
606
|
-
});
|
|
607
|
-
|
|
608
|
-
// Collect all output
|
|
609
|
-
let output = '';
|
|
610
|
-
pty.on('output', (data) => {
|
|
611
|
-
output += data;
|
|
612
|
-
console.log(data);
|
|
613
|
-
});
|
|
614
|
-
|
|
615
|
-
pty.on('error', (error) => {
|
|
616
|
-
console.error('Terminal error:', error);
|
|
617
|
-
});
|
|
618
|
-
|
|
619
|
-
// Execute interactive commands
|
|
620
|
-
pty.write('echo "Starting project setup"\n');
|
|
621
|
-
pty.write('mkdir -p /workspace/myproject\n');
|
|
622
|
-
pty.write('cd /workspace/myproject\n');
|
|
623
|
-
pty.write('npm init -y\n');
|
|
624
|
-
pty.write('npm install express\n');
|
|
625
|
-
pty.write('echo "Setup complete"\n');
|
|
626
|
-
|
|
627
|
-
// Wait for operations to complete
|
|
628
|
-
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
629
|
-
|
|
630
|
-
// Clean up
|
|
631
|
-
await pty.destroy();
|
|
632
|
-
await sandbox.destroy();
|
|
633
|
-
|
|
634
|
-
console.log('Complete output:', output);
|
|
635
|
-
```
|
|
636
|
-
|
|
637
322
|
### Using Different Providers
|
|
638
323
|
|
|
639
324
|
```typescript
|
|
@@ -647,7 +332,7 @@ compute.setConfig({
|
|
|
647
332
|
});
|
|
648
333
|
|
|
649
334
|
const e2bSandbox = await compute.sandbox.create();
|
|
650
|
-
await e2bSandbox.
|
|
335
|
+
await e2bSandbox.runCommand('python -c "import pandas as pd; print(pd.__version__)"');
|
|
651
336
|
await e2bSandbox.destroy();
|
|
652
337
|
|
|
653
338
|
// Switch to Modal for GPU workloads
|
|
@@ -659,7 +344,7 @@ compute.setConfig({
|
|
|
659
344
|
});
|
|
660
345
|
|
|
661
346
|
const modalSandbox = await compute.sandbox.create();
|
|
662
|
-
await modalSandbox.
|
|
347
|
+
await modalSandbox.runCommand('python -c "import torch; print(torch.cuda.is_available())"');
|
|
663
348
|
await modalSandbox.destroy();
|
|
664
349
|
```
|
|
665
350
|
|
|
@@ -668,7 +353,7 @@ await modalSandbox.destroy();
|
|
|
668
353
|
```typescript
|
|
669
354
|
try {
|
|
670
355
|
const sandbox = await compute.sandbox.create();
|
|
671
|
-
const result = await sandbox.
|
|
356
|
+
const result = await sandbox.runCommand('invalid python code');
|
|
672
357
|
} catch (error) {
|
|
673
358
|
console.error('Execution failed:', error.message);
|
|
674
359
|
|