@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,1007 @@
1
+ import { dayFileKey, restoreStagingPrefix, restoredFileKey, listHistoryFolder, historyFileOf } from './chunk-ZZT7CEN7.mjs';
2
+ import { resolveSetting, isGroup, isNormalPhrase } from './chunk-TYIRMPD3.mjs';
3
+ import { MAX_HISTORY_FILE_BYTES, KEY_FINGERPRINT_BYTES, DAY_MS, SCRAMBLED_ID_BYTES, QUERY_HISTORY_ARCHIVE_PREFIX, MAX_INDEX_UUID_LENGTH, MAX_MAX_LENGTH, MAX_MAX_WORDS, MIN_PHRASE_TABLE_CAPACITY, PERSON_TABLE_MIN_SLOTS, PAIR_PAGE_BLOCKS, PAIR_BLOCK_SLOTS, MAX_PEOPLE_PER_PHRASE_IN_FILE } from './chunk-PJHAB5ZY.mjs';
4
+ import { packEnvelopeWithChecksum } from './chunk-HUOYZJ3Q.mjs';
5
+ import { openChecksummedEnvelope, HEADER_SIZE, crc32 } from './chunk-YH7QY657.mjs';
6
+
7
+ // src/query-suggestions/msgpack-cursor.ts
8
+ var MalformedPayload = class extends Error {
9
+ };
10
+ function utf8ByteLength(text) {
11
+ let bytes = 0;
12
+ for (let i = 0; i < text.length; i++) {
13
+ const unit = text.charCodeAt(i);
14
+ if (unit < 128) bytes += 1;
15
+ else if (unit < 2048) bytes += 2;
16
+ else if (unit >= 55296 && unit <= 56319 && i + 1 < text.length) {
17
+ const next = text.charCodeAt(i + 1);
18
+ if (next >= 56320 && next <= 57343) {
19
+ bytes += 4;
20
+ i++;
21
+ } else bytes += 3;
22
+ } else bytes += 3;
23
+ }
24
+ return bytes;
25
+ }
26
+ function headerBytes(length, fixLimit) {
27
+ if (length <= fixLimit) return 1;
28
+ if (length <= 255) return 2;
29
+ if (length <= 65535) return 3;
30
+ return 5;
31
+ }
32
+ function stringSize(text) {
33
+ const length = utf8ByteLength(text);
34
+ return headerBytes(length, 31) + length;
35
+ }
36
+ function binarySize(length) {
37
+ return (length <= 255 ? 2 : length <= 65535 ? 3 : 5) + length;
38
+ }
39
+ function containerHeaderSize(length) {
40
+ if (length <= 15) return 1;
41
+ return length <= 65535 ? 3 : 5;
42
+ }
43
+ function uintSize(value) {
44
+ if (value < 128) return 1;
45
+ if (value <= 255) return 2;
46
+ if (value <= 65535) return 3;
47
+ if (value <= 4294967295) return 5;
48
+ return 9;
49
+ }
50
+ var ExactWriter = class {
51
+ bytes;
52
+ view;
53
+ position;
54
+ encoder = new TextEncoder();
55
+ constructor(bytes, start) {
56
+ this.bytes = bytes;
57
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
58
+ this.position = start;
59
+ }
60
+ get offset() {
61
+ return this.position;
62
+ }
63
+ skip(length) {
64
+ this.position += length;
65
+ }
66
+ map(length) {
67
+ this.container(length, 128, 222, 223);
68
+ }
69
+ array(length) {
70
+ this.container(length, 144, 220, 221);
71
+ }
72
+ string(text) {
73
+ const length = utf8ByteLength(text);
74
+ if (length <= 31) this.bytes[this.position++] = 160 | length;
75
+ else if (length <= 255) {
76
+ this.bytes[this.position++] = 217;
77
+ this.bytes[this.position++] = length;
78
+ } else if (length <= 65535) {
79
+ this.bytes[this.position++] = 218;
80
+ this.view.setUint16(this.position, length);
81
+ this.position += 2;
82
+ } else {
83
+ this.bytes[this.position++] = 219;
84
+ this.view.setUint32(this.position, length);
85
+ this.position += 4;
86
+ }
87
+ const written = this.encoder.encodeInto(text, this.bytes.subarray(this.position, this.position + length)).written;
88
+ if (written !== length) throw new MalformedPayload("A string took a different number of bytes than measured");
89
+ this.position += length;
90
+ }
91
+ binary(source, offset, length) {
92
+ if (length <= 255) {
93
+ this.bytes[this.position++] = 196;
94
+ this.bytes[this.position++] = length;
95
+ } else if (length <= 65535) {
96
+ this.bytes[this.position++] = 197;
97
+ this.view.setUint16(this.position, length);
98
+ this.position += 2;
99
+ } else {
100
+ this.bytes[this.position++] = 198;
101
+ this.view.setUint32(this.position, length);
102
+ this.position += 4;
103
+ }
104
+ this.bytes.set(source.subarray(offset, offset + length), this.position);
105
+ this.position += length;
106
+ }
107
+ uint(value) {
108
+ if (value < 128) this.bytes[this.position++] = value;
109
+ else if (value <= 255) {
110
+ this.bytes[this.position++] = 204;
111
+ this.bytes[this.position++] = value;
112
+ } else if (value <= 65535) {
113
+ this.bytes[this.position++] = 205;
114
+ this.view.setUint16(this.position, value);
115
+ this.position += 2;
116
+ } else if (value <= 4294967295) {
117
+ this.bytes[this.position++] = 206;
118
+ this.view.setUint32(this.position, value);
119
+ this.position += 4;
120
+ } else {
121
+ this.bytes[this.position++] = 207;
122
+ this.view.setBigUint64(this.position, BigInt(value));
123
+ this.position += 8;
124
+ }
125
+ }
126
+ container(length, fix, sixteen, thirtyTwo) {
127
+ if (length <= 15) this.bytes[this.position++] = fix | length;
128
+ else if (length <= 65535) {
129
+ this.bytes[this.position++] = sixteen;
130
+ this.view.setUint16(this.position, length);
131
+ this.position += 2;
132
+ } else {
133
+ this.bytes[this.position++] = thirtyTwo;
134
+ this.view.setUint32(this.position, length);
135
+ this.position += 4;
136
+ }
137
+ }
138
+ };
139
+ var StrictReader = class {
140
+ bytes;
141
+ view;
142
+ position;
143
+ decoder = new TextDecoder("utf-8", { fatal: true });
144
+ constructor(bytes, start) {
145
+ this.bytes = bytes;
146
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
147
+ this.position = start;
148
+ }
149
+ get offset() {
150
+ return this.position;
151
+ }
152
+ get atEnd() {
153
+ return this.position === this.bytes.byteLength;
154
+ }
155
+ map() {
156
+ return this.container(128, 222, 223, "a map");
157
+ }
158
+ array() {
159
+ return this.container(144, 220, 221, "an array");
160
+ }
161
+ string() {
162
+ const tag = this.byte();
163
+ let length;
164
+ if ((tag & 224) === 160) length = tag & 31;
165
+ else if (tag === 217) length = this.byte();
166
+ else if (tag === 218) length = this.uint16();
167
+ else if (tag === 219) length = this.uint32();
168
+ else throw new MalformedPayload("Expected a string");
169
+ const end = this.take(length);
170
+ try {
171
+ return this.decoder.decode(this.bytes.subarray(end - length, end));
172
+ } catch {
173
+ throw new MalformedPayload("A string holds invalid UTF-8");
174
+ }
175
+ }
176
+ binary() {
177
+ const tag = this.byte();
178
+ let length;
179
+ if (tag === 196) length = this.byte();
180
+ else if (tag === 197) length = this.uint16();
181
+ else if (tag === 198) length = this.uint32();
182
+ else throw new MalformedPayload("Expected bytes");
183
+ const end = this.take(length);
184
+ return { offset: end - length, length };
185
+ }
186
+ uint() {
187
+ const tag = this.byte();
188
+ if (tag < 128) return tag;
189
+ if (tag === 204) return this.byte();
190
+ if (tag === 205) return this.uint16();
191
+ if (tag === 206) return this.uint32();
192
+ if (tag === 207) {
193
+ this.take(8);
194
+ const value = this.view.getBigUint64(this.position - 8);
195
+ if (value > BigInt(Number.MAX_SAFE_INTEGER)) throw new MalformedPayload("An integer is out of range");
196
+ return Number(value);
197
+ }
198
+ throw new MalformedPayload("Expected an unsigned integer");
199
+ }
200
+ container(fix, sixteen, thirtyTwo, what) {
201
+ const tag = this.byte();
202
+ if ((tag & 240) === fix) return tag & 15;
203
+ if (tag === sixteen) return this.uint16();
204
+ if (tag === thirtyTwo) return this.uint32();
205
+ throw new MalformedPayload(`Expected ${what}`);
206
+ }
207
+ take(length) {
208
+ const end = this.position + length;
209
+ if (end > this.bytes.byteLength) throw new MalformedPayload("The payload ends early");
210
+ this.position = end;
211
+ return end;
212
+ }
213
+ byte() {
214
+ this.take(1);
215
+ return this.bytes[this.position - 1];
216
+ }
217
+ uint16() {
218
+ this.take(2);
219
+ return this.view.getUint16(this.position - 2);
220
+ }
221
+ uint32() {
222
+ this.take(4);
223
+ return this.view.getUint32(this.position - 4);
224
+ }
225
+ };
226
+
227
+ // src/query-suggestions/counts-groups.ts
228
+ var FILE_LIMITS = {
229
+ minLetters: 1,
230
+ maxLength: MAX_MAX_LENGTH,
231
+ maxWords: MAX_MAX_WORDS,
232
+ maxDigits: MAX_MAX_LENGTH
233
+ };
234
+ var KEY_PHRASE = "phrase";
235
+ var KEY_COUNT = "count";
236
+ var KEY_PEOPLE = "people";
237
+ var MAX_UINT32 = 4294967295;
238
+ function readPeople(reader) {
239
+ const people = reader.array();
240
+ if (people > MAX_PEOPLE_PER_PHRASE_IN_FILE) throw new MalformedPayload("A phrase lists too many people");
241
+ const offsets = [];
242
+ for (let i = 0; i < people; i++) {
243
+ const { offset, length } = reader.binary();
244
+ if (length !== SCRAMBLED_ID_BYTES) throw new MalformedPayload("A scrambled ID has the wrong length");
245
+ offsets.push(offset);
246
+ }
247
+ return offsets;
248
+ }
249
+ function walkGroups(reader, payload, visitor, onGroup = null) {
250
+ const groupCount = reader.map();
251
+ const validating = visitor === null && onGroup === null;
252
+ const seenGroups = validating ? /* @__PURE__ */ new Set() : null;
253
+ for (let g = 0; g < groupCount; g++) {
254
+ const group = reader.string();
255
+ onGroup?.(group);
256
+ if (seenGroups !== null) {
257
+ if (!isGroup(group) || seenGroups.has(group)) throw new MalformedPayload("A group is invalid or repeated");
258
+ seenGroups.add(group);
259
+ }
260
+ const phraseCount = reader.array();
261
+ const seenPhrases = validating ? /* @__PURE__ */ new Set() : null;
262
+ for (let p = 0; p < phraseCount; p++) {
263
+ const fields = reader.map();
264
+ let phrase = null;
265
+ let count = null;
266
+ let offsets = null;
267
+ for (let f = 0; f < fields; f++) {
268
+ const key = reader.string();
269
+ if (key === KEY_PHRASE && phrase === null) phrase = reader.string();
270
+ else if (key === KEY_COUNT && count === null) count = reader.uint();
271
+ else if (key === KEY_PEOPLE && offsets === null) offsets = readPeople(reader);
272
+ else throw new MalformedPayload(`A phrase holds an unexpected or repeated key "${key}"`);
273
+ }
274
+ if (phrase === null || count === null || offsets === null) throw new MalformedPayload("A phrase lacks a key");
275
+ if (seenPhrases !== null) {
276
+ if (!isNormalPhrase(phrase, FILE_LIMITS) || seenPhrases.has(phrase)) {
277
+ throw new MalformedPayload("A phrase is invalid or repeated");
278
+ }
279
+ if (offsets.length > count || count > MAX_UINT32) {
280
+ throw new MalformedPayload("A phrase lists more people than its count");
281
+ }
282
+ seenPhrases.add(phrase);
283
+ }
284
+ visitor?.(group, phrase, count, { source: payload, offsets });
285
+ }
286
+ }
287
+ }
288
+
289
+ // src/query-suggestions/counts-file.ts
290
+ var DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
291
+ var WHAT = "query counts file";
292
+ var QUERY_COUNTS_FILE_FORMAT_VERSION = 1;
293
+ var KEY_DAY = "day";
294
+ var KEY_FINGERPRINT = "keyFingerprint";
295
+ var KEY_SAVED_AT = "savedAt";
296
+ var KEY_INDEX_UUID = "indexUuid";
297
+ var KEY_GROUPS = "groups";
298
+ var KEY_PHRASE2 = "phrase";
299
+ var KEY_COUNT2 = "count";
300
+ var KEY_PEOPLE2 = "people";
301
+ var REQUIRED_TOP_LEVEL_KEYS = 4;
302
+ var PHRASE_KEYS = 3;
303
+ function isDay(value) {
304
+ if (!DAY_PATTERN.test(value)) return false;
305
+ const parsed = /* @__PURE__ */ new Date(`${value}T00:00:00.000Z`);
306
+ return !Number.isNaN(parsed.getTime()) && parsed.toISOString().slice(0, 10) === value;
307
+ }
308
+ function topLevelSize(header) {
309
+ const keys = REQUIRED_TOP_LEVEL_KEYS + (header.indexUuid === null ? 0 : 1);
310
+ return containerHeaderSize(keys) + stringSize(KEY_DAY) + stringSize(header.day) + stringSize(KEY_FINGERPRINT) + binarySize(header.fingerprint.byteLength) + stringSize(KEY_SAVED_AT) + uintSize(header.savedAt) + (header.indexUuid === null ? 0 : stringSize(KEY_INDEX_UUID) + stringSize(header.indexUuid)) + stringSize(KEY_GROUPS);
311
+ }
312
+ function writeTopLevel(writer, header) {
313
+ writer.map(REQUIRED_TOP_LEVEL_KEYS + (header.indexUuid === null ? 0 : 1));
314
+ writer.string(KEY_DAY);
315
+ writer.string(header.day);
316
+ writer.string(KEY_FINGERPRINT);
317
+ writer.binary(header.fingerprint, 0, header.fingerprint.byteLength);
318
+ writer.string(KEY_SAVED_AT);
319
+ writer.uint(header.savedAt);
320
+ if (header.indexUuid !== null) {
321
+ writer.string(KEY_INDEX_UUID);
322
+ writer.string(header.indexUuid);
323
+ }
324
+ writer.string(KEY_GROUPS);
325
+ }
326
+ function groupsSize(counts) {
327
+ let size = containerHeaderSize(counts.groupTables.size);
328
+ for (const [group, phrases] of counts.groupTables) {
329
+ size += stringSize(group) + containerHeaderSize(phrases.size);
330
+ for (const [phrase, number] of phrases) {
331
+ const people = counts.peopleLengthOf(number);
332
+ size += containerHeaderSize(PHRASE_KEYS) + stringSize(KEY_PHRASE2) + stringSize(phrase) + stringSize(KEY_COUNT2) + uintSize(counts.countOf(number)) + stringSize(KEY_PEOPLE2) + containerHeaderSize(people) + people * binarySize(SCRAMBLED_ID_BYTES);
333
+ }
334
+ }
335
+ return size;
336
+ }
337
+ function writeGroups(writer, counts) {
338
+ writer.map(counts.groupTables.size);
339
+ const people = counts.peopleBytes;
340
+ for (const [group, phrases] of counts.groupTables) {
341
+ writer.string(group);
342
+ writer.array(phrases.size);
343
+ for (const [phrase, number] of phrases) {
344
+ writer.map(PHRASE_KEYS);
345
+ writer.string(KEY_PHRASE2);
346
+ writer.string(phrase);
347
+ writer.string(KEY_COUNT2);
348
+ writer.uint(counts.countOf(number));
349
+ writer.string(KEY_PEOPLE2);
350
+ const length = counts.peopleLengthOf(number);
351
+ const offset = counts.peopleOffsetOf(number);
352
+ writer.array(length);
353
+ for (let i = 0; i < length; i++) writer.binary(people, offset + i * SCRAMBLED_ID_BYTES, SCRAMBLED_ID_BYTES);
354
+ }
355
+ }
356
+ }
357
+ function sealed(bytes, writer) {
358
+ if (writer.offset !== bytes.byteLength) throw new MalformedPayload("The query counts file took an unexpected size");
359
+ if (bytes.byteLength > MAX_HISTORY_FILE_BYTES) {
360
+ throw new MalformedPayload(`A query counts file must stay at or below ${MAX_HISTORY_FILE_BYTES} bytes`);
361
+ }
362
+ const payload = bytes.subarray(HEADER_SIZE);
363
+ const packed = packEnvelopeWithChecksum([payload], QUERY_COUNTS_FILE_FORMAT_VERSION, WHAT, crc32(payload));
364
+ bytes.set(packed.chunks[0], 0);
365
+ return bytes;
366
+ }
367
+ function encodeCountsFile(counts, header) {
368
+ const full = { ...header, day: counts.day };
369
+ const bytes = new Uint8Array(HEADER_SIZE + topLevelSize(full) + groupsSize(counts));
370
+ const writer = new ExactWriter(bytes, HEADER_SIZE);
371
+ writeTopLevel(writer, full);
372
+ writeGroups(writer, counts);
373
+ return sealed(bytes, writer);
374
+ }
375
+ function rewrittenFile(header, groups) {
376
+ const bytes = new Uint8Array(HEADER_SIZE + topLevelSize(header) + groups.byteLength);
377
+ const writer = new ExactWriter(bytes, HEADER_SIZE);
378
+ writeTopLevel(writer, header);
379
+ bytes.set(groups, writer.offset);
380
+ writer.skip(groups.byteLength);
381
+ return sealed(bytes, writer);
382
+ }
383
+ function readTopLevel(payload) {
384
+ const reader = new StrictReader(payload, 0);
385
+ const keys = reader.map();
386
+ let day = null;
387
+ let fingerprint = null;
388
+ let savedAt = null;
389
+ let indexUuid = null;
390
+ let groupsStart = null;
391
+ let groupsEnd = 0;
392
+ const seen = /* @__PURE__ */ new Set();
393
+ for (let i = 0; i < keys; i++) {
394
+ const key = reader.string();
395
+ if (seen.has(key)) throw new MalformedPayload(`The file holds the key "${key}" twice`);
396
+ seen.add(key);
397
+ if (key === KEY_DAY) day = reader.string();
398
+ else if (key === KEY_FINGERPRINT) {
399
+ const { offset, length } = reader.binary();
400
+ fingerprint = payload.subarray(offset, offset + length);
401
+ } else if (key === KEY_SAVED_AT) savedAt = reader.uint();
402
+ else if (key === KEY_INDEX_UUID) {
403
+ indexUuid = reader.string();
404
+ if (indexUuid.length === 0 || indexUuid.length > MAX_INDEX_UUID_LENGTH) {
405
+ throw new MalformedPayload("The file holds an indexUuid of the wrong length");
406
+ }
407
+ } else if (key === KEY_GROUPS) {
408
+ groupsStart = reader.offset;
409
+ walkGroups(reader, payload, null);
410
+ groupsEnd = reader.offset;
411
+ } else throw new MalformedPayload(`The file holds an unexpected key "${key}"`);
412
+ }
413
+ if (!reader.atEnd) throw new MalformedPayload("The payload holds bytes after its map");
414
+ if (day === null || fingerprint === null || savedAt === null || groupsStart === null) {
415
+ throw new MalformedPayload("The file lacks a required key");
416
+ }
417
+ return { day, fingerprint, savedAt, indexUuid, groupsStart, groupsEnd };
418
+ }
419
+ async function openCountsFile(bytes, expectedDay, fingerprint) {
420
+ if (bytes.byteLength > MAX_HISTORY_FILE_BYTES) {
421
+ return { kind: "damaged", reason: `The file is longer than ${MAX_HISTORY_FILE_BYTES} bytes` };
422
+ }
423
+ let payload;
424
+ try {
425
+ payload = (await openChecksummedEnvelope(bytes, {
426
+ envelopeFormatVersion: QUERY_COUNTS_FILE_FORMAT_VERSION,
427
+ what: WHAT,
428
+ allowsCompression: false
429
+ })).payload;
430
+ } catch (error) {
431
+ return { kind: "damaged", reason: error instanceof Error ? error.message : String(error) };
432
+ }
433
+ let top;
434
+ try {
435
+ top = readTopLevel(payload);
436
+ } catch (error) {
437
+ if (error instanceof MalformedPayload) return { kind: "damaged", reason: error.message };
438
+ throw error;
439
+ }
440
+ if (!isDay(top.day) || expectedDay !== null && top.day !== expectedDay) {
441
+ return { kind: "damaged", reason: "The file holds a day that differs from its key" };
442
+ }
443
+ if (top.fingerprint.byteLength !== KEY_FINGERPRINT_BYTES) {
444
+ return { kind: "damaged", reason: "The key fingerprint has the wrong length" };
445
+ }
446
+ if (!sameBytes(top.fingerprint, fingerprint)) return { kind: "other-key" };
447
+ const { groupsStart, groupsEnd, ...header } = top;
448
+ return {
449
+ kind: "counts",
450
+ ...header,
451
+ visit(visitor) {
452
+ walkGroups(new StrictReader(payload, groupsStart), payload, visitor);
453
+ },
454
+ groupNames() {
455
+ const names = [];
456
+ walkGroups(new StrictReader(payload, groupsStart), payload, null, (group) => names.push(group));
457
+ return names;
458
+ },
459
+ withIndexUuid(indexUuid, savedAt) {
460
+ return rewrittenFile({ ...header, indexUuid, savedAt }, payload.subarray(groupsStart, groupsEnd));
461
+ }
462
+ };
463
+ }
464
+ function sameBytes(left, right) {
465
+ if (left.byteLength !== right.byteLength) return false;
466
+ for (let i = 0; i < left.byteLength; i++) if (left[i] !== right[i]) return false;
467
+ return true;
468
+ }
469
+
470
+ // src/query-suggestions/day-counts.ts
471
+ var EMPTY_SLOT = -1;
472
+ var NO_BLOCK = -1;
473
+ function nextPowerOfTwo(value) {
474
+ let power = 1;
475
+ while (power < value) power *= 2;
476
+ return power;
477
+ }
478
+ function idsEqual(left, leftOffset, right, rightOffset) {
479
+ for (let i = 0; i < SCRAMBLED_ID_BYTES; i++) {
480
+ if (left[leftOffset + i] !== right[rightOffset + i]) return false;
481
+ }
482
+ return true;
483
+ }
484
+ function hashOfId(id, offset) {
485
+ return (id[offset] | id[offset + 1] << 8 | id[offset + 2] << 16 | id[offset + 3] << 24) >>> 0;
486
+ }
487
+ var DayCounts = class {
488
+ day;
489
+ limits;
490
+ groups = /* @__PURE__ */ new Map();
491
+ phraseCount = 0;
492
+ phraseTexts = [];
493
+ phraseGroups = [];
494
+ phraseCounts = new Uint32Array(0);
495
+ phrasePeopleLengths = new Uint16Array(0);
496
+ phrasePeople = new Uint8Array(0);
497
+ personCount = 0;
498
+ personIds = new Uint8Array(0);
499
+ personPairs = new Uint16Array(0);
500
+ personHeads = new Int32Array(0);
501
+ personTails = new Int32Array(0);
502
+ personSlots = new Int32Array(0);
503
+ pairPages = [];
504
+ linkPages = [];
505
+ blockCount = 0;
506
+ changed = false;
507
+ constructor(day, limits) {
508
+ this.day = day;
509
+ this.limits = limits;
510
+ }
511
+ get phrases() {
512
+ return this.phraseCount;
513
+ }
514
+ get people() {
515
+ return this.personCount;
516
+ }
517
+ get groupNames() {
518
+ return this.groups.keys();
519
+ }
520
+ get isChanged() {
521
+ return this.changed;
522
+ }
523
+ markSaved() {
524
+ this.changed = false;
525
+ }
526
+ markChanged() {
527
+ this.changed = true;
528
+ }
529
+ get groupTables() {
530
+ return this.groups;
531
+ }
532
+ get peopleBytes() {
533
+ return this.phrasePeople;
534
+ }
535
+ countOf(phraseNumber) {
536
+ return this.phraseCounts[phraseNumber];
537
+ }
538
+ peopleLengthOf(phraseNumber) {
539
+ return this.phrasePeopleLengths[phraseNumber];
540
+ }
541
+ peopleOffsetOf(phraseNumber) {
542
+ return phraseNumber * this.limits.minPeople * SCRAMBLED_ID_BYTES;
543
+ }
544
+ hasGroup(group) {
545
+ return this.groups.has(group);
546
+ }
547
+ count(group, phrase, scrambledId, groupAllowed) {
548
+ const groupPhrases = this.groups.get(group);
549
+ const existingPhrase = groupPhrases?.get(phrase);
550
+ const person = this.findPerson(scrambledId);
551
+ if (person !== EMPTY_SLOT && existingPhrase !== void 0 && this.personHasPhrase(person, existingPhrase)) {
552
+ return "repeat";
553
+ }
554
+ if (person !== EMPTY_SLOT && this.personPairs[person] >= this.limits.maxPhrasesPerPerson) return "person-limit";
555
+ if (existingPhrase === void 0 && this.phraseCount >= this.limits.maxPhrasesPerDay) return "phrase-limit";
556
+ if (person === EMPTY_SLOT && this.personCount >= this.limits.maxPeoplePerDay) return "people-limit";
557
+ if (groupPhrases === void 0 && (!groupAllowed || this.groups.size >= this.limits.maxGroups)) {
558
+ return "group-limit";
559
+ }
560
+ const phraseNumber = existingPhrase ?? this.addPhrase(group, phrase, groupPhrases);
561
+ const personNumber = person === EMPTY_SLOT ? this.addPerson(scrambledId) : person;
562
+ this.appendPair(personNumber, phraseNumber);
563
+ this.phraseCounts[phraseNumber] += 1;
564
+ this.addToPeople(phraseNumber, scrambledId, 0);
565
+ this.changed = true;
566
+ return "recorded";
567
+ }
568
+ restorePhrase(group, phrase, count, people, peopleCount) {
569
+ const groupPhrases = this.groups.get(group);
570
+ if (groupPhrases?.has(phrase) === true) return false;
571
+ if (this.phraseCount >= this.limits.maxPhrasesPerDay) return false;
572
+ if (groupPhrases === void 0 && this.groups.size >= this.limits.maxGroups) return false;
573
+ const phraseNumber = this.addPhrase(group, phrase, groupPhrases);
574
+ for (let i = 0; i < peopleCount; i++) this.addToPeople(phraseNumber, people, i * SCRAMBLED_ID_BYTES);
575
+ this.phraseCounts[phraseNumber] = Math.max(count, this.phrasePeopleLengths[phraseNumber]);
576
+ return true;
577
+ }
578
+ addPhrase(group, phrase, existing) {
579
+ const groupPhrases = existing ?? /* @__PURE__ */ new Map();
580
+ if (existing === void 0) this.groups.set(group, groupPhrases);
581
+ if (this.phraseCount === this.phraseCounts.length) this.growPhrases();
582
+ const number = this.phraseCount;
583
+ this.phraseCount += 1;
584
+ groupPhrases.set(phrase, number);
585
+ this.phraseTexts.push(phrase);
586
+ this.phraseGroups.push(group);
587
+ return number;
588
+ }
589
+ growPhrases() {
590
+ const capacity = Math.min(
591
+ this.limits.maxPhrasesPerDay,
592
+ Math.max(MIN_PHRASE_TABLE_CAPACITY, this.phraseCounts.length * 2)
593
+ );
594
+ const counts = new Uint32Array(capacity);
595
+ counts.set(this.phraseCounts);
596
+ this.phraseCounts = counts;
597
+ const lengths = new Uint16Array(capacity);
598
+ lengths.set(this.phrasePeopleLengths);
599
+ this.phrasePeopleLengths = lengths;
600
+ const people = new Uint8Array(capacity * this.limits.minPeople * SCRAMBLED_ID_BYTES);
601
+ people.set(this.phrasePeople);
602
+ this.phrasePeople = people;
603
+ }
604
+ addToPeople(phraseNumber, id, idOffset) {
605
+ const length = this.phrasePeopleLengths[phraseNumber];
606
+ if (length >= this.limits.minPeople) return;
607
+ const base = phraseNumber * this.limits.minPeople * SCRAMBLED_ID_BYTES;
608
+ for (let i = 0; i < length; i++) {
609
+ if (idsEqual(this.phrasePeople, base + i * SCRAMBLED_ID_BYTES, id, idOffset)) return;
610
+ }
611
+ this.phrasePeople.set(id.subarray(idOffset, idOffset + SCRAMBLED_ID_BYTES), base + length * SCRAMBLED_ID_BYTES);
612
+ this.phrasePeopleLengths[phraseNumber] = length + 1;
613
+ }
614
+ findPerson(id) {
615
+ if (this.personSlots.length === 0) return EMPTY_SLOT;
616
+ const mask = this.personSlots.length - 1;
617
+ let slot = hashOfId(id, 0) & mask;
618
+ for (; ; ) {
619
+ const person = this.personSlots[slot];
620
+ if (person === EMPTY_SLOT) return EMPTY_SLOT;
621
+ if (idsEqual(this.personIds, person * SCRAMBLED_ID_BYTES, id, 0)) return person;
622
+ slot = slot + 1 & mask;
623
+ }
624
+ }
625
+ addPerson(id) {
626
+ if (this.personCount === this.personPairs.length) this.growPeople();
627
+ const person = this.personCount;
628
+ this.personCount += 1;
629
+ this.personIds.set(id.subarray(0, SCRAMBLED_ID_BYTES), person * SCRAMBLED_ID_BYTES);
630
+ this.personHeads[person] = NO_BLOCK;
631
+ this.personTails[person] = NO_BLOCK;
632
+ this.insertSlot(person);
633
+ return person;
634
+ }
635
+ insertSlot(person) {
636
+ const mask = this.personSlots.length - 1;
637
+ let slot = hashOfId(this.personIds, person * SCRAMBLED_ID_BYTES) & mask;
638
+ while (this.personSlots[slot] !== EMPTY_SLOT) slot = slot + 1 & mask;
639
+ this.personSlots[slot] = person;
640
+ }
641
+ growPeople() {
642
+ const capacity = Math.min(
643
+ this.limits.maxPeoplePerDay,
644
+ Math.max(PERSON_TABLE_MIN_SLOTS / 2, this.personPairs.length * 2)
645
+ );
646
+ const ids = new Uint8Array(capacity * SCRAMBLED_ID_BYTES);
647
+ ids.set(this.personIds);
648
+ this.personIds = ids;
649
+ const pairs = new Uint16Array(capacity);
650
+ pairs.set(this.personPairs);
651
+ this.personPairs = pairs;
652
+ const heads = new Int32Array(capacity);
653
+ heads.set(this.personHeads);
654
+ this.personHeads = heads;
655
+ const tails = new Int32Array(capacity);
656
+ tails.set(this.personTails);
657
+ this.personTails = tails;
658
+ this.personSlots = new Int32Array(nextPowerOfTwo(capacity * 2)).fill(EMPTY_SLOT);
659
+ for (let person = 0; person < this.personCount; person++) this.insertSlot(person);
660
+ }
661
+ personHasPhrase(person, phraseNumber) {
662
+ let remaining = this.personPairs[person];
663
+ let block = this.personHeads[person];
664
+ while (block !== NO_BLOCK && remaining > 0) {
665
+ const page = this.pairPages[Math.floor(block / PAIR_PAGE_BLOCKS)];
666
+ const start = block % PAIR_PAGE_BLOCKS * PAIR_BLOCK_SLOTS;
667
+ const used = Math.min(remaining, PAIR_BLOCK_SLOTS);
668
+ for (let i = 0; i < used; i++) {
669
+ if (page[start + i] === phraseNumber) return true;
670
+ }
671
+ remaining -= used;
672
+ block = this.linkPages[Math.floor(block / PAIR_PAGE_BLOCKS)][block % PAIR_PAGE_BLOCKS];
673
+ }
674
+ return false;
675
+ }
676
+ appendPair(person, phraseNumber) {
677
+ const pairs = this.personPairs[person];
678
+ const slot = pairs % PAIR_BLOCK_SLOTS;
679
+ let block = this.personTails[person];
680
+ if (slot === 0) {
681
+ const fresh = this.allocateBlock();
682
+ if (block === NO_BLOCK) this.personHeads[person] = fresh;
683
+ else this.linkPages[Math.floor(block / PAIR_PAGE_BLOCKS)][block % PAIR_PAGE_BLOCKS] = fresh;
684
+ this.personTails[person] = fresh;
685
+ block = fresh;
686
+ }
687
+ const page = this.pairPages[Math.floor(block / PAIR_PAGE_BLOCKS)];
688
+ page[block % PAIR_PAGE_BLOCKS * PAIR_BLOCK_SLOTS + slot] = phraseNumber;
689
+ this.personPairs[person] = pairs + 1;
690
+ }
691
+ allocateBlock() {
692
+ const block = this.blockCount;
693
+ if (block % PAIR_PAGE_BLOCKS === 0) {
694
+ this.pairPages.push(new Uint16Array(PAIR_PAGE_BLOCKS * PAIR_BLOCK_SLOTS));
695
+ this.linkPages.push(new Int32Array(PAIR_PAGE_BLOCKS).fill(NO_BLOCK));
696
+ }
697
+ this.blockCount += 1;
698
+ return block;
699
+ }
700
+ };
701
+
702
+ // src/query-suggestions/history-groups.ts
703
+ var HistoryGroups = class {
704
+ host;
705
+ fingerprint;
706
+ held = /* @__PURE__ */ new Map();
707
+ constructor(host, fingerprint) {
708
+ this.host = host;
709
+ this.fingerprint = fingerprint;
710
+ }
711
+ async of(indexName, cutoff) {
712
+ const held = this.held.get(indexName);
713
+ if (held !== void 0 && held.cutoff <= cutoff) {
714
+ if (held.cutoff < cutoff) {
715
+ for (const [group, day] of held.newestDay) if (day < cutoff) held.newestDay.delete(group);
716
+ held.cutoff = cutoff;
717
+ }
718
+ return held.newestDay.keys();
719
+ }
720
+ const newestDay = await this.load(indexName, cutoff);
721
+ this.held.set(indexName, { cutoff, newestDay });
722
+ return newestDay.keys();
723
+ }
724
+ note(indexName, day, groups) {
725
+ const held = this.held.get(indexName);
726
+ if (held === void 0 || day < held.cutoff) return;
727
+ for (const group of groups) {
728
+ const newest = held.newestDay.get(group);
729
+ if (newest === void 0 || newest < day) held.newestDay.set(group, day);
730
+ }
731
+ }
732
+ forget(indexName) {
733
+ this.held.delete(indexName);
734
+ }
735
+ async load(indexName, cutoff) {
736
+ const newestDay = /* @__PURE__ */ new Map();
737
+ const expectedUuid = this.host.view(indexName)?.indexUuid ?? null;
738
+ for (const key of await listHistoryFolder(this.host.files, indexName)) {
739
+ const file = historyFileOf(indexName, key);
740
+ if (file === null || file.day < cutoff) continue;
741
+ const bytes = await this.host.files.read(key);
742
+ if (!(bytes instanceof Uint8Array)) continue;
743
+ const opened = await openCountsFile(bytes, file.day, this.fingerprint);
744
+ if (opened.kind !== "counts") continue;
745
+ if (this.host.clustered && (opened.indexUuid === null || opened.indexUuid !== expectedUuid)) continue;
746
+ for (const group of opened.groupNames()) {
747
+ const newest = newestDay.get(group);
748
+ if (newest === void 0 || newest < file.day) newestDay.set(group, file.day);
749
+ }
750
+ }
751
+ return newestDay;
752
+ }
753
+ };
754
+
755
+ // src/query-suggestions/day-store.ts
756
+ function dayOf(instant) {
757
+ return new Date(instant).toISOString().slice(0, 10);
758
+ }
759
+ function limitsOf(setting) {
760
+ return {
761
+ minPeople: setting.minPeople,
762
+ maxPhrasesPerDay: setting.maxPhrasesPerDay,
763
+ maxPhrasesPerPerson: setting.maxPhrasesPerPerson,
764
+ maxPeoplePerDay: setting.maxPeoplePerDay,
765
+ maxGroups: setting.maxGroups
766
+ };
767
+ }
768
+ function sameLimits(left, right) {
769
+ return left.minPeople === right.minPeople && left.maxPhrasesPerDay === right.maxPhrasesPerDay && left.maxPhrasesPerPerson === right.maxPhrasesPerPerson && left.maxPeoplePerDay === right.maxPeoplePerDay && left.maxGroups === right.maxGroups;
770
+ }
771
+ var DayStore = class {
772
+ host;
773
+ scrambler;
774
+ history;
775
+ days = /* @__PURE__ */ new Map();
776
+ unsavedDays = /* @__PURE__ */ new Map();
777
+ turns = /* @__PURE__ */ new Map();
778
+ warnedLimits = /* @__PURE__ */ new Map();
779
+ generations = /* @__PURE__ */ new Map();
780
+ constructor(host, scrambler) {
781
+ this.host = host;
782
+ this.scrambler = scrambler;
783
+ this.history = new HistoryGroups(host, scrambler.fingerprint);
784
+ }
785
+ hasUnsaved(indexName) {
786
+ return this.days.get(indexName)?.isChanged === true || this.unsavedDays.has(indexName);
787
+ }
788
+ indexes() {
789
+ return [.../* @__PURE__ */ new Set([...this.days.keys(), ...this.unsavedDays.keys()])];
790
+ }
791
+ inTurn(indexName, work) {
792
+ const previous = this.turns.get(indexName) ?? Promise.resolve();
793
+ const next = previous.then(work, work);
794
+ const settled = next.catch(() => void 0);
795
+ this.turns.set(indexName, settled);
796
+ void settled.then(() => {
797
+ if (this.turns.get(indexName) === settled) this.turns.delete(indexName);
798
+ });
799
+ return next;
800
+ }
801
+ count(indexName, setting, now, request) {
802
+ return this.inTurn(indexName, async () => {
803
+ if (!request.stillEnabled()) return null;
804
+ if (request.isBanned()) return "banned";
805
+ const counts = await this.currentDay(indexName, setting, now);
806
+ const groupAllowed = counts.hasGroup(request.group) || request.groupAllowed(await this.knownGroups(indexName, counts, dayOf(now - setting.retentionDays * DAY_MS)));
807
+ const outcome = counts.count(request.group, request.phrase, this.scrambler.scramble(request.person), groupAllowed);
808
+ if (outcome === "phrase-limit" || outcome === "people-limit") this.warnLimit(indexName, counts.day, outcome);
809
+ return outcome;
810
+ });
811
+ }
812
+ save(indexName) {
813
+ return this.inTurn(indexName, () => this.saveNow(indexName));
814
+ }
815
+ retire(indexName, today) {
816
+ return this.inTurn(indexName, async () => {
817
+ await this.saveNow(indexName);
818
+ const counts = this.days.get(indexName);
819
+ if (counts === void 0 || counts.day >= today || counts.isChanged) return;
820
+ this.history.note(indexName, counts.day, counts.groupNames);
821
+ this.days.delete(indexName);
822
+ });
823
+ }
824
+ forgetHistoryGroups(indexName) {
825
+ return this.inTurn(indexName, async () => this.history.forget(indexName));
826
+ }
827
+ discard(indexName) {
828
+ return this.inTurn(indexName, async () => {
829
+ this.days.delete(indexName);
830
+ this.unsavedDays.delete(indexName);
831
+ this.history.forget(indexName);
832
+ this.warnedLimits.delete(indexName);
833
+ this.generations.set(indexName, (this.generations.get(indexName) ?? 0) + 1);
834
+ });
835
+ }
836
+ async knownGroups(indexName, counts, cutoff) {
837
+ const known = new Set(counts.groupNames);
838
+ for (const unsaved of this.unsavedDays.get(indexName) ?? []) {
839
+ for (const group of unsaved.groupNames) known.add(group);
840
+ }
841
+ for (const group of await this.history.of(indexName, cutoff)) known.add(group);
842
+ return known;
843
+ }
844
+ warnLimit(indexName, day, reason) {
845
+ const mark = `${day}:${reason}`;
846
+ const warned = this.warnedLimits.get(indexName) ?? "";
847
+ if (warned.split("|").includes(mark)) return;
848
+ this.warnedLimits.set(indexName, warned === "" ? mark : `${warned}|${mark}`);
849
+ this.host.emit("querySuggestionsWarning", { indexName, reason });
850
+ }
851
+ async currentDay(indexName, setting, now) {
852
+ const day = dayOf(now);
853
+ const limits = limitsOf(setting);
854
+ const current = this.days.get(indexName);
855
+ if (current !== void 0 && current.day === day && sameLimits(current.limits, limits)) return current;
856
+ if (current !== void 0) {
857
+ await this.saveNow(indexName);
858
+ if (current.isChanged) {
859
+ if (current.day === day) return current;
860
+ this.unsavedDays.set(indexName, [...this.unsavedDays.get(indexName) ?? [], current]);
861
+ }
862
+ this.history.note(indexName, current.day, current.groupNames);
863
+ this.days.delete(indexName);
864
+ }
865
+ const loaded = await this.load(indexName, day, limits);
866
+ this.days.set(indexName, loaded);
867
+ return loaded;
868
+ }
869
+ async load(indexName, day, limits) {
870
+ const counts = new DayCounts(day, limits);
871
+ const key = dayFileKey(indexName, day);
872
+ const bytes = await this.host.files.read(key);
873
+ if (bytes === null) return counts;
874
+ if (bytes === "too-long") {
875
+ this.host.emit("querySuggestionsWarning", { indexName, reason: "damaged-file", file: key });
876
+ return counts;
877
+ }
878
+ const opened = await openCountsFile(bytes, day, this.scrambler.fingerprint);
879
+ if (opened.kind !== "counts") {
880
+ const reason = opened.kind === "other-key" ? "other-key" : "damaged-file";
881
+ this.host.emit("querySuggestionsWarning", { indexName, reason, file: key });
882
+ return counts;
883
+ }
884
+ if (this.host.clustered && opened.indexUuid !== (this.host.view(indexName)?.indexUuid ?? null)) return counts;
885
+ opened.visit((group, phrase, count, people) => {
886
+ const ids = new Uint8Array(people.offsets.length * SCRAMBLED_ID_BYTES);
887
+ people.offsets.forEach((offset, i) => {
888
+ ids.set(people.source.subarray(offset, offset + SCRAMBLED_ID_BYTES), i * SCRAMBLED_ID_BYTES);
889
+ });
890
+ counts.restorePhrase(group, phrase, count, ids, people.offsets.length);
891
+ });
892
+ return counts;
893
+ }
894
+ async saveNow(indexName) {
895
+ const unsaved = this.unsavedDays.get(indexName);
896
+ if (unsaved !== void 0) {
897
+ const stillUnsaved = [];
898
+ for (const counts2 of unsaved) {
899
+ await this.write(indexName, counts2);
900
+ if (counts2.isChanged) stillUnsaved.push(counts2);
901
+ }
902
+ if (this.unsavedDays.get(indexName) === unsaved) {
903
+ if (stillUnsaved.length === 0) this.unsavedDays.delete(indexName);
904
+ else this.unsavedDays.set(indexName, stillUnsaved);
905
+ }
906
+ }
907
+ const counts = this.days.get(indexName);
908
+ if (counts !== void 0) await this.write(indexName, counts);
909
+ }
910
+ async write(indexName, counts) {
911
+ if (!counts.isChanged) return;
912
+ const generation = this.generations.get(indexName) ?? 0;
913
+ const view = this.host.view(indexName);
914
+ const bytes = encodeCountsFile(counts, {
915
+ fingerprint: this.scrambler.fingerprint,
916
+ savedAt: Date.now(),
917
+ indexUuid: this.host.clustered ? view?.indexUuid ?? null : null
918
+ });
919
+ counts.markSaved();
920
+ try {
921
+ if ((this.generations.get(indexName) ?? 0) !== generation) return;
922
+ await this.host.files.write(dayFileKey(indexName, counts.day), bytes);
923
+ } catch (error) {
924
+ counts.markChanged();
925
+ this.host.emit("querySuggestionsWarning", {
926
+ indexName,
927
+ reason: "save-failed",
928
+ message: error instanceof Error ? error.message : String(error)
929
+ });
930
+ }
931
+ }
932
+ };
933
+
934
+ // src/query-suggestions/restored-history.ts
935
+ async function collect(chunks) {
936
+ const parts = [];
937
+ let length = 0;
938
+ for await (const chunk of chunks) {
939
+ parts.push(chunk);
940
+ length += chunk.byteLength;
941
+ }
942
+ const bytes = new Uint8Array(length);
943
+ let offset = 0;
944
+ for (const part of parts) {
945
+ bytes.set(part, offset);
946
+ offset += part.byteLength;
947
+ }
948
+ return bytes;
949
+ }
950
+ var RestoredHistory = class {
951
+ runtime;
952
+ indexName;
953
+ cutoff;
954
+ prefix = restoreStagingPrefix();
955
+ staged = [];
956
+ received = 0;
957
+ constructor(runtime, indexName, setting) {
958
+ this.runtime = runtime;
959
+ this.indexName = indexName;
960
+ this.cutoff = dayOf(Date.now() - resolveSetting(setting).retentionDays * DAY_MS);
961
+ }
962
+ async add(chunks) {
963
+ const position = this.received;
964
+ this.received += 1;
965
+ const bytes = await collect(chunks);
966
+ const scrambler = this.runtime.scrambler;
967
+ if (scrambler === null) return;
968
+ const opened = await openCountsFile(bytes, null, scrambler.fingerprint);
969
+ if (opened.kind !== "counts") {
970
+ this.runtime.host.emit("querySuggestionsWarning", {
971
+ indexName: this.indexName,
972
+ reason: opened.kind === "other-key" ? "other-key" : "damaged-file",
973
+ file: `${QUERY_HISTORY_ARCHIVE_PREFIX}${position}`
974
+ });
975
+ return;
976
+ }
977
+ if (opened.day < this.cutoff) return;
978
+ const key = `${this.prefix}${position}.nrsl`;
979
+ const indexUuid = this.runtime.host.clustered ? this.runtime.host.view(this.indexName)?.indexUuid ?? null : null;
980
+ await this.runtime.host.files.write(key, opened.withIndexUuid(indexUuid, opened.savedAt));
981
+ this.staged.push({ key, day: opened.day });
982
+ }
983
+ async commit() {
984
+ if (this.received === 0) return;
985
+ const { files } = this.runtime.host;
986
+ await this.runtime.discardHistory(this.indexName);
987
+ const sequences = /* @__PURE__ */ new Map();
988
+ const ordered = [...this.staged].sort((left, right) => left.day < right.day ? -1 : left.day > right.day ? 1 : 0);
989
+ for (const file of ordered) {
990
+ const bytes = await files.read(file.key);
991
+ if (bytes instanceof Uint8Array) {
992
+ const sequence = sequences.get(file.day) ?? 0;
993
+ sequences.set(file.day, sequence + 1);
994
+ await files.write(restoredFileKey(this.indexName, file.day, sequence), bytes);
995
+ }
996
+ await files.remove(file.key);
997
+ }
998
+ this.staged.length = 0;
999
+ await this.runtime.days?.forgetHistoryGroups(this.indexName);
1000
+ }
1001
+ async abandon() {
1002
+ for (const file of this.staged) await this.runtime.host.files.remove(file.key);
1003
+ this.staged.length = 0;
1004
+ }
1005
+ };
1006
+
1007
+ export { DayStore, RestoredHistory, dayOf, openCountsFile, sameBytes };