acceptMemory method

Future<MemoryFile> acceptMemory(
  1. ExtractedMemory memory, {
  2. String? targetPath,
  3. String? heading,
})

Accept an extracted memory and add it to a memory file.

Implementation

Future<MemoryFile> acceptMemory(
  ExtractedMemory memory, {
  String? targetPath,
  String? heading,
}) async {
  final path = targetPath ?? '$_projectRoot/NEOMAGE.md';
  final sectionHeading = heading ?? _categoryHeading(memory.category);

  // Check if section exists.
  final existing = _files[path];
  if (existing != null) {
    final sections = MemoryParser.parse(existing.content);
    final section = sections
        .where((s) => s.heading == sectionHeading)
        .firstOrNull;

    if (section != null) {
      // Append to existing section.
      final newContent = '${section.content}\n- ${memory.content}';
      return updateSection(
        path,
        heading: sectionHeading,
        newContent: newContent,
      ).then((f) => f!);
    }
  }

  // Create new section.
  return addSection(
    path,
    heading: sectionHeading,
    content: '- ${memory.content}',
  );
}