| from typing import Annotated | |
| from langchain_core.tools import InjectedToolCallId, tool | |
| from langgraph.graph import MessagesState | |
| from langgraph.prebuilt import InjectedState | |
| from langgraph.types import Command | |
| def create_handoff_tool(*, agent_name: str, description: str | None = None): | |
| name = f"transfer_to_{agent_name}" | |
| description = description or f"Ask {agent_name} for help." | |
| def handoff_tool( | |
| state: Annotated[MessagesState, InjectedState], | |
| tool_call_id: Annotated[str, InjectedToolCallId], | |
| ) -> Command: | |
| tool_message = { | |
| "role": "tool", | |
| "content": f"Successfully transferred to {agent_name}", | |
| "name": name, | |
| "tool_call_id": tool_call_id, | |
| } | |
| return Command( | |
| goto=agent_name, | |
| update={**state, "messages": state["messages"] + [tool_message]}, | |
| graph=Command.PARENT, | |
| ) | |
| return handoff_tool | |