-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathtest_contents_other_agent.py
More file actions
494 lines (439 loc) · 16.4 KB
/
test_contents_other_agent.py
File metadata and controls
494 lines (439 loc) · 16.4 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# Copyright 2026 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.
"""Behavioral tests for other agent message processing in contents module."""
from google.adk.agents.llm_agent import Agent
from google.adk.agents.run_config import RunConfig
from google.adk.events.event import Event
from google.adk.flows.llm_flows.contents import request_processor
from google.adk.models.llm_request import LlmRequest
from google.genai import types
import pytest
from ... import testing_utils
@pytest.mark.asyncio
async def test_other_agent_message_appears_as_user_context():
"""Test that messages from other agents appear as user context."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Add event from another agent
other_agent_event = Event(
invocation_id="test_inv",
author="other_agent",
content=types.ModelContent("Hello from other agent"),
)
invocation_context.session.events = [other_agent_event]
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify the other agent's message is presented as user context
assert llm_request.contents[0].role == "user"
assert llm_request.contents[0].parts == [
types.Part(text="For context:"),
types.Part(text="[other_agent] said: Hello from other agent"),
]
@pytest.mark.asyncio
async def test_other_agent_thoughts_are_excluded():
"""Test that thoughts from other agents are excluded from context."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Add event from other agent with both regular text and thoughts
other_agent_event = Event(
invocation_id="test_inv",
author="other_agent",
content=types.ModelContent([
types.Part(text="Public message", thought=False),
types.Part(text="Private thought", thought=True),
types.Part(text="Another public message"),
]),
)
invocation_context.session.events = [other_agent_event]
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify only non-thought parts are included (thoughts excluded)
assert llm_request.contents[0].role == "user"
assert llm_request.contents[0].parts == [
types.Part(text="For context:"),
types.Part(text="[other_agent] said: Public message"),
types.Part(text="[other_agent] said: Another public message"),
]
@pytest.mark.asyncio
async def test_other_agent_thoughts_can_be_included_as_context():
"""Test opt-in inclusion of thoughts from other agents."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent,
run_config=RunConfig(include_thoughts_from_other_agents=True),
)
other_agent_event = Event(
invocation_id="test_inv",
author="other_agent",
content=types.ModelContent([
types.Part(text="Public message", thought=False),
types.Part(text="Private thought", thought=True),
types.Part(text="Another public message"),
]),
)
invocation_context.session.events = [other_agent_event]
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
assert llm_request.contents[0].role == "user"
assert llm_request.contents[0].parts == [
types.Part(text="For context:"),
types.Part(text="[other_agent] said: Public message"),
types.Part(text="[other_agent] thought: Private thought"),
types.Part(text="[other_agent] said: Another public message"),
]
@pytest.mark.asyncio
async def test_other_agent_thought_only_message_can_be_included_as_context():
"""Test opt-in inclusion of thought-only messages from other agents."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent,
run_config=RunConfig(include_thoughts_from_other_agents=True),
)
other_agent_event = Event(
invocation_id="test_inv",
author="other_agent",
content=types.ModelContent([
types.Part(text="First private thought", thought=True),
types.Part(text="Second private thought", thought=True),
]),
)
invocation_context.session.events = [other_agent_event]
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
assert llm_request.contents[0].role == "user"
assert llm_request.contents[0].parts == [
types.Part(text="For context:"),
types.Part(text="[other_agent] thought: First private thought"),
types.Part(text="[other_agent] thought: Second private thought"),
]
@pytest.mark.asyncio
async def test_other_agent_thoughts_excluded_from_current_turn_only_context():
"""Test include_contents='none' does not include other-agent thoughts."""
agent = Agent(
model="gemini-2.5-flash",
name="current_agent",
include_contents="none",
)
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent,
run_config=RunConfig(include_thoughts_from_other_agents=True),
)
invocation_context.session.events = [
Event(
invocation_id="inv1",
author="user",
content=types.UserContent("Earlier user message"),
),
Event(
invocation_id="inv2",
author="other_agent",
content=types.ModelContent([
types.Part(text="Private thought", thought=True),
types.Part(text="Visible handoff"),
]),
),
]
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
assert llm_request.contents == [
types.Content(
role="user",
parts=[
types.Part(text="For context:"),
types.Part(text="[other_agent] said: Visible handoff"),
],
)
]
@pytest.mark.asyncio
async def test_other_agent_function_calls():
"""Test that function calls from other agents are preserved in context."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Add event from other agent with function call
function_call = types.FunctionCall(
id="func_123", name="search_tool", args={"query": "test query"}
)
other_agent_event = Event(
invocation_id="test_inv",
author="other_agent",
content=types.ModelContent([types.Part(function_call=function_call)]),
)
invocation_context.session.events = [other_agent_event]
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify function call is presented as context
assert llm_request.contents[0].role == "user"
assert llm_request.contents[0].parts == [
types.Part(text="For context:"),
types.Part(
text="""\
[other_agent] called tool `search_tool` with parameters: {'query': 'test query'}"""
),
]
@pytest.mark.asyncio
async def test_other_agent_function_responses():
"""Test that function responses from other agents are properly formatted."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Add event from other agent with function response
function_response = types.FunctionResponse(
id="func_123",
name="search_tool",
response={"results": ["item1", "item2"]},
)
other_agent_event = Event(
invocation_id="test_inv",
author="other_agent",
content=types.Content(
role="user", parts=[types.Part(function_response=function_response)]
),
)
invocation_context.session.events = [other_agent_event]
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify function response is presented as context
assert llm_request.contents[0].role == "user"
assert llm_request.contents[0].parts == [
types.Part(text="For context:"),
types.Part(
text=(
"[other_agent] `search_tool` tool returned result: {'results':"
" ['item1', 'item2']}"
)
),
]
@pytest.mark.asyncio
async def test_other_agent_function_call_response():
"""Test function call and response sequence from other agents."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Add function call event from other agent
function_call = types.FunctionCall(
id="func_123", name="calc_tool", args={"query": "6x7"}
)
call_event = Event(
invocation_id="test_inv1",
author="other_agent",
content=types.ModelContent([
types.Part(text="Let me calculate this"),
types.Part(function_call=function_call),
]),
)
# Add function response event
function_response = types.FunctionResponse(
id="func_123", name="calc_tool", response={"result": 42}
)
response_event = Event(
invocation_id="test_inv2",
author="other_agent",
content=types.UserContent(
parts=[types.Part(function_response=function_response)]
),
)
invocation_context.session.events = [call_event, response_event]
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify function call and response are properly formatted
assert len(llm_request.contents) == 2
# Function call from other agent
assert llm_request.contents[0].role == "user"
assert llm_request.contents[0].parts == [
types.Part(text="For context:"),
types.Part(text="[other_agent] said: Let me calculate this"),
types.Part(
text=(
"[other_agent] called tool `calc_tool` with parameters: {'query':"
" '6x7'}"
)
),
]
# Function response from other agent
assert llm_request.contents[1].role == "user"
assert llm_request.contents[1].parts == [
types.Part(text="For context:"),
types.Part(
text="[other_agent] `calc_tool` tool returned result: {'result': 42}"
),
]
@pytest.mark.asyncio
async def test_other_agent_empty_content():
"""Test that other agent messages with only thoughts or empty content are filtered out."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Add events: user message, other agents with empty content, user message
events = [
Event(
invocation_id="inv1",
author="user",
content=types.UserContent("Hello"),
),
# Other agent with only thoughts
Event(
invocation_id="inv2",
author="other_agent1",
content=types.ModelContent([
types.Part(text="This is a private thought", thought=True),
types.Part(text="Another private thought", thought=True),
]),
),
# Other agent with empty text and thoughts
Event(
invocation_id="inv3",
author="other_agent2",
content=types.ModelContent([
types.Part(text="", thought=False),
types.Part(text="Secret thought", thought=True),
]),
),
Event(
invocation_id="inv4",
author="user",
content=types.UserContent("World"),
),
]
invocation_context.session.events = events
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify empty content events are completely filtered out
assert llm_request.contents == [
types.UserContent("Hello"),
types.UserContent("World"),
]
@pytest.mark.asyncio
async def test_multiple_agents_in_conversation():
"""Test handling multiple agents in a conversation flow."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Create a multi-agent conversation
events = [
Event(
invocation_id="inv1",
author="user",
content=types.UserContent("Hello everyone"),
),
Event(
invocation_id="inv2",
author="agent1",
content=types.ModelContent("Hi from agent1"),
),
Event(
invocation_id="inv3",
author="agent2",
content=types.ModelContent("Hi from agent2"),
),
]
invocation_context.session.events = events
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify all messages are properly processed
assert len(llm_request.contents) == 3
# User message should remain as user
assert llm_request.contents[0] == types.UserContent("Hello everyone")
# Other agents' messages should be converted to user context
assert llm_request.contents[1].role == "user"
assert llm_request.contents[1].parts == [
types.Part(text="For context:"),
types.Part(text="[agent1] said: Hi from agent1"),
]
assert llm_request.contents[2].role == "user"
assert llm_request.contents[2].parts == [
types.Part(text="For context:"),
types.Part(text="[agent2] said: Hi from agent2"),
]
@pytest.mark.asyncio
async def test_current_agent_messages_not_converted():
"""Test that the current agent's own messages are not converted."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Add events from both current agent and other agent
events = [
Event(
invocation_id="inv1",
author="current_agent",
content=types.ModelContent("My own message"),
),
Event(
invocation_id="inv2",
author="other_agent",
content=types.ModelContent("Other agent message"),
),
]
invocation_context.session.events = events
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify current agent's message stays as model role
# and other agent's message is converted to user context
assert len(llm_request.contents) == 2
assert llm_request.contents[0] == types.ModelContent("My own message")
assert llm_request.contents[1].role == "user"
assert llm_request.contents[1].parts == [
types.Part(text="For context:"),
types.Part(text="[other_agent] said: Other agent message"),
]
@pytest.mark.asyncio
async def test_user_messages_preserved():
"""Test that user messages are preserved as-is."""
agent = Agent(model="gemini-2.5-flash", name="current_agent")
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)
# Add user message
user_event = Event(
invocation_id="inv1",
author="user",
content=types.UserContent("User message"),
)
invocation_context.session.events = [user_event]
# Process the request
async for _ in request_processor.run_async(invocation_context, llm_request):
pass
# Verify user message is preserved exactly
assert len(llm_request.contents) == 1
assert llm_request.contents[0] == types.UserContent("User message")