pub struct Agent { /* private fields */ }Expand description
Stateful wrapper around agent_loop with conversation state, cancellation,
event subscription, and message queue management.
Implementations§
Source§impl Agent
impl Agent
Sourcepub fn new(
provider: Box<dyn Provider>,
tools: Vec<Box<dyn Tool>>,
model: String,
system: Option<String>,
config: AgentLoopConfig,
hooks: Box<dyn AgentHooks>,
) -> Self
pub fn new( provider: Box<dyn Provider>, tools: Vec<Box<dyn Tool>>, model: String, system: Option<String>, config: AgentLoopConfig, hooks: Box<dyn AgentHooks>, ) -> Self
Create a new Agent with the given provider, tools, model, and hooks.
Sourcepub async fn prompt(
&mut self,
text: impl Into<String>,
) -> Result<Vec<AgentMessage>, AgentError>
pub async fn prompt( &mut self, text: impl Into<String>, ) -> Result<Vec<AgentMessage>, AgentError>
Send a user message and run the agent loop.
Resets the cancellation state if the agent was previously aborted, allowing a fresh conversation turn.
Sourcepub async fn continue_(
&mut self,
text: impl Into<String>,
) -> Result<Vec<AgentMessage>, AgentError>
pub async fn continue_( &mut self, text: impl Into<String>, ) -> Result<Vec<AgentMessage>, AgentError>
Continue the conversation with an additional user message.
Requires the last context message to be a user message or tool result.
Sourcepub fn abort(&self)
pub fn abort(&self)
Cancel the current operation.
Equivalent to the first Ctrl+C. The running prompt or continue_
call will return AgentError::Cancelled.
Sourcepub fn add_tool(&mut self, tool: Box<dyn Tool>)
pub fn add_tool(&mut self, tool: Box<dyn Tool>)
Add an additional tool to the agent’s tool set.
Sourcepub fn subscribe(&mut self, callback: Box<dyn Fn(&AgentEvent) + Send + Sync>)
pub fn subscribe(&mut self, callback: Box<dyn Fn(&AgentEvent) + Send + Sync>)
Register an event subscriber that receives all AgentEvents.
Sourcepub fn cancel_token(&self) -> CancellationToken
pub fn cancel_token(&self) -> CancellationToken
Return a clonable cancellation token for external cancellation.
Cancelling this token cancels the currently running loop operation.