refactor(chat): 将 get_chat_type_and_target_info 重构为异步函数
将 `get_chat_type_and_target_info` 函数从同步改为异步,以支持其内部对异步方法 `person_info_manager.get_values` 的调用。 此更改可防止在获取聊天对象信息时阻塞事件循环。所有调用此函数的代码(包括 `SubHeartflow`, `ActionModifier`, `PlanGenerator`, `DefaultReplyer`)都已相应更新为使用 `await`。 在 `DefaultReplyer` 中引入了延迟异步初始化模式 (`_async_init`),以适应其类生命周期。
This commit is contained in:
@@ -24,7 +24,7 @@ class SubHeartflow:
|
||||
self.subheartflow_id = subheartflow_id
|
||||
self.chat_id = subheartflow_id
|
||||
|
||||
self.is_group_chat, self.chat_target_info = get_chat_type_and_target_info(self.chat_id)
|
||||
self.is_group_chat, self.chat_target_info = (None, None)
|
||||
self.log_prefix = get_chat_manager().get_stream_name(self.subheartflow_id) or self.subheartflow_id
|
||||
|
||||
# focus模式退出冷却时间管理
|
||||
@@ -38,4 +38,5 @@ class SubHeartflow:
|
||||
|
||||
async def initialize(self):
|
||||
"""异步初始化方法,创建兴趣流并确定聊天类型"""
|
||||
self.is_group_chat, self.chat_target_info = await get_chat_type_and_target_info(self.chat_id)
|
||||
await self.heart_fc_instance.start()
|
||||
|
||||
@@ -72,7 +72,7 @@ class ActionModifier:
|
||||
from src.chat.utils.utils import get_chat_type_and_target_info
|
||||
|
||||
# 获取聊天类型
|
||||
is_group_chat, _ = get_chat_type_and_target_info(self.chat_id)
|
||||
is_group_chat, _ = await get_chat_type_and_target_info(self.chat_id)
|
||||
all_registered_actions = component_registry.get_components_by_type(ComponentType.ACTION)
|
||||
|
||||
chat_type_removals = []
|
||||
|
||||
@@ -51,7 +51,7 @@ class PlanGenerator:
|
||||
Returns:
|
||||
Plan: 一个填充了初始上下文信息的 Plan 对象。
|
||||
"""
|
||||
_is_group_chat, chat_target_info_dict = get_chat_type_and_target_info(self.chat_id)
|
||||
_is_group_chat, chat_target_info_dict = await get_chat_type_and_target_info(self.chat_id)
|
||||
|
||||
target_info = None
|
||||
if chat_target_info_dict:
|
||||
|
||||
@@ -202,7 +202,9 @@ class DefaultReplyer:
|
||||
):
|
||||
self.express_model = LLMRequest(model_set=model_config.model_task_config.replyer, request_type=request_type)
|
||||
self.chat_stream = chat_stream
|
||||
self.is_group_chat, self.chat_target_info = get_chat_type_and_target_info(self.chat_stream.stream_id)
|
||||
self.is_group_chat: Optional[bool] = None
|
||||
self.chat_target_info: Optional[Dict[str, Any]] = None
|
||||
self._initialized = False
|
||||
|
||||
self.heart_fc_sender = HeartFCSender()
|
||||
self.memory_activator = MemoryActivator()
|
||||
@@ -775,6 +777,12 @@ class DefaultReplyer:
|
||||
mai_think.target = target
|
||||
return mai_think
|
||||
|
||||
async def _async_init(self):
|
||||
if self._initialized:
|
||||
return
|
||||
self.is_group_chat, self.chat_target_info = await get_chat_type_and_target_info(self.chat_stream.stream_id)
|
||||
self._initialized = True
|
||||
|
||||
async def build_prompt_reply_context(
|
||||
self,
|
||||
reply_to: str,
|
||||
@@ -800,10 +808,11 @@ class DefaultReplyer:
|
||||
"""
|
||||
if available_actions is None:
|
||||
available_actions = {}
|
||||
await self._async_init()
|
||||
chat_stream = self.chat_stream
|
||||
chat_id = chat_stream.stream_id
|
||||
person_info_manager = get_person_info_manager()
|
||||
is_group_chat = bool(chat_stream.group_info)
|
||||
is_group_chat = self.is_group_chat
|
||||
|
||||
if global_config.mood.enable_mood:
|
||||
chat_mood = mood_manager.get_mood_by_chat_id(chat_id)
|
||||
@@ -1128,9 +1137,10 @@ class DefaultReplyer:
|
||||
reply_to: str,
|
||||
reply_message: Optional[Dict[str, Any]] = None,
|
||||
) -> str: # sourcery skip: merge-else-if-into-elif, remove-redundant-if
|
||||
await self._async_init()
|
||||
chat_stream = self.chat_stream
|
||||
chat_id = chat_stream.stream_id
|
||||
is_group_chat = bool(chat_stream.group_info)
|
||||
is_group_chat = self.is_group_chat
|
||||
|
||||
if reply_message:
|
||||
sender = reply_message.get("sender")
|
||||
|
||||
@@ -619,7 +619,7 @@ def translate_timestamp_to_human_readable(timestamp: float, mode: str = "normal"
|
||||
return time.strftime("%H:%M:%S", time.localtime(timestamp))
|
||||
|
||||
|
||||
def get_chat_type_and_target_info(chat_id: str) -> Tuple[bool, Optional[Dict]]:
|
||||
async def get_chat_type_and_target_info(chat_id: str) -> Tuple[bool, Optional[Dict]]:
|
||||
"""
|
||||
获取聊天类型(是否群聊)和私聊对象信息。
|
||||
|
||||
@@ -663,7 +663,8 @@ def get_chat_type_and_target_info(chat_id: str) -> Tuple[bool, Optional[Dict]]:
|
||||
if person_id:
|
||||
# get_value is async, so await it directly
|
||||
person_info_manager = get_person_info_manager()
|
||||
person_name = person_info_manager.get_value(person_id, "person_name")
|
||||
person_data = await person_info_manager.get_values(person_id, ["person_name"])
|
||||
person_name = person_data.get("person_name")
|
||||
|
||||
target_info["person_id"] = person_id
|
||||
target_info["person_name"] = person_name
|
||||
|
||||
Reference in New Issue
Block a user