refactor(message-manager): 删除自适应流管理器并优化上下文加载
-从distribution_manager和message_manager中删除adaptive_stream_manager.py及其集成 -删除过时的message_recv_backup.py,因为MessageRecv已被DatabaseMessages完全替换 -在context_manager中添加异步历史初始化,以便在启动时从数据库加载消息 -优化default_generator以使用内存中的消息,而不是重复的数据库查询 -修复message.py中的回复消息段处理,以处理DatabaseMessages格式 -从chat_stream.py中删除deepcopy并改进context_manager初始化 -清理bot.py和适配器响应方法中未使用的通知处理 突破性变化:自适应流管理系统已被完全删除。所有消息处理现在都依赖于DatabaseMessages格式。上下文管理器现在在创建时从数据库异步初始化历史记录。
This commit is contained in:
@@ -948,40 +948,24 @@ class DefaultReplyer:
|
||||
chat_stream = await chat_manager.get_stream(chat_id)
|
||||
if chat_stream:
|
||||
stream_context = chat_stream.context_manager
|
||||
# 使用真正的已读和未读消息
|
||||
read_messages = stream_context.context.history_messages # 已读消息
|
||||
|
||||
# 确保历史消息已从数据库加载
|
||||
await stream_context.ensure_history_initialized()
|
||||
|
||||
# 直接使用内存中的已读和未读消息,无需再查询数据库
|
||||
read_messages = stream_context.context.history_messages # 已读消息(已从数据库加载)
|
||||
unread_messages = stream_context.get_unread_messages() # 未读消息
|
||||
|
||||
# 构建已读历史消息 prompt
|
||||
read_history_prompt = ""
|
||||
# 总是从数据库加载历史记录,并与会话历史合并
|
||||
logger.info("正在从数据库加载上下文并与会话历史合并...")
|
||||
db_messages_raw = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=global_config.chat.max_context_size,
|
||||
)
|
||||
|
||||
# 合并和去重
|
||||
combined_messages = {}
|
||||
# 首先添加数据库消息
|
||||
for msg in db_messages_raw:
|
||||
if msg.get("message_id"):
|
||||
combined_messages[msg["message_id"]] = msg
|
||||
|
||||
# 然后用会话消息覆盖/添加,以确保它们是最新的
|
||||
for msg_obj in read_messages:
|
||||
msg_dict = msg_obj.flatten()
|
||||
if msg_dict.get("message_id"):
|
||||
combined_messages[msg_dict["message_id"]] = msg_dict
|
||||
|
||||
# 按时间排序
|
||||
sorted_messages = sorted(combined_messages.values(), key=lambda x: x.get("time", 0))
|
||||
|
||||
read_history_prompt = ""
|
||||
if sorted_messages:
|
||||
# 限制最终用于prompt的历史消息数量
|
||||
final_history = sorted_messages[-50:]
|
||||
if read_messages:
|
||||
# 将 DatabaseMessages 对象转换为字典格式,以便使用 build_readable_messages
|
||||
read_messages_dicts = [msg.flatten() for msg in read_messages]
|
||||
|
||||
# 按时间排序并限制数量
|
||||
sorted_messages = sorted(read_messages_dicts, key=lambda x: x.get("time", 0))
|
||||
final_history = sorted_messages[-50:] # 限制最多50条
|
||||
|
||||
read_content = await build_readable_messages(
|
||||
final_history,
|
||||
replace_bot_name=True,
|
||||
@@ -989,8 +973,10 @@ class DefaultReplyer:
|
||||
truncate=True,
|
||||
)
|
||||
read_history_prompt = f"这是已读历史消息,仅作为当前聊天情景的参考:\n{read_content}"
|
||||
logger.debug(f"使用内存中的 {len(final_history)} 条历史消息构建prompt")
|
||||
else:
|
||||
read_history_prompt = "暂无已读历史消息"
|
||||
logger.debug("内存中没有历史消息")
|
||||
|
||||
# 构建未读历史消息 prompt
|
||||
unread_history_prompt = ""
|
||||
@@ -1281,17 +1267,41 @@ class DefaultReplyer:
|
||||
action_descriptions += f"- {action_name}: {action_description}\n"
|
||||
action_descriptions += "\n"
|
||||
|
||||
message_list_before_now_long = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=global_config.chat.max_context_size * 1,
|
||||
)
|
||||
|
||||
message_list_before_short = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=int(global_config.chat.max_context_size * 0.33),
|
||||
)
|
||||
# 从内存获取历史消息,避免重复查询数据库
|
||||
from src.plugin_system.apis.chat_api import get_chat_manager
|
||||
|
||||
chat_manager = get_chat_manager()
|
||||
chat_stream_obj = await chat_manager.get_stream(chat_id)
|
||||
|
||||
if chat_stream_obj:
|
||||
# 确保历史消息已初始化
|
||||
await chat_stream_obj.context_manager.ensure_history_initialized()
|
||||
|
||||
# 获取所有消息(历史+未读)
|
||||
all_messages = (
|
||||
chat_stream_obj.context_manager.context.history_messages +
|
||||
chat_stream_obj.context_manager.get_unread_messages()
|
||||
)
|
||||
|
||||
# 转换为字典格式
|
||||
message_list_before_now_long = [msg.flatten() for msg in all_messages[-(global_config.chat.max_context_size * 2):]]
|
||||
message_list_before_short = [msg.flatten() for msg in all_messages[-int(global_config.chat.max_context_size * 0.33):]]
|
||||
|
||||
logger.debug(f"使用内存中的消息: long={len(message_list_before_now_long)}, short={len(message_list_before_short)}")
|
||||
else:
|
||||
# 回退到数据库查询
|
||||
logger.warning(f"无法获取chat_stream,回退到数据库查询: {chat_id}")
|
||||
message_list_before_now_long = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=global_config.chat.max_context_size * 2,
|
||||
)
|
||||
message_list_before_short = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=int(global_config.chat.max_context_size * 0.33),
|
||||
)
|
||||
|
||||
chat_talking_prompt_short = await build_readable_messages(
|
||||
message_list_before_short,
|
||||
replace_bot_name=True,
|
||||
@@ -1653,11 +1663,36 @@ class DefaultReplyer:
|
||||
else:
|
||||
mood_prompt = ""
|
||||
|
||||
message_list_before_now_half = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=min(int(global_config.chat.max_context_size * 0.33), 15),
|
||||
)
|
||||
# 从内存获取历史消息,避免重复查询数据库
|
||||
from src.plugin_system.apis.chat_api import get_chat_manager
|
||||
|
||||
chat_manager = get_chat_manager()
|
||||
chat_stream_obj = await chat_manager.get_stream(chat_id)
|
||||
|
||||
if chat_stream_obj:
|
||||
# 确保历史消息已初始化
|
||||
await chat_stream_obj.context_manager.ensure_history_initialized()
|
||||
|
||||
# 获取所有消息(历史+未读)
|
||||
all_messages = (
|
||||
chat_stream_obj.context_manager.context.history_messages +
|
||||
chat_stream_obj.context_manager.get_unread_messages()
|
||||
)
|
||||
|
||||
# 转换为字典格式,限制数量
|
||||
limit = min(int(global_config.chat.max_context_size * 0.33), 15)
|
||||
message_list_before_now_half = [msg.flatten() for msg in all_messages[-limit:]]
|
||||
|
||||
logger.debug(f"Rewrite使用内存中的 {len(message_list_before_now_half)} 条消息")
|
||||
else:
|
||||
# 回退到数据库查询
|
||||
logger.warning(f"无法获取chat_stream,回退到数据库查询: {chat_id}")
|
||||
message_list_before_now_half = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=chat_id,
|
||||
timestamp=time.time(),
|
||||
limit=min(int(global_config.chat.max_context_size * 0.33), 15),
|
||||
)
|
||||
|
||||
chat_talking_prompt_half = await build_readable_messages(
|
||||
message_list_before_now_half,
|
||||
replace_bot_name=True,
|
||||
@@ -2071,12 +2106,35 @@ class DefaultReplyer:
|
||||
|
||||
memory_context = {key: value for key, value in memory_context.items() if value}
|
||||
|
||||
# 构建聊天历史用于存储
|
||||
message_list_before_short = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=stream.stream_id,
|
||||
timestamp=time.time(),
|
||||
limit=int(global_config.chat.max_context_size * 0.33),
|
||||
)
|
||||
# 从内存获取聊天历史用于存储,避免重复查询数据库
|
||||
from src.plugin_system.apis.chat_api import get_chat_manager
|
||||
|
||||
chat_manager = get_chat_manager()
|
||||
chat_stream_obj = await chat_manager.get_stream(stream.stream_id)
|
||||
|
||||
if chat_stream_obj:
|
||||
# 确保历史消息已初始化
|
||||
await chat_stream_obj.context_manager.ensure_history_initialized()
|
||||
|
||||
# 获取所有消息(历史+未读)
|
||||
all_messages = (
|
||||
chat_stream_obj.context_manager.context.history_messages +
|
||||
chat_stream_obj.context_manager.get_unread_messages()
|
||||
)
|
||||
|
||||
# 转换为字典格式,限制数量
|
||||
limit = int(global_config.chat.max_context_size * 0.33)
|
||||
message_list_before_short = [msg.flatten() for msg in all_messages[-limit:]]
|
||||
|
||||
logger.debug(f"记忆存储使用内存中的 {len(message_list_before_short)} 条消息")
|
||||
else:
|
||||
# 回退到数据库查询
|
||||
logger.warning(f"记忆存储:无法获取chat_stream,回退到数据库查询: {stream.stream_id}")
|
||||
message_list_before_short = await get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=stream.stream_id,
|
||||
timestamp=time.time(),
|
||||
limit=int(global_config.chat.max_context_size * 0.33),
|
||||
)
|
||||
chat_history = await build_readable_messages(
|
||||
message_list_before_short,
|
||||
replace_bot_name=True,
|
||||
|
||||
Reference in New Issue
Block a user