Spaces:
Runtime error
Runtime error
Download api/session.py from little666trader/backend-bxt: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/little666trader/backend-bxt/resolve/main/api/session.py
- Command line
-
hf download hf://spaces/little666trader/backend-bxt/api/session.py
-
curl -L -o session.py https://huggingface.co/spaces/little666trader/backend-bxt/resolve/main/api/session.py
2.77 kB
| """ | |
| @router /api/session | |
| @description 会话管理路由模块 | |
| @author wjh | |
| @created at 2025-11-6 | |
| """ | |
| from fastapi import APIRouter, Depends, HTTPException, Query, Path | |
| from typing import List | |
| from models.chat_session import ChatSession | |
| from models.user_model import User | |
| from schemas.session_schema import SessionCreate,SessionUpdate,SessionItem,SessionItemEnvelope,SessionListEnvelope | |
| from dependencies.auth import get_current_user | |
| router = APIRouter() | |
| # 获取会话列表路由 (get session list router) | |
| async def list_sessions( | |
| limit: int = Query(50, ge=1, le=200), | |
| offset: int = Query(0, ge=0), | |
| current_user: User = Depends(get_current_user) | |
| ) -> SessionListEnvelope: | |
| total = await ChatSession.filter(user_id=current_user.id).count() | |
| sessions: List[ChatSession] = await ChatSession.filter(user_id=current_user.id)\ | |
| .order_by("-updated_at")\ | |
| .offset(offset).limit(limit) | |
| items = [ | |
| SessionItem( | |
| id=s.id, | |
| title=s.title, | |
| created_at=s.created_at, | |
| updated_at=s.updated_at | |
| ) for s in sessions | |
| ] | |
| return {"success": True, "data": {"sessions": items, "total": total}} | |
| # 创建会话路由 (create session router) | |
| async def create_session( | |
| data: SessionCreate, | |
| current_user: User = Depends(get_current_user) | |
| ) -> SessionItemEnvelope: | |
| s = await ChatSession.create(user=current_user, title=data.title) | |
| return {"success": True, "data": SessionItem(id=s.id, title=s.title, created_at=s.created_at, updated_at=s.updated_at)} | |
| # 更新会话路由 (update session router) | |
| async def update_session( | |
| data: SessionUpdate, | |
| session_id: int = Path(..., ge=1), | |
| current_user: User = Depends(get_current_user) | |
| ) -> SessionItemEnvelope: | |
| s = await ChatSession.get_or_none(id=session_id, user_id=current_user.id) | |
| if not s: | |
| raise HTTPException(status_code=404, detail="会话不存在或无权访问") | |
| if data and data.title is not None: | |
| s.title = data.title | |
| await s.save() | |
| return {"success": True, "data": SessionItem(id=s.id, title=s.title, created_at=s.created_at, updated_at=s.updated_at)} | |
| # 删除会话路由 (delete session router) | |
| async def delete_session( | |
| session_id: int = Path(..., ge=1), | |
| current_user: User = Depends(get_current_user) | |
| ): | |
| s = await ChatSession.get_or_none(id=session_id, user_id=current_user.id) | |
| if not s: | |
| raise HTTPException(status_code=404, detail="会话不存在或无权访问") | |
| await s.delete() | |
| return {"success": True} |