pub struct AgentRuntime<T: State> { /* private fields */ }Expand description
The core execution engine for agent graphs
Responsible for:
- Managing the graph structure
- Executing nodes in order
- Applying edge conditions
- Maintaining service clients
- Executing middleware hooks
Implementations§
Source§impl<T: State> AgentRuntime<T>
impl<T: State> AgentRuntime<T>
Sourcepub fn from_config(config: AgentConfig) -> Result<Self>
pub fn from_config(config: AgentConfig) -> Result<Self>
Create a new runtime from a configuration
This performs:
- Configuration validation
- LLM client initialization
- MCP client setup
- Graph construction
Sourcepub fn register_node(
&mut self,
name: &str,
function: NodeFunction<T>,
) -> Result<()>
pub fn register_node( &mut self, name: &str, function: NodeFunction<T>, ) -> Result<()>
Register a handler function for a node
Replaces the placeholder function with the actual handler. Called during agent initialization.
Sourcepub fn register_edge_condition(
&mut self,
from: &str,
condition_name: &str,
condition_fn: EdgeCondition<T>,
) -> Result<()>
pub fn register_edge_condition( &mut self, from: &str, condition_name: &str, condition_fn: EdgeCondition<T>, ) -> Result<()>
Register a condition function for an edge
Associates a condition function with conditional edges.
Sourcepub fn set_checkpointer(
&mut self,
checkpointer: Arc<MemoryCheckpointer>,
) -> &mut Self
pub fn set_checkpointer( &mut self, checkpointer: Arc<MemoryCheckpointer>, ) -> &mut Self
Set the checkpointer for thread-scoped state persistence.
When set, use execute_with_thread(thread_id, state) to load/save state per thread.
Sourcepub fn with_checkpointer(self, checkpointer: Arc<MemoryCheckpointer>) -> Self
pub fn with_checkpointer(self, checkpointer: Arc<MemoryCheckpointer>) -> Self
Builder-style: set the checkpointer.
Sourcepub fn checkpointer(&self) -> Option<Arc<MemoryCheckpointer>>
pub fn checkpointer(&self) -> Option<Arc<MemoryCheckpointer>>
Get the checkpointer if set.
Sourcepub fn add_middleware(
&mut self,
middleware: Arc<dyn Middleware<T>>,
) -> &mut Self
pub fn add_middleware( &mut self, middleware: Arc<dyn Middleware<T>>, ) -> &mut Self
Add middleware to the middleware pipeline
Middleware will be executed in the order they are added during node execution. Each middleware receives an ExecutionContext and can:
- Log/monitor execution
- Validate state
- Modify state before/after execution
- Skip nodes or abort execution
§Arguments
middleware- The middleware to add (must implement Middleware trait)
§Example
runtime.add_middleware(Arc::new(LoggingMiddleware::new()));Sourcepub fn middleware_pipeline(&self) -> &MiddlewarePipeline<T>
pub fn middleware_pipeline(&self) -> &MiddlewarePipeline<T>
Get a reference to the middleware pipeline
Sourcepub fn middleware_pipeline_mut(&mut self) -> &mut MiddlewarePipeline<T>
pub fn middleware_pipeline_mut(&mut self) -> &mut MiddlewarePipeline<T>
Get mutable reference to the middleware pipeline
Sourcepub fn with_observability(&mut self) -> &mut Self
pub fn with_observability(&mut self) -> &mut Self
Add observability middleware (execution tracing, node timing, token usage, failure snapshots).
Sourcepub async fn execute(&self, initial_state: T) -> Result<T>
pub async fn execute(&self, initial_state: T) -> Result<T>
Execute the agent graph from start to end.
For thread-scoped persistence (resume, multi-turn), use execute_with_thread and
set a checkpointer via set_checkpointer or config.
Sourcepub async fn execute_with_thread(
&self,
thread_id: &str,
initial_state: T,
) -> Result<T>
pub async fn execute_with_thread( &self, thread_id: &str, initial_state: T, ) -> Result<T>
Execute with a thread id for checkpointing. When a checkpointer is set, state is loaded from the last checkpoint for this thread (if any) and saved after each node.
Sourcepub fn config(&self) -> &AgentConfig
pub fn config(&self) -> &AgentConfig
Get the configuration
Sourcepub fn llm_client(&self) -> Arc<dyn LLMClient>
pub fn llm_client(&self) -> Arc<dyn LLMClient>
Get the LLM client
Sourcepub fn visualize_graph(&self, output_path: &str) -> Result<()>
pub fn visualize_graph(&self, output_path: &str) -> Result<()>
Visualize the agent graph as a text file
Generates a text-based representation of the graph structure. This is useful for debugging and documentation.
§Arguments
output_path: Path where to save the visualization
§Example
#[cfg(feature = "visualization")]
runtime.visualize_graph("agent_graph.txt")?;