handyman-harness 3.0.0

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