Skip to main content

Session

Struct Session 

Source
pub struct Session<'a> { /* private fields */ }
Expand description

A type-safe session handle for multi-turn conversations.

Session wraps a Claude client reference and a session ID, providing .query() that automatically resumes the session. It tracks cumulative cost and turn count across all queries in the session.

Conflicting session flags are impossible because the session mode is encoded in construction rather than as independent builder methods.

Implementations§

Source§

impl<'a> Session<'a>

Source

pub fn from_result(claude: &'a Claude, result: &QueryResult) -> Self

Create a session from a completed query result.

This is the most common way to start a session: run an initial QueryCommand::execute_json() and then wrap the result.

§Example
use claude_wrapper::{Claude, QueryCommand};
use claude_wrapper::session::Session;

let claude = Claude::builder().build()?;
let result = QueryCommand::new("hello")
    .execute_json(&claude).await?;

let mut session = Session::from_result(&claude, &result);
Source

pub fn from_id(claude: &'a Claude, session_id: impl Into<String>) -> Self

Attach to an existing session by ID.

Cost and turn counters start at zero since we have no history.

Source

pub async fn continue_recent( claude: &'a Claude, prompt: impl Into<String>, ) -> Result<(Self, QueryResult)>

Continue the most recent session.

Runs the first query with --continue to discover the session ID, then returns a Session that uses --resume for subsequent queries.

Source

pub fn query(&mut self, prompt: impl Into<String>) -> SessionQuery<'_, 'a>

Send a follow-up query in this session.

Returns a SessionQuery builder with --resume pre-set. Configure additional options (model, effort, etc.) on the builder, then call .execute().

§Example
let mut session = Session::from_result(&claude, &result);

let follow_up = session.query("what about the edge cases?")
    .model("opus")
    .max_turns(5)
    .execute()
    .await?;
Source

pub async fn fork( &self, prompt: impl Into<String>, ) -> Result<(Session<'a>, QueryResult)>

Fork this session into a new one.

Sends a query with --resume and --fork-session, creating a new session branched from this one. Returns the new Session and the query result. The original session is not modified.

Source

pub fn id(&self) -> &str

Get the current session ID.

Source

pub fn total_cost_usd(&self) -> f64

Get cumulative cost in USD across all queries in this session.

Source

pub fn total_turns(&self) -> u32

Get cumulative turn count across all queries in this session.

Trait Implementations§

Source§

impl<'a> Debug for Session<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Session<'a>

§

impl<'a> RefUnwindSafe for Session<'a>

§

impl<'a> Send for Session<'a>

§

impl<'a> Sync for Session<'a>

§

impl<'a> Unpin for Session<'a>

§

impl<'a> UnsafeUnpin for Session<'a>

§

impl<'a> UnwindSafe for Session<'a>

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