agental 0.4.1 → 0.6.0

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.
Files changed (2) hide show
  1. package/dist/cli.js +180 -13
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -46,7 +46,7 @@ import { parseArgs } from "node:util";
46
46
 
47
47
  // ../src/cli-build.ts
48
48
  var PUBLISHED = true;
49
- var CLI_VERSION = true ? "0.4.1" : "dev";
49
+ var CLI_VERSION = true ? "0.6.0" : "dev";
50
50
  var DEFAULT_API_URL = true ? "https://agental.io" : "http://127.0.0.1:8741";
51
51
  var PACKAGE_NAME = "agental";
52
52
  var USER_AGENT = `agental-cli/${CLI_VERSION}`;
@@ -1149,7 +1149,7 @@ async function ssh(client, args) {
1149
1149
  if (tmux && namesCommand(passed)) {
1150
1150
  throw new Error("--tmux opens a shell that is kept. It does not go with a command.");
1151
1151
  }
1152
- const shell = tmux || !namesCommand(passed);
1152
+ const shell2 = tmux || !namesCommand(passed);
1153
1153
  let access = await reach(client, sandboxId);
1154
1154
  await letIn(client, sandboxId);
1155
1155
  for (let soon = 0; ; ) {
@@ -1158,7 +1158,7 @@ async function ssh(client, args) {
1158
1158
  if (tmux && code === 127) {
1159
1159
  process.stderr.write("tmux is not in this sandbox. Install it there, with apt-get update && apt-get install -y tmux, or leave --tmux out.\n");
1160
1160
  }
1161
- if (code !== SSH_LOST || !shell) {
1161
+ if (code !== SSH_LOST || !shell2) {
1162
1162
  process.exitCode = code;
1163
1163
  return;
1164
1164
  }
@@ -2321,6 +2321,179 @@ function needId(id, what) {
2321
2321
  return id;
2322
2322
  }
2323
2323
 
2324
+ // ../src/cli-shell.ts
2325
+ var TERMINAL_PROTOCOL = "agental.terminal.v1";
2326
+ var TICKET_PROTOCOL_PREFIX = "ticket.";
2327
+ var MAX_INPUT_BYTES = 64 * 1024;
2328
+ var LOST = 255;
2329
+ function clamp(value, fallback) {
2330
+ return Number.isInteger(value) && value > 0 ? Math.min(value, 1e3) : fallback;
2331
+ }
2332
+ function sizeOf(stdout) {
2333
+ return stdout.isTTY ? { cols: clamp(stdout.columns, 80), rows: clamp(stdout.rows, 24) } : { cols: 80, rows: 24 };
2334
+ }
2335
+ function webSocketUrl(base, path4) {
2336
+ if (typeof path4 !== "string" || !path4.startsWith("/") || path4.startsWith("//") || /[\\\s]/.test(path4)) {
2337
+ throw new Error("The API gave the terminal a path that is not one on its own address.");
2338
+ }
2339
+ const url = new URL(base);
2340
+ if (url.protocol !== "https:" && url.protocol !== "http:") {
2341
+ throw new Error(`A terminal is opened on an http: or https: API, not ${url.protocol}`);
2342
+ }
2343
+ return `${url.protocol === "https:" ? "wss:" : "ws:"}//${url.host}${url.pathname.replace(/\/+$/, "")}${path4}`;
2344
+ }
2345
+ function whyClosed(code, reason, error) {
2346
+ const said = error?.message || reason;
2347
+ switch (code) {
2348
+ case 4003:
2349
+ return `The terminal was closed: what let it be opened no longer does${said ? ` (${said})` : ""}.`;
2350
+ case 4008:
2351
+ return "The terminal was closed after 30 minutes with nothing typed.";
2352
+ case 4011:
2353
+ return `The terminal was closed: ${said || "its sandbox stopped answering"}.`;
2354
+ case 1006:
2355
+ return "The connection to the terminal was lost.";
2356
+ default:
2357
+ return `The terminal was closed (${code})${said ? `: ${said}` : ""}.`;
2358
+ }
2359
+ }
2360
+ async function shell(client, id, streams = process) {
2361
+ if (!id) {
2362
+ throw new Error("shell requires <sandbox-id>");
2363
+ }
2364
+ const WebSocket = globalThis.WebSocket;
2365
+ if (!WebSocket) {
2366
+ throw new Error(`agental shell needs Node 22 or later, which has WebSocket; this is Node ${process.versions.node}. agental ssh ${id} works here.`);
2367
+ }
2368
+ const { stdin, stdout, stderr } = streams;
2369
+ const size = sizeOf(stdout);
2370
+ const response = await client.request(`/v1/sandboxes/${encodeURIComponent(id)}/terminal`, { method: "POST", body: JSON.stringify(size) });
2371
+ const body = await response.json().catch(() => null);
2372
+ if (!response.ok || typeof body?.ticket !== "string") {
2373
+ const error2 = body?.error;
2374
+ throw new Error(error2 && typeof error2.code === "string" ? `${error2.code}: ${String(error2.message ?? "")}` : `The API answered the terminal with HTTP ${response.status}.`);
2375
+ }
2376
+ const socket = new WebSocket(webSocketUrl(client.base, body.path), [TERMINAL_PROTOCOL, `${TICKET_PROTOCOL_PREFIX}${body.ticket}`]);
2377
+ socket.binaryType = "arraybuffer";
2378
+ let exit = null;
2379
+ let error = null;
2380
+ let opened = false;
2381
+ const raw = stdin.isTTY === true && typeof stdin.setRawMode === "function";
2382
+ let lastSent = 10;
2383
+ const send2 = (chunk) => {
2384
+ if (socket.readyState !== WebSocket.OPEN || chunk.length === 0) {
2385
+ return;
2386
+ }
2387
+ for (let at4 = 0; at4 < chunk.length; at4 += MAX_INPUT_BYTES) {
2388
+ socket.send(chunk.subarray(at4, at4 + MAX_INPUT_BYTES));
2389
+ }
2390
+ lastSent = chunk[chunk.length - 1];
2391
+ };
2392
+ let tilde = false;
2393
+ let closedHere = false;
2394
+ const escaped = (chunk) => {
2395
+ const kept2 = [];
2396
+ for (const byte of chunk) {
2397
+ const atLineStart = (kept2.length > 0 ? kept2[kept2.length - 1] : lastSent) === 13 || (kept2.length > 0 ? kept2[kept2.length - 1] : lastSent) === 10;
2398
+ if (tilde) {
2399
+ tilde = false;
2400
+ if (byte === 46) {
2401
+ closedHere = true;
2402
+ socket.close(1e3, "closed");
2403
+ return Buffer.from(kept2);
2404
+ }
2405
+ kept2.push(126);
2406
+ if (byte !== 126) {
2407
+ kept2.push(byte);
2408
+ }
2409
+ } else if (byte === 126 && atLineStart) {
2410
+ tilde = true;
2411
+ } else {
2412
+ kept2.push(byte);
2413
+ }
2414
+ }
2415
+ return Buffer.from(kept2);
2416
+ };
2417
+ const typed = (chunk) => {
2418
+ const bytes3 = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
2419
+ send2(raw ? escaped(bytes3) : bytes3);
2420
+ };
2421
+ const ended = () => send2(Buffer.from(lastSent === 10 ? [4] : [4, 4]));
2422
+ const resized = () => {
2423
+ if (socket.readyState === WebSocket.OPEN) {
2424
+ socket.send(JSON.stringify({ type: "resize", ...sizeOf(stdout) }));
2425
+ }
2426
+ };
2427
+ const started = () => {
2428
+ stdin.on("data", typed);
2429
+ if (!raw) {
2430
+ stdin.once("end", ended);
2431
+ }
2432
+ stdin.resume();
2433
+ };
2434
+ return await new Promise((resolve) => {
2435
+ socket.addEventListener("open", () => {
2436
+ opened = true;
2437
+ if (socket.protocol !== TERMINAL_PROTOCOL) {
2438
+ socket.close(1e3, "not a terminal");
2439
+ return;
2440
+ }
2441
+ if (raw) {
2442
+ stdin.setRawMode(true);
2443
+ }
2444
+ stdout.on("resize", resized);
2445
+ });
2446
+ socket.addEventListener("message", (event) => {
2447
+ if (typeof event.data === "string") {
2448
+ let message;
2449
+ try {
2450
+ message = JSON.parse(event.data);
2451
+ } catch {
2452
+ return;
2453
+ }
2454
+ if (message["type"] === "started") {
2455
+ started();
2456
+ } else if (message["type"] === "exit") {
2457
+ exit = { code: Number(message["code"] ?? 0), signal: Number(message["signal"] ?? 0) };
2458
+ } else if (message["type"] === "error") {
2459
+ error = { code: String(message["code"] ?? "terminal"), message: String(message["message"] ?? "") };
2460
+ }
2461
+ return;
2462
+ }
2463
+ stdout.write(new Uint8Array(event.data));
2464
+ });
2465
+ socket.addEventListener("close", (event) => {
2466
+ stdin.off("data", typed);
2467
+ stdin.off("end", ended);
2468
+ stdout.off("resize", resized);
2469
+ if (raw) {
2470
+ stdin.setRawMode(false);
2471
+ }
2472
+ stdin.pause();
2473
+ if (!opened) {
2474
+ stderr.write("The terminal could not be opened. Its ticket may have expired, or the API may be behind a proxy that does not pass WebSockets on.\n");
2475
+ resolve(LOST);
2476
+ return;
2477
+ }
2478
+ if (closedHere) {
2479
+ stderr.write("\r\nThe terminal was closed from here.\n");
2480
+ resolve(LOST);
2481
+ return;
2482
+ }
2483
+ const shellExit = exit;
2484
+ if (event.code === 1e3 && shellExit) {
2485
+ const code = shellExit.signal > 0 ? 128 + shellExit.signal : shellExit.code;
2486
+ resolve(Number.isInteger(code) && code >= 0 && code <= 255 ? code : LOST);
2487
+ return;
2488
+ }
2489
+ stderr.write(`\r
2490
+ ${whyClosed(event.code, event.reason, error)}
2491
+ `);
2492
+ resolve(LOST);
2493
+ });
2494
+ });
2495
+ }
2496
+
2324
2497
  // left-out:cli-dev-key
2325
2498
  var DEV_KEY = null;
2326
2499
 
@@ -2586,8 +2759,6 @@ async function main() {
2586
2759
  "from-name": { type: "string" },
2587
2760
  "reply-to": { type: "string" },
2588
2761
  box: { type: "string" },
2589
- restart: { type: "boolean", default: false },
2590
- live: { type: "boolean", default: false },
2591
2762
  screen: { type: "string" },
2592
2763
  headless: { type: "boolean", default: false },
2593
2764
  headed: { type: "boolean", default: false },
@@ -2695,6 +2866,9 @@ async function main() {
2695
2866
  case "exec":
2696
2867
  await exec(positionals.slice(1));
2697
2868
  return;
2869
+ case "shell":
2870
+ process.exitCode = await shell({ base: apiBase(), request }, positionals[1]);
2871
+ return;
2698
2872
  case "delete":
2699
2873
  if (values.where !== void 0 && positionals[1] !== void 0) {
2700
2874
  throw new Error("delete takes a sandbox's ID or --where, not both");
@@ -2766,14 +2940,6 @@ async function main() {
2766
2940
  case "browser":
2767
2941
  await browser(positionals[1], values.headless, values.headed, values.off, values.status);
2768
2942
  return;
2769
- case "move": {
2770
- if (PUBLISHED) {
2771
- throw new Error("move is for whoever runs agental, from its repository.");
2772
- }
2773
- const { move: move2 } = await Promise.resolve().then(() => (init_cli_operator(), cli_operator_exports));
2774
- await move2({ request, fail }, positionals[1], boxNamed(values.box), values.restart, values.live);
2775
- return;
2776
- }
2777
2943
  case "network":
2778
2944
  await network(positionals.slice(1), values);
2779
2945
  return;
@@ -4942,6 +5108,7 @@ Usage
4942
5108
  agental meta <sandbox-id> [set <key>=<value>... | unset <key>...] [--json]
4943
5109
  agental regions [--json]
4944
5110
  agental exec <sandbox-id> -- <command> [args...]
5111
+ agental shell <sandbox-id> a login shell in it, in this terminal: no SSH key or port
4945
5112
  agental delete <sandbox-id>
4946
5113
  agental start <sandbox-id> [--max-burst-fee <dollars>]
4947
5114
  agental stop <sandbox-id> [--keep-memory]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agental",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "description": "The agental command: Linux sandboxes for your agents, from the terminal. npx agental@latest signs you in and installs the agent skills.",
5
5
  "license": "MIT",
6
6
  "author": "VERTIXER LTD",