@yaks/cli 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +1059 -2
- package/_dist/account-forward.d.ts +12 -0
- package/_dist/account-forward.d.ts.map +1 -0
- package/_dist/accounts.d.ts +23 -0
- package/_dist/accounts.d.ts.map +1 -0
- package/_dist/answer.d.ts +88 -0
- package/_dist/answer.d.ts.map +1 -0
- package/_dist/apply.d.ts +17 -0
- package/_dist/apply.d.ts.map +1 -0
- package/_dist/args.d.ts +86 -0
- package/_dist/args.d.ts.map +1 -0
- package/_dist/commands.d.ts +15 -0
- package/_dist/commands.d.ts.map +1 -0
- package/_dist/complete.d.ts +15 -0
- package/_dist/complete.d.ts.map +1 -0
- package/_dist/config.d.ts +37 -0
- package/_dist/config.d.ts.map +1 -0
- package/_dist/dependencies.d.ts +6 -0
- package/_dist/dependencies.d.ts.map +1 -0
- package/_dist/facets.d.ts +71 -0
- package/_dist/facets.d.ts.map +1 -0
- package/_dist/grammar.d.ts +5 -0
- package/_dist/grammar.d.ts.map +1 -0
- package/_dist/host.d.ts +164 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/install.d.ts +43 -0
- package/_dist/install.d.ts.map +1 -0
- package/_dist/main.d.ts +16 -0
- package/_dist/main.d.ts.map +1 -0
- package/_dist/memo.d.ts +60 -0
- package/_dist/memo.d.ts.map +1 -0
- package/_dist/mod.d.ts +46 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/page.d.ts +73 -0
- package/_dist/page.d.ts.map +1 -0
- package/_dist/platform.d.ts +27 -0
- package/_dist/platform.d.ts.map +1 -0
- package/_dist/reader.d.ts +10 -0
- package/_dist/reader.d.ts.map +1 -0
- package/_dist/release.d.ts +48 -0
- package/_dist/release.d.ts.map +1 -0
- package/_dist/remote.d.ts +13 -0
- package/_dist/remote.d.ts.map +1 -0
- package/_dist/reported.d.ts +11 -0
- package/_dist/reported.d.ts.map +1 -0
- package/_dist/roles.d.ts +2 -0
- package/_dist/roles.d.ts.map +1 -0
- package/_dist/roster.d.ts +41 -0
- package/_dist/roster.d.ts.map +1 -0
- package/_dist/rpc.d.ts +29 -0
- package/_dist/rpc.d.ts.map +1 -0
- package/_dist/run.d.ts +164 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/runtime.d.ts +4 -0
- package/_dist/runtime.d.ts.map +1 -0
- package/_dist/searching.d.ts +7 -0
- package/_dist/searching.d.ts.map +1 -0
- package/_dist/show.d.ts +22 -0
- package/_dist/show.d.ts.map +1 -0
- package/_dist/store.d.ts +25 -0
- package/_dist/store.d.ts.map +1 -0
- package/_dist/terminal.d.ts +17 -0
- package/_dist/terminal.d.ts.map +1 -0
- package/_dist/tool.d.ts +55 -0
- package/_dist/tool.d.ts.map +1 -0
- package/_dist/tty.d.ts +3 -0
- package/_dist/tty.d.ts.map +1 -0
- package/_dist/vault.d.ts +9 -0
- package/_dist/vault.d.ts.map +1 -0
- package/_dist/words.d.ts +11 -0
- package/_dist/words.d.ts.map +1 -0
- package/_dist/yak.d.ts +10 -0
- package/_dist/yak.d.ts.map +1 -0
- package/account-forward.js +40 -0
- package/account-forward.js.map +1 -0
- package/account-forward.ts +50 -0
- package/accounts.js +162 -0
- package/accounts.js.map +1 -0
- package/accounts.ts +228 -0
- package/answer.js +394 -0
- package/answer.js.map +1 -0
- package/answer.ts +569 -0
- package/apply.js +49 -0
- package/apply.js.map +1 -0
- package/apply.ts +54 -0
- package/args.js +420 -0
- package/args.js.map +1 -0
- package/args.ts +494 -0
- package/code.js +209 -0
- package/code.js.map +1 -0
- package/code.ts +227 -0
- package/commands.js +131 -0
- package/commands.js.map +1 -0
- package/commands.ts +142 -0
- package/complete.js +104 -0
- package/complete.js.map +1 -0
- package/complete.ts +152 -0
- package/config.js +125 -0
- package/config.js.map +1 -0
- package/config.ts +180 -0
- package/declarations.js +67 -0
- package/declarations.js.map +1 -0
- package/declarations.ts +84 -0
- package/deno.json +1 -0
- package/dependencies.js +126 -0
- package/dependencies.js.map +1 -0
- package/dependencies.ts +141 -0
- package/derive.js +102 -0
- package/derive.js.map +1 -0
- package/derive.ts +125 -0
- package/drain.js +83 -0
- package/drain.js.map +1 -0
- package/drain.ts +115 -0
- package/duties.ts +17 -0
- package/exports.json +1486 -0
- package/external.js +373 -0
- package/external.js.map +1 -0
- package/external.ts +435 -0
- package/facets.js +214 -0
- package/facets.js.map +1 -0
- package/facets.ts +232 -0
- package/feedback.js +54 -0
- package/feedback.js.map +1 -0
- package/feedback.ts +57 -0
- package/files.js +84 -0
- package/files.js.map +1 -0
- package/files.ts +112 -0
- package/get_starts.js +31 -0
- package/get_starts.js.map +1 -0
- package/get_starts.ts +34 -0
- package/git-credential.js +79 -0
- package/git-credential.js.map +1 -0
- package/git-credential.ts +77 -0
- package/grammar.js +4 -0
- package/grammar.js.map +1 -0
- package/grammar.ts +5 -0
- package/held.js +51 -0
- package/held.js.map +1 -0
- package/held.ts +66 -0
- package/host.js +1174 -0
- package/host.js.map +1 -0
- package/host.ts +1343 -0
- package/init.js +138 -0
- package/init.js.map +1 -0
- package/init.ts +139 -0
- package/install.js +216 -0
- package/install.js.map +1 -0
- package/install.ts +223 -0
- package/lifetime.js +56 -0
- package/lifetime.js.map +1 -0
- package/lifetime.ts +66 -0
- package/local-page.ts +114 -0
- package/local.js +417 -0
- package/local.js.map +1 -0
- package/local.ts +537 -0
- package/lockfile.js +20 -0
- package/lockfile.js.map +1 -0
- package/lockfile.ts +32 -0
- package/machines.js +33 -0
- package/machines.js.map +1 -0
- package/machines.ts +51 -0
- package/made/4f855610bdabcd736100d34e4e467ea5.json +1 -0
- package/main.js +534 -0
- package/main.js.map +1 -0
- package/main.ts +547 -0
- package/mcp.js +108 -0
- package/mcp.js.map +1 -0
- package/mcp.ts +113 -0
- package/member.js +103 -0
- package/member.js.map +1 -0
- package/member.ts +133 -0
- package/memo.js +503 -0
- package/memo.js.map +1 -0
- package/memo.ts +634 -0
- package/mod.js +46 -0
- package/mod.js.map +1 -0
- package/mod.ts +99 -0
- package/operations.js +53 -0
- package/operations.js.map +1 -0
- package/operations.ts +68 -0
- package/package.json +115 -4
- package/page.js +211 -0
- package/page.js.map +1 -0
- package/page.ts +308 -0
- package/parser.js +45 -0
- package/parser.js.map +1 -0
- package/parser.ts +77 -0
- package/people-search.js +112 -0
- package/people-search.js.map +1 -0
- package/people-search.ts +162 -0
- package/platform.js +184 -0
- package/platform.js.map +1 -0
- package/platform.ts +224 -0
- package/platform_effects.js +5 -0
- package/platform_effects.js.map +1 -0
- package/platform_effects.ts +13 -0
- package/prepare.js +40 -0
- package/prepare.js.map +1 -0
- package/prepare.ts +46 -0
- package/read_activity.js +54 -0
- package/read_activity.js.map +1 -0
- package/read_activity.ts +61 -0
- package/read_thread.js +387 -0
- package/read_thread.js.map +1 -0
- package/read_thread.ts +506 -0
- package/read_worker.ts +164 -0
- package/reader.js +48 -0
- package/reader.js.map +1 -0
- package/reader.ts +57 -0
- package/release.js +82 -0
- package/release.js.map +1 -0
- package/release.json +127 -0
- package/release.ts +98 -0
- package/remote-page.ts +70 -0
- package/remote.js +96 -0
- package/remote.js.map +1 -0
- package/remote.ts +153 -0
- package/report.js +83 -0
- package/report.js.map +1 -0
- package/report.ts +101 -0
- package/reported.js +32 -0
- package/reported.js.map +1 -0
- package/reported.ts +38 -0
- package/restart.js +349 -0
- package/restart.js.map +1 -0
- package/restart.ts +478 -0
- package/restore.js +32 -0
- package/restore.js.map +1 -0
- package/restore.ts +38 -0
- package/roles.js +32 -0
- package/roles.js.map +1 -0
- package/roles.ts +49 -0
- package/roster.js +65 -0
- package/roster.js.map +1 -0
- package/roster.ts +84 -0
- package/rpc.js +113 -0
- package/rpc.js.map +1 -0
- package/rpc.ts +159 -0
- package/run.js +533 -0
- package/run.js.map +1 -0
- package/run.ts +741 -0
- package/runtime.js +381 -0
- package/runtime.js.map +1 -0
- package/runtime.ts +433 -0
- package/sandboxes.js +26 -0
- package/sandboxes.js.map +1 -0
- package/sandboxes.ts +30 -0
- package/searching.js +33 -0
- package/searching.js.map +1 -0
- package/searching.ts +50 -0
- package/show.js +109 -0
- package/show.js.map +1 -0
- package/show.ts +144 -0
- package/shown.js +13 -0
- package/shown.js.map +1 -0
- package/shown.ts +44 -0
- package/stall.js +90 -0
- package/stall.js.map +1 -0
- package/stall.ts +156 -0
- package/stall_profile.js +113 -0
- package/stall_profile.js.map +1 -0
- package/stall_profile.ts +142 -0
- package/stall_wait.js +183 -0
- package/stall_wait.js.map +1 -0
- package/stall_wait.ts +249 -0
- package/stall_wait_worker.ts +11 -0
- package/start.js +349 -0
- package/start.js.map +1 -0
- package/start.ts +343 -0
- package/starvation.js +147 -0
- package/starvation.js.map +1 -0
- package/starvation.ts +224 -0
- package/store.js +118 -0
- package/store.js.map +1 -0
- package/store.ts +193 -0
- package/subcommands.js +62 -0
- package/subcommands.js.map +1 -0
- package/subcommands.ts +77 -0
- package/terminal.js +93 -0
- package/terminal.js.map +1 -0
- package/terminal.ts +128 -0
- package/text.js +20 -0
- package/text.js.map +1 -0
- package/text.ts +37 -0
- package/tool.js +76 -0
- package/tool.js.map +1 -0
- package/tool.ts +157 -0
- package/trace_control.js +231 -0
- package/trace_control.js.map +1 -0
- package/trace_control.ts +233 -0
- package/traces.js +138 -0
- package/traces.js.map +1 -0
- package/traces.ts +160 -0
- package/tracker.js +22 -0
- package/tracker.js.map +1 -0
- package/tracker.ts +27 -0
- package/tty.js +82 -0
- package/tty.js.map +1 -0
- package/tty.ts +142 -0
- package/unit_failure.js +178 -0
- package/unit_failure.js.map +1 -0
- package/unit_failure.ts +191 -0
- package/upkeep_thread.js +32 -0
- package/upkeep_thread.js.map +1 -0
- package/upkeep_thread.ts +35 -0
- package/upkeep_worker.ts +27 -0
- package/vault.js +186 -0
- package/vault.js.map +1 -0
- package/vault.ts +220 -0
- package/vocab-ids.js +63 -0
- package/vocab-ids.js.map +1 -0
- package/vocab-ids.ts +62 -0
- package/vocab.json +35 -0
- package/warm.js +138 -0
- package/warm.js.map +1 -0
- package/warm.ts +171 -0
- package/watch_revision.js +25 -0
- package/watch_revision.js.map +1 -0
- package/watch_revision.ts +30 -0
- package/words.js +29 -0
- package/words.js.map +1 -0
- package/words.ts +41 -0
- package/workspaces.js +40 -0
- package/workspaces.js.map +1 -0
- package/workspaces.ts +58 -0
- package/yak-unit-failure.conf +6 -0
- package/yak-unit-handoff.conf +2 -0
- package/yak.js +123 -0
- package/yak.js.map +1 -0
- package/yak.ts +136 -0
package/run.ts
ADDED
|
@@ -0,0 +1,741 @@
|
|
|
1
|
+
import * as system from '@yaks/process/system'
|
|
2
|
+
import * as files from '@yaks/file/io'
|
|
3
|
+
// One command line, run. `cli(commands, opts)` is the whole interface: hand
|
|
4
|
+
// it the commands and it reads the command line — the subcommand, either order
|
|
5
|
+
// of a two-word tool, the arguments through that tool's own input schema —
|
|
6
|
+
// runs the one it found, and returns the exit code.
|
|
7
|
+
//
|
|
8
|
+
// A command is a @yaks/graph `Tool` declaration and nothing else. There is no
|
|
9
|
+
// registration format between a tool and the command line that runs it: a
|
|
10
|
+
// program that wants more subcommands passes more commands, and the first to
|
|
11
|
+
// claim a name wins, so the order of the list is the precedence.
|
|
12
|
+
//
|
|
13
|
+
// Where a command runs is {@link aimed}: the graph a config file names, opened
|
|
14
|
+
// in this process, or an MCP server to call over `/mcp`. The file is the
|
|
15
|
+
// ordinary case — a graph this machine can open needs nothing listening — and
|
|
16
|
+
// an MCP server is for a graph it cannot open.
|
|
17
|
+
//
|
|
18
|
+
// Two things cost more to gather than a list does, and both are opts rather
|
|
19
|
+
// than commands. `more` is a list that has to be fetched — the tools of the
|
|
20
|
+
// graph this command opens, or an MCP server's `tools/list` — asked for only
|
|
21
|
+
// when the commands already in hand did not match the first word, so
|
|
22
|
+
// `yak login` still works with no graph in sight, and printed on the usage
|
|
23
|
+
// page as a reason when it cannot be had. A list that came from a cache is
|
|
24
|
+
// trusted for what it holds, never for what it lacks: a word it does not have
|
|
25
|
+
// is asked of the source again (`fresh`) before anything else reads it, since
|
|
26
|
+
// the server behind a cached address may list tools the cache never saw.
|
|
27
|
+
// `stray` is the other half: a first word nothing claimed. `yak recipes add_recipe` is an app and one of its
|
|
28
|
+
// commands, and only something that knows about apps can recognize that, so it
|
|
29
|
+
// is asked last and only when nothing else matched.
|
|
30
|
+
|
|
31
|
+
import type { Tool, ToolId } from 'jsr:@yaks/graph@0.2.36'
|
|
32
|
+
import { argsFor, commandFor, type Grammar, type Reads, Usage } from './args.js'
|
|
33
|
+
import { lineOf, safe, sketch, toolHelp } from './show.js'
|
|
34
|
+
import { commandOf, titleOf } from './tool.js'
|
|
35
|
+
import { timed } from '@yaks/api/timing'
|
|
36
|
+
import { doorUrl, type Rpc, rpc, Unauthorized } from './rpc.js'
|
|
37
|
+
import { configPath } from './config.js'
|
|
38
|
+
import type { Tty } from './answer.ts'
|
|
39
|
+
import { type Env, stateDir, tokenFor } from './store.js'
|
|
40
|
+
|
|
41
|
+
// This process's environment: what every `env` seam here defaults to.
|
|
42
|
+
let own: Env = (name) => system.env.get(name)
|
|
43
|
+
|
|
44
|
+
/** What a command is handed: where it runs, where to print, and the global
|
|
45
|
+
* flags. */
|
|
46
|
+
export type Ctx = {
|
|
47
|
+
/** The MCP server this command talks to, where it talks to one — and the
|
|
48
|
+
* name its bearer token is stored under either way. */
|
|
49
|
+
host: string
|
|
50
|
+
/** Optional account name; absent uses the computed default. */
|
|
51
|
+
as?: string
|
|
52
|
+
/** The config file naming the graph this command opens, in this process.
|
|
53
|
+
* Absent where the command named an MCP server instead ({@link aimed}). */
|
|
54
|
+
config?: string
|
|
55
|
+
/** Whether this command may run duties if it explicitly starts them —
|
|
56
|
+
* false under `--no-duties`: no inline duties; web still restores external workers. */
|
|
57
|
+
duties: boolean
|
|
58
|
+
json: boolean
|
|
59
|
+
/** Whether an answer is held in the terminal (@yaks/tui) rather than
|
|
60
|
+
* printed — `--tui`. */
|
|
61
|
+
tui: boolean
|
|
62
|
+
/** Where an answer is painted rather than printed: present when stdout is
|
|
63
|
+
* a terminal (and `NO_COLOR` is unset), with its width and a writer that
|
|
64
|
+
* keeps the painter's escapes — the painter strips content's own. */
|
|
65
|
+
tty?: Tty
|
|
66
|
+
help: boolean
|
|
67
|
+
/** The session this command line speaks for ({@link via}), named the same
|
|
68
|
+
* way to a host over HTTP and to a graph this process opened. */
|
|
69
|
+
via?: string
|
|
70
|
+
/** The directory this machine keeps its token and tool lists in
|
|
71
|
+
* (./store.ts): its own, unless a test hands in a scratch one. */
|
|
72
|
+
state?: string
|
|
73
|
+
ask: Rpc
|
|
74
|
+
reads: Reads
|
|
75
|
+
out: (line: string) => void
|
|
76
|
+
note: (line: string) => void
|
|
77
|
+
/** Every command this run can reach, including the ones that cost a
|
|
78
|
+
* composition or a round trip. Gathered once; a list that cannot be fetched
|
|
79
|
+
* is left out. */
|
|
80
|
+
all: () => Promise<Command[]>
|
|
81
|
+
/** The usage page — every tool one line each, and why a list is missing. */
|
|
82
|
+
page: () => Promise<string>
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A terminal an answer is painted on: how wide it is, and where its bytes go. */
|
|
86
|
+
export type { Tty } from './answer.ts'
|
|
87
|
+
|
|
88
|
+
// This process's stdout, where it is a terminal a person reads in color. A
|
|
89
|
+
// terminal that reports no size (`script` run without one) is the usual 80
|
|
90
|
+
// wide, rather than a width nothing fits in.
|
|
91
|
+
let terminal = (env: Env): Tty | undefined =>
|
|
92
|
+
system.stdout.isTerminal() && !env('NO_COLOR')
|
|
93
|
+
? {
|
|
94
|
+
columns: system.consoleSize().columns || 80,
|
|
95
|
+
write: (t) => console.log(t),
|
|
96
|
+
}
|
|
97
|
+
: undefined
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* One command this program can run: a tool's declaration — its name, its
|
|
101
|
+
* schema, how it is written on a command line — with a run of its own.
|
|
102
|
+
*
|
|
103
|
+
* A graph tool is `run(call, graph) → bundles` and only @yaks/tools' runner
|
|
104
|
+
* calls one. A command is the other end: it takes the arguments parsed off the
|
|
105
|
+
* command line, prints, and returns an exit code. The same declaration, so a
|
|
106
|
+
* command is listed, helped and tab-completed from the one schema; a different
|
|
107
|
+
* run, because a command line is not a tool call. `yak <tool>` is a command
|
|
108
|
+
* wrapping a call — local.ts writes one against the graph this command opened,
|
|
109
|
+
* platform.ts sends one to the MCP server it named.
|
|
110
|
+
*/
|
|
111
|
+
export type Command = Omit<Tool, 'run' | 'recover'> & {
|
|
112
|
+
/** Global flags explicitly bound to this command's input properties. */
|
|
113
|
+
globals?: readonly GlobalFlag[]
|
|
114
|
+
/** Whether the usage page lists it here: a command for something this
|
|
115
|
+
* line's graph does not have (a box's process plan) is still run when
|
|
116
|
+
* typed and still has its own page, but is not offered. */
|
|
117
|
+
offered?: (c: Ctx) => boolean
|
|
118
|
+
run: (
|
|
119
|
+
args: Record<string, unknown>,
|
|
120
|
+
c: Ctx,
|
|
121
|
+
) => number | Promise<number>
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type GlobalFlag =
|
|
125
|
+
| 'as'
|
|
126
|
+
| 'host'
|
|
127
|
+
| 'config'
|
|
128
|
+
| 'duties'
|
|
129
|
+
| 'json'
|
|
130
|
+
| 'tui'
|
|
131
|
+
| 'help'
|
|
132
|
+
| 'timing'
|
|
133
|
+
|
|
134
|
+
/** What a program brings besides its tools. */
|
|
135
|
+
export type Opts = {
|
|
136
|
+
/** The words to read. Defaults to `system.args`. */
|
|
137
|
+
argv?: string[]
|
|
138
|
+
/** What the program calls itself, in its usage and its help pages. */
|
|
139
|
+
name?: string
|
|
140
|
+
/** The line the usage page opens with. */
|
|
141
|
+
about?: string
|
|
142
|
+
/** The release this program is, which `--version` prints. */
|
|
143
|
+
version?: string
|
|
144
|
+
/** The notes it closes with. */
|
|
145
|
+
notes?: string
|
|
146
|
+
/** Global flags reserved by the program; tool inputs use name=value. */
|
|
147
|
+
globalFlags?: readonly string[]
|
|
148
|
+
/** The MCP server a command talks to where it names neither a config nor a
|
|
149
|
+
* host. */
|
|
150
|
+
host?: string
|
|
151
|
+
/** More commands, where gathering them costs a composition or a round
|
|
152
|
+
* trip. `fresh` asks past any cache the list is kept in. */
|
|
153
|
+
more?: (c: Ctx, o?: { fresh?: boolean }) => Command[] | Promise<Command[]>
|
|
154
|
+
/** What to say where a line named no place and this machine keeps no graph,
|
|
155
|
+
* and the host it fell to would not answer it (`aimed`'s `unplaced`). */
|
|
156
|
+
nowhere?: string
|
|
157
|
+
/** A command for a first word nothing else claimed. */
|
|
158
|
+
stray?: (
|
|
159
|
+
word: string,
|
|
160
|
+
args: string[],
|
|
161
|
+
c: Ctx,
|
|
162
|
+
) => Command | undefined | Promise<Command | undefined>
|
|
163
|
+
/** How a JSON-RPC request is sent, where a program has a caller of its own
|
|
164
|
+
* — a test passes a function that records what it was asked. */
|
|
165
|
+
ask?: Rpc
|
|
166
|
+
/** The session this command line speaks for. Defaults to {@link via}. */
|
|
167
|
+
via?: string
|
|
168
|
+
/** Where the environment is read from: this process's by default. A test
|
|
169
|
+
* passes a function, so it never sets a variable every other test shares. */
|
|
170
|
+
env?: Env
|
|
171
|
+
/** Where a value written `@path` or `-` is read from. */
|
|
172
|
+
reads?: Reads
|
|
173
|
+
out?: (line: string) => void
|
|
174
|
+
note?: (line: string) => void
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The tools, refused where two of them answer to one command line. A two-word
|
|
179
|
+
* tool is reachable in either order, so `session list` and `list session` are
|
|
180
|
+
* the same tool, and a second tool using either pair is a command line that
|
|
181
|
+
* means two things.
|
|
182
|
+
*
|
|
183
|
+
* `cli` does not apply this to the whole list, because shadowing is the point
|
|
184
|
+
* of the order: a program that carries its own `login` means it. A contributor
|
|
185
|
+
* runs this over its own list, where two tools of one name are a mistake.
|
|
186
|
+
*/
|
|
187
|
+
export let unique = <T extends ToolId>(tools: readonly T[]): readonly T[] => {
|
|
188
|
+
let said = new Set<string>()
|
|
189
|
+
for (let t of tools) {
|
|
190
|
+
let words = t.noun && t.verb
|
|
191
|
+
? [`${t.noun} ${t.verb}`, `${t.verb} ${t.noun}`]
|
|
192
|
+
: [commandOf(t)]
|
|
193
|
+
if (!words[0]) throw new Error('a tool needs a name, or a noun and a verb')
|
|
194
|
+
for (let w of new Set(words)) {
|
|
195
|
+
if (said.has(w)) throw new Error(`two tools answer to: ${w}`)
|
|
196
|
+
said.add(w)
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return tools
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The tool a command line names, and the words left for its schema. A two-word
|
|
204
|
+
* tool takes the word after it in either order; a one-word tool takes none.
|
|
205
|
+
*
|
|
206
|
+
* ```ts
|
|
207
|
+
* commandFor([{noun: 'session', verb: 'list', description: '', run: () => 0}],
|
|
208
|
+
* ['list', 'session', '--all'])?.args // ['--all']
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
export { commandFor } from './args.js'
|
|
212
|
+
|
|
213
|
+
// One column across the page, so it reads as one page. Capped: a long
|
|
214
|
+
// argument sketch pushes its own line out rather than every other line.
|
|
215
|
+
let WIDE = 30
|
|
216
|
+
|
|
217
|
+
/** One tool per listed name, in the order they were given: where two answer
|
|
218
|
+
* to one command line, the first one wins. */
|
|
219
|
+
let once = <T extends Grammar>(tools: readonly T[]): T[] => {
|
|
220
|
+
let seen = new Set<string>()
|
|
221
|
+
return tools.filter((t) => !seen.has(commandOf(t)) && seen.add(commandOf(t)))
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The tools grouped under the noun each is typed with: `graph` holds `apply`,
|
|
226
|
+
* `query` and the rest, and a tool that declared one word alone — or none — is
|
|
227
|
+
* grouped under `''`, which is the page's own first block. Nouns come in the
|
|
228
|
+
* order they were first named, so the order of the list is still the order of
|
|
229
|
+
* the page.
|
|
230
|
+
*
|
|
231
|
+
* ```ts
|
|
232
|
+
* nouns([{ noun: 'graph', verb: 'apply', description: '', run: () => 0 }])
|
|
233
|
+
* .map(([noun]) => noun) // ['graph']
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
export let nouns = <T extends Grammar>(
|
|
237
|
+
tools: readonly T[],
|
|
238
|
+
): [string, T[]][] => {
|
|
239
|
+
let by = new Map<string, T[]>([['', []]])
|
|
240
|
+
for (let t of once(tools)) {
|
|
241
|
+
let noun = t.noun && t.verb ? t.noun : ''
|
|
242
|
+
let said = by.get(noun) ?? []
|
|
243
|
+
by.set(noun, said)
|
|
244
|
+
said.push(t)
|
|
245
|
+
}
|
|
246
|
+
return [...by].filter(([, said]) => said.length)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
type Listed = Grammar & {
|
|
250
|
+
title?: string
|
|
251
|
+
description?: string
|
|
252
|
+
globals?: Command['globals']
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// The line to type, with the heading's own word left out: under `graph`, a
|
|
256
|
+
// tool is its verb and its arguments.
|
|
257
|
+
let lineIn = (noun: string, t: Listed, omit: readonly string[]): string =>
|
|
258
|
+
noun
|
|
259
|
+
? `${t.verb} ${sketch(t, t.globals, omit)}`.trimEnd()
|
|
260
|
+
: lineOf(t, t.globals, omit)
|
|
261
|
+
|
|
262
|
+
let helpFor = (t: Command, opts: Opts): string =>
|
|
263
|
+
toolHelp(
|
|
264
|
+
t,
|
|
265
|
+
opts.name,
|
|
266
|
+
(opts.globalFlags ?? []).filter((n) =>
|
|
267
|
+
!t.globals?.includes(n as GlobalFlag)
|
|
268
|
+
),
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
type Line = { text: string; title: string }
|
|
272
|
+
let lines = (
|
|
273
|
+
noun: string,
|
|
274
|
+
said: Listed[],
|
|
275
|
+
omit: readonly string[],
|
|
276
|
+
): Line[] =>
|
|
277
|
+
said.map((t) => ({
|
|
278
|
+
text: lineIn(noun, t, omit),
|
|
279
|
+
title: titleOf(t),
|
|
280
|
+
}))
|
|
281
|
+
|
|
282
|
+
let block = (noun: string, said: Line[], wide: number): string[] => [
|
|
283
|
+
...(noun ? ['', noun] : []),
|
|
284
|
+
...said.map(({ text, title }) =>
|
|
285
|
+
` ${text.padEnd(wide)} ${title}`.trimEnd()
|
|
286
|
+
),
|
|
287
|
+
]
|
|
288
|
+
|
|
289
|
+
// Measure the same lines that are printed, without sketching their arguments
|
|
290
|
+
// again: a help page renders every tool's schema only once.
|
|
291
|
+
let column = (groups: [string, Line[]][]): number =>
|
|
292
|
+
Math.min(
|
|
293
|
+
WIDE,
|
|
294
|
+
Math.max(
|
|
295
|
+
0,
|
|
296
|
+
...groups.flatMap(([, said]) => said.map((l) => l.text.length)),
|
|
297
|
+
),
|
|
298
|
+
)
|
|
299
|
+
|
|
300
|
+
/** Every tool, one line each, its verbs grouped under their noun. */
|
|
301
|
+
export let usage = (
|
|
302
|
+
tools: readonly Listed[],
|
|
303
|
+
opts: Opts = {},
|
|
304
|
+
): string => {
|
|
305
|
+
let groups: [string, Line[]][] = nouns(tools).map((
|
|
306
|
+
[noun, said],
|
|
307
|
+
) => [noun, lines(noun, said, opts.globalFlags ?? [])])
|
|
308
|
+
let wide = column(groups)
|
|
309
|
+
return [
|
|
310
|
+
...(opts.about ? [opts.about, ''] : []),
|
|
311
|
+
...groups.flatMap(([noun, said]) => block(noun, said, wide)),
|
|
312
|
+
...(opts.notes ? ['', opts.notes] : []),
|
|
313
|
+
].join('\n')
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** One noun's own page: its verbs, one line each, under the word itself —
|
|
317
|
+
* what `yak graph` and `yak graph --help` print. Nothing where no tool here is
|
|
318
|
+
* typed under that word. */
|
|
319
|
+
export let nounUsage = (
|
|
320
|
+
tools: readonly Listed[],
|
|
321
|
+
word: string,
|
|
322
|
+
reserved: readonly string[] = [],
|
|
323
|
+
): string | undefined => {
|
|
324
|
+
let said = nouns(tools).find(([noun]) => noun && noun == word)
|
|
325
|
+
if (!said) return undefined
|
|
326
|
+
let group: [string, Line[]] = [said[0], lines(said[0], said[1], reserved)]
|
|
327
|
+
return block(group[0], group[1], column([group])).slice(1)
|
|
328
|
+
.join('\n')
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** The flags a program keeps for itself, lifted off the command line before a
|
|
332
|
+
* command ever sees it. `host` and `config` are what the command line gave and
|
|
333
|
+
* nothing else — where a command runs when it gives neither is
|
|
334
|
+
* {@link aimed}'s answer. */
|
|
335
|
+
export let globals = (
|
|
336
|
+
argv: readonly string[],
|
|
337
|
+
env: Env = own,
|
|
338
|
+
): {
|
|
339
|
+
host?: string
|
|
340
|
+
config?: string
|
|
341
|
+
as?: string
|
|
342
|
+
duties: boolean
|
|
343
|
+
json: boolean
|
|
344
|
+
tui: boolean
|
|
345
|
+
help: boolean
|
|
346
|
+
version: boolean
|
|
347
|
+
timing: boolean
|
|
348
|
+
rest: string[]
|
|
349
|
+
} => {
|
|
350
|
+
let host: string | undefined
|
|
351
|
+
let json = false
|
|
352
|
+
let tui = false
|
|
353
|
+
let duties = true
|
|
354
|
+
let help = false
|
|
355
|
+
let version = false
|
|
356
|
+
let config: string | undefined
|
|
357
|
+
// A whole process tree runs as one account with YAK_AS (`yak admin
|
|
358
|
+
// throwaway`); one command asks with the flag.
|
|
359
|
+
let as = env('YAK_AS') || undefined
|
|
360
|
+
// A whole shell asks for the timing line with YAKS_TIMING=1; one command
|
|
361
|
+
// asks with the flag.
|
|
362
|
+
let timing = env('YAKS_TIMING') == '1'
|
|
363
|
+
let rest: string[] = []
|
|
364
|
+
for (let i = 0; i < argv.length; i++) {
|
|
365
|
+
let a = argv[i]
|
|
366
|
+
if (a == '--json') json = true
|
|
367
|
+
else if (a == '--tui') tui = true
|
|
368
|
+
else if (a == '--no-duties') duties = false
|
|
369
|
+
else if (a == '--help' || a == '-h') help = true
|
|
370
|
+
// After a command's name, --version is one of its arguments
|
|
371
|
+
// (`app_rollback --version 3`), not a question about this program.
|
|
372
|
+
else if (a == '--version' && !rest.length) version = true
|
|
373
|
+
else if (a == '--timing') timing = true
|
|
374
|
+
else if (a == '--as') as = argv[++i] ?? as
|
|
375
|
+
else if (a.startsWith('--as=')) as = a.slice(5)
|
|
376
|
+
else if (a == '--host') host = argv[++i] ?? host
|
|
377
|
+
else if (a.startsWith('--host=')) host = a.slice(7)
|
|
378
|
+
else if (a == '--config') config = argv[++i] ?? config
|
|
379
|
+
else if (a.startsWith('--config=')) config = a.slice(9)
|
|
380
|
+
else rest.push(a)
|
|
381
|
+
}
|
|
382
|
+
return { host, config, as, duties, json, tui, help, version, timing, rest }
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Where this command runs what it was asked: the graph a config file names,
|
|
387
|
+
* which it opens in this process, or an MCP server it calls over `/mcp`.
|
|
388
|
+
*
|
|
389
|
+
* A config names A graph. `yak --config yak.json task list` reads that file,
|
|
390
|
+
* composes its plugins over the SQLite file it names, runs the tool here and
|
|
391
|
+
* exits — no server, and nothing to wait for. `--host` is for a graph this
|
|
392
|
+
* machine cannot open as a file; given together the two name two places, which
|
|
393
|
+
* is a command line that means two things.
|
|
394
|
+
*
|
|
395
|
+
* In order: `--config`, `--host`, `$YAKS_HOST`, `$YAK_CONFIG`, the config this
|
|
396
|
+
* machine keeps for its own graph (`~/.yak/yak.json`), and the platform this
|
|
397
|
+
* program came with. A host name comes back either way, because it is also the
|
|
398
|
+
* name a bearer token is stored under.
|
|
399
|
+
*
|
|
400
|
+
* A config the line named is the one place the line means. A config found in
|
|
401
|
+
* the environment or at home is only the first place in the order, so a word
|
|
402
|
+
* its graph lacks goes `onward`, to the host: `yak app_list` on a machine that
|
|
403
|
+
* keeps a graph of its own still reaches the platform.
|
|
404
|
+
*
|
|
405
|
+
* A line that said nothing, on a machine that keeps no graph, falls to the
|
|
406
|
+
* platform only by default: it is `unplaced`, and a platform that will not
|
|
407
|
+
* answer it is not what the person was asking about.
|
|
408
|
+
*
|
|
409
|
+
* ```ts
|
|
410
|
+
* aimed({ host: 'yaks.app' }) // { host: 'yaks.app' }
|
|
411
|
+
* aimed({}, 'yaks.app', () => undefined) // { host: 'yaks.app', unplaced: true }
|
|
412
|
+
* aimed({}, 'yaks.app', (n) => n == 'YAK_CONFIG' ? 'yak.json' : undefined)
|
|
413
|
+
* // { config: 'yak.json', host: 'yaks.app', onward: true }
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
export let aimed = (
|
|
417
|
+
said: { host?: string; config?: string },
|
|
418
|
+
dflt = 'yaks.app',
|
|
419
|
+
env: Env = own,
|
|
420
|
+
): { config?: string; host: string; onward?: boolean; unplaced?: boolean } => {
|
|
421
|
+
if (said.host && said.config) {
|
|
422
|
+
throw new Usage('--host and --config name two places — a line names one')
|
|
423
|
+
}
|
|
424
|
+
if (said.config) return { config: said.config, host: dflt }
|
|
425
|
+
let door = said.host ?? env('YAKS_HOST')
|
|
426
|
+
if (door) return { host: door }
|
|
427
|
+
let path = configPath(undefined, env)
|
|
428
|
+
return path
|
|
429
|
+
? { config: path, host: dflt, onward: true }
|
|
430
|
+
: { host: dflt, unplaced: true }
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/** Claude Code keeps its session beside the process that starts an MCP
|
|
434
|
+
* client. Reading that parent's record preserves identity over stdio. */
|
|
435
|
+
export let parentVia = (
|
|
436
|
+
env: Env = own,
|
|
437
|
+
parent = system.ppid,
|
|
438
|
+
read: (path: string) => string = files.readTextFileSync,
|
|
439
|
+
): string | undefined => {
|
|
440
|
+
let directory = env('CLAUDE_CONFIG_DIR') ||
|
|
441
|
+
(env('HOME') ? `${env('HOME')}/.claude` : undefined)
|
|
442
|
+
if (!directory) return
|
|
443
|
+
try {
|
|
444
|
+
let session = JSON.parse(read(`${directory}/sessions/${parent}.json`))
|
|
445
|
+
return typeof session.sessionId == 'string' ? session.sessionId : undefined
|
|
446
|
+
} catch {
|
|
447
|
+
return undefined
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* The run this command line is part of, as the environment names it: a harness
|
|
453
|
+
* sets it, every shell and hook under that harness inherits it, and a write
|
|
454
|
+
* made from any of them carries the transcript that produced it without
|
|
455
|
+
* anybody passing a flag. It is sent in the `x-via` header, which the server
|
|
456
|
+
* resolves to the session, and what the command writes is signed with it
|
|
457
|
+
* (@yaks/session/graph). A graph this process opened itself is asked the
|
|
458
|
+
* same way, through the same door (local.ts).
|
|
459
|
+
*
|
|
460
|
+
* The variable names are the harnesses' own. Claude Code hands a subagent its
|
|
461
|
+
* parent's session id and marks the shell as an agent's (`AI_AGENT=…_agent`),
|
|
462
|
+
* so the one place a subagent's own id shows is the worktree Claude Code made
|
|
463
|
+
* for it, `.claude/worktrees/agent-<id>`: a command a subagent runs there
|
|
464
|
+
* speaks for that subagent, whose session the harness's `SubagentStart` hook
|
|
465
|
+
* wrote (@yaks/session `session_context`). A subagent working in its parent's
|
|
466
|
+
* directory is told apart by nothing in its environment, and speaks for the
|
|
467
|
+
* parent.
|
|
468
|
+
*/
|
|
469
|
+
export let via = (
|
|
470
|
+
env: Env = own,
|
|
471
|
+
cwd = here(),
|
|
472
|
+
parent: () => string | undefined = () => parentVia(env),
|
|
473
|
+
): string | undefined => {
|
|
474
|
+
let claude = env('CLAUDE_CODE_SESSION_ID')
|
|
475
|
+
let agent = claude && env('AI_AGENT')?.endsWith('_agent')
|
|
476
|
+
? cwd.match(WORKTREE)?.[1]
|
|
477
|
+
: undefined
|
|
478
|
+
return agent ?? claude ?? env('TASKS_SESSION') ?? env('CODEX_THREAD_ID') ??
|
|
479
|
+
parent()
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// The worktree Claude Code makes for a subagent, named by the subagent's id.
|
|
483
|
+
let WORKTREE = /\/\.claude\/worktrees\/agent-([0-9a-f]+)(?:\/|$)/
|
|
484
|
+
|
|
485
|
+
// This process's directory, or none where it was removed under it.
|
|
486
|
+
let here = (): string => {
|
|
487
|
+
try {
|
|
488
|
+
return system.cwd()
|
|
489
|
+
} catch {
|
|
490
|
+
return ''
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
let disk: Reads = {
|
|
495
|
+
cwd: system.cwd,
|
|
496
|
+
file: (path) => files.readTextFile(path),
|
|
497
|
+
stdin: () => new Response(system.stdin.readable).text(),
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Run one command line. Returns the exit code a script reads: 0 succeeded, 1
|
|
502
|
+
* the tool or the server refused, 2 the command line was wrong.
|
|
503
|
+
*/
|
|
504
|
+
export let cli = async (
|
|
505
|
+
tools: readonly Command[],
|
|
506
|
+
opts: Opts = {},
|
|
507
|
+
): Promise<number> => {
|
|
508
|
+
let out = opts.out ?? ((line: string) => console.log(safe(line)))
|
|
509
|
+
let note = opts.note ?? ((line: string) => console.error(safe(line)))
|
|
510
|
+
let env = opts.env ?? own
|
|
511
|
+
let said = globals(opts.argv ?? system.args, env)
|
|
512
|
+
let { duties, json, tui, help, timing, rest, as } = said
|
|
513
|
+
// Like the usage page, the version needs no graph, no network and nobody
|
|
514
|
+
// signed in.
|
|
515
|
+
if (said.version && opts.version) {
|
|
516
|
+
out(opts.version)
|
|
517
|
+
return 0
|
|
518
|
+
}
|
|
519
|
+
// Where nothing placed this line, the host's refusal is not the answer: the
|
|
520
|
+
// program's own word for a machine with no graph is.
|
|
521
|
+
let lost = (why: string) => why
|
|
522
|
+
try {
|
|
523
|
+
// Where this command runs: a file it opens, or an MCP server it calls. A
|
|
524
|
+
// command line naming both is refused here, like any other that means two
|
|
525
|
+
// things.
|
|
526
|
+
let { config, host, onward, unplaced } = aimed(
|
|
527
|
+
said,
|
|
528
|
+
opts.host ?? 'yaks.app',
|
|
529
|
+
env,
|
|
530
|
+
)
|
|
531
|
+
lost = (why: string) => unplaced && opts.nowhere ? opts.nowhere : why
|
|
532
|
+
let state = stateDir(env)
|
|
533
|
+
// A list that cannot be fetched is a reason printed on the page, not a
|
|
534
|
+
// page nobody gets: `yak` with no argument is what a person types when
|
|
535
|
+
// nothing works.
|
|
536
|
+
let extra: Command[] | undefined
|
|
537
|
+
let why: string | undefined
|
|
538
|
+
let asked = false
|
|
539
|
+
let fetched = async (fresh = false): Promise<Command[]> => {
|
|
540
|
+
if (!opts.more || (extra && (!fresh || asked))) return extra ?? []
|
|
541
|
+
asked ||= fresh
|
|
542
|
+
try {
|
|
543
|
+
extra = [...await opts.more(c, { fresh })]
|
|
544
|
+
why = undefined
|
|
545
|
+
} catch (e) {
|
|
546
|
+
extra = []
|
|
547
|
+
why = (e as Error).message
|
|
548
|
+
}
|
|
549
|
+
return extra
|
|
550
|
+
}
|
|
551
|
+
let speaks = opts.via ?? via(env)
|
|
552
|
+
let c: Ctx = {
|
|
553
|
+
host,
|
|
554
|
+
config,
|
|
555
|
+
as,
|
|
556
|
+
duties,
|
|
557
|
+
json,
|
|
558
|
+
tui,
|
|
559
|
+
// Only this process's own stdout can be a terminal: a caller that hands
|
|
560
|
+
// in `out` reads text.
|
|
561
|
+
tty: opts.out ? undefined : terminal(env),
|
|
562
|
+
help,
|
|
563
|
+
via: speaks,
|
|
564
|
+
state,
|
|
565
|
+
ask: opts.ask ?? rpc({
|
|
566
|
+
url: doorUrl(host),
|
|
567
|
+
token: () =>
|
|
568
|
+
tokenFor(host, state, { config, as, env, agent: !!speaks }),
|
|
569
|
+
via: speaks,
|
|
570
|
+
fetch: timing ? timed(note) : undefined,
|
|
571
|
+
}),
|
|
572
|
+
reads: opts.reads ?? disk,
|
|
573
|
+
out,
|
|
574
|
+
note,
|
|
575
|
+
all: async () => [...tools, ...await fetched()],
|
|
576
|
+
page: async () =>
|
|
577
|
+
usage((await c.all()).filter((t) => t.offered?.(c) ?? true), opts) +
|
|
578
|
+
(why ? `\n\n (${lost(why)})` : ''),
|
|
579
|
+
}
|
|
580
|
+
// The one thing that must work with no network and nobody signed in.
|
|
581
|
+
if (!rest.length) {
|
|
582
|
+
out(await c.page())
|
|
583
|
+
return 0
|
|
584
|
+
}
|
|
585
|
+
// A stray is asked of the host, so a line that named a graph of its own
|
|
586
|
+
// has none: it names that graph, and a word the graph lacks is a word
|
|
587
|
+
// the graph lacks, not an app somewhere else. A graph the line did not
|
|
588
|
+
// name was only first in the order (`aimed`), so a word it lacks is
|
|
589
|
+
// asked of the host, exactly as a line aimed there would ask it.
|
|
590
|
+
let far: Ctx = { ...c, config: undefined }
|
|
591
|
+
let at = c
|
|
592
|
+
// A held list is trusted for what it holds, never for what it lacks: a
|
|
593
|
+
// word it lacks is asked of its source again at once, and a line the
|
|
594
|
+
// schema it holds refuses (an argument added since) is read against the
|
|
595
|
+
// source's list once more before it is refused (`again`).
|
|
596
|
+
let lookup = async (list: (fresh: boolean) => Promise<Command[]>) => {
|
|
597
|
+
let held = await list(false)
|
|
598
|
+
let found = commandFor(held, rest)
|
|
599
|
+
if (found) {
|
|
600
|
+
let again = async () => commandFor(await list(true), rest)
|
|
601
|
+
return { found, list: held, again }
|
|
602
|
+
}
|
|
603
|
+
let now = await list(true)
|
|
604
|
+
return { found: commandFor(now, rest), list: now, again: undefined }
|
|
605
|
+
}
|
|
606
|
+
let found = commandFor(tools, rest)
|
|
607
|
+
let again: (() => Promise<typeof found>) | undefined
|
|
608
|
+
if (!found) ({ found, again } = await lookup(fetched))
|
|
609
|
+
let page = !found
|
|
610
|
+
? nounUsage(await c.all(), rest[0], opts.globalFlags)
|
|
611
|
+
: undefined
|
|
612
|
+
if (!found && !page) {
|
|
613
|
+
// A host whose list never came is the answer: past this point a line
|
|
614
|
+
// it would have understood reads as an app's stray, and its words as
|
|
615
|
+
// that app's arguments.
|
|
616
|
+
if (why) throw new Error(lost(why))
|
|
617
|
+
let stray = config
|
|
618
|
+
? undefined
|
|
619
|
+
: await opts.stray?.(rest[0], rest.slice(1), c)
|
|
620
|
+
if (stray) found = { verb: stray, args: rest.slice(1) }
|
|
621
|
+
else if (onward && opts.more) {
|
|
622
|
+
let more = opts.more
|
|
623
|
+
let theirs: Awaited<ReturnType<typeof lookup>>
|
|
624
|
+
try {
|
|
625
|
+
// The host's list is a cache too.
|
|
626
|
+
theirs = await lookup(async (fresh) => [
|
|
627
|
+
...await more(far, { fresh }),
|
|
628
|
+
])
|
|
629
|
+
} catch (e) {
|
|
630
|
+
// Most often a typo, and the host only a second place to look: say
|
|
631
|
+
// where the commands are listed, then what asking the host needs.
|
|
632
|
+
throw new Error(
|
|
633
|
+
`${config} has nothing called ${rest[0]} (\`yak help\` lists ` +
|
|
634
|
+
`what it has), and ${host} could not be asked: ` +
|
|
635
|
+
(e instanceof Unauthorized
|
|
636
|
+
? `sign in to it with \`yak auth ${host}\``
|
|
637
|
+
: (e as Error).message),
|
|
638
|
+
)
|
|
639
|
+
}
|
|
640
|
+
;({ found, again } = theirs)
|
|
641
|
+
page = !found
|
|
642
|
+
? nounUsage([...tools, ...theirs.list], rest[0], opts.globalFlags)
|
|
643
|
+
: undefined
|
|
644
|
+
if (!found && !page) {
|
|
645
|
+
let stray = await opts.stray?.(rest[0], rest.slice(1), far)
|
|
646
|
+
if (stray) found = { verb: stray, args: rest.slice(1) }
|
|
647
|
+
}
|
|
648
|
+
if (found) at = far
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
if (!found) {
|
|
652
|
+
// A noun on its own is a question, not a mistake: `yak graph` (and
|
|
653
|
+
// `yak graph --help`, which is the same command line with the flag
|
|
654
|
+
// lifted off) asks what that word can do, and the answer is its verbs.
|
|
655
|
+
if (page) {
|
|
656
|
+
if (rest.length > 1) {
|
|
657
|
+
throw new Usage(`Unknown command: ${rest[1]}\n\n${page}`)
|
|
658
|
+
}
|
|
659
|
+
out(page)
|
|
660
|
+
return 0
|
|
661
|
+
}
|
|
662
|
+
let where = onward && opts.more
|
|
663
|
+
? `neither ${config} nor ${host} has anything`
|
|
664
|
+
: `${config ?? host} has nothing`
|
|
665
|
+
throw new Usage(
|
|
666
|
+
`${where} called ${rest[0]} — try \`${opts.name ?? 'yak'} help\``,
|
|
667
|
+
)
|
|
668
|
+
}
|
|
669
|
+
if (help) {
|
|
670
|
+
out(helpFor(found.verb, opts))
|
|
671
|
+
return 0
|
|
672
|
+
}
|
|
673
|
+
try {
|
|
674
|
+
let given = await argsFor(found.verb, found.args, c.reads).catch(
|
|
675
|
+
async (e) => {
|
|
676
|
+
let now = e instanceof Usage && await again?.()
|
|
677
|
+
if (!now) throw e
|
|
678
|
+
found = now
|
|
679
|
+
return await argsFor(now.verb, now.args, c.reads)
|
|
680
|
+
},
|
|
681
|
+
)
|
|
682
|
+
return await found.verb.run(
|
|
683
|
+
{
|
|
684
|
+
...given,
|
|
685
|
+
...Object.fromEntries(
|
|
686
|
+
(found.verb.globals ?? []).flatMap((name) =>
|
|
687
|
+
said[name] === undefined ? [] : [[name, said[name]]]
|
|
688
|
+
),
|
|
689
|
+
),
|
|
690
|
+
},
|
|
691
|
+
at,
|
|
692
|
+
)
|
|
693
|
+
} catch (e) {
|
|
694
|
+
if (!(e instanceof Usage)) throw e
|
|
695
|
+
note(
|
|
696
|
+
`${opts.name ?? 'yak'}: ${e.message}\n\n${helpFor(found.verb, opts)}`,
|
|
697
|
+
)
|
|
698
|
+
return 2
|
|
699
|
+
}
|
|
700
|
+
} catch (e) {
|
|
701
|
+
let message = (e as Error).message
|
|
702
|
+
note(
|
|
703
|
+
`${opts.name ?? 'yak'}: ${
|
|
704
|
+
e instanceof Unauthorized ? lost(message) : message
|
|
705
|
+
}`,
|
|
706
|
+
)
|
|
707
|
+
return e instanceof Usage ? 2 : 1
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/** The `help` command every program here carries: the whole usage page, or
|
|
712
|
+
* one subcommand's own. */
|
|
713
|
+
export let helpTool = (opts: Opts = {}): Command => ({
|
|
714
|
+
name: 'help',
|
|
715
|
+
description: 'every command, one line each — or one command’s own page',
|
|
716
|
+
inputSchema: {
|
|
717
|
+
type: 'object',
|
|
718
|
+
additionalProperties: false,
|
|
719
|
+
properties: { words: { type: 'array', items: { type: 'string' } } },
|
|
720
|
+
},
|
|
721
|
+
positional: ['words...'],
|
|
722
|
+
readOnly: true,
|
|
723
|
+
run: async (args, c) => {
|
|
724
|
+
let words = (args.words ?? []) as string[]
|
|
725
|
+
if (!words.length) {
|
|
726
|
+
c.out(await c.page())
|
|
727
|
+
return 0
|
|
728
|
+
}
|
|
729
|
+
let found = commandFor(await c.all(), words)
|
|
730
|
+
if (found) {
|
|
731
|
+
c.out(helpFor(found.verb, opts))
|
|
732
|
+
return 0
|
|
733
|
+
}
|
|
734
|
+
// `yak help graph` is the same question `yak graph` asks: the verbs that
|
|
735
|
+
// word holds.
|
|
736
|
+
let page = nounUsage(await c.all(), words[0], opts.globalFlags)
|
|
737
|
+
if (!page) throw new Usage(`nothing here is called ${words[0]}`)
|
|
738
|
+
c.out(page)
|
|
739
|
+
return 0
|
|
740
|
+
},
|
|
741
|
+
})
|