|
1 | 1 | import asyncio |
2 | 2 | import random |
3 | | -from typing import Any, Optional |
| 3 | +from typing import Any, Optional, cast |
4 | 4 |
|
5 | 5 | from pydantic import BaseModel |
6 | 6 |
|
|
15 | 15 | function_tool, |
16 | 16 | ) |
17 | 17 | from agents.items import ModelResponse, TResponseInputItem |
| 18 | +from agents.tool_context import ToolContext |
18 | 19 |
|
19 | 20 |
|
20 | 21 | class LoggingHooks(AgentHooks[Any]): |
@@ -71,16 +72,22 @@ async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output: A |
71 | 72 |
|
72 | 73 | async def on_tool_start(self, context: RunContextWrapper, agent: Agent, tool: Tool) -> None: |
73 | 74 | self.event_counter += 1 |
| 75 | + # While this type cast is not ideal, |
| 76 | + # we don't plan to change the context arg type in the near future for backwards compatibility. |
| 77 | + tool_context = cast(ToolContext[Any], context) |
74 | 78 | print( |
75 | | - f"### {self.event_counter}: Tool {tool.name} started. name={context.tool_name}, call_id={context.tool_call_id}, args={context.tool_arguments}. Usage: {self._usage_to_str(context.usage)}" # type: ignore[attr-defined] |
| 79 | + f"### {self.event_counter}: Tool {tool.name} started. name={tool_context.tool_name}, call_id={tool_context.tool_call_id}, args={tool_context.tool_arguments}. Usage: {self._usage_to_str(tool_context.usage)}" |
76 | 80 | ) |
77 | 81 |
|
78 | 82 | async def on_tool_end( |
79 | 83 | self, context: RunContextWrapper, agent: Agent, tool: Tool, result: str |
80 | 84 | ) -> None: |
81 | 85 | self.event_counter += 1 |
| 86 | + # While this type cast is not ideal, |
| 87 | + # we don't plan to change the context arg type in the near future for backwards compatibility. |
| 88 | + tool_context = cast(ToolContext[Any], context) |
82 | 89 | print( |
83 | | - f"### {self.event_counter}: Tool {tool.name} finished. result={result}, name={context.tool_name}, call_id={context.tool_call_id}, args={context.tool_arguments}. Usage: {self._usage_to_str(context.usage)}" # type: ignore[attr-defined] |
| 90 | + f"### {self.event_counter}: Tool {tool.name} finished. result={result}, name={tool_context.tool_name}, call_id={tool_context.tool_call_id}, args={tool_context.tool_arguments}. Usage: {self._usage_to_str(tool_context.usage)}" |
84 | 91 | ) |
85 | 92 |
|
86 | 93 | async def on_handoff( |
|
0 commit comments