Memory¶
MemoryProvider ¶
Bases: ABC
Pluggable memory backend for AI context construction.
Implement this ABC to control how conversation history is retrieved
for AI generation. The library ships with :class:SlidingWindowMemory
(simple last-N events) as the default.
Lifecycle methods ingest, clear, and close are concrete
no-ops so that simple implementations only need to override retrieve.
retrieve
abstractmethod
async
¶
Retrieve context for AI generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
room_id
|
str
|
The room being processed. |
required |
current_event
|
RoomEvent
|
The event that triggered AI generation. |
required |
context
|
RoomContext
|
Full room context including recent events, bindings, and participants. |
required |
channel_id
|
str | None
|
The AI channel requesting memory (useful when multiple AI channels share a room). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
MemoryResult
|
class: |
MemoryResult
|
in the AI context. |
ingest
async
¶
Ingest an event into memory (optional).
Stateful providers (e.g. summarization, vector stores) can override this to update their internal state as events arrive. The default implementation is a no-op.
MemoryResult
dataclass
¶
Result returned by a memory provider.
Memory providers can return pre-built AI messages (e.g. summaries, synthetic context) and/or raw room events that AIChannel will convert using its own content extraction logic (preserving vision support).
A provider may populate one or both fields. messages are prepended
first, then events are converted and appended.
SlidingWindowMemory ¶
Bases: MemoryProvider
Return the most recent events from the room context.
This replicates the original AIChannel behavior of slicing
context.recent_events[-max_events:].
MockMemoryProvider ¶
Bases: MemoryProvider
Mock memory provider that records calls and returns configured results.
Example::
mock = MockMemoryProvider(
messages=[AIMessage(role="system", content="Summary of conversation")],
)
result = await mock.retrieve("room1", event, context)
assert len(mock.retrieve_calls) == 1