refactor(core): reuse computeNewContent in performAddMemoryEntry (#6689)

Co-authored-by: chen893 <chenshuanglong@fuzhi.ai>
Co-authored-by: Sandy Tao <sandytao520@icloud.com>
This commit is contained in:
chen
2025-08-30 01:35:00 +08:00
committed by GitHub
parent af6a792caa
commit ea21f0fa03

View File

@@ -330,49 +330,18 @@ export class MemoryTool
) => Promise<string | undefined>;
},
): Promise<void> {
let processedText = text.trim();
// Remove leading hyphens and spaces that might be misinterpreted as markdown list items
processedText = processedText.replace(/^(-+\s*)+/, '').trim();
const newMemoryItem = `- ${processedText}`;
try {
await fsAdapter.mkdir(path.dirname(memoryFilePath), { recursive: true });
let content = '';
let currentContent = '';
try {
content = await fsAdapter.readFile(memoryFilePath, 'utf-8');
currentContent = await fsAdapter.readFile(memoryFilePath, 'utf-8');
} catch (_e) {
// File doesn't exist, will be created with header and item.
// File doesn't exist, which is fine. currentContent will be empty.
}
const headerIndex = content.indexOf(MEMORY_SECTION_HEADER);
const newContent = computeNewContent(currentContent, text);
if (headerIndex === -1) {
// Header not found, append header and then the entry
const separator = ensureNewlineSeparation(content);
content += `${separator}${MEMORY_SECTION_HEADER}\n${newMemoryItem}\n`;
} else {
// Header found, find where to insert the new memory entry
const startOfSectionContent =
headerIndex + MEMORY_SECTION_HEADER.length;
let endOfSectionIndex = content.indexOf('\n## ', startOfSectionContent);
if (endOfSectionIndex === -1) {
endOfSectionIndex = content.length; // End of file
}
const beforeSectionMarker = content
.substring(0, startOfSectionContent)
.trimEnd();
let sectionContent = content
.substring(startOfSectionContent, endOfSectionIndex)
.trimEnd();
const afterSectionMarker = content.substring(endOfSectionIndex);
sectionContent += `\n${newMemoryItem}`;
content =
`${beforeSectionMarker}\n${sectionContent.trimStart()}\n${afterSectionMarker}`.trimEnd() +
'\n';
}
await fsAdapter.writeFile(memoryFilePath, content, 'utf-8');
await fsAdapter.writeFile(memoryFilePath, newContent, 'utf-8');
} catch (error) {
console.error(
`[MemoryTool] Error adding memory entry to ${memoryFilePath}:`,