|
| 1 | +"""Claude SDK hooks for streaming tool calls and subagent execution to AgentEx UI. |
| 2 | +
|
| 3 | +This module provides hook callbacks that integrate with Claude SDK's hooks system |
| 4 | +to stream tool execution lifecycle events in real-time. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +from claude_agent_sdk import HookMatcher |
| 12 | + |
| 13 | +from agentex.lib.utils.logging import make_logger |
| 14 | +from agentex.lib import adk |
| 15 | +from agentex.types.tool_request_content import ToolRequestContent |
| 16 | +from agentex.types.tool_response_content import ToolResponseContent |
| 17 | +from agentex.types.task_message_update import StreamTaskMessageFull |
| 18 | + |
| 19 | +logger = make_logger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class TemporalStreamingHooks: |
| 23 | + """Hooks for streaming Claude SDK lifecycle events to AgentEx UI. |
| 24 | +
|
| 25 | + Implements Claude SDK hook callbacks: |
| 26 | + - PreToolUse: Called before tool execution → stream tool request |
| 27 | + - PostToolUse: Called after tool execution → stream tool result |
| 28 | +
|
| 29 | + Also handles subagent detection and nested tracing. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + task_id: str | None, |
| 35 | + trace_id: str | None = None, |
| 36 | + parent_span_id: str | None = None, |
| 37 | + ): |
| 38 | + """Initialize streaming hooks. |
| 39 | +
|
| 40 | + Args: |
| 41 | + task_id: AgentEx task ID for routing streams |
| 42 | + trace_id: Trace ID for nested spans |
| 43 | + parent_span_id: Parent span ID for subagent spans |
| 44 | + """ |
| 45 | + self.task_id = task_id |
| 46 | + self.trace_id = trace_id |
| 47 | + self.parent_span_id = parent_span_id |
| 48 | + |
| 49 | + # Track active subagent spans |
| 50 | + self.subagent_spans: dict[str, Any] = {} # tool_call_id → (ctx, span) |
| 51 | + |
| 52 | + async def pre_tool_use( |
| 53 | + self, |
| 54 | + input_data: dict[str, Any], |
| 55 | + tool_use_id: str | None, |
| 56 | + context: Any, |
| 57 | + ) -> dict[str, Any]: |
| 58 | + """Hook called before tool execution. |
| 59 | +
|
| 60 | + Args: |
| 61 | + input_data: Contains tool_name, tool_input from Claude SDK |
| 62 | + tool_use_id: Unique ID for this tool call |
| 63 | + context: Hook context from Claude SDK |
| 64 | +
|
| 65 | + Returns: |
| 66 | + Empty dict (allow execution to proceed) |
| 67 | + """ |
| 68 | + if not self.task_id or not tool_use_id: |
| 69 | + return {} |
| 70 | + |
| 71 | + tool_name = input_data.get("tool_name", "unknown") |
| 72 | + tool_input = input_data.get("tool_input", {}) |
| 73 | + |
| 74 | + logger.info(f"🔧 Tool request: {tool_name}") |
| 75 | + |
| 76 | + # Special handling for Task tool (subagents) - create nested span |
| 77 | + if tool_name == "Task" and self.trace_id and self.parent_span_id: |
| 78 | + subagent_type = tool_input.get("subagent_type", "unknown") |
| 79 | + logger.info(f"🤖 Subagent started: {subagent_type}") |
| 80 | + |
| 81 | + # Create nested trace span for subagent |
| 82 | + subagent_ctx = adk.tracing.span( |
| 83 | + trace_id=self.trace_id, |
| 84 | + parent_id=self.parent_span_id, |
| 85 | + name=f"Subagent: {subagent_type}", |
| 86 | + input=tool_input, |
| 87 | + ) |
| 88 | + subagent_span = await subagent_ctx.__aenter__() |
| 89 | + self.subagent_spans[tool_use_id] = (subagent_ctx, subagent_span) |
| 90 | + |
| 91 | + # Stream tool request to UI |
| 92 | + try: |
| 93 | + async with adk.streaming.streaming_task_message_context( |
| 94 | + task_id=self.task_id, |
| 95 | + initial_content=ToolRequestContent( |
| 96 | + author="agent", |
| 97 | + name=tool_name, |
| 98 | + arguments=tool_input, |
| 99 | + tool_call_id=tool_use_id, |
| 100 | + ) |
| 101 | + ) as tool_ctx: |
| 102 | + await tool_ctx.stream_update( |
| 103 | + StreamTaskMessageFull( |
| 104 | + parent_task_message=tool_ctx.task_message, |
| 105 | + content=ToolRequestContent( |
| 106 | + author="agent", |
| 107 | + name=tool_name, |
| 108 | + arguments=tool_input, |
| 109 | + tool_call_id=tool_use_id, |
| 110 | + ), |
| 111 | + type="full" |
| 112 | + ) |
| 113 | + ) |
| 114 | + except Exception as e: |
| 115 | + logger.warning(f"Failed to stream tool request: {e}") |
| 116 | + |
| 117 | + return {} # Allow execution |
| 118 | + |
| 119 | + async def post_tool_use( |
| 120 | + self, |
| 121 | + input_data: dict[str, Any], |
| 122 | + tool_use_id: str | None, |
| 123 | + context: Any, |
| 124 | + ) -> dict[str, Any]: |
| 125 | + """Hook called after tool execution. |
| 126 | +
|
| 127 | + Args: |
| 128 | + input_data: Contains tool_name, tool_output from Claude SDK |
| 129 | + tool_use_id: Unique ID for this tool call |
| 130 | + context: Hook context from Claude SDK |
| 131 | +
|
| 132 | + Returns: |
| 133 | + Empty dict |
| 134 | + """ |
| 135 | + if not self.task_id or not tool_use_id: |
| 136 | + return {} |
| 137 | + |
| 138 | + tool_name = input_data.get("tool_name", "unknown") |
| 139 | + tool_output = input_data.get("tool_output", "") |
| 140 | + |
| 141 | + logger.info(f"✅ Tool result: {tool_name}") |
| 142 | + |
| 143 | + # If this was a subagent, close the nested span |
| 144 | + if tool_use_id in self.subagent_spans: |
| 145 | + subagent_ctx, subagent_span = self.subagent_spans[tool_use_id] |
| 146 | + subagent_span.output = {"result": tool_output} |
| 147 | + await subagent_ctx.__aexit__(None, None, None) |
| 148 | + logger.info(f"🤖 Subagent completed: {tool_name}") |
| 149 | + del self.subagent_spans[tool_use_id] |
| 150 | + |
| 151 | + # Stream tool response to UI |
| 152 | + try: |
| 153 | + async with adk.streaming.streaming_task_message_context( |
| 154 | + task_id=self.task_id, |
| 155 | + initial_content=ToolResponseContent( |
| 156 | + author="agent", |
| 157 | + name=tool_name, |
| 158 | + content=tool_output, |
| 159 | + tool_call_id=tool_use_id, |
| 160 | + ) |
| 161 | + ) as tool_ctx: |
| 162 | + await tool_ctx.stream_update( |
| 163 | + StreamTaskMessageFull( |
| 164 | + parent_task_message=tool_ctx.task_message, |
| 165 | + content=ToolResponseContent( |
| 166 | + author="agent", |
| 167 | + name=tool_name, |
| 168 | + content=tool_output, |
| 169 | + tool_call_id=tool_use_id, |
| 170 | + ), |
| 171 | + type="full" |
| 172 | + ) |
| 173 | + ) |
| 174 | + except Exception as e: |
| 175 | + logger.warning(f"Failed to stream tool response: {e}") |
| 176 | + |
| 177 | + return {} |
| 178 | + |
| 179 | + |
| 180 | +def create_streaming_hooks( |
| 181 | + task_id: str | None, |
| 182 | + trace_id: str | None = None, |
| 183 | + parent_span_id: str | None = None, |
| 184 | +) -> dict[str, list[HookMatcher]]: |
| 185 | + """Create Claude SDK hooks configuration for streaming. |
| 186 | +
|
| 187 | + Returns hooks dict suitable for ClaudeAgentOptions(hooks=...). |
| 188 | +
|
| 189 | + Args: |
| 190 | + task_id: AgentEx task ID for streaming |
| 191 | + trace_id: Trace ID for nested spans |
| 192 | + parent_span_id: Parent span ID for subagent spans |
| 193 | +
|
| 194 | + Returns: |
| 195 | + Dict with PreToolUse and PostToolUse hook configurations |
| 196 | + """ |
| 197 | + hooks_instance = TemporalStreamingHooks(task_id, trace_id, parent_span_id) |
| 198 | + |
| 199 | + return { |
| 200 | + "PreToolUse": [ |
| 201 | + HookMatcher( |
| 202 | + matcher=None, # Match all tools |
| 203 | + hooks=[hooks_instance.pre_tool_use] |
| 204 | + ) |
| 205 | + ], |
| 206 | + "PostToolUse": [ |
| 207 | + HookMatcher( |
| 208 | + matcher=None, # Match all tools |
| 209 | + hooks=[hooks_instance.post_tool_use] |
| 210 | + ) |
| 211 | + ], |
| 212 | + } |
0 commit comments