This commit is contained in:
SengokuCola
2025-04-26 21:16:27 +08:00
10 changed files with 93 additions and 107 deletions

View File

@@ -180,6 +180,10 @@ MaiCore是一个开源项目我们非常欢迎你的参与。你的贡献
- [NapCat](https://github.com/NapNeko/NapCatQQ): 现代化的基于 NTQQ 的 Bot 协议端实现 - [NapCat](https://github.com/NapNeko/NapCatQQ): 现代化的基于 NTQQ 的 Bot 协议端实现
## 麦麦仓库状态
![Alt](https://repobeats.axiom.co/api/embed/9faca9fccfc467931b87dd357b60c6362b5cfae0.svg "Repobeats analytics image")
### 贡献者 ### 贡献者
感谢各位大佬! 感谢各位大佬!

View File

@@ -478,8 +478,8 @@ class SubHeartflowManager:
# --- 解析 JSON 响应 --- # --- 解析 JSON 响应 ---
try: try:
# 尝试去除可能的Markdown代码块标记 # 尝试去除可能的Markdown代码块标记
cleaned_response = response_text.strip().strip('`').strip() cleaned_response = response_text.strip().strip("`").strip()
if cleaned_response.startswith('json'): if cleaned_response.startswith("json"):
cleaned_response = cleaned_response[4:].strip() cleaned_response = cleaned_response[4:].strip()
data = json.loads(cleaned_response) data = json.loads(cleaned_response)
@@ -489,7 +489,9 @@ class SubHeartflowManager:
logger.debug(f"{log_prefix} LLM评估结果 (来自JSON): {'建议转换' if decision else '建议不转换'}") logger.debug(f"{log_prefix} LLM评估结果 (来自JSON): {'建议转换' if decision else '建议不转换'}")
return decision return decision
else: else:
logger.warning(f"{log_prefix} LLM 返回的 JSON 中 'decision' 键的值不是布尔型: {decision}。响应: {response_text}") logger.warning(
f"{log_prefix} LLM 返回的 JSON 中 'decision' 键的值不是布尔型: {decision}。响应: {response_text}"
)
return None # 值类型不正确 return None # 值类型不正确
except json.JSONDecodeError as json_err: except json.JSONDecodeError as json_err:

View File

@@ -8,6 +8,7 @@ from .pfc_utils import get_items_from_json
from src.individuality.individuality import Individuality from src.individuality.individuality import Individuality
from .observation_info import ObservationInfo from .observation_info import ObservationInfo
from .conversation_info import ConversationInfo from .conversation_info import ConversationInfo
from src.plugins.utils.chat_message_builder import build_readable_messages
pfc_action_log_config = LogConfig( pfc_action_log_config = LogConfig(
console_format=PFC_ACTION_PLANNER_STYLE_CONFIG["console_format"], console_format=PFC_ACTION_PLANNER_STYLE_CONFIG["console_format"],
@@ -132,12 +133,7 @@ class ActionPlanner:
chat_history_text = "" chat_history_text = ""
try: try:
if hasattr(observation_info, "chat_history") and observation_info.chat_history: if hasattr(observation_info, "chat_history") and observation_info.chat_history:
chat_history_list = observation_info.chat_history[-20:] chat_history_text = observation_info.chat_history_str
for msg in chat_history_list:
if isinstance(msg, dict) and "detailed_plain_text" in msg:
chat_history_text += f"{msg.get('detailed_plain_text', '')}\n"
elif isinstance(msg, str):
chat_history_text += f"{msg}\n"
if not chat_history_text: # 如果历史记录是空列表 if not chat_history_text: # 如果历史记录是空列表
chat_history_text = "还没有聊天记录。\n" chat_history_text = "还没有聊天记录。\n"
else: else:
@@ -146,12 +142,16 @@ class ActionPlanner:
if hasattr(observation_info, "new_messages_count") and observation_info.new_messages_count > 0: if hasattr(observation_info, "new_messages_count") and observation_info.new_messages_count > 0:
if hasattr(observation_info, "unprocessed_messages") and observation_info.unprocessed_messages: if hasattr(observation_info, "unprocessed_messages") and observation_info.unprocessed_messages:
new_messages_list = observation_info.unprocessed_messages new_messages_list = observation_info.unprocessed_messages
chat_history_text += f"--- 以下是 {observation_info.new_messages_count} 条新消息 ---\n" new_messages_str = await build_readable_messages(
for msg in new_messages_list: new_messages_list,
if isinstance(msg, dict) and "detailed_plain_text" in msg: replace_bot_name=True,
chat_history_text += f"{msg.get('detailed_plain_text', '')}\n" merge_messages=False,
elif isinstance(msg, str): timestamp_mode="relative",
chat_history_text += f"{msg}\n" read_mark=0.0,
)
chat_history_text += (
f"\n--- 以下是 {observation_info.new_messages_count} 条新消息 ---\n{new_messages_str}"
)
# 清理消息应该由调用者或 observation_info 内部逻辑处理,这里不再调用 clear # 清理消息应该由调用者或 observation_info 内部逻辑处理,这里不再调用 clear
# if hasattr(observation_info, 'clear_unprocessed_messages'): # if hasattr(observation_info, 'clear_unprocessed_messages'):
# observation_info.clear_unprocessed_messages() # observation_info.clear_unprocessed_messages()

View File

@@ -3,8 +3,9 @@ import asyncio
import datetime import datetime
# from .message_storage import MongoDBMessageStorage # from .message_storage import MongoDBMessageStorage
from src.plugins.utils.chat_message_builder import get_raw_msg_before_timestamp_with_chat from src.plugins.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat
from ...config.config import global_config
# from ...config.config import global_config
from typing import Dict, Any from typing import Dict, Any
from ..chat.message import Message from ..chat.message import Message
from .pfc_types import ConversationState from .pfc_types import ConversationState
@@ -77,14 +78,22 @@ class Conversation:
raise raise
try: try:
logger.info(f"{self.stream_id} 加载初始聊天记录...") logger.info(f"{self.stream_id} 加载初始聊天记录...")
initial_messages = await get_raw_msg_before_timestamp_with_chat( # initial_messages = get_raw_msg_before_timestamp_with_chat( #
chat_id=self.stream_id, chat_id=self.stream_id,
timestamp=time.time(), timestamp=time.time(),
limit=30, # 加载最近30条作为初始上下文可以调整 limit=30, # 加载最近30条作为初始上下文可以调整
) )
chat_talking_prompt = await build_readable_messages(
initial_messages,
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",
read_mark=0.0,
)
if initial_messages: if initial_messages:
# 将加载的消息填充到 ObservationInfo 的 chat_history # 将加载的消息填充到 ObservationInfo 的 chat_history
self.observation_info.chat_history = initial_messages self.observation_info.chat_history = initial_messages
self.observation_info.chat_history_str = chat_talking_prompt + "\n"
self.observation_info.chat_history_count = len(initial_messages) self.observation_info.chat_history_count = len(initial_messages)
# 更新 ObservationInfo 中的时间戳等信息 # 更新 ObservationInfo 中的时间戳等信息
@@ -162,7 +171,7 @@ class Conversation:
if hasattr(self.observation_info, "clear_unprocessed_messages"): if hasattr(self.observation_info, "clear_unprocessed_messages"):
# 确保 clear_unprocessed_messages 方法存在 # 确保 clear_unprocessed_messages 方法存在
logger.debug(f"准备执行 direct_reply清理 {initial_new_message_count} 条规划时已知的新消息。") logger.debug(f"准备执行 direct_reply清理 {initial_new_message_count} 条规划时已知的新消息。")
self.observation_info.clear_unprocessed_messages() await self.observation_info.clear_unprocessed_messages()
# 手动重置计数器,确保状态一致性(理想情况下 clear 方法会做这个) # 手动重置计数器,确保状态一致性(理想情况下 clear 方法会做这个)
if hasattr(self.observation_info, "new_messages_count"): if hasattr(self.observation_info, "new_messages_count"):
self.observation_info.new_messages_count = 0 self.observation_info.new_messages_count = 0
@@ -272,6 +281,7 @@ class Conversation:
reply=self.generated_reply, reply=self.generated_reply,
goal=current_goal_str, goal=current_goal_str,
chat_history=observation_info.chat_history, chat_history=observation_info.chat_history,
chat_history_str=observation_info.chat_history_str,
retry_count=reply_attempt_count - 1, # 传递当前尝试次数从0开始计数 retry_count=reply_attempt_count - 1, # 传递当前尝试次数从0开始计数
) )
logger.info( logger.info(
@@ -436,42 +446,11 @@ class Conversation:
try: try:
# 外层 try: 捕获发送消息和后续处理中的主要错误 # 外层 try: 捕获发送消息和后续处理中的主要错误
current_time = time.time() # 获取当前时间戳 _current_time = time.time() # 获取当前时间戳
reply_content = self.generated_reply # 获取要发送的内容 reply_content = self.generated_reply # 获取要发送的内容
# 发送消息 # 发送消息
await self.direct_sender.send_message(chat_stream=self.chat_stream, content=reply_content) await self.direct_sender.send_message(chat_stream=self.chat_stream, content=reply_content)
logger.info(f"消息已发送: {reply_content}") # 可以在发送后加个日志确认
# --- 添加的立即更新状态逻辑开始 ---
try:
# 内层 try: 专门捕获手动更新状态时可能出现的错误
# 创建一个代表刚刚发送的消息的字典
bot_message_info = {
"message_id": f"bot_sent_{current_time}", # 创建一个简单的唯一ID
"time": current_time,
"user_info": UserInfo( # 使用 UserInfo 类构建用户信息
user_id=str(global_config.BOT_QQ),
user_nickname=global_config.BOT_NICKNAME,
platform=self.chat_stream.platform, # 从 chat_stream 获取平台信息
).to_dict(), # 转换为字典格式存储
"processed_plain_text": reply_content, # 使用发送的内容
"detailed_plain_text": f"{int(current_time)},{global_config.BOT_NICKNAME}:{reply_content}", # 构造一个简单的详细文本, 时间戳取整
# 可以根据需要添加其他字段,保持与 observation_info.chat_history 中其他消息结构一致
}
# 直接更新 ObservationInfo 实例
if self.observation_info:
self.observation_info.chat_history.append(bot_message_info) # 将消息添加到历史记录末尾
self.observation_info.last_bot_speak_time = current_time # 更新 Bot 最后发言时间
self.observation_info.last_message_time = current_time # 更新最后消息时间
logger.debug("已手动将Bot发送的消息添加到 ObservationInfo")
else:
logger.warning("无法手动更新 ObservationInfo实例不存在")
except Exception as update_err:
logger.error(f"手动更新 ObservationInfo 时出错: {update_err}")
# --- 添加的立即更新状态逻辑结束 ---
# 原有的触发更新和等待代码 # 原有的触发更新和等待代码
self.chat_observer.trigger_update() self.chat_observer.trigger_update()

View File

@@ -7,6 +7,7 @@ from dataclasses import dataclass, field
from src.common.logger import get_module_logger from src.common.logger import get_module_logger
from .chat_observer import ChatObserver from .chat_observer import ChatObserver
from .chat_states import NotificationHandler, NotificationType from .chat_states import NotificationHandler, NotificationType
from src.plugins.utils.chat_message_builder import build_readable_messages
logger = get_module_logger("observation_info") logger = get_module_logger("observation_info")
@@ -97,6 +98,7 @@ class ObservationInfo:
# data_list # data_list
chat_history: List[str] = field(default_factory=list) chat_history: List[str] = field(default_factory=list)
chat_history_str: str = ""
unprocessed_messages: List[Dict[str, Any]] = field(default_factory=list) unprocessed_messages: List[Dict[str, Any]] = field(default_factory=list)
active_users: Set[str] = field(default_factory=set) active_users: Set[str] = field(default_factory=set)
@@ -223,11 +225,18 @@ class ObservationInfo:
return None return None
return time.time() - self.last_bot_speak_time return time.time() - self.last_bot_speak_time
def clear_unprocessed_messages(self): async def clear_unprocessed_messages(self):
"""清空未处理消息列表""" """清空未处理消息列表"""
# 将未处理消息添加到历史记录中 # 将未处理消息添加到历史记录中
for message in self.unprocessed_messages: for message in self.unprocessed_messages:
self.chat_history.append(message) self.chat_history.append(message)
self.chat_history_str = await build_readable_messages(
self.chat_history[-20:] if len(self.chat_history) > 20 else self.chat_history,
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",
read_mark=0.0,
)
# 清空未处理消息列表 # 清空未处理消息列表
self.has_unread_messages = False self.has_unread_messages = False
self.unprocessed_messages.clear() self.unprocessed_messages.clear()

View File

@@ -19,6 +19,7 @@ from src.individuality.individuality import Individuality
from .conversation_info import ConversationInfo from .conversation_info import ConversationInfo
from .observation_info import ObservationInfo from .observation_info import ObservationInfo
import time import time
from src.plugins.utils.chat_message_builder import build_readable_messages
if TYPE_CHECKING: if TYPE_CHECKING:
pass pass
@@ -80,19 +81,20 @@ class GoalAnalyzer:
goals_str = f"目标:{goal},产生该对话目标的原因:{reasoning}\n" goals_str = f"目标:{goal},产生该对话目标的原因:{reasoning}\n"
# 获取聊天历史记录 # 获取聊天历史记录
chat_history_list = observation_info.chat_history chat_history_text = observation_info.chat_history
chat_history_text = ""
for msg in chat_history_list:
chat_history_text += f"{msg}\n"
if observation_info.new_messages_count > 0: if observation_info.new_messages_count > 0:
new_messages_list = observation_info.unprocessed_messages new_messages_list = observation_info.unprocessed_messages
new_messages_str = await build_readable_messages(
new_messages_list,
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",
read_mark=0.0,
)
chat_history_text += f"\n--- 以下是 {observation_info.new_messages_count} 条新消息 ---\n{new_messages_str}"
chat_history_text += f"{observation_info.new_messages_count}条新消息:\n" # await observation_info.clear_unprocessed_messages()
for msg in new_messages_list:
chat_history_text += f"{msg}\n"
observation_info.clear_unprocessed_messages()
identity_details_only = self.identity_detail_info identity_details_only = self.identity_detail_info
identity_addon = "" identity_addon = ""
@@ -376,6 +378,7 @@ class DirectMessageSender:
# 发送消息 # 发送消息
try: try:
await self.send_via_ws(message) await self.send_via_ws(message)
await self.storage.store_message(message, chat_stream)
logger.success(f"PFC消息已发送: {content}") logger.success(f"PFC消息已发送: {content}")
except Exception as e: except Exception as e:
logger.error(f"PFC消息发送失败: {str(e)}") logger.error(f"PFC消息发送失败: {str(e)}")

View File

@@ -1,5 +1,4 @@
import json import json
import datetime
from typing import Tuple, List, Dict, Any from typing import Tuple, List, Dict, Any
from src.common.logger import get_module_logger from src.common.logger import get_module_logger
from ..models.utils_model import LLMRequest from ..models.utils_model import LLMRequest
@@ -22,7 +21,7 @@ class ReplyChecker:
self.max_retries = 3 # 最大重试次数 self.max_retries = 3 # 最大重试次数
async def check( async def check(
self, reply: str, goal: str, chat_history: List[Dict[str, Any]], retry_count: int = 0 self, reply: str, goal: str, chat_history: List[Dict[str, Any]], chat_history_text: str, retry_count: int = 0
) -> Tuple[bool, str, bool]: ) -> Tuple[bool, str, bool]:
"""检查生成的回复是否合适 """检查生成的回复是否合适
@@ -36,7 +35,6 @@ class ReplyChecker:
""" """
# 不再从 observer 获取,直接使用传入的 chat_history # 不再从 observer 获取,直接使用传入的 chat_history
# messages = self.chat_observer.get_cached_messages(limit=20) # messages = self.chat_observer.get_cached_messages(limit=20)
chat_history_text = ""
try: try:
# 筛选出最近由 Bot 自己发送的消息 # 筛选出最近由 Bot 自己发送的消息
bot_messages = [] bot_messages = []
@@ -82,14 +80,6 @@ class ReplyChecker:
logger.error(f"检查回复时出错: 类型={type(e)}, 值={e}") logger.error(f"检查回复时出错: 类型={type(e)}, 值={e}")
logger.error(traceback.format_exc()) # 打印详细的回溯信息 logger.error(traceback.format_exc()) # 打印详细的回溯信息
for msg in chat_history[-20:]:
time_str = datetime.datetime.fromtimestamp(msg["time"]).strftime("%H:%M:%S")
user_info = UserInfo.from_dict(msg.get("user_info", {}))
sender = user_info.user_nickname or f"用户{user_info.user_id}"
if sender == self.name:
sender = "你说"
chat_history_text += f"{time_str},{sender}:{msg.get('processed_plain_text', '')}\n"
prompt = f"""请检查以下回复或消息是否合适: prompt = f"""请检查以下回复或消息是否合适:
当前对话目标:{goal} 当前对话目标:{goal}

View File

@@ -7,6 +7,7 @@ from .reply_checker import ReplyChecker
from src.individuality.individuality import Individuality from src.individuality.individuality import Individuality
from .observation_info import ObservationInfo from .observation_info import ObservationInfo
from .conversation_info import ConversationInfo from .conversation_info import ConversationInfo
from src.plugins.utils.chat_message_builder import build_readable_messages
logger = get_module_logger("reply_generator") logger = get_module_logger("reply_generator")
@@ -68,23 +69,19 @@ class ReplyGenerator:
goals_str = f"目标:{goal},产生该对话目标的原因:{reasoning}\n" goals_str = f"目标:{goal},产生该对话目标的原因:{reasoning}\n"
# 获取聊天历史记录 # 获取聊天历史记录
chat_history_list = ( chat_history_text = observation_info.chat_history_str
observation_info.chat_history[-20:]
if len(observation_info.chat_history) >= 20
else observation_info.chat_history
)
chat_history_text = ""
for msg in chat_history_list:
chat_history_text += f"{msg.get('detailed_plain_text', '')}\n"
if observation_info.new_messages_count > 0: if observation_info.new_messages_count > 0:
new_messages_list = observation_info.unprocessed_messages new_messages_list = observation_info.unprocessed_messages
new_messages_str = await build_readable_messages(
chat_history_text += f"{observation_info.new_messages_count}条新消息:\n" new_messages_list,
for msg in new_messages_list: replace_bot_name=True,
chat_history_text += f"{msg.get('detailed_plain_text', '')}\n" merge_messages=False,
timestamp_mode="relative",
observation_info.clear_unprocessed_messages() read_mark=0.0,
)
chat_history_text += f"\n--- 以下是 {observation_info.new_messages_count} 条新消息 ---\n{new_messages_str}"
# await observation_info.clear_unprocessed_messages()
identity_details_only = self.identity_detail_info identity_details_only = self.identity_detail_info
identity_addon = "" identity_addon = ""
@@ -173,7 +170,7 @@ class ReplyGenerator:
return "抱歉,我现在有点混乱,让我重新思考一下..." return "抱歉,我现在有点混乱,让我重新思考一下..."
async def check_reply( async def check_reply(
self, reply: str, goal: str, chat_history: List[Dict[str, Any]], retry_count: int = 0 self, reply: str, goal: str, chat_history: List[Dict[str, Any]], chat_history_str: str, retry_count: int = 0
) -> Tuple[bool, str, bool]: ) -> Tuple[bool, str, bool]:
"""检查回复是否合适 """检查回复是否合适
@@ -185,4 +182,4 @@ class ReplyGenerator:
Returns: Returns:
Tuple[bool, str, bool]: (是否合适, 原因, 是否需要重新规划) Tuple[bool, str, bool]: (是否合适, 原因, 是否需要重新规划)
""" """
return await self.reply_checker.check(reply, goal, chat_history, retry_count) return await self.reply_checker.check(reply, goal, chat_history, chat_history_str, retry_count)

View File

@@ -342,7 +342,9 @@ class NormalChat:
mean_interest = statistics.mean(interest_values) mean_interest = statistics.mean(interest_values)
stdev_interest = statistics.stdev(interest_values) stdev_interest = statistics.stdev(interest_values)
threshold = mean_interest + stdev_interest threshold = mean_interest + stdev_interest
logger.info(f"[{self.stream_name}] 初始兴趣值 均值: {mean_interest:.2f}, 标准差: {stdev_interest:.2f}, 阈值: {threshold:.2f}") logger.info(
f"[{self.stream_name}] 初始兴趣值 均值: {mean_interest:.2f}, 标准差: {stdev_interest:.2f}, 阈值: {threshold:.2f}"
)
# 找出高于阈值的消息 # 找出高于阈值的消息
for item in items_to_process: for item in items_to_process:
@@ -359,9 +361,7 @@ class NormalChat:
msg_id, (message, interest_value, is_mentioned) = item msg_id, (message, interest_value, is_mentioned) = item
try: try:
logger.info(f"[{self.stream_name}] 处理初始高兴趣消息 {msg_id} (兴趣值: {interest_value:.2f})") logger.info(f"[{self.stream_name}] 处理初始高兴趣消息 {msg_id} (兴趣值: {interest_value:.2f})")
await self.normal_response( await self.normal_response(message=message, is_mentioned=is_mentioned, interested_rate=interest_value)
message=message, is_mentioned=is_mentioned, interested_rate=interest_value
)
processed_count += 1 processed_count += 1
except Exception as e: except Exception as e:
logger.error(f"[{self.stream_name}] 处理初始兴趣消息 {msg_id} 时出错: {e}\n{traceback.format_exc()}") logger.error(f"[{self.stream_name}] 处理初始兴趣消息 {msg_id} 时出错: {e}\n{traceback.format_exc()}")
@@ -369,8 +369,10 @@ class NormalChat:
# 无论成功与否都清空兴趣字典 # 无论成功与否都清空兴趣字典
self.interest_dict.clear() self.interest_dict.clear()
logger.info(
f"[{self.stream_name}] 初始高兴趣消息处理完毕,共处理 {processed_count} 条。剩余 {len(self.interest_dict)} 条待轮询。"
)
logger.info(f"[{self.stream_name}] 初始高兴趣消息处理完毕,共处理 {processed_count} 条。剩余 {len(self.interest_dict)} 条待轮询。")
# --- 新增结束 --- # --- 新增结束 ---
# 保持 staticmethod, 因为不依赖实例状态, 但需要 chat 对象来获取日志上下文 # 保持 staticmethod, 因为不依赖实例状态, 但需要 chat 对象来获取日志上下文