2424from ...agents .invocation_context import InvocationContext
2525from ...events .event import Event
2626from ...models .llm_request import LlmRequest
27- from ...tools .function_tool import FunctionTool
2827from ...tools .tool_context import ToolContext
29- from ...tools .transfer_to_agent_tool import transfer_to_agent
28+ from ...tools .transfer_to_agent_tool import TransferToAgentTool
3029from ._base_llm_processor import BaseLlmRequestProcessor
3130
3231if typing .TYPE_CHECKING :
@@ -50,13 +49,18 @@ async def run_async(
5049 if not transfer_targets :
5150 return
5251
52+ transfer_to_agent_tool = TransferToAgentTool (
53+ agent_names = [agent .name for agent in transfer_targets ]
54+ )
55+
5356 llm_request .append_instructions ([
5457 _build_target_agents_instructions (
55- invocation_context .agent , transfer_targets
58+ transfer_to_agent_tool .name ,
59+ invocation_context .agent ,
60+ transfer_targets ,
5661 )
5762 ])
5863
59- transfer_to_agent_tool = FunctionTool (func = transfer_to_agent )
6064 tool_context = ToolContext (invocation_context )
6165 await transfer_to_agent_tool .process_llm_request (
6266 tool_context = tool_context , llm_request = llm_request
@@ -80,10 +84,13 @@ def _build_target_agents_info(target_agent: BaseAgent) -> str:
8084
8185
8286def _build_target_agents_instructions (
83- agent : LlmAgent , target_agents : list [BaseAgent ]
87+ tool_name : str ,
88+ agent : LlmAgent ,
89+ target_agents : list [BaseAgent ],
8490) -> str :
8591 # Build list of available agent names for the NOTE
86- # target_agents already includes parent agent if applicable, so no need to add it again
92+ # target_agents already includes parent agent if applicable,
93+ # so no need to add it again
8794 available_agent_names = [target_agent .name for target_agent in target_agents ]
8895
8996 # Sort for consistency
@@ -101,15 +108,16 @@ def _build_target_agents_instructions(
101108 _build_target_agents_info (target_agent ) for target_agent in target_agents
102109])}
103110
104- If you are the best to answer the question according to your description, you
105- can answer it.
111+ If you are the best to answer the question according to your description,
112+ you can answer it.
106113
107114If another agent is better for answering the question according to its
108- description, call `{ _TRANSFER_TO_AGENT_FUNCTION_NAME } ` function to transfer the
109- question to that agent. When transferring, do not generate any text other than
110- the function call.
115+ description, call `{ tool_name } ` function to transfer the question to that
116+ agent. When transferring, do not generate any text other than the function
117+ call.
111118
112- **NOTE**: the only available agents for `{ _TRANSFER_TO_AGENT_FUNCTION_NAME } ` function are { formatted_agent_names } .
119+ **NOTE**: the only available agents for `{ tool_name } ` function are
120+ { formatted_agent_names } .
113121"""
114122
115123 if agent .parent_agent and not agent .disallow_transfer_to_parent :
@@ -119,9 +127,6 @@ def _build_target_agents_instructions(
119127 return si
120128
121129
122- _TRANSFER_TO_AGENT_FUNCTION_NAME = transfer_to_agent .__name__
123-
124-
125130def _get_transfer_targets (agent : LlmAgent ) -> list [BaseAgent ]:
126131 from ...agents .llm_agent import LlmAgent
127132
0 commit comments