de02bdea06
- Refactor memory system (episodic/STM/LTM with components) - Implement complete subtitle domain (scanner, matcher, placer) - Add YAML workflow infrastructure - Externalize knowledge base (patterns, release groups) - Add comprehensive testing suite - Create manual testing CLIs"
91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
"""Memory — unified manager for the 3 memory tiers."""
|
|
|
|
import json
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
from .episodic import EpisodicMemory
|
|
from .ltm import LongTermMemory
|
|
from .stm import ShortTermMemory
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Memory:
|
|
"""
|
|
Unified manager for the 3 memory tiers.
|
|
|
|
Usage:
|
|
memory = Memory("data/memory")
|
|
memory.ltm.workspace.download = "/downloads"
|
|
memory.stm.add_message("user", "Hello")
|
|
memory.episodic.store_search_results("query", results)
|
|
memory.save()
|
|
"""
|
|
|
|
def __init__(self, storage_dir: str = "memory"):
|
|
self.storage_dir = Path(storage_dir)
|
|
self.storage_dir.mkdir(parents=True, exist_ok=True)
|
|
self.ltm_file = self.storage_dir / "ltm.json"
|
|
|
|
self.ltm = self._load_ltm()
|
|
self.stm = ShortTermMemory()
|
|
self.episodic = EpisodicMemory()
|
|
|
|
logger.info(f"Memory initialized (storage: {storage_dir})")
|
|
|
|
def _load_ltm(self) -> LongTermMemory:
|
|
"""Load LTM from disk, or return a fresh instance."""
|
|
if self.ltm_file.exists():
|
|
try:
|
|
data = json.loads(self.ltm_file.read_text(encoding="utf-8"))
|
|
logger.info("LTM loaded from file")
|
|
return LongTermMemory.from_dict(data)
|
|
except (OSError, json.JSONDecodeError) as e:
|
|
logger.warning(f"Could not load LTM: {e}")
|
|
return LongTermMemory()
|
|
|
|
def save(self) -> None:
|
|
"""Persist LTM to disk (STM and Episodic are volatile)."""
|
|
try:
|
|
self.ltm_file.write_text(
|
|
json.dumps(self.ltm.to_dict(), indent=2, ensure_ascii=False),
|
|
encoding="utf-8",
|
|
)
|
|
logger.debug("LTM saved")
|
|
except OSError as e:
|
|
logger.error(f"Failed to save LTM: {e}")
|
|
raise
|
|
|
|
def get_context_for_prompt(self) -> dict:
|
|
"""Snapshot of relevant memory for the system prompt."""
|
|
return {
|
|
"workspace": self.ltm.workspace.as_dict(),
|
|
"library_paths": self.ltm.library_paths.to_dict(),
|
|
"preferences": self.ltm.preferences.to_dict(),
|
|
"current_workflow": self.stm.workflow.to_dict(),
|
|
"current_topic": self.stm.entities.topic,
|
|
"extracted_entities": self.stm.entities.data,
|
|
"last_search": {
|
|
"query": self.episodic.search_results.last.get("query") if self.episodic.search_results.last else None,
|
|
"result_count": len(self.episodic.search_results.last.get("results", [])) if self.episodic.search_results.last else 0,
|
|
},
|
|
"active_downloads_count": len(self.episodic.downloads.active),
|
|
"pending_question": self.episodic.pending_question is not None,
|
|
"unread_events": len([e for e in self.episodic.events.items if not e.get("read")]),
|
|
}
|
|
|
|
def get_full_state(self) -> dict:
|
|
"""Full state dump for debug/API."""
|
|
return {
|
|
"ltm": self.ltm.to_dict(),
|
|
"stm": self.stm.to_dict(),
|
|
"episodic": self.episodic.to_dict(),
|
|
}
|
|
|
|
def clear_session(self) -> None:
|
|
"""Reset volatile memories (STM + Episodic)."""
|
|
self.stm.clear()
|
|
self.episodic.clear()
|
|
logger.info("Session memories cleared")
|