pi-better-model-select 0.2.0 → 0.3.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/CHANGELOG.md +63 -0
- package/README.md +17 -7
- package/docs/configuration.md +8 -2
- package/docs/demo/showcase.gif +0 -0
- package/extensions/bms.ts +62 -50
- package/extensions/lib/defaults.ts +146 -52
- package/extensions/lib/effort-editor.ts +35 -20
- package/extensions/lib/flows.ts +126 -496
- package/extensions/lib/picker-dialog.ts +11 -19
- package/extensions/lib/picker.ts +90 -162
- package/extensions/lib/store.ts +3 -0
- package/extensions/lib/tui.ts +320 -0
- package/package.json +3 -1
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Input, fuzzyFilter, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi,
|
|
3
|
+
type Component, type Focusable,
|
|
4
|
+
} from "@earendil-works/pi-tui";
|
|
5
|
+
|
|
6
|
+
import { loadSettings } from "./store.ts";
|
|
7
|
+
|
|
8
|
+
export type Placement = "above" | "below";
|
|
9
|
+
|
|
10
|
+
// Also works with older settings files while the store migration is rolled out.
|
|
11
|
+
export function resultPlacement(): Placement {
|
|
12
|
+
return (loadSettings() as { resultPlacement?: Placement }).resultPlacement ?? "below";
|
|
13
|
+
}
|
|
14
|
+
export interface TableColumn { key: string; label: string; width?: number }
|
|
15
|
+
export interface TableRow {
|
|
16
|
+
value: string;
|
|
17
|
+
label: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
cells?: Record<string, string>;
|
|
20
|
+
}
|
|
21
|
+
export interface TableSelectOptions {
|
|
22
|
+
title: string;
|
|
23
|
+
columns?: TableColumn[];
|
|
24
|
+
rows: TableRow[];
|
|
25
|
+
hint?: string;
|
|
26
|
+
search?: boolean;
|
|
27
|
+
placement?: Placement;
|
|
28
|
+
initialValue?: string;
|
|
29
|
+
state?: SelectionState;
|
|
30
|
+
}
|
|
31
|
+
export interface SelectionState { query?: string; selectedValue?: string }
|
|
32
|
+
|
|
33
|
+
/** Distinguish keys from their actions; omit whole hints rather than cut a key. */
|
|
34
|
+
export function shortcutLine(theme: any, hint: string, width: number): string {
|
|
35
|
+
const chunks = hint.split(/\s*·\s*/);
|
|
36
|
+
const rendered: string[] = [];
|
|
37
|
+
let used = 0;
|
|
38
|
+
for (const chunk of chunks) {
|
|
39
|
+
const match = chunk.match(/^(↑↓|enter|esc|space|ctrl\+[a-z])\s+(.*)$/i);
|
|
40
|
+
const key = match?.[1].replace(/^enter$/i, "Enter").replace(/^esc$/i, "Esc").replace(/^space$/i, "Space").replace(/^ctrl\+([a-z])$/i, (_, letter: string) => `Ctrl+${letter.toUpperCase()}`);
|
|
41
|
+
const plain = match ? `[${key}] ${match[2]}` : chunk;
|
|
42
|
+
if (used + plain.length + (rendered.length ? 3 : 0) > width) break;
|
|
43
|
+
rendered.push(match ? color(theme, "accent", `[${key}]`) + " " + color(theme, "muted", match[2]) : color(theme, "muted", chunk));
|
|
44
|
+
used += plain.length + (rendered.length > 1 ? 3 : 0);
|
|
45
|
+
}
|
|
46
|
+
return rendered.join(color(theme, "dim", " "));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function color(theme: any, role: string, text: string): string {
|
|
50
|
+
return typeof theme?.fg === "function" ? theme.fg(role, text) : text;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Keep each cell on one terminal line. Widths are terminal cells, not JS length.
|
|
54
|
+
function singleLine(text: string): string { return text.replace(/[\r\n\t]/g, " "); }
|
|
55
|
+
export function fitCell(text: string, width: number): string {
|
|
56
|
+
const clipped = truncateToWidth(singleLine(text), Math.max(0, width), "…");
|
|
57
|
+
return clipped + " ".repeat(Math.max(0, width - visibleWidth(clipped)));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A prompt-area dialog. Pi focuses this object, which forwards focus to Input. */
|
|
61
|
+
export class TableSelect implements Component, Focusable {
|
|
62
|
+
protected input: Input;
|
|
63
|
+
protected options: TableSelectOptions;
|
|
64
|
+
protected theme: any;
|
|
65
|
+
protected changed: () => void;
|
|
66
|
+
protected done: (value: string | undefined) => void;
|
|
67
|
+
protected filtered: TableRow[] = [];
|
|
68
|
+
protected selected = 0;
|
|
69
|
+
private start = 0;
|
|
70
|
+
private _focused = false;
|
|
71
|
+
get focused(): boolean { return this._focused; }
|
|
72
|
+
set focused(value: boolean) {
|
|
73
|
+
this._focused = value;
|
|
74
|
+
this.input.focused = value && this.options.search !== false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
constructor(options: TableSelectOptions, theme: any, changed: () => void,
|
|
78
|
+
done: (value: string | undefined) => void, state: SelectionState = {}) {
|
|
79
|
+
this.options = options;
|
|
80
|
+
this.theme = theme;
|
|
81
|
+
this.changed = changed;
|
|
82
|
+
this.done = done;
|
|
83
|
+
this.input = new Input({ prompt: "> ", placeholder: "Search…",
|
|
84
|
+
placeholderStyle: text => color(this.theme, "dim", text) });
|
|
85
|
+
this.input.setValue(state.query ?? "");
|
|
86
|
+
this.filter(state.selectedValue ?? options.initialValue);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get state(): SelectionState {
|
|
90
|
+
return { query: this.input.getValue(), selectedValue: this.currentItem()?.value };
|
|
91
|
+
}
|
|
92
|
+
currentItem(): TableRow | undefined { return this.filtered[this.selected]; }
|
|
93
|
+
private filter(preferred?: string): void {
|
|
94
|
+
const query = this.input.getValue().trim();
|
|
95
|
+
this.filtered = query ? fuzzyFilter(this.options.rows, query,
|
|
96
|
+
row => `${row.label} ${row.value} ${row.description ?? ""} ${Object.values(row.cells ?? {}).join(" ")}`)
|
|
97
|
+
: this.options.rows;
|
|
98
|
+
const index = this.filtered.findIndex(row => row.value === preferred);
|
|
99
|
+
this.selected = index < 0 ? 0 : index;
|
|
100
|
+
this.start = 0;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
handleInput(data: string): void {
|
|
104
|
+
if (matchesKey(data, "escape")) { this.done(undefined); return; }
|
|
105
|
+
if (matchesKey(data, "enter")) {
|
|
106
|
+
const item = this.currentItem();
|
|
107
|
+
if (item) this.done(item.value);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const up = matchesKey(data, "up") || matchesKey(data, "pageUp");
|
|
111
|
+
const down = matchesKey(data, "down") || matchesKey(data, "pageDown");
|
|
112
|
+
if (up || down) {
|
|
113
|
+
const direction = (up ? -1 : 1) * (this.options.search !== false && this.options.placement === "above" ? -1 : 1);
|
|
114
|
+
const step = matchesKey(data, "pageUp") || matchesKey(data, "pageDown") ? 6 : 1;
|
|
115
|
+
this.selected = Math.max(0, Math.min(this.filtered.length - 1, this.selected + direction * step));
|
|
116
|
+
} else if (this.options.search !== false) {
|
|
117
|
+
const before = this.input.getValue();
|
|
118
|
+
const selectedValue = this.currentItem()?.value;
|
|
119
|
+
this.input.handleInput(data);
|
|
120
|
+
if (this.input.getValue() !== before) this.filter(selectedValue);
|
|
121
|
+
}
|
|
122
|
+
this.changed();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
protected tableLines(width: number): string[] {
|
|
126
|
+
const columns = this.options.columns ?? [
|
|
127
|
+
{ key: "label", label: "", width: 28 }, { key: "description", label: "" },
|
|
128
|
+
];
|
|
129
|
+
const cell = (row: TableRow, key: string) => row.cells?.[key]
|
|
130
|
+
?? (key === "label" ? row.label : key === "description" ? row.description ?? "" : "");
|
|
131
|
+
// Reserve natural widths for metadata, give remaining room to the main label.
|
|
132
|
+
const sizes = columns.map(column => Math.max(1, column.width ?? Math.max(
|
|
133
|
+
visibleWidth(column.label), ...this.options.rows.map(row => visibleWidth(singleLine(cell(row, column.key)))))));
|
|
134
|
+
const available = Math.max(0, width - 2 - Math.max(0, columns.length - 1) * 2);
|
|
135
|
+
if (sizes.length) sizes[0] = Math.max(1, Math.min(24, Math.floor(width * 0.4)),
|
|
136
|
+
available - sizes.slice(1).reduce((a, b) => a + b, 0));
|
|
137
|
+
const line = (values: string[]) => values.map((value, i) => fitCell(value, sizes[i]!)).join(" ");
|
|
138
|
+
const header = columns.some(column => column.label)
|
|
139
|
+
? [color(this.theme, "muted", truncateToWidth(" " + line(columns.map(column => column.label)), width, ""))] : [];
|
|
140
|
+
if (!this.filtered.length) return [...header, color(this.theme, "warning", truncateToWidth(" No matches", width, ""))];
|
|
141
|
+
this.start = Math.min(this.start, Math.max(0, this.filtered.length - 6));
|
|
142
|
+
if (this.selected < this.start) this.start = this.selected;
|
|
143
|
+
if (this.selected >= this.start + 6) this.start = this.selected - 5;
|
|
144
|
+
let indices = Array.from({ length: Math.min(6, this.filtered.length - this.start) }, (_, i) => this.start + i);
|
|
145
|
+
if (this.options.search !== false && this.options.placement === "above") indices = indices.reverse();
|
|
146
|
+
return [...header, ...indices.map(index => {
|
|
147
|
+
const row = this.filtered[index]!;
|
|
148
|
+
const text = fitCell((index === this.selected ? "› " : " ") + line(columns.map(column => cell(row, column.key))), width);
|
|
149
|
+
if (index !== this.selected) return text;
|
|
150
|
+
const selected = color(this.theme, "accent", text);
|
|
151
|
+
return typeof this.theme?.bg === "function" ? this.theme.bg("selectedBg", selected) : selected;
|
|
152
|
+
})];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
render(width: number): string[] {
|
|
156
|
+
width = Math.max(1, width);
|
|
157
|
+
const border = color(this.theme, "borderAccent", "─".repeat(width));
|
|
158
|
+
const prompt = this.options.search === false ? [] : [border, ...this.input.render(width), border];
|
|
159
|
+
const results = this.tableLines(width);
|
|
160
|
+
const count = this.filtered.length === this.options.rows.length
|
|
161
|
+
? `${this.filtered.length} results` : `${this.filtered.length} of ${this.options.rows.length} results`;
|
|
162
|
+
const summary = color(this.theme, "dim", truncateToWidth(count, width, ""));
|
|
163
|
+
const shortcuts = shortcutLine(this.theme, this.options.hint ?? "↑↓ move · enter select · esc back", width);
|
|
164
|
+
return [color(this.theme, "accent", truncateToWidth(singleLine(this.options.title), width, "…")),
|
|
165
|
+
...(this.options.placement === "above" && prompt.length
|
|
166
|
+
? [shortcuts, summary, ...results, ...prompt]
|
|
167
|
+
: [...prompt, ...results, summary, shortcuts])];
|
|
168
|
+
}
|
|
169
|
+
invalidate(): void { this.input.invalidate(); }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export async function tableSelect(ctx: any, options: TableSelectOptions): Promise<string | undefined> {
|
|
173
|
+
return ctx.ui.custom((tui: any, theme: any, _kb: any, done: any) => {
|
|
174
|
+
const table = new TableSelect({ ...options, placement: options.placement ?? resultPlacement() }, theme, () => tui.requestRender(), value => {
|
|
175
|
+
if (options.state) Object.assign(options.state, table.state);
|
|
176
|
+
done(value);
|
|
177
|
+
}, options.state);
|
|
178
|
+
return table;
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface ConfirmActionOptions { title: string; lines: string[]; confirmLabel: string }
|
|
183
|
+
export class ConfirmAction extends TableSelect {
|
|
184
|
+
private lines: string[];
|
|
185
|
+
constructor(options: ConfirmActionOptions, theme: any, changed: () => void, done: (confirmed: boolean) => void) {
|
|
186
|
+
super({ title: options.title, search: false,
|
|
187
|
+
rows: [{ value: "cancel", label: "Cancel" }, { value: "confirm", label: options.confirmLabel }],
|
|
188
|
+
}, theme, changed, value => done(value === "confirm"));
|
|
189
|
+
this.lines = options.lines;
|
|
190
|
+
}
|
|
191
|
+
override render(width: number): string[] {
|
|
192
|
+
const [title, ...choices] = super.render(width);
|
|
193
|
+
return [title!, ...this.lines.flatMap(line => wrapTextWithAnsi(line, Math.max(1, width))), ...choices];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
export async function confirmAction(ctx: any, options: ConfirmActionOptions): Promise<boolean> {
|
|
197
|
+
return (await ctx.ui.custom((tui: any, theme: any, _kb: any, done: any) =>
|
|
198
|
+
new ConfirmAction(options, theme, () => tui.requestRender(), done))) === true;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface FormField {
|
|
202
|
+
key: string; label: string; value: string; description?: string;
|
|
203
|
+
edit: () => Promise<string | undefined>;
|
|
204
|
+
}
|
|
205
|
+
export interface EditFormOptions {
|
|
206
|
+
title: string; fields: FormField[]; onSave: () => Promise<boolean | void>; hint?: string;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Field callbacks edit the caller's draft. Only onSave may persist it. */
|
|
210
|
+
export class Form implements Component {
|
|
211
|
+
private options: EditFormOptions;
|
|
212
|
+
private theme: any;
|
|
213
|
+
private changed: () => void;
|
|
214
|
+
private done: (saved: boolean) => void;
|
|
215
|
+
private fields: FormField[];
|
|
216
|
+
private originals: string[];
|
|
217
|
+
private selected = 0;
|
|
218
|
+
private busy = false;
|
|
219
|
+
private discard = false;
|
|
220
|
+
private discardSelected = false;
|
|
221
|
+
private error = "";
|
|
222
|
+
private editField: (field: FormField) => Promise<string | undefined>;
|
|
223
|
+
private save: () => Promise<boolean | void>;
|
|
224
|
+
constructor(options: EditFormOptions, theme: any, changed: () => void, done: (saved: boolean) => void,
|
|
225
|
+
editField: (field: FormField) => Promise<string | undefined> = field => field.edit(),
|
|
226
|
+
save: () => Promise<boolean | void> = options.onSave) {
|
|
227
|
+
this.editField = editField;
|
|
228
|
+
this.save = save;
|
|
229
|
+
this.options = options;
|
|
230
|
+
this.theme = theme;
|
|
231
|
+
this.changed = changed;
|
|
232
|
+
this.done = done;
|
|
233
|
+
this.fields = options.fields.map(field => ({ ...field }));
|
|
234
|
+
this.originals = this.fields.map(field => field.value);
|
|
235
|
+
}
|
|
236
|
+
private get dirty(): boolean { return this.fields.some((field, i) => field.value !== this.originals[i]); }
|
|
237
|
+
private async run(action: () => Promise<void>): Promise<void> {
|
|
238
|
+
this.busy = true;
|
|
239
|
+
this.error = "";
|
|
240
|
+
this.changed();
|
|
241
|
+
try { await action(); } catch (error) { this.error = error instanceof Error ? error.message : String(error); }
|
|
242
|
+
finally { this.busy = false; this.changed(); }
|
|
243
|
+
}
|
|
244
|
+
handleInput(data: string): void {
|
|
245
|
+
if (this.busy) return;
|
|
246
|
+
if (this.discard) {
|
|
247
|
+
if (matchesKey(data, "escape")) this.discard = false;
|
|
248
|
+
else if (matchesKey(data, "up") || matchesKey(data, "down")) this.discardSelected = !this.discardSelected;
|
|
249
|
+
else if (matchesKey(data, "enter")) {
|
|
250
|
+
if (this.discardSelected) this.done(false);
|
|
251
|
+
else this.discard = false;
|
|
252
|
+
}
|
|
253
|
+
} else if (matchesKey(data, "escape")) {
|
|
254
|
+
if (!this.dirty) this.done(false);
|
|
255
|
+
else { this.discard = true; this.discardSelected = false; }
|
|
256
|
+
} else if (matchesKey(data, "ctrl+s") || (matchesKey(data, "enter") && this.selected === this.fields.length)) {
|
|
257
|
+
void this.run(async () => { if (await this.save() !== false) this.done(true); });
|
|
258
|
+
} else if (matchesKey(data, "enter") && this.selected === this.fields.length + 1) {
|
|
259
|
+
if (!this.dirty) this.done(false);
|
|
260
|
+
else { this.discard = true; this.discardSelected = false; }
|
|
261
|
+
} else if (matchesKey(data, "up")) this.selected = Math.max(0, this.selected - 1);
|
|
262
|
+
else if (matchesKey(data, "down")) this.selected = Math.min(this.fields.length + 1, this.selected + 1);
|
|
263
|
+
else if (matchesKey(data, "enter")) {
|
|
264
|
+
const field = this.fields[this.selected];
|
|
265
|
+
if (field) void this.run(async () => {
|
|
266
|
+
const value = await this.editField(field);
|
|
267
|
+
if (value !== undefined) field.value = value;
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
this.changed();
|
|
271
|
+
}
|
|
272
|
+
render(width: number): string[] {
|
|
273
|
+
const start = Math.max(0, this.selected - 5);
|
|
274
|
+
const labelWidth = Math.min(28, Math.max(18, ...this.fields.map(field => visibleWidth(field.label))));
|
|
275
|
+
const formRows = [...this.fields, { label: "Save configuration", value: "", key: "__save" }, { label: "Back", value: "", key: "__back" }];
|
|
276
|
+
const rows = this.discard ? ["Discard unsaved changes?",
|
|
277
|
+
`${!this.discardSelected ? "›" : " "} Keep editing`, `${this.discardSelected ? "›" : " "} Discard`]
|
|
278
|
+
: formRows.slice(start, start + 6).map((field, index) => {
|
|
279
|
+
const text = fitCell(`${index + start === this.selected ? "›" : " "} ${fitCell(field.label, labelWidth)} ${field.value}`, width);
|
|
280
|
+
return index + start === this.selected && typeof this.theme?.bg === "function"
|
|
281
|
+
? this.theme.bg("selectedBg", color(this.theme, "accent", text)) : text;
|
|
282
|
+
});
|
|
283
|
+
return [color(this.theme, "accent", this.options.title + (this.dirty ? " *" : "")),
|
|
284
|
+
color(this.theme, "muted", ` ${fitCell("FIELD", labelWidth)} VALUE`), ...rows,
|
|
285
|
+
...(this.fields[this.selected]?.description ? [color(this.theme, "dim", this.fields[this.selected]!.description!)] : []),
|
|
286
|
+
...(this.options.hint ? this.options.hint.split("\n").map(line => color(this.theme, "dim", line)) : []),
|
|
287
|
+
...(this.busy || this.error ? [this.busy ? "Working…" : color(this.theme, "error", this.error)] : []),
|
|
288
|
+
shortcutLine(this.theme, this.discard ? "↑↓ move · enter choose · esc back" : "↑↓ move · enter choose · ctrl+s save · esc back", width)]
|
|
289
|
+
.map(line => truncateToWidth(line, Math.max(1, width), "…"));
|
|
290
|
+
}
|
|
291
|
+
invalidate(): void {}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export async function editForm(ctx: any, options: EditFormOptions): Promise<boolean> {
|
|
295
|
+
// Pi restores its editor when a custom dialog closes. Suspend the form before
|
|
296
|
+
// opening child dialogs, then remount the same draft/selection afterwards.
|
|
297
|
+
let form: Form | undefined;
|
|
298
|
+
let finish: (value: any) => void = () => {};
|
|
299
|
+
let render = () => {};
|
|
300
|
+
let completed: boolean | undefined;
|
|
301
|
+
for (;;) {
|
|
302
|
+
const result: any = await ctx.ui.custom((tui: any, theme: any, _kb: any, done: any) => {
|
|
303
|
+
finish = done;
|
|
304
|
+
render = () => tui.requestRender();
|
|
305
|
+
form ??= new Form(options, theme, () => render(), saved => { completed = saved; finish(saved); },
|
|
306
|
+
field => new Promise<string | undefined>((resolve, reject) => {
|
|
307
|
+
finish({ edit: field.edit, resolve, reject });
|
|
308
|
+
}), () => new Promise<boolean | void>((resolve, reject) => {
|
|
309
|
+
finish({ edit: options.onSave, resolve, reject });
|
|
310
|
+
}));
|
|
311
|
+
return form;
|
|
312
|
+
});
|
|
313
|
+
if (typeof result === "boolean" || !result?.edit) return result === true;
|
|
314
|
+
try { result.resolve(await result.edit()); } catch (error) { result.reject(error); }
|
|
315
|
+
// Let the form's pending edit callback update its display before remounting.
|
|
316
|
+
await Promise.resolve();
|
|
317
|
+
await Promise.resolve();
|
|
318
|
+
if (completed !== undefined) return completed;
|
|
319
|
+
}
|
|
320
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-better-model-select",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Searchable, usage-ranked model picker for Pi with model management, default model and thinking effort selection, and per-role subagent defaults.",
|
|
6
6
|
"keywords": [
|
|
@@ -25,8 +25,10 @@
|
|
|
25
25
|
"extensions/",
|
|
26
26
|
"!extensions/**/*.test.ts",
|
|
27
27
|
"README.md",
|
|
28
|
+
"CHANGELOG.md",
|
|
28
29
|
"docs/configuration.md",
|
|
29
30
|
"docs/development.md",
|
|
31
|
+
"docs/demo/showcase.gif",
|
|
30
32
|
"LICENSE"
|
|
31
33
|
],
|
|
32
34
|
"pi": {
|