mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 12:52:26 +00:00
fix(mcp): 优化智能对话流式响应并增强异常处理
- 确保智能对话流式响应返回字节串,防止类型错误 - 在流式响应异常时返回友好错误信息,避免连接中断 - WebSocket聊天控制器中添加异常处理,保证异常信息反馈客户端 - WebSocket连接异常后使用finally确保连接正确关闭 refactor(resource): 统一资源接口返回HTTP URL路径 - 资源控制器新增Request参数,传递base_url实现URL转换 - 资源服务中所有路径替换为返回基于base_url的HTTP URL路径 - 文件信息、目录列表、文件上传下载接口均返回HTTP URL,提升前端友好度 - 移除递归参数及相关逻辑,简化目录统计实现 fix(database): 简化数据库依赖生成器,避免不必要的事务开启 - dependencies中db_getter取消多余事务管理,仅yield数据库会话 style(router_class): 优化操作日志路由异常处理,去除多余事务 - 简化操作日志写入流程,移除嵌套事务开启,提升代码可读性 refactor(initialize): 优化数据库初始化逻辑并增强日志与事务管理 - 初始化数据插入前打印日志,插入后标记是否需要提交事务 - PostgreSQL序列更新拆分成单独方法,针对有id字段的表执行 - 增加完整的异常捕获和回滚,保证初始化失败时事务回滚 - 读取初始化数据文件时增加日志及异常处理 fix(ai_client): 修正AI客户端HTTP连接管理及异常处理 - 使用自定义httpx AsyncClient替代默认客户端,确保连接配置 - 增加关闭客户端连接方法,避免资源泄露 - 流式响应时检查选择器内容有效性,提升稳定性 chore(cleanup): 移除MySQL快照SQL文件,保持仓库整洁 - 删除无用的SQL数据转储文件,减小仓库体积并维护清洁度
This commit is contained in:
@@ -24,10 +24,16 @@ async def chat_controller(
|
||||
logger.info(f"用户 {auth.user.name} 发起智能对话: {query.message[:50]}...")
|
||||
|
||||
async def generate_response():
|
||||
async for chunk in MCPService.chat_query(query.message):
|
||||
yield chunk
|
||||
try:
|
||||
async for chunk in MCPService.chat_query(query.message):
|
||||
# 确保返回的是字节串
|
||||
if chunk:
|
||||
yield chunk.encode('utf-8') if isinstance(chunk, str) else chunk
|
||||
except Exception as e:
|
||||
logger.error(f"流式响应出错: {str(e)}")
|
||||
yield f"抱歉,处理您的请求时出现了错误: {str(e)}".encode('utf-8')
|
||||
|
||||
return StreamingResponse(generate_response(), media_type="text/plain")
|
||||
return StreamingResponse(generate_response(), media_type="text/plain; charset=utf-8")
|
||||
|
||||
|
||||
@MCPRouter.websocket("/ws/chat", name="WebSocket聊天")
|
||||
@@ -43,8 +49,14 @@ async def websocket_chat_controller(
|
||||
while True:
|
||||
data = await websocket.receive_text()
|
||||
# 流式发送响应
|
||||
async for chunk in MCPService.chat_query(data):
|
||||
await websocket.send_text(chunk)
|
||||
try:
|
||||
async for chunk in MCPService.chat_query(data):
|
||||
if chunk:
|
||||
await websocket.send_text(chunk)
|
||||
except Exception as e:
|
||||
logger.error(f"处理聊天查询出错: {str(e)}")
|
||||
await websocket.send_text(f"抱歉,处理您的请求时出现了错误: {str(e)}")
|
||||
except Exception as e:
|
||||
logger.error(f"WebSocket聊天出错: {str(e)}")
|
||||
await websocket.close()
|
||||
finally:
|
||||
await websocket.close()
|
||||
@@ -6,4 +6,11 @@ from typing import Optional
|
||||
|
||||
class ChatQuerySchema(BaseModel):
|
||||
"""聊天查询模型"""
|
||||
message: str = Field(..., min_length=1, max_length=4000, description="聊天消息")
|
||||
message: str = Field(..., min_length=1, max_length=4000, description="聊天消息", example="你好,你能帮我什么?")
|
||||
|
||||
class Config:
|
||||
json_schema_extra = {
|
||||
"example": {
|
||||
"message": "你好,你能帮我什么?"
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,10 @@ class MCPService:
|
||||
"""处理聊天查询"""
|
||||
# 创建MCP客户端实例
|
||||
mcp_client = AIClient()
|
||||
# 处理消息
|
||||
async for response in mcp_client.process(message):
|
||||
yield response
|
||||
try:
|
||||
# 处理消息
|
||||
async for response in mcp_client.process(message):
|
||||
yield response
|
||||
finally:
|
||||
# 确保关闭客户端连接
|
||||
await mcp_client.close()
|
||||
@@ -26,19 +26,19 @@ from .service import ResourceService
|
||||
ResourceRouter = APIRouter(route_class=OperationLogRoute, prefix="/resource", tags=["资源管理"])
|
||||
|
||||
|
||||
@ResourceRouter.get("/list", summary="获取目录列表", description="获取指定目录的文件列表")
|
||||
@ResourceRouter.get("/list", summary="获取目录列表", description="获取指定目录下的文件和子目录列表")
|
||||
async def get_directory_list_controller(
|
||||
request: Request,
|
||||
path: Optional[str] = Query(None, description="目录路径"),
|
||||
recursive: bool = Query(False, description="递归获取"),
|
||||
include_hidden: bool = Query(False, description="包含隐藏文件"),
|
||||
include_hidden: bool = Query(False, description="是否包含隐藏文件"),
|
||||
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:query"]))
|
||||
) -> JSONResponse:
|
||||
"""获取目录列表"""
|
||||
result_dict = await ResourceService.get_directory_list_service(
|
||||
auth=auth,
|
||||
path=path,
|
||||
recursive=recursive,
|
||||
include_hidden=include_hidden
|
||||
include_hidden=include_hidden,
|
||||
base_url=str(request.base_url)
|
||||
)
|
||||
logger.info(f"获取目录列表成功: {path or 'default'}")
|
||||
return SuccessResponse(data=result_dict, msg="获取目录列表成功")
|
||||
@@ -46,11 +46,16 @@ async def get_directory_list_controller(
|
||||
|
||||
@ResourceRouter.post("/search", summary="搜索资源", description="根据条件搜索资源")
|
||||
async def search_resources_controller(
|
||||
request: Request,
|
||||
search: ResourceSearchSchema,
|
||||
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:search"]))
|
||||
) -> JSONResponse:
|
||||
"""搜索资源"""
|
||||
result_list = await ResourceService.search_resources_service(auth=auth, search=search)
|
||||
result_list = await ResourceService.search_resources_service(
|
||||
auth=auth,
|
||||
search=search,
|
||||
base_url=str(request.base_url)
|
||||
)
|
||||
logger.info(f"搜索资源成功,找到 {len(result_list)} 个结果")
|
||||
return SuccessResponse(data=result_list, msg=f"搜索成功,找到 {len(result_list)} 个结果")
|
||||
|
||||
@@ -58,6 +63,7 @@ async def search_resources_controller(
|
||||
@ResourceRouter.post("/upload", summary="上传文件", description="上传文件到指定目录")
|
||||
async def upload_file_controller(
|
||||
file: UploadFile,
|
||||
request: Request,
|
||||
target_path: Optional[str] = Form(None, description="目标目录路径"),
|
||||
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:upload"]))
|
||||
) -> JSONResponse:
|
||||
@@ -65,7 +71,8 @@ async def upload_file_controller(
|
||||
result_dict = await ResourceService.upload_file_service(
|
||||
auth=auth,
|
||||
file=file,
|
||||
target_path=target_path
|
||||
target_path=target_path,
|
||||
base_url=str(request.base_url)
|
||||
)
|
||||
logger.info(f"上传文件成功: {result_dict['filename']}")
|
||||
return SuccessResponse(data=result_dict, msg="上传文件成功")
|
||||
@@ -73,11 +80,16 @@ async def upload_file_controller(
|
||||
|
||||
@ResourceRouter.get("/download", summary="下载文件", description="下载指定文件")
|
||||
async def download_file_controller(
|
||||
request: Request,
|
||||
path: str = Query(..., description="文件路径"),
|
||||
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:download"]))
|
||||
) -> FileResponse:
|
||||
"""下载文件"""
|
||||
file_path = await ResourceService.download_file_service(auth=auth, file_path=path)
|
||||
file_path = await ResourceService.download_file_service(
|
||||
auth=auth,
|
||||
file_path=path,
|
||||
base_url=str(request.base_url)
|
||||
)
|
||||
|
||||
# 获取文件名
|
||||
import os
|
||||
@@ -148,22 +160,31 @@ async def create_directory_controller(
|
||||
|
||||
@ResourceRouter.get("/stats", summary="获取资源统计", description="获取资源统计信息")
|
||||
async def get_resource_stats_controller(
|
||||
request: Request,
|
||||
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:query"]))
|
||||
) -> JSONResponse:
|
||||
"""获取资源统计"""
|
||||
result_dict = await ResourceService.get_stats_service(auth=auth)
|
||||
result_dict = await ResourceService.get_stats_service(
|
||||
auth=auth,
|
||||
base_url=str(request.base_url)
|
||||
)
|
||||
logger.info("获取资源统计成功")
|
||||
return SuccessResponse(data=result_dict, msg="获取资源统计成功")
|
||||
|
||||
|
||||
@ResourceRouter.post("/export", summary="导出资源列表", description="导出资源列表")
|
||||
async def export_resource_list_controller(
|
||||
request: Request,
|
||||
search: ResourceSearchSchema,
|
||||
auth: AuthSchema = Depends(AuthPermission(permissions=["resource:file:export"]))
|
||||
) -> StreamingResponse:
|
||||
"""导出资源列表"""
|
||||
# 获取搜索结果
|
||||
result_list = await ResourceService.search_resources_service(auth=auth, search=search)
|
||||
result_list = await ResourceService.search_resources_service(
|
||||
auth=auth,
|
||||
search=search,
|
||||
base_url=str(request.base_url)
|
||||
)
|
||||
export_result = await ResourceService.export_resource_service(data_list=result_list)
|
||||
|
||||
logger.info("导出资源列表成功")
|
||||
|
||||
@@ -99,7 +99,7 @@ class ResourceService:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def _get_file_info(cls, file_path: str) -> Dict[str, Any]:
|
||||
def _get_file_info(cls, file_path: str, base_url: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""获取文件信息"""
|
||||
try:
|
||||
safe_path = cls._get_safe_path(file_path)
|
||||
@@ -143,9 +143,16 @@ class ResourceService:
|
||||
except ValueError:
|
||||
depth = 0
|
||||
|
||||
# 生成HTTP URL路径而不是文件系统路径
|
||||
if base_url:
|
||||
from urllib.parse import urljoin
|
||||
http_url = urljoin(base_url.rstrip('/') + '/', f"{settings.STATIC_URL.lstrip('/')}/{relative_path}".lstrip('/')).replace('\\', '/').replace('//', '/')
|
||||
else:
|
||||
http_url = f"{settings.STATIC_URL}/{relative_path}".replace('\\', '/').replace('//', '/')
|
||||
|
||||
return {
|
||||
'name': path_obj.name,
|
||||
'path': safe_path,
|
||||
'path': http_url, # 返回HTTP URL而不是文件系统路径
|
||||
'relative_path': relative_path,
|
||||
'is_file': os.path.isfile(safe_path),
|
||||
'is_dir': os.path.isdir(safe_path),
|
||||
@@ -168,16 +175,37 @@ class ResourceService:
|
||||
cls,
|
||||
auth: AuthSchema,
|
||||
path: Optional[str] = None,
|
||||
recursive: bool = False,
|
||||
include_hidden: bool = False
|
||||
include_hidden: bool = False,
|
||||
base_url: Optional[str] = None
|
||||
) -> Dict:
|
||||
"""获取目录列表"""
|
||||
try:
|
||||
# 如果没有指定路径,使用静态文件根目录
|
||||
if path is None:
|
||||
safe_path = cls._get_resource_root()
|
||||
# 对于根目录,返回静态URL路径
|
||||
if base_url:
|
||||
from urllib.parse import urljoin
|
||||
display_path = urljoin(base_url.rstrip('/') + '/', settings.STATIC_URL.lstrip('/'))
|
||||
else:
|
||||
display_path = settings.STATIC_URL
|
||||
else:
|
||||
safe_path = cls._get_safe_path(path)
|
||||
# 对于子目录,生成相对于静态URL的路径
|
||||
resource_root = cls._get_resource_root()
|
||||
try:
|
||||
relative_path = os.path.relpath(safe_path, resource_root)
|
||||
if base_url:
|
||||
from urllib.parse import urljoin
|
||||
display_path = urljoin(base_url.rstrip('/') + '/', f"{settings.STATIC_URL.lstrip('/')}/{relative_path}".lstrip('/')).replace('\\', '/').replace('//', '/')
|
||||
else:
|
||||
display_path = f"{settings.STATIC_URL}/{relative_path}".replace('\\', '/').replace('//', '/')
|
||||
except ValueError:
|
||||
if base_url:
|
||||
from urllib.parse import urljoin
|
||||
display_path = urljoin(base_url.rstrip('/') + '/', settings.STATIC_URL.lstrip('/'))
|
||||
else:
|
||||
display_path = settings.STATIC_URL
|
||||
|
||||
if not os.path.exists(safe_path):
|
||||
raise CustomException(msg='目录不存在')
|
||||
@@ -197,7 +225,7 @@ class ResourceService:
|
||||
continue
|
||||
|
||||
item_path = os.path.join(safe_path, item_name)
|
||||
file_info = cls._get_file_info(item_path)
|
||||
file_info = cls._get_file_info(item_path, base_url)
|
||||
|
||||
if file_info:
|
||||
items.append(ResourceItemSchema(**file_info))
|
||||
@@ -207,19 +235,12 @@ class ResourceService:
|
||||
total_size += file_info.get('size', 0) or 0
|
||||
elif file_info['is_dir']:
|
||||
total_dirs += 1
|
||||
|
||||
# 递归统计子目录
|
||||
if recursive:
|
||||
sub_stats = await cls._get_directory_stats(item_path, include_hidden)
|
||||
total_files += sub_stats['files']
|
||||
total_dirs += sub_stats['dirs']
|
||||
total_size += sub_stats['size']
|
||||
|
||||
except PermissionError:
|
||||
raise CustomException(msg='没有权限访问此目录')
|
||||
|
||||
return ResourceDirectorySchema(
|
||||
path=safe_path,
|
||||
path=display_path, # 返回HTTP URL路径而不是文件系统路径
|
||||
name=os.path.basename(safe_path),
|
||||
items=items,
|
||||
total_files=total_files,
|
||||
@@ -264,7 +285,8 @@ class ResourceService:
|
||||
async def search_resources_service(
|
||||
cls,
|
||||
auth: AuthSchema,
|
||||
search: ResourceSearchSchema
|
||||
search: ResourceSearchSchema,
|
||||
base_url: Optional[str] = None
|
||||
) -> List[Dict]:
|
||||
"""搜索资源"""
|
||||
try:
|
||||
@@ -302,7 +324,7 @@ class ResourceService:
|
||||
if file_ext not in search.extensions:
|
||||
continue
|
||||
|
||||
file_info = cls._get_file_info(file_path)
|
||||
file_info = cls._get_file_info(file_path, base_url)
|
||||
|
||||
if cls._match_search_criteria(file_info, search):
|
||||
results.append(file_info)
|
||||
@@ -387,7 +409,8 @@ class ResourceService:
|
||||
cls,
|
||||
auth: AuthSchema,
|
||||
file: UploadFile,
|
||||
target_path: Optional[str] = None
|
||||
target_path: Optional[str] = None,
|
||||
base_url: Optional[str] = None
|
||||
) -> Dict:
|
||||
"""上传文件到指定目录"""
|
||||
if not file or not file.filename:
|
||||
@@ -432,7 +455,7 @@ class ResourceService:
|
||||
f.write(content)
|
||||
|
||||
# 获取文件信息
|
||||
file_info = cls._get_file_info(file_path)
|
||||
file_info = cls._get_file_info(file_path, base_url)
|
||||
|
||||
# 生成相对于资源根目录的URL路径
|
||||
resource_root = cls._get_resource_root()
|
||||
@@ -440,16 +463,25 @@ class ResourceService:
|
||||
relative_path = os.path.relpath(file_path, resource_root)
|
||||
# 确保路径使用正斜杠(URL格式)
|
||||
file_url_path = relative_path.replace(os.sep, '/')
|
||||
file_url = f"/resource/download?path={file_url_path}"
|
||||
# 如果提供了base_url,使用它生成完整URL,否则使用settings.STATIC_URL
|
||||
if base_url:
|
||||
from urllib.parse import urljoin
|
||||
file_url = urljoin(base_url.rstrip('/') + '/', f"{settings.STATIC_URL.lstrip('/')}/{file_url_path}".lstrip('/'))
|
||||
else:
|
||||
file_url = f"{settings.STATIC_URL}/{file_url_path}".replace('//', '/')
|
||||
except ValueError:
|
||||
# 如果无法计算相对路径,使用文件名
|
||||
file_url = f"/resource/download?path={filename}"
|
||||
if base_url:
|
||||
from urllib.parse import urljoin
|
||||
file_url = urljoin(base_url.rstrip('/') + '/', f"{settings.STATIC_URL.lstrip('/')}/{filename}".lstrip('/'))
|
||||
else:
|
||||
file_url = f"{settings.STATIC_URL}/{filename}"
|
||||
|
||||
logger.info(f"文件上传成功: {filename}")
|
||||
|
||||
return ResourceUploadSchema(
|
||||
filename=filename,
|
||||
file_path=file_path,
|
||||
file_path=file_url, # 返回HTTP URL而不是文件系统路径
|
||||
file_url=file_url,
|
||||
file_size=file_info.get('size', 0),
|
||||
resource_type=file_info.get('resource_type', ResourceType.OTHER),
|
||||
@@ -461,7 +493,7 @@ class ResourceService:
|
||||
raise CustomException(msg=f"文件上传失败: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
async def download_file_service(cls, auth: AuthSchema, file_path: str) -> str:
|
||||
async def download_file_service(cls, auth: AuthSchema, file_path: str, base_url: Optional[str] = None) -> str:
|
||||
"""下载文件(返回文件路径)"""
|
||||
try:
|
||||
safe_path = cls._get_safe_path(file_path)
|
||||
@@ -472,8 +504,28 @@ class ResourceService:
|
||||
if not os.path.isfile(safe_path):
|
||||
raise CustomException(msg='路径不是文件')
|
||||
|
||||
logger.info(f"下载文件: {safe_path}")
|
||||
return safe_path
|
||||
# 生成HTTP URL路径而不是返回文件系统路径
|
||||
resource_root = cls._get_resource_root()
|
||||
try:
|
||||
relative_path = os.path.relpath(safe_path, resource_root)
|
||||
# 生成HTTP URL
|
||||
if base_url:
|
||||
from urllib.parse import urljoin
|
||||
http_url = urljoin(base_url.rstrip('/') + '/', f"{settings.STATIC_URL.lstrip('/')}/{relative_path}".lstrip('/')).replace('\\', '/').replace('//', '/')
|
||||
else:
|
||||
http_url = f"{settings.STATIC_URL}/{relative_path}".replace('\\', '/').replace('//', '/')
|
||||
logger.info(f"生成文件访问URL: {http_url}")
|
||||
return http_url
|
||||
except ValueError:
|
||||
# 如果无法计算相对路径,使用文件名
|
||||
filename = os.path.basename(safe_path)
|
||||
if base_url:
|
||||
from urllib.parse import urljoin
|
||||
http_url = urljoin(base_url.rstrip('/') + '/', f"{settings.STATIC_URL.lstrip('/')}/{filename}".lstrip('/'))
|
||||
else:
|
||||
http_url = f"{settings.STATIC_URL}/{filename}"
|
||||
logger.info(f"生成文件访问URL: {http_url}")
|
||||
return http_url
|
||||
|
||||
except CustomException:
|
||||
raise
|
||||
@@ -628,7 +680,7 @@ class ResourceService:
|
||||
raise CustomException(msg=f"创建目录失败: {str(e)}")
|
||||
|
||||
@classmethod
|
||||
async def get_stats_service(cls, auth: AuthSchema) -> Dict:
|
||||
async def get_stats_service(cls, auth: AuthSchema, base_url: Optional[str] = None) -> Dict:
|
||||
"""获取资源统计信息"""
|
||||
try:
|
||||
# 使用静态文件根目录
|
||||
@@ -653,7 +705,7 @@ class ResourceService:
|
||||
for file in files:
|
||||
file_path = os.path.join(root, file)
|
||||
try:
|
||||
file_info = cls._get_file_info(file_path)
|
||||
file_info = cls._get_file_info(file_path, base_url)
|
||||
if file_info:
|
||||
total_files += 1
|
||||
total_size += file_info.get('size', 0) or 0
|
||||
|
||||
Reference in New Issue
Block a user