feat:精简处理器上下文

This commit is contained in:
SengokuCola
2025-06-23 00:39:13 +08:00
parent 2128eb6bf2
commit 3cb60b03b4
10 changed files with 159 additions and 90 deletions

View File

@@ -660,6 +660,7 @@ class HeartFChatting:
}
with Timer("执行动作", cycle_timers):
action_type, action_data, reasoning = (
plan_result.get("action_result", {}).get("action_type", "error"),
plan_result.get("action_result", {}).get("action_data", {}),
@@ -674,7 +675,7 @@ class HeartFChatting:
action_str = action_type
logger.debug(f"{self.log_prefix} 麦麦想要:'{action_str}'")
success, reply_text, command = await self._handle_action(
action_type, reasoning, action_data, cycle_timers, thinking_id
)

View File

@@ -20,7 +20,7 @@ class HFCVersionManager:
"""HFC版本号管理器"""
# 默认版本号
DEFAULT_VERSION = "v1.0.0"
DEFAULT_VERSION = "v2.0.2"
# 当前运行时版本号
_current_version: Optional[str] = None

View File

@@ -16,6 +16,8 @@ class ObsInfo(InfoBase):
Data Fields:
talking_message (str): 说话消息内容
talking_message_str_truncate (str): 截断后的说话消息内容
talking_message_str_short (str): 简短版本的说话消息内容(使用最新一半消息)
talking_message_str_truncate_short (str): 截断简短版本的说话消息内容(使用最新一半消息)
chat_type (str): 聊天类型,可以是 "private"(私聊)、"group"(群聊)或 "other"(其他)
"""
@@ -37,6 +39,22 @@ class ObsInfo(InfoBase):
"""
self.data["talking_message_str_truncate"] = message
def set_talking_message_str_short(self, message: str) -> None:
"""设置简短版本的说话消息
Args:
message (str): 简短版本的说话消息内容
"""
self.data["talking_message_str_short"] = message
def set_talking_message_str_truncate_short(self, message: str) -> None:
"""设置截断简短版本的说话消息
Args:
message (str): 截断简短版本的说话消息内容
"""
self.data["talking_message_str_truncate_short"] = message
def set_previous_chat_info(self, message: str) -> None:
"""设置之前聊天信息
@@ -62,6 +80,22 @@ class ObsInfo(InfoBase):
chat_target (str): 聊天目标,可以是 "private"(私聊)、"group"(群聊)或 "other"(其他)
"""
self.data["chat_target"] = chat_target
def set_chat_id(self, chat_id: str) -> None:
"""设置聊天ID
Args:
chat_id (str): 聊天ID
"""
self.data["chat_id"] = chat_id
def get_chat_id(self) -> Optional[str]:
"""获取聊天ID
Returns:
Optional[str]: 聊天ID如果未设置则返回 None
"""
return self.get_info("chat_id")
def get_talking_message(self) -> Optional[str]:
"""获取说话消息
@@ -79,6 +113,22 @@ class ObsInfo(InfoBase):
"""
return self.get_info("talking_message_str_truncate")
def get_talking_message_str_short(self) -> Optional[str]:
"""获取简短版本的说话消息
Returns:
Optional[str]: 简短版本的说话消息内容,如果未设置则返回 None
"""
return self.get_info("talking_message_str_short")
def get_talking_message_str_truncate_short(self) -> Optional[str]:
"""获取截断简短版本的说话消息
Returns:
Optional[str]: 截断简短版本的说话消息内容,如果未设置则返回 None
"""
return self.get_info("talking_message_str_truncate_short")
def get_chat_type(self) -> str:
"""获取聊天类型

View File

@@ -62,6 +62,10 @@ class ChattingInfoProcessor(BaseProcessor):
# 改为异步任务,不阻塞主流程
# asyncio.create_task(self.chat_compress(obs))
# 设置聊天ID
if hasattr(obs, "chat_id"):
obs_info.set_chat_id(obs.chat_id)
# 设置说话消息
if hasattr(obs, "talking_message_str"):
# print(f"设置说话消息obs.talking_message_str: {obs.talking_message_str}")
@@ -72,6 +76,14 @@ class ChattingInfoProcessor(BaseProcessor):
# print(f"设置截断后的说话消息obs.talking_message_str_truncate: {obs.talking_message_str_truncate}")
obs_info.set_talking_message_str_truncate(obs.talking_message_str_truncate)
# 设置简短版本的说话消息
if hasattr(obs, "talking_message_str_short"):
obs_info.set_talking_message_str_short(obs.talking_message_str_short)
# 设置截断简短版本的说话消息
if hasattr(obs, "talking_message_str_truncate_short"):
obs_info.set_talking_message_str_truncate_short(obs.talking_message_str_truncate_short)
if hasattr(obs, "mid_memory_info"):
# print(f"设置之前聊天信息obs.mid_memory_info: {obs.mid_memory_info}")
obs_info.set_previous_chat_info(obs.mid_memory_info)
@@ -82,7 +94,8 @@ class ChattingInfoProcessor(BaseProcessor):
chat_type = "group"
else:
chat_type = "private"
obs_info.set_chat_target(obs.chat_target_info.get("person_name", "某人"))
if hasattr(obs, "chat_target_info") and obs.chat_target_info:
obs_info.set_chat_target(obs.chat_target_info.get("person_name", "某人"))
obs_info.set_chat_type(chat_type)
# logger.debug(f"聊天信息处理器处理后的信息: {obs_info}")

View File

@@ -149,7 +149,8 @@ class ExpressionSelectorProcessor(BaseProcessor):
if observations:
for observation in observations:
if isinstance(observation, ChattingObservation):
chat_info = observation.get_observe_info()
# chat_info = observation.get_observe_info()
chat_info = observation.talking_message_str_truncate_short
break
if not chat_info:

View File

@@ -144,7 +144,7 @@ def init_prompt():
fetch_info_prompt = """
{name_block}
以下是你对{person_name}的了解,请你从中提取用户的有关"{info_type}"的信息如果用户没有相关信息请输出none
以下是你在之前与{person_name}的交流中,产生的{person_name}的了解,请你从中提取用户的有关"{info_type}"的信息如果用户没有相关信息请输出none
{person_impression_block}
{points_text_block}
请严格按照以下json输出格式不要输出多余内容
@@ -547,8 +547,7 @@ class PersonImpressionpProcessor(BaseProcessor):
for observation in observations:
if isinstance(observation, ChattingObservation):
chat_observe_info = observation.get_observe_info()
chat_observe_info = chat_observe_info[-300:]
# latest_message_time = observation.last_observe_time
# 从聊天观察中提取用户信息并更新消息段
# 获取最新的非bot消息来更新消息段
latest_messages = get_raw_msg_by_timestamp_with_chat(

View File

@@ -112,7 +112,8 @@ class ToolProcessor(BaseProcessor):
is_group_chat = observation.is_group_chat
chat_observe_info = observation.get_observe_info()
# chat_observe_info = observation.get_observe_info()
chat_observe_info = observation.talking_message_str_truncate_short
# person_list = observation.person_list
# 获取时间信息

View File

@@ -94,7 +94,7 @@ class MemoryActivator:
obs_info_text = ""
for observation in observations:
if isinstance(observation, ChattingObservation):
obs_info_text += observation.get_observe_info()
obs_info_text += observation.talking_message_str_truncate_short
elif isinstance(observation, StructureObservation):
working_info = observation.get_observe_info()
for working_info_item in working_info:

View File

@@ -63,6 +63,8 @@ class ChattingObservation(Observation):
self.talking_message = []
self.talking_message_str = ""
self.talking_message_str_truncate = ""
self.talking_message_str_short = ""
self.talking_message_str_truncate_short = ""
self.name = global_config.bot.nickname
self.nick_name = global_config.bot.alias_names
self.max_now_obs_len = global_config.focus_chat.observation_context_size
@@ -88,6 +90,8 @@ class ChattingObservation(Observation):
"chat_target_info": self.chat_target_info,
"talking_message_str": self.talking_message_str,
"talking_message_str_truncate": self.talking_message_str_truncate,
"talking_message_str_short": self.talking_message_str_short,
"talking_message_str_truncate_short": self.talking_message_str_truncate_short,
"name": self.name,
"nick_name": self.nick_name,
"last_observe_time": self.last_observe_time,
@@ -228,6 +232,24 @@ class ChattingObservation(Observation):
truncate=True,
show_actions=True,
)
# 构建简短版本 - 使用最新一半的消息
half_count = len(self.talking_message) // 2
recent_messages = self.talking_message[-half_count:] if half_count > 0 else self.talking_message
self.talking_message_str_short = build_readable_messages(
messages=recent_messages,
timestamp_mode="lite",
read_mark=last_obs_time_mark,
show_actions=True,
)
self.talking_message_str_truncate_short = build_readable_messages(
messages=recent_messages,
timestamp_mode="normal_no_YMD",
read_mark=last_obs_time_mark,
truncate=True,
show_actions=True,
)
self.person_list = await get_person_id_list(self.talking_message)