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 prompt_with_content(
&mut self,
content: Vec<InputContent>,
) -> Result<Vec<AgentMessage>, AgentError>
pub async fn prompt_with_content( &mut self, content: Vec<InputContent>, ) -> Result<Vec<AgentMessage>, AgentError>
Send a user message with arbitrary content (text + images) and run the agent loop.
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_model(&mut self, model: String)
pub fn set_model(&mut self, model: String)
Change the model used by subsequent provider requests.
Sourcepub fn thinking_config(&self) -> ThinkingConfig
pub fn thinking_config(&self) -> ThinkingConfig
Return the thinking configuration used by subsequent provider requests.
Sourcepub fn set_thinking_config(&mut self, thinking: Option<ThinkingConfig>)
pub fn set_thinking_config(&mut self, thinking: Option<ThinkingConfig>)
Change the thinking configuration used by subsequent provider requests.
Sourcepub fn set_max_tokens(&mut self, max_tokens: Option<u64>)
pub fn set_max_tokens(&mut self, max_tokens: Option<u64>)
Change the maximum output tokens used by subsequent provider requests.
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.
Sourcepub fn control_handle(&self) -> AgentControl
pub fn control_handle(&self) -> AgentControl
Return a clonable handle for cancellation and message queues.
Sourcepub fn steer(&self, message: String)
pub fn steer(&self, message: String)
Add a steering message to be delivered before the next provider request.
Steering messages are high-priority and delivered after the current turn’s tool calls complete but before the next provider request.
Sourcepub fn follow_up(&self, message: String)
pub fn follow_up(&self, message: String)
Add a follow-up message to be delivered when the agent would otherwise stop.
Follow-up messages are only delivered when the agent has no tool calls pending and no steering messages queued.
Sourcepub fn reset_cancel_if_cancelled(&mut self)
pub fn reset_cancel_if_cancelled(&mut self)
Reset cancellation state before handing out a control handle for a new turn.