refactor: Rename long util function name in runner.py and move it to functions.py

PiperOrigin-RevId: 774880990
This commit is contained in:
Xiang (Sean) Zhou
2025-06-23 12:24:10 -07:00
committed by Copybara-Service
parent 29cd183aa1
commit 120cbabeb2
4 changed files with 170 additions and 206 deletions
-171
View File
@@ -18,7 +18,6 @@ from google.adk.agents.base_agent import BaseAgent
from google.adk.agents.llm_agent import LlmAgent
from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService
from google.adk.events.event import Event
from google.adk.runners import _find_function_call_event_if_last_event_is_function_response
from google.adk.runners import Runner
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.adk.sessions.session import Session
@@ -73,176 +72,6 @@ class MockLlmAgent(LlmAgent):
)
class TestFindFunctionCallEventIfLastEventIsFunctionResponse:
"""Tests for _find_function_call_event_if_last_event_is_function_response function."""
def test_no_function_response_in_last_event(self):
"""Test when last event has no function response."""
session = Session(
id="test_session",
user_id="test_user",
app_name="test_app",
events=[
Event(
invocation_id="inv1",
author="user",
content=types.Content(
role="user", parts=[types.Part(text="Hello")]
),
)
],
)
result = _find_function_call_event_if_last_event_is_function_response(
session
)
assert result is None
def test_empty_session_events(self):
"""Test when session has no events."""
session = Session(
id="test_session", user_id="test_user", app_name="test_app", events=[]
)
result = _find_function_call_event_if_last_event_is_function_response(
session
)
assert result is None
def test_last_event_has_function_response_but_no_matching_call(self):
"""Test when last event has function response but no matching call found."""
# Create a function response
function_response = types.FunctionResponse(
id="func_123", name="test_func", response={}
)
session = Session(
id="test_session",
user_id="test_user",
app_name="test_app",
events=[
Event(
invocation_id="inv1",
author="agent1",
content=types.Content(
role="model",
parts=[types.Part(text="Some other response")],
),
),
Event(
invocation_id="inv2",
author="user",
content=types.Content(
role="user",
parts=[types.Part(function_response=function_response)],
),
),
],
)
result = _find_function_call_event_if_last_event_is_function_response(
session
)
assert result is None
def test_last_event_has_function_response_with_matching_call(self):
"""Test when last event has function response with matching function call."""
# Create a function call
function_call = types.FunctionCall(id="func_123", name="test_func", args={})
# Create a function response with matching ID
function_response = types.FunctionResponse(
id="func_123", name="test_func", response={}
)
call_event = Event(
invocation_id="inv1",
author="agent1",
content=types.Content(
role="model", parts=[types.Part(function_call=function_call)]
),
)
response_event = Event(
invocation_id="inv2",
author="user",
content=types.Content(
role="user", parts=[types.Part(function_response=function_response)]
),
)
session = Session(
id="test_session",
user_id="test_user",
app_name="test_app",
events=[call_event, response_event],
)
result = _find_function_call_event_if_last_event_is_function_response(
session
)
assert result == call_event
def test_last_event_has_multiple_function_responses(self):
"""Test when last event has multiple function responses."""
# Create function calls
function_call1 = types.FunctionCall(
id="func_123", name="test_func1", args={}
)
function_call2 = types.FunctionCall(
id="func_456", name="test_func2", args={}
)
# Create function responses
function_response1 = types.FunctionResponse(
id="func_123", name="test_func1", response={}
)
function_response2 = types.FunctionResponse(
id="func_456", name="test_func2", response={}
)
call_event1 = Event(
invocation_id="inv1",
author="agent1",
content=types.Content(
role="model", parts=[types.Part(function_call=function_call1)]
),
)
call_event2 = Event(
invocation_id="inv2",
author="agent2",
content=types.Content(
role="model", parts=[types.Part(function_call=function_call2)]
),
)
response_event = Event(
invocation_id="inv3",
author="user",
content=types.Content(
role="user",
parts=[
types.Part(function_response=function_response1),
types.Part(function_response=function_response2),
],
),
)
session = Session(
id="test_session",
user_id="test_user",
app_name="test_app",
events=[call_event1, call_event2, response_event],
)
# Should return the first matching function call event found
result = _find_function_call_event_if_last_event_is_function_response(
session
)
assert result == call_event1 # First match (func_123)
class TestRunnerFindAgentToRun:
"""Tests for Runner._find_agent_to_run method."""