46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 | class ToolStep(BaseModel):
"""Executes all tool calls from the preceding `LLMStep` concurrently.
Yields a `StepResult[ToolOutput]` on success, or
`StepResult[ClarificationNeededOutput]` with `TransitionAction.END` when
any tool signals that human input is needed.
"""
model_config = ConfigDict(arbitrary_types_allowed=True)
name: str = "tool"
registry: SkipValidation[ToolRegistry]
async def run(
self,
state: State,
ctx: InvocationContext | None,
):
last: Message = state.last_message
if not isinstance(last, ToolCallMessage):
raise TypeError(
f"ToolStep expects last message to be ToolCallMessage, got {type(last).__name__}"
)
tool_calls: list[ToolCall] = last.tool_calls
tasks: list[Task[ToolCallResultMessage | ClarificationNeededOutput]] = [
asyncio.create_task(self._run_one(tc, ctx)) for tc in tool_calls
]
result_dicts: list[dict[str, Any]] = []
clarification: ClarificationNeededOutput | None = None
try:
for fut in asyncio.as_completed(tasks):
outcome: ToolCallResultMessage | ClarificationNeededOutput = await fut
if isinstance(outcome, ClarificationNeededOutput):
clarification: ClarificationNeededOutput = clarification or outcome
continue
msg: ToolCallResultMessage = outcome
result_dicts.append(
{
"tool_call_id": msg.tool_call_id,
"name": msg.name,
"content": msg.content,
}
)
yield ToolResultEvent(
content=msg.content or "",
message=msg,
)
finally:
await asyncio.gather(*tasks, return_exceptions=True)
if clarification is not None:
yield ClarificationNeededEvent(
content=clarification.question,
metadata={
"tool_call_id": clarification.tool_call_id,
"name": clarification.tool_name,
},
)
yield StepResult(
output=clarification,
transition=Transition(action=TransitionAction.END),
)
return
yield StepResult(
output=ToolOutput(results=tuple(result_dicts)),
transition=Transition(action=TransitionAction.CONTINUE, next_step="llm"),
)
async def _run_one(
self,
tool_call: ToolCall,
ctx: InvocationContext | None,
) -> ToolCallResultMessage | ClarificationNeededOutput:
"""Execute a single tool call and return its result or a clarification request.
Args:
tool_call: The tool call to execute, including name and arguments.
ctx: Invocation context, or None if not available.
Returns:
A `ToolCallResultMessage` with the serialized result, or a
`ClarificationNeededOutput` if the tool needs human input.
"""
tool_name: str = getattr(tool_call.function, "name", "<unknown>")
tool_call_id: str = getattr(tool_call, "id", "<unknown>")
if tool_name not in self.registry:
return ToolCallResultMessage(
name=tool_name,
tool_call_id=tool_call_id,
content=f"ERROR: Tool '{tool_name}' not found in registry.",
)
args_str: str = tool_call.function.arguments or ""
try:
parsed_args: dict[str, Any] = self._parse_args(args_str, tool_name)
except ValueError as exc:
return ToolCallResultMessage(
name=tool_name,
tool_call_id=tool_call_id,
content=f"ERROR: {exc}",
)
tool: Tool = self.registry[tool_name]
try:
async with obs.span(
tool_name,
as_type="tool",
input=parsed_args,
metadata={"tool_call_id": tool_call_id},
) as span:
res: Any = await tool.ainvoke(**parsed_args)
if isinstance(res, ClarificationNeededOutput):
return ClarificationNeededOutput(
question=res.question,
tool_call_id=tool_call_id,
tool_name=tool_name,
)
serialized = _serialize_result(res)
span.update(output=serialized)
return ToolCallResultMessage(
name=tool_name,
tool_call_id=tool_call_id,
content=serialized,
)
except Exception as exc:
return ToolCallResultMessage(
name=tool_name,
tool_call_id=tool_call_id,
content=f"ERROR: {exc}",
)
@staticmethod
def _parse_args(args_str: str, tool_name: str) -> dict[str, Any]:
"""Parse a JSON arguments string into a dict.
Args:
args_str: Raw JSON string from the tool call function arguments.
tool_name: Name of the tool, used in the error message if parsing fails.
Returns:
Parsed arguments as a dict. Returns an empty dict for empty input.
Raises:
ValueError: If `args_str` is not valid JSON or not a JSON object.
"""
if not args_str:
return {}
try:
result = json.loads(args_str)
if isinstance(result, dict):
return result
except Exception:
pass
raise ValueError(
f"Could not parse arguments for tool '{tool_name}' as JSON: {args_str!r}"
)
|