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