* Recover plan content from the message history. Plan content can appear in * three forms depending on what happened during the session: * * 1. ExitPlanMode tool_use input — normalizeToolInput injects the plan content * into the tool_use input, which persists in the transcript. * * 2. planC
(log: LogOption)
| 277 | * plan across compaction boundaries. |
| 278 | */ |
| 279 | function recoverPlanFromMessages(log: LogOption): string | null { |
| 280 | for (let i = log.messages.length - 1; i >= 0; i--) { |
| 281 | const msg = log.messages[i] |
| 282 | if (!msg) { |
| 283 | continue |
| 284 | } |
| 285 | |
| 286 | if (msg.type === 'assistant') { |
| 287 | const { content } = (msg as AssistantMessage).message |
| 288 | if (Array.isArray(content)) { |
| 289 | for (const block of content) { |
| 290 | if ( |
| 291 | block.type === 'tool_use' && |
| 292 | block.name === EXIT_PLAN_MODE_V2_TOOL_NAME |
| 293 | ) { |
| 294 | const input = block.input as Record<string, unknown> | undefined |
| 295 | const plan = input?.plan |
| 296 | if (typeof plan === 'string' && plan.length > 0) { |
| 297 | return plan |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | if (msg.type === 'user') { |
| 305 | const userMsg = msg as UserMessage |
| 306 | if ( |
| 307 | typeof userMsg.planContent === 'string' && |
| 308 | userMsg.planContent.length > 0 |
| 309 | ) { |
| 310 | return userMsg.planContent |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (msg.type === 'attachment') { |
| 315 | const attachmentMsg = msg as AttachmentMessage |
| 316 | if (attachmentMsg.attachment?.type === 'plan_file_reference') { |
| 317 | const plan = (attachmentMsg.attachment as { planContent?: string }) |
| 318 | .planContent |
| 319 | if (typeof plan === 'string' && plan.length > 0) { |
| 320 | return plan |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | return null |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Find a file entry in the most recent file-snapshot system message in the transcript. |