(workspaceDir, query, callLLM)
| 181 | * Orchestrates: index + relevant memories + daily logs. |
| 182 | */ |
| 183 | export 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. |
nothing calls this directly
no test coverage detected