forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_computer_action.py
311 lines (262 loc) · 11.2 KB
/
test_computer_action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""Unit tests for the ComputerAction methods in `agents._run_impl`.
These confirm that the correct computer action method is invoked for each action type and
that screenshots are taken and wrapped appropriately, and that the execute function invokes
hooks and returns the expected ToolCallOutputItem."""
from typing import Any
import pytest
from openai.types.responses.response_computer_tool_call import (
ActionClick,
ActionDoubleClick,
ActionDrag,
ActionDragPath,
ActionKeypress,
ActionMove,
ActionScreenshot,
ActionScroll,
ActionType,
ActionWait,
ResponseComputerToolCall,
)
from agents import (
Agent,
AgentHooks,
AsyncComputer,
Computer,
ComputerTool,
RunConfig,
RunContextWrapper,
RunHooks,
)
from agents._run_impl import ComputerAction, ToolRunComputerAction
from agents.items import ToolCallOutputItem
class LoggingComputer(Computer):
"""A `Computer` implementation that logs calls to its methods for verification in tests."""
def __init__(self, screenshot_return: str = "screenshot"):
self.calls: list[tuple[str, tuple[Any, ...]]] = []
self._screenshot_return = screenshot_return
@property
def environment(self):
return "mac"
@property
def dimensions(self) -> tuple[int, int]:
return (800, 600)
def screenshot(self) -> str:
self.calls.append(("screenshot", ()))
return self._screenshot_return
def click(self, x: int, y: int, button: str) -> None:
self.calls.append(("click", (x, y, button)))
def double_click(self, x: int, y: int) -> None:
self.calls.append(("double_click", (x, y)))
def scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> None:
self.calls.append(("scroll", (x, y, scroll_x, scroll_y)))
def type(self, text: str) -> None:
self.calls.append(("type", (text,)))
def wait(self) -> None:
self.calls.append(("wait", ()))
def move(self, x: int, y: int) -> None:
self.calls.append(("move", (x, y)))
def keypress(self, keys: list[str]) -> None:
self.calls.append(("keypress", (keys,)))
def drag(self, path: list[tuple[int, int]]) -> None:
self.calls.append(("drag", (tuple(path),)))
class LoggingAsyncComputer(AsyncComputer):
"""An `AsyncComputer` implementation that logs calls to its methods for verification."""
def __init__(self, screenshot_return: str = "async_screenshot"):
self.calls: list[tuple[str, tuple[Any, ...]]] = []
self._screenshot_return = screenshot_return
@property
def environment(self):
return "mac"
@property
def dimensions(self) -> tuple[int, int]:
return (800, 600)
async def screenshot(self) -> str:
self.calls.append(("screenshot", ()))
return self._screenshot_return
async def click(self, x: int, y: int, button: str) -> None:
self.calls.append(("click", (x, y, button)))
async def double_click(self, x: int, y: int) -> None:
self.calls.append(("double_click", (x, y)))
async def scroll(self, x: int, y: int, scroll_x: int, scroll_y: int) -> None:
self.calls.append(("scroll", (x, y, scroll_x, scroll_y)))
async def type(self, text: str) -> None:
self.calls.append(("type", (text,)))
async def wait(self) -> None:
self.calls.append(("wait", ()))
async def move(self, x: int, y: int) -> None:
self.calls.append(("move", (x, y)))
async def keypress(self, keys: list[str]) -> None:
self.calls.append(("keypress", (keys,)))
async def drag(self, path: list[tuple[int, int]]) -> None:
self.calls.append(("drag", (tuple(path),)))
@pytest.mark.asyncio
@pytest.mark.parametrize(
"action,expected_call",
[
(ActionClick(type="click", x=10, y=21, button="left"), ("click", (10, 21, "left"))),
(ActionDoubleClick(type="double_click", x=42, y=47), ("double_click", (42, 47))),
(
ActionDrag(type="drag", path=[ActionDragPath(x=1, y=2), ActionDragPath(x=3, y=4)]),
("drag", (((1, 2), (3, 4)),)),
),
(ActionKeypress(type="keypress", keys=["a", "b"]), ("keypress", (["a", "b"],))),
(ActionMove(type="move", x=100, y=200), ("move", (100, 200))),
(ActionScreenshot(type="screenshot"), ("screenshot", ())),
(
ActionScroll(type="scroll", x=1, y=2, scroll_x=3, scroll_y=4),
("scroll", (1, 2, 3, 4)),
),
(ActionType(type="type", text="hello"), ("type", ("hello",))),
(ActionWait(type="wait"), ("wait", ())),
],
)
async def test_get_screenshot_sync_executes_action_and_takes_screenshot(
action: Any, expected_call: tuple[str, tuple[Any, ...]]
) -> None:
"""For each action type, assert that the corresponding computer method is invoked
and that a screenshot is taken and returned."""
computer = LoggingComputer(screenshot_return="synthetic")
tool_call = ResponseComputerToolCall(
id="c1",
type="computer_call",
action=action,
call_id="c1",
pending_safety_checks=[],
status="completed",
)
screenshot_output = await ComputerAction._get_screenshot_sync(computer, tool_call)
# The last call is always to screenshot()
if isinstance(action, ActionScreenshot):
# Screenshot is taken twice: initial explicit call plus final capture.
assert computer.calls == [("screenshot", ()), ("screenshot", ())]
else:
assert computer.calls == [expected_call, ("screenshot", ())]
assert screenshot_output == "synthetic"
@pytest.mark.asyncio
@pytest.mark.parametrize(
"action,expected_call",
[
(ActionClick(type="click", x=2, y=3, button="right"), ("click", (2, 3, "right"))),
(ActionDoubleClick(type="double_click", x=12, y=13), ("double_click", (12, 13))),
(
ActionDrag(type="drag", path=[ActionDragPath(x=5, y=6), ActionDragPath(x=6, y=7)]),
("drag", (((5, 6), (6, 7)),)),
),
(ActionKeypress(type="keypress", keys=["ctrl", "c"]), ("keypress", (["ctrl", "c"],))),
(ActionMove(type="move", x=8, y=9), ("move", (8, 9))),
(ActionScreenshot(type="screenshot"), ("screenshot", ())),
(
ActionScroll(type="scroll", x=9, y=8, scroll_x=7, scroll_y=6),
("scroll", (9, 8, 7, 6)),
),
(ActionType(type="type", text="world"), ("type", ("world",))),
(ActionWait(type="wait"), ("wait", ())),
],
)
async def test_get_screenshot_async_executes_action_and_takes_screenshot(
action: Any, expected_call: tuple[str, tuple[Any, ...]]
) -> None:
"""For each action type on an `AsyncComputer`, the corresponding coroutine should be awaited
and a screenshot taken."""
computer = LoggingAsyncComputer(screenshot_return="async_return")
assert computer.environment == "mac"
assert computer.dimensions == (800, 600)
tool_call = ResponseComputerToolCall(
id="c2",
type="computer_call",
action=action,
call_id="c2",
pending_safety_checks=[],
status="completed",
)
screenshot_output = await ComputerAction._get_screenshot_async(computer, tool_call)
if isinstance(action, ActionScreenshot):
assert computer.calls == [("screenshot", ()), ("screenshot", ())]
else:
assert computer.calls == [expected_call, ("screenshot", ())]
assert screenshot_output == "async_return"
class LoggingRunHooks(RunHooks[Any]):
"""Capture on_tool_start and on_tool_end invocations."""
def __init__(self) -> None:
super().__init__()
self.started: list[tuple[Agent[Any], Any]] = []
self.ended: list[tuple[Agent[Any], Any, str]] = []
async def on_tool_start(
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any
) -> None:
self.started.append((agent, tool))
async def on_tool_end(
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any, result: str
) -> None:
self.ended.append((agent, tool, result))
class LoggingAgentHooks(AgentHooks[Any]):
"""Minimal override to capture agent's tool hook invocations."""
def __init__(self) -> None:
super().__init__()
self.started: list[tuple[Agent[Any], Any]] = []
self.ended: list[tuple[Agent[Any], Any, str]] = []
async def on_tool_start(
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any
) -> None:
self.started.append((agent, tool))
async def on_tool_end(
self, context: RunContextWrapper[Any], agent: Agent[Any], tool: Any, result: str
) -> None:
self.ended.append((agent, tool, result))
@pytest.mark.asyncio
async def test_execute_invokes_hooks_and_returns_tool_call_output() -> None:
# ComputerAction.execute should invoke lifecycle hooks and return a proper ToolCallOutputItem.
computer = LoggingComputer(screenshot_return="xyz")
comptool = ComputerTool(computer=computer)
# Create a dummy click action to trigger a click and screenshot.
action = ActionClick(type="click", x=1, y=2, button="left")
tool_call = ResponseComputerToolCall(
id="tool123",
type="computer_call",
action=action,
call_id="tool123",
pending_safety_checks=[],
status="completed",
)
tool_call.call_id = "tool123"
# Wrap tool call in ToolRunComputerAction
tool_run = ToolRunComputerAction(tool_call=tool_call, computer_tool=comptool)
# Setup agent and hooks.
agent = Agent(name="test_agent", tools=[comptool])
# Attach per-agent hooks as well as global run hooks.
agent_hooks = LoggingAgentHooks()
agent.hooks = agent_hooks
run_hooks = LoggingRunHooks()
context_wrapper: RunContextWrapper[Any] = RunContextWrapper(context=None)
# Execute the computer action.
output_item = await ComputerAction.execute(
agent=agent,
action=tool_run,
hooks=run_hooks,
context_wrapper=context_wrapper,
config=RunConfig(),
)
# Both global and per-agent hooks should have been called once.
assert len(run_hooks.started) == 1 and len(agent_hooks.started) == 1
assert len(run_hooks.ended) == 1 and len(agent_hooks.ended) == 1
# The hook invocations should refer to our agent and tool.
assert run_hooks.started[0][0] is agent
assert run_hooks.ended[0][0] is agent
assert run_hooks.started[0][1] is comptool
assert run_hooks.ended[0][1] is comptool
# The result passed to on_tool_end should be the raw screenshot string.
assert run_hooks.ended[0][2] == "xyz"
assert agent_hooks.ended[0][2] == "xyz"
# The computer should have performed a click then a screenshot.
assert computer.calls == [("click", (1, 2, "left")), ("screenshot", ())]
# The returned item should include the agent, output string, and a ComputerCallOutput.
assert output_item.agent is agent
assert isinstance(output_item, ToolCallOutputItem)
assert output_item.output == "data:image/png;base64,xyz"
raw = output_item.raw_item
# Raw item is a dict-like mapping with expected output fields.
assert isinstance(raw, dict)
assert raw["type"] == "computer_call_output"
assert raw["output"]["type"] == "computer_screenshot"
assert "image_url" in raw["output"]
assert raw["output"]["image_url"].endswith("xyz")