Skip to main content

Session

Struct Session 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn kill(self) -> Result<()>

Kill the subprocess immediately.

Source

pub fn is_finished(&self) -> bool

Returns true if the session has received a result message or the subprocess has exited.

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