Created
December 6, 2025 07:25
-
-
Save kennethleungty/c47c452f1d8082d8d03191b6898f9580 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def supervisor_command_node( | |
| state: SupervisorState, | |
| ) -> Command[Literal["transaction_history_agent", "property_profile_agent"]]: | |
| """Supervisor node using Command for routing + state updates. | |
| Uses structured output to: | |
| 1. Determine which agent to route to | |
| 2. Extract property_name from the query | |
| 3. Update state via Command.update | |
| This showcases Command's power: routing + state updates in one function. | |
| """ | |
| messages = state["messages"] | |
| # Get structured decision from supervisor LLM | |
| llm_with_structure = supervisor_llm.with_structured_output(SupervisorDecision) | |
| decision: SupervisorDecision = llm_with_structure.invoke( | |
| [SystemMessage(content=SUPERVISOR_PROMPT)] + messages | |
| ) | |
| # If no routing needed, supervisor handles directly | |
| if decision.next_agent == "none": | |
| return Command[str]( | |
| goto="__end__", | |
| update={"messages": [AIMessage(content=decision.response, name="supervisor")]}, | |
| ) | |
| # Build state update dict with extracted context | |
| update_dict = {"messages": [AIMessage(content=f"Routing to {decision.next_agent}", name="supervisor")]} | |
| if decision.property_name: | |
| update_dict["property_name"] = decision.property_name | |
| return Command(goto=decision.next_agent, update=update_dict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment