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 activatedSession<Active>: Can record deltas, close, or expireSession<Closed>: Terminal, no further transitionsSession<SessionExpired>: Terminal, no further transitions
Fields§
§data: SessionRecord§_state: PhantomData<S>Implementations§
Source§impl<S: SessionState> Session<S>
impl<S: SessionState> Session<S>
Sourcepub fn data(&self) -> &SessionRecord
pub fn data(&self) -> &SessionRecord
Access the underlying session data (read-only).
Sourcepub fn session_id(&self) -> SessionId
pub fn session_id(&self) -> SessionId
Get the session ID.
Sourcepub fn provider_id(&self) -> &str
pub fn provider_id(&self) -> &str
Get the provider ID.
Sourcepub fn created_at(&self) -> Timestamp
pub fn created_at(&self) -> Timestamp
Get when the session was created.
Sourcepub fn round_count(&self) -> u64
pub fn round_count(&self) -> u64
Get the round count.
Sourcepub fn into_data(self) -> SessionRecord
pub fn into_data(self) -> SessionRecord
Consume and return the underlying data (for serialization).
Source§impl Session<Active>
impl Session<Active>
Sourcepub fn activated_at(&self) -> Timestamp
pub fn activated_at(&self) -> Timestamp
Get when the session was activated.
Sourcepub fn record_delta(&mut self, delta_tokens: u64)
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.
Sourcepub fn tokens_saved(&self) -> u64
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
Sourcepub fn close(self, closed_at: Timestamp) -> Session<Closed>
pub fn close(self, closed_at: Timestamp) -> Session<Closed>
Close the session normally.
Transitions to Session<Closed> (terminal state).
Consumes the current session.
Sourcepub fn expire(self, expired_at: Timestamp) -> Session<SessionExpired>
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<SessionExpired>
impl Session<SessionExpired>
Sourcepub fn expired_at(&self) -> Timestamp
pub fn expired_at(&self) -> Timestamp
Get when the session expired.