MCPcopy
hub / github.com/claude-code-best/claude-code / extractReadFilesFromMessages

Function extractReadFilesFromMessages

src/utils/queryHelpers.ts:388–551  ·  view source on GitHub ↗
(
  messages: Message[],
  cwd: string,
  maxSize: number = ASK_READ_FILE_STATE_CACHE_SIZE,
)

Source from the content-addressed store, hash-verified

386
387// Create a function to extract read files from messages
388export 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 (

Callers 3

REPLFunction · 0.85
runHeadlessStreamingFunction · 0.85
handleSpeculationAcceptFunction · 0.85

Calls 8

expandPathFunction · 0.85
readFileSyncWithMetadataFunction · 0.85
getFileModificationTimeFunction · 0.85
isFsInaccessibleFunction · 0.85
setMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected