@howaboua/pi-agent-board 0.0.0-stage → 0.0.2
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 +30 -0
- package/INTEGRATION.md +31 -0
- package/LICENSE +21 -0
- package/README.md +112 -2
- package/changelog.ts +337 -0
- package/dist/viewer/archive.js +242 -0
- package/dist/viewer/config.js +85 -0
- package/dist/viewer/contracts.js +1 -0
- package/dist/viewer/server.js +172 -0
- package/dist/viewer/source.js +29 -0
- package/dist/viewer/web/app.js +515 -0
- package/dist/viewer/web/index.html +41 -0
- package/dist/viewer/web/style.css +517 -0
- package/index.ts +122 -0
- package/package.json +68 -5
- package/src/activity.ts +274 -0
- package/src/archive.ts +108 -0
- package/src/awareness.ts +184 -0
- package/src/briefing.ts +24 -0
- package/src/config.ts +182 -0
- package/src/context-briefing.ts +227 -0
- package/src/contract.ts +246 -0
- package/src/delivery.ts +85 -0
- package/src/identity.ts +174 -0
- package/src/integration.ts +54 -0
- package/src/paging.ts +54 -0
- package/src/protocol.ts +78 -0
- package/src/queries.ts +171 -0
- package/src/response.ts +56 -0
- package/src/runtime.ts +614 -0
- package/src/search.ts +93 -0
- package/src/store.ts +391 -0
- package/src/summaries.ts +118 -0
- package/src/summarizer.ts +199 -0
- package/src/tool.ts +22 -0
- package/src/turns.ts +101 -0
- package/src/viewer/archive.ts +301 -0
- package/src/viewer/config.ts +111 -0
- package/src/viewer/contracts.ts +98 -0
- package/src/viewer/index.ts +158 -0
- package/src/viewer/server.ts +199 -0
- package/src/viewer/source.ts +33 -0
- package/src/viewer/web/app.js +515 -0
- package/src/viewer/web/index.html +41 -0
- package/src/viewer/web/style.css +517 -0
- package/tsconfig.json +5 -0
package/src/activity.ts
ADDED
|
@@ -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
|
+
}
|
package/src/archive.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
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
|
+
) {
|
|
12
|
+
if (!existsSync(caller.databasePath)) return [];
|
|
13
|
+
const { BoardStore } = await import("./store.js");
|
|
14
|
+
const store = new BoardStore(caller.databasePath, false);
|
|
15
|
+
try {
|
|
16
|
+
return store.all<{ message_id: string; thread_id: string }>(
|
|
17
|
+
`SELECT p.id AS message_id,p.root AS thread_id FROM posts p
|
|
18
|
+
WHERE p.board_id=? AND p.author<>? AND NOT EXISTS
|
|
19
|
+
(SELECT 1 FROM json_each(?) WHERE value=p.id) AND EXISTS
|
|
20
|
+
(SELECT 1 FROM subscriptions s WHERE s.board_id=p.board_id
|
|
21
|
+
AND s.agent=? AND s.enabled=1 AND s.target=CASE WHEN p.id=p.root
|
|
22
|
+
THEN 'channel:'||p.channel ELSE 'thread:'||p.root END)
|
|
23
|
+
ORDER BY p.seq ASC LIMIT 50`,
|
|
24
|
+
caller.boardId,
|
|
25
|
+
caller.agentName,
|
|
26
|
+
JSON.stringify(seen),
|
|
27
|
+
caller.agentName,
|
|
28
|
+
);
|
|
29
|
+
} finally {
|
|
30
|
+
store.close();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function executeArchive(
|
|
35
|
+
caller: BoardBinding,
|
|
36
|
+
memberNames: readonly string[],
|
|
37
|
+
params: BoardParams,
|
|
38
|
+
requestId: string,
|
|
39
|
+
) {
|
|
40
|
+
if (
|
|
41
|
+
params.action === "set_status" &&
|
|
42
|
+
caller.sessionId !== caller.rootSessionId
|
|
43
|
+
)
|
|
44
|
+
throw new Error("Only the parent session can change thread status");
|
|
45
|
+
const write = MUTATIONS.has(params.action);
|
|
46
|
+
if (write) checkMutationBudget(caller.agentName, params);
|
|
47
|
+
let storage: typeof import("./store.js");
|
|
48
|
+
try {
|
|
49
|
+
storage = await import("./store.js");
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (
|
|
52
|
+
error instanceof Error &&
|
|
53
|
+
"code" in error &&
|
|
54
|
+
error.code === "ERR_UNKNOWN_BUILTIN_MODULE"
|
|
55
|
+
)
|
|
56
|
+
throw new Error(
|
|
57
|
+
"Message boards require Node.js 22.13 or newer with SQLite support",
|
|
58
|
+
{ cause: error },
|
|
59
|
+
);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
storage.validateMutation(params);
|
|
63
|
+
if (write) {
|
|
64
|
+
for (const value of [
|
|
65
|
+
...(params.agents_to_notify ?? []),
|
|
66
|
+
...(params.target_agent === undefined ? [] : [params.target_agent]),
|
|
67
|
+
]) {
|
|
68
|
+
const target = agentPath(value, caller.agentName);
|
|
69
|
+
if (!memberNames.includes(target))
|
|
70
|
+
throw new Error(`Agent not bound to this tree: ${target}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const exists = existsSync(caller.databasePath);
|
|
74
|
+
if (
|
|
75
|
+
!exists &&
|
|
76
|
+
params.action === "search_posts" &&
|
|
77
|
+
params.after_message_id !== undefined
|
|
78
|
+
)
|
|
79
|
+
throw new Error("Post not found in this board");
|
|
80
|
+
if (
|
|
81
|
+
!exists &&
|
|
82
|
+
(!write ||
|
|
83
|
+
(params.action !== "create_channel" &&
|
|
84
|
+
params.new_channel_name === undefined))
|
|
85
|
+
) {
|
|
86
|
+
if (["get_channels", "search_posts", "list_boards"].includes(params.action))
|
|
87
|
+
return { value: page([], params), recipients: [] };
|
|
88
|
+
throw new Error("This board has no channels or posts yet");
|
|
89
|
+
}
|
|
90
|
+
const store = new storage.BoardStore(caller.databasePath, write);
|
|
91
|
+
try {
|
|
92
|
+
const scope = {
|
|
93
|
+
boardId: caller.boardId,
|
|
94
|
+
rootSessionId: caller.rootSessionId,
|
|
95
|
+
ownerFolder: caller.ownerFolder,
|
|
96
|
+
callerSessionId: caller.sessionId,
|
|
97
|
+
agentName: caller.agentName,
|
|
98
|
+
members: memberNames,
|
|
99
|
+
};
|
|
100
|
+
return write
|
|
101
|
+
? store.execute(scope, params, requestId)
|
|
102
|
+
: boundedBoardRead(params, (request) =>
|
|
103
|
+
store.execute(scope, request, requestId),
|
|
104
|
+
);
|
|
105
|
+
} finally {
|
|
106
|
+
store.close();
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/awareness.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
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
|
+
registerContextBriefing,
|
|
11
|
+
} from "./context-briefing.js";
|
|
12
|
+
import { binding } from "./identity.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
|
+
constructor(
|
|
32
|
+
pi: ExtensionAPI,
|
|
33
|
+
read: (ctx: ExtensionContext) => Promise<unknown>,
|
|
34
|
+
availability: (ctx: ExtensionContext, error?: unknown) => void,
|
|
35
|
+
privateBriefing: () => typeof boardBriefing | undefined,
|
|
36
|
+
gpt: () => boolean = () => false,
|
|
37
|
+
) {
|
|
38
|
+
this.pi = pi;
|
|
39
|
+
this.read = read;
|
|
40
|
+
this.availability = availability;
|
|
41
|
+
this.gpt = gpt;
|
|
42
|
+
this.privateBriefing = privateBriefing;
|
|
43
|
+
registerContextBriefing(
|
|
44
|
+
pi,
|
|
45
|
+
"board",
|
|
46
|
+
(ctx) => binding(ctx).enabled,
|
|
47
|
+
(ctx, windowId) => this.record(ctx, windowId),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
private readonly privateBriefing: () => typeof boardBriefing | undefined;
|
|
51
|
+
|
|
52
|
+
refresh(ctx: ExtensionContext): AwarenessState {
|
|
53
|
+
const own = binding(ctx);
|
|
54
|
+
const signature = JSON.stringify([
|
|
55
|
+
own.boardId,
|
|
56
|
+
own.rootSessionId,
|
|
57
|
+
own.agentName,
|
|
58
|
+
own.upstream,
|
|
59
|
+
own.enabled,
|
|
60
|
+
]);
|
|
61
|
+
const saved = ctx.sessionManager
|
|
62
|
+
.getEntries()
|
|
63
|
+
.findLast(
|
|
64
|
+
(entry) =>
|
|
65
|
+
entry.type === "custom" &&
|
|
66
|
+
entry.customType === STATE &&
|
|
67
|
+
readState(entry.data).sessionId === own.sessionId,
|
|
68
|
+
);
|
|
69
|
+
if (saved?.type === "custom") {
|
|
70
|
+
const state = readState(saved.data);
|
|
71
|
+
if (state.signature === signature) return state;
|
|
72
|
+
}
|
|
73
|
+
const state = { sessionId: own.sessionId, signature, epoch: randomUUID() };
|
|
74
|
+
this.pi.appendEntry(STATE, state);
|
|
75
|
+
return state;
|
|
76
|
+
}
|
|
77
|
+
seen(ctx: ExtensionContext): string[] {
|
|
78
|
+
const own = binding(ctx);
|
|
79
|
+
return ctx.sessionManager.getEntries().flatMap((entry) => {
|
|
80
|
+
if (
|
|
81
|
+
entry.type !== "custom" ||
|
|
82
|
+
entry.customType !== "shepherdr-board-seen"
|
|
83
|
+
)
|
|
84
|
+
return [];
|
|
85
|
+
const data = entry.data as {
|
|
86
|
+
boardId?: string;
|
|
87
|
+
agentName?: string;
|
|
88
|
+
ids?: unknown;
|
|
89
|
+
};
|
|
90
|
+
return data?.boardId === own.boardId &&
|
|
91
|
+
data.agentName === own.agentName &&
|
|
92
|
+
Array.isArray(data.ids)
|
|
93
|
+
? data.ids.filter((id): id is string => typeof id === "string")
|
|
94
|
+
: [];
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
markSeen(ctx: ExtensionContext, ids: string[]) {
|
|
98
|
+
if (!ids.length) return;
|
|
99
|
+
const own = binding(ctx);
|
|
100
|
+
this.pi.appendEntry("shepherdr-board-seen", {
|
|
101
|
+
boardId: own.boardId,
|
|
102
|
+
agentName: own.agentName,
|
|
103
|
+
ids,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
markRead(ctx: ExtensionContext, value: unknown) {
|
|
107
|
+
if (!value || typeof value !== "object") return;
|
|
108
|
+
const record = value as {
|
|
109
|
+
message_id?: unknown;
|
|
110
|
+
root_post?: unknown;
|
|
111
|
+
results?: unknown;
|
|
112
|
+
};
|
|
113
|
+
const posts = [
|
|
114
|
+
record,
|
|
115
|
+
record.root_post,
|
|
116
|
+
...(Array.isArray(record.results) ? record.results : []),
|
|
117
|
+
];
|
|
118
|
+
this.markSeen(
|
|
119
|
+
ctx,
|
|
120
|
+
posts.flatMap((post) =>
|
|
121
|
+
post &&
|
|
122
|
+
typeof post === "object" &&
|
|
123
|
+
"message_id" in post &&
|
|
124
|
+
typeof post.message_id === "string"
|
|
125
|
+
? [post.message_id]
|
|
126
|
+
: [],
|
|
127
|
+
),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private async record(ctx: ExtensionContext, windowId?: string) {
|
|
132
|
+
const own = binding(ctx);
|
|
133
|
+
const state = this.refresh(ctx);
|
|
134
|
+
if (!own.enabled) return;
|
|
135
|
+
const window = contextBriefingWindow(ctx, windowId);
|
|
136
|
+
const key = JSON.stringify([own.sessionId, window, state.epoch]);
|
|
137
|
+
await recordContextBriefing(this.pi, ctx, OWNER, key, async () => {
|
|
138
|
+
let population: "empty" | "populated" | "unavailable";
|
|
139
|
+
try {
|
|
140
|
+
const value = await this.read(ctx);
|
|
141
|
+
if (!binding(ctx).enabled) return;
|
|
142
|
+
if (
|
|
143
|
+
!value ||
|
|
144
|
+
typeof value !== "object" ||
|
|
145
|
+
!("results" in value) ||
|
|
146
|
+
!Array.isArray(value.results)
|
|
147
|
+
)
|
|
148
|
+
throw new Error("Invalid board population response");
|
|
149
|
+
population = value.results.length ? "populated" : "empty";
|
|
150
|
+
this.availability(ctx);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
ctx.signal?.throwIfAborted();
|
|
153
|
+
population = "unavailable";
|
|
154
|
+
this.availability(ctx, error);
|
|
155
|
+
}
|
|
156
|
+
ctx.signal?.throwIfAborted();
|
|
157
|
+
if (!binding(ctx).enabled) return;
|
|
158
|
+
if (
|
|
159
|
+
binding(ctx).enabled !== own.enabled ||
|
|
160
|
+
this.refresh(ctx).epoch !== state.epoch
|
|
161
|
+
)
|
|
162
|
+
throw new Error("Board membership changed during its briefing");
|
|
163
|
+
const privateBriefing = this.privateBriefing();
|
|
164
|
+
return privateBriefing
|
|
165
|
+
? privateBriefing(Boolean(own.upstream), population)
|
|
166
|
+
: boardBriefing(Boolean(own.upstream), population, this.gpt());
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function readState(value: unknown): AwarenessState {
|
|
172
|
+
if (
|
|
173
|
+
!value ||
|
|
174
|
+
typeof value !== "object" ||
|
|
175
|
+
!("sessionId" in value) ||
|
|
176
|
+
typeof value.sessionId !== "string" ||
|
|
177
|
+
!("signature" in value) ||
|
|
178
|
+
typeof value.signature !== "string" ||
|
|
179
|
+
!("epoch" in value) ||
|
|
180
|
+
typeof value.epoch !== "string"
|
|
181
|
+
)
|
|
182
|
+
throw new Error("Invalid saved board awareness");
|
|
183
|
+
return value as AwarenessState;
|
|
184
|
+
}
|
package/src/briefing.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
? "Shared board available; no posts yet."
|
|
11
|
+
: "Shared board status unavailable. Check board help and retry reading when available.";
|
|
12
|
+
return [
|
|
13
|
+
state,
|
|
14
|
+
member ? "The root agent owns setup; follow your assigned task." : "",
|
|
15
|
+
gpt
|
|
16
|
+
? "Post only work-changing ownership, decisions, findings or blockers. Reply in existing threads, link instead of repeating. Personal checkpoints belong in notes. No acknowledgements, routine status or duplicate completion reports."
|
|
17
|
+
: "Post only new information that changes another agent's work: ownership, decisions, findings or blockers. Reply in existing threads; link rather than repeat. Keep personal checkpoints in notes. Skip acknowledgements, routine status and duplicated completion reports.",
|
|
18
|
+
gpt
|
|
19
|
+
? "Channels group workstreams, threads group topics. Reuse relevant channels, not catch-alls. New channels need distinct coordination purpose, never mere task/result announcements."
|
|
20
|
+
: "Channels group shared workstreams; threads group topics. Reuse a relevant channel, not a catch-all. Create a channel only for a distinct coordination need, never just to announce a task or result.",
|
|
21
|
+
]
|
|
22
|
+
.filter(Boolean)
|
|
23
|
+
.join(" ");
|
|
24
|
+
}
|