@ferris1225/pi-subagents 4.1.2 → 4.1.3
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 +561 -506
- package/agents/cleaner.md +2 -2
- package/agents/documenter.md +46 -44
- package/agents/explorer.md +15 -11
- package/agents/reviewer.md +5 -2
- package/agents/worker.md +5 -3
- package/package.json +55 -55
- package/src/agents.ts +42 -1
- package/src/completion.ts +160 -160
- package/src/dispatch.ts +634 -637
- package/src/fixloop.ts +15 -32
- package/src/models.ts +189 -189
- package/src/monitor.ts +97 -27
- package/src/recovery.ts +145 -145
- package/src/rpc-run.ts +6 -3
- package/src/runtime.ts +5 -0
- package/src/session-fork.ts +80 -80
- package/src/setup.ts +151 -136
- package/src/spawn.ts +8 -2
- package/src/thread-lifecycle.ts +43 -11
- package/src/tools.ts +44 -30
- package/src/widget.ts +65 -19
package/src/fixloop.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import { isWriteCapableAgent, type AgentConfig } from "./agents.ts";
|
|
13
13
|
import { getResultOutput, isFailedResult, reviewVerdict, type SingleResult } from "./spawn.ts";
|
|
14
|
-
import {
|
|
14
|
+
import { formatUsageCompact, sumUsage } from "./monitor.ts";
|
|
15
15
|
import type { SubagentsConfig } from "./config.ts";
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -224,16 +224,6 @@ export interface ManagedWorkflowOutcome {
|
|
|
224
224
|
steps: ChainStep[];
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
-
/** Max distinguishing fragments kept in a one-line chain summary. */
|
|
228
|
-
export const CHAIN_SUMMARY_FRAGMENTS_MAX = 3;
|
|
229
|
-
|
|
230
|
-
/** The most telling fragments (paths, quoted phrases, symbols) of a run's final
|
|
231
|
-
* output: for a worker these are the paths it changed, for a reviewer the
|
|
232
|
-
* issues it found. Capped so summaries stay one line. */
|
|
233
|
-
export function chainKeyFragments(result: SingleResult): string[] {
|
|
234
|
-
return extractKeyFragments(getResultOutput(result)).slice(0, CHAIN_SUMMARY_FRAGMENTS_MAX);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
227
|
function workflowResultStatus(result: SingleResult): string {
|
|
238
228
|
if (isFailedResult(result)) return "failed";
|
|
239
229
|
if (result.agent === "reviewer") {
|
|
@@ -244,15 +234,8 @@ function workflowResultStatus(result: SingleResult): string {
|
|
|
244
234
|
}
|
|
245
235
|
|
|
246
236
|
function workflowStepLine(step: ChainStep): string {
|
|
247
|
-
const fragments = chainKeyFragments(step.result);
|
|
248
|
-
const writer = step.result.agent === "worker" || step.result.agent === "cleaner" || step.result.agent === "documenter";
|
|
249
|
-
const suffix = fragments.length > 0
|
|
250
|
-
? writer
|
|
251
|
-
? ` — changed: ${fragments.join(" · ")}`
|
|
252
|
-
: ` — ${fragments.join(" · ")}`
|
|
253
|
-
: "";
|
|
254
237
|
const id = step.runId !== undefined ? `#${step.runId} ` : "";
|
|
255
|
-
return `- ${id}${step.result.agent} · ${step.relation} · ${workflowResultStatus(step.result)}
|
|
238
|
+
return `- ${id}${step.result.agent} · ${step.relation} · ${workflowResultStatus(step.result)}`;
|
|
256
239
|
}
|
|
257
240
|
|
|
258
241
|
function appendWorkflowFooter(lines: string[], steps: readonly ChainStep[]): void {
|
|
@@ -260,7 +243,13 @@ function appendWorkflowFooter(lines: string[], steps: readonly ChainStep[]): voi
|
|
|
260
243
|
const usage = formatUsageCompact(total);
|
|
261
244
|
lines.push("", `Totals: ${steps.length} run${steps.length === 1 ? "" : "s"}${usage ? ` · ${usage}` : ""}`);
|
|
262
245
|
const ids = steps.filter((step) => step.runId !== undefined).map((step) => `#${step.runId}`);
|
|
263
|
-
lines.push(`
|
|
246
|
+
lines.push(`Per-run details: subagent_status ${ids.join(" ")}`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function formatWorkflowSummary(title: string, steps: readonly ChainStep[]): string {
|
|
250
|
+
const lines = [title, "", ...steps.map(workflowStepLine)];
|
|
251
|
+
appendWorkflowFooter(lines, steps);
|
|
252
|
+
return lines.join("\n");
|
|
264
253
|
}
|
|
265
254
|
|
|
266
255
|
/** Condensed compatibility summary for a direct REVIEW_FAIL auto-fix chain. */
|
|
@@ -269,13 +258,10 @@ export function formatChainSummary(
|
|
|
269
258
|
terminalResult: SingleResult = steps[steps.length - 1]!.result,
|
|
270
259
|
): string {
|
|
271
260
|
const rounds = steps.filter((step) => step.relation.startsWith("fix round")).length;
|
|
272
|
-
|
|
261
|
+
return formatWorkflowSummary(
|
|
273
262
|
`## Auto-fix chain: ${Math.max(1, rounds)} round${rounds === 1 ? "" : "s"} — final ${workflowResultStatus(terminalResult)}`,
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
];
|
|
277
|
-
appendWorkflowFooter(lines, steps);
|
|
278
|
-
return lines.join("\n");
|
|
263
|
+
steps,
|
|
264
|
+
);
|
|
279
265
|
}
|
|
280
266
|
|
|
281
267
|
/** One clear final delivery for all newly managed writer/documenter workflows. */
|
|
@@ -286,13 +272,10 @@ export function formatManagedWorkflowSummary(
|
|
|
286
272
|
const route = steps.map((step) => step.result.agent).join(" → ");
|
|
287
273
|
const fixRounds = steps.filter((step) => step.relation.startsWith("fix round")).length;
|
|
288
274
|
const roundNote = fixRounds > 0 ? ` · ${fixRounds} fix round${fixRounds === 1 ? "" : "s"}` : "";
|
|
289
|
-
|
|
275
|
+
return formatWorkflowSummary(
|
|
290
276
|
`## Managed workflow: ${route}${roundNote} — final ${workflowResultStatus(terminalResult)}`,
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
];
|
|
294
|
-
appendWorkflowFooter(lines, steps);
|
|
295
|
-
return lines.join("\n");
|
|
277
|
+
steps,
|
|
278
|
+
);
|
|
296
279
|
}
|
|
297
280
|
|
|
298
281
|
/** Build the first independent final gate after a top-level writer or required
|
package/src/models.ts
CHANGED
|
@@ -1,189 +1,189 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Model routing, capability-aware thinking, and setup-picker helpers.
|
|
3
|
-
*
|
|
4
|
-
* Runtime has one explicit fallback only: a configured agent model hands
|
|
5
|
-
* off directly to the current main-window model. Setup lists only currently
|
|
6
|
-
* available models and derives thinking choices from Pi's model metadata.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
clampThinkingLevel,
|
|
11
|
-
getSupportedThinkingLevels,
|
|
12
|
-
type Api,
|
|
13
|
-
type Model,
|
|
14
|
-
} from "@earendil-works/pi-ai";
|
|
15
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
16
|
-
import { DEFAULT_THINKING_LEVEL, type ThinkingLevel } from "./config.ts";
|
|
17
|
-
|
|
18
|
-
export type ModelContext = Pick<ExtensionContext, "model" | "modelRegistry"> &
|
|
19
|
-
Partial<Pick<ExtensionContext, "scopedModels">>;
|
|
20
|
-
|
|
21
|
-
export const CURRENT_MAIN_MODEL = "__current_main_model__";
|
|
22
|
-
|
|
23
|
-
export interface ModelPickerItem {
|
|
24
|
-
value: string;
|
|
25
|
-
label: string;
|
|
26
|
-
description?: string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type ModelListEntry = Pick<
|
|
30
|
-
Model<Api>,
|
|
31
|
-
"provider" | "id" | "name" | "input" | "reasoning" | "thinkingLevelMap"
|
|
32
|
-
>;
|
|
33
|
-
|
|
34
|
-
export interface ResolvedAgentModelRoute {
|
|
35
|
-
/** Effective first candidate. Undefined means let Pi use its normal default. */
|
|
36
|
-
primaryRef?: string;
|
|
37
|
-
/** Current main-window model when it differs from the selection. */
|
|
38
|
-
mainFallbackRef?: string;
|
|
39
|
-
/** Runtime order, useful for status/tests. */
|
|
40
|
-
candidateRefs: string[];
|
|
41
|
-
/** Configured ref skipped because Pi does not currently report it available. */
|
|
42
|
-
unavailableSelectedRef?: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface AgentModelRouteInput {
|
|
46
|
-
selectedRef?: string;
|
|
47
|
-
mainRef?: string;
|
|
48
|
-
declaredDefaultRef?: string;
|
|
49
|
-
/** When supplied, a configured selection outside this live set is skipped. */
|
|
50
|
-
availableRefs?: readonly string[];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function cleanModelRef(ref: string | undefined): string | undefined {
|
|
54
|
-
const trimmed = ref?.trim();
|
|
55
|
-
return trimmed || undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function modelRef(model: { provider: string; id: string }): string {
|
|
59
|
-
return `${model.provider}/${model.id}`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function currentModelRef(ctx: Pick<ModelContext, "model">): string | undefined {
|
|
63
|
-
return ctx.model ? modelRef(ctx.model) : undefined;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Current authenticated registry models narrowed by the session scope. Scope
|
|
68
|
-
* entries are a session snapshot, so they act only as a whitelist; the live
|
|
69
|
-
* registry remains the source of truth for availability and model metadata.
|
|
70
|
-
*/
|
|
71
|
-
export function availableModelsInScope(ctx: ModelContext): readonly Model<Api>[] {
|
|
72
|
-
const models = ctx.modelRegistry.getAvailable();
|
|
73
|
-
// scopedModels was added after the original Pi minimum. Treat a missing field
|
|
74
|
-
// exactly like an empty scope and use the full live registry.
|
|
75
|
-
const scopedModels = ctx.scopedModels ?? [];
|
|
76
|
-
if (scopedModels.length === 0) return models;
|
|
77
|
-
const scopedRefs = new Set(scopedModels.map((entry) => modelRef(entry.model)));
|
|
78
|
-
return models.filter((model) => scopedRefs.has(modelRef(model)));
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function findModelByRef(
|
|
82
|
-
models: readonly Model<Api>[],
|
|
83
|
-
ref: string | undefined,
|
|
84
|
-
): Model<Api> | undefined {
|
|
85
|
-
const normalized = cleanModelRef(ref);
|
|
86
|
-
return normalized ? models.find((model) => modelRef(model) === normalized) : undefined;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Resolve one agent's runtime route:
|
|
91
|
-
*
|
|
92
|
-
* configured selection -> current main-window model
|
|
93
|
-
*
|
|
94
|
-
* Without an override, current main is primary; the agent-declared default is
|
|
95
|
-
* used only when no main model exists. A configured selection that Pi no longer
|
|
96
|
-
* reports as available is skipped immediately instead of spawning a doomed child.
|
|
97
|
-
*/
|
|
98
|
-
export function resolveAgentModelRoute(input: AgentModelRouteInput): ResolvedAgentModelRoute {
|
|
99
|
-
const selectedRef = cleanModelRef(input.selectedRef);
|
|
100
|
-
const mainRef = cleanModelRef(input.mainRef);
|
|
101
|
-
const declaredDefaultRef = cleanModelRef(input.declaredDefaultRef);
|
|
102
|
-
const available = input.availableRefs
|
|
103
|
-
? new Set(input.availableRefs.map((ref) => ref.trim()).filter(Boolean))
|
|
104
|
-
: undefined;
|
|
105
|
-
const selectedAvailable = !selectedRef || !available || available.has(selectedRef);
|
|
106
|
-
const usableSelectedRef = selectedAvailable ? selectedRef : undefined;
|
|
107
|
-
const primaryRef = usableSelectedRef ?? mainRef ?? declaredDefaultRef;
|
|
108
|
-
const ordered = [primaryRef, usableSelectedRef && usableSelectedRef !== mainRef ? mainRef : undefined];
|
|
109
|
-
const candidateRefs = [...new Set(ordered.filter((ref): ref is string => Boolean(ref)))];
|
|
110
|
-
return {
|
|
111
|
-
primaryRef,
|
|
112
|
-
...(candidateRefs[1] ? { mainFallbackRef: candidateRefs[1] } : {}),
|
|
113
|
-
candidateRefs,
|
|
114
|
-
...(!selectedAvailable && selectedRef ? { unavailableSelectedRef: selectedRef } : {}),
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/** The exact levels Pi exposes for this model, including `off` when supported. */
|
|
119
|
-
export function supportedThinkingLevels(model: Model<Api> | undefined): ThinkingLevel[] {
|
|
120
|
-
return model ? (getSupportedThinkingLevels(model) as ThinkingLevel[]) : [];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/** Clamp an agent preference to the effective model's actual capability map. */
|
|
124
|
-
export function resolveThinkingLevel(
|
|
125
|
-
model: Model<Api> | undefined,
|
|
126
|
-
preferred: ThinkingLevel = DEFAULT_THINKING_LEVEL,
|
|
127
|
-
): ThinkingLevel {
|
|
128
|
-
return model ? (clampThinkingLevel(model, preferred) as ThinkingLevel) : preferred;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function modelCapabilities(model: ModelListEntry): string {
|
|
132
|
-
const input = model.input.includes("image") ? "vision" : "text-only";
|
|
133
|
-
const thinking = getSupportedThinkingLevels(model as Model<Api>).join("/");
|
|
134
|
-
return `${input} · thinking: ${thinking}`;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** Build one searchable list for agent model selection. Only models Pi
|
|
138
|
-
* currently reports as available are supplied by setup. */
|
|
139
|
-
export function buildModelPickerItems(options: {
|
|
140
|
-
models: readonly ModelListEntry[];
|
|
141
|
-
configuredRef?: string;
|
|
142
|
-
mainRef?: string;
|
|
143
|
-
}): ModelPickerItem[] {
|
|
144
|
-
const configuredRef = cleanModelRef(options.configuredRef);
|
|
145
|
-
const mainRef = cleanModelRef(options.mainRef);
|
|
146
|
-
const byRef = new Map<string, ModelListEntry>();
|
|
147
|
-
for (const model of options.models) {
|
|
148
|
-
const ref = modelRef(model);
|
|
149
|
-
if (!byRef.has(ref)) byRef.set(ref, model);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const refs = [...byRef.keys()]
|
|
153
|
-
.sort((left, right) => {
|
|
154
|
-
const leftRank = left === configuredRef ? 0 : left === mainRef ? 1 : 2;
|
|
155
|
-
const rightRank = right === configuredRef ? 0 : right === mainRef ? 1 : 2;
|
|
156
|
-
return leftRank - rightRank || left.localeCompare(right);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
const dynamic: ModelPickerItem = {
|
|
160
|
-
value: CURRENT_MAIN_MODEL,
|
|
161
|
-
label: "Current main model (dynamic)",
|
|
162
|
-
description: "Clear agent override; use the current main model dynamically",
|
|
163
|
-
};
|
|
164
|
-
const items: ModelPickerItem[] = [dynamic];
|
|
165
|
-
for (const ref of refs) {
|
|
166
|
-
const model = byRef.get(ref)!;
|
|
167
|
-
const tags = [ref === configuredRef ? "configured" : "", ref === mainRef ? "current main" : ""]
|
|
168
|
-
.filter(Boolean);
|
|
169
|
-
const name = model.name.trim() && model.name !== model.id ? model.name.trim() : undefined;
|
|
170
|
-
items.push({
|
|
171
|
-
value: ref,
|
|
172
|
-
label: ref,
|
|
173
|
-
description: [name, modelCapabilities(model), ...tags].filter(Boolean).join(" · "),
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
return items;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/** The dynamic choice removes the persisted per-agent override. */
|
|
180
|
-
export function applyAgentModelChoice(
|
|
181
|
-
current: Record<string, string>,
|
|
182
|
-
agentName: string,
|
|
183
|
-
choice: string,
|
|
184
|
-
): Record<string, string> {
|
|
185
|
-
const next = { ...current };
|
|
186
|
-
if (choice === CURRENT_MAIN_MODEL) delete next[agentName];
|
|
187
|
-
else next[agentName] = choice.trim();
|
|
188
|
-
return next;
|
|
189
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
* Model routing, capability-aware thinking, and setup-picker helpers.
|
|
3
|
+
*
|
|
4
|
+
* Runtime has one explicit fallback only: a configured agent model hands
|
|
5
|
+
* off directly to the current main-window model. Setup lists only currently
|
|
6
|
+
* available models and derives thinking choices from Pi's model metadata.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
clampThinkingLevel,
|
|
11
|
+
getSupportedThinkingLevels,
|
|
12
|
+
type Api,
|
|
13
|
+
type Model,
|
|
14
|
+
} from "@earendil-works/pi-ai";
|
|
15
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
16
|
+
import { DEFAULT_THINKING_LEVEL, type ThinkingLevel } from "./config.ts";
|
|
17
|
+
|
|
18
|
+
export type ModelContext = Pick<ExtensionContext, "model" | "modelRegistry"> &
|
|
19
|
+
Partial<Pick<ExtensionContext, "scopedModels">>;
|
|
20
|
+
|
|
21
|
+
export const CURRENT_MAIN_MODEL = "__current_main_model__";
|
|
22
|
+
|
|
23
|
+
export interface ModelPickerItem {
|
|
24
|
+
value: string;
|
|
25
|
+
label: string;
|
|
26
|
+
description?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type ModelListEntry = Pick<
|
|
30
|
+
Model<Api>,
|
|
31
|
+
"provider" | "id" | "name" | "input" | "reasoning" | "thinkingLevelMap"
|
|
32
|
+
>;
|
|
33
|
+
|
|
34
|
+
export interface ResolvedAgentModelRoute {
|
|
35
|
+
/** Effective first candidate. Undefined means let Pi use its normal default. */
|
|
36
|
+
primaryRef?: string;
|
|
37
|
+
/** Current main-window model when it differs from the selection. */
|
|
38
|
+
mainFallbackRef?: string;
|
|
39
|
+
/** Runtime order, useful for status/tests. */
|
|
40
|
+
candidateRefs: string[];
|
|
41
|
+
/** Configured ref skipped because Pi does not currently report it available. */
|
|
42
|
+
unavailableSelectedRef?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface AgentModelRouteInput {
|
|
46
|
+
selectedRef?: string;
|
|
47
|
+
mainRef?: string;
|
|
48
|
+
declaredDefaultRef?: string;
|
|
49
|
+
/** When supplied, a configured selection outside this live set is skipped. */
|
|
50
|
+
availableRefs?: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function cleanModelRef(ref: string | undefined): string | undefined {
|
|
54
|
+
const trimmed = ref?.trim();
|
|
55
|
+
return trimmed || undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function modelRef(model: { provider: string; id: string }): string {
|
|
59
|
+
return `${model.provider}/${model.id}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function currentModelRef(ctx: Pick<ModelContext, "model">): string | undefined {
|
|
63
|
+
return ctx.model ? modelRef(ctx.model) : undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Current authenticated registry models narrowed by the session scope. Scope
|
|
68
|
+
* entries are a session snapshot, so they act only as a whitelist; the live
|
|
69
|
+
* registry remains the source of truth for availability and model metadata.
|
|
70
|
+
*/
|
|
71
|
+
export function availableModelsInScope(ctx: ModelContext): readonly Model<Api>[] {
|
|
72
|
+
const models = ctx.modelRegistry.getAvailable();
|
|
73
|
+
// scopedModels was added after the original Pi minimum. Treat a missing field
|
|
74
|
+
// exactly like an empty scope and use the full live registry.
|
|
75
|
+
const scopedModels = ctx.scopedModels ?? [];
|
|
76
|
+
if (scopedModels.length === 0) return models;
|
|
77
|
+
const scopedRefs = new Set(scopedModels.map((entry) => modelRef(entry.model)));
|
|
78
|
+
return models.filter((model) => scopedRefs.has(modelRef(model)));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function findModelByRef(
|
|
82
|
+
models: readonly Model<Api>[],
|
|
83
|
+
ref: string | undefined,
|
|
84
|
+
): Model<Api> | undefined {
|
|
85
|
+
const normalized = cleanModelRef(ref);
|
|
86
|
+
return normalized ? models.find((model) => modelRef(model) === normalized) : undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Resolve one agent's runtime route:
|
|
91
|
+
*
|
|
92
|
+
* configured selection -> current main-window model
|
|
93
|
+
*
|
|
94
|
+
* Without an override, current main is primary; the agent-declared default is
|
|
95
|
+
* used only when no main model exists. A configured selection that Pi no longer
|
|
96
|
+
* reports as available is skipped immediately instead of spawning a doomed child.
|
|
97
|
+
*/
|
|
98
|
+
export function resolveAgentModelRoute(input: AgentModelRouteInput): ResolvedAgentModelRoute {
|
|
99
|
+
const selectedRef = cleanModelRef(input.selectedRef);
|
|
100
|
+
const mainRef = cleanModelRef(input.mainRef);
|
|
101
|
+
const declaredDefaultRef = cleanModelRef(input.declaredDefaultRef);
|
|
102
|
+
const available = input.availableRefs
|
|
103
|
+
? new Set(input.availableRefs.map((ref) => ref.trim()).filter(Boolean))
|
|
104
|
+
: undefined;
|
|
105
|
+
const selectedAvailable = !selectedRef || !available || available.has(selectedRef);
|
|
106
|
+
const usableSelectedRef = selectedAvailable ? selectedRef : undefined;
|
|
107
|
+
const primaryRef = usableSelectedRef ?? mainRef ?? declaredDefaultRef;
|
|
108
|
+
const ordered = [primaryRef, usableSelectedRef && usableSelectedRef !== mainRef ? mainRef : undefined];
|
|
109
|
+
const candidateRefs = [...new Set(ordered.filter((ref): ref is string => Boolean(ref)))];
|
|
110
|
+
return {
|
|
111
|
+
primaryRef,
|
|
112
|
+
...(candidateRefs[1] ? { mainFallbackRef: candidateRefs[1] } : {}),
|
|
113
|
+
candidateRefs,
|
|
114
|
+
...(!selectedAvailable && selectedRef ? { unavailableSelectedRef: selectedRef } : {}),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The exact levels Pi exposes for this model, including `off` when supported. */
|
|
119
|
+
export function supportedThinkingLevels(model: Model<Api> | undefined): ThinkingLevel[] {
|
|
120
|
+
return model ? (getSupportedThinkingLevels(model) as ThinkingLevel[]) : [];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Clamp an agent preference to the effective model's actual capability map. */
|
|
124
|
+
export function resolveThinkingLevel(
|
|
125
|
+
model: Model<Api> | undefined,
|
|
126
|
+
preferred: ThinkingLevel = DEFAULT_THINKING_LEVEL,
|
|
127
|
+
): ThinkingLevel {
|
|
128
|
+
return model ? (clampThinkingLevel(model, preferred) as ThinkingLevel) : preferred;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function modelCapabilities(model: ModelListEntry): string {
|
|
132
|
+
const input = model.input.includes("image") ? "vision" : "text-only";
|
|
133
|
+
const thinking = getSupportedThinkingLevels(model as Model<Api>).join("/");
|
|
134
|
+
return `${input} · thinking: ${thinking}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Build one searchable list for agent model selection. Only models Pi
|
|
138
|
+
* currently reports as available are supplied by setup. */
|
|
139
|
+
export function buildModelPickerItems(options: {
|
|
140
|
+
models: readonly ModelListEntry[];
|
|
141
|
+
configuredRef?: string;
|
|
142
|
+
mainRef?: string;
|
|
143
|
+
}): ModelPickerItem[] {
|
|
144
|
+
const configuredRef = cleanModelRef(options.configuredRef);
|
|
145
|
+
const mainRef = cleanModelRef(options.mainRef);
|
|
146
|
+
const byRef = new Map<string, ModelListEntry>();
|
|
147
|
+
for (const model of options.models) {
|
|
148
|
+
const ref = modelRef(model);
|
|
149
|
+
if (!byRef.has(ref)) byRef.set(ref, model);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const refs = [...byRef.keys()]
|
|
153
|
+
.sort((left, right) => {
|
|
154
|
+
const leftRank = left === configuredRef ? 0 : left === mainRef ? 1 : 2;
|
|
155
|
+
const rightRank = right === configuredRef ? 0 : right === mainRef ? 1 : 2;
|
|
156
|
+
return leftRank - rightRank || left.localeCompare(right);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const dynamic: ModelPickerItem = {
|
|
160
|
+
value: CURRENT_MAIN_MODEL,
|
|
161
|
+
label: "Current main model (dynamic)",
|
|
162
|
+
description: "Clear agent override; use the current main model dynamically",
|
|
163
|
+
};
|
|
164
|
+
const items: ModelPickerItem[] = [dynamic];
|
|
165
|
+
for (const ref of refs) {
|
|
166
|
+
const model = byRef.get(ref)!;
|
|
167
|
+
const tags = [ref === configuredRef ? "configured" : "", ref === mainRef ? "current main" : ""]
|
|
168
|
+
.filter(Boolean);
|
|
169
|
+
const name = model.name.trim() && model.name !== model.id ? model.name.trim() : undefined;
|
|
170
|
+
items.push({
|
|
171
|
+
value: ref,
|
|
172
|
+
label: ref,
|
|
173
|
+
description: [name, modelCapabilities(model), ...tags].filter(Boolean).join(" · "),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return items;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** The dynamic choice removes the persisted per-agent override. */
|
|
180
|
+
export function applyAgentModelChoice(
|
|
181
|
+
current: Record<string, string>,
|
|
182
|
+
agentName: string,
|
|
183
|
+
choice: string,
|
|
184
|
+
): Record<string, string> {
|
|
185
|
+
const next = { ...current };
|
|
186
|
+
if (choice === CURRENT_MAIN_MODEL) delete next[agentName];
|
|
187
|
+
else next[agentName] = choice.trim();
|
|
188
|
+
return next;
|
|
189
|
+
}
|