PFC消息记录格式与HFC对齐
This commit is contained in:
@@ -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,14 @@ 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()
|
||||||
@@ -242,7 +240,7 @@ class ActionPlanner:
|
|||||||
last_action_context += "- 【重要】失败/取消原因未明确记录。\n"
|
last_action_context += "- 【重要】失败/取消原因未明确记录。\n"
|
||||||
else:
|
else:
|
||||||
last_action_context += f"- 该行动当前状态: {status}\n"
|
last_action_context += f"- 该行动当前状态: {status}\n"
|
||||||
|
print(f"chat_history_text:\n{chat_history_text}")
|
||||||
# --- 构建最终的 Prompt ---
|
# --- 构建最终的 Prompt ---
|
||||||
prompt = f"""{persona_text}。现在你在参与一场QQ私聊,请根据以下【所有信息】审慎且灵活的决策下一步行动,可以发言,可以等待,可以倾听,可以调取知识:
|
prompt = f"""{persona_text}。现在你在参与一场QQ私聊,请根据以下【所有信息】审慎且灵活的决策下一步行动,可以发言,可以等待,可以倾听,可以调取知识:
|
||||||
|
|
||||||
@@ -304,4 +302,4 @@ end_conversation: 结束对话,对方长时间没回复或者当你觉得对
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"规划行动时调用 LLM 或处理结果出错: {str(e)}")
|
logger.error(f"规划行动时调用 LLM 或处理结果出错: {str(e)}")
|
||||||
return "wait", f"行动规划处理中发生错误,暂时等待: {str(e)}"
|
return "wait", f"行动规划处理中发生错误,暂时等待: {str(e)}"
|
||||||
@@ -3,9 +3,8 @@ 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
|
||||||
@@ -83,9 +82,17 @@ class Conversation:
|
|||||||
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 中的时间戳等信息
|
||||||
@@ -163,7 +170,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
|
||||||
@@ -442,7 +449,6 @@ class Conversation:
|
|||||||
|
|
||||||
# 发送消息
|
# 发送消息
|
||||||
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}") # 可以在发送后加个日志确认
|
|
||||||
|
|
||||||
# 原有的触发更新和等待代码
|
# 原有的触发更新和等待代码
|
||||||
self.chat_observer.trigger_update()
|
self.chat_observer.trigger_update()
|
||||||
@@ -454,4 +460,4 @@ class Conversation:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
# 这是外层 try 对应的 except
|
# 这是外层 try 对应的 except
|
||||||
logger.error(f"发送消息或更新状态时失败: {str(e)}")
|
logger.error(f"发送消息或更新状态时失败: {str(e)}")
|
||||||
self.state = ConversationState.ANALYZING # 出错也要尝试恢复状态
|
self.state = ConversationState.ANALYZING # 出错也要尝试恢复状态
|
||||||
@@ -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,13 +225,20 @@ 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()
|
||||||
self.chat_history_count = len(self.chat_history)
|
self.chat_history_count = len(self.chat_history)
|
||||||
self.new_messages_count = 0
|
self.new_messages_count = 0
|
||||||
@@ -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 = ""
|
||||||
@@ -379,4 +381,4 @@ class DirectMessageSender:
|
|||||||
await self.storage.store_message(message, chat_stream)
|
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)}")
|
||||||
@@ -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")
|
||||||
|
|
||||||
@@ -73,18 +74,19 @@ class ReplyGenerator:
|
|||||||
if len(observation_info.chat_history) >= 20
|
if len(observation_info.chat_history) >= 20
|
||||||
else observation_info.chat_history
|
else observation_info.chat_history
|
||||||
)
|
)
|
||||||
chat_history_text = ""
|
chat_history_text = observation_info.chat_history_str
|
||||||
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 = ""
|
||||||
@@ -185,4 +187,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, retry_count)
|
||||||
Reference in New Issue
Block a user