@howaboua/pi-agent-comms 0.0.0-stage → 0.0.1-dev.14.1.g7d1d3da173aa
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,323 @@
|
|
|
1
|
+
import type { ToolCall, ToolResultMessage } from "@earendil-works/pi-ai";
|
|
2
|
+
import {
|
|
3
|
+
buildSessionProjection,
|
|
4
|
+
type SessionEntry,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { NOTE_RECEIPT, type NoteReceipt, noteReceipt } from "./notes.js";
|
|
7
|
+
import { isNoteWrite } from "./query-input.js";
|
|
8
|
+
|
|
9
|
+
export const NESTED_NOTE_OUTCOME = "notes-compaction:nested-write:v1";
|
|
10
|
+
export type NestedNoteOutcome = {
|
|
11
|
+
protocol: 1;
|
|
12
|
+
runId: string;
|
|
13
|
+
windowId: string;
|
|
14
|
+
callId: string;
|
|
15
|
+
parentCallId: string;
|
|
16
|
+
origin?: "routed";
|
|
17
|
+
} & ({ pending: true; saved: false } | { pending: false; saved: boolean });
|
|
18
|
+
export const NOTES_RUN_ENTRY = "notes-compaction:run:v1";
|
|
19
|
+
|
|
20
|
+
/** A receipt proves persistence. Current-run tool outcomes prove the checkpoint actually completed. */
|
|
21
|
+
export function hasFreshNotes(
|
|
22
|
+
branch: readonly SessionEntry[],
|
|
23
|
+
windowId: string | undefined,
|
|
24
|
+
runId?: string,
|
|
25
|
+
): boolean {
|
|
26
|
+
const { messages, receipts, outcomes } = noteEvidence(
|
|
27
|
+
branch,
|
|
28
|
+
windowId,
|
|
29
|
+
runId,
|
|
30
|
+
);
|
|
31
|
+
const final = messages.findLast((message) => message.role === "assistant");
|
|
32
|
+
if (
|
|
33
|
+
final?.role !== "assistant" ||
|
|
34
|
+
final.stopReason !== "stop" ||
|
|
35
|
+
final.content.some((block) => block.type === "toolCall")
|
|
36
|
+
)
|
|
37
|
+
return false;
|
|
38
|
+
return completedWrites(
|
|
39
|
+
messages.flatMap((message) =>
|
|
40
|
+
message.role === "assistant"
|
|
41
|
+
? message.content.filter((block) => block.type === "toolCall")
|
|
42
|
+
: [],
|
|
43
|
+
),
|
|
44
|
+
messages.flatMap((message) =>
|
|
45
|
+
message.role === "toolResult" ? [message] : [],
|
|
46
|
+
),
|
|
47
|
+
receipts,
|
|
48
|
+
outcomes,
|
|
49
|
+
messages.flatMap((message) =>
|
|
50
|
+
message.role === "toolResult" ? [message] : [],
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Only the preceding tool-call group (or completed same-group predecessors) qualifies. */
|
|
56
|
+
export function hasPrecedingNoteWrite(
|
|
57
|
+
branch: readonly SessionEntry[],
|
|
58
|
+
windowId: string | undefined,
|
|
59
|
+
runId: string,
|
|
60
|
+
requestCallId: string,
|
|
61
|
+
): boolean {
|
|
62
|
+
const { messages, receipts, outcomes } = noteEvidence(
|
|
63
|
+
branch,
|
|
64
|
+
windowId,
|
|
65
|
+
runId,
|
|
66
|
+
);
|
|
67
|
+
const request = messages.findLastIndex(
|
|
68
|
+
(message) =>
|
|
69
|
+
message.role === "assistant" &&
|
|
70
|
+
message.content.some(
|
|
71
|
+
(block) => block.type === "toolCall" && block.id === requestCallId,
|
|
72
|
+
),
|
|
73
|
+
);
|
|
74
|
+
if (request < 0) return false;
|
|
75
|
+
const groups = messages
|
|
76
|
+
.slice(0, request + 1)
|
|
77
|
+
.flatMap((message) =>
|
|
78
|
+
message.role === "assistant"
|
|
79
|
+
? [message.content.filter((block) => block.type === "toolCall")]
|
|
80
|
+
: [],
|
|
81
|
+
)
|
|
82
|
+
.filter((calls) => calls.length > 0);
|
|
83
|
+
const current = groups.pop() ?? [];
|
|
84
|
+
const predecessors = current.slice(
|
|
85
|
+
0,
|
|
86
|
+
current.findIndex((call) => call.id === requestCallId),
|
|
87
|
+
);
|
|
88
|
+
const calls = predecessors.length ? predecessors : (groups.at(-1) ?? []);
|
|
89
|
+
const allResults = messages.flatMap((message) =>
|
|
90
|
+
message.role === "toolResult" ? [message] : [],
|
|
91
|
+
);
|
|
92
|
+
const results = allResults.filter((result) =>
|
|
93
|
+
calls.some(
|
|
94
|
+
(call) => call.id === result.toolCallId && call.name === result.toolName,
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
return (
|
|
98
|
+
calls.length > 0 &&
|
|
99
|
+
calls.every((call) =>
|
|
100
|
+
results.some((result) => result.toolCallId === call.id),
|
|
101
|
+
) &&
|
|
102
|
+
completedWrites(calls, results, receipts, outcomes, allResults)
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function noteEvidence(
|
|
107
|
+
branch: readonly SessionEntry[],
|
|
108
|
+
windowId: string | undefined,
|
|
109
|
+
runId?: string,
|
|
110
|
+
) {
|
|
111
|
+
const lastUser = branch.findLastIndex(
|
|
112
|
+
(entry) => entry.type === "message" && entry.message.role === "user",
|
|
113
|
+
);
|
|
114
|
+
const lastMessage = branch.findLastIndex(
|
|
115
|
+
(entry) =>
|
|
116
|
+
entry.type === "custom_message" ||
|
|
117
|
+
(entry.type === "message" && entry.message.role !== "system"),
|
|
118
|
+
);
|
|
119
|
+
// Custom kickoffs have no user message. Scope both receipts and tool outcomes
|
|
120
|
+
// to their native preparation boundary, not an earlier custom-only run.
|
|
121
|
+
// /tree returns to a prompt's parent, which can include its preparation marker
|
|
122
|
+
// without the prompt. Only a delivered message starts a new historical run.
|
|
123
|
+
const runStart = branch.findLastIndex(
|
|
124
|
+
(entry, index) =>
|
|
125
|
+
entry.type === "custom" &&
|
|
126
|
+
entry.customType === NOTES_RUN_ENTRY &&
|
|
127
|
+
entry.data &&
|
|
128
|
+
typeof entry.data === "object" &&
|
|
129
|
+
"runId" in entry.data &&
|
|
130
|
+
(runId ? entry.data.runId === runId : index < lastMessage),
|
|
131
|
+
);
|
|
132
|
+
const run = branch.slice(Math.max(lastUser, runStart) + 1);
|
|
133
|
+
const receipts = run
|
|
134
|
+
.filter(
|
|
135
|
+
(entry) => entry.type === "custom" && entry.customType === NOTE_RECEIPT,
|
|
136
|
+
)
|
|
137
|
+
.flatMap((entry) =>
|
|
138
|
+
entry.type === "custom" ? [noteReceipt(entry.data)] : [],
|
|
139
|
+
)
|
|
140
|
+
.filter((receipt): receipt is NoteReceipt =>
|
|
141
|
+
Boolean(
|
|
142
|
+
receipt &&
|
|
143
|
+
receipt.origin !== "routed" &&
|
|
144
|
+
receipt.windowId === windowId &&
|
|
145
|
+
(!runId || receipt.runId === runId),
|
|
146
|
+
),
|
|
147
|
+
);
|
|
148
|
+
const messages = buildSessionProjection(run).messages;
|
|
149
|
+
const outcomes = run.flatMap((entry) => {
|
|
150
|
+
if (entry.type !== "custom" || entry.customType !== NESTED_NOTE_OUTCOME)
|
|
151
|
+
return [];
|
|
152
|
+
const value = entry.data as Partial<NestedNoteOutcome> | undefined;
|
|
153
|
+
return value?.protocol === 1 &&
|
|
154
|
+
value.windowId === windowId &&
|
|
155
|
+
(!runId || value.runId === runId) &&
|
|
156
|
+
typeof value.callId === "string" &&
|
|
157
|
+
typeof value.parentCallId === "string" &&
|
|
158
|
+
(value.origin === undefined || value.origin === "routed") &&
|
|
159
|
+
typeof value.pending === "boolean" &&
|
|
160
|
+
(!value.pending || value.saved === false) &&
|
|
161
|
+
typeof value.saved === "boolean"
|
|
162
|
+
? [value as NestedNoteOutcome]
|
|
163
|
+
: [];
|
|
164
|
+
});
|
|
165
|
+
// Starts remain failed proof until their matching completion replaces them.
|
|
166
|
+
const latest = new Map<string, NestedNoteOutcome>();
|
|
167
|
+
for (const outcome of outcomes) latest.set(outcome.callId, outcome);
|
|
168
|
+
return { messages, receipts, outcomes: [...latest.values()] };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function completedWrites(
|
|
172
|
+
calls: readonly ToolCall[],
|
|
173
|
+
results: readonly ToolResultMessage[],
|
|
174
|
+
receipts: readonly NoteReceipt[],
|
|
175
|
+
outcomes: readonly NestedNoteOutcome[],
|
|
176
|
+
allResults: readonly ToolResultMessage[],
|
|
177
|
+
): boolean {
|
|
178
|
+
if (!receipts.length) return false;
|
|
179
|
+
const writes = calls.filter(
|
|
180
|
+
(call) => call.name === "notes" && isNoteWrite(call.arguments),
|
|
181
|
+
);
|
|
182
|
+
let completedWrite = false;
|
|
183
|
+
for (const call of writes) {
|
|
184
|
+
const result = results.find(
|
|
185
|
+
(result) =>
|
|
186
|
+
result.toolCallId === call.id && result.toolName === call.name,
|
|
187
|
+
);
|
|
188
|
+
if (
|
|
189
|
+
result?.details &&
|
|
190
|
+
typeof result.details === "object" &&
|
|
191
|
+
"notesCompactionRouted" in result.details &&
|
|
192
|
+
result.details["notesCompactionRouted"] === true
|
|
193
|
+
)
|
|
194
|
+
continue;
|
|
195
|
+
if (
|
|
196
|
+
!result ||
|
|
197
|
+
result.isError ||
|
|
198
|
+
!result.details ||
|
|
199
|
+
typeof result.details !== "object" ||
|
|
200
|
+
!("notesCompaction" in result.details)
|
|
201
|
+
)
|
|
202
|
+
return false;
|
|
203
|
+
const details = result.details["notesCompaction"];
|
|
204
|
+
if (
|
|
205
|
+
!details ||
|
|
206
|
+
typeof details !== "object" ||
|
|
207
|
+
!("protocol" in details) ||
|
|
208
|
+
details["protocol"] !== 1 ||
|
|
209
|
+
!("saved" in details) ||
|
|
210
|
+
details["saved"] !== true ||
|
|
211
|
+
!("runId" in details) ||
|
|
212
|
+
!receipts.some((receipt) => receipt?.runId === details["runId"])
|
|
213
|
+
)
|
|
214
|
+
return false;
|
|
215
|
+
completedWrite = true;
|
|
216
|
+
}
|
|
217
|
+
for (const result of results) {
|
|
218
|
+
const cell = codeCell(result);
|
|
219
|
+
const terminal = cell
|
|
220
|
+
? allResults.findLast(
|
|
221
|
+
(item) =>
|
|
222
|
+
codeCell(item)?.id === cell.id &&
|
|
223
|
+
codeCell(item)?.status !== "yielded",
|
|
224
|
+
)
|
|
225
|
+
: undefined;
|
|
226
|
+
const parents = new Set(
|
|
227
|
+
allResults
|
|
228
|
+
.filter((item) => cell && codeCell(item)?.id === cell.id)
|
|
229
|
+
.map((item) => item.toolCallId),
|
|
230
|
+
);
|
|
231
|
+
parents.add(result.toolCallId);
|
|
232
|
+
const completed = outcomes.filter((item) => parents.has(item.parentCallId));
|
|
233
|
+
const local = completed.filter((item) => item.origin !== "routed");
|
|
234
|
+
if (local.length) {
|
|
235
|
+
if (local.some((item) => !item.saved)) return false;
|
|
236
|
+
if (
|
|
237
|
+
result.isError ||
|
|
238
|
+
(cell &&
|
|
239
|
+
(!terminal ||
|
|
240
|
+
terminal.isError ||
|
|
241
|
+
codeCell(terminal)?.status !== "result" ||
|
|
242
|
+
codeCell(terminal)?.failed))
|
|
243
|
+
)
|
|
244
|
+
return false;
|
|
245
|
+
completedWrite = true;
|
|
246
|
+
}
|
|
247
|
+
// Older saved PCC runs used a note-specific completion field. Read it,
|
|
248
|
+
// but new executions rely on Notes-owned outcomes and public Pi events.
|
|
249
|
+
const legacy =
|
|
250
|
+
result.details &&
|
|
251
|
+
typeof result.details === "object" &&
|
|
252
|
+
"contextNotesSaved" in result.details
|
|
253
|
+
? result.details["contextNotesSaved"]
|
|
254
|
+
: undefined;
|
|
255
|
+
if (!completed.length && legacy !== undefined) {
|
|
256
|
+
if (
|
|
257
|
+
legacy === false ||
|
|
258
|
+
result.isError ||
|
|
259
|
+
(cell &&
|
|
260
|
+
(!terminal ||
|
|
261
|
+
codeCell(terminal)?.status !== "result" ||
|
|
262
|
+
codeCell(terminal)?.failed))
|
|
263
|
+
)
|
|
264
|
+
return false;
|
|
265
|
+
if (legacy === true) completedWrite = true;
|
|
266
|
+
}
|
|
267
|
+
const nested = result.nestedCalls;
|
|
268
|
+
const noteCalls =
|
|
269
|
+
nested?.calls.filter((call) => call.name === "notes") ?? [];
|
|
270
|
+
const uncovered =
|
|
271
|
+
legacy === true && !completed.length
|
|
272
|
+
? []
|
|
273
|
+
: noteCalls.filter(
|
|
274
|
+
(call) => !outcomes.some((item) => item.callId === call.id),
|
|
275
|
+
);
|
|
276
|
+
const untrackedWrites = uncovered.filter(
|
|
277
|
+
(call) => !call.arguments || isNoteWrite(call.arguments),
|
|
278
|
+
);
|
|
279
|
+
// Pi also marks argument omission as incomplete. Notes' start/end ledger
|
|
280
|
+
// proves write completion even when that bounded record loses arguments or calls.
|
|
281
|
+
const tracked =
|
|
282
|
+
completed.length > 0 && completed.every((item) => item.pending === false);
|
|
283
|
+
if (
|
|
284
|
+
untrackedWrites.some((call) => call.status !== "ok") ||
|
|
285
|
+
(nested?.complete === false &&
|
|
286
|
+
(noteCalls.length > 0 || completed.length > 0) &&
|
|
287
|
+
!(legacy === true && !completed.length) &&
|
|
288
|
+
(!tracked || untrackedWrites.length > 0))
|
|
289
|
+
)
|
|
290
|
+
return false;
|
|
291
|
+
const nestedWrites = untrackedWrites.filter((call) =>
|
|
292
|
+
isNoteWrite(call.arguments),
|
|
293
|
+
);
|
|
294
|
+
if (
|
|
295
|
+
nestedWrites.some((call) => call.status !== "ok") ||
|
|
296
|
+
(nestedWrites.length && (result.isError || !nested?.complete))
|
|
297
|
+
)
|
|
298
|
+
return false;
|
|
299
|
+
if (nestedWrites.length) completedWrite = true;
|
|
300
|
+
}
|
|
301
|
+
return completedWrite;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function codeCell(
|
|
305
|
+
result: ToolResultMessage,
|
|
306
|
+
): { id: string; status: unknown; failed: boolean } | undefined {
|
|
307
|
+
const details = result.details;
|
|
308
|
+
if (
|
|
309
|
+
!details ||
|
|
310
|
+
typeof details !== "object" ||
|
|
311
|
+
!("codeMode" in details) ||
|
|
312
|
+
details["codeMode"] !== true ||
|
|
313
|
+
!("cellId" in details) ||
|
|
314
|
+
typeof details["cellId"] !== "string" ||
|
|
315
|
+
!("status" in details)
|
|
316
|
+
)
|
|
317
|
+
return;
|
|
318
|
+
return {
|
|
319
|
+
id: details["cellId"],
|
|
320
|
+
status: details["status"],
|
|
321
|
+
failed: "scriptError" in details && Boolean(details["scriptError"]),
|
|
322
|
+
};
|
|
323
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface SearchItem {
|
|
2
|
+
item_id: string;
|
|
3
|
+
content: string;
|
|
4
|
+
}
|
|
5
|
+
export type SearchRequest =
|
|
6
|
+
| {
|
|
7
|
+
action: "index";
|
|
8
|
+
sessionId: string;
|
|
9
|
+
file: string | undefined;
|
|
10
|
+
items: readonly SearchItem[];
|
|
11
|
+
removeIds: string[];
|
|
12
|
+
}
|
|
13
|
+
| {
|
|
14
|
+
action: "search";
|
|
15
|
+
sessionId: string;
|
|
16
|
+
ids: string[];
|
|
17
|
+
match: string;
|
|
18
|
+
limit: number;
|
|
19
|
+
recentFirst: boolean;
|
|
20
|
+
}
|
|
21
|
+
| {
|
|
22
|
+
action: "prune_session";
|
|
23
|
+
sessionId: string;
|
|
24
|
+
file: string;
|
|
25
|
+
}
|
|
26
|
+
| {
|
|
27
|
+
action: "prune";
|
|
28
|
+
activeSessionId: string;
|
|
29
|
+
sources: Record<string, string | null>;
|
|
30
|
+
};
|
|
31
|
+
export interface PruneResult {
|
|
32
|
+
removed: number;
|
|
33
|
+
unknownPaths: number;
|
|
34
|
+
inaccessible: number;
|
|
35
|
+
storageBusy: boolean;
|
|
36
|
+
}
|
|
37
|
+
export type SearchResponse =
|
|
38
|
+
| { id: number; value: string[] | PruneResult | null }
|
|
39
|
+
| { id: number; error: string };
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { open } from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
/** @typedef {{file:string; sessionId:string; generation:string; excluded:Set<string>}} PrunedHistory */
|
|
4
|
+
|
|
5
|
+
/** The live SessionManager still contains old text until restart. Read the committed
|
|
6
|
+
* source so later indexing, settings toggles and /reload cannot resurrect its terms.
|
|
7
|
+
*/
|
|
8
|
+
export function createHistoryPruning() {
|
|
9
|
+
/** @type {PrunedHistory | undefined} */
|
|
10
|
+
let cached;
|
|
11
|
+
const empty = new Set();
|
|
12
|
+
/** @param {import("./sqlite.mjs").Database} db
|
|
13
|
+
* @param {string} sessionId
|
|
14
|
+
* @param {string | undefined} file
|
|
15
|
+
* @returns {Promise<ReadonlySet<string>>}
|
|
16
|
+
*/
|
|
17
|
+
return async (db, sessionId, file) => {
|
|
18
|
+
if (!file) return empty;
|
|
19
|
+
let input;
|
|
20
|
+
try {
|
|
21
|
+
input = await open(file, "r");
|
|
22
|
+
} catch (error) {
|
|
23
|
+
// A new Pi session may not have flushed its first message yet.
|
|
24
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
25
|
+
return empty;
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
let stream;
|
|
29
|
+
try {
|
|
30
|
+
const { size } = await input.stat();
|
|
31
|
+
if (!size) return empty;
|
|
32
|
+
stream = input.createReadStream({
|
|
33
|
+
start: 0,
|
|
34
|
+
end: size - 1,
|
|
35
|
+
autoClose: false,
|
|
36
|
+
encoding: "utf8",
|
|
37
|
+
});
|
|
38
|
+
/** @type {PrunedHistory | undefined} */
|
|
39
|
+
let source;
|
|
40
|
+
let pending = "";
|
|
41
|
+
for await (const chunk of stream) {
|
|
42
|
+
pending += chunk;
|
|
43
|
+
let start = 0;
|
|
44
|
+
let end;
|
|
45
|
+
while ((end = pending.indexOf("\n", start)) >= 0) {
|
|
46
|
+
const entry = JSON.parse(pending.slice(start, end));
|
|
47
|
+
start = end + 1;
|
|
48
|
+
if (!source) {
|
|
49
|
+
if (entry.type !== "session" || entry.id !== sessionId)
|
|
50
|
+
throw new Error("History source identity changed");
|
|
51
|
+
const marker = entry.notesPruning;
|
|
52
|
+
if (marker === undefined) return empty;
|
|
53
|
+
if (
|
|
54
|
+
marker?.version !== 1 ||
|
|
55
|
+
marker.sessionId !== sessionId ||
|
|
56
|
+
typeof marker.generation !== "string"
|
|
57
|
+
)
|
|
58
|
+
throw new Error("Invalid history pruning marker");
|
|
59
|
+
if (
|
|
60
|
+
cached?.file === file &&
|
|
61
|
+
cached.sessionId === sessionId &&
|
|
62
|
+
cached.generation === marker.generation
|
|
63
|
+
)
|
|
64
|
+
return cached.excluded;
|
|
65
|
+
source = {
|
|
66
|
+
file,
|
|
67
|
+
sessionId,
|
|
68
|
+
generation: marker.generation,
|
|
69
|
+
excluded: new Set(),
|
|
70
|
+
};
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const message = entry.type === "message" ? entry.message : undefined;
|
|
74
|
+
if (
|
|
75
|
+
(entry.type === "custom" &&
|
|
76
|
+
entry.customType === "notes-compaction:pruned:v1") ||
|
|
77
|
+
(["user", "assistant"].includes(message?.role) &&
|
|
78
|
+
!visibleText(message.content))
|
|
79
|
+
)
|
|
80
|
+
source.excluded.add(entry.id);
|
|
81
|
+
}
|
|
82
|
+
pending = pending.slice(start);
|
|
83
|
+
}
|
|
84
|
+
// Only complete lines from the opened inode count. A concurrent append's
|
|
85
|
+
// incomplete suffix is new history, never an old pruned conversation.
|
|
86
|
+
if (!source) throw new Error("History source header is missing");
|
|
87
|
+
const excluded = source.excluded;
|
|
88
|
+
const removeTerms = db.prepare(
|
|
89
|
+
"DELETE FROM history_terms WHERE rowid = ?",
|
|
90
|
+
);
|
|
91
|
+
const removeDocument = db.prepare(
|
|
92
|
+
"DELETE FROM history_documents WHERE id = ?",
|
|
93
|
+
);
|
|
94
|
+
/** @type {import("./sqlite.mjs").Statement<[string], {id:number; item_id:string}>} */
|
|
95
|
+
const documents = db.prepare(
|
|
96
|
+
"SELECT id, item_id FROM history_documents WHERE session_id = ?",
|
|
97
|
+
);
|
|
98
|
+
db.transaction(() => {
|
|
99
|
+
for (const row of documents.all(sessionId)) {
|
|
100
|
+
if (!excluded.has(row.item_id)) continue;
|
|
101
|
+
removeTerms.run(row.id);
|
|
102
|
+
removeDocument.run(row.id);
|
|
103
|
+
}
|
|
104
|
+
}).immediate();
|
|
105
|
+
cached = source;
|
|
106
|
+
return excluded;
|
|
107
|
+
} finally {
|
|
108
|
+
stream?.destroy();
|
|
109
|
+
await input.close();
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @param {unknown} content */
|
|
115
|
+
function visibleText(content) {
|
|
116
|
+
return typeof content === "string"
|
|
117
|
+
? content.length > 0
|
|
118
|
+
: Array.isArray(content) &&
|
|
119
|
+
content.some((part) => part?.type === "text" && part.text?.length > 0);
|
|
120
|
+
}
|