Session

Struct Session 

Source
pub struct Session<S: SessionState> {
    data: SessionRecord,
    _state: PhantomData<S>,
}
Expand description

A stateful LLM session with compile-time state tracking.

The type parameter S indicates the current state of the session. Methods are only available in appropriate states:

  • Session<Created>: Can be activated
  • Session<Active>: Can record deltas, close, or expire
  • Session<Closed>: Terminal, no further transitions
  • Session<SessionExpired>: Terminal, no further transitions

Fields§

§data: SessionRecord§_state: PhantomData<S>

Implementations§

Source§

impl<S: SessionState> Session<S>

Source

pub fn data(&self) -> &SessionRecord

Access the underlying session data (read-only).

Source

pub fn session_id(&self) -> SessionId

Get the session ID.

Source

pub fn tenant_id(&self) -> TenantId

Get the tenant ID.

Source

pub fn provider_id(&self) -> &str

Get the provider ID.

Source

pub fn model(&self) -> &str

Get the model.

Source

pub fn created_at(&self) -> Timestamp

Get when the session was created.

Source

pub fn round_count(&self) -> u64

Get the round count.

Source

pub fn into_data(self) -> SessionRecord

Consume and return the underlying data (for serialization).

Source§

impl Session<Created>

Source

pub fn new(data: SessionRecord) -> Self

Create a new session in the Created state.

Source

pub fn activate(self, activated_at: Timestamp) -> Session<Active>

Activate the session.

Transitions to Session<Active>. Consumes the current session.

Source§

impl Session<Active>

Source

pub fn activated_at(&self) -> Timestamp

Get when the session was activated.

Source

pub fn record_delta(&mut self, delta_tokens: u64)

Record a delta (new messages sent to the provider).

This is a looping state — it does not consume self. Increments round_count and accumulates delta_tokens.

Source

pub fn tokens_saved(&self) -> u64

Calculate total tokens saved by using stateful sessions.

Without sessions, each round would re-send the full initial context. Savings = (round_count * initial_tokens) - delta_tokens

Source

pub fn close(self, closed_at: Timestamp) -> Session<Closed>

Close the session normally.

Transitions to Session<Closed> (terminal state). Consumes the current session.

Source

pub fn expire(self, expired_at: Timestamp) -> Session<SessionExpired>

Expire the session due to TTL.

Transitions to Session<SessionExpired> (terminal state). Consumes the current session.

Source§

impl Session<Closed>

Source

pub fn closed_at(&self) -> Timestamp

Get when the session was closed.

Source§

impl Session<SessionExpired>

Source

pub fn expired_at(&self) -> Timestamp

Get when the session expired.

Trait Implementations§

Source§

impl<S: Clone + SessionState> Clone for Session<S>

Source§

fn clone(&self) -> Session<S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + SessionState> Debug for Session<S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for Session<S>

§

impl<S> RefUnwindSafe for Session<S>
where S: RefUnwindSafe,

§

impl<S> Send for Session<S>

§

impl<S> Sync for Session<S>

§

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

§

impl<S> UnwindSafe for Session<S>
where 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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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

Source§

type Output = T

Should always be Self
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.