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

Function extractReadFilesFromMessages

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

Source from the content-addressed store, hash-verified

344
345// Create a function to extract read files from messages
346export 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.

Callers 3

REPLFunction · 0.85
runHeadlessStreamingFunction · 0.85
handleSpeculationAcceptFunction · 0.85

Calls 7

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

Tested by

no test coverage detected