|
1 | 1 | from typing import Any, List
|
2 | 2 |
|
| 3 | +from pydantic import BaseModel |
| 4 | +from typing_extensions import Self |
| 5 | + |
3 | 6 | from .._cancellation_token import CancellationToken
|
| 7 | +from .._component_config import Component |
4 | 8 | from ..model_context import ChatCompletionContext
|
5 | 9 | from ..models import SystemMessage
|
6 | 10 | from ._base_memory import Memory, MemoryContent, MemoryQueryResult, UpdateContextResult
|
7 | 11 |
|
8 | 12 |
|
9 |
| -class ListMemory(Memory): |
| 13 | +class ListMemoryConfig(BaseModel): |
| 14 | + """Configuration for ListMemory component.""" |
| 15 | + |
| 16 | + name: str | None = None |
| 17 | + """Optional identifier for this memory instance.""" |
| 18 | + memory_contents: List[MemoryContent] = [] |
| 19 | + """List of memory contents stored in this memory instance.""" |
| 20 | + |
| 21 | + |
| 22 | +class ListMemory(Memory, Component[ListMemoryConfig]): |
10 | 23 | """Simple chronological list-based memory implementation.
|
11 | 24 |
|
12 | 25 | This memory implementation stores contents in a list and retrieves them in
|
@@ -53,9 +66,13 @@ async def main() -> None:
|
53 | 66 |
|
54 | 67 | """
|
55 | 68 |
|
56 |
| - def __init__(self, name: str | None = None) -> None: |
| 69 | + component_type = "memory" |
| 70 | + component_provider_override = "autogen_core.memory.ListMemory" |
| 71 | + component_config_schema = ListMemoryConfig |
| 72 | + |
| 73 | + def __init__(self, name: str | None = None, memory_contents: List[MemoryContent] | None = None) -> None: |
57 | 74 | self._name = name or "default_list_memory"
|
58 |
| - self._contents: List[MemoryContent] = [] |
| 75 | + self._contents: List[MemoryContent] = memory_contents if memory_contents is not None else [] |
59 | 76 |
|
60 | 77 | @property
|
61 | 78 | def name(self) -> str:
|
@@ -146,3 +163,10 @@ async def clear(self) -> None:
|
146 | 163 | async def close(self) -> None:
|
147 | 164 | """Cleanup resources if needed."""
|
148 | 165 | pass
|
| 166 | + |
| 167 | + @classmethod |
| 168 | + def _from_config(cls, config: ListMemoryConfig) -> Self: |
| 169 | + return cls(name=config.name, memory_contents=config.memory_contents) |
| 170 | + |
| 171 | + def _to_config(self) -> ListMemoryConfig: |
| 172 | + return ListMemoryConfig(name=self.name, memory_contents=self._contents) |
0 commit comments