@howaboua/pi-agent-board 0.0.1 → 0.0.3
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 +26 -1
- package/README.md +39 -2
- package/dist/viewer/archive.js +69 -12
- package/dist/viewer/web/app.js +36 -6
- package/dist/viewer/web/index.html +3 -1
- package/dist/viewer/web/style.css +36 -0
- package/index.ts +96 -10
- package/package.json +7 -4
- package/src/activity.ts +1 -0
- package/src/archive.ts +5 -0
- package/src/awareness.ts +7 -4
- package/src/briefing.ts +21 -6
- package/src/config.ts +94 -6
- package/src/consolidation.ts +32 -0
- package/src/contract.ts +60 -3
- package/src/delivery.ts +9 -0
- package/src/identity.ts +2 -2
- package/src/paging.ts +7 -8
- package/src/queries.ts +38 -43
- package/src/response.ts +20 -0
- package/src/runtime.ts +62 -13
- package/src/search.ts +93 -0
- package/src/store.ts +71 -7
- package/src/summaries.ts +124 -0
- package/src/summarizer.ts +200 -0
- package/src/tool.ts +7 -2
- package/src/viewer/archive.ts +86 -12
- package/src/viewer/contracts.ts +17 -1
- package/src/viewer/web/app.js +36 -6
- package/src/viewer/web/index.html +3 -1
- package/src/viewer/web/style.css +36 -0
package/src/runtime.ts
CHANGED
|
@@ -8,14 +8,23 @@ import {
|
|
|
8
8
|
} from "./activity.js";
|
|
9
9
|
import { executeArchive, readSubscribedUpdates } from "./archive.js";
|
|
10
10
|
import { BoardAwareness } from "./awareness.js";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
type BoardScope,
|
|
13
|
+
writeBoardConfig,
|
|
14
|
+
writeBoardSummaryAutomatic,
|
|
15
|
+
} from "./config.js";
|
|
12
16
|
import {
|
|
13
17
|
agentPath,
|
|
14
18
|
type BoardParams,
|
|
15
19
|
boardHelp,
|
|
20
|
+
gptBoardHelp,
|
|
16
21
|
parseBoardRequest,
|
|
17
22
|
} from "./contract.js";
|
|
18
|
-
import {
|
|
23
|
+
import {
|
|
24
|
+
initializeDelivery,
|
|
25
|
+
isMissingOptionalModule,
|
|
26
|
+
preparePolicyMessage,
|
|
27
|
+
} from "./delivery.js";
|
|
19
28
|
import {
|
|
20
29
|
binding,
|
|
21
30
|
members,
|
|
@@ -26,14 +35,18 @@ import {
|
|
|
26
35
|
} from "./identity.js";
|
|
27
36
|
import type { BoardAdapter } from "./integration.js";
|
|
28
37
|
import { type BoardEnvelope, parseEnvelope } from "./protocol.js";
|
|
38
|
+
import { BoardSummarizer } from "./summarizer.js";
|
|
29
39
|
import { createBoardTool } from "./tool.js";
|
|
30
40
|
import { BoardTurns } from "./turns.js";
|
|
31
41
|
|
|
32
42
|
export class BoardRuntime {
|
|
43
|
+
gptPromptMode = false;
|
|
44
|
+
private preparePromptMode: ((ctx: ExtensionContext) => void) | undefined;
|
|
33
45
|
private readonly pi: ExtensionAPI;
|
|
34
46
|
private adapter: BoardAdapter | undefined;
|
|
35
47
|
private readonly turns: BoardTurns;
|
|
36
48
|
private readonly awareness: BoardAwareness;
|
|
49
|
+
private readonly summarizer = new BoardSummarizer();
|
|
37
50
|
private refreshTools: (() => void) | undefined;
|
|
38
51
|
private lastSetting: { sessionId: string; enabled: boolean } | undefined;
|
|
39
52
|
private configError: string | undefined;
|
|
@@ -47,6 +60,7 @@ export class BoardRuntime {
|
|
|
47
60
|
(ctx) => this.population(ctx),
|
|
48
61
|
availability,
|
|
49
62
|
() => this.adapter?.briefing,
|
|
63
|
+
() => this.gptPromptMode,
|
|
50
64
|
);
|
|
51
65
|
this.turns = new BoardTurns(
|
|
52
66
|
pi,
|
|
@@ -55,11 +69,14 @@ export class BoardRuntime {
|
|
|
55
69
|
(ctx, ids) => this.awareness.markSeen(ctx, ids),
|
|
56
70
|
);
|
|
57
71
|
pi.on("session_start", (_event, ctx) => {
|
|
72
|
+
this.summarizer.cancelAll();
|
|
58
73
|
this.availabilityWarning = undefined;
|
|
59
74
|
setBoardDefault(ctx, true);
|
|
60
75
|
return this.refresh(ctx);
|
|
61
76
|
});
|
|
77
|
+
pi.on("session_shutdown", () => this.summarizer.cancelAll());
|
|
62
78
|
pi.on("before_agent_start", async (_event, ctx) => {
|
|
79
|
+
this.preparePromptMode?.(ctx);
|
|
63
80
|
// Reconcile resumed/offline children before tool/prompt preparation.
|
|
64
81
|
const own = binding(ctx);
|
|
65
82
|
if (own.upstream) {
|
|
@@ -88,11 +105,11 @@ export class BoardRuntime {
|
|
|
88
105
|
catchup.accepted();
|
|
89
106
|
if (catchup.content)
|
|
90
107
|
return {
|
|
91
|
-
message: {
|
|
108
|
+
message: preparePolicyMessage({
|
|
92
109
|
customType: "shepherdr-board-catchup",
|
|
93
110
|
content: catchup.content.trim(),
|
|
94
111
|
display: true,
|
|
95
|
-
},
|
|
112
|
+
}),
|
|
96
113
|
};
|
|
97
114
|
});
|
|
98
115
|
}
|
|
@@ -116,6 +133,9 @@ export class BoardRuntime {
|
|
|
116
133
|
setToolRefresh(refresh: () => void) {
|
|
117
134
|
this.refreshTools = refresh;
|
|
118
135
|
}
|
|
136
|
+
setPromptModePreparation(prepare: (ctx: ExtensionContext) => void) {
|
|
137
|
+
this.preparePromptMode = prepare;
|
|
138
|
+
}
|
|
119
139
|
private async population(ctx: ExtensionContext) {
|
|
120
140
|
const own = binding(ctx);
|
|
121
141
|
if (own.upstream) {
|
|
@@ -186,7 +206,7 @@ export class BoardRuntime {
|
|
|
186
206
|
.some((tool) => ["exec", "code", "notebook"].includes(tool));
|
|
187
207
|
return {
|
|
188
208
|
content: result.length
|
|
189
|
-
? `\n\
|
|
209
|
+
? `\n\n${this.gptPromptMode ? "Unread board updates. Read task-relevant threads:" : "Unread board updates available; read if relevant to the current task:"} ${[
|
|
190
210
|
...new Set(result.map((post) => post.thread_id)),
|
|
191
211
|
]
|
|
192
212
|
.map((thread_id) => {
|
|
@@ -220,18 +240,17 @@ export class BoardRuntime {
|
|
|
220
240
|
if (own.enabled) active.push("board");
|
|
221
241
|
this.pi.setActiveTools(active);
|
|
222
242
|
this.refreshTools?.();
|
|
223
|
-
if (!own.enabled)
|
|
243
|
+
if (!own.enabled) {
|
|
244
|
+
this.turns.active.clear();
|
|
245
|
+
this.summarizer.cancelAll();
|
|
246
|
+
}
|
|
224
247
|
const changed =
|
|
225
248
|
this.lastSetting?.sessionId !== own.sessionId ||
|
|
226
249
|
this.lastSetting.enabled !== own.enabled;
|
|
227
250
|
this.lastSetting = { sessionId: own.sessionId, enabled: own.enabled };
|
|
228
251
|
if (changed && !own.upstream) await this.propagateEnabled(ctx, own.enabled);
|
|
229
252
|
}
|
|
230
|
-
async setSetting(
|
|
231
|
-
ctx: ExtensionContext,
|
|
232
|
-
scope: BoardScope,
|
|
233
|
-
enabled: boolean | undefined,
|
|
234
|
-
) {
|
|
253
|
+
async setSetting(ctx: ExtensionContext, scope: BoardScope, enabled: boolean) {
|
|
235
254
|
if (!ctx.isIdle())
|
|
236
255
|
throw new Error("Change the board setting after this session settles");
|
|
237
256
|
const own = binding(ctx);
|
|
@@ -246,6 +265,10 @@ export class BoardRuntime {
|
|
|
246
265
|
const setting = own.upstream ? undefined : rootBoardSetting(ctx);
|
|
247
266
|
return `Board ${own.enabled ? "on" : "off"} (${setting?.source ?? "inherited from owner"})${includeArchive ? `. ${own.boardId}\nArchive: ${own.databasePath}` : ""}${setting?.error ? `\n${setting.error}` : ""}`;
|
|
248
267
|
}
|
|
268
|
+
setAutomaticSummaries(ctx: ExtensionContext, automatic: boolean) {
|
|
269
|
+
writeBoardSummaryAutomatic(ctx.cwd, automatic);
|
|
270
|
+
if (!automatic) this.summarizer.cancelAll();
|
|
271
|
+
}
|
|
249
272
|
async execute(ctx: ExtensionContext, input: unknown, requestId: string) {
|
|
250
273
|
const params = parseBoardRequest(input);
|
|
251
274
|
const caller = binding(ctx);
|
|
@@ -276,7 +299,9 @@ export class BoardRuntime {
|
|
|
276
299
|
"warning",
|
|
277
300
|
);
|
|
278
301
|
}
|
|
279
|
-
return
|
|
302
|
+
return params.action === "help" && this.gptPromptMode
|
|
303
|
+
? gptBoardHelp(value)
|
|
304
|
+
: value;
|
|
280
305
|
}
|
|
281
306
|
async handle(
|
|
282
307
|
ctx: ExtensionContext,
|
|
@@ -414,6 +439,18 @@ export class BoardRuntime {
|
|
|
414
439
|
request.requestId,
|
|
415
440
|
);
|
|
416
441
|
if ("notice" in result && result.notice) {
|
|
442
|
+
// Replayed mutations have no notice: never start or cancel twice for a retry.
|
|
443
|
+
if (params.action === "set_status") {
|
|
444
|
+
if (params.status === "open")
|
|
445
|
+
this.summarizer.cancel(own.boardId, result.notice.thread_id);
|
|
446
|
+
else
|
|
447
|
+
this.summarizer.start(
|
|
448
|
+
ctx,
|
|
449
|
+
own,
|
|
450
|
+
result.notice.thread_id,
|
|
451
|
+
result.notice.message_id,
|
|
452
|
+
);
|
|
453
|
+
}
|
|
417
454
|
for (let start = 0; start < result.recipients.length; start += 16) {
|
|
418
455
|
await Promise.all(
|
|
419
456
|
result.recipients.slice(start, start + 16).map(async (target) => {
|
|
@@ -528,6 +565,18 @@ async function createRuntime(pi: ExtensionAPI) {
|
|
|
528
565
|
const runtime = new BoardRuntime(pi);
|
|
529
566
|
const tool = createBoardTool(runtime);
|
|
530
567
|
pi.registerTool(tool);
|
|
568
|
+
try {
|
|
569
|
+
const { registerGptPromptMode, prepareGptPromptMode } = await import(
|
|
570
|
+
"@howaboua/pi-codex-conversion/prompt-mode"
|
|
571
|
+
);
|
|
572
|
+
runtime.setPromptModePreparation((ctx) => prepareGptPromptMode(pi, ctx));
|
|
573
|
+
registerGptPromptMode(pi, (active) => {
|
|
574
|
+
runtime.gptPromptMode = active;
|
|
575
|
+
return [tool];
|
|
576
|
+
});
|
|
577
|
+
} catch (error) {
|
|
578
|
+
if (!isMissingOptionalModule(error, "prompt-mode")) throw error;
|
|
579
|
+
}
|
|
531
580
|
registerBoardActivityRenderer(pi);
|
|
532
581
|
try {
|
|
533
582
|
const { adaptToolForCodeMode, registerCodeModeExtensionTools } =
|
|
@@ -538,7 +587,7 @@ async function createRuntime(pi: ExtensionAPI) {
|
|
|
538
587
|
adaptToolForCodeMode(tool, {
|
|
539
588
|
prepareInput: (input) =>
|
|
540
589
|
input === undefined || input === null ? {} : input,
|
|
541
|
-
|
|
590
|
+
discovery: "help",
|
|
542
591
|
}),
|
|
543
592
|
],
|
|
544
593
|
{ isActive: (ctx) => runtime.enabled(ctx) },
|
package/src/search.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { caseFold } from "unicode-case-folding";
|
|
2
|
+
import { agentPath, type BoardParams } from "./contract.js";
|
|
3
|
+
import { budget, type PostRecord, page, preview, window } from "./paging.js";
|
|
4
|
+
import type { BoardStore } from "./store.js";
|
|
5
|
+
import { summaryPreview } from "./summaries.js";
|
|
6
|
+
|
|
7
|
+
/** Summaries replace their source posts, never supplement them in default search. */
|
|
8
|
+
export function searchBoard(
|
|
9
|
+
store: BoardStore,
|
|
10
|
+
board: string,
|
|
11
|
+
params: BoardParams,
|
|
12
|
+
) {
|
|
13
|
+
const { limit, offset } = window(params);
|
|
14
|
+
const summarized = store.summaries.available && !params.full;
|
|
15
|
+
const clauses = ["board_id=?"];
|
|
16
|
+
const args: (string | number)[] = [board];
|
|
17
|
+
if (params.channel_name !== undefined) {
|
|
18
|
+
clauses.push("channel_name=?");
|
|
19
|
+
args.push(params.channel_name);
|
|
20
|
+
}
|
|
21
|
+
if (params.author !== undefined) {
|
|
22
|
+
clauses.push(`(author=? OR (kind='summary' AND EXISTS (
|
|
23
|
+
SELECT 1 FROM posts a WHERE a.board_id=c.board_id AND a.root=c.thread_id AND a.author=?)))`);
|
|
24
|
+
const author = agentPath(params.author, "/root");
|
|
25
|
+
args.push(author, author);
|
|
26
|
+
}
|
|
27
|
+
if (params.query !== undefined) {
|
|
28
|
+
clauses.push("instr(body_search,?)>0");
|
|
29
|
+
args.push(caseFold(params.query));
|
|
30
|
+
}
|
|
31
|
+
if (params.after_message_id !== undefined) {
|
|
32
|
+
const after = store.one<{ created_at: string; seq: number }>(
|
|
33
|
+
"SELECT created_at,seq FROM posts WHERE board_id=? AND id=?",
|
|
34
|
+
board,
|
|
35
|
+
params.after_message_id,
|
|
36
|
+
);
|
|
37
|
+
if (!after) throw new Error("Post not found in this board");
|
|
38
|
+
clauses.push("(created_at,seq)>(?,?)");
|
|
39
|
+
args.push(after.created_at, after.seq);
|
|
40
|
+
}
|
|
41
|
+
const rows = store.all<
|
|
42
|
+
| (PostRecord & { kind: "post" })
|
|
43
|
+
| (Omit<PostRecord, "author"> & { kind: "summary"; author: null })
|
|
44
|
+
>(
|
|
45
|
+
`WITH candidates AS (
|
|
46
|
+
SELECT p.board_id,p.id AS message_id,p.channel AS channel_name,p.author,p.root AS thread_id,
|
|
47
|
+
p.created_at,p.text,p.body_search,p.seq,'post' AS kind FROM posts p
|
|
48
|
+
${summarized ? "WHERE NOT EXISTS (SELECT 1 FROM thread_summaries s WHERE s.board_id=p.board_id AND s.thread_id=p.root)" : ""}
|
|
49
|
+
${
|
|
50
|
+
summarized
|
|
51
|
+
? `UNION ALL
|
|
52
|
+
SELECT s.board_id,p.id,p.channel,NULL,s.thread_id,p.created_at,s.text,s.body_search,p.seq,'summary'
|
|
53
|
+
FROM thread_summaries s JOIN posts p ON p.board_id=s.board_id AND p.id=s.through_message_id`
|
|
54
|
+
: ""
|
|
55
|
+
}
|
|
56
|
+
) SELECT * FROM candidates c WHERE ${clauses.join(" AND ")}
|
|
57
|
+
ORDER BY created_at DESC,seq DESC LIMIT ? OFFSET ?`,
|
|
58
|
+
...args,
|
|
59
|
+
limit + 1,
|
|
60
|
+
offset,
|
|
61
|
+
);
|
|
62
|
+
const chars = budget(params, Math.min(rows.length, limit));
|
|
63
|
+
return page(
|
|
64
|
+
rows.map((row) => {
|
|
65
|
+
const status = store.threadStatus(board, row.thread_id);
|
|
66
|
+
if (row.kind === "summary") {
|
|
67
|
+
const summary = store.summaries.get(board, row.thread_id);
|
|
68
|
+
if (!summary)
|
|
69
|
+
throw new Error(
|
|
70
|
+
"Thread summary is no longer available; repeat the search",
|
|
71
|
+
);
|
|
72
|
+
return {
|
|
73
|
+
kind: "summary",
|
|
74
|
+
thread_id: row.thread_id,
|
|
75
|
+
channel_name: row.channel_name,
|
|
76
|
+
...status,
|
|
77
|
+
summary: summaryPreview(summary, chars),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
// Do not expose SQL-only columns as post metadata.
|
|
81
|
+
const post: PostRecord = {
|
|
82
|
+
message_id: row.message_id,
|
|
83
|
+
channel_name: row.channel_name,
|
|
84
|
+
author: row.author,
|
|
85
|
+
thread_id: row.thread_id,
|
|
86
|
+
created_at: row.created_at,
|
|
87
|
+
text: row.text,
|
|
88
|
+
};
|
|
89
|
+
return { kind: "post", ...preview(post, chars), ...status };
|
|
90
|
+
}),
|
|
91
|
+
params,
|
|
92
|
+
);
|
|
93
|
+
}
|
package/src/store.ts
CHANGED
|
@@ -8,10 +8,12 @@ import {
|
|
|
8
8
|
type BoardParams,
|
|
9
9
|
MUTATIONS,
|
|
10
10
|
required,
|
|
11
|
+
type ThreadStatus,
|
|
11
12
|
} from "./contract.js";
|
|
12
13
|
import { metadata, POST_COLUMNS, type PostRecord, preview } from "./paging.js";
|
|
13
14
|
import { queryBoard } from "./queries.js";
|
|
14
15
|
import { serializeBoardResult } from "./response.js";
|
|
16
|
+
import { ThreadSummaries } from "./summaries.js";
|
|
15
17
|
|
|
16
18
|
export interface BoardScope {
|
|
17
19
|
boardId: string;
|
|
@@ -30,10 +32,16 @@ CREATE INDEX posts_root ON posts(board_id,root,created_at,seq);
|
|
|
30
32
|
CREATE INDEX posts_order ON posts(board_id,created_at,seq);
|
|
31
33
|
CREATE TABLE subscriptions (board_id TEXT NOT NULL,target TEXT NOT NULL,agent TEXT NOT NULL,enabled INTEGER NOT NULL,PRIMARY KEY(board_id,target,agent));
|
|
32
34
|
PRAGMA user_version=1;`;
|
|
35
|
+
const THREAD_STATUS_SCHEMA = `CREATE TABLE IF NOT EXISTS thread_statuses (
|
|
36
|
+
board_id TEXT NOT NULL, thread_id TEXT NOT NULL,
|
|
37
|
+
status TEXT NOT NULL CHECK(status IN ('open','completed','abandoned')),
|
|
38
|
+
message_id TEXT NOT NULL, PRIMARY KEY(board_id,thread_id));`;
|
|
33
39
|
|
|
34
40
|
/** One transaction/connection per call. Reads never create a file, schema or board. */
|
|
35
41
|
export class BoardStore {
|
|
36
42
|
readonly db: DatabaseSync;
|
|
43
|
+
readonly summaries: ThreadSummaries;
|
|
44
|
+
private readonly hasThreadStatuses: boolean;
|
|
37
45
|
constructor(path: string, write: boolean) {
|
|
38
46
|
const fresh = !existsSync(path);
|
|
39
47
|
if (write && fresh) mkdirSync(dirname(path), { recursive: true });
|
|
@@ -56,6 +64,11 @@ export class BoardStore {
|
|
|
56
64
|
this.db.exec(SCHEMA);
|
|
57
65
|
else if (version !== 1)
|
|
58
66
|
throw new Error("Unsupported Shepherdr board archive schema");
|
|
67
|
+
if (write) this.db.exec(THREAD_STATUS_SCHEMA);
|
|
68
|
+
this.hasThreadStatuses = !!this.one(
|
|
69
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='thread_statuses'",
|
|
70
|
+
);
|
|
71
|
+
this.summaries = new ThreadSummaries(this.db, write);
|
|
59
72
|
if (write) this.db.exec("COMMIT");
|
|
60
73
|
} catch (error) {
|
|
61
74
|
this.db.close();
|
|
@@ -86,6 +99,23 @@ export class BoardStore {
|
|
|
86
99
|
throw new Error("thread_id must identify a first post");
|
|
87
100
|
return post;
|
|
88
101
|
}
|
|
102
|
+
threadStatus(
|
|
103
|
+
board: string,
|
|
104
|
+
thread: string,
|
|
105
|
+
): {
|
|
106
|
+
status: ThreadStatus;
|
|
107
|
+
status_message_id: string | null;
|
|
108
|
+
} {
|
|
109
|
+
return (
|
|
110
|
+
(this.hasThreadStatuses
|
|
111
|
+
? this.one<{ status: ThreadStatus; status_message_id: string }>(
|
|
112
|
+
"SELECT status,message_id AS status_message_id FROM thread_statuses WHERE board_id=? AND thread_id=?",
|
|
113
|
+
board,
|
|
114
|
+
thread,
|
|
115
|
+
)
|
|
116
|
+
: undefined) ?? { status: "open", status_message_id: null }
|
|
117
|
+
);
|
|
118
|
+
}
|
|
89
119
|
channel(board: string, name: string) {
|
|
90
120
|
const channel = this.one<{
|
|
91
121
|
channel_name: string;
|
|
@@ -171,7 +201,8 @@ export class BoardStore {
|
|
|
171
201
|
}
|
|
172
202
|
private mutate(scope: BoardScope, params: BoardParams, requestId: string) {
|
|
173
203
|
const board = scope.boardId;
|
|
174
|
-
if (params.action === "post"
|
|
204
|
+
if (params.action === "post" || params.action === "set_status")
|
|
205
|
+
return this.publish(scope, params, requestId);
|
|
175
206
|
if (params.action === "create_channel") {
|
|
176
207
|
const name = required(params.channel_name, "channel_name");
|
|
177
208
|
const now = new Date().toISOString();
|
|
@@ -238,7 +269,13 @@ export class BoardStore {
|
|
|
238
269
|
if (previous) {
|
|
239
270
|
if (previous.request !== request)
|
|
240
271
|
throw new Error("Invocation ID already used for a different post");
|
|
241
|
-
return {
|
|
272
|
+
return {
|
|
273
|
+
value: {
|
|
274
|
+
...metadata(this.post(board, previous.id)),
|
|
275
|
+
...(params.action === "set_status" ? { status: params.status } : {}),
|
|
276
|
+
},
|
|
277
|
+
recipients: [],
|
|
278
|
+
};
|
|
242
279
|
}
|
|
243
280
|
const recipients = new Set(
|
|
244
281
|
(params.agents_to_notify ?? []).map((name) => this.resolve(scope, name)),
|
|
@@ -251,6 +288,13 @@ export class BoardStore {
|
|
|
251
288
|
const post = this.root(board, params.thread_id);
|
|
252
289
|
channel = post.channel_name;
|
|
253
290
|
root = post.thread_id;
|
|
291
|
+
if (
|
|
292
|
+
params.action === "post" &&
|
|
293
|
+
this.threadStatus(board, root).status !== "open"
|
|
294
|
+
)
|
|
295
|
+
throw new Error(
|
|
296
|
+
"Thread is closed. The parent session must reopen it before replies.",
|
|
297
|
+
);
|
|
254
298
|
} else if (params.new_channel_name !== undefined) {
|
|
255
299
|
channel = params.new_channel_name;
|
|
256
300
|
this.ensureBoard(scope, now);
|
|
@@ -275,7 +319,10 @@ export class BoardStore {
|
|
|
275
319
|
author: scope.agentName,
|
|
276
320
|
thread_id: root,
|
|
277
321
|
created_at: now,
|
|
278
|
-
text:
|
|
322
|
+
text:
|
|
323
|
+
params.action === "set_status"
|
|
324
|
+
? required(params.comment, "comment")
|
|
325
|
+
: required(params.text, "text"),
|
|
279
326
|
};
|
|
280
327
|
this.run(
|
|
281
328
|
"INSERT INTO posts(board_id,id,channel,root,author,created_at,text,body_search,request_id,request) VALUES(?,?,?,?,?,?,?,?,?,?)",
|
|
@@ -290,9 +337,23 @@ export class BoardStore {
|
|
|
290
337
|
key,
|
|
291
338
|
request,
|
|
292
339
|
);
|
|
340
|
+
if (params.action === "set_status") {
|
|
341
|
+
this.run(
|
|
342
|
+
`INSERT INTO thread_statuses(board_id,thread_id,status,message_id) VALUES(?,?,?,?)
|
|
343
|
+
ON CONFLICT(board_id,thread_id) DO UPDATE SET status=excluded.status,message_id=excluded.message_id`,
|
|
344
|
+
board,
|
|
345
|
+
root,
|
|
346
|
+
required(params.status, "status"),
|
|
347
|
+
id,
|
|
348
|
+
);
|
|
349
|
+
this.summaries.discard(board, root);
|
|
350
|
+
}
|
|
293
351
|
this.subscription(board, `thread:${root}`, scope.agentName, true, true);
|
|
294
352
|
return {
|
|
295
|
-
value:
|
|
353
|
+
value: {
|
|
354
|
+
...metadata(post),
|
|
355
|
+
...(params.action === "set_status" ? { status: params.status } : {}),
|
|
356
|
+
},
|
|
296
357
|
notice: preview(post, 150),
|
|
297
358
|
recipients: [...recipients],
|
|
298
359
|
};
|
|
@@ -319,9 +380,12 @@ export function validateMutation(params: BoardParams) {
|
|
|
319
380
|
"Channel must be 1-128 UTF-8 bytes without edge whitespace/control characters",
|
|
320
381
|
);
|
|
321
382
|
}
|
|
322
|
-
if (params.action === "post") {
|
|
323
|
-
const
|
|
383
|
+
if (params.action === "post" || params.action === "set_status") {
|
|
384
|
+
const field = params.action === "set_status" ? "comment" : "text";
|
|
385
|
+
const text = required(params[field], field);
|
|
324
386
|
if (!text || !text.isWellFormed() || Buffer.byteLength(text) > 65536)
|
|
325
|
-
throw new Error(
|
|
387
|
+
throw new Error(
|
|
388
|
+
`${params.action === "post" ? "Post" : "Comment"} must be 1-65536 UTF-8 bytes`,
|
|
389
|
+
);
|
|
326
390
|
}
|
|
327
391
|
}
|
package/src/summaries.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { caseFold } from "unicode-case-folding";
|
|
3
|
+
|
|
4
|
+
export interface ThreadSummary {
|
|
5
|
+
thread_id: string;
|
|
6
|
+
status_message_id: string;
|
|
7
|
+
through_message_id: string;
|
|
8
|
+
created_at: string;
|
|
9
|
+
provider: string;
|
|
10
|
+
model: string;
|
|
11
|
+
reasoning: string;
|
|
12
|
+
text: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Derived closure snapshots, separate from posts and their activity/subscriptions. */
|
|
16
|
+
export class ThreadSummaries {
|
|
17
|
+
readonly available: boolean;
|
|
18
|
+
private readonly db: DatabaseSync;
|
|
19
|
+
constructor(db: DatabaseSync, write: boolean) {
|
|
20
|
+
this.db = db;
|
|
21
|
+
if (write)
|
|
22
|
+
db.exec(`CREATE TABLE IF NOT EXISTS thread_summaries (
|
|
23
|
+
board_id TEXT NOT NULL, thread_id TEXT NOT NULL,
|
|
24
|
+
status_message_id TEXT NOT NULL, through_message_id TEXT NOT NULL,
|
|
25
|
+
created_at TEXT NOT NULL, provider TEXT NOT NULL, model TEXT NOT NULL,
|
|
26
|
+
reasoning TEXT NOT NULL, text TEXT NOT NULL, body_search TEXT NOT NULL,
|
|
27
|
+
PRIMARY KEY(board_id,thread_id));`);
|
|
28
|
+
this.available = !!db
|
|
29
|
+
.prepare(
|
|
30
|
+
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='thread_summaries'",
|
|
31
|
+
)
|
|
32
|
+
.get();
|
|
33
|
+
}
|
|
34
|
+
get(
|
|
35
|
+
board: string,
|
|
36
|
+
thread: string,
|
|
37
|
+
): (ThreadSummary & { has_newer_posts: boolean }) | undefined {
|
|
38
|
+
if (!this.available) return undefined;
|
|
39
|
+
const row = this.db
|
|
40
|
+
.prepare(
|
|
41
|
+
`SELECT thread_id,status_message_id,through_message_id,created_at,provider,model,reasoning,text,
|
|
42
|
+
EXISTS(SELECT 1 FROM posts p WHERE p.board_id=s.board_id AND p.root=s.thread_id
|
|
43
|
+
AND p.seq>(SELECT seq FROM posts WHERE board_id=s.board_id AND id=s.through_message_id)) AS has_newer_posts
|
|
44
|
+
FROM thread_summaries s WHERE board_id=? AND thread_id=?`,
|
|
45
|
+
)
|
|
46
|
+
.get(board, thread) as
|
|
47
|
+
| (ThreadSummary & { has_newer_posts: number })
|
|
48
|
+
| undefined;
|
|
49
|
+
return row
|
|
50
|
+
? { ...row, has_newer_posts: row.has_newer_posts === 1 }
|
|
51
|
+
: undefined;
|
|
52
|
+
}
|
|
53
|
+
/** A late completion cannot restore a reopened/reclosed or changed snapshot. */
|
|
54
|
+
save(board: string, summary: ThreadSummary): boolean {
|
|
55
|
+
if (
|
|
56
|
+
!summary.text.trim() ||
|
|
57
|
+
!summary.text.isWellFormed() ||
|
|
58
|
+
Buffer.byteLength(summary.text) > 65536
|
|
59
|
+
)
|
|
60
|
+
throw new Error("Summary must be 1-65536 UTF-8 bytes of nonempty text");
|
|
61
|
+
if (
|
|
62
|
+
![summary.provider, summary.model, summary.reasoning].every((value) =>
|
|
63
|
+
value.trim(),
|
|
64
|
+
)
|
|
65
|
+
)
|
|
66
|
+
throw new Error("Summary model metadata is required");
|
|
67
|
+
const result = this.db
|
|
68
|
+
.prepare(`INSERT INTO thread_summaries
|
|
69
|
+
(board_id,thread_id,status_message_id,through_message_id,created_at,provider,model,reasoning,text,body_search)
|
|
70
|
+
SELECT ?,?,?,?,?,?,?,?,?,? WHERE EXISTS (
|
|
71
|
+
SELECT 1 FROM thread_statuses s WHERE s.board_id=? AND s.thread_id=?
|
|
72
|
+
AND s.status<>'open' AND s.message_id=?
|
|
73
|
+
AND (SELECT id FROM posts WHERE board_id=s.board_id AND root=s.thread_id ORDER BY seq DESC LIMIT 1)=?
|
|
74
|
+
)
|
|
75
|
+
ON CONFLICT(board_id,thread_id) DO UPDATE SET
|
|
76
|
+
status_message_id=excluded.status_message_id,through_message_id=excluded.through_message_id,
|
|
77
|
+
created_at=excluded.created_at,provider=excluded.provider,model=excluded.model,
|
|
78
|
+
reasoning=excluded.reasoning,text=excluded.text,body_search=excluded.body_search`)
|
|
79
|
+
.run(
|
|
80
|
+
board,
|
|
81
|
+
summary.thread_id,
|
|
82
|
+
summary.status_message_id,
|
|
83
|
+
summary.through_message_id,
|
|
84
|
+
summary.created_at,
|
|
85
|
+
summary.provider,
|
|
86
|
+
summary.model,
|
|
87
|
+
summary.reasoning,
|
|
88
|
+
summary.text,
|
|
89
|
+
caseFold(summary.text),
|
|
90
|
+
board,
|
|
91
|
+
summary.thread_id,
|
|
92
|
+
summary.status_message_id,
|
|
93
|
+
summary.through_message_id,
|
|
94
|
+
);
|
|
95
|
+
return result.changes === 1;
|
|
96
|
+
}
|
|
97
|
+
discard(board: string, thread: string): void {
|
|
98
|
+
if (this.available)
|
|
99
|
+
this.db
|
|
100
|
+
.prepare(
|
|
101
|
+
"DELETE FROM thread_summaries WHERE board_id=? AND thread_id=?",
|
|
102
|
+
)
|
|
103
|
+
.run(board, thread);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function summaryPreview(
|
|
108
|
+
{
|
|
109
|
+
text,
|
|
110
|
+
provider: _provider,
|
|
111
|
+
model: _model,
|
|
112
|
+
reasoning: _reasoning,
|
|
113
|
+
...metadata
|
|
114
|
+
}: ThreadSummary & { has_newer_posts: boolean },
|
|
115
|
+
chars: number,
|
|
116
|
+
) {
|
|
117
|
+
const points = Array.from(text);
|
|
118
|
+
return {
|
|
119
|
+
...metadata,
|
|
120
|
+
text_preview: points.slice(0, chars).join(""),
|
|
121
|
+
n_chars: points.length,
|
|
122
|
+
truncated: points.length > chars,
|
|
123
|
+
};
|
|
124
|
+
}
|