forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostSamplingHooks.ts
More file actions
70 lines (62 loc) · 1.95 KB
/
Copy pathpostSamplingHooks.ts
File metadata and controls
70 lines (62 loc) · 1.95 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
69
70
import type { QuerySource } from '../../constants/querySource.js'
import type { ToolUseContext } from '../../Tool.js'
import type { Message } from '../../types/message.js'
import { toError } from '../errors.js'
import { logError } from '../log.js'
import type { SystemPrompt } from '../systemPromptType.js'
// Post-sampling hook - not exposed in settings.json config (yet), only used programmatically
// Generic context for REPL hooks (both post-sampling and stop hooks)
export type REPLHookContext = {
messages: Message[] // Full message history including assistant responses
systemPrompt: SystemPrompt
userContext: { [k: string]: string }
systemContext: { [k: string]: string }
toolUseContext: ToolUseContext
querySource?: QuerySource
}
export type PostSamplingHook = (
context: REPLHookContext,
) => Promise<void> | void
// Internal registry for post-sampling hooks
const postSamplingHooks: PostSamplingHook[] = []
/**
* Register a post-sampling hook that will be called after model sampling completes
* This is an internal API not exposed through settings
*/
export function registerPostSamplingHook(hook: PostSamplingHook): void {
postSamplingHooks.push(hook)
}
/**
* Clear all registered post-sampling hooks (for testing)
*/
export function clearPostSamplingHooks(): void {
postSamplingHooks.length = 0
}
/**
* Execute all registered post-sampling hooks
*/
export async function executePostSamplingHooks(
messages: Message[],
systemPrompt: SystemPrompt,
userContext: { [k: string]: string },
systemContext: { [k: string]: string },
toolUseContext: ToolUseContext,
querySource?: QuerySource,
): Promise<void> {
const context: REPLHookContext = {
messages,
systemPrompt,
userContext,
systemContext,
toolUseContext,
querySource,
}
for (const hook of postSamplingHooks) {
try {
await hook(context)
} catch (error) {
// Log but don't fail on hook errors
logError(toError(error))
}
}
}