@narumitw/pi-plan-mode 0.57.1 → 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 (41) hide show
  1. package/README.md +68 -4
  2. package/dist/chunks/{chunk-57OBPS7P.js → chunk-HF7ZEYIC.js} +169 -164
  3. package/dist/chunks/chunk-HF7ZEYIC.js.map +7 -0
  4. package/dist/chunks/{interactive-ui-N2ZKBVNE.js → interactive-ui-UK7IGPOE.js} +312 -32
  5. package/dist/chunks/interactive-ui-UK7IGPOE.js.map +7 -0
  6. package/dist/index.ts +670 -212
  7. package/dist/index.ts.map +4 -4
  8. package/docs/settings.md +26 -2
  9. package/package.json +50 -51
  10. package/src/active-implementation-menu.ts +57 -60
  11. package/src/command.ts +22 -21
  12. package/src/completion-tool.ts +53 -56
  13. package/src/extension-runtime.ts +5 -5
  14. package/src/finalization-request.ts +40 -44
  15. package/src/fresh-handoff-coordinator.ts +52 -0
  16. package/src/fresh-implementation.ts +312 -205
  17. package/src/implementation-models.ts +43 -0
  18. package/src/implementation-retention.ts +109 -123
  19. package/src/message-transform.ts +218 -252
  20. package/src/mode-contract.ts +46 -49
  21. package/src/plan-action-controller.ts +139 -89
  22. package/src/plan-action-menus.ts +414 -177
  23. package/src/plan-export-controller.ts +23 -28
  24. package/src/plan-export-screen.ts +13 -13
  25. package/src/plan-export.ts +85 -102
  26. package/src/plan-launch-menu.ts +119 -119
  27. package/src/plan-mode.ts +2027 -1675
  28. package/src/presentation.ts +84 -108
  29. package/src/prompt.ts +1 -1
  30. package/src/question-tool.ts +239 -267
  31. package/src/required-tools.ts +15 -19
  32. package/src/saved-plan-menu.ts +82 -84
  33. package/src/saved-plan-preflight.ts +29 -33
  34. package/src/settings-menu.ts +489 -377
  35. package/src/settings.ts +403 -382
  36. package/src/state.ts +177 -146
  37. package/src/tool-policy.ts +634 -699
  38. package/src/tool-selection.ts +39 -44
  39. package/src/workflow-mutex.ts +66 -66
  40. package/dist/chunks/chunk-57OBPS7P.js.map +0 -7
  41. package/dist/chunks/interactive-ui-N2ZKBVNE.js.map +0 -7
package/src/state.ts CHANGED
@@ -1,197 +1,228 @@
1
1
  import {
2
- normalizePlanModeCompletion,
3
- PLAN_MODE_COMPLETE_TOOL_NAME,
4
- planFromCompletionDetails,
2
+ normalizePlanModeCompletion,
3
+ PLAN_MODE_COMPLETE_TOOL_NAME,
4
+ planFromCompletionDetails,
5
5
  } from "./completion-tool.js";
6
+ import { type ImplementationModelOverride, isPendingImplementationModelIdentifier } from "./implementation-models.js";
6
7
  import {
7
- IMPLEMENTATION_PLAN_RETENTIONS,
8
- type ImplementationPlanRetention,
9
- PLAN_MODE_THINKING_LEVELS,
10
- type PlanModeFixedThinkingLevel,
8
+ IMPLEMENTATION_PLAN_RETENTIONS,
9
+ type ImplementationPlanRetention,
10
+ PLAN_MODE_THINKING_LEVELS,
11
+ type PlanModeFixedThinkingLevel,
11
12
  } from "./settings.js";
12
13
 
14
+ export type { ImplementationModelOverride } from "./implementation-models.js";
15
+ export {
16
+ isPendingImplementationModelIdentifier,
17
+ MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH,
18
+ } from "./implementation-models.js";
19
+
13
20
  export type PlanCompletionSource = typeof PLAN_MODE_COMPLETE_TOOL_NAME | "legacy_proposed_plan";
14
21
 
15
22
  export interface ActiveImplementationPlan {
16
- id: string;
17
- plan: string;
18
- source: PlanCompletionSource;
19
- startedAt: number;
20
- retention?: ImplementationPlanRetention;
23
+ id: string;
24
+ plan: string;
25
+ source: PlanCompletionSource;
26
+ startedAt: number;
27
+ retention?: ImplementationPlanRetention;
21
28
  }
22
29
 
23
30
  export interface SavedPlan {
24
- plan: string;
25
- source: PlanCompletionSource;
31
+ plan: string;
32
+ source: PlanCompletionSource;
33
+ }
34
+
35
+ export interface ImplementationRuntimeSelection {
36
+ model?: ImplementationModelOverride;
37
+ thinkingLevel?: PlanModeFixedThinkingLevel;
38
+ }
39
+
40
+ export interface PendingImplementationRuntime extends ImplementationRuntimeSelection {
41
+ version: 1;
26
42
  }
27
43
 
28
44
  export interface PlanModeWorkflowToolPolicy {
29
- kind: "automatic" | "explicit";
30
- desiredNames?: string[];
31
- allowedNames: string[];
32
- resolved: boolean;
45
+ kind: "automatic" | "explicit";
46
+ desiredNames?: string[];
47
+ allowedNames: string[];
48
+ resolved: boolean;
33
49
  }
34
50
 
35
51
  export interface PlanModeState {
36
- enabled: boolean;
37
- latestPlan?: string;
38
- latestPlanSource?: PlanCompletionSource;
39
- awaitingAction: boolean;
40
- savedPlan?: SavedPlan;
41
- activeImplementation?: ActiveImplementationPlan;
42
- selectedToolNames?: string[];
43
- selectedToolKeys?: string[];
44
- workflowToolPolicy?: PlanModeWorkflowToolPolicy;
45
- previousThinkingLevel?: PlanModeFixedThinkingLevel;
46
- appliedThinkingLevel?: PlanModeFixedThinkingLevel;
47
- manualThinkingLevel?: PlanModeFixedThinkingLevel;
52
+ enabled: boolean;
53
+ latestPlan?: string;
54
+ latestPlanSource?: PlanCompletionSource;
55
+ awaitingAction: boolean;
56
+ savedPlan?: SavedPlan;
57
+ activeImplementation?: ActiveImplementationPlan;
58
+ pendingImplementationRuntime?: PendingImplementationRuntime;
59
+ selectedToolNames?: string[];
60
+ selectedToolKeys?: string[];
61
+ workflowToolPolicy?: PlanModeWorkflowToolPolicy;
62
+ previousThinkingLevel?: PlanModeFixedThinkingLevel;
63
+ appliedThinkingLevel?: PlanModeFixedThinkingLevel;
64
+ manualThinkingLevel?: PlanModeFixedThinkingLevel;
48
65
  }
49
66
 
50
67
  type SessionEntry = {
51
- type?: string;
52
- customType?: string;
53
- data?: unknown;
54
- message?: {
55
- role?: string;
56
- toolName?: string;
57
- details?: unknown;
58
- };
68
+ type?: string;
69
+ customType?: string;
70
+ data?: unknown;
71
+ message?: {
72
+ role?: string;
73
+ toolName?: string;
74
+ details?: unknown;
75
+ };
59
76
  };
60
77
 
61
78
  export function restorePlanModeState(entries: unknown[], stateEntryType: string): PlanModeState {
62
- const branch = entries as SessionEntry[];
63
- let stateEntryIndex = -1;
64
- for (let index = branch.length - 1; index >= 0; index -= 1) {
65
- const candidate = branch[index];
66
- if (candidate?.type === "custom" && candidate.customType === stateEntryType) {
67
- stateEntryIndex = index;
68
- break;
69
- }
70
- }
71
- const entry = branch[stateEntryIndex];
72
- if (!isRecord(entry?.data)) return { enabled: false, awaitingAction: false };
73
-
74
- const enabled = entry.data.enabled === true;
75
- const persistedSource = enabled ? planCompletionSource(entry.data.latestPlanSource) : undefined;
76
- const persistedPlan = enabled ? normalizePersistedPlan(entry.data.latestPlan) : undefined;
77
- const recoveredPlan =
78
- enabled && !persistedPlan ? latestCompletionPlan(branch.slice(stateEntryIndex + 1)) : undefined;
79
- const latestPlan = persistedPlan ?? recoveredPlan;
80
- const activeImplementation = enabled
81
- ? undefined
82
- : normalizeActiveImplementation(entry.data.activeImplementation);
83
- const savedPlan =
84
- enabled || activeImplementation ? undefined : normalizeSavedPlan(entry.data.savedPlan);
85
- return {
86
- enabled,
87
- latestPlan,
88
- latestPlanSource: enabled
89
- ? ((persistedPlan ? persistedSource : undefined) ??
90
- (recoveredPlan ? PLAN_MODE_COMPLETE_TOOL_NAME : undefined))
91
- : undefined,
92
- awaitingAction: enabled && latestPlan !== undefined,
93
- savedPlan,
94
- activeImplementation,
95
- selectedToolNames: stringArray(entry.data.selectedToolNames),
96
- selectedToolKeys: stringArray(entry.data.selectedToolKeys),
97
- workflowToolPolicy: enabled
98
- ? normalizeWorkflowToolPolicy(entry.data.workflowToolPolicy)
99
- : undefined,
100
- previousThinkingLevel: enabled
101
- ? fixedThinkingLevel(entry.data.previousThinkingLevel)
102
- : undefined,
103
- appliedThinkingLevel: enabled ? fixedThinkingLevel(entry.data.appliedThinkingLevel) : undefined,
104
- manualThinkingLevel: enabled ? fixedThinkingLevel(entry.data.manualThinkingLevel) : undefined,
105
- };
79
+ const branch = entries as SessionEntry[];
80
+ let stateEntryIndex = -1;
81
+ for (let index = branch.length - 1; index >= 0; index -= 1) {
82
+ const candidate = branch[index];
83
+ if (candidate?.type === "custom" && candidate.customType === stateEntryType) {
84
+ stateEntryIndex = index;
85
+ break;
86
+ }
87
+ }
88
+ const entry = branch[stateEntryIndex];
89
+ if (!isRecord(entry?.data)) return { enabled: false, awaitingAction: false };
90
+
91
+ const enabled = entry.data.enabled === true;
92
+ const persistedSource = enabled ? planCompletionSource(entry.data.latestPlanSource) : undefined;
93
+ const persistedPlan = enabled ? normalizePersistedPlan(entry.data.latestPlan) : undefined;
94
+ const recoveredPlan = enabled && !persistedPlan ? latestCompletionPlan(branch.slice(stateEntryIndex + 1)) : undefined;
95
+ const latestPlan = persistedPlan ?? recoveredPlan;
96
+ const activeImplementation = enabled ? undefined : normalizeActiveImplementation(entry.data.activeImplementation);
97
+ const savedPlan = enabled || activeImplementation ? undefined : normalizeSavedPlan(entry.data.savedPlan);
98
+ const pendingImplementationRuntime = enabled
99
+ ? undefined
100
+ : normalizePendingImplementationRuntime(entry.data.pendingImplementationRuntime);
101
+ return {
102
+ enabled,
103
+ latestPlan,
104
+ latestPlanSource: enabled
105
+ ? ((persistedPlan ? persistedSource : undefined) ?? (recoveredPlan ? PLAN_MODE_COMPLETE_TOOL_NAME : undefined))
106
+ : undefined,
107
+ awaitingAction: enabled && latestPlan !== undefined,
108
+ savedPlan,
109
+ activeImplementation,
110
+ pendingImplementationRuntime,
111
+ selectedToolNames: stringArray(entry.data.selectedToolNames),
112
+ selectedToolKeys: stringArray(entry.data.selectedToolKeys),
113
+ workflowToolPolicy: enabled ? normalizeWorkflowToolPolicy(entry.data.workflowToolPolicy) : undefined,
114
+ previousThinkingLevel: enabled ? fixedThinkingLevel(entry.data.previousThinkingLevel) : undefined,
115
+ appliedThinkingLevel: enabled ? fixedThinkingLevel(entry.data.appliedThinkingLevel) : undefined,
116
+ manualThinkingLevel: enabled ? fixedThinkingLevel(entry.data.manualThinkingLevel) : undefined,
117
+ };
106
118
  }
107
119
 
108
120
  function normalizeWorkflowToolPolicy(value: unknown): PlanModeWorkflowToolPolicy | undefined {
109
- if (value === undefined) return undefined;
110
- const denied: PlanModeWorkflowToolPolicy = {
111
- kind: "automatic",
112
- allowedNames: [],
113
- resolved: true,
114
- };
115
- if (!isRecord(value)) return denied;
116
- const kind = value.kind === "automatic" || value.kind === "explicit" ? value.kind : undefined;
117
- const allowedNames = stringArray(value.allowedNames);
118
- if (!kind || !allowedNames || typeof value.resolved !== "boolean") return denied;
119
- if (kind === "automatic") {
120
- return { kind, allowedNames, resolved: value.resolved };
121
- }
122
- const desiredNames = stringArray(value.desiredNames);
123
- if (!desiredNames) return denied;
124
- return { kind, desiredNames, allowedNames, resolved: value.resolved };
121
+ if (value === undefined) return undefined;
122
+ const denied: PlanModeWorkflowToolPolicy = {
123
+ kind: "automatic",
124
+ allowedNames: [],
125
+ resolved: true,
126
+ };
127
+ if (!isRecord(value)) return denied;
128
+ const kind = value.kind === "automatic" || value.kind === "explicit" ? value.kind : undefined;
129
+ const allowedNames = stringArray(value.allowedNames);
130
+ if (!kind || !allowedNames || typeof value.resolved !== "boolean") return denied;
131
+ if (kind === "automatic") {
132
+ return { kind, allowedNames, resolved: value.resolved };
133
+ }
134
+ const desiredNames = stringArray(value.desiredNames);
135
+ if (!desiredNames) return denied;
136
+ return { kind, desiredNames, allowedNames, resolved: value.resolved };
125
137
  }
126
138
 
127
139
  function normalizeSavedPlan(value: unknown): SavedPlan | undefined {
128
- if (!isRecord(value)) return undefined;
129
- const source = planCompletionSource(value.source);
130
- const normalized = normalizePlanModeCompletion({ plan: value.plan });
131
- if (!source || !normalized.ok) return undefined;
132
- return { plan: normalized.plan, source };
140
+ if (!isRecord(value)) return undefined;
141
+ const source = planCompletionSource(value.source);
142
+ const normalized = normalizePlanModeCompletion({ plan: value.plan });
143
+ if (!source || !normalized.ok) return undefined;
144
+ return { plan: normalized.plan, source };
145
+ }
146
+
147
+ function normalizePendingImplementationRuntime(value: unknown): PendingImplementationRuntime | undefined {
148
+ if (!isRecord(value) || value.version !== 1) return undefined;
149
+ if (Object.keys(value).some((key) => key !== "version" && key !== "model" && key !== "thinkingLevel")) {
150
+ return undefined;
151
+ }
152
+ let model: ImplementationModelOverride | undefined;
153
+ if (value.model !== undefined) {
154
+ if (!isRecord(value.model) || Object.keys(value.model).some((key) => key !== "provider" && key !== "modelId")) {
155
+ return undefined;
156
+ }
157
+ if (
158
+ !isPendingImplementationModelIdentifier(value.model.provider) ||
159
+ !isPendingImplementationModelIdentifier(value.model.modelId)
160
+ ) {
161
+ return undefined;
162
+ }
163
+ model = { provider: value.model.provider, modelId: value.model.modelId };
164
+ }
165
+ const thinkingLevel = value.thinkingLevel === undefined ? undefined : fixedThinkingLevel(value.thinkingLevel);
166
+ if (value.thinkingLevel !== undefined && !thinkingLevel) return undefined;
167
+ if (!model && !thinkingLevel) return undefined;
168
+ return {
169
+ version: 1,
170
+ ...(model ? { model } : {}),
171
+ ...(thinkingLevel ? { thinkingLevel } : {}),
172
+ };
133
173
  }
134
174
 
135
175
  function normalizeActiveImplementation(value: unknown): ActiveImplementationPlan | undefined {
136
- if (!isRecord(value)) return undefined;
137
- const id =
138
- typeof value.id === "string" && /^[A-Za-z0-9._:-]{1,128}$/u.test(value.id)
139
- ? value.id
140
- : undefined;
141
- const source = planCompletionSource(value.source);
142
- const normalized = normalizePlanModeCompletion({ plan: value.plan });
143
- const startedAt =
144
- typeof value.startedAt === "number" &&
145
- Number.isSafeInteger(value.startedAt) &&
146
- value.startedAt >= 0
147
- ? value.startedAt
148
- : undefined;
149
- if (!id || !source || !normalized.ok || startedAt === undefined) return undefined;
150
- const retention = IMPLEMENTATION_PLAN_RETENTIONS.includes(
151
- value.retention as ImplementationPlanRetention,
152
- )
153
- ? (value.retention as ImplementationPlanRetention)
154
- : "keep";
155
- return { id, plan: normalized.plan, source, startedAt, retention };
176
+ if (!isRecord(value)) return undefined;
177
+ const id = typeof value.id === "string" && /^[A-Za-z0-9._:-]{1,128}$/u.test(value.id) ? value.id : undefined;
178
+ const source = planCompletionSource(value.source);
179
+ const normalized = normalizePlanModeCompletion({ plan: value.plan });
180
+ const startedAt =
181
+ typeof value.startedAt === "number" && Number.isSafeInteger(value.startedAt) && value.startedAt >= 0
182
+ ? value.startedAt
183
+ : undefined;
184
+ if (!id || !source || !normalized.ok || startedAt === undefined) return undefined;
185
+ const retention = IMPLEMENTATION_PLAN_RETENTIONS.includes(value.retention as ImplementationPlanRetention)
186
+ ? (value.retention as ImplementationPlanRetention)
187
+ : "keep";
188
+ return { id, plan: normalized.plan, source, startedAt, retention };
156
189
  }
157
190
 
158
191
  function normalizePersistedPlan(value: unknown) {
159
- const normalized = normalizePlanModeCompletion({ plan: value });
160
- return normalized.ok ? normalized.plan : undefined;
192
+ const normalized = normalizePlanModeCompletion({ plan: value });
193
+ return normalized.ok ? normalized.plan : undefined;
161
194
  }
162
195
 
163
196
  function latestCompletionPlan(entries: SessionEntry[]) {
164
- for (let index = entries.length - 1; index >= 0; index -= 1) {
165
- const message = entries[index]?.message;
166
- if (message?.role !== "toolResult" || message.toolName !== PLAN_MODE_COMPLETE_TOOL_NAME) {
167
- continue;
168
- }
169
- const plan = planFromCompletionDetails(message.details);
170
- if (plan) return plan;
171
- }
172
- return undefined;
197
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
198
+ const message = entries[index]?.message;
199
+ if (message?.role !== "toolResult" || message.toolName !== PLAN_MODE_COMPLETE_TOOL_NAME) {
200
+ continue;
201
+ }
202
+ const plan = planFromCompletionDetails(message.details);
203
+ if (plan) return plan;
204
+ }
205
+ return undefined;
173
206
  }
174
207
 
175
208
  function planCompletionSource(value: unknown): PlanCompletionSource | undefined {
176
- return value === PLAN_MODE_COMPLETE_TOOL_NAME || value === "legacy_proposed_plan"
177
- ? value
178
- : undefined;
209
+ return value === PLAN_MODE_COMPLETE_TOOL_NAME || value === "legacy_proposed_plan" ? value : undefined;
179
210
  }
180
211
 
181
212
  function fixedThinkingLevel(value: unknown): PlanModeFixedThinkingLevel | undefined {
182
- return typeof value === "string" &&
183
- value !== "inherit" &&
184
- PLAN_MODE_THINKING_LEVELS.includes(value as (typeof PLAN_MODE_THINKING_LEVELS)[number])
185
- ? (value as PlanModeFixedThinkingLevel)
186
- : undefined;
213
+ return typeof value === "string" &&
214
+ value !== "inherit" &&
215
+ PLAN_MODE_THINKING_LEVELS.includes(value as (typeof PLAN_MODE_THINKING_LEVELS)[number])
216
+ ? (value as PlanModeFixedThinkingLevel)
217
+ : undefined;
187
218
  }
188
219
 
189
220
  function stringArray(value: unknown) {
190
- return Array.isArray(value) && value.every((item): item is string => typeof item === "string")
191
- ? Array.from(new Set(value))
192
- : undefined;
221
+ return Array.isArray(value) && value.every((item): item is string => typeof item === "string")
222
+ ? Array.from(new Set(value))
223
+ : undefined;
193
224
  }
194
225
 
195
226
  function isRecord(value: unknown): value is Record<string, unknown> {
196
- return typeof value === "object" && value !== null && !Array.isArray(value);
227
+ return typeof value === "object" && value !== null && !Array.isArray(value);
197
228
  }