( hook: AgentHook, hookName: string, hookEvent: HookEvent, jsonInput: string, signal: AbortSignal, toolUseContext: ToolUseContext, toolUseID: string | undefined, // Kept for signature stability with the other exec*Hook functions. // Was used by hook.prompt(messages) before the .transform() was removed // (CC-79) — the only consumer of that was ExitPlanModeV2Tool's // programmatic construction, since refactored into VerifyPlanExecutionTool. _messages: Message[], agentName?: string, )
| 34 | * Execute an agent-based hook using a multi-turn LLM query |
| 35 | */ |
| 36 | export async function execAgentHook( |
| 37 | hook: AgentHook, |
| 38 | hookName: string, |
| 39 | hookEvent: HookEvent, |
| 40 | jsonInput: string, |
| 41 | signal: AbortSignal, |
| 42 | toolUseContext: ToolUseContext, |
| 43 | toolUseID: string | undefined, |
| 44 | // Kept for signature stability with the other exec*Hook functions. |
| 45 | // Was used by hook.prompt(messages) before the .transform() was removed |
| 46 | // (CC-79) — the only consumer of that was ExitPlanModeV2Tool's |
| 47 | // programmatic construction, since refactored into VerifyPlanExecutionTool. |
| 48 | _messages: Message[], |
| 49 | agentName?: string, |
| 50 | ): Promise<HookResult> { |
| 51 | const effectiveToolUseID = toolUseID || `hook-${randomUUID()}` |
| 52 | |
| 53 | // Get transcript path from context |
| 54 | const transcriptPath = toolUseContext.agentId |
| 55 | ? getAgentTranscriptPath(toolUseContext.agentId) |
| 56 | : getTranscriptPath() |
| 57 | const hookStartTime = Date.now() |
| 58 | try { |
| 59 | // Replace $ARGUMENTS with the JSON input |
| 60 | const processedPrompt = addArgumentsToPrompt(hook.prompt, jsonInput) |
| 61 | logForDebugging( |
| 62 | `Hooks: Processing agent hook with prompt: ${processedPrompt}`, |
| 63 | ) |
| 64 | |
| 65 | // Create user message directly - no need for processUserInput which would |
| 66 | // trigger UserPromptSubmit hooks and cause infinite recursion |
| 67 | const userMessage = createUserMessage({ content: processedPrompt }) |
| 68 | const agentMessages = [userMessage] |
| 69 | |
| 70 | logForDebugging( |
| 71 | `Hooks: Starting agent query with ${agentMessages.length} messages`, |
| 72 | ) |
| 73 | |
| 74 | // Setup timeout and combine with parent signal |
| 75 | const hookTimeoutMs = hook.timeout ? hook.timeout * 1000 : 60000 |
| 76 | const hookAbortController = createAbortController() |
| 77 | |
| 78 | // Combine parent signal with timeout, and have it abort our controller |
| 79 | const { signal: parentTimeoutSignal, cleanup: cleanupCombinedSignal } = |
| 80 | createCombinedAbortSignal(signal, { timeoutMs: hookTimeoutMs }) |
| 81 | const onParentTimeout = () => hookAbortController.abort() |
| 82 | parentTimeoutSignal.addEventListener('abort', onParentTimeout) |
| 83 | |
| 84 | // Combined signal is just our controller's signal now |
| 85 | const combinedSignal = hookAbortController.signal |
| 86 | |
| 87 | try { |
| 88 | // Create StructuredOutput tool with our schema |
| 89 | const structuredOutputTool = createStructuredOutputTool() |
| 90 | |
| 91 | // Filter out any existing StructuredOutput tool to avoid duplicates with different schemas |
| 92 | // (e.g., when parent context has a StructuredOutput tool from --json-schema flag) |
| 93 | const filteredTools = toolUseContext.options.tools.filter( |
no test coverage detected