Repository navigation
Expand file tree
/
Copy pathtest_loop_agent.py
More file actions
251 lines (224 loc) · 7.61 KB
/
Copy pathtest_loop_agent.py
File metadata and controls
251 lines (224 loc) · 7.61 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
# 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.
"""Testings for the SequentialAgent."""
from typing import AsyncGenerator
from google.adk.agents.base_agent import BaseAgent
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.loop_agent import LoopAgent
from google.adk.agents.loop_agent import LoopAgentState
from google.adk.apps import ResumabilityConfig
from google.adk.events.event import Event
from google.adk.events.event_actions import EventActions
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.genai import types
import pytest
from typing_extensions import override
from .. import testing_utils
END_OF_AGENT = testing_utils.END_OF_AGENT
class _TestingAgent(BaseAgent):
@override
async def _run_async_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
yield Event(
author=self.name,
invocation_id=ctx.invocation_id,
content=types.Content(
parts=[types.Part(text=f'Hello, async {self.name}!')]
),
)
@override
async def _run_live_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
yield Event(
author=self.name,
invocation_id=ctx.invocation_id,
content=types.Content(
parts=[types.Part(text=f'Hello, live {self.name}!')]
),
)
class _TestingAgentWithEscalateAction(BaseAgent):
@override
async def _run_async_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
yield Event(
author=self.name,
invocation_id=ctx.invocation_id,
content=types.Content(
parts=[types.Part(text=f'Hello, async {self.name}!')]
),
actions=EventActions(escalate=True),
)
yield Event(
author=self.name,
invocation_id=ctx.invocation_id,
content=types.Content(
parts=[types.Part(text='I have done my job after escalation!!')]
),
)
async def _create_parent_invocation_context(
test_name: str, agent: BaseAgent, resumable: bool = False
) -> InvocationContext:
session_service = InMemorySessionService()
session = await session_service.create_session(
app_name='test_app', user_id='test_user'
)
return InvocationContext(
invocation_id=f'{test_name}_invocation_id',
agent=agent,
session=session,
session_service=session_service,
resumability_config=ResumabilityConfig(is_resumable=resumable),
)
@pytest.mark.asyncio
@pytest.mark.parametrize('resumable', [True, False])
async def test_run_async(request: pytest.FixtureRequest, resumable: bool):
agent = _TestingAgent(name=f'{request.function.__name__}_test_agent')
loop_agent = LoopAgent(
name=f'{request.function.__name__}_test_loop_agent',
max_iterations=2,
sub_agents=[
agent,
],
)
parent_ctx = await _create_parent_invocation_context(
request.function.__name__, loop_agent, resumable=resumable
)
events = [e async for e in loop_agent.run_async(parent_ctx)]
simplified_events = testing_utils.simplify_resumable_app_events(events)
if resumable:
expected_events = [
(
loop_agent.name,
{'current_sub_agent': agent.name, 'times_looped': 0},
),
(agent.name, f'Hello, async {agent.name}!'),
(
loop_agent.name,
{'current_sub_agent': agent.name, 'times_looped': 1},
),
(agent.name, f'Hello, async {agent.name}!'),
(loop_agent.name, END_OF_AGENT),
]
else:
expected_events = [
(agent.name, f'Hello, async {agent.name}!'),
(agent.name, f'Hello, async {agent.name}!'),
]
assert simplified_events == expected_events
@pytest.mark.asyncio
async def test_resume_async(request: pytest.FixtureRequest):
agent_1 = _TestingAgent(name=f'{request.function.__name__}_test_agent_1')
agent_2 = _TestingAgent(name=f'{request.function.__name__}_test_agent_2')
loop_agent = LoopAgent(
name=f'{request.function.__name__}_test_loop_agent',
max_iterations=2,
sub_agents=[
agent_1,
agent_2,
],
)
parent_ctx = await _create_parent_invocation_context(
request.function.__name__, loop_agent, resumable=True
)
parent_ctx.agent_states[loop_agent.name] = LoopAgentState(
current_sub_agent=agent_2.name, times_looped=1
).model_dump(mode='json')
events = [e async for e in loop_agent.run_async(parent_ctx)]
simplified_events = testing_utils.simplify_resumable_app_events(events)
expected_events = [
(agent_2.name, f'Hello, async {agent_2.name}!'),
(loop_agent.name, END_OF_AGENT),
]
assert simplified_events == expected_events
@pytest.mark.asyncio
async def test_run_async_skip_if_no_sub_agent(request: pytest.FixtureRequest):
loop_agent = LoopAgent(
name=f'{request.function.__name__}_test_loop_agent',
max_iterations=2,
sub_agents=[],
)
parent_ctx = await _create_parent_invocation_context(
request.function.__name__, loop_agent
)
events = [e async for e in loop_agent.run_async(parent_ctx)]
assert not events
@pytest.mark.asyncio
@pytest.mark.parametrize('resumable', [True, False])
async def test_run_async_with_escalate_action(
request: pytest.FixtureRequest, resumable: bool
):
non_escalating_agent = _TestingAgent(
name=f'{request.function.__name__}_test_non_escalating_agent'
)
escalating_agent = _TestingAgentWithEscalateAction(
name=f'{request.function.__name__}_test_escalating_agent'
)
ignored_agent = _TestingAgent(
name=f'{request.function.__name__}_test_ignored_agent'
)
loop_agent = LoopAgent(
name=f'{request.function.__name__}_test_loop_agent',
sub_agents=[non_escalating_agent, escalating_agent, ignored_agent],
)
parent_ctx = await _create_parent_invocation_context(
request.function.__name__, loop_agent, resumable=resumable
)
events = [e async for e in loop_agent.run_async(parent_ctx)]
simplified_events = testing_utils.simplify_resumable_app_events(events)
if resumable:
expected_events = [
(
loop_agent.name,
{
'current_sub_agent': non_escalating_agent.name,
'times_looped': 0,
},
),
(
non_escalating_agent.name,
f'Hello, async {non_escalating_agent.name}!',
),
(
loop_agent.name,
{'current_sub_agent': escalating_agent.name, 'times_looped': 0},
),
(
escalating_agent.name,
f'Hello, async {escalating_agent.name}!',
),
(
escalating_agent.name,
'I have done my job after escalation!!',
),
(loop_agent.name, END_OF_AGENT),
]
else:
expected_events = [
(
non_escalating_agent.name,
f'Hello, async {non_escalating_agent.name}!',
),
(
escalating_agent.name,
f'Hello, async {escalating_agent.name}!',
),
(
escalating_agent.name,
'I have done my job after escalation!!',
),
]
assert simplified_events == expected_events