@howaboua/pi-shepherdr 0.2.15 → 0.2.16
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 +21 -0
- package/README.md +40 -33
- package/index.ts +16 -26
- package/package.json +11 -11
- package/src/agents-contract.ts +11 -1
- package/src/agents-discovery.ts +21 -7
- package/src/agents-focus.ts +242 -0
- package/src/agents-spawn.ts +23 -0
- package/src/agents-tool.ts +34 -5
- 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 +39 -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 +173 -0
- package/src/peer-inbox.ts +218 -55
- package/src/remote/shepherdr-channel.mjs +225 -0
- package/src/remote/shepherdr-checkpoints.mjs +5 -3
- package/src/remote/shepherdr-peer.mjs +151 -22
- package/src/remote/shepherdr.mjs +56 -8
- package/src/remote-client.ts +115 -11
- package/src/settlement.ts +1 -0
- package/src/shared-context.ts +104 -1
- package/src/types.ts +23 -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,22 @@ 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";
|
|
7
8
|
import type { AssistantReader } from "./session-reader.js";
|
|
8
9
|
import type {
|
|
9
10
|
HerdrEvent,
|
|
10
11
|
LatestAssistant,
|
|
11
12
|
PaneInfo,
|
|
13
|
+
PeerChannel,
|
|
12
14
|
PeerDelivery,
|
|
13
15
|
PeerMessage,
|
|
14
16
|
SessionView,
|
|
15
17
|
} from "./types.js";
|
|
16
18
|
|
|
17
|
-
const BRIDGE_VERSION =
|
|
19
|
+
const BRIDGE_VERSION = 18;
|
|
18
20
|
const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
|
|
19
21
|
const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
|
|
22
|
+
const REMOTE_CHANNEL_HELPER = "~/.pi/agent/shepherdr-channel.mjs";
|
|
20
23
|
const REMOTE_SESSION_HELPER = "~/.pi/agent/shepherdr-session.mjs";
|
|
21
24
|
const REMOTE_SESSION_WATCH_HELPER = "~/.pi/agent/shepherdr-session-watch.mjs";
|
|
22
25
|
const REMOTE_CHECKPOINT_HELPER = "~/.pi/agent/shepherdr-checkpoints.mjs";
|
|
@@ -61,7 +64,7 @@ process.stdin.on("end", () => { void (async () => {
|
|
|
61
64
|
interface PendingCall {
|
|
62
65
|
reject: (error: Error) => void;
|
|
63
66
|
resolve: (value: unknown) => void;
|
|
64
|
-
timer: NodeJS.Timeout;
|
|
67
|
+
timer: NodeJS.Timeout | undefined;
|
|
65
68
|
cleanup?: () => void;
|
|
66
69
|
}
|
|
67
70
|
|
|
@@ -176,6 +179,10 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
176
179
|
private readonly onClose: (error: Error) => void;
|
|
177
180
|
private readonly pending = new Map<string, PendingCall>();
|
|
178
181
|
private readonly subscriptions = new Map<string, SubscriptionCallbacks>();
|
|
182
|
+
private readonly channels = new Map<
|
|
183
|
+
string,
|
|
184
|
+
ReturnType<typeof createBufferedChannel>
|
|
185
|
+
>();
|
|
179
186
|
|
|
180
187
|
private constructor(
|
|
181
188
|
child: ChildProcessWithoutNullStreams,
|
|
@@ -206,6 +213,7 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
206
213
|
const [
|
|
207
214
|
source,
|
|
208
215
|
peerSource,
|
|
216
|
+
channelSource,
|
|
209
217
|
sessionSource,
|
|
210
218
|
contextSource,
|
|
211
219
|
checkpointSource,
|
|
@@ -217,6 +225,11 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
217
225
|
readFile(
|
|
218
226
|
fileURLToPath(new URL("./remote/shepherdr-peer.mjs", import.meta.url)),
|
|
219
227
|
),
|
|
228
|
+
readFile(
|
|
229
|
+
fileURLToPath(
|
|
230
|
+
new URL("./remote/shepherdr-channel.mjs", import.meta.url),
|
|
231
|
+
),
|
|
232
|
+
),
|
|
220
233
|
readFile(
|
|
221
234
|
fileURLToPath(
|
|
222
235
|
new URL("./remote/shepherdr-session.mjs", import.meta.url),
|
|
@@ -239,6 +252,7 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
239
252
|
),
|
|
240
253
|
]);
|
|
241
254
|
await deploy(config, peerSource, REMOTE_PEER_HELPER);
|
|
255
|
+
await deploy(config, channelSource, REMOTE_CHANNEL_HELPER);
|
|
242
256
|
await deploy(config, sessionSource, REMOTE_SESSION_HELPER);
|
|
243
257
|
await deploy(config, sessionWatchSource, REMOTE_SESSION_WATCH_HELPER);
|
|
244
258
|
await deploy(config, checkpointSource, REMOTE_CHECKPOINT_HELPER);
|
|
@@ -299,7 +313,10 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
299
313
|
agent: PaneInfo,
|
|
300
314
|
message: PeerMessage,
|
|
301
315
|
): Promise<PeerDelivery> {
|
|
302
|
-
const result = await this.call(
|
|
316
|
+
const result = await this.call(
|
|
317
|
+
{ op: "message", agent, message },
|
|
318
|
+
message.text.trim() === "/quit" ? 0 : 26_000,
|
|
319
|
+
);
|
|
303
320
|
if (
|
|
304
321
|
!result ||
|
|
305
322
|
typeof result !== "object" ||
|
|
@@ -312,6 +329,73 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
312
329
|
return { command: result.command };
|
|
313
330
|
}
|
|
314
331
|
|
|
332
|
+
async machineIdentity(): Promise<import("./types.js").MachineIdentity> {
|
|
333
|
+
const result = await this.call({ op: "identity" });
|
|
334
|
+
if (
|
|
335
|
+
!result ||
|
|
336
|
+
typeof result !== "object" ||
|
|
337
|
+
!("host" in result) ||
|
|
338
|
+
typeof result.host !== "string" ||
|
|
339
|
+
!("session" in result) ||
|
|
340
|
+
typeof result.session !== "string"
|
|
341
|
+
)
|
|
342
|
+
throw new Error("Could not identify remote Herdr session");
|
|
343
|
+
return { host: result.host, session: result.session };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
async openFocusChannel(
|
|
347
|
+
agent: PaneInfo,
|
|
348
|
+
request: import("./types.js").FocusRequest,
|
|
349
|
+
): Promise<PeerChannel> {
|
|
350
|
+
const id = randomUUID();
|
|
351
|
+
const state = createBufferedChannel(
|
|
352
|
+
(message) => {
|
|
353
|
+
const frame = `${JSON.stringify({ op: "channel_send", id, message })}\n`;
|
|
354
|
+
if (
|
|
355
|
+
this.closed ||
|
|
356
|
+
this.child.stdin.writableLength + Buffer.byteLength(frame) >
|
|
357
|
+
1024 * 1024
|
|
358
|
+
)
|
|
359
|
+
throw new Error(
|
|
360
|
+
"Focus channel output is unavailable or exceeded its buffer",
|
|
361
|
+
);
|
|
362
|
+
this.child.stdin.write(frame);
|
|
363
|
+
},
|
|
364
|
+
() => {
|
|
365
|
+
this.channels.delete(id);
|
|
366
|
+
if (!this.closed)
|
|
367
|
+
this.child.stdin.write(
|
|
368
|
+
`${JSON.stringify({ op: "channel_close", id })}\n`,
|
|
369
|
+
);
|
|
370
|
+
},
|
|
371
|
+
);
|
|
372
|
+
this.channels.set(id, state);
|
|
373
|
+
try {
|
|
374
|
+
const result = await this.call(
|
|
375
|
+
{ op: "channel_open", id, agent, request },
|
|
376
|
+
60_000,
|
|
377
|
+
);
|
|
378
|
+
if (
|
|
379
|
+
!result ||
|
|
380
|
+
typeof result !== "object" ||
|
|
381
|
+
!("admitted" in result) ||
|
|
382
|
+
result.admitted !== true
|
|
383
|
+
)
|
|
384
|
+
throw new Error("Focus channel was not admitted");
|
|
385
|
+
return state.channel;
|
|
386
|
+
} catch (error) {
|
|
387
|
+
state.fail(error instanceof Error ? error : new Error(String(error)));
|
|
388
|
+
throw error;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
focus(
|
|
393
|
+
agent: PaneInfo,
|
|
394
|
+
request: import("./types.js").FocusRequest,
|
|
395
|
+
): Promise<unknown> {
|
|
396
|
+
return this.call({ op: "focus", agent, request }, 60_000);
|
|
397
|
+
}
|
|
398
|
+
|
|
315
399
|
async subscribe(
|
|
316
400
|
subscriptions: object[],
|
|
317
401
|
onEvent: (event: HerdrEvent) => void,
|
|
@@ -432,14 +516,18 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
432
516
|
);
|
|
433
517
|
const id = typeof message["id"] === "string" ? message["id"] : randomUUID();
|
|
434
518
|
return new Promise((resolve, reject) => {
|
|
435
|
-
const timer =
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
519
|
+
const timer =
|
|
520
|
+
timeoutMs === 0
|
|
521
|
+
? undefined
|
|
522
|
+
: setTimeout(() => {
|
|
523
|
+
const error = new Error(
|
|
524
|
+
`remote Shepherdr ${String(message["op"])} timed out`,
|
|
525
|
+
);
|
|
526
|
+
if (message["op"] === "context_relay")
|
|
527
|
+
this.rejectPending(id, error);
|
|
528
|
+
else this.disconnected(error);
|
|
529
|
+
}, timeoutMs);
|
|
530
|
+
timer?.unref();
|
|
443
531
|
const abort = () => {
|
|
444
532
|
if (!this.pending.has(id)) return;
|
|
445
533
|
this.rejectPending(
|
|
@@ -498,6 +586,20 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
498
586
|
this.resolvePending("ready", value);
|
|
499
587
|
continue;
|
|
500
588
|
}
|
|
589
|
+
if (value["type"] === "channel" && typeof value["channel"] === "string") {
|
|
590
|
+
const state = this.channels.get(value["channel"]);
|
|
591
|
+
if (value["closed"] === true)
|
|
592
|
+
state?.fail(
|
|
593
|
+
new Error(
|
|
594
|
+
typeof value["error"] === "string"
|
|
595
|
+
? value["error"]
|
|
596
|
+
: "Focus channel disconnected",
|
|
597
|
+
),
|
|
598
|
+
);
|
|
599
|
+
else if ("message" in value) state?.receive(value["message"]);
|
|
600
|
+
else state?.fail(new Error("Invalid focus channel frame"));
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
501
603
|
const subscription = value["subscription"];
|
|
502
604
|
if (typeof subscription === "string") {
|
|
503
605
|
const callbacks = this.subscriptions.get(subscription);
|
|
@@ -595,6 +697,8 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
595
697
|
}
|
|
596
698
|
|
|
597
699
|
private failPending(error: Error, notify: boolean): void {
|
|
700
|
+
for (const state of this.channels.values()) state.fail(error);
|
|
701
|
+
this.channels.clear();
|
|
598
702
|
for (const controller of this.incomingContext.values()) controller.abort();
|
|
599
703
|
this.incomingContext.clear();
|
|
600
704
|
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
|
@@ -10,6 +10,29 @@ export interface PeerDelivery {
|
|
|
10
10
|
command: boolean;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
export interface MachineIdentity {
|
|
14
|
+
host: string;
|
|
15
|
+
session: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface FocusRequest {
|
|
19
|
+
source: MachineIdentity & {
|
|
20
|
+
pane: string;
|
|
21
|
+
terminal: string;
|
|
22
|
+
sessionFile: string;
|
|
23
|
+
};
|
|
24
|
+
handoff?: string;
|
|
25
|
+
voice?: boolean;
|
|
26
|
+
voiceAudioTransport?: "webrtc" | "pcm24";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface PeerChannel {
|
|
30
|
+
send(message: unknown): void;
|
|
31
|
+
onMessage(listener: (message: unknown) => void): () => void;
|
|
32
|
+
onClose(listener: (error: Error) => void): () => void;
|
|
33
|
+
close(): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
13
36
|
export type SettledAgentStatus = Exclude<AgentStatus, "working">;
|
|
14
37
|
|
|
15
38
|
export type StableAgentActivity =
|
|
@@ -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
|
-
}
|