Skip to main content

StateGraph

Trait StateGraph 

Source
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§

Source

type State: GraphState

The state type for this graph

Source

type Compiled: CompiledGraph<Self::State>

The compiled graph type produced by this builder

Required Methods§

Source

fn new(id: impl Into<String>) -> Self

Create a new graph with the given ID

Source

fn add_node( &mut self, id: impl Into<String>, node: Box<dyn NodeFunc<Self::State>>, ) -> &mut Self

Add a node to the graph

§Arguments
  • id - Unique node identifier
  • node - Node function implementation
Source

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)
Source

fn add_conditional_edges( &mut self, from: impl Into<String>, conditions: HashMap<String, String>, ) -> &mut Self

Add conditional edges from a node

§Arguments
  • from - Source node ID
  • conditions - Map of condition names to target node IDs
§Example
graph.add_conditional_edges("classify", HashMap::from([
    ("type_a".to_string(), "handle_a".to_string()),
    ("type_b".to_string(), "handle_b".to_string()),
]));
Source

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 ID
  • targets - List of target node IDs to execute in parallel
Source

fn set_entry_point(&mut self, node: impl Into<String>) -> &mut Self

Set the entry point (equivalent to add_edge(START, node))

Source

fn set_finish_point(&mut self, node: impl Into<String>) -> &mut Self

Set a finish point (equivalent to add_edge(node, END))

Source

fn add_reducer( &mut self, key: impl Into<String>, reducer: Box<dyn Reducer>, ) -> &mut Self

Add a reducer for a state key

§Arguments
  • key - State key name
  • reducer - Reducer implementation
Source

fn with_config(&mut self, config: GraphConfig) -> &mut Self

Set the graph configuration

Source

fn id(&self) -> &str

Get the graph ID

Source

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.

Implementors§