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