49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from services.memory import (
|
|
get_all_sessions,
|
|
get_or_create_session,
|
|
delete_session,
|
|
update_session_title,
|
|
update_session_persona,
|
|
get_history,
|
|
get_message_count
|
|
)
|
|
|
|
router = APIRouter(prefix="/sessions", tags=["sessions"])
|
|
|
|
|
|
@router.get("/")
|
|
async def list_sessions():
|
|
sessions = await get_all_sessions()
|
|
result = []
|
|
for s in sessions:
|
|
count = await get_message_count(s["session_id"])
|
|
result.append({**s, "message_count": count})
|
|
return result
|
|
|
|
|
|
@router.get("/{session_id}")
|
|
async def get_session(session_id: str):
|
|
sessions = await get_all_sessions()
|
|
s = next((x for x in sessions if x["session_id"] == session_id), None)
|
|
if not s:
|
|
raise HTTPException(status_code=404, detail="Сессия не найдена")
|
|
return s
|
|
|
|
|
|
@router.patch("/{session_id}")
|
|
async def patch_session(session_id: str, data: dict):
|
|
# ensure session exists before patching
|
|
await get_or_create_session(session_id, data.get("persona_id", "default"))
|
|
if "title" in data:
|
|
await update_session_title(session_id, data["title"])
|
|
if "persona_id" in data:
|
|
await update_session_persona(session_id, data["persona_id"])
|
|
return {"status": "updated"}
|
|
|
|
|
|
@router.delete("/{session_id}")
|
|
async def remove_session(session_id: str):
|
|
await delete_session(session_id)
|
|
return {"status": "deleted", "session_id": session_id}
|