Skip to main content

greentic_session/
store.rs

1use crate::ReplyScope;
2use crate::error::SessionResult;
3use greentic_types::{SessionData, SessionKey, TenantCtx, UserId};
4use std::time::Duration;
5
6/// Persistent session storage interface used by Greentic runtimes.
7///
8/// `SessionData` captures the tenant context, flow identifier, cursor, and serialized execution
9/// state snapshot for an in-flight flow. Implementations store that payload so runners can pause
10/// execution, persist the snapshot, and resume the flow consistently after new input arrives.
11pub trait SessionStore: Send + Sync + 'static {
12    /// Creates a new session associated with the supplied tenant context and returns its key.
13    fn create_session(&self, ctx: &TenantCtx, data: SessionData) -> SessionResult<SessionKey>;
14
15    /// Fetches the session payload for the provided key, if it exists.
16    fn get_session(&self, key: &SessionKey) -> SessionResult<Option<SessionData>>;
17
18    /// Replaces the session payload for the provided key.
19    fn update_session(&self, key: &SessionKey, data: SessionData) -> SessionResult<()>;
20
21    /// Removes the session entry and clears any lookup indices.
22    fn remove_session(&self, key: &SessionKey) -> SessionResult<()>;
23
24    /// Registers a paused flow wait, persisting the session and routing indices.
25    fn register_wait(
26        &self,
27        ctx: &TenantCtx,
28        user_id: &UserId,
29        scope: &ReplyScope,
30        session_key: &SessionKey,
31        data: SessionData,
32        ttl: Option<Duration>,
33    ) -> SessionResult<()>;
34
35    /// Finds a wait registered for the provided scope, if one exists.
36    fn find_wait_by_scope(
37        &self,
38        ctx: &TenantCtx,
39        user_id: &UserId,
40        scope: &ReplyScope,
41    ) -> SessionResult<Option<SessionKey>>;
42
43    /// Lists all waits registered for the provided user.
44    fn list_waits_for_user(
45        &self,
46        ctx: &TenantCtx,
47        user_id: &UserId,
48    ) -> SessionResult<Vec<SessionKey>>;
49
50    /// Clears a wait registration for the provided scope.
51    fn clear_wait(
52        &self,
53        ctx: &TenantCtx,
54        user_id: &UserId,
55        scope: &ReplyScope,
56    ) -> SessionResult<()>;
57
58    /// Finds the active session bound to the specified tenant + user combination.
59    #[deprecated(note = "use find_wait_by_scope or list_waits_for_user instead")]
60    fn find_by_user(
61        &self,
62        ctx: &TenantCtx,
63        user: &UserId,
64    ) -> SessionResult<Option<(SessionKey, SessionData)>>;
65}