Skip to content

Commit c3287fd

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

File tree

6 files changed

+61
-24
lines changed

6 files changed

+61
-24
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}"
3535
)
3636

3737
async def on_tool_end(

examples/basic/lifecycle_example.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44

55
from pydantic import BaseModel
66

7-
from agents import Agent, RunContextWrapper, RunHooks, Runner, Tool, Usage, function_tool
8-
7+
from agents import (
8+
Agent,
9+
RunContextWrapper,
10+
RunHooks,
11+
Runner,
12+
Tool,
13+
Action,
14+
Usage,
15+
function_tool,
16+
ToolCallItem
17+
)
918

1019
class ExampleHooks(RunHooks):
1120
def __init__(self):
@@ -26,10 +35,15 @@ async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output: A
2635
f"### {self.event_counter}: Agent {agent.name} ended with output {output}. Usage: {self._usage_to_str(context.usage)}"
2736
)
2837

29-
async def on_tool_start(self, context: RunContextWrapper, agent: Agent, tool: Tool) -> None:
38+
async def on_tool_start(
39+
self,
40+
context: RunContextWrapper,
41+
agent: Agent,
42+
action: Action,
43+
) -> None:
3044
self.event_counter += 1
3145
print(
32-
f"### {self.event_counter}: Tool {tool.name} started. Usage: {self._usage_to_str(context.usage)}"
46+
f"### {self.event_counter}: Tool {action.function_tool.tool.name} started. Usage: {self._usage_to_str(context.usage)}"
3347
)
3448

3549
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/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

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

+33-12
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

@@ -127,10 +129,18 @@ async def drag(self, path: list[tuple[int, int]]) -> None:
127129
@pytest.mark.parametrize(
128130
"action,expected_call",
129131
[
130-
(ActionClick(type="click", x=10, y=21, button="left"), ("click", (10, 21, "left"))),
131-
(ActionDoubleClick(type="double_click", x=42, y=47), ("double_click", (42, 47))),
132132
(
133-
ActionDrag(type="drag", path=[ActionDragPath(x=1, y=2), ActionDragPath(x=3, y=4)]),
133+
ActionClick(type="click", x=10, y=21, button="left"),
134+
("click", (10, 21, "left")),
135+
),
136+
(
137+
ActionDoubleClick(type="double_click", x=42, y=47),
138+
("double_click", (42, 47)),
139+
),
140+
(
141+
ActionDrag(
142+
type="drag", path=[ActionDragPath(x=1, y=2), ActionDragPath(x=3, y=4)]
143+
),
134144
("drag", (((1, 2), (3, 4)),)),
135145
),
136146
(ActionKeypress(type="keypress", keys=["a", "b"]), ("keypress", (["a", "b"],))),
@@ -172,13 +182,24 @@ async def test_get_screenshot_sync_executes_action_and_takes_screenshot(
172182
@pytest.mark.parametrize(
173183
"action,expected_call",
174184
[
175-
(ActionClick(type="click", x=2, y=3, button="right"), ("click", (2, 3, "right"))),
176-
(ActionDoubleClick(type="double_click", x=12, y=13), ("double_click", (12, 13))),
177185
(
178-
ActionDrag(type="drag", path=[ActionDragPath(x=5, y=6), ActionDragPath(x=6, y=7)]),
186+
ActionClick(type="click", x=2, y=3, button="right"),
187+
("click", (2, 3, "right")),
188+
),
189+
(
190+
ActionDoubleClick(type="double_click", x=12, y=13),
191+
("double_click", (12, 13)),
192+
),
193+
(
194+
ActionDrag(
195+
type="drag", path=[ActionDragPath(x=5, y=6), ActionDragPath(x=6, y=7)]
196+
),
179197
("drag", (((5, 6), (6, 7)),)),
180198
),
181-
(ActionKeypress(type="keypress", keys=["ctrl", "c"]), ("keypress", (["ctrl", "c"],))),
199+
(
200+
ActionKeypress(type="keypress", keys=["ctrl", "c"]),
201+
("keypress", (["ctrl", "c"],)),
202+
),
182203
(ActionMove(type="move", x=8, y=9), ("move", (8, 9))),
183204
(ActionScreenshot(type="screenshot"), ("screenshot", ())),
184205
(
@@ -222,9 +243,9 @@ def __init__(self) -> None:
222243
self.ended: list[tuple[Agent[Any], Any, str]] = []
223244

224245
async def on_tool_start(
225-
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any
246+
self, context: RunContextWrapper[Any], agent: Agent[Any], action: Action,
226247
) -> None:
227-
self.started.append((agent, tool))
248+
self.started.append((agent, action.computer_tool))
228249

229250
async def on_tool_end(
230251
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any, result: str
@@ -241,9 +262,9 @@ def __init__(self) -> None:
241262
self.ended: list[tuple[Agent[Any], Any, str]] = []
242263

243264
async def on_tool_start(
244-
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any
265+
self, context: RunContextWrapper[Any], agent: Agent[Any], action: Action,
245266
) -> None:
246-
self.started.append((agent, tool))
267+
self.started.append((agent, action.computer_tool))
247268

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

0 commit comments

Comments
 (0)