@aalis/plugin-webui-server 0.11.11 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth.d.ts +3 -3
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +6 -3
- package/dist/auth.js.map +1 -1
- package/dist/client-discovery.d.ts +2 -1
- package/dist/client-discovery.d.ts.map +1 -1
- package/dist/client-discovery.js +4 -3
- package/dist/client-discovery.js.map +1 -1
- package/dist/index.d.ts +27 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +292 -223
- package/dist/index.js.map +1 -1
- package/dist/routes/files.d.ts +1 -2
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/files.js +1 -1
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/marketplace.d.ts +37 -21
- package/dist/routes/marketplace.d.ts.map +1 -1
- package/dist/routes/marketplace.js +124 -89
- package/dist/routes/marketplace.js.map +1 -1
- package/dist/routes/plugins.d.ts +41 -3
- package/dist/routes/plugins.d.ts.map +1 -1
- package/dist/routes/plugins.js +366 -120
- package/dist/routes/plugins.js.map +1 -1
- package/dist/routes/proxy.d.ts +2 -2
- package/dist/routes/proxy.d.ts.map +1 -1
- package/dist/routes/proxy.js +2 -2
- package/dist/routes/proxy.js.map +1 -1
- package/dist/routes/uploaded-files.d.ts +9 -2
- package/dist/routes/uploaded-files.d.ts.map +1 -1
- package/dist/routes/uploaded-files.js +26 -33
- package/dist/routes/uploaded-files.js.map +1 -1
- package/package.json +45 -25
package/dist/routes/plugins.js
CHANGED
|
@@ -1,8 +1,55 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { isConfigSaveRefused } from '@aalis/api-host-config';
|
|
2
3
|
import { parseInstanceId } from '@aalis/core';
|
|
3
|
-
import { CORE_CONFIG_SCHEMA, defaultsFrom, validateConfig } from '@aalis/schema-config';
|
|
4
|
+
import { CORE_CONFIG_SCHEMA, cloneConfigObject, defaultsFrom, removeExtraFields, validateConfig, } from '@aalis/schema-config';
|
|
5
|
+
function errorMessage(err) {
|
|
6
|
+
return err instanceof Error ? err.message : String(err);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 落盘被拒后运行态怎样与文件对齐:插件配置与新建的实例随配置文件热重载(文件里没有配置段的后缀实例会被卸载);
|
|
10
|
+
* 启停、实例删除与服务偏好只在重启时按文件登记
|
|
11
|
+
*/
|
|
12
|
+
const RECONCILE = {
|
|
13
|
+
reload: '修好配置文件后会按文件内容重载',
|
|
14
|
+
restart: '修好配置文件后,重启时以文件内容为准',
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* 管理动作改完运行态之后落盘;失败时回 `applied: true` 并返回 false:改动已在运行态生效、只是没写进文件,
|
|
18
|
+
* 调用方别当成「没改成」去重试。宿主拒写(配置文件有尚未生效的外部修改)回 409,之后以文件为准对账;
|
|
19
|
+
* 其它失败(权限、磁盘写满等)回 500,文件没变也不会触发重载,改动留在文档里,下一次成功保存时一并写入。
|
|
20
|
+
*/
|
|
21
|
+
export async function saveAfterApply(doc, res, reconcile) {
|
|
22
|
+
try {
|
|
23
|
+
await doc.save();
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
if (isConfigSaveRefused(err)) {
|
|
28
|
+
res.status(409).json({
|
|
29
|
+
error: `已在运行态生效,但未写入配置文件(${errorMessage(err)});${RECONCILE[reconcile]}`,
|
|
30
|
+
applied: true,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
res.status(500).json({
|
|
35
|
+
error: `已在运行态生效,但写入配置文件失败(${errorMessage(err)});改动保留在文档里,下次保存成功时一并写入`,
|
|
36
|
+
applied: true,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
4
42
|
/** 注册插件管理 + 全局配置相关 REST 路由 */
|
|
5
|
-
export function registerPluginRoutes(expressApp,
|
|
43
|
+
export function registerPluginRoutes(expressApp, caps, identify, gate, getAction) {
|
|
44
|
+
const getApp = () => caps.app.current;
|
|
45
|
+
const getPluginMgr = () => caps.plugins.current;
|
|
46
|
+
/** 配置文档由宿主提供;没有时读写文档的路由一律 503,与扫描、市场路由的缺席口径一致 */
|
|
47
|
+
const docOr503 = (res) => {
|
|
48
|
+
const doc = caps.hostConfig.current;
|
|
49
|
+
if (!doc)
|
|
50
|
+
res.status(503).json({ error: '宿主未提供配置文档(host-config),无法读写配置' });
|
|
51
|
+
return doc;
|
|
52
|
+
};
|
|
6
53
|
// 获取插件列表及状态
|
|
7
54
|
expressApp.get('/api/plugins', gate(), (_req, res) => {
|
|
8
55
|
const app = getApp();
|
|
@@ -11,8 +58,9 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
11
58
|
res.json({ plugins: [] });
|
|
12
59
|
return;
|
|
13
60
|
}
|
|
14
|
-
// 反向索引:
|
|
15
|
-
//
|
|
61
|
+
// 反向索引:instanceId -> tools / commands(名字列表 + 聚合敏感能力)。
|
|
62
|
+
// 生产登记的 pluginName 就是 contextId(= instanceId);按 definition.name 索引
|
|
63
|
+
// 会让 name:suffix 误挂主实例的工具。能力披露用聚合 capability。
|
|
16
64
|
const toolsByPlugin = new Map();
|
|
17
65
|
const capsByPlugin = new Map();
|
|
18
66
|
const addCaps = (plugin, visibility) => {
|
|
@@ -22,7 +70,7 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
22
70
|
set.add('visibility:restricted');
|
|
23
71
|
capsByPlugin.set(plugin, set);
|
|
24
72
|
};
|
|
25
|
-
const tools =
|
|
73
|
+
const tools = caps.tools.current?.getAll() ?? [];
|
|
26
74
|
for (const t of tools) {
|
|
27
75
|
const list = toolsByPlugin.get(t.pluginName) ?? [];
|
|
28
76
|
list.push(t.name);
|
|
@@ -30,7 +78,7 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
30
78
|
addCaps(t.pluginName, t.visibility);
|
|
31
79
|
}
|
|
32
80
|
const commandsByPlugin = new Map();
|
|
33
|
-
const cmds =
|
|
81
|
+
const cmds = caps.commands.current?.getAll() ?? [];
|
|
34
82
|
for (const c of cmds) {
|
|
35
83
|
const owner = c.pluginName ?? 'unknown';
|
|
36
84
|
const list = commandsByPlugin.get(owner) ?? [];
|
|
@@ -38,31 +86,38 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
38
86
|
commandsByPlugin.set(owner, list);
|
|
39
87
|
addCaps(owner, c.visibility);
|
|
40
88
|
}
|
|
41
|
-
const plugins = pm.getStatus().map(p =>
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
89
|
+
const plugins = pm.getStatus().map(p => {
|
|
90
|
+
const entry = pm.getPlugin(p.instanceId);
|
|
91
|
+
const schema = entry?.definition?.configSchema;
|
|
92
|
+
// 列表给概览用:schema.secret 字段换成固定掩码。编辑器走 GET /api/plugins/:name/config,那条不脱敏。
|
|
93
|
+
const config = maskSecretFields(cloneConfigObject((entry?.config ?? {})), schema);
|
|
94
|
+
return {
|
|
95
|
+
name: p.name,
|
|
96
|
+
instanceId: p.instanceId,
|
|
97
|
+
displayName: p.displayName,
|
|
98
|
+
state: p.state,
|
|
99
|
+
slow: p.slow,
|
|
100
|
+
provides: p.provides ?? [],
|
|
101
|
+
// 完整能力声明含内置能力与 apply 别名;外部依赖闸与工具/指令可见性仍是各自独立的事实。
|
|
102
|
+
uses: p.uses,
|
|
103
|
+
requiredServices: p.requiredServices ?? [],
|
|
104
|
+
optionalServices: p.optionalServices ?? [],
|
|
105
|
+
capabilities: [...(capsByPlugin.get(p.instanceId) ?? [])],
|
|
106
|
+
tools: toolsByPlugin.get(p.instanceId) ?? [],
|
|
107
|
+
commands: commandsByPlugin.get(p.instanceId) ?? [],
|
|
108
|
+
reusable: p.reusable ?? false,
|
|
109
|
+
// extends / config / configSchema / defaultConfig 非内核状态摘要字段
|
|
110
|
+
// (getStatus 只含内核事实):从 entry.config / entry.definition 补齐给前端。
|
|
111
|
+
extends: entry?.definition?.extends,
|
|
112
|
+
config,
|
|
113
|
+
configSchema: entry?.definition?.configSchema,
|
|
114
|
+
defaultConfig: defaultsFrom(entry?.definition?.configSchema),
|
|
115
|
+
error: p.error,
|
|
116
|
+
};
|
|
117
|
+
});
|
|
63
118
|
res.json({ plugins });
|
|
64
119
|
});
|
|
65
|
-
// 获取可用的 WebUI
|
|
120
|
+
// 获取可用的 WebUI 页面(由活跃插件经 webui-server 的绑定接口 registerPage 登记)
|
|
66
121
|
expressApp.get('/api/pages', gate(), (_req, res) => {
|
|
67
122
|
const app = getApp();
|
|
68
123
|
const pm = getPluginMgr();
|
|
@@ -70,14 +125,14 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
70
125
|
res.json([]);
|
|
71
126
|
return;
|
|
72
127
|
}
|
|
73
|
-
const webuiSvc =
|
|
128
|
+
const webuiSvc = caps.webui();
|
|
74
129
|
if (!webuiSvc) {
|
|
75
130
|
res.json([]);
|
|
76
131
|
return;
|
|
77
132
|
}
|
|
78
133
|
const displayNameByPlugin = new Map();
|
|
79
134
|
for (const p of pm.getStatus())
|
|
80
|
-
displayNameByPlugin.set(p.
|
|
135
|
+
displayNameByPlugin.set(p.instanceId, p.displayName);
|
|
81
136
|
const pages = [];
|
|
82
137
|
for (const page of webuiSvc.getPages()) {
|
|
83
138
|
pages.push({ ...page, plugin: page.pluginName, pluginDisplayName: displayNameByPlugin.get(page.pluginName) });
|
|
@@ -85,7 +140,7 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
85
140
|
pages.sort((a, b) => (a.order ?? 99) - (b.order ?? 99));
|
|
86
141
|
res.json(pages);
|
|
87
142
|
});
|
|
88
|
-
//
|
|
143
|
+
// 通用声明式页面操作:调用插件经 webuiServer.registerAction 登记的处理函数
|
|
89
144
|
expressApp.post('/api/page-action/:plugin/:method', async (req, res) => {
|
|
90
145
|
const { plugin: pluginName, method } = req.params;
|
|
91
146
|
const args = req.body ?? {};
|
|
@@ -100,15 +155,11 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
100
155
|
res.status(404).json({ error: `插件 ${pluginName} 不存在或未激活` });
|
|
101
156
|
return;
|
|
102
157
|
}
|
|
103
|
-
const handler =
|
|
104
|
-
if (
|
|
158
|
+
const handler = getAction(pluginName, method);
|
|
159
|
+
if (!handler) {
|
|
105
160
|
res.status(404).json({ error: `处理器 ${method} 不存在` });
|
|
106
161
|
return;
|
|
107
162
|
}
|
|
108
|
-
if (!entry.context) {
|
|
109
|
-
res.status(500).json({ error: `插件 ${pluginName} 上下文不可用` });
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
163
|
// ===== owner 闸 + 调用者身份(单 owner 终态)=====
|
|
113
164
|
// 单 token ⟺ webui:console ⟺ owner;auth.middleware 已校 token,这里复核身份并把
|
|
114
165
|
// caller 传给 action。各 action 自身对敏感操作再做 owner 自检(如 setUserTier)。
|
|
@@ -119,8 +170,7 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
119
170
|
return;
|
|
120
171
|
}
|
|
121
172
|
try {
|
|
122
|
-
|
|
123
|
-
res.json({ ok: true, data: result });
|
|
173
|
+
res.json({ ok: true, data: await handler(args, caller) });
|
|
124
174
|
}
|
|
125
175
|
catch (err) {
|
|
126
176
|
const msg = err instanceof Error ? err.message : String(err);
|
|
@@ -129,8 +179,10 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
129
179
|
});
|
|
130
180
|
// 获取当前全局配置
|
|
131
181
|
expressApp.get('/api/config', gate(), (_req, res) => {
|
|
132
|
-
const
|
|
133
|
-
|
|
182
|
+
const doc = docOr503(res);
|
|
183
|
+
if (!doc)
|
|
184
|
+
return;
|
|
185
|
+
res.json({ ...doc.getAll(), _schema: CORE_CONFIG_SCHEMA });
|
|
134
186
|
});
|
|
135
187
|
// 更新全局配置字段
|
|
136
188
|
expressApp.put('/api/config', gate(), async (req, res) => {
|
|
@@ -140,17 +192,22 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
140
192
|
return;
|
|
141
193
|
}
|
|
142
194
|
const app = getApp();
|
|
143
|
-
|
|
144
|
-
if (!app || !pm) {
|
|
195
|
+
if (!app) {
|
|
145
196
|
res.status(500).json({ error: 'App 不可用' });
|
|
146
197
|
return;
|
|
147
198
|
}
|
|
199
|
+
const doc = docOr503(res);
|
|
200
|
+
if (!doc)
|
|
201
|
+
return;
|
|
148
202
|
// 可改的只有 CORE_CONFIG_SCHEMA 的键。内置前端(buildDraftFromSchema)会把 GET 到的整份配置连同
|
|
149
203
|
// _schema 原样回传,其中 plugins 等还可能是过期快照(插件配置页保存后不刷新全局 config),
|
|
150
204
|
// 所以不能按键报错:其余键一律不应用,但把真有改动的点名回给调用方——不静默吞掉却回复「已保存」。
|
|
151
205
|
const allowed = Object.keys(CORE_CONFIG_SCHEMA);
|
|
152
|
-
const current =
|
|
153
|
-
|
|
206
|
+
const current = doc.getAll();
|
|
207
|
+
// 文档里没写的核心键按 schema 默认值比较:内置前端把默认值回填进草稿后整份回传,不能据此判成改动(否则只改
|
|
208
|
+
// 名字也会把默认值写进文件并触发重启)
|
|
209
|
+
const currentOf = (k) => current[k] ?? CORE_CONFIG_SCHEMA[k]?.default;
|
|
210
|
+
const differs = (k) => !isDeepStrictEqual(updates[k], currentOf(k));
|
|
154
211
|
const ignored = Object.keys(updates).filter(k => k !== '_schema' && !allowed.includes(k) && differs(k));
|
|
155
212
|
const changed = allowed.filter(k => k in updates && differs(k));
|
|
156
213
|
// 与插件配置路径同一把尺子:类型不符的值(如 name 传数字)不落盘。select 的取值范围 validateConfig
|
|
@@ -167,31 +224,57 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
167
224
|
res.status(400).json({ error: invalid.join('; ') });
|
|
168
225
|
return;
|
|
169
226
|
}
|
|
227
|
+
const previous = Object.fromEntries(changed.map(k => [k, current[k]]));
|
|
170
228
|
for (const key of changed)
|
|
171
|
-
|
|
172
|
-
// name /
|
|
173
|
-
const restartNeeded = changed.
|
|
229
|
+
doc.set(key, updates[key]);
|
|
230
|
+
// logLevel 与 slowThresholdMs 只在启动时读取,改了才重启;name 由 /api/status 每次实时读文档,保存即生效
|
|
231
|
+
const restartNeeded = changed.some(k => k === 'logLevel' || k === 'slowThresholdMs');
|
|
174
232
|
const note = ignored.length > 0 ? `(已忽略不可修改的字段: ${ignored.join(', ')})` : '';
|
|
175
233
|
try {
|
|
176
|
-
await
|
|
177
|
-
if (restartNeeded) {
|
|
178
|
-
res.json({ ok: true, message: `全局配置已更新,正在重启应用以生效…${note}`, restart: true, ignored });
|
|
179
|
-
app.restart();
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
res.json({ ok: true, message: `全局配置已更新并保存${note}`, ignored });
|
|
183
|
-
}
|
|
234
|
+
await doc.save();
|
|
184
235
|
}
|
|
185
236
|
catch (err) {
|
|
186
|
-
|
|
187
|
-
|
|
237
|
+
// 拒写与写入失败都撤回文档里的改动,免得下一次任意保存把这次没存下的修改写进文件。
|
|
238
|
+
// logLevel / slowThresholdMs 要重启才生效,name 由状态接口读文档:撤回后都回到修改前,运行态不留改动
|
|
239
|
+
for (const key of changed)
|
|
240
|
+
doc.set(key, previous[key]);
|
|
241
|
+
res
|
|
242
|
+
.status(isConfigSaveRefused(err) ? 409 : 500)
|
|
243
|
+
.json({ error: `未写入配置文件(${errorMessage(err)}),本次修改已撤回` });
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (!restartNeeded) {
|
|
247
|
+
res.json({ ok: true, message: `全局配置已更新并保存${note}`, ignored });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
// 先发起重启再回复:宿主没注入重启策略时 restart() 同步抛错,据此回 restart:false,前端才不会进入等待重连
|
|
251
|
+
// 却永远等不到。发起之后的停机是异步的(core 先广播 app:restarting,何时停机由重启策略决定,Node 宿主延迟
|
|
252
|
+
// 500ms),这条回复来得及发出。已写进文件,重启失败不撤回:改动在下次启动时生效
|
|
253
|
+
try {
|
|
254
|
+
app.restart();
|
|
188
255
|
}
|
|
256
|
+
catch (err) {
|
|
257
|
+
const reason = `全局配置已保存,但重启失败(${errorMessage(err)}),改动在下次启动时生效`;
|
|
258
|
+
caps.logger?.error(reason);
|
|
259
|
+
res.json({ ok: true, message: `${reason}${note}`, restart: false, ignored });
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
res.json({ ok: true, message: `全局配置已更新,正在重启应用以生效…${note}`, restart: true, ignored });
|
|
189
263
|
});
|
|
190
|
-
//
|
|
264
|
+
// 获取单个插件的原始配置(未脱敏,给编辑器回写用)。
|
|
265
|
+
// 列表 GET /api/plugins 已把 schema.secret 换成固定掩码;本接口必须是原文,否则保存会把掩码写回。
|
|
191
266
|
expressApp.get('/api/plugins/:name/config', gate(), (req, res) => {
|
|
192
267
|
const pluginName = req.params.name;
|
|
193
|
-
const
|
|
194
|
-
|
|
268
|
+
const doc = docOr503(res);
|
|
269
|
+
if (!doc)
|
|
270
|
+
return;
|
|
271
|
+
try {
|
|
272
|
+
const pluginConfig = doc.getPluginConfig(pluginName);
|
|
273
|
+
res.json({ name: pluginName, config: pluginConfig });
|
|
274
|
+
}
|
|
275
|
+
catch (err) {
|
|
276
|
+
res.status(400).json({ error: errorMessage(err) });
|
|
277
|
+
}
|
|
195
278
|
});
|
|
196
279
|
// 更新插件配置
|
|
197
280
|
expressApp.put('/api/plugins/:name/config', gate(), async (req, res) => {
|
|
@@ -201,24 +284,61 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
201
284
|
res.status(400).json({ error: 'config 字段必须是对象' });
|
|
202
285
|
return;
|
|
203
286
|
}
|
|
204
|
-
const app = getApp();
|
|
205
287
|
const pm = getPluginMgr();
|
|
206
|
-
if (!
|
|
207
|
-
res.status(500).json({ error: '
|
|
288
|
+
if (!pm) {
|
|
289
|
+
res.status(500).json({ error: '插件管理服务不可用' });
|
|
208
290
|
return;
|
|
209
291
|
}
|
|
292
|
+
const doc = docOr503(res);
|
|
293
|
+
if (!doc)
|
|
294
|
+
return;
|
|
210
295
|
// 补默认值再交给 updateConfig:后者是**整体替换**语义(core 的 orchestration/plugin.ts 里
|
|
211
296
|
// entry.config = newConfig 直接顶掉)。不补的话,PUT 一个部分对象就会把未列出的
|
|
212
297
|
// 字段从内存态和 yaml 里一起抹掉。默认值从 configSchema 派生(唯一声明来源)。
|
|
213
|
-
const
|
|
298
|
+
const entry = pm.getPlugin(pluginName);
|
|
299
|
+
const schema = entry?.definition?.configSchema;
|
|
214
300
|
const defaults = defaultsFrom(schema);
|
|
215
301
|
// 基线取「默认值叠已存值」而非裸默认值:defaultsFrom 只收录声明了 default 的键,
|
|
216
302
|
// 而 apiKey / accessToken 这类 secret 多数**没有** default(deepseek、embedding-openai、
|
|
217
303
|
// llm-openai、serper、onebot 皆是)。用裸 defaults 打底时,请求里没带 apiKey 就等于
|
|
218
304
|
// merged 里根本没有这个键,整体替换后用户的密钥从内存态与 yaml 一起消失。
|
|
219
305
|
// 叠上已存值后语义才是真正的部分更新:没提交的字段保持原样,要清空得显式传空串。
|
|
220
|
-
|
|
221
|
-
|
|
306
|
+
let docConfig;
|
|
307
|
+
let stored;
|
|
308
|
+
let merged;
|
|
309
|
+
const ignored = [];
|
|
310
|
+
const removed = [];
|
|
311
|
+
try {
|
|
312
|
+
docConfig = doc.getPluginConfig(pluginName);
|
|
313
|
+
// 先铺文档里的原配置、缺的默认键追加到末尾:写回时保留配置文件里原有的键序
|
|
314
|
+
stored = { ...docConfig };
|
|
315
|
+
for (const [key, value] of Object.entries(defaults)) {
|
|
316
|
+
if (!Object.hasOwn(stored, key))
|
|
317
|
+
stored[key] = value;
|
|
318
|
+
}
|
|
319
|
+
const submitted = newConfig;
|
|
320
|
+
merged = { ...stored, ...submitted };
|
|
321
|
+
// 与宿主的配置同步同一政策(host-config 的 trimUnknownFields):宿主裁剪且有 schema 时裁掉未知键并 warn,
|
|
322
|
+
// 避免 WebUI 把手滑字段写进 stored,而 YAML watch 路径却会裁掉;宿主保留未知字段时这里也不裁。
|
|
323
|
+
// 被裁的字段按来源回给调用方,不静默吞掉却回复「已更新」:本次提交里的记 ignored(与 PUT /api/config
|
|
324
|
+
// 同一口径);文档里原有、本次没提交的不是用户这次写的,随整份写回从配置文件删掉,记 removed
|
|
325
|
+
if (doc.trimUnknownFields !== false && schema && Object.keys(schema).length > 0) {
|
|
326
|
+
const shape = schema;
|
|
327
|
+
for (const [key, value] of Object.entries(merged)) {
|
|
328
|
+
removeExtraFields({ [key]: value }, shape, Object.hasOwn(submitted, key) ? ignored : removed);
|
|
329
|
+
}
|
|
330
|
+
merged = removeExtraFields(merged, shape);
|
|
331
|
+
if (ignored.length + removed.length > 0) {
|
|
332
|
+
caps.logger?.warn(`配置同步:${pluginName} 裁掉 schema 外字段 [${[...ignored, ...removed].join(', ')}]`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
catch (err) {
|
|
337
|
+
res.status(400).json({ error: errorMessage(err) });
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const note = (ignored.length > 0 ? `(已忽略未声明的配置字段: ${ignored.join(', ')})` : '') +
|
|
341
|
+
(removed.length > 0 ? `(已从配置文件移除未声明的字段: ${removed.join(', ')})` : '');
|
|
222
342
|
// 保存前校验,拦新不追旧:只拒绝本次编辑**新引入**的 invalid(配错了)。
|
|
223
343
|
// 存量问题放行——否则带着历史脏值(或 schema 表达不了的多态字段,如 mcp-client
|
|
224
344
|
// 的 args 数组形态)的插件会在 WebUI 永久存不了任何字段;启动侧 config-sync
|
|
@@ -234,34 +354,79 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
234
354
|
});
|
|
235
355
|
return;
|
|
236
356
|
}
|
|
237
|
-
|
|
357
|
+
// 运行态与文档都已是这份配置:不重建插件(updateConfig 即 bounce,会连带重启依赖它所提供服务的下游)。
|
|
358
|
+
// 仍照常落盘:上一次保存可能写文件失败(500),文档与运行态已是新配置而文件没变,原样重试要能补写;
|
|
359
|
+
// 自写回不会触发 watch 重载。只认 active:error 态插件靠原样保存来重试激活,照旧走 updateConfig
|
|
360
|
+
if (entry?.state === 'active' && isDeepStrictEqual(merged, entry.config) && isDeepStrictEqual(merged, docConfig)) {
|
|
361
|
+
if (!(await saveAfterApply(doc, res, 'reload')))
|
|
362
|
+
return;
|
|
363
|
+
res.json({ ok: true, message: `插件 ${pluginName} 配置无改动,已写回配置文件${note}`, ignored, removed });
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
let success;
|
|
367
|
+
try {
|
|
368
|
+
success = await pm.updateConfig(pluginName, merged);
|
|
369
|
+
}
|
|
370
|
+
catch (err) {
|
|
371
|
+
res.status(400).json({ error: errorMessage(err) });
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
238
374
|
if (success) {
|
|
239
|
-
|
|
240
|
-
|
|
375
|
+
// 按新配置重新激活失败、转为 error:照样写入配置(原样再存即重试激活),但按实际状态回报
|
|
376
|
+
const after = pm.getPlugin(pluginName);
|
|
377
|
+
const failure = after?.state === 'error' ? (after.error ?? '详见日志') : undefined;
|
|
378
|
+
// 管理动作只改运行态;跨重启保留要本路由写文档并落盘
|
|
379
|
+
doc.setPluginConfig(pluginName, merged);
|
|
380
|
+
if (!(await saveAfterApply(doc, res, 'reload')))
|
|
381
|
+
return;
|
|
382
|
+
if (failure !== undefined) {
|
|
383
|
+
res.status(500).json({
|
|
384
|
+
error: `插件 ${pluginName} 按新配置重新激活失败,已转为 error 态(${failure});配置已写入配置文件${note}`,
|
|
385
|
+
});
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
res.json({ ok: true, message: `插件 ${pluginName} 配置已更新${note}`, ignored, removed });
|
|
241
389
|
}
|
|
242
|
-
else {
|
|
390
|
+
else if (pm.getPlugin(pluginName)?.state === 'disabled') {
|
|
243
391
|
// 插件被禁用时这里也会走到,但「不存在」会把用户引向错误方向——区分「禁用」与「真不存在」并给出下一步。
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
res.status(404).json({ error: `插件 ${pluginName} 不存在` });
|
|
250
|
-
}
|
|
392
|
+
res.status(409).json({ error: `插件 ${pluginName} 已禁用,配置未写入——先启用插件再修改配置` });
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
res.status(404).json({ error: `插件 ${pluginName} 不存在` });
|
|
251
396
|
}
|
|
252
397
|
});
|
|
253
398
|
// 启用插件
|
|
254
399
|
expressApp.post('/api/plugins/:name/enable', gate(), async (req, res) => {
|
|
255
400
|
const pluginName = req.params.name;
|
|
256
|
-
const app = getApp();
|
|
257
401
|
const pm = getPluginMgr();
|
|
258
|
-
if (!
|
|
259
|
-
res.status(500).json({ error: '
|
|
402
|
+
if (!pm) {
|
|
403
|
+
res.status(500).json({ error: '插件管理服务不可用' });
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
const doc = docOr503(res);
|
|
407
|
+
if (!doc)
|
|
408
|
+
return;
|
|
409
|
+
let success;
|
|
410
|
+
try {
|
|
411
|
+
success = await pm.enable(pluginName);
|
|
412
|
+
}
|
|
413
|
+
catch (err) {
|
|
414
|
+
res.status(400).json({ error: errorMessage(err) });
|
|
260
415
|
return;
|
|
261
416
|
}
|
|
262
|
-
const success = await pm.enable(pluginName);
|
|
263
417
|
if (success) {
|
|
264
|
-
|
|
418
|
+
// 激活失败、转为 error:照样记为启用(重启时再激活),但按实际状态回报
|
|
419
|
+
const after = pm.getPlugin(pluginName);
|
|
420
|
+
const failure = after?.state === 'error' ? (after.error ?? '详见日志') : undefined;
|
|
421
|
+
doc.setPluginEnabled(pluginName, true);
|
|
422
|
+
if (!(await saveAfterApply(doc, res, 'restart')))
|
|
423
|
+
return;
|
|
424
|
+
if (failure !== undefined) {
|
|
425
|
+
res
|
|
426
|
+
.status(500)
|
|
427
|
+
.json({ error: `插件 ${pluginName} 激活失败,已转为 error 态(${failure});配置文件已记为启用` });
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
265
430
|
res.json({ ok: true, message: `插件 ${pluginName} 已启用` });
|
|
266
431
|
}
|
|
267
432
|
else {
|
|
@@ -271,31 +436,49 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
271
436
|
// 禁用插件
|
|
272
437
|
expressApp.post('/api/plugins/:name/disable', gate(), async (req, res) => {
|
|
273
438
|
const pluginName = req.params.name;
|
|
274
|
-
const app = getApp();
|
|
275
439
|
const pm = getPluginMgr();
|
|
276
|
-
if (!
|
|
277
|
-
res.status(500).json({ error: '
|
|
440
|
+
if (!pm) {
|
|
441
|
+
res.status(500).json({ error: '插件管理服务不可用' });
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const doc = docOr503(res);
|
|
445
|
+
if (!doc)
|
|
446
|
+
return;
|
|
447
|
+
let success;
|
|
448
|
+
try {
|
|
449
|
+
success = await pm.disable(pluginName);
|
|
450
|
+
}
|
|
451
|
+
catch (err) {
|
|
452
|
+
res.status(400).json({ error: errorMessage(err) });
|
|
278
453
|
return;
|
|
279
454
|
}
|
|
280
|
-
const success = await pm.disable(pluginName);
|
|
281
455
|
if (success) {
|
|
282
|
-
|
|
456
|
+
// 仍在初始化、不响应 abort 的插件,宽限到期转 error 而非 disabled:照样记为禁用(重启时不再激活),但按实际状态回报
|
|
457
|
+
const stuck = pm.getPlugin(pluginName)?.state === 'error';
|
|
458
|
+
doc.setPluginEnabled(pluginName, false);
|
|
459
|
+
if (!(await saveAfterApply(doc, res, 'restart')))
|
|
460
|
+
return;
|
|
461
|
+
if (stuck) {
|
|
462
|
+
res
|
|
463
|
+
.status(500)
|
|
464
|
+
.json({ error: `插件 ${pluginName} 未在宽限内停止,已转为 error 态,详见日志;配置文件已记为禁用` });
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
283
467
|
res.json({ ok: true, message: `插件 ${pluginName} 已禁用` });
|
|
284
468
|
}
|
|
285
469
|
else {
|
|
286
|
-
res.status(
|
|
470
|
+
res.status(404).json({ error: `插件 ${pluginName} 不在注册表或已处于终态,无法禁用` });
|
|
287
471
|
}
|
|
288
472
|
});
|
|
289
|
-
//
|
|
473
|
+
// 经宿主 plugin-source 重新扫描并加载新插件
|
|
290
474
|
expressApp.post('/api/plugins/scan', gate(), async (_req, res) => {
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
res.status(500).json({ error: 'App 不可用' });
|
|
475
|
+
const source = caps.source.current;
|
|
476
|
+
if (!source) {
|
|
477
|
+
res.status(503).json({ error: '宿主未提供插件来源,无法扫描' });
|
|
295
478
|
return;
|
|
296
479
|
}
|
|
297
480
|
try {
|
|
298
|
-
const loaded = await
|
|
481
|
+
const loaded = await source.rescan();
|
|
299
482
|
res.json({ ok: true, loaded, message: loaded.length > 0 ? `新加载 ${loaded.length} 个插件` : '无新插件' });
|
|
300
483
|
}
|
|
301
484
|
catch (err) {
|
|
@@ -316,17 +499,19 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
316
499
|
res.status(400).json({ error: 'suffix 只能包含字母、数字、下划线和连字符' });
|
|
317
500
|
return;
|
|
318
501
|
}
|
|
319
|
-
const app = getApp();
|
|
320
502
|
const pm = getPluginMgr();
|
|
321
|
-
if (!
|
|
322
|
-
res.status(500).json({ error: '
|
|
503
|
+
if (!pm) {
|
|
504
|
+
res.status(500).json({ error: '插件管理服务不可用' });
|
|
323
505
|
return;
|
|
324
506
|
}
|
|
507
|
+
const doc = docOr503(res);
|
|
508
|
+
if (!doc)
|
|
509
|
+
return;
|
|
325
510
|
// 实例创建编排(配置文件编排属管理面,内核只出 register 机制):
|
|
326
511
|
// 查同名 module → reusable/查重校验 → 合并默认配置写入 → register 激活。
|
|
327
512
|
const sourceModule = pm
|
|
328
513
|
.getStatus()
|
|
329
|
-
.map(p => pm.getPlugin(p.instanceId)?.
|
|
514
|
+
.map(p => pm.getPlugin(p.instanceId)?.definition)
|
|
330
515
|
.find(m => m?.name === moduleName);
|
|
331
516
|
if (!sourceModule) {
|
|
332
517
|
res.status(400).json({ error: `无法创建实例:模块 "${moduleName}" 未找到` });
|
|
@@ -342,47 +527,108 @@ export function registerPluginRoutes(expressApp, ctx, getApp, getPluginMgr, iden
|
|
|
342
527
|
return;
|
|
343
528
|
}
|
|
344
529
|
const mergedConfig = { ...defaultsFrom(sourceModule.configSchema), ...config };
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
530
|
+
let registered;
|
|
531
|
+
try {
|
|
532
|
+
doc.setPluginConfig(instanceId, mergedConfig);
|
|
533
|
+
// 文档里残留的禁用标记(手改配置文件留下的)照旧生效:以禁用态登记
|
|
534
|
+
registered = await pm.register(sourceModule, mergedConfig, instanceId, {
|
|
535
|
+
disabled: doc.isPluginDisabled(instanceId),
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
catch (err) {
|
|
539
|
+
res.status(400).json({ error: errorMessage(err) });
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (!registered) {
|
|
543
|
+
// 前面已查过重名与 reusable,走到这里的实际只有停机进行中:配置不落盘,免得下次启动按文件登记
|
|
544
|
+
doc.removePluginConfig(instanceId);
|
|
545
|
+
res.status(409).json({ error: `实例 ${instanceId} 未登记(停机中或被拒,见日志),配置未写入` });
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
if (!(await saveAfterApply(doc, res, 'reload')))
|
|
549
|
+
return;
|
|
348
550
|
res.json({ ok: true, instanceId, message: `已创建实例 ${instanceId}` });
|
|
349
551
|
});
|
|
350
552
|
// 删除插件多实例
|
|
351
553
|
expressApp.delete('/api/plugins/:instanceId/instance', gate(), async (req, res) => {
|
|
352
554
|
const instanceId = req.params.instanceId;
|
|
353
|
-
const app = getApp();
|
|
354
555
|
const pm = getPluginMgr();
|
|
355
|
-
if (!
|
|
356
|
-
res.status(500).json({ error: '
|
|
556
|
+
if (!pm) {
|
|
557
|
+
res.status(500).json({ error: '插件管理服务不可用' });
|
|
357
558
|
return;
|
|
358
559
|
}
|
|
359
|
-
|
|
560
|
+
const doc = docOr503(res);
|
|
561
|
+
if (!doc)
|
|
562
|
+
return;
|
|
563
|
+
// 实例删除编排:主实例保护 → unload(内部含级联重算)→ 移除配置条目与禁用标记(同名重建时不再以禁用态登记)。
|
|
360
564
|
const { suffix } = parseInstanceId(instanceId);
|
|
361
565
|
if (!suffix || !pm.getPlugin(instanceId)) {
|
|
362
566
|
res.status(400).json({ error: `无法删除(实例不存在或不允许删除主实例)` });
|
|
363
567
|
return;
|
|
364
568
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
569
|
+
try {
|
|
570
|
+
await pm.unload(instanceId);
|
|
571
|
+
doc.removePluginConfig(instanceId);
|
|
572
|
+
doc.setPluginEnabled(instanceId, true);
|
|
573
|
+
}
|
|
574
|
+
catch (err) {
|
|
575
|
+
res.status(400).json({ error: errorMessage(err) });
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
if (!(await saveAfterApply(doc, res, 'restart')))
|
|
579
|
+
return;
|
|
368
580
|
res.json({ ok: true, message: `已删除实例 ${instanceId}` });
|
|
369
581
|
});
|
|
370
|
-
//
|
|
582
|
+
// 保存配置文档:与其它落盘路由同一口径,宿主拒写回 409、其它失败回 500。宿主不持久化时 save 不写盘,
|
|
583
|
+
// 回执因此只说「已保存」,不说保存到磁盘
|
|
371
584
|
expressApp.post('/api/config/save', gate(), async (_req, res) => {
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
if (!app || !pm) {
|
|
375
|
-
res.status(500).json({ error: 'App 不可用' });
|
|
585
|
+
const doc = docOr503(res);
|
|
586
|
+
if (!doc)
|
|
376
587
|
return;
|
|
377
|
-
}
|
|
378
588
|
try {
|
|
379
|
-
await
|
|
380
|
-
res.json({ ok: true, message: '
|
|
589
|
+
await doc.save();
|
|
590
|
+
res.json({ ok: true, message: '配置已保存' });
|
|
381
591
|
}
|
|
382
592
|
catch (err) {
|
|
383
|
-
|
|
384
|
-
res.status(500).json({ error: msg });
|
|
593
|
+
res.status(isConfigSaveRefused(err) ? 409 : 500).json({ error: errorMessage(err) });
|
|
385
594
|
}
|
|
386
595
|
});
|
|
387
596
|
}
|
|
597
|
+
/** 列表 payload 用的固定掩码。与 PUT 响应「不回显密钥」同一政策:概览面看不到明文。 */
|
|
598
|
+
const SECRET_MASK = '••••••';
|
|
599
|
+
/**
|
|
600
|
+
* 把 schema.secret === true 的字段换成固定掩码。入参须已是拷贝——就地改,避免写穿现场 config。
|
|
601
|
+
* 分组递归;数组元素若是对象则按 items 再走一遍。缺席的键不补掩码。
|
|
602
|
+
*/
|
|
603
|
+
function maskSecretFields(config, schema) {
|
|
604
|
+
if (!schema)
|
|
605
|
+
return config;
|
|
606
|
+
for (const [key, raw] of Object.entries(schema)) {
|
|
607
|
+
if (!raw || typeof raw !== 'object')
|
|
608
|
+
continue;
|
|
609
|
+
const def = raw;
|
|
610
|
+
if (def.secret === true) {
|
|
611
|
+
if (Object.hasOwn(config, key))
|
|
612
|
+
config[key] = SECRET_MASK;
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
const nested = config[key];
|
|
616
|
+
if (def.fields &&
|
|
617
|
+
typeof def.fields === 'object' &&
|
|
618
|
+
nested &&
|
|
619
|
+
typeof nested === 'object' &&
|
|
620
|
+
!Array.isArray(nested)) {
|
|
621
|
+
maskSecretFields(nested, def.fields);
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
if (def.type === 'array' && Array.isArray(nested) && def.items && typeof def.items === 'object') {
|
|
625
|
+
for (const item of nested) {
|
|
626
|
+
if (item && typeof item === 'object' && !Array.isArray(item)) {
|
|
627
|
+
maskSecretFields(item, def.items);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return config;
|
|
633
|
+
}
|
|
388
634
|
//# sourceMappingURL=plugins.js.map
|