@in-the-loop-labs/pair-review 4.1.0 → 5.0.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 +81 -13
- package/package.json +6 -2
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin-code-critic/.claude-plugin/plugin.json +1 -1
- package/plugin-pair-loop/.claude-plugin/plugin.json +1 -1
- package/plugin-pair-loop/skills/loop/SKILL.md +17 -8
- package/public/css/ai-summary-modal.css +1 -1
- package/public/css/analysis-config.css +33 -49
- package/public/css/council-card.css +118 -0
- package/public/css/council-dropdown.css +164 -0
- package/public/css/pr.css +666 -394
- package/public/css/repo-settings.css +15 -305
- package/public/css/settings.css +1141 -0
- package/public/css/snippet-manager.css +233 -0
- package/public/index.html +40 -59
- package/public/js/components/AIPanel.js +82 -16
- package/public/js/components/ChatPanel.js +401 -5
- package/public/js/components/CouncilCard.js +202 -0
- package/public/js/components/CouncilDropdown.js +292 -0
- package/public/js/components/DiffOptionsDropdown.js +177 -4
- package/public/js/components/SnippetManager.js +463 -0
- package/public/js/index.js +7 -30
- package/public/js/local.js +20 -2
- package/public/js/modules/comment-manager.js +8 -1
- package/public/js/modules/comment-minimizer.js +249 -190
- package/public/js/modules/external-comment-manager.js +414 -48
- package/public/js/modules/file-comment-manager.js +28 -21
- package/public/js/modules/hunk-summary-renderer.js +155 -29
- package/public/js/modules/pierre-bridge.js +2513 -0
- package/public/js/modules/pierre-context.js +345 -0
- package/public/js/modules/suggestion-manager.js +176 -20
- package/public/js/modules/tour-renderer.js +400 -184
- package/public/js/pr.js +1434 -150
- package/public/js/repo-settings.js +60 -284
- package/public/js/settings.js +1086 -0
- package/public/js/theme-bootstrap.js +40 -0
- package/public/js/theme.js +247 -0
- package/public/js/utils/provider-model.js +3 -3
- package/public/js/utils/suggestion-ui.js +157 -8
- package/public/local.html +20 -31
- package/public/pr.html +14 -23
- package/public/repo-settings.html +24 -14
- package/public/settings.html +138 -0
- package/public/setup.html +8 -13
- package/src/ai/codex-provider.js +59 -11
- package/src/chat/api-reference.js +9 -5
- package/src/chat/prompt-builder.js +8 -1
- package/src/config.js +15 -6
- package/src/database.js +307 -14
- package/src/external/github-adapter.js +39 -5
- package/src/headless/delegate.js +682 -0
- package/src/local-review.js +10 -10
- package/src/local-scope.js +9 -0
- package/src/main.js +276 -157
- package/src/mcp-stdio.js +24 -1
- package/src/review-config.js +93 -25
- package/src/routes/chat.js +2 -2
- package/src/routes/config.js +12 -10
- package/src/routes/external-comments.js +5 -1
- package/src/routes/local.js +2 -4
- package/src/routes/mcp.js +18 -13
- package/src/routes/reviews.js +223 -4
- package/src/routes/settings.js +252 -0
- package/src/routes/setup.js +80 -1
- package/src/routes/shared.js +31 -29
- package/src/routes/snippets.js +154 -0
- package/src/routes/stack-analysis.js +17 -14
- package/src/server.js +86 -18
- package/src/settings/global-settings-service.js +472 -0
- package/src/settings/registry.js +624 -0
- package/src/utils/db-identity.js +44 -0
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
// Copyright 2026 Tim Perkins (tjwp) | SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
/**
|
|
3
|
+
* GlobalSettingsService
|
|
4
|
+
*
|
|
5
|
+
* Owns the effective value + source attribution for every global setting, and
|
|
6
|
+
* the read/write of in-app overrides (the `global_settings` table). It is the
|
|
7
|
+
* single authority the /settings API talks to.
|
|
8
|
+
*
|
|
9
|
+
* Precedence (highest first), per the design plan:
|
|
10
|
+
* in-app (DB override) > env/CLI > project config.local > project config >
|
|
11
|
+
* ~/.pair-review config.local > ~/.pair-review config > managed config >
|
|
12
|
+
* built-in default
|
|
13
|
+
*
|
|
14
|
+
* `buildEffectiveConfig()` produces the object stored via `app.set('config')`.
|
|
15
|
+
* It folds DB overrides into the merged file config so every per-request
|
|
16
|
+
* `req.app.get('config')` reader honors them. It does NOT fold env vars — those
|
|
17
|
+
* have always been consumed at their point of use (e.g. the review-config
|
|
18
|
+
* ladder, resolveDbName), and folding them would double-apply.
|
|
19
|
+
*
|
|
20
|
+
* Two config-driven controls layer on top:
|
|
21
|
+
* - `settings_ui.hidden` (a PREFERENCE, read from the merged effective config
|
|
22
|
+
* so a higher layer can un-hide with `[]`): omits entries from the API and
|
|
23
|
+
* rejects both PUT and DELETE. Mixes section ids and setting keys.
|
|
24
|
+
* - `final` (a LOCK, computed as the UNION across all RAW layers so a higher
|
|
25
|
+
* layer cannot un-final): a finalized key ignores the app + env tiers, is
|
|
26
|
+
* excluded from `_globalOverrides`, and rejects PUT (DELETE is allowed —
|
|
27
|
+
* it just removes the ignored DB row). The finalized registry keys are
|
|
28
|
+
* carried on the effective config as `_finalKeys` for point-of-use
|
|
29
|
+
* env-defeat (review-config.js, routes/shared.js getModel).
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
const logger = require('../utils/logger');
|
|
33
|
+
const {
|
|
34
|
+
getRegistry, getSections, getEntry, getPath, hasPath, setPath, validateValue
|
|
35
|
+
} = require('./registry');
|
|
36
|
+
const { GlobalSettingsRepository } = require('../database');
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* File layers below env, ordered highest-precedence first for attribution.
|
|
40
|
+
* `default` is handled as the final fallback (registry default), not scanned
|
|
41
|
+
* here, so a raw layer's missing key falls through to the registry default.
|
|
42
|
+
*/
|
|
43
|
+
const FILE_LAYER_ORDER = ['project.local', 'project', 'config.local', 'config', 'managed'];
|
|
44
|
+
|
|
45
|
+
/** JSON deep clone — config is always JSON-serializable (files + DEFAULT_CONFIG). */
|
|
46
|
+
function deepClone(obj) {
|
|
47
|
+
return obj === undefined ? undefined : JSON.parse(JSON.stringify(obj));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Validate a candidate hidden/final declaration into a clean array of strings.
|
|
52
|
+
* Anything that is not an array is logged and treated as empty; non-string
|
|
53
|
+
* members are dropped. Used for both `settings_ui.hidden` (merged config) and
|
|
54
|
+
* per-layer `final` arrays.
|
|
55
|
+
* @param {*} value
|
|
56
|
+
* @param {string} label - For diagnostics (e.g. 'settings_ui.hidden').
|
|
57
|
+
* @returns {string[]}
|
|
58
|
+
*/
|
|
59
|
+
function toStringList(value, label) {
|
|
60
|
+
if (value === undefined) return [];
|
|
61
|
+
if (!Array.isArray(value)) {
|
|
62
|
+
logger.warn(`GlobalSettingsService: ${label} must be an array of strings; ignoring`);
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
const out = [];
|
|
66
|
+
for (const item of value) {
|
|
67
|
+
if (typeof item === 'string') out.push(item);
|
|
68
|
+
else logger.warn(`GlobalSettingsService: ${label} entry ${JSON.stringify(item)} is not a string; ignoring`);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Property names whose values are secrets and must never be shipped to the
|
|
75
|
+
* client. Matched case-insensitively as a substring of the key, so `token`,
|
|
76
|
+
* `github_token`, `apiKey`, `api_key`, `client_secret`, `password`,
|
|
77
|
+
* `authorization`, and `passphrase` all redact. Read-only object settings
|
|
78
|
+
* (providers / chat_providers / hooks) can embed these under config files.
|
|
79
|
+
*/
|
|
80
|
+
const SECRET_KEY_PATTERN = /(token|secret|password|passphrase|api[-_]?key|apikey|auth|credential|bearer)/i;
|
|
81
|
+
|
|
82
|
+
/** The literal used in place of a redacted secret value in API payloads. */
|
|
83
|
+
const REDACTED = '***redacted***';
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Deep-clone a JSON-serializable value, replacing any property whose key looks
|
|
87
|
+
* like a secret (SECRET_KEY_PATTERN) with a fixed redaction marker. Used before
|
|
88
|
+
* shipping object-type read-only settings to the client so config-file secrets
|
|
89
|
+
* (tokens, API keys) never leave the server. Arrays are walked element-wise;
|
|
90
|
+
* primitives pass through unchanged.
|
|
91
|
+
* @param {*} value
|
|
92
|
+
* @returns {*}
|
|
93
|
+
*/
|
|
94
|
+
function redactSecrets(value) {
|
|
95
|
+
if (Array.isArray(value)) {
|
|
96
|
+
return value.map((item) => redactSecrets(item));
|
|
97
|
+
}
|
|
98
|
+
if (value && typeof value === 'object') {
|
|
99
|
+
const out = {};
|
|
100
|
+
for (const [k, v] of Object.entries(value)) {
|
|
101
|
+
if (SECRET_KEY_PATTERN.test(k)) {
|
|
102
|
+
// Redact regardless of the value's shape — a nested secret object is
|
|
103
|
+
// just as sensitive as a string token.
|
|
104
|
+
out[k] = REDACTED;
|
|
105
|
+
} else {
|
|
106
|
+
out[k] = redactSecrets(v);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Coerce a raw env-var string into the entry's type for display. Env vars are
|
|
116
|
+
* always strings; this is best-effort for the settings page only.
|
|
117
|
+
* @param {Object} entry
|
|
118
|
+
* @param {string} raw
|
|
119
|
+
* @returns {*}
|
|
120
|
+
*/
|
|
121
|
+
function coerceEnvValue(entry, raw) {
|
|
122
|
+
switch (entry.type) {
|
|
123
|
+
case 'boolean': {
|
|
124
|
+
const lower = String(raw).toLowerCase();
|
|
125
|
+
if (lower === 'true' || lower === '1') return true;
|
|
126
|
+
if (lower === 'false' || lower === '0') return false;
|
|
127
|
+
return Boolean(raw);
|
|
128
|
+
}
|
|
129
|
+
case 'integer': {
|
|
130
|
+
const n = Number(raw);
|
|
131
|
+
return Number.isFinite(n) ? n : raw;
|
|
132
|
+
}
|
|
133
|
+
default:
|
|
134
|
+
return raw;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
class GlobalSettingsService {
|
|
139
|
+
/**
|
|
140
|
+
* @param {Object} opts
|
|
141
|
+
* @param {Object} opts.db - Database handle
|
|
142
|
+
* @param {Object} opts.baseConfig - Fully merged file config (post loadConfig,
|
|
143
|
+
* env PORT/single_port already applied). Deep-cloned on construction.
|
|
144
|
+
* @param {Array<{name: string, data: Object}>} opts.layers - Ordered raw
|
|
145
|
+
* config layers (low->high) from loadConfig, for source attribution.
|
|
146
|
+
*/
|
|
147
|
+
constructor({ db, baseConfig, layers }) {
|
|
148
|
+
this.db = db;
|
|
149
|
+
this.repo = new GlobalSettingsRepository(db);
|
|
150
|
+
this.baseConfig = deepClone(baseConfig) || {};
|
|
151
|
+
// Index layers by name for O(1) lookup; keep only known layer names.
|
|
152
|
+
this.layersByName = {};
|
|
153
|
+
for (const layer of (layers || [])) {
|
|
154
|
+
if (layer && layer.name) this.layersByName[layer.name] = layer.data || {};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Config-driven hiding (`settings_ui.hidden`): a personal/org PREFERENCE
|
|
158
|
+
// read from the MERGED effective config (normal deepMerge, arrays replace
|
|
159
|
+
// wholesale — a higher layer can un-hide with `"hidden": []`). Mixes section
|
|
160
|
+
// ids and setting keys. Hidden entries are omitted from the API and reject
|
|
161
|
+
// both PUT and DELETE.
|
|
162
|
+
this.hiddenIds = new Set(
|
|
163
|
+
toStringList(getPath(this.baseConfig, 'settings_ui.hidden'), 'settings_ui.hidden')
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
// Config-driven locking (`final`): a LOCK computed as the UNION across ALL
|
|
167
|
+
// RAW layers (deliberately NOT the deepMerged array — a higher layer cannot
|
|
168
|
+
// un-final; the declaration must be removed where it lives). Mixes section
|
|
169
|
+
// ids and setting keys. A finalized key ignores the app + env tiers.
|
|
170
|
+
this.finalIds = new Set();
|
|
171
|
+
for (const layerName of Object.keys(this.layersByName)) {
|
|
172
|
+
const list = toStringList(this.layersByName[layerName].final, `final (layer "${layerName}")`);
|
|
173
|
+
for (const id of list) this.finalIds.add(id);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Expand finalized section ids / keys to the concrete registry keys they
|
|
177
|
+
// lock, for `_finalKeys` on the effective config (consulted by point-of-use
|
|
178
|
+
// env-defeat in review-config.js / routes/shared.js getModel).
|
|
179
|
+
this.finalKeys = getRegistry()
|
|
180
|
+
.filter((entry) => this._isFinal(entry))
|
|
181
|
+
.map((entry) => entry.key);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Whether a registry entry is hidden by configuration (its key or its group
|
|
186
|
+
* is listed in `settings_ui.hidden`).
|
|
187
|
+
* @param {Object} entry
|
|
188
|
+
* @returns {boolean}
|
|
189
|
+
* @private
|
|
190
|
+
*/
|
|
191
|
+
_isHidden(entry) {
|
|
192
|
+
return this.hiddenIds.has(entry.key) || this.hiddenIds.has(entry.group);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Whether a registry entry is locked as final by configuration (its key or
|
|
197
|
+
* its group appears in the union of layer `final` arrays).
|
|
198
|
+
* @param {Object} entry
|
|
199
|
+
* @returns {boolean}
|
|
200
|
+
* @private
|
|
201
|
+
*/
|
|
202
|
+
_isFinal(entry) {
|
|
203
|
+
return this.finalIds.has(entry.key) || this.finalIds.has(entry.group);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Read the persisted overrides, filtered to editable registry keys with
|
|
208
|
+
* type-valid values. Invalid or unknown rows are ignored (and logged) so a
|
|
209
|
+
* stale/corrupt row can never break resolution.
|
|
210
|
+
* @returns {Object} map of key -> parsed value
|
|
211
|
+
*/
|
|
212
|
+
getOverrides() {
|
|
213
|
+
let raw;
|
|
214
|
+
try {
|
|
215
|
+
raw = this.repo.getAll();
|
|
216
|
+
} catch (error) {
|
|
217
|
+
logger.warn(`GlobalSettingsService: failed to read overrides: ${error.message}`);
|
|
218
|
+
return {};
|
|
219
|
+
}
|
|
220
|
+
const out = {};
|
|
221
|
+
for (const [key, value] of Object.entries(raw || {})) {
|
|
222
|
+
const entry = getEntry(key);
|
|
223
|
+
if (!entry) {
|
|
224
|
+
logger.debug(`GlobalSettingsService: ignoring override for unknown key "${key}"`);
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (!entry.editable) {
|
|
228
|
+
logger.debug(`GlobalSettingsService: ignoring override for non-editable key "${key}"`);
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (this._isFinal(entry)) {
|
|
232
|
+
// The key is locked as final by configuration. The row is ignored (so
|
|
233
|
+
// it never affects resolution) but NOT deleted — un-finalizing later
|
|
234
|
+
// restores it. clearOverride() can still remove it explicitly.
|
|
235
|
+
logger.warn(`GlobalSettingsService: ignoring in-app override for "${key}" — locked as final by configuration`);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const { valid, error } = validateValue(entry, value);
|
|
239
|
+
if (!valid) {
|
|
240
|
+
logger.warn(`GlobalSettingsService: ignoring invalid override for "${key}": ${error}`);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
out[key] = value;
|
|
244
|
+
}
|
|
245
|
+
return out;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Build the effective config object: a clone of the merged file config with
|
|
250
|
+
* in-app overrides applied by dot-path. Carries a `_globalOverrides` map of
|
|
251
|
+
* the applied overrides so the review-config ladder can rank an in-app
|
|
252
|
+
* default_provider/default_model override above env vars (see
|
|
253
|
+
* src/review-config.js). `_globalOverrides` is read by key, never serialized
|
|
254
|
+
* wholesale by any route.
|
|
255
|
+
*
|
|
256
|
+
* ORDERING CONTRACT — read before adding a new call site.
|
|
257
|
+
* Every entry point (src/server.js, src/main.js, src/mcp-stdio.js) MUST fold
|
|
258
|
+
* this overlay into its config object IMMEDIATELY after opening the database
|
|
259
|
+
* and BEFORE any consumer that latches a config value into module-level or
|
|
260
|
+
* closure-captured state. Applying it late means an in-app override silently
|
|
261
|
+
* never takes effect, even after the restart its `restartRequired` badge
|
|
262
|
+
* promises. Known latching consumers:
|
|
263
|
+
* - `applyConfigOverrides(config)` (src/ai) snapshots `config.yolo` into the
|
|
264
|
+
* module-level `yoloMode` in the provider runtime (src/ai/provider.js).
|
|
265
|
+
* - `logger.setStreamDebugEnabled(config.debug_stream)` flips a logger flag.
|
|
266
|
+
* - The `devMode = config.dev_mode === true` const in src/server.js is
|
|
267
|
+
* captured by the static-file `setHeaders` closure.
|
|
268
|
+
* - `warnIfDevModeWithoutDbName(config)` reads `config.dev_mode`.
|
|
269
|
+
* - The MCP `start_analysis` tool reads `config._globalOverrides` for its
|
|
270
|
+
* provider/model ladder (src/routes/mcp.js) — a config passed without this
|
|
271
|
+
* overlay resolves as if no in-app override existed.
|
|
272
|
+
* `buildEffectiveConfig` deep-clones its base internally, so reordering a
|
|
273
|
+
* caller to fold it earlier is always safe.
|
|
274
|
+
*
|
|
275
|
+
* @param {Object} [overrides] - Pre-read overrides (avoids a second DB read)
|
|
276
|
+
* @returns {Object}
|
|
277
|
+
*/
|
|
278
|
+
buildEffectiveConfig(overrides = this.getOverrides()) {
|
|
279
|
+
const effective = deepClone(this.baseConfig) || {};
|
|
280
|
+
// `getOverrides()` already excludes finalized keys, so neither the applied
|
|
281
|
+
// dot-path sets nor `_globalOverrides` can carry a locked key.
|
|
282
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
283
|
+
setPath(effective, key, value);
|
|
284
|
+
}
|
|
285
|
+
effective._globalOverrides = { ...overrides };
|
|
286
|
+
// Finalized registry keys, for point-of-use consumers that read env/CLI
|
|
287
|
+
// directly (review-config ladder, routes/shared.js getModel) and must skip
|
|
288
|
+
// env for a locked key.
|
|
289
|
+
effective._finalKeys = [...this.finalKeys];
|
|
290
|
+
return effective;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Resolve a single setting's effective value and source.
|
|
295
|
+
* @param {string} key
|
|
296
|
+
* @param {Object} [overrides] - Pre-read overrides (avoids a DB read per key)
|
|
297
|
+
* @returns {{ value: *, source: string }|null} null when key is unknown
|
|
298
|
+
*/
|
|
299
|
+
resolve(key, overrides = this.getOverrides()) {
|
|
300
|
+
const entry = getEntry(key);
|
|
301
|
+
if (!entry) return null;
|
|
302
|
+
|
|
303
|
+
// A finalized key ignores the app AND env tiers: the value comes from the
|
|
304
|
+
// highest file layer that defines it, else the built-in default. (The row,
|
|
305
|
+
// if any, was already dropped from `overrides` by getOverrides().)
|
|
306
|
+
const isFinal = this._isFinal(entry);
|
|
307
|
+
|
|
308
|
+
// 1. In-app DB override (editable keys only; never for final keys).
|
|
309
|
+
if (!isFinal && entry.editable && Object.prototype.hasOwnProperty.call(overrides, key)) {
|
|
310
|
+
return { value: overrides[key], source: 'app' };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// 2. Env var (registry-declared; skipped for final keys). Empty string counts as unset.
|
|
314
|
+
if (!isFinal && entry.envVar) {
|
|
315
|
+
const raw = process.env[entry.envVar];
|
|
316
|
+
if (raw !== undefined && raw !== '') {
|
|
317
|
+
return { value: coerceEnvValue(entry, raw), source: 'env' };
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// 3. File layers, highest precedence first (own-property presence, not
|
|
322
|
+
// truthiness, so false/0/'' still attribute correctly).
|
|
323
|
+
for (const layerName of FILE_LAYER_ORDER) {
|
|
324
|
+
const data = this.layersByName[layerName];
|
|
325
|
+
if (data && hasPath(data, key)) {
|
|
326
|
+
return { value: getPath(data, key), source: layerName };
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// 4. Built-in default.
|
|
331
|
+
return { value: entry.default, source: 'default' };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Build the API descriptor array for every VISIBLE registry entry. Entries
|
|
336
|
+
* hidden by `settings_ui.hidden` (by key or by group) are omitted entirely.
|
|
337
|
+
* @returns {Array<Object>}
|
|
338
|
+
*/
|
|
339
|
+
describe() {
|
|
340
|
+
const overrides = this.getOverrides();
|
|
341
|
+
const effective = this.buildEffectiveConfig(overrides);
|
|
342
|
+
return getRegistry()
|
|
343
|
+
.filter((entry) => !this._isHidden(entry))
|
|
344
|
+
.map((entry) => this._describeEntry(entry, overrides, effective));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Build the ordered section metadata for the API, omitting any section that
|
|
349
|
+
* has zero visible settings. Titles/descriptions/badges come from the
|
|
350
|
+
* registry SECTIONS catalog.
|
|
351
|
+
* @param {Array<Object>} [settings] - Pre-computed descriptors (from describe())
|
|
352
|
+
* @returns {Array<{id: string, title: string, description: string|null, badge: string|null}>}
|
|
353
|
+
*/
|
|
354
|
+
describeSections(settings = this.describe()) {
|
|
355
|
+
const present = new Set((settings || []).map((s) => s.group));
|
|
356
|
+
return getSections()
|
|
357
|
+
.filter((section) => present.has(section.id))
|
|
358
|
+
.map((section) => ({
|
|
359
|
+
id: section.id,
|
|
360
|
+
title: section.title,
|
|
361
|
+
description: section.description || null,
|
|
362
|
+
badge: section.badge || null,
|
|
363
|
+
// Build-time visibility default (registry SECTIONS `hidden: true`). The
|
|
364
|
+
// settings page renderer omits hidden sections; kept separate from the
|
|
365
|
+
// config-driven `settings_ui.hidden` preference.
|
|
366
|
+
hidden: Boolean(section.hidden)
|
|
367
|
+
}));
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Describe a single registry entry (metadata + effective value + source).
|
|
372
|
+
* @private
|
|
373
|
+
*/
|
|
374
|
+
_describeEntry(entry, overrides, effective) {
|
|
375
|
+
const resolved = this.resolve(entry.key, overrides);
|
|
376
|
+
const descriptor = {
|
|
377
|
+
key: entry.key,
|
|
378
|
+
label: entry.label,
|
|
379
|
+
description: entry.description,
|
|
380
|
+
group: entry.group,
|
|
381
|
+
type: entry.type,
|
|
382
|
+
values: entry.values || null,
|
|
383
|
+
default: entry.default,
|
|
384
|
+
editable: Boolean(entry.editable),
|
|
385
|
+
restartRequired: Boolean(entry.restartRequired),
|
|
386
|
+
sensitive: Boolean(entry.sensitive),
|
|
387
|
+
badge: entry.badge || null,
|
|
388
|
+
final: this._isFinal(entry),
|
|
389
|
+
source: resolved ? resolved.source : 'default',
|
|
390
|
+
overrideValue: Object.prototype.hasOwnProperty.call(overrides, entry.key)
|
|
391
|
+
? overrides[entry.key]
|
|
392
|
+
: null
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
if (entry.sensitive) {
|
|
396
|
+
// Never leak the secret; report only whether one is configured. Consider
|
|
397
|
+
// the env var, the literal value, and the token command all as "set".
|
|
398
|
+
const envSet = entry.envVar && process.env[entry.envVar];
|
|
399
|
+
const literalSet = getPath(effective, entry.key);
|
|
400
|
+
const commandSet = entry.key === 'github_token' ? getPath(effective, 'github_token_command') : null;
|
|
401
|
+
descriptor.value = null;
|
|
402
|
+
descriptor.configured = Boolean(envSet || literalSet || commandSet);
|
|
403
|
+
} else if (entry.type === 'object') {
|
|
404
|
+
// Ship the FULL object (not just a count) so the page can render its
|
|
405
|
+
// contents inline — but redact any embedded secrets first. providers /
|
|
406
|
+
// chat_providers / hooks can carry tokens/API keys from config files;
|
|
407
|
+
// those must never reach the client.
|
|
408
|
+
const obj = getPath(effective, entry.key);
|
|
409
|
+
const isPlainObject = obj && typeof obj === 'object' && !Array.isArray(obj);
|
|
410
|
+
descriptor.value = isPlainObject ? redactSecrets(obj) : {};
|
|
411
|
+
} else {
|
|
412
|
+
descriptor.value = resolved ? resolved.value : entry.default;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return descriptor;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Validate + persist an in-app override, returning the fresh descriptor and
|
|
420
|
+
* the recomputed effective config for the caller to `app.set('config')`.
|
|
421
|
+
* @param {string} key
|
|
422
|
+
* @param {*} value
|
|
423
|
+
* @returns {{ ok: boolean, status?: number, error?: string, setting?: Object, effectiveConfig?: Object }}
|
|
424
|
+
*/
|
|
425
|
+
async setOverride(key, value) {
|
|
426
|
+
const entry = getEntry(key);
|
|
427
|
+
if (!entry) return { ok: false, status: 400, error: `Unknown setting "${key}"` };
|
|
428
|
+
// Hidden blocks both PUT and DELETE; final blocks PUT only. Check hidden
|
|
429
|
+
// first so a key that is both reports the preference-level reason.
|
|
430
|
+
if (this._isHidden(entry)) return { ok: false, status: 400, error: 'hidden by configuration' };
|
|
431
|
+
if (this._isFinal(entry)) return { ok: false, status: 400, error: 'locked as final by configuration' };
|
|
432
|
+
if (!entry.editable) return { ok: false, status: 400, error: `Setting "${key}" is read-only` };
|
|
433
|
+
const { valid, error } = validateValue(entry, value);
|
|
434
|
+
if (!valid) return { ok: false, status: 400, error };
|
|
435
|
+
|
|
436
|
+
// An empty string on a key with a non-empty default can never win: every
|
|
437
|
+
// consumer resolves these via `||` chains, so env/files would still apply
|
|
438
|
+
// while the source badge claimed "in-app". Treat it as clearing the
|
|
439
|
+
// override instead. Keys whose default IS '' keep '' as a real value
|
|
440
|
+
// ("inherit" semantics for provider/model sub-keys).
|
|
441
|
+
if (entry.type === 'string' && value === '' && entry.default !== '') {
|
|
442
|
+
return this.clearOverride(key);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
await this.repo.set(key, value);
|
|
446
|
+
const overrides = this.getOverrides();
|
|
447
|
+
const effectiveConfig = this.buildEffectiveConfig(overrides);
|
|
448
|
+
const setting = this._describeEntry(entry, overrides, effectiveConfig);
|
|
449
|
+
return { ok: true, setting, effectiveConfig };
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Clear an in-app override (idempotent — clearing an unset key still succeeds).
|
|
454
|
+
* @param {string} key
|
|
455
|
+
* @returns {{ ok: boolean, status?: number, error?: string, setting?: Object, effectiveConfig?: Object }}
|
|
456
|
+
*/
|
|
457
|
+
async clearOverride(key) {
|
|
458
|
+
const entry = getEntry(key);
|
|
459
|
+
if (!entry) return { ok: false, status: 400, error: `Unknown setting "${key}"` };
|
|
460
|
+
// Hidden blocks DELETE too. Final does NOT: clearing a finalized key removes
|
|
461
|
+
// the ignored DB row (no effective-value change), which is allowed.
|
|
462
|
+
if (this._isHidden(entry)) return { ok: false, status: 400, error: 'hidden by configuration' };
|
|
463
|
+
|
|
464
|
+
await this.repo.delete(key);
|
|
465
|
+
const overrides = this.getOverrides();
|
|
466
|
+
const effectiveConfig = this.buildEffectiveConfig(overrides);
|
|
467
|
+
const setting = this._describeEntry(entry, overrides, effectiveConfig);
|
|
468
|
+
return { ok: true, setting, effectiveConfig };
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
module.exports = { GlobalSettingsService, coerceEnvValue, redactSecrets, FILE_LAYER_ORDER };
|