(
systemPrompt: SystemPrompt,
options?: { skipGlobalCacheForSystemPrompt?: boolean },
)
| 319 | * - Everything else concatenated (cacheScope='org') |
| 320 | */ |
| 321 | export function splitSysPromptPrefix( |
| 322 | systemPrompt: SystemPrompt, |
| 323 | options?: { skipGlobalCacheForSystemPrompt?: boolean }, |
| 324 | ): SystemPromptBlock[] { |
| 325 | const useGlobalCacheFeature = shouldUseGlobalCacheScope() |
| 326 | if (useGlobalCacheFeature && options?.skipGlobalCacheForSystemPrompt) { |
| 327 | logEvent('tengu_sysprompt_using_tool_based_cache', { |
| 328 | promptBlockCount: systemPrompt.length, |
| 329 | }) |
| 330 | |
| 331 | // Filter out boundary marker, return blocks without global scope |
| 332 | let attributionHeader: string | undefined |
| 333 | let systemPromptPrefix: string | undefined |
| 334 | const rest: string[] = [] |
| 335 | |
| 336 | for (const prompt of systemPrompt) { |
| 337 | if (!prompt) continue |
| 338 | if (prompt === SYSTEM_PROMPT_DYNAMIC_BOUNDARY) continue // Skip boundary |
| 339 | if (prompt.startsWith('x-anthropic-billing-header')) { |
| 340 | attributionHeader = prompt |
| 341 | } else if (CLI_SYSPROMPT_PREFIXES.has(prompt)) { |
| 342 | systemPromptPrefix = prompt |
| 343 | } else { |
| 344 | rest.push(prompt) |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | const result: SystemPromptBlock[] = [] |
| 349 | if (attributionHeader) { |
| 350 | result.push({ text: attributionHeader, cacheScope: null }) |
| 351 | } |
| 352 | if (systemPromptPrefix) { |
| 353 | result.push({ text: systemPromptPrefix, cacheScope: 'org' }) |
| 354 | } |
| 355 | const restJoined = rest.join('\n\n') |
| 356 | if (restJoined) { |
| 357 | result.push({ text: restJoined, cacheScope: 'org' }) |
| 358 | } |
| 359 | return result |
| 360 | } |
| 361 | |
| 362 | if (useGlobalCacheFeature) { |
| 363 | const boundaryIndex = systemPrompt.findIndex( |
| 364 | s => s === SYSTEM_PROMPT_DYNAMIC_BOUNDARY, |
| 365 | ) |
| 366 | if (boundaryIndex !== -1) { |
| 367 | let attributionHeader: string | undefined |
| 368 | let systemPromptPrefix: string | undefined |
| 369 | const staticBlocks: string[] = [] |
| 370 | const dynamicBlocks: string[] = [] |
| 371 | |
| 372 | for (let i = 0; i < systemPrompt.length; i++) { |
| 373 | const block = systemPrompt[i] |
| 374 | if (!block || block === SYSTEM_PROMPT_DYNAMIC_BOUNDARY) continue |
| 375 | |
| 376 | if (block.startsWith('x-anthropic-billing-header')) { |
| 377 | attributionHeader = block |
| 378 | } else if (CLI_SYSPROMPT_PREFIXES.has(block)) { |
no test coverage detected