Skip to main content

Client

Struct Client 

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

A stateful Claude Code client that manages a persistent session.

§Lifecycle

  1. Create with Client::new(config) or Client::with_transport(config, transport)
  2. Call connect() to spawn the CLI and read the init message
  3. Use send() to send prompts and stream responses
  4. Call close() to shut down cleanly

Implementations§

Source§

impl Client

Source

pub fn new(config: ClientConfig) -> Result<Self>

Create a new client with the given configuration.

This does NOT start the CLI — call connect() next. Validates the config (e.g., cwd existence) and discovers the CLI binary.

§Errors

Returns Error::CliNotFound if the CLI binary cannot be discovered, or Error::Config if the configuration is invalid.

Source

pub fn with_transport( config: ClientConfig, transport: Arc<dyn Transport>, ) -> Result<Self>

Create a client with a custom transport (useful for testing).

Source

pub async fn connect(&mut self) -> Result<SessionInfo>

Connect to the CLI and return the session info from the init message.

This spawns the CLI process (or connects to the mock transport) and starts the background reader task. The entire connect sequence (transport connect + init message read) is subject to connect_timeout.

Source

pub fn send( &self, prompt: impl Into<String>, ) -> Result<impl Stream<Item = Result<Message>> + '_>

Send a text prompt and return a stream of response messages.

The stream yields messages until a Result message is received (which terminates the turn).

Source

pub fn send_content( &self, content: Vec<UserContent>, ) -> Result<impl Stream<Item = Result<Message>> + '_>

Send structured content blocks (text + images) and return a stream of response messages.

This is the multi-modal equivalent of send(). Content is serialised as a JSON user message and written to the CLI’s stdin.

§Errors

Returns Error::Config if content is empty, or Error::NotConnected if the client is not connected.

Source

pub fn receive_messages( &self, ) -> Result<impl Stream<Item = Result<Message>> + '_>

Return a stream of all incoming messages (without sending a prompt).

Useful for consuming messages from a resumed session.

Source

pub async fn write_to_stdin(&self, text: &str) -> Result<()>

Write raw text to the CLI’s stdin without creating a response stream.

Use this only when [receive_messages()] is already consuming responses. Do not call this while a [send()] turn is in progress — doing so interleaves writes on the same stdin handle and produces undefined protocol behaviour.

Source

pub async fn interrupt(&self) -> Result<()>

Send an interrupt signal to the CLI (SIGINT).

Source

pub async fn respond_to_permission( &self, request_id: &str, decision: PermissionDecision, ) -> Result<()>

Respond to a permission request from the CLI.

When the CLI asks for permission to use a tool, this method sends the decision back via the control protocol.

Source

pub async fn set_model(&self, model: Option<&str>) -> Result<()>

Dynamically change the model used for subsequent turns.

Pass None to revert to the session’s default model.

§Errors

Returns an error if the CLI rejects the model change or the control protocol fails.

Source

pub async fn set_permission_mode(&self, mode: PermissionMode) -> Result<()>

Dynamically change the permission mode for the current session.

§Errors

Returns an error if the CLI rejects the mode change or the control protocol fails.

Source

pub fn read_timeout(&self) -> Option<Duration>

Returns the configured read timeout.

Source

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

Returns the session ID if connected.

Source

pub fn is_connected(&self) -> bool

Returns true if the client is connected.

Source

pub async fn close(&mut self) -> Result<Option<i32>>

Close the client and shut down the CLI process.

Returns the CLI process exit code if available. After calling this, the Drop warning will not fire.

Trait Implementations§

Source§

impl Debug for Client

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for Client

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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