@howaboua/pi-shepherdr 0.2.4 → 0.2.6
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 +18 -0
- package/README.md +6 -4
- package/package.json +6 -6
- package/profiles/explorer/profile.json +1 -1
- package/profiles/general/profile.json +1 -1
- package/profiles/reviewer/profile.json +1 -1
- package/src/agents-answer.ts +108 -0
- package/src/agents-contract.ts +18 -1
- package/src/agents-discovery.ts +211 -0
- package/src/agents-spawn.ts +168 -0
- package/src/agents-tool.ts +11 -195
- package/src/agents-work.ts +194 -0
- package/src/ask-answer.ts +54 -2
- package/src/messages.ts +9 -3
- package/src/monitor.ts +2 -1
- package/src/remote/shepherdr-session.mjs +294 -0
- package/src/remote/shepherdr.mjs +5 -233
- package/src/remote-client.ts +9 -2
- package/src/session-reader.ts +58 -0
- package/src/settlement.ts +19 -1
- package/src/types.ts +6 -0
- package/src/agents-operations.ts +0 -413
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// @howaboua/pi-shepherdr managed bridge
|
|
2
|
+
import { open, stat } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
const READ_CHUNK_BYTES = 64 * 1024;
|
|
7
|
+
const sessionCache = new Map();
|
|
8
|
+
|
|
9
|
+
function expandHome(path) {
|
|
10
|
+
return path === "~"
|
|
11
|
+
? homedir()
|
|
12
|
+
: path.startsWith("~/")
|
|
13
|
+
? join(homedir(), path.slice(2))
|
|
14
|
+
: path;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function assistantFromMessage(id, message) {
|
|
18
|
+
if (message.role !== "assistant") return undefined;
|
|
19
|
+
const text = [];
|
|
20
|
+
if (typeof message.content === "string") text.push(message.content);
|
|
21
|
+
else if (Array.isArray(message.content)) {
|
|
22
|
+
for (const part of message.content) {
|
|
23
|
+
if (
|
|
24
|
+
part &&
|
|
25
|
+
typeof part === "object" &&
|
|
26
|
+
part.type === "text" &&
|
|
27
|
+
typeof part.text === "string"
|
|
28
|
+
)
|
|
29
|
+
text.push(part.text);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const joined = text.join("");
|
|
33
|
+
const stopReason =
|
|
34
|
+
typeof message.stopReason === "string" ? message.stopReason : undefined;
|
|
35
|
+
if (!joined && stopReason !== "error") return undefined;
|
|
36
|
+
return {
|
|
37
|
+
id,
|
|
38
|
+
text: joined,
|
|
39
|
+
...(stopReason ? { stopReason } : {}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function inputFromMessage(id, message) {
|
|
44
|
+
if (message.role !== "user") return undefined;
|
|
45
|
+
const text = [];
|
|
46
|
+
if (typeof message.content === "string") text.push(message.content);
|
|
47
|
+
else if (Array.isArray(message.content)) {
|
|
48
|
+
for (const part of message.content) {
|
|
49
|
+
if (
|
|
50
|
+
part &&
|
|
51
|
+
typeof part === "object" &&
|
|
52
|
+
part.type === "text" &&
|
|
53
|
+
typeof part.text === "string"
|
|
54
|
+
)
|
|
55
|
+
text.push(part.text);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const joined = text.join("");
|
|
59
|
+
return joined ? { id, text: joined } : undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function askChoice(value) {
|
|
63
|
+
if (!value || typeof value !== "object" || typeof value.label !== "string")
|
|
64
|
+
return undefined;
|
|
65
|
+
return {
|
|
66
|
+
label: value.label,
|
|
67
|
+
...(typeof value.description === "string"
|
|
68
|
+
? { description: value.description }
|
|
69
|
+
: {}),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function askPrompt(value) {
|
|
74
|
+
if (!value || typeof value !== "object" || typeof value.title !== "string")
|
|
75
|
+
return undefined;
|
|
76
|
+
return {
|
|
77
|
+
title: value.title,
|
|
78
|
+
multiple: value.multiple === true,
|
|
79
|
+
choices: (Array.isArray(value.choices) ? value.choices : [])
|
|
80
|
+
.map(askChoice)
|
|
81
|
+
.filter(Boolean),
|
|
82
|
+
...(typeof value.body === "string" ? { body: value.body } : {}),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function askCall(value) {
|
|
87
|
+
if (
|
|
88
|
+
!value ||
|
|
89
|
+
typeof value !== "object" ||
|
|
90
|
+
value.type !== "toolCall" ||
|
|
91
|
+
value.name !== "ask" ||
|
|
92
|
+
typeof value.id !== "string"
|
|
93
|
+
)
|
|
94
|
+
return undefined;
|
|
95
|
+
return askFromInput(value.id, value.arguments);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function askFromInput(toolCallId, input) {
|
|
99
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
100
|
+
return undefined;
|
|
101
|
+
const prompts = (Array.isArray(input.prompts) ? input.prompts : [])
|
|
102
|
+
.map(askPrompt)
|
|
103
|
+
.filter(Boolean);
|
|
104
|
+
if (prompts.length === 0) return undefined;
|
|
105
|
+
return {
|
|
106
|
+
toolCallId,
|
|
107
|
+
handoff: input.handoff === true,
|
|
108
|
+
prompts,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function askResult(message) {
|
|
113
|
+
if (message.role !== "toolResult" || message.toolName !== "ask")
|
|
114
|
+
return undefined;
|
|
115
|
+
const id = message.toolCallId ?? message.tool_call_id;
|
|
116
|
+
if (typeof id !== "string") return undefined;
|
|
117
|
+
if (message.isError === true) return [id, { status: "rejected" }];
|
|
118
|
+
if (message.isError !== false) return [id, { status: "unknown" }];
|
|
119
|
+
if (message.details?.dismissed === true) return [id, { status: "rejected" }];
|
|
120
|
+
const rawResponses = message.details?.responses;
|
|
121
|
+
const rawResponseCount = Array.isArray(rawResponses)
|
|
122
|
+
? rawResponses.length
|
|
123
|
+
: -1;
|
|
124
|
+
const responses = Array.isArray(rawResponses)
|
|
125
|
+
? rawResponses
|
|
126
|
+
.map((response) => {
|
|
127
|
+
if (
|
|
128
|
+
!response ||
|
|
129
|
+
typeof response !== "object" ||
|
|
130
|
+
typeof response.id !== "string" ||
|
|
131
|
+
!Array.isArray(response.selections) ||
|
|
132
|
+
!response.selections.every(
|
|
133
|
+
(selection) => typeof selection === "string",
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
return undefined;
|
|
137
|
+
return {
|
|
138
|
+
id: response.id,
|
|
139
|
+
selections: response.selections,
|
|
140
|
+
...(typeof response.comment === "string"
|
|
141
|
+
? { comment: response.comment }
|
|
142
|
+
: {}),
|
|
143
|
+
};
|
|
144
|
+
})
|
|
145
|
+
.filter(Boolean)
|
|
146
|
+
: undefined;
|
|
147
|
+
return [
|
|
148
|
+
id,
|
|
149
|
+
{
|
|
150
|
+
status: "accepted",
|
|
151
|
+
...(responses && responses.length === rawResponseCount
|
|
152
|
+
? { responses }
|
|
153
|
+
: {}),
|
|
154
|
+
},
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function sessionView(path, size) {
|
|
159
|
+
const file = await open(path, "r");
|
|
160
|
+
let targetId;
|
|
161
|
+
let assistant;
|
|
162
|
+
let assistantDepth;
|
|
163
|
+
let ask;
|
|
164
|
+
const askResults = new Map();
|
|
165
|
+
let depth = 0;
|
|
166
|
+
let input;
|
|
167
|
+
let inputDepth;
|
|
168
|
+
const resolved = new Set();
|
|
169
|
+
const result = () => ({
|
|
170
|
+
...(assistant ? { assistant } : {}),
|
|
171
|
+
...(ask ? { ask } : {}),
|
|
172
|
+
...(askResults.size > 0
|
|
173
|
+
? { askResults: Object.fromEntries(askResults) }
|
|
174
|
+
: {}),
|
|
175
|
+
...(input ? { input } : {}),
|
|
176
|
+
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
177
|
+
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
178
|
+
: {}),
|
|
179
|
+
});
|
|
180
|
+
const inspect = (line) => {
|
|
181
|
+
if (line.length === 0) return false;
|
|
182
|
+
let entry;
|
|
183
|
+
try {
|
|
184
|
+
entry = JSON.parse(line.toString("utf8"));
|
|
185
|
+
} catch {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
if (typeof entry.id !== "string") return false;
|
|
189
|
+
targetId ??= entry.id;
|
|
190
|
+
if (entry.id !== targetId) return false;
|
|
191
|
+
const currentDepth = depth;
|
|
192
|
+
depth += 1;
|
|
193
|
+
if (entry.type === "custom" && entry.customType === "pi-ask-active") {
|
|
194
|
+
const update = entry.data;
|
|
195
|
+
if (update?.version === 1 && typeof update.id === "string") {
|
|
196
|
+
if (update.state === "closed") resolved.add(update.id);
|
|
197
|
+
else if (
|
|
198
|
+
update.state === "active" &&
|
|
199
|
+
!ask &&
|
|
200
|
+
!resolved.has(update.id)
|
|
201
|
+
) {
|
|
202
|
+
ask = askFromInput(update.id, update);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (
|
|
207
|
+
!input &&
|
|
208
|
+
entry.type === "custom_message" &&
|
|
209
|
+
entry.customType === "herdr-agent-message" &&
|
|
210
|
+
typeof entry.content === "string" &&
|
|
211
|
+
entry.content
|
|
212
|
+
) {
|
|
213
|
+
input = { id: entry.id, text: entry.content };
|
|
214
|
+
inputDepth = currentDepth;
|
|
215
|
+
}
|
|
216
|
+
const message = entry.message;
|
|
217
|
+
if (message && typeof message === "object") {
|
|
218
|
+
const result = askResult(message);
|
|
219
|
+
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
220
|
+
if (
|
|
221
|
+
(message.role === "toolResult" || message.role === "tool") &&
|
|
222
|
+
typeof (message.toolCallId ?? message.tool_call_id) === "string"
|
|
223
|
+
)
|
|
224
|
+
resolved.add(message.toolCallId ?? message.tool_call_id);
|
|
225
|
+
if (!assistant) {
|
|
226
|
+
assistant = assistantFromMessage(entry.id, message);
|
|
227
|
+
if (assistant) assistantDepth = currentDepth;
|
|
228
|
+
}
|
|
229
|
+
if (!input) {
|
|
230
|
+
input = inputFromMessage(entry.id, message);
|
|
231
|
+
if (input) inputDepth = currentDepth;
|
|
232
|
+
}
|
|
233
|
+
if (
|
|
234
|
+
!ask &&
|
|
235
|
+
message.role === "assistant" &&
|
|
236
|
+
Array.isArray(message.content)
|
|
237
|
+
)
|
|
238
|
+
ask = [...message.content]
|
|
239
|
+
.reverse()
|
|
240
|
+
.map(askCall)
|
|
241
|
+
.find(
|
|
242
|
+
(candidate) => candidate && !resolved.has(candidate.toolCallId),
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
if (typeof entry.parentId !== "string") return true;
|
|
246
|
+
targetId = entry.parentId;
|
|
247
|
+
return false;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
try {
|
|
251
|
+
let position = size;
|
|
252
|
+
let partial = Buffer.alloc(0);
|
|
253
|
+
while (position > 0) {
|
|
254
|
+
const length = Math.min(READ_CHUNK_BYTES, position);
|
|
255
|
+
position -= length;
|
|
256
|
+
const chunk = Buffer.allocUnsafe(length);
|
|
257
|
+
const { bytesRead } = await file.read(chunk, 0, length, position);
|
|
258
|
+
const data = Buffer.concat([chunk.subarray(0, bytesRead), partial]);
|
|
259
|
+
let lineEnd = data.length;
|
|
260
|
+
for (let index = data.length - 1; index >= 0; index -= 1) {
|
|
261
|
+
if (data[index] !== 0x0a) continue;
|
|
262
|
+
if (inspect(data.subarray(index + 1, lineEnd))) return result();
|
|
263
|
+
lineEnd = index;
|
|
264
|
+
}
|
|
265
|
+
partial = data.subarray(0, lineEnd);
|
|
266
|
+
}
|
|
267
|
+
inspect(partial);
|
|
268
|
+
return result();
|
|
269
|
+
} finally {
|
|
270
|
+
await file.close();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export async function readSessionView(path) {
|
|
275
|
+
if (!path) return {};
|
|
276
|
+
const expanded = expandHome(path);
|
|
277
|
+
let metadata;
|
|
278
|
+
try {
|
|
279
|
+
metadata = await stat(expanded);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
if (error.code === "ENOENT") return {};
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
const cached = sessionCache.get(expanded);
|
|
285
|
+
if (cached?.size === metadata.size && cached.mtimeMs === metadata.mtimeMs)
|
|
286
|
+
return cached.result;
|
|
287
|
+
const result = await sessionView(expanded, metadata.size);
|
|
288
|
+
sessionCache.set(expanded, {
|
|
289
|
+
size: metadata.size,
|
|
290
|
+
mtimeMs: metadata.mtimeMs,
|
|
291
|
+
result,
|
|
292
|
+
});
|
|
293
|
+
return result;
|
|
294
|
+
}
|
package/src/remote/shepherdr.mjs
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
// @howaboua/pi-shepherdr managed bridge
|
|
2
|
-
import {
|
|
2
|
+
import { readFile, stat } from "node:fs/promises";
|
|
3
3
|
import { createConnection } from "node:net";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
|
-
import {
|
|
5
|
+
import { join, resolve } from "node:path";
|
|
6
6
|
import { sendPeerMessage } from "./shepherdr-peer.mjs";
|
|
7
|
+
import { readSessionView } from "./shepherdr-session.mjs";
|
|
7
8
|
|
|
8
|
-
const BRIDGE_VERSION =
|
|
9
|
+
const BRIDGE_VERSION = 7;
|
|
9
10
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
10
|
-
const READ_CHUNK_BYTES = 64 * 1024;
|
|
11
11
|
const subscriptions = new Map();
|
|
12
|
-
const sessionCache = new Map();
|
|
13
12
|
|
|
14
13
|
function argument(name) {
|
|
15
14
|
const index = process.argv.indexOf(`--${name}`);
|
|
@@ -228,233 +227,6 @@ function subscribe(id, requested) {
|
|
|
228
227
|
});
|
|
229
228
|
}
|
|
230
229
|
|
|
231
|
-
function assistantFromMessage(id, message) {
|
|
232
|
-
if (message.role !== "assistant") return undefined;
|
|
233
|
-
const text = [];
|
|
234
|
-
if (typeof message.content === "string") text.push(message.content);
|
|
235
|
-
else if (Array.isArray(message.content)) {
|
|
236
|
-
for (const part of message.content) {
|
|
237
|
-
if (
|
|
238
|
-
part &&
|
|
239
|
-
typeof part === "object" &&
|
|
240
|
-
part.type === "text" &&
|
|
241
|
-
typeof part.text === "string"
|
|
242
|
-
)
|
|
243
|
-
text.push(part.text);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
const joined = text.join("");
|
|
247
|
-
const stopReason =
|
|
248
|
-
typeof message.stopReason === "string" ? message.stopReason : undefined;
|
|
249
|
-
if (!joined && stopReason !== "error") return undefined;
|
|
250
|
-
return {
|
|
251
|
-
id,
|
|
252
|
-
text: joined,
|
|
253
|
-
...(stopReason ? { stopReason } : {}),
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function inputFromMessage(id, message) {
|
|
258
|
-
if (message.role !== "user") return undefined;
|
|
259
|
-
const text = [];
|
|
260
|
-
if (typeof message.content === "string") text.push(message.content);
|
|
261
|
-
else if (Array.isArray(message.content)) {
|
|
262
|
-
for (const part of message.content) {
|
|
263
|
-
if (
|
|
264
|
-
part &&
|
|
265
|
-
typeof part === "object" &&
|
|
266
|
-
part.type === "text" &&
|
|
267
|
-
typeof part.text === "string"
|
|
268
|
-
)
|
|
269
|
-
text.push(part.text);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
const joined = text.join("");
|
|
273
|
-
return joined ? { id, text: joined } : undefined;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
function askChoice(value) {
|
|
277
|
-
if (!value || typeof value !== "object" || typeof value.label !== "string")
|
|
278
|
-
return undefined;
|
|
279
|
-
return {
|
|
280
|
-
label: value.label,
|
|
281
|
-
...(typeof value.description === "string"
|
|
282
|
-
? { description: value.description }
|
|
283
|
-
: {}),
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function askPrompt(value) {
|
|
288
|
-
if (!value || typeof value !== "object" || typeof value.title !== "string")
|
|
289
|
-
return undefined;
|
|
290
|
-
return {
|
|
291
|
-
title: value.title,
|
|
292
|
-
multiple: value.multiple === true,
|
|
293
|
-
choices: (Array.isArray(value.choices) ? value.choices : [])
|
|
294
|
-
.map(askChoice)
|
|
295
|
-
.filter(Boolean),
|
|
296
|
-
...(typeof value.body === "string" ? { body: value.body } : {}),
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
function askCall(value) {
|
|
301
|
-
if (
|
|
302
|
-
!value ||
|
|
303
|
-
typeof value !== "object" ||
|
|
304
|
-
value.type !== "toolCall" ||
|
|
305
|
-
value.name !== "ask" ||
|
|
306
|
-
typeof value.id !== "string"
|
|
307
|
-
)
|
|
308
|
-
return undefined;
|
|
309
|
-
return askFromInput(value.id, value.arguments);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function askFromInput(toolCallId, input) {
|
|
313
|
-
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
314
|
-
return undefined;
|
|
315
|
-
const prompts = (Array.isArray(input.prompts) ? input.prompts : [])
|
|
316
|
-
.map(askPrompt)
|
|
317
|
-
.filter(Boolean);
|
|
318
|
-
if (prompts.length === 0) return undefined;
|
|
319
|
-
return {
|
|
320
|
-
toolCallId,
|
|
321
|
-
handoff: input.handoff === true,
|
|
322
|
-
prompts,
|
|
323
|
-
};
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
async function sessionView(path, size) {
|
|
327
|
-
const file = await open(path, "r");
|
|
328
|
-
let targetId;
|
|
329
|
-
let assistant;
|
|
330
|
-
let assistantDepth;
|
|
331
|
-
let ask;
|
|
332
|
-
let depth = 0;
|
|
333
|
-
let input;
|
|
334
|
-
let inputDepth;
|
|
335
|
-
const resolved = new Set();
|
|
336
|
-
const result = () => ({
|
|
337
|
-
...(assistant ? { assistant } : {}),
|
|
338
|
-
...(ask ? { ask } : {}),
|
|
339
|
-
...(input ? { input } : {}),
|
|
340
|
-
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
341
|
-
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
342
|
-
: {}),
|
|
343
|
-
});
|
|
344
|
-
const inspect = (line) => {
|
|
345
|
-
if (line.length === 0) return false;
|
|
346
|
-
let entry;
|
|
347
|
-
try {
|
|
348
|
-
entry = JSON.parse(line.toString("utf8"));
|
|
349
|
-
} catch {
|
|
350
|
-
return false;
|
|
351
|
-
}
|
|
352
|
-
if (typeof entry.id !== "string") return false;
|
|
353
|
-
targetId ??= entry.id;
|
|
354
|
-
if (entry.id !== targetId) return false;
|
|
355
|
-
const currentDepth = depth;
|
|
356
|
-
depth += 1;
|
|
357
|
-
if (entry.type === "custom" && entry.customType === "pi-ask-active") {
|
|
358
|
-
const update = entry.data;
|
|
359
|
-
if (update?.version === 1 && typeof update.id === "string") {
|
|
360
|
-
if (update.state === "closed") resolved.add(update.id);
|
|
361
|
-
else if (
|
|
362
|
-
update.state === "active" &&
|
|
363
|
-
!ask &&
|
|
364
|
-
!resolved.has(update.id)
|
|
365
|
-
) {
|
|
366
|
-
ask = askFromInput(update.id, update);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
if (
|
|
371
|
-
!input &&
|
|
372
|
-
entry.type === "custom_message" &&
|
|
373
|
-
entry.customType === "herdr-agent-message" &&
|
|
374
|
-
typeof entry.content === "string" &&
|
|
375
|
-
entry.content
|
|
376
|
-
) {
|
|
377
|
-
input = { id: entry.id, text: entry.content };
|
|
378
|
-
inputDepth = currentDepth;
|
|
379
|
-
}
|
|
380
|
-
const message = entry.message;
|
|
381
|
-
if (message && typeof message === "object") {
|
|
382
|
-
if (
|
|
383
|
-
(message.role === "toolResult" || message.role === "tool") &&
|
|
384
|
-
typeof (message.toolCallId ?? message.tool_call_id) === "string"
|
|
385
|
-
)
|
|
386
|
-
resolved.add(message.toolCallId ?? message.tool_call_id);
|
|
387
|
-
if (!assistant) {
|
|
388
|
-
assistant = assistantFromMessage(entry.id, message);
|
|
389
|
-
if (assistant) assistantDepth = currentDepth;
|
|
390
|
-
}
|
|
391
|
-
if (!input) {
|
|
392
|
-
input = inputFromMessage(entry.id, message);
|
|
393
|
-
if (input) inputDepth = currentDepth;
|
|
394
|
-
}
|
|
395
|
-
if (
|
|
396
|
-
!ask &&
|
|
397
|
-
message.role === "assistant" &&
|
|
398
|
-
Array.isArray(message.content)
|
|
399
|
-
)
|
|
400
|
-
ask = [...message.content]
|
|
401
|
-
.reverse()
|
|
402
|
-
.map(askCall)
|
|
403
|
-
.find(
|
|
404
|
-
(candidate) => candidate && !resolved.has(candidate.toolCallId),
|
|
405
|
-
);
|
|
406
|
-
}
|
|
407
|
-
if (typeof entry.parentId !== "string") return true;
|
|
408
|
-
targetId = entry.parentId;
|
|
409
|
-
return false;
|
|
410
|
-
};
|
|
411
|
-
|
|
412
|
-
try {
|
|
413
|
-
let position = size;
|
|
414
|
-
let partial = Buffer.alloc(0);
|
|
415
|
-
while (position > 0) {
|
|
416
|
-
const length = Math.min(READ_CHUNK_BYTES, position);
|
|
417
|
-
position -= length;
|
|
418
|
-
const chunk = Buffer.allocUnsafe(length);
|
|
419
|
-
const { bytesRead } = await file.read(chunk, 0, length, position);
|
|
420
|
-
const data = Buffer.concat([chunk.subarray(0, bytesRead), partial]);
|
|
421
|
-
let lineEnd = data.length;
|
|
422
|
-
for (let index = data.length - 1; index >= 0; index -= 1) {
|
|
423
|
-
if (data[index] !== 0x0a) continue;
|
|
424
|
-
if (inspect(data.subarray(index + 1, lineEnd))) return result();
|
|
425
|
-
lineEnd = index;
|
|
426
|
-
}
|
|
427
|
-
partial = data.subarray(0, lineEnd);
|
|
428
|
-
}
|
|
429
|
-
inspect(partial);
|
|
430
|
-
return result();
|
|
431
|
-
} finally {
|
|
432
|
-
await file.close();
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
async function view(path) {
|
|
437
|
-
if (!path) return {};
|
|
438
|
-
const expanded = expandHome(path);
|
|
439
|
-
let metadata;
|
|
440
|
-
try {
|
|
441
|
-
metadata = await stat(expanded);
|
|
442
|
-
} catch (error) {
|
|
443
|
-
if (error.code === "ENOENT") return {};
|
|
444
|
-
throw error;
|
|
445
|
-
}
|
|
446
|
-
const cached = sessionCache.get(expanded);
|
|
447
|
-
if (cached?.size === metadata.size && cached.mtimeMs === metadata.mtimeMs)
|
|
448
|
-
return cached.result;
|
|
449
|
-
const result = await sessionView(expanded, metadata.size);
|
|
450
|
-
sessionCache.set(expanded, {
|
|
451
|
-
size: metadata.size,
|
|
452
|
-
mtimeMs: metadata.mtimeMs,
|
|
453
|
-
result,
|
|
454
|
-
});
|
|
455
|
-
return result;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
230
|
async function directory(value, fallback) {
|
|
459
231
|
const base = expandHome(fallback?.trim() || homedir());
|
|
460
232
|
const path = resolve(base, expandHome(value?.trim() || "."));
|
|
@@ -513,7 +285,7 @@ async function handle(message) {
|
|
|
513
285
|
subscriptions.delete(message.subscription);
|
|
514
286
|
return { unsubscribed: true };
|
|
515
287
|
}
|
|
516
|
-
if (message.op === "view") return
|
|
288
|
+
if (message.op === "view") return readSessionView(message.path);
|
|
517
289
|
if (message.op === "keybindings") return keybindings();
|
|
518
290
|
if (message.op === "directory")
|
|
519
291
|
return directory(message.value, message.fallback);
|
package/src/remote-client.ts
CHANGED
|
@@ -14,9 +14,10 @@ import type {
|
|
|
14
14
|
SessionView,
|
|
15
15
|
} from "./types.js";
|
|
16
16
|
|
|
17
|
-
const BRIDGE_VERSION =
|
|
17
|
+
const BRIDGE_VERSION = 7;
|
|
18
18
|
const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
|
|
19
19
|
const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
|
|
20
|
+
const REMOTE_SESSION_HELPER = "~/.pi/agent/shepherdr-session.mjs";
|
|
20
21
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
21
22
|
const MAX_DIAGNOSTIC_BYTES = 8 * 1024;
|
|
22
23
|
const DEPLOY_TIMEOUT_MS = 20_000;
|
|
@@ -193,15 +194,21 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
193
194
|
config: SshMachine,
|
|
194
195
|
onClose: (error: Error) => void,
|
|
195
196
|
): Promise<RemoteHerdrClient> {
|
|
196
|
-
const [source, peerSource] = await Promise.all([
|
|
197
|
+
const [source, peerSource, sessionSource] = await Promise.all([
|
|
197
198
|
readFile(
|
|
198
199
|
fileURLToPath(new URL("./remote/shepherdr.mjs", import.meta.url)),
|
|
199
200
|
),
|
|
200
201
|
readFile(
|
|
201
202
|
fileURLToPath(new URL("./remote/shepherdr-peer.mjs", import.meta.url)),
|
|
202
203
|
),
|
|
204
|
+
readFile(
|
|
205
|
+
fileURLToPath(
|
|
206
|
+
new URL("./remote/shepherdr-session.mjs", import.meta.url),
|
|
207
|
+
),
|
|
208
|
+
),
|
|
203
209
|
]);
|
|
204
210
|
await deploy(config, peerSource, REMOTE_PEER_HELPER);
|
|
211
|
+
await deploy(config, sessionSource, REMOTE_SESSION_HELPER);
|
|
205
212
|
await deploy(config, source, REMOTE_HELPER);
|
|
206
213
|
const child = spawnConnector(config, remoteCommand(config));
|
|
207
214
|
const client = new RemoteHerdrClient(child, onClose);
|
package/src/session-reader.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { open, stat } from "node:fs/promises";
|
|
2
2
|
import type {
|
|
3
|
+
AskResult,
|
|
3
4
|
LatestAssistant,
|
|
4
5
|
LatestInput,
|
|
5
6
|
PendingAsk,
|
|
@@ -131,6 +132,57 @@ function resolvedToolCallId(
|
|
|
131
132
|
return typeof value === "string" ? value : undefined;
|
|
132
133
|
}
|
|
133
134
|
|
|
135
|
+
function askResult(
|
|
136
|
+
message: Record<string, unknown>,
|
|
137
|
+
): [string, AskResult] | undefined {
|
|
138
|
+
if (message["role"] !== "toolResult" || message["toolName"] !== "ask") {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
const id = resolvedToolCallId(message);
|
|
142
|
+
if (!id) return undefined;
|
|
143
|
+
if (message["isError"] === true) return [id, { status: "rejected" }];
|
|
144
|
+
if (message["isError"] !== false) return [id, { status: "unknown" }];
|
|
145
|
+
const details = record(message["details"]);
|
|
146
|
+
if (details?.["dismissed"] === true) return [id, { status: "rejected" }];
|
|
147
|
+
const rawResponses = details?.["responses"];
|
|
148
|
+
const rawResponseCount = Array.isArray(rawResponses)
|
|
149
|
+
? rawResponses.length
|
|
150
|
+
: -1;
|
|
151
|
+
const responses = Array.isArray(rawResponses)
|
|
152
|
+
? rawResponses
|
|
153
|
+
.map((value) => {
|
|
154
|
+
const response = record(value);
|
|
155
|
+
if (
|
|
156
|
+
!response ||
|
|
157
|
+
typeof response["id"] !== "string" ||
|
|
158
|
+
!Array.isArray(response["selections"]) ||
|
|
159
|
+
!response["selections"].every(
|
|
160
|
+
(selection) => typeof selection === "string",
|
|
161
|
+
)
|
|
162
|
+
) {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
id: response["id"],
|
|
167
|
+
selections: response["selections"] as string[],
|
|
168
|
+
...(typeof response["comment"] === "string"
|
|
169
|
+
? { comment: response["comment"] }
|
|
170
|
+
: {}),
|
|
171
|
+
};
|
|
172
|
+
})
|
|
173
|
+
.filter((response) => response !== undefined)
|
|
174
|
+
: undefined;
|
|
175
|
+
return [
|
|
176
|
+
id,
|
|
177
|
+
{
|
|
178
|
+
status: "accepted",
|
|
179
|
+
...(responses && responses.length === rawResponseCount
|
|
180
|
+
? { responses }
|
|
181
|
+
: {}),
|
|
182
|
+
},
|
|
183
|
+
];
|
|
184
|
+
}
|
|
185
|
+
|
|
134
186
|
async function readSessionView(
|
|
135
187
|
path: string,
|
|
136
188
|
size: number,
|
|
@@ -140,6 +192,7 @@ async function readSessionView(
|
|
|
140
192
|
let assistant: LatestAssistant | undefined;
|
|
141
193
|
let assistantDepth: number | undefined;
|
|
142
194
|
let ask: PendingAsk | undefined;
|
|
195
|
+
const askResults = new Map<string, AskResult>();
|
|
143
196
|
let depth = 0;
|
|
144
197
|
let input: LatestInput | undefined;
|
|
145
198
|
let inputDepth: number | undefined;
|
|
@@ -147,6 +200,9 @@ async function readSessionView(
|
|
|
147
200
|
const result = (): SessionView => ({
|
|
148
201
|
...(assistant ? { assistant } : {}),
|
|
149
202
|
...(ask ? { ask } : {}),
|
|
203
|
+
...(askResults.size > 0
|
|
204
|
+
? { askResults: Object.fromEntries(askResults) }
|
|
205
|
+
: {}),
|
|
150
206
|
...(input ? { input } : {}),
|
|
151
207
|
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
152
208
|
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
@@ -192,6 +248,8 @@ async function readSessionView(
|
|
|
192
248
|
}
|
|
193
249
|
const message = record(entry["message"]);
|
|
194
250
|
if (message) {
|
|
251
|
+
const result = askResult(message);
|
|
252
|
+
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
195
253
|
const resolvedId = resolvedToolCallId(message);
|
|
196
254
|
if (resolvedId) resolved.add(resolvedId);
|
|
197
255
|
if (!assistant) {
|