@celestoai/sdk 0.1.6 → 0.1.7
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/CHANGELOG.md +24 -0
- package/README.md +89 -270
- package/dist/{chunk-B4JRBDUO.js → chunk-3HEUI5UL.js} +278 -304
- package/dist/chunk-3HEUI5UL.js.map +1 -0
- package/dist/computers/index.cjs +148 -345
- package/dist/computers/index.cjs.map +1 -1
- package/dist/computers/index.d.cts +1 -270
- package/dist/computers/index.d.ts +1 -270
- package/dist/computers/index.js +1 -2
- package/dist/index-59d7VJXL.d.cts +303 -0
- package/dist/index-59d7VJXL.d.ts +303 -0
- package/dist/index.cjs +191 -1573
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +22 -4
- package/dist/index.d.ts +22 -4
- package/dist/index.js +5 -61
- package/package.json +25 -22
- package/dist/agents/index.cjs +0 -1226
- package/dist/agents/index.cjs.map +0 -1
- package/dist/agents/index.d.cts +0 -2
- package/dist/agents/index.d.ts +0 -2
- package/dist/agents/index.js +0 -60
- package/dist/agents/index.js.map +0 -1
- package/dist/chunk-6Y2YZCPU.js +0 -1007
- package/dist/chunk-6Y2YZCPU.js.map +0 -1
- package/dist/chunk-B4JRBDUO.js.map +0 -1
- package/dist/chunk-P7F52CSQ.js +0 -177
- package/dist/chunk-P7F52CSQ.js.map +0 -1
- package/dist/chunk-THPVQCTE.js +0 -183
- package/dist/chunk-THPVQCTE.js.map +0 -1
- package/dist/config-dDPvsH-K.d.cts +0 -42
- package/dist/config-dDPvsH-K.d.ts +0 -42
- package/dist/gatekeeper/index.cjs +0 -348
- package/dist/gatekeeper/index.cjs.map +0 -1
- package/dist/gatekeeper/index.d.cts +0 -104
- package/dist/gatekeeper/index.d.ts +0 -104
- package/dist/gatekeeper/index.js +0 -8
- package/dist/gatekeeper/index.js.map +0 -1
- package/dist/index-Cii1_OFg.d.ts +0 -696
- package/dist/index-CsEA7qMF.d.cts +0 -696
package/dist/computers/index.cjs
CHANGED
|
@@ -38,12 +38,23 @@ __export(computers_exports, {
|
|
|
38
38
|
module.exports = __toCommonJS(computers_exports);
|
|
39
39
|
|
|
40
40
|
// src/core/auth.ts
|
|
41
|
-
var import_node_child_process = require("child_process");
|
|
42
41
|
var import_promises = require("fs/promises");
|
|
42
|
+
var import_node_os = require("os");
|
|
43
43
|
var import_node_path = __toESM(require("path"), 1);
|
|
44
|
-
var import_node_util = require("util");
|
|
45
44
|
var import_dotenv = require("dotenv");
|
|
46
45
|
|
|
46
|
+
// src/core/config.ts
|
|
47
|
+
var DEFAULT_BASE_URL = "https://api.celesto.ai";
|
|
48
|
+
var buildRequestContext = (config) => ({
|
|
49
|
+
fetch: config.fetch ?? fetch,
|
|
50
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
|
|
51
|
+
token: config.token ?? config.apiKey,
|
|
52
|
+
organizationId: config.organizationId,
|
|
53
|
+
userAgent: config.userAgent,
|
|
54
|
+
timeoutMs: config.timeoutMs,
|
|
55
|
+
headers: config.headers
|
|
56
|
+
});
|
|
57
|
+
|
|
47
58
|
// src/core/errors.ts
|
|
48
59
|
var CelestoError = class extends Error {
|
|
49
60
|
constructor(message) {
|
|
@@ -68,9 +79,13 @@ var CelestoNetworkError = class extends CelestoError {
|
|
|
68
79
|
this.cause = cause;
|
|
69
80
|
}
|
|
70
81
|
};
|
|
82
|
+
function toNetworkError(cause) {
|
|
83
|
+
if (cause instanceof CelestoNetworkError) return cause;
|
|
84
|
+
const error = cause instanceof Error ? cause : new Error(String(cause));
|
|
85
|
+
return new CelestoNetworkError(error.message, error);
|
|
86
|
+
}
|
|
71
87
|
|
|
72
88
|
// src/core/auth.ts
|
|
73
|
-
var execFile = (0, import_node_util.promisify)(import_node_child_process.execFile);
|
|
74
89
|
var MISSING_CREDENTIALS_MESSAGE = "No Celesto credentials found. Run pip install celesto && celesto auth login, or export CELESTO_API_KEY.";
|
|
75
90
|
function nonEmpty(value) {
|
|
76
91
|
const trimmed = value?.trim();
|
|
@@ -84,47 +99,50 @@ async function readDotEnvApiKey(cwd, readDotEnv) {
|
|
|
84
99
|
return void 0;
|
|
85
100
|
}
|
|
86
101
|
}
|
|
87
|
-
async function
|
|
102
|
+
async function loadCliCredentials(home) {
|
|
88
103
|
try {
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
104
|
+
const saved = JSON.parse(await (0, import_promises.readFile)(import_node_path.default.join(home, ".celesto", "credentials"), "utf8"));
|
|
105
|
+
const token = typeof saved?.api_key === "string" ? nonEmpty(saved.api_key) : void 0;
|
|
106
|
+
if (!token) return void 0;
|
|
107
|
+
const url = new URL(nonEmpty(typeof saved.base_url === "string" ? saved.base_url : void 0) ?? DEFAULT_BASE_URL);
|
|
108
|
+
const local = url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
|
|
109
|
+
if (!local && url.protocol !== "https:" || url.username || url.password || url.pathname !== "/" || url.search || url.hash) return void 0;
|
|
110
|
+
return { token, baseUrl: url.origin };
|
|
95
111
|
} catch {
|
|
96
112
|
return void 0;
|
|
97
113
|
}
|
|
98
114
|
}
|
|
99
|
-
async function
|
|
115
|
+
async function resolveCredentials(options) {
|
|
100
116
|
const env = options.env ?? process.env;
|
|
101
117
|
const environmentKey = nonEmpty(env.CELESTO_API_KEY);
|
|
102
|
-
if (environmentKey) return environmentKey;
|
|
118
|
+
if (environmentKey) return { token: environmentKey };
|
|
103
119
|
const dotEnvKey = await readDotEnvApiKey(
|
|
104
120
|
options.cwd ?? process.cwd(),
|
|
105
121
|
options.readDotEnv ?? ((filePath) => (0, import_promises.readFile)(filePath, "utf8"))
|
|
106
122
|
);
|
|
107
|
-
if (dotEnvKey) return dotEnvKey;
|
|
108
|
-
|
|
123
|
+
if (dotEnvKey) return { token: dotEnvKey };
|
|
124
|
+
if (options.loadSavedApiKey) {
|
|
125
|
+
const token = await options.loadSavedApiKey();
|
|
126
|
+
return token ? { token } : void 0;
|
|
127
|
+
}
|
|
128
|
+
return loadCliCredentials(options.homeDir ?? (0, import_node_os.homedir)());
|
|
129
|
+
}
|
|
130
|
+
async function resolveCelestoApiKey(options = {}) {
|
|
131
|
+
return (await resolveCredentials(options))?.token;
|
|
109
132
|
}
|
|
110
133
|
async function resolveClientConfig(config = {}, options = {}) {
|
|
111
134
|
if (nonEmpty(config.token) || nonEmpty(config.apiKey)) return config;
|
|
112
|
-
const
|
|
113
|
-
if (!
|
|
114
|
-
|
|
135
|
+
const credentials = await resolveCredentials(options);
|
|
136
|
+
if (!credentials) throw new CelestoError(MISSING_CREDENTIALS_MESSAGE);
|
|
137
|
+
if (credentials.baseUrl && config.baseUrl !== void 0 && new URL(config.baseUrl).origin !== credentials.baseUrl) {
|
|
138
|
+
throw new CelestoError("Your saved Celesto key is for a different API URL. Run 'celesto auth login' for this URL or pass an API key explicitly.");
|
|
139
|
+
}
|
|
140
|
+
const baseUrl = config.baseUrl ?? credentials.baseUrl;
|
|
141
|
+
return { ...config, token: credentials.token, ...baseUrl === void 0 ? {} : { baseUrl } };
|
|
115
142
|
}
|
|
116
143
|
|
|
117
|
-
// src/
|
|
118
|
-
var
|
|
119
|
-
var buildRequestContext = (config) => ({
|
|
120
|
-
fetch: config.fetch ?? fetch,
|
|
121
|
-
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
|
|
122
|
-
token: config.token ?? config.apiKey,
|
|
123
|
-
organizationId: config.organizationId,
|
|
124
|
-
userAgent: config.userAgent,
|
|
125
|
-
timeoutMs: config.timeoutMs,
|
|
126
|
-
headers: config.headers
|
|
127
|
-
});
|
|
144
|
+
// src/computers/client.ts
|
|
145
|
+
var import_node_crypto = require("crypto");
|
|
128
146
|
|
|
129
147
|
// src/core/http.ts
|
|
130
148
|
var joinUrl = (baseUrl, path2) => {
|
|
@@ -194,8 +212,7 @@ var buildRequestInit = (ctx, options) => {
|
|
|
194
212
|
}
|
|
195
213
|
const init = {
|
|
196
214
|
method: options.method,
|
|
197
|
-
headers
|
|
198
|
-
signal: options.signal
|
|
215
|
+
headers
|
|
199
216
|
};
|
|
200
217
|
if (options.body !== void 0) {
|
|
201
218
|
headers["Content-Type"] = headers["Content-Type"] ?? "application/json";
|
|
@@ -203,17 +220,30 @@ var buildRequestInit = (ctx, options) => {
|
|
|
203
220
|
}
|
|
204
221
|
return { url, init };
|
|
205
222
|
};
|
|
223
|
+
function requestDeadline(signal, timeoutMs) {
|
|
224
|
+
if (!timeoutMs || timeoutMs <= 0 || signal?.aborted) return { signal, cleanup: () => {
|
|
225
|
+
} };
|
|
226
|
+
const controller = new AbortController();
|
|
227
|
+
const abort = () => controller.abort(signal?.reason);
|
|
228
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
229
|
+
const timer = setTimeout(
|
|
230
|
+
() => controller.abort(new DOMException("Celesto request timed out.", "TimeoutError")),
|
|
231
|
+
timeoutMs
|
|
232
|
+
);
|
|
233
|
+
return {
|
|
234
|
+
signal: controller.signal,
|
|
235
|
+
cleanup: () => {
|
|
236
|
+
clearTimeout(timer);
|
|
237
|
+
signal?.removeEventListener("abort", abort);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
206
241
|
var request = async (ctx, options) => {
|
|
207
242
|
const { url, init } = buildRequestInit(ctx, options);
|
|
208
|
-
|
|
209
|
-
let controller;
|
|
210
|
-
if (!options.signal && ctx.timeoutMs && ctx.timeoutMs > 0) {
|
|
211
|
-
controller = new AbortController();
|
|
212
|
-
timeoutId = setTimeout(() => controller?.abort(), ctx.timeoutMs);
|
|
213
|
-
init.signal = controller.signal;
|
|
214
|
-
}
|
|
243
|
+
const { signal, cleanup } = requestDeadline(options.signal, ctx.timeoutMs);
|
|
215
244
|
try {
|
|
216
|
-
|
|
245
|
+
if (signal?.aborted) throw signal.reason;
|
|
246
|
+
const response = await ctx.fetch(url, { ...init, signal });
|
|
217
247
|
const data = await parseResponseBody(response);
|
|
218
248
|
if (!response.ok) {
|
|
219
249
|
const message = extractErrorMessage(data, response.status);
|
|
@@ -230,36 +260,9 @@ var request = async (ctx, options) => {
|
|
|
230
260
|
if (err instanceof CelestoApiError) {
|
|
231
261
|
throw err;
|
|
232
262
|
}
|
|
233
|
-
|
|
234
|
-
throw new CelestoNetworkError(error.message, error);
|
|
263
|
+
throw toNetworkError(err);
|
|
235
264
|
} finally {
|
|
236
|
-
|
|
237
|
-
clearTimeout(timeoutId);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
var requestStream = async (ctx, options) => {
|
|
242
|
-
const { url, init } = buildRequestInit(ctx, options);
|
|
243
|
-
try {
|
|
244
|
-
const response = await ctx.fetch(url, init);
|
|
245
|
-
if (!response.ok) {
|
|
246
|
-
const data = await parseResponseBody(response);
|
|
247
|
-
const message = extractErrorMessage(data, response.status);
|
|
248
|
-
throw new CelestoApiError(
|
|
249
|
-
message,
|
|
250
|
-
response.status,
|
|
251
|
-
data,
|
|
252
|
-
response.headers.get("x-request-id") ?? void 0,
|
|
253
|
-
response.headers
|
|
254
|
-
);
|
|
255
|
-
}
|
|
256
|
-
return response;
|
|
257
|
-
} catch (err) {
|
|
258
|
-
if (err instanceof CelestoApiError) {
|
|
259
|
-
throw err;
|
|
260
|
-
}
|
|
261
|
-
const error = err instanceof Error ? err : new Error(String(err));
|
|
262
|
-
throw new CelestoNetworkError(error.message, error);
|
|
265
|
+
cleanup();
|
|
263
266
|
}
|
|
264
267
|
};
|
|
265
268
|
|
|
@@ -289,6 +292,7 @@ var toComputerInfo = (payload) => ({
|
|
|
289
292
|
id: payload.id,
|
|
290
293
|
name: payload.name,
|
|
291
294
|
status: payload.status,
|
|
295
|
+
...payload.size_id === void 0 ? {} : { sizeId: payload.size_id },
|
|
292
296
|
vcpus: payload.vcpus,
|
|
293
297
|
ramMb: payload.ram_mb,
|
|
294
298
|
diskSizeMb: payload.disk_size_mb,
|
|
@@ -381,92 +385,36 @@ var toSandboxTemplateInfo = (payload) => ({
|
|
|
381
385
|
version: payload.version ?? null,
|
|
382
386
|
experimental: payload.experimental
|
|
383
387
|
});
|
|
384
|
-
var
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
var parseDiskString = (disk) => {
|
|
400
|
-
const value = disk.trim();
|
|
401
|
-
if (!value) {
|
|
402
|
-
return void 0;
|
|
403
|
-
}
|
|
404
|
-
let index = 0;
|
|
405
|
-
let digitCount = 0;
|
|
406
|
-
let hasDecimalPoint = false;
|
|
407
|
-
while (index < value.length) {
|
|
408
|
-
const char = value[index];
|
|
409
|
-
if (isAsciiDigit(char)) {
|
|
410
|
-
digitCount += 1;
|
|
411
|
-
index += 1;
|
|
412
|
-
continue;
|
|
388
|
+
var resourceOverrides = ["cpus", "vcpus", "memory", "ramMb", "disk", "diskSizeMb"];
|
|
389
|
+
var createOptions = /* @__PURE__ */ new Set([
|
|
390
|
+
"sizeId",
|
|
391
|
+
"idempotencyKey",
|
|
392
|
+
"networkPolicy",
|
|
393
|
+
"image",
|
|
394
|
+
"templateId",
|
|
395
|
+
"templateVersion",
|
|
396
|
+
"persistentHome",
|
|
397
|
+
...resourceOverrides
|
|
398
|
+
]);
|
|
399
|
+
var buildCreateComputerBody = (params) => {
|
|
400
|
+
for (const field of Object.keys(params)) {
|
|
401
|
+
if (field === "size" || field === "size_id") {
|
|
402
|
+
throw new TypeError(`${field} is not a Cloud computer create option. Use sizeId instead.`);
|
|
413
403
|
}
|
|
414
|
-
if (
|
|
415
|
-
|
|
416
|
-
index += 1;
|
|
417
|
-
continue;
|
|
404
|
+
if (!createOptions.has(field)) {
|
|
405
|
+
throw new TypeError(`Unknown computer create option ${JSON.stringify(field)}. Remove it and retry.`);
|
|
418
406
|
}
|
|
419
|
-
break;
|
|
420
|
-
}
|
|
421
|
-
if (digitCount === 0 || value[index - 1] === ".") {
|
|
422
|
-
return void 0;
|
|
423
407
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
index += 1;
|
|
427
|
-
}
|
|
428
|
-
const unit = value.slice(index);
|
|
429
|
-
if ([...unit].some((char) => !isAsciiLetter(char))) {
|
|
430
|
-
return void 0;
|
|
431
|
-
}
|
|
432
|
-
return { amount, unit };
|
|
433
|
-
};
|
|
434
|
-
var parseDiskSizeMb = (disk) => {
|
|
435
|
-
if (disk === void 0) {
|
|
436
|
-
return void 0;
|
|
408
|
+
if (resourceOverrides.some((field) => params[field] !== void 0)) {
|
|
409
|
+
throw new TypeError("Cloud computers do not accept CPU, memory, or disk overrides. Use sizeId instead.");
|
|
437
410
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
411
|
+
const body = {};
|
|
412
|
+
if (params.sizeId !== void 0) {
|
|
413
|
+
if (typeof params.sizeId !== "string" || !params.sizeId.trim() || params.sizeId.length > 32) {
|
|
414
|
+
throw new TypeError('Choose a sizeId of 1\u201332 characters, such as "micro".');
|
|
441
415
|
}
|
|
442
|
-
|
|
443
|
-
}
|
|
444
|
-
const parsedDisk = parseDiskString(disk);
|
|
445
|
-
if (!parsedDisk) {
|
|
446
|
-
throw new Error("disk must be a size in MB or a string like '2gb'.");
|
|
447
|
-
}
|
|
448
|
-
const multiplier = diskUnitsToMb[parsedDisk.unit.toLowerCase()];
|
|
449
|
-
if (multiplier === void 0) {
|
|
450
|
-
throw new Error("disk must use MB, GB, or TB, for example '2048mb' or '2gb'.");
|
|
451
|
-
}
|
|
452
|
-
const sizeMb = Number(parsedDisk.amount) * multiplier;
|
|
453
|
-
if (!Number.isInteger(sizeMb) || sizeMb <= 0) {
|
|
454
|
-
throw new Error("disk must resolve to a whole number of MB, for example '1536mb' or '1.5gb'.");
|
|
455
|
-
}
|
|
456
|
-
return sizeMb;
|
|
457
|
-
};
|
|
458
|
-
var buildCreateComputerBody = (params) => {
|
|
459
|
-
if (params.cpus !== void 0 && params.vcpus !== void 0 && params.cpus !== params.vcpus) {
|
|
460
|
-
throw new Error("cpus and vcpus must have the same value when both are provided.");
|
|
461
|
-
}
|
|
462
|
-
if (params.memory !== void 0 && params.ramMb !== void 0 && params.memory !== params.ramMb) {
|
|
463
|
-
throw new Error("memory and ramMb must have the same value when both are provided.");
|
|
464
|
-
}
|
|
465
|
-
const parsedDiskSizeMb = parseDiskSizeMb(params.disk);
|
|
466
|
-
if (parsedDiskSizeMb !== void 0 && params.diskSizeMb !== void 0 && parsedDiskSizeMb !== params.diskSizeMb) {
|
|
467
|
-
throw new Error("disk and diskSizeMb must have the same value when both are provided.");
|
|
416
|
+
body.size_id = params.sizeId;
|
|
468
417
|
}
|
|
469
|
-
const body = {};
|
|
470
418
|
if (params.networkPolicy !== void 0) {
|
|
471
419
|
const policy = params.networkPolicy;
|
|
472
420
|
if (!policy || Object.keys(policy).length !== 1 || !("mode" in policy) || !["open", "off"].includes(policy.mode)) {
|
|
@@ -477,18 +425,6 @@ var buildCreateComputerBody = (params) => {
|
|
|
477
425
|
}
|
|
478
426
|
body.network_policy = { mode: policy.mode };
|
|
479
427
|
}
|
|
480
|
-
const vcpus = params.vcpus ?? params.cpus;
|
|
481
|
-
const ramMb = params.ramMb ?? params.memory;
|
|
482
|
-
const diskSizeMb = params.diskSizeMb ?? parsedDiskSizeMb;
|
|
483
|
-
if (vcpus !== void 0) {
|
|
484
|
-
body.vcpus = vcpus;
|
|
485
|
-
}
|
|
486
|
-
if (ramMb !== void 0) {
|
|
487
|
-
body.ram_mb = ramMb;
|
|
488
|
-
}
|
|
489
|
-
if (diskSizeMb !== void 0) {
|
|
490
|
-
body.disk_size_mb = diskSizeMb;
|
|
491
|
-
}
|
|
492
428
|
if (params.image !== void 0) {
|
|
493
429
|
body.image = params.image;
|
|
494
430
|
}
|
|
@@ -508,110 +444,31 @@ var pickOverrides = (options) => ({
|
|
|
508
444
|
headers: options?.headers,
|
|
509
445
|
signal: options?.signal
|
|
510
446
|
});
|
|
511
|
-
var optionalNumber = (value) => typeof value === "number" ? value : void 0;
|
|
512
|
-
var optionalBoolean = (value) => typeof value === "boolean" ? value : void 0;
|
|
513
|
-
var parseExecStreamEvent = (value) => {
|
|
514
|
-
if (!value || typeof value !== "object") {
|
|
515
|
-
throw new Error("Celesto returned an invalid command stream event.");
|
|
516
|
-
}
|
|
517
|
-
const event = value;
|
|
518
|
-
if (event.type === "stdout" || event.type === "stderr") {
|
|
519
|
-
if (typeof event.data !== "string") {
|
|
520
|
-
throw new Error("Celesto returned command output without text data.");
|
|
521
|
-
}
|
|
522
|
-
return { type: event.type, data: event.data };
|
|
523
|
-
}
|
|
524
|
-
if (event.type === "started") {
|
|
525
|
-
if (typeof event.command_id !== "string" || typeof event.started_at_unix_ms !== "number" || typeof event.timeout_seconds !== "number") {
|
|
526
|
-
throw new Error("Celesto returned an invalid command-start event.");
|
|
527
|
-
}
|
|
528
|
-
return {
|
|
529
|
-
type: "started",
|
|
530
|
-
commandId: event.command_id,
|
|
531
|
-
startedAtUnixMs: event.started_at_unix_ms,
|
|
532
|
-
timeoutSeconds: event.timeout_seconds
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
if (event.type === "exit") {
|
|
536
|
-
if (typeof event.exit_code !== "number") {
|
|
537
|
-
throw new Error("Celesto returned an invalid command-exit event.");
|
|
538
|
-
}
|
|
539
|
-
const result = {
|
|
540
|
-
type: "exit",
|
|
541
|
-
exitCode: event.exit_code
|
|
542
|
-
};
|
|
543
|
-
if (typeof event.command_id === "string") result.commandId = event.command_id;
|
|
544
|
-
const startedAtUnixMs = optionalNumber(event.started_at_unix_ms);
|
|
545
|
-
const endedAtUnixMs = optionalNumber(event.ended_at_unix_ms);
|
|
546
|
-
const durationMs = optionalNumber(event.duration_ms);
|
|
547
|
-
const timedOut = optionalBoolean(event.timed_out);
|
|
548
|
-
if (startedAtUnixMs !== void 0) result.startedAtUnixMs = startedAtUnixMs;
|
|
549
|
-
if (endedAtUnixMs !== void 0) result.endedAtUnixMs = endedAtUnixMs;
|
|
550
|
-
if (durationMs !== void 0) result.durationMs = durationMs;
|
|
551
|
-
if (timedOut !== void 0) result.timedOut = timedOut;
|
|
552
|
-
return result;
|
|
553
|
-
}
|
|
554
|
-
throw new Error("Celesto returned an unknown command stream event.");
|
|
555
|
-
};
|
|
556
|
-
var parseSseDataLine = (line) => {
|
|
557
|
-
const text = line.trim();
|
|
558
|
-
if (!text || !text.startsWith("data:")) {
|
|
559
|
-
return void 0;
|
|
560
|
-
}
|
|
561
|
-
const data = text.slice("data:".length).trim();
|
|
562
|
-
if (!data || data === "[DONE]") {
|
|
563
|
-
return void 0;
|
|
564
|
-
}
|
|
565
|
-
try {
|
|
566
|
-
return parseExecStreamEvent(JSON.parse(data));
|
|
567
|
-
} catch (error) {
|
|
568
|
-
if (error instanceof SyntaxError) {
|
|
569
|
-
throw new Error("Celesto returned malformed JSON in the command stream.");
|
|
570
|
-
}
|
|
571
|
-
throw error;
|
|
572
|
-
}
|
|
573
|
-
};
|
|
574
|
-
var createManagedSignal = (signal, timeoutMs) => {
|
|
575
|
-
if (!timeoutMs || timeoutMs <= 0) {
|
|
576
|
-
return { signal, cleanup: () => {
|
|
577
|
-
} };
|
|
578
|
-
}
|
|
579
|
-
const controller = new AbortController();
|
|
580
|
-
const abort = () => controller.abort();
|
|
581
|
-
if (signal?.aborted) {
|
|
582
|
-
abort();
|
|
583
|
-
} else {
|
|
584
|
-
signal?.addEventListener("abort", abort, { once: true });
|
|
585
|
-
}
|
|
586
|
-
const timer = setTimeout(abort, timeoutMs);
|
|
587
|
-
return {
|
|
588
|
-
signal: controller.signal,
|
|
589
|
-
cleanup: () => {
|
|
590
|
-
clearTimeout(timer);
|
|
591
|
-
signal?.removeEventListener("abort", abort);
|
|
592
|
-
}
|
|
593
|
-
};
|
|
594
|
-
};
|
|
595
|
-
var toNetworkError = (error) => {
|
|
596
|
-
if (error instanceof CelestoNetworkError) return error;
|
|
597
|
-
const cause = error instanceof Error ? error : new Error(String(error));
|
|
598
|
-
return new CelestoNetworkError(cause.message, cause);
|
|
599
|
-
};
|
|
600
|
-
var withFallbackCommandId = (event, commandId) => {
|
|
601
|
-
if (event.type !== "exit" || event.commandId || !commandId) return event;
|
|
602
|
-
return { ...event, commandId };
|
|
603
|
-
};
|
|
604
447
|
var ComputersClient = class {
|
|
605
448
|
constructor(config) {
|
|
606
449
|
this.config = config;
|
|
607
450
|
}
|
|
608
451
|
async create(params = {}, options) {
|
|
609
452
|
const ctx = buildRequestContext(this.config);
|
|
610
|
-
const
|
|
453
|
+
const headers = {};
|
|
454
|
+
let suppliedKey;
|
|
455
|
+
for (const source of [ctx.headers, options?.headers]) {
|
|
456
|
+
for (const [name, value] of Object.entries(source ?? {})) {
|
|
457
|
+
if (name.toLowerCase() === "idempotency-key") suppliedKey = value;
|
|
458
|
+
else headers[name] = value;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
const key = params.idempotencyKey ?? suppliedKey ?? (0, import_node_crypto.randomUUID)();
|
|
462
|
+
if (typeof key !== "string" || !key.trim()) {
|
|
463
|
+
throw new TypeError("idempotencyKey must not be empty. Omit it to generate a key.");
|
|
464
|
+
}
|
|
465
|
+
headers["Idempotency-Key"] = key;
|
|
466
|
+
const data = await request({ ...ctx, headers: void 0 }, {
|
|
611
467
|
method: "POST",
|
|
612
468
|
path: computersPath(""),
|
|
613
469
|
body: buildCreateComputerBody(params),
|
|
614
|
-
|
|
470
|
+
headers,
|
|
471
|
+
signal: options?.signal
|
|
615
472
|
});
|
|
616
473
|
return toComputerInfo(data);
|
|
617
474
|
}
|
|
@@ -652,100 +509,43 @@ var ComputersClient = class {
|
|
|
652
509
|
return toComputerInfo(data);
|
|
653
510
|
}
|
|
654
511
|
async exec(computerId, command, params = {}, options) {
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
params.signal ?? options?.signal,
|
|
658
|
-
this.config.timeoutMs
|
|
659
|
-
);
|
|
660
|
-
try {
|
|
661
|
-
const data = await request(ctx, {
|
|
662
|
-
method: "POST",
|
|
663
|
-
path: computersPath(`/${encodeURIComponent(computerId)}/exec`),
|
|
664
|
-
body: {
|
|
665
|
-
command,
|
|
666
|
-
timeout: params.timeout ?? 30
|
|
667
|
-
},
|
|
668
|
-
headers: options?.headers,
|
|
669
|
-
signal: managedSignal.signal
|
|
670
|
-
});
|
|
671
|
-
return toExecResponse(data);
|
|
672
|
-
} finally {
|
|
673
|
-
managedSignal.cleanup();
|
|
512
|
+
if (typeof command !== "string" || command.length === 0) {
|
|
513
|
+
throw new TypeError("Provide a non-empty command to run on the Cloud computer.");
|
|
674
514
|
}
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
this.config.timeoutMs
|
|
681
|
-
);
|
|
682
|
-
let reader;
|
|
683
|
-
let completed = false;
|
|
684
|
-
try {
|
|
685
|
-
const response = await requestStream(ctx, {
|
|
686
|
-
method: "POST",
|
|
687
|
-
path: computersPath(`/${encodeURIComponent(computerId)}/exec/stream`),
|
|
688
|
-
body: {
|
|
689
|
-
command,
|
|
690
|
-
timeout: params.timeout ?? 30
|
|
691
|
-
},
|
|
692
|
-
headers: options?.headers,
|
|
693
|
-
signal: managedSignal.signal
|
|
694
|
-
});
|
|
695
|
-
if (!response.body) {
|
|
696
|
-
throw new Error("Celesto did not return a command output stream.");
|
|
697
|
-
}
|
|
698
|
-
const responseCommandId = response.headers.get("x-celesto-command-id") ?? void 0;
|
|
699
|
-
reader = response.body.getReader();
|
|
700
|
-
const decoder = new TextDecoder();
|
|
701
|
-
let buffer = "";
|
|
702
|
-
while (true) {
|
|
703
|
-
let chunk;
|
|
704
|
-
try {
|
|
705
|
-
chunk = await reader.read();
|
|
706
|
-
} catch (error) {
|
|
707
|
-
throw toNetworkError(error);
|
|
708
|
-
}
|
|
709
|
-
if (chunk.done) {
|
|
710
|
-
completed = true;
|
|
711
|
-
buffer += decoder.decode();
|
|
712
|
-
break;
|
|
713
|
-
}
|
|
714
|
-
buffer += decoder.decode(chunk.value, { stream: true });
|
|
715
|
-
let newlineIndex = buffer.indexOf("\n");
|
|
716
|
-
while (newlineIndex !== -1) {
|
|
717
|
-
const line = buffer.slice(0, newlineIndex).replace(/\r$/, "");
|
|
718
|
-
buffer = buffer.slice(newlineIndex + 1);
|
|
719
|
-
const parsed = parseSseDataLine(line);
|
|
720
|
-
if (parsed) {
|
|
721
|
-
const event = withFallbackCommandId(parsed, responseCommandId);
|
|
722
|
-
yield event;
|
|
723
|
-
if (event.type === "exit") return;
|
|
724
|
-
}
|
|
725
|
-
newlineIndex = buffer.indexOf("\n");
|
|
515
|
+
if (command.length > 1e4) {
|
|
516
|
+
let count = 0;
|
|
517
|
+
for (const _ of command) {
|
|
518
|
+
if (++count > 1e4) {
|
|
519
|
+
throw new TypeError("Cloud commands allow at most 10,000 characters. Shorten the command and retry.");
|
|
726
520
|
}
|
|
727
521
|
}
|
|
728
|
-
if (buffer) {
|
|
729
|
-
const parsed = parseSseDataLine(buffer);
|
|
730
|
-
if (parsed) {
|
|
731
|
-
const event = withFallbackCommandId(parsed, responseCommandId);
|
|
732
|
-
yield event;
|
|
733
|
-
if (event.type === "exit") return;
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
throw new Error("Command stream ended before Celesto returned an exit status.");
|
|
737
|
-
} finally {
|
|
738
|
-
if (reader) {
|
|
739
|
-
if (!completed) {
|
|
740
|
-
try {
|
|
741
|
-
await reader.cancel();
|
|
742
|
-
} catch {
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
|
-
reader.releaseLock();
|
|
746
|
-
}
|
|
747
|
-
managedSignal.cleanup();
|
|
748
522
|
}
|
|
523
|
+
const timeout = params.timeout ?? 30;
|
|
524
|
+
if (!Number.isInteger(timeout) || timeout < 1 || timeout > 300) {
|
|
525
|
+
throw new TypeError("Choose a whole-number command timeout between 1 and 300 seconds.");
|
|
526
|
+
}
|
|
527
|
+
const ctx = buildRequestContext(this.config);
|
|
528
|
+
const data = await request(ctx, {
|
|
529
|
+
method: "POST",
|
|
530
|
+
path: computersPath(`/${encodeURIComponent(computerId)}/exec`),
|
|
531
|
+
body: { command, timeout },
|
|
532
|
+
headers: options?.headers,
|
|
533
|
+
signal: params.signal ?? options?.signal
|
|
534
|
+
});
|
|
535
|
+
return toExecResponse(data);
|
|
536
|
+
}
|
|
537
|
+
/** Yield output and exit events after the command has finished. */
|
|
538
|
+
async *execStream(computerId, command, params = {}, options) {
|
|
539
|
+
const { stdout, stderr, exitCode, commandId, durationMs, timedOut } = await this.exec(computerId, command, params, options);
|
|
540
|
+
if (stdout) yield { type: "stdout", data: stdout };
|
|
541
|
+
if (stderr) yield { type: "stderr", data: stderr };
|
|
542
|
+
yield {
|
|
543
|
+
type: "exit",
|
|
544
|
+
exitCode,
|
|
545
|
+
...commandId == null ? {} : { commandId },
|
|
546
|
+
...durationMs == null ? {} : { durationMs },
|
|
547
|
+
...timedOut == null ? {} : { timedOut }
|
|
548
|
+
};
|
|
749
549
|
}
|
|
750
550
|
async listCommandHistory(computerId, params = {}, options) {
|
|
751
551
|
const ctx = buildRequestContext(this.config);
|
|
@@ -897,6 +697,9 @@ var Computer = class _Computer {
|
|
|
897
697
|
get status() {
|
|
898
698
|
return this.info.status;
|
|
899
699
|
}
|
|
700
|
+
get sizeId() {
|
|
701
|
+
return this.info.sizeId;
|
|
702
|
+
}
|
|
900
703
|
get vcpus() {
|
|
901
704
|
return this.info.vcpus;
|
|
902
705
|
}
|
|
@@ -951,7 +754,7 @@ var Computer = class _Computer {
|
|
|
951
754
|
async exec(command, params = {}) {
|
|
952
755
|
return this.run(command, params);
|
|
953
756
|
}
|
|
954
|
-
/** Run a shell command and
|
|
757
|
+
/** Run a shell command and yield buffered output and exit events after it finishes. */
|
|
955
758
|
runStream(command, params = {}) {
|
|
956
759
|
return this.client.execStream(this.id, command, params);
|
|
957
760
|
}
|