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:
minecraft1024a
2025-09-13 14:46:50 +08:00
committed by Windpicker-owo
parent b9f8f9f967
commit 8d8cae8c10
2 changed files with 12 additions and 7 deletions

View File

@@ -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:
# 如果查询失败,保持默认描述