@elaraai/east-codex-plugin 0.0.0-stage → 1.0.90

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.
@@ -0,0 +1,2163 @@
1
+ // ../east-plugin/dist/status/report.js
2
+ import { dirname as dirname4, resolve as resolve2 } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ // ../east-plugin/dist/lib/plugin-status.js
6
+ import { existsSync as existsSync2, readFileSync as readFileSync2, readdirSync } from "node:fs";
7
+ import { createRequire } from "node:module";
8
+ import { dirname as dirname3, join as join3, resolve } from "node:path";
9
+
10
+ // ../east-plugin/dist/lib/search.js
11
+ import { readFile } from "node:fs/promises";
12
+
13
+ // ../../node_modules/.pnpm/minisearch@7.2.0/node_modules/minisearch/dist/es/index.js
14
+ var ENTRIES = "ENTRIES";
15
+ var KEYS = "KEYS";
16
+ var VALUES = "VALUES";
17
+ var LEAF = "";
18
+ var TreeIterator = class {
19
+ constructor(set, type) {
20
+ const node = set._tree;
21
+ const keys = Array.from(node.keys());
22
+ this.set = set;
23
+ this._type = type;
24
+ this._path = keys.length > 0 ? [{ node, keys }] : [];
25
+ }
26
+ next() {
27
+ const value = this.dive();
28
+ this.backtrack();
29
+ return value;
30
+ }
31
+ dive() {
32
+ if (this._path.length === 0) {
33
+ return { done: true, value: void 0 };
34
+ }
35
+ const { node, keys } = last$1(this._path);
36
+ if (last$1(keys) === LEAF) {
37
+ return { done: false, value: this.result() };
38
+ }
39
+ const child = node.get(last$1(keys));
40
+ this._path.push({ node: child, keys: Array.from(child.keys()) });
41
+ return this.dive();
42
+ }
43
+ backtrack() {
44
+ if (this._path.length === 0) {
45
+ return;
46
+ }
47
+ const keys = last$1(this._path).keys;
48
+ keys.pop();
49
+ if (keys.length > 0) {
50
+ return;
51
+ }
52
+ this._path.pop();
53
+ this.backtrack();
54
+ }
55
+ key() {
56
+ return this.set._prefix + this._path.map(({ keys }) => last$1(keys)).filter((key) => key !== LEAF).join("");
57
+ }
58
+ value() {
59
+ return last$1(this._path).node.get(LEAF);
60
+ }
61
+ result() {
62
+ switch (this._type) {
63
+ case VALUES:
64
+ return this.value();
65
+ case KEYS:
66
+ return this.key();
67
+ default:
68
+ return [this.key(), this.value()];
69
+ }
70
+ }
71
+ [Symbol.iterator]() {
72
+ return this;
73
+ }
74
+ };
75
+ var last$1 = (array) => {
76
+ return array[array.length - 1];
77
+ };
78
+ var fuzzySearch = (node, query, maxDistance) => {
79
+ const results = /* @__PURE__ */ new Map();
80
+ if (query === void 0)
81
+ return results;
82
+ const n = query.length + 1;
83
+ const m = n + maxDistance;
84
+ const matrix = new Uint8Array(m * n).fill(maxDistance + 1);
85
+ for (let j = 0; j < n; ++j)
86
+ matrix[j] = j;
87
+ for (let i = 1; i < m; ++i)
88
+ matrix[i * n] = i;
89
+ recurse(node, query, maxDistance, results, matrix, 1, n, "");
90
+ return results;
91
+ };
92
+ var recurse = (node, query, maxDistance, results, matrix, m, n, prefix) => {
93
+ const offset = m * n;
94
+ key: for (const key of node.keys()) {
95
+ if (key === LEAF) {
96
+ const distance = matrix[offset - 1];
97
+ if (distance <= maxDistance) {
98
+ results.set(prefix, [node.get(key), distance]);
99
+ }
100
+ } else {
101
+ let i = m;
102
+ for (let pos = 0; pos < key.length; ++pos, ++i) {
103
+ const char = key[pos];
104
+ const thisRowOffset = n * i;
105
+ const prevRowOffset = thisRowOffset - n;
106
+ let minDistance = matrix[thisRowOffset];
107
+ const jmin = Math.max(0, i - maxDistance - 1);
108
+ const jmax = Math.min(n - 1, i + maxDistance);
109
+ for (let j = jmin; j < jmax; ++j) {
110
+ const different = char !== query[j];
111
+ const rpl = matrix[prevRowOffset + j] + +different;
112
+ const del = matrix[prevRowOffset + j + 1] + 1;
113
+ const ins = matrix[thisRowOffset + j] + 1;
114
+ const dist = matrix[thisRowOffset + j + 1] = Math.min(rpl, del, ins);
115
+ if (dist < minDistance)
116
+ minDistance = dist;
117
+ }
118
+ if (minDistance > maxDistance) {
119
+ continue key;
120
+ }
121
+ }
122
+ recurse(node.get(key), query, maxDistance, results, matrix, i, n, prefix + key);
123
+ }
124
+ }
125
+ };
126
+ var SearchableMap = class _SearchableMap {
127
+ /**
128
+ * The constructor is normally called without arguments, creating an empty
129
+ * map. In order to create a {@link SearchableMap} from an iterable or from an
130
+ * object, check {@link SearchableMap.from} and {@link
131
+ * SearchableMap.fromObject}.
132
+ *
133
+ * The constructor arguments are for internal use, when creating derived
134
+ * mutable views of a map at a prefix.
135
+ */
136
+ constructor(tree = /* @__PURE__ */ new Map(), prefix = "") {
137
+ this._size = void 0;
138
+ this._tree = tree;
139
+ this._prefix = prefix;
140
+ }
141
+ /**
142
+ * Creates and returns a mutable view of this {@link SearchableMap},
143
+ * containing only entries that share the given prefix.
144
+ *
145
+ * ### Usage:
146
+ *
147
+ * ```javascript
148
+ * let map = new SearchableMap()
149
+ * map.set("unicorn", 1)
150
+ * map.set("universe", 2)
151
+ * map.set("university", 3)
152
+ * map.set("unique", 4)
153
+ * map.set("hello", 5)
154
+ *
155
+ * let uni = map.atPrefix("uni")
156
+ * uni.get("unique") // => 4
157
+ * uni.get("unicorn") // => 1
158
+ * uni.get("hello") // => undefined
159
+ *
160
+ * let univer = map.atPrefix("univer")
161
+ * univer.get("unique") // => undefined
162
+ * univer.get("universe") // => 2
163
+ * univer.get("university") // => 3
164
+ * ```
165
+ *
166
+ * @param prefix The prefix
167
+ * @return A {@link SearchableMap} representing a mutable view of the original
168
+ * Map at the given prefix
169
+ */
170
+ atPrefix(prefix) {
171
+ if (!prefix.startsWith(this._prefix)) {
172
+ throw new Error("Mismatched prefix");
173
+ }
174
+ const [node, path] = trackDown(this._tree, prefix.slice(this._prefix.length));
175
+ if (node === void 0) {
176
+ const [parentNode, key] = last(path);
177
+ for (const k of parentNode.keys()) {
178
+ if (k !== LEAF && k.startsWith(key)) {
179
+ const node2 = /* @__PURE__ */ new Map();
180
+ node2.set(k.slice(key.length), parentNode.get(k));
181
+ return new _SearchableMap(node2, prefix);
182
+ }
183
+ }
184
+ }
185
+ return new _SearchableMap(node, prefix);
186
+ }
187
+ /**
188
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear
189
+ */
190
+ clear() {
191
+ this._size = void 0;
192
+ this._tree.clear();
193
+ }
194
+ /**
195
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete
196
+ * @param key Key to delete
197
+ */
198
+ delete(key) {
199
+ this._size = void 0;
200
+ return remove(this._tree, key);
201
+ }
202
+ /**
203
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries
204
+ * @return An iterator iterating through `[key, value]` entries.
205
+ */
206
+ entries() {
207
+ return new TreeIterator(this, ENTRIES);
208
+ }
209
+ /**
210
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach
211
+ * @param fn Iteration function
212
+ */
213
+ forEach(fn) {
214
+ for (const [key, value] of this) {
215
+ fn(key, value, this);
216
+ }
217
+ }
218
+ /**
219
+ * Returns a Map of all the entries that have a key within the given edit
220
+ * distance from the search key. The keys of the returned Map are the matching
221
+ * keys, while the values are two-element arrays where the first element is
222
+ * the value associated to the key, and the second is the edit distance of the
223
+ * key to the search key.
224
+ *
225
+ * ### Usage:
226
+ *
227
+ * ```javascript
228
+ * let map = new SearchableMap()
229
+ * map.set('hello', 'world')
230
+ * map.set('hell', 'yeah')
231
+ * map.set('ciao', 'mondo')
232
+ *
233
+ * // Get all entries that match the key 'hallo' with a maximum edit distance of 2
234
+ * map.fuzzyGet('hallo', 2)
235
+ * // => Map(2) { 'hello' => ['world', 1], 'hell' => ['yeah', 2] }
236
+ *
237
+ * // In the example, the "hello" key has value "world" and edit distance of 1
238
+ * // (change "e" to "a"), the key "hell" has value "yeah" and edit distance of 2
239
+ * // (change "e" to "a", delete "o")
240
+ * ```
241
+ *
242
+ * @param key The search key
243
+ * @param maxEditDistance The maximum edit distance (Levenshtein)
244
+ * @return A Map of the matching keys to their value and edit distance
245
+ */
246
+ fuzzyGet(key, maxEditDistance) {
247
+ return fuzzySearch(this._tree, key, maxEditDistance);
248
+ }
249
+ /**
250
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
251
+ * @param key Key to get
252
+ * @return Value associated to the key, or `undefined` if the key is not
253
+ * found.
254
+ */
255
+ get(key) {
256
+ const node = lookup(this._tree, key);
257
+ return node !== void 0 ? node.get(LEAF) : void 0;
258
+ }
259
+ /**
260
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has
261
+ * @param key Key
262
+ * @return True if the key is in the map, false otherwise
263
+ */
264
+ has(key) {
265
+ const node = lookup(this._tree, key);
266
+ return node !== void 0 && node.has(LEAF);
267
+ }
268
+ /**
269
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys
270
+ * @return An `Iterable` iterating through keys
271
+ */
272
+ keys() {
273
+ return new TreeIterator(this, KEYS);
274
+ }
275
+ /**
276
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set
277
+ * @param key Key to set
278
+ * @param value Value to associate to the key
279
+ * @return The {@link SearchableMap} itself, to allow chaining
280
+ */
281
+ set(key, value) {
282
+ if (typeof key !== "string") {
283
+ throw new Error("key must be a string");
284
+ }
285
+ this._size = void 0;
286
+ const node = createPath(this._tree, key);
287
+ node.set(LEAF, value);
288
+ return this;
289
+ }
290
+ /**
291
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size
292
+ */
293
+ get size() {
294
+ if (this._size) {
295
+ return this._size;
296
+ }
297
+ this._size = 0;
298
+ const iter = this.entries();
299
+ while (!iter.next().done)
300
+ this._size += 1;
301
+ return this._size;
302
+ }
303
+ /**
304
+ * Updates the value at the given key using the provided function. The function
305
+ * is called with the current value at the key, and its return value is used as
306
+ * the new value to be set.
307
+ *
308
+ * ### Example:
309
+ *
310
+ * ```javascript
311
+ * // Increment the current value by one
312
+ * searchableMap.update('somekey', (currentValue) => currentValue == null ? 0 : currentValue + 1)
313
+ * ```
314
+ *
315
+ * If the value at the given key is or will be an object, it might not require
316
+ * re-assignment. In that case it is better to use `fetch()`, because it is
317
+ * faster.
318
+ *
319
+ * @param key The key to update
320
+ * @param fn The function used to compute the new value from the current one
321
+ * @return The {@link SearchableMap} itself, to allow chaining
322
+ */
323
+ update(key, fn) {
324
+ if (typeof key !== "string") {
325
+ throw new Error("key must be a string");
326
+ }
327
+ this._size = void 0;
328
+ const node = createPath(this._tree, key);
329
+ node.set(LEAF, fn(node.get(LEAF)));
330
+ return this;
331
+ }
332
+ /**
333
+ * Fetches the value of the given key. If the value does not exist, calls the
334
+ * given function to create a new value, which is inserted at the given key
335
+ * and subsequently returned.
336
+ *
337
+ * ### Example:
338
+ *
339
+ * ```javascript
340
+ * const map = searchableMap.fetch('somekey', () => new Map())
341
+ * map.set('foo', 'bar')
342
+ * ```
343
+ *
344
+ * @param key The key to update
345
+ * @param initial A function that creates a new value if the key does not exist
346
+ * @return The existing or new value at the given key
347
+ */
348
+ fetch(key, initial) {
349
+ if (typeof key !== "string") {
350
+ throw new Error("key must be a string");
351
+ }
352
+ this._size = void 0;
353
+ const node = createPath(this._tree, key);
354
+ let value = node.get(LEAF);
355
+ if (value === void 0) {
356
+ node.set(LEAF, value = initial());
357
+ }
358
+ return value;
359
+ }
360
+ /**
361
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values
362
+ * @return An `Iterable` iterating through values.
363
+ */
364
+ values() {
365
+ return new TreeIterator(this, VALUES);
366
+ }
367
+ /**
368
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator
369
+ */
370
+ [Symbol.iterator]() {
371
+ return this.entries();
372
+ }
373
+ /**
374
+ * Creates a {@link SearchableMap} from an `Iterable` of entries
375
+ *
376
+ * @param entries Entries to be inserted in the {@link SearchableMap}
377
+ * @return A new {@link SearchableMap} with the given entries
378
+ */
379
+ static from(entries) {
380
+ const tree = new _SearchableMap();
381
+ for (const [key, value] of entries) {
382
+ tree.set(key, value);
383
+ }
384
+ return tree;
385
+ }
386
+ /**
387
+ * Creates a {@link SearchableMap} from the iterable properties of a JavaScript object
388
+ *
389
+ * @param object Object of entries for the {@link SearchableMap}
390
+ * @return A new {@link SearchableMap} with the given entries
391
+ */
392
+ static fromObject(object) {
393
+ return _SearchableMap.from(Object.entries(object));
394
+ }
395
+ };
396
+ var trackDown = (tree, key, path = []) => {
397
+ if (key.length === 0 || tree == null) {
398
+ return [tree, path];
399
+ }
400
+ for (const k of tree.keys()) {
401
+ if (k !== LEAF && key.startsWith(k)) {
402
+ path.push([tree, k]);
403
+ return trackDown(tree.get(k), key.slice(k.length), path);
404
+ }
405
+ }
406
+ path.push([tree, key]);
407
+ return trackDown(void 0, "", path);
408
+ };
409
+ var lookup = (tree, key) => {
410
+ if (key.length === 0 || tree == null) {
411
+ return tree;
412
+ }
413
+ for (const k of tree.keys()) {
414
+ if (k !== LEAF && key.startsWith(k)) {
415
+ return lookup(tree.get(k), key.slice(k.length));
416
+ }
417
+ }
418
+ };
419
+ var createPath = (node, key) => {
420
+ const keyLength = key.length;
421
+ outer: for (let pos = 0; node && pos < keyLength; ) {
422
+ for (const k of node.keys()) {
423
+ if (k !== LEAF && key[pos] === k[0]) {
424
+ const len = Math.min(keyLength - pos, k.length);
425
+ let offset = 1;
426
+ while (offset < len && key[pos + offset] === k[offset])
427
+ ++offset;
428
+ const child2 = node.get(k);
429
+ if (offset === k.length) {
430
+ node = child2;
431
+ } else {
432
+ const intermediate = /* @__PURE__ */ new Map();
433
+ intermediate.set(k.slice(offset), child2);
434
+ node.set(key.slice(pos, pos + offset), intermediate);
435
+ node.delete(k);
436
+ node = intermediate;
437
+ }
438
+ pos += offset;
439
+ continue outer;
440
+ }
441
+ }
442
+ const child = /* @__PURE__ */ new Map();
443
+ node.set(key.slice(pos), child);
444
+ return child;
445
+ }
446
+ return node;
447
+ };
448
+ var remove = (tree, key) => {
449
+ const [node, path] = trackDown(tree, key);
450
+ if (node === void 0) {
451
+ return;
452
+ }
453
+ node.delete(LEAF);
454
+ if (node.size === 0) {
455
+ cleanup(path);
456
+ } else if (node.size === 1) {
457
+ const [key2, value] = node.entries().next().value;
458
+ merge(path, key2, value);
459
+ }
460
+ };
461
+ var cleanup = (path) => {
462
+ if (path.length === 0) {
463
+ return;
464
+ }
465
+ const [node, key] = last(path);
466
+ node.delete(key);
467
+ if (node.size === 0) {
468
+ cleanup(path.slice(0, -1));
469
+ } else if (node.size === 1) {
470
+ const [key2, value] = node.entries().next().value;
471
+ if (key2 !== LEAF) {
472
+ merge(path.slice(0, -1), key2, value);
473
+ }
474
+ }
475
+ };
476
+ var merge = (path, key, value) => {
477
+ if (path.length === 0) {
478
+ return;
479
+ }
480
+ const [node, nodeKey] = last(path);
481
+ node.set(nodeKey + key, value);
482
+ node.delete(nodeKey);
483
+ };
484
+ var last = (array) => {
485
+ return array[array.length - 1];
486
+ };
487
+ var OR = "or";
488
+ var AND = "and";
489
+ var AND_NOT = "and_not";
490
+ var MiniSearch = class _MiniSearch {
491
+ /**
492
+ * @param options Configuration options
493
+ *
494
+ * ### Examples:
495
+ *
496
+ * ```javascript
497
+ * // Create a search engine that indexes the 'title' and 'text' fields of your
498
+ * // documents:
499
+ * const miniSearch = new MiniSearch({ fields: ['title', 'text'] })
500
+ * ```
501
+ *
502
+ * ### ID Field:
503
+ *
504
+ * ```javascript
505
+ * // Your documents are assumed to include a unique 'id' field, but if you want
506
+ * // to use a different field for document identification, you can set the
507
+ * // 'idField' option:
508
+ * const miniSearch = new MiniSearch({ idField: 'key', fields: ['title', 'text'] })
509
+ * ```
510
+ *
511
+ * ### Options and defaults:
512
+ *
513
+ * ```javascript
514
+ * // The full set of options (here with their default value) is:
515
+ * const miniSearch = new MiniSearch({
516
+ * // idField: field that uniquely identifies a document
517
+ * idField: 'id',
518
+ *
519
+ * // extractField: function used to get the value of a field in a document.
520
+ * // By default, it assumes the document is a flat object with field names as
521
+ * // property keys and field values as string property values, but custom logic
522
+ * // can be implemented by setting this option to a custom extractor function.
523
+ * extractField: (document, fieldName) => document[fieldName],
524
+ *
525
+ * // tokenize: function used to split fields into individual terms. By
526
+ * // default, it is also used to tokenize search queries, unless a specific
527
+ * // `tokenize` search option is supplied. When tokenizing an indexed field,
528
+ * // the field name is passed as the second argument.
529
+ * tokenize: (string, _fieldName) => string.split(SPACE_OR_PUNCTUATION),
530
+ *
531
+ * // processTerm: function used to process each tokenized term before
532
+ * // indexing. It can be used for stemming and normalization. Return a falsy
533
+ * // value in order to discard a term. By default, it is also used to process
534
+ * // search queries, unless a specific `processTerm` option is supplied as a
535
+ * // search option. When processing a term from a indexed field, the field
536
+ * // name is passed as the second argument.
537
+ * processTerm: (term, _fieldName) => term.toLowerCase(),
538
+ *
539
+ * // searchOptions: default search options, see the `search` method for
540
+ * // details
541
+ * searchOptions: undefined,
542
+ *
543
+ * // fields: document fields to be indexed. Mandatory, but not set by default
544
+ * fields: undefined
545
+ *
546
+ * // storeFields: document fields to be stored and returned as part of the
547
+ * // search results.
548
+ * storeFields: []
549
+ * })
550
+ * ```
551
+ */
552
+ constructor(options) {
553
+ if ((options === null || options === void 0 ? void 0 : options.fields) == null) {
554
+ throw new Error('MiniSearch: option "fields" must be provided');
555
+ }
556
+ const autoVacuum = options.autoVacuum == null || options.autoVacuum === true ? defaultAutoVacuumOptions : options.autoVacuum;
557
+ this._options = {
558
+ ...defaultOptions,
559
+ ...options,
560
+ autoVacuum,
561
+ searchOptions: { ...defaultSearchOptions, ...options.searchOptions || {} },
562
+ autoSuggestOptions: { ...defaultAutoSuggestOptions, ...options.autoSuggestOptions || {} }
563
+ };
564
+ this._index = new SearchableMap();
565
+ this._documentCount = 0;
566
+ this._documentIds = /* @__PURE__ */ new Map();
567
+ this._idToShortId = /* @__PURE__ */ new Map();
568
+ this._fieldIds = {};
569
+ this._fieldLength = /* @__PURE__ */ new Map();
570
+ this._avgFieldLength = [];
571
+ this._nextId = 0;
572
+ this._storedFields = /* @__PURE__ */ new Map();
573
+ this._dirtCount = 0;
574
+ this._currentVacuum = null;
575
+ this._enqueuedVacuum = null;
576
+ this._enqueuedVacuumConditions = defaultVacuumConditions;
577
+ this.addFields(this._options.fields);
578
+ }
579
+ /**
580
+ * Adds a document to the index
581
+ *
582
+ * @param document The document to be indexed
583
+ */
584
+ add(document) {
585
+ const { extractField, stringifyField, tokenize, processTerm, fields, idField } = this._options;
586
+ const id = extractField(document, idField);
587
+ if (id == null) {
588
+ throw new Error(`MiniSearch: document does not have ID field "${idField}"`);
589
+ }
590
+ if (this._idToShortId.has(id)) {
591
+ throw new Error(`MiniSearch: duplicate ID ${id}`);
592
+ }
593
+ const shortDocumentId = this.addDocumentId(id);
594
+ this.saveStoredFields(shortDocumentId, document);
595
+ for (const field of fields) {
596
+ const fieldValue = extractField(document, field);
597
+ if (fieldValue == null)
598
+ continue;
599
+ const tokens = tokenize(stringifyField(fieldValue, field), field);
600
+ const fieldId = this._fieldIds[field];
601
+ const uniqueTerms = new Set(tokens).size;
602
+ this.addFieldLength(shortDocumentId, fieldId, this._documentCount - 1, uniqueTerms);
603
+ for (const term of tokens) {
604
+ const processedTerm = processTerm(term, field);
605
+ if (Array.isArray(processedTerm)) {
606
+ for (const t of processedTerm) {
607
+ this.addTerm(fieldId, shortDocumentId, t);
608
+ }
609
+ } else if (processedTerm) {
610
+ this.addTerm(fieldId, shortDocumentId, processedTerm);
611
+ }
612
+ }
613
+ }
614
+ }
615
+ /**
616
+ * Adds all the given documents to the index
617
+ *
618
+ * @param documents An array of documents to be indexed
619
+ */
620
+ addAll(documents) {
621
+ for (const document of documents)
622
+ this.add(document);
623
+ }
624
+ /**
625
+ * Adds all the given documents to the index asynchronously.
626
+ *
627
+ * Returns a promise that resolves (to `undefined`) when the indexing is done.
628
+ * This method is useful when index many documents, to avoid blocking the main
629
+ * thread. The indexing is performed asynchronously and in chunks.
630
+ *
631
+ * @param documents An array of documents to be indexed
632
+ * @param options Configuration options
633
+ * @return A promise resolving to `undefined` when the indexing is done
634
+ */
635
+ addAllAsync(documents, options = {}) {
636
+ const { chunkSize = 10 } = options;
637
+ const acc = { chunk: [], promise: Promise.resolve() };
638
+ const { chunk, promise } = documents.reduce(({ chunk: chunk2, promise: promise2 }, document, i) => {
639
+ chunk2.push(document);
640
+ if ((i + 1) % chunkSize === 0) {
641
+ return {
642
+ chunk: [],
643
+ promise: promise2.then(() => new Promise((resolve3) => setTimeout(resolve3, 0))).then(() => this.addAll(chunk2))
644
+ };
645
+ } else {
646
+ return { chunk: chunk2, promise: promise2 };
647
+ }
648
+ }, acc);
649
+ return promise.then(() => this.addAll(chunk));
650
+ }
651
+ /**
652
+ * Removes the given document from the index.
653
+ *
654
+ * The document to remove must NOT have changed between indexing and removal,
655
+ * otherwise the index will be corrupted.
656
+ *
657
+ * This method requires passing the full document to be removed (not just the
658
+ * ID), and immediately removes the document from the inverted index, allowing
659
+ * memory to be released. A convenient alternative is {@link
660
+ * MiniSearch#discard}, which needs only the document ID, and has the same
661
+ * visible effect, but delays cleaning up the index until the next vacuuming.
662
+ *
663
+ * @param document The document to be removed
664
+ */
665
+ remove(document) {
666
+ const { tokenize, processTerm, extractField, stringifyField, fields, idField } = this._options;
667
+ const id = extractField(document, idField);
668
+ if (id == null) {
669
+ throw new Error(`MiniSearch: document does not have ID field "${idField}"`);
670
+ }
671
+ const shortId = this._idToShortId.get(id);
672
+ if (shortId == null) {
673
+ throw new Error(`MiniSearch: cannot remove document with ID ${id}: it is not in the index`);
674
+ }
675
+ for (const field of fields) {
676
+ const fieldValue = extractField(document, field);
677
+ if (fieldValue == null)
678
+ continue;
679
+ const tokens = tokenize(stringifyField(fieldValue, field), field);
680
+ const fieldId = this._fieldIds[field];
681
+ const uniqueTerms = new Set(tokens).size;
682
+ this.removeFieldLength(shortId, fieldId, this._documentCount, uniqueTerms);
683
+ for (const term of tokens) {
684
+ const processedTerm = processTerm(term, field);
685
+ if (Array.isArray(processedTerm)) {
686
+ for (const t of processedTerm) {
687
+ this.removeTerm(fieldId, shortId, t);
688
+ }
689
+ } else if (processedTerm) {
690
+ this.removeTerm(fieldId, shortId, processedTerm);
691
+ }
692
+ }
693
+ }
694
+ this._storedFields.delete(shortId);
695
+ this._documentIds.delete(shortId);
696
+ this._idToShortId.delete(id);
697
+ this._fieldLength.delete(shortId);
698
+ this._documentCount -= 1;
699
+ }
700
+ /**
701
+ * Removes all the given documents from the index. If called with no arguments,
702
+ * it removes _all_ documents from the index.
703
+ *
704
+ * @param documents The documents to be removed. If this argument is omitted,
705
+ * all documents are removed. Note that, for removing all documents, it is
706
+ * more efficient to call this method with no arguments than to pass all
707
+ * documents.
708
+ */
709
+ removeAll(documents) {
710
+ if (documents) {
711
+ for (const document of documents)
712
+ this.remove(document);
713
+ } else if (arguments.length > 0) {
714
+ throw new Error("Expected documents to be present. Omit the argument to remove all documents.");
715
+ } else {
716
+ this._index = new SearchableMap();
717
+ this._documentCount = 0;
718
+ this._documentIds = /* @__PURE__ */ new Map();
719
+ this._idToShortId = /* @__PURE__ */ new Map();
720
+ this._fieldLength = /* @__PURE__ */ new Map();
721
+ this._avgFieldLength = [];
722
+ this._storedFields = /* @__PURE__ */ new Map();
723
+ this._nextId = 0;
724
+ }
725
+ }
726
+ /**
727
+ * Discards the document with the given ID, so it won't appear in search results
728
+ *
729
+ * It has the same visible effect of {@link MiniSearch.remove} (both cause the
730
+ * document to stop appearing in searches), but a different effect on the
731
+ * internal data structures:
732
+ *
733
+ * - {@link MiniSearch#remove} requires passing the full document to be
734
+ * removed as argument, and removes it from the inverted index immediately.
735
+ *
736
+ * - {@link MiniSearch#discard} instead only needs the document ID, and
737
+ * works by marking the current version of the document as discarded, so it
738
+ * is immediately ignored by searches. This is faster and more convenient
739
+ * than {@link MiniSearch#remove}, but the index is not immediately
740
+ * modified. To take care of that, vacuuming is performed after a certain
741
+ * number of documents are discarded, cleaning up the index and allowing
742
+ * memory to be released.
743
+ *
744
+ * After discarding a document, it is possible to re-add a new version, and
745
+ * only the new version will appear in searches. In other words, discarding
746
+ * and re-adding a document works exactly like removing and re-adding it. The
747
+ * {@link MiniSearch.replace} method can also be used to replace a document
748
+ * with a new version.
749
+ *
750
+ * #### Details about vacuuming
751
+ *
752
+ * Repetite calls to this method would leave obsolete document references in
753
+ * the index, invisible to searches. Two mechanisms take care of cleaning up:
754
+ * clean up during search, and vacuuming.
755
+ *
756
+ * - Upon search, whenever a discarded ID is found (and ignored for the
757
+ * results), references to the discarded document are removed from the
758
+ * inverted index entries for the search terms. This ensures that subsequent
759
+ * searches for the same terms do not need to skip these obsolete references
760
+ * again.
761
+ *
762
+ * - In addition, vacuuming is performed automatically by default (see the
763
+ * `autoVacuum` field in {@link Options}) after a certain number of
764
+ * documents are discarded. Vacuuming traverses all terms in the index,
765
+ * cleaning up all references to discarded documents. Vacuuming can also be
766
+ * triggered manually by calling {@link MiniSearch#vacuum}.
767
+ *
768
+ * @param id The ID of the document to be discarded
769
+ */
770
+ discard(id) {
771
+ const shortId = this._idToShortId.get(id);
772
+ if (shortId == null) {
773
+ throw new Error(`MiniSearch: cannot discard document with ID ${id}: it is not in the index`);
774
+ }
775
+ this._idToShortId.delete(id);
776
+ this._documentIds.delete(shortId);
777
+ this._storedFields.delete(shortId);
778
+ (this._fieldLength.get(shortId) || []).forEach((fieldLength, fieldId) => {
779
+ this.removeFieldLength(shortId, fieldId, this._documentCount, fieldLength);
780
+ });
781
+ this._fieldLength.delete(shortId);
782
+ this._documentCount -= 1;
783
+ this._dirtCount += 1;
784
+ this.maybeAutoVacuum();
785
+ }
786
+ maybeAutoVacuum() {
787
+ if (this._options.autoVacuum === false) {
788
+ return;
789
+ }
790
+ const { minDirtFactor, minDirtCount, batchSize, batchWait } = this._options.autoVacuum;
791
+ this.conditionalVacuum({ batchSize, batchWait }, { minDirtCount, minDirtFactor });
792
+ }
793
+ /**
794
+ * Discards the documents with the given IDs, so they won't appear in search
795
+ * results
796
+ *
797
+ * It is equivalent to calling {@link MiniSearch#discard} for all the given
798
+ * IDs, but with the optimization of triggering at most one automatic
799
+ * vacuuming at the end.
800
+ *
801
+ * Note: to remove all documents from the index, it is faster and more
802
+ * convenient to call {@link MiniSearch.removeAll} with no argument, instead
803
+ * of passing all IDs to this method.
804
+ */
805
+ discardAll(ids) {
806
+ const autoVacuum = this._options.autoVacuum;
807
+ try {
808
+ this._options.autoVacuum = false;
809
+ for (const id of ids) {
810
+ this.discard(id);
811
+ }
812
+ } finally {
813
+ this._options.autoVacuum = autoVacuum;
814
+ }
815
+ this.maybeAutoVacuum();
816
+ }
817
+ /**
818
+ * It replaces an existing document with the given updated version
819
+ *
820
+ * It works by discarding the current version and adding the updated one, so
821
+ * it is functionally equivalent to calling {@link MiniSearch#discard}
822
+ * followed by {@link MiniSearch#add}. The ID of the updated document should
823
+ * be the same as the original one.
824
+ *
825
+ * Since it uses {@link MiniSearch#discard} internally, this method relies on
826
+ * vacuuming to clean up obsolete document references from the index, allowing
827
+ * memory to be released (see {@link MiniSearch#discard}).
828
+ *
829
+ * @param updatedDocument The updated document to replace the old version
830
+ * with
831
+ */
832
+ replace(updatedDocument) {
833
+ const { idField, extractField } = this._options;
834
+ const id = extractField(updatedDocument, idField);
835
+ this.discard(id);
836
+ this.add(updatedDocument);
837
+ }
838
+ /**
839
+ * Triggers a manual vacuuming, cleaning up references to discarded documents
840
+ * from the inverted index
841
+ *
842
+ * Vacuuming is only useful for applications that use the {@link
843
+ * MiniSearch#discard} or {@link MiniSearch#replace} methods.
844
+ *
845
+ * By default, vacuuming is performed automatically when needed (controlled by
846
+ * the `autoVacuum` field in {@link Options}), so there is usually no need to
847
+ * call this method, unless one wants to make sure to perform vacuuming at a
848
+ * specific moment.
849
+ *
850
+ * Vacuuming traverses all terms in the inverted index in batches, and cleans
851
+ * up references to discarded documents from the posting list, allowing memory
852
+ * to be released.
853
+ *
854
+ * The method takes an optional object as argument with the following keys:
855
+ *
856
+ * - `batchSize`: the size of each batch (1000 by default)
857
+ *
858
+ * - `batchWait`: the number of milliseconds to wait between batches (10 by
859
+ * default)
860
+ *
861
+ * On large indexes, vacuuming could have a non-negligible cost: batching
862
+ * avoids blocking the thread for long, diluting this cost so that it is not
863
+ * negatively affecting the application. Nonetheless, this method should only
864
+ * be called when necessary, and relying on automatic vacuuming is usually
865
+ * better.
866
+ *
867
+ * It returns a promise that resolves (to undefined) when the clean up is
868
+ * completed. If vacuuming is already ongoing at the time this method is
869
+ * called, a new one is enqueued immediately after the ongoing one, and a
870
+ * corresponding promise is returned. However, no more than one vacuuming is
871
+ * enqueued on top of the ongoing one, even if this method is called more
872
+ * times (enqueuing multiple ones would be useless).
873
+ *
874
+ * @param options Configuration options for the batch size and delay. See
875
+ * {@link VacuumOptions}.
876
+ */
877
+ vacuum(options = {}) {
878
+ return this.conditionalVacuum(options);
879
+ }
880
+ conditionalVacuum(options, conditions) {
881
+ if (this._currentVacuum) {
882
+ this._enqueuedVacuumConditions = this._enqueuedVacuumConditions && conditions;
883
+ if (this._enqueuedVacuum != null) {
884
+ return this._enqueuedVacuum;
885
+ }
886
+ this._enqueuedVacuum = this._currentVacuum.then(() => {
887
+ const conditions2 = this._enqueuedVacuumConditions;
888
+ this._enqueuedVacuumConditions = defaultVacuumConditions;
889
+ return this.performVacuuming(options, conditions2);
890
+ });
891
+ return this._enqueuedVacuum;
892
+ }
893
+ if (this.vacuumConditionsMet(conditions) === false) {
894
+ return Promise.resolve();
895
+ }
896
+ this._currentVacuum = this.performVacuuming(options);
897
+ return this._currentVacuum;
898
+ }
899
+ async performVacuuming(options, conditions) {
900
+ const initialDirtCount = this._dirtCount;
901
+ if (this.vacuumConditionsMet(conditions)) {
902
+ const batchSize = options.batchSize || defaultVacuumOptions.batchSize;
903
+ const batchWait = options.batchWait || defaultVacuumOptions.batchWait;
904
+ let i = 1;
905
+ for (const [term, fieldsData] of this._index) {
906
+ for (const [fieldId, fieldIndex] of fieldsData) {
907
+ for (const [shortId] of fieldIndex) {
908
+ if (this._documentIds.has(shortId)) {
909
+ continue;
910
+ }
911
+ if (fieldIndex.size <= 1) {
912
+ fieldsData.delete(fieldId);
913
+ } else {
914
+ fieldIndex.delete(shortId);
915
+ }
916
+ }
917
+ }
918
+ if (this._index.get(term).size === 0) {
919
+ this._index.delete(term);
920
+ }
921
+ if (i % batchSize === 0) {
922
+ await new Promise((resolve3) => setTimeout(resolve3, batchWait));
923
+ }
924
+ i += 1;
925
+ }
926
+ this._dirtCount -= initialDirtCount;
927
+ }
928
+ await null;
929
+ this._currentVacuum = this._enqueuedVacuum;
930
+ this._enqueuedVacuum = null;
931
+ }
932
+ vacuumConditionsMet(conditions) {
933
+ if (conditions == null) {
934
+ return true;
935
+ }
936
+ let { minDirtCount, minDirtFactor } = conditions;
937
+ minDirtCount = minDirtCount || defaultAutoVacuumOptions.minDirtCount;
938
+ minDirtFactor = minDirtFactor || defaultAutoVacuumOptions.minDirtFactor;
939
+ return this.dirtCount >= minDirtCount && this.dirtFactor >= minDirtFactor;
940
+ }
941
+ /**
942
+ * Is `true` if a vacuuming operation is ongoing, `false` otherwise
943
+ */
944
+ get isVacuuming() {
945
+ return this._currentVacuum != null;
946
+ }
947
+ /**
948
+ * The number of documents discarded since the most recent vacuuming
949
+ */
950
+ get dirtCount() {
951
+ return this._dirtCount;
952
+ }
953
+ /**
954
+ * A number between 0 and 1 giving an indication about the proportion of
955
+ * documents that are discarded, and can therefore be cleaned up by vacuuming.
956
+ * A value close to 0 means that the index is relatively clean, while a higher
957
+ * value means that the index is relatively dirty, and vacuuming could release
958
+ * memory.
959
+ */
960
+ get dirtFactor() {
961
+ return this._dirtCount / (1 + this._documentCount + this._dirtCount);
962
+ }
963
+ /**
964
+ * Returns `true` if a document with the given ID is present in the index and
965
+ * available for search, `false` otherwise
966
+ *
967
+ * @param id The document ID
968
+ */
969
+ has(id) {
970
+ return this._idToShortId.has(id);
971
+ }
972
+ /**
973
+ * Returns the stored fields (as configured in the `storeFields` constructor
974
+ * option) for the given document ID. Returns `undefined` if the document is
975
+ * not present in the index.
976
+ *
977
+ * @param id The document ID
978
+ */
979
+ getStoredFields(id) {
980
+ const shortId = this._idToShortId.get(id);
981
+ if (shortId == null) {
982
+ return void 0;
983
+ }
984
+ return this._storedFields.get(shortId);
985
+ }
986
+ /**
987
+ * Search for documents matching the given search query.
988
+ *
989
+ * The result is a list of scored document IDs matching the query, sorted by
990
+ * descending score, and each including data about which terms were matched and
991
+ * in which fields.
992
+ *
993
+ * ### Basic usage:
994
+ *
995
+ * ```javascript
996
+ * // Search for "zen art motorcycle" with default options: terms have to match
997
+ * // exactly, and individual terms are joined with OR
998
+ * miniSearch.search('zen art motorcycle')
999
+ * // => [ { id: 2, score: 2.77258, match: { ... } }, { id: 4, score: 1.38629, match: { ... } } ]
1000
+ * ```
1001
+ *
1002
+ * ### Restrict search to specific fields:
1003
+ *
1004
+ * ```javascript
1005
+ * // Search only in the 'title' field
1006
+ * miniSearch.search('zen', { fields: ['title'] })
1007
+ * ```
1008
+ *
1009
+ * ### Field boosting:
1010
+ *
1011
+ * ```javascript
1012
+ * // Boost a field
1013
+ * miniSearch.search('zen', { boost: { title: 2 } })
1014
+ * ```
1015
+ *
1016
+ * ### Prefix search:
1017
+ *
1018
+ * ```javascript
1019
+ * // Search for "moto" with prefix search (it will match documents
1020
+ * // containing terms that start with "moto" or "neuro")
1021
+ * miniSearch.search('moto neuro', { prefix: true })
1022
+ * ```
1023
+ *
1024
+ * ### Fuzzy search:
1025
+ *
1026
+ * ```javascript
1027
+ * // Search for "ismael" with fuzzy search (it will match documents containing
1028
+ * // terms similar to "ismael", with a maximum edit distance of 0.2 term.length
1029
+ * // (rounded to nearest integer)
1030
+ * miniSearch.search('ismael', { fuzzy: 0.2 })
1031
+ * ```
1032
+ *
1033
+ * ### Combining strategies:
1034
+ *
1035
+ * ```javascript
1036
+ * // Mix of exact match, prefix search, and fuzzy search
1037
+ * miniSearch.search('ismael mob', {
1038
+ * prefix: true,
1039
+ * fuzzy: 0.2
1040
+ * })
1041
+ * ```
1042
+ *
1043
+ * ### Advanced prefix and fuzzy search:
1044
+ *
1045
+ * ```javascript
1046
+ * // Perform fuzzy and prefix search depending on the search term. Here
1047
+ * // performing prefix and fuzzy search only on terms longer than 3 characters
1048
+ * miniSearch.search('ismael mob', {
1049
+ * prefix: term => term.length > 3
1050
+ * fuzzy: term => term.length > 3 ? 0.2 : null
1051
+ * })
1052
+ * ```
1053
+ *
1054
+ * ### Combine with AND:
1055
+ *
1056
+ * ```javascript
1057
+ * // Combine search terms with AND (to match only documents that contain both
1058
+ * // "motorcycle" and "art")
1059
+ * miniSearch.search('motorcycle art', { combineWith: 'AND' })
1060
+ * ```
1061
+ *
1062
+ * ### Combine with AND_NOT:
1063
+ *
1064
+ * There is also an AND_NOT combinator, that finds documents that match the
1065
+ * first term, but do not match any of the other terms. This combinator is
1066
+ * rarely useful with simple queries, and is meant to be used with advanced
1067
+ * query combinations (see later for more details).
1068
+ *
1069
+ * ### Filtering results:
1070
+ *
1071
+ * ```javascript
1072
+ * // Filter only results in the 'fiction' category (assuming that 'category'
1073
+ * // is a stored field)
1074
+ * miniSearch.search('motorcycle art', {
1075
+ * filter: (result) => result.category === 'fiction'
1076
+ * })
1077
+ * ```
1078
+ *
1079
+ * ### Wildcard query
1080
+ *
1081
+ * Searching for an empty string (assuming the default tokenizer) returns no
1082
+ * results. Sometimes though, one needs to match all documents, like in a
1083
+ * "wildcard" search. This is possible by passing the special value
1084
+ * {@link MiniSearch.wildcard} as the query:
1085
+ *
1086
+ * ```javascript
1087
+ * // Return search results for all documents
1088
+ * miniSearch.search(MiniSearch.wildcard)
1089
+ * ```
1090
+ *
1091
+ * Note that search options such as `filter` and `boostDocument` are still
1092
+ * applied, influencing which results are returned, and their order:
1093
+ *
1094
+ * ```javascript
1095
+ * // Return search results for all documents in the 'fiction' category
1096
+ * miniSearch.search(MiniSearch.wildcard, {
1097
+ * filter: (result) => result.category === 'fiction'
1098
+ * })
1099
+ * ```
1100
+ *
1101
+ * ### Advanced combination of queries:
1102
+ *
1103
+ * It is possible to combine different subqueries with OR, AND, and AND_NOT,
1104
+ * and even with different search options, by passing a query expression
1105
+ * tree object as the first argument, instead of a string.
1106
+ *
1107
+ * ```javascript
1108
+ * // Search for documents that contain "zen" and ("motorcycle" or "archery")
1109
+ * miniSearch.search({
1110
+ * combineWith: 'AND',
1111
+ * queries: [
1112
+ * 'zen',
1113
+ * {
1114
+ * combineWith: 'OR',
1115
+ * queries: ['motorcycle', 'archery']
1116
+ * }
1117
+ * ]
1118
+ * })
1119
+ *
1120
+ * // Search for documents that contain ("apple" or "pear") but not "juice" and
1121
+ * // not "tree"
1122
+ * miniSearch.search({
1123
+ * combineWith: 'AND_NOT',
1124
+ * queries: [
1125
+ * {
1126
+ * combineWith: 'OR',
1127
+ * queries: ['apple', 'pear']
1128
+ * },
1129
+ * 'juice',
1130
+ * 'tree'
1131
+ * ]
1132
+ * })
1133
+ * ```
1134
+ *
1135
+ * Each node in the expression tree can be either a string, or an object that
1136
+ * supports all {@link SearchOptions} fields, plus a `queries` array field for
1137
+ * subqueries.
1138
+ *
1139
+ * Note that, while this can become complicated to do by hand for complex or
1140
+ * deeply nested queries, it provides a formalized expression tree API for
1141
+ * external libraries that implement a parser for custom query languages.
1142
+ *
1143
+ * @param query Search query
1144
+ * @param searchOptions Search options. Each option, if not given, defaults to the corresponding value of `searchOptions` given to the constructor, or to the library default.
1145
+ */
1146
+ search(query, searchOptions = {}) {
1147
+ const { searchOptions: globalSearchOptions } = this._options;
1148
+ const searchOptionsWithDefaults = { ...globalSearchOptions, ...searchOptions };
1149
+ const rawResults = this.executeQuery(query, searchOptions);
1150
+ const results = [];
1151
+ for (const [docId, { score, terms, match }] of rawResults) {
1152
+ const quality = terms.length || 1;
1153
+ const result = {
1154
+ id: this._documentIds.get(docId),
1155
+ score: score * quality,
1156
+ terms: Object.keys(match),
1157
+ queryTerms: terms,
1158
+ match
1159
+ };
1160
+ Object.assign(result, this._storedFields.get(docId));
1161
+ if (searchOptionsWithDefaults.filter == null || searchOptionsWithDefaults.filter(result)) {
1162
+ results.push(result);
1163
+ }
1164
+ }
1165
+ if (query === _MiniSearch.wildcard && searchOptionsWithDefaults.boostDocument == null) {
1166
+ return results;
1167
+ }
1168
+ results.sort(byScore);
1169
+ return results;
1170
+ }
1171
+ /**
1172
+ * Provide suggestions for the given search query
1173
+ *
1174
+ * The result is a list of suggested modified search queries, derived from the
1175
+ * given search query, each with a relevance score, sorted by descending score.
1176
+ *
1177
+ * By default, it uses the same options used for search, except that by
1178
+ * default it performs prefix search on the last term of the query, and
1179
+ * combine terms with `'AND'` (requiring all query terms to match). Custom
1180
+ * options can be passed as a second argument. Defaults can be changed upon
1181
+ * calling the {@link MiniSearch} constructor, by passing a
1182
+ * `autoSuggestOptions` option.
1183
+ *
1184
+ * ### Basic usage:
1185
+ *
1186
+ * ```javascript
1187
+ * // Get suggestions for 'neuro':
1188
+ * miniSearch.autoSuggest('neuro')
1189
+ * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ]
1190
+ * ```
1191
+ *
1192
+ * ### Multiple words:
1193
+ *
1194
+ * ```javascript
1195
+ * // Get suggestions for 'zen ar':
1196
+ * miniSearch.autoSuggest('zen ar')
1197
+ * // => [
1198
+ * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
1199
+ * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
1200
+ * // ]
1201
+ * ```
1202
+ *
1203
+ * ### Fuzzy suggestions:
1204
+ *
1205
+ * ```javascript
1206
+ * // Correct spelling mistakes using fuzzy search:
1207
+ * miniSearch.autoSuggest('neromancer', { fuzzy: 0.2 })
1208
+ * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ]
1209
+ * ```
1210
+ *
1211
+ * ### Filtering:
1212
+ *
1213
+ * ```javascript
1214
+ * // Get suggestions for 'zen ar', but only within the 'fiction' category
1215
+ * // (assuming that 'category' is a stored field):
1216
+ * miniSearch.autoSuggest('zen ar', {
1217
+ * filter: (result) => result.category === 'fiction'
1218
+ * })
1219
+ * // => [
1220
+ * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
1221
+ * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
1222
+ * // ]
1223
+ * ```
1224
+ *
1225
+ * @param queryString Query string to be expanded into suggestions
1226
+ * @param options Search options. The supported options and default values
1227
+ * are the same as for the {@link MiniSearch#search} method, except that by
1228
+ * default prefix search is performed on the last term in the query, and terms
1229
+ * are combined with `'AND'`.
1230
+ * @return A sorted array of suggestions sorted by relevance score.
1231
+ */
1232
+ autoSuggest(queryString, options = {}) {
1233
+ options = { ...this._options.autoSuggestOptions, ...options };
1234
+ const suggestions = /* @__PURE__ */ new Map();
1235
+ for (const { score, terms } of this.search(queryString, options)) {
1236
+ const phrase = terms.join(" ");
1237
+ const suggestion = suggestions.get(phrase);
1238
+ if (suggestion != null) {
1239
+ suggestion.score += score;
1240
+ suggestion.count += 1;
1241
+ } else {
1242
+ suggestions.set(phrase, { score, terms, count: 1 });
1243
+ }
1244
+ }
1245
+ const results = [];
1246
+ for (const [suggestion, { score, terms, count }] of suggestions) {
1247
+ results.push({ suggestion, terms, score: score / count });
1248
+ }
1249
+ results.sort(byScore);
1250
+ return results;
1251
+ }
1252
+ /**
1253
+ * Total number of documents available to search
1254
+ */
1255
+ get documentCount() {
1256
+ return this._documentCount;
1257
+ }
1258
+ /**
1259
+ * Number of terms in the index
1260
+ */
1261
+ get termCount() {
1262
+ return this._index.size;
1263
+ }
1264
+ /**
1265
+ * Deserializes a JSON index (serialized with `JSON.stringify(miniSearch)`)
1266
+ * and instantiates a MiniSearch instance. It should be given the same options
1267
+ * originally used when serializing the index.
1268
+ *
1269
+ * ### Usage:
1270
+ *
1271
+ * ```javascript
1272
+ * // If the index was serialized with:
1273
+ * let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
1274
+ * miniSearch.addAll(documents)
1275
+ *
1276
+ * const json = JSON.stringify(miniSearch)
1277
+ * // It can later be deserialized like this:
1278
+ * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
1279
+ * ```
1280
+ *
1281
+ * @param json JSON-serialized index
1282
+ * @param options configuration options, same as the constructor
1283
+ * @return An instance of MiniSearch deserialized from the given JSON.
1284
+ */
1285
+ static loadJSON(json, options) {
1286
+ if (options == null) {
1287
+ throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");
1288
+ }
1289
+ return this.loadJS(JSON.parse(json), options);
1290
+ }
1291
+ /**
1292
+ * Async equivalent of {@link MiniSearch.loadJSON}
1293
+ *
1294
+ * This function is an alternative to {@link MiniSearch.loadJSON} that returns
1295
+ * a promise, and loads the index in batches, leaving pauses between them to avoid
1296
+ * blocking the main thread. It tends to be slower than the synchronous
1297
+ * version, but does not block the main thread, so it can be a better choice
1298
+ * when deserializing very large indexes.
1299
+ *
1300
+ * @param json JSON-serialized index
1301
+ * @param options configuration options, same as the constructor
1302
+ * @return A Promise that will resolve to an instance of MiniSearch deserialized from the given JSON.
1303
+ */
1304
+ static async loadJSONAsync(json, options) {
1305
+ if (options == null) {
1306
+ throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");
1307
+ }
1308
+ return this.loadJSAsync(JSON.parse(json), options);
1309
+ }
1310
+ /**
1311
+ * Returns the default value of an option. It will throw an error if no option
1312
+ * with the given name exists.
1313
+ *
1314
+ * @param optionName Name of the option
1315
+ * @return The default value of the given option
1316
+ *
1317
+ * ### Usage:
1318
+ *
1319
+ * ```javascript
1320
+ * // Get default tokenizer
1321
+ * MiniSearch.getDefault('tokenize')
1322
+ *
1323
+ * // Get default term processor
1324
+ * MiniSearch.getDefault('processTerm')
1325
+ *
1326
+ * // Unknown options will throw an error
1327
+ * MiniSearch.getDefault('notExisting')
1328
+ * // => throws 'MiniSearch: unknown option "notExisting"'
1329
+ * ```
1330
+ */
1331
+ static getDefault(optionName) {
1332
+ if (defaultOptions.hasOwnProperty(optionName)) {
1333
+ return getOwnProperty(defaultOptions, optionName);
1334
+ } else {
1335
+ throw new Error(`MiniSearch: unknown option "${optionName}"`);
1336
+ }
1337
+ }
1338
+ /**
1339
+ * @ignore
1340
+ */
1341
+ static loadJS(js, options) {
1342
+ const { index, documentIds, fieldLength, storedFields, serializationVersion } = js;
1343
+ const miniSearch = this.instantiateMiniSearch(js, options);
1344
+ miniSearch._documentIds = objectToNumericMap(documentIds);
1345
+ miniSearch._fieldLength = objectToNumericMap(fieldLength);
1346
+ miniSearch._storedFields = objectToNumericMap(storedFields);
1347
+ for (const [shortId, id] of miniSearch._documentIds) {
1348
+ miniSearch._idToShortId.set(id, shortId);
1349
+ }
1350
+ for (const [term, data] of index) {
1351
+ const dataMap = /* @__PURE__ */ new Map();
1352
+ for (const fieldId of Object.keys(data)) {
1353
+ let indexEntry = data[fieldId];
1354
+ if (serializationVersion === 1) {
1355
+ indexEntry = indexEntry.ds;
1356
+ }
1357
+ dataMap.set(parseInt(fieldId, 10), objectToNumericMap(indexEntry));
1358
+ }
1359
+ miniSearch._index.set(term, dataMap);
1360
+ }
1361
+ return miniSearch;
1362
+ }
1363
+ /**
1364
+ * @ignore
1365
+ */
1366
+ static async loadJSAsync(js, options) {
1367
+ const { index, documentIds, fieldLength, storedFields, serializationVersion } = js;
1368
+ const miniSearch = this.instantiateMiniSearch(js, options);
1369
+ miniSearch._documentIds = await objectToNumericMapAsync(documentIds);
1370
+ miniSearch._fieldLength = await objectToNumericMapAsync(fieldLength);
1371
+ miniSearch._storedFields = await objectToNumericMapAsync(storedFields);
1372
+ for (const [shortId, id] of miniSearch._documentIds) {
1373
+ miniSearch._idToShortId.set(id, shortId);
1374
+ }
1375
+ let count = 0;
1376
+ for (const [term, data] of index) {
1377
+ const dataMap = /* @__PURE__ */ new Map();
1378
+ for (const fieldId of Object.keys(data)) {
1379
+ let indexEntry = data[fieldId];
1380
+ if (serializationVersion === 1) {
1381
+ indexEntry = indexEntry.ds;
1382
+ }
1383
+ dataMap.set(parseInt(fieldId, 10), await objectToNumericMapAsync(indexEntry));
1384
+ }
1385
+ if (++count % 1e3 === 0)
1386
+ await wait(0);
1387
+ miniSearch._index.set(term, dataMap);
1388
+ }
1389
+ return miniSearch;
1390
+ }
1391
+ /**
1392
+ * @ignore
1393
+ */
1394
+ static instantiateMiniSearch(js, options) {
1395
+ const { documentCount, nextId, fieldIds, averageFieldLength, dirtCount, serializationVersion } = js;
1396
+ if (serializationVersion !== 1 && serializationVersion !== 2) {
1397
+ throw new Error("MiniSearch: cannot deserialize an index created with an incompatible version");
1398
+ }
1399
+ const miniSearch = new _MiniSearch(options);
1400
+ miniSearch._documentCount = documentCount;
1401
+ miniSearch._nextId = nextId;
1402
+ miniSearch._idToShortId = /* @__PURE__ */ new Map();
1403
+ miniSearch._fieldIds = fieldIds;
1404
+ miniSearch._avgFieldLength = averageFieldLength;
1405
+ miniSearch._dirtCount = dirtCount || 0;
1406
+ miniSearch._index = new SearchableMap();
1407
+ return miniSearch;
1408
+ }
1409
+ /**
1410
+ * @ignore
1411
+ */
1412
+ executeQuery(query, searchOptions = {}) {
1413
+ if (query === _MiniSearch.wildcard) {
1414
+ return this.executeWildcardQuery(searchOptions);
1415
+ }
1416
+ if (typeof query !== "string") {
1417
+ const options2 = { ...searchOptions, ...query, queries: void 0 };
1418
+ const results2 = query.queries.map((subquery) => this.executeQuery(subquery, options2));
1419
+ return this.combineResults(results2, options2.combineWith);
1420
+ }
1421
+ const { tokenize, processTerm, searchOptions: globalSearchOptions } = this._options;
1422
+ const options = { tokenize, processTerm, ...globalSearchOptions, ...searchOptions };
1423
+ const { tokenize: searchTokenize, processTerm: searchProcessTerm } = options;
1424
+ const terms = searchTokenize(query).flatMap((term) => searchProcessTerm(term)).filter((term) => !!term);
1425
+ const queries = terms.map(termToQuerySpec(options));
1426
+ const results = queries.map((query2) => this.executeQuerySpec(query2, options));
1427
+ return this.combineResults(results, options.combineWith);
1428
+ }
1429
+ /**
1430
+ * @ignore
1431
+ */
1432
+ executeQuerySpec(query, searchOptions) {
1433
+ const options = { ...this._options.searchOptions, ...searchOptions };
1434
+ const boosts = (options.fields || this._options.fields).reduce((boosts2, field) => ({ ...boosts2, [field]: getOwnProperty(options.boost, field) || 1 }), {});
1435
+ const { boostDocument, weights, maxFuzzy, bm25: bm25params } = options;
1436
+ const { fuzzy: fuzzyWeight, prefix: prefixWeight } = { ...defaultSearchOptions.weights, ...weights };
1437
+ const data = this._index.get(query.term);
1438
+ const results = this.termResults(query.term, query.term, 1, query.termBoost, data, boosts, boostDocument, bm25params);
1439
+ let prefixMatches;
1440
+ let fuzzyMatches;
1441
+ if (query.prefix) {
1442
+ prefixMatches = this._index.atPrefix(query.term);
1443
+ }
1444
+ if (query.fuzzy) {
1445
+ const fuzzy = query.fuzzy === true ? 0.2 : query.fuzzy;
1446
+ const maxDistance = fuzzy < 1 ? Math.min(maxFuzzy, Math.round(query.term.length * fuzzy)) : fuzzy;
1447
+ if (maxDistance)
1448
+ fuzzyMatches = this._index.fuzzyGet(query.term, maxDistance);
1449
+ }
1450
+ if (prefixMatches) {
1451
+ for (const [term, data2] of prefixMatches) {
1452
+ const distance = term.length - query.term.length;
1453
+ if (!distance) {
1454
+ continue;
1455
+ }
1456
+ fuzzyMatches === null || fuzzyMatches === void 0 ? void 0 : fuzzyMatches.delete(term);
1457
+ const weight = prefixWeight * term.length / (term.length + 0.3 * distance);
1458
+ this.termResults(query.term, term, weight, query.termBoost, data2, boosts, boostDocument, bm25params, results);
1459
+ }
1460
+ }
1461
+ if (fuzzyMatches) {
1462
+ for (const term of fuzzyMatches.keys()) {
1463
+ const [data2, distance] = fuzzyMatches.get(term);
1464
+ if (!distance) {
1465
+ continue;
1466
+ }
1467
+ const weight = fuzzyWeight * term.length / (term.length + distance);
1468
+ this.termResults(query.term, term, weight, query.termBoost, data2, boosts, boostDocument, bm25params, results);
1469
+ }
1470
+ }
1471
+ return results;
1472
+ }
1473
+ /**
1474
+ * @ignore
1475
+ */
1476
+ executeWildcardQuery(searchOptions) {
1477
+ const results = /* @__PURE__ */ new Map();
1478
+ const options = { ...this._options.searchOptions, ...searchOptions };
1479
+ for (const [shortId, id] of this._documentIds) {
1480
+ const score = options.boostDocument ? options.boostDocument(id, "", this._storedFields.get(shortId)) : 1;
1481
+ results.set(shortId, {
1482
+ score,
1483
+ terms: [],
1484
+ match: {}
1485
+ });
1486
+ }
1487
+ return results;
1488
+ }
1489
+ /**
1490
+ * @ignore
1491
+ */
1492
+ combineResults(results, combineWith = OR) {
1493
+ if (results.length === 0) {
1494
+ return /* @__PURE__ */ new Map();
1495
+ }
1496
+ const operator = combineWith.toLowerCase();
1497
+ const combinator = combinators[operator];
1498
+ if (!combinator) {
1499
+ throw new Error(`Invalid combination operator: ${combineWith}`);
1500
+ }
1501
+ return results.reduce(combinator) || /* @__PURE__ */ new Map();
1502
+ }
1503
+ /**
1504
+ * Allows serialization of the index to JSON, to possibly store it and later
1505
+ * deserialize it with {@link MiniSearch.loadJSON}.
1506
+ *
1507
+ * Normally one does not directly call this method, but rather call the
1508
+ * standard JavaScript `JSON.stringify()` passing the {@link MiniSearch}
1509
+ * instance, and JavaScript will internally call this method. Upon
1510
+ * deserialization, one must pass to {@link MiniSearch.loadJSON} the same
1511
+ * options used to create the original instance that was serialized.
1512
+ *
1513
+ * ### Usage:
1514
+ *
1515
+ * ```javascript
1516
+ * // Serialize the index:
1517
+ * let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
1518
+ * miniSearch.addAll(documents)
1519
+ * const json = JSON.stringify(miniSearch)
1520
+ *
1521
+ * // Later, to deserialize it:
1522
+ * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
1523
+ * ```
1524
+ *
1525
+ * @return A plain-object serializable representation of the search index.
1526
+ */
1527
+ toJSON() {
1528
+ const index = [];
1529
+ for (const [term, fieldIndex] of this._index) {
1530
+ const data = {};
1531
+ for (const [fieldId, freqs] of fieldIndex) {
1532
+ data[fieldId] = Object.fromEntries(freqs);
1533
+ }
1534
+ index.push([term, data]);
1535
+ }
1536
+ return {
1537
+ documentCount: this._documentCount,
1538
+ nextId: this._nextId,
1539
+ documentIds: Object.fromEntries(this._documentIds),
1540
+ fieldIds: this._fieldIds,
1541
+ fieldLength: Object.fromEntries(this._fieldLength),
1542
+ averageFieldLength: this._avgFieldLength,
1543
+ storedFields: Object.fromEntries(this._storedFields),
1544
+ dirtCount: this._dirtCount,
1545
+ index,
1546
+ serializationVersion: 2
1547
+ };
1548
+ }
1549
+ /**
1550
+ * @ignore
1551
+ */
1552
+ termResults(sourceTerm, derivedTerm, termWeight, termBoost, fieldTermData, fieldBoosts, boostDocumentFn, bm25params, results = /* @__PURE__ */ new Map()) {
1553
+ if (fieldTermData == null)
1554
+ return results;
1555
+ for (const field of Object.keys(fieldBoosts)) {
1556
+ const fieldBoost = fieldBoosts[field];
1557
+ const fieldId = this._fieldIds[field];
1558
+ const fieldTermFreqs = fieldTermData.get(fieldId);
1559
+ if (fieldTermFreqs == null)
1560
+ continue;
1561
+ let matchingFields = fieldTermFreqs.size;
1562
+ const avgFieldLength = this._avgFieldLength[fieldId];
1563
+ for (const docId of fieldTermFreqs.keys()) {
1564
+ if (!this._documentIds.has(docId)) {
1565
+ this.removeTerm(fieldId, docId, derivedTerm);
1566
+ matchingFields -= 1;
1567
+ continue;
1568
+ }
1569
+ const docBoost = boostDocumentFn ? boostDocumentFn(this._documentIds.get(docId), derivedTerm, this._storedFields.get(docId)) : 1;
1570
+ if (!docBoost)
1571
+ continue;
1572
+ const termFreq = fieldTermFreqs.get(docId);
1573
+ const fieldLength = this._fieldLength.get(docId)[fieldId];
1574
+ const rawScore = calcBM25Score(termFreq, matchingFields, this._documentCount, fieldLength, avgFieldLength, bm25params);
1575
+ const weightedScore = termWeight * termBoost * fieldBoost * docBoost * rawScore;
1576
+ const result = results.get(docId);
1577
+ if (result) {
1578
+ result.score += weightedScore;
1579
+ assignUniqueTerm(result.terms, sourceTerm);
1580
+ const match = getOwnProperty(result.match, derivedTerm);
1581
+ if (match) {
1582
+ match.push(field);
1583
+ } else {
1584
+ result.match[derivedTerm] = [field];
1585
+ }
1586
+ } else {
1587
+ results.set(docId, {
1588
+ score: weightedScore,
1589
+ terms: [sourceTerm],
1590
+ match: { [derivedTerm]: [field] }
1591
+ });
1592
+ }
1593
+ }
1594
+ }
1595
+ return results;
1596
+ }
1597
+ /**
1598
+ * @ignore
1599
+ */
1600
+ addTerm(fieldId, documentId, term) {
1601
+ const indexData = this._index.fetch(term, createMap);
1602
+ let fieldIndex = indexData.get(fieldId);
1603
+ if (fieldIndex == null) {
1604
+ fieldIndex = /* @__PURE__ */ new Map();
1605
+ fieldIndex.set(documentId, 1);
1606
+ indexData.set(fieldId, fieldIndex);
1607
+ } else {
1608
+ const docs = fieldIndex.get(documentId);
1609
+ fieldIndex.set(documentId, (docs || 0) + 1);
1610
+ }
1611
+ }
1612
+ /**
1613
+ * @ignore
1614
+ */
1615
+ removeTerm(fieldId, documentId, term) {
1616
+ if (!this._index.has(term)) {
1617
+ this.warnDocumentChanged(documentId, fieldId, term);
1618
+ return;
1619
+ }
1620
+ const indexData = this._index.fetch(term, createMap);
1621
+ const fieldIndex = indexData.get(fieldId);
1622
+ if (fieldIndex == null || fieldIndex.get(documentId) == null) {
1623
+ this.warnDocumentChanged(documentId, fieldId, term);
1624
+ } else if (fieldIndex.get(documentId) <= 1) {
1625
+ if (fieldIndex.size <= 1) {
1626
+ indexData.delete(fieldId);
1627
+ } else {
1628
+ fieldIndex.delete(documentId);
1629
+ }
1630
+ } else {
1631
+ fieldIndex.set(documentId, fieldIndex.get(documentId) - 1);
1632
+ }
1633
+ if (this._index.get(term).size === 0) {
1634
+ this._index.delete(term);
1635
+ }
1636
+ }
1637
+ /**
1638
+ * @ignore
1639
+ */
1640
+ warnDocumentChanged(shortDocumentId, fieldId, term) {
1641
+ for (const fieldName of Object.keys(this._fieldIds)) {
1642
+ if (this._fieldIds[fieldName] === fieldId) {
1643
+ this._options.logger("warn", `MiniSearch: document with ID ${this._documentIds.get(shortDocumentId)} has changed before removal: term "${term}" was not present in field "${fieldName}". Removing a document after it has changed can corrupt the index!`, "version_conflict");
1644
+ return;
1645
+ }
1646
+ }
1647
+ }
1648
+ /**
1649
+ * @ignore
1650
+ */
1651
+ addDocumentId(documentId) {
1652
+ const shortDocumentId = this._nextId;
1653
+ this._idToShortId.set(documentId, shortDocumentId);
1654
+ this._documentIds.set(shortDocumentId, documentId);
1655
+ this._documentCount += 1;
1656
+ this._nextId += 1;
1657
+ return shortDocumentId;
1658
+ }
1659
+ /**
1660
+ * @ignore
1661
+ */
1662
+ addFields(fields) {
1663
+ for (let i = 0; i < fields.length; i++) {
1664
+ this._fieldIds[fields[i]] = i;
1665
+ }
1666
+ }
1667
+ /**
1668
+ * @ignore
1669
+ */
1670
+ addFieldLength(documentId, fieldId, count, length) {
1671
+ let fieldLengths = this._fieldLength.get(documentId);
1672
+ if (fieldLengths == null)
1673
+ this._fieldLength.set(documentId, fieldLengths = []);
1674
+ fieldLengths[fieldId] = length;
1675
+ const averageFieldLength = this._avgFieldLength[fieldId] || 0;
1676
+ const totalFieldLength = averageFieldLength * count + length;
1677
+ this._avgFieldLength[fieldId] = totalFieldLength / (count + 1);
1678
+ }
1679
+ /**
1680
+ * @ignore
1681
+ */
1682
+ removeFieldLength(documentId, fieldId, count, length) {
1683
+ if (count === 1) {
1684
+ this._avgFieldLength[fieldId] = 0;
1685
+ return;
1686
+ }
1687
+ const totalFieldLength = this._avgFieldLength[fieldId] * count - length;
1688
+ this._avgFieldLength[fieldId] = totalFieldLength / (count - 1);
1689
+ }
1690
+ /**
1691
+ * @ignore
1692
+ */
1693
+ saveStoredFields(documentId, doc) {
1694
+ const { storeFields, extractField } = this._options;
1695
+ if (storeFields == null || storeFields.length === 0) {
1696
+ return;
1697
+ }
1698
+ let documentFields = this._storedFields.get(documentId);
1699
+ if (documentFields == null)
1700
+ this._storedFields.set(documentId, documentFields = {});
1701
+ for (const fieldName of storeFields) {
1702
+ const fieldValue = extractField(doc, fieldName);
1703
+ if (fieldValue !== void 0)
1704
+ documentFields[fieldName] = fieldValue;
1705
+ }
1706
+ }
1707
+ };
1708
+ MiniSearch.wildcard = /* @__PURE__ */ Symbol("*");
1709
+ var getOwnProperty = (object, property) => Object.prototype.hasOwnProperty.call(object, property) ? object[property] : void 0;
1710
+ var combinators = {
1711
+ [OR]: (a, b) => {
1712
+ for (const docId of b.keys()) {
1713
+ const existing = a.get(docId);
1714
+ if (existing == null) {
1715
+ a.set(docId, b.get(docId));
1716
+ } else {
1717
+ const { score, terms, match } = b.get(docId);
1718
+ existing.score = existing.score + score;
1719
+ existing.match = Object.assign(existing.match, match);
1720
+ assignUniqueTerms(existing.terms, terms);
1721
+ }
1722
+ }
1723
+ return a;
1724
+ },
1725
+ [AND]: (a, b) => {
1726
+ const combined = /* @__PURE__ */ new Map();
1727
+ for (const docId of b.keys()) {
1728
+ const existing = a.get(docId);
1729
+ if (existing == null)
1730
+ continue;
1731
+ const { score, terms, match } = b.get(docId);
1732
+ assignUniqueTerms(existing.terms, terms);
1733
+ combined.set(docId, {
1734
+ score: existing.score + score,
1735
+ terms: existing.terms,
1736
+ match: Object.assign(existing.match, match)
1737
+ });
1738
+ }
1739
+ return combined;
1740
+ },
1741
+ [AND_NOT]: (a, b) => {
1742
+ for (const docId of b.keys())
1743
+ a.delete(docId);
1744
+ return a;
1745
+ }
1746
+ };
1747
+ var defaultBM25params = { k: 1.2, b: 0.7, d: 0.5 };
1748
+ var calcBM25Score = (termFreq, matchingCount, totalCount, fieldLength, avgFieldLength, bm25params) => {
1749
+ const { k, b, d } = bm25params;
1750
+ const invDocFreq = Math.log(1 + (totalCount - matchingCount + 0.5) / (matchingCount + 0.5));
1751
+ return invDocFreq * (d + termFreq * (k + 1) / (termFreq + k * (1 - b + b * fieldLength / avgFieldLength)));
1752
+ };
1753
+ var termToQuerySpec = (options) => (term, i, terms) => {
1754
+ const fuzzy = typeof options.fuzzy === "function" ? options.fuzzy(term, i, terms) : options.fuzzy || false;
1755
+ const prefix = typeof options.prefix === "function" ? options.prefix(term, i, terms) : options.prefix === true;
1756
+ const termBoost = typeof options.boostTerm === "function" ? options.boostTerm(term, i, terms) : 1;
1757
+ return { term, fuzzy, prefix, termBoost };
1758
+ };
1759
+ var defaultOptions = {
1760
+ idField: "id",
1761
+ extractField: (document, fieldName) => document[fieldName],
1762
+ stringifyField: (fieldValue, fieldName) => fieldValue.toString(),
1763
+ tokenize: (text) => text.split(SPACE_OR_PUNCTUATION),
1764
+ processTerm: (term) => term.toLowerCase(),
1765
+ fields: void 0,
1766
+ searchOptions: void 0,
1767
+ storeFields: [],
1768
+ logger: (level, message) => {
1769
+ if (typeof (console === null || console === void 0 ? void 0 : console[level]) === "function")
1770
+ console[level](message);
1771
+ },
1772
+ autoVacuum: true
1773
+ };
1774
+ var defaultSearchOptions = {
1775
+ combineWith: OR,
1776
+ prefix: false,
1777
+ fuzzy: false,
1778
+ maxFuzzy: 6,
1779
+ boost: {},
1780
+ weights: { fuzzy: 0.45, prefix: 0.375 },
1781
+ bm25: defaultBM25params
1782
+ };
1783
+ var defaultAutoSuggestOptions = {
1784
+ combineWith: AND,
1785
+ prefix: (term, i, terms) => i === terms.length - 1
1786
+ };
1787
+ var defaultVacuumOptions = { batchSize: 1e3, batchWait: 10 };
1788
+ var defaultVacuumConditions = { minDirtFactor: 0.1, minDirtCount: 20 };
1789
+ var defaultAutoVacuumOptions = { ...defaultVacuumOptions, ...defaultVacuumConditions };
1790
+ var assignUniqueTerm = (target, term) => {
1791
+ if (!target.includes(term))
1792
+ target.push(term);
1793
+ };
1794
+ var assignUniqueTerms = (target, source) => {
1795
+ for (const term of source) {
1796
+ if (!target.includes(term))
1797
+ target.push(term);
1798
+ }
1799
+ };
1800
+ var byScore = ({ score: a }, { score: b }) => b - a;
1801
+ var createMap = () => /* @__PURE__ */ new Map();
1802
+ var objectToNumericMap = (object) => {
1803
+ const map = /* @__PURE__ */ new Map();
1804
+ for (const key of Object.keys(object)) {
1805
+ map.set(parseInt(key, 10), object[key]);
1806
+ }
1807
+ return map;
1808
+ };
1809
+ var objectToNumericMapAsync = async (object) => {
1810
+ const map = /* @__PURE__ */ new Map();
1811
+ let count = 0;
1812
+ for (const key of Object.keys(object)) {
1813
+ map.set(parseInt(key, 10), object[key]);
1814
+ if (++count % 1e3 === 0) {
1815
+ await wait(0);
1816
+ }
1817
+ }
1818
+ return map;
1819
+ };
1820
+ var wait = (ms) => new Promise((resolve3) => setTimeout(resolve3, ms));
1821
+ var SPACE_OR_PUNCTUATION = /[\n\r\p{Z}\p{P}]+/u;
1822
+
1823
+ // ../east-plugin/dist/lib/search-guidance.js
1824
+ var EAST_GUIDANCE = {
1825
+ searchTool: "mcp__plugin_east_east__search_east_examples",
1826
+ getTool: "mcp__plugin_east_east__get_east_example",
1827
+ corpus: "East",
1828
+ scope: "@elaraai"
1829
+ };
1830
+ function bare(tool) {
1831
+ const at = tool.lastIndexOf("__");
1832
+ return at < 0 ? tool : tool.slice(at + 2);
1833
+ }
1834
+ function gateText(g) {
1835
+ return [
1836
+ `STOP: no ${g.corpus} example search on record in this session, and this is ${g.corpus} code.`,
1837
+ `Before writing or changing ${g.corpus} code, search the tested example index \u2014 it is the API reference:`,
1838
+ `1. \`${g.searchTool}\` with what you are about to do (language: "python" for east-py, "typescript" otherwise); summaries come back \u2014 id, signature, inputs and result.`,
1839
+ `2. \`${g.getTool}\` for the one or two that match, and pattern your code on them.`,
1840
+ `Do not read node_modules/${g.scope}/** or *.examples.ts files instead: the index is the same corpus, exact and far cheaper. Every ${g.corpus} skill requires this step.`
1841
+ ].join("\n");
1842
+ }
1843
+ function readText(g) {
1844
+ return [
1845
+ `Note: the ${g.corpus} example index is the API reference \u2014 \`${g.searchTool}\` (then \`${bare(g.getTool)}\`) returns the same tested programs as the ${g.corpus} packages' examples and type declarations, exact, printed in TypeScript or python, at a fraction of the tokens.`,
1846
+ `Reading \`.d.ts\` signatures or sweeping \`*.examples.ts\` files reliably produces broken ${g.corpus} code that still type-checks: the signatures omit the runtime rules. Search instead, and read a specific file only when the search pointed you at it.`
1847
+ ].join("\n");
1848
+ }
1849
+ function corpusReadFor(scope) {
1850
+ const escaped = scope.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1851
+ const packagePath = new RegExp(`[/\\\\]node_modules[/\\\\]${escaped}[/\\\\]`);
1852
+ const packagePattern = new RegExp(`(^|[/\\\\])node_modules[/\\\\]${escaped}([/\\\\]|$)`);
1853
+ const examplesFile = /\.examples\.tsx?$/;
1854
+ return (tool, input) => {
1855
+ const file = typeof input["file_path"] === "string" ? input["file_path"] : "";
1856
+ const dir = typeof input["path"] === "string" ? input["path"] : "";
1857
+ const pattern = typeof input["pattern"] === "string" ? input["pattern"] : "";
1858
+ return tool === "Read" ? packagePath.test(file) || examplesFile.test(file) : packagePath.test(dir) || packagePattern.test(pattern) || /\.examples\.tsx?/.test(pattern);
1859
+ };
1860
+ }
1861
+ var GATE_TEXT = gateText(EAST_GUIDANCE);
1862
+ var READ_TEXT = readText(EAST_GUIDANCE);
1863
+ var isExampleCorpusRead = corpusReadFor(EAST_GUIDANCE.scope);
1864
+
1865
+ // ../east-plugin/dist/lib/search.js
1866
+ async function buildSearchIndex(indexPath) {
1867
+ return (await loadIndex(indexPath)).search;
1868
+ }
1869
+ async function loadIndex(indexPath) {
1870
+ return loadIndexes([indexPath]);
1871
+ }
1872
+ async function loadIndexes(indexPaths) {
1873
+ const entries = [];
1874
+ const fileOf = /* @__PURE__ */ new Map();
1875
+ for (const indexPath of indexPaths) {
1876
+ const data2 = JSON.parse(await readFile(indexPath, "utf-8"));
1877
+ for (const entry of data2.entries) {
1878
+ const first = fileOf.get(entry.id);
1879
+ if (first !== void 0)
1880
+ throw new Error(`example id "${entry.id}" is in both ${first} and ${indexPath}`);
1881
+ fileOf.set(entry.id, indexPath);
1882
+ entries.push(entry);
1883
+ }
1884
+ }
1885
+ const data = { entries };
1886
+ const miniSearch = new MiniSearch({
1887
+ idField: "id",
1888
+ fields: ["keywordsText", "test", "builtinsText", "suite", "typesText", "code"],
1889
+ storeFields: ["id", "skill", "package", "suite", "test", "keywords", "imports", "languages", "source", "ts", "python", "signature", "inputs", "returns", "builtins"],
1890
+ searchOptions: {
1891
+ boost: { keywordsText: 3, test: 2, builtinsText: 2, suite: 1.5, typesText: 1, code: 1 },
1892
+ fuzzy: 0.2,
1893
+ prefix: true
1894
+ }
1895
+ });
1896
+ const documents = data.entries.map((entry) => ({
1897
+ ...entry,
1898
+ keywordsText: entry.keywords.join(" "),
1899
+ builtinsText: (entry.builtins ?? []).join(" "),
1900
+ typesText: entry.signature ? [...entry.signature.inputs, entry.signature.output].join(" ") : "",
1901
+ code: entry.source ?? entry.ts ?? ""
1902
+ }));
1903
+ miniSearch.addAll(documents);
1904
+ return { search: miniSearch, packages: [...new Set(data.entries.map((e) => e.package))].sort() };
1905
+ }
1906
+ var EAST_SCOPES = [EAST_GUIDANCE.scope];
1907
+
1908
+ // ../east-plugin/dist/lib/east-project.js
1909
+ import { readFile as readFile2 } from "node:fs/promises";
1910
+ import { join, dirname } from "node:path";
1911
+ var PACKAGE_SKILL_MAP = {
1912
+ "@elaraai/east": "east",
1913
+ "@elaraai/east-node-std": "east-node-std",
1914
+ "@elaraai/east-node-io": "east-node-io",
1915
+ "@elaraai/east-py-datascience": "east-py-datascience",
1916
+ "@elaraai/east-ui": "east-ui",
1917
+ "@elaraai/e3": "e3",
1918
+ "@elaraai/e3-ui": "e3-ui"
1919
+ };
1920
+ var PYTHON_SKILL_MAP = [
1921
+ [/elaraai-east-py-datascience(?![\w-])/, "east-py-datascience"],
1922
+ [/elaraai-east-py-std(?![\w-])/, "east-py-std"],
1923
+ [/elaraai-east-py-io(?![\w-])/, "east-py-io"],
1924
+ [/elaraai-east-py(?![\w-])/, "east-py"]
1925
+ ];
1926
+ async function findPackageJson(startDir) {
1927
+ let dir = startDir;
1928
+ while (true) {
1929
+ try {
1930
+ const content = await readFile2(join(dir, "package.json"), "utf-8");
1931
+ return JSON.parse(content);
1932
+ } catch {
1933
+ const parent = dirname(dir);
1934
+ if (parent === dir)
1935
+ return null;
1936
+ dir = parent;
1937
+ }
1938
+ }
1939
+ }
1940
+ async function findPyProject(startDir) {
1941
+ let dir = startDir;
1942
+ let nearest = null;
1943
+ while (true) {
1944
+ try {
1945
+ const text = await readFile2(join(dir, "pyproject.toml"), "utf-8");
1946
+ if (PYTHON_SKILL_MAP.some(([pattern]) => pattern.test(text)))
1947
+ return text;
1948
+ nearest ??= text;
1949
+ } catch {
1950
+ }
1951
+ const parent = dirname(dir);
1952
+ if (parent === dir)
1953
+ return nearest;
1954
+ dir = parent;
1955
+ }
1956
+ }
1957
+ function detectEastSkills(pkg) {
1958
+ if (!pkg)
1959
+ return [];
1960
+ const allDeps = {
1961
+ ...pkg.dependencies,
1962
+ ...pkg.devDependencies
1963
+ };
1964
+ const skills = [];
1965
+ for (const [packageName, skillName] of Object.entries(PACKAGE_SKILL_MAP)) {
1966
+ if (packageName in allDeps) {
1967
+ skills.push(skillName);
1968
+ }
1969
+ }
1970
+ return skills;
1971
+ }
1972
+ function detectPythonSkills(pyproject) {
1973
+ if (pyproject === null)
1974
+ return [];
1975
+ const skills = [];
1976
+ for (const [pattern, skill] of PYTHON_SKILL_MAP) {
1977
+ if (pattern.test(pyproject))
1978
+ skills.push(skill);
1979
+ }
1980
+ return skills;
1981
+ }
1982
+ async function getEastProjectInfo(cwd2) {
1983
+ const pkg = await findPackageJson(cwd2);
1984
+ const tsSkills = detectEastSkills(pkg);
1985
+ const pySkills = detectPythonSkills(await findPyProject(cwd2));
1986
+ const languages = [];
1987
+ if (tsSkills.length > 0)
1988
+ languages.push("typescript");
1989
+ if (pySkills.length > 0)
1990
+ languages.push("python");
1991
+ const skills = [...tsSkills, ...pySkills.filter((s) => !tsSkills.includes(s))];
1992
+ return { isEast: skills.length > 0, skills, languages, pkg };
1993
+ }
1994
+
1995
+ // ../east-plugin/dist/lib/plugin-status.js
1996
+ import { execFile } from "node:child_process";
1997
+
1998
+ // ../east-diagnostics/dist/src/python-lint.js
1999
+ import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2000
+ import { basename, dirname as dirname2, join as join2 } from "node:path";
2001
+ function findEastPy(fromDir) {
2002
+ const override = process.env["EAST_PY_LINT"];
2003
+ if (override !== void 0 && override !== "")
2004
+ return override;
2005
+ let dir = fromDir;
2006
+ for (; ; ) {
2007
+ for (const candidate of [join2(dir, ".venv", "bin", "east-py"), join2(dir, ".venv", "Scripts", "east-py.exe")]) {
2008
+ if (existsSync(candidate))
2009
+ return candidate;
2010
+ }
2011
+ const parent = dirname2(dir);
2012
+ if (parent === dir)
2013
+ return "east-py";
2014
+ dir = parent;
2015
+ }
2016
+ }
2017
+
2018
+ // ../east-plugin/dist/lib/plugin-status.js
2019
+ var ICON = { ok: "\u2713", warn: "\u26A0", fail: "\u2717" };
2020
+ function check(name, fn) {
2021
+ return Promise.resolve().then(fn).catch((e) => ({ name, status: "fail", detail: `check errored: ${String(e).slice(0, 100)}` }));
2022
+ }
2023
+ function resolves(fromDir, spec) {
2024
+ try {
2025
+ return createRequire(resolve(fromDir, "_.js")).resolve(spec);
2026
+ } catch {
2027
+ return void 0;
2028
+ }
2029
+ }
2030
+ function nearestTsconfig(fromDir) {
2031
+ let dir = resolve(fromDir);
2032
+ for (; ; ) {
2033
+ const candidate = join3(dir, "tsconfig.json");
2034
+ if (existsSync2(candidate))
2035
+ return candidate;
2036
+ const parent = dirname3(dir);
2037
+ if (parent === dir)
2038
+ return void 0;
2039
+ dir = parent;
2040
+ }
2041
+ }
2042
+ var BUNDLED = [
2043
+ ".build/hooks/session-start.js",
2044
+ ".build/hooks/subagent-start.js",
2045
+ ".build/hooks/pre-write.js",
2046
+ ".build/hooks/pre-read.js",
2047
+ ".build/hooks/diagnose.js",
2048
+ ".build/hooks/diagnose-bash.js",
2049
+ ".build/daemon/server.js",
2050
+ ".build/daemon/lsp.js",
2051
+ ".build/daemon/east-py-lsp.js",
2052
+ ".build/mcp/server.js"
2053
+ ];
2054
+ async function checkPluginStatus(pluginRoot2, cwd2) {
2055
+ const checks2 = [];
2056
+ checks2.push(await check("Plugin", () => {
2057
+ const pkg = JSON.parse(readFileSync2(join3(pluginRoot2, existsSync2(join3(pluginRoot2, ".codex-plugin")) ? ".codex-plugin" : ".claude-plugin", "plugin.json"), "utf8"));
2058
+ return { name: "Plugin", status: "ok", detail: `version ${pkg.version ?? "?"} (${pluginRoot2})` };
2059
+ }));
2060
+ checks2.push(await check("Bundled artifacts", () => {
2061
+ const artifacts = existsSync2(join3(pluginRoot2, ".codex-plugin")) ? [...BUNDLED, ".build/hooks/codex-tools.js"] : BUNDLED;
2062
+ const missing = artifacts.filter((a) => !existsSync2(join3(pluginRoot2, a)));
2063
+ return missing.length === 0 ? { name: "Bundled artifacts", status: "ok", detail: `all ${artifacts.length} hook/daemon/MCP bundles present` } : { name: "Bundled artifacts", status: "fail", detail: `missing ${missing.length}: ${missing.join(", ")}` };
2064
+ }));
2065
+ checks2.push(await check("Hook configuration", () => {
2066
+ const json = JSON.parse(readFileSync2(join3(pluginRoot2, "hooks", "hooks.json"), "utf8"));
2067
+ const events = Object.keys(json.hooks ?? {});
2068
+ const diagnoseWired = /diagnose\.js|codex-tools\.js/.test(JSON.stringify(json.hooks ?? {}));
2069
+ return {
2070
+ name: "Hook configuration",
2071
+ status: diagnoseWired ? "ok" : "warn",
2072
+ detail: diagnoseWired ? `${events.length} events: ${events.join(", ")}` : `${events.join(", ")} \u2014 PostToolUse is NOT wired to diagnose.js (stale install?)`
2073
+ };
2074
+ }));
2075
+ checks2.push(await check("Example search", async () => {
2076
+ const indexPath = join3(pluginRoot2, "index.json");
2077
+ const data = JSON.parse(readFileSync2(indexPath, "utf8"));
2078
+ const entries = data.entries ?? [];
2079
+ const programs = entries.filter((e) => e.ir !== void 0);
2080
+ const python = programs.filter((e) => typeof e.python === "string").length;
2081
+ const index = await buildSearchIndex(indexPath);
2082
+ const hits = index.search("array map", { limit: 3 });
2083
+ const ok = entries.length > 0 && hits.length > 0 && programs.length > 0 && python === programs.length;
2084
+ return {
2085
+ name: "Example search (index + MCP)",
2086
+ status: ok ? "ok" : "warn",
2087
+ detail: `${entries.length} examples indexed, ${programs.length} as IR (${python} with a python rendering); sample query \u2192 ${hits.length} hits`
2088
+ };
2089
+ }));
2090
+ checks2.push(await check("Skills", () => {
2091
+ const dirs = readdirSync(join3(pluginRoot2, "skills"), { withFileTypes: true }).filter((d) => d.isDirectory() && existsSync2(join3(pluginRoot2, "skills", d.name, "SKILL.md"))).map((d) => d.name);
2092
+ return { name: "Skills", status: dirs.length > 0 ? "ok" : "warn", detail: `${dirs.length}: ${dirs.join(", ")}` };
2093
+ }));
2094
+ checks2.push(await check("East project (cwd)", async () => {
2095
+ const { isEast, skills } = await getEastProjectInfo(cwd2);
2096
+ return {
2097
+ name: "East project (cwd)",
2098
+ status: isEast ? "ok" : "warn",
2099
+ detail: isEast ? `detected: ${skills.join(", ")}` : `${cwd2} is not an East project \u2014 hooks stay idle here (expected outside East projects)`
2100
+ };
2101
+ }));
2102
+ checks2.push(await check("Diagnostics (python / east-py)", async () => {
2103
+ const command = findEastPy(cwd2);
2104
+ const run = (args) => new Promise((done) => {
2105
+ execFile(command, args, { timeout: 8e3, encoding: "utf-8" }, (error, stdout, stderr) => {
2106
+ done({ ok: error === null, stdout: String(stdout), stderr: String(stderr) });
2107
+ });
2108
+ });
2109
+ const listed = await run(["lint", "--list-rules"]);
2110
+ if (!listed.ok) {
2111
+ return {
2112
+ name: "Diagnostics (python / east-py)",
2113
+ status: "warn",
2114
+ detail: `\`${command}\` did not answer \u2014 python East files get NO review until east-py resolves (a project .venv above the file, east-py on PATH, or EAST_PY_LINT)`
2115
+ };
2116
+ }
2117
+ const rules = listed.stdout.split("\n").filter((l) => l.startsWith("EAS")).length;
2118
+ const probe = await run(["lsp", "--probe"]);
2119
+ return probe.ok ? { name: "Diagnostics (python / east-py)", status: "ok", detail: `${command} \u2014 ${rules} rules; ${probe.stdout.trim()} (warm server + build tier)` } : {
2120
+ name: "Diagnostics (python / east-py)",
2121
+ status: "warn",
2122
+ detail: `${command} \u2014 ${rules} rules, but \`east-py lsp\` cannot start (${probe.stderr.trim().split("\n")[0] ?? "no reason given"}): the rules still run per change, the build tier is off`
2123
+ };
2124
+ }));
2125
+ checks2.push(await check("Diagnostics (PostToolUse daemon)", () => {
2126
+ const parts = [];
2127
+ let status = "ok";
2128
+ const tsOk = resolves(cwd2, "typescript") !== void 0;
2129
+ parts.push(tsOk ? "typescript resolvable" : "typescript NOT resolvable");
2130
+ if (!tsOk)
2131
+ status = "warn";
2132
+ const tsconfig = nearestTsconfig(cwd2) !== void 0;
2133
+ parts.push(tsconfig ? "tsconfig found" : "no tsconfig near cwd");
2134
+ if (!tsconfig)
2135
+ status = "warn";
2136
+ const eastOk = resolves(cwd2, "@elaraai/east") !== void 0;
2137
+ parts.push(eastOk ? "@elaraai/east resolvable (built)" : "@elaraai/east not resolvable/built");
2138
+ const daemonOk = existsSync2(join3(pluginRoot2, ".build/daemon/server.js"));
2139
+ if (!daemonOk) {
2140
+ parts.push("daemon bundle MISSING");
2141
+ status = "fail";
2142
+ }
2143
+ return {
2144
+ name: "Diagnostics (PostToolUse daemon)",
2145
+ status,
2146
+ detail: `${parts.join("; ")} \u2014 ${status === "ok" ? "ready" : status === "fail" ? "broken" : "limited (prereqs missing)"}`
2147
+ };
2148
+ }));
2149
+ return checks2;
2150
+ }
2151
+ function formatStatus(checks2) {
2152
+ const anyFail = checks2.some((c) => c.status === "fail");
2153
+ const anyWarn = checks2.some((c) => c.status === "warn");
2154
+ const header = anyFail ? "East plugin status \u2014 ISSUES" : anyWarn ? "East plugin status \u2014 OK (with notes)" : "East plugin status \u2014 OK";
2155
+ const lines = checks2.map((c) => `- ${ICON[c.status]} **${c.name}**: ${c.detail}`);
2156
+ return [`## ${header}`, "", ...lines].join("\n");
2157
+ }
2158
+
2159
+ // ../east-plugin/dist/status/report.js
2160
+ var pluginRoot = resolve2(dirname4(fileURLToPath(import.meta.url)), "..", "..");
2161
+ var cwd = resolve2(process.argv[2] ?? process.cwd());
2162
+ var checks = await checkPluginStatus(pluginRoot, cwd);
2163
+ console.log(formatStatus(checks));