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

Function getRelevantMemoryAttachments

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

Source from the content-addressed store, hash-verified

2246}
2247
2248async function getRelevantMemoryAttachments(
2249 input: string,
2250 agents: AgentDefinition[],
2251 readFileState: FileStateCache,
2252 recentTools: readonly string[],
2253 signal: AbortSignal,
2254 alreadySurfaced: ReadonlySet<string>,
2255 parentSpan?: unknown,
2256): Promise<Attachment[]> {
2257 // If an agent is @-mentioned, search only its memory dir (isolation).
2258 // Otherwise search the auto-memory dir.
2259 const memoryDirs = extractAgentMentions(input).flatMap(mention => {
2260 const agentType = mention.replace('agent-', '')
2261 const agentDef = agents.find(def => def.agentType === agentType)
2262 return agentDef?.memory
2263 ? [getAgentMemoryDir(agentType, agentDef.memory)]
2264 : []
2265 })
2266 const dirs = memoryDirs.length > 0 ? memoryDirs : [getAutoMemPath()]
2267
2268 const allResults = await Promise.all(
2269 dirs.map(dir =>
2270 findRelevantMemories(
2271 input,
2272 dir,
2273 signal,
2274 recentTools,
2275 alreadySurfaced,
2276 parentSpan as Parameters<typeof findRelevantMemories>[5],
2277 ).catch(() => []),
2278 ),
2279 )
2280 // alreadySurfaced is filtered inside the selector so Sonnet spends its
2281 // 5-slot budget on fresh candidates; readFileState catches files the
2282 // model read via FileReadTool. The redundant alreadySurfaced check here
2283 // is a belt-and-suspenders guard (multi-dir results may re-introduce a
2284 // path the selector filtered in a different dir).
2285 const selected = allResults
2286 .flat()
2287 .filter(m => !readFileState.has(m.path) && !alreadySurfaced.has(m.path))
2288 .slice(0, 5)
2289
2290 const memories = await readMemoriesForSurfacing(selected, signal)
2291
2292 if (memories.length === 0) {
2293 return []
2294 }
2295 return [{ type: 'relevant_memories' as const, memories }]
2296}
2297
2298/**
2299 * 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