systemPromptSections.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {
  2. clearBetaHeaderLatches,
  3. clearSystemPromptSectionState,
  4. getSystemPromptSectionCache,
  5. setSystemPromptSectionCacheEntry,
  6. } from '../bootstrap/state.js'
  7. type ComputeFn = () => string | null | Promise<string | null>
  8. type SystemPromptSection = {
  9. name: string
  10. compute: ComputeFn
  11. cacheBreak: boolean
  12. }
  13. /**
  14. * Create a memoized system prompt section.
  15. * Computed once, cached until /clear or /compact.
  16. */
  17. export function systemPromptSection(
  18. name: string,
  19. compute: ComputeFn,
  20. ): SystemPromptSection {
  21. return { name, compute, cacheBreak: false }
  22. }
  23. /**
  24. * Create a volatile system prompt section that recomputes every turn.
  25. * This WILL break the prompt cache when the value changes.
  26. * Requires a reason explaining why cache-breaking is necessary.
  27. */
  28. export function DANGEROUS_uncachedSystemPromptSection(
  29. name: string,
  30. compute: ComputeFn,
  31. _reason: string,
  32. ): SystemPromptSection {
  33. return { name, compute, cacheBreak: true }
  34. }
  35. /**
  36. * Resolve all system prompt sections, returning prompt strings.
  37. */
  38. export async function resolveSystemPromptSections(
  39. sections: SystemPromptSection[],
  40. ): Promise<(string | null)[]> {
  41. const cache = getSystemPromptSectionCache()
  42. return Promise.all(
  43. sections.map(async s => {
  44. if (!s.cacheBreak && cache.has(s.name)) {
  45. return cache.get(s.name) ?? null
  46. }
  47. const value = await s.compute()
  48. setSystemPromptSectionCacheEntry(s.name, value)
  49. return value
  50. }),
  51. )
  52. }
  53. /**
  54. * Clear all system prompt section state. Called on /clear and /compact.
  55. * Also resets beta header latches so a fresh conversation gets fresh
  56. * evaluation of AFK/fast-mode/cache-editing headers.
  57. */
  58. export function clearSystemPromptSections(): void {
  59. clearSystemPromptSectionState()
  60. clearBetaHeaderLatches()
  61. }