|
11 | 11 | import logging |
12 | 12 | import time |
13 | 13 | import uuid |
14 | | -from typing import TYPE_CHECKING, Any, AsyncGenerator |
| 14 | +from typing import TYPE_CHECKING, Any, AsyncGenerator, cast |
15 | 15 |
|
16 | 16 | from ..experimental.hooks import AfterToolInvocationEvent, BeforeToolInvocationEvent |
17 | 17 | from ..experimental.hooks.registry import get_registry |
|
21 | 21 | from ..types.content import Message |
22 | 22 | from ..types.exceptions import ContextWindowOverflowException, EventLoopException, ModelThrottledException |
23 | 23 | from ..types.streaming import Metrics, StopReason |
24 | | -from ..types.tools import ToolGenerator, ToolResult, ToolUse |
| 24 | +from ..types.tools import ToolChoice, ToolChoiceAuto, ToolConfig, ToolGenerator, ToolResult, ToolUse |
25 | 25 | from .message_processor import clean_orphaned_empty_tool_uses |
26 | 26 | from .streaming import stream_messages |
27 | 27 |
|
@@ -112,10 +112,12 @@ async def event_loop_cycle(agent: "Agent", kwargs: dict[str, Any]) -> AsyncGener |
112 | 112 | model_id=model_id, |
113 | 113 | ) |
114 | 114 |
|
| 115 | + tool_specs = agent.tool_registry.get_all_tool_specs() |
| 116 | + |
115 | 117 | try: |
116 | 118 | # TODO: To maintain backwards compatibility, we need to combine the stream event with kwargs before yielding |
117 | 119 | # to the callback handler. This will be revisited when migrating to strongly typed events. |
118 | | - async for event in stream_messages(agent.model, agent.system_prompt, agent.messages, agent.tool_config): |
| 120 | + async for event in stream_messages(agent.model, agent.system_prompt, agent.messages, tool_specs): |
119 | 121 | if "callback" in event: |
120 | 122 | yield {"callback": {**event["callback"], **(kwargs if "delta" in event["callback"] else {})}} |
121 | 123 |
|
@@ -172,12 +174,6 @@ async def event_loop_cycle(agent: "Agent", kwargs: dict[str, Any]) -> AsyncGener |
172 | 174 |
|
173 | 175 | # If the model is requesting to use tools |
174 | 176 | if stop_reason == "tool_use": |
175 | | - if agent.tool_config is None: |
176 | | - raise EventLoopException( |
177 | | - Exception("Model requested tool use but no tool config provided"), |
178 | | - kwargs["request_state"], |
179 | | - ) |
180 | | - |
181 | 177 | # Handle tool execution |
182 | 178 | events = _handle_tool_execution( |
183 | 179 | stop_reason, |
@@ -285,7 +281,10 @@ def run_tool(agent: "Agent", tool_use: ToolUse, kwargs: dict[str, Any]) -> ToolG |
285 | 281 | "model": agent.model, |
286 | 282 | "system_prompt": agent.system_prompt, |
287 | 283 | "messages": agent.messages, |
288 | | - "tool_config": agent.tool_config, |
| 284 | + "tool_config": ToolConfig( # for backwards compatability |
| 285 | + tools=[{"toolSpec": tool_spec} for tool_spec in agent.tool_registry.get_all_tool_specs()], |
| 286 | + toolChoice=cast(ToolChoice, {"auto": ToolChoiceAuto()}), |
| 287 | + ), |
289 | 288 | } |
290 | 289 | ) |
291 | 290 |
|
|
0 commit comments