pub struct Session {
pub id: String,
pub options: SessionOptions,
/* private fields */
}Expand description
A conversation session with Claude
Session provides a simplified interface for multi-turn conversations with Claude.
It wraps the underlying ClaudeClient and provides easier-to-use methods.
§Example
use claude_agent_sdk::v2::{create_session, SessionOptions};
#[tokio::main]
async fn example() -> Result<(), Box<dyn std::error::Error>> {
let mut session = create_session(SessionOptions::default()).await?;
// Send a message
session.send("What is 2 + 2?").await?;
// Receive all responses
let messages = session.receive().await?;
for msg in messages {
println!("Message: {:?}", msg);
}
// Close the session
session.close().await?;
Ok(())
}Fields§
§id: StringUnique session identifier
options: SessionOptionsSession options
Implementations§
Source§impl Session
impl Session
Sourcepub async fn send(&mut self, message: impl Into<String>) -> Result<()>
pub async fn send(&mut self, message: impl Into<String>) -> Result<()>
Send a message to Claude
This method sends a user message to Claude and queues it for processing.
Call receive() to get Claude’s response.
§Arguments
message- The message text to send
§Example
session.send("Hello, Claude!").await?;§Errors
Returns an error if:
- The message is empty
- Network connection fails
- Session is closed
Sourcepub async fn receive(&self) -> Result<Vec<V2Message>>
pub async fn receive(&self) -> Result<Vec<V2Message>>
Receive messages from Claude
This method returns all pending messages from Claude since the last send() call.
Messages are returned until a Result message is encountered (end of turn).
§Returns
A vector of V2Message objects
§Example
let messages = session.receive().await?;
for msg in messages {
if let Some(text) = msg.as_text() {
println!("Claude: {}", text);
}
}Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Check if the session is still connected
Auto Trait Implementations§
impl Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl !UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more