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
impl Session
Sourcepub async fn connect(&mut self) -> Result<()>
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.
Sourcepub async fn send(&mut self, message: &str) -> Result<String>
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);Sourcepub async fn send_stream(
&mut self,
message: &str,
handler: &mut impl EventHandler,
) -> Result<String>
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 sendhandler- 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?;Sourcepub fn history(&self) -> &[ConversationMessage]
pub fn history(&self) -> &[ConversationMessage]
Get the conversation history.
Returns all messages exchanged in this session, in chronological order.
Sourcepub fn clear(&mut self)
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.
Sourcepub fn stats(&self) -> &TaskStats
pub fn stats(&self) -> &TaskStats
Get the accumulated statistics for this session.
Statistics are accumulated across all turns in the session.
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
Get the session ID.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if the session is connected.
Sourcepub async fn interrupt(&self) -> Result<()>
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.
Sourcepub async fn disconnect(&mut self) -> Result<()>
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.