优化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([
'message' => 'required|string|max:2000',
'room_id' => 'required|integer',
'is_secret' => 'nullable|boolean',
]);
// 私聊模式:AI 回复也走悄悄话,仅发言人和 AI 可见
$isSecret = (bool) $request->input('is_secret', false);
// 检查全局开关
$enabled = Sysparam::getValue('chatbot_enabled', '0');
if ($enabled !== '1') {
@@ -148,14 +152,14 @@ class ChatBotController extends Controller
}
}
// 广播 AI 回复消息
// 广播 AI 回复消息(私聊模式下仅发言人与 AI 可见)
$botMsg = [
'id' => $this->chatState->nextMessageId($roomId),
'room_id' => $roomId,
'from_user' => 'AI小班长',
'to_user' => $user->username,
'content' => $reply,
'is_secret' => false,
'is_secret' => $isSecret,
'font_color' => '#16a34a',
'action' => '',
'sent_at' => now()->toDateTimeString(),
@@ -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();
}
/**
@@ -3397,7 +3397,7 @@
// 如果发言对象是 AI 小助手,也发送一份给专用机器人 API,不打断正常的发消息流程
if (toUser === 'AI小班长' && content) {
sendToChatBot(content); // 异步调用,不阻塞全局发送
sendToChatBot(content, composerState.isSecret); // 异步调用,私聊状态一并传递
}
// ── 神秘箱子暗号拦截 ────────────────────────────────────