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

Function getRelevantMemoryAttachments

src/utils/attachments.ts:2203–2249  ·  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

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