From 23935a1a285ee14ed6033d79f630b500ec44f72f Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Sun, 12 Apr 2026 18:41:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=AE=80=E5=8C=96=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=8F=91=E9=80=81=E6=B5=81=E7=A8=8B=EF=BC=8C=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=AE=8C=E6=95=B4=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main_v2.py | 55 +++++++------------ templates/index.html | 125 +++---------------------------------------- 2 files changed, 24 insertions(+), 156 deletions(-) diff --git a/main_v2.py b/main_v2.py index 6afb527..4fc996f 100644 --- a/main_v2.py +++ b/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({ diff --git a/templates/index.html b/templates/index.html index 66a6ab7..07576c8 100644 --- a/templates/index.html +++ b/templates/index.html @@ -364,6 +364,7 @@ // 处理WebSocket消息 function handleWebSocketMessage(data) { + console.log('收到消息:', data.type); switch (data.type) { case 'history': displayHistory(data.messages); @@ -385,22 +386,10 @@ currentAgentId = data.agent_id; console.log('Agent已切换:', data.agent_name); break; - // 流式输出处理 - case 'thinking_start': - createStreamingMessage('thinking'); - break; - case 'thinking_stream': - appendStreamText('thinking', data.text); - break; - case 'thinking_end': - collapseThinking(data.content); - createStreamingMessage('content'); - break; - case 'content_stream': - appendStreamText('content', data.text); + case 'ping': + // 心跳,忽略 break; case 'stream_end': - finishStreaming(); document.getElementById('sendBtn').disabled = false; break; case 'user_message': @@ -411,8 +400,9 @@ } break; case 'assistant_message': - // 流式完成后,更新最终消息(添加Agent信息等) - updateFinalMessage(data.message); + // 直接显示完整回复 + appendMessage('assistant', data.message.content, data.message.source, data.message.thinking_content, data.message.agent_name); + document.getElementById('sendBtn').disabled = false; break; case 'error': showError(data.message); @@ -421,109 +411,6 @@ } } - // 创建流式消息容器 - function createStreamingMessage(type) { - const container = document.getElementById('messagesContainer'); - container.querySelector('.welcome')?.remove(); - - const existingStream = document.getElementById('streaming-message'); - if (existingStream && type === 'content') return; // 回答区域已存在 - - const div = document.createElement('div'); - div.id = 'streaming-message'; - div.className = 'message assistant'; - - if (type === 'thinking') { - div.innerHTML = ` -
🤔
-
-
- 💭 思考过程
- -
-
- `; - } else { - div.innerHTML = ` -
🤖
-
- - -
- `; - } - container.appendChild(div); - container.scrollTop = container.scrollHeight; - } - - // 追加流式文本 - function appendStreamText(type, text) { - const textEl = document.getElementById(type === 'thinking' ? 'thinking-text' : 'content-text'); - if (textEl) { - textEl.textContent += text; - const container = document.getElementById('messagesContainer'); - container.scrollTop = container.scrollHeight; - } - } - - // 折叠思考内容 - function collapseThinking(content) { - const streamEl = document.getElementById('thinking-stream'); - if (streamEl) { - // 创建折叠版本 - const collapsedDiv = document.getElementById('thinking-collapsed'); - if (collapsedDiv) { - collapsedDiv.style.display = 'block'; - collapsedDiv.innerHTML = ` -
- 思考过程 (点击展开) -
- - `; - } - streamEl.style.display = 'none'; - } - } - - // 展开/折叠思考内容 - function toggleThinkingBlock(el) { - const expanded = el.parentElement.querySelector('.thinking-expanded'); - const hint = el.querySelector('.toggle-hint'); - if (expanded.style.display === 'none') { - expanded.style.display = 'block'; - hint.textContent = '(点击折叠)'; - } else { - expanded.style.display = 'none'; - hint.textContent = '(点击展开)'; - } - } - - // 完成流式输出 - function finishStreaming() { - const streamMsg = document.getElementById('streaming-message'); - if (streamMsg) { - streamMsg.id = ''; // 移除临时ID - } - } - - // 更新最终消息(添加Agent信息) - function updateFinalMessage(msg) { - const messages = document.querySelectorAll('.message.assistant'); - const lastMsg = messages[messages.length - 1]; - if (lastMsg && msg.agent_name) { - const contentDiv = lastMsg.querySelector('.message-content'); - if (contentDiv && !contentDiv.querySelector('.agent-info')) { - const agentInfo = document.createElement('div'); - agentInfo.className = 'agent-info'; - agentInfo.style.cssText = 'font-size:12px;color:#999;margin-top:8px;'; - agentInfo.innerHTML = ` ${msg.agent_name}`; - contentDiv.appendChild(agentInfo); - } - } - } - // 显示错误 function showError(msg) { const container = document.getElementById('messagesContainer');