pub struct StateGraph {
pub schema: StateSchema,
pub nodes: HashMap<String, Arc<dyn Node>>,
pub edges: Vec<Edge>,
pub deferred_configs: HashMap<String, DeferredNodeConfig>,
}Expand description
Builder for constructing graphs
Fields§
§schema: StateSchemaState schema
nodes: HashMap<String, Arc<dyn Node>>Registered nodes
edges: Vec<Edge>Registered edges
deferred_configs: HashMap<String, DeferredNodeConfig>Fan-in (deferred) node configurations, keyed by node name.
Implementations§
Source§impl StateGraph
impl StateGraph
Sourcepub fn new(schema: StateSchema) -> Self
pub fn new(schema: StateSchema) -> Self
Create a new graph with the given state schema
Sourcepub fn with_channels(channels: &[&str]) -> Self
pub fn with_channels(channels: &[&str]) -> Self
Create with a simple schema (just channel names, all overwrite)
Sourcepub fn add_node_fn<F, Fut>(self, name: &str, func: F) -> Selfwhere
F: Fn(NodeContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<NodeOutput>> + Send + 'static,
pub fn add_node_fn<F, Fut>(self, name: &str, func: F) -> Selfwhere
F: Fn(NodeContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<NodeOutput>> + Send + 'static,
Add a function as a node
Sourcepub fn add_deferred_node_fn<F, Fut>(
self,
name: &str,
func: F,
config: DeferredNodeConfig,
) -> Selfwhere
F: Fn(NodeContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<NodeOutput>> + Send + 'static,
pub fn add_deferred_node_fn<F, Fut>(
self,
name: &str,
func: F,
config: DeferredNodeConfig,
) -> Selfwhere
F: Fn(NodeContext) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<NodeOutput>> + Send + 'static,
Add a fan-in (deferred) function node.
Unlike add_node_fn, a deferred node does not run as
soon as one upstream edge completes — the scheduler holds it until all
upstream paths that can reach it have finished (or, with a configured
fan_in_timeout, until that deadline). This is what makes a fan-out /
fan-in pattern correct: several branches run in parallel and a single
aggregator node runs once, after they all complete.
The DeferredNodeConfig selects how the upstream outputs are exposed
(e.g. MergeStrategy::Collect)
and an optional fan-in timeout.
§Example
use adk_graph::{StateGraph, DeferredNodeConfig, MergeStrategy};
let graph = StateGraph::with_channels(&["x"])
.add_node_fn("a", |_| async { Ok(Default::default()) })
.add_node_fn("b", |_| async { Ok(Default::default()) })
.add_deferred_node_fn("join", |_| async { Ok(Default::default()) },
DeferredNodeConfig { merge_strategy: MergeStrategy::Collect, ..Default::default() })
.add_edge("a", "join")
.add_edge("b", "join");Sourcepub fn mark_deferred(self, name: &str, config: DeferredNodeConfig) -> Self
pub fn mark_deferred(self, name: &str, config: DeferredNodeConfig) -> Self
Sourcepub fn add_edge(self, source: &str, target: &str) -> Self
pub fn add_edge(self, source: &str, target: &str) -> Self
Add a direct edge from source to target
Sourcepub fn add_conditional_edges<F, I>(
self,
source: &str,
router: F,
targets: I,
) -> Self
pub fn add_conditional_edges<F, I>( self, source: &str, router: F, targets: I, ) -> Self
Add a conditional edge with a router function
Sourcepub fn add_conditional_edges_arc<I>(
self,
source: &str,
router: RouterFn,
targets: I,
) -> Self
pub fn add_conditional_edges_arc<I>( self, source: &str, router: RouterFn, targets: I, ) -> Self
Add a conditional edge with an Arc router (for pre-built routers)
Sourcepub fn compile(self) -> Result<CompiledGraph>
pub fn compile(self) -> Result<CompiledGraph>
Compile the graph for execution