Spaces:
Sleeping
Sleeping
File size: 2,597 Bytes
e803935 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | """
NeuroCheck Agentic AI β Session Memory
File-based session store: no Redis required for local deployment.
Each session is a JSON file in /tmp/neurocheck_sessions/.
"""
from __future__ import annotations
import json, uuid, os
from datetime import datetime
from pathlib import Path
from typing import List, Dict, Any
SESSION_DIR = Path("/tmp/neurocheck_sessions")
SESSION_DIR.mkdir(parents=True, exist_ok=True)
MAX_HISTORY = 40 # max messages kept per session
def new_session() -> str:
"""Create a new session and return its ID."""
sid = str(uuid.uuid4())
_write(sid, {
"session_id": sid,
"created_at": _now(),
"messages": [],
"classification": None,
"symptoms_collected": [],
})
return sid
def get_session(sid: str) -> Dict[str, Any] | None:
p = _path(sid)
if not p.exists():
return None
return json.loads(p.read_text())
def append_message(sid: str, role: str, content: str) -> None:
"""Append a human/assistant message to session history."""
s = get_session(sid)
if s is None:
return
s["messages"].append({"role": role, "content": content, "ts": _now()})
# Keep last MAX_HISTORY messages
if len(s["messages"]) > MAX_HISTORY:
s["messages"] = s["messages"][-MAX_HISTORY:]
_write(sid, s)
def set_classification(sid: str, classification: Dict[str, Any]) -> None:
s = get_session(sid)
if s is None:
return
s["classification"] = classification
_write(sid, s)
def get_history(sid: str) -> List[Dict[str, str]]:
s = get_session(sid)
if s is None:
return []
return s.get("messages", [])
def get_lc_messages(sid: str):
"""Return LangChain-compatible message list for context injection."""
from langchain_core.messages import HumanMessage, AIMessage
msgs = get_history(sid)
lc = []
for m in msgs:
if m["role"] == "human":
lc.append(HumanMessage(content=m["content"]))
else:
lc.append(AIMessage(content=m["content"]))
return lc
def list_sessions() -> List[str]:
return [p.stem for p in SESSION_DIR.glob("*.json")]
# ββ Internal helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _path(sid: str) -> Path:
return SESSION_DIR / f"{sid}.json"
def _write(sid: str, data: Dict) -> None:
_path(sid).write_text(json.dumps(data, indent=2))
def _now() -> str:
return datetime.utcnow().isoformat() + "Z"
|