opencode-rag-plugin 2.12.3 → 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 CHANGED
@@ -79,7 +79,8 @@ No GPU is required, although it is recommended when using code and image descrip
79
79
  npm install -g opencode-rag-plugin
80
80
 
81
81
  # 2. Set up the runtime, the workspace, and your models
82
- # (a short interactive wizard guides you through the configuration)
82
+ # (a short interactive wizard guides you through the configuration;
83
+ # the web UI then opens on the Configuration page)
83
84
  cd /path/to/your/project
84
85
  opencode-rag setup
85
86
 
@@ -114,6 +115,7 @@ It handles the whole onboarding in one command:
114
115
  - installs or verifies the machine-level runtime in `~/.opencode/`
115
116
  - scaffolds the current workspace (`.opencode/` config, plugin entries, skill files)
116
117
  - on the first run, or with `--reconfigure`, starts a **model wizard** for the embedding, description, vision, and decision models
118
+ - an interactive run finishes by opening the **Web UI on its Configuration page**, so the generated settings can be reviewed and adjusted; the server stays up until you press `Ctrl+C` (`--no-ui` keeps setup headless)
117
119
 
118
120
  A plain re-run only syncs the workspace files. The wizard is scriptable (`--yes`, `--provider`, `--base-url`, `--embedding-model`, `--description off`, and more), and `--skip-runtime` limits the run to the workspace. Use `opencode-rag update` to upgrade the runtime to a newer release.
119
121
 
@@ -387,7 +389,7 @@ Image files (PNG, JPEG, WebP, and others) are sent to a vision-capable LLM, and
387
389
 
388
390
  ### 7. Web UI, evaluation, and calibration
389
391
 
390
- **Web UI.** A browser dashboard for exploring the vector database: browse, visualize, and inspect chunks, and evaluate OpenCode sessions by retrieved chunks and consumed tokens. Launch it with `opencode-rag ui`. See the [Web UI docs](doc/webui.md).
392
+ **Web UI.** A browser dashboard for exploring the vector database: browse, visualize, and inspect chunks side-by-side with a movable splitter, edit the global and project configuration layers, switch between dark, plasma, drama, and light themes, and evaluate OpenCode sessions by retrieved chunks and consumed tokens. Launch it with `opencode-rag ui` (or let `opencode-rag setup` open it on the Configuration page). See the [Web UI docs](doc/webui.md).
391
393
 
392
394
  [![OpenCodeRAG Web UI](doc/assets/webui-3d.png)](doc/assets/webui-3d.png)
393
395
 
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @fileoverview Cross-platform helper for opening a URL in the user's default browser.
3
+ */
4
+ /**
5
+ * Open a URL in the system default browser.
6
+ *
7
+ * Spawns the platform launcher detached so it outlives the CLI process:
8
+ * `cmd.exe /c start` on Windows, `open` on macOS and `xdg-open` elsewhere.
9
+ * Every spawn passes `windowsHide: true` — the plugin also runs inside
10
+ * OpenCode's console-less service, where an un-hidden spawn flashes a
11
+ * terminal window per call.
12
+ *
13
+ * The returned promise resolves once the launcher process either spawned or
14
+ * failed (a missing `xdg-open` emits an `error` event rather than throwing),
15
+ * so callers can tell "no browser available" from "browser opened".
16
+ *
17
+ * @param url - Absolute URL to open.
18
+ * @returns `true` when the launcher process was spawned, `false` otherwise.
19
+ */
20
+ export declare function openInBrowser(url: string): Promise<boolean>;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @fileoverview Cross-platform helper for opening a URL in the user's default browser.
3
+ */
4
+ import { spawn } from "node:child_process";
5
+ /**
6
+ * Open a URL in the system default browser.
7
+ *
8
+ * Spawns the platform launcher detached so it outlives the CLI process:
9
+ * `cmd.exe /c start` on Windows, `open` on macOS and `xdg-open` elsewhere.
10
+ * Every spawn passes `windowsHide: true` — the plugin also runs inside
11
+ * OpenCode's console-less service, where an un-hidden spawn flashes a
12
+ * terminal window per call.
13
+ *
14
+ * The returned promise resolves once the launcher process either spawned or
15
+ * failed (a missing `xdg-open` emits an `error` event rather than throwing),
16
+ * so callers can tell "no browser available" from "browser opened".
17
+ *
18
+ * @param url - Absolute URL to open.
19
+ * @returns `true` when the launcher process was spawned, `false` otherwise.
20
+ */
21
+ export async function openInBrowser(url) {
22
+ try {
23
+ // The empty title argument keeps `start` from treating the URL as the
24
+ // window title on Windows.
25
+ const child = process.platform === "win32"
26
+ ? spawn("cmd.exe", ["/c", "start", "", url], {
27
+ detached: true,
28
+ stdio: "ignore",
29
+ windowsHide: true,
30
+ })
31
+ : spawn(process.platform === "darwin" ? "open" : "xdg-open", [url], {
32
+ detached: true,
33
+ stdio: "ignore",
34
+ windowsHide: true,
35
+ });
36
+ const spawned = await new Promise((resolve) => {
37
+ child.once("spawn", () => resolve(true));
38
+ child.once("error", () => resolve(false));
39
+ });
40
+ child.unref();
41
+ return spawned;
42
+ }
43
+ catch {
44
+ // `spawn` throws synchronously only for invalid arguments — treat any
45
+ // throw as "could not open a browser".
46
+ return false;
47
+ }
48
+ }
49
+ //# sourceMappingURL=browser.js.map
@@ -15,6 +15,7 @@ import { generateDefaultConfigJson } from "./workspace-helpers.js";
15
15
  import { detectOllamaBackend } from "./backend-detect.js";
16
16
  import { isLikelyProjectRoot, runWorkspaceHealthCheck, scaffoldWorkspace, } from "../workspace.js";
17
17
  import { destroyAllPooledConnections } from "../../embedder/http.js";
18
+ import { launchSetupConfigUi, shouldLaunchSetupUi } from "../setup-ui.js";
18
19
  import { getGlobalPluginDir, installGlobalPlugin, isGlobalPluginInstalled, isWorkspacePluginInstalled, removeGlobalPlugin, } from "../../core/global-plugin.js";
19
20
  const PLUGIN_NAME = "opencode-rag-plugin";
20
21
  function removeIfExists(targetPath) {
@@ -72,6 +73,9 @@ export function registerSetupCommand(program) {
72
73
  " Use --reconfigure to re-open the wizard, or pass flags for scripted setups.\n" +
73
74
  " - Updating: use 'opencode-rag update' to install a newer release.\n" +
74
75
  " - Troubleshooting: use --check to inspect the runtime, or --force to reinstall.\n" +
76
+ "\nAfter an interactive run the web UI opens on the Configuration page so the\n" +
77
+ "generated settings can be reviewed; press Ctrl+C there to finish. Use --no-ui\n" +
78
+ "to keep setup headless.\n" +
75
79
  "\nProvider/model settings are written to the global config\n" +
76
80
  "(~/.config/opencode/opencode-rag.json); feature toggles to the workspace\n" +
77
81
  "config (.opencode/opencode-rag.json).\n")
@@ -104,6 +108,7 @@ export function registerSetupCommand(program) {
104
108
  .option("--skip-runtime [on|off]", "skip the machine-level runtime install (workspace setup only)")
105
109
  .option("--workspace-plugin", "install the plugin into this workspace instead of the global config")
106
110
  .option("--no-index", "do not offer to build the search index after setup")
111
+ .option("--no-ui", "do not open the web UI Configuration page after setup")
107
112
  .action(async (options) => {
108
113
  await runSetup(options);
109
114
  });
@@ -413,6 +418,12 @@ async function runSetup(rawOptions) {
413
418
  console.log(` ${c.dim(" 1. Restart OpenCode if it is running")}`);
414
419
  console.log(` ${c.dim(" 2. Run `opencode-rag index` to build the search index")}`);
415
420
  checkOpenCodeRunning();
421
+ // Interactive runs hand off to the Configuration page so the generated
422
+ // settings can be reviewed immediately; the process stays alive until the
423
+ // user presses Ctrl+C. Never in scripted (`-y`) or failed runs.
424
+ if ((process.exitCode ?? 0) === 0 && shouldLaunchSetupUi(options)) {
425
+ await launchSetupConfigUi(process.cwd());
426
+ }
416
427
  console.log();
417
428
  }
418
429
  /**
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import path from "node:path";
8
8
  import { c, resolveCliContext, cleanupContext, logCliError, logCliInfo } from "../format.js";
9
+ import { openInBrowser } from "../browser.js";
9
10
  /**
10
11
  * Register the `ui` command on the given Commander program.
11
12
  *
@@ -43,17 +44,8 @@ export function registerUiCommand(program) {
43
44
  logCliInfo(logFilePath, "ui", ` ${c.dim("The token in the URL authenticates this session — keep it private.")}`);
44
45
  logCliInfo(logFilePath, "ui", ` ${c.dim("Press Ctrl+C to stop")}\n`);
45
46
  if (openBrowser) {
46
- const { spawn } = await import("node:child_process");
47
- try {
48
- if (process.platform === "win32") {
49
- spawn("cmd.exe", ["/c", "start", url], { detached: true, stdio: "ignore", windowsHide: true }).unref();
50
- }
51
- else {
52
- const cmd = process.platform === "darwin" ? "open" : "xdg-open";
53
- spawn(cmd, [url], { detached: true, stdio: "ignore" }).unref();
54
- }
55
- }
56
- catch {
47
+ const opened = await openInBrowser(url);
48
+ if (!opened) {
57
49
  console.error(c.dim(`Could not open browser automatically. Open ${url} manually.`));
58
50
  }
59
51
  }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @fileoverview Optional hand-off from `opencode-rag setup` to the web UI's
3
+ * Configuration view.
4
+ *
5
+ * Once the workspace is configured, an interactive setup opens the browser on
6
+ * the Configuration page and keeps the UI server alive until the user presses
7
+ * Ctrl+C. Scripted runs (`-y`), non-TTY runs and `--no-ui` never launch it.
8
+ */
9
+ /** Setup options that decide whether the Configuration UI opens. */
10
+ export interface SetupUiLaunchOptions {
11
+ /** Set to `false` by `--no-ui`. */
12
+ ui?: boolean;
13
+ /** Set by `-y`/`--yes`: scripted runs never open a browser. */
14
+ yes?: boolean;
15
+ }
16
+ /**
17
+ * Decide whether a finished setup should open the Configuration UI.
18
+ *
19
+ * Deliberately conservative: the setup process stays alive while the UI is
20
+ * open, so it must never trigger in a scripted (`-y`), non-interactive or
21
+ * `--no-ui` run — those would hang a CI job or an install pipeline.
22
+ *
23
+ * @param options - Parsed setup options.
24
+ * @param interactive - Whether stdin/stdout are a TTY (injectable for tests).
25
+ * @returns `true` when the UI should be launched.
26
+ */
27
+ export declare function shouldLaunchSetupUi(options: SetupUiLaunchOptions, interactive?: boolean): boolean;
28
+ /**
29
+ * Open the web UI on the Configuration view and keep it running until the
30
+ * user stops it.
31
+ *
32
+ * Best-effort by design: setup has already completed, so a failure to start
33
+ * the server or open a browser only prints a hint and returns. The browser
34
+ * link carries the per-run auth token and the `#config` hash route.
35
+ *
36
+ * @param cwd - Workspace root (defaults to the current directory).
37
+ */
38
+ export declare function launchSetupConfigUi(cwd?: string): Promise<void>;
@@ -0,0 +1,133 @@
1
+ /**
2
+ * @fileoverview Optional hand-off from `opencode-rag setup` to the web UI's
3
+ * Configuration view.
4
+ *
5
+ * Once the workspace is configured, an interactive setup opens the browser on
6
+ * the Configuration page and keeps the UI server alive until the user presses
7
+ * Ctrl+C. Scripted runs (`-y`), non-TTY runs and `--no-ui` never launch it.
8
+ */
9
+ import { c, cleanupContext } from "./format.js";
10
+ import { openInBrowser } from "./browser.js";
11
+ import { isInteractiveTerminal } from "./prompt.js";
12
+ import { findConfigFile, projectConfigPath } from "../core/config.js";
13
+ import { resolveRagContext } from "../core/bootstrap.js";
14
+ /**
15
+ * Decide whether a finished setup should open the Configuration UI.
16
+ *
17
+ * Deliberately conservative: the setup process stays alive while the UI is
18
+ * open, so it must never trigger in a scripted (`-y`), non-interactive or
19
+ * `--no-ui` run — those would hang a CI job or an install pipeline.
20
+ *
21
+ * @param options - Parsed setup options.
22
+ * @param interactive - Whether stdin/stdout are a TTY (injectable for tests).
23
+ * @returns `true` when the UI should be launched.
24
+ */
25
+ export function shouldLaunchSetupUi(options, interactive = isInteractiveTerminal()) {
26
+ if (options.ui === false)
27
+ return false;
28
+ if (options.yes === true)
29
+ return false;
30
+ return interactive;
31
+ }
32
+ /**
33
+ * Open the web UI on the Configuration view and keep it running until the
34
+ * user stops it.
35
+ *
36
+ * Best-effort by design: setup has already completed, so a failure to start
37
+ * the server or open a browser only prints a hint and returns. The browser
38
+ * link carries the per-run auth token and the `#config` hash route.
39
+ *
40
+ * @param cwd - Workspace root (defaults to the current directory).
41
+ */
42
+ export async function launchSetupConfigUi(cwd = process.cwd()) {
43
+ let ctx;
44
+ try {
45
+ // Read-only context: never probe the provider or reconcile quirks during
46
+ // setup — the Configuration page only needs the resolved config and paths.
47
+ ctx = await resolveRagContext({ cwd, skipProbe: true, skipKeywordIndex: true });
48
+ }
49
+ catch (err) {
50
+ console.log(` ${c.warn("Configuration UI skipped:")} ${err.message}`);
51
+ return;
52
+ }
53
+ if (ctx.config.ui?.openBrowser === false) {
54
+ console.log(` ${c.dim("Configuration UI: browser launch disabled (ui.openBrowser = false).")} ` +
55
+ `Run ${c.file("opencode-rag ui")} to open the dashboard.`);
56
+ await cleanupContext(ctx);
57
+ return;
58
+ }
59
+ const configPath = findConfigFile(cwd) ?? projectConfigPath(cwd);
60
+ const port = ctx.config.ui?.port ?? 3210;
61
+ let server;
62
+ try {
63
+ const { startWebUi } = await import("../web/server.js");
64
+ try {
65
+ server = await startWebUi(ctx.storePath, port, cwd, ctx.dimension, ctx.config, configPath);
66
+ }
67
+ catch (err) {
68
+ if (!isAddressInUse(err) || port === 0)
69
+ throw err;
70
+ console.log(` ${c.dim(`Port ${port} is in use — using a free port for the Configuration UI.`)}`);
71
+ server = await startWebUi(ctx.storePath, 0, cwd, ctx.dimension, ctx.config, configPath);
72
+ }
73
+ }
74
+ catch (err) {
75
+ console.log(` ${c.warn("Configuration UI could not start:")} ${err.message}`);
76
+ console.log(` ${c.dim("Run `opencode-rag ui` later to open the dashboard.")}`);
77
+ await cleanupContext(ctx);
78
+ return;
79
+ }
80
+ const url = `http://127.0.0.1:${server.port}/?token=${server.token}#config`;
81
+ console.log(`\n${c.heading("Configuration UI")}`);
82
+ console.log(` ${c.label("URL:")} ${c.value(url)}`);
83
+ console.log(` ${c.dim("The token in the URL authenticates this session — keep it private.")}`);
84
+ console.log(` ${c.dim("Edit the global and project settings in the browser, then press Ctrl+C to stop.")}\n`);
85
+ if (!(await openInBrowser(url))) {
86
+ console.log(` ${c.warn("Could not open the browser automatically.")} Open the URL above manually,`);
87
+ console.log(` ${c.dim("or run `opencode-rag ui` later to open the dashboard.")}`);
88
+ // Nobody is looking at a page we could not open — stop instead of hanging.
89
+ await closeUi(server, ctx);
90
+ return;
91
+ }
92
+ let shuttingDown = false;
93
+ const shutdown = async () => {
94
+ if (shuttingDown) {
95
+ // Second Ctrl+C: do not wait for the graceful close.
96
+ process.exit(1);
97
+ }
98
+ shuttingDown = true;
99
+ await closeUi(server, ctx);
100
+ process.exit(0);
101
+ };
102
+ process.on("SIGINT", shutdown);
103
+ process.on("SIGTERM", shutdown);
104
+ }
105
+ /**
106
+ * Tear down the UI server and the bootstrap context it borrowed.
107
+ *
108
+ * @param server - Running web UI handle.
109
+ * @param ctx - Bootstrap context created for the launch.
110
+ */
111
+ async function closeUi(server, ctx) {
112
+ try {
113
+ await server.close();
114
+ }
115
+ catch {
116
+ // best-effort — setup is finishing anyway
117
+ }
118
+ try {
119
+ await cleanupContext(ctx);
120
+ }
121
+ catch {
122
+ // best-effort cleanup
123
+ }
124
+ }
125
+ /** Whether a `listen()` error is a port conflict. */
126
+ function isAddressInUse(err) {
127
+ if (typeof err !== "object" || err === null)
128
+ return false;
129
+ if (err.code === "EADDRINUSE")
130
+ return true;
131
+ return /EADDRINUSE/.test(err.message ?? "");
132
+ }
133
+ //# sourceMappingURL=setup-ui.js.map
@@ -59,6 +59,11 @@ export interface SetupOptions extends WizardFlags {
59
59
  workspacePlugin?: boolean;
60
60
  /** Set to `false` by `--no-index`: do not offer to build the index. */
61
61
  index?: boolean;
62
+ /**
63
+ * Set to `false` by `--no-ui`: do not open the web UI Configuration page
64
+ * when an interactive setup finishes.
65
+ */
66
+ ui?: boolean;
62
67
  }
63
68
  /** Shape of the `package.json` metadata required by CLI helpers. */
64
69
  export interface PackageMetadata {
@@ -321,6 +321,13 @@ export interface UiConfig {
321
321
  port: number;
322
322
  /** Whether to automatically open the browser on startup. */
323
323
  openBrowser: boolean;
324
+ /**
325
+ * Dashboard color theme: `dark`, `light`, `plasma` or `drama`.
326
+ * Machine-wide preference, so it lives in the global config; the web UI
327
+ * caches it in localStorage for first paint and treats this as the source
328
+ * of truth on snapshot load.
329
+ */
330
+ theme: string;
324
331
  }
325
332
  /** Configuration for the automated documentation mode that adds JSDoc/TSDoc to source files. */
326
333
  export interface DocumentationModeConfig {
@@ -376,6 +376,7 @@ export const DEFAULT_CONFIG = {
376
376
  ui: {
377
377
  port: 3210,
378
378
  openBrowser: true,
379
+ theme: "dark",
379
380
  },
380
381
  tui: {
381
382
  fileListKeybinding: "ctrl+enter",
@@ -612,6 +613,9 @@ export function validateConfig(config) {
612
613
  if (config.ui.port < 1 || config.ui.port > 65535) {
613
614
  warnings.push("ui.port must be between 1 and 65535");
614
615
  }
616
+ if (!["dark", "light", "plasma", "drama"].includes(config.ui.theme)) {
617
+ warnings.push(`ui.theme "${config.ui.theme}" — expected "dark", "light", "plasma" or "drama"`);
618
+ }
615
619
  }
616
620
  if (config.description) {
617
621
  const KNOWN_DESCRIPTION_PROVIDERS = new Set(["ollama", "openai", "anthropic", "google"]);
package/dist/web/api.d.ts CHANGED
@@ -28,9 +28,16 @@ interface ApiResponse {
28
28
  * @param getEmbedder - Lazily-created embedder for /api/retrieve and reindex passes.
29
29
  * @param token - Per-run auth token required on every request.
30
30
  * @param configPath - Path to the opencode-rag.json config file, used by PUT /api/config.
31
+ * @param onConfigChanged - Called after a layered config write (reset embedder/caches).
31
32
  * @returns An async handler that returns `true` when a route matched or `false` otherwise.
32
33
  */
33
- export declare function createApiHandler(store: ChunkBrowseStore, keywordIndex: KeywordIndex, storePath: string, cwd?: string, cfg?: RagConfig, getEmbedder?: () => Promise<EmbeddingProvider>, token?: string, configPath?: string): (req: IncomingMessage, res: ServerResponse) => Promise<boolean>;
34
+ export declare function createApiHandler(store: ChunkBrowseStore, keywordIndex: KeywordIndex, storePath: string, cwd?: string, cfg?: RagConfig, getEmbedder?: () => Promise<EmbeddingProvider>, token?: string, configPath?: string, onConfigChanged?: (cfg: RagConfig, changedPaths: string[][]) => void): (req: IncomingMessage, res: ServerResponse) => Promise<boolean>;
35
+ /**
36
+ * Return the effective configuration with API keys redacted.
37
+ *
38
+ * Retained for the legacy `/api/config` contract used by embedded clients.
39
+ */
40
+ export declare function handleConfig(cfg: RagConfig): ApiResponse;
34
41
  /**
35
42
  * Apply a validated config patch (`{ indexing: { includeDirs: [...] } }`)
36
43
  * to the on-disk config file and refresh the in-memory config used by
package/dist/web/api.js CHANGED
@@ -6,9 +6,11 @@ import { analyzeTokenUsage, compareTokenAnalyses, projectTokenSavings } from "..
6
6
  import { listQuirks, lintQuirks, removeQuirk } from "../quirks/quirk-store.js";
7
7
  import { retrieve } from "../retriever/retriever.js";
8
8
  import { getRerankerFor } from "../reranker/factory.js";
9
- import { updateConfigValue, ensureConfigFile } from "../core/config.js";
9
+ import { updateConfigValue, ensureConfigFile, globalConfigPath, projectConfigPath, } from "../core/config.js";
10
10
  import { createExcludeMatcher } from "../core/exclude.js";
11
11
  import { CODE_SEARCH_FILTER } from "../core/interfaces.js";
12
+ import { handleConfigLayers, handleConfigPatch, } from "./config-api.js";
13
+ import { redactConfigSecrets } from "./config-schema.js";
12
14
  const FILE_MIME_TYPES = {
13
15
  ".png": "image/png",
14
16
  ".jpg": "image/jpeg",
@@ -71,7 +73,7 @@ function tokensEqual(a, b) {
71
73
  function sendJson(res, response, origin) {
72
74
  const headers = {
73
75
  "Content-Type": "application/json",
74
- "Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
76
+ "Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
75
77
  "Access-Control-Allow-Headers": "Content-Type, Authorization",
76
78
  "X-Content-Type-Options": "nosniff",
77
79
  };
@@ -100,13 +102,23 @@ function sendJson(res, response, origin) {
100
102
  * @param getEmbedder - Lazily-created embedder for /api/retrieve and reindex passes.
101
103
  * @param token - Per-run auth token required on every request.
102
104
  * @param configPath - Path to the opencode-rag.json config file, used by PUT /api/config.
105
+ * @param onConfigChanged - Called after a layered config write (reset embedder/caches).
103
106
  * @returns An async handler that returns `true` when a route matched or `false` otherwise.
104
107
  */
105
- export function createApiHandler(store, keywordIndex, storePath, cwd, cfg, getEmbedder, token, configPath) {
108
+ export function createApiHandler(store, keywordIndex, storePath, cwd, cfg, getEmbedder, token, configPath, onConfigChanged) {
106
109
  // Mutable config reference so PUT /api/config can update the in-memory
107
110
  // config used by subsequent requests (retrieve, reindex, quirk endpoints).
108
111
  const configRef = { current: cfg };
109
112
  const effectiveConfig = () => configRef.current ?? cfg ?? {};
113
+ const workspaceDir = cwd ?? process.cwd();
114
+ const configCtx = {
115
+ workspaceDir,
116
+ projectConfigPath: configPath ?? projectConfigPath(workspaceDir),
117
+ globalConfigPath: globalConfigPath(),
118
+ storePath,
119
+ configRef,
120
+ ...(onConfigChanged ? { onConfigChanged } : {}),
121
+ };
110
122
  return async (req, res) => {
111
123
  const url = req.url ?? "/";
112
124
  const method = req.method ?? "GET";
@@ -122,7 +134,7 @@ export function createApiHandler(store, keywordIndex, storePath, cwd, cfg, getEm
122
134
  res.writeHead(204, {
123
135
  "Access-Control-Allow-Origin": origin,
124
136
  "Vary": "Origin",
125
- "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
137
+ "Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
126
138
  "Access-Control-Allow-Headers": "Content-Type, Authorization",
127
139
  });
128
140
  res.end();
@@ -186,7 +198,11 @@ export function createApiHandler(store, keywordIndex, storePath, cwd, cfg, getEm
186
198
  response = await handleReindex(cwd, effectiveConfig(), storePath, store, getEmbedder);
187
199
  }
188
200
  else if (path === "/api/config" && method === "GET") {
189
- response = await handleConfig(effectiveConfig());
201
+ response = handleConfigLayers(configCtx);
202
+ }
203
+ else if (path === "/api/config" && method === "PATCH") {
204
+ const body = await readBody(req);
205
+ response = handleConfigPatch(configCtx, body);
190
206
  }
191
207
  else if (path === "/api/config" && method === "PUT") {
192
208
  const body = await readBody(req);
@@ -656,21 +672,19 @@ async function handleReindex(cwd, cfg, storePath, store, getEmbedder) {
656
672
  }
657
673
  /**
658
674
  * Return the effective configuration with API keys redacted.
675
+ *
676
+ * Retained for the legacy `/api/config` contract used by embedded clients.
659
677
  */
660
- function handleConfig(cfg) {
678
+ export function handleConfig(cfg) {
661
679
  const redacted = JSON.parse(JSON.stringify(cfg));
662
680
  redactKeys(redacted);
663
681
  return { status: 200, body: { config: redacted } };
664
682
  }
665
683
  function redactKeys(obj) {
666
- for (const key of Object.keys(obj)) {
667
- if (/api\s*key|apikey|password|passwd|secret|token|authorization|credential/i.test(key)) {
668
- obj[key] = "***";
669
- }
670
- else if (typeof obj[key] === "object" && obj[key] !== null) {
671
- redactKeys(obj[key]);
672
- }
673
- }
684
+ // Schema-aware: only real credential paths are redacted. The old
685
+ // `/token/i` substring rule also matched numeric settings such as
686
+ // `memory.autoInjectMinTokenOverlap`.
687
+ redactConfigSecrets(obj);
674
688
  }
675
689
  /**
676
690
  * Config keys under `indexing` that the UI may write. Values must be arrays
@@ -0,0 +1,60 @@
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 { type RagConfig } from "../core/config.js";
18
+ import { isSecretConfigPath } from "./config-schema.js";
19
+ /** HTTP-shape response used by the API router. */
20
+ export interface ConfigApiResponse {
21
+ status: number;
22
+ body: unknown;
23
+ }
24
+ /** Dependencies for the layered config handlers. */
25
+ export interface ConfigApiContext {
26
+ /** Workspace directory whose effective config is served. */
27
+ workspaceDir: string;
28
+ /** Canonical project config path (write target). */
29
+ projectConfigPath: string;
30
+ /** Machine-global config path. */
31
+ globalConfigPath: string;
32
+ /** Store directory that holds `runtime-overrides.json`. */
33
+ storePath: string;
34
+ /** Mutable in-memory config used by other API routes. */
35
+ configRef: {
36
+ current?: RagConfig;
37
+ };
38
+ /** Called after a successful write so the server can reset caches. */
39
+ onConfigChanged?: (cfg: RagConfig, changedPaths: string[][]) => void;
40
+ }
41
+ /**
42
+ * Resolve the workspace config file to read and write.
43
+ *
44
+ * An explicit `--config` path (anything that is not the default canonical
45
+ * location) stays authoritative. Otherwise prefer the canonical
46
+ * `.opencode/opencode-rag.json`, then fall back to a legacy root file so an
47
+ * edit keeps that file authoritative instead of creating a second config.
48
+ */
49
+ export declare function resolveProjectReadPath(workspaceDir: string, canonical: string): string;
50
+ /** Load a full snapshot of schema, layers, effective config, and metadata. */
51
+ export declare function readConfigSnapshot(ctx: ConfigApiContext): Record<string, unknown>;
52
+ /** GET /api/config — schema + layers + effective config. */
53
+ export declare function handleConfigLayers(ctx: ConfigApiContext): ConfigApiResponse;
54
+ /** PATCH /api/config — validated, atomic layer write. */
55
+ export declare function handleConfigPatch(ctx: ConfigApiContext, body: unknown): ConfigApiResponse;
56
+ /** Default context factory used by the server when no overrides are supplied. */
57
+ export declare function defaultConfigApiContext(workspaceDir: string, storePath: string, projectConfigPath?: string, globalPath?: string): Pick<ConfigApiContext, "workspaceDir" | "projectConfigPath" | "globalConfigPath" | "storePath">;
58
+ /** All schema leaf paths, for the completeness test. */
59
+ export declare function schemaLeafPaths(): string[];
60
+ export { isSecretConfigPath };