@0xkahi/pi-qol 1.0.0 → 1.0.1
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/package.json +1 -1
- package/src/codemap.md +25 -16
- package/src/extensions/auto-session-name/codemap.md +17 -12
- package/src/extensions/codemap.md +165 -12
- package/src/extensions/context-view/codemap.md +201 -16
- package/src/extensions/context-view/ui/codemap.md +82 -0
- package/src/extensions/context-view/ui/context-view-dialog.ts +32 -54
- package/src/extensions/context-view/ui/injections-model.ts +2 -141
- package/src/extensions/context-view/ui/injections-view.ts +63 -195
- package/src/extensions/context-view/ui/usage-view.ts +68 -189
- package/src/extensions/model-select/codemap.md +34 -17
- package/src/extensions/model-select/model-select-dialog.ts +85 -324
- package/src/libs/codemap.md +8 -1
- package/src/libs/modal/README.md +212 -0
- package/src/libs/modal/codemap.md +34 -0
- package/src/libs/modal/index.ts +29 -0
- package/src/libs/modal/list-navigator.ts +167 -0
- package/src/libs/modal/modal-dialog.ts +297 -0
- package/src/libs/modal/navigation/pi-scheme.ts +33 -0
- package/src/libs/modal/navigation/vim-scheme.ts +56 -0
- package/src/libs/modal/preview-layer.ts +87 -0
- package/src/libs/modal/render-cache.ts +33 -0
- package/src/libs/modal/tab-strip.ts +87 -0
- package/src/libs/modal/tabs/list-tab.ts +159 -0
- package/src/{extensions/context-view/ui/layout.ts → libs/modal/text.ts} +15 -15
- package/src/libs/modal/types.ts +70 -0
- package/src/schemas/codemap.md +100 -18
- package/src/extensions/context-view/ui/navigation.ts +0 -37
|
@@ -1,28 +1,31 @@
|
|
|
1
1
|
/** Forked from dimk90/pi-context-view at f6f007b867212bcf81a61519c8e40ce209cdd608 (MIT). */
|
|
2
2
|
/**
|
|
3
|
-
* Focused `/context usage`
|
|
3
|
+
* Focused `/context usage` tab: estimated context composition with a
|
|
4
4
|
* proportional context-window map, pi-reported metadata, selectable category
|
|
5
|
-
* rows, and
|
|
5
|
+
* rows, and Enter-opened chronological content previews. Modal plumbing —
|
|
6
|
+
* frame, navigation dispatch, preview layers, dismissal — is owned by the
|
|
7
|
+
* shared modal library; this tab owns only its content.
|
|
6
8
|
*/
|
|
7
9
|
import type { Theme, ThemeColor } from '@earendil-works/pi-coding-agent';
|
|
8
|
-
import {
|
|
10
|
+
import { matchesKey, visibleWidth, wrapTextWithAnsi } from '@earendil-works/pi-tui';
|
|
9
11
|
|
|
10
|
-
import type { ContextUsageSnapshot, UsageCategory, UsagePreviewEntry } from '../model';
|
|
11
|
-
import { collectPreviewEntries } from '../usage';
|
|
12
|
-
import { ListNavigator, normalizeInlineText, normalizePreviewText, PreviewScroller } from './injections-model';
|
|
13
12
|
import {
|
|
14
13
|
BODY_INDENT,
|
|
15
14
|
calculateViewport,
|
|
16
|
-
DEFAULT_TERMINAL_ROWS,
|
|
17
15
|
fitLine,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
type Hint,
|
|
17
|
+
ListNavigator,
|
|
18
|
+
type ModalTab,
|
|
19
|
+
type ModalTabContext,
|
|
20
|
+
type NavigationAction,
|
|
21
|
+
PreviewLayer,
|
|
22
|
+
RenderCache,
|
|
22
23
|
spreadLine,
|
|
23
24
|
wrapDescriptionLines,
|
|
24
|
-
} from '
|
|
25
|
-
import type {
|
|
25
|
+
} from '../../../libs/modal';
|
|
26
|
+
import type { ContextUsageSnapshot, UsageCategory, UsagePreviewEntry } from '../model';
|
|
27
|
+
import { collectPreviewEntries } from '../usage';
|
|
28
|
+
import { normalizeInlineText, normalizePreviewText } from './injections-model';
|
|
26
29
|
import { splitSkillPreview } from './skill-preview';
|
|
27
30
|
import { buildUsageMap, calculateFitMapScale, DEFAULT_MAP_COLUMNS, DEFAULT_MAP_ROWS, type UsageMapCell } from './usage-map';
|
|
28
31
|
|
|
@@ -33,10 +36,8 @@ const INVISIBLE_REASONING_DESCRIPTION =
|
|
|
33
36
|
'≈ is a provider-reported count; ~ is a rough approximation when no breakdown ' +
|
|
34
37
|
'is reported and excluded from category totals. ' +
|
|
35
38
|
'Encoded replaces Reasoning when the provider replays encrypted reasoning with its message.';
|
|
36
|
-
const USAGE_TAIL_FIXED_LINE_COUNT = 5;
|
|
37
|
-
const DETAIL_CATEGORY_HEADER_LINE_COUNT = 1;
|
|
38
|
-
const PREVIEW_FIXED_LINE_COUNT = 8;
|
|
39
39
|
const PREVIEW_ENTRY_MAX_LINES = 20;
|
|
40
|
+
const DETAIL_CATEGORY_HEADER_LINE_COUNT = 1;
|
|
40
41
|
const CURSOR_COLUMN_WIDTH = 2;
|
|
41
42
|
const MAX_LEGEND_VALUE_COLUMN = 32;
|
|
42
43
|
const LEGEND_VALUE_GAP = 2;
|
|
@@ -55,7 +56,6 @@ const BREAKDOWN_MARKER = '•';
|
|
|
55
56
|
/** Everything the Usage view renders, classified once when the view opens. */
|
|
56
57
|
export interface UsageViewInput {
|
|
57
58
|
readonly usage: ContextUsageSnapshot;
|
|
58
|
-
readonly degradedReason?: string;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/** View-local denominator selected for the context map. */
|
|
@@ -85,82 +85,52 @@ interface LegendColumns {
|
|
|
85
85
|
readonly tokenWidth: number;
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
/** Stateful Usage tab retained by
|
|
89
|
-
export class UsageView {
|
|
88
|
+
/** Stateful Usage tab retained by the Context View dialog. */
|
|
89
|
+
export class UsageView implements ModalTab {
|
|
90
90
|
private readonly theme: Theme;
|
|
91
|
-
private readonly input: UsageViewInput;
|
|
92
|
-
private readonly done: (result: undefined) => void;
|
|
93
|
-
private readonly getTerminalRows: () => number;
|
|
94
91
|
private readonly usage: ContextUsageSnapshot;
|
|
95
92
|
private readonly legendRows: readonly LegendRow[];
|
|
96
93
|
private readonly navigator: ListNavigator;
|
|
97
|
-
private readonly
|
|
94
|
+
private readonly cache = new RenderCache();
|
|
98
95
|
private readonly fitMapScale: number | undefined;
|
|
96
|
+
private context: ModalTabContext | undefined;
|
|
99
97
|
private mapScale: UsageMapScale = 'window';
|
|
100
98
|
private currentWidth: number | undefined;
|
|
101
|
-
private previewRow: CategoryLegendRow | undefined;
|
|
102
99
|
private cachedPreviewEntries: readonly UsagePreviewEntry[] | undefined;
|
|
103
100
|
private previewLines: string[] | undefined;
|
|
104
101
|
private previewWrapWidth: number | undefined;
|
|
105
|
-
private cachedWidth: number | undefined;
|
|
106
|
-
private cachedTerminalRows: number | undefined;
|
|
107
|
-
private cachedLines: string[] | undefined;
|
|
108
102
|
|
|
109
103
|
/** Create a view over one precomputed usage snapshot. */
|
|
110
|
-
public constructor(
|
|
111
|
-
theme: Theme,
|
|
112
|
-
input: UsageViewInput,
|
|
113
|
-
done: (result: undefined) => void,
|
|
114
|
-
getTerminalRows: () => number = () => process.stdout.rows ?? DEFAULT_TERMINAL_ROWS,
|
|
115
|
-
) {
|
|
104
|
+
public constructor(theme: Theme, input: UsageViewInput) {
|
|
116
105
|
this.theme = theme;
|
|
117
|
-
this.input = input;
|
|
118
|
-
this.done = done;
|
|
119
|
-
this.getTerminalRows = getTerminalRows;
|
|
120
106
|
this.usage = input.usage;
|
|
121
|
-
this.fitMapScale = calculateFitMapScale(this.usage);
|
|
122
107
|
this.legendRows = this.buildLegendRows();
|
|
123
|
-
// The trailing buffer/free block has no preview: it scrolls with the list but is never selectable.
|
|
124
108
|
const selectableCount = this.legendRows.filter(row => row.type === 'category').length;
|
|
125
109
|
this.navigator = new ListNavigator(this.legendRows.length, 1, selectableCount);
|
|
110
|
+
this.fitMapScale = calculateFitMapScale(this.usage);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public get label(): string {
|
|
114
|
+
return '[Usage]';
|
|
126
115
|
}
|
|
127
116
|
|
|
128
|
-
|
|
117
|
+
public attach(context: ModalTabContext): void {
|
|
118
|
+
this.context = context;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public hints(): Hint[] {
|
|
122
|
+
const hints: Hint[] = [['Enter', 'Preview']];
|
|
123
|
+
if (this.canToggleMapScale(this.currentWidth)) hints.push(['Z', 'Zoom']);
|
|
124
|
+
return hints;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Handle the view-local map-zoom key; navigation arrives as actions. */
|
|
129
128
|
public handleInput(data: string): void {
|
|
130
|
-
if (this.
|
|
131
|
-
this.handlePreviewInput(data);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (matchesKey(data, Key.escape) || data === 'q') {
|
|
135
|
-
this.done(undefined);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
if (matchesKey(data, 'z')) {
|
|
139
|
-
this.toggleMapScale();
|
|
140
|
-
} else if (matchesKey(data, Key.enter)) {
|
|
141
|
-
this.openPreview();
|
|
142
|
-
}
|
|
129
|
+
if (matchesKey(data, 'z')) this.toggleMapScale();
|
|
143
130
|
}
|
|
144
131
|
|
|
145
|
-
/** Apply semantic movement in
|
|
132
|
+
/** Apply semantic movement in the category list, or open a preview layer. */
|
|
146
133
|
public handleNavigation(action: NavigationAction): void {
|
|
147
|
-
if (this.previewRow !== undefined) {
|
|
148
|
-
const changed =
|
|
149
|
-
action === 'step-back'
|
|
150
|
-
? this.previewScroller.scrollBy(-1)
|
|
151
|
-
: action === 'step-forward'
|
|
152
|
-
? this.previewScroller.scrollBy(1)
|
|
153
|
-
: action === 'page-back'
|
|
154
|
-
? this.previewScroller.page(-1)
|
|
155
|
-
: action === 'page-forward'
|
|
156
|
-
? this.previewScroller.page(1)
|
|
157
|
-
: action === 'first'
|
|
158
|
-
? this.previewScroller.scrollTo(0)
|
|
159
|
-
: this.previewScroller.scrollTo(this.previewScroller.maxOffset);
|
|
160
|
-
if (changed) this.clearCache();
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
134
|
const changed =
|
|
165
135
|
action === 'step-back'
|
|
166
136
|
? this.navigator.moveBy(-1)
|
|
@@ -172,46 +142,34 @@ export class UsageView {
|
|
|
172
142
|
? this.navigator.page(1)
|
|
173
143
|
: action === 'first'
|
|
174
144
|
? this.navigator.moveTo(0)
|
|
175
|
-
:
|
|
176
|
-
|
|
145
|
+
: action === 'last'
|
|
146
|
+
? this.navigator.moveTo(this.legendRows.length - 1)
|
|
147
|
+
: false;
|
|
148
|
+
if (changed) this.cache.clear();
|
|
149
|
+
if (action === 'confirm') this.openPreview();
|
|
177
150
|
}
|
|
178
151
|
|
|
179
|
-
/** Render a cached tab frame for the current width and
|
|
180
|
-
public render(width: number): string[] {
|
|
152
|
+
/** Render a cached tab frame for the current width and content height. */
|
|
153
|
+
public render(width: number, height: number | undefined): string[] {
|
|
181
154
|
this.currentWidth = width;
|
|
182
|
-
const
|
|
183
|
-
if (
|
|
184
|
-
return this.cachedLines;
|
|
185
|
-
}
|
|
155
|
+
const cached = this.cache.read(width, height);
|
|
156
|
+
if (cached !== undefined) return cached;
|
|
186
157
|
|
|
187
|
-
const
|
|
188
|
-
|
|
189
|
-
this.
|
|
190
|
-
|
|
191
|
-
this.
|
|
192
|
-
|
|
158
|
+
const terminalRows = height ?? 20;
|
|
159
|
+
const header = this.headerLines(width);
|
|
160
|
+
const description = wrapDescriptionLines(this.theme, USAGE_DESCRIPTION, 'dim', width);
|
|
161
|
+
const availableDashboardRows = Math.max(1, terminalRows - header.length - description.length - 2);
|
|
162
|
+
const dashboard = this.dashboardLines(width, availableDashboardRows).slice(0, availableDashboardRows);
|
|
163
|
+
while (dashboard.length < availableDashboardRows) dashboard.push('');
|
|
164
|
+
|
|
165
|
+
return this.cache.write(width, height, [...header, '', ...dashboard, '', ...description]);
|
|
193
166
|
}
|
|
194
167
|
|
|
195
168
|
/** Invalidate theme-dependent rendered output. */
|
|
196
169
|
public invalidate(): void {
|
|
197
170
|
this.previewLines = undefined;
|
|
198
171
|
this.previewWrapWidth = undefined;
|
|
199
|
-
this.
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// === Dashboard mode ===
|
|
203
|
-
|
|
204
|
-
/** Full map/legend frame with navigation hints. */
|
|
205
|
-
private renderDashboard(width: number, terminalRows: number): string[] {
|
|
206
|
-
const theme = this.theme;
|
|
207
|
-
const border = theme.fg('border', '─'.repeat(Math.max(1, width)));
|
|
208
|
-
const prefix = [border, '', ...this.headerLines(width), '', ...this.degradedWarningLines(width)];
|
|
209
|
-
const descriptionLines = wrapDescriptionLines(theme, USAGE_DESCRIPTION, 'dim', width);
|
|
210
|
-
const availableDashboardRows = Math.max(1, terminalRows - prefix.length - USAGE_TAIL_FIXED_LINE_COUNT - descriptionLines.length);
|
|
211
|
-
const dashboard = this.dashboardLines(width, availableDashboardRows).slice(0, availableDashboardRows);
|
|
212
|
-
while (dashboard.length < availableDashboardRows) dashboard.push('');
|
|
213
|
-
const tail = ['', ...descriptionLines, '', this.fit(hintRow(theme, this.dashboardHints(width)), width), '', border];
|
|
214
|
-
return fitToTerminalHeight([...prefix, ...dashboard, ...tail], terminalRows, border);
|
|
172
|
+
this.cache.clear();
|
|
215
173
|
}
|
|
216
174
|
|
|
217
175
|
/** Accent title with responsive model, zoom label, and true-window usage metadata. */
|
|
@@ -254,7 +212,7 @@ export class UsageView {
|
|
|
254
212
|
private toggleMapScale(): void {
|
|
255
213
|
if (!this.canToggleMapScale(this.currentWidth)) return;
|
|
256
214
|
this.mapScale = this.mapScale === 'window' ? 'fit' : 'window';
|
|
257
|
-
this.
|
|
215
|
+
this.cache.clear();
|
|
258
216
|
}
|
|
259
217
|
|
|
260
218
|
/** Whether zoom can help and its binding is visible at this width. */
|
|
@@ -276,19 +234,6 @@ export class UsageView {
|
|
|
276
234
|
return this.theme.fg('mdHeading', `Zoom ${formatTokens(contextWindow)} → ${formatTokens(this.fitMapScale)}`);
|
|
277
235
|
}
|
|
278
236
|
|
|
279
|
-
/** Dashboard hints with Zoom immediately before Close when the binding is active. */
|
|
280
|
-
private dashboardHints(width: number): Array<readonly [string, string]> {
|
|
281
|
-
const hints: Array<readonly [string, string]> = [
|
|
282
|
-
[STEP_KEY_HINT, 'Navigate'],
|
|
283
|
-
['Ctrl+u/d', 'Page'],
|
|
284
|
-
['gg/G', 'Bounds'],
|
|
285
|
-
['Enter', 'Preview'],
|
|
286
|
-
];
|
|
287
|
-
if (this.canToggleMapScale(width)) hints.push(['Z', 'Zoom']);
|
|
288
|
-
hints.push(['Esc', 'Close']);
|
|
289
|
-
return hints;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
237
|
/** Render the map and legend side by side, or only details when width/window data is insufficient. */
|
|
293
238
|
private dashboardLines(width: number, rows: number): string[] {
|
|
294
239
|
const scaleTokens = this.mapScale === 'fit' ? this.fitMapScale : undefined;
|
|
@@ -466,84 +411,25 @@ export class UsageView {
|
|
|
466
411
|
return this.theme.fg(categoryColor(cell.categoryId), glyph);
|
|
467
412
|
}
|
|
468
413
|
|
|
469
|
-
|
|
470
|
-
private degradedWarningLines(width: number): string[] {
|
|
471
|
-
if (this.input.degradedReason === undefined) return [];
|
|
472
|
-
const reason = normalizeInlineText(this.input.degradedReason);
|
|
473
|
-
return wrapTextWithAnsi(this.theme.fg('warning', `${BODY_INDENT}${reason}`), width);
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
// === Preview mode ===
|
|
477
|
-
|
|
478
|
-
/** Preview scrolling and return-to-list keys. */
|
|
479
|
-
private handlePreviewInput(data: string): void {
|
|
480
|
-
if (matchesKey(data, Key.escape) || data === 'q') {
|
|
481
|
-
this.closePreview();
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
}
|
|
414
|
+
// === Preview ===
|
|
485
415
|
|
|
486
|
-
/** Open the selected category's content preview
|
|
416
|
+
/** Open the selected category's content preview as a shell-managed layer. */
|
|
487
417
|
private openPreview(): void {
|
|
488
418
|
const row = this.legendRows[this.navigator.selected];
|
|
489
419
|
if (row === undefined || row.type !== 'category') return;
|
|
490
|
-
this.previewRow = row;
|
|
491
420
|
this.cachedPreviewEntries = undefined;
|
|
492
421
|
this.previewLines = undefined;
|
|
493
422
|
this.previewWrapWidth = undefined;
|
|
494
|
-
this.previewScroller.reset();
|
|
495
|
-
this.clearCache();
|
|
496
|
-
}
|
|
497
423
|
|
|
498
|
-
/** Return to the list with the same selected row. */
|
|
499
|
-
private closePreview(): void {
|
|
500
|
-
this.previewRow = undefined;
|
|
501
|
-
this.cachedPreviewEntries = undefined;
|
|
502
|
-
this.previewLines = undefined;
|
|
503
|
-
this.previewWrapWidth = undefined;
|
|
504
|
-
this.clearCache();
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
/** Scrollable chronological content stream for one category. */
|
|
508
|
-
private renderPreview(width: number, terminalRows: number, row: CategoryLegendRow): string[] {
|
|
509
|
-
const theme = this.theme;
|
|
510
|
-
const border = theme.fg('border', '─'.repeat(Math.max(1, width)));
|
|
511
|
-
const body = this.previewBodyLines(width, row);
|
|
512
|
-
const descriptionLines = this.previewDescriptionLines(width, row);
|
|
513
|
-
const descriptionLineCount = descriptionLines.length === 0 ? 0 : descriptionLines.length + 1;
|
|
514
|
-
const viewport = calculateViewport(body.length, terminalRows, PREVIEW_FIXED_LINE_COUNT, descriptionLineCount);
|
|
515
|
-
this.previewScroller.setExtent(body.length, viewport.visibleCount);
|
|
516
|
-
|
|
517
|
-
const lines: string[] = [border, ''];
|
|
518
|
-
const title = theme.fg('accent', theme.bold(normalizeInlineText(row.category.label)));
|
|
519
424
|
const percent = this.plainLegendPercent(row.category.tokens);
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (viewport.showScroll) {
|
|
530
|
-
lines.push(this.fit(theme.fg('dim', `${BODY_INDENT}(${this.previewScroller.visibleEnd}/${body.length})`), width));
|
|
531
|
-
}
|
|
532
|
-
if (descriptionLines.length > 0) lines.push('', ...descriptionLines);
|
|
533
|
-
lines.push('');
|
|
534
|
-
lines.push(
|
|
535
|
-
this.fit(
|
|
536
|
-
hintRow(theme, [
|
|
537
|
-
[STEP_KEY_HINT, 'Scroll'],
|
|
538
|
-
['Ctrl+u/d', 'Page'],
|
|
539
|
-
['gg/G', 'Bounds'],
|
|
540
|
-
['Esc', 'Back'],
|
|
541
|
-
]),
|
|
542
|
-
width,
|
|
543
|
-
),
|
|
425
|
+
this.context?.pushLayer(
|
|
426
|
+
new PreviewLayer(this.theme, {
|
|
427
|
+
title: () => this.theme.fg('accent', this.theme.bold(normalizeInlineText(row.category.label))),
|
|
428
|
+
meta: () => this.theme.fg('muted', `${formatTokens(row.category.tokens)}${percent === '' ? '' : ` · ${percent}`}`),
|
|
429
|
+
body: width => this.previewBodyLines(width, row),
|
|
430
|
+
description: width => this.previewDescriptionLines(width, row),
|
|
431
|
+
}),
|
|
544
432
|
);
|
|
545
|
-
lines.push('', border);
|
|
546
|
-
return fitToTerminalHeight(lines, terminalRows, border);
|
|
547
433
|
}
|
|
548
434
|
|
|
549
435
|
/** Cached wrapped entry stream: bracket headers plus capped sanitized content. */
|
|
@@ -638,13 +524,6 @@ export class UsageView {
|
|
|
638
524
|
private fit(line: string, width: number): string {
|
|
639
525
|
return fitLine(line, width);
|
|
640
526
|
}
|
|
641
|
-
|
|
642
|
-
/** Clear render-cache keys after data, theme, or input changes. */
|
|
643
|
-
private clearCache(): void {
|
|
644
|
-
this.cachedWidth = undefined;
|
|
645
|
-
this.cachedTerminalRows = undefined;
|
|
646
|
-
this.cachedLines = undefined;
|
|
647
|
-
}
|
|
648
527
|
}
|
|
649
528
|
|
|
650
529
|
/** Show top-level categories plus Tool Output's direct per-tool breakdown. */
|
|
@@ -6,17 +6,18 @@ This extension implements the `/select-model` command and its interactive picker
|
|
|
6
6
|
|
|
7
7
|
## Design Patterns
|
|
8
8
|
|
|
9
|
-
- **Lazy command activation**: `registerModelSelect` waits for the first `session_start` event before calling `activateModelSelect`, ensuring the extension is wired up once a session exists.
|
|
9
|
+
- **Lazy command activation**: `registerModelSelect` waits for the first `session_start` event before calling `activateModelSelect`, ensuring the extension is wired up once a session exists and only when `model_select` is enabled.
|
|
10
10
|
- **Command + event-bus controller**: `activateModelSelect` registers the `/select-model` command and also listens on a cross-extension event ID so external extensions can open the picker without invoking the command directly.
|
|
11
11
|
- **Context caching for async triggers**: The most recent `ExtensionContext` is stored (`latestCtx`) so the event-bus handler (which receives no context argument) can still open the dialog.
|
|
12
12
|
- **Separation of concerns**:
|
|
13
|
-
- `index.ts`: orchestrates activation, command handling, applying the selected model, and bridging registry errors into the dialog.
|
|
13
|
+
- `index.ts`: orchestrates activation, command handling, applying the selected model, setting default reasoning, and bridging registry errors into the dialog.
|
|
14
14
|
- `model-lists.ts`: queries the registry and prepares favourite, grouped favourite, and searchable model lists; validates favourite entries against the registry and configured auth.
|
|
15
|
-
- `model-formatter.ts`: pure static utilities for labels, descriptions, sorting, token formatting, and search-text generation.
|
|
16
|
-
- `model-select-dialog.ts`:
|
|
15
|
+
- `model-formatter.ts`: pure static utilities for labels, descriptions, sorting, token formatting, capability detection, and search-text generation.
|
|
16
|
+
- `model-select-dialog.ts`: thin `ModalDialog` (shared modal library) configuration: one `ListTab` per section (permanent Favourites, groups, Search), a shared filter input, notices for config warnings, and per-section row/empty-state/footer hooks.
|
|
17
17
|
- `constants.ts`: command name, cross-extension event ID, and dialog rendering limits.
|
|
18
|
-
-
|
|
19
|
-
- **
|
|
18
|
+
- `types.ts`: shared item, list, tab, and dialog option types.
|
|
19
|
+
- **Modal library delegation**: `ModelSelectDialog` implements `Component` and `Focusable` by delegating to a `ModalDialog` from `src/libs/modal/`; the shell owns the tab strip, tab cycling, keybinding-driven navigation (wrap-around selection via `ListTab`), the shared filter `Input`, and the help footer.
|
|
20
|
+
- **Layout-aware rendering**: the dialog uses the shell's `inline` frame or `bordered` frame (centered by host overlay options) depending on the `model_select.layout` config value.
|
|
20
21
|
- **Defensive guards**: checks feature enablement, UI availability, idle state (`waitForIdle`), exact-match short-circuit, and auth availability before applying a model.
|
|
21
22
|
|
|
22
23
|
## Data & Control Flow
|
|
@@ -27,21 +28,22 @@ This extension implements the `/select-model` command and its interactive picker
|
|
|
27
28
|
|
|
28
29
|
2. **Command invocation**
|
|
29
30
|
- The command handler stores the command context as `latestCtx`.
|
|
30
|
-
- It aborts if `model_select` is disabled.
|
|
31
|
+
- It aborts with a warning if `model_select` is disabled.
|
|
31
32
|
- It calls `showModelSelector` with the raw argument string.
|
|
32
33
|
|
|
33
34
|
3. **Selection short-circuit**
|
|
34
35
|
- `showModelSelector` waits for idle if available, then refreshes the model registry.
|
|
35
|
-
- `findExactModel` tries two forms: `provider/modelId` and whitespace-separated `provider modelId`. If a matching model is found, it is applied immediately and the dialog is skipped.
|
|
36
|
+
- `findExactModel` tries two forms: `provider/modelId` and whitespace-separated `provider modelId`. If a matching model is found, it is applied immediately via `applySelectedModel` and the dialog is skipped.
|
|
36
37
|
- If no UI is available and there is no exact match, a warning is shown and the picker exits.
|
|
37
38
|
|
|
38
39
|
4. **Dialog path**
|
|
39
40
|
- If no exact match is found and a UI is present, the extension loads the `model_select` config.
|
|
40
|
-
- `buildModelLists` refreshes the registry, applies `provider_filter` only to Search, sorts search items with the current model first, and validates configured favourites
|
|
41
|
-
-
|
|
41
|
+
- `buildModelLists` refreshes the registry, applies `provider_filter` only to the Search tab, sorts search items with the current model first, and validates configured favourites.
|
|
42
|
+
- Favourites must exist in the registry and have configured auth; missing or unauthenticated entries are collected as warnings and omitted from the list.
|
|
43
|
+
- Accepted favourites retain exact group memberships; the ordered `groups` config defines the visible group tabs. Duplicate favourite models are deduplicated by provider/id, and duplicate group names are collapsed while preserving configured order.
|
|
42
44
|
- Any model-registry error is added to the dialog's `configWarnings` so it appears inside the picker.
|
|
43
|
-
- `ctx.ui.custom` creates a `ModelSelectDialog` with favourite/group/search lists, tab visibility controls, the current model, warnings, `initialSearch`, `layout`, and an `onDone` callback. The overlay layout is centered at 85% width with a one-cell margin.
|
|
44
|
-
- The dialog always creates Favourites
|
|
45
|
+
- `ctx.ui.custom` creates a `ModelSelectDialog` with favourite/group/search lists, tab visibility controls, the current model, warnings, `initialSearch`, `layout`, `defaultReasoning`, and an `onDone` callback. The overlay layout is centered at 85% width with a one-cell margin.
|
|
46
|
+
- The dialog always creates a Favourites tab, adds visible group tabs and optional Search, and renders a width-aware tab strip, shared filter input, warnings, and help footer. If `initialSearch` is non-empty and Search is visible, the dialog starts on the Search tab and seeds the input.
|
|
45
47
|
|
|
46
48
|
5. **User interaction**
|
|
47
49
|
- Keyboard input is mapped through `keybindings.matches` for navigation, ordered tab cycling (`tab`/`shift+tab`), page up/down, confirmation, and cancellation.
|
|
@@ -51,15 +53,30 @@ This extension implements the `/select-model` command and its interactive picker
|
|
|
51
53
|
|
|
52
54
|
6. **Apply result**
|
|
53
55
|
- If a model is selected, `applySelectedModel` calls `pi.setModel`.
|
|
56
|
+
- On success, if `model_select.default_reasoning` is configured and the chosen model supports that level, `pi.setThinkingLevel` is called to apply it.
|
|
54
57
|
- A success or missing-auth notification is shown via `ctx.ui.notify`.
|
|
55
58
|
|
|
59
|
+
## Configuration & Schema
|
|
60
|
+
|
|
61
|
+
The `model_select` configuration is defined by `../../schemas/model-select.config.schema` and included in the top-level `ConfigSchema` in `../../schemas/config.schema`.
|
|
62
|
+
|
|
63
|
+
- `enabled` (`boolean`, default `false`): feature toggle.
|
|
64
|
+
- `favourite` (`array`): each entry is `{ provider, modelId, groups?: string[] }`. Favourites are validated against the registry and auth state.
|
|
65
|
+
- `favourite_label` (`string`, default `'Favourites'`): label for the Favourites tab.
|
|
66
|
+
- `groups` (`array` of non-empty strings): ordered group names used as tabs; favourites can be assigned to one or more groups.
|
|
67
|
+
- `hide_tabs` (`{ groups: boolean, search: boolean }`, default both `false`): controls visibility of group tabs and the Search tab.
|
|
68
|
+
- `provider_filter` (`array` of non-empty strings): restricts the Search tab to the listed providers; Favourites and group tabs are not filtered.
|
|
69
|
+
- `default_reasoning` (optional `'off' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh'`): applied to the selected model if `getSupportedThinkingLevels` includes it.
|
|
70
|
+
- `layout` (`'inline' | 'overlay'`, default `'inline'`): picker layout.
|
|
71
|
+
|
|
56
72
|
## Integration Points
|
|
57
73
|
|
|
58
|
-
- **`ExtensionAPI` (`@earendil-works/pi-coding-agent`)**: used to register the command, listen to `session_start`, set the active model (`pi.setModel`), and access the shared event bus (`pi.events`).
|
|
59
|
-
- **`ExtensionContext` / `ExtensionCommandContext` (`@earendil-works/pi-coding-agent`)**: provides `modelRegistry`, `ui`, `hasUI`, `model`, and the optional `waitForIdle` guard.
|
|
74
|
+
- **`ExtensionAPI` (`@earendil-works/pi-coding-agent`)**: used to register the command, listen to `session_start`, set the active model (`pi.setModel`), set the thinking level (`pi.setThinkingLevel`), and access the shared event bus (`pi.events`).
|
|
75
|
+
- **`ExtensionContext` / `ExtensionCommandContext` (`@earendil-works/pi-coding-agent`)**: provides `modelRegistry`, `ui`, `hasUI`, `model`, `cwd`, and the optional `waitForIdle` guard.
|
|
60
76
|
- **`ModelRegistry`**: refreshed and queried for available models, configured auth, lookup by `provider` + `modelId`, and error state (`getError`).
|
|
61
|
-
- **`ConfigLoader` (`../../config-loader`)**: provides feature toggle state (`isEnabled('model_select')`)
|
|
62
|
-
- **`@earendil-works/pi-ai`**: supplies the `Model<Api>` type
|
|
77
|
+
- **`ConfigLoader` (`../../config-loader`)**: provides feature toggle state (`isEnabled('model_select')`), the typed `model_select` configuration object, and merges global + project JSON config.
|
|
78
|
+
- **`@earendil-works/pi-ai`**: supplies the `Model<Api>` type, `modelsAreEqual` for stable identity comparisons, and `getSupportedThinkingLevels` for reasoning-level validation.
|
|
63
79
|
- **`@earendil-works/pi-tui`**: supplies the `Input` component, `fuzzyFilter`, `matchesKey`, theme helpers, and rendering utilities (`truncateToWidth`, `visibleWidth`) used by the dialog.
|
|
64
80
|
- **`pi-vim-keys` / other extensions**: can trigger the picker by emitting `PI_VIM_KEY_EVENT_ID` on the shared event bus.
|
|
65
|
-
-
|
|
81
|
+
- **`../../constants`**: provides the `piVimKeyEventId` helper used to derive the cross-extension event ID.
|
|
82
|
+
- **Configuration schemas**: `model_select` shape, `ModelSelectLayout`, `HideTabs`, and `ReasoningLevel` are defined by `../../schemas/model-select.config.schema`, `../../schemas/config.schema`, and `../../schemas/shared-config.schema`.
|