computesdk 1.0.2 → 1.1.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.mts CHANGED
@@ -1,16 +1,12 @@
1
1
  /**
2
- * ComputeSDK Types
2
+ * Sandbox Types
3
3
  *
4
- * This file contains all the type definitions for the ComputeSDK.
4
+ * Types related to sandbox execution, filesystem, terminal operations
5
5
  */
6
6
  /**
7
7
  * Supported runtime environments
8
8
  */
9
9
  type Runtime = 'node' | 'python';
10
- /**
11
- * Supported provider types
12
- */
13
- type ProviderType = 'e2b' | 'vercel' | 'cloudflare' | 'fly' | 'auto';
14
10
  /**
15
11
  * Sandbox status types
16
12
  */
@@ -52,416 +48,496 @@ interface SandboxInfo {
52
48
  metadata?: Record<string, any>;
53
49
  }
54
50
  /**
55
- * Configuration for container-based providers
56
- */
57
- interface ContainerConfig {
58
- /** Docker image to use */
59
- image: string;
60
- /** Command to run in the container */
61
- command?: string[];
62
- /** Environment variables for the container */
63
- env?: Record<string, string>;
64
- /** Ports to expose from the container */
65
- ports?: number[];
66
- /** Working directory in the container */
67
- workdir?: string;
68
- }
69
- /**
70
- * Configuration for creating a compute sandbox
51
+ * Options for creating a sandbox
71
52
  */
72
- interface SandboxConfig {
73
- /** Provider to use for execution */
74
- provider?: ProviderType;
75
- /** Runtime environment to use */
53
+ interface CreateSandboxOptions {
54
+ /** Runtime environment */
76
55
  runtime?: Runtime;
77
- /** Container configuration if using container-based provider */
78
- container?: string | ContainerConfig;
79
56
  /** Execution timeout in milliseconds */
80
57
  timeout?: number;
58
+ /** Custom sandbox ID (if supported by provider) */
59
+ sandboxId?: string;
81
60
  }
82
61
  /**
83
- * Provider specification that all providers must implement
84
- */
85
- interface ComputeSpecification {
86
- /** Version of the specification */
87
- specificationVersion: 'v1';
88
- /** Provider identifier */
89
- provider: string;
90
- /** Sandbox identifier */
91
- sandboxId: string;
92
- /** Execute code in the sandbox */
93
- doExecute(code: string, runtime?: Runtime): Promise<ExecutionResult>;
94
- /** Kill the sandbox */
95
- doKill(): Promise<void>;
96
- /** Get information about the sandbox */
97
- doGetInfo(): Promise<SandboxInfo>;
62
+ * File system entry information
63
+ */
64
+ interface FileEntry {
65
+ /** File/directory name */
66
+ name: string;
67
+ /** Full path to the entry */
68
+ path: string;
69
+ /** Whether this is a directory */
70
+ isDirectory: boolean;
71
+ /** File size in bytes (0 for directories) */
72
+ size: number;
73
+ /** Last modified timestamp */
74
+ lastModified: Date;
98
75
  }
99
76
  /**
100
- * The core compute sandbox interface that all providers expose
101
- */
102
- interface ComputeSandbox {
103
- /** Provider identifier */
104
- provider: string;
105
- /** Sandbox identifier */
106
- sandboxId: string;
107
- /** Execute code in the sandbox */
108
- execute(code: string, runtime?: Runtime): Promise<ExecutionResult>;
109
- /** Kill the sandbox */
77
+ * File system interface for sandbox operations
78
+ */
79
+ interface SandboxFileSystem {
80
+ /** Read file contents */
81
+ readFile(path: string): Promise<string>;
82
+ /** Write file contents */
83
+ writeFile(path: string, content: string): Promise<void>;
84
+ /** Create directory */
85
+ mkdir(path: string): Promise<void>;
86
+ /** List directory contents */
87
+ readdir(path: string): Promise<FileEntry[]>;
88
+ /** Check if file/directory exists */
89
+ exists(path: string): Promise<boolean>;
90
+ /** Remove file or directory */
91
+ remove(path: string): Promise<void>;
92
+ }
93
+ /**
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 */
110
114
  kill(): Promise<void>;
111
- /** Get information about the sandbox */
112
- getInfo(): Promise<SandboxInfo>;
115
+ /** Data stream handler */
116
+ onData?: (data: Uint8Array) => void;
117
+ /** Exit handler */
118
+ onExit?: (exitCode: number) => void;
113
119
  }
114
120
  /**
115
- * Parameters for the executeSandbox function
121
+ * Terminal creation options
116
122
  */
117
- interface ExecuteSandboxParams {
118
- /** Sandbox to execute in */
119
- sandbox: ComputeSandbox;
120
- /** Code to execute */
121
- code: string;
122
- /** Runtime to use */
123
- runtime?: Runtime;
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;
124
136
  }
125
137
  /**
126
- * Provider registry configuration
127
- */
128
- interface ProviderRegistry {
129
- /** Get a sandbox by ID */
130
- sandbox(id: string): ComputeSandbox;
138
+ * Terminal operations interface for managing terminal sessions in a sandbox
139
+ */
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>;
131
149
  }
132
150
  /**
133
- * Provider factory function type
134
- */
135
- type ProviderFactory = (config?: any) => ComputeSandbox;
151
+ * Factory method definitions for sandbox management operations
152
+ * Used by createProvider() to generate ProviderSandboxManager implementations
153
+ */
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
+ }
136
173
  /**
137
- * Provider registry map type
174
+ * Base sandbox interface - what developers interact with
138
175
  */
139
- type ProviderMap = Record<string, ProviderFactory>;
176
+ interface Sandbox {
177
+ /** Unique identifier for the sandbox */
178
+ readonly sandboxId: string;
179
+ /** Provider that created this sandbox */
180
+ readonly provider: string;
181
+ /** Execute code in the sandbox */
182
+ runCode(code: string, runtime?: Runtime): Promise<ExecutionResult>;
183
+ /** Execute shell commands */
184
+ runCommand(command: string, args?: string[]): Promise<ExecutionResult>;
185
+ /** Get information about the sandbox */
186
+ getInfo(): Promise<SandboxInfo>;
187
+ /** Kill the sandbox */
188
+ kill(): Promise<void>;
189
+ /** Destroy the sandbox and clean up resources */
190
+ destroy(): Promise<void>;
191
+ /** File system operations */
192
+ readonly filesystem: SandboxFileSystem;
193
+ /** Terminal operations */
194
+ readonly terminal: SandboxTerminal;
195
+ }
140
196
 
141
197
  /**
142
- * ComputeSDK Error Handling
198
+ * Provider Types
143
199
  *
144
- * This file contains standardized error classes for the ComputeSDK.
200
+ * Types related to provider configuration, authentication, and resource management
145
201
  */
202
+
146
203
  /**
147
- * Base error class for all ComputeSDK errors
148
- */
149
- declare abstract class ComputeError extends Error {
150
- /** Error code identifier */
151
- abstract readonly code: string;
152
- /** Whether the operation can be retried */
153
- abstract readonly isRetryable: boolean;
154
- /** Provider where the error occurred */
155
- readonly provider: string;
156
- /** Sandbox ID where the error occurred */
157
- readonly sandboxId?: string;
158
- /**
159
- * Create a new ComputeError
160
- *
161
- * @param message Error message
162
- * @param provider Provider identifier
163
- * @param sandboxId Optional sandbox identifier
164
- */
165
- constructor(message: string, provider: string, sandboxId?: string);
204
+ * Provider sandbox management interface
205
+ */
206
+ interface ProviderSandboxManager {
207
+ /** Create a new sandbox */
208
+ create(options?: CreateSandboxOptions): Promise<Sandbox>;
209
+ /** Get an existing sandbox by ID */
210
+ getById(sandboxId: string): Promise<Sandbox | null>;
211
+ /** List all active sandboxes */
212
+ list(): Promise<Sandbox[]>;
213
+ /** Destroy a sandbox */
214
+ destroy(sandboxId: string): Promise<void>;
166
215
  }
167
216
  /**
168
- * Error thrown when code execution fails
217
+ * Provider interface - creates and manages resources
169
218
  */
170
- declare class ExecutionError extends ComputeError {
171
- /** Error code */
172
- readonly code = "EXECUTION_ERROR";
173
- /** Execution errors are generally not retryable */
174
- readonly isRetryable = false;
175
- /** Exit code from the failed execution */
176
- readonly exitCode: number;
177
- /**
178
- * Create a new ExecutionError
179
- *
180
- * @param message Error message
181
- * @param provider Provider identifier
182
- * @param exitCode Exit code from the execution
183
- * @param sandboxId Optional sandbox identifier
184
- */
185
- constructor(message: string, provider: string, exitCode: number, sandboxId?: string);
219
+ interface Provider {
220
+ /** Provider name/type */
221
+ readonly name: string;
222
+ /** Sandbox management operations */
223
+ readonly sandbox: ProviderSandboxManager;
186
224
  }
187
225
  /**
188
- * Error thrown when code execution times out
226
+ * Configuration for the compute singleton
189
227
  */
190
- declare class TimeoutError extends ComputeError {
191
- /** Error code */
192
- readonly code = "TIMEOUT_ERROR";
193
- /** Timeout errors may be retryable with a longer timeout */
194
- readonly isRetryable = true;
195
- /** Timeout duration in milliseconds */
196
- readonly timeoutMs: number;
197
- /**
198
- * Create a new TimeoutError
199
- *
200
- * @param message Error message
201
- * @param provider Provider identifier
202
- * @param timeoutMs Timeout duration in milliseconds
203
- * @param sandboxId Optional sandbox identifier
204
- */
205
- constructor(message: string, provider: string, timeoutMs: number, sandboxId?: string);
228
+ interface ComputeConfig {
229
+ /** Default provider to use when none is specified */
230
+ provider: Provider;
206
231
  }
207
232
  /**
208
- * Error thrown when provider-specific operations fail
233
+ * Parameters for compute.sandbox.create()
209
234
  */
210
- declare class ProviderError extends ComputeError {
211
- /** Error code */
212
- readonly code = "PROVIDER_ERROR";
213
- /** Provider errors may be retryable */
214
- readonly isRetryable = true;
215
- /** Original error from the provider */
216
- readonly originalError?: Error;
217
- /**
218
- * Create a new ProviderError
219
- *
220
- * @param message Error message
221
- * @param provider Provider identifier
222
- * @param originalError Optional original error from the provider
223
- * @param sandboxId Optional sandbox identifier
224
- */
225
- constructor(message: string, provider: string, originalError?: Error, sandboxId?: string);
226
- }
227
- /**
228
- * Error thrown when configuration is invalid
229
- */
230
- declare class ConfigurationError extends ComputeError {
231
- /** Error code */
232
- readonly code = "CONFIGURATION_ERROR";
233
- /** Configuration errors are not retryable without changes */
234
- readonly isRetryable = false;
235
- /**
236
- * Create a new ConfigurationError
237
- *
238
- * @param message Error message
239
- * @param provider Provider identifier
240
- * @param sandboxId Optional sandbox identifier
241
- */
242
- constructor(message: string, provider: string, sandboxId?: string);
235
+ interface CreateSandboxParams {
236
+ /** Provider instance to use */
237
+ provider: Provider;
238
+ /** Optional sandbox creation options */
239
+ options?: CreateSandboxOptions;
243
240
  }
244
241
  /**
245
- * Error thrown when authentication fails
242
+ * Parameters for compute.sandbox.create() with optional provider
246
243
  */
247
- declare class AuthenticationError extends ComputeError {
248
- /** Error code */
249
- readonly code = "AUTHENTICATION_ERROR";
250
- /** Authentication errors are not retryable without new credentials */
251
- readonly isRetryable = false;
252
- /**
253
- * Create a new AuthenticationError
254
- *
255
- * @param message Error message
256
- * @param provider Provider identifier
257
- * @param sandboxId Optional sandbox identifier
258
- */
259
- constructor(message: string, provider: string, sandboxId?: string);
244
+ interface CreateSandboxParamsWithOptionalProvider {
245
+ /** Provider instance to use (optional if default is set) */
246
+ provider?: Provider;
247
+ /** Optional sandbox creation options */
248
+ options?: CreateSandboxOptions;
260
249
  }
261
250
  /**
262
- * Error thrown when the provider is not available
263
- */
264
- declare class ProviderUnavailableError extends ComputeError {
265
- /** Error code */
266
- readonly code = "PROVIDER_UNAVAILABLE";
267
- /** Provider unavailability may be temporary */
268
- readonly isRetryable = true;
269
- /**
270
- * Create a new ProviderUnavailableError
271
- *
272
- * @param message Error message
273
- * @param provider Provider identifier
274
- * @param sandboxId Optional sandbox identifier
275
- */
276
- constructor(message: string, provider: string, sandboxId?: string);
251
+ * Compute singleton interface
252
+ */
253
+ interface ComputeAPI {
254
+ /** Configuration management */
255
+ setConfig(config: ComputeConfig): void;
256
+ getConfig(): ComputeConfig | null;
257
+ clearConfig(): void;
258
+ sandbox: {
259
+ /** Create a sandbox from a provider (or default provider if configured) */
260
+ create(params: CreateSandboxParams | CreateSandboxParamsWithOptionalProvider): Promise<Sandbox>;
261
+ /** Get an existing sandbox by ID from a provider (or default provider if configured) */
262
+ getById(providerOrSandboxId: Provider | string, sandboxId?: string): Promise<Sandbox | null>;
263
+ /** List all active sandboxes from a provider (or default provider if configured) */
264
+ list(provider?: Provider): Promise<Sandbox[]>;
265
+ /** Destroy a sandbox via a provider (or default provider if configured) */
266
+ destroy(providerOrSandboxId: Provider | string, sandboxId?: string): Promise<void>;
267
+ };
277
268
  }
278
269
 
279
270
  /**
280
- * ComputeSDK Configuration Management
271
+ * Compute Singleton - Main API Orchestrator
281
272
  *
282
- * This file manages configuration and provider selection logic.
273
+ * Provides the unified compute.* API and delegates to specialized managers
283
274
  */
284
275
 
285
- declare global {
286
- var DurableObject: any;
287
- var WebSocketPair: any;
288
- }
289
- declare const DEFAULT_TIMEOUT = 300000;
290
276
  /**
291
- * Environment variable names for provider API keys
277
+ * Singleton instance - the main API
292
278
  */
293
- declare const ENV_KEYS: {
294
- E2B: string;
295
- VERCEL: string;
296
- CLOUDFLARE: string;
297
- FLY: string;
298
- };
299
- /**
300
- * Detect if running in Cloudflare Workers environment
301
- *
302
- * @returns True if running in Cloudflare Workers
303
- */
304
- declare function isCloudflareWorkers(): boolean;
305
- /**
306
- * Detect available providers based on environment variables
307
- *
308
- * @returns Array of available provider types
309
- */
310
- declare function detectAvailableProviders(): ProviderType[];
311
- /**
312
- * Auto-select the best provider based on available API keys
313
- *
314
- * @returns Selected provider type or undefined if none available
315
- */
316
- declare function autoSelectProvider(): ProviderType | undefined;
317
- /**
318
- * Validate and normalize container configuration
319
- *
320
- * @param container Container configuration or image string
321
- * @returns Normalized container configuration
322
- */
323
- declare function normalizeContainerConfig(container: string | ContainerConfig | undefined): ContainerConfig | undefined;
324
- /**
325
- * Get the appropriate runtime based on provider and configuration
326
- *
327
- * @param provider Provider type
328
- * @param runtime Optional runtime preference
329
- * @returns Selected runtime
330
- */
331
- declare function getDefaultRuntime(provider: ProviderType, runtime?: Runtime): Runtime;
332
- /**
333
- * Validate API key for selected provider
334
- *
335
- * @param provider Provider type
336
- * @throws AuthenticationError if API key is missing
337
- */
338
- declare function validateProviderApiKey(provider: ProviderType): void;
279
+ declare const compute: ComputeAPI;
280
+
339
281
  /**
340
- * Normalize and validate sandbox configuration
282
+ * Request Handler for Web Framework Integration
341
283
  *
342
- * @param config User-provided configuration
343
- * @returns Normalized configuration with defaults applied
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
344
289
  */
345
- declare function normalizeSandboxConfig(config?: Partial<SandboxConfig>): SandboxConfig;
346
290
 
347
291
  /**
348
- * Utility functions for ComputeSDK
349
- */
350
-
292
+ * Extended request structure supporting all SDK capabilities
293
+ */
294
+ 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) */
298
+ sandboxId?: string;
299
+ /** Code to execute (for runCode action) */
300
+ code?: string;
301
+ /** Command to run (for runCommand action) */
302
+ command?: string;
303
+ /** Command arguments (for runCommand action) */
304
+ args?: string[];
305
+ /** Runtime environment */
306
+ runtime?: Runtime;
307
+ /** File path (for filesystem operations) */
308
+ path?: string;
309
+ /** File content (for writeFile action) */
310
+ 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 */
326
+ options?: {
327
+ runtime?: Runtime;
328
+ timeout?: number;
329
+ sandboxId?: string;
330
+ };
331
+ }
351
332
  /**
352
- * Execute code in a sandbox
333
+ * Response structure for compute operations
353
334
  */
354
- declare function executeSandbox(params: ExecuteSandboxParams): Promise<ExecutionResult>;
335
+ interface ComputeResponse {
336
+ /** Whether the operation was successful */
337
+ success: boolean;
338
+ /** Error message if operation failed */
339
+ error?: string;
340
+ /** Sandbox ID involved in the operation */
341
+ sandboxId: string;
342
+ /** Provider that handled the operation */
343
+ 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
+ };
400
+ }
355
401
  /**
356
- * Retry function with exponential backoff
402
+ * Parameters for handleComputeRequest
357
403
  */
358
- declare function retry<T>(fn: () => Promise<T>, maxRetries?: number, baseDelay?: number): Promise<T>;
359
-
404
+ interface HandleComputeRequestParams {
405
+ /** The compute request to handle */
406
+ request: ComputeRequest;
407
+ /** Provider to use for the operation */
408
+ provider: Provider;
409
+ }
360
410
  /**
361
- * ComputeSDK Provider Registry
411
+ * Handle a compute request - unified API for web frameworks
362
412
  *
363
- * This file implements the provider registry for managing multiple providers.
364
- */
365
-
366
- /**
367
- * Create a provider registry for managing multiple providers
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.*
368
417
  *
369
- * @param providers Map of provider factories
370
- * @returns Provider registry instance
371
- */
372
- declare function createComputeRegistry(providers: ProviderMap): ProviderRegistry;
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
+ * ```
441
+ */
442
+ declare function handleComputeRequest(params: HandleComputeRequestParams): Promise<ComputeResponse>;
373
443
 
374
444
  /**
375
- * ComputeSDK Base Provider
445
+ * Sandbox Management - Provider-Centric Approach
376
446
  *
377
- * This file contains the base provider implementation that all
378
- * specific provider implementations extend.
447
+ * No registry - providers are the single source of truth
379
448
  */
380
449
 
381
450
  /**
382
- * Base implementation of the ComputeSandbox interface
383
- *
384
- * Provides common functionality and wraps provider-specific implementations.
451
+ * Sandbox manager - thin wrapper around provider APIs
385
452
  */
386
- declare abstract class BaseProvider implements ComputeSandbox, ComputeSpecification {
387
- /** Specification version */
388
- readonly specificationVersion = "v1";
389
- /** Provider identifier */
390
- readonly provider: string;
391
- /** Sandbox identifier */
392
- readonly sandboxId: string;
393
- /** Execution timeout in milliseconds */
394
- protected readonly timeout: number;
395
- /**
396
- * Create a new base provider
397
- *
398
- * @param provider Provider identifier
399
- * @param timeout Execution timeout in milliseconds
400
- */
401
- constructor(provider: string, timeout: number);
402
- /**
403
- * Execute code in the sandbox
404
- *
405
- * @param code Code to execute
406
- * @param runtime Optional runtime to use
407
- * @returns Execution result
408
- */
409
- execute(code: string, runtime?: Runtime): Promise<ExecutionResult>;
410
- /**
411
- * Kill the sandbox
412
- *
413
- * @returns Promise that resolves when the sandbox is killed
414
- */
415
- kill(): Promise<void>;
453
+ declare class SandboxManager {
416
454
  /**
417
- * Get information about the sandbox
418
- *
419
- * @returns Sandbox information
455
+ * Create a sandbox from a provider
420
456
  */
421
- getInfo(): Promise<SandboxInfo>;
457
+ create(provider: Provider, options?: CreateSandboxOptions): Promise<Sandbox>;
422
458
  /**
423
- * Provider-specific implementation of code execution
424
- *
425
- * @param code Code to execute
426
- * @param runtime Optional runtime to use
427
- * @returns Execution result
459
+ * Get an existing sandbox by ID from a provider
428
460
  */
429
- abstract doExecute(code: string, runtime?: Runtime): Promise<ExecutionResult>;
461
+ getById(provider: Provider, sandboxId: string): Promise<Sandbox | null>;
430
462
  /**
431
- * Provider-specific implementation of sandbox termination
432
- *
433
- * @returns Promise that resolves when the sandbox is killed
463
+ * List all active sandboxes from a provider
434
464
  */
435
- abstract doKill(): Promise<void>;
465
+ list(provider: Provider): Promise<Sandbox[]>;
436
466
  /**
437
- * Provider-specific implementation of retrieving sandbox information
438
- *
439
- * @returns Sandbox information
467
+ * Destroy a sandbox via a provider
440
468
  */
441
- abstract doGetInfo(): Promise<SandboxInfo>;
469
+ destroy(provider: Provider, sandboxId: string): Promise<void>;
442
470
  }
443
471
 
444
- declare class ComputeSDK {
445
- /**
446
- * Create a new sandbox with the specified configuration
447
- *
448
- * @param config Optional sandbox configuration
449
- * @returns Configured sandbox instance
450
- */
451
- static createSandbox(config?: Partial<SandboxConfig>): ComputeSandbox;
452
- /**
453
- * Detect available providers based on environment variables
454
- *
455
- * @returns Array of available provider types
456
- */
457
- static detectProviders(): ProviderType[];
458
- }
472
+ /**
473
+ * Provider Factory - Creates providers from method definitions
474
+ *
475
+ * Eliminates boilerplate by auto-generating Provider/Sandbox classes
476
+ * from simple method definitions with automatic feature detection.
477
+ */
459
478
 
460
479
  /**
461
- * ComputeSDK Core
480
+ * Flat sandbox method implementations - all operations in one place
481
+ */
482
+ interface SandboxMethods<TSandbox = any, TConfig = any> {
483
+ create: (config: TConfig, options?: CreateSandboxOptions) => Promise<{
484
+ sandbox: TSandbox;
485
+ sandboxId: string;
486
+ }>;
487
+ getById: (config: TConfig, sandboxId: string) => Promise<{
488
+ sandbox: TSandbox;
489
+ sandboxId: string;
490
+ } | null>;
491
+ list: (config: TConfig) => Promise<Array<{
492
+ sandbox: TSandbox;
493
+ sandboxId: string;
494
+ }>>;
495
+ destroy: (config: TConfig, sandboxId: string) => Promise<void>;
496
+ runCode: (sandbox: TSandbox, code: string, runtime?: Runtime, config?: TConfig) => Promise<ExecutionResult>;
497
+ runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>;
498
+ getInfo: (sandbox: TSandbox) => Promise<SandboxInfo>;
499
+ 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>;
524
+ };
525
+ }
526
+ /**
527
+ * Provider configuration for createProvider()
528
+ */
529
+ interface ProviderConfig<TSandbox = any, TConfig = any> {
530
+ name: string;
531
+ methods: {
532
+ sandbox: SandboxMethods<TSandbox, TConfig>;
533
+ };
534
+ }
535
+ /**
536
+ * Create a provider from method definitions
462
537
  *
463
- * A unified abstraction layer for executing code in secure,
464
- * isolated sandboxed environments across multiple cloud providers.
538
+ * Auto-generates all boilerplate classes and provides feature detection
539
+ * based on which methods are implemented.
465
540
  */
541
+ declare function createProvider<TSandbox, TConfig>(providerConfig: ProviderConfig<TSandbox, TConfig>): (config: TConfig) => Provider;
466
542
 
467
- export { AuthenticationError, BaseProvider, ComputeError, ComputeSDK, type ComputeSandbox, type ComputeSpecification, ConfigurationError, type ContainerConfig, DEFAULT_TIMEOUT, ENV_KEYS, type ExecuteSandboxParams, ExecutionError, type ExecutionResult, ProviderError, type ProviderFactory, type ProviderMap, type ProviderRegistry, type ProviderType, ProviderUnavailableError, type Runtime, type SandboxConfig, type SandboxInfo, type SandboxStatus, TimeoutError, autoSelectProvider, createComputeRegistry, ComputeSDK as default, detectAvailableProviders, executeSandbox, getDefaultRuntime, isCloudflareWorkers, normalizeContainerConfig, normalizeSandboxConfig, retry, validateProviderApiKey };
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 };