pub struct StateGraph<S: GraphState> { /* private fields */ }Expand description
Builder for state graphs.
Use the fluent API to add nodes, edges, and compile the graph.
§Examples
use regula_core::{StateGraph, start, end, node_fn, router_fn, RouteOutput};
let graph = StateGraph::<MyState>::new()
.add_node("agent", agent_node)
.add_node("tools", tools_node)
.add_edge(start(), "agent")
.add_conditional_edges("agent", router_fn(|state| {
if state.done {
RouteOutput::end()
} else {
RouteOutput::one("tools")
}
}))
.add_edge("tools", "agent")
.compile(Default::default())?;Implementations§
Source§impl<S: GraphState> StateGraph<S>
impl<S: GraphState> StateGraph<S>
Sourcepub fn try_add_node<N: Node<S> + 'static>(
self,
name: impl Into<NodeId>,
node: N,
) -> Result<Self>
pub fn try_add_node<N: Node<S> + 'static>( self, name: impl Into<NodeId>, node: N, ) -> Result<Self>
Try to add a node to the graph.
Returns an error if a node with this name already exists.
Sourcepub fn add_edge(self, from: impl Into<NodeId>, to: impl Into<NodeId>) -> Self
pub fn add_edge(self, from: impl Into<NodeId>, to: impl Into<NodeId>) -> Self
Add a normal (fixed) edge between two nodes.
§Arguments
from: The source node (orstart()for entry point).to: The target node (orend()for termination).
Sourcepub fn add_conditional_edges<R: EdgeRouter<S> + 'static>(
self,
from: impl Into<NodeId>,
router: R,
) -> Self
pub fn add_conditional_edges<R: EdgeRouter<S> + 'static>( self, from: impl Into<NodeId>, router: R, ) -> Self
Add conditional edges with a router function.
The router determines the next node(s) based on the current state.
§Arguments
from: The source node.router: The routing logic.
Sourcepub fn add_conditional_edges_with_map<R: EdgeRouter<S> + 'static>(
self,
from: impl Into<NodeId>,
router: R,
path_map: HashMap<String, NodeId>,
) -> Self
pub fn add_conditional_edges_with_map<R: EdgeRouter<S> + 'static>( self, from: impl Into<NodeId>, router: R, path_map: HashMap<String, NodeId>, ) -> Self
Add conditional edges with a path map.
The path map translates router string outputs to node IDs.
Sourcepub fn set_entry_point(self, node: impl Into<NodeId>) -> Self
pub fn set_entry_point(self, node: impl Into<NodeId>) -> Self
Set the entry point explicitly.
This is equivalent to add_edge(start(), node).
Sourcepub fn set_finish_point(self, node: impl Into<NodeId>) -> Self
pub fn set_finish_point(self, node: impl Into<NodeId>) -> Self
Set a finish point explicitly.
This is equivalent to add_edge(node, end()).
Sourcepub fn interrupt_before(self, node: impl Into<NodeId>) -> Self
pub fn interrupt_before(self, node: impl Into<NodeId>) -> Self
Add a node to interrupt before execution.
When the graph reaches this node, it will pause before executing and return to the caller.
Sourcepub fn interrupt_after(self, node: impl Into<NodeId>) -> Self
pub fn interrupt_after(self, node: impl Into<NodeId>) -> Self
Add a node to interrupt after execution.
When this node completes, the graph will pause and return to the caller.
Sourcepub fn node_names(&self) -> impl Iterator<Item = &NodeId>
pub fn node_names(&self) -> impl Iterator<Item = &NodeId>
Get the list of node names.
Sourcepub fn compile(self, config: CompileConfig) -> Result<CompiledStateGraph<S>>
pub fn compile(self, config: CompileConfig) -> Result<CompiledStateGraph<S>>
Compile the graph into an executable form.
This validates the graph structure and produces a CompiledStateGraph
ready for execution.