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 from src.chat.message_receive.chat_stream import get_chat_manager
chat_manager = 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"): if chat_stream and hasattr(chat_stream, "context_manager"):
history_limit = self._determine_history_limit(context) history_limit = self._determine_history_limit(context)
messages = chat_stream.context_manager.get_messages(limit=history_limit, include_unread=True) 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._batch_storage_enabled = False
self._current_chat_id = None self._current_chat_id = None
self._pending_actions = [] # 清空队列
logger.debug("已禁用批量存储模式") logger.debug("已禁用批量存储模式")
def add_action_to_batch(self, action_name: str, action_data: dict, thinking_id: str = "", 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): def __init__(self):
self._repliers: dict[str, DefaultReplyer] = {} self._repliers: dict[str, DefaultReplyer] = {}
def get_replyer( async def get_replyer(
self, self,
chat_stream: ChatStream | None = None, chat_stream: ChatStream | None = None,
chat_id: str | None = None, chat_id: str | None = None,
@@ -37,7 +37,7 @@ class ReplyerManager:
target_stream = chat_stream target_stream = chat_stream
if not target_stream: if not target_stream:
if chat_manager := get_chat_manager(): 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: if not target_stream:
logger.warning(f"[ReplyerManager] 未找到 stream_id='{stream_id}' 的聊天流,无法创建回复器。") 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 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( return await temp_generator.build_s4u_chat_history_prompts(
message_list_before_now, target_user_id, sender, chat_id message_list_before_now, target_user_id, sender, chat_id
) )

View File

@@ -9,17 +9,18 @@
""" """
import traceback import traceback
from typing import Any from typing import TYPE_CHECKING, Any
from rich.traceback import install from rich.traceback import install
from src.chat.message_receive.chat_stream import ChatStream from src.chat.message_receive.chat_stream import ChatStream
from src.chat.replyer.default_generator import DefaultReplyer
from src.chat.replyer.replyer_manager import replyer_manager
from src.chat.utils.utils import process_llm_response from src.chat.utils.utils import process_llm_response
from src.common.logger import get_logger from src.common.logger import get_logger
from src.plugin_system.base.component_types import ActionInfo from src.plugin_system.base.component_types import ActionInfo
if TYPE_CHECKING:
from src.chat.replyer.default_generator import DefaultReplyer
install(extra_lines=3) install(extra_lines=3)
# 日志记录器 # 日志记录器
@@ -31,11 +32,11 @@ logger = get_logger("generator_api")
# ============================================================================= # =============================================================================
def get_replyer( async def get_replyer(
chat_stream: ChatStream | None = None, chat_stream: ChatStream | None = None,
chat_id: str | None = None, chat_id: str | None = None,
request_type: str = "replyer", request_type: str = "replyer",
) -> DefaultReplyer | None: ) -> Any | None:
"""获取回复器对象 """获取回复器对象
优先使用chat_stream如果没有则使用chat_id直接查找。 优先使用chat_stream如果没有则使用chat_id直接查找。
@@ -56,7 +57,9 @@ def get_replyer(
raise ValueError("chat_stream 和 chat_id 不可均为空") raise ValueError("chat_stream 和 chat_id 不可均为空")
try: try:
logger.debug(f"[GeneratorAPI] 正在获取回复器chat_id: {chat_id}, chat_stream: {'' if chat_stream else ''}") logger.debug(f"[GeneratorAPI] 正在获取回复器chat_id: {chat_id}, chat_stream: {'' if chat_stream else ''}")
return replyer_manager.get_replyer( # 动态导入避免循环依赖
from src.chat.replyer.replyer_manager import replyer_manager
return await replyer_manager.get_replyer(
chat_stream=chat_stream, chat_stream=chat_stream,
chat_id=chat_id, chat_id=chat_id,
request_type=request_type, request_type=request_type,
@@ -110,7 +113,7 @@ async def generate_reply(
""" """
try: try:
# 获取回复器 # 获取回复器
replyer = get_replyer(chat_stream, chat_id, request_type=request_type) replyer = await get_replyer(chat_stream, chat_id, request_type=request_type)
if not replyer: if not replyer:
logger.error("[GeneratorAPI] 无法获取回复器") logger.error("[GeneratorAPI] 无法获取回复器")
return False, [], None return False, [], None
@@ -199,7 +202,7 @@ async def rewrite_reply(
""" """
try: try:
# 获取回复器 # 获取回复器
replyer = get_replyer(chat_stream, chat_id, request_type=request_type) replyer = await get_replyer(chat_stream, chat_id, request_type=request_type)
if not replyer: if not replyer:
logger.error("[GeneratorAPI] 无法获取回复器") logger.error("[GeneratorAPI] 无法获取回复器")
return False, [], None return False, [], None
@@ -285,7 +288,7 @@ async def generate_response_custom(
Returns: Returns:
Optional[str]: 生成的回复内容 Optional[str]: 生成的回复内容
""" """
replyer = get_replyer(chat_stream, chat_id, request_type=request_type) replyer = await get_replyer(chat_stream, chat_id, request_type=request_type)
if not replyer: if not replyer:
logger.error("[GeneratorAPI] 无法获取回复器") logger.error("[GeneratorAPI] 无法获取回复器")
return None return None

View File

@@ -441,7 +441,7 @@ class ChatterPlanExecutor:
# 通过 chat_id 获取真实的 chat_stream 对象 # 通过 chat_id 获取真实的 chat_stream 对象
from src.plugin_system.apis.chat_api import get_chat_manager from src.plugin_system.apis.chat_api import get_chat_manager
chat_manager = get_chat_manager() chat_manager = get_chat_manager()
chat_stream = chat_manager.get_stream(plan.chat_id) chat_stream = await chat_manager.get_stream(plan.chat_id)
if chat_stream: if chat_stream:
# 调用 action_manager 的批量存储 # 调用 action_manager 的批量存储