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
@@ -519,3 +519,35 @@ def merge_parallel_function_response_events(
# Use the base_event as the timestamp
merged_event.timestamp = base_event.timestamp
return merged_event
def find_matching_function_call(
events: list[Event],
) -> Optional[Event]:
"""Finds the function call event that matches the function response id of the last event."""
if not events:
return None
last_event = events[-1]
if (
last_event.content
and last_event.content.parts
and any(part.function_response for part in last_event.content.parts)
):
function_call_id = next(
part.function_response.id
for part in last_event.content.parts
if part.function_response
)
for i in range(len(events) - 2, -1, -1):
event = events[i]
# looking for the system long running request euc function call
function_calls = event.get_function_calls()
if not function_calls:
continue
for function_call in function_calls:
if function_call.id == function_call_id:
return event
return None
+2 -35
View File
@@ -36,6 +36,7 @@ from .artifacts.in_memory_artifact_service import InMemoryArtifactService
from .auth.credential_service.base_credential_service import BaseCredentialService
from .code_executors.built_in_code_executor import BuiltInCodeExecutor
from .events.event import Event
from .flows.llm_flows.functions import find_matching_function_call
from .memory.base_memory_service import BaseMemoryService
from .memory.in_memory_memory_service import InMemoryMemoryService
from .platform.thread import create_thread
@@ -354,9 +355,7 @@ class Runner:
# the agent that returned the corressponding function call regardless the
# type of the agent. e.g. a remote a2a agent may surface a credential
# request as a special long running function tool call.
event = _find_function_call_event_if_last_event_is_function_response(
session
)
event = find_matching_function_call(session.events)
if event and event.author:
return root_agent.find_agent(event.author)
for event in filter(lambda e: e.author != 'user', reversed(session.events)):
@@ -538,35 +537,3 @@ class InMemoryRunner(Runner):
session_service=self._in_memory_session_service,
memory_service=InMemoryMemoryService(),
)
def _find_function_call_event_if_last_event_is_function_response(
session: Session,
) -> Optional[Event]:
events = session.events
if not events:
return None
last_event = events[-1]
if (
last_event.content
and last_event.content.parts
and any(part.function_response for part in last_event.content.parts)
):
function_call_id = next(
part.function_response.id
for part in last_event.content.parts
if part.function_response
)
for i in range(len(events) - 2, -1, -1):
event = events[i]
# looking for the system long running request euc function call
function_calls = event.get_function_calls()
if not function_calls:
continue
for function_call in function_calls:
if function_call.id == function_call_id:
return event
return None