@cmodjs/core 7.0.0 → 8.0.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/docs/commands.md +104 -70
- package/docs/dependencies.md +153 -23
- package/docs/index.md +54 -10
- package/docs/install-steps.md +20 -17
- package/docs/jobs.md +42 -17
- package/docs/mod.md +37 -30
- package/docs/options.md +2 -2
- package/docs/session.md +39 -29
- package/docs/testing.md +84 -23
- package/docs/ui.md +128 -58
- package/installer.d.ts +2 -2
- package/jobs/context.js +1 -1
- package/jobs/program.d.ts +2 -3
- package/jobs/program.d.ts.map +1 -1
- package/jobs/program.js +72 -23
- package/mod.d.ts +23 -8
- package/mod.d.ts.map +1 -1
- package/mod.js +3 -3
- package/package.json +1 -1
- package/records.d.ts +5 -3
- package/records.d.ts.map +1 -1
- package/records.js +18 -9
- package/register.d.ts.map +1 -1
- package/register.js +7 -15
- package/rpc.d.ts +7 -0
- package/rpc.d.ts.map +1 -0
- package/rpc.js +59 -0
- package/runtime/bound.d.ts +1 -0
- package/runtime/bound.d.ts.map +1 -1
- package/runtime/bound.js +10 -0
- package/runtime/bun.d.ts +20 -0
- package/runtime/bun.d.ts.map +1 -0
- package/runtime/bun.js +20 -0
- package/runtime/claude.d.ts +1 -0
- package/runtime/claude.d.ts.map +1 -1
- package/runtime/conversations.d.ts +9 -2
- package/runtime/conversations.d.ts.map +1 -1
- package/runtime/conversations.js +244 -185
- package/runtime/deadline.d.ts +4 -3
- package/runtime/deadline.d.ts.map +1 -1
- package/runtime/deadline.js +10 -4
- package/runtime/engine.d.ts +4 -4
- package/runtime/engine.d.ts.map +1 -1
- package/runtime/installer.d.ts +3 -3
- package/runtime/installer.d.ts.map +1 -1
- package/runtime/installer.js +10 -9
- package/runtime/interfaces.d.ts +108 -42
- package/runtime/interfaces.d.ts.map +1 -1
- package/runtime/interfaces.js +639 -143
- package/runtime/lifecycle.d.ts +3 -5
- package/runtime/lifecycle.d.ts.map +1 -1
- package/runtime/lifecycle.js +74 -63
- package/runtime/options.d.ts.map +1 -1
- package/runtime/options.js +2 -6
- package/runtime/parts.d.ts +64 -1
- package/runtime/parts.d.ts.map +1 -1
- package/runtime/parts.js +311 -4
- package/runtime/programs.d.ts +1 -0
- package/runtime/programs.d.ts.map +1 -1
- package/runtime/programs.js +5 -3
- package/runtime/rpc.d.ts +2 -0
- package/runtime/rpc.d.ts.map +1 -1
- package/runtime/rpc.js +30 -5
- package/runtime/sessions.d.ts +3 -3
- package/runtime/sessions.d.ts.map +1 -1
- package/runtime/sessions.js +38 -73
- package/runtime/toaster.js +1 -1
- package/runtime/ui.d.ts +19 -8
- package/runtime/ui.d.ts.map +1 -1
- package/runtime/ui.js +51 -47
- package/runtime/web.d.ts +0 -73
- package/runtime/web.d.ts.map +1 -1
- package/runtime/web.js +9 -5
- package/runtime/widgets.d.ts +4 -2
- package/runtime/widgets.d.ts.map +1 -1
- package/runtime/widgets.js +17 -16
- package/session/conversation.d.ts +6 -2
- package/session/conversation.d.ts.map +1 -1
- package/session/session.d.ts +22 -6
- package/session/session.d.ts.map +1 -1
- package/session/session.js +1 -0
- package/src/installer.ts +2 -2
- package/src/jobs/context.ts +1 -1
- package/src/jobs/program.ts +63 -23
- package/src/mod.ts +22 -11
- package/src/records.ts +21 -9
- package/src/register.ts +7 -15
- package/src/rpc.ts +67 -0
- package/src/runtime/bound.ts +10 -0
- package/src/runtime/bun.ts +40 -0
- package/src/runtime/claude.ts +1 -0
- package/src/runtime/conversations.ts +247 -182
- package/src/runtime/deadline.ts +13 -6
- package/src/runtime/engine.ts +4 -4
- package/src/runtime/installer.ts +13 -12
- package/src/runtime/interfaces.ts +753 -172
- package/src/runtime/lifecycle.ts +93 -67
- package/src/runtime/options.ts +2 -5
- package/src/runtime/parts.ts +364 -5
- package/src/runtime/programs.ts +6 -3
- package/src/runtime/rpc.ts +32 -5
- package/src/runtime/sessions.ts +39 -73
- package/src/runtime/toaster.ts +1 -1
- package/src/runtime/ui.ts +67 -55
- package/src/runtime/web.ts +10 -39
- package/src/runtime/widgets.ts +20 -18
- package/src/session/conversation.ts +7 -2
- package/src/session/session.ts +14 -6
- package/src/testing/fake-claude.ts +10 -4
- package/src/testing/fake-serve.ts +106 -6
- package/src/testing/fake-sessions.ts +6 -5
- package/src/testing.ts +149 -67
- package/src/ui/areas.ts +2 -2
- package/src/ui/elements.ts +21 -16
- package/src/ui/panes.ts +14 -7
- package/testing/fake-claude.d.ts +2 -0
- package/testing/fake-claude.d.ts.map +1 -1
- package/testing/fake-claude.js +10 -4
- package/testing/fake-serve.d.ts +32 -2
- package/testing/fake-serve.d.ts.map +1 -1
- package/testing/fake-serve.js +81 -6
- package/testing/fake-sessions.d.ts.map +1 -1
- package/testing/fake-sessions.js +6 -5
- package/testing.d.ts +21 -4
- package/testing.d.ts.map +1 -1
- package/testing.js +131 -55
- package/ui/areas.d.ts +3 -1
- package/ui/areas.d.ts.map +1 -1
- package/ui/elements.d.ts +18 -9
- package/ui/elements.d.ts.map +1 -1
- package/ui/elements.js +9 -8
- package/ui/panes.d.ts +10 -6
- package/ui/panes.d.ts.map +1 -1
- package/ui/panes.js +5 -3
- package/runtime/pages.d.ts +0 -34
- package/runtime/pages.d.ts.map +0 -1
- package/runtime/pages.js +0 -157
- package/src/runtime/pages.ts +0 -198
package/docs/session.md
CHANGED
|
@@ -8,7 +8,7 @@ import { Prompt, sessions } from '../node_modules/@cmodjs/core/mod.js'
|
|
|
8
8
|
const prompts = (await sessions.current.messages()).filter((message) => message.type === Prompt)
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
A mod
|
|
11
|
+
A mod reads and acts on the session it runs in, every other session on the computer, and every session of another device a sessions provider serves to this one. It hears only the session it runs in, as a VS Code extension hears only its own window.
|
|
12
12
|
|
|
13
13
|
## The shape of a conversation
|
|
14
14
|
|
|
@@ -25,7 +25,7 @@ Session one agent and its record
|
|
|
25
25
|
└── Message a prompt, a reply, a tool call, and the rest
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
Every part of the record answers
|
|
28
|
+
Every part of the record answers `search`, which finds its messages. Every part of the session the mod runs in also answers `on`, which hears what happens in it, and `before`, which changes or refuses what is about to happen in it.
|
|
29
29
|
|
|
30
30
|
## sessions
|
|
31
31
|
|
|
@@ -35,13 +35,12 @@ type Sessions = {
|
|
|
35
35
|
list(options?: { running?: boolean; since?: string; everyProject?: boolean }): Promise<Session[]>
|
|
36
36
|
get(id: string): Promise<Session>
|
|
37
37
|
search(text: string, matcher?: Matcher<Message>, options?: { running?: boolean; since?: string; everyProject?: boolean; before?: Message }): AsyncIterable<Message>
|
|
38
|
-
onMessage(matcher?, listener, options?): Subscription // and every on method below
|
|
39
38
|
}
|
|
40
39
|
```
|
|
41
40
|
|
|
42
41
|
- `current` is the session the mod runs in. It is there from `setup` on, and follows `/clear`, `/resume`, a fork, `/cd`, and a `cd` in a Bash or PowerShell call.
|
|
43
|
-
- `list` lists the project's sessions, the most recent first. `everyProject` lists every project's, `running` keeps the sessions still open in a Claude Code window, and `since` keeps those active since a time, written as `30m`, `6h`, `2d`, `1w`, or a date such as `2026-10-01`. It lists this computer's sessions and those of every
|
|
44
|
-
- `get` finds one session by its `id`, running or ended, on this computer or
|
|
42
|
+
- `list` lists the project's sessions, the most recent first. `everyProject` lists every project's, `running` keeps the sessions still open in a Claude Code window, and `since` keeps those active since a time, written as `30m`, `6h`, `2d`, `1w`, or a date such as `2026-10-01`. It lists this computer's sessions and those of every sessions provider a mod's program runs ([Sessions providers](#sessions-providers)).
|
|
43
|
+
- `get` finds one session by its `id`, running or ended, on this computer or through any sessions provider, and rejects with `No session <id> on this computer.` when none holds it.
|
|
45
44
|
- `search` yields the messages whose text contains `text`, any case, newest first, in the project's sessions or, with `everyProject`, every project's. `text` is at least 3 characters. It reads the index a page at a time as the loop asks for more, and `break` stops it. A matcher keeps the messages it matches, and `before` starts after a message, so a "load more" picks up where a loop broke. A tool call is searched by its name and the first 1,024 characters of each input value, an instruction file by its path and text, and a tool result is not searched.
|
|
46
45
|
|
|
47
46
|
```ts
|
|
@@ -53,13 +52,15 @@ for await (const message of sessions.search('deploy job', { type: Prompt }, { si
|
|
|
53
52
|
const more = sessions.search('deploy job', { type: Prompt }, { since: '1w', before: found.at(-1) })
|
|
54
53
|
```
|
|
55
54
|
|
|
56
|
-
-
|
|
55
|
+
- `sessions` and a listed session have no `on` and no `before` methods, because a mod hears only the session it runs in. `sessions.current` and its parts have them ([Listen](#listen-to-a-record)).
|
|
57
56
|
|
|
58
|
-
While cmod has set up a mod other than cmod, the cmod plugin indexes every conversation in the background at each session's start, so `search` waits only for what changed since. The `cmod
|
|
57
|
+
While cmod has set up a mod other than cmod, the cmod plugin indexes every conversation in the background at each session's start, so `search` waits only for what changed since. The `cmod sessions` command answers the same questions in a terminal ([commands.md](commands.md#cmod-sessions)), and a script `cmod run` runs reads and drives sessions with the same `sessions` ([commands.md](commands.md#cmod-run)).
|
|
59
58
|
|
|
60
|
-
###
|
|
59
|
+
### Sessions providers
|
|
61
60
|
|
|
62
|
-
One `cmod serve` per computer serves its sessions to every session and script on it ([commands.md](commands.md#cmod-serve)).
|
|
61
|
+
One `cmod serve` per computer serves its sessions to every session and script on it ([commands.md](commands.md#cmod-serve)). When a mod's program provides `cmod/Devices` ([dependencies.md](dependencies.md#cmod-devices-providers-on-your-other-devices)), `cmod serve` reaches the `cmod serve` of each device it names, and that `cmod serve` provides `cmod/Sessions` for its device's sessions. A mod whose program provides the `cmod/Sessions` interface ([dependencies.md](dependencies.md#interfaces-several-mods-answer-one-name)) serves the sessions of other devices the same way. So `list`, `get`, and `search` read every device's sessions, and every action reaches them, as they reach this computer's. A call on one session goes to each provider in turn, `cmod serve` first, and the first that holds the session answers it. A provider that does not hold the session throws `NotHeld` from `rpc.js`, and a call no provider holds rejects with the last provider's message, such as `No session <id> on this computer.` or `This provider holds no session <id>.`
|
|
62
|
+
|
|
63
|
+
A provider answers each session it lists with `device: { id, name }`, the computer the session runs on, in the shape `device.read()` answers ([mod.md](mod.md#device-the-computer-the-session-runs-on)). `cmod serve` names this computer, and a provider a mod's program runs names the device each of its sessions runs on. A session of a computer without a machine id has no `device`.
|
|
63
64
|
|
|
64
65
|
## Session
|
|
65
66
|
|
|
@@ -77,6 +78,11 @@ type Session = {
|
|
|
77
78
|
readonly lastAt: string
|
|
78
79
|
readonly forkedFrom?: string
|
|
79
80
|
readonly visitedAt?: string
|
|
81
|
+
readonly device?: { id: string; name: string }
|
|
82
|
+
readonly status?: 'busy' | 'shell' | 'idle' | 'waiting'
|
|
83
|
+
readonly waitingFor?: { dialog: string; tool?: string; input?: Record<string, unknown>; question?: string }
|
|
84
|
+
readonly contextPercent?: number
|
|
85
|
+
readonly lastTurn?: { reason: 'answered' | 'stopped' | 'failed'; at: string; failure?: string }
|
|
80
86
|
readonly goal: Goal
|
|
81
87
|
context(): Promise<Context>
|
|
82
88
|
contexts(): Promise<Context[]>
|
|
@@ -84,10 +90,8 @@ type Session = {
|
|
|
84
90
|
turns(options?: { last?: number }): Promise<Turn[]>
|
|
85
91
|
messages(options?: { after?: string; last?: number }): Promise<Message[]>
|
|
86
92
|
search(text: string, matcher?: Matcher<Message>): AsyncIterable<Message>
|
|
87
|
-
onMessage(matcher?, listener, options?): Subscription // and every on method below
|
|
88
|
-
beforePrompt(matcher?, listener, options?): Subscription // and every before method below
|
|
89
93
|
send(message: { type: Prompt | Note; text: string; as?: Person | Session }): Promise<void>
|
|
90
|
-
startTask(task: { prompt: string; title: string; agentType?: string; model?: string; name?: string; cwd?: string; isBackground?: boolean;
|
|
94
|
+
startTask(task: { prompt: string; title: string; agentType?: string; model?: string; name?: string; cwd?: string; isBackground?: boolean; device?: string }): Promise<Task>
|
|
91
95
|
fork(task: { prompt: string; title: string; isBackground?: boolean; at?: string }): Promise<Task>
|
|
92
96
|
callTool(call: { name: string; input: Record<string, unknown> }): Promise<ToolResult>
|
|
93
97
|
usage(): Promise<Usage>
|
|
@@ -98,7 +102,7 @@ type Session = {
|
|
|
98
102
|
}
|
|
99
103
|
```
|
|
100
104
|
|
|
101
|
-
- `id` is Claude Code's session id, and a task owner's `id` is its task's id. `title` is the name the person gave it with `/rename`, or the first line of its first prompt. A resumed or forked session with no name, and a session the mod started in after its first prompt, has an empty `sessions.current.title` until `sessions.list()` reads it.
|
|
105
|
+
- `id` is Claude Code's session id, and a task owner's `id` is its task's id. `title` is the name the person gave it with `/rename`, or the first line of its first prompt. A resumed or forked session with no name, and a session the mod started in after its first prompt, has an empty `sessions.current.title` until `sessions.list()` reads it. `sessions.current.device` is empty until `sessions.list()` reads the current session from `cmod serve`, which names this computer, and holds from then on.
|
|
102
106
|
- `color` is the color the person last picked with `/color`, such as `cyan`, and is absent when they never picked one or reset it with `/color default`. A task's owner has none. `sessions.current.color` follows each `/color` the person runs, and a resumed session's color holds from the mod's start, as `title` does.
|
|
103
107
|
- `project` is the project the session works in, and `cwd` is its working folder. For the current session both follow `/cd`, and `cwd` follows a `cd` in a Bash or PowerShell call.
|
|
104
108
|
- `agentType` is the agent definition the session runs, such as `Explore` for a task's owner, or the agent a session started with `claude --agent` runs. A plain session has none.
|
|
@@ -106,6 +110,12 @@ type Session = {
|
|
|
106
110
|
- `forkedFrom` names the session a `/branch` or a fork copied it from, and for a task's owner the session whose `fork` started it.
|
|
107
111
|
- `visitedAt` is when the person last opened the session's reply, in the browser app or in a mod that calls `visit`, as an ISO time. A reply after it is unread. A session nobody visited has none.
|
|
108
112
|
- `isRunning` is true while the session is open in a Claude Code window, and for a task's owner while its task runs.
|
|
113
|
+
- `device` is the computer the session runs on, its `id` and `name` as `device.read()` answers them ([mod.md](mod.md#device-the-computer-the-session-runs-on)). A listed session's is the one its sessions provider names ([Sessions providers](#sessions-providers)). A session of a computer without a machine id has none, and so does a task's `owner`. `sessions.get(task.id)` reads a background task's session with the device its store names.
|
|
114
|
+
- `status` is what a running session does now, as Claude Code records it: `busy` while a turn runs, `waiting` while a dialog waits on the person, `shell` while a command a stopped turn left runs in its terminal, and `idle` between turns. A session whose Claude Code records none has the one its cmod plugin hears: `busy` from a turn's start to its end, `waiting` while a permission prompt or a question is open, else `idle`. An ended session, a task's `owner`, and `sessions.current` have none.
|
|
115
|
+
- `waitingFor` is what a `waiting` session waits on. `dialog` is Claude Code's name for it, such as `permission prompt`, `input needed`, or `dialog open`. A permission prompt for a tool call adds the call's `tool` and `input`, and an `AskUserQuestion` call adds its first `question`.
|
|
116
|
+
- `contextPercent` is how full the session's context window is, in percent, from its `usage()` after each turn. A session before its first answer has none.
|
|
117
|
+
- `lastTurn` is how the session's last main turn ended, as its cmod plugin hears it: `answered`, `stopped` when the person stopped it with Esc or an app's `abort`, or `failed` with the `failure` text Claude Code gave, and `at`, the ISO time it ended. A turn stopped with Esc has no ending in its transcript until the next prompt, so only the cmod plugin's hooks hear it. A running turn, a session whose cmod plugin has heard no turn end since it started, and an ended session have none.
|
|
118
|
+
- `cmod/Sessions` tells a `Change` of `part: 'status'` whenever a session's `status`, `waitingFor`, `contextPercent`, or `lastTurn` changes.
|
|
109
119
|
|
|
110
120
|
### Read a record
|
|
111
121
|
|
|
@@ -123,7 +133,7 @@ const explorers = (await sessions.current.goal.tasks()).filter((task) => task.ow
|
|
|
123
133
|
|
|
124
134
|
A session that has ended keeps its record, so every read works on it.
|
|
125
135
|
|
|
126
|
-
`sessions.current` adds two members for the rows Claude Code draws, `scrollTo(message, options?)` and `caption(toolCall, text)` ([ui.md](ui.md#the-
|
|
136
|
+
`sessions.current` adds two members for the rows Claude Code draws, `scrollTo(message, options?)` and `caption(toolCall, text)` ([ui.md](ui.md#the-conversation-s-rows)).
|
|
127
137
|
|
|
128
138
|
### Listen to a record
|
|
129
139
|
|
|
@@ -132,19 +142,21 @@ part.on<Event>(listener: (payload, time: { deadline: number; signal: AbortSignal
|
|
|
132
142
|
part.on<Event>(matcher: Matcher<payload>, listener, options?): Subscription
|
|
133
143
|
```
|
|
134
144
|
|
|
135
|
-
Each event has its own method, such as `onReply` or `onTaskStart`, which runs `listener` for each new message, turn, context, task, goal change, or usage change, as it happens. `sessions`,
|
|
145
|
+
Each event has its own method, such as `onReply` or `onTaskStart`, which runs `listener` for each new message, turn, context, task, goal change, or usage change, as it happens. `sessions.current`, its goal, a task, a task's owner, a context, and a turn each have every `on` method, and each hears what happens inside it: an event reaches the part it happens in and every part above it.
|
|
146
|
+
|
|
147
|
+
A mod hears only the session it runs in, as a VS Code extension hears only its own window. A listed session has no `on` method, and a listener on a part of another session, such as a background task's owner or a turn read from another session, throws `<mod>: onReply works only in a mod's own session.`, as every listener of a `cmod run` script does. A background task's own `onTaskEnd` still hears it end, because the task belongs to this session's goal.
|
|
136
148
|
|
|
137
149
|
A session is one agent: the conversation's main agent, or the agent doing a task. An event runs up one chain:
|
|
138
150
|
|
|
139
151
|
```
|
|
140
|
-
Turn ──> Context ──> Session ──> that session's Task ──> each Task above ──> Goal
|
|
152
|
+
Turn ──> Context ──> Session ──> that session's Task ──> each Task above ──> Goal
|
|
141
153
|
```
|
|
142
154
|
|
|
143
155
|
The goal belongs to the main agent, so the main agent's events reach it too. So:
|
|
144
156
|
- `sessions.current.onToolCall(…)` hears the main agent's tool calls only.
|
|
145
157
|
- `sessions.current.goal.onToolCall(…)` hears every agent's in the session.
|
|
146
158
|
- `task.onMessage(…)` hears every message of the task's owner and of the tasks it handed on, and `task.owner.onMessage(…)` hears the owner's alone.
|
|
147
|
-
- `turn.onReply(…)` hears the replies of that turn only
|
|
159
|
+
- `turn.onReply(…)` hears the replies of that turn only.
|
|
148
160
|
|
|
149
161
|
A turn or a context that has ended takes no listener: its methods throw `<mod>: turn <id> has ended, so its onReply never fires. Listen on its Session.`
|
|
150
162
|
|
|
@@ -158,7 +170,7 @@ A turn or a context that has ended takes no listener: its methods throw `<mod>:
|
|
|
158
170
|
| `onGoalChange` | The `Goal` with its new text. | The goal |
|
|
159
171
|
| `onUsageChange` | The session's new `Usage`. | The session |
|
|
160
172
|
|
|
161
|
-
The
|
|
173
|
+
The events arrive as Claude Code writes them.
|
|
162
174
|
|
|
163
175
|
A matcher keeps the payloads it matches. It is a partial copy of the payload: a key it names must match, and a key it leaves out matches anything. A string matches the same string, a `RegExp` matches the text it tests true on, and a list matches any one of its items. A key the payload lacks matches nothing.
|
|
164
176
|
|
|
@@ -173,7 +185,7 @@ A listener that throws shows the error toast `The <method> listener failed`, suc
|
|
|
173
185
|
|
|
174
186
|
### Give a listener time
|
|
175
187
|
|
|
176
|
-
A listener gets 10 seconds. `timeoutMs` changes that, a whole number above 0 and at most `600000`. The second argument a listener gets is its `deadline`, the time it ends as `Date.now()` counts, and a `signal` that aborts then, or as soon as the person cancels the work the listener runs for, as Esc does during a tool call, so a slow call such as `http.fetch` can stop with it. Time a listener spends waiting on a call, such as `process.run`, never counts against the 10 seconds Claude Code gives a hook's own code, so a listener that waits can use its whole `timeoutMs
|
|
188
|
+
A listener gets 10 seconds. `timeoutMs` changes that, a whole number above 0 and at most `600000`. The second argument a listener gets is its `deadline`, the time it ends as `Date.now()` counts, and a `signal` that aborts then, or as soon as the person cancels the work the listener runs for, as Esc does during a tool call, so a slow call such as `http.fetch` can stop with it. Time a listener spends waiting on a call, such as `process.run`, never counts against the 10 seconds Claude Code gives a hook's own code, so a listener that waits can use its whole `timeoutMs`:
|
|
177
189
|
|
|
178
190
|
```ts
|
|
179
191
|
sessions.current.onTurnEnd(async (turn, { signal }) => {
|
|
@@ -183,7 +195,7 @@ sessions.current.onTurnEnd(async (turn, { signal }) => {
|
|
|
183
195
|
|
|
184
196
|
A cancel stops the listener quietly. Claude Code stops the programs its `process.run` and `process.spawn` calls started, cmod goes on without its answer, and nothing it throws shows a toast.
|
|
185
197
|
|
|
186
|
-
An `on` listener that passes its time stops quietly. A `before` listener that passes its time shows the error toast, and what was about to happen goes on as it was. With `onFailure: 'refuse'`, a `before` listener that throws or passes its time refuses instead, with the error as the reason, for a gate that must hold even when it breaks. The gate holds when Claude Code stops the mod's hook too, as it does when the listener's own code runs past 10 seconds without waiting on a call, and the toast `Claude Code stopped its <hook> hook` says so.
|
|
198
|
+
An `on` listener that passes its time stops quietly. A `before` listener that passes its time shows the error toast, and what was about to happen goes on as it was. With `onFailure: 'refuse'`, a `before` listener that throws or passes its time refuses instead, with the error as the reason, for a gate that must hold even when it breaks. The gate holds when Claude Code stops the mod's hook too, as it does when the listener's own code runs past 10 seconds without waiting on a call, and the toast `Claude Code stopped its <hook> hook` says so. A `beforePermissionRequest` listener always refuses when it fails.
|
|
187
199
|
|
|
188
200
|
### Change what happens
|
|
189
201
|
|
|
@@ -192,7 +204,7 @@ part.before<Event>(listener: (payload, time) => Answer | void, options?: { timeo
|
|
|
192
204
|
part.before<Event>(matcher: Matcher<payload>, listener, options?): Subscription
|
|
193
205
|
```
|
|
194
206
|
|
|
195
|
-
Each `before` method, such as `beforeToolCall`, runs `listener` just before something happens, and its answer changes or refuses it. Returning nothing lets it happen as it was. Each part
|
|
207
|
+
Each `before` method, such as `beforeToolCall`, runs `listener` just before something happens, and its answer changes or refuses it. Returning nothing lets it happen as it was. Each part that has `on` methods has every `before` method, and hears what is about to happen inside it, the way `on` methods do.
|
|
196
208
|
|
|
197
209
|
| Method | The listener can answer | Covers |
|
|
198
210
|
| --- | --- | --- |
|
|
@@ -204,7 +216,7 @@ Each `before` method, such as `beforeToolCall`, runs `listener` just before some
|
|
|
204
216
|
| `beforeReply`, `beforeNotice` | `{ text }` | The text the transcript keeps |
|
|
205
217
|
| `beforeToolResult` | `{ output?, isError?, note? }` | The result, after the tool ran. Here `output` is what the tool answered Claude Code, such as `{ stdout, stderr }` for Bash, and an `output` answer replaces it in the same shape, `note` adds text beside it, and `isError: true` makes Claude read it as a failure, with the note as the reason. A failed call takes only a note |
|
|
206
218
|
| `beforeToolCall` | `{ input?, note? }`, `{ result, note? }`, or `{ refuse }` | A tool call. `result` answers it without running the tool, and `note` reaches Claude with the call's result |
|
|
207
|
-
| `beforeTaskStart` | `{ prompt?, title?, owner?: { agentType?, model?, cwd? }, isAsync? }
|
|
219
|
+
| `beforeTaskStart` | `{ prompt?, title?, owner?: { agentType?, model?, cwd? }, isAsync? }` or `{ refuse }` | A task about to start. `prompt` and `title` replace what Claude wrote, `owner` picks the agent that does it, its model, and its folder, and `isAsync` runs a subagent while Claude goes on. A background task takes `prompt`, `title`, `owner`, and `refuse`, a background fork takes `prompt`, `title`, and `refuse`, and a workflow's agent takes only `refuse` |
|
|
208
220
|
| `beforeTurnEnd` | `{ note }` or `{ refuse }` | The agent about to stop. `refuse` tells it it may not stop yet, and it reads the reason as an error. `note` gives it new information, which it reads at once and keeps working. On `task.owner`, it is the task about to hand back its answer |
|
|
209
221
|
| `beforeContextEnd` | `{ summary }` or `{ refuse }` | A compaction about to start. `summary` is the text the new context starts with, which Claude reads as one prompt, in place of the summary Claude Code's model would write, so no model runs. `refuse` keeps the context, whatever another mod answered. When several mods answer `summary`, the last one asked stands |
|
|
210
222
|
|
|
@@ -257,11 +269,9 @@ sessions.current.beforeModelChange({ model: /haiku/ }, () => ({ refuse: 'This pr
|
|
|
257
269
|
sessions.current.onCwdChange(({ cwd, previous }) => mod.log(`moved from ${previous} to ${cwd}`))
|
|
258
270
|
```
|
|
259
271
|
|
|
260
|
-
A `before` method on another session asks the mod over cmod from that session. That session waits up to the listener's `timeoutMs` and 1 second more for the answer, then lets the event happen as it was, or refuses it for `onFailure: 'refuse'`, and logs why.
|
|
261
|
-
|
|
262
272
|
### Act on a session
|
|
263
273
|
|
|
264
|
-
Every action works on the current session and on every other running session, on this computer or
|
|
274
|
+
Every action works on the current session and on every other running session, on this computer or through a sessions provider. cmod carries an action to another session, and that session's cmod does it there.
|
|
265
275
|
|
|
266
276
|
- `send` adds a message to the session.
|
|
267
277
|
- A `Note` is text the agent reads on its next call to the model and the person does not see. It starts no turn. It rejects with `<mod>: Claude Code refused the note: <reason>` when a plugin refuses it.
|
|
@@ -271,7 +281,7 @@ Every action works on the current session and on every other running session, on
|
|
|
271
281
|
- To a session that has ended, a `Prompt` resumes it in a new background Claude Code session, as `claude --bg --resume` does, which answers it.
|
|
272
282
|
- `startTask` starts a task in the session, the way the Agent tool starts one, and resolves the `Task` once it started. It does not wait for the task to finish. `title` is the task's title, `agentType` names the owner's agent type, such as `Explore`, `name` is the address a teammate's `SendMessage` reaches it by, and `model` and `cwd` change what it runs on and where. It rejects with `<mod>: Claude Code refused the task: <reason>` when a hook refuses it.
|
|
273
283
|
- With `isBackground: true`, the owner is a new background Claude Code session, as `claude --bg` starts, which starts fresh on the prompt and keeps running after this session ends. It takes no `name`, and rejects with `<mod>: a background task takes no name.` for one.
|
|
274
|
-
- `
|
|
284
|
+
- `device` names the device the background session runs on, by its name or its cmod device id. A `device` other than this computer starts through the sessions provider that serves it ([Sessions providers](#sessions-providers)): cmod asks the `startTask` of each provider on that device, its `cmod serve` among them, or of every provider when none runs there, in turn, with the whole task, `device` included, and the first that does not answer not-held starts the session there and answers its task. A device's `cmod serve` starts it in the task's `cwd`, or the project of this session when the task names none, and rejects with `<device> has no folder <cwd>, so the task did not start. Give the task a cwd on <device>.` when that device lacks the folder. With no such provider, the call rejects with `<mod>: no mod starts sessions on <device>, so the task did not start. Leave device out to start it on <this computer>.`
|
|
275
285
|
- `fork` starts a subagent that keeps this session's whole conversation and does what the prompt says.
|
|
276
286
|
- With `isBackground: true`, the owner is a new background session on a copy of the conversation, which cmod writes as a transcript of its own, and which carries on with the prompt.
|
|
277
287
|
- `at` is the `id` of a message of the conversation. The copy holds the conversation from its first message through that one, and nothing after it, and needs `isBackground: true`, else the call rejects with `<mod>: a fork through a message runs as a background session. Add isBackground: true.`
|
|
@@ -344,7 +354,7 @@ type MessageOf<Type> = {
|
|
|
344
354
|
}
|
|
345
355
|
```
|
|
346
356
|
|
|
347
|
-
- `id` names the transcript row the message is in. When one row holds several messages, such as a reply with its tool calls, each `id` ends in `:<n>`. `sessions.current.scrollTo(message)` scrolls to the message's row ([ui.md](ui.md#the-
|
|
357
|
+
- `id` names the transcript row the message is in. When one row holds several messages, such as a reply with its tool calls, each `id` ends in `:<n>`. `sessions.current.scrollTo(message)` scrolls to the message's row ([ui.md](ui.md#the-conversation-s-rows)).
|
|
348
358
|
- `at` is its ISO time. `session` is the session's id, and `context` the number of the context it is in, from 1.
|
|
349
359
|
- `turn` is the `id` of the prompt that opened its turn. A task's message names its owner's turn. A message before the first prompt has none.
|
|
350
360
|
- `task` is the id of the task whose message it is. A message of the session itself has none.
|
|
@@ -456,8 +466,8 @@ sessions.current.onTurnEnd((turn) => {
|
|
|
456
466
|
- A context's `number` counts from 1. Its `closedAt` is set at the compaction that ended it.
|
|
457
467
|
- A goal's text starts as the session's first prompt, and `text()` reads it. `update` replaces it, and `onGoalChange` hears the goal, whose `text()` reads the new text.
|
|
458
468
|
- A task's `owner` is a `TaskOwner`: the session doing the work, which can't start tasks or call tools. Its goal is the task's prompt, its `agentType` and `model` are what the task runs, and its turns and messages are the task's own. Claude Code's checklist tools, `TaskCreate`, `TaskUpdate`, and `TaskList`, are not tasks: they are its todo list, which `onTodoCreate` and `onTodoComplete` hear.
|
|
459
|
-
- A task's `openedBy` is the `callId` of the Agent call that started it, and empty for a task a mod started. Its `closedAt` is set once it answered: a subagent's when it stops,
|
|
460
|
-
- A task with `isBackground` is done by a separate session, whose `id` is the task's. Its owner reads that session's own turns and messages, and `task.messages()`, `task.tasks()`, and `task.search` read them too. `task.on…` and `task.before…` hear the task's start and end
|
|
469
|
+
- A task's `openedBy` is the `callId` of the Agent call that started it, and empty for a task a mod started. Its `closedAt` is set once it answered: a subagent's when it stops, a background session's at that session's first stop, which its cmod tells this session, and a background session on another device once its sessions provider tells, with a `Change` of `part: 'running'`, that the session no longer runs.
|
|
470
|
+
- A task with `isBackground` is done by a separate session, whose `id` is the task's. Its owner reads that session's own turns and messages, and `task.messages()`, `task.tasks()`, and `task.search` read them too. `task.on…` and `task.before…` hear the task's start and end. `task.owner` takes no listener, because it is another session. Claude Code's own fork agent reads as a fork: an Agent call that forks shows an owner `forkedFrom` this session, with no `agentType`.
|
|
461
471
|
- A task of `sessions.current` that Claude Code still runs holds `status`, where its agent stands now, `name`, the name a teammate is messaged by, and `spawnedBy`, the plugin that started it, absent when Claude or the person did. A task read from another session or after its agent ended has none of these. A background task holds the `status` `claude agents` lists for its session.
|
|
462
472
|
|
|
463
473
|
## Test it
|
package/docs/testing.md
CHANGED
|
@@ -19,7 +19,7 @@ test('/todo adds the item to the project list', async () => {
|
|
|
19
19
|
})
|
|
20
20
|
```
|
|
21
21
|
|
|
22
|
-
Assert what a person or Claude sees: `tested.shown.
|
|
22
|
+
Assert what a person or Claude sees: `tested.shown.view.lines()`, `tested.lines(...)`, `tested.shown`, `tested.state`, and what `tested.raise...`, `tested.type(...)`, and `tested.callTool(...)` return.
|
|
23
23
|
|
|
24
24
|
## TestOptions
|
|
25
25
|
|
|
@@ -35,6 +35,15 @@ type TestOptions<State, Options> = {
|
|
|
35
35
|
readonly links?: Readonly<Record<string, string>>
|
|
36
36
|
readonly secrets?: Readonly<Record<string, string>>
|
|
37
37
|
readonly sessions?: readonly FakeSession[]
|
|
38
|
+
readonly providers?: readonly FakeProvider[]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type FakeProvider = {
|
|
42
|
+
readonly name: InterfaceName
|
|
43
|
+
readonly mod: string
|
|
44
|
+
readonly program?: string
|
|
45
|
+
readonly device?: { readonly id: string; readonly name: string }
|
|
46
|
+
readonly methods: InterfaceMethods<name>
|
|
38
47
|
}
|
|
39
48
|
```
|
|
40
49
|
|
|
@@ -50,15 +59,16 @@ type TestOptions<State, Options> = {
|
|
|
50
59
|
| `links` | Symbolic links in the fake file system, absolute link path to its target | no links |
|
|
51
60
|
| `secrets` | The mod's secrets `mod.secrets` starts with, key to value. The fake keeps what the mod stores in memory, and `tested.calls` never holds a value | no secrets |
|
|
52
61
|
| `sessions` | The earlier conversations `sessions.list` and `sessions.search` read, each `{ id, messages?, goal?, … }` ([Build a conversation](#build-a-conversation)) | none: only the current session, `test-session` |
|
|
62
|
+
| `providers` | The providers of interfaces other mods and programs run, which `providers(name)` and a `program` job's `provider(name)` find ([providers](#providers)) | none: only the tested mod's own `provide` |
|
|
53
63
|
|
|
54
64
|
A project plugin finds its repository through a `.git` entry at `projectRoot`, so a test of one puts it in `files`, such as `files: { '/test/project/.git': '' }`. Without it the project plugin has no repository, and its rules match calls in every folder, as a user mod's do.
|
|
55
65
|
|
|
56
|
-
The fake session's id is `test-session`, its model is `test-model`, and its home is `/test/home`. So `mod.dataFolder` is `/test/home/.local/share/cmod/data/<name>`, and `mod.folder` is `/test/plugins/<name>`. The mod is installed and draws on a terminal 80 columns wide.
|
|
66
|
+
The fake session's id is `test-session`, its model is `test-model`, and its home is `/test/home`. So `mod.dataFolder` is `/test/home/.local/share/cmod/data/<name>`, `mod.cacheFolder` is `/test/home/.cache/cmod/<name>`, and `mod.folder` is `/test/plugins/<name>`. The mod is installed and draws on a terminal 80 columns wide.
|
|
57
67
|
|
|
58
68
|
## TestedMod
|
|
59
69
|
|
|
60
70
|
```ts
|
|
61
|
-
type TestedMod<State> = {
|
|
71
|
+
type TestedMod<State, Given> = {
|
|
62
72
|
start(): Promise<void>
|
|
63
73
|
readonly raise: Raise
|
|
64
74
|
fire(event, input, below?): Promise<EventResult>
|
|
@@ -80,12 +90,13 @@ type TestedMod<State> = {
|
|
|
80
90
|
readonly calls: readonly TestCall[]
|
|
81
91
|
readonly fakes: Fakes
|
|
82
92
|
readonly shown: Shown
|
|
93
|
+
readonly providers: { [Index in keyof Given]: Provided<Given[Index]['name']> }
|
|
83
94
|
}
|
|
84
95
|
```
|
|
85
96
|
|
|
86
97
|
### start
|
|
87
98
|
|
|
88
|
-
Starts the mod: loads its state, runs `setup`, and adds its listeners, pages, panes, widgets, and jobs. `raise`, `fire`, `lines`, `type`, `hotkey`, `callTool`, `press`, `moveTo`, and `api` start the mod first, so a test calls `start` only before it reads `state` or `shown` with nothing else to run. It rejects with the reason when the mod does not start.
|
|
99
|
+
Starts the mod: loads its state, runs `setup`, and adds its listeners, views, pages, panes, widgets, and jobs. `raise`, `fire`, `lines`, `type`, `hotkey`, `callTool`, `press`, `moveTo`, and `api` start the mod first, so a test calls `start` only before it reads `state` or `shown` with nothing else to run. It rejects with the reason when the mod does not start.
|
|
89
100
|
|
|
90
101
|
### raise
|
|
91
102
|
|
|
@@ -124,10 +135,11 @@ fire(event: 'tool.check' | 'prompt.context' | 'cmod.call' | 'ui.render' | 'ui.sc
|
|
|
124
135
|
- `fire('cmod.call', { to, method, input })` calls a method `setup` returned, as another mod's `mod.dependencies` does, and returns `{ value }` with what the method returned, or `{ deny }` with the reason it failed. `tested.api.<method>(input)` does the same and resolves the value, or rejects with the reason.
|
|
125
136
|
- `ui.render`, `ui.scroll`, and `ui.close` are what `lines` and a pane's own events raise. `shown.pane.lines()` and `lines` are the way to draw.
|
|
126
137
|
- `fire('ui.close', { id, origin: { kind: 'person' } })` closes the pane as Escape or its ✕ does, and draws it once more before it closes, as Claude Code does. A pane's `ui.render` shows a pane Claude Code held back, as Claude Code does when the terminal widens.
|
|
138
|
+
- `claudeCodeOf(tested).fire('ConfigChange', { source: 'user_settings', file_path })` edits a settings file. It runs the `settings.beforeChange` listeners, and then, unless one of them or `below` refused, the `settings.onChange` listeners, as Claude Code loads the change.
|
|
127
139
|
- `claudeCodeOf(tested).fire('turn.complete', { …, isAborted: true })`, and a Stop fired with an aborted signal, are the person's Esc. From then on `claude.command.run` refuses with `the session was stopped and nobody has sent input since` until the person's next prompt, command, or press, as Claude Code holds every plugin's input after an Esc.
|
|
128
140
|
|
|
129
141
|
```ts
|
|
130
|
-
expect(await tested.fire('cmod.call', { to: '
|
|
142
|
+
expect(await tested.fire('cmod.call', { to: 'my-mod', method: 'restore', input: { path: 'a.ts' } })).toEqual({ value: { restored: true } })
|
|
131
143
|
```
|
|
132
144
|
|
|
133
145
|
```ts
|
|
@@ -188,7 +200,7 @@ The current session, `test-session`, starts with one message: the cmod plugin's
|
|
|
188
200
|
|
|
189
201
|
Each one runs the mod's session listeners, as a new message does in Claude Code. A `MessageDraft` is a message without its place: `{ type: Prompt, text, from? }`, `{ type: Note, text, from? }`, `{ type: Reply, text }`, `{ type: ToolCall, name, input?, callId? }`, `{ type: ToolResult, callId, output, isError? }`, `{ type: Skill, name, text }`, `{ type: InstructionFile, path, text }`, `{ type: AttachedFile, path, attachmentType, text }`, or `{ type: Notice, text }`.
|
|
190
202
|
|
|
191
|
-
The `sessions` option gives the earlier conversations, each a `FakeSession`: an `id`, its `messages` as drafts, its `goal`, and any `Session` field, such as `title`, `color`, `project`, or `isRunning`. A session that names no `project` is in the test's project. `{ id: 'test-session', color: 'cyan' }` gives the current session its `/color` from the mod's start, and `tested.type('/color cyan')` and `tested.type('/color default')` run Claude Code's `/color`, which changes `sessions.current.color`. `tested.type('/color')` throws, because Claude Code picks a random color for it.
|
|
203
|
+
The `sessions` option gives the earlier conversations, each a `FakeSession`: an `id`, its `messages` as drafts, its `goal`, and any `Session` field, such as `title`, `color`, `project`, or `isRunning`. A session that names no `project` is in the test's project, and one that names no `device` runs on `test-computer`, such as `{ id: 'remote', device: { id: '9c41d07e2b5a4f86a3e1c8d2f07b6e95', name: 'build-box' } }` for a session on another device. `{ id: 'test-session', color: 'cyan' }` gives the current session its `/color` from the mod's start, and `tested.type('/color cyan')` and `tested.type('/color default')` run Claude Code's `/color`, which changes `sessions.current.color`. `tested.type('/color')` throws, because Claude Code picks a random color for it.
|
|
192
204
|
|
|
193
205
|
```ts
|
|
194
206
|
import { expect, test } from 'bun:test'
|
|
@@ -212,10 +224,10 @@ A `before` listener on the current session answers what `raise` asks, such as `t
|
|
|
212
224
|
|
|
213
225
|
### lines
|
|
214
226
|
|
|
215
|
-
- `shown.
|
|
216
|
-
- A `Markdown` element in the mod's pages, panes, and widgets draws through the mod's own markdown styles, as it does in a session. Other mods' styles are not drawn.
|
|
217
|
-
- A key in a page or widget draws as the cmod plugin gives it to Claude Code: `<mod>/<page>:<key>` in a page, `<mod>/<widget>:<key>` in a widget, and `<mod>/markdown:<scope>:<block>:<key>` in what a markdown style draws, where `<scope>` is the drawn key of the `Markdown` element that holds the block, or its place among the `Markdown` elements without a key, and `<block>` is the block's number in that element's markdown, each from `0`. In the mod's own pane, a style's key draws as `<scope>:<block>:<key>`, and in a reply as `<block>:<key>`. `press`, `input`, and `select` take the key the mod wrote, and a key two blocks share reaches the first.
|
|
218
|
-
- Claude Code refuses a whole tree in which two `Input`, `Select`, `Markdown`, `Client`, `Raster`, or `Image` elements have one key, anywhere in it, and a refused Sidebar draws blank for the session. So drawing a page, a pane, a widget area, or a style that does it rejects with Claude Code's own text, such as `cmod: returned a tree Pane refuses: Markdown "my-mod/all:item-text:0" is drawn twice; each takes its own key`. Elements of different types, such as a `Box` and a `Markdown`, may share a key. Claude Code also refuses a `Box`, `Text`, or `Button` whose `hover` names no `scope` and has no `Box` with a key around it, the `Box` itself included, so drawing one rejects with `cmod: returned a tree Pane refuses: Button "my-mod/all:archive" hover has no Box with a key around it; the nearest keyed Box is what the pointer hovers, or a scope names its group`.
|
|
227
|
+
- `shown.view.lines()` draws the view the mod shows in the Sidebar now, `shown.page.lines()` the page, and `shown.pane.lines()` the pane, and each returns its rows of text. Each rejects when the mod shows none, such as `No pane of my-mod shows. Open one with pane.open(), from a slash command, a hotkey, or setup.` A view or page gets `params`, `isFocused`, `columns`, 80 unless `lines({ columns })` gives another, and `surface: 'terminal'`. A pane gets `params`, `title`, `isFocused`, true once an `open` or `toggle` with `{ focus: true }` opened it, `bodyColumns`, which is the `columns` that `lines({ columns })` gives, else the pane's own `columns`, else 80, `placement: 'dock'`, `scroll: { offset: 0, bodyRows: 24 }`, and `view: {}`. `shown.view.id`, `shown.page.id`, and `shown.pane.id` name what shows, and `params` holds its params.
|
|
228
|
+
- A `Markdown` element in the mod's views, pages, panes, and widgets draws through the mod's own markdown styles, as it does in a session. Other mods' styles are not drawn.
|
|
229
|
+
- A key in a view, page, or widget draws as the cmod plugin gives it to Claude Code: `<mod>/<view or page>:<key>` in a view or page, `<mod>/<widget>:<key>` in a widget, and `<mod>/markdown:<scope>:<block>:<key>` in what a markdown style draws, where `<scope>` is the drawn key of the `Markdown` element that holds the block, or its place among the `Markdown` elements without a key, and `<block>` is the block's number in that element's markdown, each from `0`. In the mod's own pane, a style's key draws as `<scope>:<block>:<key>`, and in a reply as `<block>:<key>`. `press`, `input`, and `select` take the key the mod wrote, and a key two blocks share reaches the first.
|
|
230
|
+
- Claude Code refuses a whole tree in which two `Input`, `Select`, `Markdown`, `Client`, `Raster`, or `Image` elements have one key, anywhere in it, and a refused Sidebar draws blank for the session. So drawing a view, a page, a pane, a widget area, or a style that does it rejects with Claude Code's own text, such as `cmod: returned a tree Pane refuses: Markdown "my-mod/all:item-text:0" is drawn twice; each takes its own key`. Elements of different types, such as a `Box` and a `Markdown`, may share a key. Claude Code also refuses a `Box`, `Text`, or `Button` whose `hover` names no `scope` and has no `Box` with a key around it, the `Box` itself included, so drawing one rejects with `cmod: returned a tree Pane refuses: Button "my-mod/all:archive" hover has no Box with a key around it; the nearest keyed Box is what the pointer hovers, or a scope names its group`.
|
|
219
231
|
- A `Button` draws as Claude Code draws it: `[ Save ]`, or `1: Save` when it is `plain` with a `hotkey`, or `Save` when it is `plain` alone. Borders and colors are not drawn.
|
|
220
232
|
- `lines(area)` draws the mod's widgets in that area, in the rows the person would see, and returns them. A new widget is in the first row of its first area. A layout given in `files` as `/test/home/.local/share/cmod/widgets.json` places the widgets another way. `{ columns }` sets the row's width, 80 by default. `lines(TurnEnd)` draws under the last turn that ended, and rejects when none has: end one with `tested.endTurn()` first.
|
|
221
233
|
- `lines(kind, props, requestId?)` draws the mod's style of that kind with `props` and returns its rows. `Default` draws a plain listing of the props it gets, so a test sees what the style changed. Draw `Reply` with a reply's `text` to test a markdown style. Draws that pass one `requestId` grow one streamed reply.
|
|
@@ -240,14 +252,14 @@ test('the prompt count shows below the prompt', async () => {
|
|
|
240
252
|
|
|
241
253
|
### hotkey
|
|
242
254
|
|
|
243
|
-
`hotkey(name)` presses the hotkey `defineMod` declared under `name`, as the person pressing its combo does, and awaits its listeners. A
|
|
255
|
+
`hotkey(name)` presses the hotkey `defineMod` declared under `name`, as the person pressing its combo does, and awaits its listeners. A view it opens shows as `shown.view`, and a page as `shown.page`. It rejects a name the mod does not declare.
|
|
244
256
|
|
|
245
257
|
```ts
|
|
246
258
|
test('the hotkey shows the prompt count in the Sidebar', async () => {
|
|
247
259
|
const tested = testMod(myMod)
|
|
248
260
|
await tested.add({ type: Prompt, text: 'hello' })
|
|
249
261
|
await tested.hotkey('toggle')
|
|
250
|
-
expect(await tested.shown.
|
|
262
|
+
expect(await tested.shown.view.lines()).toEqual(['Prompts this session: 1'])
|
|
251
263
|
})
|
|
252
264
|
```
|
|
253
265
|
|
|
@@ -257,7 +269,7 @@ test('the hotkey shows the prompt count in the Sidebar', async () => {
|
|
|
257
269
|
|
|
258
270
|
### press, input, and select
|
|
259
271
|
|
|
260
|
-
`shown.page
|
|
272
|
+
`shown.view`, `shown.page`, and `shown.pane` act on an element of what shows as the person does, and await its handler. Each rejects when it does not draw the element: `The pane "<pane>" of <mod> draws no <Element> with the key "<key>".`, or the same for a view or page.
|
|
261
273
|
|
|
262
274
|
- `press(key)` presses the `Button` whose `key` or `label` is `key`, and runs its `onPress`. A `Button` with `href` opens its page as a press in the terminal does ([ui.md](ui.md#a-button-opens-a-web-page-with-href)): it runs `uname -s` and the opener through `fakes.process.run`, or copies the link through `ui.copy` on a Linux with no display.
|
|
263
275
|
- `press(key, link)` presses the link `link` in the `Markdown` whose `key` is `key`, and runs its `onLinkPress`.
|
|
@@ -266,16 +278,16 @@ test('the hotkey shows the prompt count in the Sidebar', async () => {
|
|
|
266
278
|
- `tested.press(area, key)` presses a `Button` one of the mod's widgets draws in that area, the way the person presses it in a row.
|
|
267
279
|
|
|
268
280
|
```ts
|
|
269
|
-
await tested.shown.
|
|
270
|
-
await tested.shown.
|
|
271
|
-
expect(await tested.shown.
|
|
281
|
+
await tested.shown.view.input('search', 'deploy')
|
|
282
|
+
await tested.shown.view.select('agent', 'explorer')
|
|
283
|
+
expect(await tested.shown.view.lines()).toContain('deploy by explorer')
|
|
272
284
|
```
|
|
273
285
|
|
|
274
|
-
`shown.page.focusedKey()
|
|
286
|
+
`shown.view.focusedKey()`, `shown.page.focusedKey()`, and `shown.pane.focusedKey()` return the `key` of the `Button`, `Input`, or `Select` that holds the keyboard in what shows, or `undefined` while it does not hold the keyboard or no element does. A view, page, or pane takes the keyboard when it opens with `{ focus: true }`, and its first `autoFocus` element takes it then. `focus(key)` moves it to another element until it closes or opens again.
|
|
275
287
|
|
|
276
288
|
```ts
|
|
277
|
-
await tested.shown.
|
|
278
|
-
expect(await tested.shown.
|
|
289
|
+
await tested.shown.view.press('Answer')
|
|
290
|
+
expect(await tested.shown.view.focusedKey()).toBe('answer')
|
|
279
291
|
```
|
|
280
292
|
|
|
281
293
|
### moveTo
|
|
@@ -321,9 +333,10 @@ Set a fake to answer a call:
|
|
|
321
333
|
- A `program` job is the exception: it catches the `process.spawn` error and retries after 1, 2, 4, 8 and 16 seconds on real timers, so its `ready()` waits past `bun test`'s 5-second timeout. Set `fakes.process.spawn` before a test starts a `program` job.
|
|
322
334
|
- A `program` job with `singleInstance` spawns `/test/home/.local/share/cmod/programs/cmod program <mod> --no-wait -- <command…>`, so `fakes.process.spawn` gets that `argv`, and the first line the fake writes to standard output is the address of the copy this session runs. A fake that writes `held` and ends tests a session another session runs the copy for: `ready()` resolves the address in `/test/home/.local/share/cmod/instances/<mod>/<program>.json`, which the test gives as `{"address":"unix:/tmp/x.sock"}` in `files`.
|
|
323
335
|
- A `program` job with `isService` spawns `/test/home/.local/share/cmod/programs/cmod program <mod> --service --env <name> -- <command…>`, with one `--env` for each key of its `environment`, every time it starts, and a fake that writes `held` and ends stands for the service: `ready()` resolves the address in the instance file, as above. A file `/test/home/.local/share/cmod/services/<mod>/<program>.json` in `files` stands for a service an earlier version of the mod wrote: when the mod's setup adds no `isService` program of that name, its start spawns `/test/home/.local/share/cmod/programs/cmod program <mod> --remove-service -- <program>`.
|
|
324
|
-
- A `program` job with `
|
|
336
|
+
- A `program` job with `processType` puts `--process-type <type>` right after `--service`.
|
|
325
337
|
- `serve` answers what the cmod plugin asks `cmod serve` for the sessions of the computer, which only a test of the cmod plugin itself reaches: `sessions` lists them, `read(session, part, options)` answers a part such as `messages`, and `unheld(session, action, input)` answers an action on a session no running cmod plugin holds.
|
|
326
338
|
- A `program` job with `isWebApp` puts `--web-app` right after `<mod>` in that `argv`. `ready()` resolves `tailnetUrl` when the instance file gives one, such as `{"address":"127.0.0.1:62021","tailnetUrl":"https://my-computer.my-tailnet.ts.net:62021/"}` in `files`.
|
|
339
|
+
- A `program` job's `provider(name)` waits until the program is `fatal`, and then rejects as `ready()` does, unless the test fakes that program's provider in `providers` ([providers](#providers)). The test kit's cmod answers each of its asks with no provider 2.5 seconds later on `clock.after`, and the job asks again.
|
|
327
340
|
- `files` already fills the `fs` fakes: a write is read back, and `list` lists the given files. The fake file system resolves a relative path against `/`, not against the session's working folder, so a mod under test reads `${sessions.current.project}/<file>` rather than a relative path. `fs.list()` with no path rejects.
|
|
328
341
|
- `ui.open` places every pane by default. A fake that answers `{ isPlaced: false }` holds a pane back.
|
|
329
342
|
- `ui.scroll` answers `{}` by default, as when the window moved. A fake that answers `{ deny }` refuses the scroll.
|
|
@@ -336,7 +349,7 @@ Set a fake to answer a call:
|
|
|
336
349
|
- `command.list` answers the slash commands the mod added by default, and `command.run` answers `{}`, a command that printed nothing.
|
|
337
350
|
- `tool.call` answers `CronCreate`, `CronDelete`, and `CronList` by default, keeping the crons a `schedule` makes in `shown.schedules`. A call to any other tool needs a fake.
|
|
338
351
|
- `files` takes symbolic links too: `testMod(mod, { files, links: { '/work/skills': '/shared/skills' } })` makes `/work/skills` a link to `/shared/skills`, so a test of `fs.find` or `metadata.update` follows it as a real file system does.
|
|
339
|
-
- `testMod` opens no install
|
|
352
|
+
- `testMod` opens no install view: an option with no default and no value fails `start()` with its fix, and the `installer` steps are not shown. Test a step's `render` and `isDone` by calling them with the started `mod`.
|
|
340
353
|
- The mod below runs `git`, and the `process.run` fake answers in its place.
|
|
341
354
|
|
|
342
355
|
```ts
|
|
@@ -372,6 +385,7 @@ type Shown = {
|
|
|
372
385
|
readonly tools: string[]
|
|
373
386
|
readonly schedules: { readonly id: string; readonly cron: string; readonly prompt: string }[]
|
|
374
387
|
readonly sent: { readonly session: string; readonly type: string; readonly text: string; readonly as?: string }[]
|
|
388
|
+
readonly view: ShownPart
|
|
375
389
|
readonly page: ShownPart
|
|
376
390
|
readonly pane: ShownPart
|
|
377
391
|
}
|
|
@@ -383,6 +397,7 @@ type ShownPart = {
|
|
|
383
397
|
press(key: string, link?: string): Promise<void>
|
|
384
398
|
input(key: string, text: string, type?: 'submit' | 'change'): Promise<void>
|
|
385
399
|
select(key: string, value: string): Promise<void>
|
|
400
|
+
focusedKey(): Promise<string | undefined>
|
|
386
401
|
}
|
|
387
402
|
|
|
388
403
|
type ShownToast = { readonly type: ToastType; readonly message: string; readonly description?: string }
|
|
@@ -390,7 +405,7 @@ type ShownToast = { readonly type: ToastType; readonly message: string; readonly
|
|
|
390
405
|
|
|
391
406
|
What the person and Claude would see: the toasts shown now, with a text description, the notes the mod added for Claude with `sessions.current.send` or a `prompt` that follows the state, and the `beforePrompt` notes a subagent reads as it starts from a spawn the test fires, the prompts it sent to the current session, the messages it sent to other sessions in `sent`, the log lines, the debug log lines, the open panes, the slash commands and tools the mod added, and the crons its `schedule` keeps now.
|
|
392
407
|
|
|
393
|
-
The test kit keeps no Log,
|
|
408
|
+
The test kit keeps no Log, so every error cmod catches for the mod shows in `toasts` ([ui.md](ui.md#toasts)).
|
|
394
409
|
|
|
395
410
|
```ts
|
|
396
411
|
test('the Mode prompt follows the mode', async () => {
|
|
@@ -404,7 +419,53 @@ test('the Mode prompt follows the mode', async () => {
|
|
|
404
419
|
})
|
|
405
420
|
```
|
|
406
421
|
|
|
407
|
-
`testing.js` exports the types `TestOptions`, `TestedMod`, `Raise`, `Fakes`, `Shown`, and `
|
|
422
|
+
`testing.js` exports the types `TestOptions`, `TestedMod`, `Raise`, `Fakes`, `Shown`, `TestCall`, and `FakeProvider`, so a test helper can take them.
|
|
423
|
+
|
|
424
|
+
### providers
|
|
425
|
+
|
|
426
|
+
`providers` holds a fake for each provider the test lists in `TestOptions.providers`, in the same order, so a mod tests its side of an interface before the other side ships. Each fake answers its `methods` and runs where its fields say:
|
|
427
|
+
|
|
428
|
+
- With a `program`, it is that program of `mod`, connected to the test kit's `cmod serve`.
|
|
429
|
+
- With a `program` and a `device`, it is that program on that device. The test kit reaches the device as `cmod serve` does, through a `cmod/Devices` provider, the `devices` program of `testMod`, which `providers('cmod/Devices')` lists too.
|
|
430
|
+
- With no `program`, it is the session code of `mod`, another mod in this session. It takes no `device`, and `mod` is never the tested mod.
|
|
431
|
+
|
|
432
|
+
`providers(name)` lists each fake with its `device`, this computer's unless it names one. A `program` job's `provider(name)` resolves with the fake of its own program, and a fake of the tested mod's program stands for that program, so the job spawns nothing the test fakes.
|
|
433
|
+
|
|
434
|
+
Every call and listener goes through the routing a session uses: the cmod plugin's, then `cmod serve`'s, then the other device's. So a call past its 30 s deadline, a method the fake lacks, and a fake that left fail as they would in a session.
|
|
435
|
+
|
|
436
|
+
Each fake is a `Provided` ([dependencies.md](dependencies.md#interfaces-several-mods-answer-one-name)):
|
|
437
|
+
|
|
438
|
+
- `tell(event, payload)` tells each `onE` listener on the fake.
|
|
439
|
+
- `ask(event, payload)` asks each `beforeE` listener and resolves with their answers.
|
|
440
|
+
- `remove()` makes the fake leave, so `providers(name)` no longer lists it, and a call to it fails with `<mod>'s <program> program does not run, so it answers no <interface>.`
|
|
441
|
+
|
|
442
|
+
Each waits until the calls and listeners the mod already sent reach the fake, so a listener the mod just added hears the next `tell`.
|
|
443
|
+
|
|
444
|
+
The mod below keeps each machine change a `demo/Platform` provider tells, and the test fakes that provider as the `platform` program of the `demo` mod on another device:
|
|
445
|
+
|
|
446
|
+
```ts
|
|
447
|
+
const machines = defineMod({
|
|
448
|
+
name: 'machines',
|
|
449
|
+
state: { session: { changed: [] as readonly string[] } },
|
|
450
|
+
async setup(mod) {
|
|
451
|
+
for (const platform of await providers('demo/Platform')) {
|
|
452
|
+
platform.onMachineChange((machine) => void (mod.state.session.changed = [...mod.state.session.changed, `${platform.device.name}: ${machine.name}`]))
|
|
453
|
+
}
|
|
454
|
+
},
|
|
455
|
+
})
|
|
456
|
+
|
|
457
|
+
test('a machine change on devbox is kept with its device', async () => {
|
|
458
|
+
const tested = testMod(machines, {
|
|
459
|
+
providers: [{ name: 'demo/Platform', mod: 'demo', program: 'platform', device: { id: 'devbox-id', name: 'devbox' }, methods: { machines: (region) => [`${region}-1`] } }],
|
|
460
|
+
})
|
|
461
|
+
await tested.start()
|
|
462
|
+
|
|
463
|
+
tested.providers[0].tell('MachineChange', { name: 'eu-1' })
|
|
464
|
+
await tested.settle()
|
|
465
|
+
|
|
466
|
+
expect(tested.state.session.changed).toEqual(['devbox: eu-1'])
|
|
467
|
+
})
|
|
468
|
+
```
|
|
408
469
|
|
|
409
470
|
## Run the tests
|
|
410
471
|
|