@delali/narsil 0.3.1-next.99 → 0.4.1-next.211

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 (426) hide show
  1. package/NOTICE +45 -0
  2. package/README.md +30 -11
  3. package/dist/adapters/filesystem.d.ts +1 -1
  4. package/dist/adapters/filesystem.mjs +101 -13
  5. package/dist/adapters/gcs.d.ts +49 -0
  6. package/dist/adapters/gcs.mjs +213 -0
  7. package/dist/adapters/indexeddb.d.ts +1 -1
  8. package/dist/adapters/indexeddb.mjs +2 -7
  9. package/dist/adapters/memory.d.ts +1 -1
  10. package/dist/adapters/memory.mjs +5 -0
  11. package/dist/adapters/s3.d.ts +53 -0
  12. package/dist/adapters/s3.mjs +263 -0
  13. package/dist/{adapters-BQFQ4PRz.d.ts → adapters-pljAqmrC.d.ts} +33 -0
  14. package/dist/browser/chunk-4E5APHTM.mjs +1459 -0
  15. package/dist/browser/chunk-NDYADQ4Y.mjs +23426 -0
  16. package/dist/browser/chunk-QAOVAYAG.mjs +564 -0
  17. package/dist/browser/chunk-WC2DEXWU.mjs +29 -0
  18. package/dist/browser/chunk-YINZFPLT.mjs +1239 -0
  19. package/dist/browser/chunk-YJHGOJAC.mjs +15 -0
  20. package/dist/browser/{chunk-Z7QH2XHB.mjs → core-leaf-HWAXCEWX.mjs} +3987 -2103
  21. package/dist/browser/custom-token-results-R5BIX7SL.mjs +59 -0
  22. package/dist/browser/digest-leaf-64IPRPZD.mjs +151 -0
  23. package/dist/browser/durability-config-75NSFCQN.mjs +97 -0
  24. package/dist/browser/embed-GG6PS3EA.mjs +264 -0
  25. package/dist/browser/geo-index-GNHY4PUB.mjs +1484 -0
  26. package/dist/browser/gzip-JF3S6PPT.mjs +31 -0
  27. package/dist/browser/module-ZZTNG5JZ.mjs +5762 -0
  28. package/dist/browser/msgpack-leaf-44XNWSSY.mjs +302 -0
  29. package/dist/browser/optional-reads-XMVPG6HG.mjs +3230 -0
  30. package/dist/browser/optional-writes-76DWMRQH.mjs +3603 -0
  31. package/dist/browser/phrase-module-6UGFVW5J.mjs +617 -0
  32. package/dist/browser/resolve-vector-text-XRR2OVHE.mjs +85 -0
  33. package/dist/browser/restore-queue-SG5O735W.mjs +196 -0
  34. package/dist/browser/{runtime-NYE5TB6O.mjs → runtime-OMKE7DF4.mjs} +83 -25
  35. package/dist/browser/segment-codec-leaf-A427QJUX.mjs +554 -0
  36. package/dist/browser/segment-merge-leaf-7BZ54JPI.mjs +1636 -0
  37. package/dist/browser/storage-leaf-5UTR5K7J.mjs +19829 -0
  38. package/dist/browser/store-leaf-3L33V35P.mjs +2070 -0
  39. package/dist/browser/suggestions-leaf-UBNDVKBD.mjs +1430 -0
  40. package/dist/browser/worker-copies-JXSGYR5X.mjs +2951 -0
  41. package/dist/{capabilities-BTTIs96E.d.ts → capabilities-DMte5K9_.d.ts} +162 -18
  42. package/dist/chunk-2AFZE7Y3.mjs +72 -0
  43. package/dist/chunk-2CKDCC65.mjs +47 -0
  44. package/dist/chunk-3ECIRFS5.mjs +61 -0
  45. package/dist/chunk-44W3CBNG.mjs +7 -0
  46. package/dist/chunk-4H4RG2FR.mjs +13 -0
  47. package/dist/chunk-4PY4JYMY.mjs +11 -0
  48. package/dist/chunk-4ZPVAHWF.mjs +1289 -0
  49. package/dist/chunk-5EI5CXO6.mjs +197 -0
  50. package/dist/chunk-5F5MXWS5.mjs +283 -0
  51. package/dist/{chunk-OHP7QR33.mjs → chunk-5FLRXF73.mjs} +25 -4
  52. package/dist/chunk-6FOENO4E.mjs +273 -0
  53. package/dist/chunk-6GHX35ZM.mjs +108 -0
  54. package/dist/chunk-6MSZ543Y.mjs +32 -0
  55. package/dist/chunk-6N6HHBCQ.mjs +4596 -0
  56. package/dist/{chunk-LXPIXDUJ.mjs → chunk-7P5YWDRQ.mjs} +403 -169
  57. package/dist/{chunk-TGP5M5R7.mjs → chunk-7T4LAYT4.mjs} +1 -1
  58. package/dist/{chunk-GGSH6KBQ.mjs → chunk-ASZLNVWW.mjs} +23 -3
  59. package/dist/chunk-AVBPB3MI.mjs +602 -0
  60. package/dist/{chunk-UCDF35PU.mjs → chunk-AVGUEEQ3.mjs} +494 -245
  61. package/dist/chunk-BUMREPLU.mjs +15 -0
  62. package/dist/chunk-BY6NALRK.mjs +31921 -0
  63. package/dist/chunk-C4ZMXXTL.mjs +92 -0
  64. package/dist/{chunk-2WYMG3OZ.mjs → chunk-C74KPRR7.mjs} +274 -19
  65. package/dist/{chunk-RSRVGAIQ.mjs → chunk-D456ZFPQ.mjs} +3 -2
  66. package/dist/chunk-DXZ6JDWG.mjs +797 -0
  67. package/dist/chunk-FJXJZUWF.mjs +209 -0
  68. package/dist/chunk-FS3HPPSC.mjs +212 -0
  69. package/dist/chunk-FT4KLZXW.mjs +1164 -0
  70. package/dist/chunk-GR5GMFJJ.mjs +104 -0
  71. package/dist/chunk-GXCDWLQI.mjs +193 -0
  72. package/dist/chunk-GXED6R3Q.mjs +271 -0
  73. package/dist/chunk-HLPRHOSO.mjs +17 -0
  74. package/dist/chunk-IL5DRCNU.mjs +3041 -0
  75. package/dist/{chunk-P36MUFEJ.mjs → chunk-JDDXV6UF.mjs} +2 -1
  76. package/dist/chunk-K5HSUPYS.mjs +11 -0
  77. package/dist/chunk-KYIRXTCA.mjs +100 -0
  78. package/dist/chunk-LFXS6OF7.mjs +289 -0
  79. package/dist/chunk-LNGE7SLY.mjs +151 -0
  80. package/dist/chunk-M2Z5WVWW.mjs +194 -0
  81. package/dist/{chunk-45AVKHMJ.mjs → chunk-M3B7SGJB.mjs} +63 -50
  82. package/dist/chunk-M3OW2XSP.mjs +40 -0
  83. package/dist/chunk-MFPSQZ6P.mjs +127 -0
  84. package/dist/chunk-MRCJBWJZ.mjs +1292 -0
  85. package/dist/chunk-MSIN4NM3.mjs +25 -0
  86. package/dist/chunk-MW3CJVGP.mjs +219 -0
  87. package/dist/chunk-MYM6CAVK.mjs +76 -0
  88. package/dist/chunk-NJJN4FMM.mjs +84 -0
  89. package/dist/chunk-NM7I3BM7.mjs +59 -0
  90. package/dist/chunk-OIJ64LGT.mjs +19 -0
  91. package/dist/{chunk-K7D4ZTSW.mjs → chunk-P4F4UWLC.mjs} +1 -1
  92. package/dist/chunk-Q2AP2FSN.mjs +15 -0
  93. package/dist/chunk-R2PJKSNL.mjs +54 -0
  94. package/dist/{chunk-HXJBSHXF.mjs → chunk-R724DJSG.mjs} +5056 -2619
  95. package/dist/chunk-S2POFTLN.mjs +10 -0
  96. package/dist/chunk-S5QMWECL.mjs +7 -0
  97. package/dist/chunk-SLAC5PAB.mjs +5008 -0
  98. package/dist/chunk-T4HMSZCX.mjs +5 -0
  99. package/dist/chunk-TACP6VK6.mjs +111 -0
  100. package/dist/chunk-TGPKS2UM.mjs +125 -0
  101. package/dist/chunk-TKKXPKEO.mjs +979 -0
  102. package/dist/chunk-V6EUBAIT.mjs +19 -0
  103. package/dist/chunk-XL56L4IR.mjs +228 -0
  104. package/dist/chunk-XQIAMDAG.mjs +3076 -0
  105. package/dist/chunk-YTUZ4R22.mjs +1063 -0
  106. package/dist/{chunk-KV3AZFT3.mjs → chunk-ZKOQR7EA.mjs} +45 -1
  107. package/dist/chunk-ZPJFGGGI.mjs +638 -0
  108. package/dist/chunk-ZXX5GZEX.mjs +17 -0
  109. package/dist/{client-i9eLJrw5.d.ts → client-BKqtl0_V.d.ts} +442 -22
  110. package/dist/client.d.ts +11 -10
  111. package/dist/client.mjs +8 -898
  112. package/dist/config-Bnl7L-0H.d.ts +608 -0
  113. package/dist/{config-P__Diiy4.d.ts → config-CJ-jfI0a.d.ts} +119 -9
  114. package/dist/core-leaf-6JVHEVFB.mjs +8 -0
  115. package/dist/custom-token-results-NXHBVY7U.mjs +59 -0
  116. package/dist/digest-leaf-KXBGKYHZ.mjs +1 -0
  117. package/dist/direct-executor-V3WOYU2Y.mjs +17 -0
  118. package/dist/distribution/coordinator/etcd.d.ts +63 -7
  119. package/dist/distribution/coordinator/etcd.mjs +725 -81
  120. package/dist/distribution/coordinator/in-memory.d.ts +3 -3
  121. package/dist/distribution/coordinator/in-memory.mjs +65 -10
  122. package/dist/distribution/transport/grpc.d.ts +2 -2
  123. package/dist/distribution/transport/grpc.mjs +39 -10
  124. package/dist/distribution/transport/in-memory.d.ts +2 -2
  125. package/dist/distribution/transport/in-memory.mjs +9 -6
  126. package/dist/distribution/transport/tcp.d.ts +2 -2
  127. package/dist/distribution/transport/tcp.mjs +20 -15
  128. package/dist/distribution.d.ts +267 -56
  129. package/dist/distribution.mjs +23156 -11362
  130. package/dist/durability-config-VWS2K24E.mjs +96 -0
  131. package/dist/embed-4PHHKGBK.mjs +263 -0
  132. package/dist/embeddings/openai.d.ts +1 -1
  133. package/dist/embeddings/openai.mjs +2 -2
  134. package/dist/{memory-CUEDwr-F.d.ts → engine-qjAxJSKk.d.ts} +2254 -1123
  135. package/dist/geo-index-2ZMMWWSK.mjs +1484 -0
  136. package/dist/gzip-2UCZW2MM.mjs +31 -0
  137. package/dist/{index-lcdGLyJH.d.ts → index-B_vmxpgW.d.ts} +260 -102
  138. package/dist/index.browser.mjs +4 -4
  139. package/dist/index.d.ts +2133 -57
  140. package/dist/index.mjs +16 -32
  141. package/dist/index.react-native.mjs +12 -0
  142. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  143. package/dist/invalidation/filesystem.d.ts +1 -1
  144. package/dist/invalidation/filesystem.mjs +1 -2
  145. package/dist/invalidation/noop.d.ts +1 -1
  146. package/dist/languages/albanian.mjs +1 -1
  147. package/dist/languages/amharic.mjs +1 -1
  148. package/dist/languages/arabic.mjs +1 -1
  149. package/dist/languages/armenian.mjs +1 -1
  150. package/dist/languages/azerbaijani.mjs +1 -1
  151. package/dist/languages/bambara.mjs +1 -1
  152. package/dist/languages/basque.mjs +1 -1
  153. package/dist/languages/belarusian.mjs +1 -1
  154. package/dist/languages/bengali.mjs +1 -1
  155. package/dist/languages/bosnian.mjs +1 -1
  156. package/dist/languages/breton.mjs +1 -1
  157. package/dist/languages/bulgarian.mjs +1 -1
  158. package/dist/languages/burmese.mjs +1 -1
  159. package/dist/languages/catalan.mjs +1 -1
  160. package/dist/languages/chinese.mjs +1 -1
  161. package/dist/languages/croatian.mjs +1 -1
  162. package/dist/languages/czech.mjs +1 -1
  163. package/dist/languages/dagbani.mjs +1 -1
  164. package/dist/languages/danish.mjs +1 -1
  165. package/dist/languages/dutch.mjs +1 -1
  166. package/dist/languages/english.mjs +1 -1
  167. package/dist/languages/esperanto.mjs +1 -1
  168. package/dist/languages/estonian.mjs +1 -1
  169. package/dist/languages/ewe.mjs +1 -1
  170. package/dist/languages/faroese.mjs +1 -1
  171. package/dist/languages/fijian.mjs +1 -1
  172. package/dist/languages/finnish.mjs +1 -1
  173. package/dist/languages/french.mjs +1 -1
  174. package/dist/languages/ga.mjs +1 -1
  175. package/dist/languages/galician.mjs +1 -1
  176. package/dist/languages/georgian.mjs +1 -1
  177. package/dist/languages/german.mjs +1 -1
  178. package/dist/languages/greek.mjs +1 -1
  179. package/dist/languages/guarani.mjs +1 -1
  180. package/dist/languages/gujarati.mjs +1 -1
  181. package/dist/languages/haitian-creole.mjs +1 -1
  182. package/dist/languages/hausa.mjs +1 -1
  183. package/dist/languages/hawaiian.mjs +1 -1
  184. package/dist/languages/hebrew.mjs +1 -1
  185. package/dist/languages/hindi.mjs +1 -1
  186. package/dist/languages/hungarian.mjs +1 -1
  187. package/dist/languages/icelandic.mjs +1 -1
  188. package/dist/languages/igbo.mjs +1 -1
  189. package/dist/languages/indonesian.mjs +1 -1
  190. package/dist/languages/irish.mjs +1 -1
  191. package/dist/languages/italian.mjs +1 -1
  192. package/dist/languages/japanese.mjs +1 -1
  193. package/dist/languages/kannada.mjs +1 -1
  194. package/dist/languages/kazakh.mjs +1 -1
  195. package/dist/languages/khmer.mjs +1 -1
  196. package/dist/languages/kinyarwanda.mjs +1 -1
  197. package/dist/languages/kirundi.mjs +1 -1
  198. package/dist/languages/korean.mjs +1 -1
  199. package/dist/languages/kurmanji.mjs +1 -1
  200. package/dist/languages/kyrgyz.mjs +1 -1
  201. package/dist/languages/lao.mjs +1 -1
  202. package/dist/languages/latin.mjs +1 -1
  203. package/dist/languages/latvian.mjs +1 -1
  204. package/dist/languages/lingala.mjs +1 -1
  205. package/dist/languages/lithuanian.mjs +1 -1
  206. package/dist/languages/luxembourgish.mjs +1 -1
  207. package/dist/languages/macedonian.mjs +1 -1
  208. package/dist/languages/malagasy.mjs +1 -1
  209. package/dist/languages/malay.mjs +1 -1
  210. package/dist/languages/malayalam.mjs +1 -1
  211. package/dist/languages/maltese.mjs +1 -1
  212. package/dist/languages/maori.mjs +1 -1
  213. package/dist/languages/marathi.mjs +1 -1
  214. package/dist/languages/nepali.mjs +1 -1
  215. package/dist/languages/norwegian.mjs +1 -1
  216. package/dist/languages/oromo.mjs +1 -1
  217. package/dist/languages/persian.mjs +1 -1
  218. package/dist/languages/polish.mjs +1 -1
  219. package/dist/languages/portuguese.mjs +1 -1
  220. package/dist/languages/punjabi.mjs +1 -1
  221. package/dist/languages/romanian.mjs +1 -1
  222. package/dist/languages/russian.mjs +1 -1
  223. package/dist/languages/samoan.mjs +1 -1
  224. package/dist/languages/sanskrit.mjs +1 -1
  225. package/dist/languages/scottish-gaelic.mjs +1 -1
  226. package/dist/languages/serbian.mjs +1 -1
  227. package/dist/languages/shona.mjs +1 -1
  228. package/dist/languages/sinhala.mjs +1 -1
  229. package/dist/languages/slovak.mjs +1 -1
  230. package/dist/languages/slovenian.mjs +1 -1
  231. package/dist/languages/sorani.mjs +1 -1
  232. package/dist/languages/spanish.mjs +1 -1
  233. package/dist/languages/swahili.mjs +1 -1
  234. package/dist/languages/swedish.mjs +1 -1
  235. package/dist/languages/tagalog.mjs +1 -1
  236. package/dist/languages/tamil.mjs +1 -1
  237. package/dist/languages/tatar.mjs +1 -1
  238. package/dist/languages/telugu.mjs +1 -1
  239. package/dist/languages/thai.mjs +1 -1
  240. package/dist/languages/tibetan.mjs +1 -1
  241. package/dist/languages/tigrinya.mjs +1 -1
  242. package/dist/languages/tongan.mjs +1 -1
  243. package/dist/languages/turkish.mjs +1 -1
  244. package/dist/languages/twi.mjs +1 -1
  245. package/dist/languages/ukrainian.mjs +1 -1
  246. package/dist/languages/urdu.mjs +1 -1
  247. package/dist/languages/vietnamese.mjs +1 -1
  248. package/dist/languages/welsh.mjs +1 -1
  249. package/dist/languages/wolof.mjs +1 -1
  250. package/dist/languages/xhosa.mjs +1 -1
  251. package/dist/languages/yoruba.mjs +1 -1
  252. package/dist/languages/zulu.mjs +1 -1
  253. package/dist/long-query-thread-UCRK7MSI.mjs +154 -0
  254. package/dist/module-5ZSLUQII.mjs +3729 -0
  255. package/dist/msgpack-leaf-H5ZYW7S7.mjs +9 -0
  256. package/dist/optional-reads-5HFMF2QZ.mjs +3373 -0
  257. package/dist/optional-writes-4UOQKETU.mjs +247 -0
  258. package/dist/paged-partition-archive-BBRLZCIF.mjs +253 -0
  259. package/dist/partial-words.browser.mjs +1650 -0
  260. package/dist/partial-words.mjs +17 -11
  261. package/dist/{chunk-ASY5PHYI.mjs → pattern.browser.mjs} +253 -311
  262. package/dist/pattern.mjs +6 -7
  263. package/dist/persistence/durability/checkpoint-worker.d.ts +93 -25
  264. package/dist/persistence/durability/checkpoint-worker.mjs +51 -41
  265. package/dist/phrase-module-EFESWOCR.mjs +635 -0
  266. package/dist/query-actions-HMJUVWGT.mjs +119 -0
  267. package/dist/query-suggestions/merge-worker.d.ts +55 -0
  268. package/dist/query-suggestions/merge-worker.mjs +53 -0
  269. package/dist/query-suggestions-CBYkrQ5T.d.ts +311 -0
  270. package/dist/query-suggestions.browser.mjs +47 -0
  271. package/dist/query-suggestions.d.ts +20 -0
  272. package/dist/query-suggestions.mjs +19 -0
  273. package/dist/react-native/chunks/chunk-3OEFSHVV.mjs +970 -0
  274. package/dist/{browser/chunk-RELZRJNR.mjs → react-native/chunks/chunk-43R4GCM7.mjs} +49 -50
  275. package/dist/react-native/chunks/chunk-AKRYHAVG.mjs +6729 -0
  276. package/dist/react-native/chunks/chunk-CW2ZFXSE.mjs +14 -0
  277. package/dist/react-native/chunks/chunk-G3WCDTOF.mjs +29640 -0
  278. package/dist/react-native/chunks/chunk-GXXHHA2T.mjs +212 -0
  279. package/dist/react-native/chunks/chunk-H3MDVPBO.mjs +151 -0
  280. package/dist/react-native/chunks/chunk-HQEVQEDJ.mjs +52 -0
  281. package/dist/react-native/chunks/chunk-HRDMIQ63.mjs +131 -0
  282. package/dist/react-native/chunks/chunk-HTBY4GON.mjs +565 -0
  283. package/dist/react-native/chunks/chunk-LX62Y4B5.mjs +61 -0
  284. package/dist/react-native/chunks/chunk-MRH36RN3.mjs +902 -0
  285. package/dist/react-native/chunks/chunk-Q32DDFKX.mjs +19 -0
  286. package/dist/react-native/chunks/chunk-UE4ANHSX.mjs +127 -0
  287. package/dist/react-native/chunks/chunk-UNZPWLVL.mjs +1063 -0
  288. package/dist/react-native/chunks/chunk-UO44MA5M.mjs +232 -0
  289. package/dist/react-native/chunks/chunk-UYKIV2KT.mjs +42 -0
  290. package/dist/react-native/chunks/chunk-VUJCSQG5.mjs +11 -0
  291. package/dist/react-native/chunks/chunk-WVHF4ZAK.mjs +802 -0
  292. package/dist/react-native/chunks/chunk-ZUJCUPVS.mjs +54 -0
  293. package/dist/react-native/chunks/core-leaf-S2KNBWDY.mjs +7 -0
  294. package/dist/react-native/chunks/custom-token-results-3OZEMRE7.mjs +59 -0
  295. package/dist/react-native/chunks/digest-leaf-X6OIFDIU.mjs +1 -0
  296. package/dist/react-native/chunks/durability-config-FCSD4MXY.mjs +96 -0
  297. package/dist/react-native/chunks/embed-HA2I7RCF.mjs +263 -0
  298. package/dist/react-native/chunks/geo-index-QEYKEVPH.mjs +1484 -0
  299. package/dist/react-native/chunks/gzip.react-native-CVM4NQG2.mjs +12 -0
  300. package/dist/react-native/chunks/module-FMBX75GP.mjs +5209 -0
  301. package/dist/react-native/chunks/msgpack-leaf-JMNGSTRW.mjs +7 -0
  302. package/dist/react-native/chunks/optional-reads-AFPO2FEB.mjs +3364 -0
  303. package/dist/react-native/chunks/optional-writes-XOH25XZI.mjs +296 -0
  304. package/dist/react-native/chunks/phrase-module-IGYBK7XH.mjs +636 -0
  305. package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
  306. package/dist/react-native/chunks/resolve-vector-text-GE2CRJKI.mjs +84 -0
  307. package/dist/react-native/chunks/restore-queue-LLNMYB4Z.mjs +187 -0
  308. package/dist/react-native/chunks/runtime-UOHD2XUJ.mjs +615 -0
  309. package/dist/react-native/chunks/segment-codec-leaf-VR6O5GLC.mjs +13 -0
  310. package/dist/react-native/chunks/segment-merge-leaf-LLMWU4EW.mjs +14 -0
  311. package/dist/react-native/chunks/storage-leaf-JD7PIDO4.mjs +19633 -0
  312. package/dist/react-native/chunks/store-leaf-4UVCMYGN.mjs +15 -0
  313. package/dist/react-native/chunks/suggestions-leaf-J5VT5RHY.mjs +1321 -0
  314. package/dist/react-native/chunks/worker-copies-ZYHY7FKJ.mjs +2831 -0
  315. package/dist/react-native/partial-words.mjs +1658 -0
  316. package/dist/react-native/pattern.mjs +2570 -0
  317. package/dist/react-native/query-suggestions.mjs +55 -0
  318. package/dist/react-native/sync.mjs +2759 -0
  319. package/dist/react-native/synonyms.mjs +676 -0
  320. package/dist/react.d.ts +12 -8
  321. package/dist/react.mjs +45 -1
  322. package/dist/{rerank-NWTKK636.mjs → rerank-RI2PMNOD.mjs} +22 -12
  323. package/dist/rerankers/http.d.ts +1 -1
  324. package/dist/rerankers/http.mjs +2 -2
  325. package/dist/resolve-vector-text-OD4J3WHF.mjs +3 -0
  326. package/dist/restore-queue-PCKWDAKQ.mjs +192 -0
  327. package/dist/runtime-USQYKN3I.mjs +615 -0
  328. package/dist/{schema-4W_7tk1f.d.ts → schema-CzG9OUw5.d.ts} +70 -30
  329. package/dist/segment-codec-leaf-QJM7I6QG.mjs +18 -0
  330. package/dist/segment-merge-leaf-4BJGIJHY.mjs +18 -0
  331. package/dist/server.d.ts +348 -22
  332. package/dist/server.mjs +2211 -327
  333. package/dist/storage-leaf-GRRRI7UV.mjs +5962 -0
  334. package/dist/store-leaf-6VMMSVBE.mjs +20 -0
  335. package/dist/stored-values-BWIMpiT_.d.ts +25 -0
  336. package/dist/suggestions-leaf-4KPTMPTL.mjs +54 -0
  337. package/dist/sync/filesystem.d.ts +30 -0
  338. package/dist/sync/filesystem.mjs +288 -0
  339. package/dist/sync/indexeddb.browser.mjs +287 -0
  340. package/dist/sync/indexeddb.d.ts +30 -0
  341. package/dist/sync/indexeddb.mjs +242 -0
  342. package/dist/sync.browser.mjs +1271 -0
  343. package/dist/sync.d.ts +23 -0
  344. package/dist/sync.mjs +2712 -0
  345. package/dist/synonyms.browser.mjs +658 -0
  346. package/dist/synonyms.mjs +17 -7
  347. package/dist/tasks-65b6t6lk.d.ts +782 -0
  348. package/dist/thread-UJ3W5LVI.mjs +31 -0
  349. package/dist/{types-B3b68nfF.d.ts → types-Ck97nB3D.d.ts} +1 -1
  350. package/dist/types-CpCg-Gq-.d.ts +227 -0
  351. package/dist/{types-Cb9kiY5P.d.ts → types-DvncmvuQ.d.ts} +12 -1
  352. package/dist/vector/search-worker.d.ts +10 -2
  353. package/dist/vector/search-worker.mjs +11 -11
  354. package/dist/worker-copies-YSXZXIGC.mjs +2232 -0
  355. package/dist/workers/entry.mjs +75 -15
  356. package/openapi.json +6840 -0
  357. package/package.json +404 -19
  358. package/dist/browser/chunk-CN4I4FZU.mjs +0 -602
  359. package/dist/browser/chunk-HOZW3PKP.mjs +0 -9
  360. package/dist/browser/chunk-JPTQZWJQ.mjs +0 -10
  361. package/dist/browser/chunk-LD55ZGU3.mjs +0 -347
  362. package/dist/browser/chunk-NAFT3K6X.mjs +0 -24546
  363. package/dist/browser/chunk-NNG3EEB6.mjs +0 -2728
  364. package/dist/browser/chunk-QWES44AW.mjs +0 -473
  365. package/dist/browser/durability-wiring-PY3L7DES.mjs +0 -3679
  366. package/dist/browser/module-PDXYHT75.mjs +0 -2822
  367. package/dist/browser/release-76MXRPGV.mjs +0 -33
  368. package/dist/browser/restore-queue-DVIYJ7RU.mjs +0 -35
  369. package/dist/browser/snapshot-J57MZK6L.mjs +0 -1207
  370. package/dist/browser/write-L7FAQA47.mjs +0 -208
  371. package/dist/chunk-37CD3OKS.mjs +0 -87
  372. package/dist/chunk-5XNIRLFC.mjs +0 -96
  373. package/dist/chunk-7X5JST5L.mjs +0 -114
  374. package/dist/chunk-AV4IJKD2.mjs +0 -35
  375. package/dist/chunk-AY3AQ3UH.mjs +0 -73
  376. package/dist/chunk-BKB3XTMI.mjs +0 -707
  377. package/dist/chunk-C7SLA6CM.mjs +0 -19
  378. package/dist/chunk-COVCQ73F.mjs +0 -587
  379. package/dist/chunk-DJHHF2L5.mjs +0 -17
  380. package/dist/chunk-E74YLAIG.mjs +0 -24
  381. package/dist/chunk-FMWRCP2C.mjs +0 -57
  382. package/dist/chunk-GES7U2PM.mjs +0 -68
  383. package/dist/chunk-HJSN7UYG.mjs +0 -10
  384. package/dist/chunk-JFRNNBOM.mjs +0 -35
  385. package/dist/chunk-K25LZA54.mjs +0 -8484
  386. package/dist/chunk-KEHK2O6P.mjs +0 -13
  387. package/dist/chunk-KOKM4PQN.mjs +0 -52
  388. package/dist/chunk-KZ3NX2P7.mjs +0 -21
  389. package/dist/chunk-L3L5YFXC.mjs +0 -614
  390. package/dist/chunk-L5ORED7V.mjs +0 -25
  391. package/dist/chunk-LH4NZNJJ.mjs +0 -1731
  392. package/dist/chunk-LU2CE5AE.mjs +0 -507
  393. package/dist/chunk-LXWDQLTW.mjs +0 -30
  394. package/dist/chunk-MHOSCQCQ.mjs +0 -948
  395. package/dist/chunk-NKRBEGJZ.mjs +0 -1705
  396. package/dist/chunk-NTRMFARG.mjs +0 -462
  397. package/dist/chunk-NW4XDSP2.mjs +0 -212
  398. package/dist/chunk-OOEXLUGR.mjs +0 -117
  399. package/dist/chunk-ORBLD3EL.mjs +0 -6
  400. package/dist/chunk-OWNZYMRE.mjs +0 -3591
  401. package/dist/chunk-PPY2GSIB.mjs +0 -44
  402. package/dist/chunk-QY7GC2MW.mjs +0 -33
  403. package/dist/chunk-R6A773WK.mjs +0 -226
  404. package/dist/chunk-R6PR4WYZ.mjs +0 -68
  405. package/dist/chunk-R7EEKAJE.mjs +0 -593
  406. package/dist/chunk-RONEOEON.mjs +0 -426
  407. package/dist/chunk-VWULPWRJ.mjs +0 -7469
  408. package/dist/chunk-WCJKUGHB.mjs +0 -262
  409. package/dist/chunk-WQBCBKY3.mjs +0 -8
  410. package/dist/chunk-X5CV2E2B.mjs +0 -1
  411. package/dist/chunk-YA7XLE2R.mjs +0 -1256
  412. package/dist/chunk-YTJCGKM4.mjs +0 -56
  413. package/dist/chunk-ZG477IPV.mjs +0 -526
  414. package/dist/chunk-ZYNAMIYX.mjs +0 -113
  415. package/dist/config-BGB_fDtq.d.ts +0 -237
  416. package/dist/direct-executor-CXZ66ACP.mjs +0 -22
  417. package/dist/durability-wiring-QTHOGFMZ.mjs +0 -3229
  418. package/dist/engine-BGCEm9E_.d.ts +0 -577
  419. package/dist/module-3NONKBZX.mjs +0 -1860
  420. package/dist/release-BH3DKW3T.mjs +0 -33
  421. package/dist/restore-queue-JPRBUYII.mjs +0 -35
  422. package/dist/runtime-ZKYZFJJC.mjs +0 -177
  423. package/dist/snapshot-HOGXNO5W.mjs +0 -793
  424. package/dist/tasks-HG3rlxx6.d.ts +0 -302
  425. package/dist/thread-B657LQXK.mjs +0 -692
  426. package/dist/write-XLIGTXBU.mjs +0 -238
@@ -0,0 +1,1658 @@
1
+ import { spellingCountsOf, encodePartialSpellings, countSpelling, SurfaceTable, SURFACE_TABLE_START_CAPACITY, StringPlan, SegmentRefusal, reserveStrings, grownCapacity, PARTIAL_WORD_RUN_CODE_POINTS, encodeStringBlob, sameFields, countDocumentSpellings, tokenize, GROWING_PARTIAL_ENTRY_BYTES, GROWING_PARTIAL_RUN_BYTES, GROWING_PARTIAL_RUN_MEMBER_BYTES, GROWING_PARTIAL_PENDING_ENTRY_BYTES, RUN_TABLE_START_CAPACITY, TERM_REGION_GROWTH_FACTOR, RUN_MEMBER_START_CAPACITY, SLICE_CAPACITIES, TERM_SLOT_LOAD_DENOMINATOR, TERM_SLOT_LOAD_NUMERATOR, PoolPlan, requireRoom, releaseRange, requireSegment, termText, partitionDocumentFrequency, visitDocumentSpellings, visitDocumentSurfaceSpellings, resolveStopWords, transformToken, expansionSource, liveFrequency, codePointCountOf, GROWING_PARTIAL_PENDING_ENTRIES_PER_ENTRY, typedRegion, regionsOverlap, mergeText } from './chunks/chunk-G3WCDTOF.mjs';
2
+ import './chunks/chunk-HQEVQEDJ.mjs';
3
+ import { FNV_OFFSET_BASIS, FNV_PRIME } from './chunks/chunk-AKRYHAVG.mjs';
4
+ import { PARTIAL_WORD_MIN_CODE_POINTS, PARTIAL_WORDS_ADDED_PER_QUERY, DEFAULT_FUZZY_PREFIX_LENGTH } from './chunks/chunk-LX62Y4B5.mjs';
5
+ import { fuzzyTermMatches, storedText } from './chunks/chunk-HTBY4GON.mjs';
6
+ import './chunks/chunk-UYKIV2KT.mjs';
7
+ import './chunks/chunk-CW2ZFXSE.mjs';
8
+ import './chunks/chunk-UO44MA5M.mjs';
9
+ import './chunks/chunk-MRH36RN3.mjs';
10
+ import { compareCodePoints, codePointCount } from './chunks/chunk-HRDMIQ63.mjs';
11
+ import './chunks/chunk-VUJCSQG5.mjs';
12
+ import { NarsilError, ErrorCodes } from './chunks/chunk-43R4GCM7.mjs';
13
+
14
+ // src/core/expansion/partial-bindings.ts
15
+ var PARTIAL_BUDGET;
16
+ var PARTIAL_RUN_POINTS;
17
+ var captureExpansionSource;
18
+ var expansionDictionary;
19
+ var expansionText;
20
+ var expansionToken;
21
+ var readExpansion;
22
+ var validExpansionText;
23
+ function bindPartialExpansionCode(code) {
24
+ ({
25
+ PARTIAL_BUDGET,
26
+ PARTIAL_RUN_POINTS,
27
+ captureExpansionSource,
28
+ expansionDictionary,
29
+ expansionText,
30
+ expansionToken,
31
+ readExpansion,
32
+ validExpansionText
33
+ } = code);
34
+ }
35
+
36
+ // src/core/segment-writes/runs.ts
37
+ function hashRun(points) {
38
+ let hash = FNV_OFFSET_BASIS;
39
+ for (const point of points) {
40
+ for (let byte = 0; byte < 4; byte++) hash = Math.imul(hash ^ point >>> byte * 8 & 255, FNV_PRIME);
41
+ }
42
+ return hash >>> 0;
43
+ }
44
+ function compareRuns(left, right) {
45
+ for (let at = 0; at < 3; at++) if (left[at] !== right[at]) return left[at] < right[at] ? -1 : 1;
46
+ return 0;
47
+ }
48
+ function equalRun(table, ordinal, points) {
49
+ for (let at = 0; at < 3; at++) if (table.runs[ordinal * 3 + at] !== points[at]) return false;
50
+ return true;
51
+ }
52
+ function collectRuns(spellings) {
53
+ const members = [];
54
+ for (let at = 0; at < spellings.size; at++) {
55
+ if (spellings.added[at] === 0 || spellings.points[at] <= 3) continue;
56
+ const spelling = spellings.ordinals[at];
57
+ let first = 0;
58
+ let second = 0;
59
+ let seen = 0;
60
+ for (const character of spellings.texts[at]) {
61
+ const point = character.codePointAt(0) ?? 0;
62
+ if (++seen >= 3) members.push({ points: [first, second, point], spelling });
63
+ first = second;
64
+ second = point;
65
+ }
66
+ }
67
+ members.sort((left, right) => compareRuns(left.points, right.points) || left.spelling - right.spelling);
68
+ let kept = 0;
69
+ for (const member of members) {
70
+ const previous = members[kept - 1];
71
+ if (previous === void 0 || compareRuns(previous.points, member.points) !== 0 || previous.spelling !== member.spelling)
72
+ members[kept++] = member;
73
+ }
74
+ members.length = kept;
75
+ return members;
76
+ }
77
+ var RunPlan = class {
78
+ constructor(table, members) {
79
+ this.table = table;
80
+ this.members = members;
81
+ this.pool = new PoolPlan().begin(table, table.members);
82
+ for (let first = 0; first < members.length; ) {
83
+ let end = first + 1;
84
+ while (end < members.length && compareRuns(members[first].points, members[end].points) === 0) end++;
85
+ const source = { start: 0, length: 0, sliceClass: 255 };
86
+ const change = {
87
+ first,
88
+ length: end - first,
89
+ ordinal: 0,
90
+ slot: 0,
91
+ oldLength: 0,
92
+ added: false,
93
+ source,
94
+ target: source
95
+ };
96
+ this.locate(change);
97
+ if (!change.added) this.previousSlice(change);
98
+ requireRoom(change.length <= 4294967295 - change.oldLength);
99
+ const length = change.oldLength + change.length;
100
+ change.target = length > change.source.length ? this.pool.allocate(length) : change.source;
101
+ this.changes.push(change);
102
+ first = end;
103
+ }
104
+ this.pool.check();
105
+ }
106
+ pool;
107
+ changes = [];
108
+ placed = /* @__PURE__ */ new Set();
109
+ added = 0;
110
+ publish() {
111
+ const table = this.table;
112
+ this.pool.publish();
113
+ for (const change of this.changes) {
114
+ const ordinal = change.ordinal;
115
+ if (change.added) {
116
+ const points = this.members[change.first].points;
117
+ for (let at = 0; at < 3; at++) table.runs[ordinal * 3 + at] = points[at];
118
+ table.slots[change.slot] = ordinal + 1;
119
+ }
120
+ const target = change.target.start;
121
+ if (change.oldLength > 0 && target !== change.source.start)
122
+ table.members.copyWithin(target, change.source.start, change.source.start + change.oldLength);
123
+ for (let at = 0; at < change.length; at++)
124
+ table.members[target + change.oldLength + at] = this.members[change.first + at].spelling;
125
+ table.listStart[ordinal] = target;
126
+ table.listLength[ordinal] = change.oldLength + change.length;
127
+ table.listClass[ordinal] = change.target.sliceClass;
128
+ }
129
+ for (const change of this.changes) {
130
+ if (change.source.sliceClass !== 255 && change.source.start !== change.target.start)
131
+ releaseRange(table, table.members, change.source.start, change.source.length, change.source.sliceClass);
132
+ }
133
+ table.header[0] += this.added;
134
+ }
135
+ locate(change) {
136
+ const table = this.table;
137
+ const points = this.members[change.first].points;
138
+ const mask = table.slots.length - 1;
139
+ let slot = hashRun(points) & mask;
140
+ let available = -1;
141
+ for (let scanned = 0; scanned < table.slots.length; scanned++) {
142
+ if (!this.placed.has(slot)) {
143
+ const entry = table.slots[slot];
144
+ if (entry > 0 && entry <= table.header[0] && equalRun(table, entry - 1, points)) {
145
+ change.ordinal = entry - 1;
146
+ change.slot = slot;
147
+ return;
148
+ }
149
+ if (entry <= 0) {
150
+ if (available < 0) available = slot;
151
+ else if (entry === 0) {
152
+ const ordinal = table.header[0] + this.added;
153
+ requireRoom(ordinal < table.listStart.length && ordinal < 2147483647);
154
+ change.ordinal = ordinal;
155
+ change.slot = available;
156
+ change.added = true;
157
+ this.placed.add(available);
158
+ this.added++;
159
+ return;
160
+ }
161
+ }
162
+ }
163
+ slot = slot + 1 & mask;
164
+ }
165
+ requireRoom(false);
166
+ }
167
+ previousSlice(change) {
168
+ const table = this.table;
169
+ const ordinal = change.ordinal;
170
+ const sliceClass = table.listClass[ordinal];
171
+ const start = table.listStart[ordinal];
172
+ requireSegment(sliceClass < table.sliceCapacities.length && start <= table.allocation[0]);
173
+ const capacity = table.sliceCapacities[sliceClass];
174
+ const length = table.listLength[ordinal];
175
+ requireSegment(capacity <= table.allocation[0] - start && length <= capacity);
176
+ change.source = { start, length: capacity, sliceClass };
177
+ change.oldLength = length;
178
+ }
179
+ };
180
+ function hasSpelling(members, first, end, spelling) {
181
+ let low = first;
182
+ let high = end;
183
+ while (low < high) {
184
+ const middle = low + Math.floor((high - low) / 2);
185
+ if (members[middle].spelling < spelling) low = middle + 1;
186
+ else high = middle;
187
+ }
188
+ return low < end && members[low].spelling === spelling;
189
+ }
190
+ function removalOrdinal(table, points) {
191
+ const mask = table.slots.length - 1;
192
+ let slot = hashRun(points) & mask;
193
+ for (let scanned = 0; scanned < table.slots.length; scanned++) {
194
+ const entry = table.slots[slot];
195
+ if (entry === 0) break;
196
+ if (entry > 0 && entry <= table.header[0] && equalRun(table, entry - 1, points)) return entry - 1;
197
+ slot = slot + 1 & mask;
198
+ }
199
+ throw new SegmentRefusal(1);
200
+ }
201
+ function checkRemoval(table, ordinal, members, first, end) {
202
+ const start = table.listStart[ordinal];
203
+ const length = table.listLength[ordinal];
204
+ const allocated = table.allocation[0];
205
+ const kind = table.listClass[ordinal];
206
+ requireSegment(
207
+ allocated <= table.members.length && start <= allocated && kind < table.sliceCapacities.length && length <= table.sliceCapacities[kind] && table.sliceCapacities[kind] <= allocated - start
208
+ );
209
+ let found = first;
210
+ for (let at = start; at < start + length; at++) {
211
+ const spelling = table.members[at];
212
+ requireSegment(at === start || spelling > table.members[at - 1]);
213
+ if (found < end && spelling === members[found].spelling) found++;
214
+ }
215
+ requireSegment(found === end);
216
+ }
217
+ function prepareRunRemoval(table, members) {
218
+ const changes = [];
219
+ for (let first = 0; first < members.length; ) {
220
+ let end = first + 1;
221
+ while (end < members.length && compareRuns(members[first].points, members[end].points) === 0) end++;
222
+ const ordinal = removalOrdinal(table, members[first].points);
223
+ checkRemoval(table, ordinal, members, first, end);
224
+ changes.push({ ordinal, first, end });
225
+ first = end;
226
+ }
227
+ changes.sort((left, right) => table.listStart[left.ordinal] - table.listStart[right.ordinal]);
228
+ for (let at = 1; at < changes.length; at++) {
229
+ const previous = changes[at - 1].ordinal;
230
+ requireSegment(
231
+ table.listStart[previous] + table.sliceCapacities[table.listClass[previous]] <= table.listStart[changes[at].ordinal]
232
+ );
233
+ }
234
+ return () => {
235
+ for (const { ordinal, first, end } of changes) {
236
+ const start = table.listStart[ordinal];
237
+ const length = table.listLength[ordinal];
238
+ let kept = 0;
239
+ for (let at = start; at < start + length; at++) {
240
+ const spelling = table.members[at];
241
+ if (!hasSpelling(members, first, end, spelling)) table.members[start + kept++] = spelling;
242
+ }
243
+ table.listLength[ordinal] = kept;
244
+ }
245
+ };
246
+ }
247
+
248
+ // src/core/segment-writes/run-table.ts
249
+ var COUNT = 0;
250
+ var SLOT_CAPACITY = 2;
251
+ var HEADER_WORDS = 8;
252
+ var RUN_POINTS = 3;
253
+ var LAST_SLOT_CAPACITY = 2147483648;
254
+ var LAST_RUN_COUNT = 2147483647;
255
+ var NO_FREE_SLICE = 4294967295;
256
+ function hashRun2(runs, at) {
257
+ let hash = FNV_OFFSET_BASIS;
258
+ for (let index = at; index < at + RUN_POINTS; index++) {
259
+ const point = runs[index];
260
+ for (let byte = 0; byte < 4; byte++) hash = Math.imul(hash ^ point >>> byte * 8 & 255, FNV_PRIME);
261
+ }
262
+ return hash >>> 0;
263
+ }
264
+ var RunTable = class {
265
+ header = new Uint32Array(HEADER_WORDS);
266
+ slots = new Int32Array(RUN_TABLE_START_CAPACITY * TERM_REGION_GROWTH_FACTOR);
267
+ runs = new Uint32Array(RUN_TABLE_START_CAPACITY * RUN_POINTS);
268
+ listStart = new Uint32Array(RUN_TABLE_START_CAPACITY);
269
+ listLength = new Uint32Array(RUN_TABLE_START_CAPACITY);
270
+ listClass = new Uint8Array(RUN_TABLE_START_CAPACITY);
271
+ members = new Uint32Array(RUN_MEMBER_START_CAPACITY);
272
+ allocation = new Uint32Array(2);
273
+ freeHeads = new Uint32Array(SLICE_CAPACITIES.length).fill(NO_FREE_SLICE);
274
+ sliceCapacities = SLICE_CAPACITIES;
275
+ constructor() {
276
+ this.header[SLOT_CAPACITY] = this.slots.length;
277
+ }
278
+ reserve(count, members) {
279
+ if (!Number.isInteger(count) || count < this.header[COUNT] || count > LAST_RUN_COUNT)
280
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The run table exceeds its slot layout");
281
+ let slots = this.slots;
282
+ let slotCapacity = slots.length;
283
+ while (count * TERM_SLOT_LOAD_DENOMINATOR >= slotCapacity * TERM_SLOT_LOAD_NUMERATOR) {
284
+ slotCapacity *= TERM_REGION_GROWTH_FACTOR;
285
+ if (slotCapacity > LAST_SLOT_CAPACITY)
286
+ throw new NarsilError(ErrorCodes.CONFIG_INVALID, "The run table exceeds its slot layout");
287
+ }
288
+ if (slotCapacity > slots.length) slots = this.rehash(slotCapacity);
289
+ let runs = this.runs;
290
+ let starts = this.listStart;
291
+ let lengths = this.listLength;
292
+ let classes = this.listClass;
293
+ if (count > starts.length) {
294
+ const capacity = Math.min(LAST_RUN_COUNT, Math.max(count, starts.length * TERM_REGION_GROWTH_FACTOR));
295
+ runs = new Uint32Array(capacity * RUN_POINTS);
296
+ starts = new Uint32Array(capacity);
297
+ lengths = new Uint32Array(capacity);
298
+ classes = new Uint8Array(capacity);
299
+ runs.set(this.runs);
300
+ starts.set(this.listStart);
301
+ lengths.set(this.listLength);
302
+ classes.set(this.listClass);
303
+ }
304
+ let values = this.members;
305
+ if (members > values.length) {
306
+ values = new Uint32Array(grownCapacity(values.length, members));
307
+ values.set(this.members.subarray(0, this.allocation[0]));
308
+ }
309
+ this.slots = slots;
310
+ this.header[SLOT_CAPACITY] = slotCapacity;
311
+ this.runs = runs;
312
+ this.listStart = starts;
313
+ this.listLength = lengths;
314
+ this.listClass = classes;
315
+ this.members = values;
316
+ }
317
+ byteLength() {
318
+ return this.header.byteLength + this.slots.byteLength + this.runs.byteLength + this.listStart.byteLength + this.listLength.byteLength + this.listClass.byteLength + this.members.byteLength + this.allocation.byteLength + this.freeHeads.byteLength;
319
+ }
320
+ trim() {
321
+ const count = Math.max(RUN_TABLE_START_CAPACITY, this.header[COUNT]);
322
+ let capacity = RUN_TABLE_START_CAPACITY * TERM_REGION_GROWTH_FACTOR;
323
+ while (count * TERM_SLOT_LOAD_DENOMINATOR >= capacity * TERM_SLOT_LOAD_NUMERATOR)
324
+ capacity *= TERM_REGION_GROWTH_FACTOR;
325
+ const columns = {
326
+ slots: capacity < this.slots.length ? this.rehash(capacity) : this.slots,
327
+ runs: this.runs.slice(0, count * RUN_POINTS),
328
+ listStart: this.listStart.slice(0, count),
329
+ listLength: this.listLength.slice(0, count),
330
+ listClass: this.listClass.slice(0, count),
331
+ members: this.members.slice(0, Math.max(RUN_MEMBER_START_CAPACITY, this.allocation[0]))
332
+ };
333
+ Object.assign(this, columns);
334
+ this.header[SLOT_CAPACITY] = this.slots.length;
335
+ }
336
+ rehash(capacity) {
337
+ const slots = new Int32Array(capacity);
338
+ const mask = capacity - 1;
339
+ for (const entry of this.slots) {
340
+ if (entry <= 0) continue;
341
+ let slot = hashRun2(this.runs, (entry - 1) * RUN_POINTS) & mask;
342
+ while (slots[slot] !== 0) slot = slot + 1 & mask;
343
+ slots[slot] = entry;
344
+ }
345
+ return slots;
346
+ }
347
+ };
348
+
349
+ // src/core/segment-writes/spelling-table.ts
350
+ var SpellingTable = class extends SurfaceTable {
351
+ codePoints = new Uint32Array(SURFACE_TABLE_START_CAPACITY);
352
+ runs = new RunTable();
353
+ load(source, terms) {
354
+ this.reserveWords(new Set(source.spellings));
355
+ const plan = new StringPlan(this.strings);
356
+ for (let at = 0; at < source.spellings.length; at++) {
357
+ const token = terms.ids.get(source.tokens[at]);
358
+ if (token === void 0 || terms.frequencyAt(token) === 0) continue;
359
+ const spelling = source.spellings[at];
360
+ const entry = plan.prepare(spelling);
361
+ if (plan.counts[entry] === 0) plan.tokens[entry] = token;
362
+ plan.counts[entry] += source.counts[at];
363
+ let points = 0;
364
+ for (const _point of spelling) points++;
365
+ plan.points[entry] = points;
366
+ if (plan.counts[entry] > 4294967295) throw new SegmentRefusal(8);
367
+ }
368
+ plan.check();
369
+ const runs = new RunPlan(this.runs, collectRuns(plan));
370
+ plan.publish();
371
+ for (let at = 0; at < plan.size; at++) {
372
+ const ordinal = plan.ordinals[at];
373
+ this.token[ordinal] = plan.tokens[at];
374
+ this.count[ordinal] = plan.counts[at];
375
+ this.codePoints[ordinal] = plan.points[at];
376
+ }
377
+ runs.publish();
378
+ }
379
+ reserveWords(words) {
380
+ const spellings = /* @__PURE__ */ new Set();
381
+ const plan = new StringPlan(this.strings);
382
+ for (const word of words) {
383
+ let points = 0;
384
+ for (const _point of word) points++;
385
+ if (points <= 3) continue;
386
+ spellings.add(word);
387
+ const entry = plan.prepare(word);
388
+ plan.points[entry] = points;
389
+ }
390
+ reserveStrings(this, spellings);
391
+ const members = collectRuns(plan);
392
+ if (members.length === 0) return;
393
+ const count = this.runs.header[0] + members.length;
394
+ this.runs.reserve(count, this.runs.members.length);
395
+ for (; ; ) {
396
+ try {
397
+ new RunPlan(this.runs, members);
398
+ return;
399
+ } catch (error) {
400
+ if (!(error instanceof SegmentRefusal) || error.status !== 3) throw error;
401
+ this.runs.reserve(count, grownCapacity(this.runs.members.length, this.runs.members.length + 1));
402
+ }
403
+ }
404
+ }
405
+ reserve(entries, units2) {
406
+ const prepared = this.prepareCapacity(entries, units2);
407
+ let points = this.codePoints;
408
+ if (prepared.capacity > points.length) {
409
+ points = new Uint32Array(prepared.capacity);
410
+ points.set(this.codePoints);
411
+ }
412
+ prepared.apply();
413
+ this.codePoints = points;
414
+ }
415
+ byteLength() {
416
+ return super.byteLength() + this.codePoints.byteLength + this.runs.byteLength();
417
+ }
418
+ };
419
+
420
+ // src/core/segment-writes/vacuum-runs.ts
421
+ var RunVacuum = class {
422
+ constructor(table, mapping) {
423
+ this.table = table;
424
+ this.mapping = mapping;
425
+ this.sourceCount = table.header[0];
426
+ const end = table.allocation[0];
427
+ requireSegment(end <= table.members.length && table.allocation[1] <= end);
428
+ let previous = 0;
429
+ for (const capacity of table.sliceCapacities) {
430
+ requireSegment(capacity > previous);
431
+ previous = capacity;
432
+ }
433
+ this.members = new Uint32Array(end);
434
+ for (let at = 0; at < this.sourceCount; at++) this.collect(at);
435
+ requireSegment(this.runs.length < table.slots.length);
436
+ this.slices.sort((left, right) => left.start - right.start);
437
+ for (let at = 1; at < this.slices.length; at++)
438
+ requireSegment(this.slices[at].start - this.slices[at - 1].start >= this.slices[at - 1].length);
439
+ }
440
+ runs = [];
441
+ members;
442
+ slices = [];
443
+ memberCount = 0;
444
+ sourceCount;
445
+ validSlot(ordinal, points) {
446
+ const table = this.table;
447
+ const mask = table.slots.length - 1;
448
+ let slot = hashRun(points) & mask;
449
+ for (let seen = 0; seen < table.slots.length; seen++) {
450
+ const entry = table.slots[slot];
451
+ if (entry === 0) return false;
452
+ if (entry > 0 && entry <= this.sourceCount && points.every((point, at) => point === table.runs[(entry - 1) * 3 + at]))
453
+ return entry - 1 === ordinal;
454
+ slot = slot + 1 & mask;
455
+ }
456
+ return false;
457
+ }
458
+ collect(ordinal) {
459
+ const table = this.table;
460
+ const start = table.listStart[ordinal];
461
+ const length = table.listLength[ordinal];
462
+ const end = table.allocation[0];
463
+ const kind = table.listClass[ordinal];
464
+ const points = Array.from(table.runs.subarray(ordinal * 3, ordinal * 3 + 3));
465
+ requireSegment(kind < table.sliceCapacities.length && start <= end && this.validSlot(ordinal, points));
466
+ const size = table.sliceCapacities[kind];
467
+ requireSegment(size <= end - start && length <= size);
468
+ this.slices.push({ start, length: size });
469
+ const run = { points, start: this.memberCount, length: 0, sliceClass: 0 };
470
+ for (let at = start; at < start + length; at++) {
471
+ const member = table.members[at];
472
+ requireSegment(member < this.mapping.length && (at === start || table.members[at - 1] < member));
473
+ const target = this.mapping[member];
474
+ if (target === 4294967295) continue;
475
+ requireSegment(this.memberCount < end);
476
+ this.members[this.memberCount++] = target;
477
+ run.length++;
478
+ }
479
+ if (run.length === 0) return;
480
+ while (run.sliceClass < table.sliceCapacities.length && table.sliceCapacities[run.sliceClass] < run.length)
481
+ run.sliceClass++;
482
+ requireSegment(
483
+ run.sliceClass < table.sliceCapacities.length && table.sliceCapacities[run.sliceClass] <= end - run.start
484
+ );
485
+ this.memberCount = run.start + table.sliceCapacities[run.sliceClass];
486
+ this.runs.push(run);
487
+ }
488
+ publish() {
489
+ const table = this.table;
490
+ table.slots.fill(0);
491
+ const mask = table.slots.length - 1;
492
+ for (let at = 0; at < this.runs.length; at++) {
493
+ const run = this.runs[at];
494
+ table.runs.set(run.points, at * 3);
495
+ let slot = hashRun(run.points) & mask;
496
+ while (table.slots[slot] !== 0) slot = slot + 1 & mask;
497
+ table.slots[slot] = at + 1;
498
+ table.listStart[at] = run.start;
499
+ table.listLength[at] = run.length;
500
+ table.listClass[at] = run.sliceClass;
501
+ table.members.set(this.members.subarray(run.start, run.start + run.length), run.start);
502
+ }
503
+ table.listLength.fill(0, this.runs.length, this.sourceCount);
504
+ table.header[0] = this.runs.length;
505
+ table.allocation[0] = this.memberCount;
506
+ table.allocation[1] = 0;
507
+ table.freeHeads.fill(4294967295);
508
+ }
509
+ };
510
+
511
+ // src/core/expansion/partial-regions.ts
512
+ function unsigned(value) {
513
+ requireSegment(Number.isInteger(value) && value >= 0 && value <= 4294967295);
514
+ }
515
+ function readPartialExpansion(input, requests, outStarts, outSegments, outTokens, outFactors) {
516
+ requireSegment(Array.isArray(input) && input.length <= 4294967295);
517
+ requireSegment(Array.isArray(requests) && requests.length <= 4294967295);
518
+ const sources = input.map((source) => {
519
+ requireSegment(typeof source === "object" && source !== null);
520
+ const raw = { ...source };
521
+ requireSegment(raw.runs === null || typeof raw.runs === "object" && raw.runs !== null);
522
+ return { segment: captureExpansionSource(raw.segment), runs: raw.runs === null ? null : { ...raw.runs } };
523
+ });
524
+ const words = requests.map((request) => {
525
+ requireSegment(typeof request === "object" && request !== null);
526
+ return { ...request };
527
+ });
528
+ const held = readExpansion(
529
+ sources.map((source) => source.segment),
530
+ words.flatMap((word) => [word.written, word.own]),
531
+ outSegments,
532
+ outTokens,
533
+ outFactors
534
+ );
535
+ const starts = typedRegion(outStarts, "Uint32Array");
536
+ requireSegment(starts !== null);
537
+ const outputs = [starts, held.segments, held.tokens, held.factors];
538
+ for (let at = 1; at < outputs.length; at++) requireSegment(!regionsOverlap(starts, outputs[at]));
539
+ for (const region of held.arrays) requireSegment(!regionsOverlap(starts, region));
540
+ const runs = sources.map((source, at) => {
541
+ const raw = source.runs;
542
+ requireSegment(held.sources[at].growing === null || raw === null);
543
+ if (raw === null) return null;
544
+ const points = typedRegion(raw.runs, "Uint32Array");
545
+ const offsets = typedRegion(raw.offsets, "Uint32Array");
546
+ const members = typedRegion(raw.members, "Uint32Array");
547
+ requireSegment(points !== null && offsets !== null && members !== null);
548
+ requireSegment(points.length % 3 === 0 && points.length / 3 <= 4294967295);
549
+ requireSegment(offsets.length === points.length / 3 + 1 && members.length <= 4294967295);
550
+ for (const output of outputs)
551
+ for (const region of [points, offsets, members]) requireSegment(!regionsOverlap(output, region));
552
+ return { runs: points, offsets, members };
553
+ });
554
+ const queries = words.map((word) => {
555
+ unsigned(word.tolerance);
556
+ unsigned(word.prefixLength);
557
+ return { ...word, written: expansionText(word.written), own: expansionText(word.own) };
558
+ });
559
+ requireRoom(starts.length >= words.length + 1);
560
+ return { ...held, starts, runs, queries };
561
+ }
562
+
563
+ // src/core/expansion/partial-source.ts
564
+ function growingRun(table, points) {
565
+ const mask = table.slots.length - 1;
566
+ let slot = hashRun(points) & mask;
567
+ for (let scanned = 0; scanned < table.slots.length; scanned++) {
568
+ const entry = table.slots[slot];
569
+ if (entry === 0) return -1;
570
+ if (entry > 0 && entry <= table.header[0]) {
571
+ const start = (entry - 1) * PARTIAL_RUN_POINTS;
572
+ if (points.every((point, at) => point === table.runs[start + at])) return entry - 1;
573
+ }
574
+ slot = slot + 1 & mask;
575
+ }
576
+ return -1;
577
+ }
578
+ function frozenRun(table, points) {
579
+ let low = 0;
580
+ let high = table.runs.length / PARTIAL_RUN_POINTS;
581
+ while (low < high) {
582
+ const middle = low + Math.floor((high - low) / 2);
583
+ const start = middle * PARTIAL_RUN_POINTS;
584
+ const difference = table.runs[start] - points[0] || table.runs[start + 1] - points[1] || table.runs[start + 2] - points[2];
585
+ if (difference === 0) return middle;
586
+ if (difference < 0) low = middle + 1;
587
+ else high = middle;
588
+ }
589
+ return -1;
590
+ }
591
+ function partialMembers(source, frozenRuns, points) {
592
+ const growing = source.growing?.spellings?.runs;
593
+ if (growing === void 0 && frozenRuns === null) return null;
594
+ let selected = null;
595
+ for (let at = 0; at + PARTIAL_RUN_POINTS <= points.length; at++) {
596
+ const triple = points.slice(at, at + PARTIAL_RUN_POINTS);
597
+ let members;
598
+ if (growing !== void 0) {
599
+ const run = growingRun(growing, triple);
600
+ if (run < 0) return null;
601
+ const start = growing.listStart[run];
602
+ const count = growing.listLength[run];
603
+ const kind = growing.listClass[run];
604
+ const allocated = growing.allocation[0];
605
+ if (kind >= growing.sliceCapacities.length || allocated > growing.members.length || start > allocated) return null;
606
+ const capacity = growing.sliceCapacities[kind];
607
+ if (count > capacity || capacity > allocated - start) return null;
608
+ members = growing.members.subarray(start, start + count);
609
+ } else {
610
+ if (frozenRuns === null) return null;
611
+ const run = frozenRun(frozenRuns, triple);
612
+ if (run < 0) return null;
613
+ const start = frozenRuns.offsets[run];
614
+ const end = frozenRuns.offsets[run + 1];
615
+ if (start > end || end > frozenRuns.members.length) return null;
616
+ members = frozenRuns.members.subarray(start, end);
617
+ }
618
+ if (selected === null || members.length < selected.length) selected = members;
619
+ }
620
+ return selected;
621
+ }
622
+ function partialSpelling(source, ordinal) {
623
+ let text;
624
+ let token;
625
+ if (source.growing !== null) {
626
+ const table = source.growing.spellings;
627
+ if (table === null || ordinal >= table.strings.header[0] || table.count[ordinal] === 0) return null;
628
+ const start = table.strings.termOffset[ordinal];
629
+ const length = table.strings.termLength[ordinal];
630
+ if (start > table.strings.header[4] || length > table.strings.header[4] - start || length > 1073741824) return null;
631
+ text = storedText(table.strings.units, start, length);
632
+ token = table.token[ordinal];
633
+ } else {
634
+ const table = source.frozen?.spellings;
635
+ const state = source.state;
636
+ if (table === void 0 || table === null || state === null || ordinal >= table.counts.length || state.removedSpellings[ordinal] >= table.counts[ordinal])
637
+ return null;
638
+ text = mergeText(table, ordinal);
639
+ token = source.frozen?.spellingTokens[ordinal] ?? 4294967295;
640
+ }
641
+ return text === null || !validExpansionText(text) ? null : { text, token };
642
+ }
643
+
644
+ // src/core/expansion/partial.ts
645
+ var expandPartialWords = (...args) => {
646
+ try {
647
+ const held = readPartialExpansion(...args);
648
+ const dictionaries = held.sources.map(expansionDictionary);
649
+ const lengths = held.queries.map((query) => codePointCount(query.written));
650
+ const eligible = Math.min(PARTIAL_BUDGET, lengths.filter((length) => length >= PARTIAL_RUN_POINTS).length);
651
+ const output = [];
652
+ const starts = [0];
653
+ let order = 0;
654
+ for (let queryAt = 0; queryAt < held.queries.length; queryAt++) {
655
+ const query = held.queries[queryAt];
656
+ if (lengths[queryAt] < PARTIAL_RUN_POINTS || order >= PARTIAL_BUDGET) {
657
+ starts.push(output.length);
658
+ continue;
659
+ }
660
+ const share = Math.floor(PARTIAL_BUDGET / eligible) + (order++ < PARTIAL_BUDGET % eligible ? 1 : 0);
661
+ const points = Array.from(query.written, (character) => character.codePointAt(0) ?? 0);
662
+ const candidates = /* @__PURE__ */ new Map();
663
+ for (let segment = 0; segment < held.sources.length; segment++) {
664
+ const source = held.sources[segment];
665
+ const dictionary = dictionaries[segment];
666
+ for (const entry of partialMembers(source, held.runs[segment], points) ?? []) {
667
+ const spelling = partialSpelling(source, entry);
668
+ if (spelling === null || !spelling.text.includes(query.written)) continue;
669
+ const length = codePointCount(spelling.text);
670
+ if (length <= points.length) continue;
671
+ const text = termText(dictionary, spelling.token);
672
+ if (text === null || !validExpansionText(text) || liveFrequency(dictionary, spelling.token) === 0) continue;
673
+ if (fuzzyTermMatches(query.own, text, query.tolerance, query.prefixLength)) continue;
674
+ const previous = candidates.get(text);
675
+ if (previous !== void 0) {
676
+ previous.length = Math.min(previous.length, length);
677
+ if (segment < previous.segment || segment === previous.segment && spelling.token < previous.token) {
678
+ previous.segment = segment;
679
+ previous.token = spelling.token;
680
+ }
681
+ } else candidates.set(text, { text, segment, token: spelling.token, length, documents: 0 });
682
+ }
683
+ }
684
+ const ranked = [...candidates.values()];
685
+ for (const candidate of ranked) {
686
+ for (const dictionary of dictionaries) {
687
+ const token = expansionToken(dictionary, candidate.text);
688
+ if (token !== null) candidate.documents += liveFrequency(dictionary, token);
689
+ }
690
+ }
691
+ ranked.sort((left, right) => right.documents - left.documents || compareCodePoints(left.text, right.text));
692
+ for (let at = 0; at < Math.min(share, ranked.length); at++) {
693
+ const candidate = ranked[at];
694
+ output.push({ segment: candidate.segment, token: candidate.token, factor: points.length / candidate.length });
695
+ }
696
+ starts.push(output.length);
697
+ }
698
+ requireRoom(
699
+ output.length <= held.segments.length && output.length <= held.tokens.length && output.length <= held.factors.length
700
+ );
701
+ for (let at = 0; at < output.length; at++) {
702
+ held.segments[at] = output[at].segment;
703
+ held.tokens[at] = output[at].token;
704
+ held.factors[at] = output[at].factor;
705
+ }
706
+ held.starts.set(starts);
707
+ return output.length;
708
+ } catch (error) {
709
+ if (error instanceof SegmentRefusal) return -error.status;
710
+ throw error;
711
+ }
712
+ };
713
+
714
+ // src/core/expansion/partial-host.ts
715
+ function sourceOf(state, analysis) {
716
+ state.partialWords.readerFor(analysis);
717
+ const source = expansionSource(state);
718
+ if (source === null) return null;
719
+ if (source.growing !== null) {
720
+ let spellings = state.partialWords.growingRegions?.() ?? null;
721
+ if (spellings === null) {
722
+ const encoded = state.partialWords.spellings();
723
+ const terms = state.invertedIdx.livePostings?.terms;
724
+ if (encoded !== null && terms !== void 0) {
725
+ const table2 = new SpellingTable();
726
+ table2.load(encoded, terms);
727
+ spellings = table2;
728
+ }
729
+ }
730
+ return { segment: { ...source, growing: { ...source.growing, spellings } }, runs: null };
731
+ }
732
+ const held = state.partialWords.removalState?.();
733
+ if (held === void 0 || held === null || source.frozen === null || source.state === null)
734
+ return { segment: source, runs: null };
735
+ const { table, removed } = held;
736
+ return {
737
+ segment: {
738
+ ...source,
739
+ growing: null,
740
+ frozen: {
741
+ ...source.frozen,
742
+ spellings: { text: table.spellings, offsets: table.spellingOffsets, counts: table.spellingCounts },
743
+ spellingTokens: table.spellingTokens
744
+ },
745
+ state: { ...source.state, removedSpellings: removed }
746
+ },
747
+ runs: { runs: table.runs, offsets: table.runOffsets, members: table.runEntries }
748
+ };
749
+ }
750
+ function units(text) {
751
+ return Uint16Array.from({ length: text.length }, (_, at) => text.charCodeAt(at));
752
+ }
753
+ function partialAdditions(states, analysis, requests) {
754
+ if (analysis.fields.length === 0 || requests.length === 0) return requests.map(() => []);
755
+ const sources = [];
756
+ for (const state of states) {
757
+ const source = sourceOf(state, analysis);
758
+ if (source === null) return null;
759
+ sources.push(source);
760
+ }
761
+ const starts = new Uint32Array(requests.length + 1);
762
+ const segments = new Uint32Array(PARTIAL_BUDGET);
763
+ const tokens = new Uint32Array(PARTIAL_BUDGET);
764
+ const factors = new Float64Array(PARTIAL_BUDGET);
765
+ const queries = requests.map((request) => ({
766
+ written: units(request.written),
767
+ own: units(request.ownToken),
768
+ tolerance: request.tolerance,
769
+ prefixLength: request.prefixLength
770
+ }));
771
+ const count = expandPartialWords(sources, queries, starts, segments, tokens, factors);
772
+ if (count < 0)
773
+ throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, `The partial-word operation refused status ${-count}`);
774
+ return requests.map((_, query) => {
775
+ const added = [];
776
+ for (let at = starts[query]; at < starts[query + 1]; at++) {
777
+ const token = termText(expansionDictionary(sources[segments[at]].segment), tokens[at]);
778
+ if (token === null)
779
+ throw new NarsilError(ErrorCodes.PARTITION_CORRUPTED, "The partial-word addition refers to an invalid token");
780
+ added.push({ token, factor: factors[at] });
781
+ }
782
+ return added;
783
+ });
784
+ }
785
+
786
+ // src/core/partition/partial-words/text.ts
787
+ var encoder = new TextEncoder();
788
+ var LAST_BASIC_PLANE_CODE_POINT = 65535;
789
+ var BASIC_PLANE_SIZE = 65536;
790
+ var UTF8_CONTINUATION_MASK = 192;
791
+ var UTF8_CONTINUATION_BITS = 128;
792
+ function codePointsOf(text) {
793
+ const points = [];
794
+ let index = 0;
795
+ while (index < text.length) {
796
+ const point = text.codePointAt(index) ?? text.charCodeAt(index);
797
+ points.push(point);
798
+ index += point > LAST_BASIC_PLANE_CODE_POINT ? 2 : 1;
799
+ }
800
+ return points;
801
+ }
802
+ function runKeyAt(points, at) {
803
+ return String.fromCodePoint(points[at], points[at + 1], points[at + 2]);
804
+ }
805
+ function packedRunKeyAt(points, at) {
806
+ const first = points[at];
807
+ const second = points[at + 1];
808
+ const third = points[at + 2];
809
+ if ((first | second | third) > LAST_BASIC_PLANE_CODE_POINT) return String.fromCodePoint(first, second, third);
810
+ return first * BASIC_PLANE_SIZE * BASIC_PLANE_SIZE + second * BASIC_PLANE_SIZE + third;
811
+ }
812
+ function partialWordProbe(text) {
813
+ return { text, codePoints: codePointsOf(text), utf8: encoder.encode(text) };
814
+ }
815
+ function utf8Contains(haystack, start, end, needle) {
816
+ const last = end - needle.length;
817
+ for (let from = start; from <= last; from++) {
818
+ let at = 0;
819
+ while (at < needle.length && haystack[from + at] === needle[at]) at++;
820
+ if (at === needle.length) return true;
821
+ }
822
+ return false;
823
+ }
824
+ function utf8CodePointCount(bytes, start, end) {
825
+ let count = 0;
826
+ for (let at = start; at < end; at++) {
827
+ if ((bytes[at] & UTF8_CONTINUATION_MASK) !== UTF8_CONTINUATION_BITS) count++;
828
+ }
829
+ return count;
830
+ }
831
+
832
+ // src/core/partition/partial-words/expansion.ts
833
+ function excludedFromRequest(request, token) {
834
+ if (token === request.ownToken) return true;
835
+ return request.tolerance > 0 && fuzzyTermMatches(request.ownToken, token, request.tolerance, request.prefixLength);
836
+ }
837
+ function addedWordsFor(subs, readers, request) {
838
+ if (request.share <= 0) return [];
839
+ const probe = partialWordProbe(request.written);
840
+ const shortestSpelling = /* @__PURE__ */ new Map();
841
+ for (const reader of readers) {
842
+ reader.forEachContaining(probe, (token, spellingLength) => {
843
+ const known = shortestSpelling.get(token);
844
+ if (known === void 0 || spellingLength < known) shortestSpelling.set(token, spellingLength);
845
+ });
846
+ }
847
+ const ranked = [];
848
+ for (const [token, spellingLength] of shortestSpelling) {
849
+ if (excludedFromRequest(request, token)) continue;
850
+ const documents = partitionDocumentFrequency(subs, token);
851
+ if (documents > 0) ranked.push({ token, documents, spellingLength });
852
+ }
853
+ ranked.sort((a, b) => b.documents - a.documents || compareCodePoints(a.token, b.token));
854
+ if (ranked.length > request.share) ranked.length = request.share;
855
+ const writtenLength = probe.codePoints.length;
856
+ return ranked.map((word) => ({ token: word.token, factor: writtenLength / word.spellingLength }));
857
+ }
858
+ function expandPartialWords2(subs, analysis, requests) {
859
+ const native = partialAdditions(subs, analysis, requests);
860
+ if (native !== null) return native;
861
+ const readers = [];
862
+ for (const sub of subs) {
863
+ const reader = sub.partialWords.readerFor(analysis);
864
+ if (reader !== null) readers.push(reader);
865
+ }
866
+ return requests.map((request) => readers.length === 0 ? [] : addedWordsFor(subs, readers, request));
867
+ }
868
+
869
+ // src/core/partition/partial-words/frozen-table.ts
870
+ var decoder = new TextDecoder("utf-8", { ignoreBOM: true });
871
+ function compareEntries(left, right) {
872
+ return compareCodePoints(left.spelling, right.spelling) || left.tokenIndex - right.tokenIndex;
873
+ }
874
+ function orderedEntriesOf(source, tokenTable) {
875
+ const entries = [];
876
+ for (let at = 0; at < source.tokens.length; at++) {
877
+ if (source.counts[at] <= 0) continue;
878
+ const tokenIndex = tokenTable.find(source.tokens[at]);
879
+ if (tokenIndex >= 0) entries.push({ spelling: source.spellings[at], tokenIndex, count: source.counts[at] });
880
+ }
881
+ entries.sort(compareEntries);
882
+ const distinct = [];
883
+ for (const entry of entries) {
884
+ const last = distinct[distinct.length - 1];
885
+ if (last !== void 0 && compareEntries(last, entry) === 0) last.count += entry.count;
886
+ else distinct.push(entry);
887
+ }
888
+ return distinct;
889
+ }
890
+ function buildPartialWordTable(source, tokenTable) {
891
+ const spellings = [];
892
+ const spellingTokens = [];
893
+ const spellingCounts = [];
894
+ const runLists = /* @__PURE__ */ new Map();
895
+ const addSpelling = (spelling, tokenIndex, count) => {
896
+ const points = codePointsOf(spelling);
897
+ if (points.length <= PARTIAL_WORD_RUN_CODE_POINTS) return;
898
+ const entry = spellings.length;
899
+ spellings.push(spelling);
900
+ spellingTokens.push(tokenIndex);
901
+ spellingCounts.push(count);
902
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
903
+ const key = runKeyAt(points, at);
904
+ const list = runLists.get(key);
905
+ if (list === void 0) runLists.set(key, [entry]);
906
+ else if (list[list.length - 1] !== entry) list.push(entry);
907
+ }
908
+ };
909
+ for (const entry of orderedEntriesOf(source, tokenTable)) addSpelling(entry.spelling, entry.tokenIndex, entry.count);
910
+ const runKeys = [...runLists.keys()].sort(compareCodePoints);
911
+ const runs = new Uint32Array(runKeys.length * PARTIAL_WORD_RUN_CODE_POINTS);
912
+ const runOffsets = new Uint32Array(runKeys.length + 1);
913
+ let memberCount = 0;
914
+ for (const key of runKeys) memberCount += runLists.get(key)?.length ?? 0;
915
+ const runEntries = new Uint32Array(memberCount);
916
+ let cursor = 0;
917
+ for (let run = 0; run < runKeys.length; run++) {
918
+ const points = codePointsOf(runKeys[run]);
919
+ runs.set(points, run * PARTIAL_WORD_RUN_CODE_POINTS);
920
+ for (const entry of runLists.get(runKeys[run]) ?? []) runEntries[cursor++] = entry;
921
+ runOffsets[run + 1] = cursor;
922
+ }
923
+ const spellingBlob = encodeStringBlob(spellings);
924
+ return {
925
+ fields: [...source.fields],
926
+ spellings: spellingBlob.blob,
927
+ spellingOffsets: spellingBlob.offsets,
928
+ spellingTokens: Uint32Array.from(spellingTokens),
929
+ spellingCounts: Uint32Array.from(spellingCounts),
930
+ runs,
931
+ runOffsets,
932
+ runEntries
933
+ };
934
+ }
935
+ function countTableSpellings(counts, table, tokenTable, removedCounts) {
936
+ const { spellings, spellingOffsets, spellingTokens, spellingCounts } = table;
937
+ for (let entry = 0; entry < spellingTokens.length; entry++) {
938
+ const spelling = decoder.decode(spellings.subarray(spellingOffsets[entry], spellingOffsets[entry + 1]));
939
+ countSpelling(
940
+ counts,
941
+ tokenTable.tokenAt(spellingTokens[entry]),
942
+ spelling,
943
+ Math.max(0, spellingCounts[entry] - (removedCounts?.[entry] ?? 0))
944
+ );
945
+ }
946
+ }
947
+ function findRun(runs, first, second, third) {
948
+ let low = 0;
949
+ let high = runs.length / PARTIAL_WORD_RUN_CODE_POINTS;
950
+ while (low < high) {
951
+ const middle = low + high >>> 1;
952
+ const at = middle * PARTIAL_WORD_RUN_CODE_POINTS;
953
+ const difference = runs[at] - first || runs[at + 1] - second || runs[at + 2] - third;
954
+ if (difference === 0) return middle;
955
+ if (difference < 0) low = middle + 1;
956
+ else high = middle;
957
+ }
958
+ return -1;
959
+ }
960
+ function partialWordTableBytes(table) {
961
+ return table.spellings.byteLength + table.spellingOffsets.byteLength + table.spellingTokens.byteLength + table.spellingCounts.byteLength + table.runs.byteLength + table.runOffsets.byteLength + table.runEntries.byteLength;
962
+ }
963
+ function readPartialWordTable(table, tokenTable, removedCounts) {
964
+ const { spellings, spellingOffsets, spellingTokens, spellingCounts, runs, runOffsets, runEntries } = table;
965
+ function narrowestRun(points) {
966
+ let narrowest = -1;
967
+ let narrowestLength = Number.POSITIVE_INFINITY;
968
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
969
+ const run = findRun(runs, points[at], points[at + 1], points[at + 2]);
970
+ if (run < 0) return -1;
971
+ const length = runOffsets[run + 1] - runOffsets[run];
972
+ if (length < narrowestLength) {
973
+ narrowest = run;
974
+ narrowestLength = length;
975
+ }
976
+ }
977
+ return narrowest;
978
+ }
979
+ return {
980
+ forEachContaining(probe, visit) {
981
+ if (probe.codePoints.length < PARTIAL_WORD_RUN_CODE_POINTS) return;
982
+ const run = narrowestRun(probe.codePoints);
983
+ if (run < 0) return;
984
+ for (let member = runOffsets[run]; member < runOffsets[run + 1]; member++) {
985
+ const entry = runEntries[member];
986
+ const start = spellingOffsets[entry];
987
+ const end = spellingOffsets[entry + 1];
988
+ if (end - start <= probe.utf8.length) continue;
989
+ if (removedCounts[entry] >= spellingCounts[entry]) continue;
990
+ if (!utf8Contains(spellings, start, end, probe.utf8)) continue;
991
+ const length = utf8CodePointCount(spellings, start, end);
992
+ if (length <= probe.codePoints.length) continue;
993
+ visit(tokenTable.tokenAt(spellingTokens[entry]), length);
994
+ }
995
+ },
996
+ entryOf(spelling, token) {
997
+ const probe = partialWordProbe(spelling);
998
+ if (probe.codePoints.length <= PARTIAL_WORD_RUN_CODE_POINTS) return -1;
999
+ const run = narrowestRun(probe.codePoints);
1000
+ if (run < 0) return -1;
1001
+ let exhausted = -1;
1002
+ for (let member = runOffsets[run]; member < runOffsets[run + 1]; member++) {
1003
+ const entry = runEntries[member];
1004
+ if (token !== void 0 && tokenTable.tokenAt(spellingTokens[entry]) !== token) continue;
1005
+ const start = spellingOffsets[entry];
1006
+ const end = spellingOffsets[entry + 1];
1007
+ if (end - start !== probe.utf8.length || !utf8Contains(spellings, start, end, probe.utf8)) continue;
1008
+ if (removedCounts[entry] < spellingCounts[entry]) return entry;
1009
+ if (exhausted < 0) exhausted = entry;
1010
+ }
1011
+ return exhausted;
1012
+ },
1013
+ bytes() {
1014
+ return partialWordTableBytes(table);
1015
+ }
1016
+ };
1017
+ }
1018
+
1019
+ // src/core/partition/partial-words/frozen-slot.ts
1020
+ function tombstonedOrdinalsOf(documents) {
1021
+ let marks = null;
1022
+ for (let ordinal = 0; ordinal < documents.count; ordinal++) {
1023
+ if (!documents.isTombstoned(ordinal)) continue;
1024
+ marks ??= new Uint8Array(documents.count);
1025
+ marks[ordinal] = 1;
1026
+ }
1027
+ return marks;
1028
+ }
1029
+ function addCounts(target, source) {
1030
+ for (const [token, bySpelling] of source) {
1031
+ for (const [spelling, count] of bySpelling) countSpelling(target, token, spelling, count);
1032
+ }
1033
+ }
1034
+ function countRemovedSpellings(removed, document, analysis) {
1035
+ const count = (spelling) => {
1036
+ removed.set(spelling, (removed.get(spelling) ?? 0) + 1);
1037
+ };
1038
+ if (analysis.options?.customTokenizer !== void 0) {
1039
+ visitDocumentSpellings(document, analysis, (_token, spelling) => count(spelling));
1040
+ return;
1041
+ }
1042
+ visitDocumentSurfaceSpellings(document, analysis, count);
1043
+ }
1044
+ function unstemmedSpellingsOf(analysis) {
1045
+ const { language } = analysis;
1046
+ if (language.normalizer === void 0 || language.tokenizer?.normalizeDiacritics === true) return () => null;
1047
+ const stopWords = resolveStopWords(language, analysis.options?.stopWordOverride);
1048
+ return (spelling) => {
1049
+ if (stopWords.has(spelling)) return null;
1050
+ const normalized = transformToken(spelling, language, false, false);
1051
+ return normalized === spelling ? null : normalized;
1052
+ };
1053
+ }
1054
+ function subtractRemovedSpellings(counts, removed, analysis) {
1055
+ const tokenOf = /* @__PURE__ */ new Map();
1056
+ for (const [token, bySpelling] of counts) {
1057
+ for (const spelling of bySpelling.keys()) tokenOf.set(spelling, token);
1058
+ }
1059
+ const unstemmedSpelling = unstemmedSpellingsOf(analysis);
1060
+ for (const [spelling, count] of removed) {
1061
+ const stored = tokenOf.has(spelling) ? spelling : unstemmedSpelling(spelling) ?? spelling;
1062
+ const token = tokenOf.get(stored);
1063
+ if (token !== void 0) countSpelling(counts, token, stored, -count);
1064
+ }
1065
+ }
1066
+ function createFrozenPartialWordSlot(initial, tokenTable, documents) {
1067
+ let table = initial;
1068
+ let uncounted = null;
1069
+ let removedCounts = new Uint32Array(initial?.spellingCounts.length ?? 0);
1070
+ let reader = initial === null ? null : readPartialWordTable(initial, tokenTable, removedCounts);
1071
+ let pendingRemovals = [];
1072
+ function isCounted(ordinal) {
1073
+ return (uncounted === null || uncounted[ordinal] === 0) && !documents.isTombstoned(ordinal);
1074
+ }
1075
+ function applyRemoval(changes) {
1076
+ if (table === null) return;
1077
+ for (const [entry, count] of changes)
1078
+ removedCounts[entry] = Math.min(table.spellingCounts[entry], removedCounts[entry] + count);
1079
+ }
1080
+ function subtractPendingRemovals(current, analysis) {
1081
+ if (pendingRemovals.length === 0) return;
1082
+ const removed = /* @__PURE__ */ new Map();
1083
+ for (const ordinal of pendingRemovals) countRemovedSpellings(removed, documents.documentAt(ordinal), analysis);
1084
+ const unstemmedSpelling = unstemmedSpellingsOf(analysis);
1085
+ const changes = /* @__PURE__ */ new Map();
1086
+ for (const [spelling, count] of removed) {
1087
+ let entry = current.entryOf(spelling);
1088
+ if (entry < 0) {
1089
+ const fallback = unstemmedSpelling(spelling);
1090
+ if (fallback !== null) entry = current.entryOf(fallback);
1091
+ }
1092
+ if (entry >= 0) changes.set(entry, (changes.get(entry) ?? 0) + count);
1093
+ }
1094
+ applyRemoval(changes);
1095
+ pendingRemovals = [];
1096
+ }
1097
+ function rebuild(analysis) {
1098
+ const excluded = tombstonedOrdinalsOf(documents);
1099
+ const counts = /* @__PURE__ */ new Map();
1100
+ for (let ordinal = 0; ordinal < documents.count; ordinal++) {
1101
+ if (excluded === null || excluded[ordinal] === 0)
1102
+ countDocumentSpellings(counts, documents.documentAt(ordinal), analysis, 1);
1103
+ }
1104
+ const rebuilt = buildPartialWordTable(encodePartialSpellings(analysis.fields, counts), tokenTable);
1105
+ const countsRemoved = new Uint32Array(rebuilt.spellingCounts.length);
1106
+ const rebuiltReader = readPartialWordTable(rebuilt, tokenTable, countsRemoved);
1107
+ table = rebuilt;
1108
+ uncounted = excluded;
1109
+ removedCounts = countsRemoved;
1110
+ pendingRemovals = [];
1111
+ reader = rebuiltReader;
1112
+ return rebuiltReader;
1113
+ }
1114
+ function correctionsAgainstTable(mergedOrdinals) {
1115
+ let corrections = 0;
1116
+ for (let ordinal = 0; ordinal < mergedOrdinals.length; ordinal++) {
1117
+ if (mergedOrdinals[ordinal] >= 0 !== isCounted(ordinal)) corrections += 1;
1118
+ }
1119
+ return corrections;
1120
+ }
1121
+ function keptCount(mergedOrdinals) {
1122
+ let kept = 0;
1123
+ for (let ordinal = 0; ordinal < mergedOrdinals.length; ordinal++) {
1124
+ if (mergedOrdinals[ordinal] >= 0) kept += 1;
1125
+ }
1126
+ return kept;
1127
+ }
1128
+ return {
1129
+ tokenForSpelling(spelling) {
1130
+ const entry = reader?.entryOf(spelling) ?? -1;
1131
+ return table === null || entry < 0 ? void 0 : tokenTable.tokenAt(table.spellingTokens[entry]);
1132
+ },
1133
+ removalState() {
1134
+ return table === null ? null : { table, removed: removedCounts };
1135
+ },
1136
+ readerFor(analysis) {
1137
+ if (analysis.fields.length === 0) return null;
1138
+ documents.settleRemovals();
1139
+ if (table === null || reader === null || !sameFields(table.fields, analysis.fields)) return rebuild(analysis);
1140
+ subtractPendingRemovals(reader, analysis);
1141
+ return reader;
1142
+ },
1143
+ prepareRemoval(ordinal, spellings) {
1144
+ if (table === null || reader === null) return () => void 0;
1145
+ if (spellings === void 0)
1146
+ return () => {
1147
+ pendingRemovals.push(ordinal);
1148
+ };
1149
+ const changes = /* @__PURE__ */ new Map();
1150
+ for (const [token, bySpelling] of spellings ?? []) {
1151
+ for (const [spelling, count] of bySpelling) {
1152
+ const entry = reader.entryOf(spelling, token);
1153
+ if (entry >= 0) changes.set(entry, (changes.get(entry) ?? 0) + count);
1154
+ }
1155
+ }
1156
+ return () => applyRemoval(changes);
1157
+ },
1158
+ tableOfEveryDocument(analysis) {
1159
+ if (table === null || uncounted !== null || !sameFields(table.fields, analysis.fields)) return null;
1160
+ return table;
1161
+ },
1162
+ countKeptSpellings(counts, analysis, mergedOrdinals) {
1163
+ documents.settleRemovals();
1164
+ if (reader !== null && table !== null && sameFields(table.fields, analysis.fields))
1165
+ subtractPendingRemovals(reader, analysis);
1166
+ if (table === null || !sameFields(table.fields, analysis.fields) || correctionsAgainstTable(mergedOrdinals) > keptCount(mergedOrdinals)) {
1167
+ for (let ordinal = 0; ordinal < mergedOrdinals.length; ordinal++) {
1168
+ if (mergedOrdinals[ordinal] >= 0) countDocumentSpellings(counts, documents.documentAt(ordinal), analysis, 1);
1169
+ }
1170
+ return;
1171
+ }
1172
+ const corrected = /* @__PURE__ */ new Map();
1173
+ countTableSpellings(corrected, table, tokenTable, removedCounts);
1174
+ const removed = /* @__PURE__ */ new Map();
1175
+ const returned = [];
1176
+ for (let ordinal = 0; ordinal < mergedOrdinals.length; ordinal++) {
1177
+ const kept = mergedOrdinals[ordinal] >= 0;
1178
+ if (kept === isCounted(ordinal)) continue;
1179
+ if (kept) returned.push(ordinal);
1180
+ else countRemovedSpellings(removed, documents.documentAt(ordinal), analysis);
1181
+ }
1182
+ subtractRemovedSpellings(corrected, removed, analysis);
1183
+ for (const ordinal of returned) countDocumentSpellings(corrected, documents.documentAt(ordinal), analysis, 1);
1184
+ addCounts(counts, corrected);
1185
+ },
1186
+ spellings() {
1187
+ return null;
1188
+ },
1189
+ bytes() {
1190
+ if (table === null) return 0;
1191
+ return partialWordTableBytes(table) + removedCounts.byteLength;
1192
+ }
1193
+ };
1194
+ }
1195
+
1196
+ // src/core/partition/partial-words/growing-reader.ts
1197
+ function runOrdinal(table, points) {
1198
+ const mask = table.slots.length - 1;
1199
+ let slot = hashRun(points) & mask;
1200
+ for (let scanned = 0; scanned < table.slots.length; scanned++) {
1201
+ const entry = table.slots[slot];
1202
+ if (entry === 0) return -1;
1203
+ if (entry > 0) {
1204
+ const start = (entry - 1) * 3;
1205
+ if (table.runs[start] === points[0] && table.runs[start + 1] === points[1] && table.runs[start + 2] === points[2])
1206
+ return entry - 1;
1207
+ }
1208
+ slot = slot + 1 & mask;
1209
+ }
1210
+ return -1;
1211
+ }
1212
+ function contains(table, ordinal, text) {
1213
+ const strings = table.strings;
1214
+ const start = strings.termOffset[ordinal];
1215
+ const last = start + strings.termLength[ordinal] - text.length;
1216
+ for (let from = start; from <= last; from++) {
1217
+ let at = 0;
1218
+ while (at < text.length && strings.units[from + at] === text.charCodeAt(at)) at++;
1219
+ if (at === text.length) return true;
1220
+ }
1221
+ return false;
1222
+ }
1223
+ function growingSpellingReader(segment) {
1224
+ return {
1225
+ forEachContaining(probe, visit) {
1226
+ const table = segment.spellings;
1227
+ const points = probe.codePoints;
1228
+ if (table === null || points.length < 3) return;
1229
+ const runs = table.runs;
1230
+ const triple = [0, 0, 0];
1231
+ let narrowest = -1;
1232
+ for (let at = 0; at + 3 <= points.length; at++) {
1233
+ for (let unit = 0; unit < 3; unit++) triple[unit] = points[at + unit];
1234
+ const ordinal = runOrdinal(runs, triple);
1235
+ if (ordinal < 0) return;
1236
+ if (narrowest < 0 || runs.listLength[ordinal] < runs.listLength[narrowest]) narrowest = ordinal;
1237
+ }
1238
+ if (narrowest < 0) return;
1239
+ const start = runs.listStart[narrowest];
1240
+ const end = start + runs.listLength[narrowest];
1241
+ const terms = segment.postings.terms;
1242
+ for (let at = start; at < end; at++) {
1243
+ const spelling = runs.members[at];
1244
+ if (table.count[spelling] === 0 || table.codePoints[spelling] <= points.length) continue;
1245
+ if (contains(table, spelling, probe.text))
1246
+ visit(terms.tokens[table.token[spelling]], table.codePoints[spelling]);
1247
+ }
1248
+ },
1249
+ bytes: () => 0
1250
+ };
1251
+ }
1252
+ function growingSpellings(segment, fields) {
1253
+ const encoded = { fields: [...fields], tokens: [], spellings: [], counts: [] };
1254
+ const table = segment.spellings;
1255
+ if (table === null) return encoded;
1256
+ const terms = segment.postings.terms;
1257
+ for (const ordinal of table.liveEntries()) {
1258
+ encoded.tokens.push(terms.tokens[table.token[ordinal]]);
1259
+ encoded.spellings.push(table.textAt(ordinal));
1260
+ encoded.counts.push(table.count[ordinal]);
1261
+ }
1262
+ return encoded;
1263
+ }
1264
+
1265
+ // src/core/partition/partial-words/growing-slot.ts
1266
+ function createGrowingPartialWordSlot(view) {
1267
+ let source = null;
1268
+ let sourceReader = null;
1269
+ let fields = [];
1270
+ let everyDocumentPending = false;
1271
+ const pending = /* @__PURE__ */ new Set();
1272
+ const entriesByToken = /* @__PURE__ */ new Map();
1273
+ const entrySpellings = [];
1274
+ const entryTokens = [];
1275
+ const entryLengths = [];
1276
+ const entryCounts = [];
1277
+ const entryInRuns = [];
1278
+ const freeEntries = [];
1279
+ const entriesOutsideRuns = [];
1280
+ const runs = /* @__PURE__ */ new Map();
1281
+ let liveEntries = 0;
1282
+ let runMembers = 0;
1283
+ function resetSpellings() {
1284
+ pending.clear();
1285
+ entriesByToken.clear();
1286
+ entrySpellings.length = 0;
1287
+ entryTokens.length = 0;
1288
+ entryLengths.length = 0;
1289
+ entryCounts.length = 0;
1290
+ entryInRuns.length = 0;
1291
+ freeEntries.length = 0;
1292
+ entriesOutsideRuns.length = 0;
1293
+ runs.clear();
1294
+ liveEntries = 0;
1295
+ runMembers = 0;
1296
+ }
1297
+ function addEntry(byToken, token, spelling, count) {
1298
+ const length = codePointCountOf(spelling);
1299
+ if (length <= PARTIAL_WORD_RUN_CODE_POINTS) return;
1300
+ const entry = freeEntries.pop() ?? entrySpellings.length;
1301
+ entrySpellings[entry] = spelling;
1302
+ entryTokens[entry] = token;
1303
+ entryLengths[entry] = length;
1304
+ entryCounts[entry] = count;
1305
+ entryInRuns[entry] = false;
1306
+ entriesOutsideRuns.push(entry);
1307
+ if (entriesOutsideRuns.length > GROWING_PARTIAL_PENDING_ENTRIES_PER_ENTRY * entrySpellings.length) {
1308
+ dropFreedAndRepeatedPendingEntries();
1309
+ }
1310
+ byToken.set(spelling, entry);
1311
+ liveEntries++;
1312
+ }
1313
+ function dropFreedAndRepeatedPendingEntries() {
1314
+ let kept = 0;
1315
+ for (const entry of entriesOutsideRuns) {
1316
+ if (entryLengths[entry] === 0 || entryInRuns[entry]) continue;
1317
+ entryInRuns[entry] = true;
1318
+ entriesOutsideRuns[kept++] = entry;
1319
+ }
1320
+ entriesOutsideRuns.length = kept;
1321
+ for (let at = 0; at < kept; at++) entryInRuns[entriesOutsideRuns[at]] = false;
1322
+ }
1323
+ function addToRuns(entry) {
1324
+ const points = codePointsOf(entrySpellings[entry]);
1325
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
1326
+ const key = packedRunKeyAt(points, at);
1327
+ let members = runs.get(key);
1328
+ if (members === void 0) {
1329
+ members = /* @__PURE__ */ new Set();
1330
+ runs.set(key, members);
1331
+ }
1332
+ const size = members.size;
1333
+ members.add(entry);
1334
+ if (members.size !== size) runMembers++;
1335
+ }
1336
+ entryInRuns[entry] = true;
1337
+ }
1338
+ function addEntriesToRuns() {
1339
+ for (const entry of entriesOutsideRuns) {
1340
+ if (entryLengths[entry] > 0 && !entryInRuns[entry]) addToRuns(entry);
1341
+ }
1342
+ entriesOutsideRuns.length = 0;
1343
+ }
1344
+ function removeFromRuns(entry) {
1345
+ const points = codePointsOf(entrySpellings[entry]);
1346
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
1347
+ const key = packedRunKeyAt(points, at);
1348
+ const members = runs.get(key);
1349
+ if (members === void 0 || !members.delete(entry)) continue;
1350
+ runMembers--;
1351
+ if (members.size === 0) runs.delete(key);
1352
+ }
1353
+ entryInRuns[entry] = false;
1354
+ }
1355
+ function removeEntry(byToken, token, spelling, entry) {
1356
+ if (entryInRuns[entry]) removeFromRuns(entry);
1357
+ byToken.delete(spelling);
1358
+ if (byToken.size === 0) entriesByToken.delete(token);
1359
+ entrySpellings[entry] = "";
1360
+ entryTokens[entry] = "";
1361
+ entryLengths[entry] = 0;
1362
+ entryCounts[entry] = 0;
1363
+ freeEntries.push(entry);
1364
+ liveEntries--;
1365
+ }
1366
+ function change(token, spelling, delta) {
1367
+ let byToken = entriesByToken.get(token);
1368
+ const entry = byToken?.get(spelling);
1369
+ if (byToken !== void 0 && entry !== void 0) {
1370
+ const after = entryCounts[entry] + delta;
1371
+ if (after > 0) entryCounts[entry] = after;
1372
+ else removeEntry(byToken, token, spelling, entry);
1373
+ return;
1374
+ }
1375
+ if (delta <= 0) return;
1376
+ if (byToken === void 0) {
1377
+ byToken = /* @__PURE__ */ new Map();
1378
+ entriesByToken.set(token, byToken);
1379
+ }
1380
+ addEntry(byToken, token, spelling, delta);
1381
+ if (byToken.size === 0) entriesByToken.delete(token);
1382
+ }
1383
+ function encodedSpellings() {
1384
+ const encoded = { fields: [...fields], tokens: [], spellings: [], counts: [] };
1385
+ for (const [token, byToken] of entriesByToken) {
1386
+ for (const [spelling, entry] of byToken) {
1387
+ encoded.tokens.push(token);
1388
+ encoded.spellings.push(spelling);
1389
+ encoded.counts.push(entryCounts[entry]);
1390
+ }
1391
+ }
1392
+ return encoded;
1393
+ }
1394
+ function applyCounts(source2, direction) {
1395
+ for (const [token, byToken] of source2) {
1396
+ for (const [spelling, count] of byToken) change(token, spelling, count * direction);
1397
+ }
1398
+ }
1399
+ function adoptFields(next) {
1400
+ if (sameFields(fields, next)) return;
1401
+ fields = [...next];
1402
+ resetSpellings();
1403
+ everyDocumentPending = fields.length > 0;
1404
+ }
1405
+ function documentAt(internalId) {
1406
+ const docId = view.docStore.getExternalId(internalId);
1407
+ return docId === void 0 ? void 0 : view.docStore.get(docId)?.fields;
1408
+ }
1409
+ function resolvePending(analysis) {
1410
+ if (everyDocumentPending) {
1411
+ resetSpellings();
1412
+ const derived2 = /* @__PURE__ */ new Map();
1413
+ for (const [, stored] of view.docStore.all()) countDocumentSpellings(derived2, stored.fields, analysis, 1);
1414
+ applyCounts(derived2, 1);
1415
+ everyDocumentPending = false;
1416
+ return;
1417
+ }
1418
+ if (pending.size === 0) return;
1419
+ const derived = /* @__PURE__ */ new Map();
1420
+ for (const internalId of pending) {
1421
+ const document = documentAt(internalId);
1422
+ if (document !== void 0) countDocumentSpellings(derived, document, analysis, 1);
1423
+ }
1424
+ pending.clear();
1425
+ applyCounts(derived, 1);
1426
+ }
1427
+ const reader = {
1428
+ forEachContaining(probe, visit) {
1429
+ const points = probe.codePoints;
1430
+ if (points.length < PARTIAL_WORD_RUN_CODE_POINTS) return;
1431
+ let narrowest = null;
1432
+ for (let at = 0; at + PARTIAL_WORD_RUN_CODE_POINTS <= points.length; at++) {
1433
+ const members = runs.get(packedRunKeyAt(points, at));
1434
+ if (members === void 0) return;
1435
+ if (narrowest === null || members.size < narrowest.size) narrowest = members;
1436
+ }
1437
+ if (narrowest === null) return;
1438
+ for (const entry of narrowest) {
1439
+ const length = entryLengths[entry];
1440
+ if (length <= points.length) continue;
1441
+ if (!entrySpellings[entry].includes(probe.text)) continue;
1442
+ visit(entryTokens[entry], length);
1443
+ }
1444
+ },
1445
+ bytes() {
1446
+ return slot.bytes();
1447
+ }
1448
+ };
1449
+ const slot = {
1450
+ growingRegions: () => source?.spellings ?? null,
1451
+ adopt(segment, nextFields) {
1452
+ resetSpellings();
1453
+ fields = [...nextFields];
1454
+ everyDocumentPending = false;
1455
+ source = segment;
1456
+ sourceReader = growingSpellingReader(segment);
1457
+ },
1458
+ detach() {
1459
+ if (source === null) return;
1460
+ const encoded = growingSpellings(source, fields);
1461
+ source = null;
1462
+ sourceReader = null;
1463
+ for (let at = 0; at < encoded.tokens.length; at++)
1464
+ change(encoded.tokens[at], encoded.spellings[at], encoded.counts[at]);
1465
+ },
1466
+ readerFor(analysis) {
1467
+ if (analysis.fields.length === 0) return null;
1468
+ if (sourceReader !== null && sameFields(fields, analysis.fields)) return sourceReader;
1469
+ slot.detach();
1470
+ adoptFields(analysis.fields);
1471
+ resolvePending(analysis);
1472
+ addEntriesToRuns();
1473
+ return reader;
1474
+ },
1475
+ spellings() {
1476
+ if (fields.length === 0 || everyDocumentPending || pending.size > 0) return null;
1477
+ if (source !== null) return growingSpellings(source, fields);
1478
+ return encodedSpellings();
1479
+ },
1480
+ afterDocument(documentFields, internalId, spellings, direction) {
1481
+ if (source !== null) return;
1482
+ if (documentFields.length === 0) return;
1483
+ if (!sameFields(fields, documentFields)) {
1484
+ const holdsOnlyThisDocument = direction === 1 && view.docStore.count() === 1;
1485
+ adoptFields(documentFields);
1486
+ if (holdsOnlyThisDocument) everyDocumentPending = false;
1487
+ }
1488
+ if (everyDocumentPending) return;
1489
+ if (direction === -1 && pending.delete(internalId)) return;
1490
+ if (spellings !== null) {
1491
+ applyCounts(spellings, direction);
1492
+ } else if (direction === 1) {
1493
+ pending.add(internalId);
1494
+ } else {
1495
+ everyDocumentPending = true;
1496
+ }
1497
+ },
1498
+ afterMerge(spellings, internalIds) {
1499
+ if (internalIds.length === 0) return;
1500
+ slot.detach();
1501
+ if (fields.length === 0 && spellings !== null && view.docStore.count() === internalIds.length) {
1502
+ adoptFields(spellings.fields);
1503
+ everyDocumentPending = false;
1504
+ }
1505
+ if (fields.length === 0 || everyDocumentPending) return;
1506
+ if (spellings === null || !sameFields(fields, spellings.fields)) {
1507
+ for (const internalId of internalIds) pending.add(internalId);
1508
+ return;
1509
+ }
1510
+ for (let at = 0; at < spellings.tokens.length; at++) {
1511
+ change(spellings.tokens[at], spellings.spellings[at], spellings.counts[at]);
1512
+ }
1513
+ },
1514
+ forgetSpellings() {
1515
+ source = null;
1516
+ sourceReader = null;
1517
+ resetSpellings();
1518
+ everyDocumentPending = fields.length > 0;
1519
+ },
1520
+ clear() {
1521
+ source = null;
1522
+ sourceReader = null;
1523
+ fields = [];
1524
+ everyDocumentPending = false;
1525
+ resetSpellings();
1526
+ },
1527
+ bytes() {
1528
+ return liveEntries * GROWING_PARTIAL_ENTRY_BYTES + runs.size * GROWING_PARTIAL_RUN_BYTES + runMembers * GROWING_PARTIAL_RUN_MEMBER_BYTES + entriesOutsideRuns.length * GROWING_PARTIAL_PENDING_ENTRY_BYTES;
1529
+ }
1530
+ };
1531
+ return slot;
1532
+ }
1533
+
1534
+ // src/core/partition/partial-words/highlight.ts
1535
+ function containsPartialWord(spelling, words) {
1536
+ for (const word of words) {
1537
+ if (spelling.length > word.length && spelling.includes(word)) return true;
1538
+ }
1539
+ return false;
1540
+ }
1541
+ function partialWordMatcher(text, language, partial) {
1542
+ const indexed = tokenize(text, language, {
1543
+ stem: true,
1544
+ removeStopWords: true,
1545
+ collectSurfaces: true,
1546
+ customTokenizer: partial.customTokenizer,
1547
+ stopWordOverride: partial.stopWordOverride
1548
+ });
1549
+ const positions = /* @__PURE__ */ new Set();
1550
+ const spellings = /* @__PURE__ */ new Set();
1551
+ for (let at = 0; at < indexed.tokens.length; at++) {
1552
+ const spelling = indexed.surfaces?.[at] ?? indexed.tokens[at].token;
1553
+ if (!containsPartialWord(spelling, partial.words)) continue;
1554
+ positions.add(indexed.tokens[at].position);
1555
+ spellings.add(spelling);
1556
+ }
1557
+ if (partial.customTokenizer !== void 0) return (_position, fieldToken) => spellings.has(fieldToken);
1558
+ return (position) => positions.has(position);
1559
+ }
1560
+
1561
+ // src/core/partition/partial-words/query.ts
1562
+ function addPartialWords(group, added) {
1563
+ const membersByToken = /* @__PURE__ */ new Map();
1564
+ for (const member of group.members) membersByToken.set(member.token, member);
1565
+ for (const word of added) {
1566
+ const member = membersByToken.get(word.token);
1567
+ if (member === void 0) {
1568
+ group.members.push({ token: word.token, factor: 0, partialFactor: word.factor });
1569
+ } else if (word.factor > member.partialFactor) {
1570
+ member.partialFactor = word.factor;
1571
+ }
1572
+ }
1573
+ }
1574
+ function partialWordRequests(query) {
1575
+ const eligible = [];
1576
+ for (const queryToken of query.queryTokens) {
1577
+ const word = query.written.get(queryToken.position);
1578
+ if (word === void 0 || codePointCount(word) < PARTIAL_WORD_MIN_CODE_POINTS) continue;
1579
+ eligible.push({ token: queryToken.token, written: word });
1580
+ if (eligible.length === PARTIAL_WORDS_ADDED_PER_QUERY) break;
1581
+ }
1582
+ const count = eligible.length;
1583
+ if (count === 0) return [];
1584
+ const evenShare = Math.floor(PARTIAL_WORDS_ADDED_PER_QUERY / count);
1585
+ const remainder = PARTIAL_WORDS_ADDED_PER_QUERY % count;
1586
+ return eligible.map((entry, order) => ({
1587
+ token: entry.token,
1588
+ request: {
1589
+ written: entry.written,
1590
+ ownToken: entry.token,
1591
+ share: evenShare + (order < remainder ? 1 : 0),
1592
+ tolerance: entry.token === query.prefixToken ? 0 : query.tolerance,
1593
+ prefixLength: query.prefixLength ?? DEFAULT_FUZZY_PREFIX_LENGTH
1594
+ }
1595
+ }));
1596
+ }
1597
+ function addPartialWordMembers(query, expand, groupOf) {
1598
+ const requests = partialWordRequests(query);
1599
+ const added = expand(
1600
+ query.analysis,
1601
+ requests.map((entry) => entry.request)
1602
+ );
1603
+ for (let at = 0; at < requests.length; at++) {
1604
+ if (added[at].length === 0) continue;
1605
+ const token = requests[at].token;
1606
+ addPartialWords(groupOf(token, token !== query.prefixToken), added[at]);
1607
+ }
1608
+ }
1609
+ function partialWordsToHighlight(queryTokens, writtenWords) {
1610
+ const writtenAt = /* @__PURE__ */ new Map();
1611
+ for (const word of writtenWords) {
1612
+ if (!writtenAt.has(word.position)) writtenAt.set(word.position, word.token);
1613
+ }
1614
+ const seenTokens = /* @__PURE__ */ new Set();
1615
+ const words = [];
1616
+ for (const queryToken of queryTokens) {
1617
+ if (seenTokens.has(queryToken.token)) continue;
1618
+ seenTokens.add(queryToken.token);
1619
+ const word = writtenAt.get(queryToken.position);
1620
+ if (word === void 0 || codePointCount(word) < PARTIAL_WORD_MIN_CODE_POINTS) continue;
1621
+ words.push(word);
1622
+ if (words.length === PARTIAL_WORDS_ADDED_PER_QUERY) break;
1623
+ }
1624
+ return words;
1625
+ }
1626
+
1627
+ // src/core/partition/partial-words/module.ts
1628
+ var partialWordSearchModule = {
1629
+ name: "partialWords",
1630
+ bindExpansionCode: bindPartialExpansionCode,
1631
+ createSpellingTable: () => new SpellingTable(),
1632
+ prepareSpellingRuns(table, plan) {
1633
+ const runs = new RunPlan(table, collectRuns(plan));
1634
+ return () => runs.publish();
1635
+ },
1636
+ prepareSpellingRemoval(table, plan) {
1637
+ return prepareRunRemoval(table, collectRuns(plan));
1638
+ },
1639
+ prepareSpellingVacuum(table, mapping) {
1640
+ const vacuum = new RunVacuum(table, mapping);
1641
+ return () => vacuum.publish();
1642
+ },
1643
+ createGrowingSlot: createGrowingPartialWordSlot,
1644
+ createFrozenSlot: createFrozenPartialWordSlot,
1645
+ buildTable: buildPartialWordTable,
1646
+ expand: expandPartialWords2,
1647
+ countSpelling,
1648
+ encodeSpellings: encodePartialSpellings,
1649
+ spellingCountsOf,
1650
+ highlightMatcher: partialWordMatcher,
1651
+ addTermMembers: addPartialWordMembers,
1652
+ highlightWords: partialWordsToHighlight
1653
+ };
1654
+
1655
+ // src/partial-words/index.ts
1656
+ var partialWordSearch = partialWordSearchModule;
1657
+
1658
+ export { partialWordSearch };