@modusensus/dsh-mneme 0.6.7 → 0.6.8

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.
Files changed (117) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +463 -463
  3. package/cordis.patch.yml +15 -15
  4. package/lib/api.js +783 -783
  5. package/lib/client.js +1757 -1757
  6. package/lib/commands.js +64 -64
  7. package/lib/config.js +288 -288
  8. package/lib/dream/clustering.js +118 -118
  9. package/lib/dream/decisions.js +439 -439
  10. package/lib/dream/sleep.js +561 -554
  11. package/lib/dream/tag-extractor.js +156 -156
  12. package/lib/dream.js +935 -929
  13. package/lib/embedding.js +154 -154
  14. package/lib/entities/extractor.js +242 -242
  15. package/lib/hot-memory.js +53 -53
  16. package/lib/index.js +361 -361
  17. package/lib/inject.js +208 -208
  18. package/lib/local-embedder.js +282 -282
  19. package/lib/mirror.js +170 -170
  20. package/lib/parser/tag.js +59 -59
  21. package/lib/parser/wiki-link.js +38 -38
  22. package/lib/quality-filter.js +123 -123
  23. package/lib/reranker.js +218 -218
  24. package/lib/search/adaptive.js +22 -22
  25. package/lib/search/bm25.js +96 -96
  26. package/lib/search/tag-boost.js +61 -61
  27. package/lib/service.js +1726 -1726
  28. package/lib/settings.js +172 -172
  29. package/lib/store.js +2238 -2238
  30. package/lib/summarize.js +236 -236
  31. package/lib/tools.js +290 -290
  32. package/lib/vector-index.js +116 -116
  33. package/package.json +80 -80
  34. package/scripts/benchmark-embed.js +201 -201
  35. package/scripts/benchmark-recall.js +133 -133
  36. package/scripts/benchmark-rerank.js +166 -166
  37. package/scripts/e2e-dsh.js +218 -218
  38. package/scripts/stress-dsh.js +255 -255
  39. package/scripts/sync-lib.js +52 -52
  40. package/src/api.js +783 -783
  41. package/src/commands.js +64 -64
  42. package/src/config.js +288 -288
  43. package/src/dream/clustering.js +118 -118
  44. package/src/dream/decisions.js +439 -439
  45. package/src/dream/sleep.js +561 -554
  46. package/src/dream/tag-extractor.js +156 -156
  47. package/src/dream.js +935 -929
  48. package/src/embedding.js +154 -154
  49. package/src/entities/extractor.js +242 -242
  50. package/src/hot-memory.js +53 -53
  51. package/src/index.js +361 -361
  52. package/src/inject.js +208 -208
  53. package/src/local-embedder.js +282 -282
  54. package/src/mirror.js +170 -170
  55. package/src/parser/tag.js +59 -59
  56. package/src/parser/wiki-link.js +38 -38
  57. package/src/quality-filter.js +123 -123
  58. package/src/reranker.js +218 -218
  59. package/src/search/adaptive.js +22 -22
  60. package/src/search/bm25.js +96 -96
  61. package/src/search/tag-boost.js +61 -61
  62. package/src/service.js +1726 -1726
  63. package/src/settings.js +172 -172
  64. package/src/store.js +2238 -2238
  65. package/src/summarize.js +236 -236
  66. package/src/tools.js +290 -290
  67. package/src/vector-index.js +116 -116
  68. package/test/api.test.js +594 -594
  69. package/test/audit.test.js +448 -448
  70. package/test/benchmark.test.js +35 -35
  71. package/test/boundary-v0625.test.js +82 -82
  72. package/test/client.test.js +368 -368
  73. package/test/clustering.test.js +100 -100
  74. package/test/commands.test.js +69 -69
  75. package/test/config.test.js +50 -50
  76. package/test/conflict-freeze.test.js +290 -290
  77. package/test/directory.test.js +134 -134
  78. package/test/dream.test.js +903 -901
  79. package/test/entities.test.js +522 -522
  80. package/test/epistemic.test.js +298 -298
  81. package/test/fnew-0112.test.js +311 -311
  82. package/test/fnew-03.test.js +422 -422
  83. package/test/graph-api.test.js +175 -175
  84. package/test/helpers/dream-mock.js +82 -82
  85. package/test/hot-memory.test.js +174 -174
  86. package/test/inject.test.js +103 -103
  87. package/test/llm-audit.test.js +279 -279
  88. package/test/local-embedder.test.js +227 -227
  89. package/test/mirror-dirty.test.js +424 -424
  90. package/test/mirror-edit-digest.test.js +187 -187
  91. package/test/mirror-generation.test.js +499 -499
  92. package/test/mirror.test.js +249 -249
  93. package/test/normalize-decisions.test.js +120 -120
  94. package/test/peer-blockers.test.js +190 -190
  95. package/test/policy-epoch.test.js +259 -259
  96. package/test/provenance.test.js +103 -103
  97. package/test/quality-filter.test.js +118 -118
  98. package/test/reasoning-effort.test.js +199 -199
  99. package/test/recall-evals.test.js +235 -235
  100. package/test/recall-layer.test.js +315 -315
  101. package/test/receipt-chain.test.js +451 -451
  102. package/test/reflection.test.js +226 -226
  103. package/test/reranker.test.js +240 -240
  104. package/test/search-fusion.test.js +90 -90
  105. package/test/semantic.test.js +124 -124
  106. package/test/service-search.test.js +199 -199
  107. package/test/service.test.js +435 -435
  108. package/test/settings.test.js +118 -118
  109. package/test/sleep.test.js +365 -365
  110. package/test/store.test.js +436 -436
  111. package/test/stress.test.js +209 -209
  112. package/test/summarize.test.js +191 -191
  113. package/test/tag-boost.test.js +125 -125
  114. package/test/tag.test.js +312 -312
  115. package/test/tools.test.js +285 -285
  116. package/test/vector-index.test.js +221 -221
  117. package/test/wiki-link.test.js +332 -332
@@ -1,282 +1,282 @@
1
- // Fully-local embedding backends for dsh-mneme: ONNX via transformers.js,
2
- // Ollama's HTTP API, and the OpenAI-compatible HTTP API (extracted from the
3
- // old embedding.js logic). All classes share one interface so the orchestrator
4
- // can pick a backend by provider name and degrade gracefully on failure.
5
- // Methods throw on error — the caller decides the fallback chain.
6
- import os from "node:os";
7
- import path from "node:path";
8
-
9
- const DEFAULT_TIMEOUT_MS = 15000;
10
-
11
- /** djb2 — stable, fast fingerprint for a provider/model string. */
12
- function hashString(s) {
13
- let h = 5381;
14
- for (let i = 0; i < s.length; i++) h = ((h << 5) + h + s.charCodeAt(i)) >>> 0;
15
- return h.toString(16);
16
- }
17
-
18
- /** Full provider+model fingerprint used for index-consistency checks. */
19
- function modelHash(model) {
20
- return `${model}#${hashString(model)}`;
21
- }
22
-
23
- /** Lazy default loader: dynamic import keeps module load cheap. */
24
- async function defaultPipelineLoader(task, model, options) {
25
- const { env, pipeline } = await import("@huggingface/transformers");
26
- // issue #13: transformers.js's get_tokenizer_files() drops the caller's
27
- // cache_dir when it pre-checks tokenizer_config.json metadata, so the HEAD
28
- // request falls back to env.cacheDir and hits the network even when the
29
- // model is fully cached locally. Mirroring the cache_dir onto env.cacheDir
30
- // makes that pre-check resolve locally too — fully offline loading.
31
- if (options?.cache_dir) env.cacheDir = options.cache_dir;
32
- return pipeline(task, model, options);
33
- }
34
-
35
- /** Flatten a transformers.js Tensor [batch, dim] into number[][]. */
36
- function tensorToRows(tensor) {
37
- const { data, dims } = tensor;
38
- const rowLen = dims[dims.length - 1] || 0;
39
- const rows = [];
40
- for (let i = 0; i < data.length; i += rowLen) {
41
- rows.push(Array.from(data.subarray(i, i + rowLen)));
42
- }
43
- // Single-text input may come back without the batch axis.
44
- if (rows.length === 0 && rowLen > 0) rows.push(Array.from(data));
45
- return rows;
46
- }
47
-
48
- /**
49
- * ONNX text embedder backed by transformers.js (onnxruntime-node underneath).
50
- * Runs fully offline with mean pooling + L2 normalization for BERT-style
51
- * models like bge-small-zh. `engineFactory` is injectable for tests.
52
- */
53
- export class LocalEmbedder {
54
- constructor(opts = {}) {
55
- this.model = opts.model || "Xenova/bge-small-zh-v1.5";
56
- this._dimension = opts.dimension || 512;
57
- this.device = opts.device || "cpu";
58
- this.batchSize = opts.batchSize || 8;
59
- this.cacheDir =
60
- String(opts.cacheDir ?? "").trim() ||
61
- path.join(os.homedir(), ".dsh", "mneme", "models");
62
- this.useDtype = opts.useDtype || "q8";
63
- this.logger = opts.logger ?? null;
64
- // Test hook: replace the pipeline factory without touching modules.
65
- this.engineFactory = opts.engineFactory || defaultPipelineLoader;
66
- this.extractor = null;
67
- // issue #6: readiness flag for the service's scheduleEmbed gate. False until
68
- // init() succeeds, so "ready" in embedder is observable even pre-init.
69
- this.ready = false;
70
- }
71
-
72
- /** Load the model; throws when it cannot be loaded. Idempotent. */
73
- async init() {
74
- if (this.extractor) return this; // already initialized: no-op
75
- const options = {
76
- dtype: this.useDtype,
77
- device: this.device
78
- };
79
- if (this.cacheDir) options.cache_dir = this.cacheDir;
80
- this.extractor = await this.engineFactory("feature-extraction", this.model, options);
81
- this.ready = true; // service reads this to flush queued re-embeds
82
- this.logger?.info?.(
83
- `[dsh-mneme] local embedder ready: ${this.model} (dim=${this._dimension}, device=${this.device})`
84
- );
85
- return this;
86
- }
87
-
88
- /** Embed many texts with mean pooling; chunks at batchSize. */
89
- async embed(texts) {
90
- if (!Array.isArray(texts)) throw new TypeError("embed expects an array of strings");
91
- if (!this.extractor) throw new Error("LocalEmbedder not initialized");
92
- const out = [];
93
- for (let i = 0; i < texts.length; i += this.batchSize) {
94
- const chunk = texts.slice(i, i + this.batchSize);
95
- const tensor = await this.extractor(chunk, { pooling: "mean", normalize: true });
96
- out.push(...tensorToRows(tensor));
97
- }
98
- return out;
99
- }
100
-
101
- async embedSingle(text) {
102
- const rows = await this.embed([String(text)]);
103
- return rows[0];
104
- }
105
-
106
- get dimension() {
107
- return this._dimension;
108
- }
109
-
110
- get modelHash() {
111
- return modelHash(this.model);
112
- }
113
-
114
- dispose() {
115
- try {
116
- this.extractor?.dispose?.();
117
- } catch {
118
- // best-effort: some engines free resources on GC
119
- }
120
- this.extractor = null;
121
- this.ready = false;
122
- }
123
- }
124
-
125
- /** Chunk texts into batches of at most `size`. */
126
- function chunk(texts, size) {
127
- const out = [];
128
- for (let i = 0; i < texts.length; i += size) out.push(texts.slice(i, i + size));
129
- return out;
130
- }
131
-
132
- /**
133
- * Ollama embedder over its native HTTP API. `dimension` is inferred from the
134
- * first response. init() verifies reachability and that the model exists.
135
- */
136
- export class OllamaEmbedder {
137
- constructor(opts = {}) {
138
- this.baseUrl = String(opts.baseUrl ?? "http://localhost:11434").trim().replace(/\/+$/, "");
139
- this.model = String(opts.model ?? "nomic-embed-text").trim();
140
- this.logger = opts.logger ?? null;
141
- this._dimension = null;
142
- }
143
-
144
- async _post(body) {
145
- return fetch(`${this.baseUrl}/api/embeddings`, {
146
- method: "POST",
147
- headers: { "Content-Type": "application/json" },
148
- body: JSON.stringify(body),
149
- signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS)
150
- });
151
- }
152
-
153
- /** Probe the server with a tiny prompt; throws when unreachable/missing. */
154
- async init() {
155
- const res = await this._post({ model: this.model, prompt: "ping" });
156
- if (!res.ok) throw new Error(`Ollama ${this.model} unavailable: HTTP ${res.status}`);
157
- const body = await res.json();
158
- if (!Array.isArray(body?.embedding)) throw new Error(`Ollama ${this.model} returned no embedding`);
159
- this._dimension = body.embedding.length;
160
- this.logger?.info?.(
161
- `[dsh-mneme] ollama embedder ready: ${this.model} (dim=${this._dimension})`
162
- );
163
- return this;
164
- }
165
-
166
- async embed(texts) {
167
- if (!Array.isArray(texts)) throw new TypeError("embed expects an array of strings");
168
- const out = [];
169
- for (const text of texts) out.push(await this.embedSingle(text));
170
- return out;
171
- }
172
-
173
- async embedSingle(text) {
174
- const res = await this._post({ model: this.model, prompt: String(text).slice(0, 8000) });
175
- if (!res.ok) throw new Error(`Ollama embed failed: HTTP ${res.status}`);
176
- const body = await res.json();
177
- const vec = body?.embedding;
178
- if (!Array.isArray(vec) || !vec.length) throw new Error("Ollama returned no embedding");
179
- if (this._dimension == null) this._dimension = vec.length;
180
- return Array.from(vec);
181
- }
182
-
183
- get dimension() {
184
- return this._dimension ?? 0;
185
- }
186
-
187
- get modelHash() {
188
- return modelHash(this.model);
189
- }
190
-
191
- dispose() {
192
- this._dimension = null;
193
- }
194
- }
195
-
196
- /**
197
- * OpenAI-compatible embedder (OpenAI, SiliconFlow, Zhipu, local proxies).
198
- * Backward-compatible behavior lifted from embedding.js, but batchable and
199
- * throwing on failure instead of returning null.
200
- */
201
- export class OpenAIEmbedder {
202
- constructor(opts = {}) {
203
- this.baseUrl = String(opts.baseUrl ?? "").trim().replace(/\/+$/, "");
204
- this.apiKey = String(opts.apiKey ?? "").trim();
205
- this.model = String(opts.model ?? "").trim();
206
- this.timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
207
- this.logger = opts.logger ?? null;
208
- this._dimension = null;
209
- // Accept both "https://host/v1" and a full path ending in /embeddings.
210
- this._url = /\/embeddings$/i.test(this.baseUrl)
211
- ? this.baseUrl
212
- : this.baseUrl ? `${this.baseUrl}/embeddings` : "";
213
- }
214
-
215
- async init() {
216
- if (!this._url || !this.apiKey || !this.model) {
217
- throw new Error("OpenAI embedder requires baseUrl, apiKey and model");
218
- }
219
- this.logger?.info?.(`[dsh-mneme] openai embedder ready: ${this.model}`);
220
- return this;
221
- }
222
-
223
- async embed(texts) {
224
- if (!Array.isArray(texts)) throw new TypeError("embed expects an array of strings");
225
- const out = [];
226
- for (const batch of chunk(texts, 32)) {
227
- const res = await fetch(this._url, {
228
- method: "POST",
229
- headers: {
230
- "Content-Type": "application/json",
231
- "Authorization": `Bearer ${this.apiKey}`
232
- },
233
- body: JSON.stringify({ model: this.model, input: batch.map((t) => String(t).slice(0, 8000)) }),
234
- signal: AbortSignal.timeout(this.timeoutMs)
235
- });
236
- if (!res.ok) throw new Error(`Embedding API failed: HTTP ${res.status}`);
237
- const body = await res.json();
238
- const list = body?.data;
239
- if (!Array.isArray(list) || list.length !== batch.length) {
240
- throw new Error("Embedding API returned unexpected payload");
241
- }
242
- for (const item of list) {
243
- const vec = item?.embedding;
244
- if (!Array.isArray(vec) || !vec.length) throw new Error("Embedding API returned empty vector");
245
- if (this._dimension == null) this._dimension = vec.length;
246
- out.push(Array.from(vec));
247
- }
248
- }
249
- return out;
250
- }
251
-
252
- async embedSingle(text) {
253
- const rows = await this.embed([String(text)]);
254
- return rows[0];
255
- }
256
-
257
- get dimension() {
258
- return this._dimension ?? 0;
259
- }
260
-
261
- get modelHash() {
262
- return modelHash(this.model);
263
- }
264
-
265
- dispose() {
266
- this._dimension = null;
267
- }
268
- }
269
-
270
- /** Pick a backend instance by provider name. Throws on unknown providers. */
271
- export function createEmbedderByProvider(provider, opts) {
272
- switch (String(provider ?? "").toLowerCase()) {
273
- case "local":
274
- return new LocalEmbedder(opts);
275
- case "ollama":
276
- return new OllamaEmbedder(opts);
277
- case "openai":
278
- return new OpenAIEmbedder(opts);
279
- default:
280
- throw new Error(`Unknown embedding provider: ${provider}`);
281
- }
282
- }
1
+ // Fully-local embedding backends for dsh-mneme: ONNX via transformers.js,
2
+ // Ollama's HTTP API, and the OpenAI-compatible HTTP API (extracted from the
3
+ // old embedding.js logic). All classes share one interface so the orchestrator
4
+ // can pick a backend by provider name and degrade gracefully on failure.
5
+ // Methods throw on error — the caller decides the fallback chain.
6
+ import os from "node:os";
7
+ import path from "node:path";
8
+
9
+ const DEFAULT_TIMEOUT_MS = 15000;
10
+
11
+ /** djb2 — stable, fast fingerprint for a provider/model string. */
12
+ function hashString(s) {
13
+ let h = 5381;
14
+ for (let i = 0; i < s.length; i++) h = ((h << 5) + h + s.charCodeAt(i)) >>> 0;
15
+ return h.toString(16);
16
+ }
17
+
18
+ /** Full provider+model fingerprint used for index-consistency checks. */
19
+ function modelHash(model) {
20
+ return `${model}#${hashString(model)}`;
21
+ }
22
+
23
+ /** Lazy default loader: dynamic import keeps module load cheap. */
24
+ async function defaultPipelineLoader(task, model, options) {
25
+ const { env, pipeline } = await import("@huggingface/transformers");
26
+ // issue #13: transformers.js's get_tokenizer_files() drops the caller's
27
+ // cache_dir when it pre-checks tokenizer_config.json metadata, so the HEAD
28
+ // request falls back to env.cacheDir and hits the network even when the
29
+ // model is fully cached locally. Mirroring the cache_dir onto env.cacheDir
30
+ // makes that pre-check resolve locally too — fully offline loading.
31
+ if (options?.cache_dir) env.cacheDir = options.cache_dir;
32
+ return pipeline(task, model, options);
33
+ }
34
+
35
+ /** Flatten a transformers.js Tensor [batch, dim] into number[][]. */
36
+ function tensorToRows(tensor) {
37
+ const { data, dims } = tensor;
38
+ const rowLen = dims[dims.length - 1] || 0;
39
+ const rows = [];
40
+ for (let i = 0; i < data.length; i += rowLen) {
41
+ rows.push(Array.from(data.subarray(i, i + rowLen)));
42
+ }
43
+ // Single-text input may come back without the batch axis.
44
+ if (rows.length === 0 && rowLen > 0) rows.push(Array.from(data));
45
+ return rows;
46
+ }
47
+
48
+ /**
49
+ * ONNX text embedder backed by transformers.js (onnxruntime-node underneath).
50
+ * Runs fully offline with mean pooling + L2 normalization for BERT-style
51
+ * models like bge-small-zh. `engineFactory` is injectable for tests.
52
+ */
53
+ export class LocalEmbedder {
54
+ constructor(opts = {}) {
55
+ this.model = opts.model || "Xenova/bge-small-zh-v1.5";
56
+ this._dimension = opts.dimension || 512;
57
+ this.device = opts.device || "cpu";
58
+ this.batchSize = opts.batchSize || 8;
59
+ this.cacheDir =
60
+ String(opts.cacheDir ?? "").trim() ||
61
+ path.join(os.homedir(), ".dsh", "mneme", "models");
62
+ this.useDtype = opts.useDtype || "q8";
63
+ this.logger = opts.logger ?? null;
64
+ // Test hook: replace the pipeline factory without touching modules.
65
+ this.engineFactory = opts.engineFactory || defaultPipelineLoader;
66
+ this.extractor = null;
67
+ // issue #6: readiness flag for the service's scheduleEmbed gate. False until
68
+ // init() succeeds, so "ready" in embedder is observable even pre-init.
69
+ this.ready = false;
70
+ }
71
+
72
+ /** Load the model; throws when it cannot be loaded. Idempotent. */
73
+ async init() {
74
+ if (this.extractor) return this; // already initialized: no-op
75
+ const options = {
76
+ dtype: this.useDtype,
77
+ device: this.device
78
+ };
79
+ if (this.cacheDir) options.cache_dir = this.cacheDir;
80
+ this.extractor = await this.engineFactory("feature-extraction", this.model, options);
81
+ this.ready = true; // service reads this to flush queued re-embeds
82
+ this.logger?.info?.(
83
+ `[dsh-mneme] local embedder ready: ${this.model} (dim=${this._dimension}, device=${this.device})`
84
+ );
85
+ return this;
86
+ }
87
+
88
+ /** Embed many texts with mean pooling; chunks at batchSize. */
89
+ async embed(texts) {
90
+ if (!Array.isArray(texts)) throw new TypeError("embed expects an array of strings");
91
+ if (!this.extractor) throw new Error("LocalEmbedder not initialized");
92
+ const out = [];
93
+ for (let i = 0; i < texts.length; i += this.batchSize) {
94
+ const chunk = texts.slice(i, i + this.batchSize);
95
+ const tensor = await this.extractor(chunk, { pooling: "mean", normalize: true });
96
+ out.push(...tensorToRows(tensor));
97
+ }
98
+ return out;
99
+ }
100
+
101
+ async embedSingle(text) {
102
+ const rows = await this.embed([String(text)]);
103
+ return rows[0];
104
+ }
105
+
106
+ get dimension() {
107
+ return this._dimension;
108
+ }
109
+
110
+ get modelHash() {
111
+ return modelHash(this.model);
112
+ }
113
+
114
+ dispose() {
115
+ try {
116
+ this.extractor?.dispose?.();
117
+ } catch {
118
+ // best-effort: some engines free resources on GC
119
+ }
120
+ this.extractor = null;
121
+ this.ready = false;
122
+ }
123
+ }
124
+
125
+ /** Chunk texts into batches of at most `size`. */
126
+ function chunk(texts, size) {
127
+ const out = [];
128
+ for (let i = 0; i < texts.length; i += size) out.push(texts.slice(i, i + size));
129
+ return out;
130
+ }
131
+
132
+ /**
133
+ * Ollama embedder over its native HTTP API. `dimension` is inferred from the
134
+ * first response. init() verifies reachability and that the model exists.
135
+ */
136
+ export class OllamaEmbedder {
137
+ constructor(opts = {}) {
138
+ this.baseUrl = String(opts.baseUrl ?? "http://localhost:11434").trim().replace(/\/+$/, "");
139
+ this.model = String(opts.model ?? "nomic-embed-text").trim();
140
+ this.logger = opts.logger ?? null;
141
+ this._dimension = null;
142
+ }
143
+
144
+ async _post(body) {
145
+ return fetch(`${this.baseUrl}/api/embeddings`, {
146
+ method: "POST",
147
+ headers: { "Content-Type": "application/json" },
148
+ body: JSON.stringify(body),
149
+ signal: AbortSignal.timeout(DEFAULT_TIMEOUT_MS)
150
+ });
151
+ }
152
+
153
+ /** Probe the server with a tiny prompt; throws when unreachable/missing. */
154
+ async init() {
155
+ const res = await this._post({ model: this.model, prompt: "ping" });
156
+ if (!res.ok) throw new Error(`Ollama ${this.model} unavailable: HTTP ${res.status}`);
157
+ const body = await res.json();
158
+ if (!Array.isArray(body?.embedding)) throw new Error(`Ollama ${this.model} returned no embedding`);
159
+ this._dimension = body.embedding.length;
160
+ this.logger?.info?.(
161
+ `[dsh-mneme] ollama embedder ready: ${this.model} (dim=${this._dimension})`
162
+ );
163
+ return this;
164
+ }
165
+
166
+ async embed(texts) {
167
+ if (!Array.isArray(texts)) throw new TypeError("embed expects an array of strings");
168
+ const out = [];
169
+ for (const text of texts) out.push(await this.embedSingle(text));
170
+ return out;
171
+ }
172
+
173
+ async embedSingle(text) {
174
+ const res = await this._post({ model: this.model, prompt: String(text).slice(0, 8000) });
175
+ if (!res.ok) throw new Error(`Ollama embed failed: HTTP ${res.status}`);
176
+ const body = await res.json();
177
+ const vec = body?.embedding;
178
+ if (!Array.isArray(vec) || !vec.length) throw new Error("Ollama returned no embedding");
179
+ if (this._dimension == null) this._dimension = vec.length;
180
+ return Array.from(vec);
181
+ }
182
+
183
+ get dimension() {
184
+ return this._dimension ?? 0;
185
+ }
186
+
187
+ get modelHash() {
188
+ return modelHash(this.model);
189
+ }
190
+
191
+ dispose() {
192
+ this._dimension = null;
193
+ }
194
+ }
195
+
196
+ /**
197
+ * OpenAI-compatible embedder (OpenAI, SiliconFlow, Zhipu, local proxies).
198
+ * Backward-compatible behavior lifted from embedding.js, but batchable and
199
+ * throwing on failure instead of returning null.
200
+ */
201
+ export class OpenAIEmbedder {
202
+ constructor(opts = {}) {
203
+ this.baseUrl = String(opts.baseUrl ?? "").trim().replace(/\/+$/, "");
204
+ this.apiKey = String(opts.apiKey ?? "").trim();
205
+ this.model = String(opts.model ?? "").trim();
206
+ this.timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
207
+ this.logger = opts.logger ?? null;
208
+ this._dimension = null;
209
+ // Accept both "https://host/v1" and a full path ending in /embeddings.
210
+ this._url = /\/embeddings$/i.test(this.baseUrl)
211
+ ? this.baseUrl
212
+ : this.baseUrl ? `${this.baseUrl}/embeddings` : "";
213
+ }
214
+
215
+ async init() {
216
+ if (!this._url || !this.apiKey || !this.model) {
217
+ throw new Error("OpenAI embedder requires baseUrl, apiKey and model");
218
+ }
219
+ this.logger?.info?.(`[dsh-mneme] openai embedder ready: ${this.model}`);
220
+ return this;
221
+ }
222
+
223
+ async embed(texts) {
224
+ if (!Array.isArray(texts)) throw new TypeError("embed expects an array of strings");
225
+ const out = [];
226
+ for (const batch of chunk(texts, 32)) {
227
+ const res = await fetch(this._url, {
228
+ method: "POST",
229
+ headers: {
230
+ "Content-Type": "application/json",
231
+ "Authorization": `Bearer ${this.apiKey}`
232
+ },
233
+ body: JSON.stringify({ model: this.model, input: batch.map((t) => String(t).slice(0, 8000)) }),
234
+ signal: AbortSignal.timeout(this.timeoutMs)
235
+ });
236
+ if (!res.ok) throw new Error(`Embedding API failed: HTTP ${res.status}`);
237
+ const body = await res.json();
238
+ const list = body?.data;
239
+ if (!Array.isArray(list) || list.length !== batch.length) {
240
+ throw new Error("Embedding API returned unexpected payload");
241
+ }
242
+ for (const item of list) {
243
+ const vec = item?.embedding;
244
+ if (!Array.isArray(vec) || !vec.length) throw new Error("Embedding API returned empty vector");
245
+ if (this._dimension == null) this._dimension = vec.length;
246
+ out.push(Array.from(vec));
247
+ }
248
+ }
249
+ return out;
250
+ }
251
+
252
+ async embedSingle(text) {
253
+ const rows = await this.embed([String(text)]);
254
+ return rows[0];
255
+ }
256
+
257
+ get dimension() {
258
+ return this._dimension ?? 0;
259
+ }
260
+
261
+ get modelHash() {
262
+ return modelHash(this.model);
263
+ }
264
+
265
+ dispose() {
266
+ this._dimension = null;
267
+ }
268
+ }
269
+
270
+ /** Pick a backend instance by provider name. Throws on unknown providers. */
271
+ export function createEmbedderByProvider(provider, opts) {
272
+ switch (String(provider ?? "").toLowerCase()) {
273
+ case "local":
274
+ return new LocalEmbedder(opts);
275
+ case "ollama":
276
+ return new OllamaEmbedder(opts);
277
+ case "openai":
278
+ return new OpenAIEmbedder(opts);
279
+ default:
280
+ throw new Error(`Unknown embedding provider: ${provider}`);
281
+ }
282
+ }