From e98fe3eec554ee100caf3f5e0f9d4ad5c8703aa1 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Tue, 15 Jul 2025 19:03:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=BD=AC=E5=8F=91=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E7=95=99=E8=A8=80=E6=97=B6ai=E4=BC=9A?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E4=B8=A4=E6=9D=A1=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/WebSocketDialog.php | 21 +++++++++++ app/Models/WebSocketDialogMsg.php | 61 ++++++++++++++++--------------- app/Tasks/BotReceiveMsgTask.php | 4 ++ 3 files changed, 56 insertions(+), 30 deletions(-) diff --git a/app/Models/WebSocketDialog.php b/app/Models/WebSocketDialog.php index 42d2c829f..25b71f94d 100644 --- a/app/Models/WebSocketDialog.php +++ b/app/Models/WebSocketDialog.php @@ -705,6 +705,27 @@ class WebSocketDialog extends AbstractModel return WebSocketDialogUser::whereDialogId($this->id)->where('userid', '>', 0)->count() === 1; } + /** + * 检查是否是AI对话 + * @return bool + */ + public function isAiDialog() + { + // 这个不会有变化,所以可以使用永久缓存 + return Cache::rememberForever('is-ai-dialog-' . $this->id, function () { + if ($this->type !== 'user') { + return false; + } + $data = $this->dialogUserBuilder()->get(); + foreach ($data as $item) { + if (preg_match('/^ai-(.*?)@bot\.system$/', $item->email)) { + return true; + } + } + return false; + }); + } + /** * 获取对话(同时检验对话身份) * @param $dialog_id diff --git a/app/Models/WebSocketDialogMsg.php b/app/Models/WebSocketDialogMsg.php index 83e56cbdd..15b3041d3 100644 --- a/app/Models/WebSocketDialogMsg.php +++ b/app/Models/WebSocketDialogMsg.php @@ -456,27 +456,10 @@ class WebSocketDialogMsg extends AbstractModel 'parent_id' => $this->id, // 转发的消息ID 'parent_userid' => $this->userid, // 转发的消息会员ID 'show' => $showSource, // 是否显示原发送者信息 + 'leave' => $leaveMessage ? 1 : 0, // 是否留言(用于判断是否发给AI) ]; $msgs = []; - $already = []; - if ($dialogids) { - if (!is_array($dialogids)) { - $dialogids = [$dialogids]; - } - foreach ($dialogids as $dialogid) { - $res = self::sendMsg('forward-' . $forwardId, $dialogid, $this->type, $msgData, $user->userid); - if (Base::isSuccess($res)) { - $msgs[] = $res['data']; - $already[] = $dialogid; - } - if ($leaveMessage) { - $res = self::sendMsg(null, $dialogid, 'text', ['text' => $leaveMessage], $user->userid); - if (Base::isSuccess($res)) { - $msgs[] = $res['data']; - } - } - } - } + $dialogs = []; if ($userids) { if (!is_array($userids)) { $userids = [$userids]; @@ -486,17 +469,35 @@ class WebSocketDialogMsg extends AbstractModel continue; } $dialog = WebSocketDialog::checkUserDialog($user, $userid); - if ($dialog && !in_array($dialog->id, $already)) { - $res = self::sendMsg('forward-' . $forwardId, $dialog->id, $this->type, $msgData, $user->userid); - if (Base::isSuccess($res)) { - $msgs[] = $res['data']; - } - if ($leaveMessage) { - $res = self::sendMsg(null, $dialog->id, 'text', ['text' => $leaveMessage], $user->userid); - if (Base::isSuccess($res)) { - $msgs[] = $res['data']; - } - } + if ($dialog) { + $dialogs[$dialog->id] = $dialog; + } + } + } + if ($dialogids) { + if (!is_array($dialogids)) { + $dialogids = [$dialogids]; + } + foreach ($dialogids as $dialogid) { + if (isset($dialogs[$dialogid])) { + continue; + } + $dialog = WebSocketDialog::find($dialogid); + if ($dialog) { + $dialogs[$dialog->id] = $dialog; + } + } + } + foreach ($dialogs as $dialog) { + $res = self::sendMsg('forward-' . $forwardId, $dialog->id, $this->type, $msgData, $user->userid); + if (Base::isSuccess($res)) { + $msgs[] = $res['data']; + } + if ($leaveMessage) { + $action = $dialog->isAiDialog() ? "reply-{$res['data']['id']}" : null; + $res = self::sendMsg($action, $dialog->id, 'text', ['text' => $leaveMessage], $user->userid); + if (Base::isSuccess($res)) { + $msgs[] = $res['data']; } } } diff --git a/app/Tasks/BotReceiveMsgTask.php b/app/Tasks/BotReceiveMsgTask.php index 66b7567e0..89ad8907d 100644 --- a/app/Tasks/BotReceiveMsgTask.php +++ b/app/Tasks/BotReceiveMsgTask.php @@ -408,6 +408,10 @@ class BotReceiveMsgTask extends AbstractTask $errorContent = null; if ($botUser->isAiBot($type)) { // AI机器人 + if (Base::val($msg->msg, 'forward_data.leave')) { + // AI机器人不处理带有留言的转发消息,因为他要处理那条留言消息 + return; + } $setting = Base::setting('aibotSetting'); $extras = [ 'model_type' => match ($type) {