fix(chat): 修复 plan executor 对嵌套 user_info 数据结构的解析兼容性
`action_message` 可能以对象或字典的形式出现,且用户信息统一嵌套在 `user_info` 字段下。 旧代码在处理字典格式时,未能正确处理此嵌套结构,导致无法正确解析用户信息。本次修改统一了逻辑,确保在两种情况下都能稳定地从 `user_info` 中提取用户ID和昵称,增强了代码的健壮性。
This commit is contained in:
@@ -126,7 +126,13 @@ class PlanExecutor:
|
|||||||
try:
|
try:
|
||||||
logger.info(f"执行回复动作: {action_info.action_type}, 原因: {action_info.reasoning}")
|
logger.info(f"执行回复动作: {action_info.action_type}, 原因: {action_info.reasoning}")
|
||||||
|
|
||||||
if action_info.action_message.user_info.user_id == str(global_config.bot.qq_account):
|
# 获取用户ID - 兼容对象和字典
|
||||||
|
if hasattr(action_info.action_message, "user_info"):
|
||||||
|
user_id = action_info.action_message.user_info.user_id
|
||||||
|
else:
|
||||||
|
user_id = action_info.action_message.get("user_info", {}).get("user_id")
|
||||||
|
|
||||||
|
if user_id == str(global_config.bot.qq_account):
|
||||||
logger.warning("尝试回复自己,跳过此动作以防止死循环。")
|
logger.warning("尝试回复自己,跳过此动作以防止死循环。")
|
||||||
return {
|
return {
|
||||||
"action_type": action_info.action_type,
|
"action_type": action_info.action_type,
|
||||||
@@ -246,15 +252,17 @@ class PlanExecutor:
|
|||||||
return
|
return
|
||||||
|
|
||||||
# 获取用户信息 - 处理对象和字典两种情况
|
# 获取用户信息 - 处理对象和字典两种情况
|
||||||
if hasattr(action_info.action_message, "user_id"):
|
if hasattr(action_info.action_message, "user_info"):
|
||||||
# 对象情况
|
# 对象情况
|
||||||
user_id = action_info.action_message.user_id
|
user_info = action_info.action_message.user_info
|
||||||
user_name = getattr(action_info.action_message, "user_nickname", user_id) or user_id
|
user_id = user_info.user_id
|
||||||
user_message = getattr(action_info.action_message, "content", "")
|
user_name = user_info.user_nickname or user_id
|
||||||
|
user_message = action_info.action_message.content
|
||||||
else:
|
else:
|
||||||
# 字典情况
|
# 字典情况
|
||||||
user_id = action_info.action_message.get("user_id", "")
|
user_info = action_info.action_message.get("user_info", {})
|
||||||
user_name = action_info.action_message.get("user_nickname", user_id) or user_id
|
user_id = user_info.get("user_id")
|
||||||
|
user_name = user_info.get("user_nickname") or user_id
|
||||||
user_message = action_info.action_message.get("content", "")
|
user_message = action_info.action_message.get("content", "")
|
||||||
|
|
||||||
if not user_id:
|
if not user_id:
|
||||||
|
|||||||
Reference in New Issue
Block a user