Skip to content

aimu.sessions

Multi-user session storage: per-conversation state keyed by channel:sender, so one process serves many users/chats. See how-to: use sessions.

Sync, matching the MemoryStore / ConversationManager family; the async assistant loop calls it directly.

aimu.sessions.Session dataclass

Session(key: str, messages: list[dict] = list(), memory_namespace: Optional[str] = None, metadata: dict[str, Any] = dict())

One conversation's persisted state.

Attributes:

Name Type Description
key str

The session key (see :func:session_key).

messages list[dict]

Conversation history as list[dict] in OpenAI format (plain data).

memory_namespace Optional[str]

Optional scope a caller can pass to a MemoryStore to isolate this session's memories (e.g. a collection name / prefix). The store ABC is unchanged.

metadata dict[str, Any]

Free-form, opaque to the library (display name, locale, ...).

aimu.sessions.SessionStore

Bases: ABC

A store of :class:Session state keyed by session_key(channel, sender).

get returns a detached snapshot (mutating it does nothing until save); save persists.

get abstractmethod

get(key: str) -> Session

Return the session for key, or a fresh empty :class:Session if none is stored.

save abstractmethod

save(session: Session) -> None

Persist session (create or replace by session.key).

list_keys abstractmethod

list_keys() -> list[str]

Return the keys of all saved sessions.

delete abstractmethod

delete(key: str) -> None

Delete the session for key. A no-op if no such session exists.

close

close() -> None

Release resources. Default no-op.

aimu.sessions.InMemorySessionStore

InMemorySessionStore()

Bases: SessionStore

A non-durable :class:SessionStore backed by a dict. Sessions are lost on process exit.

get and save copy, so the stored state is detached from the returned/passed object (same contract as the persisted stores).

aimu.sessions.TinyDBSessionStore

TinyDBSessionStore(db_path: str = 'sessions.json')

Bases: SessionStore

A durable :class:SessionStore persisting one document per session to a TinyDB file.

aimu.sessions.SessionLocks

SessionLocks()

Lazily-created per-key asyncio.Lock.

Serializes a single session's turns (shared message state, ordering) while letting different sessions run concurrently::

locks = SessionLocks()
async with locks(key):
    ...  # this session's turn

Created locks are retained, so the same key always returns the same lock. Bound to the running event loop via asyncio.Lock; use one SessionLocks per assistant process.

aimu.sessions.session_key

session_key(channel: Optional[str], sender: Optional[str]) -> str

Canonical session key from a message's channel + sender.

Single-user transports (no channel/sender) collapse to "default:default", so single-user usage needs no ceremony.