pi-subagent-manager 0.5.1 → 0.6.0
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/README.md +1 -1
- package/package.json +1 -1
- package/src/index.ts +10 -10
- package/src/{manager.ts → orch/manager.ts} +2 -2
- package/src/{prompt.ts → orch/prompt.ts} +1 -1
- package/src/{runtime.ts → orch/runtime.ts} +3 -3
- package/src/{tools.ts → orch/tools.ts} +2 -2
- package/src/{agent-import.ts → prefs/agent-import.ts} +2 -2
- package/src/{config.ts → prefs/config.ts} +2 -2
- package/src/{dialog.ts → ui/dialog.ts} +7 -5
- package/src/ui/model-picker.ts +451 -0
- package/src/{settings-ui.ts → ui/settings-ui.ts} +15 -17
- package/src/{status-ui.ts → ui/status-ui.ts} +1 -1
- package/src/{thread-tree.ts → ui/thread-tree.ts} +1 -1
- package/src/{ui.ts → ui/ui.ts} +3 -3
- package/src/model-picker.ts +0 -687
- /package/src/{mailbox.ts → orch/mailbox.ts} +0 -0
- /package/src/{paths.ts → orch/paths.ts} +0 -0
- /package/src/{import-discovery.ts → prefs/import-discovery.ts} +0 -0
- /package/src/{import-instructions.ts → prefs/import-instructions.ts} +0 -0
- /package/src/{models.ts → prefs/models.ts} +0 -0
- /package/src/{settings.ts → prefs/settings.ts} +0 -0
- /package/src/{import-picker.ts → ui/import-picker.ts} +0 -0
package/src/model-picker.ts
DELETED
|
@@ -1,687 +0,0 @@
|
|
|
1
|
-
import fuzzysort, { type SnapshotKeys } from "fuzzysort";
|
|
2
|
-
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
3
|
-
import {
|
|
4
|
-
getSelectListTheme,
|
|
5
|
-
type ExtensionCommandContext,
|
|
6
|
-
type ScopedModel,
|
|
7
|
-
type Theme,
|
|
8
|
-
} from "@earendil-works/pi-coding-agent";
|
|
9
|
-
import {
|
|
10
|
-
Container,
|
|
11
|
-
getKeybindings,
|
|
12
|
-
Input,
|
|
13
|
-
SelectList,
|
|
14
|
-
Spacer,
|
|
15
|
-
stripTerminalSequences,
|
|
16
|
-
truncateToWidth,
|
|
17
|
-
Text,
|
|
18
|
-
type SelectItem,
|
|
19
|
-
} from "@earendil-works/pi-tui";
|
|
20
|
-
import { modelIdentity } from "./models.ts";
|
|
21
|
-
import { dialogHeight, frameDialog, DIALOG_OPTIONS } from "./dialog.ts";
|
|
22
|
-
|
|
23
|
-
const MODEL_EDITOR_LAYOUT = {
|
|
24
|
-
minPrimaryColumnWidth: 24,
|
|
25
|
-
maxPrimaryColumnWidth: 52,
|
|
26
|
-
} as const;
|
|
27
|
-
|
|
28
|
-
const PICKER_TITLE = "Preferred models";
|
|
29
|
-
|
|
30
|
-
export const MODEL_EDITOR_CANCEL = Symbol("model-editor-cancel");
|
|
31
|
-
|
|
32
|
-
type AvailableModel = Pick<Model<Api>, "provider" | "id" | "name">;
|
|
33
|
-
|
|
34
|
-
interface ModelSelectItem extends SelectItem {
|
|
35
|
-
group?: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type ModelEditorMode =
|
|
39
|
-
| { kind: "menu" }
|
|
40
|
-
| { kind: "actions"; index: number }
|
|
41
|
-
| {
|
|
42
|
-
kind: "picker";
|
|
43
|
-
replaceIndex: number | undefined;
|
|
44
|
-
returnTo: "menu" | "actions";
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
interface OrderedModelEditorComponentOptions {
|
|
48
|
-
tui: { requestRender(force?: boolean): void; terminal?: { rows: number } };
|
|
49
|
-
theme: Theme;
|
|
50
|
-
availableModels: readonly AvailableModel[];
|
|
51
|
-
scopedModels: readonly ScopedModel[];
|
|
52
|
-
initialModels?: readonly string[];
|
|
53
|
-
modelSuggestions?: readonly string[];
|
|
54
|
-
onDone(models: string[]): void;
|
|
55
|
-
onCancel(): void;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function sanitizeModelText(text: string): string {
|
|
59
|
-
return stripTerminalSequences(text).replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function renderModelIdentity(identity: string): string {
|
|
63
|
-
return sanitizeModelText(identity);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function modelSearchText(text: string): string {
|
|
67
|
-
// Providers spell the same version differently (sonnet-5.5 vs sonnet-5-5).
|
|
68
|
-
// Strip separators rather than splitting them into repeated numeric terms;
|
|
69
|
-
// raw identities and labels remain untouched.
|
|
70
|
-
return sanitizeModelText(text).replace(/[._:/-]+/g, "");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function sortModels(models: readonly AvailableModel[]): AvailableModel[] {
|
|
74
|
-
const unique = new Map<string, AvailableModel>();
|
|
75
|
-
for (const model of models) unique.set(modelIdentity(model), model);
|
|
76
|
-
return [...unique.values()].sort(
|
|
77
|
-
(a, b) =>
|
|
78
|
-
a.provider.localeCompare(b.provider) ||
|
|
79
|
-
a.id.localeCompare(b.id) ||
|
|
80
|
-
sanitizeModelText(a.name).localeCompare(sanitizeModelText(b.name)),
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function describeModel(
|
|
85
|
-
model: AvailableModel,
|
|
86
|
-
scopedIdentities: ReadonlySet<string>,
|
|
87
|
-
): string {
|
|
88
|
-
const annotation = scopedIdentities.has(modelIdentity(model))
|
|
89
|
-
? "scoped in this session"
|
|
90
|
-
: "portable preference · not scoped in this session";
|
|
91
|
-
const name = sanitizeModelText(model.name);
|
|
92
|
-
return [name, annotation].filter(Boolean).join(" · ");
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export class OrderedModelEditorComponent extends Container {
|
|
96
|
-
private readonly tui: {
|
|
97
|
-
requestRender(force?: boolean): void;
|
|
98
|
-
terminal?: { rows: number };
|
|
99
|
-
};
|
|
100
|
-
private readonly theme: Theme;
|
|
101
|
-
private readonly allModels: readonly AvailableModel[];
|
|
102
|
-
private readonly modelSearch: SnapshotKeys<AvailableModel>;
|
|
103
|
-
private readonly suggestedScores: ReadonlyMap<string, number>;
|
|
104
|
-
private readonly modelsByIdentity = new Map<string, AvailableModel>();
|
|
105
|
-
private readonly scopedIdentities: ReadonlySet<string>;
|
|
106
|
-
private readonly onDone: (models: string[]) => void;
|
|
107
|
-
private readonly onCancel: () => void;
|
|
108
|
-
private readonly body = new Container();
|
|
109
|
-
private readonly titleText = new Text();
|
|
110
|
-
private readonly subtitleText = new Text();
|
|
111
|
-
private readonly footerText = new Text();
|
|
112
|
-
private draft: string[];
|
|
113
|
-
private mode: ModelEditorMode;
|
|
114
|
-
private pickerQuery = "";
|
|
115
|
-
private pickerSelectedValue: string | undefined;
|
|
116
|
-
private currentItems: ModelSelectItem[] = [];
|
|
117
|
-
private selectList = new SelectList(
|
|
118
|
-
[],
|
|
119
|
-
1,
|
|
120
|
-
getSelectListTheme(),
|
|
121
|
-
MODEL_EDITOR_LAYOUT,
|
|
122
|
-
);
|
|
123
|
-
private searchInput: Input | undefined;
|
|
124
|
-
private pickerListChildIndex = -1;
|
|
125
|
-
private menuSelectedValue: string | undefined;
|
|
126
|
-
private actionSelectedValue: string | undefined;
|
|
127
|
-
private _focused = false;
|
|
128
|
-
|
|
129
|
-
constructor(options: OrderedModelEditorComponentOptions) {
|
|
130
|
-
super();
|
|
131
|
-
this.tui = options.tui;
|
|
132
|
-
this.theme = options.theme;
|
|
133
|
-
this.allModels = sortModels(options.availableModels);
|
|
134
|
-
// The registry is fixed for this dialog; prepare searchable fields once.
|
|
135
|
-
// Scope annotations deliberately aren't searchable model metadata.
|
|
136
|
-
this.modelSearch = fuzzysort.snapshot(this.allModels, {
|
|
137
|
-
keys: [
|
|
138
|
-
(model) => modelSearchText(modelIdentity(model)),
|
|
139
|
-
(model) => modelSearchText(model.name),
|
|
140
|
-
],
|
|
141
|
-
});
|
|
142
|
-
this.suggestedScores = this.searchScores(options.modelSuggestions ?? []);
|
|
143
|
-
this.onDone = options.onDone;
|
|
144
|
-
this.onCancel = options.onCancel;
|
|
145
|
-
for (const model of this.allModels)
|
|
146
|
-
this.modelsByIdentity.set(modelIdentity(model), model);
|
|
147
|
-
this.scopedIdentities = new Set(
|
|
148
|
-
options.scopedModels.map((entry) => modelIdentity(entry.model)),
|
|
149
|
-
);
|
|
150
|
-
this.draft = [...(options.initialModels ?? [])];
|
|
151
|
-
this.mode =
|
|
152
|
-
this.draft.length === 0
|
|
153
|
-
? { kind: "picker", replaceIndex: undefined, returnTo: "menu" }
|
|
154
|
-
: { kind: "menu" };
|
|
155
|
-
|
|
156
|
-
this.rebuild();
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
get focused(): boolean {
|
|
160
|
-
return this._focused;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
set focused(value: boolean) {
|
|
164
|
-
this._focused = value;
|
|
165
|
-
if (this.searchInput) this.searchInput.focused = value;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
getMode(): ModelEditorMode["kind"] {
|
|
169
|
-
return this.mode.kind;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
getDraftModels(): readonly string[] {
|
|
173
|
-
return this.draft;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
getCurrentItems(): readonly SelectItem[] {
|
|
177
|
-
return this.currentItems;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
getSelectList(): SelectList {
|
|
181
|
-
return this.selectList;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
getSearchInput(): Input | undefined {
|
|
185
|
-
return this.searchInput;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
render(width: number): string[] {
|
|
189
|
-
const height = dialogHeight(this.tui);
|
|
190
|
-
const inner = Math.max(1, width - 4);
|
|
191
|
-
const header = [
|
|
192
|
-
truncateToWidth(
|
|
193
|
-
this.subtitleText
|
|
194
|
-
.render(1000)
|
|
195
|
-
.map((line) => line.trim())
|
|
196
|
-
.filter(Boolean)
|
|
197
|
-
.join(" "),
|
|
198
|
-
inner,
|
|
199
|
-
"",
|
|
200
|
-
),
|
|
201
|
-
];
|
|
202
|
-
if (this.searchInput) header.push(...this.searchInput.render(inner));
|
|
203
|
-
const budget = Math.max(1, height - 4 - header.length);
|
|
204
|
-
const selected = this.selectList.getSelectedItem()?.value;
|
|
205
|
-
// Headings are visual rows only: keyboard navigation selects models, not groups.
|
|
206
|
-
const displayRows: (ModelSelectItem | string)[] = [];
|
|
207
|
-
let previousGroup: string | undefined;
|
|
208
|
-
for (const item of this.currentItems) {
|
|
209
|
-
if (item.group && item.group !== previousGroup) {
|
|
210
|
-
displayRows.push(item.group);
|
|
211
|
-
}
|
|
212
|
-
previousGroup = item.group;
|
|
213
|
-
displayRows.push(item);
|
|
214
|
-
}
|
|
215
|
-
const index = Math.max(
|
|
216
|
-
0,
|
|
217
|
-
displayRows.findIndex(
|
|
218
|
-
(row) => typeof row !== "string" && row.value === selected,
|
|
219
|
-
),
|
|
220
|
-
);
|
|
221
|
-
const start = Math.max(
|
|
222
|
-
0,
|
|
223
|
-
Math.min(index - budget + 1, displayRows.length - budget),
|
|
224
|
-
);
|
|
225
|
-
// Only format visible rows; a registry can contain thousands of models.
|
|
226
|
-
const rows = displayRows.slice(start, start + budget).map((row) => {
|
|
227
|
-
if (typeof row === "string") return this.theme.fg("muted", row);
|
|
228
|
-
const text = `${row.value === selected ? "›" : " "} ${row.label} ${this.theme.fg("muted", row.description ?? "")}`;
|
|
229
|
-
return row.value === selected ? this.theme.fg("accent", text) : text;
|
|
230
|
-
});
|
|
231
|
-
if (!rows.length) rows.push(this.theme.fg("muted", "No matching models"));
|
|
232
|
-
return frameDialog(
|
|
233
|
-
this.theme,
|
|
234
|
-
width,
|
|
235
|
-
height,
|
|
236
|
-
this.titleText.render(1000).join(" ").trim(),
|
|
237
|
-
[...header, ...rows].map((line) => ` ${line}`),
|
|
238
|
-
this.footerText
|
|
239
|
-
.render(1000)
|
|
240
|
-
.map((line) => line.trim())
|
|
241
|
-
.filter(Boolean)
|
|
242
|
-
.join(" "),
|
|
243
|
-
);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
handleInput(keyData: string): void {
|
|
247
|
-
if (this.mode.kind === "picker" && this.searchInput) {
|
|
248
|
-
const kb = getKeybindings();
|
|
249
|
-
const isNav =
|
|
250
|
-
kb.matches(keyData, "tui.select.up") ||
|
|
251
|
-
kb.matches(keyData, "tui.select.down") ||
|
|
252
|
-
kb.matches(keyData, "tui.select.confirm") ||
|
|
253
|
-
kb.matches(keyData, "tui.select.cancel");
|
|
254
|
-
if (isNav) {
|
|
255
|
-
this.selectList.handleInput(keyData);
|
|
256
|
-
} else {
|
|
257
|
-
this.searchInput.handleInput(keyData);
|
|
258
|
-
const nextQuery = this.searchInput.getValue();
|
|
259
|
-
if (nextQuery !== this.pickerQuery) {
|
|
260
|
-
this.pickerQuery = nextQuery;
|
|
261
|
-
this.pickerSelectedValue = undefined;
|
|
262
|
-
this.refreshPickerList(this.mode.replaceIndex, this.mode.returnTo);
|
|
263
|
-
this.tui.requestRender();
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
this.tui.requestRender();
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
this.selectList.handleInput(keyData);
|
|
271
|
-
this.tui.requestRender();
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
private rebuild(): void {
|
|
275
|
-
this.body.clear();
|
|
276
|
-
this.searchInput = undefined;
|
|
277
|
-
this.pickerListChildIndex = -1;
|
|
278
|
-
if (this.mode.kind === "menu") {
|
|
279
|
-
this.buildMenu();
|
|
280
|
-
} else if (this.mode.kind === "actions") {
|
|
281
|
-
this.buildActions(this.mode.index);
|
|
282
|
-
} else {
|
|
283
|
-
this.buildPicker(this.mode.replaceIndex);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
private buildMenu(): void {
|
|
288
|
-
this.titleText.setText(this.theme.fg("accent", PICKER_TITLE));
|
|
289
|
-
this.subtitleText.setText(
|
|
290
|
-
this.theme.fg(
|
|
291
|
-
"muted",
|
|
292
|
-
this.withScopeWarning(
|
|
293
|
-
this.draft.length
|
|
294
|
-
? "Ordered fallback preferences. Select a numbered entry for actions."
|
|
295
|
-
: "No explicit preferences. Add models or done to inherit the default.",
|
|
296
|
-
),
|
|
297
|
-
),
|
|
298
|
-
);
|
|
299
|
-
const items: SelectItem[] = [
|
|
300
|
-
...this.draft.map((identity, index) => ({
|
|
301
|
-
value: `entry:${index}`,
|
|
302
|
-
label: `${index + 1}. ${renderModelIdentity(identity)}`,
|
|
303
|
-
description: this.describeDraftIdentity(identity),
|
|
304
|
-
})),
|
|
305
|
-
{
|
|
306
|
-
value: "action:add",
|
|
307
|
-
label: "Add model",
|
|
308
|
-
description: "Append another preferred model to the ordered list",
|
|
309
|
-
},
|
|
310
|
-
{
|
|
311
|
-
value: "action:clear",
|
|
312
|
-
label: "Use inherited default",
|
|
313
|
-
description: "Clear explicit preferences and inherit the default model",
|
|
314
|
-
},
|
|
315
|
-
{
|
|
316
|
-
value: "action:done",
|
|
317
|
-
label: "Done",
|
|
318
|
-
description: "Commit this ordered model preference list",
|
|
319
|
-
},
|
|
320
|
-
{
|
|
321
|
-
value: "action:cancel",
|
|
322
|
-
label: "Cancel",
|
|
323
|
-
description: "Discard unsaved model preference changes",
|
|
324
|
-
},
|
|
325
|
-
];
|
|
326
|
-
this.currentItems = items;
|
|
327
|
-
this.selectList = this.buildSelectList(
|
|
328
|
-
items,
|
|
329
|
-
this.menuSelectedValue,
|
|
330
|
-
(value) => {
|
|
331
|
-
if (value.startsWith("entry:")) {
|
|
332
|
-
const index = Number(value.slice("entry:".length));
|
|
333
|
-
if (!Number.isNaN(index) && this.draft[index] !== undefined) {
|
|
334
|
-
this.mode = { kind: "actions", index };
|
|
335
|
-
this.actionSelectedValue = "action:replace";
|
|
336
|
-
this.rebuildAndRender();
|
|
337
|
-
}
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
if (value === "action:add") {
|
|
341
|
-
this.openPicker(undefined, "menu");
|
|
342
|
-
} else if (value === "action:clear") {
|
|
343
|
-
this.draft = [];
|
|
344
|
-
this.menuSelectedValue = "action:done";
|
|
345
|
-
this.rebuildAndRender();
|
|
346
|
-
} else if (value === "action:done") {
|
|
347
|
-
this.onDone([...this.draft]);
|
|
348
|
-
} else if (value === "action:cancel") {
|
|
349
|
-
this.onCancel();
|
|
350
|
-
}
|
|
351
|
-
},
|
|
352
|
-
() => this.onCancel(),
|
|
353
|
-
);
|
|
354
|
-
this.body.addChild(this.selectList);
|
|
355
|
-
this.footerText.setText(
|
|
356
|
-
this.theme.fg(
|
|
357
|
-
"dim",
|
|
358
|
-
"↑↓ select · Enter edit · Esc cancel · Done applies preferences",
|
|
359
|
-
),
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
private buildActions(index: number): void {
|
|
364
|
-
const identity = this.draft[index] ?? "";
|
|
365
|
-
this.titleText.setText(this.theme.fg("accent", `Model ${index + 1}`));
|
|
366
|
-
this.subtitleText.setText(
|
|
367
|
-
this.theme.fg(
|
|
368
|
-
"muted",
|
|
369
|
-
`${index + 1}. ${renderModelIdentity(identity)} · ${this.describeDraftIdentity(identity)}`,
|
|
370
|
-
),
|
|
371
|
-
);
|
|
372
|
-
const items: SelectItem[] = [
|
|
373
|
-
{
|
|
374
|
-
value: "action:replace",
|
|
375
|
-
label: "Replace",
|
|
376
|
-
description: "Choose a different available model for this position",
|
|
377
|
-
},
|
|
378
|
-
{
|
|
379
|
-
value: "action:remove",
|
|
380
|
-
label: "Remove",
|
|
381
|
-
description: "Delete this preference from the ordered list",
|
|
382
|
-
},
|
|
383
|
-
...(index > 0
|
|
384
|
-
? [
|
|
385
|
-
{
|
|
386
|
-
value: "action:earlier",
|
|
387
|
-
label: "Move earlier",
|
|
388
|
-
description: "Swap with the previous preference",
|
|
389
|
-
},
|
|
390
|
-
]
|
|
391
|
-
: []),
|
|
392
|
-
...(index < this.draft.length - 1
|
|
393
|
-
? [
|
|
394
|
-
{
|
|
395
|
-
value: "action:later",
|
|
396
|
-
label: "Move later",
|
|
397
|
-
description: "Swap with the next preference",
|
|
398
|
-
},
|
|
399
|
-
]
|
|
400
|
-
: []),
|
|
401
|
-
{
|
|
402
|
-
value: "action:back",
|
|
403
|
-
label: "Back",
|
|
404
|
-
description: "Return to the ordered preference list",
|
|
405
|
-
},
|
|
406
|
-
];
|
|
407
|
-
this.currentItems = items;
|
|
408
|
-
this.selectList = this.buildSelectList(
|
|
409
|
-
items,
|
|
410
|
-
this.actionSelectedValue,
|
|
411
|
-
(value) => {
|
|
412
|
-
if (value === "action:replace") {
|
|
413
|
-
this.openPicker(index, "actions");
|
|
414
|
-
} else if (value === "action:remove") {
|
|
415
|
-
this.draft.splice(index, 1);
|
|
416
|
-
this.mode = { kind: "menu" };
|
|
417
|
-
this.menuSelectedValue = this.draft[index]
|
|
418
|
-
? `entry:${index}`
|
|
419
|
-
: "action:add";
|
|
420
|
-
this.rebuildAndRender();
|
|
421
|
-
} else if (value === "action:earlier" && index > 0) {
|
|
422
|
-
[this.draft[index - 1], this.draft[index]] = [
|
|
423
|
-
this.draft[index],
|
|
424
|
-
this.draft[index - 1],
|
|
425
|
-
];
|
|
426
|
-
this.mode = { kind: "menu" };
|
|
427
|
-
this.menuSelectedValue = `entry:${index - 1}`;
|
|
428
|
-
this.rebuildAndRender();
|
|
429
|
-
} else if (value === "action:later" && index < this.draft.length - 1) {
|
|
430
|
-
[this.draft[index], this.draft[index + 1]] = [
|
|
431
|
-
this.draft[index + 1],
|
|
432
|
-
this.draft[index],
|
|
433
|
-
];
|
|
434
|
-
this.mode = { kind: "menu" };
|
|
435
|
-
this.menuSelectedValue = `entry:${index + 1}`;
|
|
436
|
-
this.rebuildAndRender();
|
|
437
|
-
} else if (value === "action:back") {
|
|
438
|
-
this.mode = { kind: "menu" };
|
|
439
|
-
this.menuSelectedValue = `entry:${index}`;
|
|
440
|
-
this.rebuildAndRender();
|
|
441
|
-
}
|
|
442
|
-
},
|
|
443
|
-
() => {
|
|
444
|
-
this.mode = { kind: "menu" };
|
|
445
|
-
this.menuSelectedValue = `entry:${index}`;
|
|
446
|
-
this.rebuildAndRender();
|
|
447
|
-
},
|
|
448
|
-
);
|
|
449
|
-
this.body.addChild(this.selectList);
|
|
450
|
-
this.footerText.setText(
|
|
451
|
-
this.theme.fg("dim", "↑↓ select · Enter choose · Esc back"),
|
|
452
|
-
);
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
private buildPicker(replaceIndex: number | undefined): void {
|
|
456
|
-
const isReplace = replaceIndex !== undefined;
|
|
457
|
-
this.titleText.setText(
|
|
458
|
-
this.theme.fg(
|
|
459
|
-
"accent",
|
|
460
|
-
isReplace ? `Replace model ${replaceIndex + 1}` : "Add model",
|
|
461
|
-
),
|
|
462
|
-
);
|
|
463
|
-
this.subtitleText.setText(
|
|
464
|
-
this.theme.fg(
|
|
465
|
-
"muted",
|
|
466
|
-
this.withScopeWarning(
|
|
467
|
-
this.suggestedScores.size
|
|
468
|
-
? "Suggested matches: scoped first, then other models; the rest A–Z. Type to search."
|
|
469
|
-
: "Search by provider/id or model name. Models A–Z; selected duplicates are hidden.",
|
|
470
|
-
),
|
|
471
|
-
),
|
|
472
|
-
);
|
|
473
|
-
this.searchInput = new Input({ placeholder: "Search models" });
|
|
474
|
-
this.searchInput.setValue(this.pickerQuery);
|
|
475
|
-
this.searchInput.focused = this._focused;
|
|
476
|
-
this.searchInput.onSubmit = () => {
|
|
477
|
-
this.selectList.handleInput("\r");
|
|
478
|
-
this.tui.requestRender();
|
|
479
|
-
};
|
|
480
|
-
this.body.addChild(this.searchInput);
|
|
481
|
-
this.body.addChild(new Spacer(1));
|
|
482
|
-
const returnTo = this.mode.kind === "picker" ? this.mode.returnTo : "menu";
|
|
483
|
-
this.pickerListChildIndex = this.body.children.length;
|
|
484
|
-
this.refreshPickerList(replaceIndex, returnTo);
|
|
485
|
-
this.footerText.setText(
|
|
486
|
-
this.theme.fg("dim", "Type filter · ↑↓ select · Enter choose · Esc back"),
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
private rebuildAndRender(): void {
|
|
491
|
-
this.rebuild();
|
|
492
|
-
this.tui.requestRender();
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
private openPicker(
|
|
496
|
-
replaceIndex: number | undefined,
|
|
497
|
-
returnTo: "menu" | "actions",
|
|
498
|
-
): void {
|
|
499
|
-
this.mode = { kind: "picker", replaceIndex, returnTo };
|
|
500
|
-
this.pickerQuery = "";
|
|
501
|
-
this.pickerSelectedValue = undefined;
|
|
502
|
-
this.rebuildAndRender();
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
private refreshPickerList(
|
|
506
|
-
replaceIndex: number | undefined,
|
|
507
|
-
returnTo: "menu" | "actions",
|
|
508
|
-
): void {
|
|
509
|
-
const items = this.getPickerItems(replaceIndex);
|
|
510
|
-
this.currentItems = items;
|
|
511
|
-
const list = this.buildSelectList(
|
|
512
|
-
items,
|
|
513
|
-
this.pickerSelectedValue,
|
|
514
|
-
(value) => {
|
|
515
|
-
if (replaceIndex === undefined) this.draft.push(value);
|
|
516
|
-
else this.draft[replaceIndex] = value;
|
|
517
|
-
this.mode = { kind: "menu" };
|
|
518
|
-
this.menuSelectedValue = `entry:${replaceIndex === undefined ? this.draft.length - 1 : replaceIndex}`;
|
|
519
|
-
this.pickerQuery = "";
|
|
520
|
-
this.pickerSelectedValue = undefined;
|
|
521
|
-
this.rebuildAndRender();
|
|
522
|
-
},
|
|
523
|
-
() => {
|
|
524
|
-
this.mode =
|
|
525
|
-
returnTo === "actions" && replaceIndex !== undefined
|
|
526
|
-
? { kind: "actions", index: replaceIndex }
|
|
527
|
-
: { kind: "menu" };
|
|
528
|
-
this.pickerQuery = "";
|
|
529
|
-
this.pickerSelectedValue = undefined;
|
|
530
|
-
this.rebuildAndRender();
|
|
531
|
-
},
|
|
532
|
-
);
|
|
533
|
-
this.selectList = list;
|
|
534
|
-
if (this.body.children[this.pickerListChildIndex])
|
|
535
|
-
this.body.children[this.pickerListChildIndex] = list;
|
|
536
|
-
else this.body.addChild(list);
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
private buildSelectList(
|
|
540
|
-
items: SelectItem[],
|
|
541
|
-
selectedValue: string | undefined,
|
|
542
|
-
onSelect: (value: string) => void,
|
|
543
|
-
onCancel: () => void,
|
|
544
|
-
): SelectList {
|
|
545
|
-
const list = new SelectList(
|
|
546
|
-
items,
|
|
547
|
-
Math.min(Math.max(items.length, 1), 10),
|
|
548
|
-
getSelectListTheme(),
|
|
549
|
-
MODEL_EDITOR_LAYOUT,
|
|
550
|
-
);
|
|
551
|
-
const selectedIndex = selectedValue
|
|
552
|
-
? items.findIndex((item) => item.value === selectedValue)
|
|
553
|
-
: 0;
|
|
554
|
-
if (selectedIndex >= 0) list.setSelectedIndex(selectedIndex);
|
|
555
|
-
list.onSelect = (item) => {
|
|
556
|
-
if (this.mode.kind === "menu") this.menuSelectedValue = item.value;
|
|
557
|
-
else if (this.mode.kind === "actions")
|
|
558
|
-
this.actionSelectedValue = item.value;
|
|
559
|
-
else this.pickerSelectedValue = item.value;
|
|
560
|
-
onSelect(item.value);
|
|
561
|
-
};
|
|
562
|
-
list.onSelectionChange = (item) => {
|
|
563
|
-
if (this.mode.kind === "menu") this.menuSelectedValue = item.value;
|
|
564
|
-
else if (this.mode.kind === "actions")
|
|
565
|
-
this.actionSelectedValue = item.value;
|
|
566
|
-
else this.pickerSelectedValue = item.value;
|
|
567
|
-
this.tui.requestRender();
|
|
568
|
-
};
|
|
569
|
-
list.onCancel = onCancel;
|
|
570
|
-
return list;
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
private searchScores(queries: readonly string[]): Map<string, number> {
|
|
574
|
-
const scores = new Map<string, number>();
|
|
575
|
-
const uniqueQueries = new Set(
|
|
576
|
-
queries.map((query) => modelSearchText(query).trim()).filter(Boolean),
|
|
577
|
-
);
|
|
578
|
-
for (const query of uniqueQueries) {
|
|
579
|
-
// Both defaults are restrictive (10 results, score >= .5); collect every
|
|
580
|
-
// fuzzy match so suggestions aren't silently capped or omitted.
|
|
581
|
-
for (const result of fuzzysort.go(query, this.modelSearch, {
|
|
582
|
-
limit: 0,
|
|
583
|
-
threshold: 0,
|
|
584
|
-
})) {
|
|
585
|
-
const identity = modelIdentity(result.obj);
|
|
586
|
-
scores.set(identity, Math.max(scores.get(identity) ?? 0, result.score));
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
return scores;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
private getPickerItems(replaceIndex: number | undefined): ModelSelectItem[] {
|
|
593
|
-
const excluded = new Set(
|
|
594
|
-
this.draft.filter((_, index) => index !== replaceIndex),
|
|
595
|
-
);
|
|
596
|
-
const query = this.pickerQuery.trim();
|
|
597
|
-
const queryScores = query ? this.searchScores([query]) : undefined;
|
|
598
|
-
const scoped: AvailableModel[] = [];
|
|
599
|
-
const other: AvailableModel[] = [];
|
|
600
|
-
const remaining: AvailableModel[] = [];
|
|
601
|
-
for (const model of this.allModels) {
|
|
602
|
-
const identity = modelIdentity(model);
|
|
603
|
-
if (excluded.has(identity) || (queryScores && !queryScores.has(identity)))
|
|
604
|
-
continue;
|
|
605
|
-
if (this.suggestedScores.has(identity)) {
|
|
606
|
-
(this.scopedIdentities.has(identity) ? scoped : other).push(model);
|
|
607
|
-
} else {
|
|
608
|
-
remaining.push(model);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
const bySuggestionScore = (a: AvailableModel, b: AvailableModel) =>
|
|
612
|
-
this.suggestedScores.get(modelIdentity(b))! -
|
|
613
|
-
this.suggestedScores.get(modelIdentity(a))!;
|
|
614
|
-
// Stable score sorting preserves alphabetical order for ties. The last
|
|
615
|
-
// tier retains the registry's alphabetical order, irrespective of scope.
|
|
616
|
-
scoped.sort(bySuggestionScore);
|
|
617
|
-
other.sort(bySuggestionScore);
|
|
618
|
-
const items = (
|
|
619
|
-
models: AvailableModel[],
|
|
620
|
-
group: string,
|
|
621
|
-
): ModelSelectItem[] =>
|
|
622
|
-
models.map((model) => ({
|
|
623
|
-
value: modelIdentity(model),
|
|
624
|
-
label: renderModelIdentity(modelIdentity(model)),
|
|
625
|
-
description: describeModel(model, this.scopedIdentities),
|
|
626
|
-
group,
|
|
627
|
-
}));
|
|
628
|
-
return [
|
|
629
|
-
...items(scoped, "Suggested matches · scoped models"),
|
|
630
|
-
...items(other, "Suggested matches · other models"),
|
|
631
|
-
...items(
|
|
632
|
-
remaining,
|
|
633
|
-
scoped.length || other.length
|
|
634
|
-
? "All other models · A–Z"
|
|
635
|
-
: "All models · A–Z",
|
|
636
|
-
),
|
|
637
|
-
];
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
private describeDraftIdentity(identity: string): string {
|
|
641
|
-
const model = this.modelsByIdentity.get(identity);
|
|
642
|
-
if (!model) return "unavailable in current model registry";
|
|
643
|
-
return describeModel(model, this.scopedIdentities);
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
private withScopeWarning(text: string): string {
|
|
647
|
-
if (this.scopedIdentities.size === 0) {
|
|
648
|
-
return `No scoped models: explicit preferences cannot run. Configure /scoped-models. ${text}`;
|
|
649
|
-
}
|
|
650
|
-
return text;
|
|
651
|
-
}
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
export async function editModelPreferences(
|
|
655
|
-
ctx: ExtensionCommandContext,
|
|
656
|
-
currentModels?: readonly string[],
|
|
657
|
-
modelSuggestions?: readonly string[],
|
|
658
|
-
): Promise<readonly string[] | typeof MODEL_EDITOR_CANCEL> {
|
|
659
|
-
if (ctx.mode !== "tui") {
|
|
660
|
-
if (ctx.hasUI) {
|
|
661
|
-
ctx.ui.notify(
|
|
662
|
-
"Ordered model preferences require interactive TUI mode; draft unchanged.",
|
|
663
|
-
"warning",
|
|
664
|
-
);
|
|
665
|
-
}
|
|
666
|
-
return MODEL_EDITOR_CANCEL;
|
|
667
|
-
}
|
|
668
|
-
const availableModels = ctx.modelRegistry.getAvailable().map((model) => ({
|
|
669
|
-
provider: model.provider,
|
|
670
|
-
id: model.id,
|
|
671
|
-
name: model.name,
|
|
672
|
-
}));
|
|
673
|
-
return ctx.ui.custom<readonly string[] | typeof MODEL_EDITOR_CANCEL>(
|
|
674
|
-
(tui, theme, _keys, done) =>
|
|
675
|
-
new OrderedModelEditorComponent({
|
|
676
|
-
tui,
|
|
677
|
-
theme,
|
|
678
|
-
availableModels,
|
|
679
|
-
scopedModels: ctx.scopedModels,
|
|
680
|
-
initialModels: currentModels,
|
|
681
|
-
modelSuggestions,
|
|
682
|
-
onDone: (models) => done(models),
|
|
683
|
-
onCancel: () => done(MODEL_EDITOR_CANCEL),
|
|
684
|
-
}),
|
|
685
|
-
DIALOG_OPTIONS,
|
|
686
|
-
);
|
|
687
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|