写数据库,优化

This commit is contained in:
zihanjian
2025-09-25 18:15:22 +08:00
parent b04258d63a
commit db63ebfb6e
3 changed files with 21 additions and 18 deletions

View File

@@ -207,7 +207,7 @@ class PerplexityManager:
return False
# 发送等待消息
send_text_func("正在联网查询,请稍候...")
send_text_func("正在联网查询,请稍候...", record_message=False)
# 添加线程完成回调,自动清理线程
def thread_finished_callback():

View File

@@ -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
return False

View File

@@ -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 未初始化,无法记录发送的消息")