fde-console 0.0.0-stage → 2.9.68
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/LICENSE +21 -0
- package/README.md +128 -2
- package/README.zh-CN.md +130 -0
- package/assets/icon.svg +9 -0
- package/cordis.patch.yml +7 -0
- package/lib/agentActivity.js +1180 -0
- package/lib/agentContext.js +146 -0
- package/lib/agentTranscript.js +1398 -0
- package/lib/analyze.js +625 -0
- package/lib/attribute.js +205 -0
- package/lib/backup.js +366 -0
- package/lib/desktopAgents.js +123 -0
- package/lib/diary.js +333 -0
- package/lib/dingtalk.js +412 -0
- package/lib/feishu.js +580 -0
- package/lib/giteaClient.js +810 -0
- package/lib/giteeClient.js +372 -0
- package/lib/githubClient.js +361 -0
- package/lib/heatmap.js +99 -0
- package/lib/imNotify.js +378 -0
- package/lib/inbox.js +438 -0
- package/lib/instanceLock.js +122 -0
- package/lib/jobs.js +56 -0
- package/lib/learn.js +1388 -0
- package/lib/learnImport.js +283 -0
- package/lib/learnSeeds.js +97 -0
- package/lib/learnSeedsAiEng.js +77 -0
- package/lib/mcpCore.js +135 -0
- package/lib/members.js +160 -0
- package/lib/migration.js +403 -0
- package/lib/overview.js +376 -0
- package/lib/postmortem.js +148 -0
- package/lib/radarIgnore.js +134 -0
- package/lib/redact.js +76 -0
- package/lib/router.js +246 -0
- package/lib/routes-gitea.js +262 -0
- package/lib/routes-setup.js +40 -0
- package/lib/scorecard.js +111 -0
- package/lib/sessionRef.js +227 -0
- package/lib/sessionSnapshot.js +67 -0
- package/lib/sessionSuggest.js +186 -0
- package/lib/settings.js +783 -0
- package/lib/setupCheck.js +330 -0
- package/lib/ssrfGuard.js +145 -0
- package/lib/store.js +564 -0
- package/lib/storeDb.js +418 -0
- package/lib/taskEvents.js +243 -0
- package/lib/webhookOut.js +125 -0
- package/lib/weekly.js +439 -0
- package/lib/workSearch.js +220 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +46 -4
- package/plugin/adapter.js +157 -0
- package/plugin/client.js +282 -0
- package/plugin/index.mjs +116 -0
- package/plugin/static.js +99 -0
- package/scripts/backup-data.js +41 -0
- package/scripts/mcp-server.mjs +68 -0
- package/scripts/migrate-data.js +301 -0
- package/scripts/migrate-storage.js +47 -0
- package/scripts/seed-demo.js +200 -0
- package/scripts/write-agent-context.js +43 -0
- package/test/helpers/appAssembler.js +1078 -0
- package/web/client/fde-client.js +292 -0
- package/web/css/app.css +1510 -0
- package/web/index.html +69 -0
- package/web/js/api.js +72 -0
- package/web/js/app.js +313 -0
- package/web/js/icons.js +48 -0
- package/web/js/mock.js +1150 -0
- package/web/js/state.js +7 -0
- package/web/js/ui.js +276 -0
- package/web/js/views/activity.js +410 -0
- package/web/js/views/agents.js +892 -0
- package/web/js/views/board.js +1656 -0
- package/web/js/views/learn.js +530 -0
- package/web/js/views/members.js +344 -0
- package/web/js/views/onboarding.js +103 -0
- package/web/js/views/overview.js +508 -0
- package/web/js/views/radar.js +1359 -0
- package/web/js/views/search.js +149 -0
- package/web/js/views/settings.js +1351 -0
- package/web/js/views/topology.js +471 -0
- package/web/mock.json +2363 -0
package/lib/settings.js
ADDED
|
@@ -0,0 +1,783 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* settings.js — B5 设置读写
|
|
5
|
+
* - GET /api/settings:脱敏视图(凭据永不出现,仅显示来源枚举);dataDir 回显磁盘原值(v1.5 只读)
|
|
6
|
+
* - PATCH /api/settings:按节 deep merge 后整体写回 config.json;host 白名单仅接受
|
|
7
|
+
* 127.0.0.1/0.0.0.0(SPEC-PLUGIN 2.1:与宿主 webServer schema 同源,业主拍板允许显式
|
|
8
|
+
* 配置 LAN 绑定,默认仍 127.0.0.1);dataDir/webDir 只读(PATCH 忽略,防止相对路径被
|
|
9
|
+
* 持久化为绝对路径,v1.5)
|
|
10
|
+
* - WP6 焊死(SPEC-PLUGIN 2.6):port 退出 config.json 与设置面——监听参数归宿主
|
|
11
|
+
* webServer(--port 由 web-startup 解析),本模块不再展示/校验/写回 port
|
|
12
|
+
* - security 节:redactKeys/allowedProtocols/credentialSource 仍仅手改(#10 防改弱脱敏边界);
|
|
13
|
+
* 唯一例外 trustedHosts(插件态 Host 防线 allowlist 扩展,与脱敏无关)可经 PATCH 修改,
|
|
14
|
+
* 条目须为裸 host 字面量(域名或 IPv4);回环字面量恒允许(防线内置),配置与否无影响。
|
|
15
|
+
* 注意:Host 防线为启动期快照(plugin/adapter.js createHostGuard),trustedHosts 变更需重启生效
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const store = require('./store');
|
|
19
|
+
const agentActivity = require('./agentActivity');
|
|
20
|
+
const fs = require('node:fs');
|
|
21
|
+
const path = require('node:path');
|
|
22
|
+
const { HttpError, sendJSON } = require('./router');
|
|
23
|
+
|
|
24
|
+
const CREDENTIAL_VIEW = {
|
|
25
|
+
'git-config-extraheader': 'GIT_CONFIG_EXTRAHEADER',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// v1.5(goose 验收附带 bug):dataDir/webDir 对 API 只读——不进 PATCH 白名单,
|
|
29
|
+
// 一次设置页 PATCH 就曾把 config.json 的相对路径持久化成绝对路径(换机/挪目录部署即漂移)。
|
|
30
|
+
// 运行期目录解析仍由装配侧启动时归一化(#6,gitea provider 依赖),但绝不写回磁盘。
|
|
31
|
+
// WP6:port 出白名单(监听归宿主 webServer,config.json 不再有 port 键)
|
|
32
|
+
// v2.2:agentActivity 入白名单(智能体工作观测:总开关/限额/各源开关与根路径,本机路径无敏感值)
|
|
33
|
+
// v2.2:gateway 出白名单(网关集成整体移除,业主 2026-10-20;PATCH 传入即静默忽略)
|
|
34
|
+
const TOP_LEVEL_ALLOWED = new Set(['host', 'gitea', 'security', 'dingtalk', 'feishu', 'analyze', 'agentActivity', 'github', 'gitee', 'im', 'learn']);
|
|
35
|
+
|
|
36
|
+
/** 读磁盘 config.json 的目录原值(不经 store.readJSON——GET 不得触发损坏隔离等副作用) */
|
|
37
|
+
function diskDirRaw(configPath, key, fallback) {
|
|
38
|
+
try {
|
|
39
|
+
const raw = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
40
|
+
return typeof raw[key] === 'string' ? raw[key] : fallback;
|
|
41
|
+
} catch (e) {
|
|
42
|
+
return fallback;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 白名单条目非法性检查:拒绝 localhost/IP 字面量/无点号单段主机名(§5-②) */
|
|
47
|
+
function forbiddenWhitelistEntry(entry) {
|
|
48
|
+
if (typeof entry !== 'string' || entry.trim() === '') return '空白项';
|
|
49
|
+
const h = entry.trim().toLowerCase();
|
|
50
|
+
if (h === 'localhost' || h === 'localhost.') return 'localhost';
|
|
51
|
+
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(h)) return 'IPv4 字面量';
|
|
52
|
+
if (/^[0-9a-f:]+:[0-9a-f:]*$/i.test(h) && h.includes(':')) return 'IPv6 字面量';
|
|
53
|
+
if (!h.includes('.')) return '缺少点号的单段主机名';
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* security.trustedHosts 条目校验:仅接受裸 host 字面量(域名或 IPv4)。
|
|
59
|
+
* 禁止协议前缀/路径分隔符/空白字符/userinfo/端口与 IPv6 写法——Host 防线(plugin/adapter.js
|
|
60
|
+
* hostOf)按 authority host 部分比较,条目必须是纯 host 才有确定语义。
|
|
61
|
+
* localhost/127.0.0.1 恒允许(防线内置回环兜底),配置与否无影响——回环条目冗余但无害,
|
|
62
|
+
* 接受之(防「GET 视图原样存回」被 400;迁移模板 pluginDefaultConfig 即含回环条目)。
|
|
63
|
+
* @returns {string|null} 非法原因;合法返回 null
|
|
64
|
+
*/
|
|
65
|
+
function trustedHostEntryError(entry) {
|
|
66
|
+
if (typeof entry !== 'string' || entry.trim() === '') return '空白项';
|
|
67
|
+
if (/\s/.test(entry)) return '含空白字符';
|
|
68
|
+
if (entry.includes('://')) return '含协议前缀';
|
|
69
|
+
if (entry.includes('/') || entry.includes('\\')) return '含路径分隔符';
|
|
70
|
+
if (entry.includes('@')) return '含 userinfo';
|
|
71
|
+
if (entry.includes(':')) return '含端口或 IPv6 写法(仅支持域名或 IPv4 字面量)';
|
|
72
|
+
if (entry.includes('?') || entry.includes('#') || entry.includes('%')) return '含非 host 字符';
|
|
73
|
+
const h = entry.toLowerCase();
|
|
74
|
+
if (h.length > 253) return '超过 253 字符上限';
|
|
75
|
+
if (!/^[a-z0-9._-]+$/.test(h)) return '含非法字符(仅允许字母/数字/点/连字符)';
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* v2.2 智能体工作观测视图:节归一化后如实带出(enabled/限额/各源开关与根路径——
|
|
81
|
+
* 本机路径无敏感值,同 dwsBin/dshBin 口径;路径空串 = 运行时回退本机默认位置)。
|
|
82
|
+
* v2.2 移除:gateway 视图节(网关集成整体移除,GET 不再返回该节)。
|
|
83
|
+
*/
|
|
84
|
+
function agentActivityView(config) {
|
|
85
|
+
return agentActivity.normalizeAgentActivity(config && config.agentActivity);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** baseUrl 校验:协议 ∈ allowedProtocols 且 host 精确命中白名单 */
|
|
89
|
+
function baseUrlError(urlStr, whitelist, allowedProtocols) {
|
|
90
|
+
let u;
|
|
91
|
+
try {
|
|
92
|
+
u = new URL(String(urlStr));
|
|
93
|
+
} catch (e) {
|
|
94
|
+
return 'baseUrl 不是合法 URL';
|
|
95
|
+
}
|
|
96
|
+
const proto = u.protocol.replace(/:$/, '');
|
|
97
|
+
if (!allowedProtocols.includes(proto)) return `协议 ${proto} 不在 allowedProtocols`;
|
|
98
|
+
const host = u.hostname.toLowerCase();
|
|
99
|
+
if (!whitelist.some((w) => String(w).trim().toLowerCase() === host)) return 'baseUrl host 不在白名单';
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 脱敏视图(SPEC §3;另含 F5 需要的时效阈值 staleWarnSec/staleDangerSec 与雷达配置视图) */
|
|
104
|
+
function redactedView(config) {
|
|
105
|
+
const g = config.gitea || {};
|
|
106
|
+
const dt = config.dingtalk || {};
|
|
107
|
+
const fx = config.feishu || {};
|
|
108
|
+
const az = config.analyze || {};
|
|
109
|
+
const sec = config.security || {};
|
|
110
|
+
return {
|
|
111
|
+
host: config.host,
|
|
112
|
+
dataDir: config.dataDir,
|
|
113
|
+
gitea: {
|
|
114
|
+
baseUrl: g.baseUrl,
|
|
115
|
+
apiPrefix: g.apiPrefix,
|
|
116
|
+
org: g.org,
|
|
117
|
+
pollIntervalSec: g.pollIntervalSec,
|
|
118
|
+
hostWhitelist: store.clone(g.hostWhitelist || []),
|
|
119
|
+
staleWarnSec: g.staleWarnSec,
|
|
120
|
+
staleDangerSec: g.staleDangerSec,
|
|
121
|
+
// v2.9.2 忽略账号(成员册派生排除名单)如实带出
|
|
122
|
+
ignoreLogins: store.clone(Array.isArray(g.ignoreLogins) ? g.ignoreLogins : []),
|
|
123
|
+
},
|
|
124
|
+
// 任务雷达(PRD §6 设置页):钉钉通道开关 + 引擎超时;dwsBin/dshBin 为可编辑键如实带出
|
|
125
|
+
//(本机路径无敏感值;空串 = 运行时按已知安装位置自动解析)
|
|
126
|
+
dingtalk: {
|
|
127
|
+
enabled: dt.enabled !== false,
|
|
128
|
+
dwsBin: typeof dt.dwsBin === 'string' ? dt.dwsBin : '',
|
|
129
|
+
dwsTimeoutSec: Number(dt.dwsTimeoutSec) || 60,
|
|
130
|
+
maxMessages: Number(dt.maxMessages) || 200,
|
|
131
|
+
hostWhitelist: store.clone(dt.hostWhitelist || []),
|
|
132
|
+
},
|
|
133
|
+
// 飞书妙记通道(与 dingtalk 同构;larkBin 为本机路径无敏感值,空串 = 运行时自动解析)
|
|
134
|
+
feishu: {
|
|
135
|
+
enabled: fx.enabled === true,
|
|
136
|
+
larkBin: typeof fx.larkBin === 'string' ? fx.larkBin : '',
|
|
137
|
+
larkTimeoutSec: Number(fx.larkTimeoutSec) || 60,
|
|
138
|
+
maxMessages: Number(fx.maxMessages) || 200,
|
|
139
|
+
lookbackDays: Number(fx.lookbackDays) || 90,
|
|
140
|
+
hostWhitelist: store.clone(fx.hostWhitelist || []),
|
|
141
|
+
},
|
|
142
|
+
analyze: {
|
|
143
|
+
dshBin: typeof az.dshBin === 'string' ? az.dshBin : '',
|
|
144
|
+
timeoutSec: Number(az.timeoutSec) || 300,
|
|
145
|
+
maxTasksPerMessage: Number(az.maxTasksPerMessage) || 10,
|
|
146
|
+
},
|
|
147
|
+
// v2.2 智能体工作观测(本期仅设置视图带出,字段留好;设置 UI 后续接入)
|
|
148
|
+
agentActivity: agentActivityView(config),
|
|
149
|
+
// v2.9 GitHub 组织动态源(v2.9.17 页面化:enabled/org/baseUrl 可经设置页 PATCH,
|
|
150
|
+
// hot-apply 随下个轮询周期生效;凭据仍走 git config 零落盘不进页面体系)
|
|
151
|
+
github: {
|
|
152
|
+
enabled: !!(config.github && config.github.enabled === true),
|
|
153
|
+
baseUrl: config.github ? config.github.baseUrl : '',
|
|
154
|
+
org: config.github ? config.github.org : '',
|
|
155
|
+
hostWhitelist: store.clone(config.github && Array.isArray(config.github.hostWhitelist) ? config.github.hostWhitelist : []),
|
|
156
|
+
},
|
|
157
|
+
// v2.9.18 Gitee 组织动态源(同 github 口径:三键可 PATCH 热生效,凭据 git config 零落盘)
|
|
158
|
+
gitee: {
|
|
159
|
+
enabled: !!(config.gitee && config.gitee.enabled === true),
|
|
160
|
+
baseUrl: config.gitee ? config.gitee.baseUrl : '',
|
|
161
|
+
org: config.gitee ? config.gitee.org : '',
|
|
162
|
+
hostWhitelist: store.clone(config.gitee && Array.isArray(config.gitee.hostWhitelist) ? config.gitee.hostWhitelist : []),
|
|
163
|
+
},
|
|
164
|
+
security: {
|
|
165
|
+
trustedHosts: store.clone(Array.isArray(sec.trustedHosts) ? sec.trustedHosts : []),
|
|
166
|
+
},
|
|
167
|
+
// v2.9.29 D4:新配置节只读可见性(webhookOut/agentContext 仅 config.json 手改——
|
|
168
|
+
// 页面只展示状态不给编辑面;url 只出 host 不出 query,下游令牌不进设置视图)
|
|
169
|
+
webhookOut: {
|
|
170
|
+
enabled: !!(config.webhookOut && config.webhookOut.enabled === true),
|
|
171
|
+
urlHost: urlHostOf(config.webhookOut && config.webhookOut.url),
|
|
172
|
+
},
|
|
173
|
+
agentContext: {
|
|
174
|
+
enabled: !!(config.agentContext && config.agentContext.enabled === true),
|
|
175
|
+
dirsCount: Array.isArray(config.agentContext && config.agentContext.dirs) ? config.agentContext.dirs.length : 0,
|
|
176
|
+
},
|
|
177
|
+
credential: CREDENTIAL_VIEW[sec.credentialSource] || 'UNKNOWN',
|
|
178
|
+
// v2.9.39 IM 通知(飞书/钉钉):只读回显配置状态;token 只出 host + 尾 4 位,secret 只报有无
|
|
179
|
+
im: imView(config),
|
|
180
|
+
// v2.9.44 知识库学习:只读回显(root 是本机路径,无敏感值;rootOk 由 lib/learn 校验)
|
|
181
|
+
learn: learnView(config),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** 学习模式视图:路径与范围如实带出,rootOk 由调用方补(需要 fs 校验,放在路由层) */
|
|
186
|
+
function learnView(config) {
|
|
187
|
+
const l = (config && config.learn) || {};
|
|
188
|
+
return {
|
|
189
|
+
enabled: l.enabled === true,
|
|
190
|
+
root: typeof l.root === 'string' ? l.root : '',
|
|
191
|
+
scope: store.clone(Array.isArray(l.scope) ? l.scope : []),
|
|
192
|
+
exclude: store.clone(Array.isArray(l.exclude) ? l.exclude : []),
|
|
193
|
+
seeds: store.clone(Array.isArray(l.seeds) ? l.seeds : []),
|
|
194
|
+
dailyNew: Number(l.dailyNew) || 1,
|
|
195
|
+
dailyReview: Number(l.dailyReview) || 3,
|
|
196
|
+
minutes: Number(l.minutes) || 15,
|
|
197
|
+
engine: {
|
|
198
|
+
mode: (l.engine && l.engine.mode) || 'auto',
|
|
199
|
+
timeoutSec: Number(l.engine && l.engine.timeoutSec) || 180,
|
|
200
|
+
reasoningEffort: (l.engine && l.engine.reasoningEffort) || 'off',
|
|
201
|
+
maxTokens: Number(l.engine && l.engine.maxTokens) || 1200,
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** IM 目标脱敏视图:平台 + host + token 尾 4 位(机器人 URL 含令牌,绝不整串回显) */
|
|
207
|
+
function imTargetView(t) {
|
|
208
|
+
const url = t && typeof t.url === 'string' ? t.url : '';
|
|
209
|
+
let tail = '';
|
|
210
|
+
try {
|
|
211
|
+
const u = new URL(url);
|
|
212
|
+
const lastSeg = u.pathname.split('/').filter(Boolean).pop() || '';
|
|
213
|
+
const token = u.searchParams.get('access_token') || lastSeg;
|
|
214
|
+
tail = token ? token.slice(-4) : '';
|
|
215
|
+
} catch (e) {
|
|
216
|
+
tail = '';
|
|
217
|
+
}
|
|
218
|
+
const platform = t && (t.platform === 'dingtalk' || t.platform === 'feishu') ? t.platform : '';
|
|
219
|
+
return {
|
|
220
|
+
platform,
|
|
221
|
+
urlHost: urlHostOf(url),
|
|
222
|
+
tokenTail: tail,
|
|
223
|
+
hasSecret: !!(t && typeof t.secret === 'string' && t.secret.trim() !== ''),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const HHMM_RE = /^([01]\d|2[0-3]):[0-5]\d$/;
|
|
228
|
+
|
|
229
|
+
function imView(config) {
|
|
230
|
+
const im = (config && config.im) || {};
|
|
231
|
+
const dg = im.digest || {};
|
|
232
|
+
const ev = im.events || {};
|
|
233
|
+
return {
|
|
234
|
+
enabled: im.enabled === true,
|
|
235
|
+
digest: { enabled: dg.enabled === true, at: HHMM_RE.test(String(dg.at || '')) ? dg.at : '09:10' },
|
|
236
|
+
events: { review: ev.review === true, completed: ev.completed === true },
|
|
237
|
+
targets: (Array.isArray(im.targets) ? im.targets : []).map(imTargetView),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** webhook url → 仅协议+host(query 里的下游令牌不进设置视图) */
|
|
242
|
+
function urlHostOf(u) {
|
|
243
|
+
try {
|
|
244
|
+
const p = new URL(String(u || ''));
|
|
245
|
+
return `${p.protocol}//${p.host}`;
|
|
246
|
+
} catch (e) {
|
|
247
|
+
return '';
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function deepMerge(target, patch) {
|
|
252
|
+
for (const k of Object.keys(patch)) {
|
|
253
|
+
const v = patch[k];
|
|
254
|
+
if (store.isPlainObject(v) && store.isPlainObject(target[k])) {
|
|
255
|
+
deepMerge(target[k], v);
|
|
256
|
+
} else {
|
|
257
|
+
target[k] = store.isPlainObject(v) ? store.clone(v) : v;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return target;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function requirePositiveInt(obj, key, label) {
|
|
264
|
+
if (obj[key] !== undefined && (!Number.isInteger(obj[key]) || obj[key] <= 0)) {
|
|
265
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `${label} 必须为正整数`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function applyPatch(config, body, configPath) {
|
|
270
|
+
if (!store.isPlainObject(body)) throw new HttpError(400, 'SETTINGS_INVALID', '请求体必须是 JSON 对象');
|
|
271
|
+
|
|
272
|
+
// ① host 白名单:仅接受 127.0.0.1/0.0.0.0(SPEC-PLUGIN 2.1 与宿主 webServer schema 同源)
|
|
273
|
+
if (body.host !== undefined && body.host !== '127.0.0.1' && body.host !== '0.0.0.0') {
|
|
274
|
+
throw new HttpError(400, 'HOST_FORBIDDEN', 'host 仅接受 127.0.0.1/0.0.0.0');
|
|
275
|
+
}
|
|
276
|
+
// ② 目录(v1.5:dataDir/webDir 只读,PATCH 一律忽略,不再校验也不再写回;WP6:port 出白名单,传入即静默忽略)
|
|
277
|
+
// ③ gitea 节
|
|
278
|
+
let nextWhitelist = store.clone((config.gitea || {}).hostWhitelist || []);
|
|
279
|
+
if (body.gitea !== undefined) {
|
|
280
|
+
const g = body.gitea;
|
|
281
|
+
if (!store.isPlainObject(g)) throw new HttpError(400, 'SETTINGS_INVALID', 'gitea 必须为对象');
|
|
282
|
+
if (g.hostWhitelist !== undefined) {
|
|
283
|
+
if (!Array.isArray(g.hostWhitelist) || g.hostWhitelist.length === 0) {
|
|
284
|
+
throw new HttpError(400, 'WHITELIST_INVALID', 'hostWhitelist 必须为非空字符串数组');
|
|
285
|
+
}
|
|
286
|
+
for (const entry of g.hostWhitelist) {
|
|
287
|
+
const bad = forbiddenWhitelistEntry(entry);
|
|
288
|
+
if (bad) throw new HttpError(400, 'WHITELIST_INVALID', `白名单含禁止项 "${entry}"(${bad})`);
|
|
289
|
+
}
|
|
290
|
+
nextWhitelist = g.hostWhitelist;
|
|
291
|
+
}
|
|
292
|
+
if (g.baseUrl !== undefined) {
|
|
293
|
+
const err = baseUrlError(g.baseUrl, nextWhitelist, config.security.allowedProtocols);
|
|
294
|
+
if (err) throw new HttpError(400, 'HOST_NOT_ALLOWED', err);
|
|
295
|
+
}
|
|
296
|
+
requirePositiveInt(g, 'staleWarnSec', 'staleWarnSec');
|
|
297
|
+
requirePositiveInt(g, 'staleDangerSec', 'staleDangerSec');
|
|
298
|
+
requirePositiveInt(g, 'maxActivities', 'maxActivities');
|
|
299
|
+
// v2.9.2 忽略账号:字符串数组(成员册派生排除名单)
|
|
300
|
+
if (g.ignoreLogins !== undefined) {
|
|
301
|
+
if (!Array.isArray(g.ignoreLogins) || g.ignoreLogins.some((t) => typeof t !== 'string')) {
|
|
302
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'ignoreLogins 必须为字符串数组');
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// pollIntervalSec 允许 0(= 关闭轮询器),其余须正整数
|
|
306
|
+
if (g.pollIntervalSec !== undefined && (!Number.isInteger(g.pollIntervalSec) || g.pollIntervalSec < 0)) {
|
|
307
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'pollIntervalSec 必须为 ≥0 的整数(0 表示关闭轮询器)');
|
|
308
|
+
}
|
|
309
|
+
if (g.apiPrefix !== undefined && (typeof g.apiPrefix !== 'string' || !g.apiPrefix.startsWith('/'))) {
|
|
310
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'apiPrefix 必须为以 / 开头的字符串');
|
|
311
|
+
}
|
|
312
|
+
if (g.org !== undefined && (typeof g.org !== 'string' || g.org.trim() === '')) {
|
|
313
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'org 必须为非空字符串');
|
|
314
|
+
}
|
|
315
|
+
if (g.activityTypes !== undefined) {
|
|
316
|
+
if (!Array.isArray(g.activityTypes) || g.activityTypes.some((t) => typeof t !== 'string')) {
|
|
317
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'activityTypes 必须为字符串数组');
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
// ④ security 节(路由层已先行拦截:仅 trustedHosts 可经 API 修改,其余键仍 #10 仅手改)
|
|
322
|
+
if (body.security !== undefined) {
|
|
323
|
+
const s = body.security;
|
|
324
|
+
if (!store.isPlainObject(s)) throw new HttpError(400, 'SETTINGS_INVALID', 'security 必须为对象');
|
|
325
|
+
if (s.trustedHosts !== undefined) {
|
|
326
|
+
if (!Array.isArray(s.trustedHosts)) {
|
|
327
|
+
throw new HttpError(400, 'WHITELIST_INVALID', 'security.trustedHosts 必须为字符串数组');
|
|
328
|
+
}
|
|
329
|
+
if (s.trustedHosts.length > 64) {
|
|
330
|
+
throw new HttpError(400, 'WHITELIST_INVALID', 'security.trustedHosts 最多 64 项');
|
|
331
|
+
}
|
|
332
|
+
for (const entry of s.trustedHosts) {
|
|
333
|
+
const bad = trustedHostEntryError(entry);
|
|
334
|
+
if (bad) throw new HttpError(400, 'WHITELIST_INVALID', `trustedHosts 含非法项 "${entry}"(${bad})`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (s.allowedProtocols !== undefined) {
|
|
338
|
+
if (!Array.isArray(s.allowedProtocols) || s.allowedProtocols.some((p) => typeof p !== 'string')) {
|
|
339
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'allowedProtocols 必须为字符串数组');
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (s.credentialSource !== undefined && typeof s.credentialSource !== 'string') {
|
|
343
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'credentialSource 必须为字符串');
|
|
344
|
+
}
|
|
345
|
+
if (s.redactKeys !== undefined) {
|
|
346
|
+
if (!Array.isArray(s.redactKeys) || s.redactKeys.some((k) => typeof k !== 'string')) {
|
|
347
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'redactKeys 必须为字符串数组');
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// ⑤ dingtalk 节(任务雷达;hostWhitelist 与 gitea 同一红线)
|
|
352
|
+
if (body.dingtalk !== undefined) {
|
|
353
|
+
const dt = body.dingtalk;
|
|
354
|
+
if (!store.isPlainObject(dt)) throw new HttpError(400, 'SETTINGS_INVALID', 'dingtalk 必须为对象');
|
|
355
|
+
if (dt.enabled !== undefined && typeof dt.enabled !== 'boolean') {
|
|
356
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'dingtalk.enabled 必须为布尔值');
|
|
357
|
+
}
|
|
358
|
+
if (dt.hostWhitelist !== undefined) {
|
|
359
|
+
if (!Array.isArray(dt.hostWhitelist) || dt.hostWhitelist.length === 0) {
|
|
360
|
+
throw new HttpError(400, 'WHITELIST_INVALID', 'dingtalk.hostWhitelist 必须为非空字符串数组');
|
|
361
|
+
}
|
|
362
|
+
for (const entry of dt.hostWhitelist) {
|
|
363
|
+
const bad = forbiddenWhitelistEntry(entry);
|
|
364
|
+
if (bad) throw new HttpError(400, 'WHITELIST_INVALID', `白名单含禁止项 "${entry}"(${bad})`);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
// dwsBin:字符串即可(空串 = 重置为运行时按已知安装位置自动解析,见 lib/dingtalk.js resolveDwsBin)
|
|
368
|
+
if (dt.dwsBin !== undefined && typeof dt.dwsBin !== 'string') {
|
|
369
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'dingtalk.dwsBin 必须为字符串(空串 = 重置为自动解析)');
|
|
370
|
+
}
|
|
371
|
+
requirePositiveInt(dt, 'dwsTimeoutSec', 'dingtalk.dwsTimeoutSec');
|
|
372
|
+
requirePositiveInt(dt, 'maxMessages', 'dingtalk.maxMessages');
|
|
373
|
+
}
|
|
374
|
+
// ⑤-b feishu 节(飞书妙记通道;hostWhitelist 与 dingtalk 同一红线)
|
|
375
|
+
if (body.feishu !== undefined) {
|
|
376
|
+
const fx = body.feishu;
|
|
377
|
+
if (!store.isPlainObject(fx)) throw new HttpError(400, 'SETTINGS_INVALID', 'feishu 必须为对象');
|
|
378
|
+
if (fx.enabled !== undefined && typeof fx.enabled !== 'boolean') {
|
|
379
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'feishu.enabled 必须为布尔值');
|
|
380
|
+
}
|
|
381
|
+
if (fx.hostWhitelist !== undefined) {
|
|
382
|
+
if (!Array.isArray(fx.hostWhitelist) || fx.hostWhitelist.length === 0) {
|
|
383
|
+
throw new HttpError(400, 'WHITELIST_INVALID', 'feishu.hostWhitelist 必须为非空字符串数组');
|
|
384
|
+
}
|
|
385
|
+
for (const entry of fx.hostWhitelist) {
|
|
386
|
+
const bad = forbiddenWhitelistEntry(entry);
|
|
387
|
+
if (bad) throw new HttpError(400, 'WHITELIST_INVALID', `白名单含禁止项 "${entry}"(${bad})`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
// larkBin:字符串即可(空串 = 重置为运行时按已知安装位置自动解析,见 lib/feishu.js resolveLarkBin)
|
|
391
|
+
if (fx.larkBin !== undefined && typeof fx.larkBin !== 'string') {
|
|
392
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'feishu.larkBin 必须为字符串(空串 = 重置为自动解析)');
|
|
393
|
+
}
|
|
394
|
+
requirePositiveInt(fx, 'larkTimeoutSec', 'feishu.larkTimeoutSec');
|
|
395
|
+
requirePositiveInt(fx, 'maxMessages', 'feishu.maxMessages');
|
|
396
|
+
requirePositiveInt(fx, 'lookbackDays', 'feishu.lookbackDays');
|
|
397
|
+
}
|
|
398
|
+
// ⑥ analyze 节
|
|
399
|
+
if (body.analyze !== undefined) {
|
|
400
|
+
const az = body.analyze;
|
|
401
|
+
if (!store.isPlainObject(az)) throw new HttpError(400, 'SETTINGS_INVALID', 'analyze 必须为对象');
|
|
402
|
+
// dshBin:字符串即可(空串 = 重置为运行时自动解析,见 lib/analyze.js resolveDshBin)
|
|
403
|
+
if (az.dshBin !== undefined && typeof az.dshBin !== 'string') {
|
|
404
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'analyze.dshBin 必须为字符串(空串 = 重置为自动解析)');
|
|
405
|
+
}
|
|
406
|
+
requirePositiveInt(az, 'timeoutSec', 'analyze.timeoutSec');
|
|
407
|
+
requirePositiveInt(az, 'maxTasksPerMessage', 'analyze.maxTasksPerMessage');
|
|
408
|
+
requirePositiveInt(az, 'maxPromptChars', 'analyze.maxPromptChars');
|
|
409
|
+
}
|
|
410
|
+
// ⑦ gateway 节:v2.2 已随 网关集成整体移除——不校验、不合并、不回填,
|
|
411
|
+
// PATCH 中的 gateway 键被 TOP_LEVEL_ALLOWED 白名单静默丢弃(同 port 口径)
|
|
412
|
+
|
|
413
|
+
// ⑥⑦ github 节(v2.9.17 页面化):仅 enabled/org/baseUrl 三键可经 API——凭据仍走 git config
|
|
414
|
+
// 零落盘(不进页面体系);eventsPages/hostWhitelist 保持仅手改(携带即 400 防误配)。
|
|
415
|
+
// baseUrl 校验协议 + host 是否在 github.hostWhitelist(自建 GitHub Enterprise 需先手改白名单)
|
|
416
|
+
if (body.github !== undefined) {
|
|
417
|
+
const gh = body.github;
|
|
418
|
+
if (!store.isPlainObject(gh)) throw new HttpError(400, 'SETTINGS_INVALID', 'github 必须为对象');
|
|
419
|
+
const ghForeign = Object.keys(gh).filter((k) => !['enabled', 'org', 'baseUrl'].includes(k));
|
|
420
|
+
if (ghForeign.length > 0) {
|
|
421
|
+
throw new HttpError(
|
|
422
|
+
400,
|
|
423
|
+
'SETTINGS_INVALID',
|
|
424
|
+
`github.${ghForeign[0]} 不可通过 API 修改(仅 enabled/org/baseUrl;eventsPages/hostWhitelist 仅手改 config.json)`
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
if (gh.enabled !== undefined && typeof gh.enabled !== 'boolean') {
|
|
428
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'github.enabled 必须为布尔值');
|
|
429
|
+
}
|
|
430
|
+
if (gh.org !== undefined && (typeof gh.org !== 'string' || gh.org.trim().length > 100)) {
|
|
431
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'github.org 必须为 ≤100 字符的字符串(空串 = 清除)');
|
|
432
|
+
}
|
|
433
|
+
if (gh.baseUrl !== undefined && gh.baseUrl !== '') {
|
|
434
|
+
let u;
|
|
435
|
+
try { u = new URL(String(gh.baseUrl)); } catch (e) {
|
|
436
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'github.baseUrl 不是合法 URL');
|
|
437
|
+
}
|
|
438
|
+
const proto = u.protocol.replace(/:$/, '');
|
|
439
|
+
if (!(config.security.allowedProtocols || []).includes(proto)) {
|
|
440
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `github.baseUrl 协议 ${proto} 不在 allowedProtocols`);
|
|
441
|
+
}
|
|
442
|
+
const ghWl = (config.github && Array.isArray(config.github.hostWhitelist)) ? config.github.hostWhitelist : [];
|
|
443
|
+
const ghHost = u.hostname.toLowerCase();
|
|
444
|
+
if (!ghWl.some((w) => String(w).trim().toLowerCase() === ghHost)) {
|
|
445
|
+
throw new HttpError(
|
|
446
|
+
400,
|
|
447
|
+
'HOST_NOT_ALLOWED',
|
|
448
|
+
`github.baseUrl host "${u.hostname}" 不在 github.hostWhitelist(默认 api.github.com;自建 GitHub Enterprise 先手改 config.json 白名单)`
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// ⑥⑧ gitee 节(v2.9.18 页面化):镜像 github 节口径——仅 enabled/org/baseUrl 三键可经 API;
|
|
455
|
+
// eventsPages/hostWhitelist 仅手改(携带即 400);baseUrl host 须在 gitee.hostWhitelist
|
|
456
|
+
if (body.gitee !== undefined) {
|
|
457
|
+
const ge = body.gitee;
|
|
458
|
+
if (!store.isPlainObject(ge)) throw new HttpError(400, 'SETTINGS_INVALID', 'gitee 必须为对象');
|
|
459
|
+
const geForeign = Object.keys(ge).filter((k) => !['enabled', 'org', 'baseUrl'].includes(k));
|
|
460
|
+
if (geForeign.length > 0) {
|
|
461
|
+
throw new HttpError(
|
|
462
|
+
400,
|
|
463
|
+
'SETTINGS_INVALID',
|
|
464
|
+
`gitee.${geForeign[0]} 不可通过 API 修改(仅 enabled/org/baseUrl;eventsPages/hostWhitelist 仅手改 config.json)`
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
if (ge.enabled !== undefined && typeof ge.enabled !== 'boolean') {
|
|
468
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'gitee.enabled 必须为布尔值');
|
|
469
|
+
}
|
|
470
|
+
if (ge.org !== undefined && (typeof ge.org !== 'string' || ge.org.trim().length > 100)) {
|
|
471
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'gitee.org 必须为 ≤100 字符的字符串(空串 = 清除)');
|
|
472
|
+
}
|
|
473
|
+
if (ge.baseUrl !== undefined && ge.baseUrl !== '') {
|
|
474
|
+
let gu;
|
|
475
|
+
try { gu = new URL(String(ge.baseUrl)); } catch (e) {
|
|
476
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'gitee.baseUrl 不是合法 URL');
|
|
477
|
+
}
|
|
478
|
+
const gproto = gu.protocol.replace(/:$/, '');
|
|
479
|
+
if (!(config.security.allowedProtocols || []).includes(gproto)) {
|
|
480
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `gitee.baseUrl 协议 ${gproto} 不在 allowedProtocols`);
|
|
481
|
+
}
|
|
482
|
+
const geWl = (config.gitee && Array.isArray(config.gitee.hostWhitelist)) ? config.gitee.hostWhitelist : [];
|
|
483
|
+
const geHost = gu.hostname.toLowerCase();
|
|
484
|
+
if (!geWl.some((w) => String(w).trim().toLowerCase() === geHost)) {
|
|
485
|
+
throw new HttpError(
|
|
486
|
+
400,
|
|
487
|
+
'HOST_NOT_ALLOWED',
|
|
488
|
+
`gitee.baseUrl host "${gu.hostname}" 不在 gitee.hostWhitelist(默认 gitee.com;自建/企业实例先手改 config.json 白名单)`
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// ⑧ agentActivity 节(v2.2 智能体工作观测):总开关/限额为正整数,sources 各源
|
|
495
|
+
// enabled 布尔 + root/dbPath 字符串(空串 = 回退本机默认位置),含桌面成员源 codex/opencode
|
|
496
|
+
let agentActivityPatch;
|
|
497
|
+
if (body.agentActivity !== undefined) {
|
|
498
|
+
const aa = body.agentActivity;
|
|
499
|
+
if (!store.isPlainObject(aa)) throw new HttpError(400, 'SETTINGS_INVALID', 'agentActivity 必须为对象');
|
|
500
|
+
if (aa.enabled !== undefined && typeof aa.enabled !== 'boolean') {
|
|
501
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'agentActivity.enabled 必须为布尔值');
|
|
502
|
+
}
|
|
503
|
+
for (const k of ['perSourceLimit', 'maxItems', 'previewChars']) {
|
|
504
|
+
requirePositiveInt(aa, k, `agentActivity.${k}`);
|
|
505
|
+
}
|
|
506
|
+
// v2.3 会话阅读器限额:sessionList/transcript 子节(正整数键;normalize 兜底缺省,F1)
|
|
507
|
+
for (const sub of ['sessionList', 'transcript']) {
|
|
508
|
+
if (aa[sub] === undefined) continue;
|
|
509
|
+
if (!store.isPlainObject(aa[sub])) {
|
|
510
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `agentActivity.${sub} 必须为对象`);
|
|
511
|
+
}
|
|
512
|
+
for (const k of Object.keys(agentActivity.DEFAULT_AGENT_ACTIVITY[sub])) {
|
|
513
|
+
if (aa[sub][k] !== undefined) requirePositiveInt(aa[sub], k, `agentActivity.${sub}.${k}`);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
if (aa.sources !== undefined) {
|
|
517
|
+
if (!store.isPlainObject(aa.sources)) {
|
|
518
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'agentActivity.sources 必须为对象');
|
|
519
|
+
}
|
|
520
|
+
for (const id of agentActivity.ALL_SOURCE_IDS) {
|
|
521
|
+
const s = aa.sources[id];
|
|
522
|
+
if (s === undefined) continue;
|
|
523
|
+
if (!store.isPlainObject(s)) {
|
|
524
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `agentActivity.sources.${id} 必须为对象`);
|
|
525
|
+
}
|
|
526
|
+
if (s.enabled !== undefined && typeof s.enabled !== 'boolean') {
|
|
527
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `agentActivity.sources.${id}.enabled 必须为布尔值`);
|
|
528
|
+
}
|
|
529
|
+
for (const k of ['root', 'dbPath']) {
|
|
530
|
+
if (s[k] !== undefined && typeof s[k] !== 'string') {
|
|
531
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `agentActivity.sources.${id}.${k} 必须为字符串(空串 = 默认位置)`);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
agentActivityPatch = aa; // 全节参与合并(normalize 回填兜底缺省键)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// ⑨ im 节(v2.9.39 IM 通知:飞书/钉钉自定义机器人)。URL 必过出站闸(私网/回环拒绝),
|
|
540
|
+
// secret 只校验类型与长度——值本身不回显、不进日志(HMAC 用,属加签密钥)
|
|
541
|
+
if (body.im !== undefined) {
|
|
542
|
+
const im = body.im;
|
|
543
|
+
if (!store.isPlainObject(im)) throw new HttpError(400, 'SETTINGS_INVALID', 'im 必须为对象');
|
|
544
|
+
if (im.enabled !== undefined && typeof im.enabled !== 'boolean') {
|
|
545
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'im.enabled 必须为布尔值');
|
|
546
|
+
}
|
|
547
|
+
if (im.digest !== undefined) {
|
|
548
|
+
if (!store.isPlainObject(im.digest)) throw new HttpError(400, 'SETTINGS_INVALID', 'im.digest 必须为对象');
|
|
549
|
+
if (im.digest.enabled !== undefined && typeof im.digest.enabled !== 'boolean') {
|
|
550
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'im.digest.enabled 必须为布尔值');
|
|
551
|
+
}
|
|
552
|
+
if (im.digest.at !== undefined && !HHMM_RE.test(String(im.digest.at))) {
|
|
553
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'im.digest.at 必须为 HH:MM(24 小时制本地时间)');
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
if (im.events !== undefined) {
|
|
557
|
+
if (!store.isPlainObject(im.events)) throw new HttpError(400, 'SETTINGS_INVALID', 'im.events 必须为对象');
|
|
558
|
+
for (const k of ['review', 'completed']) {
|
|
559
|
+
if (im.events[k] !== undefined && typeof im.events[k] !== 'boolean') {
|
|
560
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `im.events.${k} 必须为布尔值`);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
if (im.targets !== undefined) {
|
|
565
|
+
if (!Array.isArray(im.targets)) throw new HttpError(400, 'SETTINGS_INVALID', 'im.targets 必须为数组');
|
|
566
|
+
if (im.targets.length > 4) throw new HttpError(400, 'SETTINGS_INVALID', 'im.targets 最多 4 个');
|
|
567
|
+
// 懒加载:settings 与 webhookOut 互不依赖初始化顺序(webhookOut 又 require giteaClient)
|
|
568
|
+
const { guardWebhookUrl } = require('./webhookOut');
|
|
569
|
+
im.targets.forEach((t, i) => {
|
|
570
|
+
if (!store.isPlainObject(t)) throw new HttpError(400, 'SETTINGS_INVALID', `im.targets[${i}] 必须为对象`);
|
|
571
|
+
if (t.platform !== 'dingtalk' && t.platform !== 'feishu') {
|
|
572
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `im.targets[${i}].platform 仅支持 dingtalk/feishu`);
|
|
573
|
+
}
|
|
574
|
+
if (typeof t.url !== 'string' || t.url.trim() === '') {
|
|
575
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `im.targets[${i}].url 必填`);
|
|
576
|
+
}
|
|
577
|
+
if (t.url.length > 500) throw new HttpError(400, 'SETTINGS_INVALID', `im.targets[${i}].url 超过 500 字符`);
|
|
578
|
+
const guard = guardWebhookUrl(t.url);
|
|
579
|
+
if (!guard.ok) throw new HttpError(400, 'HOST_NOT_ALLOWED', `im.targets[${i}].url: ${guard.reason}`);
|
|
580
|
+
if (t.secret !== undefined && (typeof t.secret !== 'string' || t.secret.length > 200)) {
|
|
581
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `im.targets[${i}].secret 必须为不超过 200 字符的字符串`);
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// ⑩ learn 节(v2.9.44 知识库学习)。root 只做"绝对路径"形状校验——存在性/AGENTS 标记由
|
|
588
|
+
// /api/learn/status 与「测试根路径」校验(配路径时盘可能还没挂,不该拦住保存)
|
|
589
|
+
if (body.learn !== undefined) {
|
|
590
|
+
const l = body.learn;
|
|
591
|
+
if (!store.isPlainObject(l)) throw new HttpError(400, 'SETTINGS_INVALID', 'learn 必须为对象');
|
|
592
|
+
if (l.enabled !== undefined && typeof l.enabled !== 'boolean') {
|
|
593
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'learn.enabled 必须为布尔值');
|
|
594
|
+
}
|
|
595
|
+
if (l.root !== undefined) {
|
|
596
|
+
if (typeof l.root !== 'string') throw new HttpError(400, 'SETTINGS_INVALID', 'learn.root 必须为字符串');
|
|
597
|
+
if (l.root.length > 400) throw new HttpError(400, 'SETTINGS_INVALID', 'learn.root 超过 400 字符');
|
|
598
|
+
if (l.root.trim() !== '' && !path.isAbsolute(l.root.trim())) {
|
|
599
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'learn.root 必须是绝对路径(如 D:\\\\knowledge\\\\my-vault)');
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
for (const k of ['scope', 'exclude']) {
|
|
603
|
+
if (l[k] === undefined) continue;
|
|
604
|
+
if (!Array.isArray(l[k]) || l[k].some((x) => typeof x !== 'string')) {
|
|
605
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `learn.${k} 必须为字符串数组`);
|
|
606
|
+
}
|
|
607
|
+
if (l[k].length > 50) throw new HttpError(400, 'SETTINGS_INVALID', `learn.${k} 最多 50 项`);
|
|
608
|
+
if (l[k].some((x) => x.length > 200)) throw new HttpError(400, 'SETTINGS_INVALID', `learn.${k} 单项超过 200 字符`);
|
|
609
|
+
}
|
|
610
|
+
// v2.9.52 内置主题:只接受 lib/learnSeeds.js 里声明过的名字(空数组 = 只用知识库)
|
|
611
|
+
if (l.seeds !== undefined) {
|
|
612
|
+
if (!Array.isArray(l.seeds) || l.seeds.some((x) => typeof x !== 'string')) {
|
|
613
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'learn.seeds 必须为字符串数组');
|
|
614
|
+
}
|
|
615
|
+
const known = require('./learnSeeds').DOMAINS;
|
|
616
|
+
const bad = l.seeds.filter((x) => !known.includes(x));
|
|
617
|
+
if (bad.length) throw new HttpError(400, 'SETTINGS_INVALID', `learn.seeds 含未知主题:${bad.join('、')}(可选:${known.join('、')})`);
|
|
618
|
+
if (l.seeds.length > 10) throw new HttpError(400, 'SETTINGS_INVALID', 'learn.seeds 最多 10 个主题');
|
|
619
|
+
}
|
|
620
|
+
for (const k of ['dailyNew', 'dailyReview']) {
|
|
621
|
+
if (l[k] === undefined) continue;
|
|
622
|
+
if (!Number.isInteger(l[k]) || l[k] < 0 || l[k] > 10) {
|
|
623
|
+
throw new HttpError(400, 'SETTINGS_INVALID', `learn.${k} 必须为 0..10 的整数`);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
if (l.minutes !== undefined && (!Number.isInteger(l.minutes) || l.minutes < 1 || l.minutes > 240)) {
|
|
627
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'learn.minutes 必须为 1..240 的整数');
|
|
628
|
+
}
|
|
629
|
+
if (l.engine !== undefined) {
|
|
630
|
+
if (!store.isPlainObject(l.engine)) throw new HttpError(400, 'SETTINGS_INVALID', 'learn.engine 必须为对象');
|
|
631
|
+
if (l.engine.mode !== undefined && !['auto', 'host', 'dsh', 'off'].includes(l.engine.mode)) {
|
|
632
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'learn.engine.mode 仅支持 auto/host/dsh/off');
|
|
633
|
+
}
|
|
634
|
+
if (l.engine.reasoningEffort !== undefined && (typeof l.engine.reasoningEffort !== 'string' || !['off', 'low', 'medium', 'high', 'max'].includes(l.engine.reasoningEffort))) {
|
|
635
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'learn.engine.reasoningEffort 仅支持 off/low/medium/high/max');
|
|
636
|
+
}
|
|
637
|
+
if (l.engine.maxTokens !== undefined && (!Number.isInteger(l.engine.maxTokens) || l.engine.maxTokens < 100 || l.engine.maxTokens > 8000)) {
|
|
638
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'learn.engine.maxTokens 必须为 100..8000 的整数');
|
|
639
|
+
}
|
|
640
|
+
if (l.engine.timeoutSec !== undefined && (!Number.isInteger(l.engine.timeoutSec) || l.engine.timeoutSec < 10 || l.engine.timeoutSec > 1800)) {
|
|
641
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'learn.engine.timeoutSec 必须为 10..1800 的整数');
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// 仅取白名单顶层键做校验;合并基线取磁盘现值(#8:保留 API 不认识的手改键,
|
|
647
|
+
// 包括 PATCH 间隔期间直接写入磁盘的未知顶层/嵌套键),整体写回
|
|
648
|
+
const patch = {};
|
|
649
|
+
for (const k of TOP_LEVEL_ALLOWED) {
|
|
650
|
+
if (body[k] !== undefined) patch[k] = body[k];
|
|
651
|
+
}
|
|
652
|
+
// gateway 节:v2.2 已移除,白名单外顶层键静默丢弃(不合并、不回填)
|
|
653
|
+
// agentActivity 节整体参与合并(normalizeAgentActivity 回填 sources 缺省源)
|
|
654
|
+
if (agentActivityPatch) patch.agentActivity = agentActivityPatch;
|
|
655
|
+
const diskRaw = configPath ? store.readJSON(configPath, null) : null;
|
|
656
|
+
const base = store.isPlainObject(diskRaw) ? diskRaw : store.clone(config);
|
|
657
|
+
const next = deepMerge(base, patch);
|
|
658
|
+
// v2.9.39 im.targets:按平台合并——PATCH 只填一个平台时另一个不得被数组整体替换冲掉;
|
|
659
|
+
// 同平台未给 secret 则沿用旧密钥(页面永远拿不到密钥原文)。显式空数组 = 清空全部。
|
|
660
|
+
if (Array.isArray(body.im && body.im.targets)) {
|
|
661
|
+
const patchTargets = body.im.targets;
|
|
662
|
+
next.im = store.isPlainObject(next.im) ? next.im : {};
|
|
663
|
+
if (patchTargets.length === 0) {
|
|
664
|
+
next.im.targets = [];
|
|
665
|
+
} else {
|
|
666
|
+
const baseList = store.isPlainObject(base.im) && Array.isArray(base.im.targets) ? base.im.targets : [];
|
|
667
|
+
const pool = new Map();
|
|
668
|
+
for (const t of baseList) if (t && typeof t.platform === 'string') pool.set(t.platform, t);
|
|
669
|
+
const merged = [];
|
|
670
|
+
for (const t of patchTargets) {
|
|
671
|
+
const prev = pool.get(t.platform) || {};
|
|
672
|
+
const secret = typeof t.secret === 'string' && t.secret.trim() !== ''
|
|
673
|
+
? t.secret
|
|
674
|
+
: (typeof prev.secret === 'string' ? prev.secret : undefined);
|
|
675
|
+
merged.push(Object.assign({}, prev, t, secret === undefined ? {} : { secret }));
|
|
676
|
+
pool.delete(t.platform);
|
|
677
|
+
}
|
|
678
|
+
for (const rest of pool.values()) merged.push(rest);
|
|
679
|
+
next.im.targets = merged;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
// 任务雷达节缺省回填:PATCH 基线取磁盘现值,而旧 config.json 尚无 dingtalk/analyze 节——
|
|
683
|
+
// 不回填的话,首次 PATCH 会把运行期 config 的默认注入一并抹掉,雷达被误判「通道未启用」
|
|
684
|
+
//(真实联调 7795 实测复现,勿回退)。按缺省键级合并:局部 PATCH(如只写 enabled)
|
|
685
|
+
// 也不得丢掉 hostWhitelist 等未涉及键的默认值
|
|
686
|
+
next.dingtalk = Object.assign(store.clone(store.DEFAULT_CONFIG.dingtalk), store.isPlainObject(next.dingtalk) ? next.dingtalk : {});
|
|
687
|
+
// 飞书妙记节缺省回填(同 dingtalk 口径):磁盘无 feishu 节时 PATCH 不得丢掉默认注入
|
|
688
|
+
next.feishu = Object.assign(store.clone(store.DEFAULT_CONFIG.feishu), store.isPlainObject(next.feishu) ? next.feishu : {});
|
|
689
|
+
next.analyze = Object.assign(store.clone(store.DEFAULT_CONFIG.analyze), store.isPlainObject(next.analyze) ? next.analyze : {});
|
|
690
|
+
// v2.2:gateway 节缺省回填已随 网关集成移除——磁盘遗留 gateway 键在 PATCH 写回时一并剥除
|
|
691
|
+
delete next.gateway;
|
|
692
|
+
// security 节缺省回填(同 dingtalk/analyze 口径):磁盘无 security 节时 PATCH trustedHosts
|
|
693
|
+
// 不得丢掉 allowedProtocols/credentialSource/redactKeys 默认注入(baseUrl 校验依赖 allowedProtocols)
|
|
694
|
+
next.security = Object.assign(store.clone(store.DEFAULT_CONFIG.security), store.isPlainObject(next.security) ? next.security : {});
|
|
695
|
+
// v2.9.17 github 节缺省回填(同 dingtalk/analyze 口径):磁盘无 github 节或节内无 hostWhitelist/
|
|
696
|
+
// eventsPages 时补默认值,页面 PATCH enabled/org/baseUrl 不得丢掉白名单等手改键的默认注入
|
|
697
|
+
next.github = Object.assign(store.clone(store.DEFAULT_CONFIG.github), store.isPlainObject(next.github) ? next.github : {});
|
|
698
|
+
// v2.9.18 gitee 节缺省回填(同 github 口径)
|
|
699
|
+
next.gitee = Object.assign(store.clone(store.DEFAULT_CONFIG.gitee), store.isPlainObject(next.gitee) ? next.gitee : {});
|
|
700
|
+
// v2.9.39 im 节缺省回填(digest/events 按缺省键级合并,targets 数组整体替换——
|
|
701
|
+
// 旧 config.json 无 im 节时,首次 PATCH 不得抹掉默认开关结构)
|
|
702
|
+
const imBase = store.isPlainObject(next.im) ? next.im : {};
|
|
703
|
+
next.im = Object.assign({}, store.clone(store.DEFAULT_CONFIG.im), imBase, {
|
|
704
|
+
digest: Object.assign({}, store.clone(store.DEFAULT_CONFIG.im.digest), store.isPlainObject(imBase.digest) ? imBase.digest : {}),
|
|
705
|
+
events: Object.assign({}, store.clone(store.DEFAULT_CONFIG.im.events), store.isPlainObject(imBase.events) ? imBase.events : {}),
|
|
706
|
+
targets: Array.isArray(imBase.targets) ? imBase.targets : [],
|
|
707
|
+
});
|
|
708
|
+
// v2.2:智能体工作观测节缺省回填(sources 深合并,防局部 PATCH 抹掉其他源默认开关/路径)
|
|
709
|
+
next.agentActivity = agentActivity.normalizeAgentActivity(next.agentActivity);
|
|
710
|
+
// v2.9.44 learn 节缺省回填(engine 深合并;scope/exclude 数组整体替换,防首次 PATCH 抹掉默认结构)
|
|
711
|
+
const learnBase = store.isPlainObject(next.learn) ? next.learn : {};
|
|
712
|
+
next.learn = Object.assign({}, store.clone(store.DEFAULT_CONFIG.learn), learnBase, {
|
|
713
|
+
scope: Array.isArray(learnBase.scope) && learnBase.scope.length ? learnBase.scope : store.clone(store.DEFAULT_CONFIG.learn.scope),
|
|
714
|
+
exclude: Array.isArray(learnBase.exclude) ? learnBase.exclude : [],
|
|
715
|
+
seeds: Array.isArray(learnBase.seeds) ? learnBase.seeds : store.clone(store.DEFAULT_CONFIG.learn.seeds),
|
|
716
|
+
engine: Object.assign({}, store.clone(store.DEFAULT_CONFIG.learn.engine), store.isPlainObject(learnBase.engine) ? learnBase.engine : {}),
|
|
717
|
+
});
|
|
718
|
+
// 运行期归一化字段回填(磁盘可能是首次写回前的人改值;WP6:port 不再回填——
|
|
719
|
+
// 磁盘无 port 即保持无 port,监听参数不进 config.json)
|
|
720
|
+
if (typeof next.host !== 'string') next.host = config.host;
|
|
721
|
+
return next;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
function registerSettingsRoutes(router, ctx) {
|
|
725
|
+
router.add('GET', '/api/settings', (c) => {
|
|
726
|
+
const view = redactedView(ctx.config);
|
|
727
|
+
// v1.5:dataDir 只读——回显磁盘原值(相对路径),而非主服务运行期归一化的绝对路径
|
|
728
|
+
view.dataDir = diskDirRaw(ctx.configPath, 'dataDir', view.dataDir);
|
|
729
|
+
// v2.9.41 数据备份状态(只读:份数/最近备份/最近恢复);懒加载避免 settings↔backup 初始化耦合
|
|
730
|
+
try {
|
|
731
|
+
view.backup = require('./backup').backupStatus(ctx);
|
|
732
|
+
} catch (e) {
|
|
733
|
+
view.backup = null;
|
|
734
|
+
}
|
|
735
|
+
sendJSON(c.res, 200, view);
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
router.add('PATCH', '/api/settings', (c) => {
|
|
739
|
+
// #10:security 节默认不可经 API 改动,防改弱脱敏边界;唯一例外 trustedHosts
|
|
740
|
+
//(插件态 Host 防线 allowlist 扩展,与脱敏无关)。注意:Host 防线为启动期快照
|
|
741
|
+
//(plugin/adapter.js createHostGuard),trustedHosts 变更需重启生效。
|
|
742
|
+
if (c.body !== null && typeof c.body === 'object' && !Array.isArray(c.body) && c.body.security !== undefined) {
|
|
743
|
+
const s = c.body.security;
|
|
744
|
+
if (!store.isPlainObject(s)) {
|
|
745
|
+
throw new HttpError(400, 'SETTINGS_INVALID', 'security 必须为对象');
|
|
746
|
+
}
|
|
747
|
+
const foreign = Object.keys(s).filter((k) => k !== 'trustedHosts');
|
|
748
|
+
if (foreign.length > 0) {
|
|
749
|
+
throw new HttpError(
|
|
750
|
+
400,
|
|
751
|
+
'SETTINGS_INVALID',
|
|
752
|
+
`security.${foreign[0]} 不可通过 API 修改(仅允许手改 config.json 后重启;仅 trustedHosts 可经 API 修改)`
|
|
753
|
+
);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
const next = applyPatch(ctx.config, c.body, ctx.configPath);
|
|
757
|
+
// v1.5:dataDir/webDir 已从 PATCH 白名单移除——next 里是磁盘原值,视图先取再恢复运行期值
|
|
758
|
+
const view = redactedView(next);
|
|
759
|
+
view.dataDir = diskDirRaw(ctx.configPath, 'dataDir', view.dataDir);
|
|
760
|
+
store.writeJSON(ctx.configPath, next);
|
|
761
|
+
// #3:pollIntervalSec 变更即时重排轮询器(改前取旧值,ctx.config 覆写后再比较)
|
|
762
|
+
const prevInterval = (ctx.config.gitea || {}).pollIntervalSec;
|
|
763
|
+
// #6:运行期 config 的目录保持启动期归一化后的绝对路径(gitea provider 依赖),
|
|
764
|
+
// PATCH 只改其他键,绝不把绝对路径写回磁盘
|
|
765
|
+
next.dataDir = ctx.config.dataDir;
|
|
766
|
+
next.webDir = ctx.config.webDir;
|
|
767
|
+
ctx.config = next;
|
|
768
|
+
const newInterval = (next.gitea || {}).pollIntervalSec;
|
|
769
|
+
if (typeof ctx.reschedulePoller === 'function' && newInterval !== prevInterval) {
|
|
770
|
+
ctx.reschedulePoller(newInterval);
|
|
771
|
+
}
|
|
772
|
+
sendJSON(c.res, 200, view);
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
module.exports = {
|
|
777
|
+
registerSettingsRoutes,
|
|
778
|
+
redactedView,
|
|
779
|
+
deepMerge,
|
|
780
|
+
forbiddenWhitelistEntry,
|
|
781
|
+
trustedHostEntryError,
|
|
782
|
+
baseUrlError,
|
|
783
|
+
};
|