@narumitw/pi-plan-mode 0.58.0 → 0.58.2

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-L2CXUWCH.ts} +29 -111
  2. package/dist/chunks/chunk-L2CXUWCH.ts.map +7 -0
  3. package/dist/chunks/{interactive-ui-UNJGC2RQ.js → interactive-ui-VHZ5GODE.ts} +13 -41
  4. package/dist/chunks/interactive-ui-VHZ5GODE.ts.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
package/src/state.ts CHANGED
@@ -1,259 +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
- type ImplementationModelOverride,
8
- isPendingImplementationModelIdentifier,
9
- } from "./implementation-models.js";
10
- import {
11
- IMPLEMENTATION_PLAN_RETENTIONS,
12
- type ImplementationPlanRetention,
13
- PLAN_MODE_THINKING_LEVELS,
14
- type PlanModeFixedThinkingLevel,
8
+ IMPLEMENTATION_PLAN_RETENTIONS,
9
+ type ImplementationPlanRetention,
10
+ PLAN_MODE_THINKING_LEVELS,
11
+ type PlanModeFixedThinkingLevel,
15
12
  } from "./settings.js";
16
13
 
17
14
  export type { ImplementationModelOverride } from "./implementation-models.js";
18
15
  export {
19
- isPendingImplementationModelIdentifier,
20
- MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH,
16
+ isPendingImplementationModelIdentifier,
17
+ MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH,
21
18
  } from "./implementation-models.js";
22
19
 
23
20
  export type PlanCompletionSource = typeof PLAN_MODE_COMPLETE_TOOL_NAME | "legacy_proposed_plan";
24
21
 
25
22
  export interface ActiveImplementationPlan {
26
- id: string;
27
- plan: string;
28
- source: PlanCompletionSource;
29
- startedAt: number;
30
- retention?: ImplementationPlanRetention;
23
+ id: string;
24
+ plan: string;
25
+ source: PlanCompletionSource;
26
+ startedAt: number;
27
+ retention?: ImplementationPlanRetention;
31
28
  }
32
29
 
33
30
  export interface SavedPlan {
34
- plan: string;
35
- source: PlanCompletionSource;
31
+ plan: string;
32
+ source: PlanCompletionSource;
36
33
  }
37
34
 
38
35
  export interface ImplementationRuntimeSelection {
39
- model?: ImplementationModelOverride;
40
- thinkingLevel?: PlanModeFixedThinkingLevel;
36
+ model?: ImplementationModelOverride;
37
+ thinkingLevel?: PlanModeFixedThinkingLevel;
41
38
  }
42
39
 
43
40
  export interface PendingImplementationRuntime extends ImplementationRuntimeSelection {
44
- version: 1;
41
+ version: 1;
45
42
  }
46
43
 
47
44
  export interface PlanModeWorkflowToolPolicy {
48
- kind: "automatic" | "explicit";
49
- desiredNames?: string[];
50
- allowedNames: string[];
51
- resolved: boolean;
45
+ kind: "automatic" | "explicit";
46
+ desiredNames?: string[];
47
+ allowedNames: string[];
48
+ resolved: boolean;
52
49
  }
53
50
 
54
51
  export interface PlanModeState {
55
- enabled: boolean;
56
- latestPlan?: string;
57
- latestPlanSource?: PlanCompletionSource;
58
- awaitingAction: boolean;
59
- savedPlan?: SavedPlan;
60
- activeImplementation?: ActiveImplementationPlan;
61
- pendingImplementationRuntime?: PendingImplementationRuntime;
62
- selectedToolNames?: string[];
63
- selectedToolKeys?: string[];
64
- workflowToolPolicy?: PlanModeWorkflowToolPolicy;
65
- previousThinkingLevel?: PlanModeFixedThinkingLevel;
66
- appliedThinkingLevel?: PlanModeFixedThinkingLevel;
67
- 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;
68
65
  }
69
66
 
70
67
  type SessionEntry = {
71
- type?: string;
72
- customType?: string;
73
- data?: unknown;
74
- message?: {
75
- role?: string;
76
- toolName?: string;
77
- details?: unknown;
78
- };
68
+ type?: string;
69
+ customType?: string;
70
+ data?: unknown;
71
+ message?: {
72
+ role?: string;
73
+ toolName?: string;
74
+ details?: unknown;
75
+ };
79
76
  };
80
77
 
81
78
  export function restorePlanModeState(entries: unknown[], stateEntryType: string): PlanModeState {
82
- const branch = entries as SessionEntry[];
83
- let stateEntryIndex = -1;
84
- for (let index = branch.length - 1; index >= 0; index -= 1) {
85
- const candidate = branch[index];
86
- if (candidate?.type === "custom" && candidate.customType === stateEntryType) {
87
- stateEntryIndex = index;
88
- break;
89
- }
90
- }
91
- const entry = branch[stateEntryIndex];
92
- if (!isRecord(entry?.data)) return { enabled: false, awaitingAction: false };
93
-
94
- const enabled = entry.data.enabled === true;
95
- const persistedSource = enabled ? planCompletionSource(entry.data.latestPlanSource) : undefined;
96
- const persistedPlan = enabled ? normalizePersistedPlan(entry.data.latestPlan) : undefined;
97
- const recoveredPlan =
98
- enabled && !persistedPlan ? latestCompletionPlan(branch.slice(stateEntryIndex + 1)) : undefined;
99
- const latestPlan = persistedPlan ?? recoveredPlan;
100
- const activeImplementation = enabled
101
- ? undefined
102
- : normalizeActiveImplementation(entry.data.activeImplementation);
103
- const savedPlan =
104
- enabled || activeImplementation ? undefined : normalizeSavedPlan(entry.data.savedPlan);
105
- const pendingImplementationRuntime = enabled
106
- ? undefined
107
- : normalizePendingImplementationRuntime(entry.data.pendingImplementationRuntime);
108
- return {
109
- enabled,
110
- latestPlan,
111
- latestPlanSource: enabled
112
- ? ((persistedPlan ? persistedSource : undefined) ??
113
- (recoveredPlan ? PLAN_MODE_COMPLETE_TOOL_NAME : undefined))
114
- : undefined,
115
- awaitingAction: enabled && latestPlan !== undefined,
116
- savedPlan,
117
- activeImplementation,
118
- pendingImplementationRuntime,
119
- selectedToolNames: stringArray(entry.data.selectedToolNames),
120
- selectedToolKeys: stringArray(entry.data.selectedToolKeys),
121
- workflowToolPolicy: enabled
122
- ? normalizeWorkflowToolPolicy(entry.data.workflowToolPolicy)
123
- : undefined,
124
- previousThinkingLevel: enabled
125
- ? fixedThinkingLevel(entry.data.previousThinkingLevel)
126
- : undefined,
127
- appliedThinkingLevel: enabled ? fixedThinkingLevel(entry.data.appliedThinkingLevel) : undefined,
128
- manualThinkingLevel: enabled ? fixedThinkingLevel(entry.data.manualThinkingLevel) : undefined,
129
- };
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
+ };
130
118
  }
131
119
 
132
120
  function normalizeWorkflowToolPolicy(value: unknown): PlanModeWorkflowToolPolicy | undefined {
133
- if (value === undefined) return undefined;
134
- const denied: PlanModeWorkflowToolPolicy = {
135
- kind: "automatic",
136
- allowedNames: [],
137
- resolved: true,
138
- };
139
- if (!isRecord(value)) return denied;
140
- const kind = value.kind === "automatic" || value.kind === "explicit" ? value.kind : undefined;
141
- const allowedNames = stringArray(value.allowedNames);
142
- if (!kind || !allowedNames || typeof value.resolved !== "boolean") return denied;
143
- if (kind === "automatic") {
144
- return { kind, allowedNames, resolved: value.resolved };
145
- }
146
- const desiredNames = stringArray(value.desiredNames);
147
- if (!desiredNames) return denied;
148
- 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 };
149
137
  }
150
138
 
151
139
  function normalizeSavedPlan(value: unknown): SavedPlan | undefined {
152
- if (!isRecord(value)) return undefined;
153
- const source = planCompletionSource(value.source);
154
- const normalized = normalizePlanModeCompletion({ plan: value.plan });
155
- if (!source || !normalized.ok) return undefined;
156
- return { plan: normalized.plan, source };
157
- }
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
- };
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
+ };
195
173
  }
196
174
 
197
175
  function normalizeActiveImplementation(value: unknown): ActiveImplementationPlan | undefined {
198
- if (!isRecord(value)) return undefined;
199
- const id =
200
- typeof value.id === "string" && /^[A-Za-z0-9._:-]{1,128}$/u.test(value.id)
201
- ? value.id
202
- : undefined;
203
- const source = planCompletionSource(value.source);
204
- const normalized = normalizePlanModeCompletion({ plan: value.plan });
205
- const startedAt =
206
- typeof value.startedAt === "number" &&
207
- Number.isSafeInteger(value.startedAt) &&
208
- value.startedAt >= 0
209
- ? value.startedAt
210
- : undefined;
211
- if (!id || !source || !normalized.ok || startedAt === undefined) return undefined;
212
- const retention = IMPLEMENTATION_PLAN_RETENTIONS.includes(
213
- value.retention as ImplementationPlanRetention,
214
- )
215
- ? (value.retention as ImplementationPlanRetention)
216
- : "keep";
217
- 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 };
218
189
  }
219
190
 
220
191
  function normalizePersistedPlan(value: unknown) {
221
- const normalized = normalizePlanModeCompletion({ plan: value });
222
- return normalized.ok ? normalized.plan : undefined;
192
+ const normalized = normalizePlanModeCompletion({ plan: value });
193
+ return normalized.ok ? normalized.plan : undefined;
223
194
  }
224
195
 
225
196
  function latestCompletionPlan(entries: SessionEntry[]) {
226
- for (let index = entries.length - 1; index >= 0; index -= 1) {
227
- const message = entries[index]?.message;
228
- if (message?.role !== "toolResult" || message.toolName !== PLAN_MODE_COMPLETE_TOOL_NAME) {
229
- continue;
230
- }
231
- const plan = planFromCompletionDetails(message.details);
232
- if (plan) return plan;
233
- }
234
- 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;
235
206
  }
236
207
 
237
208
  function planCompletionSource(value: unknown): PlanCompletionSource | undefined {
238
- return value === PLAN_MODE_COMPLETE_TOOL_NAME || value === "legacy_proposed_plan"
239
- ? value
240
- : undefined;
209
+ return value === PLAN_MODE_COMPLETE_TOOL_NAME || value === "legacy_proposed_plan" ? value : undefined;
241
210
  }
242
211
 
243
212
  function fixedThinkingLevel(value: unknown): PlanModeFixedThinkingLevel | undefined {
244
- return typeof value === "string" &&
245
- value !== "inherit" &&
246
- PLAN_MODE_THINKING_LEVELS.includes(value as (typeof PLAN_MODE_THINKING_LEVELS)[number])
247
- ? (value as PlanModeFixedThinkingLevel)
248
- : 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;
249
218
  }
250
219
 
251
220
  function stringArray(value: unknown) {
252
- return Array.isArray(value) && value.every((item): item is string => typeof item === "string")
253
- ? Array.from(new Set(value))
254
- : undefined;
221
+ return Array.isArray(value) && value.every((item): item is string => typeof item === "string")
222
+ ? Array.from(new Set(value))
223
+ : undefined;
255
224
  }
256
225
 
257
226
  function isRecord(value: unknown): value is Record<string, unknown> {
258
- return typeof value === "object" && value !== null && !Array.isArray(value);
227
+ return typeof value === "object" && value !== null && !Array.isArray(value);
259
228
  }