Skip to main content

AgentRuntime

Struct AgentRuntime 

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

Source

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
Source

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.

Source

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.

Source

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.

Source

pub fn with_checkpointer(self, checkpointer: Arc<MemoryCheckpointer>) -> Self

Builder-style: set the checkpointer.

Source

pub fn checkpointer(&self) -> Option<Arc<MemoryCheckpointer>>

Get the checkpointer if set.

Source

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

pub fn middleware_pipeline(&self) -> &MiddlewarePipeline<T>

Get a reference to the middleware pipeline

Source

pub fn middleware_pipeline_mut(&mut self) -> &mut MiddlewarePipeline<T>

Get mutable reference to the middleware pipeline

Source

pub fn with_observability(&mut self) -> &mut Self

Add observability middleware (execution tracing, node timing, token usage, failure snapshots).

Source

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.

Source

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.

Source

pub fn graph(&self) -> &Graph<T>

Get a reference to the graph for inspection

Source

pub fn graph_mut(&mut self) -> &mut Graph<T>

Get mutable access to the graph

Source

pub fn config(&self) -> &AgentConfig

Get the configuration

Source

pub fn llm_client(&self) -> Arc<dyn LLMClient>

Get the LLM client

Source

pub fn mcp_client(&self, name: &str) -> Option<Arc<dyn MCPClient>>

Get an MCP client by name

Source

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")?;

Auto Trait Implementations§

§

impl<T> Freeze for AgentRuntime<T>

§

impl<T> !RefUnwindSafe for AgentRuntime<T>

§

impl<T> Send for AgentRuntime<T>

§

impl<T> Sync for AgentRuntime<T>

§

impl<T> Unpin for AgentRuntime<T>

§

impl<T> UnsafeUnpin for AgentRuntime<T>

§

impl<T> !UnwindSafe for AgentRuntime<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,