better:更好的关系处理器
This commit is contained in:
@@ -413,7 +413,6 @@ class HeartFChatting:
|
||||
"action_result": {
|
||||
"action_type": "error",
|
||||
"action_data": {},
|
||||
"reasoning": f"上下文处理失败: {e}",
|
||||
},
|
||||
"observed_messages": "",
|
||||
},
|
||||
@@ -650,143 +649,9 @@ class HeartFChatting:
|
||||
|
||||
return all_plan_info, processor_time_costs
|
||||
|
||||
async def _process_post_planning_processors(self, observations: List[Observation], action_data: dict) -> dict:
|
||||
"""
|
||||
处理后期处理器(规划后执行的处理器)
|
||||
包括:关系处理器、表达选择器、记忆激活器
|
||||
|
||||
参数:
|
||||
observations: 观察器列表
|
||||
action_data: 原始动作数据
|
||||
|
||||
返回:
|
||||
dict: 更新后的动作数据
|
||||
"""
|
||||
logger.info(f"{self.log_prefix} 开始执行后期处理器")
|
||||
|
||||
# 创建所有后期任务
|
||||
task_list = []
|
||||
task_to_name_map = {}
|
||||
|
||||
# 添加后期处理器任务
|
||||
for processor in self.post_planning_processors:
|
||||
processor_name = processor.__class__.__name__
|
||||
|
||||
async def run_processor_with_timeout(proc=processor):
|
||||
return await asyncio.wait_for(
|
||||
proc.process_info(observations=observations),
|
||||
timeout=global_config.focus_chat.processor_max_time,
|
||||
)
|
||||
|
||||
task = asyncio.create_task(run_processor_with_timeout())
|
||||
task_list.append(task)
|
||||
task_to_name_map[task] = ("processor", processor_name)
|
||||
logger.info(f"{self.log_prefix} 启动后期处理器任务: {processor_name}")
|
||||
|
||||
# 添加记忆激活器任务
|
||||
async def run_memory_with_timeout():
|
||||
return await asyncio.wait_for(
|
||||
self.memory_activator.activate_memory(observations),
|
||||
timeout=MEMORY_ACTIVATION_TIMEOUT,
|
||||
)
|
||||
|
||||
memory_task = asyncio.create_task(run_memory_with_timeout())
|
||||
task_list.append(memory_task)
|
||||
task_to_name_map[memory_task] = ("memory", "MemoryActivator")
|
||||
logger.info(f"{self.log_prefix} 启动记忆激活器任务")
|
||||
|
||||
# 如果没有任何后期任务,直接返回
|
||||
if not task_list:
|
||||
logger.info(f"{self.log_prefix} 没有启用的后期处理器或记忆激活器")
|
||||
return action_data
|
||||
|
||||
# 等待所有任务完成
|
||||
pending_tasks = set(task_list)
|
||||
all_post_plan_info = []
|
||||
running_memorys = []
|
||||
|
||||
while pending_tasks:
|
||||
done, pending_tasks = await asyncio.wait(pending_tasks, return_when=asyncio.FIRST_COMPLETED)
|
||||
|
||||
for task in done:
|
||||
task_type, task_name = task_to_name_map[task]
|
||||
|
||||
try:
|
||||
result = await task
|
||||
|
||||
if task_type == "processor":
|
||||
logger.info(f"{self.log_prefix} 后期处理器 {task_name} 已完成!")
|
||||
if result is not None:
|
||||
all_post_plan_info.extend(result)
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} 后期处理器 {task_name} 返回了 None")
|
||||
elif task_type == "memory":
|
||||
logger.info(f"{self.log_prefix} 记忆激活器已完成!")
|
||||
if result is not None:
|
||||
running_memorys = result
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} 记忆激活器返回了 None")
|
||||
running_memorys = []
|
||||
|
||||
except asyncio.TimeoutError:
|
||||
if task_type == "processor":
|
||||
logger.warning(
|
||||
f"{self.log_prefix} 后期处理器 {task_name} 超时(>{global_config.focus_chat.processor_max_time}s),已跳过"
|
||||
)
|
||||
elif task_type == "memory":
|
||||
logger.warning(f"{self.log_prefix} 记忆激活器超时(>{MEMORY_ACTIVATION_TIMEOUT}s),已跳过")
|
||||
running_memorys = []
|
||||
except Exception as e:
|
||||
if task_type == "processor":
|
||||
logger.error(
|
||||
f"{self.log_prefix} 后期处理器 {task_name} 执行失败. 错误: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
elif task_type == "memory":
|
||||
logger.error(f"{self.log_prefix} 记忆激活器执行失败. 错误: {e}", exc_info=True)
|
||||
running_memorys = []
|
||||
|
||||
# 将后期处理器的结果整合到 action_data 中
|
||||
updated_action_data = action_data.copy()
|
||||
|
||||
relation_info = ""
|
||||
selected_expressions = []
|
||||
structured_info = ""
|
||||
|
||||
for info in all_post_plan_info:
|
||||
if isinstance(info, RelationInfo):
|
||||
relation_info = info.get_processed_info()
|
||||
elif isinstance(info, ExpressionSelectionInfo):
|
||||
selected_expressions = info.get_expressions_for_action_data()
|
||||
elif isinstance(info, StructuredInfo):
|
||||
structured_info = info.get_processed_info()
|
||||
|
||||
if relation_info:
|
||||
updated_action_data["relation_info_block"] = relation_info
|
||||
|
||||
if selected_expressions:
|
||||
updated_action_data["selected_expressions"] = selected_expressions
|
||||
|
||||
if structured_info:
|
||||
updated_action_data["structured_info"] = structured_info
|
||||
|
||||
# 特殊处理running_memorys
|
||||
if running_memorys:
|
||||
memory_str = "以下是当前在聊天中,你回忆起的记忆:\n"
|
||||
for running_memory in running_memorys:
|
||||
memory_str += f"{running_memory['content']}\n"
|
||||
updated_action_data["memory_block"] = memory_str
|
||||
logger.info(f"{self.log_prefix} 添加了 {len(running_memorys)} 个激活的记忆到action_data")
|
||||
|
||||
if all_post_plan_info or running_memorys:
|
||||
logger.info(
|
||||
f"{self.log_prefix} 后期处理完成,产生了 {len(all_post_plan_info)} 个信息项和 {len(running_memorys)} 个记忆"
|
||||
)
|
||||
|
||||
return updated_action_data
|
||||
|
||||
async def _process_post_planning_processors_with_timing(
|
||||
self, observations: List[Observation], action_data: dict
|
||||
self, observations: List[Observation], action_type: str, action_data: dict
|
||||
) -> tuple[dict, dict]:
|
||||
"""
|
||||
处理后期处理器(规划后执行的处理器)并收集详细时间统计
|
||||
@@ -794,6 +659,7 @@ class HeartFChatting:
|
||||
|
||||
参数:
|
||||
observations: 观察器列表
|
||||
action_type: 动作类型
|
||||
action_data: 原始动作数据
|
||||
|
||||
返回:
|
||||
@@ -815,7 +681,9 @@ class HeartFChatting:
|
||||
start_time = time.time()
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
proc.process_info(observations=observations),
|
||||
proc.process_info(
|
||||
observations=observations, action_type=action_type, action_data=action_data
|
||||
),
|
||||
timeout=global_config.focus_chat.processor_max_time,
|
||||
)
|
||||
end_time = time.time()
|
||||
@@ -1074,7 +942,7 @@ class HeartFChatting:
|
||||
# 记录详细的后处理器时间
|
||||
post_start_time = time.time()
|
||||
action_data, post_processor_time_costs = await self._process_post_planning_processors_with_timing(
|
||||
self.observations, action_data
|
||||
self.observations, action_type, action_data
|
||||
)
|
||||
post_end_time = time.time()
|
||||
logger.info(f"{self.log_prefix} 后期处理器总耗时: {post_end_time - post_start_time:.3f}秒")
|
||||
|
||||
Reference in New Issue
Block a user