@narumitw/pi-plan-mode 0.57.0 → 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.
@@ -70,109 +70,262 @@ function isRecord(value) {
70
70
  return typeof value === "object" && value !== null && !Array.isArray(value);
71
71
  }
72
72
 
73
- // src/implementation-preferences.ts
74
- function observeImplementationModelSelections(pi) {
75
- let latest;
76
- pi.on("model_select", (_event, ctx) => {
77
- latest = { model: ctx.model, thinking: pi.getThinkingLevel() };
78
- });
79
- return () => latest;
73
+ // src/message-transform.ts
74
+ var PLAN_CONTEXT_MESSAGE_TYPE = "plan-mode-context";
75
+ var PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE = "plan-mode-implementation-context";
76
+ var PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
77
+ var PLAN_IMPLEMENTATION_HANDOFF_PREFIX = "Plan mode is now disabled. Full tool access is restored. Implement this proposed plan now:";
78
+ var PLAN_HISTORY_IMPLEMENTATION_PROMPT = "Implement the plan.";
79
+ var PROPOSED_PLAN_PATTERN = /^<proposed_plan>[\t ]*\r?\n([\s\S]*?)\r?\n<\/proposed_plan>[\t ]*$/gm;
80
+ var PROPOSED_PLAN_BLOCK_PATTERN = /^<proposed_plan>[\t ]*\r?\n[\s\S]*?\r?\n<\/proposed_plan>[\t ]*$/gm;
81
+ function parseProposedPlan(text) {
82
+ const openingCount = text.match(/<proposed_plan>/gi)?.length ?? 0;
83
+ const closingCount = text.match(/<\/proposed_plan>/gi)?.length ?? 0;
84
+ if (openingCount === 0 && closingCount === 0) return { kind: "absent" };
85
+ if (openingCount > 1 || closingCount > 1) return { kind: "multiple" };
86
+ if (openingCount === 1 && closingCount === 0) return { kind: "unclosed" };
87
+ if (openingCount !== 1 || closingCount !== 1) return { kind: "malformed" };
88
+ const matches = Array.from(text.matchAll(PROPOSED_PLAN_PATTERN));
89
+ if (matches.length !== 1) return { kind: "malformed" };
90
+ const plan = matches[0]?.[1]?.trim() ?? "";
91
+ return plan ? { kind: "valid", plan } : { kind: "empty" };
80
92
  }
81
- function hasImplementationPreferences(preferences) {
82
- return preferences.implementationModel !== void 0 || preferences.implementationThinkingLevel !== void 0 && preferences.implementationThinkingLevel !== "inherit";
93
+ function invalidPlanMessage(kind) {
94
+ const detail = {
95
+ empty: "the block is empty",
96
+ multiple: "more than one plan block was produced",
97
+ malformed: "the tags must be on their own lines",
98
+ unclosed: "the closing tag is missing"
99
+ }[kind];
100
+ return `Proposed plan is not ready: ${detail}. Continue Plan mode and produce one complete non-empty <proposed_plan> block.`;
83
101
  }
84
- function sameModel(left, right) {
85
- return left?.provider === right?.provider && left?.id === right?.id;
102
+ function latestAssistantText(messages) {
103
+ if (!Array.isArray(messages)) return "";
104
+ for (const entry of [...messages].reverse()) {
105
+ const message = entry?.message ?? entry;
106
+ if (message?.role !== "assistant") continue;
107
+ const text = messageText(message);
108
+ if (text) return text;
109
+ }
110
+ return "";
86
111
  }
87
- function safeImplementationText(value) {
88
- return [...value].map((character) => {
89
- const code = character.codePointAt(0) ?? 0;
90
- return code <= 31 || code >= 127 && code <= 159 ? " " : character;
91
- }).join("").trim();
112
+ function latestAssistantStopReason(messages) {
113
+ if (!Array.isArray(messages)) return void 0;
114
+ for (const entry of [...messages].reverse()) {
115
+ const message = entry?.message ?? entry;
116
+ if (message?.role === "assistant") return message.stopReason;
117
+ }
118
+ return void 0;
92
119
  }
93
- function preferenceError(error) {
94
- return safeImplementationText(error instanceof Error ? error.message : String(error)).slice(
95
- 0,
96
- 500
120
+ function messageTextContent(message) {
121
+ return messageText(
122
+ message?.message ?? message
97
123
  );
98
124
  }
99
- async function preflightImplementationPreferences(ctx, preferences, isCurrent) {
100
- if (!isCurrent()) return void 0;
101
- const identity = preferences.implementationModel;
102
- const model = identity ? ctx.modelRegistry.find(identity.provider, identity.id) : ctx.model;
103
- if (!model)
104
- throw new Error(
105
- "Implementation model is unavailable. Select a registered model or use current."
106
- );
107
- const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
108
- if (!isCurrent()) return void 0;
109
- const currentModel = identity ? ctx.modelRegistry.find(identity.provider, identity.id) : model;
110
- if (!currentModel) {
111
- throw new Error(
112
- "Implementation model catalogue changed during preflight. Reopen the menu and retry."
113
- );
114
- }
115
- if (!auth.ok) throw new Error(auth.error);
116
- return currentModel;
125
+ function messageContainsLegacyPlanModeContextArtifact(message) {
126
+ return unwrapSessionMessage(message).customType === PLAN_CONTEXT_MESSAGE_TYPE;
117
127
  }
118
- function createImplementationPreferenceChange(pi, ctx, isSessionCurrent, original = { model: ctx.model, thinking: pi.getThinkingLevel() }, selectionSnapshot) {
119
- let applied;
120
- const snapshot = () => ({
121
- model: ctx.model,
122
- thinking: pi.getThinkingLevel()
123
- });
124
- const matches = (value) => sameModel(ctx.model, value.model) && pi.getThinkingLevel() === value.thinking;
125
- return {
126
- async apply(model, preferences, isCurrent) {
127
- if (!isCurrent()) return false;
128
- applied = snapshot();
129
- if (preferences.implementationModel && !sameModel(ctx.model, model)) {
130
- const beforeSelection = selectionSnapshot?.();
131
- if (!await pi.setModel(model))
132
- throw new Error("Implementation model authentication is unavailable.");
133
- if (!isSessionCurrent()) return false;
134
- if (!sameModel(ctx.model, model)) return false;
135
- const selected = selectionSnapshot?.();
136
- applied = selected && selected !== beforeSelection ? selected : snapshot();
137
- if (!matches(applied)) {
138
- if (ctx.hasUI)
139
- ctx.ui.notify(
140
- "Model or thinking changed during implementation preparation. The plan remains available; retry when ready.",
141
- "warning"
142
- );
143
- return false;
144
- }
145
- if (!isCurrent()) return false;
146
- }
147
- const level = preferences.implementationThinkingLevel;
148
- if (level && level !== "inherit") {
149
- pi.setThinkingLevel(level);
150
- applied = snapshot();
151
- if (applied.thinking !== level && ctx.hasUI)
152
- ctx.ui.notify(
153
- `Implementation thinking: ${applied.thinking} (requested ${level}; adjusted by Pi for this model).`,
154
- "info"
155
- );
156
- }
157
- return isCurrent();
158
- },
159
- async rollback() {
160
- if (!applied || !isSessionCurrent() || !ctx.isIdle() || !matches(applied)) return;
161
- if (!sameModel(ctx.model, original.model)) {
162
- const beforeSelection = selectionSnapshot?.();
163
- if (!original.model || !await pi.setModel(original.model)) {
164
- throw new Error(
165
- "Could not restore the previous model. The plan remains available; select a model before retrying."
166
- );
167
- }
168
- if (!isSessionCurrent() || !ctx.isIdle() || !sameModel(ctx.model, original.model)) return;
169
- const selected = selectionSnapshot?.();
170
- if (selected && selected !== beforeSelection && !matches(selected)) return;
128
+ function messageContainsPlanModeImplementationContextArtifact(message) {
129
+ return unwrapSessionMessage(message).customType === PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE;
130
+ }
131
+ function injectActiveImplementationContext(messages, activeImplementation) {
132
+ let foundCurrentContext = false;
133
+ let foundCurrentHandoff = false;
134
+ const expectedContext = activeImplementationContextContent(activeImplementation);
135
+ const messagesWithoutStaleContext = messages.filter((message) => {
136
+ if (messageContainsPlanModeImplementationContextArtifact(message)) {
137
+ const candidate = unwrapSessionMessage(message);
138
+ if (!foundCurrentContext && candidate.content === expectedContext) {
139
+ foundCurrentContext = true;
140
+ return true;
171
141
  }
172
- pi.setThinkingLevel(original.thinking);
173
- applied = void 0;
142
+ return false;
143
+ }
144
+ if (!messageContainsPlanModeImplementationHandoff(message)) return true;
145
+ if (!foundCurrentHandoff && messageContainsExactPlanModeImplementationHandoff(message, activeImplementation.plan)) {
146
+ foundCurrentHandoff = true;
147
+ return true;
174
148
  }
149
+ return false;
150
+ });
151
+ if (foundCurrentHandoff || foundCurrentContext) return messagesWithoutStaleContext;
152
+ let insertionIndex = 0;
153
+ while (isSummaryMessage(messagesWithoutStaleContext[insertionIndex])) insertionIndex += 1;
154
+ const contextMessage = {
155
+ role: "custom",
156
+ customType: PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE,
157
+ content: expectedContext,
158
+ display: false,
159
+ timestamp: activeImplementation.startedAt
175
160
  };
161
+ return [
162
+ ...messagesWithoutStaleContext.slice(0, insertionIndex),
163
+ contextMessage,
164
+ ...messagesWithoutStaleContext.slice(insertionIndex)
165
+ ];
166
+ }
167
+ function activeImplementationContextContent(activeImplementation) {
168
+ return `[ACTIVE IMPLEMENTATION PLAN]
169
+
170
+ The user approved the exact implementation plan below. Continue following it until the user explicitly clears or supersedes it. The exact plan is the remainder of this message:
171
+
172
+ ${activeImplementation.plan}`;
173
+ }
174
+ function messageContainsInactivePlanModeArtifact(message) {
175
+ const candidate = unwrapSessionMessage(message);
176
+ return candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE || candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME;
177
+ }
178
+ function findHistoryImplementationArtifact(messages) {
179
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
180
+ const candidate = unwrapSessionMessage(messages[index]);
181
+ if (candidate.role !== "user" || contentText(candidate.content).trim() !== PLAN_HISTORY_IMPLEMENTATION_PROMPT) {
182
+ continue;
183
+ }
184
+ const artifact = findHistoryImplementationArtifactBeforeKickoff(messages, index);
185
+ if (artifact) return artifact;
186
+ }
187
+ return void 0;
188
+ }
189
+ function findHistoryImplementationArtifactBeforeKickoff(messages, kickoffIndex) {
190
+ for (let index = kickoffIndex - 1; index >= 0; index -= 1) {
191
+ const candidate = unwrapSessionMessage(messages[index]);
192
+ if (candidate.role === "user" || isSummaryMessage(messages[index])) return void 0;
193
+ if (candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME) {
194
+ const toolCallId = candidate.toolCallId;
195
+ const toolCallMessageIndex = toolCallId ? findPlanModeCompletionCallMessageIndex(messages, index, toolCallId) : void 0;
196
+ if (toolCallId && toolCallMessageIndex !== void 0) {
197
+ return {
198
+ messageIndex: index,
199
+ kind: "completion",
200
+ toolCallId,
201
+ toolCallMessageIndex
202
+ };
203
+ }
204
+ continue;
205
+ }
206
+ if (candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE) {
207
+ return { messageIndex: index, kind: "presentation" };
208
+ }
209
+ if (candidate.role === "assistant" && contentText(candidate.content).match(PROPOSED_PLAN_BLOCK_PATTERN)) {
210
+ return { messageIndex: index, kind: "legacy" };
211
+ }
212
+ }
213
+ return void 0;
214
+ }
215
+ function findPlanModeCompletionCallMessageIndex(messages, endIndex, toolCallId) {
216
+ for (let index = endIndex - 1; index >= 0; index -= 1) {
217
+ const candidate = unwrapSessionMessage(messages[index]);
218
+ if (candidate.role === "user" || isSummaryMessage(messages[index])) return void 0;
219
+ if (!Array.isArray(candidate.content)) continue;
220
+ if (candidate.content.some((block) => {
221
+ const toolCall = block;
222
+ return toolCall.type === "toolCall" && toolCall.id === toolCallId && toolCall.name === PLAN_MODE_COMPLETE_TOOL_NAME;
223
+ })) {
224
+ return index;
225
+ }
226
+ }
227
+ return void 0;
228
+ }
229
+ function messageContainsPlanModeImplementationHandoff(message) {
230
+ const candidate = unwrapSessionMessage(message);
231
+ return candidate.role === "user" && contentText(candidate.content).trimStart().startsWith(PLAN_IMPLEMENTATION_HANDOFF_PREFIX);
232
+ }
233
+ function messageContainsExactPlanModeImplementationHandoff(message, plan) {
234
+ const candidate = unwrapSessionMessage(message);
235
+ if (candidate.role !== "user") return false;
236
+ return contentText(candidate.content).trim() === `${PLAN_IMPLEMENTATION_HANDOFF_PREFIX}
237
+
238
+ ${plan}`.trim();
239
+ }
240
+ function isSummaryMessage(message) {
241
+ const role = unwrapSessionMessage(message)?.role;
242
+ return role === "compactionSummary" || role === "branchSummary";
243
+ }
244
+ function stripProposedPlanBlocksFromMessage(message) {
245
+ return replaceAssistantContent(message, stripProposedPlanBlocksFromContent);
246
+ }
247
+ function stripPlanModeCompletionCallsFromMessage(message, preservedToolCallId) {
248
+ return replaceAssistantContent(message, (content) => {
249
+ if (!Array.isArray(content)) return content;
250
+ const nextContent = content.filter((block) => {
251
+ const candidate = block;
252
+ return !(candidate.type === "toolCall" && candidate.name === PLAN_MODE_COMPLETE_TOOL_NAME && (!preservedToolCallId || candidate.id !== preservedToolCallId));
253
+ });
254
+ return nextContent.length === content.length ? content : nextContent;
255
+ });
256
+ }
257
+ function isEmptyAssistantMessage(message) {
258
+ const candidate = unwrapSessionMessage(message);
259
+ return candidate.role === "assistant" && Array.isArray(candidate.content) && candidate.content.length === 0;
260
+ }
261
+ function replaceAssistantContent(message, transform) {
262
+ const candidate = unwrapSessionMessage(message);
263
+ if (candidate.role !== "assistant") return message;
264
+ const content = transform(candidate.content);
265
+ if (content === candidate.content) return message;
266
+ if (isSessionMessageEntry(message)) {
267
+ return { ...message, message: { ...candidate, content } };
268
+ }
269
+ return { ...candidate, content };
270
+ }
271
+ function unwrapSessionMessage(message) {
272
+ const entry = message;
273
+ return entry?.message ?? message ?? {};
274
+ }
275
+ function isSessionMessageEntry(message) {
276
+ return typeof message === "object" && message !== null && "message" in message;
277
+ }
278
+ function stripProposedPlanBlocksFromContent(content) {
279
+ if (typeof content === "string") return stripProposedPlanBlocks(content);
280
+ if (!Array.isArray(content)) return content;
281
+ let changed = false;
282
+ const nextContent = content.map((block) => {
283
+ const textBlock = block;
284
+ if (textBlock.type !== "text" || typeof textBlock.text !== "string") return block;
285
+ const text = stripProposedPlanBlocks(textBlock.text);
286
+ if (text === textBlock.text) return block;
287
+ changed = true;
288
+ return { ...textBlock, text };
289
+ });
290
+ return changed ? nextContent : content;
291
+ }
292
+ function stripProposedPlanBlocks(text) {
293
+ return text.replace(PROPOSED_PLAN_BLOCK_PATTERN, "");
294
+ }
295
+ function messageText(message) {
296
+ return contentText(message.content);
297
+ }
298
+ function contentText(content) {
299
+ if (typeof content === "string") return content;
300
+ if (!Array.isArray(content)) return "";
301
+ return content.map((block) => {
302
+ const textBlock = block;
303
+ return textBlock.type === "text" && typeof textBlock.text === "string" ? textBlock.text : "";
304
+ }).filter(Boolean).join("\n");
305
+ }
306
+
307
+ // src/implementation-models.ts
308
+ var MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH = 512;
309
+ function isPendingImplementationModelIdentifier(value) {
310
+ return typeof value === "string" && value.trim().length > 0 && value.length <= MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH;
311
+ }
312
+ function snapshotAvailableImplementationModels(ctx) {
313
+ const getAvailable = ctx.modelRegistry.getAvailable;
314
+ const availableModels = typeof getAvailable === "function" ? getAvailable.call(ctx.modelRegistry) : [];
315
+ const scopedModels = ctx.scopedModels ?? [];
316
+ if (scopedModels.length === 0) return availableModels;
317
+ return scopedModels.flatMap((entry) => {
318
+ const availableModel = availableModels.find(
319
+ (model) => model.provider === entry.model.provider && model.id === entry.model.id
320
+ );
321
+ return availableModel ? [availableModel] : [];
322
+ });
323
+ }
324
+ function findAvailableImplementationModel(models, configured) {
325
+ if (!configured) return void 0;
326
+ return models.find(
327
+ (model) => model.provider === configured.provider && model.id === configured.modelId
328
+ );
176
329
  }
177
330
 
178
331
  // src/settings.ts
@@ -194,6 +347,15 @@ var PLAN_MODE_THINKING_LEVELS = [
194
347
  "xhigh",
195
348
  "max"
196
349
  ];
350
+ var IMPLEMENTATION_THINKING_LEVELS = [
351
+ "off",
352
+ "minimal",
353
+ "low",
354
+ "medium",
355
+ "high",
356
+ "xhigh",
357
+ "max"
358
+ ];
197
359
  var IMPLEMENTATION_PLAN_RETENTIONS = [
198
360
  "clear-on-start",
199
361
  "clear-after-first-run",
@@ -271,17 +433,6 @@ function normalizePlanModeSettings(value) {
271
433
  const settings = {
272
434
  thinkingLevel
273
435
  };
274
- if (Object.hasOwn(value, "implementationModel")) {
275
- const model = Reflect.get(value, "implementationModel");
276
- if (!isSettingsDocument(model) || !validModelIdentity(model.provider) || !validModelIdentity(model.id))
277
- return void 0;
278
- settings.implementationModel = { provider: model.provider, id: model.id };
279
- }
280
- if (Object.hasOwn(value, "implementationThinkingLevel")) {
281
- const level = Reflect.get(value, "implementationThinkingLevel");
282
- if (!PLAN_MODE_THINKING_LEVELS.includes(level)) return void 0;
283
- settings.implementationThinkingLevel = level;
284
- }
285
436
  if (Object.hasOwn(value, "defaultPlanTools")) {
286
437
  const defaultPlanTools = normalizeToolNames(Reflect.get(value, "defaultPlanTools"));
287
438
  if (!defaultPlanTools) return void 0;
@@ -296,6 +447,25 @@ function normalizePlanModeSettings(value) {
296
447
  }
297
448
  settings.implementationPlanRetention = implementationPlanRetention;
298
449
  }
450
+ if (Object.hasOwn(value, "defaultImplementationModel")) {
451
+ const defaultImplementationModel = normalizeImplementationModel(
452
+ Reflect.get(value, "defaultImplementationModel")
453
+ );
454
+ if (!defaultImplementationModel) return void 0;
455
+ settings.defaultImplementationModel = defaultImplementationModel;
456
+ }
457
+ if (Object.hasOwn(value, "defaultImplementationThinkingLevel")) {
458
+ const defaultImplementationThinkingLevel = Reflect.get(
459
+ value,
460
+ "defaultImplementationThinkingLevel"
461
+ );
462
+ if (!IMPLEMENTATION_THINKING_LEVELS.includes(
463
+ defaultImplementationThinkingLevel
464
+ )) {
465
+ return void 0;
466
+ }
467
+ settings.defaultImplementationThinkingLevel = defaultImplementationThinkingLevel;
468
+ }
299
469
  if (Object.hasOwn(value, "defaultPlanExportPath")) {
300
470
  const defaultPlanExportPath = normalizePlanExportPath(
301
471
  Reflect.get(value, "defaultPlanExportPath")
@@ -315,8 +485,16 @@ function normalizePlanModeSettings(value) {
315
485
  }
316
486
  return settings;
317
487
  }
318
- function validModelIdentity(value) {
319
- return typeof value === "string" && value.trim().length > 0 && value.length <= 4096;
488
+ function normalizeImplementationModel(value) {
489
+ if (!isSettingsDocument(value) || Object.keys(value).some((key) => key !== "provider" && key !== "modelId")) {
490
+ return void 0;
491
+ }
492
+ const provider = typeof value.provider === "string" ? value.provider.trim() : value.provider;
493
+ const modelId = typeof value.modelId === "string" ? value.modelId.trim() : value.modelId;
494
+ if (!isPendingImplementationModelIdentifier(provider) || !isPendingImplementationModelIdentifier(modelId)) {
495
+ return void 0;
496
+ }
497
+ return { provider, modelId };
320
498
  }
321
499
  function normalizeToolNames(value) {
322
500
  if (!Array.isArray(value) || !value.every((item) => typeof item === "string" && item.trim().length > 0)) {
@@ -399,13 +577,6 @@ function updatePlanModeSettings(patch, options = {}) {
399
577
  const current = await readSettingsDocumentForUpdate(settingsPath, legacySettingsPath);
400
578
  const updated = { ...current };
401
579
  if (patch.thinkingLevel !== void 0) updated.thinkingLevel = patch.thinkingLevel;
402
- if (patch.implementationModel === null) delete updated.implementationModel;
403
- else if (patch.implementationModel !== void 0) {
404
- const previous = isSettingsDocument(updated.implementationModel) ? updated.implementationModel : {};
405
- updated.implementationModel = { ...previous, ...patch.implementationModel };
406
- }
407
- if (patch.implementationThinkingLevel !== void 0)
408
- updated.implementationThinkingLevel = patch.implementationThinkingLevel;
409
580
  if (patch.defaultPlanTools === null) delete updated.defaultPlanTools;
410
581
  else if (patch.defaultPlanTools !== void 0) {
411
582
  updated.defaultPlanTools = [...patch.defaultPlanTools];
@@ -413,6 +584,17 @@ function updatePlanModeSettings(patch, options = {}) {
413
584
  if (patch.implementationPlanRetention !== void 0) {
414
585
  updated.implementationPlanRetention = patch.implementationPlanRetention;
415
586
  }
587
+ if (patch.defaultImplementationModel === null) delete updated.defaultImplementationModel;
588
+ else if (patch.defaultImplementationModel !== void 0) {
589
+ const model = normalizeImplementationModel(patch.defaultImplementationModel);
590
+ if (!model) throw invalidSettingsError(settingsPath, "invalid implementation model");
591
+ updated.defaultImplementationModel = model;
592
+ }
593
+ if (patch.defaultImplementationThinkingLevel === null) {
594
+ delete updated.defaultImplementationThinkingLevel;
595
+ } else if (patch.defaultImplementationThinkingLevel !== void 0) {
596
+ updated.defaultImplementationThinkingLevel = patch.defaultImplementationThinkingLevel;
597
+ }
416
598
  if (patch.defaultPlanExportPath === null) delete updated.defaultPlanExportPath;
417
599
  else if (patch.defaultPlanExportPath !== void 0) {
418
600
  updated.defaultPlanExportPath = patch.defaultPlanExportPath;
@@ -567,6 +749,12 @@ function configuredThinkingLevel(settings) {
567
749
  function configuredImplementationPlanRetention(settings) {
568
750
  return settings.implementationPlanRetention ?? "clear-on-start";
569
751
  }
752
+ function configuredImplementationModel(settings) {
753
+ return settings.defaultImplementationModel;
754
+ }
755
+ function configuredImplementationThinkingLevel(settings) {
756
+ return settings.defaultImplementationThinkingLevel;
757
+ }
570
758
  function configuredPlanExportPath(settings) {
571
759
  return settings.defaultPlanExportPath ?? DEFAULT_PLAN_EXPORT_PATH;
572
760
  }
@@ -574,240 +762,6 @@ function configuredPlanModeToggleShortcut(settings) {
574
762
  return settings.toggleShortcut;
575
763
  }
576
764
 
577
- // src/message-transform.ts
578
- var PLAN_CONTEXT_MESSAGE_TYPE = "plan-mode-context";
579
- var PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE = "plan-mode-implementation-context";
580
- var PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
581
- var PLAN_IMPLEMENTATION_HANDOFF_PREFIX = "Plan mode is now disabled. Full tool access is restored. Implement this proposed plan now:";
582
- var PLAN_HISTORY_IMPLEMENTATION_PROMPT = "Implement the plan.";
583
- var PROPOSED_PLAN_PATTERN = /^<proposed_plan>[\t ]*\r?\n([\s\S]*?)\r?\n<\/proposed_plan>[\t ]*$/gm;
584
- var PROPOSED_PLAN_BLOCK_PATTERN = /^<proposed_plan>[\t ]*\r?\n[\s\S]*?\r?\n<\/proposed_plan>[\t ]*$/gm;
585
- function parseProposedPlan(text) {
586
- const openingCount = text.match(/<proposed_plan>/gi)?.length ?? 0;
587
- const closingCount = text.match(/<\/proposed_plan>/gi)?.length ?? 0;
588
- if (openingCount === 0 && closingCount === 0) return { kind: "absent" };
589
- if (openingCount > 1 || closingCount > 1) return { kind: "multiple" };
590
- if (openingCount === 1 && closingCount === 0) return { kind: "unclosed" };
591
- if (openingCount !== 1 || closingCount !== 1) return { kind: "malformed" };
592
- const matches = Array.from(text.matchAll(PROPOSED_PLAN_PATTERN));
593
- if (matches.length !== 1) return { kind: "malformed" };
594
- const plan = matches[0]?.[1]?.trim() ?? "";
595
- return plan ? { kind: "valid", plan } : { kind: "empty" };
596
- }
597
- function invalidPlanMessage(kind) {
598
- const detail = {
599
- empty: "the block is empty",
600
- multiple: "more than one plan block was produced",
601
- malformed: "the tags must be on their own lines",
602
- unclosed: "the closing tag is missing"
603
- }[kind];
604
- return `Proposed plan is not ready: ${detail}. Continue Plan mode and produce one complete non-empty <proposed_plan> block.`;
605
- }
606
- function latestAssistantText(messages) {
607
- if (!Array.isArray(messages)) return "";
608
- for (const entry of [...messages].reverse()) {
609
- const message = entry?.message ?? entry;
610
- if (message?.role !== "assistant") continue;
611
- const text = messageText(message);
612
- if (text) return text;
613
- }
614
- return "";
615
- }
616
- function latestAssistantStopReason(messages) {
617
- if (!Array.isArray(messages)) return void 0;
618
- for (const entry of [...messages].reverse()) {
619
- const message = entry?.message ?? entry;
620
- if (message?.role === "assistant") return message.stopReason;
621
- }
622
- return void 0;
623
- }
624
- function messageTextContent(message) {
625
- return messageText(
626
- message?.message ?? message
627
- );
628
- }
629
- function messageContainsLegacyPlanModeContextArtifact(message) {
630
- return unwrapSessionMessage(message).customType === PLAN_CONTEXT_MESSAGE_TYPE;
631
- }
632
- function messageContainsPlanModeImplementationContextArtifact(message) {
633
- return unwrapSessionMessage(message).customType === PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE;
634
- }
635
- function injectActiveImplementationContext(messages, activeImplementation) {
636
- let foundCurrentContext = false;
637
- let foundCurrentHandoff = false;
638
- const expectedContext = activeImplementationContextContent(activeImplementation);
639
- const messagesWithoutStaleContext = messages.filter((message) => {
640
- if (messageContainsPlanModeImplementationContextArtifact(message)) {
641
- const candidate = unwrapSessionMessage(message);
642
- if (!foundCurrentContext && candidate.content === expectedContext) {
643
- foundCurrentContext = true;
644
- return true;
645
- }
646
- return false;
647
- }
648
- if (!messageContainsPlanModeImplementationHandoff(message)) return true;
649
- if (!foundCurrentHandoff && messageContainsExactPlanModeImplementationHandoff(message, activeImplementation.plan)) {
650
- foundCurrentHandoff = true;
651
- return true;
652
- }
653
- return false;
654
- });
655
- if (foundCurrentHandoff || foundCurrentContext) return messagesWithoutStaleContext;
656
- let insertionIndex = 0;
657
- while (isSummaryMessage(messagesWithoutStaleContext[insertionIndex])) insertionIndex += 1;
658
- const contextMessage = {
659
- role: "custom",
660
- customType: PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE,
661
- content: expectedContext,
662
- display: false,
663
- timestamp: activeImplementation.startedAt
664
- };
665
- return [
666
- ...messagesWithoutStaleContext.slice(0, insertionIndex),
667
- contextMessage,
668
- ...messagesWithoutStaleContext.slice(insertionIndex)
669
- ];
670
- }
671
- function activeImplementationContextContent(activeImplementation) {
672
- return `[ACTIVE IMPLEMENTATION PLAN]
673
-
674
- The user approved the exact implementation plan below. Continue following it until the user explicitly clears or supersedes it. The exact plan is the remainder of this message:
675
-
676
- ${activeImplementation.plan}`;
677
- }
678
- function messageContainsInactivePlanModeArtifact(message) {
679
- const candidate = unwrapSessionMessage(message);
680
- return candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE || candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME;
681
- }
682
- function findHistoryImplementationArtifact(messages) {
683
- for (let index = messages.length - 1; index >= 0; index -= 1) {
684
- const candidate = unwrapSessionMessage(messages[index]);
685
- if (candidate.role !== "user" || contentText(candidate.content).trim() !== PLAN_HISTORY_IMPLEMENTATION_PROMPT) {
686
- continue;
687
- }
688
- const artifact = findHistoryImplementationArtifactBeforeKickoff(messages, index);
689
- if (artifact) return artifact;
690
- }
691
- return void 0;
692
- }
693
- function findHistoryImplementationArtifactBeforeKickoff(messages, kickoffIndex) {
694
- for (let index = kickoffIndex - 1; index >= 0; index -= 1) {
695
- const candidate = unwrapSessionMessage(messages[index]);
696
- if (candidate.role === "user" || isSummaryMessage(messages[index])) return void 0;
697
- if (candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME) {
698
- const toolCallId = candidate.toolCallId;
699
- const toolCallMessageIndex = toolCallId ? findPlanModeCompletionCallMessageIndex(messages, index, toolCallId) : void 0;
700
- if (toolCallId && toolCallMessageIndex !== void 0) {
701
- return {
702
- messageIndex: index,
703
- kind: "completion",
704
- toolCallId,
705
- toolCallMessageIndex
706
- };
707
- }
708
- continue;
709
- }
710
- if (candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE) {
711
- return { messageIndex: index, kind: "presentation" };
712
- }
713
- if (candidate.role === "assistant" && contentText(candidate.content).match(PROPOSED_PLAN_BLOCK_PATTERN)) {
714
- return { messageIndex: index, kind: "legacy" };
715
- }
716
- }
717
- return void 0;
718
- }
719
- function findPlanModeCompletionCallMessageIndex(messages, endIndex, toolCallId) {
720
- for (let index = endIndex - 1; index >= 0; index -= 1) {
721
- const candidate = unwrapSessionMessage(messages[index]);
722
- if (candidate.role === "user" || isSummaryMessage(messages[index])) return void 0;
723
- if (!Array.isArray(candidate.content)) continue;
724
- if (candidate.content.some((block) => {
725
- const toolCall = block;
726
- return toolCall.type === "toolCall" && toolCall.id === toolCallId && toolCall.name === PLAN_MODE_COMPLETE_TOOL_NAME;
727
- })) {
728
- return index;
729
- }
730
- }
731
- return void 0;
732
- }
733
- function messageContainsPlanModeImplementationHandoff(message) {
734
- const candidate = unwrapSessionMessage(message);
735
- return candidate.role === "user" && contentText(candidate.content).trimStart().startsWith(PLAN_IMPLEMENTATION_HANDOFF_PREFIX);
736
- }
737
- function messageContainsExactPlanModeImplementationHandoff(message, plan) {
738
- const candidate = unwrapSessionMessage(message);
739
- if (candidate.role !== "user") return false;
740
- return contentText(candidate.content).trim() === `${PLAN_IMPLEMENTATION_HANDOFF_PREFIX}
741
-
742
- ${plan}`.trim();
743
- }
744
- function isSummaryMessage(message) {
745
- const role = unwrapSessionMessage(message)?.role;
746
- return role === "compactionSummary" || role === "branchSummary";
747
- }
748
- function stripProposedPlanBlocksFromMessage(message) {
749
- return replaceAssistantContent(message, stripProposedPlanBlocksFromContent);
750
- }
751
- function stripPlanModeCompletionCallsFromMessage(message, preservedToolCallId) {
752
- return replaceAssistantContent(message, (content) => {
753
- if (!Array.isArray(content)) return content;
754
- const nextContent = content.filter((block) => {
755
- const candidate = block;
756
- return !(candidate.type === "toolCall" && candidate.name === PLAN_MODE_COMPLETE_TOOL_NAME && (!preservedToolCallId || candidate.id !== preservedToolCallId));
757
- });
758
- return nextContent.length === content.length ? content : nextContent;
759
- });
760
- }
761
- function isEmptyAssistantMessage(message) {
762
- const candidate = unwrapSessionMessage(message);
763
- return candidate.role === "assistant" && Array.isArray(candidate.content) && candidate.content.length === 0;
764
- }
765
- function replaceAssistantContent(message, transform) {
766
- const candidate = unwrapSessionMessage(message);
767
- if (candidate.role !== "assistant") return message;
768
- const content = transform(candidate.content);
769
- if (content === candidate.content) return message;
770
- if (isSessionMessageEntry(message)) {
771
- return { ...message, message: { ...candidate, content } };
772
- }
773
- return { ...candidate, content };
774
- }
775
- function unwrapSessionMessage(message) {
776
- const entry = message;
777
- return entry?.message ?? message ?? {};
778
- }
779
- function isSessionMessageEntry(message) {
780
- return typeof message === "object" && message !== null && "message" in message;
781
- }
782
- function stripProposedPlanBlocksFromContent(content) {
783
- if (typeof content === "string") return stripProposedPlanBlocks(content);
784
- if (!Array.isArray(content)) return content;
785
- let changed = false;
786
- const nextContent = content.map((block) => {
787
- const textBlock = block;
788
- if (textBlock.type !== "text" || typeof textBlock.text !== "string") return block;
789
- const text = stripProposedPlanBlocks(textBlock.text);
790
- if (text === textBlock.text) return block;
791
- changed = true;
792
- return { ...textBlock, text };
793
- });
794
- return changed ? nextContent : content;
795
- }
796
- function stripProposedPlanBlocks(text) {
797
- return text.replace(PROPOSED_PLAN_BLOCK_PATTERN, "");
798
- }
799
- function messageText(message) {
800
- return contentText(message.content);
801
- }
802
- function contentText(content) {
803
- if (typeof content === "string") return content;
804
- if (!Array.isArray(content)) return "";
805
- return content.map((block) => {
806
- const textBlock = block;
807
- return textBlock.type === "text" && typeof textBlock.text === "string" ? textBlock.text : "";
808
- }).filter(Boolean).join("\n");
809
- }
810
-
811
765
  // src/implementation-retention.ts
812
766
  function retentionLabel(retention) {
813
767
  return {
@@ -1891,31 +1845,30 @@ export {
1891
1845
  planFromCompletionDetails,
1892
1846
  planModeCompleted,
1893
1847
  renderPlanModeCompletion,
1894
- observeImplementationModelSelections,
1895
- hasImplementationPreferences,
1896
- sameModel,
1897
- safeImplementationText,
1898
- preferenceError,
1899
- preflightImplementationPreferences,
1900
- createImplementationPreferenceChange,
1848
+ PLAN_HISTORY_IMPLEMENTATION_PROMPT,
1849
+ parseProposedPlan,
1850
+ invalidPlanMessage,
1851
+ latestAssistantText,
1852
+ latestAssistantStopReason,
1853
+ messageTextContent,
1854
+ MAX_PENDING_IMPLEMENTATION_MODEL_IDENTIFIER_LENGTH,
1855
+ isPendingImplementationModelIdentifier,
1856
+ snapshotAvailableImplementationModels,
1857
+ findAvailableImplementationModel,
1901
1858
  PLAN_MODE_THINKING_LEVELS,
1859
+ IMPLEMENTATION_THINKING_LEVELS,
1902
1860
  IMPLEMENTATION_PLAN_RETENTIONS,
1903
1861
  planModeSettingsPath,
1904
- normalizePlanModeSettings,
1905
1862
  normalizeKeyId,
1906
1863
  readPlanModeSettings,
1907
1864
  updatePlanModeSettings,
1908
1865
  awaitPlanModeSettingsWrites,
1909
1866
  configuredThinkingLevel,
1910
1867
  configuredImplementationPlanRetention,
1868
+ configuredImplementationModel,
1869
+ configuredImplementationThinkingLevel,
1911
1870
  configuredPlanExportPath,
1912
1871
  configuredPlanModeToggleShortcut,
1913
- PLAN_HISTORY_IMPLEMENTATION_PROMPT,
1914
- parseProposedPlan,
1915
- invalidPlanMessage,
1916
- latestAssistantText,
1917
- latestAssistantStopReason,
1918
- messageTextContent,
1919
1872
  retentionLabel,
1920
1873
  implementationRetentionPreview,
1921
1874
  createImplementationRetentionCoordinator,
@@ -1936,4 +1889,4 @@ export {
1936
1889
  defaultPlanModeToolNames,
1937
1890
  snapshotPlanModeSelectedNames
1938
1891
  };
1939
- //# sourceMappingURL=chunk-YUFPCQVG.js.map
1892
+ //# sourceMappingURL=chunk-QHHHBTDO.js.map