pub struct Session { /* private fields */ }Expand description
A live session with a running claude subprocess.
Provides methods for sending messages, sending tool results, reading events, and interrupting the model.
Implementations§
Source§impl Session
impl Session
Sourcepub async fn send_message(&mut self, text: &str) -> Result<()>
pub async fn send_message(&mut self, text: &str) -> Result<()>
Send a text message to the model.
This writes a user message to the subprocess’s stdin in NDJSON format.
§Errors
Returns an error if the session is not connected or writing fails.
Sourcepub async fn send_message_with_images(
&mut self,
text: &str,
images: Vec<(String, String)>,
) -> Result<()>
pub async fn send_message_with_images( &mut self, text: &str, images: Vec<(String, String)>, ) -> Result<()>
Send a message with text and images.
Images are (media_type, base64_data) tuples.
Sourcepub async fn send_tool_result(&mut self, result: &ToolResult) -> Result<()>
pub async fn send_tool_result(&mut self, result: &ToolResult) -> Result<()>
Send a tool result back to the model.
After receiving a tool-use request via next_event,
execute the tool and send the result back with this method.
§Errors
Returns an error if the session is not connected or writing fails.
Sourcepub async fn next_event(&mut self) -> Result<Option<Event>>
pub async fn next_event(&mut self) -> Result<Option<Event>>
Get the next event from the stream.
Returns Ok(None) when the session is complete (either the subprocess
exited or a result message was received).
§Errors
Returns an error on I/O failure, JSON parse failure, or if the subprocess dies unexpectedly.
Sourcepub async fn interrupt(&mut self) -> Result<()>
pub async fn interrupt(&mut self) -> Result<()>
Send an interrupt signal to the subprocess (SIGINT).
This tells Claude to stop its current operation. The session remains open and can continue to receive events and send new messages.
§Errors
Returns an error if the signal cannot be sent.
Sourcepub fn close_stdin(&mut self)
pub fn close_stdin(&mut self)
Close stdin, signaling to the CLI that no more messages will be sent.
After this call, send_message and
send_tool_result will return
SdkError::NotConnected. The session remains open for reading
events via next_event until the CLI process
finishes.
This is useful when you have sent all your messages and want the CLI
to process them and emit its response. In --input-format stream-json
mode, the CLI may wait for EOF on stdin before finalizing.
Sourcepub async fn close(self) -> Result<(Option<i32>, Option<String>)>
pub async fn close(self) -> Result<(Option<i32>, Option<String>)>
Close the session by closing stdin and waiting for the process to exit.
Returns the exit code and any captured stderr output.
Sourcepub async fn wait_for_stderr(&mut self) -> Result<Option<String>>
pub async fn wait_for_stderr(&mut self) -> Result<Option<String>>
Wait for the subprocess to exit and return any captured stderr.
Unlike close, this does not consume the session.
Useful for retrieving stderr diagnostics after the session has finished.
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Returns true if the session has received a result message or the
subprocess has exited.