@howaboua/pi-notes-compaction 0.0.9 → 0.0.11
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 +28 -5
- package/PCC-INTEGRATION.md +7 -98
- package/README.md +21 -13
- package/package.json +7 -3
- package/src/activation.ts +152 -0
- package/src/agent-identity.ts +119 -0
- package/src/attachment-snapshot.ts +103 -0
- package/src/binding-startup.ts +30 -0
- package/src/bridge.ts +1 -43
- package/src/compaction-projection.ts +88 -0
- package/src/context-owner.ts +103 -0
- package/src/context-sharing.ts +144 -0
- package/src/extension-hooks.ts +198 -0
- package/src/extension-sharing.ts +118 -0
- package/src/fresh-checkpoint.ts +117 -0
- package/src/fresh-notes.ts +141 -26
- package/src/history-search-protocol.ts +39 -0
- package/src/history-search-pruning.mjs +120 -0
- package/src/history-search-worker.mjs +258 -0
- package/src/history-search.ts +269 -0
- package/src/history.ts +212 -40
- package/src/idle-admission.ts +201 -0
- package/src/index.ts +71 -401
- package/src/integrations.ts +159 -0
- package/src/lifecycle.ts +63 -448
- package/src/note-save-marker.ts +112 -0
- package/src/notes-lookup.ts +112 -0
- package/src/notes.ts +24 -28
- package/src/query-executor.ts +190 -0
- package/src/remote.ts +357 -0
- package/src/rollover.ts +212 -0
- package/src/saved-session.ts +44 -7
- package/src/session-pruning-content.ts +237 -0
- package/src/session-pruning-plan.ts +243 -0
- package/src/session-pruning-policy.ts +20 -0
- package/src/session-pruning-screen.ts +204 -0
- package/src/session-pruning-source.ts +44 -0
- package/src/session-pruning-transaction.ts +141 -0
- package/src/session-pruning.ts +155 -0
- package/src/settings-command.ts +77 -0
- package/src/settings-screen.ts +84 -19
- package/src/settings.ts +143 -11
- package/src/shared-notes.ts +37 -14
- package/src/sharing-service.ts +348 -0
- package/src/sqlite.mjs +33 -0
- package/src/store.ts +46 -13
- package/src/task-boundary.ts +6 -1
- package/src/tool-contracts.ts +279 -0
- package/src/tools.ts +19 -560
- package/src/tree-archive.ts +149 -0
- package/src/tree-checkpoint.ts +170 -0
- package/src/tree-handoff-read.ts +192 -0
- package/src/window-checkpoint.ts +105 -0
- package/src/windows.ts +25 -9
package/src/index.ts
CHANGED
|
@@ -1,435 +1,105 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
2
|
import {
|
|
3
3
|
type ExtensionAPI,
|
|
4
|
-
type ExtensionContext,
|
|
5
4
|
getAgentDir,
|
|
6
5
|
} from "@earendil-works/pi-coding-agent";
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from "./bridge.js";
|
|
6
|
+
import { createNotesActivation } from "./activation.js";
|
|
7
|
+
import { createNotesContextOwner } from "./context-owner.js";
|
|
8
|
+
import { registerNotesHooks } from "./extension-hooks.js";
|
|
9
|
+
import { registerNotesSharing } from "./extension-sharing.js";
|
|
10
|
+
import { HistorySearch } from "./history-search.js";
|
|
11
|
+
import { registerNotesToolIntegrations } from "./integrations.js";
|
|
14
12
|
import { NotesLifecycle } from "./lifecycle.js";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
type NotesSettings,
|
|
19
|
-
readSettings,
|
|
20
|
-
writeSettings,
|
|
21
|
-
} from "./settings.js";
|
|
22
|
-
import { openNotesSettings } from "./settings-screen.js";
|
|
13
|
+
import { registerNoteSaveMarkers } from "./note-save-marker.js";
|
|
14
|
+
import { SessionPruning } from "./session-pruning.js";
|
|
15
|
+
import { registerNotesSettingsCommand } from "./settings-command.js";
|
|
23
16
|
import { NotesStore } from "./store.js";
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
HISTORY_USAGE,
|
|
28
|
-
NOTES_USAGE,
|
|
29
|
-
} from "./tools.js";
|
|
30
|
-
import {
|
|
31
|
-
registerWindowRenderer,
|
|
32
|
-
WINDOW_MESSAGE,
|
|
33
|
-
windowDetails,
|
|
34
|
-
} from "./windows.js";
|
|
17
|
+
import { createTools } from "./tools.js";
|
|
18
|
+
import { WindowCheckpoints } from "./window-checkpoint.js";
|
|
19
|
+
import { registerWindowRenderer } from "./windows.js";
|
|
35
20
|
|
|
36
21
|
export default async function notesCompaction(pi: ExtensionAPI): Promise<void> {
|
|
37
22
|
registerWindowRenderer(pi);
|
|
38
23
|
const dir = join(getAgentDir(), "notes-compaction");
|
|
39
24
|
const settingsPath = join(dir, "settings.json");
|
|
40
25
|
const store = new NotesStore(join(dir, "notes.sqlite"));
|
|
26
|
+
const search = new HistorySearch(join(dir, "history-search.sqlite"));
|
|
41
27
|
const lifecycle = new NotesLifecycle(pi, store);
|
|
42
|
-
|
|
43
|
-
|
|
28
|
+
const checkpoints = new WindowCheckpoints((entries) =>
|
|
29
|
+
lifecycle.windows.projectBranch(entries),
|
|
30
|
+
);
|
|
31
|
+
let refreshNested = () => {};
|
|
32
|
+
const activation = createNotesActivation(
|
|
33
|
+
pi,
|
|
34
|
+
store,
|
|
35
|
+
search,
|
|
36
|
+
lifecycle,
|
|
37
|
+
settingsPath,
|
|
38
|
+
() => refreshNested(),
|
|
39
|
+
);
|
|
44
40
|
const stopAdmission = pi.events.on(
|
|
45
41
|
"pi-notes-compaction:custom-turn-admission:v1",
|
|
46
42
|
(value) => {
|
|
47
43
|
const request = value as {
|
|
48
44
|
protocol?: number;
|
|
49
45
|
ctx: Parameters<NotesLifecycle["prepareCustomTurn"]>[0];
|
|
50
|
-
preparation?: Promise<void
|
|
46
|
+
preparation?: Promise<void>;
|
|
51
47
|
};
|
|
52
48
|
if (request?.protocol === 1 && !request.preparation) {
|
|
53
|
-
refreshActivation(request.ctx);
|
|
54
|
-
request.preparation =
|
|
49
|
+
activation.refreshActivation(request.ctx);
|
|
50
|
+
request.preparation = (async () => {
|
|
51
|
+
if (activation.available && request.ctx.isIdle())
|
|
52
|
+
await checkpoints.restore(request.ctx);
|
|
53
|
+
await lifecycle.prepareCustomTurn(request.ctx);
|
|
54
|
+
})();
|
|
55
55
|
}
|
|
56
56
|
},
|
|
57
57
|
);
|
|
58
|
-
const tools = createTools(pi, store, lifecycle);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
lifecycle
|
|
67
|
-
} catch (error) {
|
|
68
|
-
if (
|
|
69
|
-
!(error instanceof Error) ||
|
|
70
|
-
!("code" in error) ||
|
|
71
|
-
![
|
|
72
|
-
"ERR_MODULE_NOT_FOUND",
|
|
73
|
-
"MODULE_NOT_FOUND",
|
|
74
|
-
"ERR_PACKAGE_PATH_NOT_EXPORTED",
|
|
75
|
-
].includes(String(error.code)) ||
|
|
76
|
-
!error.message.includes("@howaboua/pi-codex-conversion")
|
|
77
|
-
)
|
|
78
|
-
throw error;
|
|
79
|
-
}
|
|
80
|
-
let ownSource: string | undefined;
|
|
81
|
-
let unavailable: string | undefined;
|
|
82
|
-
const owner: NotesOwner = {
|
|
83
|
-
protocol: 1,
|
|
84
|
-
owner: "pi-notes-compaction",
|
|
85
|
-
identity: () =>
|
|
86
|
-
lifecycle.windows.current ? { ...lifecycle.windows.current } : undefined,
|
|
87
|
-
projectMessages: (messages, entries) => {
|
|
88
|
-
if (entries) lifecycle.windows.restore(entries);
|
|
89
|
-
const projected = lifecycle.windows.projectMessages(
|
|
90
|
-
messages,
|
|
91
|
-
lifecycle.bridge?.prepareReminder,
|
|
92
|
-
);
|
|
93
|
-
return lifecycle.active
|
|
94
|
-
? projected
|
|
95
|
-
: projected.filter(
|
|
96
|
-
(message) =>
|
|
97
|
-
message.role !== "custom" ||
|
|
98
|
-
!message.customType.startsWith("notes-compaction:"),
|
|
99
|
-
);
|
|
100
|
-
},
|
|
101
|
-
projectBranch: (entries) => lifecycle.windows.projectBranch(entries),
|
|
102
|
-
allowsCompaction: (reason) => lifecycle.allowsCompaction(reason),
|
|
103
|
-
allowsWindowRequests: () => lifecycle.newWindowRequests !== "off",
|
|
104
|
-
canBind: (ctx, entries = ctx.sessionManager.getEntries()) =>
|
|
105
|
-
lifecycle.active &&
|
|
106
|
-
ctx.isIdle() &&
|
|
107
|
-
entries.every((entry) => {
|
|
108
|
-
// The first startup after an update records a UI-only changelog.
|
|
109
|
-
if (
|
|
110
|
-
entry.type === "custom" &&
|
|
111
|
-
entry.customType === "@howaboua/pi-stuff/changelog"
|
|
112
|
-
)
|
|
113
|
-
return true;
|
|
114
|
-
// PCC links the fresh worker before binding its shared notes family.
|
|
115
|
-
if (
|
|
116
|
-
entry.type === "custom" &&
|
|
117
|
-
entry.customType === "codex-agent-relationship"
|
|
118
|
-
)
|
|
119
|
-
return (
|
|
120
|
-
(entry.data as { threadId?: unknown } | undefined)?.threadId ===
|
|
121
|
-
ctx.sessionManager.getSessionId()
|
|
122
|
-
);
|
|
123
|
-
// Herdr activation persists its empty registry before first task delivery.
|
|
124
|
-
if (
|
|
125
|
-
entry.type === "custom" &&
|
|
126
|
-
entry.customType === "herdr-agents-monitor-state"
|
|
127
|
-
) {
|
|
128
|
-
const data = entry.data as
|
|
129
|
-
| { version?: unknown; agents?: unknown }
|
|
130
|
-
| undefined;
|
|
131
|
-
return Boolean(
|
|
132
|
-
data?.version === 2 &&
|
|
133
|
-
Array.isArray(data.agents) &&
|
|
134
|
-
data.agents.length === 0,
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
// Board adoption precedes context binding on a fresh spawn. These
|
|
138
|
-
// metadata records are preparation, not evidence of a first turn.
|
|
139
|
-
if (
|
|
140
|
-
entry.type === "custom" &&
|
|
141
|
-
[
|
|
142
|
-
"shepherdr-board-binding",
|
|
143
|
-
"shepherdr-board-awareness",
|
|
144
|
-
"codex-context-briefing",
|
|
145
|
-
].includes(entry.customType)
|
|
146
|
-
)
|
|
147
|
-
return true;
|
|
148
|
-
if (
|
|
149
|
-
["model_change", "thinking_level_change", "session_info"].includes(
|
|
150
|
-
entry.type,
|
|
151
|
-
)
|
|
152
|
-
)
|
|
153
|
-
return true;
|
|
154
|
-
if (
|
|
155
|
-
entry.type !== "custom_message" ||
|
|
156
|
-
entry.customType !== WINDOW_MESSAGE
|
|
157
|
-
)
|
|
158
|
-
return false;
|
|
159
|
-
const details = windowDetails(entry);
|
|
160
|
-
return Boolean(
|
|
161
|
-
details &&
|
|
162
|
-
details.identity.windowNumber === 0 &&
|
|
163
|
-
!details.trim &&
|
|
164
|
-
(details.kind === "window" || details.kind === "identity"),
|
|
165
|
-
);
|
|
166
|
-
}),
|
|
167
|
-
snapshotNotes: exportNotesSnapshot,
|
|
168
|
-
executeShared: createSharedExecutor(pi, store, lifecycle),
|
|
169
|
-
};
|
|
170
|
-
const stopOwner = pi.events.on(OWNER_CHANNEL, (data) => {
|
|
171
|
-
if (!data || typeof data !== "object") return;
|
|
172
|
-
const request = data as Partial<OwnerRequest>;
|
|
173
|
-
if (request.protocol === 1 && ready && !unavailable)
|
|
174
|
-
request.projection?.(owner);
|
|
175
|
-
if (
|
|
176
|
-
request.protocol === 1 &&
|
|
177
|
-
typeof request.reply === "function" &&
|
|
178
|
-
ready &&
|
|
179
|
-
!unavailable &&
|
|
180
|
-
(request.ctx
|
|
181
|
-
? modelIsActive(settings, request.ctx.model)
|
|
182
|
-
: lifecycle.active)
|
|
183
|
-
)
|
|
184
|
-
request.reply(owner);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
function refreshActivation(ctx: ExtensionContext): void {
|
|
188
|
-
if (!ready || unavailable) return;
|
|
189
|
-
const active = modelIsActive(settings, ctx.model);
|
|
190
|
-
if (!active) {
|
|
191
|
-
if (lifecycle.active) lifecycle.reset(ctx);
|
|
192
|
-
lifecycle.active = false;
|
|
193
|
-
if (lifecycle.bridge) {
|
|
194
|
-
// PCC rediscovers ownership before deciding its native policy.
|
|
195
|
-
lifecycle.bridge.configureTools?.(tools, ctx, {
|
|
196
|
-
notes: NOTES_USAGE,
|
|
197
|
-
history: HISTORY_USAGE,
|
|
198
|
-
});
|
|
199
|
-
} else {
|
|
200
|
-
pi.setActiveTools(
|
|
201
|
-
pi
|
|
202
|
-
.getActiveTools()
|
|
203
|
-
.filter((name) => !TOOL_NAMES.some((tool) => tool === name)),
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
if (lifecycle.active) return;
|
|
209
|
-
try {
|
|
210
|
-
const claim = lifecycle.bridge?.claim(owner);
|
|
211
|
-
if (claim && !claim.claimed) throw new Error(claim.reason);
|
|
212
|
-
if (claim?.claimed) {
|
|
213
|
-
claim.registerTools(tools, ctx);
|
|
214
|
-
} else {
|
|
215
|
-
const conflicts = pi
|
|
216
|
-
.getAllTools()
|
|
217
|
-
.filter(
|
|
218
|
-
(tool) =>
|
|
219
|
-
TOOL_NAMES.some((name) => name === tool.name) &&
|
|
220
|
-
tool.sourceInfo.path !== ownSource,
|
|
221
|
-
);
|
|
222
|
-
if (conflicts.length)
|
|
223
|
-
throw new Error(
|
|
224
|
-
"Another extension already owns notes continuity. Installed PCC needs the documented takeover bridge before these extensions can run together.",
|
|
225
|
-
);
|
|
226
|
-
for (const tool of tools) pi.registerTool(tool);
|
|
227
|
-
ownSource = pi.getAllTools().find((tool) => tool.name === "notes")
|
|
228
|
-
?.sourceInfo.path;
|
|
229
|
-
}
|
|
230
|
-
lifecycle.active = true;
|
|
231
|
-
configureTools(ctx);
|
|
232
|
-
store.updateFile(sessionRef(ctx));
|
|
233
|
-
} catch (error) {
|
|
234
|
-
lifecycle.active = false;
|
|
235
|
-
unavailable = error instanceof Error ? error.message : String(error);
|
|
236
|
-
ctx.ui.notify(`Notes continuity inactive: ${unavailable}`, "warning");
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
function configureTools(ctx: ExtensionContext): void {
|
|
240
|
-
if (lifecycle.bridge) {
|
|
241
|
-
lifecycle.bridge.configureTools?.(tools, ctx, {
|
|
242
|
-
notes: NOTES_USAGE,
|
|
243
|
-
history: HISTORY_USAGE,
|
|
244
|
-
});
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
const names = TOOL_NAMES.filter(
|
|
248
|
-
(name) => name !== "new_context" || lifecycle.newWindowRequests !== "off",
|
|
249
|
-
);
|
|
250
|
-
pi.setActiveTools([
|
|
251
|
-
...new Set([
|
|
252
|
-
...pi
|
|
253
|
-
.getActiveTools()
|
|
254
|
-
.filter((name) => !TOOL_NAMES.some((tool) => tool === name)),
|
|
255
|
-
...names,
|
|
256
|
-
]),
|
|
257
|
-
]);
|
|
258
|
-
}
|
|
259
|
-
function applySettings(value: NotesSettings): void {
|
|
260
|
-
settings = value;
|
|
261
|
-
lifecycle.normalCompaction = value.normalCompaction;
|
|
262
|
-
lifecycle.newWindowRequests = value.newWindowRequests;
|
|
263
|
-
lifecycle.idleMinutes = value.idleMinutes;
|
|
264
|
-
lifecycle.taskBoundaryReminder = value.taskBoundaryReminder;
|
|
265
|
-
}
|
|
266
|
-
pi.on("session_start", (_event, ctx) => {
|
|
267
|
-
lifecycle.reset(ctx);
|
|
268
|
-
lifecycle.active = false;
|
|
269
|
-
ready = false;
|
|
270
|
-
unavailable = undefined;
|
|
271
|
-
try {
|
|
272
|
-
settings = readSettings(settingsPath);
|
|
273
|
-
writeSettings(settingsPath, settings);
|
|
274
|
-
applySettings(settings);
|
|
275
|
-
lifecycle.bridge = discoverBridge(pi);
|
|
276
|
-
ready = true;
|
|
277
|
-
// Register standalone tools even when initially hidden, for later model switches.
|
|
278
|
-
if (!lifecycle.bridge) {
|
|
279
|
-
if (
|
|
280
|
-
pi
|
|
281
|
-
.getAllTools()
|
|
282
|
-
.some(
|
|
283
|
-
(tool) =>
|
|
284
|
-
TOOL_NAMES.some((name) => name === tool.name) &&
|
|
285
|
-
tool.sourceInfo.path !== ownSource,
|
|
286
|
-
)
|
|
287
|
-
)
|
|
288
|
-
throw new Error(
|
|
289
|
-
"Another extension already owns notes continuity. Update it to a version supporting notes integration, then reload Pi.",
|
|
290
|
-
);
|
|
291
|
-
for (const tool of tools) pi.registerTool(tool);
|
|
292
|
-
ownSource = pi.getAllTools().find((tool) => tool.name === "notes")
|
|
293
|
-
?.sourceInfo.path;
|
|
294
|
-
}
|
|
295
|
-
refreshActivation(ctx);
|
|
296
|
-
} catch (error) {
|
|
297
|
-
unavailable = error instanceof Error ? error.message : String(error);
|
|
298
|
-
ctx.ui.notify(`Notes continuity inactive: ${unavailable}`, "warning");
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
pi.on("before_agent_start", (event, ctx) => {
|
|
302
|
-
refreshActivation(ctx);
|
|
303
|
-
if (lifecycle.active) return lifecycle.beforeStart(ctx, event);
|
|
304
|
-
});
|
|
305
|
-
pi.on("input", (event, ctx) => {
|
|
306
|
-
refreshActivation(ctx);
|
|
307
|
-
return lifecycle.active ? lifecycle.input(event, ctx) : undefined;
|
|
308
|
-
});
|
|
309
|
-
pi.on("agent_before_settle", (event) => {
|
|
310
|
-
if (lifecycle.active) lifecycle.outcome(event.outcome);
|
|
311
|
-
});
|
|
312
|
-
pi.on("agent_settled", async (event, ctx) => {
|
|
313
|
-
if (!lifecycle.active) return;
|
|
314
|
-
try {
|
|
315
|
-
await lifecycle.settled(event, ctx);
|
|
316
|
-
} catch (error) {
|
|
317
|
-
ctx.ui.notify(
|
|
318
|
-
`Context rollover failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
319
|
-
"error",
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
pi.on("turn_end", (event, ctx) => {
|
|
324
|
-
if (!lifecycle.active || lifecycle.newWindowRequests === "off") return;
|
|
325
|
-
const reminder = lifecycle.windows.reminder(
|
|
326
|
-
ctx,
|
|
327
|
-
event.message.role === "assistant" &&
|
|
328
|
-
!ctx.signal?.aborted &&
|
|
329
|
-
!["error", "length", "aborted"].includes(event.message.stopReason) &&
|
|
330
|
-
event.toolResults.length > 0 &&
|
|
331
|
-
pi.getActiveTools().includes("new_context")
|
|
332
|
-
? lifecycle.taskBoundaryReminder
|
|
333
|
-
: 0,
|
|
334
|
-
!lifecycle.fresh(ctx, lifecycle.runId),
|
|
335
|
-
);
|
|
336
|
-
return reminder
|
|
337
|
-
? {
|
|
338
|
-
entries: [
|
|
339
|
-
...event.entries,
|
|
340
|
-
lifecycle.bridge?.prepareReminder?.(reminder) ?? reminder,
|
|
341
|
-
],
|
|
342
|
-
}
|
|
343
|
-
: undefined;
|
|
344
|
-
});
|
|
345
|
-
pi.on("context", (event, ctx) => {
|
|
346
|
-
try {
|
|
347
|
-
return {
|
|
348
|
-
messages: owner.projectMessages(
|
|
349
|
-
event.messages,
|
|
350
|
-
ctx.sessionManager.getBranch(),
|
|
351
|
-
),
|
|
352
|
-
};
|
|
353
|
-
} catch (error) {
|
|
354
|
-
ctx.abort();
|
|
355
|
-
throw error;
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
pi.on("session_before_compact", (event, ctx) =>
|
|
359
|
-
ready && !unavailable ? lifecycle.beforeCompact(event, ctx) : undefined,
|
|
58
|
+
const tools = createTools(pi, store, lifecycle, search);
|
|
59
|
+
refreshNested = await registerNotesToolIntegrations(pi, lifecycle, tools);
|
|
60
|
+
for (const tool of tools) pi.registerTool(tool);
|
|
61
|
+
const owner = createNotesContextOwner(pi, store, lifecycle, search);
|
|
62
|
+
const { family, stopOwner } = await registerNotesSharing(
|
|
63
|
+
pi,
|
|
64
|
+
owner,
|
|
65
|
+
activation,
|
|
66
|
+
lifecycle,
|
|
360
67
|
);
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
68
|
+
const markers = registerNoteSaveMarkers(
|
|
69
|
+
pi,
|
|
70
|
+
() => lifecycle.active && activation.settings.noteSaveMarkers,
|
|
71
|
+
);
|
|
72
|
+
await registerNotesHooks(
|
|
73
|
+
pi,
|
|
74
|
+
activation,
|
|
75
|
+
lifecycle,
|
|
76
|
+
owner,
|
|
77
|
+
checkpoints,
|
|
78
|
+
markers,
|
|
79
|
+
family,
|
|
80
|
+
);
|
|
81
|
+
const sessionPruning = new SessionPruning(
|
|
82
|
+
pi,
|
|
83
|
+
store,
|
|
84
|
+
search,
|
|
85
|
+
() => lifecycle.windows.current?.currentWindowId,
|
|
86
|
+
);
|
|
87
|
+
sessionPruning.register();
|
|
379
88
|
pi.on("session_shutdown", () => {
|
|
380
89
|
lifecycle.reset();
|
|
381
90
|
lifecycle.active = false;
|
|
382
91
|
store.close();
|
|
92
|
+
search.close();
|
|
383
93
|
stopOwner();
|
|
384
94
|
stopAdmission();
|
|
385
95
|
});
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
ctx.ui.notify("Notes settings require interactive Pi.", "warning");
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
const save = (value: NotesSettings) => {
|
|
399
|
-
writeSettings(settingsPath, value);
|
|
400
|
-
applySettings(value);
|
|
401
|
-
refreshActivation(ctx);
|
|
402
|
-
if (lifecycle.active) configureTools(ctx);
|
|
403
|
-
};
|
|
404
|
-
const prune = async () => {
|
|
405
|
-
await ctx.waitForIdle();
|
|
406
|
-
store.updateFile(sessionRef(ctx));
|
|
407
|
-
const result = store.prune(ctx.sessionManager.getSessionId());
|
|
408
|
-
ctx.ui.notify(
|
|
409
|
-
`Pruned ${result.removed} sessions. Retained ${result.unknownPaths} unknown paths and ${result.inaccessible} inaccessible files.`,
|
|
410
|
-
"info",
|
|
411
|
-
);
|
|
412
|
-
};
|
|
413
|
-
try {
|
|
414
|
-
await openNotesSettings(ctx, {
|
|
415
|
-
settings: () => settings,
|
|
416
|
-
save,
|
|
417
|
-
settingsPath,
|
|
418
|
-
prune,
|
|
419
|
-
status: () => {
|
|
420
|
-
const counts = store.status();
|
|
421
|
-
return [
|
|
422
|
-
lifecycle.active
|
|
423
|
-
? "Continuity active"
|
|
424
|
-
: `Continuity inactive: ${unavailable ?? (ready ? "model not in whitelist" : "not initialized")}`,
|
|
425
|
-
`${counts.sessions} sessions · ${counts.revisions} note revisions · ${counts.cachedResponses} cached responses`,
|
|
426
|
-
`Hybrid lookup: ${lifecycle.bridge?.lookup ? "available" : "unavailable"} · Encrypted delivery: ${lifecycle.bridge?.projectResult ? "available" : "unavailable"}`,
|
|
427
|
-
];
|
|
428
|
-
},
|
|
429
|
-
});
|
|
430
|
-
} catch (error) {
|
|
431
|
-
ctx.ui.notify(`Notes settings failed: ${String(error)}`, "error");
|
|
432
|
-
}
|
|
433
|
-
},
|
|
434
|
-
});
|
|
96
|
+
registerNotesSettingsCommand(
|
|
97
|
+
pi,
|
|
98
|
+
activation,
|
|
99
|
+
lifecycle,
|
|
100
|
+
store,
|
|
101
|
+
search,
|
|
102
|
+
settingsPath,
|
|
103
|
+
sessionPruning,
|
|
104
|
+
);
|
|
435
105
|
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionAPI,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type { NotesActivation } from "./activation.js";
|
|
6
|
+
import type { NotesLifecycle } from "./lifecycle.js";
|
|
7
|
+
import {
|
|
8
|
+
GPT_NEW_CONTEXT_DESCRIPTION,
|
|
9
|
+
GPT_NOTES_DESCRIPTION,
|
|
10
|
+
HISTORY_USAGE,
|
|
11
|
+
NEW_CONTEXT_DESCRIPTION,
|
|
12
|
+
NOTES_DESCRIPTION,
|
|
13
|
+
NOTES_USAGE,
|
|
14
|
+
} from "./tool-contracts.js";
|
|
15
|
+
import type { createTools } from "./tools.js";
|
|
16
|
+
import { projectTreeCheckpointBranch } from "./tree-checkpoint.js";
|
|
17
|
+
import { projectTreeHandoffBranch } from "./tree-handoff-read.js";
|
|
18
|
+
import type { WindowCheckpoints } from "./window-checkpoint.js";
|
|
19
|
+
|
|
20
|
+
export async function registerNotesToolIntegrations(
|
|
21
|
+
pi: ExtensionAPI,
|
|
22
|
+
lifecycle: NotesLifecycle,
|
|
23
|
+
tools: ReturnType<typeof createTools>,
|
|
24
|
+
) {
|
|
25
|
+
try {
|
|
26
|
+
const { registerGptPromptMode } = await import(
|
|
27
|
+
"@howaboua/pi-codex-conversion/prompt-mode"
|
|
28
|
+
);
|
|
29
|
+
const registration = registerGptPromptMode(pi, (active) => {
|
|
30
|
+
for (const [name, normal, gpt] of [
|
|
31
|
+
["notes", NOTES_DESCRIPTION, GPT_NOTES_DESCRIPTION],
|
|
32
|
+
["new_context", NEW_CONTEXT_DESCRIPTION, GPT_NEW_CONTEXT_DESCRIPTION],
|
|
33
|
+
] as const) {
|
|
34
|
+
const tool = tools.find((tool) => tool.name === name);
|
|
35
|
+
const description = active ? gpt : normal;
|
|
36
|
+
if (tool && tool.description !== description) {
|
|
37
|
+
tool.description = description;
|
|
38
|
+
pi.registerTool(tool);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return lifecycle.active ? tools : [];
|
|
42
|
+
});
|
|
43
|
+
lifecycle.gptPromptMode = () => registration.active;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
if (
|
|
46
|
+
!(error instanceof Error) ||
|
|
47
|
+
!("code" in error) ||
|
|
48
|
+
![
|
|
49
|
+
"ERR_MODULE_NOT_FOUND",
|
|
50
|
+
"MODULE_NOT_FOUND",
|
|
51
|
+
"ERR_PACKAGE_PATH_NOT_EXPORTED",
|
|
52
|
+
].includes(String(error.code)) ||
|
|
53
|
+
!error.message.includes("@howaboua/pi-codex-conversion")
|
|
54
|
+
)
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
let refreshNested = () => {};
|
|
58
|
+
try {
|
|
59
|
+
const { adaptToolForCodeMode, registerCodeModeExtensionTools } =
|
|
60
|
+
await import("@howaboua/pi-codex-conversion/code-mode");
|
|
61
|
+
const registration = registerCodeModeExtensionTools(
|
|
62
|
+
pi,
|
|
63
|
+
() =>
|
|
64
|
+
tools
|
|
65
|
+
.filter((tool) => tool.name === "notes" || tool.name === "history")
|
|
66
|
+
.map((tool) =>
|
|
67
|
+
adaptToolForCodeMode(tool, {
|
|
68
|
+
discovery: "help",
|
|
69
|
+
usage: tool.name === "notes" ? NOTES_USAGE : HISTORY_USAGE,
|
|
70
|
+
deferLoading: true,
|
|
71
|
+
opaqueResult: tool.name === "notes",
|
|
72
|
+
resultValue(result) {
|
|
73
|
+
const [part] = result.content;
|
|
74
|
+
if (result.content.length === 1 && part?.type === "text") {
|
|
75
|
+
try {
|
|
76
|
+
return JSON.parse(part.text);
|
|
77
|
+
} catch {
|
|
78
|
+
return part.text;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { content: result.content };
|
|
82
|
+
},
|
|
83
|
+
}),
|
|
84
|
+
),
|
|
85
|
+
{ isActive: () => lifecycle.active },
|
|
86
|
+
);
|
|
87
|
+
refreshNested = registration.refresh;
|
|
88
|
+
pi.on("session_shutdown", () => registration.unregister());
|
|
89
|
+
const { withCodexDeveloperMessageDetails } = await import(
|
|
90
|
+
"@howaboua/pi-codex-conversion/developer-messages"
|
|
91
|
+
);
|
|
92
|
+
lifecycle.prepareReminder = withCodexDeveloperMessageDetails;
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (!optionalPccMissing(error)) throw error;
|
|
95
|
+
}
|
|
96
|
+
return refreshNested;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function registerNotesCompactionIntegration(
|
|
100
|
+
pi: ExtensionAPI,
|
|
101
|
+
activation: NotesActivation,
|
|
102
|
+
lifecycle: NotesLifecycle,
|
|
103
|
+
checkpoints: WindowCheckpoints,
|
|
104
|
+
) {
|
|
105
|
+
let prepareProviderWindow:
|
|
106
|
+
| ((ctx: ExtensionContext) => Promise<void>)
|
|
107
|
+
| undefined;
|
|
108
|
+
const prepareCompaction = (
|
|
109
|
+
event: import("@earendil-works/pi-coding-agent").SessionBeforeCompactEvent,
|
|
110
|
+
ctx: ExtensionContext,
|
|
111
|
+
) =>
|
|
112
|
+
activation.available
|
|
113
|
+
? (checkpoints.prepare(event, ctx) ?? lifecycle.beforeCompact(event, ctx))
|
|
114
|
+
: undefined;
|
|
115
|
+
try {
|
|
116
|
+
const {
|
|
117
|
+
registerCompactionPreparation,
|
|
118
|
+
registerContextBranchProjection,
|
|
119
|
+
prepareContextWindow,
|
|
120
|
+
} = await import("@howaboua/pi-codex-conversion/compaction");
|
|
121
|
+
prepareProviderWindow = (ctx) => prepareContextWindow(pi, ctx);
|
|
122
|
+
const registration = registerCompactionPreparation(pi, prepareCompaction);
|
|
123
|
+
const stopProjection = registerContextBranchProjection(
|
|
124
|
+
pi,
|
|
125
|
+
(entries, ctx) =>
|
|
126
|
+
activation.available
|
|
127
|
+
? projectTreeHandoffBranch(
|
|
128
|
+
lifecycle.windows.projectBranch(
|
|
129
|
+
projectTreeCheckpointBranch(
|
|
130
|
+
entries,
|
|
131
|
+
ctx.sessionManager.getEntries(),
|
|
132
|
+
),
|
|
133
|
+
),
|
|
134
|
+
)
|
|
135
|
+
: entries,
|
|
136
|
+
);
|
|
137
|
+
pi.on("session_shutdown", stopProjection);
|
|
138
|
+
pi.on("session_before_compact", registration.prepare);
|
|
139
|
+
pi.on("session_shutdown", () => registration.dispose());
|
|
140
|
+
} catch (error) {
|
|
141
|
+
if (!optionalPccMissing(error)) throw error;
|
|
142
|
+
pi.on("session_before_compact", prepareCompaction);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return prepareProviderWindow;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function optionalPccMissing(error: unknown): boolean {
|
|
149
|
+
return (
|
|
150
|
+
error instanceof Error &&
|
|
151
|
+
"code" in error &&
|
|
152
|
+
[
|
|
153
|
+
"ERR_MODULE_NOT_FOUND",
|
|
154
|
+
"MODULE_NOT_FOUND",
|
|
155
|
+
"ERR_PACKAGE_PATH_NOT_EXPORTED",
|
|
156
|
+
].includes(String(error.code)) &&
|
|
157
|
+
error.message.includes("@howaboua/pi-codex-conversion")
|
|
158
|
+
);
|
|
159
|
+
}
|