@narumitw/pi-plan-mode 0.57.1 → 0.58.0

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.
@@ -1,14 +1,28 @@
1
1
  import type { ExtensionContext, ToolInfo } from "@earendil-works/pi-coding-agent";
2
- import { defineMenu, type RunMenuResult, runMenu } from "@narumitw/pi-tui-kit";
2
+ import {
3
+ defineMenu,
4
+ type RunMenuResult,
5
+ runMenu,
6
+ sanitizeTerminalText,
7
+ } from "@narumitw/pi-tui-kit";
3
8
  import { PLAN_MODE_COMPLETE_TOOL_NAME } from "./completion-tool.js";
9
+ import {
10
+ type AvailableImplementationModel,
11
+ findAvailableImplementationModel,
12
+ type ImplementationModelOverride,
13
+ snapshotAvailableImplementationModels,
14
+ } from "./implementation-models.js";
4
15
  import { retentionLabel } from "./implementation-retention.js";
5
16
  import { planExportDestination } from "./plan-export.js";
6
17
  import { PLAN_MODE_QUESTION_TOOL_NAME } from "./question-tool.js";
7
18
  import {
19
+ configuredImplementationModel,
8
20
  configuredImplementationPlanRetention,
21
+ configuredImplementationThinkingLevel,
9
22
  configuredPlanExportPath,
10
23
  configuredPlanModeToggleShortcut,
11
24
  IMPLEMENTATION_PLAN_RETENTIONS,
25
+ IMPLEMENTATION_THINKING_LEVELS,
12
26
  normalizeKeyId,
13
27
  PLAN_MODE_THINKING_LEVELS,
14
28
  type PlanModeSettings,
@@ -44,13 +58,16 @@ export interface PlanModeSettingsMenuOptions {
44
58
  onSaved(settings: PlanModeSettings): void;
45
59
  }
46
60
 
47
- type Screen = "settings" | "tools" | "export" | "shortcut";
61
+ type Screen = "settings" | "tools" | "implementation-model" | "export" | "shortcut";
48
62
  type Action =
49
63
  | "set-thinking"
50
64
  | "open-tools"
51
65
  | "toggle-tool"
52
66
  | "reset-tools"
53
67
  | "set-retention"
68
+ | "open-implementation-model"
69
+ | "set-implementation-model"
70
+ | "set-implementation-thinking"
54
71
  | "open-export"
55
72
  | "set-export"
56
73
  | "open-shortcut"
@@ -74,6 +91,13 @@ export async function showPlanModeSettings(
74
91
  tools.map((tool, index) => [tool.name, `plan-settings-tool:${index}`]),
75
92
  );
76
93
  const toolsByItemId = new Map(tools.map((tool) => [toolItemIds.get(tool.name) as string, tool]));
94
+ const implementationModels = snapshotAvailableImplementationModels(ctx);
95
+ const modelItemIds = new Map(
96
+ implementationModels.map((model, index) => [model, `plan-settings-model:${index}`]),
97
+ );
98
+ const modelsByItemId = new Map(
99
+ implementationModels.map((model) => [modelItemIds.get(model) as string, model]),
100
+ );
77
101
 
78
102
  const loadState = async (): Promise<SettingsMenuState> => {
79
103
  const loaded = await readSettings(options.settingsPath);
@@ -130,6 +154,23 @@ export async function showPlanModeSettings(
130
154
  values: IMPLEMENTATION_PLAN_RETENTIONS.map(retentionLabel),
131
155
  action: "set-retention",
132
156
  },
157
+ {
158
+ id: "defaultImplementationModel",
159
+ label: "Fresh model",
160
+ description: "Choose the default model for a fresh implementation session.",
161
+ currentValue: implementationModelValue(state.settings, implementationModels),
162
+ action: "open-implementation-model",
163
+ },
164
+ {
165
+ id: "defaultImplementationThinkingLevel",
166
+ label: "Fresh thinking",
167
+ description:
168
+ "Choose the default thinking level for a fresh implementation session.",
169
+ currentValue:
170
+ configuredImplementationThinkingLevel(state.settings) ?? "same as plan",
171
+ values: ["same as plan", ...IMPLEMENTATION_THINKING_LEVELS],
172
+ action: "set-implementation-thinking",
173
+ },
133
174
  {
134
175
  id: "defaultPlanExportPath",
135
176
  label: "Export destination",
@@ -172,6 +213,21 @@ export async function showPlanModeSettings(
172
213
  ],
173
214
  hint: "back",
174
215
  }),
216
+ "implementation-model": ({ state }) => ({
217
+ kind: "choice",
218
+ title: "Fresh implementation model",
219
+ lines: ["Same as plan is the default and fallback when a configured model is unavailable."],
220
+ items: implementationModelItems(implementationModels, modelItemIds),
221
+ action: "set-implementation-model",
222
+ initialItemId: implementationModelItemId(
223
+ configuredImplementationModel(state.settings),
224
+ implementationModels,
225
+ modelItemIds,
226
+ ),
227
+ enableSearch: true,
228
+ viewportSize: 10,
229
+ hint: "back",
230
+ }),
175
231
  export: ({ state }) => {
176
232
  const configured = configuredPlanExportPath(state.settings);
177
233
  const destination = planExportDestination(configured, ctx.cwd);
@@ -227,6 +283,52 @@ export async function showPlanModeSettings(
227
283
  `Plan reinjection: ${retentionLabel(implementationPlanRetention)}. Applies to the next Implement action.`,
228
284
  );
229
285
  },
286
+ "open-implementation-model": async () => ({
287
+ kind: "to",
288
+ screen: "implementation-model",
289
+ }),
290
+ "set-implementation-model": async ({ ctx: actionCtx, itemId, signal }) => {
291
+ const model = itemId ? modelsByItemId.get(itemId) : undefined;
292
+ if (itemId !== "same-as-plan" && !model) return { kind: "rejected" };
293
+ const defaultImplementationModel = model
294
+ ? { provider: model.provider, modelId: model.id }
295
+ : null;
296
+ const result = await savePatch(
297
+ actionCtx,
298
+ { defaultImplementationModel },
299
+ signal,
300
+ model
301
+ ? `Fresh implementation model: ${safeModelReference({ provider: model.provider, modelId: model.id })}.`
302
+ : "Fresh implementation model: same as plan.",
303
+ );
304
+ return result.kind === "stay" ? { kind: "to", screen: "settings" } : result;
305
+ },
306
+ "set-implementation-thinking": async ({ ctx: actionCtx, value, signal }) => {
307
+ if (value === "same as plan") {
308
+ return savePatch(
309
+ actionCtx,
310
+ { defaultImplementationThinkingLevel: null },
311
+ signal,
312
+ "Fresh implementation thinking: same as plan.",
313
+ );
314
+ }
315
+ if (
316
+ !IMPLEMENTATION_THINKING_LEVELS.includes(
317
+ value as (typeof IMPLEMENTATION_THINKING_LEVELS)[number],
318
+ )
319
+ ) {
320
+ return { kind: "rejected" };
321
+ }
322
+ return savePatch(
323
+ actionCtx,
324
+ {
325
+ defaultImplementationThinkingLevel:
326
+ value as PlanModeSettings["defaultImplementationThinkingLevel"],
327
+ },
328
+ signal,
329
+ `Fresh implementation thinking: ${value}.`,
330
+ );
331
+ },
230
332
  "open-export": async () => ({ kind: "to", screen: "export" }),
231
333
  "set-export": async ({ ctx: actionCtx, value, signal }) => {
232
334
  const defaultPlanExportPath = value?.trim() || null;
@@ -349,6 +451,59 @@ function retentionFromLabel(value: string | undefined) {
349
451
  return IMPLEMENTATION_PLAN_RETENTIONS.find((retention) => retentionLabel(retention) === value);
350
452
  }
351
453
 
454
+ function implementationModelValue(
455
+ settings: PlanModeSettings,
456
+ models: readonly AvailableImplementationModel[],
457
+ ) {
458
+ const configured = configuredImplementationModel(settings);
459
+ if (!configured) return "same as plan";
460
+ return findAvailableImplementationModel(models, configured)
461
+ ? safeModelReference(configured)
462
+ : `same as plan · ${safeModelReference(configured)} unavailable`;
463
+ }
464
+
465
+ function implementationModelItems(
466
+ models: readonly AvailableImplementationModel[],
467
+ itemIds: ReadonlyMap<AvailableImplementationModel, string>,
468
+ ) {
469
+ return [
470
+ {
471
+ id: "same-as-plan",
472
+ label: "Same as plan",
473
+ description: "Use the planning session model.",
474
+ },
475
+ ...models.map((model) => {
476
+ const reference = safeModelReference({ provider: model.provider, modelId: model.id });
477
+ const name = safeModelMetadata(model.name, "");
478
+ return {
479
+ id: itemIds.get(model) as string,
480
+ label: reference,
481
+ ...(name ? { details: [`Model Name: ${name}`] } : {}),
482
+ searchText: [reference, name].filter(Boolean).join(" "),
483
+ };
484
+ }),
485
+ ];
486
+ }
487
+
488
+ function implementationModelItemId(
489
+ configured: ImplementationModelOverride | undefined,
490
+ models: readonly AvailableImplementationModel[],
491
+ itemIds: ReadonlyMap<AvailableImplementationModel, string>,
492
+ ) {
493
+ const model = findAvailableImplementationModel(models, configured);
494
+ return model ? itemIds.get(model) : "same-as-plan";
495
+ }
496
+
497
+ function safeModelReference(model: ImplementationModelOverride) {
498
+ return `${safeModelMetadata(model.modelId, "unknown model")} [${safeModelMetadata(model.provider, "unknown provider")}]`;
499
+ }
500
+
501
+ function safeModelMetadata(value: unknown, fallback: string) {
502
+ if (typeof value !== "string") return fallback;
503
+ const safe = sanitizeTerminalText(value).trim() || fallback;
504
+ return [...safe].slice(0, 512).join("");
505
+ }
506
+
352
507
  function defaultToolsValue(configured: string[] | undefined) {
353
508
  if (configured === undefined) return "Automatic safe built-ins";
354
509
  if (configured.length === 0) return "No optional tools";
package/src/settings.ts CHANGED
@@ -4,6 +4,10 @@ import { mkdir, open, rename, rm, writeFile } from "node:fs/promises";
4
4
  import { basename, dirname, join } from "node:path";
5
5
  import { getAgentDir } from "@earendil-works/pi-coding-agent";
6
6
  import type { KeyId } from "@earendil-works/pi-tui";
7
+ import {
8
+ type ImplementationModelOverride,
9
+ isPendingImplementationModelIdentifier,
10
+ } from "./implementation-models.js";
7
11
  import type { SafeSubcommands } from "./tool-policy.js";
8
12
 
9
13
  export const PLAN_MODE_SETTINGS_FILE = "pi-plan-mode.json";
@@ -19,6 +23,15 @@ export const PLAN_MODE_THINKING_LEVELS = [
19
23
  "xhigh",
20
24
  "max",
21
25
  ] as const;
26
+ export const IMPLEMENTATION_THINKING_LEVELS = [
27
+ "off",
28
+ "minimal",
29
+ "low",
30
+ "medium",
31
+ "high",
32
+ "xhigh",
33
+ "max",
34
+ ] as const;
22
35
  export const IMPLEMENTATION_PLAN_RETENTIONS = [
23
36
  "clear-on-start",
24
37
  "clear-after-first-run",
@@ -83,11 +96,13 @@ const MAX_PLAN_EXPORT_PATH_LENGTH = 4096;
83
96
 
84
97
  export type PlanModeThinkingLevel = (typeof PLAN_MODE_THINKING_LEVELS)[number];
85
98
  export type ImplementationPlanRetention = (typeof IMPLEMENTATION_PLAN_RETENTIONS)[number];
86
- export type PlanModeFixedThinkingLevel = Exclude<PlanModeThinkingLevel, "inherit">;
99
+ export type PlanModeFixedThinkingLevel = (typeof IMPLEMENTATION_THINKING_LEVELS)[number];
87
100
  export interface PlanModeSettings {
88
101
  thinkingLevel: PlanModeThinkingLevel;
89
102
  defaultPlanTools?: string[];
90
103
  implementationPlanRetention?: ImplementationPlanRetention;
104
+ defaultImplementationModel?: ImplementationModelOverride;
105
+ defaultImplementationThinkingLevel?: PlanModeFixedThinkingLevel;
91
106
  defaultPlanExportPath?: string;
92
107
  safeSubcommands?: SafeSubcommands;
93
108
  toggleShortcut?: KeyId;
@@ -96,6 +111,8 @@ export interface PlanModeSettingsPatch {
96
111
  thinkingLevel?: PlanModeThinkingLevel;
97
112
  defaultPlanTools?: readonly string[] | null;
98
113
  implementationPlanRetention?: ImplementationPlanRetention;
114
+ defaultImplementationModel?: ImplementationModelOverride | null;
115
+ defaultImplementationThinkingLevel?: PlanModeFixedThinkingLevel | null;
99
116
  defaultPlanExportPath?: string | null;
100
117
  toggleShortcut?: KeyId | null;
101
118
  }
@@ -154,6 +171,28 @@ export function normalizePlanModeSettings(value: unknown): PlanModeSettings | un
154
171
  settings.implementationPlanRetention =
155
172
  implementationPlanRetention as ImplementationPlanRetention;
156
173
  }
174
+ if (Object.hasOwn(value, "defaultImplementationModel")) {
175
+ const defaultImplementationModel = normalizeImplementationModel(
176
+ Reflect.get(value, "defaultImplementationModel"),
177
+ );
178
+ if (!defaultImplementationModel) return undefined;
179
+ settings.defaultImplementationModel = defaultImplementationModel;
180
+ }
181
+ if (Object.hasOwn(value, "defaultImplementationThinkingLevel")) {
182
+ const defaultImplementationThinkingLevel = Reflect.get(
183
+ value,
184
+ "defaultImplementationThinkingLevel",
185
+ );
186
+ if (
187
+ !IMPLEMENTATION_THINKING_LEVELS.includes(
188
+ defaultImplementationThinkingLevel as PlanModeFixedThinkingLevel,
189
+ )
190
+ ) {
191
+ return undefined;
192
+ }
193
+ settings.defaultImplementationThinkingLevel =
194
+ defaultImplementationThinkingLevel as PlanModeFixedThinkingLevel;
195
+ }
157
196
  if (Object.hasOwn(value, "defaultPlanExportPath")) {
158
197
  const defaultPlanExportPath = normalizePlanExportPath(
159
198
  Reflect.get(value, "defaultPlanExportPath"),
@@ -174,6 +213,24 @@ export function normalizePlanModeSettings(value: unknown): PlanModeSettings | un
174
213
  return settings;
175
214
  }
176
215
 
216
+ function normalizeImplementationModel(value: unknown): ImplementationModelOverride | undefined {
217
+ if (
218
+ !isSettingsDocument(value) ||
219
+ Object.keys(value).some((key) => key !== "provider" && key !== "modelId")
220
+ ) {
221
+ return undefined;
222
+ }
223
+ const provider = typeof value.provider === "string" ? value.provider.trim() : value.provider;
224
+ const modelId = typeof value.modelId === "string" ? value.modelId.trim() : value.modelId;
225
+ if (
226
+ !isPendingImplementationModelIdentifier(provider) ||
227
+ !isPendingImplementationModelIdentifier(modelId)
228
+ ) {
229
+ return undefined;
230
+ }
231
+ return { provider, modelId };
232
+ }
233
+
177
234
  function normalizeToolNames(value: unknown) {
178
235
  if (
179
236
  !Array.isArray(value) ||
@@ -296,6 +353,17 @@ export function updatePlanModeSettings(
296
353
  if (patch.implementationPlanRetention !== undefined) {
297
354
  updated.implementationPlanRetention = patch.implementationPlanRetention;
298
355
  }
356
+ if (patch.defaultImplementationModel === null) delete updated.defaultImplementationModel;
357
+ else if (patch.defaultImplementationModel !== undefined) {
358
+ const model = normalizeImplementationModel(patch.defaultImplementationModel);
359
+ if (!model) throw invalidSettingsError(settingsPath, "invalid implementation model");
360
+ updated.defaultImplementationModel = model;
361
+ }
362
+ if (patch.defaultImplementationThinkingLevel === null) {
363
+ delete updated.defaultImplementationThinkingLevel;
364
+ } else if (patch.defaultImplementationThinkingLevel !== undefined) {
365
+ updated.defaultImplementationThinkingLevel = patch.defaultImplementationThinkingLevel;
366
+ }
299
367
  if (patch.defaultPlanExportPath === null) delete updated.defaultPlanExportPath;
300
368
  else if (patch.defaultPlanExportPath !== undefined) {
301
369
  updated.defaultPlanExportPath = patch.defaultPlanExportPath;
@@ -478,6 +546,18 @@ export function configuredImplementationPlanRetention(
478
546
  return settings.implementationPlanRetention ?? "clear-on-start";
479
547
  }
480
548
 
549
+ export function configuredImplementationModel(
550
+ settings: PlanModeSettings,
551
+ ): ImplementationModelOverride | undefined {
552
+ return settings.defaultImplementationModel;
553
+ }
554
+
555
+ export function configuredImplementationThinkingLevel(
556
+ settings: PlanModeSettings,
557
+ ): PlanModeFixedThinkingLevel | undefined {
558
+ return settings.defaultImplementationThinkingLevel;
559
+ }
560
+
481
561
  export function configuredPlanExportPath(settings: PlanModeSettings) {
482
562
  return settings.defaultPlanExportPath ?? DEFAULT_PLAN_EXPORT_PATH;
483
563
  }
package/src/state.ts CHANGED
@@ -3,6 +3,10 @@ import {
3
3
  PLAN_MODE_COMPLETE_TOOL_NAME,
4
4
  planFromCompletionDetails,
5
5
  } from "./completion-tool.js";
6
+ import {
7
+ type ImplementationModelOverride,
8
+ isPendingImplementationModelIdentifier,
9
+ } from "./implementation-models.js";
6
10
  import {
7
11
  IMPLEMENTATION_PLAN_RETENTIONS,
8
12
  type ImplementationPlanRetention,
@@ -10,6 +14,12 @@ import {
10
14
  type PlanModeFixedThinkingLevel,
11
15
  } from "./settings.js";
12
16
 
17
+ export type { ImplementationModelOverride } from "./implementation-models.js";
18
+ export {
19
+ isPendingImplementationModelIdentifier,
20
+ MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH,
21
+ } from "./implementation-models.js";
22
+
13
23
  export type PlanCompletionSource = typeof PLAN_MODE_COMPLETE_TOOL_NAME | "legacy_proposed_plan";
14
24
 
15
25
  export interface ActiveImplementationPlan {
@@ -25,6 +35,15 @@ export interface SavedPlan {
25
35
  source: PlanCompletionSource;
26
36
  }
27
37
 
38
+ export interface ImplementationRuntimeSelection {
39
+ model?: ImplementationModelOverride;
40
+ thinkingLevel?: PlanModeFixedThinkingLevel;
41
+ }
42
+
43
+ export interface PendingImplementationRuntime extends ImplementationRuntimeSelection {
44
+ version: 1;
45
+ }
46
+
28
47
  export interface PlanModeWorkflowToolPolicy {
29
48
  kind: "automatic" | "explicit";
30
49
  desiredNames?: string[];
@@ -39,6 +58,7 @@ export interface PlanModeState {
39
58
  awaitingAction: boolean;
40
59
  savedPlan?: SavedPlan;
41
60
  activeImplementation?: ActiveImplementationPlan;
61
+ pendingImplementationRuntime?: PendingImplementationRuntime;
42
62
  selectedToolNames?: string[];
43
63
  selectedToolKeys?: string[];
44
64
  workflowToolPolicy?: PlanModeWorkflowToolPolicy;
@@ -82,6 +102,9 @@ export function restorePlanModeState(entries: unknown[], stateEntryType: string)
82
102
  : normalizeActiveImplementation(entry.data.activeImplementation);
83
103
  const savedPlan =
84
104
  enabled || activeImplementation ? undefined : normalizeSavedPlan(entry.data.savedPlan);
105
+ const pendingImplementationRuntime = enabled
106
+ ? undefined
107
+ : normalizePendingImplementationRuntime(entry.data.pendingImplementationRuntime);
85
108
  return {
86
109
  enabled,
87
110
  latestPlan,
@@ -92,6 +115,7 @@ export function restorePlanModeState(entries: unknown[], stateEntryType: string)
92
115
  awaitingAction: enabled && latestPlan !== undefined,
93
116
  savedPlan,
94
117
  activeImplementation,
118
+ pendingImplementationRuntime,
95
119
  selectedToolNames: stringArray(entry.data.selectedToolNames),
96
120
  selectedToolKeys: stringArray(entry.data.selectedToolKeys),
97
121
  workflowToolPolicy: enabled
@@ -132,6 +156,44 @@ function normalizeSavedPlan(value: unknown): SavedPlan | undefined {
132
156
  return { plan: normalized.plan, source };
133
157
  }
134
158
 
159
+ function normalizePendingImplementationRuntime(
160
+ value: unknown,
161
+ ): PendingImplementationRuntime | undefined {
162
+ if (!isRecord(value) || value.version !== 1) return undefined;
163
+ if (
164
+ Object.keys(value).some(
165
+ (key) => key !== "version" && key !== "model" && key !== "thinkingLevel",
166
+ )
167
+ ) {
168
+ return undefined;
169
+ }
170
+ let model: ImplementationModelOverride | undefined;
171
+ if (value.model !== undefined) {
172
+ if (
173
+ !isRecord(value.model) ||
174
+ Object.keys(value.model).some((key) => key !== "provider" && key !== "modelId")
175
+ ) {
176
+ return undefined;
177
+ }
178
+ if (
179
+ !isPendingImplementationModelIdentifier(value.model.provider) ||
180
+ !isPendingImplementationModelIdentifier(value.model.modelId)
181
+ ) {
182
+ return undefined;
183
+ }
184
+ model = { provider: value.model.provider, modelId: value.model.modelId };
185
+ }
186
+ const thinkingLevel =
187
+ value.thinkingLevel === undefined ? undefined : fixedThinkingLevel(value.thinkingLevel);
188
+ if (value.thinkingLevel !== undefined && !thinkingLevel) return undefined;
189
+ if (!model && !thinkingLevel) return undefined;
190
+ return {
191
+ version: 1,
192
+ ...(model ? { model } : {}),
193
+ ...(thinkingLevel ? { thinkingLevel } : {}),
194
+ };
195
+ }
196
+
135
197
  function normalizeActiveImplementation(value: unknown): ActiveImplementationPlan | undefined {
136
198
  if (!isRecord(value)) return undefined;
137
199
  const id =