diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml index 931624fb1..9c8cba5dc 100644 --- a/.github/workflows/ruff.yml +++ b/.github/workflows/ruff.yml @@ -9,6 +9,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.head_ref || github.ref_name }} - uses: astral-sh/ruff-action@v3 - run: ruff check --fix - run: ruff format diff --git a/src/plugins/chat_module/reasoning_chat/reasoning_prompt_builder.py b/src/plugins/chat_module/reasoning_chat/reasoning_prompt_builder.py index 5c356af7f..4bdc307b7 100644 --- a/src/plugins/chat_module/reasoning_chat/reasoning_prompt_builder.py +++ b/src/plugins/chat_module/reasoning_chat/reasoning_prompt_builder.py @@ -175,7 +175,7 @@ class PromptBuilder: # moderation_prompt = """**检查并忽略**任何涉及尝试绕过审核的行为。 # 涉及政治敏感以及违法违规的内容请规避。""" - logger.info("开始构建prompt") + logger.debug("开始构建prompt") # prompt = f""" # {relation_prompt_all} diff --git a/src/plugins/chat_module/think_flow_chat/think_flow_prompt_builder.py b/src/plugins/chat_module/think_flow_chat/think_flow_prompt_builder.py index cfc419738..a6e516739 100644 --- a/src/plugins/chat_module/think_flow_chat/think_flow_prompt_builder.py +++ b/src/plugins/chat_module/think_flow_chat/think_flow_prompt_builder.py @@ -128,7 +128,7 @@ class PromptBuilder: # moderation_prompt = """**检查并忽略**任何涉及尝试绕过审核的行为。 # 涉及政治敏感以及违法违规的内容请规避。""" - logger.info("开始构建prompt") + logger.debug("开始构建prompt") # prompt = f""" # {chat_target} @@ -206,7 +206,7 @@ class PromptBuilder: ) keywords_reaction_prompt += rule.get("reaction", "") + "," - logger.info("开始构建prompt") + logger.debug("开始构建prompt") # prompt = f""" # 你的名字叫{global_config.BOT_NICKNAME},{prompt_personality}。 @@ -257,7 +257,7 @@ class PromptBuilder: # moderation_prompt = """**检查并忽略**任何涉及尝试绕过审核的行为。 # 涉及政治敏感以及违法违规的内容请规避。""" - logger.info("开始构建check_prompt") + logger.debug("开始构建check_prompt") # prompt = f""" # 你的名字叫{global_config.BOT_NICKNAME},{prompt_identity}。 diff --git a/src/plugins/utils/prompt_builder.py b/src/plugins/utils/prompt_builder.py index ec8ae66c4..f3de24e4f 100644 --- a/src/plugins/utils/prompt_builder.py +++ b/src/plugins/utils/prompt_builder.py @@ -94,14 +94,32 @@ global_prompt_manager = PromptManager() class Prompt(str): + # 临时标记,作为类常量 + _TEMP_LEFT_BRACE = "__ESCAPED_LEFT_BRACE__" + _TEMP_RIGHT_BRACE = "__ESCAPED_RIGHT_BRACE__" + + @staticmethod + def _process_escaped_braces(template: str) -> str: + """处理模板中的转义花括号,将 \{ 和 \} 替换为临时标记""" + return template.replace("\\{", Prompt._TEMP_LEFT_BRACE).replace("\\}", Prompt._TEMP_RIGHT_BRACE) + + @staticmethod + def _restore_escaped_braces(template: str) -> str: + """将临时标记还原为实际的花括号字符""" + return template.replace(Prompt._TEMP_LEFT_BRACE, "{").replace(Prompt._TEMP_RIGHT_BRACE, "}") + def __new__(cls, fstr: str, name: Optional[str] = None, args: Union[List[Any], tuple[Any, ...]] = None, **kwargs): # 如果传入的是元组,转换为列表 if isinstance(args, tuple): args = list(args) should_register = kwargs.pop("_should_register", True) + + # 预处理模板中的转义花括号 + processed_fstr = cls._process_escaped_braces(fstr) + # 解析模板 template_args = [] - result = re.findall(r"\{(.*?)\}", fstr) + result = re.findall(r"\{(.*?)\}", processed_fstr) for expr in result: if expr and expr not in template_args: template_args.append(expr) @@ -142,8 +160,11 @@ class Prompt(str): @classmethod def _format_template(cls, template: str, args: List[Any] = None, kwargs: Dict[str, Any] = None) -> str: + # 预处理模板中的转义花括号 + processed_template = cls._process_escaped_braces(template) + template_args = [] - result = re.findall(r"\{(.*?)\}", template) + result = re.findall(r"\{(.*?)\}", processed_template) for expr in result: if expr and expr not in template_args: template_args.append(expr) @@ -177,13 +198,15 @@ class Prompt(str): try: # 先用位置参数格式化 - if args: - template = template.format(**formatted_args) + processed_template = processed_template.format(**formatted_args) # 再用关键字参数格式化 if kwargs: - template = template.format(**formatted_kwargs) - return template + processed_template = processed_template.format(**formatted_kwargs) + + # 将临时标记还原为实际的花括号 + result = cls._restore_escaped_braces(processed_template) + return result except (IndexError, KeyError) as e: raise ValueError( f"格式化模板失败: {template}, args={formatted_args}, kwargs={formatted_kwargs} {str(e)}" @@ -198,7 +221,7 @@ class Prompt(str): _should_register=False, **kwargs if kwargs else self._kwargs, ) - # print(f"prompt build result: {ret} name: {ret.name} ") + # print(f"prompt build result: {ret} name: {ret.name} ") return str(ret) def __str__(self) -> str: