@howaboua/pi-agent-comms 0.0.0-stage → 0.0.1-dev.13.1.gef1ce49691d6
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/INTEGRATION.md +31 -0
- package/LICENSE +21 -0
- package/PCC-INTEGRATION.md +17 -0
- package/README.md +53 -2
- package/dist/viewer/archive.js +249 -0
- package/dist/viewer/config.js +85 -0
- package/dist/viewer/contracts.js +1 -0
- package/dist/viewer/server.js +177 -0
- package/dist/viewer/source.js +29 -0
- package/dist/viewer/web/app.js +88 -0
- package/dist/viewer/web/index.html +41 -0
- package/dist/viewer/web/navigation.js +72 -0
- package/dist/viewer/web/posts.js +105 -0
- package/dist/viewer/web/render.js +147 -0
- package/dist/viewer/web/session.js +86 -0
- package/dist/viewer/web/style.css +517 -0
- package/dist/viewer/web/updates.js +106 -0
- package/docs/BOARD-CHANGELOG.md +49 -0
- package/docs/BOARD.md +124 -0
- package/docs/NOTES-CHANGELOG.md +127 -0
- package/docs/NOTES.md +122 -0
- package/index.ts +21 -0
- package/package.json +75 -5
- package/src/board/AGENTS.md +12 -0
- package/src/board/activity.ts +274 -0
- package/src/board/archive.ts +118 -0
- package/src/board/awareness.ts +179 -0
- package/src/board/briefing.ts +25 -0
- package/src/board/config.ts +192 -0
- package/src/board/consolidation.ts +32 -0
- package/src/board/context-briefing.ts +245 -0
- package/src/board/contract.ts +254 -0
- package/src/board/delivery.ts +85 -0
- package/src/board/feature.ts +91 -0
- package/src/board/identity.ts +238 -0
- package/src/board/integration.ts +55 -0
- package/src/board/paging.ts +54 -0
- package/src/board/protocol.ts +78 -0
- package/src/board/queries.ts +168 -0
- package/src/board/response.ts +82 -0
- package/src/board/runtime/activation.ts +50 -0
- package/src/board/runtime/catchup.ts +96 -0
- package/src/board/runtime/coordinator.ts +311 -0
- package/src/board/runtime/engine.ts +40 -0
- package/src/board/runtime/index.ts +63 -0
- package/src/board/runtime/owner.ts +168 -0
- package/src/board/runtime/routing.ts +152 -0
- package/src/board/runtime/turns.ts +109 -0
- package/src/board/search.ts +95 -0
- package/src/board/settings/panel.ts +193 -0
- package/src/board/store.ts +427 -0
- package/src/board/summaries.ts +124 -0
- package/src/board/summarizer.ts +201 -0
- package/src/board/tool.ts +27 -0
- package/src/board/viewer/AGENTS.md +1 -0
- package/src/board/viewer/archive.ts +308 -0
- package/src/board/viewer/config.ts +111 -0
- package/src/board/viewer/contracts.ts +98 -0
- package/src/board/viewer/index.ts +162 -0
- package/src/board/viewer/server.ts +204 -0
- package/src/board/viewer/source.ts +33 -0
- package/src/board/viewer/web/app.js +88 -0
- package/src/board/viewer/web/index.html +41 -0
- package/src/board/viewer/web/navigation.js +72 -0
- package/src/board/viewer/web/posts.js +105 -0
- package/src/board/viewer/web/render.js +147 -0
- package/src/board/viewer/web/session.js +86 -0
- package/src/board/viewer/web/style.css +517 -0
- package/src/board/viewer/web/updates.js +106 -0
- package/src/guidance.ts +121 -0
- package/src/notes/AGENTS.md +27 -0
- package/src/notes/activation.ts +150 -0
- package/src/notes/agent-identity.ts +221 -0
- package/src/notes/attachment-sharing.ts +165 -0
- package/src/notes/attachment-snapshot.ts +113 -0
- package/src/notes/binding-startup.ts +30 -0
- package/src/notes/bridge.ts +130 -0
- package/src/notes/compaction-projection.ts +88 -0
- package/src/notes/context-owner.ts +99 -0
- package/src/notes/context-sharing.ts +178 -0
- package/src/notes/extension-hooks.ts +200 -0
- package/src/notes/extension-sharing.ts +133 -0
- package/src/notes/feature.ts +168 -0
- package/src/notes/fresh-checkpoint.ts +143 -0
- package/src/notes/fresh-notes.ts +323 -0
- package/src/notes/history-search-protocol.ts +39 -0
- package/src/notes/history-search-pruning.mjs +120 -0
- package/src/notes/history-search-worker.mjs +258 -0
- package/src/notes/history-search.ts +269 -0
- package/src/notes/history-windows.ts +221 -0
- package/src/notes/history.ts +158 -0
- package/src/notes/idle-admission.ts +201 -0
- package/src/notes/index.ts +125 -0
- package/src/notes/integrations.ts +174 -0
- package/src/notes/lifecycle.ts +308 -0
- package/src/notes/model-role-config.ts +79 -0
- package/src/notes/model-role-screen.ts +74 -0
- package/src/notes/model-roles.ts +225 -0
- package/src/notes/note-query.ts +148 -0
- package/src/notes/note-result.ts +88 -0
- package/src/notes/note-save-marker.ts +128 -0
- package/src/notes/notes-lookup.ts +115 -0
- package/src/notes/notes.ts +302 -0
- package/src/notes/query-executor.ts +242 -0
- package/src/notes/query-input.ts +126 -0
- package/src/notes/remote.ts +376 -0
- package/src/notes/rollover.ts +224 -0
- package/src/notes/saved-session-access.ts +117 -0
- package/src/notes/saved-session-index.mjs +83 -0
- package/src/notes/saved-session.mjs +241 -0
- package/src/notes/saved-session.ts +51 -0
- package/src/notes/saved-sharing-records.ts +135 -0
- package/src/notes/session-pruning-content.ts +237 -0
- package/src/notes/session-pruning-plan.ts +243 -0
- package/src/notes/session-pruning-policy.ts +20 -0
- package/src/notes/session-pruning-screen.ts +204 -0
- package/src/notes/session-pruning-source.ts +44 -0
- package/src/notes/session-pruning-transaction.ts +141 -0
- package/src/notes/session-pruning.ts +155 -0
- package/src/notes/settings-command.ts +95 -0
- package/src/notes/settings-controls.ts +199 -0
- package/src/notes/settings-screen.ts +113 -0
- package/src/notes/settings.ts +287 -0
- package/src/notes/shared-notes.ts +411 -0
- package/src/notes/shared-session-reader.ts +143 -0
- package/src/notes/sharing-service.ts +282 -0
- package/src/notes/sqlite.mjs +33 -0
- package/src/notes/store.ts +283 -0
- package/src/notes/task-boundary.ts +37 -0
- package/src/notes/tool-contracts.ts +312 -0
- package/src/notes/tools.ts +148 -0
- package/src/notes/tree-archive-manifest.mjs +27 -0
- package/src/notes/tree-archive.ts +129 -0
- package/src/notes/tree-checkpoint.ts +170 -0
- package/src/notes/tree-handoff-read.ts +192 -0
- package/src/notes/window-checkpoint.ts +105 -0
- package/src/notes/window-message.ts +185 -0
- package/src/notes/windows.ts +272 -0
- package/src/settings-navigation.ts +11 -0
- package/src/timestamps.ts +3 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionAPI,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
MessageRenderer,
|
|
5
|
+
SessionEntry,
|
|
6
|
+
Theme,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { Text, truncateToWidth } from "@earendil-works/pi-tui";
|
|
9
|
+
import type { BoardParams } from "./contract.js";
|
|
10
|
+
|
|
11
|
+
const BOARD_POST_MARKER = "shepherdr-board-post-marker";
|
|
12
|
+
|
|
13
|
+
export function recordBoardActivityMarker(
|
|
14
|
+
pi: ExtensionAPI,
|
|
15
|
+
ctx: ExtensionContext,
|
|
16
|
+
params: BoardParams,
|
|
17
|
+
value: unknown,
|
|
18
|
+
requestId: string,
|
|
19
|
+
): void {
|
|
20
|
+
const labels: Partial<Record<BoardParams["action"], string>> = {
|
|
21
|
+
create_channel: "Created board channel",
|
|
22
|
+
post: params.thread_id ? "Posted board reply" : "Created board thread",
|
|
23
|
+
set_status: `Marked board thread ${params.status}`,
|
|
24
|
+
read_thread: "Read board thread",
|
|
25
|
+
read_post: "Read board post",
|
|
26
|
+
list_boards: "Read board list",
|
|
27
|
+
get_channels: "Read board channels",
|
|
28
|
+
list_threads: "Read board threads",
|
|
29
|
+
search_posts: "Searched board posts",
|
|
30
|
+
};
|
|
31
|
+
const label = labels[params.action];
|
|
32
|
+
if (!label || !value || typeof value !== "object") return;
|
|
33
|
+
const result = value as Record<string, unknown>;
|
|
34
|
+
const root = result["root_post"];
|
|
35
|
+
const metadata =
|
|
36
|
+
root && typeof root === "object"
|
|
37
|
+
? (root as Record<string, unknown>)
|
|
38
|
+
: result;
|
|
39
|
+
const field = (key: string) =>
|
|
40
|
+
typeof metadata[key] === "string" ? metadata[key] : undefined;
|
|
41
|
+
if (
|
|
42
|
+
ctx.sessionManager
|
|
43
|
+
.getBranch()
|
|
44
|
+
.some(
|
|
45
|
+
(entry) =>
|
|
46
|
+
entry.type === "custom" &&
|
|
47
|
+
entry.customType === BOARD_POST_MARKER &&
|
|
48
|
+
entry.data &&
|
|
49
|
+
typeof entry.data === "object" &&
|
|
50
|
+
"requestId" in entry.data &&
|
|
51
|
+
entry.data.requestId === requestId,
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
return;
|
|
55
|
+
// Owner acknowledgements reach the sender here, never the subscription inbox.
|
|
56
|
+
pi.appendEntry(BOARD_POST_MARKER, {
|
|
57
|
+
operation: params.action,
|
|
58
|
+
label,
|
|
59
|
+
requestId,
|
|
60
|
+
boardId: params.board_id,
|
|
61
|
+
messageId: field("message_id") ?? params.message_id,
|
|
62
|
+
threadId: field("thread_id") ?? params.thread_id,
|
|
63
|
+
channelName:
|
|
64
|
+
field("channel_name") ?? params.channel_name ?? params.new_channel_name,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const BOARD_NOTICE = "shepherdr-board-post";
|
|
69
|
+
const BOARD_CATCHUP = "shepherdr-board-catchup";
|
|
70
|
+
|
|
71
|
+
interface BoardRow {
|
|
72
|
+
id: string;
|
|
73
|
+
label: string;
|
|
74
|
+
detail: () => string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function boardRow(entry: SessionEntry): BoardRow | undefined {
|
|
78
|
+
if (
|
|
79
|
+
entry.type === "custom_message" &&
|
|
80
|
+
(entry.customType === BOARD_NOTICE || entry.customType === BOARD_CATCHUP)
|
|
81
|
+
) {
|
|
82
|
+
return {
|
|
83
|
+
id: entry.id,
|
|
84
|
+
label: "Board notice received",
|
|
85
|
+
detail: () =>
|
|
86
|
+
typeof entry.content === "string"
|
|
87
|
+
? entry.content
|
|
88
|
+
: JSON.stringify(entry.content),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (entry.type === "custom" && entry.customType === BOARD_POST_MARKER) {
|
|
92
|
+
const data =
|
|
93
|
+
entry.data && typeof entry.data === "object"
|
|
94
|
+
? (entry.data as Record<string, unknown>)
|
|
95
|
+
: undefined;
|
|
96
|
+
const label =
|
|
97
|
+
typeof data?.["label"] === "string" ? data["label"] : "Board action";
|
|
98
|
+
const channel =
|
|
99
|
+
typeof data?.["channelName"] === "string" ? data["channelName"] : "";
|
|
100
|
+
return {
|
|
101
|
+
id: entry.id,
|
|
102
|
+
label: [label, channel]
|
|
103
|
+
.filter(Boolean)
|
|
104
|
+
.join(" · ")
|
|
105
|
+
.replace(/[\r\n\t\x00-\x1f\x7f]/g, " "),
|
|
106
|
+
detail: () => JSON.stringify(entry.data, null, 2) ?? "",
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (
|
|
110
|
+
entry.type === "message" &&
|
|
111
|
+
entry.message.role === "custom" &&
|
|
112
|
+
(entry.message.customType === BOARD_NOTICE ||
|
|
113
|
+
entry.message.customType === BOARD_CATCHUP)
|
|
114
|
+
) {
|
|
115
|
+
const content = entry.message.content;
|
|
116
|
+
return {
|
|
117
|
+
id: entry.id,
|
|
118
|
+
label: "Board notice received",
|
|
119
|
+
detail: () =>
|
|
120
|
+
typeof content === "string" ? content : JSON.stringify(content),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Project existing branch entries only. No new history or model delivery. */
|
|
127
|
+
export function registerBoardActivityRenderer(pi: ExtensionAPI): void {
|
|
128
|
+
let sessionManager: ExtensionContext["sessionManager"] | undefined;
|
|
129
|
+
let generation = 0;
|
|
130
|
+
const restore = (_event: unknown, ctx: ExtensionContext) => {
|
|
131
|
+
sessionManager = ctx.sessionManager;
|
|
132
|
+
generation++;
|
|
133
|
+
};
|
|
134
|
+
pi.on("session_start", restore);
|
|
135
|
+
pi.on("session_tree", restore);
|
|
136
|
+
pi.on("session_compact", restore);
|
|
137
|
+
pi.on("session_shutdown", () => {
|
|
138
|
+
sessionManager = undefined;
|
|
139
|
+
cache = undefined;
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
interface BoardGroup {
|
|
143
|
+
rows: BoardRow[];
|
|
144
|
+
summary: string;
|
|
145
|
+
details?: string;
|
|
146
|
+
}
|
|
147
|
+
let cache:
|
|
148
|
+
| {
|
|
149
|
+
leaf: string | null;
|
|
150
|
+
generation: number;
|
|
151
|
+
groups: Map<string, BoardGroup>;
|
|
152
|
+
notices: Map<unknown, string>;
|
|
153
|
+
}
|
|
154
|
+
| undefined;
|
|
155
|
+
const projection = () => {
|
|
156
|
+
const leaf = sessionManager?.getLeafId() ?? null;
|
|
157
|
+
if (cache?.leaf === leaf && cache.generation === generation) return cache;
|
|
158
|
+
const groups = new Map<string, BoardGroup>();
|
|
159
|
+
const notices = new Map<unknown, string>();
|
|
160
|
+
let rows: BoardRow[] = [];
|
|
161
|
+
let running = false;
|
|
162
|
+
const finish = () => {
|
|
163
|
+
if (!rows.length) return;
|
|
164
|
+
const counts = new Map<string, number>();
|
|
165
|
+
for (const row of rows)
|
|
166
|
+
counts.set(row.label, (counts.get(row.label) ?? 0) + 1);
|
|
167
|
+
const group: BoardGroup = {
|
|
168
|
+
rows,
|
|
169
|
+
summary:
|
|
170
|
+
"Board · " +
|
|
171
|
+
[...counts]
|
|
172
|
+
.map(([label, count]) => label + (count > 1 ? ` ×${count}` : ""))
|
|
173
|
+
.join(" · "),
|
|
174
|
+
};
|
|
175
|
+
for (const row of rows) groups.set(row.id, group);
|
|
176
|
+
};
|
|
177
|
+
for (const entry of sessionManager?.getBranch() ?? []) {
|
|
178
|
+
if (
|
|
179
|
+
entry.type === "message" &&
|
|
180
|
+
entry.message.role === "user" &&
|
|
181
|
+
!running
|
|
182
|
+
) {
|
|
183
|
+
finish();
|
|
184
|
+
rows = [];
|
|
185
|
+
running = true;
|
|
186
|
+
}
|
|
187
|
+
if (entry.type === "message" && entry.message.role === "assistant") {
|
|
188
|
+
const message = entry.message;
|
|
189
|
+
// Interrupted partial tool calls do not keep the next kickoff in this run.
|
|
190
|
+
running =
|
|
191
|
+
!["aborted", "error", "length"].includes(message.stopReason) &&
|
|
192
|
+
(message.stopReason === "toolUse" ||
|
|
193
|
+
message.content.some((block) => block.type === "toolCall"));
|
|
194
|
+
}
|
|
195
|
+
const row = boardRow(entry);
|
|
196
|
+
if (!row) continue;
|
|
197
|
+
rows.push(row);
|
|
198
|
+
if (entry.type === "custom_message") notices.set(entry.content, entry.id);
|
|
199
|
+
else if (entry.type === "message" && entry.message.role === "custom")
|
|
200
|
+
notices.set(entry.message.content, entry.id);
|
|
201
|
+
}
|
|
202
|
+
finish();
|
|
203
|
+
cache = { leaf, generation, groups, notices };
|
|
204
|
+
return cache;
|
|
205
|
+
};
|
|
206
|
+
const component = (
|
|
207
|
+
identity: string | { content: unknown },
|
|
208
|
+
expanded: boolean,
|
|
209
|
+
theme: Theme,
|
|
210
|
+
outputPad: number,
|
|
211
|
+
pending?: string,
|
|
212
|
+
) =>
|
|
213
|
+
new (class extends Text {
|
|
214
|
+
private readonly createdGeneration = generation;
|
|
215
|
+
private persisted = false;
|
|
216
|
+
override render(width: number): string[] {
|
|
217
|
+
if (!sessionManager) return [];
|
|
218
|
+
const padding = Math.min(
|
|
219
|
+
outputPad,
|
|
220
|
+
Math.max(0, Math.floor((width - 1) / 2)),
|
|
221
|
+
);
|
|
222
|
+
const contentWidth = Math.max(1, width - padding * 2);
|
|
223
|
+
const current = projection();
|
|
224
|
+
const ownId =
|
|
225
|
+
typeof identity === "string"
|
|
226
|
+
? identity
|
|
227
|
+
: current.notices.get(identity.content);
|
|
228
|
+
const group = ownId ? current.groups.get(ownId) : undefined;
|
|
229
|
+
if (group) this.persisted = true;
|
|
230
|
+
// Streamed custom messages can render before message_end persists them.
|
|
231
|
+
if (
|
|
232
|
+
!group &&
|
|
233
|
+
pending &&
|
|
234
|
+
!this.persisted &&
|
|
235
|
+
this.createdGeneration === generation
|
|
236
|
+
) {
|
|
237
|
+
this.setText(
|
|
238
|
+
theme.style(
|
|
239
|
+
truncateToWidth("Board · Notice received", contentWidth),
|
|
240
|
+
{ fg: "syntaxString", dim: true },
|
|
241
|
+
) + (expanded ? "\n" + pending : ""),
|
|
242
|
+
);
|
|
243
|
+
return super.render(width);
|
|
244
|
+
}
|
|
245
|
+
if (!group || group.rows.at(-1)?.id !== ownId) return [];
|
|
246
|
+
const heading = theme.style(
|
|
247
|
+
truncateToWidth(group.summary, contentWidth),
|
|
248
|
+
{ fg: "syntaxString", dim: true },
|
|
249
|
+
);
|
|
250
|
+
if (expanded)
|
|
251
|
+
group.details ??= group.rows
|
|
252
|
+
.map((row) => row.label + "\n" + row.detail())
|
|
253
|
+
.join("\n\n");
|
|
254
|
+
this.setText(heading + (expanded ? "\n" + group.details : ""));
|
|
255
|
+
return super.render(width);
|
|
256
|
+
}
|
|
257
|
+
})("", outputPad, 0);
|
|
258
|
+
// Pi's entry renderer contract does not expose outputPad; use its default inset.
|
|
259
|
+
pi.registerEntryRenderer(BOARD_POST_MARKER, (entry, { expanded }, theme) =>
|
|
260
|
+
component(entry.id, expanded, theme, 1),
|
|
261
|
+
);
|
|
262
|
+
const incoming: MessageRenderer = (message, { expanded, outputPad }, theme) =>
|
|
263
|
+
component(
|
|
264
|
+
{ content: message.content },
|
|
265
|
+
expanded,
|
|
266
|
+
theme,
|
|
267
|
+
outputPad,
|
|
268
|
+
typeof message.content === "string"
|
|
269
|
+
? message.content
|
|
270
|
+
: JSON.stringify(message.content),
|
|
271
|
+
);
|
|
272
|
+
pi.registerMessageRenderer(BOARD_NOTICE, incoming);
|
|
273
|
+
pi.registerMessageRenderer(BOARD_CATCHUP, incoming);
|
|
274
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
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 readSubscribedUpdates(
|
|
9
|
+
caller: BoardBinding,
|
|
10
|
+
seen: string[],
|
|
11
|
+
aliases: [string, string][] = [],
|
|
12
|
+
) {
|
|
13
|
+
if (!existsSync(caller.databasePath)) return [];
|
|
14
|
+
const { BoardStore } = await import("./store.js");
|
|
15
|
+
const store = new BoardStore(caller.databasePath, false, aliases);
|
|
16
|
+
try {
|
|
17
|
+
const identities = JSON.stringify(store.agentIdentities(caller.agentName));
|
|
18
|
+
return store.all<{ message_id: string; thread_id: string }>(
|
|
19
|
+
`SELECT p.id AS message_id,p.root AS thread_id FROM posts p
|
|
20
|
+
WHERE p.board_id=? AND p.author NOT IN (SELECT value FROM json_each(?)) AND NOT EXISTS
|
|
21
|
+
(SELECT 1 FROM json_each(?) WHERE value=p.id) AND EXISTS
|
|
22
|
+
(SELECT 1 FROM subscriptions s WHERE s.board_id=p.board_id
|
|
23
|
+
AND s.agent IN (SELECT value FROM json_each(?)) AND s.enabled=1
|
|
24
|
+
AND (s.agent=? OR NOT EXISTS (SELECT 1 FROM subscriptions n WHERE n.board_id=s.board_id AND n.target=s.target AND n.agent=?))
|
|
25
|
+
AND s.target=CASE WHEN p.id=p.root THEN 'channel:'||p.channel ELSE 'thread:'||p.root END)
|
|
26
|
+
ORDER BY p.seq ASC LIMIT 50`,
|
|
27
|
+
caller.boardId,
|
|
28
|
+
identities,
|
|
29
|
+
JSON.stringify(seen),
|
|
30
|
+
identities,
|
|
31
|
+
caller.agentName,
|
|
32
|
+
caller.agentName,
|
|
33
|
+
);
|
|
34
|
+
} finally {
|
|
35
|
+
store.close();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function executeArchive(
|
|
40
|
+
caller: BoardBinding,
|
|
41
|
+
memberNames: readonly string[],
|
|
42
|
+
params: BoardParams,
|
|
43
|
+
requestId: string,
|
|
44
|
+
aliases: [string, string][] = [],
|
|
45
|
+
) {
|
|
46
|
+
if (
|
|
47
|
+
params.action === "set_status" &&
|
|
48
|
+
caller.sessionId !== caller.rootSessionId
|
|
49
|
+
)
|
|
50
|
+
throw new Error("Only the parent session can change thread status");
|
|
51
|
+
const write = MUTATIONS.has(params.action);
|
|
52
|
+
if (write) checkMutationBudget(caller.agentName, params);
|
|
53
|
+
let storage: typeof import("./store.js");
|
|
54
|
+
try {
|
|
55
|
+
storage = await import("./store.js");
|
|
56
|
+
} catch (error) {
|
|
57
|
+
if (
|
|
58
|
+
error instanceof Error &&
|
|
59
|
+
"code" in error &&
|
|
60
|
+
error.code === "ERR_UNKNOWN_BUILTIN_MODULE"
|
|
61
|
+
)
|
|
62
|
+
throw new Error(
|
|
63
|
+
"Message boards require Node.js 22.13 or newer with SQLite support",
|
|
64
|
+
{ cause: error },
|
|
65
|
+
);
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
storage.validateMutation(params);
|
|
69
|
+
if (write) {
|
|
70
|
+
for (const value of [
|
|
71
|
+
...(params.agents_to_notify ?? []),
|
|
72
|
+
...(params.target_agent === undefined ? [] : [params.target_agent]),
|
|
73
|
+
]) {
|
|
74
|
+
const path = agentPath(value, caller.legacyAgentName ?? caller.agentName);
|
|
75
|
+
const target = new Map(aliases).get(path) ?? path;
|
|
76
|
+
if (!memberNames.includes(target))
|
|
77
|
+
throw new Error(`Agent not bound to this tree: ${target}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const exists = existsSync(caller.databasePath);
|
|
81
|
+
if (
|
|
82
|
+
!exists &&
|
|
83
|
+
params.action === "search_posts" &&
|
|
84
|
+
params.after_message_id !== undefined
|
|
85
|
+
)
|
|
86
|
+
throw new Error("Post not found in this board");
|
|
87
|
+
if (
|
|
88
|
+
!exists &&
|
|
89
|
+
(!write ||
|
|
90
|
+
(params.action !== "create_channel" &&
|
|
91
|
+
params.new_channel_name === undefined))
|
|
92
|
+
) {
|
|
93
|
+
if (["get_channels", "search_posts", "list_boards"].includes(params.action))
|
|
94
|
+
return { value: page([], params), recipients: [] };
|
|
95
|
+
throw new Error("This board has no channels or posts yet");
|
|
96
|
+
}
|
|
97
|
+
const store = new storage.BoardStore(caller.databasePath, write, aliases);
|
|
98
|
+
try {
|
|
99
|
+
const scope = {
|
|
100
|
+
boardId: caller.boardId,
|
|
101
|
+
rootSessionId: caller.rootSessionId,
|
|
102
|
+
ownerFolder: caller.ownerFolder,
|
|
103
|
+
callerSessionId: caller.sessionId,
|
|
104
|
+
agentName: caller.agentName,
|
|
105
|
+
...(caller.legacyAgentName
|
|
106
|
+
? { legacyAgentName: caller.legacyAgentName }
|
|
107
|
+
: {}),
|
|
108
|
+
members: memberNames,
|
|
109
|
+
};
|
|
110
|
+
return write
|
|
111
|
+
? store.execute(scope, params, requestId)
|
|
112
|
+
: boundedBoardRead(params, (request) =>
|
|
113
|
+
store.execute(scope, request, requestId),
|
|
114
|
+
);
|
|
115
|
+
} finally {
|
|
116
|
+
store.close();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { boardBriefing } from "./briefing.js";
|
|
7
|
+
import {
|
|
8
|
+
contextBriefingWindow,
|
|
9
|
+
recordContextBriefing,
|
|
10
|
+
} from "./context-briefing.js";
|
|
11
|
+
import { binding } from "./identity.js";
|
|
12
|
+
import type { BoardAdapter } from "./integration.js";
|
|
13
|
+
|
|
14
|
+
const OWNER = "shepherdr-board";
|
|
15
|
+
const STATE = "shepherdr-board-awareness";
|
|
16
|
+
interface AwarenessState {
|
|
17
|
+
sessionId: string;
|
|
18
|
+
signature: string;
|
|
19
|
+
epoch: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Board state is sampled once per selected window/membership, never on a timer. */
|
|
23
|
+
export class BoardAwareness {
|
|
24
|
+
private readonly pi: ExtensionAPI;
|
|
25
|
+
private readonly read: (ctx: ExtensionContext) => Promise<unknown>;
|
|
26
|
+
private readonly availability: (
|
|
27
|
+
ctx: ExtensionContext,
|
|
28
|
+
error?: unknown,
|
|
29
|
+
) => void;
|
|
30
|
+
private readonly gpt: () => boolean;
|
|
31
|
+
private readonly privateBriefing: () => BoardAdapter["briefing"];
|
|
32
|
+
constructor(
|
|
33
|
+
pi: ExtensionAPI,
|
|
34
|
+
read: (ctx: ExtensionContext) => Promise<unknown>,
|
|
35
|
+
availability: (ctx: ExtensionContext, error?: unknown) => void,
|
|
36
|
+
privateBriefing: () => BoardAdapter["briefing"],
|
|
37
|
+
gpt: () => boolean = () => false,
|
|
38
|
+
) {
|
|
39
|
+
this.pi = pi;
|
|
40
|
+
this.read = read;
|
|
41
|
+
this.availability = availability;
|
|
42
|
+
this.gpt = gpt;
|
|
43
|
+
this.privateBriefing = privateBriefing;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
refresh(ctx: ExtensionContext): AwarenessState {
|
|
47
|
+
const own = binding(ctx);
|
|
48
|
+
const signature = JSON.stringify([
|
|
49
|
+
own.boardId,
|
|
50
|
+
own.rootSessionId,
|
|
51
|
+
own.agentName,
|
|
52
|
+
own.upstream,
|
|
53
|
+
own.enabled,
|
|
54
|
+
]);
|
|
55
|
+
const saved = ctx.sessionManager
|
|
56
|
+
.getEntries()
|
|
57
|
+
.findLast(
|
|
58
|
+
(entry) =>
|
|
59
|
+
entry.type === "custom" &&
|
|
60
|
+
entry.customType === STATE &&
|
|
61
|
+
readState(entry.data).sessionId === own.sessionId,
|
|
62
|
+
);
|
|
63
|
+
if (saved?.type === "custom") {
|
|
64
|
+
const state = readState(saved.data);
|
|
65
|
+
if (state.signature === signature) return state;
|
|
66
|
+
}
|
|
67
|
+
const state = { sessionId: own.sessionId, signature, epoch: randomUUID() };
|
|
68
|
+
this.pi.appendEntry(STATE, state);
|
|
69
|
+
return state;
|
|
70
|
+
}
|
|
71
|
+
seen(ctx: ExtensionContext): string[] {
|
|
72
|
+
const own = binding(ctx);
|
|
73
|
+
return ctx.sessionManager.getEntries().flatMap((entry) => {
|
|
74
|
+
if (
|
|
75
|
+
entry.type !== "custom" ||
|
|
76
|
+
entry.customType !== "shepherdr-board-seen"
|
|
77
|
+
)
|
|
78
|
+
return [];
|
|
79
|
+
const data = entry.data as {
|
|
80
|
+
boardId?: string;
|
|
81
|
+
agentName?: string;
|
|
82
|
+
ids?: unknown;
|
|
83
|
+
};
|
|
84
|
+
return data?.boardId === own.boardId &&
|
|
85
|
+
(data.agentName === own.agentName ||
|
|
86
|
+
data.agentName === own.legacyAgentName) &&
|
|
87
|
+
Array.isArray(data.ids)
|
|
88
|
+
? data.ids.filter((id): id is string => typeof id === "string")
|
|
89
|
+
: [];
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
markSeen(ctx: ExtensionContext, ids: string[]) {
|
|
93
|
+
if (!ids.length) return;
|
|
94
|
+
const own = binding(ctx);
|
|
95
|
+
this.pi.appendEntry("shepherdr-board-seen", {
|
|
96
|
+
boardId: own.boardId,
|
|
97
|
+
agentName: own.agentName,
|
|
98
|
+
ids,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
markRead(ctx: ExtensionContext, value: unknown) {
|
|
102
|
+
if (!value || typeof value !== "object") return;
|
|
103
|
+
const record = value as {
|
|
104
|
+
message_id?: unknown;
|
|
105
|
+
root_post?: unknown;
|
|
106
|
+
results?: unknown;
|
|
107
|
+
};
|
|
108
|
+
const posts = [
|
|
109
|
+
record,
|
|
110
|
+
record.root_post,
|
|
111
|
+
...(Array.isArray(record.results) ? record.results : []),
|
|
112
|
+
];
|
|
113
|
+
this.markSeen(
|
|
114
|
+
ctx,
|
|
115
|
+
posts.flatMap((post) =>
|
|
116
|
+
post &&
|
|
117
|
+
typeof post === "object" &&
|
|
118
|
+
"message_id" in post &&
|
|
119
|
+
typeof post.message_id === "string"
|
|
120
|
+
? [post.message_id]
|
|
121
|
+
: [],
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async record(ctx: ExtensionContext, windowId?: string) {
|
|
127
|
+
const own = binding(ctx);
|
|
128
|
+
const state = this.refresh(ctx);
|
|
129
|
+
if (!own.enabled) return;
|
|
130
|
+
const window = contextBriefingWindow(ctx, windowId);
|
|
131
|
+
const key = JSON.stringify([own.sessionId, window, state.epoch]);
|
|
132
|
+
await recordContextBriefing(this.pi, ctx, OWNER, key, async () => {
|
|
133
|
+
let population: "empty" | "populated" | "unavailable";
|
|
134
|
+
try {
|
|
135
|
+
const value = await this.read(ctx);
|
|
136
|
+
if (!binding(ctx).enabled) return;
|
|
137
|
+
if (
|
|
138
|
+
!value ||
|
|
139
|
+
typeof value !== "object" ||
|
|
140
|
+
!("results" in value) ||
|
|
141
|
+
!Array.isArray(value.results)
|
|
142
|
+
)
|
|
143
|
+
throw new Error("Invalid board population response");
|
|
144
|
+
population = value.results.length ? "populated" : "empty";
|
|
145
|
+
this.availability(ctx);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
ctx.signal?.throwIfAborted();
|
|
148
|
+
population = "unavailable";
|
|
149
|
+
this.availability(ctx, error);
|
|
150
|
+
}
|
|
151
|
+
ctx.signal?.throwIfAborted();
|
|
152
|
+
if (!binding(ctx).enabled) return;
|
|
153
|
+
if (
|
|
154
|
+
binding(ctx).enabled !== own.enabled ||
|
|
155
|
+
this.refresh(ctx).epoch !== state.epoch
|
|
156
|
+
)
|
|
157
|
+
throw new Error("Board membership changed during its briefing");
|
|
158
|
+
const privateBriefing = this.privateBriefing();
|
|
159
|
+
return privateBriefing
|
|
160
|
+
? privateBriefing(Boolean(own.upstream), population)
|
|
161
|
+
: boardBriefing(Boolean(own.upstream), population, this.gpt());
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function readState(value: unknown): AwarenessState {
|
|
167
|
+
if (
|
|
168
|
+
!value ||
|
|
169
|
+
typeof value !== "object" ||
|
|
170
|
+
!("sessionId" in value) ||
|
|
171
|
+
typeof value.sessionId !== "string" ||
|
|
172
|
+
!("signature" in value) ||
|
|
173
|
+
typeof value.signature !== "string" ||
|
|
174
|
+
!("epoch" in value) ||
|
|
175
|
+
typeof value.epoch !== "string"
|
|
176
|
+
)
|
|
177
|
+
throw new Error("Invalid saved board awareness");
|
|
178
|
+
return value as AwarenessState;
|
|
179
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function boardBriefing(
|
|
2
|
+
member: boolean,
|
|
3
|
+
population: "empty" | "populated" | "unavailable",
|
|
4
|
+
gpt = false,
|
|
5
|
+
) {
|
|
6
|
+
const state =
|
|
7
|
+
population === "populated"
|
|
8
|
+
? "Read relevant board threads."
|
|
9
|
+
: population === "empty"
|
|
10
|
+
? gpt
|
|
11
|
+
? "Shared board available; posts=none."
|
|
12
|
+
: "Shared board available; no posts yet."
|
|
13
|
+
: gpt
|
|
14
|
+
? "Shared board status unavailable; board help→retry read when available."
|
|
15
|
+
: "Shared board status unavailable. Check board help and retry reading when available.";
|
|
16
|
+
return [
|
|
17
|
+
state,
|
|
18
|
+
member ? "Follow your assigned scope." : "",
|
|
19
|
+
gpt
|
|
20
|
+
? "Channels=workstreams; threads=topics. Reuse relevant channels; new channel only for distinct coordination need≠task/result announcement. Read≠subscribe; subscribe to threads→replies, channels→new threads."
|
|
21
|
+
: "Channels group workstreams, threads group topics. Reuse a relevant channel. Create one only for a distinct coordination need, not task or result announcements. Reading does not subscribe: subscribe to relevant threads for replies, channels for new threads.",
|
|
22
|
+
]
|
|
23
|
+
.filter(Boolean)
|
|
24
|
+
.join(" ");
|
|
25
|
+
}
|