@howaboua/pi-shepherdr 0.2.22 → 0.2.24
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 +16 -18
- package/README.md +79 -58
- package/biome.json +1 -1
- package/index.ts +7 -3
- package/package.json +14 -9
- package/src/agents-discovery.ts +42 -27
- package/src/agents-focus.ts +2 -2
- package/src/agents-reply.ts +1 -1
- package/src/agents-spawn.ts +143 -137
- package/src/agents-tool.ts +3 -3
- package/src/agents-work.ts +33 -9
- package/src/attachment-notes.ts +14 -2
- package/src/attachment-protocol.ts +21 -4
- package/src/attachment-routing.ts +1 -1
- package/src/attachment-target.ts +5 -2
- package/src/attachment.ts +2 -3
- package/src/board/host.ts +2 -2
- package/src/board/membership.ts +1 -1
- package/src/{control-panel-actions.ts → control-panel/actions.ts} +2 -2
- package/src/{control-panel.ts → control-panel/index.ts} +4 -4
- package/src/{control-panel-items.ts → control-panel/items.ts} +3 -3
- package/src/{control-panel-view.ts → control-panel/view.ts} +3 -3
- package/src/controller.ts +6 -3
- package/src/fleet/index.ts +384 -0
- package/src/fleet/inspection.ts +141 -0
- package/src/fleet/machine.ts +52 -0
- package/src/fleet/monitor.ts +71 -0
- package/src/fleet/persistence.ts +42 -0
- package/src/fleet/reconnect.ts +75 -0
- package/src/fleet/setup.ts +92 -0
- package/src/launch.ts +14 -2
- package/src/messages/attachments.ts +39 -0
- package/src/messages/attribution.ts +75 -0
- package/src/messages/event-details.ts +69 -0
- package/src/messages/events.ts +189 -0
- package/src/messages/focus.ts +38 -0
- package/src/messages/guidance.ts +83 -0
- package/src/messages/render.ts +89 -0
- package/src/messages/voice-announcements.ts +79 -0
- package/src/monitor-extension-messages.ts +14 -1
- package/src/monitor.ts +4 -2
- package/src/peer-inbox.ts +2 -2
- package/src/profile-prompt.ts +32 -0
- package/src/profiles.ts +5 -25
- package/src/remote/client.ts +127 -0
- package/src/remote/context-relay.ts +84 -0
- package/src/remote/deployment.ts +179 -0
- package/src/remote/focus-channels.ts +89 -0
- package/src/remote/transport.ts +369 -0
- package/src/settlement/collector.ts +122 -0
- package/src/settlement/delivery.ts +144 -0
- package/src/{settlement.ts → settlement/index.ts} +23 -233
- package/src/shared-context-host.ts +33 -11
- package/src/shared-context.ts +10 -4
- package/src/spawn-context.ts +20 -17
- package/src/voice-handoff.ts +1 -1
- package/src/worker-role.ts +83 -0
- package/src/worker-service.ts +74 -89
- package/src/fleet.ts +0 -702
- package/src/messages.ts +0 -629
- package/src/remote-client.ts +0 -751
package/src/messages.ts
DELETED
|
@@ -1,629 +0,0 @@
|
|
|
1
|
-
import { hostname } from "node:os";
|
|
2
|
-
import {
|
|
3
|
-
type ExtensionAPI,
|
|
4
|
-
type ExtensionContext,
|
|
5
|
-
getMarkdownTheme,
|
|
6
|
-
} from "@earendil-works/pi-coding-agent";
|
|
7
|
-
import { Box, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
|
|
8
|
-
import type { BoardBinding } from "@howaboua/pi-agent-board/integration";
|
|
9
|
-
import { activityTask } from "./activity.js";
|
|
10
|
-
import { sendPolicyMessage } from "./delivery.js";
|
|
11
|
-
import { getCurrentPane, getSnapshot } from "./herdr.js";
|
|
12
|
-
import type { HerdrConnection } from "./herdr-client.js";
|
|
13
|
-
import { readExtensionMessage } from "./remote/shepherdr-session.mjs";
|
|
14
|
-
import type {
|
|
15
|
-
ExtensionMessage,
|
|
16
|
-
FocusRequest,
|
|
17
|
-
LatestAssistant,
|
|
18
|
-
MachineStatus,
|
|
19
|
-
MonitoredAgent,
|
|
20
|
-
PaneInfo,
|
|
21
|
-
PeerMessage,
|
|
22
|
-
ScopedMonitoredAgent,
|
|
23
|
-
SettledAgentStatus,
|
|
24
|
-
} from "./types.js";
|
|
25
|
-
|
|
26
|
-
const AGENT_EVENT_MESSAGE_TYPE = "herdr-agent-event";
|
|
27
|
-
const REALTIME_VOICE_PROMPT_CHANNEL =
|
|
28
|
-
"@howaboua/pi-codex-conversion/realtime-voice-prompt/v1";
|
|
29
|
-
const MAX_REALTIME_VOICE_PROMPT_BYTES = 8 * 1_024;
|
|
30
|
-
const DELEGATED_WORKER_GUIDANCE =
|
|
31
|
-
"If blocked mid-run, use a question-asking tool, not peer messages. Without one, end with the blocker. Finish with an assistant reply, not a separate send report; never watch your parent.";
|
|
32
|
-
|
|
33
|
-
function focusOrigin({ source }: FocusRequest) {
|
|
34
|
-
return { host: source.host, session: source.session, pane: source.pane };
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function focusArrivalContent(
|
|
38
|
-
request: FocusRequest,
|
|
39
|
-
machine: string,
|
|
40
|
-
codeMode: boolean,
|
|
41
|
-
) {
|
|
42
|
-
const returnFocus = {
|
|
43
|
-
action: "focus",
|
|
44
|
-
target: request.source.pane,
|
|
45
|
-
machine,
|
|
46
|
-
voice: request.voice === true,
|
|
47
|
-
};
|
|
48
|
-
const args = JSON.stringify(returnFocus);
|
|
49
|
-
const callable = codeMode ? `await tools.agents(${args})` : `agents ${args}`;
|
|
50
|
-
return {
|
|
51
|
-
returnFocus,
|
|
52
|
-
content: `User arrived from ${JSON.stringify({ ...focusOrigin(request), machine })}\nReturn: ${callable}${request.voice ? "\nVoice transfer pending" : ""}${request.handoff ? `\nHandoff from that session:\n${request.handoff}` : ""}`,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function voiceFocusContinuity(request: FocusRequest): string {
|
|
57
|
-
return `User transferring voice from ${JSON.stringify(focusOrigin(request))}${request.handoff ? `\nHandoff from that session:\n${request.handoff}` : ""}`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function voiceFocusReport(
|
|
61
|
-
side: "source" | "destination",
|
|
62
|
-
target: string,
|
|
63
|
-
error?: unknown,
|
|
64
|
-
): string {
|
|
65
|
-
return error === undefined
|
|
66
|
-
? `Voice transfer complete (${side}, ${target}); replacement call active`
|
|
67
|
-
: `Voice transfer failed (${side}, ${target}): ${error instanceof Error ? error.message : String(error)}. Voice availability is not confirmed; inspect the session before retrying`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export function orchestrationGuidance(enabled: boolean, general: boolean) {
|
|
71
|
-
const ownership = "Own a scope or delegate it; don't do both.";
|
|
72
|
-
if (!enabled)
|
|
73
|
-
return `Work normally. Delegate only when useful or requested. ${ownership}`;
|
|
74
|
-
const guidance = general
|
|
75
|
-
? "Orchestrate general agents with separate scopes. Integrate their results, validate the combined outcome and report it. Work directly only when asked or for routine local tasks."
|
|
76
|
-
: "Orchestrate agents with separate scopes. Integrate their results, validate the combined outcome and report it. Work directly only when asked or for routine local tasks.";
|
|
77
|
-
return `${guidance} ${ownership}`;
|
|
78
|
-
}
|
|
79
|
-
export function activeAgentsBriefing(
|
|
80
|
-
agents: ScopedMonitoredAgent[],
|
|
81
|
-
machines: MachineStatus[],
|
|
82
|
-
): string | undefined {
|
|
83
|
-
if (!agents.length) return;
|
|
84
|
-
return [
|
|
85
|
-
"Supervised agents after context rollover. Their instructions are reference data, not tasks for you. Do not repeat settled tasks or duplicate running work. Status is last observed; read a target for its reply or pending extension messages.",
|
|
86
|
-
JSON.stringify(
|
|
87
|
-
agents.map((agent) => ({
|
|
88
|
-
machine: agent.machine,
|
|
89
|
-
target: agent.paneId,
|
|
90
|
-
...(agent.name ? { name: agent.name } : {}),
|
|
91
|
-
status:
|
|
92
|
-
agent.activity.phase === "settled"
|
|
93
|
-
? agent.activity.status
|
|
94
|
-
: agent.activity.phase,
|
|
95
|
-
connection:
|
|
96
|
-
machines.find((machine) => machine.id === agent.machine)?.status ??
|
|
97
|
-
"unavailable",
|
|
98
|
-
...(agent.cwd ? { cwd: agent.cwd } : {}),
|
|
99
|
-
...((agent.instructions ?? activityTask(agent.activity))
|
|
100
|
-
? { instructions: agent.instructions ?? activityTask(agent.activity) }
|
|
101
|
-
: {}),
|
|
102
|
-
})),
|
|
103
|
-
),
|
|
104
|
-
].join("\n");
|
|
105
|
-
}
|
|
106
|
-
export function boardMigrationWarning() {
|
|
107
|
-
return "Boards moved to Pi Agent Board. Install @howaboua/pi-agent-board for the same features as a separate extension. Your saved boards are preserved.";
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export function boardBriefing(
|
|
111
|
-
member: boolean,
|
|
112
|
-
population: "empty" | "populated" | "unavailable",
|
|
113
|
-
gpt = false,
|
|
114
|
-
) {
|
|
115
|
-
const state =
|
|
116
|
-
population === "populated"
|
|
117
|
-
? "Read relevant board threads."
|
|
118
|
-
: population === "empty"
|
|
119
|
-
? "Shared board available; no posts yet."
|
|
120
|
-
: "Shared board status unavailable. Check board help and retry reading when available.";
|
|
121
|
-
const setup = member
|
|
122
|
-
? "The root agent owns setup; follow your assigned task."
|
|
123
|
-
: "";
|
|
124
|
-
if (gpt)
|
|
125
|
-
return [
|
|
126
|
-
state,
|
|
127
|
-
setup,
|
|
128
|
-
"Post only work-changing ownership, decisions, findings or blockers. Reply in existing threads, link instead of repeating. Personal checkpoints belong in notes. No acknowledgements, routine status or duplicate completion reports. Use agents for assignments and urgent messages.",
|
|
129
|
-
"Channels group workstreams, threads group topics. Reuse relevant channels, not catch-alls. New channels need distinct coordination purpose, never mere task/result announcements.",
|
|
130
|
-
]
|
|
131
|
-
.filter(Boolean)
|
|
132
|
-
.join(" ");
|
|
133
|
-
return (
|
|
134
|
-
[
|
|
135
|
-
state,
|
|
136
|
-
setup,
|
|
137
|
-
"Post only new information that changes another agent's work: ownership, decisions, findings or blockers. Reply in existing threads; link rather than repeat. Keep personal checkpoints in notes. Skip acknowledgements, routine status and duplicated completion reports. Use agents for assignments and urgent messages.",
|
|
138
|
-
]
|
|
139
|
-
.filter(Boolean)
|
|
140
|
-
.join(" ") +
|
|
141
|
-
"\n\nChannels group shared workstreams; threads group topics. Reuse a relevant channel, not a catch-all. Create a channel only for a distinct coordination need, never just to announce a task or result."
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
export function attachmentMessage(
|
|
146
|
-
controllerContextAgent?: string,
|
|
147
|
-
boardAgent?: string,
|
|
148
|
-
) {
|
|
149
|
-
return {
|
|
150
|
-
customType: "herdr-agent-attachment",
|
|
151
|
-
content: [
|
|
152
|
-
...(controllerContextAgent
|
|
153
|
-
? [
|
|
154
|
-
`Controller notes/history: ${controllerContextAgent}; own context unchanged`,
|
|
155
|
-
]
|
|
156
|
-
: []),
|
|
157
|
-
...(boardAgent ? [`Shared board: ${boardAgent}`] : []),
|
|
158
|
-
].join("\n"),
|
|
159
|
-
display: true,
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function detachmentMessage(
|
|
164
|
-
controllerContextAgent?: string,
|
|
165
|
-
board?: BoardBinding,
|
|
166
|
-
) {
|
|
167
|
-
return {
|
|
168
|
-
customType: "herdr-agent-detachment",
|
|
169
|
-
content: [
|
|
170
|
-
...(controllerContextAgent
|
|
171
|
-
? [
|
|
172
|
-
`Detached controller: ${controllerContextAgent}; checkpoints read-only, history unavailable; own context unchanged`,
|
|
173
|
-
]
|
|
174
|
-
: []),
|
|
175
|
-
...(board
|
|
176
|
-
? [`Board restored: ${board.boardId} as ${board.agentName}`]
|
|
177
|
-
: []),
|
|
178
|
-
].join("\n"),
|
|
179
|
-
display: true,
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function xml(value: string): string {
|
|
184
|
-
return value
|
|
185
|
-
.replaceAll("&", "&")
|
|
186
|
-
.replaceAll("<", "<")
|
|
187
|
-
.replaceAll(">", ">")
|
|
188
|
-
.replaceAll('"', """)
|
|
189
|
-
.replaceAll("'", "'");
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
interface AgentEventLabels {
|
|
193
|
-
tab?: string;
|
|
194
|
-
workspace?: string;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export function agentSource(agent: PaneInfo, labels: AgentEventLabels) {
|
|
198
|
-
const paneName = agent.label || agent.title;
|
|
199
|
-
return {
|
|
200
|
-
pane: agent.pane_id,
|
|
201
|
-
workspace: agent.workspace_id,
|
|
202
|
-
tab: agent.tab_id,
|
|
203
|
-
...(agent.name ? { name: agent.name } : {}),
|
|
204
|
-
...(paneName ? { pane_name: paneName } : {}),
|
|
205
|
-
...(labels.workspace ? { workspace_name: labels.workspace } : {}),
|
|
206
|
-
...(labels.tab ? { tab_name: labels.tab } : {}),
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function sourceAttributes(source: Record<string, string | undefined>): string {
|
|
211
|
-
return Object.entries(source)
|
|
212
|
-
.filter((entry): entry is [string, string] => entry[1] !== undefined)
|
|
213
|
-
.map(
|
|
214
|
-
([key, value]) =>
|
|
215
|
-
`${key}="${xml(value).replaceAll("\n", " ").replaceAll("\r", " ")}"`,
|
|
216
|
-
)
|
|
217
|
-
.join(" ");
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export async function attributeAgentPrompt(
|
|
221
|
-
client: HerdrConnection,
|
|
222
|
-
message: string,
|
|
223
|
-
kind: "message" | "task",
|
|
224
|
-
): Promise<PeerMessage> {
|
|
225
|
-
const [pane, snapshot] = await Promise.all([
|
|
226
|
-
getCurrentPane(client),
|
|
227
|
-
getSnapshot(client),
|
|
228
|
-
]);
|
|
229
|
-
const workspace = snapshot.workspaces.find(
|
|
230
|
-
(workspace) => workspace.workspace_id === pane.workspace_id,
|
|
231
|
-
)?.label;
|
|
232
|
-
const tab = snapshot.tabs.find((tab) => tab.tab_id === pane.tab_id)?.label;
|
|
233
|
-
const source = {
|
|
234
|
-
kind,
|
|
235
|
-
host: hostname(),
|
|
236
|
-
session: process.env["HERDR_SESSION"] || "default",
|
|
237
|
-
...agentSource(pane, {
|
|
238
|
-
...(workspace ? { workspace } : {}),
|
|
239
|
-
...(tab ? { tab } : {}),
|
|
240
|
-
}),
|
|
241
|
-
};
|
|
242
|
-
return {
|
|
243
|
-
sender: `<herdr_sender ${sourceAttributes(source)} />`,
|
|
244
|
-
text: message,
|
|
245
|
-
...(kind === "task" ? { context: DELEGATED_WORKER_GUIDANCE } : {}),
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
interface AgentEventOptions {
|
|
250
|
-
agent: PaneInfo;
|
|
251
|
-
agentToolName: string;
|
|
252
|
-
codeMode?: boolean;
|
|
253
|
-
extensionMessages?: ExtensionMessage[];
|
|
254
|
-
blockedMessage?: string;
|
|
255
|
-
labels: AgentEventLabels;
|
|
256
|
-
machine: string;
|
|
257
|
-
machineLabel: string;
|
|
258
|
-
operatorPrefix: string;
|
|
259
|
-
record: MonitoredAgent;
|
|
260
|
-
reply?: LatestAssistant;
|
|
261
|
-
status: SettledAgentStatus | "extension-message";
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
interface AgentEventDetails {
|
|
265
|
-
extensionMessages?: ExtensionMessage[];
|
|
266
|
-
blockedOn?: string;
|
|
267
|
-
cwd?: string;
|
|
268
|
-
machine: string;
|
|
269
|
-
machineLabel?: string;
|
|
270
|
-
name?: string;
|
|
271
|
-
paneId: string;
|
|
272
|
-
response?: string;
|
|
273
|
-
state: "blocked" | "failed" | "finished" | "extension-message";
|
|
274
|
-
tab?: string;
|
|
275
|
-
task?: string;
|
|
276
|
-
workspace?: string;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
function eventExtensionMessages(
|
|
280
|
-
value: unknown,
|
|
281
|
-
): ExtensionMessage[] | undefined {
|
|
282
|
-
if (!Array.isArray(value)) return undefined;
|
|
283
|
-
const messages = value.map((message) => {
|
|
284
|
-
if (typeof message !== "object" || message === null) return undefined;
|
|
285
|
-
return readExtensionMessage({ ...message, version: 1, state: "pending" });
|
|
286
|
-
});
|
|
287
|
-
return messages.every((message) => message !== undefined)
|
|
288
|
-
? messages
|
|
289
|
-
: undefined;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function boundedVoicePrompt(prompt: string): string {
|
|
293
|
-
const bytes = new TextEncoder().encode(prompt);
|
|
294
|
-
if (bytes.byteLength <= MAX_REALTIME_VOICE_PROMPT_BYTES) return prompt;
|
|
295
|
-
return new TextDecoder()
|
|
296
|
-
.decode(bytes.subarray(0, MAX_REALTIME_VOICE_PROMPT_BYTES), {
|
|
297
|
-
stream: true,
|
|
298
|
-
})
|
|
299
|
-
.trimEnd();
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
function agentVoicePrompt(details: AgentEventDetails): string {
|
|
303
|
-
const lines = [
|
|
304
|
-
`Worker: ${details.machineLabel ?? details.machine} / ${details.name?.trim() || "unnamed"}`,
|
|
305
|
-
`State: ${details.state}`,
|
|
306
|
-
];
|
|
307
|
-
if (details.task?.trim()) lines.push(`Task:\n${details.task.trim()}`);
|
|
308
|
-
let instruction: string;
|
|
309
|
-
if (details.state === "blocked" || details.state === "extension-message") {
|
|
310
|
-
instruction =
|
|
311
|
-
details.state === "extension-message"
|
|
312
|
-
? "Briefly tell the user what this worker reports."
|
|
313
|
-
: "Briefly tell the user why this monitored worker is blocked and what attention may be required.";
|
|
314
|
-
if (details.blockedOn?.trim())
|
|
315
|
-
lines.push(`Reason:\n${details.blockedOn.trim()}`);
|
|
316
|
-
if (details.extensionMessages)
|
|
317
|
-
lines.push(
|
|
318
|
-
...details.extensionMessages.map((message) => message.message),
|
|
319
|
-
);
|
|
320
|
-
} else {
|
|
321
|
-
instruction =
|
|
322
|
-
details.state === "failed"
|
|
323
|
-
? "Briefly tell the user that this monitored worker failed and include useful detail from its report."
|
|
324
|
-
: "Briefly tell the user what this monitored worker found or completed; do not merely announce that it finished.";
|
|
325
|
-
if (details.response?.trim())
|
|
326
|
-
lines.push(`Report:\n${details.response.trim()}`);
|
|
327
|
-
}
|
|
328
|
-
return boundedVoicePrompt(`${instruction}\n\n${lines.join("\n\n")}`);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
function announceAgentEvent(
|
|
332
|
-
pi: ExtensionAPI,
|
|
333
|
-
candidateId: string,
|
|
334
|
-
prompt: string,
|
|
335
|
-
): void {
|
|
336
|
-
const id =
|
|
337
|
-
new TextEncoder().encode(candidateId).byteLength <= 160
|
|
338
|
-
? candidateId
|
|
339
|
-
: "pi-shepherdr:worker";
|
|
340
|
-
pi.events.emit(REALTIME_VOICE_PROMPT_CHANNEL, { id, active: true, prompt });
|
|
341
|
-
pi.events.emit(REALTIME_VOICE_PROMPT_CHANNEL, { id, active: false, prompt });
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
export function announcePeerMessage(
|
|
345
|
-
pi: ExtensionAPI,
|
|
346
|
-
message: PeerMessage,
|
|
347
|
-
): void {
|
|
348
|
-
const source = Object.fromEntries(
|
|
349
|
-
Array.from(
|
|
350
|
-
message.sender.matchAll(/ ([a-z_]+)="([^"]*)"/g),
|
|
351
|
-
([, key, value]) => [key, value],
|
|
352
|
-
),
|
|
353
|
-
);
|
|
354
|
-
if (source["kind"] !== "message" || message.text.startsWith("/")) return;
|
|
355
|
-
announceAgentEvent(
|
|
356
|
-
pi,
|
|
357
|
-
`pi-shepherdr:${source["host"] ?? "local"}:${source["pane"] ?? "peer"}`,
|
|
358
|
-
boundedVoicePrompt(
|
|
359
|
-
`Briefly tell the user the useful update from this peer.\n\nPeer: ${source["host"] ?? "local"} / ${source["name"] || source["pane"] || "unnamed"}\n\nUpdate:\n${message.text.trim()}`,
|
|
360
|
-
),
|
|
361
|
-
);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
function operatorCommand(operatorPrefix: string, command: string): string {
|
|
365
|
-
return `\`${operatorPrefix} ${command}\``;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
function blockedOperatorHint(
|
|
369
|
-
agentToolName: string,
|
|
370
|
-
machine: string,
|
|
371
|
-
operatorPrefix: string,
|
|
372
|
-
paneId: string,
|
|
373
|
-
): string {
|
|
374
|
-
return `Inspect first with ${operatorCommand(operatorPrefix, `agent read ${paneId} --source visible`)}; respond through ${agentToolName} with machine=${JSON.stringify(machine)}, or use ${operatorCommand(operatorPrefix, `agent send-keys ${paneId} <keys>`)} for interactive controls.`;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
function failedOperatorHint(
|
|
378
|
-
agentToolName: string,
|
|
379
|
-
machine: string,
|
|
380
|
-
operatorPrefix: string,
|
|
381
|
-
paneId: string,
|
|
382
|
-
): string {
|
|
383
|
-
return `Inspect with ${operatorCommand(operatorPrefix, `agent read ${paneId} --source recent-unwrapped --lines 80`)} and assess the failure. If this task has not already been retried and one simple corrective prompt could recover it, try once through ${agentToolName} with machine=${JSON.stringify(machine)}. If it fails again or the setup looks broken, stop retrying and tell the user.`;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
function eventDetails(value: unknown): AgentEventDetails | undefined {
|
|
387
|
-
if (typeof value !== "object" || value === null) return undefined;
|
|
388
|
-
const details = value as Record<string, unknown>;
|
|
389
|
-
if (
|
|
390
|
-
typeof details["paneId"] !== "string" ||
|
|
391
|
-
(details["machine"] !== undefined &&
|
|
392
|
-
typeof details["machine"] !== "string") ||
|
|
393
|
-
(details["state"] !== "blocked" &&
|
|
394
|
-
details["state"] !== "extension-message" &&
|
|
395
|
-
details["state"] !== "failed" &&
|
|
396
|
-
details["state"] !== "finished")
|
|
397
|
-
) {
|
|
398
|
-
return undefined;
|
|
399
|
-
}
|
|
400
|
-
const optional = (field: keyof AgentEventDetails) =>
|
|
401
|
-
typeof details[field] === "string"
|
|
402
|
-
? { [field]: details[field] as string }
|
|
403
|
-
: {};
|
|
404
|
-
const extensionMessages = eventExtensionMessages(
|
|
405
|
-
details["extensionMessages"],
|
|
406
|
-
);
|
|
407
|
-
return {
|
|
408
|
-
machine:
|
|
409
|
-
typeof details["machine"] === "string" ? details["machine"] : "local",
|
|
410
|
-
paneId: details["paneId"],
|
|
411
|
-
state: details["state"],
|
|
412
|
-
...(extensionMessages ? { extensionMessages } : {}),
|
|
413
|
-
...optional("blockedOn"),
|
|
414
|
-
...optional("cwd"),
|
|
415
|
-
...optional("machineLabel"),
|
|
416
|
-
...optional("name"),
|
|
417
|
-
...optional("response"),
|
|
418
|
-
...optional("tab"),
|
|
419
|
-
...optional("task"),
|
|
420
|
-
...optional("workspace"),
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
function agentEvent(options: AgentEventOptions): {
|
|
425
|
-
content: string;
|
|
426
|
-
details: AgentEventDetails;
|
|
427
|
-
} {
|
|
428
|
-
const {
|
|
429
|
-
agent,
|
|
430
|
-
agentToolName,
|
|
431
|
-
codeMode,
|
|
432
|
-
extensionMessages,
|
|
433
|
-
blockedMessage,
|
|
434
|
-
labels,
|
|
435
|
-
machine,
|
|
436
|
-
machineLabel,
|
|
437
|
-
operatorPrefix,
|
|
438
|
-
record,
|
|
439
|
-
reply,
|
|
440
|
-
status,
|
|
441
|
-
} = options;
|
|
442
|
-
const task = activityTask(record.activity);
|
|
443
|
-
const blocked = status === "blocked";
|
|
444
|
-
const notification = status === "extension-message";
|
|
445
|
-
const failed = !blocked && reply?.stopReason === "error";
|
|
446
|
-
const cwd = agent.foreground_cwd ?? agent.cwd ?? record.cwd;
|
|
447
|
-
const tag = notification
|
|
448
|
-
? "herdr_agent_extension_message"
|
|
449
|
-
: blocked
|
|
450
|
-
? "herdr_agent_blocked"
|
|
451
|
-
: failed
|
|
452
|
-
? "herdr_agent_failed"
|
|
453
|
-
: "herdr_agent_result";
|
|
454
|
-
const attributes = sourceAttributes({
|
|
455
|
-
machine,
|
|
456
|
-
machine_name: machineLabel !== machine ? machineLabel : undefined,
|
|
457
|
-
...agentSource(agent, labels),
|
|
458
|
-
name: agent.name ?? record.name,
|
|
459
|
-
directory: cwd,
|
|
460
|
-
});
|
|
461
|
-
const lines = [`<${tag} ${attributes}>`];
|
|
462
|
-
if (task && !notification) lines.push(`<task>${xml(task)}</task>`);
|
|
463
|
-
if (blockedMessage) {
|
|
464
|
-
lines.push(`<blocked_on>${xml(blockedMessage)}</blocked_on>`);
|
|
465
|
-
}
|
|
466
|
-
for (const message of extensionMessages ?? []) {
|
|
467
|
-
lines.push(message.message);
|
|
468
|
-
if (message.reply !== undefined) {
|
|
469
|
-
const request = {
|
|
470
|
-
action: "reply",
|
|
471
|
-
machine,
|
|
472
|
-
target: agent.pane_id,
|
|
473
|
-
extension: message.extension,
|
|
474
|
-
id: message.id,
|
|
475
|
-
message: message.reply,
|
|
476
|
-
};
|
|
477
|
-
const call = codeMode
|
|
478
|
-
? `await tools.${agentToolName}(${JSON.stringify(request)})`
|
|
479
|
-
: `${agentToolName} ${JSON.stringify(request)}`;
|
|
480
|
-
lines.push(`Reply: ${call}`);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
if (blocked) {
|
|
484
|
-
lines.push(
|
|
485
|
-
`<operator_hint>${xml(blockedOperatorHint(agentToolName, machine, operatorPrefix, agent.pane_id))}</operator_hint>`,
|
|
486
|
-
);
|
|
487
|
-
}
|
|
488
|
-
if (failed) {
|
|
489
|
-
lines.push(
|
|
490
|
-
`<operator_hint>${xml(failedOperatorHint(agentToolName, machine, operatorPrefix, agent.pane_id))}</operator_hint>`,
|
|
491
|
-
);
|
|
492
|
-
}
|
|
493
|
-
if (reply) lines.push(`<response>${xml(reply.text)}</response>`);
|
|
494
|
-
lines.push(`</${tag}>`);
|
|
495
|
-
|
|
496
|
-
return {
|
|
497
|
-
content: lines.join("\n"),
|
|
498
|
-
details: {
|
|
499
|
-
machine,
|
|
500
|
-
machineLabel,
|
|
501
|
-
paneId: agent.pane_id,
|
|
502
|
-
state: notification
|
|
503
|
-
? "extension-message"
|
|
504
|
-
: blocked
|
|
505
|
-
? "blocked"
|
|
506
|
-
: failed
|
|
507
|
-
? "failed"
|
|
508
|
-
: "finished",
|
|
509
|
-
...(extensionMessages ? { extensionMessages } : {}),
|
|
510
|
-
...(agent.name || record.name ? { name: agent.name || record.name } : {}),
|
|
511
|
-
...(cwd ? { cwd } : {}),
|
|
512
|
-
...(labels.workspace ? { workspace: labels.workspace } : {}),
|
|
513
|
-
...(labels.tab ? { tab: labels.tab } : {}),
|
|
514
|
-
...(task && !notification ? { task } : {}),
|
|
515
|
-
...(blockedMessage ? { blockedOn: blockedMessage } : {}),
|
|
516
|
-
...(reply ? { response: reply.text } : {}),
|
|
517
|
-
},
|
|
518
|
-
};
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
export function injectAgentEvent(
|
|
522
|
-
pi: ExtensionAPI,
|
|
523
|
-
_ctx: ExtensionContext,
|
|
524
|
-
options: AgentEventOptions,
|
|
525
|
-
): void {
|
|
526
|
-
const tools = pi.getActiveTools();
|
|
527
|
-
const message = agentEvent({
|
|
528
|
-
...options,
|
|
529
|
-
codeMode:
|
|
530
|
-
tools.includes("exec") ||
|
|
531
|
-
tools.includes("code") ||
|
|
532
|
-
tools.includes("notebook"),
|
|
533
|
-
});
|
|
534
|
-
const delivery = { triggerTurn: true, deliverAs: "steer" as const };
|
|
535
|
-
sendPolicyMessage(
|
|
536
|
-
pi,
|
|
537
|
-
{
|
|
538
|
-
customType: AGENT_EVENT_MESSAGE_TYPE,
|
|
539
|
-
content: message.content,
|
|
540
|
-
details: message.details,
|
|
541
|
-
display: true,
|
|
542
|
-
},
|
|
543
|
-
delivery,
|
|
544
|
-
);
|
|
545
|
-
announceAgentEvent(
|
|
546
|
-
pi,
|
|
547
|
-
`pi-shepherdr:${message.details.machine}:${message.details.paneId}`,
|
|
548
|
-
agentVoicePrompt(message.details),
|
|
549
|
-
);
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
export function registerAgentEventRenderer(pi: ExtensionAPI): void {
|
|
553
|
-
pi.registerMessageRenderer<AgentEventDetails>(
|
|
554
|
-
AGENT_EVENT_MESSAGE_TYPE,
|
|
555
|
-
(message, { expanded, outputPad }, theme) => {
|
|
556
|
-
const details = eventDetails(message.details);
|
|
557
|
-
if (!details) {
|
|
558
|
-
const box = new Box(outputPad, 1, (value) =>
|
|
559
|
-
theme.bg("customMessageBg", value),
|
|
560
|
-
);
|
|
561
|
-
box.addChild(
|
|
562
|
-
new Text(
|
|
563
|
-
typeof message.content === "string"
|
|
564
|
-
? message.content
|
|
565
|
-
: "Herdr agent event unavailable",
|
|
566
|
-
0,
|
|
567
|
-
0,
|
|
568
|
-
),
|
|
569
|
-
);
|
|
570
|
-
return box;
|
|
571
|
-
}
|
|
572
|
-
const blocked = details?.state === "blocked";
|
|
573
|
-
const failed = details?.state === "failed";
|
|
574
|
-
const notification = details.state === "extension-message";
|
|
575
|
-
const agentIdentity = details?.name
|
|
576
|
-
? `${details.name} (${details.paneId})`
|
|
577
|
-
: (details?.paneId ?? "unknown");
|
|
578
|
-
const identity = `${details.machineLabel ?? details.machine} / ${agentIdentity}`;
|
|
579
|
-
const title = theme.fg(
|
|
580
|
-
blocked || failed ? "error" : "success",
|
|
581
|
-
`Herdr agent ${identity} · ${notification ? "extension message pending" : blocked ? "blocked" : failed ? "failed" : "finished"}`,
|
|
582
|
-
);
|
|
583
|
-
const location = [details?.workspace, details?.tab]
|
|
584
|
-
.filter(Boolean)
|
|
585
|
-
.join(" / ");
|
|
586
|
-
const metadata = [location, details?.cwd].filter(Boolean).join(" · ");
|
|
587
|
-
const box = new Box(outputPad, 1, (value) =>
|
|
588
|
-
theme.bg("customMessageBg", value),
|
|
589
|
-
);
|
|
590
|
-
box.addChild(new Text(title, 0, 0));
|
|
591
|
-
if (metadata) box.addChild(new Text(theme.fg("dim", metadata), 0, 0));
|
|
592
|
-
if (blocked || notification) {
|
|
593
|
-
box.addChild(new Spacer(1));
|
|
594
|
-
box.addChild(
|
|
595
|
-
new Text(
|
|
596
|
-
theme.fg(
|
|
597
|
-
"warning",
|
|
598
|
-
details?.blockedOn ??
|
|
599
|
-
(notification
|
|
600
|
-
? "Extension message pending"
|
|
601
|
-
: "Agent needs input or approval"),
|
|
602
|
-
),
|
|
603
|
-
0,
|
|
604
|
-
0,
|
|
605
|
-
),
|
|
606
|
-
);
|
|
607
|
-
if (details.extensionMessages && expanded) {
|
|
608
|
-
for (const message of details.extensionMessages) {
|
|
609
|
-
box.addChild(new Spacer(1));
|
|
610
|
-
box.addChild(
|
|
611
|
-
new Markdown(message.message, 0, 0, getMarkdownTheme()),
|
|
612
|
-
);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
if (expanded && details?.task) {
|
|
617
|
-
box.addChild(new Spacer(1));
|
|
618
|
-
box.addChild(new Text(theme.fg("muted", "Task"), 0, 0));
|
|
619
|
-
box.addChild(new Markdown(details.task, 0, 0, getMarkdownTheme()));
|
|
620
|
-
}
|
|
621
|
-
if (details?.response) {
|
|
622
|
-
box.addChild(new Spacer(1));
|
|
623
|
-
box.addChild(new Text(theme.fg("muted", "Response"), 0, 0));
|
|
624
|
-
box.addChild(new Markdown(details.response, 0, 0, getMarkdownTheme()));
|
|
625
|
-
}
|
|
626
|
-
return box;
|
|
627
|
-
},
|
|
628
|
-
);
|
|
629
|
-
}
|