@delali/narsil 0.3.1-next.154 → 0.3.1-next.155

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 (297) hide show
  1. package/README.md +1 -0
  2. package/dist/adapters/filesystem.mjs +2 -2
  3. package/dist/adapters/gcs.mjs +2 -2
  4. package/dist/adapters/indexeddb.mjs +1 -1
  5. package/dist/adapters/s3.mjs +2 -2
  6. package/dist/{analysis-change-4GCRVRBW.mjs → analysis-change-3TFIHT5E.mjs} +25 -25
  7. package/dist/{react-native/chunks/analysis-change-entry-TNLUC6IZ.mjs → analysis-change-entry-KA6JAO4X.mjs} +1 -1
  8. package/dist/archive-history-E4R73RWD.mjs +29 -0
  9. package/dist/browser/{analysis-change-IXWPXXJJ.mjs → analysis-change-TC36BU47.mjs} +29 -10
  10. package/dist/{analysis-change-entry-KYGSXW4K.mjs → browser/analysis-change-entry-4MH3CZXH.mjs} +1 -1
  11. package/dist/browser/archive-history-4IP5XQZY.mjs +29 -0
  12. package/dist/browser/{chunk-4PD752F6.mjs → chunk-23XRXIBW.mjs} +1 -1
  13. package/dist/browser/chunk-3UIEB4QF.mjs +63 -0
  14. package/dist/browser/{chunk-AJ4HHOVT.mjs → chunk-4EWUGZYZ.mjs} +4 -4
  15. package/dist/browser/{chunk-U2E3HMYW.mjs → chunk-4WCVMOU3.mjs} +1 -1
  16. package/dist/browser/{chunk-QXTPSX2H.mjs → chunk-56W3HQVJ.mjs} +7 -7
  17. package/dist/browser/{chunk-3U3QR5YP.mjs → chunk-5TCXK5SB.mjs} +2 -2
  18. package/dist/browser/chunk-7IGKGWSQ.mjs +79 -0
  19. package/dist/browser/{chunk-J433A7P3.mjs → chunk-A5KUEGDY.mjs} +5 -5
  20. package/dist/browser/{chunk-XRV72TJD.mjs → chunk-ABCXENU5.mjs} +2 -2
  21. package/dist/browser/chunk-ATEBYXP6.mjs +10 -0
  22. package/dist/{react-native/chunks/chunk-3XKMRNRE.mjs → browser/chunk-BXM5XOCA.mjs} +27 -10
  23. package/dist/browser/chunk-CJSYBQ4J.mjs +100 -0
  24. package/dist/{react-native/chunks/chunk-DCKMICOB.mjs → browser/chunk-EXQZGUQH.mjs} +1 -1
  25. package/dist/browser/{chunk-VCOYGVP7.mjs → chunk-FNIFPFB7.mjs} +3 -3
  26. package/dist/browser/{chunk-ML6HY7T7.mjs → chunk-G64Q6OJF.mjs} +1 -1
  27. package/dist/browser/chunk-GPPOG6PT.mjs +9 -0
  28. package/dist/browser/chunk-IKDKX5VQ.mjs +1007 -0
  29. package/dist/browser/{chunk-7TTWXUH6.mjs → chunk-JIJXMI42.mjs} +3 -2
  30. package/dist/browser/chunk-JVBP5EYO.mjs +1106 -0
  31. package/dist/browser/{chunk-2FX6ROWR.mjs → chunk-K4GCSZLI.mjs} +4 -4
  32. package/dist/browser/{chunk-EUNBNLKM.mjs → chunk-K5AXJBLL.mjs} +5 -5
  33. package/dist/browser/chunk-KKAMCABR.mjs +432 -0
  34. package/dist/browser/{chunk-7MEYN7V6.mjs → chunk-LFIWRCEA.mjs} +1 -9
  35. package/dist/browser/chunk-M3MQE2X7.mjs +151 -0
  36. package/dist/browser/{chunk-W5X2UFWQ.mjs → chunk-NLWTBAGB.mjs} +12 -3
  37. package/dist/browser/chunk-OEDCEZXJ.mjs +92 -0
  38. package/dist/browser/{chunk-G7LUMBSG.mjs → chunk-RND5CLX3.mjs} +28 -18
  39. package/dist/browser/{chunk-AYY6PIKL.mjs → chunk-RQBUNZQW.mjs} +2 -2
  40. package/dist/browser/{chunk-C4KS2QNF.mjs → chunk-RRO3EV4C.mjs} +9 -9
  41. package/dist/browser/{chunk-2SHAKW2Z.mjs → chunk-SMEQ36NT.mjs} +80 -134
  42. package/dist/{chunk-KUZKUONM.mjs → browser/chunk-SRMZXJSL.mjs} +1 -0
  43. package/dist/{react-native/chunks/chunk-3O57C2ZO.mjs → browser/chunk-SUDNZKPL.mjs} +2 -2
  44. package/dist/browser/{chunk-SJB6VIZG.mjs → chunk-U3JOQGWX.mjs} +1 -1
  45. package/dist/browser/{chunk-H6QFBMVE.mjs → chunk-XY4VVBBO.mjs} +3 -3
  46. package/dist/browser/{cold-module-VTUGDYNY.mjs → cold-module-NEUGHUPA.mjs} +18 -17
  47. package/dist/browser/durability-config-TIYKW6XX.mjs +89 -0
  48. package/dist/browser/{durability-wiring-GL4A7KDV.mjs → durability-wiring-CVGPRO6D.mjs} +46 -150
  49. package/dist/browser/engine-hooks-YXMPHZKZ.mjs +16 -0
  50. package/dist/browser/{import-YI5GNB4H.mjs → import-OM6QAW45.mjs} +10 -9
  51. package/dist/browser/{methods-LMUTS7OI.mjs → methods-KTQCSIN4.mjs} +3 -3
  52. package/dist/browser/{module-EC3FYW7N.mjs → module-3HNFCBJ5.mjs} +10 -9
  53. package/dist/browser/{module-M4JNBV6V.mjs → module-E33X4AOK.mjs} +14 -13
  54. package/dist/browser/operations-OJ3BXS55.mjs +5 -0
  55. package/dist/browser/{phrase-module-NPYUVX75.mjs → phrase-module-ZXXU7S3B.mjs} +1 -1
  56. package/dist/browser/{pool-WCGDZHJP.mjs → pool-3JLET5RO.mjs} +1 -1
  57. package/dist/browser/restore-hooks-IF2ZROUP.mjs +113 -0
  58. package/dist/browser/restore-queue-ZBS7CJA6.mjs +55 -0
  59. package/dist/browser/{retention-6QMRS5VK.mjs → retention-ASWUHAYI.mjs} +16 -14
  60. package/dist/browser/{runtime-3EW2AGB6.mjs → runtime-OTAGEFQW.mjs} +1 -1
  61. package/dist/browser/{snapshot-DN3CRVUQ.mjs → snapshot-PN7ZW6Q4.mjs} +65 -31
  62. package/dist/browser/start-ZIRB7MQ2.mjs +24 -0
  63. package/dist/browser/{write-WAWAFGKQ.mjs → write-HMDYCTV2.mjs} +14 -13
  64. package/dist/{capabilities-B3Brs1Rc.d.ts → capabilities-Da4ywVVl.d.ts} +35 -3
  65. package/dist/{chunk-K4B7OH65.mjs → chunk-2MANFZJ3.mjs} +2 -2
  66. package/dist/{chunk-O43Z4MS6.mjs → chunk-2NE7VCYE.mjs} +2 -2
  67. package/dist/{chunk-3Y3J4V2R.mjs → chunk-3C7MTCCI.mjs} +1 -1
  68. package/dist/{chunk-RVKFI3A2.mjs → chunk-3HB3PEC6.mjs} +11 -8
  69. package/dist/chunk-3HHQLEEQ.mjs +1092 -0
  70. package/dist/{chunk-HVYICDY3.mjs → chunk-3OH67G7R.mjs} +1 -1
  71. package/dist/{chunk-QKZHLADX.mjs → chunk-3R4UZWQG.mjs} +26 -17
  72. package/dist/{chunk-ZIFJXCKA.mjs → chunk-4BYWRYPM.mjs} +1 -1
  73. package/dist/{chunk-VMXSVVXG.mjs → chunk-4KRC2APY.mjs} +5 -5
  74. package/dist/{chunk-IQ4R2I6N.mjs → chunk-5FLRXF73.mjs} +5 -1
  75. package/dist/chunk-5FVU47TB.mjs +20 -0
  76. package/dist/chunk-5YNRKI2C.mjs +81 -0
  77. package/dist/chunk-6L3XJXGU.mjs +470 -0
  78. package/dist/{chunk-C6ASEFZI.mjs → chunk-6OEGXLBX.mjs} +1 -1
  79. package/dist/{browser/chunk-SIR3LMTV.mjs → chunk-6RC4K46J.mjs} +2 -2
  80. package/dist/{chunk-466PYR4P.mjs → chunk-AILVAOLX.mjs} +2 -2
  81. package/dist/chunk-BDRIVYOK.mjs +72 -0
  82. package/dist/{chunk-TRISKD4Q.mjs → chunk-BY6ZDGYS.mjs} +2 -2
  83. package/dist/{chunk-NZMAXR2S.mjs → chunk-DCNKVEDB.mjs} +121 -10
  84. package/dist/{chunk-C73JLU4E.mjs → chunk-DIBHI6W5.mjs} +2 -2
  85. package/dist/{chunk-VREK3MYG.mjs → chunk-DUOLGBTE.mjs} +3 -3
  86. package/dist/{chunk-USR7FQRZ.mjs → chunk-GH4WQ22V.mjs} +2 -2
  87. package/dist/chunk-GQPTDQCL.mjs +54 -0
  88. package/dist/chunk-H4KHPLHJ.mjs +382 -0
  89. package/dist/{chunk-K7KLL263.mjs → chunk-H6VIGE62.mjs} +12 -12
  90. package/dist/{chunk-YROWNOMH.mjs → chunk-HJJI72X7.mjs} +1 -1
  91. package/dist/{chunk-37IVJLP6.mjs → chunk-HNVVEQP7.mjs} +1 -1
  92. package/dist/{chunk-PFKM7VNZ.mjs → chunk-HW5TNXBO.mjs} +28 -10
  93. package/dist/{chunk-3A5KI24C.mjs → chunk-I54T6CQP.mjs} +1 -1
  94. package/dist/{chunk-YBJDOA2T.mjs → chunk-INJYO3BU.mjs} +1 -1
  95. package/dist/{chunk-KCUIFVVW.mjs → chunk-J4IWBIVY.mjs} +9 -9
  96. package/dist/{chunk-AO2FAYIZ.mjs → chunk-K4ZIGL2O.mjs} +1 -1
  97. package/dist/{chunk-7NTMUDC5.mjs → chunk-KBDBPVLN.mjs} +5 -5
  98. package/dist/{chunk-JFNKIBDT.mjs → chunk-KBKEXQZA.mjs} +2 -2
  99. package/dist/{chunk-REL7LFLR.mjs → chunk-KFLK5NA2.mjs} +3 -3
  100. package/dist/{chunk-WCBA2LVZ.mjs → chunk-KHKIRS5D.mjs} +1 -1
  101. package/dist/{chunk-7QUGDVFC.mjs → chunk-KJ3RTZDJ.mjs} +1 -1
  102. package/dist/{chunk-LSM2YTKM.mjs → chunk-KMUNHGBQ.mjs} +81 -139
  103. package/dist/chunk-KP7TYHWT.mjs +92 -0
  104. package/dist/{chunk-MMBEANB2.mjs → chunk-KPIV4RUC.mjs} +3 -3
  105. package/dist/{chunk-62G63NMH.mjs → chunk-KXCVQWOR.mjs} +4 -4
  106. package/dist/{chunk-TV2L3BN7.mjs → chunk-L7ZD4L3K.mjs} +263 -263
  107. package/dist/{chunk-OKX2BBNW.mjs → chunk-LC3FDWTX.mjs} +2 -2
  108. package/dist/{chunk-URPCYQVJ.mjs → chunk-LHKOSI2J.mjs} +1 -1
  109. package/dist/chunk-LNGE7SLY.mjs +151 -0
  110. package/dist/{react-native/chunks/chunk-CUNGPR5R.mjs → chunk-MP7ABPTD.mjs} +1 -0
  111. package/dist/chunk-MT2NRTA6.mjs +216 -0
  112. package/dist/{chunk-Q3TCTQCC.mjs → chunk-NF4DT3IH.mjs} +1 -1
  113. package/dist/{chunk-3OFU3NJT.mjs → chunk-OIJ64LGT.mjs} +1 -1
  114. package/dist/{chunk-4HWZV5MK.mjs → chunk-PCRA7HTI.mjs} +5 -5
  115. package/dist/chunk-PHQNACDT.mjs +63 -0
  116. package/dist/{chunk-Q7MS6P2J.mjs → chunk-Q622CTMC.mjs} +2 -2
  117. package/dist/{chunk-HVKXNBFL.mjs → chunk-Q7G3BV3Y.mjs} +1 -1
  118. package/dist/{chunk-IBNLW3AV.mjs → chunk-QH3YIQP4.mjs} +20 -6
  119. package/dist/{chunk-B7S6YEBP.mjs → chunk-QJVOLKU6.mjs} +12 -3
  120. package/dist/chunk-QUJHMPTP.mjs +30 -0
  121. package/dist/{chunk-7HCZWC7B.mjs → chunk-RSVULW4Q.mjs} +49 -7
  122. package/dist/{chunk-YPUEV6ZC.mjs → chunk-RVGRFOGN.mjs} +1 -1
  123. package/dist/{chunk-JCLTESFN.mjs → chunk-RZWWB2XR.mjs} +1 -1
  124. package/dist/{chunk-X6NSJYQ3.mjs → chunk-S2EQU2GE.mjs} +13 -13
  125. package/dist/chunk-S5LN746F.mjs +10 -0
  126. package/dist/{chunk-MMEMEUOO.mjs → chunk-SG3JBNHY.mjs} +2 -2
  127. package/dist/{chunk-LKUE5GXS.mjs → chunk-SMVH6JBE.mjs} +2 -2
  128. package/dist/{chunk-IJU7LL7D.mjs → chunk-SY3EWKQY.mjs} +5 -5
  129. package/dist/{chunk-CP5RRPJR.mjs → chunk-TQXHPFWT.mjs} +1 -1
  130. package/dist/{chunk-5KGYQT6W.mjs → chunk-U4BS74YN.mjs} +26 -9
  131. package/dist/{chunk-S2N75OTN.mjs → chunk-UHICS3JI.mjs} +1 -1
  132. package/dist/chunk-UZRZKNNZ.mjs +101 -0
  133. package/dist/{chunk-DMQPFLGI.mjs → chunk-VGF7CFRP.mjs} +5 -5
  134. package/dist/chunk-VHBNF4MA.mjs +543 -0
  135. package/dist/{chunk-BPSKLCS7.mjs → chunk-VQEUR6T6.mjs} +3 -3
  136. package/dist/{chunk-Z3TNWCI5.mjs → chunk-WQRN64Q6.mjs} +2 -2
  137. package/dist/{chunk-LTUGDMV4.mjs → chunk-WZ7YV54N.mjs} +1 -1
  138. package/dist/{chunk-NTZOXVUZ.mjs → chunk-YEDIDCR7.mjs} +3 -3
  139. package/dist/{chunk-PGVPE7GJ.mjs → chunk-YEKL4JBK.mjs} +1 -1
  140. package/dist/{chunk-5FUOVJKG.mjs → chunk-Z2BE2MAM.mjs} +13 -13
  141. package/dist/{chunk-HWEDFLB7.mjs → chunk-ZAUJFLEN.mjs} +10 -10
  142. package/dist/{chunk-MLHNSAPK.mjs → chunk-ZHCUZZE5.mjs} +2 -2
  143. package/dist/{chunk-ECX4SGX6.mjs → chunk-ZTW5WTGG.mjs} +9 -9
  144. package/dist/{client-Df76GNKm.d.ts → client-DRv7UVeY.d.ts} +123 -11
  145. package/dist/client.d.ts +9 -8
  146. package/dist/client.mjs +65 -3
  147. package/dist/{cold-module-YLWMTTZG.mjs → cold-module-7XXCEGTZ.mjs} +35 -35
  148. package/dist/{config-B8DP2VwR.d.ts → config-CoSig5Ox.d.ts} +22 -3
  149. package/dist/{config-05G_hy-1.d.ts → config-DMwffLKn.d.ts} +20 -5
  150. package/dist/{direct-executor-FI5GJHWX.mjs → direct-executor-G5TK7NLA.mjs} +16 -16
  151. package/dist/distribution/coordinator/etcd.d.ts +2 -2
  152. package/dist/distribution/coordinator/etcd.mjs +1 -1
  153. package/dist/distribution/coordinator/in-memory.d.ts +2 -2
  154. package/dist/distribution/transport/grpc.mjs +4 -4
  155. package/dist/distribution/transport/in-memory.mjs +2 -2
  156. package/dist/distribution/transport/tcp.mjs +3 -3
  157. package/dist/distribution.d.ts +8 -8
  158. package/dist/distribution.mjs +6788 -5934
  159. package/dist/durability-config-GE4U7MCZ.mjs +89 -0
  160. package/dist/{durability-wiring-RHLXDIVF.mjs → durability-wiring-NCKUC5EO.mjs} +71 -175
  161. package/dist/embeddings/openai.mjs +2 -2
  162. package/dist/{sync-uXR7MmM7.d.ts → engine-CFxPYqEk.d.ts} +702 -2
  163. package/dist/engine-hooks-DHCMOADY.mjs +47 -0
  164. package/dist/{import-SRBNC6TE.mjs → import-NBFXFCSG.mjs} +26 -26
  165. package/dist/{index-nRqDM6uS.d.ts → index-C4AmUnl1.d.ts} +1 -1
  166. package/dist/index.browser.mjs +7 -6
  167. package/dist/index.d.ts +7 -7
  168. package/dist/index.mjs +23 -23
  169. package/dist/index.react-native.mjs +13 -12
  170. package/dist/{long-query-thread-EMYTGSJP.mjs → long-query-thread-J4DVRM4A.mjs} +25 -24
  171. package/dist/{methods-ABM5ELBR.mjs → methods-7IOINERF.mjs} +3 -3
  172. package/dist/{module-MG7JMV2U.mjs → module-DWKPBSMN.mjs} +31 -31
  173. package/dist/{module-3OMJZUFT.mjs → module-UN76TF7I.mjs} +11 -11
  174. package/dist/operations-G5EP6JPX.mjs +5 -0
  175. package/dist/{paged-partition-archive-UOLHOEZJ.mjs → paged-partition-archive-DPNWVSK2.mjs} +38 -34
  176. package/dist/partial-words.mjs +7 -7
  177. package/dist/pattern.mjs +3 -3
  178. package/dist/persistence/durability/checkpoint-worker.d.ts +8 -1
  179. package/dist/persistence/durability/checkpoint-worker.mjs +32 -32
  180. package/dist/{phrase-module-O3MDRQXP.mjs → phrase-module-ZI4WSQVH.mjs} +1 -1
  181. package/dist/{pool-CQXYPFLG.mjs → pool-Y4A46T36.mjs} +1 -1
  182. package/dist/query-suggestions/merge-worker.d.ts +55 -0
  183. package/dist/query-suggestions/merge-worker.mjs +81 -0
  184. package/dist/query-suggestions-CBYkrQ5T.d.ts +311 -0
  185. package/dist/query-suggestions.browser.mjs +246 -0
  186. package/dist/query-suggestions.d.ts +20 -0
  187. package/dist/query-suggestions.mjs +62 -0
  188. package/dist/react-native/chunks/{analysis-change-EAYO3Y6Q.mjs → analysis-change-6BPDPH6K.mjs} +36 -17
  189. package/dist/{browser/analysis-change-entry-FCSHFQSS.mjs → react-native/chunks/analysis-change-entry-JLL42PH6.mjs} +1 -1
  190. package/dist/react-native/chunks/archive-history-MJKUQLSU.mjs +29 -0
  191. package/dist/react-native/chunks/{chunk-O32RB3BL.mjs → chunk-3JIBF3XT.mjs} +3 -3
  192. package/dist/react-native/chunks/{chunk-75VWIY35.mjs → chunk-4DII3ZSM.mjs} +1 -1
  193. package/dist/react-native/chunks/{chunk-NKIZX26F.mjs → chunk-4J74WHQM.mjs} +2 -2
  194. package/dist/{browser/chunk-TTD27VXX.mjs → react-native/chunks/chunk-4LCEN2IU.mjs} +27 -10
  195. package/dist/react-native/chunks/{chunk-AUT7PMA4.mjs → chunk-4Y53T4YE.mjs} +5 -5
  196. package/dist/react-native/chunks/chunk-5CQX3XXV.mjs +17 -0
  197. package/dist/react-native/chunks/chunk-6VBXNFLE.mjs +9 -0
  198. package/dist/react-native/chunks/{chunk-SJWDBRNP.mjs → chunk-6WA3C3DV.mjs} +1 -1
  199. package/dist/react-native/chunks/chunk-7GRCBBME.mjs +63 -0
  200. package/dist/react-native/chunks/{chunk-7GWPJDOA.mjs → chunk-7PXRWYUU.mjs} +1 -1
  201. package/dist/react-native/chunks/{chunk-GFYFQLJD.mjs → chunk-ALOIBXWN.mjs} +2 -2
  202. package/dist/react-native/chunks/{chunk-VQSKZ3EZ.mjs → chunk-C3PTWYKT.mjs} +8 -8
  203. package/dist/react-native/chunks/{chunk-OQQVRWZ2.mjs → chunk-C5G6TJ7A.mjs} +1 -1
  204. package/dist/react-native/chunks/{chunk-RVAEBN74.mjs → chunk-CKNRFDTZ.mjs} +1 -1
  205. package/dist/react-native/chunks/{chunk-UWDNZDWK.mjs → chunk-CUH5F57W.mjs} +6 -6
  206. package/dist/react-native/chunks/chunk-DIYDOIM4.mjs +10 -0
  207. package/dist/react-native/chunks/{chunk-YBEW6YTR.mjs → chunk-DVDX4SDR.mjs} +2 -2
  208. package/dist/{browser/chunk-MYITLCHT.mjs → react-native/chunks/chunk-DZ7H7D2C.mjs} +1 -0
  209. package/dist/react-native/chunks/{chunk-RYMDHGL4.mjs → chunk-EIOYM5TO.mjs} +5 -5
  210. package/dist/react-native/chunks/chunk-GIZTJXYW.mjs +1007 -0
  211. package/dist/react-native/chunks/{chunk-NP7DD4G5.mjs → chunk-GJFXPHXI.mjs} +5 -5
  212. package/dist/react-native/chunks/chunk-H3MDVPBO.mjs +151 -0
  213. package/dist/react-native/chunks/{chunk-63YSZOXB.mjs → chunk-HUOYZJ3Q.mjs} +88 -142
  214. package/dist/react-native/chunks/{chunk-DH3TVWWK.mjs → chunk-IYNT75CE.mjs} +12 -3
  215. package/dist/react-native/chunks/{chunk-NCJS3WIY.mjs → chunk-JLIIWJUV.mjs} +1 -1
  216. package/dist/react-native/chunks/chunk-KJIG57UD.mjs +1106 -0
  217. package/dist/react-native/chunks/{chunk-JX2RIAXH.mjs → chunk-KTTAU63H.mjs} +5 -5
  218. package/dist/{browser/chunk-ZPTA75ZC.mjs → react-native/chunks/chunk-LZSLNSUN.mjs} +1 -1
  219. package/dist/react-native/chunks/{chunk-ZBSWRSBG.mjs → chunk-MJDQU5MW.mjs} +1 -1
  220. package/dist/react-native/chunks/{chunk-IOY2SM2P.mjs → chunk-MVXELMJX.mjs} +1 -1
  221. package/dist/react-native/chunks/{chunk-2CGSQM4O.mjs → chunk-N2WUOWLL.mjs} +12 -12
  222. package/dist/react-native/chunks/{chunk-YHKZSLOC.mjs → chunk-OL3WDV53.mjs} +5 -5
  223. package/dist/react-native/chunks/{chunk-VVFYOADG.mjs → chunk-PEHS2UO3.mjs} +6 -6
  224. package/dist/react-native/chunks/chunk-PJHAB5ZY.mjs +79 -0
  225. package/dist/react-native/chunks/chunk-QJXTNPLQ.mjs +100 -0
  226. package/dist/react-native/chunks/{chunk-EH5CFV6Z.mjs → chunk-RDWG33MM.mjs} +2 -2
  227. package/dist/react-native/chunks/{chunk-ZYDKJXQJ.mjs → chunk-SUPXRTMD.mjs} +1 -1
  228. package/dist/react-native/chunks/chunk-TYIRMPD3.mjs +432 -0
  229. package/dist/react-native/chunks/{chunk-JPAJWO6D.mjs → chunk-U4KSKA4Z.mjs} +1 -1
  230. package/dist/react-native/chunks/{chunk-I5PGWQJF.mjs → chunk-VXGE4ZXL.mjs} +31 -22
  231. package/dist/{chunk-DVWYODEB.mjs → react-native/chunks/chunk-WBYQRHDY.mjs} +2 -2
  232. package/dist/react-native/chunks/{chunk-C2H2QQFW.mjs → chunk-YH7QY657.mjs} +3 -3
  233. package/dist/react-native/chunks/chunk-ZZT7CEN7.mjs +92 -0
  234. package/dist/react-native/chunks/{cold-module-TJL6AFAD.mjs → cold-module-5PHF6DZR.mjs} +24 -23
  235. package/dist/react-native/chunks/durability-config-QXVZEO7T.mjs +89 -0
  236. package/dist/react-native/chunks/{durability-wiring-RRDYJLLM.mjs → durability-wiring-KO565FZC.mjs} +52 -156
  237. package/dist/react-native/chunks/engine-hooks-MVRRHH6B.mjs +28 -0
  238. package/dist/react-native/chunks/gzip.react-native-HLEPDKZ3.mjs +3 -0
  239. package/dist/react-native/chunks/import-YVZA4V4V.mjs +30 -0
  240. package/dist/react-native/chunks/{methods-X6F36SJE.mjs → methods-6W5U4C3H.mjs} +3 -3
  241. package/dist/react-native/chunks/{module-S4DKSWMF.mjs → module-H5MVPSP2.mjs} +11 -10
  242. package/dist/react-native/chunks/{module-QT5HZAY4.mjs → module-ZL5ZG6YB.mjs} +20 -19
  243. package/dist/react-native/chunks/operations-GNRMD6EX.mjs +5 -0
  244. package/dist/react-native/chunks/{phrase-module-JZ37TB7N.mjs → phrase-module-RTVKEWJT.mjs} +1 -1
  245. package/dist/react-native/chunks/{pool-RTGXJXPU.mjs → pool-JR3NJCAF.mjs} +1 -1
  246. package/dist/react-native/chunks/restore-hooks-SILFPU6B.mjs +125 -0
  247. package/dist/react-native/chunks/restore-queue-HSYP2AEC.mjs +55 -0
  248. package/dist/react-native/chunks/{retention-PPCTP2VY.mjs → retention-7I7UAWYK.mjs} +21 -19
  249. package/dist/react-native/chunks/{runtime-KUATOLYB.mjs → runtime-R5E3TB3J.mjs} +1 -1
  250. package/dist/react-native/chunks/{snapshot-ZBIEOZTZ.mjs → snapshot-EZYB6SLF.mjs} +70 -36
  251. package/dist/react-native/chunks/start-SHVZ72I5.mjs +36 -0
  252. package/dist/react-native/chunks/{write-2W4FTX7J.mjs → write-JUESXHDL.mjs} +20 -19
  253. package/dist/react-native/partial-words.mjs +7 -6
  254. package/dist/react-native/pattern.mjs +3 -3
  255. package/dist/react-native/query-suggestions.mjs +258 -0
  256. package/dist/react-native/sync.mjs +76 -13
  257. package/dist/react-native/synonyms.mjs +4 -4
  258. package/dist/react.d.ts +11 -11
  259. package/dist/react.mjs +1 -0
  260. package/dist/{rerank-IZOB6NII.mjs → rerank-MQ37IYRV.mjs} +4 -4
  261. package/dist/rerankers/http.mjs +2 -2
  262. package/dist/restore-hooks-HZ3ILAMO.mjs +146 -0
  263. package/dist/restore-queue-JVGENGGE.mjs +55 -0
  264. package/dist/{retention-55WA7GPU.mjs → retention-HROPV2QU.mjs} +33 -32
  265. package/dist/{runtime-UZOC3NPY.mjs → runtime-COIUORCN.mjs} +5 -5
  266. package/dist/{schema-DDauvDOv.d.ts → schema-Jwt928nJ.d.ts} +1 -1
  267. package/dist/server.d.ts +108 -12
  268. package/dist/server.mjs +293 -46
  269. package/dist/{snapshot-NFX2EYN2.mjs → snapshot-4HS2G2UM.mjs} +85 -54
  270. package/dist/start-UTWHGH42.mjs +59 -0
  271. package/dist/sync/filesystem.d.ts +4 -4
  272. package/dist/sync/filesystem.mjs +4 -4
  273. package/dist/sync/indexeddb.d.ts +4 -4
  274. package/dist/sync/indexeddb.mjs +2 -2
  275. package/dist/sync.browser.mjs +74 -11
  276. package/dist/sync.d.ts +5 -5
  277. package/dist/sync.mjs +76 -14
  278. package/dist/synonyms.mjs +5 -5
  279. package/dist/{tasks-BShx8pdK.d.ts → tasks-qrJojgST.d.ts} +13 -5
  280. package/dist/{thread-P2G2FWM2.mjs → thread-6MOXY2KD.mjs} +24 -23
  281. package/dist/{types-SnO7_tAI.d.ts → types-BZCGjfLn.d.ts} +2 -3
  282. package/dist/vector/search-worker.d.ts +1 -1
  283. package/dist/vector/search-worker.mjs +5 -5
  284. package/dist/workers/entry.mjs +8 -8
  285. package/dist/{write-FGB62BHQ.mjs → write-GTTWWRRG.mjs} +30 -30
  286. package/openapi.json +798 -7
  287. package/package.json +24 -7
  288. package/dist/browser/operations-WVYLF72Q.mjs +0 -5
  289. package/dist/browser/restore-queue-TYIGDL5L.mjs +0 -41
  290. package/dist/engine-DYu-e0oR.d.ts +0 -674
  291. package/dist/operations-PGOAY2KS.mjs +0 -5
  292. package/dist/react-native/chunks/chunk-TJMSKBEM.mjs +0 -25
  293. package/dist/react-native/chunks/gzip.react-native-DNB5YNFA.mjs +0 -3
  294. package/dist/react-native/chunks/import-JYBMEJPQ.mjs +0 -29
  295. package/dist/react-native/chunks/operations-RYKTSZQA.mjs +0 -5
  296. package/dist/react-native/chunks/restore-queue-ZBVIKJWI.mjs +0 -41
  297. package/dist/restore-queue-COI2GSSK.mjs +0 -41
@@ -0,0 +1,1092 @@
1
+ import { DayStore, RestoredHistory, dayOf } from './chunk-VHBNF4MA.mjs';
2
+ import { historyFilesOf, buildRecordKey, listHistoryFolder, historyFileOf, removeHistoryFolder, removeRestoreLeftovers } from './chunk-KP7TYHWT.mjs';
3
+ import { SHA256_BLOCK_BYTES, sha256, SHA256_INITIAL_STATE, compressSha256Block, paddedSha256Message, sha256Digest } from './chunk-LNGE7SLY.mjs';
4
+ import { resolveSetting, parseSchedule, nextMatchAfter, latestMatchAtOrBefore } from './chunk-H4KHPLHJ.mjs';
5
+ import { oneSyncChangeAtATime } from './chunk-V6EUBAIT.mjs';
6
+ import { spawnNodeWorker } from './chunk-BEZH7VEH.mjs';
7
+ import { Merger } from './chunk-BDRIVYOK.mjs';
8
+ import { sameBytes } from './chunk-6L3XJXGU.mjs';
9
+ import { isPerson, isGroup, acceptedPhrase } from './chunk-GQPTDQCL.mjs';
10
+ import { SUGGESTIONS_INDEX_INFIX, BUILD_WRITE_BATCH, MIN_KEY_BYTES, DAY_MS, BYTES_PER_MIB, MS_PER_SECOND, SCRAMBLED_ID_BYTES, KEY_FINGERPRINT_LABEL, KEY_FINGERPRINT_BYTES, HISTORY_SWEEP_INTERVAL_MS, SCHEDULE_OFF, MAX_TIMER_DELAY_MS, BUILD_LIST_PAGE } from './chunk-5YNRKI2C.mjs';
11
+ import { emitEngineEvent, emitWriteEventAfter, createEngineIndex, packEnvelopeWithChecksum } from './chunk-KMUNHGBQ.mjs';
12
+ import { resolveWorkerEntry } from './chunk-LHKOSI2J.mjs';
13
+ import { openChecksummedEnvelope } from './chunk-3OH67G7R.mjs';
14
+ import { crc32 } from './chunk-7GXJUJ24.mjs';
15
+ import { withOpenIndex } from './chunk-Y4WPNHMC.mjs';
16
+ import { NarsilError, ErrorCodes } from './chunk-MP7ABPTD.mjs';
17
+ import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
18
+ import { encode, decode } from '@msgpack/msgpack';
19
+
20
+ var BUILD_RECORD_FORMAT_VERSION = 1;
21
+ var SOURCES_DIGEST_BYTES = 32;
22
+ var TRIGGERS = /* @__PURE__ */ new Set(["schedule", "missed", "caller"]);
23
+ var STATUSES = /* @__PURE__ */ new Set(["running", "completed", "failed", "skipped"]);
24
+ var WHAT = "query suggestions build record";
25
+ function sourcesDigestOf(sources) {
26
+ const sorted = [...sources].sort(
27
+ (left, right) => compareCodePoints(left.nodeId, right.nodeId) || compareCodePoints(left.key, right.key)
28
+ );
29
+ return sha256(encode(sorted.map((source) => [source.nodeId, source.key, source.savedAt])));
30
+ }
31
+ function isCount(value) {
32
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
33
+ }
34
+ function buildRecordToWire(record) {
35
+ return encode({
36
+ buildId: record.buildId,
37
+ trigger: record.trigger,
38
+ status: record.status,
39
+ startedAt: record.startedAt,
40
+ ...record.endedAt !== void 0 ? { endedAt: record.endedAt } : {},
41
+ published: record.published,
42
+ removed: record.removed,
43
+ discarded: record.discarded,
44
+ sourcesDigest: record.sourcesDigest,
45
+ ...record.errorCode !== void 0 ? { errorCode: record.errorCode } : {}
46
+ });
47
+ }
48
+ function buildRecordFromWire(bytes) {
49
+ let raw;
50
+ try {
51
+ raw = decode(bytes);
52
+ } catch {
53
+ return null;
54
+ }
55
+ if (raw === null || typeof raw !== "object" || Array.isArray(raw)) return null;
56
+ const value = raw;
57
+ const { buildId, trigger, status, startedAt, endedAt, published, removed, discarded, sourcesDigest, errorCode } = value;
58
+ if (typeof buildId !== "string" || typeof trigger !== "string" || !TRIGGERS.has(trigger)) return null;
59
+ if (typeof status !== "string" || !STATUSES.has(status)) return null;
60
+ if (!isCount(startedAt) || !isCount(published) || !isCount(removed) || !isCount(discarded)) return null;
61
+ if (endedAt !== void 0 && !isCount(endedAt)) return null;
62
+ if (errorCode !== void 0 && typeof errorCode !== "string") return null;
63
+ if (!(sourcesDigest instanceof Uint8Array) || sourcesDigest.byteLength !== SOURCES_DIGEST_BYTES) return null;
64
+ return {
65
+ buildId,
66
+ trigger,
67
+ status,
68
+ startedAt,
69
+ ...endedAt !== void 0 ? { endedAt } : {},
70
+ published,
71
+ removed,
72
+ discarded,
73
+ sourcesDigest: new Uint8Array(sourcesDigest),
74
+ ...errorCode !== void 0 ? { errorCode } : {}
75
+ };
76
+ }
77
+ function encodeBuildRecordFile(record) {
78
+ const payload = buildRecordToWire(record);
79
+ const packed = packEnvelopeWithChecksum([payload], BUILD_RECORD_FORMAT_VERSION, WHAT, crc32(payload));
80
+ const bytes = new Uint8Array(packed.byteLength);
81
+ let offset = 0;
82
+ for (const chunk of packed.chunks) {
83
+ bytes.set(chunk, offset);
84
+ offset += chunk.byteLength;
85
+ }
86
+ return bytes;
87
+ }
88
+ async function decodeBuildRecordFile(bytes) {
89
+ try {
90
+ const { payload } = await openChecksummedEnvelope(bytes, {
91
+ envelopeFormatVersion: BUILD_RECORD_FORMAT_VERSION,
92
+ what: WHAT,
93
+ allowsCompression: false
94
+ });
95
+ return buildRecordFromWire(payload);
96
+ } catch {
97
+ return null;
98
+ }
99
+ }
100
+ function publicBuildOf(record) {
101
+ return {
102
+ buildId: record.buildId,
103
+ trigger: record.trigger,
104
+ status: record.status,
105
+ startedAt: record.startedAt,
106
+ endedAt: record.endedAt ?? null,
107
+ published: record.published,
108
+ removed: record.removed,
109
+ discarded: record.discarded,
110
+ errorCode: record.errorCode ?? null
111
+ };
112
+ }
113
+
114
+ // src/query-suggestions/local-host.ts
115
+ var SUGGESTIONS_SCHEMA = { query: "string", count: "number", origin: "enum" };
116
+ async function* noRemoteHistory() {
117
+ }
118
+ function persistState(core, indexName, next) {
119
+ const entry = core.requireIndex(indexName);
120
+ const previous = entry.querySuggestions;
121
+ entry.querySuggestions = next;
122
+ const persisted = core.durability?.manager.persistMetadata(indexName) ?? Promise.resolve();
123
+ return persisted.then(
124
+ () => next,
125
+ (error) => {
126
+ if (core.indexRegistry.get(indexName) === entry) entry.querySuggestions = previous;
127
+ throw error;
128
+ }
129
+ );
130
+ }
131
+ function createLocalHost(core, engine, config) {
132
+ const files = historyFilesOf(core.durability?.manager.historyStorage?.() ?? null);
133
+ return {
134
+ engine,
135
+ nodeId: "",
136
+ files,
137
+ clustered: false,
138
+ view(indexName) {
139
+ const entry = core.indexRegistry.get(indexName);
140
+ if (entry === void 0) return null;
141
+ return {
142
+ state: entry.querySuggestions,
143
+ schema: entry.config.schema,
144
+ language: entry.language.name,
145
+ indexUuid: entry.indexUuid,
146
+ hasSync: entry.sync !== null
147
+ };
148
+ },
149
+ indexNames: () => [...core.indexRegistry.keys()],
150
+ updateState(indexName, change) {
151
+ return withOpenIndex(
152
+ core,
153
+ indexName,
154
+ () => oneSyncChangeAtATime(
155
+ core.indexRegistry,
156
+ indexName,
157
+ () => persistState(core, indexName, change(core.requireIndex(indexName).querySuggestions))
158
+ )
159
+ );
160
+ },
161
+ async createSuggestionsIndex(name, source, language) {
162
+ const indexConfig = {
163
+ schema: { ...SUGGESTIONS_SCHEMA },
164
+ language,
165
+ stopWords: /* @__PURE__ */ new Set(),
166
+ partitions: { maxPartitions: 1 }
167
+ };
168
+ await emitWriteEventAfter(
169
+ core,
170
+ name,
171
+ createEngineIndex(core, config, name, indexConfig, {
172
+ appliesCreationLimits: true,
173
+ querySuggestions: { querySuggestionsSource: source }
174
+ })
175
+ );
176
+ },
177
+ remoteHistory: noRemoteHistory,
178
+ async readBuildRecord(indexName) {
179
+ const bytes = await files.read(buildRecordKey(indexName));
180
+ if (bytes === null || bytes === "too-long") return null;
181
+ return decodeBuildRecordFile(bytes);
182
+ },
183
+ async writeBuildRecord(indexName, record) {
184
+ await files.write(buildRecordKey(indexName), encodeBuildRecordFile(record));
185
+ },
186
+ mayBuild: () => true,
187
+ emit(name, payload) {
188
+ if (emitEngineEvent(core.eventHandlers, name, payload) > 0 || name !== "querySuggestionsWarning") return;
189
+ console.warn("Popular query suggestions warning:", payload);
190
+ }
191
+ };
192
+ }
193
+
194
+ // src/query-suggestions/merge-worker-entry.ts
195
+ function resolveMergeWorkerEntry() {
196
+ return resolveWorkerEntry(import.meta.url, /\/src\/query-suggestions\/[^/]+$/, "query-suggestions/merge-worker.mjs");
197
+ }
198
+
199
+ // src/query-suggestions/merge-run.ts
200
+ function mergeFailed(message) {
201
+ return new NarsilError(ErrorCodes.WORKER_ACTION_FAILED, `The query suggestions merge failed: ${message}`);
202
+ }
203
+ function ownedBuffer(bytes) {
204
+ if (bytes.byteOffset === 0 && bytes.byteLength === bytes.buffer.byteLength && bytes.buffer instanceof ArrayBuffer) {
205
+ return bytes.buffer;
206
+ }
207
+ return bytes.slice().buffer;
208
+ }
209
+ function inThreadSession(settings) {
210
+ const merger = new Merger(settings);
211
+ return {
212
+ addFile: (input) => merger.addFile(input.bytes, input.day),
213
+ finish: async () => merger.result(),
214
+ abandon() {
215
+ }
216
+ };
217
+ }
218
+ function workerSession(worker, settings) {
219
+ let nextId = 0;
220
+ let failure = null;
221
+ const waiting = /* @__PURE__ */ new Map();
222
+ let finishing = null;
223
+ function failAll(error) {
224
+ if (failure !== null) return;
225
+ failure = error;
226
+ for (const pending of waiting.values()) pending.reject(error);
227
+ waiting.clear();
228
+ finishing?.reject(error);
229
+ finishing = null;
230
+ void Promise.resolve(worker.terminate()).catch(() => void 0);
231
+ }
232
+ worker.on("message", (raw) => {
233
+ const reply = raw;
234
+ if (reply.type === "file") {
235
+ const pending = waiting.get(reply.id);
236
+ waiting.delete(reply.id);
237
+ pending?.resolve(reply.outcome);
238
+ } else if (reply.type === "result") {
239
+ finishing?.resolve(reply.result);
240
+ finishing = null;
241
+ void Promise.resolve(worker.terminate()).catch(() => void 0);
242
+ } else failAll(mergeFailed(reply.message));
243
+ });
244
+ worker.on("error", (error) => failAll(mergeFailed(error instanceof Error ? error.message : String(error))));
245
+ worker.on("exit", () => failAll(mergeFailed("the merge worker stopped")));
246
+ worker.postMessage({ type: "start", settings });
247
+ return {
248
+ addFile(input) {
249
+ if (failure !== null) return Promise.reject(failure);
250
+ const id = nextId++;
251
+ const buffer = ownedBuffer(input.bytes);
252
+ return new Promise((resolve, reject) => {
253
+ waiting.set(id, { resolve, reject });
254
+ worker.postMessage({ type: "file", id, day: input.day, bytes: new Uint8Array(buffer) }, [
255
+ buffer
256
+ ]);
257
+ });
258
+ },
259
+ finish() {
260
+ if (failure !== null) return Promise.reject(failure);
261
+ return new Promise((resolve, reject) => {
262
+ finishing = { resolve, reject };
263
+ worker.postMessage({ type: "finish" });
264
+ });
265
+ },
266
+ abandon() {
267
+ failAll(mergeFailed("the build stopped"));
268
+ }
269
+ };
270
+ }
271
+ async function startMerge(settings, memoryBytes) {
272
+ const entry = resolveMergeWorkerEntry();
273
+ if (entry === null) return inThreadSession(settings);
274
+ const worker = await spawnNodeWorker(new URL(entry), {
275
+ maxOldGenerationSizeMb: Math.max(1, Math.floor(memoryBytes / BYTES_PER_MIB))
276
+ });
277
+ if (worker === null) return inThreadSession(settings);
278
+ worker.unref?.();
279
+ return workerSession(worker, settings);
280
+ }
281
+
282
+ // src/query-suggestions/suggestions-writer.ts
283
+ var ORIGIN_POPULAR = "popular";
284
+ var ORIGIN_MANUAL = "manual";
285
+ function suggestionsIndexName(indexName, group) {
286
+ return `${indexName}${SUGGESTIONS_INDEX_INFIX}${group}`;
287
+ }
288
+ function suggestionDocument(phrase, count, origin) {
289
+ return { id: phrase, query: phrase, count, origin };
290
+ }
291
+ function markedSuggestionIndexes(host, indexName) {
292
+ const byGroup = /* @__PURE__ */ new Map();
293
+ for (const name of host.indexNames()) {
294
+ const source = host.view(name)?.state?.querySuggestionsSource;
295
+ if (source?.index === indexName) byGroup.set(source.group, name);
296
+ }
297
+ return byGroup;
298
+ }
299
+ async function ensureSuggestionsIndex(host, indexName, group, created = []) {
300
+ const name = suggestionsIndexName(indexName, group);
301
+ const source = host.view(indexName);
302
+ if (source === null) {
303
+ throw new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" does not exist`, { indexName });
304
+ }
305
+ const existing = host.view(name);
306
+ if (existing === null) {
307
+ await host.createSuggestionsIndex(name, { index: indexName, group }, source.language);
308
+ created.push(name);
309
+ return name;
310
+ }
311
+ const marker = existing.state?.querySuggestionsSource;
312
+ if (marker?.index !== indexName || marker.group !== group) {
313
+ throw new NarsilError(
314
+ ErrorCodes.CONFIG_INVALID,
315
+ `Index "${name}" exists without the marker of a suggestions index for "${indexName}", so the build leaves it alone`,
316
+ { indexName: name }
317
+ );
318
+ }
319
+ if (existing.language !== source.language) {
320
+ await host.engine.changeAnalysis(name, { language: source.language });
321
+ }
322
+ return name;
323
+ }
324
+ async function readSuggestions(host, name) {
325
+ const stored = /* @__PURE__ */ new Map();
326
+ let cursor;
327
+ do {
328
+ const page = await host.engine.listDocuments(name, {
329
+ limit: BUILD_LIST_PAGE,
330
+ ...cursor !== void 0 ? { cursor } : {}
331
+ });
332
+ for (const { id, document } of page.documents) {
333
+ const count = typeof document.count === "number" ? document.count : 0;
334
+ const origin = typeof document.origin === "string" ? document.origin : ORIGIN_POPULAR;
335
+ stored.set(id, { count, origin });
336
+ }
337
+ cursor = page.cursor ?? void 0;
338
+ } while (cursor !== void 0);
339
+ return stored;
340
+ }
341
+ function requireWritten(result) {
342
+ const first = result.failed[0];
343
+ if (first !== void 0) throw first.error;
344
+ }
345
+ async function removeBatchOf(host, name, ids) {
346
+ const result = await host.engine.removeBatch(name, [...ids]);
347
+ for (const failure of result.failed) {
348
+ if (failure.error.code !== ErrorCodes.DOC_NOT_FOUND) throw failure.error;
349
+ }
350
+ return result.succeeded;
351
+ }
352
+ async function removeSuggestions(host, name, ids) {
353
+ let removed = 0;
354
+ for (let start = 0; start < ids.length; start += BUILD_WRITE_BATCH) {
355
+ removed += (await removeBatchOf(host, name, ids.slice(start, start + BUILD_WRITE_BATCH))).length;
356
+ }
357
+ return removed;
358
+ }
359
+ function restoreDocument(entry) {
360
+ return suggestionDocument(entry.phrase, entry.stored.count, entry.stored.origin);
361
+ }
362
+ async function undoWrites(host, steps, banned) {
363
+ for (const step of [...steps].reverse()) {
364
+ await removeSuggestions(host, step.name, step.remove);
365
+ const restore = step.restore.filter((entry) => !banned.has(entry.phrase));
366
+ for (let start = 0; start < restore.length; start += BUILD_WRITE_BATCH) {
367
+ const batch = restore.slice(start, start + BUILD_WRITE_BATCH);
368
+ await removeSuggestions(
369
+ host,
370
+ step.name,
371
+ batch.map((entry) => entry.phrase)
372
+ );
373
+ requireWritten(await host.engine.insertBatch(step.name, batch.map(restoreDocument)));
374
+ }
375
+ }
376
+ }
377
+ function planWrites(stored, due, banned, onlyAdd) {
378
+ const plan = { inserts: [], updates: [], stale: [], banned: [] };
379
+ for (const [phrase, count] of due) {
380
+ if (banned.has(phrase)) continue;
381
+ const current = stored.get(phrase);
382
+ if (current === void 0) plan.inserts.push(suggestionDocument(phrase, count, ORIGIN_POPULAR));
383
+ else if (current.origin === ORIGIN_POPULAR && current.count !== count) {
384
+ plan.updates.push({ phrase, count, previous: current });
385
+ }
386
+ }
387
+ for (const [phrase, current] of stored) {
388
+ if (banned.has(phrase)) plan.banned.push(phrase);
389
+ else if (!onlyAdd && current.origin === ORIGIN_POPULAR && !due.has(phrase)) {
390
+ plan.stale.push({ phrase, stored: current });
391
+ }
392
+ }
393
+ return plan;
394
+ }
395
+ async function writeGroup(host, name, due, banned, onlyAdd, undo) {
396
+ const plan = planWrites(await readSuggestions(host, name), due, banned, onlyAdd);
397
+ const step = { name, remove: [], restore: [] };
398
+ undo.push(step);
399
+ for (let start = 0; start < plan.inserts.length; start += BUILD_WRITE_BATCH) {
400
+ const result = await host.engine.insertBatch(name, plan.inserts.slice(start, start + BUILD_WRITE_BATCH));
401
+ step.remove.push(...result.succeeded);
402
+ requireWritten(result);
403
+ }
404
+ for (let start = 0; start < plan.updates.length; start += BUILD_WRITE_BATCH) {
405
+ const batch = plan.updates.slice(start, start + BUILD_WRITE_BATCH);
406
+ const result = await host.engine.updateBatch(
407
+ name,
408
+ batch.map((entry) => ({
409
+ docId: entry.phrase,
410
+ document: suggestionDocument(entry.phrase, entry.count, ORIGIN_POPULAR)
411
+ }))
412
+ );
413
+ const succeeded = new Set(result.succeeded);
414
+ for (const entry of batch) {
415
+ if (succeeded.has(entry.phrase)) step.restore.push({ phrase: entry.phrase, stored: entry.previous });
416
+ }
417
+ requireWritten(result);
418
+ }
419
+ let removed = 0;
420
+ for (let start = 0; start < plan.stale.length; start += BUILD_WRITE_BATCH) {
421
+ const batch = plan.stale.slice(start, start + BUILD_WRITE_BATCH);
422
+ const succeeded = new Set(
423
+ await removeBatchOf(
424
+ host,
425
+ name,
426
+ batch.map((entry) => entry.phrase)
427
+ )
428
+ );
429
+ removed += succeeded.size;
430
+ for (const entry of batch) if (succeeded.has(entry.phrase)) step.restore.push(entry);
431
+ }
432
+ removed += await removeSuggestions(host, name, plan.banned);
433
+ return { published: plan.inserts.length + plan.updates.length, removed };
434
+ }
435
+
436
+ // src/query-suggestions/build.ts
437
+ function enabledSetting(host, indexName) {
438
+ const view = host.view(indexName);
439
+ if (view === null)
440
+ throw new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" does not exist`, { indexName });
441
+ const setting = view.state?.querySuggestions;
442
+ if (setting === void 0 || !setting.enabled) {
443
+ throw new NarsilError(
444
+ ErrorCodes.QUERY_SUGGESTIONS_NOT_ENABLED,
445
+ `Index "${indexName}" has popular query suggestions off`,
446
+ { indexName }
447
+ );
448
+ }
449
+ return resolveSetting(setting);
450
+ }
451
+ function cutoffDay(now, retentionDays) {
452
+ return dayOf(now - retentionDays * DAY_MS);
453
+ }
454
+ function warn(host, indexName, reason, file) {
455
+ host.emit("querySuggestionsWarning", { indexName, reason, file });
456
+ }
457
+ async function mergeFile(ctx, session, merged, indexName, file, removeLocal) {
458
+ const outcome = await session.addFile({ day: file.day, bytes: file.bytes });
459
+ if (outcome.kind === "merged") {
460
+ merged.sources.push({ nodeId: file.nodeId, key: file.key, savedAt: outcome.savedAt });
461
+ if (merged.oldestDay === null || outcome.day < merged.oldestDay) merged.oldestDay = outcome.day;
462
+ return;
463
+ }
464
+ if (outcome.kind === "other-index") {
465
+ warn(ctx.host, indexName, "damaged-file", file.key);
466
+ await removeLocal?.();
467
+ return;
468
+ }
469
+ warn(ctx.host, indexName, outcome.kind === "other-key" ? "other-key" : "damaged-file", file.key);
470
+ }
471
+ async function mergeHistory(ctx, indexName, setting, now) {
472
+ const { host } = ctx;
473
+ const view = host.view(indexName);
474
+ await ctx.saveDay(indexName);
475
+ const cutoff = cutoffDay(now, setting.retentionDays);
476
+ const session = await startMerge(
477
+ {
478
+ minPeople: setting.minPeople,
479
+ maxMergePhrases: setting.maxMergePhrases,
480
+ maxGroups: setting.maxGroups,
481
+ markedGroups: [...markedSuggestionIndexes(host, indexName).keys()],
482
+ fingerprint: ctx.scrambler.fingerprint,
483
+ indexUuid: view?.indexUuid ?? null,
484
+ clustered: host.clustered,
485
+ banned: view?.state?.bannedQueries ?? []
486
+ },
487
+ setting.mergeMemoryBytes
488
+ );
489
+ const merged = { result: { groups: {}, discarded: 0 }, sources: [], oldestDay: null, silentNodes: 0 };
490
+ try {
491
+ let keys;
492
+ try {
493
+ keys = await listHistoryFolder(host.files, indexName);
494
+ } catch (error) {
495
+ throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, "The node cannot read its query history folder", {
496
+ cause: error instanceof Error ? error.message : String(error)
497
+ });
498
+ }
499
+ const files = [];
500
+ for (const key of keys) {
501
+ const file = historyFileOf(indexName, key);
502
+ if (file !== null && file.day >= cutoff) files.push(file);
503
+ }
504
+ files.sort((left, right) => compareCodePoints(left.key, right.key));
505
+ let localMerged = false;
506
+ const mergeLocalFiles = async () => {
507
+ if (localMerged) return;
508
+ localMerged = true;
509
+ for (const file of files) {
510
+ const bytes = await host.files.read(file.key);
511
+ if (bytes === null) continue;
512
+ if (bytes === "too-long") {
513
+ warn(host, indexName, "damaged-file", file.key);
514
+ continue;
515
+ }
516
+ await mergeFile(
517
+ ctx,
518
+ session,
519
+ merged,
520
+ indexName,
521
+ { nodeId: host.nodeId, key: file.key, day: file.day, bytes },
522
+ () => host.files.remove(file.key)
523
+ );
524
+ }
525
+ };
526
+ for await (const remote of host.remoteHistory(indexName)) {
527
+ if (remote.kind === "silent") {
528
+ merged.silentNodes += 1;
529
+ continue;
530
+ }
531
+ if (compareCodePoints(remote.nodeId, host.nodeId) > 0) await mergeLocalFiles();
532
+ const name = historyFileOf(indexName, remote.key);
533
+ if (name === null || name.day < cutoff) continue;
534
+ await mergeFile(ctx, session, merged, indexName, { ...remote, day: name.day }, null);
535
+ }
536
+ await mergeLocalFiles();
537
+ merged.result = await session.finish();
538
+ return merged;
539
+ } catch (error) {
540
+ session.abandon();
541
+ throw error;
542
+ }
543
+ }
544
+ async function pace(checksPerSecond) {
545
+ await new Promise((resolve) => setTimeout(resolve, MS_PER_SECOND / checksPerSecond));
546
+ }
547
+ async function meetsMinResults(ctx, indexName, setting, phrase) {
548
+ if (ctx.isStopping()) throw new NarsilError(ErrorCodes.INSTANCE_SHUT_DOWN, "The engine is shutting down");
549
+ const checked = await ctx.host.engine.preflight(indexName, { term: phrase, fields: setting.fields, termMatch: "all" });
550
+ await pace(setting.resultChecksPerSecond);
551
+ return checked.count >= setting.minResults;
552
+ }
553
+ async function choose(ctx, indexName, setting, phrases) {
554
+ const due = /* @__PURE__ */ new Map();
555
+ for (const { phrase, count } of phrases) {
556
+ if (await meetsMinResults(ctx, indexName, setting, phrase)) due.set(phrase, count);
557
+ }
558
+ return due;
559
+ }
560
+ function groupsToWrite(ctx, indexName, setting, result) {
561
+ const existing = markedSuggestionIndexes(ctx.host, indexName);
562
+ const groups = [...existing.keys()];
563
+ let discarded = 0;
564
+ for (const group of Object.keys(result.groups).sort()) {
565
+ if (existing.has(group)) continue;
566
+ if (groups.length < setting.maxGroups) groups.push(group);
567
+ else discarded += result.groups[group]?.length ?? 0;
568
+ }
569
+ return { groups, discarded };
570
+ }
571
+ async function runBuild(ctx, indexName, lastCompleted, now) {
572
+ const setting = enabledSetting(ctx.host, indexName);
573
+ const merged = await mergeHistory(ctx, indexName, setting, now);
574
+ const sourcesDigest = sourcesDigestOf(merged.sources);
575
+ if (lastCompleted !== null && sameBytes(lastCompleted.sourcesDigest, sourcesDigest)) {
576
+ return { status: "skipped", published: 0, removed: 0, discarded: 0, sourcesDigest };
577
+ }
578
+ const onlyAdd = merged.silentNodes > 0 || merged.oldestDay === null || merged.oldestDay > cutoffDay(now, setting.retentionDays);
579
+ const { groups, discarded: groupsDiscarded } = groupsToWrite(ctx, indexName, setting, merged.result);
580
+ const dueByGroup = /* @__PURE__ */ new Map();
581
+ for (const group of groups) {
582
+ dueByGroup.set(group, await choose(ctx, indexName, setting, merged.result.groups[group] ?? []));
583
+ }
584
+ let published = 0;
585
+ let removed = 0;
586
+ const undo = [];
587
+ const created = [];
588
+ const bannedNow = () => new Set(ctx.host.view(indexName)?.state?.bannedQueries ?? []);
589
+ try {
590
+ for (const [group, due] of dueByGroup) {
591
+ const changes = await ctx.withWriteLock(indexName, async () => {
592
+ const name = await ensureSuggestionsIndex(ctx.host, indexName, group, created);
593
+ return writeGroup(ctx.host, name, due, bannedNow(), onlyAdd, undo);
594
+ });
595
+ published += changes.published;
596
+ removed += changes.removed;
597
+ }
598
+ } catch (error) {
599
+ await ctx.withWriteLock(indexName, () => undoWrites(ctx.host, undo, bannedNow())).catch(() => void 0);
600
+ for (const name of created) await ctx.host.engine.dropIndex(name).catch(() => void 0);
601
+ throw error;
602
+ }
603
+ return {
604
+ status: "completed",
605
+ published,
606
+ removed,
607
+ discarded: merged.result.discarded + groupsDiscarded,
608
+ sourcesDigest
609
+ };
610
+ }
611
+
612
+ // src/query-suggestions/build-runner.ts
613
+ function errorCodeOf(error) {
614
+ return error instanceof NarsilError ? error.code : ErrorCodes.WORKER_ACTION_FAILED;
615
+ }
616
+ function runningRecordOf(identity) {
617
+ return {
618
+ ...identity,
619
+ status: "running",
620
+ published: 0,
621
+ removed: 0,
622
+ discarded: 0,
623
+ sourcesDigest: new Uint8Array(SOURCES_DIGEST_BYTES)
624
+ };
625
+ }
626
+ function newBuild(trigger, status) {
627
+ return {
628
+ buildId: globalThis.crypto.randomUUID(),
629
+ trigger,
630
+ status,
631
+ startedAt: Date.now(),
632
+ endedAt: null,
633
+ published: 0,
634
+ removed: 0,
635
+ discarded: 0,
636
+ errorCode: null
637
+ };
638
+ }
639
+ var BuildRunner = class {
640
+ runtime;
641
+ running = /* @__PURE__ */ new Map();
642
+ finished = /* @__PURE__ */ new Map();
643
+ constructor(runtime) {
644
+ this.runtime = runtime;
645
+ }
646
+ async lastBuild(indexName) {
647
+ const running = this.running.get(indexName);
648
+ if (running !== void 0) return { ...running.build };
649
+ const finished = this.finished.get(indexName);
650
+ if (finished !== void 0) return { ...finished };
651
+ const record = await this.runtime.host.readBuildRecord(indexName).catch(() => null);
652
+ return record === null ? null : publicBuildOf(record);
653
+ }
654
+ isRunning(indexName) {
655
+ return this.running.has(indexName);
656
+ }
657
+ start(indexName, trigger) {
658
+ return this.begin(indexName, trigger).done;
659
+ }
660
+ begin(indexName, trigger) {
661
+ this.runtime.enabledSetting(indexName);
662
+ if (this.runtime.scrambler === null) {
663
+ throw new NarsilError(
664
+ ErrorCodes.CONFIG_INVALID,
665
+ "This engine has no query suggestions key, so it builds no suggestions",
666
+ { indexName }
667
+ );
668
+ }
669
+ if (this.runtime.isStopping) throw new NarsilError(ErrorCodes.INSTANCE_SHUT_DOWN, "The engine is shutting down");
670
+ if (this.running.has(indexName)) {
671
+ const skipped = { ...newBuild(trigger, "skipped"), endedAt: Date.now() };
672
+ this.emitPhase(indexName, skipped);
673
+ return { build: skipped, started: Promise.resolve(), done: Promise.resolve(skipped) };
674
+ }
675
+ const build = newBuild(trigger, "running");
676
+ let markStarted = () => void 0;
677
+ const started = new Promise((resolve) => {
678
+ markStarted = resolve;
679
+ });
680
+ const done = this.run(indexName, build, markStarted);
681
+ void done.finally(markStarted);
682
+ const running = { build, started, done };
683
+ this.running.set(indexName, running);
684
+ void done.finally(() => {
685
+ if (this.running.get(indexName)?.build === build) this.running.delete(indexName);
686
+ });
687
+ return running;
688
+ }
689
+ async settled() {
690
+ await Promise.allSettled([...this.running.values()].map((running) => running.done));
691
+ }
692
+ forget(indexName) {
693
+ this.finished.delete(indexName);
694
+ }
695
+ emitPhase(indexName, build) {
696
+ const phase = build.status === "running" ? "started" : build.status;
697
+ this.runtime.host.emit("querySuggestionsBuild", {
698
+ indexName,
699
+ buildId: build.buildId,
700
+ phase,
701
+ trigger: build.trigger,
702
+ published: build.published,
703
+ removed: build.removed,
704
+ discarded: build.discarded,
705
+ ...build.errorCode !== null ? { errorCode: build.errorCode } : {}
706
+ });
707
+ }
708
+ async lastCompleted(indexName) {
709
+ const record = await this.runtime.host.readBuildRecord(indexName).catch(() => null);
710
+ return record !== null && (record.status === "completed" || record.status === "skipped") ? record : null;
711
+ }
712
+ async run(indexName, build, markStarted) {
713
+ await Promise.resolve();
714
+ this.emitPhase(indexName, build);
715
+ const runtime = this.runtime;
716
+ let record;
717
+ const identity = { buildId: build.buildId, trigger: build.trigger, startedAt: build.startedAt };
718
+ try {
719
+ const scrambler = runtime.scrambler;
720
+ if (scrambler === null)
721
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "This engine has no query suggestions key");
722
+ const lastCompleted = await this.lastCompleted(indexName);
723
+ if (runtime.host.clustered) await runtime.host.writeBuildRecord(indexName, runningRecordOf(identity));
724
+ markStarted();
725
+ const outcome = await runBuild(
726
+ {
727
+ host: runtime.host,
728
+ scrambler,
729
+ saveDay: (name) => runtime.saveDay(name),
730
+ withWriteLock: (name, work) => runtime.withWriteLock(name, work),
731
+ isStopping: () => runtime.isStopping
732
+ },
733
+ indexName,
734
+ lastCompleted,
735
+ build.startedAt
736
+ );
737
+ record = { ...identity, ...outcome, endedAt: Date.now() };
738
+ } catch (error) {
739
+ record = {
740
+ ...identity,
741
+ status: "failed",
742
+ endedAt: Date.now(),
743
+ published: 0,
744
+ removed: 0,
745
+ discarded: 0,
746
+ sourcesDigest: new Uint8Array(SOURCES_DIGEST_BYTES),
747
+ errorCode: errorCodeOf(error)
748
+ };
749
+ }
750
+ const finalRecord = record;
751
+ await runtime.host.writeBuildRecord(indexName, finalRecord).catch((error) => {
752
+ runtime.host.emit("querySuggestionsWarning", {
753
+ indexName,
754
+ reason: "save-failed",
755
+ message: error instanceof Error ? error.message : String(error)
756
+ });
757
+ });
758
+ const finished = publicBuildOf(finalRecord);
759
+ this.finished.set(indexName, finished);
760
+ this.emitPhase(indexName, finished);
761
+ return finished;
762
+ }
763
+ };
764
+
765
+ // src/query-suggestions/scramble.ts
766
+ var INNER_PAD = 54;
767
+ var OUTER_PAD = 92;
768
+ function padState(key, pad, words) {
769
+ const block = new Uint8Array(SHA256_BLOCK_BYTES);
770
+ for (let i = 0; i < SHA256_BLOCK_BYTES; i++) block[i] = (key[i] ?? 0) ^ pad;
771
+ const state = Uint32Array.from(SHA256_INITIAL_STATE);
772
+ compressSha256Block(state, new DataView(block.buffer), 0, words);
773
+ return state;
774
+ }
775
+ function createPersonScrambler(key) {
776
+ const words = new Uint32Array(64);
777
+ const blockKey = key.byteLength > SHA256_BLOCK_BYTES ? sha256(key) : key;
778
+ const innerState = padState(blockKey, INNER_PAD, words);
779
+ const outerState = padState(blockKey, OUTER_PAD, words);
780
+ const working = new Uint32Array(8);
781
+ const encoder = new TextEncoder();
782
+ function digestAfter(prefixState, message) {
783
+ working.set(prefixState);
784
+ const view = paddedSha256Message(message, SHA256_BLOCK_BYTES);
785
+ for (let block = 0; block < view.byteLength; block += SHA256_BLOCK_BYTES) {
786
+ compressSha256Block(working, view, block, words);
787
+ }
788
+ return sha256Digest(working);
789
+ }
790
+ function hmac(message) {
791
+ return digestAfter(outerState, digestAfter(innerState, message));
792
+ }
793
+ return {
794
+ fingerprint: hmac(encoder.encode(KEY_FINGERPRINT_LABEL)).slice(0, KEY_FINGERPRINT_BYTES),
795
+ scramble: (person) => hmac(encoder.encode(person)).slice(0, SCRAMBLED_ID_BYTES)
796
+ };
797
+ }
798
+
799
+ // src/query-suggestions/timers.ts
800
+ function unrefTimer(timer) {
801
+ timer.unref?.();
802
+ return timer;
803
+ }
804
+ var SuggestionTimers = class {
805
+ runtime;
806
+ saves = /* @__PURE__ */ new Map();
807
+ schedules = /* @__PURE__ */ new Map();
808
+ sweep = null;
809
+ midnight = null;
810
+ stopped = false;
811
+ constructor(runtime) {
812
+ this.runtime = runtime;
813
+ }
814
+ saveSoon(indexName, intervalMs) {
815
+ if (this.stopped || this.saves.has(indexName)) return;
816
+ const timer = setTimeout(() => {
817
+ this.saves.delete(indexName);
818
+ void this.runtime.saveDay(indexName).then(() => {
819
+ if (this.runtime.days?.hasUnsaved(indexName) === true) this.saveSoon(indexName, intervalMs);
820
+ }).catch(() => void 0);
821
+ }, intervalMs);
822
+ this.saves.set(indexName, unrefTimer(timer));
823
+ }
824
+ forgetSave(indexName) {
825
+ const timer = this.saves.get(indexName);
826
+ if (timer !== void 0) clearTimeout(timer);
827
+ this.saves.delete(indexName);
828
+ }
829
+ forgetIndex(indexName) {
830
+ this.forgetSave(indexName);
831
+ const timer = this.schedules.get(indexName);
832
+ if (timer !== void 0) clearTimeout(timer);
833
+ this.schedules.delete(indexName);
834
+ }
835
+ async start() {
836
+ await removeRestoreLeftovers(this.runtime.host.files).catch(() => void 0);
837
+ await this.sweepNow();
838
+ this.sweep = setInterval(() => void this.sweepNow(), HISTORY_SWEEP_INTERVAL_MS);
839
+ this.sweep.unref?.();
840
+ this.armMidnight();
841
+ for (const indexName of this.runtime.host.indexNames()) {
842
+ if (this.runtime.settingOf(indexName)?.enabled !== true) continue;
843
+ await this.startMissedBuild(indexName);
844
+ this.rearm(indexName);
845
+ }
846
+ }
847
+ async catchUpMissedBuilds() {
848
+ for (const indexName of this.runtime.host.indexNames()) {
849
+ if (this.stopped) return;
850
+ if (this.runtime.settingOf(indexName)?.enabled === true) await this.startMissedBuild(indexName);
851
+ }
852
+ }
853
+ stop() {
854
+ this.stopped = true;
855
+ for (const timer of this.saves.values()) clearTimeout(timer);
856
+ for (const timer of this.schedules.values()) clearTimeout(timer);
857
+ this.saves.clear();
858
+ this.schedules.clear();
859
+ if (this.sweep !== null) clearInterval(this.sweep);
860
+ if (this.midnight !== null) clearTimeout(this.midnight);
861
+ this.sweep = null;
862
+ this.midnight = null;
863
+ }
864
+ nextBuildAt(indexName) {
865
+ const setting = this.runtime.settingOf(indexName);
866
+ if (setting?.enabled !== true || this.runtime.scrambler === null) return null;
867
+ const resolved = resolveSetting(setting);
868
+ if (resolved.schedule === SCHEDULE_OFF) return null;
869
+ const schedule = parseSchedule(resolved.schedule);
870
+ return schedule === null ? null : nextMatchAfter(schedule, resolved.timeZone, Date.now());
871
+ }
872
+ rearm(indexName) {
873
+ const previous = this.schedules.get(indexName);
874
+ if (previous !== void 0) clearTimeout(previous);
875
+ this.schedules.delete(indexName);
876
+ if (this.stopped) return;
877
+ const at = this.nextBuildAt(indexName);
878
+ if (at !== null) this.arm(indexName, at);
879
+ }
880
+ arm(indexName, at) {
881
+ const delay = Math.min(Math.max(at - Date.now(), 0), MAX_TIMER_DELAY_MS);
882
+ const timer = setTimeout(() => {
883
+ this.schedules.delete(indexName);
884
+ if (this.stopped) return;
885
+ if (Date.now() < at) {
886
+ this.arm(indexName, at);
887
+ return;
888
+ }
889
+ this.startBuild(indexName, "schedule");
890
+ this.rearm(indexName);
891
+ }, delay);
892
+ this.schedules.set(indexName, unrefTimer(timer));
893
+ }
894
+ startBuild(indexName, trigger) {
895
+ if (!this.runtime.host.mayBuild()) return;
896
+ try {
897
+ void this.runtime.builds.start(indexName, trigger).catch(() => void 0);
898
+ } catch {
899
+ }
900
+ }
901
+ async startMissedBuild(indexName) {
902
+ const setting = this.runtime.settingOf(indexName);
903
+ if (setting === null) return;
904
+ const resolved = resolveSetting(setting);
905
+ if (resolved.schedule === SCHEDULE_OFF) return;
906
+ const schedule = parseSchedule(resolved.schedule);
907
+ if (schedule === null) return;
908
+ const latest = latestMatchAtOrBefore(schedule, resolved.timeZone, Date.now());
909
+ if (latest === null) return;
910
+ const record = await this.runtime.host.readBuildRecord(indexName).catch(() => null);
911
+ const completedSince = record !== null && (record.status === "completed" || record.status === "skipped") && record.startedAt >= latest;
912
+ if (!completedSince) this.startBuild(indexName, "missed");
913
+ }
914
+ armMidnight() {
915
+ if (this.stopped) return;
916
+ const now = Date.now();
917
+ const delay = Math.min(Math.floor(now / DAY_MS + 1) * DAY_MS - now, MAX_TIMER_DELAY_MS);
918
+ this.midnight = unrefTimer(
919
+ setTimeout(() => {
920
+ this.midnight = null;
921
+ void this.retireDays().finally(() => this.armMidnight());
922
+ }, delay)
923
+ );
924
+ }
925
+ async retireDays() {
926
+ const days = this.runtime.days;
927
+ if (days === null) return;
928
+ const today = dayOf(Date.now());
929
+ for (const indexName of days.indexes()) await days.retire(indexName, today).catch(() => void 0);
930
+ }
931
+ async sweepNow() {
932
+ const { host } = this.runtime;
933
+ const today = Date.now();
934
+ for (const indexName of host.indexNames()) {
935
+ const setting = this.runtime.settingOf(indexName);
936
+ try {
937
+ if (setting?.enabled !== true) {
938
+ if (host.view(indexName)?.state?.querySuggestionsSource === void 0) {
939
+ await removeHistoryFolder(host.files, indexName);
940
+ }
941
+ continue;
942
+ }
943
+ const cutoff = dayOf(today - resolveSetting(setting).retentionDays * DAY_MS);
944
+ for (const key of await listHistoryFolder(host.files, indexName)) {
945
+ const file = historyFileOf(indexName, key);
946
+ if (file !== null && file.day < cutoff) await host.files.remove(key);
947
+ }
948
+ } catch {
949
+ }
950
+ }
951
+ }
952
+ };
953
+
954
+ // src/query-suggestions/runtime.ts
955
+ function keyBytesOf(key) {
956
+ const bytes = typeof key === "string" ? new TextEncoder().encode(key) : new Uint8Array(key);
957
+ if (bytes.byteLength < MIN_KEY_BYTES) {
958
+ throw new NarsilError(
959
+ ErrorCodes.CONFIG_INVALID,
960
+ `The query suggestions key must hold at least ${MIN_KEY_BYTES} bytes, and it holds ${bytes.byteLength}`,
961
+ { bytes: bytes.byteLength }
962
+ );
963
+ }
964
+ return bytes;
965
+ }
966
+ function notFound(indexName) {
967
+ return new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Index "${indexName}" does not exist`, { indexName });
968
+ }
969
+ function notEnabled(indexName) {
970
+ return new NarsilError(
971
+ ErrorCodes.QUERY_SUGGESTIONS_NOT_ENABLED,
972
+ `Index "${indexName}" has popular query suggestions off`,
973
+ { indexName }
974
+ );
975
+ }
976
+ var QuerySuggestionsRuntime = class {
977
+ host;
978
+ scrambler;
979
+ days;
980
+ builds;
981
+ timers;
982
+ locks = /* @__PURE__ */ new Map();
983
+ stopping = false;
984
+ constructor(host, key) {
985
+ this.host = host;
986
+ this.scrambler = key === null ? null : createPersonScrambler(key);
987
+ this.days = this.scrambler === null ? null : new DayStore(host, this.scrambler);
988
+ this.builds = new BuildRunner(this);
989
+ this.timers = new SuggestionTimers(this);
990
+ }
991
+ get isStopping() {
992
+ return this.stopping;
993
+ }
994
+ enabledSetting(indexName) {
995
+ const view = this.host.view(indexName);
996
+ if (view === null) throw notFound(indexName);
997
+ const setting = view.state?.querySuggestions;
998
+ if (setting === void 0 || !setting.enabled) throw notEnabled(indexName);
999
+ return resolveSetting(setting);
1000
+ }
1001
+ settingOf(indexName) {
1002
+ return this.host.view(indexName)?.state?.querySuggestions ?? null;
1003
+ }
1004
+ withWriteLock(indexName, work) {
1005
+ const previous = this.locks.get(indexName) ?? Promise.resolve();
1006
+ const next = previous.then(work, work);
1007
+ const settled = next.catch(() => void 0);
1008
+ this.locks.set(indexName, settled);
1009
+ void settled.then(() => {
1010
+ if (this.locks.get(indexName) === settled) this.locks.delete(indexName);
1011
+ });
1012
+ return next;
1013
+ }
1014
+ async saveDay(indexName) {
1015
+ await this.days?.save(indexName);
1016
+ }
1017
+ groupAllowed(indexName, group, maxGroups, known) {
1018
+ for (const marked of markedSuggestionIndexes(this.host, indexName).keys()) known.add(marked);
1019
+ return known.has(group) || known.size < maxGroups;
1020
+ }
1021
+ async record(indexName, input) {
1022
+ if (this.stopping) throw new NarsilError(ErrorCodes.INSTANCE_SHUT_DOWN, "The engine is shutting down");
1023
+ const setting = this.enabledSetting(indexName);
1024
+ if (this.days === null) return { recorded: false, outcome: "no-key" };
1025
+ const { query, person, group } = input ?? {};
1026
+ if (typeof query !== "string" || !isPerson(person) || !isGroup(group)) {
1027
+ return { recorded: false, outcome: "invalid" };
1028
+ }
1029
+ const phrase = acceptedPhrase(query, setting);
1030
+ if (phrase === null) return { recorded: false, outcome: "invalid" };
1031
+ const isBanned = () => (this.host.view(indexName)?.state?.bannedQueries ?? []).includes(phrase);
1032
+ if (isBanned()) return { recorded: false, outcome: "banned" };
1033
+ const outcome = await this.days.count(indexName, setting, Date.now(), {
1034
+ group,
1035
+ phrase,
1036
+ person,
1037
+ groupAllowed: (known) => this.groupAllowed(indexName, group, setting.maxGroups, known),
1038
+ stillEnabled: () => this.settingOf(indexName)?.enabled === true,
1039
+ isBanned
1040
+ });
1041
+ if (outcome === null) throw notEnabled(indexName);
1042
+ if (outcome === "recorded") this.timers.saveSoon(indexName, setting.saveIntervalMs);
1043
+ return { recorded: outcome === "recorded", outcome };
1044
+ }
1045
+ restoredHistory(indexName, setting) {
1046
+ if (setting?.enabled !== true || this.scrambler === null) return null;
1047
+ return new RestoredHistory(this, indexName, setting);
1048
+ }
1049
+ async discardHistory(indexName) {
1050
+ this.timers.forgetSave(indexName);
1051
+ await this.days?.discard(indexName);
1052
+ await removeHistoryFolder(this.host.files, indexName);
1053
+ }
1054
+ async onDrop(indexName) {
1055
+ this.timers.forgetIndex(indexName);
1056
+ this.builds.forget(indexName);
1057
+ await this.days?.discard(indexName);
1058
+ await removeHistoryFolder(this.host.files, indexName).catch(() => void 0);
1059
+ }
1060
+ warnWithoutKey() {
1061
+ if (this.scrambler !== null) return;
1062
+ for (const indexName of this.host.indexNames()) {
1063
+ if (this.settingOf(indexName)?.enabled === true) {
1064
+ this.host.emit("querySuggestionsWarning", { indexName, reason: "no-key" });
1065
+ }
1066
+ }
1067
+ }
1068
+ async start() {
1069
+ this.warnWithoutKey();
1070
+ if (this.scrambler === null) {
1071
+ await this.timers.sweepNow();
1072
+ return;
1073
+ }
1074
+ await this.timers.start();
1075
+ }
1076
+ settingChanged(indexName) {
1077
+ this.timers.rearm(indexName);
1078
+ }
1079
+ async stop() {
1080
+ if (this.stopping) return;
1081
+ this.stopping = true;
1082
+ this.timers.stop();
1083
+ await this.builds.settled();
1084
+ if (this.days === null) return;
1085
+ for (const indexName of this.days.indexes()) await this.days.save(indexName);
1086
+ }
1087
+ today() {
1088
+ return dayOf(Date.now());
1089
+ }
1090
+ };
1091
+
1092
+ export { ORIGIN_MANUAL, QuerySuggestionsRuntime, SUGGESTIONS_SCHEMA, buildRecordFromWire, buildRecordToWire, createLocalHost, enabledSetting, ensureSuggestionsIndex, groupsToWrite, keyBytesOf, markedSuggestionIndexes, meetsMinResults, mergeHistory, publicBuildOf, removeSuggestions, suggestionDocument, suggestionsIndexName };