agentdb 1.0.12 → 1.1.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/README.md +196 -838
- package/dist/benchmarks/comprehensive-benchmark.js +664 -0
- package/dist/benchmarks/frontier-benchmark.js +419 -0
- package/dist/benchmarks/reflexion-benchmark.js +370 -0
- package/dist/cli/agentdb-cli.d.ts +117 -0
- package/dist/cli/agentdb-cli.d.ts.map +1 -0
- package/dist/cli/agentdb-cli.js +718 -0
- package/dist/cli/agentdb-cli.js.map +1 -0
- package/dist/controllers/CausalMemoryGraph.d.ts +125 -0
- package/dist/controllers/CausalMemoryGraph.d.ts.map +1 -0
- package/dist/controllers/CausalMemoryGraph.js +323 -0
- package/dist/controllers/CausalMemoryGraph.js.map +1 -0
- package/dist/controllers/CausalRecall.d.ts +111 -0
- package/dist/controllers/CausalRecall.d.ts.map +1 -0
- package/dist/controllers/CausalRecall.js +282 -0
- package/dist/controllers/CausalRecall.js.map +1 -0
- package/dist/controllers/EmbeddingService.d.ts +37 -0
- package/dist/controllers/EmbeddingService.d.ts.map +1 -0
- package/dist/controllers/EmbeddingService.js +119 -0
- package/dist/controllers/EmbeddingService.js.map +1 -0
- package/dist/controllers/ExplainableRecall.d.ts +138 -0
- package/dist/controllers/ExplainableRecall.d.ts.map +1 -0
- package/dist/controllers/ExplainableRecall.js +388 -0
- package/dist/controllers/ExplainableRecall.js.map +1 -0
- package/dist/controllers/NightlyLearner.d.ts +113 -0
- package/dist/controllers/NightlyLearner.d.ts.map +1 -0
- package/dist/controllers/NightlyLearner.js +383 -0
- package/dist/controllers/NightlyLearner.js.map +1 -0
- package/dist/controllers/ReflexionMemory.d.ts +84 -0
- package/dist/controllers/ReflexionMemory.d.ts.map +1 -0
- package/dist/controllers/ReflexionMemory.js +240 -0
- package/dist/controllers/ReflexionMemory.js.map +1 -0
- package/dist/controllers/SkillLibrary.d.ts +96 -0
- package/dist/controllers/SkillLibrary.d.ts.map +1 -0
- package/dist/controllers/SkillLibrary.js +277 -0
- package/dist/controllers/SkillLibrary.js.map +1 -0
- package/dist/controllers/frontier-index.d.ts +14 -0
- package/dist/controllers/frontier-index.d.ts.map +1 -0
- package/dist/controllers/frontier-index.js +10 -0
- package/dist/controllers/frontier-index.js.map +1 -0
- package/dist/controllers/index.d.ts +12 -0
- package/dist/controllers/index.d.ts.map +1 -0
- package/dist/controllers/index.js +9 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/optimizations/BatchOperations.d.ts +61 -0
- package/dist/optimizations/BatchOperations.d.ts.map +1 -0
- package/dist/optimizations/BatchOperations.js +199 -0
- package/dist/optimizations/BatchOperations.js.map +1 -0
- package/dist/optimizations/QueryOptimizer.d.ts +82 -0
- package/dist/optimizations/QueryOptimizer.d.ts.map +1 -0
- package/dist/optimizations/QueryOptimizer.js +226 -0
- package/dist/optimizations/QueryOptimizer.js.map +1 -0
- package/dist/optimizations/index.d.ts +10 -0
- package/dist/optimizations/index.d.ts.map +1 -0
- package/dist/optimizations/index.js +8 -0
- package/dist/optimizations/index.js.map +1 -0
- package/dist/schemas/frontier-schema.sql +341 -0
- package/dist/schemas/schema.sql +382 -0
- package/dist/tests/frontier-features.test.js +665 -0
- package/package.json +54 -117
- package/src/cli/agentdb-cli.ts +861 -0
- package/src/cli/examples.sh +83 -0
- package/src/controllers/CausalMemoryGraph.ts +504 -0
- package/src/controllers/CausalRecall.ts +395 -0
- package/src/controllers/EmbeddingService.ts +141 -0
- package/src/controllers/ExplainableRecall.ts +577 -0
- package/src/controllers/NightlyLearner.ts +475 -0
- package/src/controllers/ReflexionMemory.ts +349 -0
- package/src/controllers/SkillLibrary.ts +391 -0
- package/src/controllers/frontier-index.ts +35 -0
- package/src/controllers/index.ts +13 -0
- package/src/optimizations/BatchOperations.ts +292 -0
- package/src/optimizations/QueryOptimizer.ts +294 -0
- package/src/optimizations/index.ts +11 -0
- package/src/schemas/frontier-schema.sql +341 -0
- package/src/schemas/schema.sql +382 -0
- package/CHANGELOG.md +0 -166
- package/LICENSE +0 -38
- package/LICENSE-APACHE +0 -190
- package/LICENSE-MIT +0 -21
- package/bin/agentdb.js +0 -733
- package/bin/benchmark.js +0 -165
- package/bin/plugin-cli-wrapper.mjs +0 -21
- package/dist/agentdb.js +0 -5352
- package/dist/agentdb.js.map +0 -7
- package/dist/agentdb.min.js +0 -143
- package/dist/agentdb.min.js.map +0 -7
- package/dist/cache/query-cache.d.ts +0 -105
- package/dist/cache/query-cache.d.ts.map +0 -1
- package/dist/cache/query-cache.js +0 -224
- package/dist/cache/query-cache.js.map +0 -1
- package/dist/cache/query-cache.mjs +0 -219
- package/dist/cli/cache/query-cache.d.ts +0 -104
- package/dist/cli/cache/query-cache.js +0 -244
- package/dist/cli/cli/db-commands.d.ts +0 -48
- package/dist/cli/cli/db-commands.js +0 -613
- package/dist/cli/commands.d.ts +0 -7
- package/dist/cli/commands.d.ts.map +0 -1
- package/dist/cli/commands.js +0 -113
- package/dist/cli/commands.js.map +0 -1
- package/dist/cli/commands.mjs +0 -104
- package/dist/cli/core/backend-interface.d.ts +0 -70
- package/dist/cli/core/backend-interface.js +0 -15
- package/dist/cli/core/native-backend.d.ts +0 -140
- package/dist/cli/core/native-backend.js +0 -432
- package/dist/cli/core/vector-db.d.ts +0 -126
- package/dist/cli/core/vector-db.js +0 -338
- package/dist/cli/core/wasm-backend.d.ts +0 -95
- package/dist/cli/core/wasm-backend.js +0 -418
- package/dist/cli/db-commands.d.ts +0 -49
- package/dist/cli/db-commands.d.ts.map +0 -1
- package/dist/cli/db-commands.js +0 -533
- package/dist/cli/db-commands.js.map +0 -1
- package/dist/cli/db-commands.mjs +0 -522
- package/dist/cli/generator.d.ts +0 -11
- package/dist/cli/generator.d.ts.map +0 -1
- package/dist/cli/generator.js +0 -567
- package/dist/cli/generator.js.map +0 -1
- package/dist/cli/generator.mjs +0 -527
- package/dist/cli/help.d.ts +0 -18
- package/dist/cli/help.d.ts.map +0 -1
- package/dist/cli/help.js +0 -676
- package/dist/cli/help.js.map +0 -1
- package/dist/cli/help.mjs +0 -667
- package/dist/cli/index/hnsw.d.ts +0 -164
- package/dist/cli/index/hnsw.js +0 -558
- package/dist/cli/plugin-cli.d.ts +0 -7
- package/dist/cli/plugin-cli.d.ts.map +0 -1
- package/dist/cli/plugin-cli.js +0 -295
- package/dist/cli/plugin-cli.js.map +0 -1
- package/dist/cli/plugin-cli.mjs +0 -289
- package/dist/cli/quantization/product-quantization.d.ts +0 -108
- package/dist/cli/quantization/product-quantization.js +0 -350
- package/dist/cli/query/query-builder.d.ts +0 -322
- package/dist/cli/query/query-builder.js +0 -600
- package/dist/cli/templates.d.ts +0 -14
- package/dist/cli/templates.d.ts.map +0 -1
- package/dist/cli/templates.js +0 -182
- package/dist/cli/templates.js.map +0 -1
- package/dist/cli/templates.mjs +0 -176
- package/dist/cli/types/index.d.ts +0 -116
- package/dist/cli/types/index.js +0 -5
- package/dist/cli/types.d.ts +0 -91
- package/dist/cli/types.d.ts.map +0 -1
- package/dist/cli/types.js +0 -6
- package/dist/cli/types.js.map +0 -1
- package/dist/cli/types.mjs +0 -4
- package/dist/cli/wizard/index.d.ts +0 -6
- package/dist/cli/wizard/index.d.ts.map +0 -1
- package/dist/cli/wizard/index.js +0 -138
- package/dist/cli/wizard/index.js.map +0 -1
- package/dist/cli/wizard/index.mjs +0 -131
- package/dist/cli/wizard/prompts.d.ts +0 -11
- package/dist/cli/wizard/prompts.d.ts.map +0 -1
- package/dist/cli/wizard/prompts.js +0 -482
- package/dist/cli/wizard/prompts.js.map +0 -1
- package/dist/cli/wizard/prompts.mjs +0 -470
- package/dist/cli/wizard/validator.d.ts +0 -13
- package/dist/cli/wizard/validator.d.ts.map +0 -1
- package/dist/cli/wizard/validator.js +0 -234
- package/dist/cli/wizard/validator.js.map +0 -1
- package/dist/cli/wizard/validator.mjs +0 -224
- package/dist/core/backend-interface.d.ts +0 -71
- package/dist/core/backend-interface.d.ts.map +0 -1
- package/dist/core/backend-interface.js +0 -16
- package/dist/core/backend-interface.js.map +0 -1
- package/dist/core/backend-interface.mjs +0 -12
- package/dist/core/native-backend.d.ts +0 -141
- package/dist/core/native-backend.d.ts.map +0 -1
- package/dist/core/native-backend.js +0 -457
- package/dist/core/native-backend.js.map +0 -1
- package/dist/core/native-backend.mjs +0 -449
- package/dist/core/vector-db.d.ts +0 -127
- package/dist/core/vector-db.d.ts.map +0 -1
- package/dist/core/vector-db.js +0 -270
- package/dist/core/vector-db.js.map +0 -1
- package/dist/core/vector-db.mjs +0 -265
- package/dist/core/wasm-backend.d.ts +0 -96
- package/dist/core/wasm-backend.d.ts.map +0 -1
- package/dist/core/wasm-backend.js +0 -393
- package/dist/core/wasm-backend.js.map +0 -1
- package/dist/core/wasm-backend.mjs +0 -385
- package/dist/index/hnsw-optimized.d.ts +0 -75
- package/dist/index/hnsw-optimized.d.ts.map +0 -1
- package/dist/index/hnsw-optimized.js +0 -412
- package/dist/index/hnsw-optimized.js.map +0 -1
- package/dist/index/hnsw-optimized.mjs +0 -407
- package/dist/index/hnsw.d.ts +0 -165
- package/dist/index/hnsw.d.ts.map +0 -1
- package/dist/index/hnsw.js +0 -521
- package/dist/index/hnsw.js.map +0 -1
- package/dist/index/hnsw.mjs +0 -516
- package/dist/index.browser.d.ts +0 -47
- package/dist/index.browser.d.ts.map +0 -1
- package/dist/index.browser.js +0 -72
- package/dist/index.browser.js.map +0 -1
- package/dist/index.browser.mjs +0 -54
- package/dist/index.d.ts +0 -59
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -84
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -65
- package/dist/mcp/learning/core/experience-buffer.d.ts +0 -61
- package/dist/mcp/learning/core/experience-buffer.d.ts.map +0 -1
- package/dist/mcp/learning/core/experience-buffer.js +0 -175
- package/dist/mcp/learning/core/experience-buffer.js.map +0 -1
- package/dist/mcp/learning/core/experience-buffer.mjs +0 -170
- package/dist/mcp/learning/core/experience-recorder.d.ts +0 -40
- package/dist/mcp/learning/core/experience-recorder.d.ts.map +0 -1
- package/dist/mcp/learning/core/experience-recorder.js +0 -200
- package/dist/mcp/learning/core/experience-recorder.js.map +0 -1
- package/dist/mcp/learning/core/experience-recorder.mjs +0 -195
- package/dist/mcp/learning/core/learning-manager.d.ts +0 -66
- package/dist/mcp/learning/core/learning-manager.d.ts.map +0 -1
- package/dist/mcp/learning/core/learning-manager.js +0 -252
- package/dist/mcp/learning/core/learning-manager.js.map +0 -1
- package/dist/mcp/learning/core/learning-manager.mjs +0 -247
- package/dist/mcp/learning/core/policy-optimizer.d.ts +0 -53
- package/dist/mcp/learning/core/policy-optimizer.d.ts.map +0 -1
- package/dist/mcp/learning/core/policy-optimizer.js +0 -251
- package/dist/mcp/learning/core/policy-optimizer.js.map +0 -1
- package/dist/mcp/learning/core/policy-optimizer.mjs +0 -246
- package/dist/mcp/learning/core/reward-estimator.d.ts +0 -44
- package/dist/mcp/learning/core/reward-estimator.d.ts.map +0 -1
- package/dist/mcp/learning/core/reward-estimator.js +0 -158
- package/dist/mcp/learning/core/reward-estimator.js.map +0 -1
- package/dist/mcp/learning/core/reward-estimator.mjs +0 -153
- package/dist/mcp/learning/core/session-manager.d.ts +0 -63
- package/dist/mcp/learning/core/session-manager.d.ts.map +0 -1
- package/dist/mcp/learning/core/session-manager.js +0 -202
- package/dist/mcp/learning/core/session-manager.js.map +0 -1
- package/dist/mcp/learning/core/session-manager.mjs +0 -197
- package/dist/mcp/learning/index.d.ts +0 -19
- package/dist/mcp/learning/index.d.ts.map +0 -1
- package/dist/mcp/learning/index.js +0 -30
- package/dist/mcp/learning/index.js.map +0 -1
- package/dist/mcp/learning/index.mjs +0 -19
- package/dist/mcp/learning/tools/mcp-learning-tools.d.ts +0 -369
- package/dist/mcp/learning/tools/mcp-learning-tools.d.ts.map +0 -1
- package/dist/mcp/learning/tools/mcp-learning-tools.js +0 -361
- package/dist/mcp/learning/tools/mcp-learning-tools.js.map +0 -1
- package/dist/mcp/learning/tools/mcp-learning-tools.mjs +0 -356
- package/dist/mcp/learning/types/index.d.ts +0 -138
- package/dist/mcp/learning/types/index.d.ts.map +0 -1
- package/dist/mcp/learning/types/index.js +0 -6
- package/dist/mcp/learning/types/index.js.map +0 -1
- package/dist/mcp/learning/types/index.mjs +0 -4
- package/dist/mcp-server.d.ts +0 -29
- package/dist/mcp-server.d.ts.map +0 -1
- package/dist/mcp-server.js +0 -862
- package/dist/mcp-server.js.map +0 -1
- package/dist/mcp-server.mjs +0 -857
- package/dist/plugins/base-plugin.d.ts +0 -114
- package/dist/plugins/base-plugin.d.ts.map +0 -1
- package/dist/plugins/base-plugin.js +0 -313
- package/dist/plugins/base-plugin.js.map +0 -1
- package/dist/plugins/base-plugin.mjs +0 -275
- package/dist/plugins/implementations/active-learning.d.ts +0 -135
- package/dist/plugins/implementations/active-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/active-learning.js +0 -372
- package/dist/plugins/implementations/active-learning.js.map +0 -1
- package/dist/plugins/implementations/active-learning.mjs +0 -367
- package/dist/plugins/implementations/actor-critic.d.ts +0 -64
- package/dist/plugins/implementations/actor-critic.d.ts.map +0 -1
- package/dist/plugins/implementations/actor-critic.js +0 -363
- package/dist/plugins/implementations/actor-critic.js.map +0 -1
- package/dist/plugins/implementations/actor-critic.mjs +0 -358
- package/dist/plugins/implementations/adversarial-training.d.ts +0 -133
- package/dist/plugins/implementations/adversarial-training.d.ts.map +0 -1
- package/dist/plugins/implementations/adversarial-training.js +0 -409
- package/dist/plugins/implementations/adversarial-training.js.map +0 -1
- package/dist/plugins/implementations/adversarial-training.mjs +0 -404
- package/dist/plugins/implementations/curriculum-learning.d.ts +0 -132
- package/dist/plugins/implementations/curriculum-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/curriculum-learning.js +0 -354
- package/dist/plugins/implementations/curriculum-learning.js.map +0 -1
- package/dist/plugins/implementations/curriculum-learning.mjs +0 -349
- package/dist/plugins/implementations/decision-transformer.d.ts +0 -77
- package/dist/plugins/implementations/decision-transformer.d.ts.map +0 -1
- package/dist/plugins/implementations/decision-transformer.js +0 -422
- package/dist/plugins/implementations/decision-transformer.js.map +0 -1
- package/dist/plugins/implementations/decision-transformer.mjs +0 -417
- package/dist/plugins/implementations/federated-learning.d.ts +0 -126
- package/dist/plugins/implementations/federated-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/federated-learning.js +0 -436
- package/dist/plugins/implementations/federated-learning.js.map +0 -1
- package/dist/plugins/implementations/federated-learning.mjs +0 -431
- package/dist/plugins/implementations/index.d.ts +0 -30
- package/dist/plugins/implementations/index.d.ts.map +0 -1
- package/dist/plugins/implementations/index.js +0 -45
- package/dist/plugins/implementations/index.js.map +0 -1
- package/dist/plugins/implementations/index.mjs +0 -31
- package/dist/plugins/implementations/multi-task-learning.d.ts +0 -115
- package/dist/plugins/implementations/multi-task-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/multi-task-learning.js +0 -369
- package/dist/plugins/implementations/multi-task-learning.js.map +0 -1
- package/dist/plugins/implementations/multi-task-learning.mjs +0 -364
- package/dist/plugins/implementations/neural-architecture-search.d.ts +0 -148
- package/dist/plugins/implementations/neural-architecture-search.d.ts.map +0 -1
- package/dist/plugins/implementations/neural-architecture-search.js +0 -379
- package/dist/plugins/implementations/neural-architecture-search.js.map +0 -1
- package/dist/plugins/implementations/neural-architecture-search.mjs +0 -374
- package/dist/plugins/implementations/q-learning.d.ts +0 -98
- package/dist/plugins/implementations/q-learning.d.ts.map +0 -1
- package/dist/plugins/implementations/q-learning.js +0 -435
- package/dist/plugins/implementations/q-learning.js.map +0 -1
- package/dist/plugins/implementations/q-learning.mjs +0 -430
- package/dist/plugins/implementations/sarsa.d.ts +0 -103
- package/dist/plugins/implementations/sarsa.d.ts.map +0 -1
- package/dist/plugins/implementations/sarsa.js +0 -347
- package/dist/plugins/implementations/sarsa.js.map +0 -1
- package/dist/plugins/implementations/sarsa.mjs +0 -342
- package/dist/plugins/index.d.ts +0 -107
- package/dist/plugins/index.d.ts.map +0 -1
- package/dist/plugins/index.js +0 -179
- package/dist/plugins/index.js.map +0 -1
- package/dist/plugins/index.mjs +0 -168
- package/dist/plugins/interface.d.ts +0 -439
- package/dist/plugins/interface.d.ts.map +0 -1
- package/dist/plugins/interface.js +0 -12
- package/dist/plugins/interface.js.map +0 -1
- package/dist/plugins/interface.mjs +0 -10
- package/dist/plugins/learning-plugin.interface.d.ts +0 -257
- package/dist/plugins/learning-plugin.interface.d.ts.map +0 -1
- package/dist/plugins/learning-plugin.interface.js +0 -7
- package/dist/plugins/learning-plugin.interface.js.map +0 -1
- package/dist/plugins/learning-plugin.interface.mjs +0 -5
- package/dist/plugins/plugin-exports.d.ts +0 -71
- package/dist/plugins/plugin-exports.d.ts.map +0 -1
- package/dist/plugins/plugin-exports.js +0 -78
- package/dist/plugins/plugin-exports.js.map +0 -1
- package/dist/plugins/plugin-exports.mjs +0 -69
- package/dist/plugins/registry.d.ts +0 -206
- package/dist/plugins/registry.d.ts.map +0 -1
- package/dist/plugins/registry.js +0 -365
- package/dist/plugins/registry.js.map +0 -1
- package/dist/plugins/registry.mjs +0 -356
- package/dist/plugins/validator.d.ts +0 -63
- package/dist/plugins/validator.d.ts.map +0 -1
- package/dist/plugins/validator.js +0 -464
- package/dist/plugins/validator.js.map +0 -1
- package/dist/plugins/validator.mjs +0 -458
- package/dist/presets.d.ts +0 -65
- package/dist/presets.d.ts.map +0 -1
- package/dist/presets.js +0 -145
- package/dist/presets.js.map +0 -1
- package/dist/presets.mjs +0 -140
- package/dist/quantization/binary-quantization.d.ts +0 -104
- package/dist/quantization/binary-quantization.d.ts.map +0 -1
- package/dist/quantization/binary-quantization.js +0 -246
- package/dist/quantization/binary-quantization.js.map +0 -1
- package/dist/quantization/binary-quantization.mjs +0 -240
- package/dist/quantization/optimized-pq.d.ts +0 -138
- package/dist/quantization/optimized-pq.d.ts.map +0 -1
- package/dist/quantization/optimized-pq.js +0 -320
- package/dist/quantization/optimized-pq.js.map +0 -1
- package/dist/quantization/optimized-pq.mjs +0 -313
- package/dist/quantization/product-quantization.d.ts +0 -109
- package/dist/quantization/product-quantization.d.ts.map +0 -1
- package/dist/quantization/product-quantization.js +0 -287
- package/dist/quantization/product-quantization.js.map +0 -1
- package/dist/quantization/product-quantization.mjs +0 -282
- package/dist/quantization/scalar-quantization.d.ts +0 -100
- package/dist/quantization/scalar-quantization.d.ts.map +0 -1
- package/dist/quantization/scalar-quantization.js +0 -324
- package/dist/quantization/scalar-quantization.js.map +0 -1
- package/dist/quantization/scalar-quantization.mjs +0 -319
- package/dist/query/index.d.ts +0 -6
- package/dist/query/index.d.ts.map +0 -1
- package/dist/query/index.js +0 -9
- package/dist/query/index.js.map +0 -1
- package/dist/query/index.mjs +0 -4
- package/dist/query/query-builder.d.ts +0 -323
- package/dist/query/query-builder.d.ts.map +0 -1
- package/dist/query/query-builder.js +0 -524
- package/dist/query/query-builder.js.map +0 -1
- package/dist/query/query-builder.mjs +0 -519
- package/dist/reasoning/context-synthesizer.d.ts +0 -57
- package/dist/reasoning/context-synthesizer.d.ts.map +0 -1
- package/dist/reasoning/context-synthesizer.js +0 -224
- package/dist/reasoning/context-synthesizer.js.map +0 -1
- package/dist/reasoning/context-synthesizer.mjs +0 -219
- package/dist/reasoning/experience-curator.d.ts +0 -66
- package/dist/reasoning/experience-curator.d.ts.map +0 -1
- package/dist/reasoning/experience-curator.js +0 -288
- package/dist/reasoning/experience-curator.js.map +0 -1
- package/dist/reasoning/experience-curator.mjs +0 -283
- package/dist/reasoning/memory-optimizer.d.ts +0 -69
- package/dist/reasoning/memory-optimizer.d.ts.map +0 -1
- package/dist/reasoning/memory-optimizer.js +0 -331
- package/dist/reasoning/memory-optimizer.js.map +0 -1
- package/dist/reasoning/memory-optimizer.mjs +0 -326
- package/dist/reasoning/pattern-matcher.d.ts +0 -59
- package/dist/reasoning/pattern-matcher.d.ts.map +0 -1
- package/dist/reasoning/pattern-matcher.js +0 -229
- package/dist/reasoning/pattern-matcher.js.map +0 -1
- package/dist/reasoning/pattern-matcher.mjs +0 -224
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts +0 -118
- package/dist/reasoningbank/adapter/agentdb-adapter.d.ts.map +0 -1
- package/dist/reasoningbank/adapter/agentdb-adapter.js +0 -477
- package/dist/reasoningbank/adapter/agentdb-adapter.js.map +0 -1
- package/dist/reasoningbank/adapter/types.d.ts +0 -113
- package/dist/reasoningbank/adapter/types.d.ts.map +0 -1
- package/dist/reasoningbank/adapter/types.js +0 -9
- package/dist/reasoningbank/adapter/types.js.map +0 -1
- package/dist/reasoningbank/cli/commands.d.ts +0 -16
- package/dist/reasoningbank/cli/commands.d.ts.map +0 -1
- package/dist/reasoningbank/cli/commands.js +0 -272
- package/dist/reasoningbank/cli/commands.js.map +0 -1
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts +0 -319
- package/dist/reasoningbank/mcp/agentdb-tools.d.ts.map +0 -1
- package/dist/reasoningbank/mcp/agentdb-tools.js +0 -301
- package/dist/reasoningbank/mcp/agentdb-tools.js.map +0 -1
- package/dist/reasoningbank/migration/migrate.d.ts +0 -25
- package/dist/reasoningbank/migration/migrate.d.ts.map +0 -1
- package/dist/reasoningbank/migration/migrate.js +0 -178
- package/dist/reasoningbank/migration/migrate.js.map +0 -1
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts +0 -37
- package/dist/reasoningbank/reasoning/context-synthesizer.d.ts.map +0 -1
- package/dist/reasoningbank/reasoning/context-synthesizer.js +0 -114
- package/dist/reasoningbank/reasoning/context-synthesizer.js.map +0 -1
- package/dist/reasoningbank/reasoning/experience-curator.d.ts +0 -39
- package/dist/reasoningbank/reasoning/experience-curator.d.ts.map +0 -1
- package/dist/reasoningbank/reasoning/experience-curator.js +0 -98
- package/dist/reasoningbank/reasoning/experience-curator.js.map +0 -1
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts +0 -44
- package/dist/reasoningbank/reasoning/memory-optimizer.d.ts.map +0 -1
- package/dist/reasoningbank/reasoning/memory-optimizer.js +0 -184
- package/dist/reasoningbank/reasoning/memory-optimizer.js.map +0 -1
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts +0 -40
- package/dist/reasoningbank/reasoning/pattern-matcher.d.ts.map +0 -1
- package/dist/reasoningbank/reasoning/pattern-matcher.js +0 -87
- package/dist/reasoningbank/reasoning/pattern-matcher.js.map +0 -1
- package/dist/reasoningbank/sync/quic-sync.d.ts +0 -77
- package/dist/reasoningbank/sync/quic-sync.d.ts.map +0 -1
- package/dist/reasoningbank/sync/quic-sync.js +0 -165
- package/dist/reasoningbank/sync/quic-sync.js.map +0 -1
- package/dist/sync/conflict.d.ts +0 -78
- package/dist/sync/conflict.d.ts.map +0 -1
- package/dist/sync/conflict.js +0 -202
- package/dist/sync/conflict.js.map +0 -1
- package/dist/sync/conflict.mjs +0 -196
- package/dist/sync/coordinator.d.ts +0 -111
- package/dist/sync/coordinator.d.ts.map +0 -1
- package/dist/sync/coordinator.js +0 -256
- package/dist/sync/coordinator.js.map +0 -1
- package/dist/sync/coordinator.mjs +0 -250
- package/dist/sync/delta.d.ts +0 -81
- package/dist/sync/delta.d.ts.map +0 -1
- package/dist/sync/delta.js +0 -245
- package/dist/sync/delta.js.map +0 -1
- package/dist/sync/delta.mjs +0 -238
- package/dist/sync/index.d.ts +0 -11
- package/dist/sync/index.d.ts.map +0 -1
- package/dist/sync/index.js +0 -22
- package/dist/sync/index.js.map +0 -1
- package/dist/sync/index.mjs +0 -9
- package/dist/sync/quic-sync.d.ts +0 -81
- package/dist/sync/quic-sync.d.ts.map +0 -1
- package/dist/sync/quic-sync.js +0 -329
- package/dist/sync/quic-sync.js.map +0 -1
- package/dist/sync/quic-sync.mjs +0 -323
- package/dist/sync/types.d.ts +0 -168
- package/dist/sync/types.d.ts.map +0 -1
- package/dist/sync/types.js +0 -8
- package/dist/sync/types.js.map +0 -1
- package/dist/sync/types.mjs +0 -6
- package/dist/types/index.d.ts +0 -117
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -6
- package/dist/types/index.js.map +0 -1
- package/dist/types/index.mjs +0 -4
- package/dist/wasm/sql-wasm-debug.js +0 -6989
- package/dist/wasm/sql-wasm-debug.wasm +0 -0
- package/dist/wasm/sql-wasm.js +0 -188
- package/dist/wasm/sql-wasm.wasm +0 -0
- package/dist/wasm-loader.d.ts +0 -32
- package/dist/wasm-loader.d.ts.map +0 -1
- package/dist/wasm-loader.js +0 -78
- package/dist/wasm-loader.js.map +0 -1
- package/dist/wasm-loader.mjs +0 -67
- package/examples/adaptive-learning.ts +0 -284
- package/examples/browser/README.md +0 -732
- package/examples/browser/adaptive-recommendations/index.html +0 -427
- package/examples/browser/collaborative-filtering/index.html +0 -310
- package/examples/browser/continual-learning/index.html +0 -736
- package/examples/browser/experience-replay/index.html +0 -616
- package/examples/browser/index.html +0 -369
- package/examples/browser/meta-learning/index.html +0 -789
- package/examples/browser/neuro-symbolic/index.html +0 -692
- package/examples/browser/pattern-learning/index.html +0 -620
- package/examples/browser/quantum-inspired/index.html +0 -728
- package/examples/browser/rag/index.html +0 -624
- package/examples/browser/swarm-intelligence/index.html +0 -811
- package/examples/browser-basic.html +0 -170
- package/examples/browser-wasm-real.html +0 -231
- package/examples/hnsw-example.ts +0 -148
- package/examples/mcp-learning-example.ts +0 -220
- package/examples/node-basic.js +0 -70
- package/examples/quic-sync-example.ts +0 -310
- package/examples/quick-start.js +0 -68
- package/examples/test-v1.0.7-cdn.html +0 -190
- package/examples/wasm-example.ts +0 -222
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CausalRecall - Utility-Based Reranking + Certificate Issuer
|
|
3
|
+
*
|
|
4
|
+
* Combines:
|
|
5
|
+
* 1. Vector similarity search
|
|
6
|
+
* 2. Causal uplift from CausalMemoryGraph
|
|
7
|
+
* 3. Utility-based reranking: U = α*similarity + β*uplift − γ*latencyCost
|
|
8
|
+
* 4. Automatic certificate issuance via ExplainableRecall
|
|
9
|
+
*
|
|
10
|
+
* This is the main entry point for production retrieval with:
|
|
11
|
+
* - Causal-aware ranking
|
|
12
|
+
* - Explainable provenance
|
|
13
|
+
* - Policy compliance
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { Database } from 'better-sqlite3';
|
|
17
|
+
import { CausalMemoryGraph, CausalEdge } from './CausalMemoryGraph.js';
|
|
18
|
+
import { ExplainableRecall, RecallCertificate } from './ExplainableRecall.js';
|
|
19
|
+
import { EmbeddingService } from './EmbeddingService.js';
|
|
20
|
+
|
|
21
|
+
export interface RerankConfig {
|
|
22
|
+
alpha: number; // Similarity weight (default: 0.7)
|
|
23
|
+
beta: number; // Uplift weight (default: 0.2)
|
|
24
|
+
gamma: number; // Latency penalty (default: 0.1)
|
|
25
|
+
minConfidence?: number; // Min causal confidence (default: 0.6)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface RerankCandidate {
|
|
29
|
+
id: string;
|
|
30
|
+
type: 'episode' | 'skill' | 'note' | 'fact';
|
|
31
|
+
content: string;
|
|
32
|
+
similarity: number;
|
|
33
|
+
uplift?: number;
|
|
34
|
+
causalConfidence?: number;
|
|
35
|
+
latencyMs?: number;
|
|
36
|
+
utilityScore: number;
|
|
37
|
+
rank: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface CausalRecallResult {
|
|
41
|
+
candidates: RerankCandidate[];
|
|
42
|
+
certificate: RecallCertificate;
|
|
43
|
+
queryId: string;
|
|
44
|
+
totalLatencyMs: number;
|
|
45
|
+
metrics: {
|
|
46
|
+
vectorSearchMs: number;
|
|
47
|
+
causalLookupMs: number;
|
|
48
|
+
rerankMs: number;
|
|
49
|
+
certificateMs: number;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class CausalRecall {
|
|
54
|
+
private db: Database;
|
|
55
|
+
private causalGraph: CausalMemoryGraph;
|
|
56
|
+
private explainableRecall: ExplainableRecall;
|
|
57
|
+
private embedder: EmbeddingService;
|
|
58
|
+
|
|
59
|
+
constructor(
|
|
60
|
+
db: Database,
|
|
61
|
+
embedder: EmbeddingService,
|
|
62
|
+
private config: RerankConfig = {
|
|
63
|
+
alpha: 0.7,
|
|
64
|
+
beta: 0.2,
|
|
65
|
+
gamma: 0.1,
|
|
66
|
+
minConfidence: 0.6
|
|
67
|
+
}
|
|
68
|
+
) {
|
|
69
|
+
this.db = db;
|
|
70
|
+
this.embedder = embedder;
|
|
71
|
+
this.causalGraph = new CausalMemoryGraph(db);
|
|
72
|
+
this.explainableRecall = new ExplainableRecall(db);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Main recall function with utility-based reranking and certificate issuance
|
|
77
|
+
*
|
|
78
|
+
* @param queryId Unique query identifier
|
|
79
|
+
* @param queryText Natural language query
|
|
80
|
+
* @param k Number of results to return (default: 12)
|
|
81
|
+
* @param requirements Optional list of requirements for completeness checking
|
|
82
|
+
* @param accessLevel Security access level for certificate
|
|
83
|
+
* @returns Reranked results with certificate
|
|
84
|
+
*/
|
|
85
|
+
async recall(
|
|
86
|
+
queryId: string,
|
|
87
|
+
queryText: string,
|
|
88
|
+
k: number = 12,
|
|
89
|
+
requirements?: string[],
|
|
90
|
+
accessLevel: 'public' | 'internal' | 'confidential' | 'restricted' = 'internal'
|
|
91
|
+
): Promise<CausalRecallResult> {
|
|
92
|
+
const startTime = Date.now();
|
|
93
|
+
const metrics = {
|
|
94
|
+
vectorSearchMs: 0,
|
|
95
|
+
causalLookupMs: 0,
|
|
96
|
+
rerankMs: 0,
|
|
97
|
+
certificateMs: 0
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// Step 1: Vector similarity search
|
|
101
|
+
const vectorStart = Date.now();
|
|
102
|
+
const queryEmbedding = await this.embedder.embed(queryText);
|
|
103
|
+
const candidates = await this.vectorSearch(queryEmbedding, k * 2); // Fetch 2k for reranking
|
|
104
|
+
metrics.vectorSearchMs = Date.now() - vectorStart;
|
|
105
|
+
|
|
106
|
+
// Step 2: Load causal edges for candidates
|
|
107
|
+
const causalStart = Date.now();
|
|
108
|
+
const causalEdges = await this.loadCausalEdges(candidates.map(c => c.id));
|
|
109
|
+
metrics.causalLookupMs = Date.now() - causalStart;
|
|
110
|
+
|
|
111
|
+
// Step 3: Rerank by utility
|
|
112
|
+
const rerankStart = Date.now();
|
|
113
|
+
const reranked = this.rerankByUtility(candidates, causalEdges);
|
|
114
|
+
const topK = reranked.slice(0, k);
|
|
115
|
+
metrics.rerankMs = Date.now() - rerankStart;
|
|
116
|
+
|
|
117
|
+
// Step 4: Issue certificate
|
|
118
|
+
const certStart = Date.now();
|
|
119
|
+
const certificate = this.issueCertificate({
|
|
120
|
+
queryId,
|
|
121
|
+
queryText,
|
|
122
|
+
candidates: topK,
|
|
123
|
+
requirements: requirements || this.extractRequirements(queryText),
|
|
124
|
+
accessLevel
|
|
125
|
+
});
|
|
126
|
+
metrics.certificateMs = Date.now() - certStart;
|
|
127
|
+
|
|
128
|
+
const totalLatencyMs = Date.now() - startTime;
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
candidates: topK,
|
|
132
|
+
certificate,
|
|
133
|
+
queryId,
|
|
134
|
+
totalLatencyMs,
|
|
135
|
+
metrics
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Vector similarity search using cosine similarity
|
|
141
|
+
*/
|
|
142
|
+
private async vectorSearch(
|
|
143
|
+
queryEmbedding: Float32Array,
|
|
144
|
+
k: number
|
|
145
|
+
): Promise<Array<{ id: string; type: string; content: string; similarity: number; latencyMs: number }>> {
|
|
146
|
+
const results: any[] = [];
|
|
147
|
+
|
|
148
|
+
// Search episode embeddings
|
|
149
|
+
const episodes = this.db.prepare(`
|
|
150
|
+
SELECT
|
|
151
|
+
e.id,
|
|
152
|
+
'episode' as type,
|
|
153
|
+
e.task || ' ' || COALESCE(e.output, '') as content,
|
|
154
|
+
ee.embedding,
|
|
155
|
+
e.latency_ms
|
|
156
|
+
FROM episodes e
|
|
157
|
+
JOIN episode_embeddings ee ON e.id = ee.episode_id
|
|
158
|
+
ORDER BY e.ts DESC
|
|
159
|
+
LIMIT ?
|
|
160
|
+
`).all(k * 2);
|
|
161
|
+
|
|
162
|
+
for (const ep of episodes) {
|
|
163
|
+
const episodeRow = ep as any;
|
|
164
|
+
const embedding = new Float32Array(JSON.parse(episodeRow.embedding));
|
|
165
|
+
const similarity = this.cosineSimilarity(queryEmbedding, embedding);
|
|
166
|
+
results.push({
|
|
167
|
+
id: episodeRow.id.toString(),
|
|
168
|
+
type: episodeRow.type,
|
|
169
|
+
content: episodeRow.content,
|
|
170
|
+
similarity,
|
|
171
|
+
latencyMs: episodeRow.latency_ms || 0
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Sort by similarity and return top k
|
|
176
|
+
return results
|
|
177
|
+
.sort((a, b) => b.similarity - a.similarity)
|
|
178
|
+
.slice(0, k);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Load causal edges for candidates
|
|
183
|
+
*/
|
|
184
|
+
private async loadCausalEdges(candidateIds: string[]): Promise<Map<string, CausalEdge[]>> {
|
|
185
|
+
const edgeMap = new Map<string, CausalEdge[]>();
|
|
186
|
+
|
|
187
|
+
if (candidateIds.length === 0) {
|
|
188
|
+
return edgeMap;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const placeholders = candidateIds.map(() => '?').join(',');
|
|
192
|
+
const edges = this.db.prepare(`
|
|
193
|
+
SELECT * FROM causal_edges
|
|
194
|
+
WHERE from_memory_id IN (${placeholders})
|
|
195
|
+
AND confidence >= ?
|
|
196
|
+
`).all(...candidateIds.map(id => parseInt(id)), this.config.minConfidence || 0.6) as any[];
|
|
197
|
+
|
|
198
|
+
for (const edge of edges) {
|
|
199
|
+
const fromId = edge.from_memory_id.toString();
|
|
200
|
+
if (!edgeMap.has(fromId)) {
|
|
201
|
+
edgeMap.set(fromId, []);
|
|
202
|
+
}
|
|
203
|
+
edgeMap.get(fromId)!.push({
|
|
204
|
+
id: edge.id,
|
|
205
|
+
fromMemoryId: edge.from_memory_id,
|
|
206
|
+
fromMemoryType: edge.from_memory_type,
|
|
207
|
+
toMemoryId: edge.to_memory_id,
|
|
208
|
+
toMemoryType: edge.to_memory_type,
|
|
209
|
+
similarity: edge.similarity,
|
|
210
|
+
uplift: edge.uplift,
|
|
211
|
+
confidence: edge.confidence,
|
|
212
|
+
sampleSize: edge.sample_size,
|
|
213
|
+
evidenceIds: edge.evidence_ids ? JSON.parse(edge.evidence_ids) : undefined,
|
|
214
|
+
mechanism: edge.mechanism
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return edgeMap;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Rerank by utility: U = α*similarity + β*uplift − γ*latencyCost
|
|
223
|
+
*/
|
|
224
|
+
private rerankByUtility(
|
|
225
|
+
candidates: Array<{ id: string; type: string; content: string; similarity: number; latencyMs: number }>,
|
|
226
|
+
causalEdges: Map<string, CausalEdge[]>
|
|
227
|
+
): RerankCandidate[] {
|
|
228
|
+
const { alpha, beta, gamma } = this.config;
|
|
229
|
+
|
|
230
|
+
const reranked = candidates.map(candidate => {
|
|
231
|
+
// Get causal uplift (average if multiple edges)
|
|
232
|
+
const edges = causalEdges.get(candidate.id) || [];
|
|
233
|
+
const avgUplift = edges.length > 0
|
|
234
|
+
? edges.reduce((sum, e) => sum + (e.uplift || 0), 0) / edges.length
|
|
235
|
+
: 0;
|
|
236
|
+
|
|
237
|
+
const avgConfidence = edges.length > 0
|
|
238
|
+
? edges.reduce((sum, e) => sum + e.confidence, 0) / edges.length
|
|
239
|
+
: 0;
|
|
240
|
+
|
|
241
|
+
// Normalize latency (assume max 1000ms)
|
|
242
|
+
const latencyCost = Math.min(candidate.latencyMs / 1000, 1.0);
|
|
243
|
+
|
|
244
|
+
// Calculate utility
|
|
245
|
+
const utilityScore = alpha * candidate.similarity + beta * avgUplift - gamma * latencyCost;
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
id: candidate.id,
|
|
249
|
+
type: candidate.type as any,
|
|
250
|
+
content: candidate.content,
|
|
251
|
+
similarity: candidate.similarity,
|
|
252
|
+
uplift: avgUplift,
|
|
253
|
+
causalConfidence: avgConfidence,
|
|
254
|
+
latencyMs: candidate.latencyMs,
|
|
255
|
+
utilityScore,
|
|
256
|
+
rank: 0 // Will be set after sorting
|
|
257
|
+
};
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// Sort by utility score descending
|
|
261
|
+
reranked.sort((a, b) => b.utilityScore - a.utilityScore);
|
|
262
|
+
|
|
263
|
+
// Assign ranks
|
|
264
|
+
reranked.forEach((candidate, idx) => {
|
|
265
|
+
candidate.rank = idx + 1;
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
return reranked;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Issue certificate for the retrieval
|
|
273
|
+
*/
|
|
274
|
+
private issueCertificate(params: {
|
|
275
|
+
queryId: string;
|
|
276
|
+
queryText: string;
|
|
277
|
+
candidates: RerankCandidate[];
|
|
278
|
+
requirements: string[];
|
|
279
|
+
accessLevel: 'public' | 'internal' | 'confidential' | 'restricted';
|
|
280
|
+
}): RecallCertificate {
|
|
281
|
+
const { queryId, queryText, candidates, requirements, accessLevel } = params;
|
|
282
|
+
|
|
283
|
+
const chunks = candidates.map(c => ({
|
|
284
|
+
id: c.id,
|
|
285
|
+
type: c.type,
|
|
286
|
+
content: c.content,
|
|
287
|
+
relevance: c.similarity
|
|
288
|
+
}));
|
|
289
|
+
|
|
290
|
+
return this.explainableRecall.createCertificate({
|
|
291
|
+
queryId,
|
|
292
|
+
queryText,
|
|
293
|
+
chunks,
|
|
294
|
+
requirements,
|
|
295
|
+
accessLevel
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Extract requirements from query text (simple keyword extraction)
|
|
301
|
+
*/
|
|
302
|
+
private extractRequirements(queryText: string): string[] {
|
|
303
|
+
// Simple extraction: split on common words and filter
|
|
304
|
+
const stopWords = new Set(['a', 'an', 'the', 'is', 'are', 'was', 'were', 'to', 'from', 'for', 'with', 'how', 'what', 'where', 'when', 'why', 'who']);
|
|
305
|
+
|
|
306
|
+
const words = queryText
|
|
307
|
+
.toLowerCase()
|
|
308
|
+
.replace(/[^a-z0-9\s]/g, '')
|
|
309
|
+
.split(/\s+/)
|
|
310
|
+
.filter(w => w.length > 3 && !stopWords.has(w));
|
|
311
|
+
|
|
312
|
+
// Return unique words
|
|
313
|
+
return [...new Set(words)];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Cosine similarity between two vectors
|
|
318
|
+
*/
|
|
319
|
+
private cosineSimilarity(a: Float32Array, b: Float32Array): number {
|
|
320
|
+
if (a.length !== b.length) {
|
|
321
|
+
throw new Error('Vector dimensions must match');
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
let dotProduct = 0;
|
|
325
|
+
let magnitudeA = 0;
|
|
326
|
+
let magnitudeB = 0;
|
|
327
|
+
|
|
328
|
+
for (let i = 0; i < a.length; i++) {
|
|
329
|
+
dotProduct += a[i] * b[i];
|
|
330
|
+
magnitudeA += a[i] * a[i];
|
|
331
|
+
magnitudeB += b[i] * b[i];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const magnitude = Math.sqrt(magnitudeA) * Math.sqrt(magnitudeB);
|
|
335
|
+
return magnitude === 0 ? 0 : dotProduct / magnitude;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Batch recall for multiple queries
|
|
340
|
+
*/
|
|
341
|
+
async batchRecall(
|
|
342
|
+
queries: Array<{ queryId: string; queryText: string; k?: number }>,
|
|
343
|
+
requirements?: string[],
|
|
344
|
+
accessLevel: 'public' | 'internal' | 'confidential' | 'restricted' = 'internal'
|
|
345
|
+
): Promise<CausalRecallResult[]> {
|
|
346
|
+
const results: CausalRecallResult[] = [];
|
|
347
|
+
|
|
348
|
+
for (const query of queries) {
|
|
349
|
+
const result = await this.recall(
|
|
350
|
+
query.queryId,
|
|
351
|
+
query.queryText,
|
|
352
|
+
query.k || 12,
|
|
353
|
+
requirements,
|
|
354
|
+
accessLevel
|
|
355
|
+
);
|
|
356
|
+
results.push(result);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return results;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Get recall statistics
|
|
364
|
+
*/
|
|
365
|
+
getStats(): {
|
|
366
|
+
totalCausalEdges: number;
|
|
367
|
+
totalCertificates: number;
|
|
368
|
+
avgRedundancyRatio: number;
|
|
369
|
+
avgCompletenessScore: number;
|
|
370
|
+
} {
|
|
371
|
+
const causalEdges = this.db.prepare('SELECT COUNT(*) as count FROM causal_edges').get() as any;
|
|
372
|
+
const certificates = this.db.prepare('SELECT COUNT(*) as count FROM recall_certificates').get() as any;
|
|
373
|
+
|
|
374
|
+
const avgStats = this.db.prepare(`
|
|
375
|
+
SELECT
|
|
376
|
+
AVG(redundancy_ratio) as avg_redundancy,
|
|
377
|
+
AVG(completeness_score) as avg_completeness
|
|
378
|
+
FROM recall_certificates
|
|
379
|
+
`).get() as any;
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
totalCausalEdges: causalEdges.count,
|
|
383
|
+
totalCertificates: certificates.count,
|
|
384
|
+
avgRedundancyRatio: avgStats?.avg_redundancy || 0,
|
|
385
|
+
avgCompletenessScore: avgStats?.avg_completeness || 0
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Update rerank configuration
|
|
391
|
+
*/
|
|
392
|
+
updateConfig(config: Partial<RerankConfig>): void {
|
|
393
|
+
this.config = { ...this.config, ...config };
|
|
394
|
+
}
|
|
395
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EmbeddingService - Text Embedding Generation
|
|
3
|
+
*
|
|
4
|
+
* Handles text-to-vector embedding generation using various models.
|
|
5
|
+
* Supports both local (transformers.js) and remote (OpenAI, etc.) embeddings.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface EmbeddingConfig {
|
|
9
|
+
model: string;
|
|
10
|
+
dimension: number;
|
|
11
|
+
provider: 'transformers' | 'openai' | 'local';
|
|
12
|
+
apiKey?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class EmbeddingService {
|
|
16
|
+
private config: EmbeddingConfig;
|
|
17
|
+
private pipeline: any; // transformers.js pipeline
|
|
18
|
+
private cache: Map<string, Float32Array>;
|
|
19
|
+
|
|
20
|
+
constructor(config: EmbeddingConfig) {
|
|
21
|
+
this.config = config;
|
|
22
|
+
this.cache = new Map();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Initialize the embedding service
|
|
27
|
+
*/
|
|
28
|
+
async initialize(): Promise<void> {
|
|
29
|
+
if (this.config.provider === 'transformers') {
|
|
30
|
+
// Use transformers.js for local embeddings
|
|
31
|
+
try {
|
|
32
|
+
const { pipeline } = await import('@xenova/transformers');
|
|
33
|
+
this.pipeline = await pipeline('feature-extraction', this.config.model);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.warn('Transformers.js not available, falling back to mock embeddings');
|
|
36
|
+
this.pipeline = null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Generate embedding for text
|
|
43
|
+
*/
|
|
44
|
+
async embed(text: string): Promise<Float32Array> {
|
|
45
|
+
// Check cache
|
|
46
|
+
const cacheKey = `${this.config.model}:${text}`;
|
|
47
|
+
if (this.cache.has(cacheKey)) {
|
|
48
|
+
return this.cache.get(cacheKey)!;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let embedding: Float32Array;
|
|
52
|
+
|
|
53
|
+
if (this.config.provider === 'transformers' && this.pipeline) {
|
|
54
|
+
// Use transformers.js
|
|
55
|
+
const output = await this.pipeline(text, { pooling: 'mean', normalize: true });
|
|
56
|
+
embedding = new Float32Array(output.data);
|
|
57
|
+
} else if (this.config.provider === 'openai' && this.config.apiKey) {
|
|
58
|
+
// Use OpenAI API
|
|
59
|
+
embedding = await this.embedOpenAI(text);
|
|
60
|
+
} else {
|
|
61
|
+
// Mock embedding for testing
|
|
62
|
+
embedding = this.mockEmbedding(text);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Cache result
|
|
66
|
+
if (this.cache.size > 10000) {
|
|
67
|
+
// Simple LRU: clear half the cache
|
|
68
|
+
const keysToDelete = Array.from(this.cache.keys()).slice(0, 5000);
|
|
69
|
+
keysToDelete.forEach(k => this.cache.delete(k));
|
|
70
|
+
}
|
|
71
|
+
this.cache.set(cacheKey, embedding);
|
|
72
|
+
|
|
73
|
+
return embedding;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Batch embed multiple texts
|
|
78
|
+
*/
|
|
79
|
+
async embedBatch(texts: string[]): Promise<Float32Array[]> {
|
|
80
|
+
return Promise.all(texts.map(text => this.embed(text)));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Clear embedding cache
|
|
85
|
+
*/
|
|
86
|
+
clearCache(): void {
|
|
87
|
+
this.cache.clear();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ========================================================================
|
|
91
|
+
// Private Methods
|
|
92
|
+
// ========================================================================
|
|
93
|
+
|
|
94
|
+
private async embedOpenAI(text: string): Promise<Float32Array> {
|
|
95
|
+
const response = await fetch('https://api.openai.com/v1/embeddings', {
|
|
96
|
+
method: 'POST',
|
|
97
|
+
headers: {
|
|
98
|
+
'Authorization': `Bearer ${this.config.apiKey}`,
|
|
99
|
+
'Content-Type': 'application/json'
|
|
100
|
+
},
|
|
101
|
+
body: JSON.stringify({
|
|
102
|
+
model: this.config.model,
|
|
103
|
+
input: text
|
|
104
|
+
})
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const data = await response.json();
|
|
108
|
+
return new Float32Array(data.data[0].embedding);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
private mockEmbedding(text: string): Float32Array {
|
|
112
|
+
// Simple deterministic mock embedding for testing
|
|
113
|
+
const embedding = new Float32Array(this.config.dimension);
|
|
114
|
+
|
|
115
|
+
// Use simple hash-based generation
|
|
116
|
+
let hash = 0;
|
|
117
|
+
for (let i = 0; i < text.length; i++) {
|
|
118
|
+
hash = ((hash << 5) - hash) + text.charCodeAt(i);
|
|
119
|
+
hash = hash & hash; // Convert to 32bit integer
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Fill embedding with pseudo-random values based on hash
|
|
123
|
+
for (let i = 0; i < this.config.dimension; i++) {
|
|
124
|
+
const seed = hash + i * 31;
|
|
125
|
+
embedding[i] = Math.sin(seed) * Math.cos(seed * 0.5);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Normalize
|
|
129
|
+
let norm = 0;
|
|
130
|
+
for (let i = 0; i < embedding.length; i++) {
|
|
131
|
+
norm += embedding[i] * embedding[i];
|
|
132
|
+
}
|
|
133
|
+
norm = Math.sqrt(norm);
|
|
134
|
+
|
|
135
|
+
for (let i = 0; i < embedding.length; i++) {
|
|
136
|
+
embedding[i] /= norm;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return embedding;
|
|
140
|
+
}
|
|
141
|
+
}
|