Skip to content

Commit 1a7f3c4

Browse files
committed
Add arguments to hook tool calls
1 parent fd495a9 commit 1a7f3c4

File tree

1 file changed

+19
-11
lines changed
  • src/agentex/lib/core/temporal/plugins/openai_agents/hooks

1 file changed

+19
-11
lines changed

src/agentex/lib/core/temporal/plugins/openai_agents/hooks/hooks.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ async def on_agent_start(self, context, agent):
5555
Power users can ignore this class and subclass agents.RunHooks directly for full control.
5656
5757
Note:
58-
Tool arguments are not available in hooks due to OpenAI SDK architecture.
59-
The SDK's hook signature doesn't include tool arguments - they're only passed
60-
to the actual tool function. This is why arguments={} in ToolRequestContent.
58+
Tool arguments are extracted from the ToolContext's tool_arguments field,
59+
which contains a JSON string of the arguments passed to the tool.
6160
6261
Attributes:
6362
task_id: The AgentEx task ID for routing streamed events
@@ -108,21 +107,30 @@ async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output: A
108107
async def on_tool_start(self, context: RunContextWrapper, agent: Agent, tool: Tool) -> None: # noqa: ARG002
109108
"""Stream tool request when a tool starts execution.
110109
111-
Extracts the tool_call_id from the context and streams a ToolRequestContent
112-
message to the UI showing that the tool is about to execute.
113-
114-
Note: Tool arguments are not available in the hook context due to OpenAI SDK
115-
design. The hook signature doesn't include tool arguments - they're passed
116-
directly to the tool function instead. We send an empty dict as a placeholder.
110+
Extracts the tool_call_id and tool_arguments from the context and streams a
111+
ToolRequestContent message to the UI showing that the tool is about to execute.
117112
118113
Args:
119-
context: The run context wrapper (will be a ToolContext with tool_call_id)
114+
context: The run context wrapper (will be a ToolContext with tool_call_id and tool_arguments)
120115
agent: The agent executing the tool
121116
tool: The tool being executed
122117
"""
118+
import json
119+
123120
tool_context = context if isinstance(context, ToolContext) else None
124121
tool_call_id = tool_context.tool_call_id if tool_context else f"call_{id(tool)}"
125122

123+
# Extract tool arguments from context
124+
tool_arguments = {}
125+
if tool_context and hasattr(tool_context, 'tool_arguments'):
126+
try:
127+
# tool_arguments is a JSON string, parse it
128+
tool_arguments = json.loads(tool_context.tool_arguments)
129+
except (json.JSONDecodeError, TypeError):
130+
# If parsing fails, log and use empty dict
131+
logger.warning(f"Failed to parse tool arguments: {tool_context.tool_arguments}")
132+
tool_arguments = {}
133+
126134
await workflow.execute_activity_method(
127135
stream_lifecycle_content,
128136
args=[
@@ -131,7 +139,7 @@ async def on_tool_start(self, context: RunContextWrapper, agent: Agent, tool: To
131139
author="agent",
132140
tool_call_id=tool_call_id,
133141
name=tool.name,
134-
arguments={}, # Not available in hook context - SDK limitation
142+
arguments=tool_arguments, # Now properly extracted from context
135143
),
136144
],
137145
start_to_close_timeout=self.timeout,

0 commit comments

Comments
 (0)