@blaxel/core 0.2.84-preview.154 → 0.2.84

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.
@@ -11,8 +11,8 @@ const index_js_1 = require("../authentication/index.js");
11
11
  const env_js_1 = require("../common/env.js");
12
12
  const node_js_1 = require("../common/node.js");
13
13
  // Build info - these placeholders are replaced at build time by build:replace-imports
14
- const BUILD_VERSION = "0.2.84-preview.154";
15
- const BUILD_COMMIT = "44e0848f215027fe6f3981e7decdc1ec192544cf";
14
+ const BUILD_VERSION = "0.2.84";
15
+ const BUILD_COMMIT = "b37c7bd0ef1257de732b54a2f784c6c91a8d450d";
16
16
  const BUILD_SENTRY_DSN = "https://fd5e60e1c9820e1eef5ccebb84a07127@o4508714045276160.ingest.us.sentry.io/4510465864564736";
17
17
  const BLAXEL_API_VERSION = "2026-04-16";
18
18
  // Cache for config.yaml tracking value
@@ -33,7 +33,7 @@ class CodeInterpreter extends sandbox_js_1.SandboxInstance {
33
33
  };
34
34
  return new CodeInterpreter(config);
35
35
  }
36
- static async create(sandbox, { safe = true } = {}) {
36
+ static async create(sandbox, { safe = true, createIfNotExist = false } = {}) {
37
37
  // Build a SandboxCreateConfiguration with CodeInterpreter defaults
38
38
  const defaults = {
39
39
  image: CodeInterpreter.DEFAULT_IMAGE,
@@ -66,7 +66,7 @@ class CodeInterpreter extends sandbox_js_1.SandboxInstance {
66
66
  else {
67
67
  merged = defaults;
68
68
  }
69
- const baseInstance = await super.create(merged, { safe });
69
+ const baseInstance = await super.create(merged, { safe, createIfNotExist });
70
70
  // Create config from the instance
71
71
  const config = {
72
72
  metadata: baseInstance.metadata,
@@ -47,6 +47,13 @@ const index_js_6 = require("./process/index.js");
47
47
  const session_js_1 = require("./session.js");
48
48
  const system_js_1 = require("./system.js");
49
49
  const types_js_1 = require("./types.js");
50
+ const NON_REUSABLE_SANDBOX_STATUSES = new Set([
51
+ "FAILED",
52
+ "TERMINATED",
53
+ "TERMINATING",
54
+ "DELETING",
55
+ "DEACTIVATING",
56
+ ]);
50
57
  class SandboxInstance {
51
58
  sandbox;
52
59
  fs;
@@ -132,7 +139,7 @@ class SandboxInstance {
132
139
  logger_js_1.logger.warn("⚠️ Warning: sandbox.wait() is deprecated. You don't need to wait for the sandbox to be deployed anymore.");
133
140
  return this;
134
141
  }
135
- static async create(sandbox, { safe = false } = {}) {
142
+ static async create(sandbox, { safe = false, createIfNotExist = false } = {}) {
136
143
  const defaultName = `sandbox-${(0, uuid_1.v4)().replace(/-/g, '').substring(0, 8)}`;
137
144
  const defaultImage = `blaxel/base-image:latest`;
138
145
  const defaultMemory = 4096;
@@ -216,6 +223,7 @@ class SandboxInstance {
216
223
  const [{ data }, h2Session] = await Promise.all([
217
224
  (0, index_js_1.createSandbox)({
218
225
  body: sandbox,
226
+ query: createIfNotExist ? { createIfNotExist } : undefined,
219
227
  throwOnError: true,
220
228
  }),
221
229
  edgeDomain && !settings_js_1.settings.disableH2 ? Promise.resolve().then(() => __importStar(require("../common/h2pool.js"))).then(({ h2Pool }) => h2Pool.get(edgeDomain)).catch(() => null) : Promise.resolve(null),
@@ -302,27 +310,30 @@ class SandboxInstance {
302
310
  return SandboxInstance.attachH2Session(instance);
303
311
  }
304
312
  static async createIfNotExists(sandbox) {
305
- try {
306
- return await this.create(sandbox);
307
- }
308
- catch (e) {
309
- if (typeof e === "object" && e !== null && "code" in e && (e.code === 409 || e.code === 'SANDBOX_ALREADY_EXISTS')) {
310
- const name = 'name' in sandbox ? sandbox.name : sandbox.metadata.name;
311
- if (!name) {
312
- throw new Error("Sandbox name is required");
313
- }
314
- // Get the existing sandbox to check its status
315
- const sandboxInstance = await this.get(name);
316
- // If the sandbox is TERMINATED, treat it as not existing
317
- if (sandboxInstance.status === "TERMINATED") {
318
- // Create a new sandbox - backend will handle cleanup of the terminated one
319
- return await this.create(sandbox);
313
+ const ATTEMPTS = 3;
314
+ for (let i = 0; i < ATTEMPTS; ++i) {
315
+ try {
316
+ return await this.create(sandbox, { createIfNotExist: true });
317
+ }
318
+ catch (e) {
319
+ if (typeof e === "object" && e !== null && "code" in e && (e.code === 409 || e.code === 'SANDBOX_ALREADY_EXISTS')) {
320
+ const name = 'name' in sandbox ? sandbox.name : sandbox.metadata.name;
321
+ if (!name) {
322
+ throw new Error("Sandbox name is required");
323
+ }
324
+ // Get the existing sandbox to check its status
325
+ const sandboxInstance = await this.get(name);
326
+ // Recreate instead of returning sandbox records that cannot be reused.
327
+ if (!NON_REUSABLE_SANDBOX_STATUSES.has(sandboxInstance.status ?? "")) {
328
+ return sandboxInstance;
329
+ }
330
+ // Retry creation. We want the same error handling on the retry as creates can race.
331
+ continue;
320
332
  }
321
- // Otherwise return the existing running sandbox
322
- return sandboxInstance;
333
+ throw e;
323
334
  }
324
- throw e;
325
335
  }
336
+ throw new Error(`Unable to create sandbox after ${ATTEMPTS} attempts.`);
326
337
  }
327
338
  /* eslint-disable */
328
339
  static async fromSession(session) {
@@ -8,8 +8,9 @@ export declare class CodeInterpreter extends SandboxInstance {
8
8
  private _sandboxConfig;
9
9
  constructor(sandbox: SandboxConfiguration);
10
10
  static get(sandboxName: string): Promise<CodeInterpreter>;
11
- static create(sandbox?: Sandbox | SandboxCreateConfiguration | Record<string, any> | null, { safe }?: {
11
+ static create(sandbox?: Sandbox | SandboxCreateConfiguration | Record<string, any> | null, { safe, createIfNotExist }?: {
12
12
  safe?: boolean;
13
+ createIfNotExist?: boolean;
13
14
  }): Promise<CodeInterpreter>;
14
15
  static createIfNotExists(sandbox: Sandbox | SandboxCreateConfiguration): Promise<CodeInterpreter>;
15
16
  get _jupyterUrl(): string;
@@ -46,8 +46,9 @@ export declare class SandboxInstance {
46
46
  maxWait?: number;
47
47
  interval?: number;
48
48
  }): Promise<this>;
49
- static create(sandbox?: SandboxModel | SandboxCreateConfiguration, { safe }?: {
49
+ static create(sandbox?: SandboxModel | SandboxCreateConfiguration, { safe, createIfNotExist }?: {
50
50
  safe?: boolean;
51
+ createIfNotExist?: boolean;
51
52
  }): Promise<SandboxInstance>;
52
53
  static get(sandboxName: string): Promise<SandboxInstance>;
53
54
  static list(): Promise<SandboxInstance[]>;