MCPcopy Create free account
hub / github.com/Work-Fisher/code-claw / buildMemoryContext

Function buildMemoryContext

tools/claw-launcher-ui/memory.mjs:183–223  ·  view source on GitHub ↗
(workspaceDir, query, callLLM)

Source from the content-addressed store, hash-verified

181 * Orchestrates: index + relevant memories + daily logs.
182 */
183export async function buildMemoryContext(workspaceDir, query, callLLM) {
184 if (!workspaceDir) return ''
185
186 const parts = []
187
188 // 1. Memory index (always loaded)
189 const index = await loadMemoryIndex(workspaceDir)
190 if (index && index !== '# Memory Index\n\n(暂无记忆)\n') {
191 parts.push(`## 长期记忆索引\n${index}`)
192 }
193
194 // 2. Load memory files
195 const manifest = await scanMemoryManifest(workspaceDir)
196 if (manifest.length > 0) {
197 if (callLLM && manifest.length > 8) {
198 // Too many files — use LLM to pick the most relevant ones
199 const relevant = await findRelevantMemories(query, manifest, callLLM)
200 if (relevant.length > 0) {
201 const files = await loadMemoryFiles(workspaceDir, relevant)
202 for (const file of files) {
203 parts.push(`## 记忆: ${file.frontmatter.name || file.filename}\n${file.content}`)
204 }
205 }
206 } else {
207 // Few files — load all of them directly (no LLM call, no latency)
208 const allFiles = await loadMemoryFiles(workspaceDir, manifest.map(m => m.filename))
209 for (const file of allFiles) {
210 parts.push(`## 记忆: ${file.frontmatter.name || file.filename}\n${file.content}`)
211 }
212 }
213 }
214
215 // 3. Daily logs
216 const logs = await loadDailyLogs(workspaceDir, 2)
217 if (logs) {
218 parts.push(logs)
219 }
220
221 if (parts.length === 0) return ''
222 return parts.join('\n\n')
223}
224
225/**
226 * Append an entry to today's daily log.

Callers

nothing calls this directly

Calls 6

loadMemoryIndexFunction · 0.85
scanMemoryManifestFunction · 0.85
findRelevantMemoriesFunction · 0.85
loadMemoryFilesFunction · 0.85
loadDailyLogsFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected