Skip to main content

SessionManager

Struct SessionManager 

Source
pub struct SessionManager { /* private fields */ }
Expand description

Manages session lifecycle — creation, channel-aware resolution, closure, and usage tracking.

Implementations§

Source§

impl SessionManager

Source

pub fn from_pool(pool: SqlitePool) -> Self

Create a SessionManager from a shared pool.

The pool should already have migrations applied (via CoreDb).

Source

pub async fn resolve_session( &self, channel: &Channel, key: &str, gap_minutes: Option<i64>, ) -> Result<SessionDecision>

Resolve session for a given channel and key.

  • Main (explicit): always continues the matching open session if one exists.
  • Telegram (time-gap): continues if last message was within the gap threshold, otherwise auto-closes the old session and returns New.

gap_minutes is the inactivity gap from config. Pass None for explicit channels that don’t use time-gap sessions.

Source

pub async fn resolve_session_for_user( &self, channel: &Channel, key: &str, gap_minutes: Option<i64>, user_id: &str, ) -> Result<SessionDecision>

Resolve session for a given channel, key, and user.

Source

pub async fn create_session( &self, channel: &Channel, key: &str, ) -> Result<String>

Create a new session for a channel and key, returning its ID.

Source

pub async fn create_session_for_user( &self, channel: &Channel, key: &str, user_id: &str, ) -> Result<String>

Create a new session for a channel, key, and user, returning its ID.

Source

pub async fn create_session_full( &self, channel: &Channel, key: &str, user_id: &str, triggered_by: Option<&str>, ) -> Result<String>

Create a new session with full metadata, including an optional trigger source.

triggered_by records the cron job name (e.g. "daily-digest") or "__heartbeat__" when the session is created by the scheduler.

Source

pub async fn close_session(&self, id: &str, summary: &str) -> Result<()>

Mark a session as closed with an optional summary.

Source

pub async fn mark_read(&self, id: &str, is_read: bool) -> Result<()>

Mark a session as read or unread.

Source

pub async fn touch_session(&self, id: &str) -> Result<()>

Update the last_message_at timestamp and increment message_count.

Source

pub async fn set_title_if_empty(&self, id: &str, title: &str) -> Result<()>

Set the session title if it hasn’t been set yet.

Source

pub async fn record_usage( &self, session_id: &str, usage: &UsageRecord, turn: u32, ) -> Result<()>

Record token usage for a turn.

Source

pub async fn list_sessions(&self, limit: usize) -> Result<Vec<SessionMeta>>

List sessions, most recent first.

Source

pub async fn get_session(&self, id: &str) -> Result<Option<SessionMeta>>

Get a specific session by ID.

Source

pub async fn session_usage(&self, session_id: &str) -> Result<UsageSummary>

Get total usage stats for a session.

total_input_tokens includes uncached, cache-read, and cache-write tokens so the caller gets the true context size. Cache breakdown is available via total_cache_read / total_cache_write.

Source

pub async fn cost_overview(&self, since: Option<&str>) -> Result<CostOverview>

Get a full cost overview with breakdowns by user and model.

If since is provided (RFC 3339 timestamp), only usage after that time is included.

Source

pub async fn record_compaction( &self, session_id: &str, trigger: &str, pre_tokens: u64, summary: &str, messages_compacted: usize, ) -> Result<()>

Record a compaction event for a session.

Source

pub async fn save_message( &self, session_id: &str, role: &str, content: &str, ) -> Result<()>

Save a message to a session.

When the first “user” message is saved, the session title is automatically set to the message text (truncated to 100 chars).

Source

pub async fn get_messages( &self, session_id: &str, ) -> Result<Vec<SessionMessage>>

Get all messages for a session, ordered by ID.

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

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