|
| 1 | +"""Routing functions for conditional edges in the agent graph.""" |
| 2 | + |
| 3 | +from typing import Literal |
| 4 | + |
| 5 | +from langchain_core.messages import AIMessage, AnyMessage, ToolCall |
| 6 | +from uipath.agent.react import END_EXECUTION_TOOL, RAISE_ERROR_TOOL |
| 7 | + |
| 8 | +from .constants import MAX_SUCCESSIVE_COMPLETIONS |
| 9 | +from .exceptions import AgentNodeRoutingException |
| 10 | +from .state import AgentGraphNode, AgentGraphState |
| 11 | +from .utils import count_successive_completions |
| 12 | + |
| 13 | +FLOW_CONTROL_TOOLS = [END_EXECUTION_TOOL.name, RAISE_ERROR_TOOL.name] |
| 14 | + |
| 15 | + |
| 16 | +def __filter_control_flow_tool_calls( |
| 17 | + tool_calls: list[ToolCall], |
| 18 | +) -> list[ToolCall]: |
| 19 | + """Remove control flow tools when multiple tool calls exist.""" |
| 20 | + if len(tool_calls) <= 1: |
| 21 | + return tool_calls |
| 22 | + |
| 23 | + return [tc for tc in tool_calls if tc.get("name") not in FLOW_CONTROL_TOOLS] |
| 24 | + |
| 25 | + |
| 26 | +def __has_control_flow_tool(tool_calls: list[ToolCall]) -> bool: |
| 27 | + """Check if any tool call is of a control flow tool.""" |
| 28 | + return any(tc.get("name") in FLOW_CONTROL_TOOLS for tc in tool_calls) |
| 29 | + |
| 30 | + |
| 31 | +def __validate_last_message_is_AI(messages: list[AnyMessage]) -> AIMessage: |
| 32 | + """Validate and return last message from state. |
| 33 | +
|
| 34 | + Raises: |
| 35 | + AgentNodeRoutingException: If messages are empty or last message is not AIMessage |
| 36 | + """ |
| 37 | + if not messages: |
| 38 | + raise AgentNodeRoutingException( |
| 39 | + "No messages in state - cannot route after agent" |
| 40 | + ) |
| 41 | + |
| 42 | + last_message = messages[-1] |
| 43 | + if not isinstance(last_message, AIMessage): |
| 44 | + raise AgentNodeRoutingException( |
| 45 | + f"Last message is not AIMessage (type: {type(last_message).__name__}) - cannot route after agent" |
| 46 | + ) |
| 47 | + |
| 48 | + return last_message |
| 49 | + |
| 50 | + |
| 51 | +def route_agent( |
| 52 | + state: AgentGraphState, |
| 53 | +) -> list[str] | Literal[AgentGraphNode.AGENT, AgentGraphNode.TERMINATE]: |
| 54 | + """Route after agent: handles all routing logic including control flow detection. |
| 55 | +
|
| 56 | + Routing logic: |
| 57 | + 1. If multiple tool calls exist, filter out control flow tools (EndExecution, RaiseError) |
| 58 | + 2. If control flow tool(s) remain, route to TERMINATE |
| 59 | + 3. If regular tool calls remain, route to specific tool nodes (return list of tool names) |
| 60 | + 4. If no tool calls, handle successive completions |
| 61 | +
|
| 62 | + Returns: |
| 63 | + - list[str]: Tool node names for parallel execution |
| 64 | + - AgentGraphNode.AGENT: For successive completions |
| 65 | + - AgentGraphNode.TERMINATE: For control flow termination |
| 66 | +
|
| 67 | + Raises: |
| 68 | + AgentNodeRoutingException: When encountering unexpected state (empty messages, non-AIMessage, or excessive completions) |
| 69 | + """ |
| 70 | + messages = state.get("messages", []) |
| 71 | + last_message = __validate_last_message_is_AI(messages) |
| 72 | + |
| 73 | + tool_calls = list(last_message.tool_calls) if last_message.tool_calls else [] |
| 74 | + tool_calls = __filter_control_flow_tool_calls(tool_calls) |
| 75 | + |
| 76 | + if tool_calls and __has_control_flow_tool(tool_calls): |
| 77 | + return AgentGraphNode.TERMINATE |
| 78 | + |
| 79 | + if tool_calls: |
| 80 | + return [tc["name"] for tc in tool_calls] |
| 81 | + |
| 82 | + successive_completions = count_successive_completions(messages) |
| 83 | + |
| 84 | + if successive_completions > MAX_SUCCESSIVE_COMPLETIONS: |
| 85 | + raise AgentNodeRoutingException( |
| 86 | + f"Agent exceeded successive completions limit without producing tool calls " |
| 87 | + f"(completions: {successive_completions}, max: {MAX_SUCCESSIVE_COMPLETIONS}). " |
| 88 | + f"This should not happen as tool_choice='required' is enforced at the limit." |
| 89 | + ) |
| 90 | + |
| 91 | + if last_message.content: |
| 92 | + return AgentGraphNode.AGENT |
| 93 | + |
| 94 | + raise AgentNodeRoutingException( |
| 95 | + f"Agent produced empty response without tool calls " |
| 96 | + f"(completions: {successive_completions}, has_content: False)" |
| 97 | + ) |
0 commit comments