@hawkeyexl/manni 4.16.0 → 4.17.1
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.
- package/README.md +1 -1
- package/dist/{batch-L5W4JS7A.js → batch-SAW4E46C.js} +14 -14
- package/dist/{build-GNZUVDZX.js → build-F6S3W77I.js} +15 -14
- package/dist/{calibrate-2LYIDN6U.js → calibrate-3DW2VV7F.js} +15 -15
- package/dist/{capture-OEDMFU5Z.js → capture-PKEYY2VO.js} +14 -14
- package/dist/{check-JR2TYL2Y.js → check-7PUIHLFM.js} +15 -15
- package/dist/{check-NLLFHNWG.js → check-EZN7YNVF.js} +14 -15
- package/dist/{check-Z5VGDSDT.js → check-LRX2YFDF.js} +13 -14
- package/dist/check-NW7GHSX6.js +47 -0
- package/dist/{cli-THEU2QG2.js → chunk-245W2Q7C.js} +133 -365
- package/dist/chunk-245W2Q7C.js.map +1 -0
- package/dist/{chunk-LEOQO3S6.js → chunk-2A5OIZHT.js} +7 -7
- package/dist/{chunk-FU3OCYYH.js → chunk-2LCJNH73.js} +2 -2
- package/dist/{chunk-7UC55CAU.js → chunk-2TLNAGVJ.js} +187 -159
- package/dist/chunk-2TLNAGVJ.js.map +1 -0
- package/dist/{chunk-6ANTGYNT.js → chunk-3446DW4W.js} +2 -2
- package/dist/{chunk-7GB3KEEM.js → chunk-3UYSONT4.js} +7 -453
- package/dist/chunk-3UYSONT4.js.map +1 -0
- package/dist/{chunk-NFJGIBES.js → chunk-4GGADKTU.js} +82 -238
- package/dist/chunk-4GGADKTU.js.map +1 -0
- package/dist/{chunk-YF6IZXAC.js → chunk-5QOTR53S.js} +7 -7
- package/dist/{chunk-2XRET3ZG.js → chunk-5VNFO2RT.js} +12 -38
- package/dist/chunk-5VNFO2RT.js.map +1 -0
- package/dist/{chunk-6YFEV7CM.js → chunk-64EDFJ5S.js} +5 -5
- package/dist/chunk-6J2ED4ED.js +545 -0
- package/dist/chunk-6J2ED4ED.js.map +1 -0
- package/dist/{chunk-2AUULNIJ.js → chunk-ABAFKOQF.js} +3 -3
- package/dist/{chunk-3GLDB7MZ.js → chunk-AIZBZYQM.js} +1311 -18
- package/dist/chunk-AIZBZYQM.js.map +1 -0
- package/dist/{chunk-GQQ2AX63.js → chunk-B5R5BWMY.js} +14 -429
- package/dist/chunk-B5R5BWMY.js.map +1 -0
- package/dist/{chunk-ZT5NUSZA.js → chunk-D3AO2PMV.js} +9 -9
- package/dist/{chunk-EZCJPF72.js → chunk-DSJTYRI5.js} +3 -3
- package/dist/{chunk-OJ7FEM2G.js → chunk-DU6CXY5R.js} +47 -10
- package/dist/chunk-DU6CXY5R.js.map +1 -0
- package/dist/{chunk-WD33LE5A.js → chunk-FAS3SRQB.js} +927 -99
- package/dist/chunk-FAS3SRQB.js.map +1 -0
- package/dist/{chunk-DTG6S2UG.js → chunk-FEURGKJC.js} +2 -2
- package/dist/{chunk-EA5KKIFG.js → chunk-FPPHEHPZ.js} +3 -3
- package/dist/chunk-HEVYEI6O.js +1338 -0
- package/dist/chunk-HEVYEI6O.js.map +1 -0
- package/dist/{chunk-5OOQ3HQ3.js → chunk-IGE6CIKZ.js} +11 -2
- package/dist/chunk-IGE6CIKZ.js.map +1 -0
- package/dist/{chunk-WT3AATHC.js → chunk-JK56UPNQ.js} +252 -425
- package/dist/chunk-JK56UPNQ.js.map +1 -0
- package/dist/{chunk-B3XXCDYW.js → chunk-KAWS7H4C.js} +2385 -2061
- package/dist/chunk-KAWS7H4C.js.map +1 -0
- package/dist/{chunk-O6YTTBSN.js → chunk-KB4MNUKC.js} +2 -2
- package/dist/{chunk-GR6QQ7GM.js → chunk-KCNBM42T.js} +4 -4
- package/dist/chunk-KUXAHHDE.js +314 -0
- package/dist/chunk-KUXAHHDE.js.map +1 -0
- package/dist/{chunk-EOSQENQF.js → chunk-LDIUA4ZG.js} +2 -2
- package/dist/{chunk-L3YGGYBG.js → chunk-M6DNOHJO.js} +3 -3
- package/dist/{chunk-4L3JBAW4.js → chunk-MBLVLQWI.js} +2 -2
- package/dist/{chunk-4HEEXPVX.js → chunk-MDXDTO7E.js} +6 -6
- package/dist/{chunk-WDENIUC7.js → chunk-MDZXRHIJ.js} +1 -1
- package/dist/chunk-MDZXRHIJ.js.map +1 -0
- package/dist/{chunk-IGRXQC3H.js → chunk-N25KDXOV.js} +2 -2
- package/dist/{chunk-DW22KCLN.js → chunk-NPZWOPOH.js} +3 -3
- package/dist/chunk-OZL5M764.js +443 -0
- package/dist/chunk-OZL5M764.js.map +1 -0
- package/dist/{chunk-NDBISART.js → chunk-PS4Y7EPP.js} +3 -3
- package/dist/{chunk-F4GXZMEO.js → chunk-Q7E43SXN.js} +12 -7
- package/dist/{chunk-F4GXZMEO.js.map → chunk-Q7E43SXN.js.map} +1 -1
- package/dist/{chunk-63GILHR5.js → chunk-QIKCIREY.js} +4 -4
- package/dist/{chunk-ZJZQPOJO.js → chunk-QIWSEKA6.js} +2 -2
- package/dist/{chunk-F6B2TNXU.js → chunk-TNYIXIYV.js} +6 -6
- package/dist/{chunk-CSLJMML4.js → chunk-TUMR35B3.js} +4 -4
- package/dist/{chunk-DSTRT6YD.js → chunk-U5NSIZRS.js} +3 -3
- package/dist/chunk-UP6NBQ62.js +2 -0
- package/dist/{chunk-BMYCLTFK.js → chunk-VRMQSP7O.js} +3 -3
- package/dist/{chunk-IM67NRUW.js → chunk-W7IEP7IG.js} +2 -2
- package/dist/{chunk-5PO2BWZF.js → chunk-W7XOMNYC.js} +2 -2
- package/dist/{chunk-6NTNLKNQ.js → chunk-WBXVQW57.js} +6 -5
- package/dist/{chunk-6NTNLKNQ.js.map → chunk-WBXVQW57.js.map} +1 -1
- package/dist/{chunk-PBZZUNDV.js → chunk-WP5EKWTU.js} +7 -7
- package/dist/chunk-WRSWZEVU.js +580 -0
- package/dist/chunk-WRSWZEVU.js.map +1 -0
- package/dist/{chunk-LPBVWUKS.js → chunk-WZSL7KF2.js} +2 -2
- package/dist/{chunk-GC47NBGO.js → chunk-XCEEJEVA.js} +4 -4
- package/dist/{chunk-4LLYPDNG.js → chunk-XPGK4ECA.js} +7 -7
- package/dist/{chunk-AWAXAJ6R.js → chunk-XRWQQHBR.js} +2 -2
- package/dist/{chunk-D4ANAOY7.js → chunk-XWUIFSWK.js} +20 -6
- package/dist/chunk-XWUIFSWK.js.map +1 -0
- package/dist/{chunk-IL62WATF.js → chunk-YJHHXM5B.js} +2 -2
- package/dist/{chunk-NH6B6DDI.js → chunk-ZEYNLKOH.js} +4 -4
- package/dist/{chunk-PHNAQ4QX.js → chunk-ZIXU7A4C.js} +21 -21
- package/dist/{cli-LZW6Z33F.js → cli-5ZQXVWWC.js} +18 -19
- package/dist/{cli-LZW6Z33F.js.map → cli-5ZQXVWWC.js.map} +1 -1
- package/dist/{cli-5QTSTGTX.js → cli-D5WYQVUF.js} +18 -18
- package/dist/{cli-4Y2XY52B.js → cli-FCGGNZ5C.js} +2 -2
- package/dist/{cli-DBMPGGOV.js → cli-FNX4LXMI.js} +37 -37
- package/dist/{cli-KAUXC7EZ.js → cli-LQRHK23G.js} +9 -9
- package/dist/{cli-RWRYV5FB.js → cli-LRKBCQRS.js} +9 -9
- package/dist/{cli-2EKRURBU.js → cli-NJC3H3CW.js} +15 -15
- package/dist/{cli-2SDBEUER.js → cli-OHKWCJIJ.js} +21 -21
- package/dist/{cli-BXUZ3KEQ.js → cli-RKYIZ6KU.js} +634 -36
- package/dist/cli-RKYIZ6KU.js.map +1 -0
- package/dist/cli-TJDQ5GUW.js +379 -0
- package/dist/cli-TJDQ5GUW.js.map +1 -0
- package/dist/{cli-TZ2OHF3H.js → cli-VC3BXCVR.js} +19 -19
- package/dist/cli.js +14 -14
- package/dist/docmeta.js +8 -8
- package/dist/{github-UR6LMUMW.js → github-6PV2JTZ7.js} +13 -14
- package/dist/github-7ZT4KYQU.js +35 -0
- package/dist/graph/runtime.d.ts +633 -217
- package/dist/graph/runtime.js +1001 -370
- package/dist/graph/runtime.js.map +1 -1
- package/dist/index.d.ts +2617 -1945
- package/dist/index.js +108 -64
- package/dist/index.js.map +1 -1
- package/dist/{json-QTYN2JQB.js → json-VWXLDTXR.js} +13 -14
- package/dist/{labels-UM2AFR5S.js → labels-ZJK27RL3.js} +12 -13
- package/dist/{labels-UM2AFR5S.js.map → labels-ZJK27RL3.js.map} +1 -1
- package/dist/{lint-LOFNZZPR.js → lint-5OERIERT.js} +13 -13
- package/dist/{list-GU7S6KEX.js → list-MANBRF7Z.js} +13 -14
- package/dist/{list-EPMHOU7T.js → list-SEEANTU6.js} +15 -15
- package/dist/{page-25CN3AFY.js → page-O4M65KMJ.js} +13 -14
- package/dist/{prepare-5GYMBF4P.js → prepare-3WULLZER.js} +13 -13
- package/dist/{pretty-QW6OSYRS.js → pretty-5FRBLTXU.js} +13 -14
- package/dist/{pretty-52Z74O6F.js → pretty-D3M366JD.js} +14 -15
- package/dist/{reporters-WT53GD4P.js → reporters-4WWZRHKI.js} +4 -4
- package/dist/{reporters-FSXJ42EB.js → reporters-6TEJDXXO.js} +4 -4
- package/dist/{reporters-3FXX2W4X.js → reporters-GDCZ5P7C.js} +12 -12
- package/dist/{run-H7XJMUWK.js → run-TZDLG6EN.js} +16 -16
- package/dist/sarif-PUZAMEFQ.js +36 -0
- package/dist/{sarif-C427U3NA.js → sarif-SSSPOUCK.js} +11 -11
- package/dist/site/rehype.d.ts +33 -0
- package/dist/site/rehype.js +49 -0
- package/dist/site/rehype.js.map +1 -0
- package/dist/{term-VG65JRNR.js → term-DHWMUL5N.js} +18 -19
- package/dist/{term-VG65JRNR.js.map → term-DHWMUL5N.js.map} +1 -1
- package/dist/{types-VMOT7RAL.js → types-7N7ETP3W.js} +2 -2
- package/dist/{validate-K36F54UB.js → validate-4AW7KIIY.js} +5 -5
- package/package.json +10 -1
- package/plugin/manni/.claude-plugin/plugin.json +1 -1
- package/shapes/graph/shapes-1.2.0.ttl +1161 -0
- package/dist/check-LVO4HZ3F.js +0 -36
- package/dist/chunk-2XRET3ZG.js.map +0 -1
- package/dist/chunk-3GLDB7MZ.js.map +0 -1
- package/dist/chunk-3WZQ2FR3.js +0 -236
- package/dist/chunk-3WZQ2FR3.js.map +0 -1
- package/dist/chunk-5OOQ3HQ3.js.map +0 -1
- package/dist/chunk-7GB3KEEM.js.map +0 -1
- package/dist/chunk-7UC55CAU.js.map +0 -1
- package/dist/chunk-B3XXCDYW.js.map +0 -1
- package/dist/chunk-D4ANAOY7.js.map +0 -1
- package/dist/chunk-GQQ2AX63.js.map +0 -1
- package/dist/chunk-IKEWBMCN.js +0 -98
- package/dist/chunk-IKEWBMCN.js.map +0 -1
- package/dist/chunk-NFJGIBES.js.map +0 -1
- package/dist/chunk-OJ7FEM2G.js.map +0 -1
- package/dist/chunk-WD33LE5A.js.map +0 -1
- package/dist/chunk-WDENIUC7.js.map +0 -1
- package/dist/chunk-WT3AATHC.js.map +0 -1
- package/dist/chunk-YUTVAMWW.js +0 -2
- package/dist/cli-BXUZ3KEQ.js.map +0 -1
- package/dist/cli-THEU2QG2.js.map +0 -1
- package/dist/github-ICZ5UDGX.js +0 -36
- package/dist/sarif-UVAFWZKS.js +0 -37
- /package/dist/{batch-L5W4JS7A.js.map → batch-SAW4E46C.js.map} +0 -0
- /package/dist/{build-GNZUVDZX.js.map → build-F6S3W77I.js.map} +0 -0
- /package/dist/{calibrate-2LYIDN6U.js.map → calibrate-3DW2VV7F.js.map} +0 -0
- /package/dist/{capture-OEDMFU5Z.js.map → capture-PKEYY2VO.js.map} +0 -0
- /package/dist/{check-JR2TYL2Y.js.map → check-7PUIHLFM.js.map} +0 -0
- /package/dist/{check-LVO4HZ3F.js.map → check-EZN7YNVF.js.map} +0 -0
- /package/dist/{check-NLLFHNWG.js.map → check-LRX2YFDF.js.map} +0 -0
- /package/dist/{check-Z5VGDSDT.js.map → check-NW7GHSX6.js.map} +0 -0
- /package/dist/{chunk-LEOQO3S6.js.map → chunk-2A5OIZHT.js.map} +0 -0
- /package/dist/{chunk-FU3OCYYH.js.map → chunk-2LCJNH73.js.map} +0 -0
- /package/dist/{chunk-6ANTGYNT.js.map → chunk-3446DW4W.js.map} +0 -0
- /package/dist/{chunk-YF6IZXAC.js.map → chunk-5QOTR53S.js.map} +0 -0
- /package/dist/{chunk-6YFEV7CM.js.map → chunk-64EDFJ5S.js.map} +0 -0
- /package/dist/{chunk-2AUULNIJ.js.map → chunk-ABAFKOQF.js.map} +0 -0
- /package/dist/{chunk-ZT5NUSZA.js.map → chunk-D3AO2PMV.js.map} +0 -0
- /package/dist/{chunk-EZCJPF72.js.map → chunk-DSJTYRI5.js.map} +0 -0
- /package/dist/{chunk-DTG6S2UG.js.map → chunk-FEURGKJC.js.map} +0 -0
- /package/dist/{chunk-EA5KKIFG.js.map → chunk-FPPHEHPZ.js.map} +0 -0
- /package/dist/{chunk-O6YTTBSN.js.map → chunk-KB4MNUKC.js.map} +0 -0
- /package/dist/{chunk-GR6QQ7GM.js.map → chunk-KCNBM42T.js.map} +0 -0
- /package/dist/{chunk-EOSQENQF.js.map → chunk-LDIUA4ZG.js.map} +0 -0
- /package/dist/{chunk-L3YGGYBG.js.map → chunk-M6DNOHJO.js.map} +0 -0
- /package/dist/{chunk-4L3JBAW4.js.map → chunk-MBLVLQWI.js.map} +0 -0
- /package/dist/{chunk-4HEEXPVX.js.map → chunk-MDXDTO7E.js.map} +0 -0
- /package/dist/{chunk-IGRXQC3H.js.map → chunk-N25KDXOV.js.map} +0 -0
- /package/dist/{chunk-DW22KCLN.js.map → chunk-NPZWOPOH.js.map} +0 -0
- /package/dist/{chunk-NDBISART.js.map → chunk-PS4Y7EPP.js.map} +0 -0
- /package/dist/{chunk-63GILHR5.js.map → chunk-QIKCIREY.js.map} +0 -0
- /package/dist/{chunk-ZJZQPOJO.js.map → chunk-QIWSEKA6.js.map} +0 -0
- /package/dist/{chunk-F6B2TNXU.js.map → chunk-TNYIXIYV.js.map} +0 -0
- /package/dist/{chunk-CSLJMML4.js.map → chunk-TUMR35B3.js.map} +0 -0
- /package/dist/{chunk-DSTRT6YD.js.map → chunk-U5NSIZRS.js.map} +0 -0
- /package/dist/{chunk-YUTVAMWW.js.map → chunk-UP6NBQ62.js.map} +0 -0
- /package/dist/{chunk-BMYCLTFK.js.map → chunk-VRMQSP7O.js.map} +0 -0
- /package/dist/{chunk-IM67NRUW.js.map → chunk-W7IEP7IG.js.map} +0 -0
- /package/dist/{chunk-5PO2BWZF.js.map → chunk-W7XOMNYC.js.map} +0 -0
- /package/dist/{chunk-PBZZUNDV.js.map → chunk-WP5EKWTU.js.map} +0 -0
- /package/dist/{chunk-LPBVWUKS.js.map → chunk-WZSL7KF2.js.map} +0 -0
- /package/dist/{chunk-GC47NBGO.js.map → chunk-XCEEJEVA.js.map} +0 -0
- /package/dist/{chunk-4LLYPDNG.js.map → chunk-XPGK4ECA.js.map} +0 -0
- /package/dist/{chunk-AWAXAJ6R.js.map → chunk-XRWQQHBR.js.map} +0 -0
- /package/dist/{chunk-IL62WATF.js.map → chunk-YJHHXM5B.js.map} +0 -0
- /package/dist/{chunk-NH6B6DDI.js.map → chunk-ZEYNLKOH.js.map} +0 -0
- /package/dist/{chunk-PHNAQ4QX.js.map → chunk-ZIXU7A4C.js.map} +0 -0
- /package/dist/{cli-5QTSTGTX.js.map → cli-D5WYQVUF.js.map} +0 -0
- /package/dist/{cli-4Y2XY52B.js.map → cli-FCGGNZ5C.js.map} +0 -0
- /package/dist/{cli-DBMPGGOV.js.map → cli-FNX4LXMI.js.map} +0 -0
- /package/dist/{cli-KAUXC7EZ.js.map → cli-LQRHK23G.js.map} +0 -0
- /package/dist/{cli-RWRYV5FB.js.map → cli-LRKBCQRS.js.map} +0 -0
- /package/dist/{cli-2EKRURBU.js.map → cli-NJC3H3CW.js.map} +0 -0
- /package/dist/{cli-2SDBEUER.js.map → cli-OHKWCJIJ.js.map} +0 -0
- /package/dist/{cli-TZ2OHF3H.js.map → cli-VC3BXCVR.js.map} +0 -0
- /package/dist/{github-ICZ5UDGX.js.map → github-6PV2JTZ7.js.map} +0 -0
- /package/dist/{github-UR6LMUMW.js.map → github-7ZT4KYQU.js.map} +0 -0
- /package/dist/{json-QTYN2JQB.js.map → json-VWXLDTXR.js.map} +0 -0
- /package/dist/{lint-LOFNZZPR.js.map → lint-5OERIERT.js.map} +0 -0
- /package/dist/{list-EPMHOU7T.js.map → list-MANBRF7Z.js.map} +0 -0
- /package/dist/{list-GU7S6KEX.js.map → list-SEEANTU6.js.map} +0 -0
- /package/dist/{page-25CN3AFY.js.map → page-O4M65KMJ.js.map} +0 -0
- /package/dist/{prepare-5GYMBF4P.js.map → prepare-3WULLZER.js.map} +0 -0
- /package/dist/{pretty-52Z74O6F.js.map → pretty-5FRBLTXU.js.map} +0 -0
- /package/dist/{pretty-QW6OSYRS.js.map → pretty-D3M366JD.js.map} +0 -0
- /package/dist/{reporters-3FXX2W4X.js.map → reporters-4WWZRHKI.js.map} +0 -0
- /package/dist/{reporters-FSXJ42EB.js.map → reporters-6TEJDXXO.js.map} +0 -0
- /package/dist/{reporters-WT53GD4P.js.map → reporters-GDCZ5P7C.js.map} +0 -0
- /package/dist/{run-H7XJMUWK.js.map → run-TZDLG6EN.js.map} +0 -0
- /package/dist/{sarif-C427U3NA.js.map → sarif-PUZAMEFQ.js.map} +0 -0
- /package/dist/{sarif-UVAFWZKS.js.map → sarif-SSSPOUCK.js.map} +0 -0
- /package/dist/{types-VMOT7RAL.js.map → types-7N7ETP3W.js.map} +0 -0
- /package/dist/{validate-K36F54UB.js.map → validate-4AW7KIIY.js.map} +0 -0
|
@@ -0,0 +1,1338 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
GRAPH_NOT_APPLICABLE_TO_VARIANT,
|
|
4
|
+
GRAPH_NOT_SOFTWARE_SUBJECT,
|
|
5
|
+
IIRDS_HAS_SUBJECT,
|
|
6
|
+
IIRDS_RELATES_TO_PRODUCT_VARIANT,
|
|
7
|
+
NS,
|
|
8
|
+
RDF_TYPE,
|
|
9
|
+
SOFTWARE_SUBJECT_IRIS,
|
|
10
|
+
byCodeUnit,
|
|
11
|
+
canonicalLanguageTag,
|
|
12
|
+
parseLocalizations
|
|
13
|
+
} from "./chunk-4GGADKTU.js";
|
|
14
|
+
|
|
15
|
+
// src/graph/core/vector-index.ts
|
|
16
|
+
var MAGIC = new Uint8Array([77, 71, 82, 86]);
|
|
17
|
+
var FORMAT_VERSION = 2;
|
|
18
|
+
var HEADER_OFFSET = 12;
|
|
19
|
+
var VectorIndexError = class extends Error {
|
|
20
|
+
constructor(message) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = "VectorIndexError";
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
function normalize(vector) {
|
|
26
|
+
let sum = 0;
|
|
27
|
+
for (const value of vector) sum += value * value;
|
|
28
|
+
if (sum === 0) return vector;
|
|
29
|
+
const inv = 1 / Math.sqrt(sum);
|
|
30
|
+
vector.set(vector.map((value) => value * inv));
|
|
31
|
+
return vector;
|
|
32
|
+
}
|
|
33
|
+
function encodeVectorIndex(entries, meta) {
|
|
34
|
+
const sorted = [...entries].sort((a, b) => byCodeUnit(a.id, b.id));
|
|
35
|
+
const dims = sorted[0]?.vector.length ?? 0;
|
|
36
|
+
for (const entry of sorted) {
|
|
37
|
+
if (entry.vector.length !== dims) {
|
|
38
|
+
throw new VectorIndexError(
|
|
39
|
+
`Inconsistent vector dimensions: ${entry.id} has ${entry.vector.length}, expected ${dims}.`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const header = {
|
|
44
|
+
model: meta.model,
|
|
45
|
+
dtype: meta.dtype,
|
|
46
|
+
dims,
|
|
47
|
+
count: sorted.length,
|
|
48
|
+
ids: sorted.map((e) => e.id),
|
|
49
|
+
source: meta.source,
|
|
50
|
+
language: meta.language
|
|
51
|
+
};
|
|
52
|
+
const headerBytes = new TextEncoder().encode(JSON.stringify(header));
|
|
53
|
+
const payloadBytes = sorted.length * dims * 4;
|
|
54
|
+
const out = new Uint8Array(HEADER_OFFSET + headerBytes.length + payloadBytes);
|
|
55
|
+
const view = new DataView(out.buffer);
|
|
56
|
+
out.set(MAGIC, 0);
|
|
57
|
+
view.setUint32(4, FORMAT_VERSION, true);
|
|
58
|
+
view.setUint32(8, headerBytes.length, true);
|
|
59
|
+
out.set(headerBytes, HEADER_OFFSET);
|
|
60
|
+
let offset = HEADER_OFFSET + headerBytes.length;
|
|
61
|
+
for (const entry of sorted) {
|
|
62
|
+
const vector = normalize(Float32Array.from(entry.vector));
|
|
63
|
+
for (const value of vector) {
|
|
64
|
+
view.setFloat32(offset, value, true);
|
|
65
|
+
offset += 4;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
function decodeVectorIndex(bytes) {
|
|
71
|
+
if (bytes.length < HEADER_OFFSET) {
|
|
72
|
+
throw new VectorIndexError("Not a manni graph vector index: file is too short.");
|
|
73
|
+
}
|
|
74
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
75
|
+
if (!MAGIC.every((byte, i) => bytes[i] === byte)) {
|
|
76
|
+
throw new VectorIndexError(
|
|
77
|
+
"Not a manni graph vector index: bad magic \u2014 is this the right file?"
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const version = view.getUint32(4, true);
|
|
81
|
+
if (version !== FORMAT_VERSION) {
|
|
82
|
+
throw new VectorIndexError(
|
|
83
|
+
`Unsupported vector index version ${version} (this build reads ${FORMAT_VERSION}) \u2014 re-run \`manni graph embed\`.`
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const headerLength = view.getUint32(8, true);
|
|
87
|
+
if (HEADER_OFFSET + headerLength > bytes.length) {
|
|
88
|
+
throw new VectorIndexError("Vector index is truncated: header overruns.");
|
|
89
|
+
}
|
|
90
|
+
let header;
|
|
91
|
+
try {
|
|
92
|
+
header = JSON.parse(
|
|
93
|
+
new TextDecoder().decode(
|
|
94
|
+
bytes.subarray(HEADER_OFFSET, HEADER_OFFSET + headerLength)
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
} catch {
|
|
98
|
+
throw new VectorIndexError("Vector index header is not valid JSON.");
|
|
99
|
+
}
|
|
100
|
+
if (!Array.isArray(header.ids) || typeof header.dims !== "number" || header.ids.length !== header.count) {
|
|
101
|
+
throw new VectorIndexError("Vector index header is malformed.");
|
|
102
|
+
}
|
|
103
|
+
const start = HEADER_OFFSET + headerLength;
|
|
104
|
+
const expected = header.count * header.dims * 4;
|
|
105
|
+
if (bytes.length - start !== expected) {
|
|
106
|
+
throw new VectorIndexError(
|
|
107
|
+
`Vector index is truncated: expected ${expected} payload bytes, found ${bytes.length - start}.`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
const vectors = new Float32Array(header.count * header.dims);
|
|
111
|
+
for (let i = 0; i < vectors.length; i++) {
|
|
112
|
+
vectors[i] = view.getFloat32(start + i * 4, true);
|
|
113
|
+
}
|
|
114
|
+
return { header, vectors };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/graph/runtime/chunks.ts
|
|
118
|
+
function range(spec) {
|
|
119
|
+
const m = /^(\d+)(?:-(\d+))?$/.exec(spec.trim());
|
|
120
|
+
if (m?.[1] === void 0) return void 0;
|
|
121
|
+
const start = Number(m[1]);
|
|
122
|
+
const end = m[2] === void 0 ? start : Number(m[2]);
|
|
123
|
+
return end < start ? void 0 : { start, end };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/graph/runtime/graph.ts
|
|
127
|
+
var XSD_STRING = "http://www.w3.org/2001/XMLSchema#string";
|
|
128
|
+
function compareValues(a, b) {
|
|
129
|
+
if (a.kind !== b.kind) return a.kind === "iri" ? -1 : 1;
|
|
130
|
+
const v = byCodeUnit(a.value, b.value);
|
|
131
|
+
if (v !== 0) return v;
|
|
132
|
+
const da = a.kind === "literal" ? a.datatype ?? "" : "";
|
|
133
|
+
const db = b.kind === "literal" ? b.datatype ?? "" : "";
|
|
134
|
+
const la = a.kind === "literal" ? a.language ?? "" : "";
|
|
135
|
+
const lb = b.kind === "literal" ? b.language ?? "" : "";
|
|
136
|
+
return byCodeUnit(da, db) || byCodeUnit(la, lb);
|
|
137
|
+
}
|
|
138
|
+
var GraphIndex = class _GraphIndex {
|
|
139
|
+
nodes = /* @__PURE__ */ new Map();
|
|
140
|
+
/** subject → predicate → object IRIs (sorted). */
|
|
141
|
+
outbound = /* @__PURE__ */ new Map();
|
|
142
|
+
/** object → predicate → subject IRIs (sorted). */
|
|
143
|
+
inbound = /* @__PURE__ */ new Map();
|
|
144
|
+
/** class IRI → instance IRIs (sorted). */
|
|
145
|
+
byType = /* @__PURE__ */ new Map();
|
|
146
|
+
/** prefix → namespace, from the artifact's own `@context`. */
|
|
147
|
+
context;
|
|
148
|
+
constructor(context) {
|
|
149
|
+
this.context = context;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Build from a `graph.jsonld` document — a JSON string or the parsed object.
|
|
153
|
+
* CURIE keys and `@type` values are expanded using the document's own
|
|
154
|
+
* `@context`, so the index is self-describing.
|
|
155
|
+
*/
|
|
156
|
+
static fromJsonLd(input) {
|
|
157
|
+
const doc = typeof input === "string" ? JSON.parse(input) : input;
|
|
158
|
+
const context = doc["@context"] ?? {};
|
|
159
|
+
const index = new _GraphIndex({ ...context });
|
|
160
|
+
for (const raw of doc["@graph"] ?? []) {
|
|
161
|
+
index.addJsonLdNode(raw);
|
|
162
|
+
}
|
|
163
|
+
index.finalize();
|
|
164
|
+
return index;
|
|
165
|
+
}
|
|
166
|
+
/** Build from dockg's internal quad shape (the Node/CLI path). */
|
|
167
|
+
static fromQuads(quads, context = {}) {
|
|
168
|
+
const index = new _GraphIndex({ ...context });
|
|
169
|
+
for (const q of quads) {
|
|
170
|
+
const o = q.o;
|
|
171
|
+
const value = o.kind === "iri" ? { kind: "iri", value: o.value } : o.language ? { kind: "literal", value: o.value, language: o.language } : o.datatype && o.datatype !== XSD_STRING ? { kind: "literal", value: o.value, datatype: o.datatype } : { kind: "literal", value: o.value };
|
|
172
|
+
index.addTriple(q.s, q.p, value);
|
|
173
|
+
}
|
|
174
|
+
index.finalize();
|
|
175
|
+
return index;
|
|
176
|
+
}
|
|
177
|
+
/** Expand a CURIE (`dcterms:title`) to a full IRI using `@context`. */
|
|
178
|
+
expand(term) {
|
|
179
|
+
const colon = term.indexOf(":");
|
|
180
|
+
if (colon <= 0) return term;
|
|
181
|
+
const ns = this.context[term.slice(0, colon)];
|
|
182
|
+
return ns ? `${ns}${term.slice(colon + 1)}` : term;
|
|
183
|
+
}
|
|
184
|
+
/** Compact a full IRI to a CURIE when a context namespace matches. */
|
|
185
|
+
compact(iri) {
|
|
186
|
+
let best = iri;
|
|
187
|
+
let bestNs = "";
|
|
188
|
+
for (const [prefix, ns] of Object.entries(this.context)) {
|
|
189
|
+
if (iri.startsWith(ns) && iri.length > ns.length && ns.length > bestNs.length) {
|
|
190
|
+
best = `${prefix}:${iri.slice(ns.length)}`;
|
|
191
|
+
bestNs = ns;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return best;
|
|
195
|
+
}
|
|
196
|
+
addJsonLdNode(raw) {
|
|
197
|
+
const id = typeof raw["@id"] === "string" ? this.expand(raw["@id"]) : "";
|
|
198
|
+
if (!id) return;
|
|
199
|
+
this.ensureNode(id);
|
|
200
|
+
const rawTypes = raw["@type"];
|
|
201
|
+
for (const t of toArray(rawTypes)) {
|
|
202
|
+
if (typeof t === "string") {
|
|
203
|
+
this.addTriple(id, RDF_TYPE, { kind: "iri", value: this.expand(t) });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
for (const [key, rawValue] of Object.entries(raw)) {
|
|
207
|
+
if (key === "@id" || key === "@type") continue;
|
|
208
|
+
const predicate = this.expand(key);
|
|
209
|
+
for (const v of toArray(rawValue)) {
|
|
210
|
+
const value = this.toValue(v);
|
|
211
|
+
if (value) this.addTriple(id, predicate, value);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
toValue(v) {
|
|
216
|
+
if (typeof v === "string") return { kind: "literal", value: v };
|
|
217
|
+
if (typeof v === "number" || typeof v === "boolean") {
|
|
218
|
+
return { kind: "literal", value: String(v) };
|
|
219
|
+
}
|
|
220
|
+
if (v && typeof v === "object") {
|
|
221
|
+
const o = v;
|
|
222
|
+
if (typeof o["@id"] === "string") {
|
|
223
|
+
return { kind: "iri", value: this.expand(o["@id"]) };
|
|
224
|
+
}
|
|
225
|
+
if (o["@value"] !== void 0) {
|
|
226
|
+
const raw = o["@value"];
|
|
227
|
+
const lexical = typeof raw === "string" ? raw : JSON.stringify(raw);
|
|
228
|
+
if (typeof o["@language"] === "string") {
|
|
229
|
+
return { kind: "literal", value: lexical, language: o["@language"] };
|
|
230
|
+
}
|
|
231
|
+
const dt = typeof o["@type"] === "string" ? this.expand(o["@type"]) : void 0;
|
|
232
|
+
return dt && dt !== XSD_STRING ? { kind: "literal", value: lexical, datatype: dt } : { kind: "literal", value: lexical };
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return void 0;
|
|
236
|
+
}
|
|
237
|
+
ensureNode(id) {
|
|
238
|
+
let node = this.nodes.get(id);
|
|
239
|
+
if (!node) {
|
|
240
|
+
node = { id, types: [], props: /* @__PURE__ */ new Map() };
|
|
241
|
+
this.nodes.set(id, node);
|
|
242
|
+
}
|
|
243
|
+
return node;
|
|
244
|
+
}
|
|
245
|
+
addTriple(s, p, o) {
|
|
246
|
+
const node = this.ensureNode(s);
|
|
247
|
+
let values = node.props.get(p);
|
|
248
|
+
if (!values) {
|
|
249
|
+
values = [];
|
|
250
|
+
node.props.set(p, values);
|
|
251
|
+
}
|
|
252
|
+
values.push(o);
|
|
253
|
+
if (p === RDF_TYPE && o.kind === "iri") {
|
|
254
|
+
node.types.push(o.value);
|
|
255
|
+
const instances = this.byType.get(o.value);
|
|
256
|
+
if (instances) instances.push(s);
|
|
257
|
+
else this.byType.set(o.value, [s]);
|
|
258
|
+
}
|
|
259
|
+
if (o.kind === "iri") {
|
|
260
|
+
addEdge(this.outbound, s, p, o.value);
|
|
261
|
+
addEdge(this.inbound, o.value, p, s);
|
|
262
|
+
this.ensureNode(o.value);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/** Sort every collection so traversal order is deterministic. */
|
|
266
|
+
finalize() {
|
|
267
|
+
for (const node of this.nodes.values()) {
|
|
268
|
+
node.types.sort(byCodeUnit);
|
|
269
|
+
for (const values of node.props.values()) values.sort(compareValues);
|
|
270
|
+
}
|
|
271
|
+
for (const instances of this.byType.values()) instances.sort(byCodeUnit);
|
|
272
|
+
for (const map of [this.outbound, this.inbound]) {
|
|
273
|
+
for (const preds of map.values()) {
|
|
274
|
+
for (const targets of preds.values()) targets.sort(byCodeUnit);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/** Every node IRI, sorted. */
|
|
279
|
+
ids() {
|
|
280
|
+
return [...this.nodes.keys()].sort(byCodeUnit);
|
|
281
|
+
}
|
|
282
|
+
size() {
|
|
283
|
+
return this.nodes.size;
|
|
284
|
+
}
|
|
285
|
+
node(iri) {
|
|
286
|
+
return this.nodes.get(iri);
|
|
287
|
+
}
|
|
288
|
+
has(iri) {
|
|
289
|
+
return this.nodes.has(iri);
|
|
290
|
+
}
|
|
291
|
+
/** Class IRIs of a node, sorted. */
|
|
292
|
+
types(iri) {
|
|
293
|
+
return this.nodes.get(iri)?.types ?? [];
|
|
294
|
+
}
|
|
295
|
+
/** Instances of a class, sorted. */
|
|
296
|
+
instancesOf(classIri) {
|
|
297
|
+
return this.byType.get(classIri) ?? [];
|
|
298
|
+
}
|
|
299
|
+
/** All values for a predicate, sorted. */
|
|
300
|
+
values(iri, predicate) {
|
|
301
|
+
return this.nodes.get(iri)?.props.get(predicate) ?? [];
|
|
302
|
+
}
|
|
303
|
+
/** Literal lexical forms for a predicate, sorted. */
|
|
304
|
+
literals(iri, predicate) {
|
|
305
|
+
return this.values(iri, predicate).filter((v) => v.kind === "literal").map((v) => v.value);
|
|
306
|
+
}
|
|
307
|
+
/** The first literal for a predicate, if any. */
|
|
308
|
+
literal(iri, predicate) {
|
|
309
|
+
return this.literals(iri, predicate)[0];
|
|
310
|
+
}
|
|
311
|
+
/** Outbound IRI targets: all predicates, or one. Sorted. */
|
|
312
|
+
out(iri, predicate) {
|
|
313
|
+
return edges(this.outbound, iri, predicate);
|
|
314
|
+
}
|
|
315
|
+
/** Inbound IRI sources (who points at this node). Sorted. */
|
|
316
|
+
in(iri, predicate) {
|
|
317
|
+
return edges(this.inbound, iri, predicate);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
function addEdge(map, key, predicate, target) {
|
|
321
|
+
let preds = map.get(key);
|
|
322
|
+
if (!preds) {
|
|
323
|
+
preds = /* @__PURE__ */ new Map();
|
|
324
|
+
map.set(key, preds);
|
|
325
|
+
}
|
|
326
|
+
const targets = preds.get(predicate);
|
|
327
|
+
if (targets) targets.push(target);
|
|
328
|
+
else preds.set(predicate, [target]);
|
|
329
|
+
}
|
|
330
|
+
function edges(map, iri, predicate) {
|
|
331
|
+
const preds = map.get(iri);
|
|
332
|
+
if (!preds) return [];
|
|
333
|
+
const out = [];
|
|
334
|
+
const keys = predicate ? [predicate] : [...preds.keys()].sort(byCodeUnit);
|
|
335
|
+
for (const p of keys) {
|
|
336
|
+
for (const target of preds.get(p) ?? []) out.push({ predicate: p, target });
|
|
337
|
+
}
|
|
338
|
+
return out;
|
|
339
|
+
}
|
|
340
|
+
function toArray(v) {
|
|
341
|
+
if (v === void 0 || v === null) return [];
|
|
342
|
+
return Array.isArray(v) ? v : [v];
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// src/graph/runtime/vector.ts
|
|
346
|
+
var VectorMismatchError = class extends Error {
|
|
347
|
+
reason;
|
|
348
|
+
constructor(mismatch) {
|
|
349
|
+
super(mismatch.detail);
|
|
350
|
+
this.name = "VectorMismatchError";
|
|
351
|
+
this.reason = mismatch.reason;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
async function searchIndexDigest(raw) {
|
|
355
|
+
const webCrypto = globalThis.crypto;
|
|
356
|
+
const subtle = webCrypto?.subtle;
|
|
357
|
+
if (!subtle) {
|
|
358
|
+
throw new Error(
|
|
359
|
+
"searchIndexDigest needs Web Crypto (globalThis.crypto.subtle), which is unavailable here \u2014 a page served over plain HTTP does not get it."
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
const digest = await subtle.digest("SHA-256", new TextEncoder().encode(raw));
|
|
363
|
+
const hex = [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
364
|
+
return `sha256:${hex}`;
|
|
365
|
+
}
|
|
366
|
+
function createVectorIndex(bytes) {
|
|
367
|
+
const { header, vectors } = decodeVectorIndex(bytes);
|
|
368
|
+
const { dims, ids } = header;
|
|
369
|
+
return {
|
|
370
|
+
model: header.model,
|
|
371
|
+
dtype: header.dtype,
|
|
372
|
+
dims,
|
|
373
|
+
source: header.source,
|
|
374
|
+
size: () => ids.length,
|
|
375
|
+
idAt: (row) => ids[row],
|
|
376
|
+
check(expected) {
|
|
377
|
+
if (expected.model !== void 0 && expected.model !== header.model) {
|
|
378
|
+
return {
|
|
379
|
+
reason: "model",
|
|
380
|
+
detail: `Vector index was built with ${header.model}, but the embedder is ${expected.model}. Re-run \`manni graph embed\`.`
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
if (expected.dtype !== void 0 && expected.dtype !== header.dtype) {
|
|
384
|
+
return {
|
|
385
|
+
reason: "dtype",
|
|
386
|
+
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\`.`
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
if (expected.dims !== void 0 && expected.dims !== dims) {
|
|
390
|
+
return {
|
|
391
|
+
reason: "dims",
|
|
392
|
+
detail: `Vector index has ${dims} dimensions, but the embedder produces ${expected.dims}.`
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
if (expected.source !== void 0 && expected.source !== header.source) {
|
|
396
|
+
return {
|
|
397
|
+
reason: "stale-source",
|
|
398
|
+
detail: "Vector index was built from a different search index \u2014 the corpus changed. Re-run `manni graph embed`."
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
return void 0;
|
|
402
|
+
},
|
|
403
|
+
search(query, options = {}) {
|
|
404
|
+
if (dims === 0 || ids.length === 0) return [];
|
|
405
|
+
if (query.length !== dims) {
|
|
406
|
+
throw new VectorMismatchError({
|
|
407
|
+
reason: "dims",
|
|
408
|
+
detail: `Query vector has ${query.length} dimensions, but the index has ${dims}.`
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
const limit = normalizeLimit(options.limit);
|
|
412
|
+
if (limit === 0) return [];
|
|
413
|
+
const minScore = options.minScore ?? 0;
|
|
414
|
+
const q = normalize(Float32Array.from(query));
|
|
415
|
+
const scored = [];
|
|
416
|
+
for (const [row, iri] of ids.entries()) {
|
|
417
|
+
const stored = vectors.subarray(row * dims, (row + 1) * dims);
|
|
418
|
+
let score = 0;
|
|
419
|
+
for (const [i, value] of q.entries()) score += value * (stored[i] ?? 0);
|
|
420
|
+
if (score >= minScore) {
|
|
421
|
+
scored.push({ iri, score, via: "vector" });
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
scored.sort(
|
|
425
|
+
(a, b) => a.score === b.score ? byCodeUnit(a.iri, b.iri) : b.score - a.score
|
|
426
|
+
);
|
|
427
|
+
return scored.slice(0, limit);
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
function normalizeLimit(limit) {
|
|
432
|
+
return typeof limit === "number" && Number.isInteger(limit) && limit >= 0 ? limit : 10;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// src/graph/runtime/trace.ts
|
|
436
|
+
function createTrace() {
|
|
437
|
+
return { entry: [], hops: [], exclusions: [], resolutions: [] };
|
|
438
|
+
}
|
|
439
|
+
function reachedNodes(trace) {
|
|
440
|
+
const reached = /* @__PURE__ */ new Set();
|
|
441
|
+
for (const e of trace.entry) reached.add(e.iri);
|
|
442
|
+
for (const h of trace.hops) reached.add(h.to);
|
|
443
|
+
return reached;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// src/graph/runtime/entry.ts
|
|
447
|
+
function rrfMerge(rankings, k = 60) {
|
|
448
|
+
const fused = /* @__PURE__ */ new Map();
|
|
449
|
+
for (const ranking of rankings) {
|
|
450
|
+
ranking.forEach((candidate, i) => {
|
|
451
|
+
const current = fused.get(candidate.iri) ?? {
|
|
452
|
+
score: 0,
|
|
453
|
+
vias: /* @__PURE__ */ new Set()
|
|
454
|
+
};
|
|
455
|
+
current.score += 1 / (k + i + 1);
|
|
456
|
+
current.vias.add(candidate.via);
|
|
457
|
+
fused.set(candidate.iri, current);
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
return [...fused.entries()].map(([iri, { score, vias }]) => {
|
|
461
|
+
const [only] = vias;
|
|
462
|
+
return {
|
|
463
|
+
iri,
|
|
464
|
+
score,
|
|
465
|
+
via: vias.size === 1 && only !== void 0 ? only : "hybrid"
|
|
466
|
+
};
|
|
467
|
+
}).sort(
|
|
468
|
+
(a, b) => a.score === b.score ? byCodeUnit(a.iri, b.iri) : b.score - a.score
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
async function findEntry(query, options) {
|
|
472
|
+
const trace = options.trace ?? createTrace();
|
|
473
|
+
const limit = typeof options.limit === "number" && Number.isInteger(options.limit) && options.limit >= 0 ? options.limit : 10;
|
|
474
|
+
const lexical = options.lexical.search(query, { limit });
|
|
475
|
+
const embedder = options.embedder;
|
|
476
|
+
const embed = embedder ? (q) => embedder.embed(q) : options.embedQuery;
|
|
477
|
+
let vector = [];
|
|
478
|
+
if (options.vectors && embed && query.trim() !== "") {
|
|
479
|
+
const mismatch = options.vectors.check({
|
|
480
|
+
...options.embedder ? { model: options.embedder.model, dtype: options.embedder.dtype } : {},
|
|
481
|
+
// Staleness is checked whichever way the query is embedded — it is a
|
|
482
|
+
// property of the corpus, not of the embedder, so even the unverified
|
|
483
|
+
// `embedQuery` path gets it when the caller supplies a digest.
|
|
484
|
+
...options.source === void 0 ? {} : { source: options.source }
|
|
485
|
+
});
|
|
486
|
+
if (mismatch) throw new VectorMismatchError(mismatch);
|
|
487
|
+
const queryVector = await embed(query);
|
|
488
|
+
vector = options.vectors.search(queryVector, { limit });
|
|
489
|
+
}
|
|
490
|
+
const rankings = vector.length > 0 ? [lexical, vector] : [lexical];
|
|
491
|
+
const candidates = rrfMerge(rankings).slice(0, limit);
|
|
492
|
+
for (const candidate of candidates) trace.entry.push(candidate);
|
|
493
|
+
return { candidates, lexical, vector, trace };
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// src/graph/runtime/lexical.ts
|
|
497
|
+
import MiniSearch from "minisearch";
|
|
498
|
+
function createLexicalIndex(input) {
|
|
499
|
+
const doc = typeof input === "string" ? JSON.parse(input) : input;
|
|
500
|
+
const entries = Array.isArray(doc?.entries) ? doc.entries : [];
|
|
501
|
+
const mini = new MiniSearch({
|
|
502
|
+
fields: ["title", "labels", "description", "text"],
|
|
503
|
+
storeFields: ["id"],
|
|
504
|
+
idField: "id",
|
|
505
|
+
// Titles and labels are the strongest signal that a node *is* the subject;
|
|
506
|
+
// body text is evidence it merely mentions it.
|
|
507
|
+
searchOptions: {
|
|
508
|
+
boost: { title: 3, labels: 2, description: 1.5 },
|
|
509
|
+
prefix: true,
|
|
510
|
+
fuzzy: 0.2
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
mini.addAll(entries);
|
|
514
|
+
const byIri = new Map(entries.map((e) => [e.id, e]));
|
|
515
|
+
return {
|
|
516
|
+
size: () => entries.length,
|
|
517
|
+
entry: (iri) => byIri.get(iri),
|
|
518
|
+
search(query, options = {}) {
|
|
519
|
+
const trimmed = query.trim();
|
|
520
|
+
if (trimmed === "") return [];
|
|
521
|
+
const hits = mini.search(trimmed);
|
|
522
|
+
hits.sort(
|
|
523
|
+
(a, b) => a.score === b.score ? byCodeUnit(String(a.id), String(b.id)) : b.score - a.score
|
|
524
|
+
);
|
|
525
|
+
return hits.slice(0, options.limit ?? 10).map((h) => ({ iri: String(h.id), score: h.score, via: "lexical" }));
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// src/graph/runtime/traverse.ts
|
|
531
|
+
var DCTERMS_TITLE = `${NS.dcterms}title`;
|
|
532
|
+
var DCTERMS_REFERENCES = `${NS.dcterms}references`;
|
|
533
|
+
var DCTERMS_LANGUAGE = `${NS.dcterms}language`;
|
|
534
|
+
var STATEMENT_DATA = /* @__PURE__ */ new Set([`${NS.graph}Block`, `${NS.graph}Verification`]);
|
|
535
|
+
function isStatementData(graph, iri) {
|
|
536
|
+
return graph.types(iri).some((t) => STATEMENT_DATA.has(t));
|
|
537
|
+
}
|
|
538
|
+
function documentOf(iri) {
|
|
539
|
+
const hash = iri.indexOf("#");
|
|
540
|
+
return hash === -1 ? iri : iri.slice(0, hash);
|
|
541
|
+
}
|
|
542
|
+
function resolveVariant(graph, input) {
|
|
543
|
+
if (graph.has(input)) return input;
|
|
544
|
+
const wanted = input.toLowerCase();
|
|
545
|
+
const variants = graph.instancesOf(`${NS.iirds}ProductVariant`);
|
|
546
|
+
for (const v of variants) {
|
|
547
|
+
if (graph.literal(v, DCTERMS_TITLE)?.toLowerCase() === wanted) return v;
|
|
548
|
+
}
|
|
549
|
+
for (const v of variants) {
|
|
550
|
+
if (v.slice(v.lastIndexOf("/") + 1).toLowerCase() === wanted) return v;
|
|
551
|
+
}
|
|
552
|
+
return void 0;
|
|
553
|
+
}
|
|
554
|
+
function resolveSubject(graph, input) {
|
|
555
|
+
const mapped = SOFTWARE_SUBJECT_IRIS[input.toLowerCase()];
|
|
556
|
+
if (mapped) return mapped;
|
|
557
|
+
const expanded = graph.expand(input);
|
|
558
|
+
return expanded.startsWith("http") ? expanded : void 0;
|
|
559
|
+
}
|
|
560
|
+
function scopeExclusion(graph, iri, scope) {
|
|
561
|
+
const checks = [
|
|
562
|
+
[
|
|
563
|
+
scope.variantIri,
|
|
564
|
+
IIRDS_RELATES_TO_PRODUCT_VARIANT,
|
|
565
|
+
GRAPH_NOT_APPLICABLE_TO_VARIANT,
|
|
566
|
+
"iri"
|
|
567
|
+
],
|
|
568
|
+
[scope.subjectIri, IIRDS_HAS_SUBJECT, GRAPH_NOT_SOFTWARE_SUBJECT, "iri"],
|
|
569
|
+
[scope.language, DCTERMS_LANGUAGE, void 0, "literal"]
|
|
570
|
+
];
|
|
571
|
+
for (const [target, positive, negative, kind] of checks) {
|
|
572
|
+
if (!target) continue;
|
|
573
|
+
if (negative !== void 0) {
|
|
574
|
+
const denied = graph.values(iri, negative).some((v) => v.kind === kind && v.value === target);
|
|
575
|
+
if (denied) return { node: iri, rule: negative, value: target };
|
|
576
|
+
}
|
|
577
|
+
const subject = positive === DCTERMS_LANGUAGE ? documentOf(iri) : iri;
|
|
578
|
+
const claimed = graph.values(subject, positive);
|
|
579
|
+
if (claimed.length > 0) {
|
|
580
|
+
const matches = claimed.some(
|
|
581
|
+
(v) => v.kind === kind && v.value === target
|
|
582
|
+
);
|
|
583
|
+
if (!matches) return { node: iri, rule: positive, value: target };
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return void 0;
|
|
587
|
+
}
|
|
588
|
+
function traverse(graph, options) {
|
|
589
|
+
const trace = options.trace ?? createTrace();
|
|
590
|
+
const maxDepth = options.depth ?? 1;
|
|
591
|
+
const direction = options.direction ?? "out";
|
|
592
|
+
const allowed = options.predicates && options.predicates.length > 0 ? new Set(options.predicates.map((p) => graph.expand(p))) : void 0;
|
|
593
|
+
const variantIri = options.variant ? resolveVariant(graph, options.variant) : void 0;
|
|
594
|
+
const subjectIri = options.subject ? resolveSubject(graph, options.subject) : void 0;
|
|
595
|
+
const language = options.language === void 0 ? void 0 : canonicalLanguageTag(options.language);
|
|
596
|
+
const scope = { variantIri, subjectIri, language };
|
|
597
|
+
const followTypes = options.includeTypeEdges === true || allowed?.has(RDF_TYPE) === true;
|
|
598
|
+
const excluded = /* @__PURE__ */ new Set();
|
|
599
|
+
const keep = (iri) => {
|
|
600
|
+
if (excluded.has(iri)) return false;
|
|
601
|
+
const exclusion = scopeExclusion(graph, iri, scope);
|
|
602
|
+
if (exclusion) {
|
|
603
|
+
excluded.add(iri);
|
|
604
|
+
trace.exclusions.push(exclusion);
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
return true;
|
|
608
|
+
};
|
|
609
|
+
const nodes = [];
|
|
610
|
+
const seen = /* @__PURE__ */ new Set();
|
|
611
|
+
let frontier = [];
|
|
612
|
+
const alreadySeeded = new Set(trace.entry.map((e) => e.iri));
|
|
613
|
+
for (const seed of [...options.seeds].sort(byCodeUnit)) {
|
|
614
|
+
if (!graph.has(seed) || seen.has(seed)) continue;
|
|
615
|
+
seen.add(seed);
|
|
616
|
+
if (!alreadySeeded.has(seed)) {
|
|
617
|
+
trace.entry.push({ iri: seed, score: 1, via: "explicit" });
|
|
618
|
+
}
|
|
619
|
+
if (keep(seed)) nodes.push({ iri: seed, depth: 0 });
|
|
620
|
+
frontier.push(seed);
|
|
621
|
+
}
|
|
622
|
+
const limit = options.limit ?? Infinity;
|
|
623
|
+
for (let depth = 1; depth <= maxDepth && frontier.length > 0; depth++) {
|
|
624
|
+
const next = [];
|
|
625
|
+
for (const from of frontier) {
|
|
626
|
+
if (nodes.length >= limit) break;
|
|
627
|
+
for (const { predicate, target, dir } of neighbors(
|
|
628
|
+
graph,
|
|
629
|
+
from,
|
|
630
|
+
direction
|
|
631
|
+
)) {
|
|
632
|
+
if (allowed && !allowed.has(predicate)) continue;
|
|
633
|
+
if (!followTypes && predicate === RDF_TYPE) continue;
|
|
634
|
+
if (isStatementData(graph, target)) continue;
|
|
635
|
+
trace.hops.push({ from, predicate, to: target, depth, direction: dir });
|
|
636
|
+
if (seen.has(target)) continue;
|
|
637
|
+
seen.add(target);
|
|
638
|
+
if (!keep(target)) continue;
|
|
639
|
+
if (nodes.length >= limit) break;
|
|
640
|
+
nodes.push({ iri: target, depth });
|
|
641
|
+
next.push(target);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
frontier = next;
|
|
645
|
+
}
|
|
646
|
+
return { nodes, trace };
|
|
647
|
+
}
|
|
648
|
+
function neighbors(graph, iri, direction) {
|
|
649
|
+
const out = [];
|
|
650
|
+
if (direction === "out" || direction === "both") {
|
|
651
|
+
for (const e of graph.out(iri)) out.push({ ...e, dir: "out" });
|
|
652
|
+
}
|
|
653
|
+
if (direction === "in" || direction === "both") {
|
|
654
|
+
for (const e of graph.in(iri)) out.push({ ...e, dir: "in" });
|
|
655
|
+
}
|
|
656
|
+
return out;
|
|
657
|
+
}
|
|
658
|
+
function reverseReferences(graph, iri, options = {}) {
|
|
659
|
+
return traverse(graph, {
|
|
660
|
+
...options,
|
|
661
|
+
seeds: [iri],
|
|
662
|
+
direction: "in",
|
|
663
|
+
predicates: [DCTERMS_REFERENCES],
|
|
664
|
+
depth: options.depth ?? 1
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
function impact(graph, iri, options = {}) {
|
|
668
|
+
const result = traverse(graph, {
|
|
669
|
+
...options,
|
|
670
|
+
seeds: [iri],
|
|
671
|
+
direction: "in",
|
|
672
|
+
depth: options.depth ?? 3,
|
|
673
|
+
// The seed *may* occupy a slot in the walker's limit, so ask for one more:
|
|
674
|
+
// `limit` means "this many *affected* nodes". Whether it actually takes
|
|
675
|
+
// one depends on the scope filter — a seed the filter excludes still
|
|
676
|
+
// expands but is not in `nodes` — so the slack is trimmed below rather
|
|
677
|
+
// than assumed spent.
|
|
678
|
+
limit: options.limit === void 0 ? void 0 : options.limit + 1
|
|
679
|
+
});
|
|
680
|
+
const affected = result.nodes.filter((n) => n.iri !== iri);
|
|
681
|
+
return {
|
|
682
|
+
nodes: options.limit === void 0 ? affected : affected.slice(0, options.limit),
|
|
683
|
+
trace: result.trace
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// src/graph/runtime/retrieve.ts
|
|
688
|
+
var StaleArtifactError = class extends Error {
|
|
689
|
+
constructor(manifestGraph, found, file = "graph.jsonld") {
|
|
690
|
+
super(`chunks.json was built from graph ${manifestGraph}, and ${file} is ${found}; publish again.`);
|
|
691
|
+
this.name = "StaleArtifactError";
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
var ModelMismatchError = class extends Error {
|
|
695
|
+
expected;
|
|
696
|
+
actual;
|
|
697
|
+
constructor(expected, actual) {
|
|
698
|
+
super(`chunks.json was embedded with ${expected}, and the embedder loads ${actual}.`);
|
|
699
|
+
this.name = "ModelMismatchError";
|
|
700
|
+
this.expected = expected;
|
|
701
|
+
this.actual = actual;
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
var IMPACT_DEPTH = 3;
|
|
705
|
+
var ENTRY_POOL = 50;
|
|
706
|
+
function withholds(record, reason) {
|
|
707
|
+
if (reason === "drifted") return record.pins.error > 0;
|
|
708
|
+
return record.evals !== null && record.evals.current && record.evals.fail + record.evals.error > 0;
|
|
709
|
+
}
|
|
710
|
+
function failedField(row, where) {
|
|
711
|
+
for (const { field, value } of where) {
|
|
712
|
+
const held = row.metadata[field];
|
|
713
|
+
const values = Array.isArray(held) ? held : [held];
|
|
714
|
+
if (!values.some((v) => v !== void 0 && v !== null && scalarText(v) === value)) return field;
|
|
715
|
+
}
|
|
716
|
+
return void 0;
|
|
717
|
+
}
|
|
718
|
+
function scalarText(v) {
|
|
719
|
+
return typeof v === "string" ? v : JSON.stringify(v);
|
|
720
|
+
}
|
|
721
|
+
function modelOf(manifest, language) {
|
|
722
|
+
return manifest.languages[language]?.model ?? null;
|
|
723
|
+
}
|
|
724
|
+
async function rank(ctx, question, language, mode, where) {
|
|
725
|
+
const model = modelOf(ctx.manifest, language);
|
|
726
|
+
const wantVector = mode === "vector" || mode === "hybrid" || mode === void 0 && model !== null;
|
|
727
|
+
let vector = [];
|
|
728
|
+
const storeRows = /* @__PURE__ */ new Map();
|
|
729
|
+
let fallback = mode === void 0 && model === null && Object.values(ctx.manifest.languages).some((l) => l.model !== null);
|
|
730
|
+
if (wantVector && question.trim() !== "") {
|
|
731
|
+
let queryVector = null;
|
|
732
|
+
const embedder = model === null ? null : await ctx.embedder(model);
|
|
733
|
+
if (model !== null && embedder !== null) {
|
|
734
|
+
if (embedder.model !== model.id) throw new ModelMismatchError(model.id, embedder.model);
|
|
735
|
+
queryVector = Array.from(await embedder.embed(question));
|
|
736
|
+
}
|
|
737
|
+
const found = await ctx.store.query({ text: question, vector: queryVector, k: ENTRY_POOL, language, where });
|
|
738
|
+
for (const m of found) if (m.row !== void 0) storeRows.set(m.id, m.row);
|
|
739
|
+
vector = found.map((m) => ({ iri: m.id, score: m.score, via: "vector" })).sort((a, b) => a.score === b.score ? byCodeUnit(a.iri, b.iri) : b.score - a.score);
|
|
740
|
+
fallback = queryVector === null && vector.length === 0;
|
|
741
|
+
}
|
|
742
|
+
let lexical = [];
|
|
743
|
+
if (mode !== "vector" || fallback) {
|
|
744
|
+
const index = await ctx.lexical(language);
|
|
745
|
+
lexical = index?.search(question, { limit: ENTRY_POOL }) ?? [];
|
|
746
|
+
}
|
|
747
|
+
const lexicalScore = new Map(lexical.map((c) => [c.iri, c.score]));
|
|
748
|
+
const vectorScore = new Map(vector.map((c) => [c.iri, c.score]));
|
|
749
|
+
const ranked = rrfMerge([lexical, vector].filter((leg) => leg.length > 0)).map((c) => {
|
|
750
|
+
const row = storeRows.get(c.iri);
|
|
751
|
+
return {
|
|
752
|
+
iri: c.iri,
|
|
753
|
+
via: c.via,
|
|
754
|
+
fused: c.score,
|
|
755
|
+
lexical: lexicalScore.get(c.iri) ?? null,
|
|
756
|
+
vector: vectorScore.get(c.iri) ?? null,
|
|
757
|
+
...row === void 0 ? {} : { row }
|
|
758
|
+
};
|
|
759
|
+
});
|
|
760
|
+
const best = lexical[0]?.score ?? vector[0]?.score;
|
|
761
|
+
return { ranked, fallback, best };
|
|
762
|
+
}
|
|
763
|
+
async function rowsFor(ctx, ids, language) {
|
|
764
|
+
const out = /* @__PURE__ */ new Map();
|
|
765
|
+
if (ids.length === 0) return out;
|
|
766
|
+
for (const row of await ctx.chunks.get([...ids], language)) {
|
|
767
|
+
if (row.graph !== ctx.manifest.graph) {
|
|
768
|
+
throw new StaleArtifactError(ctx.manifest.graph, row.graph, ctx.manifest.languages[row.language]?.file ?? "a chunk row");
|
|
769
|
+
}
|
|
770
|
+
out.set(row.id, row);
|
|
771
|
+
}
|
|
772
|
+
return out;
|
|
773
|
+
}
|
|
774
|
+
function chunkFile(ctx, language) {
|
|
775
|
+
return language === null ? "" : ctx.manifest.languages[language]?.file ?? "";
|
|
776
|
+
}
|
|
777
|
+
function admit(ctx, iri, row, language, floor, trace) {
|
|
778
|
+
const exported = floor.exported.get(iri);
|
|
779
|
+
if (exported !== void 0) {
|
|
780
|
+
trace.withheld.push({ node: iri, because: exported, at: "export" });
|
|
781
|
+
return { kind: "withheld" };
|
|
782
|
+
}
|
|
783
|
+
const sourceUrl = row === void 0 ? chunkFile(ctx, language) : chunkFile(ctx, row.language);
|
|
784
|
+
if (row === void 0) {
|
|
785
|
+
trace.resolutions.push({ iri, sourceUrl, ok: false, error: "no chunk row" });
|
|
786
|
+
return { kind: "skipped" };
|
|
787
|
+
}
|
|
788
|
+
const field = failedField(row, floor.where);
|
|
789
|
+
if (field !== void 0) {
|
|
790
|
+
trace.filtered.push({ node: iri, field });
|
|
791
|
+
return { kind: "skipped" };
|
|
792
|
+
}
|
|
793
|
+
const because = floor.withhold.filter((reason) => withholds(row.provenance, reason));
|
|
794
|
+
if (because.length > 0) {
|
|
795
|
+
trace.withheld.push({ node: iri, because, at: "query" });
|
|
796
|
+
return { kind: "withheld" };
|
|
797
|
+
}
|
|
798
|
+
trace.resolutions.push({ iri, sourceUrl, ok: true });
|
|
799
|
+
return { kind: "row", row };
|
|
800
|
+
}
|
|
801
|
+
function floorOf(ctx, withhold, where) {
|
|
802
|
+
return {
|
|
803
|
+
withhold: withhold ?? [],
|
|
804
|
+
where: where ?? [],
|
|
805
|
+
exported: new Map(ctx.manifest.withheld.map((w) => [w.id, w.because]))
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
function emptyTrace() {
|
|
809
|
+
return { entry: { candidates: [] }, hops: [], exclusions: [], resolutions: [], withheld: [], filtered: [] };
|
|
810
|
+
}
|
|
811
|
+
async function selectMatches(ctx, entry, language, seeds, floor, trace) {
|
|
812
|
+
const rows = await rowsFor(
|
|
813
|
+
ctx,
|
|
814
|
+
entry.ranked.filter((r) => r.row === void 0).map((r) => r.iri),
|
|
815
|
+
language
|
|
816
|
+
);
|
|
817
|
+
const kept = [];
|
|
818
|
+
let withheld = 0;
|
|
819
|
+
for (const ranked of entry.ranked) {
|
|
820
|
+
if (kept.length + withheld >= seeds) break;
|
|
821
|
+
const row = ranked.row ?? rows.get(ranked.iri);
|
|
822
|
+
if (row !== void 0 && row.graph !== ctx.manifest.graph) {
|
|
823
|
+
throw new StaleArtifactError(ctx.manifest.graph, row.graph, chunkFile(ctx, language) || "a chunk row");
|
|
824
|
+
}
|
|
825
|
+
const admitted = admit(ctx, ranked.iri, row, language, floor, trace);
|
|
826
|
+
if (admitted.kind === "skipped") continue;
|
|
827
|
+
trace.entry.candidates.push({ iri: ranked.iri, score: ranked.fused, via: ranked.via });
|
|
828
|
+
if (admitted.kind === "withheld") withheld += 1;
|
|
829
|
+
else kept.push({ ranked, row: admitted.row, rank: kept.length + withheld + 1 });
|
|
830
|
+
}
|
|
831
|
+
return { kept, withheld };
|
|
832
|
+
}
|
|
833
|
+
function absorb(trace, walked) {
|
|
834
|
+
trace.hops.push(...walked.hops);
|
|
835
|
+
trace.exclusions.push(...walked.exclusions);
|
|
836
|
+
}
|
|
837
|
+
async function passagesOf(ctx, nodes, language, floor, trace) {
|
|
838
|
+
const rows = await rowsFor(ctx, nodes.map((n) => n.iri), language);
|
|
839
|
+
const out = [];
|
|
840
|
+
for (const n of nodes) {
|
|
841
|
+
const admitted = admit(ctx, n.iri, rows.get(n.iri), language, floor, trace);
|
|
842
|
+
if (admitted.kind === "row") out.push({ iri: n.iri, depth: n.depth, row: admitted.row });
|
|
843
|
+
}
|
|
844
|
+
return out;
|
|
845
|
+
}
|
|
846
|
+
function edgeOf(graph, hop) {
|
|
847
|
+
const predicate = graph.compact(hop.predicate);
|
|
848
|
+
return hop.direction === "out" ? { from: hop.from, predicate, to: hop.to } : { from: hop.to, predicate, to: hop.from };
|
|
849
|
+
}
|
|
850
|
+
function languagesOf(manifest) {
|
|
851
|
+
return Object.keys(manifest.languages).sort(byCodeUnit);
|
|
852
|
+
}
|
|
853
|
+
function languageNamed(manifest, tag) {
|
|
854
|
+
const wanted = canonicalLanguageTag(tag);
|
|
855
|
+
return languagesOf(manifest).find((l) => canonicalLanguageTag(l) === wanted);
|
|
856
|
+
}
|
|
857
|
+
function refusal(reason, detail) {
|
|
858
|
+
return { reason, detail };
|
|
859
|
+
}
|
|
860
|
+
function scopeRefusal(ctx, scope) {
|
|
861
|
+
if (scope.variant !== void 0 && resolveVariant(ctx.graph, scope.variant) === void 0) {
|
|
862
|
+
return refusal("unknown-scope", `No product variant matches ${scope.variant}.`);
|
|
863
|
+
}
|
|
864
|
+
if (scope.softwareSubject !== void 0 && resolveSubject(ctx.graph, scope.softwareSubject) === void 0) {
|
|
865
|
+
return refusal("unknown-scope", `No software subject matches ${scope.softwareSubject}.`);
|
|
866
|
+
}
|
|
867
|
+
if (scope.lang !== void 0 && languageNamed(ctx.manifest, scope.lang) === void 0) {
|
|
868
|
+
const have = languagesOf(ctx.manifest).join(", ") || "none";
|
|
869
|
+
return refusal("unknown-scope", `No localization ${scope.lang}; this corpus has ${have}.`);
|
|
870
|
+
}
|
|
871
|
+
return void 0;
|
|
872
|
+
}
|
|
873
|
+
function pagesOf(manifest, pages) {
|
|
874
|
+
const out = {};
|
|
875
|
+
for (const page of [...new Set(pages)].sort(byCodeUnit)) {
|
|
876
|
+
out[page] = { metadata: manifest.pages[page]?.metadata ?? {} };
|
|
877
|
+
}
|
|
878
|
+
return out;
|
|
879
|
+
}
|
|
880
|
+
function bundleOf(ctx, head, language, items, maxChars, trace, empty) {
|
|
881
|
+
const base = { ...head, language, trace };
|
|
882
|
+
if (items.length === 0) {
|
|
883
|
+
return { ...base, context: [], citations: [], pages: {}, truncated: false, refusal: empty };
|
|
884
|
+
}
|
|
885
|
+
const context = [];
|
|
886
|
+
let used = 0;
|
|
887
|
+
let truncated = false;
|
|
888
|
+
for (const { row, depth, role } of items) {
|
|
889
|
+
if (used + row.text.length > maxChars) {
|
|
890
|
+
truncated = true;
|
|
891
|
+
continue;
|
|
892
|
+
}
|
|
893
|
+
used += row.text.length;
|
|
894
|
+
context.push({
|
|
895
|
+
iri: row.id,
|
|
896
|
+
title: row.title,
|
|
897
|
+
text: row.text,
|
|
898
|
+
sourceUrl: row.page,
|
|
899
|
+
depth,
|
|
900
|
+
page: row.page,
|
|
901
|
+
provenance: row.provenance,
|
|
902
|
+
role
|
|
903
|
+
});
|
|
904
|
+
}
|
|
905
|
+
if (context.length === 0) {
|
|
906
|
+
return {
|
|
907
|
+
...base,
|
|
908
|
+
context: [],
|
|
909
|
+
citations: [],
|
|
910
|
+
pages: {},
|
|
911
|
+
truncated,
|
|
912
|
+
refusal: refusal("no-content", "Every matching passage exceeded the character budget.")
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
return {
|
|
916
|
+
...base,
|
|
917
|
+
context,
|
|
918
|
+
citations: context.map((b) => ({ iri: b.iri, title: b.title, sourceUrl: b.sourceUrl })),
|
|
919
|
+
pages: pagesOf(ctx.manifest, context.map((b) => b.page)),
|
|
920
|
+
truncated
|
|
921
|
+
};
|
|
922
|
+
}
|
|
923
|
+
function budget(passages, maxChars) {
|
|
924
|
+
let used = 0;
|
|
925
|
+
let cut = false;
|
|
926
|
+
for (const p of passages) {
|
|
927
|
+
const n = p.text?.length ?? 0;
|
|
928
|
+
if (used + n > maxChars) {
|
|
929
|
+
p.text = null;
|
|
930
|
+
p.truncated = true;
|
|
931
|
+
cut = true;
|
|
932
|
+
} else {
|
|
933
|
+
used += n;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
return cut;
|
|
937
|
+
}
|
|
938
|
+
function passageOf(row) {
|
|
939
|
+
return {
|
|
940
|
+
iri: row.id,
|
|
941
|
+
title: row.title,
|
|
942
|
+
page: row.page,
|
|
943
|
+
lines: row.lines,
|
|
944
|
+
text: row.text,
|
|
945
|
+
truncated: false,
|
|
946
|
+
provenance: row.provenance
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
async function retrieve(input) {
|
|
950
|
+
const ctx = input;
|
|
951
|
+
const trace = emptyTrace();
|
|
952
|
+
const settings = ctx.manifest.retrieve;
|
|
953
|
+
const seeds = input.seeds ?? settings.seeds;
|
|
954
|
+
const depth = input.depth ?? settings.depth;
|
|
955
|
+
const maxChars = input.maxChars ?? settings.maxChars;
|
|
956
|
+
const head = { question: input.question };
|
|
957
|
+
const refuse = (r, language2) => bundleOf(ctx, head, language2, [], maxChars, trace, r);
|
|
958
|
+
const scope = scopeRefusal(ctx, input);
|
|
959
|
+
if (scope !== void 0) return refuse(scope, null);
|
|
960
|
+
const languages = languagesOf(ctx.manifest);
|
|
961
|
+
const language = input.lang === void 0 ? languages[0] : languageNamed(ctx.manifest, input.lang);
|
|
962
|
+
if (input.lang === void 0 && languages.length > 1) {
|
|
963
|
+
return refuse(
|
|
964
|
+
refusal("lang-required", `This corpus has more than one localization (${languages.join(", ")}); pass lang to choose one.`),
|
|
965
|
+
null
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
if (language === void 0) return refuse(refusal("no-route", "This artifact holds no passages."), null);
|
|
969
|
+
const where = input.where ?? [];
|
|
970
|
+
const entry = await rank(ctx, input.question, language, input.mode, where);
|
|
971
|
+
if (entry.fallback) trace.entry.fallback = "lexical";
|
|
972
|
+
const floor = floorOf(ctx, input.withhold, where);
|
|
973
|
+
const { kept, withheld } = await selectMatches(ctx, entry, language, seeds, floor, trace);
|
|
974
|
+
if (kept.length === 0) {
|
|
975
|
+
return refuse(
|
|
976
|
+
withheld > 0 ? refusal("withheld", "Every match was withheld by the trust floor; trace.withheld names them.") : refusal("no-route", "No passage in the docs matches this question."),
|
|
977
|
+
language
|
|
978
|
+
);
|
|
979
|
+
}
|
|
980
|
+
const walked = createTrace();
|
|
981
|
+
const { nodes } = traverse(ctx.graph, {
|
|
982
|
+
seeds: kept.map((m) => m.row.id),
|
|
983
|
+
depth,
|
|
984
|
+
direction: "out",
|
|
985
|
+
...input.variant === void 0 ? {} : { variant: input.variant },
|
|
986
|
+
...input.softwareSubject === void 0 ? {} : { subject: input.softwareSubject },
|
|
987
|
+
language,
|
|
988
|
+
trace: walked
|
|
989
|
+
});
|
|
990
|
+
absorb(trace, walked);
|
|
991
|
+
const inScope = new Set(nodes.filter((n) => n.depth === 0).map((n) => n.iri));
|
|
992
|
+
const reached = await passagesOf(ctx, nodes.filter((n) => n.depth > 0), language, floor, trace);
|
|
993
|
+
const items = [
|
|
994
|
+
...kept.filter((m) => inScope.has(m.row.id)).map((m) => ({ row: m.row, depth: 0, role: "match" })),
|
|
995
|
+
...reached.map((r) => ({ row: r.row, depth: r.depth, role: "context" }))
|
|
996
|
+
];
|
|
997
|
+
return bundleOf(ctx, head, language, items, maxChars, trace, refusal("no-route", "Every match is outside the requested scope."));
|
|
998
|
+
}
|
|
999
|
+
async function walkPassages(input) {
|
|
1000
|
+
const ctx = input;
|
|
1001
|
+
const trace = emptyTrace();
|
|
1002
|
+
const maxChars = input.maxChars ?? ctx.manifest.retrieve.maxChars;
|
|
1003
|
+
const node = ctx.graph.expand(input.node);
|
|
1004
|
+
const head = { node };
|
|
1005
|
+
const refuse = (r) => bundleOf(ctx, head, null, [], maxChars, trace, r);
|
|
1006
|
+
if (!ctx.graph.has(node)) return refuse(refusal("unknown-node", `No node ${input.node} in the graph.`));
|
|
1007
|
+
const scope = scopeRefusal(ctx, input);
|
|
1008
|
+
if (scope !== void 0) return refuse(scope);
|
|
1009
|
+
const language = input.lang === void 0 ? null : languageNamed(ctx.manifest, input.lang) ?? null;
|
|
1010
|
+
const walked = createTrace();
|
|
1011
|
+
const options = {
|
|
1012
|
+
...input.depth === void 0 ? {} : { depth: input.depth },
|
|
1013
|
+
...input.predicates === void 0 ? {} : { predicates: input.predicates },
|
|
1014
|
+
...input.variant === void 0 ? {} : { variant: input.variant },
|
|
1015
|
+
...input.softwareSubject === void 0 ? {} : { subject: input.softwareSubject },
|
|
1016
|
+
...input.lang === void 0 ? {} : { language: input.lang },
|
|
1017
|
+
...input.limit === void 0 ? {} : { limit: input.limit },
|
|
1018
|
+
trace: walked
|
|
1019
|
+
};
|
|
1020
|
+
const { nodes } = input.impact === true ? impact(ctx.graph, node, options) : traverse(ctx.graph, { ...options, seeds: [node], direction: input.reverse === true ? "in" : "out" });
|
|
1021
|
+
absorb(trace, walked);
|
|
1022
|
+
trace.entry.candidates.push({ iri: node, score: 1, via: "explicit" });
|
|
1023
|
+
const floor = floorOf(ctx, input.withhold, input.where);
|
|
1024
|
+
const start = await passagesOf(ctx, [{ iri: node, depth: 0 }], language, floor, trace);
|
|
1025
|
+
const reached = await passagesOf(ctx, nodes.filter((n) => n.iri !== node), language, floor, trace);
|
|
1026
|
+
const inward = input.impact === true || input.reverse === true;
|
|
1027
|
+
const items = [
|
|
1028
|
+
...start.map((r) => ({ row: r.row, depth: 0, role: "match" })),
|
|
1029
|
+
...reached.map((r) => ({ row: r.row, depth: r.depth, role: inward ? "dependent" : "context" }))
|
|
1030
|
+
];
|
|
1031
|
+
const languageOut = language ?? start[0]?.row.language ?? null;
|
|
1032
|
+
return bundleOf(
|
|
1033
|
+
ctx,
|
|
1034
|
+
head,
|
|
1035
|
+
languageOut,
|
|
1036
|
+
items,
|
|
1037
|
+
maxChars,
|
|
1038
|
+
trace,
|
|
1039
|
+
refusal("no-route", "No passage was reachable from this node under the active scope filter.")
|
|
1040
|
+
);
|
|
1041
|
+
}
|
|
1042
|
+
async function lookup(input) {
|
|
1043
|
+
const ctx = input;
|
|
1044
|
+
const trace = emptyTrace();
|
|
1045
|
+
const { seeds, depth, maxChars } = ctx.manifest.retrieve;
|
|
1046
|
+
const empty = (language2, r) => ({
|
|
1047
|
+
question: input.question,
|
|
1048
|
+
language: language2,
|
|
1049
|
+
matches: { passages: [], truncated: false },
|
|
1050
|
+
traversal: { passages: [], edges: [], truncated: false },
|
|
1051
|
+
impact: { passages: [], edges: [], truncated: false },
|
|
1052
|
+
pages: {},
|
|
1053
|
+
citations: [],
|
|
1054
|
+
trace,
|
|
1055
|
+
refusal: r
|
|
1056
|
+
});
|
|
1057
|
+
const choose = async (mode) => {
|
|
1058
|
+
const scores = {};
|
|
1059
|
+
let chosen;
|
|
1060
|
+
for (const language2 of languagesOf(ctx.manifest)) {
|
|
1061
|
+
const entry2 = await rank(ctx, input.question, language2, mode, []);
|
|
1062
|
+
if (entry2.best === void 0) continue;
|
|
1063
|
+
scores[language2] = entry2.best;
|
|
1064
|
+
if (chosen === void 0 || entry2.best > chosen.best) chosen = { language: language2, entry: entry2, best: entry2.best };
|
|
1065
|
+
}
|
|
1066
|
+
return { scores, chosen };
|
|
1067
|
+
};
|
|
1068
|
+
let picked = await choose("lexical");
|
|
1069
|
+
if (picked.chosen === void 0) picked = await choose(void 0);
|
|
1070
|
+
else picked.chosen.entry = await rank(ctx, input.question, picked.chosen.language, void 0, []);
|
|
1071
|
+
trace.entry.languages = picked.scores;
|
|
1072
|
+
if (picked.chosen === void 0) return empty(null, refusal("no-route", "No passage in the docs matches this question."));
|
|
1073
|
+
const { language, entry } = picked.chosen;
|
|
1074
|
+
trace.entry.chosen = language;
|
|
1075
|
+
if (entry.fallback) trace.entry.fallback = "lexical";
|
|
1076
|
+
const floor = floorOf(ctx, input.withhold, []);
|
|
1077
|
+
const { kept, withheld } = await selectMatches(ctx, entry, language, seeds, floor, trace);
|
|
1078
|
+
if (kept.length === 0) {
|
|
1079
|
+
return empty(
|
|
1080
|
+
language,
|
|
1081
|
+
withheld > 0 ? refusal("withheld", "Every match was withheld by the trust floor; trace.withheld names them.") : refusal("no-route", "No passage in the docs matches this question.")
|
|
1082
|
+
);
|
|
1083
|
+
}
|
|
1084
|
+
const seedIris = kept.map((m) => m.row.id);
|
|
1085
|
+
const walk = async (direction, reach) => {
|
|
1086
|
+
const walked = createTrace();
|
|
1087
|
+
const { nodes } = traverse(ctx.graph, { seeds: seedIris, depth: reach, direction, language, trace: walked });
|
|
1088
|
+
absorb(trace, walked);
|
|
1089
|
+
const reached = await passagesOf(ctx, nodes.filter((n) => n.depth > 0), language, floor, trace);
|
|
1090
|
+
const passages = reached.map((r) => ({ ...passageOf(r.row), depth: r.depth }));
|
|
1091
|
+
return {
|
|
1092
|
+
passages,
|
|
1093
|
+
edges: walked.hops.map((h) => edgeOf(ctx.graph, h)),
|
|
1094
|
+
truncated: budget(passages, maxChars)
|
|
1095
|
+
};
|
|
1096
|
+
};
|
|
1097
|
+
const matches = kept.map(
|
|
1098
|
+
(m) => ({
|
|
1099
|
+
...passageOf(m.row),
|
|
1100
|
+
rank: m.rank,
|
|
1101
|
+
score: { lexical: m.ranked.lexical, vector: m.ranked.vector, fused: m.ranked.fused }
|
|
1102
|
+
})
|
|
1103
|
+
);
|
|
1104
|
+
const matchesTruncated = budget(matches, maxChars);
|
|
1105
|
+
const traversal = await walk("out", depth);
|
|
1106
|
+
const impact2 = await walk("in", IMPACT_DEPTH);
|
|
1107
|
+
const served = [...matches, ...traversal.passages, ...impact2.passages];
|
|
1108
|
+
const citations = [];
|
|
1109
|
+
const cited = /* @__PURE__ */ new Set();
|
|
1110
|
+
for (const p of served) {
|
|
1111
|
+
if (p.text === null || cited.has(p.iri)) continue;
|
|
1112
|
+
cited.add(p.iri);
|
|
1113
|
+
citations.push({ iri: p.iri, title: p.title, sourceUrl: p.page });
|
|
1114
|
+
}
|
|
1115
|
+
return {
|
|
1116
|
+
question: input.question,
|
|
1117
|
+
language,
|
|
1118
|
+
matches: { passages: matches, truncated: matchesTruncated },
|
|
1119
|
+
traversal,
|
|
1120
|
+
impact: impact2,
|
|
1121
|
+
pages: pagesOf(ctx.manifest, served.map((p) => p.page)),
|
|
1122
|
+
citations,
|
|
1123
|
+
trace
|
|
1124
|
+
};
|
|
1125
|
+
}
|
|
1126
|
+
function createStaticVectorStore(rows) {
|
|
1127
|
+
return {
|
|
1128
|
+
query(q) {
|
|
1129
|
+
if (q.vector === null) return Promise.resolve([]);
|
|
1130
|
+
const query = normalize(Float32Array.from(q.vector));
|
|
1131
|
+
const scored = [];
|
|
1132
|
+
for (const row of rows) {
|
|
1133
|
+
if (row.vector === void 0 || row.vector.length !== query.length) continue;
|
|
1134
|
+
if (q.language !== null && row.language !== q.language) continue;
|
|
1135
|
+
if (failedField(row, q.where) !== void 0) continue;
|
|
1136
|
+
const stored = normalize(Float32Array.from(row.vector));
|
|
1137
|
+
let score = 0;
|
|
1138
|
+
for (const [i, value] of query.entries()) score += value * (stored[i] ?? 0);
|
|
1139
|
+
scored.push({ id: row.id, score, row });
|
|
1140
|
+
}
|
|
1141
|
+
scored.sort((a, b) => a.score === b.score ? byCodeUnit(a.id, b.id) : b.score - a.score);
|
|
1142
|
+
return Promise.resolve(scored.slice(0, q.k));
|
|
1143
|
+
}
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
function createHttpVectorStore(options) {
|
|
1147
|
+
const doFetch = options.fetch ?? defaultFetch;
|
|
1148
|
+
return {
|
|
1149
|
+
async query(q) {
|
|
1150
|
+
const response = await doFetch(options.url, {
|
|
1151
|
+
method: "POST",
|
|
1152
|
+
headers: { "content-type": "application/json" },
|
|
1153
|
+
body: JSON.stringify({ text: q.text, vector: q.vector, k: q.k, language: q.language, where: q.where })
|
|
1154
|
+
});
|
|
1155
|
+
if (!response.ok) throw new Error(`${options.url} answered HTTP ${String(response.status)}.`);
|
|
1156
|
+
const body = JSON.parse(await response.text());
|
|
1157
|
+
if (!Array.isArray(body.matches)) throw new Error(`${options.url} returned no matches array.`);
|
|
1158
|
+
return body.matches.filter(isMatch);
|
|
1159
|
+
}
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1162
|
+
function isMatch(m) {
|
|
1163
|
+
if (typeof m !== "object" || m === null) return false;
|
|
1164
|
+
const o = m;
|
|
1165
|
+
return typeof o.id === "string" && typeof o.score === "number";
|
|
1166
|
+
}
|
|
1167
|
+
function parseChunkRows(text) {
|
|
1168
|
+
const rows = [];
|
|
1169
|
+
for (const [i, line] of text.split(/\r?\n/).entries()) {
|
|
1170
|
+
if (line.trim() === "") continue;
|
|
1171
|
+
const row = JSON.parse(line);
|
|
1172
|
+
if (!isChunkRow(row)) throw new Error(`Line ${String(i + 1)} is not a chunk row.`);
|
|
1173
|
+
rows.push(row);
|
|
1174
|
+
}
|
|
1175
|
+
return rows;
|
|
1176
|
+
}
|
|
1177
|
+
function isRecord(v) {
|
|
1178
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
1179
|
+
}
|
|
1180
|
+
function isChunkRow(v) {
|
|
1181
|
+
return isRecord(v) && ["id", "graph", "page", "lines", "language", "title", "text"].every((k) => typeof v[k] === "string") && (v.vector === void 0 || Array.isArray(v.vector)) && isRecord(v.metadata) && isRecord(v.provenance);
|
|
1182
|
+
}
|
|
1183
|
+
function parseChunksManifest(input) {
|
|
1184
|
+
const doc = typeof input === "string" ? JSON.parse(input) : input;
|
|
1185
|
+
const valid = isRecord(doc) && doc.format === "manni-graph-chunks" && doc.version === 1 && ["graph", "graphFile", "searchIndex"].every((k) => typeof doc[k] === "string") && !("model" in doc) && isRecord(doc.retrieve) && isRecord(doc.pages) && Array.isArray(doc.withheld) && isRecord(doc.languages) && Object.values(doc.languages).every((l) => isRecord(l) && typeof l.file === "string" && isModel(l.model));
|
|
1186
|
+
if (!valid) throw new Error('Not a manni graph chunk manifest: expected format "manni-graph-chunks", version 1.');
|
|
1187
|
+
return doc;
|
|
1188
|
+
}
|
|
1189
|
+
function isModel(v) {
|
|
1190
|
+
return v === null || isRecord(v) && typeof v.id === "string" && typeof v.dimensions === "number";
|
|
1191
|
+
}
|
|
1192
|
+
function createChunkFileSource(textOrUrl, options = {}) {
|
|
1193
|
+
const isText = textOrUrl.trim() === "" || textOrUrl.trimStart().startsWith("{");
|
|
1194
|
+
let rows;
|
|
1195
|
+
const load = () => rows ??= isText ? Promise.resolve(parseChunkRows(textOrUrl)) : fetchText(options.fetch ?? defaultFetch, textOrUrl).then(parseChunkRows);
|
|
1196
|
+
return {
|
|
1197
|
+
async get(ids, language) {
|
|
1198
|
+
const want = new Set(ids);
|
|
1199
|
+
return (await load()).filter((r) => want.has(r.id) && (language === null || r.language === language));
|
|
1200
|
+
}
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
function defaultFetch(url, init) {
|
|
1204
|
+
return globalThis.fetch(url, init);
|
|
1205
|
+
}
|
|
1206
|
+
async function fetchText(doFetch, url) {
|
|
1207
|
+
const response = await doFetch(url);
|
|
1208
|
+
if (!response.ok) throw new Error(`${url} answered HTTP ${String(response.status)}.`);
|
|
1209
|
+
return response.text();
|
|
1210
|
+
}
|
|
1211
|
+
function artifactContext(manifest, graph, read, seams) {
|
|
1212
|
+
const embedders = /* @__PURE__ */ new Map();
|
|
1213
|
+
const embedder = (model) => {
|
|
1214
|
+
let pending = embedders.get(model.id);
|
|
1215
|
+
if (pending === void 0) {
|
|
1216
|
+
pending = seams.embedder(model);
|
|
1217
|
+
embedders.set(model.id, pending);
|
|
1218
|
+
}
|
|
1219
|
+
return pending;
|
|
1220
|
+
};
|
|
1221
|
+
const loaded = /* @__PURE__ */ new Map();
|
|
1222
|
+
const rowsOf = (language) => {
|
|
1223
|
+
let pending = loaded.get(language);
|
|
1224
|
+
if (pending === void 0) {
|
|
1225
|
+
const entry = manifest.languages[language];
|
|
1226
|
+
pending = entry === void 0 ? Promise.resolve([]) : read(entry.file).then(parseChunkRows);
|
|
1227
|
+
loaded.set(language, pending);
|
|
1228
|
+
}
|
|
1229
|
+
return pending;
|
|
1230
|
+
};
|
|
1231
|
+
const allRows = async (language) => language === null ? (await Promise.all(languagesOf(manifest).map(rowsOf))).flat() : rowsOf(language);
|
|
1232
|
+
const chunks = seams.chunks ?? {
|
|
1233
|
+
async get(ids, language) {
|
|
1234
|
+
const want = new Set(ids);
|
|
1235
|
+
return (await allRows(language)).filter((r) => want.has(r.id));
|
|
1236
|
+
}
|
|
1237
|
+
};
|
|
1238
|
+
const store = seams.store ?? {
|
|
1239
|
+
async query(q) {
|
|
1240
|
+
return createStaticVectorStore(await allRows(q.language)).query(q);
|
|
1241
|
+
}
|
|
1242
|
+
};
|
|
1243
|
+
let localizations;
|
|
1244
|
+
const lexicons = /* @__PURE__ */ new Map();
|
|
1245
|
+
const lexical = (language) => {
|
|
1246
|
+
let pending = lexicons.get(language);
|
|
1247
|
+
if (pending === void 0) {
|
|
1248
|
+
pending = (async () => {
|
|
1249
|
+
localizations ??= read(manifest.searchIndex).then(parseLocalizations);
|
|
1250
|
+
const doc = await localizations;
|
|
1251
|
+
const wanted = canonicalLanguageTag(language);
|
|
1252
|
+
const entry = doc?.languages.find((l) => canonicalLanguageTag(l.language) === wanted);
|
|
1253
|
+
return entry === void 0 ? void 0 : createLexicalIndex(await read(entry.search.path));
|
|
1254
|
+
})();
|
|
1255
|
+
lexicons.set(language, pending);
|
|
1256
|
+
}
|
|
1257
|
+
return pending;
|
|
1258
|
+
};
|
|
1259
|
+
return { manifest, graph, lexical, store, chunks, embedder };
|
|
1260
|
+
}
|
|
1261
|
+
async function defaultEmbedder(model) {
|
|
1262
|
+
try {
|
|
1263
|
+
const specifier = "./embed.js";
|
|
1264
|
+
const embed = await import(
|
|
1265
|
+
/* @vite-ignore */
|
|
1266
|
+
specifier
|
|
1267
|
+
);
|
|
1268
|
+
return model.id === "mock" ? embed.createMockEmbedder({ dims: model.dimensions }) : await embed.createLocalEmbedder({ model: model.id, role: "query" });
|
|
1269
|
+
} catch {
|
|
1270
|
+
return null;
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
async function createRetriever(options) {
|
|
1274
|
+
const doFetch = options.fetch ?? defaultFetch;
|
|
1275
|
+
const fromUrl = typeof options.manifest === "string";
|
|
1276
|
+
const manifest = parseChunksManifest(
|
|
1277
|
+
typeof options.manifest === "string" ? await fetchText(doFetch, options.manifest) : options.manifest
|
|
1278
|
+
);
|
|
1279
|
+
const base = typeof options.manifest === "string" ? options.manifest.slice(0, options.manifest.lastIndexOf("/") + 1) : "";
|
|
1280
|
+
const resolvePath = (path) => fromUrl && !/^[a-z][a-z0-9+.-]*:/i.test(path) ? base + path : path;
|
|
1281
|
+
const read = (path) => fetchText(doFetch, resolvePath(path));
|
|
1282
|
+
const graphText = await read(manifest.graphFile);
|
|
1283
|
+
const digest = `sha256-${(await searchIndexDigest(graphText)).slice("sha256:".length)}`;
|
|
1284
|
+
if (digest !== manifest.graph) throw new StaleArtifactError(manifest.graph, digest);
|
|
1285
|
+
const graph = GraphIndex.fromJsonLd(graphText);
|
|
1286
|
+
const embed = options.embed;
|
|
1287
|
+
const embedder = embed === false ? () => Promise.resolve(null) : embed === void 0 ? defaultEmbedder : () => Promise.resolve(embed);
|
|
1288
|
+
const ctx = artifactContext(manifest, graph, read, {
|
|
1289
|
+
...options.store === void 0 ? {} : { store: options.store },
|
|
1290
|
+
...options.chunks === void 0 ? {} : { chunks: options.chunks },
|
|
1291
|
+
embedder
|
|
1292
|
+
});
|
|
1293
|
+
return {
|
|
1294
|
+
manifest,
|
|
1295
|
+
lookup: (question) => lookup({ ...ctx, question }),
|
|
1296
|
+
retrieve: (question, o = {}) => retrieve({ ...ctx, ...o, question }),
|
|
1297
|
+
traverse: (node, o = {}) => walkPassages({ ...ctx, ...o, node }),
|
|
1298
|
+
impact: (node, o = {}) => walkPassages({ ...ctx, ...o, node, impact: true })
|
|
1299
|
+
};
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
export {
|
|
1303
|
+
VectorIndexError,
|
|
1304
|
+
normalize,
|
|
1305
|
+
encodeVectorIndex,
|
|
1306
|
+
decodeVectorIndex,
|
|
1307
|
+
range,
|
|
1308
|
+
GraphIndex,
|
|
1309
|
+
VectorMismatchError,
|
|
1310
|
+
searchIndexDigest,
|
|
1311
|
+
createVectorIndex,
|
|
1312
|
+
createTrace,
|
|
1313
|
+
reachedNodes,
|
|
1314
|
+
rrfMerge,
|
|
1315
|
+
findEntry,
|
|
1316
|
+
createLexicalIndex,
|
|
1317
|
+
resolveVariant,
|
|
1318
|
+
resolveSubject,
|
|
1319
|
+
scopeExclusion,
|
|
1320
|
+
traverse,
|
|
1321
|
+
reverseReferences,
|
|
1322
|
+
impact,
|
|
1323
|
+
StaleArtifactError,
|
|
1324
|
+
ModelMismatchError,
|
|
1325
|
+
IMPACT_DEPTH,
|
|
1326
|
+
withholds,
|
|
1327
|
+
retrieve,
|
|
1328
|
+
walkPassages,
|
|
1329
|
+
lookup,
|
|
1330
|
+
createStaticVectorStore,
|
|
1331
|
+
createHttpVectorStore,
|
|
1332
|
+
parseChunkRows,
|
|
1333
|
+
parseChunksManifest,
|
|
1334
|
+
createChunkFileSource,
|
|
1335
|
+
artifactContext,
|
|
1336
|
+
createRetriever
|
|
1337
|
+
};
|
|
1338
|
+
//# sourceMappingURL=chunk-HEVYEI6O.js.map
|