@@ -26,11 +26,28 @@ def get_plugins():
|
||||
"""获取所有插件列表"""
|
||||
try:
|
||||
server = current_app.dashboard_server
|
||||
# 统一改为消费 PluginManager 的标准治理快照:
|
||||
# 1. 这样既能覆盖“已加载插件”,也能覆盖“发现但加载失败/配置禁用”的模块;
|
||||
# 2. 后台不必重复拼装版本、命令、依赖、配置健康等字段;
|
||||
# 3. 后续继续补错误统计、性能排名时,也只需要在快照层扩展。
|
||||
plugin_list = server.plugin_manager.get_plugin_snapshots()
|
||||
# 获取插件注册表
|
||||
plugins = server.plugin_registry.get_all_plugins()
|
||||
|
||||
# 转换为前端需要的格式
|
||||
plugin_list = []
|
||||
for name, plugin in plugins.items():
|
||||
# 获取插件模块名
|
||||
try:
|
||||
module_name = plugin.__class__.__module__.split('.')[-2]
|
||||
except (IndexError, AttributeError):
|
||||
module_name = "unknown"
|
||||
|
||||
plugin_info = {
|
||||
"name": plugin.name,
|
||||
"module_name": module_name,
|
||||
"version": getattr(plugin, 'version', 'N/A'),
|
||||
"author": getattr(plugin, 'author', 'N/A'),
|
||||
"description": getattr(plugin, 'description', 'N/A'),
|
||||
"status": plugin.status.name if hasattr(plugin, 'status') else 'UNKNOWN'
|
||||
}
|
||||
plugin_list.append(plugin_info)
|
||||
|
||||
return jsonify({"success": True, "data": plugin_list})
|
||||
except Exception as e:
|
||||
LOG.error(f"获取插件列表失败: {str(e)}", exc_info=True)
|
||||
@@ -176,10 +193,31 @@ def get_plugin_info():
|
||||
if not plugin_name:
|
||||
return jsonify({"success": False, "message": "缺少插件名称参数"})
|
||||
|
||||
plugin_info = server.plugin_manager.get_plugin_snapshot(plugin_name)
|
||||
if not plugin_info:
|
||||
# 获取插件管理器
|
||||
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
|
||||
|
||||
if not plugin:
|
||||
return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"})
|
||||
|
||||
# 获取插件模块名
|
||||
try:
|
||||
module_name = plugin.__class__.__module__.split('.')[-2]
|
||||
except (IndexError, AttributeError):
|
||||
module_name = "unknown"
|
||||
|
||||
# 构建详细信息
|
||||
plugin_info = {
|
||||
"name": plugin.name,
|
||||
"module_name": module_name,
|
||||
"version": getattr(plugin, 'version', 'N/A'),
|
||||
"author": getattr(plugin, 'author', 'N/A'),
|
||||
"description": getattr(plugin, 'description', 'N/A'),
|
||||
"status": plugin.status.name if hasattr(plugin, 'status') else 'UNKNOWN',
|
||||
"command_prefix": getattr(plugin, 'command_prefix', ''),
|
||||
"commands": getattr(plugin, 'commands', []),
|
||||
"config": getattr(plugin, '_config', {})
|
||||
}
|
||||
|
||||
return jsonify({"success": True, "data": plugin_info})
|
||||
except Exception as e:
|
||||
LOG.error(f"获取插件详情失败: {str(e)}", exc_info=True)
|
||||
@@ -197,14 +235,9 @@ def enable_plugin():
|
||||
if not plugin_name:
|
||||
return jsonify({"success": False, "message": "缺少插件名称参数"})
|
||||
|
||||
# 已加载插件直接启动;尚未加载的插件则先尝试加载,再进入启动流程。
|
||||
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
|
||||
if not plugin:
|
||||
plugin = server.plugin_manager.load_plugin(plugin_name)
|
||||
if plugin:
|
||||
display_name = plugin.name
|
||||
|
||||
if plugin and server.plugin_manager.start_plugin(display_name or plugin_name):
|
||||
# 获取插件管理器
|
||||
# 启用插件
|
||||
if server.plugin_manager.start_plugin(plugin_name):
|
||||
return jsonify({"success": True, "message": f"插件 {plugin_name} 启用成功"})
|
||||
else:
|
||||
return jsonify({"success": False, "message": f"插件 {plugin_name} 启用失败"})
|
||||
@@ -245,14 +278,8 @@ def reload_plugin():
|
||||
if not plugin_name:
|
||||
return jsonify({"success": False, "message": "缺少插件名称参数"})
|
||||
|
||||
# 已加载插件优先走重载;若当前未加载,则退化为“重新尝试加载并启动”。
|
||||
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
|
||||
if plugin:
|
||||
reloaded_plugin = server.plugin_manager.reload_plugin(plugin_name)
|
||||
else:
|
||||
reloaded_plugin = server.plugin_manager.load_plugin(plugin_name)
|
||||
if reloaded_plugin:
|
||||
server.plugin_manager.start_plugin(reloaded_plugin.name)
|
||||
# 重载插件
|
||||
reloaded_plugin = server.plugin_manager.reload_plugin(plugin_name)
|
||||
|
||||
if reloaded_plugin:
|
||||
return jsonify({"success": True, "message": f"插件 {plugin_name} 重载成功"})
|
||||
@@ -273,11 +300,16 @@ def get_raw_plugin_config():
|
||||
if not plugin_name:
|
||||
return jsonify({"success": False, "message": "缺少插件名称参数"})
|
||||
|
||||
plugin_snapshot = server.plugin_manager.get_plugin_snapshot(plugin_name)
|
||||
if not plugin_snapshot:
|
||||
# 获取插件管理器
|
||||
|
||||
# 查找插件
|
||||
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
|
||||
|
||||
if not plugin:
|
||||
return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"})
|
||||
|
||||
config_path = str(plugin_snapshot.get("config_path", "") or "").strip()
|
||||
# 获取配置文件路径
|
||||
config_path = plugin.get_config_path()
|
||||
|
||||
if not os.path.exists(config_path):
|
||||
return jsonify({"success": False, "message": f"配置文件不存在: {config_path}"})
|
||||
@@ -317,29 +349,15 @@ def update_plugin_config():
|
||||
if not plugin_name or config_text is None:
|
||||
return jsonify({"success": False, "message": "缺少必要参数"})
|
||||
|
||||
# 查找插件
|
||||
# 获取插件管理器
|
||||
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
|
||||
plugin_snapshot = server.plugin_manager.get_plugin_snapshot(plugin_name)
|
||||
if not plugin_snapshot:
|
||||
|
||||
if not plugin:
|
||||
return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"})
|
||||
|
||||
config_path = str(plugin_snapshot.get("config_path", "") or "").strip()
|
||||
if not config_path:
|
||||
return jsonify({"success": False, "message": "插件未声明配置路径,暂不支持在线编辑"})
|
||||
|
||||
# 保存前先做格式校验:
|
||||
# 1. 避免把坏 TOML 先写回磁盘,再让插件进入“文件已坏但提示成功”的状态;
|
||||
# 2. 校验通过后再真正落盘,失败则保留线上旧配置;
|
||||
# 3. 这也是插件治理中心第一阶段的“配置校验底座”。
|
||||
try:
|
||||
if format_type == 'toml':
|
||||
config_obj = toml.loads(config_text)
|
||||
elif format_type == 'json':
|
||||
config_obj = json.loads(config_text)
|
||||
else:
|
||||
return jsonify({"success": False, "message": f"不支持的配置格式: {format_type}"})
|
||||
except Exception as parse_error:
|
||||
LOG.error(f"解析配置失败: {str(parse_error)}", exc_info=True)
|
||||
return jsonify({"success": False, "message": f"配置格式校验失败: {str(parse_error)}"})
|
||||
# 获取配置文件路径
|
||||
config_path = plugin.get_config_path()
|
||||
|
||||
# 确保配置目录存在
|
||||
os.makedirs(os.path.dirname(config_path), exist_ok=True)
|
||||
@@ -348,11 +366,22 @@ def update_plugin_config():
|
||||
with open(config_path, 'w', encoding='utf-8') as f:
|
||||
f.write(config_text)
|
||||
|
||||
# 若插件当前已加载,则同步刷新内存中的配置镜像,减少“保存后详情弹窗仍是旧配置”的困惑。
|
||||
if plugin:
|
||||
# 解析配置并更新插件内部配置
|
||||
try:
|
||||
if format_type == 'toml':
|
||||
config_obj = toml.loads(config_text)
|
||||
elif format_type == 'json':
|
||||
config_obj = json.loads(config_text)
|
||||
else:
|
||||
return jsonify({"success": False, "message": f"不支持的配置格式: {format_type}"})
|
||||
|
||||
# 更新插件内部配置
|
||||
plugin._config = config_obj
|
||||
|
||||
return jsonify({"success": True, "message": "配置已保存并通过格式校验"})
|
||||
return jsonify({"success": True, "message": "配置已保存"})
|
||||
except Exception as e:
|
||||
LOG.error(f"解析配置失败: {str(e)}", exc_info=True)
|
||||
return jsonify({"success": False, "message": f"配置已保存,但解析失败: {str(e)}"})
|
||||
|
||||
except Exception as e:
|
||||
LOG.error(f"更新插件配置失败: {str(e)}", exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user