pub struct SessionManager { /* private fields */ }Expand description
Manages session lifecycle — creation, channel-aware resolution, closure, and usage tracking.
Implementations§
Source§impl SessionManager
impl SessionManager
Sourcepub fn from_pool(pool: SqlitePool) -> Self
pub fn from_pool(pool: SqlitePool) -> Self
Create a SessionManager from a shared pool.
The pool should already have migrations applied (via CoreDb).
Sourcepub async fn resolve_session(
&self,
channel: &Channel,
key: &str,
gap_minutes: Option<i64>,
) -> Result<SessionDecision>
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.
Sourcepub async fn resolve_session_for_user(
&self,
channel: &Channel,
key: &str,
gap_minutes: Option<i64>,
user_id: &str,
) -> Result<SessionDecision>
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.
Sourcepub async fn create_session(
&self,
channel: &Channel,
key: &str,
) -> Result<String>
pub async fn create_session( &self, channel: &Channel, key: &str, ) -> Result<String>
Create a new session for a channel and key, returning its ID.
Sourcepub async fn create_session_for_user(
&self,
channel: &Channel,
key: &str,
user_id: &str,
) -> Result<String>
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.
Sourcepub async fn create_session_full(
&self,
channel: &Channel,
key: &str,
user_id: &str,
triggered_by: Option<&str>,
) -> Result<String>
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.
Sourcepub async fn close_session(&self, id: &str, summary: &str) -> Result<()>
pub async fn close_session(&self, id: &str, summary: &str) -> Result<()>
Mark a session as closed with an optional summary.
Sourcepub async fn mark_read(&self, id: &str, is_read: bool) -> Result<()>
pub async fn mark_read(&self, id: &str, is_read: bool) -> Result<()>
Mark a session as read or unread.
Sourcepub async fn touch_session(&self, id: &str) -> Result<()>
pub async fn touch_session(&self, id: &str) -> Result<()>
Update the last_message_at timestamp and increment message_count.
Sourcepub async fn set_title_if_empty(&self, id: &str, title: &str) -> Result<()>
pub async fn set_title_if_empty(&self, id: &str, title: &str) -> Result<()>
Set the session title if it hasn’t been set yet.
Sourcepub async fn record_usage(
&self,
session_id: &str,
usage: &UsageRecord,
turn: u32,
) -> Result<()>
pub async fn record_usage( &self, session_id: &str, usage: &UsageRecord, turn: u32, ) -> Result<()>
Record token usage for a turn.
Sourcepub async fn list_sessions(&self, limit: usize) -> Result<Vec<SessionMeta>>
pub async fn list_sessions(&self, limit: usize) -> Result<Vec<SessionMeta>>
List sessions, most recent first.
Sourcepub async fn get_session(&self, id: &str) -> Result<Option<SessionMeta>>
pub async fn get_session(&self, id: &str) -> Result<Option<SessionMeta>>
Get a specific session by ID.
Sourcepub async fn session_usage(&self, session_id: &str) -> Result<UsageSummary>
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.
Sourcepub async fn cost_overview(&self, since: Option<&str>) -> Result<CostOverview>
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.
Sourcepub async fn record_compaction(
&self,
session_id: &str,
trigger: &str,
pre_tokens: u64,
summary: &str,
messages_compacted: usize,
) -> Result<()>
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.
Sourcepub async fn save_message(
&self,
session_id: &str,
role: &str,
content: &str,
) -> Result<()>
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).
Sourcepub async fn get_messages(
&self,
session_id: &str,
) -> Result<Vec<SessionMessage>>
pub async fn get_messages( &self, session_id: &str, ) -> Result<Vec<SessionMessage>>
Get all messages for a session, ordered by ID.
Auto Trait Implementations§
impl Freeze for SessionManager
impl !RefUnwindSafe for SessionManager
impl Send for SessionManager
impl Sync for SessionManager
impl Unpin for SessionManager
impl UnsafeUnpin for SessionManager
impl !UnwindSafe for SessionManager
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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