@howaboua/pi-shepherdr 0.2.16 → 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 +14 -0
- package/README.md +39 -7
- package/index.ts +1 -1
- package/package.json +3 -3
- package/src/agent-identity.ts +17 -0
- package/src/agents-contract.ts +38 -28
- package/src/agents-discovery.ts +9 -6
- package/src/agents-reply.ts +68 -0
- package/src/agents-spawn.ts +1 -10
- package/src/agents-tool.ts +23 -44
- 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 +29 -1
- 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-commands.ts +29 -15
- package/src/peer-inbox.ts +54 -1
- package/src/remote/shepherdr-channel.mjs +0 -1
- package/src/remote/shepherdr-peer.mjs +101 -18
- package/src/remote/shepherdr-session.mjs +106 -190
- package/src/remote/shepherdr.mjs +12 -2
- package/src/remote-client.ts +22 -1
- package/src/session-reader.ts +3 -364
- package/src/settlement.ts +41 -23
- package/src/types.ts +20 -16
- package/src/agents-answer.ts +0 -130
- package/src/ask-answer.ts +0 -377
package/src/peer-inbox.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import { acceptFocus, readFocus } from "./agents-focus.js";
|
|
9
9
|
import { sendPolicyMessage, startPreparedIdleTurn } from "./delivery.js";
|
|
10
|
+
import { dispatchExtensionReply } from "./extension-reply.js";
|
|
10
11
|
import type { AgentFleet } from "./fleet.js";
|
|
11
12
|
import { getCurrentPane } from "./herdr.js";
|
|
12
13
|
import { HerdrClient } from "./herdr-client.js";
|
|
@@ -116,10 +117,14 @@ async function openInbox(
|
|
|
116
117
|
ctx.sessionManager.getSessionFile() !== sessionFile ||
|
|
117
118
|
typeof request["id"] !== "string" ||
|
|
118
119
|
(request["kind"] !== "focus" &&
|
|
120
|
+
request["kind"] !== "commands" &&
|
|
119
121
|
request["kind"] !== "message" &&
|
|
120
|
-
request["kind"] !== "channel"
|
|
122
|
+
request["kind"] !== "channel" &&
|
|
123
|
+
request["kind"] !== "extension-reply") ||
|
|
121
124
|
(request["kind"] !== "focus" &&
|
|
125
|
+
request["kind"] !== "commands" &&
|
|
122
126
|
request["kind"] !== "channel" &&
|
|
127
|
+
request["kind"] !== "extension-reply" &&
|
|
123
128
|
(typeof request["text"] !== "string" ||
|
|
124
129
|
!request["text"].trim() ||
|
|
125
130
|
typeof request["sender"] !== "string" ||
|
|
@@ -134,6 +139,54 @@ async function openInbox(
|
|
|
134
139
|
});
|
|
135
140
|
return;
|
|
136
141
|
}
|
|
142
|
+
if (request["kind"] === "extension-reply") {
|
|
143
|
+
const extension = request["extension"],
|
|
144
|
+
id = request["messageId"],
|
|
145
|
+
message = request["message"];
|
|
146
|
+
if (
|
|
147
|
+
typeof extension !== "string" ||
|
|
148
|
+
!extension.trim() ||
|
|
149
|
+
typeof id !== "string" ||
|
|
150
|
+
!id.trim() ||
|
|
151
|
+
typeof message !== "string"
|
|
152
|
+
) {
|
|
153
|
+
reply({
|
|
154
|
+
ok: false,
|
|
155
|
+
rejected: true,
|
|
156
|
+
error: "Extension reply requires extension, id and message strings",
|
|
157
|
+
});
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
// No agent turn, UI automation or idle/busy restriction: the producer owns delivery.
|
|
161
|
+
socket.setTimeout(60_000, () => socket.destroy());
|
|
162
|
+
try {
|
|
163
|
+
const result = await dispatchExtensionReply(pi, {
|
|
164
|
+
extension,
|
|
165
|
+
id,
|
|
166
|
+
message,
|
|
167
|
+
});
|
|
168
|
+
reply({ ok: true, command: false, result });
|
|
169
|
+
} catch (error) {
|
|
170
|
+
reply({
|
|
171
|
+
ok: false,
|
|
172
|
+
rejected: false,
|
|
173
|
+
error: error instanceof Error ? error.message : String(error),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (request["kind"] === "commands") {
|
|
179
|
+
try {
|
|
180
|
+
reply({ ok: true, command: false, commands: commands.available() });
|
|
181
|
+
} catch (error) {
|
|
182
|
+
reply({
|
|
183
|
+
ok: false,
|
|
184
|
+
rejected: true,
|
|
185
|
+
error: error instanceof Error ? error.message : String(error),
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
137
190
|
if (request["kind"] === "channel") {
|
|
138
191
|
let focus;
|
|
139
192
|
try {
|
|
@@ -160,7 +160,6 @@ export async function openPeerChannel(request, expected, focus) {
|
|
|
160
160
|
const { descriptor, sessionFile, agent } = await resolvePeerReceiver(
|
|
161
161
|
request,
|
|
162
162
|
expected,
|
|
163
|
-
true,
|
|
164
163
|
);
|
|
165
164
|
const id = randomUUID();
|
|
166
165
|
const frame = `${JSON.stringify({ protocol: PEER_PROTOCOL, kind: "channel", id, token: descriptor.token, sessionFile, terminalId: agent.terminal_id, focus })}\n`;
|
|
@@ -6,7 +6,7 @@ import { createConnection } from "node:net";
|
|
|
6
6
|
import { basename, dirname } from "node:path";
|
|
7
7
|
|
|
8
8
|
export const MAX_PEER_FRAME_BYTES = 8 * 1024 * 1024;
|
|
9
|
-
export const PEER_PROTOCOL =
|
|
9
|
+
export const PEER_PROTOCOL = 4;
|
|
10
10
|
|
|
11
11
|
/** @param {string} sessionFile @param {string} terminalId */
|
|
12
12
|
export function peerInboxPath(sessionFile, terminalId) {
|
|
@@ -86,19 +86,25 @@ export async function readReceiver(path) {
|
|
|
86
86
|
* @param {import("../types.js").PaneInfo} expected
|
|
87
87
|
* @param {import("../types.js").PeerMessage} message
|
|
88
88
|
* @param {import("../types.js").FocusRequest} [focus]
|
|
89
|
+
* @param {boolean} [commands]
|
|
89
90
|
* @returns {Promise<import("../types.js").PeerDelivery>}
|
|
90
91
|
*/
|
|
91
|
-
export async function sendPeerMessage(
|
|
92
|
+
export async function sendPeerMessage(
|
|
93
|
+
request,
|
|
94
|
+
expected,
|
|
95
|
+
message,
|
|
96
|
+
focus,
|
|
97
|
+
commands = false,
|
|
98
|
+
) {
|
|
92
99
|
const { descriptor, sessionFile, agent } = await resolvePeerReceiver(
|
|
93
100
|
request,
|
|
94
101
|
expected,
|
|
95
|
-
Boolean(focus),
|
|
96
102
|
);
|
|
97
103
|
const id = randomUUID();
|
|
98
104
|
const frame =
|
|
99
105
|
JSON.stringify({
|
|
100
106
|
protocol: PEER_PROTOCOL,
|
|
101
|
-
kind: focus ? "focus" : "message",
|
|
107
|
+
kind: commands ? "commands" : focus ? "focus" : "message",
|
|
102
108
|
focus,
|
|
103
109
|
id,
|
|
104
110
|
token: descriptor.token,
|
|
@@ -123,9 +129,51 @@ export async function sendPeerMessage(request, expected, message, focus) {
|
|
|
123
129
|
/**
|
|
124
130
|
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
125
131
|
* @param {import("../types.js").PaneInfo} expected
|
|
126
|
-
* @param {
|
|
132
|
+
* @param {import("../types.js").ExtensionReply} reply
|
|
133
|
+
* @returns {Promise<string>}
|
|
127
134
|
*/
|
|
128
|
-
export async function
|
|
135
|
+
export async function sendPeerExtensionReply(request, expected, reply) {
|
|
136
|
+
const { descriptor, sessionFile, agent } = await resolvePeerReceiver(
|
|
137
|
+
request,
|
|
138
|
+
expected,
|
|
139
|
+
);
|
|
140
|
+
const id = randomUUID();
|
|
141
|
+
const frame =
|
|
142
|
+
JSON.stringify({
|
|
143
|
+
protocol: PEER_PROTOCOL,
|
|
144
|
+
kind: "extension-reply",
|
|
145
|
+
id,
|
|
146
|
+
token: descriptor.token,
|
|
147
|
+
sessionFile,
|
|
148
|
+
terminalId: agent.terminal_id,
|
|
149
|
+
extension: reply.extension,
|
|
150
|
+
messageId: reply.id,
|
|
151
|
+
message: reply.message,
|
|
152
|
+
}) + "\n";
|
|
153
|
+
const receipt = await deliverPeerFrame(
|
|
154
|
+
request,
|
|
155
|
+
expected,
|
|
156
|
+
{ text: "", sender: "" },
|
|
157
|
+
undefined,
|
|
158
|
+
descriptor,
|
|
159
|
+
sessionFile,
|
|
160
|
+
id,
|
|
161
|
+
frame,
|
|
162
|
+
true,
|
|
163
|
+
);
|
|
164
|
+
if (typeof receipt.result !== "string")
|
|
165
|
+
throw deliveryError(
|
|
166
|
+
"Extension reply was not acknowledged; inspect the target before retrying",
|
|
167
|
+
false,
|
|
168
|
+
);
|
|
169
|
+
return receipt.result;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
174
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
175
|
+
*/
|
|
176
|
+
export async function resolvePeerReceiver(request, expected) {
|
|
129
177
|
const result = await request("agent.get", { target: expected.pane_id });
|
|
130
178
|
const agent =
|
|
131
179
|
result &&
|
|
@@ -160,16 +208,6 @@ export async function resolvePeerReceiver(request, expected, allowBlocked) {
|
|
|
160
208
|
true,
|
|
161
209
|
);
|
|
162
210
|
}
|
|
163
|
-
if (
|
|
164
|
-
!allowBlocked &&
|
|
165
|
-
"agent_status" in agent &&
|
|
166
|
-
agent.agent_status === "blocked"
|
|
167
|
-
) {
|
|
168
|
-
throw deliveryError(
|
|
169
|
-
"Target is blocked; answer its pending question first",
|
|
170
|
-
true,
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
211
|
let descriptor;
|
|
174
212
|
try {
|
|
175
213
|
descriptor = JSON.parse(
|
|
@@ -208,6 +246,7 @@ export async function resolvePeerReceiver(request, expected, allowBlocked) {
|
|
|
208
246
|
* @param {string} sessionFile
|
|
209
247
|
* @param {string} id
|
|
210
248
|
* @param {string} frame
|
|
249
|
+
* @param {boolean} [extensionReply]
|
|
211
250
|
* @returns {Promise<import("../types.js").PeerDelivery>}
|
|
212
251
|
*/
|
|
213
252
|
function deliverPeerFrame(
|
|
@@ -219,6 +258,7 @@ function deliverPeerFrame(
|
|
|
219
258
|
sessionFile,
|
|
220
259
|
id,
|
|
221
260
|
frame,
|
|
261
|
+
extensionReply = false,
|
|
222
262
|
) {
|
|
223
263
|
if (Buffer.byteLength(frame) > MAX_PEER_FRAME_BYTES) {
|
|
224
264
|
throw deliveryError("Peer message is too large", true);
|
|
@@ -250,7 +290,7 @@ function deliverPeerFrame(
|
|
|
250
290
|
!attempted,
|
|
251
291
|
),
|
|
252
292
|
);
|
|
253
|
-
const timer = setTimeout(failed, focus ? 55_000 : 10_000);
|
|
293
|
+
const timer = setTimeout(failed, focus || extensionReply ? 55_000 : 10_000);
|
|
254
294
|
timer.unref();
|
|
255
295
|
socket.setEncoding("utf8");
|
|
256
296
|
socket.on("error", failed);
|
|
@@ -295,7 +335,16 @@ function deliverPeerFrame(
|
|
|
295
335
|
// The receiver retains this socket through session_shutdown.
|
|
296
336
|
// Only process exit ends it; never close a pane on a timeout.
|
|
297
337
|
clearTimeout(timer);
|
|
298
|
-
} else
|
|
338
|
+
} else
|
|
339
|
+
finish(undefined, {
|
|
340
|
+
command: reply.command,
|
|
341
|
+
...(typeof reply.result === "string"
|
|
342
|
+
? { result: reply.result }
|
|
343
|
+
: {}),
|
|
344
|
+
...(reply.commands === undefined
|
|
345
|
+
? {}
|
|
346
|
+
: { commands: reply.commands }),
|
|
347
|
+
});
|
|
299
348
|
} else if (reply.ok === false && typeof reply.error === "string")
|
|
300
349
|
finish(deliveryError(reply.error, reply.rejected === true));
|
|
301
350
|
else failed();
|
|
@@ -320,6 +369,40 @@ export function sendPeerFocus(request, expected, focus) {
|
|
|
320
369
|
);
|
|
321
370
|
}
|
|
322
371
|
|
|
372
|
+
/** @param {unknown} value @returns {import("../types.js").PeerCommand[]} */
|
|
373
|
+
export function readPeerCommands(value) {
|
|
374
|
+
if (
|
|
375
|
+
!Array.isArray(value) ||
|
|
376
|
+
!value.every(
|
|
377
|
+
(entry) =>
|
|
378
|
+
entry &&
|
|
379
|
+
typeof entry === "object" &&
|
|
380
|
+
typeof entry.name === "string" &&
|
|
381
|
+
typeof entry.description === "string",
|
|
382
|
+
)
|
|
383
|
+
)
|
|
384
|
+
throw new Error(
|
|
385
|
+
"Command discovery returned an invalid list; ask the user to update and reload Shepherdr in the target session",
|
|
386
|
+
);
|
|
387
|
+
return value.map(({ name, description }) => ({ name, description }));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
392
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
393
|
+
* @returns {Promise<import("../types.js").PeerCommand[]>}
|
|
394
|
+
*/
|
|
395
|
+
export async function sendPeerCommands(request, expected) {
|
|
396
|
+
const receipt = await sendPeerMessage(
|
|
397
|
+
request,
|
|
398
|
+
expected,
|
|
399
|
+
{ text: "", sender: "" },
|
|
400
|
+
undefined,
|
|
401
|
+
true,
|
|
402
|
+
);
|
|
403
|
+
return readPeerCommands(receipt.commands);
|
|
404
|
+
}
|
|
405
|
+
|
|
323
406
|
/**
|
|
324
407
|
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
325
408
|
* @param {import("../types.js").PaneInfo} expected
|
|
@@ -4,9 +4,18 @@ import { open, stat } from "node:fs/promises";
|
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
|
|
7
|
+
/** @param {unknown} value @returns {Record<string, unknown> | undefined} */
|
|
8
|
+
function record(value) {
|
|
9
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
10
|
+
? /** @type {Record<string, unknown>} */ (value)
|
|
11
|
+
: undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
7
14
|
const READ_CHUNK_BYTES = 64 * 1024;
|
|
15
|
+
/** @type {Map<string,{mtimeMs:number,size:number,result:import("../types.js").SessionView}>} */
|
|
8
16
|
const sessionCache = new Map();
|
|
9
17
|
|
|
18
|
+
/** @param {string} path */
|
|
10
19
|
function expandHome(path) {
|
|
11
20
|
return path === "~"
|
|
12
21
|
? homedir()
|
|
@@ -15,24 +24,23 @@ function expandHome(path) {
|
|
|
15
24
|
: path;
|
|
16
25
|
}
|
|
17
26
|
|
|
27
|
+
/** @param {string} id @param {Record<string, unknown>} message @returns {import("../types.js").LatestAssistant | undefined} */
|
|
18
28
|
function assistantFromMessage(id, message) {
|
|
19
|
-
if (message
|
|
29
|
+
if (message["role"] !== "assistant") return undefined;
|
|
20
30
|
const text = [];
|
|
21
|
-
if (typeof message
|
|
22
|
-
else if (Array.isArray(message
|
|
23
|
-
for (const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
part.type === "text" &&
|
|
28
|
-
typeof part.text === "string"
|
|
29
|
-
)
|
|
30
|
-
text.push(part.text);
|
|
31
|
+
if (typeof message["content"] === "string") text.push(message["content"]);
|
|
32
|
+
else if (Array.isArray(message["content"])) {
|
|
33
|
+
for (const value of message["content"]) {
|
|
34
|
+
const part = record(value);
|
|
35
|
+
if (part && part["type"] === "text" && typeof part["text"] === "string")
|
|
36
|
+
text.push(part["text"]);
|
|
31
37
|
}
|
|
32
38
|
}
|
|
33
39
|
const joined = text.join("");
|
|
34
40
|
const stopReason =
|
|
35
|
-
typeof message
|
|
41
|
+
typeof message["stopReason"] === "string"
|
|
42
|
+
? message["stopReason"]
|
|
43
|
+
: undefined;
|
|
36
44
|
if (!joined && stopReason !== "error") return undefined;
|
|
37
45
|
return {
|
|
38
46
|
id,
|
|
@@ -41,239 +49,145 @@ function assistantFromMessage(id, message) {
|
|
|
41
49
|
};
|
|
42
50
|
}
|
|
43
51
|
|
|
52
|
+
/** @param {string} id @param {Record<string, unknown>} message @returns {import("../types.js").LatestInput | undefined} */
|
|
44
53
|
function inputFromMessage(id, message) {
|
|
45
|
-
if (message
|
|
54
|
+
if (message["role"] !== "user") return undefined;
|
|
46
55
|
const text = [];
|
|
47
|
-
if (typeof message
|
|
48
|
-
else if (Array.isArray(message
|
|
49
|
-
for (const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
part.type === "text" &&
|
|
54
|
-
typeof part.text === "string"
|
|
55
|
-
)
|
|
56
|
-
text.push(part.text);
|
|
56
|
+
if (typeof message["content"] === "string") text.push(message["content"]);
|
|
57
|
+
else if (Array.isArray(message["content"])) {
|
|
58
|
+
for (const value of message["content"]) {
|
|
59
|
+
const part = record(value);
|
|
60
|
+
if (part && part["type"] === "text" && typeof part["text"] === "string")
|
|
61
|
+
text.push(part["text"]);
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
64
|
const joined = text.join("");
|
|
60
65
|
return joined ? { id, text: joined } : undefined;
|
|
61
66
|
}
|
|
62
67
|
|
|
63
|
-
|
|
64
|
-
|
|
68
|
+
/** @param {unknown} value @returns {import("../types.js").ExtensionMessage | undefined} */
|
|
69
|
+
export function readExtensionMessage(value) {
|
|
70
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
65
71
|
return undefined;
|
|
66
|
-
|
|
67
|
-
label: value.label,
|
|
68
|
-
...(typeof value.description === "string"
|
|
69
|
-
? { description: value.description }
|
|
70
|
-
: {}),
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function askPrompt(value) {
|
|
75
|
-
if (!value || typeof value !== "object" || typeof value.title !== "string")
|
|
76
|
-
return undefined;
|
|
77
|
-
return {
|
|
78
|
-
title: value.title,
|
|
79
|
-
multiple: value.multiple === true,
|
|
80
|
-
choices: (Array.isArray(value.choices) ? value.choices : [])
|
|
81
|
-
.map(askChoice)
|
|
82
|
-
.filter(Boolean),
|
|
83
|
-
...(typeof value.body === "string" ? { body: value.body } : {}),
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function askCall(value) {
|
|
72
|
+
const data = /** @type {Record<string, unknown>} */ (value);
|
|
88
73
|
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
typeof
|
|
74
|
+
data["version"] !== 1 ||
|
|
75
|
+
data["state"] !== "pending" ||
|
|
76
|
+
typeof data["extension"] !== "string" ||
|
|
77
|
+
!data["extension"].trim() ||
|
|
78
|
+
typeof data["id"] !== "string" ||
|
|
79
|
+
!data["id"].trim() ||
|
|
80
|
+
typeof data["message"] !== "string" ||
|
|
81
|
+
typeof data["blocking"] !== "boolean" ||
|
|
82
|
+
(data["reply"] !== undefined && typeof data["reply"] !== "string")
|
|
94
83
|
)
|
|
95
84
|
return undefined;
|
|
96
|
-
return askFromInput(value.id, value.arguments);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function askFromInput(toolCallId, input) {
|
|
100
|
-
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
101
|
-
return undefined;
|
|
102
|
-
const prompts = (Array.isArray(input.prompts) ? input.prompts : [])
|
|
103
|
-
.map(askPrompt)
|
|
104
|
-
.filter(Boolean);
|
|
105
|
-
if (prompts.length === 0) return undefined;
|
|
106
85
|
return {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
86
|
+
extension: data["extension"],
|
|
87
|
+
id: data["id"],
|
|
88
|
+
message: data["message"],
|
|
89
|
+
blocking: data["blocking"],
|
|
90
|
+
...(typeof data["reply"] === "string" ? { reply: data["reply"] } : {}),
|
|
111
91
|
};
|
|
112
92
|
}
|
|
113
93
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const id = message.toolCallId ?? message.tool_call_id;
|
|
118
|
-
if (typeof id !== "string") return undefined;
|
|
119
|
-
if (message.details?.pending === true) return undefined;
|
|
120
|
-
if (message.isError === true) return [id, { status: "rejected" }];
|
|
121
|
-
if (message.isError !== false) return [id, { status: "unknown" }];
|
|
122
|
-
if (message.details?.dismissed === true) return [id, { status: "rejected" }];
|
|
123
|
-
const rawResponses = message.details?.responses;
|
|
124
|
-
const rawResponseCount = Array.isArray(rawResponses)
|
|
125
|
-
? rawResponses.length
|
|
126
|
-
: -1;
|
|
127
|
-
const responses = Array.isArray(rawResponses)
|
|
128
|
-
? rawResponses
|
|
129
|
-
.map((response) => {
|
|
130
|
-
if (
|
|
131
|
-
!response ||
|
|
132
|
-
typeof response !== "object" ||
|
|
133
|
-
typeof response.id !== "string" ||
|
|
134
|
-
!Array.isArray(response.selections) ||
|
|
135
|
-
!response.selections.every(
|
|
136
|
-
(selection) => typeof selection === "string",
|
|
137
|
-
)
|
|
138
|
-
)
|
|
139
|
-
return undefined;
|
|
140
|
-
return {
|
|
141
|
-
id: response.id,
|
|
142
|
-
selections: response.selections,
|
|
143
|
-
...(typeof response.comment === "string"
|
|
144
|
-
? { comment: response.comment }
|
|
145
|
-
: {}),
|
|
146
|
-
};
|
|
147
|
-
})
|
|
148
|
-
.filter(Boolean)
|
|
149
|
-
: undefined;
|
|
150
|
-
return [
|
|
151
|
-
id,
|
|
152
|
-
{
|
|
153
|
-
status:
|
|
154
|
-
responses &&
|
|
155
|
-
responses.length > 0 &&
|
|
156
|
-
responses.length === rawResponseCount
|
|
157
|
-
? "accepted"
|
|
158
|
-
: "unknown",
|
|
159
|
-
...(responses && responses.length === rawResponseCount
|
|
160
|
-
? { responses }
|
|
161
|
-
: {}),
|
|
162
|
-
},
|
|
163
|
-
];
|
|
94
|
+
/** @param {{extension:string,id:string}} value */
|
|
95
|
+
export function extensionMessageKey(value) {
|
|
96
|
+
return JSON.stringify([value.extension, value.id]);
|
|
164
97
|
}
|
|
165
98
|
|
|
99
|
+
/** @param {string} path @param {number} size @returns {Promise<import("../types.js").SessionView>} */
|
|
166
100
|
async function sessionView(path, size) {
|
|
167
101
|
const file = await open(path, "r");
|
|
102
|
+
/** @type {string | undefined} */
|
|
168
103
|
let targetId;
|
|
104
|
+
/** @type {import("../types.js").LatestAssistant | undefined} */
|
|
169
105
|
let assistant;
|
|
106
|
+
/** @type {number | undefined} */
|
|
170
107
|
let assistantDepth;
|
|
171
|
-
|
|
172
|
-
const
|
|
108
|
+
/** @type {import("../types.js").ExtensionMessage[]} */
|
|
109
|
+
const extensionMessages = [];
|
|
173
110
|
let depth = 0;
|
|
111
|
+
/** @type {import("../types.js").LatestInput | undefined} */
|
|
174
112
|
let input;
|
|
113
|
+
/** @type {number | undefined} */
|
|
175
114
|
let inputDepth;
|
|
176
115
|
const resolved = new Set();
|
|
177
116
|
const result = () => ({
|
|
178
117
|
...(assistant ? { assistant } : {}),
|
|
179
|
-
...(
|
|
180
|
-
...(askResults.size > 0
|
|
181
|
-
? { askResults: Object.fromEntries(askResults) }
|
|
182
|
-
: {}),
|
|
118
|
+
...(extensionMessages.length ? { extensionMessages } : {}),
|
|
183
119
|
...(input ? { input } : {}),
|
|
184
120
|
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
185
121
|
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
186
122
|
: {}),
|
|
187
123
|
});
|
|
124
|
+
/** @param {Buffer} line */
|
|
188
125
|
const inspect = (line) => {
|
|
189
126
|
if (line.length === 0) return false;
|
|
190
|
-
|
|
127
|
+
/** @type {unknown} */
|
|
128
|
+
let parsed;
|
|
191
129
|
try {
|
|
192
|
-
|
|
130
|
+
parsed = JSON.parse(line.toString("utf8"));
|
|
193
131
|
} catch {
|
|
194
132
|
return false;
|
|
195
133
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
134
|
+
const entry = record(parsed);
|
|
135
|
+
if (!entry || typeof entry["id"] !== "string") return false;
|
|
136
|
+
targetId ??= entry["id"];
|
|
137
|
+
if (entry["id"] !== targetId) return false;
|
|
199
138
|
const currentDepth = depth;
|
|
200
139
|
depth += 1;
|
|
201
|
-
if (
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
update.state === "active" &&
|
|
207
|
-
!ask &&
|
|
208
|
-
!resolved.has(update.id)
|
|
209
|
-
) {
|
|
210
|
-
ask = askFromInput(update.id, update);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (entry.type === "custom" && entry.customType === "pi-ask-pending") {
|
|
215
|
-
const update = entry.data;
|
|
140
|
+
if (
|
|
141
|
+
entry["type"] === "custom" &&
|
|
142
|
+
entry["customType"] === "shepherdr-extension-message"
|
|
143
|
+
) {
|
|
144
|
+
const update = record(entry["data"]);
|
|
216
145
|
if (
|
|
217
|
-
update?.version === 1 &&
|
|
218
|
-
update
|
|
219
|
-
|
|
146
|
+
update?.["version"] === 1 &&
|
|
147
|
+
typeof update["extension"] === "string" &&
|
|
148
|
+
update["extension"].trim() &&
|
|
149
|
+
typeof update["id"] === "string" &&
|
|
150
|
+
update["id"].trim()
|
|
220
151
|
) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
toolName: "ask",
|
|
225
|
-
toolCallId: update.id,
|
|
226
|
-
isError: false,
|
|
227
|
-
details: update,
|
|
152
|
+
const key = extensionMessageKey({
|
|
153
|
+
extension: update["extension"],
|
|
154
|
+
id: update["id"],
|
|
228
155
|
});
|
|
229
|
-
if (
|
|
156
|
+
if (!resolved.has(key)) {
|
|
157
|
+
if (update["state"] === "closed") resolved.add(key);
|
|
158
|
+
else {
|
|
159
|
+
const pending = readExtensionMessage(update);
|
|
160
|
+
if (pending) {
|
|
161
|
+
extensionMessages.push(pending);
|
|
162
|
+
resolved.add(key);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
230
166
|
}
|
|
231
167
|
}
|
|
232
168
|
if (
|
|
233
169
|
!input &&
|
|
234
|
-
entry
|
|
235
|
-
entry
|
|
236
|
-
typeof entry
|
|
237
|
-
entry
|
|
170
|
+
entry["type"] === "custom_message" &&
|
|
171
|
+
entry["customType"] === "herdr-agent-message" &&
|
|
172
|
+
typeof entry["content"] === "string" &&
|
|
173
|
+
entry["content"]
|
|
238
174
|
) {
|
|
239
|
-
input = { id: entry
|
|
175
|
+
input = { id: entry["id"], text: entry["content"] };
|
|
240
176
|
inputDepth = currentDepth;
|
|
241
177
|
}
|
|
242
|
-
const message = entry
|
|
178
|
+
const message = record(entry["message"]);
|
|
243
179
|
if (message && typeof message === "object") {
|
|
244
|
-
const result = askResult(message);
|
|
245
|
-
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
246
|
-
if (
|
|
247
|
-
(message.role === "toolResult" || message.role === "tool") &&
|
|
248
|
-
message.details?.pending !== true &&
|
|
249
|
-
typeof (message.toolCallId ?? message.tool_call_id) === "string"
|
|
250
|
-
)
|
|
251
|
-
resolved.add(message.toolCallId ?? message.tool_call_id);
|
|
252
180
|
if (!assistant) {
|
|
253
|
-
assistant = assistantFromMessage(entry
|
|
181
|
+
assistant = assistantFromMessage(entry["id"], message);
|
|
254
182
|
if (assistant) assistantDepth = currentDepth;
|
|
255
183
|
}
|
|
256
184
|
if (!input) {
|
|
257
|
-
input = inputFromMessage(entry
|
|
185
|
+
input = inputFromMessage(entry["id"], message);
|
|
258
186
|
if (input) inputDepth = currentDepth;
|
|
259
187
|
}
|
|
260
|
-
if (
|
|
261
|
-
!ask &&
|
|
262
|
-
message.role === "assistant" &&
|
|
263
|
-
Array.isArray(message.content)
|
|
264
|
-
)
|
|
265
|
-
ask = [...message.content]
|
|
266
|
-
.reverse()
|
|
267
|
-
.map(askCall)
|
|
268
|
-
.find(
|
|
269
|
-
(candidate) =>
|
|
270
|
-
candidate &&
|
|
271
|
-
candidate.delivery !== "steer" &&
|
|
272
|
-
!resolved.has(candidate.toolCallId),
|
|
273
|
-
);
|
|
274
188
|
}
|
|
275
|
-
if (typeof entry
|
|
276
|
-
targetId = entry
|
|
189
|
+
if (typeof entry["parentId"] !== "string") return true;
|
|
190
|
+
targetId = entry["parentId"];
|
|
277
191
|
return false;
|
|
278
192
|
};
|
|
279
193
|
|
|
@@ -286,13 +200,13 @@ async function sessionView(path, size) {
|
|
|
286
200
|
const chunk = Buffer.allocUnsafe(length);
|
|
287
201
|
const { bytesRead } = await file.read(chunk, 0, length, position);
|
|
288
202
|
const data = Buffer.concat([chunk.subarray(0, bytesRead), partial]);
|
|
289
|
-
let lineEnd = data
|
|
290
|
-
for (let index = data
|
|
203
|
+
let lineEnd = data["length"];
|
|
204
|
+
for (let index = data["length"] - 1; index >= 0; index -= 1) {
|
|
291
205
|
if (data[index] !== 0x0a) continue;
|
|
292
|
-
if (inspect(data
|
|
206
|
+
if (inspect(data["subarray"](index + 1, lineEnd))) return result();
|
|
293
207
|
lineEnd = index;
|
|
294
208
|
}
|
|
295
|
-
partial = data
|
|
209
|
+
partial = data["subarray"](0, lineEnd);
|
|
296
210
|
}
|
|
297
211
|
inspect(partial);
|
|
298
212
|
return result();
|
|
@@ -301,6 +215,7 @@ async function sessionView(path, size) {
|
|
|
301
215
|
}
|
|
302
216
|
}
|
|
303
217
|
|
|
218
|
+
/** @param {string | undefined} path @returns {Promise<import("../types.js").SessionView>} */
|
|
304
219
|
export async function readSessionView(path) {
|
|
305
220
|
if (!path) return {};
|
|
306
221
|
const expanded = expandHome(path);
|
|
@@ -308,7 +223,8 @@ export async function readSessionView(path) {
|
|
|
308
223
|
try {
|
|
309
224
|
metadata = await stat(expanded);
|
|
310
225
|
} catch (error) {
|
|
311
|
-
if (error.code === "ENOENT")
|
|
226
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
227
|
+
return {};
|
|
312
228
|
throw error;
|
|
313
229
|
}
|
|
314
230
|
const cached = sessionCache.get(expanded);
|