From 7b6a8500b0e167089e90e2ed9d1ccab66305b06a Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Wed, 10 Dec 2025 11:46:20 +0800 Subject: [PATCH] =?UTF-8?q?chore(addon/aikefu):=20=E5=8F=AA=E5=85=81?= =?UTF-8?q?=E8=AE=B8info=E5=8F=8Aerror=E7=9A=84=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/addon/aikefu/api/controller/Kefu.php | 4 ++ src/addon/aikefu/docs/stream_chat_demo.html | 57 +++++++++++++++------ 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/addon/aikefu/api/controller/Kefu.php b/src/addon/aikefu/api/controller/Kefu.php index f8c2f1593..1248f2ef0 100644 --- a/src/addon/aikefu/api/controller/Kefu.php +++ b/src/addon/aikefu/api/controller/Kefu.php @@ -358,6 +358,10 @@ class Kefu extends BaseApi */ private function log($message, $level = 'info') { + // 只允许info、error级别 + if (!in_array($level, ['info', 'error'])) { + return; + } log_write($message, $level, '', 2); } diff --git a/src/addon/aikefu/docs/stream_chat_demo.html b/src/addon/aikefu/docs/stream_chat_demo.html index 7f94fbd03..e6326508a 100644 --- a/src/addon/aikefu/docs/stream_chat_demo.html +++ b/src/addon/aikefu/docs/stream_chat_demo.html @@ -1,5 +1,6 @@ + @@ -12,20 +13,24 @@ padding: 20px; background-color: #f5f5f5; } + .container { background-color: white; border-radius: 8px; - box-shadow: 0 2px 10px rgba(0,0,0,0.1); + box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); padding: 20px; } + h1 { text-align: center; color: #333; } + .method-selector { margin-bottom: 20px; text-align: center; } + .method-btn { padding: 10px 20px; margin: 0 10px; @@ -36,9 +41,11 @@ cursor: pointer; font-size: 14px; } + .method-btn.active { background-color: #0056b3; } + #chat-container { border: 1px solid #ddd; border-radius: 4px; @@ -48,27 +55,32 @@ padding: 10px; background-color: #fafafa; } + .message { margin-bottom: 15px; padding: 10px; border-radius: 8px; max-width: 80%; } + .user-message { background-color: #007bff; color: white; margin-left: auto; } + .ai-message { background-color: #e9ecef; color: #333; margin-right: auto; white-space: pre-wrap; } + .input-area { display: flex; gap: 10px; } + #message-input { flex: 1; padding: 12px; @@ -76,6 +88,7 @@ border-radius: 4px; font-size: 14px; } + #send-btn { padding: 12px 20px; background-color: #28a745; @@ -85,10 +98,12 @@ cursor: pointer; font-size: 14px; } + #send-btn:disabled { background-color: #6c757d; cursor: not-allowed; } + .status { margin-top: 10px; font-size: 12px; @@ -97,23 +112,24 @@ } +

流式聊天测试 Demo

- +

选择请求方式:

- +
- +
- +
就绪
@@ -160,10 +176,10 @@ // 清空输入框 messageInput.value = ''; - + // 添加用户消息到聊天记录 addMessageToChat(message, 'user'); - + // 禁用发送按钮 sendBtn.disabled = true; statusText.textContent = '正在发送请求...'; @@ -206,7 +222,7 @@ console.log('收到消息:', event); try { const data = JSON.parse(event.data); - + if (data.event === 'message') { // 更新 AI 消息 aiMessage += data.answer || ''; @@ -214,8 +230,8 @@ } if (data.event === 'message_end') { - statusText.textContent = '对话完成'; - sendBtn.disabled = false; + statusText.textContent = '对话完成'; + sendBtn.disabled = false; } if (data.conversation_id) { @@ -244,10 +260,18 @@ // 监听错误事件 es.addEventListener('error', (error) => { console.error('EventSource 错误:', error); + // 添加更详细的错误信息 + if (es.readyState === EventSource.CLOSED) { + console.error('EventSource 连接已关闭'); + } else if (es.readyState === EventSource.CONNECTING) { + console.error('EventSource 连接中出现错误'); + } statusText.textContent = '连接错误'; sendBtn.disabled = false; - es.close(); - es = null; + if (es) { + es.close(); + es = null; + } }); // 监听关闭事件 @@ -315,7 +339,7 @@ if (done) break; buffer += decoder.decode(value, { stream: true }); - + // 处理接收到的数据 buffer = processStreamData(buffer, (newData) => { if (newData) { @@ -363,7 +387,7 @@ if (dataPart) { try { const data = JSON.parse(dataPart); - + if (data.event === 'message') { callback(data.answer || ''); } @@ -393,7 +417,7 @@ messageDiv.textContent = message; chatContainer.appendChild(messageDiv); chatContainer.scrollTop = chatContainer.scrollHeight; - + // 如果是用户消息,添加一个临时的 AI 消息容器 if (type === 'user') { const aiMessageDiv = document.createElement('div'); @@ -414,7 +438,7 @@ aiMessageDiv.className = 'message ai-message'; chatContainer.appendChild(aiMessageDiv); } - + aiMessageDiv.textContent = message; chatContainer.scrollTop = chatContainer.scrollHeight; } @@ -433,4 +457,5 @@ statusText.textContent = '就绪,使用 EventSource 方式'; + \ No newline at end of file