refactor(replyer): 将 reply_message 类型从 dict 更改为 Messages 模型

将 `DefaultReplyer` 中多个方法的 `reply_message` 参数类型从通用的 `dict` 更改为更具体的 SQLAlchemy 模型 `Messages`。

这一更改提高了代码的类型安全性和可读性,使得数据结构更加明确。同时,更新了相关代码以直接访问模型属性,而不是使用字典的 `get()` 方法。
This commit is contained in:
minecraft1024a
2025-10-12 14:03:20 +08:00
committed by Windpicker-owo
parent 804af4d8be
commit f90fab8790

View File

@@ -26,6 +26,7 @@ from src.chat.utils.memory_mappings import get_memory_type_chinese_label
from src.chat.utils.prompt import Prompt, PromptParameters, global_prompt_manager
from src.chat.utils.timer_calculator import Timer
from src.chat.utils.utils import get_chat_type_and_target_info
from src.common.database.sqlalchemy_models import Messages
from src.common.logger import get_logger
from src.config.config import global_config, model_config
from src.individuality.individuality import get_individuality
@@ -264,7 +265,7 @@ class DefaultReplyer:
enable_tool: bool = True,
from_plugin: bool = True,
stream_id: str | None = None,
reply_message: dict[str, Any] | None = None,
reply_message: Messages | None = None,
) -> tuple[bool, dict[str, Any] | None, str | None]:
# sourcery skip: merge-nested-ifs
"""
@@ -1149,7 +1150,7 @@ class DefaultReplyer:
extra_info: str = "",
available_actions: dict[str, ActionInfo] | None = None,
enable_tool: bool = True,
reply_message: dict[str, Any] | None = None,
reply_message: Messages | None = None,
) -> str:
"""
构建回复器上下文
@@ -1193,10 +1194,10 @@ class DefaultReplyer:
if reply_message is None:
logger.warning("reply_message 为 None无法构建prompt")
return ""
platform = reply_message.get("chat_info_platform")
platform = reply_message.chat_info_platform
person_id = person_info_manager.get_person_id(
platform, # type: ignore
reply_message.get("user_id"), # type: ignore
reply_message.user_id, # type: ignore
)
person_name = await person_info_manager.get_value(person_id, "person_name")
@@ -1205,41 +1206,22 @@ class DefaultReplyer:
# 尝试从reply_message获取用户名
await person_info_manager.first_knowing_some_one(
platform, # type: ignore
reply_message.get("user_id"), # type: ignore
reply_message.get("user_nickname") or "",
reply_message.get("user_cardname") or "",
reply_message.user_id, # type: ignore
reply_message.user_nickname or "",
reply_message.user_cardname or "",
)
# 检查是否是bot自己的名字如果是则替换为"(你)"
bot_user_id = str(global_config.bot.qq_account)
current_user_id = await person_info_manager.get_value(person_id, "user_id")
current_platform = reply_message.get("chat_info_platform")
current_platform = reply_message.chat_info_platform
if current_user_id == bot_user_id and current_platform == global_config.bot.platform:
sender = f"{person_name}(你)"
else:
try:
recent_msgs = await get_raw_msg_before_timestamp_with_chat(
chat_id=chat_id,
timestamp=time.time(),
limit= max(10, int(global_config.chat.max_context_size * 0.5)),
)
# 从最近到更早遍历找第一条不是bot的
for m in reversed(recent_msgs):
if str(m.get("user_id")) != bot_user_id:
candidate_msg = m
break
except Exception as e:
logger.error(f"获取最近消息失败: {e}")
if not candidate_msg:
logger.debug("未找到可作为目标的非bot消息静默不回复。")
return ""
platform = candidate_msg.get("chat_info_platform") or self.chat_stream.platform
person_id = person_info_manager.get_person_id(platform, candidate_msg.get("user_id"))
person_info = await person_info_manager.get_values(person_id, ["person_name", "user_id"]) if person_id else {}
person_name = person_info.get("person_name") or candidate_msg.get("user_nickname") or candidate_msg.get("user_id") or "未知用户"
sender = person_name
target = candidate_msg.get("processed_plain_text") or candidate_msg.get("raw_message") or ""
# 如果不是bot自己直接使用person_name
sender = person_name
target = reply_message.processed_plain_text
# 最终的空值检查确保sender和target不为None
if sender is None:
@@ -1586,7 +1568,7 @@ class DefaultReplyer:
raw_reply: str,
reason: str,
reply_to: str,
reply_message: dict[str, Any] | None = None,
reply_message: Messages | None = None,
) -> str: # sourcery skip: merge-else-if-into-elif, remove-redundant-if
await self._async_init()
chat_stream = self.chat_stream
@@ -1594,8 +1576,8 @@ class DefaultReplyer:
is_group_chat = self.is_group_chat
if reply_message:
sender = reply_message.get("sender")
target = reply_message.get("target")
sender = reply_message.user_nickname or reply_message.user_cardname
target = reply_message.processed_plain_text
else:
sender, target = self._parse_reply_target(reply_to)