@delali/narsil 0.3.1-next.72 → 0.3.1-next.74

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 (70) hide show
  1. package/README.md +2 -2
  2. package/dist/adapters/filesystem.mjs +1 -1
  3. package/dist/adapters/indexeddb.mjs +1 -1
  4. package/dist/{capabilities-D6gjjQyA.d.ts → capabilities-CAMzmkAg.d.ts} +19 -4
  5. package/dist/{chunk-P2ZZN32C.mjs → chunk-2E35UM3G.mjs} +2 -12
  6. package/dist/chunk-3DDUNI6N.mjs +48 -0
  7. package/dist/{chunk-KZTI25D3.mjs → chunk-4MQ5F4IL.mjs} +1 -1
  8. package/dist/{chunk-HVJYTKK7.mjs → chunk-BVBIQQKG.mjs} +947 -1327
  9. package/dist/{chunk-2X2ZQOAB.mjs → chunk-CTOSGRM7.mjs} +101 -21
  10. package/dist/{chunk-7QMTA7AA.mjs → chunk-CUZOX6CN.mjs} +7784 -4276
  11. package/dist/chunk-I23G3IZC.mjs +593 -0
  12. package/dist/{chunk-RV77FR2F.mjs → chunk-I64LXKO3.mjs} +0 -1
  13. package/dist/chunk-KEHK2O6P.mjs +13 -0
  14. package/dist/{chunk-CVPA5Q7R.mjs → chunk-LX4JERXI.mjs} +9 -6
  15. package/dist/{chunk-EDMCZLSJ.mjs → chunk-NOZUQM7J.mjs} +193 -13
  16. package/dist/{chunk-LCCL6EEH.mjs → chunk-PO53FV4Q.mjs} +93 -94
  17. package/dist/chunk-T5ZWEWVD.mjs +658 -0
  18. package/dist/{chunk-Y52WZWAU.mjs → chunk-TVFLHM7M.mjs} +4 -4
  19. package/dist/{chunk-QR4OKRVY.mjs → chunk-VKX2BPXD.mjs} +147 -642
  20. package/dist/{chunk-3HQPUCCT.mjs → chunk-XW25X6AU.mjs} +66 -237
  21. package/dist/{chunk-M6XF5HDE.mjs → chunk-Z3DQPOJQ.mjs} +127 -30
  22. package/dist/{client-C2TiW29y.d.ts → client-C-I70ArH.d.ts} +77 -26
  23. package/dist/client.d.ts +9 -9
  24. package/dist/client.mjs +44 -22
  25. package/dist/{config-BLguB-om.d.ts → config-C6Iw5rCX.d.ts} +11 -4
  26. package/dist/{config-C7Ip8Ife.d.ts → config-DJPH3IQB.d.ts} +11 -2
  27. package/dist/direct-executor-KFAGILJC.mjs +17 -0
  28. package/dist/distribution/coordinator/etcd.d.ts +29 -16
  29. package/dist/distribution/coordinator/etcd.mjs +162 -75
  30. package/dist/distribution/coordinator/in-memory.d.ts +2 -2
  31. package/dist/distribution/transport/grpc.d.ts +2 -2
  32. package/dist/distribution/transport/grpc.mjs +22 -2
  33. package/dist/distribution/transport/in-memory.d.ts +2 -2
  34. package/dist/distribution/transport/in-memory.mjs +6 -3
  35. package/dist/distribution/transport/tcp.d.ts +2 -2
  36. package/dist/distribution/transport/tcp.mjs +40 -16
  37. package/dist/distribution.d.ts +9 -9
  38. package/dist/distribution.mjs +647 -366
  39. package/dist/embeddings/openai.mjs +1 -1
  40. package/dist/{engine-Dm4446j1.d.ts → engine-B8eRPO7m.d.ts} +97 -188
  41. package/dist/index.browser.mjs +12132 -8592
  42. package/dist/index.d.ts +27 -8
  43. package/dist/index.mjs +15 -14
  44. package/dist/{memory-CKHTU1CL.d.ts → memory-DJEwWoCi.d.ts} +1204 -968
  45. package/dist/partial-words-DTIHO8G4.d.ts +19 -0
  46. package/dist/partial-words.d.ts +22 -0
  47. package/dist/partial-words.mjs +11 -0
  48. package/dist/pattern.mjs +1 -1
  49. package/dist/persistence/durability/checkpoint-worker.d.ts +57 -29
  50. package/dist/persistence/durability/checkpoint-worker.mjs +26 -16
  51. package/dist/react.d.ts +7 -7
  52. package/dist/react.mjs +0 -1
  53. package/dist/{schema-BBOCdhND.d.ts → schema-DgyDijhf.d.ts} +6 -2
  54. package/dist/server.d.ts +11 -11
  55. package/dist/server.mjs +212 -32
  56. package/dist/{tasks-BECd0b__.d.ts → tasks-BVmAKYBh.d.ts} +3 -3
  57. package/dist/{thread-IBSIZZ6W.mjs → thread-W6DPKEF6.mjs} +82 -21
  58. package/dist/{types-BQDlIvfS.d.ts → types-B3b68nfF.d.ts} +1 -1
  59. package/dist/{types-D5OwhtsQ.d.ts → types-Cb9kiY5P.d.ts} +1 -1
  60. package/dist/{types-BiUmaiWT.d.ts → types-ChrVy4lH.d.ts} +1 -1
  61. package/dist/vector/search-worker.d.ts +1 -1
  62. package/dist/vector/search-worker.mjs +4 -3
  63. package/dist/workers/entry.mjs +4 -4
  64. package/package.json +15 -7
  65. package/dist/chunk-3L4U6EJQ.mjs +0 -49
  66. package/dist/chunk-IFBKLBGB.mjs +0 -7
  67. package/dist/chunk-XZ4PAREY.mjs +0 -25
  68. package/dist/direct-executor-SVCHXR74.mjs +0 -14
  69. package/dist/serialization/crc32-worker.d.ts +0 -20
  70. package/dist/serialization/crc32-worker.mjs +0 -62
@@ -0,0 +1,658 @@
1
+ import { tokenizeOptions, getNestedValue, tokenizeIterator, fuzzyTermMatches } from './chunk-I23G3IZC.mjs';
2
+ import { compareCodePoints } from './chunk-LJYTW7GT.mjs';
3
+
4
+ // src/core/partition/suggestions.ts
5
+ function verbatimOccurrences(state, token) {
6
+ const postingList = state.invertedIdx.lookup(token);
7
+ if (!postingList) return 0;
8
+ const derived = postingList.totalTermFrequency - state.surfaceRegistry.stemChangedTotalFor(token);
9
+ return derived > 0 ? derived : 0;
10
+ }
11
+ function suggestDisplayTerms(state, surfacePrefix, stemmedPrefix, limit) {
12
+ if (limit <= 0 || surfacePrefix.length === 0) return [];
13
+ const groups = /* @__PURE__ */ new Map();
14
+ for (const candidate of state.surfaceRegistry.candidatesForPrefix(surfacePrefix)) {
15
+ const postingList = state.invertedIdx.lookup(candidate.token);
16
+ if (!postingList || postingList.docIdSet.size === 0) continue;
17
+ let group = groups.get(candidate.token);
18
+ if (!group) {
19
+ group = { documentFrequency: postingList.docIdSet.size, surfaces: [] };
20
+ groups.set(candidate.token, group);
21
+ }
22
+ group.surfaces.push({ surface: candidate.surface, occurrences: candidate.occurrences });
23
+ }
24
+ const termPrefixes = state.surfaceRegistry.size() === 0 && stemmedPrefix !== surfacePrefix && stemmedPrefix.length > 0 ? [surfacePrefix, stemmedPrefix] : [surfacePrefix];
25
+ for (const prefix of termPrefixes) {
26
+ for (const suggestion of state.invertedIdx.prefixSearch(prefix, Number.MAX_SAFE_INTEGER)) {
27
+ const occurrences = verbatimOccurrences(state, suggestion.term);
28
+ if (occurrences === 0) continue;
29
+ let group = groups.get(suggestion.term);
30
+ if (!group) {
31
+ group = { documentFrequency: suggestion.documentFrequency, surfaces: [] };
32
+ groups.set(suggestion.term, group);
33
+ } else if (group.surfaces.some((s) => s.surface === suggestion.term)) {
34
+ continue;
35
+ }
36
+ group.surfaces.push({ surface: suggestion.term, occurrences });
37
+ }
38
+ }
39
+ const results = [];
40
+ for (const [token, group] of groups) {
41
+ results.push({ token, documentFrequency: group.documentFrequency, surfaces: group.surfaces });
42
+ }
43
+ results.sort((a, b) => b.documentFrequency - a.documentFrequency || compareCodePoints(a.token, b.token));
44
+ if (results.length > limit) results.length = limit;
45
+ return results;
46
+ }
47
+ function partitionDocumentFrequency(subs, token) {
48
+ let documents = 0;
49
+ for (const sub of subs) documents += sub.invertedIdx.documentFrequency(token);
50
+ return documents;
51
+ }
52
+ function expandTermPrefix(subs, surfacePrefix, stemmedToken, maxExpansions) {
53
+ if (maxExpansions <= 0 || surfacePrefix.length === 0) return [];
54
+ const candidates = /* @__PURE__ */ new Set();
55
+ const holdsSurfaceForms = subs.some((sub) => sub.surfaceRegistry.size() > 0);
56
+ const termPrefixes = !holdsSurfaceForms && stemmedToken !== surfacePrefix && stemmedToken.length > 0 ? [surfacePrefix, stemmedToken] : [surfacePrefix];
57
+ for (const sub of subs) {
58
+ for (const candidate of sub.surfaceRegistry.candidatesForPrefix(surfacePrefix)) candidates.add(candidate.token);
59
+ for (const prefix of termPrefixes) {
60
+ for (const suggestion of sub.invertedIdx.prefixSearch(prefix, Number.MAX_SAFE_INTEGER)) {
61
+ if (candidates.has(suggestion.term)) continue;
62
+ if (verbatimOccurrences(sub, suggestion.term) > 0) candidates.add(suggestion.term);
63
+ }
64
+ }
65
+ }
66
+ const ranked = [];
67
+ for (const token of candidates) {
68
+ const documents = partitionDocumentFrequency(subs, token);
69
+ if (documents > 0) ranked.push({ token, documents });
70
+ }
71
+ ranked.sort((a, b) => b.documents - a.documents || compareCodePoints(a.token, b.token));
72
+ if (ranked.length > maxExpansions) ranked.length = maxExpansions;
73
+ return ranked.map((entry) => entry.token);
74
+ }
75
+
76
+ // src/core/partition/partial-words/text.ts
77
+ var encoder = new TextEncoder();
78
+ var LAST_BASIC_PLANE_CODE_POINT = 65535;
79
+ var UTF8_CONTINUATION_MASK = 192;
80
+ var UTF8_CONTINUATION_BITS = 128;
81
+ function codePointsOf(text) {
82
+ const points = [];
83
+ let index = 0;
84
+ while (index < text.length) {
85
+ const point = text.codePointAt(index) ?? text.charCodeAt(index);
86
+ points.push(point);
87
+ index += point > LAST_BASIC_PLANE_CODE_POINT ? 2 : 1;
88
+ }
89
+ return points;
90
+ }
91
+ function runKeyAt(points, at) {
92
+ return String.fromCodePoint(points[at], points[at + 1], points[at + 2]);
93
+ }
94
+ function partialWordProbe(text) {
95
+ return { text, codePoints: codePointsOf(text), utf8: encoder.encode(text) };
96
+ }
97
+ function utf8Contains(haystack, start, end, needle) {
98
+ const last = end - needle.length;
99
+ for (let from = start; from <= last; from++) {
100
+ let at = 0;
101
+ while (at < needle.length && haystack[from + at] === needle[at]) at++;
102
+ if (at === needle.length) return true;
103
+ }
104
+ return false;
105
+ }
106
+ function utf8CodePointCount(bytes, start, end) {
107
+ let count = 0;
108
+ for (let at = start; at < end; at++) {
109
+ if ((bytes[at] & UTF8_CONTINUATION_MASK) !== UTF8_CONTINUATION_BITS) count++;
110
+ }
111
+ return count;
112
+ }
113
+
114
+ // src/core/partition/partial-words/expansion.ts
115
+ function excludedFromRequest(request, token) {
116
+ if (token === request.ownToken) return true;
117
+ return request.tolerance > 0 && fuzzyTermMatches(request.ownToken, token, request.tolerance, request.prefixLength);
118
+ }
119
+ function addedWordsFor(subs, readers, request) {
120
+ if (request.share <= 0) return [];
121
+ const probe = partialWordProbe(request.written);
122
+ const shortestSpelling = /* @__PURE__ */ new Map();
123
+ for (const reader of readers) {
124
+ reader.forEachContaining(probe, (token, spellingLength) => {
125
+ const known = shortestSpelling.get(token);
126
+ if (known === void 0 || spellingLength < known) shortestSpelling.set(token, spellingLength);
127
+ });
128
+ }
129
+ const ranked = [];
130
+ for (const [token, spellingLength] of shortestSpelling) {
131
+ if (excludedFromRequest(request, token)) continue;
132
+ const documents = partitionDocumentFrequency(subs, token);
133
+ if (documents > 0) ranked.push({ token, documents, spellingLength });
134
+ }
135
+ ranked.sort((a, b) => b.documents - a.documents || compareCodePoints(a.token, b.token));
136
+ if (ranked.length > request.share) ranked.length = request.share;
137
+ const writtenLength = probe.codePoints.length;
138
+ return ranked.map((word) => ({ token: word.token, factor: writtenLength / word.spellingLength }));
139
+ }
140
+ function expandPartialWords(subs, analysis, requests) {
141
+ const readers = [];
142
+ for (const sub of subs) {
143
+ const reader = sub.partialWords.readerFor(analysis);
144
+ if (reader !== null) readers.push(reader);
145
+ }
146
+ return requests.map((request) => readers.length === 0 ? [] : addedWordsFor(subs, readers, request));
147
+ }
148
+
149
+ // src/core/partition/partial-words/constants.ts
150
+ var PARTIAL_WORD_RUN_CODE_POINTS = 3;
151
+ var GROWING_PARTIAL_ENTRY_BYTES = 96;
152
+ var GROWING_PARTIAL_RUN_BYTES = 120;
153
+ var GROWING_PARTIAL_RUN_MEMBER_BYTES = 24;
154
+ var GROWING_PARTIAL_COUNTED_SPELLING_BYTES = 64;
155
+ var FROZEN_PARTIAL_REMOVED_ENTRY_BYTES = 48;
156
+
157
+ // src/core/partition/frozen/string-blob.ts
158
+ var SURROGATE = /[\ud800-\udfff]/;
159
+ var encoder2 = new TextEncoder();
160
+ function encodeOneByOne(strings) {
161
+ const encoded = new Array(strings.length);
162
+ let blobLength = 0;
163
+ for (let i = 0; i < strings.length; i++) {
164
+ encoded[i] = encoder2.encode(strings[i]);
165
+ blobLength += encoded[i].length;
166
+ }
167
+ const blob = new Uint8Array(blobLength);
168
+ const offsets = new Uint32Array(strings.length + 1);
169
+ let cursor = 0;
170
+ for (let i = 0; i < strings.length; i++) {
171
+ blob.set(encoded[i], cursor);
172
+ cursor += encoded[i].length;
173
+ offsets[i + 1] = cursor;
174
+ }
175
+ return { blob, offsets };
176
+ }
177
+ function basicPlaneUtf8Bytes(text) {
178
+ let bytes = text.length;
179
+ for (let i = 0; i < text.length; i++) {
180
+ const unit = text.charCodeAt(i);
181
+ if (unit >= 128) bytes += unit < 2048 ? 1 : 2;
182
+ }
183
+ return bytes;
184
+ }
185
+ function encodeStringBlob(strings) {
186
+ const joined = strings.join("");
187
+ if (SURROGATE.test(joined)) return encodeOneByOne(strings);
188
+ const blob = encoder2.encode(joined);
189
+ const offsets = new Uint32Array(strings.length + 1);
190
+ let cursor = 0;
191
+ for (let i = 0; i < strings.length; i++) {
192
+ cursor += basicPlaneUtf8Bytes(strings[i]);
193
+ offsets[i + 1] = cursor;
194
+ }
195
+ return cursor === blob.length ? { blob, offsets } : encodeOneByOne(strings);
196
+ }
197
+ function orderByComparator(strings) {
198
+ const order = new Array(strings.length);
199
+ for (let i = 0; i < strings.length; i++) order[i] = i;
200
+ return order.sort((a, b) => compareCodePoints(strings[a], strings[b]));
201
+ }
202
+ function alreadyInCodePointOrder(strings) {
203
+ for (let i = 1; i < strings.length; i++) {
204
+ if (compareCodePoints(strings[i - 1], strings[i]) > 0) return false;
205
+ }
206
+ return true;
207
+ }
208
+ function codePointOrder(strings) {
209
+ if (alreadyInCodePointOrder(strings)) return strings.map((_, i) => i);
210
+ if (SURROGATE.test(strings.join(""))) return orderByComparator(strings);
211
+ const positions = /* @__PURE__ */ new Map();
212
+ for (let i = 0; i < strings.length; i++) positions.set(strings[i], i);
213
+ if (positions.size !== strings.length) return orderByComparator(strings);
214
+ const order = new Array(strings.length);
215
+ const sorted = [...strings].sort();
216
+ for (let i = 0; i < sorted.length; i++) order[i] = positions.get(sorted[i]) ?? 0;
217
+ return order;
218
+ }
219
+
220
+ // src/core/partition/partial-words/frozen-table.ts
221
+ function buildPartialWordTable(source, tokenTable) {
222
+ const spellings = [];
223
+ const spellingTokens = [];
224
+ const spellingCounts = [];
225
+ const runLists = /* @__PURE__ */ new Map();
226
+ const addSpelling = (spelling, tokenIndex, count) => {
227
+ const points = codePointsOf(spelling);
228
+ if (points.length <= PARTIAL_WORD_RUN_CODE_POINTS) return;
229
+ const entry = spellings.length;
230
+ spellings.push(spelling);
231
+ spellingTokens.push(tokenIndex);
232
+ spellingCounts.push(count);
233
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
234
+ const key = runKeyAt(points, at);
235
+ const list = runLists.get(key);
236
+ if (list === void 0) runLists.set(key, [entry]);
237
+ else if (list[list.length - 1] !== entry) list.push(entry);
238
+ }
239
+ };
240
+ for (let at = 0; at < source.tokens.length; at++) {
241
+ if (source.counts[at] <= 0) continue;
242
+ const tokenIndex = tokenTable.find(source.tokens[at]);
243
+ if (tokenIndex >= 0) addSpelling(source.spellings[at], tokenIndex, source.counts[at]);
244
+ }
245
+ const runKeys = [...runLists.keys()].sort(compareCodePoints);
246
+ const runs = new Uint32Array(runKeys.length * PARTIAL_WORD_RUN_CODE_POINTS);
247
+ const runOffsets = new Uint32Array(runKeys.length + 1);
248
+ let memberCount = 0;
249
+ for (const key of runKeys) memberCount += runLists.get(key)?.length ?? 0;
250
+ const runEntries = new Uint32Array(memberCount);
251
+ let cursor = 0;
252
+ for (let run = 0; run < runKeys.length; run++) {
253
+ const points = codePointsOf(runKeys[run]);
254
+ runs.set(points, run * PARTIAL_WORD_RUN_CODE_POINTS);
255
+ for (const entry of runLists.get(runKeys[run]) ?? []) runEntries[cursor++] = entry;
256
+ runOffsets[run + 1] = cursor;
257
+ }
258
+ const spellingBlob = encodeStringBlob(spellings);
259
+ return {
260
+ fields: [...source.fields],
261
+ spellings: spellingBlob.blob,
262
+ spellingOffsets: spellingBlob.offsets,
263
+ spellingTokens: Uint32Array.from(spellingTokens),
264
+ spellingCounts: Uint32Array.from(spellingCounts),
265
+ runs,
266
+ runOffsets,
267
+ runEntries
268
+ };
269
+ }
270
+ function findRun(runs, first, second, third) {
271
+ let low = 0;
272
+ let high = runs.length / PARTIAL_WORD_RUN_CODE_POINTS;
273
+ while (low < high) {
274
+ const middle = low + high >>> 1;
275
+ const at = middle * PARTIAL_WORD_RUN_CODE_POINTS;
276
+ const difference = runs[at] - first || runs[at + 1] - second || runs[at + 2] - third;
277
+ if (difference === 0) return middle;
278
+ if (difference < 0) low = middle + 1;
279
+ else high = middle;
280
+ }
281
+ return -1;
282
+ }
283
+ function partialWordTableBytes(table) {
284
+ return table.spellings.byteLength + table.spellingOffsets.byteLength + table.spellingTokens.byteLength + table.spellingCounts.byteLength + table.runs.byteLength + table.runOffsets.byteLength + table.runEntries.byteLength;
285
+ }
286
+ function readPartialWordTable(table, tokenTable, removedCounts) {
287
+ const { spellings, spellingOffsets, spellingTokens, spellingCounts, runs, runOffsets, runEntries } = table;
288
+ function narrowestRun(points) {
289
+ let narrowest = -1;
290
+ let narrowestLength = Number.POSITIVE_INFINITY;
291
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
292
+ const run = findRun(runs, points[at], points[at + 1], points[at + 2]);
293
+ if (run < 0) return -1;
294
+ const length = runOffsets[run + 1] - runOffsets[run];
295
+ if (length < narrowestLength) {
296
+ narrowest = run;
297
+ narrowestLength = length;
298
+ }
299
+ }
300
+ return narrowest;
301
+ }
302
+ return {
303
+ forEachContaining(probe, visit) {
304
+ if (probe.codePoints.length < PARTIAL_WORD_RUN_CODE_POINTS) return;
305
+ const run = narrowestRun(probe.codePoints);
306
+ if (run < 0) return;
307
+ for (let member = runOffsets[run]; member < runOffsets[run + 1]; member++) {
308
+ const entry = runEntries[member];
309
+ const start = spellingOffsets[entry];
310
+ const end = spellingOffsets[entry + 1];
311
+ if (end - start <= probe.utf8.length) continue;
312
+ if ((removedCounts.get(entry) ?? 0) >= spellingCounts[entry]) continue;
313
+ if (!utf8Contains(spellings, start, end, probe.utf8)) continue;
314
+ const length = utf8CodePointCount(spellings, start, end);
315
+ if (length <= probe.codePoints.length) continue;
316
+ visit(tokenTable.tokenAt(spellingTokens[entry]), length);
317
+ }
318
+ },
319
+ entryOf(spelling, token) {
320
+ const tokenIndex = tokenTable.find(token);
321
+ if (tokenIndex < 0) return -1;
322
+ const probe = partialWordProbe(spelling);
323
+ if (probe.codePoints.length <= PARTIAL_WORD_RUN_CODE_POINTS) return -1;
324
+ const run = narrowestRun(probe.codePoints);
325
+ if (run < 0) return -1;
326
+ for (let member = runOffsets[run]; member < runOffsets[run + 1]; member++) {
327
+ const entry = runEntries[member];
328
+ if (spellingTokens[entry] !== tokenIndex) continue;
329
+ const start = spellingOffsets[entry];
330
+ const end = spellingOffsets[entry + 1];
331
+ if (end - start === probe.utf8.length && utf8Contains(spellings, start, end, probe.utf8)) return entry;
332
+ }
333
+ return -1;
334
+ },
335
+ bytes() {
336
+ return partialWordTableBytes(table);
337
+ }
338
+ };
339
+ }
340
+
341
+ // src/core/partition/partial-words/spellings.ts
342
+ function countSpelling(counts, token, spelling, change) {
343
+ let byToken = counts.get(token);
344
+ if (byToken === void 0) {
345
+ if (change <= 0) return;
346
+ byToken = /* @__PURE__ */ new Map();
347
+ counts.set(token, byToken);
348
+ }
349
+ const next = (byToken.get(spelling) ?? 0) + change;
350
+ if (next > 0) {
351
+ byToken.set(spelling, next);
352
+ return;
353
+ }
354
+ byToken.delete(spelling);
355
+ if (byToken.size === 0) counts.delete(token);
356
+ }
357
+ function textsOf(value) {
358
+ if (typeof value === "string") return [value];
359
+ if (!Array.isArray(value)) return [];
360
+ return value.filter((element) => typeof element === "string");
361
+ }
362
+ function countDocumentSpellings(counts, document, analysis, direction) {
363
+ const tokenizing = { ...tokenizeOptions(analysis.options), collectSurfaces: true };
364
+ for (const field of analysis.fields) {
365
+ for (const text of textsOf(getNestedValue(document, field))) {
366
+ for (const occurrence of tokenizeIterator(text, analysis.language, tokenizing)) {
367
+ countSpelling(counts, occurrence.token, occurrence.surface ?? occurrence.token, direction);
368
+ }
369
+ }
370
+ }
371
+ }
372
+ function spellingCountsOf(documents, analysis) {
373
+ const counts = /* @__PURE__ */ new Map();
374
+ for (const document of documents) countDocumentSpellings(counts, document, analysis, 1);
375
+ return counts;
376
+ }
377
+ function encodePartialSpellings(fields, counts) {
378
+ const encoded = { fields: [...fields], tokens: [], spellings: [], counts: [] };
379
+ for (const [token, byToken] of counts) {
380
+ for (const [spelling, count] of byToken) {
381
+ encoded.tokens.push(token);
382
+ encoded.spellings.push(spelling);
383
+ encoded.counts.push(count);
384
+ }
385
+ }
386
+ return encoded;
387
+ }
388
+ function sameFields(left, right) {
389
+ if (left.length !== right.length) return false;
390
+ for (let at = 0; at < left.length; at++) {
391
+ if (left[at] !== right[at]) return false;
392
+ }
393
+ return true;
394
+ }
395
+
396
+ // src/core/partition/partial-words/frozen-slot.ts
397
+ function createFrozenPartialWordSlot(initial, tokenTable, documents) {
398
+ let table = initial;
399
+ let removedCounts = /* @__PURE__ */ new Map();
400
+ let reader = initial === null ? null : readPartialWordTable(initial, tokenTable, removedCounts);
401
+ let pendingRemovals = [];
402
+ function subtractPendingRemovals(current, analysis) {
403
+ if (pendingRemovals.length === 0) return;
404
+ const removed = /* @__PURE__ */ new Map();
405
+ for (const ordinal of pendingRemovals) countDocumentSpellings(removed, documents.documentAt(ordinal), analysis, 1);
406
+ pendingRemovals = [];
407
+ for (const [token, bySpelling] of removed) {
408
+ for (const [spelling, count] of bySpelling) {
409
+ const entry = current.entryOf(spelling, token);
410
+ if (entry >= 0) removedCounts.set(entry, (removedCounts.get(entry) ?? 0) + count);
411
+ }
412
+ }
413
+ }
414
+ return {
415
+ readerFor(analysis) {
416
+ if (analysis.fields.length === 0) return null;
417
+ if (table === null || reader === null || !sameFields(table.fields, analysis.fields)) {
418
+ const counts = spellingCountsOf(documents.survivors(), analysis);
419
+ table = buildPartialWordTable(encodePartialSpellings(analysis.fields, counts), tokenTable);
420
+ removedCounts = /* @__PURE__ */ new Map();
421
+ pendingRemovals = [];
422
+ reader = readPartialWordTable(table, tokenTable, removedCounts);
423
+ return reader;
424
+ }
425
+ subtractPendingRemovals(reader, analysis);
426
+ return reader;
427
+ },
428
+ afterRemoval(ordinal) {
429
+ if (table !== null) pendingRemovals.push(ordinal);
430
+ },
431
+ spellings() {
432
+ return null;
433
+ },
434
+ bytes() {
435
+ if (table === null) return 0;
436
+ return partialWordTableBytes(table) + removedCounts.size * FROZEN_PARTIAL_REMOVED_ENTRY_BYTES;
437
+ }
438
+ };
439
+ }
440
+
441
+ // src/core/partition/partial-words/growing-slot.ts
442
+ function createGrowingPartialWordSlot(view) {
443
+ let fields = [];
444
+ let everyDocumentPending = false;
445
+ const pending = /* @__PURE__ */ new Set();
446
+ const counts = /* @__PURE__ */ new Map();
447
+ const entriesByToken = /* @__PURE__ */ new Map();
448
+ const entrySpellings = [];
449
+ const entryTokens = [];
450
+ const entryLengths = [];
451
+ const freeEntries = [];
452
+ const runs = /* @__PURE__ */ new Map();
453
+ let liveEntries = 0;
454
+ let runMembers = 0;
455
+ let countedSpellings = 0;
456
+ function resetSpellings() {
457
+ pending.clear();
458
+ counts.clear();
459
+ countedSpellings = 0;
460
+ entriesByToken.clear();
461
+ entrySpellings.length = 0;
462
+ entryTokens.length = 0;
463
+ entryLengths.length = 0;
464
+ freeEntries.length = 0;
465
+ runs.clear();
466
+ liveEntries = 0;
467
+ runMembers = 0;
468
+ }
469
+ function addEntry(token, spelling) {
470
+ const points = codePointsOf(spelling);
471
+ if (points.length <= PARTIAL_WORD_RUN_CODE_POINTS) return;
472
+ const entry = freeEntries.pop() ?? entrySpellings.length;
473
+ entrySpellings[entry] = spelling;
474
+ entryTokens[entry] = token;
475
+ entryLengths[entry] = points.length;
476
+ let byToken = entriesByToken.get(token);
477
+ if (byToken === void 0) {
478
+ byToken = /* @__PURE__ */ new Map();
479
+ entriesByToken.set(token, byToken);
480
+ }
481
+ byToken.set(spelling, entry);
482
+ liveEntries++;
483
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
484
+ const key = runKeyAt(points, at);
485
+ let members = runs.get(key);
486
+ if (members === void 0) {
487
+ members = /* @__PURE__ */ new Set();
488
+ runs.set(key, members);
489
+ }
490
+ if (members.has(entry)) continue;
491
+ members.add(entry);
492
+ runMembers++;
493
+ }
494
+ }
495
+ function removeEntry(token, spelling) {
496
+ const byToken = entriesByToken.get(token);
497
+ const entry = byToken?.get(spelling);
498
+ if (byToken === void 0 || entry === void 0) return;
499
+ const points = codePointsOf(spelling);
500
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
501
+ const key = runKeyAt(points, at);
502
+ const members = runs.get(key);
503
+ if (members === void 0 || !members.delete(entry)) continue;
504
+ runMembers--;
505
+ if (members.size === 0) runs.delete(key);
506
+ }
507
+ byToken.delete(spelling);
508
+ if (byToken.size === 0) entriesByToken.delete(token);
509
+ entrySpellings[entry] = "";
510
+ entryTokens[entry] = "";
511
+ entryLengths[entry] = 0;
512
+ freeEntries.push(entry);
513
+ liveEntries--;
514
+ }
515
+ function change(token, spelling, delta) {
516
+ let byToken = counts.get(token);
517
+ const before = byToken?.get(spelling) ?? 0;
518
+ const after = Math.max(0, before + delta);
519
+ if (after === before) return;
520
+ if (byToken === void 0) {
521
+ byToken = /* @__PURE__ */ new Map();
522
+ counts.set(token, byToken);
523
+ }
524
+ if (after > 0) byToken.set(spelling, after);
525
+ else byToken.delete(spelling);
526
+ if (byToken.size === 0) counts.delete(token);
527
+ if (before === 0) {
528
+ countedSpellings++;
529
+ addEntry(token, spelling);
530
+ } else if (after === 0) {
531
+ countedSpellings--;
532
+ removeEntry(token, spelling);
533
+ }
534
+ }
535
+ function applyCounts(source, direction) {
536
+ for (const [token, byToken] of source) {
537
+ for (const [spelling, count] of byToken) change(token, spelling, count * direction);
538
+ }
539
+ }
540
+ function adoptFields(next) {
541
+ if (sameFields(fields, next)) return;
542
+ fields = [...next];
543
+ resetSpellings();
544
+ everyDocumentPending = fields.length > 0;
545
+ }
546
+ function documentAt(internalId) {
547
+ const docId = view.docStore.getExternalId(internalId);
548
+ return docId === void 0 ? void 0 : view.docStore.get(docId)?.fields;
549
+ }
550
+ function resolvePending(analysis) {
551
+ if (everyDocumentPending) {
552
+ resetSpellings();
553
+ const derived2 = /* @__PURE__ */ new Map();
554
+ for (const [, stored] of view.docStore.all()) countDocumentSpellings(derived2, stored.fields, analysis, 1);
555
+ applyCounts(derived2, 1);
556
+ everyDocumentPending = false;
557
+ return;
558
+ }
559
+ if (pending.size === 0) return;
560
+ const derived = /* @__PURE__ */ new Map();
561
+ for (const internalId of pending) {
562
+ const document = documentAt(internalId);
563
+ if (document !== void 0) countDocumentSpellings(derived, document, analysis, 1);
564
+ }
565
+ pending.clear();
566
+ applyCounts(derived, 1);
567
+ }
568
+ const reader = {
569
+ forEachContaining(probe, visit) {
570
+ const points = probe.codePoints;
571
+ if (points.length < PARTIAL_WORD_RUN_CODE_POINTS) return;
572
+ let narrowest = null;
573
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
574
+ const members = runs.get(runKeyAt(points, at));
575
+ if (members === void 0) return;
576
+ if (narrowest === null || members.size < narrowest.size) narrowest = members;
577
+ }
578
+ if (narrowest === null) return;
579
+ for (const entry of narrowest) {
580
+ const length = entryLengths[entry];
581
+ if (length <= points.length) continue;
582
+ if (!entrySpellings[entry].includes(probe.text)) continue;
583
+ visit(entryTokens[entry], length);
584
+ }
585
+ },
586
+ bytes() {
587
+ return slot.bytes();
588
+ }
589
+ };
590
+ const slot = {
591
+ readerFor(analysis) {
592
+ if (analysis.fields.length === 0) return null;
593
+ adoptFields(analysis.fields);
594
+ resolvePending(analysis);
595
+ return reader;
596
+ },
597
+ spellings() {
598
+ if (fields.length === 0 || everyDocumentPending || pending.size > 0) return null;
599
+ return encodePartialSpellings(fields, counts);
600
+ },
601
+ afterDocument(documentFields, internalId, spellings, direction) {
602
+ if (documentFields.length === 0) return;
603
+ if (!sameFields(fields, documentFields)) {
604
+ const holdsOnlyThisDocument = direction === 1 && view.docStore.count() === 1;
605
+ adoptFields(documentFields);
606
+ if (holdsOnlyThisDocument) everyDocumentPending = false;
607
+ }
608
+ if (everyDocumentPending) return;
609
+ if (direction === -1 && pending.delete(internalId)) return;
610
+ if (spellings !== null) {
611
+ applyCounts(spellings, direction);
612
+ } else if (direction === 1) {
613
+ pending.add(internalId);
614
+ } else {
615
+ everyDocumentPending = true;
616
+ }
617
+ },
618
+ afterMerge(spellings, internalIds) {
619
+ if (internalIds.length === 0) return;
620
+ if (fields.length === 0 && spellings !== null && view.docStore.count() === internalIds.length) {
621
+ adoptFields(spellings.fields);
622
+ everyDocumentPending = false;
623
+ }
624
+ if (fields.length === 0 || everyDocumentPending) return;
625
+ if (spellings === null || !sameFields(fields, spellings.fields)) {
626
+ for (const internalId of internalIds) pending.add(internalId);
627
+ return;
628
+ }
629
+ for (let at = 0; at < spellings.tokens.length; at++) {
630
+ change(spellings.tokens[at], spellings.spellings[at], spellings.counts[at]);
631
+ }
632
+ },
633
+ forgetSpellings() {
634
+ resetSpellings();
635
+ everyDocumentPending = fields.length > 0;
636
+ },
637
+ clear() {
638
+ fields = [];
639
+ everyDocumentPending = false;
640
+ resetSpellings();
641
+ },
642
+ bytes() {
643
+ return liveEntries * GROWING_PARTIAL_ENTRY_BYTES + runs.size * GROWING_PARTIAL_RUN_BYTES + runMembers * GROWING_PARTIAL_RUN_MEMBER_BYTES + countedSpellings * GROWING_PARTIAL_COUNTED_SPELLING_BYTES;
644
+ }
645
+ };
646
+ return slot;
647
+ }
648
+
649
+ // src/core/partition/partial-words/module.ts
650
+ var partialWordSearchModule = {
651
+ name: "partialWords",
652
+ createGrowingSlot: createGrowingPartialWordSlot,
653
+ createFrozenSlot: createFrozenPartialWordSlot,
654
+ buildTable: buildPartialWordTable,
655
+ expand: expandPartialWords
656
+ };
657
+
658
+ export { codePointOrder, countSpelling, encodePartialSpellings, encodeStringBlob, expandTermPrefix, partialWordSearchModule, partitionDocumentFrequency, spellingCountsOf, suggestDisplayTerms };
@@ -1,8 +1,8 @@
1
- import { readSharedConfigFields, restoredEmbedding, restoredPartitionConfig, MAX_PARTITION_COUNT, MAX_REPLICATION_FACTOR } from './chunk-CVPA5Q7R.mjs';
2
- import { MAX_INDEX_NAME_LENGTH, INDEX_NAME_PATTERN } from './chunk-XZ4PAREY.mjs';
3
- import { getLanguage } from './chunk-KZTI25D3.mjs';
1
+ import { readSharedConfigFields, restoredEmbedding, restoredPartitionConfig, MAX_PARTITION_COUNT, MAX_REPLICATION_FACTOR } from './chunk-LX4JERXI.mjs';
2
+ import { getLanguage } from './chunk-4MQ5F4IL.mjs';
3
+ import { MAX_INDEX_NAME_LENGTH, INDEX_NAME_PATTERN } from './chunk-3DDUNI6N.mjs';
4
4
  import { compareCodePoints } from './chunk-LJYTW7GT.mjs';
5
- import { NarsilError, ErrorCodes } from './chunk-RV77FR2F.mjs';
5
+ import { NarsilError, ErrorCodes } from './chunk-I64LXKO3.mjs';
6
6
  import { encode, decode } from '@msgpack/msgpack';
7
7
 
8
8
  // src/distribution/cluster/index-settings.ts