pub struct GraphBuilder { /* private fields */ }Expand description
Builder for a Graph. Register nodes and edges, set the entry point,
then GraphBuilder::compile to validate and freeze.
Implementations§
Source§impl GraphBuilder
impl GraphBuilder
Sourcepub fn add_node<N>(&mut self, name: impl Into<String>, node: N) -> &mut Selfwhere
N: Node + 'static,
pub fn add_node<N>(&mut self, name: impl Into<String>, node: N) -> &mut Selfwhere
N: Node + 'static,
Register a node under name.
Accepts any Node impl — including async closures
(Fn(NodeContext) -> impl Future<Output = Result<NodeOutput>>) via
the blanket impl. Re-registering the same name replaces the node.
Sourcepub fn add_edge(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
) -> &mut Self
pub fn add_edge( &mut self, from: impl Into<String>, to: impl Into<String>, ) -> &mut Self
Add a static edge from → to. Multiple outgoing static edges from
the same node activate all destinations in parallel in the next
super-step.
GraphBuilder::compile rejects a node that has both static and
conditional edges — ambiguous routing fails when you call compile(),
not as a runtime surprise. Dynamic routing via Command::goto from a node
that also has static edges remains a runtime rule: both paths
execute.
Sourcepub fn add_conditional_edges<F, Fut>(
&mut self,
from: impl Into<String>,
router: F,
) -> &mut Self
pub fn add_conditional_edges<F, Fut>( &mut self, from: impl Into<String>, router: F, ) -> &mut Self
Add a conditional edge: after from completes, run router against
the post-barrier state and follow the returned Route.
use rusty_agent_runtime::graph::{GraphBuilder, Route};
let mut builder = GraphBuilder::new();
builder.add_conditional_edges("agent", |state| async move {
let needs_tools = state.get("messages")
.and_then(|m| m.as_array())
.and_then(|a| a.last())
.map(|m| m.get("tool_calls").is_some())
.unwrap_or(false);
Ok(if needs_tools { Route::Node("tools".into()) } else { Route::End })
});Sourcepub fn set_entry_point(&mut self, name: impl Into<String>) -> &mut Self
pub fn set_entry_point(&mut self, name: impl Into<String>) -> &mut Self
Set the entry-point node (the LangGraph START edge).
Sourcepub fn compile(self) -> Result<Graph>
pub fn compile(self) -> Result<Graph>
Validate and freeze the graph.
Validation (all failures are RustyError::Graph):
- at least one node is registered;
- the entry point is set and references a known node;
- every edge endpoint (
from, andtofor static edges) references a known node; - no node is registered under a reserved name (
ENDor any__-prefixed name); - no duplicate static
from → toedge (a duplicate would activate the target twice in one super-step, which surfaces as a spurious double-write failure on single-write channels); - at most one conditional edge per source node;
- no source node mixes static and conditional edges.
Conditional router targets and Send node names are validated at
execution time (they are data-dependent by design).