@bridgebrew/harness-echo 0.0.0-stage → 0.0.3
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 +21 -0
- package/README.md +30 -2
- package/package.json +58 -4
- package/server.ts +498 -0
- package/tsconfig.json +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 bridgebrew
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Echo driver (`@bridgebrew/harness-echo`)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An agent that answers each message with that message: no process, no model, no account. It is for tests and for
|
|
4
|
+
trying Sessions without a real agent.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
From the Marketplace (the Plugins screen), or:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
bridge install @bridgebrew/harness-echo --workspace <slug>
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Use
|
|
15
|
+
|
|
16
|
+
In Settings › Harness, an Echo entry takes:
|
|
17
|
+
|
|
18
|
+
- **Reply delay (ms)**: how long each reply waits, to try a running turn (Stop, the queue). Empty: it answers at once.
|
|
19
|
+
- **Steers**: a message sent while a turn runs goes into it, answered at once with `Steered:` and the message. Off:
|
|
20
|
+
the agent cannot steer, to try one that cannot.
|
|
21
|
+
|
|
22
|
+
A few messages act instead of echoing:
|
|
23
|
+
|
|
24
|
+
- `/tool <name> <json arguments>` calls that tool of the session's `bridge` MCP server and answers its text.
|
|
25
|
+
- `/ask <title>` opens a decision the turn waits on until you answer it.
|
|
26
|
+
- `/questions <JSON array>` asks those questions as one decision and says the answer it got.
|
|
27
|
+
- `/subagent <title>` runs a short sub-agent, with a thought, a `Read` call and its words.
|
|
28
|
+
|
|
29
|
+
## Contracts
|
|
30
|
+
|
|
31
|
+
Requires `harness`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bridgebrew/harness-echo",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "Lets the harness run an echo agent: it answers each message with that message, with no process and no model, for tests and trials.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"bridge-plugin"
|
|
8
|
+
],
|
|
9
|
+
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"*.json",
|
|
12
|
+
"*.ts",
|
|
13
|
+
"!**/*.test.ts",
|
|
14
|
+
"!**/*.test.tsx"
|
|
15
|
+
],
|
|
16
|
+
"bridge": {
|
|
17
|
+
"kind": "service",
|
|
18
|
+
"title": "Echo driver",
|
|
19
|
+
"icon": "plug",
|
|
20
|
+
"domain": "agents",
|
|
21
|
+
"server": "./server.ts",
|
|
22
|
+
"requires": {
|
|
23
|
+
"harness": {}
|
|
24
|
+
},
|
|
25
|
+
"driver": {
|
|
26
|
+
"name": "echo",
|
|
27
|
+
"title": "Echo",
|
|
28
|
+
"description": "An agent that answers each message with that message: no process, no model, no account. For tests and for trying Sessions.",
|
|
29
|
+
"config": {
|
|
30
|
+
"type": "object",
|
|
31
|
+
"properties": {
|
|
32
|
+
"delayMs": {
|
|
33
|
+
"type": "integer",
|
|
34
|
+
"minimum": 0,
|
|
35
|
+
"title": "Reply delay (ms)",
|
|
36
|
+
"description": "How long each reply waits, to try a running turn (Stop, the queue). Empty: it answers at once."
|
|
37
|
+
},
|
|
38
|
+
"steer": {
|
|
39
|
+
"type": "boolean",
|
|
40
|
+
"title": "Steers",
|
|
41
|
+
"description": "A message sent while a turn runs goes into it (answered at once with Steered: and the message). Off: the agent cannot steer, to try one that cannot."
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"provenance": "official",
|
|
47
|
+
"placement": "both"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"cordis": "4.0.0-rc.11"
|
|
51
|
+
},
|
|
52
|
+
"releaseStage": "dev",
|
|
53
|
+
"scripts": {
|
|
54
|
+
"test": "bridge plugin test .",
|
|
55
|
+
"typecheck": "bridge plugin check ."
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"bridge": ">=0.0.1-nightly.20261119.1"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/server.ts
ADDED
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
// The `echo` driver (AD-28): answers each message with that message, with no model, for tests and for
|
|
2
|
+
// trying Sessions. Its entry's `delayMs` holds each reply, to try a running turn (Stop, the queue); `interrupt` and
|
|
3
|
+
// `close` clear a held reply, so no timer outlives a turn. A message `/tool <name> <json arguments>` calls that tool of
|
|
4
|
+
// the session's `bridge` MCP server (started through `ctx.processes`, as every harness starts its agent) and answers its text, to try a tool without a model (an agent's `bridge_link`);
|
|
5
|
+
// several such lines run in order in one turn, a turn as another, its server stopped by `interrupt` and `close`; a
|
|
6
|
+
// line `/ask <title>` among them is a decision the turn waits on until the user answers it (a test's pause); a line
|
|
7
|
+
// `/questions <JSON array>` asks those questions (the contract's `questions`: header, options with descriptions,
|
|
8
|
+
// multiSelect, custom, required) as one decision of kind `question`, refuses an answer they did not offer, and says
|
|
9
|
+
// the answer it got (`Answered: <JSON>`, `Dismissed`). `steer` puts a message into the running turn (a held reply, a
|
|
10
|
+
// `/tool`, `/ask` or `/questions` turn): it is answered at once with `Steered: <message>`, and the turn goes on as it
|
|
11
|
+
// was; an entry with `steer: false` cannot steer (no `steer`), to try an agent that cannot.
|
|
12
|
+
// `/subagent <title>` runs a sub-agent of the echo agent's own (the contract's `subagent` event): its `Task` tool call,
|
|
13
|
+
// the sub-agent running with a thought, a `Read` call and its words (each event naming it), held `delayMs` before it
|
|
14
|
+
// completes, then the call's result; `stopSubagent` ends a held one `interrupted`, the turn going on.
|
|
15
|
+
import { randomUUID } from 'node:crypto';
|
|
16
|
+
import { readFileSync } from 'node:fs';
|
|
17
|
+
import type { Context } from 'cordis';
|
|
18
|
+
|
|
19
|
+
export const name = 'harness-echo';
|
|
20
|
+
export const inject = ['harness', 'processes'];
|
|
21
|
+
|
|
22
|
+
/** The `harness` contract's driver (`ctx.harness`, typed by the harness plugin; never imported, AD-8). */
|
|
23
|
+
type Driver = Parameters<Context['harness']['register']>[1];
|
|
24
|
+
type Config = Parameters<Driver['capabilities']>[0];
|
|
25
|
+
type Session = Awaited<ReturnType<Driver['open']>>;
|
|
26
|
+
type TurnEnd = Awaited<ReturnType<Session['prompt']>>;
|
|
27
|
+
type HarnessEvent = Parameters<Parameters<Session['prompt']>[1]>[0];
|
|
28
|
+
type Question = NonNullable<Extract<HarnessEvent, { type: 'decision' }>['questions']>[number];
|
|
29
|
+
/** What answers a decision: an option id, a question request's answers, `null` (dismissed). */
|
|
30
|
+
type Answer = Parameters<Session['answer']>[1];
|
|
31
|
+
type Answers = Exclude<Answer, string | null>;
|
|
32
|
+
|
|
33
|
+
/** Whether `answers` fits `questions` as the harness contract says: each question once, its options among its own,
|
|
34
|
+
* several only with `multiSelect`, a text only with `custom`, every required question answered. */
|
|
35
|
+
export const fits = (questions: Question[], answers: Answers) => {
|
|
36
|
+
if (!Array.isArray(answers.answers)) return false;
|
|
37
|
+
const seen = new Set<string>();
|
|
38
|
+
for (const each of answers.answers) {
|
|
39
|
+
const question = questions.find((one) => one.id === each.question);
|
|
40
|
+
if (!question || seen.has(question.id)) return false;
|
|
41
|
+
seen.add(question.id);
|
|
42
|
+
const chosen = each.options ?? [];
|
|
43
|
+
if (chosen.some((id) => !question.options.some((option) => option.id === id))) return false;
|
|
44
|
+
if (new Set(chosen).size !== chosen.length) return false;
|
|
45
|
+
if (chosen.length > 1 && !question.multiSelect) return false;
|
|
46
|
+
if (each.text !== undefined && !question.custom) return false;
|
|
47
|
+
}
|
|
48
|
+
return questions.every((question) => {
|
|
49
|
+
if (question.required === false) return true;
|
|
50
|
+
const given = answers.answers.find((each) => each.question === question.id);
|
|
51
|
+
return !!given?.options?.length || !!given?.text?.trim();
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** A `/questions` line's questions (its JSON array, each with an id, a question and its options), else undefined. */
|
|
56
|
+
const questionsOf = (text: string): Question[] | undefined => {
|
|
57
|
+
let parsed: unknown;
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(text);
|
|
60
|
+
} catch {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
if (!Array.isArray(parsed) || !parsed.length) return undefined;
|
|
64
|
+
const questions: Question[] = [];
|
|
65
|
+
for (const each of parsed as unknown[]) {
|
|
66
|
+
if (typeof each !== 'object' || each === null) return undefined;
|
|
67
|
+
const { id, header, question, options, multiSelect, custom, required } = each as Record<
|
|
68
|
+
string,
|
|
69
|
+
unknown
|
|
70
|
+
>;
|
|
71
|
+
if (typeof id !== 'string' || typeof question !== 'string' || !Array.isArray(options))
|
|
72
|
+
return undefined;
|
|
73
|
+
const choices: Question['options'] = [];
|
|
74
|
+
for (const option of options as unknown[]) {
|
|
75
|
+
if (typeof option !== 'object' || option === null) return undefined;
|
|
76
|
+
const { id: choice, label, description } = option as Record<string, unknown>;
|
|
77
|
+
if (typeof choice !== 'string' || typeof label !== 'string') return undefined;
|
|
78
|
+
choices.push({ id: choice, label, ...(typeof description === 'string' && { description }) });
|
|
79
|
+
}
|
|
80
|
+
questions.push({
|
|
81
|
+
id,
|
|
82
|
+
...(typeof header === 'string' && { header }),
|
|
83
|
+
question,
|
|
84
|
+
options: choices,
|
|
85
|
+
...(multiSelect === true && { multiSelect: true as const }),
|
|
86
|
+
...(custom === true && { custom: true as const }),
|
|
87
|
+
...(typeof required === 'boolean' && { required }),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return questions;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** Whether the entry's sessions steer: unless its `steer` is false. */
|
|
94
|
+
const steersOf = (config: Config) => config.steer !== false;
|
|
95
|
+
|
|
96
|
+
/** The entry's `delayMs` when it is a usable hold (a non-negative finite number), else 0: answer at once. */
|
|
97
|
+
const delayOf = (config: Config): number => {
|
|
98
|
+
const delay = config.delayMs;
|
|
99
|
+
return typeof delay === 'number' && Number.isFinite(delay) && delay >= 0 ? delay : 0;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
type Servers = Parameters<Driver['open']>[0]['mcpServers'];
|
|
103
|
+
/** Starts a process through the host (`ctx.processes.spawn` for this plugin: its guard, its own process group, killed
|
|
104
|
+
* with the plugin). */
|
|
105
|
+
export type Spawn = (
|
|
106
|
+
options: Parameters<Context['processes']['spawn']>[1],
|
|
107
|
+
) => ReturnType<Context['processes']['spawn']>;
|
|
108
|
+
|
|
109
|
+
/** One `/tool <name> <arguments>` line's call: the tool called on the session's `bridge` server (one JSON-RPC call over
|
|
110
|
+
* its stdio): `answer` its text (always settles: an answer, the server leaving without one, or not starting), `stop`
|
|
111
|
+
* ends its server. */
|
|
112
|
+
const toolCall = (
|
|
113
|
+
name: string,
|
|
114
|
+
given: string,
|
|
115
|
+
{ servers, spawn, cwd }: { servers: Servers; spawn: Spawn | undefined; cwd: string },
|
|
116
|
+
): { answer: Promise<string>; stop: () => void } => {
|
|
117
|
+
const server = servers.find((each) => each.name === 'bridge' && 'command' in each);
|
|
118
|
+
const now = (answer: string) => ({ answer: Promise.resolve(answer), stop: () => {} });
|
|
119
|
+
if (!server || !('command' in server)) return now('No bridge MCP server in this session.');
|
|
120
|
+
if (!spawn) return now('No process service here to start the bridge MCP server.');
|
|
121
|
+
let args: unknown;
|
|
122
|
+
try {
|
|
123
|
+
args = given ? JSON.parse(given) : {};
|
|
124
|
+
} catch {
|
|
125
|
+
return now('The arguments are no JSON object.');
|
|
126
|
+
}
|
|
127
|
+
let stopped = false;
|
|
128
|
+
let kill = () => {};
|
|
129
|
+
const answer = (async (): Promise<string> => {
|
|
130
|
+
let child: Awaited<ReturnType<Spawn>>;
|
|
131
|
+
try {
|
|
132
|
+
child = await spawn({
|
|
133
|
+
command: server.command,
|
|
134
|
+
args: server.args,
|
|
135
|
+
cwd,
|
|
136
|
+
...(server.env && { env: server.env }),
|
|
137
|
+
});
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return `The bridge MCP server did not start: ${error instanceof Error ? error.message : String(error)}`;
|
|
140
|
+
}
|
|
141
|
+
kill = () => void child.kill().catch(() => {});
|
|
142
|
+
if (stopped) kill();
|
|
143
|
+
return new Promise<string>((resolve) => {
|
|
144
|
+
let out = '';
|
|
145
|
+
child.onOutput((stream, chunk) => {
|
|
146
|
+
if (stream !== 'stdout') return;
|
|
147
|
+
out += chunk;
|
|
148
|
+
const line = out.split('\n')[0];
|
|
149
|
+
if (!out.includes('\n') || line === undefined) return;
|
|
150
|
+
kill();
|
|
151
|
+
try {
|
|
152
|
+
const reply: unknown = JSON.parse(line);
|
|
153
|
+
const content = (reply as { result?: { content?: { text?: unknown }[] } }).result
|
|
154
|
+
?.content;
|
|
155
|
+
resolve(content?.map((each) => String(each.text ?? '')).join('\n') ?? line);
|
|
156
|
+
} catch {
|
|
157
|
+
resolve(line);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
// Gone without a line (a first answer above wins: a promise settles once).
|
|
161
|
+
void child.exited.then(() => resolve('The bridge MCP server ended without an answer.'));
|
|
162
|
+
child
|
|
163
|
+
.write(
|
|
164
|
+
`${JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name, arguments: args } })}\n`,
|
|
165
|
+
)
|
|
166
|
+
.catch(() => {});
|
|
167
|
+
});
|
|
168
|
+
})();
|
|
169
|
+
return {
|
|
170
|
+
answer,
|
|
171
|
+
stop: () => {
|
|
172
|
+
stopped = true;
|
|
173
|
+
kill();
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** A message's `/tool`, `/ask` and `/questions` lines (each a line of its own: the first message comes after Bridge's
|
|
179
|
+
* session preamble), one after the other: a tool's answer to `say`, an `/ask <title>` a decision the turn waits on
|
|
180
|
+
* (`ask`, a pause the user ends), a `/questions <JSON>` its questions asked, the answer said. `done` settles after the
|
|
181
|
+
* last line (or once stopped), `stop` ends the line running and the rest; none for a message without one. */
|
|
182
|
+
const toolCalls = (
|
|
183
|
+
text: string,
|
|
184
|
+
where: { servers: Servers; spawn: Spawn | undefined; cwd: string },
|
|
185
|
+
{
|
|
186
|
+
say,
|
|
187
|
+
ask,
|
|
188
|
+
subagent,
|
|
189
|
+
}: {
|
|
190
|
+
say: (answer: string) => void;
|
|
191
|
+
ask: (title: string, questions?: Question[]) => Promise<Answer | undefined>;
|
|
192
|
+
subagent: (title: string) => Promise<void>;
|
|
193
|
+
},
|
|
194
|
+
): { done: Promise<void>; stop: () => void } | undefined => {
|
|
195
|
+
const lines = [...text.matchAll(/^\/(tool|ask|questions|subagent)[ \t]+(\S+)?[ \t]*(.*)$/gm)];
|
|
196
|
+
if (!lines.length) return undefined;
|
|
197
|
+
let stopped = false;
|
|
198
|
+
let said = false;
|
|
199
|
+
let running: { stop: () => void } | undefined;
|
|
200
|
+
const done = (async () => {
|
|
201
|
+
// Its turn is recorded first (the caller's next lines): a first line runs as that turn's.
|
|
202
|
+
await Promise.resolve();
|
|
203
|
+
for (const line of lines) {
|
|
204
|
+
if (stopped) return;
|
|
205
|
+
if (line[1] === 'ask') {
|
|
206
|
+
await ask(`${line[2] ?? ''} ${line[3] ?? ''}`.trim() || 'Continue?');
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (line[1] === 'questions') {
|
|
210
|
+
const questions = questionsOf(`${line[2] ?? ''} ${line[3] ?? ''}`);
|
|
211
|
+
const answer = questions
|
|
212
|
+
? await ask(
|
|
213
|
+
questions
|
|
214
|
+
.map((each) => (each.header ? `${each.header}: ${each.question}` : each.question))
|
|
215
|
+
.join('\n'),
|
|
216
|
+
questions,
|
|
217
|
+
)
|
|
218
|
+
: undefined;
|
|
219
|
+
if (stopped) return;
|
|
220
|
+
say(
|
|
221
|
+
`${said ? '\n\n' : ''}${
|
|
222
|
+
!questions
|
|
223
|
+
? 'The questions are no JSON array of questions.'
|
|
224
|
+
: answer === null || answer === undefined
|
|
225
|
+
? 'Dismissed'
|
|
226
|
+
: `Answered: ${JSON.stringify(answer)}`
|
|
227
|
+
}`,
|
|
228
|
+
);
|
|
229
|
+
said = true;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (line[1] === 'subagent') {
|
|
233
|
+
await subagent(`${line[2] ?? ''} ${line[3] ?? ''}`.trim() || 'Sub-task');
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const call = toolCall(line[2] ?? '', line[3] ?? '', where);
|
|
237
|
+
running = call;
|
|
238
|
+
const answer = await call.answer;
|
|
239
|
+
if (stopped) return;
|
|
240
|
+
// The turn's text is one: a blank line between two answers.
|
|
241
|
+
say(said ? `\n\n${answer}` : answer);
|
|
242
|
+
said = true;
|
|
243
|
+
}
|
|
244
|
+
})();
|
|
245
|
+
return {
|
|
246
|
+
done,
|
|
247
|
+
stop: () => {
|
|
248
|
+
stopped = true;
|
|
249
|
+
running?.stop();
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/** The echo driver: a session answers each turn with its text, after `delayMs` when the entry sets one. */
|
|
255
|
+
/** `spawn`: the plugin's way to start a process (its `/tool` lines' MCP server); none, those lines say so. */
|
|
256
|
+
export const echoDriver = (spawn?: Spawn): Driver => ({
|
|
257
|
+
name: 'echo',
|
|
258
|
+
status: async () => ({ installed: true, version: '1' }),
|
|
259
|
+
capabilities: (config) => ({
|
|
260
|
+
resume: true,
|
|
261
|
+
images: false,
|
|
262
|
+
models: true,
|
|
263
|
+
modes: false,
|
|
264
|
+
steer: steersOf(config),
|
|
265
|
+
}),
|
|
266
|
+
models: async () => [{ id: 'echo', name: 'Echo', current: true }],
|
|
267
|
+
open: async ({ config, model, resume, mcpServers, cwd }) => {
|
|
268
|
+
const delayMs = delayOf(config);
|
|
269
|
+
let closed = false;
|
|
270
|
+
/** The turn running: `stop` ends what it waits for (its reply's timer, its tool's server); `say` streams text to
|
|
271
|
+
* it (a steered message's answer). */
|
|
272
|
+
let turn:
|
|
273
|
+
| { stop: () => void; end: (end: TurnEnd) => void; say: (text: string) => void }
|
|
274
|
+
| undefined;
|
|
275
|
+
/** An `/ask` or `/questions` line's decision the turn waits on, ended by its answer (`undefined`: the turn
|
|
276
|
+
* stopped). */
|
|
277
|
+
let asking:
|
|
278
|
+
| { id: string; resolve: (answer?: Answer) => void; questions?: Question[] }
|
|
279
|
+
| undefined;
|
|
280
|
+
/** A `/subagent` line's sub-agent being held (`delayMs`): `end` settles it, stopped or not. */
|
|
281
|
+
let held: { id: string; end: (stopped: boolean) => void } | undefined;
|
|
282
|
+
let subagents = 0;
|
|
283
|
+
let markExited = () => {};
|
|
284
|
+
const exited = new Promise<void>((resolve) => {
|
|
285
|
+
markExited = resolve;
|
|
286
|
+
});
|
|
287
|
+
/** Ends the running turn, clearing its timer; nothing when none runs. */
|
|
288
|
+
const finish = (end: TurnEnd) => {
|
|
289
|
+
const running = turn;
|
|
290
|
+
if (!running) return;
|
|
291
|
+
turn = undefined;
|
|
292
|
+
running.stop();
|
|
293
|
+
running.end(end);
|
|
294
|
+
};
|
|
295
|
+
return {
|
|
296
|
+
resume: resume ?? randomUUID(),
|
|
297
|
+
model: model ?? 'echo',
|
|
298
|
+
mode: undefined,
|
|
299
|
+
get closed() {
|
|
300
|
+
return closed;
|
|
301
|
+
},
|
|
302
|
+
exited,
|
|
303
|
+
resumed: resume !== undefined,
|
|
304
|
+
prompt: (text, onEvent) => {
|
|
305
|
+
if (closed)
|
|
306
|
+
return Promise.resolve({ reason: 'failed', message: 'The echo session is closed' });
|
|
307
|
+
if (turn)
|
|
308
|
+
return Promise.resolve({ reason: 'failed', message: 'A turn is already running' });
|
|
309
|
+
let running:
|
|
310
|
+
| { stop: () => void; end: (end: TurnEnd) => void; say: (text: string) => void }
|
|
311
|
+
| undefined;
|
|
312
|
+
// Interrupted or closed meanwhile: the turn ended, nothing more of it.
|
|
313
|
+
const live = () => running !== undefined && turn === running;
|
|
314
|
+
const called = toolCalls(
|
|
315
|
+
text,
|
|
316
|
+
{ servers: mcpServers, spawn, cwd },
|
|
317
|
+
{
|
|
318
|
+
say: (answer) => {
|
|
319
|
+
if (live()) onEvent({ type: 'text', text: answer });
|
|
320
|
+
},
|
|
321
|
+
subagent: async (title) => {
|
|
322
|
+
if (!live()) return;
|
|
323
|
+
const id = `sub-${++subagents}`;
|
|
324
|
+
const tool = `task-${subagents}`;
|
|
325
|
+
const call = {
|
|
326
|
+
type: 'tool' as const,
|
|
327
|
+
id: tool,
|
|
328
|
+
title: `Task: ${title}`,
|
|
329
|
+
kind: 'other',
|
|
330
|
+
};
|
|
331
|
+
onEvent({ ...call, status: 'running', input: { description: title, prompt: title } });
|
|
332
|
+
const started = { type: 'subagent' as const, id, title, agent: 'echo', tool };
|
|
333
|
+
onEvent({ ...started, status: 'running', prompt: title });
|
|
334
|
+
onEvent({ type: 'thought', text: `Looking into ${title}`, subagent: id });
|
|
335
|
+
const read = { type: 'tool' as const, id: `${id}-read`, title: 'Read notes.md' };
|
|
336
|
+
onEvent({ ...read, kind: 'read', status: 'running', subagent: id });
|
|
337
|
+
onEvent({
|
|
338
|
+
...read,
|
|
339
|
+
kind: 'read',
|
|
340
|
+
status: 'completed',
|
|
341
|
+
output: 'notes',
|
|
342
|
+
subagent: id,
|
|
343
|
+
});
|
|
344
|
+
onEvent({ type: 'text', text: `${title}: done`, subagent: id });
|
|
345
|
+
const stopped =
|
|
346
|
+
delayMs > 0 &&
|
|
347
|
+
(await new Promise<boolean>((resolve) => {
|
|
348
|
+
const timer = setTimeout(() => resolve(false), delayMs);
|
|
349
|
+
held = {
|
|
350
|
+
id,
|
|
351
|
+
end: (stopped) => {
|
|
352
|
+
clearTimeout(timer);
|
|
353
|
+
held = undefined;
|
|
354
|
+
resolve(stopped);
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
}));
|
|
358
|
+
if (!live()) return;
|
|
359
|
+
onEvent({ ...started, status: stopped ? 'interrupted' : 'completed' });
|
|
360
|
+
onEvent({
|
|
361
|
+
...call,
|
|
362
|
+
status: stopped ? 'failed' : 'completed',
|
|
363
|
+
output: stopped ? 'Stopped' : `${title}: done`,
|
|
364
|
+
});
|
|
365
|
+
},
|
|
366
|
+
ask: (title, questions) =>
|
|
367
|
+
new Promise<Answer | undefined>((resolve) => {
|
|
368
|
+
if (!live()) return resolve(undefined);
|
|
369
|
+
asking = { id: randomUUID(), resolve, ...(questions && { questions }) };
|
|
370
|
+
const [only] = questions ?? [];
|
|
371
|
+
onEvent({
|
|
372
|
+
type: 'decision',
|
|
373
|
+
id: asking.id,
|
|
374
|
+
title,
|
|
375
|
+
...(questions
|
|
376
|
+
? {
|
|
377
|
+
kind: 'question' as const,
|
|
378
|
+
questions,
|
|
379
|
+
// One single-choice question: its options are the decision's too.
|
|
380
|
+
options:
|
|
381
|
+
questions.length === 1 && only && !only.multiSelect
|
|
382
|
+
? only.options.map(({ id, label }) => ({
|
|
383
|
+
id,
|
|
384
|
+
label,
|
|
385
|
+
kind: 'other' as const,
|
|
386
|
+
}))
|
|
387
|
+
: [],
|
|
388
|
+
}
|
|
389
|
+
: { options: [{ id: 'continue', label: 'Continue', kind: 'other' as const }] }),
|
|
390
|
+
});
|
|
391
|
+
}),
|
|
392
|
+
},
|
|
393
|
+
);
|
|
394
|
+
if (called)
|
|
395
|
+
return new Promise<TurnEnd>((resolve) => {
|
|
396
|
+
running = {
|
|
397
|
+
stop: () => {
|
|
398
|
+
called.stop();
|
|
399
|
+
const waiting = asking;
|
|
400
|
+
asking = undefined;
|
|
401
|
+
waiting?.resolve();
|
|
402
|
+
held?.end(true);
|
|
403
|
+
},
|
|
404
|
+
end: resolve,
|
|
405
|
+
say: (said) => onEvent({ type: 'text', text: said }),
|
|
406
|
+
};
|
|
407
|
+
const mine = running;
|
|
408
|
+
turn = mine;
|
|
409
|
+
void called.done.then(() => {
|
|
410
|
+
if (turn !== mine) return;
|
|
411
|
+
turn = undefined;
|
|
412
|
+
resolve({ reason: 'completed' });
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
return new Promise<TurnEnd>((resolve) => {
|
|
416
|
+
/** The reply: the message back, then the turn completed. */
|
|
417
|
+
const reply = () => {
|
|
418
|
+
turn = undefined;
|
|
419
|
+
onEvent({ type: 'text', text });
|
|
420
|
+
resolve({ reason: 'completed' });
|
|
421
|
+
};
|
|
422
|
+
if (delayMs === 0) {
|
|
423
|
+
reply();
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
const timer = setTimeout(reply, delayMs);
|
|
427
|
+
turn = {
|
|
428
|
+
stop: () => clearTimeout(timer),
|
|
429
|
+
end: resolve,
|
|
430
|
+
say: (said) => onEvent({ type: 'text', text: said }),
|
|
431
|
+
};
|
|
432
|
+
});
|
|
433
|
+
},
|
|
434
|
+
interrupt: async () => finish({ reason: 'interrupted' }),
|
|
435
|
+
...(steersOf(config) && {
|
|
436
|
+
steer: async (text: string) => {
|
|
437
|
+
if (closed || !turn) return false;
|
|
438
|
+
turn.say(`Steered: ${text}\n\n`);
|
|
439
|
+
return true;
|
|
440
|
+
},
|
|
441
|
+
}),
|
|
442
|
+
close: async () => {
|
|
443
|
+
if (closed) return;
|
|
444
|
+
closed = true;
|
|
445
|
+
finish({ reason: 'interrupted', message: 'Session closed' });
|
|
446
|
+
markExited();
|
|
447
|
+
},
|
|
448
|
+
answer: async (decision, option) => {
|
|
449
|
+
if (asking?.id !== decision) return false;
|
|
450
|
+
const pending = asking;
|
|
451
|
+
if (option !== null && typeof option === 'object') {
|
|
452
|
+
if (!pending.questions || !fits(pending.questions, option)) return false;
|
|
453
|
+
} else if (
|
|
454
|
+
option !== null &&
|
|
455
|
+
!(pending.questions
|
|
456
|
+
? pending.questions.length === 1 &&
|
|
457
|
+
!pending.questions[0]!.multiSelect &&
|
|
458
|
+
pending.questions[0]!.options.some((each) => each.id === option)
|
|
459
|
+
: option === 'continue')
|
|
460
|
+
)
|
|
461
|
+
return false;
|
|
462
|
+
asking = undefined;
|
|
463
|
+
pending.resolve(option);
|
|
464
|
+
return true;
|
|
465
|
+
},
|
|
466
|
+
setMode: async () => {
|
|
467
|
+
throw new Error('harness echo has no modes');
|
|
468
|
+
},
|
|
469
|
+
stopSubagent: async (id) => {
|
|
470
|
+
if (held?.id !== id) return false;
|
|
471
|
+
held.end(true);
|
|
472
|
+
return true;
|
|
473
|
+
},
|
|
474
|
+
};
|
|
475
|
+
},
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
/** The manifest's `bridge.driver`, for screens (title, description, settings schema). */
|
|
479
|
+
const infoOf = () => {
|
|
480
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- the plugin's own bundled package.json
|
|
481
|
+
const manifest = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')) as {
|
|
482
|
+
bridge?: { driver?: Record<string, unknown> };
|
|
483
|
+
};
|
|
484
|
+
const { title, description, config } = manifest.bridge?.driver ?? {};
|
|
485
|
+
return {
|
|
486
|
+
...(typeof title === 'string' && { title }),
|
|
487
|
+
...(typeof description === 'string' && { description }),
|
|
488
|
+
...(typeof config === 'object' &&
|
|
489
|
+
config !== null && { config: config as Record<string, unknown> }),
|
|
490
|
+
};
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
export function apply(ctx: Context) {
|
|
494
|
+
ctx.harness.register(ctx, {
|
|
495
|
+
...echoDriver((options) => ctx.processes.spawn(ctx, options)),
|
|
496
|
+
info: infoOf(),
|
|
497
|
+
});
|
|
498
|
+
}
|
package/tsconfig.json
ADDED