use crate::core::agent_node::AgentNode;
use crate::core::agent_state::AgentState;
use crate::core::error::LangGraphError;
use futures::future::join_all;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
pub struct StateGraph<S: AgentState + Send + Sync> {
nodes: HashMap<String, Box<dyn AgentNode<S>>>,
edges: HashMap<String, HashSet<String>>,
conditional_edges: HashMap<String, Vec<Box<dyn Fn(&S) -> String>>>,
start_node: String,
end_node: String,
max_steps: usize,
}
impl<S: AgentState + Send + Sync> StateGraph<S> {
pub(crate) fn new(
max_steps: usize,
nodes: HashMap<String, Box<dyn AgentNode<S>>>,
edges: HashMap<String, HashSet<String>>,
conditional_edges: HashMap<String, Vec<Box<dyn Fn(&S) -> String>>>,
start_node: String,
end_node: String,
) -> Self {
StateGraph {
max_steps,
nodes,
edges,
conditional_edges,
start_node,
end_node,
}
}
pub async fn invoke(&self, state: Arc<S>) -> Result<(), LangGraphError> {
let mut current = HashSet::new();
current.insert(self.start_node.to_string());
let mut step_count: usize = 0;
let max_steps = self.max_steps;
loop {
if step_count >= max_steps {
break;
}
step_count = step_count + 1;
if self.is_end_node(current.clone())? {
current.remove(&self.end_node);
}
if current.is_empty() {
break;
}
if !self.is_start_node(current.clone())? {
let nodes = self.get_node_by_keys(¤t)?;
if nodes.is_empty() {
return Err(LangGraphError::NotFound(format!(
"Dead-end: nodes {:?} have no find by keys",
current
)));
}
self.batch_apply(nodes, Arc::clone(&state)).await?;
}
let next = self.get_next_node_key(¤t, state.as_ref())?;
if next.is_empty() {
return Err(LangGraphError::GraphError(format!(
"Dead-end: nodes {:?} have no outgoing edges",
current
)));
}
current = next;
}
Ok(())
}
fn get_node_by_key(&self, key: &String) -> Result<&Box<dyn AgentNode<S>>, LangGraphError> {
Ok(self
.nodes
.get(key)
.ok_or_else(|| LangGraphError::NotFound(format!("Key '{}' not found", key)))?)
}
fn get_node_by_keys(
&self,
keys: &HashSet<String>,
) -> Result<Vec<&Box<dyn AgentNode<S>>>, LangGraphError> {
let mut nodes = Vec::new();
for key in keys {
let node = self.get_node_by_key(key)?;
nodes.push(node);
}
Ok(nodes)
}
fn is_start_node(&self, keys: HashSet<String>) -> Result<bool, LangGraphError> {
if keys.is_empty() {
return Ok(false);
}
if keys.contains(&self.start_node) {
return Ok(true);
}
Ok(false)
}
fn is_end_node(&self, keys: HashSet<String>) -> Result<bool, LangGraphError> {
if keys.is_empty() {
return Ok(false);
}
if keys.contains(&self.end_node) {
return Ok(true);
}
Ok(false)
}
fn get_next_node_key(
&self,
keys: &HashSet<String>,
state: &S,
) -> Result<HashSet<String>, LangGraphError> {
if keys.is_empty() {
return Ok(HashSet::new());
}
let mut next_node_keys = HashSet::new();
for key in keys {
if let Some(targets) = self.edges.get(key) {
if !targets.is_empty() {
for target in targets {
next_node_keys.insert(target.clone());
}
}
}
if let Some(routers) = self.conditional_edges.get(key) {
for router in routers {
next_node_keys.insert(router(state));
}
}
}
Ok(next_node_keys)
}
async fn batch_apply(
&self,
nodes: Vec<&Box<dyn AgentNode<S>>>,
state: Arc<S>,
) -> Result<(), LangGraphError> {
let futures: Vec<_> = nodes
.into_iter()
.map(|node| {
let state_clone = Arc::clone(&state);
async move { node.apply(state_clone).await }
})
.collect();
let results = join_all(futures).await;
for result in results {
result?; }
Ok(())
}
}