computesdk 1.1.0 → 1.2.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/dist/index.d.ts CHANGED
@@ -3,6 +3,10 @@
3
3
  *
4
4
  * Types related to sandbox execution, filesystem, terminal operations
5
5
  */
6
+ interface Provider$1 {
7
+ readonly name: string;
8
+ readonly sandbox: any;
9
+ }
6
10
  /**
7
11
  * Supported runtime environments
8
12
  */
@@ -57,6 +61,14 @@ interface CreateSandboxOptions {
57
61
  timeout?: number;
58
62
  /** Custom sandbox ID (if supported by provider) */
59
63
  sandboxId?: string;
64
+ /** Template ID for sandbox creation (provider-specific) */
65
+ templateId?: string;
66
+ /** Additional metadata for the sandbox */
67
+ metadata?: Record<string, any>;
68
+ /** Domain for sandbox connection (provider-specific) */
69
+ domain?: string;
70
+ /** Environment variables for the sandbox */
71
+ envs?: Record<string, string>;
60
72
  }
61
73
  /**
62
74
  * File system entry information
@@ -91,85 +103,27 @@ interface SandboxFileSystem {
91
103
  remove(path: string): Promise<void>;
92
104
  }
93
105
  /**
94
- * Terminal session information
95
- */
96
- interface TerminalSession {
97
- /** Terminal process ID */
98
- pid: number;
99
- /** Terminal command */
100
- command: string;
101
- /** Terminal status */
102
- status: 'running' | 'exited';
103
- /** Exit code (if exited) */
104
- exitCode?: number;
105
- /** Terminal columns */
106
- cols: number;
107
- /** Terminal rows */
108
- rows: number;
109
- /** Write data to this terminal session */
110
- write(data: Uint8Array | string): Promise<void>;
111
- /** Resize this terminal session */
112
- resize(cols: number, rows: number): Promise<void>;
113
- /** Kill this terminal session */
114
- kill(): Promise<void>;
115
- /** Data stream handler */
116
- onData?: (data: Uint8Array) => void;
117
- /** Exit handler */
118
- onExit?: (exitCode: number) => void;
119
- }
120
- /**
121
- * Terminal creation options
122
- */
123
- interface TerminalCreateOptions {
124
- /** Command to run (defaults to shell) */
125
- command?: string;
126
- /** Terminal columns */
127
- cols?: number;
128
- /** Terminal rows */
129
- rows?: number;
130
- /** Environment variables */
131
- env?: Record<string, string>;
132
- /** Data stream handler */
133
- onData?: (data: Uint8Array) => void;
134
- /** Exit handler */
135
- onExit?: (exitCode: number) => void;
136
- }
137
- /**
138
- * Terminal operations interface for managing terminal sessions in a sandbox
106
+ * Error thrown when a command exits with a non-zero status
139
107
  */
140
- interface SandboxTerminal {
141
- /** Create a new interactive terminal session */
142
- create(options?: TerminalCreateOptions): Promise<TerminalSession>;
143
- /** Get existing terminal session by ID */
144
- getById(terminalId: string): Promise<TerminalSession | null>;
145
- /** List active terminal sessions */
146
- list(): Promise<TerminalSession[]>;
147
- /** Destroy a terminal session */
148
- destroy(terminalId: string): Promise<void>;
108
+ declare class CommandExitError extends Error {
109
+ result: {
110
+ exitCode: number;
111
+ stdout: string;
112
+ stderr: string;
113
+ error: boolean;
114
+ };
115
+ name: string;
116
+ constructor(result: {
117
+ exitCode: number;
118
+ stdout: string;
119
+ stderr: string;
120
+ error: boolean;
121
+ });
149
122
  }
150
123
  /**
151
- * Factory method definitions for sandbox management operations
152
- * Used by createProvider() to generate ProviderSandboxManager implementations
124
+ * Type guard to check if an error is a CommandExitError
153
125
  */
154
- interface SandboxManagerMethods<TSandbox = any, TConfig = any> {
155
- /** Create a new sandbox */
156
- create: (config: TConfig, options?: CreateSandboxOptions) => Promise<{
157
- sandbox: TSandbox;
158
- sandboxId: string;
159
- }>;
160
- /** Get an existing sandbox by ID */
161
- getById: (config: TConfig, sandboxId: string) => Promise<{
162
- sandbox: TSandbox;
163
- sandboxId: string;
164
- } | null>;
165
- /** List all active sandboxes */
166
- list: (config: TConfig) => Promise<Array<{
167
- sandbox: TSandbox;
168
- sandboxId: string;
169
- }>>;
170
- /** Destroy a sandbox */
171
- destroy: (config: TConfig, sandboxId: string) => Promise<void>;
172
- }
126
+ declare function isCommandExitError(error: unknown): error is CommandExitError;
173
127
  /**
174
128
  * Base sandbox interface - what developers interact with
175
129
  */
@@ -178,20 +132,27 @@ interface Sandbox {
178
132
  readonly sandboxId: string;
179
133
  /** Provider that created this sandbox */
180
134
  readonly provider: string;
135
+ /** Access to the native provider sandbox instance */
136
+ readonly instance: unknown;
181
137
  /** Execute code in the sandbox */
182
138
  runCode(code: string, runtime?: Runtime): Promise<ExecutionResult>;
183
139
  /** Execute shell commands */
184
140
  runCommand(command: string, args?: string[]): Promise<ExecutionResult>;
185
141
  /** Get information about the sandbox */
186
142
  getInfo(): Promise<SandboxInfo>;
143
+ /** Get URL for accessing the sandbox on a specific port */
144
+ getUrl(options: {
145
+ port: number;
146
+ protocol?: string;
147
+ }): Promise<string>;
148
+ /** Get the provider instance that created this sandbox */
149
+ getProvider(): Provider$1;
187
150
  /** Kill the sandbox */
188
151
  kill(): Promise<void>;
189
152
  /** Destroy the sandbox and clean up resources */
190
153
  destroy(): Promise<void>;
191
154
  /** File system operations */
192
155
  readonly filesystem: SandboxFileSystem;
193
- /** Terminal operations */
194
- readonly terminal: SandboxTerminal;
195
156
  }
196
157
 
197
158
  /**
@@ -227,7 +188,9 @@ interface Provider {
227
188
  */
228
189
  interface ComputeConfig {
229
190
  /** Default provider to use when none is specified */
230
- provider: Provider;
191
+ defaultProvider?: Provider;
192
+ /** @deprecated Use defaultProvider instead. Kept for backwards compatibility */
193
+ provider?: Provider;
231
194
  }
232
195
  /**
233
196
  * Parameters for compute.sandbox.create()
@@ -257,7 +220,7 @@ interface ComputeAPI {
257
220
  clearConfig(): void;
258
221
  sandbox: {
259
222
  /** Create a sandbox from a provider (or default provider if configured) */
260
- create(params: CreateSandboxParams | CreateSandboxParamsWithOptionalProvider): Promise<Sandbox>;
223
+ create(params?: CreateSandboxParams | CreateSandboxParamsWithOptionalProvider): Promise<Sandbox>;
261
224
  /** Get an existing sandbox by ID from a provider (or default provider if configured) */
262
225
  getById(providerOrSandboxId: Provider | string, sandboxId?: string): Promise<Sandbox | null>;
263
226
  /** List all active sandboxes from a provider (or default provider if configured) */
@@ -279,50 +242,27 @@ interface ComputeAPI {
279
242
  declare const compute: ComputeAPI;
280
243
 
281
244
  /**
282
- * Request Handler for Web Framework Integration
245
+ * Simplified Request Handler for Web Framework Integration
283
246
  *
284
- * Maps web requests to ComputeSDK operations with comprehensive support for:
285
- * - Sandbox management (create, destroy, getInfo)
286
- * - Code execution
287
- * - Filesystem operations
288
- * - Terminal operations
247
+ * Handles JSON requests for sandbox and code execution operations.
248
+ * Terminal support removed - will be re-added with WebSocket VM connections.
289
249
  */
290
250
 
291
251
  /**
292
- * Extended request structure supporting all SDK capabilities
252
+ * Request structure supporting sandbox and code execution capabilities
293
253
  */
294
254
  interface ComputeRequest {
295
- /** Type of operation to perform */
296
- action: 'compute.sandbox.create' | 'compute.sandbox.destroy' | 'compute.sandbox.getInfo' | 'compute.sandbox.list' | 'compute.sandbox.runCode' | 'compute.sandbox.runCommand' | 'compute.sandbox.filesystem.readFile' | 'compute.sandbox.filesystem.writeFile' | 'compute.sandbox.filesystem.mkdir' | 'compute.sandbox.filesystem.readdir' | 'compute.sandbox.filesystem.exists' | 'compute.sandbox.filesystem.remove' | 'compute.sandbox.terminal.create' | 'compute.sandbox.terminal.getById' | 'compute.sandbox.terminal.list' | 'compute.sandbox.terminal.destroy' | 'compute.sandbox.terminal.write' | 'compute.sandbox.terminal.resize' | 'compute.sandbox.terminal.kill';
297
- /** Sandbox ID (required for operations on existing sandboxes) */
255
+ /** Action in dot notation (e.g., 'compute.sandbox.create') */
256
+ action: string;
257
+ /** Parameters for the action */
298
258
  sandboxId?: string;
299
- /** Code to execute (for runCode action) */
300
259
  code?: string;
301
- /** Command to run (for runCommand action) */
302
260
  command?: string;
303
- /** Command arguments (for runCommand action) */
304
261
  args?: string[];
305
- /** Runtime environment */
306
262
  runtime?: Runtime;
307
- /** File path (for filesystem operations) */
308
263
  path?: string;
309
- /** File content (for writeFile action) */
310
264
  content?: string;
311
- /** Terminal options (for terminal.create) */
312
- terminalOptions?: {
313
- command?: string;
314
- cols?: number;
315
- rows?: number;
316
- env?: Record<string, string>;
317
- };
318
- /** Terminal ID (for terminal operations) */
319
- terminalId?: string;
320
- /** Terminal input data (for terminal.write) */
321
- data?: string | Uint8Array;
322
- /** Terminal resize dimensions (for terminal.resize) */
323
- cols?: number;
324
- rows?: number;
325
- /** Additional sandbox creation options */
265
+ /** Sandbox creation options */
326
266
  options?: {
327
267
  runtime?: Runtime;
328
268
  timeout?: number;
@@ -333,140 +273,23 @@ interface ComputeRequest {
333
273
  * Response structure for compute operations
334
274
  */
335
275
  interface ComputeResponse {
336
- /** Whether the operation was successful */
337
276
  success: boolean;
338
- /** Error message if operation failed */
339
277
  error?: string;
340
- /** Sandbox ID involved in the operation */
341
278
  sandboxId: string;
342
- /** Provider that handled the operation */
343
279
  provider: string;
344
- /** Execution result (for runCode/runCommand actions) */
345
- result?: {
346
- stdout: string;
347
- stderr: string;
348
- exitCode: number;
349
- executionTime: number;
350
- };
351
- /** Sandbox info (for getInfo action) */
352
- info?: {
353
- id: string;
354
- provider: string;
355
- runtime: Runtime;
356
- status: string;
357
- createdAt: string;
358
- timeout: number;
359
- metadata?: Record<string, any>;
360
- };
361
- /** File content (for readFile action) */
362
- fileContent?: string;
363
- /** Directory listing (for readdir action) */
364
- files?: Array<{
365
- name: string;
366
- path: string;
367
- isDirectory: boolean;
368
- size: number;
369
- lastModified: string;
370
- }>;
371
- /** File/directory exists (for exists action) */
372
- exists?: boolean;
373
- /** Sandbox list (for list action) */
374
- sandboxes?: Array<{
375
- sandboxId: string;
376
- provider: string;
377
- }>;
378
- /** Terminal session (for terminal.create action) */
379
- terminal?: {
380
- pid: number;
381
- command: string;
382
- status: string;
383
- cols: number;
384
- rows: number;
385
- };
386
- /** Terminal sessions (for terminal.list action) */
387
- terminals?: Array<{
388
- pid: number;
389
- command: string;
390
- status: string;
391
- cols: number;
392
- rows: number;
393
- }>;
394
- /** WebSocket connection info (for terminal.connect action) */
395
- websocket?: {
396
- url: string;
397
- headers?: Record<string, string>;
398
- protocols?: string[];
399
- };
280
+ [key: string]: any;
400
281
  }
401
282
  /**
402
- * Parameters for handleComputeRequest
403
- */
404
- interface HandleComputeRequestParams {
405
- /** The compute request to handle */
406
- request: ComputeRequest;
407
- /** Provider to use for the operation */
408
- provider: Provider;
409
- }
410
- /**
411
- * Handle a compute request - unified API for web frameworks
412
- *
413
- * Maps web requests to ComputeSDK operations with full support for:
414
- * - All sandbox operations via compute.sandbox.*
415
- * - All filesystem operations via sandbox.filesystem.*
416
- * - All terminal operations via sandbox.terminal.*
417
- *
418
- * @example
419
- * ```typescript
420
- * // Execute code
421
- * const response = await handleComputeRequest({
422
- * request: {
423
- * action: 'sandbox.runCode',
424
- * code: 'print("hello")',
425
- * sandboxId: 'existing-sandbox-id' // optional, creates new if not provided
426
- * },
427
- * provider: e2b({ apiKey: process.env.E2B_API_KEY })
428
- * });
429
- *
430
- * // Filesystem operations
431
- * const response = await handleComputeRequest({
432
- * request: {
433
- * action: 'filesystem.writeFile',
434
- * sandboxId: 'sandbox-id',
435
- * path: '/tmp/hello.py',
436
- * content: 'print("Hello from file!")'
437
- * },
438
- * provider: e2b({ apiKey: process.env.E2B_API_KEY })
439
- * });
440
- * ```
283
+ * Main request handler - handles HTTP requests and pre-parsed bodies
441
284
  */
442
285
  declare function handleComputeRequest(params: HandleComputeRequestParams): Promise<ComputeResponse>;
443
-
286
+ declare function handleComputeRequest(requestOrBody: Request | ComputeRequest, provider: Provider): Promise<Response>;
444
287
  /**
445
- * Sandbox Management - Provider-Centric Approach
446
- *
447
- * No registry - providers are the single source of truth
448
- */
449
-
450
- /**
451
- * Sandbox manager - thin wrapper around provider APIs
288
+ * Legacy export for backward compatibility
452
289
  */
453
- declare class SandboxManager {
454
- /**
455
- * Create a sandbox from a provider
456
- */
457
- create(provider: Provider, options?: CreateSandboxOptions): Promise<Sandbox>;
458
- /**
459
- * Get an existing sandbox by ID from a provider
460
- */
461
- getById(provider: Provider, sandboxId: string): Promise<Sandbox | null>;
462
- /**
463
- * List all active sandboxes from a provider
464
- */
465
- list(provider: Provider): Promise<Sandbox[]>;
466
- /**
467
- * Destroy a sandbox via a provider
468
- */
469
- destroy(provider: Provider, sandboxId: string): Promise<void>;
290
+ interface HandleComputeRequestParams {
291
+ request: ComputeRequest;
292
+ provider: Provider;
470
293
  }
471
294
 
472
295
  /**
@@ -496,31 +319,17 @@ interface SandboxMethods<TSandbox = any, TConfig = any> {
496
319
  runCode: (sandbox: TSandbox, code: string, runtime?: Runtime, config?: TConfig) => Promise<ExecutionResult>;
497
320
  runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>;
498
321
  getInfo: (sandbox: TSandbox) => Promise<SandboxInfo>;
322
+ getUrl: (sandbox: TSandbox, options: {
323
+ port: number;
324
+ protocol?: string;
325
+ }) => Promise<string>;
499
326
  filesystem?: {
500
- readFile: (sandbox: TSandbox, path: string) => Promise<string>;
501
- writeFile: (sandbox: TSandbox, path: string, content: string) => Promise<void>;
502
- mkdir: (sandbox: TSandbox, path: string) => Promise<void>;
503
- readdir: (sandbox: TSandbox, path: string) => Promise<FileEntry[]>;
504
- exists: (sandbox: TSandbox, path: string) => Promise<boolean>;
505
- remove: (sandbox: TSandbox, path: string) => Promise<void>;
506
- };
507
- terminal?: {
508
- create: (sandbox: TSandbox, options?: TerminalCreateOptions) => Promise<{
509
- terminal: any;
510
- terminalId: string;
511
- }>;
512
- getById: (sandbox: TSandbox, terminalId: string) => Promise<{
513
- terminal: any;
514
- terminalId: string;
515
- } | null>;
516
- list: (sandbox: TSandbox) => Promise<Array<{
517
- terminal: any;
518
- terminalId: string;
519
- }>>;
520
- destroy: (sandbox: TSandbox, terminalId: string) => Promise<void>;
521
- write: (sandbox: TSandbox, terminal: any, data: Uint8Array | string) => Promise<void>;
522
- resize: (sandbox: TSandbox, terminal: any, cols: number, rows: number) => Promise<void>;
523
- kill: (sandbox: TSandbox, terminal: any) => Promise<void>;
327
+ readFile: (sandbox: TSandbox, path: string, runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>) => Promise<string>;
328
+ writeFile: (sandbox: TSandbox, path: string, content: string, runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>) => Promise<void>;
329
+ mkdir: (sandbox: TSandbox, path: string, runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>) => Promise<void>;
330
+ readdir: (sandbox: TSandbox, path: string, runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>) => Promise<FileEntry[]>;
331
+ exists: (sandbox: TSandbox, path: string, runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>) => Promise<boolean>;
332
+ remove: (sandbox: TSandbox, path: string, runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>) => Promise<void>;
524
333
  };
525
334
  }
526
335
  /**
@@ -540,4 +349,4 @@ interface ProviderConfig<TSandbox = any, TConfig = any> {
540
349
  */
541
350
  declare function createProvider<TSandbox, TConfig>(providerConfig: ProviderConfig<TSandbox, TConfig>): (config: TConfig) => Provider;
542
351
 
543
- export { type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxOptions, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type ExecutionResult, type FileEntry, type HandleComputeRequestParams, type Provider, type ProviderConfig, type ProviderSandboxManager, type Runtime, type Sandbox, type SandboxFileSystem, type SandboxInfo, SandboxManager, type SandboxManagerMethods, type SandboxMethods, type SandboxStatus, type SandboxTerminal, type TerminalCreateOptions, type TerminalSession, compute, createProvider, handleComputeRequest };
352
+ export { CommandExitError, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxOptions, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type ExecutionResult, type FileEntry, type HandleComputeRequestParams, type Provider, type ProviderConfig, type ProviderSandboxManager, type Runtime, type Sandbox, type SandboxFileSystem, type SandboxInfo, type SandboxMethods, type SandboxStatus, compute, createProvider, handleComputeRequest, isCommandExitError };