analyzeSectionSizes function

Map<String, int> analyzeSectionSizes(
  1. String content
)

Parse the session memory file and analyse section sizes.

Implementation

Map<String, int> analyzeSectionSizes(String content) {
  final sections = <String, int>{};
  final lines = content.split('\n');
  var currentSection = '';
  final currentContent = <String>[];

  for (final line in lines) {
    if (line.startsWith('# ')) {
      if (currentSection.isNotEmpty && currentContent.isNotEmpty) {
        final sectionContent = currentContent.join('\n').trim();
        sections[currentSection] = roughTokenCountEstimation(sectionContent);
      }
      currentSection = line;
      currentContent.clear();
    } else {
      currentContent.add(line);
    }
  }

  if (currentSection.isNotEmpty && currentContent.isNotEmpty) {
    final sectionContent = currentContent.join('\n').trim();
    sections[currentSection] = roughTokenCountEstimation(sectionContent);
  }

  return sections;
}