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>
impl<'a> Session<'a>
Sourcepub fn from_result(claude: &'a Claude, result: &QueryResult) -> Self
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);Sourcepub fn from_id(claude: &'a Claude, session_id: impl Into<String>) -> Self
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.
Sourcepub async fn continue_recent(
claude: &'a Claude,
prompt: impl Into<String>,
) -> Result<(Self, QueryResult)>
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.
Sourcepub fn query(&mut self, prompt: impl Into<String>) -> SessionQuery<'_, 'a>
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?;Sourcepub async fn fork(
&self,
prompt: impl Into<String>,
) -> Result<(Session<'a>, QueryResult)>
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.
Sourcepub fn total_cost_usd(&self) -> f64
pub fn total_cost_usd(&self) -> f64
Get cumulative cost in USD across all queries in this session.
Sourcepub fn total_turns(&self) -> u32
pub fn total_turns(&self) -> u32
Get cumulative turn count across all queries in this session.