@restranger/repi-advisor 0.0.0-stage → 0.0.1-6455ac6
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 +88 -2
- package/package.json +44 -5
- package/src/advisor-ui.ts +1117 -0
- package/src/command.ts +169 -0
- package/src/config.ts +302 -0
- package/src/context.ts +62 -0
- package/src/execute.ts +355 -0
- package/src/handlers.ts +73 -0
- package/src/index.ts +31 -0
- package/src/inventory.ts +99 -0
- package/src/messages.ts +64 -0
- package/src/pi-compat.ts +90 -0
- package/src/plugin.ts +22 -0
- package/src/policy.ts +49 -0
- package/src/prompt.ts +16 -0
- package/src/register.ts +74 -0
- package/src/restore.ts +168 -0
- package/src/state.ts +36 -0
- package/src/types.ts +57 -0
|
@@ -0,0 +1,1117 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type PiContext,
|
|
3
|
+
type PiExtensionAPI,
|
|
4
|
+
type PiModel,
|
|
5
|
+
SUBAGENTS_CONTRACT_ID,
|
|
6
|
+
type SubagentSummary,
|
|
7
|
+
type SubagentsService,
|
|
8
|
+
boldText,
|
|
9
|
+
getService,
|
|
10
|
+
getSessionId,
|
|
11
|
+
isModelLike,
|
|
12
|
+
modelKey,
|
|
13
|
+
resolveModel,
|
|
14
|
+
sanitize,
|
|
15
|
+
truncate,
|
|
16
|
+
} from "@restranger/repi-lib";
|
|
17
|
+
import { NativeMenuView, fuzzyScore, isPrintable, mouseWheelDirection, primaryColumnWidth } from "@restranger/repi-ui";
|
|
18
|
+
import { saveAdvisorConfig } from "./config.js";
|
|
19
|
+
import { reconcileAdvisorTool } from "./handlers.js";
|
|
20
|
+
import {
|
|
21
|
+
DEFAULT_EFFORT,
|
|
22
|
+
EFFORT_ORDINAL,
|
|
23
|
+
type GradedEffort,
|
|
24
|
+
MSG_ADVISOR_DISABLED,
|
|
25
|
+
NO_ADVISOR_VALUE,
|
|
26
|
+
msgAdvisorEnabled,
|
|
27
|
+
msgAdvisorEnabledInactive,
|
|
28
|
+
} from "./messages.js";
|
|
29
|
+
import { isExecutorBlocked } from "./policy.js";
|
|
30
|
+
import {
|
|
31
|
+
clearAdvisorState,
|
|
32
|
+
getAdvisorEffort,
|
|
33
|
+
getAdvisorFallbacks,
|
|
34
|
+
getAdvisorModel,
|
|
35
|
+
setAdvisorEffort,
|
|
36
|
+
setAdvisorFallbacks,
|
|
37
|
+
setAdvisorModel,
|
|
38
|
+
} from "./state.js";
|
|
39
|
+
import type { AdvisorPluginOptions } from "./types.js";
|
|
40
|
+
|
|
41
|
+
export interface AdvisorMenuViewOptions {
|
|
42
|
+
readonly currentModel?: string;
|
|
43
|
+
readonly currentEffort?: GradedEffort;
|
|
44
|
+
readonly currentFallbacks?: readonly string[];
|
|
45
|
+
readonly availableModels: readonly PiModel[];
|
|
46
|
+
/** Kept for callers that pass discovery results; the menu itself edits global settings. */
|
|
47
|
+
readonly subagents?: readonly SubagentSummary[];
|
|
48
|
+
readonly onSave: (result: {
|
|
49
|
+
modelKey?: string | null;
|
|
50
|
+
effort?: GradedEffort | null;
|
|
51
|
+
fallbackModels?: readonly string[];
|
|
52
|
+
}) => void;
|
|
53
|
+
readonly onDone?: () => void;
|
|
54
|
+
readonly maxRows?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A model offered by the picker, already reduced to what a list row needs. */
|
|
58
|
+
interface ModelCandidate {
|
|
59
|
+
/** `provider/id`, or `__no_advisor__`. */
|
|
60
|
+
readonly id: string;
|
|
61
|
+
readonly label: string;
|
|
62
|
+
readonly description: string;
|
|
63
|
+
/** Lowercased haystack for the search field. */
|
|
64
|
+
readonly search: string;
|
|
65
|
+
/** Provider used by the "toggle provider" action; undefined for `No advisor`. */
|
|
66
|
+
readonly provider?: string;
|
|
67
|
+
/** Whether the candidate supports reasoning / thinking. */
|
|
68
|
+
readonly reasoning?: boolean;
|
|
69
|
+
/** The model is selected/configured but is not currently available. */
|
|
70
|
+
readonly unavailable?: boolean;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface EffortOption {
|
|
74
|
+
/** `undefined` means "model default". */
|
|
75
|
+
readonly effort?: GradedEffort;
|
|
76
|
+
readonly label: string;
|
|
77
|
+
readonly description: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
type SettingId = "model" | "effort" | "fallbacks";
|
|
81
|
+
type PickerKind = "model" | "fallback" | "effort";
|
|
82
|
+
|
|
83
|
+
const MIN_WIDTH = 36;
|
|
84
|
+
|
|
85
|
+
const SETTINGS: readonly { readonly id: SettingId; readonly label: string }[] = [
|
|
86
|
+
{ id: "model", label: "Model" },
|
|
87
|
+
{ id: "effort", label: "Reasoning effort" },
|
|
88
|
+
{ id: "fallbacks", label: "Fallback models" },
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
const NO_ADVISOR_CANDIDATE: ModelCandidate = {
|
|
92
|
+
id: NO_ADVISOR_VALUE,
|
|
93
|
+
label: "No advisor",
|
|
94
|
+
description: "Disable the advisor tool",
|
|
95
|
+
search: "no advisor disable none off",
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Resolves the graded effort levels supported by a model.
|
|
100
|
+
*
|
|
101
|
+
* In Pi, models with `reasoning: true` support `minimal`, `low`, `medium`, and `high` by default,
|
|
102
|
+
* unless mapped to `null` in `thinkingLevelMap`. Levels `xhigh` and `max` require an explicit
|
|
103
|
+
* non-null entry in `thinkingLevelMap`. Models without reasoning support no effort levels.
|
|
104
|
+
*/
|
|
105
|
+
export function getSupportedEffortLevels(model: PiModel | undefined): readonly GradedEffort[] {
|
|
106
|
+
if (!model?.reasoning) {
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
const map = model.thinkingLevelMap as Record<string, string | null | undefined> | undefined;
|
|
110
|
+
return EFFORT_ORDINAL.filter((level) => {
|
|
111
|
+
const mapped = map?.[level];
|
|
112
|
+
if (mapped === null) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
if (level === "xhigh" || level === "max") {
|
|
116
|
+
return mapped !== undefined;
|
|
117
|
+
}
|
|
118
|
+
return true;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function pluralModels(count: number): string {
|
|
123
|
+
return `${count} model${count === 1 ? "" : "s"}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* `/advisor` menu, rendered exactly like `/fallback` and Pi's own pickers (`/models`,
|
|
128
|
+
* `/scoped-models`, `ctx.ui.select`): an inline frame in the editor slot, no box, no second pane and
|
|
129
|
+
* no full-screen overlay.
|
|
130
|
+
*
|
|
131
|
+
* The settings screen edits three fields. `Enter` opens the matching picker:
|
|
132
|
+
* - `Model`: single-select with fuzzy search and `No advisor`. Choosing a reasoning model
|
|
133
|
+
* seamlessly transitions to its supported effort levels.
|
|
134
|
+
* - `Reasoning effort`: dynamically tailored to the selected model's reasoning capabilities and
|
|
135
|
+
* `thinkingLevelMap`. Disabled when the model does not support reasoning.
|
|
136
|
+
* - `Fallback models`: multi-select editor matching `/scoped-models` and `/fallback` (`Enter`
|
|
137
|
+
* toggle, `Ctrl+A`/`Ctrl+X`, `Ctrl+P` provider, `Alt+↑/↓` reorder, `#n` rank).
|
|
138
|
+
*
|
|
139
|
+
* `Ctrl+S` persists the whole configuration; unsaved edits are marked `unsaved` in the subtitle.
|
|
140
|
+
*/
|
|
141
|
+
export class AdvisorMenuView extends NativeMenuView {
|
|
142
|
+
private mode: "settings" | "picker" = "settings";
|
|
143
|
+
private pickerKind: PickerKind = "model";
|
|
144
|
+
private query = "";
|
|
145
|
+
private notice?: string;
|
|
146
|
+
private dirty = false;
|
|
147
|
+
private settingsSelected = 0;
|
|
148
|
+
|
|
149
|
+
private selectedModelKey?: string;
|
|
150
|
+
private selectedEffort?: GradedEffort;
|
|
151
|
+
private selectedFallbacks: string[];
|
|
152
|
+
private readonly availableModels: readonly PiModel[];
|
|
153
|
+
private readonly candidates: readonly ModelCandidate[];
|
|
154
|
+
|
|
155
|
+
constructor(private readonly options: AdvisorMenuViewOptions) {
|
|
156
|
+
super({ maxRows: options.maxRows });
|
|
157
|
+
this.selectedModelKey = options.currentModel;
|
|
158
|
+
this.selectedEffort = options.currentEffort;
|
|
159
|
+
this.selectedFallbacks = options.currentFallbacks ? [...options.currentFallbacks] : [];
|
|
160
|
+
this.availableModels = options.availableModels.filter(isModelLike);
|
|
161
|
+
|
|
162
|
+
const seen = new Set<string>();
|
|
163
|
+
const candidates: ModelCandidate[] = [];
|
|
164
|
+
for (const model of this.availableModels) {
|
|
165
|
+
const id = modelKey(model);
|
|
166
|
+
if (seen.has(id)) {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
seen.add(id);
|
|
170
|
+
const name = model.name ? ` ${model.name}` : "";
|
|
171
|
+
const reasoningBadge = model.reasoning ? " (reasoning)" : "";
|
|
172
|
+
candidates.push({
|
|
173
|
+
id,
|
|
174
|
+
label: model.id,
|
|
175
|
+
description: `[${model.provider}]${reasoningBadge}`,
|
|
176
|
+
search: `${model.id} ${model.provider}${name}${reasoningBadge}`,
|
|
177
|
+
provider: model.provider,
|
|
178
|
+
reasoning: Boolean(model.reasoning),
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
candidates.sort((left, right) => left.id.localeCompare(right.id));
|
|
182
|
+
this.candidates = candidates;
|
|
183
|
+
|
|
184
|
+
// Clean up effort if current model does not support it
|
|
185
|
+
const currentModel = this.getSelectedModel();
|
|
186
|
+
if (currentModel && !currentModel.reasoning) {
|
|
187
|
+
this.selectedEffort = undefined;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private getSelectedModel(): PiModel | undefined {
|
|
192
|
+
if (!this.selectedModelKey) {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
return this.availableModels.find((m) => modelKey(m) === this.selectedModelKey);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private getEffortOptions(): readonly EffortOption[] {
|
|
199
|
+
const model = this.getSelectedModel();
|
|
200
|
+
if (!model) {
|
|
201
|
+
if (this.selectedModelKey) {
|
|
202
|
+
return [
|
|
203
|
+
{ label: "model default", description: "use the model's own default" },
|
|
204
|
+
...EFFORT_ORDINAL.map((effort) => ({
|
|
205
|
+
effort,
|
|
206
|
+
label: effort,
|
|
207
|
+
description: effort === DEFAULT_EFFORT ? "recommended" : "",
|
|
208
|
+
})),
|
|
209
|
+
];
|
|
210
|
+
}
|
|
211
|
+
return [];
|
|
212
|
+
}
|
|
213
|
+
const levels = getSupportedEffortLevels(model);
|
|
214
|
+
if (levels.length === 0) {
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
return [
|
|
218
|
+
{ label: "model default", description: "use the model's own default" },
|
|
219
|
+
...levels.map((effort) => ({
|
|
220
|
+
effort,
|
|
221
|
+
label: effort,
|
|
222
|
+
description: effort === DEFAULT_EFFORT ? "recommended" : "",
|
|
223
|
+
})),
|
|
224
|
+
];
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Rows the base class moves through with ↑/↓. */
|
|
228
|
+
protected itemCount(): number {
|
|
229
|
+
if (this.mode === "settings") {
|
|
230
|
+
return SETTINGS.length;
|
|
231
|
+
}
|
|
232
|
+
if (this.pickerKind === "model") {
|
|
233
|
+
return this.visibleModelCandidates().length;
|
|
234
|
+
}
|
|
235
|
+
if (this.pickerKind === "fallback") {
|
|
236
|
+
return this.visibleFallbackCandidates().length;
|
|
237
|
+
}
|
|
238
|
+
return this.getEffortOptions().length;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
protected onSelectionChanged(): void {
|
|
242
|
+
this.notice = undefined;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
protected onClose(): void {
|
|
246
|
+
this.options.onDone?.();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
handleInput(data: string): void {
|
|
250
|
+
if (this.mode === "picker") {
|
|
251
|
+
this.handlePickerInput(data);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (data === "q" || this.matches(data, "tui.select.cancel")) {
|
|
256
|
+
this.close();
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (this.isSave(data) || data === "s" || data === "S") {
|
|
261
|
+
this.save();
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (this.isUp(data)) {
|
|
266
|
+
this.moveSetting(-1);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (this.isDown(data)) {
|
|
271
|
+
this.moveSetting(1);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (data === "\x1b[H") {
|
|
276
|
+
this.select(0);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (data === "\x1b[F") {
|
|
281
|
+
this.select(SETTINGS.length - 1);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (data === "\r" || data === "\n" || this.matches(data, "tui.select.confirm")) {
|
|
286
|
+
this.openPicker();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
render(width: number): string[] {
|
|
291
|
+
const terminalWidth = Math.max(1, Math.floor(width));
|
|
292
|
+
if (terminalWidth < MIN_WIDTH) {
|
|
293
|
+
return [truncate("Advisor needs at least 36 columns. Esc closes.", terminalWidth)];
|
|
294
|
+
}
|
|
295
|
+
const lines = this.mode === "picker" ? this.renderPicker(terminalWidth) : this.renderSettings(terminalWidth);
|
|
296
|
+
return lines.map((line) => truncate(line, terminalWidth));
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// --- settings screen -----------------------------------------------------------------------
|
|
300
|
+
|
|
301
|
+
private renderSettings(width: number): string[] {
|
|
302
|
+
const hints = this.packHints(this.settingsHints(), width);
|
|
303
|
+
const frameRows = 9 + hints.length + (this.notice ? 2 : 0);
|
|
304
|
+
this.maxVisibleRows(frameRows);
|
|
305
|
+
const primary = primaryColumnWidth(SETTINGS.map((setting) => setting.label));
|
|
306
|
+
const lines: string[] = [
|
|
307
|
+
this.rule(width),
|
|
308
|
+
"",
|
|
309
|
+
this.pad(this.paint("accent", boldText(this.theme, "Advisor")), width),
|
|
310
|
+
this.pad(this.settingsSubtitle(), width),
|
|
311
|
+
"",
|
|
312
|
+
];
|
|
313
|
+
|
|
314
|
+
for (let index = 0; index < SETTINGS.length; index++) {
|
|
315
|
+
const setting = SETTINGS[index];
|
|
316
|
+
if (!setting) {
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
const desc = this.settingValue(setting.id);
|
|
320
|
+
const isUnsupported = setting.id === "effort" && desc === "not supported";
|
|
321
|
+
lines.push(
|
|
322
|
+
this.row(width, {
|
|
323
|
+
selected: index === this.selected,
|
|
324
|
+
cursor: "→ ",
|
|
325
|
+
marker: false,
|
|
326
|
+
checked: false,
|
|
327
|
+
label: setting.label,
|
|
328
|
+
description: desc,
|
|
329
|
+
descriptionColor: isUnsupported ? "dim" : "muted",
|
|
330
|
+
primary,
|
|
331
|
+
}),
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (this.notice) {
|
|
336
|
+
lines.push("", this.pad(this.paint("warning", this.notice), width));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
lines.push(
|
|
340
|
+
"",
|
|
341
|
+
...this.chainLines("Fallback chain", this.selectedFallbacks, width),
|
|
342
|
+
"",
|
|
343
|
+
...hints,
|
|
344
|
+
"",
|
|
345
|
+
this.rule(width),
|
|
346
|
+
);
|
|
347
|
+
return lines;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private settingsSubtitle(): string {
|
|
351
|
+
const modelKey = this.selectedModelKey ?? "no advisor";
|
|
352
|
+
const model = this.getSelectedModel();
|
|
353
|
+
const effortText = !this.selectedModelKey
|
|
354
|
+
? "no effort"
|
|
355
|
+
: model && !model.reasoning
|
|
356
|
+
? "effort n/a"
|
|
357
|
+
: `effort ${this.selectedEffort ?? "model default"}`;
|
|
358
|
+
const count = this.selectedFallbacks.length;
|
|
359
|
+
const base = this.paint("dim", `${modelKey} · ${effortText} · ${pluralModels(count)}`);
|
|
360
|
+
return this.dirty ? `${base}${this.paint("warning", " · unsaved")}` : base;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private settingValue(id: SettingId): string {
|
|
364
|
+
if (id === "model") {
|
|
365
|
+
return this.selectedModelKey ?? "No advisor";
|
|
366
|
+
}
|
|
367
|
+
if (id === "effort") {
|
|
368
|
+
const model = this.getSelectedModel();
|
|
369
|
+
if (this.selectedModelKey && model && !model.reasoning) {
|
|
370
|
+
return "not supported";
|
|
371
|
+
}
|
|
372
|
+
if (!this.selectedModelKey) {
|
|
373
|
+
return "not applicable";
|
|
374
|
+
}
|
|
375
|
+
return this.selectedEffort ?? "model default";
|
|
376
|
+
}
|
|
377
|
+
const count = this.selectedFallbacks.length;
|
|
378
|
+
return count === 0 ? "none" : pluralModels(count);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private settingsHints(): string[] {
|
|
382
|
+
return [
|
|
383
|
+
this.hint(this.navLabel(), "setting"),
|
|
384
|
+
this.hint(this.hintLabel("tui.select.confirm", "Enter"), "edit"),
|
|
385
|
+
this.hint(this.hintLabel("app.models.save", "Ctrl+S"), "save"),
|
|
386
|
+
this.hint(this.hintLabel("tui.select.cancel", "Esc"), "close"),
|
|
387
|
+
];
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
private moveSetting(delta: number): void {
|
|
391
|
+
const count = SETTINGS.length;
|
|
392
|
+
if (count === 0) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
this.selected = (this.selected + delta + count) % count;
|
|
396
|
+
this.notice = undefined;
|
|
397
|
+
this.tui?.requestRender();
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// --- picker --------------------------------------------------------------------------------
|
|
401
|
+
|
|
402
|
+
private renderPicker(width: number): string[] {
|
|
403
|
+
const showSearch = this.pickerKind !== "effort";
|
|
404
|
+
const hints = this.packHints(this.pickerHints(), width);
|
|
405
|
+
const frameRows = 10 + hints.length + (this.notice ? 2 : 0);
|
|
406
|
+
const maxVisible = this.maxVisibleRows(frameRows);
|
|
407
|
+
const lines: string[] = [
|
|
408
|
+
this.rule(width),
|
|
409
|
+
"",
|
|
410
|
+
this.pad(this.paint("accent", boldText(this.theme, "Advisor")), width),
|
|
411
|
+
this.pad(this.pickerSubtitle(), width),
|
|
412
|
+
"",
|
|
413
|
+
];
|
|
414
|
+
|
|
415
|
+
if (showSearch) {
|
|
416
|
+
lines.push(this.pad(this.searchLine(), width, 0), "");
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
lines.push(...this.pickerRows(width, maxVisible));
|
|
420
|
+
|
|
421
|
+
if (this.notice) {
|
|
422
|
+
lines.push("", this.pad(this.paint("warning", this.notice), width));
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
lines.push("", ...hints, "", this.rule(width));
|
|
426
|
+
return lines;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
private pickerSubtitle(): string {
|
|
430
|
+
let text: string;
|
|
431
|
+
if (this.pickerKind === "model") {
|
|
432
|
+
text = this.paint("dim", `Model · ${this.selectedModelKey ?? "No advisor"}`);
|
|
433
|
+
} else if (this.pickerKind === "effort") {
|
|
434
|
+
text = this.paint("dim", `Reasoning effort · ${this.selectedEffort ?? "model default"}`);
|
|
435
|
+
} else {
|
|
436
|
+
const stats = this.fallbackStats();
|
|
437
|
+
const parts = ["Fallback models", `${stats.enabled}/${stats.total} models`];
|
|
438
|
+
if (stats.unavailable > 0) {
|
|
439
|
+
parts.push(`${stats.unavailable} unavailable`);
|
|
440
|
+
}
|
|
441
|
+
text = this.paint("dim", parts.join(" · "));
|
|
442
|
+
}
|
|
443
|
+
return this.dirty ? `${text}${this.paint("warning", " · unsaved")}` : text;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
private searchLine(): string {
|
|
447
|
+
return `> ${sanitize(this.query)}${this.paint("accent", "▌")}`;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private pickerRows(width: number, maxVisible: number): string[] {
|
|
451
|
+
if (this.pickerKind === "model") {
|
|
452
|
+
return this.modelRows(width, maxVisible);
|
|
453
|
+
}
|
|
454
|
+
if (this.pickerKind === "effort") {
|
|
455
|
+
return this.effortRows(width, maxVisible);
|
|
456
|
+
}
|
|
457
|
+
return this.fallbackRows(width, maxVisible);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private modelRows(width: number, maxVisible: number): string[] {
|
|
461
|
+
const candidates = this.visibleModelCandidates();
|
|
462
|
+
if (candidates.length === 0) {
|
|
463
|
+
return [this.pad(this.paint("warning", this.emptyMessage()), width)];
|
|
464
|
+
}
|
|
465
|
+
const primary = primaryColumnWidth(candidates.map((candidate) => candidate.label));
|
|
466
|
+
const start = this.windowStart(this.selected, candidates.length, maxVisible);
|
|
467
|
+
const end = Math.min(start + maxVisible, candidates.length);
|
|
468
|
+
const lines: string[] = [];
|
|
469
|
+
for (let index = start; index < end; index++) {
|
|
470
|
+
const candidate = candidates[index];
|
|
471
|
+
if (!candidate) {
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
lines.push(
|
|
475
|
+
this.row(width, {
|
|
476
|
+
selected: index === this.selected,
|
|
477
|
+
cursor: "→ ",
|
|
478
|
+
marker: true,
|
|
479
|
+
checked: this.isCurrentModel(candidate),
|
|
480
|
+
label: candidate.label,
|
|
481
|
+
description: candidate.description,
|
|
482
|
+
descriptionColor: candidate.unavailable ? "warning" : "muted",
|
|
483
|
+
unavailable: candidate.unavailable === true,
|
|
484
|
+
primary,
|
|
485
|
+
}),
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
if (start > 0 || end < candidates.length) {
|
|
489
|
+
lines.push(this.scrollInfo(this.selected + 1, candidates.length, width));
|
|
490
|
+
}
|
|
491
|
+
return lines;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
private effortRows(width: number, maxVisible: number): string[] {
|
|
495
|
+
const options = this.getEffortOptions();
|
|
496
|
+
if (options.length === 0) {
|
|
497
|
+
return [this.pad(this.paint("warning", "Reasoning effort is not supported by this model"), width)];
|
|
498
|
+
}
|
|
499
|
+
const primary = primaryColumnWidth(options.map((option) => option.label));
|
|
500
|
+
const start = this.windowStart(this.selected, options.length, maxVisible);
|
|
501
|
+
const end = Math.min(start + maxVisible, options.length);
|
|
502
|
+
const lines: string[] = [];
|
|
503
|
+
for (let index = start; index < end; index++) {
|
|
504
|
+
const option = options[index];
|
|
505
|
+
if (!option) {
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
lines.push(
|
|
509
|
+
this.row(width, {
|
|
510
|
+
selected: index === this.selected,
|
|
511
|
+
cursor: "→ ",
|
|
512
|
+
marker: true,
|
|
513
|
+
checked: option.effort === this.selectedEffort,
|
|
514
|
+
label: option.label,
|
|
515
|
+
description: option.description,
|
|
516
|
+
primary,
|
|
517
|
+
}),
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
if (start > 0 || end < options.length) {
|
|
521
|
+
lines.push(this.scrollInfo(this.selected + 1, options.length, width));
|
|
522
|
+
}
|
|
523
|
+
return lines;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
private fallbackRows(width: number, maxVisible: number): string[] {
|
|
527
|
+
const candidates = this.visibleFallbackCandidates();
|
|
528
|
+
if (candidates.length === 0) {
|
|
529
|
+
return [this.pad(this.paint("warning", this.emptyMessage()), width)];
|
|
530
|
+
}
|
|
531
|
+
const primary = primaryColumnWidth(candidates.map((candidate) => candidate.label));
|
|
532
|
+
// On narrow frames the provider badge would squeeze the id; keep the rank, which is what the
|
|
533
|
+
// fallback order is about. `[unavailable]` always stays.
|
|
534
|
+
const showProvider = width >= 52;
|
|
535
|
+
const start = this.windowStart(this.selected, candidates.length, maxVisible);
|
|
536
|
+
const end = Math.min(start + maxVisible, candidates.length);
|
|
537
|
+
const chainIndex = new Map(this.selectedFallbacks.map((id, index) => [id, index]));
|
|
538
|
+
const lines: string[] = [];
|
|
539
|
+
for (let index = start; index < end; index++) {
|
|
540
|
+
const candidate = candidates[index];
|
|
541
|
+
if (!candidate) {
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
const rank = chainIndex.get(candidate.id);
|
|
545
|
+
lines.push(
|
|
546
|
+
this.row(width, {
|
|
547
|
+
selected: index === this.selected,
|
|
548
|
+
cursor: "→ ",
|
|
549
|
+
marker: true,
|
|
550
|
+
checked: rank !== undefined,
|
|
551
|
+
label: candidate.label,
|
|
552
|
+
description: candidate.unavailable || showProvider ? candidate.description : "",
|
|
553
|
+
descriptionColor: candidate.unavailable ? "warning" : "muted",
|
|
554
|
+
unavailable: candidate.unavailable === true,
|
|
555
|
+
trailing: rank !== undefined ? ` #${rank + 1}` : "",
|
|
556
|
+
primary,
|
|
557
|
+
}),
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
if (start > 0 || end < candidates.length) {
|
|
561
|
+
lines.push(this.scrollInfo(this.selected + 1, candidates.length, width));
|
|
562
|
+
}
|
|
563
|
+
return lines;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
private emptyMessage(): string {
|
|
567
|
+
return this.query ? `No models match "${sanitize(this.query)}"` : "No models available";
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
private pickerHints(): string[] {
|
|
571
|
+
const nav = this.hint(this.navLabel(), this.pickerKind === "effort" ? "effort" : "model");
|
|
572
|
+
const select = this.hint(
|
|
573
|
+
this.hintLabel("tui.select.confirm", "Enter"),
|
|
574
|
+
this.pickerKind === "fallback" ? "toggle" : "select",
|
|
575
|
+
);
|
|
576
|
+
if (this.pickerKind === "fallback") {
|
|
577
|
+
return [
|
|
578
|
+
nav,
|
|
579
|
+
select,
|
|
580
|
+
this.hint(this.hintLabel("app.models.enableAll", "Ctrl+A"), "all"),
|
|
581
|
+
this.hint(this.hintLabel("app.models.clearAll", "Ctrl+X"), "clear"),
|
|
582
|
+
this.hint(this.hintLabel("app.models.toggleProvider", "Ctrl+P"), "provider"),
|
|
583
|
+
this.hint(this.reorderLabel(), "order"),
|
|
584
|
+
this.hint(this.hintLabel("app.models.save", "Ctrl+S"), "save"),
|
|
585
|
+
this.hint(this.hintLabel("tui.select.cancel", "Esc"), "back"),
|
|
586
|
+
];
|
|
587
|
+
}
|
|
588
|
+
return [nav, select, this.hint(this.hintLabel("tui.select.cancel", "Esc"), "back")];
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// --- candidate lists -----------------------------------------------------------------------
|
|
592
|
+
|
|
593
|
+
private isCurrentModel(candidate: ModelCandidate): boolean {
|
|
594
|
+
if (candidate.id === NO_ADVISOR_VALUE) {
|
|
595
|
+
return this.selectedModelKey === undefined;
|
|
596
|
+
}
|
|
597
|
+
return candidate.id === this.selectedModelKey;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
private visibleModelCandidates(): ModelCandidate[] {
|
|
601
|
+
const known = new Set(this.candidates.map((candidate) => candidate.id));
|
|
602
|
+
const items: ModelCandidate[] = [NO_ADVISOR_CANDIDATE, ...this.candidates];
|
|
603
|
+
if (this.selectedModelKey && !known.has(this.selectedModelKey)) {
|
|
604
|
+
items.push({
|
|
605
|
+
id: this.selectedModelKey,
|
|
606
|
+
label: this.selectedModelKey,
|
|
607
|
+
description: "[unavailable]",
|
|
608
|
+
search: this.selectedModelKey,
|
|
609
|
+
unavailable: true,
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
const query = this.query.trim();
|
|
614
|
+
if (query.length === 0) {
|
|
615
|
+
return items;
|
|
616
|
+
}
|
|
617
|
+
const scored: { candidate: ModelCandidate; score: number; order: number }[] = [];
|
|
618
|
+
items.forEach((candidate, order) => {
|
|
619
|
+
const score = fuzzyScore(candidate.search, query);
|
|
620
|
+
if (score !== undefined) {
|
|
621
|
+
scored.push({ candidate, score, order });
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
scored.sort((left, right) => right.score - left.score || left.order - right.order);
|
|
625
|
+
return scored.map((entry) => entry.candidate);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/** The fallback list: enabled models first, in chain order, then the rest. */
|
|
629
|
+
private visibleFallbackCandidates(): ModelCandidate[] {
|
|
630
|
+
const chainIndex = new Map(this.selectedFallbacks.map((id, index) => [id, index]));
|
|
631
|
+
const known = new Set(this.candidates.map((candidate) => candidate.id));
|
|
632
|
+
const items: ModelCandidate[] = [...this.candidates];
|
|
633
|
+
for (const id of this.selectedFallbacks) {
|
|
634
|
+
if (!known.has(id)) {
|
|
635
|
+
items.push({ id, label: id, description: "[unavailable]", search: id, unavailable: true });
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
const query = this.query.trim();
|
|
640
|
+
if (query.length === 0) {
|
|
641
|
+
return items.sort((left, right) => {
|
|
642
|
+
const leftRank = chainIndex.get(left.id);
|
|
643
|
+
const rightRank = chainIndex.get(right.id);
|
|
644
|
+
if (leftRank !== undefined && rightRank !== undefined) return leftRank - rightRank;
|
|
645
|
+
if (leftRank !== undefined) return -1;
|
|
646
|
+
if (rightRank !== undefined) return 1;
|
|
647
|
+
return 0;
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const scored: { candidate: ModelCandidate; score: number; order: number }[] = [];
|
|
652
|
+
items.forEach((candidate, order) => {
|
|
653
|
+
const score = fuzzyScore(candidate.search, query);
|
|
654
|
+
if (score !== undefined) {
|
|
655
|
+
scored.push({ candidate, score, order });
|
|
656
|
+
}
|
|
657
|
+
});
|
|
658
|
+
scored.sort((left, right) => {
|
|
659
|
+
const leftRank = chainIndex.get(left.candidate.id);
|
|
660
|
+
const rightRank = chainIndex.get(right.candidate.id);
|
|
661
|
+
if (leftRank !== undefined && rightRank !== undefined) return leftRank - rightRank;
|
|
662
|
+
if (leftRank !== undefined) return -1;
|
|
663
|
+
if (rightRank !== undefined) return 1;
|
|
664
|
+
return right.score - left.score || left.order - right.order;
|
|
665
|
+
});
|
|
666
|
+
return scored.map((entry) => entry.candidate);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
private fallbackStats(): { enabled: number; total: number; unavailable: number } {
|
|
670
|
+
const known = new Set(this.candidates.map((candidate) => candidate.id));
|
|
671
|
+
let enabled = 0;
|
|
672
|
+
let unavailable = 0;
|
|
673
|
+
for (const id of this.selectedFallbacks) {
|
|
674
|
+
if (known.has(id)) {
|
|
675
|
+
enabled++;
|
|
676
|
+
} else {
|
|
677
|
+
unavailable++;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
return { enabled, total: this.candidates.length, unavailable };
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
// --- picker input --------------------------------------------------------------------------
|
|
684
|
+
|
|
685
|
+
private handlePickerInput(data: string): void {
|
|
686
|
+
if (data === "\x1b" || data === "\x03" || this.matches(data, "tui.select.cancel")) {
|
|
687
|
+
this.leavePickerStep();
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (this.isSave(data)) {
|
|
692
|
+
this.save();
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
if (this.pickerKind === "fallback") {
|
|
697
|
+
if (data === "\x01" || this.matches(data, "app.models.enableAll")) {
|
|
698
|
+
this.enableAll();
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
if (data === "\x18" || this.matches(data, "app.models.clearAll")) {
|
|
702
|
+
this.clearAll();
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
if (data === "\x10" || this.matches(data, "app.models.toggleProvider")) {
|
|
706
|
+
this.toggleProvider();
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
if (this.isReorderUp(data)) {
|
|
710
|
+
this.reorder(-1);
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
if (this.isReorderDown(data)) {
|
|
714
|
+
this.reorder(1);
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
if (this.pickerKind !== "effort" && (data === "\x7f" || data === "\b")) {
|
|
720
|
+
if (this.query.length > 0) {
|
|
721
|
+
this.query = this.query.slice(0, -1);
|
|
722
|
+
this.selected = 0;
|
|
723
|
+
}
|
|
724
|
+
this.tui?.requestRender();
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// Wheel moves the selection like a native SelectList; printable keys stay in the search field.
|
|
729
|
+
const wheel = mouseWheelDirection(data);
|
|
730
|
+
if (wheel !== undefined) {
|
|
731
|
+
this.movePicker(wheel);
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// Arrow keys (and any non-printable binding) drive the list; printable keys are the search field.
|
|
736
|
+
if (!isPrintable(data) && (this.matches(data, "tui.select.up") || this.isArrowUp(data))) {
|
|
737
|
+
this.movePicker(-1);
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
if (!isPrintable(data) && (this.matches(data, "tui.select.down") || this.isArrowDown(data))) {
|
|
742
|
+
this.movePicker(1);
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
if (data === "\x1b[H") {
|
|
747
|
+
this.selected = 0;
|
|
748
|
+
this.tui?.requestRender();
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
if (data === "\x1b[F") {
|
|
753
|
+
this.selected = Math.max(0, this.itemCount() - 1);
|
|
754
|
+
this.tui?.requestRender();
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
if (data === "\x1b[5~" || (!isPrintable(data) && this.matches(data, "tui.select.pageUp"))) {
|
|
759
|
+
this.movePicker(-this.pageStep());
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
if (data === "\x1b[6~" || (!isPrintable(data) && this.matches(data, "tui.select.pageDown"))) {
|
|
764
|
+
this.movePicker(this.pageStep());
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
if (data === "\r" || data === "\n" || this.matches(data, "tui.select.confirm")) {
|
|
769
|
+
this.choose();
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
if (this.pickerKind !== "effort" && isPrintable(data)) {
|
|
774
|
+
this.query += data;
|
|
775
|
+
this.selected = 0;
|
|
776
|
+
this.tui?.requestRender();
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
private isReorderUp(data: string): boolean {
|
|
781
|
+
return data === "\x1b[1;3A" || this.matches(data, "app.models.reorderUp");
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
private isReorderDown(data: string): boolean {
|
|
785
|
+
return data === "\x1b[1;3B" || this.matches(data, "app.models.reorderDown");
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
private isSave(data: string): boolean {
|
|
789
|
+
return data === "\x13" || this.matches(data, "app.models.save");
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
private leavePickerStep(): void {
|
|
793
|
+
if (this.pickerKind !== "effort" && this.query.length > 0) {
|
|
794
|
+
this.query = "";
|
|
795
|
+
this.selected = 0;
|
|
796
|
+
} else {
|
|
797
|
+
this.mode = "settings";
|
|
798
|
+
this.selected = this.settingsSelected;
|
|
799
|
+
this.notice = undefined;
|
|
800
|
+
}
|
|
801
|
+
this.tui?.requestRender();
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
private movePicker(delta: number): void {
|
|
805
|
+
const count = this.itemCount();
|
|
806
|
+
if (count === 0) {
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
this.selected = (this.selected + delta + count) % count;
|
|
810
|
+
this.notice = undefined;
|
|
811
|
+
this.tui?.requestRender();
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
private choose(): void {
|
|
815
|
+
if (this.pickerKind === "model") {
|
|
816
|
+
const candidate = this.visibleModelCandidates()[this.selected];
|
|
817
|
+
if (!candidate) {
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (candidate.id === NO_ADVISOR_VALUE) {
|
|
821
|
+
this.selectedModelKey = undefined;
|
|
822
|
+
this.selectedEffort = undefined;
|
|
823
|
+
this.finishChoosing();
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
this.selectedModelKey = candidate.id;
|
|
827
|
+
const model = this.getSelectedModel();
|
|
828
|
+
if (!model?.reasoning) {
|
|
829
|
+
// Non-reasoning model: reasoning effort is not supported
|
|
830
|
+
this.selectedEffort = undefined;
|
|
831
|
+
this.finishChoosing();
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// Validate current effort against the new model's supported levels
|
|
836
|
+
const supported = getSupportedEffortLevels(model);
|
|
837
|
+
if (this.selectedEffort && !supported.includes(this.selectedEffort)) {
|
|
838
|
+
this.selectedEffort = undefined;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
// Seamlessly transition to choosing effort for this reasoning model
|
|
842
|
+
this.pickerKind = "effort";
|
|
843
|
+
this.query = "";
|
|
844
|
+
const options = this.getEffortOptions();
|
|
845
|
+
const index = options.findIndex((opt) => opt.effort === this.selectedEffort);
|
|
846
|
+
this.selected = index >= 0 ? index : 0;
|
|
847
|
+
this.notice = undefined;
|
|
848
|
+
this.dirty = true;
|
|
849
|
+
this.tui?.requestRender();
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
if (this.pickerKind === "effort") {
|
|
854
|
+
const options = this.getEffortOptions();
|
|
855
|
+
const option = options[this.selected];
|
|
856
|
+
if (!option) {
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
this.selectedEffort = option.effort;
|
|
860
|
+
this.finishChoosing();
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
this.toggleSelected();
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
private finishChoosing(): void {
|
|
868
|
+
this.dirty = true;
|
|
869
|
+
this.notice = undefined;
|
|
870
|
+
this.mode = "settings";
|
|
871
|
+
this.selected = this.settingsSelected;
|
|
872
|
+
this.query = "";
|
|
873
|
+
this.tui?.requestRender();
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
private currentFallbackCandidate(): ModelCandidate | undefined {
|
|
877
|
+
return this.visibleFallbackCandidates()[this.selected];
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
private toggleSelected(): void {
|
|
881
|
+
const candidate = this.currentFallbackCandidate();
|
|
882
|
+
if (!candidate) {
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
const at = this.selectedFallbacks.indexOf(candidate.id);
|
|
886
|
+
if (at >= 0) {
|
|
887
|
+
this.selectedFallbacks.splice(at, 1);
|
|
888
|
+
} else if (!candidate.unavailable) {
|
|
889
|
+
this.selectedFallbacks.push(candidate.id);
|
|
890
|
+
}
|
|
891
|
+
this.dirty = true;
|
|
892
|
+
this.notice = undefined;
|
|
893
|
+
this.keepSelectionOn(candidate.id);
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
private keepSelectionOn(id: string | undefined): void {
|
|
897
|
+
const candidates = this.visibleFallbackCandidates();
|
|
898
|
+
const index = id === undefined ? -1 : candidates.findIndex((candidate) => candidate.id === id);
|
|
899
|
+
this.selected = index >= 0 ? index : Math.min(this.selected, Math.max(0, candidates.length - 1));
|
|
900
|
+
this.tui?.requestRender();
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/** Enable every candidate in the filtered list, or every candidate when the search is empty. */
|
|
904
|
+
private enableAll(): void {
|
|
905
|
+
const current = this.currentFallbackCandidate()?.id;
|
|
906
|
+
const targets = this.query.trim() ? this.visibleFallbackCandidates() : this.candidates;
|
|
907
|
+
for (const candidate of targets) {
|
|
908
|
+
if (!candidate.unavailable && !this.selectedFallbacks.includes(candidate.id)) {
|
|
909
|
+
this.selectedFallbacks.push(candidate.id);
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
this.dirty = true;
|
|
913
|
+
this.notice = undefined;
|
|
914
|
+
this.keepSelectionOn(current);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/** Remove every candidate in the filtered list, or the whole chain when the search is empty. */
|
|
918
|
+
private clearAll(): void {
|
|
919
|
+
const current = this.currentFallbackCandidate()?.id;
|
|
920
|
+
if (this.query.trim()) {
|
|
921
|
+
const targets = new Set(this.visibleFallbackCandidates().map((candidate) => candidate.id));
|
|
922
|
+
this.selectedFallbacks = this.selectedFallbacks.filter((id) => !targets.has(id));
|
|
923
|
+
} else {
|
|
924
|
+
this.selectedFallbacks = [];
|
|
925
|
+
}
|
|
926
|
+
this.dirty = true;
|
|
927
|
+
this.notice = undefined;
|
|
928
|
+
this.keepSelectionOn(current);
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
private toggleProvider(): void {
|
|
932
|
+
const candidate = this.currentFallbackCandidate();
|
|
933
|
+
if (!candidate?.provider || candidate.unavailable) {
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
const provider = candidate.provider;
|
|
937
|
+
const ids = this.candidates.filter((entry) => entry.provider === provider).map((entry) => entry.id);
|
|
938
|
+
const allEnabled = ids.every((id) => this.selectedFallbacks.includes(id));
|
|
939
|
+
if (allEnabled) {
|
|
940
|
+
const remove = new Set(ids);
|
|
941
|
+
this.selectedFallbacks = this.selectedFallbacks.filter((id) => !remove.has(id));
|
|
942
|
+
} else {
|
|
943
|
+
for (const id of ids) {
|
|
944
|
+
if (!this.selectedFallbacks.includes(id)) {
|
|
945
|
+
this.selectedFallbacks.push(id);
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
this.dirty = true;
|
|
950
|
+
this.notice = undefined;
|
|
951
|
+
this.keepSelectionOn(candidate.id);
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
private reorder(delta: number): void {
|
|
955
|
+
const candidate = this.currentFallbackCandidate();
|
|
956
|
+
if (!candidate) {
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
const at = this.selectedFallbacks.indexOf(candidate.id);
|
|
960
|
+
if (at < 0) {
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
const to = at + delta;
|
|
964
|
+
if (to < 0 || to >= this.selectedFallbacks.length) {
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
const [moved] = this.selectedFallbacks.splice(at, 1);
|
|
968
|
+
if (moved !== undefined) {
|
|
969
|
+
this.selectedFallbacks.splice(to, 0, moved);
|
|
970
|
+
}
|
|
971
|
+
this.dirty = true;
|
|
972
|
+
this.notice = undefined;
|
|
973
|
+
this.keepSelectionOn(candidate.id);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
private openPicker(): void {
|
|
977
|
+
this.settingsSelected = this.selected;
|
|
978
|
+
const setting = SETTINGS[this.selected];
|
|
979
|
+
if (!setting) {
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
if (setting.id === "effort") {
|
|
983
|
+
const model = this.getSelectedModel();
|
|
984
|
+
if (!this.selectedModelKey) {
|
|
985
|
+
this.notice = "Select an advisor model first.";
|
|
986
|
+
this.tui?.requestRender();
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
if (model && !model.reasoning) {
|
|
990
|
+
this.notice = `${model.id} does not support reasoning effort.`;
|
|
991
|
+
this.tui?.requestRender();
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
const options = this.getEffortOptions();
|
|
995
|
+
if (options.length === 0) {
|
|
996
|
+
this.notice = "No reasoning effort levels available for this model.";
|
|
997
|
+
this.tui?.requestRender();
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
1000
|
+
this.pickerKind = "effort";
|
|
1001
|
+
const index = options.findIndex((option) => option.effort === this.selectedEffort);
|
|
1002
|
+
this.selected = index >= 0 ? index : 0;
|
|
1003
|
+
} else if (setting.id === "fallbacks") {
|
|
1004
|
+
this.pickerKind = "fallback";
|
|
1005
|
+
this.selected = 0;
|
|
1006
|
+
} else {
|
|
1007
|
+
this.pickerKind = "model";
|
|
1008
|
+
this.selected = 0;
|
|
1009
|
+
}
|
|
1010
|
+
this.mode = "picker";
|
|
1011
|
+
this.query = "";
|
|
1012
|
+
this.notice = undefined;
|
|
1013
|
+
this.tui?.requestRender();
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
// --- actions -------------------------------------------------------------------------------
|
|
1017
|
+
|
|
1018
|
+
private save(): void {
|
|
1019
|
+
try {
|
|
1020
|
+
this.options.onSave({
|
|
1021
|
+
modelKey: this.selectedModelKey ?? null,
|
|
1022
|
+
effort: this.selectedEffort ?? null,
|
|
1023
|
+
fallbackModels: [...this.selectedFallbacks],
|
|
1024
|
+
});
|
|
1025
|
+
this.dirty = false;
|
|
1026
|
+
this.notice = "Saved advisor configuration";
|
|
1027
|
+
} catch (err) {
|
|
1028
|
+
this.notice = `Save failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
1029
|
+
}
|
|
1030
|
+
this.tui?.requestRender();
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/**
|
|
1035
|
+
* Encapsulated runner that collects session context, mounts the AdvisorMenuView,
|
|
1036
|
+
* reconciles runtime state on save, and handles user notifications.
|
|
1037
|
+
*/
|
|
1038
|
+
export async function openAdvisorMenu(
|
|
1039
|
+
pi: PiExtensionAPI,
|
|
1040
|
+
ctx: PiContext,
|
|
1041
|
+
options: AdvisorPluginOptions = {},
|
|
1042
|
+
): Promise<void> {
|
|
1043
|
+
if (typeof ctx.ui?.custom !== "function") {
|
|
1044
|
+
ctx.ui?.notify?.("Interactive advisor menu requires UI support.", "error");
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
const sessionId = getSessionId(ctx, "default");
|
|
1049
|
+
const cwd = options.cwd ?? ctx.cwd;
|
|
1050
|
+
const workspaceTrusted = ctx.isProjectTrusted?.() === true;
|
|
1051
|
+
const persistOptions = {
|
|
1052
|
+
workspaceTrusted,
|
|
1053
|
+
...(options.homeDir ? { homeDir: options.homeDir } : {}),
|
|
1054
|
+
...(options.env ? { env: options.env } : {}),
|
|
1055
|
+
};
|
|
1056
|
+
|
|
1057
|
+
const available = ctx.modelRegistry?.getAvailable?.();
|
|
1058
|
+
const availableModels = available && available.length > 0 ? available : (ctx.modelRegistry?.getAll?.() ?? []);
|
|
1059
|
+
const current = getAdvisorModel();
|
|
1060
|
+
|
|
1061
|
+
const subagentsService = getService<SubagentsService>(SUBAGENTS_CONTRACT_ID, sessionId);
|
|
1062
|
+
const subagents = subagentsService?.listAgents?.() ?? [];
|
|
1063
|
+
|
|
1064
|
+
const view = new AdvisorMenuView({
|
|
1065
|
+
currentModel: current ? modelKey(current) : undefined,
|
|
1066
|
+
currentEffort: getAdvisorEffort(),
|
|
1067
|
+
currentFallbacks: getAdvisorFallbacks(),
|
|
1068
|
+
availableModels,
|
|
1069
|
+
subagents,
|
|
1070
|
+
onSave: (result) => {
|
|
1071
|
+
if (!saveAdvisorConfig(cwd, result, persistOptions)) {
|
|
1072
|
+
throw new Error("could not write the advisor config");
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
if (result.modelKey === null) {
|
|
1076
|
+
clearAdvisorState();
|
|
1077
|
+
reconcileAdvisorTool(pi, ctx, { blocked: true });
|
|
1078
|
+
ctx.ui.notify(MSG_ADVISOR_DISABLED, "info");
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
if (result.modelKey) {
|
|
1083
|
+
const picked = resolveModel<PiModel>(result.modelKey, ctx.modelRegistry);
|
|
1084
|
+
if (picked) {
|
|
1085
|
+
setAdvisorModel(picked);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
if (result.effort !== undefined) {
|
|
1090
|
+
setAdvisorEffort(result.effort ?? undefined);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
if (result.fallbackModels !== undefined) {
|
|
1094
|
+
setAdvisorFallbacks(result.fallbackModels);
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
const advisor = getAdvisorModel();
|
|
1098
|
+
const blocked = isExecutorBlocked(ctx, pi.getThinkingLevel?.());
|
|
1099
|
+
reconcileAdvisorTool(pi, ctx, { blocked });
|
|
1100
|
+
|
|
1101
|
+
if (advisor) {
|
|
1102
|
+
const key = modelKey(advisor);
|
|
1103
|
+
ctx.ui.notify(
|
|
1104
|
+
blocked
|
|
1105
|
+
? msgAdvisorEnabledInactive(key, getAdvisorEffort())
|
|
1106
|
+
: msgAdvisorEnabled(key, getAdvisorEffort(), getAdvisorFallbacks()),
|
|
1107
|
+
"info",
|
|
1108
|
+
);
|
|
1109
|
+
}
|
|
1110
|
+
},
|
|
1111
|
+
});
|
|
1112
|
+
|
|
1113
|
+
await ctx.ui.custom((tui, theme, kb, done) => {
|
|
1114
|
+
view.attach(tui, theme, kb, done);
|
|
1115
|
+
return view;
|
|
1116
|
+
});
|
|
1117
|
+
}
|