pub struct StateGraph<S: State> { /* private fields */ }Expand description
Builder for creating state-based graphs.
StateGraph provides a declarative API for building graphs where nodes communicate through shared state. It compiles into a CompiledGraph which can be executed.
§Example
use rust_langgraph::{StateGraph, Config};
let mut graph = StateGraph::new();
graph.add_node("process", |mut state: MyState, _config: &Config| async move {
state.count += 1;
Ok(state)
});
graph.set_entry_point("process");
graph.set_finish_point("process");
let app = graph.compile(None).unwrap();Implementations§
Source§impl<S: State> StateGraph<S>
impl<S: State> StateGraph<S>
Sourcepub fn add_node(
&mut self,
name: impl Into<String>,
node: impl Node<S> + 'static,
) -> &mut Self
pub fn add_node( &mut self, name: impl Into<String>, node: impl Node<S> + 'static, ) -> &mut Self
Sourcepub fn add_conditional_edges(
&mut self,
source: impl Into<String>,
branch: impl Branch<S> + 'static,
) -> &mut Self
pub fn add_conditional_edges( &mut self, source: impl Into<String>, branch: impl Branch<S> + 'static, ) -> &mut Self
Add conditional edges from a source node
The branch function examines state and returns which node(s) to route to next.
§Arguments
source- Source node namebranch- Branch logic that determines routing
§Example
let mut graph = StateGraph::new();
graph.add_conditional_edges(
"check",
|state: &MyState| async move {
if state.value > 0 {
Ok(BranchResult::single("positive"))
} else {
Ok(BranchResult::single("negative"))
}
}
);Sourcepub fn set_entry_point(&mut self, node: impl Into<String>) -> &mut Self
pub fn set_entry_point(&mut self, node: impl Into<String>) -> &mut Self
Set the entry point for the graph
This is the first node to execute when the graph is invoked.
Sourcepub fn set_finish_point(&mut self, node: impl Into<String>) -> &mut Self
pub fn set_finish_point(&mut self, node: impl Into<String>) -> &mut Self
Set a finish point for the graph
When execution reaches a finish point, the graph completes.
Sourcepub fn add_finish_points(&mut self, nodes: Vec<impl Into<String>>) -> &mut Self
pub fn add_finish_points(&mut self, nodes: Vec<impl Into<String>>) -> &mut Self
Add multiple finish points
Sourcepub fn compile(
self,
checkpointer: Option<Arc<dyn BaseCheckpointSaver>>,
) -> Result<CompiledGraph<S>>
pub fn compile( self, checkpointer: Option<Arc<dyn BaseCheckpointSaver>>, ) -> Result<CompiledGraph<S>>
Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for StateGraph<S>
impl<S> !RefUnwindSafe for StateGraph<S>
impl<S> Send for StateGraph<S>
impl<S> Sync for StateGraph<S>
impl<S> Unpin for StateGraph<S>
impl<S> UnsafeUnpin for StateGraph<S>
impl<S> !UnwindSafe for StateGraph<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more