pub struct AgentLoop<P: Platform> { /* private fields */ }Expand description
The core agent loop that processes inbound messages.
Consumes messages from the bus, invokes the LLM pipeline, executes tool calls, and dispatches responses. This struct holds all the dependencies needed for the full processing cycle.
§Processing flow
- Consume: Pull the next
InboundMessagefrom theMessageBus. - Session: Look up or create a
Sessionkeyed bychannel:chat_id. - Context: Build the LLM message list via
ContextBuilder::build_messages. - Pipeline: Run the assembled context through the 6-stage pipeline (Classifier -> Router -> Assembler -> Transport -> Scorer -> Learner).
- Tools: If the LLM response contains tool calls, execute them
via the
ToolRegistry, append results, and loop back to step 4 (up tomax_tool_iterations). - Respond: Extract the final text response and dispatch an
OutboundMessageto the bus. - Persist: Save the updated session and append to history.
Implementations§
Source§impl<P: Platform> AgentLoop<P>
impl<P: Platform> AgentLoop<P>
Sourcepub fn new(
config: AgentsConfig,
platform: Arc<P>,
bus: Arc<MessageBus>,
pipeline: PipelineRegistry,
tools: Arc<ToolRegistry>,
context: ContextBuilder<P>,
sessions: Arc<SessionManager<P>>,
permission_resolver: PermissionResolver,
) -> Self
pub fn new( config: AgentsConfig, platform: Arc<P>, bus: Arc<MessageBus>, pipeline: PipelineRegistry, tools: Arc<ToolRegistry>, context: ContextBuilder<P>, sessions: Arc<SessionManager<P>>, permission_resolver: PermissionResolver, ) -> Self
Create a new agent loop with all dependencies wired.
§Arguments
config– Agent configuration (model, max_tokens, etc.)platform– Platform abstraction for filesystem/env/httpbus– Message bus for consuming inbound and dispatching outboundpipeline– Pipeline registry for LLM invocationtools– Tool registry for executing tool callscontext– Context builder for assembling promptssessions– Session manager for conversation persistence
Sourcepub fn with_cancel(self, token: CancellationToken) -> Self
pub fn with_cancel(self, token: CancellationToken) -> Self
Attach a CancellationToken so the agent loop exits promptly on
shutdown instead of waiting for all bus senders to be dropped.
Sourcepub fn with_auto_delegation(self, delegation: Arc<dyn AutoDelegation>) -> Self
pub fn with_auto_delegation(self, delegation: Arc<dyn AutoDelegation>) -> Self
Attach an auto-delegation router for pre-LLM routing.
When set, messages matching delegation rules are routed to the
delegate_task tool before the local LLM processes them.
Sourcepub fn config(&self) -> &AgentsConfig
pub fn config(&self) -> &AgentsConfig
Get a reference to the agent configuration.
Sourcepub fn bus(&self) -> &Arc<MessageBus>
pub fn bus(&self) -> &Arc<MessageBus>
Get a reference to the message bus.
Sourcepub async fn run(&self) -> Result<()>
pub async fn run(&self) -> Result<()>
Run the agent loop, consuming messages until the bus is closed or
the optional CancellationToken is triggered.
This is the main entrypoint. It pulls messages from the inbound channel and processes each one through the full pipeline. Errors on individual messages are logged but do not terminate the loop.
Auto Trait Implementations§
impl<P> Freeze for AgentLoop<P>
impl<P> !RefUnwindSafe for AgentLoop<P>
impl<P> Send for AgentLoop<P>
impl<P> Sync for AgentLoop<P>
impl<P> Unpin for AgentLoop<P>
impl<P> UnsafeUnpin for AgentLoop<P>
impl<P> !UnwindSafe for AgentLoop<P>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more