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,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BatchOperations - Optimized Batch Processing for AgentDB
|
|
3
|
+
*
|
|
4
|
+
* Implements efficient batch operations:
|
|
5
|
+
* - Bulk inserts with transactions
|
|
6
|
+
* - Batch embedding generation
|
|
7
|
+
* - Parallel processing
|
|
8
|
+
* - Progress tracking
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { Database } from 'better-sqlite3';
|
|
12
|
+
import { EmbeddingService } from '../controllers/EmbeddingService';
|
|
13
|
+
import { Episode } from '../controllers/ReflexionMemory';
|
|
14
|
+
|
|
15
|
+
export interface BatchConfig {
|
|
16
|
+
batchSize: number;
|
|
17
|
+
parallelism: number;
|
|
18
|
+
progressCallback?: (progress: number, total: number) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class BatchOperations {
|
|
22
|
+
private db: Database;
|
|
23
|
+
private embedder: EmbeddingService;
|
|
24
|
+
private config: BatchConfig;
|
|
25
|
+
|
|
26
|
+
constructor(db: Database, embedder: EmbeddingService, config?: Partial<BatchConfig>) {
|
|
27
|
+
this.db = db;
|
|
28
|
+
this.embedder = embedder;
|
|
29
|
+
this.config = {
|
|
30
|
+
batchSize: 100,
|
|
31
|
+
parallelism: 4,
|
|
32
|
+
...config
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Bulk insert episodes with embeddings
|
|
38
|
+
*/
|
|
39
|
+
async insertEpisodes(episodes: Episode[]): Promise<number> {
|
|
40
|
+
const totalBatches = Math.ceil(episodes.length / this.config.batchSize);
|
|
41
|
+
let completed = 0;
|
|
42
|
+
|
|
43
|
+
for (let i = 0; i < episodes.length; i += this.config.batchSize) {
|
|
44
|
+
const batch = episodes.slice(i, i + this.config.batchSize);
|
|
45
|
+
|
|
46
|
+
// Generate embeddings in parallel
|
|
47
|
+
const texts = batch.map(ep => this.buildEpisodeText(ep));
|
|
48
|
+
const embeddings = await this.embedder.embedBatch(texts);
|
|
49
|
+
|
|
50
|
+
// Insert with transaction
|
|
51
|
+
const transaction = this.db.transaction(() => {
|
|
52
|
+
const episodeStmt = this.db.prepare(`
|
|
53
|
+
INSERT INTO episodes (
|
|
54
|
+
session_id, task, input, output, critique, reward, success,
|
|
55
|
+
latency_ms, tokens_used, tags, metadata
|
|
56
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
57
|
+
`);
|
|
58
|
+
|
|
59
|
+
const embeddingStmt = this.db.prepare(`
|
|
60
|
+
INSERT INTO episode_embeddings (episode_id, embedding)
|
|
61
|
+
VALUES (?, ?)
|
|
62
|
+
`);
|
|
63
|
+
|
|
64
|
+
batch.forEach((episode, idx) => {
|
|
65
|
+
const result = episodeStmt.run(
|
|
66
|
+
episode.sessionId,
|
|
67
|
+
episode.task,
|
|
68
|
+
episode.input || null,
|
|
69
|
+
episode.output || null,
|
|
70
|
+
episode.critique || null,
|
|
71
|
+
episode.reward,
|
|
72
|
+
episode.success ? 1 : 0,
|
|
73
|
+
episode.latencyMs || null,
|
|
74
|
+
episode.tokensUsed || null,
|
|
75
|
+
episode.tags ? JSON.stringify(episode.tags) : null,
|
|
76
|
+
episode.metadata ? JSON.stringify(episode.metadata) : null
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const episodeId = result.lastInsertRowid as number;
|
|
80
|
+
embeddingStmt.run(episodeId, Buffer.from(embeddings[idx].buffer));
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
transaction();
|
|
85
|
+
|
|
86
|
+
completed += batch.length;
|
|
87
|
+
|
|
88
|
+
if (this.config.progressCallback) {
|
|
89
|
+
this.config.progressCallback(completed, episodes.length);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return completed;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Bulk update embeddings for existing episodes
|
|
98
|
+
*/
|
|
99
|
+
async regenerateEmbeddings(episodeIds?: number[]): Promise<number> {
|
|
100
|
+
let episodes: any[];
|
|
101
|
+
|
|
102
|
+
if (episodeIds) {
|
|
103
|
+
const placeholders = episodeIds.map(() => '?').join(',');
|
|
104
|
+
episodes = this.db.prepare(
|
|
105
|
+
`SELECT id, task, critique, output FROM episodes WHERE id IN (${placeholders})`
|
|
106
|
+
).all(...episodeIds);
|
|
107
|
+
} else {
|
|
108
|
+
episodes = this.db.prepare(
|
|
109
|
+
'SELECT id, task, critique, output FROM episodes'
|
|
110
|
+
).all();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let completed = 0;
|
|
114
|
+
const totalBatches = Math.ceil(episodes.length / this.config.batchSize);
|
|
115
|
+
|
|
116
|
+
for (let i = 0; i < episodes.length; i += this.config.batchSize) {
|
|
117
|
+
const batch = episodes.slice(i, i + this.config.batchSize);
|
|
118
|
+
|
|
119
|
+
// Generate embeddings
|
|
120
|
+
const texts = batch.map((ep: any) =>
|
|
121
|
+
[ep.task, ep.critique, ep.output].filter(Boolean).join('\n')
|
|
122
|
+
);
|
|
123
|
+
const embeddings = await this.embedder.embedBatch(texts);
|
|
124
|
+
|
|
125
|
+
// Update with transaction
|
|
126
|
+
const transaction = this.db.transaction(() => {
|
|
127
|
+
const stmt = this.db.prepare(`
|
|
128
|
+
INSERT OR REPLACE INTO episode_embeddings (episode_id, embedding)
|
|
129
|
+
VALUES (?, ?)
|
|
130
|
+
`);
|
|
131
|
+
|
|
132
|
+
batch.forEach((episode: any, idx: number) => {
|
|
133
|
+
stmt.run(episode.id, Buffer.from(embeddings[idx].buffer));
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
transaction();
|
|
138
|
+
|
|
139
|
+
completed += batch.length;
|
|
140
|
+
|
|
141
|
+
if (this.config.progressCallback) {
|
|
142
|
+
this.config.progressCallback(completed, episodes.length);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return completed;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Parallel batch processing with worker pool
|
|
151
|
+
*/
|
|
152
|
+
async processInParallel<T, R>(
|
|
153
|
+
items: T[],
|
|
154
|
+
processor: (item: T) => Promise<R>
|
|
155
|
+
): Promise<R[]> {
|
|
156
|
+
const results: R[] = [];
|
|
157
|
+
const chunks = this.chunkArray(items, this.config.parallelism);
|
|
158
|
+
|
|
159
|
+
for (const chunk of chunks) {
|
|
160
|
+
const chunkResults = await Promise.all(
|
|
161
|
+
chunk.map(item => processor(item))
|
|
162
|
+
);
|
|
163
|
+
results.push(...chunkResults);
|
|
164
|
+
|
|
165
|
+
if (this.config.progressCallback) {
|
|
166
|
+
this.config.progressCallback(results.length, items.length);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return results;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Bulk delete with conditions
|
|
175
|
+
*/
|
|
176
|
+
bulkDelete(table: string, conditions: Record<string, any>): number {
|
|
177
|
+
const whereClause = Object.keys(conditions)
|
|
178
|
+
.map(key => `${key} = ?`)
|
|
179
|
+
.join(' AND ');
|
|
180
|
+
|
|
181
|
+
const values = Object.values(conditions);
|
|
182
|
+
|
|
183
|
+
const stmt = this.db.prepare(`DELETE FROM ${table} WHERE ${whereClause}`);
|
|
184
|
+
const result = stmt.run(...values);
|
|
185
|
+
|
|
186
|
+
return result.changes;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Bulk update with conditions
|
|
191
|
+
*/
|
|
192
|
+
bulkUpdate(
|
|
193
|
+
table: string,
|
|
194
|
+
updates: Record<string, any>,
|
|
195
|
+
conditions: Record<string, any>
|
|
196
|
+
): number {
|
|
197
|
+
const setClause = Object.keys(updates)
|
|
198
|
+
.map(key => `${key} = ?`)
|
|
199
|
+
.join(', ');
|
|
200
|
+
|
|
201
|
+
const whereClause = Object.keys(conditions)
|
|
202
|
+
.map(key => `${key} = ?`)
|
|
203
|
+
.join(' AND ');
|
|
204
|
+
|
|
205
|
+
const values = [...Object.values(updates), ...Object.values(conditions)];
|
|
206
|
+
|
|
207
|
+
const stmt = this.db.prepare(
|
|
208
|
+
`UPDATE ${table} SET ${setClause} WHERE ${whereClause}`
|
|
209
|
+
);
|
|
210
|
+
const result = stmt.run(...values);
|
|
211
|
+
|
|
212
|
+
return result.changes;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Vacuum and optimize database
|
|
217
|
+
*/
|
|
218
|
+
optimize(): void {
|
|
219
|
+
console.log('🔧 Optimizing database...');
|
|
220
|
+
|
|
221
|
+
// Analyze tables for query planner
|
|
222
|
+
this.db.exec('ANALYZE');
|
|
223
|
+
|
|
224
|
+
// Rebuild indexes
|
|
225
|
+
const tables = this.db.prepare(`
|
|
226
|
+
SELECT name FROM sqlite_master
|
|
227
|
+
WHERE type='table' AND name NOT LIKE 'sqlite_%'
|
|
228
|
+
`).all() as any[];
|
|
229
|
+
|
|
230
|
+
for (const { name } of tables) {
|
|
231
|
+
this.db.exec(`REINDEX ${name}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Vacuum to reclaim space
|
|
235
|
+
this.db.exec('VACUUM');
|
|
236
|
+
|
|
237
|
+
console.log('✅ Database optimized');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Get database statistics
|
|
242
|
+
*/
|
|
243
|
+
getStats(): {
|
|
244
|
+
totalSize: number;
|
|
245
|
+
tableStats: Array<{
|
|
246
|
+
name: string;
|
|
247
|
+
rows: number;
|
|
248
|
+
size: number;
|
|
249
|
+
}>;
|
|
250
|
+
} {
|
|
251
|
+
const pageSize = this.db.pragma('page_size', { simple: true }) as number;
|
|
252
|
+
const pageCount = this.db.pragma('page_count', { simple: true }) as number;
|
|
253
|
+
const totalSize = pageSize * pageCount;
|
|
254
|
+
|
|
255
|
+
const tables = this.db.prepare(`
|
|
256
|
+
SELECT name FROM sqlite_master
|
|
257
|
+
WHERE type='table' AND name NOT LIKE 'sqlite_%'
|
|
258
|
+
`).all() as any[];
|
|
259
|
+
|
|
260
|
+
const tableStats = tables.map(({ name }) => {
|
|
261
|
+
const count = this.db.prepare(`SELECT COUNT(*) as count FROM ${name}`).get() as any;
|
|
262
|
+
const pages = this.db.prepare(`SELECT COUNT(*) as count FROM dbstat WHERE name = ?`).get(name) as any;
|
|
263
|
+
|
|
264
|
+
return {
|
|
265
|
+
name,
|
|
266
|
+
rows: count.count,
|
|
267
|
+
size: (pages?.count || 0) * pageSize
|
|
268
|
+
};
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
return { totalSize, tableStats };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ========================================================================
|
|
275
|
+
// Private Methods
|
|
276
|
+
// ========================================================================
|
|
277
|
+
|
|
278
|
+
private buildEpisodeText(episode: Episode): string {
|
|
279
|
+
const parts = [episode.task];
|
|
280
|
+
if (episode.critique) parts.push(episode.critique);
|
|
281
|
+
if (episode.output) parts.push(episode.output);
|
|
282
|
+
return parts.join('\n');
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private chunkArray<T>(array: T[], size: number): T[][] {
|
|
286
|
+
const chunks: T[][] = [];
|
|
287
|
+
for (let i = 0; i < array.length; i += size) {
|
|
288
|
+
chunks.push(array.slice(i, i + size));
|
|
289
|
+
}
|
|
290
|
+
return chunks;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QueryOptimizer - Advanced Query Optimization for AgentDB
|
|
3
|
+
*
|
|
4
|
+
* Implements:
|
|
5
|
+
* - Query result caching with TTL
|
|
6
|
+
* - Prepared statement pooling
|
|
7
|
+
* - Batch operation optimization
|
|
8
|
+
* - Index usage analysis
|
|
9
|
+
* - Query plan analysis
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Database } from 'better-sqlite3';
|
|
13
|
+
|
|
14
|
+
export interface CacheConfig {
|
|
15
|
+
maxSize: number;
|
|
16
|
+
ttl: number; // milliseconds
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface QueryStats {
|
|
21
|
+
query: string;
|
|
22
|
+
executionCount: number;
|
|
23
|
+
totalTime: number;
|
|
24
|
+
avgTime: number;
|
|
25
|
+
cacheHits: number;
|
|
26
|
+
cacheMisses: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class QueryOptimizer {
|
|
30
|
+
private db: Database;
|
|
31
|
+
private cache: Map<string, { result: any; timestamp: number }>;
|
|
32
|
+
private stats: Map<string, QueryStats>;
|
|
33
|
+
private config: CacheConfig;
|
|
34
|
+
|
|
35
|
+
constructor(db: Database, config?: Partial<CacheConfig>) {
|
|
36
|
+
this.db = db;
|
|
37
|
+
this.cache = new Map();
|
|
38
|
+
this.stats = new Map();
|
|
39
|
+
this.config = {
|
|
40
|
+
maxSize: 1000,
|
|
41
|
+
ttl: 60000, // 1 minute default
|
|
42
|
+
enabled: true,
|
|
43
|
+
...config
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Execute query with caching
|
|
49
|
+
*/
|
|
50
|
+
query<T = any>(sql: string, params: any[] = [], cacheKey?: string): T {
|
|
51
|
+
const key = cacheKey || this.generateCacheKey(sql, params);
|
|
52
|
+
const startTime = Date.now();
|
|
53
|
+
|
|
54
|
+
// Check cache
|
|
55
|
+
if (this.config.enabled && this.cache.has(key)) {
|
|
56
|
+
const cached = this.cache.get(key)!;
|
|
57
|
+
if (Date.now() - cached.timestamp < this.config.ttl) {
|
|
58
|
+
this.recordStats(sql, Date.now() - startTime, true);
|
|
59
|
+
return cached.result;
|
|
60
|
+
} else {
|
|
61
|
+
this.cache.delete(key);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Execute query
|
|
66
|
+
const stmt = this.db.prepare(sql);
|
|
67
|
+
const result = params.length > 0 ? stmt.all(...params) : stmt.all();
|
|
68
|
+
|
|
69
|
+
const executionTime = Date.now() - startTime;
|
|
70
|
+
this.recordStats(sql, executionTime, false);
|
|
71
|
+
|
|
72
|
+
// Cache result
|
|
73
|
+
if (this.config.enabled) {
|
|
74
|
+
this.cacheResult(key, result);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return result as T;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Execute query that returns single row
|
|
82
|
+
*/
|
|
83
|
+
queryOne<T = any>(sql: string, params: any[] = [], cacheKey?: string): T | undefined {
|
|
84
|
+
const results = this.query<T[]>(sql, params, cacheKey);
|
|
85
|
+
return results[0];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Execute write operation (no caching)
|
|
90
|
+
*/
|
|
91
|
+
execute(sql: string, params: any[] = []): any {
|
|
92
|
+
const startTime = Date.now();
|
|
93
|
+
const stmt = this.db.prepare(sql);
|
|
94
|
+
const result = params.length > 0 ? stmt.run(...params) : stmt.run();
|
|
95
|
+
|
|
96
|
+
this.recordStats(sql, Date.now() - startTime, false);
|
|
97
|
+
|
|
98
|
+
// Invalidate relevant cache entries
|
|
99
|
+
this.invalidateCache(sql);
|
|
100
|
+
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Batch insert optimization
|
|
106
|
+
*/
|
|
107
|
+
batchInsert(table: string, columns: string[], rows: any[][]): void {
|
|
108
|
+
const placeholders = columns.map(() => '?').join(', ');
|
|
109
|
+
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${placeholders})`;
|
|
110
|
+
|
|
111
|
+
const transaction = this.db.transaction((rows: any[][]) => {
|
|
112
|
+
const stmt = this.db.prepare(sql);
|
|
113
|
+
for (const row of rows) {
|
|
114
|
+
stmt.run(...row);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const startTime = Date.now();
|
|
119
|
+
transaction(rows);
|
|
120
|
+
this.recordStats(`BATCH INSERT ${table}`, Date.now() - startTime, false);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Analyze query plan
|
|
125
|
+
*/
|
|
126
|
+
analyzeQuery(sql: string): {
|
|
127
|
+
plan: string;
|
|
128
|
+
usesIndex: boolean;
|
|
129
|
+
estimatedCost: number;
|
|
130
|
+
} {
|
|
131
|
+
const plan = this.db.prepare(`EXPLAIN QUERY PLAN ${sql}`).all();
|
|
132
|
+
const planText = plan.map((row: any) => row.detail).join(' ');
|
|
133
|
+
|
|
134
|
+
const usesIndex = planText.toLowerCase().includes('index');
|
|
135
|
+
const hasFullScan = planText.toLowerCase().includes('scan');
|
|
136
|
+
|
|
137
|
+
// Simple cost estimation
|
|
138
|
+
let estimatedCost = 1;
|
|
139
|
+
if (hasFullScan) estimatedCost *= 10;
|
|
140
|
+
if (!usesIndex) estimatedCost *= 5;
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
plan: planText,
|
|
144
|
+
usesIndex,
|
|
145
|
+
estimatedCost
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Get optimization suggestions
|
|
151
|
+
*/
|
|
152
|
+
getSuggestions(): string[] {
|
|
153
|
+
const suggestions: string[] = [];
|
|
154
|
+
|
|
155
|
+
// Analyze frequently run queries
|
|
156
|
+
const frequentQueries = Array.from(this.stats.values())
|
|
157
|
+
.filter(s => s.executionCount > 100)
|
|
158
|
+
.sort((a, b) => b.totalTime - a.totalTime)
|
|
159
|
+
.slice(0, 10);
|
|
160
|
+
|
|
161
|
+
for (const stat of frequentQueries) {
|
|
162
|
+
if (stat.avgTime > 50) {
|
|
163
|
+
const analysis = this.analyzeQuery(stat.query);
|
|
164
|
+
|
|
165
|
+
if (!analysis.usesIndex) {
|
|
166
|
+
suggestions.push(
|
|
167
|
+
`Slow query (${stat.avgTime.toFixed(1)}ms avg): Consider adding index for:\n${stat.query}`
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (stat.cacheHits === 0 && stat.executionCount > 50) {
|
|
172
|
+
suggestions.push(
|
|
173
|
+
`Frequently run query without cache hits: ${stat.query.substring(0, 50)}...`
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Check cache efficiency
|
|
180
|
+
const totalHits = Array.from(this.stats.values()).reduce((sum, s) => sum + s.cacheHits, 0);
|
|
181
|
+
const totalMisses = Array.from(this.stats.values()).reduce((sum, s) => sum + s.cacheMisses, 0);
|
|
182
|
+
const hitRate = totalHits / (totalHits + totalMisses) || 0;
|
|
183
|
+
|
|
184
|
+
if (hitRate < 0.3 && totalHits + totalMisses > 1000) {
|
|
185
|
+
suggestions.push(`Low cache hit rate (${(hitRate * 100).toFixed(1)}%). Consider increasing cache size or TTL.`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return suggestions;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Get query statistics
|
|
193
|
+
*/
|
|
194
|
+
getStats(): QueryStats[] {
|
|
195
|
+
return Array.from(this.stats.values())
|
|
196
|
+
.sort((a, b) => b.totalTime - a.totalTime);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Clear cache
|
|
201
|
+
*/
|
|
202
|
+
clearCache(): void {
|
|
203
|
+
this.cache.clear();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Get cache statistics
|
|
208
|
+
*/
|
|
209
|
+
getCacheStats(): {
|
|
210
|
+
size: number;
|
|
211
|
+
hitRate: number;
|
|
212
|
+
totalHits: number;
|
|
213
|
+
totalMisses: number;
|
|
214
|
+
} {
|
|
215
|
+
const totalHits = Array.from(this.stats.values()).reduce((sum, s) => sum + s.cacheHits, 0);
|
|
216
|
+
const totalMisses = Array.from(this.stats.values()).reduce((sum, s) => sum + s.cacheMisses, 0);
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
size: this.cache.size,
|
|
220
|
+
hitRate: totalHits / (totalHits + totalMisses) || 0,
|
|
221
|
+
totalHits,
|
|
222
|
+
totalMisses
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ========================================================================
|
|
227
|
+
// Private Methods
|
|
228
|
+
// ========================================================================
|
|
229
|
+
|
|
230
|
+
private generateCacheKey(sql: string, params: any[]): string {
|
|
231
|
+
return `${sql}:${JSON.stringify(params)}`;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private cacheResult(key: string, result: any): void {
|
|
235
|
+
if (this.cache.size >= this.config.maxSize) {
|
|
236
|
+
// Simple LRU: remove oldest entry
|
|
237
|
+
const oldestKey = this.cache.keys().next().value;
|
|
238
|
+
this.cache.delete(oldestKey);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
this.cache.set(key, {
|
|
242
|
+
result,
|
|
243
|
+
timestamp: Date.now()
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private invalidateCache(sql: string): void {
|
|
248
|
+
// Invalidate cache entries related to modified tables
|
|
249
|
+
const tables = this.extractTables(sql);
|
|
250
|
+
|
|
251
|
+
for (const [key] of this.cache) {
|
|
252
|
+
for (const table of tables) {
|
|
253
|
+
if (key.toLowerCase().includes(table.toLowerCase())) {
|
|
254
|
+
this.cache.delete(key);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private extractTables(sql: string): string[] {
|
|
261
|
+
const matches = sql.match(/(?:FROM|INTO|UPDATE|JOIN)\s+(\w+)/gi);
|
|
262
|
+
if (!matches) return [];
|
|
263
|
+
|
|
264
|
+
return matches
|
|
265
|
+
.map(m => m.split(/\s+/)[1])
|
|
266
|
+
.filter((v, i, a) => a.indexOf(v) === i); // unique
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private recordStats(sql: string, time: number, cacheHit: boolean): void {
|
|
270
|
+
const key = sql.substring(0, 100); // Use first 100 chars as key
|
|
271
|
+
|
|
272
|
+
if (!this.stats.has(key)) {
|
|
273
|
+
this.stats.set(key, {
|
|
274
|
+
query: sql,
|
|
275
|
+
executionCount: 0,
|
|
276
|
+
totalTime: 0,
|
|
277
|
+
avgTime: 0,
|
|
278
|
+
cacheHits: 0,
|
|
279
|
+
cacheMisses: 0
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const stat = this.stats.get(key)!;
|
|
284
|
+
stat.executionCount++;
|
|
285
|
+
stat.totalTime += time;
|
|
286
|
+
stat.avgTime = stat.totalTime / stat.executionCount;
|
|
287
|
+
|
|
288
|
+
if (cacheHit) {
|
|
289
|
+
stat.cacheHits++;
|
|
290
|
+
} else {
|
|
291
|
+
stat.cacheMisses++;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentDB Optimizations
|
|
3
|
+
*
|
|
4
|
+
* Performance optimization utilities
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export { QueryOptimizer } from './QueryOptimizer';
|
|
8
|
+
export { BatchOperations } from './BatchOperations';
|
|
9
|
+
|
|
10
|
+
export type { CacheConfig, QueryStats } from './QueryOptimizer';
|
|
11
|
+
export type { BatchConfig } from './BatchOperations';
|