@howaboua/pi-notes-compaction 0.0.10 → 0.0.12

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/src/history.ts CHANGED
@@ -1,222 +1,6 @@
1
1
  import type { SessionEntry } from "@earendil-works/pi-coding-agent";
2
2
  import type { HistorySearch } from "./history-search.js";
3
- import { buildTreeArchiveIndex } from "./tree-archive.js";
4
- import { windowDetails } from "./windows.js";
5
-
6
- interface HistoryItem {
7
- window_id: string;
8
- item_id: string;
9
- role: string;
10
- tool_name?: string;
11
- tool_namespace?: string;
12
- content: string;
13
- }
14
-
15
- // Pi entries are immutable once persisted. Rendering is lazy so indexing never
16
- // serializes the ongoing context, and repeated lookups reuse unchanged text.
17
- const rendered = new WeakMap<SessionEntry, string>();
18
- const cachedItems = new WeakMap<SessionEntry, HistoryItem>();
19
- const indexItems = new WeakMap<
20
- HistoryItem,
21
- Pick<HistoryItem, "item_id" | "content">
22
- >();
23
- function content(entry: SessionEntry, render: () => string): string {
24
- let value = rendered.get(entry);
25
- if (value === undefined) {
26
- value = render();
27
- rendered.set(entry, value);
28
- }
29
- return value;
30
- }
31
-
32
- function customHistoryRole(
33
- entry: Extract<SessionEntry, { type: "custom_message" }>,
34
- ): string {
35
- // Persisted developer metadata is a wire contract, not a package dependency.
36
- const details = entry.details;
37
- const metadata =
38
- details &&
39
- typeof details === "object" &&
40
- "@howaboua/pi-codex-conversion/developer-message" in details
41
- ? details["@howaboua/pi-codex-conversion/developer-message"]
42
- : [
43
- "codex-developer-message",
44
- "codex-context-window",
45
- "codex-current-time-reminder",
46
- "codex-toolkit-update",
47
- "codex-notebook-status",
48
- "codex-context-briefing",
49
- ].includes(entry.customType)
50
- ? details
51
- : undefined;
52
- return metadata &&
53
- typeof metadata === "object" &&
54
- "protocol" in metadata &&
55
- metadata.protocol === 1 &&
56
- "id" in metadata &&
57
- typeof metadata.id === "string" &&
58
- metadata.id.trim() !== ""
59
- ? "developer"
60
- : "user";
61
- }
62
-
63
- /** Pi's selected JSONL branch is the archive. */
64
- async function historyWindows(
65
- entries: readonly SessionEntry[],
66
- sessionId: string,
67
- allEntries: readonly SessionEntry[],
68
- signal?: AbortSignal,
69
- ): Promise<{ windows: Map<string, HistoryItem[]>; retired: Set<string> }> {
70
- const archive = buildTreeArchiveIndex(allEntries, entries);
71
- if (archive.invalidManifest)
72
- throw new Error(
73
- "Saved history could not be read. Open an intact copy of the source session.",
74
- );
75
- if (archive.archives.some((item) => !item.entries))
76
- throw new Error(
77
- "Archived history is unavailable in this session copy. Resume the source session.",
78
- );
79
- const seen = new Set<string>();
80
- entries = [
81
- ...archive.archives.flatMap((item) => item.entries ?? []),
82
- ...entries,
83
- ].filter((entry) => {
84
- if (seen.has(entry.id)) return false;
85
- seen.add(entry.id);
86
- return true;
87
- });
88
- let windowId = `session:${sessionId}`;
89
- let activeStart = 0;
90
- const positions = new Map(
91
- entries.map((entry, position) => [entry.id, position]),
92
- );
93
- for (const [position, entry] of entries.entries()) {
94
- const boundary = windowDetails(entry);
95
- if (boundary?.kind === "window" && boundary.trim)
96
- activeStart = position + 1;
97
- if (entry.type === "compaction") {
98
- const kept = positions.get(entry.firstKeptEntryId);
99
- if (kept !== undefined && kept <= position)
100
- activeStart = Math.max(activeStart, kept);
101
- }
102
- }
103
- const retired = new Set(
104
- entries.slice(0, activeStart).map((entry) => entry.id),
105
- );
106
- const windows = new Map<string, HistoryItem[]>();
107
- windows.set(windowId, []);
108
- for (const [position, entry] of entries.entries()) {
109
- if (position % 512 === 0) {
110
- await setImmediate();
111
- signal?.throwIfAborted();
112
- }
113
- const boundary = windowDetails(entry);
114
- if (boundary?.kind === "window") {
115
- windowId = boundary.identity.currentWindowId;
116
- if (!windows.has(windowId)) windows.set(windowId, []);
117
- continue;
118
- }
119
- const cached = cachedItems.get(entry);
120
- if (cached?.window_id === windowId) {
121
- windows.get(windowId)!.push(cached);
122
- continue;
123
- }
124
- let item: HistoryItem | undefined;
125
- if (entry.type === "message") {
126
- const message = entry.message;
127
- const call =
128
- message.role === "assistant"
129
- ? message.content.find((block) => block.type === "toolCall")
130
- : undefined;
131
- const toolName =
132
- message.role === "toolResult"
133
- ? message.toolName
134
- : call?.type === "toolCall"
135
- ? call.name
136
- : undefined;
137
- const render = () =>
138
- !("content" in message)
139
- ? JSON.stringify(message)
140
- : typeof message.content === "string"
141
- ? message.content
142
- : message.content
143
- .map((block) => {
144
- if (block.type === "text") return block.text;
145
- if (block.type === "thinking") return block.thinking;
146
- if (block.type === "toolCall")
147
- return JSON.stringify({
148
- tool: block.name,
149
- arguments: block.arguments,
150
- });
151
- if (block.type === "image")
152
- return `[image ${block.mimeType}]`;
153
- return JSON.stringify(block);
154
- })
155
- .join("\n");
156
- item = {
157
- window_id: windowId,
158
- item_id: entry.id,
159
- role: message.role === "toolResult" ? "tool" : message.role,
160
- ...(toolName
161
- ? {
162
- tool_name: toolName,
163
- ...(["notes", "history"].includes(toolName)
164
- ? { tool_namespace: toolName }
165
- : {}),
166
- }
167
- : {}),
168
- get content() {
169
- return content(entry, render);
170
- },
171
- };
172
- if (message.role === "user" || message.role === "assistant") {
173
- const body = message.content;
174
- const hasText =
175
- typeof body === "string"
176
- ? body.length > 0
177
- : body.some(
178
- (block) => block.type === "text" && block.text.length > 0,
179
- );
180
- let indexedContent: string | undefined;
181
- if (hasText)
182
- indexItems.set(item, {
183
- item_id: item.item_id,
184
- get content() {
185
- return (indexedContent ??=
186
- typeof body === "string"
187
- ? body
188
- : body
189
- .filter((block) => block.type === "text")
190
- .map((block) => block.text)
191
- .join("\n"));
192
- },
193
- });
194
- }
195
- } else if (entry.type === "custom_message" && !boundary) {
196
- item = {
197
- window_id: windowId,
198
- item_id: entry.id,
199
- role: customHistoryRole(entry),
200
- get content() {
201
- return content(entry, () =>
202
- typeof entry.content === "string"
203
- ? entry.content
204
- : entry.content
205
- .map((block) =>
206
- block.type === "text" ? block.text : "[image]",
207
- )
208
- .join("\n"),
209
- );
210
- },
211
- };
212
- }
213
- if (item) {
214
- cachedItems.set(entry, item);
215
- windows.get(windowId)!.push(item);
216
- }
217
- }
218
- return { windows, retired };
219
- }
3
+ import { historyWindows } from "./history-windows.js";
220
4
 
221
5
  export async function indexHistory(
222
6
  entries: readonly SessionEntry[],
@@ -225,7 +9,7 @@ export async function indexHistory(
225
9
  allEntries: readonly SessionEntry[] = entries,
226
10
  ): Promise<void> {
227
11
  if (!search.isEnabled(sessionId)) return;
228
- const { windows, retired } = await historyWindows(
12
+ const { windows, retired, indexItems } = await historyWindows(
229
13
  entries,
230
14
  sessionId,
231
15
  allEntries,
@@ -253,7 +37,7 @@ export async function useHistory(
253
37
  liveAccess = false,
254
38
  ): Promise<Record<string, unknown>> {
255
39
  signal?.throwIfAborted();
256
- const { windows, retired } = await historyWindows(
40
+ const { windows, retired, indexItems } = await historyWindows(
257
41
  entries,
258
42
  sessionId,
259
43
  allEntries,
@@ -276,7 +60,7 @@ export async function useHistory(
276
60
  const itemId = params["item_id"];
277
61
  if (typeof windowId !== "string" || typeof itemId !== "string")
278
62
  throw new Error(
279
- `history read_item requires ${typeof windowId !== "string" ? "window_id" : "item_id"} as a string. Copy window_id and item_id from the same search_contents or list_items result`,
63
+ `history read requires ${typeof windowId !== "string" ? "window_id" : "item_id"} as a string. Copy window_id and item_id from the same search or list result`,
280
64
  );
281
65
  const item = windows.get(windowId)?.find((item) => item.item_id === itemId);
282
66
  if (!item) return { item: null };
@@ -365,5 +149,3 @@ function integer(value: unknown, fallback: number, max: number): number {
365
149
  ? Math.max(0, Math.min(value, max))
366
150
  : fallback;
367
151
  }
368
-
369
- import { setImmediate } from "node:timers/promises";
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { join } from "node:path";
2
2
  import {
3
3
  type ExtensionAPI,
4
+ type ExtensionContext,
4
5
  getAgentDir,
5
6
  } from "@earendil-works/pi-coding-agent";
6
7
  import { createNotesActivation } from "./activation.js";
@@ -10,10 +11,12 @@ import { registerNotesSharing } from "./extension-sharing.js";
10
11
  import { HistorySearch } from "./history-search.js";
11
12
  import { registerNotesToolIntegrations } from "./integrations.js";
12
13
  import { NotesLifecycle } from "./lifecycle.js";
14
+ import { createModelRoles } from "./model-roles.js";
13
15
  import { registerNoteSaveMarkers } from "./note-save-marker.js";
14
16
  import { SessionPruning } from "./session-pruning.js";
15
17
  import { registerNotesSettingsCommand } from "./settings-command.js";
16
18
  import { NotesStore } from "./store.js";
19
+ import { notesSchema } from "./tool-contracts.js";
17
20
  import { createTools } from "./tools.js";
18
21
  import { WindowCheckpoints } from "./window-checkpoint.js";
19
22
  import { registerWindowRenderer } from "./windows.js";
@@ -55,8 +58,29 @@ export default async function notesCompaction(pi: ExtensionAPI): Promise<void> {
55
58
  }
56
59
  },
57
60
  );
58
- const tools = createTools(pi, store, lifecycle, search);
59
- refreshNested = await registerNotesToolIntegrations(pi, lifecycle, tools);
61
+ const refreshRoleContract = (ctx?: ExtensionContext) => {
62
+ if (ctx) activation.refreshActivation(ctx);
63
+ const tool = tools.find((tool) => tool.name === "notes");
64
+ if (!tool) return;
65
+ const schema = notesSchema(roles.enabled());
66
+ if (JSON.stringify(tool.parameters) === JSON.stringify(schema)) return;
67
+ tool.parameters = schema;
68
+ pi.registerTool(tool);
69
+ refreshNested();
70
+ };
71
+ const roles = createModelRoles(
72
+ pi,
73
+ activation,
74
+ lifecycle,
75
+ refreshRoleContract,
76
+ );
77
+ const tools = createTools(pi, store, lifecycle, search, roles);
78
+ refreshNested = await registerNotesToolIntegrations(
79
+ pi,
80
+ lifecycle,
81
+ tools,
82
+ refreshRoleContract,
83
+ );
60
84
  for (const tool of tools) pi.registerTool(tool);
61
85
  const owner = createNotesContextOwner(pi, store, lifecycle, search);
62
86
  const { family, stopOwner } = await registerNotesSharing(
@@ -101,5 +125,6 @@ export default async function notesCompaction(pi: ExtensionAPI): Promise<void> {
101
125
  search,
102
126
  settingsPath,
103
127
  sessionPruning,
128
+ roles,
104
129
  );
105
130
  }
@@ -21,12 +21,14 @@ export async function registerNotesToolIntegrations(
21
21
  pi: ExtensionAPI,
22
22
  lifecycle: NotesLifecycle,
23
23
  tools: ReturnType<typeof createTools>,
24
+ prepareTools: (ctx?: ExtensionContext) => void,
24
25
  ) {
25
26
  try {
26
27
  const { registerGptPromptMode } = await import(
27
28
  "@howaboua/pi-codex-conversion/prompt-mode"
28
29
  );
29
- const registration = registerGptPromptMode(pi, (active) => {
30
+ const registration = registerGptPromptMode(pi, (active, ctx) => {
31
+ prepareTools(ctx);
30
32
  for (const [name, normal, gpt] of [
31
33
  ["notes", NOTES_DESCRIPTION, GPT_NOTES_DESCRIPTION],
32
34
  ["new_context", NEW_CONTEXT_DESCRIPTION, GPT_NEW_CONTEXT_DESCRIPTION],
@@ -0,0 +1,99 @@
1
+ import {
2
+ existsSync,
3
+ mkdirSync,
4
+ readFileSync,
5
+ renameSync,
6
+ writeFileSync,
7
+ } from "node:fs";
8
+ import { dirname, join } from "node:path";
9
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
10
+ export const THINKING_LEVELS = [
11
+ "off",
12
+ "minimal",
13
+ "low",
14
+ "medium",
15
+ "high",
16
+ "xhigh",
17
+ "max",
18
+ ] as const;
19
+ export type ThinkingLevel = (typeof THINKING_LEVELS)[number];
20
+
21
+ export interface ModelRole {
22
+ id: string;
23
+ description: string;
24
+ provider: string;
25
+ model: string;
26
+ reasoning: ThinkingLevel;
27
+ }
28
+
29
+ export interface RoleConfig {
30
+ enabled: boolean;
31
+ roles: ModelRole[];
32
+ }
33
+
34
+ function roleConfigPath(): string {
35
+ return join(getAgentDir(), "notes-compaction", "model-roles.json");
36
+ }
37
+
38
+ export function readRoleConfig(path = roleConfigPath()): RoleConfig {
39
+ if (!existsSync(path)) return { enabled: false, roles: [] };
40
+ const value: unknown = JSON.parse(readFileSync(path, "utf8"));
41
+ if (
42
+ !value ||
43
+ typeof value !== "object" ||
44
+ !("enabled" in value) ||
45
+ typeof value.enabled !== "boolean" ||
46
+ !("roles" in value) ||
47
+ !Array.isArray(value.roles)
48
+ )
49
+ throw new Error(
50
+ "Model roles configuration must contain enabled and roles; repair it with /roles",
51
+ );
52
+ const ids = new Set<string>();
53
+ const roles = value.roles.map((role: unknown): ModelRole => {
54
+ if (
55
+ !role ||
56
+ typeof role !== "object" ||
57
+ !("id" in role) ||
58
+ typeof role.id !== "string" ||
59
+ !/^[a-z][a-z0-9-]*$/.test(role.id) ||
60
+ !("description" in role) ||
61
+ typeof role.description !== "string" ||
62
+ !role.description.trim() ||
63
+ !("provider" in role) ||
64
+ typeof role.provider !== "string" ||
65
+ !role.provider.trim() ||
66
+ !("model" in role) ||
67
+ typeof role.model !== "string" ||
68
+ !role.model.trim() ||
69
+ !("reasoning" in role) ||
70
+ typeof role.reasoning !== "string" ||
71
+ !THINKING_LEVELS.includes(role.reasoning as ThinkingLevel) ||
72
+ ids.has(role.id)
73
+ )
74
+ throw new Error(
75
+ "Invalid or duplicate model role; repair model-roles.json or use /roles to replace roles",
76
+ );
77
+ ids.add(role.id);
78
+ return {
79
+ id: role.id,
80
+ description: role.description,
81
+ provider: role.provider,
82
+ model: role.model,
83
+ reasoning: role.reasoning as ThinkingLevel,
84
+ };
85
+ });
86
+ return { enabled: value.enabled, roles };
87
+ }
88
+
89
+ export function writeRoleConfig(
90
+ config: RoleConfig,
91
+ path = roleConfigPath(),
92
+ ): void {
93
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
94
+ const temporary = `${path}.${process.pid}.tmp`;
95
+ writeFileSync(temporary, `${JSON.stringify(config, null, 2)}\n`, {
96
+ mode: 0o600,
97
+ });
98
+ renameSync(temporary, path);
99
+ }
@@ -0,0 +1,40 @@
1
+ import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
+ import {
3
+ type ModelRole,
4
+ THINKING_LEVELS,
5
+ type ThinkingLevel,
6
+ } from "./model-role-config.js";
7
+
8
+ export async function editModelRole(
9
+ ctx: ExtensionCommandContext,
10
+ ): Promise<ModelRole | undefined> {
11
+ const id = (
12
+ await ctx.ui.input("Role ID", "Lowercase letters, numbers and hyphens")
13
+ )?.trim();
14
+ if (!id) return;
15
+ if (!/^[a-z][a-z0-9-]*$/.test(id))
16
+ throw new Error(
17
+ "Role IDs must start with a lowercase letter and contain only lowercase letters, numbers and hyphens",
18
+ );
19
+ const description = (
20
+ await ctx.ui.input("When should this role be preferred?")
21
+ )?.trim();
22
+ if (!description) return;
23
+ const models = ctx.modelRegistry.getAvailable();
24
+ const labels = models.map((model) => `${model.provider}/${model.id}`);
25
+ const selected = await ctx.ui.select("Role model", labels);
26
+ const model = models[labels.indexOf(selected ?? "")];
27
+ if (!model) return;
28
+ const reasoning = await ctx.ui.select(
29
+ "Preferred reasoning (Pi clamps unsupported levels)",
30
+ [...THINKING_LEVELS],
31
+ );
32
+ if (!reasoning) return;
33
+ return {
34
+ id,
35
+ description,
36
+ provider: model.provider,
37
+ model: model.id,
38
+ reasoning: reasoning as ThinkingLevel,
39
+ };
40
+ }
@@ -0,0 +1,58 @@
1
+ import {
2
+ type ExtensionContext,
3
+ getSettingsListTheme,
4
+ } from "@earendil-works/pi-coding-agent";
5
+ import {
6
+ Key,
7
+ matchesKey,
8
+ SettingsList,
9
+ truncateToWidth,
10
+ wrapTextWithAnsi,
11
+ } from "@earendil-works/pi-tui";
12
+ import type { ModelRole } from "./model-role-config.js";
13
+
14
+ export function openModelRoleScreen(
15
+ ctx: ExtensionContext,
16
+ choices: string[],
17
+ roles: ModelRole[],
18
+ ) {
19
+ return ctx.ui.custom<string | undefined>((tui, theme, _kb, done) => {
20
+ const list = new SettingsList(
21
+ choices.map((choice) => ({
22
+ id: choice,
23
+ label: choice,
24
+ currentValue: "select…",
25
+ values: ["select…", "open…"],
26
+ })),
27
+ 8,
28
+ getSettingsListTheme(),
29
+ (id) => done(id),
30
+ () => done(undefined),
31
+ );
32
+ return {
33
+ render: (width: number) =>
34
+ [
35
+ ` ${theme.fg("dim", "Notes")} / ${theme.bold("Roles")}`,
36
+ "",
37
+ ...roles.flatMap((role) =>
38
+ wrapTextWithAnsi(
39
+ ` ${role.id}: ${role.provider}/${role.model} (${role.reasoning})\n ${role.description}`,
40
+ width,
41
+ ),
42
+ ),
43
+ "",
44
+ ...list.render(width),
45
+ " Tab switch tab · ↑/↓ select · Enter change · Esc close",
46
+ ].map((line) => truncateToWidth(line, width)),
47
+ invalidate: () => list.invalidate(),
48
+ handleInput: (data: string) => {
49
+ if (matchesKey(data, Key.tab) || matchesKey(data, Key.shift(Key.tab))) {
50
+ done("notes-tab");
51
+ return;
52
+ }
53
+ list.handleInput(data);
54
+ tui.requestRender();
55
+ },
56
+ };
57
+ });
58
+ }