@rhizogrip/spore-cli 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/spore.js CHANGED
@@ -88,7 +88,7 @@ const VERBS = `Usage:
88
88
  spore doctor what is wrong on this machine, and the command that fixes each`
89
89
 
90
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`)
91
+ console.log(`spore — AI agents you run on your own machines\n\n${VERBS}\n\n spore --help all of it: the flags, and why\n spore --version the installed version\n`)
92
92
  process.exit(0)
93
93
  }
94
94
 
@@ -98,84 +98,28 @@ if (argv.version) {
98
98
  }
99
99
 
100
100
  if (argv.help) {
101
- console.log(`spore — run an agent in a workspace, and see the workspaces you have
101
+ console.log(`spore — AI agents you run on your own machines
102
102
 
103
103
  ${VERBS}
104
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
105
+ name a spore in your home, or a path to one; the read verbs default to the
106
+ one you are standing in
109
107
  message read from stdin when not given
110
- -- ends the options: what follows is words, so a message may start with -
108
+ -- ends the options, for a message that starts with -
111
109
  --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
110
+ --from the spore to cut it from: what it can do, never what it did
111
+ --json for a program: the answer as JSON, with the same exit code
112
+ --unsafe no Landlock sandbox and no namespaces: no confinement at all
118
113
  --debug verbose logging on stderr
119
114
 
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`)
115
+ send, attach and fork exit 0 when the turn answered, 1 when it ended with no answer,
116
+ 3 when you walked away — Ctrl-C leaves the turn running, spore attach picks it up —
117
+ and 4 when it wrapped up at a soft limit, its answer saying what remains.
118
+
119
+ ~/.config/spore what you write: config.json, sporangia/, sources.json, environment
120
+ home what spore keeps: one folder per spore; spore setup offers ~/.spore
121
+
122
+ Everything else: https://github.com/RHIZOGRIP/spore/blob/main/packages/spore/README.md`)
179
123
  process.exit(0)
180
124
  }
181
125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhizogrip/spore-cli",
3
- "version": "1.0.2",
3
+ "version": "1.1.0",
4
4
  "author": {
5
5
  "name": "Christophe Le Bars",
6
6
  "email": "clb@toort.net"
@@ -50,7 +50,7 @@
50
50
  "tui": "node bin/spore.js tui"
51
51
  },
52
52
  "dependencies": {
53
- "@rhizogrip/spore": "1.0.2",
53
+ "@rhizogrip/spore": "1.1.0",
54
54
  "chalk": "^6.0.1",
55
55
  "minimist": "^1.2.8"
56
56
  },
package/src/dispatch.js CHANGED
@@ -69,7 +69,8 @@ function renderList (result) {
69
69
  const outside = result.workspaces.filter(row => row.from !== result.home)
70
70
 
71
71
  if (!result.workspaces.length) {
72
- if (result.home) console.log(`no spores yet — spore define <name> makes one in ${result.home}`)
72
+ if (result.asked) console.log(`no spores in ${result.home} — spore ls alone lists your home, spore show <name> shows one spore`)
73
+ else if (result.home) console.log(`no spores yet — spore define <name> makes one in ${result.home}`)
73
74
  else console.log('nowhere configured to look — spore config set home <dir>')
74
75
  return
75
76
  }
package/src/setup.js CHANGED
@@ -124,7 +124,7 @@ export async function setup ({
124
124
  if (home) {
125
125
  line('home', home)
126
126
  } else {
127
- const dir = await prompt('where should your spores live?', '~/spores')
127
+ const dir = await prompt('where should your spores live?', '~/.spore')
128
128
  setKey('home', dir)
129
129
  line('home', `${readConfig().home} — spore config set home <dir> moves where new ones are made`)
130
130
  }
package/tui/README.md CHANGED
@@ -12,11 +12,13 @@ alt+enter a new line
12
12
  ↑ ↓ earlier messages
13
13
  ctrl+p open another spore
14
14
  ctrl+o tool calls in full — an edit or a write as its diff
15
+ alt+c copy the last code block of the last answer, exactly as written
15
16
  ctrl+g write the message in $EDITOR
16
17
  esc esc stop the running turn; when none runs, clear the line
17
18
  ctrl+c clear the line
18
19
  ctrl+d leave, on an empty line
19
20
  / commands: /stop /wrap /extend /model /curate /pause /unpause /spores /status /copy /help /quit
21
+ /copy [<n>] the last answer, or its code block <n> — each block's opening fence shows its number
20
22
  //text a message that starts with /
21
23
  ```
22
24
 
@@ -40,10 +42,3 @@ time from a sporangium you pick. It adds two commands:
40
42
  screen, with an application's own slash commands added and its own way of drawing a tool call;
41
43
  [INTEGRATION.md](../../../INTEGRATION.md#a-terminal-for-your-application) has its options. It is
42
44
  not a declared entry point yet, so the path may change in a release.
43
-
44
- ## What it is not
45
-
46
- It is an **application against the package**, not part of the runtime. Everything it does
47
- goes through `@rhizogrip/spore`'s exports, and it knows nothing the library does not export.
48
- If it ever needs something it cannot get that way, the gap is in the library rather than
49
- here.
package/tui/answer.js ADDED
@@ -0,0 +1,48 @@
1
+ import { Markdown } from '@earendil-works/pi-tui'
2
+
3
+ /**
4
+ * An answer as pi-tui draws it, keeping the code blocks it drew: `blocks` is each one's text
5
+ * exactly as written, in the order shown. Labelled, each opening fence says how to copy it.
6
+ */
7
+ export class Answer extends Markdown {
8
+ constructor (text, theme, { labelled = false } = {}) {
9
+ super(text, 0, 0, theme)
10
+ this.theme = {
11
+ ...theme,
12
+ codeBlockBorder: (fence) => this.fence(fence),
13
+ highlightCode: (code, lang) => this.code(code, lang)
14
+ }
15
+ this.base = theme
16
+ this.labelled = labelled
17
+ this.blocks = []
18
+ this.inBlock = false
19
+ }
20
+
21
+ label (on) {
22
+ if (on === this.labelled) return
23
+ this.labelled = on
24
+ this.invalidate()
25
+ }
26
+
27
+ render (width) {
28
+ if (this.cachedText !== this.text || this.cachedWidth !== width) {
29
+ this.blocks = []
30
+ this.inBlock = false
31
+ }
32
+ return super.render(width)
33
+ }
34
+
35
+ fence (fence) {
36
+ if (this.inBlock) {
37
+ this.inBlock = false
38
+ return this.base.codeBlockBorder(fence)
39
+ }
40
+ this.inBlock = true
41
+ return this.base.codeBlockBorder(this.labelled ? `${fence} · /copy ${this.blocks.length + 1}` : fence)
42
+ }
43
+
44
+ code (text, lang) {
45
+ this.blocks.push({ lang: lang ?? '', text })
46
+ return text.split('\n').map(line => this.base.codeBlock(line))
47
+ }
48
+ }
package/tui/commands.js CHANGED
@@ -8,7 +8,7 @@ export const COMMANDS = [
8
8
  { name: 'unpause', description: 'let turns start again' },
9
9
  { name: 'spores', description: 'open another spore (Ctrl+P)' },
10
10
  { name: 'status', description: 'jobs, schedules, held deliveries, answers waiting to be delivered' },
11
- { name: 'copy', description: 'copy the last answer' },
11
+ { name: 'copy', description: 'copy the last answer, or code block <n> of it as numbered on screen (Alt+C: the last one)', argumentHint: '[<n>]' },
12
12
  { name: 'help', description: 'keys and commands' },
13
13
  { name: 'quit', description: 'leave (Ctrl+D)' }
14
14
  ]
package/tui/index.js CHANGED
@@ -3,7 +3,7 @@ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync
3
3
  import { tmpdir } from 'node:os'
4
4
  import { basename, dirname, join } from 'node:path'
5
5
 
6
- import { CombinedAutocompleteProvider, Editor, Input, isKeyRelease, Markdown, matchesKey, ProcessTerminal, ScrollView, SelectList, truncateToWidth, TuiAltScreen, visibleWidth, VStack, wrapTextWithAnsi } from '@earendil-works/pi-tui'
6
+ import { CombinedAutocompleteProvider, Input, isKeyRelease, Markdown, matchesKey, ProcessTerminal, ScrollView, SelectList, truncateToWidth, TuiAltScreen, visibleWidth, VStack, wrapTextWithAnsi } from '@earendil-works/pi-tui'
7
7
  import { changeText, curate, durationText, extendTurn, grantOf, list, loadMohdelEnvironment, outbox, pause, paused, readEvents, readInbox, readTurns, RUNNING, send, SERVICE_STATE_DIR, show, stateOf, stop, switchModel, unpause, upstream, watchAll, wrapTurn } from '@rhizogrip/spore'
8
8
  import chalk from 'chalk'
9
9
 
@@ -12,6 +12,8 @@ import { COMMANDS, parseCommand } from './commands.js'
12
12
  import { applyEvent, createLive, history } from './conversation.js'
13
13
  import { changeOf, counts, hunks } from './diff.js'
14
14
  import { printable } from './printable.js'
15
+ import { PromptEditor } from './prompt.js'
16
+ import { Answer } from './answer.js'
15
17
  import { matching, priceText, statusParts } from './status.js'
16
18
 
17
19
  const REFRESH_MS = 1000
@@ -98,23 +100,37 @@ class Conversation {
98
100
  }
99
101
 
100
102
  markdown (item) {
101
- if (!this.answers.has(item)) this.answers.set(item, new Markdown(item.text, 0, 0, markdownTheme))
103
+ if (!this.answers.has(item)) this.answers.set(item, new Answer(item.text, markdownTheme))
102
104
  return this.answers.get(item)
103
105
  }
104
106
 
107
+ lastAnswer () {
108
+ return [...this.items].reverse().find(item => item.kind === 'answer')
109
+ }
110
+
111
+ /** The code blocks of the last answer, numbered on screen from 1. */
112
+ lastBlocks () {
113
+ const last = this.lastAnswer()
114
+ return last ? this.markdown(last).blocks : []
115
+ }
116
+
105
117
  render (width) {
106
118
  const out = []
107
119
  if (!this.items.length && !this.live) out.push(dim('nothing said yet — write below, / for commands'))
120
+ const last = this.lastAnswer()
108
121
  for (const item of this.items) {
109
122
  if (item.kind === 'said') {
110
- const from = item.origin === 'terminal' ? chalk.cyan('›') : chalk.cyan(`${item.origin} ›`)
111
- out.push('', ...lines(`${from} ${item.text}${item.waiting && !this.live ? dim(' (not read yet)') : ''}`, width))
123
+ const from = item.origin === 'terminal' ? chalk.cyan('>') : chalk.cyan(`${item.origin} >`)
124
+ out.push('', ...lines(`${from} ${chalk.bold(item.text)}${item.waiting && !this.live ? dim(' (not read yet)') : ''}`, width))
112
125
  } else if (item.kind === 'answer') {
113
- out.push('', ...this.markdown(item).render(width))
126
+ const answer = this.markdown(item)
127
+ answer.label(item === last)
128
+ out.push('', ...answer.render(width))
114
129
  } else if (item.kind === 'end') {
115
130
  const how = [item.stopReason, item.error].filter(Boolean).join(' · ')
116
131
  const took = item.durationMs === null ? '' : ` · ${durationText(item.durationMs)}`
117
- out.push(dim(truncateToWidth(`── turn ${item.turnNo} · ${how} · ${item.rounds ?? 0} rounds · $${(item.treeUsd ?? 0).toFixed(4)}${took}`, width)))
132
+ const label = `── turn ${item.turnNo} · ${how} · ${item.rounds ?? 0} rounds · $${(item.treeUsd ?? 0).toFixed(4)}${took} `
133
+ out.push(dim(truncateToWidth(label + '─'.repeat(Math.max(0, width - visibleWidth(label))), width)))
118
134
  if (item.error && item.detail) out.push(...lines(` ${item.detail.split('\n')[0]}`, width, chalk.red))
119
135
  }
120
136
  }
@@ -175,7 +191,7 @@ class Picker {
175
191
  this.items = items
176
192
  this.onSelect = onSelect
177
193
  this.onCancel = onCancel
178
- this.filter = new Input({ prompt: '› ', placeholder: 'type to filter', placeholderStyle: dim })
194
+ this.filter = new Input({ prompt: '> ', placeholder: 'type to filter', placeholderStyle: dim })
179
195
  this.filter.focused = true
180
196
  this.list = this.listOf(items)
181
197
  }
@@ -263,7 +279,7 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
263
279
  const tui = new TuiAltScreen(terminal)
264
280
  const conversation = new Conversation({ renderTool })
265
281
  const status = new StatusLine()
266
- const editor = new Editor(tui, editorTheme)
282
+ const editor = new PromptEditor(tui, editorTheme)
267
283
  const view = new ScrollView(conversation, { follow: 'end', primary: true })
268
284
  tui.setLayoutRoot(new VStack([
269
285
  { component: view, basis: 0, grow: 1, minSize: 1 },
@@ -369,7 +385,7 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
369
385
 
370
386
  const helpPanel = () => overlay(new Panel('keys and commands', [
371
387
  'enter send · alt+enter new line · ↑↓ earlier messages · tab complete',
372
- 'ctrl+p open another spore · ctrl+o show tool calls in full · ctrl+g write in $EDITOR',
388
+ 'ctrl+p open another spore · ctrl+o show tool calls in full · ctrl+g write in $EDITOR · alt+c copy the last code block',
373
389
  'esc twice stop the running turn, or clear the line when none runs · ctrl+c clear · ctrl+d on an empty line leave',
374
390
  '',
375
391
  ...allCommands.map(c => `/${c.name}${c.argumentHint ? ` ${c.argumentHint}` : ''} — ${c.description}`)
@@ -382,6 +398,10 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
382
398
  finish(0)
383
399
  }
384
400
 
401
+ const copy = async (text, what) => {
402
+ if (await tui.copyTextToClipboard(text)) notice(`copied ${what}`)
403
+ }
404
+
385
405
  const say = (text) => {
386
406
  let sent
387
407
  try {
@@ -470,10 +490,16 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
470
490
  if (name === 'help') return helpPanel()
471
491
  if (name === 'quit') return leave()
472
492
  if (name === 'copy') {
473
- const answer = conversation.live?.complete?.output ?? [...conversation.items].reverse().find(item => item.kind === 'answer')?.text
474
- if (!answer) return notice('no answer to copy yet')
475
- terminal.write(`\x1b]52;c;${Buffer.from(answer).toString('base64')}\x07`)
476
- return notice('copied the last answer')
493
+ if (!args.length) {
494
+ const answer = conversation.live?.complete?.output ?? conversation.lastAnswer()?.text
495
+ if (!answer) return notice('no answer to copy yet')
496
+ return copy(answer, 'the last answer')
497
+ }
498
+ const blocks = conversation.lastBlocks()
499
+ const n = Number(args[0])
500
+ if (!blocks.length) return notice('the last answer has no code block')
501
+ if (!Number.isInteger(n) || n < 1 || n > blocks.length) return notice(`the last answer has ${blocks.length === 1 ? '1 code block — /copy 1' : `${blocks.length} code blocks — /copy 1 to /copy ${blocks.length}`}`)
502
+ return copy(blocks[n - 1].text, `code block ${n}`)
477
503
  }
478
504
  const added = commands.find(c => c.name === name)
479
505
  if (!added) return notice(`no /${name} — /help lists them`)
@@ -549,6 +575,12 @@ export async function runTui ({ ws, commands = [], renderTool = null, greeting =
549
575
  return { consume: true }
550
576
  }
551
577
  if (matchesKey(data, 'ctrl+p')) { picker(); return { consume: true } }
578
+ if (matchesKey(data, 'alt+c')) {
579
+ const block = conversation.lastBlocks().at(-1)
580
+ if (block) copy(block.text, 'the last code block')
581
+ else notice('the last answer has no code block')
582
+ return { consume: true }
583
+ }
552
584
  if (matchesKey(data, 'ctrl+o')) { conversation.expanded = !conversation.expanded; tui.requestRender(); return { consume: true } }
553
585
  if (matchesKey(data, 'ctrl+g')) { compose(); return { consume: true } }
554
586
  if (matchesKey(data, 'escape') && !editor.isShowingAutocomplete()) {
package/tui/prompt.js ADDED
@@ -0,0 +1,20 @@
1
+ import { Editor } from '@earendil-works/pi-tui'
2
+
3
+ export const PROMPT = '> '
4
+
5
+ export class PromptEditor extends Editor {
6
+ render (width) {
7
+ const lines = super.render(width - PROMPT.length)
8
+ const bottom = this.renderedVisibleLineCount + 1
9
+ const indent = ' '.repeat(PROMPT.length)
10
+ return lines.map((line, i) => {
11
+ if (i === 0 || i === bottom) return this.borderColor('─'.repeat(PROMPT.length)) + line
12
+ if (i === 1 && this.scrollOffset === 0) return PROMPT + line
13
+ return indent + line
14
+ })
15
+ }
16
+
17
+ handleMouse (event) {
18
+ return super.handleMouse({ ...event, x: Math.max(0, event.x - PROMPT.length), width: event.width - PROMPT.length })
19
+ }
20
+ }