@howaboua/pi-shepherdr 0.2.17 → 0.2.18
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/CHANGELOG.md +10 -0
- package/README.md +37 -7
- package/package.json +3 -3
- package/src/agent-identity.ts +17 -0
- package/src/agents-contract.ts +13 -21
- package/src/agents-discovery.ts +7 -6
- package/src/agents-reply.ts +68 -0
- package/src/agents-spawn.ts +1 -10
- package/src/agents-tool.ts +11 -43
- package/src/agents-work.ts +22 -18
- package/src/controller.ts +1 -1
- package/src/extension-reply.ts +35 -0
- package/src/herdr-client.ts +15 -0
- package/src/messages.ts +82 -129
- package/src/{monitor-questions.ts → monitor-extension-messages.ts} +37 -111
- package/src/monitor-record.ts +19 -6
- package/src/monitor-state.ts +71 -25
- package/src/monitor.ts +20 -24
- package/src/peer-inbox.ts +40 -1
- package/src/remote/shepherdr-channel.mjs +0 -1
- package/src/remote/shepherdr-peer.mjs +50 -14
- package/src/remote/shepherdr-session.mjs +106 -190
- package/src/remote/shepherdr.mjs +4 -1
- package/src/remote-client.ts +16 -1
- package/src/session-reader.ts +3 -364
- package/src/settlement.ts +41 -23
- package/src/types.ts +14 -16
- package/src/agents-answer.ts +0 -130
- package/src/ask-answer.ts +0 -377
package/src/ask-answer.ts
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
import { homedir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { getAgent } from "./herdr.js";
|
|
5
|
-
import type { HerdrConnection } from "./herdr-client.js";
|
|
6
|
-
import type { AgentMonitor } from "./monitor.js";
|
|
7
|
-
import type { PaneInfo, PendingAsk } from "./types.js";
|
|
8
|
-
|
|
9
|
-
const REPHRASE_REQUEST_RESPONSE =
|
|
10
|
-
"User asked for rephrase, split, or follow-up.";
|
|
11
|
-
|
|
12
|
-
export interface AskAnswer {
|
|
13
|
-
comment?: string;
|
|
14
|
-
other?: string;
|
|
15
|
-
selections?: string[];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function answersMatch(
|
|
19
|
-
answers: AskAnswer[],
|
|
20
|
-
responses:
|
|
21
|
-
| Array<{ comment?: string; id: string; selections: string[] }>
|
|
22
|
-
| undefined,
|
|
23
|
-
): boolean {
|
|
24
|
-
if (!responses || responses.length !== answers.length) return false;
|
|
25
|
-
return answers.every((answer, index) => {
|
|
26
|
-
const response = responses[index];
|
|
27
|
-
if (!response || response.id !== `p${index + 1}`) return false;
|
|
28
|
-
let selections = [...(answer.selections ?? [])];
|
|
29
|
-
if (answer.other !== undefined) {
|
|
30
|
-
const custom = answer.other.trim() || REPHRASE_REQUEST_RESPONSE;
|
|
31
|
-
selections = [
|
|
32
|
-
...selections.filter((selection) => selection !== custom),
|
|
33
|
-
custom,
|
|
34
|
-
];
|
|
35
|
-
}
|
|
36
|
-
if (selections.length === 0) selections.push(REPHRASE_REQUEST_RESPONSE);
|
|
37
|
-
if (
|
|
38
|
-
[...selections].sort().join("\0") !==
|
|
39
|
-
[...response.selections].sort().join("\0")
|
|
40
|
-
) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
return (answer.comment?.trim() || undefined) === response.comment;
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
interface InputStep {
|
|
48
|
-
final?: boolean;
|
|
49
|
-
keys?: string[];
|
|
50
|
-
text?: string;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
interface AskControls {
|
|
54
|
-
confirm: string;
|
|
55
|
-
down: string;
|
|
56
|
-
next: string;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function down(count: number, key: string): string[] {
|
|
60
|
-
return Array.from({ length: Math.max(0, count) }, () => key);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function answerPlan(
|
|
64
|
-
prompts: PendingAsk["prompts"],
|
|
65
|
-
answers: AskAnswer[],
|
|
66
|
-
controls: AskControls,
|
|
67
|
-
): InputStep[] {
|
|
68
|
-
if (answers.length !== prompts.length) {
|
|
69
|
-
throw new Error(
|
|
70
|
-
`answer requires ${prompts.length} response${prompts.length === 1 ? "" : "s"}, in prompt order`,
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
const steps: InputStep[] = [];
|
|
74
|
-
for (const [promptIndex, prompt] of prompts.entries()) {
|
|
75
|
-
const answer = answers[promptIndex]!;
|
|
76
|
-
const labels = answer.selections ?? [];
|
|
77
|
-
const indexes = labels.map((label) => {
|
|
78
|
-
const index = prompt.choices.findIndex(
|
|
79
|
-
(choice) => choice.label === label,
|
|
80
|
-
);
|
|
81
|
-
if (index < 0) {
|
|
82
|
-
throw new Error(
|
|
83
|
-
`unknown choice ${JSON.stringify(label)} for ${JSON.stringify(prompt.title)}`,
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
return index;
|
|
87
|
-
});
|
|
88
|
-
if (new Set(indexes).size !== indexes.length) {
|
|
89
|
-
throw new Error(`duplicate choice for ${JSON.stringify(prompt.title)}`);
|
|
90
|
-
}
|
|
91
|
-
if (!prompt.multiple && indexes.length > 1) {
|
|
92
|
-
throw new Error(`${JSON.stringify(prompt.title)} accepts one choice`);
|
|
93
|
-
}
|
|
94
|
-
if (!prompt.multiple && indexes.length > 0 && answer.other !== undefined) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
`${JSON.stringify(prompt.title)} cannot combine a choice with Other/rephrase`,
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
indexes.sort((left, right) => left - right);
|
|
100
|
-
let focus = 0;
|
|
101
|
-
for (const index of indexes) {
|
|
102
|
-
steps.push({
|
|
103
|
-
keys: [...down(index - focus, controls.down), controls.confirm],
|
|
104
|
-
});
|
|
105
|
-
focus = index;
|
|
106
|
-
}
|
|
107
|
-
const needsOther =
|
|
108
|
-
answer.other !== undefined ||
|
|
109
|
-
(indexes.length === 0 && answer.comment !== undefined);
|
|
110
|
-
if (needsOther) {
|
|
111
|
-
steps.push({
|
|
112
|
-
keys: [
|
|
113
|
-
...down(prompt.choices.length - focus, controls.down),
|
|
114
|
-
controls.confirm,
|
|
115
|
-
],
|
|
116
|
-
});
|
|
117
|
-
steps.push({
|
|
118
|
-
text: answer.other ?? "",
|
|
119
|
-
keys: [controls.confirm],
|
|
120
|
-
});
|
|
121
|
-
focus = prompt.choices.length;
|
|
122
|
-
}
|
|
123
|
-
if (answer.comment !== undefined) {
|
|
124
|
-
steps.push({
|
|
125
|
-
keys: [
|
|
126
|
-
...down(prompt.choices.length + 1 - focus, controls.down),
|
|
127
|
-
controls.confirm,
|
|
128
|
-
],
|
|
129
|
-
});
|
|
130
|
-
steps.push({ text: answer.comment, keys: [controls.next] });
|
|
131
|
-
} else {
|
|
132
|
-
steps.push({ keys: [controls.next] });
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
steps.push({ keys: [controls.confirm], final: true });
|
|
136
|
-
return steps;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async function readKeybindings(
|
|
140
|
-
client: HerdrConnection,
|
|
141
|
-
): Promise<Record<string, unknown>> {
|
|
142
|
-
if ("keybindings" in client && typeof client.keybindings === "function") {
|
|
143
|
-
const value = await client.keybindings();
|
|
144
|
-
return typeof value === "object" && value !== null
|
|
145
|
-
? (value as Record<string, unknown>)
|
|
146
|
-
: {};
|
|
147
|
-
}
|
|
148
|
-
const directory =
|
|
149
|
-
process.env["PI_CODING_AGENT_DIR"] ?? join(homedir(), ".pi", "agent");
|
|
150
|
-
try {
|
|
151
|
-
const value = JSON.parse(
|
|
152
|
-
await readFile(join(directory, "keybindings.json"), "utf8"),
|
|
153
|
-
) as unknown;
|
|
154
|
-
return typeof value === "object" && value !== null
|
|
155
|
-
? (value as Record<string, unknown>)
|
|
156
|
-
: {};
|
|
157
|
-
} catch {
|
|
158
|
-
return {};
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function keyFor(
|
|
163
|
-
bindings: Record<string, unknown>,
|
|
164
|
-
action: string,
|
|
165
|
-
fallback: string,
|
|
166
|
-
): string {
|
|
167
|
-
const configured = bindings[action];
|
|
168
|
-
if (typeof configured === "string" && configured) return configured;
|
|
169
|
-
if (Array.isArray(configured)) {
|
|
170
|
-
const first = configured.find(
|
|
171
|
-
(key) => typeof key === "string" && key.length > 0,
|
|
172
|
-
);
|
|
173
|
-
if (typeof first === "string") return first;
|
|
174
|
-
if (configured.length === 0) {
|
|
175
|
-
throw new Error(`${action} has no configured keybinding`);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
return fallback;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
async function screen(
|
|
182
|
-
client: HerdrConnection,
|
|
183
|
-
paneId: string,
|
|
184
|
-
): Promise<string> {
|
|
185
|
-
const result = await client.request<unknown>("agent.read", {
|
|
186
|
-
target: paneId,
|
|
187
|
-
source: "visible",
|
|
188
|
-
format: "text",
|
|
189
|
-
lines: 80,
|
|
190
|
-
strip_ansi: true,
|
|
191
|
-
});
|
|
192
|
-
if (
|
|
193
|
-
typeof result !== "object" ||
|
|
194
|
-
result === null ||
|
|
195
|
-
!("read" in result) ||
|
|
196
|
-
typeof result.read !== "object" ||
|
|
197
|
-
result.read === null ||
|
|
198
|
-
!("text" in result.read) ||
|
|
199
|
-
typeof result.read.text !== "string"
|
|
200
|
-
) {
|
|
201
|
-
throw new Error("Herdr agent.read returned no text");
|
|
202
|
-
}
|
|
203
|
-
return result.read.text;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function askFrame(value: string): string {
|
|
207
|
-
const lines = value.split("\n").map((line) => line.trimEnd());
|
|
208
|
-
// Ask's outer rules start at column zero. Nested editors are indented.
|
|
209
|
-
// Require the tab row so a clipped opening cannot borrow a transcript rule.
|
|
210
|
-
const start = lines.findLastIndex(
|
|
211
|
-
(line, index) =>
|
|
212
|
-
/^─+$/.test(line) &&
|
|
213
|
-
/^ +[□■] 1(?: +[□■] \d+)* +(?:Review|Resume)(?: +Agent continues while you decide)?$/.test(
|
|
214
|
-
lines[index + 1] ?? "",
|
|
215
|
-
),
|
|
216
|
-
);
|
|
217
|
-
if (start < 0) return "";
|
|
218
|
-
const end = lines.findIndex(
|
|
219
|
-
(line, index) => index > start && /^─+$/.test(line),
|
|
220
|
-
);
|
|
221
|
-
return end < 0 || lines[end] !== lines[start]
|
|
222
|
-
? ""
|
|
223
|
-
: lines.slice(start + 1, end).join("\n");
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function inspectAskScreen(
|
|
227
|
-
value: string,
|
|
228
|
-
ask: PendingAsk,
|
|
229
|
-
): { currentPrompt?: string; recognized: boolean; selected?: string } {
|
|
230
|
-
const frame = askFrame(value);
|
|
231
|
-
const selected = frame.match(/^>\s+(.*?)\s*$/m)?.[1];
|
|
232
|
-
const review =
|
|
233
|
-
/^\s*Review\s*$/m.test(frame) && frame.includes("enter submit");
|
|
234
|
-
const currentPrompt = review
|
|
235
|
-
? "Review"
|
|
236
|
-
: ask.prompts.find((prompt) =>
|
|
237
|
-
frame.split("\n").some((line) => line.trim() === prompt.title),
|
|
238
|
-
)?.title;
|
|
239
|
-
return {
|
|
240
|
-
recognized:
|
|
241
|
-
Boolean(currentPrompt) ||
|
|
242
|
-
(frame.includes("Other/rephrase") &&
|
|
243
|
-
frame.includes("Comment (optional)")),
|
|
244
|
-
...(currentPrompt ? { currentPrompt } : {}),
|
|
245
|
-
...(selected ? { selected } : {}),
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
async function sendInput(
|
|
250
|
-
client: HerdrConnection,
|
|
251
|
-
panel: PaneInfo,
|
|
252
|
-
step: InputStep,
|
|
253
|
-
signal: AbortSignal,
|
|
254
|
-
): Promise<void> {
|
|
255
|
-
signal.throwIfAborted();
|
|
256
|
-
if (step.text !== undefined) {
|
|
257
|
-
await requireSameAgent(client, panel);
|
|
258
|
-
signal.throwIfAborted();
|
|
259
|
-
await client.request("pane.send_input", {
|
|
260
|
-
pane_id: panel.pane_id,
|
|
261
|
-
text: step.text,
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
if (step.keys) {
|
|
265
|
-
await requireSameAgent(client, panel);
|
|
266
|
-
signal.throwIfAborted();
|
|
267
|
-
await client.request("agent.send_keys", {
|
|
268
|
-
target: panel.pane_id,
|
|
269
|
-
keys: step.keys,
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export function sameAgentIdentity(
|
|
275
|
-
expected: PaneInfo,
|
|
276
|
-
current: PaneInfo,
|
|
277
|
-
): boolean {
|
|
278
|
-
return (
|
|
279
|
-
current.pane_id === expected.pane_id &&
|
|
280
|
-
current.workspace_id === expected.workspace_id &&
|
|
281
|
-
current.tab_id === expected.tab_id &&
|
|
282
|
-
current.terminal_id === expected.terminal_id &&
|
|
283
|
-
current.agent_session?.agent === expected.agent_session?.agent &&
|
|
284
|
-
current.agent_session?.kind === expected.agent_session?.kind &&
|
|
285
|
-
current.agent_session?.source === expected.agent_session?.source &&
|
|
286
|
-
current.agent_session?.value === expected.agent_session?.value
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
async function requireSameAgent(
|
|
291
|
-
client: HerdrConnection,
|
|
292
|
-
panel: PaneInfo,
|
|
293
|
-
): Promise<void> {
|
|
294
|
-
const current = await getAgent(client, panel.pane_id);
|
|
295
|
-
if (!sameAgentIdentity(panel, current)) {
|
|
296
|
-
throw new Error(`${panel.pane_id} no longer hosts the targeted Pi agent`);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
export async function prepareAskAnswer(
|
|
301
|
-
client: HerdrConnection,
|
|
302
|
-
monitor: AgentMonitor,
|
|
303
|
-
panel: PaneInfo,
|
|
304
|
-
answers: AskAnswer[],
|
|
305
|
-
signal: AbortSignal,
|
|
306
|
-
expectedAskId: string,
|
|
307
|
-
): Promise<{ ask: PendingAsk; submit(signal?: AbortSignal): Promise<void> }> {
|
|
308
|
-
const view = await monitor.view(panel);
|
|
309
|
-
if (!view.ask) {
|
|
310
|
-
throw new Error(`${panel.pane_id} has no live Ask on its active branch`);
|
|
311
|
-
}
|
|
312
|
-
const ask = view.ask;
|
|
313
|
-
if (panel.agent_status !== "blocked" && ask.delivery !== "steer") {
|
|
314
|
-
throw new Error(`${panel.pane_id} is not waiting on Ask`);
|
|
315
|
-
}
|
|
316
|
-
if (ask.toolCallId !== expectedAskId) {
|
|
317
|
-
throw new Error(`${panel.pane_id} has a different Ask call`);
|
|
318
|
-
}
|
|
319
|
-
const requireLiveAsk = async () => {
|
|
320
|
-
const current = await getAgent(client, panel.pane_id);
|
|
321
|
-
if (
|
|
322
|
-
!sameAgentIdentity(panel, current) ||
|
|
323
|
-
(ask.delivery !== "steer" && current.agent_status !== "blocked") ||
|
|
324
|
-
(await monitor.view(current)).ask?.toolCallId !== ask.toolCallId
|
|
325
|
-
) {
|
|
326
|
-
throw new Error("ask closed before all requested answers were entered");
|
|
327
|
-
}
|
|
328
|
-
};
|
|
329
|
-
const initial = inspectAskScreen(await screen(client, panel.pane_id), ask);
|
|
330
|
-
const first = ask.prompts[0];
|
|
331
|
-
const expectedSelection = first?.choices[0]?.label ?? "Other/rephrase";
|
|
332
|
-
if (
|
|
333
|
-
!initial.recognized ||
|
|
334
|
-
initial.currentPrompt !== first?.title ||
|
|
335
|
-
initial.selected !== expectedSelection
|
|
336
|
-
) {
|
|
337
|
-
throw new Error(
|
|
338
|
-
"ask UI is not at the first prompt's default selection; refusing to send guessed keys",
|
|
339
|
-
);
|
|
340
|
-
}
|
|
341
|
-
const bindings = await readKeybindings(client);
|
|
342
|
-
const plan = answerPlan(ask.prompts, answers, {
|
|
343
|
-
down: keyFor(bindings, "tui.select.down", "down"),
|
|
344
|
-
confirm: keyFor(bindings, "tui.select.confirm", "enter"),
|
|
345
|
-
next: keyFor(bindings, "tui.input.tab", "tab"),
|
|
346
|
-
});
|
|
347
|
-
const final = plan.pop();
|
|
348
|
-
if (!final?.final) throw new Error("ask answer plan has no final submission");
|
|
349
|
-
for (const step of plan) {
|
|
350
|
-
await requireLiveAsk();
|
|
351
|
-
await sendInput(client, panel, step, signal);
|
|
352
|
-
await new Promise((resolve) => setTimeout(resolve, 35));
|
|
353
|
-
await requireLiveAsk();
|
|
354
|
-
if (
|
|
355
|
-
!inspectAskScreen(await screen(client, panel.pane_id), ask).recognized
|
|
356
|
-
) {
|
|
357
|
-
throw new Error(
|
|
358
|
-
"ask UI became unrecognized; stopped without retrying input",
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return {
|
|
363
|
-
ask,
|
|
364
|
-
submit: async (submitSignal = signal) => {
|
|
365
|
-
await requireLiveAsk();
|
|
366
|
-
if (
|
|
367
|
-
inspectAskScreen(await screen(client, panel.pane_id), ask)
|
|
368
|
-
.currentPrompt !== "Review"
|
|
369
|
-
) {
|
|
370
|
-
throw new Error(
|
|
371
|
-
"ask UI is not ready to submit; stopped without retrying input",
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
await sendInput(client, panel, final, submitSignal);
|
|
375
|
-
},
|
|
376
|
-
};
|
|
377
|
-
}
|