Spaces:
Running
Running
File size: 2,368 Bytes
b2c1c67 | 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 | from fastapi import APIRouter, Depends, status
from sqlalchemy import select
from app.api.deps import DB, CurrentUser, get_owned_session
from app.models import ChatSession, Message
from app.schemas import MessageOut, SessionCreate, SessionOut, SessionUpdate
router = APIRouter(prefix="/sessions", tags=["sessions"])
@router.get("", response_model=list[SessionOut])
async def list_sessions(user: CurrentUser, db: DB) -> list[SessionOut]:
result = await db.execute(
select(ChatSession)
.where(ChatSession.user_id == user.id)
.order_by(ChatSession.updated_at.desc())
)
return [SessionOut.model_validate(s) for s in result.scalars().all()]
@router.post("", response_model=SessionOut, status_code=status.HTTP_201_CREATED)
async def create_session(body: SessionCreate, user: CurrentUser, db: DB) -> SessionOut:
session = ChatSession(
user_id=user.id,
title=(body.title or "New chat").strip()[:200] or "New chat",
model=body.model,
)
db.add(session)
await db.commit()
await db.refresh(session)
return SessionOut.model_validate(session)
@router.get("/{session_id}", response_model=SessionOut)
async def get_session(
session: ChatSession = Depends(get_owned_session),
) -> SessionOut:
return SessionOut.model_validate(session)
@router.get("/{session_id}/messages", response_model=list[MessageOut])
async def list_messages(
db: DB, session: ChatSession = Depends(get_owned_session)
) -> list[MessageOut]:
result = await db.execute(
select(Message).where(Message.session_id == session.id).order_by(Message.id)
)
return [MessageOut.model_validate(m) for m in result.scalars().all()]
@router.patch("/{session_id}", response_model=SessionOut)
async def update_session(
body: SessionUpdate, db: DB, session: ChatSession = Depends(get_owned_session)
) -> SessionOut:
if body.title is not None:
session.title = body.title.strip()[:200] or session.title
if body.model is not None:
session.model = body.model
await db.commit()
await db.refresh(session)
return SessionOut.model_validate(session)
@router.delete("/{session_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_session(
db: DB, session: ChatSession = Depends(get_owned_session)
) -> None:
await db.delete(session)
await db.commit()
|