refactor(core): 统一消息对象类型并增强代码健壮性
本次提交对多个核心模块进行了重构和修复,主要目标是统一内部消息对象的类型为 `DatabaseMessages`,并增加多处空值检查和类型注解,以提升代码的健壮性和可维护性。
- **统一消息类型**: 在 `action_manager` 中,将 `action_message` 和 `target_message` 的类型注解和处理逻辑统一为 `DatabaseMessages`,消除了对 `dict` 类型的兼容代码,使逻辑更清晰。
- **增强健壮性**:
- 在 `permission_api` 中,为所有对外方法增加了对 `_permission_manager` 未初始化时的空值检查,防止在管理器未就绪时调用引发异常。
- 在 `chat_api` 和 `cross_context_api` 中,增加了对 `stream.user_info` 的存在性检查,避免在私聊场景下 `user_info` 为空时导致 `AttributeError`。
- **类型修复**: 修正了 `action_modifier` 和 `plugin_base` 中的类型注解错误,并解决了 `action_modifier` 中因 `chat_stream` 未初始化可能导致的潜在问题。
- **代码简化**: 移除了 `action_manager` 中因兼容 `dict` 类型而产生的冗余代码分支,使逻辑更直接。
This commit is contained in:
@@ -2,9 +2,9 @@ import asyncio
|
||||
import hashlib
|
||||
import random
|
||||
import time
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from src.chat.message_receive.chat_stream import ChatStream, get_chat_manager
|
||||
from src.chat.planner_actions.action_manager import ChatterActionManager
|
||||
from src.chat.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat
|
||||
from src.common.data_models.message_manager_data_model import StreamContext
|
||||
@@ -32,7 +32,7 @@ class ActionModifier:
|
||||
"""初始化动作处理器"""
|
||||
self.chat_id = chat_id
|
||||
# chat_stream 和 log_prefix 将在异步方法中初始化
|
||||
self.chat_stream = None # type: ignore
|
||||
self.chat_stream: ChatStream | None = None
|
||||
self.log_prefix = f"[{chat_id}]"
|
||||
|
||||
self.action_manager = action_manager
|
||||
@@ -111,7 +111,7 @@ class ActionModifier:
|
||||
logger.debug(f"{self.log_prefix} - 移除 {action_name}: {reason}")
|
||||
|
||||
message_list_before_now_half = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=self.chat_stream.stream_id,
|
||||
chat_id=self.chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=min(int(global_config.chat.max_context_size * 0.33), 10),
|
||||
)
|
||||
@@ -137,6 +137,9 @@ class ActionModifier:
|
||||
logger.debug(f"{self.log_prefix}阶段一移除动作: {disabled_action_name},原因: 用户自行禁用")
|
||||
|
||||
# === 第二阶段:检查动作的关联类型 ===
|
||||
if not self.chat_stream:
|
||||
logger.error(f"{self.log_prefix} chat_stream 未初始化,无法执行第二阶段")
|
||||
return
|
||||
chat_context = self.chat_stream.context_manager.context
|
||||
current_actions_s2 = self.action_manager.get_using_actions()
|
||||
type_mismatched_actions = self._check_action_associated_types(current_actions_s2, chat_context)
|
||||
@@ -209,6 +212,7 @@ class ActionModifier:
|
||||
deactivated_actions = []
|
||||
|
||||
# 获取 Action 类注册表
|
||||
from src.plugin_system.base.base_action import BaseAction
|
||||
from src.plugin_system.base.component_types import ComponentType
|
||||
from src.plugin_system.core.component_registry import component_registry
|
||||
|
||||
@@ -232,15 +236,13 @@ class ActionModifier:
|
||||
try:
|
||||
# 创建一个最小化的实例
|
||||
action_instance = object.__new__(action_class)
|
||||
# 使用 cast 来“欺骗”类型检查器
|
||||
action_instance = cast(BaseAction, action_instance)
|
||||
# 设置必要的属性
|
||||
action_instance.action_name = action_name
|
||||
action_instance.log_prefix = self.log_prefix
|
||||
# 设置聊天内容,用于激活判断
|
||||
action_instance._activation_chat_content = chat_content
|
||||
|
||||
# 调用 go_activate 方法(不再需要传入 chat_content)
|
||||
# 调用 go_activate 方法
|
||||
task = action_instance.go_activate(
|
||||
llm_judge_model=self.llm_judge,
|
||||
llm_judge_model=self.llm_judge
|
||||
)
|
||||
activation_tasks.append(task)
|
||||
task_action_names.append(action_name)
|
||||
|
||||
Reference in New Issue
Block a user