@howaboua/pi-shepherdr 0.1.6 → 0.2.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/CHANGELOG.md +25 -0
- package/README.md +25 -6
- package/index.ts +2 -0
- package/package.json +4 -4
- package/src/agents-contract.ts +11 -6
- package/src/agents-operations.ts +99 -23
- package/src/agents-tool.ts +77 -45
- package/src/controller.ts +4 -8
- package/src/delivery.ts +100 -1
- package/src/fleet.ts +59 -63
- package/src/herdr-client.ts +26 -2
- package/src/herdr.ts +17 -1
- package/src/launch.ts +0 -25
- package/src/machine-catalog.ts +77 -0
- package/src/messages.ts +75 -14
- package/src/monitor-record.ts +6 -19
- package/src/monitor-state.ts +36 -6
- package/src/monitor.ts +14 -9
- package/src/peer-inbox.ts +237 -0
- package/src/remote/shepherdr-peer.mjs +225 -0
- package/src/remote/shepherdr.mjs +33 -13
- package/src/remote-client.ts +55 -21
- package/src/session-reader.ts +21 -11
- package/src/settlement.ts +15 -13
- package/src/types.ts +19 -4
- package/src/widget.ts +4 -4
- package/tsconfig.json +7 -1
- package/src/machine-menu.ts +0 -163
- package/src/machines-config.ts +0 -148
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// @howaboua/pi-shepherdr managed bridge
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import { watch } from "node:fs";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { createConnection } from "node:net";
|
|
6
|
+
import { basename, dirname } from "node:path";
|
|
7
|
+
|
|
8
|
+
export const MAX_PEER_FRAME_BYTES = 8 * 1024 * 1024;
|
|
9
|
+
|
|
10
|
+
/** @param {string} sessionFile @param {string} terminalId */
|
|
11
|
+
export function peerInboxPath(sessionFile, terminalId) {
|
|
12
|
+
const terminal = createHash("sha256")
|
|
13
|
+
.update(terminalId)
|
|
14
|
+
.digest("hex")
|
|
15
|
+
.slice(0, 16);
|
|
16
|
+
return `${sessionFile}.shepherdr-${terminal}.json`;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @param {string} message @param {boolean} rejected */
|
|
20
|
+
function deliveryError(message, rejected) {
|
|
21
|
+
return Object.assign(new Error(message), {
|
|
22
|
+
code: rejected
|
|
23
|
+
? "SHEPHERDR_DELIVERY_REJECTED"
|
|
24
|
+
: "SHEPHERDR_DELIVERY_UNKNOWN",
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @param {unknown} error */
|
|
29
|
+
function isMissing(error) {
|
|
30
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @param {string} path @returns {Promise<string>} */
|
|
34
|
+
async function readReceiver(path) {
|
|
35
|
+
try {
|
|
36
|
+
return await readFile(path, "utf8");
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (!isMissing(error)) throw error;
|
|
39
|
+
}
|
|
40
|
+
// Herdr can report Pi ready before all session_start handlers complete.
|
|
41
|
+
// Wait for receiver publication, never retry a submitted message.
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const watcher = watch(dirname(path));
|
|
44
|
+
let settled = false;
|
|
45
|
+
const finish = () => {
|
|
46
|
+
if (settled) return false;
|
|
47
|
+
settled = true;
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
watcher.close();
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
/** @param {unknown} error */
|
|
53
|
+
const fail = (error) => {
|
|
54
|
+
if (finish()) reject(error);
|
|
55
|
+
};
|
|
56
|
+
const timer = setTimeout(
|
|
57
|
+
() =>
|
|
58
|
+
fail(
|
|
59
|
+
Object.assign(new Error("Receiver not published"), {
|
|
60
|
+
code: "ENOENT",
|
|
61
|
+
}),
|
|
62
|
+
),
|
|
63
|
+
5_000,
|
|
64
|
+
);
|
|
65
|
+
const read = () => {
|
|
66
|
+
void readFile(path, "utf8").then(
|
|
67
|
+
(value) => {
|
|
68
|
+
if (finish()) resolve(value);
|
|
69
|
+
},
|
|
70
|
+
(error) => {
|
|
71
|
+
if (!isMissing(error)) fail(error);
|
|
72
|
+
},
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
watcher.on("error", fail);
|
|
76
|
+
watcher.on("change", (_event, file) => {
|
|
77
|
+
if (file === null || file === basename(path)) read();
|
|
78
|
+
});
|
|
79
|
+
read();
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
85
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
86
|
+
* @param {import("../types.js").PeerMessage} message
|
|
87
|
+
* @returns {Promise<import("../types.js").PeerDelivery>}
|
|
88
|
+
*/
|
|
89
|
+
export async function sendPeerMessage(request, expected, message) {
|
|
90
|
+
const result = await request("agent.get", { target: expected.pane_id });
|
|
91
|
+
const agent =
|
|
92
|
+
result &&
|
|
93
|
+
typeof result === "object" &&
|
|
94
|
+
"agent" in result &&
|
|
95
|
+
result.agent &&
|
|
96
|
+
typeof result.agent === "object"
|
|
97
|
+
? result.agent
|
|
98
|
+
: {};
|
|
99
|
+
const session = "agent_session" in agent ? agent.agent_session : undefined;
|
|
100
|
+
const sessionFile =
|
|
101
|
+
session &&
|
|
102
|
+
typeof session === "object" &&
|
|
103
|
+
"kind" in session &&
|
|
104
|
+
session.kind === "path" &&
|
|
105
|
+
"value" in session &&
|
|
106
|
+
typeof session.value === "string"
|
|
107
|
+
? session.value
|
|
108
|
+
: undefined;
|
|
109
|
+
if (
|
|
110
|
+
!("agent" in agent) ||
|
|
111
|
+
agent.agent !== "pi" ||
|
|
112
|
+
!("terminal_id" in agent) ||
|
|
113
|
+
typeof agent.terminal_id !== "string" ||
|
|
114
|
+
agent.terminal_id !== expected.terminal_id ||
|
|
115
|
+
!sessionFile ||
|
|
116
|
+
(expected.agent_session?.kind === "path" &&
|
|
117
|
+
expected.agent_session.value !== sessionFile)
|
|
118
|
+
) {
|
|
119
|
+
throw deliveryError(
|
|
120
|
+
"Target Pi session changed or is not ready; resolve the target again",
|
|
121
|
+
true,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
if ("agent_status" in agent && agent.agent_status === "blocked") {
|
|
125
|
+
throw deliveryError(
|
|
126
|
+
"Target is blocked; answer its pending question first",
|
|
127
|
+
true,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
let descriptor;
|
|
131
|
+
try {
|
|
132
|
+
descriptor = JSON.parse(
|
|
133
|
+
await readReceiver(peerInboxPath(sessionFile, agent.terminal_id)),
|
|
134
|
+
);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
throw deliveryError(
|
|
137
|
+
isMissing(error)
|
|
138
|
+
? "Target has no native Shepherdr receiver; update and reload Shepherdr in the target Pi session"
|
|
139
|
+
: `Could not read target Shepherdr receiver: ${error instanceof Error ? error.message : String(error)}`,
|
|
140
|
+
true,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
if (
|
|
144
|
+
descriptor?.protocol !== 1 ||
|
|
145
|
+
!Number.isInteger(descriptor.port) ||
|
|
146
|
+
descriptor.port < 1 ||
|
|
147
|
+
descriptor.port > 65535 ||
|
|
148
|
+
typeof descriptor.token !== "string" ||
|
|
149
|
+
!/^[a-f0-9]{64}$/.test(descriptor.token)
|
|
150
|
+
) {
|
|
151
|
+
throw deliveryError(
|
|
152
|
+
"Target Shepherdr receiver is invalid; reload the target Pi session",
|
|
153
|
+
true,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
const id = randomUUID();
|
|
157
|
+
const frame =
|
|
158
|
+
JSON.stringify({
|
|
159
|
+
protocol: 1,
|
|
160
|
+
id,
|
|
161
|
+
token: descriptor.token,
|
|
162
|
+
sessionFile,
|
|
163
|
+
terminalId: agent.terminal_id,
|
|
164
|
+
text: message.text,
|
|
165
|
+
sender: message.sender,
|
|
166
|
+
context: message.context,
|
|
167
|
+
}) + "\n";
|
|
168
|
+
if (Buffer.byteLength(frame) > MAX_PEER_FRAME_BYTES) {
|
|
169
|
+
throw deliveryError("Peer message is too large", true);
|
|
170
|
+
}
|
|
171
|
+
return new Promise((resolve, reject) => {
|
|
172
|
+
let attempted = false;
|
|
173
|
+
let settled = false;
|
|
174
|
+
let buffer = "";
|
|
175
|
+
const socket = createConnection({
|
|
176
|
+
host: "127.0.0.1",
|
|
177
|
+
port: descriptor.port,
|
|
178
|
+
});
|
|
179
|
+
/** @param {unknown} [error] @param {import("../types.js").PeerDelivery} [receipt] */
|
|
180
|
+
const finish = (error, receipt) => {
|
|
181
|
+
if (settled) return;
|
|
182
|
+
settled = true;
|
|
183
|
+
clearTimeout(timer);
|
|
184
|
+
socket.destroy();
|
|
185
|
+
if (error) reject(error);
|
|
186
|
+
else if (receipt) resolve(receipt);
|
|
187
|
+
};
|
|
188
|
+
const failed = () =>
|
|
189
|
+
finish(
|
|
190
|
+
deliveryError(
|
|
191
|
+
attempted
|
|
192
|
+
? "Peer delivery was not acknowledged; inspect the target before retrying"
|
|
193
|
+
: "Target Shepherdr receiver is unavailable; reload the target Pi session",
|
|
194
|
+
!attempted,
|
|
195
|
+
),
|
|
196
|
+
);
|
|
197
|
+
const timer = setTimeout(failed, 10_000);
|
|
198
|
+
timer.unref();
|
|
199
|
+
socket.setEncoding("utf8");
|
|
200
|
+
socket.on("error", failed);
|
|
201
|
+
socket.on("end", failed);
|
|
202
|
+
socket.on("close", failed);
|
|
203
|
+
socket.on("connect", () => {
|
|
204
|
+
attempted = true;
|
|
205
|
+
socket.write(frame);
|
|
206
|
+
});
|
|
207
|
+
socket.on("data", (chunk) => {
|
|
208
|
+
buffer += chunk;
|
|
209
|
+
if (Buffer.byteLength(buffer) > MAX_PEER_FRAME_BYTES) return failed();
|
|
210
|
+
const newline = buffer.indexOf("\n");
|
|
211
|
+
if (newline < 0) return;
|
|
212
|
+
try {
|
|
213
|
+
const reply = JSON.parse(buffer.slice(0, newline));
|
|
214
|
+
if (reply.protocol !== 1 || reply.id !== id) return failed();
|
|
215
|
+
if (reply.ok === true && typeof reply.command === "boolean")
|
|
216
|
+
finish(undefined, { command: reply.command });
|
|
217
|
+
else if (reply.ok === false && typeof reply.error === "string")
|
|
218
|
+
finish(deliveryError(reply.error, reply.rejected === true));
|
|
219
|
+
else failed();
|
|
220
|
+
} catch {
|
|
221
|
+
failed();
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
}
|
package/src/remote/shepherdr.mjs
CHANGED
|
@@ -3,8 +3,9 @@ import { open, readFile, stat } from "node:fs/promises";
|
|
|
3
3
|
import { createConnection } from "node:net";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
5
|
import { dirname, join, resolve } from "node:path";
|
|
6
|
+
import { sendPeerMessage } from "./shepherdr-peer.mjs";
|
|
6
7
|
|
|
7
|
-
const BRIDGE_VERSION =
|
|
8
|
+
const BRIDGE_VERSION = 6;
|
|
8
9
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
9
10
|
const READ_CHUNK_BYTES = 64 * 1024;
|
|
10
11
|
const subscriptions = new Map();
|
|
@@ -24,11 +25,17 @@ function expandHome(path) {
|
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
function socketPath() {
|
|
27
|
-
const explicit = argument("socket");
|
|
28
|
-
if (explicit) return expandHome(explicit);
|
|
29
28
|
const root = process.env.XDG_CONFIG_HOME || join(homedir(), ".config");
|
|
30
29
|
const session = argument("session");
|
|
31
|
-
|
|
30
|
+
if (
|
|
31
|
+
!session ||
|
|
32
|
+
!/^[a-zA-Z0-9_.-]{1,64}$/.test(session) ||
|
|
33
|
+
session === "." ||
|
|
34
|
+
session === ".."
|
|
35
|
+
) {
|
|
36
|
+
throw new Error("A valid Herdr --session is required");
|
|
37
|
+
}
|
|
38
|
+
return session !== "default"
|
|
32
39
|
? join(root, "herdr", "sessions", session, "herdr.sock")
|
|
33
40
|
: join(root, "herdr", "herdr.sock");
|
|
34
41
|
}
|
|
@@ -247,7 +254,7 @@ function assistantFromMessage(id, message) {
|
|
|
247
254
|
};
|
|
248
255
|
}
|
|
249
256
|
|
|
250
|
-
function
|
|
257
|
+
function inputFromMessage(id, message) {
|
|
251
258
|
if (message.role !== "user") return undefined;
|
|
252
259
|
const text = [];
|
|
253
260
|
if (typeof message.content === "string") text.push(message.content);
|
|
@@ -323,15 +330,15 @@ async function sessionView(path, size) {
|
|
|
323
330
|
let assistantDepth;
|
|
324
331
|
let ask;
|
|
325
332
|
let depth = 0;
|
|
326
|
-
let
|
|
327
|
-
let
|
|
333
|
+
let input;
|
|
334
|
+
let inputDepth;
|
|
328
335
|
const resolved = new Set();
|
|
329
336
|
const result = () => ({
|
|
330
337
|
...(assistant ? { assistant } : {}),
|
|
331
338
|
...(ask ? { ask } : {}),
|
|
332
|
-
...(
|
|
333
|
-
...(assistantDepth !== undefined &&
|
|
334
|
-
? {
|
|
339
|
+
...(input ? { input } : {}),
|
|
340
|
+
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
341
|
+
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
335
342
|
: {}),
|
|
336
343
|
});
|
|
337
344
|
const inspect = (line) => {
|
|
@@ -360,6 +367,16 @@ async function sessionView(path, size) {
|
|
|
360
367
|
}
|
|
361
368
|
}
|
|
362
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
|
+
}
|
|
363
380
|
const message = entry.message;
|
|
364
381
|
if (message && typeof message === "object") {
|
|
365
382
|
if (
|
|
@@ -371,9 +388,9 @@ async function sessionView(path, size) {
|
|
|
371
388
|
assistant = assistantFromMessage(entry.id, message);
|
|
372
389
|
if (assistant) assistantDepth = currentDepth;
|
|
373
390
|
}
|
|
374
|
-
if (!
|
|
375
|
-
|
|
376
|
-
if (
|
|
391
|
+
if (!input) {
|
|
392
|
+
input = inputFromMessage(entry.id, message);
|
|
393
|
+
if (input) inputDepth = currentDepth;
|
|
377
394
|
}
|
|
378
395
|
if (
|
|
379
396
|
!ask &&
|
|
@@ -484,6 +501,9 @@ async function handle(message) {
|
|
|
484
501
|
if (message.op === "request") {
|
|
485
502
|
return request(message.method, message.params ?? {}, message.timeoutMs);
|
|
486
503
|
}
|
|
504
|
+
if (message.op === "message") {
|
|
505
|
+
return sendPeerMessage(request, message.agent, message.message);
|
|
506
|
+
}
|
|
487
507
|
if (message.op === "subscribe") {
|
|
488
508
|
await subscribe(message.id, message.subscriptions ?? []);
|
|
489
509
|
return { subscribed: true };
|
package/src/remote-client.ts
CHANGED
|
@@ -3,12 +3,20 @@ import { randomUUID } from "node:crypto";
|
|
|
3
3
|
import { readFile } from "node:fs/promises";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import type { HerdrConnection } from "./herdr-client.js";
|
|
6
|
-
import type {
|
|
6
|
+
import type { SshMachine } from "./machine-catalog.js";
|
|
7
7
|
import type { AssistantReader } from "./session-reader.js";
|
|
8
|
-
import type {
|
|
8
|
+
import type {
|
|
9
|
+
HerdrEvent,
|
|
10
|
+
LatestAssistant,
|
|
11
|
+
PaneInfo,
|
|
12
|
+
PeerDelivery,
|
|
13
|
+
PeerMessage,
|
|
14
|
+
SessionView,
|
|
15
|
+
} from "./types.js";
|
|
9
16
|
|
|
10
|
-
const BRIDGE_VERSION =
|
|
17
|
+
const BRIDGE_VERSION = 6;
|
|
11
18
|
const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
|
|
19
|
+
const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
|
|
12
20
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
13
21
|
const MAX_DIAGNOSTIC_BYTES = 8 * 1024;
|
|
14
22
|
const DEPLOY_TIMEOUT_MS = 20_000;
|
|
@@ -73,26 +81,28 @@ function executablePath(path: string): string {
|
|
|
73
81
|
: shellQuote(path);
|
|
74
82
|
}
|
|
75
83
|
|
|
76
|
-
function remoteCommand(config:
|
|
84
|
+
function remoteCommand(config: SshMachine): string {
|
|
77
85
|
const args = [
|
|
78
86
|
"exec",
|
|
79
|
-
|
|
87
|
+
"node",
|
|
80
88
|
executablePath(REMOTE_HELPER),
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
"--session",
|
|
90
|
+
shellQuote(config.session),
|
|
83
91
|
];
|
|
84
92
|
return args.join(" ");
|
|
85
93
|
}
|
|
86
94
|
|
|
87
95
|
function spawnConnector(
|
|
88
|
-
config:
|
|
96
|
+
config: SshMachine,
|
|
89
97
|
command: string,
|
|
90
98
|
): ChildProcessWithoutNullStreams {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
99
|
+
return spawn(
|
|
100
|
+
"ssh",
|
|
101
|
+
["-T", "-o", "BatchMode=yes", "--", config.target, command],
|
|
102
|
+
{
|
|
103
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
104
|
+
},
|
|
105
|
+
);
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
function appendBounded(current: string, chunk: Buffer): string {
|
|
@@ -103,14 +113,15 @@ function appendBounded(current: string, chunk: Buffer): string {
|
|
|
103
113
|
}
|
|
104
114
|
|
|
105
115
|
async function deploy(
|
|
106
|
-
config:
|
|
116
|
+
config: SshMachine,
|
|
107
117
|
source: Buffer,
|
|
118
|
+
target: string,
|
|
108
119
|
): Promise<void> {
|
|
109
120
|
const command = [
|
|
110
|
-
|
|
121
|
+
"node",
|
|
111
122
|
"-e",
|
|
112
123
|
shellQuote(DEPLOY_SOURCE),
|
|
113
|
-
shellQuote(
|
|
124
|
+
shellQuote(target),
|
|
114
125
|
].join(" ");
|
|
115
126
|
const child = spawnConnector(config, command);
|
|
116
127
|
let stdout = "";
|
|
@@ -179,13 +190,19 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
179
190
|
}
|
|
180
191
|
|
|
181
192
|
static async connect(
|
|
182
|
-
config:
|
|
193
|
+
config: SshMachine,
|
|
183
194
|
onClose: (error: Error) => void,
|
|
184
195
|
): Promise<RemoteHerdrClient> {
|
|
185
|
-
const source = await
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
196
|
+
const [source, peerSource] = await Promise.all([
|
|
197
|
+
readFile(
|
|
198
|
+
fileURLToPath(new URL("./remote/shepherdr.mjs", import.meta.url)),
|
|
199
|
+
),
|
|
200
|
+
readFile(
|
|
201
|
+
fileURLToPath(new URL("./remote/shepherdr-peer.mjs", import.meta.url)),
|
|
202
|
+
),
|
|
203
|
+
]);
|
|
204
|
+
await deploy(config, peerSource, REMOTE_PEER_HELPER);
|
|
205
|
+
await deploy(config, source, REMOTE_HELPER);
|
|
189
206
|
const child = spawnConnector(config, remoteCommand(config));
|
|
190
207
|
const client = new RemoteHerdrClient(child, onClose);
|
|
191
208
|
try {
|
|
@@ -208,6 +225,23 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
208
225
|
)) as T;
|
|
209
226
|
}
|
|
210
227
|
|
|
228
|
+
async sendMessage(
|
|
229
|
+
agent: PaneInfo,
|
|
230
|
+
message: PeerMessage,
|
|
231
|
+
): Promise<PeerDelivery> {
|
|
232
|
+
const result = await this.call({ op: "message", agent, message }, 26_000);
|
|
233
|
+
if (
|
|
234
|
+
!result ||
|
|
235
|
+
typeof result !== "object" ||
|
|
236
|
+
!("command" in result) ||
|
|
237
|
+
typeof result.command !== "boolean"
|
|
238
|
+
)
|
|
239
|
+
throw new Error(
|
|
240
|
+
"Invalid peer acknowledgement; inspect the target before retrying",
|
|
241
|
+
);
|
|
242
|
+
return { command: result.command };
|
|
243
|
+
}
|
|
244
|
+
|
|
211
245
|
async subscribe(
|
|
212
246
|
subscriptions: object[],
|
|
213
247
|
onEvent: (event: HerdrEvent) => void,
|
package/src/session-reader.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { open, stat } from "node:fs/promises";
|
|
2
2
|
import type {
|
|
3
3
|
LatestAssistant,
|
|
4
|
-
|
|
4
|
+
LatestInput,
|
|
5
5
|
PendingAsk,
|
|
6
6
|
SessionView,
|
|
7
7
|
} from "./types.js";
|
|
@@ -43,10 +43,10 @@ function assistantFromMessage(
|
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
function
|
|
46
|
+
function inputFromMessage(
|
|
47
47
|
id: string,
|
|
48
48
|
message: Record<string, unknown>,
|
|
49
|
-
):
|
|
49
|
+
): LatestInput | undefined {
|
|
50
50
|
if (message["role"] !== "user") return undefined;
|
|
51
51
|
const text: string[] = [];
|
|
52
52
|
if (typeof message["content"] === "string") {
|
|
@@ -141,15 +141,15 @@ async function readSessionView(
|
|
|
141
141
|
let assistantDepth: number | undefined;
|
|
142
142
|
let ask: PendingAsk | undefined;
|
|
143
143
|
let depth = 0;
|
|
144
|
-
let
|
|
145
|
-
let
|
|
144
|
+
let input: LatestInput | undefined;
|
|
145
|
+
let inputDepth: number | undefined;
|
|
146
146
|
const resolved = new Set<string>();
|
|
147
147
|
const result = (): SessionView => ({
|
|
148
148
|
...(assistant ? { assistant } : {}),
|
|
149
149
|
...(ask ? { ask } : {}),
|
|
150
|
-
...(
|
|
151
|
-
...(assistantDepth !== undefined &&
|
|
152
|
-
? {
|
|
150
|
+
...(input ? { input } : {}),
|
|
151
|
+
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
152
|
+
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
153
153
|
: {}),
|
|
154
154
|
});
|
|
155
155
|
const inspect = (line: Buffer): boolean => {
|
|
@@ -180,6 +180,16 @@ async function readSessionView(
|
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
|
+
if (
|
|
184
|
+
!input &&
|
|
185
|
+
entry["type"] === "custom_message" &&
|
|
186
|
+
entry["customType"] === "herdr-agent-message" &&
|
|
187
|
+
typeof entry["content"] === "string" &&
|
|
188
|
+
entry["content"]
|
|
189
|
+
) {
|
|
190
|
+
input = { id, text: entry["content"] };
|
|
191
|
+
inputDepth = currentDepth;
|
|
192
|
+
}
|
|
183
193
|
const message = record(entry["message"]);
|
|
184
194
|
if (message) {
|
|
185
195
|
const resolvedId = resolvedToolCallId(message);
|
|
@@ -188,9 +198,9 @@ async function readSessionView(
|
|
|
188
198
|
assistant = assistantFromMessage(id, message);
|
|
189
199
|
if (assistant) assistantDepth = currentDepth;
|
|
190
200
|
}
|
|
191
|
-
if (!
|
|
192
|
-
|
|
193
|
-
if (
|
|
201
|
+
if (!input) {
|
|
202
|
+
input = inputFromMessage(id, message);
|
|
203
|
+
if (input) inputDepth = currentDepth;
|
|
194
204
|
}
|
|
195
205
|
if (
|
|
196
206
|
!ask &&
|
package/src/settlement.ts
CHANGED
|
@@ -179,8 +179,11 @@ export class SettlementReporter {
|
|
|
179
179
|
private readonly collector: SettlementCollector;
|
|
180
180
|
private readonly persist: () => void;
|
|
181
181
|
private readonly pi: ExtensionAPI;
|
|
182
|
-
private readonly
|
|
183
|
-
|
|
182
|
+
private readonly origin: {
|
|
183
|
+
machine: string;
|
|
184
|
+
label: () => string;
|
|
185
|
+
operatorPrefix: string;
|
|
186
|
+
};
|
|
184
187
|
private readonly reporting = new Map<string, SettledAgentStatus>();
|
|
185
188
|
private readonly pendingBlocked = new Map<string, PendingBlockedSettlement>();
|
|
186
189
|
private readonly claims = new Map<string, SettlementClaim>();
|
|
@@ -192,16 +195,14 @@ export class SettlementReporter {
|
|
|
192
195
|
client: HerdrConnection,
|
|
193
196
|
state: MonitorState,
|
|
194
197
|
persist: () => void,
|
|
195
|
-
reader
|
|
196
|
-
machine
|
|
197
|
-
operatorPrefix = "herdr",
|
|
198
|
+
reader: AssistantReader | undefined,
|
|
199
|
+
origin: { machine: string; label: () => string; operatorPrefix: string },
|
|
198
200
|
) {
|
|
199
201
|
this.pi = pi;
|
|
200
202
|
this.collector = new SettlementCollector(client, reader);
|
|
201
203
|
this.state = state;
|
|
202
204
|
this.persist = persist;
|
|
203
|
-
this.
|
|
204
|
-
this.operatorPrefix = operatorPrefix;
|
|
205
|
+
this.origin = origin;
|
|
205
206
|
}
|
|
206
207
|
|
|
207
208
|
latest(panel: PaneInfo): Promise<LatestAssistant | undefined> {
|
|
@@ -328,15 +329,15 @@ export class SettlementReporter {
|
|
|
328
329
|
if (
|
|
329
330
|
!blocked &&
|
|
330
331
|
expectedUser &&
|
|
331
|
-
(settlement.session.
|
|
332
|
-
!settlement.session.
|
|
333
|
-
settlement.session.
|
|
332
|
+
(settlement.session.assistantAfterInput !== true ||
|
|
333
|
+
!settlement.session.input ||
|
|
334
|
+
settlement.session.input.id === expectedUser.after)
|
|
334
335
|
) {
|
|
335
336
|
if (!this.retry(lifecycle, retryRequest, retryState)) {
|
|
336
337
|
this.failReconciliation(
|
|
337
338
|
lifecycle,
|
|
338
339
|
current,
|
|
339
|
-
"the submitted
|
|
340
|
+
"the submitted input and reply were not persisted",
|
|
340
341
|
);
|
|
341
342
|
}
|
|
342
343
|
return;
|
|
@@ -466,8 +467,9 @@ export class SettlementReporter {
|
|
|
466
467
|
agent: settlement.agent,
|
|
467
468
|
agentToolName: "agents",
|
|
468
469
|
...(settlement.ask ? { ask: settlement.ask } : {}),
|
|
469
|
-
machine: this.machine,
|
|
470
|
-
|
|
470
|
+
machine: this.origin.machine,
|
|
471
|
+
machineLabel: this.origin.label(),
|
|
472
|
+
operatorPrefix: this.origin.operatorPrefix,
|
|
471
473
|
...(blockedMessage ? { blockedMessage } : {}),
|
|
472
474
|
labels: settlement.labels,
|
|
473
475
|
record,
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
export type AgentStatus = "idle" | "working" | "blocked" | "done" | "unknown";
|
|
2
2
|
|
|
3
|
+
export interface PeerMessage {
|
|
4
|
+
text: string;
|
|
5
|
+
sender: string;
|
|
6
|
+
context?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface PeerDelivery {
|
|
10
|
+
command: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
3
13
|
export type SettledAgentStatus = Exclude<AgentStatus, "working">;
|
|
4
14
|
|
|
5
15
|
export type StableAgentActivity =
|
|
@@ -39,6 +49,7 @@ export interface PaneInfo {
|
|
|
39
49
|
launch_pending?: boolean;
|
|
40
50
|
name?: string | null;
|
|
41
51
|
pane_id: string;
|
|
52
|
+
title?: string | null;
|
|
42
53
|
tab_id: string;
|
|
43
54
|
terminal_id: string;
|
|
44
55
|
workspace_id: string;
|
|
@@ -63,6 +74,7 @@ export interface SessionSnapshot {
|
|
|
63
74
|
}
|
|
64
75
|
|
|
65
76
|
export interface MonitoredAgent {
|
|
77
|
+
readonly scope: "task" | "persistent";
|
|
66
78
|
readonly activity: AgentActivity;
|
|
67
79
|
readonly cwd?: string;
|
|
68
80
|
readonly lastAssistantId?: string;
|
|
@@ -82,7 +94,10 @@ export interface MonitoringIssue {
|
|
|
82
94
|
|
|
83
95
|
export interface MachineStatus {
|
|
84
96
|
local: boolean;
|
|
85
|
-
|
|
97
|
+
id: string;
|
|
98
|
+
label?: string;
|
|
99
|
+
target?: string;
|
|
100
|
+
session?: string;
|
|
86
101
|
monitoringIssue?: MonitoringIssue;
|
|
87
102
|
reason?: string;
|
|
88
103
|
status: MachineConnectionStatus;
|
|
@@ -96,7 +111,7 @@ export interface LatestAssistant {
|
|
|
96
111
|
text: string;
|
|
97
112
|
}
|
|
98
113
|
|
|
99
|
-
export interface
|
|
114
|
+
export interface LatestInput {
|
|
100
115
|
id: string;
|
|
101
116
|
text: string;
|
|
102
117
|
}
|
|
@@ -115,8 +130,8 @@ export interface PendingAsk {
|
|
|
115
130
|
export interface SessionView {
|
|
116
131
|
ask?: PendingAsk;
|
|
117
132
|
assistant?: LatestAssistant;
|
|
118
|
-
|
|
119
|
-
|
|
133
|
+
assistantAfterInput?: boolean;
|
|
134
|
+
input?: LatestInput;
|
|
120
135
|
}
|
|
121
136
|
|
|
122
137
|
export interface HerdrEvent {
|
package/src/widget.ts
CHANGED
|
@@ -42,7 +42,7 @@ function agentLine(
|
|
|
42
42
|
? ({ icon: "?", tone: "warning" } as const)
|
|
43
43
|
: statusAppearance(status)
|
|
44
44
|
: ({ icon: "?", tone: "error" } as const);
|
|
45
|
-
const name = `${agent.machine} / ${agent.name ?? agent.paneId}`;
|
|
45
|
+
const name = `${machine?.label ?? agent.machine} / ${agent.name ?? agent.paneId}`;
|
|
46
46
|
const location = agent.cwd ? basename(agent.cwd) : agent.paneId;
|
|
47
47
|
return [
|
|
48
48
|
theme.fg("muted", "│"),
|
|
@@ -76,7 +76,7 @@ export function renderAgentWidget(
|
|
|
76
76
|
(left.name ?? left.paneId).localeCompare(right.name ?? right.paneId),
|
|
77
77
|
);
|
|
78
78
|
const machinesByName = new Map(
|
|
79
|
-
machines.map((machine) => [machine.
|
|
79
|
+
machines.map((machine) => [machine.id, machine]),
|
|
80
80
|
);
|
|
81
81
|
const lines = [
|
|
82
82
|
`${theme.fg("accent", "╭─ herdr agents")} ${theme.fg("dim", `${agents.length} · ${machines.filter((machine) => machine.status === "connected").length}/${machines.length} machines`)}`,
|
|
@@ -97,9 +97,9 @@ export function renderAgentWidget(
|
|
|
97
97
|
: "unavailable";
|
|
98
98
|
const recovery = machine.local
|
|
99
99
|
? "retrying"
|
|
100
|
-
: `/herdr connect ${machine.
|
|
100
|
+
: `/herdr connect ${machine.id}`;
|
|
101
101
|
lines.push(
|
|
102
|
-
`${theme.fg("muted", "│")} ${theme.fg("warning", `${machine.
|
|
102
|
+
`${theme.fg("muted", "│")} ${theme.fg("warning", `${machine.id} monitoring ${status} · ${recovery}`)}`,
|
|
103
103
|
);
|
|
104
104
|
}
|
|
105
105
|
lines.push(theme.fg("muted", "╰─"));
|