rag-lite-ts 2.1.1 → 2.2.0

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 (309) hide show
  1. package/dist/{core → cjs/core}/model-validator.js +1 -1
  2. package/dist/{core → cjs/core}/vector-index.js +4 -2
  3. package/dist/esm/api-errors.d.ts +90 -0
  4. package/dist/esm/api-errors.js +320 -0
  5. package/dist/esm/cli/indexer.d.ts +11 -0
  6. package/dist/esm/cli/indexer.js +471 -0
  7. package/dist/esm/cli/search.d.ts +7 -0
  8. package/dist/esm/cli/search.js +332 -0
  9. package/dist/esm/cli.d.ts +3 -0
  10. package/dist/esm/cli.js +529 -0
  11. package/dist/esm/config.d.ts +51 -0
  12. package/dist/esm/config.js +79 -0
  13. package/dist/esm/core/abstract-embedder.d.ts +125 -0
  14. package/dist/esm/core/abstract-embedder.js +264 -0
  15. package/dist/esm/core/actionable-error-messages.d.ts +60 -0
  16. package/dist/esm/core/actionable-error-messages.js +397 -0
  17. package/dist/esm/core/adapters.d.ts +93 -0
  18. package/dist/esm/core/adapters.js +139 -0
  19. package/dist/esm/core/batch-processing-optimizer.d.ts +155 -0
  20. package/dist/esm/core/batch-processing-optimizer.js +536 -0
  21. package/dist/esm/core/binary-index-format.d.ts +78 -0
  22. package/dist/esm/core/binary-index-format.js +291 -0
  23. package/dist/esm/core/chunker.d.ts +119 -0
  24. package/dist/esm/core/chunker.js +73 -0
  25. package/dist/esm/core/cli-database-utils.d.ts +53 -0
  26. package/dist/esm/core/cli-database-utils.js +239 -0
  27. package/dist/esm/core/config.d.ts +102 -0
  28. package/dist/esm/core/config.js +247 -0
  29. package/dist/esm/core/content-errors.d.ts +111 -0
  30. package/dist/esm/core/content-errors.js +362 -0
  31. package/dist/esm/core/content-manager.d.ts +335 -0
  32. package/dist/esm/core/content-manager.js +1476 -0
  33. package/dist/esm/core/content-performance-optimizer.d.ts +150 -0
  34. package/dist/esm/core/content-performance-optimizer.js +516 -0
  35. package/dist/esm/core/content-resolver.d.ts +104 -0
  36. package/dist/esm/core/content-resolver.js +285 -0
  37. package/dist/esm/core/cross-modal-search.d.ts +164 -0
  38. package/dist/esm/core/cross-modal-search.js +342 -0
  39. package/dist/esm/core/database-connection-manager.d.ts +109 -0
  40. package/dist/esm/core/database-connection-manager.js +310 -0
  41. package/dist/esm/core/db.d.ts +213 -0
  42. package/dist/esm/core/db.js +895 -0
  43. package/dist/esm/core/embedder-factory.d.ts +154 -0
  44. package/dist/esm/core/embedder-factory.js +311 -0
  45. package/dist/esm/core/error-handler.d.ts +112 -0
  46. package/dist/esm/core/error-handler.js +239 -0
  47. package/dist/esm/core/index.d.ts +59 -0
  48. package/dist/esm/core/index.js +69 -0
  49. package/dist/esm/core/ingestion.d.ts +202 -0
  50. package/dist/esm/core/ingestion.js +901 -0
  51. package/dist/esm/core/interfaces.d.ts +408 -0
  52. package/dist/esm/core/interfaces.js +106 -0
  53. package/dist/esm/core/lazy-dependency-loader.d.ts +147 -0
  54. package/dist/esm/core/lazy-dependency-loader.js +435 -0
  55. package/dist/esm/core/mode-detection-service.d.ts +150 -0
  56. package/dist/esm/core/mode-detection-service.js +565 -0
  57. package/dist/esm/core/mode-model-validator.d.ts +92 -0
  58. package/dist/esm/core/mode-model-validator.js +203 -0
  59. package/dist/esm/core/model-registry.d.ts +116 -0
  60. package/dist/esm/core/model-registry.js +411 -0
  61. package/dist/esm/core/model-validator.d.ts +217 -0
  62. package/dist/esm/core/model-validator.js +782 -0
  63. package/dist/esm/core/path-manager.d.ts +47 -0
  64. package/dist/esm/core/path-manager.js +71 -0
  65. package/dist/esm/core/raglite-paths.d.ts +121 -0
  66. package/dist/esm/core/raglite-paths.js +145 -0
  67. package/dist/esm/core/reranking-config.d.ts +42 -0
  68. package/dist/esm/core/reranking-config.js +147 -0
  69. package/dist/esm/core/reranking-factory.d.ts +92 -0
  70. package/dist/esm/core/reranking-factory.js +410 -0
  71. package/dist/esm/core/reranking-strategies.d.ts +310 -0
  72. package/dist/esm/core/reranking-strategies.js +650 -0
  73. package/dist/esm/core/resource-cleanup.d.ts +163 -0
  74. package/dist/esm/core/resource-cleanup.js +371 -0
  75. package/dist/esm/core/resource-manager.d.ts +212 -0
  76. package/dist/esm/core/resource-manager.js +564 -0
  77. package/dist/esm/core/search-pipeline.d.ts +111 -0
  78. package/dist/esm/core/search-pipeline.js +287 -0
  79. package/dist/esm/core/search.d.ts +141 -0
  80. package/dist/esm/core/search.js +320 -0
  81. package/dist/esm/core/streaming-operations.d.ts +145 -0
  82. package/dist/esm/core/streaming-operations.js +409 -0
  83. package/dist/esm/core/types.d.ts +66 -0
  84. package/dist/esm/core/types.js +6 -0
  85. package/dist/esm/core/universal-embedder.d.ts +177 -0
  86. package/dist/esm/core/universal-embedder.js +139 -0
  87. package/dist/esm/core/validation-messages.d.ts +99 -0
  88. package/dist/esm/core/validation-messages.js +334 -0
  89. package/dist/esm/core/vector-index.d.ts +72 -0
  90. package/dist/esm/core/vector-index.js +333 -0
  91. package/dist/esm/dom-polyfills.d.ts +6 -0
  92. package/dist/esm/dom-polyfills.js +37 -0
  93. package/dist/esm/factories/index.d.ts +27 -0
  94. package/dist/esm/factories/index.js +29 -0
  95. package/dist/esm/factories/ingestion-factory.d.ts +200 -0
  96. package/dist/esm/factories/ingestion-factory.js +477 -0
  97. package/dist/esm/factories/search-factory.d.ts +154 -0
  98. package/dist/esm/factories/search-factory.js +344 -0
  99. package/dist/esm/file-processor.d.ts +147 -0
  100. package/dist/esm/file-processor.js +963 -0
  101. package/dist/esm/index-manager.d.ts +116 -0
  102. package/dist/esm/index-manager.js +598 -0
  103. package/dist/esm/index.d.ts +75 -0
  104. package/dist/esm/index.js +110 -0
  105. package/dist/esm/indexer.d.ts +7 -0
  106. package/dist/esm/indexer.js +54 -0
  107. package/dist/esm/ingestion.d.ts +63 -0
  108. package/dist/esm/ingestion.js +124 -0
  109. package/dist/esm/mcp-server.d.ts +46 -0
  110. package/dist/esm/mcp-server.js +1820 -0
  111. package/dist/esm/multimodal/clip-embedder.d.ts +327 -0
  112. package/dist/esm/multimodal/clip-embedder.js +996 -0
  113. package/dist/esm/multimodal/index.d.ts +6 -0
  114. package/dist/esm/multimodal/index.js +6 -0
  115. package/dist/esm/preprocess.d.ts +19 -0
  116. package/dist/esm/preprocess.js +203 -0
  117. package/dist/esm/preprocessors/index.d.ts +17 -0
  118. package/dist/esm/preprocessors/index.js +38 -0
  119. package/dist/esm/preprocessors/mdx.d.ts +25 -0
  120. package/dist/esm/preprocessors/mdx.js +101 -0
  121. package/dist/esm/preprocessors/mermaid.d.ts +68 -0
  122. package/dist/esm/preprocessors/mermaid.js +329 -0
  123. package/dist/esm/preprocessors/registry.d.ts +56 -0
  124. package/dist/esm/preprocessors/registry.js +179 -0
  125. package/dist/esm/run-error-recovery-tests.d.ts +7 -0
  126. package/dist/esm/run-error-recovery-tests.js +101 -0
  127. package/dist/esm/search-standalone.d.ts +7 -0
  128. package/dist/esm/search-standalone.js +117 -0
  129. package/dist/esm/search.d.ts +99 -0
  130. package/dist/esm/search.js +177 -0
  131. package/dist/esm/test-utils.d.ts +18 -0
  132. package/dist/esm/test-utils.js +27 -0
  133. package/dist/esm/text/chunker.d.ts +33 -0
  134. package/dist/esm/text/chunker.js +279 -0
  135. package/dist/esm/text/embedder.d.ts +111 -0
  136. package/dist/esm/text/embedder.js +386 -0
  137. package/dist/esm/text/index.d.ts +8 -0
  138. package/dist/esm/text/index.js +9 -0
  139. package/dist/esm/text/preprocessors/index.d.ts +17 -0
  140. package/dist/esm/text/preprocessors/index.js +38 -0
  141. package/dist/esm/text/preprocessors/mdx.d.ts +25 -0
  142. package/dist/esm/text/preprocessors/mdx.js +101 -0
  143. package/dist/esm/text/preprocessors/mermaid.d.ts +68 -0
  144. package/dist/esm/text/preprocessors/mermaid.js +330 -0
  145. package/dist/esm/text/preprocessors/registry.d.ts +56 -0
  146. package/dist/esm/text/preprocessors/registry.js +180 -0
  147. package/dist/esm/text/reranker.d.ts +49 -0
  148. package/dist/esm/text/reranker.js +274 -0
  149. package/dist/esm/text/sentence-transformer-embedder.d.ts +96 -0
  150. package/dist/esm/text/sentence-transformer-embedder.js +340 -0
  151. package/dist/esm/text/tokenizer.d.ts +22 -0
  152. package/dist/esm/text/tokenizer.js +64 -0
  153. package/dist/esm/types.d.ts +83 -0
  154. package/dist/esm/types.js +3 -0
  155. package/dist/esm/utils/vector-math.d.ts +31 -0
  156. package/dist/esm/utils/vector-math.js +70 -0
  157. package/package.json +30 -12
  158. /package/dist/{api-errors.d.ts → cjs/api-errors.d.ts} +0 -0
  159. /package/dist/{api-errors.js → cjs/api-errors.js} +0 -0
  160. /package/dist/{cli → cjs/cli}/indexer.d.ts +0 -0
  161. /package/dist/{cli → cjs/cli}/indexer.js +0 -0
  162. /package/dist/{cli → cjs/cli}/search.d.ts +0 -0
  163. /package/dist/{cli → cjs/cli}/search.js +0 -0
  164. /package/dist/{cli.d.ts → cjs/cli.d.ts} +0 -0
  165. /package/dist/{cli.js → cjs/cli.js} +0 -0
  166. /package/dist/{config.d.ts → cjs/config.d.ts} +0 -0
  167. /package/dist/{config.js → cjs/config.js} +0 -0
  168. /package/dist/{core → cjs/core}/abstract-embedder.d.ts +0 -0
  169. /package/dist/{core → cjs/core}/abstract-embedder.js +0 -0
  170. /package/dist/{core → cjs/core}/actionable-error-messages.d.ts +0 -0
  171. /package/dist/{core → cjs/core}/actionable-error-messages.js +0 -0
  172. /package/dist/{core → cjs/core}/adapters.d.ts +0 -0
  173. /package/dist/{core → cjs/core}/adapters.js +0 -0
  174. /package/dist/{core → cjs/core}/batch-processing-optimizer.d.ts +0 -0
  175. /package/dist/{core → cjs/core}/batch-processing-optimizer.js +0 -0
  176. /package/dist/{core → cjs/core}/binary-index-format.d.ts +0 -0
  177. /package/dist/{core → cjs/core}/binary-index-format.js +0 -0
  178. /package/dist/{core → cjs/core}/chunker.d.ts +0 -0
  179. /package/dist/{core → cjs/core}/chunker.js +0 -0
  180. /package/dist/{core → cjs/core}/cli-database-utils.d.ts +0 -0
  181. /package/dist/{core → cjs/core}/cli-database-utils.js +0 -0
  182. /package/dist/{core → cjs/core}/config.d.ts +0 -0
  183. /package/dist/{core → cjs/core}/config.js +0 -0
  184. /package/dist/{core → cjs/core}/content-errors.d.ts +0 -0
  185. /package/dist/{core → cjs/core}/content-errors.js +0 -0
  186. /package/dist/{core → cjs/core}/content-manager.d.ts +0 -0
  187. /package/dist/{core → cjs/core}/content-manager.js +0 -0
  188. /package/dist/{core → cjs/core}/content-performance-optimizer.d.ts +0 -0
  189. /package/dist/{core → cjs/core}/content-performance-optimizer.js +0 -0
  190. /package/dist/{core → cjs/core}/content-resolver.d.ts +0 -0
  191. /package/dist/{core → cjs/core}/content-resolver.js +0 -0
  192. /package/dist/{core → cjs/core}/cross-modal-search.d.ts +0 -0
  193. /package/dist/{core → cjs/core}/cross-modal-search.js +0 -0
  194. /package/dist/{core → cjs/core}/database-connection-manager.d.ts +0 -0
  195. /package/dist/{core → cjs/core}/database-connection-manager.js +0 -0
  196. /package/dist/{core → cjs/core}/db.d.ts +0 -0
  197. /package/dist/{core → cjs/core}/db.js +0 -0
  198. /package/dist/{core → cjs/core}/embedder-factory.d.ts +0 -0
  199. /package/dist/{core → cjs/core}/embedder-factory.js +0 -0
  200. /package/dist/{core → cjs/core}/error-handler.d.ts +0 -0
  201. /package/dist/{core → cjs/core}/error-handler.js +0 -0
  202. /package/dist/{core → cjs/core}/index.d.ts +0 -0
  203. /package/dist/{core → cjs/core}/index.js +0 -0
  204. /package/dist/{core → cjs/core}/ingestion.d.ts +0 -0
  205. /package/dist/{core → cjs/core}/ingestion.js +0 -0
  206. /package/dist/{core → cjs/core}/interfaces.d.ts +0 -0
  207. /package/dist/{core → cjs/core}/interfaces.js +0 -0
  208. /package/dist/{core → cjs/core}/lazy-dependency-loader.d.ts +0 -0
  209. /package/dist/{core → cjs/core}/lazy-dependency-loader.js +0 -0
  210. /package/dist/{core → cjs/core}/mode-detection-service.d.ts +0 -0
  211. /package/dist/{core → cjs/core}/mode-detection-service.js +0 -0
  212. /package/dist/{core → cjs/core}/mode-model-validator.d.ts +0 -0
  213. /package/dist/{core → cjs/core}/mode-model-validator.js +0 -0
  214. /package/dist/{core → cjs/core}/model-registry.d.ts +0 -0
  215. /package/dist/{core → cjs/core}/model-registry.js +0 -0
  216. /package/dist/{core → cjs/core}/model-validator.d.ts +0 -0
  217. /package/dist/{core → cjs/core}/path-manager.d.ts +0 -0
  218. /package/dist/{core → cjs/core}/path-manager.js +0 -0
  219. /package/dist/{core → cjs/core}/raglite-paths.d.ts +0 -0
  220. /package/dist/{core → cjs/core}/raglite-paths.js +0 -0
  221. /package/dist/{core → cjs/core}/reranking-config.d.ts +0 -0
  222. /package/dist/{core → cjs/core}/reranking-config.js +0 -0
  223. /package/dist/{core → cjs/core}/reranking-factory.d.ts +0 -0
  224. /package/dist/{core → cjs/core}/reranking-factory.js +0 -0
  225. /package/dist/{core → cjs/core}/reranking-strategies.d.ts +0 -0
  226. /package/dist/{core → cjs/core}/reranking-strategies.js +0 -0
  227. /package/dist/{core → cjs/core}/resource-cleanup.d.ts +0 -0
  228. /package/dist/{core → cjs/core}/resource-cleanup.js +0 -0
  229. /package/dist/{core → cjs/core}/resource-manager.d.ts +0 -0
  230. /package/dist/{core → cjs/core}/resource-manager.js +0 -0
  231. /package/dist/{core → cjs/core}/search-pipeline.d.ts +0 -0
  232. /package/dist/{core → cjs/core}/search-pipeline.js +0 -0
  233. /package/dist/{core → cjs/core}/search.d.ts +0 -0
  234. /package/dist/{core → cjs/core}/search.js +0 -0
  235. /package/dist/{core → cjs/core}/streaming-operations.d.ts +0 -0
  236. /package/dist/{core → cjs/core}/streaming-operations.js +0 -0
  237. /package/dist/{core → cjs/core}/types.d.ts +0 -0
  238. /package/dist/{core → cjs/core}/types.js +0 -0
  239. /package/dist/{core → cjs/core}/universal-embedder.d.ts +0 -0
  240. /package/dist/{core → cjs/core}/universal-embedder.js +0 -0
  241. /package/dist/{core → cjs/core}/validation-messages.d.ts +0 -0
  242. /package/dist/{core → cjs/core}/validation-messages.js +0 -0
  243. /package/dist/{core → cjs/core}/vector-index.d.ts +0 -0
  244. /package/dist/{dom-polyfills.d.ts → cjs/dom-polyfills.d.ts} +0 -0
  245. /package/dist/{dom-polyfills.js → cjs/dom-polyfills.js} +0 -0
  246. /package/dist/{factories → cjs/factories}/index.d.ts +0 -0
  247. /package/dist/{factories → cjs/factories}/index.js +0 -0
  248. /package/dist/{factories → cjs/factories}/ingestion-factory.d.ts +0 -0
  249. /package/dist/{factories → cjs/factories}/ingestion-factory.js +0 -0
  250. /package/dist/{factories → cjs/factories}/search-factory.d.ts +0 -0
  251. /package/dist/{factories → cjs/factories}/search-factory.js +0 -0
  252. /package/dist/{file-processor.d.ts → cjs/file-processor.d.ts} +0 -0
  253. /package/dist/{file-processor.js → cjs/file-processor.js} +0 -0
  254. /package/dist/{index-manager.d.ts → cjs/index-manager.d.ts} +0 -0
  255. /package/dist/{index-manager.js → cjs/index-manager.js} +0 -0
  256. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  257. /package/dist/{index.js → cjs/index.js} +0 -0
  258. /package/dist/{indexer.d.ts → cjs/indexer.d.ts} +0 -0
  259. /package/dist/{indexer.js → cjs/indexer.js} +0 -0
  260. /package/dist/{ingestion.d.ts → cjs/ingestion.d.ts} +0 -0
  261. /package/dist/{ingestion.js → cjs/ingestion.js} +0 -0
  262. /package/dist/{mcp-server.d.ts → cjs/mcp-server.d.ts} +0 -0
  263. /package/dist/{mcp-server.js → cjs/mcp-server.js} +0 -0
  264. /package/dist/{multimodal → cjs/multimodal}/clip-embedder.d.ts +0 -0
  265. /package/dist/{multimodal → cjs/multimodal}/clip-embedder.js +0 -0
  266. /package/dist/{multimodal → cjs/multimodal}/index.d.ts +0 -0
  267. /package/dist/{multimodal → cjs/multimodal}/index.js +0 -0
  268. /package/dist/{preprocess.d.ts → cjs/preprocess.d.ts} +0 -0
  269. /package/dist/{preprocess.js → cjs/preprocess.js} +0 -0
  270. /package/dist/{preprocessors → cjs/preprocessors}/index.d.ts +0 -0
  271. /package/dist/{preprocessors → cjs/preprocessors}/index.js +0 -0
  272. /package/dist/{preprocessors → cjs/preprocessors}/mdx.d.ts +0 -0
  273. /package/dist/{preprocessors → cjs/preprocessors}/mdx.js +0 -0
  274. /package/dist/{preprocessors → cjs/preprocessors}/mermaid.d.ts +0 -0
  275. /package/dist/{preprocessors → cjs/preprocessors}/mermaid.js +0 -0
  276. /package/dist/{preprocessors → cjs/preprocessors}/registry.d.ts +0 -0
  277. /package/dist/{preprocessors → cjs/preprocessors}/registry.js +0 -0
  278. /package/dist/{run-error-recovery-tests.d.ts → cjs/run-error-recovery-tests.d.ts} +0 -0
  279. /package/dist/{run-error-recovery-tests.js → cjs/run-error-recovery-tests.js} +0 -0
  280. /package/dist/{search-standalone.d.ts → cjs/search-standalone.d.ts} +0 -0
  281. /package/dist/{search-standalone.js → cjs/search-standalone.js} +0 -0
  282. /package/dist/{search.d.ts → cjs/search.d.ts} +0 -0
  283. /package/dist/{search.js → cjs/search.js} +0 -0
  284. /package/dist/{test-utils.d.ts → cjs/test-utils.d.ts} +0 -0
  285. /package/dist/{test-utils.js → cjs/test-utils.js} +0 -0
  286. /package/dist/{text → cjs/text}/chunker.d.ts +0 -0
  287. /package/dist/{text → cjs/text}/chunker.js +0 -0
  288. /package/dist/{text → cjs/text}/embedder.d.ts +0 -0
  289. /package/dist/{text → cjs/text}/embedder.js +0 -0
  290. /package/dist/{text → cjs/text}/index.d.ts +0 -0
  291. /package/dist/{text → cjs/text}/index.js +0 -0
  292. /package/dist/{text → cjs/text}/preprocessors/index.d.ts +0 -0
  293. /package/dist/{text → cjs/text}/preprocessors/index.js +0 -0
  294. /package/dist/{text → cjs/text}/preprocessors/mdx.d.ts +0 -0
  295. /package/dist/{text → cjs/text}/preprocessors/mdx.js +0 -0
  296. /package/dist/{text → cjs/text}/preprocessors/mermaid.d.ts +0 -0
  297. /package/dist/{text → cjs/text}/preprocessors/mermaid.js +0 -0
  298. /package/dist/{text → cjs/text}/preprocessors/registry.d.ts +0 -0
  299. /package/dist/{text → cjs/text}/preprocessors/registry.js +0 -0
  300. /package/dist/{text → cjs/text}/reranker.d.ts +0 -0
  301. /package/dist/{text → cjs/text}/reranker.js +0 -0
  302. /package/dist/{text → cjs/text}/sentence-transformer-embedder.d.ts +0 -0
  303. /package/dist/{text → cjs/text}/sentence-transformer-embedder.js +0 -0
  304. /package/dist/{text → cjs/text}/tokenizer.d.ts +0 -0
  305. /package/dist/{text → cjs/text}/tokenizer.js +0 -0
  306. /package/dist/{types.d.ts → cjs/types.d.ts} +0 -0
  307. /package/dist/{types.js → cjs/types.js} +0 -0
  308. /package/dist/{utils → cjs/utils}/vector-math.d.ts +0 -0
  309. /package/dist/{utils → cjs/utils}/vector-math.js +0 -0
@@ -0,0 +1,310 @@
1
+ /**
2
+ * Database Connection Manager - Centralized connection handling
3
+ * Prevents database locking issues by managing shared connections
4
+ * Addresses production issues with MCP server + CLI concurrent usage
5
+ */
6
+ import { openDatabase } from './db.js';
7
+ import { resolve as pathResolve } from 'node:path';
8
+ /**
9
+ * Database Connection Manager
10
+ * Manages shared database connections to prevent locking issues
11
+ */
12
+ export class DatabaseConnectionManager {
13
+ static connections = new Map();
14
+ static cleanupInterval = null;
15
+ static CLEANUP_INTERVAL = 30000; // 30 seconds
16
+ static MAX_IDLE_TIME = 60000; // 1 minute
17
+ /**
18
+ * Get a shared database connection
19
+ * Creates new connection if none exists, otherwise returns existing
20
+ */
21
+ static async getConnection(dbPath) {
22
+ const normalizedPath = this.normalizePath(dbPath);
23
+ let connectionInfo = this.connections.get(normalizedPath);
24
+ // Check if cached connection exists but database file was deleted
25
+ if (connectionInfo && !connectionInfo.isClosing) {
26
+ const { existsSync } = await import('fs');
27
+ if (!existsSync(normalizedPath)) {
28
+ // Database file was deleted - invalidate cached connection
29
+ console.log(`🔄 Database file deleted, invalidating cached connection: ${normalizedPath}`);
30
+ await this.forceCloseConnection(normalizedPath);
31
+ connectionInfo = undefined; // Force creation of new connection
32
+ }
33
+ }
34
+ if (!connectionInfo || connectionInfo.isClosing) {
35
+ // Create new connection
36
+ const connection = await openDatabase(dbPath);
37
+ connectionInfo = {
38
+ connection,
39
+ refCount: 1,
40
+ lastAccessed: Date.now(),
41
+ isClosing: false
42
+ };
43
+ this.connections.set(normalizedPath, connectionInfo);
44
+ // Start cleanup timer if this is the first connection
45
+ if (this.connections.size === 1 && !this.cleanupInterval) {
46
+ this.startCleanupTimer();
47
+ }
48
+ console.log(`📊 Database connection created: ${normalizedPath} (total: ${this.connections.size})`);
49
+ }
50
+ else {
51
+ // Reuse existing connection
52
+ connectionInfo.refCount++;
53
+ connectionInfo.lastAccessed = Date.now();
54
+ console.log(`🔄 Database connection reused: ${normalizedPath} (refs: ${connectionInfo.refCount})`);
55
+ }
56
+ return connectionInfo.connection;
57
+ }
58
+ /**
59
+ * Release a database connection reference
60
+ * Connection is kept alive for potential reuse
61
+ */
62
+ static async releaseConnection(dbPath) {
63
+ const normalizedPath = this.normalizePath(dbPath);
64
+ const connectionInfo = this.connections.get(normalizedPath);
65
+ if (connectionInfo && !connectionInfo.isClosing) {
66
+ connectionInfo.refCount = Math.max(0, connectionInfo.refCount - 1);
67
+ connectionInfo.lastAccessed = Date.now();
68
+ console.log(`📉 Database connection released: ${normalizedPath} (refs: ${connectionInfo.refCount})`);
69
+ // Don't immediately close - let cleanup timer handle it
70
+ // This prevents rapid open/close cycles
71
+ }
72
+ }
73
+ /**
74
+ * Force close a specific database connection
75
+ * Use with caution - only for cleanup or error recovery
76
+ */
77
+ static async forceCloseConnection(dbPath) {
78
+ const normalizedPath = this.normalizePath(dbPath);
79
+ const connectionInfo = this.connections.get(normalizedPath);
80
+ if (connectionInfo && !connectionInfo.isClosing) {
81
+ connectionInfo.isClosing = true;
82
+ try {
83
+ await connectionInfo.connection.close();
84
+ console.log(`🔒 Database connection force closed: ${normalizedPath}`);
85
+ }
86
+ catch (error) {
87
+ console.warn(`⚠️ Warning: Error force closing connection ${normalizedPath}:`, error);
88
+ }
89
+ finally {
90
+ this.connections.delete(normalizedPath);
91
+ }
92
+ }
93
+ }
94
+ /**
95
+ * Close all database connections
96
+ * Used during application shutdown
97
+ */
98
+ static async closeAllConnections() {
99
+ if (this.connections.size === 0) {
100
+ return; // Nothing to close
101
+ }
102
+ const isCLI = process.env.RAG_CLI_MODE === 'true';
103
+ if (!isCLI) {
104
+ console.log(`🧹 Closing all database connections (${this.connections.size} active)`);
105
+ }
106
+ const closePromises = [];
107
+ for (const [path, connectionInfo] of this.connections.entries()) {
108
+ if (!connectionInfo.isClosing) {
109
+ connectionInfo.isClosing = true;
110
+ closePromises.push(connectionInfo.connection.close()
111
+ .then(() => {
112
+ if (!isCLI) {
113
+ console.log(`✅ Closed connection: ${path}`);
114
+ }
115
+ })
116
+ .catch(error => {
117
+ if (!isCLI) {
118
+ console.warn(`⚠️ Error closing connection ${path}:`, error);
119
+ }
120
+ }));
121
+ }
122
+ }
123
+ await Promise.all(closePromises);
124
+ this.connections.clear();
125
+ if (this.cleanupInterval) {
126
+ clearInterval(this.cleanupInterval);
127
+ this.cleanupInterval = null;
128
+ }
129
+ if (!isCLI) {
130
+ console.log('✅ All database connections closed');
131
+ }
132
+ }
133
+ /**
134
+ * Get connection statistics for monitoring
135
+ */
136
+ static getConnectionStats() {
137
+ const now = Date.now();
138
+ const connections = Array.from(this.connections.entries()).map(([path, info]) => ({
139
+ path,
140
+ refCount: info.refCount,
141
+ lastAccessed: new Date(info.lastAccessed),
142
+ idleTime: now - info.lastAccessed
143
+ }));
144
+ return {
145
+ totalConnections: this.connections.size,
146
+ connections
147
+ };
148
+ }
149
+ /**
150
+ * Check if a connection exists for a given path
151
+ */
152
+ static hasConnection(dbPath) {
153
+ const normalizedPath = this.normalizePath(dbPath);
154
+ const connectionInfo = this.connections.get(normalizedPath);
155
+ return connectionInfo !== undefined && !connectionInfo.isClosing;
156
+ }
157
+ /**
158
+ * Wait for database to become available
159
+ * Useful for handling temporary locking issues
160
+ */
161
+ static async waitForDatabaseAccess(dbPath, maxWaitMs = 5000) {
162
+ const startTime = Date.now();
163
+ while (Date.now() - startTime < maxWaitMs) {
164
+ try {
165
+ // Try to get a connection
166
+ const connection = await this.getConnection(dbPath);
167
+ await this.releaseConnection(dbPath);
168
+ return; // Success
169
+ }
170
+ catch (error) {
171
+ if (error instanceof Error && error.message.includes('SQLITE_BUSY')) {
172
+ // Wait a bit and retry
173
+ await new Promise(resolve => setTimeout(resolve, 100));
174
+ continue;
175
+ }
176
+ // Other errors should be thrown immediately
177
+ throw error;
178
+ }
179
+ }
180
+ throw new Error(`Database ${dbPath} is busy after ${maxWaitMs}ms. Please try again later.`);
181
+ }
182
+ // =============================================================================
183
+ // PRIVATE METHODS
184
+ // =============================================================================
185
+ /**
186
+ * Normalize database path for consistent key usage
187
+ */
188
+ static normalizePath(dbPath) {
189
+ // Convert to absolute path and normalize separators
190
+ // Use Node.js path.resolve for proper relative path handling
191
+ const absolutePath = pathResolve(dbPath);
192
+ // Normalize separators for cross-platform consistency
193
+ return absolutePath.replace(/\\/g, '/');
194
+ }
195
+ /**
196
+ * Start the cleanup timer for idle connections
197
+ * Only start if not in CLI mode to prevent hanging
198
+ */
199
+ static startCleanupTimer() {
200
+ // Don't start cleanup timer for CLI commands - they should exit quickly
201
+ const isCLI = process.argv.some(arg => arg.includes('cli.js') ||
202
+ arg.includes('raglite') ||
203
+ process.env.RAG_CLI_MODE === 'true');
204
+ if (!isCLI) {
205
+ this.cleanupInterval = setInterval(() => {
206
+ this.cleanupIdleConnections();
207
+ }, this.CLEANUP_INTERVAL);
208
+ }
209
+ }
210
+ /**
211
+ * Clean up idle connections that haven't been used recently
212
+ */
213
+ static async cleanupIdleConnections() {
214
+ const now = Date.now();
215
+ const connectionsToClose = [];
216
+ for (const [path, connectionInfo] of this.connections.entries()) {
217
+ const idleTime = now - connectionInfo.lastAccessed;
218
+ // Close connections that are idle and have no active references
219
+ if (connectionInfo.refCount === 0 && idleTime > this.MAX_IDLE_TIME && !connectionInfo.isClosing) {
220
+ connectionsToClose.push(path);
221
+ }
222
+ }
223
+ if (connectionsToClose.length > 0) {
224
+ console.log(`🧹 Cleaning up ${connectionsToClose.length} idle database connections`);
225
+ for (const path of connectionsToClose) {
226
+ await this.forceCloseConnection(path);
227
+ }
228
+ }
229
+ // Stop cleanup timer if no connections remain
230
+ if (this.connections.size === 0 && this.cleanupInterval) {
231
+ clearInterval(this.cleanupInterval);
232
+ this.cleanupInterval = null;
233
+ }
234
+ }
235
+ }
236
+ /**
237
+ * Convenience function to get a managed database connection
238
+ * Use this instead of openDatabase() directly
239
+ */
240
+ export async function getManagedConnection(dbPath) {
241
+ return DatabaseConnectionManager.getConnection(dbPath);
242
+ }
243
+ /**
244
+ * Convenience function to release a managed database connection
245
+ * Use this instead of connection.close() directly
246
+ */
247
+ export async function releaseManagedConnection(dbPath) {
248
+ return DatabaseConnectionManager.releaseConnection(dbPath);
249
+ }
250
+ /**
251
+ * Enhanced database connection wrapper
252
+ * Automatically manages connection lifecycle
253
+ */
254
+ export class ManagedDatabaseConnection {
255
+ dbPath;
256
+ connection = null;
257
+ constructor(dbPath) {
258
+ this.dbPath = dbPath;
259
+ }
260
+ /**
261
+ * Get the underlying database connection
262
+ * Automatically acquires managed connection on first use
263
+ */
264
+ async getConnection() {
265
+ if (!this.connection) {
266
+ this.connection = await DatabaseConnectionManager.getConnection(this.dbPath);
267
+ }
268
+ return this.connection;
269
+ }
270
+ /**
271
+ * Execute a database operation with automatic connection management
272
+ */
273
+ async execute(operation) {
274
+ const connection = await this.getConnection();
275
+ return operation(connection);
276
+ }
277
+ /**
278
+ * Release the managed connection
279
+ * Connection may be kept alive for reuse by other components
280
+ */
281
+ async release() {
282
+ if (this.connection) {
283
+ await DatabaseConnectionManager.releaseConnection(this.dbPath);
284
+ this.connection = null;
285
+ }
286
+ }
287
+ /**
288
+ * Force close the connection
289
+ * Use only during cleanup or error recovery
290
+ */
291
+ async forceClose() {
292
+ if (this.connection) {
293
+ await DatabaseConnectionManager.forceCloseConnection(this.dbPath);
294
+ this.connection = null;
295
+ }
296
+ }
297
+ }
298
+ // =============================================================================
299
+ // PROCESS CLEANUP HANDLERS
300
+ // =============================================================================
301
+ /**
302
+ * Ensure all connections are closed on process exit
303
+ */
304
+ process.on('exit', () => {
305
+ // Synchronous cleanup only
306
+ console.log('🔄 Process exiting, cleaning up database connections...');
307
+ });
308
+ // Don't set up global process handlers - let applications handle their own cleanup
309
+ // This prevents CLI commands from hanging due to active event listeners
310
+ //# sourceMappingURL=database-connection-manager.js.map
@@ -0,0 +1,213 @@
1
+ /**
2
+ * CORE MODULE — Shared between text-only (rag-lite-ts) and future multimodal (rag-lite-mm)
3
+ * Model-agnostic. No transformer or modality-specific logic.
4
+ */
5
+ import sqlite3 from 'sqlite3';
6
+ import type { SystemInfo } from '../types.js';
7
+ export interface DatabaseConnection {
8
+ db: sqlite3.Database;
9
+ run: (sql: string, params?: any[]) => Promise<sqlite3.RunResult>;
10
+ get: (sql: string, params?: any[]) => Promise<any>;
11
+ all: (sql: string, params?: any[]) => Promise<any[]>;
12
+ close: () => Promise<void>;
13
+ }
14
+ export interface ContentChunk {
15
+ id: number;
16
+ embedding_id: string;
17
+ document_id: number;
18
+ content: string;
19
+ content_type: string;
20
+ chunk_index: number;
21
+ metadata?: Record<string, any>;
22
+ created_at: string;
23
+ document_source: string;
24
+ document_title: string;
25
+ document_content_type: string;
26
+ document_content_id?: string;
27
+ }
28
+ /**
29
+ * Opens a SQLite database connection with promisified methods
30
+ * @param dbPath - Path to the SQLite database file
31
+ * @returns Promise that resolves to a database connection object
32
+ */
33
+ export declare function openDatabase(dbPath: string): Promise<DatabaseConnection>;
34
+ /**
35
+ * Initializes the database schema with all required tables and indexes
36
+ * Enhanced to support content types for multimodal use
37
+ * @param connection - Database connection object
38
+ */
39
+ export declare function initializeSchema(connection: DatabaseConnection): Promise<void>;
40
+ /**
41
+ * Inserts a new document into the database with content type support
42
+ * @param connection - Database connection object
43
+ * @param source - Source path of the document
44
+ * @param title - Title of the document
45
+ * @param contentType - Type of content ('text', 'image', etc.)
46
+ * @param metadata - Optional metadata object
47
+ * @param contentId - Optional content ID referencing content_metadata table
48
+ * @returns Promise that resolves to the document ID
49
+ */
50
+ export declare function insertDocument(connection: DatabaseConnection, source: string, title: string, contentType?: string, metadata?: Record<string, any>, contentId?: string): Promise<number>;
51
+ /**
52
+ * Inserts or updates a chunk in the database with content type support (upsert operation)
53
+ * @param connection - Database connection object
54
+ * @param embeddingId - Unique embedding ID for the chunk
55
+ * @param documentId - ID of the parent document
56
+ * @param content - Content of the chunk (text, image path, etc.)
57
+ * @param chunkIndex - Index of the chunk within the document
58
+ * @param contentType - Type of content ('text', 'image', etc.)
59
+ * @param metadata - Optional metadata object
60
+ */
61
+ export declare function insertChunk(connection: DatabaseConnection, embeddingId: string, documentId: number, content: string, chunkIndex: number, contentType?: string, metadata?: Record<string, any>): Promise<void>;
62
+ /**
63
+ * Inserts a new document or returns existing document ID if it already exists
64
+ * Enhanced with content type support
65
+ * @param connection - Database connection object
66
+ * @param source - Source path of the document
67
+ * @param title - Title of the document
68
+ * @param contentType - Type of content ('text', 'image', etc.)
69
+ * @param metadata - Optional metadata object
70
+ * @param contentId - Optional content ID referencing content_metadata table
71
+ * @returns Promise that resolves to the document ID
72
+ */
73
+ export declare function upsertDocument(connection: DatabaseConnection, source: string, title: string, contentType?: string, metadata?: Record<string, any>, contentId?: string): Promise<number>;
74
+ /**
75
+ * Retrieves chunks by their embedding IDs with document metadata
76
+ * Enhanced to include content type information
77
+ * @param connection - Database connection object
78
+ * @param embeddingIds - Array of embedding IDs to retrieve
79
+ * @returns Promise that resolves to an array of chunk results with document metadata
80
+ */
81
+ export declare function getChunksByEmbeddingIds(connection: DatabaseConnection, embeddingIds: string[]): Promise<ContentChunk[]>;
82
+ /**
83
+ * Gets the complete system information from system_info table
84
+ * @param connection - Database connection object
85
+ * @returns Promise that resolves to SystemInfo object or null if not set
86
+ */
87
+ export declare function getSystemInfo(connection: DatabaseConnection): Promise<SystemInfo | null>;
88
+ /**
89
+ * Sets the complete system information in system_info table
90
+ * @param connection - Database connection object
91
+ * @param systemInfo - SystemInfo object to store
92
+ */
93
+ export declare function setSystemInfo(connection: DatabaseConnection, systemInfo: Partial<SystemInfo>): Promise<void>;
94
+ /**
95
+ * Retrieves documents by content type
96
+ * @param connection - Database connection object
97
+ * @param contentType - Content type to filter by
98
+ * @returns Promise that resolves to an array of documents
99
+ */
100
+ export declare function getDocumentsByContentType(connection: DatabaseConnection, contentType: string): Promise<Array<{
101
+ id: number;
102
+ source: string;
103
+ title: string;
104
+ content_type: string;
105
+ metadata?: Record<string, any>;
106
+ created_at: string;
107
+ }>>;
108
+ /**
109
+ * Retrieves chunks by content type
110
+ * @param connection - Database connection object
111
+ * @param contentType - Content type to filter by
112
+ * @returns Promise that resolves to an array of chunks with document metadata
113
+ */
114
+ export declare function getChunksByContentType(connection: DatabaseConnection, contentType: string): Promise<ContentChunk[]>;
115
+ /**
116
+ * Gets content type statistics from the database
117
+ * @param connection - Database connection object
118
+ * @returns Promise that resolves to content type statistics
119
+ */
120
+ export declare function getContentTypeStatistics(connection: DatabaseConnection): Promise<{
121
+ documents: Record<string, number>;
122
+ chunks: Record<string, number>;
123
+ total: {
124
+ documents: number;
125
+ chunks: number;
126
+ };
127
+ }>;
128
+ /**
129
+ * Updates document metadata
130
+ * @param connection - Database connection object
131
+ * @param documentId - ID of the document to update
132
+ * @param metadata - New metadata object
133
+ */
134
+ export declare function updateDocumentMetadata(connection: DatabaseConnection, documentId: number, metadata: Record<string, any>): Promise<void>;
135
+ /**
136
+ * Updates chunk metadata
137
+ * @param connection - Database connection object
138
+ * @param chunkId - ID of the chunk to update
139
+ * @param metadata - New metadata object
140
+ */
141
+ export declare function updateChunkMetadata(connection: DatabaseConnection, chunkId: number, metadata: Record<string, any>): Promise<void>;
142
+ /**
143
+ * Content metadata interface for unified content system
144
+ */
145
+ export interface ContentMetadata {
146
+ id: string;
147
+ storageType: 'filesystem' | 'content_dir';
148
+ originalPath?: string;
149
+ contentPath: string;
150
+ displayName: string;
151
+ contentType: string;
152
+ fileSize: number;
153
+ contentHash: string;
154
+ createdAt: Date;
155
+ }
156
+ /**
157
+ * Inserts content metadata into the content_metadata table
158
+ * @param connection - Database connection object
159
+ * @param contentMetadata - Content metadata to insert
160
+ */
161
+ export declare function insertContentMetadata(connection: DatabaseConnection, contentMetadata: Omit<ContentMetadata, 'createdAt'>): Promise<void>;
162
+ /**
163
+ * Gets content metadata by content ID
164
+ * @param connection - Database connection object
165
+ * @param contentId - Content ID to retrieve
166
+ * @returns Promise that resolves to ContentMetadata or null if not found
167
+ */
168
+ export declare function getContentMetadata(connection: DatabaseConnection, contentId: string): Promise<ContentMetadata | null>;
169
+ /**
170
+ * Gets content metadata by content hash (for deduplication)
171
+ * @param connection - Database connection object
172
+ * @param contentHash - Content hash to search for
173
+ * @returns Promise that resolves to ContentMetadata or null if not found
174
+ */
175
+ export declare function getContentMetadataByHash(connection: DatabaseConnection, contentHash: string): Promise<ContentMetadata | null>;
176
+ /**
177
+ * Gets all content metadata by storage type
178
+ * @param connection - Database connection object
179
+ * @param storageType - Storage type to filter by
180
+ * @returns Promise that resolves to array of ContentMetadata
181
+ */
182
+ export declare function getContentMetadataByStorageType(connection: DatabaseConnection, storageType: 'filesystem' | 'content_dir'): Promise<ContentMetadata[]>;
183
+ /**
184
+ * Deletes content metadata by content ID
185
+ * @param connection - Database connection object
186
+ * @param contentId - Content ID to delete
187
+ * @returns Promise that resolves to true if deleted, false if not found
188
+ */
189
+ export declare function deleteContentMetadata(connection: DatabaseConnection, contentId: string): Promise<boolean>;
190
+ /**
191
+ * Gets storage statistics from storage_stats table
192
+ * @param connection - Database connection object
193
+ * @returns Promise that resolves to storage statistics
194
+ */
195
+ export declare function getStorageStats(connection: DatabaseConnection): Promise<{
196
+ contentDirFiles: number;
197
+ contentDirSize: number;
198
+ filesystemRefs: number;
199
+ lastCleanup: Date | null;
200
+ updatedAt: Date;
201
+ } | null>;
202
+ /**
203
+ * Updates storage statistics in storage_stats table
204
+ * @param connection - Database connection object
205
+ * @param stats - Partial storage statistics to update
206
+ */
207
+ export declare function updateStorageStats(connection: DatabaseConnection, stats: {
208
+ contentDirFiles?: number;
209
+ contentDirSize?: number;
210
+ filesystemRefs?: number;
211
+ lastCleanup?: Date;
212
+ }): Promise<void>;
213
+ //# sourceMappingURL=db.d.ts.map