@howaboua/pi-shepherdr 0.2.15 → 0.2.17
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 +42 -33
- package/index.ts +16 -26
- package/package.json +11 -11
- package/src/agents-contract.ts +36 -8
- package/src/agents-discovery.ts +23 -7
- package/src/agents-focus.ts +242 -0
- package/src/agents-spawn.ts +23 -0
- package/src/agents-tool.ts +46 -6
- package/src/attachment-protocol.ts +7 -3
- package/src/attachment-target.ts +3 -1
- package/src/attachment.ts +3 -1
- package/src/board/host.ts +98 -366
- package/src/board/membership.ts +14 -8
- package/src/command-config.ts +51 -0
- package/src/control-panel-actions.ts +0 -20
- package/src/control-panel-items.ts +8 -36
- package/src/control-panel-view.ts +2 -11
- package/src/control-panel.ts +2 -11
- package/src/controller.ts +5 -5
- package/src/delivery.ts +0 -33
- package/src/herdr-client.ts +52 -6
- package/src/messages.ts +61 -61
- package/src/monitor-events.ts +24 -2
- package/src/monitor-questions.ts +12 -3
- package/src/monitor.ts +2 -2
- package/src/peer-channel.ts +39 -0
- package/src/peer-commands.ts +187 -0
- package/src/peer-inbox.ts +232 -55
- package/src/remote/shepherdr-channel.mjs +225 -0
- package/src/remote/shepherdr-checkpoints.mjs +5 -3
- package/src/remote/shepherdr-peer.mjs +198 -22
- package/src/remote/shepherdr.mjs +63 -8
- package/src/remote-client.ts +121 -11
- package/src/settlement.ts +1 -0
- package/src/shared-context.ts +104 -1
- package/src/types.ts +29 -0
- package/src/voice-handoff.ts +177 -0
- package/src/board/archive.ts +0 -77
- package/src/board/awareness.ts +0 -115
- package/src/board/config.ts +0 -121
- package/src/board/contract.ts +0 -181
- package/src/board/identity.ts +0 -169
- package/src/board/paging.ts +0 -55
- package/src/board/protocol.ts +0 -73
- package/src/board/queries.ts +0 -176
- package/src/board/response.ts +0 -56
- package/src/board/store.ts +0 -327
- package/src/board/tool.ts +0 -23
- package/src/board/turns.ts +0 -100
- package/src/context-briefing.ts +0 -226
package/src/remote-client.ts
CHANGED
|
@@ -4,19 +4,23 @@ import { readFile } from "node:fs/promises";
|
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
5
|
import type { HerdrConnection } from "./herdr-client.js";
|
|
6
6
|
import type { SshMachine } from "./machine-catalog.js";
|
|
7
|
+
import { createBufferedChannel } from "./remote/shepherdr-channel.mjs";
|
|
8
|
+
import { readPeerCommands } from "./remote/shepherdr-peer.mjs";
|
|
7
9
|
import type { AssistantReader } from "./session-reader.js";
|
|
8
10
|
import type {
|
|
9
11
|
HerdrEvent,
|
|
10
12
|
LatestAssistant,
|
|
11
13
|
PaneInfo,
|
|
14
|
+
PeerChannel,
|
|
12
15
|
PeerDelivery,
|
|
13
16
|
PeerMessage,
|
|
14
17
|
SessionView,
|
|
15
18
|
} from "./types.js";
|
|
16
19
|
|
|
17
|
-
const BRIDGE_VERSION =
|
|
20
|
+
const BRIDGE_VERSION = 19;
|
|
18
21
|
const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
|
|
19
22
|
const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
|
|
23
|
+
const REMOTE_CHANNEL_HELPER = "~/.pi/agent/shepherdr-channel.mjs";
|
|
20
24
|
const REMOTE_SESSION_HELPER = "~/.pi/agent/shepherdr-session.mjs";
|
|
21
25
|
const REMOTE_SESSION_WATCH_HELPER = "~/.pi/agent/shepherdr-session-watch.mjs";
|
|
22
26
|
const REMOTE_CHECKPOINT_HELPER = "~/.pi/agent/shepherdr-checkpoints.mjs";
|
|
@@ -61,7 +65,7 @@ process.stdin.on("end", () => { void (async () => {
|
|
|
61
65
|
interface PendingCall {
|
|
62
66
|
reject: (error: Error) => void;
|
|
63
67
|
resolve: (value: unknown) => void;
|
|
64
|
-
timer: NodeJS.Timeout;
|
|
68
|
+
timer: NodeJS.Timeout | undefined;
|
|
65
69
|
cleanup?: () => void;
|
|
66
70
|
}
|
|
67
71
|
|
|
@@ -176,6 +180,10 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
176
180
|
private readonly onClose: (error: Error) => void;
|
|
177
181
|
private readonly pending = new Map<string, PendingCall>();
|
|
178
182
|
private readonly subscriptions = new Map<string, SubscriptionCallbacks>();
|
|
183
|
+
private readonly channels = new Map<
|
|
184
|
+
string,
|
|
185
|
+
ReturnType<typeof createBufferedChannel>
|
|
186
|
+
>();
|
|
179
187
|
|
|
180
188
|
private constructor(
|
|
181
189
|
child: ChildProcessWithoutNullStreams,
|
|
@@ -206,6 +214,7 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
206
214
|
const [
|
|
207
215
|
source,
|
|
208
216
|
peerSource,
|
|
217
|
+
channelSource,
|
|
209
218
|
sessionSource,
|
|
210
219
|
contextSource,
|
|
211
220
|
checkpointSource,
|
|
@@ -217,6 +226,11 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
217
226
|
readFile(
|
|
218
227
|
fileURLToPath(new URL("./remote/shepherdr-peer.mjs", import.meta.url)),
|
|
219
228
|
),
|
|
229
|
+
readFile(
|
|
230
|
+
fileURLToPath(
|
|
231
|
+
new URL("./remote/shepherdr-channel.mjs", import.meta.url),
|
|
232
|
+
),
|
|
233
|
+
),
|
|
220
234
|
readFile(
|
|
221
235
|
fileURLToPath(
|
|
222
236
|
new URL("./remote/shepherdr-session.mjs", import.meta.url),
|
|
@@ -239,6 +253,7 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
239
253
|
),
|
|
240
254
|
]);
|
|
241
255
|
await deploy(config, peerSource, REMOTE_PEER_HELPER);
|
|
256
|
+
await deploy(config, channelSource, REMOTE_CHANNEL_HELPER);
|
|
242
257
|
await deploy(config, sessionSource, REMOTE_SESSION_HELPER);
|
|
243
258
|
await deploy(config, sessionWatchSource, REMOTE_SESSION_WATCH_HELPER);
|
|
244
259
|
await deploy(config, checkpointSource, REMOTE_CHECKPOINT_HELPER);
|
|
@@ -295,11 +310,19 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
295
310
|
return this.relayPath;
|
|
296
311
|
}
|
|
297
312
|
|
|
313
|
+
async commands(agent: PaneInfo): Promise<import("./types.js").PeerCommand[]> {
|
|
314
|
+
const result = await this.call({ op: "commands", agent }, 26_000);
|
|
315
|
+
return readPeerCommands(result);
|
|
316
|
+
}
|
|
317
|
+
|
|
298
318
|
async sendMessage(
|
|
299
319
|
agent: PaneInfo,
|
|
300
320
|
message: PeerMessage,
|
|
301
321
|
): Promise<PeerDelivery> {
|
|
302
|
-
const result = await this.call(
|
|
322
|
+
const result = await this.call(
|
|
323
|
+
{ op: "message", agent, message },
|
|
324
|
+
message.text.trim() === "/quit" ? 0 : 26_000,
|
|
325
|
+
);
|
|
303
326
|
if (
|
|
304
327
|
!result ||
|
|
305
328
|
typeof result !== "object" ||
|
|
@@ -312,6 +335,73 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
312
335
|
return { command: result.command };
|
|
313
336
|
}
|
|
314
337
|
|
|
338
|
+
async machineIdentity(): Promise<import("./types.js").MachineIdentity> {
|
|
339
|
+
const result = await this.call({ op: "identity" });
|
|
340
|
+
if (
|
|
341
|
+
!result ||
|
|
342
|
+
typeof result !== "object" ||
|
|
343
|
+
!("host" in result) ||
|
|
344
|
+
typeof result.host !== "string" ||
|
|
345
|
+
!("session" in result) ||
|
|
346
|
+
typeof result.session !== "string"
|
|
347
|
+
)
|
|
348
|
+
throw new Error("Could not identify remote Herdr session");
|
|
349
|
+
return { host: result.host, session: result.session };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async openFocusChannel(
|
|
353
|
+
agent: PaneInfo,
|
|
354
|
+
request: import("./types.js").FocusRequest,
|
|
355
|
+
): Promise<PeerChannel> {
|
|
356
|
+
const id = randomUUID();
|
|
357
|
+
const state = createBufferedChannel(
|
|
358
|
+
(message) => {
|
|
359
|
+
const frame = `${JSON.stringify({ op: "channel_send", id, message })}\n`;
|
|
360
|
+
if (
|
|
361
|
+
this.closed ||
|
|
362
|
+
this.child.stdin.writableLength + Buffer.byteLength(frame) >
|
|
363
|
+
1024 * 1024
|
|
364
|
+
)
|
|
365
|
+
throw new Error(
|
|
366
|
+
"Focus channel output is unavailable or exceeded its buffer",
|
|
367
|
+
);
|
|
368
|
+
this.child.stdin.write(frame);
|
|
369
|
+
},
|
|
370
|
+
() => {
|
|
371
|
+
this.channels.delete(id);
|
|
372
|
+
if (!this.closed)
|
|
373
|
+
this.child.stdin.write(
|
|
374
|
+
`${JSON.stringify({ op: "channel_close", id })}\n`,
|
|
375
|
+
);
|
|
376
|
+
},
|
|
377
|
+
);
|
|
378
|
+
this.channels.set(id, state);
|
|
379
|
+
try {
|
|
380
|
+
const result = await this.call(
|
|
381
|
+
{ op: "channel_open", id, agent, request },
|
|
382
|
+
60_000,
|
|
383
|
+
);
|
|
384
|
+
if (
|
|
385
|
+
!result ||
|
|
386
|
+
typeof result !== "object" ||
|
|
387
|
+
!("admitted" in result) ||
|
|
388
|
+
result.admitted !== true
|
|
389
|
+
)
|
|
390
|
+
throw new Error("Focus channel was not admitted");
|
|
391
|
+
return state.channel;
|
|
392
|
+
} catch (error) {
|
|
393
|
+
state.fail(error instanceof Error ? error : new Error(String(error)));
|
|
394
|
+
throw error;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
focus(
|
|
399
|
+
agent: PaneInfo,
|
|
400
|
+
request: import("./types.js").FocusRequest,
|
|
401
|
+
): Promise<unknown> {
|
|
402
|
+
return this.call({ op: "focus", agent, request }, 60_000);
|
|
403
|
+
}
|
|
404
|
+
|
|
315
405
|
async subscribe(
|
|
316
406
|
subscriptions: object[],
|
|
317
407
|
onEvent: (event: HerdrEvent) => void,
|
|
@@ -432,14 +522,18 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
432
522
|
);
|
|
433
523
|
const id = typeof message["id"] === "string" ? message["id"] : randomUUID();
|
|
434
524
|
return new Promise((resolve, reject) => {
|
|
435
|
-
const timer =
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
525
|
+
const timer =
|
|
526
|
+
timeoutMs === 0
|
|
527
|
+
? undefined
|
|
528
|
+
: setTimeout(() => {
|
|
529
|
+
const error = new Error(
|
|
530
|
+
`remote Shepherdr ${String(message["op"])} timed out`,
|
|
531
|
+
);
|
|
532
|
+
if (message["op"] === "context_relay")
|
|
533
|
+
this.rejectPending(id, error);
|
|
534
|
+
else this.disconnected(error);
|
|
535
|
+
}, timeoutMs);
|
|
536
|
+
timer?.unref();
|
|
443
537
|
const abort = () => {
|
|
444
538
|
if (!this.pending.has(id)) return;
|
|
445
539
|
this.rejectPending(
|
|
@@ -498,6 +592,20 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
498
592
|
this.resolvePending("ready", value);
|
|
499
593
|
continue;
|
|
500
594
|
}
|
|
595
|
+
if (value["type"] === "channel" && typeof value["channel"] === "string") {
|
|
596
|
+
const state = this.channels.get(value["channel"]);
|
|
597
|
+
if (value["closed"] === true)
|
|
598
|
+
state?.fail(
|
|
599
|
+
new Error(
|
|
600
|
+
typeof value["error"] === "string"
|
|
601
|
+
? value["error"]
|
|
602
|
+
: "Focus channel disconnected",
|
|
603
|
+
),
|
|
604
|
+
);
|
|
605
|
+
else if ("message" in value) state?.receive(value["message"]);
|
|
606
|
+
else state?.fail(new Error("Invalid focus channel frame"));
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
501
609
|
const subscription = value["subscription"];
|
|
502
610
|
if (typeof subscription === "string") {
|
|
503
611
|
const callbacks = this.subscriptions.get(subscription);
|
|
@@ -595,6 +703,8 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
595
703
|
}
|
|
596
704
|
|
|
597
705
|
private failPending(error: Error, notify: boolean): void {
|
|
706
|
+
for (const state of this.channels.values()) state.fail(error);
|
|
707
|
+
this.channels.clear();
|
|
598
708
|
for (const controller of this.incomingContext.values()) controller.abort();
|
|
599
709
|
this.incomingContext.clear();
|
|
600
710
|
for (const pending of this.pending.values()) {
|
package/src/settlement.ts
CHANGED
|
@@ -400,6 +400,7 @@ export class SettlementReporter {
|
|
|
400
400
|
}
|
|
401
401
|
} catch (error) {
|
|
402
402
|
if (!lifecycle.isCurrent()) return;
|
|
403
|
+
if (!this.state.byTerminal(request.record.terminalId)) return;
|
|
403
404
|
if (request.status === "blocked") {
|
|
404
405
|
const current = this.state.byTerminal(request.record.terminalId);
|
|
405
406
|
if (current) {
|
package/src/shared-context.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type {
|
|
|
3
3
|
ExtensionAPI,
|
|
4
4
|
ExtensionContext,
|
|
5
5
|
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { binding, isBoardEnvelope } from "@howaboua/pi-agent-board/integration";
|
|
6
7
|
import type {
|
|
7
8
|
ContextSharingService,
|
|
8
9
|
SharedContextRequest,
|
|
@@ -16,7 +17,6 @@ import {
|
|
|
16
17
|
Identity,
|
|
17
18
|
} from "./attachment-protocol.js";
|
|
18
19
|
import type { AgentBoard } from "./board/host.js";
|
|
19
|
-
import { isBoardEnvelope } from "./board/protocol.js";
|
|
20
20
|
import type { AgentFleet, ConnectedMachine } from "./fleet.js";
|
|
21
21
|
import { sessionPath } from "./herdr.js";
|
|
22
22
|
import {
|
|
@@ -76,6 +76,58 @@ export class SharedAgentContext {
|
|
|
76
76
|
value: unknown,
|
|
77
77
|
signal?: AbortSignal,
|
|
78
78
|
): Promise<unknown> {
|
|
79
|
+
if (
|
|
80
|
+
value &&
|
|
81
|
+
typeof value === "object" &&
|
|
82
|
+
"operation" in value &&
|
|
83
|
+
value.operation === "agent-root"
|
|
84
|
+
) {
|
|
85
|
+
const own = binding(ctx);
|
|
86
|
+
const identity = this.getService()?.describe(ctx);
|
|
87
|
+
if (
|
|
88
|
+
!("scope" in value) ||
|
|
89
|
+
(value.scope !== "board" && value.scope !== "context")
|
|
90
|
+
)
|
|
91
|
+
throw new Error("Invalid /root family");
|
|
92
|
+
const family =
|
|
93
|
+
value.scope === "board" ? own.rootSessionId : identity?.sessionId;
|
|
94
|
+
if (
|
|
95
|
+
!("family" in value) ||
|
|
96
|
+
value.family !== family ||
|
|
97
|
+
!("visited" in value) ||
|
|
98
|
+
!Array.isArray(value.visited) ||
|
|
99
|
+
value.visited.some((item) => typeof item !== "string") ||
|
|
100
|
+
value.visited.length >= 64 ||
|
|
101
|
+
value.visited.includes(ctx.sessionManager.getSessionId())
|
|
102
|
+
)
|
|
103
|
+
throw new Error("No live /root in this agent family");
|
|
104
|
+
const routing = identity?.routing as Partial<Routing> | undefined;
|
|
105
|
+
const upstream =
|
|
106
|
+
value.scope === "board"
|
|
107
|
+
? own.upstream
|
|
108
|
+
: identity?.agentName !== "/root" &&
|
|
109
|
+
routing?.transport === "shepherdr"
|
|
110
|
+
? routing.parent
|
|
111
|
+
: undefined;
|
|
112
|
+
if (upstream)
|
|
113
|
+
return requestContext(
|
|
114
|
+
upstream,
|
|
115
|
+
{
|
|
116
|
+
...value,
|
|
117
|
+
visited: [...value.visited, ctx.sessionManager.getSessionId()],
|
|
118
|
+
},
|
|
119
|
+
signal,
|
|
120
|
+
);
|
|
121
|
+
if (
|
|
122
|
+
(value.scope === "board" ? own.agentName : identity?.agentName) !==
|
|
123
|
+
"/root"
|
|
124
|
+
)
|
|
125
|
+
throw new Error("No live /root in this agent family");
|
|
126
|
+
return {
|
|
127
|
+
...(await this.fleet.connected().client.machineIdentity()),
|
|
128
|
+
sessionFile: ctx.sessionManager.getSessionFile(),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
79
131
|
if (isBoardEnvelope(value)) return this.board.handle(ctx, value, signal);
|
|
80
132
|
if (!value || typeof value !== "object")
|
|
81
133
|
throw new Error("Invalid shared context request");
|
|
@@ -187,6 +239,57 @@ export class SharedAgentContext {
|
|
|
187
239
|
);
|
|
188
240
|
}
|
|
189
241
|
|
|
242
|
+
async resolveRoot(
|
|
243
|
+
ctx: ExtensionContext,
|
|
244
|
+
machine?: string,
|
|
245
|
+
signal?: AbortSignal,
|
|
246
|
+
) {
|
|
247
|
+
const own = binding(ctx);
|
|
248
|
+
const identity = this.getService()?.describe(ctx);
|
|
249
|
+
const scope = own.upstream || !identity ? "board" : "context";
|
|
250
|
+
const family = scope === "board" ? own.rootSessionId : identity?.sessionId;
|
|
251
|
+
let result: unknown;
|
|
252
|
+
try {
|
|
253
|
+
result = await this.handle(
|
|
254
|
+
ctx,
|
|
255
|
+
{ operation: "agent-root", scope, family, visited: [] },
|
|
256
|
+
signal,
|
|
257
|
+
);
|
|
258
|
+
} catch (error) {
|
|
259
|
+
signal?.throwIfAborted();
|
|
260
|
+
throw new Error(
|
|
261
|
+
"No live /root in this agent family; use find for an exact target",
|
|
262
|
+
{ cause: error },
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
if (
|
|
266
|
+
!result ||
|
|
267
|
+
typeof result !== "object" ||
|
|
268
|
+
!("host" in result) ||
|
|
269
|
+
!("session" in result) ||
|
|
270
|
+
!("sessionFile" in result) ||
|
|
271
|
+
typeof result.sessionFile !== "string"
|
|
272
|
+
)
|
|
273
|
+
throw new Error("No live /root in this agent family");
|
|
274
|
+
const matches = [];
|
|
275
|
+
for (const entry of await this.fleet.snapshots(machine)) {
|
|
276
|
+
if (!entry.snapshot) continue;
|
|
277
|
+
const runtime = this.fleet.connected(entry.id);
|
|
278
|
+
const identity = await runtime.client.machineIdentity();
|
|
279
|
+
if (identity.host !== result.host || identity.session !== result.session)
|
|
280
|
+
continue;
|
|
281
|
+
for (const agent of entry.snapshot.agents) {
|
|
282
|
+
if (agent.agent === "pi" && sessionPath(agent) === result.sessionFile)
|
|
283
|
+
matches.push({ runtime, target: agent.pane_id });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (matches.length !== 1)
|
|
287
|
+
throw new Error(
|
|
288
|
+
"No unique live /root in this agent family; use find for an exact target",
|
|
289
|
+
);
|
|
290
|
+
return matches[0]!;
|
|
291
|
+
}
|
|
292
|
+
|
|
190
293
|
async prepare(
|
|
191
294
|
ctx: ExtensionContext,
|
|
192
295
|
runtime: ConnectedMachine,
|
package/src/types.ts
CHANGED
|
@@ -8,6 +8,35 @@ export interface PeerMessage {
|
|
|
8
8
|
|
|
9
9
|
export interface PeerDelivery {
|
|
10
10
|
command: boolean;
|
|
11
|
+
commands?: PeerCommand[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface PeerCommand {
|
|
15
|
+
name: string;
|
|
16
|
+
description: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface MachineIdentity {
|
|
20
|
+
host: string;
|
|
21
|
+
session: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface FocusRequest {
|
|
25
|
+
source: MachineIdentity & {
|
|
26
|
+
pane: string;
|
|
27
|
+
terminal: string;
|
|
28
|
+
sessionFile: string;
|
|
29
|
+
};
|
|
30
|
+
handoff?: string;
|
|
31
|
+
voice?: boolean;
|
|
32
|
+
voiceAudioTransport?: "webrtc" | "pcm24";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PeerChannel {
|
|
36
|
+
send(message: unknown): void;
|
|
37
|
+
onMessage(listener: (message: unknown) => void): () => void;
|
|
38
|
+
onClose(listener: (error: Error) => void): () => void;
|
|
39
|
+
close(): void;
|
|
11
40
|
}
|
|
12
41
|
|
|
13
42
|
export type SettledAgentStatus = Exclude<AgentStatus, "working">;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { recordContextBriefing } from "@howaboua/pi-agent-board/integration";
|
|
7
|
+
import { voiceFocusContinuity, voiceFocusReport } from "./messages.js";
|
|
8
|
+
import { registerFocusChannelReceiver } from "./peer-channel.js";
|
|
9
|
+
import type { FocusRequest, PaneInfo, PeerChannel } from "./types.js";
|
|
10
|
+
|
|
11
|
+
const PROTOCOL = "@howaboua/pi/voice-handoff/v1";
|
|
12
|
+
const REPORT = "herdr-voice-focus-report";
|
|
13
|
+
|
|
14
|
+
// Optional event service contract; no dependency on either voice extension.
|
|
15
|
+
interface VoiceService {
|
|
16
|
+
protocol: typeof PROTOCOL;
|
|
17
|
+
priority: number;
|
|
18
|
+
audioTransport?: "webrtc" | "pcm24";
|
|
19
|
+
status(): { active: boolean; busy: boolean };
|
|
20
|
+
depart(channel: PeerChannel): Promise<void>;
|
|
21
|
+
arrive(channel: PeerChannel, continuity: string): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function voiceServices(pi: ExtensionAPI): VoiceService[] {
|
|
25
|
+
const services = new Set<VoiceService>();
|
|
26
|
+
pi.events.emit(`${PROTOCOL}/request`, {
|
|
27
|
+
protocol: PROTOCOL,
|
|
28
|
+
accept(service: VoiceService) {
|
|
29
|
+
if (
|
|
30
|
+
service?.protocol === PROTOCOL &&
|
|
31
|
+
Number.isFinite(service.priority) &&
|
|
32
|
+
(service.audioTransport === undefined ||
|
|
33
|
+
service.audioTransport === "webrtc" ||
|
|
34
|
+
service.audioTransport === "pcm24") &&
|
|
35
|
+
typeof service.status === "function" &&
|
|
36
|
+
typeof service.depart === "function" &&
|
|
37
|
+
typeof service.arrive === "function"
|
|
38
|
+
)
|
|
39
|
+
services.add(service);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
return [...services];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function activeVoiceService(pi: ExtensionAPI): VoiceService {
|
|
46
|
+
const active = voiceServices(pi).filter((service) => service.status().active);
|
|
47
|
+
if (active.length !== 1 || active[0]!.status().busy)
|
|
48
|
+
throw new Error(
|
|
49
|
+
"Voice transfer needs exactly one active, available voice provider in the source session; use voice:false for text-only focus",
|
|
50
|
+
);
|
|
51
|
+
return active[0]!;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function passiveReport(
|
|
55
|
+
pi: ExtensionAPI,
|
|
56
|
+
ctx: ExtensionContext,
|
|
57
|
+
sessionFile: string | undefined,
|
|
58
|
+
side: "source" | "destination",
|
|
59
|
+
target: string,
|
|
60
|
+
error?: unknown,
|
|
61
|
+
): Promise<void> {
|
|
62
|
+
const content = voiceFocusReport(side, target, error);
|
|
63
|
+
if (ctx.sessionManager.getSessionFile() === sessionFile) {
|
|
64
|
+
pi.appendEntry(REPORT, {
|
|
65
|
+
content,
|
|
66
|
+
side,
|
|
67
|
+
target,
|
|
68
|
+
status: error === undefined ? "active" : "failed",
|
|
69
|
+
});
|
|
70
|
+
await recordContextBriefing(
|
|
71
|
+
pi,
|
|
72
|
+
ctx,
|
|
73
|
+
REPORT,
|
|
74
|
+
randomUUID(),
|
|
75
|
+
async () => content,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
ctx.ui.notify(content, error === undefined ? "info" : "error");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function observeVoiceTransfer(
|
|
82
|
+
pi: ExtensionAPI,
|
|
83
|
+
ctx: ExtensionContext,
|
|
84
|
+
completion: Promise<void>,
|
|
85
|
+
channel: PeerChannel,
|
|
86
|
+
side: "source" | "destination",
|
|
87
|
+
target: string,
|
|
88
|
+
): void {
|
|
89
|
+
const sessionFile = ctx.sessionManager.getSessionFile();
|
|
90
|
+
void completion
|
|
91
|
+
.then(
|
|
92
|
+
() => passiveReport(pi, ctx, sessionFile, side, target),
|
|
93
|
+
(error: unknown) => {
|
|
94
|
+
try {
|
|
95
|
+
channel.close();
|
|
96
|
+
} catch (closeError) {
|
|
97
|
+
ctx.ui.notify(
|
|
98
|
+
`Voice transfer cleanup failed: ${String(closeError)}`,
|
|
99
|
+
"error",
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return passiveReport(
|
|
103
|
+
pi,
|
|
104
|
+
ctx,
|
|
105
|
+
sessionFile,
|
|
106
|
+
side,
|
|
107
|
+
target,
|
|
108
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
109
|
+
);
|
|
110
|
+
},
|
|
111
|
+
)
|
|
112
|
+
.catch((error: unknown) =>
|
|
113
|
+
ctx.ui.notify(
|
|
114
|
+
`Voice transfer report could not be saved: ${String(error)}`,
|
|
115
|
+
"error",
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function registerVoiceFocusReceiver(
|
|
121
|
+
pi: ExtensionAPI,
|
|
122
|
+
focus: (
|
|
123
|
+
ctx: ExtensionContext,
|
|
124
|
+
pane: PaneInfo,
|
|
125
|
+
request: FocusRequest,
|
|
126
|
+
) => Promise<void>,
|
|
127
|
+
): void {
|
|
128
|
+
registerFocusChannelReceiver(pi, async (channel, request, ctx, pane) => {
|
|
129
|
+
if (request.voice !== true)
|
|
130
|
+
throw new Error("Voice channel requires voice:true");
|
|
131
|
+
const service = voiceServices(pi)
|
|
132
|
+
.filter((candidate) => {
|
|
133
|
+
const status = candidate.status();
|
|
134
|
+
return (
|
|
135
|
+
!status.active &&
|
|
136
|
+
!status.busy &&
|
|
137
|
+
(candidate.audioTransport ?? "webrtc") ===
|
|
138
|
+
(request.voiceAudioTransport ?? "webrtc")
|
|
139
|
+
);
|
|
140
|
+
})
|
|
141
|
+
.sort((a, b) => b.priority - a.priority)[0];
|
|
142
|
+
if (!service)
|
|
143
|
+
throw new Error(
|
|
144
|
+
"Destination has no available voice provider compatible with the current call; choose another session or use voice:false for text-only focus",
|
|
145
|
+
);
|
|
146
|
+
// arrive reserves synchronously; do not await replacement startup or source
|
|
147
|
+
// settlement during admission. Attach rejection handling immediately.
|
|
148
|
+
const completion = service.arrive(channel, voiceFocusContinuity(request));
|
|
149
|
+
const outcome = completion.then(
|
|
150
|
+
() => ({ ok: true as const }),
|
|
151
|
+
(error: unknown) => ({ ok: false as const, error }),
|
|
152
|
+
);
|
|
153
|
+
try {
|
|
154
|
+
await focus(ctx, pane, request);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
try {
|
|
157
|
+
channel.close();
|
|
158
|
+
} catch (closeError) {
|
|
159
|
+
ctx.ui.notify(
|
|
160
|
+
`Voice transfer cleanup failed: ${String(closeError)}`,
|
|
161
|
+
"error",
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
observeVoiceTransfer(
|
|
167
|
+
pi,
|
|
168
|
+
ctx,
|
|
169
|
+
outcome.then((result) => {
|
|
170
|
+
if (!result.ok) throw result.error;
|
|
171
|
+
}),
|
|
172
|
+
channel,
|
|
173
|
+
"destination",
|
|
174
|
+
pane.pane_id,
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
}
|
package/src/board/archive.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { existsSync } from "node:fs";
|
|
2
|
-
import type { BoardParams } from "./contract.js";
|
|
3
|
-
import { agentPath, MUTATIONS } from "./contract.js";
|
|
4
|
-
import type { BoardBinding } from "./identity.js";
|
|
5
|
-
import { page } from "./paging.js";
|
|
6
|
-
import { boundedBoardRead, checkMutationBudget } from "./response.js";
|
|
7
|
-
|
|
8
|
-
export async function executeArchive(
|
|
9
|
-
caller: BoardBinding,
|
|
10
|
-
memberNames: readonly string[],
|
|
11
|
-
params: BoardParams,
|
|
12
|
-
requestId: string,
|
|
13
|
-
) {
|
|
14
|
-
const write = MUTATIONS.has(params.action);
|
|
15
|
-
if (write) checkMutationBudget(caller.agentName, params);
|
|
16
|
-
let storage: typeof import("./store.js");
|
|
17
|
-
try {
|
|
18
|
-
storage = await import("./store.js");
|
|
19
|
-
} catch (error) {
|
|
20
|
-
if (
|
|
21
|
-
error instanceof Error &&
|
|
22
|
-
"code" in error &&
|
|
23
|
-
error.code === "ERR_UNKNOWN_BUILTIN_MODULE"
|
|
24
|
-
)
|
|
25
|
-
throw new Error(
|
|
26
|
-
"Message boards require Node.js 22.13 or newer with SQLite support",
|
|
27
|
-
{ cause: error },
|
|
28
|
-
);
|
|
29
|
-
throw error;
|
|
30
|
-
}
|
|
31
|
-
storage.validateMutation(params);
|
|
32
|
-
if (write) {
|
|
33
|
-
for (const value of [
|
|
34
|
-
...(params.agents_to_notify ?? []),
|
|
35
|
-
...(params.target_agent === undefined ? [] : [params.target_agent]),
|
|
36
|
-
]) {
|
|
37
|
-
const target = agentPath(value, caller.agentName);
|
|
38
|
-
if (!memberNames.includes(target))
|
|
39
|
-
throw new Error(`Agent not bound to this tree: ${target}`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
const exists = existsSync(caller.databasePath);
|
|
43
|
-
if (
|
|
44
|
-
!exists &&
|
|
45
|
-
params.action === "search_posts" &&
|
|
46
|
-
params.after_message_id !== undefined
|
|
47
|
-
)
|
|
48
|
-
throw new Error("Post not found in this board");
|
|
49
|
-
if (
|
|
50
|
-
!exists &&
|
|
51
|
-
(!write ||
|
|
52
|
-
(params.action !== "create_channel" &&
|
|
53
|
-
params.new_channel_name === undefined))
|
|
54
|
-
) {
|
|
55
|
-
if (["get_channels", "search_posts", "list_boards"].includes(params.action))
|
|
56
|
-
return { value: page([], params), recipients: [] };
|
|
57
|
-
throw new Error("This board has no channels or posts yet");
|
|
58
|
-
}
|
|
59
|
-
const store = new storage.BoardStore(caller.databasePath, write);
|
|
60
|
-
try {
|
|
61
|
-
const scope = {
|
|
62
|
-
boardId: caller.boardId,
|
|
63
|
-
rootSessionId: caller.rootSessionId,
|
|
64
|
-
ownerFolder: caller.ownerFolder,
|
|
65
|
-
callerSessionId: caller.sessionId,
|
|
66
|
-
agentName: caller.agentName,
|
|
67
|
-
members: memberNames,
|
|
68
|
-
};
|
|
69
|
-
return write
|
|
70
|
-
? store.execute(scope, params, requestId)
|
|
71
|
-
: boundedBoardRead(params, (request) =>
|
|
72
|
-
store.execute(scope, request, requestId),
|
|
73
|
-
);
|
|
74
|
-
} finally {
|
|
75
|
-
store.close();
|
|
76
|
-
}
|
|
77
|
-
}
|