@celestoai/sdk 0.1.1 → 0.1.3

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/dist/index.cjs CHANGED
@@ -20,11 +20,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
- Celesto: () => Celesto,
24
23
  CelestoApiError: () => CelestoApiError,
25
24
  CelestoError: () => CelestoError,
26
25
  CelestoNetworkError: () => CelestoNetworkError,
27
- ComputersClient: () => ComputersClient,
26
+ Computer: () => Computer,
28
27
  GatekeeperClient: () => GatekeeperClient
29
28
  });
30
29
  module.exports = __toCommonJS(src_exports);
@@ -169,161 +168,6 @@ var request = async (ctx, options) => {
169
168
  }
170
169
  };
171
170
 
172
- // src/computers/client.ts
173
- var toConnection = (payload) => {
174
- if (!payload) {
175
- return void 0;
176
- }
177
- const out = {};
178
- if (payload.ssh != null) {
179
- out.ssh = payload.ssh;
180
- }
181
- if (payload.access_url != null) {
182
- out.accessUrl = payload.access_url;
183
- }
184
- return out;
185
- };
186
- var toComputerInfo = (payload) => ({
187
- id: payload.id,
188
- name: payload.name,
189
- status: payload.status,
190
- vcpus: payload.vcpus,
191
- ramMb: payload.ram_mb,
192
- image: payload.image,
193
- connection: toConnection(payload.connection),
194
- lastError: payload.last_error ?? null,
195
- createdAt: payload.created_at,
196
- stoppedAt: payload.stopped_at ?? null
197
- });
198
- var toExecResponse = (payload) => ({
199
- exitCode: payload.exit_code,
200
- stdout: payload.stdout,
201
- stderr: payload.stderr
202
- });
203
- var computersPath = (path) => `/v1/computers${path}`;
204
- var pickOverrides = (options) => ({
205
- headers: options?.headers,
206
- signal: options?.signal
207
- });
208
- var ComputersClient = class {
209
- constructor(config) {
210
- this.config = config;
211
- }
212
- async create(params = {}, options) {
213
- const ctx = buildRequestContext(this.config);
214
- const data = await request(ctx, {
215
- method: "POST",
216
- path: computersPath(""),
217
- body: {
218
- vcpus: params.cpus ?? 1,
219
- ram_mb: params.memory ?? 1024,
220
- image: params.image ?? "ubuntu-desktop-24.04"
221
- },
222
- ...pickOverrides(options)
223
- });
224
- return toComputerInfo(data);
225
- }
226
- async list(options) {
227
- const ctx = buildRequestContext(this.config);
228
- const data = await request(ctx, {
229
- method: "GET",
230
- path: computersPath(""),
231
- ...pickOverrides(options)
232
- });
233
- return {
234
- computers: data.computers.map(toComputerInfo),
235
- count: data.count
236
- };
237
- }
238
- async get(computerId, options) {
239
- const ctx = buildRequestContext(this.config);
240
- const data = await request(ctx, {
241
- method: "GET",
242
- path: computersPath(`/${encodeURIComponent(computerId)}`),
243
- ...pickOverrides(options)
244
- });
245
- return toComputerInfo(data);
246
- }
247
- async exec(computerId, command, params = {}, options) {
248
- const ctx = buildRequestContext(this.config);
249
- const data = await request(ctx, {
250
- method: "POST",
251
- path: computersPath(`/${encodeURIComponent(computerId)}/exec`),
252
- body: {
253
- command,
254
- timeout: params.timeout ?? 30
255
- },
256
- ...pickOverrides(options)
257
- });
258
- return toExecResponse(data);
259
- }
260
- async stop(computerId, options) {
261
- const ctx = buildRequestContext(this.config);
262
- const data = await request(ctx, {
263
- method: "POST",
264
- path: computersPath(`/${encodeURIComponent(computerId)}/stop`),
265
- ...pickOverrides(options)
266
- });
267
- return toComputerInfo(data);
268
- }
269
- async start(computerId, options) {
270
- const ctx = buildRequestContext(this.config);
271
- const data = await request(ctx, {
272
- method: "POST",
273
- path: computersPath(`/${encodeURIComponent(computerId)}/start`),
274
- ...pickOverrides(options)
275
- });
276
- return toComputerInfo(data);
277
- }
278
- async delete(computerId, options) {
279
- const ctx = buildRequestContext(this.config);
280
- const data = await request(ctx, {
281
- method: "DELETE",
282
- path: computersPath(`/${encodeURIComponent(computerId)}`),
283
- ...pickOverrides(options)
284
- });
285
- return toComputerInfo(data);
286
- }
287
- /**
288
- * Get connection info for opening a WebSocket terminal session.
289
- *
290
- * Accepts either a computer ID (e.g. `cmp_xxx`) or a human-readable name.
291
- * The name is resolved to the canonical ID via a GET call — the backend's
292
- * WebSocket endpoint does not resolve names on its own.
293
- *
294
- * Returns the URL, headers, and first message needed to open the connection
295
- * with any WebSocket library of your choice.
296
- *
297
- * @example
298
- * ```ts
299
- * const conn = await celesto.computers.getTerminalConnection("my-computer");
300
- * const ws = new WebSocket(conn.url, { headers: conn.headers });
301
- * ws.on("open", () => ws.send(conn.firstMessage));
302
- * ws.on("message", (data) => process.stdout.write(data));
303
- * ```
304
- */
305
- async getTerminalConnection(computerIdOrName) {
306
- const info = await this.get(computerIdOrName);
307
- const ctx = buildRequestContext(this.config);
308
- if (!ctx.token) {
309
- throw new Error("A token is required for terminal connections");
310
- }
311
- const wsBase = ctx.baseUrl.replace(/^https:/i, "wss:").replace(/^http:/i, "ws:");
312
- const url = `${wsBase}/v1/computers/${encodeURIComponent(info.id)}/terminal`;
313
- const headers = {
314
- Authorization: `Bearer ${ctx.token}`
315
- };
316
- if (ctx.organizationId) {
317
- headers["X-Current-Organization"] = ctx.organizationId;
318
- }
319
- return {
320
- url,
321
- headers,
322
- firstMessage: JSON.stringify({ token: ctx.token })
323
- };
324
- }
325
- };
326
-
327
171
  // src/gatekeeper/client.ts
328
172
  var toConnectRequest = (payload) => ({
329
173
  subject: payload.subject,
@@ -336,7 +180,7 @@ var toConnectResponse = (payload) => ({
336
180
  oauthUrl: payload.oauth_url ?? void 0,
337
181
  connectionId: payload.connection_id ?? void 0
338
182
  });
339
- var toConnection2 = (payload) => ({
183
+ var toConnection = (payload) => ({
340
184
  id: payload.id,
341
185
  subject: payload.subject,
342
186
  provider: payload.provider,
@@ -376,7 +220,7 @@ var toDriveFile = (payload) => ({
376
220
  ownedByMe: payload.owned_by_me ?? null
377
221
  });
378
222
  var gatekeeperPath = (path) => `/v1/gatekeeper${path}`;
379
- var pickOverrides2 = (options) => ({
223
+ var pickOverrides = (options) => ({
380
224
  headers: options?.headers,
381
225
  signal: options?.signal
382
226
  });
@@ -390,7 +234,7 @@ var GatekeeperClient = class {
390
234
  method: "POST",
391
235
  path: gatekeeperPath("/connect"),
392
236
  body: toConnectRequest(payload),
393
- ...pickOverrides2(options)
237
+ ...pickOverrides(options)
394
238
  });
395
239
  return toConnectResponse(data);
396
240
  }
@@ -403,11 +247,11 @@ var GatekeeperClient = class {
403
247
  project_name: params.projectName,
404
248
  status_filter: params.statusFilter
405
249
  },
406
- ...pickOverrides2(options)
250
+ ...pickOverrides(options)
407
251
  });
408
252
  return {
409
253
  total: data.total,
410
- data: data.data.map(toConnection2)
254
+ data: data.data.map(toConnection)
411
255
  };
412
256
  }
413
257
  async getConnection(connectionId, options) {
@@ -415,9 +259,9 @@ var GatekeeperClient = class {
415
259
  const data = await request(ctx, {
416
260
  method: "GET",
417
261
  path: gatekeeperPath(`/connections/${connectionId}`),
418
- ...pickOverrides2(options)
262
+ ...pickOverrides(options)
419
263
  });
420
- return toConnection2(data);
264
+ return toConnection(data);
421
265
  }
422
266
  async revokeConnection(params, options) {
423
267
  const ctx = buildRequestContext(this.config);
@@ -429,7 +273,7 @@ var GatekeeperClient = class {
429
273
  project_name: params.projectName,
430
274
  provider: params.provider
431
275
  },
432
- ...pickOverrides2(options)
276
+ ...pickOverrides(options)
433
277
  });
434
278
  }
435
279
  async listDriveFiles(params, options) {
@@ -447,7 +291,7 @@ var GatekeeperClient = class {
447
291
  include_folders: params.includeFolders,
448
292
  order_by: params.orderBy
449
293
  },
450
- ...pickOverrides2(options)
294
+ ...pickOverrides(options)
451
295
  });
452
296
  return {
453
297
  files: data.files.map(toDriveFile),
@@ -459,7 +303,7 @@ var GatekeeperClient = class {
459
303
  const data = await request(ctx, {
460
304
  method: "GET",
461
305
  path: gatekeeperPath(`/connections/${connectionId}/access-rules`),
462
- ...pickOverrides2(options)
306
+ ...pickOverrides(options)
463
307
  });
464
308
  return toAccessRules(data);
465
309
  }
@@ -477,7 +321,7 @@ var GatekeeperClient = class {
477
321
  allowed_folders: params.accessRules.allowedFolders ?? [],
478
322
  allowed_files: params.accessRules.allowedFiles ?? []
479
323
  },
480
- ...pickOverrides2(options)
324
+ ...pickOverrides(options)
481
325
  });
482
326
  return toAccessRules(data);
483
327
  }
@@ -486,26 +330,483 @@ var GatekeeperClient = class {
486
330
  const data = await request(ctx, {
487
331
  method: "DELETE",
488
332
  path: gatekeeperPath(`/connections/${connectionId}/access-rules`),
489
- ...pickOverrides2(options)
333
+ ...pickOverrides(options)
490
334
  });
491
335
  return toAccessRules(data);
492
336
  }
493
337
  };
494
338
 
495
- // src/index.ts
496
- var Celesto = class {
339
+ // src/computers/client.ts
340
+ var toConnection2 = (payload) => {
341
+ if (!payload) {
342
+ return void 0;
343
+ }
344
+ const out = {};
345
+ if (payload.ssh != null) {
346
+ out.ssh = payload.ssh;
347
+ }
348
+ if (payload.access_url != null) {
349
+ out.accessUrl = payload.access_url;
350
+ }
351
+ return out;
352
+ };
353
+ var toPublishedPortInfo = (payload) => ({
354
+ id: payload.id ?? null,
355
+ computerId: payload.computer_id,
356
+ port: payload.port,
357
+ url: payload.url ?? null,
358
+ status: payload.status,
359
+ createdAt: payload.created_at ?? null
360
+ });
361
+ var toComputerInfo = (payload) => ({
362
+ id: payload.id,
363
+ name: payload.name,
364
+ status: payload.status,
365
+ vcpus: payload.vcpus,
366
+ ramMb: payload.ram_mb,
367
+ diskSizeMb: payload.disk_size_mb,
368
+ image: payload.image,
369
+ templateId: payload.template_id,
370
+ templateVersion: payload.template_version ?? null,
371
+ connection: toConnection2(payload.connection),
372
+ publishedPorts: payload.published_ports?.map(toPublishedPortInfo),
373
+ lastError: payload.last_error ?? null,
374
+ createdAt: payload.created_at,
375
+ stoppedAt: payload.stopped_at ?? null
376
+ });
377
+ var toExecResponse = (payload) => ({
378
+ exitCode: payload.exit_code,
379
+ stdout: payload.stdout,
380
+ stderr: payload.stderr
381
+ });
382
+ var toSandboxTemplateInfo = (payload) => ({
383
+ id: payload.id,
384
+ displayName: payload.display_name,
385
+ description: payload.description,
386
+ defaultVcpus: payload.default_vcpus,
387
+ defaultRamMb: payload.default_ram_mb,
388
+ defaultDiskSizeMb: payload.default_disk_size_mb,
389
+ version: payload.version ?? null,
390
+ experimental: payload.experimental
391
+ });
392
+ var diskUnitsToMb = {
393
+ "": 1,
394
+ m: 1,
395
+ mb: 1,
396
+ mib: 1,
397
+ g: 1024,
398
+ gb: 1024,
399
+ gib: 1024,
400
+ t: 1024 * 1024,
401
+ tb: 1024 * 1024,
402
+ tib: 1024 * 1024
403
+ };
404
+ var isAsciiDigit = (char) => char >= "0" && char <= "9";
405
+ var isAsciiLetter = (char) => char >= "a" && char <= "z" || char >= "A" && char <= "Z";
406
+ var isSeparatorWhitespace = (char) => char === " " || char === " ";
407
+ var parseDiskString = (disk) => {
408
+ const value = disk.trim();
409
+ if (!value) {
410
+ return void 0;
411
+ }
412
+ let index = 0;
413
+ let digitCount = 0;
414
+ let hasDecimalPoint = false;
415
+ while (index < value.length) {
416
+ const char = value[index];
417
+ if (isAsciiDigit(char)) {
418
+ digitCount += 1;
419
+ index += 1;
420
+ continue;
421
+ }
422
+ if (char === "." && !hasDecimalPoint) {
423
+ hasDecimalPoint = true;
424
+ index += 1;
425
+ continue;
426
+ }
427
+ break;
428
+ }
429
+ if (digitCount === 0 || value[index - 1] === ".") {
430
+ return void 0;
431
+ }
432
+ const amount = value.slice(0, index);
433
+ while (index < value.length && isSeparatorWhitespace(value[index])) {
434
+ index += 1;
435
+ }
436
+ const unit = value.slice(index);
437
+ if ([...unit].some((char) => !isAsciiLetter(char))) {
438
+ return void 0;
439
+ }
440
+ return { amount, unit };
441
+ };
442
+ var parseDiskSizeMb = (disk) => {
443
+ if (disk === void 0) {
444
+ return void 0;
445
+ }
446
+ if (typeof disk === "number") {
447
+ if (!Number.isInteger(disk) || disk <= 0) {
448
+ throw new Error("disk as a number must be a whole number of MB.");
449
+ }
450
+ return disk;
451
+ }
452
+ const parsedDisk = parseDiskString(disk);
453
+ if (!parsedDisk) {
454
+ throw new Error("disk must be a size in MB or a string like '2gb'.");
455
+ }
456
+ const multiplier = diskUnitsToMb[parsedDisk.unit.toLowerCase()];
457
+ if (multiplier === void 0) {
458
+ throw new Error("disk must use MB, GB, or TB, for example '2048mb' or '2gb'.");
459
+ }
460
+ const sizeMb = Number(parsedDisk.amount) * multiplier;
461
+ if (!Number.isInteger(sizeMb) || sizeMb <= 0) {
462
+ throw new Error("disk must resolve to a whole number of MB, for example '1536mb' or '1.5gb'.");
463
+ }
464
+ return sizeMb;
465
+ };
466
+ var buildCreateComputerBody = (params) => {
467
+ if (params.cpus !== void 0 && params.vcpus !== void 0 && params.cpus !== params.vcpus) {
468
+ throw new Error("cpus and vcpus must have the same value when both are provided.");
469
+ }
470
+ if (params.memory !== void 0 && params.ramMb !== void 0 && params.memory !== params.ramMb) {
471
+ throw new Error("memory and ramMb must have the same value when both are provided.");
472
+ }
473
+ const parsedDiskSizeMb = parseDiskSizeMb(params.disk);
474
+ if (parsedDiskSizeMb !== void 0 && params.diskSizeMb !== void 0 && parsedDiskSizeMb !== params.diskSizeMb) {
475
+ throw new Error("disk and diskSizeMb must have the same value when both are provided.");
476
+ }
477
+ const body = {};
478
+ const vcpus = params.vcpus ?? params.cpus;
479
+ const ramMb = params.ramMb ?? params.memory;
480
+ const diskSizeMb = params.diskSizeMb ?? parsedDiskSizeMb;
481
+ if (vcpus !== void 0) {
482
+ body.vcpus = vcpus;
483
+ }
484
+ if (ramMb !== void 0) {
485
+ body.ram_mb = ramMb;
486
+ }
487
+ if (diskSizeMb !== void 0) {
488
+ body.disk_size_mb = diskSizeMb;
489
+ }
490
+ if (params.image !== void 0) {
491
+ body.image = params.image;
492
+ }
493
+ if (params.templateId !== void 0) {
494
+ body.template_id = params.templateId;
495
+ }
496
+ if (params.templateVersion !== void 0) {
497
+ body.template_version = params.templateVersion;
498
+ }
499
+ return body;
500
+ };
501
+ var computersPath = (path) => `/v1/computers${path}`;
502
+ var pickOverrides2 = (options) => ({
503
+ headers: options?.headers,
504
+ signal: options?.signal
505
+ });
506
+ var ComputersClient = class {
497
507
  constructor(config) {
498
- this.gatekeeper = new GatekeeperClient(config);
499
- this.computers = new ComputersClient(config);
508
+ this.config = config;
509
+ }
510
+ async create(params = {}, options) {
511
+ const ctx = buildRequestContext(this.config);
512
+ const data = await request(ctx, {
513
+ method: "POST",
514
+ path: computersPath(""),
515
+ body: buildCreateComputerBody(params),
516
+ ...pickOverrides2(options)
517
+ });
518
+ return toComputerInfo(data);
519
+ }
520
+ async listTemplates(options) {
521
+ const ctx = buildRequestContext(this.config);
522
+ const data = await request(ctx, {
523
+ method: "GET",
524
+ path: computersPath("/templates"),
525
+ ...pickOverrides2(options)
526
+ });
527
+ return data.map(toSandboxTemplateInfo);
528
+ }
529
+ async list(params = {}, options) {
530
+ const ctx = buildRequestContext(this.config);
531
+ const data = await request(ctx, {
532
+ method: "GET",
533
+ path: computersPath(""),
534
+ query: {
535
+ status: params.status,
536
+ template_id: params.templateId,
537
+ project_id: params.projectId,
538
+ limit: params.limit
539
+ },
540
+ ...pickOverrides2(options)
541
+ });
542
+ return {
543
+ computers: data.computers.map(toComputerInfo),
544
+ count: data.count
545
+ };
546
+ }
547
+ async get(computerId, options) {
548
+ const ctx = buildRequestContext(this.config);
549
+ const data = await request(ctx, {
550
+ method: "GET",
551
+ path: computersPath(`/${encodeURIComponent(computerId)}`),
552
+ ...pickOverrides2(options)
553
+ });
554
+ return toComputerInfo(data);
555
+ }
556
+ async exec(computerId, command, params = {}, options) {
557
+ const ctx = buildRequestContext(this.config);
558
+ const data = await request(ctx, {
559
+ method: "POST",
560
+ path: computersPath(`/${encodeURIComponent(computerId)}/exec`),
561
+ body: {
562
+ command,
563
+ timeout: params.timeout ?? 30
564
+ },
565
+ ...pickOverrides2(options)
566
+ });
567
+ return toExecResponse(data);
568
+ }
569
+ async stop(computerId, options) {
570
+ const ctx = buildRequestContext(this.config);
571
+ const data = await request(ctx, {
572
+ method: "POST",
573
+ path: computersPath(`/${encodeURIComponent(computerId)}/stop`),
574
+ ...pickOverrides2(options)
575
+ });
576
+ return toComputerInfo(data);
577
+ }
578
+ async start(computerId, options) {
579
+ const ctx = buildRequestContext(this.config);
580
+ const data = await request(ctx, {
581
+ method: "POST",
582
+ path: computersPath(`/${encodeURIComponent(computerId)}/start`),
583
+ ...pickOverrides2(options)
584
+ });
585
+ return toComputerInfo(data);
586
+ }
587
+ async delete(computerId, options) {
588
+ const ctx = buildRequestContext(this.config);
589
+ const data = await request(ctx, {
590
+ method: "DELETE",
591
+ path: computersPath(`/${encodeURIComponent(computerId)}`),
592
+ ...pickOverrides2(options)
593
+ });
594
+ return toComputerInfo(data);
595
+ }
596
+ async publishPort(computerId, port = 8e3, options) {
597
+ const ctx = buildRequestContext(this.config);
598
+ const data = await request(ctx, {
599
+ method: "POST",
600
+ path: computersPath(`/${encodeURIComponent(computerId)}/published-ports`),
601
+ body: { port },
602
+ ...pickOverrides2(options)
603
+ });
604
+ return toPublishedPortInfo(data);
605
+ }
606
+ async listPublishedPorts(computerId, options) {
607
+ const ctx = buildRequestContext(this.config);
608
+ const data = await request(ctx, {
609
+ method: "GET",
610
+ path: computersPath(`/${encodeURIComponent(computerId)}/published-ports`),
611
+ ...pickOverrides2(options)
612
+ });
613
+ return data.map(toPublishedPortInfo);
614
+ }
615
+ async unpublishPort(computerId, port = 8e3, options) {
616
+ const ctx = buildRequestContext(this.config);
617
+ const data = await request(ctx, {
618
+ method: "DELETE",
619
+ path: computersPath(`/${encodeURIComponent(computerId)}/published-ports/${port}`),
620
+ ...pickOverrides2(options)
621
+ });
622
+ return toPublishedPortInfo(data);
623
+ }
624
+ /**
625
+ * Get connection info for opening a WebSocket terminal session.
626
+ *
627
+ * Accepts either a computer ID (e.g. `cmp_xxx`) or a human-readable name.
628
+ * The name is resolved to the canonical ID via a GET call — the backend's
629
+ * WebSocket endpoint does not resolve names on its own.
630
+ *
631
+ * Returns the URL, headers, and first message needed to open the connection
632
+ * with any WebSocket library of your choice.
633
+ *
634
+ * @example
635
+ * ```ts
636
+ * const conn = await client.getTerminalConnection("my-computer");
637
+ * const ws = new WebSocket(conn.url, { headers: conn.headers });
638
+ * ws.on("open", () => ws.send(conn.firstMessage));
639
+ * ws.on("message", (data) => process.stdout.write(data));
640
+ * ```
641
+ */
642
+ async getTerminalConnection(computerIdOrName) {
643
+ const info = await this.get(computerIdOrName);
644
+ const ctx = buildRequestContext(this.config);
645
+ if (!ctx.token) {
646
+ throw new Error("A token is required for terminal connections");
647
+ }
648
+ const wsBase = ctx.baseUrl.replace(/^https:/i, "wss:").replace(/^http:/i, "ws:");
649
+ const url = `${wsBase}/v1/computers/${encodeURIComponent(info.id)}/terminal`;
650
+ const headers = {
651
+ Authorization: `Bearer ${ctx.token}`
652
+ };
653
+ if (ctx.organizationId) {
654
+ headers["X-Current-Organization"] = ctx.organizationId;
655
+ }
656
+ return {
657
+ url,
658
+ headers,
659
+ firstMessage: JSON.stringify({ token: ctx.token })
660
+ };
661
+ }
662
+ };
663
+
664
+ // src/computers/computer.ts
665
+ var defaultConfig = () => ({
666
+ token: typeof process !== "undefined" ? process.env.CELESTO_API_KEY : void 0
667
+ });
668
+ var Computer = class _Computer {
669
+ constructor(client, info) {
670
+ this.client = client;
671
+ this.info = info;
672
+ }
673
+ /** Create a new computer and return an object with methods on it. */
674
+ static async create(params = {}, config = defaultConfig()) {
675
+ const client = new ComputersClient(config);
676
+ const info = await client.create(params);
677
+ return new _Computer(client, info);
678
+ }
679
+ /** Load an existing computer by name or ID. */
680
+ static async get(computerIdOrName, config = defaultConfig()) {
681
+ const client = new ComputersClient(config);
682
+ const info = await client.get(computerIdOrName);
683
+ return new _Computer(client, info);
684
+ }
685
+ /** List computers in the current account. */
686
+ static async list(params = {}, config = defaultConfig()) {
687
+ const client = new ComputersClient(config);
688
+ const response = await client.list(params);
689
+ return response.computers.map((info) => new _Computer(client, info));
690
+ }
691
+ /** List available computer templates. */
692
+ static async listTemplates(config = defaultConfig()) {
693
+ const client = new ComputersClient(config);
694
+ return client.listTemplates();
695
+ }
696
+ /** Wrap an existing API response in the convenience object. */
697
+ static fromInfo(info, config = defaultConfig()) {
698
+ return new _Computer(new ComputersClient(config), info);
699
+ }
700
+ get id() {
701
+ return this.info.id;
702
+ }
703
+ get name() {
704
+ return this.info.name;
705
+ }
706
+ get status() {
707
+ return this.info.status;
708
+ }
709
+ get vcpus() {
710
+ return this.info.vcpus;
711
+ }
712
+ get ramMb() {
713
+ return this.info.ramMb;
714
+ }
715
+ get diskSizeMb() {
716
+ return this.info.diskSizeMb;
717
+ }
718
+ get image() {
719
+ return this.info.image;
720
+ }
721
+ get templateId() {
722
+ return this.info.templateId;
723
+ }
724
+ get templateVersion() {
725
+ return this.info.templateVersion;
726
+ }
727
+ get connection() {
728
+ return this.info.connection;
729
+ }
730
+ get publishedPorts() {
731
+ return this.info.publishedPorts;
732
+ }
733
+ get lastError() {
734
+ return this.info.lastError;
735
+ }
736
+ get createdAt() {
737
+ return this.info.createdAt;
738
+ }
739
+ get stoppedAt() {
740
+ return this.info.stoppedAt;
741
+ }
742
+ /** Get one field from the latest computer data. */
743
+ get(key) {
744
+ return this.info[key];
745
+ }
746
+ /** Return a copy of the latest computer data. */
747
+ toJSON() {
748
+ return { ...this.info };
749
+ }
750
+ /** Reload this computer from Celesto. */
751
+ async refresh() {
752
+ this.info = await this.client.get(this.id);
753
+ return this;
754
+ }
755
+ /** Run a shell command on this computer. */
756
+ async run(command, params = {}) {
757
+ return this.client.exec(this.id, command, params);
758
+ }
759
+ /** Alias for run(). */
760
+ async exec(command, params = {}) {
761
+ return this.run(command, params);
762
+ }
763
+ /** Stop this computer and update the local data. */
764
+ async stop() {
765
+ this.info = await this.client.stop(this.id);
766
+ return this;
767
+ }
768
+ /** Start this computer and update the local data. */
769
+ async start() {
770
+ this.info = await this.client.start(this.id);
771
+ return this;
772
+ }
773
+ /** Delete this computer and update the local data. */
774
+ async delete() {
775
+ this.info = await this.client.delete(this.id);
776
+ return this;
777
+ }
778
+ /** Publish a port and return its public URL when the API provides one. */
779
+ async publishPort(port = 8e3) {
780
+ const published = await this.client.publishPort(this.id, port);
781
+ const existing = this.info.publishedPorts ?? [];
782
+ this.info = {
783
+ ...this.info,
784
+ publishedPorts: [...existing.filter((item) => item.port !== published.port), published]
785
+ };
786
+ return published.url ?? null;
787
+ }
788
+ /** List public ports for this computer. */
789
+ async listPublishedPorts() {
790
+ const publishedPorts = await this.client.listPublishedPorts(this.id);
791
+ this.info = { ...this.info, publishedPorts };
792
+ return publishedPorts;
793
+ }
794
+ /** Remove a public port route. */
795
+ async unpublishPort(port = 8e3) {
796
+ const unpublished = await this.client.unpublishPort(this.id, port);
797
+ this.info = {
798
+ ...this.info,
799
+ publishedPorts: (this.info.publishedPorts ?? []).filter((item) => item.port !== port)
800
+ };
801
+ return unpublished;
500
802
  }
501
803
  };
502
804
  // Annotate the CommonJS export names for ESM import in node:
503
805
  0 && (module.exports = {
504
- Celesto,
505
806
  CelestoApiError,
506
807
  CelestoError,
507
808
  CelestoNetworkError,
508
- ComputersClient,
809
+ Computer,
509
810
  GatekeeperClient
510
811
  });
511
812
  //# sourceMappingURL=index.cjs.map