Merge branch 'PFC-test' of https://github.com/Dax233/MaiMBot into PFC-test

pull/937/head
Bakadax 2025-05-08 22:05:42 +08:00
commit 5a89bf4981
2 changed files with 37 additions and 21 deletions

View File

@ -3,6 +3,7 @@ import traceback
from typing import Tuple, Optional, Dict, Any, List
from src.common.logger_manager import get_logger
# from src.individuality.individuality import Individuality
from src.plugins.utils.chat_message_builder import build_readable_messages
from ..models.utils_model import LLMRequest
@ -154,6 +155,7 @@ block_and_ignore: 更加极端的结束对话方式,直接结束对话并在
注意请严格按照JSON格式输出不要包含任何其他内容"""
class ActionPlanner:
"""行动规划器"""
@ -214,13 +216,12 @@ class ActionPlanner:
goals_str = self._build_goals_string(conversation_info)
chat_history_text = await self._build_chat_history_text(observation_info)
# 获取 sender_name, relationship_text, current_emotion_text
sender_name_str = getattr(observation_info, 'sender_name', '对方') # 从 observation_info 获取
sender_name_str = getattr(observation_info, "sender_name", "对方") # 从 observation_info 获取
if not sender_name_str:
sender_name_str = '对方' # 再次确保有默认值
relationship_text_str = getattr(conversation_info, 'relationship_text', '你们还不熟悉。')
current_emotion_text_str = getattr(conversation_info, 'current_emotion_text', '心情平静。')
sender_name_str = "对方" # 再次确保有默认值
relationship_text_str = getattr(conversation_info, "relationship_text", "你们还不熟悉。")
current_emotion_text_str = getattr(conversation_info, "current_emotion_text", "心情平静。")
persona_text = f"{self.name}"
action_history_summary, last_action_context = self._build_action_history_context(conversation_info)
@ -237,14 +238,16 @@ class ActionPlanner:
# --- 2. 选择并格式化 Prompt ---
try:
if use_reflect_prompt: # 新增的判断
if use_reflect_prompt: # 新增的判断
prompt_template = PROMPT_REFLECT_AND_ACT
log_msg = "使用 PROMPT_REFLECT_AND_ACT (反思决策)"
# 对于 PROMPT_REFLECT_AND_ACT它不包含 send_new_message 选项,所以 spam_warning_message 中的相关提示可以调整或省略
# 但为了保持占位符填充的一致性,我们仍然计算它
spam_warning_message = ""
if conversation_info.my_message_count > 5: # 这里的 my_message_count 仍有意义,表示之前连续发送了多少
spam_warning_message = f"⚠️【警告】**你之前已连续发送{str(conversation_info.my_message_count)}条消息!请谨慎决策。**"
if conversation_info.my_message_count > 5: # 这里的 my_message_count 仍有意义,表示之前连续发送了多少
spam_warning_message = (
f"⚠️【警告】**你之前已连续发送{str(conversation_info.my_message_count)}条消息!请谨慎决策。**"
)
elif conversation_info.my_message_count > 2:
spam_warning_message = f"💬【提示】**你之前已连续发送{str(conversation_info.my_message_count)}条消息。请注意保持对话平衡。**"
@ -260,12 +263,12 @@ class ActionPlanner:
else:
prompt_template = PROMPT_INITIAL_REPLY
log_msg = "使用 PROMPT_INITIAL_REPLY (首次/非连续回复决策)"
spam_warning_message = "" # 初始回复时通常不需要刷屏警告
spam_warning_message = "" # 初始回复时通常不需要刷屏警告
logger.debug(f"[私聊][{self.private_name}] {log_msg}")
current_time_value = "获取时间失败"
if observation_info and hasattr(observation_info, 'current_time_str') and observation_info.current_time_str:
if observation_info and hasattr(observation_info, "current_time_str") and observation_info.current_time_str:
current_time_value = observation_info.current_time_str
if spam_warning_message:
@ -285,7 +288,7 @@ class ActionPlanner:
spam_warning_info=spam_warning_message,
sender_name=sender_name_str,
relationship_text=relationship_text_str,
current_emotion_text=current_emotion_text_str
current_emotion_text=current_emotion_text_str,
)
logger.debug(f"[私聊][{self.private_name}] 发送到LLM的最终提示词:\n------\n{prompt}\n------")
except KeyError as fmt_key_err:
@ -333,10 +336,11 @@ class ActionPlanner:
time_str_for_end_decision = observation_info.current_time_str
final_action, final_reason = await self._handle_end_conversation_decision(
persona_text,
chat_history_text, initial_reason,
time_str_for_end_decision,
chat_history_text,
initial_reason,
time_str_for_end_decision,
sender_name_str=sender_name_str,
relationship_text_str=relationship_text_str
relationship_text_str=relationship_text_str,
)
except Exception as end_dec_err:
logger.error(f"[私聊][{self.private_name}] 处理结束对话决策时出错: {end_dec_err}")
@ -361,7 +365,7 @@ class ActionPlanner:
"block_and_ignore",
"say_goodbye",
]
valid_actions_reflect = [ # PROMPT_REFLECT_AND_ACT 的动作
valid_actions_reflect = [ # PROMPT_REFLECT_AND_ACT 的动作
"wait",
"listening",
"rethink_goal",
@ -502,9 +506,7 @@ class ActionPlanner:
)
logger.debug(f"[私聊][{self.private_name}] 向 LLM 追加了 {other_unread_count} 条未读消息。")
else:
chat_history_text += (
"\n--- 以上均为已读消息,未读消息均已处理完毕 ---\n"
)
chat_history_text += "\n--- 以上均为已读消息,未读消息均已处理完毕 ---\n"
except AttributeError as e:
logger.warning(f"[私聊][{self.private_name}] 构建聊天记录文本时属性错误: {e}")
chat_history_text = "[获取聊天记录时出错]\n"
@ -562,11 +564,23 @@ class ActionPlanner:
# --- Helper method for handling end_conversation decision ---
async def _handle_end_conversation_decision(
self, persona_text: str, chat_history_text: str, initial_reason: str, current_time_str: str, sender_name_str: str, relationship_text_str: str
self,
persona_text: str,
chat_history_text: str,
initial_reason: str,
current_time_str: str,
sender_name_str: str,
relationship_text_str: str,
) -> Tuple[str, str]:
"""处理结束对话前的告别决策"""
logger.info(f"[私聊][{self.private_name}] 初步规划结束对话,进入告别决策...")
end_decision_prompt = PROMPT_END_DECISION.format(persona_text=persona_text, chat_history_text=chat_history_text,current_time_str=current_time_str,sender_name = sender_name_str, relationship_text = relationship_text_str)
end_decision_prompt = PROMPT_END_DECISION.format(
persona_text=persona_text,
chat_history_text=chat_history_text,
current_time_str=current_time_str,
sender_name=sender_name_str,
relationship_text=relationship_text_str,
)
logger.debug(f"[私聊][{self.private_name}] 发送到LLM的结束决策提示词:\n------\n{end_decision_prompt}\n------")
llm_start_time = time.time()
end_content, _ = await self.llm.generate_response_async(end_decision_prompt)

View File

@ -295,7 +295,9 @@ async def handle_action(
# 后续的 plan 循环会检测到这个 "done_no_reply" 状态并使用反思 prompt
elif is_suitable: # 适用于 direct_reply 或 (send_new_message 且 RG决定发送并通过检查)
logger.debug(f"[私聊][{conversation_instance.private_name}] 动作 '{action}': 找到合适的回复,准备发送。")
logger.debug(
f"[私聊][{conversation_instance.private_name}] 动作 '{action}': 找到合适的回复,准备发送。"
)
# conversation_info.last_reply_rejection_reason = None # 已在循环内清除
# conversation_info.last_rejected_reply_content = None
conversation_instance.generated_reply = generated_content_for_check_or_send # 使用检查通过的内容