refactor(distribution_manager): 优化流循环管理逻辑,减少重复代码并改进异常处理

refactor(action_manager): 将异步存储操作改为非阻塞任务,提升性能
refactor(default_generator): 简化回复生成器中的消息处理逻辑
refactor(generator_api): 更新类型提示,增强代码可读性
refactor(affinity_chatter): 清理异常处理中的冗余代码,确保处理标记的正确清理
refactor(affinity_interest_calculator): 重命名阈值调整方法,提升代码一致性
refactor(plan_executor): 移除冗余的已读消息处理逻辑
refactor(planner): 优化规划器中的异常处理,确保正常模式的退出检查
This commit is contained in:
Windpicker-owo
2025-11-12 16:08:52 +08:00
parent 66c23e1ba1
commit 80736a0deb
8 changed files with 38 additions and 78 deletions

View File

@@ -107,22 +107,20 @@ class AffinityChatter(BaseChatter):
logger.info(f"亲和力聊天处理器 {self.stream_id} 处理被取消")
self.stats["failed_executions"] += 1
self.last_activity_time = time.time()
# 清理 processing_message_id
context.processing_message_id = None
raise
except Exception as e:
logger.error(f"亲和力聊天处理器 {self.stream_id} 处理StreamContext时出错: {e}\n{traceback.format_exc()}")
self.stats["failed_executions"] += 1
self.last_activity_time = time.time()
# 清理 processing_message_id
context.processing_message_id = None
return {
"success": False,
"stream_id": self.stream_id,
"error_message": str(e),
"executed_count": 0,
}
finally:
# 清理 processing_message_id
context.processing_message_id = None
def get_stats(self) -> dict[str, Any]:
"""

View File

@@ -140,7 +140,7 @@ class AffinityInterestCalculator(BaseInterestCalculator):
# 5. 考虑连续不回复的阈值调整
adjusted_score = total_score
adjusted_reply_threshold, adjusted_action_threshold = self._apply_no_reply_threshold_adjustment()
adjusted_reply_threshold, adjusted_action_threshold = self._apply_threshold_adjustment()
logger.debug(
f"[Affinity兴趣计算] 连续不回复调整: 回复阈值 {self.reply_threshold:.3f}{adjusted_reply_threshold:.3f}, "
f"动作阈值 {global_config.affinity_flow.non_reply_action_interest_threshold:.3f}{adjusted_action_threshold:.3f}"
@@ -282,7 +282,7 @@ class AffinityInterestCalculator(BaseInterestCalculator):
logger.debug("[提及分计算] 未提及机器人返回0.0")
return 0.0
def _apply_no_reply_threshold_adjustment(self) -> tuple[float, float]:
def _apply_threshold_adjustment(self) -> tuple[float, float]:
"""应用阈值调整(包括连续不回复和回复后降低机制)
Returns:

View File

@@ -228,10 +228,6 @@ class ChatterPlanExecutor:
error_message = str(e)
logger.error(f"执行回复动作失败: {action_info.action_type}, 错误: {error_message}")
# 将机器人回复添加到已读消息中
if success and action_info.action_message:
await self._add_bot_reply_to_read_messages(action_info, plan, reply_content)
execution_time = time.time() - start_time
self.execution_stats["execution_times"].append(execution_time)

View File

@@ -81,16 +81,10 @@ class ChatterActionPlanner:
except asyncio.CancelledError:
logger.info(f"规划流程被取消: {self.chat_id}")
self.planner_stats["failed_plans"] += 1
# 确保清理 processing_message_id
if context:
context.processing_message_id = None
raise
except Exception as e:
logger.error(f"规划流程出错: {e}")
self.planner_stats["failed_plans"] += 1
# 确保清理 processing_message_id
if context:
context.processing_message_id = None
return [], None
async def _enhanced_plan_flow(self, context: "StreamContext | None") -> tuple[list[dict[str, Any]], Any | None]:
@@ -310,8 +304,6 @@ class ChatterActionPlanner:
action_data={},
action_message=None,
)
# 检查是否需要退出Normal模式
await self._check_exit_normal_mode(context)
return [asdict(no_action)], None
# 2. 检查是否有消息达到reply阈值
@@ -341,8 +333,6 @@ class ChatterActionPlanner:
action_data={},
action_message=None,
)
# 检查是否需要退出Normal模式
await self._check_exit_normal_mode(context)
return [asdict(no_action)], None
# 记录当前正在处理的消息ID
@@ -387,9 +377,6 @@ class ChatterActionPlanner:
context.processing_message_id = None
logger.debug("Normal模式 - 已清理处理标记")
# 8. 检查是否需要退出Normal模式
await self._check_exit_normal_mode(context)
# respond动作不返回目标消息因为它是统一回应所有未读消息
return [asdict(respond_action)], None
else:
@@ -406,25 +393,19 @@ class ChatterActionPlanner:
# 更新连续不回复计数
await self._update_interest_calculator_state(replied=False)
# 检查是否需要退出Normal模式
await self._check_exit_normal_mode(context)
return [asdict(no_action)], None
except asyncio.CancelledError:
logger.info(f"Normal模式流程被取消: {self.chat_id}")
self.planner_stats["failed_plans"] += 1
# 清理处理标记
if context:
context.processing_message_id = None
raise
except Exception as e:
logger.error(f"Normal模式 - 流程出错: {e}")
self.planner_stats["failed_plans"] += 1
# 清理处理标记
if context:
context.processing_message_id = None
return [], None
finally:
# 检查是否需要退出Normal模式
await self._check_exit_normal_mode(context)
async def _check_enter_normal_mode(self, context: "StreamContext | None") -> None:
"""检查并执行进入Normal模式的判定