fix: Use the agent name as the author of the audio event

Co-authored-by: Xiang (Sean) Zhou <seanzhougoogle@google.com>
PiperOrigin-RevId: 855789317
This commit is contained in:
Xiang (Sean) Zhou
2026-01-13 10:38:53 -08:00
committed by Copybara-Service
parent f668a5de44
commit ab62b1bffd
2 changed files with 58 additions and 1 deletions
@@ -181,10 +181,16 @@ class AudioCacheManager:
artifact_ref = f'artifact://{invocation_context.app_name}/{invocation_context.user_id}/{invocation_context.session.id}/_adk_live/{filename}#{revision_id}'
# Create event with file data reference to add to session
# For model events, author should be the agent name, not the role
author = (
invocation_context.agent.name
if audio_cache[0].role == 'model'
else audio_cache[0].role
)
audio_event = Event(
id=Event.new_id(),
invocation_id=invocation_context.invocation_id,
author=audio_cache[0].role,
author=author,
content=types.Content(
role=audio_cache[0].role,
parts=[
@@ -387,3 +387,54 @@ class TestAudioCacheManager:
assert filename.startswith(
f'adk_live_audio_storage_input_audio_{expected_timestamp_ms}'
)
@pytest.mark.asyncio
async def test_flush_event_author_for_user_audio(self):
"""Test that flushed user audio events have 'user' as author."""
invocation_context = await testing_utils.create_invocation_context(
testing_utils.create_test_agent()
)
# Set up mock artifact service
mock_artifact_service = AsyncMock()
mock_artifact_service.save_artifact.return_value = 123
invocation_context.artifact_service = mock_artifact_service
# Cache user input audio
input_blob = types.Blob(data=b'user_audio_data', mime_type='audio/pcm')
self.manager.cache_audio(invocation_context, input_blob, 'input')
# Flush cache and get events
events = await self.manager.flush_caches(
invocation_context, flush_user_audio=True, flush_model_audio=False
)
# Verify event author is 'user' for user audio
assert len(events) == 1
assert events[0].author == 'user'
assert events[0].content.role == 'user'
@pytest.mark.asyncio
async def test_flush_event_author_for_model_audio(self):
"""Test that flushed model audio events have agent name as author, not 'model'."""
agent = testing_utils.create_test_agent(name='my_test_agent')
invocation_context = await testing_utils.create_invocation_context(agent)
# Set up mock artifact service
mock_artifact_service = AsyncMock()
mock_artifact_service.save_artifact.return_value = 123
invocation_context.artifact_service = mock_artifact_service
# Cache model output audio
output_blob = types.Blob(data=b'model_audio_data', mime_type='audio/wav')
self.manager.cache_audio(invocation_context, output_blob, 'output')
# Flush cache and get events
events = await self.manager.flush_caches(
invocation_context, flush_user_audio=False, flush_model_audio=True
)
# Verify event author is agent name (not 'model') for model audio
assert len(events) == 1
assert events[0].author == 'my_test_agent' # Agent name, not 'model'
assert events[0].content.role == 'model' # Role is still 'model'