opencode-rag-plugin 2.12.2 → 2.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ReadMe.md +4 -2
- package/dist/cli/browser.d.ts +20 -0
- package/dist/cli/browser.js +49 -0
- package/dist/cli/commands/setup.js +11 -0
- package/dist/cli/commands/ui.js +3 -11
- package/dist/cli/setup-ui.d.ts +38 -0
- package/dist/cli/setup-ui.js +133 -0
- package/dist/cli/types.d.ts +5 -0
- package/dist/core/config.d.ts +7 -0
- package/dist/core/config.js +4 -0
- package/dist/plugin.js +21 -1
- package/dist/retriever/keyword-index.d.ts +58 -16
- package/dist/retriever/keyword-index.js +457 -159
- package/dist/web/api.d.ts +8 -1
- package/dist/web/api.js +28 -14
- package/dist/web/config-api.d.ts +60 -0
- package/dist/web/config-api.js +614 -0
- package/dist/web/config-schema.d.ts +88 -0
- package/dist/web/config-schema.js +821 -0
- package/dist/web/server.js +14 -4
- package/dist/web/ui/assets/{ScatterPlot3D-XjXIwavM.js → ScatterPlot3D-CJpVHNv-.js} +1 -1
- package/dist/web/ui/assets/index-BitHPGOk.css +2 -0
- package/dist/web/ui/assets/index-DDZPYJ4D.js +4 -0
- package/dist/web/ui/index.html +20 -3
- package/package.json +1 -1
- package/dist/web/ui/assets/index-B-RH6fko.css +0 -2
- package/dist/web/ui/assets/index-FFPRbgLx.js +0 -4
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Layered configuration API for the Web UI.
|
|
3
|
+
*
|
|
4
|
+
* Exposes the project layer (`.opencode/opencode-rag.json`), the machine-global
|
|
5
|
+
* layer (`~/.config/opencode/opencode-rag.json`), the runtime overrides file,
|
|
6
|
+
* and the effective merged config — plus a validated, atomic write path.
|
|
7
|
+
*
|
|
8
|
+
* Design rules:
|
|
9
|
+
* - Reads always come from disk; the cached in-memory config is only a mirror.
|
|
10
|
+
* - Secrets never leave the server: layer payloads omit them and report
|
|
11
|
+
* `secretSet` booleans instead.
|
|
12
|
+
* - A patch is validated in full before the first byte is written and is
|
|
13
|
+
* applied as one atomic file replacement.
|
|
14
|
+
* - Writing a path clears the matching runtime override, which would otherwise
|
|
15
|
+
* silently shadow the new value.
|
|
16
|
+
*/
|
|
17
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { dirname } from "node:path";
|
|
19
|
+
import { DEFAULT_CONFIG, findConfigFile, globalConfigPath as defaultGlobalConfigPath, loadConfigLayers, projectConfigPath as defaultProjectConfigPath, } from "../core/config.js";
|
|
20
|
+
import { applyRuntimeOverrides, deleteRuntimeOverride, loadRuntimeOverrides, } from "../core/runtime-overrides.js";
|
|
21
|
+
import { SETTINGS_SECTIONS, cloneConfigValue, collectConfigLeafPaths, dottedPath, findSettingField, isPlainConfigObject, isSafeConfigPath, isSecretConfigPath, redactConfigSecrets, } from "./config-schema.js";
|
|
22
|
+
/**
|
|
23
|
+
* Resolve the workspace config file to read and write.
|
|
24
|
+
*
|
|
25
|
+
* An explicit `--config` path (anything that is not the default canonical
|
|
26
|
+
* location) stays authoritative. Otherwise prefer the canonical
|
|
27
|
+
* `.opencode/opencode-rag.json`, then fall back to a legacy root file so an
|
|
28
|
+
* edit keeps that file authoritative instead of creating a second config.
|
|
29
|
+
*/
|
|
30
|
+
export function resolveProjectReadPath(workspaceDir, canonical) {
|
|
31
|
+
if (canonical !== defaultProjectConfigPath(workspaceDir))
|
|
32
|
+
return canonical;
|
|
33
|
+
if (existsSync(canonical))
|
|
34
|
+
return canonical;
|
|
35
|
+
return findConfigFile(workspaceDir) ?? canonical;
|
|
36
|
+
}
|
|
37
|
+
/** Read the mtime of a file, or 0 when it does not exist. */
|
|
38
|
+
function fileMtime(filePath) {
|
|
39
|
+
try {
|
|
40
|
+
return statSync(filePath).mtimeMs;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/** Read a JSON object from disk without throwing on a missing file. */
|
|
47
|
+
function readJsonObject(filePath) {
|
|
48
|
+
const raw = readFileSync(filePath, "utf-8");
|
|
49
|
+
const stripped = raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw;
|
|
50
|
+
const parsed = JSON.parse(stripped);
|
|
51
|
+
if (!isPlainConfigObject(parsed)) {
|
|
52
|
+
throw new Error("expected a JSON object at the top level");
|
|
53
|
+
}
|
|
54
|
+
return parsed;
|
|
55
|
+
}
|
|
56
|
+
/** Read one layer, turning parse errors into an `error` field instead of throwing. */
|
|
57
|
+
function readLayer(filePath) {
|
|
58
|
+
if (!existsSync(filePath)) {
|
|
59
|
+
return {
|
|
60
|
+
path: filePath,
|
|
61
|
+
exists: false,
|
|
62
|
+
mtimeMs: 0,
|
|
63
|
+
values: {},
|
|
64
|
+
secretSet: {},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const values = readJsonObject(filePath);
|
|
69
|
+
const sanitized = sanitizeLayer(values);
|
|
70
|
+
return {
|
|
71
|
+
path: filePath,
|
|
72
|
+
exists: true,
|
|
73
|
+
mtimeMs: fileMtime(filePath),
|
|
74
|
+
values: sanitized.values,
|
|
75
|
+
secretSet: sanitized.secretSet,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
return {
|
|
80
|
+
path: filePath,
|
|
81
|
+
exists: true,
|
|
82
|
+
mtimeMs: fileMtime(filePath),
|
|
83
|
+
values: {},
|
|
84
|
+
secretSet: {},
|
|
85
|
+
error: err.message,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** Remove secret values from a raw layer and record which paths are populated. */
|
|
90
|
+
function sanitizeLayer(raw) {
|
|
91
|
+
const values = cloneConfigValue(raw);
|
|
92
|
+
const secretSet = {};
|
|
93
|
+
for (const section of SETTINGS_SECTIONS) {
|
|
94
|
+
for (const entry of section.fields) {
|
|
95
|
+
if (!entry.secret)
|
|
96
|
+
continue;
|
|
97
|
+
const current = getPath(values, entry.path);
|
|
98
|
+
if (typeof current === "string" && current.trim().length > 0) {
|
|
99
|
+
secretSet[dottedPath(entry.path)] = true;
|
|
100
|
+
}
|
|
101
|
+
if (current !== undefined)
|
|
102
|
+
deletePath(values, entry.path);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Unknown credential-named leaves are removed entirely (no sentinel).
|
|
106
|
+
redactConfigSecrets(values);
|
|
107
|
+
return { values, secretSet };
|
|
108
|
+
}
|
|
109
|
+
/** Read a nested value by path. */
|
|
110
|
+
function getPath(root, path) {
|
|
111
|
+
let cursor = root;
|
|
112
|
+
for (const segment of path) {
|
|
113
|
+
if (!isPlainConfigObject(cursor))
|
|
114
|
+
return undefined;
|
|
115
|
+
cursor = cursor[segment];
|
|
116
|
+
}
|
|
117
|
+
return cursor;
|
|
118
|
+
}
|
|
119
|
+
/** True when a path exists as an own property chain. */
|
|
120
|
+
function hasPath(root, path) {
|
|
121
|
+
let cursor = root;
|
|
122
|
+
for (const segment of path) {
|
|
123
|
+
if (!isPlainConfigObject(cursor) || !Object.hasOwn(cursor, segment))
|
|
124
|
+
return false;
|
|
125
|
+
cursor = cursor[segment];
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
/** Set a nested value, creating intermediate objects. */
|
|
130
|
+
function setPath(root, path, value) {
|
|
131
|
+
let cursor = root;
|
|
132
|
+
for (const segment of path.slice(0, -1)) {
|
|
133
|
+
const next = cursor[segment];
|
|
134
|
+
if (!isPlainConfigObject(next)) {
|
|
135
|
+
cursor[segment] = {};
|
|
136
|
+
}
|
|
137
|
+
cursor = cursor[segment];
|
|
138
|
+
}
|
|
139
|
+
const leaf = path[path.length - 1];
|
|
140
|
+
if (leaf !== undefined)
|
|
141
|
+
cursor[leaf] = value;
|
|
142
|
+
}
|
|
143
|
+
/** Delete a nested value, pruning containers that become empty. */
|
|
144
|
+
function deletePath(root, path) {
|
|
145
|
+
const chain = [];
|
|
146
|
+
let cursor = root;
|
|
147
|
+
for (const segment of path.slice(0, -1)) {
|
|
148
|
+
const next = cursor[segment];
|
|
149
|
+
if (!isPlainConfigObject(next))
|
|
150
|
+
return;
|
|
151
|
+
chain.push(cursor);
|
|
152
|
+
cursor = next;
|
|
153
|
+
}
|
|
154
|
+
const leaf = path[path.length - 1];
|
|
155
|
+
if (leaf === undefined || !Object.hasOwn(cursor, leaf))
|
|
156
|
+
return;
|
|
157
|
+
delete cursor[leaf];
|
|
158
|
+
for (let i = chain.length - 1; i >= 0; i -= 1) {
|
|
159
|
+
const parent = chain[i];
|
|
160
|
+
const childKey = path[i];
|
|
161
|
+
if (!parent || childKey === undefined)
|
|
162
|
+
break;
|
|
163
|
+
const child = parent[childKey];
|
|
164
|
+
if (isPlainConfigObject(child) && Object.keys(child).length === 0) {
|
|
165
|
+
delete parent[childKey];
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Write a layer atomically (temp file + rename) with a one-generation backup. */
|
|
173
|
+
function writeLayerFile(filePath, value) {
|
|
174
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
175
|
+
if (existsSync(filePath)) {
|
|
176
|
+
try {
|
|
177
|
+
writeFileSync(`${filePath}.bak`, readFileSync(filePath));
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
// A missing backup must not block the write.
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const tmpPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
184
|
+
writeFileSync(tmpPath, `${JSON.stringify(value, null, 2)}\n`, "utf-8");
|
|
185
|
+
renameSync(tmpPath, filePath);
|
|
186
|
+
}
|
|
187
|
+
/** Build the effective config from the two file layers plus runtime overrides. */
|
|
188
|
+
function buildEffective(globalPath, projectPath, overrides) {
|
|
189
|
+
// Skip a malformed layer instead of throwing away the healthy one — the
|
|
190
|
+
// layer is already reported through `warnings`.
|
|
191
|
+
const layers = [];
|
|
192
|
+
for (const filePath of [globalPath, projectPath]) {
|
|
193
|
+
if (!existsSync(filePath))
|
|
194
|
+
continue;
|
|
195
|
+
try {
|
|
196
|
+
readJsonObject(filePath);
|
|
197
|
+
layers.push(filePath);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
// reported as invalid-json in layerWarnings()
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
let cfg;
|
|
204
|
+
try {
|
|
205
|
+
cfg = layers.length === 0 ? structuredClone(DEFAULT_CONFIG) : loadConfigLayers(layers);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
cfg = structuredClone(DEFAULT_CONFIG);
|
|
209
|
+
}
|
|
210
|
+
return applyRuntimeOverrides(cfg, overrides);
|
|
211
|
+
}
|
|
212
|
+
/** Fields whose scope is machine-global, as dotted paths. */
|
|
213
|
+
const GLOBAL_SCOPE_PATHS = SETTINGS_SECTIONS.flatMap((section) => section.fields.filter((entry) => entry.scope === "global").map((entry) => entry.path));
|
|
214
|
+
/** Known top-level config sections (from the schema). */
|
|
215
|
+
const KNOWN_SECTIONS = new Set(SETTINGS_SECTIONS.map((section) => section.id));
|
|
216
|
+
/** Project keys that shadow the machine-global layer (secrets included). */
|
|
217
|
+
function findShadowedProviderPaths(project) {
|
|
218
|
+
return GLOBAL_SCOPE_PATHS.filter((path) => hasPath(project.values, path) || project.secretSet[dottedPath(path)] === true);
|
|
219
|
+
}
|
|
220
|
+
/** Warnings that both GET and PATCH surface. */
|
|
221
|
+
function layerWarnings(project, global) {
|
|
222
|
+
const warnings = [];
|
|
223
|
+
if (global.error) {
|
|
224
|
+
warnings.push({
|
|
225
|
+
code: "invalid-json",
|
|
226
|
+
message: `The global config is not valid JSON: ${global.error}`,
|
|
227
|
+
paths: [global.path],
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (project.error) {
|
|
231
|
+
warnings.push({
|
|
232
|
+
code: "invalid-json",
|
|
233
|
+
message: `The project config is not valid JSON: ${project.error}`,
|
|
234
|
+
paths: [project.path],
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
const shadowed = findShadowedProviderPaths(project);
|
|
238
|
+
if (shadowed.length > 0) {
|
|
239
|
+
warnings.push({
|
|
240
|
+
code: "provider-shadowed",
|
|
241
|
+
message: `${shadowed.length} machine-global provider setting(s) are duplicated in the project file and override the global layer.`,
|
|
242
|
+
paths: shadowed.map(dottedPath),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
for (const [layerName, layer] of [["project", project], ["global", global]]) {
|
|
246
|
+
const unknown = Object.keys(layer.values).filter((key) => !KNOWN_SECTIONS.has(key));
|
|
247
|
+
if (unknown.length > 0) {
|
|
248
|
+
warnings.push({
|
|
249
|
+
code: "unknown-sections",
|
|
250
|
+
message: `${layerName} config contains unknown section(s): ${unknown.join(", ")}`,
|
|
251
|
+
paths: unknown,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return warnings;
|
|
256
|
+
}
|
|
257
|
+
/** Provenance map for every schema field. */
|
|
258
|
+
function buildProvenance(project, global, overrides) {
|
|
259
|
+
const provenance = {};
|
|
260
|
+
for (const section of SETTINGS_SECTIONS) {
|
|
261
|
+
for (const entry of section.fields) {
|
|
262
|
+
const key = dottedPath(entry.path);
|
|
263
|
+
if (hasPath(overrides, entry.path))
|
|
264
|
+
provenance[key] = "runtime";
|
|
265
|
+
else if (hasPath(project.values, entry.path))
|
|
266
|
+
provenance[key] = "project";
|
|
267
|
+
else if (hasPath(global.values, entry.path))
|
|
268
|
+
provenance[key] = "global";
|
|
269
|
+
else
|
|
270
|
+
provenance[key] = "default";
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return provenance;
|
|
274
|
+
}
|
|
275
|
+
/** Load a full snapshot of schema, layers, effective config, and metadata. */
|
|
276
|
+
export function readConfigSnapshot(ctx) {
|
|
277
|
+
const projectReadPath = resolveProjectReadPath(ctx.workspaceDir, ctx.projectConfigPath);
|
|
278
|
+
const project = readLayer(projectReadPath);
|
|
279
|
+
const global = readLayer(ctx.globalConfigPath);
|
|
280
|
+
const overrides = loadRuntimeOverrides(ctx.storePath);
|
|
281
|
+
const effective = buildEffective(global.path, project.path, overrides);
|
|
282
|
+
const redactedEffective = cloneConfigValue(effective);
|
|
283
|
+
redactConfigSecrets(redactedEffective);
|
|
284
|
+
const warnings = layerWarnings(project, global);
|
|
285
|
+
return {
|
|
286
|
+
config: redactedEffective,
|
|
287
|
+
schema: { version: 1, sections: SETTINGS_SECTIONS },
|
|
288
|
+
layers: {
|
|
289
|
+
global: {
|
|
290
|
+
path: global.path,
|
|
291
|
+
exists: global.exists,
|
|
292
|
+
mtimeMs: global.mtimeMs,
|
|
293
|
+
values: global.values,
|
|
294
|
+
secretSet: global.secretSet,
|
|
295
|
+
...(global.error ? { error: global.error } : {}),
|
|
296
|
+
},
|
|
297
|
+
project: {
|
|
298
|
+
path: project.path,
|
|
299
|
+
writePath: ctx.projectConfigPath,
|
|
300
|
+
exists: project.exists,
|
|
301
|
+
mtimeMs: project.mtimeMs,
|
|
302
|
+
values: project.values,
|
|
303
|
+
secretSet: project.secretSet,
|
|
304
|
+
...(project.error ? { error: project.error } : {}),
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
runtimeOverrides: {
|
|
308
|
+
path: `${ctx.storePath}/runtime-overrides.json`,
|
|
309
|
+
values: overrides,
|
|
310
|
+
},
|
|
311
|
+
provenance: buildProvenance(project, global, overrides),
|
|
312
|
+
warnings,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/** GET /api/config — schema + layers + effective config. */
|
|
316
|
+
export function handleConfigLayers(ctx) {
|
|
317
|
+
return { status: 200, body: readConfigSnapshot(ctx) };
|
|
318
|
+
}
|
|
319
|
+
/** Validate a field value; returns an error message or undefined. */
|
|
320
|
+
function validateFieldValue(field, value) {
|
|
321
|
+
switch (field.type) {
|
|
322
|
+
case "boolean":
|
|
323
|
+
return typeof value === "boolean" ? undefined : "Must be true or false";
|
|
324
|
+
case "number": {
|
|
325
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
326
|
+
return "Must be a number";
|
|
327
|
+
if (field.minimum !== undefined && value < field.minimum) {
|
|
328
|
+
return `Must be at least ${field.minimum}`;
|
|
329
|
+
}
|
|
330
|
+
if (field.maximum !== undefined && value > field.maximum) {
|
|
331
|
+
return `Must be at most ${field.maximum}`;
|
|
332
|
+
}
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
case "string":
|
|
336
|
+
if (typeof value !== "string")
|
|
337
|
+
return "Must be text";
|
|
338
|
+
if (value.length > 100_000)
|
|
339
|
+
return "Text is too long";
|
|
340
|
+
return undefined;
|
|
341
|
+
case "enum":
|
|
342
|
+
if (typeof value !== "string")
|
|
343
|
+
return "Must be text";
|
|
344
|
+
if (field.enumValues && !field.enumValues.includes(value)) {
|
|
345
|
+
return `Must be one of: ${field.enumValues.join(", ")}`;
|
|
346
|
+
}
|
|
347
|
+
return undefined;
|
|
348
|
+
case "string[]":
|
|
349
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string")) {
|
|
350
|
+
return "Must be a list of strings";
|
|
351
|
+
}
|
|
352
|
+
return undefined;
|
|
353
|
+
case "stringMap":
|
|
354
|
+
if (!isPlainConfigObject(value))
|
|
355
|
+
return "Must be an object mapping names to strings";
|
|
356
|
+
if (Object.values(value).some((entry) => typeof entry !== "string")) {
|
|
357
|
+
return "Every value must be a string";
|
|
358
|
+
}
|
|
359
|
+
return undefined;
|
|
360
|
+
case "stringArrayMap":
|
|
361
|
+
if (!isPlainConfigObject(value))
|
|
362
|
+
return "Must be an object mapping names to lists";
|
|
363
|
+
if (Object.values(value).some((entry) => !Array.isArray(entry) || entry.some((item) => typeof item !== "string"))) {
|
|
364
|
+
return "Every value must be a list of strings";
|
|
365
|
+
}
|
|
366
|
+
return undefined;
|
|
367
|
+
case "json":
|
|
368
|
+
return undefined;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/** Parse and fully validate the PATCH body before any write. */
|
|
372
|
+
function parsePatchBody(body) {
|
|
373
|
+
if (!isPlainConfigObject(body)) {
|
|
374
|
+
return { ok: false, response: { status: 400, body: { error: "Invalid config patch" } } };
|
|
375
|
+
}
|
|
376
|
+
const layer = body.layer;
|
|
377
|
+
if (layer !== "project" && layer !== "global" && layer !== "runtime") {
|
|
378
|
+
return {
|
|
379
|
+
ok: false,
|
|
380
|
+
response: { status: 400, body: { error: "layer must be 'project', 'global', or 'runtime'" } },
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
const rawOperations = body.operations;
|
|
384
|
+
if (!Array.isArray(rawOperations) || rawOperations.length === 0) {
|
|
385
|
+
return {
|
|
386
|
+
ok: false,
|
|
387
|
+
response: { status: 400, body: { error: "operations must be a non-empty array" } },
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
if (rawOperations.length > 200) {
|
|
391
|
+
return {
|
|
392
|
+
ok: false,
|
|
393
|
+
response: { status: 400, body: { error: "At most 200 operations per request" } },
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
const fieldErrors = {};
|
|
397
|
+
const operations = [];
|
|
398
|
+
for (const raw of rawOperations) {
|
|
399
|
+
if (!isPlainConfigObject(raw)) {
|
|
400
|
+
return { ok: false, response: { status: 400, body: { error: "Invalid operation" } } };
|
|
401
|
+
}
|
|
402
|
+
const op = raw.op;
|
|
403
|
+
const path = raw.path;
|
|
404
|
+
if (op !== "set" && op !== "unset") {
|
|
405
|
+
return { ok: false, response: { status: 400, body: { error: "op must be 'set' or 'unset'" } } };
|
|
406
|
+
}
|
|
407
|
+
if (!Array.isArray(path) || !path.every((segment) => typeof segment === "string") || !isSafeConfigPath(path)) {
|
|
408
|
+
return { ok: false, response: { status: 400, body: { error: "Invalid path" } } };
|
|
409
|
+
}
|
|
410
|
+
const key = dottedPath(path);
|
|
411
|
+
if (layer === "runtime") {
|
|
412
|
+
if (op !== "unset") {
|
|
413
|
+
return {
|
|
414
|
+
ok: false,
|
|
415
|
+
response: { status: 400, body: { error: "The runtime layer only supports 'unset'" } },
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
operations.push({ op, path: path, ...(raw.value !== undefined ? { value: raw.value } : {}) });
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
const field = findSettingField(path);
|
|
422
|
+
if (!field) {
|
|
423
|
+
fieldErrors[key] = "Unknown setting";
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (field.scope === "global" && layer === "project") {
|
|
427
|
+
fieldErrors[key] = "This setting can only be written to the machine-global config";
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
if (op === "unset") {
|
|
431
|
+
operations.push({ op, path: path });
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
if (!("value" in raw)) {
|
|
435
|
+
fieldErrors[key] = "Missing value";
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
if (field.secret && raw.value === "***") {
|
|
439
|
+
// The UI echoes the redaction sentinel when the secret is unchanged.
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
const error = validateFieldValue(field, raw.value);
|
|
443
|
+
if (error) {
|
|
444
|
+
fieldErrors[key] = error;
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
operations.push({ op, path: path, value: raw.value });
|
|
448
|
+
}
|
|
449
|
+
if (Object.keys(fieldErrors).length > 0) {
|
|
450
|
+
return {
|
|
451
|
+
ok: false,
|
|
452
|
+
response: {
|
|
453
|
+
status: 400,
|
|
454
|
+
body: { error: "Some settings are invalid", code: "invalid-fields", fieldErrors },
|
|
455
|
+
},
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
const base = body.base;
|
|
459
|
+
let baseMtime;
|
|
460
|
+
if (isPlainConfigObject(base)) {
|
|
461
|
+
const layerBase = base[layer];
|
|
462
|
+
if (isPlainConfigObject(layerBase) && typeof layerBase.mtimeMs === "number") {
|
|
463
|
+
baseMtime = layerBase.mtimeMs;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return { ok: true, layer, operations, ...(baseMtime !== undefined ? { baseMtime } : {}) };
|
|
467
|
+
}
|
|
468
|
+
/** Apply validated operations, then return a fresh snapshot. */
|
|
469
|
+
function applyPatch(ctx, layer, operations, baseMtime) {
|
|
470
|
+
const targetPath = layer === "project" ? ctx.projectConfigPath : ctx.globalConfigPath;
|
|
471
|
+
const readPath = layer === "project" ? resolveProjectReadPath(ctx.workspaceDir, ctx.projectConfigPath) : targetPath;
|
|
472
|
+
if (baseMtime !== undefined) {
|
|
473
|
+
const current = fileMtime(readPath);
|
|
474
|
+
if (current !== baseMtime && !(current === 0 && baseMtime === 0)) {
|
|
475
|
+
return {
|
|
476
|
+
status: 409,
|
|
477
|
+
body: {
|
|
478
|
+
error: "The config file changed on disk since it was loaded. Reload and try again.",
|
|
479
|
+
code: "stale-layer",
|
|
480
|
+
layer,
|
|
481
|
+
currentMtimeMs: current,
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
let raw;
|
|
487
|
+
try {
|
|
488
|
+
raw = existsSync(readPath) ? readJsonObject(readPath) : {};
|
|
489
|
+
}
|
|
490
|
+
catch (err) {
|
|
491
|
+
return {
|
|
492
|
+
status: 422,
|
|
493
|
+
body: {
|
|
494
|
+
error: `Cannot edit ${readPath}: ${err.message}`,
|
|
495
|
+
code: "invalid-json",
|
|
496
|
+
layer,
|
|
497
|
+
},
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
for (const operation of operations) {
|
|
501
|
+
if (operation.op === "unset") {
|
|
502
|
+
deletePath(raw, operation.path);
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
setPath(raw, operation.path, operation.value);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
try {
|
|
509
|
+
// Editing a legacy/root file keeps that file authoritative — write it
|
|
510
|
+
// rather than creating a second config in `.opencode/` that would win.
|
|
511
|
+
writeLayerFile(readPath, raw);
|
|
512
|
+
}
|
|
513
|
+
catch (err) {
|
|
514
|
+
return {
|
|
515
|
+
status: 500,
|
|
516
|
+
body: { error: `Failed to write ${readPath}: ${err.message}` },
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
const changedPaths = operations.map((operation) => operation.path);
|
|
520
|
+
// A fresh file value must not be shadowed by an older runtime toggle.
|
|
521
|
+
for (const path of changedPaths) {
|
|
522
|
+
try {
|
|
523
|
+
deleteRuntimeOverride(ctx.storePath, [...path]);
|
|
524
|
+
}
|
|
525
|
+
catch {
|
|
526
|
+
// The override file is auxiliary; a failed cleanup must not undo the save.
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
let reloaded;
|
|
530
|
+
try {
|
|
531
|
+
reloaded = buildEffective(layer === "global" ? readPath : ctx.globalConfigPath, layer === "project" ? readPath : resolveProjectReadPath(ctx.workspaceDir, ctx.projectConfigPath), loadRuntimeOverrides(ctx.storePath));
|
|
532
|
+
}
|
|
533
|
+
catch {
|
|
534
|
+
reloaded = ctx.configRef.current;
|
|
535
|
+
}
|
|
536
|
+
if (reloaded) {
|
|
537
|
+
ctx.configRef.current = reloaded;
|
|
538
|
+
ctx.onConfigChanged?.(reloaded, changedPaths);
|
|
539
|
+
}
|
|
540
|
+
const snapshot = readConfigSnapshot(ctx);
|
|
541
|
+
const impacts = new Set(operations.map((operation) => findSettingField(operation.path)?.impact ?? "live"));
|
|
542
|
+
const impact = impacts.has("restart") ? "restart" : impacts.has("reindex") ? "reindex" : "live";
|
|
543
|
+
return {
|
|
544
|
+
status: 200,
|
|
545
|
+
body: {
|
|
546
|
+
...snapshot,
|
|
547
|
+
saved: {
|
|
548
|
+
layer,
|
|
549
|
+
applied: operations.length,
|
|
550
|
+
changedPaths: changedPaths.map(dottedPath),
|
|
551
|
+
impact,
|
|
552
|
+
},
|
|
553
|
+
},
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
/** PATCH /api/config — validated, atomic layer write. */
|
|
557
|
+
export function handleConfigPatch(ctx, body) {
|
|
558
|
+
const parsed = parsePatchBody(body);
|
|
559
|
+
if (!parsed.ok)
|
|
560
|
+
return parsed.response;
|
|
561
|
+
if (parsed.layer === "runtime") {
|
|
562
|
+
for (const operation of parsed.operations) {
|
|
563
|
+
try {
|
|
564
|
+
deleteRuntimeOverride(ctx.storePath, [...operation.path]);
|
|
565
|
+
}
|
|
566
|
+
catch (err) {
|
|
567
|
+
return {
|
|
568
|
+
status: 500,
|
|
569
|
+
body: { error: `Failed to clear runtime override: ${err.message}` },
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
let reloaded;
|
|
574
|
+
try {
|
|
575
|
+
reloaded = buildEffective(ctx.globalConfigPath, resolveProjectReadPath(ctx.workspaceDir, ctx.projectConfigPath), loadRuntimeOverrides(ctx.storePath));
|
|
576
|
+
}
|
|
577
|
+
catch {
|
|
578
|
+
reloaded = ctx.configRef.current;
|
|
579
|
+
}
|
|
580
|
+
if (reloaded) {
|
|
581
|
+
ctx.configRef.current = reloaded;
|
|
582
|
+
ctx.onConfigChanged?.(reloaded, []);
|
|
583
|
+
}
|
|
584
|
+
const snapshot = readConfigSnapshot(ctx);
|
|
585
|
+
return {
|
|
586
|
+
status: 200,
|
|
587
|
+
body: {
|
|
588
|
+
...snapshot,
|
|
589
|
+
saved: {
|
|
590
|
+
layer: "runtime",
|
|
591
|
+
applied: parsed.operations.length,
|
|
592
|
+
changedPaths: parsed.operations.map((operation) => dottedPath(operation.path)),
|
|
593
|
+
impact: "live",
|
|
594
|
+
},
|
|
595
|
+
},
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
return applyPatch(ctx, parsed.layer, parsed.operations, parsed.baseMtime);
|
|
599
|
+
}
|
|
600
|
+
/** Default context factory used by the server when no overrides are supplied. */
|
|
601
|
+
export function defaultConfigApiContext(workspaceDir, storePath, projectConfigPath, globalPath) {
|
|
602
|
+
return {
|
|
603
|
+
workspaceDir,
|
|
604
|
+
projectConfigPath: projectConfigPath ?? defaultProjectConfigPath(workspaceDir),
|
|
605
|
+
globalConfigPath: globalPath ?? defaultGlobalConfigPath(),
|
|
606
|
+
storePath,
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
/** All schema leaf paths, for the completeness test. */
|
|
610
|
+
export function schemaLeafPaths() {
|
|
611
|
+
return collectConfigLeafPaths(Object.fromEntries(SETTINGS_SECTIONS.map((section) => [section.id, section.fields])));
|
|
612
|
+
}
|
|
613
|
+
export { isSecretConfigPath };
|
|
614
|
+
//# sourceMappingURL=config-api.js.map
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Declarative schema for the Web UI Configuration page.
|
|
3
|
+
*
|
|
4
|
+
* The browser never derives setting names or types on its own: this module is
|
|
5
|
+
* the single server-owned description of what is editable, where it may be
|
|
6
|
+
* written (project vs. machine-global provider options), how it is validated,
|
|
7
|
+
* and what a change implies (live, reindex, restart).
|
|
8
|
+
*
|
|
9
|
+
* Keeping the schema on the server also lets the redaction layer work from
|
|
10
|
+
* known secret paths instead of guessing from key names — the old regex
|
|
11
|
+
* redacted numeric settings such as `memory.autoInjectMinTokenOverlap`.
|
|
12
|
+
*/
|
|
13
|
+
/** Control rendered for a setting. */
|
|
14
|
+
export type SettingType = "boolean" | "number" | "string" | "enum" | "string[]" | "stringMap" | "stringArrayMap" | "json";
|
|
15
|
+
/** What applying a change implies for the running system. */
|
|
16
|
+
export type SettingImpact = "live" | "reindex" | "restart";
|
|
17
|
+
/** Config layer a setting may be written to. */
|
|
18
|
+
export type SettingScope = "project" | "global";
|
|
19
|
+
/** One editable configuration value. */
|
|
20
|
+
export interface SettingField {
|
|
21
|
+
/** Dotted path segments into the config object (`["retrieval", "topK"]`). */
|
|
22
|
+
path: string[];
|
|
23
|
+
/** Short human label. */
|
|
24
|
+
label: string;
|
|
25
|
+
/** One-line hint shown under the control. */
|
|
26
|
+
description?: string;
|
|
27
|
+
/** Control type. */
|
|
28
|
+
type: SettingType;
|
|
29
|
+
/** Allowed values for `type: "enum"`. */
|
|
30
|
+
enumValues?: string[];
|
|
31
|
+
/** Inclusive minimum for `type: "number"`. */
|
|
32
|
+
minimum?: number;
|
|
33
|
+
/** Inclusive maximum for `type: "number"`. */
|
|
34
|
+
maximum?: number;
|
|
35
|
+
/** Step for `type: "number"` inputs. */
|
|
36
|
+
step?: number;
|
|
37
|
+
/** Placeholder for text inputs. */
|
|
38
|
+
placeholder?: string;
|
|
39
|
+
/** Render a textarea instead of a single-line input. */
|
|
40
|
+
multiline?: boolean;
|
|
41
|
+
/** Secret: never sent to the browser, only `secretSet` booleans are. */
|
|
42
|
+
secret?: boolean;
|
|
43
|
+
/** Shown only when the page's Advanced toggle is on. */
|
|
44
|
+
advanced?: boolean;
|
|
45
|
+
/** Writer layer. `global` settings are rejected by a project-layer write. */
|
|
46
|
+
scope: SettingScope;
|
|
47
|
+
/** Post-save guidance. */
|
|
48
|
+
impact: SettingImpact;
|
|
49
|
+
}
|
|
50
|
+
/** A group of related settings. */
|
|
51
|
+
export interface SettingSection {
|
|
52
|
+
id: string;
|
|
53
|
+
label: string;
|
|
54
|
+
description?: string;
|
|
55
|
+
fields: SettingField[];
|
|
56
|
+
}
|
|
57
|
+
/** The complete settings schema, in page order. */
|
|
58
|
+
export declare const SETTINGS_SECTIONS: SettingSection[];
|
|
59
|
+
/** Dotted form of a config path. */
|
|
60
|
+
export declare function dottedPath(path: readonly string[]): string;
|
|
61
|
+
/** Look up a schema field by path. */
|
|
62
|
+
export declare function findSettingField(path: readonly string[]): SettingField | undefined;
|
|
63
|
+
/** True when every segment is a safe object key (no prototype pollution). */
|
|
64
|
+
export declare function isSafeConfigPath(path: readonly string[]): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Secret-ness of a path for redaction purposes.
|
|
67
|
+
*
|
|
68
|
+
* Schema fields win. For paths the schema does not know (custom or future
|
|
69
|
+
* keys) only an EXACT credential leaf name counts — never a substring match,
|
|
70
|
+
* which is what wrongly redacted `autoInjectMinTokenOverlap`.
|
|
71
|
+
*/
|
|
72
|
+
export declare function isSecretConfigPath(path: readonly string[]): boolean;
|
|
73
|
+
/** True when a value is a plain JSON object. */
|
|
74
|
+
export declare function isPlainConfigObject(value: unknown): value is Record<string, unknown>;
|
|
75
|
+
/** Deep-clone a JSON-ish value. */
|
|
76
|
+
export declare function cloneConfigValue<T>(value: T): T;
|
|
77
|
+
/**
|
|
78
|
+
* Strip secret values from a config object, in place.
|
|
79
|
+
*
|
|
80
|
+
* Known secret fields become the `***` sentinel (so the UI can show that a
|
|
81
|
+
* value exists without receiving it); unknown credential-named leaves are
|
|
82
|
+
* removed entirely.
|
|
83
|
+
*/
|
|
84
|
+
export declare function redactConfigSecrets(value: unknown, path?: readonly string[]): void;
|
|
85
|
+
/** Explicitly ignored leaves that are not user-editable settings. */
|
|
86
|
+
export declare const IGNORED_CONFIG_PATHS: Set<string>;
|
|
87
|
+
/** Collect the dotted leaf paths of a config object (arrays are leaves). */
|
|
88
|
+
export declare function collectConfigLeafPaths(value: unknown, path?: readonly string[], out?: string[]): string[];
|