- libs: 新增 hr_sdk / hotel_report / dingtalk,更新 desktopauto - controlplane-next: SDK 管理模块(controller/entity/migrations)与前端视图 - electron-shell: 插件同步、注册表、runner 调整 - plugins: XQ0035/XQ0041/XQ0042/XQ0045_XQ0046/XQ0048/XQ0076/XQ0078 等版本更新 - tests/tools: 新增 SDK 兼容性检查与打包工具(sdk_pack.py、sdk_compat_check.py)
140 lines
5.3 KiB
JavaScript
140 lines
5.3 KiB
JavaScript
// 插件注册表:读取 plugins/<id>/plugin.json 元数据(manifest 驱动,与 Python meta.py 语义一致)。
|
||
"use strict";
|
||
|
||
const fs = require("fs");
|
||
const path = require("path");
|
||
const config = require("./config");
|
||
const { makeLogger } = require("./logger");
|
||
|
||
const logger = makeLogger("registry");
|
||
|
||
function pluginRoot() {
|
||
return config.pluginRoot();
|
||
}
|
||
|
||
// 本地已落盘插件条目(目录包含 __init__.py 或 plugin.json,后者兜底下载中断的残缺目录)
|
||
function listLocalPlugins() {
|
||
const root = pluginRoot();
|
||
const out = [];
|
||
if (!fs.existsSync(root)) return out;
|
||
for (const name of fs.readdirSync(root)) {
|
||
if (name.startsWith("_") || name === "__pycache__") continue;
|
||
const full = path.join(root, name);
|
||
let stat;
|
||
try {
|
||
stat = fs.statSync(full);
|
||
} catch (e) {
|
||
continue;
|
||
}
|
||
if (stat.isDirectory()) {
|
||
if (fs.existsSync(path.join(full, "__init__.py")) || fs.existsSync(path.join(full, "plugin.json"))) {
|
||
out.push({ id: name, dir: full });
|
||
}
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// 读取插件 manifest;缺文件/解析失败返回 null(不阻断)
|
||
function readManifest(pluginId) {
|
||
const mf = path.join(pluginRoot(), pluginId, "plugin.json");
|
||
if (!fs.existsSync(mf)) return null;
|
||
try {
|
||
const data = JSON.parse(fs.readFileSync(mf, "utf-8"));
|
||
return data && typeof data === "object" ? data : null;
|
||
} catch (e) {
|
||
logger.warning(`插件 ${pluginId} plugin.json 解析失败:${e.message}`);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// JSON-Schema(object.properties) → 前端表单 [{key,label,type,default,options,description}]
|
||
function fieldsFromSchema(schema) {
|
||
if (!schema || schema.type !== "object" || !schema.properties) return null;
|
||
const out = [];
|
||
for (const [key, prop] of Object.entries(schema.properties)) {
|
||
const item = { key, label: prop.title || key, default: prop.default ?? "" };
|
||
const t = prop.type || "string";
|
||
if (t === "integer" || t === "number") item.type = "int";
|
||
else if (t === "string" && Array.isArray(prop.enum)) {
|
||
item.type = "select";
|
||
item.options = prop.enum;
|
||
} else if (t === "string" && prop.format === "password") item.type = "password";
|
||
else if (t === "array") {
|
||
item.type = "multiselect";
|
||
if (prop.items && Array.isArray(prop.items.enum)) item.options = prop.items.enum;
|
||
} else item.type = "text";
|
||
if (prop.description) item.description = prop.description;
|
||
out.push(item);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// 插件级执行超时(秒):plugin.json.runTimeoutSeconds;0=不限时,缺省/非法返回 null(回退壳默认)
|
||
function parseRunTimeout(manifest) {
|
||
if (manifest && manifest.runTimeoutSeconds !== undefined) {
|
||
const v = Number(manifest.runTimeoutSeconds);
|
||
if (Number.isFinite(v) && v >= 0) return v;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// 组装插件元数据(manifest 驱动,与 Python meta._plugin_schema 一致)
|
||
function pluginSchema(pluginId) {
|
||
const manifest = readManifest(pluginId);
|
||
const base = {
|
||
id: pluginId,
|
||
name: pluginId,
|
||
version: "0",
|
||
description: "",
|
||
uses_month: false,
|
||
ui: false,
|
||
capabilities: [],
|
||
permissions: {},
|
||
fields: [],
|
||
};
|
||
if (!manifest) return base;
|
||
const fields = fieldsFromSchema(manifest.fields);
|
||
return {
|
||
id: manifest.id || pluginId,
|
||
name: manifest.name || pluginId,
|
||
version: manifest.version || "0",
|
||
description: manifest.description || "",
|
||
category: manifest.category || "", // 场景分类(左侧场景库按此分组折叠,空则归"其他")
|
||
uses_month: !!manifest.uses_month,
|
||
ui: !!manifest.ui,
|
||
platform: manifest.platform || "", // hr / oa / ""(前端平台徽章 + 运行并发闸按(账号,平台)隔离)
|
||
platforms: Array.isArray(manifest.platforms) && manifest.platforms.length
|
||
? manifest.platforms // 插件声明的全部平台依赖(运行前登录态预检按此逐个检查)
|
||
: manifest.platform ? [manifest.platform] : [], // 缺省回退单 platform 字段
|
||
min_runtime_version: manifest.minRuntimeVersion || "", // 版本兼容校验声明(安装时强校验,此处透传前端展示)
|
||
timeout_seconds: parseRunTimeout(manifest), // 插件级执行超时(秒,0=不限时,null=回退壳默认)
|
||
capabilities: manifest.capabilities || [],
|
||
permissions: manifest.permissions || {},
|
||
// 依赖的 SDK(requires.sdks 的键)与声明范围,供前端/设置页展示"缺少哪个 SDK"
|
||
requires_sdks: Object.entries((manifest.requires || {}).sdks || {}).map(([id, conf]) => ({
|
||
id,
|
||
compat: String((conf || {}).compat || "*"),
|
||
optional: !!((conf || {}).optional),
|
||
})),
|
||
fields: fields || [],
|
||
};
|
||
}
|
||
|
||
function find(pluginId) {
|
||
const dir = path.join(pluginRoot(), pluginId);
|
||
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) throw new Error(`插件不存在: ${pluginId}`);
|
||
const manifest = readManifest(pluginId);
|
||
if (!manifest && !fs.existsSync(path.join(dir, "__init__.py"))) {
|
||
throw new Error(`插件不存在: ${pluginId}`);
|
||
}
|
||
return { id: pluginId, dir, manifest, ...pluginSchema(pluginId) };
|
||
}
|
||
|
||
function findByName(name) {
|
||
// 供 /api/runs 以插件名查找(与 PLUGINS.find 语义对齐)
|
||
return find(name);
|
||
}
|
||
|
||
module.exports = { pluginRoot, listLocalPlugins, readManifest, fieldsFromSchema, pluginSchema, find };
|