From 827945e6047bff2ecfc2a7a4a5dc907e40f874dd Mon Sep 17 00:00:00 2001 From: shiinawhitee Date: Sun, 6 Apr 2025 04:00:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=AA=E8=AF=BB=E7=BE=A4?= =?UTF-8?q?=E7=BB=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=B9=B6=E4=B8=94=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E4=B8=BA=E5=8F=AA=E8=AF=BB=E7=BE=A4=E7=BB=84=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=92=8C=E7=BB=B4=E6=8A=A4=E5=AD=90=E5=BF=83=E6=B5=81?= =?UTF-8?q?=EF=BC=8C=E5=87=8F=E5=B0=91=E8=B5=84=E6=BA=90=E6=B6=88=E8=80=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/heart_flow/heartflow.py | 22 ++++++++++++++++++ src/plugins/chat/bot.py | 23 +++++++++++++++---- .../think_flow_chat/think_flow_chat.py | 11 ++++++++- template/bot_config_template.toml | 3 ++- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/heart_flow/heartflow.py b/src/heart_flow/heartflow.py index 2d032638..65b8f043 100644 --- a/src/heart_flow/heartflow.py +++ b/src/heart_flow/heartflow.py @@ -4,6 +4,7 @@ from src.plugins.moods.moods import MoodManager from src.plugins.models.utils_model import LLM_request from src.plugins.config.config import global_config from src.plugins.schedule.schedule_generator import bot_schedule +from src.plugins.chat.chat_stream import chat_manager import asyncio from src.common.logger import get_module_logger, LogConfig, HEARTFLOW_STYLE_CONFIG # noqa: E402 import time @@ -146,6 +147,16 @@ class Heartflow: """ try: + # 检查是否是只读群组的ID + # 正常情况下,只读群组在bot.py中已被过滤,不会执行到heartflow的相关方法 + # 此检查确保即使有代码变更或异常路径,只读群组也不会创建子心流 + chat_stream = chat_manager.get_stream(subheartflow_id) + if chat_stream and chat_stream.group_info and chat_stream.group_info.group_id: + group_id = chat_stream.group_info.group_id + if group_id in global_config.talk_read_only: + logger.info(f"群组 {group_id} 在只读模式中,跳过创建子心流") + return None + if subheartflow_id not in self._subheartflows: logger.debug(f"创建 subheartflow: {subheartflow_id}") subheartflow = SubHeartflow(subheartflow_id) @@ -169,6 +180,17 @@ class Heartflow: def get_subheartflow(self, observe_chat_id): """获取指定ID的SubHeartflow实例""" + # 检查是否是只读群组的ID + # 正常情况下,只读群组在bot.py中已被过滤,不会调用此方法 + # 此检查防止系统在代码变更或异常情况下意外为只读群组创建或获取子心流 + chat_stream = chat_manager.get_stream(observe_chat_id) + if chat_stream and chat_stream.group_info and chat_stream.group_info.group_id: + group_id = chat_stream.group_info.group_id + if group_id in global_config.talk_read_only: + logger.debug(f"群组 {group_id} 在只读模式中,跳过获取子心流") + # 返回None表示这是只读群组 + return None + return self._subheartflows.get(observe_chat_id) diff --git a/src/plugins/chat/bot.py b/src/plugins/chat/bot.py index 32308bfa..c3783bb6 100644 --- a/src/plugins/chat/bot.py +++ b/src/plugins/chat/bot.py @@ -136,12 +136,25 @@ class ChatBot: logger.error(f"未知的回复模式,请检查配置文件!!: {global_config.response_mode}") else: # 群聊处理 if groupinfo.group_id in global_config.talk_allowed_groups: - if global_config.response_mode == "heart_flow": - await self.think_flow_chat.process_message(message_data) - elif global_config.response_mode == "reasoning": - await self.reasoning_chat.process_message(message_data) + # 检查是否在只读取不回复的群组列表中 + if groupinfo.group_id in global_config.talk_read_only: + # 只读取消息,不回复 + logger.info(f"群组 {groupinfo.group_id}({groupinfo.group_name}) 在只读模式中,只读取不回复消息") + # 创建聊天流 + chat = await chat_manager.get_or_create_stream( + platform=message.message_info.platform, + user_info=userinfo, + group_info=groupinfo, + ) + message.update_chat_stream(chat) + await self.only_process_chat.process_message(message) else: - logger.error(f"未知的回复模式,请检查配置文件!!: {global_config.response_mode}") + if global_config.response_mode == "heart_flow": + await self.think_flow_chat.process_message(message_data) + elif global_config.response_mode == "reasoning": + await self.reasoning_chat.process_message(message_data) + else: + logger.error(f"未知的回复模式,请检查配置文件!!: {global_config.response_mode}") except Exception as e: logger.error(f"预处理消息失败: {e}") diff --git a/src/plugins/chat_module/think_flow_chat/think_flow_chat.py b/src/plugins/chat_module/think_flow_chat/think_flow_chat.py index 9c2eebd9..7c7ffef1 100644 --- a/src/plugins/chat_module/think_flow_chat/think_flow_chat.py +++ b/src/plugins/chat_module/think_flow_chat/think_flow_chat.py @@ -176,7 +176,16 @@ class ThinkFlowChat: message.update_chat_stream(chat) # 创建心流与chat的观察 - heartflow.create_subheartflow(chat.stream_id) + sub_heartflow = heartflow.create_subheartflow(chat.stream_id) + + # 如果创建子心流返回None,表示这是只读群组,只读取消息但不回复 + # 正常情况下,只读群组在bot.py中已被过滤,不会执行到这里 + # 此检查防止由于代码路径变更或其他异常情况导致只读群组消息进入思维流处理流程 + if sub_heartflow is None: + logger.info(f"群组 {groupinfo.group_id}({groupinfo.group_name}) 在只读模式中,只读取不回复消息") + await message.process() + await self.storage.store_message(message, chat) + return await message.process() logger.debug(f"消息处理成功{message.processed_plain_text}") diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index d7ec90cd..4b1d9ac3 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "1.2.4" +version = "1.2.5" #以下是给开发人员阅读的,一般用户不需要阅读 @@ -31,6 +31,7 @@ talk_allowed = [ 123, ] #可以回复消息的群号码 talk_frequency_down = [] #降低回复频率的群号码 +talk_read_only = [] #只读取消息但不回复的群号码(心流) ban_user_id = [] #禁止回复和读取消息的QQ号 [personality]