-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_agent_tool_event_callback.py
More file actions
294 lines (234 loc) · 7.48 KB
/
test_agent_tool_event_callback.py
File metadata and controls
294 lines (234 loc) · 7.48 KB
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
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for AgentTool event_callback functionality."""
from google.adk.agents.llm_agent import Agent
from google.adk.events.event import Event
from google.adk.tools.agent_tool import AgentTool
from google.genai.types import Part
from pytest import mark
from .. import testing_utils
@mark.asyncio
async def test_event_callback_sync_invocation():
"""Test that synchronous event callbacks are invoked correctly."""
captured_events = []
def sync_callback(event: Event) -> None:
captured_events.append(event)
function_call = Part.from_function_call(
name='tool_agent', args={'request': 'test1'}
)
mock_model = testing_utils.MockModel.create(
responses=[
function_call,
'response1',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent, event_callback=sync_callback)],
)
runner = testing_utils.InMemoryRunner(root_agent)
runner.run('test1')
# Verify that events were captured
assert len(captured_events) > 0
# All captured items should be Event instances
assert all(isinstance(e, Event) for e in captured_events)
# Should capture the tool agent's response
assert any(
e.content and any(p.text == 'response1' for p in e.content.parts)
for e in captured_events
)
@mark.asyncio
async def test_event_callback_async_invocation():
"""Test that asynchronous event callbacks are invoked correctly."""
captured_events = []
async def async_callback(event: Event) -> None:
captured_events.append(event)
function_call = Part.from_function_call(
name='tool_agent', args={'request': 'test1'}
)
mock_model = testing_utils.MockModel.create(
responses=[
function_call,
'response1',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent, event_callback=async_callback)],
)
runner = testing_utils.InMemoryRunner(root_agent)
runner.run('test1')
# Verify that events were captured
assert len(captured_events) > 0
# All captured items should be Event instances
assert all(isinstance(e, Event) for e in captured_events)
# Should capture the tool agent's response
assert any(
e.content and any(p.text == 'response1' for p in e.content.parts)
for e in captured_events
)
@mark.asyncio
async def test_event_callback_receives_all_events():
"""Test that callbacks receive all child agent events."""
captured_events = []
def capture_callback(event: Event) -> None:
captured_events.append(event)
function_call = Part.from_function_call(
name='tool_agent', args={'request': 'test1'}
)
mock_model = testing_utils.MockModel.create(
responses=[
function_call,
'response1',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent, event_callback=capture_callback)],
)
runner = testing_utils.InMemoryRunner(root_agent)
runner.run('test1')
# Verify multiple events were captured (should include at least response)
assert len(captured_events) >= 1
# Check that events have expected structure
for event in captured_events:
assert isinstance(event, Event)
assert hasattr(event, 'author')
assert hasattr(event, 'content')
assert hasattr(event, 'actions')
@mark.asyncio
async def test_event_callback_backward_compatibility():
"""Test AgentTool works without event_callback (backward compatibility)."""
function_call = Part.from_function_call(
name='tool_agent', args={'request': 'test1'}
)
function_response = Part.from_function_response(
name='tool_agent', response={'result': 'response1'}
)
mock_model = testing_utils.MockModel.create(
responses=[
function_call,
'response1',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
# Create AgentTool without event_callback parameter
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent)],
)
runner = testing_utils.InMemoryRunner(root_agent)
# Should work without errors
result = testing_utils.simplify_events(runner.run('test1'))
assert result == [
('root_agent', function_call),
('root_agent', function_response),
('root_agent', 'response2'),
]
@mark.asyncio
async def test_event_callback_can_access_event_metadata():
"""Test that callbacks can access event metadata like grounding_metadata."""
captured_metadata = []
def metadata_callback(event: Event) -> None:
if event.grounding_metadata:
captured_metadata.append(event.grounding_metadata)
function_call = Part.from_function_call(
name='tool_agent', args={'request': 'test1'}
)
mock_model = testing_utils.MockModel.create(
responses=[
function_call,
'response1',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent, event_callback=metadata_callback)],
)
runner = testing_utils.InMemoryRunner(root_agent)
runner.run('test1')
# Test passes if no errors occur (grounding_metadata access works)
# Note: captured_metadata may be empty if mock doesn't provide metadata
@mark.asyncio
async def test_event_callback_with_multiple_tool_calls():
"""Test that callbacks work correctly with multiple tool invocations."""
captured_events = []
def capture_callback(event: Event) -> None:
captured_events.append(event)
function_call_1 = Part.from_function_call(
name='tool_agent', args={'request': 'call1'}
)
function_call_2 = Part.from_function_call(
name='tool_agent', args={'request': 'call2'}
)
mock_model = testing_utils.MockModel.create(
responses=[
function_call_1,
'response1',
function_call_2,
'response2',
'final',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent, event_callback=capture_callback)],
)
runner = testing_utils.InMemoryRunner(root_agent)
runner.run('test1')
# Should capture events from both tool invocations
assert len(captured_events) >= 2
# Verify we got responses from both calls
response_texts = []
for event in captured_events:
if event.content:
for part in event.content.parts:
if part.text:
response_texts.append(part.text)
assert 'response1' in response_texts
assert 'response2' in response_texts