computesdk 1.7.0 → 1.7.2
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 +46 -6
- package/dist/index.d.mts +91 -5
- package/dist/index.d.ts +91 -5
- package/dist/index.js +127 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +127 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
|
|
20
20
|
## What is ComputeSDK?
|
|
21
21
|
|
|
22
|
-
ComputeSDK is a free and open-source toolkit for running other people's code in your applications. Think of it as the "AI SDK for compute" - providing a consistent TypeScript interface whether you're using E2B, Vercel, or Daytona.
|
|
22
|
+
ComputeSDK is a free and open-source toolkit for running other people's code in your applications. Think of it as the "AI SDK for compute" - providing a consistent TypeScript interface whether you're using Blaxel, E2B, Vercel, or Daytona.
|
|
23
23
|
|
|
24
24
|
**Why ComputeSDK?**
|
|
25
|
-
- 🔄 **Provider-agnostic** - Switch between E2B, Vercel, Daytona and more (coming soon) without code changes
|
|
25
|
+
- 🔄 **Provider-agnostic** - Switch between Blaxel, E2B, Vercel, Daytona and more (coming soon) without code changes
|
|
26
26
|
- 🛡️ **Security-first** - Isolated sandboxes protect your infrastructure
|
|
27
27
|
- ⚡ **Developer experience** - Simple, TypeScript-native API
|
|
28
28
|
- 🌍 **Production-ready** - Used by teams building the next generation of developer tools
|
|
@@ -36,7 +36,7 @@ ComputeSDK is a free and open-source toolkit for running other people's code in
|
|
|
36
36
|
|
|
37
37
|
## Features
|
|
38
38
|
|
|
39
|
-
- 🚀 **Multi-provider support** - E2B, Vercel, Daytona
|
|
39
|
+
- 🚀 **Multi-provider support** - Blaxel, E2B, Vercel, Daytona
|
|
40
40
|
- 📁 **Filesystem operations** - Read, write, create directories across providers
|
|
41
41
|
- 🖥️ **Terminal support** - Interactive PTY terminals (E2B)
|
|
42
42
|
- ⚡ **Command execution** - Run shell commands directly
|
|
@@ -53,6 +53,7 @@ ComputeSDK is a free and open-source toolkit for running other people's code in
|
|
|
53
53
|
npm install computesdk
|
|
54
54
|
|
|
55
55
|
# Add your preferred provider
|
|
56
|
+
npm install @computesdk/blaxel # For AI-powered code execution
|
|
56
57
|
npm install @computesdk/e2b # For data science and Python
|
|
57
58
|
npm install @computesdk/vercel # For web-scale Node.js/Python
|
|
58
59
|
npm install @computesdk/daytona # For development workspaces
|
|
@@ -64,7 +65,9 @@ npm install @computesdk/ui # React hooks and utilities
|
|
|
64
65
|
Set your environment variables and you're ready to go:
|
|
65
66
|
|
|
66
67
|
```bash
|
|
67
|
-
export
|
|
68
|
+
export BLAXEL_API_KEY=your_api_key
|
|
69
|
+
export BLAXEL_WORKSPACE=your_workspace
|
|
70
|
+
# or E2B_API_KEY=your_api_key
|
|
68
71
|
# or VERCEL_TOKEN=your_token
|
|
69
72
|
# or DAYTONA_API_KEY=your_key
|
|
70
73
|
```
|
|
@@ -73,11 +76,14 @@ export E2B_API_KEY=your_api_key
|
|
|
73
76
|
|
|
74
77
|
```typescript
|
|
75
78
|
import { compute } from 'computesdk';
|
|
76
|
-
import {
|
|
79
|
+
import { blaxel } from '@computesdk/blaxel';
|
|
77
80
|
|
|
78
81
|
// Set default provider
|
|
79
82
|
compute.setConfig({
|
|
80
|
-
provider:
|
|
83
|
+
provider: blaxel({
|
|
84
|
+
apiKey: process.env.BLAXEL_API_KEY,
|
|
85
|
+
workspace: process.env.BLAXEL_WORKSPACE
|
|
86
|
+
})
|
|
81
87
|
});
|
|
82
88
|
|
|
83
89
|
// Create a sandbox
|
|
@@ -93,6 +99,40 @@ await compute.sandbox.destroy(sandbox.sandboxId);
|
|
|
93
99
|
|
|
94
100
|
## Provider Setup
|
|
95
101
|
|
|
102
|
+
### Blaxel - AI-Powered Code Execution
|
|
103
|
+
|
|
104
|
+
Blaxel provides intelligent code execution with AI assistance:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
export BLAXEL_API_KEY=your_blaxel_api_key_here
|
|
108
|
+
export BLAXEL_WORKSPACE=your_workspace_here
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { compute } from 'computesdk';
|
|
113
|
+
import { blaxel } from '@computesdk/blaxel';
|
|
114
|
+
|
|
115
|
+
compute.setConfig({
|
|
116
|
+
provider: blaxel({
|
|
117
|
+
apiKey: process.env.BLAXEL_API_KEY,
|
|
118
|
+
workspace: process.env.BLAXEL_WORKSPACE
|
|
119
|
+
})
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const sandbox = await compute.sandbox.create({});
|
|
123
|
+
|
|
124
|
+
// Execute code with AI assistance
|
|
125
|
+
const result = await sandbox.runCode(`
|
|
126
|
+
print("Hello from Blaxel!")
|
|
127
|
+
# Your code can leverage AI capabilities
|
|
128
|
+
import json
|
|
129
|
+
data = {"message": "AI-powered execution"}
|
|
130
|
+
print(json.dumps(data, indent=2))
|
|
131
|
+
`);
|
|
132
|
+
|
|
133
|
+
console.log(result.stdout);
|
|
134
|
+
```
|
|
135
|
+
|
|
96
136
|
### E2B - Full Development Environment
|
|
97
137
|
|
|
98
138
|
E2B provides full filesystem and terminal support:
|
package/dist/index.d.mts
CHANGED
|
@@ -86,7 +86,7 @@ interface SandboxInfo {
|
|
|
86
86
|
* Options for creating a sandbox
|
|
87
87
|
*/
|
|
88
88
|
interface CreateSandboxOptions {
|
|
89
|
-
/** Runtime environment */
|
|
89
|
+
/** Runtime environment (defaults to 'node' if not specified) */
|
|
90
90
|
runtime?: Runtime;
|
|
91
91
|
/** Execution timeout in milliseconds */
|
|
92
92
|
timeout?: number;
|
|
@@ -201,6 +201,42 @@ interface TypedSandbox<TProvider extends Provider$1> extends Omit<Sandbox<Extrac
|
|
|
201
201
|
getInstance(): ExtractProviderSandboxType<TProvider>;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
+
/**
|
|
205
|
+
* Common options for creating snapshots
|
|
206
|
+
*/
|
|
207
|
+
interface CreateSnapshotOptions {
|
|
208
|
+
/** Optional name for the snapshot */
|
|
209
|
+
name?: string;
|
|
210
|
+
/** Optional metadata for the snapshot */
|
|
211
|
+
metadata?: Record<string, string>;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Common options for listing snapshots
|
|
215
|
+
*/
|
|
216
|
+
interface ListSnapshotsOptions {
|
|
217
|
+
/** Filter by sandbox ID */
|
|
218
|
+
sandboxId?: string;
|
|
219
|
+
/** Limit the number of results */
|
|
220
|
+
limit?: number;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Common options for creating templates/blueprints
|
|
224
|
+
*/
|
|
225
|
+
interface CreateTemplateOptions {
|
|
226
|
+
/** Name of the template */
|
|
227
|
+
name: string;
|
|
228
|
+
/** Optional description */
|
|
229
|
+
description?: string;
|
|
230
|
+
/** Optional metadata for the template */
|
|
231
|
+
metadata?: Record<string, string>;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Common options for listing templates
|
|
235
|
+
*/
|
|
236
|
+
interface ListTemplatesOptions {
|
|
237
|
+
/** Limit the number of results */
|
|
238
|
+
limit?: number;
|
|
239
|
+
}
|
|
204
240
|
/**
|
|
205
241
|
* Provider sandbox manager interface - handles sandbox lifecycle
|
|
206
242
|
*/
|
|
@@ -214,14 +250,42 @@ interface ProviderSandboxManager<TSandbox = any> {
|
|
|
214
250
|
/** Destroy a sandbox */
|
|
215
251
|
destroy(sandboxId: string): Promise<void>;
|
|
216
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Provider template manager interface - handles template/blueprint lifecycle
|
|
255
|
+
*/
|
|
256
|
+
interface ProviderTemplateManager<TTemplate = any> {
|
|
257
|
+
/** Create a new template */
|
|
258
|
+
create(options: CreateTemplateOptions | any): Promise<TTemplate>;
|
|
259
|
+
/** List all available templates */
|
|
260
|
+
list(options?: ListTemplatesOptions): Promise<TTemplate[]>;
|
|
261
|
+
/** Delete a template */
|
|
262
|
+
delete(templateId: string): Promise<void>;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Provider snapshot manager interface - handles snapshot lifecycle
|
|
266
|
+
*/
|
|
267
|
+
interface ProviderSnapshotManager<TSnapshot = any> {
|
|
268
|
+
/** Create a snapshot from a sandbox */
|
|
269
|
+
create(sandboxId: string, options?: CreateSnapshotOptions): Promise<TSnapshot>;
|
|
270
|
+
/** List all snapshots */
|
|
271
|
+
list(options?: ListSnapshotsOptions): Promise<TSnapshot[]>;
|
|
272
|
+
/** Delete a snapshot */
|
|
273
|
+
delete(snapshotId: string): Promise<void>;
|
|
274
|
+
}
|
|
217
275
|
/**
|
|
218
276
|
* Provider interface - creates and manages resources
|
|
219
277
|
*/
|
|
220
|
-
interface Provider<TSandbox = any> {
|
|
278
|
+
interface Provider<TSandbox = any, TTemplate = any, TSnapshot = any> {
|
|
221
279
|
/** Provider name/type */
|
|
222
280
|
readonly name: string;
|
|
223
281
|
/** Sandbox management operations */
|
|
224
282
|
readonly sandbox: ProviderSandboxManager<TSandbox>;
|
|
283
|
+
/** Optional template management operations */
|
|
284
|
+
readonly template?: ProviderTemplateManager<TTemplate>;
|
|
285
|
+
/** Optional snapshot management operations */
|
|
286
|
+
readonly snapshot?: ProviderSnapshotManager<TSnapshot>;
|
|
287
|
+
/** Get the list of supported runtime environments */
|
|
288
|
+
getSupportedRuntimes(): Runtime[];
|
|
225
289
|
/** Phantom type property for TypeScript inference - not used at runtime */
|
|
226
290
|
readonly __sandboxType: TSandbox;
|
|
227
291
|
}
|
|
@@ -233,6 +297,10 @@ interface ComputeConfig<TProvider extends Provider = Provider> {
|
|
|
233
297
|
defaultProvider?: TProvider;
|
|
234
298
|
/** @deprecated Use defaultProvider instead. Kept for backwards compatibility */
|
|
235
299
|
provider?: TProvider;
|
|
300
|
+
/** API key for compute CLI authentication */
|
|
301
|
+
apiKey?: string;
|
|
302
|
+
/** JWT token for compute CLI authentication */
|
|
303
|
+
jwt?: string;
|
|
236
304
|
}
|
|
237
305
|
/**
|
|
238
306
|
* Parameters for compute.sandbox.create()
|
|
@@ -422,13 +490,31 @@ interface SandboxMethods<TSandbox = any, TConfig = any> {
|
|
|
422
490
|
remove: (sandbox: TSandbox, path: string, runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>) => Promise<void>;
|
|
423
491
|
};
|
|
424
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* Template method implementations
|
|
495
|
+
*/
|
|
496
|
+
interface TemplateMethods<TTemplate = any, TConfig = any> {
|
|
497
|
+
create: (config: TConfig, options: CreateTemplateOptions | any) => Promise<TTemplate>;
|
|
498
|
+
list: (config: TConfig, options?: ListTemplatesOptions) => Promise<TTemplate[]>;
|
|
499
|
+
delete: (config: TConfig, templateId: string) => Promise<void>;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Snapshot method implementations
|
|
503
|
+
*/
|
|
504
|
+
interface SnapshotMethods<TSnapshot = any, TConfig = any> {
|
|
505
|
+
create: (config: TConfig, sandboxId: string, options?: CreateSnapshotOptions) => Promise<TSnapshot>;
|
|
506
|
+
list: (config: TConfig, options?: ListSnapshotsOptions) => Promise<TSnapshot[]>;
|
|
507
|
+
delete: (config: TConfig, snapshotId: string) => Promise<void>;
|
|
508
|
+
}
|
|
425
509
|
/**
|
|
426
510
|
* Provider configuration for createProvider()
|
|
427
511
|
*/
|
|
428
|
-
interface ProviderConfig<TSandbox = any, TConfig = any> {
|
|
512
|
+
interface ProviderConfig<TSandbox = any, TConfig = any, TTemplate = any, TSnapshot = any> {
|
|
429
513
|
name: string;
|
|
430
514
|
methods: {
|
|
431
515
|
sandbox: SandboxMethods<TSandbox, TConfig>;
|
|
516
|
+
template?: TemplateMethods<TTemplate, TConfig>;
|
|
517
|
+
snapshot?: SnapshotMethods<TSnapshot, TConfig>;
|
|
432
518
|
};
|
|
433
519
|
}
|
|
434
520
|
/**
|
|
@@ -437,6 +523,6 @@ interface ProviderConfig<TSandbox = any, TConfig = any> {
|
|
|
437
523
|
* Auto-generates all boilerplate classes and provides feature detection
|
|
438
524
|
* based on which methods are implemented.
|
|
439
525
|
*/
|
|
440
|
-
declare function createProvider<TSandbox, TConfig>(providerConfig: ProviderConfig<TSandbox, TConfig>): (config: TConfig) => Provider<TSandbox>;
|
|
526
|
+
declare function createProvider<TSandbox, TConfig = any, TTemplate = any, TSnapshot = any>(providerConfig: ProviderConfig<TSandbox, TConfig, TTemplate, TSnapshot>): (config: TConfig) => Provider<TSandbox, TTemplate, TSnapshot>;
|
|
441
527
|
|
|
442
|
-
export { CommandExitError, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxOptions, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type ExecutionResult, type ExtractSandboxInstanceType, type FileEntry, type HandleComputeRequestParams, type Provider, type ProviderConfig, type ProviderSandboxManager, type ProviderSandboxTypeMap, type RunCommandOptions, type Runtime, type Sandbox, type SandboxFileSystem, type SandboxInfo, type SandboxMethods, type SandboxStatus, type TypedComputeAPI, type TypedSandbox, compute, createBackgroundCommand, createCompute, createProvider, handleComputeRequest, isCommandExitError };
|
|
528
|
+
export { CommandExitError, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxOptions, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type CreateSnapshotOptions, type CreateTemplateOptions, type ExecutionResult, type ExtractSandboxInstanceType, type FileEntry, type HandleComputeRequestParams, type ListSnapshotsOptions, type ListTemplatesOptions, type Provider, type ProviderConfig, type ProviderSandboxManager, type ProviderSandboxTypeMap, type ProviderSnapshotManager, type ProviderTemplateManager, type RunCommandOptions, type Runtime, type Sandbox, type SandboxFileSystem, type SandboxInfo, type SandboxMethods, type SandboxStatus, type SnapshotMethods, type TemplateMethods, type TypedComputeAPI, type TypedSandbox, compute, createBackgroundCommand, createCompute, createProvider, handleComputeRequest, isCommandExitError };
|
package/dist/index.d.ts
CHANGED
|
@@ -86,7 +86,7 @@ interface SandboxInfo {
|
|
|
86
86
|
* Options for creating a sandbox
|
|
87
87
|
*/
|
|
88
88
|
interface CreateSandboxOptions {
|
|
89
|
-
/** Runtime environment */
|
|
89
|
+
/** Runtime environment (defaults to 'node' if not specified) */
|
|
90
90
|
runtime?: Runtime;
|
|
91
91
|
/** Execution timeout in milliseconds */
|
|
92
92
|
timeout?: number;
|
|
@@ -201,6 +201,42 @@ interface TypedSandbox<TProvider extends Provider$1> extends Omit<Sandbox<Extrac
|
|
|
201
201
|
getInstance(): ExtractProviderSandboxType<TProvider>;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
+
/**
|
|
205
|
+
* Common options for creating snapshots
|
|
206
|
+
*/
|
|
207
|
+
interface CreateSnapshotOptions {
|
|
208
|
+
/** Optional name for the snapshot */
|
|
209
|
+
name?: string;
|
|
210
|
+
/** Optional metadata for the snapshot */
|
|
211
|
+
metadata?: Record<string, string>;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Common options for listing snapshots
|
|
215
|
+
*/
|
|
216
|
+
interface ListSnapshotsOptions {
|
|
217
|
+
/** Filter by sandbox ID */
|
|
218
|
+
sandboxId?: string;
|
|
219
|
+
/** Limit the number of results */
|
|
220
|
+
limit?: number;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Common options for creating templates/blueprints
|
|
224
|
+
*/
|
|
225
|
+
interface CreateTemplateOptions {
|
|
226
|
+
/** Name of the template */
|
|
227
|
+
name: string;
|
|
228
|
+
/** Optional description */
|
|
229
|
+
description?: string;
|
|
230
|
+
/** Optional metadata for the template */
|
|
231
|
+
metadata?: Record<string, string>;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Common options for listing templates
|
|
235
|
+
*/
|
|
236
|
+
interface ListTemplatesOptions {
|
|
237
|
+
/** Limit the number of results */
|
|
238
|
+
limit?: number;
|
|
239
|
+
}
|
|
204
240
|
/**
|
|
205
241
|
* Provider sandbox manager interface - handles sandbox lifecycle
|
|
206
242
|
*/
|
|
@@ -214,14 +250,42 @@ interface ProviderSandboxManager<TSandbox = any> {
|
|
|
214
250
|
/** Destroy a sandbox */
|
|
215
251
|
destroy(sandboxId: string): Promise<void>;
|
|
216
252
|
}
|
|
253
|
+
/**
|
|
254
|
+
* Provider template manager interface - handles template/blueprint lifecycle
|
|
255
|
+
*/
|
|
256
|
+
interface ProviderTemplateManager<TTemplate = any> {
|
|
257
|
+
/** Create a new template */
|
|
258
|
+
create(options: CreateTemplateOptions | any): Promise<TTemplate>;
|
|
259
|
+
/** List all available templates */
|
|
260
|
+
list(options?: ListTemplatesOptions): Promise<TTemplate[]>;
|
|
261
|
+
/** Delete a template */
|
|
262
|
+
delete(templateId: string): Promise<void>;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Provider snapshot manager interface - handles snapshot lifecycle
|
|
266
|
+
*/
|
|
267
|
+
interface ProviderSnapshotManager<TSnapshot = any> {
|
|
268
|
+
/** Create a snapshot from a sandbox */
|
|
269
|
+
create(sandboxId: string, options?: CreateSnapshotOptions): Promise<TSnapshot>;
|
|
270
|
+
/** List all snapshots */
|
|
271
|
+
list(options?: ListSnapshotsOptions): Promise<TSnapshot[]>;
|
|
272
|
+
/** Delete a snapshot */
|
|
273
|
+
delete(snapshotId: string): Promise<void>;
|
|
274
|
+
}
|
|
217
275
|
/**
|
|
218
276
|
* Provider interface - creates and manages resources
|
|
219
277
|
*/
|
|
220
|
-
interface Provider<TSandbox = any> {
|
|
278
|
+
interface Provider<TSandbox = any, TTemplate = any, TSnapshot = any> {
|
|
221
279
|
/** Provider name/type */
|
|
222
280
|
readonly name: string;
|
|
223
281
|
/** Sandbox management operations */
|
|
224
282
|
readonly sandbox: ProviderSandboxManager<TSandbox>;
|
|
283
|
+
/** Optional template management operations */
|
|
284
|
+
readonly template?: ProviderTemplateManager<TTemplate>;
|
|
285
|
+
/** Optional snapshot management operations */
|
|
286
|
+
readonly snapshot?: ProviderSnapshotManager<TSnapshot>;
|
|
287
|
+
/** Get the list of supported runtime environments */
|
|
288
|
+
getSupportedRuntimes(): Runtime[];
|
|
225
289
|
/** Phantom type property for TypeScript inference - not used at runtime */
|
|
226
290
|
readonly __sandboxType: TSandbox;
|
|
227
291
|
}
|
|
@@ -233,6 +297,10 @@ interface ComputeConfig<TProvider extends Provider = Provider> {
|
|
|
233
297
|
defaultProvider?: TProvider;
|
|
234
298
|
/** @deprecated Use defaultProvider instead. Kept for backwards compatibility */
|
|
235
299
|
provider?: TProvider;
|
|
300
|
+
/** API key for compute CLI authentication */
|
|
301
|
+
apiKey?: string;
|
|
302
|
+
/** JWT token for compute CLI authentication */
|
|
303
|
+
jwt?: string;
|
|
236
304
|
}
|
|
237
305
|
/**
|
|
238
306
|
* Parameters for compute.sandbox.create()
|
|
@@ -422,13 +490,31 @@ interface SandboxMethods<TSandbox = any, TConfig = any> {
|
|
|
422
490
|
remove: (sandbox: TSandbox, path: string, runCommand: (sandbox: TSandbox, command: string, args?: string[]) => Promise<ExecutionResult>) => Promise<void>;
|
|
423
491
|
};
|
|
424
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* Template method implementations
|
|
495
|
+
*/
|
|
496
|
+
interface TemplateMethods<TTemplate = any, TConfig = any> {
|
|
497
|
+
create: (config: TConfig, options: CreateTemplateOptions | any) => Promise<TTemplate>;
|
|
498
|
+
list: (config: TConfig, options?: ListTemplatesOptions) => Promise<TTemplate[]>;
|
|
499
|
+
delete: (config: TConfig, templateId: string) => Promise<void>;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Snapshot method implementations
|
|
503
|
+
*/
|
|
504
|
+
interface SnapshotMethods<TSnapshot = any, TConfig = any> {
|
|
505
|
+
create: (config: TConfig, sandboxId: string, options?: CreateSnapshotOptions) => Promise<TSnapshot>;
|
|
506
|
+
list: (config: TConfig, options?: ListSnapshotsOptions) => Promise<TSnapshot[]>;
|
|
507
|
+
delete: (config: TConfig, snapshotId: string) => Promise<void>;
|
|
508
|
+
}
|
|
425
509
|
/**
|
|
426
510
|
* Provider configuration for createProvider()
|
|
427
511
|
*/
|
|
428
|
-
interface ProviderConfig<TSandbox = any, TConfig = any> {
|
|
512
|
+
interface ProviderConfig<TSandbox = any, TConfig = any, TTemplate = any, TSnapshot = any> {
|
|
429
513
|
name: string;
|
|
430
514
|
methods: {
|
|
431
515
|
sandbox: SandboxMethods<TSandbox, TConfig>;
|
|
516
|
+
template?: TemplateMethods<TTemplate, TConfig>;
|
|
517
|
+
snapshot?: SnapshotMethods<TSnapshot, TConfig>;
|
|
432
518
|
};
|
|
433
519
|
}
|
|
434
520
|
/**
|
|
@@ -437,6 +523,6 @@ interface ProviderConfig<TSandbox = any, TConfig = any> {
|
|
|
437
523
|
* Auto-generates all boilerplate classes and provides feature detection
|
|
438
524
|
* based on which methods are implemented.
|
|
439
525
|
*/
|
|
440
|
-
declare function createProvider<TSandbox, TConfig>(providerConfig: ProviderConfig<TSandbox, TConfig>): (config: TConfig) => Provider<TSandbox>;
|
|
526
|
+
declare function createProvider<TSandbox, TConfig = any, TTemplate = any, TSnapshot = any>(providerConfig: ProviderConfig<TSandbox, TConfig, TTemplate, TSnapshot>): (config: TConfig) => Provider<TSandbox, TTemplate, TSnapshot>;
|
|
441
527
|
|
|
442
|
-
export { CommandExitError, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxOptions, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type ExecutionResult, type ExtractSandboxInstanceType, type FileEntry, type HandleComputeRequestParams, type Provider, type ProviderConfig, type ProviderSandboxManager, type ProviderSandboxTypeMap, type RunCommandOptions, type Runtime, type Sandbox, type SandboxFileSystem, type SandboxInfo, type SandboxMethods, type SandboxStatus, type TypedComputeAPI, type TypedSandbox, compute, createBackgroundCommand, createCompute, createProvider, handleComputeRequest, isCommandExitError };
|
|
528
|
+
export { CommandExitError, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxOptions, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type CreateSnapshotOptions, type CreateTemplateOptions, type ExecutionResult, type ExtractSandboxInstanceType, type FileEntry, type HandleComputeRequestParams, type ListSnapshotsOptions, type ListTemplatesOptions, type Provider, type ProviderConfig, type ProviderSandboxManager, type ProviderSandboxTypeMap, type ProviderSnapshotManager, type ProviderTemplateManager, type RunCommandOptions, type Runtime, type Sandbox, type SandboxFileSystem, type SandboxInfo, type SandboxMethods, type SandboxStatus, type SnapshotMethods, type TemplateMethods, type TypedComputeAPI, type TypedSandbox, compute, createBackgroundCommand, createCompute, createProvider, handleComputeRequest, isCommandExitError };
|
package/dist/index.js
CHANGED
|
@@ -43,10 +43,70 @@ function isCommandExitError(error) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
// src/compute.ts
|
|
46
|
+
var import_client = require("@computesdk/client");
|
|
47
|
+
async function authorizeApiKey(apiKey) {
|
|
48
|
+
try {
|
|
49
|
+
const response = await fetch("https://preview.computesdk.com/__api/license/authorize", {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers: {
|
|
52
|
+
"Content-Type": "application/json"
|
|
53
|
+
},
|
|
54
|
+
body: JSON.stringify({
|
|
55
|
+
key: apiKey,
|
|
56
|
+
increment_usage: 1
|
|
57
|
+
})
|
|
58
|
+
});
|
|
59
|
+
if (!response.ok) {
|
|
60
|
+
throw new Error(`License authorization failed: ${response.statusText}`);
|
|
61
|
+
}
|
|
62
|
+
const data = await response.json();
|
|
63
|
+
if (!data.jwt) {
|
|
64
|
+
throw new Error("No JWT token received from license server");
|
|
65
|
+
}
|
|
66
|
+
if (!data.sandbox_url) {
|
|
67
|
+
throw new Error("No sandbox_url received from license server");
|
|
68
|
+
}
|
|
69
|
+
if (!data.preview_url) {
|
|
70
|
+
throw new Error("No preview_url received from license server");
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
jwt: data.jwt,
|
|
74
|
+
sandbox_url: data.sandbox_url,
|
|
75
|
+
preview_url: data.preview_url
|
|
76
|
+
};
|
|
77
|
+
} catch (error) {
|
|
78
|
+
throw new Error(`Failed to authorize API key: ${error}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async function installComputeInSandbox(sandbox, config) {
|
|
82
|
+
try {
|
|
83
|
+
console.log("Installing and starting compute CLI in sandbox...");
|
|
84
|
+
let authResponse = null;
|
|
85
|
+
let jwt = config?.jwt;
|
|
86
|
+
if (config?.apiKey && !jwt) {
|
|
87
|
+
console.log("Authorizing API key and getting JWT token...");
|
|
88
|
+
authResponse = await authorizeApiKey(config.apiKey);
|
|
89
|
+
jwt = authResponse.jwt;
|
|
90
|
+
}
|
|
91
|
+
let installCommand = "curl -fsSL https://computesdk.com/install.sh | sh";
|
|
92
|
+
if (jwt) {
|
|
93
|
+
installCommand = `curl -fsSL https://computesdk.com/install.sh | sh -s -- --jwt ${jwt}`;
|
|
94
|
+
}
|
|
95
|
+
const result = await sandbox.runCommand("sh", ["-c", installCommand]);
|
|
96
|
+
if (result.exitCode !== 0) {
|
|
97
|
+
throw new Error(`Failed to install and start compute CLI: ${result.stderr}`);
|
|
98
|
+
}
|
|
99
|
+
console.log("Compute CLI installed and started successfully");
|
|
100
|
+
return authResponse;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throw new Error(`Failed to install compute CLI in sandbox: ${error}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
46
105
|
var ComputeManager = class {
|
|
47
106
|
constructor() {
|
|
48
107
|
this.config = null;
|
|
49
108
|
this.typedState = { isTyped: false, provider: null };
|
|
109
|
+
this.computeAuth = {};
|
|
50
110
|
this.sandbox = {
|
|
51
111
|
/**
|
|
52
112
|
* Create a sandbox from a provider (or default provider if configured)
|
|
@@ -71,10 +131,20 @@ var ComputeManager = class {
|
|
|
71
131
|
const provider = params && "provider" in params && params.provider ? params.provider : this.getDefaultProvider();
|
|
72
132
|
const options = params?.options;
|
|
73
133
|
const sandbox = await provider.sandbox.create(options);
|
|
134
|
+
const authResponse = await installComputeInSandbox(sandbox, this.computeAuth);
|
|
135
|
+
let finalSandbox = sandbox;
|
|
136
|
+
if (authResponse) {
|
|
137
|
+
finalSandbox = new import_client.ComputeClient({
|
|
138
|
+
sandboxUrl: authResponse.sandbox_url,
|
|
139
|
+
sandboxId: sandbox.sandboxId,
|
|
140
|
+
provider: sandbox.provider,
|
|
141
|
+
token: authResponse.jwt
|
|
142
|
+
});
|
|
143
|
+
}
|
|
74
144
|
if (this.typedState.isTyped && (!params || !("provider" in params && params.provider))) {
|
|
75
|
-
return
|
|
145
|
+
return finalSandbox;
|
|
76
146
|
}
|
|
77
|
-
return
|
|
147
|
+
return finalSandbox;
|
|
78
148
|
},
|
|
79
149
|
/**
|
|
80
150
|
* Get an existing sandbox by ID from a provider (or default provider if configured)
|
|
@@ -134,7 +204,13 @@ var ComputeManager = class {
|
|
|
134
204
|
const actualProvider = config.defaultProvider || config.provider;
|
|
135
205
|
this.config = {
|
|
136
206
|
provider: actualProvider,
|
|
137
|
-
defaultProvider: actualProvider
|
|
207
|
+
defaultProvider: actualProvider,
|
|
208
|
+
apiKey: config.apiKey,
|
|
209
|
+
jwt: config.jwt
|
|
210
|
+
};
|
|
211
|
+
this.computeAuth = {
|
|
212
|
+
apiKey: config.apiKey,
|
|
213
|
+
jwt: config.jwt
|
|
138
214
|
};
|
|
139
215
|
this.typedState = {
|
|
140
216
|
isTyped: true,
|
|
@@ -176,12 +252,18 @@ function createCompute(config) {
|
|
|
176
252
|
const actualProvider = config.defaultProvider || config.provider;
|
|
177
253
|
manager["config"] = {
|
|
178
254
|
provider: actualProvider,
|
|
179
|
-
defaultProvider: actualProvider
|
|
255
|
+
defaultProvider: actualProvider,
|
|
256
|
+
apiKey: config.apiKey,
|
|
257
|
+
jwt: config.jwt
|
|
180
258
|
};
|
|
181
259
|
manager["typedState"] = {
|
|
182
260
|
isTyped: true,
|
|
183
261
|
provider: actualProvider
|
|
184
262
|
};
|
|
263
|
+
manager["computeAuth"] = {
|
|
264
|
+
apiKey: config.apiKey,
|
|
265
|
+
jwt: config.jwt
|
|
266
|
+
};
|
|
185
267
|
return {
|
|
186
268
|
setConfig: (cfg) => createCompute(cfg),
|
|
187
269
|
getConfig: () => manager.getConfig(),
|
|
@@ -591,7 +673,8 @@ var GeneratedSandboxManager = class {
|
|
|
591
673
|
this.activeSandboxes = /* @__PURE__ */ new Map();
|
|
592
674
|
}
|
|
593
675
|
async create(options) {
|
|
594
|
-
const
|
|
676
|
+
const optionsWithDefaults = { runtime: "node", ...options };
|
|
677
|
+
const result = await this.methods.create(this.config, optionsWithDefaults);
|
|
595
678
|
const sandbox = new GeneratedSandbox(
|
|
596
679
|
result.sandbox,
|
|
597
680
|
result.sandboxId,
|
|
@@ -651,6 +734,36 @@ var GeneratedSandboxManager = class {
|
|
|
651
734
|
this.activeSandboxes.delete(sandboxId);
|
|
652
735
|
}
|
|
653
736
|
};
|
|
737
|
+
var GeneratedTemplateManager = class {
|
|
738
|
+
constructor(config, methods) {
|
|
739
|
+
this.config = config;
|
|
740
|
+
this.methods = methods;
|
|
741
|
+
}
|
|
742
|
+
async create(options) {
|
|
743
|
+
return await this.methods.create(this.config, options);
|
|
744
|
+
}
|
|
745
|
+
async list(options) {
|
|
746
|
+
return await this.methods.list(this.config, options);
|
|
747
|
+
}
|
|
748
|
+
async delete(templateId) {
|
|
749
|
+
return await this.methods.delete(this.config, templateId);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
var GeneratedSnapshotManager = class {
|
|
753
|
+
constructor(config, methods) {
|
|
754
|
+
this.config = config;
|
|
755
|
+
this.methods = methods;
|
|
756
|
+
}
|
|
757
|
+
async create(sandboxId, options) {
|
|
758
|
+
return await this.methods.create(this.config, sandboxId, options);
|
|
759
|
+
}
|
|
760
|
+
async list(options) {
|
|
761
|
+
return await this.methods.list(this.config, options);
|
|
762
|
+
}
|
|
763
|
+
async delete(snapshotId) {
|
|
764
|
+
return await this.methods.delete(this.config, snapshotId);
|
|
765
|
+
}
|
|
766
|
+
};
|
|
654
767
|
var GeneratedProvider = class {
|
|
655
768
|
// Phantom type for TypeScript inference
|
|
656
769
|
constructor(config, providerConfig) {
|
|
@@ -661,6 +774,15 @@ var GeneratedProvider = class {
|
|
|
661
774
|
providerConfig.methods.sandbox,
|
|
662
775
|
this
|
|
663
776
|
);
|
|
777
|
+
if (providerConfig.methods.template) {
|
|
778
|
+
this.template = new GeneratedTemplateManager(config, providerConfig.methods.template);
|
|
779
|
+
}
|
|
780
|
+
if (providerConfig.methods.snapshot) {
|
|
781
|
+
this.snapshot = new GeneratedSnapshotManager(config, providerConfig.methods.snapshot);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
getSupportedRuntimes() {
|
|
785
|
+
return ["node", "python"];
|
|
664
786
|
}
|
|
665
787
|
};
|
|
666
788
|
function createProvider(providerConfig) {
|