opencode-rag-plugin 2.12.3 → 2.13.1

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/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 };