Skip to main content

Conversation

Struct Conversation 

Source
pub struct Conversation<R: CommandRunner = DefaultRunner> { /* private fields */ }
Expand description

Stateful multi-turn conversation wrapper around ClaudeClient.

Manages session_id automatically across turns using --resume. The base config is cloned per turn; each turn builds a temporary config with --resume <session_id> injected.

§Design decisions

Ownership model: Owns cloned copies of ClaudeConfig and the runner instead of borrowing &ClaudeClient. ClaudeClient is stateless (config + runner only, no connection pool), so cloning is cheap and avoids lifetime parameters that complicate async usage (spawn, struct storage).

session_id storage: Uses Arc<Mutex<Option<String>>> so that the streaming path can update the session ID while the caller consumes the returned Stream (which outlives the &mut self borrow).

§Note

Callers must set ClaudeConfigBuilder::no_session_persistence(false) in the config for multi-turn to work. The library does not override this; option validation is the CLI’s responsibility.

Implementations§

Source§

impl<R: CommandRunner> Conversation<R>

Source

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

Returns the current session ID, or None if no turn has completed.

Source§

impl<R: CommandRunner + Clone> Conversation<R>

Source

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

Sends a prompt and returns the response.

Shorthand for ask_with(prompt, |b| b).

Source

pub async fn ask_with<F>( &mut self, prompt: &str, config_fn: F, ) -> Result<ClaudeResponse, ClaudeError>

Sends a prompt with per-turn config overrides and returns the response.

The closure receives a ClaudeConfigBuilder pre-filled with the base config. Overrides apply to this turn only; the base config is unchanged.

Source§

impl Conversation

Source

pub async fn ask_stream( &mut self, prompt: &str, ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamEvent, ClaudeError>> + Send>>, ClaudeError>

Available on crate feature stream only.

Sends a prompt and returns a stream of events.

Shorthand for ask_stream_with(prompt, |b| b).

Only available for Conversation<DefaultRunner> (i.e., conversations created via ClaudeClient::new). The CommandRunner trait’s run method returns a completed std::process::Output, which cannot support streaming; therefore streaming always spawns a real CLI subprocess.

Note: Timeout from the base config is not applied to streams. Use tokio_stream::StreamExt::timeout() if needed.

Source

pub async fn ask_stream_with<F>( &mut self, prompt: &str, config_fn: F, ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamEvent, ClaudeError>> + Send>>, ClaudeError>

Available on crate feature stream only.

Sends a prompt with per-turn config overrides and returns a stream.

The closure receives a ClaudeConfigBuilder pre-filled with the base config. Overrides apply to this turn only; the base config is unchanged.

All events are passed through transparently. Internally, session_id is captured from StreamEvent::SystemInit and updated from StreamEvent::Result.

Only available for Conversation<DefaultRunner>. See ask_stream for details on the streaming constraint.

Trait Implementations§

Source§

impl<R: Debug + CommandRunner> Debug for Conversation<R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for Conversation<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for Conversation<R>
where R: RefUnwindSafe,

§

impl<R> Send for Conversation<R>

§

impl<R> Sync for Conversation<R>

§

impl<R> Unpin for Conversation<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for Conversation<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for Conversation<R>
where R: UnwindSafe,

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