@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
@@ -5,352 +5,318 @@ const PLAN_CONTEXT_MESSAGE_TYPE = "plan-mode-context";
5
5
  export const PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE = "plan-mode-implementation-context";
6
6
  const PROPOSED_PLAN_MESSAGE_TYPE = "proposed-plan";
7
7
  const PLAN_IMPLEMENTATION_HANDOFF_PREFIX =
8
- "Plan mode is now disabled. Full tool access is restored. Implement this proposed plan now:";
8
+ "Plan mode is now disabled. Full tool access is restored. Implement this proposed plan now:";
9
9
  export const PLAN_HISTORY_IMPLEMENTATION_PROMPT = "Implement the plan.";
10
- const PROPOSED_PLAN_PATTERN =
11
- /^<proposed_plan>[\t ]*\r?\n([\s\S]*?)\r?\n<\/proposed_plan>[\t ]*$/gm;
12
- const PROPOSED_PLAN_BLOCK_PATTERN =
13
- /^<proposed_plan>[\t ]*\r?\n[\s\S]*?\r?\n<\/proposed_plan>[\t ]*$/gm;
10
+ const PROPOSED_PLAN_PATTERN = /^<proposed_plan>[\t ]*\r?\n([\s\S]*?)\r?\n<\/proposed_plan>[\t ]*$/gm;
11
+ const PROPOSED_PLAN_BLOCK_PATTERN = /^<proposed_plan>[\t ]*\r?\n[\s\S]*?\r?\n<\/proposed_plan>[\t ]*$/gm;
14
12
 
15
13
  export type ProposedPlanParseResult =
16
- | { kind: "absent" }
17
- | { kind: "valid"; plan: string }
18
- | { kind: "empty" }
19
- | { kind: "multiple" }
20
- | { kind: "malformed" }
21
- | { kind: "unclosed" };
14
+ | { kind: "absent" }
15
+ | { kind: "valid"; plan: string }
16
+ | { kind: "empty" }
17
+ | { kind: "multiple" }
18
+ | { kind: "malformed" }
19
+ | { kind: "unclosed" };
22
20
 
23
21
  type SessionMessage = {
24
- role?: string;
25
- content?: unknown;
26
- stopReason?: string;
22
+ role?: string;
23
+ content?: unknown;
24
+ stopReason?: string;
27
25
  };
28
26
 
29
27
  type TextBlock = {
30
- type?: string;
31
- text?: string;
28
+ type?: string;
29
+ text?: string;
32
30
  };
33
31
 
34
32
  export function parseProposedPlan(text: string): ProposedPlanParseResult {
35
- const openingCount = text.match(/<proposed_plan>/gi)?.length ?? 0;
36
- const closingCount = text.match(/<\/proposed_plan>/gi)?.length ?? 0;
37
- if (openingCount === 0 && closingCount === 0) return { kind: "absent" };
38
- if (openingCount > 1 || closingCount > 1) return { kind: "multiple" };
39
- if (openingCount === 1 && closingCount === 0) return { kind: "unclosed" };
40
- if (openingCount !== 1 || closingCount !== 1) return { kind: "malformed" };
41
-
42
- const matches = Array.from(text.matchAll(PROPOSED_PLAN_PATTERN));
43
- if (matches.length !== 1) return { kind: "malformed" };
44
- const plan = matches[0]?.[1]?.trim() ?? "";
45
- return plan ? { kind: "valid", plan } : { kind: "empty" };
33
+ const openingCount = text.match(/<proposed_plan>/gi)?.length ?? 0;
34
+ const closingCount = text.match(/<\/proposed_plan>/gi)?.length ?? 0;
35
+ if (openingCount === 0 && closingCount === 0) return { kind: "absent" };
36
+ if (openingCount > 1 || closingCount > 1) return { kind: "multiple" };
37
+ if (openingCount === 1 && closingCount === 0) return { kind: "unclosed" };
38
+ if (openingCount !== 1 || closingCount !== 1) return { kind: "malformed" };
39
+
40
+ const matches = Array.from(text.matchAll(PROPOSED_PLAN_PATTERN));
41
+ if (matches.length !== 1) return { kind: "malformed" };
42
+ const plan = matches[0]?.[1]?.trim() ?? "";
43
+ return plan ? { kind: "valid", plan } : { kind: "empty" };
46
44
  }
47
45
 
48
46
  export function extractProposedPlan(text: string) {
49
- const result = parseProposedPlan(text);
50
- return result.kind === "valid" ? result.plan : undefined;
47
+ const result = parseProposedPlan(text);
48
+ return result.kind === "valid" ? result.plan : undefined;
51
49
  }
52
50
 
53
51
  export function invalidPlanMessage(kind: "empty" | "multiple" | "malformed" | "unclosed") {
54
- const detail = {
55
- empty: "the block is empty",
56
- multiple: "more than one plan block was produced",
57
- malformed: "the tags must be on their own lines",
58
- unclosed: "the closing tag is missing",
59
- }[kind];
60
- return `Proposed plan is not ready: ${detail}. Continue Plan mode and produce one complete non-empty <proposed_plan> block.`;
52
+ const detail = {
53
+ empty: "the block is empty",
54
+ multiple: "more than one plan block was produced",
55
+ malformed: "the tags must be on their own lines",
56
+ unclosed: "the closing tag is missing",
57
+ }[kind];
58
+ return `Proposed plan is not ready: ${detail}. Continue Plan mode and produce one complete non-empty <proposed_plan> block.`;
61
59
  }
62
60
 
63
61
  export function latestAssistantText(messages: unknown) {
64
- if (!Array.isArray(messages)) return "";
65
- for (const entry of [...messages].reverse()) {
66
- const message = (entry as { message?: SessionMessage })?.message ?? (entry as SessionMessage);
67
- if (message?.role !== "assistant") continue;
68
- const text = messageText(message);
69
- if (text) return text;
70
- }
71
- return "";
62
+ if (!Array.isArray(messages)) return "";
63
+ for (const entry of [...messages].reverse()) {
64
+ const message = (entry as { message?: SessionMessage })?.message ?? (entry as SessionMessage);
65
+ if (message?.role !== "assistant") continue;
66
+ const text = messageText(message);
67
+ if (text) return text;
68
+ }
69
+ return "";
72
70
  }
73
71
 
74
72
  export function latestAssistantStopReason(messages: unknown) {
75
- if (!Array.isArray(messages)) return undefined;
76
- for (const entry of [...messages].reverse()) {
77
- const message = (entry as { message?: SessionMessage })?.message ?? (entry as SessionMessage);
78
- if (message?.role === "assistant") return message.stopReason;
79
- }
80
- return undefined;
73
+ if (!Array.isArray(messages)) return undefined;
74
+ for (const entry of [...messages].reverse()) {
75
+ const message = (entry as { message?: SessionMessage })?.message ?? (entry as SessionMessage);
76
+ if (message?.role === "assistant") return message.stopReason;
77
+ }
78
+ return undefined;
81
79
  }
82
80
 
83
81
  export function messageTextContent(message: unknown) {
84
- return messageText(
85
- (message as { message?: SessionMessage })?.message ?? (message as SessionMessage),
86
- );
82
+ return messageText((message as { message?: SessionMessage })?.message ?? (message as SessionMessage));
87
83
  }
88
84
 
89
85
  export function messageContainsLegacyPlanModeContextArtifact(message: unknown) {
90
- return unwrapSessionMessage(message).customType === PLAN_CONTEXT_MESSAGE_TYPE;
86
+ return unwrapSessionMessage(message).customType === PLAN_CONTEXT_MESSAGE_TYPE;
91
87
  }
92
88
 
93
89
  export function messageContainsPlanModeImplementationContextArtifact(message: unknown) {
94
- return unwrapSessionMessage(message).customType === PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE;
90
+ return unwrapSessionMessage(message).customType === PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE;
95
91
  }
96
92
 
97
- export function injectActiveImplementationContext(
98
- messages: unknown[],
99
- activeImplementation: ActiveImplementationPlan,
100
- ) {
101
- let foundCurrentContext = false;
102
- let foundCurrentHandoff = false;
103
- const expectedContext = activeImplementationContextContent(activeImplementation);
104
- const messagesWithoutStaleContext = messages.filter((message) => {
105
- if (messageContainsPlanModeImplementationContextArtifact(message)) {
106
- const candidate = unwrapSessionMessage(message);
107
- if (!foundCurrentContext && candidate.content === expectedContext) {
108
- foundCurrentContext = true;
109
- return true;
110
- }
111
- return false;
112
- }
113
- if (!messageContainsPlanModeImplementationHandoff(message)) return true;
114
- if (
115
- !foundCurrentHandoff &&
116
- messageContainsExactPlanModeImplementationHandoff(message, activeImplementation.plan)
117
- ) {
118
- foundCurrentHandoff = true;
119
- return true;
120
- }
121
- return false;
122
- });
123
- if (foundCurrentHandoff || foundCurrentContext) return messagesWithoutStaleContext;
124
-
125
- let insertionIndex = 0;
126
- while (isSummaryMessage(messagesWithoutStaleContext[insertionIndex])) insertionIndex += 1;
127
- const contextMessage = {
128
- role: "custom" as const,
129
- customType: PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE,
130
- content: expectedContext,
131
- display: false,
132
- timestamp: activeImplementation.startedAt,
133
- };
134
- return [
135
- ...messagesWithoutStaleContext.slice(0, insertionIndex),
136
- contextMessage,
137
- ...messagesWithoutStaleContext.slice(insertionIndex),
138
- ];
93
+ export function injectActiveImplementationContext(messages: unknown[], activeImplementation: ActiveImplementationPlan) {
94
+ let foundCurrentContext = false;
95
+ let foundCurrentHandoff = false;
96
+ const expectedContext = activeImplementationContextContent(activeImplementation);
97
+ const messagesWithoutStaleContext = messages.filter((message) => {
98
+ if (messageContainsPlanModeImplementationContextArtifact(message)) {
99
+ const candidate = unwrapSessionMessage(message);
100
+ if (!foundCurrentContext && candidate.content === expectedContext) {
101
+ foundCurrentContext = true;
102
+ return true;
103
+ }
104
+ return false;
105
+ }
106
+ if (!messageContainsPlanModeImplementationHandoff(message)) return true;
107
+ if (!foundCurrentHandoff && messageContainsExactPlanModeImplementationHandoff(message, activeImplementation.plan)) {
108
+ foundCurrentHandoff = true;
109
+ return true;
110
+ }
111
+ return false;
112
+ });
113
+ if (foundCurrentHandoff || foundCurrentContext) return messagesWithoutStaleContext;
114
+
115
+ let insertionIndex = 0;
116
+ while (isSummaryMessage(messagesWithoutStaleContext[insertionIndex])) insertionIndex += 1;
117
+ const contextMessage = {
118
+ role: "custom" as const,
119
+ customType: PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE,
120
+ content: expectedContext,
121
+ display: false,
122
+ timestamp: activeImplementation.startedAt,
123
+ };
124
+ return [
125
+ ...messagesWithoutStaleContext.slice(0, insertionIndex),
126
+ contextMessage,
127
+ ...messagesWithoutStaleContext.slice(insertionIndex),
128
+ ];
139
129
  }
140
130
 
141
131
  function activeImplementationContextContent(activeImplementation: ActiveImplementationPlan) {
142
- return `[ACTIVE IMPLEMENTATION PLAN]\n\nThe 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:\n\n${activeImplementation.plan}`;
132
+ return `[ACTIVE IMPLEMENTATION PLAN]\n\nThe 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:\n\n${activeImplementation.plan}`;
143
133
  }
144
134
 
145
135
  export function messageContainsInactivePlanModeArtifact(message: unknown) {
146
- const candidate = unwrapSessionMessage(message);
147
- return (
148
- candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE ||
149
- (candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME)
150
- );
136
+ const candidate = unwrapSessionMessage(message);
137
+ return (
138
+ candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE ||
139
+ (candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME)
140
+ );
151
141
  }
152
142
 
153
143
  interface HistoryImplementationArtifact {
154
- messageIndex: number;
155
- kind: "completion" | "legacy" | "presentation";
156
- toolCallId?: string;
157
- toolCallMessageIndex?: number;
144
+ messageIndex: number;
145
+ kind: "completion" | "legacy" | "presentation";
146
+ toolCallId?: string;
147
+ toolCallMessageIndex?: number;
158
148
  }
159
149
 
160
150
  // The kickoff text is intentionally generic, so only trust a plan artifact in the same
161
151
  // uninterrupted user/summary segment and require a complete tool-call/result pair.
162
- export function findHistoryImplementationArtifact(
163
- messages: unknown[],
164
- ): HistoryImplementationArtifact | undefined {
165
- for (let index = messages.length - 1; index >= 0; index -= 1) {
166
- const candidate = unwrapSessionMessage(messages[index]);
167
- if (
168
- candidate.role !== "user" ||
169
- contentText(candidate.content).trim() !== PLAN_HISTORY_IMPLEMENTATION_PROMPT
170
- ) {
171
- continue;
172
- }
173
- const artifact = findHistoryImplementationArtifactBeforeKickoff(messages, index);
174
- if (artifact) return artifact;
175
- }
176
- return undefined;
152
+ export function findHistoryImplementationArtifact(messages: unknown[]): HistoryImplementationArtifact | undefined {
153
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
154
+ const candidate = unwrapSessionMessage(messages[index]);
155
+ if (candidate.role !== "user" || contentText(candidate.content).trim() !== PLAN_HISTORY_IMPLEMENTATION_PROMPT) {
156
+ continue;
157
+ }
158
+ const artifact = findHistoryImplementationArtifactBeforeKickoff(messages, index);
159
+ if (artifact) return artifact;
160
+ }
161
+ return undefined;
177
162
  }
178
163
 
179
164
  function findHistoryImplementationArtifactBeforeKickoff(
180
- messages: unknown[],
181
- kickoffIndex: number,
165
+ messages: unknown[],
166
+ kickoffIndex: number,
182
167
  ): HistoryImplementationArtifact | undefined {
183
- for (let index = kickoffIndex - 1; index >= 0; index -= 1) {
184
- const candidate = unwrapSessionMessage(messages[index]);
185
- if (candidate.role === "user" || isSummaryMessage(messages[index])) return undefined;
186
- if (candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME) {
187
- const toolCallId = candidate.toolCallId;
188
- const toolCallMessageIndex = toolCallId
189
- ? findPlanModeCompletionCallMessageIndex(messages, index, toolCallId)
190
- : undefined;
191
- if (toolCallId && toolCallMessageIndex !== undefined) {
192
- return {
193
- messageIndex: index,
194
- kind: "completion",
195
- toolCallId,
196
- toolCallMessageIndex,
197
- };
198
- }
199
- continue;
200
- }
201
- if (candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE) {
202
- return { messageIndex: index, kind: "presentation" };
203
- }
204
- if (
205
- candidate.role === "assistant" &&
206
- contentText(candidate.content).match(PROPOSED_PLAN_BLOCK_PATTERN)
207
- ) {
208
- return { messageIndex: index, kind: "legacy" };
209
- }
210
- }
211
- return undefined;
168
+ for (let index = kickoffIndex - 1; index >= 0; index -= 1) {
169
+ const candidate = unwrapSessionMessage(messages[index]);
170
+ if (candidate.role === "user" || isSummaryMessage(messages[index])) return undefined;
171
+ if (candidate.role === "toolResult" && candidate.toolName === PLAN_MODE_COMPLETE_TOOL_NAME) {
172
+ const toolCallId = candidate.toolCallId;
173
+ const toolCallMessageIndex = toolCallId
174
+ ? findPlanModeCompletionCallMessageIndex(messages, index, toolCallId)
175
+ : undefined;
176
+ if (toolCallId && toolCallMessageIndex !== undefined) {
177
+ return {
178
+ messageIndex: index,
179
+ kind: "completion",
180
+ toolCallId,
181
+ toolCallMessageIndex,
182
+ };
183
+ }
184
+ continue;
185
+ }
186
+ if (candidate.customType === PROPOSED_PLAN_MESSAGE_TYPE) {
187
+ return { messageIndex: index, kind: "presentation" };
188
+ }
189
+ if (candidate.role === "assistant" && contentText(candidate.content).match(PROPOSED_PLAN_BLOCK_PATTERN)) {
190
+ return { messageIndex: index, kind: "legacy" };
191
+ }
192
+ }
193
+ return undefined;
212
194
  }
213
195
 
214
- function findPlanModeCompletionCallMessageIndex(
215
- messages: unknown[],
216
- endIndex: number,
217
- toolCallId: string,
218
- ) {
219
- for (let index = endIndex - 1; index >= 0; index -= 1) {
220
- const candidate = unwrapSessionMessage(messages[index]);
221
- if (candidate.role === "user" || isSummaryMessage(messages[index])) return undefined;
222
- if (!Array.isArray(candidate.content)) continue;
223
- if (
224
- candidate.content.some((block) => {
225
- const toolCall = block as { type?: string; id?: string; name?: string };
226
- return (
227
- toolCall.type === "toolCall" &&
228
- toolCall.id === toolCallId &&
229
- toolCall.name === PLAN_MODE_COMPLETE_TOOL_NAME
230
- );
231
- })
232
- ) {
233
- return index;
234
- }
235
- }
236
- return undefined;
196
+ function findPlanModeCompletionCallMessageIndex(messages: unknown[], endIndex: number, toolCallId: string) {
197
+ for (let index = endIndex - 1; index >= 0; index -= 1) {
198
+ const candidate = unwrapSessionMessage(messages[index]);
199
+ if (candidate.role === "user" || isSummaryMessage(messages[index])) return undefined;
200
+ if (!Array.isArray(candidate.content)) continue;
201
+ if (
202
+ candidate.content.some((block) => {
203
+ const toolCall = block as { type?: string; id?: string; name?: string };
204
+ return (
205
+ toolCall.type === "toolCall" && toolCall.id === toolCallId && toolCall.name === PLAN_MODE_COMPLETE_TOOL_NAME
206
+ );
207
+ })
208
+ ) {
209
+ return index;
210
+ }
211
+ }
212
+ return undefined;
237
213
  }
238
214
 
239
215
  export function messageContainsPlanModeImplementationHandoff(message: unknown) {
240
- const candidate = unwrapSessionMessage(message);
241
- return (
242
- candidate.role === "user" &&
243
- contentText(candidate.content).trimStart().startsWith(PLAN_IMPLEMENTATION_HANDOFF_PREFIX)
244
- );
216
+ const candidate = unwrapSessionMessage(message);
217
+ return (
218
+ candidate.role === "user" &&
219
+ contentText(candidate.content).trimStart().startsWith(PLAN_IMPLEMENTATION_HANDOFF_PREFIX)
220
+ );
245
221
  }
246
222
 
247
223
  export function messageContainsExactPlanModeImplementationHandoff(message: unknown, plan: string) {
248
- const candidate = unwrapSessionMessage(message);
249
- if (candidate.role !== "user") return false;
250
- return (
251
- contentText(candidate.content).trim() ===
252
- `${PLAN_IMPLEMENTATION_HANDOFF_PREFIX}\n\n${plan}`.trim()
253
- );
224
+ const candidate = unwrapSessionMessage(message);
225
+ if (candidate.role !== "user") return false;
226
+ return contentText(candidate.content).trim() === `${PLAN_IMPLEMENTATION_HANDOFF_PREFIX}\n\n${plan}`.trim();
254
227
  }
255
228
 
256
229
  function isSummaryMessage(message: unknown) {
257
- const role = unwrapSessionMessage(message)?.role;
258
- return role === "compactionSummary" || role === "branchSummary";
230
+ const role = unwrapSessionMessage(message)?.role;
231
+ return role === "compactionSummary" || role === "branchSummary";
259
232
  }
260
233
 
261
234
  export function stripProposedPlanBlocksFromMessage<T>(message: T): T {
262
- return replaceAssistantContent(message, stripProposedPlanBlocksFromContent);
235
+ return replaceAssistantContent(message, stripProposedPlanBlocksFromContent);
263
236
  }
264
237
 
265
- export function stripPlanModeCompletionCallsFromMessage<T>(
266
- message: T,
267
- preservedToolCallId?: string,
268
- ): T {
269
- return replaceAssistantContent(message, (content) => {
270
- if (!Array.isArray(content)) return content;
271
- const nextContent = content.filter((block) => {
272
- const candidate = block as { type?: string; id?: string; name?: string };
273
- return !(
274
- candidate.type === "toolCall" &&
275
- candidate.name === PLAN_MODE_COMPLETE_TOOL_NAME &&
276
- (!preservedToolCallId || candidate.id !== preservedToolCallId)
277
- );
278
- });
279
- return nextContent.length === content.length ? content : nextContent;
280
- });
238
+ export function stripPlanModeCompletionCallsFromMessage<T>(message: T, preservedToolCallId?: string): T {
239
+ return replaceAssistantContent(message, (content) => {
240
+ if (!Array.isArray(content)) return content;
241
+ const nextContent = content.filter((block) => {
242
+ const candidate = block as { type?: string; id?: string; name?: string };
243
+ return !(
244
+ candidate.type === "toolCall" &&
245
+ candidate.name === PLAN_MODE_COMPLETE_TOOL_NAME &&
246
+ (!preservedToolCallId || candidate.id !== preservedToolCallId)
247
+ );
248
+ });
249
+ return nextContent.length === content.length ? content : nextContent;
250
+ });
281
251
  }
282
252
 
283
253
  export function isEmptyAssistantMessage(message: unknown) {
284
- const candidate = unwrapSessionMessage(message);
285
- return (
286
- candidate.role === "assistant" &&
287
- Array.isArray(candidate.content) &&
288
- candidate.content.length === 0
289
- );
254
+ const candidate = unwrapSessionMessage(message);
255
+ return candidate.role === "assistant" && Array.isArray(candidate.content) && candidate.content.length === 0;
290
256
  }
291
257
 
292
258
  function replaceAssistantContent<T>(message: T, transform: (content: unknown) => unknown): T {
293
- const candidate = unwrapSessionMessage(message);
294
- if (candidate.role !== "assistant") return message;
259
+ const candidate = unwrapSessionMessage(message);
260
+ if (candidate.role !== "assistant") return message;
295
261
 
296
- const content = transform(candidate.content);
297
- if (content === candidate.content) return message;
262
+ const content = transform(candidate.content);
263
+ if (content === candidate.content) return message;
298
264
 
299
- if (isSessionMessageEntry(message)) {
300
- return { ...message, message: { ...candidate, content } };
301
- }
302
- return { ...candidate, content } as T;
265
+ if (isSessionMessageEntry(message)) {
266
+ return { ...message, message: { ...candidate, content } };
267
+ }
268
+ return { ...candidate, content } as T;
303
269
  }
304
270
 
305
271
  function unwrapSessionMessage(message: unknown) {
306
- const entry = message as { message?: unknown } | null | undefined;
307
- return (entry?.message ?? message ?? {}) as {
308
- role?: string;
309
- customType?: string;
310
- toolName?: string;
311
- toolCallId?: string;
312
- content?: unknown;
313
- };
272
+ const entry = message as { message?: unknown } | null | undefined;
273
+ return (entry?.message ?? message ?? {}) as {
274
+ role?: string;
275
+ customType?: string;
276
+ toolName?: string;
277
+ toolCallId?: string;
278
+ content?: unknown;
279
+ };
314
280
  }
315
281
 
316
282
  function isSessionMessageEntry<T>(message: T): message is T & { message: SessionMessage } {
317
- return typeof message === "object" && message !== null && "message" in message;
283
+ return typeof message === "object" && message !== null && "message" in message;
318
284
  }
319
285
 
320
286
  function stripProposedPlanBlocksFromContent(content: unknown) {
321
- if (typeof content === "string") return stripProposedPlanBlocks(content);
322
- if (!Array.isArray(content)) return content;
287
+ if (typeof content === "string") return stripProposedPlanBlocks(content);
288
+ if (!Array.isArray(content)) return content;
323
289
 
324
- let changed = false;
325
- const nextContent = content.map((block) => {
326
- const textBlock = block as TextBlock;
327
- if (textBlock.type !== "text" || typeof textBlock.text !== "string") return block;
290
+ let changed = false;
291
+ const nextContent = content.map((block) => {
292
+ const textBlock = block as TextBlock;
293
+ if (textBlock.type !== "text" || typeof textBlock.text !== "string") return block;
328
294
 
329
- const text = stripProposedPlanBlocks(textBlock.text);
330
- if (text === textBlock.text) return block;
295
+ const text = stripProposedPlanBlocks(textBlock.text);
296
+ if (text === textBlock.text) return block;
331
297
 
332
- changed = true;
333
- return { ...textBlock, text };
334
- });
335
- return changed ? nextContent : content;
298
+ changed = true;
299
+ return { ...textBlock, text };
300
+ });
301
+ return changed ? nextContent : content;
336
302
  }
337
303
 
338
304
  export function stripProposedPlanBlocks(text: string) {
339
- return text.replace(PROPOSED_PLAN_BLOCK_PATTERN, "");
305
+ return text.replace(PROPOSED_PLAN_BLOCK_PATTERN, "");
340
306
  }
341
307
 
342
308
  function messageText(message: SessionMessage) {
343
- return contentText(message.content);
309
+ return contentText(message.content);
344
310
  }
345
311
 
346
312
  function contentText(content: unknown): string {
347
- if (typeof content === "string") return content;
348
- if (!Array.isArray(content)) return "";
349
- return content
350
- .map((block) => {
351
- const textBlock = block as TextBlock;
352
- return textBlock.type === "text" && typeof textBlock.text === "string" ? textBlock.text : "";
353
- })
354
- .filter(Boolean)
355
- .join("\n");
313
+ if (typeof content === "string") return content;
314
+ if (!Array.isArray(content)) return "";
315
+ return content
316
+ .map((block) => {
317
+ const textBlock = block as TextBlock;
318
+ return textBlock.type === "text" && typeof textBlock.text === "string" ? textBlock.text : "";
319
+ })
320
+ .filter(Boolean)
321
+ .join("\n");
356
322
  }