pub struct Session { /* private fields */ }Expand description
A multi-turn conversation handle.
Owns an Arc<Claude> so it can be moved between tasks and live
inside long-running actors. Tracks session_id, cumulative cost,
turn count, and per-turn result history.
Implementations§
Source§impl Session
impl Session
Sourcepub fn new(claude: Arc<Claude>) -> Self
pub fn new(claude: Arc<Claude>) -> Self
Start a fresh session. The first turn will discover a session id
from its result; subsequent turns reuse it via --resume.
Sourcepub fn resume(claude: Arc<Claude>, session_id: impl Into<String>) -> Self
pub fn resume(claude: Arc<Claude>, session_id: impl Into<String>) -> Self
Reattach to an existing session by id. The next turn immediately
passes --resume <id>. Cost and turn counters start at zero
since no history is available.
Sourcepub async fn send(&mut self, prompt: impl Into<String>) -> Result<QueryResult>
pub async fn send(&mut self, prompt: impl Into<String>) -> Result<QueryResult>
Send a plain-prompt turn. Equivalent to
execute(QueryCommand::new(prompt)).
Sourcepub async fn execute(&mut self, cmd: QueryCommand) -> Result<QueryResult>
pub async fn execute(&mut self, cmd: QueryCommand) -> Result<QueryResult>
Send a turn with a fully-configured QueryCommand.
Any session-related flags on cmd (--resume, --continue,
--session-id, --fork-session) are overridden with this
session’s current id, so they can’t conflict.
Sourcepub async fn stream<F>(
&mut self,
prompt: impl Into<String>,
handler: F,
) -> Result<()>where
F: FnMut(StreamEvent),
pub async fn stream<F>(
&mut self,
prompt: impl Into<String>,
handler: F,
) -> Result<()>where
F: FnMut(StreamEvent),
Stream a plain-prompt turn, dispatching each NDJSON event to
handler. The session’s id is captured from the first event
that carries one, so subsequent turns can resume, and the id
persists even if the stream errors partway through.
Sourcepub async fn stream_execute<F>(
&mut self,
cmd: QueryCommand,
handler: F,
) -> Result<()>where
F: FnMut(StreamEvent),
pub async fn stream_execute<F>(
&mut self,
cmd: QueryCommand,
handler: F,
) -> Result<()>where
F: FnMut(StreamEvent),
Stream a turn with a fully-configured QueryCommand, with the
same session-id capture semantics as Session::stream.
The command’s output format is forced to stream-json and any
session-related flags are overridden as in Session::execute.
Sourcepub fn total_cost_usd(&self) -> f64
pub fn total_cost_usd(&self) -> f64
Cumulative cost in USD across all turns in this session.
Sourcepub fn total_turns(&self) -> u32
pub fn total_turns(&self) -> u32
Cumulative turn count across all turns in this session.
Sourcepub fn history(&self) -> &[QueryResult]
pub fn history(&self) -> &[QueryResult]
Full per-turn result history.
Sourcepub fn last_result(&self) -> Option<&QueryResult>
pub fn last_result(&self) -> Option<&QueryResult>
Result of the most recent turn, if any.