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