MCPcopy Create free account
hub / github.com/Noumena-Network/code / extractReadFilesFromMessages

Function extractReadFilesFromMessages

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

Source from the content-addressed store, hash-verified

339
340// Create a function to extract read files from messages
341export 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.

Callers 4

REPLFunction · 0.85
runHeadlessStreamingFunction · 0.85
handleSpeculationAcceptFunction · 0.85

Calls 7

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

Tested by

no test coverage detected