diff --git a/src/chat/utils/prompt_builder.py b/src/chat/utils/prompt_builder.py index ca94d542..3f725453 100644 --- a/src/chat/utils/prompt_builder.py +++ b/src/chat/utils/prompt_builder.py @@ -169,10 +169,26 @@ class PromptManager: if name in ['action_prompt', 'replyer_prompt', 'planner_prompt', 'tool_executor_prompt']: tool_history = get_tool_history_prompt(message_id) - # 如果有工具历史,添加到提示词末尾 + # 获取基本格式化结果 result = prompt.format(**kwargs) + + # 如果有工具历史,插入到适当位置 if tool_history: - result = f"{result}\n\n{tool_history}" + # 查找合适的插入点 + # 在人格信息和身份块之后,但在主要内容之前 + identity_end = result.find("```\n现在,你说:") + if identity_end == -1: + # 如果找不到特定标记,尝试在第一个段落后插入 + first_double_newline = result.find("\n\n") + if first_double_newline != -1: + # 在第一个双换行后插入 + result = f"{result[:first_double_newline + 2]}{tool_history}\n{result[first_double_newline + 2:]}" + else: + # 如果找不到合适的位置,添加到开头 + result = f"{tool_history}\n\n{result}" + else: + # 在找到的位置插入 + result = f"{result[:identity_end]}\n{tool_history}\n{result[identity_end:]}" return result