( hook: PromptHook, hookName: string, hookEvent: HookEvent, jsonInput: string, signal: AbortSignal, toolUseContext: ToolUseContext, messages?: Message[], toolUseID?: string, )
| 19 | * Execute a prompt-based hook using an LLM |
| 20 | */ |
| 21 | export 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 | |
| 67 | Your response must be a JSON object matching one of the following schemas: |
| 68 | 1. If the condition is met, return: {"ok": true} |
| 69 | 2. 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 | }, |
no test coverage detected