@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/session-reader.ts
CHANGED
|
@@ -1,342 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readSessionView } from "./remote/shepherdr-session.mjs";
|
|
2
2
|
import { watchSessions } from "./remote/shepherdr-session-watch.mjs";
|
|
3
|
-
import type {
|
|
4
|
-
AskResult,
|
|
5
|
-
LatestAssistant,
|
|
6
|
-
LatestInput,
|
|
7
|
-
PendingAsk,
|
|
8
|
-
SessionView,
|
|
9
|
-
} from "./types.js";
|
|
10
|
-
|
|
11
|
-
const READ_CHUNK_BYTES = 64 * 1024;
|
|
12
|
-
|
|
13
|
-
function record(value: unknown): Record<string, unknown> | undefined {
|
|
14
|
-
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
15
|
-
? (value as Record<string, unknown>)
|
|
16
|
-
: undefined;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function assistantFromMessage(
|
|
20
|
-
id: string,
|
|
21
|
-
message: Record<string, unknown>,
|
|
22
|
-
): LatestAssistant | undefined {
|
|
23
|
-
if (message["role"] !== "assistant") return undefined;
|
|
24
|
-
const text: string[] = [];
|
|
25
|
-
if (typeof message["content"] === "string") {
|
|
26
|
-
text.push(message["content"]);
|
|
27
|
-
} else if (Array.isArray(message["content"])) {
|
|
28
|
-
for (const value of message["content"]) {
|
|
29
|
-
const part = record(value);
|
|
30
|
-
if (part?.["type"] === "text" && typeof part["text"] === "string") {
|
|
31
|
-
text.push(part["text"]);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
const joined = text.join("");
|
|
36
|
-
const stopReason =
|
|
37
|
-
typeof message["stopReason"] === "string"
|
|
38
|
-
? message["stopReason"]
|
|
39
|
-
: undefined;
|
|
40
|
-
if (!joined && stopReason !== "error") return undefined;
|
|
41
|
-
return {
|
|
42
|
-
id,
|
|
43
|
-
text: joined,
|
|
44
|
-
...(stopReason ? { stopReason } : {}),
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function inputFromMessage(
|
|
49
|
-
id: string,
|
|
50
|
-
message: Record<string, unknown>,
|
|
51
|
-
): LatestInput | undefined {
|
|
52
|
-
if (message["role"] !== "user") return undefined;
|
|
53
|
-
const text: string[] = [];
|
|
54
|
-
if (typeof message["content"] === "string") {
|
|
55
|
-
text.push(message["content"]);
|
|
56
|
-
} else if (Array.isArray(message["content"])) {
|
|
57
|
-
for (const value of message["content"]) {
|
|
58
|
-
const part = record(value);
|
|
59
|
-
if (part?.["type"] === "text" && typeof part["text"] === "string") {
|
|
60
|
-
text.push(part["text"]);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const joined = text.join("");
|
|
65
|
-
return joined ? { id, text: joined } : undefined;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function askChoice(
|
|
69
|
-
value: unknown,
|
|
70
|
-
): PendingAsk["prompts"][number]["choices"][number] | undefined {
|
|
71
|
-
const choice = record(value);
|
|
72
|
-
if (!choice || typeof choice["label"] !== "string") return undefined;
|
|
73
|
-
return {
|
|
74
|
-
label: choice["label"],
|
|
75
|
-
...(typeof choice["description"] === "string"
|
|
76
|
-
? { description: choice["description"] }
|
|
77
|
-
: {}),
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function askPrompt(value: unknown): PendingAsk["prompts"][number] | undefined {
|
|
82
|
-
const prompt = record(value);
|
|
83
|
-
if (!prompt || typeof prompt["title"] !== "string") return undefined;
|
|
84
|
-
return {
|
|
85
|
-
title: prompt["title"],
|
|
86
|
-
multiple: prompt["multiple"] === true,
|
|
87
|
-
choices: (Array.isArray(prompt["choices"]) ? prompt["choices"] : [])
|
|
88
|
-
.map(askChoice)
|
|
89
|
-
.filter((choice): choice is NonNullable<typeof choice> =>
|
|
90
|
-
Boolean(choice),
|
|
91
|
-
),
|
|
92
|
-
...(typeof prompt["body"] === "string" ? { body: prompt["body"] } : {}),
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function askCall(value: unknown): PendingAsk | undefined {
|
|
97
|
-
const part = record(value);
|
|
98
|
-
if (
|
|
99
|
-
!part ||
|
|
100
|
-
part["type"] !== "toolCall" ||
|
|
101
|
-
part["name"] !== "ask" ||
|
|
102
|
-
typeof part["id"] !== "string"
|
|
103
|
-
) {
|
|
104
|
-
return undefined;
|
|
105
|
-
}
|
|
106
|
-
return askFromInput(part["id"], part["arguments"]);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function askFromInput(
|
|
110
|
-
toolCallId: string,
|
|
111
|
-
input: unknown,
|
|
112
|
-
): PendingAsk | undefined {
|
|
113
|
-
const args = record(input);
|
|
114
|
-
if (!args) return undefined;
|
|
115
|
-
const prompts = (Array.isArray(args["prompts"]) ? args["prompts"] : [])
|
|
116
|
-
.map(askPrompt)
|
|
117
|
-
.filter((prompt): prompt is NonNullable<typeof prompt> => Boolean(prompt));
|
|
118
|
-
if (prompts.length === 0) return undefined;
|
|
119
|
-
return {
|
|
120
|
-
toolCallId,
|
|
121
|
-
handoff: args["handoff"] === true,
|
|
122
|
-
...(args["delivery"] === "steer" ? { delivery: "steer" as const } : {}),
|
|
123
|
-
prompts,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function resolvedToolCallId(
|
|
128
|
-
message: Record<string, unknown>,
|
|
129
|
-
): string | undefined {
|
|
130
|
-
if (message["role"] !== "toolResult" && message["role"] !== "tool") {
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
const value = message["toolCallId"] ?? message["tool_call_id"];
|
|
134
|
-
return typeof value === "string" ? value : undefined;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function askResult(
|
|
138
|
-
message: Record<string, unknown>,
|
|
139
|
-
): [string, AskResult] | undefined {
|
|
140
|
-
if (message["role"] !== "toolResult" || message["toolName"] !== "ask") {
|
|
141
|
-
return undefined;
|
|
142
|
-
}
|
|
143
|
-
const id = resolvedToolCallId(message);
|
|
144
|
-
if (!id) return undefined;
|
|
145
|
-
if (message["isError"] === true) return [id, { status: "rejected" }];
|
|
146
|
-
if (message["isError"] !== false) return [id, { status: "unknown" }];
|
|
147
|
-
const details = record(message["details"]);
|
|
148
|
-
if (details?.["pending"] === true) return undefined;
|
|
149
|
-
if (details?.["dismissed"] === true) return [id, { status: "rejected" }];
|
|
150
|
-
const rawResponses = details?.["responses"];
|
|
151
|
-
const rawResponseCount = Array.isArray(rawResponses)
|
|
152
|
-
? rawResponses.length
|
|
153
|
-
: -1;
|
|
154
|
-
const responses = Array.isArray(rawResponses)
|
|
155
|
-
? rawResponses
|
|
156
|
-
.map((value) => {
|
|
157
|
-
const response = record(value);
|
|
158
|
-
if (
|
|
159
|
-
!response ||
|
|
160
|
-
typeof response["id"] !== "string" ||
|
|
161
|
-
!Array.isArray(response["selections"]) ||
|
|
162
|
-
!response["selections"].every(
|
|
163
|
-
(selection) => typeof selection === "string",
|
|
164
|
-
)
|
|
165
|
-
) {
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
return {
|
|
169
|
-
id: response["id"],
|
|
170
|
-
selections: response["selections"] as string[],
|
|
171
|
-
...(typeof response["comment"] === "string"
|
|
172
|
-
? { comment: response["comment"] }
|
|
173
|
-
: {}),
|
|
174
|
-
};
|
|
175
|
-
})
|
|
176
|
-
.filter((response) => response !== undefined)
|
|
177
|
-
: undefined;
|
|
178
|
-
return [
|
|
179
|
-
id,
|
|
180
|
-
{
|
|
181
|
-
status:
|
|
182
|
-
responses &&
|
|
183
|
-
responses.length > 0 &&
|
|
184
|
-
responses.length === rawResponseCount
|
|
185
|
-
? "accepted"
|
|
186
|
-
: "unknown",
|
|
187
|
-
...(responses && responses.length === rawResponseCount
|
|
188
|
-
? { responses }
|
|
189
|
-
: {}),
|
|
190
|
-
},
|
|
191
|
-
];
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async function readSessionView(
|
|
195
|
-
path: string,
|
|
196
|
-
size: number,
|
|
197
|
-
): Promise<SessionView> {
|
|
198
|
-
const file = await open(path, "r");
|
|
199
|
-
let targetId: string | undefined;
|
|
200
|
-
let assistant: LatestAssistant | undefined;
|
|
201
|
-
let assistantDepth: number | undefined;
|
|
202
|
-
let ask: PendingAsk | undefined;
|
|
203
|
-
const askResults = new Map<string, AskResult>();
|
|
204
|
-
let depth = 0;
|
|
205
|
-
let input: LatestInput | undefined;
|
|
206
|
-
let inputDepth: number | undefined;
|
|
207
|
-
const resolved = new Set<string>();
|
|
208
|
-
const result = (): SessionView => ({
|
|
209
|
-
...(assistant ? { assistant } : {}),
|
|
210
|
-
...(ask ? { ask } : {}),
|
|
211
|
-
...(askResults.size > 0
|
|
212
|
-
? { askResults: Object.fromEntries(askResults) }
|
|
213
|
-
: {}),
|
|
214
|
-
...(input ? { input } : {}),
|
|
215
|
-
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
216
|
-
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
217
|
-
: {}),
|
|
218
|
-
});
|
|
219
|
-
const inspect = (line: Buffer): boolean => {
|
|
220
|
-
if (line.length === 0) return false;
|
|
221
|
-
let entry: Record<string, unknown>;
|
|
222
|
-
try {
|
|
223
|
-
entry = JSON.parse(line.toString("utf8")) as Record<string, unknown>;
|
|
224
|
-
} catch {
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
const id = entry["id"];
|
|
228
|
-
if (typeof id !== "string") return false;
|
|
229
|
-
targetId ??= id;
|
|
230
|
-
if (id !== targetId) return false;
|
|
231
|
-
const currentDepth = depth;
|
|
232
|
-
depth += 1;
|
|
233
|
-
if (entry["type"] === "custom" && entry["customType"] === "pi-ask-active") {
|
|
234
|
-
const update = record(entry["data"]);
|
|
235
|
-
if (update?.["version"] === 1 && typeof update["id"] === "string") {
|
|
236
|
-
const callId = update["id"];
|
|
237
|
-
if (update["state"] === "closed") resolved.add(callId);
|
|
238
|
-
else if (
|
|
239
|
-
update["state"] === "active" &&
|
|
240
|
-
!ask &&
|
|
241
|
-
!resolved.has(callId)
|
|
242
|
-
) {
|
|
243
|
-
ask = askFromInput(callId, update);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
if (
|
|
248
|
-
entry["type"] === "custom" &&
|
|
249
|
-
entry["customType"] === "pi-ask-pending"
|
|
250
|
-
) {
|
|
251
|
-
const update = record(entry["data"]);
|
|
252
|
-
if (
|
|
253
|
-
update?.["version"] === 1 &&
|
|
254
|
-
update["state"] === "closed" &&
|
|
255
|
-
typeof update["id"] === "string"
|
|
256
|
-
) {
|
|
257
|
-
resolved.add(update["id"]);
|
|
258
|
-
const result = askResult({
|
|
259
|
-
role: "toolResult",
|
|
260
|
-
toolName: "ask",
|
|
261
|
-
toolCallId: update["id"],
|
|
262
|
-
isError: false,
|
|
263
|
-
details: update,
|
|
264
|
-
});
|
|
265
|
-
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
if (
|
|
269
|
-
!input &&
|
|
270
|
-
entry["type"] === "custom_message" &&
|
|
271
|
-
entry["customType"] === "herdr-agent-message" &&
|
|
272
|
-
typeof entry["content"] === "string" &&
|
|
273
|
-
entry["content"]
|
|
274
|
-
) {
|
|
275
|
-
input = { id, text: entry["content"] };
|
|
276
|
-
inputDepth = currentDepth;
|
|
277
|
-
}
|
|
278
|
-
const message = record(entry["message"]);
|
|
279
|
-
if (message) {
|
|
280
|
-
const result = askResult(message);
|
|
281
|
-
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
282
|
-
const resolvedId = resolvedToolCallId(message);
|
|
283
|
-
if (resolvedId && record(message["details"])?.["pending"] !== true)
|
|
284
|
-
resolved.add(resolvedId);
|
|
285
|
-
if (!assistant) {
|
|
286
|
-
assistant = assistantFromMessage(id, message);
|
|
287
|
-
if (assistant) assistantDepth = currentDepth;
|
|
288
|
-
}
|
|
289
|
-
if (!input) {
|
|
290
|
-
input = inputFromMessage(id, message);
|
|
291
|
-
if (input) inputDepth = currentDepth;
|
|
292
|
-
}
|
|
293
|
-
if (
|
|
294
|
-
!ask &&
|
|
295
|
-
message["role"] === "assistant" &&
|
|
296
|
-
Array.isArray(message["content"])
|
|
297
|
-
) {
|
|
298
|
-
ask = [...message["content"]]
|
|
299
|
-
.reverse()
|
|
300
|
-
.map(askCall)
|
|
301
|
-
.find(
|
|
302
|
-
(candidate) =>
|
|
303
|
-
candidate !== undefined &&
|
|
304
|
-
candidate.delivery !== "steer" &&
|
|
305
|
-
!resolved.has(candidate.toolCallId),
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
const parentId = entry["parentId"];
|
|
310
|
-
if (typeof parentId !== "string") return true;
|
|
311
|
-
targetId = parentId;
|
|
312
|
-
return false;
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
try {
|
|
316
|
-
let position = size;
|
|
317
|
-
let partial = Buffer.alloc(0);
|
|
318
|
-
while (position > 0) {
|
|
319
|
-
const length = Math.min(READ_CHUNK_BYTES, position);
|
|
320
|
-
position -= length;
|
|
321
|
-
const chunk = Buffer.allocUnsafe(length);
|
|
322
|
-
const { bytesRead } = await file.read(chunk, 0, length, position);
|
|
323
|
-
const data = Buffer.concat([chunk.subarray(0, bytesRead), partial]);
|
|
324
|
-
let lineEnd = data.length;
|
|
325
|
-
for (let index = data.length - 1; index >= 0; index -= 1) {
|
|
326
|
-
if (data[index] !== 0x0a) continue;
|
|
327
|
-
if (inspect(data.subarray(index + 1, lineEnd))) {
|
|
328
|
-
return result();
|
|
329
|
-
}
|
|
330
|
-
lineEnd = index;
|
|
331
|
-
}
|
|
332
|
-
partial = data.subarray(0, lineEnd);
|
|
333
|
-
}
|
|
334
|
-
inspect(partial);
|
|
335
|
-
return result();
|
|
336
|
-
} finally {
|
|
337
|
-
await file.close();
|
|
338
|
-
}
|
|
339
|
-
}
|
|
3
|
+
import type { LatestAssistant, SessionView } from "./types.js";
|
|
340
4
|
|
|
341
5
|
export class SessionReader {
|
|
342
6
|
async watch(
|
|
@@ -347,38 +11,13 @@ export class SessionReader {
|
|
|
347
11
|
): Promise<() => void> {
|
|
348
12
|
return watchSessions(paths, onChange, onError, signal);
|
|
349
13
|
}
|
|
350
|
-
private readonly cache = new Map<
|
|
351
|
-
string,
|
|
352
|
-
{ mtimeMs: number; result: SessionView; size: number }
|
|
353
|
-
>();
|
|
354
|
-
|
|
355
14
|
async view(path?: string): Promise<SessionView> {
|
|
356
|
-
|
|
357
|
-
let metadata;
|
|
358
|
-
try {
|
|
359
|
-
metadata = await stat(path);
|
|
360
|
-
} catch (error) {
|
|
361
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
|
362
|
-
throw error;
|
|
363
|
-
}
|
|
364
|
-
const cached = this.cache.get(path);
|
|
365
|
-
if (cached?.size === metadata.size && cached.mtimeMs === metadata.mtimeMs) {
|
|
366
|
-
return cached.result;
|
|
367
|
-
}
|
|
368
|
-
const result = await readSessionView(path, metadata.size);
|
|
369
|
-
this.cache.set(path, {
|
|
370
|
-
mtimeMs: metadata.mtimeMs,
|
|
371
|
-
result,
|
|
372
|
-
size: metadata.size,
|
|
373
|
-
});
|
|
374
|
-
return result;
|
|
15
|
+
return readSessionView(path);
|
|
375
16
|
}
|
|
376
|
-
|
|
377
17
|
async latest(path?: string): Promise<LatestAssistant | undefined> {
|
|
378
18
|
return (await this.view(path)).assistant;
|
|
379
19
|
}
|
|
380
20
|
}
|
|
381
|
-
|
|
382
21
|
export interface AssistantReader {
|
|
383
22
|
watch(
|
|
384
23
|
paths: string[],
|
package/src/settlement.ts
CHANGED
|
@@ -14,10 +14,11 @@ import { injectAgentEvent } from "./messages.js";
|
|
|
14
14
|
import type { MonitorState, WorkAttempt } from "./monitor-state.js";
|
|
15
15
|
import { type AssistantReader, SessionReader } from "./session-reader.js";
|
|
16
16
|
import type {
|
|
17
|
+
ExtensionMessage,
|
|
18
|
+
ExtensionReply,
|
|
17
19
|
LatestAssistant,
|
|
18
20
|
MonitoredAgent,
|
|
19
21
|
PaneInfo,
|
|
20
|
-
PendingAsk,
|
|
21
22
|
SessionView,
|
|
22
23
|
SettledAgentStatus,
|
|
23
24
|
} from "./types.js";
|
|
@@ -29,7 +30,7 @@ interface SettlementLabels {
|
|
|
29
30
|
|
|
30
31
|
interface CollectedSettlement {
|
|
31
32
|
agent: PaneInfo;
|
|
32
|
-
|
|
33
|
+
extensionMessages?: ExtensionMessage[];
|
|
33
34
|
labels: SettlementLabels;
|
|
34
35
|
reply?: LatestAssistant;
|
|
35
36
|
status: SettledAgentStatus;
|
|
@@ -45,7 +46,7 @@ export interface ClaimedSettlement extends CollectedSettlement {
|
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
interface SettlementClaim {
|
|
48
|
-
|
|
49
|
+
replyingTo?: ExtensionReply;
|
|
49
50
|
cleanup(): void;
|
|
50
51
|
reject(error: Error): void;
|
|
51
52
|
resolve(settlement: ClaimedSettlement): void;
|
|
@@ -169,7 +170,9 @@ export class SettlementCollector {
|
|
|
169
170
|
...(tab ? { tab } : {}),
|
|
170
171
|
},
|
|
171
172
|
...(view.assistant ? { reply: view.assistant } : {}),
|
|
172
|
-
...(
|
|
173
|
+
...(view.extensionMessages
|
|
174
|
+
? { extensionMessages: view.extensionMessages }
|
|
175
|
+
: {}),
|
|
173
176
|
session: view,
|
|
174
177
|
status,
|
|
175
178
|
};
|
|
@@ -229,12 +232,11 @@ export class SettlementReporter {
|
|
|
229
232
|
claim(
|
|
230
233
|
attempt: WorkAttempt,
|
|
231
234
|
signal: AbortSignal,
|
|
232
|
-
|
|
233
|
-
): Promise<ClaimedSettlement> {
|
|
235
|
+
replyingTo?: ExtensionReply,
|
|
236
|
+
): Promise<ClaimedSettlement> | undefined {
|
|
234
237
|
signal.throwIfAborted();
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
+
// Updates share the worker's current settlement, without stealing its waiter.
|
|
239
|
+
if (this.claims.has(attempt.attemptId)) return;
|
|
238
240
|
return new Promise<ClaimedSettlement>((resolve, reject) => {
|
|
239
241
|
const abort = () => {
|
|
240
242
|
const claim = this.claims.get(attempt.attemptId);
|
|
@@ -248,7 +250,7 @@ export class SettlementReporter {
|
|
|
248
250
|
);
|
|
249
251
|
};
|
|
250
252
|
const claim: SettlementClaim = {
|
|
251
|
-
...(
|
|
253
|
+
...(replyingTo ? { replyingTo } : {}),
|
|
252
254
|
cleanup: () => signal.removeEventListener("abort", abort),
|
|
253
255
|
reject,
|
|
254
256
|
resolve,
|
|
@@ -303,9 +305,12 @@ export class SettlementReporter {
|
|
|
303
305
|
}
|
|
304
306
|
this.reporting.set(key, request.status);
|
|
305
307
|
try {
|
|
308
|
+
const replyingTo = retryState.attemptId
|
|
309
|
+
? this.claims.get(retryState.attemptId)?.replyingTo
|
|
310
|
+
: undefined;
|
|
306
311
|
const settlement = await this.collector.collect(
|
|
307
312
|
request.record,
|
|
308
|
-
request.status === "blocked" ? "blocked" : undefined,
|
|
313
|
+
request.status === "blocked" && !replyingTo ? "blocked" : undefined,
|
|
309
314
|
);
|
|
310
315
|
if (!settlement || !lifecycle.isCurrent()) return;
|
|
311
316
|
if (this.pendingBlocked.has(key)) return;
|
|
@@ -330,17 +335,28 @@ export class SettlementReporter {
|
|
|
330
335
|
const retryRequest = task ? { ...request, task } : request;
|
|
331
336
|
const expectedUser = activityExpectedUser(current.activity);
|
|
332
337
|
const blocked = settlement.status === "blocked";
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
338
|
+
// Closing a message and publishing resumed status use separate transports.
|
|
339
|
+
if (blocked && replyingTo) {
|
|
340
|
+
if (
|
|
341
|
+
settlement.extensionMessages?.some(
|
|
342
|
+
(message) =>
|
|
343
|
+
message.extension === replyingTo.extension &&
|
|
344
|
+
message.id === replyingTo.id,
|
|
345
|
+
)
|
|
346
|
+
)
|
|
347
|
+
return;
|
|
348
|
+
if (
|
|
349
|
+
!settlement.extensionMessages?.some((message) => message.blocking)
|
|
350
|
+
) {
|
|
351
|
+
if (!this.retry(lifecycle, retryRequest, retryState)) {
|
|
352
|
+
this.failReconciliation(
|
|
353
|
+
lifecycle,
|
|
354
|
+
current,
|
|
355
|
+
"the worker remained blocked after its extension reply",
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
344
360
|
}
|
|
345
361
|
// A blocked status is authoritative even when Pi has not persisted the turn.
|
|
346
362
|
// Pi persists expanded skills/templates, not the submitted command text.
|
|
@@ -485,7 +501,9 @@ export class SettlementReporter {
|
|
|
485
501
|
injectAgentEvent(this.pi, lifecycle.context, {
|
|
486
502
|
agent: settlement.agent,
|
|
487
503
|
agentToolName: "agents",
|
|
488
|
-
...(settlement.
|
|
504
|
+
...(settlement.extensionMessages
|
|
505
|
+
? { extensionMessages: settlement.extensionMessages }
|
|
506
|
+
: {}),
|
|
489
507
|
machine: this.origin.machine,
|
|
490
508
|
machineLabel: this.origin.label(),
|
|
491
509
|
operatorPrefix: this.origin.operatorPrefix,
|
package/src/types.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface PeerMessage {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface PeerDelivery {
|
|
10
|
+
result?: string;
|
|
10
11
|
command: boolean;
|
|
11
12
|
commands?: PeerCommand[];
|
|
12
13
|
}
|
|
@@ -103,11 +104,12 @@ export interface SessionSnapshot {
|
|
|
103
104
|
}
|
|
104
105
|
|
|
105
106
|
export interface MonitoredAgent {
|
|
107
|
+
readonly instructions?: string;
|
|
106
108
|
readonly scope: "task" | "persistent";
|
|
107
109
|
readonly activity: AgentActivity;
|
|
108
110
|
readonly cwd?: string;
|
|
109
111
|
readonly lastAssistantId?: string;
|
|
110
|
-
readonly
|
|
112
|
+
readonly notifiedExtensionMessages?: string[];
|
|
111
113
|
readonly name?: string;
|
|
112
114
|
readonly paneId: string;
|
|
113
115
|
readonly tabId: string;
|
|
@@ -148,26 +150,22 @@ export interface LatestInput {
|
|
|
148
150
|
text: string;
|
|
149
151
|
}
|
|
150
152
|
|
|
151
|
-
export interface
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
multiple: boolean;
|
|
158
|
-
title: string;
|
|
159
|
-
}>;
|
|
160
|
-
toolCallId: string;
|
|
153
|
+
export interface ExtensionMessage {
|
|
154
|
+
extension: string;
|
|
155
|
+
id: string;
|
|
156
|
+
message: string;
|
|
157
|
+
reply?: string;
|
|
158
|
+
blocking: boolean;
|
|
161
159
|
}
|
|
162
160
|
|
|
163
|
-
export interface
|
|
164
|
-
|
|
165
|
-
|
|
161
|
+
export interface ExtensionReply {
|
|
162
|
+
extension: string;
|
|
163
|
+
id: string;
|
|
164
|
+
message: string;
|
|
166
165
|
}
|
|
167
166
|
|
|
168
167
|
export interface SessionView {
|
|
169
|
-
|
|
170
|
-
askResults?: Record<string, AskResult>;
|
|
168
|
+
extensionMessages?: ExtensionMessage[];
|
|
171
169
|
assistant?: LatestAssistant;
|
|
172
170
|
assistantAfterInput?: boolean;
|
|
173
171
|
input?: LatestInput;
|
package/src/agents-answer.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import type { AgentToolUpdateCallback } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { AgentsParams } from "./agents-contract.js";
|
|
3
|
-
import {
|
|
4
|
-
dispatchAgentWork,
|
|
5
|
-
settlementResult,
|
|
6
|
-
toolResult,
|
|
7
|
-
} from "./agents-work.js";
|
|
8
|
-
import {
|
|
9
|
-
type AskAnswer,
|
|
10
|
-
answersMatch,
|
|
11
|
-
prepareAskAnswer,
|
|
12
|
-
sameAgentIdentity,
|
|
13
|
-
} from "./ask-answer.js";
|
|
14
|
-
import type { ConnectedMachine } from "./fleet.js";
|
|
15
|
-
import { getAgent } from "./herdr.js";
|
|
16
|
-
import type { PaneInfo } from "./types.js";
|
|
17
|
-
|
|
18
|
-
export async function answerAgent(
|
|
19
|
-
runtime: ConnectedMachine,
|
|
20
|
-
panel: PaneInfo,
|
|
21
|
-
params: AgentsParams,
|
|
22
|
-
signal: AbortSignal,
|
|
23
|
-
onUpdate: AgentToolUpdateCallback<Record<string, unknown>>,
|
|
24
|
-
) {
|
|
25
|
-
const answers = params.answers as AskAnswer[];
|
|
26
|
-
const askId = params.ask_id!.trim();
|
|
27
|
-
const view = await runtime.monitor.view(panel);
|
|
28
|
-
const prior = view.askResults?.[askId];
|
|
29
|
-
if (prior) {
|
|
30
|
-
const accepted =
|
|
31
|
-
prior.status === "accepted" && answersMatch(answers, prior.responses);
|
|
32
|
-
return toolResult({
|
|
33
|
-
answered: accepted,
|
|
34
|
-
ask_id: askId,
|
|
35
|
-
machine: runtime.machine,
|
|
36
|
-
status:
|
|
37
|
-
prior.status === "rejected"
|
|
38
|
-
? "rejected"
|
|
39
|
-
: accepted
|
|
40
|
-
? "accepted"
|
|
41
|
-
: "unknown",
|
|
42
|
-
target: panel.pane_id,
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
if (view.ask?.toolCallId !== askId) {
|
|
46
|
-
return toolResult({
|
|
47
|
-
answered: false,
|
|
48
|
-
ask_id: askId,
|
|
49
|
-
machine: runtime.machine,
|
|
50
|
-
status: "unknown",
|
|
51
|
-
target: panel.pane_id,
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
const prepared = await prepareAskAnswer(
|
|
55
|
-
runtime.client,
|
|
56
|
-
runtime.monitor,
|
|
57
|
-
panel,
|
|
58
|
-
answers,
|
|
59
|
-
signal,
|
|
60
|
-
askId,
|
|
61
|
-
);
|
|
62
|
-
if (prepared.ask.delivery === "steer") {
|
|
63
|
-
const result = await runtime.monitor.submitAskAnswer(
|
|
64
|
-
panel,
|
|
65
|
-
askId,
|
|
66
|
-
prepared.submit,
|
|
67
|
-
signal,
|
|
68
|
-
);
|
|
69
|
-
const accepted =
|
|
70
|
-
result?.status === "accepted" && answersMatch(answers, result.responses);
|
|
71
|
-
return toolResult({
|
|
72
|
-
answered: accepted,
|
|
73
|
-
ask_id: askId,
|
|
74
|
-
machine: runtime.machine,
|
|
75
|
-
status:
|
|
76
|
-
result?.status === "rejected"
|
|
77
|
-
? "rejected"
|
|
78
|
-
: accepted
|
|
79
|
-
? "accepted"
|
|
80
|
-
: "unknown",
|
|
81
|
-
target: panel.pane_id,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
const task = `Answer: ${prepared.ask.prompts
|
|
85
|
-
.map((prompt) => prompt.title)
|
|
86
|
-
.join(", ")}`;
|
|
87
|
-
const { settlement, warning } = await dispatchAgentWork(
|
|
88
|
-
runtime,
|
|
89
|
-
panel,
|
|
90
|
-
task,
|
|
91
|
-
true,
|
|
92
|
-
signal,
|
|
93
|
-
onUpdate,
|
|
94
|
-
prepared.submit,
|
|
95
|
-
{ answeringAskId: askId },
|
|
96
|
-
);
|
|
97
|
-
const current = await getAgent(runtime.client, panel.pane_id);
|
|
98
|
-
const result = (await runtime.monitor.view(current)).askResults?.[askId];
|
|
99
|
-
const accepted =
|
|
100
|
-
sameAgentIdentity(panel, current) &&
|
|
101
|
-
result?.status === "accepted" &&
|
|
102
|
-
answersMatch(answers, result.responses);
|
|
103
|
-
const {
|
|
104
|
-
machine: _workerMachine,
|
|
105
|
-
status: workerStatus,
|
|
106
|
-
target: _workerTarget,
|
|
107
|
-
...workerResult
|
|
108
|
-
} = settlement
|
|
109
|
-
? settlementResult(runtime.machine, settlement, {
|
|
110
|
-
reportWorkerError: true,
|
|
111
|
-
})
|
|
112
|
-
: {};
|
|
113
|
-
return toolResult(
|
|
114
|
-
{
|
|
115
|
-
...workerResult,
|
|
116
|
-
answered: accepted,
|
|
117
|
-
ask_id: askId,
|
|
118
|
-
machine: runtime.machine,
|
|
119
|
-
status:
|
|
120
|
-
result?.status === "rejected"
|
|
121
|
-
? "rejected"
|
|
122
|
-
: accepted
|
|
123
|
-
? "accepted"
|
|
124
|
-
: "unknown",
|
|
125
|
-
target: panel.pane_id,
|
|
126
|
-
...(workerStatus ? { worker_status: workerStatus } : {}),
|
|
127
|
-
},
|
|
128
|
-
warning,
|
|
129
|
-
);
|
|
130
|
-
}
|