Skip to main content

Client

Struct Client 

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

A reusable conversation client with optional automatic tool execution.

Manual mode streams blocks as they arrive. Auto mode runs tool rounds first, then yields the final response. Use Client::interrupt_handle for cancellation from another task without locking the client across an await.

Implementations§

§

impl Client

pub fn new(options: AgentOptions) -> Result<Self>

Creates a reusable HTTP client with the configured timeout and redirect policy.

§

impl Client

pub async fn send(&mut self, prompt: &str) -> Result<()>

Starts a request from a text prompt, applying UserPromptSubmit hooks first. The prompt enters history before HTTP I/O, even on transport failure. Empty prompts are retained for tool continuation. Starting a new request discards pending output from the previous one.

§

impl Client

pub async fn send_message(&mut self, message: Message) -> Result<()>

Starts a request with a pre-built message, including image content. UserPromptSubmit hooks apply only to Self::send, which accepts text. The message enters history before HTTP I/O; pending output is discarded.

§

impl Client

pub async fn receive(&mut self) -> Result<Option<ContentBlock>>

Receives the next content block, or None when the request has finished.

Manual mode records the complete assistant turn at EOF; errors and abandoned streams discard partial history. Auto mode executes tool rounds before yielding final text. Read Self::finish_reason and Self::reasoning after draining.

pub fn interrupt(&self)

Requests cancellation between stream events, tool calls, and hook phases. Pending network reads still wait for data or the HTTP timeout; a running tool or hook future finishes first. A new send resets the flag.

pub fn interrupt_handle(&self) -> Arc<AtomicBool> ⓘ

Shares the cancellation flag for use from another task.

pub fn history(&self) -> &[Message]

Returns conversation history, excluding the separately configured system prompt.

pub fn history_mut(&mut self) -> &mut Vec<Message>

Allows explicit history edits, such as crate::truncate_messages.

pub fn options(&self) -> &AgentOptions

Returns the client’s configuration.

pub fn finish_reason(&self) -> Option<&FinishReason>

Why the last completed generation stopped, or MaxToolIterations when the auto loop reached its limit. Reset when a new request starts.

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

Captured reasoning across all rounds of the latest request, when opted in. Reasoning never enters conversation history.

§

impl Client

pub fn clear_history(&mut self)

Clears conversation history and abandons pending output, retaining options.

pub fn add_tool_result( &mut self, tool_use_id: &str, content: Value, ) -> Result<()>

Adds a manual tool result after flushing the pending assistant tool call. Continue the conversation with send("") after supplying the results.

pub fn get_tool(&self, name: &str) -> Option<&Tool>

Looks up a registered tool by name.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Client

§

impl !Sync for Client

§

impl !UnwindSafe for Client

§

impl Freeze for Client

§

impl Send for Client

§

impl Unpin for Client

§

impl UnsafeUnpin for Client

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: Sized + 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: Sized + 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 = !

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