tensorlake 0.5.131 → 0.5.133
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/README.md +27 -0
- package/dist/applications/index.cjs +492 -708
- package/dist/applications/index.cjs.map +1 -1
- package/dist/applications/index.d.cts +3 -3
- package/dist/applications/index.d.ts +3 -3
- package/dist/applications/index.js +493 -713
- package/dist/applications/index.js.map +1 -1
- package/dist/function-agent/main.cjs +5 -2
- package/dist/function-agent/main.cjs.map +1 -1
- package/dist/function-agent/main.js +6 -3
- package/dist/function-agent/main.js.map +1 -1
- package/dist/function-executor/main.cjs +6 -3
- package/dist/function-executor/main.cjs.map +1 -1
- package/dist/function-executor/main.js +7 -4
- package/dist/function-executor/main.js.map +1 -1
- package/dist/index.cjs +121 -418
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -22
- package/dist/index.d.ts +8 -22
- package/dist/index.js +122 -423
- package/dist/index.js.map +1 -1
- package/dist/native-worker.cjs +1 -0
- package/dist/native-worker.cjs.map +1 -1
- package/dist/native-worker.js +1 -0
- package/dist/native-worker.js.map +1 -1
- package/dist/{remote-DRCCuRth.d.cts → remote-D95Y6Czr.d.cts} +1 -1
- package/dist/{remote-0dzBqScB.d.ts → remote-omwTzbfj.d.ts} +1 -1
- package/dist/{sandbox-image-Bf5bcxtO.d.cts → sandbox-image-ChzWDeZy.d.cts} +2 -0
- package/dist/{sandbox-image-Bf5bcxtO.d.ts → sandbox-image-ChzWDeZy.d.ts} +2 -0
- package/dist/sandbox-image.cjs +476 -453
- package/dist/sandbox-image.cjs.map +1 -1
- package/dist/sandbox-image.d.cts +1 -1
- package/dist/sandbox-image.d.ts +1 -1
- package/dist/sandbox-image.js +475 -456
- package/dist/sandbox-image.js.map +1 -1
- package/package.json +8 -8
- package/runtime/function-executor/manifest.json +14 -14
- package/runtime/function-executor/package/dist/function-executor/main.js +7 -4
- package/runtime/function-executor/package/dist/function-executor/main.js.map +1 -1
- package/runtime/function-executor/package/dist/native-worker.cjs +1 -0
- package/runtime/function-executor/package/dist/native-worker.cjs.map +1 -1
- package/runtime/function-executor/package/npm-shrinkwrap.json +2 -12
- package/runtime/function-executor/package/package.json +1 -2
- package/runtime/typescript-function-runner/manifest.json +13 -13
- package/runtime/typescript-function-runner/package/dist/function-agent/main.js +6 -3
- package/runtime/typescript-function-runner/package/dist/function-agent/main.js.map +1 -1
- package/runtime/typescript-function-runner/package/dist/native-worker.cjs +1 -0
- package/runtime/typescript-function-runner/package/dist/native-worker.cjs.map +1 -1
- package/runtime/typescript-function-runner/package/npm-shrinkwrap.json +20 -29
- package/runtime/typescript-function-runner/package/package.json +7 -7
package/dist/sandbox-image.cjs
CHANGED
|
@@ -44,12 +44,12 @@ var init_cjs_shims = __esm({
|
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
// src/defaults.ts
|
|
47
|
-
var SDK_VERSION, API_URL, API_KEY, NAMESPACE, SANDBOX_PROXY_URL, DEFAULT_HTTP_TIMEOUT_MS, MAX_RETRIES, RETRY_BACKOFF_MS
|
|
47
|
+
var SDK_VERSION, API_URL, API_KEY, NAMESPACE, SANDBOX_PROXY_URL, DEFAULT_HTTP_TIMEOUT_MS, MAX_RETRIES, RETRY_BACKOFF_MS;
|
|
48
48
|
var init_defaults = __esm({
|
|
49
49
|
"src/defaults.ts"() {
|
|
50
50
|
"use strict";
|
|
51
51
|
init_cjs_shims();
|
|
52
|
-
SDK_VERSION = "0.5.
|
|
52
|
+
SDK_VERSION = "0.5.133";
|
|
53
53
|
API_URL = process.env.TENSORLAKE_API_URL ?? "https://api.tensorlake.ai";
|
|
54
54
|
API_KEY = process.env.TENSORLAKE_API_KEY ?? void 0;
|
|
55
55
|
NAMESPACE = process.env.INDEXIFY_NAMESPACE ?? "default";
|
|
@@ -57,33 +57,10 @@ var init_defaults = __esm({
|
|
|
57
57
|
DEFAULT_HTTP_TIMEOUT_MS = 3e5;
|
|
58
58
|
MAX_RETRIES = 3;
|
|
59
59
|
RETRY_BACKOFF_MS = 500;
|
|
60
|
-
RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([429, 502, 503, 504]);
|
|
61
60
|
}
|
|
62
61
|
});
|
|
63
62
|
|
|
64
63
|
// src/errors.ts
|
|
65
|
-
function describeError(err) {
|
|
66
|
-
const parts = [];
|
|
67
|
-
const seen = /* @__PURE__ */ new Set();
|
|
68
|
-
let current = err;
|
|
69
|
-
for (let depth = 0; depth < 6; depth++) {
|
|
70
|
-
if (current == null || typeof current !== "object" || seen.has(current)) {
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
seen.add(current);
|
|
74
|
-
const e = current;
|
|
75
|
-
const message = typeof e.message === "string" ? e.message.trim() : "";
|
|
76
|
-
const code = typeof e.code === "string" ? e.code : void 0;
|
|
77
|
-
let segment = message;
|
|
78
|
-
if (code && (!message || !message.includes(code))) {
|
|
79
|
-
segment = segment ? `${segment} (${code})` : code;
|
|
80
|
-
}
|
|
81
|
-
if (segment && !parts.includes(segment)) parts.push(segment);
|
|
82
|
-
current = e.cause;
|
|
83
|
-
}
|
|
84
|
-
if (parts.length > 0) return parts.join(": ");
|
|
85
|
-
return err instanceof Error ? err.message : String(err);
|
|
86
|
-
}
|
|
87
64
|
function formatErrorDetails(errorDetails) {
|
|
88
65
|
if (errorDetails == null) return void 0;
|
|
89
66
|
if (typeof errorDetails === "string") {
|
|
@@ -197,360 +174,464 @@ var init_errors = __esm({
|
|
|
197
174
|
}
|
|
198
175
|
});
|
|
199
176
|
|
|
200
|
-
// src/
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
function nowMs() {
|
|
210
|
-
return performance.now();
|
|
211
|
-
}
|
|
212
|
-
function logSdkTiming(op, phase, startedAt, fields = {}) {
|
|
213
|
-
if (!sdkTimingsEnabled()) return;
|
|
214
|
-
logSdkTimingEvent(op, phase, {
|
|
215
|
-
...fields,
|
|
216
|
-
duration_ms: Math.round((nowMs() - startedAt) * 100) / 100
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
function logSdkTimingEvent(op, phase, fields = {}) {
|
|
220
|
-
if (!sdkTimingsEnabled()) return;
|
|
221
|
-
const parts = [
|
|
222
|
-
"[tensorlake:sdk-timing]",
|
|
223
|
-
`op=${op}`,
|
|
224
|
-
`phase=${phase}`
|
|
225
|
-
];
|
|
226
|
-
for (const [key, value] of Object.entries(fields)) {
|
|
227
|
-
if (value == null) continue;
|
|
228
|
-
parts.push(`${key}=${formatSdkTimingValue(value)}`);
|
|
229
|
-
}
|
|
230
|
-
console.error(parts.join(" "));
|
|
231
|
-
}
|
|
232
|
-
function formatSdkTimingValue(value) {
|
|
233
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
234
|
-
return String(value);
|
|
177
|
+
// src/native-worker-protocol.ts
|
|
178
|
+
var cancelNativeCall, closeNativeHandle;
|
|
179
|
+
var init_native_worker_protocol = __esm({
|
|
180
|
+
"src/native-worker-protocol.ts"() {
|
|
181
|
+
"use strict";
|
|
182
|
+
init_cjs_shims();
|
|
183
|
+
cancelNativeCall = /* @__PURE__ */ Symbol.for("tensorlake.native.cancel.v1");
|
|
184
|
+
closeNativeHandle = /* @__PURE__ */ Symbol.for("tensorlake.native.close.v1");
|
|
235
185
|
}
|
|
236
|
-
|
|
237
|
-
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// src/url.ts
|
|
189
|
+
function trimTrailingSlashes(url) {
|
|
190
|
+
return url.endsWith("/") ? url.slice(0, -1) : url;
|
|
191
|
+
}
|
|
192
|
+
function resolveProxyTarget(proxyUrl, sandboxId) {
|
|
193
|
+
try {
|
|
194
|
+
const parsed = new URL(proxyUrl);
|
|
195
|
+
const host = parsed.hostname;
|
|
196
|
+
if (host === "localhost" || host === "127.0.0.1") {
|
|
197
|
+
return {
|
|
198
|
+
baseUrl: trimTrailingSlashes(proxyUrl),
|
|
199
|
+
hostHeader: `${sandboxId}.local`,
|
|
200
|
+
sandboxIdHeader: void 0
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
const port = parsed.port ? `:${parsed.port}` : "";
|
|
204
|
+
return {
|
|
205
|
+
baseUrl: `${parsed.protocol}//${host}${port}`,
|
|
206
|
+
hostHeader: void 0,
|
|
207
|
+
sandboxIdHeader: sandboxId
|
|
208
|
+
};
|
|
209
|
+
} catch {
|
|
210
|
+
return {
|
|
211
|
+
baseUrl: `${trimTrailingSlashes(proxyUrl)}/${sandboxId}`,
|
|
212
|
+
hostHeader: void 0,
|
|
213
|
+
sandboxIdHeader: void 0
|
|
214
|
+
};
|
|
238
215
|
}
|
|
239
|
-
return JSON.stringify(value);
|
|
240
216
|
}
|
|
241
|
-
var
|
|
242
|
-
|
|
243
|
-
"src/sdk-timings.ts"() {
|
|
217
|
+
var init_url = __esm({
|
|
218
|
+
"src/url.ts"() {
|
|
244
219
|
"use strict";
|
|
245
220
|
init_cjs_shims();
|
|
246
|
-
|
|
247
|
-
SDK_TIMING_PAYLOADS_ENV_VAR = "TENSORLAKE_SDK_TIMING_PAYLOADS";
|
|
221
|
+
init_defaults();
|
|
248
222
|
}
|
|
249
223
|
});
|
|
250
224
|
|
|
251
|
-
// src/
|
|
252
|
-
function
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
225
|
+
// src/native-worker-client.ts
|
|
226
|
+
function createWorkerBinding(runtime) {
|
|
227
|
+
return {
|
|
228
|
+
NativeCloudClient: class {
|
|
229
|
+
constructor(...args) {
|
|
230
|
+
return runtime.handle("NativeCloudClient", args);
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
NativeSandboxClient: class {
|
|
234
|
+
constructor(...args) {
|
|
235
|
+
return runtime.handle("NativeSandboxClient", args);
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
NativeSandboxProxyClient: class {
|
|
239
|
+
constructor(...args) {
|
|
240
|
+
return runtime.handle("NativeSandboxProxyClient", args);
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
NativeRepositoryClient: class {
|
|
244
|
+
constructor(...args) {
|
|
245
|
+
return runtime.handle("NativeRepositoryClient", args);
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
validateManagedName: (name) => runtime.call(void 0, "validateManagedName", [name])
|
|
249
|
+
};
|
|
257
250
|
}
|
|
258
|
-
function
|
|
259
|
-
|
|
260
|
-
|
|
251
|
+
function workerBinding() {
|
|
252
|
+
return createWorkerBinding(
|
|
253
|
+
runtimes[runtimeKey] ??= new NativeWorkerClient()
|
|
254
|
+
);
|
|
261
255
|
}
|
|
262
|
-
function
|
|
263
|
-
|
|
264
|
-
return void 0;
|
|
265
|
-
}
|
|
266
|
-
if (body instanceof Uint8Array) {
|
|
267
|
-
return Uint8Array.from(body).buffer;
|
|
268
|
-
}
|
|
269
|
-
return body;
|
|
256
|
+
function releaseNativeHandle(handle) {
|
|
257
|
+
handle?.[closeNativeHandle]?.();
|
|
270
258
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
259
|
+
var import_node_module, import_node_worker_threads, NativeWorkerClient, runtimeKey, runtimes;
|
|
260
|
+
var init_native_worker_client = __esm({
|
|
261
|
+
"src/native-worker-client.ts"() {
|
|
262
|
+
"use strict";
|
|
263
|
+
init_cjs_shims();
|
|
264
|
+
import_node_module = require("module");
|
|
265
|
+
import_node_worker_threads = require("worker_threads");
|
|
266
|
+
init_native_worker_protocol();
|
|
267
|
+
init_url();
|
|
268
|
+
NativeWorkerClient = class {
|
|
269
|
+
constructor(workerPath = () => (0, import_node_module.createRequire)(importMetaUrl).resolve(
|
|
270
|
+
"tensorlake/internal/native-worker"
|
|
271
|
+
)) {
|
|
272
|
+
this.workerPath = workerPath;
|
|
273
|
+
}
|
|
274
|
+
worker;
|
|
275
|
+
nextId = 1;
|
|
276
|
+
registered = /* @__PURE__ */ new Set();
|
|
277
|
+
pending = /* @__PURE__ */ new Map();
|
|
278
|
+
finalizers = new FinalizationRegistry(
|
|
279
|
+
(id) => this.release(id)
|
|
280
|
+
);
|
|
281
|
+
handle(kind, args, parent) {
|
|
282
|
+
const handle = { id: this.nextId++, kind, args, parent };
|
|
283
|
+
this.finalizers.register(handle, handle.id);
|
|
284
|
+
let closed = false;
|
|
285
|
+
return new Proxy(
|
|
286
|
+
{},
|
|
287
|
+
{
|
|
288
|
+
get: (_target, method) => {
|
|
289
|
+
if (method === "then") return void 0;
|
|
290
|
+
if (method === closeNativeHandle)
|
|
291
|
+
return () => {
|
|
292
|
+
closed = true;
|
|
293
|
+
this.release(handle.id);
|
|
294
|
+
};
|
|
295
|
+
if (method === "connectProxy")
|
|
296
|
+
return (...proxyArgs) => {
|
|
297
|
+
if (closed) throw new Error("Tensorlake native client is closed");
|
|
298
|
+
return this.handle("NativeSandboxProxyClient", proxyArgs, handle);
|
|
299
|
+
};
|
|
300
|
+
if (method === "baseUrl")
|
|
301
|
+
return () => resolveProxyTarget(String(args[0]), String(args[1])).baseUrl;
|
|
302
|
+
if (typeof method !== "string") return void 0;
|
|
303
|
+
return (...callArgs) => {
|
|
304
|
+
if (closed)
|
|
305
|
+
return Promise.reject(
|
|
306
|
+
new Error("Tensorlake native client is closed")
|
|
307
|
+
);
|
|
308
|
+
const emit = typeof callArgs.at(-1) === "function" ? callArgs.pop() : void 0;
|
|
309
|
+
return this.call(handle, method, callArgs, emit);
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
call(handle, method, args, emit) {
|
|
316
|
+
const id = this.nextId++;
|
|
317
|
+
let worker;
|
|
318
|
+
const promise = new Promise((resolve, reject) => {
|
|
319
|
+
try {
|
|
320
|
+
worker = this.getWorker();
|
|
321
|
+
this.pending.set(id, { resolve, reject, emit, handle });
|
|
322
|
+
worker.ref();
|
|
323
|
+
if (handle) this.register(handle, worker);
|
|
324
|
+
this.send(worker, {
|
|
325
|
+
type: "call",
|
|
326
|
+
id,
|
|
327
|
+
handleId: handle?.id,
|
|
328
|
+
method,
|
|
329
|
+
args,
|
|
330
|
+
stream: !!emit
|
|
331
|
+
});
|
|
332
|
+
} catch (error) {
|
|
333
|
+
this.pending.delete(id);
|
|
334
|
+
this.unrefIfIdle();
|
|
335
|
+
reject(error);
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
promise[cancelNativeCall] = () => {
|
|
339
|
+
const pending = this.pending.get(id);
|
|
340
|
+
if (pending && worker) {
|
|
341
|
+
pending.emit = void 0;
|
|
342
|
+
this.send(worker, { type: "cancel", id });
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
return promise;
|
|
346
|
+
}
|
|
347
|
+
getWorker() {
|
|
348
|
+
if (this.worker) return this.worker;
|
|
349
|
+
const worker = new import_node_worker_threads.Worker(this.workerPath(), {
|
|
350
|
+
// The bundled worker needs no application loaders or telemetry preloads.
|
|
351
|
+
// NODE_OPTIONS is parsed independently of execArgv by Node.
|
|
352
|
+
execArgv: [],
|
|
353
|
+
env: { ...process.env, NODE_OPTIONS: "" },
|
|
354
|
+
name: "tensorlake-native"
|
|
355
|
+
});
|
|
356
|
+
this.worker = worker;
|
|
357
|
+
worker.on("message", (message) => {
|
|
358
|
+
const pending = this.pending.get(message.id);
|
|
359
|
+
if (!pending || this.worker !== worker) return;
|
|
360
|
+
if (message.type === "event") {
|
|
361
|
+
Promise.resolve().then(() => pending.emit?.(message.value)).then(
|
|
362
|
+
() => {
|
|
363
|
+
if (this.worker === worker)
|
|
364
|
+
this.send(worker, { type: "ack", id: message.id });
|
|
365
|
+
},
|
|
366
|
+
(error) => {
|
|
367
|
+
this.send(worker, { type: "cancel", id: message.id });
|
|
368
|
+
pending.reject(error);
|
|
369
|
+
}
|
|
370
|
+
);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
this.pending.delete(message.id);
|
|
374
|
+
if (message.type === "error") {
|
|
375
|
+
const error = new Error(message.error.message);
|
|
376
|
+
error.name = message.error.name;
|
|
377
|
+
error.stack = message.error.stack ?? error.stack;
|
|
378
|
+
pending.reject(error);
|
|
379
|
+
} else {
|
|
380
|
+
pending.resolve(message.value);
|
|
381
|
+
}
|
|
382
|
+
this.unrefIfIdle();
|
|
383
|
+
});
|
|
384
|
+
const fail = (error) => {
|
|
385
|
+
if (this.worker !== worker) return;
|
|
386
|
+
this.worker = void 0;
|
|
387
|
+
this.registered.clear();
|
|
388
|
+
for (const pending of this.pending.values()) pending.reject(error);
|
|
389
|
+
this.pending.clear();
|
|
390
|
+
void worker.terminate();
|
|
391
|
+
};
|
|
392
|
+
worker.on("error", fail);
|
|
393
|
+
worker.on("messageerror", fail);
|
|
394
|
+
worker.on(
|
|
395
|
+
"exit",
|
|
396
|
+
(code) => fail(
|
|
397
|
+
new Error(
|
|
398
|
+
`Tensorlake native worker exited (${code}); in-flight operations were not retried`
|
|
399
|
+
)
|
|
400
|
+
)
|
|
401
|
+
);
|
|
402
|
+
worker.unref();
|
|
403
|
+
return worker;
|
|
404
|
+
}
|
|
405
|
+
register(handle, worker) {
|
|
406
|
+
if (this.registered.has(handle.id)) return;
|
|
407
|
+
if (handle.parent) this.register(handle.parent, worker);
|
|
408
|
+
this.send(worker, {
|
|
409
|
+
type: "create",
|
|
410
|
+
handle: { id: handle.id, kind: handle.kind, args: handle.args },
|
|
411
|
+
parentId: handle.parent?.id
|
|
412
|
+
});
|
|
413
|
+
this.registered.add(handle.id);
|
|
414
|
+
}
|
|
415
|
+
release(handleId) {
|
|
416
|
+
if (this.worker && this.registered.delete(handleId))
|
|
417
|
+
this.send(this.worker, { type: "release", handleId });
|
|
418
|
+
}
|
|
419
|
+
send(worker, message) {
|
|
420
|
+
worker.postMessage(message);
|
|
421
|
+
}
|
|
422
|
+
unrefIfIdle() {
|
|
423
|
+
if (this.pending.size === 0) this.worker?.unref();
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
runtimeKey = /* @__PURE__ */ Symbol.for(
|
|
427
|
+
`tensorlake.native.worker.${false ? "source" : "0.5.133"}.v1`
|
|
428
|
+
);
|
|
429
|
+
runtimes = globalThis;
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
// src/native-sandbox.ts
|
|
434
|
+
function loadNativeSandboxBinding() {
|
|
435
|
+
return cachedBinding ??= workerBinding();
|
|
274
436
|
}
|
|
275
|
-
function
|
|
276
|
-
|
|
437
|
+
function parseNativeError(error) {
|
|
438
|
+
const message = error instanceof Error ? error.message : typeof error === "string" ? error : "";
|
|
439
|
+
if (!message) return null;
|
|
277
440
|
try {
|
|
278
|
-
const parsed = JSON.parse(
|
|
279
|
-
if (parsed
|
|
280
|
-
|
|
441
|
+
const parsed = JSON.parse(message);
|
|
442
|
+
if (parsed && typeof parsed.category === "string") {
|
|
443
|
+
return {
|
|
444
|
+
category: parsed.category,
|
|
445
|
+
status: typeof parsed.status === "number" ? parsed.status : null,
|
|
446
|
+
message: typeof parsed.message === "string" ? parsed.message : message
|
|
447
|
+
};
|
|
448
|
+
}
|
|
281
449
|
} catch {
|
|
282
450
|
}
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
function translateNativeError(error, context) {
|
|
454
|
+
const payload = parseNativeError(error);
|
|
455
|
+
if (!payload) {
|
|
456
|
+
if (error instanceof Error) return error;
|
|
457
|
+
return new SandboxError(String(error));
|
|
458
|
+
}
|
|
459
|
+
const { category, status, message } = payload;
|
|
460
|
+
if (category === "connection") {
|
|
461
|
+
return new SandboxConnectionError(message, { cause: error });
|
|
462
|
+
}
|
|
283
463
|
if (status === 404) {
|
|
284
|
-
if (
|
|
285
|
-
|
|
286
|
-
if (match) throw new PoolNotFoundError(match[1]);
|
|
464
|
+
if (context?.notFoundKind === "pool" && context.poolId) {
|
|
465
|
+
return new PoolNotFoundError(context.poolId);
|
|
287
466
|
}
|
|
288
|
-
if (
|
|
289
|
-
|
|
290
|
-
if (match) throw new SandboxNotFoundError(match[1]);
|
|
467
|
+
if (context?.notFoundKind === "sandbox" && context.sandboxId) {
|
|
468
|
+
return new SandboxNotFoundError(context.sandboxId);
|
|
291
469
|
}
|
|
292
|
-
|
|
470
|
+
return new RemoteAPIError(404, message);
|
|
293
471
|
}
|
|
294
|
-
if (status === 409) {
|
|
295
|
-
|
|
296
|
-
const match = path3.match(/sandbox-pools\/([^/]+)/);
|
|
297
|
-
if (match) throw new PoolInUseError(match[1], message);
|
|
298
|
-
}
|
|
472
|
+
if (status === 409 && context?.poolId) {
|
|
473
|
+
return new PoolInUseError(context.poolId, message);
|
|
299
474
|
}
|
|
300
|
-
|
|
475
|
+
if (typeof status === "number") {
|
|
476
|
+
return new RemoteAPIError(status, message);
|
|
477
|
+
}
|
|
478
|
+
return new SandboxError(message);
|
|
301
479
|
}
|
|
302
|
-
function
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return controller.signal;
|
|
308
|
-
}
|
|
309
|
-
signal.addEventListener("abort", () => controller.abort(signal.reason), {
|
|
310
|
-
once: true
|
|
311
|
-
});
|
|
480
|
+
async function callNative(fn, context) {
|
|
481
|
+
try {
|
|
482
|
+
return await fn();
|
|
483
|
+
} catch (error) {
|
|
484
|
+
throw translateNativeError(error, context);
|
|
312
485
|
}
|
|
313
|
-
return controller.signal;
|
|
314
486
|
}
|
|
315
|
-
function
|
|
316
|
-
|
|
487
|
+
async function* nativeEventStream(start, context, signal) {
|
|
488
|
+
if (signal?.aborted) return;
|
|
489
|
+
const queue = [];
|
|
490
|
+
let wake;
|
|
491
|
+
let finished = false;
|
|
492
|
+
let stopped = false;
|
|
493
|
+
let failure;
|
|
494
|
+
const notify = () => {
|
|
495
|
+
const resolve = wake;
|
|
496
|
+
wake = void 0;
|
|
497
|
+
resolve?.();
|
|
498
|
+
};
|
|
499
|
+
const emit = (value) => {
|
|
500
|
+
if (stopped) return Promise.resolve();
|
|
501
|
+
return new Promise((consumed) => {
|
|
502
|
+
queue.push({ value, consumed });
|
|
503
|
+
notify();
|
|
504
|
+
});
|
|
505
|
+
};
|
|
506
|
+
let call;
|
|
507
|
+
const onAbort = () => {
|
|
508
|
+
stopped = true;
|
|
509
|
+
call?.[cancelNativeCall]?.();
|
|
510
|
+
notify();
|
|
511
|
+
};
|
|
512
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
513
|
+
try {
|
|
514
|
+
call = start(emit);
|
|
515
|
+
void call.catch((error) => {
|
|
516
|
+
failure = error;
|
|
517
|
+
}).finally(() => {
|
|
518
|
+
finished = true;
|
|
519
|
+
notify();
|
|
520
|
+
});
|
|
521
|
+
while (!stopped) {
|
|
522
|
+
const event = queue.shift();
|
|
523
|
+
if (event) {
|
|
524
|
+
try {
|
|
525
|
+
yield JSON.parse(event.value);
|
|
526
|
+
} finally {
|
|
527
|
+
event.consumed();
|
|
528
|
+
}
|
|
529
|
+
} else if (finished) {
|
|
530
|
+
break;
|
|
531
|
+
} else {
|
|
532
|
+
await new Promise((resolve) => {
|
|
533
|
+
wake = resolve;
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if (!stopped && failure !== void 0) throw translateNativeError(failure, context);
|
|
538
|
+
} finally {
|
|
539
|
+
stopped = true;
|
|
540
|
+
signal?.removeEventListener("abort", onAbort);
|
|
541
|
+
call?.[cancelNativeCall]?.();
|
|
542
|
+
for (const event of queue) event.consumed();
|
|
543
|
+
}
|
|
317
544
|
}
|
|
318
|
-
var
|
|
319
|
-
var
|
|
320
|
-
"src/
|
|
545
|
+
var cachedBinding;
|
|
546
|
+
var init_native_sandbox = __esm({
|
|
547
|
+
"src/native-sandbox.ts"() {
|
|
321
548
|
"use strict";
|
|
322
549
|
init_cjs_shims();
|
|
323
|
-
import_undici = require("undici");
|
|
324
|
-
init_defaults();
|
|
325
550
|
init_errors();
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
551
|
+
init_native_worker_client();
|
|
552
|
+
init_native_worker_protocol();
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
// src/native-cloud.ts
|
|
557
|
+
async function serializeRequest(method, path3, options) {
|
|
558
|
+
const headers = { ...options?.headers };
|
|
559
|
+
let body = options?.body;
|
|
560
|
+
if (options?.json !== void 0) {
|
|
561
|
+
body = JSON.stringify(options.json);
|
|
562
|
+
if (!Object.keys(headers).some((key) => key.toLowerCase() === "content-type")) {
|
|
563
|
+
headers["Content-Type"] = "application/json";
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
const request = {
|
|
567
|
+
method,
|
|
568
|
+
path: path3,
|
|
569
|
+
headersJson: JSON.stringify(headers),
|
|
570
|
+
statusOnlyCodes: [...options?.statusOnlyCodes ?? []]
|
|
571
|
+
};
|
|
572
|
+
if (body instanceof FormData) {
|
|
573
|
+
request.parts = [];
|
|
574
|
+
for (const [name, value] of body.entries()) {
|
|
575
|
+
request.parts.push(typeof value === "string" ? { name, data: Buffer.from(value) } : { name, data: Buffer.from(await value.arrayBuffer()), filename: value.name, contentType: value.type || "application/octet-stream" });
|
|
576
|
+
}
|
|
577
|
+
} else if (body !== void 0) {
|
|
578
|
+
if (body instanceof Blob) request.body = Buffer.from(await body.arrayBuffer());
|
|
579
|
+
else if (typeof body === "string") request.body = Buffer.from(body);
|
|
580
|
+
else if (body instanceof ArrayBuffer) request.body = Buffer.from(new Uint8Array(body));
|
|
581
|
+
else request.body = Buffer.from(body);
|
|
582
|
+
}
|
|
583
|
+
return request;
|
|
584
|
+
}
|
|
585
|
+
var NativeCloudTransport;
|
|
586
|
+
var init_native_cloud = __esm({
|
|
587
|
+
"src/native-cloud.ts"() {
|
|
588
|
+
"use strict";
|
|
589
|
+
init_cjs_shims();
|
|
590
|
+
init_native_sandbox();
|
|
591
|
+
init_native_worker_client();
|
|
592
|
+
NativeCloudTransport = class {
|
|
341
593
|
constructor(options) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
this.retryBackoffMs = options.retryBackoffMs ?? RETRY_BACKOFF_MS;
|
|
346
|
-
this.timeoutMs = options.timeoutMs === void 0 ? DEFAULT_HTTP_TIMEOUT_MS : options.timeoutMs;
|
|
347
|
-
this.headers = {
|
|
348
|
-
"User-Agent": `tensorlake-typescript-sdk/${SDK_VERSION}`
|
|
349
|
-
};
|
|
350
|
-
if (this.timeoutMs != null) {
|
|
351
|
-
this.headers[REQUEST_TIMEOUT_HEADER] = String(this.timeoutMs);
|
|
352
|
-
}
|
|
353
|
-
if (options.apiKey) {
|
|
354
|
-
this.headers["Authorization"] = `Bearer ${options.apiKey}`;
|
|
355
|
-
}
|
|
356
|
-
if (options.organizationId) {
|
|
357
|
-
this.headers["X-Forwarded-Organization-Id"] = options.organizationId;
|
|
358
|
-
}
|
|
359
|
-
if (options.projectId) {
|
|
360
|
-
this.headers["X-Forwarded-Project-Id"] = options.projectId;
|
|
361
|
-
}
|
|
362
|
-
if (options.hostHeader) {
|
|
363
|
-
this.headers["Host"] = options.hostHeader;
|
|
364
|
-
}
|
|
365
|
-
if (options.sandboxIdHeader) {
|
|
366
|
-
this.headers["X-Tensorlake-Sandbox-Id"] = options.sandboxIdHeader;
|
|
594
|
+
this.options = options;
|
|
595
|
+
if (!Number.isSafeInteger(options.maxRetries) || options.maxRetries < 0 || options.maxRetries > 4294967295) {
|
|
596
|
+
throw new RangeError("maxRetries must be a non-negative 32-bit integer");
|
|
367
597
|
}
|
|
368
|
-
if (options.
|
|
369
|
-
|
|
598
|
+
if (!Number.isFinite(options.retryBackoffMs) || options.retryBackoffMs < 0) {
|
|
599
|
+
throw new RangeError("retryBackoffMs must be a non-negative finite number");
|
|
370
600
|
}
|
|
371
601
|
}
|
|
602
|
+
native;
|
|
603
|
+
closed = false;
|
|
604
|
+
client() {
|
|
605
|
+
if (this.closed) throw new Error("Tensorlake native client is closed");
|
|
606
|
+
if (this.native) return this.native;
|
|
607
|
+
const ctor = loadNativeSandboxBinding().NativeCloudClient;
|
|
608
|
+
if (!ctor) throw new Error("Native binding does not export NativeCloudClient; rebuild with 'npm run build:native'");
|
|
609
|
+
return this.native = new ctor(JSON.stringify(this.options));
|
|
610
|
+
}
|
|
372
611
|
close() {
|
|
373
|
-
this.
|
|
374
|
-
this.
|
|
375
|
-
|
|
376
|
-
/** Make a JSON request, returning the parsed response body. */
|
|
377
|
-
async requestJson(method, path3, options) {
|
|
378
|
-
const response = await this.requestResponse(method, path3, {
|
|
379
|
-
json: options?.body,
|
|
380
|
-
headers: options?.headers,
|
|
381
|
-
signal: options?.signal,
|
|
382
|
-
retryableStatusCodes: options?.retryableStatusCodes,
|
|
383
|
-
allowedErrorStatusCodes: options?.allowedErrorStatusCodes,
|
|
384
|
-
allowHttpErrors: options?.allowHttpErrors
|
|
385
|
-
});
|
|
386
|
-
const text = await response.text();
|
|
387
|
-
const data = text ? JSON.parse(text) : void 0;
|
|
388
|
-
return withTraceId(data, response.traceId);
|
|
389
|
-
}
|
|
390
|
-
/** Make a request returning raw bytes. */
|
|
391
|
-
async requestBytes(method, path3, options) {
|
|
392
|
-
const headers = { ...options?.headers ?? {} };
|
|
393
|
-
if (options?.contentType) {
|
|
394
|
-
headers["Content-Type"] = options.contentType;
|
|
395
|
-
}
|
|
396
|
-
const response = await this.requestResponse(method, path3, {
|
|
397
|
-
body: options?.body,
|
|
398
|
-
headers,
|
|
399
|
-
signal: options?.signal
|
|
400
|
-
});
|
|
401
|
-
const buffer = await response.arrayBuffer();
|
|
402
|
-
return withTraceId(new Uint8Array(buffer), response.traceId);
|
|
403
|
-
}
|
|
404
|
-
/** Make a request and return the response body as an SSE stream. */
|
|
405
|
-
async requestStream(method, path3, options) {
|
|
406
|
-
const response = await this.requestResponse(method, path3, {
|
|
407
|
-
json: options?.json,
|
|
408
|
-
headers: { Accept: "text/event-stream" },
|
|
409
|
-
signal: options?.signal
|
|
410
|
-
});
|
|
411
|
-
if (!response.body) {
|
|
412
|
-
throw new RemoteAPIError(
|
|
413
|
-
response.status,
|
|
414
|
-
"No response body for SSE stream"
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
return withTraceId(response.body, response.traceId);
|
|
612
|
+
this.closed = true;
|
|
613
|
+
releaseNativeHandle(this.native ?? null);
|
|
614
|
+
this.native = void 0;
|
|
418
615
|
}
|
|
419
|
-
/** Make a request and return the raw Response. */
|
|
420
616
|
async requestResponse(method, path3, options) {
|
|
421
|
-
const
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
if (hasJsonBody && !hasHeader(headers, "Content-Type")) {
|
|
427
|
-
headers["Content-Type"] = "application/json";
|
|
428
|
-
}
|
|
429
|
-
const body = hasJsonBody ? JSON.stringify(options?.json) : normalizeRequestBody(options?.body);
|
|
430
|
-
const { response, traceId } = await this.doFetch(
|
|
431
|
-
method,
|
|
432
|
-
path3,
|
|
433
|
-
body,
|
|
434
|
-
headers,
|
|
435
|
-
options?.signal,
|
|
436
|
-
options?.allowHttpErrors ?? false,
|
|
437
|
-
options?.retryableStatusCodes ?? RETRYABLE_STATUS_CODES,
|
|
438
|
-
options?.allowedErrorStatusCodes ?? /* @__PURE__ */ new Set()
|
|
439
|
-
);
|
|
440
|
-
return withTraceId(response, traceId);
|
|
441
|
-
}
|
|
442
|
-
static makeTraceparent() {
|
|
443
|
-
const randomHex = (bytes) => Array.from(crypto.getRandomValues(new Uint8Array(bytes))).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
444
|
-
const traceId = randomHex(16);
|
|
445
|
-
const spanId = randomHex(8);
|
|
446
|
-
return { traceparent: `00-${traceId}-${spanId}-01`, traceId };
|
|
447
|
-
}
|
|
448
|
-
async doFetch(method, path3, body, headers, signal, allowHttpErrors = false, retryableStatusCodes = RETRYABLE_STATUS_CODES, allowedErrorStatusCodes = /* @__PURE__ */ new Set()) {
|
|
449
|
-
const url = `${this.baseUrl}${path3}`;
|
|
450
|
-
const { traceparent, traceId } = _HttpClient.makeTraceparent();
|
|
451
|
-
headers["traceparent"] = traceparent;
|
|
452
|
-
let lastError;
|
|
453
|
-
const operationStart = nowMs();
|
|
454
|
-
const timingPath = sdkTimingPayloadsEnabled() ? path3 : stripPathPayload(path3);
|
|
455
|
-
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
456
|
-
if (attempt > 0) {
|
|
457
|
-
const delay = this.retryBackoffMs * Math.pow(2, attempt - 1);
|
|
458
|
-
logSdkTimingEvent("http.request", "retry_sleep_start", {
|
|
459
|
-
method,
|
|
460
|
-
path: timingPath,
|
|
461
|
-
attempt,
|
|
462
|
-
delay_ms: delay,
|
|
463
|
-
server_trace_id: traceId
|
|
464
|
-
});
|
|
465
|
-
await sleep(delay);
|
|
466
|
-
}
|
|
467
|
-
this.abortController = new AbortController();
|
|
468
|
-
let timedOut = false;
|
|
469
|
-
const timeoutId = this.timeoutMs == null ? null : setTimeout(() => {
|
|
470
|
-
timedOut = true;
|
|
471
|
-
this.abortController?.abort();
|
|
472
|
-
}, this.timeoutMs);
|
|
473
|
-
const combinedSignal = signal ? anySignal([signal, this.abortController.signal]) : this.abortController.signal;
|
|
474
|
-
const attemptStart = nowMs();
|
|
475
|
-
try {
|
|
476
|
-
const response = await (0, import_undici.fetch)(url, {
|
|
477
|
-
method,
|
|
478
|
-
headers,
|
|
479
|
-
body,
|
|
480
|
-
signal: combinedSignal
|
|
481
|
-
});
|
|
482
|
-
if (timeoutId != null) clearTimeout(timeoutId);
|
|
483
|
-
logSdkTiming("http.request", "headers", attemptStart, {
|
|
484
|
-
method,
|
|
485
|
-
path: timingPath,
|
|
486
|
-
status: response.status,
|
|
487
|
-
attempt,
|
|
488
|
-
server_trace_id: traceId
|
|
489
|
-
});
|
|
490
|
-
if (response.ok) {
|
|
491
|
-
logSdkTiming("http.request", "complete", operationStart, {
|
|
492
|
-
method,
|
|
493
|
-
path: timingPath,
|
|
494
|
-
status: response.status,
|
|
495
|
-
attempts: attempt + 1,
|
|
496
|
-
server_trace_id: traceId
|
|
497
|
-
});
|
|
498
|
-
return { response, traceId };
|
|
499
|
-
}
|
|
500
|
-
if (retryableStatusCodes.has(response.status) && attempt < this.maxRetries) {
|
|
501
|
-
lastError = new RemoteAPIError(
|
|
502
|
-
response.status,
|
|
503
|
-
await response.text().catch(() => "")
|
|
504
|
-
);
|
|
505
|
-
logSdkTimingEvent("http.request", "retryable_status", {
|
|
506
|
-
method,
|
|
507
|
-
path: timingPath,
|
|
508
|
-
status: response.status,
|
|
509
|
-
attempt,
|
|
510
|
-
server_trace_id: traceId
|
|
511
|
-
});
|
|
512
|
-
continue;
|
|
513
|
-
}
|
|
514
|
-
if (allowHttpErrors || allowedErrorStatusCodes.has(response.status)) {
|
|
515
|
-
logSdkTiming("http.request", "complete", operationStart, {
|
|
516
|
-
method,
|
|
517
|
-
path: timingPath,
|
|
518
|
-
status: response.status,
|
|
519
|
-
attempts: attempt + 1,
|
|
520
|
-
server_trace_id: traceId
|
|
521
|
-
});
|
|
522
|
-
return { response, traceId };
|
|
523
|
-
}
|
|
524
|
-
const errorBody = await response.text().catch(() => "");
|
|
525
|
-
throwMappedError(response.status, errorBody, path3);
|
|
526
|
-
} catch (err) {
|
|
527
|
-
if (timeoutId != null) clearTimeout(timeoutId);
|
|
528
|
-
if (err instanceof RemoteAPIError || err instanceof SandboxNotFoundError || err instanceof PoolNotFoundError || err instanceof PoolInUseError) {
|
|
529
|
-
throw err;
|
|
530
|
-
}
|
|
531
|
-
if (signal?.aborted && !timedOut) {
|
|
532
|
-
throw new SandboxConnectionError("request aborted by caller", {
|
|
533
|
-
cause: err
|
|
534
|
-
});
|
|
535
|
-
}
|
|
536
|
-
lastError = err instanceof Error ? err : new Error(String(err));
|
|
537
|
-
logSdkTiming("http.request", "error", attemptStart, {
|
|
538
|
-
method,
|
|
539
|
-
path: timingPath,
|
|
540
|
-
attempt,
|
|
541
|
-
server_trace_id: traceId,
|
|
542
|
-
error: timedOut ? `request timed out after ${this.timeoutMs}ms` : describeError(err)
|
|
543
|
-
});
|
|
544
|
-
if (attempt >= this.maxRetries) {
|
|
545
|
-
const detail = timedOut ? `request timed out after ${this.timeoutMs}ms` : describeError(err);
|
|
546
|
-
throw new SandboxConnectionError(detail, { cause: err });
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
throw new SandboxConnectionError(
|
|
551
|
-
lastError ? describeError(lastError) : "request failed",
|
|
552
|
-
{ cause: lastError }
|
|
617
|
+
const request = await serializeRequest(method, path3, options);
|
|
618
|
+
const raw = await callNative(() => this.client().request(request));
|
|
619
|
+
const response = new Response(
|
|
620
|
+
[204, 205, 304].includes(raw.status) ? null : Uint8Array.from(raw.data).buffer,
|
|
621
|
+
{ status: raw.status, headers: JSON.parse(raw.headersJson) }
|
|
553
622
|
);
|
|
623
|
+
return Object.assign(response, { traceId: raw.traceId });
|
|
624
|
+
}
|
|
625
|
+
async requestJson(method, path3) {
|
|
626
|
+
const response = await this.requestResponse(method, path3);
|
|
627
|
+
const text = await response.text();
|
|
628
|
+
const value = text ? JSON.parse(text) : null;
|
|
629
|
+
return Object.assign(value ?? {}, { traceId: response.traceId });
|
|
630
|
+
}
|
|
631
|
+
async *stream(method, path3, signal) {
|
|
632
|
+
if (signal?.aborted) return;
|
|
633
|
+
const request = await serializeRequest(method, path3, { headers: { Accept: "text/event-stream" } });
|
|
634
|
+
yield* nativeEventStream((emit) => this.client().stream(request, emit), void 0, signal);
|
|
554
635
|
}
|
|
555
636
|
};
|
|
556
637
|
}
|
|
@@ -606,65 +687,6 @@ var init_models = __esm({
|
|
|
606
687
|
}
|
|
607
688
|
});
|
|
608
689
|
|
|
609
|
-
// src/sse.ts
|
|
610
|
-
async function* parseSSEMessages(stream, signal) {
|
|
611
|
-
const reader = stream.getReader();
|
|
612
|
-
const decoder = new TextDecoder();
|
|
613
|
-
let buffer = "";
|
|
614
|
-
try {
|
|
615
|
-
while (true) {
|
|
616
|
-
if (signal?.aborted) break;
|
|
617
|
-
const { done, value } = await reader.read();
|
|
618
|
-
if (done) break;
|
|
619
|
-
buffer += decoder.decode(value, { stream: true });
|
|
620
|
-
const parts = buffer.split(/\r?\n\r?\n/);
|
|
621
|
-
buffer = parts.pop() ?? "";
|
|
622
|
-
for (const part of parts) {
|
|
623
|
-
const lines = part.split(/\r?\n/);
|
|
624
|
-
const dataLines = [];
|
|
625
|
-
let event;
|
|
626
|
-
let id;
|
|
627
|
-
for (const line of lines) {
|
|
628
|
-
if (!line || line.startsWith(":")) continue;
|
|
629
|
-
const separator = line.indexOf(":");
|
|
630
|
-
const field = separator === -1 ? line : line.slice(0, separator);
|
|
631
|
-
let value2 = separator === -1 ? "" : line.slice(separator + 1);
|
|
632
|
-
if (value2.startsWith(" ")) {
|
|
633
|
-
value2 = value2.slice(1);
|
|
634
|
-
}
|
|
635
|
-
if (field === "data") {
|
|
636
|
-
dataLines.push(value2);
|
|
637
|
-
} else if (field === "event") {
|
|
638
|
-
event = value2;
|
|
639
|
-
} else if (field === "id") {
|
|
640
|
-
id = value2;
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
if (dataLines.length > 0 || event || id) {
|
|
644
|
-
yield { data: dataLines.join("\n"), event, id };
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
} finally {
|
|
649
|
-
reader.releaseLock();
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
async function* parseSSEStream(stream, signal) {
|
|
653
|
-
for await (const message of parseSSEMessages(stream, signal)) {
|
|
654
|
-
if (!message.data) continue;
|
|
655
|
-
try {
|
|
656
|
-
yield JSON.parse(message.data);
|
|
657
|
-
} catch {
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
var init_sse = __esm({
|
|
662
|
-
"src/sse.ts"() {
|
|
663
|
-
"use strict";
|
|
664
|
-
init_cjs_shims();
|
|
665
|
-
}
|
|
666
|
-
});
|
|
667
|
-
|
|
668
690
|
// src/cloud-client.ts
|
|
669
691
|
function secretFromMetadata(metadata) {
|
|
670
692
|
return {
|
|
@@ -726,7 +748,7 @@ function createApplicationBuildForm(request, imageContexts) {
|
|
|
726
748
|
}
|
|
727
749
|
return form;
|
|
728
750
|
}
|
|
729
|
-
function
|
|
751
|
+
function trimTrailingSlashes2(value) {
|
|
730
752
|
return value.endsWith("/") ? value.slice(0, -1) : value;
|
|
731
753
|
}
|
|
732
754
|
function nextRequestPath(next) {
|
|
@@ -764,11 +786,10 @@ var init_cloud_client = __esm({
|
|
|
764
786
|
init_cjs_shims();
|
|
765
787
|
import_nanoid = require("nanoid");
|
|
766
788
|
init_defaults();
|
|
767
|
-
|
|
789
|
+
init_native_cloud();
|
|
768
790
|
init_models();
|
|
769
|
-
init_sse();
|
|
770
791
|
CloudClient = class _CloudClient {
|
|
771
|
-
|
|
792
|
+
transport;
|
|
772
793
|
organizationId;
|
|
773
794
|
projectId;
|
|
774
795
|
namespace;
|
|
@@ -776,20 +797,22 @@ var init_cloud_client = __esm({
|
|
|
776
797
|
this.organizationId = options?.organizationId;
|
|
777
798
|
this.projectId = options?.projectId;
|
|
778
799
|
this.namespace = options?.namespace ?? NAMESPACE;
|
|
779
|
-
this.
|
|
800
|
+
this.transport = new NativeCloudTransport({
|
|
780
801
|
baseUrl: options?.apiUrl ?? API_URL,
|
|
781
802
|
apiKey: options?.apiKey ?? API_KEY,
|
|
782
803
|
organizationId: this.organizationId,
|
|
783
804
|
projectId: this.projectId,
|
|
784
805
|
maxRetries: options?.maxRetries ?? MAX_RETRIES,
|
|
785
|
-
retryBackoffMs: options?.retryBackoffMs ?? RETRY_BACKOFF_MS
|
|
806
|
+
retryBackoffMs: options?.retryBackoffMs ?? RETRY_BACKOFF_MS,
|
|
807
|
+
timeoutMs: DEFAULT_HTTP_TIMEOUT_MS,
|
|
808
|
+
userAgent: `tensorlake-typescript-sdk/${SDK_VERSION}`
|
|
786
809
|
});
|
|
787
810
|
}
|
|
788
811
|
static forCloud(options) {
|
|
789
812
|
return new _CloudClient(options);
|
|
790
813
|
}
|
|
791
814
|
close() {
|
|
792
|
-
this.
|
|
815
|
+
this.transport.close();
|
|
793
816
|
}
|
|
794
817
|
async upsertApplication(manifest, codeZip, upgradeRunningRequests = false) {
|
|
795
818
|
const applicationManifest = { ...manifest };
|
|
@@ -817,18 +840,18 @@ var init_cloud_client = __esm({
|
|
|
817
840
|
"upgrade_requests_to_latest_code",
|
|
818
841
|
String(upgradeRunningRequests)
|
|
819
842
|
);
|
|
820
|
-
await this.
|
|
843
|
+
await this.transport.requestResponse("POST", this.namespacePath("applications"), {
|
|
821
844
|
body: form
|
|
822
845
|
});
|
|
823
846
|
}
|
|
824
847
|
async deleteApplication(applicationName) {
|
|
825
|
-
await this.
|
|
848
|
+
await this.transport.requestResponse(
|
|
826
849
|
"DELETE",
|
|
827
850
|
this.namespacePath(`applications/${encodeURIComponent(applicationName)}`)
|
|
828
851
|
);
|
|
829
852
|
}
|
|
830
853
|
async deleteSandboxImage(imageName) {
|
|
831
|
-
await this.
|
|
854
|
+
await this.transport.requestResponse(
|
|
832
855
|
"DELETE",
|
|
833
856
|
this.namespacePath(`sandbox-images/${encodeURIComponent(imageName)}`)
|
|
834
857
|
);
|
|
@@ -847,10 +870,10 @@ var init_cloud_client = __esm({
|
|
|
847
870
|
options?.organizationId,
|
|
848
871
|
options?.projectId
|
|
849
872
|
);
|
|
850
|
-
const response = await this.
|
|
873
|
+
const response = await this.transport.requestResponse(
|
|
851
874
|
"GET",
|
|
852
875
|
`${base}/by-name/${encodeURIComponent(imageName)}`,
|
|
853
|
-
{
|
|
876
|
+
{ statusOnlyCodes: /* @__PURE__ */ new Set([404]) }
|
|
854
877
|
);
|
|
855
878
|
if (response.status === 404) {
|
|
856
879
|
return null;
|
|
@@ -872,7 +895,7 @@ var init_cloud_client = __esm({
|
|
|
872
895
|
let path3 = `${base}?pageSize=100`;
|
|
873
896
|
const templates = [];
|
|
874
897
|
while (path3 !== null) {
|
|
875
|
-
const page = await this.
|
|
898
|
+
const page = await this.transport.requestJson(
|
|
876
899
|
"GET",
|
|
877
900
|
path3
|
|
878
901
|
);
|
|
@@ -885,7 +908,7 @@ var init_cloud_client = __esm({
|
|
|
885
908
|
return templates;
|
|
886
909
|
}
|
|
887
910
|
async applications() {
|
|
888
|
-
const raw = await this.
|
|
911
|
+
const raw = await this.transport.requestJson(
|
|
889
912
|
"GET",
|
|
890
913
|
this.namespacePath("applications")
|
|
891
914
|
);
|
|
@@ -894,7 +917,7 @@ var init_cloud_client = __esm({
|
|
|
894
917
|
);
|
|
895
918
|
}
|
|
896
919
|
async applicationManifest(applicationName) {
|
|
897
|
-
const raw = await this.
|
|
920
|
+
const raw = await this.transport.requestJson(
|
|
898
921
|
"GET",
|
|
899
922
|
this.namespacePath(`applications/${encodeURIComponent(applicationName)}`)
|
|
900
923
|
);
|
|
@@ -904,10 +927,10 @@ var init_cloud_client = __esm({
|
|
|
904
927
|
const path3 = this.namespacePath(
|
|
905
928
|
`applications/${encodeURIComponent(applicationName)}`
|
|
906
929
|
);
|
|
907
|
-
const response = inputs.length === 0 ? await this.
|
|
930
|
+
const response = inputs.length === 0 ? await this.transport.requestResponse("POST", path3, {
|
|
908
931
|
body: new Uint8Array(),
|
|
909
932
|
headers: { Accept: "application/json" }
|
|
910
|
-
}) : inputs.length === 1 && inputs[0].name === "0" ? await this.
|
|
933
|
+
}) : inputs.length === 1 && inputs[0].name === "0" ? await this.transport.requestResponse("POST", path3, {
|
|
911
934
|
body: toRequestBody(inputs[0].data),
|
|
912
935
|
headers: {
|
|
913
936
|
Accept: "application/json",
|
|
@@ -922,13 +945,13 @@ var init_cloud_client = __esm({
|
|
|
922
945
|
return requestId;
|
|
923
946
|
}
|
|
924
947
|
async waitOnRequestCompletion(applicationName, requestId) {
|
|
925
|
-
const stream =
|
|
948
|
+
const stream = this.transport.stream(
|
|
926
949
|
"GET",
|
|
927
950
|
this.namespacePath(
|
|
928
951
|
`applications/${encodeURIComponent(applicationName)}/requests/${encodeURIComponent(requestId)}/progress`
|
|
929
952
|
)
|
|
930
953
|
);
|
|
931
|
-
for await (const event of
|
|
954
|
+
for await (const event of stream) {
|
|
932
955
|
if (Object.prototype.hasOwnProperty.call(event, "RequestFinished")) {
|
|
933
956
|
return;
|
|
934
957
|
}
|
|
@@ -936,7 +959,7 @@ var init_cloud_client = __esm({
|
|
|
936
959
|
throw new Error("progress stream ended before request completion");
|
|
937
960
|
}
|
|
938
961
|
async requestMetadata(applicationName, requestId) {
|
|
939
|
-
const raw = await this.
|
|
962
|
+
const raw = await this.transport.requestJson(
|
|
940
963
|
"GET",
|
|
941
964
|
this.namespacePath(
|
|
942
965
|
`applications/${encodeURIComponent(applicationName)}/requests/${encodeURIComponent(requestId)}`
|
|
@@ -945,7 +968,7 @@ var init_cloud_client = __esm({
|
|
|
945
968
|
return fromSnakeKeys(raw);
|
|
946
969
|
}
|
|
947
970
|
async requestOutput(applicationName, requestId) {
|
|
948
|
-
const response = await this.
|
|
971
|
+
const response = await this.transport.requestResponse(
|
|
949
972
|
"GET",
|
|
950
973
|
this.namespacePath(
|
|
951
974
|
`applications/${encodeURIComponent(applicationName)}/requests/${encodeURIComponent(requestId)}/output`
|
|
@@ -963,7 +986,7 @@ var init_cloud_client = __esm({
|
|
|
963
986
|
options?.organizationId,
|
|
964
987
|
options?.projectId
|
|
965
988
|
);
|
|
966
|
-
const raw = await this.
|
|
989
|
+
const raw = await this.transport.requestJson("GET", base);
|
|
967
990
|
const pageSize = Math.max(0, options?.pageSize ?? 100);
|
|
968
991
|
return Object.assign(
|
|
969
992
|
{
|
|
@@ -978,7 +1001,7 @@ var init_cloud_client = __esm({
|
|
|
978
1001
|
options?.organizationId,
|
|
979
1002
|
options?.projectId
|
|
980
1003
|
);
|
|
981
|
-
const raw = await this.
|
|
1004
|
+
const raw = await this.transport.requestJson(
|
|
982
1005
|
"GET",
|
|
983
1006
|
`${base}/${encodeURIComponent(secretId)}`
|
|
984
1007
|
);
|
|
@@ -1003,24 +1026,24 @@ var init_cloud_client = __esm({
|
|
|
1003
1026
|
options?.organizationId,
|
|
1004
1027
|
options?.projectId
|
|
1005
1028
|
);
|
|
1006
|
-
await this.
|
|
1029
|
+
await this.transport.requestResponse(
|
|
1007
1030
|
"DELETE",
|
|
1008
1031
|
`${base}/${encodeURIComponent(secretId)}`
|
|
1009
1032
|
);
|
|
1010
1033
|
}
|
|
1011
1034
|
async upsertSecret(base, secret) {
|
|
1012
|
-
let response = await this.
|
|
1035
|
+
let response = await this.transport.requestResponse("POST", base, {
|
|
1013
1036
|
json: secret,
|
|
1014
1037
|
headers: { "Idempotency-Key": (0, import_nanoid.nanoid)() },
|
|
1015
|
-
|
|
1038
|
+
statusOnlyCodes: /* @__PURE__ */ new Set([409])
|
|
1016
1039
|
});
|
|
1017
1040
|
if (response.status === 409) {
|
|
1018
1041
|
const prefix = base.slice(0, -"/secrets".length);
|
|
1019
|
-
const existing = await this.
|
|
1042
|
+
const existing = await this.transport.requestJson(
|
|
1020
1043
|
"GET",
|
|
1021
1044
|
`${prefix}/secret-names/${encodeURIComponent(secret.name)}`
|
|
1022
1045
|
);
|
|
1023
|
-
response = await this.
|
|
1046
|
+
response = await this.transport.requestResponse(
|
|
1024
1047
|
"POST",
|
|
1025
1048
|
`${base}/${encodeURIComponent(existing.id)}/versions`,
|
|
1026
1049
|
{
|
|
@@ -1046,9 +1069,9 @@ var init_cloud_client = __esm({
|
|
|
1046
1069
|
new Blob([toBlobPart(request.buildContext)]),
|
|
1047
1070
|
"context.tar.gz"
|
|
1048
1071
|
);
|
|
1049
|
-
const response = await this.
|
|
1072
|
+
const response = await this.transport.requestResponse(
|
|
1050
1073
|
"PUT",
|
|
1051
|
-
`${
|
|
1074
|
+
`${trimTrailingSlashes2(buildServicePath)}/builds`,
|
|
1052
1075
|
{ body: form }
|
|
1053
1076
|
);
|
|
1054
1077
|
const raw = await parseJsonResponse(response);
|
|
@@ -1056,48 +1079,48 @@ var init_cloud_client = __esm({
|
|
|
1056
1079
|
}
|
|
1057
1080
|
async createApplicationBuild(buildServicePath, request, imageContexts) {
|
|
1058
1081
|
const form = createApplicationBuildForm(request, imageContexts);
|
|
1059
|
-
const response = await this.
|
|
1082
|
+
const response = await this.transport.requestResponse(
|
|
1060
1083
|
"POST",
|
|
1061
|
-
|
|
1084
|
+
trimTrailingSlashes2(buildServicePath),
|
|
1062
1085
|
{ body: form }
|
|
1063
1086
|
);
|
|
1064
1087
|
const raw = await parseJsonResponse(response);
|
|
1065
1088
|
return fromSnakeKeys(raw);
|
|
1066
1089
|
}
|
|
1067
1090
|
async applicationBuildInfo(buildServicePath, applicationBuildId) {
|
|
1068
|
-
const raw = await this.
|
|
1091
|
+
const raw = await this.transport.requestJson(
|
|
1069
1092
|
"GET",
|
|
1070
|
-
`${
|
|
1093
|
+
`${trimTrailingSlashes2(buildServicePath)}/${encodeURIComponent(applicationBuildId)}`
|
|
1071
1094
|
);
|
|
1072
1095
|
return fromSnakeKeys(raw);
|
|
1073
1096
|
}
|
|
1074
1097
|
async cancelApplicationBuild(buildServicePath, applicationBuildId) {
|
|
1075
|
-
const raw = await this.
|
|
1098
|
+
const raw = await this.transport.requestJson(
|
|
1076
1099
|
"POST",
|
|
1077
|
-
`${
|
|
1100
|
+
`${trimTrailingSlashes2(buildServicePath)}/${encodeURIComponent(applicationBuildId)}/cancel`
|
|
1078
1101
|
);
|
|
1079
1102
|
return fromSnakeKeys(raw);
|
|
1080
1103
|
}
|
|
1081
1104
|
async buildInfo(buildServicePath, buildId) {
|
|
1082
|
-
const raw = await this.
|
|
1105
|
+
const raw = await this.transport.requestJson(
|
|
1083
1106
|
"GET",
|
|
1084
|
-
`${
|
|
1107
|
+
`${trimTrailingSlashes2(buildServicePath)}/builds/${encodeURIComponent(buildId)}`
|
|
1085
1108
|
);
|
|
1086
1109
|
return fromSnakeKeys(raw);
|
|
1087
1110
|
}
|
|
1088
1111
|
async cancelBuild(buildServicePath, buildId) {
|
|
1089
|
-
await this.
|
|
1112
|
+
await this.transport.requestResponse(
|
|
1090
1113
|
"POST",
|
|
1091
|
-
`${
|
|
1114
|
+
`${trimTrailingSlashes2(buildServicePath)}/builds/${encodeURIComponent(buildId)}/cancel`
|
|
1092
1115
|
);
|
|
1093
1116
|
}
|
|
1094
1117
|
async *streamBuildLogs(buildServicePath, buildId, signal) {
|
|
1095
|
-
const stream =
|
|
1118
|
+
const stream = this.transport.stream(
|
|
1096
1119
|
"GET",
|
|
1097
|
-
`${
|
|
1098
|
-
|
|
1120
|
+
`${trimTrailingSlashes2(buildServicePath)}/builds/${encodeURIComponent(buildId)}/logs`,
|
|
1121
|
+
signal
|
|
1099
1122
|
);
|
|
1100
|
-
for await (const event of
|
|
1123
|
+
for await (const event of stream) {
|
|
1101
1124
|
yield fromSnakeKeys(event);
|
|
1102
1125
|
}
|
|
1103
1126
|
}
|
|
@@ -1110,7 +1133,7 @@ var init_cloud_client = __esm({
|
|
|
1110
1133
|
input.name
|
|
1111
1134
|
);
|
|
1112
1135
|
}
|
|
1113
|
-
return this.
|
|
1136
|
+
return this.transport.requestResponse("POST", path3, {
|
|
1114
1137
|
body: form,
|
|
1115
1138
|
headers: { Accept: "application/json" }
|
|
1116
1139
|
});
|
|
@@ -1119,10 +1142,10 @@ var init_cloud_client = __esm({
|
|
|
1119
1142
|
return `/v1/namespaces/${encodeURIComponent(this.namespace)}/${subpath.replace(/^\/+/, "")}`;
|
|
1120
1143
|
}
|
|
1121
1144
|
async existingApplicationPublicEndpointId(applicationName) {
|
|
1122
|
-
const response = await this.
|
|
1145
|
+
const response = await this.transport.requestResponse(
|
|
1123
1146
|
"GET",
|
|
1124
1147
|
this.namespacePath(`applications/${encodeURIComponent(applicationName)}`),
|
|
1125
|
-
{
|
|
1148
|
+
{ statusOnlyCodes: /* @__PURE__ */ new Set([404]) }
|
|
1126
1149
|
);
|
|
1127
1150
|
if (response.status === 404) {
|
|
1128
1151
|
return void 0;
|
|
@@ -1390,7 +1413,7 @@ function nativePackageName(platform = process.platform, arch = process.arch, opt
|
|
|
1390
1413
|
function loadNative() {
|
|
1391
1414
|
const target = nativeTargetId();
|
|
1392
1415
|
const bindingPath = nativeBindingPath();
|
|
1393
|
-
const require2 = (0,
|
|
1416
|
+
const require2 = (0, import_node_module2.createRequire)(importMetaUrl);
|
|
1394
1417
|
if (fs.existsSync(bindingPath)) return require2(bindingPath);
|
|
1395
1418
|
const packageName = nativePackageName();
|
|
1396
1419
|
if (!packageName) {
|
|
@@ -1409,14 +1432,14 @@ function loadNative() {
|
|
|
1409
1432
|
}
|
|
1410
1433
|
return require2(packageEntry);
|
|
1411
1434
|
}
|
|
1412
|
-
var import_libc, fs,
|
|
1435
|
+
var import_libc, fs, import_node_module2, import_node_path, import_node_url, NATIVE_PACKAGES, cachedLinuxLibc;
|
|
1413
1436
|
var init_native_binding = __esm({
|
|
1414
1437
|
"src/native-binding.ts"() {
|
|
1415
1438
|
"use strict";
|
|
1416
1439
|
init_cjs_shims();
|
|
1417
1440
|
import_libc = __toESM(require_libc(), 1);
|
|
1418
1441
|
fs = __toESM(require("fs"), 1);
|
|
1419
|
-
|
|
1442
|
+
import_node_module2 = require("module");
|
|
1420
1443
|
import_node_path = __toESM(require("path"), 1);
|
|
1421
1444
|
import_node_url = require("url");
|
|
1422
1445
|
NATIVE_PACKAGES = {
|