pub struct Engine { /* private fields */ }Expand description
The core ReAct agent loop.
Drives an LLM through iterative reasoning and tool execution until the model produces a final text answer or the iteration limit is reached.
§Starting a turn
Every agent turn goes through Engine::run, which returns a
RunBuilder. Chain axis setters (.ops, .cancel, .chunked)
in any order, then call one of the three terminators
(.result, .callback, .stream) to execute the turn:
use std::sync::Arc;
use motosan_agent_loop::{Engine, Message};
let agent = Arc::new(Engine::builder().build());
// Simplest: just the final result.
let result = Arc::clone(&agent)
.run(llm.clone(), vec![Message::user("Hi!")])
.result()
.await?;
// Live token-by-token UI with AskUser support:
Arc::clone(&agent)
.run(llm, vec![Message::user("Ask me a question")])
.chunked()
.ops(ops_rx)
.callback(|event| println!("{event:?}"))
.await?;See RunBuilder for the full axis/terminator matrix and
Engine::run for the entry point.
The loop does not own the LLM client; instead, RunBuilder
takes Arc<dyn LlmClient> so the same loop can be reused with
different backends.
Implementations§
Source§impl Engine
impl Engine
Sourcepub fn builder() -> EngineBuilder
pub fn builder() -> EngineBuilder
Create a builder with default settings.
Sourcepub fn channel_config(&self) -> &ChannelConfig
pub fn channel_config(&self) -> &ChannelConfig
Returns the channel configuration for this agent loop.
Used by AgentSession to create bounded
channels with the configured capacities and policies.
Source§impl Engine
impl Engine
Sourcepub fn run(
self: Arc<Self>,
llm: Arc<dyn LlmClient>,
messages: Vec<Message>,
) -> RunBuilder
pub fn run( self: Arc<Self>, llm: Arc<dyn LlmClient>, messages: Vec<Message>, ) -> RunBuilder
Build a run configuration for this agent.
Chain axis setters (.ops(rx), .cancel(token), .chunked())
in any order, then call one of the terminators (.result(),
.callback(cb), .stream()) to execute the turn.
§Example
use std::sync::Arc;
use motosan_agent_loop::{Engine, Message};
let agent = Arc::new(Engine::builder().build());
// Simplest: just the final result.
let result = Arc::clone(&agent)
.run(llm.clone(), vec![Message::user("Hi!")])
.result()
.await?;See the RunBuilder struct documentation for the full set
of axis setters and terminators.