feat(plugin): 防止禁用最后一个启用的 Chatter 组件

为了确保系统的核心对话功能始终可用,在禁用插件时增加了保护机制。

该机制会检查目标插件是否包含 Chatter 组件。如果是,它会进一步判断禁用该插件是否会导致系统中没有任何已启用的 Chatter 组件。如果出现这种情况,禁用操作将被阻止并返回失败,从而避免因误操作导致系统核心功能失效。
This commit is contained in:
minecraft1024a
2025-12-06 18:40:02 +08:00
parent 70c8557e02
commit af59966d8b

View File

@@ -267,6 +267,7 @@ async def disable_plugin(plugin_name: str,) -> bool:
禁用一个插件。 禁用一个插件。
禁用插件不会卸载它,只会标记为禁用状态。 禁用插件不会卸载它,只会标记为禁用状态。
包含对 Chatter 组件的保护机制,防止禁用最后一个启用的 Chatter。
Args: Args:
plugin_name (str): 要禁用的插件名称。 plugin_name (str): 要禁用的插件名称。
@@ -280,6 +281,42 @@ async def disable_plugin(plugin_name: str,) -> bool:
logger.warning(f"插件 '{plugin_name}' 未加载,无需禁用。") logger.warning(f"插件 '{plugin_name}' 未加载,无需禁用。")
return True return True
# Chatter 保护检查:确保系统中至少有一个 Chatter 组件处于启用状态
try:
from src.plugin_system.base.component_types import ComponentType
from src.plugin_system.core.component_registry import component_registry
# 获取该插件的所有组件
plugin_info = component_registry.get_plugin_info(plugin_name)
if plugin_info:
# 检查插件是否包含 Chatter 组件
has_chatter = any(
comp.component_type == ComponentType.CHATTER
for comp in plugin_info.components
)
if has_chatter:
# 获取所有启用的 Chatter 组件
enabled_chatters = component_registry.get_enabled_components_by_type(ComponentType.CHATTER)
# 统计该插件中启用的 Chatter 数量
plugin_enabled_chatters = [
comp.name for comp in plugin_info.components
if comp.component_type == ComponentType.CHATTER
and comp.name in enabled_chatters
]
# 如果禁用此插件会导致没有可用的 Chatter则阻止操作
if len(enabled_chatters) <= len(plugin_enabled_chatters):
logger.warning(
f"操作被阻止:禁用插件 '{plugin_name}' 将导致系统中没有可用的 Chatter 组件。"
f"至少需要保持一个 Chatter 组件处于启用状态。"
)
return False
except Exception as e:
logger.warning(f"检查 Chatter 保护机制时发生错误: {e}")
# 即使检查失败,也继续执行禁用操作(降级处理)
# 设置插件为禁用状态 # 设置插件为禁用状态
plugin_instance.enable_plugin = False plugin_instance.enable_plugin = False
logger.info(f"插件 '{plugin_name}' 已禁用。") logger.info(f"插件 '{plugin_name}' 已禁用。")