Skip to main content

Agent

Struct Agent 

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

The primary concrete agent: pairs a chat client with instructions, default options, tools, context providers, and middleware.

Cheaply cloneable (the client, context providers, and middleware are shared via Arc), which is what makes Agent::as_tool possible.

Implementations§

Source§

impl Agent

Source

pub fn builder(client: impl ChatClient + 'static) -> AgentBuilder

Start building an agent from a chat client. The client is automatically wrapped with FunctionInvokingChatClient so local tools are executed.

Source

pub fn instructions(&self) -> Option<&str>

The agent’s default instructions.

Source

pub async fn run_stream( &self, messages: impl IntoMessages, session: Option<AgentSession>, options: Option<AgentRunOptions>, ) -> Result<AgentRunStream>

Run and stream incremental updates — an ergonomic wrapper over the object-safe SupportsAgentRun::run_stream trait method (the real streaming implementation), accepting impl IntoMessages.

The session’s context providers (including any history provider) are driven when the stream completes; because provider storage is shared via Arc, updates are visible on the original session once the returned stream is fully consumed. Pass per-run AgentRunOptions to override the agent’s defaults for this call only.

Source

pub async fn run_stream_once( &self, messages: impl IntoMessages, ) -> Result<AgentRunStream>

Ergonomic streaming run with a fresh session and no per-run options (mirrors Agent::run_once).

Source

pub async fn run_once( &self, messages: impl IntoMessages, ) -> Result<AgentResponse>

Ergonomic run without an explicit session.

Source§

impl Agent

Source

pub fn description(&self) -> Option<&str>

The agent description, if any.

Source

pub fn create_session_with_service_id( &self, service_session_id: impl Into<String>, ) -> AgentSession

Create a new service-managed session bound to service_session_id, mirroring Python’s get_new_thread(service_thread_id=…) (_agents.py:1078-1082).

The agent’s own context providers are NOT copied onto the returned session; see the note on Agent::create_session.

Source

pub fn session_from_dict(&self, state: &Value) -> Result<AgentSession>

Reconstruct a session from state (as produced by AgentSession::to_dict), mirroring Python’s BaseAgent.deserialize_thread (_agents.py:378-392).

Conversation history is not part of this state (see AgentSession::to_dict); reattach a crate::history::HistoryProvider (e.g. via crate::history::InMemoryHistoryProvider::from_dict) to context_providers separately when restoring a conversation. The agent’s own context providers are NOT copied onto the returned session; see the note on Agent::create_session.

Source

pub fn as_tool(&self, options: AsToolOptions) -> ToolDefinition

Wrap this agent as a ToolDefinition usable by another agent’s .tool(...). Mirrors Python BaseAgent.as_tool.

The tool takes a single string argument (default name "task") and, on each call, runs this agent and returns the response text. By default each call runs statelessly (a fresh session per call); with AsToolOptions::propagate_session the parent agent’s session is forwarded instead (as an AgentSession::child). Set AsToolOptions::stream_callback to observe the sub-agent’s streamed updates, and AsToolOptions::approval_mode to gate calls behind human approval.

A run that ends with pending user-input requests (function-approval requests from the sub-agent’s own tools) cannot be satisfied from within a tool call and surfaces as a tool error — mirroring upstream’s UserInputRequiredException.

let research_tool = researcher.as_tool(AsToolOptions::new().name("research"));
let coordinator = Agent::builder(coordinator_client)
    .tool(research_tool)
    .build();

Trait Implementations§

Source§

impl Clone for Agent

Source§

fn clone(&self) -> Agent

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 SupportsAgentRun for Agent

Source§

fn run<'life0, 'life1, 'async_trait>( &'life0 self, messages: Vec<Message>, session: Option<&'life1 mut AgentSession>, ) -> Pin<Box<dyn Future<Output = Result<AgentResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Run the agent to completion.
Source§

fn run_with_options<'life0, 'life1, 'async_trait>( &'life0 self, messages: Vec<Message>, session: Option<&'life1 mut AgentSession>, options: AgentRunOptions, ) -> Pin<Box<dyn Future<Output = Result<AgentResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Run the agent to completion, applying per-run AgentRunOptions over the agent’s build-time defaults. Read more
Source§

fn run_stream<'life0, 'async_trait>( &'life0 self, messages: Vec<Message>, session: Option<AgentSession>, options: Option<AgentRunOptions>, ) -> Pin<Box<dyn Future<Output = Result<AgentRunStream>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Run the agent and stream incremental AgentResponseUpdates. Read more
Source§

fn id(&self) -> &str

A stable identifier for this agent.
Source§

fn name(&self) -> Option<&str>

The optional human-readable name.
Source§

fn create_session(&self) -> AgentSession

A fresh session for a new conversation.
Source§

fn display_name(&self) -> String

The display name: name if set, else id.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Agent

§

impl !UnwindSafe for Agent

§

impl Freeze for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin 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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> 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 = 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