pub struct Client { /* private fields */ }Expand description
A stateful Claude Code client that manages a persistent session.
§Lifecycle
- Create with
Client::new(config)orClient::with_transport(config, transport) - Call
connect()to spawn the CLI and read the init message - Use
send()to send prompts and stream responses - Call
close()to shut down cleanly
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(config: ClientConfig) -> Result<Self>
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.
Sourcepub fn with_transport(
config: ClientConfig,
transport: Arc<dyn Transport>,
) -> Result<Self>
pub fn with_transport( config: ClientConfig, transport: Arc<dyn Transport>, ) -> Result<Self>
Create a client with a custom transport (useful for testing).
Sourcepub async fn connect(&mut self) -> Result<SessionInfo>
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.
Sourcepub fn send(
&self,
prompt: impl Into<String>,
) -> Result<impl Stream<Item = Result<Message>> + '_>
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).
Sourcepub fn send_content(
&self,
content: Vec<UserContent>,
) -> Result<impl Stream<Item = Result<Message>> + '_>
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.
Sourcepub fn receive_messages(
&self,
) -> Result<impl Stream<Item = Result<Message>> + '_>
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.
Sourcepub async fn write_to_stdin(&self, text: &str) -> Result<()>
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.
Sourcepub async fn respond_to_permission(
&self,
request_id: &str,
decision: PermissionDecision,
) -> Result<()>
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.
Sourcepub async fn set_model(&self, model: Option<&str>) -> Result<()>
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.
Sourcepub async fn set_permission_mode(&self, mode: PermissionMode) -> Result<()>
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.
Sourcepub fn read_timeout(&self) -> Option<Duration>
pub fn read_timeout(&self) -> Option<Duration>
Returns the configured read timeout.
Sourcepub fn session_id(&self) -> Option<&str>
pub fn session_id(&self) -> Option<&str>
Returns the session ID if connected.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns true if the client is connected.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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