computesdk 1.9.3 → 1.9.5

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
@@ -259,17 +259,89 @@ interface TypedComputeAPI<TProvider extends Provider> extends Omit<ComputeAPI, '
259
259
  destroy(sandboxId: string): Promise<void>;
260
260
  };
261
261
  }
262
+ /**
263
+ * E2B provider configuration for explicit compute mode
264
+ */
265
+ interface E2BProviderConfig {
266
+ /** E2B API key */
267
+ apiKey?: string;
268
+ /** E2B project ID */
269
+ projectId?: string;
270
+ /** E2B environment/template ID */
271
+ templateId?: string;
272
+ }
273
+ /**
274
+ * Modal provider configuration for explicit compute mode
275
+ */
276
+ interface ModalProviderConfig {
277
+ /** Modal token ID */
278
+ tokenId?: string;
279
+ /** Modal token secret */
280
+ tokenSecret?: string;
281
+ }
282
+ /**
283
+ * Railway provider configuration for explicit compute mode
284
+ */
285
+ interface RailwayProviderConfig {
286
+ /** Railway API token */
287
+ apiToken?: string;
288
+ }
289
+ /**
290
+ * Supported provider names for explicit compute mode
291
+ */
292
+ type ExplicitProviderName = 'e2b' | 'modal' | 'railway';
293
+ /**
294
+ * Explicit compute configuration for callable compute()
295
+ *
296
+ * Used when calling compute as a function: compute({ provider: 'e2b', ... })
297
+ * Always uses gateway mode.
298
+ */
299
+ interface ExplicitComputeConfig {
300
+ /** Provider name to use */
301
+ provider: ExplicitProviderName;
302
+ /** ComputeSDK API key (required for gateway mode) */
303
+ apiKey: string;
304
+ /** E2B provider configuration */
305
+ e2b?: E2BProviderConfig;
306
+ /** Modal provider configuration */
307
+ modal?: ModalProviderConfig;
308
+ /** Railway provider configuration */
309
+ railway?: RailwayProviderConfig;
310
+ }
311
+ /**
312
+ * Callable compute type - works as both singleton and factory function
313
+ */
314
+ type CallableCompute = ComputeAPI & ((config: ExplicitComputeConfig) => ComputeAPI);
262
315
 
263
316
  /**
264
317
  * Compute Singleton - Main API Orchestrator
265
318
  *
266
- * Provides the unified compute.* API and delegates to specialized managers
319
+ * Provides the unified compute.* API and delegates to specialized managers.
320
+ * The `compute` export works as both a singleton and a callable function:
321
+ *
322
+ * - Singleton: `compute.sandbox.create()` (auto-detects from env vars)
323
+ * - Callable: `compute({ provider: 'e2b', ... }).sandbox.create()` (explicit config, uses gateway)
267
324
  */
268
325
 
269
326
  /**
270
- * Singleton instance - the main API (untyped)
327
+ * Callable compute - works as both singleton and factory function
328
+ *
329
+ * @example
330
+ * ```typescript
331
+ * import { compute } from 'computesdk';
332
+ *
333
+ * // Singleton mode (auto-detects from env vars)
334
+ * const sandbox1 = await compute.sandbox.create();
335
+ *
336
+ * // Callable mode (explicit config, uses gateway)
337
+ * const sandbox2 = await compute({
338
+ * provider: 'e2b',
339
+ * apiKey: 'computesdk_xxx',
340
+ * e2b: { apiKey: 'e2b_xxx' }
341
+ * }).sandbox.create();
342
+ * ```
271
343
  */
272
- declare const compute: ComputeAPI;
344
+ declare const compute: CallableCompute;
273
345
  /**
274
346
  * Create a compute instance with proper typing
275
347
  *
@@ -294,6 +366,21 @@ declare const compute: ComputeAPI;
294
366
  declare function createCompute(): ComputeAPI;
295
367
  declare function createCompute<TProvider extends Provider>(config: ComputeConfig<TProvider>): TypedComputeAPI<TProvider>;
296
368
 
369
+ /**
370
+ * Explicit Config
371
+ *
372
+ * Converts explicit compute configuration to a gateway provider.
373
+ * Used when compute() is called as a function with configuration.
374
+ */
375
+
376
+ /**
377
+ * Create a gateway provider from explicit configuration
378
+ *
379
+ * @param config - Explicit compute configuration
380
+ * @returns A configured gateway provider
381
+ */
382
+ declare function createProviderFromConfig(config: ExplicitComputeConfig): Provider;
383
+
297
384
  /**
298
385
  * Provider Factory - Creates providers from method definitions
299
386
  *
@@ -583,4 +670,4 @@ interface HandleComputeRequestParams {
583
670
  provider: Provider;
584
671
  }
585
672
 
586
- export { type BaseProviderConfig, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type CreateSnapshotOptions, type CreateTemplateOptions, type ExtractProviderSandboxType, GATEWAY_URL, type GatewayConfig, type HandleComputeRequestParams, type ListSnapshotsOptions, type ListTemplatesOptions, PROVIDER_ENV_VARS, PROVIDER_PRIORITY, type Provider, type ProviderConfig, type ProviderMode, type ProviderName, type ProviderSandbox, type ProviderSandboxManager, type ProviderSnapshotManager, type ProviderTemplateManager, type SandboxInfo, type SandboxMethods, type SnapshotMethods, type TemplateMethods, type TypedComputeAPI, type TypedProviderSandbox, autoConfigureCompute, calculateBackoff, compute, createCompute, createProvider, detectProvider, gateway, getProviderHeaders, handleComputeRequest, isGatewayModeEnabled };
673
+ export { type BaseProviderConfig, type CallableCompute, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type CreateSnapshotOptions, type CreateTemplateOptions, type E2BProviderConfig, type ExplicitComputeConfig, type ExplicitProviderName, type ExtractProviderSandboxType, GATEWAY_URL, type GatewayConfig, type HandleComputeRequestParams, type ListSnapshotsOptions, type ListTemplatesOptions, type ModalProviderConfig, PROVIDER_ENV_VARS, PROVIDER_PRIORITY, type Provider, type ProviderConfig, type ProviderMode, type ProviderName, type ProviderSandbox, type ProviderSandboxManager, type ProviderSnapshotManager, type ProviderTemplateManager, type RailwayProviderConfig, type SandboxInfo, type SandboxMethods, type SnapshotMethods, type TemplateMethods, type TypedComputeAPI, type TypedProviderSandbox, autoConfigureCompute, calculateBackoff, compute, createCompute, createProvider, createProviderFromConfig, detectProvider, gateway, getProviderHeaders, handleComputeRequest, isGatewayModeEnabled };
package/dist/index.d.ts CHANGED
@@ -259,17 +259,89 @@ interface TypedComputeAPI<TProvider extends Provider> extends Omit<ComputeAPI, '
259
259
  destroy(sandboxId: string): Promise<void>;
260
260
  };
261
261
  }
262
+ /**
263
+ * E2B provider configuration for explicit compute mode
264
+ */
265
+ interface E2BProviderConfig {
266
+ /** E2B API key */
267
+ apiKey?: string;
268
+ /** E2B project ID */
269
+ projectId?: string;
270
+ /** E2B environment/template ID */
271
+ templateId?: string;
272
+ }
273
+ /**
274
+ * Modal provider configuration for explicit compute mode
275
+ */
276
+ interface ModalProviderConfig {
277
+ /** Modal token ID */
278
+ tokenId?: string;
279
+ /** Modal token secret */
280
+ tokenSecret?: string;
281
+ }
282
+ /**
283
+ * Railway provider configuration for explicit compute mode
284
+ */
285
+ interface RailwayProviderConfig {
286
+ /** Railway API token */
287
+ apiToken?: string;
288
+ }
289
+ /**
290
+ * Supported provider names for explicit compute mode
291
+ */
292
+ type ExplicitProviderName = 'e2b' | 'modal' | 'railway';
293
+ /**
294
+ * Explicit compute configuration for callable compute()
295
+ *
296
+ * Used when calling compute as a function: compute({ provider: 'e2b', ... })
297
+ * Always uses gateway mode.
298
+ */
299
+ interface ExplicitComputeConfig {
300
+ /** Provider name to use */
301
+ provider: ExplicitProviderName;
302
+ /** ComputeSDK API key (required for gateway mode) */
303
+ apiKey: string;
304
+ /** E2B provider configuration */
305
+ e2b?: E2BProviderConfig;
306
+ /** Modal provider configuration */
307
+ modal?: ModalProviderConfig;
308
+ /** Railway provider configuration */
309
+ railway?: RailwayProviderConfig;
310
+ }
311
+ /**
312
+ * Callable compute type - works as both singleton and factory function
313
+ */
314
+ type CallableCompute = ComputeAPI & ((config: ExplicitComputeConfig) => ComputeAPI);
262
315
 
263
316
  /**
264
317
  * Compute Singleton - Main API Orchestrator
265
318
  *
266
- * Provides the unified compute.* API and delegates to specialized managers
319
+ * Provides the unified compute.* API and delegates to specialized managers.
320
+ * The `compute` export works as both a singleton and a callable function:
321
+ *
322
+ * - Singleton: `compute.sandbox.create()` (auto-detects from env vars)
323
+ * - Callable: `compute({ provider: 'e2b', ... }).sandbox.create()` (explicit config, uses gateway)
267
324
  */
268
325
 
269
326
  /**
270
- * Singleton instance - the main API (untyped)
327
+ * Callable compute - works as both singleton and factory function
328
+ *
329
+ * @example
330
+ * ```typescript
331
+ * import { compute } from 'computesdk';
332
+ *
333
+ * // Singleton mode (auto-detects from env vars)
334
+ * const sandbox1 = await compute.sandbox.create();
335
+ *
336
+ * // Callable mode (explicit config, uses gateway)
337
+ * const sandbox2 = await compute({
338
+ * provider: 'e2b',
339
+ * apiKey: 'computesdk_xxx',
340
+ * e2b: { apiKey: 'e2b_xxx' }
341
+ * }).sandbox.create();
342
+ * ```
271
343
  */
272
- declare const compute: ComputeAPI;
344
+ declare const compute: CallableCompute;
273
345
  /**
274
346
  * Create a compute instance with proper typing
275
347
  *
@@ -294,6 +366,21 @@ declare const compute: ComputeAPI;
294
366
  declare function createCompute(): ComputeAPI;
295
367
  declare function createCompute<TProvider extends Provider>(config: ComputeConfig<TProvider>): TypedComputeAPI<TProvider>;
296
368
 
369
+ /**
370
+ * Explicit Config
371
+ *
372
+ * Converts explicit compute configuration to a gateway provider.
373
+ * Used when compute() is called as a function with configuration.
374
+ */
375
+
376
+ /**
377
+ * Create a gateway provider from explicit configuration
378
+ *
379
+ * @param config - Explicit compute configuration
380
+ * @returns A configured gateway provider
381
+ */
382
+ declare function createProviderFromConfig(config: ExplicitComputeConfig): Provider;
383
+
297
384
  /**
298
385
  * Provider Factory - Creates providers from method definitions
299
386
  *
@@ -583,4 +670,4 @@ interface HandleComputeRequestParams {
583
670
  provider: Provider;
584
671
  }
585
672
 
586
- export { type BaseProviderConfig, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type CreateSnapshotOptions, type CreateTemplateOptions, type ExtractProviderSandboxType, GATEWAY_URL, type GatewayConfig, type HandleComputeRequestParams, type ListSnapshotsOptions, type ListTemplatesOptions, PROVIDER_ENV_VARS, PROVIDER_PRIORITY, type Provider, type ProviderConfig, type ProviderMode, type ProviderName, type ProviderSandbox, type ProviderSandboxManager, type ProviderSnapshotManager, type ProviderTemplateManager, type SandboxInfo, type SandboxMethods, type SnapshotMethods, type TemplateMethods, type TypedComputeAPI, type TypedProviderSandbox, autoConfigureCompute, calculateBackoff, compute, createCompute, createProvider, detectProvider, gateway, getProviderHeaders, handleComputeRequest, isGatewayModeEnabled };
673
+ export { type BaseProviderConfig, type CallableCompute, type ComputeAPI, type ComputeConfig, type ComputeRequest, type ComputeResponse, type CreateSandboxParams, type CreateSandboxParamsWithOptionalProvider, type CreateSnapshotOptions, type CreateTemplateOptions, type E2BProviderConfig, type ExplicitComputeConfig, type ExplicitProviderName, type ExtractProviderSandboxType, GATEWAY_URL, type GatewayConfig, type HandleComputeRequestParams, type ListSnapshotsOptions, type ListTemplatesOptions, type ModalProviderConfig, PROVIDER_ENV_VARS, PROVIDER_PRIORITY, type Provider, type ProviderConfig, type ProviderMode, type ProviderName, type ProviderSandbox, type ProviderSandboxManager, type ProviderSnapshotManager, type ProviderTemplateManager, type RailwayProviderConfig, type SandboxInfo, type SandboxMethods, type SnapshotMethods, type TemplateMethods, type TypedComputeAPI, type TypedProviderSandbox, autoConfigureCompute, calculateBackoff, compute, createCompute, createProvider, createProviderFromConfig, detectProvider, gateway, getProviderHeaders, handleComputeRequest, isGatewayModeEnabled };
package/dist/index.js CHANGED
@@ -30,6 +30,7 @@ __export(index_exports, {
30
30
  compute: () => compute,
31
31
  createCompute: () => createCompute,
32
32
  createProvider: () => createProvider,
33
+ createProviderFromConfig: () => createProviderFromConfig,
33
34
  detectProvider: () => detectProvider,
34
35
  gateway: () => gateway,
35
36
  getProviderHeaders: () => getProviderHeaders,
@@ -487,7 +488,7 @@ var gateway = createProvider({
487
488
  sandbox: {
488
489
  create: async (config, options) => {
489
490
  const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
490
- const result = await gatewayFetch(`${gatewayUrl}/sandbox`, config, {
491
+ const result = await gatewayFetch(`${gatewayUrl}/v1/sandbox`, config, {
491
492
  method: "POST",
492
493
  body: JSON.stringify(options || {})
493
494
  });
@@ -524,7 +525,7 @@ var gateway = createProvider({
524
525
  },
525
526
  getById: async (config, sandboxId) => {
526
527
  const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
527
- const result = await gatewayFetch(`${gatewayUrl}/sandbox/${sandboxId}`, config);
528
+ const result = await gatewayFetch(`${gatewayUrl}/v1/sandbox/${sandboxId}`, config);
528
529
  if (!result.success || !result.data) {
529
530
  return null;
530
531
  }
@@ -554,7 +555,7 @@ var gateway = createProvider({
554
555
  },
555
556
  destroy: async (config, sandboxId) => {
556
557
  const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
557
- await gatewayFetch(`${gatewayUrl}/sandbox/${sandboxId}`, config, {
558
+ await gatewayFetch(`${gatewayUrl}/v1/sandbox/${sandboxId}`, config, {
558
559
  method: "DELETE"
559
560
  });
560
561
  },
@@ -772,6 +773,91 @@ Check your COMPUTESDK_GATEWAY_URL environment variable.`
772
773
  });
773
774
  }
774
775
 
776
+ // src/explicit-config.ts
777
+ function buildProviderHeaders(config) {
778
+ const headers = {};
779
+ switch (config.provider) {
780
+ case "e2b":
781
+ if (config.e2b?.apiKey) {
782
+ headers["X-E2B-API-Key"] = config.e2b.apiKey;
783
+ }
784
+ break;
785
+ case "modal":
786
+ if (config.modal?.tokenId) {
787
+ headers["X-Modal-Token-Id"] = config.modal.tokenId;
788
+ }
789
+ if (config.modal?.tokenSecret) {
790
+ headers["X-Modal-Token-Secret"] = config.modal.tokenSecret;
791
+ }
792
+ break;
793
+ case "railway":
794
+ if (config.railway?.apiToken) {
795
+ headers["X-Railway-API-Token"] = config.railway.apiToken;
796
+ }
797
+ break;
798
+ default: {
799
+ const exhaustiveCheck = config.provider;
800
+ throw new Error(`Unknown provider: ${exhaustiveCheck}`);
801
+ }
802
+ }
803
+ return headers;
804
+ }
805
+ function validateProviderConfig(config) {
806
+ const providerName = config.provider;
807
+ switch (providerName) {
808
+ case "e2b":
809
+ if (!config.e2b?.apiKey) {
810
+ throw new Error(
811
+ `Missing E2B configuration. When using provider: 'e2b', you must provide:
812
+ e2b: { apiKey: 'your-e2b-api-key' }
813
+
814
+ Get your API key at: https://e2b.dev/dashboard`
815
+ );
816
+ }
817
+ break;
818
+ case "modal":
819
+ if (!config.modal?.tokenId || !config.modal?.tokenSecret) {
820
+ throw new Error(
821
+ `Missing Modal configuration. When using provider: 'modal', you must provide:
822
+ modal: { tokenId: '...', tokenSecret: '...' }
823
+
824
+ Get your tokens at: https://modal.com/settings`
825
+ );
826
+ }
827
+ break;
828
+ case "railway":
829
+ if (!config.railway?.apiToken) {
830
+ throw new Error(
831
+ `Missing Railway configuration. When using provider: 'railway', you must provide:
832
+ railway: { apiToken: 'your-railway-token' }
833
+
834
+ Get your token at: https://railway.app/account/tokens`
835
+ );
836
+ }
837
+ break;
838
+ default: {
839
+ const exhaustiveCheck = providerName;
840
+ throw new Error(`Unknown provider: ${exhaustiveCheck}`);
841
+ }
842
+ }
843
+ }
844
+ function createProviderFromConfig(config) {
845
+ if (!config.apiKey) {
846
+ throw new Error(
847
+ `Missing ComputeSDK API key. The 'apiKey' field is required.
848
+
849
+ Get your API key at: https://computesdk.com/dashboard`
850
+ );
851
+ }
852
+ validateProviderConfig(config);
853
+ const providerHeaders = buildProviderHeaders(config);
854
+ return gateway({
855
+ apiKey: config.apiKey,
856
+ provider: config.provider,
857
+ providerHeaders
858
+ });
859
+ }
860
+
775
861
  // src/compute.ts
776
862
  var ComputeManager = class {
777
863
  constructor() {
@@ -895,7 +981,27 @@ var ComputeManager = class {
895
981
  return provider;
896
982
  }
897
983
  };
898
- var compute = new ComputeManager();
984
+ var singletonInstance = new ComputeManager();
985
+ function computeFactory(config) {
986
+ const provider = createProviderFromConfig(config);
987
+ return createCompute({ provider });
988
+ }
989
+ var compute = new Proxy(
990
+ computeFactory,
991
+ {
992
+ get(_target, prop, _receiver) {
993
+ const singleton = singletonInstance;
994
+ const value = singleton[prop];
995
+ if (typeof value === "function") {
996
+ return value.bind(singletonInstance);
997
+ }
998
+ return value;
999
+ },
1000
+ apply(_target, _thisArg, args) {
1001
+ return computeFactory(args[0]);
1002
+ }
1003
+ }
1004
+ );
899
1005
  function createCompute(config) {
900
1006
  const manager = new ComputeManager();
901
1007
  if (!config) {
@@ -1143,6 +1249,7 @@ async function handleComputeRequest(paramsOrRequestOrBody, provider) {
1143
1249
  compute,
1144
1250
  createCompute,
1145
1251
  createProvider,
1252
+ createProviderFromConfig,
1146
1253
  detectProvider,
1147
1254
  gateway,
1148
1255
  getProviderHeaders,