Fix recupero elenco sessioni e reload

This commit is contained in:
Samuele E. Locatelli
2025-09-03 14:35:33 +00:00
parent 30d21f0180
commit bf10b64627
2 changed files with 21 additions and 5 deletions
+4 -1
View File
@@ -28,7 +28,10 @@ async def list_sessions_endpoint(
"""
Get a list of all saved sessions for a user (most recent first).
"""
return redis_service.get_sessions(user_id)
#print(f"[DEBUG] user_id={user_id!r}")
sessions = redis_service.get_sessions(user_id)
print(f"[DEBUG] sessions={sessions}")
return sessions
@router.get("/sessions/{session_id}", response_model=dict)
+17 -4
View File
@@ -72,18 +72,30 @@ def create_session(user_id: str, first_message: str) -> dict:
index_key = f"chatSessionsIndex:{user_id}"
r.set(meta_key, json.dumps(meta), ex=DEFAULT_TTL_SECONDS)
r.zadd(index_key, {session_id: datetime.utcnow().timestamp()})
# Score come timestamp float, ma non sarà usato per filtrare
score = float(datetime.utcnow().timestamp())
r.zadd(index_key, {session_id: score})
r.expire(index_key, DEFAULT_TTL_SECONDS)
return meta
def get_sessions(user_id: str) -> List[dict]:
"""Return all sessions for a user, newest first."""
session_ids = r.zrevrangebyscore(f"chatSessionsIndex:{user_id}", "+inf", "-inf")
"""Return all sessions for a user, newest first, ignoring score filtering."""
key = f"chatSessionsIndex:{user_id}"
# Recupera tutti i membri in ordine di score decrescente
session_ids = r.zrevrange(key, 0, -1)
print(f"[DEBUG] key={key}")
print(f"[DEBUG] session_ids={session_ids}")
sessions = []
for sid in session_ids:
raw = r.get(f"chatSession:{user_id}:{sid}")
session_key = f"chatSession:{user_id}:{sid}"
raw = r.get(session_key)
print(f"[DEBUG] GET {session_key} -> {raw}")
if raw:
sessions.append(json.loads(raw))
return sessions
@@ -131,6 +143,7 @@ def extend_session_ttl(user_id: str, session_id: str, extra_seconds: int):
else:
r.expire(key, extra_seconds)
def refresh_session_ttl(user_id: str, session_id: str):
"""Reset TTL for session metadata, history, and index to default."""
keys = [