From feeeb26d9476cf346d2a5c513d32ce65ea9b1b59 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Tue, 23 Sep 2025 19:39:13 +0800 Subject: [PATCH] no message --- app/Http/Controllers/Api/DialogController.php | 4 +- app/Models/WebSocketDialogMsg.php | 105 +++++++++++++++++ app/Tasks/BotReceiveMsgTask.php | 108 +----------------- 3 files changed, 111 insertions(+), 106 deletions(-) diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index e6f559d2c..e2c96fe49 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -1061,14 +1061,14 @@ class DialogController extends AbstractController $recentMessages = WebSocketDialogMsg::whereDialogId($dialog_id) ->orderByDesc('id') - ->take(6) + ->take(15) ->with('user') ->get(); if ($recentMessages->isNotEmpty()) { $context['recent_messages'] = $recentMessages->reverse()->map(function ($msg) { return [ 'sender' => $msg->user->nickname ?? ('用户' . $msg->userid), - 'summary' => WebSocketDialogMsg::previewMsg($msg), + 'summary' => $msg->extractMessageContent(300), ]; })->filter(function ($item) { return !empty($item['summary']); diff --git a/app/Models/WebSocketDialogMsg.php b/app/Models/WebSocketDialogMsg.php index 440214fbd..868531d74 100644 --- a/app/Models/WebSocketDialogMsg.php +++ b/app/Models/WebSocketDialogMsg.php @@ -851,6 +851,111 @@ class WebSocketDialogMsg extends AbstractModel return $msg; } + /** + * 提取消息内容 + * 根据消息类型(文件、文本等)提取相应的内容文本 + * + * @param int $maxLength 最大长度,超过则截取,0表示不限制 + * @return string 提取出的消息文本内容 + */ + public function extractMessageContent(int $maxLength = 0): string + { + $reserves = []; + switch ($this->type) { + case "file": + // 提取文件消息 + $msgData = Base::json2array($this->getRawOriginal('msg')); + $result = $this->convertMentionFormat("path", $msgData['path'], $msgData['name'], $reserves); + break; + + case "text": + // 提取文本消息 + $result = $this->msg['text'] ?: ''; + if (empty($result)) { + return ''; + } + + // 提取快捷键 + if (preg_match("/]*?data-quick-key=([\"'])([^\"']+?)\\1[^>]*?>(.*?)<\/span>/is", $result, $match)) { + $command = $match[2] ?? ''; + $command = preg_replace("/^%3A\.?/", ":", $command); + $command = trim($command); + if ($command) { + return $command; + } + } + + // 提及任务、文件、报告 + $result = preg_replace_callback_array([ + // 用户 + "/(.*?)<\/span>/" => function () { + return ""; + }, + + // 任务 + "/#?(.*?)<\/span>/" => function ($match) use (&$reserves) { + return $this->convertMentionFormat("task", $match[1], $match[2], $reserves); + }, + + // 文件 + "/]*?>~?(.*?)<\/a>/" => function ($match) use (&$reserves) { + if (preg_match("/single\/file\/(.*?)$/", $match[1], $subMatch)) { + return $this->convertMentionFormat("file", $subMatch[1], $match[2], $reserves); + } + return ""; + }, + + // 报告 + "/]*?>%?(.*?)<\/a>/" => function ($match) use (&$reserves) { + if (preg_match("/single\/report\/detail\/(.*?)$/", $match[1], $subMatch)) { + return $this->convertMentionFormat("report", $subMatch[1], $match[2], $reserves); + } + return ""; + }, + ], $result); + + // 转成 markdown + if ($this->msg['type'] !== 'md') { + $result = Base::html2markdown($result); + } + break; + + default: + // 其他类型消息不处理 + return ''; + } + + // 处理 reserves + foreach ($reserves as $rand => $mention) { + $result = str_replace($rand, $mention, $result); + } + + // 截取最大长度 + if ($maxLength > 0 && mb_strlen($result) > $maxLength) { + $result = mb_substr($result, 0, $maxLength); + } + + return $result; + } + + /** + * 转换提及消息格式 + * 将提及的任务、文件、报告等转换为统一的格式 [type#key#name] + * + * @param string $type 提及类型(task、file、report、path) + * @param string $key 提及对象的唯一标识 + * @param string $name 提及对象的显示名称 + * @return string 格式化后的提及字符串 + */ + private function convertMentionFormat($type, $key, $name, &$reserves) + { + $key = str_replace(['#', '-->'], '', $key); + $name = str_replace(['#', '-->'], '', $name); + $rand = Base::generatePassword(12); + $reserves[$rand] = ""; + return $rand; + } + /** * 处理文本消息内容,用于发送前 * @param $text diff --git a/app/Tasks/BotReceiveMsgTask.php b/app/Tasks/BotReceiveMsgTask.php index 61ef3c5e2..4e0012856 100644 --- a/app/Tasks/BotReceiveMsgTask.php +++ b/app/Tasks/BotReceiveMsgTask.php @@ -117,10 +117,10 @@ class BotReceiveMsgTask extends AbstractTask } // 提取指令 - $sendText = $this->extractMessageContent($msg); + $sendText = $msg->extractMessageContent(); $replyText = null; if ($msg->reply_id && $replyMsg = WebSocketDialogMsg::find($msg->reply_id)) { - $replyText = $this->extractMessageContent($replyMsg); + $replyText = $replyMsg->extractMessageContent(); } // 没有提取到指令,则不处理 @@ -594,106 +594,6 @@ class BotReceiveMsgTask extends AbstractTask } } - /** - * 提取消息内容 - * 根据消息类型(文件、文本等)提取相应的内容文本 - * - * @param WebSocketDialogMsg $msg 消息对象 - * @return string 提取出的消息文本内容 - */ - private function extractMessageContent(WebSocketDialogMsg $msg) - { - $reserves = []; - switch ($msg->type) { - case "file": - // 提取文件消息 - $msgData = Base::json2array($msg->getRawOriginal('msg')); - $result = $this->convertMentionFormat("path", $msgData['path'], $msgData['name'], $reserves); - break; - - case "text": - // 提取文本消息 - $result = $msg->msg['text'] ?: ''; - if (empty($result)) { - return ''; - } - - // 提取快捷键 - if (preg_match("/]*?data-quick-key=([\"'])([^\"']+?)\\1[^>]*?>(.*?)<\/span>/is", $result, $match)) { - $command = $match[2] ?? ''; - $command = preg_replace("/^%3A\.?/", ":", $command); - $command = trim($command); - if ($command) { - return $command; - } - } - - // 提及任务、文件、报告 - $result = preg_replace_callback_array([ - // 用户 - "/(.*?)<\/span>/" => function () { - return ""; - }, - - // 任务 - "/#?(.*?)<\/span>/" => function ($match) use (&$reserves) { - return $this->convertMentionFormat("task", $match[1], $match[2], $reserves); - }, - - // 文件 - "/]*?>~?(.*?)<\/a>/" => function ($match) use (&$reserves) { - if (preg_match("/single\/file\/(.*?)$/", $match[1], $subMatch)) { - return $this->convertMentionFormat("file", $subMatch[1], $match[2], $reserves); - } - return ""; - }, - - // 报告 - "/]*?>%?(.*?)<\/a>/" => function ($match) use (&$reserves) { - if (preg_match("/single\/report\/detail\/(.*?)$/", $match[1], $subMatch)) { - return $this->convertMentionFormat("report", $subMatch[1], $match[2], $reserves); - } - return ""; - }, - ], $result); - - // 转成 markdown - if ($msg->msg['type'] !== 'md') { - $result = Base::html2markdown($result); - } - break; - - default: - // 其他类型消息不处理 - return ''; - } - - // 处理 reserves - foreach ($reserves as $rand => $mention) { - $result = str_replace($rand, $mention, $result); - } - - return $result; - } - - /** - * 转换提及消息格式 - * 将提及的任务、文件、报告等转换为统一的格式 [type#key#name] - * - * @param string $type 提及类型(task、file、report、path) - * @param string $key 提及对象的唯一标识 - * @param string $name 提及对象的显示名称 - * @return string 格式化后的提及字符串 - */ - private function convertMentionFormat($type, $key, $name, &$reserves) - { - $key = str_replace(['#', '-->'], '', $key); - $name = str_replace(['#', '-->'], '', $name); - $rand = Base::generatePassword(12); - $reserves[$rand] = ""; - return $rand; - } - /** * 为AI机器人转换提及消息格式 * 将提及的任务、文件、报告转换为AI可理解的格式,并提取相关内容 @@ -912,7 +812,7 @@ class BotReceiveMsgTask extends AbstractTask // 聊天历史 if ($dialog->type === 'group') { - $chatHistory = $this->getRecentChatHistory($dialog, 10); + $chatHistory = $this->getRecentChatHistory($dialog, 15); if ($chatHistory) { $sections[] = << @@ -974,7 +874,7 @@ class BotReceiveMsgTask extends AbstractTask ->get() ->map(function (WebSocketDialogMsg $message) { $userName = $message->user?->nickname ?? '未知用户'; - $content = $this->extractMessageContent($message); + $content = $message->extractMessageContent(500); if (!$content) { return null; }