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:
zhangtao
2025-09-09 01:09:33 +08:00
parent 5fa3a3a759
commit 8bb5134cb2
15 changed files with 1042 additions and 911 deletions
+18 -6
View File
@@ -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()