feat:将是否拦截改为拦截登记

pull/1414/head
SengokuCola 2025-12-03 09:52:23 +08:00
parent f85c5e9b5f
commit 056a909c77
17 changed files with 47 additions and 46 deletions

View File

@ -165,7 +165,7 @@ class BrainChatting:
limit_mode="latest", limit_mode="latest",
filter_mai=True, filter_mai=True,
filter_command=False, filter_command=False,
filter_no_read_command=True, filter_intercept_message_level=1,
) )
if len(recent_messages_list) >= 1: if len(recent_messages_list) >= 1:
@ -279,7 +279,7 @@ class BrainChatting:
chat_id=self.stream_id, chat_id=self.stream_id,
timestamp=time.time(), timestamp=time.time(),
limit=int(global_config.chat.max_context_size * 0.6), limit=int(global_config.chat.max_context_size * 0.6),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
chat_content_block, message_id_list = build_readable_messages_with_id( chat_content_block, message_id_list = build_readable_messages_with_id(
messages=message_list_before_now, messages=message_list_before_now,

View File

@ -226,7 +226,7 @@ class BrainPlanner:
chat_id=self.chat_id, chat_id=self.chat_id,
timestamp=time.time(), timestamp=time.time(),
limit=int(global_config.chat.max_context_size * 0.6), limit=int(global_config.chat.max_context_size * 0.6),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
message_id_list: list[Tuple[str, "DatabaseMessages"]] = [] message_id_list: list[Tuple[str, "DatabaseMessages"]] = []
chat_content_block, message_id_list = build_readable_messages_with_id( chat_content_block, message_id_list = build_readable_messages_with_id(

View File

@ -190,7 +190,7 @@ class HeartFChatting:
limit_mode="latest", limit_mode="latest",
filter_mai=True, filter_mai=True,
filter_command=False, filter_command=False,
filter_no_read_command=True, filter_intercept_message_level=0,
) )
# 根据连续 no_reply 次数动态调整阈值 # 根据连续 no_reply 次数动态调整阈值
@ -485,7 +485,7 @@ class HeartFChatting:
chat_id=self.stream_id, chat_id=self.stream_id,
timestamp=time.time(), timestamp=time.time(),
limit=int(global_config.chat.max_context_size * 0.6), limit=int(global_config.chat.max_context_size * 0.6),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
chat_content_block, message_id_list = build_readable_messages_with_id( chat_content_block, message_id_list = build_readable_messages_with_id(
messages=message_list_before_now, messages=message_list_before_now,

View File

@ -83,7 +83,7 @@ class ChatBot:
self._started = True self._started = True
async def _process_commands_with_new_system(self, message: MessageRecv): async def _process_commands(self, message: MessageRecv):
# sourcery skip: use-named-expression # sourcery skip: use-named-expression
"""使用新插件系统处理命令""" """使用新插件系统处理命令"""
try: try:
@ -115,17 +115,17 @@ class ChatBot:
try: try:
# 执行命令 # 执行命令
success, response, intercept_message = await command_instance.execute() success, response, intercept_message_level = await command_instance.execute()
message.is_no_read_command = bool(intercept_message) message.intercept_message_level = intercept_message_level
# 记录命令执行结果 # 记录命令执行结果
if success: if success:
logger.info(f"命令执行成功: {command_class.__name__} (拦截: {intercept_message})") logger.info(f"命令执行成功: {command_class.__name__} (拦截等级: {intercept_message_level})")
else: else:
logger.warning(f"命令执行失败: {command_class.__name__} - {response}") logger.warning(f"命令执行失败: {command_class.__name__} - {response}")
# 根据命令的拦截设置决定是否继续处理消息 # 根据命令的拦截设置决定是否继续处理消息
return True, response, not intercept_message # 找到命令根据intercept_message决定是否继续 return True, response, not bool(intercept_message_level) # 找到命令根据intercept_message决定是否继续
except Exception as e: except Exception as e:
logger.error(f"执行命令时出错: {command_class.__name__} - {e}") logger.error(f"执行命令时出错: {command_class.__name__} - {e}")
@ -295,7 +295,7 @@ class ChatBot:
# return # return
# 命令处理 - 使用新插件系统检查并处理命令 # 命令处理 - 使用新插件系统检查并处理命令
is_command, cmd_result, continue_process = await self._process_commands_with_new_system(message) is_command, cmd_result, continue_process = await self._process_commands(message)
# 如果是命令且不需要继续处理,则直接返回 # 如果是命令且不需要继续处理,则直接返回
if is_command and not continue_process: if is_command and not continue_process:

View File

@ -122,7 +122,7 @@ class MessageRecv(Message):
self.is_notify = False self.is_notify = False
self.is_command = False self.is_command = False
self.is_no_read_command = False self.intercept_message_level = 0
self.priority_mode = "interest" self.priority_mode = "interest"
self.priority_info = None self.priority_info = None

View File

@ -72,7 +72,7 @@ class MessageStorage:
key_words = "" key_words = ""
key_words_lite = "" key_words_lite = ""
selected_expressions = message.selected_expressions selected_expressions = message.selected_expressions
is_no_read_command = False intercept_message_level = 0
else: else:
filtered_display_message = "" filtered_display_message = ""
interest_value = message.interest_value interest_value = message.interest_value
@ -86,7 +86,7 @@ class MessageStorage:
is_picid = message.is_picid is_picid = message.is_picid
is_notify = message.is_notify is_notify = message.is_notify
is_command = message.is_command is_command = message.is_command
is_no_read_command = getattr(message, "is_no_read_command", False) intercept_message_level = getattr(message, "intercept_message_level", 0)
# 序列化关键词列表为JSON字符串 # 序列化关键词列表为JSON字符串
key_words = MessageStorage._serialize_keywords(message.key_words) key_words = MessageStorage._serialize_keywords(message.key_words)
key_words_lite = MessageStorage._serialize_keywords(message.key_words_lite) key_words_lite = MessageStorage._serialize_keywords(message.key_words_lite)
@ -138,7 +138,7 @@ class MessageStorage:
is_picid=is_picid, is_picid=is_picid,
is_notify=is_notify, is_notify=is_notify,
is_command=is_command, is_command=is_command,
is_no_read_command=is_no_read_command, intercept_message_level=intercept_message_level,
key_words=key_words, key_words=key_words,
key_words_lite=key_words_lite, key_words_lite=key_words_lite,
selected_expressions=selected_expressions, selected_expressions=selected_expressions,

View File

@ -69,7 +69,7 @@ class ActionModifier:
chat_id=self.chat_stream.stream_id, chat_id=self.chat_stream.stream_id,
timestamp=time.time(), timestamp=time.time(),
limit=min(int(global_config.chat.max_context_size * 0.33), 10), limit=min(int(global_config.chat.max_context_size * 0.33), 10),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
chat_content = build_readable_messages( chat_content = build_readable_messages(

View File

@ -316,7 +316,7 @@ class ActionPlanner:
chat_id=self.chat_id, chat_id=self.chat_id,
timestamp=time.time(), timestamp=time.time(),
limit=int(global_config.chat.max_context_size * 0.6), limit=int(global_config.chat.max_context_size * 0.6),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
message_id_list: list[Tuple[str, "DatabaseMessages"]] = [] message_id_list: list[Tuple[str, "DatabaseMessages"]] = []
chat_content_block, message_id_list = build_readable_messages_with_id( chat_content_block, message_id_list = build_readable_messages_with_id(

View File

@ -751,14 +751,14 @@ class DefaultReplyer:
chat_id=chat_id, chat_id=chat_id,
timestamp=reply_time_point, timestamp=reply_time_point,
limit=global_config.chat.max_context_size * 1, limit=global_config.chat.max_context_size * 1,
filter_no_read_command=True, filter_intercept_message_level=1,
) )
message_list_before_short = get_raw_msg_before_timestamp_with_chat( message_list_before_short = get_raw_msg_before_timestamp_with_chat(
chat_id=chat_id, chat_id=chat_id,
timestamp=reply_time_point, timestamp=reply_time_point,
limit=int(global_config.chat.max_context_size * 0.33), limit=int(global_config.chat.max_context_size * 0.33),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
person_list_short: List[Person] = [] person_list_short: List[Person] = []
@ -941,7 +941,7 @@ class DefaultReplyer:
chat_id=chat_id, chat_id=chat_id,
timestamp=time.time(), timestamp=time.time(),
limit=min(int(global_config.chat.max_context_size * 0.33), 15), limit=min(int(global_config.chat.max_context_size * 0.33), 15),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
chat_talking_prompt_half = build_readable_messages( chat_talking_prompt_half = build_readable_messages(
message_list_before_now_half, message_list_before_now_half,

View File

@ -663,7 +663,7 @@ class PrivateReplyer:
chat_id=chat_id, chat_id=chat_id,
timestamp=time.time(), timestamp=time.time(),
limit=global_config.chat.max_context_size, limit=global_config.chat.max_context_size,
filter_no_read_command=True, filter_intercept_message_level=1,
) )
dialogue_prompt = build_readable_messages( dialogue_prompt = build_readable_messages(
@ -678,7 +678,7 @@ class PrivateReplyer:
chat_id=chat_id, chat_id=chat_id,
timestamp=time.time(), timestamp=time.time(),
limit=int(global_config.chat.max_context_size * 0.33), limit=int(global_config.chat.max_context_size * 0.33),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
person_list_short: List[Person] = [] person_list_short: List[Person] = []
@ -878,7 +878,7 @@ class PrivateReplyer:
chat_id=chat_id, chat_id=chat_id,
timestamp=time.time(), timestamp=time.time(),
limit=min(int(global_config.chat.max_context_size * 0.33), 15), limit=min(int(global_config.chat.max_context_size * 0.33), 15),
filter_no_read_command=True, filter_intercept_message_level=1,
) )
chat_talking_prompt_half = build_readable_messages( chat_talking_prompt_half = build_readable_messages(
message_list_before_now_half, message_list_before_now_half,

View File

@ -120,7 +120,7 @@ def get_raw_msg_by_timestamp_with_chat(
limit_mode: str = "latest", limit_mode: str = "latest",
filter_bot=False, filter_bot=False,
filter_command=False, filter_command=False,
filter_no_read_command=False, filter_intercept_message_level: Optional[int] = None,
) -> List[DatabaseMessages]: ) -> List[DatabaseMessages]:
"""获取在特定聊天从指定时间戳到指定时间戳的消息,按时间升序排序,返回消息列表 """获取在特定聊天从指定时间戳到指定时间戳的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制 limit: 限制返回的消息数量0为不限制
@ -138,7 +138,7 @@ def get_raw_msg_by_timestamp_with_chat(
limit_mode=limit_mode, limit_mode=limit_mode,
filter_bot=filter_bot, filter_bot=filter_bot,
filter_command=filter_command, filter_command=filter_command,
filter_no_read_command=filter_no_read_command, filter_intercept_message_level=filter_intercept_message_level,
) )
@ -150,7 +150,7 @@ def get_raw_msg_by_timestamp_with_chat_inclusive(
limit_mode: str = "latest", limit_mode: str = "latest",
filter_bot=False, filter_bot=False,
filter_command=False, filter_command=False,
filter_no_read_command=False, filter_intercept_message_level: Optional[int] = None,
) -> List[DatabaseMessages]: ) -> List[DatabaseMessages]:
"""获取在特定聊天从指定时间戳到指定时间戳的消息(包含边界),按时间升序排序,返回消息列表 """获取在特定聊天从指定时间戳到指定时间戳的消息(包含边界),按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制 limit: 限制返回的消息数量0为不限制
@ -167,7 +167,7 @@ def get_raw_msg_by_timestamp_with_chat_inclusive(
limit_mode=limit_mode, limit_mode=limit_mode,
filter_bot=filter_bot, filter_bot=filter_bot,
filter_command=filter_command, filter_command=filter_command,
filter_no_read_command=filter_no_read_command, filter_intercept_message_level=filter_intercept_message_level,
) )
@ -303,7 +303,7 @@ def get_raw_msg_before_timestamp(timestamp: float, limit: int = 0) -> List[Datab
def get_raw_msg_before_timestamp_with_chat( def get_raw_msg_before_timestamp_with_chat(
chat_id: str, timestamp: float, limit: int = 0, filter_no_read_command: bool = False chat_id: str, timestamp: float, limit: int = 0, filter_intercept_message_level: Optional[int] = None
) -> List[DatabaseMessages]: ) -> List[DatabaseMessages]:
"""获取指定时间戳之前的消息,按时间升序排序,返回消息列表 """获取指定时间戳之前的消息,按时间升序排序,返回消息列表
limit: 限制返回的消息数量0为不限制 limit: 限制返回的消息数量0为不限制
@ -311,7 +311,7 @@ def get_raw_msg_before_timestamp_with_chat(
filter_query = {"chat_id": chat_id, "time": {"$lt": timestamp}} filter_query = {"chat_id": chat_id, "time": {"$lt": timestamp}}
sort_order = [("time", 1)] sort_order = [("time", 1)]
return find_messages( return find_messages(
message_filter=filter_query, sort=sort_order, limit=limit, filter_no_read_command=filter_no_read_command message_filter=filter_query, sort=sort_order, limit=limit, filter_intercept_message_level=filter_intercept_message_level
) )

View File

@ -77,7 +77,7 @@ class DatabaseMessages(BaseDataModel):
is_emoji: bool = False, is_emoji: bool = False,
is_picid: bool = False, is_picid: bool = False,
is_command: bool = False, is_command: bool = False,
is_no_read_command: bool = False, intercept_message_level: int = 0,
is_notify: bool = False, is_notify: bool = False,
selected_expressions: Optional[str] = None, selected_expressions: Optional[str] = None,
user_id: str = "", user_id: str = "",
@ -120,7 +120,7 @@ class DatabaseMessages(BaseDataModel):
self.is_emoji = is_emoji self.is_emoji = is_emoji
self.is_picid = is_picid self.is_picid = is_picid
self.is_command = is_command self.is_command = is_command
self.is_no_read_command = is_no_read_command self.intercept_message_level = intercept_message_level
self.is_notify = is_notify self.is_notify = is_notify
self.selected_expressions = selected_expressions self.selected_expressions = selected_expressions
@ -188,7 +188,7 @@ class DatabaseMessages(BaseDataModel):
"is_emoji": self.is_emoji, "is_emoji": self.is_emoji,
"is_picid": self.is_picid, "is_picid": self.is_picid,
"is_command": self.is_command, "is_command": self.is_command,
"is_no_read_command": self.is_no_read_command, "intercept_message_level": self.intercept_message_level,
"is_notify": self.is_notify, "is_notify": self.is_notify,
"selected_expressions": self.selected_expressions, "selected_expressions": self.selected_expressions,
"user_id": self.user_info.user_id, "user_id": self.user_info.user_id,

View File

@ -22,7 +22,7 @@ class MessageAndActionModel(BaseDataModel):
is_action_record: bool = field(default=False) is_action_record: bool = field(default=False)
action_name: Optional[str] = None action_name: Optional[str] = None
is_command: bool = field(default=False) is_command: bool = field(default=False)
is_no_read_command: bool = field(default=False) intercept_message_level: int = field(default=0)
@classmethod @classmethod
def from_DatabaseMessages(cls, message: "DatabaseMessages"): def from_DatabaseMessages(cls, message: "DatabaseMessages"):
@ -37,7 +37,7 @@ class MessageAndActionModel(BaseDataModel):
display_message=message.display_message, display_message=message.display_message,
chat_info_platform=message.chat_info.platform, chat_info_platform=message.chat_info.platform,
is_command=message.is_command, is_command=message.is_command,
is_no_read_command=getattr(message, "is_no_read_command", False), intercept_message_level=getattr(message, "intercept_message_level", 0),
) )

View File

@ -170,7 +170,7 @@ class Messages(BaseModel):
is_emoji = BooleanField(default=False) is_emoji = BooleanField(default=False)
is_picid = BooleanField(default=False) is_picid = BooleanField(default=False)
is_command = BooleanField(default=False) is_command = BooleanField(default=False)
is_no_read_command = BooleanField(default=False) intercept_message_level = IntegerField(default=0)
is_notify = BooleanField(default=False) is_notify = BooleanField(default=False)
selected_expressions = TextField(null=True) selected_expressions = TextField(null=True)

View File

@ -25,7 +25,7 @@ def find_messages(
limit_mode: str = "latest", limit_mode: str = "latest",
filter_bot=False, filter_bot=False,
filter_command=False, filter_command=False,
filter_no_read_command=False, filter_intercept_message_level: Optional[int] = None,
) -> List[DatabaseMessages]: ) -> List[DatabaseMessages]:
""" """
根据提供的过滤器排序和限制条件查找消息 根据提供的过滤器排序和限制条件查找消息
@ -85,8 +85,9 @@ def find_messages(
# 使用按位取反构造 Peewee 的 NOT 条件,避免直接与 False 比较 # 使用按位取反构造 Peewee 的 NOT 条件,避免直接与 False 比较
query = query.where(~Messages.is_command) query = query.where(~Messages.is_command)
if filter_no_read_command: if filter_intercept_message_level is not None:
query = query.where(~Messages.is_no_read_command) # 过滤掉所有 intercept_message_level > filter_intercept_message_level 的消息
query = query.where(Messages.intercept_message_level <= filter_intercept_message_level)
if limit > 0: if limit > 0:
if limit_mode == "earliest": if limit_mode == "earliest":

View File

@ -72,7 +72,7 @@ def get_messages_by_time_in_chat(
limit_mode: str = "latest", limit_mode: str = "latest",
filter_mai: bool = False, filter_mai: bool = False,
filter_command: bool = False, filter_command: bool = False,
filter_no_read_command: bool = False, filter_intercept_message_level: Optional[int] = None,
) -> List[DatabaseMessages]: ) -> List[DatabaseMessages]:
""" """
获取指定聊天中指定时间范围内的消息 获取指定聊天中指定时间范围内的消息
@ -111,7 +111,7 @@ def get_messages_by_time_in_chat(
limit_mode=limit_mode, limit_mode=limit_mode,
filter_bot=filter_mai, filter_bot=filter_mai,
filter_command=filter_command, filter_command=filter_command,
filter_no_read_command=filter_no_read_command, filter_intercept_message_level=filter_intercept_message_level,
) )
@ -123,7 +123,7 @@ def get_messages_by_time_in_chat_inclusive(
limit_mode: str = "latest", limit_mode: str = "latest",
filter_mai: bool = False, filter_mai: bool = False,
filter_command: bool = False, filter_command: bool = False,
filter_no_read_command: bool = False, filter_intercept_message_level: Optional[int] = None,
) -> List[DatabaseMessages]: ) -> List[DatabaseMessages]:
""" """
获取指定聊天中指定时间范围内的消息包含边界 获取指定聊天中指定时间范围内的消息包含边界
@ -158,7 +158,7 @@ def get_messages_by_time_in_chat_inclusive(
limit_mode=limit_mode, limit_mode=limit_mode,
filter_bot=filter_mai, filter_bot=filter_mai,
filter_command=filter_command, filter_command=filter_command,
filter_no_read_command=filter_no_read_command, filter_intercept_message_level=filter_intercept_message_level,
) )
if filter_mai: if filter_mai:
return filter_mai_messages(messages) return filter_mai_messages(messages)
@ -284,7 +284,7 @@ def get_messages_before_time_in_chat(
timestamp: float, timestamp: float,
limit: int = 0, limit: int = 0,
filter_mai: bool = False, filter_mai: bool = False,
filter_no_read_command: bool = False, filter_intercept_message_level: Optional[int] = None,
) -> List[DatabaseMessages]: ) -> List[DatabaseMessages]:
""" """
获取指定聊天中指定时间戳之前的消息 获取指定聊天中指定时间戳之前的消息
@ -313,7 +313,7 @@ def get_messages_before_time_in_chat(
chat_id=chat_id, chat_id=chat_id,
timestamp=timestamp, timestamp=timestamp,
limit=limit, limit=limit,
filter_no_read_command=filter_no_read_command, filter_intercept_message_level=filter_intercept_message_level,
) )
if filter_mai: if filter_mai:
return filter_mai_messages(messages) return filter_mai_messages(messages)

View File

@ -55,11 +55,11 @@ class BaseCommand(ABC):
self.matched_groups = groups self.matched_groups = groups
@abstractmethod @abstractmethod
async def execute(self) -> Tuple[bool, Optional[str], bool]: async def execute(self) -> Tuple[bool, Optional[str], int]:
"""执行Command的抽象方法子类必须实现 """执行Command的抽象方法子类必须实现
Returns: Returns:
Tuple[bool, Optional[str], bool]: (是否执行成功, 可选的回复消息, 是否拦截消息 不进行 后续处理) Tuple[bool, Optional[str], int]: (是否执行成功, 可选的回复消息, 拦截消息力度0代表不拦截1代表仅不触发回复replyer可见2代表不触发回复replyer不可见)
""" """
pass pass