pub struct ClaudeClient<R: CommandRunner = DefaultRunner> { /* private fields */ }Expand description
Claude Code CLI client.
Implementations§
Source§impl ClaudeClient
impl ClaudeClient
Sourcepub fn new(config: ClaudeConfig) -> Self
pub fn new(config: ClaudeConfig) -> Self
Creates a new client with the default DefaultRunner.
Source§impl ClaudeClient
impl ClaudeClient
Sourcepub async fn ask_stream(
&self,
prompt: &str,
) -> Result<Pin<Box<dyn Stream<Item = Result<StreamEvent, ClaudeError>> + Send>>, ClaudeError>
Available on crate feature stream only.
pub async fn ask_stream( &self, prompt: &str, ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamEvent, ClaudeError>> + Send>>, ClaudeError>
stream only.Sends a prompt and returns a stream of events.
Spawns the CLI with --output-format stream-json and streams events
in real-time. The stream ends with a StreamEvent::Result on success.
For real-time token-level streaming, enable
crate::ClaudeConfigBuilder::include_partial_messages. This produces
StreamEvent::Text / StreamEvent::Thinking delta chunks.
Without it, only complete StreamEvent::AssistantText /
StreamEvent::AssistantThinking messages are emitted.
Use crate::ClaudeConfigBuilder::stream_idle_timeout to set an idle timeout.
If no event arrives within the specified duration, the stream yields
ClaudeError::Timeout and terminates.
Source§impl<R: CommandRunner> ClaudeClient<R>
impl<R: CommandRunner> ClaudeClient<R>
Sourcepub fn with_runner(config: ClaudeConfig, runner: R) -> Self
pub fn with_runner(config: ClaudeConfig, runner: R) -> Self
Creates a new client with a custom CommandRunner for testing.
Sourcepub async fn ask_structured<T: DeserializeOwned>(
&self,
prompt: &str,
) -> Result<T, ClaudeError>
pub async fn ask_structured<T: DeserializeOwned>( &self, prompt: &str, ) -> Result<T, ClaudeError>
Sends a prompt and deserializes the result into T.
Requires json_schema to be set on the config beforehand.
Use generate_schema to auto-generate it
(requires the structured feature).
Sourcepub async fn ask(&self, prompt: &str) -> Result<ClaudeResponse, ClaudeError>
pub async fn ask(&self, prompt: &str) -> Result<ClaudeResponse, ClaudeError>
Sends a prompt and returns the response.
Source§impl<R: CommandRunner + Clone> ClaudeClient<R>
impl<R: CommandRunner + Clone> ClaudeClient<R>
Sourcepub fn conversation(&self) -> Conversation<R>
pub fn conversation(&self) -> Conversation<R>
Creates a new Conversation for multi-turn interaction.
The conversation manages session_id automatically, injecting
--resume from the second turn onwards.
Callers must set crate::ClaudeConfigBuilder::no_session_persistence(false)
for multi-turn to work.
Sourcepub fn conversation_resume(
&self,
session_id: impl Into<String>,
) -> Conversation<R>
pub fn conversation_resume( &self, session_id: impl Into<String>, ) -> Conversation<R>
Creates a Conversation that resumes an existing session.
The first ask() / ask_stream() call will include --resume
with the given session ID.
Trait Implementations§
Source§impl<R: Clone + CommandRunner> Clone for ClaudeClient<R>
impl<R: Clone + CommandRunner> Clone for ClaudeClient<R>
Source§fn clone(&self) -> ClaudeClient<R>
fn clone(&self) -> ClaudeClient<R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more