@aalis/plugin-webui-server 0.14.1 → 0.15.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/README.md +5 -1
- package/dist/admin-channel.d.ts +10 -0
- package/dist/admin-channel.d.ts.map +1 -0
- package/dist/admin-channel.js +57 -0
- package/dist/admin-channel.js.map +1 -0
- package/dist/attention.d.ts +36 -0
- package/dist/attention.d.ts.map +1 -0
- package/dist/attention.js +73 -0
- package/dist/attention.js.map +1 -0
- package/dist/auth.d.ts +22 -3
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +192 -68
- package/dist/auth.js.map +1 -1
- package/dist/client-discovery.d.ts +7 -3
- package/dist/client-discovery.d.ts.map +1 -1
- package/dist/client-discovery.js +9 -3
- package/dist/client-discovery.js.map +1 -1
- package/dist/client-switch-page.d.ts +2 -0
- package/dist/client-switch-page.d.ts.map +1 -1
- package/dist/client-switch-page.js +37 -36
- package/dist/client-switch-page.js.map +1 -1
- package/dist/gate.d.ts +3 -3
- package/dist/gate.js +3 -3
- package/dist/index.d.ts +21 -26
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +561 -1015
- package/dist/index.js.map +1 -1
- package/dist/layout.d.ts +18 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +198 -0
- package/dist/layout.js.map +1 -0
- package/dist/logs.d.ts +10 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +50 -0
- package/dist/logs.js.map +1 -0
- package/dist/page-check.d.ts +56 -0
- package/dist/page-check.d.ts.map +1 -0
- package/dist/page-check.js +369 -0
- package/dist/page-check.js.map +1 -0
- package/dist/page-files.d.ts +13 -0
- package/dist/page-files.d.ts.map +1 -0
- package/dist/page-files.js +39 -0
- package/dist/page-files.js.map +1 -0
- package/dist/pages.d.ts +60 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +470 -0
- package/dist/pages.js.map +1 -0
- package/dist/route.d.ts +11 -0
- package/dist/route.d.ts.map +1 -0
- package/dist/route.js +13 -0
- package/dist/route.js.map +1 -0
- package/dist/routes/files.d.ts +8 -9
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/files.js +32 -119
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/frames.d.ts +4 -0
- package/dist/routes/frames.d.ts.map +1 -0
- package/dist/routes/frames.js +103 -0
- package/dist/routes/frames.js.map +1 -0
- package/dist/routes/marketplace.d.ts +46 -129
- package/dist/routes/marketplace.d.ts.map +1 -1
- package/dist/routes/marketplace.js +138 -86
- package/dist/routes/marketplace.js.map +1 -1
- package/dist/routes/media.d.ts +7 -0
- package/dist/routes/media.d.ts.map +1 -0
- package/dist/routes/media.js +63 -0
- package/dist/routes/media.js.map +1 -0
- package/dist/routes/pages.d.ts +20 -0
- package/dist/routes/pages.d.ts.map +1 -0
- package/dist/routes/pages.js +86 -0
- package/dist/routes/pages.js.map +1 -0
- package/dist/routes/pipe-to-response.d.ts +9 -0
- package/dist/routes/pipe-to-response.d.ts.map +1 -0
- package/dist/routes/pipe-to-response.js +16 -0
- package/dist/routes/pipe-to-response.js.map +1 -0
- package/dist/routes/plugins.d.ts +22 -24
- package/dist/routes/plugins.d.ts.map +1 -1
- package/dist/routes/plugins.js +285 -275
- package/dist/routes/plugins.js.map +1 -1
- package/dist/routes/proxy.d.ts.map +1 -1
- package/dist/routes/proxy.js +2 -1
- package/dist/routes/proxy.js.map +1 -1
- package/dist/session-transport.d.ts +6 -0
- package/dist/session-transport.d.ts.map +1 -0
- package/dist/session-transport.js +32 -0
- package/dist/session-transport.js.map +1 -0
- package/package.json +34 -31
- package/dist/protocol.d.ts +0 -113
- package/dist/protocol.d.ts.map +0 -1
- package/dist/protocol.js +0 -59
- package/dist/protocol.js.map +0 -1
- package/dist/routes/uploaded-files.d.ts +0 -20
- package/dist/routes/uploaded-files.d.ts.map +0 -1
- package/dist/routes/uploaded-files.js +0 -161
- package/dist/routes/uploaded-files.js.map +0 -1
package/dist/routes/plugins.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { describeUnmet, unmetDependencies } from '@aalis/api-doctor';
|
|
2
3
|
import { isConfigSaveRefused } from '@aalis/api-host-config';
|
|
3
4
|
import { parseInstanceId } from '@aalis/core';
|
|
4
|
-
import { CORE_CONFIG_SCHEMA, deepMergeDefaults, defaultsFrom, parseConfig, removeExtraFields, validateConfig, } from '@aalis/schema-config';
|
|
5
|
+
import { CORE_CONFIG_SCHEMA, deepMergeDefaults, defaultsFrom, isSecretRef, mapSecretFields, parseConfig, removeExtraFields, validateConfig, } from '@aalis/schema-config';
|
|
6
|
+
import { blockedStatus } from '../attention.js';
|
|
7
|
+
import { route } from '../route.js';
|
|
5
8
|
function errorMessage(err) {
|
|
6
9
|
return err instanceof Error ? err.message : String(err);
|
|
7
10
|
}
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
12
|
+
* 落盘被拒后运行态怎样与文件对齐:插件配置、新建的实例与服务偏好随配置文件热重载(文件里没有配置段的后缀实例会被卸载);
|
|
13
|
+
* 启停只在重启时按文件登记。被拒的可能是配置文件,也可能是密钥文件,拒写原因里点了名,这里不指具体哪个文件
|
|
11
14
|
*/
|
|
12
15
|
const RECONCILE = {
|
|
13
|
-
reload: '
|
|
14
|
-
restart: '
|
|
16
|
+
reload: '修好该文件后会按文件内容重载',
|
|
17
|
+
restart: '修好该文件后,重启时以文件内容为准',
|
|
15
18
|
};
|
|
16
19
|
/**
|
|
17
20
|
* 管理动作改完运行态之后落盘;失败时回 `applied: true` 并返回 false:改动已在运行态生效、只是没写进文件,
|
|
@@ -26,22 +29,40 @@ export async function saveAfterApply(doc, res, reconcile, outcome) {
|
|
|
26
29
|
return true;
|
|
27
30
|
}
|
|
28
31
|
catch (err) {
|
|
29
|
-
|
|
30
|
-
if (isConfigSaveRefused(err)) {
|
|
31
|
-
res.status(409).json({
|
|
32
|
-
error: `${lead}已在运行态生效,但未写入配置文件(${errorMessage(err)});${RECONCILE[reconcile]}`,
|
|
33
|
-
applied: true,
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
res.status(500).json({
|
|
38
|
-
error: `${lead}已在运行态生效,但写入配置文件失败(${errorMessage(err)});改动保留在文档里,下次保存成功时一并写入`,
|
|
39
|
-
applied: true,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
32
|
+
reportSaveFailure(res, err, reconcile, outcome);
|
|
42
33
|
return false;
|
|
43
34
|
}
|
|
44
35
|
}
|
|
36
|
+
/** 落盘失败的回执,口径见 {@link saveAfterApply};applyPluginConfig 带回的 saveError 同样用它 */
|
|
37
|
+
function reportSaveFailure(res, err, reconcile, outcome) {
|
|
38
|
+
const lead = outcome ? `${outcome};` : '';
|
|
39
|
+
if (isConfigSaveRefused(err)) {
|
|
40
|
+
res.status(409).json({
|
|
41
|
+
error: `${lead}已在运行态生效,但未写入配置文件(${errorMessage(err)});${RECONCILE[reconcile]}`,
|
|
42
|
+
applied: true,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
res.status(500).json({
|
|
47
|
+
error: `${lead}已在运行态生效,但写入配置文件失败(${errorMessage(err)});改动保留在文档里,下次保存成功时一并写入`,
|
|
48
|
+
applied: true,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** 宿主配置里要重启才生效的键:runtime 与 core 只在启动时读它们。name 只是显示用的应用名称,保存即生效 */
|
|
53
|
+
const RESTART_KEYS = ['logLevel', 'slowThresholdMs'];
|
|
54
|
+
/** 文档里每个密钥键被引用的处数:表单据此提示「替换会同时改到另外 N 处」 */
|
|
55
|
+
function countSecretRefs(value, into = {}) {
|
|
56
|
+
if (isSecretRef(value))
|
|
57
|
+
into[value.$secret] = (into[value.$secret] ?? 0) + 1;
|
|
58
|
+
else if (Array.isArray(value))
|
|
59
|
+
for (const item of value)
|
|
60
|
+
countSecretRefs(item, into);
|
|
61
|
+
else if (value !== null && typeof value === 'object')
|
|
62
|
+
for (const v of Object.values(value))
|
|
63
|
+
countSecretRefs(v, into);
|
|
64
|
+
return into;
|
|
65
|
+
}
|
|
45
66
|
/**
|
|
46
67
|
* 管理动作受理后等重算落定,再读实例状态写回执:动作撞上在飞的重算时只排队、立即返回,立即读到的还是 pending,
|
|
47
68
|
* 之后的激活失败就漏报了。idle 不等转入后台的慢激活:重算对单个激活至多等到慢激活阈值。
|
|
@@ -50,13 +71,21 @@ async function settledEntry(pm, instanceId) {
|
|
|
50
71
|
await pm.idle();
|
|
51
72
|
return pm.getPlugin(instanceId);
|
|
52
73
|
}
|
|
53
|
-
/**
|
|
54
|
-
|
|
74
|
+
/**
|
|
75
|
+
* 落定后既没激活也没失败的说明:慢激活转入后台仍在进行,或 required 依赖没满足(缺什么、在等谁,按 api-doctor 的共享判据
|
|
76
|
+
* 沿链追到根,与插件列表同一说法),不缺服务时在等自己的旧实例停下(同插件列表,说法同依赖链的 lingering 根);依赖都在、
|
|
77
|
+
* 只是提供者正在关停换代时没有可说的;其余状态由各路由自己说明
|
|
78
|
+
*/
|
|
79
|
+
function notYetActive(pm, services, instanceId, state) {
|
|
55
80
|
if (state === 'activating')
|
|
56
81
|
return '仍在激活(超过慢激活阈值,已转入后台),结果以插件列表为准';
|
|
57
|
-
if (state
|
|
58
|
-
return
|
|
59
|
-
|
|
82
|
+
if (state !== 'pending')
|
|
83
|
+
return undefined;
|
|
84
|
+
const status = pm.getStatus();
|
|
85
|
+
const unmet = unmetDependencies(status, services.names()).get(instanceId);
|
|
86
|
+
const lingering = !unmet?.lacking.length && status.find(entry => entry.instanceId === instanceId)?.lingering;
|
|
87
|
+
const why = lingering ? `等待 ${instanceId} 的旧实例停下` : unmet && describeUnmet(unmet);
|
|
88
|
+
return why ? `尚未激活:${why}` : '尚未激活,结果以插件列表为准';
|
|
60
89
|
}
|
|
61
90
|
/**
|
|
62
91
|
* 管理动作返回 false 时的回执。条目还在注册表里,说明是停机中或正在卸载拒绝了这次操作,回 409;
|
|
@@ -71,16 +100,21 @@ function refuse(pm, res, instanceId, action) {
|
|
|
71
100
|
}
|
|
72
101
|
}
|
|
73
102
|
/** 注册插件管理 + 全局配置相关 REST 路由 */
|
|
74
|
-
export function registerPluginRoutes(expressApp, caps,
|
|
75
|
-
const getApp = () => caps.app.current;
|
|
76
|
-
const getPluginMgr = () => caps.plugins.current;
|
|
103
|
+
export function registerPluginRoutes(expressApp, caps, gate) {
|
|
77
104
|
/** 配置文档由宿主提供;没有时读写文档的路由一律 503,与扫描、市场路由的缺席口径一致 */
|
|
78
105
|
const docOr503 = (res) => {
|
|
79
|
-
const doc = caps.hostConfig.
|
|
106
|
+
const doc = caps.hostConfig.present ? caps.hostConfig.use : undefined;
|
|
80
107
|
if (!doc)
|
|
81
108
|
res.status(503).json({ error: '宿主未提供配置文档(host-config),无法读写配置' });
|
|
82
109
|
return doc;
|
|
83
110
|
};
|
|
111
|
+
/** 提供这个界面的插件经界面停掉就回不来了:拒绝停用它 */
|
|
112
|
+
const refuseSelf = (res, name) => {
|
|
113
|
+
if (name !== caps.self)
|
|
114
|
+
return false;
|
|
115
|
+
res.status(409).json({ error: `插件 ${name} 提供这个界面,不能从界面停用` });
|
|
116
|
+
return true;
|
|
117
|
+
};
|
|
84
118
|
/**
|
|
85
119
|
* 把提交的插件配置合进基线(改配置时是配置文档里的原配置,建实例时是空对象),改配置与建实例同一套:
|
|
86
120
|
* 提交里的顶层键整键替换基线的同名键(分组、数组整块替换),没提交的保持原值;值为 null 的顶层键视为删除,
|
|
@@ -117,165 +151,110 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
117
151
|
return { merged, ignored, removed };
|
|
118
152
|
};
|
|
119
153
|
// 获取插件列表及状态
|
|
120
|
-
expressApp
|
|
121
|
-
const app =
|
|
122
|
-
const pm =
|
|
154
|
+
route(expressApp, 'GET /api/plugins', gate(), (_req, res) => {
|
|
155
|
+
const app = caps.app.present ? caps.app.use : undefined;
|
|
156
|
+
const pm = caps.plugins.present ? caps.plugins.use : undefined;
|
|
123
157
|
if (!app || !pm) {
|
|
124
|
-
res.json({ plugins: [] });
|
|
158
|
+
res.json({ plugins: [], secretRefCounts: {} });
|
|
125
159
|
return;
|
|
126
160
|
}
|
|
127
|
-
// 反向索引:instanceId -> tools / commands
|
|
128
|
-
//
|
|
129
|
-
// 会让 name:suffix 误挂主实例的工具。能力披露用聚合 capability。
|
|
161
|
+
// 反向索引:instanceId -> tools / commands。生产登记的 pluginName 就是 contextId(= instanceId);
|
|
162
|
+
// 按 definition.name 索引会让 name:suffix 误挂主实例的工具
|
|
130
163
|
const toolsByPlugin = new Map();
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
if (visibility !== 'restricted')
|
|
134
|
-
return;
|
|
135
|
-
const set = capsByPlugin.get(plugin) ?? new Set();
|
|
136
|
-
set.add('visibility:restricted');
|
|
137
|
-
capsByPlugin.set(plugin, set);
|
|
138
|
-
};
|
|
139
|
-
const tools = caps.tools.current?.getAll() ?? [];
|
|
140
|
-
for (const t of tools) {
|
|
141
|
-
const list = toolsByPlugin.get(t.pluginName) ?? [];
|
|
142
|
-
list.push(t.name);
|
|
143
|
-
toolsByPlugin.set(t.pluginName, list);
|
|
144
|
-
addCaps(t.pluginName, t.visibility);
|
|
164
|
+
for (const t of caps.tools.present ? caps.tools.use.getAll() : []) {
|
|
165
|
+
toolsByPlugin.set(t.pluginName, [...(toolsByPlugin.get(t.pluginName) ?? []), t.name]);
|
|
145
166
|
}
|
|
146
167
|
const commandsByPlugin = new Map();
|
|
147
|
-
const
|
|
148
|
-
for (const c of cmds) {
|
|
168
|
+
for (const c of caps.commands.present ? caps.commands.use.getAll() : []) {
|
|
149
169
|
const owner = c.pluginName ?? 'unknown';
|
|
150
|
-
|
|
151
|
-
list.push(c.name);
|
|
152
|
-
commandsByPlugin.set(owner, list);
|
|
153
|
-
addCaps(owner, c.visibility);
|
|
170
|
+
commandsByPlugin.set(owner, [...(commandsByPlugin.get(owner) ?? []), c.name]);
|
|
154
171
|
}
|
|
155
|
-
const
|
|
172
|
+
const doc = caps.hostConfig.present ? caps.hostConfig.use : undefined;
|
|
173
|
+
const sections = doc?.getAll().plugins ?? {};
|
|
174
|
+
const versionOf = caps.versions();
|
|
175
|
+
const plugins = blockedStatus({ plugins: pm, services: caps.services, doc }).map(p => {
|
|
156
176
|
const entry = pm.getPlugin(p.instanceId);
|
|
177
|
+
const schema = entry?.definition?.configSchema;
|
|
178
|
+
// 线上格式照旧给 requiredServices / optionalServices:从 core 状态里唯一的依赖信息 uses 派生
|
|
179
|
+
const requiredServices = p.uses.filter(use => use.kind === 'required').map(use => use.service);
|
|
180
|
+
const optionalServices = p.uses.filter(use => use.kind === 'optional').map(use => use.service);
|
|
181
|
+
const declared = [...requiredServices, ...optionalServices];
|
|
157
182
|
return {
|
|
158
183
|
name: p.name,
|
|
159
184
|
instanceId: p.instanceId,
|
|
160
185
|
displayName: p.displayName,
|
|
186
|
+
// 展示元数据:子系统按插件声明原样给,归哪组、组名与顺序由界面定;版本取已装的包
|
|
187
|
+
subsystem: entry?.definition?.subsystem,
|
|
188
|
+
version: versionOf(p.name),
|
|
161
189
|
state: p.state,
|
|
162
190
|
slow: p.slow,
|
|
191
|
+
lingering: p.lingering,
|
|
192
|
+
self: p.instanceId === caps.self || undefined,
|
|
163
193
|
provides: p.provides ?? [],
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
194
|
+
requiredServices,
|
|
195
|
+
optionalServices,
|
|
196
|
+
servicePreferences: Object.fromEntries(declared.flatMap(name => {
|
|
197
|
+
const preferred = caps.services.preferred(name, { for: p.instanceId });
|
|
198
|
+
return preferred === undefined ? [] : [[name, preferred]];
|
|
199
|
+
})),
|
|
200
|
+
serviceSelections: Object.fromEntries(declared.flatMap(name => {
|
|
201
|
+
const [selected] = caps.services.inspect(name, { for: p.instanceId });
|
|
202
|
+
return selected ? [[name, { contextId: selected.contextId, basis: selected.basis }]] : [];
|
|
203
|
+
})),
|
|
204
|
+
lacking: p.lacking,
|
|
205
|
+
waiting: p.waiting,
|
|
206
|
+
unconfigured: p.unconfigured,
|
|
169
207
|
tools: toolsByPlugin.get(p.instanceId) ?? [],
|
|
170
208
|
commands: commandsByPlugin.get(p.instanceId) ?? [],
|
|
171
209
|
reusable: p.reusable ?? false,
|
|
172
210
|
// extends / config / configSchema / defaultConfig 非内核状态摘要字段
|
|
173
|
-
// (getStatus
|
|
174
|
-
//
|
|
211
|
+
// (getStatus 只含内核事实):从配置文档与 entry.definition 补齐给前端。
|
|
212
|
+
// 配置给文档形态:密钥字段上是引用,不是原值;前端据它建编辑草稿,引用原样发回即保持原值。
|
|
213
|
+
// 宿主不提供配置文档时给运行态,去掉密钥字段(运行态里是原值)
|
|
175
214
|
extends: entry?.definition?.extends,
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
215
|
+
configSection: Object.hasOwn(sections, p.instanceId),
|
|
216
|
+
config: doc ? doc.getPluginConfig(p.instanceId) : mapSecretFields(schema, entry?.config ?? {}, () => undefined),
|
|
217
|
+
configSchema: schema,
|
|
218
|
+
defaultConfig: defaultsFrom(schema),
|
|
179
219
|
error: p.error,
|
|
180
220
|
};
|
|
181
221
|
});
|
|
182
|
-
res.json({ plugins });
|
|
222
|
+
res.json({ plugins, secretRefCounts: doc ? countSecretRefs(doc.getAll().plugins) : {} });
|
|
183
223
|
});
|
|
184
|
-
//
|
|
185
|
-
expressApp
|
|
186
|
-
const app = getApp();
|
|
187
|
-
const pm = getPluginMgr();
|
|
188
|
-
if (!app || !pm) {
|
|
189
|
-
res.json([]);
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
const webuiSvc = caps.webui();
|
|
193
|
-
if (!webuiSvc) {
|
|
194
|
-
res.json([]);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
const displayNameByPlugin = new Map();
|
|
198
|
-
for (const p of pm.getStatus())
|
|
199
|
-
displayNameByPlugin.set(p.instanceId, p.displayName);
|
|
200
|
-
const pages = [];
|
|
201
|
-
for (const page of webuiSvc.getPages()) {
|
|
202
|
-
pages.push({ ...page, plugin: page.pluginName, pluginDisplayName: displayNameByPlugin.get(page.pluginName) });
|
|
203
|
-
}
|
|
204
|
-
pages.sort((a, b) => (a.order ?? 99) - (b.order ?? 99));
|
|
205
|
-
res.json(pages);
|
|
206
|
-
});
|
|
207
|
-
// 通用声明式页面操作:调用插件经 webuiServer.registerAction 登记的处理函数
|
|
208
|
-
expressApp.post('/api/page-action/:plugin/:method', async (req, res) => {
|
|
209
|
-
const { plugin: pluginName, method } = req.params;
|
|
210
|
-
const args = req.body ?? {};
|
|
211
|
-
const app = getApp();
|
|
212
|
-
const pm = getPluginMgr();
|
|
213
|
-
if (!app || !pm) {
|
|
214
|
-
res.status(500).json({ error: 'App 不可用' });
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
const entry = pm.getPlugin(pluginName);
|
|
218
|
-
if (!entry || entry.state !== 'active') {
|
|
219
|
-
res.status(404).json({ error: `插件 ${pluginName} 不存在或未激活` });
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
const handler = getAction(pluginName, method);
|
|
223
|
-
if (!handler) {
|
|
224
|
-
res.status(404).json({ error: `处理器 ${method} 不存在` });
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
// ===== owner 闸 + 调用者身份(单 owner 终态)=====
|
|
228
|
-
// 单 token ⟺ webui:console ⟺ owner;auth.middleware 已校 token,这里复核身份并把
|
|
229
|
-
// caller 传给 action。各 action 自身对敏感操作再做 owner 自检(如 setUserTier)。
|
|
230
|
-
// 多账户的 action: 能力委托(per-user grant/deny / actionsMeta 可见性)已剥离。
|
|
231
|
-
const caller = identify(req);
|
|
232
|
-
if (!caller) {
|
|
233
|
-
res.status(403).json({ error: `操作 ${pluginName}/${method} 需要 owner 权限` });
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
try {
|
|
237
|
-
res.json({ ok: true, data: await handler(args, caller) });
|
|
238
|
-
}
|
|
239
|
-
catch (err) {
|
|
240
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
241
|
-
res.status(500).json({ error: msg });
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
// 获取当前全局配置
|
|
245
|
-
expressApp.get('/api/config', gate(), (_req, res) => {
|
|
224
|
+
// 宿主配置:文档里写了的核心键、它们的 schema、要重启才生效的键与家目录
|
|
225
|
+
route(expressApp, 'GET /api/config', gate(), (_req, res) => {
|
|
246
226
|
const doc = docOr503(res);
|
|
247
227
|
if (!doc)
|
|
248
228
|
return;
|
|
249
|
-
|
|
229
|
+
const all = doc.getAll();
|
|
230
|
+
const config = Object.fromEntries(Object.keys(CORE_CONFIG_SCHEMA)
|
|
231
|
+
.filter(key => all[key] !== undefined)
|
|
232
|
+
.map(key => [key, all[key]]));
|
|
233
|
+
res.json({ config, schema: CORE_CONFIG_SCHEMA, restartKeys: [...RESTART_KEYS], home: caps.home });
|
|
250
234
|
});
|
|
251
|
-
//
|
|
252
|
-
expressApp
|
|
235
|
+
// 改宿主配置的核心键:只保存,要重启才生效的键改了时回执写明,由界面出「重启以生效」,重启走 POST /api/restart
|
|
236
|
+
route(expressApp, 'PUT /api/config', gate(), async (req, res) => {
|
|
253
237
|
const updates = req.body;
|
|
254
238
|
if (!updates || typeof updates !== 'object') {
|
|
255
239
|
res.status(400).json({ error: '请求体必须是对象' });
|
|
256
240
|
return;
|
|
257
241
|
}
|
|
258
|
-
const app = getApp();
|
|
259
|
-
if (!app) {
|
|
260
|
-
res.status(500).json({ error: 'App 不可用' });
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
242
|
const doc = docOr503(res);
|
|
264
243
|
if (!doc)
|
|
265
244
|
return;
|
|
266
|
-
//
|
|
267
|
-
// _schema 原样回传,其中 plugins 等还可能是过期快照(插件配置页保存后不刷新全局 config),
|
|
268
|
-
// 所以不能按键报错:其余键一律不应用,但把真有改动的点名回给调用方——不静默吞掉却回复「已保存」。
|
|
245
|
+
// 只收 CORE_CONFIG_SCHEMA 的键:别的顶层键(插件配置、禁用名单等)各有自己的路由,写在这里的一律拒收
|
|
269
246
|
const allowed = Object.keys(CORE_CONFIG_SCHEMA);
|
|
247
|
+
const foreign = Object.keys(updates).filter(k => !allowed.includes(k));
|
|
248
|
+
if (foreign.length > 0) {
|
|
249
|
+
res.status(400).json({ error: `只能改核心键(${allowed.join('、')}),不收:${foreign.join('、')}` });
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
270
252
|
const current = doc.getAll();
|
|
271
253
|
const defaultOf = (k) => CORE_CONFIG_SCHEMA[k]?.default;
|
|
272
|
-
// 文档里没写的核心键按 schema
|
|
273
|
-
// 名字也会把默认值写进文件并触发重启)
|
|
254
|
+
// 文档里没写的核心键按 schema 默认值比较:回传的默认值不算改动,不写进文件
|
|
274
255
|
const currentOf = (k) => current[k] ?? defaultOf(k);
|
|
275
|
-
|
|
276
|
-
// 核心键提交 null 表示清空(前端清空数字框时发 null):回到默认值,并从文档里删掉这个键,不把默认值写死进文件
|
|
256
|
+
// 核心键提交 null 表示清空(恢复默认):回到默认值,并从文档里删掉这个键,不把默认值写死进文件
|
|
277
257
|
const submittedOf = (k) => (updates[k] === null ? defaultOf(k) : updates[k]);
|
|
278
|
-
const ignored = Object.keys(updates).filter(k => k !== '_schema' && !allowed.includes(k) && differs(k));
|
|
279
258
|
const changed = allowed.filter(k => k in updates && !isDeepStrictEqual(submittedOf(k), currentOf(k)));
|
|
280
259
|
// 与插件配置路径同一把尺子:类型不符、越界或不在选项里的值(如 logLevel 传 nope)不落盘。
|
|
281
260
|
const picked = Object.fromEntries(changed.map(k => [k, submittedOf(k)]));
|
|
@@ -291,16 +270,12 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
291
270
|
// 提交 null 的键置为 undefined,写文件时省略
|
|
292
271
|
for (const key of changed)
|
|
293
272
|
doc.set(key, updates[key] === null ? undefined : values[key]);
|
|
294
|
-
|
|
295
|
-
// 装有人设时聊天显示人设名,应用名称只在仪表盘上看得到
|
|
296
|
-
const restartNeeded = changed.some(k => k === 'logLevel' || k === 'slowThresholdMs');
|
|
297
|
-
const note = ignored.length > 0 ? `(已忽略不可修改的字段: ${ignored.join(', ')})` : '';
|
|
273
|
+
const restartNeeded = changed.some(k => RESTART_KEYS.includes(k));
|
|
298
274
|
try {
|
|
299
275
|
await doc.save();
|
|
300
276
|
}
|
|
301
277
|
catch (err) {
|
|
302
|
-
//
|
|
303
|
-
// logLevel / slowThresholdMs 要重启才生效,name 由状态接口读文档:撤回后都回到修改前,运行态不留改动
|
|
278
|
+
// 拒写与写入失败都撤回文档里的改动,免得下一次任意保存把这次没存下的修改写进文件
|
|
304
279
|
for (const key of changed)
|
|
305
280
|
doc.set(key, previous[key]);
|
|
306
281
|
res
|
|
@@ -308,47 +283,40 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
308
283
|
.json({ error: `未写入配置文件(${errorMessage(err)}),本次修改已撤回` });
|
|
309
284
|
return;
|
|
310
285
|
}
|
|
311
|
-
|
|
312
|
-
|
|
286
|
+
res.json({
|
|
287
|
+
ok: true,
|
|
288
|
+
message: restartNeeded ? '全局配置已保存,重启后生效' : '全局配置已更新并保存',
|
|
289
|
+
restartNeeded,
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
// 重启应用。发起之后的停机是异步的(core 先广播 app:restarting,何时停机由重启策略决定),这条回复来得及发出;
|
|
293
|
+
// 宿主没注入重启策略时 restart() 同步抛错,回 500,界面据此不去等待重连
|
|
294
|
+
route(expressApp, 'POST /api/restart', gate(), (_req, res) => {
|
|
295
|
+
const app = caps.app.present ? caps.app.use : undefined;
|
|
296
|
+
if (!app) {
|
|
297
|
+
res.status(500).json({ error: 'App 不可用' });
|
|
313
298
|
return;
|
|
314
299
|
}
|
|
315
|
-
// 先发起重启再回复:宿主没注入重启策略时 restart() 同步抛错,据此回 restart:false,前端才不会进入等待重连
|
|
316
|
-
// 却永远等不到。发起之后的停机是异步的(core 先广播 app:restarting,何时停机由重启策略决定,Node 宿主延迟
|
|
317
|
-
// 500ms),这条回复来得及发出。已写进文件,重启失败不撤回:改动在下次启动时生效
|
|
318
300
|
try {
|
|
319
301
|
app.restart();
|
|
320
302
|
}
|
|
321
303
|
catch (err) {
|
|
322
|
-
const reason =
|
|
304
|
+
const reason = `重启失败(${errorMessage(err)}),改动在下次启动时生效`;
|
|
323
305
|
caps.logger?.error(reason);
|
|
324
|
-
res.json({
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
res.json({ ok: true, message: `全局配置已更新,正在重启应用以生效…${note}`, restart: true, ignored });
|
|
328
|
-
});
|
|
329
|
-
// 获取单个插件在配置文档里的配置
|
|
330
|
-
expressApp.get('/api/plugins/:name/config', gate(), (req, res) => {
|
|
331
|
-
const pluginName = req.params.name;
|
|
332
|
-
const doc = docOr503(res);
|
|
333
|
-
if (!doc)
|
|
306
|
+
res.status(500).json({ error: reason });
|
|
334
307
|
return;
|
|
335
|
-
try {
|
|
336
|
-
const pluginConfig = doc.getPluginConfig(pluginName);
|
|
337
|
-
res.json({ name: pluginName, config: pluginConfig });
|
|
338
|
-
}
|
|
339
|
-
catch (err) {
|
|
340
|
-
res.status(400).json({ error: errorMessage(err) });
|
|
341
308
|
}
|
|
309
|
+
res.json({ ok: true, message: '正在重启应用' });
|
|
342
310
|
});
|
|
343
311
|
// 更新插件配置
|
|
344
|
-
expressApp
|
|
312
|
+
route(expressApp, 'PUT /api/plugins/:name/config', gate(), async (req, res) => {
|
|
345
313
|
const pluginName = req.params.name;
|
|
346
314
|
const newConfig = req.body?.config;
|
|
347
315
|
if (!newConfig || typeof newConfig !== 'object' || Array.isArray(newConfig)) {
|
|
348
316
|
res.status(400).json({ error: 'config 字段必须是对象' });
|
|
349
317
|
return;
|
|
350
318
|
}
|
|
351
|
-
const pm =
|
|
319
|
+
const pm = caps.plugins.present ? caps.plugins.use : undefined;
|
|
352
320
|
if (!pm) {
|
|
353
321
|
res.status(500).json({ error: '插件管理服务不可用' });
|
|
354
322
|
return;
|
|
@@ -356,11 +324,12 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
356
324
|
const doc = docOr503(res);
|
|
357
325
|
if (!doc)
|
|
358
326
|
return;
|
|
359
|
-
//
|
|
327
|
+
// 合进原配置再交给宿主的 applyPluginConfig:配置是**整体替换**语义(core 的 orchestration/plugin.ts 里
|
|
360
328
|
// entry.config = newConfig 直接顶掉)。基线取配置文档里的原配置而非裸默认值:defaultsFrom 只收录声明了
|
|
361
|
-
// default 的键,而 apiKey / accessToken 这类 secret 多数**没有** default
|
|
362
|
-
//
|
|
363
|
-
//
|
|
329
|
+
// default 的键,而 apiKey / accessToken 这类 secret 多数**没有** default,以裸默认值打底时请求里没带 apiKey,
|
|
330
|
+
// 整体替换后用户的密钥就从内存态与 yaml 一起消失。叠上原配置后语义才是真正的部分更新:没提交的字段保持原样,
|
|
331
|
+
// 要清空得显式提交。
|
|
332
|
+
// 原配置是文档形态,密钥字段上是引用;前端原样发回引用即保持原值,发明文即替换(由宿主拆进密钥存储)。
|
|
364
333
|
const entry = pm.getPlugin(pluginName);
|
|
365
334
|
const schema = entry?.definition?.configSchema;
|
|
366
335
|
let docConfig;
|
|
@@ -377,11 +346,16 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
377
346
|
res.status(400).json({ error: errorMessage(err) });
|
|
378
347
|
return;
|
|
379
348
|
}
|
|
349
|
+
// 没登记的实例不经这里新建(宿主的 applyPluginConfig 会按同模块定义登记它),建实例走 POST instances
|
|
350
|
+
if (!entry) {
|
|
351
|
+
res.status(404).json({ error: `插件 ${pluginName} 不存在` });
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
380
354
|
const note = (ignored.length > 0 ? `(已忽略未声明的配置字段: ${ignored.join(', ')})` : '') +
|
|
381
355
|
(removed.length > 0 ? `(已从配置文件移除未声明的字段: ${removed.join(', ')})` : '');
|
|
382
356
|
// 保存前校验,拦新不追旧:只拒绝本次编辑**新引入**的 invalid(配错了)。
|
|
383
|
-
// 存量问题放行——否则带着历史脏值(或 schema
|
|
384
|
-
//
|
|
357
|
+
// 存量问题放行——否则带着历史脏值(或 schema 表达不了的多态字段,如字符串与数组两种写法都收的
|
|
358
|
+
// 字段)的插件会在 WebUI 永久存不了任何字段;启动侧 config-sync
|
|
385
359
|
// 对它们已有告警。missing(没配全)也放行:半成品配置是启用插件配到一半的
|
|
386
360
|
// 正常中间态。
|
|
387
361
|
// 存量按「去掉下标的路径 + 原因」计数,新配置里同类问题的条数超出存量才算新增:list / multiselect /
|
|
@@ -407,54 +381,58 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
407
381
|
});
|
|
408
382
|
return;
|
|
409
383
|
}
|
|
410
|
-
//
|
|
411
|
-
//
|
|
412
|
-
//
|
|
413
|
-
|
|
414
|
-
if (!(await saveAfterApply(doc, res, 'reload')))
|
|
415
|
-
return;
|
|
416
|
-
res.json({ ok: true, message: `插件 ${pluginName} 配置无改动,已写回配置文件${note}`, ignored, removed });
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
let success;
|
|
384
|
+
// 运行态、文档、密钥与落盘由宿主一步完成:解析出的配置与运行态相同、实例在 active 时不重建插件
|
|
385
|
+
// (updateConfig 即 bounce,会连带重启依赖它所提供服务的下游),照常写回文档并落盘:上一次保存可能写文件失败(500),
|
|
386
|
+
// 原样重试要能补写;自写回不会触发 watch 重载。error 态插件靠原样保存来重试激活
|
|
387
|
+
let result;
|
|
420
388
|
try {
|
|
421
|
-
|
|
389
|
+
result = await doc.applyPluginConfig(pluginName, merged);
|
|
422
390
|
}
|
|
423
391
|
catch (err) {
|
|
424
392
|
res.status(400).json({ error: errorMessage(err) });
|
|
425
393
|
return;
|
|
426
394
|
}
|
|
427
|
-
if (
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
: undefined;
|
|
436
|
-
const aside = after?.state === 'disabled' ? '插件已禁用,启用时按新配置激活' : notYetActive(after?.state);
|
|
437
|
-
if (!(await saveAfterApply(doc, res, 'reload', failure ?? aside)))
|
|
438
|
-
return;
|
|
439
|
-
if (failure !== undefined) {
|
|
440
|
-
res.status(500).json({ error: `${failure};配置已写入配置文件${note}` });
|
|
395
|
+
if (result.status === 'refused') {
|
|
396
|
+
refuse(pm, res, pluginName, '配置更新');
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
// 运行态与文档都没变:回「无改动」。只有运行态没变(例如文档里补了配置段)照常按已更新回报,插件没有重建
|
|
400
|
+
if (!result.reloaded && isDeepStrictEqual(merged, docConfig)) {
|
|
401
|
+
if (result.saveError !== undefined) {
|
|
402
|
+
reportSaveFailure(res, result.saveError, 'reload');
|
|
441
403
|
return;
|
|
442
404
|
}
|
|
443
|
-
res.json({
|
|
444
|
-
|
|
445
|
-
message: `插件 ${pluginName} 配置已更新${note}${aside ? `;${aside}` : ''}`,
|
|
446
|
-
ignored,
|
|
447
|
-
removed,
|
|
448
|
-
});
|
|
449
|
-
}
|
|
450
|
-
else {
|
|
451
|
-
refuse(pm, res, pluginName, '配置更新');
|
|
405
|
+
res.json({ ok: true, message: `插件 ${pluginName} 配置无改动,已写回配置文件${note}`, ignored, removed });
|
|
406
|
+
return;
|
|
452
407
|
}
|
|
408
|
+
// 按新配置重新激活失败、转为 error:照样写入配置(原样再存即重试激活),回执按实际状态说明。
|
|
409
|
+
// 禁用态只换上新配置、保持禁用,启用时按它激活
|
|
410
|
+
const after = await settledEntry(pm, pluginName);
|
|
411
|
+
const failure = after?.state === 'error'
|
|
412
|
+
? `插件 ${pluginName} 按新配置重新激活失败,已转为 error 态(${after.error ?? '详见日志'})`
|
|
413
|
+
: undefined;
|
|
414
|
+
const aside = after?.state === 'disabled'
|
|
415
|
+
? '插件已禁用,启用时按新配置激活'
|
|
416
|
+
: notYetActive(pm, caps.services, pluginName, after?.state);
|
|
417
|
+
if (result.saveError !== undefined) {
|
|
418
|
+
reportSaveFailure(res, result.saveError, 'reload', failure ?? aside);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
// 写进了配置文件就回 2xx,激活失败也一样:表单据状态码区分「没写成」(留着改)与「写成了」(收起、刷新)。
|
|
422
|
+
// 写成之后草稿已经过时,例如清除的密钥键已随这次保存从 secrets.yaml 删掉,再「撤销」只会引用不存在的键
|
|
423
|
+
res.json({
|
|
424
|
+
ok: true,
|
|
425
|
+
message: failure !== undefined
|
|
426
|
+
? `${failure};配置已写入配置文件${note}`
|
|
427
|
+
: `插件 ${pluginName} 配置已更新${note}${aside ? `;${aside}` : ''}`,
|
|
428
|
+
ignored,
|
|
429
|
+
removed,
|
|
430
|
+
});
|
|
453
431
|
});
|
|
454
432
|
// 启用插件
|
|
455
|
-
expressApp
|
|
433
|
+
route(expressApp, 'POST /api/plugins/:name/enable', gate(), async (req, res) => {
|
|
456
434
|
const pluginName = req.params.name;
|
|
457
|
-
const pm =
|
|
435
|
+
const pm = caps.plugins.present ? caps.plugins.use : undefined;
|
|
458
436
|
if (!pm) {
|
|
459
437
|
res.status(500).json({ error: '插件管理服务不可用' });
|
|
460
438
|
return;
|
|
@@ -472,28 +450,31 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
472
450
|
}
|
|
473
451
|
if (success) {
|
|
474
452
|
doc.setPluginEnabled(pluginName, true);
|
|
475
|
-
// 激活失败、转为 error
|
|
453
|
+
// 激活失败、转为 error:照样记为启用(重启时再激活),回执按实际状态说明。写进了配置文件就回 2xx,口径同改配置
|
|
476
454
|
const after = await settledEntry(pm, pluginName);
|
|
477
455
|
const failure = after?.state === 'error'
|
|
478
456
|
? `插件 ${pluginName} 激活失败,已转为 error 态(${after.error ?? '详见日志'})`
|
|
479
457
|
: undefined;
|
|
480
|
-
const aside = notYetActive(after?.state);
|
|
458
|
+
const aside = notYetActive(pm, caps.services, pluginName, after?.state);
|
|
481
459
|
if (!(await saveAfterApply(doc, res, 'restart', failure ?? aside)))
|
|
482
460
|
return;
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
461
|
+
res.json({
|
|
462
|
+
ok: true,
|
|
463
|
+
message: failure !== undefined
|
|
464
|
+
? `${failure};配置文件已记为启用`
|
|
465
|
+
: `插件 ${pluginName} 已启用${aside ? `;${aside}` : ''}`,
|
|
466
|
+
});
|
|
488
467
|
}
|
|
489
468
|
else {
|
|
490
469
|
refuse(pm, res, pluginName, '启用');
|
|
491
470
|
}
|
|
492
471
|
});
|
|
493
472
|
// 禁用插件
|
|
494
|
-
expressApp
|
|
473
|
+
route(expressApp, 'POST /api/plugins/:name/disable', gate(), async (req, res) => {
|
|
495
474
|
const pluginName = req.params.name;
|
|
496
|
-
|
|
475
|
+
if (refuseSelf(res, pluginName))
|
|
476
|
+
return;
|
|
477
|
+
const pm = caps.plugins.present ? caps.plugins.use : undefined;
|
|
497
478
|
if (!pm) {
|
|
498
479
|
res.status(500).json({ error: '插件管理服务不可用' });
|
|
499
480
|
return;
|
|
@@ -510,30 +491,62 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
510
491
|
return;
|
|
511
492
|
}
|
|
512
493
|
if (success) {
|
|
513
|
-
//
|
|
514
|
-
const stuck = pm.
|
|
515
|
-
? `插件 ${pluginName}
|
|
494
|
+
// 已禁用,但旧实例(如不响应 abort 的初始化)超过宽限仍没停下:回执说明这一点。写进了配置文件就回 2xx,口径同改配置
|
|
495
|
+
const stuck = pm.getStatus().find(entry => entry.instanceId === pluginName)?.lingering
|
|
496
|
+
? `插件 ${pluginName} 已禁用,但超过宽限仍未停下,详见日志`
|
|
516
497
|
: undefined;
|
|
517
498
|
doc.setPluginEnabled(pluginName, false);
|
|
518
499
|
if (!(await saveAfterApply(doc, res, 'restart', stuck)))
|
|
519
500
|
return;
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
}
|
|
524
|
-
res.json({ ok: true, message: `插件 ${pluginName} 已禁用` });
|
|
501
|
+
res.json({
|
|
502
|
+
ok: true,
|
|
503
|
+
message: stuck !== undefined ? `${stuck};配置文件已记为禁用` : `插件 ${pluginName} 已禁用`,
|
|
504
|
+
});
|
|
525
505
|
}
|
|
526
506
|
else {
|
|
527
507
|
refuse(pm, res, pluginName, '禁用');
|
|
528
508
|
}
|
|
529
509
|
});
|
|
510
|
+
// 重载:拆掉当前激活再按原配置重新激活,不写配置文件;失败的插件重载即重试
|
|
511
|
+
route(expressApp, 'POST /api/plugins/:name/reload', gate(), async (req, res) => {
|
|
512
|
+
const pluginName = req.params.name;
|
|
513
|
+
const pm = caps.plugins.present ? caps.plugins.use : undefined;
|
|
514
|
+
if (!pm) {
|
|
515
|
+
res.status(500).json({ error: '插件管理服务不可用' });
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
if (pm.getPlugin(pluginName)?.state === 'disabled') {
|
|
519
|
+
res.status(409).json({ error: `插件 ${pluginName} 已停用,启用后才能重载` });
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
let success;
|
|
523
|
+
try {
|
|
524
|
+
success = await pm.bounce(pluginName);
|
|
525
|
+
}
|
|
526
|
+
catch (err) {
|
|
527
|
+
res.status(400).json({ error: errorMessage(err) });
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
if (!success) {
|
|
531
|
+
refuse(pm, res, pluginName, '重载');
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
const after = await settledEntry(pm, pluginName);
|
|
535
|
+
const aside = notYetActive(pm, caps.services, pluginName, after?.state);
|
|
536
|
+
res.json({
|
|
537
|
+
ok: true,
|
|
538
|
+
message: after?.state === 'error'
|
|
539
|
+
? `插件 ${pluginName} 重新激活失败,已转为 error 态(${after.error ?? '详见日志'})`
|
|
540
|
+
: `插件 ${pluginName} 已重载${aside ? `;${aside}` : ''}`,
|
|
541
|
+
});
|
|
542
|
+
});
|
|
530
543
|
// 经宿主 plugin-source 重新扫描并加载新插件
|
|
531
|
-
expressApp
|
|
532
|
-
|
|
533
|
-
if (!source) {
|
|
544
|
+
route(expressApp, 'POST /api/plugins/scan', gate(), async (_req, res) => {
|
|
545
|
+
if (!caps.source.present) {
|
|
534
546
|
res.status(503).json({ error: '宿主未提供插件来源,无法扫描' });
|
|
535
547
|
return;
|
|
536
548
|
}
|
|
549
|
+
const source = caps.source.use;
|
|
537
550
|
try {
|
|
538
551
|
const loaded = await source.rescan();
|
|
539
552
|
res.json({ ok: true, loaded, message: loaded.length > 0 ? `新加载 ${loaded.length} 个插件` : '无新插件' });
|
|
@@ -544,7 +557,7 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
544
557
|
}
|
|
545
558
|
});
|
|
546
559
|
// 创建插件多实例
|
|
547
|
-
expressApp
|
|
560
|
+
route(expressApp, 'POST /api/plugins/:name/instances', gate(), async (req, res) => {
|
|
548
561
|
const moduleName = req.params.name;
|
|
549
562
|
const suffix = req.body?.suffix;
|
|
550
563
|
const config = req.body?.config ?? {};
|
|
@@ -560,7 +573,7 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
560
573
|
res.status(400).json({ error: 'config 字段必须是对象' });
|
|
561
574
|
return;
|
|
562
575
|
}
|
|
563
|
-
const pm =
|
|
576
|
+
const pm = caps.plugins.present ? caps.plugins.use : undefined;
|
|
564
577
|
if (!pm) {
|
|
565
578
|
res.status(500).json({ error: '插件管理服务不可用' });
|
|
566
579
|
return;
|
|
@@ -570,10 +583,13 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
570
583
|
return;
|
|
571
584
|
// 实例创建编排(配置文件编排属管理面,内核只出 register 机制):
|
|
572
585
|
// 查同名 module → reusable/查重校验 → 合并默认配置、裁剪与校验 → 写入 → register 激活。
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
586
|
+
// 模块定义主实例优先、否则取任一在册实例的,与宿主登记新实例时取定义的规则一致:合并、裁剪与校验所用的 schema
|
|
587
|
+
// 就是登记时的那一份
|
|
588
|
+
const sourceModule = pm.getPlugin(moduleName)?.definition ??
|
|
589
|
+
pm
|
|
590
|
+
.getStatus()
|
|
591
|
+
.map(p => pm.getPlugin(p.instanceId)?.definition)
|
|
592
|
+
.find(m => m?.name === moduleName);
|
|
577
593
|
if (!sourceModule) {
|
|
578
594
|
res.status(400).json({ error: `无法创建实例:模块 "${moduleName}" 未找到` });
|
|
579
595
|
return;
|
|
@@ -600,44 +616,46 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
600
616
|
return;
|
|
601
617
|
}
|
|
602
618
|
const note = ignored.length > 0 ? `(已忽略未声明的配置字段: ${ignored.join(', ')})` : '';
|
|
603
|
-
|
|
619
|
+
// 宿主先写文档再登记(文档里残留的禁用标记照旧生效,以禁用态登记),没登记上时撤回文档与密钥
|
|
620
|
+
let result;
|
|
604
621
|
try {
|
|
605
|
-
doc.
|
|
606
|
-
// 文档里残留的禁用标记(手改配置文件留下的)照旧生效:以禁用态登记
|
|
607
|
-
registered = await pm.register(sourceModule, mergedConfig, instanceId, {
|
|
608
|
-
disabled: doc.isPluginDisabled(instanceId),
|
|
609
|
-
});
|
|
622
|
+
result = await doc.applyPluginConfig(instanceId, mergedConfig);
|
|
610
623
|
}
|
|
611
624
|
catch (err) {
|
|
612
625
|
res.status(400).json({ error: errorMessage(err) });
|
|
613
626
|
return;
|
|
614
627
|
}
|
|
615
|
-
if (
|
|
628
|
+
if (result.status === 'refused') {
|
|
616
629
|
// 前面已查过重名与 reusable,走到这里的实际只有停机进行中:配置不落盘,免得下次启动按文件登记
|
|
617
|
-
doc.removePluginConfig(instanceId);
|
|
618
630
|
res.status(409).json({ error: `实例 ${instanceId} 未登记(停机中或被拒,见日志),配置未写入` });
|
|
619
631
|
return;
|
|
620
632
|
}
|
|
621
|
-
// 激活失败、转为 error
|
|
633
|
+
// 激活失败、转为 error:实例与配置照样保留并落盘(改配置保存即重试激活),回执按实际状态说明。
|
|
634
|
+
// 写进了配置文件就回 2xx,口径同改配置:实例已登记,表单该收起,留着再点「创建」只会得到「已存在」
|
|
622
635
|
const after = await settledEntry(pm, instanceId);
|
|
623
636
|
const failure = after?.state === 'error'
|
|
624
637
|
? `已创建实例 ${instanceId},但激活失败,已转为 error 态(${after.error ?? '详见日志'})`
|
|
625
638
|
: undefined;
|
|
626
639
|
const aside = after?.state === 'disabled'
|
|
627
640
|
? '配置文件的 disabledPlugins 里有它,已按禁用态登记,启用后激活'
|
|
628
|
-
: notYetActive(after?.state);
|
|
629
|
-
if (
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
641
|
+
: notYetActive(pm, caps.services, instanceId, after?.state);
|
|
642
|
+
if (result.saveError !== undefined) {
|
|
643
|
+
reportSaveFailure(res, result.saveError, 'reload', failure ?? aside);
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
res.json({
|
|
647
|
+
ok: true,
|
|
648
|
+
instanceId,
|
|
649
|
+
message: failure !== undefined
|
|
650
|
+
? `${failure};配置已写入配置文件${note}`
|
|
651
|
+
: `已创建实例 ${instanceId}${note}${aside ? `;${aside}` : ''}`,
|
|
652
|
+
ignored,
|
|
653
|
+
});
|
|
636
654
|
});
|
|
637
655
|
// 删除插件多实例
|
|
638
|
-
expressApp
|
|
656
|
+
route(expressApp, 'DELETE /api/plugins/:instanceId/instance', gate(), async (req, res) => {
|
|
639
657
|
const instanceId = req.params.instanceId;
|
|
640
|
-
const pm =
|
|
658
|
+
const pm = caps.plugins.present ? caps.plugins.use : undefined;
|
|
641
659
|
if (!pm) {
|
|
642
660
|
res.status(500).json({ error: '插件管理服务不可用' });
|
|
643
661
|
return;
|
|
@@ -670,8 +688,9 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
670
688
|
.json({ error: `未删除实例 ${instanceId}:未写入配置文件(${errorMessage(err)})` });
|
|
671
689
|
return;
|
|
672
690
|
}
|
|
691
|
+
let unloaded;
|
|
673
692
|
try {
|
|
674
|
-
await pm.unload(instanceId);
|
|
693
|
+
unloaded = await pm.unload(instanceId);
|
|
675
694
|
}
|
|
676
695
|
catch (err) {
|
|
677
696
|
res.status(500).json({
|
|
@@ -679,21 +698,12 @@ export function registerPluginRoutes(expressApp, caps, identify, gate, getAction
|
|
|
679
698
|
});
|
|
680
699
|
return;
|
|
681
700
|
}
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
// 回执因此只说「已保存」,不说保存到磁盘
|
|
686
|
-
expressApp.post('/api/config/save', gate(), async (_req, res) => {
|
|
687
|
-
const doc = docOr503(res);
|
|
688
|
-
if (!doc)
|
|
701
|
+
// 返回 false 而条目还在:停机中拒绝了卸载,配置段已删、实例仍在跑到停机为止。条目已不在的是别处先卸掉了,照常回已删除
|
|
702
|
+
if (!unloaded && pm.getPlugin(instanceId)) {
|
|
703
|
+
res.json({ ok: true, message: `已从配置文件删除实例 ${instanceId};停机中,未卸载运行中的实例` });
|
|
689
704
|
return;
|
|
690
|
-
try {
|
|
691
|
-
await doc.save();
|
|
692
|
-
res.json({ ok: true, message: '配置已保存' });
|
|
693
|
-
}
|
|
694
|
-
catch (err) {
|
|
695
|
-
res.status(isConfigSaveRefused(err) ? 409 : 500).json({ error: errorMessage(err) });
|
|
696
705
|
}
|
|
706
|
+
res.json({ ok: true, message: `已删除实例 ${instanceId}` });
|
|
697
707
|
});
|
|
698
708
|
}
|
|
699
709
|
//# sourceMappingURL=plugins.js.map
|