generateSectionReminders function

String generateSectionReminders(
  1. Map<String, int> sectionSizes,
  2. int totalTokens
)

Generate reminders for sections that are too long.

Implementation

String generateSectionReminders(
  Map<String, int> sectionSizes,
  int totalTokens,
) {
  final overBudget = totalTokens > _maxTotalSessionMemoryTokens;
  final oversizedSections =
      sectionSizes.entries.where((e) => e.value > _maxSectionLength).toList()
        ..sort((a, b) => b.value.compareTo(a.value));
  final oversizedLines = oversizedSections
      .map(
        (e) => '- "${e.key}" is ~${e.value} tokens (limit: $_maxSectionLength)',
      )
      .toList();

  if (oversizedLines.isEmpty && !overBudget) return '';

  final parts = <String>[];

  if (overBudget) {
    parts.add(
      '\n\nCRITICAL: The session memory file is currently ~$totalTokens tokens, '
      'which exceeds the maximum of $_maxTotalSessionMemoryTokens tokens. '
      'You MUST condense the file to fit within this budget.',
    );
  }

  if (oversizedLines.isNotEmpty) {
    final prefix = overBudget
        ? 'Oversized sections to condense'
        : 'IMPORTANT: The following sections exceed the per-section limit and MUST be condensed';
    parts.add('\n\n$prefix:\n${oversizedLines.join('\n')}');
  }

  return parts.join('');
}