mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-26 22:31:21 +00:00
- 全面更新README,添加项目特性、架构设计与技术栈介绍 - 详细补充项目结构说明及模块设计规范 - 增加快速开始步骤,包括环境配置、数据库初始化和服务启动 - 补充主要API模块路径和认证授权使用示例 - 添加开发指南、数据库迁移与测试方法 - 集成监控、日志级别说明及性能监控内容 - 完善Docker、传统部署及Nginx配置示例 - 添加贡献指南和代码规范说明 - 新增MCP模块概述及智能对话API接口文档 - 清理和移除module_ai中旧的mcp_server相关实现代码 - 在api/v1初始化文件中注册AI模块路由 - 修改resource模块,增强资源路径安全检查和文件类型检测逻辑 - 增强资源搜索和上传服务的健壮性及安全性
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from fastapi import APIRouter, Depends, WebSocket
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
|
|
from app.common.response import StreamResponse, SuccessResponse
|
|
from app.core.dependencies import AuthPermission
|
|
from app.core.router_class import OperationLogRoute
|
|
from app.core.logger import logger
|
|
from app.api.v1.module_system.auth.schema import AuthSchema
|
|
from .service import MCPService
|
|
from .schema import ChatQuerySchema
|
|
|
|
|
|
MCPRouter = APIRouter(route_class=OperationLogRoute, prefix="/mcp", tags=["MCP智能助手"])
|
|
|
|
|
|
@MCPRouter.post("/chat", summary="智能对话", description="与MCP智能助手进行对话")
|
|
async def chat_controller(
|
|
query: ChatQuerySchema,
|
|
auth: AuthSchema = Depends(AuthPermission(permissions=["ai:mcp:chat"]))
|
|
) -> StreamingResponse:
|
|
"""智能对话接口"""
|
|
logger.info(f"用户 {auth.user.name} 发起智能对话: {query.message[:50]}...")
|
|
|
|
async def generate_response():
|
|
async for chunk in MCPService.chat_query(query.message):
|
|
yield chunk
|
|
|
|
return StreamingResponse(generate_response(), media_type="text/plain")
|
|
|
|
|
|
@MCPRouter.websocket("/ws/chat", name="WebSocket聊天")
|
|
async def websocket_chat_controller(
|
|
websocket: WebSocket,
|
|
):
|
|
"""WebSocket聊天接口
|
|
|
|
ws://127.0.0.1:8001/api/v1/ai/mcp/ws/chat
|
|
"""
|
|
await websocket.accept()
|
|
try:
|
|
while True:
|
|
data = await websocket.receive_text()
|
|
# 流式发送响应
|
|
async for chunk in MCPService.chat_query(data):
|
|
await websocket.send_text(chunk)
|
|
except Exception as e:
|
|
logger.error(f"WebSocket聊天出错: {str(e)}")
|
|
await websocket.close()
|