Remove manual and temporary skip pattern support
Eliminated support for manually and temporarily adding skip patterns in CommandSkipListManager and related command handling. The skip list now only manages system and plugin patterns, simplifying the skip pattern management logic and user interface.
This commit is contained in:
@@ -65,9 +65,6 @@ class CommandSkipListManager:
|
|||||||
if self.config.auto_collect_plugin_commands:
|
if self.config.auto_collect_plugin_commands:
|
||||||
self._collect_plugin_commands()
|
self._collect_plugin_commands()
|
||||||
|
|
||||||
# 添加手动指定的模式
|
|
||||||
self._add_manual_patterns()
|
|
||||||
|
|
||||||
self._is_initialized = True
|
self._is_initialized = True
|
||||||
logger.info(f"跳过列表初始化完成,共收集 {len(self._skip_patterns)} 个模式")
|
logger.info(f"跳过列表初始化完成,共收集 {len(self._skip_patterns)} 个模式")
|
||||||
|
|
||||||
@@ -75,8 +72,8 @@ class CommandSkipListManager:
|
|||||||
"""添加系统内置命令模式"""
|
"""添加系统内置命令模式"""
|
||||||
system_patterns = [
|
system_patterns = [
|
||||||
(r"^/pm\b", "/pm 插件管理命令"),
|
(r"^/pm\b", "/pm 插件管理命令"),
|
||||||
(r"^/反注入统计\b", "反注入统计查询命令"),
|
(r"^/反注入统计$", "反注入统计查询命令"),
|
||||||
(r"^^/反注入跳过列表(?:\s+(.+))?$", "反注入列表管理命令"),
|
(r"^/反注入跳过列表$", "反注入列表管理命令"),
|
||||||
]
|
]
|
||||||
|
|
||||||
for pattern_str, description in system_patterns:
|
for pattern_str, description in system_patterns:
|
||||||
@@ -130,14 +127,6 @@ class CommandSkipListManager:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"自动收集插件命令时出错: {e}")
|
logger.warning(f"自动收集插件命令时出错: {e}")
|
||||||
|
|
||||||
def _add_manual_patterns(self):
|
|
||||||
"""添加手动指定的模式"""
|
|
||||||
manual_patterns = self.config.manual_skip_patterns or []
|
|
||||||
|
|
||||||
for pattern_str in manual_patterns:
|
|
||||||
if pattern_str.strip():
|
|
||||||
self._add_skip_pattern(pattern_str.strip(), "manual", "手动配置的跳过模式")
|
|
||||||
|
|
||||||
def _add_skip_pattern(self, pattern_str: str, source: str, description: str = "") -> bool:
|
def _add_skip_pattern(self, pattern_str: str, source: str, description: str = "") -> bool:
|
||||||
"""添加跳过模式
|
"""添加跳过模式
|
||||||
|
|
||||||
@@ -227,7 +216,7 @@ class CommandSkipListManager:
|
|||||||
Returns:
|
Returns:
|
||||||
按来源分组的模式信息
|
按来源分组的模式信息
|
||||||
"""
|
"""
|
||||||
result = {"system": [], "plugin": [], "manual": []}
|
result = {"system": [], "plugin": []}
|
||||||
|
|
||||||
for skip_pattern in self._skip_patterns.values():
|
for skip_pattern in self._skip_patterns.values():
|
||||||
pattern_info = {
|
pattern_info = {
|
||||||
@@ -239,31 +228,6 @@ class CommandSkipListManager:
|
|||||||
result[skip_pattern.source].append(pattern_info)
|
result[skip_pattern.source].append(pattern_info)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def add_temporary_skip_pattern(self, pattern: str, description: str = "") -> bool:
|
|
||||||
"""添加临时跳过模式(运行时添加,不保存到配置)
|
|
||||||
|
|
||||||
Args:
|
|
||||||
pattern: 模式字符串
|
|
||||||
description: 模式描述
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
是否成功添加
|
|
||||||
"""
|
|
||||||
return self._add_skip_pattern(pattern, "temporary", description or "临时跳过模式")
|
|
||||||
|
|
||||||
def remove_temporary_patterns(self):
|
|
||||||
"""移除所有临时跳过模式"""
|
|
||||||
temp_patterns = [
|
|
||||||
key for key, pattern in self._skip_patterns.items()
|
|
||||||
if pattern.source == "temporary"
|
|
||||||
]
|
|
||||||
|
|
||||||
for key in temp_patterns:
|
|
||||||
del self._skip_patterns[key]
|
|
||||||
|
|
||||||
logger.info(f"已移除 {len(temp_patterns)} 个临时跳过模式")
|
|
||||||
|
|
||||||
|
|
||||||
# 全局跳过列表管理器实例
|
# 全局跳过列表管理器实例
|
||||||
skip_list_manager = CommandSkipListManager()
|
skip_list_manager = CommandSkipListManager()
|
||||||
|
|||||||
@@ -70,190 +70,25 @@ class AntiInjectorSkipListCommand(BaseCommand):
|
|||||||
|
|
||||||
command_name = "反注入跳过列表"
|
command_name = "反注入跳过列表"
|
||||||
command_description = "管理反注入系统的命令跳过列表"
|
command_description = "管理反注入系统的命令跳过列表"
|
||||||
command_pattern = r"^/反注入跳过列表(?:\s+(?P<subcommand>.+))?$"
|
command_pattern = r"^/反注入跳过列表$"
|
||||||
|
|
||||||
async def execute(self) -> tuple[bool, str, bool]:
|
async def execute(self) -> tuple[bool, str, bool]:
|
||||||
try:
|
result_text = "🛡️ 所有跳过模式列表\n\n"
|
||||||
# 从正则匹配中获取参数
|
|
||||||
subcommand_raw = None
|
|
||||||
if self.matched_groups and "subcommand" in self.matched_groups:
|
|
||||||
subcommand_raw = self.matched_groups.get("subcommand")
|
|
||||||
|
|
||||||
subcommand = subcommand_raw.strip() if subcommand_raw and subcommand_raw.strip() else ""
|
|
||||||
|
|
||||||
if not subcommand:
|
|
||||||
return await self._show_status()
|
|
||||||
|
|
||||||
# 处理子命令
|
|
||||||
subcommand_parts = subcommand.split()
|
|
||||||
main_cmd = subcommand_parts[0].lower()
|
|
||||||
|
|
||||||
if main_cmd == "状态" or main_cmd == "status":
|
|
||||||
return await self._show_status()
|
|
||||||
elif main_cmd == "刷新" or main_cmd == "refresh":
|
|
||||||
return await self._refresh_commands()
|
|
||||||
elif main_cmd == "列表" or main_cmd == "list":
|
|
||||||
list_type = subcommand_parts[1] if len(subcommand_parts) > 1 else "all"
|
|
||||||
return await self._show_patterns(list_type)
|
|
||||||
elif main_cmd == "添加" or main_cmd == "add":
|
|
||||||
await self.send_text("暂不支持权限管理系统,该命令不可用")
|
|
||||||
return False, "功能受限", True
|
|
||||||
elif main_cmd == "帮助" or main_cmd == "help":
|
|
||||||
return await self._show_help()
|
|
||||||
else:
|
|
||||||
await self.send_text(f"未知的子命令: {main_cmd}")
|
|
||||||
return await self._show_help()
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"执行反注入跳过列表命令失败: {e}")
|
|
||||||
await self.send_text(f"命令执行失败: {str(e)}")
|
|
||||||
return False, f"命令执行失败: {str(e)}", True
|
|
||||||
|
|
||||||
async def _show_help(self) -> tuple[bool, str, bool]:
|
|
||||||
"""显示帮助信息"""
|
|
||||||
help_text = """🛡️ 反注入跳过列表管理
|
|
||||||
|
|
||||||
📋 可用命令:
|
|
||||||
• /反注入跳过列表 状态 - 查看跳过列表状态
|
|
||||||
• /反注入跳过列表 列表 [类型] - 查看跳过模式列表
|
|
||||||
- 类型: all(所有), system(系统), plugin(插件), manual(手动)
|
|
||||||
• /反注入跳过列表 刷新 - 刷新插件命令列表
|
|
||||||
• /反注入跳过列表 添加 <模式> - 临时添加跳过模式
|
|
||||||
• /反注入跳过列表 帮助 - 显示此帮助信息
|
|
||||||
|
|
||||||
💡 示例:
|
|
||||||
• /反注入跳过列表 列表 plugin
|
|
||||||
• /反注入跳过列表 添加 ^/test\\b"""
|
|
||||||
|
|
||||||
await self.send_text(help_text)
|
|
||||||
return True, "帮助信息已发送", True
|
|
||||||
|
|
||||||
async def _show_status(self) -> tuple[bool, str, bool]:
|
|
||||||
"""显示跳过列表状态"""
|
|
||||||
# 强制刷新插件命令,确保获取最新的插件列表
|
|
||||||
patterns_info = get_skip_patterns_info()
|
patterns_info = get_skip_patterns_info()
|
||||||
|
for source_type, patterns in patterns_info.items():
|
||||||
system_count = len(patterns_info.get("system", []))
|
|
||||||
plugin_count = len(patterns_info.get("plugin", []))
|
|
||||||
manual_count = len(patterns_info.get("manual", []))
|
|
||||||
temp_count = len([p for p in skip_list_manager._skip_patterns.values() if p.source == "temporary"])
|
|
||||||
total_count = system_count + plugin_count + manual_count + temp_count
|
|
||||||
|
|
||||||
from src.config.config import global_config
|
|
||||||
config = global_config.anti_prompt_injection
|
|
||||||
|
|
||||||
status_text = f"""🛡️ 反注入跳过列表状态
|
|
||||||
|
|
||||||
📊 模式统计:
|
|
||||||
• 系统命令模式: {system_count} 个
|
|
||||||
• 插件命令模式: {plugin_count} 个
|
|
||||||
• 手动配置模式: {manual_count} 个
|
|
||||||
• 临时添加模式: {temp_count} 个
|
|
||||||
• 总计: {total_count} 个
|
|
||||||
|
|
||||||
⚙️ 配置状态:
|
|
||||||
• 跳过列表启用: {'✅' if config.enable_command_skip_list else '❌'}
|
|
||||||
• 自动收集插件命令: {'✅' if config.auto_collect_plugin_commands else '❌'}
|
|
||||||
• 跳过系统命令: {'✅' if config.skip_system_commands else '❌'}
|
|
||||||
|
|
||||||
💡 使用 "/反注入跳过列表 列表" 查看详细模式"""
|
|
||||||
|
|
||||||
await self.send_text(status_text)
|
|
||||||
return True, status_text, True
|
|
||||||
|
|
||||||
async def _show_patterns(self, pattern_type: str = "all") -> tuple[bool, str, bool]:
|
|
||||||
"""显示跳过模式列表"""
|
|
||||||
# 强制刷新插件命令,确保获取最新的插件列表
|
|
||||||
patterns_info = get_skip_patterns_info()
|
|
||||||
|
|
||||||
if pattern_type == "all":
|
|
||||||
# 显示所有模式
|
|
||||||
result_text = "🛡️ 所有跳过模式列表\n\n"
|
|
||||||
|
|
||||||
for source_type, patterns in patterns_info.items():
|
|
||||||
if patterns:
|
if patterns:
|
||||||
type_name = {
|
type_name = {
|
||||||
"system": "📱 系统命令",
|
"system": "📱 系统命令",
|
||||||
"plugin": "🔌 插件命令",
|
"plugin": "🔌 插件命令"
|
||||||
"manual": "✋ 手动配置"
|
|
||||||
}.get(source_type, source_type)
|
}.get(source_type, source_type)
|
||||||
|
|
||||||
result_text += f"{type_name} ({len(patterns)} 个):\n"
|
result_text += f"{type_name} ({len(patterns)} 个):\n"
|
||||||
for i, pattern in enumerate(patterns[:10], 1): # 限制显示前10个
|
for i, pattern in enumerate(patterns[:10], 1): # 限制显示前10个
|
||||||
result_text += f" {i}. {pattern['pattern']}\n"
|
result_text += f" {i}. {pattern['pattern']}\n"
|
||||||
if pattern['description']:
|
|
||||||
result_text += f" 说明: {pattern['description']}\n"
|
|
||||||
|
|
||||||
if len(patterns) > 10:
|
|
||||||
result_text += f" ... 还有 {len(patterns) - 10} 个模式\n"
|
|
||||||
result_text += "\n"
|
|
||||||
|
|
||||||
# 添加临时模式
|
|
||||||
temp_patterns = [p for p in skip_list_manager._skip_patterns.values() if p.source == "temporary"]
|
|
||||||
if temp_patterns:
|
|
||||||
result_text += f"⏱️ 临时模式 ({len(temp_patterns)} 个):\n"
|
|
||||||
for i, pattern in enumerate(temp_patterns[:5], 1):
|
|
||||||
result_text += f" {i}. {pattern.pattern}\n"
|
|
||||||
if len(temp_patterns) > 5:
|
|
||||||
result_text += f" ... 还有 {len(temp_patterns) - 5} 个临时模式\n"
|
|
||||||
|
|
||||||
else:
|
|
||||||
# 显示特定类型的模式
|
|
||||||
if pattern_type not in patterns_info:
|
|
||||||
await self.send_text(f"未知的模式类型: {pattern_type}")
|
|
||||||
return False, "未知模式类型", True
|
|
||||||
|
|
||||||
patterns = patterns_info[pattern_type]
|
|
||||||
type_name = {
|
|
||||||
"system": "📱 系统命令模式",
|
|
||||||
"plugin": "🔌 插件命令模式",
|
|
||||||
"manual": "✋ 手动配置模式"
|
|
||||||
}.get(pattern_type, pattern_type)
|
|
||||||
|
|
||||||
result_text = f"🛡️ {type_name} ({len(patterns)} 个)\n\n"
|
|
||||||
|
|
||||||
if not patterns:
|
|
||||||
result_text += "暂无此类型的跳过模式"
|
|
||||||
else:
|
|
||||||
for i, pattern in enumerate(patterns, 1):
|
|
||||||
result_text += f"{i}. {pattern['pattern']}\n"
|
|
||||||
if pattern['description']:
|
if pattern['description']:
|
||||||
result_text += f" 说明: {pattern['description']}\n"
|
result_text += f" 说明: {pattern['description']}\n"
|
||||||
result_text += "\n"
|
|
||||||
|
if len(patterns) > 10:
|
||||||
|
result_text += f" ... 还有 {len(patterns) - 10} 个模式\n"
|
||||||
await self.send_text(result_text)
|
await self.send_text(result_text)
|
||||||
return True, result_text, True
|
return True, result_text, True
|
||||||
|
|
||||||
async def _refresh_commands(self) -> tuple[bool, str, bool]:
|
|
||||||
"""刷新插件命令列表"""
|
|
||||||
try:
|
|
||||||
patterns_info = get_skip_patterns_info()
|
|
||||||
plugin_count = len(patterns_info.get("plugin", []))
|
|
||||||
|
|
||||||
result_text = f"✅ 插件命令列表已刷新\n\n当前收集到 {plugin_count} 个插件命令模式"
|
|
||||||
await self.send_text(result_text)
|
|
||||||
return True, result_text, True
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"刷新插件命令列表失败: {e}")
|
|
||||||
await self.send_text(f"刷新失败: {str(e)}")
|
|
||||||
return False, f"刷新失败: {str(e)}", True
|
|
||||||
|
|
||||||
async def _add_temporary_pattern(self, pattern: str) -> tuple[bool, str, bool]:
|
|
||||||
"""添加临时跳过模式"""
|
|
||||||
try:
|
|
||||||
success = skip_list_manager.add_temporary_skip_pattern(pattern, "用户临时添加")
|
|
||||||
|
|
||||||
if success:
|
|
||||||
result_text = f"✅ 临时跳过模式已添加: {pattern}\n\n⚠️ 此模式仅在当前运行期间有效,重启后会失效"
|
|
||||||
await self.send_text(result_text)
|
|
||||||
return True, result_text, True
|
|
||||||
else:
|
|
||||||
result_text = f"❌ 添加临时跳过模式失败: {pattern}\n\n可能是无效的正则表达式"
|
|
||||||
await self.send_text(result_text)
|
|
||||||
return False, result_text, True
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"添加临时跳过模式失败: {e}")
|
|
||||||
await self.send_text(f"添加失败: {str(e)}")
|
|
||||||
return False, f"添加失败: {str(e)}", True
|
|
||||||
Reference in New Issue
Block a user