refactor(chat): 优化任务管理机制支持多重回复

重构聊天管理器的任务处理系统,将单一任务追踪改为支持多重回复的任务列表管理。

主要变更:
- 将 `_processing_tasks` 从单任务字典改为任务列表字典
- 新增 `add_processing_task` 和 `get_all_processing_tasks` 方法
- 增强 `cancel_all_stream_tasks` 方法支持批量取消
- 修复消息打断机制,确保取消所有相关任务
- 优化任务清理逻辑,自动移除已完成任务

这些改进使系统能够更好地处理并发回复场景,提高任务管理的灵活性和可靠性。
This commit is contained in:
Windpicker-owo
2025-10-07 17:10:26 +08:00
parent 37b13c3a42
commit ee51976095
3 changed files with 107 additions and 41 deletions

View File

@@ -5,7 +5,7 @@ PlanExecutor: 接收 Plan 对象并执行其中的所有动作。
import asyncio
import time
from typing import Any
from src.chat.planner_actions.action_manager import ChatterActionManager
from src.common.data_models.info_data_model import ActionPlannerInfo, Plan
from src.common.logger import get_logger
@@ -51,7 +51,7 @@ class ChatterPlanExecutor:
"""设置关系追踪器"""
self.relationship_tracker = relationship_tracker
async def execute(self, plan: Plan) -> dict[str, any]:
async def execute(self, plan: Plan) -> dict[str, Any]:
"""
遍历并执行Plan对象中`decided_actions`列表里的所有动作。
@@ -119,7 +119,7 @@ class ChatterPlanExecutor:
"results": execution_results,
}
async def _execute_reply_actions(self, reply_actions: list[ActionPlannerInfo], plan: Plan) -> dict[str, any]:
async def _execute_reply_actions(self, reply_actions: list[ActionPlannerInfo], plan: Plan) -> dict[str, Any]:
"""串行执行所有回复动作,增加去重逻辑,避免对同一消息多次回复"""
results = []
@@ -171,7 +171,7 @@ class ChatterPlanExecutor:
async def _execute_single_reply_action(
self, action_info: ActionPlannerInfo, plan: Plan, clear_unread: bool = True
) -> dict[str, any]:
) -> dict[str, Any]:
"""执行单个回复动作"""
start_time = time.time()
success = False
@@ -249,7 +249,7 @@ class ChatterPlanExecutor:
else reply_content,
}
async def _execute_other_actions(self, other_actions: list[ActionPlannerInfo], plan: Plan) -> dict[str, any]:
async def _execute_other_actions(self, other_actions: list[ActionPlannerInfo], plan: Plan) -> dict[str, Any]:
"""执行其他动作"""
results = []
@@ -278,7 +278,7 @@ class ChatterPlanExecutor:
return {"results": results}
async def _execute_single_other_action(self, action_info: ActionPlannerInfo, plan: Plan) -> dict[str, any]:
async def _execute_single_other_action(self, action_info: ActionPlannerInfo, plan: Plan) -> dict[str, Any]:
"""执行单个其他动作"""
start_time = time.time()
success = False
@@ -387,7 +387,7 @@ class ChatterPlanExecutor:
logger.debug(f"action_message类型: {type(action_info.action_message)}")
logger.debug(f"action_message内容: {action_info.action_message}")
def get_execution_stats(self) -> dict[str, any]:
def get_execution_stats(self) -> dict[str, Any]:
"""获取执行统计信息"""
stats = self.execution_stats.copy()
@@ -418,7 +418,7 @@ class ChatterPlanExecutor:
"execution_times": [],
}
def get_recent_performance(self, limit: int = 10) -> list[dict[str, any]]:
def get_recent_performance(self, limit: int = 10) -> list[dict[str, Any]]:
"""获取最近的执行性能"""
recent_times = self.execution_stats["execution_times"][-limit:]
if not recent_times: