Swap session backends with a unified protocol interface
The SessionStoreProtocol defines five methods that any session backend must implement. This lets you swap between JSON files, Redis, MongoDB, or your own custom store — without changing any agent or bot code.
For persistent backends (DB / JSON), clear_session() and delete_session() remove the underlying stored messages too, not just in-memory data. This ensures cleared history does not reappear after a reload or restart.
The protocol uses Python’s typing.Protocol with @runtime_checkable, so any class with matching method signatures automatically satisfies it — no inheritance needed.
In DefaultSessionStore, update_session_metadata() uses a cross-process file lock with locked reload-from-disk to ensure concurrent metadata updates and message appends are both preserved. This fixed a silent message-loss bug that affected multi-worker deployments before PR #1709.
If your custom store backs onto shared storage (Redis, Postgres, S3, another file system), make get_chat_history and get_session read from that backing store on every call rather than caching in process memory. DefaultSessionStore guarantees this; downstream code (e.g. BotSessionManager._load_history) relies on it.
JSON file-based persistence with atomic writes and file locking. Zero dependencies. All mutating methods reload from disk inside the file lock, so multiple processes can safely share one session directory.
from praisonaiagents.session import DefaultSessionStorestore = DefaultSessionStore( session_dir="/custom/path", max_messages=200,)
HierarchicalSessionStore
Extends DefaultSessionStore with session forking, snapshots, and revert.
from praisonaiagents.session import SessionStoreProtocolstore = InMemorySessionStore()# Runtime check — no registration neededassert isinstance(store, SessionStoreProtocol)# Use it anywhere a SessionStoreProtocol is expectedstore.add_message("test", "user", "Hello")history = store.get_chat_history("test")assert history == [{"role": "user", "content": "Hello"}]