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:
committed by
Windpicker-owo
parent
988bf7f7ab
commit
69c6829aed
@@ -24,7 +24,7 @@ class SubHeartflow:
|
|||||||
self.subheartflow_id = subheartflow_id
|
self.subheartflow_id = subheartflow_id
|
||||||
self.chat_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
|
self.log_prefix = get_chat_manager().get_stream_name(self.subheartflow_id) or self.subheartflow_id
|
||||||
|
|
||||||
# focus模式退出冷却时间管理
|
# focus模式退出冷却时间管理
|
||||||
@@ -38,4 +38,5 @@ class SubHeartflow:
|
|||||||
|
|
||||||
async def initialize(self):
|
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()
|
await self.heart_fc_instance.start()
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ class ActionModifier:
|
|||||||
from src.chat.utils.utils import get_chat_type_and_target_info
|
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)
|
all_registered_actions = component_registry.get_components_by_type(ComponentType.ACTION)
|
||||||
|
|
||||||
chat_type_removals = []
|
chat_type_removals = []
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class PlanGenerator:
|
|||||||
Returns:
|
Returns:
|
||||||
Plan: 一个填充了初始上下文信息的 Plan 对象。
|
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
|
target_info = None
|
||||||
if chat_target_info_dict:
|
if chat_target_info_dict:
|
||||||
|
|||||||
@@ -202,7 +202,10 @@ class DefaultReplyer:
|
|||||||
):
|
):
|
||||||
self.express_model = LLMRequest(model_set=model_config.model_task_config.replyer, request_type=request_type)
|
self.express_model = LLMRequest(model_set=model_config.model_task_config.replyer, request_type=request_type)
|
||||||
self.chat_stream = chat_stream
|
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.heart_fc_sender = HeartFCSender()
|
||||||
self.memory_activator = MemoryActivator()
|
self.memory_activator = MemoryActivator()
|
||||||
# 使用纯向量瞬时记忆系统V2,支持自定义保留时间
|
# 使用纯向量瞬时记忆系统V2,支持自定义保留时间
|
||||||
@@ -803,9 +806,12 @@ class DefaultReplyer:
|
|||||||
action_descriptions += "根据聊天情况,你决定在回复的同时做以下这些动作:\n"
|
action_descriptions += "根据聊天情况,你决定在回复的同时做以下这些动作:\n"
|
||||||
action_descriptions += choosen_action_descriptions
|
action_descriptions += choosen_action_descriptions
|
||||||
|
|
||||||
return action_descriptions
|
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(
|
async def build_prompt_reply_context(
|
||||||
self,
|
self,
|
||||||
extra_info: str = "",
|
extra_info: str = "",
|
||||||
@@ -833,22 +839,11 @@ class DefaultReplyer:
|
|||||||
"""
|
"""
|
||||||
if available_actions is None:
|
if available_actions is None:
|
||||||
available_actions = {}
|
available_actions = {}
|
||||||
|
await self._async_init()
|
||||||
chat_stream = self.chat_stream
|
chat_stream = self.chat_stream
|
||||||
chat_id = chat_stream.stream_id
|
chat_id = chat_stream.stream_id
|
||||||
is_group_chat = bool(chat_stream.group_info)
|
person_info_manager = get_person_info_manager()
|
||||||
platform = chat_stream.platform
|
is_group_chat = self.is_group_chat
|
||||||
|
|
||||||
if reply_message:
|
|
||||||
user_id = reply_message.get("user_id","")
|
|
||||||
person = Person(platform=platform, user_id=user_id)
|
|
||||||
person_name = person.person_name or user_id
|
|
||||||
sender = person_name
|
|
||||||
target = reply_message.get('processed_plain_text')
|
|
||||||
else:
|
|
||||||
person_name = "用户"
|
|
||||||
sender = "用户"
|
|
||||||
target = "消息"
|
|
||||||
|
|
||||||
|
|
||||||
if global_config.mood.enable_mood:
|
if global_config.mood.enable_mood:
|
||||||
chat_mood = mood_manager.get_mood_by_chat_id(chat_id)
|
chat_mood = mood_manager.get_mood_by_chat_id(chat_id)
|
||||||
@@ -1164,9 +1159,10 @@ class DefaultReplyer:
|
|||||||
reply_to: str,
|
reply_to: str,
|
||||||
reply_message: Optional[Dict[str, Any]] = None,
|
reply_message: Optional[Dict[str, Any]] = None,
|
||||||
) -> str: # sourcery skip: merge-else-if-into-elif, remove-redundant-if
|
) -> str: # sourcery skip: merge-else-if-into-elif, remove-redundant-if
|
||||||
|
await self._async_init()
|
||||||
chat_stream = self.chat_stream
|
chat_stream = self.chat_stream
|
||||||
chat_id = chat_stream.stream_id
|
chat_id = chat_stream.stream_id
|
||||||
is_group_chat = bool(chat_stream.group_info)
|
is_group_chat = self.is_group_chat
|
||||||
|
|
||||||
if reply_message:
|
if reply_message:
|
||||||
sender = reply_message.get("sender")
|
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))
|
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]]:
|
||||||
"""
|
"""
|
||||||
获取聊天类型(是否群聊)和私聊对象信息。
|
获取聊天类型(是否群聊)和私聊对象信息。
|
||||||
|
|
||||||
@@ -667,7 +667,8 @@ def get_chat_type_and_target_info(chat_id: str) -> Tuple[bool, Optional[Dict]]:
|
|||||||
if person_id:
|
if person_id:
|
||||||
# get_value is async, so await it directly
|
# get_value is async, so await it directly
|
||||||
person_info_manager = get_person_info_manager()
|
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_id"] = person_id
|
||||||
target_info["person_name"] = person_name
|
target_info["person_name"] = person_name
|
||||||
|
|||||||
Reference in New Issue
Block a user