CGJI01_v0.2 / core /memory_store.py
prashantmatlani's picture
updated code, to allow continuing a retrieved/saved conversation, in api_server and conversation_manager
c119621

# ./core/memory_store.py
import json
import os
from datetime import datetime
MEMORY_DIR = "data/memory"
if not os.path.exists(MEMORY_DIR):
#os.makedirs(MEMORY_DIR)
os.makedirs(MEMORY_DIR, exist_ok=True)
def generate_label(history):
"""
Simple label generator (can upgrade later with LLM)
"""
for msg in history:
if msg["role"] == "client":
return msg["content"][:40]
return "Conversation"
def save_state(session_id, state):
filepath = os.path.join(MEMORY_DIR, f"{session_id}.json")
data = {
"session_id": session_id,
"created_at": getattr(state, "created_at", datetime.now().isoformat()),
"last_updated": datetime.now().isoformat(),
"label": generate_label(state.history),
"history": state.history,
"agents": getattr(state, "agent_logs", {})
}
with open(filepath, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
print(f"📁 MEMORY DIR: {os.path.abspath(MEMORY_DIR)}\n💾 SAVED SESSION: {session_id} at {filepath}")
def load_state(session_id):
filepath = os.path.join(MEMORY_DIR, f"{session_id}.json")
if not os.path.exists(filepath):
return None
with open(filepath, "r", encoding="utf-8") as f:
return json.load(f)