feat: 优化 core_memory MCP 工具,支持跨项目导出和精简列表输出

- 新增 findMemoryAcrossProjects 函数,支持按 CMEM-xxx ID 跨所有项目搜索记忆
- export 操作增强:当前项目找不到时自动搜索所有项目数据库
- list 操作优化:返回精简格式,preview 截断为 100 字符,减少输出体积

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
catlog22
2025-12-21 16:39:45 +08:00
parent 6eebdb8898
commit c12ef3e772
2 changed files with 91 additions and 6 deletions

View File

@@ -1383,6 +1383,55 @@ export function getMemoriesFromProject(projectId: string): CoreMemory[] {
}));
}
/**
* Find a memory by ID across all projects
* Searches through all project databases to locate a specific memory
*/
export function findMemoryAcrossProjects(memoryId: string): { memory: CoreMemory; projectId: string } | null {
const projectsDir = join(getCCWHome(), 'projects');
if (!existsSync(projectsDir)) {
return null;
}
const entries = readdirSync(projectsDir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const projectId = entry.name;
const coreMemoryDb = join(projectsDir, projectId, 'core-memory', 'core_memory.db');
if (!existsSync(coreMemoryDb)) continue;
try {
const db = new Database(coreMemoryDb, { readonly: true });
const row = db.prepare('SELECT * FROM memories WHERE id = ?').get(memoryId) as any;
db.close();
if (row) {
return {
memory: {
id: row.id,
content: row.content,
summary: row.summary || '',
raw_output: row.raw_output,
created_at: row.created_at,
updated_at: row.updated_at,
archived: Boolean(row.archived),
metadata: row.metadata
},
projectId
};
}
} catch {
// Database might be locked or corrupted, skip
}
}
return null;
}
/**
* Export memories to a JSON file
*/