( effectiveSystemPrompt: readonly string[], )
| 270 | } |
| 271 | |
| 272 | async function countSystemTokens( |
| 273 | effectiveSystemPrompt: readonly string[], |
| 274 | ): Promise<{ |
| 275 | systemPromptTokens: number |
| 276 | systemPromptSections: SystemPromptSectionDetail[] |
| 277 | }> { |
| 278 | // Get system context (gitStatus, etc.) which is always included |
| 279 | const systemContext = await getSystemContext() |
| 280 | |
| 281 | // Build named entries: system prompt parts + system context values |
| 282 | // Skip empty strings and the global-cache boundary marker |
| 283 | const namedEntries: Array<{ name: string; content: string }> = [ |
| 284 | ...effectiveSystemPrompt |
| 285 | .filter( |
| 286 | content => |
| 287 | content.length > 0 && content !== SYSTEM_PROMPT_DYNAMIC_BOUNDARY, |
| 288 | ) |
| 289 | .map(content => ({ name: extractSectionName(content), content })), |
| 290 | ...Object.entries(systemContext) |
| 291 | .filter(([, content]) => content.length > 0) |
| 292 | .map(([name, content]) => ({ name, content })), |
| 293 | ] |
| 294 | |
| 295 | if (namedEntries.length < 1) { |
| 296 | return { systemPromptTokens: 0, systemPromptSections: [] } |
| 297 | } |
| 298 | |
| 299 | const systemTokenCounts = await Promise.all( |
| 300 | namedEntries.map(({ content }) => |
| 301 | countTokensWithFallback([{ role: 'user', content }], []), |
| 302 | ), |
| 303 | ) |
| 304 | |
| 305 | const systemPromptSections: SystemPromptSectionDetail[] = namedEntries.map( |
| 306 | (entry, i) => ({ |
| 307 | name: entry.name, |
| 308 | tokens: systemTokenCounts[i] || 0, |
| 309 | }), |
| 310 | ) |
| 311 | |
| 312 | const systemPromptTokens = systemTokenCounts.reduce( |
| 313 | (sum: number, tokens) => sum + (tokens || 0), |
| 314 | 0, |
| 315 | ) |
| 316 | |
| 317 | return { systemPromptTokens, systemPromptSections } |
| 318 | } |
| 319 | |
| 320 | async function countMemoryFileTokens(): Promise<{ |
| 321 | memoryFileDetails: MemoryFile[] |
no test coverage detected