Skip to content

Commit 2a23cb7

Browse files
committed
rollback
Signed-off-by: myan <[email protected]>
1 parent 6293d66 commit 2a23cb7

File tree

8 files changed

+46
-40
lines changed

8 files changed

+46
-40
lines changed

examples/basic/agent_lifecycle_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pydantic import BaseModel
66

7-
from agents import Agent, AgentHooks, RunContextWrapper, Runner, Tool, function_tool
7+
from agents import Agent, AgentHooks, RunContextWrapper, Runner, Tool, Action, function_tool
88

99

1010
class CustomAgentHooks(AgentHooks):
@@ -28,10 +28,10 @@ async def on_handoff(self, context: RunContextWrapper, agent: Agent, source: Age
2828
f"### ({self.display_name}) {self.event_counter}: Agent {source.name} handed off to {agent.name}"
2929
)
3030

31-
async def on_tool_start(self, context: RunContextWrapper, agent: Agent, tool: Tool) -> None:
31+
async def on_tool_start(self, context: RunContextWrapper, agent: Agent, action: Action) -> None:
3232
self.event_counter += 1
3333
print(
34-
f"### ({self.display_name}) {self.event_counter}: Agent {agent.name} started tool {tool.name}"
34+
f"### ({self.display_name}) {self.event_counter}: Agent {agent.name} started tool {action.function_tool.name} with arguments {action.tool_call.arguments}"
3535
)
3636

3737
async def on_tool_end(

examples/basic/lifecycle_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from pydantic import BaseModel
66

7-
from agents import Agent, RunContextWrapper, RunHooks, Runner, Tool, Usage, function_tool
7+
from agents import Agent, RunContextWrapper, RunHooks, Runner, Tool, Usage, function_tool, Action
88

99

1010
class ExampleHooks(RunHooks):
@@ -26,10 +26,10 @@ async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output: A
2626
f"### {self.event_counter}: Agent {agent.name} ended with output {output}. Usage: {self._usage_to_str(context.usage)}"
2727
)
2828

29-
async def on_tool_start(self, context: RunContextWrapper, agent: Agent, tool: Tool) -> None:
29+
async def on_tool_start(self, context: RunContextWrapper, agent: Agent, action: Action) -> None:
3030
self.event_counter += 1
3131
print(
32-
f"### {self.event_counter}: Tool {tool.name} started. Usage: {self._usage_to_str(context.usage)}"
32+
f"### {self.event_counter}: Tool {action.function_tool.tool.name} started. Usage: {self._usage_to_str(context.usage)}"
3333
)
3434

3535
async def on_tool_end(

src/agents/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
WebSearchTool,
7676
default_tool_error_function,
7777
function_tool,
78+
Action,
7879
)
7980
from .tracing import (
8081
AgentSpanData,
@@ -239,6 +240,7 @@ def enable_verbose_stdout_logging():
239240
"MCPToolApprovalRequest",
240241
"MCPToolApprovalFunctionResult",
241242
"function_tool",
243+
"Action",
242244
"Usage",
243245
"add_trace_processor",
244246
"agent_span",

src/agents/_run_impl.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
LocalShellTool,
7979
MCPToolApprovalRequest,
8080
Tool,
81+
ToolRunFunction,
82+
ToolRunComputerAction,
8183
)
8284
from .tool_context import ToolContext
8385
from .tracing import (
@@ -126,19 +128,6 @@ class ToolRunHandoff:
126128
handoff: Handoff
127129
tool_call: ResponseFunctionToolCall
128130

129-
130-
@dataclass
131-
class ToolRunFunction:
132-
tool_call: ResponseFunctionToolCall
133-
function_tool: FunctionTool
134-
135-
136-
@dataclass
137-
class ToolRunComputerAction:
138-
tool_call: ResponseComputerToolCall
139-
computer_tool: ComputerTool
140-
141-
142131
@dataclass
143132
class ToolRunMCPApprovalRequest:
144133
request_item: McpApprovalRequest
@@ -544,9 +533,9 @@ async def execute_function_tool_calls(
544533
context_wrapper: RunContextWrapper[TContext],
545534
config: RunConfig,
546535
) -> list[FunctionToolResult]:
547-
async def run_single_tool(
548-
func_tool: FunctionTool, tool_call: ResponseFunctionToolCall
549-
) -> Any:
536+
async def run_single_tool(action: ToolRunFunction) -> Any:
537+
func_tool = action.function_tool
538+
tool_call = action.tool_call
550539
with function_span(func_tool.name) as span_fn:
551540
tool_context = ToolContext.from_agent_context(
552541
context_wrapper,
@@ -557,9 +546,9 @@ async def run_single_tool(
557546
span_fn.span_data.input = tool_call.arguments
558547
try:
559548
_, _, result = await asyncio.gather(
560-
hooks.on_tool_start(tool_context, agent, func_tool),
549+
hooks.on_tool_start(context_wrapper, agent, action),
561550
(
562-
agent.hooks.on_tool_start(tool_context, agent, func_tool)
551+
agent.hooks.on_tool_start(context_wrapper, agent, action)
563552
if agent.hooks
564553
else _coro.noop_coroutine()
565554
),
@@ -591,8 +580,7 @@ async def run_single_tool(
591580

592581
tasks = []
593582
for tool_run in tool_runs:
594-
function_tool = tool_run.function_tool
595-
tasks.append(run_single_tool(function_tool, tool_run.tool_call))
583+
tasks.append(run_single_tool(tool_run))
596584

597585
results = await asyncio.gather(*tasks)
598586

@@ -1039,9 +1027,9 @@ async def execute(
10391027
)
10401028

10411029
_, _, output = await asyncio.gather(
1042-
hooks.on_tool_start(context_wrapper, agent, action.computer_tool),
1030+
hooks.on_tool_start(context_wrapper, agent, action),
10431031
(
1044-
agent.hooks.on_tool_start(context_wrapper, agent, action.computer_tool)
1032+
agent.hooks.on_tool_start(context_wrapper, agent, action)
10451033
if agent.hooks
10461034
else _coro.noop_coroutine()
10471035
),

src/agents/lifecycle.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .agent import Agent, AgentBase
66
from .run_context import RunContextWrapper, TContext
7-
from .tool import Tool
7+
from .tool import Tool, Action
88

99
TAgent = TypeVar("TAgent", bound=AgentBase, default=AgentBase)
1010

@@ -39,8 +39,8 @@ async def on_handoff(
3939
async def on_tool_start(
4040
self,
4141
context: RunContextWrapper[TContext],
42-
agent: TAgent,
43-
tool: Tool,
42+
agent: Agent[TContext],
43+
action: Action,
4444
) -> None:
4545
"""Called before a tool is invoked."""
4646
pass
@@ -90,8 +90,8 @@ async def on_handoff(
9090
async def on_tool_start(
9191
self,
9292
context: RunContextWrapper[TContext],
93-
agent: TAgent,
94-
tool: Tool,
93+
agent: Agent[TContext],
94+
action: Action,
9595
) -> None:
9696
"""Called before a tool is invoked."""
9797
pass

src/agents/tool.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from openai.types.responses.web_search_tool_param import UserLocation
1717
from pydantic import ValidationError
1818
from typing_extensions import Concatenate, NotRequired, ParamSpec, TypedDict
19+
from openai.types.responses import ResponseComputerToolCall, ResponseFunctionToolCall
1920

2021
from . import _debug
2122
from .computer import AsyncComputer, Computer
@@ -281,6 +282,19 @@ def name(self):
281282
]
282283
"""A tool that can be used in an agent."""
283284

285+
@dataclass
286+
class ToolRunFunction:
287+
tool_call: ResponseFunctionToolCall
288+
function_tool: FunctionTool
289+
290+
291+
@dataclass
292+
class ToolRunComputerAction:
293+
tool_call: ResponseComputerToolCall
294+
computer_tool: ComputerTool
295+
296+
Action = Union[ToolRunFunction, ToolRunComputerAction]
297+
"""An action that can be performed by an agent. It contains the tool call and the tool"""
284298

285299
def default_tool_error_function(ctx: RunContextWrapper[Any], error: Exception) -> str:
286300
"""The default tool error function, which just returns a generic error message."""

tests/test_agent_hooks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88
from typing_extensions import TypedDict
99

10-
from agents.agent import Agent
10+
from agents.agent import Agent, Action
1111
from agents.lifecycle import AgentHooks
1212
from agents.run import Runner
1313
from agents.run_context import RunContextWrapper, TContext
@@ -53,7 +53,7 @@ async def on_tool_start(
5353
self,
5454
context: RunContextWrapper[TContext],
5555
agent: Agent[TContext],
56-
tool: Tool,
56+
action: Action,
5757
) -> None:
5858
self.events["on_tool_start"] += 1
5959

tests/test_computer_action.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from agents import (
2626
Agent,
27+
Action,
2728
AgentHooks,
2829
AsyncComputer,
2930
Computer,
@@ -32,7 +33,8 @@
3233
RunContextWrapper,
3334
RunHooks,
3435
)
35-
from agents._run_impl import ComputerAction, RunImpl, ToolRunComputerAction
36+
from agents._run_impl import ComputerAction, RunImpl
37+
from agents.tool import ToolRunComputerAction
3638
from agents.items import ToolCallOutputItem
3739
from agents.tool import ComputerToolSafetyCheckData
3840

@@ -224,9 +226,9 @@ def __init__(self) -> None:
224226
self.ended: list[tuple[Agent[Any], Any, str]] = []
225227

226228
async def on_tool_start(
227-
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any
229+
self, context: RunContextWrapper[Any], agent: Agent[Any], action: Action,
228230
) -> None:
229-
self.started.append((agent, tool))
231+
self.started.append((agent, action.computer_tool))
230232

231233
async def on_tool_end(
232234
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any, result: str
@@ -243,9 +245,9 @@ def __init__(self) -> None:
243245
self.ended: list[tuple[Agent[Any], Any, str]] = []
244246

245247
async def on_tool_start(
246-
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any
248+
self, context: RunContextWrapper[Any], agent: Agent[Any], action: Action,
247249
) -> None:
248-
self.started.append((agent, tool))
250+
self.started.append((agent, action.computer_tool))
249251

250252
async def on_tool_end(
251253
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any, result: str

0 commit comments

Comments
 (0)