@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,6 +1,14 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
2
+ import { defineMenu, runMenu, sanitizeTerminalText } from "@narumitw/pi-tui-kit";
3
+ import {
4
+ type AvailableImplementationModel,
5
+ findAvailableImplementationModel,
6
+ type ImplementationModelOverride,
7
+ snapshotAvailableImplementationModels,
8
+ } from "./implementation-models.js";
3
9
  import { type PlanExportDestinationProvider, planExportInputScreen } from "./plan-export-screen.js";
10
+ import { IMPLEMENTATION_THINKING_LEVELS, type PlanModeFixedThinkingLevel } from "./settings.js";
11
+ import type { ImplementationRuntimeSelection } from "./state.js";
4
12
 
5
13
  interface MenuLifecycle {
6
14
  signal: AbortSignal;
@@ -12,15 +20,30 @@ const IMPLEMENTATION_CONTEXT_LINES = [
12
20
  "Start fresh transfers only the approved plan to a new session.",
13
21
  ] as const;
14
22
 
23
+ const THINKING_LEVEL_DESCRIPTIONS: Record<PlanModeFixedThinkingLevel, string> = {
24
+ off: "No reasoning",
25
+ minimal: "Very brief reasoning (~1k tokens)",
26
+ low: "Light reasoning (~2k tokens)",
27
+ medium: "Moderate reasoning (~8k tokens)",
28
+ high: "Deep reasoning (~16k tokens)",
29
+ xhigh: "Extra-high reasoning (~32k tokens)",
30
+ max: "Maximum reasoning",
31
+ };
32
+
15
33
  interface PlanMenuOptions extends MenuLifecycle {
16
34
  statusText: string;
35
+ planThinkingLevel: PlanModeFixedThinkingLevel | undefined;
36
+ implementationDefaults?: ImplementationRuntimeSelection;
17
37
  hasReadyPlan: boolean;
18
38
  implementationOutcome(): string;
19
39
  getExportDestination: PlanExportDestinationProvider;
20
40
  show(): void;
21
41
  finalize(): void;
22
42
  implementHere(): void | Promise<void>;
23
- implementFresh(signal: AbortSignal): void | Promise<void>;
43
+ implementFresh(
44
+ runtime: ImplementationRuntimeSelection,
45
+ signal: AbortSignal,
46
+ ): void | Promise<void>;
24
47
  exportPlan(path: string, signal: AbortSignal): Promise<boolean>;
25
48
  save(): void;
26
49
  stay(): void;
@@ -28,16 +51,24 @@ interface PlanMenuOptions extends MenuLifecycle {
28
51
  }
29
52
 
30
53
  export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuOptions) {
31
- type Screen = "main" | "export";
54
+ type Screen = "main" | "fresh" | "models" | "thinking" | "export";
32
55
  type Action =
33
56
  | "show"
34
57
  | "finalize"
35
58
  | "implement-here"
36
- | "implement-fresh"
59
+ | "select-model"
60
+ | "select-thinking"
61
+ | "start-fresh"
37
62
  | "export"
38
63
  | "save"
39
64
  | "stay"
40
65
  | "exit";
66
+ const freshFlow = createFreshImplementationFlow(
67
+ ctx,
68
+ options.planThinkingLevel,
69
+ options.implementationDefaults,
70
+ options.implementFresh,
71
+ );
41
72
  const menu = defineMenu<undefined, Screen, Action, ExtensionContext>({
42
73
  start: "main",
43
74
  screens: {
@@ -62,9 +93,8 @@ export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuO
62
93
  {
63
94
  id: "implement-fresh",
64
95
  label: "Start fresh and implement",
65
- description: "Open a new linked session; transfer only the approved plan.",
66
- action: "implement-fresh",
67
- busyLabel: "Starting fresh implementation session…",
96
+ description: "Configure one-shot model and thinking choices first.",
97
+ to: "fresh",
68
98
  },
69
99
  { id: "export", label: "Export plan…", to: "export" },
70
100
  { id: "save", label: "Save for later", action: "save" },
@@ -78,6 +108,9 @@ export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuO
78
108
  ],
79
109
  hint: "close",
80
110
  }),
111
+ fresh: freshFlow.settingsScreen,
112
+ models: freshFlow.modelScreen,
113
+ thinking: freshFlow.thinkingScreen,
81
114
  export: () => planExportInputScreen(options.getExportDestination),
82
115
  },
83
116
  actions: {
@@ -93,8 +126,16 @@ export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuO
93
126
  await options.implementHere();
94
127
  return { kind: "close" };
95
128
  },
96
- "implement-fresh": async ({ signal }) => {
97
- await options.implementFresh(signal);
129
+ "select-model": async ({ itemId }) => {
130
+ freshFlow.selectModel(itemId);
131
+ return { kind: "back" };
132
+ },
133
+ "select-thinking": async ({ itemId }) => {
134
+ freshFlow.selectThinking(itemId);
135
+ return { kind: "back" };
136
+ },
137
+ "start-fresh": async ({ signal }) => {
138
+ await freshFlow.start(signal);
98
139
  return { kind: "close" };
99
140
  },
100
141
  export: async ({ value, signal }) =>
@@ -121,10 +162,15 @@ export async function showPlanModeMenu(ctx: ExtensionContext, options: PlanMenuO
121
162
  }
122
163
 
123
164
  interface ReadyPlanMenuOptions extends MenuLifecycle {
165
+ planThinkingLevel: PlanModeFixedThinkingLevel | undefined;
166
+ implementationDefaults?: ImplementationRuntimeSelection;
124
167
  implementationOutcome(): string;
125
168
  getExportDestination: PlanExportDestinationProvider;
126
169
  implementHere(): void | Promise<void>;
127
- implementFresh(signal: AbortSignal): void | Promise<void>;
170
+ implementFresh(
171
+ runtime: ImplementationRuntimeSelection,
172
+ signal: AbortSignal,
173
+ ): void | Promise<void>;
128
174
  exportPlan(path: string, signal: AbortSignal): Promise<boolean>;
129
175
  save(): void;
130
176
  stay(): void;
@@ -132,8 +178,22 @@ interface ReadyPlanMenuOptions extends MenuLifecycle {
132
178
  }
133
179
 
134
180
  export async function showReadyPlanMenu(ctx: ExtensionContext, options: ReadyPlanMenuOptions) {
135
- type Screen = "ready" | "export";
136
- type Action = "implement-here" | "implement-fresh" | "export" | "save" | "stay" | "exit";
181
+ type Screen = "ready" | "fresh" | "models" | "thinking" | "export";
182
+ type Action =
183
+ | "implement-here"
184
+ | "select-model"
185
+ | "select-thinking"
186
+ | "start-fresh"
187
+ | "export"
188
+ | "save"
189
+ | "stay"
190
+ | "exit";
191
+ const freshFlow = createFreshImplementationFlow(
192
+ ctx,
193
+ options.planThinkingLevel,
194
+ options.implementationDefaults,
195
+ options.implementFresh,
196
+ );
137
197
  const menu = defineMenu<undefined, Screen, Action, ExtensionContext>({
138
198
  start: "ready",
139
199
  screens: {
@@ -151,9 +211,8 @@ export async function showReadyPlanMenu(ctx: ExtensionContext, options: ReadyPla
151
211
  {
152
212
  id: "implement-fresh",
153
213
  label: "Start fresh and implement",
154
- description: "Open a new linked session; transfer only the approved plan.",
155
- action: "implement-fresh",
156
- busyLabel: "Starting fresh implementation session…",
214
+ description: "Configure one-shot model and thinking choices first.",
215
+ to: "fresh",
157
216
  },
158
217
  { id: "export", label: "Export plan…", to: "export" },
159
218
  { id: "save", label: "Save for later", action: "save" },
@@ -162,6 +221,9 @@ export async function showReadyPlanMenu(ctx: ExtensionContext, options: ReadyPla
162
221
  ],
163
222
  hint: "close",
164
223
  }),
224
+ fresh: freshFlow.settingsScreen,
225
+ models: freshFlow.modelScreen,
226
+ thinking: freshFlow.thinkingScreen,
165
227
  export: () => planExportInputScreen(options.getExportDestination),
166
228
  },
167
229
  actions: {
@@ -169,8 +231,16 @@ export async function showReadyPlanMenu(ctx: ExtensionContext, options: ReadyPla
169
231
  await options.implementHere();
170
232
  return { kind: "close" };
171
233
  },
172
- "implement-fresh": async ({ signal }) => {
173
- await options.implementFresh(signal);
234
+ "select-model": async ({ itemId }) => {
235
+ freshFlow.selectModel(itemId);
236
+ return { kind: "back" };
237
+ },
238
+ "select-thinking": async ({ itemId }) => {
239
+ freshFlow.selectThinking(itemId);
240
+ return { kind: "back" };
241
+ },
242
+ "start-fresh": async ({ signal }) => {
243
+ await freshFlow.start(signal);
174
244
  return { kind: "close" };
175
245
  },
176
246
  export: async ({ value, signal }) =>
@@ -195,3 +265,188 @@ export async function showReadyPlanMenu(ctx: ExtensionContext, options: ReadyPla
195
265
  isCurrent: options.isCurrent,
196
266
  });
197
267
  }
268
+
269
+ interface ModelChoice {
270
+ itemId: string;
271
+ model: ImplementationModelOverride;
272
+ modelInfo: AvailableImplementationModel;
273
+ label: string;
274
+ summary: string;
275
+ details?: readonly string[];
276
+ searchText: string;
277
+ isPlanModel: boolean;
278
+ }
279
+
280
+ function createFreshImplementationFlow(
281
+ ctx: ExtensionContext,
282
+ planThinkingLevel: PlanModeFixedThinkingLevel | undefined,
283
+ implementationDefaults: ImplementationRuntimeSelection | undefined,
284
+ implementFresh: (
285
+ runtime: ImplementationRuntimeSelection,
286
+ signal: AbortSignal,
287
+ ) => void | Promise<void>,
288
+ ) {
289
+ const models = snapshotAvailableModels(ctx);
290
+ const planModel = ctx.model ? { provider: ctx.model.provider, modelId: ctx.model.id } : undefined;
291
+ const planModelSummary = ctx.model
292
+ ? `${safeModelMetadata(ctx.model.id, "unknown model")} [${safeModelMetadata(ctx.model.provider, "unknown provider")}]`
293
+ : undefined;
294
+ const configuredModel = implementationDefaults?.model;
295
+ const configuredAvailableModel = findAvailableImplementationModel(
296
+ models.map((choice) => choice.modelInfo),
297
+ configuredModel,
298
+ );
299
+ let unavailableDefaultActive = configuredModel !== undefined && !configuredAvailableModel;
300
+ let selectedModel = configuredAvailableModel
301
+ ? models.find((choice) => choice.modelInfo === configuredAvailableModel)
302
+ : undefined;
303
+ let selectedModelUsesDefault = selectedModel !== undefined;
304
+ let selectedThinkingLevel = implementationDefaults?.thinkingLevel;
305
+ return {
306
+ settingsScreen: () => ({
307
+ kind: "actions" as const,
308
+ title: "Fresh implementation settings",
309
+ lines: [
310
+ "These choices apply once to the new implementation session.",
311
+ ...(unavailableDefaultActive && configuredModel
312
+ ? [
313
+ `Configured default ${safeModelReference(configuredModel)} is unavailable; using same as plan.`,
314
+ ]
315
+ : []),
316
+ ],
317
+ items: [
318
+ {
319
+ id: "start-fresh",
320
+ label: "Start fresh implementation",
321
+ description: "Create the linked session and begin implementation.",
322
+ action: "start-fresh" as const,
323
+ busyLabel: "Starting fresh implementation session…",
324
+ },
325
+ {
326
+ id: "implementation-model",
327
+ label: "Model",
328
+ description:
329
+ selectedModel?.summary ??
330
+ (planModelSummary ? `${planModelSummary} · same as plan` : "Same as plan"),
331
+ to: "models" as const,
332
+ },
333
+ {
334
+ id: "implementation-thinking",
335
+ label: "Thinking level",
336
+ description:
337
+ selectedThinkingLevel ??
338
+ (planThinkingLevel ? `${planThinkingLevel} · same as plan` : "Same as plan"),
339
+ to: "thinking" as const,
340
+ },
341
+ ],
342
+ }),
343
+ modelScreen: () => ({
344
+ kind: "choice" as const,
345
+ title: "Implementation model",
346
+ items: [
347
+ {
348
+ id: "same-as-plan",
349
+ label: "Same as plan",
350
+ ...(planModelSummary ? { description: planModelSummary } : {}),
351
+ },
352
+ ...models.map((choice) => ({
353
+ id: choice.itemId,
354
+ label: choice.label,
355
+ details: choice.details,
356
+ searchText: choice.searchText,
357
+ })),
358
+ ],
359
+ action: "select-model" as const,
360
+ initialItemId: selectedModel?.itemId ?? "same-as-plan",
361
+ enableSearch: true,
362
+ viewportSize: 10,
363
+ }),
364
+ thinkingScreen: () => ({
365
+ kind: "choice" as const,
366
+ title: "Implementation thinking level",
367
+ items: [
368
+ {
369
+ id: "same-as-plan",
370
+ label: "Same as plan",
371
+ },
372
+ ...IMPLEMENTATION_THINKING_LEVELS.map((level) => ({
373
+ id: level,
374
+ label: `${level === planThinkingLevel ? "✓ " : ""}${level}`,
375
+ description: THINKING_LEVEL_DESCRIPTIONS[level],
376
+ })),
377
+ ],
378
+ action: "select-thinking" as const,
379
+ initialItemId: selectedThinkingLevel ?? "same-as-plan",
380
+ viewportSize: IMPLEMENTATION_THINKING_LEVELS.length + 1,
381
+ }),
382
+ selectModel(itemId: string) {
383
+ const choice = models.find((candidate) => candidate.itemId === itemId);
384
+ selectedModel = itemId === "same-as-plan" || choice?.isPlanModel ? undefined : choice;
385
+ selectedModelUsesDefault = false;
386
+ unavailableDefaultActive = false;
387
+ },
388
+ selectThinking(itemId: string) {
389
+ selectedThinkingLevel = IMPLEMENTATION_THINKING_LEVELS.find((level) => level === itemId);
390
+ },
391
+ start(signal: AbortSignal) {
392
+ if (
393
+ selectedModelUsesDefault &&
394
+ selectedModel &&
395
+ !findAvailableImplementationModel(
396
+ snapshotAvailableImplementationModels(ctx),
397
+ selectedModel.model,
398
+ )
399
+ ) {
400
+ selectedModel = undefined;
401
+ selectedModelUsesDefault = false;
402
+ unavailableDefaultActive = true;
403
+ ctx.ui.notify(
404
+ `Configured default ${safeModelReference(configuredModel)} is unavailable; using same as plan.`,
405
+ "warning",
406
+ );
407
+ }
408
+ const model = selectedModel?.model ?? planModel;
409
+ const thinkingLevel = selectedThinkingLevel ?? planThinkingLevel;
410
+ return implementFresh(
411
+ {
412
+ ...(model ? { model: { ...model } } : {}),
413
+ ...(thinkingLevel ? { thinkingLevel } : {}),
414
+ },
415
+ signal,
416
+ );
417
+ },
418
+ };
419
+ }
420
+
421
+ function snapshotAvailableModels(ctx: ExtensionContext): ModelChoice[] {
422
+ return snapshotAvailableImplementationModels(ctx)
423
+ .map((model, index) => {
424
+ const provider = safeModelMetadata(model.provider, "unknown provider");
425
+ const modelId = safeModelMetadata(model.id, "unknown model");
426
+ const name = safeModelMetadata(model.name, "");
427
+ const isPlanModel = ctx.model?.provider === model.provider && ctx.model.id === model.id;
428
+ const summary = `${modelId} [${provider}]`;
429
+ return {
430
+ itemId: `model-${index}`,
431
+ model: { provider: model.provider, modelId: model.id },
432
+ modelInfo: model,
433
+ label: `${isPlanModel ? "✓ " : ""}${summary}${isPlanModel ? " · default" : ""}`,
434
+ summary,
435
+ ...(name ? { details: [`Model Name: ${name}`] } : {}),
436
+ searchText: [provider, modelId, name].filter(Boolean).join(" "),
437
+ isPlanModel,
438
+ };
439
+ })
440
+ .sort((left, right) => Number(right.isPlanModel) - Number(left.isPlanModel));
441
+ }
442
+
443
+ function safeModelReference(model: ImplementationModelOverride | undefined) {
444
+ if (!model) return "configured model";
445
+ return `${safeModelMetadata(model.modelId, "unknown model")} [${safeModelMetadata(model.provider, "unknown provider")}]`;
446
+ }
447
+
448
+ function safeModelMetadata(value: unknown, fallback: string) {
449
+ if (typeof value !== "string") return fallback;
450
+ const safe = sanitizeTerminalText(value).trim() || fallback;
451
+ return [...safe].slice(0, 512).join("");
452
+ }