优化ai小班长聊天

This commit is contained in:
2026-04-25 00:42:46 +08:00
parent aab609f69b
commit 8cf5029711
3 changed files with 31 additions and 15 deletions
@@ -6,13 +6,30 @@
* 发送消息给 AI 机器人
* 先在包厢窗口显示用户消息,再调用 API 获取回复
*/
async function sendToChatBot(content) {
async function sendToChatBot(content, isSecret = false) {
if (chatBotSending) {
window.chatDialog.alert('AI 正在思考中,请稍候...', '提示', '#336699');
return;
}
chatBotSending = true;
/**
* AI 错误提示追加到包厢窗口,样式与正常 AI 回复保持一致。
*
* @param {string} text 错误提示文本
*/
function appendBotError(text) {
const msgBox = document.getElementById('chat-messages-container2');
if (!msgBox) return;
const errDiv = document.createElement('div');
errDiv.className = 'msg-line';
errDiv.innerHTML = `<span style="color: #dc2626;">🤖【AI小班长】${text}</span>`;
msgBox.appendChild(errDiv);
if (typeof autoScroll !== 'undefined' && autoScroll) {
msgBox.scrollTop = msgBox.scrollHeight;
}
}
try {
const res = await fetch(window.chatContext.chatBotUrl, {
method: 'POST',
@@ -24,28 +41,23 @@
},
body: JSON.stringify({
message: content,
room_id: window.chatContext.roomId
room_id: window.chatContext.roomId,
is_secret: isSecret ? 1 : 0,
})
});
const data = await res.json();
if (!res.ok || data.status !== 'success') {
const errDiv = document.createElement('div');
errDiv.className = 'msg-line';
errDiv.innerHTML =
`<span style="color: #dc2626;">🤖【AI小班长】${data.message || '回复失败,请稍后重试'}</span>`;
container.appendChild(errDiv);
// 接口返回的错误信息:仅显示在包厢窗口,不走公屏
appendBotError(data.message || '回复失败,请稍后重试');
}
} catch (e) {
const errDiv = document.createElement('div');
errDiv.className = 'msg-line';
errDiv.innerHTML = '<span style="color: #dc2626;">🤖【AI小班长】网络连接错误,请稍后重试</span>';
container.appendChild(errDiv);
// 网络层异常:同样只显示在包厢窗口
appendBotError('网络连接错误,请稍后重试');
}
chatBotSending = false;
scrollToBottom();
}
/**