use crate::constants::{end, start, NodeId};
use crate::edge::{Edge, EdgeRouter};
use crate::error::{RegulaError, Result};
use crate::node::{BoxedNode, Node};
use crate::state::GraphState;
use indexmap::IndexMap;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
#[derive(Clone)]
pub struct StateGraph<S: GraphState> {
nodes: IndexMap<NodeId, BoxedNode<S>>,
edges: Vec<Edge<S>>,
entry_point: Option<NodeId>,
interrupt_before: HashSet<NodeId>,
interrupt_after: HashSet<NodeId>,
}
impl<S: GraphState> StateGraph<S> {
pub fn new() -> Self {
Self {
nodes: IndexMap::new(),
edges: Vec::new(),
entry_point: None,
interrupt_before: HashSet::new(),
interrupt_after: HashSet::new(),
}
}
pub fn add_node<N: Node<S> + 'static>(mut self, name: impl Into<NodeId>, node: N) -> Self {
let name = name.into();
if self.nodes.contains_key(&name) {
panic!("Node '{}' already exists in graph", name);
}
self.nodes.insert(name, Arc::new(node));
self
}
pub fn try_add_node<N: Node<S> + 'static>(
mut self,
name: impl Into<NodeId>,
node: N,
) -> Result<Self> {
let name = name.into();
if self.nodes.contains_key(&name) {
return Err(RegulaError::DuplicateNode(name.to_string()));
}
self.nodes.insert(name, Arc::new(node));
Ok(self)
}
pub fn add_edge(mut self, from: impl Into<NodeId>, to: impl Into<NodeId>) -> Self {
self.edges.push(Edge::normal(from, to));
self
}
pub fn add_conditional_edges<R: EdgeRouter<S> + 'static>(
mut self,
from: impl Into<NodeId>,
router: R,
) -> Self {
self.edges.push(Edge::conditional(from, router));
self
}
pub fn add_conditional_edges_with_map<R: EdgeRouter<S> + 'static>(
mut self,
from: impl Into<NodeId>,
router: R,
path_map: HashMap<String, NodeId>,
) -> Self {
self.edges
.push(Edge::conditional_with_map(from, router, path_map));
self
}
pub fn set_entry_point(mut self, node: impl Into<NodeId>) -> Self {
let node_id = node.into();
self.entry_point = Some(node_id.clone());
self.edges.push(Edge::normal(start(), node_id));
self
}
pub fn set_finish_point(mut self, node: impl Into<NodeId>) -> Self {
self.edges.push(Edge::normal(node, end()));
self
}
pub fn interrupt_before(mut self, node: impl Into<NodeId>) -> Self {
self.interrupt_before.insert(node.into());
self
}
pub fn interrupt_after(mut self, node: impl Into<NodeId>) -> Self {
self.interrupt_after.insert(node.into());
self
}
pub fn node_names(&self) -> impl Iterator<Item = &NodeId> {
self.nodes.keys()
}
pub fn has_node(&self, name: &str) -> bool {
self.nodes.keys().any(|k| k.as_str() == name)
}
fn validate(&self) -> Result<()> {
if self.nodes.is_empty() {
return Err(RegulaError::NoEntryPoint);
}
let has_entry = self.edges.iter().any(|e| e.from().is_start());
if !has_entry && self.entry_point.is_none() {
return Err(RegulaError::NoEntryPoint);
}
for edge in &self.edges {
let from = edge.from();
if !from.is_start() && !self.nodes.contains_key(from) {
return Err(RegulaError::InvalidEdgeSource(from.to_string()));
}
if let Edge::Normal { to, .. } = edge {
if !to.is_end() && !self.nodes.contains_key(to) {
return Err(RegulaError::InvalidEdgeTarget(to.to_string()));
}
}
}
for node_id in self.nodes.keys() {
let has_outgoing = self.edges.iter().any(|e| e.from() == node_id);
if !has_outgoing {
#[cfg(debug_assertions)]
tracing::debug!(
"Node '{}' has no outgoing edges - ensure it returns a Command with goto",
node_id
);
}
}
let mut reachable = HashSet::new();
self.collect_reachable_nodes(&mut reachable);
for node_id in self.nodes.keys() {
if !reachable.contains(node_id) {
return Err(RegulaError::UnreachableNode(node_id.to_string()));
}
}
Ok(())
}
fn collect_reachable_nodes(&self, reachable: &mut HashSet<NodeId>) {
let mut queue: Vec<NodeId> = Vec::new();
for edge in &self.edges {
if edge.from().is_start() {
if let Edge::Normal { to, .. } = edge {
queue.push(to.clone());
}
}
}
while let Some(node) = queue.pop() {
if node.is_end() || reachable.contains(&node) {
continue;
}
reachable.insert(node.clone());
for edge in &self.edges {
if edge.from() == &node {
match edge {
Edge::Normal { to, .. } => {
if !to.is_end() {
queue.push(to.clone());
}
}
Edge::Conditional { path_map, .. } => {
if let Some(map) = path_map {
for target in map.values() {
if !target.is_end() {
queue.push(target.clone());
}
}
}
for other_node in self.nodes.keys() {
queue.push(other_node.clone());
}
}
}
}
}
}
}
pub fn compile(self, config: CompileConfig) -> Result<CompiledStateGraph<S>> {
self.validate()?;
Ok(CompiledStateGraph {
nodes: self.nodes,
edges: self.edges,
entry_point: self.entry_point,
interrupt_before: self.interrupt_before,
interrupt_after: self.interrupt_after,
config,
})
}
}
impl<S: GraphState> Default for StateGraph<S> {
fn default() -> Self {
Self::new()
}
}
impl<S: GraphState> std::fmt::Debug for StateGraph<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StateGraph")
.field("nodes", &self.nodes.keys().collect::<Vec<_>>())
.field("edges", &self.edges)
.field("entry_point", &self.entry_point)
.finish()
}
}
#[derive(Clone, Debug, Default)]
pub struct CompileConfig {
pub max_iterations: Option<usize>,
pub validate: bool,
pub debug: bool,
}
impl CompileConfig {
pub fn new() -> Self {
Self {
max_iterations: Some(100),
validate: true,
debug: false,
}
}
pub fn with_max_iterations(mut self, max: usize) -> Self {
self.max_iterations = Some(max);
self
}
pub fn without_validation(mut self) -> Self {
self.validate = false;
self
}
pub fn with_debug(mut self) -> Self {
self.debug = true;
self
}
}
#[derive(Clone)]
pub struct CompiledStateGraph<S: GraphState> {
pub(crate) nodes: IndexMap<NodeId, BoxedNode<S>>,
pub(crate) edges: Vec<Edge<S>>,
pub(crate) entry_point: Option<NodeId>,
pub(crate) interrupt_before: HashSet<NodeId>,
pub(crate) interrupt_after: HashSet<NodeId>,
pub(crate) config: CompileConfig,
}
impl<S: GraphState> CompiledStateGraph<S> {
pub fn get_node(&self, name: &NodeId) -> Option<&BoxedNode<S>> {
self.nodes.get(name)
}
pub fn node_names(&self) -> impl Iterator<Item = &NodeId> {
self.nodes.keys()
}
pub fn edges_from(&self, node: &NodeId) -> Vec<&Edge<S>> {
self.edges.iter().filter(|e| e.from() == node).collect()
}
pub fn entry_points(&self) -> Vec<NodeId> {
self.edges
.iter()
.filter_map(|e| {
if e.from().is_start() {
if let Edge::Normal { to, .. } = e {
return Some(to.clone());
}
}
None
})
.collect()
}
pub fn should_interrupt_before(&self, node: &NodeId) -> bool {
self.interrupt_before.contains(node)
}
pub fn should_interrupt_after(&self, node: &NodeId) -> bool {
self.interrupt_after.contains(node)
}
pub fn max_iterations(&self) -> Option<usize> {
self.config.max_iterations
}
pub fn next_nodes(&self, from: &NodeId) -> Option<Vec<NodeId>> {
let edges: Vec<_> = self.edges_from(from);
if edges.is_empty() {
return None;
}
if edges.iter().any(|e| e.is_conditional()) {
return None;
}
let targets: Vec<NodeId> = edges
.iter()
.filter_map(|e| {
if let Edge::Normal { to, .. } = e {
Some(to.clone())
} else {
None
}
})
.collect();
if targets.is_empty() {
None
} else {
Some(targets)
}
}
pub fn to_mermaid(&self) -> String {
let mut lines = vec!["graph TD".to_string()];
for name in self.nodes.keys() {
lines.push(format!(" {}[{}]", name, name));
}
for edge in &self.edges {
match edge {
Edge::Normal { from, to } => {
let from_str = if from.is_start() {
"START"
} else {
from.as_str()
};
let to_str = if to.is_end() { "END" } else { to.as_str() };
lines.push(format!(" {} --> {}", from_str, to_str));
}
Edge::Conditional { from, .. } => {
lines.push(format!(" {} -.-> |?| ...", from));
}
}
}
lines.push(" START((START))".to_string());
lines.push(" END((END))".to_string());
lines.join("\n")
}
}
impl<S: GraphState> std::fmt::Debug for CompiledStateGraph<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CompiledStateGraph")
.field("nodes", &self.nodes.keys().collect::<Vec<_>>())
.field("edges", &self.edges.len())
.field("entry_point", &self.entry_point)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::channel::ChannelSpec;
use crate::edge::router_fn;
use crate::node::{node_fn, NodeOutput};
use crate::RunnableConfig;
#[derive(Clone, serde::Serialize, serde::Deserialize)]
struct TestState {
counter: i32,
done: bool,
}
impl GraphState for TestState {
fn channels() -> HashMap<String, ChannelSpec> {
let mut channels = HashMap::new();
channels.insert("counter".to_string(), ChannelSpec::LastValue);
channels.insert("done".to_string(), ChannelSpec::LastValue);
channels
}
}
#[test]
fn test_state_graph_new() {
let graph = StateGraph::<TestState>::new();
assert_eq!(graph.nodes.len(), 0);
assert_eq!(graph.edges.len(), 0);
}
#[test]
fn test_state_graph_add_node() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let graph = StateGraph::<TestState>::new().add_node("agent", node);
assert!(graph.has_node("agent"));
}
#[test]
#[should_panic(expected = "already exists")]
fn test_state_graph_duplicate_node() {
let node1 = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let node2 = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let _ = StateGraph::<TestState>::new()
.add_node("agent", node1)
.add_node("agent", node2); }
#[test]
fn test_state_graph_try_add_node_duplicate() {
let node1 = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let node2 = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let result = StateGraph::<TestState>::new()
.add_node("agent", node1)
.try_add_node("agent", node2);
assert!(result.is_err());
}
#[test]
fn test_state_graph_add_edges() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let graph = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge(start(), "agent")
.add_edge("agent", end());
assert_eq!(graph.edges.len(), 2);
}
#[test]
fn test_state_graph_conditional_edges() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let router = router_fn(|state: &TestState| {
if state.done {
RouteOutput::end()
} else {
RouteOutput::one("agent")
}
});
let graph = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge(start(), "agent")
.add_conditional_edges("agent", router);
assert_eq!(graph.edges.len(), 2);
}
#[test]
fn test_state_graph_compile_success() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let result = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge(start(), "agent")
.add_edge("agent", end())
.compile(Default::default());
assert!(result.is_ok());
}
#[test]
fn test_state_graph_compile_no_entry() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let result = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge("agent", end())
.compile(Default::default());
assert!(result.is_err());
}
#[test]
fn test_state_graph_compile_invalid_edge_source() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let result = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge(start(), "agent")
.add_edge("nonexistent", "agent") .compile(Default::default());
assert!(result.is_err());
}
#[test]
fn test_state_graph_compile_invalid_edge_target() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let result = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge(start(), "agent")
.add_edge("agent", "nonexistent") .compile(Default::default());
assert!(result.is_err());
}
#[test]
fn test_compiled_graph_entry_points() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let graph = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge(start(), "agent")
.add_edge("agent", end())
.compile(Default::default())
.unwrap();
let entry_points = graph.entry_points();
assert_eq!(entry_points.len(), 1);
assert_eq!(entry_points[0], "agent");
}
#[test]
fn test_compiled_graph_to_mermaid() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let graph = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge(start(), "agent")
.add_edge("agent", end())
.compile(Default::default())
.unwrap();
let mermaid = graph.to_mermaid();
assert!(mermaid.contains("graph TD"));
assert!(mermaid.contains("START"));
assert!(mermaid.contains("END"));
assert!(mermaid.contains("agent"));
}
#[test]
fn test_set_entry_point() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let graph = StateGraph::<TestState>::new()
.add_node("agent", node)
.set_entry_point("agent")
.add_edge("agent", end())
.compile(Default::default())
.unwrap();
assert_eq!(graph.entry_points(), vec![NodeId::from("agent")]);
}
#[test]
fn test_interrupt_points() {
let node = node_fn(|_: &TestState, _: &RunnableConfig| async {
Ok(NodeOutput::none())
});
let graph = StateGraph::<TestState>::new()
.add_node("agent", node)
.add_edge(start(), "agent")
.add_edge("agent", end())
.interrupt_before("agent")
.interrupt_after("agent")
.compile(Default::default())
.unwrap();
assert!(graph.should_interrupt_before(&NodeId::from("agent")));
assert!(graph.should_interrupt_after(&NodeId::from("agent")));
}
}