Skip to main content

Session

Struct Session 

Source
pub struct Session { /* private fields */ }
Expand description

Multi-turn interactive session with Claude.

A session maintains a persistent connection to Claude, enabling multi-turn conversations while tracking history and statistics.

Sessions are created via Engine::session().

Implementations§

Source§

impl Session

Source

pub async fn connect(&mut self) -> Result<()>

Connect the session.

This must be called before sending messages.

§Errors

Returns an error if the connection fails.

Source

pub async fn send(&mut self, message: &str) -> Result<String>

Send a message and get the complete response.

This method sends a message to Claude and waits for the complete response. For streaming responses, use send_stream.

§Arguments
  • message - The message to send
§Errors

Returns an error if:

  • The session is not connected
  • Sending the message fails
  • Receiving the response fails
§Example
let response = session.send("Hello Claude!").await?;
println!("Claude says: {}", response);
Source

pub async fn send_stream( &mut self, message: &str, handler: &mut impl EventHandler, ) -> Result<String>

Send a message with streaming events.

This method sends a message to Claude and streams the response through an event handler, allowing real-time processing of the response.

§Arguments
  • message - The message to send
  • handler - Event handler for streaming events
§Errors

Returns an error if:

  • The session is not connected
  • Sending the message fails
  • Receiving the response fails
§Example
let mut handler = PrintEventHandler::new().with_auto_flush();
let response = session.send_stream("Explain async/await", &mut handler).await?;
Source

pub fn history(&self) -> &[ConversationMessage]

Get the conversation history.

Returns all messages exchanged in this session, in chronological order.

Source

pub fn clear(&mut self)

Clear the conversation history.

This clears the local history but does not affect the Claude session’s memory. To start a completely fresh conversation, create a new session.

Source

pub fn stats(&self) -> &TaskStats

Get the accumulated statistics for this session.

Statistics are accumulated across all turns in the session.

Source

pub fn session_id(&self) -> &str

Get the session ID.

Source

pub fn is_connected(&self) -> bool

Check if the session is connected.

Source

pub async fn interrupt(&self) -> Result<()>

Interrupt the current operation.

This sends an interrupt signal to stop any ongoing Claude operation.

§Errors

Returns an error if the session is not connected or interruption fails.

Source

pub async fn disconnect(&mut self) -> Result<()>

Disconnect the session.

This cleanly disconnects from Claude. The session cannot be used after disconnection.

§Errors

Returns an error if disconnection fails.

Trait Implementations§

Source§

impl Debug for Session

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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