forked from i-am-bee/beeai-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_memory.py
96 lines (75 loc) · 3.06 KB
/
base_memory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright 2025 IBM Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from abc import ABC, abstractmethod
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any
from beeai_framework.backend import Message
if TYPE_CHECKING:
from beeai_framework.memory.readonly_memory import ReadOnlyMemory
class BaseMemory(ABC):
"""Abstract base class for all memory implementations."""
@property
@abstractmethod
def messages(self) -> list[Message]:
"""Return list of stored messages."""
pass
@abstractmethod
async def add(self, message: Message, index: int | None = None) -> None:
"""Add a message to memory."""
pass
@abstractmethod
async def delete(self, message: Message) -> bool:
"""Delete a message from memory."""
pass
@abstractmethod
def reset(self) -> None:
"""Clear all messages from memory."""
pass
async def add_many(self, messages: Iterable[Message], start: int | None = None) -> None:
"""Add multiple messages to memory."""
for counter, msg in enumerate(messages):
index = None if start is None else start + counter
await self.add(msg, index)
async def delete_many(self, messages: Iterable[Message]) -> None:
"""Delete multiple messages from memory."""
for msg in messages:
await self.delete(msg)
async def splice(self, start: int, delete_count: int, *items: Message) -> list[Message]:
"""Remove and insert messages at a specific position."""
total = len(self.messages)
start = max(total + start, 0) if start < 0 else start
delete_count = min(delete_count, total - start)
deleted_items = self.messages[start : start + delete_count]
await self.delete_many(deleted_items)
await self.add_many(items, start)
return deleted_items
def is_empty(self) -> bool:
"""Check if memory is empty."""
return len(self.messages) == 0
def __iter__(self) -> None:
return iter(self.messages)
@abstractmethod
def create_snapshot(self) -> Any:
"""Create a serializable snapshot of current state."""
pass
@abstractmethod
def load_snapshot(self, state: Any) -> None:
"""Restore state from a snapshot."""
pass
def as_read_only(self) -> "ReadOnlyMemory":
"""Return a read-only view of this memory."""
from beeai_framework.memory.readonly_memory import ( # Import here to avoid circular import
ReadOnlyMemory,
)
return ReadOnlyMemory(self)