Added memory editing functionality
This commit is contained in:
@@ -29,6 +29,7 @@ class WorkingMemory:
|
||||
|
||||
def _notify_change(self) -> None:
|
||||
"""Notify all handlers of working memory changes."""
|
||||
self._sort_entries()
|
||||
for handler in self._change_handlers:
|
||||
handler()
|
||||
|
||||
@@ -38,6 +39,10 @@ class WorkingMemory:
|
||||
self._changed_during_update.add(entry)
|
||||
else:
|
||||
self._notify_change()
|
||||
|
||||
def _sort_entries(self) -> None:
|
||||
"""Sort entries by ID in chronological order."""
|
||||
self._entries.sort(key=lambda x: x.id)
|
||||
|
||||
def add_entry(self, entry: Entry) -> None:
|
||||
"""
|
||||
@@ -50,6 +55,7 @@ class WorkingMemory:
|
||||
raise TypeError("Entry must be an instance of Entry class")
|
||||
entry.add_change_handler(self._handle_entry_change)
|
||||
self._entries.append(entry)
|
||||
self._sort_entries()
|
||||
self._notify_change()
|
||||
|
||||
def remove_entry(self, id: str) -> None:
|
||||
@@ -64,6 +70,7 @@ class WorkingMemory:
|
||||
if entry is not None:
|
||||
entry.cleanup()
|
||||
self._entries = [e for e in self._entries if e.id != id]
|
||||
self._sort_entries()
|
||||
self._notify_change()
|
||||
|
||||
def clear(self) -> None:
|
||||
@@ -101,7 +108,7 @@ class WorkingMemory:
|
||||
Get all entries in working memory.
|
||||
|
||||
Returns:
|
||||
List[Entry]: List of all entries
|
||||
List[Entry]: List of all entries in chronological order
|
||||
"""
|
||||
return self._entries.copy()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user