pub struct Session {
pub id: String,
pub model: String,
pub messages: VecDeque<Message>,
pub total_tokens: usize,
pub max_context_tokens: usize,
pub created_at: Instant,
pub last_active: Instant,
}Expand description
A conversation session with bounded context-window management.
Fields§
§id: StringUnique session identifier.
model: StringModel associated with this session.
messages: VecDeque<Message>Ordered message history (oldest first).
total_tokens: usizeRunning sum of tokens across all messages currently in messages.
max_context_tokens: usizeHard ceiling on context tokens; messages are trimmed to stay below this.
created_at: InstantWall-clock time at which the session was created.
last_active: InstantWall-clock time of the last add_message call.
Implementations§
Source§impl Session
impl Session
Sourcepub fn add_message(&mut self, role: String, content: String, tokens: usize)
pub fn add_message(&mut self, role: String, content: String, tokens: usize)
Append a message and update the running token total.
Trims the oldest messages first if adding tokens would overflow the
context window.
Sourcepub fn trim_to_fit(&mut self, new_tokens: usize)
pub fn trim_to_fit(&mut self, new_tokens: usize)
Drop the oldest messages until total_tokens + new_tokens fits within
max_context_tokens.
Sourcepub fn token_utilization(&self) -> f64
pub fn token_utilization(&self) -> f64
Fraction of the context window currently occupied (0.0–1.0).
Sourcepub fn is_expired(&self, timeout_secs: u64) -> bool
pub fn is_expired(&self, timeout_secs: u64) -> bool
Returns true if the session has not been active for longer than
timeout_secs seconds.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Session
impl RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
impl UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request