fix(chatter): 修复计划生成和后台任务处理中的多项问题

此次更新包含对 `affinity_flow_chatter` 插件的多项修复和健壮性改进:

1.  **计划生成器 (`plan_generator.py`)**:
    *   修复了 `target_info` 未能正确实例化为 `TargetPersonInfo` 对象的问题,现在可以确保类型安全。
    *   增加了对未知聊天类型的处理,将其默认为私聊,增强了鲁棒性。
    *   将获取聊天记录的上限从 `short_memory_length` 调整为更通用的 `max_context_size`。

2.  **规划器 (`planner.py`)**:
    *   引入了后台任务管理机制,确保由 `_commit_interest_updates` 创建的异步任务能够被追踪。
    *   增加了 `_handle_task_result` 回调函数,用于捕获和记录后台任务中可能出现的异常,防止任务静默失败并提高系统的可维护性。
This commit is contained in:
minecraft1024a
2025-10-19 14:01:51 +08:00
parent eb4a48d45a
commit b4ad2daff4
2 changed files with 28 additions and 4 deletions

View File

@@ -9,7 +9,7 @@ from src.chat.utils.utils import get_chat_type_and_target_info
from src.common.data_models.database_data_model import DatabaseMessages
from src.common.data_models.info_data_model import Plan, TargetPersonInfo
from src.config.config import global_config
from src.plugin_system.base.component_types import ActionInfo, ChatMode, ChatType
from src.plugin_system.base.component_types import ActionInfo, ChatMode, ChatType, ComponentType
from src.plugin_system.core.component_registry import component_registry
@@ -55,6 +55,11 @@ class ChatterPlanGenerator:
try:
# 获取聊天类型和目标信息
chat_type, target_info = await get_chat_type_and_target_info(self.chat_id)
if chat_type:
chat_type = ChatType.GROUP
else:
#遇到未知类型也当私聊处理
chat_type = ChatType.PRIVATE
# 获取可用动作列表
available_actions = await self._get_available_actions(chat_type, mode)
@@ -62,12 +67,16 @@ class ChatterPlanGenerator:
# 获取聊天历史记录
recent_messages = await self._get_recent_messages()
# 构建计划对象
# 使用 target_info 字典创建 TargetPersonInfo 实例
target_person_info = TargetPersonInfo(**target_info) if target_info else TargetPersonInfo()
# 构建计划对象
plan = Plan(
chat_id=self.chat_id,
chat_type=chat_type,
mode=mode,
target_info=target_info,
target_info=target_person_info,
available_actions=available_actions,
chat_history=recent_messages,
)
@@ -77,6 +86,7 @@ class ChatterPlanGenerator:
except Exception:
# 如果生成失败,返回一个基本的空计划
return Plan(
chat_type = ChatType.PRIVATE,#空计划默认当成私聊
chat_id=self.chat_id,
mode=mode,
target_info=TargetPersonInfo(),
@@ -124,7 +134,7 @@ class ChatterPlanGenerator:
try:
# 获取最近的消息记录
raw_messages = await get_raw_msg_before_timestamp_with_chat(
chat_id=self.chat_id, timestamp=time.time(), limit=global_config.memory.short_memory_length
chat_id=self.chat_id, timestamp=time.time(), limit=global_config.chat.max_context_size
)
# 转换为 DatabaseMessages 对象

View File

@@ -70,6 +70,7 @@ class ChatterActionPlanner:
"replies_generated": 0,
"other_actions_executed": 0,
}
self._background_tasks: set[asyncio.Task] = set()
async def plan(self, context: "StreamContext | None" = None) -> tuple[list[dict[str, Any]], Any | None]:
"""
@@ -157,7 +158,9 @@ class ChatterActionPlanner:
)
if interest_updates:
asyncio.create_task(self._commit_interest_updates(interest_updates))
task = asyncio.create_task(self._commit_interest_updates(interest_updates))
self._background_tasks.add(task)
task.add_done_callback(self._handle_task_result)
# 检查兴趣度是否达到非回复动作阈值
non_reply_action_interest_threshold = global_config.affinity_flow.non_reply_action_interest_threshold
@@ -266,6 +269,17 @@ class ChatterActionPlanner:
return final_actions_dict, final_target_message_dict
def _handle_task_result(self, task: asyncio.Task) -> None:
"""处理后台任务的结果,记录异常。"""
try:
task.result()
except asyncio.CancelledError:
pass # 任务被取消是正常现象
except Exception as e:
logger.error(f"后台任务执行失败: {e}", exc_info=True)
finally:
self._background_tasks.discard(task)
def get_planner_stats(self) -> dict[str, Any]:
"""获取规划器统计"""
return self.planner_stats.copy()