From c63a7de1c43d675fe892f4c99a7533b0b3397a7e Mon Sep 17 00:00:00 2001 From: jinhhanhan <1683105490@qq.com> Date: Wed, 21 Jan 2026 17:33:11 +0800 Subject: [PATCH] =?UTF-8?q?chore=EF=BC=9A=E5=BE=AE=E4=BF=A1=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=8F=AF=E4=BB=A5=E6=AD=A3=E5=B8=B8=E5=AF=B9?= =?UTF-8?q?=E8=AF=9D=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/js/ai-service.js | 196 ++++++++++++++++++++-------------------- 1 file changed, 99 insertions(+), 97 deletions(-) diff --git a/common/js/ai-service.js b/common/js/ai-service.js index d6c211a..ae2fa39 100644 --- a/common/js/ai-service.js +++ b/common/js/ai-service.js @@ -60,103 +60,105 @@ export default { /** * 流式消息入口(自动适配平台) */ - async sendStreamMessage(message, onChunk, onComplete) { - // #ifdef MP-WEIXIN - return new Promise((resolve, reject) => { - const socketTask = wx.connectSocket({ - url: 'wss://dev.aigc-quickapp.com/ws/aikefu', - header: { - } - }); - - let content = ''; - let conversationId = ''; - let isAuthenticated = false; - - // 连接成功 - socketTask.onOpen(() => { - console.log('WebSocket 连接成功,开始认证...'); - // 第一步:发送 auth 认证 - socketTask.send({ - data: JSON.stringify({ - action: 'auth', - uniacid: store.state.uniacid || '1', - token: store.state.token || 'test_token', - user_id: store.state.memberInfo?.id || 'anonymous' - }) - }); - }); - - // 接收消息 - socketTask.onMessage((res) => { - try { - const data = JSON.parse(res.data); - console.log('收到 WebSocket 消息:', data); - - // 处理认证响应 - if (data.type === 'auth_success') { - console.log('认证成功,发送聊天消息...'); - isAuthenticated = true; - socketTask.send({ - data: JSON.stringify({ - action: 'chat', - uniacid: store.state.uniacid || '1', - query: message, - user_id: store.state.memberInfo?.id || 'anonymous', - conversation_id: this.getConversationId() || '' - }) - }); - } else if (data.type === 'auth_failed') { - const errorMsg = '认证失败,请重新登录'; - console.error(errorMsg, data); - reject(new Error(errorMsg)); - if (onComplete) onComplete({ error: errorMsg }); - socketTask.close(); - } - // 处理聊天流式响应 - else if (data.action === 'chat_response') { - if (data.type === 'chunk') { - const text = data.content || ''; - content += text; - if (onChunk) onChunk(text); - } else if (data.type === 'end') { - conversationId = data.conversation_id || ''; - if (conversationId) { - this.setConversationId(conversationId); - } - if (onComplete) { - onComplete({ content, conversation_id: conversationId }); - } - resolve({ content, conversation_id: conversationId }); - socketTask.close(); - } - } - } catch (e) { - console.error('WebSocket 消息解析失败:', e, '原始数据:', res.data); - } - }); - - // 连接错误 - socketTask.onError((err) => { - const errorMsg = 'WebSocket 连接失败'; - console.error(errorMsg, err); - reject(new Error(errorMsg)); - if (onComplete) onComplete({ error: errorMsg }); - }); - - // 连接关闭 - socketTask.onClose(() => { - console.log('WebSocket 连接已关闭'); - }); - const timeout = setTimeout(() => { - if (!isAuthenticated || content === '') { - console.warn('WebSocket 超时,强制关闭'); - socketTask.close(); - reject(new Error('AI服务响应超时')); - if (onComplete) onComplete({ error: 'AI服务响应超时' }); - } - }, 10000); - }); + async sendStreamMessage(message, onChunk, onComplete) { + // #ifdef MP-WEIXIN + return new Promise((resolve, reject) => { + const socketTask = wx.connectSocket({ + url: 'wss://dev.aigc-quickapp.com/ws/aikefu', + header: {} + }); + + let content = ''; + let conversationId = ''; + let isAuthenticated = false; + + socketTask.onOpen(() => { + console.log('WebSocket 连接成功,开始认证...'); + socketTask.send({ + data: JSON.stringify({ + action: 'auth', + uniacid: store.state.uniacid || '1', + token: store.state.token || 'test_token', + user_id: store.state.memberInfo?.id || 'anonymous' + }) + }); + }); + + socketTask.onMessage((res) => { + try { + const data = JSON.parse(res.data); + console.log('收到 WebSocket 消息:', data); + + if (data.type === 'auth_success') { + console.log('认证成功,发送聊天消息...'); + isAuthenticated = true; + socketTask.send({ + data: JSON.stringify({ + action: 'chat', + uniacid: store.state.uniacid || '1', + query: message, + user_id: store.state.memberInfo?.id || 'anonymous', + conversation_id: this.getConversationId() || '' + }) + }); + } else if (data.type === 'auth_failed') { + const errorMsg = '认证失败,请重新登录'; + console.error(errorMsg, data); + reject(new Error(errorMsg)); + if (onComplete) onComplete({ error: errorMsg }); + socketTask.close(); + } + + // 处理流式消息块 + else if (data.type === 'message' || data.event === 'message') { + const text = data.answer || data.content || data.text || ''; + content += text; + if (onChunk) onChunk(text); + } + + // 处理流结束 + else if (data.event === 'message_end' || data.type === 'message_end') { + conversationId = data.conversation_id || ''; + if (conversationId) { + this.setConversationId(conversationId); + } + if (onComplete) { + onComplete({ content, conversation_id: conversationId }); + } + resolve({ content, conversation_id: conversationId }); + socketTask.close(); + } + + // 可选:处理 done + else if (data.type === 'done') { + console.log('对话完成:', data); + } + + } catch (e) { + console.error('WebSocket 消息解析失败:', e, '原始数据:', res.data); + } + }); + + socketTask.onError((err) => { + const errorMsg = 'WebSocket 连接失败'; + console.error(errorMsg, err); + reject(new Error(errorMsg)); + if (onComplete) onComplete({ error: errorMsg }); + }); + + socketTask.onClose(() => { + console.log('WebSocket 连接已关闭'); + }); + + const timeout = setTimeout(() => { + if (!isAuthenticated || content === '') { + console.warn('WebSocket 超时,强制关闭'); + socketTask.close(); + reject(new Error('AI服务响应超时')); + if (onComplete) onComplete({ error: 'AI服务响应超时' }); + } + }, 10000); + }); // #endif }, /**