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:
@@ -51,7 +51,7 @@ class ChatterActionManager:
|
||||
chat_stream: ChatStream,
|
||||
log_prefix: str,
|
||||
shutting_down: bool = False,
|
||||
action_message: dict | None = None,
|
||||
action_message: DatabaseMessages | None = None,
|
||||
) -> BaseAction | None:
|
||||
"""
|
||||
创建动作处理器实例
|
||||
@@ -143,7 +143,7 @@ class ChatterActionManager:
|
||||
self,
|
||||
action_name: str,
|
||||
chat_id: str,
|
||||
target_message: dict | DatabaseMessages | None = None,
|
||||
target_message: DatabaseMessages | None = None,
|
||||
reasoning: str = "",
|
||||
action_data: dict | None = None,
|
||||
thinking_id: str | None = None,
|
||||
@@ -264,10 +264,8 @@ class ChatterActionManager:
|
||||
)
|
||||
if not success or not response_set:
|
||||
# 安全地获取 processed_plain_text
|
||||
if isinstance(target_message, DatabaseMessages):
|
||||
if target_message:
|
||||
msg_text = target_message.processed_plain_text or "未知消息"
|
||||
elif target_message:
|
||||
msg_text = target_message.get("processed_plain_text", "未知消息")
|
||||
else:
|
||||
msg_text = "未知消息"
|
||||
|
||||
@@ -336,10 +334,7 @@ class ChatterActionManager:
|
||||
# 获取目标消息ID
|
||||
target_message_id = None
|
||||
if target_message:
|
||||
if isinstance(target_message, DatabaseMessages):
|
||||
target_message_id = target_message.message_id
|
||||
elif isinstance(target_message, dict):
|
||||
target_message_id = target_message.get("message_id")
|
||||
target_message_id = target_message.message_id
|
||||
elif action_data and isinstance(action_data, dict):
|
||||
target_message_id = action_data.get("target_message_id")
|
||||
|
||||
@@ -508,14 +503,12 @@ class ChatterActionManager:
|
||||
person_info_manager = get_person_info_manager()
|
||||
|
||||
# 获取 platform,如果不存在则从 chat_stream 获取,如果还是 None 则使用默认值
|
||||
if isinstance(action_message, DatabaseMessages):
|
||||
if action_message:
|
||||
platform = action_message.chat_info.platform
|
||||
user_id = action_message.user_info.user_id
|
||||
else:
|
||||
platform = action_message.get("chat_info_platform")
|
||||
if platform is None:
|
||||
platform = getattr(chat_stream, "platform", "unknown")
|
||||
user_id = action_message.get("user_id", "")
|
||||
platform = getattr(chat_stream, "platform", "unknown")
|
||||
user_id = ""
|
||||
|
||||
# 获取用户信息并生成回复提示
|
||||
person_id = person_info_manager.get_person_id(
|
||||
@@ -593,11 +586,8 @@ class ChatterActionManager:
|
||||
# 根据新消息数量决定是否需要引用回复
|
||||
reply_text = ""
|
||||
# 检查是否为主动思考消息
|
||||
if isinstance(message_data, DatabaseMessages):
|
||||
# DatabaseMessages 对象没有 message_type 字段,默认为 False
|
||||
is_proactive_thinking = False
|
||||
elif message_data:
|
||||
is_proactive_thinking = message_data.get("message_type") == "proactive_thinking"
|
||||
if message_data:
|
||||
is_proactive_thinking = getattr(message_data, "message_type", None) == "proactive_thinking"
|
||||
else:
|
||||
is_proactive_thinking = True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user