fix(chat): 修复计划过滤器和消息构建器中的潜在空指针问题
- 在 `plan_filter.py` 中,当LLM未指定 `target_message_id` 时,明确将目标消息设置为最新的消息,避免后续操作因 `target_message_dict` 未定义而出错。 - 在 `chat_message_builder.py` 中,为 `replace_user_references_sync` 函数增加了对 `content` 为空的检查,防止后续处理引发异常。 - 将数据库查询从 `.scalar()` 改为 `.scalar_one_or_none()`,以更清晰地处理未找到结果的情况。
This commit is contained in:
committed by
Windpicker-owo
parent
b9f8f9f967
commit
8d8cae8c10
@@ -233,11 +233,13 @@ class PlanFilter:
|
||||
if action not in ["no_action", "no_reply", "do_nothing", "proactive_reply"]:
|
||||
if target_message_id := action_json.get("target_message_id"):
|
||||
target_message_dict = self._find_message_by_id(target_message_id, message_id_list)
|
||||
if target_message_dict is None:
|
||||
target_message_dict = self._get_latest_message(message_id_list)
|
||||
if target_message_dict:
|
||||
from src.common.data_models.database_data_model import DatabaseMessages
|
||||
target_message_obj = DatabaseMessages(**target_message_dict)
|
||||
else:
|
||||
# 如果LLM没有指定target_message_id,我们就默认选择最新的一条消息
|
||||
target_message_dict = self._get_latest_message(message_id_list)
|
||||
|
||||
if target_message_dict:
|
||||
from src.common.data_models.database_data_model import DatabaseMessages
|
||||
target_message_obj = DatabaseMessages(**target_message_dict)
|
||||
|
||||
available_action_names = list(plan.available_actions.keys())
|
||||
if action not in ["no_action", "no_reply", "reply", "do_nothing", "proactive_reply"] and action not in available_action_names:
|
||||
|
||||
@@ -35,6 +35,9 @@ def replace_user_references_sync(
|
||||
Returns:
|
||||
str: 处理后的内容字符串
|
||||
"""
|
||||
if not content:
|
||||
return ""
|
||||
|
||||
if name_resolver is None:
|
||||
def default_resolver(platform: str, user_id: str) -> str:
|
||||
# 检查是否是机器人自己
|
||||
@@ -812,8 +815,8 @@ def build_pic_mapping_info(pic_id_mapping: Dict[str, str]) -> str:
|
||||
description = "[图片内容未知]" # 默认描述
|
||||
try:
|
||||
with get_db_session() as session:
|
||||
image = session.execute(select(Images).where(Images.image_id == pic_id)).scalar()
|
||||
if image and image.description:
|
||||
image = session.execute(select(Images).where(Images.image_id == pic_id)).scalar_one_or_none()
|
||||
if image and image.description: # type: ignore
|
||||
description = image.description
|
||||
except Exception:
|
||||
# 如果查询失败,保持默认描述
|
||||
|
||||
Reference in New Issue
Block a user