@howaboua/pi-shepherdr 0.1.0 → 0.1.1
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/package.json +1 -1
- package/src/messages.ts +78 -2
package/package.json
CHANGED
package/src/messages.ts
CHANGED
|
@@ -13,6 +13,9 @@ import type {
|
|
|
13
13
|
} from "./types.js";
|
|
14
14
|
|
|
15
15
|
const AGENT_EVENT_MESSAGE_TYPE = "herdr-agent-event";
|
|
16
|
+
const REALTIME_VOICE_PROMPT_CHANNEL =
|
|
17
|
+
"@howaboua/pi-codex-conversion/realtime-voice-prompt/v1";
|
|
18
|
+
const MAX_REALTIME_VOICE_PROMPT_BYTES = 4 * 1_024;
|
|
16
19
|
|
|
17
20
|
function xml(value: string): string {
|
|
18
21
|
return value
|
|
@@ -49,10 +52,77 @@ interface AgentEventDetails {
|
|
|
49
52
|
workspace?: string;
|
|
50
53
|
}
|
|
51
54
|
|
|
52
|
-
function
|
|
55
|
+
function boundedVoicePrompt(prompt: string, truncationNotice: string): string {
|
|
56
|
+
const encoder = new TextEncoder();
|
|
57
|
+
const bytes = encoder.encode(prompt);
|
|
58
|
+
if (bytes.byteLength <= MAX_REALTIME_VOICE_PROMPT_BYTES) return prompt;
|
|
59
|
+
|
|
60
|
+
const suffix = `…\n\n${truncationNotice}`;
|
|
61
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
62
|
+
let end = MAX_REALTIME_VOICE_PROMPT_BYTES - encoder.encode(suffix).byteLength;
|
|
63
|
+
while (end > 0) {
|
|
64
|
+
try {
|
|
65
|
+
return `${decoder.decode(bytes.subarray(0, end)).trimEnd()}${suffix}`;
|
|
66
|
+
} catch {
|
|
67
|
+
end -= 1;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return truncationNotice;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function agentVoicePrompt(details: AgentEventDetails): string {
|
|
74
|
+
const lines = [
|
|
75
|
+
`Worker: ${details.name?.trim() || "unnamed"}`,
|
|
76
|
+
`State: ${details.state}`,
|
|
77
|
+
];
|
|
78
|
+
if (details.task?.trim()) lines.push(`Task:\n${details.task.trim()}`);
|
|
79
|
+
let instruction: string;
|
|
80
|
+
let truncationNotice: string;
|
|
81
|
+
if (details.state === "blocked") {
|
|
82
|
+
instruction =
|
|
83
|
+
"Briefly tell the user why this monitored worker is blocked and what attention may be required.";
|
|
84
|
+
truncationNotice =
|
|
85
|
+
"The blocked worker details above were truncated. Tell the user and ask whether they would like the rest.";
|
|
86
|
+
if (details.blockedOn?.trim())
|
|
87
|
+
lines.push(`Reason:\n${details.blockedOn.trim()}`);
|
|
88
|
+
} else {
|
|
89
|
+
instruction =
|
|
90
|
+
details.state === "failed"
|
|
91
|
+
? "Briefly tell the user that this monitored worker failed and include useful detail from its report."
|
|
92
|
+
: "Briefly tell the user what this monitored worker found or completed; do not merely announce that it finished.";
|
|
93
|
+
truncationNotice =
|
|
94
|
+
"The worker report above was truncated. Tell the user and ask whether they would like the rest.";
|
|
95
|
+
if (details.response?.trim())
|
|
96
|
+
lines.push(`Report:\n${details.response.trim()}`);
|
|
97
|
+
}
|
|
98
|
+
return boundedVoicePrompt(
|
|
99
|
+
`${instruction}\n\n${lines.join("\n\n")}`,
|
|
100
|
+
truncationNotice,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function announceAgentEvent(
|
|
105
|
+
pi: ExtensionAPI,
|
|
106
|
+
details: AgentEventDetails,
|
|
107
|
+
): void {
|
|
108
|
+
const candidateId = `pi-shepherdr:${details.paneId}`;
|
|
109
|
+
const id =
|
|
110
|
+
new TextEncoder().encode(candidateId).byteLength <= 160
|
|
111
|
+
? candidateId
|
|
112
|
+
: "pi-shepherdr:worker";
|
|
113
|
+
const prompt = agentVoicePrompt(details);
|
|
114
|
+
pi.events.emit(REALTIME_VOICE_PROMPT_CHANNEL, { id, active: true, prompt });
|
|
115
|
+
pi.events.emit(REALTIME_VOICE_PROMPT_CHANNEL, { id, active: false, prompt });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function blockedOperatorHint(paneId: string): string {
|
|
53
119
|
return `Inspect first with \`herdr agent read ${paneId} --source visible\`; respond with \`herdr agent prompt ${paneId} "<text>"\` or use \`herdr agent send-keys ${paneId} <keys>\` for interactive controls.`;
|
|
54
120
|
}
|
|
55
121
|
|
|
122
|
+
function failedOperatorHint(paneId: string): string {
|
|
123
|
+
return `Inspect with \`herdr 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 with \`herdr agent prompt ${paneId} "<text>"\`. If it fails again or the setup looks broken, stop retrying and tell the user.`;
|
|
124
|
+
}
|
|
125
|
+
|
|
56
126
|
function eventDetails(value: unknown): AgentEventDetails | undefined {
|
|
57
127
|
if (typeof value !== "object" || value === null) return undefined;
|
|
58
128
|
const details = value as Record<string, unknown>;
|
|
@@ -109,7 +179,12 @@ function agentEvent(options: AgentEventOptions): {
|
|
|
109
179
|
}
|
|
110
180
|
if (blocked) {
|
|
111
181
|
lines.push(
|
|
112
|
-
`<operator_hint>${xml(
|
|
182
|
+
`<operator_hint>${xml(blockedOperatorHint(agent.pane_id))}</operator_hint>`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (failed) {
|
|
186
|
+
lines.push(
|
|
187
|
+
`<operator_hint>${xml(failedOperatorHint(agent.pane_id))}</operator_hint>`,
|
|
113
188
|
);
|
|
114
189
|
}
|
|
115
190
|
if (reply) lines.push(`<response>${xml(reply.text)}</response>`);
|
|
@@ -149,6 +224,7 @@ export function injectAgentEvent(
|
|
|
149
224
|
},
|
|
150
225
|
delivery,
|
|
151
226
|
);
|
|
227
|
+
announceAgentEvent(pi, message.details);
|
|
152
228
|
}
|
|
153
229
|
|
|
154
230
|
export function registerAgentEventRenderer(pi: ExtensionAPI): void {
|