refactor(core): 适配核心数据获取与消息构建函数的异步调用
在数据库交互层异步化后,多个相关的数据获取和消息构建函数(如 `build_readable_messages`)也转为异步实现。本次提交在所有调用点添加了 `await` 关键字,以适应这一变化。 此外,本次提交还包含以下修复: - 在主动思考模块中增加了对规划器返回无效动作的检查,避免后续流程出错。 - 修正了日志记录中错误的上下文变量引用。
This commit is contained in:
@@ -192,7 +192,7 @@ class CycleProcessor:
|
|||||||
await self.action_modifier.modify_actions()
|
await self.action_modifier.modify_actions()
|
||||||
available_actions = self.context.action_manager.get_using_actions()
|
available_actions = self.context.action_manager.get_using_actions()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self.context.log_prefix} 动作修改失败: {e}")
|
logger.error(f"{self.log_prefix} 动作修改失败: {e}")
|
||||||
available_actions = {}
|
available_actions = {}
|
||||||
|
|
||||||
# 规划动作
|
# 规划动作
|
||||||
|
|||||||
@@ -120,6 +120,10 @@ class ProactiveThinker:
|
|||||||
action_result = actions[0] if actions else {}
|
action_result = actions[0] if actions else {}
|
||||||
action_type = action_result.get("action_type")
|
action_type = action_result.get("action_type")
|
||||||
|
|
||||||
|
if action_type is None:
|
||||||
|
logger.info(f"{self.context.log_prefix} 主动思考决策: 规划器未返回有效动作")
|
||||||
|
return
|
||||||
|
|
||||||
if action_type == "proactive_reply":
|
if action_type == "proactive_reply":
|
||||||
await self._generate_proactive_content_and_send(action_result, trigger_event)
|
await self._generate_proactive_content_and_send(action_result, trigger_event)
|
||||||
elif action_type not in ["do_nothing", "no_action"]:
|
elif action_type not in ["do_nothing", "no_action"]:
|
||||||
@@ -212,12 +216,12 @@ class ProactiveThinker:
|
|||||||
logger.warning(f"{self.context.log_prefix} 主题为空,跳过网络搜索。")
|
logger.warning(f"{self.context.log_prefix} 主题为空,跳过网络搜索。")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"{self.context.log_prefix} 主动思考时网络搜索失败: {e}")
|
logger.error(f"{self.context.log_prefix} 主动思考时网络搜索失败: {e}")
|
||||||
message_list = get_raw_msg_before_timestamp_with_chat(
|
message_list = await get_raw_msg_before_timestamp_with_chat(
|
||||||
chat_id=self.context.stream_id,
|
chat_id=self.context.stream_id,
|
||||||
timestamp=time.time(),
|
timestamp=time.time(),
|
||||||
limit=int(global_config.chat.max_context_size * 0.3),
|
limit=int(global_config.chat.max_context_size * 0.3),
|
||||||
)
|
)
|
||||||
chat_context_block, _ = build_readable_messages_with_id(messages=message_list)
|
chat_context_block, _ = await build_readable_messages_with_id(messages=message_list)
|
||||||
|
|
||||||
from src.llm_models.utils_model import LLMRequest
|
from src.llm_models.utils_model import LLMRequest
|
||||||
from src.config.config import model_config
|
from src.config.config import model_config
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ class PlanFilter:
|
|||||||
)
|
)
|
||||||
|
|
||||||
prompt_template = await global_prompt_manager.get_prompt_async("proactive_planner_prompt")
|
prompt_template = await global_prompt_manager.get_prompt_async("proactive_planner_prompt")
|
||||||
actions_before_now = get_actions_by_timestamp_with_chat(
|
actions_before_now = await get_actions_by_timestamp_with_chat(
|
||||||
chat_id=plan.chat_id,
|
chat_id=plan.chat_id,
|
||||||
timestamp_start=time.time() - 3600,
|
timestamp_start=time.time() - 3600,
|
||||||
timestamp_end=time.time(),
|
timestamp_end=time.time(),
|
||||||
|
|||||||
@@ -486,7 +486,7 @@ class Prompt:
|
|||||||
all_dialogue_prompt = ""
|
all_dialogue_prompt = ""
|
||||||
if message_list_before_now:
|
if message_list_before_now:
|
||||||
latest_25_msgs = message_list_before_now[-int(global_config.chat.max_context_size) :]
|
latest_25_msgs = message_list_before_now[-int(global_config.chat.max_context_size) :]
|
||||||
all_dialogue_prompt_str = build_readable_messages(
|
all_dialogue_prompt_str = await build_readable_messages(
|
||||||
latest_25_msgs,
|
latest_25_msgs,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
timestamp_mode="normal",
|
timestamp_mode="normal",
|
||||||
@@ -505,7 +505,7 @@ class Prompt:
|
|||||||
else:
|
else:
|
||||||
core_dialogue_list = core_dialogue_list[-int(global_config.chat.max_context_size * 2) :]
|
core_dialogue_list = core_dialogue_list[-int(global_config.chat.max_context_size * 2) :]
|
||||||
|
|
||||||
core_dialogue_prompt_str = build_readable_messages(
|
core_dialogue_prompt_str = await build_readable_messages(
|
||||||
core_dialogue_list,
|
core_dialogue_list,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
merge_messages=False,
|
merge_messages=False,
|
||||||
@@ -534,7 +534,7 @@ class Prompt:
|
|||||||
chat_history = ""
|
chat_history = ""
|
||||||
if self.parameters.message_list_before_now_long:
|
if self.parameters.message_list_before_now_long:
|
||||||
recent_messages = self.parameters.message_list_before_now_long[-10:]
|
recent_messages = self.parameters.message_list_before_now_long[-10:]
|
||||||
chat_history = build_readable_messages(
|
chat_history = await build_readable_messages(
|
||||||
recent_messages,
|
recent_messages,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
timestamp_mode="normal",
|
timestamp_mode="normal",
|
||||||
@@ -574,7 +574,7 @@ class Prompt:
|
|||||||
chat_history = ""
|
chat_history = ""
|
||||||
if self.parameters.message_list_before_now_long:
|
if self.parameters.message_list_before_now_long:
|
||||||
recent_messages = self.parameters.message_list_before_now_long[-20:]
|
recent_messages = self.parameters.message_list_before_now_long[-20:]
|
||||||
chat_history = build_readable_messages(
|
chat_history = await build_readable_messages(
|
||||||
recent_messages,
|
recent_messages,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
timestamp_mode="normal",
|
timestamp_mode="normal",
|
||||||
@@ -632,7 +632,7 @@ class Prompt:
|
|||||||
chat_history = ""
|
chat_history = ""
|
||||||
if self.parameters.message_list_before_now_long:
|
if self.parameters.message_list_before_now_long:
|
||||||
recent_messages = self.parameters.message_list_before_now_long[-15:]
|
recent_messages = self.parameters.message_list_before_now_long[-15:]
|
||||||
chat_history = build_readable_messages(
|
chat_history = await build_readable_messages(
|
||||||
recent_messages,
|
recent_messages,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
timestamp_mode="normal",
|
timestamp_mode="normal",
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ class ChatAction:
|
|||||||
limit=15,
|
limit=15,
|
||||||
limit_mode="last",
|
limit_mode="last",
|
||||||
)
|
)
|
||||||
chat_talking_prompt = build_readable_messages(
|
chat_talking_prompt = await build_readable_messages(
|
||||||
message_list_before_now,
|
message_list_before_now,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
merge_messages=False,
|
merge_messages=False,
|
||||||
@@ -227,7 +227,7 @@ class ChatAction:
|
|||||||
limit=10,
|
limit=10,
|
||||||
limit_mode="last",
|
limit_mode="last",
|
||||||
)
|
)
|
||||||
chat_talking_prompt = build_readable_messages(
|
chat_talking_prompt = await build_readable_messages(
|
||||||
message_list_before_now,
|
message_list_before_now,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
merge_messages=False,
|
merge_messages=False,
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ class ChatMood:
|
|||||||
limit=10,
|
limit=10,
|
||||||
limit_mode="last",
|
limit_mode="last",
|
||||||
)
|
)
|
||||||
chat_talking_prompt = build_readable_messages(
|
chat_talking_prompt = await build_readable_messages(
|
||||||
message_list_before_now,
|
message_list_before_now,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
merge_messages=False,
|
merge_messages=False,
|
||||||
@@ -246,7 +246,7 @@ class ChatMood:
|
|||||||
limit=5,
|
limit=5,
|
||||||
limit_mode="last",
|
limit_mode="last",
|
||||||
)
|
)
|
||||||
chat_talking_prompt = build_readable_messages(
|
chat_talking_prompt = await build_readable_messages(
|
||||||
message_list_before_now,
|
message_list_before_now,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
merge_messages=False,
|
merge_messages=False,
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ class PromptBuilder:
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def build_chat_history_prompts(chat_stream: ChatStream, message: MessageRecvS4U):
|
async def build_chat_history_prompts(chat_stream: ChatStream, message: MessageRecvS4U):
|
||||||
message_list_before_now = get_raw_msg_before_timestamp_with_chat(
|
message_list_before_now = get_raw_msg_before_timestamp_with_chat(
|
||||||
chat_id=chat_stream.stream_id,
|
chat_id=chat_stream.stream_id,
|
||||||
timestamp=time.time(),
|
timestamp=time.time(),
|
||||||
@@ -207,7 +207,7 @@ class PromptBuilder:
|
|||||||
background_dialogue_prompt = ""
|
background_dialogue_prompt = ""
|
||||||
if background_dialogue_list:
|
if background_dialogue_list:
|
||||||
context_msgs = background_dialogue_list[-s4u_config.max_context_message_length :]
|
context_msgs = background_dialogue_list[-s4u_config.max_context_message_length :]
|
||||||
background_dialogue_prompt_str = build_readable_messages(
|
background_dialogue_prompt_str = await build_readable_messages(
|
||||||
context_msgs,
|
context_msgs,
|
||||||
timestamp_mode="normal_no_YMD",
|
timestamp_mode="normal_no_YMD",
|
||||||
show_pic=False,
|
show_pic=False,
|
||||||
@@ -256,7 +256,7 @@ class PromptBuilder:
|
|||||||
timestamp=time.time(),
|
timestamp=time.time(),
|
||||||
limit=20,
|
limit=20,
|
||||||
)
|
)
|
||||||
all_dialogue_prompt_str = build_readable_messages(
|
all_dialogue_prompt_str = await build_readable_messages(
|
||||||
all_dialogue_prompt,
|
all_dialogue_prompt,
|
||||||
timestamp_mode="normal_no_YMD",
|
timestamp_mode="normal_no_YMD",
|
||||||
show_pic=False,
|
show_pic=False,
|
||||||
@@ -306,7 +306,7 @@ class PromptBuilder:
|
|||||||
self.build_expression_habits(chat_stream, message_txt, sender_name),
|
self.build_expression_habits(chat_stream, message_txt, sender_name),
|
||||||
)
|
)
|
||||||
|
|
||||||
core_dialogue_prompt, background_dialogue_prompt, all_dialogue_prompt = self.build_chat_history_prompts(
|
core_dialogue_prompt, background_dialogue_prompt, all_dialogue_prompt = await self.build_chat_history_prompts(
|
||||||
chat_stream, message
|
chat_stream, message
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ class ChatMood:
|
|||||||
limit=int(global_config.chat.max_context_size / 3),
|
limit=int(global_config.chat.max_context_size / 3),
|
||||||
limit_mode="last",
|
limit_mode="last",
|
||||||
)
|
)
|
||||||
chat_talking_prompt = build_readable_messages(
|
chat_talking_prompt = await build_readable_messages(
|
||||||
message_list_before_now,
|
message_list_before_now,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
merge_messages=False,
|
merge_messages=False,
|
||||||
@@ -154,7 +154,7 @@ class ChatMood:
|
|||||||
limit=15,
|
limit=15,
|
||||||
limit_mode="last",
|
limit_mode="last",
|
||||||
)
|
)
|
||||||
chat_talking_prompt = build_readable_messages(
|
chat_talking_prompt = await build_readable_messages(
|
||||||
message_list_before_now,
|
message_list_before_now,
|
||||||
replace_bot_name=True,
|
replace_bot_name=True,
|
||||||
merge_messages=False,
|
merge_messages=False,
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ def init_prompt():
|
|||||||
name_mapping[replace_person_name] = f"用户{current_user}{user_count if user_count > 1 else ''}"
|
name_mapping[replace_person_name] = f"用户{current_user}{user_count if user_count > 1 else ''}"
|
||||||
current_user = chr(ord(current_user) + 1)
|
current_user = chr(ord(current_user) + 1)
|
||||||
|
|
||||||
readable_messages = build_readable_messages(
|
readable_messages = await build_readable_messages(
|
||||||
messages=user_messages, replace_bot_name=True, timestamp_mode="normal_no_YMD", truncate=True
|
messages=user_messages, replace_bot_name=True, timestamp_mode="normal_no_YMD", truncate=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -125,7 +125,7 @@ class EmojiAction(BaseAction):
|
|||||||
recent_messages = message_api.get_recent_messages(chat_id=self.chat_id, limit=5)
|
recent_messages = message_api.get_recent_messages(chat_id=self.chat_id, limit=5)
|
||||||
messages_text = ""
|
messages_text = ""
|
||||||
if recent_messages:
|
if recent_messages:
|
||||||
messages_text = message_api.build_readable_messages(
|
messages_text = await message_api.build_readable_messages(
|
||||||
messages=recent_messages,
|
messages=recent_messages,
|
||||||
timestamp_mode="normal_no_YMD",
|
timestamp_mode="normal_no_YMD",
|
||||||
truncate=False,
|
truncate=False,
|
||||||
@@ -184,7 +184,7 @@ class EmojiAction(BaseAction):
|
|||||||
recent_messages = message_api.get_recent_messages(chat_id=self.chat_id, limit=5)
|
recent_messages = message_api.get_recent_messages(chat_id=self.chat_id, limit=5)
|
||||||
messages_text = ""
|
messages_text = ""
|
||||||
if recent_messages:
|
if recent_messages:
|
||||||
messages_text = message_api.build_readable_messages(
|
messages_text = await message_api.build_readable_messages(
|
||||||
messages=recent_messages,
|
messages=recent_messages,
|
||||||
timestamp_mode="normal_no_YMD",
|
timestamp_mode="normal_no_YMD",
|
||||||
truncate=False,
|
truncate=False,
|
||||||
|
|||||||
@@ -240,7 +240,7 @@ class QZoneService:
|
|||||||
all_messages = all_messages[-100:]
|
all_messages = all_messages[-100:]
|
||||||
|
|
||||||
# build_readable_messages_with_id 返回一个元组 (formatted_string, message_id_list)
|
# build_readable_messages_with_id 返回一个元组 (formatted_string, message_id_list)
|
||||||
formatted_string, _ = build_readable_messages_with_id(all_messages)
|
formatted_string, _ = await build_readable_messages_with_id(all_messages)
|
||||||
return formatted_string
|
return formatted_string
|
||||||
|
|
||||||
logger.debug(f"Stream ID '{stream_id}' 未在任何互通组中找到。")
|
logger.debug(f"Stream ID '{stream_id}' 未在任何互通组中找到。")
|
||||||
|
|||||||
Reference in New Issue
Block a user