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