pub struct ActiveSession<'responder, Link>{ /* 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>
impl<Link> ActiveSession<'_, Link>
Sourcepub fn session_id(&self) -> &SessionId
pub fn session_id(&self) -> &SessionId
Access the session ID.
Sourcepub fn modes(&self) -> &Option<SessionModeState>
pub fn modes(&self) -> &Option<SessionModeState>
Access modes available in this session.
Sourcepub fn response(&self) -> NewSessionResponse
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.
Sourcepub fn connection(&self) -> ConnectionTo<Link>
pub fn connection(&self) -> ConnectionTo<Link>
Access the underlying connection context used to communicate with the agent.
Sourcepub fn send_prompt(&mut self, prompt: impl ToString) -> Result<(), Error>
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.
Sourcepub async fn read_update(&mut self) -> Result<SessionMessage, Error>
pub async fn read_update(&mut self) -> Result<SessionMessage, Error>
Read an update from the agent in response to the prompt.
Sourcepub async fn read_to_string(&mut self) -> Result<String, Error>
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>
impl<Link> ActiveSession<'static, Link>
Sourcepub fn proxy_remaining_messages(self) -> Result<(), Error>
pub fn proxy_remaining_messages(self) -> Result<(), Error>
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:
- Stop the session handler - Drop the registration that routes messages
to
update_rx. After this, no new messages will be queued. - Close the channel - Drop
update_txso we can detect when the channel is fully drained. - Drain queued messages - Forward any messages that were already queued
in
update_rxto the client, preserving order. - 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.