@0xkahi/pi-qol 1.0.0 → 1.0.2

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.
Files changed (35) hide show
  1. package/README.md +4 -2
  2. package/assets/config.schema.json +10 -1
  3. package/package.json +1 -1
  4. package/src/codemap.md +25 -16
  5. package/src/extensions/auto-session-name/codemap.md +17 -12
  6. package/src/extensions/codemap.md +166 -13
  7. package/src/extensions/context-view/codemap.md +201 -16
  8. package/src/extensions/context-view/index.ts +7 -1
  9. package/src/extensions/context-view/ui/codemap.md +82 -0
  10. package/src/extensions/context-view/ui/context-view-dialog.ts +37 -51
  11. package/src/extensions/context-view/ui/injections-model.ts +2 -141
  12. package/src/extensions/context-view/ui/injections-view.ts +63 -195
  13. package/src/extensions/context-view/ui/usage-view.ts +68 -189
  14. package/src/extensions/model-select/codemap.md +34 -17
  15. package/src/extensions/model-select/index.ts +6 -13
  16. package/src/extensions/model-select/model-select-dialog.ts +85 -324
  17. package/src/extensions/model-select/types.ts +2 -2
  18. package/src/libs/codemap.md +8 -1
  19. package/src/libs/modal/README.md +223 -0
  20. package/src/libs/modal/codemap.md +35 -0
  21. package/src/libs/modal/index.ts +30 -0
  22. package/src/libs/modal/list-navigator.ts +167 -0
  23. package/src/libs/modal/modal-dialog.ts +297 -0
  24. package/src/libs/modal/navigation/pi-scheme.ts +33 -0
  25. package/src/libs/modal/navigation/vim-scheme.ts +56 -0
  26. package/src/libs/modal/presenter.ts +38 -0
  27. package/src/libs/modal/preview-layer.ts +87 -0
  28. package/src/libs/modal/render-cache.ts +33 -0
  29. package/src/libs/modal/tab-strip.ts +87 -0
  30. package/src/libs/modal/tabs/list-tab.ts +159 -0
  31. package/src/{extensions/context-view/ui/layout.ts → libs/modal/text.ts} +15 -15
  32. package/src/libs/modal/types.ts +70 -0
  33. package/src/schemas/codemap.md +101 -18
  34. package/src/schemas/context-view.config.schema.ts +5 -0
  35. package/src/extensions/context-view/ui/navigation.ts +0 -37
@@ -1,37 +1,29 @@
1
1
  /** Forked from dimk90/pi-context-view at f6f007b867212bcf81a61519c8e40ce209cdd608 (MIT). */
2
2
  /**
3
- * Focused `/context injections` view: hierarchical Initial snapshot rows. The
3
+ * Focused `/context injections` tab: hierarchical Initial snapshot rows. The
4
4
  * Runtime label stays hidden until the runtime-inspection roadmap step.
5
+ * Modal plumbing — frame, navigation dispatch, previews, dismissal — is owned
6
+ * by the shared modal library; this tab owns only its content.
5
7
  */
6
8
  import type { Theme } from '@earendil-works/pi-coding-agent';
7
- import { Key, matchesKey, visibleWidth, wrapTextWithAnsi } from '@earendil-works/pi-tui';
9
+ import { visibleWidth, wrapTextWithAnsi } from '@earendil-works/pi-tui';
8
10
 
9
- import type { InitialSnapshot, InjectionItem } from '../model';
10
- import {
11
- buildInjectionRows,
12
- collectItemsById,
13
- type InjectionRow,
14
- ListNavigator,
15
- normalizeInlineText,
16
- normalizePreviewText,
17
- PreviewScroller,
18
- } from './injections-model';
19
11
  import {
20
12
  BODY_INDENT,
21
13
  calculateViewport,
22
- DEFAULT_TERMINAL_ROWS,
23
14
  fitLine,
24
- fitToTerminalHeight,
25
- hintRow,
26
- normalizeTerminalRows,
27
- STEP_KEY_HINT,
28
- spreadLine,
15
+ type Hint,
16
+ ListNavigator,
17
+ type ModalTab,
18
+ type ModalTabContext,
19
+ type NavigationAction,
20
+ PreviewLayer,
21
+ RenderCache,
29
22
  wrapDescriptionLines,
30
- } from './layout';
31
- import type { NavigationAction } from './navigation';
23
+ } from '../../../libs/modal';
24
+ import type { InitialSnapshot, InjectionItem } from '../model';
25
+ import { buildInjectionRows, collectItemsById, type InjectionRow, normalizeInlineText, normalizePreviewText } from './injections-model';
32
26
 
33
- const LIST_FIXED_LINE_COUNT = 10;
34
- const PREVIEW_FIXED_LINE_COUNT = 8;
35
27
  const LIST_DESCRIPTION = 'Injections into the model context for the first turn, with token estimates.';
36
28
  const CURSOR_COLUMN_WIDTH = 2;
37
29
  const MAX_TOKEN_VALUE_COLUMN = 54;
@@ -40,7 +32,6 @@ const TOKEN_LEADER_GAP = 4;
40
32
  /** Everything the Injections view renders. */
41
33
  export interface InjectionsViewInput {
42
34
  readonly snapshot: InitialSnapshot;
43
- readonly degradedReason?: string;
44
35
  }
45
36
 
46
37
  /** Shared token-value column measured after the fixed cursor column. */
@@ -48,71 +39,43 @@ interface InjectionColumns {
48
39
  readonly value: number;
49
40
  }
50
41
 
51
- /** Stateful Injections tab retained by ContextViewDialog. */
52
- export class InjectionsView {
42
+ /** Stateful Injections tab retained by the Context View dialog. */
43
+ export class InjectionsView implements ModalTab {
53
44
  private readonly theme: Theme;
54
- private readonly input: InjectionsViewInput;
55
- private readonly done: (result: undefined) => void;
56
- private readonly getTerminalRows: () => number;
57
45
  private readonly rows: InjectionRow[];
58
46
  private readonly navigator: ListNavigator;
59
47
  private readonly itemsById: Map<string, InjectionItem>;
60
- private readonly previewScroller = new PreviewScroller();
48
+ private readonly cache = new RenderCache();
49
+ private context: ModalTabContext | undefined;
61
50
  private previewItem: InjectionItem | undefined;
62
51
  private previewLines: string[] | undefined;
63
52
  private previewWrapWidth: number | undefined;
64
- private cachedWidth: number | undefined;
65
- private cachedTerminalRows: number | undefined;
66
- private cachedLines: string[] | undefined;
67
53
 
68
- public constructor(
69
- theme: Theme,
70
- input: InjectionsViewInput,
71
- done: (result: undefined) => void,
72
- getTerminalRows: () => number = () => process.stdout.rows ?? DEFAULT_TERMINAL_ROWS,
73
- ) {
54
+ public constructor(theme: Theme, input: InjectionsViewInput) {
74
55
  this.theme = theme;
75
- this.input = input;
76
- this.done = done;
77
- this.getTerminalRows = getTerminalRows;
78
56
  this.rows = buildInjectionRows(input.snapshot);
79
57
  this.navigator = new ListNavigator(this.rows.length, 1, this.rows.length - 2);
80
58
  this.itemsById = collectItemsById(input.snapshot);
81
59
  }
82
60
 
83
- public handleInput(data: string): void {
84
- if (this.previewItem !== undefined) {
85
- this.handlePreviewInput(data);
86
- return;
87
- }
88
- if (matchesKey(data, Key.escape) || data === 'q') {
89
- this.done(undefined);
90
- return;
91
- }
92
- if (matchesKey(data, Key.enter)) {
93
- this.openPreview();
94
- }
61
+ public get label(): string {
62
+ return '[Injections]';
95
63
  }
96
64
 
97
- /** Apply semantic movement in either the hierarchy or its preview. */
98
- public handleNavigation(action: NavigationAction): void {
99
- if (this.previewItem !== undefined) {
100
- const changed =
101
- action === 'step-back'
102
- ? this.previewScroller.scrollBy(-1)
103
- : action === 'step-forward'
104
- ? this.previewScroller.scrollBy(1)
105
- : action === 'page-back'
106
- ? this.previewScroller.page(-1)
107
- : action === 'page-forward'
108
- ? this.previewScroller.page(1)
109
- : action === 'first'
110
- ? this.previewScroller.scrollTo(0)
111
- : this.previewScroller.scrollTo(this.previewScroller.maxOffset);
112
- if (changed) this.clearCache();
113
- return;
114
- }
65
+ public attach(context: ModalTabContext): void {
66
+ this.context = context;
67
+ }
68
+
69
+ public hints(): Hint[] {
70
+ return [['Enter', 'Preview']];
71
+ }
72
+
73
+ public handleInput(_data: string): void {
74
+ // All Injections keys are semantic navigation actions handled by the shell.
75
+ }
115
76
 
77
+ /** Apply semantic movement in the hierarchy, or open an item preview. */
78
+ public handleNavigation(action: NavigationAction): void {
116
79
  const changed =
117
80
  action === 'step-back'
118
81
  ? this.navigator.moveBy(-1)
@@ -124,73 +87,41 @@ export class InjectionsView {
124
87
  ? this.navigator.page(1)
125
88
  : action === 'first'
126
89
  ? this.navigator.moveTo(0)
127
- : this.navigator.moveTo(this.rows.length - 1);
128
- if (changed) this.clearCache();
90
+ : action === 'last'
91
+ ? this.navigator.moveTo(this.rows.length - 1)
92
+ : false;
93
+ if (changed) this.cache.clear();
94
+ if (action === 'confirm') this.openPreview();
129
95
  }
130
96
 
131
- public render(width: number): string[] {
132
- const terminalRows = normalizeTerminalRows(this.getTerminalRows());
133
- if (this.cachedLines !== undefined && this.cachedWidth === width && this.cachedTerminalRows === terminalRows) {
134
- return this.cachedLines;
135
- }
136
- if (this.previewItem !== undefined) {
137
- const lines = this.renderPreview(width, terminalRows, this.previewItem);
138
- this.cachedWidth = width;
139
- this.cachedTerminalRows = terminalRows;
140
- this.cachedLines = lines;
141
- return lines;
142
- }
143
- const theme = this.theme;
144
- const border = theme.fg('border', '─'.repeat(Math.max(1, width)));
97
+ public render(width: number, height: number | undefined): string[] {
98
+ const cached = this.cache.read(width, height);
99
+ if (cached !== undefined) return cached;
100
+
101
+ const terminalRows = height ?? this.rows.length + 10;
145
102
  const headerLines = this.headerLines(width);
146
- const warningLines = this.degradedWarningLines(width);
147
- const descriptionLines = this.descriptionLines(width);
148
- const extraLineCount = headerLines.length - 1 + warningLines.length + descriptionLines.length - 1;
149
- const viewport = calculateViewport(this.rows.length, terminalRows, LIST_FIXED_LINE_COUNT, extraLineCount);
103
+ const descriptionLines = wrapDescriptionLines(this.theme, LIST_DESCRIPTION, 'dim', width);
104
+ const viewport = calculateViewport(this.rows.length, terminalRows, headerLines.length + descriptionLines.length + 2);
150
105
  this.navigator.setVisibleCount(viewport.visibleCount);
151
- const lines: string[] = [border, '', ...headerLines, '', ...warningLines];
106
+
107
+ const lines: string[] = [...headerLines, ''];
152
108
  const listLines = this.listLines(width);
153
109
  lines.push(...listLines);
154
110
  if (viewport.showScroll) lines.push(this.scrollLine(width));
155
111
  const paddingCount = viewport.visibleCount - listLines.length;
156
112
  for (let pad = 0; pad < paddingCount; pad++) lines.push('');
157
- lines.push('');
158
- lines.push(...descriptionLines);
159
- lines.push('');
160
- lines.push(
161
- this.fit(
162
- hintRow(this.theme, [
163
- [STEP_KEY_HINT, 'Navigate'],
164
- ['Ctrl+u/d', 'Page'],
165
- ['gg/G', 'Bounds'],
166
- ['Enter', 'Preview'],
167
- ['Esc', 'Close'],
168
- ]),
169
- width,
170
- ),
171
- );
172
- lines.push('', border);
113
+ lines.push('', ...descriptionLines);
173
114
 
174
- const fittedLines = fitToTerminalHeight(lines, terminalRows, border);
175
- this.cachedWidth = width;
176
- this.cachedTerminalRows = terminalRows;
177
- this.cachedLines = fittedLines;
178
- return fittedLines;
115
+ return this.cache.write(width, height, lines);
179
116
  }
180
117
 
181
118
  public invalidate(): void {
182
- this.clearCache();
119
+ this.cache.clear();
183
120
  }
184
121
 
185
- // === Preview mode ===
186
-
187
- private handlePreviewInput(data: string): void {
188
- if (matchesKey(data, Key.escape) || data === 'q') {
189
- this.closePreview();
190
- return;
191
- }
192
- }
122
+ // === Preview ===
193
123
 
124
+ /** Open the selected item's content preview as a shell-managed layer. */
194
125
  private openPreview(): void {
195
126
  const row = this.rows[this.navigator.selected];
196
127
  if (row?.kind !== 'item') return;
@@ -199,56 +130,22 @@ export class InjectionsView {
199
130
  this.previewItem = item;
200
131
  this.previewLines = undefined;
201
132
  this.previewWrapWidth = undefined;
202
- this.previewScroller.reset();
203
- this.clearCache();
204
- }
205
-
206
- private closePreview(): void {
207
- this.previewItem = undefined;
208
- this.previewLines = undefined;
209
- this.previewWrapWidth = undefined;
210
- this.clearCache();
211
- }
212
133
 
213
- private renderPreview(width: number, terminalRows: number, item: InjectionItem): string[] {
214
- const theme = this.theme;
215
- const border = theme.fg('border', '─'.repeat(Math.max(1, width)));
216
- const wrapped = this.getPreviewLines(width, item);
217
- const viewport = calculateViewport(wrapped.length, terminalRows, PREVIEW_FIXED_LINE_COUNT);
218
- this.previewScroller.setExtent(wrapped.length, viewport.visibleCount);
219
-
220
- const lines: string[] = [border, ''];
221
- const title = theme.fg('accent', theme.bold(normalizeInlineText(item.label)));
222
134
  const source = normalizeInlineText(item.source.label);
223
- const meta = theme.fg('muted', `${source} · ${item.tokens.toLocaleString('en-US')} tokens `);
224
- lines.push(this.spread(title, meta, width));
225
- lines.push('');
226
-
227
- const start = this.previewScroller.offset;
228
- for (let index = start; index < start + viewport.visibleCount; index++) {
229
- lines.push(wrapped[index] ?? '');
230
- }
231
-
232
- if (viewport.showScroll) lines.push(this.previewScrollLine(width, wrapped.length));
233
- lines.push('');
234
- lines.push(
235
- this.fit(
236
- hintRow(this.theme, [
237
- [STEP_KEY_HINT, 'Scroll'],
238
- ['Ctrl+u/d', 'Page'],
239
- ['gg/G', 'Bounds'],
240
- ['Esc', 'Back'],
241
- ]),
242
- width,
243
- ),
135
+ this.context?.pushLayer(
136
+ new PreviewLayer(this.theme, {
137
+ title: () => this.theme.fg('accent', this.theme.bold(normalizeInlineText(item.label))),
138
+ meta: () => this.theme.fg('muted', `${source} · ${item.tokens.toLocaleString('en-US')} tokens`),
139
+ body: width => this.getPreviewLines(width, item),
140
+ }),
244
141
  );
245
- lines.push('', border);
246
- return fitToTerminalHeight(lines, terminalRows, border);
247
142
  }
248
143
 
249
144
  private getPreviewLines(width: number, item: InjectionItem): string[] {
250
145
  const wrapWidth = Math.max(10, width - BODY_INDENT.length - 1);
251
- if (this.previewLines !== undefined && this.previewWrapWidth === wrapWidth) return this.previewLines;
146
+ if (this.previewItem === item && this.previewLines !== undefined && this.previewWrapWidth === wrapWidth) {
147
+ return this.previewLines;
148
+ }
252
149
  const text = normalizePreviewText(item.text);
253
150
  const lines: string[] = [];
254
151
  for (const paragraph of text.split('\n')) {
@@ -264,10 +161,7 @@ export class InjectionsView {
264
161
  return lines;
265
162
  }
266
163
 
267
- private previewScrollLine(width: number, totalLines: number): string {
268
- if (!this.previewScroller.hasOverflow) return this.fit('', width);
269
- return this.fit(this.theme.fg('dim', `${BODY_INDENT}(${this.previewScroller.visibleEnd}/${totalLines})`), width);
270
- }
164
+ // === List rendering ===
271
165
 
272
166
  /** Keep title/label together when possible; give the narrow label its own breathing room. */
273
167
  private headerLines(width: number): string[] {
@@ -364,33 +258,7 @@ export class InjectionsView {
364
258
  return this.fit(this.theme.fg('dim', `${BODY_INDENT}(${this.navigator.selectedOrdinal + 1}/${this.navigator.selectableCount})`), width);
365
259
  }
366
260
 
367
- /** Wrapped degraded-capture reason placed below the dialog header. */
368
- private degradedWarningLines(width: number): string[] {
369
- if (this.input.degradedReason === undefined) return [];
370
- const reason = this.theme.fg('warning', `${BODY_INDENT}${normalizeInlineText(this.input.degradedReason)}`);
371
- return wrapTextWithAnsi(reason, width);
372
- }
373
-
374
- /** Wrapped dialog description, including the degraded-capture indicator when needed. */
375
- private descriptionLines(width: number): string[] {
376
- const lines = wrapDescriptionLines(this.theme, LIST_DESCRIPTION, 'dim', width);
377
- if (this.input.degradedReason !== undefined) {
378
- lines.push(...wrapDescriptionLines(this.theme, '[Degraded: pi-native fallback used]', 'warning', width));
379
- }
380
- return lines;
381
- }
382
-
383
- private spread(left: string, right: string, width: number): string {
384
- return spreadLine(left, right, width);
385
- }
386
-
387
261
  private fit(line: string, width: number): string {
388
262
  return fitLine(line, width);
389
263
  }
390
-
391
- private clearCache(): void {
392
- this.cachedWidth = undefined;
393
- this.cachedTerminalRows = undefined;
394
- this.cachedLines = undefined;
395
- }
396
264
  }