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 set_initial_messages(&mut self, messages: Vec<AgentMessage>)
pub fn set_initial_messages(&mut self, messages: Vec<AgentMessage>)
Set the initial conversation messages (for session resume).
Must be called before prompt or continue_. Replaces any
existing messages in the agent’s internal buffer.
Sourcepub fn inject_message(&mut self, message: AgentMessage)
pub fn inject_message(&mut self, message: AgentMessage)
Inject a single message into the conversation buffer.
Used after compaction to insert a CompactionSummary so subsequent
provider calls include the summary in their context window.
Sourcepub fn replace_messages(&mut self, messages: Vec<AgentMessage>)
pub fn replace_messages(&mut self, messages: Vec<AgentMessage>)
Replace the entire conversation buffer.
Used after compaction to install [summary, ...kept] so subsequent
provider requests no longer carry the compacted messages.
Sourcepub fn emit_event(&self, event: AgentEvent)
pub fn emit_event(&self, event: AgentEvent)
Emit an AgentEvent to all subscribers outside of the agent loop.
Used by callers (e.g. harness) to surface lifecycle events that occur between loop invocations, such as compaction start/end.
Sourcepub fn messages_snapshot(&self) -> Vec<AgentMessage>
pub fn messages_snapshot(&self) -> Vec<AgentMessage>
Snapshot the current conversation buffer.
The harness uses this after a turn (and any subsequent compaction) to
compute the next turn_offset and return the post-compaction message
list to callers.
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.