feat: PFC谈话模式,可选择启用,实验性功能
This commit is contained in:
69
src/plugins/chat_module/only_process/only_message_process.py
Normal file
69
src/plugins/chat_module/only_process/only_message_process.py
Normal file
@@ -0,0 +1,69 @@
|
||||
from typing import Optional
|
||||
from src.common.logger import get_module_logger
|
||||
from src.plugins.chat.message import MessageRecv
|
||||
from src.plugins.chat.chat_stream import chat_manager
|
||||
from src.plugins.storage.storage import MessageStorage
|
||||
from src.plugins.config.config import global_config
|
||||
import re
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
|
||||
logger = get_module_logger("pfc_message_processor")
|
||||
|
||||
class MessageProcessor:
|
||||
"""消息处理器,负责处理接收到的消息并存储"""
|
||||
|
||||
def __init__(self):
|
||||
self.storage = MessageStorage()
|
||||
|
||||
def _check_ban_words(self, text: str, chat, userinfo) -> bool:
|
||||
"""检查消息中是否包含过滤词"""
|
||||
for word in global_config.ban_words:
|
||||
if word in text:
|
||||
logger.info(
|
||||
f"[{chat.group_info.group_name if chat.group_info else '私聊'}]{userinfo.user_nickname}:{text}"
|
||||
)
|
||||
logger.info(f"[过滤词识别]消息中含有{word},filtered")
|
||||
return True
|
||||
return False
|
||||
|
||||
def _check_ban_regex(self, text: str, chat, userinfo) -> bool:
|
||||
"""检查消息是否匹配过滤正则表达式"""
|
||||
for pattern in global_config.ban_msgs_regex:
|
||||
if re.search(pattern, text):
|
||||
logger.info(
|
||||
f"[{chat.group_info.group_name if chat.group_info else '私聊'}]{userinfo.user_nickname}:{text}"
|
||||
)
|
||||
logger.info(f"[正则表达式过滤]消息匹配到{pattern},filtered")
|
||||
return True
|
||||
return False
|
||||
|
||||
async def process_message(self, message: MessageRecv) -> None:
|
||||
"""处理消息并存储
|
||||
|
||||
Args:
|
||||
message: 消息对象
|
||||
"""
|
||||
userinfo = message.message_info.user_info
|
||||
chat = message.chat_stream
|
||||
|
||||
# 处理消息
|
||||
await message.process()
|
||||
|
||||
# 过滤词/正则表达式过滤
|
||||
if self._check_ban_words(message.processed_plain_text, chat, userinfo) or self._check_ban_regex(
|
||||
message.raw_message, chat, userinfo
|
||||
):
|
||||
return
|
||||
|
||||
# 存储消息
|
||||
await self.storage.store_message(message, chat)
|
||||
|
||||
# 打印消息信息
|
||||
mes_name = chat.group_info.group_name if chat.group_info else "私聊"
|
||||
# 将时间戳转换为datetime对象
|
||||
current_time = datetime.fromtimestamp(message.message_info.time).strftime("%H:%M:%S")
|
||||
logger.info(
|
||||
f"[{current_time}][{mes_name}]"
|
||||
f"{chat.user_info.user_nickname}: {message.processed_plain_text}"
|
||||
)
|
||||
@@ -133,11 +133,6 @@ class ReasoningChat:
|
||||
userinfo = message.message_info.user_info
|
||||
messageinfo = message.message_info
|
||||
|
||||
|
||||
if groupinfo == None and global_config.enable_friend_chat:#如果是私聊
|
||||
pass
|
||||
elif groupinfo.group_id not in global_config.talk_allowed_groups:
|
||||
return
|
||||
|
||||
# logger.info("使用推理聊天模式")
|
||||
|
||||
|
||||
@@ -145,10 +145,6 @@ class ThinkFlowChat:
|
||||
userinfo = message.message_info.user_info
|
||||
messageinfo = message.message_info
|
||||
|
||||
if groupinfo == None and global_config.enable_friend_chat:#如果是私聊
|
||||
pass
|
||||
elif groupinfo.group_id not in global_config.talk_allowed_groups:
|
||||
return
|
||||
|
||||
# 创建聊天流
|
||||
chat = await chat_manager.get_or_create_stream(
|
||||
@@ -178,16 +174,15 @@ class ThinkFlowChat:
|
||||
)
|
||||
timer2 = time.time()
|
||||
timing_results["记忆激活"] = timer2 - timer1
|
||||
logger.debug(f"记忆激活: {interested_rate}")
|
||||
|
||||
is_mentioned = is_mentioned_bot_in_message(message)
|
||||
|
||||
# 计算回复意愿
|
||||
if global_config.enable_think_flow:
|
||||
current_willing_old = willing_manager.get_willing(chat_stream=chat)
|
||||
current_willing_new = (heartflow.get_subheartflow(chat.stream_id).current_state.willing - 5) / 4
|
||||
current_willing = (current_willing_old + current_willing_new) / 2
|
||||
else:
|
||||
current_willing = willing_manager.get_willing(chat_stream=chat)
|
||||
current_willing_old = willing_manager.get_willing(chat_stream=chat)
|
||||
current_willing_new = (heartflow.get_subheartflow(chat.stream_id).current_state.willing - 5) / 4
|
||||
current_willing = (current_willing_old + current_willing_new) / 2
|
||||
|
||||
|
||||
willing_manager.set_willing(chat.stream_id, current_willing)
|
||||
|
||||
@@ -203,6 +198,7 @@ class ThinkFlowChat:
|
||||
)
|
||||
timer2 = time.time()
|
||||
timing_results["意愿激活"] = timer2 - timer1
|
||||
logger.debug(f"意愿激活: {reply_probability}")
|
||||
|
||||
# 打印消息信息
|
||||
mes_name = chat.group_info.group_name if chat.group_info else "私聊"
|
||||
|
||||
Reference in New Issue
Block a user