MCPcopy
hub / github.com/claude-code-best/claude-code / execPromptHook

Function execPromptHook

src/utils/hooks/execPromptHook.ts:21–214  ·  view source on GitHub ↗
(
  hook: PromptHook,
  hookName: string,
  hookEvent: HookEvent,
  jsonInput: string,
  signal: AbortSignal,
  toolUseContext: ToolUseContext,
  messages?: Message[],
  toolUseID?: string,
)

Source from the content-addressed store, hash-verified

19 * Execute a prompt-based hook using an LLM
20 */
21export async function execPromptHook(
22 hook: PromptHook,
23 hookName: string,
24 hookEvent: HookEvent,
25 jsonInput: string,
26 signal: AbortSignal,
27 toolUseContext: ToolUseContext,
28 messages?: Message[],
29 toolUseID?: string,
30): Promise<HookResult> {
31 // Use provided toolUseID or generate a new one
32 const effectiveToolUseID = toolUseID || `hook-${randomUUID()}`
33 try {
34 // Replace $ARGUMENTS with the JSON input
35 const processedPrompt = addArgumentsToPrompt(hook.prompt, jsonInput)
36 logForDebugging(
37 `Hooks: Processing prompt hook with prompt: ${processedPrompt}`,
38 )
39
40 // Create user message directly - no need for processUserInput which would
41 // trigger UserPromptSubmit hooks and cause infinite recursion
42 const userMessage = createUserMessage({ content: processedPrompt })
43
44 // Prepend conversation history if provided
45 const messagesToQuery =
46 messages && messages.length > 0
47 ? [...messages, userMessage]
48 : [userMessage]
49
50 logForDebugging(
51 `Hooks: Querying model with ${messagesToQuery.length} messages`,
52 )
53
54 // Query the model with Haiku
55 const hookTimeoutMs = hook.timeout ? hook.timeout * 1000 : 30000
56
57 // Combined signal: aborts if either the hook signal or timeout triggers
58 const { signal: combinedSignal, cleanup: cleanupSignal } =
59 createCombinedAbortSignal(signal, { timeoutMs: hookTimeoutMs })
60
61 try {
62 const response = await queryModelWithoutStreaming({
63 messages: messagesToQuery,
64 systemPrompt: asSystemPrompt([
65 `You are evaluating a hook in Claude Code.
66
67Your response must be a JSON object matching one of the following schemas:
681. If the condition is met, return: {"ok": true}
692. If the condition is not met, return: {"ok": false, "reason": "Reason for why it is not met"}`,
70 ]),
71 thinkingConfig: { type: 'disabled' as const },
72 tools: toolUseContext.options.tools,
73 signal: combinedSignal,
74 options: {
75 async getToolPermissionContext() {
76 const appState = toolUseContext.getAppState()
77 return appState.toolPermissionContext
78 },

Callers 1

executeHooksFunction · 0.85

Calls 11

addArgumentsToPromptFunction · 0.85
createUserMessageFunction · 0.85
asSystemPromptFunction · 0.85
getSmallFastModelFunction · 0.85
extractTextContentFunction · 0.85
safeParseJSONFunction · 0.85
createAttachmentMessageFunction · 0.85
logForDebuggingFunction · 0.50
errorMessageFunction · 0.50

Tested by

no test coverage detected