pub struct Agent { /* private fields */ }Expand description
Stateful wrapper around the agent loop.
Implementations§
Source§impl Agent
impl Agent
Sourcepub fn new(options: AgentOptions) -> Self
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.
Sourcepub fn subscribe(&self, listener: Arc<dyn AgentListener>) -> Subscription
pub fn subscribe(&self, listener: Arc<dyn AgentListener>) -> Subscription
Registers a listener; dropping the returned Subscription removes it.
Sourcepub fn state(&self) -> AgentState
pub fn state(&self) -> AgentState
Snapshot of the state.
Sourcepub fn with_state<R>(&self, f: impl FnOnce(&AgentState) -> R) -> R
pub fn with_state<R>(&self, f: impl FnOnce(&AgentState) -> R) -> R
Runs f on the state under the lock (avoids cloning the transcript).
Sourcepub fn messages(&self) -> Vec<AgentMessage>
pub fn messages(&self) -> Vec<AgentMessage>
Copy of the transcript.
Sourcepub fn set_messages(&self, messages: Vec<AgentMessage>)
pub fn set_messages(&self, messages: Vec<AgentMessage>)
Replaces the transcript (copy-on-set).
Sourcepub fn append_message(&self, message: AgentMessage)
pub fn append_message(&self, message: AgentMessage)
Appends one message to the transcript.
Sourcepub fn replace_last_message(&self, message: AgentMessage)
pub fn replace_last_message(&self, message: AgentMessage)
Replaces the last transcript message (in-place message_end replacement).
Sourcepub fn thinking_level(&self) -> ThinkingLevel
pub fn thinking_level(&self) -> ThinkingLevel
Current thinking level.
Sourcepub fn set_thinking_level(&self, level: ThinkingLevel)
pub fn set_thinking_level(&self, level: ThinkingLevel)
Sets the thinking level of the next request.
Sourcepub fn system_prompt(&self) -> String
pub fn system_prompt(&self) -> String
S t a t e . s y s t e m P r o m p t.
Sourcepub fn is_streaming(&self) -> bool
pub fn is_streaming(&self) -> bool
Whether a run is active.
Sourcepub fn set_hooks(&self, hooks: AgentHooks)
pub fn set_hooks(&self, hooks: AgentHooks)
Replaces the hooks.
Sourcepub fn update_stream_options(&self, f: impl FnOnce(&mut StreamOptions))
pub fn update_stream_options(&self, f: impl FnOnce(&mut StreamOptions))
Updates the base stream options (sessionId, transport, budgets, …).
Sourcepub fn set_tool_execution(&self, mode: ToolExecution)
pub fn set_tool_execution(&self, mode: ToolExecution)
Sets the batch scheduling.
Sourcepub fn steering_mode(&self) -> QueueMode
pub fn steering_mode(&self) -> QueueMode
Steering drain mode.
Sourcepub fn set_steering_mode(&self, mode: QueueMode)
pub fn set_steering_mode(&self, mode: QueueMode)
Sets the steering drain mode.
Sourcepub fn follow_up_mode(&self) -> QueueMode
pub fn follow_up_mode(&self) -> QueueMode
Follow-up drain mode.
Sourcepub fn set_follow_up_mode(&self, mode: QueueMode)
pub fn set_follow_up_mode(&self, mode: QueueMode)
Sets the follow-up drain mode.
Sourcepub fn steer(&self, message: AgentMessage)
pub fn steer(&self, message: AgentMessage)
Queues a message injected after the current turn.
Sourcepub fn follow_up(&self, message: AgentMessage)
pub fn follow_up(&self, message: AgentMessage)
Queues a message delivered when the agent would otherwise stop.
Sourcepub fn update_queue<R>(
&self,
steering: bool,
edit: impl FnOnce(&mut Vec<AgentMessage>) -> R,
) -> R
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).
Sourcepub fn clear_steering_queue(&self)
pub fn clear_steering_queue(&self)
Clears the steering queue.
Sourcepub fn clear_follow_up_queue(&self)
pub fn clear_follow_up_queue(&self)
Clears the follow-up queue.
Sourcepub fn clear_all_queues(&self)
pub fn clear_all_queues(&self)
Clears both queues.
Sourcepub fn has_queued_messages(&self) -> bool
pub fn has_queued_messages(&self) -> bool
Whether either queue has messages.
Sourcepub fn peek_queued_messages(&self) -> Vec<AgentMessage>
pub fn peek_queued_messages(&self) -> Vec<AgentMessage>
Messages the next turn would take, without consuming them.
Sourcepub fn steering_queue(&self) -> Vec<AgentMessage>
pub fn steering_queue(&self) -> Vec<AgentMessage>
Every queued steering message (in order).
Sourcepub fn follow_up_queue(&self) -> Vec<AgentMessage>
pub fn follow_up_queue(&self) -> Vec<AgentMessage>
Every queued follow-up message (in order).
Sourcepub fn signal(&self) -> Option<CancellationToken>
pub fn signal(&self) -> Option<CancellationToken>
Abort signal of the active run.
Sourcepub fn recover_orphaned_run(&self)
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.
Sourcepub async fn wait_for_idle(&self)
pub async fn wait_for_idle(&self)
Resolves once no run is active and every agent_end listener has returned.
Sourcepub fn reset(&self) -> Result<(), AgentError>
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.
Sourcepub async fn prompt_text(
&self,
text: impl Into<String>,
images: Vec<ImageContent>,
) -> Result<(), AgentError>
pub async fn prompt_text( &self, text: impl Into<String>, images: Vec<ImageContent>, ) -> Result<(), AgentError>
Sourcepub async fn prompt(
&self,
messages: Vec<AgentMessage>,
) -> Result<(), AgentError>
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.
Sourcepub async fn continue_run(&self) -> Result<(), AgentError>
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.