pi-subagent-manager 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -18
- package/agents/architect.md +38 -2
- package/agents/coder.md +42 -0
- package/agents/reviewer.md +34 -0
- package/agents/tasker.md +40 -0
- package/agents/writer.md +34 -0
- package/docs/default-agents.md +112 -0
- package/docs/research-claude.md +258 -0
- package/docs/research-codex.md +143 -0
- package/docs/research-default-agent-models.md +119 -0
- package/docs/research-model-roles-coding.md +99 -0
- package/docs/research-model-roles-writing.md +78 -0
- package/package.json +8 -3
- package/src/agent-import.ts +245 -0
- package/src/config.ts +23 -9
- package/src/dialog.ts +631 -0
- package/src/import-discovery.ts +643 -0
- package/src/import-instructions.ts +156 -0
- package/src/import-picker.ts +231 -0
- package/src/index.ts +102 -24
- package/src/manager.ts +67 -2
- package/src/model-picker.ts +194 -92
- package/src/models.ts +8 -2
- package/src/runtime.ts +70 -15
- package/src/settings-ui.ts +173 -0
- package/src/settings.ts +153 -8
- package/src/status-ui.ts +288 -0
- package/src/thread-tree.ts +134 -0
- package/src/tools.ts +21 -10
- package/src/types.ts +24 -1
- package/src/ui.ts +760 -102
- package/agents/researcher.md +0 -14
- package/agents/worker.md +0 -6
package/src/manager.ts
CHANGED
|
@@ -24,6 +24,11 @@ interface Record {
|
|
|
24
24
|
run?: Promise<void>;
|
|
25
25
|
started?: Promise<void>;
|
|
26
26
|
sessionLeafId?: string | null;
|
|
27
|
+
/** Last authoritative cumulative usage applied from the current driver. */
|
|
28
|
+
usageCursor?: { input: number; output: number };
|
|
29
|
+
/** In-progress message usage. Shown live, never persisted. */
|
|
30
|
+
liveInputTokens?: number;
|
|
31
|
+
liveOutputTokens?: number;
|
|
27
32
|
pauseRequested: boolean;
|
|
28
33
|
stopRequested: boolean;
|
|
29
34
|
}
|
|
@@ -127,6 +132,10 @@ export class ThreadManager {
|
|
|
127
132
|
error: current.error,
|
|
128
133
|
createdAt: current.createdAt,
|
|
129
134
|
sessionFile: current.sessionFile,
|
|
135
|
+
// Settled totals only. startedAt and partial usage must not churn persistence.
|
|
136
|
+
elapsedMs: record.view.elapsedMs ?? 0,
|
|
137
|
+
inputTokens: record.view.inputTokens ?? 0,
|
|
138
|
+
outputTokens: record.view.outputTokens ?? 0,
|
|
130
139
|
};
|
|
131
140
|
const sessionLeafId = this.sessionLeafId(record);
|
|
132
141
|
if (sessionLeafId !== undefined) view.sessionLeafId = sessionLeafId;
|
|
@@ -155,7 +164,14 @@ export class ThreadManager {
|
|
|
155
164
|
)
|
|
156
165
|
throw new Error("Invalid saved thread registry");
|
|
157
166
|
const { sessionLeafId, ...storedView } = savedView;
|
|
158
|
-
const view: ThreadView = {
|
|
167
|
+
const view: ThreadView = {
|
|
168
|
+
...storedView,
|
|
169
|
+
updatedAt: Date.now(),
|
|
170
|
+
elapsedMs: storedView.elapsedMs ?? 0,
|
|
171
|
+
inputTokens: storedView.inputTokens ?? 0,
|
|
172
|
+
outputTokens: storedView.outputTokens ?? 0,
|
|
173
|
+
};
|
|
174
|
+
delete view.startedAt;
|
|
159
175
|
if (view.parent !== parentPath(path)) throw new Error(`Invalid saved parent for ${path}`);
|
|
160
176
|
if (active(view)) {
|
|
161
177
|
view.state = "paused";
|
|
@@ -241,6 +257,9 @@ export class ThreadManager {
|
|
|
241
257
|
status: "Starting",
|
|
242
258
|
createdAt: now,
|
|
243
259
|
updatedAt: now,
|
|
260
|
+
elapsedMs: 0,
|
|
261
|
+
inputTokens: 0,
|
|
262
|
+
outputTokens: 0,
|
|
244
263
|
},
|
|
245
264
|
definition: type,
|
|
246
265
|
inherited,
|
|
@@ -394,11 +413,13 @@ export class ThreadManager {
|
|
|
394
413
|
}
|
|
395
414
|
|
|
396
415
|
private start(record: Record, message: string): void {
|
|
416
|
+
this.freezeElapsed(record);
|
|
397
417
|
record.pauseRequested = false;
|
|
398
418
|
record.stopRequested = false;
|
|
399
419
|
record.startup = new AbortController();
|
|
400
420
|
record.view.state = "starting";
|
|
401
421
|
record.view.status = "Starting";
|
|
422
|
+
record.view.startedAt = Date.now();
|
|
402
423
|
delete record.view.output;
|
|
403
424
|
delete record.view.error;
|
|
404
425
|
const epoch = this.epoch;
|
|
@@ -437,6 +458,9 @@ export class ThreadManager {
|
|
|
437
458
|
record.view.error = errorText(error);
|
|
438
459
|
record.view.status = record.stopRequested ? "Stopped; session retained" : record.view.error;
|
|
439
460
|
} finally {
|
|
461
|
+
this.freezeElapsed(record);
|
|
462
|
+
record.liveInputTokens = 0;
|
|
463
|
+
record.liveOutputTokens = 0;
|
|
440
464
|
markStarted(); // Failed/cancelled initialization must also release callers waiting to steer.
|
|
441
465
|
this.touch(record);
|
|
442
466
|
if (!this.disposed && epoch === this.epoch)
|
|
@@ -475,6 +499,10 @@ export class ThreadManager {
|
|
|
475
499
|
signal,
|
|
476
500
|
shouldPause: () => record.pauseRequested,
|
|
477
501
|
onEvent: (event) => {
|
|
502
|
+
if (event.kind === "usage") {
|
|
503
|
+
this.applyUsage(record, event);
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
478
506
|
if (event.kind === "checkpoint") {
|
|
479
507
|
this.touch(record);
|
|
480
508
|
return;
|
|
@@ -496,6 +524,9 @@ export class ThreadManager {
|
|
|
496
524
|
throw new Error("Driver startup cancelled");
|
|
497
525
|
}
|
|
498
526
|
record.driver = driver;
|
|
527
|
+
record.usageCursor = { input: 0, output: 0 };
|
|
528
|
+
record.liveInputTokens = 0;
|
|
529
|
+
record.liveOutputTokens = 0;
|
|
499
530
|
record.view.sessionFile = driver.sessionFile;
|
|
500
531
|
if (driver.sessionLeafId !== undefined) record.sessionLeafId = driver.sessionLeafId;
|
|
501
532
|
this.touch(record);
|
|
@@ -514,6 +545,36 @@ export class ThreadManager {
|
|
|
514
545
|
record.view.updatedAt = Date.now();
|
|
515
546
|
if (!this.disposed) this.options.onEvent?.({ kind: "change", thread: this.view(record) });
|
|
516
547
|
}
|
|
548
|
+
/** Fold driver-cumulative usage into persisted totals. Partials only refresh the live view. */
|
|
549
|
+
private applyUsage(
|
|
550
|
+
record: Record,
|
|
551
|
+
event: { inputTokens: number; outputTokens: number; partial?: boolean },
|
|
552
|
+
): void {
|
|
553
|
+
const cursor = record.usageCursor ?? { input: 0, output: 0 };
|
|
554
|
+
if (event.partial) {
|
|
555
|
+
const input = Math.max(0, event.inputTokens - cursor.input);
|
|
556
|
+
const output = Math.max(0, event.outputTokens - cursor.output);
|
|
557
|
+
if (input === record.liveInputTokens && output === record.liveOutputTokens) return;
|
|
558
|
+
record.liveInputTokens = input;
|
|
559
|
+
record.liveOutputTokens = output;
|
|
560
|
+
if (!this.disposed) this.options.onEvent?.({ kind: "metrics", thread: this.view(record) });
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
record.view.inputTokens =
|
|
564
|
+
(record.view.inputTokens ?? 0) + Math.max(0, event.inputTokens - cursor.input);
|
|
565
|
+
record.view.outputTokens =
|
|
566
|
+
(record.view.outputTokens ?? 0) + Math.max(0, event.outputTokens - cursor.output);
|
|
567
|
+
record.usageCursor = { input: event.inputTokens, output: event.outputTokens };
|
|
568
|
+
record.liveInputTokens = 0;
|
|
569
|
+
record.liveOutputTokens = 0;
|
|
570
|
+
this.touch(record);
|
|
571
|
+
}
|
|
572
|
+
private freezeElapsed(record: Record): void {
|
|
573
|
+
const startedAt = record.view.startedAt;
|
|
574
|
+
if (startedAt === undefined) return;
|
|
575
|
+
record.view.elapsedMs = (record.view.elapsedMs ?? 0) + Math.max(0, Date.now() - startedAt);
|
|
576
|
+
delete record.view.startedAt;
|
|
577
|
+
}
|
|
517
578
|
private prepareInheritedContext(record: Record, readOnly = false): Promise<void> | undefined {
|
|
518
579
|
if (!record.contextPending) return record.contextReady;
|
|
519
580
|
if (record.contextReady) return record.contextReady;
|
|
@@ -552,10 +613,14 @@ export class ThreadManager {
|
|
|
552
613
|
return parentRecord.driver?.sessionFile ?? parentRecord.view.sessionFile;
|
|
553
614
|
}
|
|
554
615
|
private view(record: Record): ThreadView {
|
|
555
|
-
|
|
616
|
+
const view = structuredClone({
|
|
556
617
|
...record.view,
|
|
557
618
|
sessionFile: record.driver ? record.driver.sessionFile : record.view.sessionFile,
|
|
558
619
|
});
|
|
620
|
+
if (record.liveInputTokens) view.inputTokens = (view.inputTokens ?? 0) + record.liveInputTokens;
|
|
621
|
+
if (record.liveOutputTokens)
|
|
622
|
+
view.outputTokens = (view.outputTokens ?? 0) + record.liveOutputTokens;
|
|
623
|
+
return view;
|
|
559
624
|
}
|
|
560
625
|
private record(path: string): Record {
|
|
561
626
|
const record = this.records.get(path);
|
package/src/model-picker.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
2
2
|
import {
|
|
3
|
-
DynamicBorder,
|
|
4
3
|
getSelectListTheme,
|
|
5
4
|
type ExtensionCommandContext,
|
|
6
5
|
type ScopedModel,
|
|
@@ -14,10 +13,12 @@ import {
|
|
|
14
13
|
SelectList,
|
|
15
14
|
Spacer,
|
|
16
15
|
stripTerminalSequences,
|
|
16
|
+
truncateToWidth,
|
|
17
17
|
Text,
|
|
18
18
|
type SelectItem,
|
|
19
19
|
} from "@earendil-works/pi-tui";
|
|
20
20
|
import { modelIdentity } from "./models.ts";
|
|
21
|
+
import { dialogHeight, frameDialog, DIALOG_OPTIONS } from "./dialog.ts";
|
|
21
22
|
|
|
22
23
|
const MODEL_EDITOR_LAYOUT = {
|
|
23
24
|
minPrimaryColumnWidth: 24,
|
|
@@ -33,10 +34,14 @@ type AvailableModel = Pick<Model<Api>, "provider" | "id" | "name">;
|
|
|
33
34
|
type ModelEditorMode =
|
|
34
35
|
| { kind: "menu" }
|
|
35
36
|
| { kind: "actions"; index: number }
|
|
36
|
-
| {
|
|
37
|
+
| {
|
|
38
|
+
kind: "picker";
|
|
39
|
+
replaceIndex: number | undefined;
|
|
40
|
+
returnTo: "menu" | "actions";
|
|
41
|
+
};
|
|
37
42
|
|
|
38
43
|
interface OrderedModelEditorComponentOptions {
|
|
39
|
-
tui: { requestRender(force?: boolean): void };
|
|
44
|
+
tui: { requestRender(force?: boolean): void; terminal?: { rows: number } };
|
|
40
45
|
theme: Theme;
|
|
41
46
|
availableModels: readonly AvailableModel[];
|
|
42
47
|
scopedModels: readonly ScopedModel[];
|
|
@@ -64,7 +69,10 @@ function sortModels(models: readonly AvailableModel[]): AvailableModel[] {
|
|
|
64
69
|
);
|
|
65
70
|
}
|
|
66
71
|
|
|
67
|
-
function describeModel(
|
|
72
|
+
function describeModel(
|
|
73
|
+
model: AvailableModel,
|
|
74
|
+
scopedIdentities: ReadonlySet<string>,
|
|
75
|
+
): string {
|
|
68
76
|
const annotation = scopedIdentities.has(modelIdentity(model))
|
|
69
77
|
? "scoped in this session"
|
|
70
78
|
: "portable preference · not scoped in this session";
|
|
@@ -72,7 +80,10 @@ function describeModel(model: AvailableModel, scopedIdentities: ReadonlySet<stri
|
|
|
72
80
|
return [name, annotation].filter(Boolean).join(" · ");
|
|
73
81
|
}
|
|
74
82
|
|
|
75
|
-
function buildSearchText(
|
|
83
|
+
function buildSearchText(
|
|
84
|
+
model: AvailableModel,
|
|
85
|
+
scopedIdentities: ReadonlySet<string>,
|
|
86
|
+
): string {
|
|
76
87
|
return [
|
|
77
88
|
renderModelIdentity(modelIdentity(model)),
|
|
78
89
|
sanitizeModelText(model.name),
|
|
@@ -83,7 +94,10 @@ function buildSearchText(model: AvailableModel, scopedIdentities: ReadonlySet<st
|
|
|
83
94
|
}
|
|
84
95
|
|
|
85
96
|
export class OrderedModelEditorComponent extends Container {
|
|
86
|
-
private readonly tui: {
|
|
97
|
+
private readonly tui: {
|
|
98
|
+
requestRender(force?: boolean): void;
|
|
99
|
+
terminal?: { rows: number };
|
|
100
|
+
};
|
|
87
101
|
private readonly theme: Theme;
|
|
88
102
|
private readonly allModels: readonly AvailableModel[];
|
|
89
103
|
private readonly modelsByIdentity = new Map<string, AvailableModel>();
|
|
@@ -99,7 +113,12 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
99
113
|
private pickerQuery = "";
|
|
100
114
|
private pickerSelectedValue: string | undefined;
|
|
101
115
|
private currentItems: SelectItem[] = [];
|
|
102
|
-
private selectList = new SelectList(
|
|
116
|
+
private selectList = new SelectList(
|
|
117
|
+
[],
|
|
118
|
+
1,
|
|
119
|
+
getSelectListTheme(),
|
|
120
|
+
MODEL_EDITOR_LAYOUT,
|
|
121
|
+
);
|
|
103
122
|
private searchInput: Input | undefined;
|
|
104
123
|
private pickerListChildIndex = -1;
|
|
105
124
|
private menuSelectedValue: string | undefined;
|
|
@@ -113,23 +132,17 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
113
132
|
this.allModels = sortModels(options.availableModels);
|
|
114
133
|
this.onDone = options.onDone;
|
|
115
134
|
this.onCancel = options.onCancel;
|
|
116
|
-
for (const model of this.allModels)
|
|
117
|
-
|
|
135
|
+
for (const model of this.allModels)
|
|
136
|
+
this.modelsByIdentity.set(modelIdentity(model), model);
|
|
137
|
+
this.scopedIdentities = new Set(
|
|
138
|
+
options.scopedModels.map((entry) => modelIdentity(entry.model)),
|
|
139
|
+
);
|
|
118
140
|
this.draft = [...(options.initialModels ?? [])];
|
|
119
141
|
this.mode =
|
|
120
142
|
this.draft.length === 0
|
|
121
143
|
? { kind: "picker", replaceIndex: undefined, returnTo: "menu" }
|
|
122
144
|
: { kind: "menu" };
|
|
123
145
|
|
|
124
|
-
this.addChild(new DynamicBorder((text) => this.theme.fg("muted", text)));
|
|
125
|
-
this.addChild(new Spacer(1));
|
|
126
|
-
this.addChild(this.titleText);
|
|
127
|
-
this.addChild(this.subtitleText);
|
|
128
|
-
this.addChild(new Spacer(1));
|
|
129
|
-
this.addChild(this.body);
|
|
130
|
-
this.addChild(new Spacer(1));
|
|
131
|
-
this.addChild(this.footerText);
|
|
132
|
-
this.addChild(new DynamicBorder((text) => this.theme.fg("muted", text)));
|
|
133
146
|
this.rebuild();
|
|
134
147
|
}
|
|
135
148
|
|
|
@@ -162,6 +175,50 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
162
175
|
return this.searchInput;
|
|
163
176
|
}
|
|
164
177
|
|
|
178
|
+
render(width: number): string[] {
|
|
179
|
+
const height = dialogHeight(this.tui);
|
|
180
|
+
const inner = Math.max(1, width - 4);
|
|
181
|
+
const header = [
|
|
182
|
+
truncateToWidth(
|
|
183
|
+
this.subtitleText
|
|
184
|
+
.render(1000)
|
|
185
|
+
.map((line) => line.trim())
|
|
186
|
+
.filter(Boolean)
|
|
187
|
+
.join(" "),
|
|
188
|
+
inner,
|
|
189
|
+
"",
|
|
190
|
+
),
|
|
191
|
+
];
|
|
192
|
+
if (this.searchInput) header.push(...this.searchInput.render(inner));
|
|
193
|
+
const budget = Math.max(1, height - 4 - header.length);
|
|
194
|
+
const selected = this.selectList.getSelectedItem()?.value;
|
|
195
|
+
const index = Math.max(
|
|
196
|
+
0,
|
|
197
|
+
this.currentItems.findIndex((item) => item.value === selected),
|
|
198
|
+
);
|
|
199
|
+
const start = Math.max(
|
|
200
|
+
0,
|
|
201
|
+
Math.min(index - budget + 1, this.currentItems.length - budget),
|
|
202
|
+
);
|
|
203
|
+
const rows = this.currentItems.slice(start, start + budget).map((item) => {
|
|
204
|
+
const text = `${item.value === selected ? "›" : " "} ${item.label} ${this.theme.fg("muted", item.description ?? "")}`;
|
|
205
|
+
return item.value === selected ? this.theme.fg("accent", text) : text;
|
|
206
|
+
});
|
|
207
|
+
if (!rows.length) rows.push(this.theme.fg("muted", "No matching models"));
|
|
208
|
+
return frameDialog(
|
|
209
|
+
this.theme,
|
|
210
|
+
width,
|
|
211
|
+
height,
|
|
212
|
+
this.titleText.render(1000).join(" ").trim(),
|
|
213
|
+
[...header, ...rows].map((line) => ` ${line}`),
|
|
214
|
+
this.footerText
|
|
215
|
+
.render(1000)
|
|
216
|
+
.map((line) => line.trim())
|
|
217
|
+
.filter(Boolean)
|
|
218
|
+
.join(" "),
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
165
222
|
handleInput(keyData: string): void {
|
|
166
223
|
if (this.mode.kind === "picker" && this.searchInput) {
|
|
167
224
|
const kb = getKeybindings();
|
|
@@ -243,33 +300,38 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
243
300
|
},
|
|
244
301
|
];
|
|
245
302
|
this.currentItems = items;
|
|
246
|
-
this.selectList = this.buildSelectList(
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
303
|
+
this.selectList = this.buildSelectList(
|
|
304
|
+
items,
|
|
305
|
+
this.menuSelectedValue,
|
|
306
|
+
(value) => {
|
|
307
|
+
if (value.startsWith("entry:")) {
|
|
308
|
+
const index = Number(value.slice("entry:".length));
|
|
309
|
+
if (!Number.isNaN(index) && this.draft[index] !== undefined) {
|
|
310
|
+
this.mode = { kind: "actions", index };
|
|
311
|
+
this.actionSelectedValue = "action:replace";
|
|
312
|
+
this.rebuildAndRender();
|
|
313
|
+
}
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (value === "action:add") {
|
|
317
|
+
this.openPicker(undefined, "menu");
|
|
318
|
+
} else if (value === "action:clear") {
|
|
319
|
+
this.draft = [];
|
|
320
|
+
this.menuSelectedValue = "action:done";
|
|
252
321
|
this.rebuildAndRender();
|
|
322
|
+
} else if (value === "action:done") {
|
|
323
|
+
this.onDone([...this.draft]);
|
|
324
|
+
} else if (value === "action:cancel") {
|
|
325
|
+
this.onCancel();
|
|
253
326
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
this.openPicker(undefined, "menu");
|
|
258
|
-
} else if (value === "action:clear") {
|
|
259
|
-
this.draft = [];
|
|
260
|
-
this.menuSelectedValue = "action:done";
|
|
261
|
-
this.rebuildAndRender();
|
|
262
|
-
} else if (value === "action:done") {
|
|
263
|
-
this.onDone([...this.draft]);
|
|
264
|
-
} else if (value === "action:cancel") {
|
|
265
|
-
this.onCancel();
|
|
266
|
-
}
|
|
267
|
-
}, () => this.onCancel());
|
|
327
|
+
},
|
|
328
|
+
() => this.onCancel(),
|
|
329
|
+
);
|
|
268
330
|
this.body.addChild(this.selectList);
|
|
269
331
|
this.footerText.setText(
|
|
270
332
|
this.theme.fg(
|
|
271
333
|
"dim",
|
|
272
|
-
"
|
|
334
|
+
"↑↓ select · Enter edit · Esc cancel · Done applies preferences",
|
|
273
335
|
),
|
|
274
336
|
);
|
|
275
337
|
}
|
|
@@ -319,42 +381,60 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
319
381
|
},
|
|
320
382
|
];
|
|
321
383
|
this.currentItems = items;
|
|
322
|
-
this.selectList = this.buildSelectList(
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
384
|
+
this.selectList = this.buildSelectList(
|
|
385
|
+
items,
|
|
386
|
+
this.actionSelectedValue,
|
|
387
|
+
(value) => {
|
|
388
|
+
if (value === "action:replace") {
|
|
389
|
+
this.openPicker(index, "actions");
|
|
390
|
+
} else if (value === "action:remove") {
|
|
391
|
+
this.draft.splice(index, 1);
|
|
392
|
+
this.mode = { kind: "menu" };
|
|
393
|
+
this.menuSelectedValue = this.draft[index]
|
|
394
|
+
? `entry:${index}`
|
|
395
|
+
: "action:add";
|
|
396
|
+
this.rebuildAndRender();
|
|
397
|
+
} else if (value === "action:earlier" && index > 0) {
|
|
398
|
+
[this.draft[index - 1], this.draft[index]] = [
|
|
399
|
+
this.draft[index],
|
|
400
|
+
this.draft[index - 1],
|
|
401
|
+
];
|
|
402
|
+
this.mode = { kind: "menu" };
|
|
403
|
+
this.menuSelectedValue = `entry:${index - 1}`;
|
|
404
|
+
this.rebuildAndRender();
|
|
405
|
+
} else if (value === "action:later" && index < this.draft.length - 1) {
|
|
406
|
+
[this.draft[index], this.draft[index + 1]] = [
|
|
407
|
+
this.draft[index + 1],
|
|
408
|
+
this.draft[index],
|
|
409
|
+
];
|
|
410
|
+
this.mode = { kind: "menu" };
|
|
411
|
+
this.menuSelectedValue = `entry:${index + 1}`;
|
|
412
|
+
this.rebuildAndRender();
|
|
413
|
+
} else if (value === "action:back") {
|
|
414
|
+
this.mode = { kind: "menu" };
|
|
415
|
+
this.menuSelectedValue = `entry:${index}`;
|
|
416
|
+
this.rebuildAndRender();
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
() => {
|
|
341
420
|
this.mode = { kind: "menu" };
|
|
342
421
|
this.menuSelectedValue = `entry:${index}`;
|
|
343
422
|
this.rebuildAndRender();
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
this.mode = { kind: "menu" };
|
|
347
|
-
this.menuSelectedValue = `entry:${index}`;
|
|
348
|
-
this.rebuildAndRender();
|
|
349
|
-
});
|
|
423
|
+
},
|
|
424
|
+
);
|
|
350
425
|
this.body.addChild(this.selectList);
|
|
351
|
-
this.footerText.setText(
|
|
426
|
+
this.footerText.setText(
|
|
427
|
+
this.theme.fg("dim", "↑↓ select · Enter choose · Esc back"),
|
|
428
|
+
);
|
|
352
429
|
}
|
|
353
430
|
|
|
354
431
|
private buildPicker(replaceIndex: number | undefined): void {
|
|
355
432
|
const isReplace = replaceIndex !== undefined;
|
|
356
433
|
this.titleText.setText(
|
|
357
|
-
this.theme.fg(
|
|
434
|
+
this.theme.fg(
|
|
435
|
+
"accent",
|
|
436
|
+
isReplace ? `Replace model ${replaceIndex + 1}` : "Add model",
|
|
437
|
+
),
|
|
358
438
|
);
|
|
359
439
|
this.subtitleText.setText(
|
|
360
440
|
this.theme.fg(
|
|
@@ -376,7 +456,9 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
376
456
|
const returnTo = this.mode.kind === "picker" ? this.mode.returnTo : "menu";
|
|
377
457
|
this.pickerListChildIndex = this.body.children.length;
|
|
378
458
|
this.refreshPickerList(replaceIndex, returnTo);
|
|
379
|
-
this.footerText.setText(
|
|
459
|
+
this.footerText.setText(
|
|
460
|
+
this.theme.fg("dim", "Type filter · ↑↓ select · Enter choose · Esc back"),
|
|
461
|
+
);
|
|
380
462
|
}
|
|
381
463
|
|
|
382
464
|
private rebuildAndRender(): void {
|
|
@@ -384,7 +466,10 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
384
466
|
this.tui.requestRender();
|
|
385
467
|
}
|
|
386
468
|
|
|
387
|
-
private openPicker(
|
|
469
|
+
private openPicker(
|
|
470
|
+
replaceIndex: number | undefined,
|
|
471
|
+
returnTo: "menu" | "actions",
|
|
472
|
+
): void {
|
|
388
473
|
this.mode = { kind: "picker", replaceIndex, returnTo };
|
|
389
474
|
this.pickerQuery = "";
|
|
390
475
|
this.pickerSelectedValue = undefined;
|
|
@@ -397,25 +482,31 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
397
482
|
): void {
|
|
398
483
|
const items = this.getPickerItems(replaceIndex);
|
|
399
484
|
this.currentItems = items;
|
|
400
|
-
const list = this.buildSelectList(
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
485
|
+
const list = this.buildSelectList(
|
|
486
|
+
items,
|
|
487
|
+
this.pickerSelectedValue,
|
|
488
|
+
(value) => {
|
|
489
|
+
if (replaceIndex === undefined) this.draft.push(value);
|
|
490
|
+
else this.draft[replaceIndex] = value;
|
|
491
|
+
this.mode = { kind: "menu" };
|
|
492
|
+
this.menuSelectedValue = `entry:${replaceIndex === undefined ? this.draft.length - 1 : replaceIndex}`;
|
|
493
|
+
this.pickerQuery = "";
|
|
494
|
+
this.pickerSelectedValue = undefined;
|
|
495
|
+
this.rebuildAndRender();
|
|
496
|
+
},
|
|
497
|
+
() => {
|
|
498
|
+
this.mode =
|
|
499
|
+
returnTo === "actions" && replaceIndex !== undefined
|
|
500
|
+
? { kind: "actions", index: replaceIndex }
|
|
501
|
+
: { kind: "menu" };
|
|
502
|
+
this.pickerQuery = "";
|
|
503
|
+
this.pickerSelectedValue = undefined;
|
|
504
|
+
this.rebuildAndRender();
|
|
505
|
+
},
|
|
506
|
+
);
|
|
417
507
|
this.selectList = list;
|
|
418
|
-
if (this.body.children[this.pickerListChildIndex])
|
|
508
|
+
if (this.body.children[this.pickerListChildIndex])
|
|
509
|
+
this.body.children[this.pickerListChildIndex] = list;
|
|
419
510
|
else this.body.addChild(list);
|
|
420
511
|
}
|
|
421
512
|
|
|
@@ -425,20 +516,27 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
425
516
|
onSelect: (value: string) => void,
|
|
426
517
|
onCancel: () => void,
|
|
427
518
|
): SelectList {
|
|
428
|
-
const list = new SelectList(
|
|
519
|
+
const list = new SelectList(
|
|
520
|
+
items,
|
|
521
|
+
Math.min(Math.max(items.length, 1), 10),
|
|
522
|
+
getSelectListTheme(),
|
|
523
|
+
MODEL_EDITOR_LAYOUT,
|
|
524
|
+
);
|
|
429
525
|
const selectedIndex = selectedValue
|
|
430
526
|
? items.findIndex((item) => item.value === selectedValue)
|
|
431
527
|
: 0;
|
|
432
528
|
if (selectedIndex >= 0) list.setSelectedIndex(selectedIndex);
|
|
433
529
|
list.onSelect = (item) => {
|
|
434
530
|
if (this.mode.kind === "menu") this.menuSelectedValue = item.value;
|
|
435
|
-
else if (this.mode.kind === "actions")
|
|
531
|
+
else if (this.mode.kind === "actions")
|
|
532
|
+
this.actionSelectedValue = item.value;
|
|
436
533
|
else this.pickerSelectedValue = item.value;
|
|
437
534
|
onSelect(item.value);
|
|
438
535
|
};
|
|
439
536
|
list.onSelectionChange = (item) => {
|
|
440
537
|
if (this.mode.kind === "menu") this.menuSelectedValue = item.value;
|
|
441
|
-
else if (this.mode.kind === "actions")
|
|
538
|
+
else if (this.mode.kind === "actions")
|
|
539
|
+
this.actionSelectedValue = item.value;
|
|
442
540
|
else this.pickerSelectedValue = item.value;
|
|
443
541
|
this.tui.requestRender();
|
|
444
542
|
};
|
|
@@ -447,8 +545,12 @@ export class OrderedModelEditorComponent extends Container {
|
|
|
447
545
|
}
|
|
448
546
|
|
|
449
547
|
private getPickerItems(replaceIndex: number | undefined): SelectItem[] {
|
|
450
|
-
const excluded = new Set(
|
|
451
|
-
|
|
548
|
+
const excluded = new Set(
|
|
549
|
+
this.draft.filter((_, index) => index !== replaceIndex),
|
|
550
|
+
);
|
|
551
|
+
const available = this.allModels.filter(
|
|
552
|
+
(model) => !excluded.has(modelIdentity(model)),
|
|
553
|
+
);
|
|
452
554
|
const filtered = this.pickerQuery
|
|
453
555
|
? fuzzyFilter(available, this.pickerQuery, (model) =>
|
|
454
556
|
buildSearchText(model, this.scopedIdentities),
|
|
@@ -504,6 +606,6 @@ export async function editModelPreferences(
|
|
|
504
606
|
onDone: (models) => done(models),
|
|
505
607
|
onCancel: () => done(MODEL_EDITOR_CANCEL),
|
|
506
608
|
}),
|
|
507
|
-
|
|
609
|
+
DIALOG_OPTIONS,
|
|
508
610
|
);
|
|
509
611
|
}
|
package/src/models.ts
CHANGED
|
@@ -38,16 +38,22 @@ export function modelIdentity(model: { provider: string; id: string }): string {
|
|
|
38
38
|
|
|
39
39
|
export function selectPreferredModel(
|
|
40
40
|
type: { name: string; models?: unknown; model?: unknown },
|
|
41
|
-
|
|
41
|
+
eligibleModels: readonly { model: { provider: string; id: string } }[],
|
|
42
|
+
scopedModelFiltering = true,
|
|
42
43
|
): string | undefined {
|
|
43
44
|
const preferences = getModelPreferences(type);
|
|
44
45
|
if (preferences === undefined) return undefined;
|
|
45
|
-
const available =
|
|
46
|
+
const available = eligibleModels.map(({ model }) => modelIdentity(model));
|
|
46
47
|
for (const preference of preferences) {
|
|
47
48
|
if (available.includes(preference)) return preference;
|
|
48
49
|
}
|
|
49
50
|
const preferenceList = `[${preferences.join(", ")}]`;
|
|
50
51
|
const availableList = available.length === 0 ? "(none)" : `[${available.join(", ")}]`;
|
|
52
|
+
if (!scopedModelFiltering) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Agent type ${JSON.stringify(type.name)} prefers models ${preferenceList}, but none are available. Available models: ${availableList}. Update this agent type's models list.`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
51
57
|
throw new Error(
|
|
52
58
|
`Agent type ${JSON.stringify(type.name)} prefers scoped models ${preferenceList}, but none are available in /scoped-models. Available scoped models: ${availableList}. Update /scoped-models or this agent type's models list.`,
|
|
53
59
|
);
|