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