Skip to main content

AgentSession

Struct AgentSession 

Source
pub struct AgentSession {
    pub state: SessionState,
    pub context_providers: Vec<Arc<dyn ContextProvider>>,
    /* private fields */
}
Expand description

A conversation session: a lightweight identity + state container.

Message history is not stored here any more — see crate::history::HistoryProvider and crate::history::ensure_history_provider.

Fields§

§state: SessionState

Free-form session state (for context providers to persist per-session data across runs). Shared by reference across clones of this session — see SessionState.

§context_providers: Vec<Arc<dyn ContextProvider>>

Context providers associated with this session (memory/RAG/history injection). Combined with an agent’s own providers at request time — see Agent::combined_providers.

Implementations§

Source§

impl AgentSession

Source

pub fn new() -> Self

A fresh, local (non-service-managed) session with a newly generated session_id.

Source

pub fn child(&self) -> AgentSession

A child session for delegating part of this conversation to a sub-agent (the as_tool(propagate_session) path).

The child keeps this session’s session_id and shares its state bag by reference, but gets an isolated (cleared) service_session_id and no context providers. Isolating the server-side conversation pointer matters: after the parent’s first model call, a service-managed session carries the parent conversation id (e.g. an OpenAI Responses previous_response_id); a child that inherited it would submit a follow-up onto a conversation whose tool call is still pending, which the server rejects. Mirrors upstream’s _agent_wrapper child-session construction (_agents.py, microsoft/agent-framework#5875).

Source

pub fn service(id: impl Into<String>) -> Self

A service-managed session identified by a conversation id.

Source

pub fn with_context_providers( self, providers: Vec<Arc<dyn ContextProvider>>, ) -> Self

Attach context providers to this session, replacing any previously set.

Source

pub fn session_id(&self) -> &str

This session’s local identifier.

Source

pub fn service_session_id(&self) -> Option<&str>

The service-side conversation id, if any.

Source

pub fn set_service_session_id(&mut self, id: impl Into<String>)

Set the service-side conversation id explicitly.

Source

pub fn try_adopt_service_session_id(&mut self, id: &str) -> bool

Adopt a service-managed conversation id returned by the chat service (e.g. an OpenAI Responses previous_response_id or an Azure AI thread id), so follow-up runs continue the same service conversation.

Returns true when the id was newly adopted (it differed from what the session already carried), false when it was already current.

Source

pub fn to_dict(&self) -> Value

Serialize this session’s {session_id, service_session_id, state} to JSON. Conversation history is deliberately not included — it lives in whichever HistoryProvider, if any, is attached to context_providers; serialize that separately (e.g. InMemoryHistoryProvider::to_dict).

Source

pub fn from_dict(state: &Value) -> Result<Self>

Reconstruct a session from state produced by AgentSession::to_dict.

context_providers are not restored by this call — callers reattach their own (including any HistoryProvider, whose own state is serialized/restored independently).

Trait Implementations§

Source§

impl Clone for AgentSession

Source§

fn clone(&self) -> AgentSession

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentSession

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for AgentSession

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
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