computesdk 1.0.0 → 1.0.1

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.
@@ -0,0 +1,467 @@
1
+ /**
2
+ * ComputeSDK Types
3
+ *
4
+ * This file contains all the type definitions for the ComputeSDK.
5
+ */
6
+ /**
7
+ * Supported runtime environments
8
+ */
9
+ type Runtime = 'node' | 'python';
10
+ /**
11
+ * Supported provider types
12
+ */
13
+ type ProviderType = 'e2b' | 'vercel' | 'cloudflare' | 'fly' | 'auto';
14
+ /**
15
+ * Sandbox status types
16
+ */
17
+ type SandboxStatus = 'running' | 'stopped' | 'error';
18
+ /**
19
+ * Result of code execution
20
+ */
21
+ interface ExecutionResult {
22
+ /** Standard output from the execution */
23
+ stdout: string;
24
+ /** Standard error from the execution */
25
+ stderr: string;
26
+ /** Exit code from the execution */
27
+ exitCode: number;
28
+ /** Time taken for execution in milliseconds */
29
+ executionTime: number;
30
+ /** ID of the sandbox where the code was executed */
31
+ sandboxId: string;
32
+ /** Provider that executed the code */
33
+ provider: string;
34
+ }
35
+ /**
36
+ * Information about a sandbox
37
+ */
38
+ interface SandboxInfo {
39
+ /** Unique identifier for the sandbox */
40
+ id: string;
41
+ /** Provider hosting the sandbox */
42
+ provider: string;
43
+ /** Runtime environment in the sandbox */
44
+ runtime: Runtime;
45
+ /** Current status of the sandbox */
46
+ status: SandboxStatus;
47
+ /** When the sandbox was created */
48
+ createdAt: Date;
49
+ /** Execution timeout in milliseconds */
50
+ timeout: number;
51
+ /** Additional provider-specific metadata */
52
+ metadata?: Record<string, any>;
53
+ }
54
+ /**
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
71
+ */
72
+ interface SandboxConfig {
73
+ /** Provider to use for execution */
74
+ provider?: ProviderType;
75
+ /** Runtime environment to use */
76
+ runtime?: Runtime;
77
+ /** Container configuration if using container-based provider */
78
+ container?: string | ContainerConfig;
79
+ /** Execution timeout in milliseconds */
80
+ timeout?: number;
81
+ }
82
+ /**
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>;
98
+ }
99
+ /**
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 */
110
+ kill(): Promise<void>;
111
+ /** Get information about the sandbox */
112
+ getInfo(): Promise<SandboxInfo>;
113
+ }
114
+ /**
115
+ * Parameters for the executeSandbox function
116
+ */
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;
124
+ }
125
+ /**
126
+ * Provider registry configuration
127
+ */
128
+ interface ProviderRegistry {
129
+ /** Get a sandbox by ID */
130
+ sandbox(id: string): ComputeSandbox;
131
+ }
132
+ /**
133
+ * Provider factory function type
134
+ */
135
+ type ProviderFactory = (config?: any) => ComputeSandbox;
136
+ /**
137
+ * Provider registry map type
138
+ */
139
+ type ProviderMap = Record<string, ProviderFactory>;
140
+
141
+ /**
142
+ * ComputeSDK Error Handling
143
+ *
144
+ * This file contains standardized error classes for the ComputeSDK.
145
+ */
146
+ /**
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);
166
+ }
167
+ /**
168
+ * Error thrown when code execution fails
169
+ */
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);
186
+ }
187
+ /**
188
+ * Error thrown when code execution times out
189
+ */
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);
206
+ }
207
+ /**
208
+ * Error thrown when provider-specific operations fail
209
+ */
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);
243
+ }
244
+ /**
245
+ * Error thrown when authentication fails
246
+ */
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);
260
+ }
261
+ /**
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);
277
+ }
278
+
279
+ /**
280
+ * ComputeSDK Configuration Management
281
+ *
282
+ * This file manages configuration and provider selection logic.
283
+ */
284
+
285
+ declare global {
286
+ var DurableObject: any;
287
+ var WebSocketPair: any;
288
+ }
289
+ declare const DEFAULT_TIMEOUT = 300000;
290
+ /**
291
+ * Environment variable names for provider API keys
292
+ */
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;
339
+ /**
340
+ * Normalize and validate sandbox configuration
341
+ *
342
+ * @param config User-provided configuration
343
+ * @returns Normalized configuration with defaults applied
344
+ */
345
+ declare function normalizeSandboxConfig(config?: Partial<SandboxConfig>): SandboxConfig;
346
+
347
+ /**
348
+ * Utility functions for ComputeSDK
349
+ */
350
+
351
+ /**
352
+ * Execute code in a sandbox
353
+ */
354
+ declare function executeSandbox(params: ExecuteSandboxParams): Promise<ExecutionResult>;
355
+ /**
356
+ * Retry function with exponential backoff
357
+ */
358
+ declare function retry<T>(fn: () => Promise<T>, maxRetries?: number, baseDelay?: number): Promise<T>;
359
+
360
+ /**
361
+ * ComputeSDK Provider Registry
362
+ *
363
+ * This file implements the provider registry for managing multiple providers.
364
+ */
365
+
366
+ /**
367
+ * Create a provider registry for managing multiple providers
368
+ *
369
+ * @param providers Map of provider factories
370
+ * @returns Provider registry instance
371
+ */
372
+ declare function createComputeRegistry(providers: ProviderMap): ProviderRegistry;
373
+
374
+ /**
375
+ * ComputeSDK Base Provider
376
+ *
377
+ * This file contains the base provider implementation that all
378
+ * specific provider implementations extend.
379
+ */
380
+
381
+ /**
382
+ * Base implementation of the ComputeSandbox interface
383
+ *
384
+ * Provides common functionality and wraps provider-specific implementations.
385
+ */
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>;
416
+ /**
417
+ * Get information about the sandbox
418
+ *
419
+ * @returns Sandbox information
420
+ */
421
+ getInfo(): Promise<SandboxInfo>;
422
+ /**
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
428
+ */
429
+ abstract doExecute(code: string, runtime?: Runtime): Promise<ExecutionResult>;
430
+ /**
431
+ * Provider-specific implementation of sandbox termination
432
+ *
433
+ * @returns Promise that resolves when the sandbox is killed
434
+ */
435
+ abstract doKill(): Promise<void>;
436
+ /**
437
+ * Provider-specific implementation of retrieving sandbox information
438
+ *
439
+ * @returns Sandbox information
440
+ */
441
+ abstract doGetInfo(): Promise<SandboxInfo>;
442
+ }
443
+
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
+ }
459
+
460
+ /**
461
+ * ComputeSDK Core
462
+ *
463
+ * A unified abstraction layer for executing code in secure,
464
+ * isolated sandboxed environments across multiple cloud providers.
465
+ */
466
+
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 };