Skip to main content

Agent

Struct Agent 

Source
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

Source

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.

Source

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.

Source

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.

Source

pub fn abort(&self)

Cancel the current operation.

Equivalent to the first Ctrl+C. The running prompt or continue_ call will return AgentError::Cancelled.

Source

pub fn add_tool(&mut self, tool: Box<dyn Tool>)

Add an additional tool to the agent’s tool set.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn subscribe(&mut self, callback: Box<dyn Fn(&AgentEvent) + Send + Sync>)

Register an event subscriber that receives all AgentEvents.

Source

pub fn cancel_token(&self) -> CancellationToken

Return a clonable cancellation token for external cancellation.

Cancelling this token cancels the currently running loop operation.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl Freeze for Agent

§

impl !RefUnwindSafe for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin for Agent

§

impl !UnwindSafe for Agent

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more