Files
chat-proxy/backend/api/v1/sessions.py
T
2025-09-03 07:19:53 +00:00

101 lines
3.1 KiB
Python

# api/v1/sessions.py
from typing import List
from fastapi import Body, Query, Path
from services import redis_service
from fastapi import APIRouter
from models.session import SessionMeta
router = APIRouter()
@router.post("/sessions", response_model=dict)
async def create_session_endpoint(
user_id: str = Query(..., description="User ID"),
first_message: str = Body("", embed=True)
):
"""
Create a new chat session for a user.
Session name is taken from `first_message` or defaults to "New Chat".
"""
meta = redis_service.create_session(user_id, first_message)
return meta
@router.get("/sessions", response_model=List[dict])
async def list_sessions_endpoint(
user_id: str = Query(..., description="User ID")
):
"""
Get a list of all saved sessions for a user (most recent first).
"""
return redis_service.get_sessions(user_id)
@router.get("/sessions/{session_id}", response_model=dict)
async def get_session_meta_endpoint(
user_id: str = Query(..., description="User ID"),
session_id: str = Path(..., description="Session ID")
):
"""
Get metadata for a single session.
"""
return redis_service.get_session_meta(user_id, session_id) or {}
@router.patch("/sessions/{session_id}", response_model=dict)
async def update_session_endpoint(
user_id: str = Query(..., description="User ID"),
session_id: str = Path(..., description="Session ID"),
session_name: str = Body(..., embed=True)
):
"""
Rename a session or update metadata fields.
"""
return redis_service.update_session_meta(user_id, session_id, session_name=session_name) or {}
@router.delete("/sessions/{session_id}", response_model=dict)
async def delete_session_endpoint(
user_id: str = Query(..., description="User ID"),
session_id: str = Path(..., description="Session ID")
):
"""
Delete a session and its chat history.
"""
redis_service.delete_session(user_id, session_id)
return {"status": "deleted"}
# -------------------------
# Gestione TTL sessioni
# -------------------------
@router.post("/sessions/{session_id}/extend_ttl", response_model=dict)
async def extend_ttl_endpoint(
user_id: str = Query(..., description="User ID"),
session_id: str = Path(..., description="Session ID"),
extra_seconds: int = Body(..., embed=True, description="Seconds to extend the TTL")
):
"""
Extend the TTL (time-to-live) for a given session and its related data.
"""
redis_service.extend_session_ttl(user_id, session_id, extra_seconds)
return {"status": "ttl_extended", "extra_seconds": extra_seconds}
@router.post("/sessions/{session_id}/refresh_ttl", response_model=dict)
async def refresh_ttl_endpoint(
user_id: str = Query(..., description="User ID"),
session_id: str = Path(..., description="Session ID")
):
"""
Reset the TTL for a given session and its related data back to the default value.
"""
redis_service.refresh_session_ttl(user_id, session_id)
return {
"status": "ttl_refreshed",
"default_ttl_seconds": redis_service.DEFAULT_TTL_SECONDS
}