@songtonyli/dsh-cli 0.1.75 → 0.1.77

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 (681) hide show
  1. package/README.md +1 -1
  2. package/bin/dsh.mjs +1 -1
  3. package/node_modules/@deepseek-ai/dsh/README.i18n.yaml +2 -2
  4. package/node_modules/@deepseek-ai/dsh/README.md +1 -1
  5. package/node_modules/@deepseek-ai/dsh/README.zh.md +1 -1
  6. package/node_modules/@deepseek-ai/dsh/package.json +117 -116
  7. package/node_modules/@deepseek-ai/dsh-acp/package.json +23 -23
  8. package/node_modules/@deepseek-ai/dsh-acp-app/package.json +3 -3
  9. package/node_modules/@deepseek-ai/dsh-agent/package.json +18 -18
  10. package/node_modules/@deepseek-ai/dsh-agent-default-model/package.json +7 -7
  11. package/node_modules/@deepseek-ai/dsh-agent-instructions/package.json +24 -24
  12. package/node_modules/@deepseek-ai/dsh-agent-loop/package.json +22 -22
  13. package/node_modules/@deepseek-ai/dsh-agent-preset/package.json +6 -6
  14. package/node_modules/@deepseek-ai/dsh-agent-preset/skills/cordis-composition-reference/references/packages.md +3 -1
  15. package/node_modules/@deepseek-ai/dsh-agent-preset-registry/package.json +26 -26
  16. package/node_modules/@deepseek-ai/dsh-agent-tool-presentation/package.json +8 -8
  17. package/node_modules/@deepseek-ai/dsh-anonymous-user-id/package.json +5 -5
  18. package/node_modules/@deepseek-ai/dsh-api-account-controller/lib/typert.host.js +7 -6
  19. package/node_modules/@deepseek-ai/dsh-api-account-controller/lib/typert.remote-client.js +7 -6
  20. package/node_modules/@deepseek-ai/dsh-api-account-controller/package.json +7 -7
  21. package/node_modules/@deepseek-ai/dsh-api-authorization-controller/package.json +9 -9
  22. package/node_modules/@deepseek-ai/dsh-api-gateway/package.json +11 -11
  23. package/node_modules/@deepseek-ai/dsh-api-job-controller/package.json +9 -10
  24. package/node_modules/@deepseek-ai/dsh-api-remote-access/package.json +7 -8
  25. package/node_modules/@deepseek-ai/dsh-api-remotes/lib/client.js +490 -62
  26. package/node_modules/@deepseek-ai/dsh-api-remotes/lib/index.js +4 -0
  27. package/node_modules/@deepseek-ai/dsh-api-remotes/lib/types/client/index.js +3 -1
  28. package/node_modules/@deepseek-ai/dsh-api-remotes/lib/types/remote-events.js +1 -0
  29. package/node_modules/@deepseek-ai/dsh-api-remotes/package.json +42 -41
  30. package/node_modules/@deepseek-ai/dsh-api-session-controller/lib/typert.host.js +1 -1
  31. package/node_modules/@deepseek-ai/dsh-api-session-controller/package.json +72 -73
  32. package/node_modules/@deepseek-ai/dsh-api-settings-controller/package.json +13 -13
  33. package/node_modules/@deepseek-ai/dsh-api-terminal-controller/README.i18n.yaml +2 -2
  34. package/node_modules/@deepseek-ai/dsh-api-terminal-controller/README.md +2 -0
  35. package/node_modules/@deepseek-ai/dsh-api-terminal-controller/README.zh.md +2 -0
  36. package/node_modules/@deepseek-ai/dsh-api-terminal-controller/lib/client.js +166 -21
  37. package/node_modules/@deepseek-ai/dsh-api-terminal-controller/lib/types/client/index.js +36 -9
  38. package/node_modules/@deepseek-ai/dsh-api-terminal-controller/lib/types/client/inventory.js +101 -0
  39. package/node_modules/@deepseek-ai/dsh-api-terminal-controller/lib/types/client/model.js +23 -7
  40. package/node_modules/@deepseek-ai/dsh-api-terminal-controller/package.json +19 -19
  41. package/node_modules/@deepseek-ai/dsh-api-workspace-controller/package.json +18 -19
  42. package/node_modules/@deepseek-ai/dsh-api-workspace-files/package.json +12 -13
  43. package/node_modules/@deepseek-ai/dsh-app-boot/README.i18n.yaml +2 -2
  44. package/node_modules/@deepseek-ai/dsh-app-boot/README.md +2 -2
  45. package/node_modules/@deepseek-ai/dsh-app-boot/README.zh.md +2 -2
  46. package/node_modules/@deepseek-ai/dsh-app-boot/lib/index.js +40 -26
  47. package/node_modules/@deepseek-ai/dsh-app-boot/package.json +8 -8
  48. package/node_modules/@deepseek-ai/dsh-atomic-write/package.json +1 -1
  49. package/node_modules/@deepseek-ai/dsh-attachment/package.json +3 -3
  50. package/node_modules/@deepseek-ai/dsh-attachment-local/package.json +6 -6
  51. package/node_modules/@deepseek-ai/dsh-authorization/package.json +5 -5
  52. package/node_modules/@deepseek-ai/dsh-base/package.json +94 -94
  53. package/node_modules/@deepseek-ai/dsh-bash-local/package.json +8 -8
  54. package/node_modules/@deepseek-ai/dsh-bash-sandbox/package.json +12 -12
  55. package/node_modules/@deepseek-ai/dsh-brand/package.json +1 -1
  56. package/node_modules/@deepseek-ai/dsh-chunked-list/package.json +1 -1
  57. package/node_modules/@deepseek-ai/dsh-client-connection/package.json +10 -10
  58. package/node_modules/@deepseek-ai/dsh-client-file-upload/package.json +10 -10
  59. package/node_modules/@deepseek-ai/dsh-client-hmr/README.i18n.yaml +2 -2
  60. package/node_modules/@deepseek-ai/dsh-client-hmr/README.md +1 -1
  61. package/node_modules/@deepseek-ai/dsh-client-hmr/README.zh.md +1 -1
  62. package/node_modules/@deepseek-ai/dsh-client-hmr/lib/client.js +237 -11
  63. package/node_modules/@deepseek-ai/dsh-client-hmr/package.json +5 -4
  64. package/node_modules/@deepseek-ai/dsh-client-locale/package.json +7 -10
  65. package/node_modules/@deepseek-ai/dsh-client-modules/README.i18n.yaml +10 -7
  66. package/node_modules/@deepseek-ai/dsh-client-modules/README.md +8 -2
  67. package/node_modules/@deepseek-ai/dsh-client-modules/README.zh.md +9 -2
  68. package/node_modules/@deepseek-ai/dsh-client-modules/lib/client.js +136 -6
  69. package/node_modules/@deepseek-ai/dsh-client-modules/lib/index.js +25 -6
  70. package/node_modules/@deepseek-ai/dsh-client-modules/package.json +3 -4
  71. package/node_modules/@deepseek-ai/dsh-client-product-analytics/README.i18n.yaml +6 -6
  72. package/node_modules/@deepseek-ai/dsh-client-product-analytics/README.md +22 -4
  73. package/node_modules/@deepseek-ai/dsh-client-product-analytics/README.zh.md +22 -4
  74. package/node_modules/@deepseek-ai/dsh-client-product-analytics/lib/index.js +238 -6
  75. package/node_modules/@deepseek-ai/dsh-client-product-analytics/lib/typert.host.js +6 -6
  76. package/node_modules/@deepseek-ai/dsh-client-product-analytics/lib/typert.remote-client.js +4 -4
  77. package/node_modules/@deepseek-ai/dsh-client-product-analytics/package.json +12 -11
  78. package/node_modules/@deepseek-ai/dsh-client-resources/package.json +3 -5
  79. package/node_modules/@deepseek-ai/dsh-client-shortcuts/package.json +6 -8
  80. package/node_modules/@deepseek-ai/dsh-client-ui-agent-preset/package.json +14 -17
  81. package/node_modules/@deepseek-ai/dsh-client-ui-approval/package.json +11 -13
  82. package/node_modules/@deepseek-ai/dsh-client-ui-attachment/package.json +8 -10
  83. package/node_modules/@deepseek-ai/dsh-client-ui-brand-official/package.json +3 -4
  84. package/node_modules/@deepseek-ai/dsh-client-ui-chat/package.json +34 -37
  85. package/node_modules/@deepseek-ai/dsh-client-ui-commands/README.i18n.yaml +2 -2
  86. package/node_modules/@deepseek-ai/dsh-client-ui-commands/README.md +1 -1
  87. package/node_modules/@deepseek-ai/dsh-client-ui-commands/README.zh.md +1 -1
  88. package/node_modules/@deepseek-ai/dsh-client-ui-commands/lib/client.js +16 -1
  89. package/node_modules/@deepseek-ai/dsh-client-ui-commands/package.json +12 -15
  90. package/node_modules/@deepseek-ai/dsh-client-ui-conversation/README.i18n.yaml +2 -2
  91. package/node_modules/@deepseek-ai/dsh-client-ui-conversation/README.md +2 -2
  92. package/node_modules/@deepseek-ai/dsh-client-ui-conversation/README.zh.md +2 -2
  93. package/node_modules/@deepseek-ai/dsh-client-ui-conversation/lib/client.js +356 -124
  94. package/node_modules/@deepseek-ai/dsh-client-ui-conversation/package.json +30 -33
  95. package/node_modules/@deepseek-ai/dsh-client-ui-cordis/package.json +11 -13
  96. package/node_modules/@deepseek-ai/dsh-client-ui-deliverables/package.json +25 -28
  97. package/node_modules/@deepseek-ai/dsh-client-ui-directory-picker-browse/package.json +5 -7
  98. package/node_modules/@deepseek-ai/dsh-client-ui-directory-picker-native/package.json +3 -3
  99. package/node_modules/@deepseek-ai/dsh-client-ui-goal/package.json +12 -14
  100. package/node_modules/@deepseek-ai/dsh-client-ui-input-trigger/README.i18n.yaml +2 -2
  101. package/node_modules/@deepseek-ai/dsh-client-ui-input-trigger/README.md +1 -1
  102. package/node_modules/@deepseek-ai/dsh-client-ui-input-trigger/README.zh.md +1 -1
  103. package/node_modules/@deepseek-ai/dsh-client-ui-input-trigger/lib/client.js +18 -0
  104. package/node_modules/@deepseek-ai/dsh-client-ui-input-trigger/package.json +9 -12
  105. package/node_modules/@deepseek-ai/dsh-client-ui-jobs/package.json +8 -10
  106. package/node_modules/@deepseek-ai/dsh-client-ui-layout/package.json +7 -9
  107. package/node_modules/@deepseek-ai/dsh-client-ui-message-feedback/package.json +13 -16
  108. package/node_modules/@deepseek-ai/dsh-client-ui-model-selection/README.i18n.yaml +3 -0
  109. package/node_modules/@deepseek-ai/dsh-client-ui-model-selection/README.md +8 -0
  110. package/node_modules/@deepseek-ai/dsh-client-ui-model-selection/README.zh.md +8 -0
  111. package/node_modules/@deepseek-ai/dsh-client-ui-model-selection/lib/client.js +34 -8
  112. package/node_modules/@deepseek-ai/dsh-client-ui-model-selection/package.json +13 -16
  113. package/node_modules/@deepseek-ai/dsh-client-ui-open-in-app/package.json +16 -19
  114. package/node_modules/@deepseek-ai/dsh-client-ui-permission-presets/package.json +12 -15
  115. package/node_modules/@deepseek-ai/dsh-client-ui-plan/package.json +17 -20
  116. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-manager/README.i18n.yaml +5 -2
  117. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-manager/README.md +4 -0
  118. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-manager/README.zh.md +4 -0
  119. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-manager/lib/client.js +314 -85
  120. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-manager/package.json +13 -16
  121. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-market/README.i18n.yaml +34 -0
  122. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-market/README.md +72 -0
  123. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-market/README.zh.md +72 -0
  124. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-market/lib/client.js +1637 -0
  125. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-market/lib/index.js +6 -0
  126. package/node_modules/@deepseek-ai/dsh-client-ui-plugin-market/package.json +62 -0
  127. package/node_modules/@deepseek-ai/dsh-client-ui-reference/package.json +13 -15
  128. package/node_modules/@deepseek-ai/dsh-client-ui-renderer/package.json +1 -2
  129. package/node_modules/@deepseek-ai/dsh-client-ui-schedule/package.json +17 -19
  130. package/node_modules/@deepseek-ai/dsh-client-ui-secret-mask/package.json +4 -5
  131. package/node_modules/@deepseek-ai/dsh-client-ui-session/package.json +7 -9
  132. package/node_modules/@deepseek-ai/dsh-client-ui-settings/package.json +4 -6
  133. package/node_modules/@deepseek-ai/dsh-client-ui-settings-account/README.i18n.yaml +2 -2
  134. package/node_modules/@deepseek-ai/dsh-client-ui-settings-account/README.md +1 -1
  135. package/node_modules/@deepseek-ai/dsh-client-ui-settings-account/README.zh.md +1 -1
  136. package/node_modules/@deepseek-ai/dsh-client-ui-settings-account/lib/client.js +117 -66
  137. package/node_modules/@deepseek-ai/dsh-client-ui-settings-account/package.json +13 -16
  138. package/node_modules/@deepseek-ai/dsh-client-ui-settings-agent-loop/package.json +6 -9
  139. package/node_modules/@deepseek-ai/dsh-client-ui-settings-general/README.i18n.yaml +5 -2
  140. package/node_modules/@deepseek-ai/dsh-client-ui-settings-general/README.md +6 -0
  141. package/node_modules/@deepseek-ai/dsh-client-ui-settings-general/README.zh.md +6 -0
  142. package/node_modules/@deepseek-ai/dsh-client-ui-settings-general/lib/client.js +176 -39
  143. package/node_modules/@deepseek-ai/dsh-client-ui-settings-general/package.json +15 -14
  144. package/node_modules/@deepseek-ai/dsh-client-ui-settings-models/README.i18n.yaml +2 -2
  145. package/node_modules/@deepseek-ai/dsh-client-ui-settings-models/README.md +3 -1
  146. package/node_modules/@deepseek-ai/dsh-client-ui-settings-models/README.zh.md +3 -1
  147. package/node_modules/@deepseek-ai/dsh-client-ui-settings-models/lib/client.js +324 -36
  148. package/node_modules/@deepseek-ai/dsh-client-ui-settings-models/package.json +8 -11
  149. package/node_modules/@deepseek-ai/dsh-client-ui-settings-plugin-inventory/package.json +9 -12
  150. package/node_modules/@deepseek-ai/dsh-client-ui-settings-plugins/package.json +5 -6
  151. package/node_modules/@deepseek-ai/dsh-client-ui-settings-session-log/package.json +6 -9
  152. package/node_modules/@deepseek-ai/dsh-client-ui-settings-shell/package.json +6 -9
  153. package/node_modules/@deepseek-ai/dsh-client-ui-settings-signin/package.json +4 -7
  154. package/node_modules/@deepseek-ai/dsh-client-ui-settings-subagent/package.json +6 -9
  155. package/node_modules/@deepseek-ai/dsh-client-ui-settings-web-search/package.json +6 -9
  156. package/node_modules/@deepseek-ai/dsh-client-ui-shortcuts/package.json +7 -10
  157. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/lib/client.js +1 -1
  158. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar/package.json +9 -12
  159. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar-browser/package.json +8 -11
  160. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar-documentpreview/package.json +15 -18
  161. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar-files/package.json +11 -14
  162. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar-right/lib/client.js +1 -1
  163. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar-right/package.json +12 -15
  164. package/node_modules/@deepseek-ai/dsh-client-ui-sidebar-terminal/package.json +12 -15
  165. package/node_modules/@deepseek-ai/dsh-client-ui-skill/README.i18n.yaml +6 -6
  166. package/node_modules/@deepseek-ai/dsh-client-ui-skill/README.md +5 -3
  167. package/node_modules/@deepseek-ai/dsh-client-ui-skill/README.zh.md +5 -3
  168. package/node_modules/@deepseek-ai/dsh-client-ui-skill/lib/client.js +1 -0
  169. package/node_modules/@deepseek-ai/dsh-client-ui-skill/package.json +14 -15
  170. package/node_modules/@deepseek-ai/dsh-client-ui-subagent/package.json +15 -17
  171. package/node_modules/@deepseek-ai/dsh-client-ui-theme/package.json +9 -12
  172. package/node_modules/@deepseek-ai/dsh-client-ui-tool/README.i18n.yaml +2 -2
  173. package/node_modules/@deepseek-ai/dsh-client-ui-tool/README.md +1 -1
  174. package/node_modules/@deepseek-ai/dsh-client-ui-tool/README.zh.md +1 -1
  175. package/node_modules/@deepseek-ai/dsh-client-ui-tool/lib/client.js +75 -25
  176. package/node_modules/@deepseek-ai/dsh-client-ui-tool/package.json +25 -27
  177. package/node_modules/@deepseek-ai/dsh-client-ui-trajectory/package.json +14 -17
  178. package/node_modules/@deepseek-ai/dsh-client-ui-user-questions/package.json +18 -21
  179. package/node_modules/@deepseek-ai/dsh-client-ui-workflow-run/package.json +11 -14
  180. package/node_modules/@deepseek-ai/dsh-client-ui-workspace/package.json +23 -26
  181. package/node_modules/@deepseek-ai/dsh-cmdline/package.json +1 -1
  182. package/node_modules/@deepseek-ai/dsh-command-compact/package.json +8 -8
  183. package/node_modules/@deepseek-ai/dsh-command-feedback/package.json +11 -11
  184. package/node_modules/@deepseek-ai/dsh-command-goal/package.json +11 -11
  185. package/node_modules/@deepseek-ai/dsh-commands/package.json +16 -16
  186. package/node_modules/@deepseek-ai/dsh-compaction/package.json +9 -9
  187. package/node_modules/@deepseek-ai/dsh-compaction-basic/package.json +21 -21
  188. package/node_modules/@deepseek-ai/dsh-compaction-image-offload/package.json +16 -16
  189. package/node_modules/@deepseek-ai/dsh-compaction-tool-result-pruner/package.json +12 -12
  190. package/node_modules/@deepseek-ai/dsh-config-editor/package.json +6 -6
  191. package/node_modules/@deepseek-ai/dsh-cordis-client-runner/lib/client.js +95 -8
  192. package/node_modules/@deepseek-ai/dsh-cordis-client-runner/package.json +6 -6
  193. package/node_modules/@deepseek-ai/dsh-cordis-host-runner/package.json +18 -18
  194. package/node_modules/@deepseek-ai/dsh-credentials/package.json +2 -2
  195. package/node_modules/@deepseek-ai/dsh-credentials-local/package.json +9 -9
  196. package/node_modules/@deepseek-ai/dsh-deepseek-account/package.json +9 -7
  197. package/node_modules/@deepseek-ai/dsh-deepseek-account-platform/README.i18n.yaml +2 -2
  198. package/node_modules/@deepseek-ai/dsh-deepseek-account-platform/README.md +1 -1
  199. package/node_modules/@deepseek-ai/dsh-deepseek-account-platform/README.zh.md +1 -1
  200. package/node_modules/@deepseek-ai/dsh-deepseek-account-platform/lib/index.js +15 -13
  201. package/node_modules/@deepseek-ai/dsh-deepseek-account-platform/lib/types/details.js +10 -4
  202. package/node_modules/@deepseek-ai/dsh-deepseek-account-platform/package.json +9 -9
  203. package/node_modules/@deepseek-ai/dsh-deepseek-llm-api-extensions/package.json +1 -1
  204. package/node_modules/@deepseek-ai/dsh-deque/package.json +1 -1
  205. package/node_modules/@deepseek-ai/dsh-experimental-agent-team/package.json +19 -19
  206. package/node_modules/@deepseek-ai/dsh-experimental-agent-team-profile/package.json +4 -4
  207. package/node_modules/@deepseek-ai/dsh-experimental-api-speech-to-text/package.json +5 -5
  208. package/node_modules/@deepseek-ai/dsh-experimental-auto-review/package.json +38 -38
  209. package/node_modules/@deepseek-ai/dsh-experimental-badge-skill-bundle/package.json +2 -2
  210. package/node_modules/@deepseek-ai/dsh-experimental-client-ui-agent-team/package.json +9 -22
  211. package/node_modules/@deepseek-ai/dsh-experimental-client-ui-cot-translation/package.json +18 -26
  212. package/node_modules/@deepseek-ai/dsh-experimental-client-ui-voice-input/package.json +14 -32
  213. package/node_modules/@deepseek-ai/dsh-experimental-cot-translation-bundle/package.json +3 -3
  214. package/node_modules/@deepseek-ai/dsh-experimental-inspector/README.i18n.yaml +2 -2
  215. package/node_modules/@deepseek-ai/dsh-experimental-inspector/README.md +1 -1
  216. package/node_modules/@deepseek-ai/dsh-experimental-inspector/README.zh.md +1 -1
  217. package/node_modules/@deepseek-ai/dsh-experimental-inspector/lib/index.js +70 -17
  218. package/node_modules/@deepseek-ai/dsh-experimental-inspector/lib/worker.js +16 -12
  219. package/node_modules/@deepseek-ai/dsh-experimental-inspector/package.json +14 -20
  220. package/node_modules/@deepseek-ai/dsh-experimental-inspector-profile/package.json +3 -3
  221. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/README.i18n.yaml +34 -0
  222. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/README.md +68 -0
  223. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/README.zh.md +68 -0
  224. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/cordis.patch.yml +10 -0
  225. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/icon.svg +9 -0
  226. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/lib/index.js +1 -0
  227. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/locale/en.json +6 -0
  228. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/locale/zh.json +6 -0
  229. package/node_modules/@deepseek-ai/dsh-experimental-plugin-market-bundle/package.json +50 -0
  230. package/node_modules/@deepseek-ai/dsh-experimental-ralph-bundle/package.json +3 -3
  231. package/node_modules/@deepseek-ai/dsh-experimental-session-inspector/package.json +10 -13
  232. package/node_modules/@deepseek-ai/dsh-experimental-session-search/package.json +3 -3
  233. package/node_modules/@deepseek-ai/dsh-experimental-session-titles-bundle/package.json +2 -2
  234. package/node_modules/@deepseek-ai/dsh-experimental-speech-to-text/package.json +6 -6
  235. package/node_modules/@deepseek-ai/dsh-experimental-speech-to-text-sensevoice/lib/worker.js +26 -12
  236. package/node_modules/@deepseek-ai/dsh-experimental-speech-to-text-sensevoice/package.json +7 -7
  237. package/node_modules/@deepseek-ai/dsh-experimental-terminal-bundle/package.json +4 -4
  238. package/node_modules/@deepseek-ai/dsh-experimental-tool-agent-team/package.json +22 -22
  239. package/node_modules/@deepseek-ai/dsh-experimental-tool-worktree/package.json +18 -18
  240. package/node_modules/@deepseek-ai/dsh-experimental-translator/package.json +24 -24
  241. package/node_modules/@deepseek-ai/dsh-experimental-voice-input-bundle/package.json +5 -5
  242. package/node_modules/@deepseek-ai/dsh-experimental-worktree/package.json +21 -21
  243. package/node_modules/@deepseek-ai/dsh-file-reference/package.json +3 -3
  244. package/node_modules/@deepseek-ai/dsh-file-reference-local/package.json +13 -13
  245. package/node_modules/@deepseek-ai/dsh-fs/package.json +7 -7
  246. package/node_modules/@deepseek-ai/dsh-fs-local/package.json +4 -4
  247. package/node_modules/@deepseek-ai/dsh-fs-observation-policy/package.json +4 -4
  248. package/node_modules/@deepseek-ai/dsh-fs-sandbox/package.json +10 -10
  249. package/node_modules/@deepseek-ai/dsh-goal/package.json +20 -20
  250. package/node_modules/@deepseek-ai/dsh-goal-round-driver/package.json +15 -15
  251. package/node_modules/@deepseek-ai/dsh-harness-dev-context/package.json +11 -11
  252. package/node_modules/@deepseek-ai/dsh-headless/package.json +19 -19
  253. package/node_modules/@deepseek-ai/dsh-hmr/package.json +6 -6
  254. package/node_modules/@deepseek-ai/dsh-home-paths/package.json +1 -1
  255. package/node_modules/@deepseek-ai/dsh-host-directory-picker/package.json +1 -1
  256. package/node_modules/@deepseek-ai/dsh-host-directory-picker-auto/package.json +15 -15
  257. package/node_modules/@deepseek-ai/dsh-host-directory-picker-browse/package.json +2 -2
  258. package/node_modules/@deepseek-ai/dsh-host-directory-picker-native/package.json +3 -3
  259. package/node_modules/@deepseek-ai/dsh-host-frontend-static/package.json +6 -6
  260. package/node_modules/@deepseek-ai/dsh-host-open-in-app/package.json +5 -5
  261. package/node_modules/@deepseek-ai/dsh-host-plugin-inventory/package.json +9 -9
  262. package/node_modules/@deepseek-ai/dsh-host-product-telemetry-otel/package.json +4 -4
  263. package/node_modules/@deepseek-ai/dsh-host-webserver/package.json +1 -1
  264. package/node_modules/@deepseek-ai/dsh-http-proxy/package.json +1 -1
  265. package/node_modules/@deepseek-ai/dsh-jobs/package.json +9 -9
  266. package/node_modules/@deepseek-ai/dsh-jobs-local/package.json +11 -11
  267. package/node_modules/@deepseek-ai/dsh-launch-environment/package.json +1 -1
  268. package/node_modules/@deepseek-ai/dsh-lazy-require/package.json +1 -1
  269. package/node_modules/@deepseek-ai/dsh-llm/package.json +7 -7
  270. package/node_modules/@deepseek-ai/dsh-llm-cursor/package.json +20 -20
  271. package/node_modules/@deepseek-ai/dsh-llm-deepseek/package.json +37 -37
  272. package/node_modules/@deepseek-ai/dsh-llm-deepseek-account/package.json +13 -13
  273. package/node_modules/@deepseek-ai/dsh-llm-deepseek-api-key/package.json +9 -9
  274. package/node_modules/@deepseek-ai/dsh-llm-pi-ai/package.json +21 -21
  275. package/node_modules/@deepseek-ai/dsh-llm-retry/package.json +19 -19
  276. package/node_modules/@deepseek-ai/dsh-mcp-client/package.json +20 -20
  277. package/node_modules/@deepseek-ai/dsh-mcp-resources/package.json +12 -12
  278. package/node_modules/@deepseek-ai/dsh-message-feedback/package.json +14 -14
  279. package/node_modules/@deepseek-ai/dsh-native-command/package.json +2 -2
  280. package/node_modules/@deepseek-ai/dsh-no-progress-reminder/package.json +10 -10
  281. package/node_modules/@deepseek-ai/dsh-office-to-pdf/package.json +6 -6
  282. package/node_modules/@deepseek-ai/dsh-otel/package.json +3 -3
  283. package/node_modules/@deepseek-ai/dsh-output-retention/package.json +1 -1
  284. package/node_modules/@deepseek-ai/dsh-package-manifest/package.json +1 -1
  285. package/node_modules/@deepseek-ai/dsh-permission-presets/package.json +18 -18
  286. package/node_modules/@deepseek-ai/dsh-persona/package.json +4 -4
  287. package/node_modules/@deepseek-ai/dsh-plan-mode/package.json +20 -20
  288. package/node_modules/@deepseek-ai/dsh-plugin-manager/README.i18n.yaml +2 -2
  289. package/node_modules/@deepseek-ai/dsh-plugin-manager/README.md +2 -2
  290. package/node_modules/@deepseek-ai/dsh-plugin-manager/README.zh.md +2 -2
  291. package/node_modules/@deepseek-ai/dsh-plugin-manager/lib/index.js +4 -3
  292. package/node_modules/@deepseek-ai/dsh-plugin-manager/lib/typert.host.js +14 -14
  293. package/node_modules/@deepseek-ai/dsh-plugin-manager/lib/typert.remote-client.js +13 -13
  294. package/node_modules/@deepseek-ai/dsh-plugin-manager/lib/types/failure.js +2 -2
  295. package/node_modules/@deepseek-ai/dsh-plugin-manager/lib/types/operations.js +3 -3
  296. package/node_modules/@deepseek-ai/dsh-plugin-manager/lib/types/tools.js +1 -1
  297. package/node_modules/@deepseek-ai/dsh-plugin-manager/package.json +27 -27
  298. package/node_modules/@deepseek-ai/dsh-plugin-market/README.i18n.yaml +43 -0
  299. package/node_modules/@deepseek-ai/dsh-plugin-market/README.md +117 -0
  300. package/node_modules/@deepseek-ai/dsh-plugin-market/README.zh.md +117 -0
  301. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/index.js +661 -0
  302. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/typert.host.js +407 -0
  303. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/typert.remote-client.js +259 -0
  304. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/types/index-url.js +27 -0
  305. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/types/index.js +351 -0
  306. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/types/parse-thread.js +61 -0
  307. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/types/parse.js +36 -0
  308. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/types/search.js +135 -0
  309. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/types/tools.js +128 -0
  310. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/types/types.js +2 -0
  311. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/types/worker.js +8 -0
  312. package/node_modules/@deepseek-ai/dsh-plugin-market/lib/worker.cjs +1759 -0
  313. package/node_modules/@deepseek-ai/dsh-plugin-market/package.json +89 -0
  314. package/node_modules/@deepseek-ai/dsh-plugin-market-index/README.i18n.yaml +34 -0
  315. package/node_modules/@deepseek-ai/dsh-plugin-market-index/README.md +74 -0
  316. package/node_modules/@deepseek-ai/dsh-plugin-market-index/README.zh.md +74 -0
  317. package/node_modules/@deepseek-ai/dsh-plugin-market-index/lib/index.js +320 -0
  318. package/node_modules/@deepseek-ai/{dsh-client-ui-slots → dsh-plugin-market-index}/package.json +15 -12
  319. package/node_modules/@deepseek-ai/dsh-plugin-package-inventory-deepseek/package.json +13 -13
  320. package/node_modules/@deepseek-ai/dsh-ptc-runtime/package.json +3 -3
  321. package/node_modules/@deepseek-ai/dsh-ptc-runtime-node/package.json +20 -20
  322. package/node_modules/@deepseek-ai/dsh-pwsh-local/package.json +8 -8
  323. package/node_modules/@deepseek-ai/dsh-pwsh-sandbox/package.json +12 -12
  324. package/node_modules/@deepseek-ai/dsh-repeat-tool-reminder/package.json +9 -9
  325. package/node_modules/@deepseek-ai/dsh-sandbox/package.json +6 -6
  326. package/node_modules/@deepseek-ai/dsh-sandbox-apple-container/package.json +22 -22
  327. package/node_modules/@deepseek-ai/dsh-sandbox-local/package.json +9 -9
  328. package/node_modules/@deepseek-ai/dsh-sandbox-policy/package.json +11 -11
  329. package/node_modules/@deepseek-ai/dsh-sandbox-windows-acl/package.json +10 -10
  330. package/node_modules/@deepseek-ai/dsh-schedule/package.json +30 -30
  331. package/node_modules/@deepseek-ai/dsh-scope/package.json +1 -1
  332. package/node_modules/@deepseek-ai/dsh-sdk-app/README.i18n.yaml +4 -4
  333. package/node_modules/@deepseek-ai/dsh-sdk-app/README.md +2 -2
  334. package/node_modules/@deepseek-ai/dsh-sdk-app/README.zh.md +2 -2
  335. package/node_modules/@deepseek-ai/dsh-sdk-app/cordis.patch.yml +5 -0
  336. package/node_modules/@deepseek-ai/dsh-sdk-app/package.json +5 -5
  337. package/node_modules/@deepseek-ai/dsh-sdk-jsonrpc-server/package.json +23 -23
  338. package/node_modules/@deepseek-ai/dsh-sdk-minimal/package.json +34 -34
  339. package/node_modules/@deepseek-ai/dsh-sdk-protocol/package.json +7 -7
  340. package/node_modules/@deepseek-ai/dsh-session/package.json +8 -8
  341. package/node_modules/@deepseek-ai/dsh-session-checkpoint-policy/package.json +15 -15
  342. package/node_modules/@deepseek-ai/dsh-session-format/package.json +2 -2
  343. package/node_modules/@deepseek-ai/dsh-session-format-catalog/package.json +9 -9
  344. package/node_modules/@deepseek-ai/dsh-session-format-v0-to-v1/package.json +4 -4
  345. package/node_modules/@deepseek-ai/dsh-session-format-v1-to-v2/package.json +6 -6
  346. package/node_modules/@deepseek-ai/dsh-session-format-v2-to-v3/package.json +4 -4
  347. package/node_modules/@deepseek-ai/dsh-session-format-v3-to-v4/package.json +3 -3
  348. package/node_modules/@deepseek-ai/dsh-session-log-deepseek/package.json +13 -13
  349. package/node_modules/@deepseek-ai/dsh-session-log-export/package.json +18 -21
  350. package/node_modules/@deepseek-ai/dsh-session-persistence/package.json +9 -9
  351. package/node_modules/@deepseek-ai/dsh-session-persistence-jsonl/package.json +10 -10
  352. package/node_modules/@deepseek-ai/dsh-session-projection/package.json +3 -3
  353. package/node_modules/@deepseek-ai/dsh-session-projection-cache/package.json +10 -10
  354. package/node_modules/@deepseek-ai/dsh-session-query/package.json +18 -18
  355. package/node_modules/@deepseek-ai/dsh-session-query-sqlite/package.json +9 -9
  356. package/node_modules/@deepseek-ai/dsh-session-reference/package.json +35 -35
  357. package/node_modules/@deepseek-ai/dsh-session-stats/package.json +7 -7
  358. package/node_modules/@deepseek-ai/dsh-session-telemetry/package.json +5 -5
  359. package/node_modules/@deepseek-ai/dsh-session-telemetry-otel/package.json +21 -21
  360. package/node_modules/@deepseek-ai/dsh-session-title/package.json +14 -14
  361. package/node_modules/@deepseek-ai/dsh-session-title-all-prompts-llm/package.json +12 -12
  362. package/node_modules/@deepseek-ai/dsh-session-title-first-prompt-llm/package.json +15 -15
  363. package/node_modules/@deepseek-ai/dsh-session-title-llm/package.json +10 -10
  364. package/node_modules/@deepseek-ai/dsh-session-turn-outline/package.json +7 -7
  365. package/node_modules/@deepseek-ai/dsh-settings/package.json +10 -10
  366. package/node_modules/@deepseek-ai/dsh-shell/package.json +5 -5
  367. package/node_modules/@deepseek-ai/dsh-shell-env/package.json +11 -11
  368. package/node_modules/@deepseek-ai/dsh-skill/package.json +6 -6
  369. package/node_modules/@deepseek-ai/dsh-skill-badge/package.json +3 -3
  370. package/node_modules/@deepseek-ai/dsh-skill-filesystem/package.json +7 -7
  371. package/node_modules/@deepseek-ai/dsh-skill-office/README.i18n.yaml +2 -2
  372. package/node_modules/@deepseek-ai/dsh-skill-office/README.md +1 -1
  373. package/node_modules/@deepseek-ai/dsh-skill-office/README.zh.md +1 -1
  374. package/node_modules/@deepseek-ai/dsh-skill-office/assets/office-export-pdf/SKILL.md +1 -1
  375. package/node_modules/@deepseek-ai/dsh-skill-office/assets/office-render/SKILL.md +1 -1
  376. package/node_modules/@deepseek-ai/dsh-skill-office/package.json +4 -4
  377. package/node_modules/@deepseek-ai/dsh-spill/package.json +7 -7
  378. package/node_modules/@deepseek-ai/dsh-spill-local/package.json +6 -6
  379. package/node_modules/@deepseek-ai/dsh-spill-policy/package.json +30 -30
  380. package/node_modules/@deepseek-ai/dsh-storage/package.json +1 -1
  381. package/node_modules/@deepseek-ai/dsh-storage-domain/package.json +3 -3
  382. package/node_modules/@deepseek-ai/dsh-storage-json/package.json +3 -3
  383. package/node_modules/@deepseek-ai/dsh-subagent/package.json +46 -46
  384. package/node_modules/@deepseek-ai/dsh-subagent-acp/package.json +20 -20
  385. package/node_modules/@deepseek-ai/dsh-subagent-fork-in-process/package.json +11 -11
  386. package/node_modules/@deepseek-ai/dsh-subagent-spawn-in-process/package.json +15 -15
  387. package/node_modules/@deepseek-ai/dsh-subprocess/package.json +4 -4
  388. package/node_modules/@deepseek-ai/dsh-subprocess-local/README.i18n.yaml +4 -4
  389. package/node_modules/@deepseek-ai/dsh-subprocess-local/README.md +3 -3
  390. package/node_modules/@deepseek-ai/dsh-subprocess-local/README.zh.md +3 -3
  391. package/node_modules/@deepseek-ai/dsh-subprocess-local/lib/index.js +252 -47
  392. package/node_modules/@deepseek-ai/dsh-subprocess-local/package.json +7 -7
  393. package/node_modules/@deepseek-ai/dsh-system-prompt/package.json +5 -5
  394. package/node_modules/@deepseek-ai/dsh-terminal/package.json +6 -6
  395. package/node_modules/@deepseek-ai/dsh-terminal-bash/package.json +19 -19
  396. package/node_modules/@deepseek-ai/dsh-time-context/package.json +18 -18
  397. package/node_modules/@deepseek-ai/dsh-timeout/package.json +1 -1
  398. package/node_modules/@deepseek-ai/dsh-tmux-context/package.json +11 -11
  399. package/node_modules/@deepseek-ai/dsh-token-meter/package.json +14 -14
  400. package/node_modules/@deepseek-ai/dsh-tool-ask-user/package.json +9 -9
  401. package/node_modules/@deepseek-ai/dsh-tool-bash/package.json +32 -32
  402. package/node_modules/@deepseek-ai/dsh-tool-bash-persistent/package.json +21 -21
  403. package/node_modules/@deepseek-ai/dsh-tool-call-timeout-policy/package.json +7 -7
  404. package/node_modules/@deepseek-ai/dsh-tool-cordis/lib/types/api-catalog.js +125 -4
  405. package/node_modules/@deepseek-ai/dsh-tool-cordis/package.json +10 -10
  406. package/node_modules/@deepseek-ai/dsh-tool-fs/README.md +1 -1
  407. package/node_modules/@deepseek-ai/dsh-tool-fs/README.zh.md +1 -1
  408. package/node_modules/@deepseek-ai/dsh-tool-fs/lib/index.js +1 -1
  409. package/node_modules/@deepseek-ai/dsh-tool-fs/package.json +31 -31
  410. package/node_modules/@deepseek-ai/dsh-tool-fs-search/package.json +27 -27
  411. package/node_modules/@deepseek-ai/dsh-tool-goal/package.json +16 -16
  412. package/node_modules/@deepseek-ai/dsh-tool-jobs/package.json +15 -15
  413. package/node_modules/@deepseek-ai/dsh-tool-present/package.json +19 -19
  414. package/node_modules/@deepseek-ai/dsh-tool-pwsh/package.json +31 -31
  415. package/node_modules/@deepseek-ai/dsh-tool-pwsh-persistent/package.json +22 -22
  416. package/node_modules/@deepseek-ai/dsh-tool-ralph/package.json +19 -19
  417. package/node_modules/@deepseek-ai/dsh-tool-schedule/package.json +12 -12
  418. package/node_modules/@deepseek-ai/dsh-tool-session-query/package.json +24 -24
  419. package/node_modules/@deepseek-ai/dsh-tool-skill/package.json +14 -14
  420. package/node_modules/@deepseek-ai/dsh-tool-str-replace-editor/package.json +19 -19
  421. package/node_modules/@deepseek-ai/dsh-tool-subagent/README.md +1 -1
  422. package/node_modules/@deepseek-ai/dsh-tool-subagent/README.zh.md +1 -1
  423. package/node_modules/@deepseek-ai/dsh-tool-subagent/lib/index.js +1 -1
  424. package/node_modules/@deepseek-ai/dsh-tool-subagent/package.json +22 -22
  425. package/node_modules/@deepseek-ai/dsh-tool-subagent-control/package.json +19 -19
  426. package/node_modules/@deepseek-ai/dsh-tool-terminal/package.json +25 -25
  427. package/node_modules/@deepseek-ai/dsh-tool-todo/package.json +13 -13
  428. package/node_modules/@deepseek-ai/dsh-tool-web/package.json +19 -19
  429. package/node_modules/@deepseek-ai/dsh-tool-workflow/package.json +20 -20
  430. package/node_modules/@deepseek-ai/dsh-tool-working-directory/package.json +11 -11
  431. package/node_modules/@deepseek-ai/dsh-tool-workspace-dependencies/package.json +6 -6
  432. package/node_modules/@deepseek-ai/dsh-tools/README.i18n.yaml +2 -2
  433. package/node_modules/@deepseek-ai/dsh-tools/README.md +3 -3
  434. package/node_modules/@deepseek-ai/dsh-tools/README.zh.md +3 -3
  435. package/node_modules/@deepseek-ai/dsh-tools/lib/index.js +45 -26
  436. package/node_modules/@deepseek-ai/dsh-tools/lib/types/index.js +1 -1
  437. package/node_modules/@deepseek-ai/dsh-tools/lib/types/ptc.js +20 -10
  438. package/node_modules/@deepseek-ai/dsh-tools/lib/types/py-types.js +1 -1
  439. package/node_modules/@deepseek-ai/dsh-tools/lib/types/ts-types.js +26 -9
  440. package/node_modules/@deepseek-ai/dsh-tools/package.json +24 -24
  441. package/node_modules/@deepseek-ai/dsh-tui-app/README.i18n.yaml +6 -6
  442. package/node_modules/@deepseek-ai/dsh-tui-app/README.md +10 -10
  443. package/node_modules/@deepseek-ai/dsh-tui-app/README.zh.md +10 -10
  444. package/node_modules/@deepseek-ai/dsh-tui-app/lib/index.js +347 -136
  445. package/node_modules/@deepseek-ai/dsh-tui-app/package.json +78 -78
  446. package/node_modules/@deepseek-ai/dsh-typert-loader/package.json +5 -5
  447. package/node_modules/@deepseek-ai/dsh-typert-protocol/package.json +2 -2
  448. package/node_modules/@deepseek-ai/dsh-typert-registry/package.json +2 -2
  449. package/node_modules/@deepseek-ai/dsh-user-approval/package.json +13 -13
  450. package/node_modules/@deepseek-ai/dsh-user-questions/package.json +15 -15
  451. package/node_modules/@deepseek-ai/dsh-util-code-language/package.json +1 -1
  452. package/node_modules/@deepseek-ai/dsh-util-crypto/package.json +1 -1
  453. package/node_modules/@deepseek-ai/dsh-util-time/package.json +1 -1
  454. package/node_modules/@deepseek-ai/dsh-util-values/package.json +1 -1
  455. package/node_modules/@deepseek-ai/dsh-util-workspace-path/package.json +1 -1
  456. package/node_modules/@deepseek-ai/dsh-web/package.json +3 -3
  457. package/node_modules/@deepseek-ai/dsh-web-app/package.json +140 -139
  458. package/node_modules/@deepseek-ai/dsh-web-app/presets/cordis.patch.yml +3 -0
  459. package/node_modules/@deepseek-ai/dsh-web-app/presets/ptc.patch.yml +3 -0
  460. package/node_modules/@deepseek-ai/dsh-web-app/presets/standard.patch.yml +3 -0
  461. package/node_modules/@deepseek-ai/dsh-web-fetch-http/package.json +7 -7
  462. package/node_modules/@deepseek-ai/dsh-web-frontend/package.json +18 -20
  463. package/node_modules/@deepseek-ai/dsh-web-search-deepseek/package.json +15 -15
  464. package/node_modules/@deepseek-ai/dsh-web-search-exa/package.json +6 -6
  465. package/node_modules/@deepseek-ai/dsh-web-search-perplexity/package.json +6 -6
  466. package/node_modules/@deepseek-ai/dsh-win32-process/package.json +2 -2
  467. package/node_modules/@deepseek-ai/dsh-workflow/package.json +9 -9
  468. package/node_modules/@deepseek-ai/dsh-workflow-ptc/package.json +36 -36
  469. package/node_modules/@deepseek-ai/dsh-working-directory/package.json +18 -18
  470. package/node_modules/@deepseek-ai/dsh-workspace/package.json +12 -12
  471. package/node_modules/@deepseek-ai/dsh-workspace-changes/package.json +18 -18
  472. package/node_modules/@earendil-works/pi-ai/README.md +5 -34
  473. package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js +59 -54
  474. package/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js +16 -18
  475. package/node_modules/@earendil-works/pi-ai/dist/api/google-shared.js +2 -0
  476. package/node_modules/@earendil-works/pi-ai/dist/api/mistral-conversations.js +2 -2
  477. package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js +29 -27
  478. package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js +15 -2
  479. package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses.js +21 -4
  480. package/node_modules/@earendil-works/pi-ai/dist/api/transform-messages.js +28 -15
  481. package/node_modules/@earendil-works/pi-ai/dist/index.js +0 -1
  482. package/node_modules/@earendil-works/pi-ai/dist/providers/compat-schema.js +8 -0
  483. package/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
  484. package/node_modules/@earendil-works/pi-ai/dist/providers/data/anthropic.json +1 -1
  485. package/node_modules/@earendil-works/pi-ai/dist/providers/data/github-copilot.json +1 -1
  486. package/node_modules/@earendil-works/pi-ai/dist/providers/data/google-vertex.json +1 -1
  487. package/node_modules/@earendil-works/pi-ai/dist/providers/data/google.json +1 -1
  488. package/node_modules/@earendil-works/pi-ai/dist/providers/data/kimi-coding.json +1 -1
  489. package/node_modules/@earendil-works/pi-ai/dist/providers/data/meta.json +1 -1
  490. package/node_modules/@earendil-works/pi-ai/dist/providers/data/nvidia.json +1 -1
  491. package/node_modules/@earendil-works/pi-ai/dist/providers/data/openai-codex.json +1 -1
  492. package/node_modules/@earendil-works/pi-ai/dist/providers/data/openai.json +1 -1
  493. package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode-go.json +1 -1
  494. package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode.json +1 -1
  495. package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
  496. package/node_modules/@earendil-works/pi-ai/dist/providers/data/radius.json +1 -1
  497. package/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
  498. package/node_modules/@earendil-works/pi-ai/dist/providers/data/xiaomi-token-plan-ams.json +1 -1
  499. package/node_modules/@earendil-works/pi-ai/dist/providers/data/xiaomi-token-plan-cn.json +1 -1
  500. package/node_modules/@earendil-works/pi-ai/dist/providers/data/xiaomi-token-plan-sgp.json +1 -1
  501. package/node_modules/@earendil-works/pi-ai/dist/providers/data/xiaomi.json +1 -1
  502. package/node_modules/@earendil-works/pi-ai/dist/providers/kimi-coding.js +3 -3
  503. package/node_modules/@earendil-works/pi-ai/dist/providers/opencode-go.js +2 -0
  504. package/node_modules/@earendil-works/pi-ai/dist/utils/json-parse.js +26 -1
  505. package/node_modules/@earendil-works/pi-ai/dist/utils/retry.js +5 -1
  506. package/node_modules/@earendil-works/pi-ai/dist/utils/validation.js +3 -0
  507. package/node_modules/@earendil-works/pi-ai/package.json +2 -2
  508. package/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
  509. package/node_modules/minisearch/LICENSE.txt +7 -0
  510. package/node_modules/minisearch/README.md +347 -0
  511. package/node_modules/minisearch/dist/cjs/SearchableMap.cjs +549 -0
  512. package/node_modules/minisearch/dist/cjs/index.cjs +2007 -0
  513. package/node_modules/minisearch/dist/es/SearchableMap.js +547 -0
  514. package/node_modules/minisearch/dist/es/index.js +2005 -0
  515. package/node_modules/minisearch/dist/umd/SearchableMap.js +555 -0
  516. package/node_modules/minisearch/dist/umd/index.js +2013 -0
  517. package/node_modules/minisearch/package.json +90 -0
  518. package/node_modules/minisearch/src/MiniSearch.test.js +1997 -0
  519. package/node_modules/minisearch/src/MiniSearch.ts +2261 -0
  520. package/node_modules/minisearch/src/SearchableMap/SearchableMap.test.js +314 -0
  521. package/node_modules/minisearch/src/SearchableMap/SearchableMap.ts +424 -0
  522. package/node_modules/minisearch/src/SearchableMap/TreeIterator.ts +103 -0
  523. package/node_modules/minisearch/src/SearchableMap/fuzzySearch.ts +130 -0
  524. package/node_modules/minisearch/src/SearchableMap/types.ts +18 -0
  525. package/node_modules/minisearch/src/index.ts +4 -0
  526. package/node_modules/minisearch/src/testSetup/jest.js +1 -0
  527. package/package.json +305 -301
  528. package/node_modules/@deepseek-ai/dsh-client-store/LICENSE +0 -21
  529. package/node_modules/@deepseek-ai/dsh-client-store/README.i18n.yaml +0 -28
  530. package/node_modules/@deepseek-ai/dsh-client-store/README.md +0 -46
  531. package/node_modules/@deepseek-ai/dsh-client-store/README.zh.md +0 -46
  532. package/node_modules/@deepseek-ai/dsh-client-store/lib/index.js +0 -180
  533. package/node_modules/@deepseek-ai/dsh-client-store/lib/types/contract.d.ts +0 -121
  534. package/node_modules/@deepseek-ai/dsh-client-store/lib/types/index.d.ts +0 -92
  535. package/node_modules/@deepseek-ai/dsh-client-store/package.json +0 -37
  536. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/LICENSE +0 -21
  537. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/README.i18n.yaml +0 -61
  538. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/README.md +0 -221
  539. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/README.zh.md +0 -221
  540. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Button.module.css +0 -77
  541. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Checkbox.module.css +0 -28
  542. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/CodeCard.module.css +0 -83
  543. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/CommandText.module.css +0 -12
  544. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/ConnectionIndicator.module.css +0 -118
  545. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/DiffBlock.module.css +0 -80
  546. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/DisclosureRow.module.css +0 -111
  547. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/FileTypeIcon.module.css +0 -50
  548. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/HoverCard.module.css +0 -90
  549. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/ImageLightbox.module.css +0 -54
  550. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/ImagePreview.module.css +0 -24
  551. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/InlineEditor.module.css +0 -23
  552. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Input.module.css +0 -38
  553. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/JsonTree.module.css +0 -345
  554. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Menu.module.css +0 -285
  555. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/MenuGroup.module.css +0 -37
  556. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/MenuSurface.module.css +0 -49
  557. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Modal.module.css +0 -129
  558. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/OnboardingSurface.module.css +0 -28
  559. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/PathLabel.module.css +0 -28
  560. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Pill.module.css +0 -28
  561. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/ReadBlock.module.css +0 -43
  562. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/RiskConfirmation.module.css +0 -73
  563. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/SearchBlock.module.css +0 -112
  564. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/SegmentedControl.module.css +0 -76
  565. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/SegmentedTabs.module.css +0 -55
  566. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/ShortcutKeys.module.css +0 -33
  567. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/StateDot.module.css +0 -100
  568. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Switch.module.css +0 -49
  569. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Tag.module.css +0 -57
  570. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/TerminalBlock.module.css +0 -205
  571. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/TextShimmer.module.css +0 -88
  572. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Toast.module.css +0 -101
  573. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/Tooltip.module.css +0 -63
  574. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/WebBlock.module.css +0 -141
  575. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/index.js +0 -12583
  576. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/markdown/CodeBlock.module.css +0 -145
  577. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/markdown/JsonBlock.module.css +0 -32
  578. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/markdown/MarkdownText.module.css +0 -467
  579. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/settings-form/SettingsForm.module.css +0 -53
  580. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/settings-form/fields.module.css +0 -145
  581. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/BrandWordmark.d.ts +0 -15
  582. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Button.d.ts +0 -19
  583. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Checkbox.d.ts +0 -20
  584. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/CodeFileIcon.d.ts +0 -12
  585. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/CodeToolbar.d.ts +0 -30
  586. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/CommandText.d.ts +0 -16
  587. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/ConfigField.d.ts +0 -33
  588. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/ConnectionIndicator.d.ts +0 -26
  589. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/DiffBlock.d.ts +0 -52
  590. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/DisclosureRow.d.ts +0 -38
  591. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/FileTypeIcon.d.ts +0 -43
  592. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/FishLogo.d.ts +0 -16
  593. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/FoldToggle.d.ts +0 -20
  594. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/HoverCard.d.ts +0 -40
  595. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/HoverCardGroup.d.ts +0 -23
  596. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/ImageLightbox.d.ts +0 -27
  597. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/ImagePreview.d.ts +0 -12
  598. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/InlineEditor.d.ts +0 -23
  599. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Input.d.ts +0 -12
  600. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/JsonTree.d.ts +0 -59
  601. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/LinkIcon.d.ts +0 -34
  602. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Menu.d.ts +0 -143
  603. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/MenuGroup.d.ts +0 -22
  604. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/MenuSurface.d.ts +0 -16
  605. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Modal.d.ts +0 -44
  606. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/OnboardingSurface.d.ts +0 -11
  607. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/PathLabel.d.ts +0 -13
  608. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/PermissionIcon.d.ts +0 -51
  609. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Pill.d.ts +0 -13
  610. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/ReadBlock.d.ts +0 -48
  611. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/ReferenceIcon.d.ts +0 -21
  612. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/RiskConfirmation.d.ts +0 -20
  613. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/SearchBlock.d.ts +0 -72
  614. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/SegmentedControl.d.ts +0 -35
  615. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/SegmentedTabs.d.ts +0 -26
  616. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/ShortcutKeys.d.ts +0 -11
  617. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/SiteGlyph.d.ts +0 -25
  618. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/StateDot.d.ts +0 -20
  619. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Switch.d.ts +0 -20
  620. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Tag.d.ts +0 -32
  621. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/TerminalBlock.d.ts +0 -86
  622. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/TextShimmer.d.ts +0 -22
  623. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Toast.d.ts +0 -49
  624. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/Tooltip.d.ts +0 -63
  625. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/WebBlock.d.ts +0 -61
  626. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/ansi.d.ts +0 -17
  627. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/clipboard.d.ts +0 -9
  628. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/code-file-icon-artwork.d.ts +0 -54
  629. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/code-file-types.d.ts +0 -30
  630. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/code-highlighting.d.ts +0 -12
  631. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/darwin-desktop.d.ts +0 -9
  632. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/file-size.d.ts +0 -8
  633. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/focus.d.ts +0 -9
  634. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/guide-artwork.d.ts +0 -15
  635. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/head-tail-cap.d.ts +0 -23
  636. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/icons/index.d.ts +0 -385
  637. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/icons/props.d.ts +0 -9
  638. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/icons/shared-artwork.d.ts +0 -43
  639. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/index.d.ts +0 -102
  640. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/input-modality.d.ts +0 -19
  641. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/keyboard-composition.d.ts +0 -11
  642. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/CodeBlock.d.ts +0 -35
  643. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/JsonBlock.d.ts +0 -8
  644. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/MarkdownDelegate.d.ts +0 -46
  645. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/MarkdownText.d.ts +0 -49
  646. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/cjkFriendlyStrong.d.ts +0 -9
  647. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/file-link.d.ts +0 -13
  648. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/highlight.d.ts +0 -148
  649. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/incremental.d.ts +0 -80
  650. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/katex.d.ts +0 -27
  651. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/local-image-syntax.d.ts +0 -10
  652. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/mathCompatibility.d.ts +0 -11
  653. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/parse.d.ts +0 -25
  654. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/plain-text.d.ts +0 -22
  655. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/render.d.ts +0 -141
  656. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/markdown/useViewportHighlighting.d.ts +0 -11
  657. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/overlay-top-margin.d.ts +0 -8
  658. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/plugin-artwork.d.ts +0 -17
  659. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/pointer-grace.d.ts +0 -22
  660. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/rank-by-name.d.ts +0 -22
  661. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/relative-time.d.ts +0 -23
  662. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/settings-form/SettingsForm.d.ts +0 -46
  663. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/settings-form/fields.d.ts +0 -70
  664. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/settings-form/form-model.d.ts +0 -214
  665. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/use-copy-feedback.d.ts +0 -14
  666. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/useAnchoredMaxHeight.d.ts +0 -14
  667. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/useAnchoredPosition.d.ts +0 -36
  668. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/useDismissOnOutsidePointer.d.ts +0 -11
  669. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/useModalLayer.d.ts +0 -27
  670. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/types/user-text.d.ts +0 -29
  671. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/lib/user-text.module.css +0 -46
  672. package/node_modules/@deepseek-ai/dsh-client-ui-primitives/package.json +0 -61
  673. package/node_modules/@deepseek-ai/dsh-client-ui-slots/LICENSE +0 -21
  674. package/node_modules/@deepseek-ai/dsh-client-ui-slots/README.i18n.yaml +0 -55
  675. package/node_modules/@deepseek-ai/dsh-client-ui-slots/README.md +0 -111
  676. package/node_modules/@deepseek-ai/dsh-client-ui-slots/README.zh.md +0 -111
  677. package/node_modules/@deepseek-ai/dsh-client-ui-slots/lib/index.js +0 -577
  678. package/node_modules/@deepseek-ai/dsh-client-ui-slots/lib/types/index.d.ts +0 -955
  679. package/node_modules/@deepseek-ai/dsh-client-ui-slots/lib/types/renderer.d.ts +0 -271
  680. package/node_modules/@deepseek-ai/dsh-client-ui-slots/lib/types/store.d.ts +0 -3
  681. package/node_modules/@earendil-works/pi-ai/dist/utils/assistant-message-frame.js +0 -425
@@ -0,0 +1,2261 @@
1
+ import SearchableMap from './SearchableMap/SearchableMap'
2
+
3
+ export type LowercaseCombinationOperator = 'or' | 'and' | 'and_not'
4
+ export type CombinationOperator = LowercaseCombinationOperator | Uppercase<LowercaseCombinationOperator> | Capitalize<LowercaseCombinationOperator>
5
+
6
+ const OR: LowercaseCombinationOperator = 'or'
7
+ const AND: LowercaseCombinationOperator = 'and'
8
+ const AND_NOT: LowercaseCombinationOperator = 'and_not'
9
+
10
+ /**
11
+ * Search options to customize the search behavior.
12
+ */
13
+ export type SearchOptions = {
14
+ /**
15
+ * Names of the fields to search in. If omitted, all fields are searched.
16
+ */
17
+ fields?: string[],
18
+
19
+ /**
20
+ * Function used to filter search results, for example on the basis of stored
21
+ * fields. It takes as argument each search result and should return a boolean
22
+ * to indicate if the result should be kept or not.
23
+ */
24
+ filter?: (result: SearchResult) => boolean,
25
+
26
+ /**
27
+ * Key-value object of field names to boosting values. By default, fields are
28
+ * assigned a boosting factor of 1. If one assigns to a field a boosting value
29
+ * of 2, a result that matches the query in that field is assigned a score
30
+ * twice as high as a result matching the query in another field, all else
31
+ * being equal.
32
+ */
33
+ boost?: { [fieldName: string]: number },
34
+
35
+ /**
36
+ * Function to calculate a boost factor for each term.
37
+ *
38
+ * This function, if provided, is called for each query term (as split by
39
+ * `tokenize` and processed by `processTerm`). The arguments passed to the
40
+ * function are the query term, the positional index of the term in the query,
41
+ * and the array of all query terms. It is expected to return a numeric boost
42
+ * factor for the term. A factor lower than 1 reduces the importance of the
43
+ * term, a factor greater than 1 increases it. A factor of exactly 1 is
44
+ * neutral, and does not affect the term's importance.
45
+ */
46
+ boostTerm?: (term: string, i: number, terms: string[]) => number,
47
+
48
+ /**
49
+ * Relative weights to assign to prefix search results and fuzzy search
50
+ * results. Exact matches are assigned a weight of 1.
51
+ */
52
+ weights?: { fuzzy: number, prefix: number },
53
+
54
+ /**
55
+ * Function to calculate a boost factor for documents. It takes as arguments
56
+ * the document ID, and a term that matches the search in that document, and
57
+ * the value of the stored fields for the document (if any). It should return
58
+ * a boosting factor: a number higher than 1 increases the computed score, a
59
+ * number lower than 1 decreases the score, and a falsy value skips the search
60
+ * result completely.
61
+ */
62
+ boostDocument?: (documentId: any, term: string, storedFields?: Record<string, unknown>) => number,
63
+
64
+ /**
65
+ * Controls whether to perform prefix search. It can be a simple boolean, or a
66
+ * function.
67
+ *
68
+ * If a boolean is passed, prefix search is performed if true.
69
+ *
70
+ * If a function is passed, it is called upon search with a search term, the
71
+ * positional index of that search term in the tokenized search query, and the
72
+ * tokenized search query. The function should return a boolean to indicate
73
+ * whether to perform prefix search for that search term.
74
+ */
75
+ prefix?: boolean | ((term: string, index: number, terms: string[]) => boolean),
76
+
77
+ /**
78
+ * Controls whether to perform fuzzy search. It can be a simple boolean, or a
79
+ * number, or a function.
80
+ *
81
+ * If a boolean is given, fuzzy search with a default fuzziness parameter is
82
+ * performed if true.
83
+ *
84
+ * If a number higher or equal to 1 is given, fuzzy search is performed, with
85
+ * a maximum edit distance (Levenshtein) equal to the number.
86
+ *
87
+ * If a number between 0 and 1 is given, fuzzy search is performed within a
88
+ * maximum edit distance corresponding to that fraction of the term length,
89
+ * approximated to the nearest integer. For example, 0.2 would mean an edit
90
+ * distance of 20% of the term length, so 1 character in a 5-characters term.
91
+ * The calculated fuzziness value is limited by the `maxFuzzy` option, to
92
+ * prevent slowdown for very long queries.
93
+ *
94
+ * If a function is passed, the function is called upon search with a search
95
+ * term, a positional index of that term in the tokenized search query, and
96
+ * the tokenized search query. It should return a boolean or a number, with
97
+ * the meaning documented above.
98
+ */
99
+ fuzzy?: boolean | number | ((term: string, index: number, terms: string[]) => boolean | number),
100
+
101
+ /**
102
+ * Controls the maximum fuzziness when using a fractional fuzzy value. This is
103
+ * set to 6 by default. Very high edit distances usually don't produce
104
+ * meaningful results, but can excessively impact search performance.
105
+ */
106
+ maxFuzzy?: number,
107
+
108
+ /**
109
+ * The operand to combine partial results for each term. By default it is
110
+ * "OR", so results matching _any_ of the search terms are returned by a
111
+ * search. If "AND" is given, only results matching _all_ the search terms are
112
+ * returned by a search.
113
+ */
114
+ combineWith?: CombinationOperator,
115
+
116
+ /**
117
+ * Function to tokenize the search query. By default, the same tokenizer used
118
+ * for indexing is used also for search.
119
+ *
120
+ * @remarks This function is called after `extractField` extracts a truthy
121
+ * value from a field. This function is then expected to split the extracted
122
+ * `text` document into tokens (more commonly referred to as "terms" in this
123
+ * context). The resulting split might be simple, like for example on word
124
+ * boundaries, or it might be more complex, taking into account certain
125
+ * encoding, or parsing needs, or even just special cases. Think about how one
126
+ * might need to go about indexing the term "short-term". You would likely
127
+ * want to treat this case specially, and return two terms instead, `[
128
+ * "short", "term" ]`.
129
+ *
130
+ * Or, you could let such a case be handled by the `processTerm` function,
131
+ * which is designed to turn each token/term into whole terms or sub-terms. In
132
+ * any case, the purpose of this function is to split apart the provided
133
+ * `text` document into parts that can be processed by the `processTerm`
134
+ * function.
135
+ */
136
+ tokenize?: (text: string) => string[],
137
+
138
+ /**
139
+ * Function to process or normalize terms in the search query. By default, the
140
+ * same term processor used for indexing is used also for search.
141
+ *
142
+ * @remarks
143
+ * During the document indexing phase, the first step is to call the
144
+ * `extractField` function to fetch the requested value/field from the
145
+ * document. This is then passed off to the `tokenize` function, which will
146
+ * break apart each value into "terms". These terms are then individually
147
+ * passed through this function to compute each term individually. A term
148
+ * might for example be something like "lbs", in which case one would likely
149
+ * want to return `[ "lbs", "lb", "pound", "pounds" ]`. You may also return
150
+ * just a single string, or a falsy value if you would like to skip indexing
151
+ * entirely for a specific term.
152
+ *
153
+ * Truthy return value(s) are then fed to the indexer as positive matches for
154
+ * this document. In our example above, all four of the `[ "lbs", "lb",
155
+ * "pound", "pounds" ]` terms would be added to the indexing engine, matching
156
+ * against the current document being computed.
157
+ *
158
+ * *Note: Whatever values are returned from this function will receive no
159
+ * further processing before being indexed. This means for example, if you
160
+ * include whitespace at the beginning or end of a word, it will also be
161
+ * indexed that way, with the included whitespace.*
162
+ */
163
+ processTerm?: (term: string) => string | string[] | null | undefined | false
164
+
165
+ /**
166
+ * BM25+ algorithm parameters. Customizing these is almost never necessary,
167
+ * and finetuning them requires an understanding of the BM25 scoring model. In
168
+ * most cases, it is best to omit this option to use defaults, and instead use
169
+ * boosting to tweak scoring for specific use cases.
170
+ */
171
+ bm25?: BM25Params
172
+ }
173
+
174
+ type SearchOptionsWithDefaults = SearchOptions & {
175
+ boost: { [fieldName: string]: number },
176
+
177
+ weights: { fuzzy: number, prefix: number },
178
+
179
+ prefix: boolean | ((term: string, index: number, terms: string[]) => boolean),
180
+
181
+ fuzzy: boolean | number | ((term: string, index: number, terms: string[]) => boolean | number),
182
+
183
+ maxFuzzy: number,
184
+
185
+ combineWith: CombinationOperator
186
+
187
+ bm25: BM25Params
188
+ }
189
+
190
+ /**
191
+ * Configuration options passed to the {@link MiniSearch} constructor
192
+ *
193
+ * @typeParam T The type of documents being indexed.
194
+ */
195
+ export type Options<T = any> = {
196
+ /**
197
+ * Names of the document fields to be indexed.
198
+ */
199
+ fields: string[],
200
+
201
+ /**
202
+ * Name of the ID field, uniquely identifying a document.
203
+ */
204
+ idField?: string,
205
+
206
+ /**
207
+ * Names of fields to store, so that search results would include them. By
208
+ * default none, so results would only contain the id field.
209
+ */
210
+ storeFields?: string[],
211
+
212
+ /**
213
+ * Function used to extract the value of each field in documents. By default,
214
+ * the documents are assumed to be plain objects with field names as keys,
215
+ * but by specifying a custom `extractField` function one can completely
216
+ * customize how the fields are extracted.
217
+ *
218
+ * The function takes as arguments the document, and the name of the field to
219
+ * extract from it. It should return the field value as a string.
220
+ *
221
+ * @remarks
222
+ * The returned string is fed into the `tokenize` function to split it up
223
+ * into tokens.
224
+ */
225
+ extractField?: (document: T, fieldName: string) => any,
226
+
227
+ /**
228
+ * Function used to turn field values into strings for indexing
229
+ *
230
+ * The function takes as arguments the field value, and the name of the field
231
+ * to stringify, so that its logic can be customized on specific fields. By
232
+ * default, it simply calls `toString()` on the field value (which in many
233
+ * cases is already a string).
234
+ *
235
+ * ### Example:
236
+ *
237
+ * ```javascript
238
+ * // Custom stringifier that formats dates as "Tuesday, September 16, 2025"
239
+ * const miniSearch = new MiniSearch({
240
+ * fields: ['title', 'date'],
241
+ * stringifyField: ((fieldValue, _fieldName) => {
242
+ * if (fieldValue instanceof Date) {
243
+ * return fieldValue.toLocaleDateString('en-US', {
244
+ * weekday: 'long',
245
+ * year: 'numeric',
246
+ * month: 'long',
247
+ * day: 'numeric'
248
+ * })
249
+ * } else {
250
+ * return fieldValue.toString()
251
+ * }
252
+ * }
253
+ * })
254
+ * ```
255
+ */
256
+ stringifyField?: (fieldValue: any, fieldName: string) => string,
257
+
258
+ /**
259
+ * Function used to split a field value into individual terms to be indexed.
260
+ * The default tokenizer separates terms by space or punctuation, but a
261
+ * custom tokenizer can be provided for custom logic.
262
+ *
263
+ * The function takes as arguments string to tokenize, and the name of the
264
+ * field it comes from. It should return the terms as an array of strings.
265
+ * When used for tokenizing a search query instead of a document field, the
266
+ * `fieldName` is undefined.
267
+ *
268
+ * @remarks
269
+ * This function is called after `extractField` extracts a truthy value from a
270
+ * field. This function is then expected to split the extracted `text` document
271
+ * into tokens (more commonly referred to as "terms" in this context). The resulting
272
+ * split might be simple, like for example on word boundaries, or it might be more
273
+ * complex, taking into account certain encoding, or parsing needs, or even just
274
+ * special cases. Think about how one might need to go about indexing the term
275
+ * "short-term". You would likely want to treat this case specially, and return two
276
+ * terms instead, `[ "short", "term" ]`.
277
+ *
278
+ * Or, you could let such a case be handled by the `processTerm` function,
279
+ * which is designed to turn each token/term into whole terms or sub-terms. In any
280
+ * case, the purpose of this function is to split apart the provided `text` document
281
+ * into parts that can be processed by the `processTerm` function.
282
+ */
283
+ tokenize?: (text: string, fieldName?: string) => string[],
284
+
285
+ /**
286
+ * Function used to process a term before indexing or search. This can be
287
+ * used for normalization (such as stemming). By default, terms are
288
+ * downcased, and otherwise no other normalization is performed.
289
+ *
290
+ * The function takes as arguments a term to process, and the name of the
291
+ * field it comes from. It should return the processed term as a string, or a
292
+ * falsy value to reject the term entirely.
293
+ *
294
+ * It can also return an array of strings, in which case each string in the
295
+ * returned array is indexed as a separate term.
296
+ *
297
+ * @remarks
298
+ * During the document indexing phase, the first step is to call the `extractField`
299
+ * function to fetch the requested value/field from the document. This is then
300
+ * passed off to the `tokenizer`, which will break apart each value into "terms".
301
+ * These terms are then individually passed through the `processTerm` function
302
+ * to compute each term individually. A term might for example be something
303
+ * like "lbs", in which case one would likely want to return
304
+ * `[ "lbs", "lb", "pound", "pounds" ]`. You may also return a single string value,
305
+ * or a falsy value if you would like to skip indexing entirely for a specific term.
306
+ *
307
+ * Truthy return value(s) are then fed to the indexer as positive matches for this
308
+ * document. In our example above, all four of the `[ "lbs", "lb", "pound", "pounds" ]`
309
+ * terms would be added to the indexing engine, matching against the current document
310
+ * being computed.
311
+ *
312
+ * *Note: Whatever values are returned from this function will receive no further
313
+ * processing before being indexed. This means for example, if you include whitespace
314
+ * at the beginning or end of a word, it will also be indexed that way, with the
315
+ * included whitespace.*
316
+ */
317
+ processTerm?: (term: string, fieldName?: string) => string | string[] | null | undefined | false,
318
+
319
+ /**
320
+ * Function called to log messages. Arguments are a log level ('debug',
321
+ * 'info', 'warn', or 'error'), a log message, and an optional string code
322
+ * that identifies the reason for the log.
323
+ *
324
+ * The default implementation uses `console`, if defined.
325
+ */
326
+ logger?: (level: LogLevel, message: string, code?: string) => void
327
+
328
+ /**
329
+ * If `true` (the default), vacuuming is performed automatically as soon as
330
+ * {@link MiniSearch#discard} is called a certain number of times, cleaning up
331
+ * obsolete references from the index. If `false`, no automatic vacuuming is
332
+ * performed. Custom settings controlling auto vacuuming thresholds, as well
333
+ * as batching behavior, can be passed as an object (see the {@link
334
+ * AutoVacuumOptions} type).
335
+ */
336
+ autoVacuum?: boolean | AutoVacuumOptions
337
+
338
+ /**
339
+ * Default search options (see the {@link SearchOptions} type and the {@link
340
+ * MiniSearch#search} method for details)
341
+ */
342
+ searchOptions?: SearchOptions,
343
+
344
+ /**
345
+ * Default auto suggest options (see the {@link SearchOptions} type and the
346
+ * {@link MiniSearch#autoSuggest} method for details)
347
+ */
348
+ autoSuggestOptions?: SearchOptions
349
+ }
350
+
351
+ type OptionsWithDefaults<T = any> = Options<T> & {
352
+ storeFields: string[]
353
+
354
+ idField: string
355
+
356
+ extractField: (document: T, fieldName: string) => any
357
+
358
+ stringifyField: (fieldValue: any, fieldName: string) => string
359
+
360
+ tokenize: (text: string, fieldName: string) => string[]
361
+
362
+ processTerm: (term: string, fieldName: string) => string | string[] | null | undefined | false
363
+
364
+ logger: (level: LogLevel, message: string, code?: string) => void
365
+
366
+ autoVacuum: false | AutoVacuumOptions
367
+
368
+ searchOptions: SearchOptionsWithDefaults
369
+
370
+ autoSuggestOptions: SearchOptions
371
+ }
372
+
373
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error'
374
+
375
+ /**
376
+ * The type of auto-suggestions
377
+ */
378
+ export type Suggestion = {
379
+ /**
380
+ * The suggestion
381
+ */
382
+ suggestion: string,
383
+
384
+ /**
385
+ * Suggestion as an array of terms
386
+ */
387
+ terms: string[],
388
+
389
+ /**
390
+ * Score for the suggestion
391
+ */
392
+ score: number
393
+ }
394
+
395
+ /**
396
+ * Match information for a search result. It is a key-value object where keys
397
+ * are terms that matched, and values are the list of fields that the term was
398
+ * found in.
399
+ */
400
+ export type MatchInfo = {
401
+ [term: string]: string[]
402
+ }
403
+
404
+ /**
405
+ * Type of the search results. Each search result indicates the document ID, the
406
+ * terms that matched, the match information, the score, and all the stored
407
+ * fields.
408
+ */
409
+ export type SearchResult = {
410
+ /**
411
+ * The document ID
412
+ */
413
+ id: any,
414
+
415
+ /**
416
+ * List of document terms that matched. For example, if a prefix search for
417
+ * `"moto"` matches `"motorcycle"`, `terms` will contain `"motorcycle"`.
418
+ */
419
+ terms: string[],
420
+
421
+ /**
422
+ * List of query terms that matched. For example, if a prefix search for
423
+ * `"moto"` matches `"motorcycle"`, `queryTerms` will contain `"moto"`.
424
+ */
425
+ queryTerms: string[],
426
+
427
+ /**
428
+ * Score of the search results
429
+ */
430
+ score: number,
431
+
432
+ /**
433
+ * Match information, see {@link MatchInfo}
434
+ */
435
+ match: MatchInfo,
436
+
437
+ /**
438
+ * Stored fields
439
+ */
440
+ [key: string]: any
441
+ }
442
+
443
+ /**
444
+ * @ignore
445
+ */
446
+ export type AsPlainObject = {
447
+ documentCount: number,
448
+ nextId: number,
449
+ documentIds: { [shortId: string]: any }
450
+ fieldIds: { [fieldName: string]: number }
451
+ fieldLength: { [shortId: string]: number[] }
452
+ averageFieldLength: number[],
453
+ storedFields: { [shortId: string]: any }
454
+ dirtCount?: number,
455
+ index: [string, { [fieldId: string]: SerializedIndexEntry }][]
456
+ serializationVersion: number
457
+ }
458
+
459
+ export type QueryCombination = SearchOptions & { queries: Query[] }
460
+
461
+ /**
462
+ * Wildcard query, used to match all terms
463
+ */
464
+ export type Wildcard = typeof MiniSearch.wildcard
465
+
466
+ /**
467
+ * Search query expression, either a query string or an expression tree
468
+ * combining several queries with a combination of AND or OR.
469
+ */
470
+ export type Query = QueryCombination | string | Wildcard
471
+
472
+ /**
473
+ * Options to control vacuuming behavior.
474
+ *
475
+ * Vacuuming cleans up document references made obsolete by {@link
476
+ * MiniSearch.discard} from the index. On large indexes, vacuuming is
477
+ * potentially costly, because it has to traverse the whole inverted index.
478
+ * Therefore, in order to dilute this cost so it does not negatively affects the
479
+ * application, vacuuming is performed in batches, with a delay between each
480
+ * batch. These options are used to configure the batch size and the delay
481
+ * between batches.
482
+ */
483
+ export type VacuumOptions = {
484
+ /**
485
+ * Size of each vacuuming batch (the number of terms in the index that will be
486
+ * traversed in each batch). Defaults to 1000.
487
+ */
488
+ batchSize?: number,
489
+
490
+ /**
491
+ * Wait time between each vacuuming batch in milliseconds. Defaults to 10.
492
+ */
493
+ batchWait?: number
494
+ }
495
+
496
+ /**
497
+ * Sets minimum thresholds for `dirtCount` and `dirtFactor` that trigger an
498
+ * automatic vacuuming.
499
+ */
500
+ export type VacuumConditions = {
501
+ /**
502
+ * Minimum `dirtCount` (number of discarded documents since the last vacuuming)
503
+ * under which auto vacuum is not triggered. It defaults to 20.
504
+ */
505
+ minDirtCount?: number
506
+
507
+ /**
508
+ * Minimum `dirtFactor` (proportion of discarded documents over the total)
509
+ * under which auto vacuum is not triggered. It defaults to 0.1.
510
+ */
511
+ minDirtFactor?: number,
512
+ }
513
+
514
+ /**
515
+ * Options to control auto vacuum behavior. When discarding a document with
516
+ * {@link MiniSearch#discard}, a vacuuming operation is automatically started if
517
+ * the `dirtCount` and `dirtFactor` are above the `minDirtCount` and
518
+ * `minDirtFactor` thresholds defined by this configuration. See {@link
519
+ * VacuumConditions} for details on these.
520
+ *
521
+ * Also, `batchSize` and `batchWait` can be specified, controlling batching
522
+ * behavior (see {@link VacuumOptions}).
523
+ */
524
+ export type AutoVacuumOptions = VacuumOptions & VacuumConditions
525
+
526
+ type QuerySpec = {
527
+ prefix: boolean,
528
+ fuzzy: number | boolean,
529
+ term: string,
530
+ termBoost: number
531
+ }
532
+
533
+ type DocumentTermFreqs = Map<number, number>
534
+ type FieldTermData = Map<number, DocumentTermFreqs>
535
+
536
+ interface RawResultValue {
537
+ // Intermediate score, before applying the final score based on number of
538
+ // matched terms.
539
+ score: number,
540
+
541
+ // Set of all query terms that were matched. They may not be present in the
542
+ // text exactly in the case of prefix/fuzzy matches. We must check for
543
+ // uniqueness before adding a new term. This is much faster than using a set,
544
+ // because the number of elements is relatively small.
545
+ terms: string[],
546
+
547
+ // All terms that were found in the content, including the fields in which
548
+ // they were present. This object will be provided as part of the final search
549
+ // results.
550
+ match: MatchInfo,
551
+ }
552
+
553
+ type RawResult = Map<number, RawResultValue>
554
+
555
+ /**
556
+ * {@link MiniSearch} is the main entrypoint class, implementing a full-text
557
+ * search engine in memory.
558
+ *
559
+ * @typeParam T The type of the documents being indexed.
560
+ *
561
+ * ### Basic example:
562
+ *
563
+ * ```javascript
564
+ * const documents = [
565
+ * {
566
+ * id: 1,
567
+ * title: 'Moby Dick',
568
+ * text: 'Call me Ishmael. Some years ago...',
569
+ * category: 'fiction'
570
+ * },
571
+ * {
572
+ * id: 2,
573
+ * title: 'Zen and the Art of Motorcycle Maintenance',
574
+ * text: 'I can see by my watch...',
575
+ * category: 'fiction'
576
+ * },
577
+ * {
578
+ * id: 3,
579
+ * title: 'Neuromancer',
580
+ * text: 'The sky above the port was...',
581
+ * category: 'fiction'
582
+ * },
583
+ * {
584
+ * id: 4,
585
+ * title: 'Zen and the Art of Archery',
586
+ * text: 'At first sight it must seem...',
587
+ * category: 'non-fiction'
588
+ * },
589
+ * // ...and more
590
+ * ]
591
+ *
592
+ * // Create a search engine that indexes the 'title' and 'text' fields for
593
+ * // full-text search. Search results will include 'title' and 'category' (plus the
594
+ * // id field, that is always stored and returned)
595
+ * const miniSearch = new MiniSearch({
596
+ * fields: ['title', 'text'],
597
+ * storeFields: ['title', 'category']
598
+ * })
599
+ *
600
+ * // Add documents to the index
601
+ * miniSearch.addAll(documents)
602
+ *
603
+ * // Search for documents:
604
+ * let results = miniSearch.search('zen art motorcycle')
605
+ * // => [
606
+ * // { id: 2, title: 'Zen and the Art of Motorcycle Maintenance', category: 'fiction', score: 2.77258 },
607
+ * // { id: 4, title: 'Zen and the Art of Archery', category: 'non-fiction', score: 1.38629 }
608
+ * // ]
609
+ * ```
610
+ */
611
+ export default class MiniSearch<T = any> {
612
+ protected _options: OptionsWithDefaults<T>
613
+ protected _index: SearchableMap<FieldTermData>
614
+ protected _documentCount: number
615
+ protected _documentIds: Map<number, any>
616
+ protected _idToShortId: Map<any, number>
617
+ protected _fieldIds: { [key: string]: number }
618
+ protected _fieldLength: Map<number, number[]>
619
+ protected _avgFieldLength: number[]
620
+ protected _nextId: number
621
+ protected _storedFields: Map<number, Record<string, unknown>>
622
+ protected _dirtCount: number
623
+ private _currentVacuum: Promise<void> | null
624
+ private _enqueuedVacuum: Promise<void> | null
625
+ private _enqueuedVacuumConditions: VacuumConditions | undefined
626
+
627
+ /**
628
+ * The special wildcard symbol that can be passed to {@link MiniSearch#search}
629
+ * to match all documents
630
+ */
631
+ static readonly wildcard: unique symbol = Symbol('*')
632
+
633
+ /**
634
+ * @param options Configuration options
635
+ *
636
+ * ### Examples:
637
+ *
638
+ * ```javascript
639
+ * // Create a search engine that indexes the 'title' and 'text' fields of your
640
+ * // documents:
641
+ * const miniSearch = new MiniSearch({ fields: ['title', 'text'] })
642
+ * ```
643
+ *
644
+ * ### ID Field:
645
+ *
646
+ * ```javascript
647
+ * // Your documents are assumed to include a unique 'id' field, but if you want
648
+ * // to use a different field for document identification, you can set the
649
+ * // 'idField' option:
650
+ * const miniSearch = new MiniSearch({ idField: 'key', fields: ['title', 'text'] })
651
+ * ```
652
+ *
653
+ * ### Options and defaults:
654
+ *
655
+ * ```javascript
656
+ * // The full set of options (here with their default value) is:
657
+ * const miniSearch = new MiniSearch({
658
+ * // idField: field that uniquely identifies a document
659
+ * idField: 'id',
660
+ *
661
+ * // extractField: function used to get the value of a field in a document.
662
+ * // By default, it assumes the document is a flat object with field names as
663
+ * // property keys and field values as string property values, but custom logic
664
+ * // can be implemented by setting this option to a custom extractor function.
665
+ * extractField: (document, fieldName) => document[fieldName],
666
+ *
667
+ * // tokenize: function used to split fields into individual terms. By
668
+ * // default, it is also used to tokenize search queries, unless a specific
669
+ * // `tokenize` search option is supplied. When tokenizing an indexed field,
670
+ * // the field name is passed as the second argument.
671
+ * tokenize: (string, _fieldName) => string.split(SPACE_OR_PUNCTUATION),
672
+ *
673
+ * // processTerm: function used to process each tokenized term before
674
+ * // indexing. It can be used for stemming and normalization. Return a falsy
675
+ * // value in order to discard a term. By default, it is also used to process
676
+ * // search queries, unless a specific `processTerm` option is supplied as a
677
+ * // search option. When processing a term from a indexed field, the field
678
+ * // name is passed as the second argument.
679
+ * processTerm: (term, _fieldName) => term.toLowerCase(),
680
+ *
681
+ * // searchOptions: default search options, see the `search` method for
682
+ * // details
683
+ * searchOptions: undefined,
684
+ *
685
+ * // fields: document fields to be indexed. Mandatory, but not set by default
686
+ * fields: undefined
687
+ *
688
+ * // storeFields: document fields to be stored and returned as part of the
689
+ * // search results.
690
+ * storeFields: []
691
+ * })
692
+ * ```
693
+ */
694
+ constructor (options: Options<T>) {
695
+ if (options?.fields == null) {
696
+ throw new Error('MiniSearch: option "fields" must be provided')
697
+ }
698
+
699
+ const autoVacuum = (options.autoVacuum == null || options.autoVacuum === true) ? defaultAutoVacuumOptions : options.autoVacuum
700
+
701
+ this._options = {
702
+ ...defaultOptions,
703
+ ...options,
704
+ autoVacuum,
705
+ searchOptions: { ...defaultSearchOptions, ...(options.searchOptions || {}) },
706
+ autoSuggestOptions: { ...defaultAutoSuggestOptions, ...(options.autoSuggestOptions || {}) }
707
+ }
708
+
709
+ this._index = new SearchableMap()
710
+
711
+ this._documentCount = 0
712
+
713
+ this._documentIds = new Map()
714
+
715
+ this._idToShortId = new Map()
716
+
717
+ // Fields are defined during initialization, don't change, are few in
718
+ // number, rarely need iterating over, and have string keys. Therefore in
719
+ // this case an object is a better candidate than a Map to store the mapping
720
+ // from field key to ID.
721
+ this._fieldIds = {}
722
+
723
+ this._fieldLength = new Map()
724
+
725
+ this._avgFieldLength = []
726
+
727
+ this._nextId = 0
728
+
729
+ this._storedFields = new Map()
730
+
731
+ this._dirtCount = 0
732
+
733
+ this._currentVacuum = null
734
+
735
+ this._enqueuedVacuum = null
736
+ this._enqueuedVacuumConditions = defaultVacuumConditions
737
+
738
+ this.addFields(this._options.fields)
739
+ }
740
+
741
+ /**
742
+ * Adds a document to the index
743
+ *
744
+ * @param document The document to be indexed
745
+ */
746
+ add (document: T): void {
747
+ const { extractField, stringifyField, tokenize, processTerm, fields, idField } = this._options
748
+ const id = extractField(document, idField)
749
+ if (id == null) {
750
+ throw new Error(`MiniSearch: document does not have ID field "${idField}"`)
751
+ }
752
+
753
+ if (this._idToShortId.has(id)) {
754
+ throw new Error(`MiniSearch: duplicate ID ${id}`)
755
+ }
756
+
757
+ const shortDocumentId = this.addDocumentId(id)
758
+ this.saveStoredFields(shortDocumentId, document)
759
+
760
+ for (const field of fields) {
761
+ const fieldValue = extractField(document, field)
762
+ if (fieldValue == null) continue
763
+
764
+ const tokens = tokenize(stringifyField(fieldValue, field), field)
765
+ const fieldId = this._fieldIds[field]
766
+
767
+ const uniqueTerms = new Set(tokens).size
768
+ this.addFieldLength(shortDocumentId, fieldId, this._documentCount - 1, uniqueTerms)
769
+
770
+ for (const term of tokens) {
771
+ const processedTerm = processTerm(term, field)
772
+ if (Array.isArray(processedTerm)) {
773
+ for (const t of processedTerm) {
774
+ this.addTerm(fieldId, shortDocumentId, t)
775
+ }
776
+ } else if (processedTerm) {
777
+ this.addTerm(fieldId, shortDocumentId, processedTerm)
778
+ }
779
+ }
780
+ }
781
+ }
782
+
783
+ /**
784
+ * Adds all the given documents to the index
785
+ *
786
+ * @param documents An array of documents to be indexed
787
+ */
788
+ addAll (documents: readonly T[]): void {
789
+ for (const document of documents) this.add(document)
790
+ }
791
+
792
+ /**
793
+ * Adds all the given documents to the index asynchronously.
794
+ *
795
+ * Returns a promise that resolves (to `undefined`) when the indexing is done.
796
+ * This method is useful when index many documents, to avoid blocking the main
797
+ * thread. The indexing is performed asynchronously and in chunks.
798
+ *
799
+ * @param documents An array of documents to be indexed
800
+ * @param options Configuration options
801
+ * @return A promise resolving to `undefined` when the indexing is done
802
+ */
803
+ addAllAsync (documents: readonly T[], options: { chunkSize?: number } = {}): Promise<void> {
804
+ const { chunkSize = 10 } = options
805
+ const acc: { chunk: T[], promise: Promise<void> } = { chunk: [], promise: Promise.resolve() }
806
+
807
+ const { chunk, promise } = documents.reduce(({ chunk, promise }, document: T, i: number) => {
808
+ chunk.push(document)
809
+ if ((i + 1) % chunkSize === 0) {
810
+ return {
811
+ chunk: [],
812
+ promise: promise
813
+ .then(() => new Promise(resolve => setTimeout(resolve, 0)))
814
+ .then(() => this.addAll(chunk))
815
+ }
816
+ } else {
817
+ return { chunk, promise }
818
+ }
819
+ }, acc)
820
+
821
+ return promise.then(() => this.addAll(chunk))
822
+ }
823
+
824
+ /**
825
+ * Removes the given document from the index.
826
+ *
827
+ * The document to remove must NOT have changed between indexing and removal,
828
+ * otherwise the index will be corrupted.
829
+ *
830
+ * This method requires passing the full document to be removed (not just the
831
+ * ID), and immediately removes the document from the inverted index, allowing
832
+ * memory to be released. A convenient alternative is {@link
833
+ * MiniSearch#discard}, which needs only the document ID, and has the same
834
+ * visible effect, but delays cleaning up the index until the next vacuuming.
835
+ *
836
+ * @param document The document to be removed
837
+ */
838
+ remove (document: T): void {
839
+ const { tokenize, processTerm, extractField, stringifyField, fields, idField } = this._options
840
+ const id = extractField(document, idField)
841
+
842
+ if (id == null) {
843
+ throw new Error(`MiniSearch: document does not have ID field "${idField}"`)
844
+ }
845
+
846
+ const shortId = this._idToShortId.get(id)
847
+
848
+ if (shortId == null) {
849
+ throw new Error(`MiniSearch: cannot remove document with ID ${id}: it is not in the index`)
850
+ }
851
+
852
+ for (const field of fields) {
853
+ const fieldValue = extractField(document, field)
854
+ if (fieldValue == null) continue
855
+
856
+ const tokens = tokenize(stringifyField(fieldValue, field), field)
857
+ const fieldId = this._fieldIds[field]
858
+
859
+ const uniqueTerms = new Set(tokens).size
860
+ this.removeFieldLength(shortId, fieldId, this._documentCount, uniqueTerms)
861
+
862
+ for (const term of tokens) {
863
+ const processedTerm = processTerm(term, field)
864
+ if (Array.isArray(processedTerm)) {
865
+ for (const t of processedTerm) {
866
+ this.removeTerm(fieldId, shortId, t)
867
+ }
868
+ } else if (processedTerm) {
869
+ this.removeTerm(fieldId, shortId, processedTerm)
870
+ }
871
+ }
872
+ }
873
+
874
+ this._storedFields.delete(shortId)
875
+ this._documentIds.delete(shortId)
876
+ this._idToShortId.delete(id)
877
+ this._fieldLength.delete(shortId)
878
+ this._documentCount -= 1
879
+ }
880
+
881
+ /**
882
+ * Removes all the given documents from the index. If called with no arguments,
883
+ * it removes _all_ documents from the index.
884
+ *
885
+ * @param documents The documents to be removed. If this argument is omitted,
886
+ * all documents are removed. Note that, for removing all documents, it is
887
+ * more efficient to call this method with no arguments than to pass all
888
+ * documents.
889
+ */
890
+ removeAll (documents?: readonly T[]): void {
891
+ if (documents) {
892
+ for (const document of documents) this.remove(document)
893
+ } else if (arguments.length > 0) {
894
+ throw new Error('Expected documents to be present. Omit the argument to remove all documents.')
895
+ } else {
896
+ this._index = new SearchableMap()
897
+ this._documentCount = 0
898
+ this._documentIds = new Map()
899
+ this._idToShortId = new Map()
900
+ this._fieldLength = new Map()
901
+ this._avgFieldLength = []
902
+ this._storedFields = new Map()
903
+ this._nextId = 0
904
+ }
905
+ }
906
+
907
+ /**
908
+ * Discards the document with the given ID, so it won't appear in search results
909
+ *
910
+ * It has the same visible effect of {@link MiniSearch.remove} (both cause the
911
+ * document to stop appearing in searches), but a different effect on the
912
+ * internal data structures:
913
+ *
914
+ * - {@link MiniSearch#remove} requires passing the full document to be
915
+ * removed as argument, and removes it from the inverted index immediately.
916
+ *
917
+ * - {@link MiniSearch#discard} instead only needs the document ID, and
918
+ * works by marking the current version of the document as discarded, so it
919
+ * is immediately ignored by searches. This is faster and more convenient
920
+ * than {@link MiniSearch#remove}, but the index is not immediately
921
+ * modified. To take care of that, vacuuming is performed after a certain
922
+ * number of documents are discarded, cleaning up the index and allowing
923
+ * memory to be released.
924
+ *
925
+ * After discarding a document, it is possible to re-add a new version, and
926
+ * only the new version will appear in searches. In other words, discarding
927
+ * and re-adding a document works exactly like removing and re-adding it. The
928
+ * {@link MiniSearch.replace} method can also be used to replace a document
929
+ * with a new version.
930
+ *
931
+ * #### Details about vacuuming
932
+ *
933
+ * Repetite calls to this method would leave obsolete document references in
934
+ * the index, invisible to searches. Two mechanisms take care of cleaning up:
935
+ * clean up during search, and vacuuming.
936
+ *
937
+ * - Upon search, whenever a discarded ID is found (and ignored for the
938
+ * results), references to the discarded document are removed from the
939
+ * inverted index entries for the search terms. This ensures that subsequent
940
+ * searches for the same terms do not need to skip these obsolete references
941
+ * again.
942
+ *
943
+ * - In addition, vacuuming is performed automatically by default (see the
944
+ * `autoVacuum` field in {@link Options}) after a certain number of
945
+ * documents are discarded. Vacuuming traverses all terms in the index,
946
+ * cleaning up all references to discarded documents. Vacuuming can also be
947
+ * triggered manually by calling {@link MiniSearch#vacuum}.
948
+ *
949
+ * @param id The ID of the document to be discarded
950
+ */
951
+ discard (id: any): void {
952
+ const shortId = this._idToShortId.get(id)
953
+
954
+ if (shortId == null) {
955
+ throw new Error(`MiniSearch: cannot discard document with ID ${id}: it is not in the index`)
956
+ }
957
+
958
+ this._idToShortId.delete(id)
959
+ this._documentIds.delete(shortId)
960
+ this._storedFields.delete(shortId)
961
+
962
+ ;(this._fieldLength.get(shortId) || []).forEach((fieldLength, fieldId) => {
963
+ this.removeFieldLength(shortId, fieldId, this._documentCount, fieldLength)
964
+ })
965
+
966
+ this._fieldLength.delete(shortId)
967
+
968
+ this._documentCount -= 1
969
+ this._dirtCount += 1
970
+
971
+ this.maybeAutoVacuum()
972
+ }
973
+
974
+ private maybeAutoVacuum (): void {
975
+ if (this._options.autoVacuum === false) { return }
976
+
977
+ const { minDirtFactor, minDirtCount, batchSize, batchWait } = this._options.autoVacuum
978
+ this.conditionalVacuum({ batchSize, batchWait }, { minDirtCount, minDirtFactor })
979
+ }
980
+
981
+ /**
982
+ * Discards the documents with the given IDs, so they won't appear in search
983
+ * results
984
+ *
985
+ * It is equivalent to calling {@link MiniSearch#discard} for all the given
986
+ * IDs, but with the optimization of triggering at most one automatic
987
+ * vacuuming at the end.
988
+ *
989
+ * Note: to remove all documents from the index, it is faster and more
990
+ * convenient to call {@link MiniSearch.removeAll} with no argument, instead
991
+ * of passing all IDs to this method.
992
+ */
993
+ discardAll (ids: readonly any[]): void {
994
+ const autoVacuum = this._options.autoVacuum
995
+
996
+ try {
997
+ this._options.autoVacuum = false
998
+
999
+ for (const id of ids) {
1000
+ this.discard(id)
1001
+ }
1002
+ } finally {
1003
+ this._options.autoVacuum = autoVacuum
1004
+ }
1005
+
1006
+ this.maybeAutoVacuum()
1007
+ }
1008
+
1009
+ /**
1010
+ * It replaces an existing document with the given updated version
1011
+ *
1012
+ * It works by discarding the current version and adding the updated one, so
1013
+ * it is functionally equivalent to calling {@link MiniSearch#discard}
1014
+ * followed by {@link MiniSearch#add}. The ID of the updated document should
1015
+ * be the same as the original one.
1016
+ *
1017
+ * Since it uses {@link MiniSearch#discard} internally, this method relies on
1018
+ * vacuuming to clean up obsolete document references from the index, allowing
1019
+ * memory to be released (see {@link MiniSearch#discard}).
1020
+ *
1021
+ * @param updatedDocument The updated document to replace the old version
1022
+ * with
1023
+ */
1024
+ replace (updatedDocument: T): void {
1025
+ const { idField, extractField } = this._options
1026
+ const id = extractField(updatedDocument, idField)
1027
+
1028
+ this.discard(id)
1029
+ this.add(updatedDocument)
1030
+ }
1031
+
1032
+ /**
1033
+ * Triggers a manual vacuuming, cleaning up references to discarded documents
1034
+ * from the inverted index
1035
+ *
1036
+ * Vacuuming is only useful for applications that use the {@link
1037
+ * MiniSearch#discard} or {@link MiniSearch#replace} methods.
1038
+ *
1039
+ * By default, vacuuming is performed automatically when needed (controlled by
1040
+ * the `autoVacuum` field in {@link Options}), so there is usually no need to
1041
+ * call this method, unless one wants to make sure to perform vacuuming at a
1042
+ * specific moment.
1043
+ *
1044
+ * Vacuuming traverses all terms in the inverted index in batches, and cleans
1045
+ * up references to discarded documents from the posting list, allowing memory
1046
+ * to be released.
1047
+ *
1048
+ * The method takes an optional object as argument with the following keys:
1049
+ *
1050
+ * - `batchSize`: the size of each batch (1000 by default)
1051
+ *
1052
+ * - `batchWait`: the number of milliseconds to wait between batches (10 by
1053
+ * default)
1054
+ *
1055
+ * On large indexes, vacuuming could have a non-negligible cost: batching
1056
+ * avoids blocking the thread for long, diluting this cost so that it is not
1057
+ * negatively affecting the application. Nonetheless, this method should only
1058
+ * be called when necessary, and relying on automatic vacuuming is usually
1059
+ * better.
1060
+ *
1061
+ * It returns a promise that resolves (to undefined) when the clean up is
1062
+ * completed. If vacuuming is already ongoing at the time this method is
1063
+ * called, a new one is enqueued immediately after the ongoing one, and a
1064
+ * corresponding promise is returned. However, no more than one vacuuming is
1065
+ * enqueued on top of the ongoing one, even if this method is called more
1066
+ * times (enqueuing multiple ones would be useless).
1067
+ *
1068
+ * @param options Configuration options for the batch size and delay. See
1069
+ * {@link VacuumOptions}.
1070
+ */
1071
+ vacuum (options: VacuumOptions = {}): Promise<void> {
1072
+ return this.conditionalVacuum(options)
1073
+ }
1074
+
1075
+ private conditionalVacuum (options: VacuumOptions, conditions?: VacuumConditions): Promise<void> {
1076
+ // If a vacuum is already ongoing, schedule another as soon as it finishes,
1077
+ // unless there's already one enqueued. If one was already enqueued, do not
1078
+ // enqueue another on top, but make sure that the conditions are the
1079
+ // broadest.
1080
+ if (this._currentVacuum) {
1081
+ this._enqueuedVacuumConditions = this._enqueuedVacuumConditions && conditions
1082
+ if (this._enqueuedVacuum != null) { return this._enqueuedVacuum }
1083
+
1084
+ this._enqueuedVacuum = this._currentVacuum.then(() => {
1085
+ const conditions = this._enqueuedVacuumConditions
1086
+ this._enqueuedVacuumConditions = defaultVacuumConditions
1087
+ return this.performVacuuming(options, conditions)
1088
+ })
1089
+ return this._enqueuedVacuum
1090
+ }
1091
+
1092
+ if (this.vacuumConditionsMet(conditions) === false) { return Promise.resolve() }
1093
+
1094
+ this._currentVacuum = this.performVacuuming(options)
1095
+ return this._currentVacuum
1096
+ }
1097
+
1098
+ private async performVacuuming (options: VacuumOptions, conditions?: VacuumConditions): Promise<void> {
1099
+ const initialDirtCount = this._dirtCount
1100
+
1101
+ if (this.vacuumConditionsMet(conditions)) {
1102
+ const batchSize = options.batchSize || defaultVacuumOptions.batchSize
1103
+ const batchWait = options.batchWait || defaultVacuumOptions.batchWait
1104
+ let i = 1
1105
+
1106
+ for (const [term, fieldsData] of this._index) {
1107
+ for (const [fieldId, fieldIndex] of fieldsData) {
1108
+ for (const [shortId] of fieldIndex) {
1109
+ if (this._documentIds.has(shortId)) { continue }
1110
+
1111
+ if (fieldIndex.size <= 1) {
1112
+ fieldsData.delete(fieldId)
1113
+ } else {
1114
+ fieldIndex.delete(shortId)
1115
+ }
1116
+ }
1117
+ }
1118
+
1119
+ if (this._index.get(term)!.size === 0) {
1120
+ this._index.delete(term)
1121
+ }
1122
+
1123
+ if (i % batchSize === 0) {
1124
+ await new Promise((resolve) => setTimeout(resolve, batchWait))
1125
+ }
1126
+
1127
+ i += 1
1128
+ }
1129
+
1130
+ this._dirtCount -= initialDirtCount
1131
+ }
1132
+
1133
+ // Make the next lines always async, so they execute after this function returns
1134
+ await null
1135
+
1136
+ this._currentVacuum = this._enqueuedVacuum
1137
+ this._enqueuedVacuum = null
1138
+ }
1139
+
1140
+ private vacuumConditionsMet (conditions?: VacuumConditions) {
1141
+ if (conditions == null) { return true }
1142
+
1143
+ let { minDirtCount, minDirtFactor } = conditions
1144
+ minDirtCount = minDirtCount || defaultAutoVacuumOptions.minDirtCount
1145
+ minDirtFactor = minDirtFactor || defaultAutoVacuumOptions.minDirtFactor
1146
+
1147
+ return this.dirtCount >= minDirtCount && this.dirtFactor >= minDirtFactor
1148
+ }
1149
+
1150
+ /**
1151
+ * Is `true` if a vacuuming operation is ongoing, `false` otherwise
1152
+ */
1153
+ get isVacuuming (): boolean {
1154
+ return this._currentVacuum != null
1155
+ }
1156
+
1157
+ /**
1158
+ * The number of documents discarded since the most recent vacuuming
1159
+ */
1160
+ get dirtCount (): number {
1161
+ return this._dirtCount
1162
+ }
1163
+
1164
+ /**
1165
+ * A number between 0 and 1 giving an indication about the proportion of
1166
+ * documents that are discarded, and can therefore be cleaned up by vacuuming.
1167
+ * A value close to 0 means that the index is relatively clean, while a higher
1168
+ * value means that the index is relatively dirty, and vacuuming could release
1169
+ * memory.
1170
+ */
1171
+ get dirtFactor (): number {
1172
+ return this._dirtCount / (1 + this._documentCount + this._dirtCount)
1173
+ }
1174
+
1175
+ /**
1176
+ * Returns `true` if a document with the given ID is present in the index and
1177
+ * available for search, `false` otherwise
1178
+ *
1179
+ * @param id The document ID
1180
+ */
1181
+ has (id: any): boolean {
1182
+ return this._idToShortId.has(id)
1183
+ }
1184
+
1185
+ /**
1186
+ * Returns the stored fields (as configured in the `storeFields` constructor
1187
+ * option) for the given document ID. Returns `undefined` if the document is
1188
+ * not present in the index.
1189
+ *
1190
+ * @param id The document ID
1191
+ */
1192
+ getStoredFields (id: any): Record<string, unknown> | undefined {
1193
+ const shortId = this._idToShortId.get(id)
1194
+
1195
+ if (shortId == null) { return undefined }
1196
+
1197
+ return this._storedFields.get(shortId)
1198
+ }
1199
+
1200
+ /**
1201
+ * Search for documents matching the given search query.
1202
+ *
1203
+ * The result is a list of scored document IDs matching the query, sorted by
1204
+ * descending score, and each including data about which terms were matched and
1205
+ * in which fields.
1206
+ *
1207
+ * ### Basic usage:
1208
+ *
1209
+ * ```javascript
1210
+ * // Search for "zen art motorcycle" with default options: terms have to match
1211
+ * // exactly, and individual terms are joined with OR
1212
+ * miniSearch.search('zen art motorcycle')
1213
+ * // => [ { id: 2, score: 2.77258, match: { ... } }, { id: 4, score: 1.38629, match: { ... } } ]
1214
+ * ```
1215
+ *
1216
+ * ### Restrict search to specific fields:
1217
+ *
1218
+ * ```javascript
1219
+ * // Search only in the 'title' field
1220
+ * miniSearch.search('zen', { fields: ['title'] })
1221
+ * ```
1222
+ *
1223
+ * ### Field boosting:
1224
+ *
1225
+ * ```javascript
1226
+ * // Boost a field
1227
+ * miniSearch.search('zen', { boost: { title: 2 } })
1228
+ * ```
1229
+ *
1230
+ * ### Prefix search:
1231
+ *
1232
+ * ```javascript
1233
+ * // Search for "moto" with prefix search (it will match documents
1234
+ * // containing terms that start with "moto" or "neuro")
1235
+ * miniSearch.search('moto neuro', { prefix: true })
1236
+ * ```
1237
+ *
1238
+ * ### Fuzzy search:
1239
+ *
1240
+ * ```javascript
1241
+ * // Search for "ismael" with fuzzy search (it will match documents containing
1242
+ * // terms similar to "ismael", with a maximum edit distance of 0.2 term.length
1243
+ * // (rounded to nearest integer)
1244
+ * miniSearch.search('ismael', { fuzzy: 0.2 })
1245
+ * ```
1246
+ *
1247
+ * ### Combining strategies:
1248
+ *
1249
+ * ```javascript
1250
+ * // Mix of exact match, prefix search, and fuzzy search
1251
+ * miniSearch.search('ismael mob', {
1252
+ * prefix: true,
1253
+ * fuzzy: 0.2
1254
+ * })
1255
+ * ```
1256
+ *
1257
+ * ### Advanced prefix and fuzzy search:
1258
+ *
1259
+ * ```javascript
1260
+ * // Perform fuzzy and prefix search depending on the search term. Here
1261
+ * // performing prefix and fuzzy search only on terms longer than 3 characters
1262
+ * miniSearch.search('ismael mob', {
1263
+ * prefix: term => term.length > 3
1264
+ * fuzzy: term => term.length > 3 ? 0.2 : null
1265
+ * })
1266
+ * ```
1267
+ *
1268
+ * ### Combine with AND:
1269
+ *
1270
+ * ```javascript
1271
+ * // Combine search terms with AND (to match only documents that contain both
1272
+ * // "motorcycle" and "art")
1273
+ * miniSearch.search('motorcycle art', { combineWith: 'AND' })
1274
+ * ```
1275
+ *
1276
+ * ### Combine with AND_NOT:
1277
+ *
1278
+ * There is also an AND_NOT combinator, that finds documents that match the
1279
+ * first term, but do not match any of the other terms. This combinator is
1280
+ * rarely useful with simple queries, and is meant to be used with advanced
1281
+ * query combinations (see later for more details).
1282
+ *
1283
+ * ### Filtering results:
1284
+ *
1285
+ * ```javascript
1286
+ * // Filter only results in the 'fiction' category (assuming that 'category'
1287
+ * // is a stored field)
1288
+ * miniSearch.search('motorcycle art', {
1289
+ * filter: (result) => result.category === 'fiction'
1290
+ * })
1291
+ * ```
1292
+ *
1293
+ * ### Wildcard query
1294
+ *
1295
+ * Searching for an empty string (assuming the default tokenizer) returns no
1296
+ * results. Sometimes though, one needs to match all documents, like in a
1297
+ * "wildcard" search. This is possible by passing the special value
1298
+ * {@link MiniSearch.wildcard} as the query:
1299
+ *
1300
+ * ```javascript
1301
+ * // Return search results for all documents
1302
+ * miniSearch.search(MiniSearch.wildcard)
1303
+ * ```
1304
+ *
1305
+ * Note that search options such as `filter` and `boostDocument` are still
1306
+ * applied, influencing which results are returned, and their order:
1307
+ *
1308
+ * ```javascript
1309
+ * // Return search results for all documents in the 'fiction' category
1310
+ * miniSearch.search(MiniSearch.wildcard, {
1311
+ * filter: (result) => result.category === 'fiction'
1312
+ * })
1313
+ * ```
1314
+ *
1315
+ * ### Advanced combination of queries:
1316
+ *
1317
+ * It is possible to combine different subqueries with OR, AND, and AND_NOT,
1318
+ * and even with different search options, by passing a query expression
1319
+ * tree object as the first argument, instead of a string.
1320
+ *
1321
+ * ```javascript
1322
+ * // Search for documents that contain "zen" and ("motorcycle" or "archery")
1323
+ * miniSearch.search({
1324
+ * combineWith: 'AND',
1325
+ * queries: [
1326
+ * 'zen',
1327
+ * {
1328
+ * combineWith: 'OR',
1329
+ * queries: ['motorcycle', 'archery']
1330
+ * }
1331
+ * ]
1332
+ * })
1333
+ *
1334
+ * // Search for documents that contain ("apple" or "pear") but not "juice" and
1335
+ * // not "tree"
1336
+ * miniSearch.search({
1337
+ * combineWith: 'AND_NOT',
1338
+ * queries: [
1339
+ * {
1340
+ * combineWith: 'OR',
1341
+ * queries: ['apple', 'pear']
1342
+ * },
1343
+ * 'juice',
1344
+ * 'tree'
1345
+ * ]
1346
+ * })
1347
+ * ```
1348
+ *
1349
+ * Each node in the expression tree can be either a string, or an object that
1350
+ * supports all {@link SearchOptions} fields, plus a `queries` array field for
1351
+ * subqueries.
1352
+ *
1353
+ * Note that, while this can become complicated to do by hand for complex or
1354
+ * deeply nested queries, it provides a formalized expression tree API for
1355
+ * external libraries that implement a parser for custom query languages.
1356
+ *
1357
+ * @param query Search query
1358
+ * @param searchOptions Search options. Each option, if not given, defaults to the corresponding value of `searchOptions` given to the constructor, or to the library default.
1359
+ */
1360
+ search (query: Query, searchOptions: SearchOptions = {}): SearchResult[] {
1361
+ const { searchOptions: globalSearchOptions } = this._options
1362
+ const searchOptionsWithDefaults: SearchOptionsWithDefaults = { ...globalSearchOptions, ...searchOptions }
1363
+
1364
+ const rawResults = this.executeQuery(query, searchOptions)
1365
+ const results = []
1366
+
1367
+ for (const [docId, { score, terms, match }] of rawResults) {
1368
+ // terms are the matched query terms, which will be returned to the user
1369
+ // as queryTerms. The quality is calculated based on them, as opposed to
1370
+ // the matched terms in the document (which can be different due to
1371
+ // prefix and fuzzy match)
1372
+ const quality = terms.length || 1
1373
+
1374
+ const result = {
1375
+ id: this._documentIds.get(docId),
1376
+ score: score * quality,
1377
+ terms: Object.keys(match),
1378
+ queryTerms: terms,
1379
+ match
1380
+ }
1381
+
1382
+ Object.assign(result, this._storedFields.get(docId))
1383
+ if (searchOptionsWithDefaults.filter == null || searchOptionsWithDefaults.filter(result)) {
1384
+ results.push(result)
1385
+ }
1386
+ }
1387
+
1388
+ // If it's a wildcard query, and no document boost is applied, skip sorting
1389
+ // the results, as all results have the same score of 1
1390
+ if (query === MiniSearch.wildcard && searchOptionsWithDefaults.boostDocument == null) {
1391
+ return results
1392
+ }
1393
+
1394
+ results.sort(byScore)
1395
+ return results
1396
+ }
1397
+
1398
+ /**
1399
+ * Provide suggestions for the given search query
1400
+ *
1401
+ * The result is a list of suggested modified search queries, derived from the
1402
+ * given search query, each with a relevance score, sorted by descending score.
1403
+ *
1404
+ * By default, it uses the same options used for search, except that by
1405
+ * default it performs prefix search on the last term of the query, and
1406
+ * combine terms with `'AND'` (requiring all query terms to match). Custom
1407
+ * options can be passed as a second argument. Defaults can be changed upon
1408
+ * calling the {@link MiniSearch} constructor, by passing a
1409
+ * `autoSuggestOptions` option.
1410
+ *
1411
+ * ### Basic usage:
1412
+ *
1413
+ * ```javascript
1414
+ * // Get suggestions for 'neuro':
1415
+ * miniSearch.autoSuggest('neuro')
1416
+ * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ]
1417
+ * ```
1418
+ *
1419
+ * ### Multiple words:
1420
+ *
1421
+ * ```javascript
1422
+ * // Get suggestions for 'zen ar':
1423
+ * miniSearch.autoSuggest('zen ar')
1424
+ * // => [
1425
+ * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
1426
+ * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
1427
+ * // ]
1428
+ * ```
1429
+ *
1430
+ * ### Fuzzy suggestions:
1431
+ *
1432
+ * ```javascript
1433
+ * // Correct spelling mistakes using fuzzy search:
1434
+ * miniSearch.autoSuggest('neromancer', { fuzzy: 0.2 })
1435
+ * // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ]
1436
+ * ```
1437
+ *
1438
+ * ### Filtering:
1439
+ *
1440
+ * ```javascript
1441
+ * // Get suggestions for 'zen ar', but only within the 'fiction' category
1442
+ * // (assuming that 'category' is a stored field):
1443
+ * miniSearch.autoSuggest('zen ar', {
1444
+ * filter: (result) => result.category === 'fiction'
1445
+ * })
1446
+ * // => [
1447
+ * // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
1448
+ * // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
1449
+ * // ]
1450
+ * ```
1451
+ *
1452
+ * @param queryString Query string to be expanded into suggestions
1453
+ * @param options Search options. The supported options and default values
1454
+ * are the same as for the {@link MiniSearch#search} method, except that by
1455
+ * default prefix search is performed on the last term in the query, and terms
1456
+ * are combined with `'AND'`.
1457
+ * @return A sorted array of suggestions sorted by relevance score.
1458
+ */
1459
+ autoSuggest (queryString: string, options: SearchOptions = {}): Suggestion[] {
1460
+ options = { ...this._options.autoSuggestOptions, ...options }
1461
+
1462
+ const suggestions: Map<string, Omit<Suggestion, 'suggestion'> & { count: number }> = new Map()
1463
+
1464
+ for (const { score, terms } of this.search(queryString, options)) {
1465
+ const phrase = terms.join(' ')
1466
+ const suggestion = suggestions.get(phrase)
1467
+ if (suggestion != null) {
1468
+ suggestion.score += score
1469
+ suggestion.count += 1
1470
+ } else {
1471
+ suggestions.set(phrase, { score, terms, count: 1 })
1472
+ }
1473
+ }
1474
+
1475
+ const results = []
1476
+ for (const [suggestion, { score, terms, count }] of suggestions) {
1477
+ results.push({ suggestion, terms, score: score / count })
1478
+ }
1479
+
1480
+ results.sort(byScore)
1481
+ return results
1482
+ }
1483
+
1484
+ /**
1485
+ * Total number of documents available to search
1486
+ */
1487
+ get documentCount (): number {
1488
+ return this._documentCount
1489
+ }
1490
+
1491
+ /**
1492
+ * Number of terms in the index
1493
+ */
1494
+ get termCount (): number {
1495
+ return this._index.size
1496
+ }
1497
+
1498
+ /**
1499
+ * Deserializes a JSON index (serialized with `JSON.stringify(miniSearch)`)
1500
+ * and instantiates a MiniSearch instance. It should be given the same options
1501
+ * originally used when serializing the index.
1502
+ *
1503
+ * ### Usage:
1504
+ *
1505
+ * ```javascript
1506
+ * // If the index was serialized with:
1507
+ * let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
1508
+ * miniSearch.addAll(documents)
1509
+ *
1510
+ * const json = JSON.stringify(miniSearch)
1511
+ * // It can later be deserialized like this:
1512
+ * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
1513
+ * ```
1514
+ *
1515
+ * @param json JSON-serialized index
1516
+ * @param options configuration options, same as the constructor
1517
+ * @return An instance of MiniSearch deserialized from the given JSON.
1518
+ */
1519
+ static loadJSON<T = any> (json: string, options: Options<T>): MiniSearch<T> {
1520
+ if (options == null) {
1521
+ throw new Error('MiniSearch: loadJSON should be given the same options used when serializing the index')
1522
+ }
1523
+ return this.loadJS(JSON.parse(json), options)
1524
+ }
1525
+
1526
+ /**
1527
+ * Async equivalent of {@link MiniSearch.loadJSON}
1528
+ *
1529
+ * This function is an alternative to {@link MiniSearch.loadJSON} that returns
1530
+ * a promise, and loads the index in batches, leaving pauses between them to avoid
1531
+ * blocking the main thread. It tends to be slower than the synchronous
1532
+ * version, but does not block the main thread, so it can be a better choice
1533
+ * when deserializing very large indexes.
1534
+ *
1535
+ * @param json JSON-serialized index
1536
+ * @param options configuration options, same as the constructor
1537
+ * @return A Promise that will resolve to an instance of MiniSearch deserialized from the given JSON.
1538
+ */
1539
+ static async loadJSONAsync<T = any> (json: string, options: Options<T>): Promise<MiniSearch<T>> {
1540
+ if (options == null) {
1541
+ throw new Error('MiniSearch: loadJSON should be given the same options used when serializing the index')
1542
+ }
1543
+ return this.loadJSAsync(JSON.parse(json), options)
1544
+ }
1545
+
1546
+ /**
1547
+ * Returns the default value of an option. It will throw an error if no option
1548
+ * with the given name exists.
1549
+ *
1550
+ * @param optionName Name of the option
1551
+ * @return The default value of the given option
1552
+ *
1553
+ * ### Usage:
1554
+ *
1555
+ * ```javascript
1556
+ * // Get default tokenizer
1557
+ * MiniSearch.getDefault('tokenize')
1558
+ *
1559
+ * // Get default term processor
1560
+ * MiniSearch.getDefault('processTerm')
1561
+ *
1562
+ * // Unknown options will throw an error
1563
+ * MiniSearch.getDefault('notExisting')
1564
+ * // => throws 'MiniSearch: unknown option "notExisting"'
1565
+ * ```
1566
+ */
1567
+ static getDefault (optionName: string): any {
1568
+ if (defaultOptions.hasOwnProperty(optionName)) {
1569
+ return getOwnProperty(defaultOptions, optionName)
1570
+ } else {
1571
+ throw new Error(`MiniSearch: unknown option "${optionName}"`)
1572
+ }
1573
+ }
1574
+
1575
+ /**
1576
+ * @ignore
1577
+ */
1578
+ static loadJS<T = any> (js: AsPlainObject, options: Options<T>): MiniSearch<T> {
1579
+ const {
1580
+ index,
1581
+ documentIds,
1582
+ fieldLength,
1583
+ storedFields,
1584
+ serializationVersion
1585
+ } = js
1586
+
1587
+ const miniSearch = this.instantiateMiniSearch(js, options)
1588
+
1589
+ miniSearch._documentIds = objectToNumericMap(documentIds)
1590
+ miniSearch._fieldLength = objectToNumericMap(fieldLength)
1591
+ miniSearch._storedFields = objectToNumericMap(storedFields)
1592
+
1593
+ for (const [shortId, id] of miniSearch._documentIds) {
1594
+ miniSearch._idToShortId.set(id, shortId)
1595
+ }
1596
+
1597
+ for (const [term, data] of index) {
1598
+ const dataMap = new Map() as FieldTermData
1599
+
1600
+ for (const fieldId of Object.keys(data)) {
1601
+ let indexEntry = data[fieldId]
1602
+
1603
+ // Version 1 used to nest the index entry inside a field called ds
1604
+ if (serializationVersion === 1) {
1605
+ indexEntry = indexEntry.ds as unknown as SerializedIndexEntry
1606
+ }
1607
+
1608
+ dataMap.set(parseInt(fieldId, 10), objectToNumericMap(indexEntry) as DocumentTermFreqs)
1609
+ }
1610
+
1611
+ miniSearch._index.set(term, dataMap)
1612
+ }
1613
+
1614
+ return miniSearch
1615
+ }
1616
+
1617
+ /**
1618
+ * @ignore
1619
+ */
1620
+ static async loadJSAsync<T = any> (js: AsPlainObject, options: Options<T>): Promise<MiniSearch<T>> {
1621
+ const {
1622
+ index,
1623
+ documentIds,
1624
+ fieldLength,
1625
+ storedFields,
1626
+ serializationVersion
1627
+ } = js
1628
+
1629
+ const miniSearch = this.instantiateMiniSearch(js, options)
1630
+
1631
+ miniSearch._documentIds = await objectToNumericMapAsync(documentIds)
1632
+ miniSearch._fieldLength = await objectToNumericMapAsync(fieldLength)
1633
+ miniSearch._storedFields = await objectToNumericMapAsync(storedFields)
1634
+
1635
+ for (const [shortId, id] of miniSearch._documentIds) {
1636
+ miniSearch._idToShortId.set(id, shortId)
1637
+ }
1638
+
1639
+ let count = 0
1640
+ for (const [term, data] of index) {
1641
+ const dataMap = new Map() as FieldTermData
1642
+
1643
+ for (const fieldId of Object.keys(data)) {
1644
+ let indexEntry = data[fieldId]
1645
+
1646
+ // Version 1 used to nest the index entry inside a field called ds
1647
+ if (serializationVersion === 1) {
1648
+ indexEntry = indexEntry.ds as unknown as SerializedIndexEntry
1649
+ }
1650
+
1651
+ dataMap.set(parseInt(fieldId, 10), await objectToNumericMapAsync(indexEntry) as DocumentTermFreqs)
1652
+ }
1653
+
1654
+ if (++count % 1000 === 0) await wait(0)
1655
+ miniSearch._index.set(term, dataMap)
1656
+ }
1657
+
1658
+ return miniSearch
1659
+ }
1660
+
1661
+ /**
1662
+ * @ignore
1663
+ */
1664
+ private static instantiateMiniSearch<T = any> (js: AsPlainObject, options: Options<T>): MiniSearch<T> {
1665
+ const {
1666
+ documentCount,
1667
+ nextId,
1668
+ fieldIds,
1669
+ averageFieldLength,
1670
+ dirtCount,
1671
+ serializationVersion
1672
+ } = js
1673
+
1674
+ if (serializationVersion !== 1 && serializationVersion !== 2) {
1675
+ throw new Error('MiniSearch: cannot deserialize an index created with an incompatible version')
1676
+ }
1677
+
1678
+ const miniSearch = new MiniSearch(options)
1679
+
1680
+ miniSearch._documentCount = documentCount
1681
+ miniSearch._nextId = nextId
1682
+ miniSearch._idToShortId = new Map<any, number>()
1683
+ miniSearch._fieldIds = fieldIds
1684
+ miniSearch._avgFieldLength = averageFieldLength
1685
+ miniSearch._dirtCount = dirtCount || 0
1686
+ miniSearch._index = new SearchableMap()
1687
+
1688
+ return miniSearch
1689
+ }
1690
+
1691
+ /**
1692
+ * @ignore
1693
+ */
1694
+ private executeQuery (query: Query, searchOptions: SearchOptions = {}): RawResult {
1695
+ if (query === MiniSearch.wildcard) {
1696
+ return this.executeWildcardQuery(searchOptions)
1697
+ }
1698
+
1699
+ if (typeof query !== 'string') {
1700
+ const options = { ...searchOptions, ...query, queries: undefined }
1701
+ const results = query.queries.map((subquery) => this.executeQuery(subquery, options))
1702
+ return this.combineResults(results, options.combineWith)
1703
+ }
1704
+
1705
+ const { tokenize, processTerm, searchOptions: globalSearchOptions } = this._options
1706
+ const options = { tokenize, processTerm, ...globalSearchOptions, ...searchOptions }
1707
+ const { tokenize: searchTokenize, processTerm: searchProcessTerm } = options
1708
+ const terms = searchTokenize(query)
1709
+ .flatMap((term: string) => searchProcessTerm(term))
1710
+ .filter((term) => !!term) as string[]
1711
+ const queries: QuerySpec[] = terms.map(termToQuerySpec(options))
1712
+ const results = queries.map(query => this.executeQuerySpec(query, options))
1713
+
1714
+ return this.combineResults(results, options.combineWith)
1715
+ }
1716
+
1717
+ /**
1718
+ * @ignore
1719
+ */
1720
+ private executeQuerySpec (query: QuerySpec, searchOptions: SearchOptions): RawResult {
1721
+ const options: SearchOptionsWithDefaults = { ...this._options.searchOptions, ...searchOptions }
1722
+
1723
+ const boosts = (options.fields || this._options.fields).reduce((boosts, field) =>
1724
+ ({ ...boosts, [field]: getOwnProperty(options.boost, field) || 1 }), {})
1725
+
1726
+ const {
1727
+ boostDocument,
1728
+ weights,
1729
+ maxFuzzy,
1730
+ bm25: bm25params
1731
+ } = options
1732
+
1733
+ const { fuzzy: fuzzyWeight, prefix: prefixWeight } = { ...defaultSearchOptions.weights, ...weights }
1734
+
1735
+ const data = this._index.get(query.term)
1736
+ const results = this.termResults(query.term, query.term, 1, query.termBoost, data, boosts, boostDocument, bm25params)
1737
+
1738
+ let prefixMatches
1739
+ let fuzzyMatches
1740
+
1741
+ if (query.prefix) {
1742
+ prefixMatches = this._index.atPrefix(query.term)
1743
+ }
1744
+
1745
+ if (query.fuzzy) {
1746
+ const fuzzy = (query.fuzzy === true) ? 0.2 : query.fuzzy
1747
+ const maxDistance = fuzzy < 1 ? Math.min(maxFuzzy, Math.round(query.term.length * fuzzy)) : fuzzy
1748
+ if (maxDistance) fuzzyMatches = this._index.fuzzyGet(query.term, maxDistance)
1749
+ }
1750
+
1751
+ if (prefixMatches) {
1752
+ for (const [term, data] of prefixMatches) {
1753
+ const distance = term.length - query.term.length
1754
+ if (!distance) { continue } // Skip exact match.
1755
+
1756
+ // Delete the term from fuzzy results (if present) if it is also a
1757
+ // prefix result. This entry will always be scored as a prefix result.
1758
+ fuzzyMatches?.delete(term)
1759
+
1760
+ // Weight gradually approaches 0 as distance goes to infinity, with the
1761
+ // weight for the hypothetical distance 0 being equal to prefixWeight.
1762
+ // The rate of change is much lower than that of fuzzy matches to
1763
+ // account for the fact that prefix matches stay more relevant than
1764
+ // fuzzy matches for longer distances.
1765
+ const weight = prefixWeight * term.length / (term.length + 0.3 * distance)
1766
+ this.termResults(query.term, term, weight, query.termBoost, data, boosts, boostDocument, bm25params, results)
1767
+ }
1768
+ }
1769
+
1770
+ if (fuzzyMatches) {
1771
+ for (const term of fuzzyMatches.keys()) {
1772
+ const [data, distance] = fuzzyMatches.get(term)!
1773
+ if (!distance) { continue } // Skip exact match.
1774
+
1775
+ // Weight gradually approaches 0 as distance goes to infinity, with the
1776
+ // weight for the hypothetical distance 0 being equal to fuzzyWeight.
1777
+ const weight = fuzzyWeight * term.length / (term.length + distance)
1778
+ this.termResults(query.term, term, weight, query.termBoost, data, boosts, boostDocument, bm25params, results)
1779
+ }
1780
+ }
1781
+
1782
+ return results
1783
+ }
1784
+
1785
+ /**
1786
+ * @ignore
1787
+ */
1788
+ private executeWildcardQuery (searchOptions: SearchOptions): RawResult {
1789
+ const results = new Map() as RawResult
1790
+ const options: SearchOptionsWithDefaults = { ...this._options.searchOptions, ...searchOptions }
1791
+
1792
+ for (const [shortId, id] of this._documentIds) {
1793
+ const score = options.boostDocument ? options.boostDocument(id, '', this._storedFields.get(shortId)) : 1
1794
+ results.set(shortId, {
1795
+ score,
1796
+ terms: [],
1797
+ match: {}
1798
+ })
1799
+ }
1800
+
1801
+ return results
1802
+ }
1803
+
1804
+ /**
1805
+ * @ignore
1806
+ */
1807
+ private combineResults (results: RawResult[], combineWith: CombinationOperator = OR): RawResult {
1808
+ if (results.length === 0) { return new Map() }
1809
+ const operator = combineWith.toLowerCase()
1810
+ const combinator = (combinators as Record<string, CombinatorFunction>)[operator]
1811
+
1812
+ if (!combinator) {
1813
+ throw new Error(`Invalid combination operator: ${combineWith}`)
1814
+ }
1815
+
1816
+ return results.reduce(combinator) || new Map()
1817
+ }
1818
+
1819
+ /**
1820
+ * Allows serialization of the index to JSON, to possibly store it and later
1821
+ * deserialize it with {@link MiniSearch.loadJSON}.
1822
+ *
1823
+ * Normally one does not directly call this method, but rather call the
1824
+ * standard JavaScript `JSON.stringify()` passing the {@link MiniSearch}
1825
+ * instance, and JavaScript will internally call this method. Upon
1826
+ * deserialization, one must pass to {@link MiniSearch.loadJSON} the same
1827
+ * options used to create the original instance that was serialized.
1828
+ *
1829
+ * ### Usage:
1830
+ *
1831
+ * ```javascript
1832
+ * // Serialize the index:
1833
+ * let miniSearch = new MiniSearch({ fields: ['title', 'text'] })
1834
+ * miniSearch.addAll(documents)
1835
+ * const json = JSON.stringify(miniSearch)
1836
+ *
1837
+ * // Later, to deserialize it:
1838
+ * miniSearch = MiniSearch.loadJSON(json, { fields: ['title', 'text'] })
1839
+ * ```
1840
+ *
1841
+ * @return A plain-object serializable representation of the search index.
1842
+ */
1843
+ toJSON (): AsPlainObject {
1844
+ const index: [string, { [key: string]: SerializedIndexEntry }][] = []
1845
+
1846
+ for (const [term, fieldIndex] of this._index) {
1847
+ const data: { [key: string]: SerializedIndexEntry } = {}
1848
+
1849
+ for (const [fieldId, freqs] of fieldIndex) {
1850
+ data[fieldId] = Object.fromEntries(freqs)
1851
+ }
1852
+
1853
+ index.push([term, data])
1854
+ }
1855
+
1856
+ return {
1857
+ documentCount: this._documentCount,
1858
+ nextId: this._nextId,
1859
+ documentIds: Object.fromEntries(this._documentIds),
1860
+ fieldIds: this._fieldIds,
1861
+ fieldLength: Object.fromEntries(this._fieldLength),
1862
+ averageFieldLength: this._avgFieldLength,
1863
+ storedFields: Object.fromEntries(this._storedFields),
1864
+ dirtCount: this._dirtCount,
1865
+ index,
1866
+ serializationVersion: 2
1867
+ }
1868
+ }
1869
+
1870
+ /**
1871
+ * @ignore
1872
+ */
1873
+ private termResults (
1874
+ sourceTerm: string,
1875
+ derivedTerm: string,
1876
+ termWeight: number,
1877
+ termBoost: number,
1878
+ fieldTermData: FieldTermData | undefined,
1879
+ fieldBoosts: { [field: string]: number },
1880
+ boostDocumentFn: ((id: any, term: string, storedFields?: Record<string, unknown>) => number) | undefined,
1881
+ bm25params: BM25Params,
1882
+ results: RawResult = new Map()
1883
+ ): RawResult {
1884
+ if (fieldTermData == null) return results
1885
+
1886
+ for (const field of Object.keys(fieldBoosts)) {
1887
+ const fieldBoost = fieldBoosts[field]
1888
+ const fieldId = this._fieldIds[field]
1889
+
1890
+ const fieldTermFreqs = fieldTermData.get(fieldId)
1891
+ if (fieldTermFreqs == null) continue
1892
+
1893
+ let matchingFields = fieldTermFreqs.size
1894
+ const avgFieldLength = this._avgFieldLength[fieldId]
1895
+
1896
+ for (const docId of fieldTermFreqs.keys()) {
1897
+ if (!this._documentIds.has(docId)) {
1898
+ this.removeTerm(fieldId, docId, derivedTerm)
1899
+ matchingFields -= 1
1900
+ continue
1901
+ }
1902
+
1903
+ const docBoost = boostDocumentFn ? boostDocumentFn(this._documentIds.get(docId), derivedTerm, this._storedFields.get(docId)) : 1
1904
+ if (!docBoost) continue
1905
+
1906
+ const termFreq = fieldTermFreqs.get(docId)!
1907
+ const fieldLength = this._fieldLength.get(docId)![fieldId]
1908
+
1909
+ // NOTE: The total number of fields is set to the number of documents
1910
+ // `this._documentCount`. It could also make sense to use the number of
1911
+ // documents where the current field is non-blank as a normalization
1912
+ // factor. This will make a difference in scoring if the field is rarely
1913
+ // present. This is currently not supported, and may require further
1914
+ // analysis to see if it is a valid use case.
1915
+ const rawScore = calcBM25Score(termFreq, matchingFields, this._documentCount, fieldLength, avgFieldLength, bm25params)
1916
+ const weightedScore = termWeight * termBoost * fieldBoost * docBoost * rawScore
1917
+
1918
+ const result = results.get(docId)
1919
+ if (result) {
1920
+ result.score += weightedScore
1921
+ assignUniqueTerm(result.terms, sourceTerm)
1922
+ const match = getOwnProperty(result.match, derivedTerm)
1923
+ if (match) {
1924
+ match.push(field)
1925
+ } else {
1926
+ result.match[derivedTerm] = [field]
1927
+ }
1928
+ } else {
1929
+ results.set(docId, {
1930
+ score: weightedScore,
1931
+ terms: [sourceTerm],
1932
+ match: { [derivedTerm]: [field] }
1933
+ })
1934
+ }
1935
+ }
1936
+ }
1937
+
1938
+ return results
1939
+ }
1940
+
1941
+ /**
1942
+ * @ignore
1943
+ */
1944
+ private addTerm (fieldId: number, documentId: number, term: string): void {
1945
+ const indexData = this._index.fetch(term, createMap)
1946
+
1947
+ let fieldIndex = indexData.get(fieldId)
1948
+ if (fieldIndex == null) {
1949
+ fieldIndex = new Map()
1950
+ fieldIndex.set(documentId, 1)
1951
+ indexData.set(fieldId, fieldIndex)
1952
+ } else {
1953
+ const docs = fieldIndex.get(documentId)
1954
+ fieldIndex.set(documentId, (docs || 0) + 1)
1955
+ }
1956
+ }
1957
+
1958
+ /**
1959
+ * @ignore
1960
+ */
1961
+ private removeTerm (fieldId: number, documentId: number, term: string): void {
1962
+ if (!this._index.has(term)) {
1963
+ this.warnDocumentChanged(documentId, fieldId, term)
1964
+ return
1965
+ }
1966
+
1967
+ const indexData = this._index.fetch(term, createMap)
1968
+
1969
+ const fieldIndex = indexData.get(fieldId)
1970
+ if (fieldIndex == null || fieldIndex.get(documentId) == null) {
1971
+ this.warnDocumentChanged(documentId, fieldId, term)
1972
+ } else if (fieldIndex.get(documentId)! <= 1) {
1973
+ if (fieldIndex.size <= 1) {
1974
+ indexData.delete(fieldId)
1975
+ } else {
1976
+ fieldIndex.delete(documentId)
1977
+ }
1978
+ } else {
1979
+ fieldIndex.set(documentId, fieldIndex.get(documentId)! - 1)
1980
+ }
1981
+
1982
+ if (this._index.get(term)!.size === 0) {
1983
+ this._index.delete(term)
1984
+ }
1985
+ }
1986
+
1987
+ /**
1988
+ * @ignore
1989
+ */
1990
+ private warnDocumentChanged (shortDocumentId: number, fieldId: number, term: string): void {
1991
+ for (const fieldName of Object.keys(this._fieldIds)) {
1992
+ if (this._fieldIds[fieldName] === fieldId) {
1993
+ this._options.logger('warn', `MiniSearch: document with ID ${this._documentIds.get(shortDocumentId)} has changed before removal: term "${term}" was not present in field "${fieldName}". Removing a document after it has changed can corrupt the index!`, 'version_conflict')
1994
+ return
1995
+ }
1996
+ }
1997
+ }
1998
+
1999
+ /**
2000
+ * @ignore
2001
+ */
2002
+ private addDocumentId (documentId: any): number {
2003
+ const shortDocumentId = this._nextId
2004
+ this._idToShortId.set(documentId, shortDocumentId)
2005
+ this._documentIds.set(shortDocumentId, documentId)
2006
+ this._documentCount += 1
2007
+ this._nextId += 1
2008
+ return shortDocumentId
2009
+ }
2010
+
2011
+ /**
2012
+ * @ignore
2013
+ */
2014
+ private addFields (fields: string[]): void {
2015
+ for (let i = 0; i < fields.length; i++) {
2016
+ this._fieldIds[fields[i]] = i
2017
+ }
2018
+ }
2019
+
2020
+ /**
2021
+ * @ignore
2022
+ */
2023
+ private addFieldLength (documentId: number, fieldId: number, count: number, length: number): void {
2024
+ let fieldLengths = this._fieldLength.get(documentId)
2025
+ if (fieldLengths == null) this._fieldLength.set(documentId, fieldLengths = [])
2026
+ fieldLengths[fieldId] = length
2027
+
2028
+ const averageFieldLength = this._avgFieldLength[fieldId] || 0
2029
+ const totalFieldLength = (averageFieldLength * count) + length
2030
+ this._avgFieldLength[fieldId] = totalFieldLength / (count + 1)
2031
+ }
2032
+
2033
+ /**
2034
+ * @ignore
2035
+ */
2036
+ private removeFieldLength (documentId: number, fieldId: number, count: number, length: number): void {
2037
+ if (count === 1) {
2038
+ this._avgFieldLength[fieldId] = 0
2039
+ return
2040
+ }
2041
+ const totalFieldLength = (this._avgFieldLength[fieldId] * count) - length
2042
+ this._avgFieldLength[fieldId] = totalFieldLength / (count - 1)
2043
+ }
2044
+
2045
+ /**
2046
+ * @ignore
2047
+ */
2048
+ private saveStoredFields (documentId: number, doc: T): void {
2049
+ const { storeFields, extractField } = this._options
2050
+ if (storeFields == null || storeFields.length === 0) { return }
2051
+
2052
+ let documentFields = this._storedFields.get(documentId)
2053
+ if (documentFields == null) this._storedFields.set(documentId, documentFields = {})
2054
+
2055
+ for (const fieldName of storeFields) {
2056
+ const fieldValue = extractField(doc, fieldName)
2057
+ if (fieldValue !== undefined) documentFields[fieldName] = fieldValue
2058
+ }
2059
+ }
2060
+ }
2061
+
2062
+ const getOwnProperty = (object: any, property: string) =>
2063
+ Object.prototype.hasOwnProperty.call(object, property) ? object[property] : undefined
2064
+
2065
+ type CombinatorFunction = (a: RawResult, b: RawResult) => RawResult
2066
+
2067
+ const combinators: Record<LowercaseCombinationOperator, CombinatorFunction> = {
2068
+ [OR]: (a: RawResult, b: RawResult) => {
2069
+ for (const docId of b.keys()) {
2070
+ const existing = a.get(docId)
2071
+ if (existing == null) {
2072
+ a.set(docId, b.get(docId)!)
2073
+ } else {
2074
+ const { score, terms, match } = b.get(docId)!
2075
+ existing.score = existing.score + score
2076
+ existing.match = Object.assign(existing.match, match)
2077
+ assignUniqueTerms(existing.terms, terms)
2078
+ }
2079
+ }
2080
+
2081
+ return a
2082
+ },
2083
+ [AND]: (a: RawResult, b: RawResult) => {
2084
+ const combined = new Map()
2085
+
2086
+ for (const docId of b.keys()) {
2087
+ const existing = a.get(docId)
2088
+ if (existing == null) continue
2089
+
2090
+ const { score, terms, match } = b.get(docId)!
2091
+ assignUniqueTerms(existing.terms, terms)
2092
+ combined.set(docId, {
2093
+ score: existing.score + score,
2094
+ terms: existing.terms,
2095
+ match: Object.assign(existing.match, match)
2096
+ })
2097
+ }
2098
+
2099
+ return combined
2100
+ },
2101
+ [AND_NOT]: (a: RawResult, b: RawResult) => {
2102
+ for (const docId of b.keys()) a.delete(docId)
2103
+ return a
2104
+ }
2105
+ }
2106
+
2107
+ /**
2108
+ * Parameters of the BM25+ scoring algorithm. Customizing these is almost never
2109
+ * necessary, and finetuning them requires an understanding of the BM25 scoring
2110
+ * model.
2111
+ *
2112
+ * Some information about BM25 (and BM25+) can be found at these links:
2113
+ *
2114
+ * - https://en.wikipedia.org/wiki/Okapi_BM25
2115
+ * - https://opensourceconnections.com/blog/2015/10/16/bm25-the-next-generation-of-lucene-relevation/
2116
+ */
2117
+ export type BM25Params = {
2118
+ /** Term frequency saturation point.
2119
+ *
2120
+ * Recommended values are between `1.2` and `2`. Higher values increase the
2121
+ * difference in score between documents with higher and lower term
2122
+ * frequencies. Setting this to `0` or a negative value is invalid. Defaults
2123
+ * to `1.2`
2124
+ */
2125
+ k: number,
2126
+
2127
+ /**
2128
+ * Length normalization impact.
2129
+ *
2130
+ * Recommended values are around `0.75`. Higher values increase the weight
2131
+ * that field length has on scoring. Setting this to `0` (not recommended)
2132
+ * means that the field length has no effect on scoring. Negative values are
2133
+ * invalid. Defaults to `0.7`.
2134
+ */
2135
+ b: number,
2136
+
2137
+ /**
2138
+ * BM25+ frequency normalization lower bound (usually called δ).
2139
+ *
2140
+ * Recommended values are between `0.5` and `1`. Increasing this parameter
2141
+ * increases the minimum relevance of one occurrence of a search term
2142
+ * regardless of its (possibly very long) field length. Negative values are
2143
+ * invalid. Defaults to `0.5`.
2144
+ */
2145
+ d: number
2146
+ }
2147
+
2148
+ const defaultBM25params: BM25Params = { k: 1.2, b: 0.7, d: 0.5 }
2149
+
2150
+ const calcBM25Score = (
2151
+ termFreq: number,
2152
+ matchingCount: number,
2153
+ totalCount: number,
2154
+ fieldLength: number,
2155
+ avgFieldLength: number,
2156
+ bm25params: BM25Params
2157
+ ): number => {
2158
+ const { k, b, d } = bm25params
2159
+ const invDocFreq = Math.log(1 + (totalCount - matchingCount + 0.5) / (matchingCount + 0.5))
2160
+ return invDocFreq * (d + termFreq * (k + 1) / (termFreq + k * (1 - b + b * fieldLength / avgFieldLength)))
2161
+ }
2162
+
2163
+ const termToQuerySpec = (options: SearchOptions) => (term: string, i: number, terms: string[]): QuerySpec => {
2164
+ const fuzzy = (typeof options.fuzzy === 'function')
2165
+ ? options.fuzzy(term, i, terms)
2166
+ : (options.fuzzy || false)
2167
+ const prefix = (typeof options.prefix === 'function')
2168
+ ? options.prefix(term, i, terms)
2169
+ : (options.prefix === true)
2170
+ const termBoost = (typeof options.boostTerm === 'function')
2171
+ ? options.boostTerm(term, i, terms)
2172
+ : 1
2173
+ return { term, fuzzy, prefix, termBoost }
2174
+ }
2175
+
2176
+ const defaultOptions = {
2177
+ idField: 'id',
2178
+ extractField: (document: any, fieldName: string) => document[fieldName],
2179
+ stringifyField: (fieldValue: any, fieldName: string) => fieldValue.toString(),
2180
+ tokenize: (text: string) => text.split(SPACE_OR_PUNCTUATION),
2181
+ processTerm: (term: string) => term.toLowerCase(),
2182
+ fields: undefined,
2183
+ searchOptions: undefined,
2184
+ storeFields: [],
2185
+ logger: (level: LogLevel, message: string): void => {
2186
+ if (typeof console?.[level] === 'function') console[level](message)
2187
+ },
2188
+ autoVacuum: true
2189
+ }
2190
+
2191
+ const defaultSearchOptions = {
2192
+ combineWith: OR,
2193
+ prefix: false,
2194
+ fuzzy: false,
2195
+ maxFuzzy: 6,
2196
+ boost: {},
2197
+ weights: { fuzzy: 0.45, prefix: 0.375 },
2198
+ bm25: defaultBM25params
2199
+ }
2200
+
2201
+ const defaultAutoSuggestOptions = {
2202
+ combineWith: AND,
2203
+ prefix: (term: string, i: number, terms: string[]): boolean =>
2204
+ i === terms.length - 1
2205
+ }
2206
+
2207
+ const defaultVacuumOptions = { batchSize: 1000, batchWait: 10 }
2208
+ const defaultVacuumConditions = { minDirtFactor: 0.1, minDirtCount: 20 }
2209
+
2210
+ const defaultAutoVacuumOptions = { ...defaultVacuumOptions, ...defaultVacuumConditions }
2211
+
2212
+ const assignUniqueTerm = (target: string[], term: string): void => {
2213
+ // Avoid adding duplicate terms.
2214
+ if (!target.includes(term)) target.push(term)
2215
+ }
2216
+
2217
+ const assignUniqueTerms = (target: string[], source: readonly string[]): void => {
2218
+ for (const term of source) {
2219
+ // Avoid adding duplicate terms.
2220
+ if (!target.includes(term)) target.push(term)
2221
+ }
2222
+ }
2223
+
2224
+ type Scored = { score: number }
2225
+ const byScore = ({ score: a }: Scored, { score: b }: Scored) => b - a
2226
+
2227
+ const createMap = () => new Map()
2228
+
2229
+ interface SerializedIndexEntry {
2230
+ [key: string]: number
2231
+ }
2232
+
2233
+ const objectToNumericMap = <T>(object: { [key: string]: T }): Map<number, T> => {
2234
+ const map = new Map()
2235
+
2236
+ for (const key of Object.keys(object)) {
2237
+ map.set(parseInt(key, 10), object[key])
2238
+ }
2239
+
2240
+ return map
2241
+ }
2242
+
2243
+ const objectToNumericMapAsync = async <T>(object: { [key: string]: T }): Promise<Map<number, T>> => {
2244
+ const map = new Map()
2245
+
2246
+ let count = 0
2247
+ for (const key of Object.keys(object)) {
2248
+ map.set(parseInt(key, 10), object[key])
2249
+ if (++count % 1000 === 0) {
2250
+ await wait(0)
2251
+ }
2252
+ }
2253
+
2254
+ return map
2255
+ }
2256
+
2257
+ const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
2258
+
2259
+ // This regular expression matches any Unicode space, newline, or punctuation
2260
+ // character
2261
+ const SPACE_OR_PUNCTUATION = /[\n\r\p{Z}\p{P}]+/u