agentdb 1.0.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 (361) hide show
  1. package/LICENSE +38 -0
  2. package/LICENSE-APACHE +190 -0
  3. package/LICENSE-MIT +21 -0
  4. package/README.md +953 -0
  5. package/bin/agentdb.js +485 -0
  6. package/bin/plugin-cli-wrapper.mjs +21 -0
  7. package/dist/cache/query-cache.d.ts +105 -0
  8. package/dist/cache/query-cache.d.ts.map +1 -0
  9. package/dist/cache/query-cache.js +224 -0
  10. package/dist/cache/query-cache.js.map +1 -0
  11. package/dist/cache/query-cache.mjs +219 -0
  12. package/dist/cli/cache/query-cache.d.ts +104 -0
  13. package/dist/cli/cache/query-cache.js +244 -0
  14. package/dist/cli/cli/db-commands.d.ts +48 -0
  15. package/dist/cli/cli/db-commands.js +613 -0
  16. package/dist/cli/commands.d.ts +7 -0
  17. package/dist/cli/commands.d.ts.map +1 -0
  18. package/dist/cli/commands.js +113 -0
  19. package/dist/cli/commands.js.map +1 -0
  20. package/dist/cli/commands.mjs +104 -0
  21. package/dist/cli/core/backend-interface.d.ts +70 -0
  22. package/dist/cli/core/backend-interface.js +15 -0
  23. package/dist/cli/core/native-backend.d.ts +140 -0
  24. package/dist/cli/core/native-backend.js +432 -0
  25. package/dist/cli/core/vector-db.d.ts +126 -0
  26. package/dist/cli/core/vector-db.js +338 -0
  27. package/dist/cli/core/wasm-backend.d.ts +95 -0
  28. package/dist/cli/core/wasm-backend.js +418 -0
  29. package/dist/cli/db-commands.d.ts +49 -0
  30. package/dist/cli/db-commands.d.ts.map +1 -0
  31. package/dist/cli/db-commands.js +533 -0
  32. package/dist/cli/db-commands.js.map +1 -0
  33. package/dist/cli/db-commands.mjs +522 -0
  34. package/dist/cli/generator.d.ts +11 -0
  35. package/dist/cli/generator.d.ts.map +1 -0
  36. package/dist/cli/generator.js +567 -0
  37. package/dist/cli/generator.js.map +1 -0
  38. package/dist/cli/generator.mjs +527 -0
  39. package/dist/cli/help.d.ts +18 -0
  40. package/dist/cli/help.d.ts.map +1 -0
  41. package/dist/cli/help.js +676 -0
  42. package/dist/cli/help.js.map +1 -0
  43. package/dist/cli/help.mjs +667 -0
  44. package/dist/cli/index/hnsw.d.ts +164 -0
  45. package/dist/cli/index/hnsw.js +558 -0
  46. package/dist/cli/plugin-cli.d.ts +7 -0
  47. package/dist/cli/plugin-cli.d.ts.map +1 -0
  48. package/dist/cli/plugin-cli.js +295 -0
  49. package/dist/cli/plugin-cli.js.map +1 -0
  50. package/dist/cli/plugin-cli.mjs +289 -0
  51. package/dist/cli/quantization/product-quantization.d.ts +108 -0
  52. package/dist/cli/quantization/product-quantization.js +350 -0
  53. package/dist/cli/query/query-builder.d.ts +322 -0
  54. package/dist/cli/query/query-builder.js +600 -0
  55. package/dist/cli/templates.d.ts +14 -0
  56. package/dist/cli/templates.d.ts.map +1 -0
  57. package/dist/cli/templates.js +182 -0
  58. package/dist/cli/templates.js.map +1 -0
  59. package/dist/cli/templates.mjs +176 -0
  60. package/dist/cli/types/index.d.ts +116 -0
  61. package/dist/cli/types/index.js +5 -0
  62. package/dist/cli/types.d.ts +91 -0
  63. package/dist/cli/types.d.ts.map +1 -0
  64. package/dist/cli/types.js +6 -0
  65. package/dist/cli/types.js.map +1 -0
  66. package/dist/cli/types.mjs +4 -0
  67. package/dist/cli/wizard/index.d.ts +6 -0
  68. package/dist/cli/wizard/index.d.ts.map +1 -0
  69. package/dist/cli/wizard/index.js +138 -0
  70. package/dist/cli/wizard/index.js.map +1 -0
  71. package/dist/cli/wizard/index.mjs +131 -0
  72. package/dist/cli/wizard/prompts.d.ts +11 -0
  73. package/dist/cli/wizard/prompts.d.ts.map +1 -0
  74. package/dist/cli/wizard/prompts.js +482 -0
  75. package/dist/cli/wizard/prompts.js.map +1 -0
  76. package/dist/cli/wizard/prompts.mjs +470 -0
  77. package/dist/cli/wizard/validator.d.ts +13 -0
  78. package/dist/cli/wizard/validator.d.ts.map +1 -0
  79. package/dist/cli/wizard/validator.js +234 -0
  80. package/dist/cli/wizard/validator.js.map +1 -0
  81. package/dist/cli/wizard/validator.mjs +224 -0
  82. package/dist/core/backend-interface.d.ts +71 -0
  83. package/dist/core/backend-interface.d.ts.map +1 -0
  84. package/dist/core/backend-interface.js +16 -0
  85. package/dist/core/backend-interface.js.map +1 -0
  86. package/dist/core/backend-interface.mjs +12 -0
  87. package/dist/core/native-backend.d.ts +141 -0
  88. package/dist/core/native-backend.d.ts.map +1 -0
  89. package/dist/core/native-backend.js +457 -0
  90. package/dist/core/native-backend.js.map +1 -0
  91. package/dist/core/native-backend.mjs +449 -0
  92. package/dist/core/vector-db.d.ts +127 -0
  93. package/dist/core/vector-db.d.ts.map +1 -0
  94. package/dist/core/vector-db.js +266 -0
  95. package/dist/core/vector-db.js.map +1 -0
  96. package/dist/core/vector-db.mjs +261 -0
  97. package/dist/core/wasm-backend.d.ts +96 -0
  98. package/dist/core/wasm-backend.d.ts.map +1 -0
  99. package/dist/core/wasm-backend.js +393 -0
  100. package/dist/core/wasm-backend.js.map +1 -0
  101. package/dist/core/wasm-backend.mjs +385 -0
  102. package/dist/index/hnsw-optimized.d.ts +75 -0
  103. package/dist/index/hnsw-optimized.d.ts.map +1 -0
  104. package/dist/index/hnsw-optimized.js +412 -0
  105. package/dist/index/hnsw-optimized.js.map +1 -0
  106. package/dist/index/hnsw-optimized.mjs +407 -0
  107. package/dist/index/hnsw.d.ts +165 -0
  108. package/dist/index/hnsw.d.ts.map +1 -0
  109. package/dist/index/hnsw.js +521 -0
  110. package/dist/index/hnsw.js.map +1 -0
  111. package/dist/index/hnsw.mjs +516 -0
  112. package/dist/index.d.ts +57 -0
  113. package/dist/index.d.ts.map +1 -0
  114. package/dist/index.js +82 -0
  115. package/dist/index.js.map +1 -0
  116. package/dist/index.mjs +63 -0
  117. package/dist/mcp-server.d.ts +27 -0
  118. package/dist/mcp-server.d.ts.map +1 -0
  119. package/dist/mcp-server.js +789 -0
  120. package/dist/mcp-server.js.map +1 -0
  121. package/dist/mcp-server.mjs +784 -0
  122. package/dist/plugins/base-plugin.d.ts +114 -0
  123. package/dist/plugins/base-plugin.d.ts.map +1 -0
  124. package/dist/plugins/base-plugin.js +313 -0
  125. package/dist/plugins/base-plugin.js.map +1 -0
  126. package/dist/plugins/base-plugin.mjs +275 -0
  127. package/dist/plugins/implementations/active-learning.d.ts +135 -0
  128. package/dist/plugins/implementations/active-learning.d.ts.map +1 -0
  129. package/dist/plugins/implementations/active-learning.js +372 -0
  130. package/dist/plugins/implementations/active-learning.js.map +1 -0
  131. package/dist/plugins/implementations/active-learning.mjs +367 -0
  132. package/dist/plugins/implementations/actor-critic.d.ts +64 -0
  133. package/dist/plugins/implementations/actor-critic.d.ts.map +1 -0
  134. package/dist/plugins/implementations/actor-critic.js +363 -0
  135. package/dist/plugins/implementations/actor-critic.js.map +1 -0
  136. package/dist/plugins/implementations/actor-critic.mjs +358 -0
  137. package/dist/plugins/implementations/adversarial-training.d.ts +133 -0
  138. package/dist/plugins/implementations/adversarial-training.d.ts.map +1 -0
  139. package/dist/plugins/implementations/adversarial-training.js +409 -0
  140. package/dist/plugins/implementations/adversarial-training.js.map +1 -0
  141. package/dist/plugins/implementations/adversarial-training.mjs +404 -0
  142. package/dist/plugins/implementations/curriculum-learning.d.ts +132 -0
  143. package/dist/plugins/implementations/curriculum-learning.d.ts.map +1 -0
  144. package/dist/plugins/implementations/curriculum-learning.js +354 -0
  145. package/dist/plugins/implementations/curriculum-learning.js.map +1 -0
  146. package/dist/plugins/implementations/curriculum-learning.mjs +349 -0
  147. package/dist/plugins/implementations/decision-transformer.d.ts +77 -0
  148. package/dist/plugins/implementations/decision-transformer.d.ts.map +1 -0
  149. package/dist/plugins/implementations/decision-transformer.js +422 -0
  150. package/dist/plugins/implementations/decision-transformer.js.map +1 -0
  151. package/dist/plugins/implementations/decision-transformer.mjs +417 -0
  152. package/dist/plugins/implementations/federated-learning.d.ts +126 -0
  153. package/dist/plugins/implementations/federated-learning.d.ts.map +1 -0
  154. package/dist/plugins/implementations/federated-learning.js +436 -0
  155. package/dist/plugins/implementations/federated-learning.js.map +1 -0
  156. package/dist/plugins/implementations/federated-learning.mjs +431 -0
  157. package/dist/plugins/implementations/index.d.ts +30 -0
  158. package/dist/plugins/implementations/index.d.ts.map +1 -0
  159. package/dist/plugins/implementations/index.js +45 -0
  160. package/dist/plugins/implementations/index.js.map +1 -0
  161. package/dist/plugins/implementations/index.mjs +31 -0
  162. package/dist/plugins/implementations/multi-task-learning.d.ts +115 -0
  163. package/dist/plugins/implementations/multi-task-learning.d.ts.map +1 -0
  164. package/dist/plugins/implementations/multi-task-learning.js +369 -0
  165. package/dist/plugins/implementations/multi-task-learning.js.map +1 -0
  166. package/dist/plugins/implementations/multi-task-learning.mjs +364 -0
  167. package/dist/plugins/implementations/neural-architecture-search.d.ts +148 -0
  168. package/dist/plugins/implementations/neural-architecture-search.d.ts.map +1 -0
  169. package/dist/plugins/implementations/neural-architecture-search.js +379 -0
  170. package/dist/plugins/implementations/neural-architecture-search.js.map +1 -0
  171. package/dist/plugins/implementations/neural-architecture-search.mjs +374 -0
  172. package/dist/plugins/implementations/q-learning.d.ts +98 -0
  173. package/dist/plugins/implementations/q-learning.d.ts.map +1 -0
  174. package/dist/plugins/implementations/q-learning.js +435 -0
  175. package/dist/plugins/implementations/q-learning.js.map +1 -0
  176. package/dist/plugins/implementations/q-learning.mjs +430 -0
  177. package/dist/plugins/implementations/sarsa.d.ts +103 -0
  178. package/dist/plugins/implementations/sarsa.d.ts.map +1 -0
  179. package/dist/plugins/implementations/sarsa.js +347 -0
  180. package/dist/plugins/implementations/sarsa.js.map +1 -0
  181. package/dist/plugins/implementations/sarsa.mjs +342 -0
  182. package/dist/plugins/index.d.ts +107 -0
  183. package/dist/plugins/index.d.ts.map +1 -0
  184. package/dist/plugins/index.js +179 -0
  185. package/dist/plugins/index.js.map +1 -0
  186. package/dist/plugins/index.mjs +168 -0
  187. package/dist/plugins/interface.d.ts +439 -0
  188. package/dist/plugins/interface.d.ts.map +1 -0
  189. package/dist/plugins/interface.js +12 -0
  190. package/dist/plugins/interface.js.map +1 -0
  191. package/dist/plugins/interface.mjs +10 -0
  192. package/dist/plugins/learning-plugin.interface.d.ts +257 -0
  193. package/dist/plugins/learning-plugin.interface.d.ts.map +1 -0
  194. package/dist/plugins/learning-plugin.interface.js +7 -0
  195. package/dist/plugins/learning-plugin.interface.js.map +1 -0
  196. package/dist/plugins/learning-plugin.interface.mjs +5 -0
  197. package/dist/plugins/plugin-exports.d.ts +71 -0
  198. package/dist/plugins/plugin-exports.d.ts.map +1 -0
  199. package/dist/plugins/plugin-exports.js +78 -0
  200. package/dist/plugins/plugin-exports.js.map +1 -0
  201. package/dist/plugins/plugin-exports.mjs +69 -0
  202. package/dist/plugins/registry.d.ts +206 -0
  203. package/dist/plugins/registry.d.ts.map +1 -0
  204. package/dist/plugins/registry.js +365 -0
  205. package/dist/plugins/registry.js.map +1 -0
  206. package/dist/plugins/registry.mjs +356 -0
  207. package/dist/plugins/validator.d.ts +63 -0
  208. package/dist/plugins/validator.d.ts.map +1 -0
  209. package/dist/plugins/validator.js +464 -0
  210. package/dist/plugins/validator.js.map +1 -0
  211. package/dist/plugins/validator.mjs +458 -0
  212. package/dist/quantization/binary-quantization.d.ts +104 -0
  213. package/dist/quantization/binary-quantization.d.ts.map +1 -0
  214. package/dist/quantization/binary-quantization.js +246 -0
  215. package/dist/quantization/binary-quantization.js.map +1 -0
  216. package/dist/quantization/binary-quantization.mjs +240 -0
  217. package/dist/quantization/optimized-pq.d.ts +138 -0
  218. package/dist/quantization/optimized-pq.d.ts.map +1 -0
  219. package/dist/quantization/optimized-pq.js +320 -0
  220. package/dist/quantization/optimized-pq.js.map +1 -0
  221. package/dist/quantization/optimized-pq.mjs +313 -0
  222. package/dist/quantization/product-quantization.d.ts +109 -0
  223. package/dist/quantization/product-quantization.d.ts.map +1 -0
  224. package/dist/quantization/product-quantization.js +287 -0
  225. package/dist/quantization/product-quantization.js.map +1 -0
  226. package/dist/quantization/product-quantization.mjs +282 -0
  227. package/dist/quantization/scalar-quantization.d.ts +100 -0
  228. package/dist/quantization/scalar-quantization.d.ts.map +1 -0
  229. package/dist/quantization/scalar-quantization.js +324 -0
  230. package/dist/quantization/scalar-quantization.js.map +1 -0
  231. package/dist/quantization/scalar-quantization.mjs +319 -0
  232. package/dist/query/index.d.ts +6 -0
  233. package/dist/query/index.d.ts.map +1 -0
  234. package/dist/query/index.js +9 -0
  235. package/dist/query/index.js.map +1 -0
  236. package/dist/query/index.mjs +4 -0
  237. package/dist/query/query-builder.d.ts +323 -0
  238. package/dist/query/query-builder.d.ts.map +1 -0
  239. package/dist/query/query-builder.js +524 -0
  240. package/dist/query/query-builder.js.map +1 -0
  241. package/dist/query/query-builder.mjs +519 -0
  242. package/dist/reasoning/context-synthesizer.d.ts +57 -0
  243. package/dist/reasoning/context-synthesizer.d.ts.map +1 -0
  244. package/dist/reasoning/context-synthesizer.js +224 -0
  245. package/dist/reasoning/context-synthesizer.js.map +1 -0
  246. package/dist/reasoning/context-synthesizer.mjs +219 -0
  247. package/dist/reasoning/experience-curator.d.ts +66 -0
  248. package/dist/reasoning/experience-curator.d.ts.map +1 -0
  249. package/dist/reasoning/experience-curator.js +288 -0
  250. package/dist/reasoning/experience-curator.js.map +1 -0
  251. package/dist/reasoning/experience-curator.mjs +283 -0
  252. package/dist/reasoning/memory-optimizer.d.ts +69 -0
  253. package/dist/reasoning/memory-optimizer.d.ts.map +1 -0
  254. package/dist/reasoning/memory-optimizer.js +331 -0
  255. package/dist/reasoning/memory-optimizer.js.map +1 -0
  256. package/dist/reasoning/memory-optimizer.mjs +326 -0
  257. package/dist/reasoning/pattern-matcher.d.ts +59 -0
  258. package/dist/reasoning/pattern-matcher.d.ts.map +1 -0
  259. package/dist/reasoning/pattern-matcher.js +229 -0
  260. package/dist/reasoning/pattern-matcher.js.map +1 -0
  261. package/dist/reasoning/pattern-matcher.mjs +224 -0
  262. package/dist/reasoningbank/adapter/agentdb-adapter.d.ts +118 -0
  263. package/dist/reasoningbank/adapter/agentdb-adapter.d.ts.map +1 -0
  264. package/dist/reasoningbank/adapter/agentdb-adapter.js +477 -0
  265. package/dist/reasoningbank/adapter/agentdb-adapter.js.map +1 -0
  266. package/dist/reasoningbank/adapter/types.d.ts +113 -0
  267. package/dist/reasoningbank/adapter/types.d.ts.map +1 -0
  268. package/dist/reasoningbank/adapter/types.js +9 -0
  269. package/dist/reasoningbank/adapter/types.js.map +1 -0
  270. package/dist/reasoningbank/cli/commands.d.ts +16 -0
  271. package/dist/reasoningbank/cli/commands.d.ts.map +1 -0
  272. package/dist/reasoningbank/cli/commands.js +272 -0
  273. package/dist/reasoningbank/cli/commands.js.map +1 -0
  274. package/dist/reasoningbank/mcp/agentdb-tools.d.ts +319 -0
  275. package/dist/reasoningbank/mcp/agentdb-tools.d.ts.map +1 -0
  276. package/dist/reasoningbank/mcp/agentdb-tools.js +301 -0
  277. package/dist/reasoningbank/mcp/agentdb-tools.js.map +1 -0
  278. package/dist/reasoningbank/migration/migrate.d.ts +25 -0
  279. package/dist/reasoningbank/migration/migrate.d.ts.map +1 -0
  280. package/dist/reasoningbank/migration/migrate.js +178 -0
  281. package/dist/reasoningbank/migration/migrate.js.map +1 -0
  282. package/dist/reasoningbank/reasoning/context-synthesizer.d.ts +37 -0
  283. package/dist/reasoningbank/reasoning/context-synthesizer.d.ts.map +1 -0
  284. package/dist/reasoningbank/reasoning/context-synthesizer.js +114 -0
  285. package/dist/reasoningbank/reasoning/context-synthesizer.js.map +1 -0
  286. package/dist/reasoningbank/reasoning/experience-curator.d.ts +39 -0
  287. package/dist/reasoningbank/reasoning/experience-curator.d.ts.map +1 -0
  288. package/dist/reasoningbank/reasoning/experience-curator.js +98 -0
  289. package/dist/reasoningbank/reasoning/experience-curator.js.map +1 -0
  290. package/dist/reasoningbank/reasoning/memory-optimizer.d.ts +44 -0
  291. package/dist/reasoningbank/reasoning/memory-optimizer.d.ts.map +1 -0
  292. package/dist/reasoningbank/reasoning/memory-optimizer.js +184 -0
  293. package/dist/reasoningbank/reasoning/memory-optimizer.js.map +1 -0
  294. package/dist/reasoningbank/reasoning/pattern-matcher.d.ts +40 -0
  295. package/dist/reasoningbank/reasoning/pattern-matcher.d.ts.map +1 -0
  296. package/dist/reasoningbank/reasoning/pattern-matcher.js +87 -0
  297. package/dist/reasoningbank/reasoning/pattern-matcher.js.map +1 -0
  298. package/dist/reasoningbank/sync/quic-sync.d.ts +77 -0
  299. package/dist/reasoningbank/sync/quic-sync.d.ts.map +1 -0
  300. package/dist/reasoningbank/sync/quic-sync.js +165 -0
  301. package/dist/reasoningbank/sync/quic-sync.js.map +1 -0
  302. package/dist/sync/conflict.d.ts +78 -0
  303. package/dist/sync/conflict.d.ts.map +1 -0
  304. package/dist/sync/conflict.js +202 -0
  305. package/dist/sync/conflict.js.map +1 -0
  306. package/dist/sync/conflict.mjs +196 -0
  307. package/dist/sync/coordinator.d.ts +111 -0
  308. package/dist/sync/coordinator.d.ts.map +1 -0
  309. package/dist/sync/coordinator.js +256 -0
  310. package/dist/sync/coordinator.js.map +1 -0
  311. package/dist/sync/coordinator.mjs +250 -0
  312. package/dist/sync/delta.d.ts +81 -0
  313. package/dist/sync/delta.d.ts.map +1 -0
  314. package/dist/sync/delta.js +245 -0
  315. package/dist/sync/delta.js.map +1 -0
  316. package/dist/sync/delta.mjs +238 -0
  317. package/dist/sync/index.d.ts +11 -0
  318. package/dist/sync/index.d.ts.map +1 -0
  319. package/dist/sync/index.js +22 -0
  320. package/dist/sync/index.js.map +1 -0
  321. package/dist/sync/index.mjs +9 -0
  322. package/dist/sync/quic-sync.d.ts +81 -0
  323. package/dist/sync/quic-sync.d.ts.map +1 -0
  324. package/dist/sync/quic-sync.js +329 -0
  325. package/dist/sync/quic-sync.js.map +1 -0
  326. package/dist/sync/quic-sync.mjs +323 -0
  327. package/dist/sync/types.d.ts +168 -0
  328. package/dist/sync/types.d.ts.map +1 -0
  329. package/dist/sync/types.js +8 -0
  330. package/dist/sync/types.js.map +1 -0
  331. package/dist/sync/types.mjs +6 -0
  332. package/dist/types/index.d.ts +117 -0
  333. package/dist/types/index.d.ts.map +1 -0
  334. package/dist/types/index.js +6 -0
  335. package/dist/types/index.js.map +1 -0
  336. package/dist/types/index.mjs +4 -0
  337. package/dist/wasm-loader.d.ts +32 -0
  338. package/dist/wasm-loader.d.ts.map +1 -0
  339. package/dist/wasm-loader.js +75 -0
  340. package/dist/wasm-loader.js.map +1 -0
  341. package/dist/wasm-loader.mjs +64 -0
  342. package/examples/adaptive-learning.ts +284 -0
  343. package/examples/browser/README.md +732 -0
  344. package/examples/browser/adaptive-recommendations/index.html +427 -0
  345. package/examples/browser/collaborative-filtering/index.html +310 -0
  346. package/examples/browser/continual-learning/index.html +736 -0
  347. package/examples/browser/experience-replay/index.html +616 -0
  348. package/examples/browser/index.html +369 -0
  349. package/examples/browser/meta-learning/index.html +789 -0
  350. package/examples/browser/neuro-symbolic/index.html +692 -0
  351. package/examples/browser/pattern-learning/index.html +620 -0
  352. package/examples/browser/quantum-inspired/index.html +728 -0
  353. package/examples/browser/rag/index.html +624 -0
  354. package/examples/browser/swarm-intelligence/index.html +811 -0
  355. package/examples/browser-basic.html +170 -0
  356. package/examples/hnsw-example.ts +148 -0
  357. package/examples/node-basic.js +70 -0
  358. package/examples/quic-sync-example.ts +310 -0
  359. package/examples/quick-start.js +68 -0
  360. package/examples/wasm-example.ts +222 -0
  361. package/package.json +118 -0
@@ -0,0 +1,432 @@
1
+ "use strict";
2
+ /**
3
+ * Native backend implementation using better-sqlite3
4
+ * High-performance backend for Node.js environments
5
+ */
6
+ var __assign = (this && this.__assign) || function () {
7
+ __assign = Object.assign || function(t) {
8
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
9
+ s = arguments[i];
10
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
+ t[p] = s[p];
12
+ }
13
+ return t;
14
+ };
15
+ return __assign.apply(this, arguments);
16
+ };
17
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.NativeBackend = void 0;
22
+ var better_sqlite3_1 = __importDefault(require("better-sqlite3"));
23
+ var hnsw_1 = require("../index/hnsw");
24
+ /**
25
+ * Native backend using better-sqlite3
26
+ * Optimized for Node.js with native SQLite bindings
27
+ * Features HNSW index for high-performance approximate nearest neighbor search
28
+ */
29
+ var NativeBackend = /** @class */ (function () {
30
+ function NativeBackend() {
31
+ this.db = null;
32
+ this.insertStmt = null;
33
+ this.initialized = false;
34
+ this.hnswIndex = null;
35
+ this.hnswConfig = hnsw_1.DEFAULT_HNSW_CONFIG;
36
+ }
37
+ /**
38
+ * Initialize the native backend
39
+ */
40
+ NativeBackend.prototype.initialize = function (config) {
41
+ if (config === void 0) { config = {}; }
42
+ if (this.initialized) {
43
+ return;
44
+ }
45
+ var _a = config.path, path = _a === void 0 ? ':memory:' : _a, _b = config.memoryMode, memoryMode = _b === void 0 ? true : _b, _c = config.cacheSize, cacheSize = _c === void 0 ? 100 * 1024 : _c, // 100MB in KB
46
+ _d = config.walMode, // 100MB in KB
47
+ walMode = _d === void 0 ? true : _d, _e = config.mmapSize, mmapSize = _e === void 0 ? 256 * 1024 * 1024 : _e, // 256MB
48
+ _f = config.hnsw, // 256MB
49
+ hnsw = _f === void 0 ? {} : _f;
50
+ // Create database instance
51
+ this.db = new better_sqlite3_1.default(memoryMode ? ':memory:' : path);
52
+ // Initialize schema and prepare statements
53
+ this.initializeSchema(cacheSize, walMode, mmapSize);
54
+ this.registerCustomFunctions();
55
+ this.prepareStatements();
56
+ // Initialize HNSW index
57
+ this.hnswConfig = __assign(__assign({}, hnsw_1.DEFAULT_HNSW_CONFIG), hnsw);
58
+ if (this.hnswConfig.enabled) {
59
+ this.hnswIndex = new hnsw_1.HNSWIndex(this.db, this.hnswConfig);
60
+ }
61
+ this.initialized = true;
62
+ };
63
+ /**
64
+ * Initialize database schema
65
+ */
66
+ NativeBackend.prototype.initializeSchema = function (cacheSize, walMode, mmapSize) {
67
+ if (!this.db)
68
+ throw new Error('Database not initialized');
69
+ // Optimize SQLite for vector operations
70
+ this.db.pragma('journal_mode = ' + (walMode ? 'WAL' : 'MEMORY'));
71
+ this.db.pragma('synchronous = NORMAL');
72
+ this.db.pragma("cache_size = -".concat(cacheSize));
73
+ this.db.pragma('temp_store = MEMORY');
74
+ this.db.pragma("mmap_size = ".concat(mmapSize));
75
+ this.db.pragma('page_size = 4096');
76
+ // Create vectors table with optimized schema
77
+ this.db.exec("\n CREATE TABLE IF NOT EXISTS vectors (\n id TEXT PRIMARY KEY,\n embedding BLOB NOT NULL,\n norm REAL NOT NULL,\n metadata TEXT,\n timestamp INTEGER NOT NULL\n );\n\n CREATE INDEX IF NOT EXISTS idx_vectors_norm ON vectors(norm);\n CREATE INDEX IF NOT EXISTS idx_vectors_timestamp ON vectors(timestamp);\n ");
78
+ };
79
+ /**
80
+ * Register custom SQL functions for similarity calculations
81
+ */
82
+ NativeBackend.prototype.registerCustomFunctions = function () {
83
+ if (!this.db)
84
+ throw new Error('Database not initialized');
85
+ // Cosine similarity
86
+ this.db.function('cosine_similarity', { deterministic: true }, function (a, b, normA, normB) {
87
+ var arrA = new Float32Array(a.buffer, a.byteOffset, a.byteLength / 4);
88
+ var arrB = new Float32Array(b.buffer, b.byteOffset, b.byteLength / 4);
89
+ var dotProduct = 0;
90
+ for (var i = 0; i < arrA.length; i++) {
91
+ dotProduct += arrA[i] * arrB[i];
92
+ }
93
+ return dotProduct / (normA * normB);
94
+ });
95
+ // Euclidean distance
96
+ this.db.function('euclidean_distance', { deterministic: true }, function (a, b) {
97
+ var arrA = new Float32Array(a.buffer, a.byteOffset, a.byteLength / 4);
98
+ var arrB = new Float32Array(b.buffer, b.byteOffset, b.byteLength / 4);
99
+ var sum = 0;
100
+ for (var i = 0; i < arrA.length; i++) {
101
+ var diff = arrA[i] - arrB[i];
102
+ sum += diff * diff;
103
+ }
104
+ return Math.sqrt(sum);
105
+ });
106
+ // Dot product
107
+ this.db.function('dot_product', { deterministic: true }, function (a, b) {
108
+ var arrA = new Float32Array(a.buffer, a.byteOffset, a.byteLength / 4);
109
+ var arrB = new Float32Array(b.buffer, b.byteOffset, b.byteLength / 4);
110
+ var dotProduct = 0;
111
+ for (var i = 0; i < arrA.length; i++) {
112
+ dotProduct += arrA[i] * arrB[i];
113
+ }
114
+ return dotProduct;
115
+ });
116
+ };
117
+ /**
118
+ * Prepare frequently used statements
119
+ */
120
+ NativeBackend.prototype.prepareStatements = function () {
121
+ if (!this.db)
122
+ throw new Error('Database not initialized');
123
+ this.insertStmt = this.db.prepare("\n INSERT OR REPLACE INTO vectors (id, embedding, norm, metadata, timestamp)\n VALUES (?, ?, ?, ?, ?)\n ");
124
+ };
125
+ /**
126
+ * Calculate L2 norm of a vector
127
+ */
128
+ NativeBackend.prototype.calculateNorm = function (embedding) {
129
+ var sum = 0;
130
+ for (var _i = 0, embedding_1 = embedding; _i < embedding_1.length; _i++) {
131
+ var val = embedding_1[_i];
132
+ sum += val * val;
133
+ }
134
+ return Math.sqrt(sum);
135
+ };
136
+ /**
137
+ * Serialize embedding to binary format
138
+ */
139
+ NativeBackend.prototype.serializeEmbedding = function (embedding) {
140
+ var buffer = Buffer.allocUnsafe(embedding.length * 4);
141
+ var view = new Float32Array(buffer.buffer, buffer.byteOffset, embedding.length);
142
+ view.set(embedding);
143
+ return buffer;
144
+ };
145
+ /**
146
+ * Deserialize embedding from binary format
147
+ */
148
+ NativeBackend.prototype.deserializeEmbedding = function (buffer) {
149
+ var view = new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 4);
150
+ return Array.from(view);
151
+ };
152
+ /**
153
+ * Generate unique ID
154
+ */
155
+ NativeBackend.prototype.generateId = function () {
156
+ return "vec_".concat(Date.now(), "_").concat(Math.random().toString(36).substr(2, 9));
157
+ };
158
+ /**
159
+ * Insert a single vector
160
+ */
161
+ NativeBackend.prototype.insert = function (vector) {
162
+ if (!this.db || !this.insertStmt)
163
+ throw new Error('Database not initialized');
164
+ var id = vector.id || this.generateId();
165
+ var norm = this.calculateNorm(vector.embedding);
166
+ var embedding = this.serializeEmbedding(vector.embedding);
167
+ var metadata = vector.metadata ? JSON.stringify(vector.metadata) : null;
168
+ var timestamp = vector.timestamp || Date.now();
169
+ this.insertStmt.run(id, embedding, norm, metadata, timestamp);
170
+ // Update HNSW index if enabled
171
+ if (this.hnswIndex && this.shouldUseHNSW()) {
172
+ this.hnswIndex.insert(id, vector.embedding);
173
+ }
174
+ return id;
175
+ };
176
+ /**
177
+ * Insert multiple vectors in a transaction with optimized batch processing
178
+ * Performance improvements:
179
+ * - Single transaction for all inserts
180
+ * - Reused prepared statement
181
+ * - No wrapper function overhead
182
+ * - Chunked processing for large batches
183
+ * - Automatic HNSW index building after batch insert
184
+ */
185
+ NativeBackend.prototype.insertBatch = function (vectors) {
186
+ var _this = this;
187
+ if (!this.db || !this.insertStmt)
188
+ throw new Error('Database not initialized');
189
+ var ids = [];
190
+ var CHUNK_SIZE = 5000; // Process in chunks to avoid memory spikes
191
+ // Helper function to process a chunk of vectors
192
+ var processChunk = function (chunk) {
193
+ var chunkIds = [];
194
+ var transaction = _this.db.transaction(function (vecs) {
195
+ // Reuse the prepared statement for all inserts
196
+ for (var _i = 0, vecs_1 = vecs; _i < vecs_1.length; _i++) {
197
+ var vector = vecs_1[_i];
198
+ var id = vector.id || _this.generateId();
199
+ var norm = _this.calculateNorm(vector.embedding);
200
+ var embedding = _this.serializeEmbedding(vector.embedding);
201
+ var metadata = vector.metadata ? JSON.stringify(vector.metadata) : null;
202
+ var timestamp = vector.timestamp || Date.now();
203
+ _this.insertStmt.run(id, embedding, norm, metadata, timestamp);
204
+ chunkIds.push(id);
205
+ }
206
+ });
207
+ transaction(chunk);
208
+ return chunkIds;
209
+ };
210
+ // Process vectors in chunks
211
+ if (vectors.length <= CHUNK_SIZE) {
212
+ // Small batch - process directly
213
+ ids.push.apply(ids, processChunk(vectors));
214
+ }
215
+ else {
216
+ // Large batch - process in chunks to manage memory
217
+ for (var i = 0; i < vectors.length; i += CHUNK_SIZE) {
218
+ var chunk = vectors.slice(i, i + CHUNK_SIZE);
219
+ ids.push.apply(ids, processChunk(chunk));
220
+ }
221
+ }
222
+ // Build HNSW index if threshold reached and not already built
223
+ if (this.hnswIndex && this.shouldUseHNSW() && !this.hnswIndex.isReady()) {
224
+ console.log('Building HNSW index after batch insert...');
225
+ this.hnswIndex.build();
226
+ }
227
+ return ids;
228
+ };
229
+ /**
230
+ * Check if HNSW index should be used
231
+ */
232
+ NativeBackend.prototype.shouldUseHNSW = function () {
233
+ if (!this.hnswIndex || !this.hnswConfig.enabled) {
234
+ return false;
235
+ }
236
+ var stats = this.stats();
237
+ return stats.count >= this.hnswConfig.minVectorsForIndex;
238
+ };
239
+ /**
240
+ * Search for k-nearest neighbors
241
+ * Automatically uses HNSW index when available and beneficial
242
+ */
243
+ NativeBackend.prototype.search = function (queryEmbedding, k, metric, threshold) {
244
+ if (k === void 0) { k = 5; }
245
+ if (metric === void 0) { metric = 'cosine'; }
246
+ if (threshold === void 0) { threshold = 0.0; }
247
+ if (!this.db)
248
+ throw new Error('Database not initialized');
249
+ // Use HNSW index for euclidean distance if available
250
+ // Note: HNSW currently optimized for euclidean, can be extended for other metrics
251
+ if (this.hnswIndex && this.hnswIndex.isReady() && this.shouldUseHNSW() && metric === 'euclidean') {
252
+ return this.searchWithHNSW(queryEmbedding, k, threshold);
253
+ }
254
+ // Fall back to brute-force search
255
+ return this.bruteForceSearch(queryEmbedding, k, metric, threshold);
256
+ };
257
+ /**
258
+ * Search using HNSW index (fast approximate search)
259
+ */
260
+ NativeBackend.prototype.searchWithHNSW = function (queryEmbedding, k, threshold) {
261
+ var _this = this;
262
+ if (!this.hnswIndex || !this.db) {
263
+ throw new Error('HNSW index not initialized');
264
+ }
265
+ var results = this.hnswIndex.search(queryEmbedding, k);
266
+ // Filter by threshold and get metadata from main table
267
+ return results
268
+ .filter(function (r) { return threshold === 0 || r.distance <= threshold; })
269
+ .map(function (result) {
270
+ var stmt = _this.db.prepare('SELECT metadata FROM vectors WHERE id = ?');
271
+ var row = stmt.get(result.vectorId);
272
+ return {
273
+ id: result.vectorId,
274
+ score: result.distance,
275
+ embedding: result.embedding,
276
+ metadata: (row === null || row === void 0 ? void 0 : row.metadata) ? JSON.parse(row.metadata) : undefined
277
+ };
278
+ });
279
+ };
280
+ /**
281
+ * Brute-force search (accurate but slower)
282
+ */
283
+ NativeBackend.prototype.bruteForceSearch = function (queryEmbedding, k, metric, threshold) {
284
+ var _this = this;
285
+ if (!this.db)
286
+ throw new Error('Database not initialized');
287
+ var queryBuffer = this.serializeEmbedding(queryEmbedding);
288
+ var queryNorm = this.calculateNorm(queryEmbedding);
289
+ var sql;
290
+ var params;
291
+ switch (metric) {
292
+ case 'cosine':
293
+ sql = "\n SELECT\n id,\n embedding,\n metadata,\n cosine_similarity(embedding, ?, norm, ?) as score\n FROM vectors\n WHERE cosine_similarity(embedding, ?, norm, ?) >= ?\n ORDER BY score DESC\n LIMIT ?\n ";
294
+ params = [queryBuffer, queryNorm, queryBuffer, queryNorm, threshold, k];
295
+ break;
296
+ case 'euclidean':
297
+ sql = "\n SELECT\n id,\n embedding,\n metadata,\n euclidean_distance(embedding, ?) as score\n FROM vectors\n WHERE euclidean_distance(embedding, ?) <= ? OR ? = 0\n ORDER BY score ASC\n LIMIT ?\n ";
298
+ params = [queryBuffer, queryBuffer, threshold, threshold, k];
299
+ break;
300
+ case 'dot':
301
+ sql = "\n SELECT\n id,\n embedding,\n metadata,\n dot_product(embedding, ?) as score\n FROM vectors\n WHERE dot_product(embedding, ?) >= ?\n ORDER BY score DESC\n LIMIT ?\n ";
302
+ params = [queryBuffer, queryBuffer, threshold, k];
303
+ break;
304
+ default:
305
+ throw new Error("Unsupported similarity metric: ".concat(metric));
306
+ }
307
+ var stmt = this.db.prepare(sql);
308
+ var rows = stmt.all.apply(stmt, params);
309
+ return rows.map(function (row) { return ({
310
+ id: row.id,
311
+ score: row.score,
312
+ embedding: _this.deserializeEmbedding(row.embedding),
313
+ metadata: row.metadata ? JSON.parse(row.metadata) : undefined
314
+ }); });
315
+ };
316
+ /**
317
+ * Get vector by ID
318
+ */
319
+ NativeBackend.prototype.get = function (id) {
320
+ if (!this.db)
321
+ throw new Error('Database not initialized');
322
+ var stmt = this.db.prepare('SELECT * FROM vectors WHERE id = ?');
323
+ var row = stmt.get(id);
324
+ if (!row)
325
+ return null;
326
+ return {
327
+ id: row.id,
328
+ embedding: this.deserializeEmbedding(row.embedding),
329
+ metadata: row.metadata ? JSON.parse(row.metadata) : undefined,
330
+ norm: row.norm,
331
+ timestamp: row.timestamp
332
+ };
333
+ };
334
+ /**
335
+ * Delete vector by ID
336
+ */
337
+ NativeBackend.prototype.delete = function (id) {
338
+ if (!this.db)
339
+ throw new Error('Database not initialized');
340
+ // Delete from HNSW index if enabled
341
+ if (this.hnswIndex && this.shouldUseHNSW()) {
342
+ this.hnswIndex.delete(id);
343
+ }
344
+ var stmt = this.db.prepare('DELETE FROM vectors WHERE id = ?');
345
+ var result = stmt.run(id);
346
+ return result.changes > 0;
347
+ };
348
+ /**
349
+ * Get database statistics
350
+ */
351
+ NativeBackend.prototype.stats = function () {
352
+ if (!this.db)
353
+ throw new Error('Database not initialized');
354
+ var countStmt = this.db.prepare('SELECT COUNT(*) as count FROM vectors');
355
+ var sizeStmt = this.db.prepare('SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size()');
356
+ var count = countStmt.get().count;
357
+ var size = sizeStmt.get().size;
358
+ return { count: count, size: size };
359
+ };
360
+ /**
361
+ * Close database connection
362
+ */
363
+ NativeBackend.prototype.close = function () {
364
+ if (this.db) {
365
+ this.db.close();
366
+ this.db = null;
367
+ }
368
+ this.insertStmt = null;
369
+ this.initialized = false;
370
+ };
371
+ /**
372
+ * Get raw database instance (for advanced usage)
373
+ */
374
+ NativeBackend.prototype.getDatabase = function () {
375
+ return this.db;
376
+ };
377
+ /**
378
+ * Check if backend is initialized
379
+ */
380
+ NativeBackend.prototype.isInitialized = function () {
381
+ return this.initialized;
382
+ };
383
+ /**
384
+ * Build HNSW index manually
385
+ * Useful for pre-building index on existing data
386
+ */
387
+ NativeBackend.prototype.buildHNSWIndex = function () {
388
+ if (!this.hnswIndex) {
389
+ throw new Error('HNSW index not enabled');
390
+ }
391
+ if (!this.shouldUseHNSW()) {
392
+ var stats = this.stats();
393
+ console.warn("Vector count (".concat(stats.count, ") below threshold (").concat(this.hnswConfig.minVectorsForIndex, "). Index build skipped."));
394
+ return;
395
+ }
396
+ this.hnswIndex.build();
397
+ };
398
+ /**
399
+ * Get HNSW index statistics
400
+ */
401
+ NativeBackend.prototype.getHNSWStats = function () {
402
+ if (!this.hnswIndex) {
403
+ return null;
404
+ }
405
+ var stats = this.hnswIndex.getStats();
406
+ return __assign({ enabled: this.hnswConfig.enabled, ready: this.hnswIndex.isReady() }, stats);
407
+ };
408
+ /**
409
+ * Clear HNSW index
410
+ */
411
+ NativeBackend.prototype.clearHNSWIndex = function () {
412
+ if (this.hnswIndex) {
413
+ this.hnswIndex.clear();
414
+ }
415
+ };
416
+ /**
417
+ * Update HNSW configuration and rebuild if needed
418
+ */
419
+ NativeBackend.prototype.updateHNSWConfig = function (config, rebuild) {
420
+ if (rebuild === void 0) { rebuild = false; }
421
+ if (!this.hnswIndex) {
422
+ throw new Error('HNSW index not enabled');
423
+ }
424
+ this.hnswIndex.updateConfig(config);
425
+ this.hnswConfig = __assign(__assign({}, this.hnswConfig), config);
426
+ if (rebuild) {
427
+ this.hnswIndex.build();
428
+ }
429
+ };
430
+ return NativeBackend;
431
+ }());
432
+ exports.NativeBackend = NativeBackend;
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Core SQLiteVector database implementation with backend abstraction
3
+ * Supports both native (better-sqlite3) and WASM (sql.js) backends
4
+ */
5
+ import { Vector, SearchResult, SimilarityMetric, DatabaseConfig } from '../types';
6
+ import { VectorBackend, BackendType, ExtendedDatabaseConfig } from './backend-interface';
7
+ import { QueryCache } from '../cache/query-cache';
8
+ import { ProductQuantizer } from '../quantization/product-quantization';
9
+ import { VectorQueryBuilder } from '../query/query-builder';
10
+ export declare class SQLiteVectorDB {
11
+ private backend;
12
+ private backendType;
13
+ private queryCache?;
14
+ private quantizer?;
15
+ constructor(config?: ExtendedDatabaseConfig);
16
+ /**
17
+ * Detect appropriate backend based on environment and configuration
18
+ */
19
+ private detectBackend;
20
+ /**
21
+ * Create backend instance
22
+ */
23
+ private createBackend;
24
+ /**
25
+ * Async initialization for WASM backend
26
+ * Must be called after constructor if using WASM backend
27
+ */
28
+ initializeAsync(config?: DatabaseConfig): Promise<void>;
29
+ /**
30
+ * Get backend type
31
+ */
32
+ getBackendType(): BackendType;
33
+ /**
34
+ * Check if backend is initialized
35
+ */
36
+ isInitialized(): boolean;
37
+ /**
38
+ * Insert a single vector
39
+ */
40
+ insert(vector: Vector): string;
41
+ /**
42
+ * Insert multiple vectors in a transaction
43
+ */
44
+ insertBatch(vectors: Vector[]): string[];
45
+ /**
46
+ * Search for k-nearest neighbors with optional caching
47
+ */
48
+ search(queryEmbedding: number[], k?: number, metric?: SimilarityMetric, threshold?: number): SearchResult[];
49
+ /**
50
+ * Get vector by ID
51
+ */
52
+ get(id: string): Vector | null;
53
+ /**
54
+ * Delete vector by ID
55
+ */
56
+ delete(id: string): boolean;
57
+ /**
58
+ * Get database statistics
59
+ */
60
+ stats(): {
61
+ count: number;
62
+ size: number;
63
+ };
64
+ /**
65
+ * Close database connection
66
+ */
67
+ close(): void;
68
+ /**
69
+ * Export database to binary format (WASM only)
70
+ */
71
+ export(): Uint8Array | undefined;
72
+ /**
73
+ * Import database from binary format (WASM only)
74
+ */
75
+ importAsync(data: Uint8Array): Promise<void>;
76
+ /**
77
+ * Get raw backend instance (for advanced usage)
78
+ */
79
+ getBackend(): VectorBackend;
80
+ /**
81
+ * Get raw database instance (for advanced usage with native backend)
82
+ * @deprecated Use getBackend() instead for cross-backend compatibility
83
+ */
84
+ getDatabase(): any;
85
+ /**
86
+ * Get query cache instance
87
+ */
88
+ getQueryCache(): QueryCache<SearchResult[]> | undefined;
89
+ /**
90
+ * Get query cache statistics
91
+ */
92
+ getCacheStats(): import("../cache/query-cache").CacheStats;
93
+ /**
94
+ * Clear query cache
95
+ */
96
+ clearCache(): void;
97
+ /**
98
+ * Get quantizer instance
99
+ */
100
+ getQuantizer(): ProductQuantizer | undefined;
101
+ /**
102
+ * Train quantizer with existing vectors
103
+ */
104
+ trainQuantizer(): Promise<void>;
105
+ /**
106
+ * Get compression statistics (if quantization enabled)
107
+ */
108
+ getCompressionStats(): import("../quantization/product-quantization").CompressionStats;
109
+ /**
110
+ * Create a fluent query builder for complex queries
111
+ *
112
+ * @returns VectorQueryBuilder instance
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const results = await db.query()
117
+ * .similarTo(queryVector)
118
+ * .where('metadata.category', '=', 'tech')
119
+ * .whereBetween('metadata.year', 2020, 2024)
120
+ * .orderBySimilarity('desc')
121
+ * .limit(10)
122
+ * .execute();
123
+ * ```
124
+ */
125
+ query<T = any>(): VectorQueryBuilder<T>;
126
+ }