Skip to content

Commit 656f211

Browse files
committed
rollback
Signed-off-by: myan <[email protected]>
1 parent 4187fba commit 656f211

8 files changed

+43
-39
lines changed

examples/basic/agent_lifecycle_example.py

+3-3
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

+3-3
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

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
WebSearchTool,
6363
default_tool_error_function,
6464
function_tool,
65+
Action,
6566
)
6667
from .tracing import (
6768
AgentSpanData,
@@ -209,6 +210,7 @@ def enable_verbose_stdout_logging():
209210
"Tool",
210211
"WebSearchTool",
211212
"function_tool",
213+
"Action",
212214
"Usage",
213215
"add_trace_processor",
214216
"agent_span",

src/agents/_run_impl.py

+9-23
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from .models.interface import ModelTracing
5353
from .run_context import RunContextWrapper, TContext
5454
from .stream_events import RunItemStreamEvent, StreamEvent
55-
from .tool import ComputerTool, FunctionTool, FunctionToolResult, Tool
55+
from .tool import ComputerTool, FunctionTool, FunctionToolResult, Tool, ToolRunFunction, ToolRunComputerAction
5656
from .tracing import (
5757
SpanError,
5858
Trace,
@@ -99,19 +99,6 @@ class ToolRunHandoff:
9999
handoff: Handoff
100100
tool_call: ResponseFunctionToolCall
101101

102-
103-
@dataclass
104-
class ToolRunFunction:
105-
tool_call: ResponseFunctionToolCall
106-
function_tool: FunctionTool
107-
108-
109-
@dataclass
110-
class ToolRunComputerAction:
111-
tool_call: ResponseComputerToolCall
112-
computer_tool: ComputerTool
113-
114-
115102
@dataclass
116103
class ProcessedResponse:
117104
new_items: list[RunItem]
@@ -429,17 +416,17 @@ async def execute_function_tool_calls(
429416
context_wrapper: RunContextWrapper[TContext],
430417
config: RunConfig,
431418
) -> list[FunctionToolResult]:
432-
async def run_single_tool(
433-
func_tool: FunctionTool, tool_call: ResponseFunctionToolCall
434-
) -> Any:
419+
async def run_single_tool(action: ToolRunFunction) -> Any:
420+
func_tool = action.function_tool
421+
tool_call = action.tool_call
435422
with function_span(func_tool.name) as span_fn:
436423
if config.trace_include_sensitive_data:
437424
span_fn.span_data.input = tool_call.arguments
438425
try:
439426
_, _, result = await asyncio.gather(
440-
hooks.on_tool_start(context_wrapper, agent, func_tool),
427+
hooks.on_tool_start(context_wrapper, agent, action),
441428
(
442-
agent.hooks.on_tool_start(context_wrapper, agent, func_tool)
429+
agent.hooks.on_tool_start(context_wrapper, agent, action)
443430
if agent.hooks
444431
else _coro.noop_coroutine()
445432
),
@@ -471,8 +458,7 @@ async def run_single_tool(
471458

472459
tasks = []
473460
for tool_run in tool_runs:
474-
function_tool = tool_run.function_tool
475-
tasks.append(run_single_tool(function_tool, tool_run.tool_call))
461+
tasks.append(run_single_tool(tool_run))
476462

477463
results = await asyncio.gather(*tasks)
478464

@@ -831,9 +817,9 @@ async def execute(
831817
)
832818

833819
_, _, output = await asyncio.gather(
834-
hooks.on_tool_start(context_wrapper, agent, action.computer_tool),
820+
hooks.on_tool_start(context_wrapper, agent, action),
835821
(
836-
agent.hooks.on_tool_start(context_wrapper, agent, action.computer_tool)
822+
agent.hooks.on_tool_start(context_wrapper, agent, action)
837823
if agent.hooks
838824
else _coro.noop_coroutine()
839825
),

src/agents/lifecycle.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .agent import Agent
44
from .run_context import RunContextWrapper, TContext
5-
from .tool import Tool
5+
from .tool import Tool, Action
66

77

88
class RunHooks(Generic[TContext]):
@@ -38,7 +38,7 @@ async def on_tool_start(
3838
self,
3939
context: RunContextWrapper[TContext],
4040
agent: Agent[TContext],
41-
tool: Tool,
41+
action: Action,
4242
) -> None:
4343
"""Called before a tool is invoked."""
4444
pass
@@ -89,7 +89,7 @@ async def on_tool_start(
8989
self,
9090
context: RunContextWrapper[TContext],
9191
agent: Agent[TContext],
92-
tool: Tool,
92+
action: Action,
9393
) -> None:
9494
"""Called before a tool is invoked."""
9595
pass

src/agents/tool.py

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from openai.types.responses.web_search_tool_param import UserLocation
1111
from pydantic import ValidationError
1212
from typing_extensions import Concatenate, ParamSpec
13+
from openai.types.responses import ResponseComputerToolCall, ResponseFunctionToolCall
1314

1415
from . import _debug
1516
from .computer import AsyncComputer, Computer
@@ -133,6 +134,19 @@ def name(self):
133134
Tool = Union[FunctionTool, FileSearchTool, WebSearchTool, ComputerTool]
134135
"""A tool that can be used in an agent."""
135136

137+
@dataclass
138+
class ToolRunFunction:
139+
tool_call: ResponseFunctionToolCall
140+
function_tool: FunctionTool
141+
142+
143+
@dataclass
144+
class ToolRunComputerAction:
145+
tool_call: ResponseComputerToolCall
146+
computer_tool: ComputerTool
147+
148+
Action = Union[ToolRunFunction, ToolRunComputerAction]
149+
"""An action that can be performed by an agent. It contains the tool call and the tool"""
136150

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

tests/test_agent_hooks.py

+2-2
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

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from agents import (
2525
Agent,
26+
Action,
2627
AgentHooks,
2728
AsyncComputer,
2829
Computer,
@@ -31,7 +32,8 @@
3132
RunContextWrapper,
3233
RunHooks,
3334
)
34-
from agents._run_impl import ComputerAction, ToolRunComputerAction
35+
from agents._run_impl import ComputerAction
36+
from agents.tool import ToolRunComputerAction
3537
from agents.items import ToolCallOutputItem
3638

3739

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

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

229231
async def on_tool_end(
230232
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any, result: str
@@ -241,9 +243,9 @@ def __init__(self) -> None:
241243
self.ended: list[tuple[Agent[Any], Any, str]] = []
242244

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

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

0 commit comments

Comments
 (0)