@hawkeyexl/manni 4.1.0 → 4.2.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.
@@ -0,0 +1,3067 @@
1
+ // src/graph/core/sort.ts
2
+ function byCodeUnit(a, b) {
3
+ return a < b ? -1 : a > b ? 1 : 0;
4
+ }
5
+
6
+ // src/graph/core/vocab.ts
7
+ var NS = {
8
+ dcterms: "http://purl.org/dc/terms/",
9
+ foaf: "http://xmlns.com/foaf/0.1/",
10
+ graph: "https://hawkeyexl.github.io/manni/graph/ns#",
11
+ iirds: "http://iirds.tekom.de/iirds#",
12
+ iirdsSft: "http://iirds.tekom.de/iirds/domain/software#",
13
+ prov: "http://www.w3.org/ns/prov#",
14
+ rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
15
+ schema: "https://schema.org/",
16
+ skos: "http://www.w3.org/2004/02/skos/core#",
17
+ xsd: "http://www.w3.org/2001/XMLSchema#"
18
+ };
19
+ var PREFIXES = Object.entries(NS).sort(([a], [b]) => a < b ? -1 : 1);
20
+ var RDF_TYPE = `${NS.rdf}type`;
21
+ var ROLE = {
22
+ author: `${NS.graph}authorRole`,
23
+ generator: `${NS.graph}generatorRole`,
24
+ tool: `${NS.graph}toolRole`
25
+ };
26
+
27
+ // src/graph/runtime/graph.ts
28
+ var XSD_STRING = "http://www.w3.org/2001/XMLSchema#string";
29
+ function compareValues(a, b) {
30
+ if (a.kind !== b.kind) return a.kind === "iri" ? -1 : 1;
31
+ const v = byCodeUnit(a.value, b.value);
32
+ if (v !== 0) return v;
33
+ const da = a.kind === "literal" ? a.datatype ?? "" : "";
34
+ const db = b.kind === "literal" ? b.datatype ?? "" : "";
35
+ return byCodeUnit(da, db);
36
+ }
37
+ var GraphIndex = class _GraphIndex {
38
+ nodes = /* @__PURE__ */ new Map();
39
+ /** subject → predicate → object IRIs (sorted). */
40
+ outbound = /* @__PURE__ */ new Map();
41
+ /** object → predicate → subject IRIs (sorted). */
42
+ inbound = /* @__PURE__ */ new Map();
43
+ /** class IRI → instance IRIs (sorted). */
44
+ byType = /* @__PURE__ */ new Map();
45
+ /** prefix → namespace, from the artifact's own `@context`. */
46
+ context;
47
+ constructor(context) {
48
+ this.context = context;
49
+ }
50
+ /**
51
+ * Build from a `graph.jsonld` document — a JSON string or the parsed object.
52
+ * CURIE keys and `@type` values are expanded using the document's own
53
+ * `@context`, so the index is self-describing.
54
+ */
55
+ static fromJsonLd(input) {
56
+ const doc = typeof input === "string" ? JSON.parse(input) : input;
57
+ const context = doc["@context"] ?? {};
58
+ const index = new _GraphIndex({ ...context });
59
+ for (const raw of doc["@graph"] ?? []) {
60
+ index.addJsonLdNode(raw);
61
+ }
62
+ index.finalize();
63
+ return index;
64
+ }
65
+ /** Build from dockg's internal quad shape (the Node/CLI path). */
66
+ static fromQuads(quads, context = {}) {
67
+ const index = new _GraphIndex({ ...context });
68
+ for (const q of quads) {
69
+ const o = q.o;
70
+ const value = o.kind === "iri" ? { kind: "iri", value: o.value } : o.datatype && o.datatype !== XSD_STRING ? { kind: "literal", value: o.value, datatype: o.datatype } : { kind: "literal", value: o.value };
71
+ index.addTriple(q.s, q.p, value);
72
+ }
73
+ index.finalize();
74
+ return index;
75
+ }
76
+ /** Expand a CURIE (`dcterms:title`) to a full IRI using `@context`. */
77
+ expand(term) {
78
+ const colon = term.indexOf(":");
79
+ if (colon <= 0) return term;
80
+ const ns = this.context[term.slice(0, colon)];
81
+ return ns ? `${ns}${term.slice(colon + 1)}` : term;
82
+ }
83
+ /** Compact a full IRI to a CURIE when a context namespace matches. */
84
+ compact(iri) {
85
+ let best = iri;
86
+ let bestNs = "";
87
+ for (const [prefix, ns] of Object.entries(this.context)) {
88
+ if (iri.startsWith(ns) && iri.length > ns.length && ns.length > bestNs.length) {
89
+ best = `${prefix}:${iri.slice(ns.length)}`;
90
+ bestNs = ns;
91
+ }
92
+ }
93
+ return best;
94
+ }
95
+ addJsonLdNode(raw) {
96
+ const id = typeof raw["@id"] === "string" ? this.expand(raw["@id"]) : "";
97
+ if (!id) return;
98
+ this.ensureNode(id);
99
+ const rawTypes = raw["@type"];
100
+ for (const t of toArray(rawTypes)) {
101
+ if (typeof t === "string") {
102
+ this.addTriple(id, RDF_TYPE, { kind: "iri", value: this.expand(t) });
103
+ }
104
+ }
105
+ for (const [key, rawValue] of Object.entries(raw)) {
106
+ if (key === "@id" || key === "@type") continue;
107
+ const predicate = this.expand(key);
108
+ for (const v of toArray(rawValue)) {
109
+ const value = this.toValue(v);
110
+ if (value) this.addTriple(id, predicate, value);
111
+ }
112
+ }
113
+ }
114
+ toValue(v) {
115
+ if (typeof v === "string") return { kind: "literal", value: v };
116
+ if (typeof v === "number" || typeof v === "boolean") {
117
+ return { kind: "literal", value: String(v) };
118
+ }
119
+ if (v && typeof v === "object") {
120
+ const o = v;
121
+ if (typeof o["@id"] === "string") {
122
+ return { kind: "iri", value: this.expand(o["@id"]) };
123
+ }
124
+ if (o["@value"] !== void 0) {
125
+ const raw = o["@value"];
126
+ const lexical = typeof raw === "string" ? raw : JSON.stringify(raw);
127
+ const dt = typeof o["@type"] === "string" ? this.expand(o["@type"]) : void 0;
128
+ return dt && dt !== XSD_STRING ? { kind: "literal", value: lexical, datatype: dt } : { kind: "literal", value: lexical };
129
+ }
130
+ }
131
+ return void 0;
132
+ }
133
+ ensureNode(id) {
134
+ let node = this.nodes.get(id);
135
+ if (!node) {
136
+ node = { id, types: [], props: /* @__PURE__ */ new Map() };
137
+ this.nodes.set(id, node);
138
+ }
139
+ return node;
140
+ }
141
+ addTriple(s, p, o) {
142
+ const node = this.ensureNode(s);
143
+ let values = node.props.get(p);
144
+ if (!values) {
145
+ values = [];
146
+ node.props.set(p, values);
147
+ }
148
+ values.push(o);
149
+ if (p === RDF_TYPE && o.kind === "iri") {
150
+ node.types.push(o.value);
151
+ const instances = this.byType.get(o.value);
152
+ if (instances) instances.push(s);
153
+ else this.byType.set(o.value, [s]);
154
+ }
155
+ if (o.kind === "iri") {
156
+ addEdge(this.outbound, s, p, o.value);
157
+ addEdge(this.inbound, o.value, p, s);
158
+ this.ensureNode(o.value);
159
+ }
160
+ }
161
+ /** Sort every collection so traversal order is deterministic. */
162
+ finalize() {
163
+ for (const node of this.nodes.values()) {
164
+ node.types.sort(byCodeUnit);
165
+ for (const values of node.props.values()) values.sort(compareValues);
166
+ }
167
+ for (const instances of this.byType.values()) instances.sort(byCodeUnit);
168
+ for (const map of [this.outbound, this.inbound]) {
169
+ for (const preds of map.values()) {
170
+ for (const targets of preds.values()) targets.sort(byCodeUnit);
171
+ }
172
+ }
173
+ }
174
+ /** Every node IRI, sorted. */
175
+ ids() {
176
+ return [...this.nodes.keys()].sort(byCodeUnit);
177
+ }
178
+ size() {
179
+ return this.nodes.size;
180
+ }
181
+ node(iri) {
182
+ return this.nodes.get(iri);
183
+ }
184
+ has(iri) {
185
+ return this.nodes.has(iri);
186
+ }
187
+ /** Class IRIs of a node, sorted. */
188
+ types(iri) {
189
+ return this.nodes.get(iri)?.types ?? [];
190
+ }
191
+ /** Instances of a class, sorted. */
192
+ instancesOf(classIri) {
193
+ return this.byType.get(classIri) ?? [];
194
+ }
195
+ /** All values for a predicate, sorted. */
196
+ values(iri, predicate) {
197
+ return this.nodes.get(iri)?.props.get(predicate) ?? [];
198
+ }
199
+ /** Literal lexical forms for a predicate, sorted. */
200
+ literals(iri, predicate) {
201
+ return this.values(iri, predicate).filter((v) => v.kind === "literal").map((v) => v.value);
202
+ }
203
+ /** The first literal for a predicate, if any. */
204
+ literal(iri, predicate) {
205
+ return this.literals(iri, predicate)[0];
206
+ }
207
+ /** Outbound IRI targets: all predicates, or one. Sorted. */
208
+ out(iri, predicate) {
209
+ return edges(this.outbound, iri, predicate);
210
+ }
211
+ /** Inbound IRI sources (who points at this node). Sorted. */
212
+ in(iri, predicate) {
213
+ return edges(this.inbound, iri, predicate);
214
+ }
215
+ };
216
+ function addEdge(map, key, predicate, target) {
217
+ let preds = map.get(key);
218
+ if (!preds) {
219
+ preds = /* @__PURE__ */ new Map();
220
+ map.set(key, preds);
221
+ }
222
+ const targets = preds.get(predicate);
223
+ if (targets) targets.push(target);
224
+ else preds.set(predicate, [target]);
225
+ }
226
+ function edges(map, iri, predicate) {
227
+ const preds = map.get(iri);
228
+ if (!preds) return [];
229
+ const out = [];
230
+ const keys = predicate ? [predicate] : [...preds.keys()].sort(byCodeUnit);
231
+ for (const p of keys) {
232
+ for (const target of preds.get(p) ?? []) out.push({ predicate: p, target });
233
+ }
234
+ return out;
235
+ }
236
+ function toArray(v) {
237
+ if (v === void 0 || v === null) return [];
238
+ return Array.isArray(v) ? v : [v];
239
+ }
240
+
241
+ // src/graph/runtime/trace.ts
242
+ function createTrace() {
243
+ return { entry: [], hops: [], exclusions: [], resolutions: [] };
244
+ }
245
+ function reachedNodes(trace) {
246
+ const reached = /* @__PURE__ */ new Set();
247
+ for (const e of trace.entry) reached.add(e.iri);
248
+ for (const h of trace.hops) reached.add(h.to);
249
+ return reached;
250
+ }
251
+
252
+ // src/graph/core/localizations.ts
253
+ var LOCALIZATIONS_FILENAME = "localizations.json";
254
+ var UNDETERMINED = "und";
255
+ var LANGUAGE_TAG = /^[A-Za-z]{2,3}(-[A-Za-z]{4})?(-([A-Za-z]{2}|[0-9]{3}))?(-([A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*$/;
256
+ function isLanguageTag(value) {
257
+ return LANGUAGE_TAG.test(value);
258
+ }
259
+ function canonicalLanguageTag(tag) {
260
+ try {
261
+ return Intl.getCanonicalLocales(tag)[0] ?? tag;
262
+ } catch {
263
+ return tag;
264
+ }
265
+ }
266
+ function searchIndexFilename(language) {
267
+ return `search.${language}.json`;
268
+ }
269
+ function vectorIndexFilename(language) {
270
+ return `vectors.${language}.bin`;
271
+ }
272
+ function emitLocalizations(doc) {
273
+ const languages = [...doc.languages].sort(
274
+ (a, b) => byCodeUnit(a.language, b.language)
275
+ );
276
+ return `${JSON.stringify({ version: doc.version, languages }, null, 2)}
277
+ `;
278
+ }
279
+ function isRecord(v) {
280
+ return typeof v === "object" && v !== null && !Array.isArray(v);
281
+ }
282
+ function parseLocalizations(text) {
283
+ let parsed;
284
+ try {
285
+ parsed = JSON.parse(text);
286
+ } catch {
287
+ return void 0;
288
+ }
289
+ if (!isRecord(parsed)) return void 0;
290
+ if (parsed["version"] !== 1 || !Array.isArray(parsed["languages"])) {
291
+ return void 0;
292
+ }
293
+ for (const entry of parsed["languages"]) {
294
+ if (!isRecord(entry)) return void 0;
295
+ const language = entry["language"];
296
+ const search = entry["search"];
297
+ if (typeof language !== "string" || !isLanguageTag(language) || typeof entry["documents"] !== "number" || !isRecord(search) || typeof search["path"] !== "string" || typeof search["entries"] !== "number" || typeof search["digest"] !== "string" || !ownsFilename(search["path"], searchIndexFilename(language))) {
298
+ return void 0;
299
+ }
300
+ const v = entry["vectors"];
301
+ if (v !== void 0) {
302
+ if (!isRecord(v) || typeof v["path"] !== "string" || typeof v["model"] !== "string" || typeof v["dtype"] !== "string" || typeof v["dims"] !== "number" || typeof v["count"] !== "number" || // Same constraint `search.path` gets: the last segment must be the
303
+ // filename this language's sidecar would be given. Without it,
304
+ // `isSafeRelativePath` alone leaves `vectors.path` far more reachable
305
+ // than `search.path` — and `search --mode vector` opens it.
306
+ !ownsFilename(v["path"], vectorIndexFilename(language))) {
307
+ return void 0;
308
+ }
309
+ }
310
+ }
311
+ return parsed;
312
+ }
313
+ function isSafeRelativePath(path) {
314
+ if (path === "" || path.startsWith("/") || /^[A-Za-z]:/.test(path)) {
315
+ return false;
316
+ }
317
+ if (path.includes("\\")) return false;
318
+ const segments = path.split("/");
319
+ let leading = true;
320
+ for (const segment of segments) {
321
+ if (segment === "" || segment === ".") return false;
322
+ if (segment === "..") {
323
+ if (!leading) return false;
324
+ continue;
325
+ }
326
+ leading = false;
327
+ }
328
+ return true;
329
+ }
330
+ function ownsFilename(path, expected) {
331
+ if (!isSafeRelativePath(path)) return false;
332
+ return path.slice(path.lastIndexOf("/") + 1) === expected;
333
+ }
334
+
335
+ // src/graph/core/iirds.ts
336
+ var IIRDS_HAS_TOPIC_TYPE = `${NS.iirds}has-topic-type`;
337
+ var IIRDS_RELATES_TO_PRODUCT_VARIANT = `${NS.iirds}relates-to-product-variant`;
338
+ var IIRDS_RELATES_TO_LIFECYCLE_PHASE = `${NS.iirds}relates-to-product-lifecycle-phase`;
339
+ var IIRDS_HAS_SUBJECT = `${NS.iirds}has-subject`;
340
+ var IIRDS_PRODUCT_VARIANT = `${NS.iirds}ProductVariant`;
341
+ var IIRDS_PACKAGE = `${NS.iirds}Package`;
342
+ var IIRDS_IIRDS_VERSION = `${NS.iirds}iiRDSVersion`;
343
+ var IIRDS_TOPIC = `${NS.iirds}Topic`;
344
+ var IIRDS_IS_PART_OF_PACKAGE = `${NS.iirds}is-part-of-package`;
345
+ var IIRDS_HAS_RENDITION = `${NS.iirds}has-rendition`;
346
+ var IIRDS_RENDITION = `${NS.iirds}Rendition`;
347
+ var IIRDS_SOURCE = `${NS.iirds}source`;
348
+ var IIRDS_FORMAT = `${NS.iirds}format`;
349
+ var IIRDS_TITLE = `${NS.iirds}title`;
350
+ var IIRDS_LANGUAGE = `${NS.iirds}language`;
351
+ var IIRDS_PARTY = `${NS.iirds}Party`;
352
+ var IIRDS_HAS_PARTY_ROLE = `${NS.iirds}has-party-role`;
353
+ var IIRDS_CREATOR = `${NS.iirds}Creator`;
354
+ var IIRDS_RELATES_TO_PARTY = `${NS.iirds}relates-to-party`;
355
+ var IIRDS_RELATES_TO_VCARD = `${NS.iirds}relates-to-vcard`;
356
+ var VCARD_NS = "http://www.w3.org/2006/vcard/ns#";
357
+ var VCARD_ORGANIZATION = `${VCARD_NS}Organization`;
358
+ var VCARD_ORGANIZATION_NAME = `${VCARD_NS}organization-name`;
359
+ var GRAPH_NOT_APPLICABLE_TO_VARIANT = `${NS.graph}notApplicableToVariant`;
360
+ var GRAPH_NOT_SOFTWARE_SUBJECT = `${NS.graph}notSoftwareSubject`;
361
+ var TOPIC_TYPE_IRIS = {
362
+ task: `${NS.iirds}GenericTask`,
363
+ concept: `${NS.iirds}GenericConcept`,
364
+ reference: `${NS.iirds}GenericReference`,
365
+ learning: `${NS.iirds}GenericLearning`,
366
+ troubleshooting: `${NS.iirds}GenericTroubleshooting`,
367
+ form: `${NS.iirds}GenericForm`
368
+ };
369
+ var SOFTWARE_LIFECYCLE_IRIS = {
370
+ administration: `${NS.iirdsSft}Administration`,
371
+ customization: `${NS.iirdsSft}Customization`,
372
+ update: `${NS.iirdsSft}Update`,
373
+ deployment: `${NS.iirdsSft}Deployment`,
374
+ integration: `${NS.iirdsSft}Integration`,
375
+ deinstallation: `${NS.iirdsSft}Deinstallation`
376
+ };
377
+ var SOFTWARE_SUBJECT_IRIS = {
378
+ architecture: `${NS.iirdsSft}Architecture`,
379
+ interface: `${NS.iirdsSft}Interface`,
380
+ "system-requirement": `${NS.iirdsSft}SystemRequirement`
381
+ };
382
+
383
+ // src/graph/runtime/traverse.ts
384
+ var DCTERMS_TITLE = `${NS.dcterms}title`;
385
+ var DCTERMS_REFERENCES = `${NS.dcterms}references`;
386
+ var DCTERMS_LANGUAGE = `${NS.dcterms}language`;
387
+ function documentOf(iri) {
388
+ const hash = iri.indexOf("#");
389
+ return hash === -1 ? iri : iri.slice(0, hash);
390
+ }
391
+ function resolveVariant(graph, input) {
392
+ if (graph.has(input)) return input;
393
+ const wanted = input.toLowerCase();
394
+ const variants = graph.instancesOf(`${NS.iirds}ProductVariant`);
395
+ for (const v of variants) {
396
+ if (graph.literal(v, DCTERMS_TITLE)?.toLowerCase() === wanted) return v;
397
+ }
398
+ for (const v of variants) {
399
+ if (v.slice(v.lastIndexOf("/") + 1).toLowerCase() === wanted) return v;
400
+ }
401
+ return void 0;
402
+ }
403
+ function resolveSubject(graph, input) {
404
+ const mapped = SOFTWARE_SUBJECT_IRIS[input.toLowerCase()];
405
+ if (mapped) return mapped;
406
+ const expanded = graph.expand(input);
407
+ return expanded.startsWith("http") ? expanded : void 0;
408
+ }
409
+ function scopeExclusion(graph, iri, scope) {
410
+ const checks = [
411
+ [
412
+ scope.variantIri,
413
+ IIRDS_RELATES_TO_PRODUCT_VARIANT,
414
+ GRAPH_NOT_APPLICABLE_TO_VARIANT,
415
+ "iri"
416
+ ],
417
+ [scope.subjectIri, IIRDS_HAS_SUBJECT, GRAPH_NOT_SOFTWARE_SUBJECT, "iri"],
418
+ [scope.language, DCTERMS_LANGUAGE, void 0, "literal"]
419
+ ];
420
+ for (const [target, positive, negative, kind] of checks) {
421
+ if (!target) continue;
422
+ if (negative !== void 0) {
423
+ const denied = graph.values(iri, negative).some((v) => v.kind === kind && v.value === target);
424
+ if (denied) return { node: iri, rule: negative, value: target };
425
+ }
426
+ const subject = positive === DCTERMS_LANGUAGE ? documentOf(iri) : iri;
427
+ const claimed = graph.values(subject, positive);
428
+ if (claimed.length > 0) {
429
+ const matches = claimed.some(
430
+ (v) => v.kind === kind && v.value === target
431
+ );
432
+ if (!matches) return { node: iri, rule: positive, value: target };
433
+ }
434
+ }
435
+ return void 0;
436
+ }
437
+ function traverse(graph, options) {
438
+ const trace = options.trace ?? createTrace();
439
+ const maxDepth = options.depth ?? 1;
440
+ const direction = options.direction ?? "out";
441
+ const allowed = options.predicates && options.predicates.length > 0 ? new Set(options.predicates.map((p) => graph.expand(p))) : void 0;
442
+ const variantIri = options.variant ? resolveVariant(graph, options.variant) : void 0;
443
+ const subjectIri = options.subject ? resolveSubject(graph, options.subject) : void 0;
444
+ const language = options.language === void 0 ? void 0 : canonicalLanguageTag(options.language);
445
+ const scope = { variantIri, subjectIri, language };
446
+ const followTypes = options.includeTypeEdges === true || allowed?.has(RDF_TYPE) === true;
447
+ const excluded = /* @__PURE__ */ new Set();
448
+ const keep = (iri) => {
449
+ if (excluded.has(iri)) return false;
450
+ const exclusion = scopeExclusion(graph, iri, scope);
451
+ if (exclusion) {
452
+ excluded.add(iri);
453
+ trace.exclusions.push(exclusion);
454
+ return false;
455
+ }
456
+ return true;
457
+ };
458
+ const nodes = [];
459
+ const seen = /* @__PURE__ */ new Set();
460
+ let frontier = [];
461
+ const alreadySeeded = new Set(trace.entry.map((e) => e.iri));
462
+ for (const seed of [...options.seeds].sort(byCodeUnit)) {
463
+ if (!graph.has(seed) || seen.has(seed)) continue;
464
+ seen.add(seed);
465
+ if (!alreadySeeded.has(seed)) {
466
+ trace.entry.push({ iri: seed, score: 1, via: "explicit" });
467
+ }
468
+ if (keep(seed)) nodes.push({ iri: seed, depth: 0 });
469
+ frontier.push(seed);
470
+ }
471
+ const limit = options.limit ?? Infinity;
472
+ for (let depth = 1; depth <= maxDepth && frontier.length > 0; depth++) {
473
+ const next = [];
474
+ for (const from of frontier) {
475
+ if (nodes.length >= limit) break;
476
+ for (const { predicate, target, dir } of neighbors(
477
+ graph,
478
+ from,
479
+ direction
480
+ )) {
481
+ if (allowed && !allowed.has(predicate)) continue;
482
+ if (!followTypes && predicate === RDF_TYPE) continue;
483
+ trace.hops.push({ from, predicate, to: target, depth, direction: dir });
484
+ if (seen.has(target)) continue;
485
+ seen.add(target);
486
+ if (!keep(target)) continue;
487
+ if (nodes.length >= limit) break;
488
+ nodes.push({ iri: target, depth });
489
+ next.push(target);
490
+ }
491
+ }
492
+ frontier = next;
493
+ }
494
+ return { nodes, trace };
495
+ }
496
+ function neighbors(graph, iri, direction) {
497
+ const out = [];
498
+ if (direction === "out" || direction === "both") {
499
+ for (const e of graph.out(iri)) out.push({ ...e, dir: "out" });
500
+ }
501
+ if (direction === "in" || direction === "both") {
502
+ for (const e of graph.in(iri)) out.push({ ...e, dir: "in" });
503
+ }
504
+ return out;
505
+ }
506
+ function reverseReferences(graph, iri, options = {}) {
507
+ return traverse(graph, {
508
+ ...options,
509
+ seeds: [iri],
510
+ direction: "in",
511
+ predicates: [DCTERMS_REFERENCES],
512
+ depth: options.depth ?? 1
513
+ });
514
+ }
515
+ function impact(graph, iri, options = {}) {
516
+ const result = traverse(graph, {
517
+ ...options,
518
+ seeds: [iri],
519
+ direction: "in",
520
+ depth: options.depth ?? 3,
521
+ // The seed *may* occupy a slot in the walker's limit, so ask for one more:
522
+ // `limit` means "this many *affected* nodes". Whether it actually takes
523
+ // one depends on the scope filter — a seed the filter excludes still
524
+ // expands but is not in `nodes` — so the slack is trimmed below rather
525
+ // than assumed spent.
526
+ limit: options.limit === void 0 ? void 0 : options.limit + 1
527
+ });
528
+ const affected = result.nodes.filter((n) => n.iri !== iri);
529
+ return {
530
+ nodes: options.limit === void 0 ? affected : affected.slice(0, options.limit),
531
+ trace: result.trace
532
+ };
533
+ }
534
+
535
+ // src/graph/runtime/resolve.ts
536
+ var GRAPH_PATH = `${NS.graph}path`;
537
+ var DCTERMS_TITLE2 = `${NS.dcterms}title`;
538
+ var DCTERMS_HAS_PART = `${NS.dcterms}hasPart`;
539
+ var GRAPH_LEVEL = `${NS.graph}level`;
540
+ var GRAPH_ORDER = `${NS.graph}order`;
541
+ function splitFragment(iri) {
542
+ const hash = iri.indexOf("#");
543
+ return hash < 0 ? { doc: iri } : { doc: iri.slice(0, hash), fragment: iri.slice(hash + 1) };
544
+ }
545
+ function fencedLines(lines) {
546
+ const mask = new Array(lines.length).fill(false);
547
+ let open;
548
+ for (const [i, line] of lines.entries()) {
549
+ const m = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
550
+ const delimiter = m?.[1];
551
+ const info = m?.[2];
552
+ const isFence = delimiter !== void 0 && info !== void 0;
553
+ if (open === void 0) {
554
+ if (isFence && !(delimiter.startsWith("`") && info.includes("`"))) {
555
+ open = delimiter;
556
+ mask[i] = true;
557
+ }
558
+ continue;
559
+ }
560
+ mask[i] = true;
561
+ if (isFence && delimiter[0] === open[0] && delimiter.length >= open.length && info.trim() === "") {
562
+ open = void 0;
563
+ }
564
+ }
565
+ return mask;
566
+ }
567
+ var ATX = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?[ \t]*$/;
568
+ var SETEXT_UNDERLINE = /^ {0,3}(=+|-+)[ \t]*$/;
569
+ function isParagraphLine(line) {
570
+ if (line.trim() === "") return false;
571
+ if (ATX.test(line)) return false;
572
+ if (/^ {0,3}([-+*]|\d{1,9}[.)])([ \t]|$)/.test(line)) return false;
573
+ if (/^ {0,3}>/.test(line)) return false;
574
+ if (/^ {0,3}([-*_])([ \t]*\1){2,}[ \t]*$/.test(line)) return false;
575
+ return !/^ {4}/.test(line);
576
+ }
577
+ function frontmatterLength(lines) {
578
+ if (lines[0]?.trim() !== "---") return 0;
579
+ for (let i = 1; i < lines.length; i++) {
580
+ const line = lines[i]?.trim();
581
+ if (line === "---" || line === "...") return i + 1;
582
+ }
583
+ return 0;
584
+ }
585
+ function sourceHeadings(lines) {
586
+ const fenced = fencedLines(lines);
587
+ const skip = frontmatterLength(lines);
588
+ const headings = [];
589
+ let paragraph = -1;
590
+ for (const [i, line] of lines.entries()) {
591
+ if (i < skip || fenced[i]) {
592
+ paragraph = -1;
593
+ continue;
594
+ }
595
+ const atx = ATX.exec(line);
596
+ const hashes = atx?.[1];
597
+ if (hashes !== void 0) {
598
+ const text = (atx?.[2] ?? "").replace(/(^|[ \t]+)#+$/, "");
599
+ headings.push({ start: i, level: hashes.length, text });
600
+ paragraph = -1;
601
+ continue;
602
+ }
603
+ const underline = SETEXT_UNDERLINE.exec(line)?.[1];
604
+ if (underline !== void 0 && paragraph >= 0) {
605
+ headings.push({
606
+ start: paragraph,
607
+ level: underline.startsWith("=") ? 1 : 2,
608
+ text: lines.slice(paragraph, i).join("\n")
609
+ });
610
+ paragraph = -1;
611
+ continue;
612
+ }
613
+ if (isParagraphLine(line)) {
614
+ if (paragraph < 0) paragraph = i;
615
+ } else {
616
+ paragraph = -1;
617
+ }
618
+ }
619
+ return headings;
620
+ }
621
+ var ENTITIES = {
622
+ amp: "&",
623
+ lt: "<",
624
+ gt: ">",
625
+ quot: '"',
626
+ apos: "'",
627
+ nbsp: "\xA0"
628
+ };
629
+ function decodeEntities(text) {
630
+ return text.replace(
631
+ /&(#x[0-9a-f]+|#[0-9]+|[a-z]+);/gi,
632
+ (whole, name) => {
633
+ if (name.startsWith("#")) {
634
+ const hex = name[1] === "x" || name[1] === "X";
635
+ const code = Number.parseInt(name.slice(hex ? 2 : 1), hex ? 16 : 10);
636
+ return Number.isNaN(code) || code > 1114111 ? whole : String.fromCodePoint(code);
637
+ }
638
+ return ENTITIES[name.toLowerCase()] ?? whole;
639
+ }
640
+ );
641
+ }
642
+ function headingKey(text) {
643
+ return text.replace(/[`*_~\\[\]]/g, "").replace(/\s+/g, " ").trim().toLowerCase();
644
+ }
645
+ function stripInline(raw) {
646
+ return decodeEntities(
647
+ raw.replace(/!?\[([^\]]*)\]\((?:[^()]|\([^()]*\))*\)/g, "$1").replace(/!?\[([^\]]*)\]\[[^\]]*\]/g, "$1").replace(/<((?:https?|mailto):[^>\s]+)>/gi, "$1")
648
+ );
649
+ }
650
+ function documentSectionOrder(graph, docIri) {
651
+ const ordered = [];
652
+ const seen = /* @__PURE__ */ new Set();
653
+ const childrenOf = (iri) => graph.out(iri, DCTERMS_HAS_PART).map((e) => e.target).sort((a, b) => {
654
+ const oa = Number.parseInt(graph.literal(a, GRAPH_ORDER) ?? "", 10);
655
+ const ob = Number.parseInt(graph.literal(b, GRAPH_ORDER) ?? "", 10);
656
+ if (Number.isNaN(oa) || Number.isNaN(ob) || oa === ob) {
657
+ return a < b ? -1 : a > b ? 1 : 0;
658
+ }
659
+ return oa - ob;
660
+ });
661
+ const walk = (iri) => {
662
+ for (const child of childrenOf(iri)) {
663
+ if (seen.has(child)) continue;
664
+ seen.add(child);
665
+ ordered.push(child);
666
+ walk(child);
667
+ }
668
+ };
669
+ walk(docIri);
670
+ return ordered;
671
+ }
672
+ function sectionOccurrences(graph, docIri) {
673
+ const occurrences = /* @__PURE__ */ new Map();
674
+ const counts = /* @__PURE__ */ new Map();
675
+ for (const section of documentSectionOrder(graph, docIri)) {
676
+ const rawTitle = graph.literal(section, DCTERMS_TITLE2);
677
+ if (rawTitle === void 0) {
678
+ occurrences.set(section, 0);
679
+ continue;
680
+ }
681
+ const title = headingKey(rawTitle);
682
+ const level = graph.literal(section, GRAPH_LEVEL) ?? "";
683
+ let byLevel = counts.get(title);
684
+ if (!byLevel) {
685
+ byLevel = /* @__PURE__ */ new Map();
686
+ counts.set(title, byLevel);
687
+ }
688
+ const seen = byLevel.get(level) ?? 0;
689
+ occurrences.set(section, seen);
690
+ byLevel.set(level, seen + 1);
691
+ }
692
+ return occurrences;
693
+ }
694
+ function sectionOccurrence(graph, sectionIri) {
695
+ const { doc } = splitFragment(sectionIri);
696
+ return sectionOccurrences(graph, doc).get(sectionIri) ?? 0;
697
+ }
698
+ function documentPreamble(markdown) {
699
+ const lines = markdown.split(/\r?\n/);
700
+ const end = sourceHeadings(lines)[0]?.start ?? lines.length;
701
+ const text = lines.slice(0, end).join("\n").trim();
702
+ return text === "" ? void 0 : text;
703
+ }
704
+ function sliceSection(markdown, title, level, occurrence = 0) {
705
+ return slice(markdown, title, level, occurrence, false);
706
+ }
707
+ function sectionOwnText(markdown, title, level, occurrence = 0) {
708
+ return slice(markdown, title, level, occurrence, true);
709
+ }
710
+ function slice(markdown, title, level, occurrence, ownTextOnly) {
711
+ const lines = markdown.split(/\r?\n/);
712
+ const headings = sourceHeadings(lines);
713
+ const wanted = headingKey(title);
714
+ let remaining = occurrence;
715
+ let found = -1;
716
+ for (const [n, heading] of headings.entries()) {
717
+ if (level !== void 0 && heading.level !== level) continue;
718
+ if (headingKey(stripInline(heading.text)) !== wanted) continue;
719
+ if (remaining > 0) {
720
+ remaining -= 1;
721
+ continue;
722
+ }
723
+ found = n;
724
+ break;
725
+ }
726
+ const match = headings[found];
727
+ if (match === void 0) return void 0;
728
+ const next = headings.slice(found + 1).find((h) => ownTextOnly || h.level <= match.level);
729
+ const end = next?.start ?? lines.length;
730
+ return lines.slice(match.start, end).join("\n").trimEnd();
731
+ }
732
+ function createFetchResolver(graph, options = {}) {
733
+ const doFetch = options.fetch ?? ((url) => globalThis.fetch(url));
734
+ const toUrl = options.pathToUrl ?? ((path) => `${options.baseUrl ?? ""}${path}`);
735
+ const cache = /* @__PURE__ */ new Map();
736
+ const occurrenceCache = /* @__PURE__ */ new Map();
737
+ const occurrenceOf = (docIri, sectionIri) => {
738
+ let map = occurrenceCache.get(docIri);
739
+ if (!map) {
740
+ map = sectionOccurrences(graph, docIri);
741
+ occurrenceCache.set(docIri, map);
742
+ }
743
+ return map.get(sectionIri) ?? 0;
744
+ };
745
+ const fetchDoc = (url) => {
746
+ let pending = cache.get(url);
747
+ if (!pending) {
748
+ pending = doFetch(url).then((r) => r.ok ? r.text() : void 0).catch(() => void 0);
749
+ cache.set(url, pending);
750
+ }
751
+ return pending;
752
+ };
753
+ return {
754
+ async resolve(iri) {
755
+ const { doc, fragment } = splitFragment(iri);
756
+ const path = graph.literal(doc, GRAPH_PATH);
757
+ if (!path) return void 0;
758
+ const sourceUrl = toUrl(path);
759
+ const body = await fetchDoc(sourceUrl);
760
+ const title = graph.literal(iri, DCTERMS_TITLE2);
761
+ if (body === void 0) {
762
+ options.trace?.resolutions.push({
763
+ iri,
764
+ sourceUrl,
765
+ ok: false,
766
+ error: "fetch failed"
767
+ });
768
+ return void 0;
769
+ }
770
+ let text = body;
771
+ if (fragment) {
772
+ const levelText = graph.literal(iri, GRAPH_LEVEL);
773
+ const level = levelText ? Number.parseInt(levelText, 10) : void 0;
774
+ const slice2 = title === void 0 ? void 0 : sliceSection(
775
+ body,
776
+ title,
777
+ Number.isNaN(level) ? void 0 : level,
778
+ occurrenceOf(doc, iri)
779
+ );
780
+ if (slice2 === void 0) {
781
+ options.trace?.resolutions.push({
782
+ iri,
783
+ sourceUrl,
784
+ ok: false,
785
+ error: "section heading not found"
786
+ });
787
+ return void 0;
788
+ }
789
+ text = slice2;
790
+ }
791
+ options.trace?.resolutions.push({ iri, sourceUrl, ok: true });
792
+ return { iri, text, sourceUrl, title };
793
+ }
794
+ };
795
+ }
796
+
797
+ // src/graph/runtime/assemble.ts
798
+ async function assemble(resolver, nodes, options = {}) {
799
+ const trace = options.trace ?? createTrace();
800
+ const budget = options.maxChars ?? Infinity;
801
+ if (nodes.length === 0) {
802
+ return {
803
+ ...options.entry ? { entry: options.entry } : {},
804
+ context: [],
805
+ citations: [],
806
+ trace,
807
+ truncated: false,
808
+ refusal: {
809
+ reason: "no-route",
810
+ detail: "No node in the graph was reachable for this query under the active scope filter."
811
+ }
812
+ };
813
+ }
814
+ const context = [];
815
+ let used = 0;
816
+ let truncated = false;
817
+ let resolvedAny = false;
818
+ for (const node of nodes) {
819
+ const resolved = await resolver.resolve(node.iri);
820
+ if (!resolved) continue;
821
+ resolvedAny = true;
822
+ if (used + resolved.text.length > budget) {
823
+ truncated = true;
824
+ continue;
825
+ }
826
+ used += resolved.text.length;
827
+ context.push({
828
+ iri: resolved.iri,
829
+ title: resolved.title,
830
+ text: resolved.text,
831
+ sourceUrl: resolved.sourceUrl,
832
+ depth: node.depth
833
+ });
834
+ }
835
+ if (context.length === 0) {
836
+ return {
837
+ ...options.entry ? { entry: options.entry } : {},
838
+ context: [],
839
+ citations: [],
840
+ trace,
841
+ truncated,
842
+ refusal: {
843
+ reason: "no-content",
844
+ detail: resolvedAny ? "Every matching node's content exceeded the character budget." : "Matching nodes were found, but none of them resolved to readable content."
845
+ }
846
+ };
847
+ }
848
+ return {
849
+ ...options.entry ? { entry: options.entry } : {},
850
+ context,
851
+ citations: context.map((b) => ({
852
+ iri: b.iri,
853
+ title: b.title,
854
+ sourceUrl: b.sourceUrl
855
+ })),
856
+ trace,
857
+ truncated
858
+ };
859
+ }
860
+
861
+ // src/graph/runtime/rdfjs.ts
862
+ var XSD_STRING2 = "http://www.w3.org/2001/XMLSchema#string";
863
+ function namedNode(value) {
864
+ return {
865
+ termType: "NamedNode",
866
+ value,
867
+ equals(other) {
868
+ return !!other && other.termType === "NamedNode" && other.value === value;
869
+ }
870
+ };
871
+ }
872
+ function defaultGraph() {
873
+ return {
874
+ termType: "DefaultGraph",
875
+ value: "",
876
+ equals(other) {
877
+ return !!other && other.termType === "DefaultGraph";
878
+ }
879
+ };
880
+ }
881
+ function literal(value, datatype) {
882
+ const dt = namedNode(datatype ?? XSD_STRING2);
883
+ return {
884
+ termType: "Literal",
885
+ value,
886
+ language: "",
887
+ datatype: dt,
888
+ equals(other) {
889
+ return !!other && other.termType === "Literal" && other.value === value && !!other.datatype && other.datatype.value === dt.value;
890
+ }
891
+ };
892
+ }
893
+ function toTerm(v) {
894
+ return v.kind === "iri" ? namedNode(v.value) : literal(v.value, v.datatype);
895
+ }
896
+ function makeQuad(s, p, o) {
897
+ const subject = namedNode(s);
898
+ const predicate = namedNode(p);
899
+ const object = toTerm(o);
900
+ const graph = defaultGraph();
901
+ return {
902
+ termType: "Quad",
903
+ subject,
904
+ predicate,
905
+ object,
906
+ graph,
907
+ equals(other) {
908
+ return !!other && subject.equals(other.subject) && predicate.equals(other.predicate) && object.equals(other.object);
909
+ }
910
+ };
911
+ }
912
+ function rdfjsQuads(graph) {
913
+ const quads = [];
914
+ for (const s of graph.ids()) {
915
+ const node = graph.node(s);
916
+ if (!node) continue;
917
+ for (const p of [...node.props.keys()].sort(byCodeUnit)) {
918
+ for (const v of node.props.get(p) ?? []) quads.push(makeQuad(s, p, v));
919
+ }
920
+ }
921
+ return quads;
922
+ }
923
+ function matchQuads(graph, subject, predicate, object) {
924
+ const quads = [];
925
+ const subjects = subject ? [subject] : graph.ids();
926
+ for (const s of subjects) {
927
+ const node = graph.node(s);
928
+ if (!node) continue;
929
+ const predicates = predicate ? [predicate] : [...node.props.keys()].sort(byCodeUnit);
930
+ for (const p of predicates) {
931
+ for (const v of node.props.get(p) ?? []) {
932
+ if (object !== void 0 && object !== null && v.value !== object) {
933
+ continue;
934
+ }
935
+ quads.push(makeQuad(s, p, v));
936
+ }
937
+ }
938
+ }
939
+ return quads;
940
+ }
941
+
942
+ // node_modules/minisearch/dist/es/index.js
943
+ var ENTRIES = "ENTRIES";
944
+ var KEYS = "KEYS";
945
+ var VALUES = "VALUES";
946
+ var LEAF = "";
947
+ var TreeIterator = class {
948
+ constructor(set, type) {
949
+ const node = set._tree;
950
+ const keys = Array.from(node.keys());
951
+ this.set = set;
952
+ this._type = type;
953
+ this._path = keys.length > 0 ? [{ node, keys }] : [];
954
+ }
955
+ next() {
956
+ const value = this.dive();
957
+ this.backtrack();
958
+ return value;
959
+ }
960
+ dive() {
961
+ if (this._path.length === 0) {
962
+ return { done: true, value: void 0 };
963
+ }
964
+ const { node, keys } = last$1(this._path);
965
+ if (last$1(keys) === LEAF) {
966
+ return { done: false, value: this.result() };
967
+ }
968
+ const child = node.get(last$1(keys));
969
+ this._path.push({ node: child, keys: Array.from(child.keys()) });
970
+ return this.dive();
971
+ }
972
+ backtrack() {
973
+ if (this._path.length === 0) {
974
+ return;
975
+ }
976
+ const keys = last$1(this._path).keys;
977
+ keys.pop();
978
+ if (keys.length > 0) {
979
+ return;
980
+ }
981
+ this._path.pop();
982
+ this.backtrack();
983
+ }
984
+ key() {
985
+ return this.set._prefix + this._path.map(({ keys }) => last$1(keys)).filter((key) => key !== LEAF).join("");
986
+ }
987
+ value() {
988
+ return last$1(this._path).node.get(LEAF);
989
+ }
990
+ result() {
991
+ switch (this._type) {
992
+ case VALUES:
993
+ return this.value();
994
+ case KEYS:
995
+ return this.key();
996
+ default:
997
+ return [this.key(), this.value()];
998
+ }
999
+ }
1000
+ [Symbol.iterator]() {
1001
+ return this;
1002
+ }
1003
+ };
1004
+ var last$1 = (array) => {
1005
+ return array[array.length - 1];
1006
+ };
1007
+ var fuzzySearch = (node, query, maxDistance) => {
1008
+ const results = /* @__PURE__ */ new Map();
1009
+ if (query === void 0)
1010
+ return results;
1011
+ const n = query.length + 1;
1012
+ const m = n + maxDistance;
1013
+ const matrix = new Uint8Array(m * n).fill(maxDistance + 1);
1014
+ for (let j = 0; j < n; ++j)
1015
+ matrix[j] = j;
1016
+ for (let i = 1; i < m; ++i)
1017
+ matrix[i * n] = i;
1018
+ recurse(node, query, maxDistance, results, matrix, 1, n, "");
1019
+ return results;
1020
+ };
1021
+ var recurse = (node, query, maxDistance, results, matrix, m, n, prefix) => {
1022
+ const offset = m * n;
1023
+ key: for (const key of node.keys()) {
1024
+ if (key === LEAF) {
1025
+ const distance = matrix[offset - 1];
1026
+ if (distance <= maxDistance) {
1027
+ results.set(prefix, [node.get(key), distance]);
1028
+ }
1029
+ } else {
1030
+ let i = m;
1031
+ for (let pos = 0; pos < key.length; ++pos, ++i) {
1032
+ const char = key[pos];
1033
+ const thisRowOffset = n * i;
1034
+ const prevRowOffset = thisRowOffset - n;
1035
+ let minDistance = matrix[thisRowOffset];
1036
+ const jmin = Math.max(0, i - maxDistance - 1);
1037
+ const jmax = Math.min(n - 1, i + maxDistance);
1038
+ for (let j = jmin; j < jmax; ++j) {
1039
+ const different = char !== query[j];
1040
+ const rpl = matrix[prevRowOffset + j] + +different;
1041
+ const del = matrix[prevRowOffset + j + 1] + 1;
1042
+ const ins = matrix[thisRowOffset + j] + 1;
1043
+ const dist = matrix[thisRowOffset + j + 1] = Math.min(rpl, del, ins);
1044
+ if (dist < minDistance)
1045
+ minDistance = dist;
1046
+ }
1047
+ if (minDistance > maxDistance) {
1048
+ continue key;
1049
+ }
1050
+ }
1051
+ recurse(node.get(key), query, maxDistance, results, matrix, i, n, prefix + key);
1052
+ }
1053
+ }
1054
+ };
1055
+ var SearchableMap = class _SearchableMap {
1056
+ /**
1057
+ * The constructor is normally called without arguments, creating an empty
1058
+ * map. In order to create a {@link SearchableMap} from an iterable or from an
1059
+ * object, check {@link SearchableMap.from} and {@link
1060
+ * SearchableMap.fromObject}.
1061
+ *
1062
+ * The constructor arguments are for internal use, when creating derived
1063
+ * mutable views of a map at a prefix.
1064
+ */
1065
+ constructor(tree = /* @__PURE__ */ new Map(), prefix = "") {
1066
+ this._size = void 0;
1067
+ this._tree = tree;
1068
+ this._prefix = prefix;
1069
+ }
1070
+ /**
1071
+ * Creates and returns a mutable view of this {@link SearchableMap},
1072
+ * containing only entries that share the given prefix.
1073
+ *
1074
+ * ### Usage:
1075
+ *
1076
+ * ```javascript
1077
+ * let map = new SearchableMap()
1078
+ * map.set("unicorn", 1)
1079
+ * map.set("universe", 2)
1080
+ * map.set("university", 3)
1081
+ * map.set("unique", 4)
1082
+ * map.set("hello", 5)
1083
+ *
1084
+ * let uni = map.atPrefix("uni")
1085
+ * uni.get("unique") // => 4
1086
+ * uni.get("unicorn") // => 1
1087
+ * uni.get("hello") // => undefined
1088
+ *
1089
+ * let univer = map.atPrefix("univer")
1090
+ * univer.get("unique") // => undefined
1091
+ * univer.get("universe") // => 2
1092
+ * univer.get("university") // => 3
1093
+ * ```
1094
+ *
1095
+ * @param prefix The prefix
1096
+ * @return A {@link SearchableMap} representing a mutable view of the original
1097
+ * Map at the given prefix
1098
+ */
1099
+ atPrefix(prefix) {
1100
+ if (!prefix.startsWith(this._prefix)) {
1101
+ throw new Error("Mismatched prefix");
1102
+ }
1103
+ const [node, path] = trackDown(this._tree, prefix.slice(this._prefix.length));
1104
+ if (node === void 0) {
1105
+ const [parentNode, key] = last(path);
1106
+ for (const k of parentNode.keys()) {
1107
+ if (k !== LEAF && k.startsWith(key)) {
1108
+ const node2 = /* @__PURE__ */ new Map();
1109
+ node2.set(k.slice(key.length), parentNode.get(k));
1110
+ return new _SearchableMap(node2, prefix);
1111
+ }
1112
+ }
1113
+ }
1114
+ return new _SearchableMap(node, prefix);
1115
+ }
1116
+ /**
1117
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear
1118
+ */
1119
+ clear() {
1120
+ this._size = void 0;
1121
+ this._tree.clear();
1122
+ }
1123
+ /**
1124
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete
1125
+ * @param key Key to delete
1126
+ */
1127
+ delete(key) {
1128
+ this._size = void 0;
1129
+ return remove(this._tree, key);
1130
+ }
1131
+ /**
1132
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries
1133
+ * @return An iterator iterating through `[key, value]` entries.
1134
+ */
1135
+ entries() {
1136
+ return new TreeIterator(this, ENTRIES);
1137
+ }
1138
+ /**
1139
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach
1140
+ * @param fn Iteration function
1141
+ */
1142
+ forEach(fn) {
1143
+ for (const [key, value] of this) {
1144
+ fn(key, value, this);
1145
+ }
1146
+ }
1147
+ /**
1148
+ * Returns a Map of all the entries that have a key within the given edit
1149
+ * distance from the search key. The keys of the returned Map are the matching
1150
+ * keys, while the values are two-element arrays where the first element is
1151
+ * the value associated to the key, and the second is the edit distance of the
1152
+ * key to the search key.
1153
+ *
1154
+ * ### Usage:
1155
+ *
1156
+ * ```javascript
1157
+ * let map = new SearchableMap()
1158
+ * map.set('hello', 'world')
1159
+ * map.set('hell', 'yeah')
1160
+ * map.set('ciao', 'mondo')
1161
+ *
1162
+ * // Get all entries that match the key 'hallo' with a maximum edit distance of 2
1163
+ * map.fuzzyGet('hallo', 2)
1164
+ * // => Map(2) { 'hello' => ['world', 1], 'hell' => ['yeah', 2] }
1165
+ *
1166
+ * // In the example, the "hello" key has value "world" and edit distance of 1
1167
+ * // (change "e" to "a"), the key "hell" has value "yeah" and edit distance of 2
1168
+ * // (change "e" to "a", delete "o")
1169
+ * ```
1170
+ *
1171
+ * @param key The search key
1172
+ * @param maxEditDistance The maximum edit distance (Levenshtein)
1173
+ * @return A Map of the matching keys to their value and edit distance
1174
+ */
1175
+ fuzzyGet(key, maxEditDistance) {
1176
+ return fuzzySearch(this._tree, key, maxEditDistance);
1177
+ }
1178
+ /**
1179
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get
1180
+ * @param key Key to get
1181
+ * @return Value associated to the key, or `undefined` if the key is not
1182
+ * found.
1183
+ */
1184
+ get(key) {
1185
+ const node = lookup(this._tree, key);
1186
+ return node !== void 0 ? node.get(LEAF) : void 0;
1187
+ }
1188
+ /**
1189
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has
1190
+ * @param key Key
1191
+ * @return True if the key is in the map, false otherwise
1192
+ */
1193
+ has(key) {
1194
+ const node = lookup(this._tree, key);
1195
+ return node !== void 0 && node.has(LEAF);
1196
+ }
1197
+ /**
1198
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys
1199
+ * @return An `Iterable` iterating through keys
1200
+ */
1201
+ keys() {
1202
+ return new TreeIterator(this, KEYS);
1203
+ }
1204
+ /**
1205
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set
1206
+ * @param key Key to set
1207
+ * @param value Value to associate to the key
1208
+ * @return The {@link SearchableMap} itself, to allow chaining
1209
+ */
1210
+ set(key, value) {
1211
+ if (typeof key !== "string") {
1212
+ throw new Error("key must be a string");
1213
+ }
1214
+ this._size = void 0;
1215
+ const node = createPath(this._tree, key);
1216
+ node.set(LEAF, value);
1217
+ return this;
1218
+ }
1219
+ /**
1220
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size
1221
+ */
1222
+ get size() {
1223
+ if (this._size) {
1224
+ return this._size;
1225
+ }
1226
+ this._size = 0;
1227
+ const iter = this.entries();
1228
+ while (!iter.next().done)
1229
+ this._size += 1;
1230
+ return this._size;
1231
+ }
1232
+ /**
1233
+ * Updates the value at the given key using the provided function. The function
1234
+ * is called with the current value at the key, and its return value is used as
1235
+ * the new value to be set.
1236
+ *
1237
+ * ### Example:
1238
+ *
1239
+ * ```javascript
1240
+ * // Increment the current value by one
1241
+ * searchableMap.update('somekey', (currentValue) => currentValue == null ? 0 : currentValue + 1)
1242
+ * ```
1243
+ *
1244
+ * If the value at the given key is or will be an object, it might not require
1245
+ * re-assignment. In that case it is better to use `fetch()`, because it is
1246
+ * faster.
1247
+ *
1248
+ * @param key The key to update
1249
+ * @param fn The function used to compute the new value from the current one
1250
+ * @return The {@link SearchableMap} itself, to allow chaining
1251
+ */
1252
+ update(key, fn) {
1253
+ if (typeof key !== "string") {
1254
+ throw new Error("key must be a string");
1255
+ }
1256
+ this._size = void 0;
1257
+ const node = createPath(this._tree, key);
1258
+ node.set(LEAF, fn(node.get(LEAF)));
1259
+ return this;
1260
+ }
1261
+ /**
1262
+ * Fetches the value of the given key. If the value does not exist, calls the
1263
+ * given function to create a new value, which is inserted at the given key
1264
+ * and subsequently returned.
1265
+ *
1266
+ * ### Example:
1267
+ *
1268
+ * ```javascript
1269
+ * const map = searchableMap.fetch('somekey', () => new Map())
1270
+ * map.set('foo', 'bar')
1271
+ * ```
1272
+ *
1273
+ * @param key The key to update
1274
+ * @param initial A function that creates a new value if the key does not exist
1275
+ * @return The existing or new value at the given key
1276
+ */
1277
+ fetch(key, initial) {
1278
+ if (typeof key !== "string") {
1279
+ throw new Error("key must be a string");
1280
+ }
1281
+ this._size = void 0;
1282
+ const node = createPath(this._tree, key);
1283
+ let value = node.get(LEAF);
1284
+ if (value === void 0) {
1285
+ node.set(LEAF, value = initial());
1286
+ }
1287
+ return value;
1288
+ }
1289
+ /**
1290
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values
1291
+ * @return An `Iterable` iterating through values.
1292
+ */
1293
+ values() {
1294
+ return new TreeIterator(this, VALUES);
1295
+ }
1296
+ /**
1297
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator
1298
+ */
1299
+ [Symbol.iterator]() {
1300
+ return this.entries();
1301
+ }
1302
+ /**
1303
+ * Creates a {@link SearchableMap} from an `Iterable` of entries
1304
+ *
1305
+ * @param entries Entries to be inserted in the {@link SearchableMap}
1306
+ * @return A new {@link SearchableMap} with the given entries
1307
+ */
1308
+ static from(entries) {
1309
+ const tree = new _SearchableMap();
1310
+ for (const [key, value] of entries) {
1311
+ tree.set(key, value);
1312
+ }
1313
+ return tree;
1314
+ }
1315
+ /**
1316
+ * Creates a {@link SearchableMap} from the iterable properties of a JavaScript object
1317
+ *
1318
+ * @param object Object of entries for the {@link SearchableMap}
1319
+ * @return A new {@link SearchableMap} with the given entries
1320
+ */
1321
+ static fromObject(object) {
1322
+ return _SearchableMap.from(Object.entries(object));
1323
+ }
1324
+ };
1325
+ var trackDown = (tree, key, path = []) => {
1326
+ if (key.length === 0 || tree == null) {
1327
+ return [tree, path];
1328
+ }
1329
+ for (const k of tree.keys()) {
1330
+ if (k !== LEAF && key.startsWith(k)) {
1331
+ path.push([tree, k]);
1332
+ return trackDown(tree.get(k), key.slice(k.length), path);
1333
+ }
1334
+ }
1335
+ path.push([tree, key]);
1336
+ return trackDown(void 0, "", path);
1337
+ };
1338
+ var lookup = (tree, key) => {
1339
+ if (key.length === 0 || tree == null) {
1340
+ return tree;
1341
+ }
1342
+ for (const k of tree.keys()) {
1343
+ if (k !== LEAF && key.startsWith(k)) {
1344
+ return lookup(tree.get(k), key.slice(k.length));
1345
+ }
1346
+ }
1347
+ };
1348
+ var createPath = (node, key) => {
1349
+ const keyLength = key.length;
1350
+ outer: for (let pos = 0; node && pos < keyLength; ) {
1351
+ for (const k of node.keys()) {
1352
+ if (k !== LEAF && key[pos] === k[0]) {
1353
+ const len = Math.min(keyLength - pos, k.length);
1354
+ let offset = 1;
1355
+ while (offset < len && key[pos + offset] === k[offset])
1356
+ ++offset;
1357
+ const child2 = node.get(k);
1358
+ if (offset === k.length) {
1359
+ node = child2;
1360
+ } else {
1361
+ const intermediate = /* @__PURE__ */ new Map();
1362
+ intermediate.set(k.slice(offset), child2);
1363
+ node.set(key.slice(pos, pos + offset), intermediate);
1364
+ node.delete(k);
1365
+ node = intermediate;
1366
+ }
1367
+ pos += offset;
1368
+ continue outer;
1369
+ }
1370
+ }
1371
+ const child = /* @__PURE__ */ new Map();
1372
+ node.set(key.slice(pos), child);
1373
+ return child;
1374
+ }
1375
+ return node;
1376
+ };
1377
+ var remove = (tree, key) => {
1378
+ const [node, path] = trackDown(tree, key);
1379
+ if (node === void 0) {
1380
+ return;
1381
+ }
1382
+ node.delete(LEAF);
1383
+ if (node.size === 0) {
1384
+ cleanup(path);
1385
+ } else if (node.size === 1) {
1386
+ const [key2, value] = node.entries().next().value;
1387
+ merge(path, key2, value);
1388
+ }
1389
+ };
1390
+ var cleanup = (path) => {
1391
+ if (path.length === 0) {
1392
+ return;
1393
+ }
1394
+ const [node, key] = last(path);
1395
+ node.delete(key);
1396
+ if (node.size === 0) {
1397
+ cleanup(path.slice(0, -1));
1398
+ } else if (node.size === 1) {
1399
+ const [key2, value] = node.entries().next().value;
1400
+ if (key2 !== LEAF) {
1401
+ merge(path.slice(0, -1), key2, value);
1402
+ }
1403
+ }
1404
+ };
1405
+ var merge = (path, key, value) => {
1406
+ if (path.length === 0) {
1407
+ return;
1408
+ }
1409
+ const [node, nodeKey] = last(path);
1410
+ node.set(nodeKey + key, value);
1411
+ node.delete(nodeKey);
1412
+ };
1413
+ var last = (array) => {
1414
+ return array[array.length - 1];
1415
+ };
1416
+ var OR = "or";
1417
+ var AND = "and";
1418
+ var AND_NOT = "and_not";
1419
+ var MiniSearch = class _MiniSearch {
1420
+ /**
1421
+ * @param options Configuration options
1422
+ *
1423
+ * ### Examples:
1424
+ *
1425
+ * ```javascript
1426
+ * // Create a search engine that indexes the 'title' and 'text' fields of your
1427
+ * // documents:
1428
+ * const miniSearch = new MiniSearch({ fields: ['title', 'text'] })
1429
+ * ```
1430
+ *
1431
+ * ### ID Field:
1432
+ *
1433
+ * ```javascript
1434
+ * // Your documents are assumed to include a unique 'id' field, but if you want
1435
+ * // to use a different field for document identification, you can set the
1436
+ * // 'idField' option:
1437
+ * const miniSearch = new MiniSearch({ idField: 'key', fields: ['title', 'text'] })
1438
+ * ```
1439
+ *
1440
+ * ### Options and defaults:
1441
+ *
1442
+ * ```javascript
1443
+ * // The full set of options (here with their default value) is:
1444
+ * const miniSearch = new MiniSearch({
1445
+ * // idField: field that uniquely identifies a document
1446
+ * idField: 'id',
1447
+ *
1448
+ * // extractField: function used to get the value of a field in a document.
1449
+ * // By default, it assumes the document is a flat object with field names as
1450
+ * // property keys and field values as string property values, but custom logic
1451
+ * // can be implemented by setting this option to a custom extractor function.
1452
+ * extractField: (document, fieldName) => document[fieldName],
1453
+ *
1454
+ * // tokenize: function used to split fields into individual terms. By
1455
+ * // default, it is also used to tokenize search queries, unless a specific
1456
+ * // `tokenize` search option is supplied. When tokenizing an indexed field,
1457
+ * // the field name is passed as the second argument.
1458
+ * tokenize: (string, _fieldName) => string.split(SPACE_OR_PUNCTUATION),
1459
+ *
1460
+ * // processTerm: function used to process each tokenized term before
1461
+ * // indexing. It can be used for stemming and normalization. Return a falsy
1462
+ * // value in order to discard a term. By default, it is also used to process
1463
+ * // search queries, unless a specific `processTerm` option is supplied as a
1464
+ * // search option. When processing a term from a indexed field, the field
1465
+ * // name is passed as the second argument.
1466
+ * processTerm: (term, _fieldName) => term.toLowerCase(),
1467
+ *
1468
+ * // searchOptions: default search options, see the `search` method for
1469
+ * // details
1470
+ * searchOptions: undefined,
1471
+ *
1472
+ * // fields: document fields to be indexed. Mandatory, but not set by default
1473
+ * fields: undefined
1474
+ *
1475
+ * // storeFields: document fields to be stored and returned as part of the
1476
+ * // search results.
1477
+ * storeFields: []
1478
+ * })
1479
+ * ```
1480
+ */
1481
+ constructor(options) {
1482
+ if ((options === null || options === void 0 ? void 0 : options.fields) == null) {
1483
+ throw new Error('MiniSearch: option "fields" must be provided');
1484
+ }
1485
+ const autoVacuum = options.autoVacuum == null || options.autoVacuum === true ? defaultAutoVacuumOptions : options.autoVacuum;
1486
+ this._options = {
1487
+ ...defaultOptions,
1488
+ ...options,
1489
+ autoVacuum,
1490
+ searchOptions: { ...defaultSearchOptions, ...options.searchOptions || {} },
1491
+ autoSuggestOptions: { ...defaultAutoSuggestOptions, ...options.autoSuggestOptions || {} }
1492
+ };
1493
+ this._index = new SearchableMap();
1494
+ this._documentCount = 0;
1495
+ this._documentIds = /* @__PURE__ */ new Map();
1496
+ this._idToShortId = /* @__PURE__ */ new Map();
1497
+ this._fieldIds = {};
1498
+ this._fieldLength = /* @__PURE__ */ new Map();
1499
+ this._avgFieldLength = [];
1500
+ this._nextId = 0;
1501
+ this._storedFields = /* @__PURE__ */ new Map();
1502
+ this._dirtCount = 0;
1503
+ this._currentVacuum = null;
1504
+ this._enqueuedVacuum = null;
1505
+ this._enqueuedVacuumConditions = defaultVacuumConditions;
1506
+ this.addFields(this._options.fields);
1507
+ }
1508
+ /**
1509
+ * Adds a document to the index
1510
+ *
1511
+ * @param document The document to be indexed
1512
+ */
1513
+ add(document) {
1514
+ const { extractField, stringifyField, tokenize, processTerm, fields, idField } = this._options;
1515
+ const id = extractField(document, idField);
1516
+ if (id == null) {
1517
+ throw new Error(`MiniSearch: document does not have ID field "${idField}"`);
1518
+ }
1519
+ if (this._idToShortId.has(id)) {
1520
+ throw new Error(`MiniSearch: duplicate ID ${id}`);
1521
+ }
1522
+ const shortDocumentId = this.addDocumentId(id);
1523
+ this.saveStoredFields(shortDocumentId, document);
1524
+ for (const field of fields) {
1525
+ const fieldValue = extractField(document, field);
1526
+ if (fieldValue == null)
1527
+ continue;
1528
+ const tokens = tokenize(stringifyField(fieldValue, field), field);
1529
+ const fieldId = this._fieldIds[field];
1530
+ const uniqueTerms = new Set(tokens).size;
1531
+ this.addFieldLength(shortDocumentId, fieldId, this._documentCount - 1, uniqueTerms);
1532
+ for (const term of tokens) {
1533
+ const processedTerm = processTerm(term, field);
1534
+ if (Array.isArray(processedTerm)) {
1535
+ for (const t of processedTerm) {
1536
+ this.addTerm(fieldId, shortDocumentId, t);
1537
+ }
1538
+ } else if (processedTerm) {
1539
+ this.addTerm(fieldId, shortDocumentId, processedTerm);
1540
+ }
1541
+ }
1542
+ }
1543
+ }
1544
+ /**
1545
+ * Adds all the given documents to the index
1546
+ *
1547
+ * @param documents An array of documents to be indexed
1548
+ */
1549
+ addAll(documents) {
1550
+ for (const document of documents)
1551
+ this.add(document);
1552
+ }
1553
+ /**
1554
+ * Adds all the given documents to the index asynchronously.
1555
+ *
1556
+ * Returns a promise that resolves (to `undefined`) when the indexing is done.
1557
+ * This method is useful when index many documents, to avoid blocking the main
1558
+ * thread. The indexing is performed asynchronously and in chunks.
1559
+ *
1560
+ * @param documents An array of documents to be indexed
1561
+ * @param options Configuration options
1562
+ * @return A promise resolving to `undefined` when the indexing is done
1563
+ */
1564
+ addAllAsync(documents, options = {}) {
1565
+ const { chunkSize = 10 } = options;
1566
+ const acc = { chunk: [], promise: Promise.resolve() };
1567
+ const { chunk, promise } = documents.reduce(({ chunk: chunk2, promise: promise2 }, document, i) => {
1568
+ chunk2.push(document);
1569
+ if ((i + 1) % chunkSize === 0) {
1570
+ return {
1571
+ chunk: [],
1572
+ promise: promise2.then(() => new Promise((resolve) => setTimeout(resolve, 0))).then(() => this.addAll(chunk2))
1573
+ };
1574
+ } else {
1575
+ return { chunk: chunk2, promise: promise2 };
1576
+ }
1577
+ }, acc);
1578
+ return promise.then(() => this.addAll(chunk));
1579
+ }
1580
+ /**
1581
+ * Removes the given document from the index.
1582
+ *
1583
+ * The document to remove must NOT have changed between indexing and removal,
1584
+ * otherwise the index will be corrupted.
1585
+ *
1586
+ * This method requires passing the full document to be removed (not just the
1587
+ * ID), and immediately removes the document from the inverted index, allowing
1588
+ * memory to be released. A convenient alternative is {@link
1589
+ * MiniSearch#discard}, which needs only the document ID, and has the same
1590
+ * visible effect, but delays cleaning up the index until the next vacuuming.
1591
+ *
1592
+ * @param document The document to be removed
1593
+ */
1594
+ remove(document) {
1595
+ const { tokenize, processTerm, extractField, stringifyField, fields, idField } = this._options;
1596
+ const id = extractField(document, idField);
1597
+ if (id == null) {
1598
+ throw new Error(`MiniSearch: document does not have ID field "${idField}"`);
1599
+ }
1600
+ const shortId = this._idToShortId.get(id);
1601
+ if (shortId == null) {
1602
+ throw new Error(`MiniSearch: cannot remove document with ID ${id}: it is not in the index`);
1603
+ }
1604
+ for (const field of fields) {
1605
+ const fieldValue = extractField(document, field);
1606
+ if (fieldValue == null)
1607
+ continue;
1608
+ const tokens = tokenize(stringifyField(fieldValue, field), field);
1609
+ const fieldId = this._fieldIds[field];
1610
+ const uniqueTerms = new Set(tokens).size;
1611
+ this.removeFieldLength(shortId, fieldId, this._documentCount, uniqueTerms);
1612
+ for (const term of tokens) {
1613
+ const processedTerm = processTerm(term, field);
1614
+ if (Array.isArray(processedTerm)) {
1615
+ for (const t of processedTerm) {
1616
+ this.removeTerm(fieldId, shortId, t);
1617
+ }
1618
+ } else if (processedTerm) {
1619
+ this.removeTerm(fieldId, shortId, processedTerm);
1620
+ }
1621
+ }
1622
+ }
1623
+ this._storedFields.delete(shortId);
1624
+ this._documentIds.delete(shortId);
1625
+ this._idToShortId.delete(id);
1626
+ this._fieldLength.delete(shortId);
1627
+ this._documentCount -= 1;
1628
+ }
1629
+ /**
1630
+ * Removes all the given documents from the index. If called with no arguments,
1631
+ * it removes _all_ documents from the index.
1632
+ *
1633
+ * @param documents The documents to be removed. If this argument is omitted,
1634
+ * all documents are removed. Note that, for removing all documents, it is
1635
+ * more efficient to call this method with no arguments than to pass all
1636
+ * documents.
1637
+ */
1638
+ removeAll(documents) {
1639
+ if (documents) {
1640
+ for (const document of documents)
1641
+ this.remove(document);
1642
+ } else if (arguments.length > 0) {
1643
+ throw new Error("Expected documents to be present. Omit the argument to remove all documents.");
1644
+ } else {
1645
+ this._index = new SearchableMap();
1646
+ this._documentCount = 0;
1647
+ this._documentIds = /* @__PURE__ */ new Map();
1648
+ this._idToShortId = /* @__PURE__ */ new Map();
1649
+ this._fieldLength = /* @__PURE__ */ new Map();
1650
+ this._avgFieldLength = [];
1651
+ this._storedFields = /* @__PURE__ */ new Map();
1652
+ this._nextId = 0;
1653
+ }
1654
+ }
1655
+ /**
1656
+ * Discards the document with the given ID, so it won't appear in search results
1657
+ *
1658
+ * It has the same visible effect of {@link MiniSearch.remove} (both cause the
1659
+ * document to stop appearing in searches), but a different effect on the
1660
+ * internal data structures:
1661
+ *
1662
+ * - {@link MiniSearch#remove} requires passing the full document to be
1663
+ * removed as argument, and removes it from the inverted index immediately.
1664
+ *
1665
+ * - {@link MiniSearch#discard} instead only needs the document ID, and
1666
+ * works by marking the current version of the document as discarded, so it
1667
+ * is immediately ignored by searches. This is faster and more convenient
1668
+ * than {@link MiniSearch#remove}, but the index is not immediately
1669
+ * modified. To take care of that, vacuuming is performed after a certain
1670
+ * number of documents are discarded, cleaning up the index and allowing
1671
+ * memory to be released.
1672
+ *
1673
+ * After discarding a document, it is possible to re-add a new version, and
1674
+ * only the new version will appear in searches. In other words, discarding
1675
+ * and re-adding a document works exactly like removing and re-adding it. The
1676
+ * {@link MiniSearch.replace} method can also be used to replace a document
1677
+ * with a new version.
1678
+ *
1679
+ * #### Details about vacuuming
1680
+ *
1681
+ * Repetite calls to this method would leave obsolete document references in
1682
+ * the index, invisible to searches. Two mechanisms take care of cleaning up:
1683
+ * clean up during search, and vacuuming.
1684
+ *
1685
+ * - Upon search, whenever a discarded ID is found (and ignored for the
1686
+ * results), references to the discarded document are removed from the
1687
+ * inverted index entries for the search terms. This ensures that subsequent
1688
+ * searches for the same terms do not need to skip these obsolete references
1689
+ * again.
1690
+ *
1691
+ * - In addition, vacuuming is performed automatically by default (see the
1692
+ * `autoVacuum` field in {@link Options}) after a certain number of
1693
+ * documents are discarded. Vacuuming traverses all terms in the index,
1694
+ * cleaning up all references to discarded documents. Vacuuming can also be
1695
+ * triggered manually by calling {@link MiniSearch#vacuum}.
1696
+ *
1697
+ * @param id The ID of the document to be discarded
1698
+ */
1699
+ discard(id) {
1700
+ const shortId = this._idToShortId.get(id);
1701
+ if (shortId == null) {
1702
+ throw new Error(`MiniSearch: cannot discard document with ID ${id}: it is not in the index`);
1703
+ }
1704
+ this._idToShortId.delete(id);
1705
+ this._documentIds.delete(shortId);
1706
+ this._storedFields.delete(shortId);
1707
+ (this._fieldLength.get(shortId) || []).forEach((fieldLength, fieldId) => {
1708
+ this.removeFieldLength(shortId, fieldId, this._documentCount, fieldLength);
1709
+ });
1710
+ this._fieldLength.delete(shortId);
1711
+ this._documentCount -= 1;
1712
+ this._dirtCount += 1;
1713
+ this.maybeAutoVacuum();
1714
+ }
1715
+ maybeAutoVacuum() {
1716
+ if (this._options.autoVacuum === false) {
1717
+ return;
1718
+ }
1719
+ const { minDirtFactor, minDirtCount, batchSize, batchWait } = this._options.autoVacuum;
1720
+ this.conditionalVacuum({ batchSize, batchWait }, { minDirtCount, minDirtFactor });
1721
+ }
1722
+ /**
1723
+ * Discards the documents with the given IDs, so they won't appear in search
1724
+ * results
1725
+ *
1726
+ * It is equivalent to calling {@link MiniSearch#discard} for all the given
1727
+ * IDs, but with the optimization of triggering at most one automatic
1728
+ * vacuuming at the end.
1729
+ *
1730
+ * Note: to remove all documents from the index, it is faster and more
1731
+ * convenient to call {@link MiniSearch.removeAll} with no argument, instead
1732
+ * of passing all IDs to this method.
1733
+ */
1734
+ discardAll(ids) {
1735
+ const autoVacuum = this._options.autoVacuum;
1736
+ try {
1737
+ this._options.autoVacuum = false;
1738
+ for (const id of ids) {
1739
+ this.discard(id);
1740
+ }
1741
+ } finally {
1742
+ this._options.autoVacuum = autoVacuum;
1743
+ }
1744
+ this.maybeAutoVacuum();
1745
+ }
1746
+ /**
1747
+ * It replaces an existing document with the given updated version
1748
+ *
1749
+ * It works by discarding the current version and adding the updated one, so
1750
+ * it is functionally equivalent to calling {@link MiniSearch#discard}
1751
+ * followed by {@link MiniSearch#add}. The ID of the updated document should
1752
+ * be the same as the original one.
1753
+ *
1754
+ * Since it uses {@link MiniSearch#discard} internally, this method relies on
1755
+ * vacuuming to clean up obsolete document references from the index, allowing
1756
+ * memory to be released (see {@link MiniSearch#discard}).
1757
+ *
1758
+ * @param updatedDocument The updated document to replace the old version
1759
+ * with
1760
+ */
1761
+ replace(updatedDocument) {
1762
+ const { idField, extractField } = this._options;
1763
+ const id = extractField(updatedDocument, idField);
1764
+ this.discard(id);
1765
+ this.add(updatedDocument);
1766
+ }
1767
+ /**
1768
+ * Triggers a manual vacuuming, cleaning up references to discarded documents
1769
+ * from the inverted index
1770
+ *
1771
+ * Vacuuming is only useful for applications that use the {@link
1772
+ * MiniSearch#discard} or {@link MiniSearch#replace} methods.
1773
+ *
1774
+ * By default, vacuuming is performed automatically when needed (controlled by
1775
+ * the `autoVacuum` field in {@link Options}), so there is usually no need to
1776
+ * call this method, unless one wants to make sure to perform vacuuming at a
1777
+ * specific moment.
1778
+ *
1779
+ * Vacuuming traverses all terms in the inverted index in batches, and cleans
1780
+ * up references to discarded documents from the posting list, allowing memory
1781
+ * to be released.
1782
+ *
1783
+ * The method takes an optional object as argument with the following keys:
1784
+ *
1785
+ * - `batchSize`: the size of each batch (1000 by default)
1786
+ *
1787
+ * - `batchWait`: the number of milliseconds to wait between batches (10 by
1788
+ * default)
1789
+ *
1790
+ * On large indexes, vacuuming could have a non-negligible cost: batching
1791
+ * avoids blocking the thread for long, diluting this cost so that it is not
1792
+ * negatively affecting the application. Nonetheless, this method should only
1793
+ * be called when necessary, and relying on automatic vacuuming is usually
1794
+ * better.
1795
+ *
1796
+ * It returns a promise that resolves (to undefined) when the clean up is
1797
+ * completed. If vacuuming is already ongoing at the time this method is
1798
+ * called, a new one is enqueued immediately after the ongoing one, and a
1799
+ * corresponding promise is returned. However, no more than one vacuuming is
1800
+ * enqueued on top of the ongoing one, even if this method is called more
1801
+ * times (enqueuing multiple ones would be useless).
1802
+ *
1803
+ * @param options Configuration options for the batch size and delay. See
1804
+ * {@link VacuumOptions}.
1805
+ */
1806
+ vacuum(options = {}) {
1807
+ return this.conditionalVacuum(options);
1808
+ }
1809
+ conditionalVacuum(options, conditions) {
1810
+ if (this._currentVacuum) {
1811
+ this._enqueuedVacuumConditions = this._enqueuedVacuumConditions && conditions;
1812
+ if (this._enqueuedVacuum != null) {
1813
+ return this._enqueuedVacuum;
1814
+ }
1815
+ this._enqueuedVacuum = this._currentVacuum.then(() => {
1816
+ const conditions2 = this._enqueuedVacuumConditions;
1817
+ this._enqueuedVacuumConditions = defaultVacuumConditions;
1818
+ return this.performVacuuming(options, conditions2);
1819
+ });
1820
+ return this._enqueuedVacuum;
1821
+ }
1822
+ if (this.vacuumConditionsMet(conditions) === false) {
1823
+ return Promise.resolve();
1824
+ }
1825
+ this._currentVacuum = this.performVacuuming(options);
1826
+ return this._currentVacuum;
1827
+ }
1828
+ async performVacuuming(options, conditions) {
1829
+ const initialDirtCount = this._dirtCount;
1830
+ if (this.vacuumConditionsMet(conditions)) {
1831
+ const batchSize = options.batchSize || defaultVacuumOptions.batchSize;
1832
+ const batchWait = options.batchWait || defaultVacuumOptions.batchWait;
1833
+ let i = 1;
1834
+ for (const [term, fieldsData] of this._index) {
1835
+ for (const [fieldId, fieldIndex] of fieldsData) {
1836
+ for (const [shortId] of fieldIndex) {
1837
+ if (this._documentIds.has(shortId)) {
1838
+ continue;
1839
+ }
1840
+ if (fieldIndex.size <= 1) {
1841
+ fieldsData.delete(fieldId);
1842
+ } else {
1843
+ fieldIndex.delete(shortId);
1844
+ }
1845
+ }
1846
+ }
1847
+ if (this._index.get(term).size === 0) {
1848
+ this._index.delete(term);
1849
+ }
1850
+ if (i % batchSize === 0) {
1851
+ await new Promise((resolve) => setTimeout(resolve, batchWait));
1852
+ }
1853
+ i += 1;
1854
+ }
1855
+ this._dirtCount -= initialDirtCount;
1856
+ }
1857
+ await null;
1858
+ this._currentVacuum = this._enqueuedVacuum;
1859
+ this._enqueuedVacuum = null;
1860
+ }
1861
+ vacuumConditionsMet(conditions) {
1862
+ if (conditions == null) {
1863
+ return true;
1864
+ }
1865
+ let { minDirtCount, minDirtFactor } = conditions;
1866
+ minDirtCount = minDirtCount || defaultAutoVacuumOptions.minDirtCount;
1867
+ minDirtFactor = minDirtFactor || defaultAutoVacuumOptions.minDirtFactor;
1868
+ return this.dirtCount >= minDirtCount && this.dirtFactor >= minDirtFactor;
1869
+ }
1870
+ /**
1871
+ * Is `true` if a vacuuming operation is ongoing, `false` otherwise
1872
+ */
1873
+ get isVacuuming() {
1874
+ return this._currentVacuum != null;
1875
+ }
1876
+ /**
1877
+ * The number of documents discarded since the most recent vacuuming
1878
+ */
1879
+ get dirtCount() {
1880
+ return this._dirtCount;
1881
+ }
1882
+ /**
1883
+ * A number between 0 and 1 giving an indication about the proportion of
1884
+ * documents that are discarded, and can therefore be cleaned up by vacuuming.
1885
+ * A value close to 0 means that the index is relatively clean, while a higher
1886
+ * value means that the index is relatively dirty, and vacuuming could release
1887
+ * memory.
1888
+ */
1889
+ get dirtFactor() {
1890
+ return this._dirtCount / (1 + this._documentCount + this._dirtCount);
1891
+ }
1892
+ /**
1893
+ * Returns `true` if a document with the given ID is present in the index and
1894
+ * available for search, `false` otherwise
1895
+ *
1896
+ * @param id The document ID
1897
+ */
1898
+ has(id) {
1899
+ return this._idToShortId.has(id);
1900
+ }
1901
+ /**
1902
+ * Returns the stored fields (as configured in the `storeFields` constructor
1903
+ * option) for the given document ID. Returns `undefined` if the document is
1904
+ * not present in the index.
1905
+ *
1906
+ * @param id The document ID
1907
+ */
1908
+ getStoredFields(id) {
1909
+ const shortId = this._idToShortId.get(id);
1910
+ if (shortId == null) {
1911
+ return void 0;
1912
+ }
1913
+ return this._storedFields.get(shortId);
1914
+ }
1915
+ /**
1916
+ * Search for documents matching the given search query.
1917
+ *
1918
+ * The result is a list of scored document IDs matching the query, sorted by
1919
+ * descending score, and each including data about which terms were matched and
1920
+ * in which fields.
1921
+ *
1922
+ * ### Basic usage:
1923
+ *
1924
+ * ```javascript
1925
+ * // Search for "zen art motorcycle" with default options: terms have to match
1926
+ * // exactly, and individual terms are joined with OR
1927
+ * miniSearch.search('zen art motorcycle')
1928
+ * // => [ { id: 2, score: 2.77258, match: { ... } }, { id: 4, score: 1.38629, match: { ... } } ]
1929
+ * ```
1930
+ *
1931
+ * ### Restrict search to specific fields:
1932
+ *
1933
+ * ```javascript
1934
+ * // Search only in the 'title' field
1935
+ * miniSearch.search('zen', { fields: ['title'] })
1936
+ * ```
1937
+ *
1938
+ * ### Field boosting:
1939
+ *
1940
+ * ```javascript
1941
+ * // Boost a field
1942
+ * miniSearch.search('zen', { boost: { title: 2 } })
1943
+ * ```
1944
+ *
1945
+ * ### Prefix search:
1946
+ *
1947
+ * ```javascript
1948
+ * // Search for "moto" with prefix search (it will match documents
1949
+ * // containing terms that start with "moto" or "neuro")
1950
+ * miniSearch.search('moto neuro', { prefix: true })
1951
+ * ```
1952
+ *
1953
+ * ### Fuzzy search:
1954
+ *
1955
+ * ```javascript
1956
+ * // Search for "ismael" with fuzzy search (it will match documents containing
1957
+ * // terms similar to "ismael", with a maximum edit distance of 0.2 term.length
1958
+ * // (rounded to nearest integer)
1959
+ * miniSearch.search('ismael', { fuzzy: 0.2 })
1960
+ * ```
1961
+ *
1962
+ * ### Combining strategies:
1963
+ *
1964
+ * ```javascript
1965
+ * // Mix of exact match, prefix search, and fuzzy search
1966
+ * miniSearch.search('ismael mob', {
1967
+ * prefix: true,
1968
+ * fuzzy: 0.2
1969
+ * })
1970
+ * ```
1971
+ *
1972
+ * ### Advanced prefix and fuzzy search:
1973
+ *
1974
+ * ```javascript
1975
+ * // Perform fuzzy and prefix search depending on the search term. Here
1976
+ * // performing prefix and fuzzy search only on terms longer than 3 characters
1977
+ * miniSearch.search('ismael mob', {
1978
+ * prefix: term => term.length > 3
1979
+ * fuzzy: term => term.length > 3 ? 0.2 : null
1980
+ * })
1981
+ * ```
1982
+ *
1983
+ * ### Combine with AND:
1984
+ *
1985
+ * ```javascript
1986
+ * // Combine search terms with AND (to match only documents that contain both
1987
+ * // "motorcycle" and "art")
1988
+ * miniSearch.search('motorcycle art', { combineWith: 'AND' })
1989
+ * ```
1990
+ *
1991
+ * ### Combine with AND_NOT:
1992
+ *
1993
+ * There is also an AND_NOT combinator, that finds documents that match the
1994
+ * first term, but do not match any of the other terms. This combinator is
1995
+ * rarely useful with simple queries, and is meant to be used with advanced
1996
+ * query combinations (see later for more details).
1997
+ *
1998
+ * ### Filtering results:
1999
+ *
2000
+ * ```javascript
2001
+ * // Filter only results in the 'fiction' category (assuming that 'category'
2002
+ * // is a stored field)
2003
+ * miniSearch.search('motorcycle art', {
2004
+ * filter: (result) => result.category === 'fiction'
2005
+ * })
2006
+ * ```
2007
+ *
2008
+ * ### Wildcard query
2009
+ *
2010
+ * Searching for an empty string (assuming the default tokenizer) returns no
2011
+ * results. Sometimes though, one needs to match all documents, like in a
2012
+ * "wildcard" search. This is possible by passing the special value
2013
+ * {@link MiniSearch.wildcard} as the query:
2014
+ *
2015
+ * ```javascript
2016
+ * // Return search results for all documents
2017
+ * miniSearch.search(MiniSearch.wildcard)
2018
+ * ```
2019
+ *
2020
+ * Note that search options such as `filter` and `boostDocument` are still
2021
+ * applied, influencing which results are returned, and their order:
2022
+ *
2023
+ * ```javascript
2024
+ * // Return search results for all documents in the 'fiction' category
2025
+ * miniSearch.search(MiniSearch.wildcard, {
2026
+ * filter: (result) => result.category === 'fiction'
2027
+ * })
2028
+ * ```
2029
+ *
2030
+ * ### Advanced combination of queries:
2031
+ *
2032
+ * It is possible to combine different subqueries with OR, AND, and AND_NOT,
2033
+ * and even with different search options, by passing a query expression
2034
+ * tree object as the first argument, instead of a string.
2035
+ *
2036
+ * ```javascript
2037
+ * // Search for documents that contain "zen" and ("motorcycle" or "archery")
2038
+ * miniSearch.search({
2039
+ * combineWith: 'AND',
2040
+ * queries: [
2041
+ * 'zen',
2042
+ * {
2043
+ * combineWith: 'OR',
2044
+ * queries: ['motorcycle', 'archery']
2045
+ * }
2046
+ * ]
2047
+ * })
2048
+ *
2049
+ * // Search for documents that contain ("apple" or "pear") but not "juice" and
2050
+ * // not "tree"
2051
+ * miniSearch.search({
2052
+ * combineWith: 'AND_NOT',
2053
+ * queries: [
2054
+ * {
2055
+ * combineWith: 'OR',
2056
+ * queries: ['apple', 'pear']
2057
+ * },
2058
+ * 'juice',
2059
+ * 'tree'
2060
+ * ]
2061
+ * })
2062
+ * ```
2063
+ *
2064
+ * Each node in the expression tree can be either a string, or an object that
2065
+ * supports all {@link SearchOptions} fields, plus a `queries` array field for
2066
+ * subqueries.
2067
+ *
2068
+ * Note that, while this can become complicated to do by hand for complex or
2069
+ * deeply nested queries, it provides a formalized expression tree API for
2070
+ * external libraries that implement a parser for custom query languages.
2071
+ *
2072
+ * @param query Search query
2073
+ * @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.
2074
+ */
2075
+ search(query, searchOptions = {}) {
2076
+ const { searchOptions: globalSearchOptions } = this._options;
2077
+ const searchOptionsWithDefaults = { ...globalSearchOptions, ...searchOptions };
2078
+ const rawResults = this.executeQuery(query, searchOptions);
2079
+ const results = [];
2080
+ for (const [docId, { score, terms, match }] of rawResults) {
2081
+ const quality = terms.length || 1;
2082
+ const result = {
2083
+ id: this._documentIds.get(docId),
2084
+ score: score * quality,
2085
+ terms: Object.keys(match),
2086
+ queryTerms: terms,
2087
+ match
2088
+ };
2089
+ Object.assign(result, this._storedFields.get(docId));
2090
+ if (searchOptionsWithDefaults.filter == null || searchOptionsWithDefaults.filter(result)) {
2091
+ results.push(result);
2092
+ }
2093
+ }
2094
+ if (query === _MiniSearch.wildcard && searchOptionsWithDefaults.boostDocument == null) {
2095
+ return results;
2096
+ }
2097
+ results.sort(byScore);
2098
+ return results;
2099
+ }
2100
+ /**
2101
+ * Provide suggestions for the given search query
2102
+ *
2103
+ * The result is a list of suggested modified search queries, derived from the
2104
+ * given search query, each with a relevance score, sorted by descending score.
2105
+ *
2106
+ * By default, it uses the same options used for search, except that by
2107
+ * default it performs prefix search on the last term of the query, and
2108
+ * combine terms with `'AND'` (requiring all query terms to match). Custom
2109
+ * options can be passed as a second argument. Defaults can be changed upon
2110
+ * calling the {@link MiniSearch} constructor, by passing a
2111
+ * `autoSuggestOptions` option.
2112
+ *
2113
+ * ### Basic usage:
2114
+ *
2115
+ * ```javascript
2116
+ * // Get suggestions for 'neuro':
2117
+ * miniSearch.autoSuggest('neuro')
2118
+ * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ]
2119
+ * ```
2120
+ *
2121
+ * ### Multiple words:
2122
+ *
2123
+ * ```javascript
2124
+ * // Get suggestions for 'zen ar':
2125
+ * miniSearch.autoSuggest('zen ar')
2126
+ * // => [
2127
+ * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
2128
+ * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
2129
+ * // ]
2130
+ * ```
2131
+ *
2132
+ * ### Fuzzy suggestions:
2133
+ *
2134
+ * ```javascript
2135
+ * // Correct spelling mistakes using fuzzy search:
2136
+ * miniSearch.autoSuggest('neromancer', { fuzzy: 0.2 })
2137
+ * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ]
2138
+ * ```
2139
+ *
2140
+ * ### Filtering:
2141
+ *
2142
+ * ```javascript
2143
+ * // Get suggestions for 'zen ar', but only within the 'fiction' category
2144
+ * // (assuming that 'category' is a stored field):
2145
+ * miniSearch.autoSuggest('zen ar', {
2146
+ * filter: (result) => result.category === 'fiction'
2147
+ * })
2148
+ * // => [
2149
+ * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
2150
+ * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
2151
+ * // ]
2152
+ * ```
2153
+ *
2154
+ * @param queryString Query string to be expanded into suggestions
2155
+ * @param options Search options. The supported options and default values
2156
+ * are the same as for the {@link MiniSearch#search} method, except that by
2157
+ * default prefix search is performed on the last term in the query, and terms
2158
+ * are combined with `'AND'`.
2159
+ * @return A sorted array of suggestions sorted by relevance score.
2160
+ */
2161
+ autoSuggest(queryString, options = {}) {
2162
+ options = { ...this._options.autoSuggestOptions, ...options };
2163
+ const suggestions = /* @__PURE__ */ new Map();
2164
+ for (const { score, terms } of this.search(queryString, options)) {
2165
+ const phrase = terms.join(" ");
2166
+ const suggestion = suggestions.get(phrase);
2167
+ if (suggestion != null) {
2168
+ suggestion.score += score;
2169
+ suggestion.count += 1;
2170
+ } else {
2171
+ suggestions.set(phrase, { score, terms, count: 1 });
2172
+ }
2173
+ }
2174
+ const results = [];
2175
+ for (const [suggestion, { score, terms, count }] of suggestions) {
2176
+ results.push({ suggestion, terms, score: score / count });
2177
+ }
2178
+ results.sort(byScore);
2179
+ return results;
2180
+ }
2181
+ /**
2182
+ * Total number of documents available to search
2183
+ */
2184
+ get documentCount() {
2185
+ return this._documentCount;
2186
+ }
2187
+ /**
2188
+ * Number of terms in the index
2189
+ */
2190
+ get termCount() {
2191
+ return this._index.size;
2192
+ }
2193
+ /**
2194
+ * Deserializes a JSON index (serialized with `JSON.stringify(miniSearch)`)
2195
+ * and instantiates a MiniSearch instance. It should be given the same options
2196
+ * originally used when serializing the index.
2197
+ *
2198
+ * ### Usage:
2199
+ *
2200
+ * ```javascript
2201
+ * // If the index was serialized with:
2202
+ * let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
2203
+ * miniSearch.addAll(documents)
2204
+ *
2205
+ * const json = JSON.stringify(miniSearch)
2206
+ * // It can later be deserialized like this:
2207
+ * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
2208
+ * ```
2209
+ *
2210
+ * @param json JSON-serialized index
2211
+ * @param options configuration options, same as the constructor
2212
+ * @return An instance of MiniSearch deserialized from the given JSON.
2213
+ */
2214
+ static loadJSON(json, options) {
2215
+ if (options == null) {
2216
+ throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");
2217
+ }
2218
+ return this.loadJS(JSON.parse(json), options);
2219
+ }
2220
+ /**
2221
+ * Async equivalent of {@link MiniSearch.loadJSON}
2222
+ *
2223
+ * This function is an alternative to {@link MiniSearch.loadJSON} that returns
2224
+ * a promise, and loads the index in batches, leaving pauses between them to avoid
2225
+ * blocking the main thread. It tends to be slower than the synchronous
2226
+ * version, but does not block the main thread, so it can be a better choice
2227
+ * when deserializing very large indexes.
2228
+ *
2229
+ * @param json JSON-serialized index
2230
+ * @param options configuration options, same as the constructor
2231
+ * @return A Promise that will resolve to an instance of MiniSearch deserialized from the given JSON.
2232
+ */
2233
+ static async loadJSONAsync(json, options) {
2234
+ if (options == null) {
2235
+ throw new Error("MiniSearch: loadJSON should be given the same options used when serializing the index");
2236
+ }
2237
+ return this.loadJSAsync(JSON.parse(json), options);
2238
+ }
2239
+ /**
2240
+ * Returns the default value of an option. It will throw an error if no option
2241
+ * with the given name exists.
2242
+ *
2243
+ * @param optionName Name of the option
2244
+ * @return The default value of the given option
2245
+ *
2246
+ * ### Usage:
2247
+ *
2248
+ * ```javascript
2249
+ * // Get default tokenizer
2250
+ * MiniSearch.getDefault('tokenize')
2251
+ *
2252
+ * // Get default term processor
2253
+ * MiniSearch.getDefault('processTerm')
2254
+ *
2255
+ * // Unknown options will throw an error
2256
+ * MiniSearch.getDefault('notExisting')
2257
+ * // => throws 'MiniSearch: unknown option "notExisting"'
2258
+ * ```
2259
+ */
2260
+ static getDefault(optionName) {
2261
+ if (defaultOptions.hasOwnProperty(optionName)) {
2262
+ return getOwnProperty(defaultOptions, optionName);
2263
+ } else {
2264
+ throw new Error(`MiniSearch: unknown option "${optionName}"`);
2265
+ }
2266
+ }
2267
+ /**
2268
+ * @ignore
2269
+ */
2270
+ static loadJS(js, options) {
2271
+ const { index, documentIds, fieldLength, storedFields, serializationVersion } = js;
2272
+ const miniSearch = this.instantiateMiniSearch(js, options);
2273
+ miniSearch._documentIds = objectToNumericMap(documentIds);
2274
+ miniSearch._fieldLength = objectToNumericMap(fieldLength);
2275
+ miniSearch._storedFields = objectToNumericMap(storedFields);
2276
+ for (const [shortId, id] of miniSearch._documentIds) {
2277
+ miniSearch._idToShortId.set(id, shortId);
2278
+ }
2279
+ for (const [term, data] of index) {
2280
+ const dataMap = /* @__PURE__ */ new Map();
2281
+ for (const fieldId of Object.keys(data)) {
2282
+ let indexEntry = data[fieldId];
2283
+ if (serializationVersion === 1) {
2284
+ indexEntry = indexEntry.ds;
2285
+ }
2286
+ dataMap.set(parseInt(fieldId, 10), objectToNumericMap(indexEntry));
2287
+ }
2288
+ miniSearch._index.set(term, dataMap);
2289
+ }
2290
+ return miniSearch;
2291
+ }
2292
+ /**
2293
+ * @ignore
2294
+ */
2295
+ static async loadJSAsync(js, options) {
2296
+ const { index, documentIds, fieldLength, storedFields, serializationVersion } = js;
2297
+ const miniSearch = this.instantiateMiniSearch(js, options);
2298
+ miniSearch._documentIds = await objectToNumericMapAsync(documentIds);
2299
+ miniSearch._fieldLength = await objectToNumericMapAsync(fieldLength);
2300
+ miniSearch._storedFields = await objectToNumericMapAsync(storedFields);
2301
+ for (const [shortId, id] of miniSearch._documentIds) {
2302
+ miniSearch._idToShortId.set(id, shortId);
2303
+ }
2304
+ let count = 0;
2305
+ for (const [term, data] of index) {
2306
+ const dataMap = /* @__PURE__ */ new Map();
2307
+ for (const fieldId of Object.keys(data)) {
2308
+ let indexEntry = data[fieldId];
2309
+ if (serializationVersion === 1) {
2310
+ indexEntry = indexEntry.ds;
2311
+ }
2312
+ dataMap.set(parseInt(fieldId, 10), await objectToNumericMapAsync(indexEntry));
2313
+ }
2314
+ if (++count % 1e3 === 0)
2315
+ await wait(0);
2316
+ miniSearch._index.set(term, dataMap);
2317
+ }
2318
+ return miniSearch;
2319
+ }
2320
+ /**
2321
+ * @ignore
2322
+ */
2323
+ static instantiateMiniSearch(js, options) {
2324
+ const { documentCount, nextId, fieldIds, averageFieldLength, dirtCount, serializationVersion } = js;
2325
+ if (serializationVersion !== 1 && serializationVersion !== 2) {
2326
+ throw new Error("MiniSearch: cannot deserialize an index created with an incompatible version");
2327
+ }
2328
+ const miniSearch = new _MiniSearch(options);
2329
+ miniSearch._documentCount = documentCount;
2330
+ miniSearch._nextId = nextId;
2331
+ miniSearch._idToShortId = /* @__PURE__ */ new Map();
2332
+ miniSearch._fieldIds = fieldIds;
2333
+ miniSearch._avgFieldLength = averageFieldLength;
2334
+ miniSearch._dirtCount = dirtCount || 0;
2335
+ miniSearch._index = new SearchableMap();
2336
+ return miniSearch;
2337
+ }
2338
+ /**
2339
+ * @ignore
2340
+ */
2341
+ executeQuery(query, searchOptions = {}) {
2342
+ if (query === _MiniSearch.wildcard) {
2343
+ return this.executeWildcardQuery(searchOptions);
2344
+ }
2345
+ if (typeof query !== "string") {
2346
+ const options2 = { ...searchOptions, ...query, queries: void 0 };
2347
+ const results2 = query.queries.map((subquery) => this.executeQuery(subquery, options2));
2348
+ return this.combineResults(results2, options2.combineWith);
2349
+ }
2350
+ const { tokenize, processTerm, searchOptions: globalSearchOptions } = this._options;
2351
+ const options = { tokenize, processTerm, ...globalSearchOptions, ...searchOptions };
2352
+ const { tokenize: searchTokenize, processTerm: searchProcessTerm } = options;
2353
+ const terms = searchTokenize(query).flatMap((term) => searchProcessTerm(term)).filter((term) => !!term);
2354
+ const queries = terms.map(termToQuerySpec(options));
2355
+ const results = queries.map((query2) => this.executeQuerySpec(query2, options));
2356
+ return this.combineResults(results, options.combineWith);
2357
+ }
2358
+ /**
2359
+ * @ignore
2360
+ */
2361
+ executeQuerySpec(query, searchOptions) {
2362
+ const options = { ...this._options.searchOptions, ...searchOptions };
2363
+ const boosts = (options.fields || this._options.fields).reduce((boosts2, field) => ({ ...boosts2, [field]: getOwnProperty(options.boost, field) || 1 }), {});
2364
+ const { boostDocument, weights, maxFuzzy, bm25: bm25params } = options;
2365
+ const { fuzzy: fuzzyWeight, prefix: prefixWeight } = { ...defaultSearchOptions.weights, ...weights };
2366
+ const data = this._index.get(query.term);
2367
+ const results = this.termResults(query.term, query.term, 1, query.termBoost, data, boosts, boostDocument, bm25params);
2368
+ let prefixMatches;
2369
+ let fuzzyMatches;
2370
+ if (query.prefix) {
2371
+ prefixMatches = this._index.atPrefix(query.term);
2372
+ }
2373
+ if (query.fuzzy) {
2374
+ const fuzzy = query.fuzzy === true ? 0.2 : query.fuzzy;
2375
+ const maxDistance = fuzzy < 1 ? Math.min(maxFuzzy, Math.round(query.term.length * fuzzy)) : fuzzy;
2376
+ if (maxDistance)
2377
+ fuzzyMatches = this._index.fuzzyGet(query.term, maxDistance);
2378
+ }
2379
+ if (prefixMatches) {
2380
+ for (const [term, data2] of prefixMatches) {
2381
+ const distance = term.length - query.term.length;
2382
+ if (!distance) {
2383
+ continue;
2384
+ }
2385
+ fuzzyMatches === null || fuzzyMatches === void 0 ? void 0 : fuzzyMatches.delete(term);
2386
+ const weight = prefixWeight * term.length / (term.length + 0.3 * distance);
2387
+ this.termResults(query.term, term, weight, query.termBoost, data2, boosts, boostDocument, bm25params, results);
2388
+ }
2389
+ }
2390
+ if (fuzzyMatches) {
2391
+ for (const term of fuzzyMatches.keys()) {
2392
+ const [data2, distance] = fuzzyMatches.get(term);
2393
+ if (!distance) {
2394
+ continue;
2395
+ }
2396
+ const weight = fuzzyWeight * term.length / (term.length + distance);
2397
+ this.termResults(query.term, term, weight, query.termBoost, data2, boosts, boostDocument, bm25params, results);
2398
+ }
2399
+ }
2400
+ return results;
2401
+ }
2402
+ /**
2403
+ * @ignore
2404
+ */
2405
+ executeWildcardQuery(searchOptions) {
2406
+ const results = /* @__PURE__ */ new Map();
2407
+ const options = { ...this._options.searchOptions, ...searchOptions };
2408
+ for (const [shortId, id] of this._documentIds) {
2409
+ const score = options.boostDocument ? options.boostDocument(id, "", this._storedFields.get(shortId)) : 1;
2410
+ results.set(shortId, {
2411
+ score,
2412
+ terms: [],
2413
+ match: {}
2414
+ });
2415
+ }
2416
+ return results;
2417
+ }
2418
+ /**
2419
+ * @ignore
2420
+ */
2421
+ combineResults(results, combineWith = OR) {
2422
+ if (results.length === 0) {
2423
+ return /* @__PURE__ */ new Map();
2424
+ }
2425
+ const operator = combineWith.toLowerCase();
2426
+ const combinator = combinators[operator];
2427
+ if (!combinator) {
2428
+ throw new Error(`Invalid combination operator: ${combineWith}`);
2429
+ }
2430
+ return results.reduce(combinator) || /* @__PURE__ */ new Map();
2431
+ }
2432
+ /**
2433
+ * Allows serialization of the index to JSON, to possibly store it and later
2434
+ * deserialize it with {@link MiniSearch.loadJSON}.
2435
+ *
2436
+ * Normally one does not directly call this method, but rather call the
2437
+ * standard JavaScript `JSON.stringify()` passing the {@link MiniSearch}
2438
+ * instance, and JavaScript will internally call this method. Upon
2439
+ * deserialization, one must pass to {@link MiniSearch.loadJSON} the same
2440
+ * options used to create the original instance that was serialized.
2441
+ *
2442
+ * ### Usage:
2443
+ *
2444
+ * ```javascript
2445
+ * // Serialize the index:
2446
+ * let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
2447
+ * miniSearch.addAll(documents)
2448
+ * const json = JSON.stringify(miniSearch)
2449
+ *
2450
+ * // Later, to deserialize it:
2451
+ * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
2452
+ * ```
2453
+ *
2454
+ * @return A plain-object serializable representation of the search index.
2455
+ */
2456
+ toJSON() {
2457
+ const index = [];
2458
+ for (const [term, fieldIndex] of this._index) {
2459
+ const data = {};
2460
+ for (const [fieldId, freqs] of fieldIndex) {
2461
+ data[fieldId] = Object.fromEntries(freqs);
2462
+ }
2463
+ index.push([term, data]);
2464
+ }
2465
+ return {
2466
+ documentCount: this._documentCount,
2467
+ nextId: this._nextId,
2468
+ documentIds: Object.fromEntries(this._documentIds),
2469
+ fieldIds: this._fieldIds,
2470
+ fieldLength: Object.fromEntries(this._fieldLength),
2471
+ averageFieldLength: this._avgFieldLength,
2472
+ storedFields: Object.fromEntries(this._storedFields),
2473
+ dirtCount: this._dirtCount,
2474
+ index,
2475
+ serializationVersion: 2
2476
+ };
2477
+ }
2478
+ /**
2479
+ * @ignore
2480
+ */
2481
+ termResults(sourceTerm, derivedTerm, termWeight, termBoost, fieldTermData, fieldBoosts, boostDocumentFn, bm25params, results = /* @__PURE__ */ new Map()) {
2482
+ if (fieldTermData == null)
2483
+ return results;
2484
+ for (const field of Object.keys(fieldBoosts)) {
2485
+ const fieldBoost = fieldBoosts[field];
2486
+ const fieldId = this._fieldIds[field];
2487
+ const fieldTermFreqs = fieldTermData.get(fieldId);
2488
+ if (fieldTermFreqs == null)
2489
+ continue;
2490
+ let matchingFields = fieldTermFreqs.size;
2491
+ const avgFieldLength = this._avgFieldLength[fieldId];
2492
+ for (const docId of fieldTermFreqs.keys()) {
2493
+ if (!this._documentIds.has(docId)) {
2494
+ this.removeTerm(fieldId, docId, derivedTerm);
2495
+ matchingFields -= 1;
2496
+ continue;
2497
+ }
2498
+ const docBoost = boostDocumentFn ? boostDocumentFn(this._documentIds.get(docId), derivedTerm, this._storedFields.get(docId)) : 1;
2499
+ if (!docBoost)
2500
+ continue;
2501
+ const termFreq = fieldTermFreqs.get(docId);
2502
+ const fieldLength = this._fieldLength.get(docId)[fieldId];
2503
+ const rawScore = calcBM25Score(termFreq, matchingFields, this._documentCount, fieldLength, avgFieldLength, bm25params);
2504
+ const weightedScore = termWeight * termBoost * fieldBoost * docBoost * rawScore;
2505
+ const result = results.get(docId);
2506
+ if (result) {
2507
+ result.score += weightedScore;
2508
+ assignUniqueTerm(result.terms, sourceTerm);
2509
+ const match = getOwnProperty(result.match, derivedTerm);
2510
+ if (match) {
2511
+ match.push(field);
2512
+ } else {
2513
+ result.match[derivedTerm] = [field];
2514
+ }
2515
+ } else {
2516
+ results.set(docId, {
2517
+ score: weightedScore,
2518
+ terms: [sourceTerm],
2519
+ match: { [derivedTerm]: [field] }
2520
+ });
2521
+ }
2522
+ }
2523
+ }
2524
+ return results;
2525
+ }
2526
+ /**
2527
+ * @ignore
2528
+ */
2529
+ addTerm(fieldId, documentId, term) {
2530
+ const indexData = this._index.fetch(term, createMap);
2531
+ let fieldIndex = indexData.get(fieldId);
2532
+ if (fieldIndex == null) {
2533
+ fieldIndex = /* @__PURE__ */ new Map();
2534
+ fieldIndex.set(documentId, 1);
2535
+ indexData.set(fieldId, fieldIndex);
2536
+ } else {
2537
+ const docs = fieldIndex.get(documentId);
2538
+ fieldIndex.set(documentId, (docs || 0) + 1);
2539
+ }
2540
+ }
2541
+ /**
2542
+ * @ignore
2543
+ */
2544
+ removeTerm(fieldId, documentId, term) {
2545
+ if (!this._index.has(term)) {
2546
+ this.warnDocumentChanged(documentId, fieldId, term);
2547
+ return;
2548
+ }
2549
+ const indexData = this._index.fetch(term, createMap);
2550
+ const fieldIndex = indexData.get(fieldId);
2551
+ if (fieldIndex == null || fieldIndex.get(documentId) == null) {
2552
+ this.warnDocumentChanged(documentId, fieldId, term);
2553
+ } else if (fieldIndex.get(documentId) <= 1) {
2554
+ if (fieldIndex.size <= 1) {
2555
+ indexData.delete(fieldId);
2556
+ } else {
2557
+ fieldIndex.delete(documentId);
2558
+ }
2559
+ } else {
2560
+ fieldIndex.set(documentId, fieldIndex.get(documentId) - 1);
2561
+ }
2562
+ if (this._index.get(term).size === 0) {
2563
+ this._index.delete(term);
2564
+ }
2565
+ }
2566
+ /**
2567
+ * @ignore
2568
+ */
2569
+ warnDocumentChanged(shortDocumentId, fieldId, term) {
2570
+ for (const fieldName of Object.keys(this._fieldIds)) {
2571
+ if (this._fieldIds[fieldName] === fieldId) {
2572
+ 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");
2573
+ return;
2574
+ }
2575
+ }
2576
+ }
2577
+ /**
2578
+ * @ignore
2579
+ */
2580
+ addDocumentId(documentId) {
2581
+ const shortDocumentId = this._nextId;
2582
+ this._idToShortId.set(documentId, shortDocumentId);
2583
+ this._documentIds.set(shortDocumentId, documentId);
2584
+ this._documentCount += 1;
2585
+ this._nextId += 1;
2586
+ return shortDocumentId;
2587
+ }
2588
+ /**
2589
+ * @ignore
2590
+ */
2591
+ addFields(fields) {
2592
+ for (let i = 0; i < fields.length; i++) {
2593
+ this._fieldIds[fields[i]] = i;
2594
+ }
2595
+ }
2596
+ /**
2597
+ * @ignore
2598
+ */
2599
+ addFieldLength(documentId, fieldId, count, length) {
2600
+ let fieldLengths = this._fieldLength.get(documentId);
2601
+ if (fieldLengths == null)
2602
+ this._fieldLength.set(documentId, fieldLengths = []);
2603
+ fieldLengths[fieldId] = length;
2604
+ const averageFieldLength = this._avgFieldLength[fieldId] || 0;
2605
+ const totalFieldLength = averageFieldLength * count + length;
2606
+ this._avgFieldLength[fieldId] = totalFieldLength / (count + 1);
2607
+ }
2608
+ /**
2609
+ * @ignore
2610
+ */
2611
+ removeFieldLength(documentId, fieldId, count, length) {
2612
+ if (count === 1) {
2613
+ this._avgFieldLength[fieldId] = 0;
2614
+ return;
2615
+ }
2616
+ const totalFieldLength = this._avgFieldLength[fieldId] * count - length;
2617
+ this._avgFieldLength[fieldId] = totalFieldLength / (count - 1);
2618
+ }
2619
+ /**
2620
+ * @ignore
2621
+ */
2622
+ saveStoredFields(documentId, doc) {
2623
+ const { storeFields, extractField } = this._options;
2624
+ if (storeFields == null || storeFields.length === 0) {
2625
+ return;
2626
+ }
2627
+ let documentFields = this._storedFields.get(documentId);
2628
+ if (documentFields == null)
2629
+ this._storedFields.set(documentId, documentFields = {});
2630
+ for (const fieldName of storeFields) {
2631
+ const fieldValue = extractField(doc, fieldName);
2632
+ if (fieldValue !== void 0)
2633
+ documentFields[fieldName] = fieldValue;
2634
+ }
2635
+ }
2636
+ };
2637
+ MiniSearch.wildcard = /* @__PURE__ */ Symbol("*");
2638
+ var getOwnProperty = (object, property) => Object.prototype.hasOwnProperty.call(object, property) ? object[property] : void 0;
2639
+ var combinators = {
2640
+ [OR]: (a, b) => {
2641
+ for (const docId of b.keys()) {
2642
+ const existing = a.get(docId);
2643
+ if (existing == null) {
2644
+ a.set(docId, b.get(docId));
2645
+ } else {
2646
+ const { score, terms, match } = b.get(docId);
2647
+ existing.score = existing.score + score;
2648
+ existing.match = Object.assign(existing.match, match);
2649
+ assignUniqueTerms(existing.terms, terms);
2650
+ }
2651
+ }
2652
+ return a;
2653
+ },
2654
+ [AND]: (a, b) => {
2655
+ const combined = /* @__PURE__ */ new Map();
2656
+ for (const docId of b.keys()) {
2657
+ const existing = a.get(docId);
2658
+ if (existing == null)
2659
+ continue;
2660
+ const { score, terms, match } = b.get(docId);
2661
+ assignUniqueTerms(existing.terms, terms);
2662
+ combined.set(docId, {
2663
+ score: existing.score + score,
2664
+ terms: existing.terms,
2665
+ match: Object.assign(existing.match, match)
2666
+ });
2667
+ }
2668
+ return combined;
2669
+ },
2670
+ [AND_NOT]: (a, b) => {
2671
+ for (const docId of b.keys())
2672
+ a.delete(docId);
2673
+ return a;
2674
+ }
2675
+ };
2676
+ var defaultBM25params = { k: 1.2, b: 0.7, d: 0.5 };
2677
+ var calcBM25Score = (termFreq, matchingCount, totalCount, fieldLength, avgFieldLength, bm25params) => {
2678
+ const { k, b, d } = bm25params;
2679
+ const invDocFreq = Math.log(1 + (totalCount - matchingCount + 0.5) / (matchingCount + 0.5));
2680
+ return invDocFreq * (d + termFreq * (k + 1) / (termFreq + k * (1 - b + b * fieldLength / avgFieldLength)));
2681
+ };
2682
+ var termToQuerySpec = (options) => (term, i, terms) => {
2683
+ const fuzzy = typeof options.fuzzy === "function" ? options.fuzzy(term, i, terms) : options.fuzzy || false;
2684
+ const prefix = typeof options.prefix === "function" ? options.prefix(term, i, terms) : options.prefix === true;
2685
+ const termBoost = typeof options.boostTerm === "function" ? options.boostTerm(term, i, terms) : 1;
2686
+ return { term, fuzzy, prefix, termBoost };
2687
+ };
2688
+ var defaultOptions = {
2689
+ idField: "id",
2690
+ extractField: (document, fieldName) => document[fieldName],
2691
+ stringifyField: (fieldValue, fieldName) => fieldValue.toString(),
2692
+ tokenize: (text) => text.split(SPACE_OR_PUNCTUATION),
2693
+ processTerm: (term) => term.toLowerCase(),
2694
+ fields: void 0,
2695
+ searchOptions: void 0,
2696
+ storeFields: [],
2697
+ logger: (level, message) => {
2698
+ if (typeof (console === null || console === void 0 ? void 0 : console[level]) === "function")
2699
+ console[level](message);
2700
+ },
2701
+ autoVacuum: true
2702
+ };
2703
+ var defaultSearchOptions = {
2704
+ combineWith: OR,
2705
+ prefix: false,
2706
+ fuzzy: false,
2707
+ maxFuzzy: 6,
2708
+ boost: {},
2709
+ weights: { fuzzy: 0.45, prefix: 0.375 },
2710
+ bm25: defaultBM25params
2711
+ };
2712
+ var defaultAutoSuggestOptions = {
2713
+ combineWith: AND,
2714
+ prefix: (term, i, terms) => i === terms.length - 1
2715
+ };
2716
+ var defaultVacuumOptions = { batchSize: 1e3, batchWait: 10 };
2717
+ var defaultVacuumConditions = { minDirtFactor: 0.1, minDirtCount: 20 };
2718
+ var defaultAutoVacuumOptions = { ...defaultVacuumOptions, ...defaultVacuumConditions };
2719
+ var assignUniqueTerm = (target, term) => {
2720
+ if (!target.includes(term))
2721
+ target.push(term);
2722
+ };
2723
+ var assignUniqueTerms = (target, source) => {
2724
+ for (const term of source) {
2725
+ if (!target.includes(term))
2726
+ target.push(term);
2727
+ }
2728
+ };
2729
+ var byScore = ({ score: a }, { score: b }) => b - a;
2730
+ var createMap = () => /* @__PURE__ */ new Map();
2731
+ var objectToNumericMap = (object) => {
2732
+ const map = /* @__PURE__ */ new Map();
2733
+ for (const key of Object.keys(object)) {
2734
+ map.set(parseInt(key, 10), object[key]);
2735
+ }
2736
+ return map;
2737
+ };
2738
+ var objectToNumericMapAsync = async (object) => {
2739
+ const map = /* @__PURE__ */ new Map();
2740
+ let count = 0;
2741
+ for (const key of Object.keys(object)) {
2742
+ map.set(parseInt(key, 10), object[key]);
2743
+ if (++count % 1e3 === 0) {
2744
+ await wait(0);
2745
+ }
2746
+ }
2747
+ return map;
2748
+ };
2749
+ var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2750
+ var SPACE_OR_PUNCTUATION = /[\n\r\p{Z}\p{P}]+/u;
2751
+
2752
+ // src/graph/runtime/lexical.ts
2753
+ function createLexicalIndex(input) {
2754
+ const doc = typeof input === "string" ? JSON.parse(input) : input;
2755
+ const entries = Array.isArray(doc?.entries) ? doc.entries : [];
2756
+ const mini = new MiniSearch({
2757
+ fields: ["title", "labels", "description", "text"],
2758
+ storeFields: ["id"],
2759
+ idField: "id",
2760
+ // Titles and labels are the strongest signal that a node *is* the subject;
2761
+ // body text is evidence it merely mentions it.
2762
+ searchOptions: {
2763
+ boost: { title: 3, labels: 2, description: 1.5 },
2764
+ prefix: true,
2765
+ fuzzy: 0.2
2766
+ }
2767
+ });
2768
+ mini.addAll(entries);
2769
+ const byIri = new Map(entries.map((e) => [e.id, e]));
2770
+ return {
2771
+ size: () => entries.length,
2772
+ entry: (iri) => byIri.get(iri),
2773
+ search(query, options = {}) {
2774
+ const trimmed = query.trim();
2775
+ if (trimmed === "") return [];
2776
+ const hits = mini.search(trimmed);
2777
+ hits.sort(
2778
+ (a, b) => a.score === b.score ? byCodeUnit(String(a.id), String(b.id)) : b.score - a.score
2779
+ );
2780
+ return hits.slice(0, options.limit ?? 10).map((h) => ({ iri: String(h.id), score: h.score, via: "lexical" }));
2781
+ }
2782
+ };
2783
+ }
2784
+
2785
+ // src/graph/core/vector-index.ts
2786
+ var MAGIC = new Uint8Array([77, 71, 82, 86]);
2787
+ var FORMAT_VERSION = 2;
2788
+ var HEADER_OFFSET = 12;
2789
+ var VectorIndexError = class extends Error {
2790
+ constructor(message) {
2791
+ super(message);
2792
+ this.name = "VectorIndexError";
2793
+ }
2794
+ };
2795
+ function normalize(vector) {
2796
+ let sum = 0;
2797
+ for (const value of vector) sum += value * value;
2798
+ if (sum === 0) return vector;
2799
+ const inv = 1 / Math.sqrt(sum);
2800
+ vector.set(vector.map((value) => value * inv));
2801
+ return vector;
2802
+ }
2803
+ function encodeVectorIndex(entries, meta) {
2804
+ const sorted = [...entries].sort((a, b) => byCodeUnit(a.id, b.id));
2805
+ const dims = sorted[0]?.vector.length ?? 0;
2806
+ for (const entry of sorted) {
2807
+ if (entry.vector.length !== dims) {
2808
+ throw new VectorIndexError(
2809
+ `Inconsistent vector dimensions: ${entry.id} has ${entry.vector.length}, expected ${dims}.`
2810
+ );
2811
+ }
2812
+ }
2813
+ const header = {
2814
+ model: meta.model,
2815
+ dtype: meta.dtype,
2816
+ dims,
2817
+ count: sorted.length,
2818
+ ids: sorted.map((e) => e.id),
2819
+ source: meta.source,
2820
+ language: meta.language
2821
+ };
2822
+ const headerBytes = new TextEncoder().encode(JSON.stringify(header));
2823
+ const payloadBytes = sorted.length * dims * 4;
2824
+ const out = new Uint8Array(HEADER_OFFSET + headerBytes.length + payloadBytes);
2825
+ const view = new DataView(out.buffer);
2826
+ out.set(MAGIC, 0);
2827
+ view.setUint32(4, FORMAT_VERSION, true);
2828
+ view.setUint32(8, headerBytes.length, true);
2829
+ out.set(headerBytes, HEADER_OFFSET);
2830
+ let offset = HEADER_OFFSET + headerBytes.length;
2831
+ for (const entry of sorted) {
2832
+ const vector = normalize(Float32Array.from(entry.vector));
2833
+ for (const value of vector) {
2834
+ view.setFloat32(offset, value, true);
2835
+ offset += 4;
2836
+ }
2837
+ }
2838
+ return out;
2839
+ }
2840
+ function decodeVectorIndex(bytes) {
2841
+ if (bytes.length < HEADER_OFFSET) {
2842
+ throw new VectorIndexError("Not a manni graph vector index: file is too short.");
2843
+ }
2844
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
2845
+ if (!MAGIC.every((byte, i) => bytes[i] === byte)) {
2846
+ throw new VectorIndexError(
2847
+ "Not a manni graph vector index: bad magic \u2014 is this the right file?"
2848
+ );
2849
+ }
2850
+ const version = view.getUint32(4, true);
2851
+ if (version !== FORMAT_VERSION) {
2852
+ throw new VectorIndexError(
2853
+ `Unsupported vector index version ${version} (this build reads ${FORMAT_VERSION}) \u2014 re-run \`manni graph embed\`.`
2854
+ );
2855
+ }
2856
+ const headerLength = view.getUint32(8, true);
2857
+ if (HEADER_OFFSET + headerLength > bytes.length) {
2858
+ throw new VectorIndexError("Vector index is truncated: header overruns.");
2859
+ }
2860
+ let header;
2861
+ try {
2862
+ header = JSON.parse(
2863
+ new TextDecoder().decode(
2864
+ bytes.subarray(HEADER_OFFSET, HEADER_OFFSET + headerLength)
2865
+ )
2866
+ );
2867
+ } catch {
2868
+ throw new VectorIndexError("Vector index header is not valid JSON.");
2869
+ }
2870
+ if (!Array.isArray(header.ids) || typeof header.dims !== "number" || header.ids.length !== header.count) {
2871
+ throw new VectorIndexError("Vector index header is malformed.");
2872
+ }
2873
+ const start = HEADER_OFFSET + headerLength;
2874
+ const expected = header.count * header.dims * 4;
2875
+ if (bytes.length - start !== expected) {
2876
+ throw new VectorIndexError(
2877
+ `Vector index is truncated: expected ${expected} payload bytes, found ${bytes.length - start}.`
2878
+ );
2879
+ }
2880
+ const vectors = new Float32Array(header.count * header.dims);
2881
+ for (let i = 0; i < vectors.length; i++) {
2882
+ vectors[i] = view.getFloat32(start + i * 4, true);
2883
+ }
2884
+ return { header, vectors };
2885
+ }
2886
+
2887
+ // src/graph/runtime/vector.ts
2888
+ var VectorMismatchError = class extends Error {
2889
+ reason;
2890
+ constructor(mismatch) {
2891
+ super(mismatch.detail);
2892
+ this.name = "VectorMismatchError";
2893
+ this.reason = mismatch.reason;
2894
+ }
2895
+ };
2896
+ async function searchIndexDigest(raw) {
2897
+ const webCrypto = globalThis.crypto;
2898
+ const subtle = webCrypto?.subtle;
2899
+ if (!subtle) {
2900
+ throw new Error(
2901
+ "searchIndexDigest needs Web Crypto (globalThis.crypto.subtle), which is unavailable here \u2014 a page served over plain HTTP does not get it."
2902
+ );
2903
+ }
2904
+ const digest = await subtle.digest("SHA-256", new TextEncoder().encode(raw));
2905
+ const hex = [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
2906
+ return `sha256:${hex}`;
2907
+ }
2908
+ function createVectorIndex(bytes) {
2909
+ const { header, vectors } = decodeVectorIndex(bytes);
2910
+ const { dims, ids } = header;
2911
+ return {
2912
+ model: header.model,
2913
+ dtype: header.dtype,
2914
+ dims,
2915
+ source: header.source,
2916
+ size: () => ids.length,
2917
+ idAt: (row) => ids[row],
2918
+ check(expected) {
2919
+ if (expected.model !== void 0 && expected.model !== header.model) {
2920
+ return {
2921
+ reason: "model",
2922
+ detail: `Vector index was built with ${header.model}, but the embedder is ${expected.model}. Re-run \`manni graph embed\`.`
2923
+ };
2924
+ }
2925
+ if (expected.dtype !== void 0 && expected.dtype !== header.dtype) {
2926
+ return {
2927
+ reason: "dtype",
2928
+ detail: `Vector index was built at dtype ${header.dtype}, but the embedder is at ${expected.dtype} \u2014 different weights are a different function. Re-run \`manni graph embed\`.`
2929
+ };
2930
+ }
2931
+ if (expected.dims !== void 0 && expected.dims !== dims) {
2932
+ return {
2933
+ reason: "dims",
2934
+ detail: `Vector index has ${dims} dimensions, but the embedder produces ${expected.dims}.`
2935
+ };
2936
+ }
2937
+ if (expected.source !== void 0 && expected.source !== header.source) {
2938
+ return {
2939
+ reason: "stale-source",
2940
+ detail: "Vector index was built from a different search index \u2014 the corpus changed. Re-run `manni graph embed`."
2941
+ };
2942
+ }
2943
+ return void 0;
2944
+ },
2945
+ search(query, options = {}) {
2946
+ if (dims === 0 || ids.length === 0) return [];
2947
+ if (query.length !== dims) {
2948
+ throw new VectorMismatchError({
2949
+ reason: "dims",
2950
+ detail: `Query vector has ${query.length} dimensions, but the index has ${dims}.`
2951
+ });
2952
+ }
2953
+ const limit = normalizeLimit(options.limit);
2954
+ if (limit === 0) return [];
2955
+ const minScore = options.minScore ?? 0;
2956
+ const q = normalize(Float32Array.from(query));
2957
+ const scored = [];
2958
+ for (const [row, iri] of ids.entries()) {
2959
+ const stored = vectors.subarray(row * dims, (row + 1) * dims);
2960
+ let score = 0;
2961
+ for (const [i, value] of q.entries()) score += value * (stored[i] ?? 0);
2962
+ if (score >= minScore) {
2963
+ scored.push({ iri, score, via: "vector" });
2964
+ }
2965
+ }
2966
+ scored.sort(
2967
+ (a, b) => a.score === b.score ? byCodeUnit(a.iri, b.iri) : b.score - a.score
2968
+ );
2969
+ return scored.slice(0, limit);
2970
+ }
2971
+ };
2972
+ }
2973
+ function normalizeLimit(limit) {
2974
+ return typeof limit === "number" && Number.isInteger(limit) && limit >= 0 ? limit : 10;
2975
+ }
2976
+
2977
+ // src/graph/runtime/entry.ts
2978
+ function rrfMerge(rankings, k = 60) {
2979
+ const fused = /* @__PURE__ */ new Map();
2980
+ for (const ranking of rankings) {
2981
+ ranking.forEach((candidate, i) => {
2982
+ const current = fused.get(candidate.iri) ?? {
2983
+ score: 0,
2984
+ vias: /* @__PURE__ */ new Set()
2985
+ };
2986
+ current.score += 1 / (k + i + 1);
2987
+ current.vias.add(candidate.via);
2988
+ fused.set(candidate.iri, current);
2989
+ });
2990
+ }
2991
+ return [...fused.entries()].map(([iri, { score, vias }]) => {
2992
+ const [only] = vias;
2993
+ return {
2994
+ iri,
2995
+ score,
2996
+ via: vias.size === 1 && only !== void 0 ? only : "hybrid"
2997
+ };
2998
+ }).sort(
2999
+ (a, b) => a.score === b.score ? byCodeUnit(a.iri, b.iri) : b.score - a.score
3000
+ );
3001
+ }
3002
+ async function findEntry(query, options) {
3003
+ const trace = options.trace ?? createTrace();
3004
+ const limit = typeof options.limit === "number" && Number.isInteger(options.limit) && options.limit >= 0 ? options.limit : 10;
3005
+ const lexical = options.lexical.search(query, { limit });
3006
+ const embedder = options.embedder;
3007
+ const embed = embedder ? (q) => embedder.embed(q) : options.embedQuery;
3008
+ let vector = [];
3009
+ if (options.vectors && embed && query.trim() !== "") {
3010
+ const mismatch = options.vectors.check({
3011
+ ...options.embedder ? { model: options.embedder.model, dtype: options.embedder.dtype } : {},
3012
+ // Staleness is checked whichever way the query is embedded — it is a
3013
+ // property of the corpus, not of the embedder, so even the unverified
3014
+ // `embedQuery` path gets it when the caller supplies a digest.
3015
+ ...options.source === void 0 ? {} : { source: options.source }
3016
+ });
3017
+ if (mismatch) throw new VectorMismatchError(mismatch);
3018
+ const queryVector = await embed(query);
3019
+ vector = options.vectors.search(queryVector, { limit });
3020
+ }
3021
+ const rankings = vector.length > 0 ? [lexical, vector] : [lexical];
3022
+ const candidates = rrfMerge(rankings).slice(0, limit);
3023
+ for (const candidate of candidates) trace.entry.push(candidate);
3024
+ return { candidates, lexical, vector, trace };
3025
+ }
3026
+ export {
3027
+ GraphIndex,
3028
+ LOCALIZATIONS_FILENAME,
3029
+ UNDETERMINED,
3030
+ VectorIndexError,
3031
+ VectorMismatchError,
3032
+ assemble,
3033
+ createFetchResolver,
3034
+ createLexicalIndex,
3035
+ createTrace,
3036
+ createVectorIndex,
3037
+ decodeVectorIndex,
3038
+ defaultGraph,
3039
+ documentPreamble,
3040
+ documentSectionOrder,
3041
+ emitLocalizations,
3042
+ encodeVectorIndex,
3043
+ findEntry,
3044
+ impact,
3045
+ literal,
3046
+ matchQuads,
3047
+ namedNode,
3048
+ normalize,
3049
+ parseLocalizations,
3050
+ rdfjsQuads,
3051
+ reachedNodes,
3052
+ resolveSubject,
3053
+ resolveVariant,
3054
+ reverseReferences,
3055
+ rrfMerge,
3056
+ scopeExclusion,
3057
+ searchIndexDigest,
3058
+ searchIndexFilename,
3059
+ sectionOccurrence,
3060
+ sectionOccurrences,
3061
+ sectionOwnText,
3062
+ sliceSection,
3063
+ splitFragment,
3064
+ traverse,
3065
+ vectorIndexFilename
3066
+ };
3067
+ //# sourceMappingURL=runtime.js.map