New archi: domain driven development
Working but need to check out code
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
"""Memory storage - Migrated from agent/memory.py"""
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict
|
||||
import json
|
||||
|
||||
from agent.config import settings
|
||||
from agent.parameters import validate_parameter, get_parameter_schema
|
||||
|
||||
|
||||
class Memory:
|
||||
"""
|
||||
Generic memory storage for agent state.
|
||||
|
||||
Provides a simple key-value store that persists to JSON.
|
||||
"""
|
||||
|
||||
def __init__(self, path: str = "memory.json"):
|
||||
self.file = Path(path)
|
||||
self.data: Dict[str, Any] = {}
|
||||
self.load()
|
||||
|
||||
def load(self) -> None:
|
||||
"""Load memory from file or initialize with defaults."""
|
||||
if self.file.exists():
|
||||
try:
|
||||
self.data = json.loads(self.file.read_text(encoding="utf-8"))
|
||||
except (json.JSONDecodeError, IOError) as e:
|
||||
print(f"Warning: Could not load memory file: {e}")
|
||||
self.data = {
|
||||
"config": {},
|
||||
"tv_shows": [],
|
||||
"history": [],
|
||||
}
|
||||
else:
|
||||
self.data = {
|
||||
"config": {},
|
||||
"tv_shows": [],
|
||||
"history": [],
|
||||
}
|
||||
|
||||
def save(self) -> None:
|
||||
self.file.write_text(
|
||||
json.dumps(self.data, indent=2, ensure_ascii=False),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
def get(self, key: str, default: Any = None) -> Any:
|
||||
"""Get a value from memory by key."""
|
||||
return self.data.get(key, default)
|
||||
|
||||
def set(self, key: str, value: Any) -> None:
|
||||
"""
|
||||
Set a value in memory and save.
|
||||
|
||||
Validates the value against the parameter schema if one exists.
|
||||
"""
|
||||
# Validate if schema exists
|
||||
is_valid, error_msg = validate_parameter(key, value)
|
||||
if not is_valid:
|
||||
print(f'Validation failed for {key}: {error_msg}')
|
||||
raise ValueError(f"Invalid value for {key}: {error_msg}")
|
||||
|
||||
print(f'Setting {key} in memory to: {value}')
|
||||
self.data[key] = value
|
||||
self.save()
|
||||
|
||||
def has(self, key: str) -> bool:
|
||||
"""Check if a key exists and has a non-None value."""
|
||||
return key in self.data and self.data[key] is not None
|
||||
|
||||
def append_history(self, role: str, content: str) -> None:
|
||||
"""
|
||||
Append a message to conversation history.
|
||||
|
||||
Args:
|
||||
role: Message role ('user' or 'assistant')
|
||||
content: Message content
|
||||
"""
|
||||
if "history" not in self.data:
|
||||
self.data["history"] = []
|
||||
|
||||
self.data["history"].append({
|
||||
"role": role,
|
||||
"content": content
|
||||
})
|
||||
self.save()
|
||||
Reference in New Issue
Block a user