AgentLoop

Struct AgentLoop 

Source
pub struct AgentLoop<Ctx, P, H, M, S>{ /* private fields */ }
Expand description

The main agent loop that orchestrates LLM calls and tool execution.

AgentLoop is the core component that:

  • Manages conversation state via message and state stores
  • Calls the LLM provider and processes responses
  • Executes tools through the tool registry
  • Emits events for real-time updates via an async channel
  • Enforces hooks for tool permissions and lifecycle events

§Type Parameters

  • Ctx: Application-specific context passed to tools (e.g., user ID, database)
  • P: The LLM provider implementation
  • H: The hooks implementation for lifecycle customization
  • M: The message store implementation
  • S: The state store implementation

§Running the Agent

let mut events = agent.run(thread_id, "Hello!".to_string(), tool_ctx);
while let Some(event) = events.recv().await {
    match event {
        AgentEvent::Text { text } => println!("{}", text),
        AgentEvent::Done { .. } => break,
        _ => {}
    }
}

Implementations§

Source§

impl<Ctx, P, H, M, S> AgentLoop<Ctx, P, H, M, S>
where Ctx: Send + Sync + 'static, P: LlmProvider + 'static, H: AgentHooks + 'static, M: MessageStore + 'static, S: StateStore + 'static,

Source

pub fn new( provider: P, tools: ToolRegistry<Ctx>, hooks: H, message_store: M, state_store: S, config: AgentConfig, ) -> Self

Create a new agent loop with all components specified directly.

Source

pub fn with_compaction( provider: P, tools: ToolRegistry<Ctx>, hooks: H, message_store: M, state_store: S, config: AgentConfig, compaction_config: CompactionConfig, ) -> Self

Create a new agent loop with compaction enabled.

Source

pub fn run( &self, thread_id: ThreadId, user_message: String, tool_context: ToolContext<Ctx>, ) -> Receiver<AgentEvent>
where Ctx: Clone,

Run the agent loop for a single user message. Returns a channel receiver that yields AgentEvents.

Auto Trait Implementations§

§

impl<Ctx, P, H, M, S> Freeze for AgentLoop<Ctx, P, H, M, S>

§

impl<Ctx, P, H, M, S> !RefUnwindSafe for AgentLoop<Ctx, P, H, M, S>

§

impl<Ctx, P, H, M, S> Send for AgentLoop<Ctx, P, H, M, S>

§

impl<Ctx, P, H, M, S> Sync for AgentLoop<Ctx, P, H, M, S>

§

impl<Ctx, P, H, M, S> Unpin for AgentLoop<Ctx, P, H, M, S>

§

impl<Ctx, P, H, M, S> !UnwindSafe for AgentLoop<Ctx, P, H, M, S>

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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