refactor(chat): 移除 ChatStream 的历史消息自动加载功能

移除 ChatStream 初始化时的 `_load_history_messages()` 调用,改为按需异步加载历史消息。这解决了启动时阻塞事件循环的问题,并提高了聊天流初始化的性能。

主要变更:
- 删除 `ChatStream._load_history_messages()` 方法及相关代码
- 将多个模块中的同步数据库查询函数改为异步版本
- 修复相关调用处的异步调用方式
- 优化图片描述查询的错误处理

BREAKING CHANGE: `get_raw_msg_before_timestamp_with_chat` 和相关消息查询函数现在改为异步操作,需要调用处使用 await
This commit is contained in:
Windpicker-owo
2025-09-28 21:31:49 +08:00
parent fd76e36320
commit 28bce19d27
12 changed files with 32 additions and 156 deletions

View File

@@ -329,11 +329,11 @@ class ChatterPlanFilter:
stream_context = chat_stream.context_manager
# 获取真正的已读和未读消息
read_messages = stream_context.history_messages # 已读消息存储在history_messages中
read_messages = stream_context.context.history_messages # 已读消息存储在history_messages中
if not read_messages:
from src.common.data_models.database_data_model import DatabaseMessages
# 如果内存中没有已读消息(比如刚启动),则从数据库加载最近的上下文
fallback_messages_dicts = get_raw_msg_before_timestamp_with_chat(
fallback_messages_dicts = await get_raw_msg_before_timestamp_with_chat(
chat_id=plan.chat_id,
timestamp=time.time(),
limit=global_config.chat.max_context_size,

View File

@@ -124,7 +124,7 @@ class ChatterPlanGenerator:
"""
try:
# 获取最近的消息记录
raw_messages = get_raw_msg_before_timestamp_with_chat(
raw_messages = await get_raw_msg_before_timestamp_with_chat(
chat_id=self.chat_id, timestamp=time.time(), limit=global_config.memory.short_memory_length
)

View File

@@ -331,7 +331,6 @@ class NoticeHandler:
like_emoji_id = raw_message.get("likes")[0].get("emoji_id")
await event_manager.trigger_event(
<<<<<<< HEAD
NapcatEvent.ON_RECEIVED.EMOJI_LIEK,
permission_group=PLUGIN_NAME,
group_id=group_id,
@@ -343,16 +342,6 @@ class NoticeHandler:
type="text",
data=f"{user_name}使用Emoji表情{QQ_FACE.get(like_emoji_id, '')}回复了你的消息[{target_message_text}]",
)
=======
NapcatEvent.ON_RECEIVED.EMOJI_LIEK,
permission_group=PLUGIN_NAME,
group_id=group_id,
user_id=user_id,
message_id=raw_message.get("message_id",""),
emoji_id=like_emoji_id
)
seg_data = Seg(type="text",data=f"{user_name}使用Emoji表情{QQ_FACE.get(like_emoji_id, '')}回复了你的消息[{target_message_text}]")
>>>>>>> 9912d7f643d347cbadcf1e3d618aa78bcbf89cc4
return seg_data, user_info
async def handle_ban_notify(self, raw_message: dict, group_id: int) -> Tuple[Seg, UserInfo] | Tuple[None, None]: