优化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 -2
View File
@@ -56,8 +56,12 @@ class ChatBotController extends Controller
$request->validate([ $request->validate([
'message' => 'required|string|max:2000', 'message' => 'required|string|max:2000',
'room_id' => 'required|integer', 'room_id' => 'required|integer',
'is_secret' => 'nullable|boolean',
]); ]);
// 私聊模式:AI 回复也走悄悄话,仅发言人和 AI 可见
$isSecret = (bool) $request->input('is_secret', false);
// 检查全局开关 // 检查全局开关
$enabled = Sysparam::getValue('chatbot_enabled', '0'); $enabled = Sysparam::getValue('chatbot_enabled', '0');
if ($enabled !== '1') { if ($enabled !== '1') {
@@ -148,14 +152,14 @@ class ChatBotController extends Controller
} }
} }
// 广播 AI 回复消息 // 广播 AI 回复消息(私聊模式下仅发言人与 AI 可见)
$botMsg = [ $botMsg = [
'id' => $this->chatState->nextMessageId($roomId), 'id' => $this->chatState->nextMessageId($roomId),
'room_id' => $roomId, 'room_id' => $roomId,
'from_user' => 'AI小班长', 'from_user' => 'AI小班长',
'to_user' => $user->username, 'to_user' => $user->username,
'content' => $reply, 'content' => $reply,
'is_secret' => false, 'is_secret' => $isSecret,
'font_color' => '#16a34a', 'font_color' => '#16a34a',
'action' => '', 'action' => '',
'sent_at' => now()->toDateTimeString(), 'sent_at' => now()->toDateTimeString(),
@@ -6,13 +6,30 @@
* 发送消息给 AI 机器人 * 发送消息给 AI 机器人
* 先在包厢窗口显示用户消息,再调用 API 获取回复 * 先在包厢窗口显示用户消息,再调用 API 获取回复
*/ */
async function sendToChatBot(content) { async function sendToChatBot(content, isSecret = false) {
if (chatBotSending) { if (chatBotSending) {
window.chatDialog.alert('AI 正在思考中,请稍候...', '提示', '#336699'); window.chatDialog.alert('AI 正在思考中,请稍候...', '提示', '#336699');
return; return;
} }
chatBotSending = true; 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 { try {
const res = await fetch(window.chatContext.chatBotUrl, { const res = await fetch(window.chatContext.chatBotUrl, {
method: 'POST', method: 'POST',
@@ -24,28 +41,23 @@
}, },
body: JSON.stringify({ body: JSON.stringify({
message: content, message: content,
room_id: window.chatContext.roomId room_id: window.chatContext.roomId,
is_secret: isSecret ? 1 : 0,
}) })
}); });
const data = await res.json(); const data = await res.json();
if (!res.ok || data.status !== 'success') { if (!res.ok || data.status !== 'success') {
const errDiv = document.createElement('div'); // 接口返回的错误信息:仅显示在包厢窗口,不走公屏
errDiv.className = 'msg-line'; appendBotError(data.message || '回复失败,请稍后重试');
errDiv.innerHTML =
`<span style="color: #dc2626;">🤖【AI小班长】${data.message || '回复失败,请稍后重试'}</span>`;
container.appendChild(errDiv);
} }
} catch (e) { } catch (e) {
const errDiv = document.createElement('div'); // 网络层异常:同样只显示在包厢窗口
errDiv.className = 'msg-line'; appendBotError('网络连接错误,请稍后重试');
errDiv.innerHTML = '<span style="color: #dc2626;">🤖【AI小班长】网络连接错误,请稍后重试</span>';
container.appendChild(errDiv);
} }
chatBotSending = false; chatBotSending = false;
scrollToBottom();
} }
/** /**
@@ -3397,7 +3397,7 @@
// 如果发言对象是 AI 小助手,也发送一份给专用机器人 API,不打断正常的发消息流程 // 如果发言对象是 AI 小助手,也发送一份给专用机器人 API,不打断正常的发消息流程
if (toUser === 'AI小班长' && content) { if (toUser === 'AI小班长' && content) {
sendToChatBot(content); // 异步调用,不阻塞全局发送 sendToChatBot(content, composerState.isSecret); // 异步调用,私聊状态一并传递
} }
// ── 神秘箱子暗号拦截 ──────────────────────────────────── // ── 神秘箱子暗号拦截 ────────────────────────────────────