新增只读群组功能,并且避免为只读群组创建和维护子心流,减少资源消耗

pull/686/head
shiinawhitee 2025-04-06 04:00:02 +08:00
parent 0acad09dd7
commit 827945e604
4 changed files with 52 additions and 7 deletions

View File

@ -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)

View File

@ -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}")

View File

@ -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}")

View File

@ -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]