@delali/narsil 0.3.1-next.145 → 0.3.1-next.149
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/dist/adapters/filesystem.mjs +2 -2
- package/dist/adapters/gcs.mjs +2 -2
- package/dist/adapters/indexeddb.mjs +1 -1
- package/dist/adapters/s3.mjs +2 -2
- package/dist/analysis-change-ZCJNYBJM.mjs +39 -0
- package/dist/{analysis-change-entry-V4SQQSXQ.mjs → analysis-change-entry-YN3ELL6C.mjs} +1 -1
- package/dist/browser/{analysis-change-QTCQ5ETU.mjs → analysis-change-M65AZQQG.mjs} +8 -5
- package/dist/browser/{analysis-change-entry-KYEQSP3I.mjs → analysis-change-entry-H3YIOLXZ.mjs} +1 -1
- package/dist/browser/{chunk-GP4QLUQM.mjs → chunk-2JABMB4S.mjs} +10 -6
- package/dist/browser/{chunk-5WYWLTM5.mjs → chunk-4DYLCICG.mjs} +2 -2
- package/dist/browser/{chunk-75PMIFQA.mjs → chunk-4RD3AFSB.mjs} +1 -1
- package/dist/browser/{chunk-XWMEGKDC.mjs → chunk-5V74FFZE.mjs} +2 -2
- package/dist/browser/{chunk-PMBSBSCN.mjs → chunk-6OWX3OP2.mjs} +2 -2
- package/dist/browser/chunk-BRWY3JCN.mjs +678 -0
- package/dist/browser/{chunk-ZALEKEUH.mjs → chunk-BVX2MEHA.mjs} +306 -607
- package/dist/browser/{chunk-FXPMF65P.mjs → chunk-D6UFGBF5.mjs} +15 -45
- package/dist/browser/{chunk-QNDWMPV2.mjs → chunk-IAXZAVYL.mjs} +25 -66
- package/dist/browser/chunk-JNOAKDUM.mjs +31 -0
- package/dist/browser/{chunk-BTMRV5MY.mjs → chunk-KAEVGLYN.mjs} +2 -2
- package/dist/browser/{chunk-FHO5N7FD.mjs → chunk-LURXVCHG.mjs} +1 -1
- package/dist/browser/chunk-MPK76GVS.mjs +63 -0
- package/dist/browser/{chunk-6OFS7RUN.mjs → chunk-NVEJJP4R.mjs} +1 -1
- package/dist/browser/{chunk-U77XQJOY.mjs → chunk-ODQP7P3H.mjs} +455 -66
- package/dist/browser/{chunk-IFGTRRPU.mjs → chunk-PIS7ZAP7.mjs} +3 -25
- package/dist/browser/{chunk-XIHLIMFX.mjs → chunk-QMNZFFD3.mjs} +4 -3
- package/dist/browser/{chunk-AOKLYVTT.mjs → chunk-SRL47USS.mjs} +2 -2
- package/dist/browser/chunk-TY7L6OXO.mjs +69 -0
- package/dist/browser/{chunk-LGJ6XMH5.mjs → chunk-W643QV3L.mjs} +6 -5
- package/dist/browser/chunk-WXLBZJMT.mjs +25 -0
- package/dist/browser/{chunk-ZKVDQ5ZH.mjs → chunk-XILIT5MT.mjs} +8 -8
- package/dist/browser/{cold-module-Y2IJAP7S.mjs → cold-module-A5S6JFDN.mjs} +20 -20
- package/dist/browser/{durability-wiring-MLXZ5QQF.mjs → durability-wiring-W5AVV3RF.mjs} +53 -30
- package/dist/browser/gzip-5S7ROJFJ.mjs +1 -0
- package/dist/browser/import-PTT63AGK.mjs +12 -0
- package/dist/browser/{module-5LMDGXQH.mjs → module-CEKA7MQH.mjs} +13 -10
- package/dist/browser/module-X5PGOE47.mjs +4288 -0
- package/dist/browser/{phrase-module-KXNRTWCS.mjs → phrase-module-CNKMUY4U.mjs} +2 -1
- package/dist/browser/pool-DTYSPBMQ.mjs +296 -0
- package/dist/{restore-queue-UAVGBSJG.mjs → browser/restore-queue-DD4LWZK7.mjs} +2 -2
- package/dist/browser/{runtime-ZNEGK33G.mjs → runtime-D2ULZGCN.mjs} +3 -2
- package/dist/browser/{snapshot-2EGS2FYU.mjs → snapshot-DQ6A4PQP.mjs} +18 -14
- package/dist/browser/{write-WMZA6NSF.mjs → write-MAGRAIEH.mjs} +14 -11
- package/dist/{capabilities-CNnzGtqP.d.ts → capabilities-BT-HLDC8.d.ts} +2 -2
- package/dist/{chunk-FRJ5RGX7.mjs → chunk-2HJWQPR6.mjs} +2 -2
- package/dist/{chunk-DKMZ7JFW.mjs → chunk-3BVGJ4JR.mjs} +10 -4
- package/dist/{chunk-QYMAYU7M.mjs → chunk-3HV7M5OJ.mjs} +155 -149
- package/dist/chunk-3MXV3VHR.mjs +46 -0
- package/dist/{chunk-6FOJKNUR.mjs → chunk-3SBJVQLL.mjs} +2 -2
- package/dist/{chunk-PRID7CA6.mjs → chunk-3TVDTACC.mjs} +1 -1
- package/dist/{chunk-RTVAWNUL.mjs → chunk-5WB2A3XU.mjs} +9 -6
- package/dist/{chunk-LWFK3TC2.mjs → chunk-65WCV5JO.mjs} +1 -1
- package/dist/{chunk-G6Q5DEF5.mjs → chunk-6BWLDLS3.mjs} +3 -46
- package/dist/{chunk-W6M4RRAT.mjs → chunk-6C5EX2QR.mjs} +3 -3
- package/dist/{chunk-HOC5JP2H.mjs → chunk-6F6USYII.mjs} +119 -11
- package/dist/{chunk-7523VCM4.mjs → chunk-7HKWO4BJ.mjs} +7 -7
- package/dist/{chunk-ONDEEBTA.mjs → chunk-7N223GWV.mjs} +2 -2
- package/dist/{chunk-UGQULMSU.mjs → chunk-ADBS5XRI.mjs} +10 -10
- package/dist/{chunk-MJTEU2XK.mjs → chunk-AWNHDBKV.mjs} +429 -73
- package/dist/{chunk-2E2ZFOPT.mjs → chunk-B2FTZNNF.mjs} +2 -2
- package/dist/{chunk-NWTEJ2T7.mjs → chunk-B7XIOAMK.mjs} +3 -3
- package/dist/{chunk-W55X2BYN.mjs → chunk-BEZH7VEH.mjs} +1 -1
- package/dist/chunk-BTPGQC6Z.mjs +27 -0
- package/dist/{chunk-KG5N7LE6.mjs → chunk-CYYSZPVV.mjs} +11 -11
- package/dist/{chunk-HTMUGEFU.mjs → chunk-DOY6EG76.mjs} +6 -6
- package/dist/{chunk-UHVTEVF5.mjs → chunk-F22IOCR2.mjs} +10 -6
- package/dist/{chunk-FTQLF5JI.mjs → chunk-IRWP524D.mjs} +1 -1
- package/dist/{chunk-XLAI4OIO.mjs → chunk-IZT3G24Q.mjs} +2 -2
- package/dist/{chunk-SE7MKJVK.mjs → chunk-J7M73VY7.mjs} +1 -1
- package/dist/chunk-KAKQ74LS.mjs +53 -0
- package/dist/{chunk-GVPYMSLO.mjs → chunk-KYVRZAYP.mjs} +1 -1
- package/dist/{chunk-F442VZUL.mjs → chunk-KZ5GQE4Z.mjs} +5 -5
- package/dist/{chunk-WDEWU46C.mjs → chunk-KZQIVGW7.mjs} +2 -2
- package/dist/{chunk-7AJAW6NN.mjs → chunk-L7TIAYPX.mjs} +8 -8
- package/dist/{chunk-GRFXSEFF.mjs → chunk-LDGGR7YT.mjs} +4 -81
- package/dist/{chunk-WMIV7PJ7.mjs → chunk-LJ4ENXOD.mjs} +15 -14
- package/dist/chunk-LQG5G2LY.mjs +39 -0
- package/dist/{chunk-IQHZMCLX.mjs → chunk-MDF6PDIA.mjs} +2 -0
- package/dist/{chunk-QXH2CS4V.mjs → chunk-NVTFQFUL.mjs} +7 -26
- package/dist/{chunk-5K2JHKM3.mjs → chunk-NYSONUB7.mjs} +1 -1
- package/dist/{chunk-OH7SX5MY.mjs → chunk-O4KUNDVF.mjs} +12 -10
- package/dist/{chunk-EBIHRL7B.mjs → chunk-Q2UL2TPC.mjs} +1 -1
- package/dist/{chunk-RNUP6WZ6.mjs → chunk-QEYF2SQ6.mjs} +2 -2
- package/dist/{chunk-RVBUQITB.mjs → chunk-QN5WVV5K.mjs} +7 -7
- package/dist/{chunk-RIIUZJQ3.mjs → chunk-QYRNLLLI.mjs} +1 -1
- package/dist/{chunk-BTARFTEN.mjs → chunk-SOUZMPME.mjs} +4 -4
- package/dist/{chunk-ISUOSACK.mjs → chunk-T4CJDBBE.mjs} +1 -1
- package/dist/{chunk-DHEJAGVW.mjs → chunk-TTCKT2RT.mjs} +5 -5
- package/dist/{chunk-QU2XF7VI.mjs → chunk-U2726TL5.mjs} +5 -4
- package/dist/{chunk-LYKUJNAV.mjs → chunk-U46IZNCW.mjs} +2 -2
- package/dist/{chunk-SNB4DYL3.mjs → chunk-U6NE6RCV.mjs} +4 -4
- package/dist/{chunk-SA52N7G3.mjs → chunk-VP6XRMWR.mjs} +4 -4
- package/dist/{chunk-YUCGFPMS.mjs → chunk-VQDZVWN3.mjs} +449 -405
- package/dist/{chunk-IWVEG5CX.mjs → chunk-XIHUI4KB.mjs} +2 -2
- package/dist/{chunk-IV2XTSS2.mjs → chunk-XKPNKR3V.mjs} +16 -46
- package/dist/{chunk-UQS5PAS5.mjs → chunk-YGQOPB2Z.mjs} +1 -1
- package/dist/{chunk-SVO6NV4D.mjs → chunk-YKEHQUSZ.mjs} +4 -4
- package/dist/{chunk-6Z22L5RN.mjs → chunk-ZAYWOHVG.mjs} +1 -1
- package/dist/{chunk-QLDLQW36.mjs → chunk-ZEKDO7YX.mjs} +2 -2
- package/dist/chunk-ZXNNR73R.mjs +31 -0
- package/dist/{chunk-VJS4JFBQ.mjs → chunk-ZXX5GZEX.mjs} +3 -1
- package/dist/{chunk-X5SBWARI.mjs → chunk-ZYKUD5NX.mjs} +9 -7
- package/dist/{client-DoYPHcUn.d.ts → client-Cn3gNxUU.d.ts} +3 -3
- package/dist/client.d.ts +5 -5
- package/dist/client.mjs +2 -0
- package/dist/{cold-module-NUD332YD.mjs → cold-module-2WWGBFJC.mjs} +36 -33
- package/dist/{config-CuUfls_H.d.ts → config-CfhH1IWQ.d.ts} +1 -1
- package/dist/{config-Dzs46im6.d.ts → config-aIsy7mcL.d.ts} +52 -1
- package/dist/direct-executor-QLVHMELJ.mjs +24 -0
- package/dist/distribution/coordinator/etcd.mjs +1 -1
- package/dist/distribution/transport/grpc.mjs +1 -1
- package/dist/distribution.d.ts +4 -4
- package/dist/distribution.mjs +150 -255
- package/dist/{durability-wiring-K7RDXWL6.mjs → durability-wiring-GNWQSZI3.mjs} +67 -42
- package/dist/embeddings/openai.mjs +2 -2
- package/dist/{engine-C0ifUEcm.d.ts → engine-CJRUHXgf.d.ts} +1 -1
- package/dist/gzip-F4HMIEEQ.mjs +1 -0
- package/dist/import-DX55PN4G.mjs +41 -0
- package/dist/index.browser.mjs +7 -4
- package/dist/index.d.ts +539 -5
- package/dist/index.mjs +22 -16
- package/dist/index.react-native.mjs +19 -0
- package/dist/{long-query-thread-FKVHRCHF.mjs → long-query-thread-SCUE62TO.mjs} +20 -17
- package/dist/{module-GJUKW5VU.mjs → module-HW6IXUM6.mjs} +601 -108
- package/dist/{module-TMFIWOD2.mjs → module-V5K4ZP23.mjs} +29 -23
- package/dist/{paged-partition-archive-WNUEMYWM.mjs → paged-partition-archive-FDIIWZNR.mjs} +31 -25
- package/dist/partial-words.mjs +5 -5
- package/dist/pattern.mjs +3 -3
- package/dist/persistence/durability/checkpoint-worker.mjs +28 -22
- package/dist/{phrase-module-OMY3QQV7.mjs → phrase-module-OUQIRUQU.mjs} +1 -1
- package/dist/pool-UPV5PVPY.mjs +298 -0
- package/dist/react-native/chunks/analysis-change-HGW3L6NY.mjs +893 -0
- package/dist/react-native/chunks/analysis-change-entry-BNRU6STO.mjs +1 -0
- package/dist/react-native/chunks/chunk-2J2727V4.mjs +495 -0
- package/dist/react-native/chunks/chunk-3CTWSQRW.mjs +1251 -0
- package/dist/react-native/chunks/chunk-5FV4ZEVA.mjs +2619 -0
- package/dist/react-native/chunks/chunk-7VWQLEFV.mjs +642 -0
- package/dist/react-native/chunks/chunk-AAAETEJK.mjs +27 -0
- package/dist/react-native/chunks/chunk-AM7RSBPF.mjs +32 -0
- package/dist/react-native/chunks/chunk-B6OD63Z3.mjs +997 -0
- package/dist/react-native/chunks/chunk-BRNAVOKP.mjs +177 -0
- package/dist/react-native/chunks/chunk-DKJTZ625.mjs +207 -0
- package/dist/react-native/chunks/chunk-FKGG4UOF.mjs +177 -0
- package/dist/{browser/chunk-K7UFXEJS.mjs → react-native/chunks/chunk-GV5KFQ6M.mjs} +12 -6
- package/dist/react-native/chunks/chunk-HURV3ASY.mjs +4416 -0
- package/dist/react-native/chunks/chunk-IO2SMUQD.mjs +10 -0
- package/dist/react-native/chunks/chunk-JV6VQY2S.mjs +17 -0
- package/dist/react-native/chunks/chunk-KUJOBRKS.mjs +217 -0
- package/dist/react-native/chunks/chunk-KWRW5U65.mjs +226 -0
- package/dist/react-native/chunks/chunk-L4M7QJNP.mjs +110 -0
- package/dist/react-native/chunks/chunk-LQ6Q4L5B.mjs +360 -0
- package/dist/react-native/chunks/chunk-LQAKO5QV.mjs +523 -0
- package/dist/react-native/chunks/chunk-LRGWK367.mjs +11 -0
- package/dist/react-native/chunks/chunk-LVIKKAA6.mjs +93 -0
- package/dist/react-native/chunks/chunk-NFGBOI46.mjs +14 -0
- package/dist/react-native/chunks/chunk-NNE4VDMM.mjs +8 -0
- package/dist/react-native/chunks/chunk-PPEPTUVV.mjs +56 -0
- package/dist/react-native/chunks/chunk-PSWKQVVT.mjs +9 -0
- package/dist/react-native/chunks/chunk-PU55IOG4.mjs +910 -0
- package/dist/react-native/chunks/chunk-QC4ZRBSX.mjs +270 -0
- package/dist/react-native/chunks/chunk-R4EEMYM6.mjs +21 -0
- package/dist/react-native/chunks/chunk-R4OPUZQ5.mjs +86 -0
- package/dist/react-native/chunks/chunk-SLGIAD6B.mjs +2634 -0
- package/dist/react-native/chunks/chunk-TJMSKBEM.mjs +25 -0
- package/dist/react-native/chunks/chunk-TLYD7IJ2.mjs +23 -0
- package/dist/react-native/chunks/chunk-UFNCLEQM.mjs +73 -0
- package/dist/react-native/chunks/chunk-YH7W556M.mjs +179 -0
- package/dist/react-native/chunks/chunk-YJSMEKRE.mjs +22385 -0
- package/dist/react-native/chunks/chunk-ZCKNBPQ4.mjs +79 -0
- package/dist/react-native/chunks/cold-module-TSFWLYWC.mjs +755 -0
- package/dist/react-native/chunks/durability-wiring-PZR6KOQK.mjs +4509 -0
- package/dist/react-native/chunks/gzip.react-native-7JWCLVOB.mjs +3 -0
- package/dist/react-native/chunks/import-VTODLPLO.mjs +22 -0
- package/dist/{browser/module-Q6VFGLMG.mjs → react-native/chunks/module-JBBH6PZH.mjs} +609 -144
- package/dist/react-native/chunks/module-VEK2KCKU.mjs +25 -0
- package/dist/react-native/chunks/phrase-module-76UAPPSG.mjs +329 -0
- package/dist/react-native/chunks/pool-CD4ANYUI.mjs +296 -0
- package/dist/react-native/chunks/release-D7ZTIANM.mjs +33 -0
- package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
- package/dist/{browser/restore-queue-SOAHKZSQ.mjs → react-native/chunks/restore-queue-OI5EH7R3.mjs} +2 -2
- package/dist/react-native/chunks/runtime-3MCDLJAF.mjs +559 -0
- package/dist/react-native/chunks/snapshot-AUZMEV36.mjs +1205 -0
- package/dist/react-native/chunks/write-DDC2QBRJ.mjs +197 -0
- package/dist/react-native/partial-words.mjs +733 -0
- package/dist/react-native/pattern.mjs +2442 -0
- package/dist/react-native/synonyms.mjs +460 -0
- package/dist/react.d.ts +7 -7
- package/dist/react.mjs +2 -0
- package/dist/{rerank-ZZ5CHKLW.mjs → rerank-3W54N6P2.mjs} +4 -4
- package/dist/rerankers/http.mjs +2 -2
- package/dist/restore-queue-BQWWNNQI.mjs +41 -0
- package/dist/{runtime-RAOCKSYA.mjs → runtime-L2DZYY6N.mjs} +5 -5
- package/dist/server.d.ts +8 -8
- package/dist/server.mjs +32 -28
- package/dist/{snapshot-DB5ZVCDN.mjs → snapshot-EHXTOTW5.mjs} +39 -33
- package/dist/synonyms.mjs +3 -3
- package/dist/{tasks-ChWp0wrF.d.ts → tasks-C4ElXr4P.d.ts} +2 -2
- package/dist/thread-I7DVHHW7.mjs +31 -0
- package/dist/vector/search-worker.mjs +7 -5
- package/dist/workers/entry.mjs +9 -9
- package/dist/{write-KZVTLGCI.mjs → write-SDJXXQ2G.mjs} +29 -23
- package/package.json +88 -7
- package/dist/analysis-change-D5APH4Z3.mjs +0 -34
- package/dist/browser/import-FTMLTPGQ.mjs +0 -9
- package/dist/direct-executor-FDQYQEJQ.mjs +0 -22
- package/dist/import-6KXFY6XI.mjs +0 -35
- package/dist/thread-OV6T555H.mjs +0 -28
- /package/dist/{chunk-22YPZVMG.mjs → chunk-ZXNQEW43.mjs} +0 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/engine/index-state/replace.ts
|
|
2
|
+
function runReplacement(context, entry, body, settle) {
|
|
3
|
+
const previous = entry.state;
|
|
4
|
+
entry.state = "replacing";
|
|
5
|
+
const run = (async () => {
|
|
6
|
+
await context.waitForDrain(entry);
|
|
7
|
+
await body();
|
|
8
|
+
})().then(
|
|
9
|
+
() => settle(true, previous),
|
|
10
|
+
(error) => {
|
|
11
|
+
settle(false, previous);
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
).finally(() => {
|
|
15
|
+
entry.replacePromise = null;
|
|
16
|
+
});
|
|
17
|
+
entry.replacePromise = run;
|
|
18
|
+
return run;
|
|
19
|
+
}
|
|
20
|
+
function replaceUnregistered(context, indexName, entry, action) {
|
|
21
|
+
return runReplacement(context, entry, action, (succeeded) => {
|
|
22
|
+
if (!succeeded) {
|
|
23
|
+
context.forget(indexName);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
entry.state = "open";
|
|
27
|
+
entry.lastAccessAt = Date.now();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
async function replaceExclusively(context, indexName, action) {
|
|
31
|
+
let entry = context.entryOf(indexName);
|
|
32
|
+
while (true) {
|
|
33
|
+
const pending = entry.dropPromise ?? entry.replacePromise ?? entry.reopenPromise ?? entry.closePromise;
|
|
34
|
+
if (pending === null) break;
|
|
35
|
+
await pending.catch(() => void 0);
|
|
36
|
+
entry = context.entryOf(indexName);
|
|
37
|
+
}
|
|
38
|
+
const wasOpen = entry.state === "open";
|
|
39
|
+
await runReplacement(
|
|
40
|
+
context,
|
|
41
|
+
entry,
|
|
42
|
+
() => action(wasOpen),
|
|
43
|
+
(succeeded, previous) => {
|
|
44
|
+
if (!succeeded) {
|
|
45
|
+
entry.state = previous;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
entry.state = "open";
|
|
49
|
+
entry.cachedError = null;
|
|
50
|
+
entry.reopenFailures = 0;
|
|
51
|
+
entry.nextReopenAt = 0;
|
|
52
|
+
entry.lastAccessAt = Date.now();
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { replaceExclusively, replaceUnregistered };
|
package/dist/{browser/restore-queue-SOAHKZSQ.mjs → react-native/chunks/restore-queue-OI5EH7R3.mjs}
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { abandonChangeOf } from './chunk-
|
|
1
|
+
import { abandonChangeOf } from './chunk-R4EEMYM6.mjs';
|
|
2
2
|
|
|
3
3
|
// src/narsil/restore-queue.ts
|
|
4
4
|
function restoreDepsOf(core, host) {
|
|
@@ -24,7 +24,7 @@ function restoreDepsOf(core, host) {
|
|
|
24
24
|
function queueRestore(core, host, restoresInFlight, indexName, data) {
|
|
25
25
|
const previous = restoresInFlight.get(indexName) ?? Promise.resolve();
|
|
26
26
|
const restored = previous.then(async () => {
|
|
27
|
-
const { restoreFromSnapshot } = await import('./snapshot-
|
|
27
|
+
const { restoreFromSnapshot } = await import('./snapshot-AUZMEV36.mjs');
|
|
28
28
|
await restoreFromSnapshot(indexName, data, restoreDepsOf(core, host));
|
|
29
29
|
});
|
|
30
30
|
const settled = restored.then(
|
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
import { MIN_RERANK_TIMEOUT_MS, MAX_RERANK_TIMEOUT_MS, MIN_RERANK_WINDOW, MAX_RERANK_WINDOW, DEFAULT_MAX_RERANK_WINDOW, DEFAULT_RERANK_TIMEOUT_MS, DEFAULT_RERANK_WINDOW } from './chunk-IO2SMUQD.mjs';
|
|
2
|
+
import { DEFAULT_LIMIT, DEFAULT_OFFSET } from './chunk-AM7RSBPF.mjs';
|
|
3
|
+
import { MIN_HYBRID_DEPTH, MAX_HYBRID_DEPTH, HYBRID_RESCORE_WINDOW, RESULT_WINDOW } from './chunk-AAAETEJK.mjs';
|
|
4
|
+
import { NarsilError, ErrorCodes } from './chunk-YH7W556M.mjs';
|
|
5
|
+
|
|
6
|
+
// src/search/hybrid-depth.ts
|
|
7
|
+
function fixedHybridDepthOf(hybrid) {
|
|
8
|
+
const depth = hybrid?.depth;
|
|
9
|
+
if (depth === void 0 || depth === null) return void 0;
|
|
10
|
+
if (!Number.isInteger(depth) || depth < MIN_HYBRID_DEPTH || depth > MAX_HYBRID_DEPTH) {
|
|
11
|
+
throw new NarsilError(
|
|
12
|
+
ErrorCodes.CONFIG_INVALID,
|
|
13
|
+
`hybrid.depth must be a whole number from ${MIN_HYBRID_DEPTH} to ${MAX_HYBRID_DEPTH}`,
|
|
14
|
+
{ depth }
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
return depth;
|
|
18
|
+
}
|
|
19
|
+
function requireWithinHybridDepth(cursorDepth, offset, depth) {
|
|
20
|
+
const start = cursorDepth + offset;
|
|
21
|
+
if (start < depth) return;
|
|
22
|
+
throw new NarsilError(
|
|
23
|
+
ErrorCodes.SEARCH_RESULT_WINDOW_EXCEEDED,
|
|
24
|
+
`A hybrid query pages through the first ${depth} fused results, and this page starts at ${start}. Raise hybrid.depth to page further`,
|
|
25
|
+
{ offset, cursorDepth, depth }
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/search/position-page.ts
|
|
30
|
+
function lastOrganic(page, pinnedIds) {
|
|
31
|
+
for (let i = page.length - 1; i >= 0; i--) {
|
|
32
|
+
if (pinnedIds === void 0 || !pinnedIds.has(page[i].id)) return page[i];
|
|
33
|
+
}
|
|
34
|
+
return void 0;
|
|
35
|
+
}
|
|
36
|
+
function positionOf(organic, id) {
|
|
37
|
+
for (let i = 0; i < organic.length; i++) {
|
|
38
|
+
if (organic[i].id === id) return i;
|
|
39
|
+
}
|
|
40
|
+
return -1;
|
|
41
|
+
}
|
|
42
|
+
function pageByPosition(placed, organic, { start, limit, pinnedIds, binding, encodeCursor }) {
|
|
43
|
+
if (limit <= 0) return { page: [] };
|
|
44
|
+
const page = placed.slice(start, start + limit);
|
|
45
|
+
const last = lastOrganic(page, pinnedIds);
|
|
46
|
+
if (placed.length <= start + limit || last === void 0) return { page };
|
|
47
|
+
const depth = pinnedIds === void 0 ? start + page.lastIndexOf(last) + 1 : positionOf(organic, last.id) + 1;
|
|
48
|
+
const cursor = encodeCursor({
|
|
49
|
+
anchor: last.id,
|
|
50
|
+
score: last.score ?? null,
|
|
51
|
+
sortKey: null,
|
|
52
|
+
sortSignature: null,
|
|
53
|
+
depth,
|
|
54
|
+
binding: binding()
|
|
55
|
+
});
|
|
56
|
+
return { page, cursor };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/engine/query/hybrid-depth.ts
|
|
60
|
+
function countOfUnion(text, vectorScored) {
|
|
61
|
+
const textIds = /* @__PURE__ */ new Set();
|
|
62
|
+
for (const doc of text.scored) textIds.add(doc.docId);
|
|
63
|
+
let vectorOnly = 0;
|
|
64
|
+
for (const doc of vectorScored) if (!textIds.has(doc.docId)) vectorOnly++;
|
|
65
|
+
if (text.scored.length === text.totalMatched) return text.totalMatched + vectorOnly;
|
|
66
|
+
return Math.max(text.totalMatched, text.scored.length + vectorOnly);
|
|
67
|
+
}
|
|
68
|
+
async function fusedAtDepth(params, context, depth, legs) {
|
|
69
|
+
const { textLeg, vectorLeg } = legs;
|
|
70
|
+
const { vector: vectorConfig, mode: _mode, hybrid: _hybrid, searchAfter: _after, pinned: _pinned, ...rest } = params;
|
|
71
|
+
const fusion = legs.resolveHybridFusion(params.hybrid);
|
|
72
|
+
const textOnlyParams = { ...rest, limit: depth, offset: 0 };
|
|
73
|
+
const filtered = legs.filteredSearchOf(params, context);
|
|
74
|
+
if (filtered !== void 0 && legs.filterSubsetsMatchNothing(filtered.first)) {
|
|
75
|
+
return { scored: [], totalMatched: 0 };
|
|
76
|
+
}
|
|
77
|
+
if (!vectorConfig || !vectorConfig.value) {
|
|
78
|
+
const textOnly = await textLeg(textOnlyParams, context);
|
|
79
|
+
return { scored: textOnly.scored, totalMatched: textOnly.totalMatched, facets: textOnly.facets };
|
|
80
|
+
}
|
|
81
|
+
const vectorPending = vectorLeg(
|
|
82
|
+
vectorConfig,
|
|
83
|
+
new Float32Array(vectorConfig.value),
|
|
84
|
+
context,
|
|
85
|
+
filtered,
|
|
86
|
+
depth,
|
|
87
|
+
HYBRID_RESCORE_WINDOW
|
|
88
|
+
);
|
|
89
|
+
const [textFanOutResult, vectorOutcome] = await Promise.all([textLeg(textOnlyParams, context), vectorPending]);
|
|
90
|
+
const vectorScored = vectorOutcome.scored;
|
|
91
|
+
const textScored = textFanOutResult.scored.slice(0, depth);
|
|
92
|
+
let fusedScored;
|
|
93
|
+
if (fusion.strategy === "rrf") {
|
|
94
|
+
fusedScored = legs.reciprocalRankFusion([textScored, vectorScored], { k: fusion.k });
|
|
95
|
+
} else {
|
|
96
|
+
fusedScored = legs.linearCombination(textScored, vectorScored, { alpha: fusion.alpha });
|
|
97
|
+
}
|
|
98
|
+
const textHoldsEveryMatch = textFanOutResult.scored.length === textFanOutResult.totalMatched;
|
|
99
|
+
const threshold = params.minScore ?? 0;
|
|
100
|
+
const filtersByScore = threshold > 0;
|
|
101
|
+
if (filtersByScore) fusedScored = fusedScored.filter((doc) => doc.score >= threshold);
|
|
102
|
+
return {
|
|
103
|
+
scored: fusedScored.length > depth ? fusedScored.slice(0, depth) : fusedScored,
|
|
104
|
+
totalMatched: filtersByScore ? fusedScored.length : countOfUnion(textFanOutResult, vectorScored),
|
|
105
|
+
matchedExact: vectorOutcome.holdsEveryMatch && textHoldsEveryMatch && (!filtersByScore || textScored.length === textFanOutResult.totalMatched),
|
|
106
|
+
facets: textFanOutResult.facets
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
async function searchFixedDepthHybrid(params, context, { cursorDepth, offset, limit }, host) {
|
|
110
|
+
const depth = fixedHybridDepthOf(params.hybrid) ?? 0;
|
|
111
|
+
requireWithinHybridDepth(cursorDepth, offset, depth);
|
|
112
|
+
const result = await fusedAtDepth(params, context, depth, host);
|
|
113
|
+
const placesPins = context.partitionIds === void 0;
|
|
114
|
+
const paginate = (placed, _limit, _offset, _binding, _cursor, _sort, pinnedIds) => {
|
|
115
|
+
const pins = placesPins ? pinnedIds : void 0;
|
|
116
|
+
const organic = pins === void 0 ? placed : result.scored.map((doc) => ({ id: doc.docId }));
|
|
117
|
+
const { page, cursor } = pageByPosition(placed, organic, {
|
|
118
|
+
start: cursorDepth + offset,
|
|
119
|
+
limit,
|
|
120
|
+
pinnedIds: pins,
|
|
121
|
+
binding: context.cursorBinding,
|
|
122
|
+
encodeCursor: host.encodePageCursor
|
|
123
|
+
});
|
|
124
|
+
return { paginated: page, nextCursor: cursor };
|
|
125
|
+
};
|
|
126
|
+
return { ...result, paginate };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/rerank/query-text.ts
|
|
130
|
+
function nonBlank(value) {
|
|
131
|
+
return typeof value === "string" && value.trim().length > 0 ? value : void 0;
|
|
132
|
+
}
|
|
133
|
+
function rerankQueryTextOf(params) {
|
|
134
|
+
const rerank = params.rerank;
|
|
135
|
+
if (typeof rerank === "object" && rerank !== null && "query" in rerank && rerank.query !== void 0) {
|
|
136
|
+
return nonBlank(rerank.query);
|
|
137
|
+
}
|
|
138
|
+
return nonBlank(params.term) ?? nonBlank(params.vector?.text);
|
|
139
|
+
}
|
|
140
|
+
function withRerankQueryText(params) {
|
|
141
|
+
const rerank = params.rerank;
|
|
142
|
+
if (rerank === void 0 || typeof rerank !== "object" || rerank === null || rerank.query !== void 0) {
|
|
143
|
+
return params;
|
|
144
|
+
}
|
|
145
|
+
const query = rerankQueryTextOf(params);
|
|
146
|
+
return query === void 0 ? params : { ...params, rerank: { ...rerank, query } };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/rerank/params.ts
|
|
150
|
+
function invalid(message, details = {}) {
|
|
151
|
+
return new NarsilError(ErrorCodes.RERANK_CONFIG_INVALID, message, details);
|
|
152
|
+
}
|
|
153
|
+
function requireTextFields(fields, schema, host) {
|
|
154
|
+
if (!Array.isArray(fields) || fields.length === 0) {
|
|
155
|
+
throw invalid("rerank.fields must list at least one text field");
|
|
156
|
+
}
|
|
157
|
+
const flat = host.flattenSchema(schema);
|
|
158
|
+
const checked = [];
|
|
159
|
+
const seen = /* @__PURE__ */ new Set();
|
|
160
|
+
for (const field of fields) {
|
|
161
|
+
if (typeof field !== "string") {
|
|
162
|
+
throw invalid("rerank.fields accepts field paths written as strings", { field: String(field) });
|
|
163
|
+
}
|
|
164
|
+
const parsed = host.parsedTypeOf(Object.hasOwn(flat, field) ? flat[field] : void 0);
|
|
165
|
+
if (parsed === void 0 || !host.isStringValuedBase(parsed.base)) {
|
|
166
|
+
throw invalid(`rerank.fields gives "${field}", which the schema does not declare as a text field`, { field });
|
|
167
|
+
}
|
|
168
|
+
if (seen.has(field)) {
|
|
169
|
+
throw invalid(`rerank.fields gives "${field}" more than once`, { field });
|
|
170
|
+
}
|
|
171
|
+
seen.add(field);
|
|
172
|
+
checked.push(field);
|
|
173
|
+
}
|
|
174
|
+
return checked;
|
|
175
|
+
}
|
|
176
|
+
function requireWindow(window, maxWindow, hybridDepth) {
|
|
177
|
+
if (window === void 0) return Math.min(DEFAULT_RERANK_WINDOW, maxWindow, hybridDepth ?? maxWindow);
|
|
178
|
+
if (typeof window !== "number" || !Number.isInteger(window) || window < MIN_RERANK_WINDOW || window > maxWindow) {
|
|
179
|
+
throw invalid(`rerank.window must be a whole number from ${MIN_RERANK_WINDOW} to ${maxWindow}`, {
|
|
180
|
+
window: typeof window === "number" ? window : String(window),
|
|
181
|
+
maxRerankWindow: maxWindow
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
if (hybridDepth !== void 0 && window > hybridDepth) {
|
|
185
|
+
throw invalid(
|
|
186
|
+
`rerank.window is ${window}, above the hybrid.depth of ${hybridDepth} that a hybrid query reranks from`,
|
|
187
|
+
{
|
|
188
|
+
window,
|
|
189
|
+
depth: hybridDepth
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
return window;
|
|
194
|
+
}
|
|
195
|
+
function resolveRerankParams(params, schema, maxWindow, hybridDepth, host) {
|
|
196
|
+
const rerank = params.rerank;
|
|
197
|
+
if (typeof rerank !== "object" || rerank === null || Array.isArray(rerank)) {
|
|
198
|
+
throw invalid("rerank must be an object that gives an adapter and the fields to rerank by");
|
|
199
|
+
}
|
|
200
|
+
if (host.normalizeSort(params.sort).length > 0) {
|
|
201
|
+
throw new NarsilError(
|
|
202
|
+
ErrorCodes.SEARCH_INVALID_MODE,
|
|
203
|
+
"A query cannot carry both rerank and a sort, because each of them sets the order of the hits"
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
const { adapter, fields, window } = rerank;
|
|
207
|
+
if (typeof adapter !== "string" || adapter.length === 0) {
|
|
208
|
+
throw invalid("rerank.adapter must give the name of a registered rerank adapter");
|
|
209
|
+
}
|
|
210
|
+
const checkedFields = requireTextFields(fields, schema, host);
|
|
211
|
+
const checkedWindow = requireWindow(window, maxWindow, hybridDepth);
|
|
212
|
+
const query = rerankQueryTextOf(params);
|
|
213
|
+
if (query === void 0) {
|
|
214
|
+
throw invalid("A reranked query needs text to compare each hit with: set rerank.query, term, or vector.text");
|
|
215
|
+
}
|
|
216
|
+
return { adapter, fields: checkedFields, query, window: checkedWindow };
|
|
217
|
+
}
|
|
218
|
+
function rerankTextOf(values) {
|
|
219
|
+
if (values === void 0) return "";
|
|
220
|
+
const lines = [];
|
|
221
|
+
for (const value of values) {
|
|
222
|
+
if (typeof value === "string") {
|
|
223
|
+
lines.push(value);
|
|
224
|
+
} else if (Array.isArray(value)) {
|
|
225
|
+
for (const member of value) if (typeof member === "string") lines.push(member);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return lines.join("\n");
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/rerank/pipeline.ts
|
|
232
|
+
function rerankCursorDepth(cursor, binding, host) {
|
|
233
|
+
const decoded = host.decodePageCursor(cursor);
|
|
234
|
+
host.requireMatchingCursor(decoded, cursor, null, true, binding());
|
|
235
|
+
return decoded.depth ?? 0;
|
|
236
|
+
}
|
|
237
|
+
function requireWithinReach(cursorDepth, offset, limit) {
|
|
238
|
+
const reach = cursorDepth + offset + limit;
|
|
239
|
+
if (reach <= RESULT_WINDOW) return;
|
|
240
|
+
throw new NarsilError(
|
|
241
|
+
ErrorCodes.SEARCH_RESULT_WINDOW_EXCEEDED,
|
|
242
|
+
`The engine pages a reranked query through the first ${RESULT_WINDOW} results, and this page ends at ${reach}`,
|
|
243
|
+
{ limit, offset, cursorDepth, window: RESULT_WINDOW }
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
function reorderByRerank(ranked, windowSize, scores, compareCodePoints) {
|
|
247
|
+
let initialLow = Number.POSITIVE_INFINITY;
|
|
248
|
+
let initialHigh = Number.NEGATIVE_INFINITY;
|
|
249
|
+
let finalLow = Number.POSITIVE_INFINITY;
|
|
250
|
+
let finalHigh = Number.NEGATIVE_INFINITY;
|
|
251
|
+
const ordered = new Array(windowSize);
|
|
252
|
+
for (let i = 0; i < windowSize; i++) {
|
|
253
|
+
const first = ranked[i].score ?? 0;
|
|
254
|
+
const score = scores[i];
|
|
255
|
+
if (first < initialLow) initialLow = first;
|
|
256
|
+
if (first > initialHigh) initialHigh = first;
|
|
257
|
+
if (score < finalLow) finalLow = score;
|
|
258
|
+
if (score > finalHigh) finalHigh = score;
|
|
259
|
+
ordered[i] = { ...ranked[i], score };
|
|
260
|
+
}
|
|
261
|
+
ordered.sort((a, b) => (b.score ?? 0) - (a.score ?? 0) || compareCodePoints(a.id, b.id));
|
|
262
|
+
const rescale = initialLow > finalLow;
|
|
263
|
+
const scale = rescale ? Math.max(finalHigh - finalLow, 1) / Math.max(initialHigh - initialLow, 1) : 1;
|
|
264
|
+
const bias = rescale ? finalLow - initialLow * scale : 0;
|
|
265
|
+
for (let i = windowSize; i < ranked.length; i++) {
|
|
266
|
+
const hit = ranked[i];
|
|
267
|
+
ordered.push(rescale ? { ...hit, score: (hit.score ?? 0) * scale + bias } : hit);
|
|
268
|
+
}
|
|
269
|
+
return ordered;
|
|
270
|
+
}
|
|
271
|
+
function firstStageDepth(hybridDepth, window, cursorDepth, offset, limit) {
|
|
272
|
+
if (hybridDepth === void 0) {
|
|
273
|
+
requireWithinReach(cursorDepth, offset, limit);
|
|
274
|
+
return Math.max(window + 1, Math.min(cursorDepth + offset + limit + 1, RESULT_WINDOW));
|
|
275
|
+
}
|
|
276
|
+
requireWithinHybridDepth(cursorDepth, offset, hybridDepth);
|
|
277
|
+
return hybridDepth;
|
|
278
|
+
}
|
|
279
|
+
async function runRerankedQuery(request, io) {
|
|
280
|
+
const fuses = io.fusesRankings(request);
|
|
281
|
+
const hybridDepth = fuses ? fixedHybridDepthOf(request.hybrid) : void 0;
|
|
282
|
+
const { host } = io;
|
|
283
|
+
const plan = resolveRerankParams(request, io.schema, io.maxWindow, hybridDepth, host);
|
|
284
|
+
const limit = host.clampRowCount(request.limit, DEFAULT_LIMIT);
|
|
285
|
+
const offset = host.clampRowCount(request.offset, DEFAULT_OFFSET);
|
|
286
|
+
const cursorDepth = request.searchAfter === void 0 ? 0 : rerankCursorDepth(request.searchAfter, io.binding, host);
|
|
287
|
+
const depth = firstStageDepth(hybridDepth, plan.window, cursorDepth, offset, limit);
|
|
288
|
+
const projection = host.resolveProjection(request.document);
|
|
289
|
+
const { rerank: _rerank, pinned: _pinned, searchAfter: _searchAfter, highlight: _highlight, ...firstParams } = request;
|
|
290
|
+
const placesPins = request.searchAfter === void 0 && request.pinned !== void 0;
|
|
291
|
+
const countsApart = placesPins || request.vector !== void 0 && !fuses;
|
|
292
|
+
const [first, plain] = await Promise.all([
|
|
293
|
+
io.firstStage({ ...firstParams, offset: 0, limit: depth, document: false }),
|
|
294
|
+
countsApart ? io.firstStage({
|
|
295
|
+
...firstParams,
|
|
296
|
+
...placesPins ? { pinned: request.pinned } : {},
|
|
297
|
+
facets: void 0,
|
|
298
|
+
group: void 0,
|
|
299
|
+
offset: cursorDepth + offset,
|
|
300
|
+
limit,
|
|
301
|
+
document: false
|
|
302
|
+
}) : void 0
|
|
303
|
+
]);
|
|
304
|
+
const ranked = first.hits;
|
|
305
|
+
let pins = placesPins ? request.pinned : void 0;
|
|
306
|
+
if (pins !== void 0) {
|
|
307
|
+
const stored = await io.storedPins(host.dedupePinnedEntries(pins).map((entry) => entry.docId));
|
|
308
|
+
const clamps = io.clampsPins || first.countExact && ranked.length >= first.count;
|
|
309
|
+
pins = pins.filter((entry) => stored.has(entry.docId) && (clamps || entry.position < depth));
|
|
310
|
+
}
|
|
311
|
+
const placedPins = pins;
|
|
312
|
+
const place = (hits) => placedPins === void 0 ? hits : host.placePinned(hits, placedPins, (docId) => ({ id: docId, score: 0, document: void 0 })).hits;
|
|
313
|
+
const start = cursorDepth + offset;
|
|
314
|
+
const windowSize = Math.min(plan.window, ranked.length);
|
|
315
|
+
const pageHoldsHits = limit > 0 && start < place(ranked).length;
|
|
316
|
+
let ordered = ranked;
|
|
317
|
+
if (windowSize > 0 && pageHoldsHits) {
|
|
318
|
+
const texts = await io.texts(
|
|
319
|
+
ranked.slice(0, windowSize).map((hit) => hit.id),
|
|
320
|
+
plan.fields
|
|
321
|
+
);
|
|
322
|
+
ordered = reorderByRerank(
|
|
323
|
+
ranked,
|
|
324
|
+
windowSize,
|
|
325
|
+
await io.score(plan.adapter, plan.query, texts),
|
|
326
|
+
host.compareCodePoints
|
|
327
|
+
);
|
|
328
|
+
} else {
|
|
329
|
+
await io.score(plan.adapter, plan.query, []);
|
|
330
|
+
}
|
|
331
|
+
const pinnedIds = placedPins === void 0 ? void 0 : new Set(placedPins.map((entry) => entry.docId));
|
|
332
|
+
const { page, cursor } = pageByPosition(place(ordered), ordered, {
|
|
333
|
+
start,
|
|
334
|
+
limit,
|
|
335
|
+
pinnedIds,
|
|
336
|
+
binding: io.binding,
|
|
337
|
+
encodeCursor: host.encodePageCursor
|
|
338
|
+
});
|
|
339
|
+
await io.fillDocuments(page, first.groups, projection);
|
|
340
|
+
if (request.highlight) io.highlight?.(page, projection);
|
|
341
|
+
return {
|
|
342
|
+
hits: page,
|
|
343
|
+
count: (plain ?? first).count,
|
|
344
|
+
countExact: (plain ?? first).countExact,
|
|
345
|
+
elapsed: 0,
|
|
346
|
+
cursor,
|
|
347
|
+
facets: first.facets,
|
|
348
|
+
groups: first.groups,
|
|
349
|
+
coverage: first.coverage
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// src/engine/query/rerank.ts
|
|
354
|
+
async function executeRerankedQuery(request, context, firstStage, host) {
|
|
355
|
+
const startTime = host.now();
|
|
356
|
+
const runner = context.rerank;
|
|
357
|
+
if (runner === void 0) {
|
|
358
|
+
throw new NarsilError(ErrorCodes.RERANK_CONFIG_INVALID, "This search path holds no rerank adapters");
|
|
359
|
+
}
|
|
360
|
+
const { manager } = context;
|
|
361
|
+
const result = await runRerankedQuery(request, {
|
|
362
|
+
host,
|
|
363
|
+
schema: context.config.schema,
|
|
364
|
+
maxWindow: runner.maxWindow,
|
|
365
|
+
binding: context.cursorBinding,
|
|
366
|
+
clampsPins: true,
|
|
367
|
+
fusesRankings: (params) => params.vector !== void 0 && (params.mode === "hybrid" || params.term !== void 0 && params.term.trim().length > 0),
|
|
368
|
+
firstStage: (params) => firstStage(params, context),
|
|
369
|
+
async texts(docIds, fields) {
|
|
370
|
+
return docIds.map((docId) => rerankTextOf(manager.fieldValues(docId, fields)));
|
|
371
|
+
},
|
|
372
|
+
async storedPins(docIds) {
|
|
373
|
+
return new Set(docIds.filter((docId) => manager.getRef(docId) !== void 0));
|
|
374
|
+
},
|
|
375
|
+
score: (adapter, query, documents) => runner.score(adapter, query, documents),
|
|
376
|
+
async fillDocuments(hits, groups, projection) {
|
|
377
|
+
const read = (docId) => projection.kind === "none" ? {} : manager.get(docId, projection) ?? {};
|
|
378
|
+
for (const hit of hits) hit.document = read(hit.id);
|
|
379
|
+
for (const group of groups ?? []) {
|
|
380
|
+
for (const hit of group.hits) hit.document = read(hit.id);
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
highlight(page, projection) {
|
|
384
|
+
const fields = request.highlight?.fields ?? [];
|
|
385
|
+
const dropped = fields.some((field) => !host.projectionKeepsField(projection, field));
|
|
386
|
+
const readStoredDocument = dropped ? (docId) => manager.get(docId, { kind: "full" }) : void 0;
|
|
387
|
+
host.applyHighlights(
|
|
388
|
+
page,
|
|
389
|
+
request,
|
|
390
|
+
context.language,
|
|
391
|
+
manager.analysis,
|
|
392
|
+
manager.schema,
|
|
393
|
+
readStoredDocument,
|
|
394
|
+
manager.synonyms
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
result.elapsed = host.now() - startTime;
|
|
399
|
+
return result;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// src/rerank/call.ts
|
|
403
|
+
function unregistered(adapters, name) {
|
|
404
|
+
const registered = [...adapters.keys()].sort();
|
|
405
|
+
return new NarsilError(
|
|
406
|
+
ErrorCodes.RERANK_CONFIG_INVALID,
|
|
407
|
+
`The query gives rerank adapter "${name}", which this engine has not registered. Register it with registerRerankAdapter or the rerankAdapters setting`,
|
|
408
|
+
{ adapter: name, registered }
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
function scoresOf(raw, expected, name) {
|
|
412
|
+
const list = Array.isArray(raw) ? raw : ArrayBuffer.isView(raw) && !(raw instanceof DataView) ? Array.from(raw) : null;
|
|
413
|
+
if (list === null) {
|
|
414
|
+
throw new NarsilError(
|
|
415
|
+
ErrorCodes.RERANK_FAILED,
|
|
416
|
+
`Rerank adapter "${name}" returned ${typeof raw}, expected a list of scores`,
|
|
417
|
+
{
|
|
418
|
+
adapter: name
|
|
419
|
+
}
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
if (list.length !== expected) {
|
|
423
|
+
throw new NarsilError(
|
|
424
|
+
ErrorCodes.RERANK_FAILED,
|
|
425
|
+
`Rerank adapter "${name}" returned ${list.length} scores for ${expected} documents`,
|
|
426
|
+
{ adapter: name, expected, actual: list.length }
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
const scores = new Array(expected);
|
|
430
|
+
for (let i = 0; i < expected; i++) {
|
|
431
|
+
const score = list[i];
|
|
432
|
+
if (typeof score !== "number" || !Number.isFinite(score)) {
|
|
433
|
+
throw new NarsilError(
|
|
434
|
+
ErrorCodes.RERANK_FAILED,
|
|
435
|
+
`Rerank adapter "${name}" returned a score that is not a finite number at position ${i}`,
|
|
436
|
+
{ adapter: name, position: i }
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
scores[i] = score;
|
|
440
|
+
}
|
|
441
|
+
return scores;
|
|
442
|
+
}
|
|
443
|
+
function requireRerankAdapter(adapters, name) {
|
|
444
|
+
const adapter = adapters.get(name);
|
|
445
|
+
if (adapter === void 0) throw unregistered(adapters, name);
|
|
446
|
+
return adapter;
|
|
447
|
+
}
|
|
448
|
+
function timedOut(name, timeoutMs) {
|
|
449
|
+
return new NarsilError(
|
|
450
|
+
ErrorCodes.RERANK_FAILED,
|
|
451
|
+
`Rerank adapter "${name}" did not answer within ${timeoutMs} ms, so the engine cancelled the call`,
|
|
452
|
+
{ adapter: name, timeoutMs }
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
function cancelled(name) {
|
|
456
|
+
return new NarsilError(ErrorCodes.RERANK_FAILED, `The engine cancelled the call to rerank adapter "${name}"`, {
|
|
457
|
+
adapter: name
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
function wrapped(err, name) {
|
|
461
|
+
if (err instanceof NarsilError && err.code === ErrorCodes.RERANK_FAILED) return err;
|
|
462
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
463
|
+
return new NarsilError(ErrorCodes.RERANK_FAILED, `Rerank adapter "${name}" failed: ${message}`, {
|
|
464
|
+
adapter: name,
|
|
465
|
+
cause: message,
|
|
466
|
+
...err instanceof NarsilError ? { causeCode: err.code } : {}
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
async function callRerankAdapter(adapters, name, query, documents, options) {
|
|
470
|
+
const adapter = requireRerankAdapter(adapters, name);
|
|
471
|
+
if (documents.length === 0) return [];
|
|
472
|
+
const outer = options.signal;
|
|
473
|
+
if (outer?.aborted) throw cancelled(name);
|
|
474
|
+
const controller = new AbortController();
|
|
475
|
+
let timer;
|
|
476
|
+
let detach;
|
|
477
|
+
const stopped = new Promise((_resolve, reject) => {
|
|
478
|
+
timer = setTimeout(() => {
|
|
479
|
+
controller.abort();
|
|
480
|
+
reject(timedOut(name, options.timeoutMs));
|
|
481
|
+
}, options.timeoutMs);
|
|
482
|
+
if (outer !== void 0) {
|
|
483
|
+
const onAbort = () => {
|
|
484
|
+
controller.abort();
|
|
485
|
+
reject(cancelled(name));
|
|
486
|
+
};
|
|
487
|
+
outer.addEventListener("abort", onAbort, { once: true });
|
|
488
|
+
detach = () => outer.removeEventListener("abort", onAbort);
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
let raw;
|
|
492
|
+
try {
|
|
493
|
+
raw = await Promise.race([
|
|
494
|
+
Promise.resolve().then(() => adapter.rerank(query, documents, controller.signal)),
|
|
495
|
+
stopped
|
|
496
|
+
]);
|
|
497
|
+
} catch (err) {
|
|
498
|
+
throw wrapped(err, name);
|
|
499
|
+
} finally {
|
|
500
|
+
clearTimeout(timer);
|
|
501
|
+
detach?.();
|
|
502
|
+
}
|
|
503
|
+
return scoresOf(raw, documents.length, name);
|
|
504
|
+
}
|
|
505
|
+
function rerankOnHost(host, name, query, documents) {
|
|
506
|
+
return callRerankAdapter(host.rerankAdapters, name, query, documents, {
|
|
507
|
+
timeoutMs: host.rerankSettings.timeoutMs,
|
|
508
|
+
signal: host.abortController.signal
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// src/rerank/runner.ts
|
|
513
|
+
function rerankRunnerFor(core) {
|
|
514
|
+
return {
|
|
515
|
+
maxWindow: core.rerankSettings.maxWindow,
|
|
516
|
+
score: (adapter, query, documents) => rerankOnHost(core, adapter, query, documents)
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// src/rerank/settings.ts
|
|
521
|
+
function wholeNumberIn(value, low, high) {
|
|
522
|
+
return typeof value === "number" && Number.isInteger(value) && value >= low && value <= high;
|
|
523
|
+
}
|
|
524
|
+
function resolveRerankSettings(config) {
|
|
525
|
+
const timeout = config?.rerankTimeout;
|
|
526
|
+
const maxWindow = config?.maxRerankWindow;
|
|
527
|
+
if (timeout !== void 0 && !wholeNumberIn(timeout, MIN_RERANK_TIMEOUT_MS, MAX_RERANK_TIMEOUT_MS)) {
|
|
528
|
+
throw new NarsilError(
|
|
529
|
+
ErrorCodes.CONFIG_INVALID,
|
|
530
|
+
`rerankTimeout must be a whole number of milliseconds from ${MIN_RERANK_TIMEOUT_MS} to ${MAX_RERANK_TIMEOUT_MS}`,
|
|
531
|
+
{ rerankTimeout: timeout }
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
if (maxWindow !== void 0 && !wholeNumberIn(maxWindow, MIN_RERANK_WINDOW, MAX_RERANK_WINDOW)) {
|
|
535
|
+
throw new NarsilError(
|
|
536
|
+
ErrorCodes.CONFIG_INVALID,
|
|
537
|
+
`maxRerankWindow must be a whole number from ${MIN_RERANK_WINDOW} to ${MAX_RERANK_WINDOW}`,
|
|
538
|
+
{
|
|
539
|
+
maxRerankWindow: maxWindow
|
|
540
|
+
}
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
return {
|
|
544
|
+
timeoutMs: timeout ?? DEFAULT_RERANK_TIMEOUT_MS,
|
|
545
|
+
maxWindow: maxWindow ?? DEFAULT_MAX_RERANK_WINDOW
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
async function shutdownRerankAdapters(adapters) {
|
|
549
|
+
const distinct = new Set(adapters.values());
|
|
550
|
+
adapters.clear();
|
|
551
|
+
for (const adapter of distinct) {
|
|
552
|
+
try {
|
|
553
|
+
await adapter.shutdown?.();
|
|
554
|
+
} catch {
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export { executeRerankedQuery, rerankOnHost, rerankRunnerFor, resolveRerankSettings, searchFixedDepthHybrid, shutdownRerankAdapters, withRerankQueryText };
|