@delali/narsil 0.3.1-next.145 → 0.3.1-next.146

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/dist/adapters/filesystem.mjs +2 -2
  2. package/dist/adapters/gcs.mjs +2 -2
  3. package/dist/adapters/indexeddb.mjs +1 -1
  4. package/dist/adapters/s3.mjs +2 -2
  5. package/dist/analysis-change-ZCJNYBJM.mjs +39 -0
  6. package/dist/{analysis-change-entry-V4SQQSXQ.mjs → analysis-change-entry-YN3ELL6C.mjs} +1 -1
  7. package/dist/browser/{analysis-change-QTCQ5ETU.mjs → analysis-change-M65AZQQG.mjs} +8 -5
  8. package/dist/browser/{analysis-change-entry-KYEQSP3I.mjs → analysis-change-entry-H3YIOLXZ.mjs} +1 -1
  9. package/dist/browser/{chunk-GP4QLUQM.mjs → chunk-2JABMB4S.mjs} +10 -6
  10. package/dist/browser/{chunk-5WYWLTM5.mjs → chunk-4DYLCICG.mjs} +2 -2
  11. package/dist/browser/{chunk-75PMIFQA.mjs → chunk-4RD3AFSB.mjs} +1 -1
  12. package/dist/browser/{chunk-XWMEGKDC.mjs → chunk-5V74FFZE.mjs} +2 -2
  13. package/dist/browser/{chunk-PMBSBSCN.mjs → chunk-6OWX3OP2.mjs} +2 -2
  14. package/dist/browser/chunk-BRWY3JCN.mjs +678 -0
  15. package/dist/browser/{chunk-ZALEKEUH.mjs → chunk-BVX2MEHA.mjs} +306 -607
  16. package/dist/browser/{chunk-FXPMF65P.mjs → chunk-D6UFGBF5.mjs} +15 -45
  17. package/dist/browser/{chunk-QNDWMPV2.mjs → chunk-IAXZAVYL.mjs} +25 -66
  18. package/dist/browser/chunk-JNOAKDUM.mjs +31 -0
  19. package/dist/browser/{chunk-BTMRV5MY.mjs → chunk-KAEVGLYN.mjs} +2 -2
  20. package/dist/browser/{chunk-FHO5N7FD.mjs → chunk-LURXVCHG.mjs} +1 -1
  21. package/dist/browser/chunk-MPK76GVS.mjs +63 -0
  22. package/dist/browser/{chunk-6OFS7RUN.mjs → chunk-NVEJJP4R.mjs} +1 -1
  23. package/dist/browser/{chunk-U77XQJOY.mjs → chunk-ODQP7P3H.mjs} +455 -66
  24. package/dist/browser/{chunk-IFGTRRPU.mjs → chunk-PIS7ZAP7.mjs} +3 -25
  25. package/dist/browser/{chunk-XIHLIMFX.mjs → chunk-QMNZFFD3.mjs} +4 -3
  26. package/dist/browser/{chunk-AOKLYVTT.mjs → chunk-SRL47USS.mjs} +2 -2
  27. package/dist/browser/chunk-TY7L6OXO.mjs +69 -0
  28. package/dist/browser/{chunk-LGJ6XMH5.mjs → chunk-W643QV3L.mjs} +6 -5
  29. package/dist/browser/chunk-WXLBZJMT.mjs +25 -0
  30. package/dist/browser/{chunk-ZKVDQ5ZH.mjs → chunk-XILIT5MT.mjs} +8 -8
  31. package/dist/browser/{cold-module-Y2IJAP7S.mjs → cold-module-A5S6JFDN.mjs} +20 -20
  32. package/dist/browser/{durability-wiring-MLXZ5QQF.mjs → durability-wiring-W5AVV3RF.mjs} +53 -30
  33. package/dist/browser/gzip-5S7ROJFJ.mjs +1 -0
  34. package/dist/browser/import-PTT63AGK.mjs +12 -0
  35. package/dist/browser/{module-5LMDGXQH.mjs → module-CEKA7MQH.mjs} +13 -10
  36. package/dist/browser/module-X5PGOE47.mjs +4288 -0
  37. package/dist/browser/{phrase-module-KXNRTWCS.mjs → phrase-module-CNKMUY4U.mjs} +2 -1
  38. package/dist/browser/pool-DTYSPBMQ.mjs +296 -0
  39. package/dist/{restore-queue-UAVGBSJG.mjs → browser/restore-queue-DD4LWZK7.mjs} +2 -2
  40. package/dist/browser/{runtime-ZNEGK33G.mjs → runtime-D2ULZGCN.mjs} +3 -2
  41. package/dist/browser/{snapshot-2EGS2FYU.mjs → snapshot-DQ6A4PQP.mjs} +18 -14
  42. package/dist/browser/{write-WMZA6NSF.mjs → write-MAGRAIEH.mjs} +14 -11
  43. package/dist/{capabilities-CNnzGtqP.d.ts → capabilities-BT-HLDC8.d.ts} +2 -2
  44. package/dist/{chunk-FRJ5RGX7.mjs → chunk-2HJWQPR6.mjs} +2 -2
  45. package/dist/{chunk-DKMZ7JFW.mjs → chunk-3BVGJ4JR.mjs} +10 -4
  46. package/dist/{chunk-QYMAYU7M.mjs → chunk-3HV7M5OJ.mjs} +155 -149
  47. package/dist/chunk-3MXV3VHR.mjs +46 -0
  48. package/dist/{chunk-6FOJKNUR.mjs → chunk-3SBJVQLL.mjs} +2 -2
  49. package/dist/{chunk-PRID7CA6.mjs → chunk-3TVDTACC.mjs} +1 -1
  50. package/dist/{chunk-RTVAWNUL.mjs → chunk-5WB2A3XU.mjs} +9 -6
  51. package/dist/{chunk-LWFK3TC2.mjs → chunk-65WCV5JO.mjs} +1 -1
  52. package/dist/{chunk-G6Q5DEF5.mjs → chunk-6BWLDLS3.mjs} +3 -46
  53. package/dist/{chunk-W6M4RRAT.mjs → chunk-6C5EX2QR.mjs} +3 -3
  54. package/dist/{chunk-HOC5JP2H.mjs → chunk-6F6USYII.mjs} +119 -11
  55. package/dist/{chunk-7523VCM4.mjs → chunk-7HKWO4BJ.mjs} +7 -7
  56. package/dist/{chunk-ONDEEBTA.mjs → chunk-7N223GWV.mjs} +2 -2
  57. package/dist/{chunk-UGQULMSU.mjs → chunk-ADBS5XRI.mjs} +10 -10
  58. package/dist/{chunk-MJTEU2XK.mjs → chunk-AWNHDBKV.mjs} +429 -73
  59. package/dist/{chunk-2E2ZFOPT.mjs → chunk-B2FTZNNF.mjs} +2 -2
  60. package/dist/{chunk-NWTEJ2T7.mjs → chunk-B7XIOAMK.mjs} +3 -3
  61. package/dist/{chunk-W55X2BYN.mjs → chunk-BEZH7VEH.mjs} +1 -1
  62. package/dist/chunk-BTPGQC6Z.mjs +27 -0
  63. package/dist/{chunk-KG5N7LE6.mjs → chunk-CYYSZPVV.mjs} +11 -11
  64. package/dist/{chunk-HTMUGEFU.mjs → chunk-DOY6EG76.mjs} +6 -6
  65. package/dist/{chunk-UHVTEVF5.mjs → chunk-F22IOCR2.mjs} +10 -6
  66. package/dist/{chunk-FTQLF5JI.mjs → chunk-IRWP524D.mjs} +1 -1
  67. package/dist/{chunk-XLAI4OIO.mjs → chunk-IZT3G24Q.mjs} +2 -2
  68. package/dist/{chunk-SE7MKJVK.mjs → chunk-J7M73VY7.mjs} +1 -1
  69. package/dist/chunk-KAKQ74LS.mjs +53 -0
  70. package/dist/{chunk-GVPYMSLO.mjs → chunk-KYVRZAYP.mjs} +1 -1
  71. package/dist/{chunk-F442VZUL.mjs → chunk-KZ5GQE4Z.mjs} +5 -5
  72. package/dist/{chunk-WDEWU46C.mjs → chunk-KZQIVGW7.mjs} +2 -2
  73. package/dist/{chunk-7AJAW6NN.mjs → chunk-L7TIAYPX.mjs} +8 -8
  74. package/dist/{chunk-GRFXSEFF.mjs → chunk-LDGGR7YT.mjs} +4 -81
  75. package/dist/{chunk-WMIV7PJ7.mjs → chunk-LJ4ENXOD.mjs} +15 -14
  76. package/dist/chunk-LQG5G2LY.mjs +39 -0
  77. package/dist/{chunk-IQHZMCLX.mjs → chunk-MDF6PDIA.mjs} +2 -0
  78. package/dist/{chunk-QXH2CS4V.mjs → chunk-NVTFQFUL.mjs} +7 -26
  79. package/dist/{chunk-5K2JHKM3.mjs → chunk-NYSONUB7.mjs} +1 -1
  80. package/dist/{chunk-OH7SX5MY.mjs → chunk-O4KUNDVF.mjs} +12 -10
  81. package/dist/{chunk-EBIHRL7B.mjs → chunk-Q2UL2TPC.mjs} +1 -1
  82. package/dist/{chunk-RNUP6WZ6.mjs → chunk-QEYF2SQ6.mjs} +2 -2
  83. package/dist/{chunk-RVBUQITB.mjs → chunk-QN5WVV5K.mjs} +7 -7
  84. package/dist/{chunk-RIIUZJQ3.mjs → chunk-QYRNLLLI.mjs} +1 -1
  85. package/dist/{chunk-BTARFTEN.mjs → chunk-SOUZMPME.mjs} +4 -4
  86. package/dist/{chunk-ISUOSACK.mjs → chunk-T4CJDBBE.mjs} +1 -1
  87. package/dist/{chunk-DHEJAGVW.mjs → chunk-TTCKT2RT.mjs} +5 -5
  88. package/dist/{chunk-QU2XF7VI.mjs → chunk-U2726TL5.mjs} +5 -4
  89. package/dist/{chunk-LYKUJNAV.mjs → chunk-U46IZNCW.mjs} +2 -2
  90. package/dist/{chunk-SNB4DYL3.mjs → chunk-U6NE6RCV.mjs} +4 -4
  91. package/dist/{chunk-SA52N7G3.mjs → chunk-VP6XRMWR.mjs} +4 -4
  92. package/dist/{chunk-YUCGFPMS.mjs → chunk-VQDZVWN3.mjs} +449 -405
  93. package/dist/{chunk-IWVEG5CX.mjs → chunk-XIHUI4KB.mjs} +2 -2
  94. package/dist/{chunk-IV2XTSS2.mjs → chunk-XKPNKR3V.mjs} +16 -46
  95. package/dist/{chunk-UQS5PAS5.mjs → chunk-YGQOPB2Z.mjs} +1 -1
  96. package/dist/{chunk-SVO6NV4D.mjs → chunk-YKEHQUSZ.mjs} +4 -4
  97. package/dist/{chunk-6Z22L5RN.mjs → chunk-ZAYWOHVG.mjs} +1 -1
  98. package/dist/{chunk-QLDLQW36.mjs → chunk-ZEKDO7YX.mjs} +2 -2
  99. package/dist/chunk-ZXNNR73R.mjs +31 -0
  100. package/dist/{chunk-VJS4JFBQ.mjs → chunk-ZXX5GZEX.mjs} +3 -1
  101. package/dist/{chunk-X5SBWARI.mjs → chunk-ZYKUD5NX.mjs} +9 -7
  102. package/dist/{client-DoYPHcUn.d.ts → client-Cn3gNxUU.d.ts} +3 -3
  103. package/dist/client.d.ts +5 -5
  104. package/dist/client.mjs +2 -0
  105. package/dist/{cold-module-NUD332YD.mjs → cold-module-2WWGBFJC.mjs} +36 -33
  106. package/dist/{config-CuUfls_H.d.ts → config-CfhH1IWQ.d.ts} +1 -1
  107. package/dist/{config-Dzs46im6.d.ts → config-aIsy7mcL.d.ts} +52 -1
  108. package/dist/direct-executor-QLVHMELJ.mjs +24 -0
  109. package/dist/distribution/coordinator/etcd.mjs +1 -1
  110. package/dist/distribution/transport/grpc.mjs +1 -1
  111. package/dist/distribution.d.ts +4 -4
  112. package/dist/distribution.mjs +150 -255
  113. package/dist/{durability-wiring-K7RDXWL6.mjs → durability-wiring-GNWQSZI3.mjs} +67 -42
  114. package/dist/embeddings/openai.mjs +2 -2
  115. package/dist/{engine-C0ifUEcm.d.ts → engine-CJRUHXgf.d.ts} +1 -1
  116. package/dist/gzip-F4HMIEEQ.mjs +1 -0
  117. package/dist/import-DX55PN4G.mjs +41 -0
  118. package/dist/index.browser.mjs +7 -4
  119. package/dist/index.d.ts +539 -5
  120. package/dist/index.mjs +22 -16
  121. package/dist/index.react-native.mjs +19 -0
  122. package/dist/{long-query-thread-FKVHRCHF.mjs → long-query-thread-SCUE62TO.mjs} +20 -17
  123. package/dist/{module-GJUKW5VU.mjs → module-HW6IXUM6.mjs} +601 -108
  124. package/dist/{module-TMFIWOD2.mjs → module-V5K4ZP23.mjs} +29 -23
  125. package/dist/{paged-partition-archive-WNUEMYWM.mjs → paged-partition-archive-FDIIWZNR.mjs} +31 -25
  126. package/dist/partial-words.mjs +5 -5
  127. package/dist/pattern.mjs +3 -3
  128. package/dist/persistence/durability/checkpoint-worker.mjs +28 -22
  129. package/dist/{phrase-module-OMY3QQV7.mjs → phrase-module-OUQIRUQU.mjs} +1 -1
  130. package/dist/pool-UPV5PVPY.mjs +298 -0
  131. package/dist/react-native/chunks/analysis-change-HGW3L6NY.mjs +893 -0
  132. package/dist/react-native/chunks/analysis-change-entry-BNRU6STO.mjs +1 -0
  133. package/dist/react-native/chunks/chunk-2J2727V4.mjs +495 -0
  134. package/dist/react-native/chunks/chunk-3CTWSQRW.mjs +1251 -0
  135. package/dist/react-native/chunks/chunk-5FV4ZEVA.mjs +2619 -0
  136. package/dist/react-native/chunks/chunk-7VWQLEFV.mjs +642 -0
  137. package/dist/react-native/chunks/chunk-AAAETEJK.mjs +27 -0
  138. package/dist/react-native/chunks/chunk-AM7RSBPF.mjs +32 -0
  139. package/dist/react-native/chunks/chunk-B6OD63Z3.mjs +997 -0
  140. package/dist/react-native/chunks/chunk-BRNAVOKP.mjs +177 -0
  141. package/dist/react-native/chunks/chunk-DKJTZ625.mjs +207 -0
  142. package/dist/react-native/chunks/chunk-FKGG4UOF.mjs +177 -0
  143. package/dist/{browser/chunk-K7UFXEJS.mjs → react-native/chunks/chunk-GV5KFQ6M.mjs} +12 -6
  144. package/dist/react-native/chunks/chunk-HURV3ASY.mjs +4416 -0
  145. package/dist/react-native/chunks/chunk-IO2SMUQD.mjs +10 -0
  146. package/dist/react-native/chunks/chunk-JV6VQY2S.mjs +17 -0
  147. package/dist/react-native/chunks/chunk-KUJOBRKS.mjs +217 -0
  148. package/dist/react-native/chunks/chunk-KWRW5U65.mjs +226 -0
  149. package/dist/react-native/chunks/chunk-L4M7QJNP.mjs +110 -0
  150. package/dist/react-native/chunks/chunk-LQ6Q4L5B.mjs +360 -0
  151. package/dist/react-native/chunks/chunk-LQAKO5QV.mjs +523 -0
  152. package/dist/react-native/chunks/chunk-LRGWK367.mjs +11 -0
  153. package/dist/react-native/chunks/chunk-LVIKKAA6.mjs +93 -0
  154. package/dist/react-native/chunks/chunk-NFGBOI46.mjs +14 -0
  155. package/dist/react-native/chunks/chunk-NNE4VDMM.mjs +8 -0
  156. package/dist/react-native/chunks/chunk-PPEPTUVV.mjs +56 -0
  157. package/dist/react-native/chunks/chunk-PSWKQVVT.mjs +9 -0
  158. package/dist/react-native/chunks/chunk-PU55IOG4.mjs +910 -0
  159. package/dist/react-native/chunks/chunk-QC4ZRBSX.mjs +270 -0
  160. package/dist/react-native/chunks/chunk-R4EEMYM6.mjs +21 -0
  161. package/dist/react-native/chunks/chunk-R4OPUZQ5.mjs +86 -0
  162. package/dist/react-native/chunks/chunk-SLGIAD6B.mjs +2634 -0
  163. package/dist/react-native/chunks/chunk-TJMSKBEM.mjs +25 -0
  164. package/dist/react-native/chunks/chunk-TLYD7IJ2.mjs +23 -0
  165. package/dist/react-native/chunks/chunk-UFNCLEQM.mjs +73 -0
  166. package/dist/react-native/chunks/chunk-YH7W556M.mjs +179 -0
  167. package/dist/react-native/chunks/chunk-YJSMEKRE.mjs +22385 -0
  168. package/dist/react-native/chunks/chunk-ZCKNBPQ4.mjs +79 -0
  169. package/dist/react-native/chunks/cold-module-TSFWLYWC.mjs +755 -0
  170. package/dist/react-native/chunks/durability-wiring-PZR6KOQK.mjs +4509 -0
  171. package/dist/react-native/chunks/gzip.react-native-7JWCLVOB.mjs +3 -0
  172. package/dist/react-native/chunks/import-VTODLPLO.mjs +22 -0
  173. package/dist/{browser/module-Q6VFGLMG.mjs → react-native/chunks/module-JBBH6PZH.mjs} +609 -144
  174. package/dist/react-native/chunks/module-VEK2KCKU.mjs +25 -0
  175. package/dist/react-native/chunks/phrase-module-76UAPPSG.mjs +329 -0
  176. package/dist/react-native/chunks/pool-CD4ANYUI.mjs +296 -0
  177. package/dist/react-native/chunks/release-D7ZTIANM.mjs +33 -0
  178. package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
  179. package/dist/{browser/restore-queue-SOAHKZSQ.mjs → react-native/chunks/restore-queue-OI5EH7R3.mjs} +2 -2
  180. package/dist/react-native/chunks/runtime-3MCDLJAF.mjs +559 -0
  181. package/dist/react-native/chunks/snapshot-AUZMEV36.mjs +1205 -0
  182. package/dist/react-native/chunks/write-DDC2QBRJ.mjs +197 -0
  183. package/dist/react-native/partial-words.mjs +733 -0
  184. package/dist/react-native/pattern.mjs +2442 -0
  185. package/dist/react-native/synonyms.mjs +460 -0
  186. package/dist/react.d.ts +7 -7
  187. package/dist/react.mjs +2 -0
  188. package/dist/{rerank-ZZ5CHKLW.mjs → rerank-3W54N6P2.mjs} +4 -4
  189. package/dist/rerankers/http.mjs +2 -2
  190. package/dist/restore-queue-BQWWNNQI.mjs +41 -0
  191. package/dist/{runtime-RAOCKSYA.mjs → runtime-L2DZYY6N.mjs} +5 -5
  192. package/dist/server.d.ts +8 -8
  193. package/dist/server.mjs +32 -28
  194. package/dist/{snapshot-DB5ZVCDN.mjs → snapshot-EHXTOTW5.mjs} +39 -33
  195. package/dist/synonyms.mjs +3 -3
  196. package/dist/{tasks-ChWp0wrF.d.ts → tasks-C4ElXr4P.d.ts} +2 -2
  197. package/dist/thread-I7DVHHW7.mjs +31 -0
  198. package/dist/vector/search-worker.mjs +7 -5
  199. package/dist/workers/entry.mjs +9 -9
  200. package/dist/{write-KZVTLGCI.mjs → write-SDJXXQ2G.mjs} +29 -23
  201. package/package.json +88 -7
  202. package/dist/analysis-change-D5APH4Z3.mjs +0 -34
  203. package/dist/browser/import-FTMLTPGQ.mjs +0 -9
  204. package/dist/direct-executor-FDQYQEJQ.mjs +0 -22
  205. package/dist/import-6KXFY6XI.mjs +0 -35
  206. package/dist/thread-OV6T555H.mjs +0 -28
  207. /package/dist/{chunk-22YPZVMG.mjs → chunk-ZXNQEW43.mjs} +0 -0
@@ -0,0 +1,4416 @@
1
+ import { osqCodeBytes, osqStagedQueryBytes, osqRecordBytes, unpackLevels, readTrailer, stageQueryLevels, writeRecord } from './chunk-LQ6Q4L5B.mjs';
2
+ import { markSimdUnavailable, OSQ_STEPS, OSQ_GRID, OSQ_QUERY_BITS, OSQ_REFINE_ROUNDS, OSQ_REFINE_TOLERANCE, OSQ_PLACEMENT_QUERY_BITS, turnsAreBudgeted, holdWrites, yieldBackgroundTurn, OSQ_LAMBDA, threadSlotGone, backgroundTurnIsSpent } from './chunk-LQAKO5QV.mjs';
3
+ import { NATIVE_THREAD_SEARCH_SLOTS, VECTOR_SCRATCH_SLOTS, VECTOR_BLOCK_BYTES_ON_A_PHONE, MAX_DOC_ID_TABLE_BYTES_ON_A_PHONE, MAX_VECTOR_ORDINALS_ON_A_PHONE, WASM_PAGE_BYTES, UNPLACED_FLAG_CHUNK_ORDINALS, VECTOR_STORE_INITIAL_CAPACITY, VECTOR_BLOCK_INITIAL_SLOTS, MAX_WASM_PAGES, NATIVE_ATTACH_RETRY_MS, LOCK_SPIN_ITERATIONS, LOCK_WAIT_SLICE_MS, GRAPH_LOCK_WAIT_SLICE_MS, GRAPH_LOCK_WAIT_DEADLINE_MS } from './chunk-L4M7QJNP.mjs';
4
+ import { DEFAULT_EF_SEARCH, COMPACTION_TOMBSTONE_RATIO, COMPACTION_ABSOLUTE_THRESHOLD, PLACEMENT_ROOM_REQUESTS, MAX_LAYER_CAP, INITIAL_LIST_CAPACITY, INITIAL_UPPER_SLOTS, CONNECT_RESTARTS_BEFORE_ONE_PASS, FARTHEST_REJECTED_LINKS_KEPT, CONNECT_STEPS_BETWEEN_CLOCK_CHECKS, NEAREST_LINK_FALLBACK_HOSTS } from './chunk-TJMSKBEM.mjs';
5
+ import { compareCodePoints } from './chunk-PPEPTUVV.mjs';
6
+ import { reactNativeHost } from './chunk-NFGBOI46.mjs';
7
+ import { NarsilError, ErrorCodes } from './chunk-YH7W556M.mjs';
8
+
9
+ // src/vector/shared-buffers/memory-limits.react-native.ts
10
+ var VECTOR_MEMORY_LIMITS = {
11
+ maxOrdinals: MAX_VECTOR_ORDINALS_ON_A_PHONE,
12
+ maxDocIdTableBytes: MAX_DOC_ID_TABLE_BYTES_ON_A_PHONE,
13
+ blockBytes: VECTOR_BLOCK_BYTES_ON_A_PHONE
14
+ };
15
+ function reserveVectorRegion(bytes) {
16
+ return reactNativeHost().reserveMemory(bytes);
17
+ }
18
+ function allocateFixedRegion(bytes) {
19
+ return reactNativeHost().allocateMemory(bytes);
20
+ }
21
+ function hostOwnsMemory(buffer) {
22
+ return reactNativeHost().ownsMemory(buffer);
23
+ }
24
+
25
+ // src/vector/shared-buffers/region.ts
26
+ function reserve(maxBytes) {
27
+ try {
28
+ return reserveVectorRegion(maxBytes);
29
+ } catch (err) {
30
+ if (err instanceof NarsilError) throw err;
31
+ throw new NarsilError(
32
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
33
+ `The device could not reserve ${maxBytes} bytes of address space for a vector structure. Remove vector fields or indexes that the app no longer needs.`,
34
+ { bytes: maxBytes, cause: err instanceof Error ? err.message : String(err) }
35
+ );
36
+ }
37
+ }
38
+ function createRegionBuffer(initialBytes, maxBytes) {
39
+ const reserved = reserve(maxBytes);
40
+ if (reserved === null) return null;
41
+ const reservedBytes = reserved.byteLength;
42
+ let logicalBytes = Math.min(initialBytes, reservedBytes);
43
+ Object.defineProperties(reserved, {
44
+ byteLength: { get: () => logicalBytes },
45
+ maxByteLength: { value: reservedBytes },
46
+ resizable: { value: true },
47
+ resize: {
48
+ value(target) {
49
+ if (!Number.isInteger(target) || target < 0 || target > reservedBytes) {
50
+ throw new RangeError(`A vector region can hold between 0 and ${reservedBytes} bytes, not ${target}`);
51
+ }
52
+ if (target < logicalBytes) new Uint8Array(reserved, target, logicalBytes - target).fill(0);
53
+ logicalBytes = target;
54
+ }
55
+ }
56
+ });
57
+ return reserved;
58
+ }
59
+
60
+ // src/vector/shared-buffers/growable.ts
61
+ function sharedMemoryAvailable() {
62
+ return typeof SharedArrayBuffer === "function";
63
+ }
64
+ var resizableBuffersAvailable = typeof ArrayBuffer.prototype.resize === "function";
65
+ function createGrowableBuffer(initialBytes, maxBytes) {
66
+ const initial = Math.min(initialBytes, maxBytes);
67
+ if (sharedMemoryAvailable()) {
68
+ return new SharedArrayBuffer(initial, { maxByteLength: maxBytes });
69
+ }
70
+ const region = resizableBuffersAvailable ? null : createRegionBuffer(initial, maxBytes);
71
+ return region ?? new ArrayBuffer(initial, { maxByteLength: maxBytes });
72
+ }
73
+ function createFixedBuffer(bytes) {
74
+ if (sharedMemoryAvailable()) return new SharedArrayBuffer(bytes);
75
+ return allocateFixedRegion(bytes) ?? new ArrayBuffer(bytes);
76
+ }
77
+ function nextGrowthTarget(have, needed, max) {
78
+ return Math.min(max, Math.max(needed, have * 2));
79
+ }
80
+ function grownByteLength(have, needed, max, step = 1) {
81
+ let size = have;
82
+ while (size < needed) {
83
+ const next = Math.ceil(nextGrowthTarget(size, Math.min(needed, size + 1), max) / step) * step;
84
+ if (next <= size) return size;
85
+ size = next;
86
+ }
87
+ return size;
88
+ }
89
+ function growBufferTo(buffer, bytes) {
90
+ const held = buffer.byteLength;
91
+ if (held >= bytes) return;
92
+ resizeBuffer(buffer, bytes, nextGrowthTarget(held, bytes, buffer.maxByteLength));
93
+ }
94
+ function growBufferToExactly(buffer, bytes) {
95
+ if (buffer.byteLength >= bytes) return;
96
+ resizeBuffer(buffer, bytes, bytes);
97
+ }
98
+ function resizeBuffer(buffer, bytes, target) {
99
+ if (bytes > buffer.maxByteLength) {
100
+ throw new NarsilError(
101
+ ErrorCodes.PARTITION_CAPACITY_EXCEEDED,
102
+ `A vector structure needs ${bytes} bytes where it may hold at most ${buffer.maxByteLength}`,
103
+ { bytes, maxBytes: buffer.maxByteLength }
104
+ );
105
+ }
106
+ if (buffer instanceof ArrayBuffer) {
107
+ buffer.resize(target);
108
+ return;
109
+ }
110
+ try {
111
+ buffer.grow(target);
112
+ } catch (err) {
113
+ if (buffer.byteLength < bytes) throw err;
114
+ }
115
+ }
116
+ function fixedView(buffer, kind) {
117
+ return new kind(buffer, 0, Math.floor(buffer.byteLength / kind.BYTES_PER_ELEMENT));
118
+ }
119
+
120
+ // src/vector/hnsw/handles.ts
121
+ var GRAPH_ENTRY_POINT = 0;
122
+ var GRAPH_TOP_LAYER = 1;
123
+ var GRAPH_M = 2;
124
+ var GRAPH_MMAX0 = 3;
125
+ var GRAPH_EF_CONSTRUCTION = 4;
126
+ var GRAPH_METRIC = 5;
127
+ var GRAPH_RETIRED = 6;
128
+ var GRAPH_NODE_COUNT = 32;
129
+ var GRAPH_TOMBSTONE_COUNT = 33;
130
+ var GRAPH_UNLINKED_NODES = 34;
131
+ var GRAPH_INCOMING_UNCOUNTED = 35;
132
+ var GRAPH_UPPER_USED = 64;
133
+ var GRAPH_SLOTS = 65;
134
+ var GRAPH_UPPER_CAPACITY = 66;
135
+ var GRAPH_SLOT_CAPACITY = 67;
136
+ var GRAPH_LOCK = 96;
137
+ var GRAPH_WRITERS_WAITING = 97;
138
+ var GRAPH_ENTRY_LOCK = 128;
139
+ var GRAPH_HEADER_WORDS = 160;
140
+ var HELD_WRITE = 0;
141
+ var HELD_WRITE_VERSION = 1;
142
+ var HELD_GRAPH = 2;
143
+ var HELD_FENCE = 3;
144
+ var HELD_GRAPH_WAITING = 4;
145
+ var HELD_ENTRY = 5;
146
+ var HELD_WORDS_PER_THREAD = 32;
147
+ var METRIC_CODES = ["cosine", "dotProduct", "euclidean"];
148
+ function perOrdinal(bytesPerOrdinal, initialOrdinals) {
149
+ return createGrowableBuffer(initialOrdinals * bytesPerOrdinal, VECTOR_MEMORY_LIMITS.maxOrdinals * bytesPerOrdinal);
150
+ }
151
+ function createSharedGraphHandles(shape) {
152
+ const header = new Int32Array(createFixedBuffer(GRAPH_HEADER_WORDS * 4), 0, GRAPH_HEADER_WORDS);
153
+ header[GRAPH_ENTRY_POINT] = -1;
154
+ header[GRAPH_TOP_LAYER] = -1;
155
+ header[GRAPH_M] = shape.m;
156
+ header[GRAPH_MMAX0] = shape.mMax0;
157
+ header[GRAPH_EF_CONSTRUCTION] = shape.efConstruction;
158
+ header[GRAPH_METRIC] = Math.max(0, METRIC_CODES.indexOf(shape.metric));
159
+ const level0Stride = shape.mMax0 + 2;
160
+ const upperStride = shape.m + 2;
161
+ const heldWords = VECTOR_SCRATCH_SLOTS * HELD_WORDS_PER_THREAD;
162
+ const handles = {
163
+ header,
164
+ nodeLevels: perOrdinal(1, VECTOR_STORE_INITIAL_CAPACITY),
165
+ level0: perOrdinal(level0Stride * 4, VECTOR_STORE_INITIAL_CAPACITY),
166
+ upperBase: perOrdinal(4, VECTOR_STORE_INITIAL_CAPACITY),
167
+ upper: perOrdinal(upperStride * 4, INITIAL_UPPER_SLOTS),
168
+ locks: perOrdinal(4, VECTOR_STORE_INITIAL_CAPACITY),
169
+ tombstones: perOrdinal(1, VECTOR_STORE_INITIAL_CAPACITY),
170
+ incoming: perOrdinal(4, VECTOR_STORE_INITIAL_CAPACITY),
171
+ nearestLinkDue: perOrdinal(1, VECTOR_STORE_INITIAL_CAPACITY),
172
+ heldLocks: new Int32Array(createFixedBuffer(heldWords * 4), 0, heldWords)
173
+ };
174
+ publishGraphCapacity(handles);
175
+ return handles;
176
+ }
177
+ function raiseHeaderWord(header, word, value) {
178
+ for (; ; ) {
179
+ const seen = Atomics.load(header, word);
180
+ if (seen >= value) return;
181
+ if (Atomics.compareExchange(header, word, seen, value) === seen) return;
182
+ }
183
+ }
184
+ function publishGraphCapacity(handles) {
185
+ const level0Stride = handles.header[GRAPH_MMAX0] + 2;
186
+ const slotCapacity = Math.min(
187
+ handles.nodeLevels.byteLength,
188
+ Math.floor(handles.level0.byteLength / (level0Stride * 4)),
189
+ Math.floor(handles.upperBase.byteLength / 4),
190
+ Math.floor(handles.locks.byteLength / 4),
191
+ handles.tombstones.byteLength,
192
+ Math.floor(handles.incoming.byteLength / 4),
193
+ handles.nearestLinkDue.byteLength
194
+ );
195
+ raiseHeaderWord(handles.header, GRAPH_SLOT_CAPACITY, slotCapacity);
196
+ raiseHeaderWord(handles.header, GRAPH_UPPER_CAPACITY, Math.floor(handles.upper.byteLength / 4));
197
+ }
198
+ function graphShapeOf(header) {
199
+ return {
200
+ m: header[GRAPH_M],
201
+ mMax0: header[GRAPH_MMAX0],
202
+ efConstruction: header[GRAPH_EF_CONSTRUCTION],
203
+ metric: METRIC_CODES[header[GRAPH_METRIC]] ?? "cosine"
204
+ };
205
+ }
206
+
207
+ // src/vector/hnsw/locks.ts
208
+ var FREE = 0;
209
+ var EXCLUSIVE = -1;
210
+ function openGraphLocks(handles, threadSlot) {
211
+ let words = fixedView(handles.locks, Int32Array);
212
+ return {
213
+ threadSlot,
214
+ header: handles.header,
215
+ held: handles.heldLocks,
216
+ words(ord) {
217
+ if (ord < words.length) return words;
218
+ if (ord * 4 >= handles.locks.byteLength) growBufferTo(handles.locks, (ord + 1) * 4);
219
+ words = fixedView(handles.locks, Int32Array);
220
+ return words;
221
+ }
222
+ };
223
+ }
224
+ var blockingAllowed = typeof document === "undefined";
225
+ function canWait(words) {
226
+ return blockingAllowed && sharedMemoryAvailable() && words.buffer instanceof SharedArrayBuffer;
227
+ }
228
+ function waitOn(words, index, seen, sliceMs = LOCK_WAIT_SLICE_MS) {
229
+ if (!canWait(words)) return;
230
+ Atomics.wait(words, index, seen, sliceMs);
231
+ }
232
+ function heldIndex(locks, kind) {
233
+ return locks.threadSlot * HELD_WORDS_PER_THREAD + kind;
234
+ }
235
+ function waitedSince(locks, startedAt, lock) {
236
+ const now = performance.now();
237
+ if (Number.isNaN(startedAt)) return now;
238
+ if (now - startedAt < GRAPH_LOCK_WAIT_DEADLINE_MS) return startedAt;
239
+ if (releaseGoneThreads(locks) > 0) return now;
240
+ throw new NarsilError(
241
+ ErrorCodes.WORKER_TIMEOUT,
242
+ `Another thread held a lock on the vector graph for ${GRAPH_LOCK_WAIT_DEADLINE_MS} ms, so this thread stopped waiting for it`,
243
+ { waitedMs: GRAPH_LOCK_WAIT_DEADLINE_MS, threadSlot: locks.threadSlot, lock }
244
+ );
245
+ }
246
+ function releaseExclusive(words, index) {
247
+ Atomics.store(words, index, FREE);
248
+ if (canWait(words)) Atomics.notify(words, index);
249
+ }
250
+ var WRITE_HELD = 1;
251
+ var WRITER_WAITING = 2;
252
+ var VERSION_STEP = 4;
253
+ function beginNodeRead(locks, ord) {
254
+ const words = locks.words(ord);
255
+ const seen = Atomics.load(words, ord);
256
+ if ((seen & WRITE_HELD) === 0) return seen;
257
+ return waitForNodeRead(locks, words, ord);
258
+ }
259
+ function waitForNodeRead(locks, words, ord) {
260
+ let spins = 0;
261
+ let startedAt = Number.NaN;
262
+ for (; ; ) {
263
+ const seen = Atomics.load(words, ord);
264
+ if ((seen & WRITE_HELD) === 0) return seen;
265
+ if (spins < LOCK_SPIN_ITERATIONS) {
266
+ spins += 1;
267
+ continue;
268
+ }
269
+ spins = 0;
270
+ startedAt = waitedSince(locks, startedAt, "node");
271
+ }
272
+ }
273
+ function nodeReadHeld(locks, ord, version) {
274
+ Atomics.add(locks.held, heldIndex(locks, HELD_FENCE), 0);
275
+ return Atomics.load(locks.words(ord), ord) === version;
276
+ }
277
+ function lockNodeWrite(locks, ord) {
278
+ const words = locks.words(ord);
279
+ let spins = 0;
280
+ let startedAt = Number.NaN;
281
+ for (; ; ) {
282
+ const seen = Atomics.load(words, ord);
283
+ if ((seen & WRITE_HELD) === 0) {
284
+ const claimed = seen | WRITE_HELD;
285
+ if (Atomics.compareExchange(words, ord, seen, claimed) === seen) {
286
+ Atomics.store(locks.held, heldIndex(locks, HELD_WRITE_VERSION), claimed);
287
+ Atomics.store(locks.held, heldIndex(locks, HELD_WRITE), ord + 1);
288
+ return;
289
+ }
290
+ continue;
291
+ }
292
+ if (spins < LOCK_SPIN_ITERATIONS) {
293
+ spins += 1;
294
+ continue;
295
+ }
296
+ startedAt = waitedSince(locks, startedAt, "node");
297
+ const waiting = seen | WRITER_WAITING;
298
+ if (waiting === seen || Atomics.compareExchange(words, ord, seen, waiting) === seen) {
299
+ waitOn(words, ord, waiting);
300
+ }
301
+ }
302
+ }
303
+ function releaseWrite(words, ord) {
304
+ const seen = Atomics.load(words, ord);
305
+ const previous = Atomics.exchange(words, ord, (seen & -4) + VERSION_STEP);
306
+ if ((previous & WRITER_WAITING) !== 0 && canWait(words)) Atomics.notify(words, ord);
307
+ }
308
+ function unlockNodeWrite(locks, ord) {
309
+ releaseWrite(locks.words(ord), ord);
310
+ Atomics.store(locks.held, heldIndex(locks, HELD_WRITE), 0);
311
+ }
312
+ function lockGraphShared(locks) {
313
+ const { header } = locks;
314
+ let spins = 0;
315
+ let startedAt = Number.NaN;
316
+ for (; ; ) {
317
+ const seen = Atomics.load(header, GRAPH_LOCK);
318
+ if (seen >= FREE && Atomics.load(header, GRAPH_WRITERS_WAITING) === 0) {
319
+ if (Atomics.compareExchange(header, GRAPH_LOCK, seen, seen + 1) === seen) {
320
+ Atomics.store(locks.held, heldIndex(locks, HELD_GRAPH), 1);
321
+ return;
322
+ }
323
+ continue;
324
+ }
325
+ if (spins < LOCK_SPIN_ITERATIONS) {
326
+ spins += 1;
327
+ continue;
328
+ }
329
+ startedAt = waitedSince(locks, startedAt, "graph");
330
+ waitOn(header, GRAPH_LOCK, seen, GRAPH_LOCK_WAIT_SLICE_MS);
331
+ }
332
+ }
333
+ function unlockGraphShared(locks) {
334
+ const { header } = locks;
335
+ Atomics.store(locks.held, heldIndex(locks, HELD_GRAPH), 0);
336
+ if (Atomics.sub(header, GRAPH_LOCK, 1) === 1 && canWait(header)) Atomics.notify(header, GRAPH_LOCK);
337
+ }
338
+ function lockGraphExclusive(locks) {
339
+ const { header } = locks;
340
+ Atomics.store(locks.held, heldIndex(locks, HELD_GRAPH_WAITING), 1);
341
+ Atomics.add(header, GRAPH_WRITERS_WAITING, 1);
342
+ try {
343
+ let spins = 0;
344
+ let startedAt = Number.NaN;
345
+ for (; ; ) {
346
+ const seen = Atomics.compareExchange(header, GRAPH_LOCK, FREE, EXCLUSIVE);
347
+ if (seen === FREE) return;
348
+ if (spins < LOCK_SPIN_ITERATIONS) {
349
+ spins += 1;
350
+ continue;
351
+ }
352
+ startedAt = waitedSince(locks, startedAt, "graphAlone");
353
+ waitOn(header, GRAPH_LOCK, seen, GRAPH_LOCK_WAIT_SLICE_MS);
354
+ }
355
+ } finally {
356
+ Atomics.sub(locks.header, GRAPH_WRITERS_WAITING, 1);
357
+ Atomics.store(locks.held, heldIndex(locks, HELD_GRAPH_WAITING), 0);
358
+ }
359
+ }
360
+ function unlockGraphExclusive(locks) {
361
+ releaseExclusive(locks.header, GRAPH_LOCK);
362
+ }
363
+ function lockEntry(locks) {
364
+ const { header } = locks;
365
+ let spins = 0;
366
+ let startedAt = Number.NaN;
367
+ for (; ; ) {
368
+ const seen = Atomics.compareExchange(header, GRAPH_ENTRY_LOCK, FREE, EXCLUSIVE);
369
+ if (seen === FREE) {
370
+ Atomics.store(locks.held, heldIndex(locks, HELD_ENTRY), 1);
371
+ return;
372
+ }
373
+ if (spins < LOCK_SPIN_ITERATIONS) {
374
+ spins += 1;
375
+ continue;
376
+ }
377
+ startedAt = waitedSince(locks, startedAt, "entry");
378
+ waitOn(header, GRAPH_ENTRY_LOCK, seen, GRAPH_LOCK_WAIT_SLICE_MS);
379
+ }
380
+ }
381
+ function unlockEntry(locks) {
382
+ Atomics.store(locks.held, heldIndex(locks, HELD_ENTRY), 0);
383
+ releaseExclusive(locks.header, GRAPH_ENTRY_LOCK);
384
+ }
385
+ function releaseGoneThreads(locks) {
386
+ let released = 0;
387
+ const slots = locks.held.length / HELD_WORDS_PER_THREAD;
388
+ for (let slot = 0; slot < slots; slot++) {
389
+ if (slot === locks.threadSlot || !threadSlotGone(slot)) continue;
390
+ const base = slot * HELD_WORDS_PER_THREAD;
391
+ const writeOrd = Atomics.load(locks.held, base + HELD_WRITE) - 1;
392
+ const holdsGraph = Atomics.load(locks.held, base + HELD_GRAPH) === 1;
393
+ const waitsForGraph = Atomics.load(locks.held, base + HELD_GRAPH_WAITING) === 1;
394
+ const holdsEntry = Atomics.load(locks.held, base + HELD_ENTRY) === 1;
395
+ if (!holdsGraph && !waitsForGraph && !holdsEntry && writeOrd < 0) continue;
396
+ releaseSlot(locks.header, locks.held, writeOrd >= 0 ? locks.words(writeOrd) : null, slot);
397
+ released += 1;
398
+ }
399
+ return released;
400
+ }
401
+ function releaseLocksHeldBy(handles, threadSlot) {
402
+ releaseSlot(handles.header, handles.heldLocks, new Int32Array(handles.locks), threadSlot);
403
+ }
404
+ function releaseSlot(header, held, words, threadSlot) {
405
+ const base = threadSlot * HELD_WORDS_PER_THREAD;
406
+ if (Atomics.exchange(held, base + HELD_GRAPH_WAITING, 0) === 1) {
407
+ Atomics.sub(header, GRAPH_WRITERS_WAITING, 1);
408
+ }
409
+ if (Atomics.exchange(held, base + HELD_ENTRY, 0) === 1) releaseExclusive(header, GRAPH_ENTRY_LOCK);
410
+ const writeOrd = Atomics.exchange(held, base + HELD_WRITE, 0) - 1;
411
+ const writeVersion = Atomics.load(held, base + HELD_WRITE_VERSION);
412
+ if (words !== null && writeOrd >= 0 && writeOrd < words.length) {
413
+ const seen = Atomics.load(words, writeOrd);
414
+ if ((seen & ~WRITER_WAITING) === writeVersion) releaseWrite(words, writeOrd);
415
+ }
416
+ if (Atomics.exchange(held, base + HELD_GRAPH, 0) !== 1) return;
417
+ for (; ; ) {
418
+ const readers = Atomics.load(header, GRAPH_LOCK);
419
+ if (readers <= FREE) return;
420
+ if (Atomics.compareExchange(header, GRAPH_LOCK, readers, readers - 1) === readers) {
421
+ if (canWait(header)) Atomics.notify(header, GRAPH_LOCK);
422
+ return;
423
+ }
424
+ }
425
+ }
426
+
427
+ // src/vector/hnsw/adjacency/views.ts
428
+ var ABSENT = -1;
429
+ function openAdjacency(handles, m, mMax0) {
430
+ return {
431
+ m,
432
+ mMax0,
433
+ level0Stride: mMax0 + 2,
434
+ upperStride: m + 2,
435
+ handles,
436
+ header: handles.header,
437
+ nodeLevels: fixedView(handles.nodeLevels, Uint8Array),
438
+ level0: fixedView(handles.level0, Int32Array),
439
+ upperBase: fixedView(handles.upperBase, Int32Array),
440
+ upper: fixedView(handles.upper, Int32Array),
441
+ incoming: fixedView(handles.incoming, Int32Array),
442
+ nearestLinkDue: fixedView(handles.nearestLinkDue, Uint8Array)
443
+ };
444
+ }
445
+ function rebindNodes(adj) {
446
+ adj.nodeLevels = fixedView(adj.handles.nodeLevels, Uint8Array);
447
+ adj.incoming = fixedView(adj.handles.incoming, Int32Array);
448
+ adj.nearestLinkDue = fixedView(adj.handles.nearestLinkDue, Uint8Array);
449
+ adj.level0 = fixedView(adj.handles.level0, Int32Array);
450
+ adj.upperBase = fixedView(adj.handles.upperBase, Int32Array);
451
+ }
452
+ function rebindUpper(adj) {
453
+ adj.upper = fixedView(adj.handles.upper, Int32Array);
454
+ }
455
+ function reachNode(adj, ord) {
456
+ if (ord < adj.nodeLevels.length) return true;
457
+ if (ord >= adj.handles.nodeLevels.byteLength) return false;
458
+ rebindNodes(adj);
459
+ return ord < adj.nodeLevels.length;
460
+ }
461
+ function reachUpper(adj, end) {
462
+ if (end <= adj.upper.length) return true;
463
+ if (end * 4 > adj.handles.upper.byteLength) return false;
464
+ rebindUpper(adj);
465
+ return end <= adj.upper.length;
466
+ }
467
+ function adjacencySlots(adj) {
468
+ return Atomics.load(adj.header, GRAPH_SLOTS);
469
+ }
470
+ function upperUsed(adj) {
471
+ return Atomics.load(adj.header, GRAPH_UPPER_USED);
472
+ }
473
+ function ensureAdjacencyCapacity(adj, needed) {
474
+ if (needed <= adj.nodeLevels.length) return;
475
+ const { handles } = adj;
476
+ if (needed <= handles.nodeLevels.byteLength) {
477
+ rebindNodes(adj);
478
+ return;
479
+ }
480
+ const capacity = Math.max(needed, Math.min(handles.nodeLevels.byteLength * 2, handles.nodeLevels.maxByteLength));
481
+ growBufferToExactly(handles.level0, capacity * adj.level0Stride * 4);
482
+ growBufferToExactly(handles.upperBase, capacity * 4);
483
+ growBufferToExactly(handles.locks, capacity * 4);
484
+ growBufferToExactly(handles.tombstones, capacity);
485
+ growBufferToExactly(handles.incoming, capacity * 4);
486
+ growBufferToExactly(handles.nearestLinkDue, capacity);
487
+ growBufferToExactly(handles.nodeLevels, capacity);
488
+ publishGraphCapacity(handles);
489
+ rebindNodes(adj);
490
+ }
491
+ function growUpperTo(adj, entries) {
492
+ const held = adj.handles.upper.byteLength;
493
+ growBufferTo(adj.handles.upper, entries * 4);
494
+ if (adj.handles.upper.byteLength !== held) publishGraphCapacity(adj.handles);
495
+ }
496
+ function ensureUpperCapacity(adj, entries) {
497
+ growUpperTo(adj, entries);
498
+ rebindUpper(adj);
499
+ }
500
+ function ensureUpperRoom(adj, levels) {
501
+ if (levels < 1) return;
502
+ growUpperTo(adj, Atomics.load(adj.header, GRAPH_UPPER_USED) + levels * adj.upperStride);
503
+ }
504
+ function allocateUpperBlock(adj, levels) {
505
+ const size = levels * adj.upperStride;
506
+ for (; ; ) {
507
+ const offset = Atomics.load(adj.header, GRAPH_UPPER_USED);
508
+ growUpperTo(adj, offset + size);
509
+ if (Atomics.compareExchange(adj.header, GRAPH_UPPER_USED, offset, offset + size) !== offset) continue;
510
+ rebindUpper(adj);
511
+ return offset;
512
+ }
513
+ }
514
+ function raiseSlots(adj, slots) {
515
+ for (; ; ) {
516
+ const seen = Atomics.load(adj.header, GRAPH_SLOTS);
517
+ if (seen >= slots) return;
518
+ if (Atomics.compareExchange(adj.header, GRAPH_SLOTS, seen, slots) === seen) return;
519
+ }
520
+ }
521
+ function createNode(adj, ord, maxLayer) {
522
+ ensureAdjacencyCapacity(adj, ord + 1);
523
+ adj.upperBase[ord] = maxLayer >= 1 ? allocateUpperBlock(adj, maxLayer) + 1 : 0;
524
+ adj.level0[ord * adj.level0Stride] = 0;
525
+ raiseSlots(adj, ord + 1);
526
+ Atomics.store(adj.nearestLinkDue, ord, 1);
527
+ Atomics.store(adj.nodeLevels, ord, maxLayer + 1);
528
+ }
529
+ function deleteNode(adj, ord) {
530
+ if (ord < 0 || !reachNode(adj, ord)) return;
531
+ Atomics.store(adj.nodeLevels, ord, 0);
532
+ adj.upperBase[ord] = 0;
533
+ adj.level0[ord * adj.level0Stride] = 0;
534
+ Atomics.store(adj.incoming, ord, 0);
535
+ Atomics.store(adj.nearestLinkDue, ord, 0);
536
+ }
537
+ function resetAdjacency(adj) {
538
+ rebindNodes(adj);
539
+ rebindUpper(adj);
540
+ adj.incoming.fill(0);
541
+ adj.nearestLinkDue.fill(0);
542
+ adj.nodeLevels.fill(0);
543
+ adj.level0.fill(0);
544
+ adj.upperBase.fill(0);
545
+ adj.upper.fill(0);
546
+ Atomics.store(adj.header, GRAPH_UPPER_USED, 0);
547
+ Atomics.store(adj.header, GRAPH_SLOTS, 0);
548
+ }
549
+ function nodeLevel(adj, ord) {
550
+ if (ord < 0 || !reachNode(adj, ord)) return ABSENT;
551
+ return adj.nodeLevels[ord] - 1;
552
+ }
553
+ function hasNode(adj, ord) {
554
+ return nodeLevel(adj, ord) !== ABSENT;
555
+ }
556
+ function graphBytes(adj) {
557
+ const handles = adj.handles;
558
+ return handles.header.byteLength + handles.nodeLevels.byteLength + handles.level0.byteLength + handles.upperBase.byteLength + handles.upper.byteLength + handles.locks.byteLength + handles.tombstones.byteLength + handles.incoming.byteLength + handles.nearestLinkDue.byteLength + handles.heldLocks.byteLength;
559
+ }
560
+
561
+ // src/vector/hnsw/adjacency/incoming.ts
562
+ function incomingCount(adj, ord) {
563
+ if (ord < 0 || !reachNode(adj, ord)) return 0;
564
+ return Atomics.load(adj.incoming, ord);
565
+ }
566
+ function noteLinked(adj, ord) {
567
+ if (ord < 0 || !reachNode(adj, ord)) return;
568
+ Atomics.add(adj.incoming, ord, 1);
569
+ }
570
+ function noteUnlinked(adj, ord) {
571
+ if (ord < 0 || !reachNode(adj, ord)) return 0;
572
+ Atomics.store(adj.nearestLinkDue, ord, 1);
573
+ for (; ; ) {
574
+ const seen = Atomics.load(adj.incoming, ord);
575
+ if (seen <= 0) return 0;
576
+ if (Atomics.compareExchange(adj.incoming, ord, seen, seen - 1) === seen) return seen - 1;
577
+ }
578
+ }
579
+ function releaseUnlessLast(adj, ord) {
580
+ if (ord < 0 || !reachNode(adj, ord)) return true;
581
+ for (; ; ) {
582
+ const seen = Atomics.load(adj.incoming, ord);
583
+ if (seen <= 1) return false;
584
+ if (Atomics.compareExchange(adj.incoming, ord, seen, seen - 1) !== seen) continue;
585
+ Atomics.store(adj.nearestLinkDue, ord, 1);
586
+ return true;
587
+ }
588
+ }
589
+ function holdsNodeAt(adj, ord) {
590
+ return ord >= 0 && ord < adj.nodeLevels.length && adj.nodeLevels[ord] !== 0;
591
+ }
592
+ function countLinksOutOf(adj, ord, step) {
593
+ if (!holdsNodeAt(adj, ord)) return;
594
+ const incoming = adj.incoming;
595
+ const level0 = adj.level0;
596
+ const base = ord * adj.level0Stride;
597
+ const count = level0[base];
598
+ for (let i = 1; i <= count; i++) {
599
+ const neighbour = level0[base + i];
600
+ if (neighbour === ord || !holdsNodeAt(adj, neighbour)) continue;
601
+ if (incoming[neighbour] + step >= 0) incoming[neighbour] += step;
602
+ }
603
+ }
604
+ function countEveryLink(adj, slots) {
605
+ for (let ord = 0; ord < slots; ord++) countLinksOutOf(adj, ord, 1);
606
+ }
607
+
608
+ // src/vector/hnsw/adjacency/neighbors.ts
609
+ function layerArray(adj, layer) {
610
+ return layer === 0 ? adj.level0 : adj.upper;
611
+ }
612
+ function layerBase(adj, ord, layer) {
613
+ const level2 = nodeLevel(adj, ord);
614
+ if (level2 === ABSENT || layer > level2) return ABSENT;
615
+ if (layer === 0) return ord * adj.level0Stride;
616
+ const base = adj.upperBase[ord];
617
+ if (base === 0) return ABSENT;
618
+ const start = base - 1 + (layer - 1) * adj.upperStride;
619
+ return reachUpper(adj, start + adj.upperStride) ? start : ABSENT;
620
+ }
621
+ function neighborCount(adj, ord, layer) {
622
+ const base = layerBase(adj, ord, layer);
623
+ if (base === ABSENT) return 0;
624
+ return layerArray(adj, layer)[base];
625
+ }
626
+ function readNeighbors(adj, ord, layer, out) {
627
+ const base = layerBase(adj, ord, layer);
628
+ if (base === ABSENT) return 0;
629
+ const array = layerArray(adj, layer);
630
+ const count = Math.min(array[base], out.length);
631
+ for (let i = 0; i < count; i++) out[i] = array[base + i + 1];
632
+ return count;
633
+ }
634
+ function collectNeighbors(adj, ord, layer) {
635
+ const base = layerBase(adj, ord, layer);
636
+ if (base === ABSENT) return [];
637
+ const array = layerArray(adj, layer);
638
+ const count = array[base];
639
+ const collected = new Array(count);
640
+ for (let i = 0; i < count; i++) collected[i] = array[base + i + 1];
641
+ return collected;
642
+ }
643
+ function addNeighbor(adj, ord, layer, neighborOrd) {
644
+ const base = layerBase(adj, ord, layer);
645
+ if (base === ABSENT) return;
646
+ const array = layerArray(adj, layer);
647
+ const count = array[base];
648
+ for (let i = 1; i <= count; i++) {
649
+ if (array[base + i] === neighborOrd) return;
650
+ }
651
+ const stride = layer === 0 ? adj.level0Stride : adj.upperStride;
652
+ if (count + 1 > stride - 1) return;
653
+ array[base + count + 1] = neighborOrd;
654
+ array[base] = count + 1;
655
+ if (layer === 0) noteLinked(adj, neighborOrd);
656
+ }
657
+ function removeNeighbor(adj, ord, layer, neighborOrd) {
658
+ const base = layerBase(adj, ord, layer);
659
+ if (base === ABSENT) return;
660
+ const array = layerArray(adj, layer);
661
+ const count = array[base];
662
+ for (let i = 1; i <= count; i++) {
663
+ if (array[base + i] !== neighborOrd) continue;
664
+ for (let j = i; j < count; j++) {
665
+ array[base + j] = array[base + j + 1];
666
+ }
667
+ array[base] = count - 1;
668
+ if (layer === 0) noteUnlinked(adj, neighborOrd);
669
+ return;
670
+ }
671
+ }
672
+ function writeNeighbors(adj, ord, layer, neighbors, count) {
673
+ const base = layerBase(adj, ord, layer);
674
+ if (base === ABSENT) return;
675
+ const array = layerArray(adj, layer);
676
+ const stride = layer === 0 ? adj.level0Stride : adj.upperStride;
677
+ const limit = Math.min(count, stride - 1);
678
+ for (let i = 0; i < limit; i++) {
679
+ array[base + i + 1] = neighbors[i];
680
+ }
681
+ array[base] = limit;
682
+ }
683
+ function holds(neighbors, count, ord) {
684
+ for (let i = 0; i < count; i++) if (neighbors[i] === ord) return true;
685
+ return false;
686
+ }
687
+ function replaceNeighbors(adj, ord, layer, neighbors, count) {
688
+ const base = layerBase(adj, ord, layer);
689
+ if (base === ABSENT) return;
690
+ if (layer > 0) {
691
+ writeNeighbors(adj, ord, layer, neighbors, count);
692
+ return;
693
+ }
694
+ const array = adj.level0;
695
+ const held = array.slice(base + 1, base + 1 + array[base]);
696
+ const limit = Math.min(count, adj.level0Stride - 1);
697
+ for (const former of held) if (!holds(neighbors, limit, former)) noteUnlinked(adj, former);
698
+ for (let i = 0; i < limit; i++) if (!holds(held, held.length, neighbors[i])) noteLinked(adj, neighbors[i]);
699
+ writeNeighbors(adj, ord, 0, neighbors, count);
700
+ }
701
+
702
+ // src/vector/native/types.ts
703
+ var NATIVE_CORE_ABI_VERSION = 6;
704
+ var NATIVE_OK = 0;
705
+ var NATIVE_NEEDS_ROOM = 3;
706
+ var NATIVE_GRAPH_NOT_HELD_ALONE = 4;
707
+
708
+ // src/vector/native/loader.react-native.ts
709
+ var loaded;
710
+ function loadNativeCore() {
711
+ if (loaded !== void 0) return loaded;
712
+ const core = reactNativeHost().core;
713
+ const reported = core.abiVersion();
714
+ if (reported !== NATIVE_CORE_ABI_VERSION) {
715
+ console.warn(
716
+ `Narsil searches without its native core on this phone, because the core reports ABI version ${reported} while this build of Narsil needs ${NATIVE_CORE_ABI_VERSION}.`
717
+ );
718
+ loaded = null;
719
+ return loaded;
720
+ }
721
+ loaded = core;
722
+ return loaded;
723
+ }
724
+
725
+ // src/vector/native/backend.ts
726
+ var nativeSearchCoreEnabled = true;
727
+ function nativeSearchCoreIsEnabled() {
728
+ return nativeSearchCoreEnabled;
729
+ }
730
+ function stopUsingTheNativeSearchCore(error) {
731
+ if (!nativeSearchCoreEnabled) return;
732
+ nativeSearchCoreEnabled = false;
733
+ console.warn(
734
+ "The native search core raised an error, so this thread searches through WebAssembly from now on:",
735
+ error instanceof Error ? error.message : String(error)
736
+ );
737
+ }
738
+ function noteFallback(operation) {
739
+ }
740
+
741
+ // src/vector/simd-wasm-binary.ts
742
+ var SIMD_DISTANCE_WASM_BASE64 = "AGFzbQEAAAABIgVgA39/fwF9YAJ/fwF9YAN/f38Bf2ABewF/YAR/f39/AX8DDw4AAQAAAgIDAgIEAgQCAgUDAQABB6QBCgZtZW1vcnkCAAtkb3RfcHJvZHVjdAAACW1hZ25pdHVkZQABGnNxdWFyZWRfZXVjbGlkZWFuX2Rpc3RhbmNlAAISZXVjbGlkZWFuX2Rpc3RhbmNlAAMGZG90X3U4AAQJc3FkaXN0X3U4AAUMb3NxX2RvdF9iaXRzAAkRb3NxX2RvdF9iaXRzX3BhaXIACxNvc3FfZG90X25pYmJsZXNfNHg0AA0KqhQOygIEAX8EewF9BH8gAkFwcSEJIAJBfHEhCv0MAAAAAAAAAAAAAAAAAAAAACEEQQAhAwJAA0AgAyAJTw0BIAAgA0ECdGohCyABIANBAnRqIQwgBCAL/QAEACAM/QAEAP3mAf3kASEEIAUgC/0ABBAgDP0ABBD95gH95AEhBSAGIAv9AAQgIAz9AAQg/eYB/eQBIQYgByAL/QAEMCAM/QAEMP3mAf3kASEHIANBEGohAwwACwsCQANAIAMgCk8NASAEIAAgA0ECdGr9AAQAIAEgA0ECdGr9AAQA/eYB/eQBIQQgA0EEaiEDDAALCyAEIAX95AEgBiAH/eQB/eQBIQQgBP0fACAE/R8BkiAE/R8CIAT9HwOSkiEIAkADQCADIAJPDQEgCCAAIANBAnRqKgIAIAEgA0ECdGoqAgCUkiEIIANBAWohAwwACwsgCAupAQQBfwJ7AX0BfyABQXxxIQb9DAAAAAAAAAAAAAAAAAAAAAAhA0EAIQICQANAIAIgBk8NASAAIAJBAnRq/QAEACEEIAMgBCAE/eYB/eQBIQMgAkEEaiECDAALCyAD/R8AIAP9HwGSIAP9HwIgA/0fA5KSIQUCQANAIAIgAU8NASAFIAAgAkECdGoqAgAgACACQQJ0aioCAJSSIQUgAkEBaiECDAALCyAFkQvAAQUBfwJ7AX0BfwF9IAJBfHEhB/0MAAAAAAAAAAAAAAAAAAAAACEEQQAhAwJAA0AgAyAHTw0BIAAgA0ECdGr9AAQAIAEgA0ECdGr9AAQA/eUBIQUgBCAFIAX95gH95AEhBCADQQRqIQMMAAsLIAT9HwAgBP0fAZIgBP0fAiAE/R8DkpIhBgJAA0AgAyACTw0BIAAgA0ECdGoqAgAgASADQQJ0aioCAJMhCCAGIAggCJSSIQYgA0EBaiEDDAALCyAGCwsAIAAgASACEAKRC7QBAwF/A3sCfyACQXBxIQhBAP0RIQRBACEDAkADQCADIAhPDQEgACADav0ABAAhBSABIANq/QAEACEGIAQgBf2JASAG/YkB/boB/a4BIQQgBCAF/YoBIAb9igH9ugH9rgEhBCADQRBqIQMMAAsLIAT9GwAgBP0bAWogBP0bAiAE/RsDamohBwJAA0AgAyACTw0BIAcgACADai0AACABIANqLQAAbGohByADQQFqIQMMAAsLIAcLzQEDAX8FewN/IAJBcHEhC0EA/REhBEEAIQMCQANAIAMgC08NASAAIANq/QAEACEFIAEgA2r9AAQAIQYgBf2JASAG/YkB/ZEBIQcgBf2KASAG/YoB/ZEBIQggBCAHIAf9ugH9rgEhBCAEIAggCP26Af2uASEEIANBEGohAwwACwsgBP0bACAE/RsBaiAE/RsCIAT9GwNqaiEJAkADQCADIAJPDQEgACADai0AACABIANqLQAAayEKIAkgCiAKbGohCSADQQFqIQMMAAsLIAkLIQEBeyAA/X8hASAB/RsAIAH9GwFqIAH9GwIgAf0bA2pqC68CAwh/A3sBfyABIAJqIQggCCACaiEJIAkgAmohCiACQXBxIQUCQANAIAMgBU8NASADQYAQaiEGIAYgBUsEQCAFIQYLQQD9ECENA0AgACADav0ABAAhCyALIAogA2r9AAQA/U79YiEMIAwgDP1uIAsgCSADav0ABAD9Tv1i/W4hDCAMIAz9biALIAggA2r9AAQA/U79Yv1uIQwgDCAM/W4gCyABIANq/QAEAP1O/WL9biEMIA0gDP19/Y4BIQ0gA0EQaiEDIAMgBkkNAAsgByANEAZqIQcMAAsLAkADQCADIAJPDQEgACADai0AACEOQQAhBAJAA0AgBEEETw0BIAcgDiABIAIgBGxqIANqLQAAcWkgBHRqIQcgBEEBaiEEDAALCyADQQFqIQMMAAsLIAcL2AIDCH8DewF/IAEgAmohCCAIIAJqIQkgCSACaiEKQQP9DyEMIAJBcHEhBQJAA0AgAyAFTw0BIANBgBBqIQYgBiAFSwRAIAUhBgtBAP0QIQ0DQCAAIANq/QAEACAM/Q0BAAMCBQQHBgkICwoNDA8OIQsgDSALIAz9TiABIANq/QAEAP2VASALQQL9jQEgDP1OIAggA2r9AAQA/ZUB/Y4BQQj9jQEgC0EE/Y0BIAz9TiAJIANq/QAEAP2VASALQQb9jQEgDP1OIAogA2r9AAQA/ZUB/Y4BQQj9jQH9jgH9jgEhDSADQRBqIQMgAyAGSQ0ACyAHIA0QBmohBwwACwsCQANAIAMgAk8NASAAIANqLQAAIQ5BACEEAkADQCAEQQRPDQEgByAOIARBAXR2QQNxIAEgAiAEbGogA2otAABsaiEHIARBAWohBAwACwsgA0EBaiEDDAALCyAHCxsAIANBAkYEfyAAIAEgAhAIBSAAIAEgAhAHCwueAQIEfwF7IAJBcHEhBAJAA0AgAyAETw0BIANBgBBqIQUgBSAESwRAIAQhBQtBAP0QIQcDQCAHIAAgA2r9AAQAIAEgA2r9AAQA/U79Yv19/Y4BIQcgA0EQaiEDIAMgBUkNAAsgBiAHEAZqIQYMAAsLAkADQCADIAJPDQEgBiAAIANqLQAAIAEgA2otAABxaWohBiADQQFqIQMMAAsLIAYLGwAgA0ECRgR/IAAgASACEAwFIAAgASACEAoLC9oCAwR/CHsEf0HVACERQdUAIRJB1QD9DyEHQaoB/Q8hCCACQXBxIQQCQANAIAMgBE8NASADQeADaiEFIAUgBEsEQCAEIQULQQD9DyEOQQD9DyEMQQD9DyENA0AgACADav0ABAAhCSABIANq/QAEACEKIAkgCv1OIQsgDiAL/WL9biEOIAwgCyAI/U79Yv1uIQwgDSAJIApBAf1tIAf9TiAKQQH9ayAI/U79UP1O/WL9biENIANBEGohAyADIAVJDQALIAYgDv19EAYgDP19EAZBA2wgDf19EAZBAXRqamohBgwACwsCQANAIAMgAk8NASAAIANqLQAAIQ8gASADai0AACEQIAYgDyARcSAQcWkgDyARcSAQQQF2IBJxcWlBAXRqIA9BAXYgEnEgECARcXFpQQF0IA9BAXYgEnEgEEEBdiAScXFpQQJ0ampqIQYgA0EBaiEDDAALCyAGC/wBAwN/CHsCfyACQXBxIQRBD/0PIQxBAP0RIQ1BACEDAkADQCADIARPDQEgACADav0ABAAhBiABIANq/QAEACEHIAYgDP1OIQggBkEE/W0hCSAHIAz9TiEKIAdBBP1tIQsgDSAIIAr9ngEgCSAL/Z4B/Y4B/X8gCCAK/Z8BIAkgC/2fAf2OAf1//a4B/a4BIQ0gA0EQaiEDDAALCyAN/RsAIA39GwFqIA39GwIgDf0bA2pqIQUCQANAIAMgAk8NASAAIANqLQAAIQ8gASADai0AACEOIAUgD0EPcSAOQQ9xbCAPQQR2IA5BBHZsamohBSADQQFqIQMMAAsLIAUL";
743
+ var SIMD_DISTANCE_SHARED_MEMORY_WASM_BASE64 = "AGFzbQEAAAABIgVgA39/fwF9YAJ/fwF9YAN/f38Bf2ABewF/YAR/f39/AX8CEgEDZW52Bm1lbW9yeQIDAYCABAMPDgABAAACAgMCAgQCBAICB5sBCQtkb3RfcHJvZHVjdAAACW1hZ25pdHVkZQABGnNxdWFyZWRfZXVjbGlkZWFuX2Rpc3RhbmNlAAISZXVjbGlkZWFuX2Rpc3RhbmNlAAMGZG90X3U4AAQJc3FkaXN0X3U4AAUMb3NxX2RvdF9iaXRzAAkRb3NxX2RvdF9iaXRzX3BhaXIACxNvc3FfZG90X25pYmJsZXNfNHg0AA0KqhQOygIEAX8EewF9BH8gAkFwcSEJIAJBfHEhCv0MAAAAAAAAAAAAAAAAAAAAACEEQQAhAwJAA0AgAyAJTw0BIAAgA0ECdGohCyABIANBAnRqIQwgBCAL/QAEACAM/QAEAP3mAf3kASEEIAUgC/0ABBAgDP0ABBD95gH95AEhBSAGIAv9AAQgIAz9AAQg/eYB/eQBIQYgByAL/QAEMCAM/QAEMP3mAf3kASEHIANBEGohAwwACwsCQANAIAMgCk8NASAEIAAgA0ECdGr9AAQAIAEgA0ECdGr9AAQA/eYB/eQBIQQgA0EEaiEDDAALCyAEIAX95AEgBiAH/eQB/eQBIQQgBP0fACAE/R8BkiAE/R8CIAT9HwOSkiEIAkADQCADIAJPDQEgCCAAIANBAnRqKgIAIAEgA0ECdGoqAgCUkiEIIANBAWohAwwACwsgCAupAQQBfwJ7AX0BfyABQXxxIQb9DAAAAAAAAAAAAAAAAAAAAAAhA0EAIQICQANAIAIgBk8NASAAIAJBAnRq/QAEACEEIAMgBCAE/eYB/eQBIQMgAkEEaiECDAALCyAD/R8AIAP9HwGSIAP9HwIgA/0fA5KSIQUCQANAIAIgAU8NASAFIAAgAkECdGoqAgAgACACQQJ0aioCAJSSIQUgAkEBaiECDAALCyAFkQvAAQUBfwJ7AX0BfwF9IAJBfHEhB/0MAAAAAAAAAAAAAAAAAAAAACEEQQAhAwJAA0AgAyAHTw0BIAAgA0ECdGr9AAQAIAEgA0ECdGr9AAQA/eUBIQUgBCAFIAX95gH95AEhBCADQQRqIQMMAAsLIAT9HwAgBP0fAZIgBP0fAiAE/R8DkpIhBgJAA0AgAyACTw0BIAAgA0ECdGoqAgAgASADQQJ0aioCAJMhCCAGIAggCJSSIQYgA0EBaiEDDAALCyAGCwsAIAAgASACEAKRC7QBAwF/A3sCfyACQXBxIQhBAP0RIQRBACEDAkADQCADIAhPDQEgACADav0ABAAhBSABIANq/QAEACEGIAQgBf2JASAG/YkB/boB/a4BIQQgBCAF/YoBIAb9igH9ugH9rgEhBCADQRBqIQMMAAsLIAT9GwAgBP0bAWogBP0bAiAE/RsDamohBwJAA0AgAyACTw0BIAcgACADai0AACABIANqLQAAbGohByADQQFqIQMMAAsLIAcLzQEDAX8FewN/IAJBcHEhC0EA/REhBEEAIQMCQANAIAMgC08NASAAIANq/QAEACEFIAEgA2r9AAQAIQYgBf2JASAG/YkB/ZEBIQcgBf2KASAG/YoB/ZEBIQggBCAHIAf9ugH9rgEhBCAEIAggCP26Af2uASEEIANBEGohAwwACwsgBP0bACAE/RsBaiAE/RsCIAT9GwNqaiEJAkADQCADIAJPDQEgACADai0AACABIANqLQAAayEKIAkgCiAKbGohCSADQQFqIQMMAAsLIAkLIQEBeyAA/X8hASAB/RsAIAH9GwFqIAH9GwIgAf0bA2pqC68CAwh/A3sBfyABIAJqIQggCCACaiEJIAkgAmohCiACQXBxIQUCQANAIAMgBU8NASADQYAQaiEGIAYgBUsEQCAFIQYLQQD9ECENA0AgACADav0ABAAhCyALIAogA2r9AAQA/U79YiEMIAwgDP1uIAsgCSADav0ABAD9Tv1i/W4hDCAMIAz9biALIAggA2r9AAQA/U79Yv1uIQwgDCAM/W4gCyABIANq/QAEAP1O/WL9biEMIA0gDP19/Y4BIQ0gA0EQaiEDIAMgBkkNAAsgByANEAZqIQcMAAsLAkADQCADIAJPDQEgACADai0AACEOQQAhBAJAA0AgBEEETw0BIAcgDiABIAIgBGxqIANqLQAAcWkgBHRqIQcgBEEBaiEEDAALCyADQQFqIQMMAAsLIAcL2AIDCH8DewF/IAEgAmohCCAIIAJqIQkgCSACaiEKQQP9DyEMIAJBcHEhBQJAA0AgAyAFTw0BIANBgBBqIQYgBiAFSwRAIAUhBgtBAP0QIQ0DQCAAIANq/QAEACAM/Q0BAAMCBQQHBgkICwoNDA8OIQsgDSALIAz9TiABIANq/QAEAP2VASALQQL9jQEgDP1OIAggA2r9AAQA/ZUB/Y4BQQj9jQEgC0EE/Y0BIAz9TiAJIANq/QAEAP2VASALQQb9jQEgDP1OIAogA2r9AAQA/ZUB/Y4BQQj9jQH9jgH9jgEhDSADQRBqIQMgAyAGSQ0ACyAHIA0QBmohBwwACwsCQANAIAMgAk8NASAAIANqLQAAIQ5BACEEAkADQCAEQQRPDQEgByAOIARBAXR2QQNxIAEgAiAEbGogA2otAABsaiEHIARBAWohBAwACwsgA0EBaiEDDAALCyAHCxsAIANBAkYEfyAAIAEgAhAIBSAAIAEgAhAHCwueAQIEfwF7IAJBcHEhBAJAA0AgAyAETw0BIANBgBBqIQUgBSAESwRAIAQhBQtBAP0QIQcDQCAHIAAgA2r9AAQAIAEgA2r9AAQA/U79Yv19/Y4BIQcgA0EQaiEDIAMgBUkNAAsgBiAHEAZqIQYMAAsLAkADQCADIAJPDQEgBiAAIANqLQAAIAEgA2otAABxaWohBiADQQFqIQMMAAsLIAYLGwAgA0ECRgR/IAAgASACEAwFIAAgASACEAoLC9oCAwR/CHsEf0HVACERQdUAIRJB1QD9DyEHQaoB/Q8hCCACQXBxIQQCQANAIAMgBE8NASADQeADaiEFIAUgBEsEQCAEIQULQQD9DyEOQQD9DyEMQQD9DyENA0AgACADav0ABAAhCSABIANq/QAEACEKIAkgCv1OIQsgDiAL/WL9biEOIAwgCyAI/U79Yv1uIQwgDSAJIApBAf1tIAf9TiAKQQH9ayAI/U79UP1O/WL9biENIANBEGohAyADIAVJDQALIAYgDv19EAYgDP19EAZBA2wgDf19EAZBAXRqamohBgwACwsCQANAIAMgAk8NASAAIANqLQAAIQ8gASADai0AACEQIAYgDyARcSAQcWkgDyARcSAQQQF2IBJxcWlBAXRqIA9BAXYgEnEgECARcXFpQQF0IA9BAXYgEnEgEEEBdiAScXFpQQJ0ampqIQYgA0EBaiEDDAALCyAGC/wBAwN/CHsCfyACQXBxIQRBD/0PIQxBAP0RIQ1BACEDAkADQCADIARPDQEgACADav0ABAAhBiABIANq/QAEACEHIAYgDP1OIQggBkEE/W0hCSAHIAz9TiEKIAdBBP1tIQsgDSAIIAr9ngEgCSAL/Z4B/Y4B/X8gCCAK/Z8BIAkgC/2fAf2OAf1//a4B/a4BIQ0gA0EQaiEDDAALCyAN/RsAIA39GwFqIA39GwIgDf0bA2pqIQUCQANAIAMgAk8NASAAIANqLQAAIQ8gASADai0AACEOIAUgD0EPcSAOQQ9xbCAPQQR2IA5BBHZsamohBSADQQFqIQMMAAsLIAUL";
744
+
745
+ // src/vector/simd.ts
746
+ var wasmExports = null;
747
+ var f32View = null;
748
+ function decodeBase64(base64) {
749
+ if (typeof Buffer !== "undefined") {
750
+ return new Uint8Array(Buffer.from(base64, "base64"));
751
+ }
752
+ const binary = atob(base64);
753
+ const bytes = new Uint8Array(binary.length);
754
+ for (let i = 0; i < binary.length; i++) {
755
+ bytes[i] = binary.charCodeAt(i);
756
+ }
757
+ return bytes;
758
+ }
759
+ try {
760
+ const bytes = decodeBase64(SIMD_DISTANCE_WASM_BASE64);
761
+ const wasmBuffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
762
+ if (typeof WebAssembly !== "undefined" && typeof WebAssembly.validate === "function" && WebAssembly.validate(wasmBuffer)) {
763
+ const module = new WebAssembly.Module(wasmBuffer);
764
+ const instance = new WebAssembly.Instance(module);
765
+ wasmExports = instance.exports;
766
+ f32View = new Float32Array(wasmExports.memory.buffer);
767
+ }
768
+ } catch {
769
+ wasmExports = null;
770
+ f32View = null;
771
+ }
772
+ if (wasmExports === null) markSimdUnavailable();
773
+ var OFFSET_B_BYTES = 16384;
774
+ function ensureMemory(dim) {
775
+ if (!wasmExports || !f32View) return;
776
+ const needed = OFFSET_B_BYTES + dim * 4;
777
+ const currentSize = wasmExports.memory.buffer.byteLength;
778
+ if (needed > currentSize) {
779
+ const pagesNeeded = Math.ceil(needed / 65536);
780
+ const currentPages = currentSize / 65536;
781
+ wasmExports.memory.grow(pagesNeeded - currentPages);
782
+ f32View = new Float32Array(wasmExports.memory.buffer);
783
+ }
784
+ }
785
+ function getF32View() {
786
+ if (!f32View) {
787
+ throw new Error("SIMD memory is not initialized");
788
+ }
789
+ return f32View;
790
+ }
791
+ function copyOneVector(a) {
792
+ getF32View().set(a, 0);
793
+ }
794
+ function copyTwoVectors(a, b) {
795
+ const view = getF32View();
796
+ view.set(a, 0);
797
+ view.set(b, OFFSET_B_BYTES / 4);
798
+ }
799
+ function simdDotProduct(a, b) {
800
+ if (!wasmExports || !f32View) return null;
801
+ ensureMemory(a.length);
802
+ copyTwoVectors(a, b);
803
+ return wasmExports.dot_product(0, OFFSET_B_BYTES, a.length);
804
+ }
805
+ function simdMagnitude(a) {
806
+ if (!wasmExports || !f32View) return null;
807
+ ensureMemory(a.length);
808
+ copyOneVector(a);
809
+ return wasmExports.magnitude(0, a.length);
810
+ }
811
+ function simdSquaredEuclideanDistance(a, b) {
812
+ if (!wasmExports || !f32View) return null;
813
+ ensureMemory(a.length);
814
+ copyTwoVectors(a, b);
815
+ return wasmExports.squared_euclidean_distance(0, OFFSET_B_BYTES, a.length);
816
+ }
817
+ var arenaModule;
818
+ function getArenaModule() {
819
+ if (arenaModule !== void 0) return arenaModule;
820
+ try {
821
+ const bytes = decodeBase64(SIMD_DISTANCE_WASM_BASE64);
822
+ const wasmBuffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
823
+ if (typeof WebAssembly === "undefined" || typeof WebAssembly.validate !== "function" || !WebAssembly.validate(wasmBuffer)) {
824
+ arenaModule = null;
825
+ return null;
826
+ }
827
+ arenaModule = new WebAssembly.Module(wasmBuffer);
828
+ } catch {
829
+ arenaModule = null;
830
+ }
831
+ return arenaModule;
832
+ }
833
+ function createArenaSimd() {
834
+ try {
835
+ const module = getArenaModule();
836
+ if (module === null) return null;
837
+ const instance = new WebAssembly.Instance(module);
838
+ const exports$1 = instance.exports;
839
+ if (typeof exports$1.dot_u8 !== "function" || typeof exports$1.sqdist_u8 !== "function" || typeof exports$1.osq_dot_nibbles_4x4 !== "function" || typeof exports$1.osq_dot_bits !== "function" || typeof exports$1.osq_dot_bits_pair !== "function" || typeof exports$1.dot_product !== "function" || typeof exports$1.magnitude !== "function" || typeof exports$1.squared_euclidean_distance !== "function" || !(exports$1.memory instanceof WebAssembly.Memory)) {
840
+ return null;
841
+ }
842
+ return exports$1;
843
+ } catch {
844
+ return null;
845
+ }
846
+ }
847
+ var sharedMemoryModule;
848
+ function getSharedMemoryModule() {
849
+ if (sharedMemoryModule !== void 0) return sharedMemoryModule;
850
+ try {
851
+ const bytes = decodeBase64(SIMD_DISTANCE_SHARED_MEMORY_WASM_BASE64);
852
+ const wasmBuffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
853
+ if (typeof WebAssembly === "undefined" || typeof WebAssembly.validate !== "function" || !WebAssembly.validate(wasmBuffer)) {
854
+ sharedMemoryModule = null;
855
+ return null;
856
+ }
857
+ sharedMemoryModule = new WebAssembly.Module(wasmBuffer);
858
+ } catch {
859
+ sharedMemoryModule = null;
860
+ }
861
+ return sharedMemoryModule;
862
+ }
863
+ function createSharedArenaSimd(memory) {
864
+ try {
865
+ const module = getSharedMemoryModule();
866
+ if (module === null) return null;
867
+ const instance = new WebAssembly.Instance(module, { env: { memory } });
868
+ const exports$1 = instance.exports;
869
+ if (typeof exports$1.dot_u8 !== "function" || typeof exports$1.sqdist_u8 !== "function" || typeof exports$1.osq_dot_nibbles_4x4 !== "function" || typeof exports$1.osq_dot_bits !== "function" || typeof exports$1.osq_dot_bits_pair !== "function" || typeof exports$1.dot_product !== "function" || typeof exports$1.magnitude !== "function" || typeof exports$1.squared_euclidean_distance !== "function") {
870
+ return null;
871
+ }
872
+ return {
873
+ memory,
874
+ dot_product: exports$1.dot_product,
875
+ magnitude: exports$1.magnitude,
876
+ squared_euclidean_distance: exports$1.squared_euclidean_distance,
877
+ dot_u8: exports$1.dot_u8,
878
+ sqdist_u8: exports$1.sqdist_u8,
879
+ osq_dot_nibbles_4x4: exports$1.osq_dot_nibbles_4x4,
880
+ osq_dot_bits: exports$1.osq_dot_bits,
881
+ osq_dot_bits_pair: exports$1.osq_dot_bits_pair
882
+ };
883
+ } catch {
884
+ return null;
885
+ }
886
+ }
887
+ function arenaFloat32Distance(simd, byteA, byteB, dimension, metric, magnitudeA, magnitudeB) {
888
+ if (metric === "euclidean") {
889
+ return Math.sqrt(simd.squared_euclidean_distance(byteA, byteB, dimension));
890
+ }
891
+ const dot = simd.dot_product(byteA, byteB, dimension);
892
+ if (metric === "dotProduct") return -dot;
893
+ if (magnitudeA === 0 || magnitudeB === 0) return 1;
894
+ return 1 - dot / (magnitudeA * magnitudeB);
895
+ }
896
+
897
+ // src/vector/vector-store/blocks.ts
898
+ var SCRATCH_ALIGNMENT_BYTES = 16;
899
+ var BLOCK_MAX_BYTES = MAX_WASM_PAGES * WASM_PAGE_BYTES;
900
+ function alignUp(bytes) {
901
+ return Math.ceil(bytes / SCRATCH_ALIGNMENT_BYTES) * SCRATCH_ALIGNMENT_BYTES;
902
+ }
903
+ function computeVectorBlockLayout(dimension, blockBytes2 = BLOCK_MAX_BYTES) {
904
+ const scratchStride = alignUp(dimension * 4);
905
+ const slotsOffset = alignUp(VECTOR_SCRATCH_SLOTS * scratchStride);
906
+ const slotStride = alignUp(dimension * 4);
907
+ return {
908
+ dimension,
909
+ scratchStride,
910
+ slotsOffset,
911
+ slotStride,
912
+ capacity: Math.max(1, Math.floor((Math.min(blockBytes2, BLOCK_MAX_BYTES) - slotsOffset) / slotStride))
913
+ };
914
+ }
915
+ function initialBlockBytes(layout) {
916
+ return layout.slotsOffset + VECTOR_BLOCK_INITIAL_SLOTS * layout.slotStride;
917
+ }
918
+ function pagesFor(bytes) {
919
+ return Math.ceil(bytes / WASM_PAGE_BYTES);
920
+ }
921
+ function createSharedMemory(pages) {
922
+ if (!sharedMemoryAvailable() || typeof WebAssembly === "undefined") return null;
923
+ try {
924
+ const memory = new WebAssembly.Memory({ initial: pages, maximum: MAX_WASM_PAGES, shared: true });
925
+ return createSharedArenaSimd(memory) === null ? null : memory;
926
+ } catch {
927
+ return null;
928
+ }
929
+ }
930
+ function createVectorBlock(layout) {
931
+ const initialBytes = initialBlockBytes(layout);
932
+ const shared = createSharedMemory(pagesFor(initialBytes));
933
+ if (shared !== null) return { layout, storage: { kind: "shared-memory", memory: shared } };
934
+ const simd = createArenaSimd();
935
+ if (simd !== null) {
936
+ const missing = pagesFor(initialBytes) - simd.memory.buffer.byteLength / WASM_PAGE_BYTES;
937
+ if (missing > 0) simd.memory.grow(missing);
938
+ return { layout, storage: { kind: "private-memory", simd } };
939
+ }
940
+ return { layout, storage: { kind: "bytes", buffer: createGrowableBuffer(initialBytes, fullBlockBytes(layout)) } };
941
+ }
942
+ function fullBlockBytes(layout) {
943
+ return Math.min(BLOCK_MAX_BYTES, layout.slotsOffset + layout.capacity * layout.slotStride);
944
+ }
945
+ function growMemoryTo(memory, bytes, layout) {
946
+ const have = memory.buffer.byteLength;
947
+ if (have >= bytes) return;
948
+ memory.grow(pagesFor(nextGrowthTarget(have, bytes, fullBlockBytes(layout))) - have / WASM_PAGE_BYTES);
949
+ }
950
+ function ensureBlockSlots(handle, slots) {
951
+ const bytes = handle.layout.slotsOffset + Math.min(slots, handle.layout.capacity) * handle.layout.slotStride;
952
+ const storage = handle.storage;
953
+ if (storage.kind === "bytes") {
954
+ growBufferTo(storage.buffer, bytes);
955
+ return;
956
+ }
957
+ growMemoryTo(storage.kind === "shared-memory" ? storage.memory : storage.simd.memory, bytes, handle.layout);
958
+ }
959
+ function blockGrowthBytes(handle, layout, slots) {
960
+ const bytes = layout.slotsOffset + Math.min(slots, layout.capacity) * layout.slotStride;
961
+ if (handle !== null && handle.storage.kind === "bytes") {
962
+ const buffer = handle.storage.buffer;
963
+ return grownByteLength(buffer.byteLength, bytes, buffer.maxByteLength) - buffer.byteLength;
964
+ }
965
+ const have = handle === null ? 0 : blockBuffer(handle).byteLength;
966
+ const start = handle === null ? pagesFor(initialBlockBytes(layout)) * WASM_PAGE_BYTES : have;
967
+ return grownByteLength(start, bytes, fullBlockBytes(layout), WASM_PAGE_BYTES) - have;
968
+ }
969
+ function blocksGrowthBytes(blocks, layout, firstOrdinal, lastOrdinal) {
970
+ let bytes = 0;
971
+ const lastBlock = Math.floor(lastOrdinal / layout.capacity);
972
+ for (let block = Math.floor(firstOrdinal / layout.capacity); block <= lastBlock; block++) {
973
+ const slots = block === lastBlock ? lastOrdinal % layout.capacity + 1 : layout.capacity;
974
+ bytes += blockGrowthBytes(blocks[block] ?? null, layout, slots);
975
+ }
976
+ return bytes;
977
+ }
978
+ function blockBuffer(handle) {
979
+ const storage = handle.storage;
980
+ if (storage.kind === "bytes") return storage.buffer;
981
+ return storage.kind === "shared-memory" ? storage.memory.buffer : storage.simd.memory.buffer;
982
+ }
983
+ var NOTHING_STAGED = -1;
984
+ var QUERY_STAGED = -2;
985
+ function openVectorBlock(handle, threadSlot) {
986
+ const { layout, storage } = handle;
987
+ const simd = storage.kind === "shared-memory" ? createSharedArenaSimd(storage.memory) : storage.kind === "private-memory" ? storage.simd : null;
988
+ const hasScratch = simd !== null && threadSlot >= 0 && threadSlot < VECTOR_SCRATCH_SLOTS;
989
+ const scratchSlot = hasScratch ? threadSlot : 0;
990
+ let float32View;
991
+ let byteView;
992
+ let dataView;
993
+ function rebindViews() {
994
+ const buffer = blockBuffer(handle);
995
+ float32View = fixedView(buffer, Float32Array);
996
+ byteView = fixedView(buffer, Uint8Array);
997
+ dataView = new DataView(buffer, 0, byteView.length);
998
+ }
999
+ rebindViews();
1000
+ return {
1001
+ handle,
1002
+ simd,
1003
+ hasScratch,
1004
+ scratchByteOffset: scratchSlot * layout.scratchStride,
1005
+ stagedOrdinal: NOTHING_STAGED,
1006
+ float32(neededLength) {
1007
+ if (neededLength > float32View.length) rebindViews();
1008
+ return float32View;
1009
+ },
1010
+ bytes(neededLength) {
1011
+ if (neededLength > byteView.length) rebindViews();
1012
+ return byteView;
1013
+ },
1014
+ data(neededLength) {
1015
+ if (neededLength > byteView.length) rebindViews();
1016
+ return dataView;
1017
+ }
1018
+ };
1019
+ }
1020
+ function slotByteOffset(layout, localOrdinal) {
1021
+ return layout.slotsOffset + localOrdinal * layout.slotStride;
1022
+ }
1023
+
1024
+ // src/vector/osq/estimate.ts
1025
+ var POPCOUNT = new Uint8Array(256);
1026
+ for (let value = 0; value < 256; value++) {
1027
+ let count = 0;
1028
+ let rest = value;
1029
+ while (rest !== 0) {
1030
+ count += rest & 1;
1031
+ rest >>= 1;
1032
+ }
1033
+ POPCOUNT[value] = count;
1034
+ }
1035
+ var LEVEL_BIT_MASK = { 1: 255, 2: 85 };
1036
+ var STAGED_QUERY_PLANES = 4;
1037
+ function osqNibblesByByteLevels(document2, documentOffset, levels, bytes) {
1038
+ let total = 0;
1039
+ for (let n = 0; n < bytes; n++) {
1040
+ const packed = document2[documentOffset + n];
1041
+ total += (packed & 15) * levels[2 * n] + (packed >> 4) * levels[2 * n + 1];
1042
+ }
1043
+ return total;
1044
+ }
1045
+ function osqNarrowPairProducts(a, aOffset, b, bOffset, bits, bytes) {
1046
+ const mask = LEVEL_BIT_MASK[bits];
1047
+ let total = 0;
1048
+ for (let j = 0; j < bits; j++) {
1049
+ for (let l = 0; l < bits; l++) {
1050
+ let bitsSet = 0;
1051
+ for (let n = 0; n < bytes; n++) bitsSet += POPCOUNT[a[aOffset + n] >> j & b[bOffset + n] >> l & mask];
1052
+ total += 2 ** (j + l) * bitsSet;
1053
+ }
1054
+ }
1055
+ return total;
1056
+ }
1057
+ function osqNibbleProducts(a, aOffset, b, bOffset, bytes) {
1058
+ let total = 0;
1059
+ for (let n = 0; n < bytes; n++) {
1060
+ const left = a[aOffset + n];
1061
+ const right = b[bOffset + n];
1062
+ total += (left & 15) * (right & 15) + (left >> 4) * (right >> 4);
1063
+ }
1064
+ return total;
1065
+ }
1066
+ function twoBitStagedProducts(document2, documentOffset, streams, streamsOffset, streamBytes) {
1067
+ let total = 0;
1068
+ for (let n = 0; n < streamBytes; n++) {
1069
+ const packed = document2[documentOffset + n];
1070
+ for (let k = 0; k < STAGED_QUERY_PLANES; k++) {
1071
+ total += (packed >> 2 * k & 3) * streams[streamsOffset + k * streamBytes + n];
1072
+ }
1073
+ }
1074
+ return total;
1075
+ }
1076
+ function osqStagedQueryProducts(document2, documentOffset, documentBits, staged, stagedOffset, codeBytes) {
1077
+ if (documentBits === 2) return twoBitStagedProducts(document2, documentOffset, staged, stagedOffset, codeBytes);
1078
+ let total = 0;
1079
+ for (let p = 0; p < STAGED_QUERY_PLANES; p++) {
1080
+ const plane = stagedOffset + p * codeBytes;
1081
+ let bitsSet = 0;
1082
+ for (let n = 0; n < codeBytes; n++) bitsSet += POPCOUNT[document2[documentOffset + n] & staged[plane + n]];
1083
+ total += 2 ** p * bitsSet;
1084
+ }
1085
+ return total;
1086
+ }
1087
+ function osqByteLevelProducts(document2, documentOffset, query, queryOffset, dimension) {
1088
+ let total = 0;
1089
+ for (let i = 0; i < dimension; i++) total += document2[documentOffset + i] * query[queryOffset + i];
1090
+ return total;
1091
+ }
1092
+ function osqEstimate(products, document2, documentBits, query, queryBits, dimension, centroidDot, metric) {
1093
+ const documentStep = (document2.upper - document2.lower) / OSQ_STEPS[documentBits];
1094
+ const queryStep = (query.upper - query.lower) / OSQ_STEPS[queryBits];
1095
+ const centred = document2.lower * query.lower * dimension + query.lower * documentStep * document2.sum + document2.lower * queryStep * query.sum + documentStep * queryStep * products;
1096
+ if (metric === "euclidean") return Math.sqrt(Math.max(0, document2.correction + query.correction - 2 * centred));
1097
+ const similarity = centred + document2.correction + query.correction - centroidDot;
1098
+ if (metric === "cosine") return Math.min(Math.max(similarity, -1), 1);
1099
+ return similarity;
1100
+ }
1101
+ function osqDistance(estimate, metric) {
1102
+ switch (metric) {
1103
+ case "cosine":
1104
+ return 1 - estimate;
1105
+ case "dotProduct":
1106
+ return -estimate;
1107
+ case "euclidean":
1108
+ return estimate;
1109
+ }
1110
+ }
1111
+ function osqQueryBits(bits) {
1112
+ return OSQ_QUERY_BITS[bits];
1113
+ }
1114
+ function osqPlacementQueryBits(bits) {
1115
+ return OSQ_PLACEMENT_QUERY_BITS[bits];
1116
+ }
1117
+
1118
+ // src/vector/vector-store/code-blocks.ts
1119
+ function computeCodeBlockLayout(dimension, bits) {
1120
+ const queryBits = osqQueryBits(bits);
1121
+ const codeBytes = osqCodeBytes(dimension, bits);
1122
+ const queryBytes = osqStagedQueryBytes(dimension, bits);
1123
+ const scratchStride = alignUp(queryBytes);
1124
+ const slotsOffset = alignUp(VECTOR_SCRATCH_SLOTS * scratchStride);
1125
+ const slotStride = osqRecordBytes(dimension, bits);
1126
+ return {
1127
+ dimension,
1128
+ bits,
1129
+ queryBits,
1130
+ codeBytes,
1131
+ queryBytes,
1132
+ scratchStride,
1133
+ slotsOffset,
1134
+ slotStride,
1135
+ capacity: Math.floor((Math.min(VECTOR_MEMORY_LIMITS.blockBytes, BLOCK_MAX_BYTES) - slotsOffset) / slotStride)
1136
+ };
1137
+ }
1138
+
1139
+ // src/vector/vector-store/handles.ts
1140
+ var STORE_SLOTS = 0;
1141
+ var STORE_LIVE_COUNT = 1;
1142
+ var STORE_BLOCK_COUNT = 2;
1143
+ var STORE_CALIBRATED = 3;
1144
+ var STORE_CODE_COUNT = 4;
1145
+ var STORE_DOC_ID_BYTES = 5;
1146
+ var STORE_CODE_BLOCK_COUNT = 6;
1147
+ var STORE_CALIBRATION_GENERATION = 7;
1148
+ var STORE_RELEASED_VECTORS_TO_DISK = 8;
1149
+ var STORE_UNPLACED_COUNT = 9;
1150
+ var STORE_REMOVED_COUNT = 10;
1151
+ var STORE_ORDINAL_LAYOUT_GENERATION = 11;
1152
+ var STORE_HEADER_WORDS = 16;
1153
+ var IN_MEMORY = -1;
1154
+ function resetStoreHeader(header) {
1155
+ Atomics.store(header, STORE_SLOTS, 0);
1156
+ Atomics.store(header, STORE_RELEASED_VECTORS_TO_DISK, 0);
1157
+ Atomics.store(header, STORE_LIVE_COUNT, 0);
1158
+ Atomics.store(header, STORE_CODE_COUNT, 0);
1159
+ Atomics.store(header, STORE_CALIBRATED, 0);
1160
+ Atomics.add(header, STORE_CALIBRATION_GENERATION, 1);
1161
+ Atomics.add(header, STORE_ORDINAL_LAYOUT_GENERATION, 1);
1162
+ }
1163
+ function fixedInt32(words) {
1164
+ return new Int32Array(createFixedBuffer(words * 4), 0, words);
1165
+ }
1166
+ function fixedFloat32(length) {
1167
+ return new Float32Array(createFixedBuffer(length * 4), 0, length);
1168
+ }
1169
+ function unplacedChunkCount(ordinals) {
1170
+ return Math.floor(ordinals / UNPLACED_FLAG_CHUNK_ORDINALS) + 1;
1171
+ }
1172
+ function perOrdinal2(bytesPerOrdinal) {
1173
+ return createGrowableBuffer(
1174
+ VECTOR_STORE_INITIAL_CAPACITY * bytesPerOrdinal,
1175
+ (VECTOR_MEMORY_LIMITS.maxOrdinals + 1) * bytesPerOrdinal
1176
+ );
1177
+ }
1178
+ function inMemoryTable() {
1179
+ const buffer = perOrdinal2(4);
1180
+ new Int32Array(buffer, 0, buffer.byteLength / 4).fill(IN_MEMORY);
1181
+ return buffer;
1182
+ }
1183
+ function createSharedVectorStoreHandles(dimension, codeBits, blockBytes2 = VECTOR_MEMORY_LIMITS.blockBytes) {
1184
+ return {
1185
+ dimension,
1186
+ codeBits,
1187
+ layout: computeVectorBlockLayout(dimension, blockBytes2),
1188
+ codeLayout: codeBits === null ? null : computeCodeBlockLayout(dimension, codeBits),
1189
+ layoutRevision: 0,
1190
+ header: fixedInt32(STORE_HEADER_WORDS),
1191
+ blocks: [],
1192
+ codeBlocks: [],
1193
+ vectorFiles: [],
1194
+ magnitudes: perOrdinal2(8),
1195
+ present: perOrdinal2(1),
1196
+ unplaced: perOrdinal2(1),
1197
+ unplacedChunks: createGrowableBuffer(4, unplacedChunkCount(VECTOR_MEMORY_LIMITS.maxOrdinals) * 4),
1198
+ removed: perOrdinal2(1),
1199
+ partitions: perOrdinal2(4),
1200
+ docIdBytes: createGrowableBuffer(VECTOR_STORE_INITIAL_CAPACITY * 16, VECTOR_MEMORY_LIMITS.maxDocIdTableBytes),
1201
+ docIdOffsets: perOrdinal2(4),
1202
+ codePresent: perOrdinal2(1),
1203
+ diskFile: inMemoryTable(),
1204
+ diskOffset: perOrdinal2(4),
1205
+ centroid: fixedFloat32(dimension)
1206
+ };
1207
+ }
1208
+ function sharedVectorStoreBytes(handles) {
1209
+ let bytes = handles.header.byteLength + handles.centroid.byteLength;
1210
+ for (const block of handles.blocks) {
1211
+ if (block !== null) bytes += blockBuffer(block).byteLength;
1212
+ }
1213
+ for (const block of handles.codeBlocks) bytes += blockBuffer(block).byteLength;
1214
+ bytes += handles.magnitudes.byteLength;
1215
+ bytes += handles.present.byteLength;
1216
+ bytes += handles.unplaced.byteLength;
1217
+ bytes += handles.unplacedChunks.byteLength;
1218
+ bytes += handles.removed.byteLength;
1219
+ bytes += handles.partitions.byteLength;
1220
+ bytes += handles.docIdBytes.byteLength;
1221
+ bytes += handles.docIdOffsets.byteLength;
1222
+ bytes += handles.codePresent.byteLength;
1223
+ bytes += handles.diskFile.byteLength;
1224
+ bytes += handles.diskOffset.byteLength;
1225
+ return bytes;
1226
+ }
1227
+
1228
+ // src/vector/native/store.ts
1229
+ var METRIC_CODES2 = { cosine: 0, dotProduct: 1, euclidean: 2 };
1230
+ var attachedStores = /* @__PURE__ */ new WeakMap();
1231
+ function nativeMetricCode(metric) {
1232
+ return METRIC_CODES2[metric];
1233
+ }
1234
+ function isSharedMemory(buffer) {
1235
+ if (typeof SharedArrayBuffer === "function" && buffer instanceof SharedArrayBuffer) return true;
1236
+ return hostOwnsMemory(buffer);
1237
+ }
1238
+ function blockBytes(block) {
1239
+ const storage = block.storage;
1240
+ if (storage.kind === "shared-memory") return new Uint8Array(storage.memory.buffer, block.layout.slotsOffset);
1241
+ if (storage.kind === "bytes" && isSharedMemory(storage.buffer)) {
1242
+ const offset = block.layout.slotsOffset;
1243
+ return new Uint8Array(storage.buffer, offset, Math.max(0, storage.buffer.byteLength - offset));
1244
+ }
1245
+ return null;
1246
+ }
1247
+ function storeMemory(store) {
1248
+ const regions = [
1249
+ store.header.buffer,
1250
+ store.codePresent,
1251
+ store.centroid.buffer,
1252
+ store.magnitudes,
1253
+ store.present,
1254
+ store.diskFile,
1255
+ store.diskOffset
1256
+ ];
1257
+ if (!regions.every(isSharedMemory)) return null;
1258
+ const codeBlocks = [];
1259
+ for (const block of store.codeBlocks) {
1260
+ const bytes = blockBytes(block);
1261
+ if (bytes === null) return null;
1262
+ codeBlocks.push(bytes);
1263
+ }
1264
+ const vectorBlocks = [];
1265
+ for (const block of store.blocks) {
1266
+ const bytes = block === null ? null : blockBytes(block);
1267
+ if (block !== null && bytes === null) return null;
1268
+ vectorBlocks.push(bytes);
1269
+ }
1270
+ return {
1271
+ storeHeader: store.header,
1272
+ dimension: store.dimension,
1273
+ bits: store.codeBits ?? 0,
1274
+ recordsPerBlock: store.codeLayout?.capacity ?? 0,
1275
+ vectorsPerBlock: store.layout.capacity,
1276
+ vectorStrideBytes: store.layout.slotStride,
1277
+ codePresent: fixedView(store.codePresent, Uint8Array),
1278
+ centroid: store.centroid,
1279
+ magnitudes: fixedView(store.magnitudes, Float64Array),
1280
+ present: fixedView(store.present, Uint8Array),
1281
+ vectorFile: fixedView(store.diskFile, Int32Array),
1282
+ vectorOffset: fixedView(store.diskOffset, Uint32Array),
1283
+ vectorFiles: [...store.vectorFiles],
1284
+ codeBlocks,
1285
+ vectorBlocks
1286
+ };
1287
+ }
1288
+ function attach(core, store) {
1289
+ const memory = storeMemory(store);
1290
+ let handle = null;
1291
+ if (memory !== null) {
1292
+ try {
1293
+ handle = core.attachStore(memory);
1294
+ } catch {
1295
+ handle = null;
1296
+ }
1297
+ }
1298
+ return {
1299
+ core,
1300
+ handle,
1301
+ layoutRevision: store.layoutRevision,
1302
+ vectorBlockCount: store.blocks.length,
1303
+ codeBlockCount: store.codeBlocks.length,
1304
+ retryAt: handle === null ? Date.now() + NATIVE_ATTACH_RETRY_MS : Number.POSITIVE_INFINITY,
1305
+ ordinals: new Int32Array(0),
1306
+ distances: new Float64Array(0)
1307
+ };
1308
+ }
1309
+ function stillCurrent(attached, store) {
1310
+ return Date.now() < attached.retryAt && attached.layoutRevision === store.layoutRevision && attached.vectorBlockCount === store.blocks.length && attached.codeBlockCount === store.codeBlocks.length;
1311
+ }
1312
+ function isUsable(attached) {
1313
+ return attached.handle !== null;
1314
+ }
1315
+ function detachNativeStore(store) {
1316
+ const attached = attachedStores.get(store);
1317
+ if (attached === void 0) return;
1318
+ attachedStores.delete(store);
1319
+ if (attached.handle === null) return;
1320
+ try {
1321
+ attached.core.detachStore(attached.handle);
1322
+ } catch (error) {
1323
+ stopUsingTheNativeSearchCore(error);
1324
+ }
1325
+ }
1326
+ function attachedNativeStore(store) {
1327
+ const attached = attachedStores.get(store);
1328
+ return attached !== void 0 && isUsable(attached) ? attached : null;
1329
+ }
1330
+ function nativeStoreScratchBytes(store) {
1331
+ const attached = attachedNativeStore(store);
1332
+ return attached === null ? 0 : attached.ordinals.byteLength + attached.distances.byteLength;
1333
+ }
1334
+ function nativeStoreFor(store) {
1335
+ if (!nativeSearchCoreIsEnabled()) return null;
1336
+ const core = loadNativeCore();
1337
+ if (core === null) return null;
1338
+ let attached = attachedStores.get(store);
1339
+ if (attached !== void 0 && !stillCurrent(attached, store)) {
1340
+ detachNativeStore(store);
1341
+ attached = void 0;
1342
+ }
1343
+ if (attached === void 0) {
1344
+ attached = attach(core, store);
1345
+ attachedStores.set(store, attached);
1346
+ }
1347
+ return isUsable(attached) ? attached : null;
1348
+ }
1349
+ function reserveOrdinals(store, count) {
1350
+ if (store.ordinals.length >= count) return;
1351
+ store.ordinals = new Int32Array(count);
1352
+ store.distances = new Float64Array(count);
1353
+ }
1354
+ function nativeScores(store, query, metric, count) {
1355
+ if (count === 0) return store.distances;
1356
+ let status;
1357
+ try {
1358
+ status = store.core.score(store.handle, query, nativeMetricCode(metric), store.ordinals, count, store.distances);
1359
+ } catch (error) {
1360
+ stopUsingTheNativeSearchCore(error);
1361
+ return null;
1362
+ }
1363
+ return status === NATIVE_OK ? store.distances : null;
1364
+ }
1365
+ function nativeScoresOf(handles, query, metric, ordinals) {
1366
+ const store = nativeStoreFor(handles);
1367
+ if (store === null) return null;
1368
+ reserveOrdinals(store, ordinals.length);
1369
+ store.ordinals.set(ordinals);
1370
+ return nativeScores(store, query, metric, ordinals.length);
1371
+ }
1372
+ function workOnRecords(handles, ordinals, work) {
1373
+ if (ordinals.length === 0) return true;
1374
+ for (let attempt = 0; attempt < 2; attempt++) {
1375
+ const store = nativeStoreFor(handles);
1376
+ if (store === null) return false;
1377
+ let status;
1378
+ try {
1379
+ status = work(store);
1380
+ } catch (error) {
1381
+ stopUsingTheNativeSearchCore(error);
1382
+ return false;
1383
+ }
1384
+ if (status === NATIVE_OK) return true;
1385
+ if (status !== NATIVE_NEEDS_ROOM) return false;
1386
+ detachNativeStore(handles);
1387
+ }
1388
+ return false;
1389
+ }
1390
+ function nativeMeasure(handles, metric, ordinals, totals) {
1391
+ return workOnRecords(
1392
+ handles,
1393
+ ordinals,
1394
+ (store) => store.core.measure(store.handle, nativeMetricCode(metric), ordinals, ordinals.length, totals)
1395
+ );
1396
+ }
1397
+ function nativeQuantise(handles, metric, ordinals) {
1398
+ if (Atomics.load(handles.header, STORE_CALIBRATED) !== 1) return false;
1399
+ return workOnRecords(
1400
+ handles,
1401
+ ordinals,
1402
+ (store) => store.core.quantise(store.handle, nativeMetricCode(metric), ordinals, ordinals.length)
1403
+ );
1404
+ }
1405
+
1406
+ // src/vector/native/field.ts
1407
+ var WAKE_COUNT_WORDS = 1;
1408
+ var WAKES_THE_CORE_KEEPS_PER_CALL = 256;
1409
+ var WAKE_LIST_WORDS = WAKE_COUNT_WORDS + WAKES_THE_CORE_KEEPS_PER_CALL;
1410
+ var WAKE_ENTRY_LOCK = -1;
1411
+ var attachedGraphs = /* @__PURE__ */ new WeakMap();
1412
+ function graphMemory(graph) {
1413
+ const regions = [
1414
+ graph.header.buffer,
1415
+ graph.nodeLevels,
1416
+ graph.level0,
1417
+ graph.upperBase,
1418
+ graph.upper,
1419
+ graph.locks,
1420
+ graph.tombstones,
1421
+ graph.incoming,
1422
+ graph.nearestLinkDue,
1423
+ graph.heldLocks.buffer
1424
+ ];
1425
+ if (!regions.every(isSharedMemory)) return null;
1426
+ return {
1427
+ graphHeader: graph.header,
1428
+ nodeLevels: fixedView(graph.nodeLevels, Uint8Array),
1429
+ level0: fixedView(graph.level0, Int32Array),
1430
+ upperBase: fixedView(graph.upperBase, Int32Array),
1431
+ upper: fixedView(graph.upper, Int32Array),
1432
+ locks: fixedView(graph.locks, Int32Array),
1433
+ tombstones: fixedView(graph.tombstones, Uint8Array),
1434
+ incoming: fixedView(graph.incoming, Int32Array),
1435
+ nearestLinkDue: fixedView(graph.nearestLinkDue, Uint8Array),
1436
+ heldLocks: graph.heldLocks
1437
+ };
1438
+ }
1439
+ function attach2(core, state) {
1440
+ const memory = graphMemory(state.adjacency.handles);
1441
+ let handle = null;
1442
+ if (memory !== null) {
1443
+ try {
1444
+ handle = core.attachGraph(memory);
1445
+ } catch {
1446
+ handle = null;
1447
+ }
1448
+ }
1449
+ return { handle, field: null };
1450
+ }
1451
+ var workspaceOwner = null;
1452
+ function nativeScratchBytes(state, field) {
1453
+ const perField = field.store.ordinals.byteLength + field.store.distances.byteLength + field.wakes.byteLength;
1454
+ const owner = workspaceOwner?.deref();
1455
+ if (owner !== void 0 && owner !== state) return perField;
1456
+ workspaceOwner = new WeakRef(state);
1457
+ return perField + field.core.workspaceBytes();
1458
+ }
1459
+ function attachedNativeField(state) {
1460
+ if (!nativeSearchCoreIsEnabled()) return null;
1461
+ return attachedGraphs.get(state)?.field ?? null;
1462
+ }
1463
+ function nativeFieldFor(state) {
1464
+ if (!nativeSearchCoreIsEnabled()) return null;
1465
+ const core = loadNativeCore();
1466
+ if (core === null) return null;
1467
+ const store = nativeStoreFor(state.store.handles);
1468
+ if (store === null) return null;
1469
+ let attached = attachedGraphs.get(state);
1470
+ if (attached === void 0) {
1471
+ attached = attach2(core, state);
1472
+ attachedGraphs.set(state, attached);
1473
+ }
1474
+ if (attached.handle === null) return null;
1475
+ if (attached.field === null) {
1476
+ attached.field = { core, graph: attached.handle, store, wakes: new Int32Array(WAKE_LIST_WORDS) };
1477
+ }
1478
+ attached.field.store = store;
1479
+ return attached.field;
1480
+ }
1481
+ function wakeSleepingThreads(state, field) {
1482
+ wakeListedThreads(state, field.wakes);
1483
+ }
1484
+ function wakeListedThreads(state, wakes) {
1485
+ const count = Math.min(wakes[0] ?? 0, wakes.length - WAKE_COUNT_WORDS);
1486
+ if (count <= 0) return;
1487
+ wakes[0] = 0;
1488
+ for (let i = 1; i <= count; i++) {
1489
+ const ordinal = wakes[i];
1490
+ if (ordinal === WAKE_ENTRY_LOCK) {
1491
+ Atomics.notify(state.header, GRAPH_ENTRY_LOCK);
1492
+ continue;
1493
+ }
1494
+ if (ordinal < 0 || ordinal >= Atomics.load(state.header, GRAPH_SLOTS)) continue;
1495
+ Atomics.notify(state.locks.words(ordinal), ordinal);
1496
+ }
1497
+ }
1498
+
1499
+ // src/vector/native/thread-slots.ts
1500
+ var freeSlots = Array.from({ length: NATIVE_THREAD_SEARCH_SLOTS }, (_, i) => VECTOR_SCRATCH_SLOTS - 1 - i);
1501
+ var waitingForSlot = [];
1502
+ function hasNativeThreads(core) {
1503
+ return core.searchAsync !== void 0 && core.scoreAsync !== void 0;
1504
+ }
1505
+ function threadedNativeCore() {
1506
+ if (!nativeSearchCoreIsEnabled()) return null;
1507
+ const core = loadNativeCore();
1508
+ return core !== null && hasNativeThreads(core) ? core : null;
1509
+ }
1510
+ function claimSlot() {
1511
+ const slot = freeSlots.pop();
1512
+ if (slot !== void 0) return Promise.resolve(slot);
1513
+ return new Promise((resolve) => waitingForSlot.push(resolve));
1514
+ }
1515
+ function claimNativeThreadSlotNow() {
1516
+ return freeSlots.pop() ?? null;
1517
+ }
1518
+ function returnSlot(slot) {
1519
+ const next = waitingForSlot.shift();
1520
+ if (next !== void 0) {
1521
+ next(slot);
1522
+ return;
1523
+ }
1524
+ freeSlots.push(slot);
1525
+ }
1526
+ async function withNativeThreadSlot(work) {
1527
+ const slot = await claimSlot();
1528
+ try {
1529
+ return await work(slot);
1530
+ } finally {
1531
+ returnSlot(slot);
1532
+ }
1533
+ }
1534
+
1535
+ // src/vector/native/writes.ts
1536
+ var CALL_RAISED = -1;
1537
+ var NO_NEARBY_NODES = new Int32Array(0);
1538
+ function writeToTheGraph(state, field, write) {
1539
+ let status;
1540
+ try {
1541
+ status = write();
1542
+ } catch (error) {
1543
+ stopUsingTheNativeSearchCore(error);
1544
+ return CALL_RAISED;
1545
+ }
1546
+ wakeSleepingThreads(state, field);
1547
+ return status;
1548
+ }
1549
+ function nativePlace(state, field, metric, ordinal, topLayer, graphHeldAlone) {
1550
+ const status = writeToTheGraph(
1551
+ state,
1552
+ field,
1553
+ () => field.core.place(
1554
+ field.graph,
1555
+ field.store.handle,
1556
+ nativeMetricCode(metric),
1557
+ ordinal,
1558
+ topLayer,
1559
+ graphHeldAlone ? 1 : 0,
1560
+ state.locks.threadSlot,
1561
+ field.wakes
1562
+ )
1563
+ );
1564
+ if (status === NATIVE_OK) return "placed";
1565
+ return status === NATIVE_NEEDS_ROOM ? "needsRoom" : "failed";
1566
+ }
1567
+ function nativeRemove(state, field, ordinal) {
1568
+ const remove = () => field.core.remove(field.graph, ordinal, state.locks.threadSlot, field.wakes);
1569
+ return writeToTheGraph(state, field, remove) === NATIVE_OK;
1570
+ }
1571
+ function nativeCompact(state, field, metric) {
1572
+ const compact = () => field.core.compact(field.graph, field.store.handle, nativeMetricCode(metric), state.locks.threadSlot, field.wakes);
1573
+ return writeToTheGraph(state, field, compact) === NATIVE_OK;
1574
+ }
1575
+ function nativeConnect(state, field, metric, nearby) {
1576
+ const connect = () => field.core.connectUnreached(
1577
+ field.graph,
1578
+ field.store.handle,
1579
+ nativeMetricCode(metric),
1580
+ nearby,
1581
+ state.locks.threadSlot,
1582
+ field.wakes
1583
+ );
1584
+ return writeToTheGraph(state, field, connect) === NATIVE_OK;
1585
+ }
1586
+ function connectsOffThread(core) {
1587
+ return core.connectUnreachedAsync !== void 0;
1588
+ }
1589
+ function nativeConnectsOffThread(field) {
1590
+ return connectsOffThread(field.core);
1591
+ }
1592
+ async function nativeConnectOffThread(state, field, metric) {
1593
+ const core = field.core;
1594
+ if (!connectsOffThread(core)) return "failed";
1595
+ const slot = claimNativeThreadSlotNow();
1596
+ if (slot === null) return "graphBusy";
1597
+ let outcome;
1598
+ try {
1599
+ outcome = await core.connectUnreachedAsync(
1600
+ field.graph,
1601
+ field.store.handle,
1602
+ nativeMetricCode(metric),
1603
+ NO_NEARBY_NODES,
1604
+ slot
1605
+ );
1606
+ } catch {
1607
+ return "failed";
1608
+ } finally {
1609
+ returnSlot(slot);
1610
+ }
1611
+ wakeListedThreads(state, outcome.wakes);
1612
+ Atomics.notify(state.header, GRAPH_LOCK);
1613
+ if (outcome.status === NATIVE_OK) return "connected";
1614
+ return outcome.status === NATIVE_GRAPH_NOT_HELD_ALONE ? "graphBusy" : "failed";
1615
+ }
1616
+
1617
+ // src/vector/hnsw/connect-steps.ts
1618
+ function newPace() {
1619
+ return { steps: 0 };
1620
+ }
1621
+ function stepTaken(pace) {
1622
+ pace.steps += 1;
1623
+ if (pace.steps < CONNECT_STEPS_BETWEEN_CLOCK_CHECKS) return false;
1624
+ pace.steps = 0;
1625
+ return true;
1626
+ }
1627
+ function drain(steps) {
1628
+ for (; ; ) {
1629
+ const next = steps.next();
1630
+ if (next.done === true) return next.value;
1631
+ }
1632
+ }
1633
+
1634
+ // src/vector/similarity.ts
1635
+ function magnitude(v) {
1636
+ const result = simdMagnitude(v);
1637
+ if (result !== null) return result;
1638
+ let sum = 0;
1639
+ for (let i = 0; i < v.length; i++) {
1640
+ sum += v[i] * v[i];
1641
+ }
1642
+ return Math.sqrt(sum);
1643
+ }
1644
+ function cosineSimilarityWithMagnitudes(a, b, magA, magB) {
1645
+ if (magA === 0 || magB === 0) return 0;
1646
+ return dotProduct(a, b) / (magA * magB);
1647
+ }
1648
+ function dotProduct(a, b) {
1649
+ const result = simdDotProduct(a, b);
1650
+ if (result !== null) return result;
1651
+ let sum = 0;
1652
+ for (let i = 0; i < a.length; i++) {
1653
+ sum += a[i] * b[i];
1654
+ }
1655
+ return sum;
1656
+ }
1657
+ function euclideanDistance(a, b) {
1658
+ return Math.sqrt(squaredEuclideanDistance(a, b));
1659
+ }
1660
+ function squaredEuclideanDistance(a, b) {
1661
+ const result = simdSquaredEuclideanDistance(a, b);
1662
+ if (result !== null) return result;
1663
+ let sum = 0;
1664
+ for (let i = 0; i < a.length; i++) {
1665
+ const diff = a[i] - b[i];
1666
+ sum += diff * diff;
1667
+ }
1668
+ return sum;
1669
+ }
1670
+
1671
+ // src/vector/hnsw/insertion-order.ts
1672
+ var GOLDEN_GAMMA = 2654435769;
1673
+ var FIRST_MIX_MULTIPLIER = 569420461;
1674
+ var SECOND_MIX_MULTIPLIER = 1935289751;
1675
+ var TWO_TO_THE_32 = 4294967296;
1676
+ var HALF_A_STEP = 0.5;
1677
+ function mixedUint32(value) {
1678
+ let mixed = value + GOLDEN_GAMMA | 0;
1679
+ mixed ^= mixed >>> 16;
1680
+ mixed = Math.imul(mixed, FIRST_MIX_MULTIPLIER);
1681
+ mixed ^= mixed >>> 15;
1682
+ mixed = Math.imul(mixed, SECOND_MIX_MULTIPLIER);
1683
+ mixed ^= mixed >>> 15;
1684
+ return mixed >>> 0;
1685
+ }
1686
+ function unitDrawOf(value) {
1687
+ return (mixedUint32(value) + HALF_A_STEP) / TWO_TO_THE_32;
1688
+ }
1689
+ function shuffleStep(items, i, stream) {
1690
+ const next = mixedUint32(stream);
1691
+ const j = Math.floor(next / TWO_TO_THE_32 * (i + 1));
1692
+ const held = items[i];
1693
+ items[i] = items[j];
1694
+ items[j] = held;
1695
+ return next;
1696
+ }
1697
+ function shuffleInPlace(items) {
1698
+ let stream = items.length;
1699
+ for (let i = items.length - 1; i > 0; i--) {
1700
+ stream = mixedUint32(stream);
1701
+ const j = Math.floor(stream / TWO_TO_THE_32 * (i + 1));
1702
+ const held = items[i];
1703
+ items[i] = items[j];
1704
+ items[j] = held;
1705
+ }
1706
+ return items;
1707
+ }
1708
+
1709
+ // src/vector/hnsw/shared.ts
1710
+ function entryPointOf(state) {
1711
+ return Atomics.load(state.header, GRAPH_ENTRY_POINT);
1712
+ }
1713
+ function topLayerOf(state) {
1714
+ return Atomics.load(state.header, GRAPH_TOP_LAYER);
1715
+ }
1716
+ function nodeCountOf(state) {
1717
+ return Atomics.load(state.header, GRAPH_NODE_COUNT);
1718
+ }
1719
+ function tombstoneCountOf(state) {
1720
+ return Atomics.load(state.header, GRAPH_TOMBSTONE_COUNT);
1721
+ }
1722
+ function ensureCapacity(state, needed) {
1723
+ ensureAdjacencyCapacity(state.adjacency, needed);
1724
+ ensureVisited(state, needed);
1725
+ }
1726
+ function ensureVisited(state, needed) {
1727
+ if (needed <= state.visited.length) return;
1728
+ let capacity = state.visited.length === 0 ? 16 : state.visited.length;
1729
+ while (capacity < needed) capacity *= 2;
1730
+ const next = new Uint32Array(capacity);
1731
+ next.set(state.visited);
1732
+ state.visited = next;
1733
+ }
1734
+ function nextVisitStamp(state) {
1735
+ state.visitStamp++;
1736
+ if (state.visitStamp === 4294967295) {
1737
+ state.visited.fill(0);
1738
+ state.visitStamp = 1;
1739
+ }
1740
+ return state.visitStamp;
1741
+ }
1742
+ function reachTombstone(state, ord) {
1743
+ if (ord < state.tombstones.length) return true;
1744
+ if (ord >= state.adjacency.handles.tombstones.byteLength) return false;
1745
+ state.tombstones = fixedView(state.adjacency.handles.tombstones, Uint8Array);
1746
+ return ord < state.tombstones.length;
1747
+ }
1748
+ function isTombstoned(state, ord) {
1749
+ return ord >= 0 && reachTombstone(state, ord) && state.tombstones[ord] === 1;
1750
+ }
1751
+ function nodeExists(state, ord) {
1752
+ return hasNode(state.adjacency, ord);
1753
+ }
1754
+ function nodeMaxLayer(state, ord) {
1755
+ return nodeLevel(state.adjacency, ord);
1756
+ }
1757
+ function toDistance(a, b, magA, magB, metric) {
1758
+ switch (metric) {
1759
+ case "cosine":
1760
+ return 1 - cosineSimilarityWithMagnitudes(a, b, magA, magB);
1761
+ case "dotProduct":
1762
+ return -dotProduct(a, b);
1763
+ case "euclidean":
1764
+ return euclideanDistance(a, b);
1765
+ }
1766
+ }
1767
+ function toScore(distance, metric) {
1768
+ switch (metric) {
1769
+ case "cosine":
1770
+ return 1 - distance;
1771
+ case "dotProduct":
1772
+ return -distance;
1773
+ case "euclidean":
1774
+ return 1 / (1 + distance);
1775
+ }
1776
+ }
1777
+ function levelOfOrdinal(ord, mL) {
1778
+ return Math.min(Math.floor(-Math.log(unitDrawOf(ord)) * mL), MAX_LAYER_CAP);
1779
+ }
1780
+ function buildsFromCodes(state) {
1781
+ return activeQuantizer(state) !== void 0;
1782
+ }
1783
+ function activeQuantizer(state) {
1784
+ const quantizer = state.quantizer;
1785
+ if (quantizer === void 0 || !quantizer.isCalibrated()) return void 0;
1786
+ return quantizer;
1787
+ }
1788
+ function nodeDistanceFunction(state, metric) {
1789
+ const storeDistance = state.store.pairDistance(metric);
1790
+ const quantizer = state.quantizer;
1791
+ if (quantizer === void 0 || !buildsFromCodes(state)) return storeDistance;
1792
+ const codeDistance = quantizer.pairDistance();
1793
+ return (aOrd, bOrd) => {
1794
+ const estimated = codeDistance(aOrd, bOrd);
1795
+ return estimated === Number.POSITIVE_INFINITY ? storeDistance(aOrd, bOrd) : estimated;
1796
+ };
1797
+ }
1798
+ function queryDistanceByOrd(state, qVec, qMag, ord, metric) {
1799
+ return state.store.distanceFromQuery(qVec, qMag, ord, metric);
1800
+ }
1801
+ function maxConns(state, layer) {
1802
+ return layer === 0 ? state.Mmax0 : state.M;
1803
+ }
1804
+ function addConnection(connections, ord) {
1805
+ for (let i = 0; i < connections.length; i++) {
1806
+ if (connections[i] === ord) return;
1807
+ }
1808
+ connections.push(ord);
1809
+ }
1810
+
1811
+ // src/vector/hnsw/workspace.ts
1812
+ function createList(capacity) {
1813
+ return { ords: new Int32Array(capacity), distances: new Float64Array(capacity), size: 0 };
1814
+ }
1815
+ function createHeap(greatestFirst) {
1816
+ return { ...createList(INITIAL_LIST_CAPACITY), greatestFirst, topOrd: -1, topDistance: 0 };
1817
+ }
1818
+ function createHNSWWorkspace() {
1819
+ return {
1820
+ frontier: createHeap(false),
1821
+ found: createHeap(true),
1822
+ entryPoints: new Int32Array(INITIAL_LIST_CAPACITY),
1823
+ entryPointCount: 0,
1824
+ traversal: createList(INITIAL_LIST_CAPACITY),
1825
+ working: createList(INITIAL_LIST_CAPACITY),
1826
+ linkSelections: [],
1827
+ pruneCandidates: createList(INITIAL_LIST_CAPACITY),
1828
+ pruneSelection: createList(INITIAL_LIST_CAPACITY),
1829
+ repairCandidates: createList(INITIAL_LIST_CAPACITY),
1830
+ repairSelection: createList(INITIAL_LIST_CAPACITY),
1831
+ droppedLinks: createList(INITIAL_LIST_CAPACITY)
1832
+ };
1833
+ }
1834
+ function linkSelectionsFor(workspace, layers) {
1835
+ const selections = workspace.linkSelections;
1836
+ while (selections.length < layers) selections.push(createList(INITIAL_LIST_CAPACITY));
1837
+ return selections;
1838
+ }
1839
+ function ensureListCapacity(list, needed) {
1840
+ if (needed <= list.ords.length) return;
1841
+ let capacity = list.ords.length === 0 ? INITIAL_LIST_CAPACITY : list.ords.length;
1842
+ while (capacity < needed) capacity *= 2;
1843
+ const ords = new Int32Array(capacity);
1844
+ ords.set(list.ords);
1845
+ list.ords = ords;
1846
+ const distances = new Float64Array(capacity);
1847
+ distances.set(list.distances);
1848
+ list.distances = distances;
1849
+ }
1850
+ function appendToList(list, ord, distance) {
1851
+ ensureListCapacity(list, list.size + 1);
1852
+ list.ords[list.size] = ord;
1853
+ list.distances[list.size] = distance;
1854
+ list.size += 1;
1855
+ }
1856
+ function copyList(source, target) {
1857
+ ensureListCapacity(target, source.size);
1858
+ target.ords.set(source.ords.subarray(0, source.size));
1859
+ target.distances.set(source.distances.subarray(0, source.size));
1860
+ target.size = source.size;
1861
+ }
1862
+ function sortListByDistance(list) {
1863
+ const { ords, distances } = list;
1864
+ for (let i = 1; i < list.size; i++) {
1865
+ const ord = ords[i];
1866
+ const distance = distances[i];
1867
+ let j = i - 1;
1868
+ while (j >= 0 && distances[j] > distance) {
1869
+ distances[j + 1] = distances[j];
1870
+ ords[j + 1] = ords[j];
1871
+ j -= 1;
1872
+ }
1873
+ distances[j + 1] = distance;
1874
+ ords[j + 1] = ord;
1875
+ }
1876
+ }
1877
+ function siftUpGreatestFirst(ords, distances, start, ord, distance) {
1878
+ let index = start;
1879
+ while (index > 0) {
1880
+ const parent = index - 1 >> 1;
1881
+ if (!(distance > distances[parent])) break;
1882
+ ords[index] = ords[parent];
1883
+ distances[index] = distances[parent];
1884
+ index = parent;
1885
+ }
1886
+ ords[index] = ord;
1887
+ distances[index] = distance;
1888
+ }
1889
+ function siftUpLeastFirst(ords, distances, start, ord, distance) {
1890
+ let index = start;
1891
+ while (index > 0) {
1892
+ const parent = index - 1 >> 1;
1893
+ if (!(distance < distances[parent])) break;
1894
+ ords[index] = ords[parent];
1895
+ distances[index] = distances[parent];
1896
+ index = parent;
1897
+ }
1898
+ ords[index] = ord;
1899
+ distances[index] = distance;
1900
+ }
1901
+ function siftDownGreatestFirst(ords, distances, size, ord, distance) {
1902
+ let index = 0;
1903
+ for (; ; ) {
1904
+ const left = 2 * index + 1;
1905
+ const right = left + 1;
1906
+ let first = index;
1907
+ let firstDistance = distance;
1908
+ if (left < size && distances[left] > firstDistance) {
1909
+ first = left;
1910
+ firstDistance = distances[left];
1911
+ }
1912
+ if (right < size && distances[right] > firstDistance) first = right;
1913
+ if (first === index) break;
1914
+ ords[index] = ords[first];
1915
+ distances[index] = distances[first];
1916
+ index = first;
1917
+ }
1918
+ ords[index] = ord;
1919
+ distances[index] = distance;
1920
+ }
1921
+ function siftDownLeastFirst(ords, distances, size, ord, distance) {
1922
+ let index = 0;
1923
+ for (; ; ) {
1924
+ const left = 2 * index + 1;
1925
+ const right = left + 1;
1926
+ let first = index;
1927
+ let firstDistance = distance;
1928
+ if (left < size && distances[left] < firstDistance) {
1929
+ first = left;
1930
+ firstDistance = distances[left];
1931
+ }
1932
+ if (right < size && distances[right] < firstDistance) first = right;
1933
+ if (first === index) break;
1934
+ ords[index] = ords[first];
1935
+ distances[index] = distances[first];
1936
+ index = first;
1937
+ }
1938
+ ords[index] = ord;
1939
+ distances[index] = distance;
1940
+ }
1941
+ function pushHeap(heap, ord, distance) {
1942
+ ensureListCapacity(heap, heap.size + 1);
1943
+ const start = heap.size;
1944
+ heap.size += 1;
1945
+ if (heap.greatestFirst) siftUpGreatestFirst(heap.ords, heap.distances, start, ord, distance);
1946
+ else siftUpLeastFirst(heap.ords, heap.distances, start, ord, distance);
1947
+ }
1948
+ function popHeap(heap) {
1949
+ if (heap.size === 0) return false;
1950
+ heap.topOrd = heap.ords[0];
1951
+ heap.topDistance = heap.distances[0];
1952
+ heap.size -= 1;
1953
+ if (heap.size > 0) {
1954
+ const size = heap.size;
1955
+ const ord = heap.ords[size];
1956
+ const distance = heap.distances[size];
1957
+ if (heap.greatestFirst) siftDownGreatestFirst(heap.ords, heap.distances, size, ord, distance);
1958
+ else siftDownLeastFirst(heap.ords, heap.distances, size, ord, distance);
1959
+ }
1960
+ return true;
1961
+ }
1962
+ function resetHeap(heap) {
1963
+ heap.size = 0;
1964
+ }
1965
+ function drainHeapNearestFirst(heap, list) {
1966
+ const count = heap.size;
1967
+ ensureListCapacity(list, count);
1968
+ list.size = count;
1969
+ for (let i = count - 1; i >= 0; i--) {
1970
+ if (!popHeap(heap)) break;
1971
+ list.ords[i] = heap.topOrd;
1972
+ list.distances[i] = heap.topDistance;
1973
+ }
1974
+ }
1975
+ function setSingleEntryPoint(workspace, ord) {
1976
+ workspace.entryPoints[0] = ord;
1977
+ workspace.entryPointCount = 1;
1978
+ }
1979
+ function setEntryPointsFromList(workspace, list) {
1980
+ if (list.size > workspace.entryPoints.length) {
1981
+ let capacity = workspace.entryPoints.length;
1982
+ while (capacity < list.size) capacity *= 2;
1983
+ workspace.entryPoints = new Int32Array(capacity);
1984
+ }
1985
+ workspace.entryPoints.set(list.ords.subarray(0, list.size));
1986
+ workspace.entryPointCount = list.size;
1987
+ }
1988
+
1989
+ // src/vector/hnsw/last-links.ts
1990
+ function isLive(state, ord) {
1991
+ return nodeExists(state, ord) && !isTombstoned(state, ord);
1992
+ }
1993
+ function noteNodeLeftUnlinked(state) {
1994
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 1);
1995
+ }
1996
+ function listHolds(list, ord) {
1997
+ for (let i = 0; i < list.size; i++) if (list.ords[i] === ord) return true;
1998
+ return false;
1999
+ }
2000
+ function collectDropped(sorted, kept, dropped) {
2001
+ dropped.size = 0;
2002
+ let next = 0;
2003
+ for (let i = 0; i < sorted.size; i++) {
2004
+ const ord = sorted.ords[i];
2005
+ if (next < kept.size && kept.ords[next] === ord) {
2006
+ next += 1;
2007
+ continue;
2008
+ }
2009
+ if (listHolds(kept, ord)) continue;
2010
+ appendToList(dropped, ord, sorted.distances[i]);
2011
+ }
2012
+ }
2013
+ function replaceFarthestShared(state, kept, held, ord, distance) {
2014
+ const tried = /* @__PURE__ */ new Set();
2015
+ for (; ; ) {
2016
+ let farthest = -1;
2017
+ for (let k = 0; k < kept.size; k++) {
2018
+ if (tried.has(k) || incomingCount(state.adjacency, kept.ords[k]) < 2) continue;
2019
+ if (farthest === -1 || kept.distances[k] > kept.distances[farthest]) farthest = k;
2020
+ }
2021
+ if (farthest === -1) return false;
2022
+ tried.add(farthest);
2023
+ const replaced = kept.ords[farthest];
2024
+ if (held(replaced) && !releaseUnlessLast(state.adjacency, replaced)) continue;
2025
+ kept.ords[farthest] = ord;
2026
+ kept.distances[farthest] = distance;
2027
+ return true;
2028
+ }
2029
+ }
2030
+ function writeKeepingLastLinks(state, ord, sorted, kept, held) {
2031
+ const adjacency = state.adjacency;
2032
+ const limit = state.Mmax0;
2033
+ const dropped = state.workspace.droppedLinks;
2034
+ collectDropped(sorted, kept, dropped);
2035
+ ensureListCapacity(kept, limit);
2036
+ for (let i = 0; i < dropped.size; i++) {
2037
+ const candidate = dropped.ords[i];
2038
+ if (!held(candidate)) continue;
2039
+ if (!isLive(state, candidate)) {
2040
+ noteUnlinked(adjacency, candidate);
2041
+ continue;
2042
+ }
2043
+ if (releaseUnlessLast(adjacency, candidate)) continue;
2044
+ if (kept.size < limit) {
2045
+ appendToList(kept, candidate, dropped.distances[i]);
2046
+ continue;
2047
+ }
2048
+ if (replaceFarthestShared(state, kept, held, candidate, dropped.distances[i])) continue;
2049
+ noteUnlinked(adjacency, candidate);
2050
+ noteNodeLeftUnlinked(state);
2051
+ }
2052
+ for (let k = 0; k < kept.size; k++) if (!held(kept.ords[k])) noteLinked(adjacency, kept.ords[k]);
2053
+ writeNeighbors(adjacency, ord, 0, kept.ords, kept.size);
2054
+ }
2055
+
2056
+ // src/vector/hnsw/nearest-links.ts
2057
+ var NOT_YET_FOUND = -2;
2058
+ function nearer(a, b) {
2059
+ return nearerPair(a.distance, a.ord, b.distance, b.ord);
2060
+ }
2061
+ function nearerPair(aDistance, aOrd, bDistance, bOrd) {
2062
+ return aDistance < bDistance || aDistance === bDistance && aOrd < bOrd;
2063
+ }
2064
+ function listOf(state, ord) {
2065
+ const adjacency = state.adjacency;
2066
+ const base = layerBase(adjacency, ord, 0);
2067
+ if (base === ABSENT) return null;
2068
+ return adjacency.level0.subarray(base + 1, base + 1 + adjacency.level0[base]);
2069
+ }
2070
+ function nearestInList(pass, ord) {
2071
+ const known = pass.nearest[ord];
2072
+ if (known !== NOT_YET_FOUND) return known;
2073
+ let nearest = -1;
2074
+ let nearestDistance = Number.POSITIVE_INFINITY;
2075
+ const level0 = pass.state.adjacency.level0;
2076
+ const base = layerBase(pass.state.adjacency, ord, 0);
2077
+ const end = base === ABSENT ? base : base + 1 + level0[base];
2078
+ for (let at = base + 1; base !== ABSENT && at < end; at++) {
2079
+ const neighbour = level0[at];
2080
+ if (neighbour === ord || !isLive(pass.state, neighbour)) continue;
2081
+ const distance = pass.distance(ord, neighbour);
2082
+ if (nearest === -1 || nearerPair(distance, neighbour, nearestDistance, nearest)) {
2083
+ nearest = neighbour;
2084
+ nearestDistance = distance;
2085
+ }
2086
+ }
2087
+ pass.nearest[ord] = nearest;
2088
+ return nearest;
2089
+ }
2090
+ function fallbackHostsFor(pass, ord, nearest) {
2091
+ const around = [];
2092
+ for (const neighbour of listOf(pass.state, nearest) ?? []) {
2093
+ if (neighbour === ord || !isLive(pass.state, neighbour)) continue;
2094
+ around.push({ ord: neighbour, distance: pass.distance(ord, neighbour) });
2095
+ }
2096
+ around.sort((a, b) => nearer(a, b) ? -1 : nearer(b, a) ? 1 : 0);
2097
+ return around.slice(0, NEAREST_LINK_FALLBACK_HOSTS).map((scored) => scored.ord);
2098
+ }
2099
+ function replaceableSlot(pass, host, list) {
2100
+ let farthest = -1;
2101
+ let farthestOrd = -1;
2102
+ let farthestDistance = Number.NEGATIVE_INFINITY;
2103
+ for (let i = 0; i < list.length; i++) {
2104
+ const neighbour = list[i];
2105
+ if (incomingCount(pass.state.adjacency, neighbour) < 2) continue;
2106
+ if (isLive(pass.state, neighbour) && nearestInList(pass, neighbour) === host) continue;
2107
+ const distance = pass.distance(host, neighbour);
2108
+ if (farthest === -1 || nearerPair(farthestDistance, farthestOrd, distance, neighbour)) {
2109
+ farthest = i;
2110
+ farthestOrd = neighbour;
2111
+ farthestDistance = distance;
2112
+ }
2113
+ }
2114
+ return farthest;
2115
+ }
2116
+ function linkFrom(pass, host, ord) {
2117
+ const adjacency = pass.state.adjacency;
2118
+ const base = layerBase(adjacency, host, 0);
2119
+ if (base === ABSENT) return false;
2120
+ const list = adjacency.level0.subarray(base + 1, base + 1 + adjacency.level0[base]);
2121
+ if (list.includes(ord)) return true;
2122
+ if (list.length < pass.state.Mmax0) {
2123
+ adjacency.level0[base + list.length + 1] = ord;
2124
+ adjacency.level0[base] = list.length + 1;
2125
+ noteLinked(adjacency, ord);
2126
+ pass.nearest[host] = NOT_YET_FOUND;
2127
+ return true;
2128
+ }
2129
+ const slot = replaceableSlot(pass, host, list);
2130
+ if (slot === -1) return false;
2131
+ const replaced = list[slot];
2132
+ list[slot] = ord;
2133
+ noteUnlinked(adjacency, replaced);
2134
+ noteLinked(adjacency, ord);
2135
+ pass.nearest[host] = NOT_YET_FOUND;
2136
+ return true;
2137
+ }
2138
+ function linkFromNearest(state) {
2139
+ drain(linkFromNearestSteps(state, newPace()));
2140
+ }
2141
+ function* linkFromNearestSteps(state, pace) {
2142
+ const adjacency = state.adjacency;
2143
+ const slots = adjacencySlots(adjacency);
2144
+ if (slots > 0) reachNode(adjacency, slots - 1);
2145
+ const pass = {
2146
+ state,
2147
+ distance: nodeDistanceFunction(state, state.buildMetric),
2148
+ nearest: new Int32Array(slots).fill(NOT_YET_FOUND)
2149
+ };
2150
+ for (let ord = 0; ord < slots; ord++) {
2151
+ if (stepTaken(pace)) yield;
2152
+ if (Atomics.load(adjacency.nearestLinkDue, ord) !== 1) continue;
2153
+ Atomics.store(adjacency.nearestLinkDue, ord, 0);
2154
+ if (!isLive(state, ord)) continue;
2155
+ const nearest = nearestInList(pass, ord);
2156
+ if (nearest === -1 || linkFrom(pass, nearest, ord)) continue;
2157
+ for (const host of fallbackHostsFor(pass, ord, nearest)) if (linkFrom(pass, host, ord)) break;
2158
+ }
2159
+ }
2160
+
2161
+ // src/vector/hnsw/connect-unreached.ts
2162
+ var NOT_REACHED_THROUGH_A_NODE = -1;
2163
+ var NO_NEARBY_NODES2 = new Int32Array(0);
2164
+ function reach(walk, node, through) {
2165
+ walk.reached[node] = 1;
2166
+ walk.firstReachedThrough[node] = through;
2167
+ walk.queue[walk.tail++] = node;
2168
+ }
2169
+ function* walkFrom(state, walk, start, through, pace) {
2170
+ const adjacency = state.adjacency;
2171
+ if (walk.reached[start] === 1) return;
2172
+ reach(walk, start, through);
2173
+ while (walk.head < walk.tail) {
2174
+ if (stepTaken(pace)) yield;
2175
+ const ord = walk.queue[walk.head++];
2176
+ const base = layerBase(adjacency, ord, 0);
2177
+ if (base === ABSENT) continue;
2178
+ const count = adjacency.level0[base];
2179
+ for (let i = 1; i <= count; i++) {
2180
+ const next = adjacency.level0[base + i];
2181
+ if (next < 0 || next >= walk.reached.length || walk.reached[next] === 1 || !nodeExists(state, next)) continue;
2182
+ reach(walk, next, ord);
2183
+ }
2184
+ }
2185
+ }
2186
+ function hasRoom(state, _walk, host) {
2187
+ const base = layerBase(state.adjacency, host, 0);
2188
+ return base !== ABSENT && state.adjacency.level0[base] < state.Mmax0;
2189
+ }
2190
+ function reachedThroughAnotherNode(walk, host, neighbour) {
2191
+ if (neighbour < 0 || neighbour >= walk.reached.length || walk.reached[neighbour] !== 1) return false;
2192
+ const through = walk.firstReachedThrough[neighbour];
2193
+ return through !== NOT_REACHED_THROUGH_A_NODE && through !== host;
2194
+ }
2195
+ function holdsLinkOutsideTheWalk(state, walk, host) {
2196
+ const adjacency = state.adjacency;
2197
+ const base = layerBase(adjacency, host, 0);
2198
+ if (base === ABSENT) return false;
2199
+ const count = adjacency.level0[base];
2200
+ for (let i = 1; i <= count; i++) if (reachedThroughAnotherNode(walk, host, adjacency.level0[base + i])) return true;
2201
+ return false;
2202
+ }
2203
+ function listOf2(state, ord) {
2204
+ const adjacency = state.adjacency;
2205
+ const base = layerBase(adjacency, ord, 0);
2206
+ if (base === ABSENT) return new Int32Array(0);
2207
+ return adjacency.level0.slice(base + 1, base + 1 + adjacency.level0[base]);
2208
+ }
2209
+ function* nearestHost(state, walk, orphan, nearby, test, pace) {
2210
+ const distance = nodeDistanceFunction(state, state.buildMetric);
2211
+ let best = -1;
2212
+ let bestDistance = Number.POSITIVE_INFINITY;
2213
+ const consider = (host) => {
2214
+ if (host === orphan || walk.reached[host] !== 1 || !isLive(state, host) || !test(state, walk, host)) return;
2215
+ const away = distance(orphan, host);
2216
+ if (best === -1 || away < bestDistance) {
2217
+ best = host;
2218
+ bestDistance = away;
2219
+ }
2220
+ };
2221
+ for (const host of nearby.list) {
2222
+ if (stepTaken(pace)) yield;
2223
+ consider(host);
2224
+ }
2225
+ for (const host of listOf2(state, orphan))
2226
+ if (host >= 0 && host < walk.reached.length && nearby.holds[host] !== 1) consider(host);
2227
+ if (best !== -1) return best;
2228
+ for (let host = 0; host < walk.reached.length; host++) {
2229
+ if (stepTaken(pace)) yield;
2230
+ consider(host);
2231
+ }
2232
+ return best;
2233
+ }
2234
+ function takeOutFarthestLinkOutsideTheWalk(state, walk, host) {
2235
+ const adjacency = state.adjacency;
2236
+ const base = layerBase(adjacency, host, 0);
2237
+ if (base === ABSENT) return;
2238
+ const distance = nodeDistanceFunction(state, state.buildMetric);
2239
+ const count = adjacency.level0[base];
2240
+ let farthest = -1;
2241
+ let farthestDistance = Number.NEGATIVE_INFINITY;
2242
+ for (let i = 1; i <= count; i++) {
2243
+ const neighbour = adjacency.level0[base + i];
2244
+ if (!reachedThroughAnotherNode(walk, host, neighbour)) continue;
2245
+ const away = distance(host, neighbour);
2246
+ if (farthest === -1 || away > farthestDistance) {
2247
+ farthest = i;
2248
+ farthestDistance = away;
2249
+ }
2250
+ }
2251
+ if (farthest === -1) return;
2252
+ const taken = adjacency.level0[base + farthest];
2253
+ for (let i = farthest; i < count; i++) adjacency.level0[base + i] = adjacency.level0[base + i + 1];
2254
+ adjacency.level0[base] = count - 1;
2255
+ noteUnlinked(adjacency, taken);
2256
+ }
2257
+ function appendTo(state, host, orphan) {
2258
+ const adjacency = state.adjacency;
2259
+ const base = layerBase(adjacency, host, 0);
2260
+ if (base === ABSENT) return;
2261
+ const count = adjacency.level0[base];
2262
+ adjacency.level0[base + count + 1] = orphan;
2263
+ adjacency.level0[base] = count + 1;
2264
+ noteLinked(adjacency, orphan);
2265
+ }
2266
+ function* connectUnreachedSteps(state, nearby, pace) {
2267
+ const slots = adjacencySlots(state.adjacency);
2268
+ const entry = entryPointOf(state);
2269
+ if (entry < 0 || entry >= slots || !nodeExists(state, entry)) return;
2270
+ const walk = {
2271
+ reached: new Uint8Array(slots),
2272
+ firstReachedThrough: new Int32Array(slots),
2273
+ queue: new Int32Array(slots),
2274
+ head: 0,
2275
+ tail: 0
2276
+ };
2277
+ yield* walkFrom(state, walk, entry, NOT_REACHED_THROUGH_A_NODE, pace);
2278
+ const held = { list: [], holds: new Uint8Array(slots) };
2279
+ for (let i = 0; i < nearby.length; i++) {
2280
+ const ord = nearby[i];
2281
+ if (ord < 0 || ord >= slots || held.holds[ord] === 1) continue;
2282
+ held.holds[ord] = 1;
2283
+ held.list.push(ord);
2284
+ }
2285
+ for (let orphan = 0; orphan < slots; orphan++) {
2286
+ if (stepTaken(pace)) yield;
2287
+ if (walk.reached[orphan] === 1 || !isLive(state, orphan)) continue;
2288
+ let host = yield* nearestHost(state, walk, orphan, held, hasRoom, pace);
2289
+ if (host === -1) {
2290
+ host = yield* nearestHost(state, walk, orphan, held, holdsLinkOutsideTheWalk, pace);
2291
+ if (host === -1) continue;
2292
+ takeOutFarthestLinkOutsideTheWalk(state, walk, host);
2293
+ }
2294
+ appendTo(state, host, orphan);
2295
+ yield* walkFrom(state, walk, orphan, host, pace);
2296
+ }
2297
+ }
2298
+ function connectUnreached(state, nearby) {
2299
+ drain(connectUnreachedSteps(state, nearby, newPace()));
2300
+ }
2301
+ function* connectInJsSteps(state) {
2302
+ const pace = newPace();
2303
+ yield* linkFromNearestSteps(state, pace);
2304
+ yield* connectUnreachedSteps(state, NO_NEARBY_NODES2, pace);
2305
+ }
2306
+ function holdsUnlinkedNode(state) {
2307
+ const slots = adjacencySlots(state.adjacency);
2308
+ const entry = entryPointOf(state);
2309
+ const incoming = state.adjacency.incoming;
2310
+ for (let ord = 0; ord < slots; ord++) {
2311
+ if (incoming[ord] === 0 && ord !== entry && isLive(state, ord)) return true;
2312
+ }
2313
+ return false;
2314
+ }
2315
+ function connectThroughEitherCore(state, nearby) {
2316
+ const field = nativeFieldFor(state);
2317
+ if (field !== null && nativeConnect(state, field, state.buildMetric, nearby)) return;
2318
+ linkFromNearest(state);
2319
+ connectUnreached(state, nearby);
2320
+ }
2321
+ function connectAfterRemoval(state, nearby) {
2322
+ connectThroughEitherCore(state, nearby);
2323
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
2324
+ }
2325
+ function connectAfterBuild(state) {
2326
+ connectThroughEitherCore(state, new Int32Array(0));
2327
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
2328
+ }
2329
+ function connectAfterPlacement(state, holdsGraphLock) {
2330
+ if (Atomics.load(state.header, GRAPH_UNLINKED_NODES) === 0) return;
2331
+ if (!holdsGraphLock) lockGraphExclusive(state.locks);
2332
+ try {
2333
+ if (Atomics.load(state.header, GRAPH_UNLINKED_NODES) === 0) return;
2334
+ connectThroughEitherCore(state, new Int32Array(0));
2335
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
2336
+ } finally {
2337
+ if (!holdsGraphLock) unlockGraphExclusive(state.locks);
2338
+ }
2339
+ }
2340
+ function baseNeighboursOfRemoved(state, removed) {
2341
+ const leaving = new Set(removed);
2342
+ const nearby = [];
2343
+ for (const ord of removed) {
2344
+ for (const neighbour of listOf2(state, ord)) if (!leaving.has(neighbour)) nearby.push(neighbour);
2345
+ }
2346
+ return Int32Array.from(nearby);
2347
+ }
2348
+ function leaveIncomingUncounted(state) {
2349
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
2350
+ Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 1);
2351
+ }
2352
+ function countIncomingBeforeWriting(state, holdsGraphLock, connectsAfterwards = false) {
2353
+ if (Atomics.load(state.header, GRAPH_INCOMING_UNCOUNTED) === 0) return;
2354
+ if (!holdsGraphLock) lockGraphExclusive(state.locks);
2355
+ try {
2356
+ if (Atomics.load(state.header, GRAPH_INCOMING_UNCOUNTED) === 0) return;
2357
+ const adjacency = state.adjacency;
2358
+ const slots = adjacencySlots(adjacency);
2359
+ if (slots > 0) reachNode(adjacency, slots - 1);
2360
+ adjacency.incoming.fill(0);
2361
+ countEveryLink(adjacency, slots);
2362
+ const unlinked = holdsUnlinkedNode(state);
2363
+ if (unlinked && !connectsAfterwards) connectThroughEitherCore(state, new Int32Array(0));
2364
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, unlinked && connectsAfterwards ? 1 : 0);
2365
+ Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 0);
2366
+ } finally {
2367
+ if (!holdsGraphLock) unlockGraphExclusive(state.locks);
2368
+ }
2369
+ }
2370
+
2371
+ // src/vector/hnsw/connect-in-turns.ts
2372
+ function connectHoldingTheGraph(state) {
2373
+ lockGraphExclusive(state.locks);
2374
+ try {
2375
+ connectAfterBuild(state);
2376
+ } finally {
2377
+ unlockGraphExclusive(state.locks);
2378
+ }
2379
+ }
2380
+ function sliceFinished(state, steps) {
2381
+ lockGraphExclusive(state.locks);
2382
+ try {
2383
+ for (; ; ) {
2384
+ if (steps.next().done === true) {
2385
+ Atomics.store(state.header, GRAPH_UNLINKED_NODES, 0);
2386
+ return true;
2387
+ }
2388
+ if (backgroundTurnIsSpent()) return false;
2389
+ }
2390
+ } finally {
2391
+ unlockGraphExclusive(state.locks);
2392
+ }
2393
+ }
2394
+ async function connectedWithoutLinkWrites(state) {
2395
+ const writesBefore = state.linkWrites;
2396
+ const steps = connectInJsSteps(state);
2397
+ while (!sliceFinished(state, steps)) {
2398
+ await yieldBackgroundTurn();
2399
+ if (state.linkWrites !== writesBefore) return false;
2400
+ }
2401
+ return state.linkWrites === writesBefore;
2402
+ }
2403
+ async function connectUntilWritesDrain(state) {
2404
+ let connected = false;
2405
+ while (!connected) connected = await connectedWithoutLinkWrites(state);
2406
+ }
2407
+ async function connectInSlices(state) {
2408
+ for (let restarts = 0; restarts < CONNECT_RESTARTS_BEFORE_ONE_PASS; restarts++) {
2409
+ if (await connectedWithoutLinkWrites(state)) return;
2410
+ }
2411
+ if (turnsAreBudgeted) await holdWrites(() => connectUntilWritesDrain(state));
2412
+ else connectHoldingTheGraph(state);
2413
+ }
2414
+ async function connectThroughTheCoreThread(state, first) {
2415
+ const startedAt = performance.now();
2416
+ let field = first;
2417
+ while (field !== null) {
2418
+ const outcome = await nativeConnectOffThread(state, field, state.buildMetric);
2419
+ if (outcome === "connected") return true;
2420
+ if (outcome === "failed" || performance.now() - startedAt >= GRAPH_LOCK_WAIT_DEADLINE_MS) return false;
2421
+ await yieldBackgroundTurn();
2422
+ field = nativeFieldFor(state);
2423
+ }
2424
+ return false;
2425
+ }
2426
+ function connectOffThread(state) {
2427
+ const field = nativeFieldFor(state);
2428
+ if (field === null || !nativeConnectsOffThread(field)) return null;
2429
+ return connectThroughTheCoreThread(state, field);
2430
+ }
2431
+ function connectOnThisThread(state, inSlices) {
2432
+ if (inSlices || nativeFieldFor(state) === null) return connectInSlices(state);
2433
+ connectHoldingTheGraph(state);
2434
+ return Promise.resolve();
2435
+ }
2436
+
2437
+ // src/vector/ordinal-filter.ts
2438
+ function createOrdinalFilter(slots) {
2439
+ return { bits: new Uint8Array(slots + 7 >>> 3), count: 0 };
2440
+ }
2441
+ function addToOrdinalFilter(filter, ordinal) {
2442
+ const index = ordinal >>> 3;
2443
+ if (index >= filter.bits.length) return;
2444
+ const bit = 1 << (ordinal & 7);
2445
+ if ((filter.bits[index] & bit) !== 0) return;
2446
+ filter.bits[index] |= bit;
2447
+ filter.count += 1;
2448
+ }
2449
+ function ordinalFilterHas(filter, ordinal) {
2450
+ const index = ordinal >>> 3;
2451
+ if (index >= filter.bits.length) return false;
2452
+ return (filter.bits[index] & 1 << (ordinal & 7)) !== 0;
2453
+ }
2454
+ function removeFromOrdinalFilter(filter, ordinal) {
2455
+ const index = ordinal >>> 3;
2456
+ if (index >= filter.bits.length) return;
2457
+ const bit = 1 << (ordinal & 7);
2458
+ if ((filter.bits[index] & bit) === 0) return;
2459
+ filter.bits[index] &= ~bit;
2460
+ filter.count -= 1;
2461
+ }
2462
+ function* ordinalFilterValues(filter) {
2463
+ const bits = filter.bits;
2464
+ for (let index = 0; index < bits.length; index++) {
2465
+ const byte = bits[index];
2466
+ if (byte === 0) continue;
2467
+ for (let offset = 0; offset < 8; offset++) {
2468
+ if ((byte & 1 << offset) !== 0) yield index << 3 | offset;
2469
+ }
2470
+ }
2471
+ }
2472
+
2473
+ // src/vector/hnsw/graph-ops.ts
2474
+ function readNeighborsLocked(state, ord, layer) {
2475
+ for (; ; ) {
2476
+ const version = beginNodeRead(state.locks, ord);
2477
+ const count = readNeighbors(state.adjacency, ord, layer, state.neighborScratch);
2478
+ if (nodeReadHeld(state.locks, ord, version)) return count;
2479
+ }
2480
+ }
2481
+ function searchLayer(state, qVec, qMag, ef, layer, metric, excludeTombstones, distFn, results, accepted, clock) {
2482
+ const getDistance = distFn ?? ((ord) => queryDistanceByOrd(state, qVec, qMag, ord, metric));
2483
+ const workspace = state.workspace;
2484
+ const frontier = workspace.frontier;
2485
+ const found = workspace.found;
2486
+ const scratch = state.neighborScratch;
2487
+ const stamp = nextVisitStamp(state);
2488
+ resetHeap(frontier);
2489
+ resetHeap(found);
2490
+ let furthestDist = Number.POSITIVE_INFINITY;
2491
+ for (let i = 0; i < workspace.entryPointCount; i++) {
2492
+ const epOrd = workspace.entryPoints[i];
2493
+ ensureVisited(state, epOrd + 1);
2494
+ if (state.visited[epOrd] === stamp) continue;
2495
+ state.visited[epOrd] = stamp;
2496
+ if (!nodeExists(state, epOrd)) continue;
2497
+ const dist = getDistance(epOrd);
2498
+ if (dist === Number.POSITIVE_INFINITY) continue;
2499
+ pushHeap(frontier, epOrd, dist);
2500
+ if (excludeTombstones && isTombstoned(state, epOrd)) continue;
2501
+ if (accepted && !ordinalFilterHas(accepted, epOrd)) continue;
2502
+ pushHeap(found, epOrd, dist);
2503
+ if (found.size > ef) {
2504
+ popHeap(found);
2505
+ }
2506
+ }
2507
+ if (found.size > 0) furthestDist = found.distances[0];
2508
+ while (popHeap(frontier)) {
2509
+ if (frontier.topDistance > furthestDist && found.size >= ef) break;
2510
+ const count = readNeighborsLocked(state, frontier.topOrd, layer);
2511
+ if (clock !== void 0) clock.tick(count * state.dimension);
2512
+ for (let i = 0; i < count; i++) {
2513
+ const neighborOrd = scratch[i];
2514
+ ensureVisited(state, neighborOrd + 1);
2515
+ if (state.visited[neighborOrd] === stamp) continue;
2516
+ state.visited[neighborOrd] = stamp;
2517
+ if (!nodeExists(state, neighborOrd)) continue;
2518
+ const dist = getDistance(neighborOrd);
2519
+ if (dist === Number.POSITIVE_INFINITY) continue;
2520
+ if (dist < furthestDist || found.size < ef) {
2521
+ pushHeap(frontier, neighborOrd, dist);
2522
+ if (excludeTombstones && isTombstoned(state, neighborOrd)) continue;
2523
+ if (accepted && !ordinalFilterHas(accepted, neighborOrd)) continue;
2524
+ pushHeap(found, neighborOrd, dist);
2525
+ if (found.size > ef) {
2526
+ popHeap(found);
2527
+ }
2528
+ furthestDist = found.distances[0];
2529
+ }
2530
+ }
2531
+ }
2532
+ drainHeapNearestFirst(found, results);
2533
+ }
2534
+ function selectNeighborsHeuristic(state, candidates, maxConnections, distance, selected) {
2535
+ const working = state.workspace.working;
2536
+ copyList(candidates, working);
2537
+ sortListByDistance(working);
2538
+ ensureListCapacity(selected, Math.min(maxConnections, working.size));
2539
+ selected.size = 0;
2540
+ for (let i = 0; i < working.size; i++) {
2541
+ if (selected.size >= maxConnections) break;
2542
+ const candidateOrd = working.ords[i];
2543
+ const candidateDistance = working.distances[i];
2544
+ let accepted = true;
2545
+ for (let s = 0; s < selected.size; s++) {
2546
+ const distBetween = distance(candidateOrd, selected.ords[s]);
2547
+ if (candidateDistance >= distBetween) {
2548
+ accepted = false;
2549
+ break;
2550
+ }
2551
+ }
2552
+ if (!accepted) continue;
2553
+ selected.ords[selected.size] = candidateOrd;
2554
+ selected.distances[selected.size] = candidateDistance;
2555
+ selected.size += 1;
2556
+ }
2557
+ keepFarthestRejected(working, maxConnections, selected);
2558
+ }
2559
+ function holdsOrd(list, ord) {
2560
+ for (let i = 0; i < list.size; i++) if (list.ords[i] === ord) return true;
2561
+ return false;
2562
+ }
2563
+ function keepFarthestRejected(sorted, maxConnections, selected) {
2564
+ if (selected.size * 2 >= maxConnections) return;
2565
+ let kept = 0;
2566
+ for (let i = sorted.size - 1; i >= 0 && kept < FARTHEST_REJECTED_LINKS_KEPT; i--) {
2567
+ if (selected.size >= maxConnections) return;
2568
+ if (holdsOrd(selected, sorted.ords[i])) continue;
2569
+ selected.ords[selected.size] = sorted.ords[i];
2570
+ selected.distances[selected.size] = sorted.distances[i];
2571
+ selected.size += 1;
2572
+ kept += 1;
2573
+ }
2574
+ }
2575
+ function pruneConnections(state, ord, layer, distance) {
2576
+ const adjacency = state.adjacency;
2577
+ const base = layerBase(adjacency, ord, layer);
2578
+ if (base === -1) return;
2579
+ const mc = maxConns(state, layer);
2580
+ const neighbors = layerArray(adjacency, layer);
2581
+ const count = neighbors[base];
2582
+ if (count <= mc) return;
2583
+ const candidates = state.workspace.pruneCandidates;
2584
+ candidates.size = 0;
2585
+ ensureListCapacity(candidates, count);
2586
+ for (let i = 1; i <= count; i++) {
2587
+ const connOrd = neighbors[base + i];
2588
+ const dist = distance(ord, connOrd);
2589
+ if (dist === Number.POSITIVE_INFINITY) {
2590
+ if (layer === 0) noteUnlinked(adjacency, connOrd);
2591
+ continue;
2592
+ }
2593
+ candidates.ords[candidates.size] = connOrd;
2594
+ candidates.distances[candidates.size] = dist;
2595
+ candidates.size += 1;
2596
+ }
2597
+ const kept = state.workspace.pruneSelection;
2598
+ selectNeighborsHeuristic(state, candidates, mc, distance, kept);
2599
+ if (layer === 0) {
2600
+ writeKeepingLastLinks(state, ord, state.workspace.working, kept, everyCandidateWasHeld);
2601
+ return;
2602
+ }
2603
+ writeNeighbors(adjacency, ord, layer, kept.ords, kept.size);
2604
+ }
2605
+ function everyCandidateWasHeld() {
2606
+ return true;
2607
+ }
2608
+
2609
+ // src/vector/hnsw/mutation.ts
2610
+ function clearTombstone(state, ord) {
2611
+ if (!reachTombstone(state, ord) || Atomics.load(state.tombstones, ord) !== 1) return;
2612
+ Atomics.sub(state.header, GRAPH_TOMBSTONE_COUNT, 1);
2613
+ Atomics.store(state.tombstones, ord, 0);
2614
+ }
2615
+ function highestNode(state, includeTombstoned) {
2616
+ let bestOrd = -1;
2617
+ let bestLayer = -1;
2618
+ const slots = adjacencySlots(state.adjacency);
2619
+ for (let ord = 0; ord < slots; ord++) {
2620
+ const level2 = nodeMaxLayer(state, ord);
2621
+ if (level2 === -1) continue;
2622
+ if (!includeTombstoned && isTombstoned(state, ord)) continue;
2623
+ if (level2 > bestLayer) {
2624
+ bestLayer = level2;
2625
+ bestOrd = ord;
2626
+ }
2627
+ }
2628
+ return bestOrd;
2629
+ }
2630
+ function setEntryPoint(state, ord) {
2631
+ Atomics.store(state.header, GRAPH_ENTRY_POINT, ord);
2632
+ Atomics.store(state.header, GRAPH_TOP_LAYER, ord === -1 ? -1 : nodeMaxLayer(state, ord));
2633
+ }
2634
+ function claimEntryIfEmpty(state, ord, level2) {
2635
+ lockEntry(state.locks);
2636
+ try {
2637
+ if (Atomics.load(state.header, GRAPH_ENTRY_POINT) !== -1) return false;
2638
+ Atomics.store(state.header, GRAPH_ENTRY_POINT, ord);
2639
+ Atomics.store(state.header, GRAPH_TOP_LAYER, level2);
2640
+ return true;
2641
+ } finally {
2642
+ unlockEntry(state.locks);
2643
+ }
2644
+ }
2645
+ function raiseEntry(state, ord, level2) {
2646
+ lockEntry(state.locks);
2647
+ try {
2648
+ if (level2 <= Atomics.load(state.header, GRAPH_TOP_LAYER)) return;
2649
+ Atomics.store(state.header, GRAPH_ENTRY_POINT, ord);
2650
+ Atomics.store(state.header, GRAPH_TOP_LAYER, level2);
2651
+ } finally {
2652
+ unlockEntry(state.locks);
2653
+ }
2654
+ }
2655
+ function placementDistance(state, vector, fullPrecision) {
2656
+ const quantizer = state.quantizer;
2657
+ if (quantizer === void 0 || !buildsFromCodes(state)) return fullPrecision;
2658
+ if (state.store.handles.vectorFiles.length > 0) return quantizer.placementDistance(vector) ?? fullPrecision;
2659
+ const prepared = quantizer.prepareQuery(vector);
2660
+ return prepared === null ? fullPrecision : quantizer.preparedDistance(prepared);
2661
+ }
2662
+ function selectNeighborsPerLayer(state, ord, level2) {
2663
+ const metric = state.buildMetric;
2664
+ const workspace = state.workspace;
2665
+ const candidates = workspace.traversal;
2666
+ const entry = state.store.entryForOrdinal(ord);
2667
+ if (entry === void 0) return -1;
2668
+ const fullPrecision = state.store.ordinalDistance(ord, metric);
2669
+ const insertDistFn = placementDistance(state, entry.vector, fullPrecision);
2670
+ const topLayer = topLayerOf(state);
2671
+ setSingleEntryPoint(workspace, entryPointOf(state));
2672
+ for (let layer = topLayer; layer > level2; layer--) {
2673
+ searchLayer(state, entry.vector, entry.magnitude, 1, layer, metric, false, insertDistFn, candidates);
2674
+ if (candidates.size > 0) setSingleEntryPoint(workspace, candidates.ords[0]);
2675
+ }
2676
+ const linkTop = Math.min(level2, topLayer);
2677
+ const selections = linkSelectionsFor(workspace, linkTop + 1);
2678
+ const distance = nodeDistanceFunction(state, metric);
2679
+ for (let layer = linkTop; layer >= 0; layer--) {
2680
+ searchLayer(state, entry.vector, entry.magnitude, state.efCons, layer, metric, false, insertDistFn, candidates);
2681
+ selectNeighborsHeuristic(state, candidates, maxConns(state, layer), distance, selections[layer]);
2682
+ if (candidates.size > 0) setEntryPointsFromList(workspace, candidates);
2683
+ }
2684
+ return linkTop;
2685
+ }
2686
+ function writeOwnLists(state, ord, linkTop) {
2687
+ const selections = state.workspace.linkSelections;
2688
+ lockNodeWrite(state.locks, ord);
2689
+ try {
2690
+ for (let layer = linkTop; layer >= 0; layer--) {
2691
+ const selected = selections[layer];
2692
+ replaceNeighbors(state.adjacency, ord, layer, selected.ords, selected.size);
2693
+ }
2694
+ } finally {
2695
+ unlockNodeWrite(state.locks, ord);
2696
+ }
2697
+ }
2698
+ function linkNeighborsBack(state, ord, linkTop) {
2699
+ const distance = nodeDistanceFunction(state, state.buildMetric);
2700
+ const selections = state.workspace.linkSelections;
2701
+ for (let layer = linkTop; layer >= 0; layer--) {
2702
+ const selected = selections[layer];
2703
+ for (let i = 0; i < selected.size; i++) {
2704
+ const neighborOrd = selected.ords[i];
2705
+ if (layer > nodeMaxLayer(state, neighborOrd)) continue;
2706
+ lockNodeWrite(state.locks, neighborOrd);
2707
+ try {
2708
+ addNeighbor(state.adjacency, neighborOrd, layer, ord);
2709
+ pruneConnections(state, neighborOrd, layer, distance);
2710
+ } finally {
2711
+ unlockNodeWrite(state.locks, neighborOrd);
2712
+ }
2713
+ }
2714
+ }
2715
+ }
2716
+ function linkNode(state, ord, level2) {
2717
+ const topLayer = topLayerOf(state);
2718
+ const formerEntry = entryPointOf(state);
2719
+ const linkTop = selectNeighborsPerLayer(state, ord, level2);
2720
+ if (linkTop < 0) return;
2721
+ writeOwnLists(state, ord, linkTop);
2722
+ linkNeighborsBack(state, ord, linkTop);
2723
+ if (level2 <= topLayer) return;
2724
+ raiseEntry(state, ord, level2);
2725
+ const demoted = entryPointOf(state) !== formerEntry && isLive(state, formerEntry);
2726
+ if (demoted && incomingCount(state.adjacency, formerEntry) === 0) noteNodeLeftUnlinked(state);
2727
+ }
2728
+ function writeRecordBeforePlacement(state, ord) {
2729
+ const quantizer = state.quantizer;
2730
+ if (quantizer === void 0 || !buildsFromCodes(state)) return;
2731
+ quantizer.writeCodes(ord);
2732
+ }
2733
+ function placeThroughTheCore(state, ord, holdsGraphLock) {
2734
+ let field = nativeFieldFor(state);
2735
+ if (field === null) return false;
2736
+ const level2 = levelOfOrdinal(ord, state.mL);
2737
+ for (let request = 0; request < PLACEMENT_ROOM_REQUESTS && field !== null; request++) {
2738
+ ensureUpperRoom(state.adjacency, level2);
2739
+ const placement = nativePlace(state, field, state.buildMetric, ord, level2, holdsGraphLock);
2740
+ if (placement === "placed") return true;
2741
+ if (placement === "failed") return false;
2742
+ ensureCapacity(state, ord + 1);
2743
+ publishGraphCapacity(state.adjacency.handles);
2744
+ field = nativeFieldFor(state);
2745
+ }
2746
+ return false;
2747
+ }
2748
+ function insertNode(state, ord, holdsGraphLock = false, connectsAfterwards = false) {
2749
+ if (!state.store.holdsOrdinal(ord)) return false;
2750
+ ensureCapacity(state, ord + 1);
2751
+ if (nodeExists(state, ord)) return false;
2752
+ countIncomingBeforeWriting(state, holdsGraphLock, connectsAfterwards);
2753
+ writeRecordBeforePlacement(state, ord);
2754
+ state.linkWrites += 1;
2755
+ if (placeThroughTheCore(state, ord, holdsGraphLock)) {
2756
+ if (!connectsAfterwards) connectAfterPlacement(state, holdsGraphLock);
2757
+ return true;
2758
+ }
2759
+ if (nodeExists(state, ord)) return false;
2760
+ if (!holdsGraphLock) lockGraphShared(state.locks);
2761
+ try {
2762
+ clearTombstone(state, ord);
2763
+ const level2 = levelOfOrdinal(ord, state.mL);
2764
+ createNode(state.adjacency, ord, level2);
2765
+ Atomics.add(state.header, GRAPH_NODE_COUNT, 1);
2766
+ if (!claimEntryIfEmpty(state, ord, level2)) linkNode(state, ord, level2);
2767
+ } finally {
2768
+ if (!holdsGraphLock) unlockGraphShared(state.locks);
2769
+ }
2770
+ if (!connectsAfterwards) connectAfterPlacement(state, holdsGraphLock);
2771
+ return true;
2772
+ }
2773
+ function removeNodeEager(state, ord, excludeOrds) {
2774
+ const maxLayer = nodeMaxLayer(state, ord);
2775
+ if (maxLayer === -1) return;
2776
+ const distance = nodeDistanceFunction(state, state.buildMetric);
2777
+ for (let layer = 0; layer <= maxLayer; layer++) {
2778
+ const formerNeighbors = collectNeighbors(state.adjacency, ord, layer);
2779
+ for (const neighborOrd of formerNeighbors) {
2780
+ if (layer <= nodeMaxLayer(state, neighborOrd)) {
2781
+ removeNeighbor(state.adjacency, neighborOrd, layer, ord);
2782
+ }
2783
+ }
2784
+ for (const neighborOrd of formerNeighbors) {
2785
+ if (layer > nodeMaxLayer(state, neighborOrd)) continue;
2786
+ const mc = maxConns(state, layer);
2787
+ if (neighborCount(state.adjacency, neighborOrd, layer) >= mc) continue;
2788
+ const heldNow = new Set(collectNeighbors(state.adjacency, neighborOrd, layer));
2789
+ const candidateOrds = new Set(heldNow);
2790
+ for (const otherOrd of formerNeighbors) {
2791
+ if (otherOrd !== neighborOrd && otherOrd !== ord) {
2792
+ if (excludeOrds?.has(otherOrd)) continue;
2793
+ candidateOrds.add(otherOrd);
2794
+ }
2795
+ }
2796
+ const candidates = state.workspace.repairCandidates;
2797
+ candidates.size = 0;
2798
+ for (const candOrd of candidateOrds) {
2799
+ const dist = distance(neighborOrd, candOrd);
2800
+ if (dist === Number.POSITIVE_INFINITY) {
2801
+ if (layer === 0 && heldNow.has(candOrd)) noteUnlinked(state.adjacency, candOrd);
2802
+ continue;
2803
+ }
2804
+ appendToList(candidates, candOrd, dist);
2805
+ }
2806
+ const selected = state.workspace.repairSelection;
2807
+ selectNeighborsHeuristic(state, candidates, mc, distance, selected);
2808
+ if (layer === 0) {
2809
+ writeKeepingLastLinks(state, neighborOrd, state.workspace.working, selected, (held) => heldNow.has(held));
2810
+ } else {
2811
+ replaceNeighbors(state.adjacency, neighborOrd, layer, selected.ords, selected.size);
2812
+ }
2813
+ for (let i = 0; i < selected.size; i++) {
2814
+ const newConnOrd = selected.ords[i];
2815
+ if (layer <= nodeMaxLayer(state, newConnOrd)) {
2816
+ addNeighbor(state.adjacency, newConnOrd, layer, neighborOrd);
2817
+ pruneConnections(state, newConnOrd, layer, distance);
2818
+ }
2819
+ }
2820
+ }
2821
+ }
2822
+ const formerBaseNeighbors = collectNeighbors(state.adjacency, ord, 0);
2823
+ for (const neighborOrd of formerBaseNeighbors) noteUnlinked(state.adjacency, neighborOrd);
2824
+ deleteNode(state.adjacency, ord);
2825
+ Atomics.sub(state.header, GRAPH_NODE_COUNT, 1);
2826
+ clearTombstone(state, ord);
2827
+ if (entryPointOf(state) === ord) {
2828
+ if (Atomics.load(state.header, GRAPH_NODE_COUNT) === 0) {
2829
+ setEntryPoint(state, -1);
2830
+ return;
2831
+ }
2832
+ const live = highestNode(state, false);
2833
+ setEntryPoint(state, live === -1 ? highestNode(state, true) : live);
2834
+ }
2835
+ }
2836
+ function markTombstone(state, ord) {
2837
+ if (!nodeExists(state, ord) || !reachTombstone(state, ord)) return;
2838
+ const field = nativeFieldFor(state);
2839
+ if (field !== null && nativeRemove(state, field, ord)) {
2840
+ state.linkWrites += 1;
2841
+ return;
2842
+ }
2843
+ if (Atomics.compareExchange(state.tombstones, ord, 0, 1) === 0) {
2844
+ Atomics.add(state.header, GRAPH_TOMBSTONE_COUNT, 1);
2845
+ }
2846
+ }
2847
+ function compactTombstones(state) {
2848
+ if (Atomics.load(state.header, GRAPH_TOMBSTONE_COUNT) === 0) return;
2849
+ countIncomingBeforeWriting(state, true);
2850
+ const tombstonedOrds = [];
2851
+ const slots = adjacencySlots(state.adjacency);
2852
+ for (let ord = 0; ord < slots; ord++) {
2853
+ if (isTombstoned(state, ord) && nodeExists(state, ord)) {
2854
+ tombstonedOrds.push(ord);
2855
+ }
2856
+ }
2857
+ const nearby = baseNeighboursOfRemoved(state, tombstonedOrds);
2858
+ const field = nativeFieldFor(state);
2859
+ if (field === null || !nativeCompact(state, field, state.buildMetric)) {
2860
+ const excl = new Set(tombstonedOrds);
2861
+ for (const ord of tombstonedOrds) {
2862
+ removeNodeEager(state, ord, excl);
2863
+ }
2864
+ }
2865
+ connectAfterRemoval(state, nearby);
2866
+ }
2867
+ function resetGraph(state) {
2868
+ resetAdjacency(state.adjacency);
2869
+ state.tombstones = fixedView(state.adjacency.handles.tombstones, Uint8Array);
2870
+ state.tombstones.fill(0);
2871
+ Atomics.store(state.header, GRAPH_TOMBSTONE_COUNT, 0);
2872
+ Atomics.store(state.header, GRAPH_NODE_COUNT, 0);
2873
+ Atomics.store(state.header, GRAPH_ENTRY_POINT, -1);
2874
+ Atomics.store(state.header, GRAPH_TOP_LAYER, -1);
2875
+ Atomics.store(state.header, GRAPH_INCOMING_UNCOUNTED, 0);
2876
+ }
2877
+ function rebuild(state) {
2878
+ const nodeCount = Atomics.load(state.header, GRAPH_NODE_COUNT);
2879
+ if (Atomics.load(state.header, GRAPH_TOMBSTONE_COUNT) === 0 && nodeCount === 0) return;
2880
+ const liveOrds = [];
2881
+ const slots = adjacencySlots(state.adjacency);
2882
+ for (let ord = 0; ord < slots; ord++) {
2883
+ if (nodeExists(state, ord) && !isTombstoned(state, ord)) {
2884
+ liveOrds.push(ord);
2885
+ }
2886
+ }
2887
+ resetGraph(state);
2888
+ for (const ord of shuffleInPlace(liveOrds)) {
2889
+ if (!state.store.holdsOrdinal(ord)) continue;
2890
+ insertNode(state, ord, true);
2891
+ }
2892
+ connectAfterBuild(state);
2893
+ }
2894
+
2895
+ // src/vector/hnsw/numbered-graph.ts
2896
+ var NO_NUMBER = -1;
2897
+ var NO_ORDINAL = -1;
2898
+ var NO_LAYER = -1;
2899
+ var BYTES_PER_VALUE = 4;
2900
+ var LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
2901
+ function swapEveryValue(bytes) {
2902
+ for (let i = 0; i + 3 < bytes.length; i += BYTES_PER_VALUE) {
2903
+ const first = bytes[i];
2904
+ const second = bytes[i + 1];
2905
+ bytes[i] = bytes[i + 3];
2906
+ bytes[i + 1] = bytes[i + 2];
2907
+ bytes[i + 2] = second;
2908
+ bytes[i + 3] = first;
2909
+ }
2910
+ }
2911
+ function littleEndianBytesOf(values) {
2912
+ const bytes = new Uint8Array(values.buffer, values.byteOffset, values.byteLength);
2913
+ if (!LITTLE_ENDIAN) swapEveryValue(bytes);
2914
+ return bytes;
2915
+ }
2916
+ function valuesOf(bytes) {
2917
+ if (LITTLE_ENDIAN && bytes.byteOffset % BYTES_PER_VALUE === 0) {
2918
+ return new Uint32Array(bytes.buffer, bytes.byteOffset, Math.floor(bytes.byteLength / BYTES_PER_VALUE));
2919
+ }
2920
+ const copy = bytes.slice(0, bytes.byteLength - bytes.byteLength % BYTES_PER_VALUE);
2921
+ if (!LITTLE_ENDIAN) swapEveryValue(copy);
2922
+ return new Uint32Array(copy.buffer, 0, copy.byteLength / BYTES_PER_VALUE);
2923
+ }
2924
+ function savedTopLayer(state, ordinal) {
2925
+ if (ordinal === NO_ORDINAL) return NO_LAYER;
2926
+ return Math.min(nodeMaxLayer(state, ordinal), MAX_LAYER_CAP);
2927
+ }
2928
+ function neighbourValueCount(state, topLayers, start, end) {
2929
+ let values = 0;
2930
+ for (let number = start; number < end; number++) {
2931
+ const top = topLayers[number];
2932
+ if (top < 0) continue;
2933
+ values += state.adjacency.level0Stride + top * state.adjacency.upperStride;
2934
+ }
2935
+ return values;
2936
+ }
2937
+ function serializePart(state, numbers, start, end) {
2938
+ const { numberOfOrdinal, ordinalOfNumber, topLayers } = numbers;
2939
+ const adjacency = state.adjacency;
2940
+ const levels = new Uint8Array(end - start);
2941
+ const values = new Uint32Array(neighbourValueCount(state, topLayers, start, end));
2942
+ let cursor = 0;
2943
+ for (let number = start; number < end; number++) {
2944
+ const top = topLayers[number];
2945
+ levels[number - start] = top + 1;
2946
+ for (let layer = 0; layer <= top; layer++) {
2947
+ const base = layerBase(adjacency, ordinalOfNumber[number], layer);
2948
+ const array = layerArray(adjacency, layer);
2949
+ const held = base === -1 ? 0 : Math.min(array[base], maxConns(state, layer));
2950
+ const countAt = cursor;
2951
+ cursor += 1;
2952
+ for (let i = 1; i <= held; i++) {
2953
+ const neighbourOrdinal = array[base + i];
2954
+ if (neighbourOrdinal < 0 || neighbourOrdinal >= numberOfOrdinal.length) continue;
2955
+ const neighbour = numberOfOrdinal[neighbourOrdinal];
2956
+ if (neighbour === NO_NUMBER || neighbour === number || topLayers[neighbour] < layer) continue;
2957
+ values[cursor] = neighbour;
2958
+ cursor += 1;
2959
+ }
2960
+ values[countAt] = cursor - countAt - 1;
2961
+ }
2962
+ }
2963
+ return { levels, neighbours: littleEndianBytesOf(values.subarray(0, cursor)) };
2964
+ }
2965
+ function serializeNumberedGraphParts(state, numberOfOrdinal, ordinalOfNumber, partSize) {
2966
+ const slots = adjacencySlots(state.adjacency);
2967
+ const topLayers = new Int8Array(ordinalOfNumber.length).fill(-1);
2968
+ for (let number = 0; number < ordinalOfNumber.length; number++) {
2969
+ const ordinal = ordinalOfNumber[number];
2970
+ if (ordinal >= slots) continue;
2971
+ topLayers[number] = savedTopLayer(state, ordinal);
2972
+ }
2973
+ const entryOrdinal = entryPointOf(state);
2974
+ const entryNumber = entryOrdinal >= 0 && entryOrdinal < numberOfOrdinal.length ? numberOfOrdinal[entryOrdinal] : NO_NUMBER;
2975
+ const entryHoldsNode = entryNumber !== NO_NUMBER && topLayers[entryNumber] >= 0;
2976
+ const header = {
2977
+ entryPoint: entryHoldsNode ? entryNumber : null,
2978
+ maxLayer: Math.max(0, Math.min(topLayerOf(state), MAX_LAYER_CAP)),
2979
+ m: state.M,
2980
+ efConstruction: state.efCons,
2981
+ metric: state.buildMetric
2982
+ };
2983
+ const numbers = { numberOfOrdinal, ordinalOfNumber, topLayers };
2984
+ const parts = [];
2985
+ for (let start = 0; start < ordinalOfNumber.length; start += partSize) {
2986
+ const end = Math.min(start + partSize, ordinalOfNumber.length);
2987
+ parts.push({ ...header, ...serializePart(state, numbers, start, end) });
2988
+ }
2989
+ return parts;
2990
+ }
2991
+ function graphWithoutNodes(part, firstNumber, numbers) {
2992
+ const values = valuesOf(part.neighbours);
2993
+ const levels = part.levels.slice();
2994
+ const kept = new Uint32Array(values.length);
2995
+ let cursor = 0;
2996
+ let length = 0;
2997
+ for (let at = 0; at < part.levels.length; at++) {
2998
+ const start = cursor;
2999
+ for (let layer = 0; layer < part.levels[at]; layer++) cursor += values[cursor] + 1;
3000
+ if (numbers.has(firstNumber + at)) {
3001
+ levels[at] = 0;
3002
+ continue;
3003
+ }
3004
+ kept.set(values.subarray(start, cursor), length);
3005
+ length += cursor - start;
3006
+ }
3007
+ const entryPoint = part.entryPoint !== null && numbers.has(part.entryPoint) ? null : part.entryPoint;
3008
+ return { ...part, entryPoint, levels, neighbours: littleEndianBytesOf(kept.subarray(0, length)) };
3009
+ }
3010
+ function endsInsideAList() {
3011
+ throw new NarsilError(
3012
+ ErrorCodes.PERSISTENCE_LOAD_FAILED,
3013
+ "Invalid vector graph payload: neighbours ends inside a list"
3014
+ );
3015
+ }
3016
+ function requireWholeNeighbourLists(graph) {
3017
+ const values = valuesOf(graph.neighbours);
3018
+ if (values.length * BYTES_PER_VALUE !== graph.neighbours.byteLength) endsInsideAList();
3019
+ let cursor = 0;
3020
+ for (let number = 0; number < graph.levels.length; number++) {
3021
+ for (let layer = 0; layer < graph.levels[number]; layer++) {
3022
+ if (cursor >= values.length) endsInsideAList();
3023
+ const count = values[cursor];
3024
+ if (count > values.length - cursor - 1) endsInsideAList();
3025
+ cursor += count + 1;
3026
+ }
3027
+ }
3028
+ }
3029
+ function topLayerInParts(graph, number) {
3030
+ const part = graph.parts[Math.floor(number / graph.partSize)];
3031
+ if (part === void 0) return -1;
3032
+ const at = number % graph.partSize;
3033
+ return at < part.levels.length ? part.levels[at] - 1 : -1;
3034
+ }
3035
+ function highestNodeOrdinal(graph, ordinalAt) {
3036
+ let highest = -1;
3037
+ for (let p = 0; p < graph.parts.length; p++) {
3038
+ const levels = graph.parts[p].levels;
3039
+ for (let at = 0; at < levels.length; at++) {
3040
+ const ordinal = ordinalAt(p * graph.partSize + at);
3041
+ if (levels[at] > 0 && ordinal > highest) highest = ordinal;
3042
+ }
3043
+ }
3044
+ return highest;
3045
+ }
3046
+ function deserializeNumberedGraph(state, graph, ordinalOfNumber) {
3047
+ for (const part of graph.parts) requireWholeNeighbourLists(part);
3048
+ resetGraph(state);
3049
+ const ordinalAt = (number) => number < ordinalOfNumber.length ? ordinalOfNumber[number] : NO_ORDINAL;
3050
+ const highestOrdinal = highestNodeOrdinal(graph, ordinalAt);
3051
+ if (highestOrdinal >= 0) ensureCapacity(state, highestOrdinal + 1);
3052
+ const kept = [];
3053
+ let highestLayer = -1;
3054
+ let nodeOnHighestLayer = NO_ORDINAL;
3055
+ for (let p = 0; p < graph.parts.length; p++) {
3056
+ const part = graph.parts[p];
3057
+ const values = valuesOf(part.neighbours);
3058
+ let cursor = 0;
3059
+ for (let at = 0; at < part.levels.length; at++) {
3060
+ const number = p * graph.partSize + at;
3061
+ const top = part.levels[at] - 1;
3062
+ if (top < 0) continue;
3063
+ const ordinal = ordinalAt(number);
3064
+ const keptTop = Math.min(top, MAX_LAYER_CAP);
3065
+ if (ordinal !== NO_ORDINAL) {
3066
+ createNode(state.adjacency, ordinal, keptTop);
3067
+ Atomics.add(state.header, GRAPH_NODE_COUNT, 1);
3068
+ if (keptTop > highestLayer) {
3069
+ highestLayer = keptTop;
3070
+ nodeOnHighestLayer = ordinal;
3071
+ }
3072
+ }
3073
+ for (let layer = 0; layer <= top; layer++) {
3074
+ const count = values[cursor];
3075
+ kept.length = 0;
3076
+ const limit = maxConns(state, layer);
3077
+ for (let i = 1; i <= count && kept.length < limit; i++) {
3078
+ const neighbour = values[cursor + i];
3079
+ const neighbourOrdinal = ordinalAt(neighbour);
3080
+ if (neighbourOrdinal === NO_ORDINAL || neighbour === number) continue;
3081
+ if (topLayerInParts(graph, neighbour) < layer) continue;
3082
+ if (!kept.includes(neighbourOrdinal)) kept.push(neighbourOrdinal);
3083
+ }
3084
+ cursor += count + 1;
3085
+ if (ordinal !== NO_ORDINAL && layer <= keptTop) {
3086
+ writeNeighbors(state.adjacency, ordinal, layer, kept, kept.length);
3087
+ }
3088
+ }
3089
+ }
3090
+ }
3091
+ const first = graph.parts[0];
3092
+ const entryPoint = first === void 0 ? null : first.entryPoint;
3093
+ const entryOrdinal = entryPoint === null ? NO_ORDINAL : ordinalAt(entryPoint);
3094
+ const entryTop = entryPoint === null ? -1 : Math.min(topLayerInParts(graph, entryPoint), MAX_LAYER_CAP);
3095
+ const entryIsUsable = entryOrdinal !== NO_ORDINAL && entryTop >= 0;
3096
+ Atomics.store(state.header, GRAPH_ENTRY_POINT, entryIsUsable ? entryOrdinal : nodeOnHighestLayer);
3097
+ Atomics.store(state.header, GRAPH_TOP_LAYER, entryIsUsable ? entryTop : highestLayer);
3098
+ leaveIncomingUncounted(state);
3099
+ }
3100
+
3101
+ // src/vector/hnsw/persistence.ts
3102
+ function resolveNodes(state, store, data) {
3103
+ const nodes = [];
3104
+ let maxOrd = -1;
3105
+ for (const [docId, maxLayer, layerConns] of data.nodes) {
3106
+ const ord = store.getOrdinal(docId);
3107
+ if (ord === void 0 || !store.holdsOrdinal(ord)) continue;
3108
+ const clampedMaxLayer = Math.min(Math.max(maxLayer, 0), MAX_LAYER_CAP);
3109
+ const layers = [];
3110
+ for (const [layer, neighbors] of layerConns) {
3111
+ if (layer < 0 || layer > clampedMaxLayer) continue;
3112
+ const limit = maxConns(state, layer);
3113
+ const resolved = [];
3114
+ for (const neighborDoc of neighbors) {
3115
+ if (resolved.length >= limit) break;
3116
+ const neighborOrd = store.getOrdinal(neighborDoc);
3117
+ if (neighborOrd === void 0) continue;
3118
+ addConnection(resolved, neighborOrd);
3119
+ if (neighborOrd > maxOrd) maxOrd = neighborOrd;
3120
+ }
3121
+ layers.push([layer, resolved]);
3122
+ }
3123
+ if (ord > maxOrd) maxOrd = ord;
3124
+ nodes.push({ ord, maxLayer: clampedMaxLayer, layers });
3125
+ }
3126
+ return { nodes, maxOrd };
3127
+ }
3128
+ function deserializeGraph(state, store, data) {
3129
+ resetGraph(state);
3130
+ const { nodes, maxOrd } = resolveNodes(state, store, data);
3131
+ if (maxOrd >= 0) {
3132
+ ensureCapacity(state, maxOrd + 1);
3133
+ }
3134
+ for (const node of nodes) {
3135
+ createNode(state.adjacency, node.ord, node.maxLayer);
3136
+ for (const [layer, neighbors] of node.layers) {
3137
+ writeNeighbors(state.adjacency, node.ord, layer, neighbors, neighbors.length);
3138
+ }
3139
+ Atomics.add(state.header, GRAPH_NODE_COUNT, 1);
3140
+ }
3141
+ if (data.entryPoint != null && data.entryPoint !== "") {
3142
+ const epOrd = store.getOrdinal(data.entryPoint);
3143
+ if (epOrd !== void 0 && nodeExists(state, epOrd)) {
3144
+ Atomics.store(state.header, GRAPH_ENTRY_POINT, epOrd);
3145
+ Atomics.store(state.header, GRAPH_TOP_LAYER, Math.min(Math.max(data.maxLayer, 0), MAX_LAYER_CAP));
3146
+ }
3147
+ }
3148
+ if (entryPointOf(state) === -1 && Atomics.load(state.header, GRAPH_NODE_COUNT) > 0) {
3149
+ let bestOrd = -1;
3150
+ let bestLayer = -1;
3151
+ const slots = adjacencySlots(state.adjacency);
3152
+ for (let ord = 0; ord < slots; ord++) {
3153
+ const level2 = nodeMaxLayer(state, ord);
3154
+ if (level2 === -1) continue;
3155
+ if (level2 > bestLayer) {
3156
+ bestLayer = level2;
3157
+ bestOrd = ord;
3158
+ }
3159
+ }
3160
+ Atomics.store(state.header, GRAPH_ENTRY_POINT, bestOrd);
3161
+ Atomics.store(state.header, GRAPH_TOP_LAYER, bestLayer);
3162
+ }
3163
+ leaveIncomingUncounted(state);
3164
+ }
3165
+
3166
+ // src/vector/hnsw/scratch-bytes.ts
3167
+ function listBytes(list) {
3168
+ return list.ords.byteLength + list.distances.byteLength;
3169
+ }
3170
+ function workspaceBytes(workspace) {
3171
+ let bytes = listBytes(workspace.frontier) + listBytes(workspace.found) + listBytes(workspace.traversal) + listBytes(workspace.working) + listBytes(workspace.pruneCandidates) + listBytes(workspace.pruneSelection) + listBytes(workspace.repairCandidates) + listBytes(workspace.repairSelection) + workspace.entryPoints.byteLength;
3172
+ for (const list of workspace.linkSelections) bytes += listBytes(list);
3173
+ return bytes;
3174
+ }
3175
+ function searchScratchBytes(state) {
3176
+ const held = workspaceBytes(state.workspace) + state.neighborScratch.byteLength + state.visited.byteLength;
3177
+ const native = attachedNativeField(state);
3178
+ return native === null ? held : held + nativeScratchBytes(state, native);
3179
+ }
3180
+
3181
+ // src/vector/native/walk.ts
3182
+ function copyCandidates(store, count, target) {
3183
+ ensureListCapacity(target, count);
3184
+ target.ords.set(store.ordinals.subarray(0, count));
3185
+ target.distances.set(store.distances.subarray(0, count));
3186
+ target.size = count;
3187
+ }
3188
+ function nativeTraverse(state, field, query, candidateCount, metric, filter, candidates) {
3189
+ const store = field.store;
3190
+ reserveOrdinals(store, candidateCount);
3191
+ let count;
3192
+ try {
3193
+ count = field.core.search(
3194
+ field.graph,
3195
+ store.handle,
3196
+ query,
3197
+ nativeMetricCode(metric),
3198
+ candidateCount,
3199
+ state.locks.threadSlot,
3200
+ store.ordinals,
3201
+ store.distances,
3202
+ filter === void 0 ? null : filter.bits
3203
+ );
3204
+ } catch (error) {
3205
+ stopUsingTheNativeSearchCore(error);
3206
+ return false;
3207
+ }
3208
+ if (count < 0) return false;
3209
+ copyCandidates(store, count, candidates);
3210
+ return true;
3211
+ }
3212
+ function nativeRescoredHits(store, candidates, query, depth, metric, minSimilarity, filter) {
3213
+ const nearest = Math.min(candidates.size, depth);
3214
+ reserveOrdinals(store, nearest);
3215
+ const ordinals = store.ordinals;
3216
+ let kept = 0;
3217
+ for (let i = 0; i < candidates.size && kept < nearest; i++) {
3218
+ const ord = candidates.ords[i];
3219
+ if (filter && !ordinalFilterHas(filter, ord)) continue;
3220
+ ordinals[kept++] = ord;
3221
+ }
3222
+ const distances = nativeScores(store, query, metric, kept);
3223
+ if (distances === null) return null;
3224
+ const hits = [];
3225
+ for (let i = 0; i < kept; i++) {
3226
+ if (distances[i] === Number.POSITIVE_INFINITY) continue;
3227
+ const score = toScore(distances[i], metric);
3228
+ if (score < minSimilarity) continue;
3229
+ hits.push({ ord: ordinals[i], score });
3230
+ }
3231
+ return hits;
3232
+ }
3233
+
3234
+ // src/vector/search-depth/constants.ts
3235
+ var NARROW_DIMENSION_LIMIT = 768;
3236
+ var WIDE_DIMENSION_LIMIT = 1024;
3237
+ var DEFAULT_EF_SEARCH_MARGIN_BY_BAND = {
3238
+ narrow: 50,
3239
+ middle: 75,
3240
+ wide: 0
3241
+ };
3242
+ var DEFAULT_OSQ2_MIDDLE_RESCORE_DEPTH_WITHIN_WALK_PER_SQUARE_ROOT_OF_LIMIT = 18;
3243
+ var DEFAULT_RESCORE_DEPTH_FLOOR_PER_LIMIT_BY_BITS = {
3244
+ 1: 3,
3245
+ 2: 3,
3246
+ 4: 1,
3247
+ 8: 1
3248
+ };
3249
+ var DEFAULT_RESCORE_DEPTH_BY_BITS = {
3250
+ 1: {
3251
+ narrow: { perLimit: 3.23, perSquareRootOfLimit: 55.9 },
3252
+ middle: { perLimit: 2.2, perSquareRootOfLimit: 27 },
3253
+ wide: { perLimit: 3, perSquareRootOfLimit: 0 }
3254
+ },
3255
+ 2: {
3256
+ narrow: { perLimit: 2.6, perSquareRootOfLimit: 22.5 },
3257
+ middle: { perLimit: 1, perSquareRootOfLimit: 16 },
3258
+ wide: { perLimit: 3, perSquareRootOfLimit: 0 }
3259
+ },
3260
+ 4: {
3261
+ narrow: { perLimit: 5, perSquareRootOfLimit: 0 },
3262
+ middle: { perLimit: 5, perSquareRootOfLimit: 0 },
3263
+ wide: { perLimit: 2, perSquareRootOfLimit: 0 }
3264
+ },
3265
+ 8: {
3266
+ narrow: { perLimit: 2, perSquareRootOfLimit: 0 },
3267
+ middle: { perLimit: 2, perSquareRootOfLimit: 0 },
3268
+ wide: { perLimit: 2, perSquareRootOfLimit: 0 }
3269
+ }
3270
+ };
3271
+
3272
+ // src/vector/search-depth/index.ts
3273
+ function dimensionBand(dimension) {
3274
+ if (dimension < NARROW_DIMENSION_LIMIT) return "narrow";
3275
+ return dimension < WIDE_DIMENSION_LIMIT ? "middle" : "wide";
3276
+ }
3277
+ function defaultRescoreDepth(bits, dimension, k) {
3278
+ const band = dimensionBand(dimension);
3279
+ const { perLimit, perSquareRootOfLimit } = DEFAULT_RESCORE_DEPTH_BY_BITS[bits][band];
3280
+ const floor = DEFAULT_RESCORE_DEPTH_FLOOR_PER_LIMIT_BY_BITS[bits] * k;
3281
+ const depth = Math.max(floor, Math.ceil(perLimit * k + perSquareRootOfLimit * Math.sqrt(k)));
3282
+ if (bits !== 2 || band !== "middle") return depth;
3283
+ const withinDefaultWalk = Math.min(
3284
+ k + DEFAULT_EF_SEARCH_MARGIN_BY_BAND.middle,
3285
+ Math.ceil(k + DEFAULT_OSQ2_MIDDLE_RESCORE_DEPTH_WITHIN_WALK_PER_SQUARE_ROOT_OF_LIMIT * Math.sqrt(k))
3286
+ );
3287
+ return Math.max(depth, withinDefaultWalk);
3288
+ }
3289
+ function defaultEfSearch(dimension, k) {
3290
+ return Math.max(DEFAULT_EF_SEARCH, k + DEFAULT_EF_SEARCH_MARGIN_BY_BAND[dimensionBand(dimension)]);
3291
+ }
3292
+
3293
+ // src/vector/hnsw/search.ts
3294
+ function searchDepths(state, k, options) {
3295
+ const calibrated = activeQuantizer(state);
3296
+ const quantizer = calibrated === void 0 || calibrated.size === 0 ? void 0 : calibrated;
3297
+ const window = options.rescoreWindow ?? k;
3298
+ let depth = window;
3299
+ if (quantizer !== void 0) {
3300
+ depth = options.oversample === void 0 ? defaultRescoreDepth(quantizer.bits, state.dimension, window) : Math.ceil(window * options.oversample);
3301
+ }
3302
+ const ef = Math.max(options.efSearch ?? defaultEfSearch(state.dimension, window), depth);
3303
+ return { ef, depth, useQuantized: quantizer !== void 0 };
3304
+ }
3305
+ function requireQueryDimension(state, query) {
3306
+ if (query.length !== state.dimension) {
3307
+ throw new NarsilError(
3308
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
3309
+ `Vector dimension mismatch: expected ${state.dimension}, got ${query.length}`,
3310
+ { expected: state.dimension, received: query.length }
3311
+ );
3312
+ }
3313
+ }
3314
+ function graphIsEmpty(state) {
3315
+ return entryPointOf(state) === -1 || nodeCountOf(state) - tombstoneCountOf(state) === 0;
3316
+ }
3317
+ function traverse(state, query, k, searchMetric, options) {
3318
+ const { ef, depth, useQuantized } = searchDepths(state, k, options);
3319
+ const quantizer = useQuantized ? activeQuantizer(state) : void 0;
3320
+ const filter = options.filter;
3321
+ const workspace = state.workspace;
3322
+ const candidates = workspace.traversal;
3323
+ const native = nativeFieldFor(state);
3324
+ if (native !== null) {
3325
+ candidates.size = 0;
3326
+ if (nativeTraverse(state, native, query, ef, searchMetric, filter, candidates)) {
3327
+ return { candidates, depth: useQuantized ? depth : 0, arenaQuery: null, qMag: 0, native };
3328
+ }
3329
+ }
3330
+ const store = state.store;
3331
+ const arenaQuery = store.prepareQueryArena(query);
3332
+ const qMag = arenaQuery ? arenaQuery.magnitude : magnitude(query);
3333
+ let distFn;
3334
+ if (quantizer !== void 0) {
3335
+ const prepared = quantizer.prepareQuery(query);
3336
+ if (prepared) distFn = quantizer.preparedDistance(prepared);
3337
+ }
3338
+ if (!distFn && arenaQuery) {
3339
+ distFn = store.queryDistance(arenaQuery, searchMetric);
3340
+ }
3341
+ candidates.size = 0;
3342
+ lockGraphShared(state.locks);
3343
+ try {
3344
+ const entryPoint = entryPointOf(state);
3345
+ if (entryPoint === -1) return { candidates, depth: useQuantized ? depth : 0, arenaQuery, qMag, native: null };
3346
+ setSingleEntryPoint(workspace, entryPoint);
3347
+ for (let layer = topLayerOf(state); layer >= 1; layer--) {
3348
+ searchLayer(state, query, qMag, 1, layer, searchMetric, false, distFn, candidates, void 0, options.clock);
3349
+ if (candidates.size > 0) {
3350
+ setSingleEntryPoint(workspace, candidates.ords[0]);
3351
+ }
3352
+ }
3353
+ searchLayer(state, query, qMag, ef, 0, searchMetric, true, distFn, candidates, filter, options.clock);
3354
+ } finally {
3355
+ unlockGraphShared(state.locks);
3356
+ }
3357
+ return { candidates, depth: useQuantized ? depth : 0, arenaQuery, qMag, native: null };
3358
+ }
3359
+ function bestFirst(a, b) {
3360
+ return b.score - a.score || compareCodePoints(a.docId, b.docId);
3361
+ }
3362
+ function resolveEvery(hits, docIdOf) {
3363
+ const resolved = [];
3364
+ for (const hit of hits) {
3365
+ const docId = docIdOf(hit.ord);
3366
+ if (docId !== void 0) resolved.push({ ord: hit.ord, score: hit.score, docId });
3367
+ }
3368
+ return resolved;
3369
+ }
3370
+ function scoresDescend(hits) {
3371
+ for (let i = 1; i < hits.length; i++) {
3372
+ if (!(hits[i].score <= hits[i - 1].score)) return false;
3373
+ }
3374
+ return hits.length === 0 || !Number.isNaN(hits[0].score);
3375
+ }
3376
+ function bestResolvedHits(hits, k, docIdOf) {
3377
+ if (!Number.isInteger(k) || k <= 0) {
3378
+ return resolveEvery(hits, docIdOf).sort(bestFirst).slice(0, k);
3379
+ }
3380
+ let ordered = hits;
3381
+ if (!scoresDescend(ordered)) {
3382
+ if (hits.some((hit) => Number.isNaN(hit.score))) return resolveEvery(hits, docIdOf).sort(bestFirst).slice(0, k);
3383
+ ordered = hits.slice().sort((a, b) => b.score - a.score);
3384
+ }
3385
+ const taken = [];
3386
+ for (const hit of ordered) {
3387
+ if (taken.length >= k && hit.score < taken[k - 1].score) break;
3388
+ const docId = docIdOf(hit.ord);
3389
+ if (docId !== void 0) taken.push({ ord: hit.ord, score: hit.score, docId });
3390
+ }
3391
+ return taken.sort(bestFirst).slice(0, k);
3392
+ }
3393
+ function collectHits(state, query, k, searchMetric, minSimilarity, options) {
3394
+ requireQueryDimension(state, query);
3395
+ if (graphIsEmpty(state)) return [];
3396
+ const traversal = traverse(state, query, k, searchMetric, options);
3397
+ const { candidates } = traversal;
3398
+ const filter = options.filter;
3399
+ if (traversal.depth === 0) return estimatedHits(candidates, 0, searchMetric, minSimilarity, filter);
3400
+ const rescored = rescoredHits(state, traversal, query, traversal.depth, searchMetric, minSimilarity, filter);
3401
+ if (options.rescoreWindow === void 0 || minSimilarity !== Number.NEGATIVE_INFINITY) return rescored;
3402
+ const rest = endOfRescored(candidates, traversal.depth, filter);
3403
+ return rescored.concat(estimatedHits(candidates, rest, searchMetric, minSimilarity, filter));
3404
+ }
3405
+ function endOfRescored(candidates, depth, filter) {
3406
+ if (filter === void 0) return Math.min(depth, candidates.size);
3407
+ let taken = 0;
3408
+ for (let i = 0; i < candidates.size; i++) {
3409
+ if (!ordinalFilterHas(filter, candidates.ords[i])) continue;
3410
+ taken += 1;
3411
+ if (taken === depth) return i + 1;
3412
+ }
3413
+ return candidates.size;
3414
+ }
3415
+ function estimatedHits(candidates, from, metric, minSimilarity, filter) {
3416
+ const hits = [];
3417
+ for (let i = from; i < candidates.size; i++) {
3418
+ const ord = candidates.ords[i];
3419
+ if (filter && !ordinalFilterHas(filter, ord)) continue;
3420
+ const score = toScore(candidates.distances[i], metric);
3421
+ if (score < minSimilarity) continue;
3422
+ hits.push({ ord, score });
3423
+ }
3424
+ return hits;
3425
+ }
3426
+ function rescoredHits(state, traversal, query, depth, metric, minSimilarity, filter) {
3427
+ const { candidates, native } = traversal;
3428
+ if (native !== null) {
3429
+ const rescored = nativeRescoredHits(native.store, candidates, query, depth, metric, minSimilarity, filter);
3430
+ if (rescored !== null) return rescored;
3431
+ const arena = state.store.prepareQueryArena(query);
3432
+ const qMag2 = arena ? arena.magnitude : magnitude(query);
3433
+ return rescoreWithFullPrecision(state, candidates, query, qMag2, arena, depth, metric, minSimilarity, filter);
3434
+ }
3435
+ const { qMag, arenaQuery } = traversal;
3436
+ return rescoreWithFullPrecision(state, candidates, query, qMag, arenaQuery, depth, metric, minSimilarity, filter);
3437
+ }
3438
+ function rescoreWithFullPrecision(state, candidates, query, qMag, arenaQuery, depth, metric, minSimilarity, filter) {
3439
+ const rescored = [];
3440
+ let taken = 0;
3441
+ for (let i = 0; i < candidates.size && taken < depth; i++) {
3442
+ const ord = candidates.ords[i];
3443
+ if (filter && !ordinalFilterHas(filter, ord)) continue;
3444
+ taken += 1;
3445
+ let fullDistance;
3446
+ if (arenaQuery) {
3447
+ fullDistance = state.store.distanceFromArena(arenaQuery, ord, metric);
3448
+ if (fullDistance === Number.POSITIVE_INFINITY) continue;
3449
+ } else {
3450
+ fullDistance = state.store.distanceFromQuery(query, qMag, ord, metric);
3451
+ if (fullDistance === Number.POSITIVE_INFINITY) continue;
3452
+ }
3453
+ const score = toScore(fullDistance, metric);
3454
+ if (score < minSimilarity) continue;
3455
+ rescored.push({ ord, score });
3456
+ }
3457
+ return rescored;
3458
+ }
3459
+ function search(state, docIdOf, query, k, searchMetric, minSimilarity, options = {}) {
3460
+ const hits = collectHits(state, query, k, searchMetric, minSimilarity, options);
3461
+ return bestResolvedHits(hits, k, docIdOf).map((hit) => ({
3462
+ docId: hit.docId,
3463
+ score: hit.score,
3464
+ termFrequencies: {},
3465
+ fieldLengths: {},
3466
+ idf: {}
3467
+ }));
3468
+ }
3469
+ function searchOrdinals(state, docIdOf, query, k, searchMetric, minSimilarity, options = {}) {
3470
+ const hits = collectHits(state, query, k, searchMetric, minSimilarity, options);
3471
+ return bestResolvedHits(hits, k, docIdOf);
3472
+ }
3473
+
3474
+ // src/vector/hnsw/search-off-thread.ts
3475
+ function chosenOrdinals(candidates, depth, filter) {
3476
+ const nearest = Math.min(candidates.size, depth);
3477
+ const ordinals = new Int32Array(nearest);
3478
+ let kept = 0;
3479
+ for (let i = 0; i < candidates.size && kept < nearest; i++) {
3480
+ const ord = candidates.ords[i];
3481
+ if (filter && !ordinalFilterHas(filter, ord)) continue;
3482
+ ordinals[kept++] = ord;
3483
+ }
3484
+ return ordinals.subarray(0, kept);
3485
+ }
3486
+ function onFullPrecision(rescore) {
3487
+ const { state, candidates, query, depth, metric, minSimilarity, filter } = rescore;
3488
+ const arena = state.store.prepareQueryArena(query);
3489
+ const qMag = arena ? arena.magnitude : magnitude(query);
3490
+ return rescoreWithFullPrecision(state, candidates, query, qMag, arena, depth, metric, minSimilarity, filter);
3491
+ }
3492
+ async function rescoredOffThread(rescore) {
3493
+ const { core, field, candidates, query, depth, metric, minSimilarity, filter, slot } = rescore;
3494
+ const ordinals = chosenOrdinals(candidates, depth, filter);
3495
+ if (ordinals.length === 0) return [];
3496
+ let scored;
3497
+ try {
3498
+ scored = await core.scoreAsync(field.store.handle, query, nativeMetricCode(metric), ordinals, ordinals.length, slot);
3499
+ } catch {
3500
+ return onFullPrecision(rescore);
3501
+ }
3502
+ if (scored.status !== NATIVE_OK) return onFullPrecision(rescore);
3503
+ const hits = [];
3504
+ for (let i = 0; i < ordinals.length; i++) {
3505
+ const distance = scored.distances[i];
3506
+ if (distance === Number.POSITIVE_INFINITY) continue;
3507
+ const score = toScore(distance, metric);
3508
+ if (score < minSimilarity) continue;
3509
+ hits.push({ ord: ordinals[i], score });
3510
+ }
3511
+ return hits;
3512
+ }
3513
+ async function searchOffThread(state, docIdOf, query, k, metric, minSimilarity, options) {
3514
+ requireQueryDimension(state, query);
3515
+ if (graphIsEmpty(state)) return [];
3516
+ const core = threadedNativeCore();
3517
+ const field = nativeFieldFor(state);
3518
+ if (core === null || field === null) return null;
3519
+ const { ef, depth, useQuantized } = searchDepths(state, k, options);
3520
+ const filter = options.filter;
3521
+ return withNativeThreadSlot(async (slot) => {
3522
+ let found;
3523
+ try {
3524
+ found = await core.searchAsync(
3525
+ field.graph,
3526
+ field.store.handle,
3527
+ query,
3528
+ nativeMetricCode(metric),
3529
+ ef,
3530
+ slot,
3531
+ filter === void 0 ? null : filter.bits
3532
+ );
3533
+ } catch {
3534
+ return null;
3535
+ }
3536
+ if (found.count < 0) return null;
3537
+ const candidates = { ords: found.ordinals, distances: found.distances, size: found.count };
3538
+ if (!useQuantized) return bestResolvedHits(estimatedHits(candidates, 0, metric, minSimilarity, filter), k, docIdOf);
3539
+ const rescored = await rescoredOffThread({
3540
+ state,
3541
+ core,
3542
+ field,
3543
+ candidates,
3544
+ query,
3545
+ depth,
3546
+ metric,
3547
+ minSimilarity,
3548
+ filter,
3549
+ slot
3550
+ });
3551
+ if (options.rescoreWindow === void 0 || minSimilarity !== Number.NEGATIVE_INFINITY) {
3552
+ return bestResolvedHits(rescored, k, docIdOf);
3553
+ }
3554
+ const rest = endOfRescored(candidates, depth, filter);
3555
+ return bestResolvedHits(rescored.concat(estimatedHits(candidates, rest, metric, minSimilarity, filter)), k, docIdOf);
3556
+ });
3557
+ }
3558
+
3559
+ // src/vector/hnsw/snapshot.ts
3560
+ function exportSnapshot(state) {
3561
+ const adj = state.adjacency;
3562
+ const slots = adjacencySlots(adj);
3563
+ const used = upperUsed(adj);
3564
+ const stride = adj.level0Stride;
3565
+ rebindNodes(adj);
3566
+ reachTombstone(state, slots);
3567
+ return {
3568
+ dimension: state.dimension,
3569
+ m: state.M,
3570
+ efConstruction: state.efCons,
3571
+ metric: state.buildMetric,
3572
+ slots,
3573
+ upperUsed: used,
3574
+ nodeLevels: adj.nodeLevels.slice(0, slots),
3575
+ level0: adj.level0.slice(0, slots * stride),
3576
+ upperBase: adj.upperBase.slice(0, slots),
3577
+ upper: adj.upper.slice(0, used),
3578
+ tombstones: state.tombstones.slice(0, slots),
3579
+ tombstoneCount: Atomics.load(state.header, GRAPH_TOMBSTONE_COUNT),
3580
+ nodeCount: Atomics.load(state.header, GRAPH_NODE_COUNT),
3581
+ entryPointOrd: Atomics.load(state.header, GRAPH_ENTRY_POINT),
3582
+ topLayer: Atomics.load(state.header, GRAPH_TOP_LAYER)
3583
+ };
3584
+ }
3585
+ function restoreSnapshot(state, snapshot) {
3586
+ resetGraph(state);
3587
+ const adj = state.adjacency;
3588
+ ensureAdjacencyCapacity(adj, snapshot.slots);
3589
+ adj.nodeLevels.set(snapshot.nodeLevels);
3590
+ adj.level0.set(snapshot.level0);
3591
+ adj.upperBase.set(snapshot.upperBase);
3592
+ ensureUpperCapacity(adj, snapshot.upper.length);
3593
+ adj.upper.set(snapshot.upper);
3594
+ Atomics.store(state.header, GRAPH_UPPER_USED, snapshot.upperUsed);
3595
+ Atomics.store(state.header, GRAPH_SLOTS, snapshot.slots);
3596
+ state.tombstones = fixedView(adj.handles.tombstones, Uint8Array);
3597
+ state.tombstones.set(snapshot.tombstones);
3598
+ Atomics.store(state.header, GRAPH_TOMBSTONE_COUNT, snapshot.tombstoneCount);
3599
+ Atomics.store(state.header, GRAPH_NODE_COUNT, snapshot.nodeCount);
3600
+ Atomics.store(state.header, GRAPH_ENTRY_POINT, snapshot.entryPointOrd);
3601
+ Atomics.store(state.header, GRAPH_TOP_LAYER, snapshot.topLayer);
3602
+ leaveIncomingUncounted(state);
3603
+ }
3604
+
3605
+ // src/vector/hnsw/state.ts
3606
+ function openGraphState(handles, dimension, store, quantizer, threadSlot) {
3607
+ const shape = graphShapeOf(handles.header);
3608
+ const adjacency = openAdjacency(handles, shape.m, shape.mMax0);
3609
+ return {
3610
+ dimension,
3611
+ store,
3612
+ quantizer,
3613
+ adjacency,
3614
+ locks: openGraphLocks(handles, threadSlot),
3615
+ header: handles.header,
3616
+ tombstones: fixedView(handles.tombstones, Uint8Array),
3617
+ visited: new Uint32Array(0),
3618
+ visitStamp: 0,
3619
+ workspace: createHNSWWorkspace(),
3620
+ neighborScratch: new Int32Array(adjacency.level0Stride),
3621
+ M: shape.m,
3622
+ Mmax0: shape.mMax0,
3623
+ efCons: shape.efConstruction,
3624
+ buildMetric: shape.metric,
3625
+ mL: 1 / Math.log(shape.m),
3626
+ linkWrites: 0
3627
+ };
3628
+ }
3629
+
3630
+ // src/vector/hnsw/index.ts
3631
+ function createHNSWIndex(dimension, store, config, quantizer, handles) {
3632
+ const M = config?.m ?? 16;
3633
+ const shared = handles ?? createSharedGraphHandles({
3634
+ m: M,
3635
+ mMax0: M * 2,
3636
+ efConstruction: config?.efConstruction ?? 200,
3637
+ metric: config?.metric ?? "cosine"
3638
+ });
3639
+ const state = openGraphState(shared, dimension, store, quantizer, 0);
3640
+ const docIdOf = (ord) => store.docIdForOrdinal(ord);
3641
+ function exclusively(operation) {
3642
+ lockGraphExclusive(state.locks);
3643
+ try {
3644
+ return operation();
3645
+ } finally {
3646
+ unlockGraphExclusive(state.locks);
3647
+ }
3648
+ }
3649
+ function relinking(operation) {
3650
+ return exclusively(() => {
3651
+ state.linkWrites += 1;
3652
+ return operation();
3653
+ });
3654
+ }
3655
+ function insertNode2(docId) {
3656
+ const ord = store.getOrdinal(docId);
3657
+ if (ord === void 0) {
3658
+ throw new Error(`Cannot insert HNSW node: vector for "${docId}" not found in VectorStore`);
3659
+ }
3660
+ const entry = store.entryForOrdinal(ord);
3661
+ if (!entry) {
3662
+ throw new Error(`Cannot insert HNSW node: vector for "${docId}" not found in VectorStore`);
3663
+ }
3664
+ if (entry.vector.length !== dimension) {
3665
+ throw new NarsilError(
3666
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
3667
+ `Vector dimension mismatch: expected ${dimension}, got ${entry.vector.length}`,
3668
+ { docId, expected: dimension, received: entry.vector.length }
3669
+ );
3670
+ }
3671
+ insertNode(state, ord);
3672
+ }
3673
+ function* entriesIterator() {
3674
+ const slots = adjacencySlots(state.adjacency);
3675
+ for (let ord = 0; ord < slots; ord++) {
3676
+ if (!hasNode(state.adjacency, ord)) continue;
3677
+ if (isTombstoned(state, ord)) continue;
3678
+ const docId = store.docIdForOrdinal(ord);
3679
+ if (docId === void 0) continue;
3680
+ const entry = store.entryForOrdinal(ord);
3681
+ if (!entry) continue;
3682
+ yield [docId, { docId, vector: entry.vector, magnitude: entry.magnitude }];
3683
+ }
3684
+ }
3685
+ function compactionNeeded() {
3686
+ const nodeCount = nodeCountOf(state);
3687
+ if (nodeCount === 0) return false;
3688
+ const tombstones = tombstoneCountOf(state);
3689
+ return tombstones / nodeCount > COMPACTION_TOMBSTONE_RATIO || tombstones > COMPACTION_ABSOLUTE_THRESHOLD;
3690
+ }
3691
+ return {
3692
+ get dimension() {
3693
+ return dimension;
3694
+ },
3695
+ get size() {
3696
+ return nodeCountOf(state) - tombstoneCountOf(state);
3697
+ },
3698
+ get tombstoneCount() {
3699
+ return tombstoneCountOf(state);
3700
+ },
3701
+ get entryPointId() {
3702
+ const entryPoint = entryPointOf(state);
3703
+ return entryPoint === -1 ? null : store.docIdForOrdinal(entryPoint) ?? null;
3704
+ },
3705
+ get topLayer() {
3706
+ return topLayerOf(state);
3707
+ },
3708
+ get m() {
3709
+ return state.M;
3710
+ },
3711
+ get efConstruction() {
3712
+ return state.efCons;
3713
+ },
3714
+ get metric() {
3715
+ return state.buildMetric;
3716
+ },
3717
+ get graphBytes() {
3718
+ return graphBytes(state.adjacency);
3719
+ },
3720
+ get searchScratchBytes() {
3721
+ return searchScratchBytes(state);
3722
+ },
3723
+ get handles() {
3724
+ return shared;
3725
+ },
3726
+ get holdsUnlinkedNode() {
3727
+ return Atomics.load(state.header, GRAPH_UNLINKED_NODES) !== 0;
3728
+ },
3729
+ insertNode: insertNode2,
3730
+ insertOrdinal: (ordinal, connectsAfterwards = false) => insertNode(state, ordinal, false, connectsAfterwards),
3731
+ markTombstone: (docId) => {
3732
+ const ord = store.getOrdinal(docId);
3733
+ if (ord !== void 0) markTombstone(state, ord);
3734
+ },
3735
+ markTombstoneOrdinal: (ordinal) => markTombstone(state, ordinal),
3736
+ holdsNode: (ordinal) => hasNode(state.adjacency, ordinal),
3737
+ holdsTombstone: (ordinal) => hasNode(state.adjacency, ordinal) && isTombstoned(state, ordinal),
3738
+ has: (docId) => {
3739
+ const ord = store.getOrdinal(docId);
3740
+ return ord !== void 0 && hasNode(state.adjacency, ord) && !isTombstoned(state, ord);
3741
+ },
3742
+ isTombstoned: (docId) => {
3743
+ const ord = store.getOrdinal(docId);
3744
+ return ord !== void 0 && isTombstoned(state, ord);
3745
+ },
3746
+ search: (query, k, searchMetric, minSimilarity, options) => search(state, docIdOf, query, k, searchMetric, minSimilarity, options),
3747
+ searchOffThread: (query, k, searchMetric, minSimilarity, options) => searchOffThread(state, docIdOf, query, k, searchMetric, minSimilarity, options),
3748
+ clear: () => relinking(() => resetGraph(state)),
3749
+ retire: () => Atomics.store(shared.header, GRAPH_RETIRED, 1),
3750
+ entries: entriesIterator,
3751
+ compactionNeeded,
3752
+ compactTombstones: () => relinking(() => compactTombstones(state)),
3753
+ connectUnreached: () => relinking(() => connectAfterBuild(state)),
3754
+ connectOffThread: () => connectOffThread(state),
3755
+ connectOnThisThread: (inSlices) => connectOnThisThread(state, inSlices),
3756
+ rebuild: () => relinking(() => rebuild(state)),
3757
+ exclusively,
3758
+ deserialize: (data) => relinking(() => deserializeGraph(state, store, data)),
3759
+ serializeNumbered: (numberOfOrdinal, ordinalOfNumber, partSize) => serializeNumberedGraphParts(state, numberOfOrdinal, ordinalOfNumber, partSize),
3760
+ deserializeNumbered: (graph, ordinalOfNumber) => relinking(() => deserializeNumberedGraph(state, graph, ordinalOfNumber)),
3761
+ exportSnapshot: () => exportSnapshot(state),
3762
+ restoreSnapshot: (snapshot) => relinking(() => restoreSnapshot(state, snapshot))
3763
+ };
3764
+ }
3765
+
3766
+ // src/vector/osq/quantize.ts
3767
+ function osqBitsOf(mode) {
3768
+ switch (mode) {
3769
+ case "osq8":
3770
+ return 8;
3771
+ case "osq4":
3772
+ return 4;
3773
+ case "osq2":
3774
+ return 2;
3775
+ case "osq1":
3776
+ return 1;
3777
+ default:
3778
+ return null;
3779
+ }
3780
+ }
3781
+ function clamp(value, low, high) {
3782
+ return Math.min(Math.max(value, low), high);
3783
+ }
3784
+ function unitNormaliseInto(target, vector) {
3785
+ let squares = 0;
3786
+ for (let i = 0; i < vector.length; i++) squares += vector[i] * vector[i];
3787
+ const length = Math.sqrt(squares);
3788
+ if (length === 0) {
3789
+ for (let i = 0; i < vector.length; i++) target[i] = vector[i];
3790
+ return;
3791
+ }
3792
+ for (let i = 0; i < vector.length; i++) target[i] = vector[i] / length;
3793
+ }
3794
+ var CENTROID_TOTALS_PAST_DIMENSION = 2;
3795
+ function createCentroidSums(dimension, metric) {
3796
+ return {
3797
+ metric,
3798
+ dimension,
3799
+ totals: new Float64Array(dimension + CENTROID_TOTALS_PAST_DIMENSION),
3800
+ scratch: new Float64Array(dimension)
3801
+ };
3802
+ }
3803
+ function countOfCentroidSums(target) {
3804
+ return target.totals[target.dimension + 1];
3805
+ }
3806
+ function squaredLengthsOfCentroidSums(target) {
3807
+ return target.totals[target.dimension];
3808
+ }
3809
+ function addToCentroidSums(target, vector) {
3810
+ const { totals, scratch, dimension } = target;
3811
+ let squares = totals[dimension];
3812
+ if (target.metric === "cosine") {
3813
+ unitNormaliseInto(scratch, vector);
3814
+ for (let i = 0; i < dimension; i++) {
3815
+ totals[i] += scratch[i];
3816
+ squares += scratch[i] * scratch[i];
3817
+ }
3818
+ } else {
3819
+ for (let i = 0; i < dimension; i++) {
3820
+ totals[i] += vector[i];
3821
+ squares += vector[i] * vector[i];
3822
+ }
3823
+ }
3824
+ totals[dimension] = squares;
3825
+ totals[dimension + 1] += 1;
3826
+ }
3827
+ function meanOfCentroidSums(target) {
3828
+ const count = countOfCentroidSums(target);
3829
+ const mean = new Float64Array(target.dimension);
3830
+ for (let i = 0; i < mean.length; i++) mean[i] = target.totals[i] / count;
3831
+ return mean;
3832
+ }
3833
+ function centroidOfCentroidSums(target) {
3834
+ if (countOfCentroidSums(target) === 0) return null;
3835
+ const dimension = target.dimension;
3836
+ const mean = meanOfCentroidSums(target);
3837
+ const centroid = new Float32Array(dimension);
3838
+ if (target.metric === "cosine") {
3839
+ let squares = 0;
3840
+ for (let i = 0; i < dimension; i++) squares += mean[i] * mean[i];
3841
+ const length = Math.sqrt(squares);
3842
+ for (let i = 0; i < dimension; i++) centroid[i] = length === 0 ? mean[i] : mean[i] / length;
3843
+ return centroid;
3844
+ }
3845
+ for (let i = 0; i < dimension; i++) centroid[i] = mean[i];
3846
+ return centroid;
3847
+ }
3848
+ function level(value, lower, upper, steps) {
3849
+ if (upper === lower) return 0;
3850
+ return Math.floor((clamp(value, lower, upper) - lower) * steps / (upper - lower) + 0.5);
3851
+ }
3852
+ function loss(x, lower, upper, steps, norm) {
3853
+ let xe = 0;
3854
+ let e = 0;
3855
+ const width = (upper - lower) / steps;
3856
+ for (let i = 0; i < x.length; i++) {
3857
+ const q = lower + width * level(x[i], lower, upper, steps);
3858
+ xe += x[i] * (x[i] - q);
3859
+ e += (x[i] - q) * (x[i] - q);
3860
+ }
3861
+ return (1 - OSQ_LAMBDA) * xe * xe / norm + OSQ_LAMBDA * e;
3862
+ }
3863
+ function refine(x, lowerStart, upperStart, steps) {
3864
+ let lower = lowerStart;
3865
+ let upper = upperStart;
3866
+ let norm = 0;
3867
+ for (let i = 0; i < x.length; i++) norm += x[i] * x[i];
3868
+ if (norm === 0 || upper === lower) return [lower, upper];
3869
+ let best = loss(x, lower, upper, steps, norm);
3870
+ for (let round = 0; round < OSQ_REFINE_ROUNDS; round++) {
3871
+ let daa = 0;
3872
+ let dab = 0;
3873
+ let dbb = 0;
3874
+ let dax = 0;
3875
+ let dbx = 0;
3876
+ for (let i = 0; i < x.length; i++) {
3877
+ const s = level(x[i], lower, upper, steps) / steps;
3878
+ daa += (1 - s) * (1 - s);
3879
+ dab += (1 - s) * s;
3880
+ dbb += s * s;
3881
+ dax += x[i] * (1 - s);
3882
+ dbx += x[i] * s;
3883
+ }
3884
+ const m0 = (1 - OSQ_LAMBDA) * dax * dax / norm + OSQ_LAMBDA * daa;
3885
+ const m1 = (1 - OSQ_LAMBDA) * dax * dbx / norm + OSQ_LAMBDA * dab;
3886
+ const m2 = (1 - OSQ_LAMBDA) * dbx * dbx / norm + OSQ_LAMBDA * dbb;
3887
+ const det = m0 * m2 - m1 * m1;
3888
+ if (det === 0) return [lower, upper];
3889
+ const a = (m2 * dax - m1 * dbx) / det;
3890
+ const b = (m0 * dbx - m1 * dax) / det;
3891
+ if (Math.abs(a - lower) < OSQ_REFINE_TOLERANCE && Math.abs(b - upper) < OSQ_REFINE_TOLERANCE) return [lower, upper];
3892
+ const candidate = loss(x, a, b, steps, norm);
3893
+ if (candidate > best) return [lower, upper];
3894
+ lower = a;
3895
+ upper = b;
3896
+ best = candidate;
3897
+ }
3898
+ return [lower, upper];
3899
+ }
3900
+ function createOsqScratch(dimension) {
3901
+ return { normalised: new Float64Array(dimension), centred: new Float64Array(dimension) };
3902
+ }
3903
+ function osqQuantize(vector, centroid, bits, metric, scratch) {
3904
+ const dimension = vector.length;
3905
+ const v = scratch.normalised;
3906
+ if (metric === "cosine") unitNormaliseInto(v, vector);
3907
+ else for (let i = 0; i < dimension; i++) v[i] = vector[i];
3908
+ const x = scratch.centred;
3909
+ let correction = 0;
3910
+ let total = 0;
3911
+ let minimum = Number.POSITIVE_INFINITY;
3912
+ let maximum = Number.NEGATIVE_INFINITY;
3913
+ for (let i = 0; i < dimension; i++) {
3914
+ x[i] = v[i] - centroid[i];
3915
+ correction += metric === "euclidean" ? x[i] * x[i] : v[i] * centroid[i];
3916
+ total += x[i];
3917
+ if (x[i] < minimum) minimum = x[i];
3918
+ if (x[i] > maximum) maximum = x[i];
3919
+ }
3920
+ const mean = total / dimension;
3921
+ let spread = 0;
3922
+ for (let i = 0; i < dimension; i++) spread += (x[i] - mean) * (x[i] - mean);
3923
+ const std = Math.sqrt(spread / dimension);
3924
+ const steps = OSQ_STEPS[bits];
3925
+ const [lower, upper] = refine(
3926
+ x,
3927
+ clamp(mean - OSQ_GRID[bits] * std, minimum, maximum),
3928
+ clamp(mean + OSQ_GRID[bits] * std, minimum, maximum),
3929
+ steps
3930
+ );
3931
+ const levels = new Uint8Array(dimension);
3932
+ let sum = 0;
3933
+ for (let i = 0; i < dimension; i++) {
3934
+ levels[i] = level(x[i], lower, upper, steps);
3935
+ sum += levels[i];
3936
+ }
3937
+ return { levels, lower, upper, correction, sum };
3938
+ }
3939
+
3940
+ // src/vector/osq/products.ts
3941
+ function simdQueryProducts(simd, layout, documentOffset, queryOffset) {
3942
+ const { bits, dimension, codeBytes } = layout;
3943
+ if (bits === 8) return simd.dot_u8(documentOffset, queryOffset, dimension);
3944
+ if (bits === 4) return simd.osq_dot_nibbles_4x4(documentOffset, queryOffset, codeBytes);
3945
+ return simd.osq_dot_bits(documentOffset, queryOffset, codeBytes, bits);
3946
+ }
3947
+ function scalarQueryProducts(layout, document2, documentOffset, packed) {
3948
+ const { bits, dimension, codeBytes } = layout;
3949
+ if (bits === 8) return osqByteLevelProducts(document2, documentOffset, packed, 0, dimension);
3950
+ if (bits === 4) return osqNibbleProducts(document2, documentOffset, packed, 0, codeBytes);
3951
+ return osqStagedQueryProducts(document2, documentOffset, bits, packed, 0, codeBytes);
3952
+ }
3953
+ function simdPairProducts(simd, layout, offsetA, offsetB) {
3954
+ const { bits, dimension, codeBytes } = layout;
3955
+ if (bits === 8) return simd.dot_u8(offsetA, offsetB, dimension);
3956
+ if (bits === 4) return simd.osq_dot_nibbles_4x4(offsetA, offsetB, codeBytes);
3957
+ return simd.osq_dot_bits_pair(offsetA, offsetB, codeBytes, bits);
3958
+ }
3959
+ function scalarPairProducts(layout, bytesA, offsetA, bytesB, offsetB) {
3960
+ const { bits, dimension, codeBytes } = layout;
3961
+ if (bits === 8) return osqByteLevelProducts(bytesA, offsetA, bytesB, offsetB, dimension);
3962
+ if (bits === 4) return osqNibbleProducts(bytesA, offsetA, bytesB, offsetB, codeBytes);
3963
+ return osqNarrowPairProducts(bytesA, offsetA, bytesB, offsetB, bits, codeBytes);
3964
+ }
3965
+
3966
+ // src/vector/osq/recoding.ts
3967
+ function createCodeRecoding(source, centroid) {
3968
+ const { store, layout, metric } = source;
3969
+ const { capacity, slotStride, bits } = layout;
3970
+ const scratch = createOsqScratch(layout.dimension);
3971
+ const blocks = [];
3972
+ let recoded = new Uint8Array(0);
3973
+ function blockReaching(blockIndex, end) {
3974
+ const held = blocks[blockIndex];
3975
+ if (held !== void 0 && held.bytes.length >= end) return held;
3976
+ const length = Math.min(capacity * slotStride, Math.max(end, (held?.bytes.length ?? 0) * 2));
3977
+ const bytes = new Uint8Array(length);
3978
+ if (held !== void 0) bytes.set(held.bytes);
3979
+ const block = { bytes, data: new DataView(bytes.buffer), usedBytes: held?.usedBytes ?? 0 };
3980
+ blocks[blockIndex] = block;
3981
+ return block;
3982
+ }
3983
+ function markRecoded(ordinal) {
3984
+ if (ordinal >= recoded.length) {
3985
+ const grown = new Uint8Array(Math.max(ordinal + 1, recoded.length * 2));
3986
+ grown.set(recoded);
3987
+ recoded = grown;
3988
+ }
3989
+ recoded[ordinal] = 1;
3990
+ }
3991
+ function recode(ordinal) {
3992
+ if (!source.holds(ordinal) || !store.holdsOrdinal(ordinal)) return;
3993
+ const offset = ordinal % capacity * slotStride;
3994
+ const block = blockReaching(Math.floor(ordinal / capacity), offset + slotStride);
3995
+ const code = osqQuantize(store.vectorAt(ordinal), centroid, bits, metric, scratch);
3996
+ writeRecord(block.bytes, offset, code, bits, block.data);
3997
+ block.usedBytes = Math.max(block.usedBytes, offset + slotStride);
3998
+ markRecoded(ordinal);
3999
+ }
4000
+ function copyIntoStore(slots) {
4001
+ for (let ordinal = 0; ordinal < slots; ordinal++) {
4002
+ if (!source.holds(ordinal) || ordinal < recoded.length && recoded[ordinal] === 1) continue;
4003
+ recode(ordinal);
4004
+ if (ordinal >= recoded.length || recoded[ordinal] !== 1) source.clearCodes(ordinal);
4005
+ }
4006
+ const start = slotByteOffset(layout, 0);
4007
+ for (let blockIndex = 0; blockIndex < blocks.length; blockIndex++) {
4008
+ const block = blocks[blockIndex];
4009
+ if (block === void 0 || block.usedBytes === 0) continue;
4010
+ const target = store.codeBlockOf(blockIndex * capacity).bytes(start + block.usedBytes);
4011
+ target.set(block.bytes.subarray(0, block.usedBytes), start);
4012
+ }
4013
+ }
4014
+ return { centroid, recode, copyIntoStore };
4015
+ }
4016
+
4017
+ // src/vector/osq/view.ts
4018
+ function openSharedQuantizer(store, layout, metric) {
4019
+ const { dimension, bits, queryBits, codeBytes, capacity } = layout;
4020
+ const scratch = createOsqScratch(dimension);
4021
+ const placementQueryBits = osqPlacementQueryBits(bits);
4022
+ const documentTrailer = { lower: 0, upper: 0, correction: 0, sum: 0 };
4023
+ const otherTrailer = { lower: 0, upper: 0, correction: 0, sum: 0 };
4024
+ let codePresent = fixedView(store.handles.codePresent, Uint8Array);
4025
+ let centroidDot = 0;
4026
+ let centroidGeneration = -1;
4027
+ let currentQuery = null;
4028
+ let recoding = null;
4029
+ const singleOrdinal = new Int32Array(1);
4030
+ function reach2(ordinal) {
4031
+ if (ordinal < codePresent.length) return true;
4032
+ if (ordinal >= store.handles.codePresent.byteLength) return false;
4033
+ codePresent = fixedView(store.handles.codePresent, Uint8Array);
4034
+ return ordinal < codePresent.length;
4035
+ }
4036
+ function isCalibrated() {
4037
+ return Atomics.load(store.handles.header, STORE_CALIBRATED) === 1;
4038
+ }
4039
+ function recomputeCentroidDot(generation) {
4040
+ const centroid = store.handles.centroid;
4041
+ let total = 0;
4042
+ for (let i = 0; i < dimension; i++) total += centroid[i] * centroid[i];
4043
+ centroidDot = total;
4044
+ centroidGeneration = generation;
4045
+ }
4046
+ function centroidDotNow() {
4047
+ const generation = Atomics.load(store.handles.header, STORE_CALIBRATION_GENERATION);
4048
+ if (generation !== centroidGeneration) recomputeCentroidDot(generation);
4049
+ return centroidDot;
4050
+ }
4051
+ function holds2(ordinal) {
4052
+ return ordinal >= 0 && reach2(ordinal) && codePresent[ordinal] === 1;
4053
+ }
4054
+ function recordOffset(ordinal) {
4055
+ return slotByteOffset(layout, ordinal % capacity);
4056
+ }
4057
+ function markPresent(ordinal) {
4058
+ if (Atomics.load(codePresent, ordinal) !== 0) return;
4059
+ Atomics.add(store.handles.header, STORE_CODE_COUNT, 1);
4060
+ Atomics.store(codePresent, ordinal, 1);
4061
+ }
4062
+ function writeCentroid(centroid) {
4063
+ store.handles.centroid.set(centroid);
4064
+ Atomics.add(store.handles.header, STORE_CALIBRATION_GENERATION, 1);
4065
+ Atomics.store(store.handles.header, STORE_CALIBRATED, 1);
4066
+ }
4067
+ function measureInto(ordinals, sums) {
4068
+ if (nativeMeasure(store.handles, metric, ordinals, sums.totals)) return;
4069
+ for (const vector of storedVectors(ordinals)) addToCentroidSums(sums, vector);
4070
+ }
4071
+ function* storedVectors(ordinals) {
4072
+ for (const ordinal of ordinals) {
4073
+ if (store.holdsOrdinal(ordinal)) yield store.vectorAt(ordinal);
4074
+ }
4075
+ }
4076
+ function writeRecordOf(ordinal) {
4077
+ if (!reach2(ordinal) || !store.holdsOrdinal(ordinal)) return;
4078
+ const code = osqQuantize(store.vectorAt(ordinal), store.handles.centroid, bits, metric, scratch);
4079
+ const block = store.codeBlockOf(ordinal);
4080
+ const offset = recordOffset(ordinal);
4081
+ writeRecord(block.bytes(offset + layout.slotStride), offset, code, bits, block.data(offset + layout.slotStride));
4082
+ markPresent(ordinal);
4083
+ recoding?.recode(ordinal);
4084
+ }
4085
+ function writeCodesOf(ordinals) {
4086
+ if (!isCalibrated() || ordinals.length === 0) return;
4087
+ if (nativeQuantise(store.handles, metric, ordinals)) {
4088
+ if (recoding !== null) for (const ordinal of ordinals) recoding.recode(ordinal);
4089
+ return;
4090
+ }
4091
+ for (const ordinal of ordinals) writeRecordOf(ordinal);
4092
+ }
4093
+ function clearCodes(ordinal) {
4094
+ if (ordinal < 0 || !reach2(ordinal)) return;
4095
+ if (Atomics.exchange(codePresent, ordinal, 0) === 1) Atomics.sub(store.handles.header, STORE_CODE_COUNT, 1);
4096
+ }
4097
+ function stageQuery(block, query) {
4098
+ if (block.simd === null || !block.hasScratch) return false;
4099
+ if (block.stagedOrdinal !== QUERY_STAGED) {
4100
+ block.bytes(block.scratchByteOffset + query.packed.length).set(query.packed, block.scratchByteOffset);
4101
+ block.stagedOrdinal = QUERY_STAGED;
4102
+ }
4103
+ return true;
4104
+ }
4105
+ function productsAgainstQuery(block, documentOffset, query) {
4106
+ if (block.simd !== null && stageQuery(block, query)) {
4107
+ return simdQueryProducts(block.simd, layout, documentOffset, block.scratchByteOffset);
4108
+ }
4109
+ return scalarQueryProducts(layout, block.bytes(documentOffset + codeBytes), documentOffset, query.packed);
4110
+ }
4111
+ function distanceFrom(products, query, levelBits) {
4112
+ const estimate = osqEstimate(products, documentTrailer, bits, query, levelBits, dimension, centroidDotNow(), metric);
4113
+ return osqDistance(estimate, metric);
4114
+ }
4115
+ function estimateDistance(ordinal, query) {
4116
+ const block = store.codeBlockOf(ordinal);
4117
+ const offset = recordOffset(ordinal);
4118
+ const products = productsAgainstQuery(block, offset, query);
4119
+ readTrailer(block.data(offset + layout.slotStride), offset, codeBytes, documentTrailer);
4120
+ return distanceFrom(products, query, queryBits);
4121
+ }
4122
+ function prepareQuery(query) {
4123
+ if (!isCalibrated() || query.length !== dimension) return null;
4124
+ const code = osqQuantize(query, store.handles.centroid, queryBits, metric, scratch);
4125
+ const packed = new Uint8Array(layout.queryBytes);
4126
+ stageQueryLevels(code.levels, bits, packed);
4127
+ for (const block of store.openCodeBlocks()) block.stagedOrdinal = NOTHING_STAGED;
4128
+ currentQuery = {
4129
+ packed,
4130
+ lower: code.lower,
4131
+ upper: code.upper,
4132
+ correction: code.correction,
4133
+ sum: code.sum
4134
+ };
4135
+ return currentQuery;
4136
+ }
4137
+ function preparedDistance(prepared) {
4138
+ const generic = (ordinal) => holds2(ordinal) ? estimateDistance(ordinal, prepared) : Number.POSITIVE_INFINITY;
4139
+ if (store.handles.codeBlocks.length === 0) return generic;
4140
+ const block = store.codeBlockOf(0);
4141
+ const simd = block.simd;
4142
+ if (simd === null || !block.hasScratch) return generic;
4143
+ const presentView = codePresent;
4144
+ const limit = Math.min(presentView.length, capacity);
4145
+ const { slotsOffset, slotStride } = layout;
4146
+ const data = block.data(slotsOffset + slotStride);
4147
+ const dataLength = data.byteLength;
4148
+ const queryOffset = block.scratchByteOffset;
4149
+ return (ordinal) => {
4150
+ if (ordinal < 0 || ordinal >= limit || block.stagedOrdinal !== QUERY_STAGED) return generic(ordinal);
4151
+ if (presentView[ordinal] !== 1) return Number.POSITIVE_INFINITY;
4152
+ const offset = slotsOffset + ordinal * slotStride;
4153
+ if (offset + slotStride > dataLength) return generic(ordinal);
4154
+ const products = simdQueryProducts(simd, layout, offset, queryOffset);
4155
+ readTrailer(data, offset, codeBytes, documentTrailer);
4156
+ return distanceFrom(products, prepared, queryBits);
4157
+ };
4158
+ }
4159
+ function placementDistance2(vector) {
4160
+ if (placementQueryBits === queryBits) {
4161
+ const prepared = prepareQuery(vector);
4162
+ return prepared === null ? null : preparedDistance(prepared);
4163
+ }
4164
+ if (!isCalibrated() || vector.length !== dimension) return null;
4165
+ const code = osqQuantize(vector, store.handles.centroid, placementQueryBits, metric, scratch);
4166
+ const levels = new Uint8Array(2 * codeBytes);
4167
+ levels.set(code.levels);
4168
+ return (ordinal) => {
4169
+ if (!holds2(ordinal)) return Number.POSITIVE_INFINITY;
4170
+ const block = store.codeBlockOf(ordinal);
4171
+ const offset = recordOffset(ordinal);
4172
+ const products = osqNibblesByByteLevels(block.bytes(offset + codeBytes), offset, levels, codeBytes);
4173
+ readTrailer(block.data(offset + layout.slotStride), offset, codeBytes, documentTrailer);
4174
+ return distanceFrom(products, code, placementQueryBits);
4175
+ };
4176
+ }
4177
+ function distanceBetween(ordA, ordB) {
4178
+ if (!holds2(ordA) || !holds2(ordB)) return Number.POSITIVE_INFINITY;
4179
+ const blockA = store.codeBlockOf(ordA);
4180
+ const blockB = store.codeBlockOf(ordB);
4181
+ const offsetA = recordOffset(ordA);
4182
+ const offsetB = recordOffset(ordB);
4183
+ const products = blockA === blockB && blockA.simd !== null ? simdPairProducts(blockA.simd, layout, offsetA, offsetB) : scalarPairProducts(
4184
+ layout,
4185
+ blockA.bytes(offsetA + codeBytes),
4186
+ offsetA,
4187
+ blockB.bytes(offsetB + codeBytes),
4188
+ offsetB
4189
+ );
4190
+ readTrailer(blockA.data(offsetA + layout.slotStride), offsetA, codeBytes, documentTrailer);
4191
+ readTrailer(blockB.data(offsetB + layout.slotStride), offsetB, codeBytes, otherTrailer);
4192
+ return distanceFrom(products, otherTrailer, bits);
4193
+ }
4194
+ function pairDistance() {
4195
+ if (store.handles.codeBlocks.length === 0) return distanceBetween;
4196
+ const block = store.codeBlockOf(0);
4197
+ const simd = block.simd;
4198
+ if (simd === null) return distanceBetween;
4199
+ const presentView = codePresent;
4200
+ const limit = Math.min(presentView.length, capacity);
4201
+ const { slotsOffset, slotStride } = layout;
4202
+ const data = block.data(slotsOffset + slotStride);
4203
+ const dataLength = data.byteLength;
4204
+ return (ordA, ordB) => {
4205
+ if (ordA < 0 || ordB < 0 || ordA >= limit || ordB >= limit) return distanceBetween(ordA, ordB);
4206
+ if (presentView[ordA] !== 1 || presentView[ordB] !== 1) return Number.POSITIVE_INFINITY;
4207
+ const offsetA = slotsOffset + ordA * slotStride;
4208
+ const offsetB = slotsOffset + ordB * slotStride;
4209
+ if (Math.max(offsetA, offsetB) + slotStride > dataLength) return distanceBetween(ordA, ordB);
4210
+ const products = simdPairProducts(simd, layout, offsetA, offsetB);
4211
+ readTrailer(data, offsetA, codeBytes, documentTrailer);
4212
+ readTrailer(data, offsetB, codeBytes, otherTrailer);
4213
+ return distanceFrom(products, otherTrailer, bits);
4214
+ };
4215
+ }
4216
+ return {
4217
+ bits,
4218
+ metric,
4219
+ get size() {
4220
+ return Atomics.load(store.handles.header, STORE_CODE_COUNT);
4221
+ },
4222
+ get centroid() {
4223
+ return isCalibrated() ? store.handles.centroid : null;
4224
+ },
4225
+ isCalibrated,
4226
+ holdsOrdinal: holds2,
4227
+ writeCodes(ordinal) {
4228
+ singleOrdinal[0] = ordinal;
4229
+ writeCodesOf(singleOrdinal);
4230
+ },
4231
+ writeCodesOf,
4232
+ measureInto,
4233
+ calibrateFrom(ordinals) {
4234
+ const sums = createCentroidSums(dimension, metric);
4235
+ measureInto(ordinals, sums);
4236
+ const centroid = centroidOfCentroidSums(sums);
4237
+ if (centroid === null) return false;
4238
+ writeCentroid(centroid);
4239
+ return true;
4240
+ },
4241
+ restoreRecord(ordinal, record) {
4242
+ if (!reach2(ordinal) || record.length !== layout.slotStride) return;
4243
+ const block = store.codeBlockOf(ordinal);
4244
+ const offset = recordOffset(ordinal);
4245
+ block.bytes(offset + layout.slotStride).set(record, offset);
4246
+ markPresent(ordinal);
4247
+ recoding?.recode(ordinal);
4248
+ },
4249
+ recordAt(ordinal) {
4250
+ const block = store.codeBlockOf(ordinal);
4251
+ const offset = recordOffset(ordinal);
4252
+ return block.bytes(offset + layout.slotStride).subarray(offset, offset + layout.slotStride);
4253
+ },
4254
+ clearCodes,
4255
+ writeCentroid,
4256
+ resetCalibration() {
4257
+ recoding = null;
4258
+ Atomics.store(store.handles.header, STORE_CALIBRATED, 0);
4259
+ store.handles.centroid.fill(0);
4260
+ Atomics.add(store.handles.header, STORE_CALIBRATION_GENERATION, 1);
4261
+ },
4262
+ resetCodes() {
4263
+ recoding = null;
4264
+ codePresent = fixedView(store.handles.codePresent, Uint8Array);
4265
+ codePresent.fill(0);
4266
+ Atomics.store(store.handles.header, STORE_CODE_COUNT, 0);
4267
+ },
4268
+ beginRecoding(centroid) {
4269
+ recoding = createCodeRecoding({ store, layout, metric, holds: holds2, clearCodes }, Float32Array.from(centroid));
4270
+ },
4271
+ recode(ordinal) {
4272
+ recoding?.recode(ordinal);
4273
+ },
4274
+ finishRecoding() {
4275
+ if (recoding === null) return false;
4276
+ const finished = recoding;
4277
+ recoding = null;
4278
+ finished.copyIntoStore(store.slots);
4279
+ writeCentroid(finished.centroid);
4280
+ return true;
4281
+ },
4282
+ abandonRecoding() {
4283
+ recoding = null;
4284
+ },
4285
+ prepareQuery,
4286
+ distanceFromPreparedByOrdinal(prepared, ordinal) {
4287
+ if (!holds2(ordinal)) return Number.POSITIVE_INFINITY;
4288
+ return estimateDistance(ordinal, prepared);
4289
+ },
4290
+ preparedDistance,
4291
+ pairDistance,
4292
+ placementDistance: placementDistance2
4293
+ };
4294
+ }
4295
+
4296
+ // src/vector/osq/index.ts
4297
+ function createOsqQuantizer(dimension, bits, metric, store) {
4298
+ let opened = null;
4299
+ function shared() {
4300
+ if (opened === null && store.dimension === dimension && store.handles.codeLayout !== null) {
4301
+ opened = openSharedQuantizer(store.view, store.handles.codeLayout, metric);
4302
+ }
4303
+ return opened;
4304
+ }
4305
+ function requireShared() {
4306
+ const view = shared();
4307
+ if (view === null) throw new Error("The vector store holds no vector for the quantizer to work on");
4308
+ return view;
4309
+ }
4310
+ function ordinalOf(docId) {
4311
+ const ordinal = store.getOrdinal(docId);
4312
+ if (ordinal === void 0) {
4313
+ throw new Error(`Cannot quantize "${docId}": the vector store holds no vector for it`);
4314
+ }
4315
+ return ordinal;
4316
+ }
4317
+ return {
4318
+ dimension,
4319
+ bits,
4320
+ metric,
4321
+ get size() {
4322
+ return shared()?.size ?? 0;
4323
+ },
4324
+ get centroid() {
4325
+ return shared()?.centroid ?? null;
4326
+ },
4327
+ isCalibrated: () => shared()?.isCalibrated() ?? false,
4328
+ calibrate(ordinals) {
4329
+ if (ordinals.length > 0) requireShared().calibrateFrom(ordinals);
4330
+ },
4331
+ measure(ordinals, sums) {
4332
+ if (ordinals.length > 0) requireShared().measureInto(ordinals, sums);
4333
+ },
4334
+ quantize(docId) {
4335
+ const ordinal = ordinalOf(docId);
4336
+ const view = requireShared();
4337
+ if (!view.isCalibrated()) view.calibrateFrom(Int32Array.of(ordinal));
4338
+ view.writeCodes(ordinal);
4339
+ },
4340
+ writeCodes(ordinal) {
4341
+ shared()?.writeCodes(ordinal);
4342
+ },
4343
+ remove(docId) {
4344
+ const ordinal = store.getOrdinal(docId);
4345
+ if (ordinal !== void 0) shared()?.clearCodes(ordinal);
4346
+ },
4347
+ removeOrdinal(ordinal) {
4348
+ shared()?.clearCodes(ordinal);
4349
+ },
4350
+ hasOrdinal: (ordinal) => shared()?.holdsOrdinal(ordinal) ?? false,
4351
+ getLevels(docId) {
4352
+ const view = shared();
4353
+ const ordinal = store.getOrdinal(docId);
4354
+ if (view === null || ordinal === void 0 || !view.holdsOrdinal(ordinal)) return void 0;
4355
+ return unpackLevels(view.recordAt(ordinal), 0, dimension, bits);
4356
+ },
4357
+ recordOf(docId) {
4358
+ const view = shared();
4359
+ const ordinal = store.getOrdinal(docId);
4360
+ if (view === null || ordinal === void 0 || !view.holdsOrdinal(ordinal)) return void 0;
4361
+ return view.recordAt(ordinal);
4362
+ },
4363
+ recordAtOrdinal(ordinal) {
4364
+ const view = shared();
4365
+ if (view === null || !view.holdsOrdinal(ordinal)) return void 0;
4366
+ return view.recordAt(ordinal);
4367
+ },
4368
+ restoreRecord(docId, record) {
4369
+ requireShared().restoreRecord(ordinalOf(docId), record);
4370
+ },
4371
+ restoreRecordAtOrdinal(ordinal, record) {
4372
+ requireShared().restoreRecord(ordinal, record);
4373
+ },
4374
+ restoreCentroid(centroid) {
4375
+ requireShared().writeCentroid(centroid);
4376
+ },
4377
+ recalibrate(ordinals) {
4378
+ if (ordinals.length === 0) return;
4379
+ const view = requireShared();
4380
+ view.resetCodes();
4381
+ view.calibrateFrom(ordinals);
4382
+ view.writeCodesOf(ordinals);
4383
+ },
4384
+ beginRecoding(centroid) {
4385
+ requireShared().beginRecoding(centroid);
4386
+ },
4387
+ recode(ordinal) {
4388
+ shared()?.recode(ordinal);
4389
+ },
4390
+ finishRecoding: () => shared()?.finishRecoding() ?? false,
4391
+ abandonRecoding() {
4392
+ shared()?.abandonRecoding();
4393
+ },
4394
+ prepareQuery: (query) => shared()?.prepareQuery(query) ?? null,
4395
+ distanceFromPreparedByOrdinal: (prepared, ordinal) => shared()?.distanceFromPreparedByOrdinal(prepared, ordinal) ?? Number.POSITIVE_INFINITY,
4396
+ preparedDistance(prepared) {
4397
+ const view = shared();
4398
+ if (view === null) return () => Number.POSITIVE_INFINITY;
4399
+ return view.preparedDistance(prepared);
4400
+ },
4401
+ pairDistance() {
4402
+ const view = shared();
4403
+ if (view === null) return () => Number.POSITIVE_INFINITY;
4404
+ return view.pairDistance();
4405
+ },
4406
+ placementDistance: (vector) => shared()?.placementDistance(vector) ?? null,
4407
+ clear() {
4408
+ const view = shared();
4409
+ if (view === null) return;
4410
+ view.resetCodes();
4411
+ view.resetCalibration();
4412
+ }
4413
+ };
4414
+ }
4415
+
4416
+ export { GRAPH_RETIRED, IN_MEMORY, NATIVE_OK, NOTHING_STAGED, QUERY_STAGED, STORE_BLOCK_COUNT, STORE_CALIBRATION_GENERATION, STORE_CODE_BLOCK_COUNT, STORE_DOC_ID_BYTES, STORE_LIVE_COUNT, STORE_ORDINAL_LAYOUT_GENERATION, STORE_RELEASED_VECTORS_TO_DISK, STORE_REMOVED_COUNT, STORE_SLOTS, STORE_UNPLACED_COUNT, VECTOR_MEMORY_LIMITS, addToOrdinalFilter, arenaFloat32Distance, blocksGrowthBytes, centroidOfCentroidSums, computeCodeBlockLayout, cosineSimilarityWithMagnitudes, countOfCentroidSums, createCentroidSums, createHNSWIndex, createOrdinalFilter, createOsqQuantizer, createOsqScratch, createSharedVectorStoreHandles, createVectorBlock, defaultEfSearch, defaultRescoreDepth, detachNativeStore, dotProduct, ensureBlockSlots, euclideanDistance, fixedView, graphWithoutNodes, growBufferTo, grownByteLength, insertNode, magnitude, meanOfCentroidSums, nativeMetricCode, nativeScoresOf, nativeStoreFor, nativeStoreScratchBytes, nodeCountOf, noteFallback, openGraphState, openSharedQuantizer, openVectorBlock, ordinalFilterHas, ordinalFilterValues, osqBitsOf, osqDistance, osqEstimate, osqQuantize, releaseLocksHeldBy, removeFromOrdinalFilter, requireWholeNeighbourLists, resetStoreHeader, scalarQueryProducts, searchOrdinals, sharedMemoryAvailable, sharedVectorStoreBytes, shuffleInPlace, shuffleStep, slotByteOffset, squaredLengthsOfCentroidSums, threadedNativeCore, toDistance, toScore, tombstoneCountOf, topLayerInParts, unplacedChunkCount, withNativeThreadSlot };