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

Function getRelevantMemoryAttachments

src/utils/attachments.ts:2196–2242  ·  view source on GitHub ↗
(
  input: string,
  agents: AgentDefinition[],
  readFileState: FileStateCache,
  recentTools: readonly string[],
  signal: AbortSignal,
  alreadySurfaced: ReadonlySet<string>,
)

Source from the content-addressed store, hash-verified

2194}
2195
2196async function getRelevantMemoryAttachments(
2197 input: string,
2198 agents: AgentDefinition[],
2199 readFileState: FileStateCache,
2200 recentTools: readonly string[],
2201 signal: AbortSignal,
2202 alreadySurfaced: ReadonlySet<string>,
2203): Promise<Attachment[]> {
2204 // If an agent is @-mentioned, search only its memory dir (isolation).
2205 // Otherwise search the auto-memory dir.
2206 const memoryDirs = extractAgentMentions(input).flatMap(mention => {
2207 const agentType = mention.replace('agent-', '')
2208 const agentDef = agents.find(def => def.agentType === agentType)
2209 return agentDef?.memory
2210 ? [getAgentMemoryDir(agentType, agentDef.memory)]
2211 : []
2212 })
2213 const dirs = memoryDirs.length > 0 ? memoryDirs : [getAutoMemPath()]
2214
2215 const allResults = await Promise.all(
2216 dirs.map(dir =>
2217 findRelevantMemories(
2218 input,
2219 dir,
2220 signal,
2221 recentTools,
2222 alreadySurfaced,
2223 ).catch(() => []),
2224 ),
2225 )
2226 // alreadySurfaced is filtered inside the selector so Sonnet spends its
2227 // 5-slot budget on fresh candidates; readFileState catches files the
2228 // model read via FileReadTool. The redundant alreadySurfaced check here
2229 // is a belt-and-suspenders guard (multi-dir results may re-introduce a
2230 // path the selector filtered in a different dir).
2231 const selected = allResults
2232 .flat()
2233 .filter(m => !readFileState.has(m.path) && !alreadySurfaced.has(m.path))
2234 .slice(0, 5)
2235
2236 const memories = await readMemoriesForSurfacing(selected, signal)
2237
2238 if (memories.length === 0) {
2239 return []
2240 }
2241 return [{ type: 'relevant_memories' as const, memories }]
2242}
2243
2244/**
2245 * Scan messages for past relevant_memories attachments. Returns both the

Callers 1

Calls 5

extractAgentMentionsFunction · 0.85
getAgentMemoryDirFunction · 0.85
findRelevantMemoriesFunction · 0.85
readMemoriesForSurfacingFunction · 0.85
hasMethod · 0.45

Tested by

no test coverage detected