@ferris1225/pi-subagents 4.1.3 → 4.1.4

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/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
+ }
package/src/prompt.ts CHANGED
@@ -34,8 +34,8 @@ export function buildDelegationDirective(
34
34
  ...(hasDocumenter ? ["documenter"] : []),
35
35
  ];
36
36
  const automaticWriterRoute = [
37
- ...(hasDocumenter ? ["documenter"] : []),
38
37
  ...(hasReviewer ? ["reviewer"] : []),
38
+ ...(hasDocumenter ? ["documenter"] : []),
39
39
  ].join(" → ");
40
40
  const namedWorktreeTargets = [
41
41
  ...(hasWorker ? ["worker"] : []),
@@ -65,7 +65,7 @@ export function buildDelegationDirective(
65
65
  : []),
66
66
  ...(hasDocumenter
67
67
  ? [
68
- `Use \`documenter\` directly for explicit whole-codebase maintenance or standalone documentation work.${codeWriterNames.length > 0 ? ` Successful ${codeWriterNames.join("/")} runs already auto-sync the actual diff; never dispatch a duplicate.` : ""} Zero edits is valid and broad mode is never inferred. It changes docs/comments only and never runtime behavior, versions, release state, or ${hasReviewer ? "the final reviewer gate" : "direct final verification"}.`,
68
+ `Use \`documenter\` directly for explicit whole-codebase maintenance or standalone documentation work.${codeWriterNames.length > 0 ? ` Successful ${codeWriterNames.join("/")} runs already auto-sync the actual diff once after the review gate; never dispatch a duplicate.` : ""} Zero edits is valid and broad mode is never inferred. It changes docs/comments only and never runtime behavior, versions, or release state.`,
69
69
  ]
70
70
  : []),
71
71
  ...(hasReviewer
@@ -103,7 +103,7 @@ export function buildDelegationDirective(
103
103
  ? [
104
104
  ...(hasDocumenter
105
105
  ? [
106
- `A direct REVIEW_PASS is preliminary: runtime runs documenter on the pending diff, then a fresh reviewer. A direct REVIEW_FAIL ${autoFixEnabled ? "keeps auto-fix; maxFixRounds limits worker fixes only, not initial docs/review." : "cannot start fixes while worker/fix rounds are disabled."}`,
106
+ `A direct REVIEW_PASS is final for code: runtime runs the final documentation sync once and delivers. A direct REVIEW_FAIL ${autoFixEnabled ? "keeps auto-fix; maxFixRounds limits worker fixes only, not the final documentation sync." : "cannot start fixes while worker/fix rounds are disabled."}`,
107
107
  ]
108
108
  : []),
109
109
  "Resolve every gate finding; do not bypass the configured auto-fix/re-review cap. A reviewer report without a standalone VERDICT is advisory and cannot trigger writes.",