@restranger/repi-model-fallback 0.0.0-stage → 0.0.1-ca9b82c
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 +70 -2
- package/package.json +43 -5
- package/src/config.ts +230 -0
- package/src/format.ts +10 -0
- package/src/index.ts +5 -0
- package/src/menu-ui.ts +584 -0
- package/src/plugin.ts +375 -0
- package/src/resolver.ts +37 -0
- package/src/types.ts +31 -0
package/src/menu-ui.ts
ADDED
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
import { ListOverlay, type PiModel, type SubagentSummary, mouseWheelDirection } from "@restranger/repi-lib";
|
|
2
|
+
import { alignRight, boldText, fitWidth, sanitize, truncate, wrapDisplay } from "./format.js";
|
|
3
|
+
|
|
4
|
+
export interface FallbackAgentEntry {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly isMain: boolean;
|
|
8
|
+
readonly primaryModel: string;
|
|
9
|
+
fallbackModels: string[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface FallbackMenuViewOptions {
|
|
13
|
+
readonly mainModel?: PiModel;
|
|
14
|
+
readonly mainFallbacks: readonly string[];
|
|
15
|
+
readonly subagents: readonly SubagentSummary[];
|
|
16
|
+
readonly availableModels: readonly PiModel[];
|
|
17
|
+
readonly onSave: (entries: readonly FallbackAgentEntry[]) => void;
|
|
18
|
+
readonly onDone?: () => void;
|
|
19
|
+
readonly maxRows?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const MIN_WIDTH = 36;
|
|
23
|
+
const MIN_ROWS = 8;
|
|
24
|
+
const SEARCH_CHAR_PATTERN = /^[a-zA-Z0-9\-_./:]+$/;
|
|
25
|
+
const MAIN_AGENT_GLYPH = "\udb81\udea9";
|
|
26
|
+
|
|
27
|
+
export class FallbackMenuView extends ListOverlay {
|
|
28
|
+
private rosterScroll = 0;
|
|
29
|
+
private selectedPickerIndex = 0;
|
|
30
|
+
private pickerScroll = 0;
|
|
31
|
+
private lastOutputBodyHeight = 0;
|
|
32
|
+
private mode: "roster" | "pick_model" = "roster";
|
|
33
|
+
private searchQuery = "";
|
|
34
|
+
private notice?: string;
|
|
35
|
+
|
|
36
|
+
private readonly agents: FallbackAgentEntry[];
|
|
37
|
+
private readonly availableModelNames: readonly string[];
|
|
38
|
+
|
|
39
|
+
constructor(private readonly options: FallbackMenuViewOptions) {
|
|
40
|
+
super({ maxRows: options.maxRows });
|
|
41
|
+
const mainModelName = options.mainModel
|
|
42
|
+
? `${options.mainModel.provider}/${options.mainModel.id}`
|
|
43
|
+
: "default parent model";
|
|
44
|
+
|
|
45
|
+
const mainEntry: FallbackAgentEntry = {
|
|
46
|
+
id: "main",
|
|
47
|
+
name: "Main Agent",
|
|
48
|
+
isMain: true,
|
|
49
|
+
primaryModel: mainModelName,
|
|
50
|
+
fallbackModels: [...options.mainFallbacks],
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const subagentEntries: FallbackAgentEntry[] = options.subagents.map((sub) => ({
|
|
54
|
+
id: sub.name,
|
|
55
|
+
name: sub.name,
|
|
56
|
+
isMain: false,
|
|
57
|
+
primaryModel: sub.model ?? "inherit",
|
|
58
|
+
fallbackModels: [...(sub.fallbackModels ?? [])],
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
this.agents = [mainEntry, ...subagentEntries];
|
|
62
|
+
|
|
63
|
+
const modelNames = options.availableModels.map((m) => `${m.provider}/${m.id}`);
|
|
64
|
+
this.availableModelNames = [...new Set(modelNames)].sort();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Number of rows the base class moves through with ↑/↓. */
|
|
68
|
+
protected itemCount(): number {
|
|
69
|
+
return this.agents.length;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Selecting another agent clears the transient notice, as before. */
|
|
73
|
+
protected onSelectionChanged(): void {
|
|
74
|
+
this.notice = undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
protected onClose(): void {
|
|
78
|
+
this.options.onDone?.();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
refresh(): void {
|
|
82
|
+
super.refresh();
|
|
83
|
+
this.clampPickerSelection();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
handleInput(data: string): void {
|
|
87
|
+
if (this.mode === "pick_model") {
|
|
88
|
+
this.handlePickerInput(data);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (data === "q" || this.matches(data, "tui.select.cancel")) {
|
|
93
|
+
this.close();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (this.matches(data, "tui.select.up") || data === "\x1b[A" || data === "\x1bOA" || data === "k") {
|
|
98
|
+
this.move(-1);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (this.matches(data, "tui.select.down") || data === "\x1b[B" || data === "\x1bOB" || data === "j") {
|
|
103
|
+
this.move(1);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (data === "\x1b[H" || data === "g") {
|
|
108
|
+
this.select(0);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (data === "\x1b[F" || data === "G") {
|
|
113
|
+
this.select(this.agents.length - 1);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (data === "K" || this.isScrollUp(data)) {
|
|
118
|
+
this.scrollDetail(-1);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (data === "J" || this.isScrollDown(data)) {
|
|
123
|
+
this.scrollDetail(1);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (data === "\x1b[5~" || this.matches(data, "tui.select.pageUp")) {
|
|
128
|
+
this.scrollDetail(-Math.max(1, this.lastOutputBodyHeight || 5));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (data === "\x1b[6~" || this.matches(data, "tui.select.pageDown")) {
|
|
133
|
+
this.scrollDetail(Math.max(1, this.lastOutputBodyHeight || 5));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (data === "a" || data === "A" || data === "\r" || data === "\n" || this.matches(data, "tui.select.confirm")) {
|
|
138
|
+
this.mode = "pick_model";
|
|
139
|
+
this.searchQuery = "";
|
|
140
|
+
this.selectedPickerIndex = 0;
|
|
141
|
+
this.pickerScroll = 0;
|
|
142
|
+
this.notice = undefined;
|
|
143
|
+
this.tui?.requestRender();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (data === "d" || data === "D" || data === "\x7f") {
|
|
148
|
+
this.removeSelectedFallback();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (data === "s" || data === "S") {
|
|
153
|
+
this.save();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private handlePickerInput(data: string): void {
|
|
159
|
+
if (data === "\x1b" || (!SEARCH_CHAR_PATTERN.test(data) && this.matches(data, "tui.select.cancel"))) {
|
|
160
|
+
if (this.searchQuery.length > 0) {
|
|
161
|
+
this.searchQuery = "";
|
|
162
|
+
this.clampPickerSelection();
|
|
163
|
+
} else {
|
|
164
|
+
this.mode = "roster";
|
|
165
|
+
this.pickerScroll = 0;
|
|
166
|
+
this.notice = undefined;
|
|
167
|
+
}
|
|
168
|
+
this.tui?.requestRender();
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (data === "\x7f" || data === "\b") {
|
|
173
|
+
if (this.searchQuery.length > 0) {
|
|
174
|
+
this.searchQuery = this.searchQuery.slice(0, -1);
|
|
175
|
+
this.clampPickerSelection();
|
|
176
|
+
}
|
|
177
|
+
this.tui?.requestRender();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (this.isScrollUp(data)) {
|
|
182
|
+
this.movePicker(-1, false);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (this.isScrollDown(data)) {
|
|
187
|
+
this.movePicker(1, false);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (
|
|
192
|
+
data === "\x1b[A" ||
|
|
193
|
+
data === "\x1bOA" ||
|
|
194
|
+
(this.searchQuery.length === 0 && (data === "k" || data === "K")) ||
|
|
195
|
+
(!SEARCH_CHAR_PATTERN.test(data) && this.matches(data, "tui.select.up"))
|
|
196
|
+
) {
|
|
197
|
+
this.movePicker(-1, true);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (
|
|
202
|
+
data === "\x1b[B" ||
|
|
203
|
+
data === "\x1bOB" ||
|
|
204
|
+
(this.searchQuery.length === 0 && (data === "j" || data === "J")) ||
|
|
205
|
+
(!SEARCH_CHAR_PATTERN.test(data) && this.matches(data, "tui.select.down"))
|
|
206
|
+
) {
|
|
207
|
+
this.movePicker(1, true);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (data === "\x1b[5~" || (!SEARCH_CHAR_PATTERN.test(data) && this.matches(data, "tui.select.pageUp"))) {
|
|
212
|
+
const step = Math.max(1, (this.lastOutputBodyHeight || 5) - 1);
|
|
213
|
+
this.movePicker(-step, false);
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (data === "\x1b[6~" || (!SEARCH_CHAR_PATTERN.test(data) && this.matches(data, "tui.select.pageDown"))) {
|
|
218
|
+
const step = Math.max(1, (this.lastOutputBodyHeight || 5) - 1);
|
|
219
|
+
this.movePicker(step, false);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (data === "\x1b[H" || (this.searchQuery.length === 0 && data === "g")) {
|
|
224
|
+
this.selectPicker(0);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (data === "\x1b[F" || (this.searchQuery.length === 0 && data === "G")) {
|
|
229
|
+
const count = this.pickerCandidates().length;
|
|
230
|
+
this.selectPicker(Math.max(0, count - 1));
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (
|
|
235
|
+
data === "\r" ||
|
|
236
|
+
data === "\n" ||
|
|
237
|
+
(!SEARCH_CHAR_PATTERN.test(data) && this.matches(data, "tui.select.confirm"))
|
|
238
|
+
) {
|
|
239
|
+
const candidates = this.pickerCandidates();
|
|
240
|
+
const picked = candidates[this.selectedPickerIndex];
|
|
241
|
+
if (picked) {
|
|
242
|
+
const current = this.agents[this.selected];
|
|
243
|
+
if (current) {
|
|
244
|
+
if (!current.fallbackModels.includes(picked)) {
|
|
245
|
+
current.fallbackModels.push(picked);
|
|
246
|
+
this.notice = `Added "${picked}" to ${current.isMain ? "main agent" : current.name}`;
|
|
247
|
+
} else {
|
|
248
|
+
this.notice = `Model "${picked}" is already in fallback chain`;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
this.mode = "roster";
|
|
252
|
+
this.pickerScroll = 0;
|
|
253
|
+
this.searchQuery = "";
|
|
254
|
+
}
|
|
255
|
+
this.tui?.requestRender();
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (SEARCH_CHAR_PATTERN.test(data)) {
|
|
260
|
+
this.searchQuery += data;
|
|
261
|
+
this.clampPickerSelection();
|
|
262
|
+
this.tui?.requestRender();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private pickerCandidates(): string[] {
|
|
268
|
+
const current = this.agents[this.selected];
|
|
269
|
+
const candidates = [...this.availableModelNames];
|
|
270
|
+
if (current && !current.isMain) {
|
|
271
|
+
candidates.unshift("inherit");
|
|
272
|
+
}
|
|
273
|
+
if (!this.searchQuery) {
|
|
274
|
+
return candidates;
|
|
275
|
+
}
|
|
276
|
+
const query = this.searchQuery.toLowerCase();
|
|
277
|
+
return candidates.filter((name) => name.toLowerCase().includes(query));
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private clampPickerSelection(): void {
|
|
281
|
+
const count = this.pickerCandidates().length;
|
|
282
|
+
if (count <= 0) {
|
|
283
|
+
this.selectedPickerIndex = 0;
|
|
284
|
+
} else {
|
|
285
|
+
this.selectedPickerIndex = Math.max(0, Math.min(this.selectedPickerIndex, count - 1));
|
|
286
|
+
}
|
|
287
|
+
this.ensurePickerVisible();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private movePicker(delta: number, wrap: boolean): void {
|
|
291
|
+
const candidates = this.pickerCandidates();
|
|
292
|
+
if (candidates.length === 0) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (wrap) {
|
|
296
|
+
this.selectedPickerIndex = (this.selectedPickerIndex + delta + candidates.length) % candidates.length;
|
|
297
|
+
} else {
|
|
298
|
+
this.selectedPickerIndex = Math.max(0, Math.min(this.selectedPickerIndex + delta, candidates.length - 1));
|
|
299
|
+
}
|
|
300
|
+
this.ensurePickerVisible();
|
|
301
|
+
this.tui?.requestRender();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private selectPicker(index: number): void {
|
|
305
|
+
const candidates = this.pickerCandidates();
|
|
306
|
+
if (candidates.length === 0) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
this.selectedPickerIndex = Math.max(0, Math.min(index, candidates.length - 1));
|
|
310
|
+
this.ensurePickerVisible();
|
|
311
|
+
this.tui?.requestRender();
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private ensurePickerVisible(outputBodyHeight?: number): void {
|
|
315
|
+
const height = outputBodyHeight ?? this.lastOutputBodyHeight;
|
|
316
|
+
if (height <= 0) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const count = this.pickerCandidates().length;
|
|
320
|
+
const maxScroll = Math.max(0, count - height);
|
|
321
|
+
if (this.selectedPickerIndex >= this.pickerScroll + height) {
|
|
322
|
+
this.pickerScroll = this.selectedPickerIndex - height + 1;
|
|
323
|
+
}
|
|
324
|
+
if (this.selectedPickerIndex < this.pickerScroll) {
|
|
325
|
+
this.pickerScroll = this.selectedPickerIndex;
|
|
326
|
+
}
|
|
327
|
+
this.pickerScroll = Math.max(0, Math.min(this.pickerScroll, maxScroll));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private removeSelectedFallback(): void {
|
|
331
|
+
const current = this.agents[this.selected];
|
|
332
|
+
if (!current || current.fallbackModels.length === 0) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const removed = current.fallbackModels.pop();
|
|
336
|
+
this.notice = removed ? `Removed "${removed}"` : undefined;
|
|
337
|
+
this.tui?.requestRender();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private save(): void {
|
|
341
|
+
try {
|
|
342
|
+
this.options.onSave(this.agents);
|
|
343
|
+
this.notice = "Saved fallback configuration!";
|
|
344
|
+
} catch (err) {
|
|
345
|
+
this.notice = `Save failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
346
|
+
}
|
|
347
|
+
this.tui?.requestRender();
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
invalidate(): void {
|
|
351
|
+
super.invalidate();
|
|
352
|
+
this.clampPickerSelection();
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
render(width: number): string[] {
|
|
356
|
+
const terminalWidth = Math.max(1, Math.floor(width));
|
|
357
|
+
if (terminalWidth < MIN_WIDTH) {
|
|
358
|
+
return [truncate("Model fallback manager needs at least 36 columns. Esc closes.", terminalWidth)];
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const rows = this.rowsBudget();
|
|
362
|
+
if (rows < MIN_ROWS) {
|
|
363
|
+
return [truncate(`Model fallback manager needs at least ${MIN_ROWS} terminal rows. Esc closes.`, terminalWidth)];
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const bodyHeight = this.bodyHeightFor(rows);
|
|
367
|
+
const rosterWidth = Math.max(22, Math.min(46, Math.floor((terminalWidth - 1) * 0.38)));
|
|
368
|
+
const detailWidth = Math.max(1, terminalWidth - rosterWidth - 1);
|
|
369
|
+
|
|
370
|
+
const selected = this.agents[this.selected];
|
|
371
|
+
const detail = this.detailSections(selected, detailWidth, bodyHeight);
|
|
372
|
+
const header = detail.header.slice(0, Math.max(0, bodyHeight - 1));
|
|
373
|
+
const outputBodyHeight = Math.max(0, bodyHeight - header.length);
|
|
374
|
+
this.lastOutputBodyHeight = outputBodyHeight;
|
|
375
|
+
|
|
376
|
+
const wrappedPerItem = detail.body.map((line) => wrapDisplay(line, Math.max(1, detailWidth)));
|
|
377
|
+
const wrapped = wrappedPerItem.flat();
|
|
378
|
+
|
|
379
|
+
let targetLine: number | undefined;
|
|
380
|
+
let targetEndLine: number | undefined;
|
|
381
|
+
if (this.mode === "pick_model") {
|
|
382
|
+
let currentLine = 0;
|
|
383
|
+
for (let i = 0; i < detail.body.length; i++) {
|
|
384
|
+
const count = wrappedPerItem[i]?.length ?? 1;
|
|
385
|
+
if (i === this.selectedPickerIndex) {
|
|
386
|
+
targetLine = currentLine;
|
|
387
|
+
targetEndLine = currentLine + count - 1;
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
currentLine += count;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const visible = [...header, ...this.windowBody(wrapped, outputBodyHeight, targetLine, targetEndLine)];
|
|
395
|
+
|
|
396
|
+
const frame: string[] = [
|
|
397
|
+
this.border("─".repeat(terminalWidth)),
|
|
398
|
+
alignRight(this.title(), this.titleRight(selected), terminalWidth),
|
|
399
|
+
this.border("─".repeat(terminalWidth)),
|
|
400
|
+
];
|
|
401
|
+
|
|
402
|
+
const roster = this.rosterLines(this.agents, rosterWidth, bodyHeight);
|
|
403
|
+
for (let index = 0; index < bodyHeight; index++) {
|
|
404
|
+
frame.push(
|
|
405
|
+
`${fitWidth(roster[index] ?? "", rosterWidth)}${this.border("│")}${fitWidth(visible[index] ?? "", detailWidth)}`,
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
frame.push(
|
|
410
|
+
this.border(`${"─".repeat(rosterWidth)}┴${"─".repeat(detailWidth)}`),
|
|
411
|
+
fitWidth(this.footer(), terminalWidth),
|
|
412
|
+
this.border("─".repeat(terminalWidth)),
|
|
413
|
+
);
|
|
414
|
+
|
|
415
|
+
return frame.map((line) => truncate(line, terminalWidth));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private title(): string {
|
|
419
|
+
return `${boldText(this.theme, "Model fallback manager")} ${this.paint("dim", "· live controls")}`;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
private titleRight(entry: FallbackAgentEntry | undefined): string {
|
|
423
|
+
if (!entry) {
|
|
424
|
+
return this.paint("dim", "no agents");
|
|
425
|
+
}
|
|
426
|
+
const glyph = entry.isMain ? this.paint("accent", MAIN_AGENT_GLYPH) : this.paint("dim", "●");
|
|
427
|
+
return `${glyph} ${this.paint("dim", entry.name)} · active`;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
private rosterLines(entries: readonly FallbackAgentEntry[], width: number, bodyHeight: number): string[] {
|
|
431
|
+
if (entries.length === 0) {
|
|
432
|
+
return [this.paint("dim", "No configured agents")];
|
|
433
|
+
}
|
|
434
|
+
const maxScroll = Math.max(0, entries.length - bodyHeight);
|
|
435
|
+
if (this.selected < this.rosterScroll) {
|
|
436
|
+
this.rosterScroll = this.selected;
|
|
437
|
+
} else if (this.selected >= this.rosterScroll + bodyHeight) {
|
|
438
|
+
this.rosterScroll = this.selected - bodyHeight + 1;
|
|
439
|
+
}
|
|
440
|
+
this.rosterScroll = Math.max(0, Math.min(this.rosterScroll, maxScroll));
|
|
441
|
+
const start = this.rosterScroll;
|
|
442
|
+
const lines: string[] = [];
|
|
443
|
+
for (let offset = 0; offset < bodyHeight; offset++) {
|
|
444
|
+
const index = start + offset;
|
|
445
|
+
const entry = entries[index];
|
|
446
|
+
if (!entry) {
|
|
447
|
+
lines.push("");
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
const isSelected = index === this.selected;
|
|
451
|
+
const marker = isSelected ? this.paint("accent", "›") : " ";
|
|
452
|
+
const glyph = entry.isMain ? this.paint("accent", MAIN_AGENT_GLYPH) : this.paint("dim", "●");
|
|
453
|
+
const name = isSelected ? boldText(this.theme, sanitize(entry.name)) : sanitize(entry.name);
|
|
454
|
+
const count = entry.fallbackModels.length;
|
|
455
|
+
const countLabel = count > 0 ? `${count} model${count === 1 ? "" : "s"}` : "none";
|
|
456
|
+
const left = `${marker} ${glyph} ${name}`;
|
|
457
|
+
lines.push(alignRight(left, this.paint("dim", `· ${countLabel}`), width));
|
|
458
|
+
}
|
|
459
|
+
return lines;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private detailSections(
|
|
463
|
+
entry: FallbackAgentEntry | undefined,
|
|
464
|
+
width: number,
|
|
465
|
+
_height: number,
|
|
466
|
+
): { header: string[]; body: string[] } {
|
|
467
|
+
if (this.mode === "pick_model") {
|
|
468
|
+
return this.pickerSections(width);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (!entry) {
|
|
472
|
+
return {
|
|
473
|
+
header: [],
|
|
474
|
+
body: ["No agent selected."],
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const glyph = entry.isMain ? this.paint("accent", MAIN_AGENT_GLYPH) : this.paint("dim", "●");
|
|
479
|
+
const header = [
|
|
480
|
+
alignRight(` ${glyph} ${boldText(this.theme, entry.name)}`, this.paint("dim", "active"), width),
|
|
481
|
+
` ${this.paint("dim", `${entry.isMain ? "primary session" : "subagent"} · ${sanitize(entry.primaryModel)}`)}`,
|
|
482
|
+
` ${this.paint("muted", `${entry.fallbackModels.length} fallback${entry.fallbackModels.length === 1 ? "" : "s"} configured`)}`,
|
|
483
|
+
`${this.paint("accent", "Fallback Chain")} ${this.paint("dim", "· priority order")}`,
|
|
484
|
+
].map((line) => truncate(line, width));
|
|
485
|
+
|
|
486
|
+
const body: string[] = [];
|
|
487
|
+
if (this.notice) {
|
|
488
|
+
body.push(this.paint("warning", ` Notice: ${this.notice}`), "");
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (entry.fallbackModels.length === 0) {
|
|
492
|
+
body.push(this.paint("dim", " No fallback models configured."));
|
|
493
|
+
body.push(this.paint("dim", " Press a or Enter to add a fallback model."));
|
|
494
|
+
} else {
|
|
495
|
+
entry.fallbackModels.forEach((model, index) => {
|
|
496
|
+
const num = this.paint("accent", `${index + 1}.`);
|
|
497
|
+
body.push(` ${num} ${model}`);
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
body.push("");
|
|
502
|
+
body.push(this.paint("dim", " Controls:"));
|
|
503
|
+
body.push(` ${this.paint("accent", "a/Enter")} Add fallback model to chain`);
|
|
504
|
+
body.push(` ${this.paint("accent", "d")} Remove last fallback model`);
|
|
505
|
+
body.push(` ${this.paint("accent", "s")} Save changes to configuration files`);
|
|
506
|
+
|
|
507
|
+
return { header, body };
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
private pickerSections(width: number): { header: string[]; body: string[] } {
|
|
511
|
+
const candidates = this.pickerCandidates();
|
|
512
|
+
const matchLabel = `${candidates.length} match${candidates.length === 1 ? "" : "es"}`;
|
|
513
|
+
const header = [
|
|
514
|
+
alignRight(
|
|
515
|
+
` ${this.paint("accent", "Select Model to Add to Fallback Chain")}`,
|
|
516
|
+
this.paint("dim", "picker"),
|
|
517
|
+
width,
|
|
518
|
+
),
|
|
519
|
+
` ${this.paint("dim", "Use ↑/↓ to navigate, Enter to select, Esc to return")}`,
|
|
520
|
+
` ${this.paint("accent", "Search:")} [${this.searchQuery}] ${this.paint("dim", `(${matchLabel})`)}`,
|
|
521
|
+
].map((line) => truncate(line, width));
|
|
522
|
+
|
|
523
|
+
const body: string[] = [];
|
|
524
|
+
if (candidates.length === 0) {
|
|
525
|
+
if (this.searchQuery) {
|
|
526
|
+
body.push(this.paint("dim", ` No models match "${this.searchQuery}"`));
|
|
527
|
+
} else {
|
|
528
|
+
body.push(this.paint("dim", " No available models to select"));
|
|
529
|
+
}
|
|
530
|
+
} else {
|
|
531
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
532
|
+
const candidate = candidates[i] ?? "";
|
|
533
|
+
const isSelected = i === this.selectedPickerIndex;
|
|
534
|
+
const marker = isSelected ? this.paint("accent", " › ") : " ";
|
|
535
|
+
const text = isSelected ? boldText(this.theme, candidate) : candidate;
|
|
536
|
+
body.push(`${marker}${text}`);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
return { header, body };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
private footer(): string {
|
|
544
|
+
if (this.mode === "pick_model") {
|
|
545
|
+
const candidates = this.pickerCandidates();
|
|
546
|
+
const position = `${candidates.length === 0 ? 0 : this.selectedPickerIndex + 1}/${candidates.length}`;
|
|
547
|
+
return ` ${this.paint("dim", `↑/↓ move · enter select · esc cancel · ${position}`)}`;
|
|
548
|
+
}
|
|
549
|
+
const up = this.keyLabel("tui.select.up", "↑");
|
|
550
|
+
const down = this.keyLabel("tui.select.down", "↓");
|
|
551
|
+
const close = this.keyLabel("tui.select.cancel", "Esc");
|
|
552
|
+
const position = `${this.agents.length === 0 ? 0 : this.selected + 1}/${this.agents.length}`;
|
|
553
|
+
return ` ${this.paint("dim", `${up}/${down} agent · a add · d remove · s save · K/J scroll · ${close} close · ${position}`)} `;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
protected windowBody(body: readonly string[], height: number, targetLine?: number, targetEndLine?: number): string[] {
|
|
557
|
+
if (this.mode !== "pick_model") {
|
|
558
|
+
return super.windowBody(body, height);
|
|
559
|
+
}
|
|
560
|
+
if (height <= 0) {
|
|
561
|
+
return [];
|
|
562
|
+
}
|
|
563
|
+
const maxScroll = Math.max(0, body.length - height);
|
|
564
|
+
const line = targetLine ?? this.selectedPickerIndex;
|
|
565
|
+
const endLine = targetEndLine ?? line;
|
|
566
|
+
if (endLine >= this.pickerScroll + height) {
|
|
567
|
+
this.pickerScroll = endLine - height + 1;
|
|
568
|
+
}
|
|
569
|
+
if (line < this.pickerScroll) {
|
|
570
|
+
this.pickerScroll = line;
|
|
571
|
+
}
|
|
572
|
+
this.pickerScroll = Math.max(0, Math.min(this.pickerScroll, maxScroll));
|
|
573
|
+
return body.slice(this.pickerScroll, this.pickerScroll + height);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** Wheel event or the host's scroll keybinding, in either direction. */
|
|
577
|
+
private isScrollUp(data: string): boolean {
|
|
578
|
+
return mouseWheelDirection(data) === -1 || this.matches(data, "tui.select.scrollUp");
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
private isScrollDown(data: string): boolean {
|
|
582
|
+
return mouseWheelDirection(data) === 1 || this.matches(data, "tui.select.scrollDown");
|
|
583
|
+
}
|
|
584
|
+
}
|