@parcha/agentrun 0.4.20 → 0.4.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/lib/cli.mjs +109 -11
- package/lib/commands.generated.mjs +586 -6
- package/lib/deploy.mjs +664 -0
- package/lib/device.mjs +260 -0
- package/lib/export.mjs +80 -0
- package/lib/harness.mjs +19 -0
- package/lib/login.mjs +77 -42
- package/lib/start.mjs +1 -1
- package/lib/target.mjs +15 -0
- package/package.json +2 -2
- package/skills/agentrun-cli/SKILL.md +8 -6
- package/skills/agentrun-cli/references/commands.md +226 -8
package/README.md
CHANGED
|
@@ -164,13 +164,25 @@ Every other command is the server's API, from a second terminal: `npx @parcha/ag
|
|
|
164
164
|
`runs get RUN_ID`, `runs message RUN_ID --content "..."`, and one command for each route the server serves
|
|
165
165
|
(`npx @parcha/agentrun help` lists them; `api METHOD PATH` calls any route). It finds the server started with the same
|
|
166
166
|
`--data` on its own: the server writes its address and where its token is to `DIR/.server.json` once it listens. For
|
|
167
|
-
another server, set `AGENTRUN_BASE_URL` and `AGENTRUN_API_KEY
|
|
167
|
+
another server, set `AGENTRUN_BASE_URL` and `AGENTRUN_API_KEY`, or sign in once with `agentrun login --to URL`
|
|
168
|
+
(`--to cloud` for AgentRun cloud): when the server offers a device sign-in, it prints an address and a code to approve
|
|
169
|
+
there, then keeps the token in `$XDG_CONFIG_HOME/agentrun/config.json` (0600); else it reads the token from stdin, as
|
|
170
|
+
`agentrun login --base-url URL < FILE` does. `agentrun login --base-url URL` typed on a terminal takes the device
|
|
171
|
+
sign-in too, when the server offers one. The response's JSON goes to stdout and an error's to
|
|
168
172
|
stderr; the exit code is 0 when it worked, 1 when the server refused, 2 for bad usage, 3 when the server cannot be
|
|
169
173
|
reached, 4 for a missing or refused token and 5 when the server does not offer the command here (a 501,
|
|
170
174
|
`unsupported_here`, its error saying why). The token is never a flag and never printed. A list command (`agents list`,
|
|
171
175
|
`runs list` and every other that takes `--cursor`) prints one page; `--all` follows `next_cursor` and prints every page
|
|
172
176
|
as one list.
|
|
173
177
|
|
|
178
|
+
Any other word runs a program from your `PATH`, as git does: `agentrun NAME [args]` runs `agentrun-NAME` with the rest
|
|
179
|
+
of the arguments, no shell between, on this terminal's input and output, and exits with its exit code. A tool adds a
|
|
180
|
+
command to the CLI by installing `agentrun-NAME`. The CLI's own commands always come first, NAME is one plain word, and
|
|
181
|
+
a relative or empty `PATH` entry, which would mean the working directory, is never searched. A signal sent to `agentrun`
|
|
182
|
+
reaches the command, and `agentrun` exits when it does. On Windows it looks for `agentrun-NAME` with `.com`, `.exe`,
|
|
183
|
+
`.bat` or `.cmd`, in that order; a batch file (`.bat`, `.cmd`) is refused, since Node runs one only through `cmd.exe`,
|
|
184
|
+
which would read the arguments again.
|
|
185
|
+
|
|
174
186
|
`runs wait RUN_ID` waits until the run ends or waits on a person and prints it; `runs start AGENT_ID ... --wait` starts
|
|
175
187
|
a run and does the same. Its exit code says how it stopped: 0 completed, 1 failed or cancelled, 8 it waits on a person
|
|
176
188
|
(a question, its spend cap, a stopped turn, a paused workflow run), with a line on stderr saying what to do next, such as
|
package/lib/cli.mjs
CHANGED
|
@@ -5,10 +5,13 @@
|
|
|
5
5
|
// validate and template eval, which check and run a package folder on this machine, are template.mjs's, harness install
|
|
6
6
|
// (the harness its owner installs: goose) is harness.mjs's, and service
|
|
7
7
|
// (AgentRun as a launchd or systemd user service) and stop (the AgentRun the installer started in the background) are
|
|
8
|
-
// service.mjs's,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import {
|
|
8
|
+
// service.mjs's, doctor (how this AgentRun is set up, read-only) is doctor.mjs's, export (an agent written as a template
|
|
9
|
+
// package folder) is export.mjs's, and deploy (an agent's package sent to another AgentRun, with its lock, rollback and
|
|
10
|
+
// delete) is deploy.mjs's. Any other word runs agentrun-WORD from PATH, as git runs git-WORD (externalOf, below).
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
import { accessSync, closeSync, constants, fstatSync, openSync, readFileSync, statSync } from "node:fs";
|
|
13
|
+
import { homedir, constants as osConstants } from "node:os";
|
|
14
|
+
import { delimiter, isAbsolute, join, resolve } from "node:path";
|
|
12
15
|
import { parseArgs } from "node:util";
|
|
13
16
|
import { COMMANDS, SECRET_ROUTES } from "./commands.generated.mjs";
|
|
14
17
|
import { ALL, FLAGS, MAX_PAGES, REQUEST_MS, WAIT, followTimeline, timerMs, waitRun } from "./follow.mjs";
|
|
@@ -17,10 +20,13 @@ import { DOCTOR } from "./doctor.mjs";
|
|
|
17
20
|
import { OPEN, SERVICE, STOP } from "./service.mjs";
|
|
18
21
|
import { SKILL_INSTALL } from "./skill.mjs";
|
|
19
22
|
import { TEMPLATE_EVAL, TEMPLATE_INSTALL, TEMPLATE_VALIDATE } from "./template.mjs";
|
|
20
|
-
import { HARNESS_CHECK, HARNESS_INSTALL } from "./harness.mjs";
|
|
23
|
+
import { HARNESS_CHECK, HARNESS_INSTALL, HARNESS_SIGN_IN } from "./harness.mjs";
|
|
24
|
+
import { EXPORT } from "./export.mjs";
|
|
25
|
+
import { DEPLOY, DEPLOY_DELETE, DEPLOY_ROLLBACK } from "./deploy.mjs";
|
|
21
26
|
|
|
22
|
-
/** The hand-written commands that run as they are given (`run`), beside the table's.
|
|
23
|
-
|
|
27
|
+
/** The hand-written commands that run as they are given (`run`), beside the table's. A command of one word that has
|
|
28
|
+
* verbs too (deploy) comes after them: `run` takes the first whose words match. */
|
|
29
|
+
const LOCAL = [...SECRETS, SKILL_INSTALL, TEMPLATE_VALIDATE, TEMPLATE_EVAL, TEMPLATE_INSTALL, EXPORT, HARNESS_INSTALL, HARNESS_SIGN_IN, HARNESS_CHECK, ...SERVICE, STOP, OPEN, DOCTOR, DEPLOY_ROLLBACK, DEPLOY_DELETE, DEPLOY];
|
|
24
30
|
export const NOUNS = new Set(["api", "help", ...[...COMMANDS, ...LOCAL].map((entry) => entry.command[0])]);
|
|
25
31
|
const DEFAULT_BASE_URL = "http://127.0.0.1:7340/v2"; // key-gate: loopback-rule the default address of the server started on this machine
|
|
26
32
|
const START = "start it with `npx @parcha/agentrun`, or pass --base-url (or set AGENTRUN_BASE_URL) to the address the server printed";
|
|
@@ -41,11 +47,11 @@ const usageError = (message) => new Exit(2, message);
|
|
|
41
47
|
const flag = (name) => name.replace(/_/g, "-").toLowerCase();
|
|
42
48
|
|
|
43
49
|
/** The words of `argv` that are not flags, past the values of the flags placed before a command (--data, --base-url,
|
|
44
|
-
* and the server's --port). */
|
|
50
|
+
* login's --to, and the server's --port) or before a verb (deploy's --to and --params). */
|
|
45
51
|
export function words(argv) {
|
|
46
52
|
const found = [];
|
|
47
53
|
for (let i = 0; i < argv.length; i++) {
|
|
48
|
-
if (["--data", "--base-url", "--port"].includes(argv[i])) i++;
|
|
54
|
+
if (["--data", "--base-url", "--port", "--to", "--params"].includes(argv[i])) i++;
|
|
49
55
|
else if (!argv[i].startsWith("-")) found.push(argv[i]);
|
|
50
56
|
}
|
|
51
57
|
return found;
|
|
@@ -62,10 +68,15 @@ const HELP = () => {
|
|
|
62
68
|
...[...groups].map(([noun, verbs]) => ` ${noun.padEnd(width)} ${verbs.join(", ")}`),
|
|
63
69
|
` ${"api".padEnd(width)} METHOD PATH [${BODY}] [--header 'NAME: VALUE'], any route, PATH under the base URL (/agents)`,
|
|
64
70
|
"",
|
|
71
|
+
"Any other NAME: agentrun NAME [args] runs the program agentrun-NAME from an absolute folder of PATH with the rest of",
|
|
72
|
+
"the arguments and passes on its exit code, as git runs git-NAME; a tool adds a command by installing agentrun-NAME.",
|
|
73
|
+
"",
|
|
65
74
|
"The server: --base-url, else AGENTRUN_BASE_URL, else base_url in $XDG_CONFIG_HOME/agentrun/config.json, else the",
|
|
66
75
|
"server started on this machine with the data directory (--data, default ~/.agentrun). The token: AGENTRUN_API_KEY,",
|
|
67
76
|
"else api_key in the config file, else that server's own; it is never a flag and never printed. agentrun login",
|
|
68
|
-
"--base-url URL checks a token read from stdin and writes the config file (0600)
|
|
77
|
+
"--base-url URL checks a token read from stdin and writes the config file (0600), or on a terminal signs in with a",
|
|
78
|
+
"device code when the server offers one; agentrun login --to URL (or cloud) signs in with a device code when the",
|
|
79
|
+
"server offers one, else reads the token from stdin; agentrun logout removes it.",
|
|
69
80
|
"Keys and other secrets are set in the console, signed in: the token can't write one, so no command does.",
|
|
70
81
|
"Output: the response's JSON on stdout, a file's bytes as they are; an error's on stderr. Exit codes: 0 ok, 1 the",
|
|
71
82
|
"server refused, 2 bad usage (a bad base URL among it), 3 the server cannot be reached, 4 no token, the token was",
|
|
@@ -520,6 +531,88 @@ async function local(entry, argv, env, io) {
|
|
|
520
531
|
return entry.run({ values, args, env, io, cli });
|
|
521
532
|
}
|
|
522
533
|
|
|
534
|
+
/** An external command's name: one plain word, so agentrun-NAME is a file in a folder of PATH, never a path below one. */
|
|
535
|
+
const EXTERNAL_NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
536
|
+
|
|
537
|
+
/** The extensions a program has on Windows, tried in this order (.bat and .cmd to refuse them by name: external). */
|
|
538
|
+
const WINDOWS_PROGRAMS = [".com", ".exe", ".bat", ".cmd"];
|
|
539
|
+
|
|
540
|
+
const isFile = (file) => {
|
|
541
|
+
try {
|
|
542
|
+
return statSync(file).isFile();
|
|
543
|
+
} catch {
|
|
544
|
+
return false;
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
/** The program agentrun-NAME: the first regular file of that name this user may run, in PATH's folders in order. A
|
|
549
|
+
* relative or empty entry means the working directory, which any checkout can fill, so it is never searched. On
|
|
550
|
+
* Windows, which has no execute bit, it is agentrun-NAME with .com, .exe, .bat or .cmd, in that order in each folder
|
|
551
|
+
* (its file names match in any case there); PATHEXT is not read. `platform` is for the tests; the paths are this
|
|
552
|
+
* machine's. */
|
|
553
|
+
export function externalOf(name, env, platform = process.platform) {
|
|
554
|
+
if (!EXTERNAL_NAME.test(name)) return undefined;
|
|
555
|
+
const stem = `agentrun-${name}`;
|
|
556
|
+
for (const dir of (env.PATH ?? "").split(delimiter)) {
|
|
557
|
+
if (!isAbsolute(dir)) continue;
|
|
558
|
+
try {
|
|
559
|
+
if (platform === "win32") {
|
|
560
|
+
const found = WINDOWS_PROGRAMS.map((ext) => join(dir, `${stem}${ext}`)).find(isFile);
|
|
561
|
+
if (found !== undefined) return found;
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
const file = join(dir, stem);
|
|
565
|
+
if (!isFile(file)) continue;
|
|
566
|
+
accessSync(file, constants.X_OK);
|
|
567
|
+
return file;
|
|
568
|
+
} catch {
|
|
569
|
+
// Not there, or not one this user may run: the next folder.
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return undefined;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/** `file` run with `args`: no shell, this process's stdin, stdout and stderr, and its exit code, or 128 plus the number
|
|
576
|
+
* of the signal that ended it. A signal sent to this process is passed on to it, and this process waits for it rather
|
|
577
|
+
* than end first. A terminal's Ctrl-C or Ctrl-\ goes to its whole process group, so the command gets that one twice,
|
|
578
|
+
* from the terminal and from here, as under foreground-child: the cost of never swallowing one sent to agentrun alone.
|
|
579
|
+
* On Windows a Ctrl-C reaches every process on the console and Node's kill ends a process outright, so there it is only
|
|
580
|
+
* held. `platform` is for the tests. */
|
|
581
|
+
export function external(file, args, env, io, platform = process.platform) {
|
|
582
|
+
// Node runs a .bat or .cmd only through cmd.exe, which reads the arguments again (% expands; " ^ & | < > ! are its
|
|
583
|
+
// own), and no quoting holds for every argument: none is run.
|
|
584
|
+
if (platform === "win32" && /\.(?:bat|cmd)$/i.test(file)) {
|
|
585
|
+
io.stderr.write(`${file} is a batch file, which Node runs only through cmd.exe, and cmd.exe would read the arguments again: agentrun runs no command through a shell. Run it yourself, or install agentrun-NAME as an .exe.\n`);
|
|
586
|
+
return Promise.resolve(126);
|
|
587
|
+
}
|
|
588
|
+
return new Promise((resolve) => {
|
|
589
|
+
let child;
|
|
590
|
+
// The handlers are on before the command starts: it may print, and be sent a signal for it, before this process runs
|
|
591
|
+
// again after the spawn. A signal's handler runs only once this turn is over, so `child` is set by then.
|
|
592
|
+
const pass = (signal) => child?.kill(signal);
|
|
593
|
+
const handlers = [["SIGINT", platform === "win32" ? () => {} : pass], ["SIGQUIT", pass], ["SIGTERM", pass], ["SIGHUP", pass]];
|
|
594
|
+
for (const [signal, handler] of handlers) process.on(signal, handler);
|
|
595
|
+
let done = false;
|
|
596
|
+
const end = (code) => {
|
|
597
|
+
if (done) return;
|
|
598
|
+
done = true;
|
|
599
|
+
for (const [signal, handler] of handlers) process.off(signal, handler);
|
|
600
|
+
resolve(code);
|
|
601
|
+
};
|
|
602
|
+
try {
|
|
603
|
+
child = spawn(file, args, { stdio: "inherit", env, shell: false });
|
|
604
|
+
} catch (error) {
|
|
605
|
+
io.stderr.write(`Cannot run ${file} (${error.code ?? error.name}).\n`);
|
|
606
|
+
return end(126);
|
|
607
|
+
}
|
|
608
|
+
child.once("error", (error) => {
|
|
609
|
+
io.stderr.write(`Cannot run ${file} (${error.code ?? error.name}).\n`);
|
|
610
|
+
end(error.code === "ENOENT" ? 127 : 126);
|
|
611
|
+
});
|
|
612
|
+
child.once("exit", (code, signal) => end(code ?? 128 + (osConstants.signals[signal] ?? 0)));
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
|
|
523
616
|
async function run(argv, env, io) {
|
|
524
617
|
const [noun, verb] = words(argv);
|
|
525
618
|
if (noun === undefined || noun === "help") return io.stdout.write(`${HELP()}\n`), 0;
|
|
@@ -529,7 +622,12 @@ async function run(argv, env, io) {
|
|
|
529
622
|
if (own) return local(own, argv, env, io);
|
|
530
623
|
const entries = COMMANDS.filter((entry) => entry.command[0] === noun);
|
|
531
624
|
const listed = [...entries, ...LOCAL.filter((e) => e.command[0] === noun), ...(noun === WAIT.command[0] ? [WAIT] : [])];
|
|
532
|
-
if (!listed.length)
|
|
625
|
+
if (!listed.length) {
|
|
626
|
+
// No command of the CLI's own: agentrun-NAME from PATH, when NAME is the first argument, with every argument after it.
|
|
627
|
+
const file = argv[0] === noun ? externalOf(noun, env) : undefined;
|
|
628
|
+
if (file) return external(file, argv.slice(1), env, io);
|
|
629
|
+
throw usageError(`agentrun has no command ${noun}.\n\n${HELP()}`);
|
|
630
|
+
}
|
|
533
631
|
const entry = entries.find((e) => e.command.length === 1 || e.command[1] === verb);
|
|
534
632
|
if (entry) return command(entry, argv, env, io);
|
|
535
633
|
if (verb === undefined) return io.stdout.write(`${nounHelp(noun, listed)}\n`), 0;
|