Skip to main content

Session

Struct Session 

Source
pub struct Session<T, S = Value> { /* private fields */ }
Expand description

A conversation with an agent.

Holds the thread id, the messages both sides have said, and the application state. S is the type the state deserializes into, and it is inferred from whatever the caller does with an Update::State — a Session handed to a function expecting Session<T, MyState>, or a match arm that keeps the state in a typed local, needs no turbofish at all. Spell it only when nothing else names it: Session::<_>::new(transport, thread) falls back to the serde_json::Value default, and Session::<_, MyState>::new(…) pins it by hand.

To stream updates, S must be Deserialize + Clone + Unpin — an Update::State carries the state by value, so a view can hold it after the run has moved on. #[derive(Clone, Deserialize)] on a plain struct is all that takes.

Implementations§

Source§

impl<T, S> Session<T, S>

Source

pub fn new(transport: T, thread_id: impl Into<ThreadId>) -> Self
where T: Transport,

A new conversation over transport.

The T: Transport bound is checked here, at the construction site, rather than on Session itself — so something that is not a transport is an error on this line instead of on the first send, which is usually in another file:

use ag_ui::client::Session;

// error[E0277]: the trait bound `str: Transport` is not satisfied,
// and the note lists the types that do implement it.
let session = Session::<_>::new("http://localhost:8080/agent", "thread-1");

(A URL is the mistake worth catching: it is what a transport is made from, so it reads plausible.) The bound stays off the struct because a bound there is viral — every application helper naming Session<T, S> would have to repeat it, including ones that only read messages. tests/bounds.rs is what says so.

Source

pub fn builder( transport: T, thread_id: impl Into<ThreadId>, ) -> SessionBuilder<T, S>
where T: Transport,

A builder, for seeding history, tools, context, or turning verification off.

Takes the same T: Transport bound as new, and for the same reason. Everything after it — including build — is unbounded.

Source

pub fn thread_id(&self) -> &ThreadId

The conversation this session is part of.

Source

pub fn messages(&self) -> &[Message]

The assembled conversation, oldest first — everything the user sent and everything the agent has said across every run.

Source

pub fn state(&self) -> Option<&S>

The application state in the caller’s type, once the agent has published one that deserializes.

Source

pub fn raw_state(&self) -> &Value

The application state as raw JSON. Always current, even when the typed view is not.

Source

pub fn reasoning(&self) -> &[ReasoningMessage]

The reasoning messages, kept out of the transcript.

Source

pub fn interrupts(&self) -> &[Interrupt]

What the agent is waiting for, if the last run paused.

Source

pub fn subagents(&self) -> &[Subagent]

The subagent invocations announced so far, across runs — a suspended one stays until the run that resumes it announces it again.

Source

pub fn subagent(&self, run_id: &SubagentRunId) -> Option<&Subagent>

One subagent invocation by id — what a view does with the Message::subagent_run_id on a message it is about to draw.

Source

pub fn applier(&self) -> &Applier

The applier underneath, for a view that wants the raw materialised state.

Source

pub fn agent(&self) -> &RemoteAgent<T>

The low-level agent underneath.

Source

pub fn push_message(&mut self, message: Message)

Appends a message without starting a run — a tool result computed on the client, or history loaded from a store.

Source

pub fn set_state(&mut self, state: impl Into<Value>)

Replaces the state without going through the agent.

Source

pub fn set_tools(&mut self, tools: impl Into<Vec<Tool>>)

Offers a different set of tools from the next run on.

Source

pub fn set_next_run_id(&mut self, run_id: impl Into<RunId>)

Names the next run explicitly, instead of the generated {thread}-run-{n}.

Servers that key resumption on a run id need this; most do not.

Source§

impl<T: Transport, S> Session<T, S>

Source

pub fn send(&mut self, text: impl Into<String>) -> RunStream<'_, T, S>

Sends the user’s turn and streams what the agent does about it.

The message is appended to the conversation before the request goes out, so it is in Session::messages whatever happens to the run.

Source

pub fn send_message(&mut self, message: Message) -> RunStream<'_, T, S>

Sends a message of any role and streams the run.

Source

pub fn run(&mut self) -> RunStream<'_, T, S>

Starts a run without adding anything — after pushing a tool result, or to let an agent continue on its own.

Source

pub fn resume( &mut self, interrupt: &Interrupt, payload: impl Into<Value>, ) -> RunStream<'_, T, S>

Answers one interrupt and resumes the paused run.

The answer’s shape is up to the agent; when the interrupt carried a responseSchema, payload should satisfy it.

Source

pub fn cancel(&mut self, interrupt: &Interrupt) -> RunStream<'_, T, S>

Declines one interrupt and resumes the paused run.

Source

pub fn resume_many( &mut self, entries: impl IntoIterator<Item = ResumeEntry>, ) -> RunStream<'_, T, S>

Answers several interrupts at once — a run can pause on more than one.

Any interrupt left unanswered is dropped: the resumed run supersedes the paused one, and the agent only sees what is in this request. Use ResumeBuilder to answer them all.

Trait Implementations§

Source§

impl<T: Debug, S: Debug> Debug for Session<T, S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, S> Freeze for Session<T, S>
where T: Freeze, S: Freeze,

§

impl<T, S> RefUnwindSafe for Session<T, S>

§

impl<T, S> Send for Session<T, S>
where T: Send, S: Send,

§

impl<T, S> Sync for Session<T, S>
where T: Sync, S: Sync,

§

impl<T, S> Unpin for Session<T, S>
where T: Unpin, S: Unpin,

§

impl<T, S> UnsafeUnpin for Session<T, S>
where T: UnsafeUnpin, S: UnsafeUnpin,

§

impl<T, S> UnwindSafe for Session<T, S>
where T: UnwindSafe, S: UnwindSafe,

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

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.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more