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.
- package/LICENSE +38 -0
- package/LICENSE-APACHE +190 -0
- package/LICENSE-MIT +21 -0
- package/README.md +953 -0
- package/bin/agentdb.js +485 -0
- package/bin/plugin-cli-wrapper.mjs +21 -0
- package/dist/cache/query-cache.d.ts +105 -0
- package/dist/cache/query-cache.d.ts.map +1 -0
- package/dist/cache/query-cache.js +224 -0
- package/dist/cache/query-cache.js.map +1 -0
- package/dist/cache/query-cache.mjs +219 -0
- package/dist/cli/cache/query-cache.d.ts +104 -0
- package/dist/cli/cache/query-cache.js +244 -0
- package/dist/cli/cli/db-commands.d.ts +48 -0
- package/dist/cli/cli/db-commands.js +613 -0
- package/dist/cli/commands.d.ts +7 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +113 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/commands.mjs +104 -0
- package/dist/cli/core/backend-interface.d.ts +70 -0
- package/dist/cli/core/backend-interface.js +15 -0
- package/dist/cli/core/native-backend.d.ts +140 -0
- package/dist/cli/core/native-backend.js +432 -0
- package/dist/cli/core/vector-db.d.ts +126 -0
- package/dist/cli/core/vector-db.js +338 -0
- package/dist/cli/core/wasm-backend.d.ts +95 -0
- package/dist/cli/core/wasm-backend.js +418 -0
- package/dist/cli/db-commands.d.ts +49 -0
- package/dist/cli/db-commands.d.ts.map +1 -0
- package/dist/cli/db-commands.js +533 -0
- package/dist/cli/db-commands.js.map +1 -0
- package/dist/cli/db-commands.mjs +522 -0
- package/dist/cli/generator.d.ts +11 -0
- package/dist/cli/generator.d.ts.map +1 -0
- package/dist/cli/generator.js +567 -0
- package/dist/cli/generator.js.map +1 -0
- package/dist/cli/generator.mjs +527 -0
- package/dist/cli/help.d.ts +18 -0
- package/dist/cli/help.d.ts.map +1 -0
- package/dist/cli/help.js +676 -0
- package/dist/cli/help.js.map +1 -0
- package/dist/cli/help.mjs +667 -0
- package/dist/cli/index/hnsw.d.ts +164 -0
- package/dist/cli/index/hnsw.js +558 -0
- package/dist/cli/plugin-cli.d.ts +7 -0
- package/dist/cli/plugin-cli.d.ts.map +1 -0
- package/dist/cli/plugin-cli.js +295 -0
- package/dist/cli/plugin-cli.js.map +1 -0
- package/dist/cli/plugin-cli.mjs +289 -0
- package/dist/cli/quantization/product-quantization.d.ts +108 -0
- package/dist/cli/quantization/product-quantization.js +350 -0
- package/dist/cli/query/query-builder.d.ts +322 -0
- package/dist/cli/query/query-builder.js +600 -0
- package/dist/cli/templates.d.ts +14 -0
- package/dist/cli/templates.d.ts.map +1 -0
- package/dist/cli/templates.js +182 -0
- package/dist/cli/templates.js.map +1 -0
- package/dist/cli/templates.mjs +176 -0
- package/dist/cli/types/index.d.ts +116 -0
- package/dist/cli/types/index.js +5 -0
- package/dist/cli/types.d.ts +91 -0
- package/dist/cli/types.d.ts.map +1 -0
- package/dist/cli/types.js +6 -0
- package/dist/cli/types.js.map +1 -0
- package/dist/cli/types.mjs +4 -0
- package/dist/cli/wizard/index.d.ts +6 -0
- package/dist/cli/wizard/index.d.ts.map +1 -0
- package/dist/cli/wizard/index.js +138 -0
- package/dist/cli/wizard/index.js.map +1 -0
- package/dist/cli/wizard/index.mjs +131 -0
- package/dist/cli/wizard/prompts.d.ts +11 -0
- package/dist/cli/wizard/prompts.d.ts.map +1 -0
- package/dist/cli/wizard/prompts.js +482 -0
- package/dist/cli/wizard/prompts.js.map +1 -0
- package/dist/cli/wizard/prompts.mjs +470 -0
- package/dist/cli/wizard/validator.d.ts +13 -0
- package/dist/cli/wizard/validator.d.ts.map +1 -0
- package/dist/cli/wizard/validator.js +234 -0
- package/dist/cli/wizard/validator.js.map +1 -0
- package/dist/cli/wizard/validator.mjs +224 -0
- package/dist/core/backend-interface.d.ts +71 -0
- package/dist/core/backend-interface.d.ts.map +1 -0
- package/dist/core/backend-interface.js +16 -0
- package/dist/core/backend-interface.js.map +1 -0
- package/dist/core/backend-interface.mjs +12 -0
- package/dist/core/native-backend.d.ts +141 -0
- package/dist/core/native-backend.d.ts.map +1 -0
- package/dist/core/native-backend.js +457 -0
- package/dist/core/native-backend.js.map +1 -0
- package/dist/core/native-backend.mjs +449 -0
- package/dist/core/vector-db.d.ts +127 -0
- package/dist/core/vector-db.d.ts.map +1 -0
- package/dist/core/vector-db.js +266 -0
- package/dist/core/vector-db.js.map +1 -0
- package/dist/core/vector-db.mjs +261 -0
- package/dist/core/wasm-backend.d.ts +96 -0
- package/dist/core/wasm-backend.d.ts.map +1 -0
- package/dist/core/wasm-backend.js +393 -0
- package/dist/core/wasm-backend.js.map +1 -0
- package/dist/core/wasm-backend.mjs +385 -0
- package/dist/index/hnsw-optimized.d.ts +75 -0
- package/dist/index/hnsw-optimized.d.ts.map +1 -0
- package/dist/index/hnsw-optimized.js +412 -0
- package/dist/index/hnsw-optimized.js.map +1 -0
- package/dist/index/hnsw-optimized.mjs +407 -0
- package/dist/index/hnsw.d.ts +165 -0
- package/dist/index/hnsw.d.ts.map +1 -0
- package/dist/index/hnsw.js +521 -0
- package/dist/index/hnsw.js.map +1 -0
- package/dist/index/hnsw.mjs +516 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +82 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +63 -0
- package/dist/mcp-server.d.ts +27 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-server.js +789 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/mcp-server.mjs +784 -0
- package/dist/plugins/base-plugin.d.ts +114 -0
- package/dist/plugins/base-plugin.d.ts.map +1 -0
- package/dist/plugins/base-plugin.js +313 -0
- package/dist/plugins/base-plugin.js.map +1 -0
- package/dist/plugins/base-plugin.mjs +275 -0
- package/dist/plugins/implementations/active-learning.d.ts +135 -0
- package/dist/plugins/implementations/active-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/active-learning.js +372 -0
- package/dist/plugins/implementations/active-learning.js.map +1 -0
- package/dist/plugins/implementations/active-learning.mjs +367 -0
- package/dist/plugins/implementations/actor-critic.d.ts +64 -0
- package/dist/plugins/implementations/actor-critic.d.ts.map +1 -0
- package/dist/plugins/implementations/actor-critic.js +363 -0
- package/dist/plugins/implementations/actor-critic.js.map +1 -0
- package/dist/plugins/implementations/actor-critic.mjs +358 -0
- package/dist/plugins/implementations/adversarial-training.d.ts +133 -0
- package/dist/plugins/implementations/adversarial-training.d.ts.map +1 -0
- package/dist/plugins/implementations/adversarial-training.js +409 -0
- package/dist/plugins/implementations/adversarial-training.js.map +1 -0
- package/dist/plugins/implementations/adversarial-training.mjs +404 -0
- package/dist/plugins/implementations/curriculum-learning.d.ts +132 -0
- package/dist/plugins/implementations/curriculum-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/curriculum-learning.js +354 -0
- package/dist/plugins/implementations/curriculum-learning.js.map +1 -0
- package/dist/plugins/implementations/curriculum-learning.mjs +349 -0
- package/dist/plugins/implementations/decision-transformer.d.ts +77 -0
- package/dist/plugins/implementations/decision-transformer.d.ts.map +1 -0
- package/dist/plugins/implementations/decision-transformer.js +422 -0
- package/dist/plugins/implementations/decision-transformer.js.map +1 -0
- package/dist/plugins/implementations/decision-transformer.mjs +417 -0
- package/dist/plugins/implementations/federated-learning.d.ts +126 -0
- package/dist/plugins/implementations/federated-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/federated-learning.js +436 -0
- package/dist/plugins/implementations/federated-learning.js.map +1 -0
- package/dist/plugins/implementations/federated-learning.mjs +431 -0
- package/dist/plugins/implementations/index.d.ts +30 -0
- package/dist/plugins/implementations/index.d.ts.map +1 -0
- package/dist/plugins/implementations/index.js +45 -0
- package/dist/plugins/implementations/index.js.map +1 -0
- package/dist/plugins/implementations/index.mjs +31 -0
- package/dist/plugins/implementations/multi-task-learning.d.ts +115 -0
- package/dist/plugins/implementations/multi-task-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/multi-task-learning.js +369 -0
- package/dist/plugins/implementations/multi-task-learning.js.map +1 -0
- package/dist/plugins/implementations/multi-task-learning.mjs +364 -0
- package/dist/plugins/implementations/neural-architecture-search.d.ts +148 -0
- package/dist/plugins/implementations/neural-architecture-search.d.ts.map +1 -0
- package/dist/plugins/implementations/neural-architecture-search.js +379 -0
- package/dist/plugins/implementations/neural-architecture-search.js.map +1 -0
- package/dist/plugins/implementations/neural-architecture-search.mjs +374 -0
- package/dist/plugins/implementations/q-learning.d.ts +98 -0
- package/dist/plugins/implementations/q-learning.d.ts.map +1 -0
- package/dist/plugins/implementations/q-learning.js +435 -0
- package/dist/plugins/implementations/q-learning.js.map +1 -0
- package/dist/plugins/implementations/q-learning.mjs +430 -0
- package/dist/plugins/implementations/sarsa.d.ts +103 -0
- package/dist/plugins/implementations/sarsa.d.ts.map +1 -0
- package/dist/plugins/implementations/sarsa.js +347 -0
- package/dist/plugins/implementations/sarsa.js.map +1 -0
- package/dist/plugins/implementations/sarsa.mjs +342 -0
- package/dist/plugins/index.d.ts +107 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/index.js +179 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/plugins/index.mjs +168 -0
- package/dist/plugins/interface.d.ts +439 -0
- package/dist/plugins/interface.d.ts.map +1 -0
- package/dist/plugins/interface.js +12 -0
- package/dist/plugins/interface.js.map +1 -0
- package/dist/plugins/interface.mjs +10 -0
- package/dist/plugins/learning-plugin.interface.d.ts +257 -0
- package/dist/plugins/learning-plugin.interface.d.ts.map +1 -0
- package/dist/plugins/learning-plugin.interface.js +7 -0
- package/dist/plugins/learning-plugin.interface.js.map +1 -0
- package/dist/plugins/learning-plugin.interface.mjs +5 -0
- package/dist/plugins/plugin-exports.d.ts +71 -0
- package/dist/plugins/plugin-exports.d.ts.map +1 -0
- package/dist/plugins/plugin-exports.js +78 -0
- package/dist/plugins/plugin-exports.js.map +1 -0
- package/dist/plugins/plugin-exports.mjs +69 -0
- package/dist/plugins/registry.d.ts +206 -0
- package/dist/plugins/registry.d.ts.map +1 -0
- package/dist/plugins/registry.js +365 -0
- package/dist/plugins/registry.js.map +1 -0
- package/dist/plugins/registry.mjs +356 -0
- package/dist/plugins/validator.d.ts +63 -0
- package/dist/plugins/validator.d.ts.map +1 -0
- package/dist/plugins/validator.js +464 -0
- package/dist/plugins/validator.js.map +1 -0
- package/dist/plugins/validator.mjs +458 -0
- package/dist/quantization/binary-quantization.d.ts +104 -0
- package/dist/quantization/binary-quantization.d.ts.map +1 -0
- package/dist/quantization/binary-quantization.js +246 -0
- package/dist/quantization/binary-quantization.js.map +1 -0
- package/dist/quantization/binary-quantization.mjs +240 -0
- package/dist/quantization/optimized-pq.d.ts +138 -0
- package/dist/quantization/optimized-pq.d.ts.map +1 -0
- package/dist/quantization/optimized-pq.js +320 -0
- package/dist/quantization/optimized-pq.js.map +1 -0
- package/dist/quantization/optimized-pq.mjs +313 -0
- package/dist/quantization/product-quantization.d.ts +109 -0
- package/dist/quantization/product-quantization.d.ts.map +1 -0
- package/dist/quantization/product-quantization.js +287 -0
- package/dist/quantization/product-quantization.js.map +1 -0
- package/dist/quantization/product-quantization.mjs +282 -0
- package/dist/quantization/scalar-quantization.d.ts +100 -0
- package/dist/quantization/scalar-quantization.d.ts.map +1 -0
- package/dist/quantization/scalar-quantization.js +324 -0
- package/dist/quantization/scalar-quantization.js.map +1 -0
- package/dist/quantization/scalar-quantization.mjs +319 -0
- package/dist/query/index.d.ts +6 -0
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/index.js +9 -0
- package/dist/query/index.js.map +1 -0
- package/dist/query/index.mjs +4 -0
- package/dist/query/query-builder.d.ts +323 -0
- package/dist/query/query-builder.d.ts.map +1 -0
- package/dist/query/query-builder.js +524 -0
- package/dist/query/query-builder.js.map +1 -0
- package/dist/query/query-builder.mjs +519 -0
- package/dist/reasoning/context-synthesizer.d.ts +57 -0
- package/dist/reasoning/context-synthesizer.d.ts.map +1 -0
- package/dist/reasoning/context-synthesizer.js +224 -0
- package/dist/reasoning/context-synthesizer.js.map +1 -0
- package/dist/reasoning/context-synthesizer.mjs +219 -0
- package/dist/reasoning/experience-curator.d.ts +66 -0
- package/dist/reasoning/experience-curator.d.ts.map +1 -0
- package/dist/reasoning/experience-curator.js +288 -0
- package/dist/reasoning/experience-curator.js.map +1 -0
- package/dist/reasoning/experience-curator.mjs +283 -0
- package/dist/reasoning/memory-optimizer.d.ts +69 -0
- package/dist/reasoning/memory-optimizer.d.ts.map +1 -0
- package/dist/reasoning/memory-optimizer.js +331 -0
- package/dist/reasoning/memory-optimizer.js.map +1 -0
- package/dist/reasoning/memory-optimizer.mjs +326 -0
- package/dist/reasoning/pattern-matcher.d.ts +59 -0
- package/dist/reasoning/pattern-matcher.d.ts.map +1 -0
- package/dist/reasoning/pattern-matcher.js +229 -0
- package/dist/reasoning/pattern-matcher.js.map +1 -0
- package/dist/reasoning/pattern-matcher.mjs +224 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts +118 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts.map +1 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.js +477 -0
- package/dist/reasoningbank/adapter/agentdb-adapter.js.map +1 -0
- package/dist/reasoningbank/adapter/types.d.ts +113 -0
- package/dist/reasoningbank/adapter/types.d.ts.map +1 -0
- package/dist/reasoningbank/adapter/types.js +9 -0
- package/dist/reasoningbank/adapter/types.js.map +1 -0
- package/dist/reasoningbank/cli/commands.d.ts +16 -0
- package/dist/reasoningbank/cli/commands.d.ts.map +1 -0
- package/dist/reasoningbank/cli/commands.js +272 -0
- package/dist/reasoningbank/cli/commands.js.map +1 -0
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts +319 -0
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts.map +1 -0
- package/dist/reasoningbank/mcp/agentdb-tools.js +301 -0
- package/dist/reasoningbank/mcp/agentdb-tools.js.map +1 -0
- package/dist/reasoningbank/migration/migrate.d.ts +25 -0
- package/dist/reasoningbank/migration/migrate.d.ts.map +1 -0
- package/dist/reasoningbank/migration/migrate.js +178 -0
- package/dist/reasoningbank/migration/migrate.js.map +1 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts +37 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.js +114 -0
- package/dist/reasoningbank/reasoning/context-synthesizer.js.map +1 -0
- package/dist/reasoningbank/reasoning/experience-curator.d.ts +39 -0
- package/dist/reasoningbank/reasoning/experience-curator.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/experience-curator.js +98 -0
- package/dist/reasoningbank/reasoning/experience-curator.js.map +1 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts +44 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.js +184 -0
- package/dist/reasoningbank/reasoning/memory-optimizer.js.map +1 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts +40 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts.map +1 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.js +87 -0
- package/dist/reasoningbank/reasoning/pattern-matcher.js.map +1 -0
- package/dist/reasoningbank/sync/quic-sync.d.ts +77 -0
- package/dist/reasoningbank/sync/quic-sync.d.ts.map +1 -0
- package/dist/reasoningbank/sync/quic-sync.js +165 -0
- package/dist/reasoningbank/sync/quic-sync.js.map +1 -0
- package/dist/sync/conflict.d.ts +78 -0
- package/dist/sync/conflict.d.ts.map +1 -0
- package/dist/sync/conflict.js +202 -0
- package/dist/sync/conflict.js.map +1 -0
- package/dist/sync/conflict.mjs +196 -0
- package/dist/sync/coordinator.d.ts +111 -0
- package/dist/sync/coordinator.d.ts.map +1 -0
- package/dist/sync/coordinator.js +256 -0
- package/dist/sync/coordinator.js.map +1 -0
- package/dist/sync/coordinator.mjs +250 -0
- package/dist/sync/delta.d.ts +81 -0
- package/dist/sync/delta.d.ts.map +1 -0
- package/dist/sync/delta.js +245 -0
- package/dist/sync/delta.js.map +1 -0
- package/dist/sync/delta.mjs +238 -0
- package/dist/sync/index.d.ts +11 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/index.js +22 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/sync/index.mjs +9 -0
- package/dist/sync/quic-sync.d.ts +81 -0
- package/dist/sync/quic-sync.d.ts.map +1 -0
- package/dist/sync/quic-sync.js +329 -0
- package/dist/sync/quic-sync.js.map +1 -0
- package/dist/sync/quic-sync.mjs +323 -0
- package/dist/sync/types.d.ts +168 -0
- package/dist/sync/types.d.ts.map +1 -0
- package/dist/sync/types.js +8 -0
- package/dist/sync/types.js.map +1 -0
- package/dist/sync/types.mjs +6 -0
- package/dist/types/index.d.ts +117 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +4 -0
- package/dist/wasm-loader.d.ts +32 -0
- package/dist/wasm-loader.d.ts.map +1 -0
- package/dist/wasm-loader.js +75 -0
- package/dist/wasm-loader.js.map +1 -0
- package/dist/wasm-loader.mjs +64 -0
- package/examples/adaptive-learning.ts +284 -0
- package/examples/browser/README.md +732 -0
- package/examples/browser/adaptive-recommendations/index.html +427 -0
- package/examples/browser/collaborative-filtering/index.html +310 -0
- package/examples/browser/continual-learning/index.html +736 -0
- package/examples/browser/experience-replay/index.html +616 -0
- package/examples/browser/index.html +369 -0
- package/examples/browser/meta-learning/index.html +789 -0
- package/examples/browser/neuro-symbolic/index.html +692 -0
- package/examples/browser/pattern-learning/index.html +620 -0
- package/examples/browser/quantum-inspired/index.html +728 -0
- package/examples/browser/rag/index.html +624 -0
- package/examples/browser/swarm-intelligence/index.html +811 -0
- package/examples/browser-basic.html +170 -0
- package/examples/hnsw-example.ts +148 -0
- package/examples/node-basic.js +70 -0
- package/examples/quic-sync-example.ts +310 -0
- package/examples/quick-start.js +68 -0
- package/examples/wasm-example.ts +222 -0
- package/package.json +118 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Core SQLiteVector database implementation with backend abstraction
|
|
4
|
+
* Supports both native (better-sqlite3) and WASM (sql.js) backends
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.SQLiteVectorDB = void 0;
|
|
8
|
+
const backend_interface_1 = require("./backend-interface");
|
|
9
|
+
const native_backend_1 = require("./native-backend");
|
|
10
|
+
const wasm_backend_1 = require("./wasm-backend");
|
|
11
|
+
const query_cache_1 = require("../cache/query-cache");
|
|
12
|
+
const product_quantization_1 = require("../quantization/product-quantization");
|
|
13
|
+
const query_builder_1 = require("../query/query-builder");
|
|
14
|
+
class SQLiteVectorDB {
|
|
15
|
+
constructor(config = {}) {
|
|
16
|
+
// If path is provided but memoryMode not specified, default to file mode
|
|
17
|
+
if (config.path && config.memoryMode === undefined) {
|
|
18
|
+
config.memoryMode = false;
|
|
19
|
+
}
|
|
20
|
+
// Determine backend type
|
|
21
|
+
this.backendType = this.detectBackend(config);
|
|
22
|
+
// Create appropriate backend
|
|
23
|
+
this.backend = this.createBackend(this.backendType);
|
|
24
|
+
// Initialize query cache if enabled
|
|
25
|
+
if (config.queryCache?.enabled !== false) {
|
|
26
|
+
this.queryCache = new query_cache_1.QueryCache(config.queryCache);
|
|
27
|
+
}
|
|
28
|
+
// Initialize quantizer if enabled
|
|
29
|
+
if (config.quantization?.enabled) {
|
|
30
|
+
this.quantizer = new product_quantization_1.ProductQuantizer(config.quantization);
|
|
31
|
+
}
|
|
32
|
+
// Initialize backend
|
|
33
|
+
if (this.backendType === backend_interface_1.BackendType.WASM) {
|
|
34
|
+
// WASM backend requires async initialization
|
|
35
|
+
// Store config for later async init
|
|
36
|
+
this.backend._pendingConfig = config;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
// Native backend can initialize synchronously
|
|
40
|
+
this.backend.initialize(config);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Detect appropriate backend based on environment and configuration
|
|
45
|
+
*/
|
|
46
|
+
detectBackend(config) {
|
|
47
|
+
// Explicit backend selection
|
|
48
|
+
if (config.backend) {
|
|
49
|
+
return config.backend;
|
|
50
|
+
}
|
|
51
|
+
// Auto-detect based on environment
|
|
52
|
+
if (typeof window !== 'undefined' || typeof process === 'undefined') {
|
|
53
|
+
// Browser environment
|
|
54
|
+
return backend_interface_1.BackendType.WASM;
|
|
55
|
+
}
|
|
56
|
+
// Node.js environment - check if better-sqlite3 is available
|
|
57
|
+
try {
|
|
58
|
+
require.resolve('better-sqlite3');
|
|
59
|
+
return backend_interface_1.BackendType.NATIVE;
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return backend_interface_1.BackendType.WASM;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Create backend instance
|
|
67
|
+
*/
|
|
68
|
+
createBackend(type) {
|
|
69
|
+
switch (type) {
|
|
70
|
+
case backend_interface_1.BackendType.NATIVE:
|
|
71
|
+
return new native_backend_1.NativeBackend();
|
|
72
|
+
case backend_interface_1.BackendType.WASM:
|
|
73
|
+
return new wasm_backend_1.WasmBackend();
|
|
74
|
+
default:
|
|
75
|
+
throw new Error(`Unsupported backend type: ${type}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Async initialization for WASM backend
|
|
80
|
+
* Must be called after constructor if using WASM backend
|
|
81
|
+
*/
|
|
82
|
+
async initializeAsync(config) {
|
|
83
|
+
if (this.backendType === backend_interface_1.BackendType.WASM) {
|
|
84
|
+
const wasmBackend = this.backend;
|
|
85
|
+
const finalConfig = config || wasmBackend._pendingConfig || {};
|
|
86
|
+
await wasmBackend.initializeAsync(finalConfig);
|
|
87
|
+
delete wasmBackend._pendingConfig;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get backend type
|
|
92
|
+
*/
|
|
93
|
+
getBackendType() {
|
|
94
|
+
return this.backendType;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Check if backend is initialized
|
|
98
|
+
*/
|
|
99
|
+
isInitialized() {
|
|
100
|
+
if (this.backendType === backend_interface_1.BackendType.WASM) {
|
|
101
|
+
return this.backend.isInitialized();
|
|
102
|
+
}
|
|
103
|
+
return this.backend.isInitialized();
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Insert a single vector
|
|
107
|
+
*/
|
|
108
|
+
insert(vector) {
|
|
109
|
+
return this.backend.insert(vector);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Insert multiple vectors in a transaction
|
|
113
|
+
*/
|
|
114
|
+
insertBatch(vectors) {
|
|
115
|
+
return this.backend.insertBatch(vectors);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Search for k-nearest neighbors with optional caching
|
|
119
|
+
*/
|
|
120
|
+
search(queryEmbedding, k = 5, metric = 'cosine', threshold = 0.0) {
|
|
121
|
+
// Check cache if enabled
|
|
122
|
+
if (this.queryCache) {
|
|
123
|
+
const cacheKey = query_cache_1.QueryCache.generateKey(queryEmbedding, k, metric, threshold);
|
|
124
|
+
const cached = this.queryCache.get(cacheKey);
|
|
125
|
+
if (cached) {
|
|
126
|
+
return cached;
|
|
127
|
+
}
|
|
128
|
+
// Cache miss - perform search and cache result
|
|
129
|
+
const results = this.backend.search(queryEmbedding, k, metric, threshold);
|
|
130
|
+
this.queryCache.set(cacheKey, results);
|
|
131
|
+
return results;
|
|
132
|
+
}
|
|
133
|
+
// No cache - perform search directly
|
|
134
|
+
return this.backend.search(queryEmbedding, k, metric, threshold);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get vector by ID
|
|
138
|
+
*/
|
|
139
|
+
get(id) {
|
|
140
|
+
return this.backend.get(id);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Delete vector by ID
|
|
144
|
+
*/
|
|
145
|
+
delete(id) {
|
|
146
|
+
return this.backend.delete(id);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get database statistics
|
|
150
|
+
*/
|
|
151
|
+
stats() {
|
|
152
|
+
return this.backend.stats();
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Close database connection
|
|
156
|
+
*/
|
|
157
|
+
close() {
|
|
158
|
+
this.backend.close();
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Export database to binary format (WASM only)
|
|
162
|
+
*/
|
|
163
|
+
export() {
|
|
164
|
+
if (this.backend.export) {
|
|
165
|
+
return this.backend.export();
|
|
166
|
+
}
|
|
167
|
+
throw new Error('Export not supported by current backend');
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Import database from binary format (WASM only)
|
|
171
|
+
*/
|
|
172
|
+
async importAsync(data) {
|
|
173
|
+
if (this.backendType === backend_interface_1.BackendType.WASM) {
|
|
174
|
+
await this.backend.importAsync(data);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
throw new Error('Import not supported by native backend');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get raw backend instance (for advanced usage)
|
|
182
|
+
*/
|
|
183
|
+
getBackend() {
|
|
184
|
+
return this.backend;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get raw database instance (for advanced usage with native backend)
|
|
188
|
+
* @deprecated Use getBackend() instead for cross-backend compatibility
|
|
189
|
+
*/
|
|
190
|
+
getDatabase() {
|
|
191
|
+
if (this.backendType === backend_interface_1.BackendType.NATIVE) {
|
|
192
|
+
return this.backend.getDatabase();
|
|
193
|
+
}
|
|
194
|
+
throw new Error('getDatabase() only supported on native backend. Use getBackend() instead.');
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Get query cache instance
|
|
198
|
+
*/
|
|
199
|
+
getQueryCache() {
|
|
200
|
+
return this.queryCache;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Get query cache statistics
|
|
204
|
+
*/
|
|
205
|
+
getCacheStats() {
|
|
206
|
+
return this.queryCache?.getStats();
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Clear query cache
|
|
210
|
+
*/
|
|
211
|
+
clearCache() {
|
|
212
|
+
this.queryCache?.clear();
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Get quantizer instance
|
|
216
|
+
*/
|
|
217
|
+
getQuantizer() {
|
|
218
|
+
return this.quantizer;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Train quantizer with existing vectors
|
|
222
|
+
*/
|
|
223
|
+
async trainQuantizer() {
|
|
224
|
+
if (!this.quantizer) {
|
|
225
|
+
throw new Error('Quantization not enabled');
|
|
226
|
+
}
|
|
227
|
+
// Get all vectors from database
|
|
228
|
+
const allStats = this.stats();
|
|
229
|
+
if (allStats.count === 0) {
|
|
230
|
+
throw new Error('No vectors to train quantizer');
|
|
231
|
+
}
|
|
232
|
+
// Fetch all embeddings (simplified - in production, batch this)
|
|
233
|
+
const vectors = [];
|
|
234
|
+
// Note: This would need backend support to efficiently fetch all embeddings
|
|
235
|
+
console.log('Training quantizer with existing vectors...');
|
|
236
|
+
await this.quantizer.train(vectors);
|
|
237
|
+
console.log('Quantizer training complete');
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Get compression statistics (if quantization enabled)
|
|
241
|
+
*/
|
|
242
|
+
getCompressionStats() {
|
|
243
|
+
return this.quantizer?.getStats();
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Create a fluent query builder for complex queries
|
|
247
|
+
*
|
|
248
|
+
* @returns VectorQueryBuilder instance
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```typescript
|
|
252
|
+
* const results = await db.query()
|
|
253
|
+
* .similarTo(queryVector)
|
|
254
|
+
* .where('metadata.category', '=', 'tech')
|
|
255
|
+
* .whereBetween('metadata.year', 2020, 2024)
|
|
256
|
+
* .orderBySimilarity('desc')
|
|
257
|
+
* .limit(10)
|
|
258
|
+
* .execute();
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
query() {
|
|
262
|
+
return new query_builder_1.VectorQueryBuilder(this);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
exports.SQLiteVectorDB = SQLiteVectorDB;
|
|
266
|
+
//# sourceMappingURL=vector-db.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector-db.js","sourceRoot":"","sources":["../../src/core/vector-db.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAGH,2DAAyF;AACzF,qDAAiD;AACjD,iDAA6C;AAC7C,sDAAkD;AAClD,+EAAwE;AACxE,0DAA4D;AAE5D,MAAa,cAAc;IAMzB,YAAY,SAAiC,EAAE;QAC7C,yEAAyE;QACzE,IAAI,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YACnD,MAAM,CAAC,UAAU,GAAG,KAAK,CAAC;QAC5B,CAAC;QAED,yBAAyB;QACzB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QAE9C,6BAA6B;QAC7B,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAEpD,oCAAoC;QACpC,IAAI,MAAM,CAAC,UAAU,EAAE,OAAO,KAAK,KAAK,EAAE,CAAC;YACzC,IAAI,CAAC,UAAU,GAAG,IAAI,wBAAU,CAAiB,MAAM,CAAC,UAAU,CAAC,CAAC;QACtE,CAAC;QAED,kCAAkC;QAClC,IAAI,MAAM,CAAC,YAAY,EAAE,OAAO,EAAE,CAAC;YACjC,IAAI,CAAC,SAAS,GAAG,IAAI,uCAAgB,CAAC,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E,QAAQ,EAAE,CAAC;IACpC,CAAC;IAED;;;;;;;;;;;;;;;OAeG;IACH,KAAK;QACH,OAAO,IAAI,kCAAkB,CAAI,IAAI,CAAC,CAAC;IACzC,CAAC;CACF;AApSD,wCAoSC"}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core SQLiteVector database implementation with backend abstraction
|
|
3
|
+
* Supports both native (better-sqlite3) and WASM (sql.js) backends
|
|
4
|
+
*/
|
|
5
|
+
import { BackendType } from './backend-interface.mjs';
|
|
6
|
+
import { NativeBackend } from './native-backend.mjs';
|
|
7
|
+
import { WasmBackend } from './wasm-backend.mjs';
|
|
8
|
+
import { QueryCache } from '../cache/query-cache.mjs';
|
|
9
|
+
import { ProductQuantizer } from '../quantization/product-quantization.mjs';
|
|
10
|
+
import { VectorQueryBuilder } from '../query/query-builder.mjs';
|
|
11
|
+
export class SQLiteVectorDB {
|
|
12
|
+
constructor(config = {}) {
|
|
13
|
+
// If path is provided but memoryMode not specified, default to file mode
|
|
14
|
+
if (config.path && config.memoryMode === undefined) {
|
|
15
|
+
config.memoryMode = false;
|
|
16
|
+
}
|
|
17
|
+
// Determine backend type
|
|
18
|
+
this.backendType = this.detectBackend(config);
|
|
19
|
+
// Create appropriate backend
|
|
20
|
+
this.backend = this.createBackend(this.backendType);
|
|
21
|
+
// Initialize query cache if enabled
|
|
22
|
+
if (config.queryCache?.enabled !== false) {
|
|
23
|
+
this.queryCache = new QueryCache(config.queryCache);
|
|
24
|
+
}
|
|
25
|
+
// Initialize quantizer if enabled
|
|
26
|
+
if (config.quantization?.enabled) {
|
|
27
|
+
this.quantizer = new ProductQuantizer(config.quantization);
|
|
28
|
+
}
|
|
29
|
+
// Initialize backend
|
|
30
|
+
if (this.backendType === BackendType.WASM) {
|
|
31
|
+
// WASM backend requires async initialization
|
|
32
|
+
// Store config for later async init
|
|
33
|
+
this.backend._pendingConfig = config;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
// Native backend can initialize synchronously
|
|
37
|
+
this.backend.initialize(config);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Detect appropriate backend based on environment and configuration
|
|
42
|
+
*/
|
|
43
|
+
detectBackend(config) {
|
|
44
|
+
// Explicit backend selection
|
|
45
|
+
if (config.backend) {
|
|
46
|
+
return config.backend;
|
|
47
|
+
}
|
|
48
|
+
// Auto-detect based on environment
|
|
49
|
+
if (typeof window !== 'undefined' || typeof process === 'undefined') {
|
|
50
|
+
// Browser environment
|
|
51
|
+
return BackendType.WASM;
|
|
52
|
+
}
|
|
53
|
+
// Node.js environment - check if better-sqlite3 is available
|
|
54
|
+
try {
|
|
55
|
+
require.resolve('better-sqlite3');
|
|
56
|
+
return BackendType.NATIVE;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return BackendType.WASM;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create backend instance
|
|
64
|
+
*/
|
|
65
|
+
createBackend(type) {
|
|
66
|
+
switch (type) {
|
|
67
|
+
case BackendType.NATIVE:
|
|
68
|
+
return new NativeBackend();
|
|
69
|
+
case BackendType.WASM:
|
|
70
|
+
return new WasmBackend();
|
|
71
|
+
default:
|
|
72
|
+
throw new Error(`Unsupported backend type: ${type}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Async initialization for WASM backend
|
|
77
|
+
* Must be called after constructor if using WASM backend
|
|
78
|
+
*/
|
|
79
|
+
async initializeAsync(config) {
|
|
80
|
+
if (this.backendType === BackendType.WASM) {
|
|
81
|
+
const wasmBackend = this.backend;
|
|
82
|
+
const finalConfig = config || wasmBackend._pendingConfig || {};
|
|
83
|
+
await wasmBackend.initializeAsync(finalConfig);
|
|
84
|
+
delete wasmBackend._pendingConfig;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Get backend type
|
|
89
|
+
*/
|
|
90
|
+
getBackendType() {
|
|
91
|
+
return this.backendType;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Check if backend is initialized
|
|
95
|
+
*/
|
|
96
|
+
isInitialized() {
|
|
97
|
+
if (this.backendType === BackendType.WASM) {
|
|
98
|
+
return this.backend.isInitialized();
|
|
99
|
+
}
|
|
100
|
+
return this.backend.isInitialized();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Insert a single vector
|
|
104
|
+
*/
|
|
105
|
+
insert(vector) {
|
|
106
|
+
return this.backend.insert(vector);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Insert multiple vectors in a transaction
|
|
110
|
+
*/
|
|
111
|
+
insertBatch(vectors) {
|
|
112
|
+
return this.backend.insertBatch(vectors);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Search for k-nearest neighbors with optional caching
|
|
116
|
+
*/
|
|
117
|
+
search(queryEmbedding, k = 5, metric = 'cosine', threshold = 0.0) {
|
|
118
|
+
// Check cache if enabled
|
|
119
|
+
if (this.queryCache) {
|
|
120
|
+
const cacheKey = QueryCache.generateKey(queryEmbedding, k, metric, threshold);
|
|
121
|
+
const cached = this.queryCache.get(cacheKey);
|
|
122
|
+
if (cached) {
|
|
123
|
+
return cached;
|
|
124
|
+
}
|
|
125
|
+
// Cache miss - perform search and cache result
|
|
126
|
+
const results = this.backend.search(queryEmbedding, k, metric, threshold);
|
|
127
|
+
this.queryCache.set(cacheKey, results);
|
|
128
|
+
return results;
|
|
129
|
+
}
|
|
130
|
+
// No cache - perform search directly
|
|
131
|
+
return this.backend.search(queryEmbedding, k, metric, threshold);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get vector by ID
|
|
135
|
+
*/
|
|
136
|
+
get(id) {
|
|
137
|
+
return this.backend.get(id);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Delete vector by ID
|
|
141
|
+
*/
|
|
142
|
+
delete(id) {
|
|
143
|
+
return this.backend.delete(id);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Get database statistics
|
|
147
|
+
*/
|
|
148
|
+
stats() {
|
|
149
|
+
return this.backend.stats();
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Close database connection
|
|
153
|
+
*/
|
|
154
|
+
close() {
|
|
155
|
+
this.backend.close();
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Export database to binary format (WASM only)
|
|
159
|
+
*/
|
|
160
|
+
export() {
|
|
161
|
+
if (this.backend.export) {
|
|
162
|
+
return this.backend.export();
|
|
163
|
+
}
|
|
164
|
+
throw new Error('Export not supported by current backend');
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Import database from binary format (WASM only)
|
|
168
|
+
*/
|
|
169
|
+
async importAsync(data) {
|
|
170
|
+
if (this.backendType === BackendType.WASM) {
|
|
171
|
+
await this.backend.importAsync(data);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
throw new Error('Import not supported by native backend');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get raw backend instance (for advanced usage)
|
|
179
|
+
*/
|
|
180
|
+
getBackend() {
|
|
181
|
+
return this.backend;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Get raw database instance (for advanced usage with native backend)
|
|
185
|
+
* @deprecated Use getBackend() instead for cross-backend compatibility
|
|
186
|
+
*/
|
|
187
|
+
getDatabase() {
|
|
188
|
+
if (this.backendType === BackendType.NATIVE) {
|
|
189
|
+
return this.backend.getDatabase();
|
|
190
|
+
}
|
|
191
|
+
throw new Error('getDatabase() only supported on native backend. Use getBackend() instead.');
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Get query cache instance
|
|
195
|
+
*/
|
|
196
|
+
getQueryCache() {
|
|
197
|
+
return this.queryCache;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get query cache statistics
|
|
201
|
+
*/
|
|
202
|
+
getCacheStats() {
|
|
203
|
+
return this.queryCache?.getStats();
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Clear query cache
|
|
207
|
+
*/
|
|
208
|
+
clearCache() {
|
|
209
|
+
this.queryCache?.clear();
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Get quantizer instance
|
|
213
|
+
*/
|
|
214
|
+
getQuantizer() {
|
|
215
|
+
return this.quantizer;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Train quantizer with existing vectors
|
|
219
|
+
*/
|
|
220
|
+
async trainQuantizer() {
|
|
221
|
+
if (!this.quantizer) {
|
|
222
|
+
throw new Error('Quantization not enabled');
|
|
223
|
+
}
|
|
224
|
+
// Get all vectors from database
|
|
225
|
+
const allStats = this.stats();
|
|
226
|
+
if (allStats.count === 0) {
|
|
227
|
+
throw new Error('No vectors to train quantizer');
|
|
228
|
+
}
|
|
229
|
+
// Fetch all embeddings (simplified - in production, batch this)
|
|
230
|
+
const vectors = [];
|
|
231
|
+
// Note: This would need backend support to efficiently fetch all embeddings
|
|
232
|
+
console.log('Training quantizer with existing vectors...');
|
|
233
|
+
await this.quantizer.train(vectors);
|
|
234
|
+
console.log('Quantizer training complete');
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Get compression statistics (if quantization enabled)
|
|
238
|
+
*/
|
|
239
|
+
getCompressionStats() {
|
|
240
|
+
return this.quantizer?.getStats();
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Create a fluent query builder for complex queries
|
|
244
|
+
*
|
|
245
|
+
* @returns VectorQueryBuilder instance
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```typescript
|
|
249
|
+
* const results = await db.query()
|
|
250
|
+
* .similarTo(queryVector)
|
|
251
|
+
* .where('metadata.category', '=', 'tech')
|
|
252
|
+
* .whereBetween('metadata.year', 2020, 2024)
|
|
253
|
+
* .orderBySimilarity('desc')
|
|
254
|
+
* .limit(10)
|
|
255
|
+
* .execute();
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
query() {
|
|
259
|
+
return new VectorQueryBuilder(this);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM backend implementation using sql.js
|
|
3
|
+
* Provides browser-compatible vector database operations
|
|
4
|
+
*/
|
|
5
|
+
import { Vector, SearchResult, SimilarityMetric, DatabaseConfig } from '../types';
|
|
6
|
+
import { VectorBackend } from './backend-interface';
|
|
7
|
+
/**
|
|
8
|
+
* WASM backend using sql.js
|
|
9
|
+
* Runs in both Node.js and browser environments
|
|
10
|
+
*/
|
|
11
|
+
export declare class WasmBackend implements VectorBackend {
|
|
12
|
+
private SQL;
|
|
13
|
+
private db;
|
|
14
|
+
private initialized;
|
|
15
|
+
/**
|
|
16
|
+
* Initialize the WASM backend
|
|
17
|
+
*/
|
|
18
|
+
initializeAsync(config?: DatabaseConfig): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Synchronous initialization (throws if not already initialized)
|
|
21
|
+
*/
|
|
22
|
+
initialize(config?: DatabaseConfig): void;
|
|
23
|
+
/**
|
|
24
|
+
* Initialize database schema
|
|
25
|
+
*/
|
|
26
|
+
private initializeSchema;
|
|
27
|
+
/**
|
|
28
|
+
* Register custom SQL functions for similarity calculations
|
|
29
|
+
*/
|
|
30
|
+
private registerCustomFunctions;
|
|
31
|
+
/**
|
|
32
|
+
* Calculate L2 norm of a vector
|
|
33
|
+
*/
|
|
34
|
+
private calculateNorm;
|
|
35
|
+
/**
|
|
36
|
+
* Serialize embedding to Uint8Array (binary format)
|
|
37
|
+
*/
|
|
38
|
+
private serializeEmbedding;
|
|
39
|
+
/**
|
|
40
|
+
* Deserialize embedding from Uint8Array
|
|
41
|
+
*/
|
|
42
|
+
private deserializeEmbedding;
|
|
43
|
+
/**
|
|
44
|
+
* Generate unique ID
|
|
45
|
+
*/
|
|
46
|
+
private generateId;
|
|
47
|
+
/**
|
|
48
|
+
* Insert a single vector
|
|
49
|
+
*/
|
|
50
|
+
insert(vector: Vector): string;
|
|
51
|
+
/**
|
|
52
|
+
* Insert multiple vectors in a transaction with optimized batch processing
|
|
53
|
+
* Performance improvements:
|
|
54
|
+
* - Single transaction for all inserts
|
|
55
|
+
* - Prepared statement created once and reused
|
|
56
|
+
* - No wrapper function overhead
|
|
57
|
+
* - Chunked processing for large batches
|
|
58
|
+
*/
|
|
59
|
+
insertBatch(vectors: Vector[]): string[];
|
|
60
|
+
/**
|
|
61
|
+
* Search for k-nearest neighbors
|
|
62
|
+
*/
|
|
63
|
+
search(queryEmbedding: number[], k?: number, metric?: SimilarityMetric, threshold?: number): SearchResult[];
|
|
64
|
+
/**
|
|
65
|
+
* Get vector by ID
|
|
66
|
+
*/
|
|
67
|
+
get(id: string): Vector | null;
|
|
68
|
+
/**
|
|
69
|
+
* Delete vector by ID
|
|
70
|
+
*/
|
|
71
|
+
delete(id: string): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Get database statistics
|
|
74
|
+
*/
|
|
75
|
+
stats(): {
|
|
76
|
+
count: number;
|
|
77
|
+
size: number;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Export database to binary format (for persistence)
|
|
81
|
+
*/
|
|
82
|
+
export(): Uint8Array;
|
|
83
|
+
/**
|
|
84
|
+
* Import database from binary format
|
|
85
|
+
*/
|
|
86
|
+
importAsync(data: Uint8Array): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Close database and cleanup resources
|
|
89
|
+
*/
|
|
90
|
+
close(): void;
|
|
91
|
+
/**
|
|
92
|
+
* Check if backend is initialized
|
|
93
|
+
*/
|
|
94
|
+
isInitialized(): boolean;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=wasm-backend.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm-backend.d.ts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