@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.
@@ -0,0 +1,209 @@
1
+ import {
2
+ type ExtensionContext,
3
+ getSettingsListTheme,
4
+ } from "@earendil-works/pi-coding-agent";
5
+ import { SettingsList } from "@earendil-works/pi-tui";
6
+ import {
7
+ IDLE_MINUTES,
8
+ NEW_WINDOW_REQUESTS,
9
+ type NotesSettings,
10
+ } from "./settings.js";
11
+ import { TASK_BOUNDARY_PRESETS, taskBoundaryLabel } from "./task-boundary.js";
12
+
13
+ export function createNotesSettingsControls(
14
+ ctx: ExtensionContext,
15
+ options: {
16
+ settings: () => NotesSettings;
17
+ settingsPath: string;
18
+ save: (settings: NotesSettings) => void;
19
+ },
20
+ done: (action: "close" | "prune" | "sessionPruning") => void,
21
+ requestRender: () => void,
22
+ ) {
23
+ const settings = options.settings();
24
+ const idleValue = (minutes: number) =>
25
+ minutes === 0
26
+ ? "Off"
27
+ : minutes < 60
28
+ ? `${minutes}m`
29
+ : `${Math.ceil(minutes / 60)}h`;
30
+ const list = new SettingsList(
31
+ [
32
+ {
33
+ id: "sessionPruning",
34
+ label: "Session auto-pruning",
35
+ currentValue: "select…",
36
+ values: ["select…", "settings…"],
37
+ description:
38
+ "Per-session history limits and Run now. Memory savings require restarting Pi.",
39
+ },
40
+ {
41
+ id: "active",
42
+ label: "Active",
43
+ description: `Whitelist matches exact model IDs from any provider. Edit modelWhitelist in ${options.settingsPath}, then reload Pi. Example: ["gpt-6-luna", "gpt-6.1-sol"].`,
44
+ currentValue: settings.active === "all" ? "All models" : "Whitelist",
45
+ values: ["All models", "Whitelist"],
46
+ },
47
+ {
48
+ id: "sharing",
49
+ label: "Share subagent context",
50
+ description:
51
+ "Let new child agents share notes and history. Existing families remain connected when off.",
52
+ currentValue: settings.shareSubagentContext ? "on" : "off",
53
+ values: ["off", "on"],
54
+ },
55
+ {
56
+ id: "rankedHistorySearch",
57
+ label: "Ranked history search",
58
+ description:
59
+ "BM25 for earlier user messages and assistant replies. Skips current context and tool activity.",
60
+ currentValue: settings.rankedHistorySearch ? "on" : "off",
61
+ values: ["off", "on"],
62
+ },
63
+ {
64
+ id: "noteSaveMarkers",
65
+ label: "Note-save markers",
66
+ currentValue: settings.noteSaveMarkers ? "on" : "off",
67
+ values: ["off", "on"],
68
+ },
69
+ {
70
+ id: "compact",
71
+ label: "Normal compaction",
72
+ description:
73
+ "Use Pi/PCC compaction for /compact and new_context. Off uses notes-only rollover; /compact then waits for your next prompt.",
74
+ currentValue: settings.normalCompaction ? "on" : "off",
75
+ values: ["off", "on"],
76
+ },
77
+ {
78
+ id: "newWindowRequests",
79
+ label: "New window requests",
80
+ description:
81
+ "Off disables new_context. Pi-limits keeps the current window when Pi finds too little history to compact. Unrestricted accepts requests at any size. Normal compaction still chooses how windows roll over. Manual /compact and idle rollover are separate.",
82
+ currentValue: settings.newWindowRequests,
83
+ values: [...NEW_WINDOW_REQUESTS],
84
+ },
85
+ {
86
+ id: "taskBoundaryReminder",
87
+ label: "Task-boundary reminder",
88
+ description:
89
+ "Suggest a separate-task rollover after this many used tokens, only with active capacity above 272k. Custom: edit taskBoundaryReminder in the notes settings JSON (whole tokens); 0 is off.",
90
+ currentValue: taskBoundaryLabel(settings.taskBoundaryReminder),
91
+ values: [
92
+ ...TASK_BOUNDARY_PRESETS.map(taskBoundaryLabel),
93
+ "custom (edit JSON)",
94
+ ],
95
+ },
96
+ {
97
+ id: "idle",
98
+ label: "Idle rollover",
99
+ description:
100
+ "Roll over before the next input only with fresh notes from a completed turn. Not a cache-expiry guarantee.",
101
+ currentValue: idleValue(settings.idleMinutes),
102
+ values: IDLE_MINUTES.map(idleValue),
103
+ },
104
+ {
105
+ id: "prune",
106
+ label: "Prune missing sessions",
107
+ description:
108
+ "Remove notes, cached responses and history index entries for missing session files. Keeps the active session, unknown paths and inaccessible files. Confirmation required.",
109
+ currentValue: "select…",
110
+ values: ["select…", "prune…"],
111
+ },
112
+ ],
113
+ 5,
114
+ getSettingsListTheme(),
115
+ (id, value) => {
116
+ if (id === "sessionPruning") {
117
+ done("sessionPruning");
118
+ return;
119
+ }
120
+ if (id === "prune") {
121
+ done("prune");
122
+ return;
123
+ }
124
+ const current = options.settings();
125
+ if (id === "taskBoundaryReminder" && value === "custom (edit JSON)") {
126
+ ctx.ui.notify(
127
+ `Set taskBoundaryReminder to a positive whole token count in ${options.settingsPath}, then reload Pi. 0 is off.`,
128
+ "info",
129
+ );
130
+ list.updateValue(id, taskBoundaryLabel(current.taskBoundaryReminder));
131
+ return;
132
+ }
133
+ try {
134
+ if (id === "active")
135
+ options.save({
136
+ ...current,
137
+ active: value === "All models" ? "all" : "whitelist",
138
+ });
139
+ else if (id === "sharing")
140
+ options.save({
141
+ ...current,
142
+ shareSubagentContext: value === "on",
143
+ });
144
+ else if (id === "noteSaveMarkers")
145
+ options.save({ ...current, noteSaveMarkers: value === "on" });
146
+ else if (id === "rankedHistorySearch")
147
+ options.save({
148
+ ...current,
149
+ rankedHistorySearch: value === "on",
150
+ });
151
+ else if (id === "compact")
152
+ options.save({ ...current, normalCompaction: value === "on" });
153
+ else if (id === "newWindowRequests") {
154
+ const newWindowRequests = NEW_WINDOW_REQUESTS.find(
155
+ (mode) => mode === value,
156
+ );
157
+ if (newWindowRequests)
158
+ options.save({ ...current, newWindowRequests });
159
+ } else if (id === "taskBoundaryReminder")
160
+ options.save({
161
+ ...current,
162
+ taskBoundaryReminder:
163
+ TASK_BOUNDARY_PRESETS.find(
164
+ (tokens) => taskBoundaryLabel(tokens) === value,
165
+ ) ?? current.taskBoundaryReminder,
166
+ });
167
+ else {
168
+ const idleMinutes = IDLE_MINUTES.find(
169
+ (minutes) => idleValue(minutes) === value,
170
+ );
171
+ if (idleMinutes !== undefined)
172
+ options.save({ ...current, idleMinutes });
173
+ }
174
+ } catch (error) {
175
+ if (id === "rankedHistorySearch") {
176
+ list.updateValue(id, current.rankedHistorySearch ? "on" : "off");
177
+ } else
178
+ list.updateValue(
179
+ id,
180
+ id === "active"
181
+ ? current.active === "all"
182
+ ? "All models"
183
+ : "Whitelist"
184
+ : id === "taskBoundaryReminder"
185
+ ? taskBoundaryLabel(current.taskBoundaryReminder)
186
+ : id === "newWindowRequests"
187
+ ? current.newWindowRequests
188
+ : id === "noteSaveMarkers"
189
+ ? current.noteSaveMarkers
190
+ ? "on"
191
+ : "off"
192
+ : id === "compact"
193
+ ? current.normalCompaction
194
+ ? "on"
195
+ : "off"
196
+ : idleValue(current.idleMinutes),
197
+ );
198
+ ctx.ui.notify(
199
+ `Could not save notes settings: ${String(error)}`,
200
+ "error",
201
+ );
202
+ }
203
+ requestRender();
204
+ },
205
+ () => done("close"),
206
+ { enableSearch: false },
207
+ );
208
+ return list;
209
+ }
@@ -1,9 +1,7 @@
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
1
2
  import {
2
- type ExtensionContext,
3
- getSettingsListTheme,
4
- } from "@earendil-works/pi-coding-agent";
5
- import {
6
- SettingsList,
3
+ Key,
4
+ matchesKey,
7
5
  truncateToWidth,
8
6
  wrapTextWithAnsi,
9
7
  } from "@earendil-works/pi-tui";
@@ -11,12 +9,10 @@ import {
11
9
  openSessionPruningSettings,
12
10
  type SessionPruningSettings,
13
11
  } from "./session-pruning-screen.js";
14
- import {
15
- IDLE_MINUTES,
16
- NEW_WINDOW_REQUESTS,
17
- type NotesSettings,
18
- } from "./settings.js";
19
- import { TASK_BOUNDARY_PRESETS, taskBoundaryLabel } from "./task-boundary.js";
12
+ import type { NotesSettings } from "./settings.js";
13
+ import { createNotesSettingsControls } from "./settings-controls.js";
14
+
15
+ type ScreenAction = "close" | "prune" | "sessionPruning" | "roles";
20
16
 
21
17
  export async function openNotesSettings(
22
18
  ctx: ExtensionContext,
@@ -28,204 +24,12 @@ export async function openNotesSettings(
28
24
  prune: () => Promise<void>;
29
25
  sessionPruning: SessionPruningSettings;
30
26
  },
31
- ): Promise<void> {
27
+ ): Promise<"roles" | undefined> {
32
28
  while (true) {
33
- const action = await ctx.ui.custom<"close" | "prune" | "sessionPruning">(
29
+ const action = await ctx.ui.custom<ScreenAction>(
34
30
  (tui, theme, _kb, done) => {
35
- const settings = options.settings();
36
- const idleValue = (minutes: number) =>
37
- minutes === 0
38
- ? "Off"
39
- : minutes < 60
40
- ? `${minutes}m`
41
- : `${Math.ceil(minutes / 60)}h`;
42
- const list = new SettingsList(
43
- [
44
- {
45
- id: "sessionPruning",
46
- label: "Session auto-pruning",
47
- currentValue: "select…",
48
- values: ["select…", "settings…"],
49
- description:
50
- "Per-session history limits and Run now. Memory savings require restarting Pi.",
51
- },
52
- {
53
- id: "active",
54
- label: "Active",
55
- description: `Whitelist matches exact model IDs from any provider. Edit modelWhitelist in ${options.settingsPath}, then reload Pi. Example: ["gpt-6-luna", "gpt-6.1-sol"].`,
56
- currentValue:
57
- settings.active === "all" ? "All models" : "Whitelist",
58
- values: ["All models", "Whitelist"],
59
- },
60
- {
61
- id: "sharing",
62
- label: "Share subagent context",
63
- description:
64
- "Let new child agents share notes and history. Existing families remain connected when off.",
65
- currentValue: settings.shareSubagentContext ? "on" : "off",
66
- values: ["off", "on"],
67
- },
68
- {
69
- id: "rankedHistorySearch",
70
- label: "Ranked history search",
71
- description:
72
- "BM25 for earlier user messages and assistant replies. Skips current context and tool activity.",
73
- currentValue: settings.rankedHistorySearch ? "on" : "off",
74
- values: ["off", "on"],
75
- },
76
- {
77
- id: "noteSaveMarkers",
78
- label: "Note-save markers",
79
- currentValue: settings.noteSaveMarkers ? "on" : "off",
80
- values: ["off", "on"],
81
- },
82
- {
83
- id: "compact",
84
- label: "Normal compaction",
85
- description:
86
- "Use Pi/PCC compaction for /compact and new_context. Off uses notes-only rollover; /compact then waits for your next prompt.",
87
- currentValue: settings.normalCompaction ? "on" : "off",
88
- values: ["off", "on"],
89
- },
90
- {
91
- id: "newWindowRequests",
92
- label: "New window requests",
93
- description:
94
- "Off disables new_context. Pi-limits keeps the current window when Pi finds too little history to compact. Unrestricted accepts requests at any size. Normal compaction still chooses how windows roll over. Manual /compact and idle rollover are separate.",
95
- currentValue: settings.newWindowRequests,
96
- values: [...NEW_WINDOW_REQUESTS],
97
- },
98
- {
99
- id: "taskBoundaryReminder",
100
- label: "Task-boundary reminder",
101
- description:
102
- "Suggest a separate-task rollover after this many used tokens, only with active capacity above 272k. Custom: edit taskBoundaryReminder in the notes settings JSON (whole tokens); 0 is off.",
103
- currentValue: taskBoundaryLabel(settings.taskBoundaryReminder),
104
- values: [
105
- ...TASK_BOUNDARY_PRESETS.map(taskBoundaryLabel),
106
- "custom (edit JSON)",
107
- ],
108
- },
109
- {
110
- id: "idle",
111
- label: "Idle rollover",
112
- description:
113
- "Roll over before the next input only with fresh notes from a completed turn. Not a cache-expiry guarantee.",
114
- currentValue: idleValue(settings.idleMinutes),
115
- values: IDLE_MINUTES.map(idleValue),
116
- },
117
- {
118
- id: "prune",
119
- label: "Prune missing sessions",
120
- description:
121
- "Remove notes, cached responses and history index entries for missing session files. Keeps the active session, unknown paths and inaccessible files. Confirmation required.",
122
- currentValue: "select…",
123
- values: ["select…", "prune…"],
124
- },
125
- ],
126
- 5,
127
- getSettingsListTheme(),
128
- (id, value) => {
129
- if (id === "sessionPruning") {
130
- done("sessionPruning");
131
- return;
132
- }
133
- if (id === "prune") {
134
- done("prune");
135
- return;
136
- }
137
- const current = options.settings();
138
- if (
139
- id === "taskBoundaryReminder" &&
140
- value === "custom (edit JSON)"
141
- ) {
142
- ctx.ui.notify(
143
- `Set taskBoundaryReminder to a positive whole token count in ${options.settingsPath}, then reload Pi. 0 is off.`,
144
- "info",
145
- );
146
- list.updateValue(
147
- id,
148
- taskBoundaryLabel(current.taskBoundaryReminder),
149
- );
150
- return;
151
- }
152
- try {
153
- if (id === "active")
154
- options.save({
155
- ...current,
156
- active: value === "All models" ? "all" : "whitelist",
157
- });
158
- else if (id === "sharing")
159
- options.save({
160
- ...current,
161
- shareSubagentContext: value === "on",
162
- });
163
- else if (id === "noteSaveMarkers")
164
- options.save({ ...current, noteSaveMarkers: value === "on" });
165
- else if (id === "rankedHistorySearch")
166
- options.save({
167
- ...current,
168
- rankedHistorySearch: value === "on",
169
- });
170
- else if (id === "compact")
171
- options.save({ ...current, normalCompaction: value === "on" });
172
- else if (id === "newWindowRequests") {
173
- const newWindowRequests = NEW_WINDOW_REQUESTS.find(
174
- (mode) => mode === value,
175
- );
176
- if (newWindowRequests)
177
- options.save({ ...current, newWindowRequests });
178
- } else if (id === "taskBoundaryReminder")
179
- options.save({
180
- ...current,
181
- taskBoundaryReminder:
182
- TASK_BOUNDARY_PRESETS.find(
183
- (tokens) => taskBoundaryLabel(tokens) === value,
184
- ) ?? current.taskBoundaryReminder,
185
- });
186
- else {
187
- const idleMinutes = IDLE_MINUTES.find(
188
- (minutes) => idleValue(minutes) === value,
189
- );
190
- if (idleMinutes !== undefined)
191
- options.save({ ...current, idleMinutes });
192
- }
193
- } catch (error) {
194
- if (id === "rankedHistorySearch") {
195
- list.updateValue(
196
- id,
197
- current.rankedHistorySearch ? "on" : "off",
198
- );
199
- } else
200
- list.updateValue(
201
- id,
202
- id === "active"
203
- ? current.active === "all"
204
- ? "All models"
205
- : "Whitelist"
206
- : id === "taskBoundaryReminder"
207
- ? taskBoundaryLabel(current.taskBoundaryReminder)
208
- : id === "newWindowRequests"
209
- ? current.newWindowRequests
210
- : id === "noteSaveMarkers"
211
- ? current.noteSaveMarkers
212
- ? "on"
213
- : "off"
214
- : id === "compact"
215
- ? current.normalCompaction
216
- ? "on"
217
- : "off"
218
- : idleValue(current.idleMinutes),
219
- );
220
- ctx.ui.notify(
221
- `Could not save notes settings: ${String(error)}`,
222
- "error",
223
- );
224
- }
225
- tui.requestRender();
226
- },
227
- () => done("close"),
228
- { enableSearch: false },
31
+ const list = createNotesSettingsControls(ctx, options, done, () =>
32
+ tui.requestRender(),
229
33
  );
230
34
  return {
231
35
  render: (width) => {
@@ -236,7 +40,7 @@ export async function openNotesSettings(
236
40
  const rule = theme.fg("accent", "─".repeat(Math.max(0, width)));
237
41
  return [
238
42
  rule,
239
- ` ${theme.bold("Notes & context")}`,
43
+ ` ${theme.bold("Notes")} / ${theme.fg("dim", "Roles")}`,
240
44
  "",
241
45
  ...options
242
46
  .status()
@@ -250,9 +54,9 @@ export async function openNotesSettings(
250
54
  "",
251
55
  ...list.render(width).slice(0, -2),
252
56
  "",
253
- ...wrap("↑/↓ select · Enter/Space change · Esc close").map(
254
- (line) => theme.fg("dim", line),
255
- ),
57
+ ...wrap(
58
+ "Tab switch tab · ↑/↓ select · Enter/Space change · Esc close",
59
+ ).map((line) => theme.fg("dim", line)),
256
60
  ...wrap("Settings saved immediately").map((line) =>
257
61
  theme.fg("dim", line),
258
62
  ),
@@ -261,6 +65,13 @@ export async function openNotesSettings(
261
65
  },
262
66
  invalidate: () => list.invalidate(),
263
67
  handleInput: (data) => {
68
+ if (
69
+ matchesKey(data, Key.tab) ||
70
+ matchesKey(data, Key.shift(Key.tab))
71
+ ) {
72
+ done("roles");
73
+ return;
74
+ }
264
75
  list.handleInput(data);
265
76
  tui.requestRender();
266
77
  },
@@ -268,6 +79,7 @@ export async function openNotesSettings(
268
79
  },
269
80
  );
270
81
  if (action === "close") return;
82
+ if (action === "roles") return "roles";
271
83
  if (action === "sessionPruning") {
272
84
  await openSessionPruningSettings(ctx, options.sessionPruning);
273
85
  continue;