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')
{
// 只允许info、error级别
if (!in_array($level, ['info', 'error'])) {
return;
}
log_write($message, $level, '', 2);
}

View File

@@ -1,5 +1,6 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@@ -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 @@
}
</style>
</head>
<body>
<div class="container">
<h1>流式聊天测试 Demo</h1>
<div class="method-selector">
<h3>选择请求方式:</h3>
<button class="method-btn active" data-method="eventsource">EventSource</button>
<button class="method-btn" data-method="fetch">Fetch API</button>
</div>
<div id="chat-container"></div>
<div class="input-area">
<input type="text" id="message-input" placeholder="输入消息...">
<button id="send-btn">发送</button>
</div>
<div class="status">
<span id="status-text">就绪</span>
</div>
@@ -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 方式';
</script>
</body>
</html>