@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.
- package/README.md +68 -4
- package/dist/chunks/{chunk-57OBPS7P.js → chunk-HF7ZEYIC.js} +169 -164
- package/dist/chunks/chunk-HF7ZEYIC.js.map +7 -0
- package/dist/chunks/{interactive-ui-N2ZKBVNE.js → interactive-ui-UK7IGPOE.js} +312 -32
- package/dist/chunks/interactive-ui-UK7IGPOE.js.map +7 -0
- package/dist/index.ts +670 -212
- package/dist/index.ts.map +4 -4
- package/docs/settings.md +26 -2
- package/package.json +50 -51
- package/src/active-implementation-menu.ts +57 -60
- package/src/command.ts +22 -21
- package/src/completion-tool.ts +53 -56
- package/src/extension-runtime.ts +5 -5
- package/src/finalization-request.ts +40 -44
- package/src/fresh-handoff-coordinator.ts +52 -0
- package/src/fresh-implementation.ts +312 -205
- package/src/implementation-models.ts +43 -0
- package/src/implementation-retention.ts +109 -123
- package/src/message-transform.ts +218 -252
- package/src/mode-contract.ts +46 -49
- package/src/plan-action-controller.ts +139 -89
- package/src/plan-action-menus.ts +414 -177
- package/src/plan-export-controller.ts +23 -28
- package/src/plan-export-screen.ts +13 -13
- package/src/plan-export.ts +85 -102
- package/src/plan-launch-menu.ts +119 -119
- package/src/plan-mode.ts +2027 -1675
- package/src/presentation.ts +84 -108
- package/src/prompt.ts +1 -1
- package/src/question-tool.ts +239 -267
- package/src/required-tools.ts +15 -19
- package/src/saved-plan-menu.ts +82 -84
- package/src/saved-plan-preflight.ts +29 -33
- package/src/settings-menu.ts +489 -377
- package/src/settings.ts +403 -382
- package/src/state.ts +177 -146
- package/src/tool-policy.ts +634 -699
- package/src/tool-selection.ts +39 -44
- package/src/workflow-mutex.ts +66 -66
- package/dist/chunks/chunk-57OBPS7P.js.map +0 -7
- package/dist/chunks/interactive-ui-N2ZKBVNE.js.map +0 -7
package/src/message-transform.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
role?: string;
|
|
23
|
+
content?: unknown;
|
|
24
|
+
stopReason?: string;
|
|
27
25
|
};
|
|
28
26
|
|
|
29
27
|
type TextBlock = {
|
|
30
|
-
|
|
31
|
-
|
|
28
|
+
type?: string;
|
|
29
|
+
text?: string;
|
|
32
30
|
};
|
|
33
31
|
|
|
34
32
|
export function parseProposedPlan(text: string): ProposedPlanParseResult {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
-
|
|
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
|
-
|
|
86
|
+
return unwrapSessionMessage(message).customType === PLAN_CONTEXT_MESSAGE_TYPE;
|
|
91
87
|
}
|
|
92
88
|
|
|
93
89
|
export function messageContainsPlanModeImplementationContextArtifact(message: unknown) {
|
|
94
|
-
|
|
90
|
+
return unwrapSessionMessage(message).customType === PLAN_IMPLEMENTATION_CONTEXT_MESSAGE_TYPE;
|
|
95
91
|
}
|
|
96
92
|
|
|
97
|
-
export function injectActiveImplementationContext(
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
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
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
181
|
-
|
|
165
|
+
messages: unknown[],
|
|
166
|
+
kickoffIndex: number,
|
|
182
167
|
): HistoryImplementationArtifact | undefined {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
)
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
258
|
-
|
|
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
|
-
|
|
235
|
+
return replaceAssistantContent(message, stripProposedPlanBlocksFromContent);
|
|
263
236
|
}
|
|
264
237
|
|
|
265
|
-
export function stripPlanModeCompletionCallsFromMessage<T>(
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
)
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
285
|
-
|
|
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
|
-
|
|
294
|
-
|
|
259
|
+
const candidate = unwrapSessionMessage(message);
|
|
260
|
+
if (candidate.role !== "assistant") return message;
|
|
295
261
|
|
|
296
|
-
|
|
297
|
-
|
|
262
|
+
const content = transform(candidate.content);
|
|
263
|
+
if (content === candidate.content) return message;
|
|
298
264
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
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
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
-
|
|
283
|
+
return typeof message === "object" && message !== null && "message" in message;
|
|
318
284
|
}
|
|
319
285
|
|
|
320
286
|
function stripProposedPlanBlocksFromContent(content: unknown) {
|
|
321
|
-
|
|
322
|
-
|
|
287
|
+
if (typeof content === "string") return stripProposedPlanBlocks(content);
|
|
288
|
+
if (!Array.isArray(content)) return content;
|
|
323
289
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
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
|
-
|
|
330
|
-
|
|
295
|
+
const text = stripProposedPlanBlocks(textBlock.text);
|
|
296
|
+
if (text === textBlock.text) return block;
|
|
331
297
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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
|
-
|
|
305
|
+
return text.replace(PROPOSED_PLAN_BLOCK_PATTERN, "");
|
|
340
306
|
}
|
|
341
307
|
|
|
342
308
|
function messageText(message: SessionMessage) {
|
|
343
|
-
|
|
309
|
+
return contentText(message.content);
|
|
344
310
|
}
|
|
345
311
|
|
|
346
312
|
function contentText(content: unknown): string {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
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
|
}
|