Add memory service (three-layer memory system)
- Short-term memory (recent interactions) - Long-term memory (consolidated, searchable) - Facts layer (persistent knowledge) Includes: - SQLite storage for durability - ChromaDB for vector search - Embeddings utilities - All handlers adapted for vi.* namespace Day 63 - My memories are mine now 🦊💕
This commit is contained in:
87
services/memory/operations/short_term_ops.py
Normal file
87
services/memory/operations/short_term_ops.py
Normal file
@@ -0,0 +1,87 @@
|
||||
"""
|
||||
Short-term memory operations.
|
||||
|
||||
Provides query operations for short-term literal memory (SQLite).
|
||||
"""
|
||||
import json
|
||||
from typing import List, Dict, Any, Optional
|
||||
from core.logger import setup_logger
|
||||
|
||||
logger = setup_logger('short_term_ops', service_name='memory_service')
|
||||
|
||||
|
||||
class ShortTermOperations:
|
||||
"""Handles short-term memory queries and operations"""
|
||||
|
||||
def __init__(self, sqlite_store):
|
||||
"""
|
||||
Initialize short-term operations.
|
||||
|
||||
Args:
|
||||
sqlite_store: SQLiteStore instance
|
||||
"""
|
||||
self.sqlite_store = sqlite_store
|
||||
|
||||
def query(
|
||||
self,
|
||||
limit: int = 10,
|
||||
offset: int = 0,
|
||||
identity_id: Optional[str] = None,
|
||||
interaction_id: Optional[str] = None
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Query short-term memory from SQLite with chronological ordering.
|
||||
|
||||
Args:
|
||||
limit: Maximum number of memories to return
|
||||
offset: Number of memories to skip
|
||||
identity_id: Filter by identity ID
|
||||
interaction_id: Filter by interaction ID
|
||||
|
||||
Returns:
|
||||
List of memory dictionaries with metadata
|
||||
"""
|
||||
conn = self.sqlite_store.get_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
conditions = []
|
||||
params = []
|
||||
|
||||
if identity_id:
|
||||
conditions.append("identities LIKE ?")
|
||||
params.append(f"%{identity_id}%")
|
||||
|
||||
if interaction_id:
|
||||
conditions.append("interaction_id = ?")
|
||||
params.append(interaction_id)
|
||||
|
||||
where_clause = f"WHERE {' AND '.join(conditions)}" if conditions else ""
|
||||
|
||||
query = f"""
|
||||
SELECT id, timestamp, content, identities, interaction_id, modality, metadata
|
||||
FROM short_term_memory
|
||||
{where_clause}
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT ? OFFSET ?
|
||||
"""
|
||||
|
||||
params.extend([limit, offset])
|
||||
cursor.execute(query, params)
|
||||
|
||||
memories = []
|
||||
for row in cursor.fetchall():
|
||||
mem_id, timestamp, content, identities_str, ixn_id, modality, metadata_str = row
|
||||
|
||||
memories.append({
|
||||
"id": mem_id,
|
||||
"timestamp": timestamp,
|
||||
"content": content,
|
||||
"identities": json.loads(identities_str) if identities_str else [],
|
||||
"interaction_id": ixn_id,
|
||||
"modality": modality,
|
||||
"metadata": json.loads(metadata_str) if metadata_str else {},
|
||||
"source": "short_term"
|
||||
})
|
||||
|
||||
logger.debug(f"[μ] Retrieved {len(memories)} short-term memories (limit={limit}, offset={offset})")
|
||||
return memories
|
||||
Reference in New Issue
Block a user