praxis_graph/
router.rs

1use crate::node::NodeType;
2use crate::types::GraphState;
3
4/// Decides which node to execute next based on current state
5pub trait Router: Send + Sync {
6    fn next(&self, state: &GraphState, current: NodeType) -> NextNode;
7}
8
9#[derive(Debug, Clone, PartialEq)]
10pub enum NextNode {
11    LLM,
12    Tool,
13    End,
14}
15
16/// Simple router implementing React agent pattern:
17/// LLM -> Tool (if tool_calls present) -> LLM -> END
18pub struct SimpleRouter;
19
20impl Router for SimpleRouter {
21    fn next(&self, state: &GraphState, current: NodeType) -> NextNode {
22        match current {
23            NodeType::LLM => {
24                // Check if last message has tool calls
25                if state.has_pending_tool_calls() {
26                    NextNode::Tool
27                } else {
28                    NextNode::End
29                }
30            }
31            NodeType::Tool => {
32                // Always return to LLM after executing tools
33                NextNode::LLM
34            }
35        }
36    }
37}
38