fix: 简化消息发送流程,直接发送完整回复
This commit is contained in:
55
main_v2.py
55
main_v2.py
@@ -60,8 +60,13 @@ class ConnectionManager:
|
||||
for connection in self.active_connections[user_id]:
|
||||
try:
|
||||
await connection.send_json(message)
|
||||
except:
|
||||
pass
|
||||
logger.info(f"发送消息到用户 {user_id}: {message.get('type', 'unknown')}")
|
||||
except Exception as e:
|
||||
logger.error(f"发送消息失败: {e}")
|
||||
|
||||
async def ping(self, user_id: str):
|
||||
"""发送心跳ping"""
|
||||
await self.send_to_user(user_id, {"type": "ping"})
|
||||
|
||||
|
||||
manager = ConnectionManager()
|
||||
@@ -661,13 +666,6 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
|
||||
|
||||
# 使用非流式调用LLM(简化版本,确保稳定)
|
||||
try:
|
||||
# 先发送思考开始
|
||||
if agent_config['agent'].get('enable_thinking') and enable_thinking:
|
||||
await websocket.send_json({
|
||||
"type": "thinking_start",
|
||||
"conversation_id": conversation_id
|
||||
})
|
||||
|
||||
# 调用LLM(非流式)
|
||||
response, thinking_content = await llm_service.chat(
|
||||
messages=history,
|
||||
@@ -676,33 +674,7 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
|
||||
enable_thinking=enable_thinking
|
||||
)
|
||||
|
||||
# 发送思考内容
|
||||
if thinking_content:
|
||||
await websocket.send_json({
|
||||
"type": "thinking_content",
|
||||
"conversation_id": conversation_id,
|
||||
"content": thinking_content
|
||||
})
|
||||
|
||||
# 发送思考结束
|
||||
await websocket.send_json({
|
||||
"type": "thinking_end",
|
||||
"conversation_id": conversation_id,
|
||||
"content": thinking_content
|
||||
})
|
||||
|
||||
# 直接发送完整回复
|
||||
await websocket.send_json({
|
||||
"type": "content_stream",
|
||||
"conversation_id": conversation_id,
|
||||
"text": response
|
||||
})
|
||||
|
||||
# 发送完成通知
|
||||
await websocket.send_json({
|
||||
"type": "stream_end",
|
||||
"conversation_id": conversation_id
|
||||
})
|
||||
logger.info(f"LLM响应: response长度={len(response)}, thinking长度={len(thinking_content) if thinking_content else 0}")
|
||||
|
||||
# 保存AI回复
|
||||
assistant_msg = conv_service.add_message(
|
||||
@@ -715,7 +687,8 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
|
||||
model_used=agent_config['provider'].get('default_model')
|
||||
)
|
||||
|
||||
await manager.send_to_user(MAIN_USER_ID, {
|
||||
# 发送完整回复(包含思考内容)
|
||||
await websocket.send_json({
|
||||
"type": "assistant_message",
|
||||
"conversation_id": conversation_id,
|
||||
"message": {
|
||||
@@ -730,6 +703,14 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
|
||||
}
|
||||
})
|
||||
|
||||
logger.info(f"AI回复已发送: conversation_id={conversation_id}")
|
||||
|
||||
# 启用发送按钮
|
||||
await websocket.send_json({
|
||||
"type": "stream_end",
|
||||
"conversation_id": conversation_id
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"LLM调用失败: {e}")
|
||||
await websocket.send_json({
|
||||
|
||||
Reference in New Issue
Block a user