tensorlake 0.4.40 → 0.4.41

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,2105 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/sandbox-image.ts
31
+ var sandbox_image_exports = {};
32
+ __export(sandbox_image_exports, {
33
+ createSandboxImage: () => createSandboxImage,
34
+ defaultRegisteredName: () => defaultRegisteredName,
35
+ loadDockerfilePlan: () => loadDockerfilePlan,
36
+ loadImagePlan: () => loadImagePlan,
37
+ logicalDockerfileLines: () => logicalDockerfileLines,
38
+ runCreateSandboxImageCli: () => runCreateSandboxImageCli
39
+ });
40
+ module.exports = __toCommonJS(sandbox_image_exports);
41
+ var import_promises = require("fs/promises");
42
+ var import_node_path = __toESM(require("path"), 1);
43
+ var import_node_util = require("util");
44
+
45
+ // src/models.ts
46
+ function snakeToCamel(str) {
47
+ return str.replace(/_([a-z])/g, (_, ch) => ch.toUpperCase());
48
+ }
49
+ function parseTimestamp(v) {
50
+ if (v == null) return void 0;
51
+ if (v instanceof Date) return v;
52
+ if (typeof v === "string") {
53
+ const parsed = Date.parse(v);
54
+ return Number.isNaN(parsed) ? void 0 : new Date(parsed);
55
+ }
56
+ const ts = Number(v);
57
+ if (isNaN(ts)) return void 0;
58
+ if (ts > 1e15) return new Date(ts / 1e3);
59
+ if (ts > 1e12) return new Date(ts);
60
+ return new Date(ts * 1e3);
61
+ }
62
+ function fromSnakeKeys(obj, idField) {
63
+ if (Array.isArray(obj)) return obj.map((item) => fromSnakeKeys(item, idField));
64
+ if (obj !== null && typeof obj === "object" && !(obj instanceof Date)) {
65
+ const result = {};
66
+ for (const [k, v] of Object.entries(obj)) {
67
+ let key;
68
+ if (k === "id" && idField) {
69
+ key = idField;
70
+ } else {
71
+ key = snakeToCamel(k);
72
+ }
73
+ if (key.endsWith("At") || key === "timestamp" || key === "startedAt" || key === "endedAt") {
74
+ result[key] = parseTimestamp(v);
75
+ } else if (typeof v === "object" && v !== null && !Array.isArray(v)) {
76
+ result[key] = fromSnakeKeys(v);
77
+ } else if (Array.isArray(v)) {
78
+ result[key] = v.map((item) => fromSnakeKeys(item));
79
+ } else {
80
+ result[key] = v === null ? void 0 : v;
81
+ }
82
+ }
83
+ return result;
84
+ }
85
+ return obj;
86
+ }
87
+
88
+ // src/defaults.ts
89
+ var API_URL = process.env.TENSORLAKE_API_URL ?? "https://api.tensorlake.ai";
90
+ var API_KEY = process.env.TENSORLAKE_API_KEY ?? void 0;
91
+ var NAMESPACE = process.env.INDEXIFY_NAMESPACE ?? "default";
92
+ var SANDBOX_PROXY_URL = process.env.TENSORLAKE_SANDBOX_PROXY_URL ?? "https://sandbox.tensorlake.ai";
93
+ var DEFAULT_HTTP_TIMEOUT_MS = 3e4;
94
+ var MAX_RETRIES = 3;
95
+ var RETRY_BACKOFF_MS = 500;
96
+ var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([429, 502, 503, 504]);
97
+
98
+ // src/errors.ts
99
+ var SandboxException = class extends Error {
100
+ constructor(message) {
101
+ super(message);
102
+ this.name = "SandboxException";
103
+ }
104
+ };
105
+ var SandboxError = class extends SandboxException {
106
+ constructor(message) {
107
+ super(message);
108
+ this.name = "SandboxError";
109
+ }
110
+ };
111
+ var SandboxConnectionError = class extends SandboxError {
112
+ constructor(message) {
113
+ super(`Connection error: ${message}`);
114
+ this.name = "SandboxConnectionError";
115
+ }
116
+ };
117
+ var SandboxNotFoundError = class extends SandboxError {
118
+ sandboxId;
119
+ constructor(sandboxId) {
120
+ super(`Sandbox not found: ${sandboxId}`);
121
+ this.name = "SandboxNotFoundError";
122
+ this.sandboxId = sandboxId;
123
+ }
124
+ };
125
+ var PoolNotFoundError = class extends SandboxError {
126
+ poolId;
127
+ constructor(poolId) {
128
+ super(`Sandbox pool not found: ${poolId}`);
129
+ this.name = "PoolNotFoundError";
130
+ this.poolId = poolId;
131
+ }
132
+ };
133
+ var PoolInUseError = class extends SandboxError {
134
+ poolId;
135
+ constructor(poolId, message) {
136
+ const base = `Cannot delete pool ${poolId}: pool is in use`;
137
+ super(message ? `${base} - ${message}` : base);
138
+ this.name = "PoolInUseError";
139
+ this.poolId = poolId;
140
+ }
141
+ };
142
+ var RemoteAPIError = class extends SandboxError {
143
+ statusCode;
144
+ responseMessage;
145
+ constructor(statusCode, message) {
146
+ super(`API error (status ${statusCode}): ${message}`);
147
+ this.name = "RemoteAPIError";
148
+ this.statusCode = statusCode;
149
+ this.responseMessage = message;
150
+ }
151
+ };
152
+
153
+ // src/http.ts
154
+ var HttpClient = class {
155
+ baseUrl;
156
+ headers;
157
+ maxRetries;
158
+ retryBackoffMs;
159
+ timeoutMs;
160
+ abortController = null;
161
+ constructor(options) {
162
+ this.baseUrl = options.baseUrl.replace(/\/+$/, "");
163
+ this.maxRetries = options.maxRetries ?? MAX_RETRIES;
164
+ this.retryBackoffMs = options.retryBackoffMs ?? RETRY_BACKOFF_MS;
165
+ this.timeoutMs = options.timeoutMs ?? DEFAULT_HTTP_TIMEOUT_MS;
166
+ this.headers = {};
167
+ if (options.apiKey) {
168
+ this.headers["Authorization"] = `Bearer ${options.apiKey}`;
169
+ }
170
+ if (options.organizationId) {
171
+ this.headers["X-Forwarded-Organization-Id"] = options.organizationId;
172
+ }
173
+ if (options.projectId) {
174
+ this.headers["X-Forwarded-Project-Id"] = options.projectId;
175
+ }
176
+ if (options.hostHeader) {
177
+ this.headers["Host"] = options.hostHeader;
178
+ }
179
+ }
180
+ close() {
181
+ this.abortController?.abort();
182
+ this.abortController = null;
183
+ }
184
+ /** Make a JSON request, returning the parsed response body. */
185
+ async requestJson(method, path2, options) {
186
+ const response = await this.requestResponse(method, path2, {
187
+ json: options?.body,
188
+ headers: options?.headers,
189
+ signal: options?.signal
190
+ });
191
+ const text = await response.text();
192
+ if (!text) return void 0;
193
+ return JSON.parse(text);
194
+ }
195
+ /** Make a request returning raw bytes. */
196
+ async requestBytes(method, path2, options) {
197
+ const headers = { ...options?.headers ?? {} };
198
+ if (options?.contentType) {
199
+ headers["Content-Type"] = options.contentType;
200
+ }
201
+ const response = await this.requestResponse(
202
+ method,
203
+ path2,
204
+ {
205
+ body: options?.body,
206
+ headers,
207
+ signal: options?.signal
208
+ }
209
+ );
210
+ const buffer = await response.arrayBuffer();
211
+ return new Uint8Array(buffer);
212
+ }
213
+ /** Make a request and return the raw Response (for SSE streaming). */
214
+ async requestStream(method, path2, options) {
215
+ const response = await this.requestResponse(
216
+ method,
217
+ path2,
218
+ {
219
+ headers: { Accept: "text/event-stream" },
220
+ signal: options?.signal
221
+ }
222
+ );
223
+ if (!response.body) {
224
+ throw new RemoteAPIError(response.status, "No response body for SSE stream");
225
+ }
226
+ return response.body;
227
+ }
228
+ /** Make a request and return the raw Response. */
229
+ async requestResponse(method, path2, options) {
230
+ const headers = {
231
+ ...this.headers,
232
+ ...options?.headers ?? {}
233
+ };
234
+ const hasJsonBody = options?.json !== void 0;
235
+ if (hasJsonBody && !hasHeader(headers, "Content-Type")) {
236
+ headers["Content-Type"] = "application/json";
237
+ }
238
+ const body = hasJsonBody ? JSON.stringify(options?.json) : normalizeRequestBody(options?.body);
239
+ return this.doFetch(
240
+ method,
241
+ path2,
242
+ body,
243
+ headers,
244
+ options?.signal,
245
+ options?.allowHttpErrors ?? false
246
+ );
247
+ }
248
+ async doFetch(method, path2, body, headers, signal, allowHttpErrors = false) {
249
+ const url = `${this.baseUrl}${path2}`;
250
+ let lastError;
251
+ for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
252
+ if (attempt > 0) {
253
+ const delay = this.retryBackoffMs * Math.pow(2, attempt - 1);
254
+ await sleep(delay);
255
+ }
256
+ this.abortController = new AbortController();
257
+ const timeoutId = setTimeout(
258
+ () => this.abortController?.abort(),
259
+ this.timeoutMs
260
+ );
261
+ const combinedSignal = signal ? anySignal([signal, this.abortController.signal]) : this.abortController.signal;
262
+ try {
263
+ const response = await fetch(url, {
264
+ method,
265
+ headers,
266
+ body,
267
+ signal: combinedSignal
268
+ });
269
+ clearTimeout(timeoutId);
270
+ if (response.ok) return response;
271
+ if (RETRYABLE_STATUS_CODES.has(response.status) && attempt < this.maxRetries) {
272
+ lastError = new RemoteAPIError(
273
+ response.status,
274
+ await response.text().catch(() => "")
275
+ );
276
+ continue;
277
+ }
278
+ if (allowHttpErrors) {
279
+ return response;
280
+ }
281
+ const errorBody = await response.text().catch(() => "");
282
+ throwMappedError(response.status, errorBody, path2);
283
+ } catch (err) {
284
+ clearTimeout(timeoutId);
285
+ if (err instanceof RemoteAPIError || err instanceof SandboxNotFoundError || err instanceof PoolNotFoundError || err instanceof PoolInUseError) {
286
+ throw err;
287
+ }
288
+ if (signal?.aborted) {
289
+ throw new SandboxConnectionError("Request aborted");
290
+ }
291
+ lastError = err instanceof Error ? err : new Error(String(err));
292
+ if (attempt >= this.maxRetries) {
293
+ throw new SandboxConnectionError(lastError.message);
294
+ }
295
+ }
296
+ }
297
+ throw new SandboxConnectionError(lastError?.message ?? "Request failed");
298
+ }
299
+ };
300
+ function hasHeader(headers, name) {
301
+ const lowered = name.toLowerCase();
302
+ return Object.keys(headers).some((key) => key.toLowerCase() === lowered);
303
+ }
304
+ function normalizeRequestBody(body) {
305
+ if (body == null) {
306
+ return void 0;
307
+ }
308
+ if (body instanceof Uint8Array) {
309
+ return Uint8Array.from(body).buffer;
310
+ }
311
+ return body;
312
+ }
313
+ function throwMappedError(status, body, path2) {
314
+ let message = body;
315
+ try {
316
+ const parsed = JSON.parse(body);
317
+ if (parsed.message) message = parsed.message;
318
+ else if (parsed.error) message = parsed.error;
319
+ } catch {
320
+ }
321
+ if (status === 404) {
322
+ if (path2.includes("sandbox-pools") || path2.includes("pools")) {
323
+ const match = path2.match(/sandbox-pools\/([^/]+)/);
324
+ if (match) throw new PoolNotFoundError(match[1]);
325
+ }
326
+ if (path2.includes("sandboxes")) {
327
+ const match = path2.match(/sandboxes\/([^/]+)/);
328
+ if (match) throw new SandboxNotFoundError(match[1]);
329
+ }
330
+ throw new RemoteAPIError(404, message);
331
+ }
332
+ if (status === 409) {
333
+ if (path2.includes("sandbox-pools") || path2.includes("pools")) {
334
+ const match = path2.match(/sandbox-pools\/([^/]+)/);
335
+ if (match) throw new PoolInUseError(match[1], message);
336
+ }
337
+ }
338
+ throw new RemoteAPIError(status, message);
339
+ }
340
+ function anySignal(signals) {
341
+ const controller = new AbortController();
342
+ for (const signal of signals) {
343
+ if (signal.aborted) {
344
+ controller.abort(signal.reason);
345
+ return controller.signal;
346
+ }
347
+ signal.addEventListener("abort", () => controller.abort(signal.reason), {
348
+ once: true
349
+ });
350
+ }
351
+ return controller.signal;
352
+ }
353
+ function sleep(ms) {
354
+ return new Promise((resolve) => setTimeout(resolve, ms));
355
+ }
356
+
357
+ // src/sse.ts
358
+ async function* parseSSEMessages(stream, signal) {
359
+ const reader = stream.getReader();
360
+ const decoder = new TextDecoder();
361
+ let buffer = "";
362
+ try {
363
+ while (true) {
364
+ if (signal?.aborted) break;
365
+ const { done, value } = await reader.read();
366
+ if (done) break;
367
+ buffer += decoder.decode(value, { stream: true });
368
+ const parts = buffer.split(/\r?\n\r?\n/);
369
+ buffer = parts.pop() ?? "";
370
+ for (const part of parts) {
371
+ const lines = part.split(/\r?\n/);
372
+ const dataLines = [];
373
+ let event;
374
+ let id;
375
+ for (const line of lines) {
376
+ if (!line || line.startsWith(":")) continue;
377
+ const separator = line.indexOf(":");
378
+ const field = separator === -1 ? line : line.slice(0, separator);
379
+ let value2 = separator === -1 ? "" : line.slice(separator + 1);
380
+ if (value2.startsWith(" ")) {
381
+ value2 = value2.slice(1);
382
+ }
383
+ if (field === "data") {
384
+ dataLines.push(value2);
385
+ } else if (field === "event") {
386
+ event = value2;
387
+ } else if (field === "id") {
388
+ id = value2;
389
+ }
390
+ }
391
+ if (dataLines.length > 0 || event || id) {
392
+ yield { data: dataLines.join("\n"), event, id };
393
+ }
394
+ }
395
+ }
396
+ } finally {
397
+ reader.releaseLock();
398
+ }
399
+ }
400
+ async function* parseSSEStream(stream, signal) {
401
+ for await (const message of parseSSEMessages(stream, signal)) {
402
+ if (!message.data) continue;
403
+ try {
404
+ yield JSON.parse(message.data);
405
+ } catch {
406
+ }
407
+ }
408
+ }
409
+
410
+ // src/url.ts
411
+ function isLocalhost(apiUrl) {
412
+ try {
413
+ const parsed = new URL(apiUrl);
414
+ return parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1";
415
+ } catch {
416
+ return false;
417
+ }
418
+ }
419
+ function resolveProxyUrl(apiUrl) {
420
+ const explicit = process.env.TENSORLAKE_SANDBOX_PROXY_URL;
421
+ if (explicit) return explicit;
422
+ if (isLocalhost(apiUrl)) return "http://localhost:9443";
423
+ try {
424
+ const parsed = new URL(apiUrl);
425
+ const host = parsed.hostname;
426
+ if (host.startsWith("api.")) {
427
+ const proxyHost = "sandbox." + host.slice(4);
428
+ return `${parsed.protocol}//${proxyHost}`;
429
+ }
430
+ } catch {
431
+ }
432
+ return SANDBOX_PROXY_URL;
433
+ }
434
+ function resolveProxyTarget(proxyUrl, sandboxId) {
435
+ try {
436
+ const parsed = new URL(proxyUrl);
437
+ const host = parsed.hostname;
438
+ if (host === "localhost" || host === "127.0.0.1") {
439
+ return {
440
+ baseUrl: proxyUrl.replace(/\/+$/, ""),
441
+ hostHeader: `${sandboxId}.local`
442
+ };
443
+ }
444
+ const port = parsed.port ? `:${parsed.port}` : "";
445
+ return {
446
+ baseUrl: `${parsed.protocol}//${sandboxId}.${host}${port}`,
447
+ hostHeader: void 0
448
+ };
449
+ } catch {
450
+ return {
451
+ baseUrl: `${proxyUrl.replace(/\/+$/, "")}/${sandboxId}`,
452
+ hostHeader: void 0
453
+ };
454
+ }
455
+ }
456
+ function lifecyclePath(path2, isLocal, namespace) {
457
+ if (isLocal) {
458
+ return `/v1/namespaces/${namespace}/${path2}`;
459
+ }
460
+ return `/${path2}`;
461
+ }
462
+
463
+ // src/sandbox.ts
464
+ var import_ws = __toESM(require("ws"), 1);
465
+ var PTY_OP_DATA = 0;
466
+ var PTY_OP_RESIZE = 1;
467
+ var PTY_OP_READY = 2;
468
+ var PTY_OP_EXIT = 3;
469
+ var Pty = class {
470
+ sessionId;
471
+ token;
472
+ wsUrl;
473
+ wsHeaders;
474
+ killSession;
475
+ socket = null;
476
+ connectPromise = null;
477
+ intentionalDisconnect = false;
478
+ exitCode = null;
479
+ waitSettled = false;
480
+ dataHandlers = /* @__PURE__ */ new Set();
481
+ exitHandlers = /* @__PURE__ */ new Set();
482
+ waitPromise;
483
+ resolveWait;
484
+ rejectWait;
485
+ constructor(options) {
486
+ this.sessionId = options.sessionId;
487
+ this.token = options.token;
488
+ this.wsUrl = options.wsUrl;
489
+ this.wsHeaders = options.wsHeaders;
490
+ this.killSession = options.killSession;
491
+ this.waitPromise = new Promise((resolve, reject) => {
492
+ this.resolveWait = resolve;
493
+ this.rejectWait = reject;
494
+ });
495
+ }
496
+ onData(handler) {
497
+ this.dataHandlers.add(handler);
498
+ return () => this.dataHandlers.delete(handler);
499
+ }
500
+ onExit(handler) {
501
+ this.exitHandlers.add(handler);
502
+ if (this.exitCode != null) {
503
+ queueMicrotask(() => handler(this.exitCode));
504
+ }
505
+ return () => this.exitHandlers.delete(handler);
506
+ }
507
+ async connect() {
508
+ if (this.socket?.readyState === import_ws.default.OPEN) {
509
+ return this;
510
+ }
511
+ if (this.connectPromise) {
512
+ return this.connectPromise;
513
+ }
514
+ this.intentionalDisconnect = false;
515
+ this.connectPromise = new Promise((resolve, reject) => {
516
+ let opened = false;
517
+ const socket = new import_ws.default(this.wsUrl, {
518
+ headers: this.wsHeaders
519
+ });
520
+ this.socket = socket;
521
+ socket.on("open", async () => {
522
+ try {
523
+ await sendPtyFrame(socket, Buffer.from([PTY_OP_READY]));
524
+ opened = true;
525
+ resolve(this);
526
+ } catch (error) {
527
+ reject(error);
528
+ }
529
+ });
530
+ socket.on("message", (message) => {
531
+ const bytes = normalizePtyMessage(message);
532
+ const opcode = bytes[0];
533
+ if (opcode === PTY_OP_DATA) {
534
+ const payload = bytes.subarray(1);
535
+ for (const handler of this.dataHandlers) {
536
+ handler(payload);
537
+ }
538
+ return;
539
+ }
540
+ if (opcode === PTY_OP_EXIT && bytes.length >= 5) {
541
+ this.finishWait(bytes.readInt32BE(1));
542
+ }
543
+ });
544
+ socket.on("close", (code, reason) => {
545
+ const closeReason = Buffer.isBuffer(reason) ? reason.toString("utf8") : String(reason);
546
+ if (this.socket === socket) {
547
+ this.socket = null;
548
+ }
549
+ this.connectPromise = null;
550
+ if (this.exitCode != null) {
551
+ this.finishWait(this.exitCode);
552
+ return;
553
+ }
554
+ if (closeReason.startsWith("exit:")) {
555
+ const parsed = Number.parseInt(closeReason.slice(5), 10);
556
+ this.finishWait(Number.isNaN(parsed) ? -1 : parsed);
557
+ return;
558
+ }
559
+ if (this.intentionalDisconnect) {
560
+ this.intentionalDisconnect = false;
561
+ return;
562
+ }
563
+ if (!opened) {
564
+ reject(new SandboxError(
565
+ `PTY websocket closed before READY completed: ${code} ${closeReason || "no reason"}`
566
+ ));
567
+ return;
568
+ }
569
+ if (closeReason === "session terminated") {
570
+ this.failWait(new SandboxError("PTY session terminated"));
571
+ return;
572
+ }
573
+ this.failWait(
574
+ new SandboxError(
575
+ `PTY websocket closed unexpectedly: ${code} ${closeReason || "no reason"}`
576
+ )
577
+ );
578
+ });
579
+ socket.on("error", (error) => {
580
+ if (!opened) {
581
+ reject(error);
582
+ }
583
+ });
584
+ });
585
+ return this.connectPromise;
586
+ }
587
+ async sendInput(input) {
588
+ const socket = this.requireOpenSocket();
589
+ await sendPtyFrame(socket, encodePtyInput(input));
590
+ }
591
+ async resize(cols, rows) {
592
+ const socket = this.requireOpenSocket();
593
+ await sendPtyFrame(socket, encodePtyResize(cols, rows));
594
+ }
595
+ disconnect(code = 1e3, reason = "client disconnect") {
596
+ if (!this.socket) return;
597
+ this.intentionalDisconnect = true;
598
+ this.socket.close(code, reason);
599
+ }
600
+ wait() {
601
+ return this.waitPromise;
602
+ }
603
+ async kill() {
604
+ await this.killSession();
605
+ }
606
+ requireOpenSocket() {
607
+ if (!this.socket || this.socket.readyState !== import_ws.default.OPEN) {
608
+ throw new SandboxError("PTY is not connected");
609
+ }
610
+ return this.socket;
611
+ }
612
+ finishWait(exitCode) {
613
+ if (this.waitSettled) return;
614
+ this.waitSettled = true;
615
+ this.exitCode = exitCode;
616
+ for (const handler of this.exitHandlers) {
617
+ handler(exitCode);
618
+ }
619
+ this.resolveWait(exitCode);
620
+ }
621
+ failWait(error) {
622
+ if (this.waitSettled) return;
623
+ this.waitSettled = true;
624
+ this.rejectWait(error);
625
+ }
626
+ };
627
+ function normalizePtyMessage(message) {
628
+ if (Buffer.isBuffer(message)) return message;
629
+ if (Array.isArray(message)) {
630
+ return Buffer.concat(message.map((part) => Buffer.from(part)));
631
+ }
632
+ return Buffer.from(message);
633
+ }
634
+ function encodePtyInput(input) {
635
+ const payload = typeof input === "string" ? Buffer.from(input, "utf8") : Buffer.from(input);
636
+ return Buffer.concat([Buffer.from([PTY_OP_DATA]), payload]);
637
+ }
638
+ function encodePtyResize(cols, rows) {
639
+ const frame = Buffer.alloc(5);
640
+ frame[0] = PTY_OP_RESIZE;
641
+ frame.writeUInt16BE(cols, 1);
642
+ frame.writeUInt16BE(rows, 3);
643
+ return frame;
644
+ }
645
+ function sendPtyFrame(socket, frame) {
646
+ return new Promise((resolve, reject) => {
647
+ socket.send(frame, (error) => error ? reject(error) : resolve());
648
+ });
649
+ }
650
+ var Sandbox = class {
651
+ sandboxId;
652
+ http;
653
+ baseUrl;
654
+ wsHeaders;
655
+ ownsSandbox = false;
656
+ lifecycleClient = null;
657
+ constructor(options) {
658
+ this.sandboxId = options.sandboxId;
659
+ const proxyUrl = options.proxyUrl ?? SANDBOX_PROXY_URL;
660
+ const { baseUrl, hostHeader } = resolveProxyTarget(proxyUrl, options.sandboxId);
661
+ this.baseUrl = baseUrl;
662
+ this.wsHeaders = {};
663
+ if (options.apiKey) {
664
+ this.wsHeaders.Authorization = `Bearer ${options.apiKey}`;
665
+ }
666
+ if (options.organizationId) {
667
+ this.wsHeaders["X-Forwarded-Organization-Id"] = options.organizationId;
668
+ }
669
+ if (options.projectId) {
670
+ this.wsHeaders["X-Forwarded-Project-Id"] = options.projectId;
671
+ }
672
+ if (hostHeader) {
673
+ this.wsHeaders.Host = hostHeader;
674
+ }
675
+ this.http = new HttpClient({
676
+ baseUrl,
677
+ apiKey: options.apiKey,
678
+ organizationId: options.organizationId,
679
+ projectId: options.projectId,
680
+ hostHeader
681
+ });
682
+ }
683
+ /** @internal Used by SandboxClient.createAndConnect to set ownership. */
684
+ _setOwner(client) {
685
+ this.ownsSandbox = true;
686
+ this.lifecycleClient = client;
687
+ }
688
+ close() {
689
+ this.http.close();
690
+ }
691
+ async terminate() {
692
+ const client = this.lifecycleClient;
693
+ this.ownsSandbox = false;
694
+ this.lifecycleClient = null;
695
+ this.close();
696
+ if (client) {
697
+ await client.delete(this.sandboxId);
698
+ }
699
+ }
700
+ // --- High-level convenience ---
701
+ async run(command, options) {
702
+ const proc = await this.startProcess(command, {
703
+ args: options?.args,
704
+ env: options?.env,
705
+ workingDir: options?.workingDir
706
+ });
707
+ const deadline = options?.timeout ? Date.now() + options.timeout * 1e3 : null;
708
+ let info;
709
+ while (true) {
710
+ info = await this.getProcess(proc.pid);
711
+ if (info.status !== "running" /* RUNNING */) break;
712
+ if (deadline && Date.now() > deadline) {
713
+ await this.killProcess(proc.pid);
714
+ throw new SandboxError(`Command timed out after ${options.timeout}s`);
715
+ }
716
+ await sleep2(100);
717
+ }
718
+ const stdoutResp = await this.getStdout(proc.pid);
719
+ const stderrResp = await this.getStderr(proc.pid);
720
+ let exitCode;
721
+ if (info.exitCode != null) {
722
+ exitCode = info.exitCode;
723
+ } else if (info.signal != null) {
724
+ exitCode = -info.signal;
725
+ } else {
726
+ exitCode = -1;
727
+ }
728
+ return {
729
+ exitCode,
730
+ stdout: stdoutResp.lines.join("\n"),
731
+ stderr: stderrResp.lines.join("\n")
732
+ };
733
+ }
734
+ // --- Process management ---
735
+ async startProcess(command, options) {
736
+ const payload = { command };
737
+ if (options?.args != null) payload.args = options.args;
738
+ if (options?.env != null) payload.env = options.env;
739
+ if (options?.workingDir != null) payload.working_dir = options.workingDir;
740
+ if (options?.stdinMode != null && options.stdinMode !== "closed" /* CLOSED */) {
741
+ payload.stdin_mode = options.stdinMode;
742
+ }
743
+ if (options?.stdoutMode != null && options.stdoutMode !== "capture" /* CAPTURE */) {
744
+ payload.stdout_mode = options.stdoutMode;
745
+ }
746
+ if (options?.stderrMode != null && options.stderrMode !== "capture" /* CAPTURE */) {
747
+ payload.stderr_mode = options.stderrMode;
748
+ }
749
+ const raw = await this.http.requestJson(
750
+ "POST",
751
+ "/api/v1/processes",
752
+ { body: payload }
753
+ );
754
+ return fromSnakeKeys(raw);
755
+ }
756
+ async listProcesses() {
757
+ const raw = await this.http.requestJson(
758
+ "GET",
759
+ "/api/v1/processes"
760
+ );
761
+ return (raw.processes ?? []).map((p) => fromSnakeKeys(p));
762
+ }
763
+ async getProcess(pid) {
764
+ const raw = await this.http.requestJson(
765
+ "GET",
766
+ `/api/v1/processes/${pid}`
767
+ );
768
+ return fromSnakeKeys(raw);
769
+ }
770
+ async killProcess(pid) {
771
+ await this.http.requestJson("DELETE", `/api/v1/processes/${pid}`);
772
+ }
773
+ async sendSignal(pid, signal) {
774
+ const raw = await this.http.requestJson(
775
+ "POST",
776
+ `/api/v1/processes/${pid}/signal`,
777
+ { body: { signal } }
778
+ );
779
+ return fromSnakeKeys(raw);
780
+ }
781
+ // --- Process I/O ---
782
+ async writeStdin(pid, data) {
783
+ await this.http.requestBytes("POST", `/api/v1/processes/${pid}/stdin`, {
784
+ body: data,
785
+ contentType: "application/octet-stream"
786
+ });
787
+ }
788
+ async closeStdin(pid) {
789
+ await this.http.requestJson("POST", `/api/v1/processes/${pid}/stdin/close`);
790
+ }
791
+ async getStdout(pid) {
792
+ const raw = await this.http.requestJson(
793
+ "GET",
794
+ `/api/v1/processes/${pid}/stdout`
795
+ );
796
+ return fromSnakeKeys(raw);
797
+ }
798
+ async getStderr(pid) {
799
+ const raw = await this.http.requestJson(
800
+ "GET",
801
+ `/api/v1/processes/${pid}/stderr`
802
+ );
803
+ return fromSnakeKeys(raw);
804
+ }
805
+ async getOutput(pid) {
806
+ const raw = await this.http.requestJson(
807
+ "GET",
808
+ `/api/v1/processes/${pid}/output`
809
+ );
810
+ return fromSnakeKeys(raw);
811
+ }
812
+ // --- Streaming (SSE) ---
813
+ async *followStdout(pid, options) {
814
+ const stream = await this.http.requestStream(
815
+ "GET",
816
+ `/api/v1/processes/${pid}/stdout/follow`,
817
+ options
818
+ );
819
+ for await (const raw of parseSSEStream(
820
+ stream,
821
+ options?.signal
822
+ )) {
823
+ yield fromSnakeKeys(raw);
824
+ }
825
+ }
826
+ async *followStderr(pid, options) {
827
+ const stream = await this.http.requestStream(
828
+ "GET",
829
+ `/api/v1/processes/${pid}/stderr/follow`,
830
+ options
831
+ );
832
+ for await (const raw of parseSSEStream(
833
+ stream,
834
+ options?.signal
835
+ )) {
836
+ yield fromSnakeKeys(raw);
837
+ }
838
+ }
839
+ async *followOutput(pid, options) {
840
+ const stream = await this.http.requestStream(
841
+ "GET",
842
+ `/api/v1/processes/${pid}/output/follow`,
843
+ options
844
+ );
845
+ for await (const raw of parseSSEStream(
846
+ stream,
847
+ options?.signal
848
+ )) {
849
+ yield fromSnakeKeys(raw);
850
+ }
851
+ }
852
+ // --- File operations ---
853
+ async readFile(path2) {
854
+ return this.http.requestBytes(
855
+ "GET",
856
+ `/api/v1/files?path=${encodeURIComponent(path2)}`
857
+ );
858
+ }
859
+ async writeFile(path2, content) {
860
+ await this.http.requestBytes(
861
+ "PUT",
862
+ `/api/v1/files?path=${encodeURIComponent(path2)}`,
863
+ { body: content, contentType: "application/octet-stream" }
864
+ );
865
+ }
866
+ async deleteFile(path2) {
867
+ await this.http.requestJson(
868
+ "DELETE",
869
+ `/api/v1/files?path=${encodeURIComponent(path2)}`
870
+ );
871
+ }
872
+ async listDirectory(path2) {
873
+ const raw = await this.http.requestJson(
874
+ "GET",
875
+ `/api/v1/files/list?path=${encodeURIComponent(path2)}`
876
+ );
877
+ return fromSnakeKeys(raw);
878
+ }
879
+ // --- PTY ---
880
+ async createPtySession(options) {
881
+ const payload = {
882
+ command: options.command,
883
+ rows: options.rows ?? 24,
884
+ cols: options.cols ?? 80
885
+ };
886
+ if (options.args != null) payload.args = options.args;
887
+ if (options.env != null) payload.env = options.env;
888
+ if (options.workingDir != null) payload.working_dir = options.workingDir;
889
+ const raw = await this.http.requestJson(
890
+ "POST",
891
+ "/api/v1/pty",
892
+ { body: payload }
893
+ );
894
+ return fromSnakeKeys(raw);
895
+ }
896
+ async createPty(options) {
897
+ const { onData, onExit, ...createOptions } = options;
898
+ const session = await this.createPtySession(createOptions);
899
+ try {
900
+ return await this.connectPty(session.sessionId, session.token, { onData, onExit });
901
+ } catch (error) {
902
+ try {
903
+ await this.http.requestResponse("DELETE", `/api/v1/pty/${session.sessionId}`);
904
+ } catch {
905
+ }
906
+ throw error;
907
+ }
908
+ }
909
+ async connectPty(sessionId, token, options) {
910
+ const wsUrl = new URL(this.ptyWsUrl(sessionId, token));
911
+ const authToken = wsUrl.searchParams.get("token") ?? token;
912
+ const pty = new Pty({
913
+ sessionId,
914
+ token: authToken,
915
+ wsUrl: wsUrl.toString(),
916
+ wsHeaders: {
917
+ ...this.wsHeaders,
918
+ "X-PTY-Token": authToken
919
+ },
920
+ killSession: async () => {
921
+ await this.http.requestResponse("DELETE", `/api/v1/pty/${sessionId}`);
922
+ }
923
+ });
924
+ if (options?.onData) {
925
+ pty.onData(options.onData);
926
+ }
927
+ if (options?.onExit) {
928
+ pty.onExit(options.onExit);
929
+ }
930
+ await pty.connect();
931
+ return pty;
932
+ }
933
+ ptyWsUrl(sessionId, token) {
934
+ let wsBase;
935
+ if (this.baseUrl.startsWith("https://")) {
936
+ wsBase = "wss://" + this.baseUrl.slice(8);
937
+ } else if (this.baseUrl.startsWith("http://")) {
938
+ wsBase = "ws://" + this.baseUrl.slice(7);
939
+ } else {
940
+ wsBase = this.baseUrl;
941
+ }
942
+ return `${wsBase}/api/v1/pty/${sessionId}/ws?token=${token}`;
943
+ }
944
+ // --- Health ---
945
+ async health() {
946
+ const raw = await this.http.requestJson(
947
+ "GET",
948
+ "/api/v1/health"
949
+ );
950
+ return fromSnakeKeys(raw);
951
+ }
952
+ async info() {
953
+ const raw = await this.http.requestJson(
954
+ "GET",
955
+ "/api/v1/info"
956
+ );
957
+ return fromSnakeKeys(raw);
958
+ }
959
+ };
960
+ function sleep2(ms) {
961
+ return new Promise((resolve) => setTimeout(resolve, ms));
962
+ }
963
+
964
+ // src/client.ts
965
+ var SandboxClient = class _SandboxClient {
966
+ http;
967
+ apiUrl;
968
+ apiKey;
969
+ organizationId;
970
+ projectId;
971
+ namespace;
972
+ local;
973
+ constructor(options) {
974
+ this.apiUrl = options?.apiUrl ?? API_URL;
975
+ this.apiKey = options?.apiKey ?? API_KEY;
976
+ this.organizationId = options?.organizationId;
977
+ this.projectId = options?.projectId;
978
+ this.namespace = options?.namespace ?? NAMESPACE;
979
+ this.local = isLocalhost(this.apiUrl);
980
+ this.http = new HttpClient({
981
+ baseUrl: this.apiUrl,
982
+ apiKey: this.apiKey,
983
+ organizationId: this.organizationId,
984
+ projectId: this.projectId,
985
+ maxRetries: options?.maxRetries ?? MAX_RETRIES,
986
+ retryBackoffMs: options?.retryBackoffMs ?? RETRY_BACKOFF_MS
987
+ });
988
+ }
989
+ /** Create a client for the TensorLake cloud platform. */
990
+ static forCloud(options) {
991
+ return new _SandboxClient({
992
+ apiUrl: options?.apiUrl ?? "https://api.tensorlake.ai",
993
+ apiKey: options?.apiKey,
994
+ organizationId: options?.organizationId,
995
+ projectId: options?.projectId
996
+ });
997
+ }
998
+ /** Create a client for a local Indexify server. */
999
+ static forLocalhost(options) {
1000
+ return new _SandboxClient({
1001
+ apiUrl: options?.apiUrl ?? "http://localhost:8900",
1002
+ namespace: options?.namespace ?? "default"
1003
+ });
1004
+ }
1005
+ close() {
1006
+ this.http.close();
1007
+ }
1008
+ // --- Path helper ---
1009
+ path(subpath) {
1010
+ return lifecyclePath(subpath, this.local, this.namespace);
1011
+ }
1012
+ // --- Sandbox CRUD ---
1013
+ async create(options) {
1014
+ const body = {
1015
+ resources: {
1016
+ cpus: options?.cpus ?? 1,
1017
+ memory_mb: options?.memoryMb ?? 1024,
1018
+ ephemeral_disk_mb: options?.ephemeralDiskMb ?? 1024
1019
+ }
1020
+ };
1021
+ if (options?.image != null) body.image = options.image;
1022
+ if (options?.secretNames != null) body.secret_names = options.secretNames;
1023
+ if (options?.timeoutSecs != null) body.timeout_secs = options.timeoutSecs;
1024
+ if (options?.entrypoint != null) body.entrypoint = options.entrypoint;
1025
+ if (options?.snapshotId != null) body.snapshot_id = options.snapshotId;
1026
+ if (options?.name != null) body.name = options.name;
1027
+ if (options?.allowInternetAccess === false || options?.allowOut != null || options?.denyOut != null) {
1028
+ body.network = {
1029
+ allow_internet_access: options?.allowInternetAccess ?? true,
1030
+ allow_out: options?.allowOut ?? [],
1031
+ deny_out: options?.denyOut ?? []
1032
+ };
1033
+ }
1034
+ const raw = await this.http.requestJson(
1035
+ "POST",
1036
+ this.path("sandboxes"),
1037
+ { body }
1038
+ );
1039
+ return fromSnakeKeys(raw, "sandboxId");
1040
+ }
1041
+ async get(sandboxId) {
1042
+ const raw = await this.http.requestJson(
1043
+ "GET",
1044
+ this.path(`sandboxes/${sandboxId}`)
1045
+ );
1046
+ return fromSnakeKeys(raw, "sandboxId");
1047
+ }
1048
+ async list() {
1049
+ const raw = await this.http.requestJson(
1050
+ "GET",
1051
+ this.path("sandboxes")
1052
+ );
1053
+ return (raw.sandboxes ?? []).map(
1054
+ (s) => fromSnakeKeys(s, "sandboxId")
1055
+ );
1056
+ }
1057
+ async update(sandboxId, options) {
1058
+ const body = {};
1059
+ if (options.name != null) body.name = options.name;
1060
+ if (options.allowUnauthenticatedAccess != null) {
1061
+ body.allow_unauthenticated_access = options.allowUnauthenticatedAccess;
1062
+ }
1063
+ if (options.exposedPorts != null) {
1064
+ body.exposed_ports = normalizeUserPorts(options.exposedPorts);
1065
+ }
1066
+ if (Object.keys(body).length === 0) {
1067
+ throw new SandboxError("At least one sandbox update field must be provided.");
1068
+ }
1069
+ const raw = await this.http.requestJson(
1070
+ "PATCH",
1071
+ this.path(`sandboxes/${sandboxId}`),
1072
+ { body }
1073
+ );
1074
+ return fromSnakeKeys(raw, "sandboxId");
1075
+ }
1076
+ async getPortAccess(sandboxId) {
1077
+ const info = await this.get(sandboxId);
1078
+ return {
1079
+ allowUnauthenticatedAccess: info.allowUnauthenticatedAccess ?? false,
1080
+ exposedPorts: dedupeAndSortPorts(info.exposedPorts ?? []),
1081
+ sandboxUrl: info.sandboxUrl
1082
+ };
1083
+ }
1084
+ async exposePorts(sandboxId, ports, options) {
1085
+ const requestedPorts = normalizeUserPorts(ports);
1086
+ const current = await this.getPortAccess(sandboxId);
1087
+ const desiredPorts = dedupeAndSortPorts([
1088
+ ...current.exposedPorts,
1089
+ ...requestedPorts
1090
+ ]);
1091
+ return this.update(sandboxId, {
1092
+ allowUnauthenticatedAccess: options?.allowUnauthenticatedAccess ?? current.allowUnauthenticatedAccess,
1093
+ exposedPorts: desiredPorts
1094
+ });
1095
+ }
1096
+ async unexposePorts(sandboxId, ports) {
1097
+ const requestedPorts = normalizeUserPorts(ports);
1098
+ const current = await this.getPortAccess(sandboxId);
1099
+ const toRemove = new Set(requestedPorts);
1100
+ const desiredPorts = current.exposedPorts.filter((port) => !toRemove.has(port));
1101
+ return this.update(sandboxId, {
1102
+ allowUnauthenticatedAccess: desiredPorts.length ? current.allowUnauthenticatedAccess : false,
1103
+ exposedPorts: desiredPorts
1104
+ });
1105
+ }
1106
+ async delete(sandboxId) {
1107
+ await this.http.requestJson(
1108
+ "DELETE",
1109
+ this.path(`sandboxes/${sandboxId}`)
1110
+ );
1111
+ }
1112
+ async suspend(sandboxId) {
1113
+ await this.http.requestResponse(
1114
+ "POST",
1115
+ this.path(`sandboxes/${sandboxId}/suspend`)
1116
+ );
1117
+ }
1118
+ async resume(sandboxId) {
1119
+ await this.http.requestResponse(
1120
+ "POST",
1121
+ this.path(`sandboxes/${sandboxId}/resume`)
1122
+ );
1123
+ }
1124
+ async claim(poolId) {
1125
+ const raw = await this.http.requestJson(
1126
+ "POST",
1127
+ this.path(`sandbox-pools/${poolId}/sandboxes`)
1128
+ );
1129
+ return fromSnakeKeys(raw, "sandboxId");
1130
+ }
1131
+ // --- Snapshots ---
1132
+ async snapshot(sandboxId) {
1133
+ const raw = await this.http.requestJson(
1134
+ "POST",
1135
+ this.path(`sandboxes/${sandboxId}/snapshot`)
1136
+ );
1137
+ return fromSnakeKeys(raw, "snapshotId");
1138
+ }
1139
+ async getSnapshot(snapshotId) {
1140
+ const raw = await this.http.requestJson(
1141
+ "GET",
1142
+ this.path(`snapshots/${snapshotId}`)
1143
+ );
1144
+ return fromSnakeKeys(raw, "snapshotId");
1145
+ }
1146
+ async listSnapshots() {
1147
+ const raw = await this.http.requestJson(
1148
+ "GET",
1149
+ this.path("snapshots")
1150
+ );
1151
+ return (raw.snapshots ?? []).map(
1152
+ (s) => fromSnakeKeys(s, "snapshotId")
1153
+ );
1154
+ }
1155
+ async deleteSnapshot(snapshotId) {
1156
+ await this.http.requestJson(
1157
+ "DELETE",
1158
+ this.path(`snapshots/${snapshotId}`)
1159
+ );
1160
+ }
1161
+ async snapshotAndWait(sandboxId, options) {
1162
+ const timeout = options?.timeout ?? 300;
1163
+ const pollInterval = options?.pollInterval ?? 1;
1164
+ const result = await this.snapshot(sandboxId);
1165
+ const deadline = Date.now() + timeout * 1e3;
1166
+ while (Date.now() < deadline) {
1167
+ const info = await this.getSnapshot(result.snapshotId);
1168
+ if (info.status === "completed" /* COMPLETED */) return info;
1169
+ if (info.status === "failed" /* FAILED */) {
1170
+ throw new SandboxError(
1171
+ `Snapshot ${result.snapshotId} failed: ${info.error}`
1172
+ );
1173
+ }
1174
+ await sleep3(pollInterval * 1e3);
1175
+ }
1176
+ throw new SandboxError(
1177
+ `Snapshot ${result.snapshotId} did not complete within ${timeout}s`
1178
+ );
1179
+ }
1180
+ // --- Pools ---
1181
+ async createPool(options) {
1182
+ const body = {
1183
+ image: options.image,
1184
+ resources: {
1185
+ cpus: options.cpus ?? 1,
1186
+ memory_mb: options.memoryMb ?? 1024,
1187
+ ephemeral_disk_mb: options.ephemeralDiskMb ?? 1024
1188
+ },
1189
+ timeout_secs: options.timeoutSecs ?? 0
1190
+ };
1191
+ if (options.secretNames != null) body.secret_names = options.secretNames;
1192
+ if (options.entrypoint != null) body.entrypoint = options.entrypoint;
1193
+ if (options.maxContainers != null) body.max_containers = options.maxContainers;
1194
+ if (options.warmContainers != null) body.warm_containers = options.warmContainers;
1195
+ const raw = await this.http.requestJson(
1196
+ "POST",
1197
+ this.path("sandbox-pools"),
1198
+ { body }
1199
+ );
1200
+ return fromSnakeKeys(raw, "poolId");
1201
+ }
1202
+ async getPool(poolId) {
1203
+ const raw = await this.http.requestJson(
1204
+ "GET",
1205
+ this.path(`sandbox-pools/${poolId}`)
1206
+ );
1207
+ return fromSnakeKeys(raw, "poolId");
1208
+ }
1209
+ async listPools() {
1210
+ const raw = await this.http.requestJson(
1211
+ "GET",
1212
+ this.path("sandbox-pools")
1213
+ );
1214
+ return (raw.pools ?? []).map(
1215
+ (p) => fromSnakeKeys(p, "poolId")
1216
+ );
1217
+ }
1218
+ async updatePool(poolId, options) {
1219
+ const body = {
1220
+ image: options.image,
1221
+ resources: {
1222
+ cpus: options.cpus ?? 1,
1223
+ memory_mb: options.memoryMb ?? 1024,
1224
+ ephemeral_disk_mb: options.ephemeralDiskMb ?? 1024
1225
+ },
1226
+ timeout_secs: options.timeoutSecs ?? 0
1227
+ };
1228
+ if (options.secretNames != null) body.secret_names = options.secretNames;
1229
+ if (options.entrypoint != null) body.entrypoint = options.entrypoint;
1230
+ if (options.maxContainers != null) body.max_containers = options.maxContainers;
1231
+ if (options.warmContainers != null) body.warm_containers = options.warmContainers;
1232
+ const raw = await this.http.requestJson(
1233
+ "PUT",
1234
+ this.path(`sandbox-pools/${poolId}`),
1235
+ { body }
1236
+ );
1237
+ return fromSnakeKeys(raw, "poolId");
1238
+ }
1239
+ async deletePool(poolId) {
1240
+ await this.http.requestJson(
1241
+ "DELETE",
1242
+ this.path(`sandbox-pools/${poolId}`)
1243
+ );
1244
+ }
1245
+ // --- Connect ---
1246
+ connect(sandboxId, proxyUrl) {
1247
+ const resolvedProxy = proxyUrl ?? resolveProxyUrl(this.apiUrl);
1248
+ return new Sandbox({
1249
+ sandboxId,
1250
+ proxyUrl: resolvedProxy,
1251
+ apiKey: this.apiKey,
1252
+ organizationId: this.organizationId,
1253
+ projectId: this.projectId
1254
+ });
1255
+ }
1256
+ async createAndConnect(options) {
1257
+ const startupTimeout = options?.startupTimeout ?? 60;
1258
+ let result;
1259
+ if (options?.poolId != null) {
1260
+ result = await this.claim(options.poolId);
1261
+ } else {
1262
+ result = await this.create(options);
1263
+ }
1264
+ const deadline = Date.now() + startupTimeout * 1e3;
1265
+ while (Date.now() < deadline) {
1266
+ const info = await this.get(result.sandboxId);
1267
+ if (info.status === "running" /* RUNNING */) {
1268
+ const sandbox = this.connect(result.sandboxId, options?.proxyUrl);
1269
+ sandbox._setOwner(this);
1270
+ return sandbox;
1271
+ }
1272
+ if (info.status === "terminated" /* TERMINATED */) {
1273
+ throw new SandboxError(
1274
+ `Sandbox ${result.sandboxId} terminated during startup`
1275
+ );
1276
+ }
1277
+ await sleep3(500);
1278
+ }
1279
+ try {
1280
+ await this.delete(result.sandboxId);
1281
+ } catch {
1282
+ }
1283
+ throw new SandboxError(
1284
+ `Sandbox ${result.sandboxId} did not start within ${startupTimeout}s`
1285
+ );
1286
+ }
1287
+ };
1288
+ function sleep3(ms) {
1289
+ return new Promise((resolve) => setTimeout(resolve, ms));
1290
+ }
1291
+ var RESERVED_SANDBOX_MANAGEMENT_PORT = 9501;
1292
+ function normalizeUserPorts(ports) {
1293
+ return dedupeAndSortPorts(ports.map(validateUserPort));
1294
+ }
1295
+ function validateUserPort(port) {
1296
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
1297
+ throw new SandboxError(`invalid port '${port}'`);
1298
+ }
1299
+ if (port === RESERVED_SANDBOX_MANAGEMENT_PORT) {
1300
+ throw new SandboxError("port 9501 is reserved for sandbox management");
1301
+ }
1302
+ return port;
1303
+ }
1304
+ function dedupeAndSortPorts(ports) {
1305
+ return [...new Set(ports)].sort((a, b) => a - b);
1306
+ }
1307
+
1308
+ // src/image.ts
1309
+ var import_node_crypto = require("crypto");
1310
+ var ImageBuildOperationType = {
1311
+ ADD: "ADD",
1312
+ COPY: "COPY",
1313
+ ENV: "ENV",
1314
+ RUN: "RUN",
1315
+ WORKDIR: "WORKDIR"
1316
+ };
1317
+ function renderOptions(options) {
1318
+ const entries = Object.entries(options);
1319
+ if (entries.length === 0) {
1320
+ return "";
1321
+ }
1322
+ return ` ${entries.map(([key, value]) => `--${key}=${value}`).join(" ")}`;
1323
+ }
1324
+ function renderBuildOp(op) {
1325
+ const options = renderOptions(op.options);
1326
+ if (op.type === ImageBuildOperationType.ENV) {
1327
+ return `ENV${options} ${op.args[0]}=${JSON.stringify(op.args[1])}`;
1328
+ }
1329
+ return `${op.type}${options} ${op.args.join(" ")}`;
1330
+ }
1331
+ function dockerfileContent(image) {
1332
+ const lines = image.baseImage == null ? [] : [`FROM ${image.baseImage}`];
1333
+ lines.push(...image.buildOperations.map((op) => renderBuildOp(op)));
1334
+ return lines.join("\n");
1335
+ }
1336
+
1337
+ // src/sandbox-image.ts
1338
+ var BUILD_SANDBOX_PIP_ENV = { PIP_BREAK_SYSTEM_PACKAGES: "1" };
1339
+ var IGNORED_DOCKERFILE_INSTRUCTIONS = /* @__PURE__ */ new Set([
1340
+ "CMD",
1341
+ "ENTRYPOINT",
1342
+ "EXPOSE",
1343
+ "HEALTHCHECK",
1344
+ "LABEL",
1345
+ "STOPSIGNAL",
1346
+ "VOLUME"
1347
+ ]);
1348
+ var UNSUPPORTED_DOCKERFILE_INSTRUCTIONS = /* @__PURE__ */ new Set([
1349
+ "ARG",
1350
+ "ONBUILD",
1351
+ "SHELL",
1352
+ "USER"
1353
+ ]);
1354
+ function defaultRegisteredName(dockerfilePath) {
1355
+ const parsed = import_node_path.default.parse(dockerfilePath);
1356
+ if (parsed.name.toLowerCase() === "dockerfile") {
1357
+ const parentName = import_node_path.default.basename(import_node_path.default.dirname(dockerfilePath)).trim();
1358
+ return parentName || "sandbox-image";
1359
+ }
1360
+ return parsed.name || "sandbox-image";
1361
+ }
1362
+ function logicalDockerfileLines(dockerfileText) {
1363
+ const logicalLines = [];
1364
+ let parts = [];
1365
+ let startLine = null;
1366
+ for (const [index, rawLine] of dockerfileText.split(/\r?\n/).entries()) {
1367
+ const lineNumber = index + 1;
1368
+ const stripped = rawLine.trim();
1369
+ if (parts.length === 0 && (!stripped || stripped.startsWith("#"))) {
1370
+ continue;
1371
+ }
1372
+ if (startLine == null) {
1373
+ startLine = lineNumber;
1374
+ }
1375
+ let line = rawLine.replace(/\s+$/, "");
1376
+ const continued = line.endsWith("\\");
1377
+ if (continued) {
1378
+ line = line.slice(0, -1);
1379
+ }
1380
+ const normalized = line.trim();
1381
+ if (normalized && !normalized.startsWith("#")) {
1382
+ parts.push(normalized);
1383
+ }
1384
+ if (continued) {
1385
+ continue;
1386
+ }
1387
+ if (parts.length > 0) {
1388
+ logicalLines.push({
1389
+ lineNumber: startLine ?? lineNumber,
1390
+ line: parts.join(" ")
1391
+ });
1392
+ }
1393
+ parts = [];
1394
+ startLine = null;
1395
+ }
1396
+ if (parts.length > 0) {
1397
+ logicalLines.push({
1398
+ lineNumber: startLine ?? 1,
1399
+ line: parts.join(" ")
1400
+ });
1401
+ }
1402
+ return logicalLines;
1403
+ }
1404
+ function splitInstruction(line, lineNumber) {
1405
+ const trimmed = line.trim();
1406
+ if (!trimmed) {
1407
+ throw new Error(`line ${lineNumber}: empty Dockerfile instruction`);
1408
+ }
1409
+ const match = trimmed.match(/^(\S+)(?:\s+(.*))?$/);
1410
+ if (!match) {
1411
+ throw new Error(`line ${lineNumber}: invalid Dockerfile instruction`);
1412
+ }
1413
+ return {
1414
+ keyword: match[1].toUpperCase(),
1415
+ value: (match[2] ?? "").trim()
1416
+ };
1417
+ }
1418
+ function shellSplit(input) {
1419
+ const tokens = [];
1420
+ let current = "";
1421
+ let quote = null;
1422
+ let escape = false;
1423
+ for (let i = 0; i < input.length; i++) {
1424
+ const char = input[i];
1425
+ if (escape) {
1426
+ current += char;
1427
+ escape = false;
1428
+ continue;
1429
+ }
1430
+ if (quote == null) {
1431
+ if (/\s/.test(char)) {
1432
+ if (current) {
1433
+ tokens.push(current);
1434
+ current = "";
1435
+ }
1436
+ continue;
1437
+ }
1438
+ if (char === "'" || char === '"') {
1439
+ quote = char;
1440
+ continue;
1441
+ }
1442
+ if (char === "\\") {
1443
+ escape = true;
1444
+ continue;
1445
+ }
1446
+ current += char;
1447
+ continue;
1448
+ }
1449
+ if (quote === "'") {
1450
+ if (char === "'") {
1451
+ quote = null;
1452
+ } else {
1453
+ current += char;
1454
+ }
1455
+ continue;
1456
+ }
1457
+ if (char === '"') {
1458
+ quote = null;
1459
+ continue;
1460
+ }
1461
+ if (char === "\\") {
1462
+ const next = input[++i];
1463
+ if (next == null) {
1464
+ throw new Error(`unterminated escape sequence in '${input}'`);
1465
+ }
1466
+ current += next;
1467
+ continue;
1468
+ }
1469
+ current += char;
1470
+ }
1471
+ if (escape) {
1472
+ throw new Error(`unterminated escape sequence in '${input}'`);
1473
+ }
1474
+ if (quote != null) {
1475
+ throw new Error(`unterminated quoted string in '${input}'`);
1476
+ }
1477
+ if (current) {
1478
+ tokens.push(current);
1479
+ }
1480
+ return tokens;
1481
+ }
1482
+ function shellQuote(value) {
1483
+ if (!value) {
1484
+ return "''";
1485
+ }
1486
+ return `'${value.replace(/'/g, `'\\''`)}'`;
1487
+ }
1488
+ function stripLeadingFlags(value) {
1489
+ const flags = {};
1490
+ let remaining = value.trimStart();
1491
+ while (remaining.startsWith("--")) {
1492
+ const firstSpace = remaining.indexOf(" ");
1493
+ if (firstSpace === -1) {
1494
+ throw new Error(`invalid Dockerfile flag syntax: ${value}`);
1495
+ }
1496
+ const token = remaining.slice(0, firstSpace);
1497
+ const rest = remaining.slice(firstSpace + 1).trimStart();
1498
+ const flagBody = token.slice(2);
1499
+ if (flagBody.includes("=")) {
1500
+ const [key, flagValue2] = flagBody.split(/=(.*)/s, 2);
1501
+ flags[key] = flagValue2;
1502
+ remaining = rest;
1503
+ continue;
1504
+ }
1505
+ const [flagValue, ...restTokens] = shellSplit(rest);
1506
+ if (flagValue == null) {
1507
+ throw new Error(`missing value for Dockerfile flag '${token}'`);
1508
+ }
1509
+ flags[flagBody] = flagValue;
1510
+ remaining = restTokens.join(" ");
1511
+ }
1512
+ return { flags, remaining };
1513
+ }
1514
+ function parseFromValue(value, lineNumber) {
1515
+ const { remaining } = stripLeadingFlags(value);
1516
+ const tokens = shellSplit(remaining);
1517
+ if (tokens.length === 0) {
1518
+ throw new Error(`line ${lineNumber}: FROM must include a base image`);
1519
+ }
1520
+ if (tokens.length > 1 && tokens[1].toLowerCase() !== "as") {
1521
+ throw new Error(`line ${lineNumber}: unsupported FROM syntax '${value}'`);
1522
+ }
1523
+ return tokens[0];
1524
+ }
1525
+ function parseCopyLikeValues(value, lineNumber, keyword) {
1526
+ const { flags, remaining } = stripLeadingFlags(value);
1527
+ if ("from" in flags) {
1528
+ throw new Error(
1529
+ `line ${lineNumber}: ${keyword} --from is not supported for sandbox image creation`
1530
+ );
1531
+ }
1532
+ const payload = remaining.trim();
1533
+ if (!payload) {
1534
+ throw new Error(
1535
+ `line ${lineNumber}: ${keyword} must include source and destination`
1536
+ );
1537
+ }
1538
+ let parts;
1539
+ if (payload.startsWith("[")) {
1540
+ let parsed;
1541
+ try {
1542
+ parsed = JSON.parse(payload);
1543
+ } catch (error) {
1544
+ throw new Error(
1545
+ `line ${lineNumber}: invalid JSON array syntax for ${keyword}: ${error.message}`
1546
+ );
1547
+ }
1548
+ if (!Array.isArray(parsed) || parsed.length < 2 || parsed.some((item) => typeof item !== "string")) {
1549
+ throw new Error(
1550
+ `line ${lineNumber}: ${keyword} JSON array form requires at least two string values`
1551
+ );
1552
+ }
1553
+ parts = parsed;
1554
+ } else {
1555
+ parts = shellSplit(payload);
1556
+ if (parts.length < 2) {
1557
+ throw new Error(
1558
+ `line ${lineNumber}: ${keyword} must include at least one source and one destination`
1559
+ );
1560
+ }
1561
+ }
1562
+ return {
1563
+ flags,
1564
+ sources: parts.slice(0, -1),
1565
+ destination: parts[parts.length - 1]
1566
+ };
1567
+ }
1568
+ function parseEnvPairs(value, lineNumber) {
1569
+ const tokens = shellSplit(value);
1570
+ if (tokens.length === 0) {
1571
+ throw new Error(`line ${lineNumber}: ENV must include a key and value`);
1572
+ }
1573
+ if (tokens.every((token) => token.includes("="))) {
1574
+ return tokens.map((token) => {
1575
+ const [key, envValue] = token.split(/=(.*)/s, 2);
1576
+ if (!key) {
1577
+ throw new Error(`line ${lineNumber}: invalid ENV token '${token}'`);
1578
+ }
1579
+ return [key, envValue];
1580
+ });
1581
+ }
1582
+ if (tokens.length < 2) {
1583
+ throw new Error(`line ${lineNumber}: ENV must include a key and value`);
1584
+ }
1585
+ return [[tokens[0], tokens.slice(1).join(" ")]];
1586
+ }
1587
+ function resolveContainerPath(containerPath, workingDir) {
1588
+ if (!containerPath) {
1589
+ return workingDir;
1590
+ }
1591
+ const normalized = containerPath.startsWith("/") ? import_node_path.default.posix.normalize(containerPath) : import_node_path.default.posix.normalize(import_node_path.default.posix.join(workingDir, containerPath));
1592
+ return normalized.startsWith("/") ? normalized : `/${normalized}`;
1593
+ }
1594
+ function buildPlanFromDockerfileText(dockerfileText, dockerfilePath, contextDir, registeredName) {
1595
+ let baseImage;
1596
+ const instructions = [];
1597
+ for (const logicalLine of logicalDockerfileLines(dockerfileText)) {
1598
+ const { keyword, value } = splitInstruction(
1599
+ logicalLine.line,
1600
+ logicalLine.lineNumber
1601
+ );
1602
+ if (keyword === "FROM") {
1603
+ if (baseImage != null) {
1604
+ throw new Error(
1605
+ `line ${logicalLine.lineNumber}: multi-stage Dockerfiles are not supported for sandbox image creation`
1606
+ );
1607
+ }
1608
+ baseImage = parseFromValue(value, logicalLine.lineNumber);
1609
+ continue;
1610
+ }
1611
+ if (UNSUPPORTED_DOCKERFILE_INSTRUCTIONS.has(keyword)) {
1612
+ throw new Error(
1613
+ `line ${logicalLine.lineNumber}: Dockerfile instruction '${keyword}' is not supported for sandbox image creation`
1614
+ );
1615
+ }
1616
+ instructions.push({
1617
+ keyword,
1618
+ value,
1619
+ lineNumber: logicalLine.lineNumber
1620
+ });
1621
+ }
1622
+ if (!baseImage) {
1623
+ throw new Error("Dockerfile must contain a FROM instruction");
1624
+ }
1625
+ return {
1626
+ dockerfilePath,
1627
+ contextDir,
1628
+ registeredName: registeredName ?? defaultRegisteredName(dockerfilePath),
1629
+ dockerfileText,
1630
+ baseImage,
1631
+ instructions
1632
+ };
1633
+ }
1634
+ async function loadDockerfilePlan(dockerfilePath, registeredName) {
1635
+ const resolvedPath = import_node_path.default.resolve(dockerfilePath);
1636
+ const fileStats = await (0, import_promises.stat)(resolvedPath).catch(() => null);
1637
+ if (!fileStats?.isFile()) {
1638
+ throw new Error(`Dockerfile not found: ${dockerfilePath}`);
1639
+ }
1640
+ const dockerfileText = await (0, import_promises.readFile)(resolvedPath, "utf8");
1641
+ return buildPlanFromDockerfileText(
1642
+ dockerfileText,
1643
+ resolvedPath,
1644
+ import_node_path.default.dirname(resolvedPath),
1645
+ registeredName
1646
+ );
1647
+ }
1648
+ function loadImagePlan(image, options = {}) {
1649
+ const contextDir = import_node_path.default.resolve(options.contextDir ?? process.cwd());
1650
+ const dockerfileText = dockerfileContent(image);
1651
+ const logicalLines = logicalDockerfileLines(dockerfileText);
1652
+ const instructions = image.baseImage == null ? logicalLines : logicalLines.slice(1);
1653
+ return {
1654
+ dockerfilePath: import_node_path.default.join(contextDir, "Dockerfile"),
1655
+ contextDir,
1656
+ registeredName: options.registeredName ?? image.name,
1657
+ dockerfileText,
1658
+ baseImage: image.baseImage ?? void 0,
1659
+ instructions: instructions.map(({ line, lineNumber }) => {
1660
+ const parsed = splitInstruction(line, lineNumber);
1661
+ return {
1662
+ keyword: parsed.keyword,
1663
+ value: parsed.value,
1664
+ lineNumber
1665
+ };
1666
+ })
1667
+ };
1668
+ }
1669
+ function defaultEmit(event) {
1670
+ process.stdout.write(`${JSON.stringify(event)}
1671
+ `);
1672
+ }
1673
+ function debugEnabled() {
1674
+ return ["1", "true", "yes", "on"].includes(
1675
+ (process.env.TENSORLAKE_DEBUG ?? "").toLowerCase()
1676
+ );
1677
+ }
1678
+ function buildContextFromEnv() {
1679
+ return {
1680
+ apiUrl: process.env.TENSORLAKE_API_URL ?? "https://api.tensorlake.ai",
1681
+ apiKey: process.env.TENSORLAKE_API_KEY,
1682
+ personalAccessToken: process.env.TENSORLAKE_PAT,
1683
+ namespace: process.env.INDEXIFY_NAMESPACE ?? "default",
1684
+ organizationId: process.env.TENSORLAKE_ORGANIZATION_ID,
1685
+ projectId: process.env.TENSORLAKE_PROJECT_ID,
1686
+ debug: debugEnabled()
1687
+ };
1688
+ }
1689
+ function createDefaultClient(context) {
1690
+ return new SandboxClient({
1691
+ apiUrl: context.apiUrl,
1692
+ apiKey: context.apiKey ?? context.personalAccessToken,
1693
+ organizationId: context.organizationId,
1694
+ projectId: context.projectId,
1695
+ namespace: context.namespace
1696
+ });
1697
+ }
1698
+ async function runChecked(sandbox, command, args, env, workingDir) {
1699
+ const result = await sandbox.run(command, {
1700
+ args,
1701
+ env,
1702
+ workingDir
1703
+ });
1704
+ if (result.exitCode !== 0) {
1705
+ throw new Error(
1706
+ `Command '${command} ${args.join(" ")}' failed with exit code ${result.exitCode}`
1707
+ );
1708
+ }
1709
+ return result;
1710
+ }
1711
+ async function runStreaming(sandbox, emit, sleep4, command, args = [], env, workingDir) {
1712
+ const proc = await sandbox.startProcess(command, {
1713
+ args,
1714
+ env,
1715
+ workingDir
1716
+ });
1717
+ let stdoutSeen = 0;
1718
+ let stderrSeen = 0;
1719
+ let info;
1720
+ while (true) {
1721
+ const stdoutResp = await sandbox.getStdout(proc.pid);
1722
+ emitOutputLines(emit, "stdout", stdoutResp, stdoutSeen);
1723
+ stdoutSeen = stdoutResp.lines.length;
1724
+ const stderrResp = await sandbox.getStderr(proc.pid);
1725
+ emitOutputLines(emit, "stderr", stderrResp, stderrSeen);
1726
+ stderrSeen = stderrResp.lines.length;
1727
+ info = await sandbox.getProcess(proc.pid);
1728
+ if (info.status !== "running" /* RUNNING */) {
1729
+ const finalStdout = await sandbox.getStdout(proc.pid);
1730
+ emitOutputLines(emit, "stdout", finalStdout, stdoutSeen);
1731
+ stdoutSeen = finalStdout.lines.length;
1732
+ const finalStderr = await sandbox.getStderr(proc.pid);
1733
+ emitOutputLines(emit, "stderr", finalStderr, stderrSeen);
1734
+ break;
1735
+ }
1736
+ await sleep4(300);
1737
+ }
1738
+ for (let i = 0; i < 10; i++) {
1739
+ if (info.exitCode != null || info.signal != null) {
1740
+ break;
1741
+ }
1742
+ await sleep4(200);
1743
+ info = await sandbox.getProcess(proc.pid);
1744
+ }
1745
+ const exitCode = info.exitCode != null ? info.exitCode : info.signal != null ? -info.signal : 0;
1746
+ if (exitCode !== 0) {
1747
+ throw new Error(
1748
+ `Command '${command} ${args.join(" ")}' failed with exit code ${exitCode}`
1749
+ );
1750
+ }
1751
+ }
1752
+ function emitOutputLines(emit, stream, response, seen) {
1753
+ for (const line of response.lines.slice(seen)) {
1754
+ emit({ type: "build_log", stream, message: line });
1755
+ }
1756
+ }
1757
+ function isPathWithinContext(contextDir, localPath) {
1758
+ const relative = import_node_path.default.relative(contextDir, localPath);
1759
+ return relative === "" || !relative.startsWith("..") && !import_node_path.default.isAbsolute(relative);
1760
+ }
1761
+ function resolveContextSourcePath(contextDir, source) {
1762
+ const resolvedContextDir = import_node_path.default.resolve(contextDir);
1763
+ const resolvedSource = import_node_path.default.resolve(resolvedContextDir, source);
1764
+ if (!isPathWithinContext(resolvedContextDir, resolvedSource)) {
1765
+ throw new Error(`Local path escapes the build context: ${source}`);
1766
+ }
1767
+ return resolvedSource;
1768
+ }
1769
+ async function copyLocalPathToSandbox(sandbox, localPath, remotePath) {
1770
+ const fileStats = await (0, import_promises.stat)(localPath).catch(() => null);
1771
+ if (!fileStats) {
1772
+ throw new Error(`Local path not found: ${localPath}`);
1773
+ }
1774
+ if (fileStats.isFile()) {
1775
+ await runChecked(sandbox, "mkdir", ["-p", import_node_path.default.posix.dirname(remotePath)]);
1776
+ await sandbox.writeFile(remotePath, await (0, import_promises.readFile)(localPath));
1777
+ return;
1778
+ }
1779
+ if (!fileStats.isDirectory()) {
1780
+ throw new Error(`Local path not found: ${localPath}`);
1781
+ }
1782
+ const entries = await (0, import_promises.readdir)(localPath, { withFileTypes: true });
1783
+ for (const entry of entries) {
1784
+ const sourcePath = import_node_path.default.join(localPath, entry.name);
1785
+ const destinationPath = import_node_path.default.posix.join(remotePath, entry.name);
1786
+ if (entry.isDirectory()) {
1787
+ await runChecked(sandbox, "mkdir", ["-p", destinationPath]);
1788
+ await copyLocalPathToSandbox(sandbox, sourcePath, destinationPath);
1789
+ } else if (entry.isFile()) {
1790
+ await runChecked(
1791
+ sandbox,
1792
+ "mkdir",
1793
+ ["-p", import_node_path.default.posix.dirname(destinationPath)]
1794
+ );
1795
+ await sandbox.writeFile(destinationPath, await (0, import_promises.readFile)(sourcePath));
1796
+ }
1797
+ }
1798
+ }
1799
+ async function persistEnvVar(sandbox, processEnv, key, value) {
1800
+ const exportLine = `export ${key}=${shellQuote(value)}`;
1801
+ await runChecked(
1802
+ sandbox,
1803
+ "sh",
1804
+ ["-c", `printf '%s\\n' ${shellQuote(exportLine)} >> /etc/environment`],
1805
+ processEnv
1806
+ );
1807
+ }
1808
+ async function copyFromContext(sandbox, emit, contextDir, sources, destination, workingDir, keyword) {
1809
+ const destinationPath = resolveContainerPath(destination, workingDir);
1810
+ if (sources.length > 1 && !destinationPath.endsWith("/")) {
1811
+ throw new Error(
1812
+ `${keyword} with multiple sources requires a directory destination ending in '/'`
1813
+ );
1814
+ }
1815
+ for (const source of sources) {
1816
+ const localSource = resolveContextSourcePath(contextDir, source);
1817
+ const localStats = await (0, import_promises.stat)(localSource).catch(() => null);
1818
+ if (!localStats) {
1819
+ throw new Error(`Local path not found: ${localSource}`);
1820
+ }
1821
+ let remoteDestination = destinationPath;
1822
+ if (sources.length > 1) {
1823
+ remoteDestination = import_node_path.default.posix.join(
1824
+ destinationPath.replace(/\/$/, ""),
1825
+ import_node_path.default.posix.basename(source.replace(/\/$/, ""))
1826
+ );
1827
+ } else if (localStats.isFile() && destinationPath.endsWith("/")) {
1828
+ remoteDestination = import_node_path.default.posix.join(
1829
+ destinationPath.replace(/\/$/, ""),
1830
+ import_node_path.default.basename(source)
1831
+ );
1832
+ }
1833
+ emit({
1834
+ type: "status",
1835
+ message: `${keyword} ${source} -> ${remoteDestination}`
1836
+ });
1837
+ await copyLocalPathToSandbox(sandbox, localSource, remoteDestination);
1838
+ }
1839
+ }
1840
+ async function addUrlToSandbox(sandbox, emit, url, destination, workingDir, processEnv, sleep4) {
1841
+ let destinationPath = resolveContainerPath(destination, workingDir);
1842
+ const parsedUrl = new URL(url);
1843
+ const fileName = import_node_path.default.posix.basename(parsedUrl.pathname.replace(/\/$/, "")) || "downloaded";
1844
+ if (destinationPath.endsWith("/")) {
1845
+ destinationPath = import_node_path.default.posix.join(destinationPath.replace(/\/$/, ""), fileName);
1846
+ }
1847
+ const parentDir = import_node_path.default.posix.dirname(destinationPath) || "/";
1848
+ emit({
1849
+ type: "status",
1850
+ message: `ADD ${url} -> ${destinationPath}`
1851
+ });
1852
+ await runChecked(sandbox, "mkdir", ["-p", parentDir], processEnv);
1853
+ await runStreaming(
1854
+ sandbox,
1855
+ emit,
1856
+ sleep4,
1857
+ "sh",
1858
+ [
1859
+ "-c",
1860
+ `curl -fsSL --location ${shellQuote(url)} -o ${shellQuote(destinationPath)}`
1861
+ ],
1862
+ processEnv,
1863
+ workingDir
1864
+ );
1865
+ }
1866
+ async function executeDockerfilePlan(sandbox, plan, emit, sleep4) {
1867
+ const processEnv = { ...BUILD_SANDBOX_PIP_ENV };
1868
+ let workingDir = "/";
1869
+ for (const instruction of plan.instructions) {
1870
+ const { keyword, value, lineNumber } = instruction;
1871
+ if (keyword === "RUN") {
1872
+ emit({ type: "status", message: `RUN ${value}` });
1873
+ await runStreaming(
1874
+ sandbox,
1875
+ emit,
1876
+ sleep4,
1877
+ "sh",
1878
+ ["-c", value],
1879
+ processEnv,
1880
+ workingDir
1881
+ );
1882
+ continue;
1883
+ }
1884
+ if (keyword === "WORKDIR") {
1885
+ const tokens = shellSplit(value);
1886
+ if (tokens.length !== 1) {
1887
+ throw new Error(`line ${lineNumber}: WORKDIR must include exactly one path`);
1888
+ }
1889
+ workingDir = resolveContainerPath(tokens[0], workingDir);
1890
+ emit({ type: "status", message: `WORKDIR ${workingDir}` });
1891
+ await runChecked(sandbox, "mkdir", ["-p", workingDir], processEnv);
1892
+ continue;
1893
+ }
1894
+ if (keyword === "ENV") {
1895
+ for (const [key, envValue] of parseEnvPairs(value, lineNumber)) {
1896
+ emit({ type: "status", message: `ENV ${key}=${envValue}` });
1897
+ processEnv[key] = envValue;
1898
+ await persistEnvVar(sandbox, processEnv, key, envValue);
1899
+ }
1900
+ continue;
1901
+ }
1902
+ if (keyword === "COPY") {
1903
+ const { sources, destination } = parseCopyLikeValues(
1904
+ value,
1905
+ lineNumber,
1906
+ keyword
1907
+ );
1908
+ await copyFromContext(
1909
+ sandbox,
1910
+ emit,
1911
+ plan.contextDir,
1912
+ sources,
1913
+ destination,
1914
+ workingDir,
1915
+ keyword
1916
+ );
1917
+ continue;
1918
+ }
1919
+ if (keyword === "ADD") {
1920
+ const { sources, destination } = parseCopyLikeValues(
1921
+ value,
1922
+ lineNumber,
1923
+ keyword
1924
+ );
1925
+ if (sources.length === 1 && /^https?:\/\//.test(sources[0])) {
1926
+ await addUrlToSandbox(
1927
+ sandbox,
1928
+ emit,
1929
+ sources[0],
1930
+ destination,
1931
+ workingDir,
1932
+ processEnv,
1933
+ sleep4
1934
+ );
1935
+ } else {
1936
+ await copyFromContext(
1937
+ sandbox,
1938
+ emit,
1939
+ plan.contextDir,
1940
+ sources,
1941
+ destination,
1942
+ workingDir,
1943
+ keyword
1944
+ );
1945
+ }
1946
+ continue;
1947
+ }
1948
+ if (IGNORED_DOCKERFILE_INSTRUCTIONS.has(keyword)) {
1949
+ emit({
1950
+ type: "warning",
1951
+ message: `Skipping Dockerfile instruction '${keyword}' during snapshot materialization. It is still preserved in the registered Dockerfile.`
1952
+ });
1953
+ continue;
1954
+ }
1955
+ throw new Error(
1956
+ `line ${lineNumber}: Dockerfile instruction '${keyword}' is not supported for sandbox image creation`
1957
+ );
1958
+ }
1959
+ }
1960
+ async function registerImage(context, name, dockerfile, snapshotId, snapshotUri, isPublic) {
1961
+ if (!context.organizationId || !context.projectId) {
1962
+ throw new Error(
1963
+ "Organization ID and Project ID are required. Run 'tl login' and 'tl init'."
1964
+ );
1965
+ }
1966
+ const bearerToken = context.apiKey ?? context.personalAccessToken;
1967
+ if (!bearerToken) {
1968
+ throw new Error("Missing TENSORLAKE_API_KEY or TENSORLAKE_PAT.");
1969
+ }
1970
+ const baseUrl = context.apiUrl.replace(/\/+$/, "");
1971
+ const url = `${baseUrl}/platform/v1/organizations/${encodeURIComponent(context.organizationId)}/projects/${encodeURIComponent(context.projectId)}/sandbox-templates`;
1972
+ const headers = {
1973
+ Authorization: `Bearer ${bearerToken}`,
1974
+ "Content-Type": "application/json"
1975
+ };
1976
+ if (context.personalAccessToken && !context.apiKey) {
1977
+ headers["X-Forwarded-Organization-Id"] = context.organizationId;
1978
+ headers["X-Forwarded-Project-Id"] = context.projectId;
1979
+ }
1980
+ const response = await fetch(url, {
1981
+ method: "POST",
1982
+ headers,
1983
+ body: JSON.stringify({
1984
+ name,
1985
+ dockerfile,
1986
+ snapshotId,
1987
+ snapshotUri,
1988
+ isPublic
1989
+ })
1990
+ });
1991
+ if (!response.ok) {
1992
+ throw new Error(
1993
+ `${response.status} ${response.statusText}: ${await response.text()}`
1994
+ );
1995
+ }
1996
+ const text = await response.text();
1997
+ return text ? JSON.parse(text) : {};
1998
+ }
1999
+ async function createSandboxImage(source, options = {}, deps = {}) {
2000
+ const emit = deps.emit ?? defaultEmit;
2001
+ const sleep4 = deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
2002
+ const context = buildContextFromEnv();
2003
+ const clientFactory = deps.createClient ?? createDefaultClient;
2004
+ const register = deps.registerImage ?? ((...args) => registerImage(...args));
2005
+ const sourceLabel = typeof source === "string" ? source : `Image(${source.name})`;
2006
+ emit({ type: "status", message: `Loading ${sourceLabel}...` });
2007
+ const plan = typeof source === "string" ? await loadDockerfilePlan(source, options.registeredName) : loadImagePlan(source, options);
2008
+ emit({
2009
+ type: "status",
2010
+ message: plan.baseImage == null ? "Starting build sandbox with the default server image..." : `Starting build sandbox from ${plan.baseImage}...`
2011
+ });
2012
+ const client = clientFactory(context);
2013
+ let sandbox;
2014
+ try {
2015
+ sandbox = await client.createAndConnect({
2016
+ ...plan.baseImage == null ? {} : { image: plan.baseImage },
2017
+ cpus: options.cpus ?? 2,
2018
+ memoryMb: options.memoryMb ?? 4096
2019
+ });
2020
+ emit({
2021
+ type: "status",
2022
+ message: `Materializing image in sandbox ${sandbox.sandboxId}...`
2023
+ });
2024
+ await executeDockerfilePlan(sandbox, plan, emit, sleep4);
2025
+ emit({ type: "status", message: "Creating snapshot..." });
2026
+ const snapshot = await client.snapshotAndWait(sandbox.sandboxId);
2027
+ emit({
2028
+ type: "snapshot_created",
2029
+ snapshot_id: snapshot.snapshotId,
2030
+ snapshot_uri: snapshot.snapshotUri ?? null
2031
+ });
2032
+ if (!snapshot.snapshotUri) {
2033
+ throw new Error(
2034
+ `Snapshot ${snapshot.snapshotId} is missing snapshotUri and cannot be registered as a sandbox image.`
2035
+ );
2036
+ }
2037
+ emit({
2038
+ type: "status",
2039
+ message: `Registering image '${plan.registeredName}'...`
2040
+ });
2041
+ const result = await register(
2042
+ context,
2043
+ plan.registeredName,
2044
+ plan.dockerfileText,
2045
+ snapshot.snapshotId,
2046
+ snapshot.snapshotUri,
2047
+ options.isPublic ?? false
2048
+ );
2049
+ emit({
2050
+ type: "image_registered",
2051
+ name: plan.registeredName,
2052
+ image_id: typeof result.id === "string" && result.id || typeof result.templateId === "string" && result.templateId || ""
2053
+ });
2054
+ emit({ type: "done" });
2055
+ return result;
2056
+ } finally {
2057
+ if (sandbox) {
2058
+ try {
2059
+ await sandbox.terminate();
2060
+ } catch {
2061
+ }
2062
+ }
2063
+ client.close();
2064
+ }
2065
+ }
2066
+ async function runCreateSandboxImageCli(argv = process.argv.slice(2)) {
2067
+ const parsed = (0, import_node_util.parseArgs)({
2068
+ args: argv,
2069
+ allowPositionals: true,
2070
+ options: {
2071
+ name: { type: "string", short: "n" },
2072
+ cpus: { type: "string" },
2073
+ memory: { type: "string" },
2074
+ public: { type: "boolean", default: false }
2075
+ }
2076
+ });
2077
+ const dockerfilePath = parsed.positionals[0];
2078
+ if (!dockerfilePath) {
2079
+ throw new Error("Usage: tensorlake-create-sandbox-image <dockerfile_path> [--name NAME] [--cpus N] [--memory MB] [--public]");
2080
+ }
2081
+ const cpus = parsed.values.cpus != null ? Number(parsed.values.cpus) : void 0;
2082
+ const memoryMb = parsed.values.memory != null ? Number(parsed.values.memory) : void 0;
2083
+ if (cpus != null && !Number.isFinite(cpus)) {
2084
+ throw new Error(`Invalid --cpus value: ${parsed.values.cpus}`);
2085
+ }
2086
+ if (memoryMb != null && !Number.isInteger(memoryMb)) {
2087
+ throw new Error(`Invalid --memory value: ${parsed.values.memory}`);
2088
+ }
2089
+ await createSandboxImage(dockerfilePath, {
2090
+ registeredName: parsed.values.name,
2091
+ cpus,
2092
+ memoryMb,
2093
+ isPublic: parsed.values.public
2094
+ });
2095
+ }
2096
+ // Annotate the CommonJS export names for ESM import in node:
2097
+ 0 && (module.exports = {
2098
+ createSandboxImage,
2099
+ defaultRegisteredName,
2100
+ loadDockerfilePlan,
2101
+ loadImagePlan,
2102
+ logicalDockerfileLines,
2103
+ runCreateSandboxImageCli
2104
+ });
2105
+ //# sourceMappingURL=sandbox-image.cjs.map