opencode-rag-plugin 2.5.0 → 2.5.2
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/chunker/docx.d.ts +6 -5
- package/dist/chunker/docx.js +117 -5
- package/dist/content/docx.d.ts +1 -1
- package/dist/content/docx.js +2 -2
- package/dist/plugin.d.ts +41 -0
- package/dist/plugin.js +158 -14
- package/package.json +4 -3
package/dist/chunker/docx.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Text extraction and paragraph-based chunker for Word (.docx) documents using mammoth.
|
|
3
|
-
*/
|
|
4
1
|
import type { Chunker, Chunk } from "../core/interfaces.js";
|
|
5
2
|
/**
|
|
6
3
|
* Extract the raw text from a Word (.docx) file buffer.
|
|
7
|
-
*
|
|
4
|
+
*
|
|
5
|
+
* Unzips the archive and reads `word/document.xml` directly. Only that single
|
|
6
|
+
* entry is decompressed, and an inflated-size cap is enforced, so a crafted
|
|
7
|
+
* archive cannot exhaust memory.
|
|
8
8
|
* @param buffer - Raw buffer of the .docx file.
|
|
9
9
|
* @returns The extracted raw text as a string.
|
|
10
|
+
* @throws If the buffer is not a valid ZIP archive or lacks `word/document.xml`.
|
|
10
11
|
*/
|
|
11
|
-
export declare function extractDocxText(buffer: Buffer):
|
|
12
|
+
export declare function extractDocxText(buffer: Buffer): string;
|
|
12
13
|
/**
|
|
13
14
|
* Chunker for Word (.docx) documents.
|
|
14
15
|
* Splits the extracted plain text by paragraph boundaries, grouping small
|
package/dist/chunker/docx.js
CHANGED
|
@@ -1,17 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Text extraction and paragraph-based chunker for Word (.docx) documents.
|
|
3
|
+
*
|
|
4
|
+
* A `.docx` file is a ZIP archive containing `word/document.xml`, where the text
|
|
5
|
+
* lives in `<w:t>` runs and paragraph boundaries are `<w:p>` elements. Extraction
|
|
6
|
+
* is done directly with fflate's `unzipSync` so the plugin pulls in no document
|
|
7
|
+
* parser (the previous `mammoth` dependency carried six transitive packages,
|
|
8
|
+
* several of them with known prototype-pollution advisories).
|
|
9
|
+
*/
|
|
10
|
+
import { unzipSync } from "fflate";
|
|
1
11
|
import { uuid } from "./uuid.js";
|
|
2
12
|
const MAX_CHUNK_CHARS = 4000;
|
|
3
13
|
const MIN_GROUP_CHARS = 300;
|
|
4
14
|
const PARAGRAPH_SPLIT = /\n\s*\n/;
|
|
15
|
+
/** ZIP entry holding the document body. */
|
|
16
|
+
const DOCUMENT_XML = "word/document.xml";
|
|
17
|
+
/**
|
|
18
|
+
* Upper bound on the decompressed size of `word/document.xml`.
|
|
19
|
+
*
|
|
20
|
+
* `.docx` files are frequently untrusted, and a small archive can inflate to
|
|
21
|
+
* gigabytes (a zip bomb). Anything past this is refused instead of buffered.
|
|
22
|
+
*/
|
|
23
|
+
const MAX_DOCUMENT_XML_BYTES = 64 * 1024 * 1024;
|
|
24
|
+
/**
|
|
25
|
+
* Decode the XML entities Word emits inside text runs.
|
|
26
|
+
* @param text - Raw (escaped) text from a `<w:t>` element.
|
|
27
|
+
* @returns The unescaped text.
|
|
28
|
+
*/
|
|
29
|
+
function decodeXmlEntities(text) {
|
|
30
|
+
return text
|
|
31
|
+
.replace(/&#x([0-9a-fA-F]+);/g, (_, hex) => String.fromCodePoint(parseInt(hex, 16)))
|
|
32
|
+
.replace(/&#(\d+);/g, (_, dec) => String.fromCodePoint(parseInt(dec, 10)))
|
|
33
|
+
.replace(/</g, "<")
|
|
34
|
+
.replace(/>/g, ">")
|
|
35
|
+
.replace(/"/g, '"')
|
|
36
|
+
.replace(/'/g, "'")
|
|
37
|
+
.replace(/&/g, "&");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Convert raw `word/document.xml` markup into plain text.
|
|
41
|
+
*
|
|
42
|
+
* Mirrors what `mammoth.extractRawText` produced for the chunker's purposes:
|
|
43
|
+
* `<w:t>` text is concatenated, `<w:tab>` becomes a tab, `<w:br>` becomes a
|
|
44
|
+
* newline, and each paragraph is terminated by a blank line so the
|
|
45
|
+
* {@link PARAGRAPH_SPLIT} regex still sees paragraph boundaries.
|
|
46
|
+
* @param xml - Contents of `word/document.xml`.
|
|
47
|
+
* @returns Plain text with paragraphs separated by blank lines.
|
|
48
|
+
*/
|
|
49
|
+
function documentXmlToText(xml) {
|
|
50
|
+
let out = "";
|
|
51
|
+
// Text outside a <w:p> is document metadata, not body text.
|
|
52
|
+
let inParagraph = false;
|
|
53
|
+
const OPEN_TAG = /<w:(t|tab|br|p)(?:\s[^>]*)?\/?>/g;
|
|
54
|
+
const CLOSE_P = /<\/w:p>/g;
|
|
55
|
+
// Walk both matchers in document order.
|
|
56
|
+
let open = OPEN_TAG.exec(xml);
|
|
57
|
+
let close = CLOSE_P.exec(xml);
|
|
58
|
+
while (open !== null) {
|
|
59
|
+
// A closing </w:p> before the next opening tag wins.
|
|
60
|
+
if (close !== null && close.index < open.index) {
|
|
61
|
+
if (inParagraph) {
|
|
62
|
+
out += "\n\n";
|
|
63
|
+
inParagraph = false;
|
|
64
|
+
}
|
|
65
|
+
close = CLOSE_P.exec(xml);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const [full, name] = open;
|
|
69
|
+
const selfClosing = full.endsWith("/>");
|
|
70
|
+
if (name === "p") {
|
|
71
|
+
if (!selfClosing && !inParagraph) {
|
|
72
|
+
inParagraph = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
else if (inParagraph) {
|
|
76
|
+
if (name === "tab") {
|
|
77
|
+
out += "\t";
|
|
78
|
+
}
|
|
79
|
+
else if (name === "br") {
|
|
80
|
+
out += "\n";
|
|
81
|
+
}
|
|
82
|
+
else if (!selfClosing) {
|
|
83
|
+
const endTag = `</w:t>`;
|
|
84
|
+
const end = xml.indexOf(endTag, open.index + full.length);
|
|
85
|
+
const stop = end === -1 ? xml.length : end;
|
|
86
|
+
out += decodeXmlEntities(xml.slice(open.index + full.length, stop));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
open = OPEN_TAG.exec(xml);
|
|
90
|
+
}
|
|
91
|
+
// Unterminated trailing paragraph.
|
|
92
|
+
if (inParagraph)
|
|
93
|
+
out += "\n\n";
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
5
96
|
/**
|
|
6
97
|
* Extract the raw text from a Word (.docx) file buffer.
|
|
7
|
-
*
|
|
98
|
+
*
|
|
99
|
+
* Unzips the archive and reads `word/document.xml` directly. Only that single
|
|
100
|
+
* entry is decompressed, and an inflated-size cap is enforced, so a crafted
|
|
101
|
+
* archive cannot exhaust memory.
|
|
8
102
|
* @param buffer - Raw buffer of the .docx file.
|
|
9
103
|
* @returns The extracted raw text as a string.
|
|
104
|
+
* @throws If the buffer is not a valid ZIP archive or lacks `word/document.xml`.
|
|
10
105
|
*/
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
106
|
+
export function extractDocxText(buffer) {
|
|
107
|
+
let tooLarge = false;
|
|
108
|
+
const entries = unzipSync(new Uint8Array(buffer), {
|
|
109
|
+
filter: (file) => {
|
|
110
|
+
if (file.name !== DOCUMENT_XML)
|
|
111
|
+
return false;
|
|
112
|
+
if (file.originalSize > MAX_DOCUMENT_XML_BYTES) {
|
|
113
|
+
tooLarge = true;
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
if (tooLarge) {
|
|
120
|
+
throw new Error(`DOCX too large: "${DOCUMENT_XML}" exceeds ${MAX_DOCUMENT_XML_BYTES} bytes when decompressed`);
|
|
121
|
+
}
|
|
122
|
+
const doc = entries[DOCUMENT_XML];
|
|
123
|
+
if (!doc) {
|
|
124
|
+
throw new Error(`Not a valid DOCX file: missing "${DOCUMENT_XML}"`);
|
|
125
|
+
}
|
|
126
|
+
return documentXmlToText(Buffer.from(doc).toString("utf8"));
|
|
15
127
|
}
|
|
16
128
|
/**
|
|
17
129
|
* Chunker for Word (.docx) documents.
|
package/dist/content/docx.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { ExtractResult } from "./types.js";
|
|
|
5
5
|
/** Recognized DOCX file extension set. */
|
|
6
6
|
export declare const DOCX_EXTENSIONS: Set<string>;
|
|
7
7
|
/**
|
|
8
|
-
* Extract text content from a DOCX buffer
|
|
8
|
+
* Extract text content from a DOCX buffer.
|
|
9
9
|
* @param _filePath - Unused, retained for API consistency.
|
|
10
10
|
* @param buffer - Raw DOCX file bytes.
|
|
11
11
|
*/
|
package/dist/content/docx.js
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
/** Recognized DOCX file extension set. */
|
|
5
5
|
export const DOCX_EXTENSIONS = new Set([".docx"]);
|
|
6
6
|
/**
|
|
7
|
-
* Extract text content from a DOCX buffer
|
|
7
|
+
* Extract text content from a DOCX buffer.
|
|
8
8
|
* @param _filePath - Unused, retained for API consistency.
|
|
9
9
|
* @param buffer - Raw DOCX file bytes.
|
|
10
10
|
*/
|
|
11
11
|
export async function extract(_filePath, buffer) {
|
|
12
12
|
try {
|
|
13
13
|
const { extractDocxText } = await import("../chunker/docx.js");
|
|
14
|
-
const content =
|
|
14
|
+
const content = extractDocxText(buffer);
|
|
15
15
|
return { content, ok: true };
|
|
16
16
|
}
|
|
17
17
|
catch (err) {
|
package/dist/plugin.d.ts
CHANGED
|
@@ -8,6 +8,47 @@ import { type EmbeddingProvider, type DescriptionProvider, type KeywordIndex, ty
|
|
|
8
8
|
import { type RagConfig } from "./core/config.js";
|
|
9
9
|
import { createEmbedder } from "./embedder/factory.js";
|
|
10
10
|
import { retrieve } from "./retriever/retriever.js";
|
|
11
|
+
/**
|
|
12
|
+
* Load the RAG configuration for a directory, searching in order:
|
|
13
|
+
* opencode-rag.json, .opencode/opencode-rag.json, .opencode/rag.json.
|
|
14
|
+
*
|
|
15
|
+
* The result is cached per directory and re-read whenever the config file
|
|
16
|
+
* changes on disk, so an edited API key or baseUrl does not require a restart.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getConfig(directory: string): Promise<RagConfig>;
|
|
19
|
+
/**
|
|
20
|
+
* Re-read a workspace's config file when it changed on disk.
|
|
21
|
+
*
|
|
22
|
+
* Only a config that this module loaded from a file is refreshed — the cache
|
|
23
|
+
* entry is the proof that the file is the source of truth for that workspace.
|
|
24
|
+
* A caller that supplies its own configuration (tests, embedders) keeps it.
|
|
25
|
+
*
|
|
26
|
+
* Synchronous on purpose: this runs on the hot path (every effective-config
|
|
27
|
+
* lookup) and `loadConfig` is synchronous. The pluggable-chunker side effect
|
|
28
|
+
* (`loadChunkersFromConfig`) mutates a global registry and is therefore
|
|
29
|
+
* re-applied asynchronously — it is not needed by the embedding path this
|
|
30
|
+
* refresh exists to fix.
|
|
31
|
+
*
|
|
32
|
+
* @param directory - Workspace directory whose config should be refreshed.
|
|
33
|
+
* @param fallback - Config to use when the workspace has no file-backed config.
|
|
34
|
+
* @returns The configuration currently in effect.
|
|
35
|
+
*/
|
|
36
|
+
export declare function refreshConfig(directory: string, fallback: RagConfig): RagConfig;
|
|
37
|
+
/**
|
|
38
|
+
* Wrap an embedding provider so it follows the live configuration.
|
|
39
|
+
*
|
|
40
|
+
* The provider is rebuilt whenever the embedding-relevant config changes on
|
|
41
|
+
* disk. Without this, the provider built at plugin setup kept using a stale
|
|
42
|
+
* API key / baseUrl until OpenCode was restarted — which surfaced as a 401
|
|
43
|
+
* that looked like an OpenAI auth error while the CLI worked fine against the
|
|
44
|
+
* very same config file.
|
|
45
|
+
*
|
|
46
|
+
* @param getConfig - Returns the configuration currently in effect.
|
|
47
|
+
* @param create - Factory that builds a provider for a configuration.
|
|
48
|
+
* @param initial - Provider to use until the configuration changes.
|
|
49
|
+
* @returns A provider that tracks the live configuration.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createLiveEmbedder(getConfig: () => RagConfig, create: (cfg: RagConfig) => EmbeddingProvider, initial?: EmbeddingProvider): EmbeddingProvider;
|
|
11
52
|
/** Internal dependencies that can be overridden for testing. */
|
|
12
53
|
type RagPluginDependencies = {
|
|
13
54
|
createEmbedder: typeof createEmbedder;
|
package/dist/plugin.js
CHANGED
|
@@ -33,7 +33,7 @@ import { listQuirks, lintQuirks, recallQuirks, sharedWords, reconcileQuirks } fr
|
|
|
33
33
|
import { autoCaptureQuirks } from "./quirks/auto-capture.js";
|
|
34
34
|
import { createToolErrorInjectionState, selectToolErrorQuirks, formatToolErrorQuirkBlock, toToolErrorCaptureEntry, } from "./quirks/tool-error-recall.js";
|
|
35
35
|
import { buildSystemGuidanceLines } from "./opencode/system-guidance.js";
|
|
36
|
-
import { existsSync, readFileSync, readdirSync, unlinkSync } from "node:fs";
|
|
36
|
+
import { existsSync, readFileSync, readdirSync, statSync, unlinkSync } from "node:fs";
|
|
37
37
|
import path from "node:path";
|
|
38
38
|
/** Maximum entries per session map before oldest entries are evicted. */
|
|
39
39
|
const MAX_SESSION_MAP_SIZE = 50;
|
|
@@ -196,21 +196,46 @@ function indentMultiline(text, indent) {
|
|
|
196
196
|
.map((line) => `${prefix}${line}`)
|
|
197
197
|
.join("\n");
|
|
198
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* mtime of a config file in milliseconds, or -1 when it does not exist.
|
|
201
|
+
*/
|
|
202
|
+
function configFileMtime(configPath) {
|
|
203
|
+
if (!configPath)
|
|
204
|
+
return -1;
|
|
205
|
+
try {
|
|
206
|
+
return statSync(configPath).mtimeMs;
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return -1;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
199
212
|
/**
|
|
200
213
|
* Load the RAG configuration for a directory, searching in order:
|
|
201
214
|
* opencode-rag.json, .opencode/opencode-rag.json, .opencode/rag.json.
|
|
202
|
-
*
|
|
215
|
+
*
|
|
216
|
+
* The result is cached per directory and re-read whenever the config file
|
|
217
|
+
* changes on disk, so an edited API key or baseUrl does not require a restart.
|
|
203
218
|
*/
|
|
204
|
-
async function getConfig(directory) {
|
|
205
|
-
const cached = configCache.get(directory);
|
|
206
|
-
if (cached)
|
|
207
|
-
return cached;
|
|
219
|
+
export async function getConfig(directory) {
|
|
208
220
|
const configPath = findConfigFile(directory);
|
|
221
|
+
const mtimeMs = configFileMtime(configPath);
|
|
222
|
+
const cached = configCache.get(directory);
|
|
223
|
+
if (cached && cached.configPath === configPath && cached.mtimeMs === mtimeMs) {
|
|
224
|
+
return cached.cfg;
|
|
225
|
+
}
|
|
209
226
|
if (configPath) {
|
|
210
227
|
try {
|
|
211
228
|
const cfg = loadConfig(configPath);
|
|
212
229
|
await loadChunkersFromConfig(cfg, path.dirname(configPath));
|
|
213
|
-
|
|
230
|
+
const hadKey = !!cfg.embedding.apiKey;
|
|
231
|
+
resolveApiKey(cfg, directory);
|
|
232
|
+
if (!hadKey && cfg.embedding.apiKey) {
|
|
233
|
+
appendDebugLog(path.resolve(directory, ".opencode", "opencode-rag.log"), {
|
|
234
|
+
scope: "config",
|
|
235
|
+
message: `Resolved embedding API key from ${process.env.OPENAI_API_KEY ? "OPENAI_API_KEY env var" : "OpenCode provider config"}`,
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
configCache.set(directory, { cfg, configPath, mtimeMs });
|
|
214
239
|
return cfg;
|
|
215
240
|
}
|
|
216
241
|
catch (err) {
|
|
@@ -221,8 +246,109 @@ async function getConfig(directory) {
|
|
|
221
246
|
});
|
|
222
247
|
}
|
|
223
248
|
}
|
|
224
|
-
|
|
225
|
-
|
|
249
|
+
// Deep-clone so a later resolveApiKey() cannot mutate the shared
|
|
250
|
+
// DEFAULT_CONFIG singleton.
|
|
251
|
+
const cfg = structuredClone(DEFAULT_CONFIG);
|
|
252
|
+
configCache.set(directory, { cfg, configPath, mtimeMs });
|
|
253
|
+
return cfg;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Re-read a workspace's config file when it changed on disk.
|
|
257
|
+
*
|
|
258
|
+
* Only a config that this module loaded from a file is refreshed — the cache
|
|
259
|
+
* entry is the proof that the file is the source of truth for that workspace.
|
|
260
|
+
* A caller that supplies its own configuration (tests, embedders) keeps it.
|
|
261
|
+
*
|
|
262
|
+
* Synchronous on purpose: this runs on the hot path (every effective-config
|
|
263
|
+
* lookup) and `loadConfig` is synchronous. The pluggable-chunker side effect
|
|
264
|
+
* (`loadChunkersFromConfig`) mutates a global registry and is therefore
|
|
265
|
+
* re-applied asynchronously — it is not needed by the embedding path this
|
|
266
|
+
* refresh exists to fix.
|
|
267
|
+
*
|
|
268
|
+
* @param directory - Workspace directory whose config should be refreshed.
|
|
269
|
+
* @param fallback - Config to use when the workspace has no file-backed config.
|
|
270
|
+
* @returns The configuration currently in effect.
|
|
271
|
+
*/
|
|
272
|
+
export function refreshConfig(directory, fallback) {
|
|
273
|
+
const cached = configCache.get(directory);
|
|
274
|
+
if (!cached) {
|
|
275
|
+
// No file-backed config for this workspace — the caller's config stands.
|
|
276
|
+
return fallback;
|
|
277
|
+
}
|
|
278
|
+
const configPath = findConfigFile(directory);
|
|
279
|
+
const mtimeMs = configFileMtime(configPath);
|
|
280
|
+
if (cached.configPath === configPath && cached.mtimeMs === mtimeMs) {
|
|
281
|
+
return cached.cfg;
|
|
282
|
+
}
|
|
283
|
+
if (!configPath) {
|
|
284
|
+
// The config file was removed — keep serving the last known config.
|
|
285
|
+
return cached.cfg;
|
|
286
|
+
}
|
|
287
|
+
try {
|
|
288
|
+
const cfg = loadConfig(configPath);
|
|
289
|
+
resolveApiKey(cfg, directory);
|
|
290
|
+
void loadChunkersFromConfig(cfg, path.dirname(configPath)).catch(() => { });
|
|
291
|
+
configCache.set(directory, { cfg, configPath, mtimeMs });
|
|
292
|
+
return cfg;
|
|
293
|
+
}
|
|
294
|
+
catch (err) {
|
|
295
|
+
appendDebugLog(path.resolve(directory, ".opencode", "opencode-rag.log"), {
|
|
296
|
+
scope: "config",
|
|
297
|
+
message: `Failed to reload config from ${configPath}`,
|
|
298
|
+
error: err,
|
|
299
|
+
});
|
|
300
|
+
return cached.cfg;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Identity of the embedding-relevant configuration. A change means the
|
|
305
|
+
* embedding provider has to be rebuilt (different key, baseUrl, model, …).
|
|
306
|
+
*/
|
|
307
|
+
function embeddingSignature(cfg) {
|
|
308
|
+
const e = cfg.embedding;
|
|
309
|
+
return [
|
|
310
|
+
e.provider,
|
|
311
|
+
e.baseUrl ?? "",
|
|
312
|
+
e.model,
|
|
313
|
+
e.apiKey ?? "",
|
|
314
|
+
e.timeoutMs ?? "",
|
|
315
|
+
JSON.stringify(e.proxy ?? null),
|
|
316
|
+
].join("\u0000");
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Wrap an embedding provider so it follows the live configuration.
|
|
320
|
+
*
|
|
321
|
+
* The provider is rebuilt whenever the embedding-relevant config changes on
|
|
322
|
+
* disk. Without this, the provider built at plugin setup kept using a stale
|
|
323
|
+
* API key / baseUrl until OpenCode was restarted — which surfaced as a 401
|
|
324
|
+
* that looked like an OpenAI auth error while the CLI worked fine against the
|
|
325
|
+
* very same config file.
|
|
326
|
+
*
|
|
327
|
+
* @param getConfig - Returns the configuration currently in effect.
|
|
328
|
+
* @param create - Factory that builds a provider for a configuration.
|
|
329
|
+
* @param initial - Provider to use until the configuration changes.
|
|
330
|
+
* @returns A provider that tracks the live configuration.
|
|
331
|
+
*/
|
|
332
|
+
export function createLiveEmbedder(getConfig, create, initial) {
|
|
333
|
+
let provider = initial ?? create(getConfig());
|
|
334
|
+
let signature = embeddingSignature(getConfig());
|
|
335
|
+
function current() {
|
|
336
|
+
const cfg = getConfig();
|
|
337
|
+
const sig = embeddingSignature(cfg);
|
|
338
|
+
if (sig !== signature) {
|
|
339
|
+
provider = create(cfg);
|
|
340
|
+
signature = sig;
|
|
341
|
+
}
|
|
342
|
+
return provider;
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
get name() {
|
|
346
|
+
return current().name;
|
|
347
|
+
},
|
|
348
|
+
async embed(texts, purpose) {
|
|
349
|
+
return current().embed(texts, purpose);
|
|
350
|
+
},
|
|
351
|
+
};
|
|
226
352
|
}
|
|
227
353
|
/**
|
|
228
354
|
* Format retrieval results into a Markdown context block for LLM injection.
|
|
@@ -464,7 +590,6 @@ export function createRagHooks(options) {
|
|
|
464
590
|
...defaultDependencies,
|
|
465
591
|
...options.dependencies,
|
|
466
592
|
};
|
|
467
|
-
const embedder = options.embedder ?? dependencies.createEmbedder(options.cfg);
|
|
468
593
|
const configuredDimension = options.cfg.embedding.vectorDimension;
|
|
469
594
|
const store = options.store ?? dependencies.createStore(options.storePath, configuredDimension && configuredDimension > 0 ? configuredDimension : 384, options.cfg);
|
|
470
595
|
const keywordIndex = options.keywordIndex;
|
|
@@ -487,10 +612,20 @@ export function createRagHooks(options) {
|
|
|
487
612
|
if (lastEffectiveCfg && Date.now() - lastEffectiveCfgTime < 1000) {
|
|
488
613
|
return lastEffectiveCfg;
|
|
489
614
|
}
|
|
490
|
-
|
|
615
|
+
// Re-read the config file when it changed on disk, so an edited API key /
|
|
616
|
+
// baseUrl / model takes effect without restarting OpenCode.
|
|
617
|
+
lastEffectiveCfg = applyRuntimeOverrides(refreshConfig(options.worktree, options.cfg), cachedOverrides);
|
|
491
618
|
lastEffectiveCfgTime = Date.now();
|
|
492
619
|
return lastEffectiveCfg;
|
|
493
620
|
}
|
|
621
|
+
// The embedder follows the live configuration: when the config file changes
|
|
622
|
+
// (new API key, baseUrl or model) the provider is rebuilt instead of
|
|
623
|
+
// keeping the one built at plugin setup.
|
|
624
|
+
//
|
|
625
|
+
// Created *after* the override state above on purpose — the facade resolves
|
|
626
|
+
// the effective config eagerly, which would otherwise hit the temporal dead
|
|
627
|
+
// zone of those `let` bindings.
|
|
628
|
+
const embedder = options.embedder ?? createLiveEmbedder(() => getEffectiveCfg(), (cfg) => dependencies.createEmbedder(cfg));
|
|
494
629
|
// Session-level caches for lazy retrieval
|
|
495
630
|
const sessionLastMessage = new Map();
|
|
496
631
|
const sessionRetrievalCache = new Map();
|
|
@@ -1463,6 +1598,10 @@ async function loadKeywordIndex(storePath, logFilePath, logLevel) {
|
|
|
1463
1598
|
* @returns A complete set of OpenCode hooks for RAG functionality.
|
|
1464
1599
|
*/
|
|
1465
1600
|
export const ragPlugin = async (input, _options) => {
|
|
1601
|
+
// Drop the cached config before loading: a plugin reload must pick up a
|
|
1602
|
+
// changed config file, and the fresh cache entry is what enables the live
|
|
1603
|
+
// refresh (refreshConfig only refreshes file-backed configs).
|
|
1604
|
+
configCache.delete(input.directory);
|
|
1466
1605
|
const cfg = await getConfig(input.directory);
|
|
1467
1606
|
const logFilePath = path.resolve(input.directory, resolveLogConfig(cfg).logFilePath);
|
|
1468
1607
|
const logLevel = resolveLogConfig(cfg).level;
|
|
@@ -1537,6 +1676,11 @@ export const ragPlugin = async (input, _options) => {
|
|
|
1537
1676
|
// When the probe fails, prefer the existing store's schema over the 384
|
|
1538
1677
|
// default — a transient outage must not downgrade the store's dimension.
|
|
1539
1678
|
const embedder = createEmbedder(effectiveCfg);
|
|
1679
|
+
// The hooks and the background watcher share one embedder that follows the
|
|
1680
|
+
// live config: when the config file changes (new API key, baseUrl or model)
|
|
1681
|
+
// the provider is rebuilt instead of keeping the one built at startup. The
|
|
1682
|
+
// raw `embedder` above stays the probe target — it is used once.
|
|
1683
|
+
const liveEmbedder = createLiveEmbedder(() => refreshConfig(input.directory, effectiveCfg), (cfg) => createEmbedder(cfg), embedder);
|
|
1540
1684
|
let vectorDimension = effectiveCfg.embedding.vectorDimension;
|
|
1541
1685
|
if (vectorDimension && vectorDimension > 0) {
|
|
1542
1686
|
appendDebugLog(logFilePath, {
|
|
@@ -1592,7 +1736,7 @@ export const ragPlugin = async (input, _options) => {
|
|
|
1592
1736
|
// or keyword index (full rebuilds drop quirk chunks — see reconcileQuirks).
|
|
1593
1737
|
// Fire-and-forget: must never block plugin startup, and the embedder may be
|
|
1594
1738
|
// offline. The background watcher re-runs this at every pass end.
|
|
1595
|
-
void reconcileQuirks({ embedder, store, keywordIndex, cfg: effectiveCfg, storePath })
|
|
1739
|
+
void reconcileQuirks({ embedder: liveEmbedder, store, keywordIndex, cfg: effectiveCfg, storePath })
|
|
1596
1740
|
.then((r) => {
|
|
1597
1741
|
if (r.restoredToStore > 0 || r.addedToKeywordIndex > 0 || r.removedOrphans > 0) {
|
|
1598
1742
|
appendDebugLog(logFilePath, {
|
|
@@ -1619,7 +1763,7 @@ export const ragPlugin = async (input, _options) => {
|
|
|
1619
1763
|
logFilePath,
|
|
1620
1764
|
logLevel,
|
|
1621
1765
|
worktree: input.directory,
|
|
1622
|
-
embedder,
|
|
1766
|
+
embedder: liveEmbedder,
|
|
1623
1767
|
store,
|
|
1624
1768
|
keywordIndex,
|
|
1625
1769
|
descriptionProvider,
|
|
@@ -1632,7 +1776,7 @@ export const ragPlugin = async (input, _options) => {
|
|
|
1632
1776
|
storePath,
|
|
1633
1777
|
config: effectiveCfg,
|
|
1634
1778
|
store,
|
|
1635
|
-
embedder,
|
|
1779
|
+
embedder: liveEmbedder,
|
|
1636
1780
|
logFilePath,
|
|
1637
1781
|
logLevel,
|
|
1638
1782
|
keywordIndex,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-rag-plugin",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.2",
|
|
4
4
|
"description": "OpenCode plugin for local-first RAG-based semantic code search",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/plugin-entry.js",
|
|
@@ -112,8 +112,8 @@
|
|
|
112
112
|
"apache-arrow": "18.1.0",
|
|
113
113
|
"chokidar": "^4.0.3",
|
|
114
114
|
"commander": "^13.1.0",
|
|
115
|
+
"fflate": "^0.8.3",
|
|
115
116
|
"js-tiktoken": "^1.0.21",
|
|
116
|
-
"mammoth": "^1.12.0",
|
|
117
117
|
"minimatch": "^8.0.7",
|
|
118
118
|
"p-limit": "^7.3.0",
|
|
119
119
|
"pdfjs-dist": "^6.3.289",
|
|
@@ -149,7 +149,8 @@
|
|
|
149
149
|
"uuid": "^11.1.1",
|
|
150
150
|
"web-tree-sitter": "$web-tree-sitter",
|
|
151
151
|
"@babel/core": "^7.29.7",
|
|
152
|
-
"@opentelemetry/core": "^2.8.0"
|
|
152
|
+
"@opentelemetry/core": "^2.8.0",
|
|
153
|
+
"seroval": "^1.6.8"
|
|
153
154
|
},
|
|
154
155
|
"allowScripts": {
|
|
155
156
|
"opencode-rag-plugin": true,
|