@rhizogrip/spore-cli 0.0.0-stage → 1.0.1
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/NOTICE +2 -0
- package/README.md +20 -2
- package/bin/spore.js +521 -0
- package/package.json +50 -4
- package/src/dispatch.js +736 -0
- package/src/doctor.js +75 -0
- package/src/here.js +105 -0
- package/src/hooks.js +89 -0
- package/src/services.js +567 -0
- package/src/setup.js +265 -0
- package/tui/README.md +49 -0
- package/tui/commands.js +22 -0
- package/tui/conversation.js +89 -0
- package/tui/diff.js +71 -0
- package/tui/here.js +86 -0
- package/tui/index.js +598 -0
- package/tui/printable.js +13 -0
- package/tui/status.js +62 -0
package/bin/spore.js
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* spore — say something to an agent in a workspace, and watch what it does.
|
|
4
|
+
* Contract: ARCHITECTURE.md, "The spore command".
|
|
5
|
+
*
|
|
6
|
+
* This is the follower and holds nothing: `send` appends to the inbox and races
|
|
7
|
+
* for the lock, the runner (native/spore-runner) holds the lock and the slot, and
|
|
8
|
+
* bin/spore-agent.js is the confined half. Ctrl-C here leaves the turn running.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { createRequire } from 'node:module'
|
|
12
|
+
import { readFileSync } from 'node:fs'
|
|
13
|
+
import { basename, resolve } from 'node:path'
|
|
14
|
+
|
|
15
|
+
import minimist from 'minimist'
|
|
16
|
+
|
|
17
|
+
import { differingDocs, fork, follow, grantOf, isWorkspace, lastEventSeq, loadEnvironment, mimeOf, readInbox, RUNNING, send, stateOf, stop, turnOfMessage, turnSummary, watch, workspaceFor } from '@rhizogrip/spore'
|
|
18
|
+
import { renderHeader } from '../src/dispatch.js'
|
|
19
|
+
|
|
20
|
+
// Detaching is neither success nor failure: the turn is still going, and 0 would claim it
|
|
21
|
+
// answered. `spore show` says what actually happened once it has.
|
|
22
|
+
const DETACHED = 3
|
|
23
|
+
const WRAPPED = 4
|
|
24
|
+
const DEFAULT_TAIL = 50
|
|
25
|
+
|
|
26
|
+
const argv = minimist(process.argv.slice(2), {
|
|
27
|
+
string: ['_', 'definition', 'from', 'sporangium', 'at', 'model', 'plugin', 'attach', 'request-id', 'after', 'since', 'until', 'tz', 'by', 'n', 'lines'],
|
|
28
|
+
boolean: ['debug', 'help', 'version', 'unsafe', 'yes', 'replay', 'json', 'all', 'outdated', 'f'],
|
|
29
|
+
alias: { h: 'help' }
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const VERBS = `Usage:
|
|
33
|
+
spore setup the first run: this machine, your models, a first spore
|
|
34
|
+
spore define <name> say what a spore is; . for here
|
|
35
|
+
spore define <name> --model <m> --plugin <p> the same, with its model and plugins filled in
|
|
36
|
+
spore define <new> --from <name> the same, cut from one that has learned
|
|
37
|
+
spore define <name> --sporangium <s> the same, made from a sporangium — reviewer, or one of yours
|
|
38
|
+
spore define <name> --definition <f> the same, from that .cmk; given to one that exists, it revises it
|
|
39
|
+
spore edit <name> its spore.cmk in $EDITOR, checked before it replaces the one in place
|
|
40
|
+
spore send <name> <message> start a turn, or land in the one running
|
|
41
|
+
spore send <name> <message> --attach <f> with a file — repeatable; the agent reads it where it lands
|
|
42
|
+
spore send <name> <message> --request-id <id> taken once: sent again under the id, it answers what the first got
|
|
43
|
+
spore send <name> -- <message> -- ends the options, for a message that starts with -
|
|
44
|
+
spore send|attach <name> --json for a program: the turn's events, one JSON object a line
|
|
45
|
+
spore attach <name> --json --after <seq> the events after <seq>; 0 for the whole log
|
|
46
|
+
spore fork <name> --at <call> <new> [--definition <f>] [--replay] carry a turn on from one of its rounds, in a copy
|
|
47
|
+
spore attach <name> [-n <lines>] watch a turn that is already going
|
|
48
|
+
spore stop <name> SIGTERM it, mid-round
|
|
49
|
+
spore extend <name> wall 30m|cost 1.50|rounds 20 give the running turn more, up to its extend.<limit>
|
|
50
|
+
spore wrap <name> [10m] ask the running turn to wrap up, now or in a while
|
|
51
|
+
spore model [<name> [<model>]] what it runs on and may switch to; with a model, switch from the next turn
|
|
52
|
+
spore ls [<dir>] your spores and their state
|
|
53
|
+
spore tui [<name>] talk to a spore in a terminal screen; the last one opened without a name
|
|
54
|
+
spore here [<dir>] the spore that works in this directory, in place — made the first time
|
|
55
|
+
spore here --sporangium <s> --model <m> --no-git made from <s>, on <m>, without asking; outside git, nothing can be undone
|
|
56
|
+
spore show [<name>] one spore in detail
|
|
57
|
+
spore remake <name> make it again from its sporangium, keeping its files
|
|
58
|
+
spore rm <name> [--yes] remove it; --yes once it has turns
|
|
59
|
+
spore log [<name>] one line per turn, and what is waiting
|
|
60
|
+
spore sql [<name> [<query>|-]] its records, read with SQL; .tables lists the views, - the query on stdin
|
|
61
|
+
spore usage [<name>] [--days 7] what it spent per day, by origin, model, outcome, precursor
|
|
62
|
+
spore usage --since <d> [--until <d>] over those local days; --tz <zone>, --by <axis>, every spore without a name
|
|
63
|
+
spore precursor ls [<name>] what it can delegate to
|
|
64
|
+
spore upgrade <name>|--all its root docs replaced by what is installed
|
|
65
|
+
spore plugin ls plugins installed beside spore, and which are allowed
|
|
66
|
+
spore plugin allow <plugin> [k=v …] checked, then recorded in config.json; disallow undoes it
|
|
67
|
+
spore plugin show <plugin> what it gives: settings, programs, env, docs, precursors
|
|
68
|
+
spore schedule set <name> <sched> [<file>|-] install schedules/<sched>.cmk, checked first; - or none reads stdin
|
|
69
|
+
spore schedule rm <name> <sched> remove one; what it fired stays in the inbox
|
|
70
|
+
spore schedule ls [<name>] its schedules, and why any is not firing
|
|
71
|
+
spore tick fire every schedule that is due — run by a timer
|
|
72
|
+
spore job ls [<name>] its jobs: running ones with their progress, ended ones how
|
|
73
|
+
spore job stop <name> <job> ask a running job's supervisor to stop it
|
|
74
|
+
spore pause [why] stop every turn and job of your spores, and start none
|
|
75
|
+
spore unpause let turns start again; what waited goes at the next tick
|
|
76
|
+
spore service ls the services spore ships (webhook, tick, api, ssh-agent, sluice) and its plugins offer
|
|
77
|
+
spore service define <svc> write its systemd user unit, checked first
|
|
78
|
+
spore service start|stop|restart <svc> now; enable|disable: at every login
|
|
79
|
+
spore service restart --outdated every service running older code than is installed
|
|
80
|
+
spore service show <svc> its state and last lines
|
|
81
|
+
spore service log <svc> [-n <lines>] [-f] its journal; -f follows it
|
|
82
|
+
spore service rm <svc> [--yes] remove it; --yes when that stops instances running
|
|
83
|
+
spore hook ls the hooks in hooks.json, each one's spores and what it said to them this week
|
|
84
|
+
spore hook show <hook> one hook: its checks, its last deliveries per spore, and its lines in the webhook journal
|
|
85
|
+
spore config get [<key>] every setting in effect, defaults marked
|
|
86
|
+
spore config set|add|rm|path where workspaces live, and what your spores may use
|
|
87
|
+
spore config set model <provider>/<name> the model new spores run on, in your default sporangium
|
|
88
|
+
spore doctor what is wrong on this machine, and the command that fixes each`
|
|
89
|
+
|
|
90
|
+
if (!process.argv.slice(2).length) {
|
|
91
|
+
console.log(`spore — an agent in a workspace\n\n${VERBS}\n\n spore --help all of it: the flags, and why\n spore --version the installed version\n`)
|
|
92
|
+
process.exit(0)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (argv.version) {
|
|
96
|
+
console.log(createRequire(import.meta.url)('../package.json').version)
|
|
97
|
+
process.exit(0)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (argv.help) {
|
|
101
|
+
console.log(`spore — run an agent in a workspace, and see the workspaces you have
|
|
102
|
+
|
|
103
|
+
${VERBS}
|
|
104
|
+
|
|
105
|
+
name a spore in your home, or a path to one. The read verbs default to
|
|
106
|
+
the one you are standing in, except usage, which without one covers
|
|
107
|
+
every spore, and ls, which takes a directory; define does not,
|
|
108
|
+
because it makes something and has to be told what
|
|
109
|
+
message read from stdin when not given
|
|
110
|
+
-- ends the options: what follows is words, so a message may start with -
|
|
111
|
+
--definition the .cmk to give it, instead of your default
|
|
112
|
+
--from another spore to cut it from: its definition, its precursors and the
|
|
113
|
+
programs those grant. What it can do, never what it did — the
|
|
114
|
+
conversation, the trail and data/memory stay where they are
|
|
115
|
+
--json for a program: what the verb answers, as one JSON object, and the
|
|
116
|
+
exit code the same as without it
|
|
117
|
+
--unsafe skip the Landlock sandbox and the namespaces: no confinement at all
|
|
118
|
+
--debug verbose logging on stderr
|
|
119
|
+
|
|
120
|
+
A verb refuses an option it does not take, and an argument more than it takes.
|
|
121
|
+
|
|
122
|
+
Defining a spore and sending to it are separate acts. spore define makes one,
|
|
123
|
+
and given --definition again it revises one, keeping the conversation and
|
|
124
|
+
everything in data/. spore send only reaches one that already exists, so a
|
|
125
|
+
mistyped name is an error rather than a second spore.
|
|
126
|
+
|
|
127
|
+
A turn runs in the background and spore send follows it. Ctrl-C walks away and
|
|
128
|
+
leaves it running; spore attach picks it up again, replaying the last few lines.
|
|
129
|
+
spore stop ends it mid-round. spore send exits 0 when the turn answered, 1 when it
|
|
130
|
+
ended with no answer, 3 when you walked away, and 4 when it wrapped up at a soft
|
|
131
|
+
limit: what it printed then says what remains, and sending again carries on.
|
|
132
|
+
spore attach and spore fork exit the same way.
|
|
133
|
+
|
|
134
|
+
What a message means is the spore's to decide: the first one is the job, a later
|
|
135
|
+
one continues the conversation, one sent to a spore that ended on a question is
|
|
136
|
+
the answer to it, and one sent mid-turn arrives between rounds — including
|
|
137
|
+
"stop", which asks it to wrap up rather than killing it.
|
|
138
|
+
|
|
139
|
+
The workspace carries the rest. <ws>/spore.cmk is the agent's definition — its
|
|
140
|
+
identity and, when it lists tools, its tool grant; <ws>/state.sqlite holds the
|
|
141
|
+
conversation, so sending to it again continues it. The agent works in <ws>/data.
|
|
142
|
+
|
|
143
|
+
A spore runs on the model its definition names, because choosing one is
|
|
144
|
+
configuration rather than something a caller decides per turn. The key stays in
|
|
145
|
+
this process — the agent reaches the model through a socket, so what it spends is
|
|
146
|
+
counted somewhere it cannot reach.
|
|
147
|
+
|
|
148
|
+
~/.config/spore/ holds what you author: config.json says where workspaces live
|
|
149
|
+
and what your spores may use, sporangia/ holds your sporangia, sources.json
|
|
150
|
+
says what each source name means, and memory.json where memory: vectors keeps
|
|
151
|
+
its index. "home" is a directory that holds your spores, one per subdirectory
|
|
152
|
+
and nothing else, and is where a new one is made. "workspaces" lists the odd
|
|
153
|
+
spore living somewhere else. What is
|
|
154
|
+
running, what it cost and what it is called are read from the directories
|
|
155
|
+
themselves, because a file recording those is wrong as soon as something
|
|
156
|
+
crashes.
|
|
157
|
+
|
|
158
|
+
A spore is made from a sporangium: a spore's layout with nothing run yet — its
|
|
159
|
+
spore.cmk, and any precursors, schedules or starting files. Every spore is made the
|
|
160
|
+
same way: the default sporangium, then the one named with --sporangium laid over it,
|
|
161
|
+
file by file. Each sporangium is the package's, then yours in
|
|
162
|
+
~/.config/spore/sporangia/<name>/, yours winning. Your default sporangium,
|
|
163
|
+
~/.config/spore/sporangia/default/spore.cmk, is copied from the package's the first
|
|
164
|
+
time. It names no model, so the first define stops and says to set one — spore config
|
|
165
|
+
set model <provider>/<name> writes it into that file, and a sporangium naming no model
|
|
166
|
+
runs on it. What a spore runs
|
|
167
|
+
on, what it may call and what it may spend all live in a .cmk and are changed by
|
|
168
|
+
editing one. define --model and --plugin (repeatable) fill them into the spore.cmk it
|
|
169
|
+
is making, and only then: a spore that exists is changed by editing its file.
|
|
170
|
+
|
|
171
|
+
A plugin is a package named spore-plugin-<name>, installed where spore is —
|
|
172
|
+
npm i -g @rhizogrip/spore-plugin-browser. spore plugin allow makes it available on this
|
|
173
|
+
machine, and a spore uses it once its spore.cmk says plugins: [browser]. What spore
|
|
174
|
+
copies to a spore's root — reference.md and libraries.md from its sporangium,
|
|
175
|
+
plugins/<name>.md — is copied once and replaced only by spore upgrade; send and
|
|
176
|
+
show say when a copy differs from what is installed.
|
|
177
|
+
|
|
178
|
+
spore config set home ~/work/spores`)
|
|
179
|
+
process.exit(0)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const withJson = (most, ...flags) => ({ flags: [...flags, 'json'], most })
|
|
183
|
+
const ALL = Infinity
|
|
184
|
+
|
|
185
|
+
// What each verb takes: the options it reads and how many words may follow it — per
|
|
186
|
+
// subcommand for a verb that has them, `bare` being the one it runs without. A message, a
|
|
187
|
+
// query, a reason to pause and a plugin's settings take every word left.
|
|
188
|
+
const SHAPES = {
|
|
189
|
+
send: { flags: ['attach', 'request-id', 'json', 'unsafe', 'debug'], most: ALL },
|
|
190
|
+
fork: { flags: ['at', 'definition', 'replay', 'unsafe', 'debug'], most: 2 },
|
|
191
|
+
attach: { flags: ['n', 'lines', 'json', 'after'], most: 1 },
|
|
192
|
+
stop: { flags: [], most: 1 },
|
|
193
|
+
extend: withJson(3),
|
|
194
|
+
wrap: withJson(2),
|
|
195
|
+
model: withJson(2),
|
|
196
|
+
define: withJson(1, 'definition', 'from', 'sporangium', 'model', 'plugin'),
|
|
197
|
+
edit: withJson(1),
|
|
198
|
+
remake: withJson(1),
|
|
199
|
+
rm: withJson(1, 'yes'),
|
|
200
|
+
ls: withJson(1),
|
|
201
|
+
show: withJson(1),
|
|
202
|
+
log: withJson(1),
|
|
203
|
+
sql: withJson(ALL),
|
|
204
|
+
precursor: { bare: 'ls', subs: { ls: withJson(1) } },
|
|
205
|
+
upgrade: withJson(1, 'all'),
|
|
206
|
+
plugin: { bare: 'ls', subs: { ls: withJson(0), show: withJson(1), allow: withJson(ALL), disallow: withJson(1) } },
|
|
207
|
+
schedule: { bare: 'ls', subs: { set: withJson(3), rm: withJson(2), ls: withJson(1) } },
|
|
208
|
+
job: { bare: 'ls', subs: { ls: withJson(1), stop: withJson(2) } },
|
|
209
|
+
tick: withJson(0),
|
|
210
|
+
pause: withJson(ALL),
|
|
211
|
+
unpause: withJson(0),
|
|
212
|
+
service: {
|
|
213
|
+
bare: 'ls',
|
|
214
|
+
subs: {
|
|
215
|
+
ls: withJson(0),
|
|
216
|
+
define: withJson(1),
|
|
217
|
+
start: withJson(1),
|
|
218
|
+
stop: withJson(1),
|
|
219
|
+
restart: withJson(1, 'outdated'),
|
|
220
|
+
enable: withJson(1),
|
|
221
|
+
disable: withJson(1),
|
|
222
|
+
show: withJson(1),
|
|
223
|
+
log: { flags: ['n', 'lines', 'f'], most: 1 },
|
|
224
|
+
rm: withJson(1, 'yes')
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
config: { bare: 'get', subs: { get: withJson(1), set: withJson(2), add: withJson(2), rm: withJson(2), path: withJson(0) } },
|
|
228
|
+
usage: withJson(1, 'days', 'since', 'until', 'tz', 'by'),
|
|
229
|
+
doctor: withJson(0),
|
|
230
|
+
hook: { bare: 'ls', subs: { ls: withJson(0), show: withJson(1) } },
|
|
231
|
+
tui: { flags: [], most: 1 },
|
|
232
|
+
here: { flags: ['sporangium', 'model', 'no-git'], most: 1 },
|
|
233
|
+
setup: { flags: [], most: 0 }
|
|
234
|
+
}
|
|
235
|
+
const verb = argv._[0]
|
|
236
|
+
|
|
237
|
+
const distance = (a, b) => {
|
|
238
|
+
let row = Array.from({ length: b.length + 1 }, (_, j) => j)
|
|
239
|
+
for (let i = 1; i <= a.length; i++) {
|
|
240
|
+
const next = [i]
|
|
241
|
+
for (let j = 1; j <= b.length; j++) next[j] = Math.min(row[j] + 1, next[j - 1] + 1, row[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1))
|
|
242
|
+
row = next
|
|
243
|
+
}
|
|
244
|
+
return row[b.length]
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (!Object.hasOwn(SHAPES, verb)) {
|
|
248
|
+
const verbs = Object.keys(SHAPES)
|
|
249
|
+
const near = verb ? verbs.map(known => ({ known, d: distance(verb, known) })).filter(v => v.d <= 2).sort((a, b) => a.d - b.d)[0] : null
|
|
250
|
+
const looksLikeAPrompt = !near && (argv._.length || !process.stdin.isTTY)
|
|
251
|
+
console.error(`spore: ${verb ? `unknown command "${verb}"` : 'no command'}${near ? ` — did you mean ${near.known}?` : ` — one of ${verbs.join(', ')}`}` +
|
|
252
|
+
(looksLikeAPrompt ? '\n a turn is `spore send <name> <message>`' : '') +
|
|
253
|
+
'\n spore alone lists what each one does')
|
|
254
|
+
process.exit(2)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The options on the command line as typed, before `--`: minimist sets every declared boolean
|
|
259
|
+
* whether it was given or not, and an alias under both names.
|
|
260
|
+
*/
|
|
261
|
+
function typed (args) {
|
|
262
|
+
const end = args.indexOf('--')
|
|
263
|
+
return (end < 0 ? args : args.slice(0, end)).filter(arg => arg.startsWith('-') && arg !== '-').flatMap(arg => {
|
|
264
|
+
if (arg.startsWith('--')) return [arg.slice(2).split('=')[0]]
|
|
265
|
+
const letters = arg.slice(1).match(/^[A-Za-z]+/)
|
|
266
|
+
return letters ? [...letters[0]] : [arg.slice(1)]
|
|
267
|
+
})
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const dashed = (name) => (name.length === 1 ? `-${name}` : `--${name}`)
|
|
271
|
+
const given = typed(process.argv.slice(2))
|
|
272
|
+
|
|
273
|
+
if (argv.attach !== undefined && verb !== 'send') fail(`--attach is for send — a file goes with a message, and ${verb} sends none`)
|
|
274
|
+
|
|
275
|
+
const { subs, bare } = SHAPES[verb]
|
|
276
|
+
const sub = argv._[1] ?? bare
|
|
277
|
+
const shape = !subs ? SHAPES[verb] : Object.hasOwn(subs, sub) ? subs[sub] : null
|
|
278
|
+
if (shape) {
|
|
279
|
+
const label = subs ? `${verb} ${sub}` : verb
|
|
280
|
+
const unknown = given.filter(name => !shape.flags.includes(name))
|
|
281
|
+
if (unknown.length) {
|
|
282
|
+
fail(`unknown option ${unknown.map(dashed).join(', ')} — ${label} takes ${shape.flags.length ? shape.flags.map(dashed).join(', ') : 'no option'}`)
|
|
283
|
+
}
|
|
284
|
+
const extra = argv._.slice(subs ? 2 : 1).slice(shape.most)
|
|
285
|
+
if (extra.length) {
|
|
286
|
+
const most = shape.most === 0 ? 'no argument' : shape.most === 1 ? 'at most one argument' : `at most ${shape.most} arguments`
|
|
287
|
+
fail(`${label} takes ${most} — ${extra.map(word => `"${word}"`).join(', ')} ${extra.length > 1 ? 'are' : 'is one'} too many`)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
try {
|
|
292
|
+
loadEnvironment()
|
|
293
|
+
} catch (err) {
|
|
294
|
+
if (verb !== 'doctor') fail(`${err.message} — ${err.detail}`)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (verb === 'tui') {
|
|
298
|
+
const { mostRecentSpore, rememberedSpore, runTui } = await screen('../tui/index.js')
|
|
299
|
+
const remembered = rememberedSpore()
|
|
300
|
+
const ws = argv._[1] ? spore(argv._[1]) : remembered && isWorkspace(remembered) ? remembered : mostRecentSpore()
|
|
301
|
+
if (!ws) fail('no spore yet — spore define <name>')
|
|
302
|
+
process.exit(await runTui({ ws }))
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
if (verb === 'setup') {
|
|
306
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
307
|
+
fail('setup asks questions, and this is not a terminal — the same steps are spore config set home <dir>, ' +
|
|
308
|
+
'mo onboard <provider>, spore config set model <provider>/<name>, spore config set spendPerDay <dollars>, spore doctor')
|
|
309
|
+
}
|
|
310
|
+
const { setup } = await import('../src/setup.js')
|
|
311
|
+
try {
|
|
312
|
+
process.exit(await setup())
|
|
313
|
+
} catch (err) {
|
|
314
|
+
if (!err.type) throw err
|
|
315
|
+
fail(err.detail ? `${err.message} — ${err.detail}` : err.message)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
if (verb === 'here') {
|
|
320
|
+
const { here } = await screen('../tui/here.js')
|
|
321
|
+
try {
|
|
322
|
+
process.exit(await here({ dir: argv._[1], sporangium: argv.sporangium, model: argv.model, git: argv.git !== false }))
|
|
323
|
+
} catch (err) {
|
|
324
|
+
if (!err.type) throw err
|
|
325
|
+
fail(err.detail ? `${err.message} — ${err.detail}` : err.message)
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (!['send', 'fork', 'attach', 'stop'].includes(verb)) {
|
|
330
|
+
const { manage } = await import('../src/dispatch.js')
|
|
331
|
+
process.exit(await manage(verb, argv))
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// An answer given at a soft limit says what remains and is not the finished one, so a caller
|
|
335
|
+
// that takes stdout as the result is told apart from a turn that answered.
|
|
336
|
+
function exitOf (turn) {
|
|
337
|
+
if (!turn?.concluded) return 1
|
|
338
|
+
if (!turn.stopReason?.startsWith('soft:')) return 0
|
|
339
|
+
process.stderr.write(`\nspore: it wrapped up at its ${turn.stopReason.slice(5)} limit — the answer says what remains; send again to carry on\n`)
|
|
340
|
+
return WRAPPED
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const exitFor = (ws) => exitOf(turnSummary(ws).last)
|
|
344
|
+
|
|
345
|
+
/** The finished turn that ended the message at `inbox`, and whether it read it; null while none has. */
|
|
346
|
+
function answeredBy (ws, inbox) {
|
|
347
|
+
const [entry] = readInbox(ws, { after: inbox - 1, upto: inbox })
|
|
348
|
+
return entry ? turnOfMessage(ws, entry) : null
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const line = (object) => process.stdout.write(JSON.stringify(object) + '\n')
|
|
352
|
+
|
|
353
|
+
/** The event log as JSON lines, from `after` until the running turn ends; true when Ctrl-C cut it. */
|
|
354
|
+
async function events (ws, after) {
|
|
355
|
+
const leaving = new AbortController()
|
|
356
|
+
process.on('SIGINT', () => leaving.abort())
|
|
357
|
+
for await (const event of watch(ws, { after, signal: leaving.signal })) line(event)
|
|
358
|
+
return leaving.signal.aborted
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** The terminal screen, which an install made with --omit=optional leaves out. */
|
|
362
|
+
async function screen (path) {
|
|
363
|
+
try {
|
|
364
|
+
return await import(path)
|
|
365
|
+
} catch (err) {
|
|
366
|
+
if (err.code === 'ERR_MODULE_NOT_FOUND' && err.message.includes('@earendil-works/pi-tui')) {
|
|
367
|
+
fail(`${verb} opens the terminal screen, which this install left out — npm install -g @rhizogrip/spore-cli without --omit=optional adds it`)
|
|
368
|
+
}
|
|
369
|
+
throw err
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function fail (problem) {
|
|
374
|
+
console.error(`spore: ${problem}`)
|
|
375
|
+
process.exit(2)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async function readStdin () {
|
|
379
|
+
if (process.stdin.isTTY) return ''
|
|
380
|
+
const chunks = []
|
|
381
|
+
for await (const chunk of process.stdin) chunks.push(chunk)
|
|
382
|
+
return Buffer.concat(chunks).toString('utf8')
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const [, asked, ...rest] = argv._
|
|
386
|
+
|
|
387
|
+
function spore (name, { needed = true } = {}) {
|
|
388
|
+
let target
|
|
389
|
+
try {
|
|
390
|
+
target = workspaceFor(name)
|
|
391
|
+
} catch (err) {
|
|
392
|
+
fail(`${err.message} — ${err.detail}`)
|
|
393
|
+
}
|
|
394
|
+
if (needed && !isWorkspace(target.path)) {
|
|
395
|
+
fail(target.named
|
|
396
|
+
? `no spore called "${name}" in ${target.home} — spore define ${name}`
|
|
397
|
+
: `${target.path} is not a workspace — spore define ${name}`)
|
|
398
|
+
}
|
|
399
|
+
return target.path
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (verb === 'stop') {
|
|
403
|
+
if (!asked) fail('stop needs a spore — spore stop <name>')
|
|
404
|
+
const ws = spore(asked)
|
|
405
|
+
let result
|
|
406
|
+
try {
|
|
407
|
+
result = stop(ws)
|
|
408
|
+
} catch (err) {
|
|
409
|
+
fail(`could not stop ${ws} — ${err.message}`)
|
|
410
|
+
}
|
|
411
|
+
if (!result.stopped) {
|
|
412
|
+
console.error(`spore: nothing running in ${ws}`)
|
|
413
|
+
process.exit(1)
|
|
414
|
+
}
|
|
415
|
+
console.error(`spore: stopping ${ws} (pid ${result.pid})`)
|
|
416
|
+
process.exit(0)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (verb === 'fork') {
|
|
420
|
+
const [into] = rest
|
|
421
|
+
if (!asked || !argv.at || !into) fail('fork needs a spore, a call and somewhere new — spore fork <name> --at <call> <new> [--definition <f>] [--replay]')
|
|
422
|
+
let made
|
|
423
|
+
try {
|
|
424
|
+
made = fork({ run: spore(asked), at: argv.at, into: workspaceFor(into).path, definition: argv.definition, replay: argv.replay, unsafe: argv.unsafe, debug: argv.debug })
|
|
425
|
+
} catch (err) {
|
|
426
|
+
fail(err.detail ? `${err.message} — ${err.detail}` : err.message)
|
|
427
|
+
}
|
|
428
|
+
console.error(`spore: forked ${made.from} at ${made.at}, round ${made.rounds} — carrying it on in ${made.ws}`)
|
|
429
|
+
renderHeader(made.ws, grantOf(made.ws))
|
|
430
|
+
const ended = await follow(made.ws, { lines: 0 })
|
|
431
|
+
if (ended === 'detached') {
|
|
432
|
+
process.stderr.write(`\nspore: still running — spore attach ${made.ws}\n`)
|
|
433
|
+
process.exit(DETACHED)
|
|
434
|
+
}
|
|
435
|
+
process.exit(exitFor(made.ws))
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (verb === 'attach') {
|
|
439
|
+
if (!asked) fail('attach needs a spore — spore attach <name>')
|
|
440
|
+
const ws = spore(asked)
|
|
441
|
+
if (argv.json) {
|
|
442
|
+
if (given.includes('n') || given.includes('lines')) fail('-n is for a person watching — --json follows the event log, from --after <seq>')
|
|
443
|
+
const after = argv.after === undefined ? lastEventSeq(ws) : Number(argv.after)
|
|
444
|
+
if (!Number.isInteger(after) || after < 0) fail(`--after ${argv.after} is not an event's seq — a whole number, 0 for the whole log`)
|
|
445
|
+
process.exit(await events(ws, after) ? DETACHED : exitFor(ws))
|
|
446
|
+
}
|
|
447
|
+
if (argv.after !== undefined) fail('--after is for --json — spore attach <name> --json --after <seq>')
|
|
448
|
+
const tail = argv.n ?? argv.lines
|
|
449
|
+
if (tail !== undefined && !/^\d+$/.test(tail)) fail(`-n ${tail} is not a number of lines`)
|
|
450
|
+
const ended = await follow(ws, { lines: tail === undefined ? DEFAULT_TAIL : Number(tail) })
|
|
451
|
+
process.exit(ended === 'detached' ? DETACHED : exitFor(ws))
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const ws = spore(asked || fail('send needs a spore and a message — spore send <name> <message>, or . for the one here'))
|
|
455
|
+
|
|
456
|
+
const attached = (file) => {
|
|
457
|
+
const path = resolve(String(file))
|
|
458
|
+
let data
|
|
459
|
+
try {
|
|
460
|
+
data = readFileSync(path)
|
|
461
|
+
} catch (err) {
|
|
462
|
+
fail(`--attach ${file} cannot be read — ${err.message}`)
|
|
463
|
+
}
|
|
464
|
+
return { name: basename(path), mime: mimeOf(path), data }
|
|
465
|
+
}
|
|
466
|
+
const attach = [].concat(argv.attach ?? []).map(attached)
|
|
467
|
+
const message = (rest.join(' ') || await readStdin()).trim()
|
|
468
|
+
if (!message && !attach.length) fail('no message — pass one as an argument or on stdin, or a file with --attach')
|
|
469
|
+
|
|
470
|
+
let differs
|
|
471
|
+
try {
|
|
472
|
+
differs = differingDocs(ws)
|
|
473
|
+
} catch (err) {
|
|
474
|
+
fail(err.detail ? `${err.message} — ${err.detail}` : err.message)
|
|
475
|
+
}
|
|
476
|
+
if (differs.length) console.error(`spore: ${differs.map(d => d.file).join(', ')} differ from what is installed — spore upgrade ${asked}`)
|
|
477
|
+
|
|
478
|
+
const requestId = argv['request-id'] || undefined
|
|
479
|
+
const after = lastEventSeq(ws)
|
|
480
|
+
|
|
481
|
+
let grant
|
|
482
|
+
let sent
|
|
483
|
+
try {
|
|
484
|
+
grant = grantOf(ws)
|
|
485
|
+
sent = send(ws, message, { attach, requestId, replyTo: 'terminal', unsafe: argv.unsafe, debug: argv.debug })
|
|
486
|
+
} catch (err) {
|
|
487
|
+
fail(err.detail ? `${err.message} — ${err.detail}` : err.message)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// Sent before under this request id and already ended: nothing runs, and what it got is on
|
|
491
|
+
// the record.
|
|
492
|
+
const settled = sent.repeated && !sent.started && stateOf(ws).state !== RUNNING
|
|
493
|
+
|
|
494
|
+
if (argv.unsafe && !sent.started && !settled) {
|
|
495
|
+
console.error('spore: --unsafe took no effect — the message went to the turn already running, which keeps the confinement it was started with')
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (argv.json) {
|
|
499
|
+
line({ event: 'sent', inbox: sent.inbox, started: sent.started, repeated: sent.repeated })
|
|
500
|
+
if (!settled && await events(ws, after)) process.exit(DETACHED)
|
|
501
|
+
const found = answeredBy(ws, sent.inbox)
|
|
502
|
+
line({ event: 'result', read: found?.read ?? false, turn: found?.turn ?? null })
|
|
503
|
+
process.exit(exitOf(found?.read ? found.turn : null))
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (settled) {
|
|
507
|
+
const found = answeredBy(ws, sent.inbox)
|
|
508
|
+
if (found?.read && found.turn.output != null) process.stdout.write(found.turn.output + '\n')
|
|
509
|
+
process.stderr.write(`spore: already sent under ${requestId} — ${found?.read ? `turn ${found.turn.turnNo} answered it` : 'no turn read it'}\n`)
|
|
510
|
+
process.exit(exitOf(found?.read ? found.turn : null))
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (sent.started) renderHeader(ws, grant)
|
|
514
|
+
|
|
515
|
+
const ended = await follow(ws, { lines: 0 })
|
|
516
|
+
if (ended === 'detached') {
|
|
517
|
+
process.stderr.write(`\nspore: still running — spore attach ${asked}\n`)
|
|
518
|
+
process.exit(DETACHED)
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
process.exit(exitFor(ws))
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rhizogrip/spore-cli",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Christophe Le Bars",
|
|
6
|
+
"email": "clb@toort.net"
|
|
7
|
+
},
|
|
8
|
+
"description": "The spore command: define, send, watch and manage spores from a terminal.",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/RHIZOGRIP/spore.git",
|
|
13
|
+
"directory": "packages/spore-cli"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/RHIZOGRIP/spore#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/RHIZOGRIP/spore/issues"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=24"
|
|
21
|
+
},
|
|
22
|
+
"os": [
|
|
23
|
+
"linux"
|
|
24
|
+
],
|
|
25
|
+
"cpu": [
|
|
26
|
+
"x64"
|
|
27
|
+
],
|
|
28
|
+
"bin": {
|
|
29
|
+
"spore": "bin/spore.js"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"bin/",
|
|
33
|
+
"src/",
|
|
34
|
+
"tui/",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE",
|
|
37
|
+
"NOTICE"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"tui": "node bin/spore.js tui"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@rhizogrip/spore": "1.0.1",
|
|
45
|
+
"chalk": "^6.0.1",
|
|
46
|
+
"minimist": "^1.2.8"
|
|
47
|
+
},
|
|
48
|
+
"optionalDependencies": {
|
|
49
|
+
"@earendil-works/pi-tui": "1.0.4"
|
|
50
|
+
},
|
|
51
|
+
"license": "Apache-2.0"
|
|
52
|
+
}
|