Skip to main content

Agent

Struct Agent 

Source
pub struct Agent { /* private fields */ }
Expand description

Stateful wrapper around the agent loop.

Implementations§

Source§

impl Agent

Source

pub fn new(options: AgentOptions) -> Self

Creates an agent; a leading system message is added for the prompt and tools unless the transcript already starts with one.

Source

pub fn subscribe(&self, listener: Arc<dyn AgentListener>) -> Subscription

Registers a listener; dropping the returned Subscription removes it.

Source

pub fn state(&self) -> AgentState

Snapshot of the state.

Source

pub fn with_state<R>(&self, f: impl FnOnce(&AgentState) -> R) -> R

Runs f on the state under the lock (avoids cloning the transcript).

Source

pub fn messages(&self) -> Vec<AgentMessage>

Copy of the transcript.

Source

pub fn set_messages(&self, messages: Vec<AgentMessage>)

Replaces the transcript (copy-on-set).

Source

pub fn append_message(&self, message: AgentMessage)

Appends one message to the transcript.

Source

pub fn replace_last_message(&self, message: AgentMessage)

Replaces the last transcript message (in-place message_end replacement).

Source

pub fn set_tools(&self, tools: Vec<Arc<dyn AgentTool>>)

Replaces the executable tools.

Source

pub fn model(&self) -> Model

Current model.

Source

pub fn set_model(&self, model: Model)

Sets the model of the next request.

Source

pub fn thinking_level(&self) -> ThinkingLevel

Current thinking level.

Source

pub fn set_thinking_level(&self, level: ThinkingLevel)

Sets the thinking level of the next request.

Source

pub fn system_prompt(&self) -> String

S t a t e . s y s t e m P r o m p t.

Source

pub fn is_streaming(&self) -> bool

Whether a run is active.

Source

pub fn set_hooks(&self, hooks: AgentHooks)

Replaces the hooks.

Source

pub fn update_stream_options(&self, f: impl FnOnce(&mut StreamOptions))

Updates the base stream options (sessionId, transport, budgets, …).

Source

pub fn set_tool_execution(&self, mode: ToolExecution)

Sets the batch scheduling.

Source

pub fn steering_mode(&self) -> QueueMode

Steering drain mode.

Source

pub fn set_steering_mode(&self, mode: QueueMode)

Sets the steering drain mode.

Source

pub fn follow_up_mode(&self) -> QueueMode

Follow-up drain mode.

Source

pub fn set_follow_up_mode(&self, mode: QueueMode)

Sets the follow-up drain mode.

Source

pub fn steer(&self, message: AgentMessage)

Queues a message injected after the current turn.

Source

pub fn follow_up(&self, message: AgentMessage)

Queues a message delivered when the agent would otherwise stop.

Source

pub fn update_queue<R>( &self, steering: bool, edit: impl FnOnce(&mut Vec<AgentMessage>) -> R, ) -> R

Edits the steering (steering = true) or follow-up queue in place, atomically with respect to draining (single-item queue edits).

Source

pub fn clear_steering_queue(&self)

Clears the steering queue.

Source

pub fn clear_follow_up_queue(&self)

Clears the follow-up queue.

Source

pub fn clear_all_queues(&self)

Clears both queues.

Source

pub fn has_queued_messages(&self) -> bool

Whether either queue has messages.

Source

pub fn peek_queued_messages(&self) -> Vec<AgentMessage>

Messages the next turn would take, without consuming them.

Source

pub fn steering_queue(&self) -> Vec<AgentMessage>

Every queued steering message (in order).

Source

pub fn follow_up_queue(&self) -> Vec<AgentMessage>

Every queued follow-up message (in order).

Source

pub fn signal(&self) -> Option<CancellationToken>

Abort signal of the active run.

Source

pub fn abort(&self)

Aborts the active run, if any.

Source

pub fn recover_orphaned_run(&self)

Clears a run orphaned by a crashed driver task (X-PANIC): a panic’s unwind dropped the run future before finish_run ran, leaving the run “active” and the session forever busy. Discards streaming state and pending tool calls; the transcript is kept — callers rebuild from the session log. No-op when no run is active.

Source

pub async fn wait_for_idle(&self)

Resolves once no run is active and every agent_end listener has returned.

Source

pub fn reset(&self) -> Result<(), AgentError>

Clears the transcript (keeping the replayed system baseline) and both queues.

§Errors

AgentError::ResetWhileBusy while a run is active.

Source

pub async fn prompt_text( &self, text: impl Into<String>, images: Vec<ImageContent>, ) -> Result<(), AgentError>

A user message with text and images.

§Errors

AgentError::PromptWhileBusy while a run is active.

Source

pub async fn prompt( &self, messages: Vec<AgentMessage>, ) -> Result<(), AgentError>

Starts a run with messages; resolves when the run is idle.

§Errors

AgentError::PromptWhileBusy while a run is active.

Source

pub async fn continue_run(&self) -> Result<(), AgentError>

Continues from the transcript; after an assistant message the queues are drained instead.

§Errors

AgentError::ContinueWhileBusy, AgentError::NoMessages, AgentError::CannotContinue.

Trait Implementations§

Source§

impl Clone for Agent

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Agent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.