chore(addon/aikefu): 只允许info及error的日志输出

This commit is contained in:
2025-12-10 11:46:20 +08:00
parent 99628f1d18
commit 7b6a8500b0
2 changed files with 45 additions and 16 deletions

View File

@@ -358,6 +358,10 @@ class Kefu extends BaseApi
*/ */
private function log($message, $level = 'info') private function log($message, $level = 'info')
{ {
// 只允许info、error级别
if (!in_array($level, ['info', 'error'])) {
return;
}
log_write($message, $level, '', 2); log_write($message, $level, '', 2);
} }

View File

@@ -1,5 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh-CN"> <html lang="zh-CN">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -12,20 +13,24 @@
padding: 20px; padding: 20px;
background-color: #f5f5f5; background-color: #f5f5f5;
} }
.container { .container {
background-color: white; background-color: white;
border-radius: 8px; 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; padding: 20px;
} }
h1 { h1 {
text-align: center; text-align: center;
color: #333; color: #333;
} }
.method-selector { .method-selector {
margin-bottom: 20px; margin-bottom: 20px;
text-align: center; text-align: center;
} }
.method-btn { .method-btn {
padding: 10px 20px; padding: 10px 20px;
margin: 0 10px; margin: 0 10px;
@@ -36,9 +41,11 @@
cursor: pointer; cursor: pointer;
font-size: 14px; font-size: 14px;
} }
.method-btn.active { .method-btn.active {
background-color: #0056b3; background-color: #0056b3;
} }
#chat-container { #chat-container {
border: 1px solid #ddd; border: 1px solid #ddd;
border-radius: 4px; border-radius: 4px;
@@ -48,27 +55,32 @@
padding: 10px; padding: 10px;
background-color: #fafafa; background-color: #fafafa;
} }
.message { .message {
margin-bottom: 15px; margin-bottom: 15px;
padding: 10px; padding: 10px;
border-radius: 8px; border-radius: 8px;
max-width: 80%; max-width: 80%;
} }
.user-message { .user-message {
background-color: #007bff; background-color: #007bff;
color: white; color: white;
margin-left: auto; margin-left: auto;
} }
.ai-message { .ai-message {
background-color: #e9ecef; background-color: #e9ecef;
color: #333; color: #333;
margin-right: auto; margin-right: auto;
white-space: pre-wrap; white-space: pre-wrap;
} }
.input-area { .input-area {
display: flex; display: flex;
gap: 10px; gap: 10px;
} }
#message-input { #message-input {
flex: 1; flex: 1;
padding: 12px; padding: 12px;
@@ -76,6 +88,7 @@
border-radius: 4px; border-radius: 4px;
font-size: 14px; font-size: 14px;
} }
#send-btn { #send-btn {
padding: 12px 20px; padding: 12px 20px;
background-color: #28a745; background-color: #28a745;
@@ -85,10 +98,12 @@
cursor: pointer; cursor: pointer;
font-size: 14px; font-size: 14px;
} }
#send-btn:disabled { #send-btn:disabled {
background-color: #6c757d; background-color: #6c757d;
cursor: not-allowed; cursor: not-allowed;
} }
.status { .status {
margin-top: 10px; margin-top: 10px;
font-size: 12px; font-size: 12px;
@@ -97,23 +112,24 @@
} }
</style> </style>
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h1>流式聊天测试 Demo</h1> <h1>流式聊天测试 Demo</h1>
<div class="method-selector"> <div class="method-selector">
<h3>选择请求方式:</h3> <h3>选择请求方式:</h3>
<button class="method-btn active" data-method="eventsource">EventSource</button> <button class="method-btn active" data-method="eventsource">EventSource</button>
<button class="method-btn" data-method="fetch">Fetch API</button> <button class="method-btn" data-method="fetch">Fetch API</button>
</div> </div>
<div id="chat-container"></div> <div id="chat-container"></div>
<div class="input-area"> <div class="input-area">
<input type="text" id="message-input" placeholder="输入消息..."> <input type="text" id="message-input" placeholder="输入消息...">
<button id="send-btn">发送</button> <button id="send-btn">发送</button>
</div> </div>
<div class="status"> <div class="status">
<span id="status-text">就绪</span> <span id="status-text">就绪</span>
</div> </div>
@@ -160,10 +176,10 @@
// 清空输入框 // 清空输入框
messageInput.value = ''; messageInput.value = '';
// 添加用户消息到聊天记录 // 添加用户消息到聊天记录
addMessageToChat(message, 'user'); addMessageToChat(message, 'user');
// 禁用发送按钮 // 禁用发送按钮
sendBtn.disabled = true; sendBtn.disabled = true;
statusText.textContent = '正在发送请求...'; statusText.textContent = '正在发送请求...';
@@ -206,7 +222,7 @@
console.log('收到消息:', event); console.log('收到消息:', event);
try { try {
const data = JSON.parse(event.data); const data = JSON.parse(event.data);
if (data.event === 'message') { if (data.event === 'message') {
// 更新 AI 消息 // 更新 AI 消息
aiMessage += data.answer || ''; aiMessage += data.answer || '';
@@ -214,8 +230,8 @@
} }
if (data.event === 'message_end') { if (data.event === 'message_end') {
statusText.textContent = '对话完成'; statusText.textContent = '对话完成';
sendBtn.disabled = false; sendBtn.disabled = false;
} }
if (data.conversation_id) { if (data.conversation_id) {
@@ -244,10 +260,18 @@
// 监听错误事件 // 监听错误事件
es.addEventListener('error', (error) => { es.addEventListener('error', (error) => {
console.error('EventSource 错误:', error); console.error('EventSource 错误:', error);
// 添加更详细的错误信息
if (es.readyState === EventSource.CLOSED) {
console.error('EventSource 连接已关闭');
} else if (es.readyState === EventSource.CONNECTING) {
console.error('EventSource 连接中出现错误');
}
statusText.textContent = '连接错误'; statusText.textContent = '连接错误';
sendBtn.disabled = false; sendBtn.disabled = false;
es.close(); if (es) {
es = null; es.close();
es = null;
}
}); });
// 监听关闭事件 // 监听关闭事件
@@ -315,7 +339,7 @@
if (done) break; if (done) break;
buffer += decoder.decode(value, { stream: true }); buffer += decoder.decode(value, { stream: true });
// 处理接收到的数据 // 处理接收到的数据
buffer = processStreamData(buffer, (newData) => { buffer = processStreamData(buffer, (newData) => {
if (newData) { if (newData) {
@@ -363,7 +387,7 @@
if (dataPart) { if (dataPart) {
try { try {
const data = JSON.parse(dataPart); const data = JSON.parse(dataPart);
if (data.event === 'message') { if (data.event === 'message') {
callback(data.answer || ''); callback(data.answer || '');
} }
@@ -393,7 +417,7 @@
messageDiv.textContent = message; messageDiv.textContent = message;
chatContainer.appendChild(messageDiv); chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight; chatContainer.scrollTop = chatContainer.scrollHeight;
// 如果是用户消息,添加一个临时的 AI 消息容器 // 如果是用户消息,添加一个临时的 AI 消息容器
if (type === 'user') { if (type === 'user') {
const aiMessageDiv = document.createElement('div'); const aiMessageDiv = document.createElement('div');
@@ -414,7 +438,7 @@
aiMessageDiv.className = 'message ai-message'; aiMessageDiv.className = 'message ai-message';
chatContainer.appendChild(aiMessageDiv); chatContainer.appendChild(aiMessageDiv);
} }
aiMessageDiv.textContent = message; aiMessageDiv.textContent = message;
chatContainer.scrollTop = chatContainer.scrollHeight; chatContainer.scrollTop = chatContainer.scrollHeight;
} }
@@ -433,4 +457,5 @@
statusText.textContent = '就绪,使用 EventSource 方式'; statusText.textContent = '就绪,使用 EventSource 方式';
</script> </script>
</body> </body>
</html> </html>