refactor(chat): 统一异步调用模式并修复循环依赖

- 将 chat_manager.get_stream() 调用改为异步
- 将 replyer_manager.get_replyer() 方法改为异步
- 在 generator_api 中使用动态导入避免循环依赖
- 在 action_manager 中添加待处理动作队列清理
- 更新所有相关调用点以支持异步模式
This commit is contained in:
Windpicker-owo
2025-10-05 18:30:16 +08:00
parent 4cdc5fc89d
commit 3e37b7cef5
6 changed files with 18 additions and 14 deletions

View File

@@ -1111,7 +1111,7 @@ class MemorySystem:
from src.chat.message_receive.chat_stream import get_chat_manager
chat_manager = get_chat_manager()
chat_stream = chat_manager.get_stream(stream_id)
chat_stream = await chat_manager.get_stream(stream_id)
if chat_stream and hasattr(chat_stream, "context_manager"):
history_limit = self._determine_history_limit(context)
messages = chat_stream.context_manager.get_messages(limit=history_limit, include_unread=True)

View File

@@ -615,6 +615,7 @@ class ChatterActionManager:
"""禁用批量存储模式"""
self._batch_storage_enabled = False
self._current_chat_id = None
self._pending_actions = [] # 清空队列
logger.debug("已禁用批量存储模式")
def add_action_to_batch(self, action_name: str, action_data: dict, thinking_id: str = "",

View File

@@ -9,7 +9,7 @@ class ReplyerManager:
def __init__(self):
self._repliers: dict[str, DefaultReplyer] = {}
def get_replyer(
async def get_replyer(
self,
chat_stream: ChatStream | None = None,
chat_id: str | None = None,
@@ -37,7 +37,7 @@ class ReplyerManager:
target_stream = chat_stream
if not target_stream:
if chat_manager := get_chat_manager():
target_stream = chat_manager.get_stream(stream_id)
target_stream = await chat_manager.get_stream(stream_id)
if not target_stream:
logger.warning(f"[ReplyerManager] 未找到 stream_id='{stream_id}' 的聊天流,无法创建回复器。")

View File

@@ -490,7 +490,7 @@ class Prompt:
from src.plugin_system.apis.generator_api import get_replyer
# 创建临时生成器实例来使用其方法
temp_generator = get_replyer(None, chat_id, request_type="prompt_building")
temp_generator = await get_replyer(None, chat_id, request_type="prompt_building")
return await temp_generator.build_s4u_chat_history_prompts(
message_list_before_now, target_user_id, sender, chat_id
)