""" @router /api/bot @description 机器人管理路由模块 @author wjh @created at 2025-11-6 """ from fastapi import APIRouter, Depends, HTTPException, Query, Path from typing import List from tortoise.exceptions import IntegrityError from models.bot_model import Bot from models.chat_session import ChatSession from models.user_model import User from schemas.bot_schema import BotItem, BotCreate, BotUpdate, BotListResponse, SessionBotsBindRequest from dependencies.auth import get_current_user # 复用认证依赖 router = APIRouter() # 机器人列表 (list bots) @router.get("", response_model=BotListResponse) async def list_bots( limit: int = Query(50, ge=1, le=200), offset: int = Query(0, ge=0), current_user: User = Depends(get_current_user) ) -> BotListResponse: # 暂不做角色权限,后续增加 admin 检查 total = await Bot.all().count() bots: List[Bot] = await Bot.all().order_by("-created_at").offset(offset).limit(limit) items = [ BotItem(id=b.id, name=b.name, description=b.description, model_name=b.model_name, created_at=b.created_at) for b in bots ] return BotListResponse(bots=items, total=total) # 创建机器人 (create bot)— 禁用用户创建 @router.post("", response_model=BotItem) async def create_bot( data: BotCreate, current_user: User = Depends(get_current_user) ) -> BotItem: raise HTTPException(status_code=403, detail="当前系统不支持创建机器人:已内置 AI 与人工律师") # 更新机器人 (update bot)— 禁用用户修改 @router.patch("/{bot_id}", response_model=BotItem) async def update_bot( bot_id: int = Path(..., ge=1), data: BotUpdate = None, current_user: User = Depends(get_current_user) ) -> BotItem: raise HTTPException(status_code=403, detail="当前系统不支持修改机器人:已内置 AI 与人工律师") # 删除机器人 (delete bot)— 禁用用户删除 @router.delete("/{bot_id}") async def delete_bot( bot_id: int = Path(..., ge=1), current_user: User = Depends(get_current_user) ): raise HTTPException(status_code=403, detail="当前系统不支持删除机器人:已内置 AI 与人工律师") # 获取会话绑定的机器人列表 (get session bots) @router.get("/session/{session_id}/bots", response_model=BotListResponse) async def get_session_bots( session_id: int = Path(..., ge=1), current_user: User = Depends(get_current_user) ) -> BotListResponse: s = await ChatSession.get_or_none(id=session_id, user_id=current_user.id) if not s: raise HTTPException(status_code=404, detail="会话不存在或无权访问") bs: List[Bot] = await s.bots.all() items = [ BotItem(id=b.id, name=b.name, description=b.description, model_name=b.model_name, created_at=b.created_at) for b in bs ] return BotListResponse(bots=items, total=len(items)) # 为会话绑定机器人(覆盖绑定) (bind bots to session: set) @router.post("/session/{session_id}/bots") async def bind_session_bots( session_id: int = Path(..., ge=1), data: SessionBotsBindRequest = None, 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.bots.clear() if data and data.bot_ids: bots = await Bot.filter(id__in=data.bot_ids) if bots: await s.bots.add(*bots) return {"success": True} # 解除绑定(移除部分机器人) (unbind specific bots) @router.delete("/session/{session_id}/bots") async def unbind_session_bots( session_id: int = Path(..., ge=1), data: SessionBotsBindRequest = None, 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="会话不存在或无权访问") if data and data.bot_ids: bots = await Bot.filter(id__in=data.bot_ids) if bots: await s.bots.remove(*bots) return {"success": True}