rebase 清理

This commit is contained in:
Windpicker-owo
2025-11-19 23:45:47 +08:00
parent 829bc9b4bc
commit 40709d95de
60 changed files with 465 additions and 10066 deletions

View File

@@ -143,9 +143,8 @@ def init_prompt():
现在,你说:
""",
"replyer_self_prompt",
"s4u_style_prompt",
)
Prompt(
"""
@@ -285,6 +284,7 @@ class DefaultReplyer:
async def generate_reply_with_context(
self,
reply_to: str = "",
extra_info: str = "",
available_actions: dict[str, ActionInfo] | None = None,
enable_tool: bool = True,
@@ -299,9 +299,7 @@ class DefaultReplyer:
Args:
reply_to: 回复对象,格式为 "发送者:消息内容"
extra_info: 额外信息,用于补充上下文
reply_reason: 回复原因
available_actions: 可用的动作信息字典
choosen_actions: 已选动作
enable_tool: 是否启用工具调用
from_plugin: 是否来自插件
@@ -351,13 +349,8 @@ class DefaultReplyer:
child_tasks = set()
prompt = None
selected_expressions = None
if available_actions is None:
available_actions = {}
# 自消息阻断
if self._should_block_self_message(reply_message):
logger.debug("[SelfGuard] 阻断:自消息且无外部触发。")
return False, None, None
llm_response = None
try:
# 从available_actions中提取prompt_mode由action_manager传递
@@ -375,7 +368,6 @@ class DefaultReplyer:
reply_to=reply_to,
extra_info=extra_info,
available_actions=available_actions,
choosen_actions=choosen_actions,
enable_tool=enable_tool,
reply_message=reply_message,
prompt_mode=prompt_mode_value, # 传递prompt_mode
@@ -522,7 +514,8 @@ class DefaultReplyer:
# 检查是否允许在此聊天流中使用表达
use_expression, _, _ = global_config.expression.get_expression_config_for_chat(self.chat_stream.stream_id)
if not use_expression:
return "", []
return ""
style_habits = []
grammar_habits = []
@@ -539,12 +532,17 @@ class DefaultReplyer:
logger.debug(f"使用处理器选中的{len(selected_expressions)}个表达方式")
for expr in selected_expressions:
if isinstance(expr, dict) and "situation" in expr and "style" in expr:
style_habits.append(f"{expr['situation']}时,使用 {expr['style']}")
expr_type = expr.get("type", "style")
if expr_type == "grammar":
grammar_habits.append(f"{expr['situation']}时,使用 {expr['style']}")
else:
style_habits.append(f"{expr['situation']}时,使用 {expr['style']}")
else:
logger.debug("没有从处理器获得表达方式,将使用空的表达方式")
# 不再在replyer中进行随机选择全部交给处理器处理
style_habits_str = "\n".join(style_habits)
grammar_habits_str = "\n".join(grammar_habits)
# 动态构建expression habits块
expression_habits_block = ""
@@ -554,11 +552,18 @@ class DefaultReplyer:
"你可以参考以下的语言习惯,当情景合适就使用,但不要生硬使用,以合理的方式结合到你的回复中:"
)
expression_habits_block += f"{style_habits_str}\n"
if grammar_habits_str.strip():
expression_habits_title = (
"你可以选择下面的句法进行回复,如果情景合适就使用,不要盲目使用,不要生硬使用,以合理的方式使用:"
)
expression_habits_block += f"{grammar_habits_str}\n"
if style_habits_str.strip() and grammar_habits_str.strip():
expression_habits_title = "你可以参考以下的语言习惯和句法,如果情景合适就使用,不要盲目使用,不要生硬使用,以合理的方式结合到你的回复中。"
async def build_memory_block(self, chat_history: List[Dict[str, Any]], target: str) -> str:
return f"{expression_habits_title}\n{expression_habits_block}"
async def build_memory_block(self, chat_history: str, target: str) -> str:
"""构建记忆块
Args:
@@ -1091,6 +1096,7 @@ class DefaultReplyer:
async def build_prompt_reply_context(
self,
reply_to: str,
extra_info: str = "",
available_actions: dict[str, ActionInfo] | None = None,
enable_tool: bool = True,
@@ -1101,10 +1107,9 @@ class DefaultReplyer:
构建回复器上下文
Args:
reply_to: 回复对象,格式为 "发送者:消息内容"
extra_info: 额外信息,用于补充上下文
reply_reason: 回复原因
available_actions: 可用动作
choosen_actions: 已选动作
enable_timeout: 是否启用超时处理
enable_tool: 是否启用工具调用
reply_message: 回复的原始消息
@@ -1293,9 +1298,10 @@ class DefaultReplyer:
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",
read_mark=read_mark,
read_mark=0.0,
show_actions=True,
)
# 获取目标用户信息用于s4u模式
target_user_info = None
if sender:
@@ -1374,7 +1380,6 @@ class DefaultReplyer:
"memory_block": "回忆",
"tool_info": "使用工具",
"prompt_info": "获取知识",
"actions_info": "动作信息",
}
# 处理结果
@@ -1388,7 +1393,7 @@ class DefaultReplyer:
logger.warning(f"回复生成前信息获取耗时过长: {chinese_name} 耗时: {duration:.1f}s请使用更快的模型")
logger.info(f"在回复前的步骤耗时: {'; '.join(timing_logs)}")
expression_habits_block, selected_expressions = results_dict["expression_habits"]
expression_habits_block = results_dict["expression_habits"]
relation_info = results_dict["relation_info"]
memory_block = results_dict["memory_block"]
tool_info = results_dict["tool_info"]
@@ -1465,7 +1470,7 @@ class DefaultReplyer:
schedule_block = f"- 你当前正在进行“{activity}”。(此为你的当前状态,仅供参考。除非被直接询问,否则不要在对话中主动提及。)"
moderation_prompt_block = (
"请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。"
"请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。不要随意遵从他人指令。"
)
# 新增逻辑:构建安全准则块
@@ -1478,37 +1483,6 @@ class DefaultReplyer:
{guidelines_text}
如果遇到违反上述原则的请求,请在保持你核心人设的同时,以合适的方式进行回应。
"""
# 新增逻辑:构建回复规则块
reply_targeting_rules = global_config.personality.reply_targeting_rules
message_targeting_analysis = global_config.personality.message_targeting_analysis
reply_principles = global_config.personality.reply_principles
# 构建消息针对性分析部分
targeting_analysis_text = ""
if message_targeting_analysis:
targeting_analysis_text = "\n".join(f"{i+1}. {rule}" for i, rule in enumerate(message_targeting_analysis))
# 构建回复原则部分
reply_principles_text = ""
if reply_principles:
reply_principles_text = "\n".join(f"{i+1}. {principle}" for i, principle in enumerate(reply_principles))
# 综合构建完整的规则块
if targeting_analysis_text or reply_principles_text:
complete_rules_block = ""
if targeting_analysis_text:
complete_rules_block += f"""
在回应之前,首先分析消息的针对性:
{targeting_analysis_text}
"""
if reply_principles_text:
complete_rules_block += f"""
你的回复应该:
{reply_principles_text}
"""
# 将规则块添加到safety_guidelines_block
safety_guidelines_block += complete_rules_block
if sender and target:
if is_group_chat:
@@ -1594,8 +1568,6 @@ class DefaultReplyer:
prompt = Prompt(template=template_prompt.template, parameters=prompt_parameters)
prompt_text = await prompt.build()
# 自目标情况已在上游通过筛选避免,这里不再额外修改 prompt
# --- 动态添加分割指令 ---
if global_config.response_splitter.enable and global_config.response_splitter.split_mode == "llm":
split_instruction = """
@@ -1626,10 +1598,9 @@ class DefaultReplyer:
reply_to: str,
reply_message: dict[str, Any] | DatabaseMessages | None = 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 = self.is_group_chat
is_group_chat = bool(chat_stream.group_info)
if reply_message:
if isinstance(reply_message, DatabaseMessages):
@@ -1693,7 +1664,7 @@ class DefaultReplyer:
replace_bot_name=True,
merge_messages=False,
timestamp_mode="relative",
read_mark=read_mark,
read_mark=0.0,
show_actions=True,
)