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,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM backend implementation using sql.js
|
|
3
|
+
* Provides browser-compatible vector database operations
|
|
4
|
+
*/
|
|
5
|
+
import initSqlJs from 'sql.js';
|
|
6
|
+
/**
|
|
7
|
+
* WASM backend using sql.js
|
|
8
|
+
* Runs in both Node.js and browser environments
|
|
9
|
+
*/
|
|
10
|
+
export class WasmBackend {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.SQL = null;
|
|
13
|
+
this.db = null;
|
|
14
|
+
this.initialized = false;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Initialize the WASM backend
|
|
18
|
+
*/
|
|
19
|
+
async initializeAsync(config = {}) {
|
|
20
|
+
if (this.initialized) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
// Load sql.js WASM module
|
|
24
|
+
this.SQL = await initSqlJs({
|
|
25
|
+
// Use CDN for WASM binary in browser, local file in Node.js
|
|
26
|
+
locateFile: (file) => {
|
|
27
|
+
if (typeof window !== 'undefined') {
|
|
28
|
+
return `https://sql.js.org/dist/${file}`;
|
|
29
|
+
}
|
|
30
|
+
return `node_modules/sql.js/dist/${file}`;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
// Create database
|
|
34
|
+
this.db = new this.SQL.Database();
|
|
35
|
+
// Initialize schema and functions
|
|
36
|
+
this.initializeSchema(config);
|
|
37
|
+
this.registerCustomFunctions();
|
|
38
|
+
this.initialized = true;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Synchronous initialization (throws if not already initialized)
|
|
42
|
+
*/
|
|
43
|
+
initialize(config = {}) {
|
|
44
|
+
if (!this.initialized) {
|
|
45
|
+
throw new Error('WASM backend must be initialized asynchronously. Call initializeAsync() first.');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Initialize database schema
|
|
50
|
+
*/
|
|
51
|
+
initializeSchema(config) {
|
|
52
|
+
if (!this.db)
|
|
53
|
+
throw new Error('Database not initialized');
|
|
54
|
+
const { cacheSize = 100 * 1024, walMode = false, // WAL not supported in sql.js
|
|
55
|
+
mmapSize = 256 * 1024 * 1024 } = config;
|
|
56
|
+
// Configure SQLite (limited options in sql.js)
|
|
57
|
+
this.db.run('PRAGMA journal_mode = MEMORY');
|
|
58
|
+
this.db.run('PRAGMA synchronous = OFF'); // Safe for in-memory
|
|
59
|
+
this.db.run(`PRAGMA cache_size = -${cacheSize}`);
|
|
60
|
+
this.db.run('PRAGMA temp_store = MEMORY');
|
|
61
|
+
this.db.run('PRAGMA page_size = 4096');
|
|
62
|
+
// Create vectors table
|
|
63
|
+
this.db.run(`
|
|
64
|
+
CREATE TABLE IF NOT EXISTS vectors (
|
|
65
|
+
id TEXT PRIMARY KEY,
|
|
66
|
+
embedding BLOB NOT NULL,
|
|
67
|
+
norm REAL NOT NULL,
|
|
68
|
+
metadata TEXT,
|
|
69
|
+
timestamp INTEGER NOT NULL
|
|
70
|
+
)
|
|
71
|
+
`);
|
|
72
|
+
// Create indexes
|
|
73
|
+
this.db.run('CREATE INDEX IF NOT EXISTS idx_vectors_norm ON vectors(norm)');
|
|
74
|
+
this.db.run('CREATE INDEX IF NOT EXISTS idx_vectors_timestamp ON vectors(timestamp)');
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Register custom SQL functions for similarity calculations
|
|
78
|
+
*/
|
|
79
|
+
registerCustomFunctions() {
|
|
80
|
+
if (!this.db)
|
|
81
|
+
throw new Error('Database not initialized');
|
|
82
|
+
// Cosine similarity function
|
|
83
|
+
this.db.create_function('cosine_similarity', (embeddingA, embeddingB, normA, normB) => {
|
|
84
|
+
const arrA = new Float32Array(embeddingA.buffer, embeddingA.byteOffset, embeddingA.byteLength / 4);
|
|
85
|
+
const arrB = new Float32Array(embeddingB.buffer, embeddingB.byteOffset, embeddingB.byteLength / 4);
|
|
86
|
+
let dotProduct = 0;
|
|
87
|
+
const len = Math.min(arrA.length, arrB.length);
|
|
88
|
+
for (let i = 0; i < len; i++) {
|
|
89
|
+
dotProduct += arrA[i] * arrB[i];
|
|
90
|
+
}
|
|
91
|
+
return dotProduct / (normA * normB);
|
|
92
|
+
});
|
|
93
|
+
// Euclidean distance function
|
|
94
|
+
this.db.create_function('euclidean_distance', (embeddingA, embeddingB) => {
|
|
95
|
+
const arrA = new Float32Array(embeddingA.buffer, embeddingA.byteOffset, embeddingA.byteLength / 4);
|
|
96
|
+
const arrB = new Float32Array(embeddingB.buffer, embeddingB.byteOffset, embeddingB.byteLength / 4);
|
|
97
|
+
let sum = 0;
|
|
98
|
+
const len = Math.min(arrA.length, arrB.length);
|
|
99
|
+
for (let i = 0; i < len; i++) {
|
|
100
|
+
const diff = arrA[i] - arrB[i];
|
|
101
|
+
sum += diff * diff;
|
|
102
|
+
}
|
|
103
|
+
return Math.sqrt(sum);
|
|
104
|
+
});
|
|
105
|
+
// Dot product function
|
|
106
|
+
this.db.create_function('dot_product', (embeddingA, embeddingB) => {
|
|
107
|
+
const arrA = new Float32Array(embeddingA.buffer, embeddingA.byteOffset, embeddingA.byteLength / 4);
|
|
108
|
+
const arrB = new Float32Array(embeddingB.buffer, embeddingB.byteOffset, embeddingB.byteLength / 4);
|
|
109
|
+
let dotProduct = 0;
|
|
110
|
+
const len = Math.min(arrA.length, arrB.length);
|
|
111
|
+
for (let i = 0; i < len; i++) {
|
|
112
|
+
dotProduct += arrA[i] * arrB[i];
|
|
113
|
+
}
|
|
114
|
+
return dotProduct;
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Calculate L2 norm of a vector
|
|
119
|
+
*/
|
|
120
|
+
calculateNorm(embedding) {
|
|
121
|
+
let sum = 0;
|
|
122
|
+
for (const val of embedding) {
|
|
123
|
+
sum += val * val;
|
|
124
|
+
}
|
|
125
|
+
return Math.sqrt(sum);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Serialize embedding to Uint8Array (binary format)
|
|
129
|
+
*/
|
|
130
|
+
serializeEmbedding(embedding) {
|
|
131
|
+
const buffer = new ArrayBuffer(embedding.length * 4);
|
|
132
|
+
const view = new Float32Array(buffer);
|
|
133
|
+
view.set(embedding);
|
|
134
|
+
return new Uint8Array(buffer);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Deserialize embedding from Uint8Array
|
|
138
|
+
*/
|
|
139
|
+
deserializeEmbedding(buffer) {
|
|
140
|
+
const view = new Float32Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 4);
|
|
141
|
+
return Array.from(view);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Generate unique ID
|
|
145
|
+
*/
|
|
146
|
+
generateId() {
|
|
147
|
+
return `vec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Insert a single vector
|
|
151
|
+
*/
|
|
152
|
+
insert(vector) {
|
|
153
|
+
if (!this.db)
|
|
154
|
+
throw new Error('Database not initialized');
|
|
155
|
+
const id = vector.id || this.generateId();
|
|
156
|
+
const norm = this.calculateNorm(vector.embedding);
|
|
157
|
+
const embedding = this.serializeEmbedding(vector.embedding);
|
|
158
|
+
const metadata = vector.metadata ? JSON.stringify(vector.metadata) : null;
|
|
159
|
+
const timestamp = vector.timestamp || Date.now();
|
|
160
|
+
this.db.run('INSERT OR REPLACE INTO vectors (id, embedding, norm, metadata, timestamp) VALUES (?, ?, ?, ?, ?)', [id, embedding, norm, metadata, timestamp]);
|
|
161
|
+
return id;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Insert multiple vectors in a transaction with optimized batch processing
|
|
165
|
+
* Performance improvements:
|
|
166
|
+
* - Single transaction for all inserts
|
|
167
|
+
* - Prepared statement created once and reused
|
|
168
|
+
* - No wrapper function overhead
|
|
169
|
+
* - Chunked processing for large batches
|
|
170
|
+
*/
|
|
171
|
+
insertBatch(vectors) {
|
|
172
|
+
if (!this.db)
|
|
173
|
+
throw new Error('Database not initialized');
|
|
174
|
+
const ids = [];
|
|
175
|
+
const CHUNK_SIZE = 5000; // Process in chunks to avoid memory spikes
|
|
176
|
+
// Helper function to process a chunk of vectors
|
|
177
|
+
const processChunk = (chunk) => {
|
|
178
|
+
const chunkIds = [];
|
|
179
|
+
this.db.run('BEGIN TRANSACTION');
|
|
180
|
+
try {
|
|
181
|
+
// Prepare statement once for all inserts in this chunk
|
|
182
|
+
const stmt = this.db.prepare('INSERT OR REPLACE INTO vectors (id, embedding, norm, metadata, timestamp) VALUES (?, ?, ?, ?, ?)');
|
|
183
|
+
try {
|
|
184
|
+
// Reuse the prepared statement for all inserts
|
|
185
|
+
for (const vector of chunk) {
|
|
186
|
+
const id = vector.id || this.generateId();
|
|
187
|
+
const norm = this.calculateNorm(vector.embedding);
|
|
188
|
+
const embedding = this.serializeEmbedding(vector.embedding);
|
|
189
|
+
const metadata = vector.metadata ? JSON.stringify(vector.metadata) : null;
|
|
190
|
+
const timestamp = vector.timestamp || Date.now();
|
|
191
|
+
stmt.bind([id, embedding, norm, metadata, timestamp]);
|
|
192
|
+
stmt.step();
|
|
193
|
+
stmt.reset();
|
|
194
|
+
chunkIds.push(id);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
stmt.free();
|
|
199
|
+
}
|
|
200
|
+
this.db.run('COMMIT');
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
this.db.run('ROLLBACK');
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
return chunkIds;
|
|
207
|
+
};
|
|
208
|
+
// Process vectors in chunks
|
|
209
|
+
if (vectors.length <= CHUNK_SIZE) {
|
|
210
|
+
// Small batch - process directly
|
|
211
|
+
ids.push(...processChunk(vectors));
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
// Large batch - process in chunks to manage memory
|
|
215
|
+
for (let i = 0; i < vectors.length; i += CHUNK_SIZE) {
|
|
216
|
+
const chunk = vectors.slice(i, i + CHUNK_SIZE);
|
|
217
|
+
ids.push(...processChunk(chunk));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return ids;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Search for k-nearest neighbors
|
|
224
|
+
*/
|
|
225
|
+
search(queryEmbedding, k = 5, metric = 'cosine', threshold = 0.0) {
|
|
226
|
+
if (!this.db)
|
|
227
|
+
throw new Error('Database not initialized');
|
|
228
|
+
const queryBuffer = this.serializeEmbedding(queryEmbedding);
|
|
229
|
+
const queryNorm = this.calculateNorm(queryEmbedding);
|
|
230
|
+
let sql;
|
|
231
|
+
let params;
|
|
232
|
+
switch (metric) {
|
|
233
|
+
case 'cosine':
|
|
234
|
+
sql = `
|
|
235
|
+
SELECT
|
|
236
|
+
id,
|
|
237
|
+
embedding,
|
|
238
|
+
metadata,
|
|
239
|
+
cosine_similarity(embedding, ?, norm, ?) as score
|
|
240
|
+
FROM vectors
|
|
241
|
+
WHERE cosine_similarity(embedding, ?, norm, ?) >= ?
|
|
242
|
+
ORDER BY score DESC
|
|
243
|
+
LIMIT ?
|
|
244
|
+
`;
|
|
245
|
+
params = [queryBuffer, queryNorm, queryBuffer, queryNorm, threshold, k];
|
|
246
|
+
break;
|
|
247
|
+
case 'euclidean':
|
|
248
|
+
sql = `
|
|
249
|
+
SELECT
|
|
250
|
+
id,
|
|
251
|
+
embedding,
|
|
252
|
+
metadata,
|
|
253
|
+
euclidean_distance(embedding, ?) as score
|
|
254
|
+
FROM vectors
|
|
255
|
+
WHERE euclidean_distance(embedding, ?) <= ? OR ? = 0
|
|
256
|
+
ORDER BY score ASC
|
|
257
|
+
LIMIT ?
|
|
258
|
+
`;
|
|
259
|
+
params = [queryBuffer, queryBuffer, threshold, threshold, k];
|
|
260
|
+
break;
|
|
261
|
+
case 'dot':
|
|
262
|
+
sql = `
|
|
263
|
+
SELECT
|
|
264
|
+
id,
|
|
265
|
+
embedding,
|
|
266
|
+
metadata,
|
|
267
|
+
dot_product(embedding, ?) as score
|
|
268
|
+
FROM vectors
|
|
269
|
+
WHERE dot_product(embedding, ?) >= ?
|
|
270
|
+
ORDER BY score DESC
|
|
271
|
+
LIMIT ?
|
|
272
|
+
`;
|
|
273
|
+
params = [queryBuffer, queryBuffer, threshold, k];
|
|
274
|
+
break;
|
|
275
|
+
default:
|
|
276
|
+
throw new Error(`Unsupported similarity metric: ${metric}`);
|
|
277
|
+
}
|
|
278
|
+
const stmt = this.db.prepare(sql);
|
|
279
|
+
stmt.bind(params);
|
|
280
|
+
const results = [];
|
|
281
|
+
while (stmt.step()) {
|
|
282
|
+
const row = stmt.getAsObject();
|
|
283
|
+
results.push({
|
|
284
|
+
id: row.id,
|
|
285
|
+
score: row.score,
|
|
286
|
+
embedding: this.deserializeEmbedding(row.embedding),
|
|
287
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : undefined
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
stmt.free();
|
|
291
|
+
return results;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Get vector by ID
|
|
295
|
+
*/
|
|
296
|
+
get(id) {
|
|
297
|
+
if (!this.db)
|
|
298
|
+
throw new Error('Database not initialized');
|
|
299
|
+
const stmt = this.db.prepare('SELECT * FROM vectors WHERE id = ?');
|
|
300
|
+
stmt.bind([id]);
|
|
301
|
+
if (!stmt.step()) {
|
|
302
|
+
stmt.free();
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
const row = stmt.getAsObject();
|
|
306
|
+
stmt.free();
|
|
307
|
+
return {
|
|
308
|
+
id: row.id,
|
|
309
|
+
embedding: this.deserializeEmbedding(row.embedding),
|
|
310
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : undefined,
|
|
311
|
+
norm: row.norm,
|
|
312
|
+
timestamp: row.timestamp
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Delete vector by ID
|
|
317
|
+
*/
|
|
318
|
+
delete(id) {
|
|
319
|
+
if (!this.db)
|
|
320
|
+
throw new Error('Database not initialized');
|
|
321
|
+
const stmt = this.db.prepare('DELETE FROM vectors WHERE id = ?');
|
|
322
|
+
stmt.bind([id]);
|
|
323
|
+
stmt.step();
|
|
324
|
+
const changes = this.db.getRowsModified();
|
|
325
|
+
stmt.free();
|
|
326
|
+
return changes > 0;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Get database statistics
|
|
330
|
+
*/
|
|
331
|
+
stats() {
|
|
332
|
+
if (!this.db)
|
|
333
|
+
throw new Error('Database not initialized');
|
|
334
|
+
// Get count
|
|
335
|
+
const countStmt = this.db.prepare('SELECT COUNT(*) as count FROM vectors');
|
|
336
|
+
countStmt.step();
|
|
337
|
+
const countRow = countStmt.getAsObject();
|
|
338
|
+
const count = countRow.count;
|
|
339
|
+
countStmt.free();
|
|
340
|
+
// Get size (approximate from exported binary)
|
|
341
|
+
const exported = this.db.export();
|
|
342
|
+
const size = exported.byteLength;
|
|
343
|
+
return { count, size };
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Export database to binary format (for persistence)
|
|
347
|
+
*/
|
|
348
|
+
export() {
|
|
349
|
+
if (!this.db)
|
|
350
|
+
throw new Error('Database not initialized');
|
|
351
|
+
return this.db.export();
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Import database from binary format
|
|
355
|
+
*/
|
|
356
|
+
async importAsync(data) {
|
|
357
|
+
if (!this.SQL) {
|
|
358
|
+
throw new Error('SQL.js not initialized');
|
|
359
|
+
}
|
|
360
|
+
// Close existing database
|
|
361
|
+
if (this.db) {
|
|
362
|
+
this.db.close();
|
|
363
|
+
}
|
|
364
|
+
// Load from binary data
|
|
365
|
+
this.db = new this.SQL.Database(data);
|
|
366
|
+
this.registerCustomFunctions();
|
|
367
|
+
this.initialized = true;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Close database and cleanup resources
|
|
371
|
+
*/
|
|
372
|
+
close() {
|
|
373
|
+
if (this.db) {
|
|
374
|
+
this.db.close();
|
|
375
|
+
this.db = null;
|
|
376
|
+
}
|
|
377
|
+
this.initialized = false;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Check if backend is initialized
|
|
381
|
+
*/
|
|
382
|
+
isInitialized() {
|
|
383
|
+
return this.initialized;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optimized HNSW Implementation with In-Memory Build Cache
|
|
3
|
+
*
|
|
4
|
+
* Key optimizations:
|
|
5
|
+
* 1. In-memory graph during build (avoid DB queries)
|
|
6
|
+
* 2. Bulk database writes (batch all edges at once)
|
|
7
|
+
* 3. Lazy persistence (write to DB only after build complete)
|
|
8
|
+
* 4. Distance calculation caching
|
|
9
|
+
* 5. Optimized neighbor selection
|
|
10
|
+
*
|
|
11
|
+
* Target: <10ms per vector build time
|
|
12
|
+
*/
|
|
13
|
+
import Database from 'better-sqlite3';
|
|
14
|
+
import { HNSWConfig } from './hnsw';
|
|
15
|
+
/**
|
|
16
|
+
* Optimized HNSW Index with in-memory build cache
|
|
17
|
+
*/
|
|
18
|
+
export declare class OptimizedHNSWIndex {
|
|
19
|
+
private db;
|
|
20
|
+
private config;
|
|
21
|
+
private entryPoint;
|
|
22
|
+
private maxLevel;
|
|
23
|
+
private isBuilt;
|
|
24
|
+
private nodeCache;
|
|
25
|
+
private edgeCache;
|
|
26
|
+
private buildMode;
|
|
27
|
+
private insertNodeStmt;
|
|
28
|
+
private insertEdgeStmt;
|
|
29
|
+
private getNodeStmt;
|
|
30
|
+
private getNeighborsStmt;
|
|
31
|
+
constructor(db: Database.Database, config?: Partial<HNSWConfig>);
|
|
32
|
+
private initializeSchema;
|
|
33
|
+
private prepareStatements;
|
|
34
|
+
private loadMetadata;
|
|
35
|
+
private saveMetadata;
|
|
36
|
+
private randomLevel;
|
|
37
|
+
private calculateDistance;
|
|
38
|
+
private serializeEmbedding;
|
|
39
|
+
private deserializeEmbedding;
|
|
40
|
+
/**
|
|
41
|
+
* Get node from cache (build mode) or database
|
|
42
|
+
*/
|
|
43
|
+
private getNode;
|
|
44
|
+
/**
|
|
45
|
+
* Get neighbors from cache (build mode) or database
|
|
46
|
+
*/
|
|
47
|
+
private getNeighbors;
|
|
48
|
+
/**
|
|
49
|
+
* Add edge to cache (build mode) or database
|
|
50
|
+
*/
|
|
51
|
+
private addEdge;
|
|
52
|
+
private searchLayer;
|
|
53
|
+
private selectNeighborsHeuristic;
|
|
54
|
+
/**
|
|
55
|
+
* Insert node into in-memory graph (build mode only)
|
|
56
|
+
*/
|
|
57
|
+
private insertToMemory;
|
|
58
|
+
/**
|
|
59
|
+
* Flush in-memory graph to database (bulk write)
|
|
60
|
+
*/
|
|
61
|
+
private flushToDatabase;
|
|
62
|
+
/**
|
|
63
|
+
* Build index with in-memory optimization
|
|
64
|
+
*/
|
|
65
|
+
buildOptimized(): void;
|
|
66
|
+
/**
|
|
67
|
+
* Search using HNSW index
|
|
68
|
+
*/
|
|
69
|
+
search(query: number[], k: number): any[];
|
|
70
|
+
clear(): void;
|
|
71
|
+
getStats(): any;
|
|
72
|
+
isReady(): boolean;
|
|
73
|
+
getConfig(): HNSWConfig;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=hnsw-optimized.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hnsw-optimized.d.ts","sourceRoot":"","sources":["../../src/index/hnsw-optimized.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,UAAU,EAAuB,MAAM,QAAQ,CAAC;AAqBzD;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,EAAE,CAAoB;IAC9B,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,QAAQ,CAAa;IAC7B,OAAO,CAAC,OAAO,CAAkB;IAGjC,OAAO,CAAC,SAAS,CAAoC;IACrD,OAAO,CAAC,SAAS,CAA0D;IAC3E,OAAO,CAAC,SAAS,CAAkB;IAGnC,OAAO,CAAC,cAAc,CAAsB;IAC5C,OAAO,CAAC,cAAc,CAAsB;IAC5C,OAAO,CAAC,WAAW,CAAsB;IACzC,OAAO,CAAC,gBAAgB,CAAsB;gBAElC,EAAE,EAAE,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAE,OAAO,CAAC,UAAU,CAAM;IAQnE,OAAO,CAAC,gBAAgB;IA8BxB,OAAO,CAAC,iBAAiB;IAyBzB,OAAO,CAAC,YAAY;IAapB,OAAO,CAAC,YAAY;IAOpB,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,iBAAiB;IASzB,OAAO,CAAC,kBAAkB;IAO1B,OAAO,CAAC,oBAAoB;IAK5B;;OAEG;IACH,OAAO,CAAC,OAAO;IAgBf;;OAEG;IACH,OAAO,CAAC,YAAY;IAUpB;;OAEG;IACH,OAAO,CAAC,OAAO;IAyBf,OAAO,CAAC,WAAW;IAwDnB,OAAO,CAAC,wBAAwB;IAKhC;;OAEG;IACH,OAAO,CAAC,cAAc;IAkEtB;;OAEG;IACH,OAAO,CAAC,eAAe;IAuCvB;;OAEG;IACH,cAAc,IAAI,IAAI;IA6CtB;;OAEG;IACH,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,GAAG,EAAE;IAwBzC,KAAK,IAAI,IAAI;IAYb,QAAQ,IAAI,GAAG;IAgBf,OAAO,IAAI,OAAO;IAIlB,SAAS,IAAI,UAAU;CAGxB"}
|