* Generate reminders for sections that are too long
( sectionSizes: Record<string, number>, totalTokens: number, )
| 162 | * Generate reminders for sections that are too long |
| 163 | */ |
| 164 | function generateSectionReminders( |
| 165 | sectionSizes: Record<string, number>, |
| 166 | totalTokens: number, |
| 167 | ): string { |
| 168 | const overBudget = totalTokens > MAX_TOTAL_SESSION_MEMORY_TOKENS |
| 169 | const oversizedSections = Object.entries(sectionSizes) |
| 170 | .filter(([_, tokens]) => tokens > MAX_SECTION_LENGTH) |
| 171 | .sort(([, a], [, b]) => b - a) |
| 172 | .map( |
| 173 | ([section, tokens]) => |
| 174 | `- "${section}" is ~${tokens} tokens (limit: ${MAX_SECTION_LENGTH})`, |
| 175 | ) |
| 176 | |
| 177 | if (oversizedSections.length === 0 && !overBudget) { |
| 178 | return '' |
| 179 | } |
| 180 | |
| 181 | const parts: string[] = [] |
| 182 | |
| 183 | if (overBudget) { |
| 184 | parts.push( |
| 185 | `\n\nCRITICAL: The session memory file is currently ~${totalTokens} tokens, which exceeds the maximum of ${MAX_TOTAL_SESSION_MEMORY_TOKENS} tokens. You MUST condense the file to fit within this budget. Aggressively shorten oversized sections by removing less important details, merging related items, and summarizing older entries. Prioritize keeping "Current State" and "Errors & Corrections" accurate and detailed.`, |
| 186 | ) |
| 187 | } |
| 188 | |
| 189 | if (oversizedSections.length > 0) { |
| 190 | parts.push( |
| 191 | `\n\n${overBudget ? 'Oversized sections to condense' : 'IMPORTANT: The following sections exceed the per-section limit and MUST be condensed'}:\n${oversizedSections.join('\n')}`, |
| 192 | ) |
| 193 | } |
| 194 | |
| 195 | return parts.join('') |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Substitute variables in the prompt template using {{variable}} syntax |
no test coverage detected