From db63ebfb6ec53441bc0dcc7fc900b8b8934e3432 Mon Sep 17 00:00:00 2001 From: zihanjian Date: Thu, 25 Sep 2025 18:15:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=99=E6=95=B0=E6=8D=AE=E5=BA=93=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ai_providers/ai_perplexity.py | 2 +- commands/context.py | 7 ++++--- robot.py | 30 ++++++++++++++++-------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/ai_providers/ai_perplexity.py b/ai_providers/ai_perplexity.py index 934814e..5e1de5c 100644 --- a/ai_providers/ai_perplexity.py +++ b/ai_providers/ai_perplexity.py @@ -207,7 +207,7 @@ class PerplexityManager: return False # 发送等待消息 - send_text_func("正在联网查询,请稍候...") + send_text_func("正在联网查询,请稍候...", record_message=False) # 添加线程完成回调,自动清理线程 def thread_finished_callback(): diff --git a/commands/context.py b/commands/context.py index 04ab9e0..0de27be 100644 --- a/commands/context.py +++ b/commands/context.py @@ -63,17 +63,18 @@ class MessageContext: """获取应答接收者ID (群聊返回群ID,私聊返回用户ID)""" return self.msg.roomid if self.is_group else self.msg.sender - def send_text(self, content: str, at_list: str = "") -> bool: + def send_text(self, content: str, at_list: str = "", record_message: bool = True) -> bool: """ 发送文本消息 :param content: 消息内容 :param at_list: 要@的用户列表,多个用逗号分隔 + :param record_message: 是否将消息记录到数据库 :return: 是否发送成功 """ if self.robot and hasattr(self.robot, "sendTextMsg"): receiver = self.get_receiver() try: - self.robot.sendTextMsg(content, receiver, at_list) + self.robot.sendTextMsg(content, receiver, at_list, record_message=record_message) return True except Exception as e: if self.logger: @@ -86,4 +87,4 @@ class MessageContext: self.logger.error("Robot实例不存在或没有sendTextMsg方法") else: print("Robot实例不存在或没有sendTextMsg方法") - return False \ No newline at end of file + return False diff --git a/robot.py b/robot.py index 9e9ad5b..930e51f 100644 --- a/robot.py +++ b/robot.py @@ -270,7 +270,7 @@ class Robot(Job): ) if reasoning_triggered: self.LOG.info("检测到推理模式触发词,跳过AI路由。") - ctx.send_text("正在深度思考,请稍候...") + ctx.send_text("正在深度思考,请稍候...", record_message=False) previous_ctx_chat = ctx.chat reasoning_chat = self._get_reasoning_chat_model() @@ -368,11 +368,12 @@ class Robot(Job): self.wcf.enable_receiving_msg() Thread(target=innerProcessMsg, name="GetMessage", args=(self.wcf,), daemon=True).start() - def sendTextMsg(self, msg: str, receiver: str, at_list: str = "") -> None: + def sendTextMsg(self, msg: str, receiver: str, at_list: str = "", record_message: bool = True) -> None: """ 发送消息并记录 :param msg: 消息字符串 :param receiver: 接收人wxid或者群id :param at_list: 要@的wxid, @所有人的wxid为:notify@all + :param record_message: 是否将本条消息写入消息历史 """ # 延迟和频率限制 (逻辑不变) time.sleep(float(str(time.time()).split('.')[-1][-2:]) / 100.0 + 0.3) @@ -404,18 +405,19 @@ class Robot(Job): self.LOG.info(f"To {receiver}:\n{ats}\n{msg}") self.wcf.send_text(full_msg_content, receiver, at_list) - if self.message_summary: # 检查 message_summary 是否初始化成功 - # 确定机器人的名字 - robot_name = self.allContacts.get(self.wxid, "机器人") - # 使用 self.wxid 作为 sender_wxid - # 注意:这里不生成时间戳,让 record_message 内部生成 - self.message_summary.record_message( - chat_id=receiver, - sender_name=robot_name, - sender_wxid=self.wxid, # 传入机器人自己的 wxid - content=message_to_send - ) - self.LOG.debug(f"已记录机器人发送的消息到 {receiver}") + if self.message_summary: + if record_message: # 仅在需要时记录消息 + # 确定机器人的名字 + robot_name = self.allContacts.get(self.wxid, "机器人") + # 使用 self.wxid 作为 sender_wxid + # 注意:这里不生成时间戳,让 record_message 内部生成 + self.message_summary.record_message( + chat_id=receiver, + sender_name=robot_name, + sender_wxid=self.wxid, # 传入机器人自己的 wxid + content=message_to_send + ) + self.LOG.debug(f"已记录机器人发送的消息到 {receiver}") else: self.LOG.warning("MessageSummary 未初始化,无法记录发送的消息")