( messages: Message[], cwd: string, maxSize: number = ASK_READ_FILE_STATE_CACHE_SIZE, )
| 339 | |
| 340 | // Create a function to extract read files from messages |
| 341 | export function extractReadFilesFromMessages( |
| 342 | messages: Message[], |
| 343 | cwd: string, |
| 344 | maxSize: number = ASK_READ_FILE_STATE_CACHE_SIZE, |
| 345 | ): FileStateCache { |
| 346 | const cache = createFileStateCacheWithSizeLimit(maxSize) |
| 347 | |
| 348 | // First pass: find all FileReadTool/FileWriteTool/FileEditTool uses in assistant messages |
| 349 | const fileReadToolUseIds = new Map<string, string>() // toolUseId -> filePath |
| 350 | const fileWriteToolUseIds = new Map< |
| 351 | string, |
| 352 | { filePath: string; content: string } |
| 353 | >() // toolUseId -> { filePath, content } |
| 354 | const fileEditToolUseIds = new Map<string, string>() // toolUseId -> filePath |
| 355 | |
| 356 | for (const message of messages) { |
| 357 | if ( |
| 358 | message.type === 'assistant' && |
| 359 | Array.isArray(message.message.content) |
| 360 | ) { |
| 361 | for (const content of message.message.content) { |
| 362 | if ( |
| 363 | content.type === 'tool_use' && |
| 364 | content.name === FILE_READ_TOOL_NAME |
| 365 | ) { |
| 366 | // Extract file_path from the tool use input |
| 367 | const input = content.input as FileReadInput | undefined |
| 368 | // Ranged reads are not added to the cache. |
| 369 | if ( |
| 370 | input?.file_path && |
| 371 | input?.offset === undefined && |
| 372 | input?.limit === undefined |
| 373 | ) { |
| 374 | // Normalize to absolute path for consistent cache lookups |
| 375 | const absolutePath = expandPath(input.file_path, cwd) |
| 376 | fileReadToolUseIds.set(content.id, absolutePath) |
| 377 | } |
| 378 | } else if ( |
| 379 | content.type === 'tool_use' && |
| 380 | content.name === FILE_WRITE_TOOL_NAME |
| 381 | ) { |
| 382 | // Extract file_path and content from the Write tool use input |
| 383 | const input = content.input as |
| 384 | | { file_path?: string; content?: string } |
| 385 | | undefined |
| 386 | if (input?.file_path && typeof input.content === 'string') { |
| 387 | // Normalize to absolute path for consistent cache lookups |
| 388 | const absolutePath = expandPath(input.file_path, cwd) |
| 389 | fileWriteToolUseIds.set(content.id, { |
| 390 | filePath: absolutePath, |
| 391 | content: input.content, |
| 392 | }) |
| 393 | } |
| 394 | } else if ( |
| 395 | content.type === 'tool_use' && |
| 396 | content.name === FILE_EDIT_TOOL_NAME |
| 397 | ) { |
| 398 | // Edit's input has old_string/new_string, not the resulting content. |
no test coverage detected