feat(chat): 统一消息格式信息处理逻辑
在多个消息处理模块中统一了 format_info 的处理方式,确保适配器支持的消息类型能够正确传递给 action_modifier: - 在 bot.py、chat_stream.py、optimized_chat_stream.py 中新增 _prepare_additional_config 方法 - 将 format_info 嵌入到 additional_config 中,确保数据库存储一致性 - 增强 action_modifier 中的适配器类型检查逻辑,添加更详细的错误日志 - 修复 storage.py 中的 additional_config 处理逻辑,避免覆盖原始配置 这些改进确保了 Action 能够正确检查适配器支持的消息类型,避免因缺少 format_info 导致的类型检查失败。
This commit is contained in:
@@ -223,12 +223,32 @@ class ActionModifier:
|
||||
list[str]: 支持的输出类型列表
|
||||
"""
|
||||
# 检查additional_config是否存在且不为空
|
||||
if (chat_context.current_message
|
||||
and hasattr(chat_context.current_message, "additional_config")
|
||||
and chat_context.current_message.additional_config):
|
||||
additional_config = None
|
||||
has_additional_config = False
|
||||
|
||||
# 先检查 current_message 是否存在
|
||||
if not chat_context.current_message:
|
||||
logger.warning(f"{self.log_prefix} [问题] chat_context.current_message 为 None,无法获取适配器支持的类型")
|
||||
return ["text", "emoji"] # 返回基础类型
|
||||
|
||||
if hasattr(chat_context.current_message, "additional_config"):
|
||||
additional_config = chat_context.current_message.additional_config
|
||||
|
||||
# 更准确的非空判断
|
||||
if additional_config is not None:
|
||||
if isinstance(additional_config, str) and additional_config.strip():
|
||||
has_additional_config = True
|
||||
elif isinstance(additional_config, dict):
|
||||
# 字典存在就可以,即使为空也可能有format_info字段
|
||||
has_additional_config = True
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} [问题] current_message 没有 additional_config 属性")
|
||||
|
||||
logger.debug(f"{self.log_prefix} [调试] has_additional_config: {has_additional_config}")
|
||||
|
||||
if has_additional_config:
|
||||
try:
|
||||
additional_config = chat_context.current_message.additional_config
|
||||
logger.debug(f"{self.log_prefix} [调试] 开始解析 additional_config")
|
||||
format_info = None
|
||||
|
||||
# 处理additional_config可能是字符串或字典的情况
|
||||
@@ -237,8 +257,7 @@ class ActionModifier:
|
||||
try:
|
||||
config = orjson.loads(additional_config)
|
||||
format_info = config.get("format_info")
|
||||
except (orjson.JSONDecodeError, AttributeError, TypeError):
|
||||
logger.debug("无法解析additional_config JSON字符串")
|
||||
except (orjson.JSONDecodeError, AttributeError, TypeError) as e:
|
||||
format_info = None
|
||||
|
||||
elif isinstance(additional_config, dict):
|
||||
@@ -247,7 +266,6 @@ class ActionModifier:
|
||||
|
||||
# 如果找到了format_info,从中提取支持的类型
|
||||
if format_info:
|
||||
# 优先检查accept_format字段
|
||||
if "accept_format" in format_info:
|
||||
accept_format = format_info["accept_format"]
|
||||
if isinstance(accept_format, str):
|
||||
@@ -258,11 +276,13 @@ class ActionModifier:
|
||||
accept_format = list(accept_format) if hasattr(accept_format, "__iter__") else []
|
||||
|
||||
# 合并基础类型和适配器特定类型
|
||||
return list(set(accept_format))
|
||||
result = list(set(accept_format))
|
||||
return result
|
||||
|
||||
# 备用检查content_format字段
|
||||
elif "content_format" in format_info:
|
||||
content_format = format_info["content_format"]
|
||||
logger.debug(f"{self.log_prefix} [调试] 找到 content_format: {content_format}")
|
||||
if isinstance(content_format, str):
|
||||
content_format = [content_format]
|
||||
elif isinstance(content_format, list):
|
||||
@@ -270,10 +290,25 @@ class ActionModifier:
|
||||
else:
|
||||
content_format = list(content_format) if hasattr(content_format, "__iter__") else []
|
||||
|
||||
return list(set(content_format))
|
||||
|
||||
result = list(set(content_format))
|
||||
return result
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} [问题] additional_config 中没有 format_info 字段")
|
||||
except Exception as e:
|
||||
logger.debug(f"解析适配器格式信息失败,使用默认支持类型: {e}")
|
||||
logger.error(f"{self.log_prefix} [问题] 解析适配器格式信息失败: {e}", exc_info=True)
|
||||
else:
|
||||
logger.warning(f"{self.log_prefix} [问题] additional_config 不存在或为空")
|
||||
|
||||
# 如果无法获取格式信息,返回默认支持的基础类型
|
||||
default_types = ["text", "emoji"]
|
||||
logger.warning(
|
||||
f"{self.log_prefix} [问题] 无法从适配器获取支持的消息类型,使用默认类型: {default_types}"
|
||||
)
|
||||
logger.warning(
|
||||
f"{self.log_prefix} [问题] 这可能导致某些 Action 被错误地过滤。"
|
||||
f"请检查适配器是否正确设置了 format_info。"
|
||||
)
|
||||
return default_types
|
||||
|
||||
|
||||
async def _get_deactivated_actions_by_type(
|
||||
|
||||
Reference in New Issue
Block a user