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