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

Function execAgentHook

src/utils/hooks/execAgentHook.ts:59–359  ·  view source on GitHub ↗
(
  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,
)

Source from the content-addressed store, hash-verified

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

Callers 1

executeHooksFunction · 0.85

Calls 15

getAgentTranscriptPathFunction · 0.85
addArgumentsToPromptFunction · 0.85
createUserMessageFunction · 0.85
createAbortControllerFunction · 0.85
asSystemPromptFunction · 0.85
getSmallFastModelFunction · 0.85
asAgentIdFunction · 0.85
queryFunction · 0.85
handleMessageFromStreamFunction · 0.85

Tested by

no test coverage detected