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 c31fd02daf
commit ae0c2704d1
12 changed files with 27 additions and 151 deletions

View File

@@ -12,6 +12,8 @@ from src.person_info.person_info import PersonInfoManager, get_person_info_manag
from src.chat.utils.utils import translate_timestamp_to_human_readable, assign_message_ids
from src.common.database.sqlalchemy_database_api import get_db_session
from sqlalchemy import select, and_
from src.common.logger import get_logger
logger = get_logger("chat_message_builder")
install(extra_lines=3)
@@ -830,10 +832,11 @@ async def build_pic_mapping_info(pic_id_mapping: Dict[str, str]) -> str:
async with get_db_session() as session:
result = await session.execute(select(Images).where(Images.image_id == pic_id))
image = result.scalar_one_or_none()
if image and image.description: # type: ignore
if image and hasattr(image, 'description') and image.description:
description = image.description
except Exception:
except Exception as e:
# 如果查询失败,保持默认描述
logger.debug(f"[chat_message_builder] 查询图片描述失败: {e}")
pass
mapping_lines.append(f"[{display_name}] 的内容:{description}")