diff --git a/src/google/adk/flows/llm_flows/contents.py b/src/google/adk/flows/llm_flows/contents.py index 9b7ef9e121..9037a37789 100644 --- a/src/google/adk/flows/llm_flows/contents.py +++ b/src/google/adk/flows/llm_flows/contents.py @@ -557,8 +557,9 @@ def _get_current_turn_contents( # Find the latest event that starts the current turn and process from there for i in range(len(events) - 1, -1, -1): event = events[i] - if _should_include_event_in_context(current_branch, event) and ( - event.author == 'user' or _is_other_agent_reply(agent_name, event) + if (_should_include_event_in_context(current_branch, event) + and (event.author == "user" or _is_other_agent_reply(agent_name, event)) + and not _is_direct_transfer(event) ): return _get_contents( current_branch, @@ -570,6 +571,22 @@ def _get_current_turn_contents( return [] +def _is_direct_transfer(event : Event) -> bool: + "Check whether the event is a direct transfer event." + + return bool( + event.actions.transfer_to_agent + or ( + event.content + and event.content.parts + and any( + p.function_call and p.function_call.name == 'transfer_to_agent' + for p in event.content.parts + ) + ) + ) + + def _is_other_agent_reply(current_agent_name: str, event: Event) -> bool: """Whether the event is a reply from another agent.""" return bool(