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 implementationH: The hooks implementation for lifecycle customizationM: The message store implementationS: 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,
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,
Sourcepub fn new(
provider: P,
tools: ToolRegistry<Ctx>,
hooks: H,
message_store: M,
state_store: S,
config: AgentConfig,
) -> Self
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.
Sourcepub fn with_compaction(
provider: P,
tools: ToolRegistry<Ctx>,
hooks: H,
message_store: M,
state_store: S,
config: AgentConfig,
compaction_config: CompactionConfig,
) -> Self
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.
Sourcepub fn run(
&self,
thread_id: ThreadId,
user_message: String,
tool_context: ToolContext<Ctx>,
) -> Receiver<AgentEvent>where
Ctx: Clone,
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> 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
Mutably borrows from an owned value. Read more