From d915bf52d5cf8c903fe6d52fc3bb0ba3be6bf19c Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Fri, 14 Nov 2025 13:34:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(prompt):=20=E6=94=AF=E6=8C=81=E6=8C=89?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=90=8D=E7=A7=B0=E6=89=B9=E9=87=8F=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E6=B3=A8=E5=85=A5=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 `remove_all_rules_by_component_name` 方法,用于一次性移除指定提示词组件在所有目标上的注入规则。 此功能简化了组件的停用和清理流程,特别是在动态管理和热插拔组件的场景下,无需再手动遍历所有可能的目标提示词来逐一移除规则。 --- src/chat/utils/prompt_component_manager.py | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/chat/utils/prompt_component_manager.py b/src/chat/utils/prompt_component_manager.py index bed098009..bbd93a64b 100644 --- a/src/chat/utils/prompt_component_manager.py +++ b/src/chat/utils/prompt_component_manager.py @@ -169,6 +169,37 @@ class PromptComponentManager: logger.warning(f"尝试移除注入规则失败: 未找到 '{prompt_name}' on '{target_prompt}'") return False + async def remove_all_rules_by_component_name(self, prompt_name: str) -> bool: + """ + 按组件名称移除其所有相关的注入规则。 + + 此方法会遍历管理器中所有的目标提示词,并移除所有与给定的 `prompt_name` + 相关联的注入规则。这对于清理或禁用某个组件的所有注入行为非常有用。 + + Args: + prompt_name (str): 要移除规则的组件的名称。 + + Returns: + bool: 如果至少移除了一条规则,则返回 True;否则返回 False。 + """ + removed = False + async with self._lock: + # 创建一个目标列表的副本进行迭代,因为我们可能会在循环中修改字典 + for target_prompt in list(self._dynamic_rules.keys()): + if prompt_name in self._dynamic_rules[target_prompt]: + del self._dynamic_rules[target_prompt][prompt_name] + removed = True + logger.info(f"成功移除注入规则: '{prompt_name}' from '{target_prompt}'") + # 如果目标下已无任何规则,则清理掉这个键 + if not self._dynamic_rules[target_prompt]: + del self._dynamic_rules[target_prompt] + logger.debug(f"目标 '{target_prompt}' 已空,已被移除。") + + if not removed: + logger.warning(f"尝试移除组件 '{prompt_name}' 的所有规则失败: 未找到任何相关规则。") + + return removed + # --- 核心注入逻辑 --- async def apply_injections(