@narumitw/pi-plan-mode 0.58.0 → 0.58.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.
Files changed (39) hide show
  1. package/dist/chunks/{chunk-QHHHBTDO.js → chunk-HF7ZEYIC.js} +29 -111
  2. package/dist/chunks/chunk-HF7ZEYIC.js.map +7 -0
  3. package/dist/chunks/{interactive-ui-UNJGC2RQ.js → interactive-ui-UK7IGPOE.js} +13 -41
  4. package/dist/chunks/interactive-ui-UK7IGPOE.js.map +7 -0
  5. package/dist/index.ts +25 -82
  6. package/dist/index.ts.map +2 -2
  7. package/package.json +50 -51
  8. package/src/active-implementation-menu.ts +57 -60
  9. package/src/command.ts +22 -22
  10. package/src/completion-tool.ts +53 -56
  11. package/src/extension-runtime.ts +5 -5
  12. package/src/finalization-request.ts +40 -44
  13. package/src/fresh-handoff-coordinator.ts +38 -41
  14. package/src/fresh-implementation.ts +293 -310
  15. package/src/implementation-models.ts +25 -30
  16. package/src/implementation-retention.ts +109 -123
  17. package/src/message-transform.ts +218 -252
  18. package/src/mode-contract.ts +46 -49
  19. package/src/plan-action-controller.ts +134 -155
  20. package/src/plan-action-menus.ts +390 -408
  21. package/src/plan-export-controller.ts +23 -28
  22. package/src/plan-export-screen.ts +13 -13
  23. package/src/plan-export.ts +85 -102
  24. package/src/plan-launch-menu.ts +119 -119
  25. package/src/plan-mode.ts +2017 -2098
  26. package/src/presentation.ts +84 -108
  27. package/src/prompt.ts +1 -1
  28. package/src/question-tool.ts +239 -267
  29. package/src/required-tools.ts +15 -19
  30. package/src/saved-plan-menu.ts +82 -84
  31. package/src/saved-plan-preflight.ts +29 -33
  32. package/src/settings-menu.ts +470 -513
  33. package/src/settings.ts +393 -452
  34. package/src/state.ts +167 -198
  35. package/src/tool-policy.ts +634 -699
  36. package/src/tool-selection.ts +39 -44
  37. package/src/workflow-mutex.ts +66 -66
  38. package/dist/chunks/chunk-QHHHBTDO.js.map +0 -7
  39. package/dist/chunks/interactive-ui-UNJGC2RQ.js.map +0 -7
@@ -13,81 +13,78 @@ Earlier Plan-mode restrictions no longer apply, but retain the planning conversa
13
13
  The visible tool schemas are session capabilities; use only tools appropriate for the current request.`;
14
14
 
15
15
  interface ContractMessage {
16
- role?: string;
17
- customType?: string;
18
- content?: unknown;
19
- details?: unknown;
16
+ role?: string;
17
+ customType?: string;
18
+ content?: unknown;
19
+ details?: unknown;
20
20
  }
21
21
 
22
22
  export function modeContractContent(mode: PlanModeContract) {
23
- return mode === "plan" ? `${PLAN_CONTRACT_MARKER}\n${buildPlanModePrompt()}` : NORMAL_CONTRACT;
23
+ return mode === "plan" ? `${PLAN_CONTRACT_MARKER}\n${buildPlanModePrompt()}` : NORMAL_CONTRACT;
24
24
  }
25
25
 
26
26
  export function createModeContractMessage(mode: PlanModeContract, timestamp = Date.now()) {
27
- return {
28
- role: "custom" as const,
29
- customType: MODE_CONTRACT_MESSAGE_TYPE,
30
- content: modeContractContent(mode),
31
- display: false,
32
- details: { version: MODE_CONTRACT_VERSION, mode },
33
- timestamp,
34
- };
27
+ return {
28
+ role: "custom" as const,
29
+ customType: MODE_CONTRACT_MESSAGE_TYPE,
30
+ content: modeContractContent(mode),
31
+ display: false,
32
+ details: { version: MODE_CONTRACT_VERSION, mode },
33
+ timestamp,
34
+ };
35
35
  }
36
36
 
37
37
  export function modeContractFromMessage(message: unknown): PlanModeContract | undefined {
38
- const candidate = unwrapMessage(message);
39
- if (candidate.customType !== MODE_CONTRACT_MESSAGE_TYPE) return undefined;
40
- if (candidate.content === modeContractContent("plan")) return "plan";
41
- if (candidate.content === modeContractContent("normal")) return "normal";
42
- return undefined;
38
+ const candidate = unwrapMessage(message);
39
+ if (candidate.customType !== MODE_CONTRACT_MESSAGE_TYPE) return undefined;
40
+ if (candidate.content === modeContractContent("plan")) return "plan";
41
+ if (candidate.content === modeContractContent("normal")) return "normal";
42
+ return undefined;
43
43
  }
44
44
 
45
45
  export function hasModeContractArtifact(messages: readonly unknown[]) {
46
- return messages.some(
47
- (message) => unwrapMessage(message).customType === MODE_CONTRACT_MESSAGE_TYPE,
48
- );
46
+ return messages.some((message) => unwrapMessage(message).customType === MODE_CONTRACT_MESSAGE_TYPE);
49
47
  }
50
48
 
51
49
  export function latestModeContract(messages: readonly unknown[]) {
52
- for (let index = messages.length - 1; index >= 0; index -= 1) {
53
- const mode = modeContractFromMessage(messages[index]);
54
- if (mode) return { index, mode };
55
- }
56
- return undefined;
50
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
51
+ const mode = modeContractFromMessage(messages[index]);
52
+ if (mode) return { index, mode };
53
+ }
54
+ return undefined;
57
55
  }
58
56
 
59
57
  export function reconcileModeContract(messages: unknown[], expected: PlanModeContract) {
60
- const latest = latestModeContract(messages);
61
- if (latest?.mode === expected) return messages;
58
+ const latest = latestModeContract(messages);
59
+ if (latest?.mode === expected) return messages;
62
60
 
63
- const latestContractIndex = findLatestContractArtifactIndex(messages);
64
- const insertionIndex =
65
- latestContractIndex >= 0 ? latestContractIndex + 1 : leadingSummaryBoundary(messages);
66
- return [
67
- ...messages.slice(0, insertionIndex),
68
- createModeContractMessage(expected, 0),
69
- ...messages.slice(insertionIndex),
70
- ];
61
+ const latestContractIndex = findLatestContractArtifactIndex(messages);
62
+ const insertionIndex = latestContractIndex >= 0 ? latestContractIndex + 1 : leadingSummaryBoundary(messages);
63
+ return [
64
+ ...messages.slice(0, insertionIndex),
65
+ createModeContractMessage(expected, 0),
66
+ ...messages.slice(insertionIndex),
67
+ ];
71
68
  }
72
69
 
73
70
  function findLatestContractArtifactIndex(messages: readonly unknown[]) {
74
- for (let index = messages.length - 1; index >= 0; index -= 1) {
75
- if (unwrapMessage(messages[index]).customType === MODE_CONTRACT_MESSAGE_TYPE) return index;
76
- }
77
- return -1;
71
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
72
+ if (unwrapMessage(messages[index]).customType === MODE_CONTRACT_MESSAGE_TYPE) return index;
73
+ }
74
+ return -1;
78
75
  }
79
76
 
80
77
  function leadingSummaryBoundary(messages: readonly unknown[]) {
81
- let index = 0;
82
- while (index < messages.length) {
83
- const role = unwrapMessage(messages[index]).role;
84
- if (role !== "compactionSummary" && role !== "branchSummary") break;
85
- index += 1;
86
- }
87
- return index;
78
+ let index = unwrapMessage(messages[0]).role === "system" ? 1 : 0;
79
+ while (index < messages.length) {
80
+ const role = unwrapMessage(messages[index]).role;
81
+ if (role !== "compactionSummary" && role !== "branchSummary") break;
82
+ index += 1;
83
+ }
84
+ return index;
88
85
  }
89
86
 
90
87
  function unwrapMessage(message: unknown): ContractMessage {
91
- const entry = message as { message?: unknown } | undefined;
92
- return (entry?.message ?? message ?? {}) as ContractMessage;
88
+ const entry = message as { message?: unknown } | undefined;
89
+ return (entry?.message ?? message ?? {}) as ContractMessage;
93
90
  }
@@ -1,174 +1,153 @@
1
1
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
- import {
3
- findAvailableImplementationModel,
4
- snapshotAvailableImplementationModels,
5
- } from "./implementation-models.js";
2
+ import { findAvailableImplementationModel, snapshotAvailableImplementationModels } from "./implementation-models.js";
6
3
  import type { PlanExportDestination } from "./plan-export.js";
7
4
  import {
8
- configuredImplementationModel,
9
- configuredImplementationThinkingLevel,
10
- type PlanModeFixedThinkingLevel,
11
- type PlanModeSettings,
5
+ configuredImplementationModel,
6
+ configuredImplementationThinkingLevel,
7
+ type PlanModeFixedThinkingLevel,
8
+ type PlanModeSettings,
12
9
  } from "./settings.js";
13
10
  import type { ImplementationRuntimeSelection, PlanModeState } from "./state.js";
14
11
 
15
12
  type InteractiveUi = typeof import("./interactive-ui.js");
16
13
 
17
14
  interface MenuLifecycle {
18
- signal: AbortSignal;
19
- isCurrent(): boolean;
15
+ signal: AbortSignal;
16
+ isCurrent(): boolean;
20
17
  }
21
18
 
22
19
  export type FreshImplementationTiming = "immediate" | "after-settled";
23
20
 
24
21
  interface PlanActionControllerOptions {
25
- loadInteractiveUi(): Promise<InteractiveUi>;
26
- getState(): PlanModeState;
27
- captureLifecycle(): MenuLifecycle;
28
- statusText(): string;
29
- getThinkingLevel(): PlanModeFixedThinkingLevel | undefined;
30
- getSettings(): PlanModeSettings;
31
- implementationOutcome(): string;
32
- getExportDestination(ctx: ExtensionContext): PlanExportDestination;
33
- show(ctx: ExtensionContext): void;
34
- finalize(ctx: ExtensionContext): void;
35
- implementHere(ctx: ExtensionContext): void | Promise<void>;
36
- implementFresh(
37
- ctx: ExtensionContext,
38
- isCurrent: () => boolean,
39
- runtime: ImplementationRuntimeSelection | undefined,
40
- timing: FreshImplementationTiming,
41
- ): void | Promise<void>;
42
- exportPlan(
43
- ctx: ExtensionContext,
44
- path: string,
45
- signal: AbortSignal,
46
- isCurrent: () => boolean,
47
- ): Promise<boolean>;
48
- settings(ctx: ExtensionContext, signal: AbortSignal, isCurrent: () => boolean): Promise<boolean>;
49
- save(ctx: ExtensionContext): void;
50
- stay(ctx: ExtensionContext): void;
51
- exitReady(ctx: ExtensionContext): void;
52
- clearSaved(ctx: ExtensionContext): void;
22
+ loadInteractiveUi(): Promise<InteractiveUi>;
23
+ getState(): PlanModeState;
24
+ captureLifecycle(): MenuLifecycle;
25
+ statusText(): string;
26
+ getThinkingLevel(): PlanModeFixedThinkingLevel | undefined;
27
+ getSettings(): PlanModeSettings;
28
+ implementationOutcome(): string;
29
+ getExportDestination(ctx: ExtensionContext): PlanExportDestination;
30
+ show(ctx: ExtensionContext): void;
31
+ finalize(ctx: ExtensionContext): void;
32
+ implementHere(ctx: ExtensionContext): void | Promise<void>;
33
+ implementFresh(
34
+ ctx: ExtensionContext,
35
+ isCurrent: () => boolean,
36
+ runtime: ImplementationRuntimeSelection | undefined,
37
+ timing: FreshImplementationTiming,
38
+ ): void | Promise<void>;
39
+ exportPlan(ctx: ExtensionContext, path: string, signal: AbortSignal, isCurrent: () => boolean): Promise<boolean>;
40
+ settings(ctx: ExtensionContext, signal: AbortSignal, isCurrent: () => boolean): Promise<boolean>;
41
+ save(ctx: ExtensionContext): void;
42
+ stay(ctx: ExtensionContext): void;
43
+ exitReady(ctx: ExtensionContext): void;
44
+ clearSaved(ctx: ExtensionContext): void;
53
45
  }
54
46
 
55
47
  export function createPlanActionController(options: PlanActionControllerOptions) {
56
- const configuredDefaults = (): ImplementationRuntimeSelection => {
57
- const settings = options.getSettings();
58
- const model = configuredImplementationModel(settings);
59
- const thinkingLevel = configuredImplementationThinkingLevel(settings);
60
- return {
61
- ...(model ? { model: { ...model } } : {}),
62
- ...(thinkingLevel ? { thinkingLevel } : {}),
63
- };
64
- };
65
- const effectiveDefaults = (ctx: ExtensionContext): ImplementationRuntimeSelection => {
66
- const configured = configuredDefaults();
67
- const availableModel = findAvailableImplementationModel(
68
- snapshotAvailableImplementationModels(ctx),
69
- configured.model,
70
- );
71
- if (configured.model && !availableModel) {
72
- ctx.ui.notify(
73
- "The configured fresh implementation model is unavailable; using the planning model.",
74
- "warning",
75
- );
76
- }
77
- const planModel = ctx.model
78
- ? { provider: ctx.model.provider, modelId: ctx.model.id }
79
- : undefined;
80
- const model = availableModel
81
- ? { provider: availableModel.provider, modelId: availableModel.id }
82
- : planModel;
83
- const thinkingLevel = configured.thinkingLevel ?? options.getThinkingLevel();
84
- return {
85
- ...(model ? { model } : {}),
86
- ...(thinkingLevel ? { thinkingLevel } : {}),
87
- };
88
- };
89
- const freshAction = (
90
- ctx: ExtensionContext,
91
- lifecycle: MenuLifecycle,
92
- signal: AbortSignal,
93
- runtime: ImplementationRuntimeSelection | undefined,
94
- timing: FreshImplementationTiming,
95
- ) => {
96
- if (signal.aborted) return;
97
- const isCurrent =
98
- timing === "after-settled"
99
- ? lifecycle.isCurrent
100
- : () => lifecycle.isCurrent() && !signal.aborted;
101
- return options.implementFresh(ctx, isCurrent, runtime, timing);
102
- };
48
+ const configuredDefaults = (): ImplementationRuntimeSelection => {
49
+ const settings = options.getSettings();
50
+ const model = configuredImplementationModel(settings);
51
+ const thinkingLevel = configuredImplementationThinkingLevel(settings);
52
+ return {
53
+ ...(model ? { model: { ...model } } : {}),
54
+ ...(thinkingLevel ? { thinkingLevel } : {}),
55
+ };
56
+ };
57
+ const effectiveDefaults = (ctx: ExtensionContext): ImplementationRuntimeSelection => {
58
+ const configured = configuredDefaults();
59
+ const availableModel = findAvailableImplementationModel(
60
+ snapshotAvailableImplementationModels(ctx),
61
+ configured.model,
62
+ );
63
+ if (configured.model && !availableModel) {
64
+ ctx.ui.notify("The configured fresh implementation model is unavailable; using the planning model.", "warning");
65
+ }
66
+ const planModel = ctx.model ? { provider: ctx.model.provider, modelId: ctx.model.id } : undefined;
67
+ const model = availableModel ? { provider: availableModel.provider, modelId: availableModel.id } : planModel;
68
+ const thinkingLevel = configured.thinkingLevel ?? options.getThinkingLevel();
69
+ return {
70
+ ...(model ? { model } : {}),
71
+ ...(thinkingLevel ? { thinkingLevel } : {}),
72
+ };
73
+ };
74
+ const freshAction = (
75
+ ctx: ExtensionContext,
76
+ lifecycle: MenuLifecycle,
77
+ signal: AbortSignal,
78
+ runtime: ImplementationRuntimeSelection | undefined,
79
+ timing: FreshImplementationTiming,
80
+ ) => {
81
+ if (signal.aborted) return;
82
+ const isCurrent = timing === "after-settled" ? lifecycle.isCurrent : () => lifecycle.isCurrent() && !signal.aborted;
83
+ return options.implementFresh(ctx, isCurrent, runtime, timing);
84
+ };
103
85
 
104
- return {
105
- async showSaved(ctx: ExtensionContext) {
106
- const lifecycle = options.captureLifecycle();
107
- if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
108
- const ui = await options.loadInteractiveUi();
109
- if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
110
- await ui.showSavedPlanMenu(ctx, {
111
- statusText: options.statusText(),
112
- implementationOutcome: options.implementationOutcome,
113
- getExportDestination: () => options.getExportDestination(ctx),
114
- signal: lifecycle.signal,
115
- isCurrent: lifecycle.isCurrent,
116
- show: () => options.show(ctx),
117
- implementHere: () => options.implementHere(ctx),
118
- implementFresh: (signal) =>
119
- freshAction(ctx, lifecycle, signal, effectiveDefaults(ctx), "immediate"),
120
- exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
121
- settings: (signal) => options.settings(ctx, signal, lifecycle.isCurrent),
122
- clear: () => options.clearSaved(ctx),
123
- });
124
- },
125
- async showCurrent(ctx: ExtensionContext) {
126
- if (!ctx.hasUI) {
127
- ctx.ui.notify(options.statusText(), "info");
128
- return;
129
- }
130
- const lifecycle = options.captureLifecycle();
131
- if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
132
- const ui = await options.loadInteractiveUi();
133
- if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
134
- await ui.showPlanModeMenu(ctx, {
135
- statusText: options.statusText(),
136
- planThinkingLevel: options.getThinkingLevel(),
137
- implementationDefaults: configuredDefaults(),
138
- hasReadyPlan: options.getState().latestPlan !== undefined,
139
- implementationOutcome: options.implementationOutcome,
140
- getExportDestination: () => options.getExportDestination(ctx),
141
- ...lifecycle,
142
- show: () => options.show(ctx),
143
- finalize: () => options.finalize(ctx),
144
- implementHere: () => options.implementHere(ctx),
145
- implementFresh: (runtime, signal) =>
146
- freshAction(ctx, lifecycle, signal, runtime, "immediate"),
147
- exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
148
- save: () => options.save(ctx),
149
- stay: () => options.stay(ctx),
150
- exit: () => options.exitReady(ctx),
151
- });
152
- },
153
- async showReady(ctx: ExtensionContext) {
154
- const lifecycle = options.captureLifecycle();
155
- if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
156
- const ui = await options.loadInteractiveUi();
157
- if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
158
- await ui.showReadyPlanMenu(ctx, {
159
- ...lifecycle,
160
- planThinkingLevel: options.getThinkingLevel(),
161
- implementationDefaults: configuredDefaults(),
162
- implementationOutcome: options.implementationOutcome,
163
- getExportDestination: () => options.getExportDestination(ctx),
164
- implementHere: () => options.implementHere(ctx),
165
- implementFresh: (runtime, signal) =>
166
- freshAction(ctx, lifecycle, signal, runtime, "after-settled"),
167
- exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
168
- save: () => options.save(ctx),
169
- stay: () => undefined,
170
- exit: () => options.exitReady(ctx),
171
- });
172
- },
173
- };
86
+ return {
87
+ async showSaved(ctx: ExtensionContext) {
88
+ const lifecycle = options.captureLifecycle();
89
+ if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
90
+ const ui = await options.loadInteractiveUi();
91
+ if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
92
+ await ui.showSavedPlanMenu(ctx, {
93
+ statusText: options.statusText(),
94
+ implementationOutcome: options.implementationOutcome,
95
+ getExportDestination: () => options.getExportDestination(ctx),
96
+ signal: lifecycle.signal,
97
+ isCurrent: lifecycle.isCurrent,
98
+ show: () => options.show(ctx),
99
+ implementHere: () => options.implementHere(ctx),
100
+ implementFresh: (signal) => freshAction(ctx, lifecycle, signal, effectiveDefaults(ctx), "immediate"),
101
+ exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
102
+ settings: (signal) => options.settings(ctx, signal, lifecycle.isCurrent),
103
+ clear: () => options.clearSaved(ctx),
104
+ });
105
+ },
106
+ async showCurrent(ctx: ExtensionContext) {
107
+ if (!ctx.hasUI) {
108
+ ctx.ui.notify(options.statusText(), "info");
109
+ return;
110
+ }
111
+ const lifecycle = options.captureLifecycle();
112
+ if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
113
+ const ui = await options.loadInteractiveUi();
114
+ if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
115
+ await ui.showPlanModeMenu(ctx, {
116
+ statusText: options.statusText(),
117
+ planThinkingLevel: options.getThinkingLevel(),
118
+ implementationDefaults: configuredDefaults(),
119
+ hasReadyPlan: options.getState().latestPlan !== undefined,
120
+ implementationOutcome: options.implementationOutcome,
121
+ getExportDestination: () => options.getExportDestination(ctx),
122
+ ...lifecycle,
123
+ show: () => options.show(ctx),
124
+ finalize: () => options.finalize(ctx),
125
+ implementHere: () => options.implementHere(ctx),
126
+ implementFresh: (runtime, signal) => freshAction(ctx, lifecycle, signal, runtime, "immediate"),
127
+ exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
128
+ save: () => options.save(ctx),
129
+ stay: () => options.stay(ctx),
130
+ exit: () => options.exitReady(ctx),
131
+ });
132
+ },
133
+ async showReady(ctx: ExtensionContext) {
134
+ const lifecycle = options.captureLifecycle();
135
+ if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
136
+ const ui = await options.loadInteractiveUi();
137
+ if (!lifecycle.isCurrent() || lifecycle.signal.aborted) return;
138
+ await ui.showReadyPlanMenu(ctx, {
139
+ ...lifecycle,
140
+ planThinkingLevel: options.getThinkingLevel(),
141
+ implementationDefaults: configuredDefaults(),
142
+ implementationOutcome: options.implementationOutcome,
143
+ getExportDestination: () => options.getExportDestination(ctx),
144
+ implementHere: () => options.implementHere(ctx),
145
+ implementFresh: (runtime, signal) => freshAction(ctx, lifecycle, signal, runtime, "after-settled"),
146
+ exportPlan: (path, signal) => options.exportPlan(ctx, path, signal, lifecycle.isCurrent),
147
+ save: () => options.save(ctx),
148
+ stay: () => undefined,
149
+ exit: () => options.exitReady(ctx),
150
+ });
151
+ },
152
+ };
174
153
  }