pub trait StateGraph: Send + Sync {
type State: GraphState;
type Compiled: CompiledGraph<Self::State>;
// Required methods
fn new(id: impl Into<String>) -> Self;
fn add_node(
&mut self,
id: impl Into<String>,
node: Box<dyn NodeFunc<Self::State>>,
) -> &mut Self;
fn add_edge(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
) -> &mut Self;
fn add_conditional_edges(
&mut self,
from: impl Into<String>,
conditions: HashMap<String, String>,
) -> &mut Self;
fn add_parallel_edges(
&mut self,
from: impl Into<String>,
targets: Vec<String>,
) -> &mut Self;
fn set_entry_point(&mut self, node: impl Into<String>) -> &mut Self;
fn set_finish_point(&mut self, node: impl Into<String>) -> &mut Self;
fn add_reducer(
&mut self,
key: impl Into<String>,
reducer: Box<dyn Reducer>,
) -> &mut Self;
fn with_config(&mut self, config: GraphConfig) -> &mut Self;
fn id(&self) -> &str;
fn compile(self) -> AgentResult<Self::Compiled>;
}Expand description
State graph builder trait
Defines the interface for building stateful workflow graphs. Implementations should provide a fluent API for constructing graphs.
§Example
ⓘ
use mofa_kernel::workflow::{StateGraph, START, END};
let graph = StateGraphImpl::<MyState>::new("my_workflow")
// Add reducers for state keys
.add_reducer("messages", Box::new(AppendReducer))
.add_reducer("result", Box::new(OverwriteReducer))
// Add nodes
.add_node("process", Box::new(ProcessNode))
.add_node("validate", Box::new(ValidateNode))
// Add edges
.add_edge(START, "process")
.add_edge("process", "validate")
.add_edge("validate", END)
// Compile
.compile()?;Required Associated Types§
Sourcetype State: GraphState
type State: GraphState
The state type for this graph
Sourcetype Compiled: CompiledGraph<Self::State>
type Compiled: CompiledGraph<Self::State>
The compiled graph type produced by this builder
Required Methods§
Sourcefn add_node(
&mut self,
id: impl Into<String>,
node: Box<dyn NodeFunc<Self::State>>,
) -> &mut Self
fn add_node( &mut self, id: impl Into<String>, node: Box<dyn NodeFunc<Self::State>>, ) -> &mut Self
Sourcefn add_edge(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
) -> &mut Self
fn add_edge( &mut self, from: impl Into<String>, to: impl Into<String>, ) -> &mut Self
Add an edge between two nodes
§Arguments
from- Source node ID (use START for entry edge)to- Target node ID (use END for exit edge)
Sourcefn add_conditional_edges(
&mut self,
from: impl Into<String>,
conditions: HashMap<String, String>,
) -> &mut Self
fn add_conditional_edges( &mut self, from: impl Into<String>, conditions: HashMap<String, String>, ) -> &mut Self
Sourcefn add_parallel_edges(
&mut self,
from: impl Into<String>,
targets: Vec<String>,
) -> &mut Self
fn add_parallel_edges( &mut self, from: impl Into<String>, targets: Vec<String>, ) -> &mut Self
Add parallel edges from a node
§Arguments
from- Source node IDtargets- List of target node IDs to execute in parallel
Sourcefn set_entry_point(&mut self, node: impl Into<String>) -> &mut Self
fn set_entry_point(&mut self, node: impl Into<String>) -> &mut Self
Set the entry point (equivalent to add_edge(START, node))
Sourcefn set_finish_point(&mut self, node: impl Into<String>) -> &mut Self
fn set_finish_point(&mut self, node: impl Into<String>) -> &mut Self
Set a finish point (equivalent to add_edge(node, END))
Sourcefn with_config(&mut self, config: GraphConfig) -> &mut Self
fn with_config(&mut self, config: GraphConfig) -> &mut Self
Set the graph configuration
Sourcefn compile(self) -> AgentResult<Self::Compiled>
fn compile(self) -> AgentResult<Self::Compiled>
Compile the graph into an executable form
This validates the graph structure and prepares it for execution.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.