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

Function execAgentHook

src/utils/hooks/execAgentHook.ts:36–339  ·  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

34 * Execute an agent-based hook using a multi-turn LLM query
35 */
36export 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(

Callers 1

executeHooksFunction · 0.85

Calls 15

getAgentTranscriptPathFunction · 0.85
getTranscriptPathFunction · 0.85
addArgumentsToPromptFunction · 0.85
logForDebuggingFunction · 0.85
createUserMessageFunction · 0.85
createAbortControllerFunction · 0.85
toolMatchesNameFunction · 0.85
asSystemPromptFunction · 0.85
getSmallFastModelFunction · 0.85
asAgentIdFunction · 0.85

Tested by

no test coverage detected