Skip to main content

ActiveSession

Struct ActiveSession 

Source
pub struct ActiveSession<'responder, Link>
where Link: HasPeer<Agent>,
{ /* private fields */ }
Expand description

Active session struct that lets you send prompts and receive updates.

The 'responder lifetime represents the span during which responders (e.g., MCP server handlers) are active. When created via SessionBuilder::start_session, this is 'static because responders are spawned into background tasks. When created via SessionBuilder::run_until, this is tied to the closure scope, preventing Self::proxy_remaining_messages from being called (since the responders would die when the closure returns).

Implementations§

Source§

impl<Link> ActiveSession<'_, Link>
where Link: HasPeer<Agent>,

Source

pub fn session_id(&self) -> &SessionId

Access the session ID.

Source

pub fn modes(&self) -> &Option<SessionModeState>

Access modes available in this session.

Source

pub fn meta(&self) -> &Option<Map<String, Value>>

Access meta data from session response.

Source

pub fn response(&self) -> NewSessionResponse

Build a NewSessionResponse from the session information.

Useful when you need to forward the session response to a client after doing some processing.

Source

pub fn connection(&self) -> ConnectionTo<Link>

Access the underlying connection context used to communicate with the agent.

Source

pub fn send_prompt(&mut self, prompt: impl ToString) -> Result<(), Error>

Send a prompt to the agent. You can then read messages sent in response.

Source

pub async fn read_update(&mut self) -> Result<SessionMessage, Error>

Read an update from the agent in response to the prompt.

Source

pub async fn read_to_string(&mut self) -> Result<String, Error>

Read all updates until the end of the turn and create a string. Ignores non-text updates.

Source§

impl<Link> ActiveSession<'static, Link>
where Link: HasPeer<Agent>,

Source

pub fn proxy_remaining_messages(self) -> Result<(), Error>
where Link: HasPeer<Client>,

Proxy all remaining messages for this session between client and agent.

Use this when you want to inject MCP servers into a session but don’t need to actively interact with it after setup. The session messages will be proxied between client and agent automatically.

This consumes the ActiveSession since you’re giving up active control.

This method is only available on ActiveSession<'static, _> (from SessionBuilder::start_session) because it requires responders to outlive the method call.

§Message Ordering Guarantees

This method ensures proper handoff from active session mode to proxy mode without losing or reordering messages:

  1. Stop the session handler - Drop the registration that routes messages to update_rx. After this, no new messages will be queued.
  2. Close the channel - Drop update_tx so we can detect when the channel is fully drained.
  3. Drain queued messages - Forward any messages that were already queued in update_rx to the client, preserving order.
  4. Install proxy handler - Now that all queued messages are forwarded, install the proxy handler to handle future messages.

This sequence prevents the race condition where messages could be delivered out of order or lost during the transition.

Trait Implementations§

Source§

impl<'responder, Link> Debug for ActiveSession<'responder, Link>
where Link: HasPeer<Agent> + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'responder, Link> Freeze for ActiveSession<'responder, Link>
where Link: Freeze,

§

impl<'responder, Link> !RefUnwindSafe for ActiveSession<'responder, Link>

§

impl<'responder, Link> Send for ActiveSession<'responder, Link>

§

impl<'responder, Link> Sync for ActiveSession<'responder, Link>

§

impl<'responder, Link> Unpin for ActiveSession<'responder, Link>
where Link: Unpin,

§

impl<'responder, Link> UnsafeUnpin for ActiveSession<'responder, Link>
where Link: UnsafeUnpin,

§

impl<'responder, Link> !UnwindSafe for ActiveSession<'responder, Link>

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> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

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