computesdk 1.9.6 → 1.10.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.mjs CHANGED
@@ -356,6 +356,54 @@ var GeneratedSandboxManager = class {
356
356
  async destroy(sandboxId) {
357
357
  await this.methods.destroy(this.config, sandboxId);
358
358
  }
359
+ async findOrCreate(options) {
360
+ if (!this.methods.findOrCreate) {
361
+ throw new Error(
362
+ `Provider '${this.providerName}' does not support findOrCreate.
363
+ This feature requires gateway provider with named sandbox support.`
364
+ );
365
+ }
366
+ const result = await this.methods.findOrCreate(this.config, options);
367
+ return new GeneratedSandbox(
368
+ result.sandbox,
369
+ result.sandboxId,
370
+ this.providerName,
371
+ this.methods,
372
+ this.config,
373
+ this.methods.destroy,
374
+ this.providerInstance
375
+ );
376
+ }
377
+ async find(options) {
378
+ if (!this.methods.find) {
379
+ throw new Error(
380
+ `Provider '${this.providerName}' does not support find.
381
+ This feature requires gateway provider with named sandbox support.`
382
+ );
383
+ }
384
+ const result = await this.methods.find(this.config, options);
385
+ if (!result) {
386
+ return null;
387
+ }
388
+ return new GeneratedSandbox(
389
+ result.sandbox,
390
+ result.sandboxId,
391
+ this.providerName,
392
+ this.methods,
393
+ this.config,
394
+ this.methods.destroy,
395
+ this.providerInstance
396
+ );
397
+ }
398
+ async extendTimeout(sandboxId, options) {
399
+ if (!this.methods.extendTimeout) {
400
+ throw new Error(
401
+ `Provider '${this.providerName}' does not support extendTimeout.
402
+ This feature requires gateway provider with timeout extension support.`
403
+ );
404
+ }
405
+ await this.methods.extendTimeout(this.config, sandboxId, options);
406
+ }
359
407
  };
360
408
  var GeneratedTemplateManager = class {
361
409
  constructor(config, methods) {
@@ -602,14 +650,14 @@ var gateway = createProvider({
602
650
  sandbox: {
603
651
  create: async (config, options) => {
604
652
  const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
605
- const result = await gatewayFetch(`${gatewayUrl}/v1/sandbox`, config, {
653
+ const result = await gatewayFetch(`${gatewayUrl}/v1/sandboxes`, config, {
606
654
  method: "POST",
607
655
  body: JSON.stringify(options || {})
608
656
  });
609
657
  if (!result.success || !result.data) {
610
658
  throw new Error(`Gateway returned invalid response: ${JSON.stringify(result)}`);
611
659
  }
612
- const { sandboxId, url, token, provider, metadata } = result.data;
660
+ const { sandboxId, url, token, provider, metadata, name, namespace } = result.data;
613
661
  if (process.env.COMPUTESDK_DEBUG) {
614
662
  console.log(`[Gateway] Sandbox created:`, {
615
663
  sandboxId,
@@ -631,7 +679,11 @@ var gateway = createProvider({
631
679
  provider,
632
680
  token: token || config.apiKey,
633
681
  // Use token from gateway, fallback to API key
634
- metadata,
682
+ metadata: {
683
+ ...metadata,
684
+ ...name && { name },
685
+ ...namespace && { namespace }
686
+ },
635
687
  WebSocket: globalThis.WebSocket
636
688
  });
637
689
  await waitForComputeReady(sandbox);
@@ -639,7 +691,7 @@ var gateway = createProvider({
639
691
  },
640
692
  getById: async (config, sandboxId) => {
641
693
  const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
642
- const result = await gatewayFetch(`${gatewayUrl}/v1/sandbox/${sandboxId}`, config);
694
+ const result = await gatewayFetch(`${gatewayUrl}/v1/sandboxes/${sandboxId}`, config);
643
695
  if (!result.success || !result.data) {
644
696
  return null;
645
697
  }
@@ -669,10 +721,97 @@ var gateway = createProvider({
669
721
  },
670
722
  destroy: async (config, sandboxId) => {
671
723
  const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
672
- await gatewayFetch(`${gatewayUrl}/v1/sandbox/${sandboxId}`, config, {
724
+ await gatewayFetch(`${gatewayUrl}/v1/sandboxes/${sandboxId}`, config, {
673
725
  method: "DELETE"
674
726
  });
675
727
  },
728
+ findOrCreate: async (config, options) => {
729
+ const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
730
+ const { name: requestedName, namespace: requestedNamespace, ...restOptions } = options;
731
+ const result = await gatewayFetch(`${gatewayUrl}/v1/sandboxes/find-or-create`, config, {
732
+ method: "POST",
733
+ body: JSON.stringify({
734
+ namespace: requestedNamespace || "default",
735
+ name: requestedName,
736
+ ...restOptions
737
+ })
738
+ });
739
+ if (!result.success || !result.data) {
740
+ throw new Error(`Gateway returned invalid response: ${JSON.stringify(result)}`);
741
+ }
742
+ const { sandboxId, url, token, provider, metadata, name, namespace } = result.data;
743
+ if (process.env.COMPUTESDK_DEBUG) {
744
+ console.log(`[Gateway] Named sandbox found/created:`, {
745
+ sandboxId,
746
+ name,
747
+ namespace,
748
+ url,
749
+ hasToken: !!token,
750
+ provider
751
+ });
752
+ }
753
+ const sandbox = new ClientSandbox({
754
+ sandboxUrl: url,
755
+ sandboxId,
756
+ provider,
757
+ token: token || config.apiKey,
758
+ metadata: {
759
+ ...metadata,
760
+ name,
761
+ namespace
762
+ },
763
+ WebSocket: globalThis.WebSocket
764
+ });
765
+ await waitForComputeReady(sandbox);
766
+ return { sandbox, sandboxId };
767
+ },
768
+ find: async (config, options) => {
769
+ const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
770
+ const result = await gatewayFetch(`${gatewayUrl}/v1/sandboxes/find`, config, {
771
+ method: "POST",
772
+ body: JSON.stringify({
773
+ namespace: options.namespace || "default",
774
+ name: options.name
775
+ })
776
+ });
777
+ if (!result.success || !result.data) {
778
+ return null;
779
+ }
780
+ const { sandboxId, url, token, provider, metadata, name, namespace } = result.data;
781
+ if (process.env.COMPUTESDK_DEBUG) {
782
+ console.log(`[Gateway] Named sandbox found:`, {
783
+ sandboxId,
784
+ name,
785
+ namespace,
786
+ url
787
+ });
788
+ }
789
+ const sandbox = new ClientSandbox({
790
+ sandboxUrl: url,
791
+ sandboxId,
792
+ provider,
793
+ token: token || config.apiKey,
794
+ metadata: {
795
+ ...metadata,
796
+ name,
797
+ namespace
798
+ },
799
+ WebSocket: globalThis.WebSocket
800
+ });
801
+ await waitForComputeReady(sandbox);
802
+ return { sandbox, sandboxId };
803
+ },
804
+ extendTimeout: async (config, sandboxId, options) => {
805
+ const gatewayUrl = config.gatewayUrl || DEFAULT_GATEWAY_URL;
806
+ const duration = options?.duration ?? 9e5;
807
+ if (process.env.COMPUTESDK_DEBUG) {
808
+ console.log(`[Gateway] Extending timeout for sandbox ${sandboxId} by ${duration}ms`);
809
+ }
810
+ await gatewayFetch(`${gatewayUrl}/v1/sandboxes/${sandboxId}/extend`, config, {
811
+ method: "POST",
812
+ body: JSON.stringify({ duration })
813
+ });
814
+ },
676
815
  // All operations delegate directly to ClientSandbox
677
816
  runCode: async (sandbox, code, runtime) => sandbox.runCode(code, runtime),
678
817
  runCommand: async (sandbox, command, args) => sandbox.runCommand(command, args),
@@ -1026,6 +1165,79 @@ var ComputeManager = class {
1026
1165
  }
1027
1166
  return await providerOrSandboxId.sandbox.destroy(sandboxId);
1028
1167
  }
1168
+ },
1169
+ /**
1170
+ * Find existing or create new sandbox by (namespace, name)
1171
+ *
1172
+ * @example
1173
+ * ```typescript
1174
+ * // Find or create sandbox for a user's project
1175
+ * const sandbox = await compute.sandbox.findOrCreate({
1176
+ * name: 'my-app',
1177
+ * namespace: 'user-123',
1178
+ * timeout: 1800000
1179
+ * });
1180
+ * ```
1181
+ */
1182
+ findOrCreate: async (options) => {
1183
+ const provider = this.getDefaultProvider();
1184
+ if (!provider.sandbox.findOrCreate) {
1185
+ throw new Error(
1186
+ `Provider '${provider.name}' does not support findOrCreate.
1187
+ This feature requires gateway provider with named sandbox support.`
1188
+ );
1189
+ }
1190
+ return await provider.sandbox.findOrCreate(options);
1191
+ },
1192
+ /**
1193
+ * Find existing sandbox by (namespace, name) without creating
1194
+ *
1195
+ * @example
1196
+ * ```typescript
1197
+ * // Find existing sandbox
1198
+ * const sandbox = await compute.sandbox.find({
1199
+ * name: 'my-app',
1200
+ * namespace: 'user-123'
1201
+ * });
1202
+ *
1203
+ * if (sandbox) {
1204
+ * console.log('Found sandbox:', sandbox.sandboxId);
1205
+ * }
1206
+ * ```
1207
+ */
1208
+ find: async (options) => {
1209
+ const provider = this.getDefaultProvider();
1210
+ if (!provider.sandbox.find) {
1211
+ throw new Error(
1212
+ `Provider '${provider.name}' does not support find.
1213
+ This feature requires gateway provider with named sandbox support.`
1214
+ );
1215
+ }
1216
+ return await provider.sandbox.find(options);
1217
+ },
1218
+ /**
1219
+ * Extend sandbox timeout/expiration
1220
+ *
1221
+ * @example
1222
+ * ```typescript
1223
+ * // Extend timeout by 15 minutes (default)
1224
+ * await compute.sandbox.extendTimeout('sandbox-123');
1225
+ *
1226
+ * // Extend timeout by custom duration
1227
+ * await compute.sandbox.extendTimeout('sandbox-123', {
1228
+ * duration: 1800000 // 30 minutes
1229
+ * });
1230
+ * ```
1231
+ */
1232
+ extendTimeout: async (sandboxId, options) => {
1233
+ const provider = this.getDefaultProvider();
1234
+ if (!provider.sandbox.extendTimeout) {
1235
+ throw new Error(
1236
+ `Provider '${provider.name}' does not support extendTimeout.
1237
+ This feature requires gateway provider with timeout extension support.`
1238
+ );
1239
+ }
1240
+ return await provider.sandbox.extendTimeout(sandboxId, options);
1029
1241
  }
1030
1242
  };
1031
1243
  }
@@ -1137,6 +1349,18 @@ function createCompute(config) {
1137
1349
  },
1138
1350
  destroy: async (sandboxId) => {
1139
1351
  return await manager.sandbox.destroy(sandboxId);
1352
+ },
1353
+ findOrCreate: async (options) => {
1354
+ const sandbox = await manager.sandbox.findOrCreate(options);
1355
+ return sandbox;
1356
+ },
1357
+ find: async (options) => {
1358
+ const sandbox = await manager.sandbox.find(options);
1359
+ if (!sandbox) return null;
1360
+ return sandbox;
1361
+ },
1362
+ extendTimeout: async (sandboxId, options) => {
1363
+ return await manager.sandbox.extendTimeout(sandboxId, options);
1140
1364
  }
1141
1365
  }
1142
1366
  };