|
|
|
|
@@ -383,6 +383,11 @@ class SessionEntry:
|
|
|
|
|
# survives gateway restarts (the old in-memory _pre_flushed_sessions
|
|
|
|
|
# set was lost on restart, causing redundant re-flushes).
|
|
|
|
|
memory_flushed: bool = False
|
|
|
|
|
|
|
|
|
|
# Lazy session creation: tracks whether the session record has been
|
|
|
|
|
# written to SQLite. New sessions start False; the DB write is
|
|
|
|
|
# deferred until the first user message arrives.
|
|
|
|
|
_db_persisted: bool = True
|
|
|
|
|
|
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
|
|
|
result = {
|
|
|
|
|
@@ -763,11 +768,10 @@ class SessionStore:
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Session DB operation failed: %s", e)
|
|
|
|
|
|
|
|
|
|
if self._db and db_create_kwargs:
|
|
|
|
|
try:
|
|
|
|
|
self._db.create_session(**db_create_kwargs)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"[gateway] Warning: Failed to create SQLite session: {e}")
|
|
|
|
|
# Lazy session creation: defer DB write until first user message.
|
|
|
|
|
# Mark the entry as not yet persisted; ensure_db_session() will
|
|
|
|
|
# flush it when the gateway receives an actual message.
|
|
|
|
|
entry._db_persisted = False
|
|
|
|
|
|
|
|
|
|
# Seed new DM thread sessions with parent DM session history.
|
|
|
|
|
# When a bot reply creates a Slack thread and the user responds in it,
|
|
|
|
|
@@ -806,6 +810,26 @@ class SessionStore:
|
|
|
|
|
|
|
|
|
|
return entry
|
|
|
|
|
|
|
|
|
|
def ensure_db_session(self, entry: SessionEntry) -> None:
|
|
|
|
|
"""Lazily persist a session to SQLite on first user message.
|
|
|
|
|
|
|
|
|
|
Called by the gateway message handler when a real message arrives.
|
|
|
|
|
If the session is already persisted, this is a no-op.
|
|
|
|
|
"""
|
|
|
|
|
if entry._db_persisted or not self._db:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
source_val = entry.platform.value if entry.platform else "unknown"
|
|
|
|
|
user_id = entry.origin.user_id if entry.origin else None
|
|
|
|
|
self._db.create_session(
|
|
|
|
|
session_id=entry.session_id,
|
|
|
|
|
source=source_val,
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
)
|
|
|
|
|
entry._db_persisted = True
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning("Failed to lazily create SQLite session: %s", e)
|
|
|
|
|
|
|
|
|
|
def update_session(
|
|
|
|
|
self,
|
|
|
|
|
session_key: str,
|
|
|
|
|
@@ -865,11 +889,8 @@ class SessionStore:
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Session DB operation failed: %s", e)
|
|
|
|
|
|
|
|
|
|
if self._db and db_create_kwargs:
|
|
|
|
|
try:
|
|
|
|
|
self._db.create_session(**db_create_kwargs)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.debug("Session DB operation failed: %s", e)
|
|
|
|
|
# Lazy: defer DB create until first message
|
|
|
|
|
new_entry._db_persisted = False
|
|
|
|
|
|
|
|
|
|
return new_entry
|
|
|
|
|
|
|
|
|
|
|