From 83ef5c2960c756d5fb46f270bd2c5b61db0e2d80 Mon Sep 17 00:00:00 2001 From: lik Date: Sat, 24 Oct 2026 18:31:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=81=8A=E5=A4=A9=E5=B8=A7?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E4=B8=8E=E6=B5=81=E5=BC=8F=E7=BB=93=E6=9D=9F?= =?UTF-8?q?=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Kimi-K3.1-Pro noreply@anthropic.com --- websocket.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/websocket.js b/websocket.js index c733ae2..0b20588 100644 --- a/websocket.js +++ b/websocket.js @@ -5,6 +5,19 @@ import { chatTask } from "./agent/bots/bot-escort/task.js"; import { adminAgent } from "./agent/bots/bot-escort-admin/agent.js" import { getUserByToken } from "./utils/user_service.js"; +let systemMessageSequence = 0; + +// 所有下行帧保持同一结构,调用方无需按消息类型猜测字段。 +const sendFrame = (ws, source, type, content = '', id = '') => { + if (ws.readyState !== WebSocket.OPEN) return; + ws.send(JSON.stringify({ + source, + type, + content, + id: id || `system_${Date.now()}_${++systemMessageSequence}` + })); +}; + export default class WebSocketServerManager { constructor(port = 8080) { this.port = port; @@ -44,7 +57,7 @@ export default class WebSocketServerManager { if (path === '/chat') { this._handleChatConnection(ws); } else { - ws.send(JSON.stringify({ type: 'system', content: 'Unknown path' })); + sendFrame(ws, 'system', 'error', 'Unknown path'); ws.close(1000, 'Unknown path'); } }); @@ -55,7 +68,7 @@ export default class WebSocketServerManager { } _handleChatConnection(ws) { - ws.send(JSON.stringify({ type: 'system', content: 'Connected to chat server' })); + sendFrame(ws, 'system', 'connected', 'Connected to chat server'); ws.on('message', (data) => this._handleMessage(ws, data)); @@ -77,7 +90,7 @@ export default class WebSocketServerManager { console.log('Received message type:', msg.type); if (msg.type === 'ping') { - ws.send(JSON.stringify({ type: 'pong' })); + sendFrame(ws, 'system', 'pong', '', msg.id); return; } @@ -86,26 +99,24 @@ export default class WebSocketServerManager { if (ws.readyState !== WebSocket.OPEN) return; const send = (source, type, content, id) => { - if (ws.readyState === WebSocket.OPEN) { - ws.send(JSON.stringify({ source, type, content, id })); - } + sendFrame(ws, source, type, content, id); }; try { // 身份只认 token:不再接受凭 userId 明文接入,防止冒充他人聊天 if (!msg.token) { - ws.send(JSON.stringify({ type: 'error', content: '缺少登录凭证,请重新登录' })); + send('system', 'error', '请先登录', msg.id); return; } const userInfo = await getUserByToken(msg.token); if (!userInfo) { - ws.send(JSON.stringify({ type: 'error', content: '登录已失效' })); + send('system', 'error', '请先登录', msg.id); return; } if (msg.agent === 'escort-admin') { // 管理端 agent 含写库/环境变量等高危工具,必须校验管理员角色,防止普通用户越权 if (!userInfo.app || !("wxapp-escort-admin" in userInfo.app)) { - ws.send(JSON.stringify({ type: 'error', content: '无管理员权限' })); + send('system', 'error', '无管理员权限', msg.id); return; } await adminAgent.streamChat(userInfo, [msg], (source, type, content, id) => { @@ -116,9 +127,11 @@ export default class WebSocketServerManager { send(source, type, content, id); }); } + // 流式 chunk 可能携带不同的模型消息 ID,结束帧统一关联本次客户端请求。 + send('system', 'end', '', msg.id); } catch (err) { console.error('streamChat error:', err); - send('system', 'error', '处理消息失败'); + send('system', 'error', '处理消息失败,请稍后重试', msg.id); } return; } @@ -131,7 +144,7 @@ export default class WebSocketServerManager { console.log('[Unknown message type]', msg.type); } catch (e) { console.error('Message parse error:', e); - ws.send(JSON.stringify({ type: 'error', content: 'Invalid JSON' })); + sendFrame(ws, 'system', 'error', '消息格式错误'); } }