Documentation Index
Fetch the complete documentation index at: https://mintlify.com/mofa-org/mofa/llms.txt
Use this file to discover all available pages before exploring further.
StateGraph API
The StateGraph API provides a LangGraph-inspired interface for building stateful workflow graphs in MoFA. It enables you to define complex workflows with nodes, edges, and state management.Overview
StateGraph is the core API for building workflow graphs. It provides:- Declarative graph building with a fluent API
- State management with customizable reducers
- Multiple edge types: single, conditional, and parallel
- Execution modes: invoke (synchronous), stream (event-based), and step (interactive)
- Fault tolerance with retry policies and circuit breakers
Core Concepts
Special Node IDs
Special node ID for the graph entry point (
"__START__")Special node ID for the graph exit point (
"__END__")StateGraph Trait
The main trait for building workflow graphs.Constructor
Create a new StateGraph with the given ID
Node Management
Add a node to the graphParameters:
id: Unique node identifiernode: Node function implementation
&mut Self for method chainingEdge Management
Add a single edge between two nodesParameters:
from: Source node ID (useSTARTfor entry edge)to: Target node ID (useENDfor exit edge)
add_conditional_edges
fn(&mut self, from: impl Into<String>, conditions: HashMap<String, String>) -> &mut Self
Add conditional edges from a node based on routing decisionsParameters:
from: Source node IDconditions: Map of route names to target node IDs
Add parallel edges to execute multiple nodes concurrentlyParameters:
from: Source node IDtargets: List of target node IDs to execute in parallel
Set the graph entry point (equivalent to
add_edge(START, node))Set a finish point (equivalent to
add_edge(node, END))State Reducers
Add a reducer for a state key to control how updates are mergedParameters:
key: State key namereducer: Reducer implementation (see Reducers)
Configuration
Set graph configuration including parallelism limits and recursion depth
Attach fault-tolerance policy to a specific node
Compilation
Compile the graph into an executable formThis validates the graph structure and prepares it for execution.Validation checks:
- Entry point is set
- All nodes are reachable from entry point
- All edge references point to valid nodes
- Fallback nodes in policies exist
CompiledGraph Trait
A compiled graph ready for execution.Execution Methods
Execute the graph synchronously and return the final stateParameters:
input: Initial stateconfig: Optional runtime configuration
Execute the graph with streaming outputReturns a stream of events as each node executes.Stream Events:
NodeStart { node_id, state }: Node startedNodeEnd { node_id, state, command }: Node completedNodeRetry { node_id, attempt, error }: Node retryingNodeFallback { from_node, to_node, reason }: Falling backCircuitOpen { node_id }: Circuit breaker openedEnd { final_state }: Execution completedError { node_id, error }: Error occurred
Execute a single step of the graphUseful for debugging or interactive execution.Returns:
StepResult containing:state: Current state after the stepnode_id: Which node was executedcommand: Command returned by the nodeis_complete: Whether execution is completenext_node: Next node to execute (if any)
Validation
Validate that a state is valid for this graph
Get the graph’s state schema showing reducer types for each key
GraphState Trait
Trait for types that can be used as workflow state.Get a value from the state by key
Apply an update to the state
EdgeTarget Types
Single target node
Conditional edges with route names to node IDs
Multiple parallel target nodes
Complete Example
Conditional Routing Example
Parallel Execution Example
See Also
- Workflow Nodes - Node types and implementations
- Workflow DSL - YAML-based workflow configuration
- Command API - Control flow and state updates