Skip to content

Commit 851654f

Browse files
mdabydeencopybara-github
authored andcommitted
refactor: improve transfer type safety
Merge #6324 PiperOrigin-RevId: 952179046
1 parent 088be86 commit 851654f

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

src/google/adk/flows/llm_flows/agent_transfer.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from __future__ import annotations
1818

1919
import typing
20-
from typing import Any
2120
from typing import AsyncGenerator
21+
from typing import Sequence
2222

2323
from typing_extensions import override
2424

@@ -30,8 +30,8 @@
3030
from ._base_llm_processor import BaseLlmRequestProcessor
3131

3232
if typing.TYPE_CHECKING:
33-
from ...agents import BaseAgent
34-
from ...agents import LlmAgent
33+
from ...agents.base_agent import BaseAgent
34+
from ...agents.llm_agent import LlmAgent
3535

3636

3737
class _AgentTransferLlmRequestProcessor(BaseLlmRequestProcessor):
@@ -72,8 +72,12 @@ async def run_async(
7272
request_processor = _AgentTransferLlmRequestProcessor()
7373

7474

75-
def _build_target_agents_info(target_agent: Any) -> str:
76-
# TODO: Refactor the annotation of the parameters
75+
class _AgentLike(typing.Protocol):
76+
name: str
77+
description: str
78+
79+
80+
def _build_target_agents_info(target_agent: _AgentLike) -> str:
7781
return f"""
7882
Agent name: {target_agent.name}
7983
Agent description: {target_agent.description}
@@ -85,17 +89,16 @@ def _build_target_agents_info(target_agent: Any) -> str:
8589

8690
def _build_transfer_instruction_body(
8791
tool_name: str,
88-
target_agents: list[Any],
92+
target_agents: Sequence[_AgentLike],
8993
) -> str:
9094
"""Build the core transfer instruction text.
91-
TODO: Refactor the annotation of the parameters
9295
9396
This is the agent-tree-agnostic portion of transfer instructions. It
94-
works with any objects having ``.name`` and ``.description`` attributes
97+
works with any BaseAgent implementation.
9598
9699
Args:
97100
tool_name: The name of the transfer tool (e.g. 'transfer_to_agent').
98-
target_agents: Objects with ``.name`` and ``.description``.
101+
target_agents: Agents available as transfer targets.
99102
100103
Returns:
101104
Instruction text for the LLM about agent transfers.
@@ -128,8 +131,8 @@ def _build_transfer_instruction_body(
128131

129132
def _build_transfer_instructions(
130133
tool_name: str,
131-
agent: 'LlmAgent',
132-
target_agents: list['BaseAgent'],
134+
agent: LlmAgent,
135+
target_agents: Sequence[BaseAgent],
133136
) -> str:
134137
"""Build instructions for agent transfer (agent-tree variant).
135138

0 commit comments

Comments
 (0)