( messages: Message[], cwd: string, maxSize: number = ASK_READ_FILE_STATE_CACHE_SIZE, )
| 386 | |
| 387 | // Create a function to extract read files from messages |
| 388 | export function extractReadFilesFromMessages( |
| 389 | messages: Message[], |
| 390 | cwd: string, |
| 391 | maxSize: number = ASK_READ_FILE_STATE_CACHE_SIZE, |
| 392 | ): FileStateCache { |
| 393 | const cache = createFileStateCacheWithSizeLimit(maxSize) |
| 394 | |
| 395 | // First pass: find all FileReadTool/FileWriteTool/FileEditTool uses in assistant messages |
| 396 | const fileReadToolUseIds = new Map<string, string>() // toolUseId -> filePath |
| 397 | const fileWriteToolUseIds = new Map< |
| 398 | string, |
| 399 | { filePath: string; content: string } |
| 400 | >() // toolUseId -> { filePath, content } |
| 401 | const fileEditToolUseIds = new Map<string, string>() // toolUseId -> filePath |
| 402 | |
| 403 | for (const message of messages) { |
| 404 | if ( |
| 405 | message.type === 'assistant' && |
| 406 | Array.isArray(message.message!.content) |
| 407 | ) { |
| 408 | for (const content of message.message!.content) { |
| 409 | if ( |
| 410 | content.type === 'tool_use' && |
| 411 | content.name === FILE_READ_TOOL_NAME |
| 412 | ) { |
| 413 | // Extract file_path from the tool use input |
| 414 | const input = content.input as FileReadInput | undefined |
| 415 | // Ranged reads are not added to the cache. |
| 416 | if ( |
| 417 | input?.file_path && |
| 418 | input?.offset === undefined && |
| 419 | input?.limit === undefined |
| 420 | ) { |
| 421 | // Normalize to absolute path for consistent cache lookups |
| 422 | const absolutePath = expandPath(input.file_path, cwd) |
| 423 | fileReadToolUseIds.set(content.id, absolutePath) |
| 424 | } |
| 425 | } else if ( |
| 426 | content.type === 'tool_use' && |
| 427 | content.name === FILE_WRITE_TOOL_NAME |
| 428 | ) { |
| 429 | // Extract file_path and content from the Write tool use input |
| 430 | const input = content.input as |
| 431 | | { file_path?: string; content?: unknown } |
| 432 | | undefined |
| 433 | if ( |
| 434 | input?.file_path && |
| 435 | input.content !== undefined && |
| 436 | input.content !== null |
| 437 | ) { |
| 438 | // Normalize to absolute path for consistent cache lookups |
| 439 | const absolutePath = expandPath(input.file_path, cwd) |
| 440 | fileWriteToolUseIds.set(content.id, { |
| 441 | filePath: absolutePath, |
| 442 | content: coerceToolContentToString(input.content), |
| 443 | }) |
| 444 | } |
| 445 | } else if ( |
no test coverage detected