@celestoai/sdk 0.1.5 → 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 +130 -254
- package/dist/{chunk-52TE4RZV.js → chunk-3HEUI5UL.js} +341 -306
- package/dist/chunk-3HEUI5UL.js.map +1 -0
- package/dist/computers/index.cjs +211 -347
- package/dist/computers/index.cjs.map +1 -1
- package/dist/computers/index.d.cts +1 -239
- package/dist/computers/index.d.ts +1 -239
- 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 +252 -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-52TE4RZV.js.map +0 -1
- package/dist/chunk-6Y2YZCPU.js +0 -1007
- package/dist/chunk-6Y2YZCPU.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,
|
|
@@ -327,18 +331,50 @@ var toCommandHistoryEntry = (payload) => ({
|
|
|
327
331
|
stderrBytes: payload.stderr_bytes,
|
|
328
332
|
errorType: payload.error_type
|
|
329
333
|
});
|
|
334
|
+
function authenticatedGatewayUrl(gatewayUrl, token) {
|
|
335
|
+
const separator = gatewayUrl.includes("?") ? "&" : "?";
|
|
336
|
+
return `${gatewayUrl}${separator}token=${encodeURIComponent(token)}`;
|
|
337
|
+
}
|
|
330
338
|
var toTerminalConnectionInfo = (payload) => {
|
|
331
|
-
const separator = payload.gateway_url.includes("?") ? "&" : "?";
|
|
332
339
|
return {
|
|
333
340
|
terminalId: payload.terminal_id,
|
|
334
341
|
gatewayUrl: payload.gateway_url,
|
|
335
|
-
url:
|
|
342
|
+
url: authenticatedGatewayUrl(payload.gateway_url, payload.token),
|
|
336
343
|
token: payload.token,
|
|
337
344
|
expiresAt: payload.expires_at,
|
|
338
345
|
headers: {},
|
|
339
346
|
firstMessage: ""
|
|
340
347
|
};
|
|
341
348
|
};
|
|
349
|
+
var toBrowserConnectionInfo = (payload) => {
|
|
350
|
+
if (typeof payload !== "object" || payload === null || typeof payload.gateway_url !== "string" || typeof payload.token !== "string" || typeof payload.expires_at !== "string") {
|
|
351
|
+
throw new CelestoError(
|
|
352
|
+
"Celesto did not return browser connection details. Call createBrowserConnection() again."
|
|
353
|
+
);
|
|
354
|
+
}
|
|
355
|
+
const connection = payload;
|
|
356
|
+
return {
|
|
357
|
+
gatewayUrl: connection.gateway_url,
|
|
358
|
+
url: authenticatedGatewayUrl(connection.gateway_url, connection.token),
|
|
359
|
+
token: connection.token,
|
|
360
|
+
expiresAt: connection.expires_at
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
var toDisplayConnectionInfo = (payload) => {
|
|
364
|
+
if (typeof payload !== "object" || payload === null || payload.mode !== "read_only" && payload.mode !== "read_write") {
|
|
365
|
+
throw new CelestoError(
|
|
366
|
+
"Celesto did not return display connection details. Call createDisplayConnection() again."
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
const connection = payload;
|
|
370
|
+
try {
|
|
371
|
+
return { ...toBrowserConnectionInfo(connection), mode: connection.mode };
|
|
372
|
+
} catch {
|
|
373
|
+
throw new CelestoError(
|
|
374
|
+
"Celesto did not return display connection details. Call createDisplayConnection() again."
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
};
|
|
342
378
|
var toSandboxTemplateInfo = (payload) => ({
|
|
343
379
|
id: payload.id,
|
|
344
380
|
displayName: payload.display_name,
|
|
@@ -349,92 +385,36 @@ var toSandboxTemplateInfo = (payload) => ({
|
|
|
349
385
|
version: payload.version ?? null,
|
|
350
386
|
experimental: payload.experimental
|
|
351
387
|
});
|
|
352
|
-
var
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
var parseDiskString = (disk) => {
|
|
368
|
-
const value = disk.trim();
|
|
369
|
-
if (!value) {
|
|
370
|
-
return void 0;
|
|
371
|
-
}
|
|
372
|
-
let index = 0;
|
|
373
|
-
let digitCount = 0;
|
|
374
|
-
let hasDecimalPoint = false;
|
|
375
|
-
while (index < value.length) {
|
|
376
|
-
const char = value[index];
|
|
377
|
-
if (isAsciiDigit(char)) {
|
|
378
|
-
digitCount += 1;
|
|
379
|
-
index += 1;
|
|
380
|
-
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.`);
|
|
381
403
|
}
|
|
382
|
-
if (
|
|
383
|
-
|
|
384
|
-
index += 1;
|
|
385
|
-
continue;
|
|
404
|
+
if (!createOptions.has(field)) {
|
|
405
|
+
throw new TypeError(`Unknown computer create option ${JSON.stringify(field)}. Remove it and retry.`);
|
|
386
406
|
}
|
|
387
|
-
break;
|
|
388
|
-
}
|
|
389
|
-
if (digitCount === 0 || value[index - 1] === ".") {
|
|
390
|
-
return void 0;
|
|
391
407
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
index += 1;
|
|
395
|
-
}
|
|
396
|
-
const unit = value.slice(index);
|
|
397
|
-
if ([...unit].some((char) => !isAsciiLetter(char))) {
|
|
398
|
-
return void 0;
|
|
399
|
-
}
|
|
400
|
-
return { amount, unit };
|
|
401
|
-
};
|
|
402
|
-
var parseDiskSizeMb = (disk) => {
|
|
403
|
-
if (disk === void 0) {
|
|
404
|
-
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.");
|
|
405
410
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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".');
|
|
409
415
|
}
|
|
410
|
-
|
|
411
|
-
}
|
|
412
|
-
const parsedDisk = parseDiskString(disk);
|
|
413
|
-
if (!parsedDisk) {
|
|
414
|
-
throw new Error("disk must be a size in MB or a string like '2gb'.");
|
|
415
|
-
}
|
|
416
|
-
const multiplier = diskUnitsToMb[parsedDisk.unit.toLowerCase()];
|
|
417
|
-
if (multiplier === void 0) {
|
|
418
|
-
throw new Error("disk must use MB, GB, or TB, for example '2048mb' or '2gb'.");
|
|
416
|
+
body.size_id = params.sizeId;
|
|
419
417
|
}
|
|
420
|
-
const sizeMb = Number(parsedDisk.amount) * multiplier;
|
|
421
|
-
if (!Number.isInteger(sizeMb) || sizeMb <= 0) {
|
|
422
|
-
throw new Error("disk must resolve to a whole number of MB, for example '1536mb' or '1.5gb'.");
|
|
423
|
-
}
|
|
424
|
-
return sizeMb;
|
|
425
|
-
};
|
|
426
|
-
var buildCreateComputerBody = (params) => {
|
|
427
|
-
if (params.cpus !== void 0 && params.vcpus !== void 0 && params.cpus !== params.vcpus) {
|
|
428
|
-
throw new Error("cpus and vcpus must have the same value when both are provided.");
|
|
429
|
-
}
|
|
430
|
-
if (params.memory !== void 0 && params.ramMb !== void 0 && params.memory !== params.ramMb) {
|
|
431
|
-
throw new Error("memory and ramMb must have the same value when both are provided.");
|
|
432
|
-
}
|
|
433
|
-
const parsedDiskSizeMb = parseDiskSizeMb(params.disk);
|
|
434
|
-
if (parsedDiskSizeMb !== void 0 && params.diskSizeMb !== void 0 && parsedDiskSizeMb !== params.diskSizeMb) {
|
|
435
|
-
throw new Error("disk and diskSizeMb must have the same value when both are provided.");
|
|
436
|
-
}
|
|
437
|
-
const body = {};
|
|
438
418
|
if (params.networkPolicy !== void 0) {
|
|
439
419
|
const policy = params.networkPolicy;
|
|
440
420
|
if (!policy || Object.keys(policy).length !== 1 || !("mode" in policy) || !["open", "off"].includes(policy.mode)) {
|
|
@@ -445,18 +425,6 @@ var buildCreateComputerBody = (params) => {
|
|
|
445
425
|
}
|
|
446
426
|
body.network_policy = { mode: policy.mode };
|
|
447
427
|
}
|
|
448
|
-
const vcpus = params.vcpus ?? params.cpus;
|
|
449
|
-
const ramMb = params.ramMb ?? params.memory;
|
|
450
|
-
const diskSizeMb = params.diskSizeMb ?? parsedDiskSizeMb;
|
|
451
|
-
if (vcpus !== void 0) {
|
|
452
|
-
body.vcpus = vcpus;
|
|
453
|
-
}
|
|
454
|
-
if (ramMb !== void 0) {
|
|
455
|
-
body.ram_mb = ramMb;
|
|
456
|
-
}
|
|
457
|
-
if (diskSizeMb !== void 0) {
|
|
458
|
-
body.disk_size_mb = diskSizeMb;
|
|
459
|
-
}
|
|
460
428
|
if (params.image !== void 0) {
|
|
461
429
|
body.image = params.image;
|
|
462
430
|
}
|
|
@@ -476,110 +444,31 @@ var pickOverrides = (options) => ({
|
|
|
476
444
|
headers: options?.headers,
|
|
477
445
|
signal: options?.signal
|
|
478
446
|
});
|
|
479
|
-
var optionalNumber = (value) => typeof value === "number" ? value : void 0;
|
|
480
|
-
var optionalBoolean = (value) => typeof value === "boolean" ? value : void 0;
|
|
481
|
-
var parseExecStreamEvent = (value) => {
|
|
482
|
-
if (!value || typeof value !== "object") {
|
|
483
|
-
throw new Error("Celesto returned an invalid command stream event.");
|
|
484
|
-
}
|
|
485
|
-
const event = value;
|
|
486
|
-
if (event.type === "stdout" || event.type === "stderr") {
|
|
487
|
-
if (typeof event.data !== "string") {
|
|
488
|
-
throw new Error("Celesto returned command output without text data.");
|
|
489
|
-
}
|
|
490
|
-
return { type: event.type, data: event.data };
|
|
491
|
-
}
|
|
492
|
-
if (event.type === "started") {
|
|
493
|
-
if (typeof event.command_id !== "string" || typeof event.started_at_unix_ms !== "number" || typeof event.timeout_seconds !== "number") {
|
|
494
|
-
throw new Error("Celesto returned an invalid command-start event.");
|
|
495
|
-
}
|
|
496
|
-
return {
|
|
497
|
-
type: "started",
|
|
498
|
-
commandId: event.command_id,
|
|
499
|
-
startedAtUnixMs: event.started_at_unix_ms,
|
|
500
|
-
timeoutSeconds: event.timeout_seconds
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
if (event.type === "exit") {
|
|
504
|
-
if (typeof event.exit_code !== "number") {
|
|
505
|
-
throw new Error("Celesto returned an invalid command-exit event.");
|
|
506
|
-
}
|
|
507
|
-
const result = {
|
|
508
|
-
type: "exit",
|
|
509
|
-
exitCode: event.exit_code
|
|
510
|
-
};
|
|
511
|
-
if (typeof event.command_id === "string") result.commandId = event.command_id;
|
|
512
|
-
const startedAtUnixMs = optionalNumber(event.started_at_unix_ms);
|
|
513
|
-
const endedAtUnixMs = optionalNumber(event.ended_at_unix_ms);
|
|
514
|
-
const durationMs = optionalNumber(event.duration_ms);
|
|
515
|
-
const timedOut = optionalBoolean(event.timed_out);
|
|
516
|
-
if (startedAtUnixMs !== void 0) result.startedAtUnixMs = startedAtUnixMs;
|
|
517
|
-
if (endedAtUnixMs !== void 0) result.endedAtUnixMs = endedAtUnixMs;
|
|
518
|
-
if (durationMs !== void 0) result.durationMs = durationMs;
|
|
519
|
-
if (timedOut !== void 0) result.timedOut = timedOut;
|
|
520
|
-
return result;
|
|
521
|
-
}
|
|
522
|
-
throw new Error("Celesto returned an unknown command stream event.");
|
|
523
|
-
};
|
|
524
|
-
var parseSseDataLine = (line) => {
|
|
525
|
-
const text = line.trim();
|
|
526
|
-
if (!text || !text.startsWith("data:")) {
|
|
527
|
-
return void 0;
|
|
528
|
-
}
|
|
529
|
-
const data = text.slice("data:".length).trim();
|
|
530
|
-
if (!data || data === "[DONE]") {
|
|
531
|
-
return void 0;
|
|
532
|
-
}
|
|
533
|
-
try {
|
|
534
|
-
return parseExecStreamEvent(JSON.parse(data));
|
|
535
|
-
} catch (error) {
|
|
536
|
-
if (error instanceof SyntaxError) {
|
|
537
|
-
throw new Error("Celesto returned malformed JSON in the command stream.");
|
|
538
|
-
}
|
|
539
|
-
throw error;
|
|
540
|
-
}
|
|
541
|
-
};
|
|
542
|
-
var createManagedSignal = (signal, timeoutMs) => {
|
|
543
|
-
if (!timeoutMs || timeoutMs <= 0) {
|
|
544
|
-
return { signal, cleanup: () => {
|
|
545
|
-
} };
|
|
546
|
-
}
|
|
547
|
-
const controller = new AbortController();
|
|
548
|
-
const abort = () => controller.abort();
|
|
549
|
-
if (signal?.aborted) {
|
|
550
|
-
abort();
|
|
551
|
-
} else {
|
|
552
|
-
signal?.addEventListener("abort", abort, { once: true });
|
|
553
|
-
}
|
|
554
|
-
const timer = setTimeout(abort, timeoutMs);
|
|
555
|
-
return {
|
|
556
|
-
signal: controller.signal,
|
|
557
|
-
cleanup: () => {
|
|
558
|
-
clearTimeout(timer);
|
|
559
|
-
signal?.removeEventListener("abort", abort);
|
|
560
|
-
}
|
|
561
|
-
};
|
|
562
|
-
};
|
|
563
|
-
var toNetworkError = (error) => {
|
|
564
|
-
if (error instanceof CelestoNetworkError) return error;
|
|
565
|
-
const cause = error instanceof Error ? error : new Error(String(error));
|
|
566
|
-
return new CelestoNetworkError(cause.message, cause);
|
|
567
|
-
};
|
|
568
|
-
var withFallbackCommandId = (event, commandId) => {
|
|
569
|
-
if (event.type !== "exit" || event.commandId || !commandId) return event;
|
|
570
|
-
return { ...event, commandId };
|
|
571
|
-
};
|
|
572
447
|
var ComputersClient = class {
|
|
573
448
|
constructor(config) {
|
|
574
449
|
this.config = config;
|
|
575
450
|
}
|
|
576
451
|
async create(params = {}, options) {
|
|
577
452
|
const ctx = buildRequestContext(this.config);
|
|
578
|
-
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 }, {
|
|
579
467
|
method: "POST",
|
|
580
468
|
path: computersPath(""),
|
|
581
469
|
body: buildCreateComputerBody(params),
|
|
582
|
-
|
|
470
|
+
headers,
|
|
471
|
+
signal: options?.signal
|
|
583
472
|
});
|
|
584
473
|
return toComputerInfo(data);
|
|
585
474
|
}
|
|
@@ -620,100 +509,43 @@ var ComputersClient = class {
|
|
|
620
509
|
return toComputerInfo(data);
|
|
621
510
|
}
|
|
622
511
|
async exec(computerId, command, params = {}, options) {
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
params.signal ?? options?.signal,
|
|
626
|
-
this.config.timeoutMs
|
|
627
|
-
);
|
|
628
|
-
try {
|
|
629
|
-
const data = await request(ctx, {
|
|
630
|
-
method: "POST",
|
|
631
|
-
path: computersPath(`/${encodeURIComponent(computerId)}/exec`),
|
|
632
|
-
body: {
|
|
633
|
-
command,
|
|
634
|
-
timeout: params.timeout ?? 30
|
|
635
|
-
},
|
|
636
|
-
headers: options?.headers,
|
|
637
|
-
signal: managedSignal.signal
|
|
638
|
-
});
|
|
639
|
-
return toExecResponse(data);
|
|
640
|
-
} finally {
|
|
641
|
-
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.");
|
|
642
514
|
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
this.config.timeoutMs
|
|
649
|
-
);
|
|
650
|
-
let reader;
|
|
651
|
-
let completed = false;
|
|
652
|
-
try {
|
|
653
|
-
const response = await requestStream(ctx, {
|
|
654
|
-
method: "POST",
|
|
655
|
-
path: computersPath(`/${encodeURIComponent(computerId)}/exec/stream`),
|
|
656
|
-
body: {
|
|
657
|
-
command,
|
|
658
|
-
timeout: params.timeout ?? 30
|
|
659
|
-
},
|
|
660
|
-
headers: options?.headers,
|
|
661
|
-
signal: managedSignal.signal
|
|
662
|
-
});
|
|
663
|
-
if (!response.body) {
|
|
664
|
-
throw new Error("Celesto did not return a command output stream.");
|
|
665
|
-
}
|
|
666
|
-
const responseCommandId = response.headers.get("x-celesto-command-id") ?? void 0;
|
|
667
|
-
reader = response.body.getReader();
|
|
668
|
-
const decoder = new TextDecoder();
|
|
669
|
-
let buffer = "";
|
|
670
|
-
while (true) {
|
|
671
|
-
let chunk;
|
|
672
|
-
try {
|
|
673
|
-
chunk = await reader.read();
|
|
674
|
-
} catch (error) {
|
|
675
|
-
throw toNetworkError(error);
|
|
676
|
-
}
|
|
677
|
-
if (chunk.done) {
|
|
678
|
-
completed = true;
|
|
679
|
-
buffer += decoder.decode();
|
|
680
|
-
break;
|
|
681
|
-
}
|
|
682
|
-
buffer += decoder.decode(chunk.value, { stream: true });
|
|
683
|
-
let newlineIndex = buffer.indexOf("\n");
|
|
684
|
-
while (newlineIndex !== -1) {
|
|
685
|
-
const line = buffer.slice(0, newlineIndex).replace(/\r$/, "");
|
|
686
|
-
buffer = buffer.slice(newlineIndex + 1);
|
|
687
|
-
const parsed = parseSseDataLine(line);
|
|
688
|
-
if (parsed) {
|
|
689
|
-
const event = withFallbackCommandId(parsed, responseCommandId);
|
|
690
|
-
yield event;
|
|
691
|
-
if (event.type === "exit") return;
|
|
692
|
-
}
|
|
693
|
-
newlineIndex = buffer.indexOf("\n");
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
if (buffer) {
|
|
697
|
-
const parsed = parseSseDataLine(buffer);
|
|
698
|
-
if (parsed) {
|
|
699
|
-
const event = withFallbackCommandId(parsed, responseCommandId);
|
|
700
|
-
yield event;
|
|
701
|
-
if (event.type === "exit") return;
|
|
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.");
|
|
702
520
|
}
|
|
703
521
|
}
|
|
704
|
-
throw new Error("Command stream ended before Celesto returned an exit status.");
|
|
705
|
-
} finally {
|
|
706
|
-
if (reader) {
|
|
707
|
-
if (!completed) {
|
|
708
|
-
try {
|
|
709
|
-
await reader.cancel();
|
|
710
|
-
} catch {
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
reader.releaseLock();
|
|
714
|
-
}
|
|
715
|
-
managedSignal.cleanup();
|
|
716
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
|
+
};
|
|
717
549
|
}
|
|
718
550
|
async listCommandHistory(computerId, params = {}, options) {
|
|
719
551
|
const ctx = buildRequestContext(this.config);
|
|
@@ -793,6 +625,27 @@ var ComputersClient = class {
|
|
|
793
625
|
});
|
|
794
626
|
return toTerminalConnectionInfo(data);
|
|
795
627
|
}
|
|
628
|
+
/** Create a short-lived CDP connection for browser automation. */
|
|
629
|
+
async createBrowserConnection(computerIdOrName, options) {
|
|
630
|
+
const ctx = buildRequestContext(this.config);
|
|
631
|
+
const data = await request(ctx, {
|
|
632
|
+
method: "POST",
|
|
633
|
+
path: computersPath(`/${encodeURIComponent(computerIdOrName)}/browser`),
|
|
634
|
+
...pickOverrides(options)
|
|
635
|
+
});
|
|
636
|
+
return toBrowserConnectionInfo(data);
|
|
637
|
+
}
|
|
638
|
+
/** Create a short-lived RFB connection to watch or control the graphical display. */
|
|
639
|
+
async createDisplayConnection(computerIdOrName, params = {}, options) {
|
|
640
|
+
const ctx = buildRequestContext(this.config);
|
|
641
|
+
const data = await request(ctx, {
|
|
642
|
+
method: "POST",
|
|
643
|
+
path: computersPath(`/${encodeURIComponent(computerIdOrName)}/display`),
|
|
644
|
+
body: params.mode === void 0 ? void 0 : { mode: params.mode },
|
|
645
|
+
...pickOverrides(options)
|
|
646
|
+
});
|
|
647
|
+
return toDisplayConnectionInfo(data);
|
|
648
|
+
}
|
|
796
649
|
/** @deprecated Use createTerminalSession(). */
|
|
797
650
|
async getTerminalConnection(computerIdOrName, options) {
|
|
798
651
|
return this.createTerminalSession(computerIdOrName, options);
|
|
@@ -844,6 +697,9 @@ var Computer = class _Computer {
|
|
|
844
697
|
get status() {
|
|
845
698
|
return this.info.status;
|
|
846
699
|
}
|
|
700
|
+
get sizeId() {
|
|
701
|
+
return this.info.sizeId;
|
|
702
|
+
}
|
|
847
703
|
get vcpus() {
|
|
848
704
|
return this.info.vcpus;
|
|
849
705
|
}
|
|
@@ -898,7 +754,7 @@ var Computer = class _Computer {
|
|
|
898
754
|
async exec(command, params = {}) {
|
|
899
755
|
return this.run(command, params);
|
|
900
756
|
}
|
|
901
|
-
/** Run a shell command and
|
|
757
|
+
/** Run a shell command and yield buffered output and exit events after it finishes. */
|
|
902
758
|
runStream(command, params = {}) {
|
|
903
759
|
return this.client.execStream(this.id, command, params);
|
|
904
760
|
}
|
|
@@ -914,6 +770,14 @@ var Computer = class _Computer {
|
|
|
914
770
|
async createTerminalSession() {
|
|
915
771
|
return this.client.createTerminalSession(this.id);
|
|
916
772
|
}
|
|
773
|
+
/** Create a short-lived CDP connection for browser automation. */
|
|
774
|
+
async createBrowserConnection() {
|
|
775
|
+
return this.client.createBrowserConnection(this.id);
|
|
776
|
+
}
|
|
777
|
+
/** Create a short-lived RFB connection to watch or control the graphical display. */
|
|
778
|
+
async createDisplayConnection(params = {}) {
|
|
779
|
+
return this.client.createDisplayConnection(this.id, params);
|
|
780
|
+
}
|
|
917
781
|
/** @deprecated Use createTerminalSession(). */
|
|
918
782
|
async getTerminalConnection() {
|
|
919
783
|
return this.createTerminalSession();
|