Compare commits

...

1 Commits

Author SHA1 Message Date
Teknium
8e5f2ba7df fix: persist API server sessions to shared SessionDB (state.db)
The API server adapter created AIAgent instances without passing
session_db, so conversations via Open WebUI and other OpenAI-compatible
frontends were never persisted to state.db. This meant 'hermes sessions
list' showed no API server sessions — they were effectively stateless.

Changes:
- Add _ensure_session_db() helper for lazy SessionDB initialization
- Pass session_db=self._ensure_session_db() in _create_agent()
- Refactor existing X-Hermes-Session-Id handler to use the shared helper

Sessions now persist with source='api_server' and are visible alongside
CLI and gateway sessions in hermes sessions list/search.
2026-04-03 10:29:13 -07:00

View File

@@ -372,6 +372,24 @@ class APIServerAdapter(BasePlatformAdapter):
status=401, status=401,
) )
# ------------------------------------------------------------------
# Session DB helper
# ------------------------------------------------------------------
def _ensure_session_db(self):
"""Lazily initialise and return the shared SessionDB instance.
Sessions are persisted to ``state.db`` so that ``hermes sessions list``
shows API-server conversations alongside CLI and gateway ones.
"""
if self._session_db is None:
try:
from hermes_state import SessionDB
self._session_db = SessionDB()
except Exception as e:
logger.debug("SessionDB unavailable for API server: %s", e)
return self._session_db
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# Agent creation helper # Agent creation helper
# ------------------------------------------------------------------ # ------------------------------------------------------------------
@@ -415,6 +433,7 @@ class APIServerAdapter(BasePlatformAdapter):
platform="api_server", platform="api_server",
stream_delta_callback=stream_delta_callback, stream_delta_callback=stream_delta_callback,
tool_progress_callback=tool_progress_callback, tool_progress_callback=tool_progress_callback,
session_db=self._ensure_session_db(),
) )
return agent return agent
@@ -503,10 +522,9 @@ class APIServerAdapter(BasePlatformAdapter):
if provided_session_id: if provided_session_id:
session_id = provided_session_id session_id = provided_session_id
try: try:
if self._session_db is None: db = self._ensure_session_db()
from hermes_state import SessionDB if db is not None:
self._session_db = SessionDB() history = db.get_messages_as_conversation(session_id)
history = self._session_db.get_messages_as_conversation(session_id)
except Exception as e: except Exception as e:
logger.warning("Failed to load session history for %s: %s", session_id, e) logger.warning("Failed to load session history for %s: %s", session_id, e)
history = [] history = []