@ai.ntellect/core 0.8.3 → 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/.mocharc.json +2 -1
- package/AGENTS.md +50 -0
- package/ARCHITECTURE.md +58 -0
- package/BENCHMARK.md +38 -0
- package/CHANGELOG.md +224 -0
- package/README.md +413 -199
- package/agent/agent-workflow.ts +569 -0
- package/agent/agent.ts +183 -0
- package/{modules/agent → agent}/base/executor.ts +11 -8
- package/{modules/agent → agent}/base/index.ts +2 -14
- package/agent/entity-resolver/confidence.ts +54 -0
- package/agent/entity-resolver/entity-index.ts +241 -0
- package/agent/entity-resolver/explore-strategy.ts +82 -0
- package/agent/entity-resolver/index.ts +22 -0
- package/agent/entity-resolver/resolver.ts +193 -0
- package/agent/entity-resolver/scorer.ts +211 -0
- package/agent/entity-resolver/types.ts +98 -0
- package/agent/generic-executor.ts +713 -0
- package/agent/handlers/cognitive-handler.ts +197 -0
- package/agent/handlers/index.ts +1 -0
- package/agent/handoff.ts +58 -0
- package/agent/llm-factory.ts +343 -0
- package/agent/orchestrator.ts +45 -0
- package/{modules/agent → agent}/prompt-builder.ts +1 -1
- package/agent/registry.ts +64 -0
- package/agent/tools/file-system.ts +471 -0
- package/agent/tools/index.ts +23 -0
- package/agent/tools/logger.ts +105 -0
- package/agent/tools/tool-resolver.ts +104 -0
- package/benchmark/cortexflow-workflow.ts +182 -0
- package/benchmark/langgraph-workflow.ts +201 -0
- package/benchmark/llm-client.ts +76 -0
- package/benchmark/run-benchmark.ts +222 -0
- package/cli-dev.ts +419 -0
- package/dist/agent/agent-workflow.d.ts +41 -0
- package/dist/agent/agent-workflow.d.ts.map +1 -0
- package/dist/agent/agent-workflow.js +560 -0
- package/dist/agent/agent-workflow.js.map +1 -0
- package/dist/agent/agent.d.ts +38 -0
- package/dist/agent/agent.d.ts.map +1 -0
- package/dist/agent/agent.js +149 -0
- package/dist/agent/agent.js.map +1 -0
- package/dist/{modules/agent → agent}/base/executor.d.ts +3 -3
- package/dist/agent/base/executor.d.ts.map +1 -0
- package/dist/{modules/agent → agent}/base/executor.js +8 -5
- package/dist/agent/base/executor.js.map +1 -0
- package/dist/{modules/agent → agent}/base/index.d.ts +1 -1
- package/dist/agent/base/index.d.ts.map +1 -0
- package/dist/{modules/agent → agent}/base/index.js +0 -12
- package/dist/agent/base/index.js.map +1 -0
- package/dist/agent/entity-resolver/confidence.d.ts +4 -0
- package/dist/agent/entity-resolver/confidence.d.ts.map +1 -0
- package/dist/agent/entity-resolver/confidence.js +47 -0
- package/dist/agent/entity-resolver/confidence.js.map +1 -0
- package/dist/agent/entity-resolver/entity-index.d.ts +40 -0
- package/dist/agent/entity-resolver/entity-index.d.ts.map +1 -0
- package/dist/agent/entity-resolver/entity-index.js +244 -0
- package/dist/agent/entity-resolver/entity-index.js.map +1 -0
- package/dist/agent/entity-resolver/explore-strategy.d.ts +13 -0
- package/dist/agent/entity-resolver/explore-strategy.d.ts.map +1 -0
- package/dist/agent/entity-resolver/explore-strategy.js +109 -0
- package/dist/agent/entity-resolver/explore-strategy.js.map +1 -0
- package/dist/agent/entity-resolver/index.d.ts +8 -0
- package/dist/agent/entity-resolver/index.d.ts.map +1 -0
- package/dist/agent/entity-resolver/index.js +16 -0
- package/dist/agent/entity-resolver/index.js.map +1 -0
- package/dist/agent/entity-resolver/resolver.d.ts +18 -0
- package/dist/agent/entity-resolver/resolver.d.ts.map +1 -0
- package/dist/agent/entity-resolver/resolver.js +158 -0
- package/dist/agent/entity-resolver/resolver.js.map +1 -0
- package/dist/agent/entity-resolver/scorer.d.ts +3 -0
- package/dist/agent/entity-resolver/scorer.d.ts.map +1 -0
- package/dist/agent/entity-resolver/scorer.js +182 -0
- package/dist/agent/entity-resolver/scorer.js.map +1 -0
- package/dist/agent/entity-resolver/types.d.ts +89 -0
- package/dist/agent/entity-resolver/types.d.ts.map +1 -0
- package/dist/agent/entity-resolver/types.js +3 -0
- package/dist/agent/entity-resolver/types.js.map +1 -0
- package/dist/{modules/agent → agent}/generic-executor.d.ts +37 -27
- package/dist/agent/generic-executor.d.ts.map +1 -0
- package/dist/agent/generic-executor.js +620 -0
- package/dist/agent/generic-executor.js.map +1 -0
- package/dist/agent/handlers/cognitive-handler.d.ts +44 -0
- package/dist/agent/handlers/cognitive-handler.d.ts.map +1 -0
- package/dist/agent/handlers/cognitive-handler.js +172 -0
- package/dist/agent/handlers/cognitive-handler.js.map +1 -0
- package/dist/agent/handlers/index.d.ts +2 -0
- package/dist/agent/handlers/index.d.ts.map +1 -0
- package/dist/agent/handlers/index.js +7 -0
- package/dist/agent/handlers/index.js.map +1 -0
- package/dist/agent/handoff.d.ts +12 -0
- package/dist/agent/handoff.d.ts.map +1 -0
- package/dist/agent/handoff.js +62 -0
- package/dist/agent/handoff.js.map +1 -0
- package/dist/agent/llm-factory.d.ts.map +1 -0
- package/dist/agent/llm-factory.js +297 -0
- package/dist/agent/llm-factory.js.map +1 -0
- package/dist/agent/orchestrator.d.ts +18 -0
- package/dist/agent/orchestrator.d.ts.map +1 -0
- package/dist/agent/orchestrator.js +44 -0
- package/dist/agent/orchestrator.js.map +1 -0
- package/dist/{modules/agent → agent}/prompt-builder.d.ts +1 -1
- package/dist/agent/prompt-builder.d.ts.map +1 -0
- package/dist/agent/prompt-builder.js.map +1 -0
- package/dist/agent/registry.d.ts +19 -0
- package/dist/agent/registry.d.ts.map +1 -0
- package/dist/agent/registry.js +48 -0
- package/dist/agent/registry.js.map +1 -0
- package/dist/agent/tools/file-system.d.ts +11 -0
- package/dist/agent/tools/file-system.d.ts.map +1 -0
- package/dist/agent/tools/file-system.js +528 -0
- package/dist/agent/tools/file-system.js.map +1 -0
- package/dist/agent/tools/index.d.ts +4 -0
- package/dist/agent/tools/index.d.ts.map +1 -0
- package/dist/agent/tools/index.js +20 -0
- package/dist/agent/tools/index.js.map +1 -0
- package/dist/agent/tools/logger.d.ts +33 -0
- package/dist/agent/tools/logger.d.ts.map +1 -0
- package/dist/agent/tools/logger.js +86 -0
- package/dist/agent/tools/logger.js.map +1 -0
- package/dist/agent/tools/tool-resolver.d.ts +7 -0
- package/dist/agent/tools/tool-resolver.d.ts.map +1 -0
- package/dist/agent/tools/tool-resolver.js +122 -0
- package/dist/agent/tools/tool-resolver.js.map +1 -0
- package/dist/execution/adapters/in-memory-checkpoint.d.ts +11 -0
- package/dist/execution/adapters/in-memory-checkpoint.d.ts.map +1 -0
- package/dist/execution/adapters/in-memory-checkpoint.js +54 -0
- package/dist/execution/adapters/in-memory-checkpoint.js.map +1 -0
- package/dist/execution/compiler.d.ts +8 -0
- package/dist/execution/compiler.d.ts.map +1 -0
- package/dist/execution/compiler.js +50 -0
- package/dist/execution/compiler.js.map +1 -0
- package/dist/execution/controller.d.ts.map +1 -0
- package/dist/execution/controller.js.map +1 -0
- package/dist/execution/event-manager.d.ts +29 -0
- package/dist/execution/event-manager.d.ts.map +1 -0
- package/dist/{graph → execution}/event-manager.js +4 -114
- package/dist/execution/event-manager.js.map +1 -0
- package/dist/{graph → execution}/index.d.ts +50 -2
- package/dist/execution/index.d.ts.map +1 -0
- package/dist/execution/index.js +638 -0
- package/dist/execution/index.js.map +1 -0
- package/dist/execution/logger.d.ts.map +1 -0
- package/dist/execution/logger.js.map +1 -0
- package/dist/{graph → execution}/node.d.ts +6 -1
- package/dist/execution/node.d.ts.map +1 -0
- package/dist/{graph → execution}/node.js +83 -10
- package/dist/execution/node.js.map +1 -0
- package/dist/execution/observer.d.ts.map +1 -0
- package/dist/execution/observer.js.map +1 -0
- package/dist/execution/planner.d.ts +14 -0
- package/dist/execution/planner.d.ts.map +1 -0
- package/dist/execution/planner.js +43 -0
- package/dist/execution/planner.js.map +1 -0
- package/dist/execution/reducer.d.ts +19 -0
- package/dist/execution/reducer.d.ts.map +1 -0
- package/dist/execution/reducer.js +61 -0
- package/dist/execution/reducer.js.map +1 -0
- package/dist/execution/registry.d.ts +38 -0
- package/dist/execution/registry.d.ts.map +1 -0
- package/dist/execution/registry.js +63 -0
- package/dist/execution/registry.js.map +1 -0
- package/dist/execution/send-api.d.ts +24 -0
- package/dist/execution/send-api.d.ts.map +1 -0
- package/dist/execution/send-api.js +52 -0
- package/dist/execution/send-api.js.map +1 -0
- package/dist/execution/types.parallel.d.ts +61 -0
- package/dist/execution/types.parallel.d.ts.map +1 -0
- package/dist/execution/types.parallel.js +3 -0
- package/dist/execution/types.parallel.js.map +1 -0
- package/dist/execution/visualizer.d.ts.map +1 -0
- package/dist/execution/visualizer.js.map +1 -0
- package/dist/index.d.ts +27 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +42 -8
- package/dist/index.js.map +1 -1
- package/dist/interfaces/index.d.ts +32 -1
- package/dist/interfaces/index.d.ts.map +1 -1
- package/dist/interfaces/index.js.map +1 -1
- package/dist/modules/cli/index.d.ts +13 -0
- package/dist/modules/cli/index.d.ts.map +1 -0
- package/dist/modules/cli/index.js +672 -0
- package/dist/modules/cli/index.js.map +1 -0
- package/dist/modules/nlp/engine.d.ts.map +1 -1
- package/dist/modules/nlp/engine.js +0 -1
- package/dist/modules/nlp/engine.js.map +1 -1
- package/dist/persistence/index.d.ts +12 -0
- package/dist/persistence/index.d.ts.map +1 -0
- package/dist/persistence/index.js +24 -0
- package/dist/persistence/index.js.map +1 -0
- package/dist/persistence/neo4j/driver.d.ts +26 -0
- package/dist/persistence/neo4j/driver.d.ts.map +1 -0
- package/dist/persistence/neo4j/driver.js +64 -0
- package/dist/persistence/neo4j/driver.js.map +1 -0
- package/dist/persistence/neo4j/entity-store.d.ts +15 -0
- package/dist/persistence/neo4j/entity-store.d.ts.map +1 -0
- package/dist/persistence/neo4j/entity-store.js +136 -0
- package/dist/persistence/neo4j/entity-store.js.map +1 -0
- package/dist/persistence/neo4j/execution-tracer.d.ts +19 -0
- package/dist/persistence/neo4j/execution-tracer.d.ts.map +1 -0
- package/dist/persistence/neo4j/execution-tracer.js +166 -0
- package/dist/persistence/neo4j/execution-tracer.js.map +1 -0
- package/dist/persistence/neo4j/memory-adapter.d.ts +35 -0
- package/dist/persistence/neo4j/memory-adapter.d.ts.map +1 -0
- package/dist/persistence/neo4j/memory-adapter.js +252 -0
- package/dist/persistence/neo4j/memory-adapter.js.map +1 -0
- package/dist/persistence/neo4j/petri-checkpoint-adapter.d.ts +22 -0
- package/dist/persistence/neo4j/petri-checkpoint-adapter.d.ts.map +1 -0
- package/dist/persistence/neo4j/petri-checkpoint-adapter.js +141 -0
- package/dist/persistence/neo4j/petri-checkpoint-adapter.js.map +1 -0
- package/dist/pipeline/agent-pipeline.d.ts +99 -0
- package/dist/pipeline/agent-pipeline.d.ts.map +1 -0
- package/dist/pipeline/agent-pipeline.js +356 -0
- package/dist/pipeline/agent-pipeline.js.map +1 -0
- package/dist/routing/checkpoint-adapter.d.ts +30 -0
- package/dist/routing/checkpoint-adapter.d.ts.map +1 -0
- package/dist/routing/checkpoint-adapter.js +83 -0
- package/dist/routing/checkpoint-adapter.js.map +1 -0
- package/dist/routing/documentation-generator.d.ts +47 -0
- package/dist/routing/documentation-generator.d.ts.map +1 -0
- package/dist/routing/documentation-generator.js +320 -0
- package/dist/routing/documentation-generator.js.map +1 -0
- package/dist/routing/index.d.ts +38 -0
- package/dist/routing/index.d.ts.map +1 -0
- package/dist/routing/index.js +375 -0
- package/dist/routing/index.js.map +1 -0
- package/dist/routing/intent-classifier.d.ts +206 -0
- package/dist/routing/intent-classifier.d.ts.map +1 -0
- package/dist/routing/intent-classifier.js +267 -0
- package/dist/routing/intent-classifier.js.map +1 -0
- package/dist/routing/matrix.d.ts +6 -0
- package/dist/routing/matrix.d.ts.map +1 -0
- package/dist/routing/matrix.js +131 -0
- package/dist/routing/matrix.js.map +1 -0
- package/dist/routing/orchestrator.d.ts +87 -0
- package/dist/routing/orchestrator.d.ts.map +1 -0
- package/dist/routing/orchestrator.js +425 -0
- package/dist/routing/orchestrator.js.map +1 -0
- package/dist/routing/postgres-checkpoint-adapter.d.ts +32 -0
- package/dist/routing/postgres-checkpoint-adapter.d.ts.map +1 -0
- package/dist/routing/postgres-checkpoint-adapter.js +167 -0
- package/dist/routing/postgres-checkpoint-adapter.js.map +1 -0
- package/dist/routing/redis-checkpoint-adapter.d.ts +34 -0
- package/dist/routing/redis-checkpoint-adapter.d.ts.map +1 -0
- package/dist/routing/redis-checkpoint-adapter.js +170 -0
- package/dist/routing/redis-checkpoint-adapter.js.map +1 -0
- package/dist/routing/types.d.ts +49 -0
- package/dist/routing/types.d.ts.map +1 -0
- package/dist/routing/types.js +3 -0
- package/dist/routing/types.js.map +1 -0
- package/dist/types/agent.d.ts +144 -88
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/agent.js +49 -0
- package/dist/types/agent.js.map +1 -1
- package/dist/types/index.d.ts +46 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/generate-action-schema.d.ts +2 -3
- package/dist/utils/generate-action-schema.d.ts.map +1 -1
- package/dist/utils/generate-action-schema.js +3 -1
- package/dist/utils/generate-action-schema.js.map +1 -1
- package/dist/utils/logger.d.ts +27 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +45 -0
- package/dist/utils/logger.js.map +1 -0
- package/docs/.gitbook/assets/image (1).png +0 -0
- package/docs/.gitbook/assets/image (2).png +0 -0
- package/docs/.gitbook/assets/image (3).png +0 -0
- package/docs/.gitbook/assets/image (4).png +0 -0
- package/docs/.gitbook/assets/image (5).png +0 -0
- package/docs/.gitbook/assets/image (6).png +0 -0
- package/docs/.gitbook/assets/image.png +0 -0
- package/docs/README.md +57 -0
- package/docs/SUMMARY.md +34 -0
- package/docs/cas-dusages.md +69 -0
- package/docs/cli/README.md +65 -0
- package/docs/concepts-cles.md +50 -0
- package/docs/core/architecture.md +87 -0
- package/docs/core/benchmark.md +59 -0
- package/docs/core/checkpoint.md +72 -0
- package/docs/core/documentation.md +55 -0
- package/docs/core/graphcontroller.md +63 -0
- package/docs/core/graphflow.md +137 -0
- package/docs/core/introduction.md +41 -0
- package/docs/core/les-evenements.md +95 -0
- package/docs/modules/agenda/README.md +63 -0
- package/docs/modules/agenda/interface-iagenda.md +170 -0
- package/docs/modules/agenda/les-adaptateurs/README.md +237 -0
- package/docs/modules/agenda/les-adaptateurs/nodecronadapter.md +91 -0
- package/docs/modules/introduction.md +55 -0
- package/docs/modules/les-adaptateurs.md +52 -0
- package/docs/modules/memoire/README.md +68 -0
- package/docs/modules/memoire/interface-imemory.md +183 -0
- package/docs/modules/memoire/les-adaptateurs/README.md +209 -0
- package/docs/modules/memoire/les-adaptateurs/inmemoryadapter.md +110 -0
- package/docs/modules/memoire/les-adaptateurs/meilisearchadapter.md +147 -0
- package/docs/modules/memoire/les-adaptateurs/redisadapter.md +212 -0
- package/docs/modules/nlp/README.md +44 -0
- package/docs/philosophie.md +51 -0
- package/docs/tutoriels/ajouter-des-conditions.md +150 -0
- package/docs/tutoriels/branching.md +194 -0
- package/docs/tutoriels/checkpoint-usage.md +99 -0
- package/docs/tutoriels/creer-agent-onchain.md +1041 -0
- package/docs/tutoriels/creer-un-agent.md +108 -0
- package/docs/tutoriels/creer-un-graphe-simple.md +92 -0
- package/docs/tutoriels/gerer-les-erreurs.md +124 -0
- package/docs/tutoriels/pour-commencer.md +73 -0
- package/docs/tutoriels/retry.md +166 -0
- package/execution/adapters/in-memory-checkpoint.ts +35 -0
- package/execution/compiler.ts +47 -0
- package/{graph → execution}/event-manager.ts +4 -117
- package/{graph → execution}/index.ts +376 -8
- package/{graph → execution}/node.ts +116 -12
- package/execution/planner.ts +40 -0
- package/execution/reducer.ts +73 -0
- package/execution/registry.ts +86 -0
- package/execution/send-api.ts +58 -0
- package/execution/types.parallel.ts +81 -0
- package/index.ts +35 -8
- package/interfaces/index.ts +37 -0
- package/modules/cli/index.ts +580 -0
- package/modules/nlp/engine.ts +0 -1
- package/package.json +50 -16
- package/persistence/index.ts +27 -0
- package/persistence/neo4j/driver.ts +34 -0
- package/persistence/neo4j/entity-store.ts +141 -0
- package/persistence/neo4j/execution-tracer.ts +194 -0
- package/persistence/neo4j/memory-adapter.ts +281 -0
- package/persistence/neo4j/petri-checkpoint-adapter.ts +153 -0
- package/pipeline/agent-pipeline.ts +426 -0
- package/routing/checkpoint-adapter.ts +79 -0
- package/routing/documentation-generator.ts +358 -0
- package/routing/index.ts +459 -0
- package/routing/intent-classifier.ts +360 -0
- package/routing/matrix.ts +138 -0
- package/routing/orchestrator.ts +498 -0
- package/routing/patterns/data-extraction.json +79 -0
- package/routing/patterns/human-approval.json +64 -0
- package/routing/patterns/rag-search.json +68 -0
- package/routing/postgres-checkpoint-adapter.ts +172 -0
- package/routing/redis-checkpoint-adapter.ts +187 -0
- package/routing/types.ts +59 -0
- package/routing/web-server.ts +260 -0
- package/scripts/generate-petri-docs.ts +70 -0
- package/scripts/get-gmail-token.js +65 -0
- package/scripts/get-gmail-token.ts +65 -0
- package/test/agent/agent.test.ts +92 -0
- package/test/agent/clone.test.ts +143 -0
- package/test/agent/cognitive-handler.test.ts +78 -0
- package/test/agent/entity-store.test.ts +80 -0
- package/test/agent/generic-executor.test.ts +230 -0
- package/test/agent/handoff.test.ts +163 -0
- package/test/agent/llm-factory.test.ts +40 -0
- package/test/agent/orchestrator.test.ts +156 -0
- package/test/agent/registry.test.ts +97 -0
- package/test/agent/tools.test.ts +267 -0
- package/test/execution/checkpoint.test.ts +811 -0
- package/test/{graph → execution}/controller.test.ts +2 -2
- package/test/{graph → execution}/event-manager.test.ts +1 -1
- package/test/{graph → execution}/index.test.ts +24 -15
- package/test/{graph → execution}/node.test.ts +3 -3
- package/test/{graph → execution}/observer.test.ts +2 -2
- package/test/execution/parallel.test.ts +135 -0
- package/test/execution/plan-llm-integration.test.ts +290 -0
- package/test/execution/plan-real-onchain.test.ts +226 -0
- package/test/execution/send-api.test.ts +121 -0
- package/test/modules/cli/index.test.ts +125 -0
- package/test/persistence/neo4j-execution-tracer.test.ts +96 -0
- package/test/persistence/neo4j-memory-adapter.test.ts +107 -0
- package/test/persistence/neo4j-petri-checkpoint.test.ts +89 -0
- package/test/pipeline/agent-pipeline.test.ts +118 -0
- package/test/routing/checkpoint-persistence.test.ts +58 -0
- package/test/routing/documentation-generator.test.ts +76 -0
- package/test/routing/integration.test.ts +261 -0
- package/test/routing/intent-classifier.test.ts +102 -0
- package/test/routing/petri.test.ts +156 -0
- package/test/routing/real-llm.test.ts +260 -0
- package/test-petri-features.ts +218 -0
- package/test-pipeline-api.ts +163 -0
- package/tsconfig.json +5 -2
- package/types/agent.ts +126 -4
- package/types/index.ts +66 -5
- package/utils/generate-action-schema.ts +5 -4
- package/utils/logger.ts +40 -0
- package/dist/graph/controller.d.ts.map +0 -1
- package/dist/graph/controller.js.map +0 -1
- package/dist/graph/event-manager.d.ts +0 -101
- package/dist/graph/event-manager.d.ts.map +0 -1
- package/dist/graph/event-manager.js.map +0 -1
- package/dist/graph/index.d.ts.map +0 -1
- package/dist/graph/index.js +0 -340
- package/dist/graph/index.js.map +0 -1
- package/dist/graph/logger.d.ts.map +0 -1
- package/dist/graph/logger.js.map +0 -1
- package/dist/graph/node.d.ts.map +0 -1
- package/dist/graph/node.js.map +0 -1
- package/dist/graph/observer.d.ts.map +0 -1
- package/dist/graph/observer.js.map +0 -1
- package/dist/graph/visualizer.d.ts.map +0 -1
- package/dist/graph/visualizer.js.map +0 -1
- package/dist/modules/agent/agent.d.ts +0 -35
- package/dist/modules/agent/agent.d.ts.map +0 -1
- package/dist/modules/agent/agent.js +0 -103
- package/dist/modules/agent/agent.js.map +0 -1
- package/dist/modules/agent/base/executor.d.ts.map +0 -1
- package/dist/modules/agent/base/executor.js.map +0 -1
- package/dist/modules/agent/base/index.d.ts.map +0 -1
- package/dist/modules/agent/base/index.js.map +0 -1
- package/dist/modules/agent/generic-assistant.d.ts +0 -20
- package/dist/modules/agent/generic-assistant.d.ts.map +0 -1
- package/dist/modules/agent/generic-assistant.js +0 -89
- package/dist/modules/agent/generic-assistant.js.map +0 -1
- package/dist/modules/agent/generic-executor.d.ts.map +0 -1
- package/dist/modules/agent/generic-executor.js +0 -189
- package/dist/modules/agent/generic-executor.js.map +0 -1
- package/dist/modules/agent/llm-factory.d.ts.map +0 -1
- package/dist/modules/agent/llm-factory.js +0 -56
- package/dist/modules/agent/llm-factory.js.map +0 -1
- package/dist/modules/agent/prompt-builder.d.ts.map +0 -1
- package/dist/modules/agent/prompt-builder.js.map +0 -1
- package/dist/modules/memory/adapters/in-memory/index.d.ts +0 -120
- package/dist/modules/memory/adapters/in-memory/index.d.ts.map +0 -1
- package/dist/modules/memory/adapters/in-memory/index.js +0 -212
- package/dist/modules/memory/adapters/in-memory/index.js.map +0 -1
- package/dist/modules/memory/adapters/meilisearch/index.d.ts +0 -110
- package/dist/modules/memory/adapters/meilisearch/index.d.ts.map +0 -1
- package/dist/modules/memory/adapters/meilisearch/index.js +0 -324
- package/dist/modules/memory/adapters/meilisearch/index.js.map +0 -1
- package/dist/modules/memory/adapters/redis/index.d.ts +0 -82
- package/dist/modules/memory/adapters/redis/index.d.ts.map +0 -1
- package/dist/modules/memory/adapters/redis/index.js +0 -160
- package/dist/modules/memory/adapters/redis/index.js.map +0 -1
- package/dist/modules/memory/index.d.ts +0 -67
- package/dist/modules/memory/index.d.ts.map +0 -1
- package/dist/modules/memory/index.js +0 -104
- package/dist/modules/memory/index.js.map +0 -1
- package/modules/agent/agent.ts +0 -107
- package/modules/agent/generic-assistant.ts +0 -92
- package/modules/agent/generic-executor.ts +0 -252
- package/modules/agent/llm-factory.ts +0 -47
- package/modules/memory/adapters/in-memory/index.ts +0 -208
- package/modules/memory/adapters/meilisearch/index.ts +0 -364
- package/modules/memory/adapters/redis/index.ts +0 -165
- package/modules/memory/index.ts +0 -93
- package/test/modules/memory/adapters/in-memory.test.ts +0 -153
- package/test/modules/memory/adapters/meilisearch.test.ts +0 -287
- package/test/modules/memory/base.test.ts +0 -230
- /package/dist/{modules/agent → agent}/llm-factory.d.ts +0 -0
- /package/dist/{modules/agent → agent}/prompt-builder.js +0 -0
- /package/dist/{graph → execution}/controller.d.ts +0 -0
- /package/dist/{graph → execution}/controller.js +0 -0
- /package/dist/{graph → execution}/logger.d.ts +0 -0
- /package/dist/{graph → execution}/logger.js +0 -0
- /package/dist/{graph → execution}/observer.d.ts +0 -0
- /package/dist/{graph → execution}/observer.js +0 -0
- /package/dist/{graph → execution}/visualizer.d.ts +0 -0
- /package/dist/{graph → execution}/visualizer.js +0 -0
- /package/{graph → execution}/controller.ts +0 -0
- /package/{graph → execution}/logger.ts +0 -0
- /package/{graph → execution}/observer.ts +0 -0
- /package/{graph → execution}/visualizer.ts +0 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { Memory } from "../modules/memory";
|
|
2
|
+
export { InMemoryCheckpointAdapter } from "../execution/adapters/in-memory-checkpoint";
|
|
3
|
+
export {
|
|
4
|
+
IPetriCheckpointAdapter,
|
|
5
|
+
InMemoryPetriCheckpointAdapter,
|
|
6
|
+
} from "../routing/checkpoint-adapter";
|
|
7
|
+
export { RedisPetriCheckpointAdapter } from "../routing/redis-checkpoint-adapter";
|
|
8
|
+
export { PostgresPetriCheckpointAdapter } from "../routing/postgres-checkpoint-adapter";
|
|
9
|
+
export {
|
|
10
|
+
Neo4jExecutionTracer,
|
|
11
|
+
} from "./neo4j/execution-tracer";
|
|
12
|
+
export {
|
|
13
|
+
Neo4jPetriCheckpointAdapter,
|
|
14
|
+
} from "./neo4j/petri-checkpoint-adapter";
|
|
15
|
+
export {
|
|
16
|
+
Neo4jMemoryAdapter,
|
|
17
|
+
} from "./neo4j/memory-adapter";
|
|
18
|
+
export {
|
|
19
|
+
Neo4jEntityStore,
|
|
20
|
+
IEntityStore,
|
|
21
|
+
} from "./neo4j/entity-store";
|
|
22
|
+
export type {
|
|
23
|
+
Neo4jConfig,
|
|
24
|
+
Neo4jDriverLike,
|
|
25
|
+
Neo4jSessionLike,
|
|
26
|
+
} from "./neo4j/driver";
|
|
27
|
+
export { createDriver } from "./neo4j/driver";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface Neo4jSessionLike {
|
|
2
|
+
run(query: string, params?: Record<string, any>): Promise<{ records: any[] }>;
|
|
3
|
+
close(): Promise<void>;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface Neo4jDriverLike {
|
|
7
|
+
session(config?: { database?: string }): Neo4jSessionLike;
|
|
8
|
+
close(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type Neo4jConfig =
|
|
12
|
+
| { driver: Neo4jDriverLike; database?: string }
|
|
13
|
+
| { url: string; username: string; password: string; database?: string };
|
|
14
|
+
|
|
15
|
+
export async function createDriver(config: Neo4jConfig): Promise<{ driver: Neo4jDriverLike; database?: string }> {
|
|
16
|
+
if ("driver" in config) {
|
|
17
|
+
return { driver: config.driver, database: config.database };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let neo4j: any;
|
|
21
|
+
try {
|
|
22
|
+
neo4j = await import("neo4j-driver");
|
|
23
|
+
} catch {
|
|
24
|
+
throw new Error(
|
|
25
|
+
"neo4j-driver is not installed. Install it with: npm install neo4j-driver"
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const driver = neo4j.default.auth
|
|
30
|
+
? neo4j.default.driver(config.url, neo4j.default.auth.basic(config.username, config.password))
|
|
31
|
+
: neo4j.default.driver(config.url, neo4j.default.auth.basic(config.username, config.password));
|
|
32
|
+
|
|
33
|
+
return { driver, database: config.database };
|
|
34
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Entity } from "../../agent/entity-resolver/types";
|
|
2
|
+
import { IEntityStore } from "../../agent/entity-resolver/entity-index";
|
|
3
|
+
import { Neo4jDriverLike, Neo4jSessionLike } from "./driver";
|
|
4
|
+
|
|
5
|
+
export { IEntityStore };
|
|
6
|
+
|
|
7
|
+
export class Neo4jEntityStore implements IEntityStore {
|
|
8
|
+
constructor(
|
|
9
|
+
private driver: Neo4jDriverLike,
|
|
10
|
+
private database?: string
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
private session(): Neo4jSessionLike {
|
|
14
|
+
return this.driver.session(this.database ? { database: this.database } : undefined);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async upsertMany(entities: Entity[]): Promise<void> {
|
|
18
|
+
const s = this.session();
|
|
19
|
+
try {
|
|
20
|
+
for (const entity of entities) {
|
|
21
|
+
await s.run(
|
|
22
|
+
`MERGE (e:Entity {id: $id})
|
|
23
|
+
SET e.category = $category,
|
|
24
|
+
e.filename = $filename,
|
|
25
|
+
e.stem = $stem,
|
|
26
|
+
e.extension = $extension,
|
|
27
|
+
e.path = $path,
|
|
28
|
+
e.scope = $scope
|
|
29
|
+
WITH e
|
|
30
|
+
UNWIND $tokens AS tokenValue
|
|
31
|
+
MERGE (t:Token {value: tokenValue})
|
|
32
|
+
MERGE (e)-[:HAS_TOKEN]->(t)`,
|
|
33
|
+
{
|
|
34
|
+
id: entity.id,
|
|
35
|
+
category: entity.category,
|
|
36
|
+
filename: entity.filename,
|
|
37
|
+
stem: entity.stem,
|
|
38
|
+
extension: entity.extension,
|
|
39
|
+
path: entity.path,
|
|
40
|
+
scope: entity.path.split("/")[0] || "root",
|
|
41
|
+
tokens: entity.tokens,
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const dir = entity.path.substring(0, entity.path.lastIndexOf("/")) || "/";
|
|
46
|
+
await s.run(
|
|
47
|
+
`MATCH (e:Entity {id: $id})
|
|
48
|
+
MERGE (d:Dir {path: $dir})
|
|
49
|
+
MERGE (e)-[:IN_DIR]->(d)`,
|
|
50
|
+
{ id: entity.id, dir }
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
} finally {
|
|
54
|
+
await s.close();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async loadAll(scope?: string): Promise<Entity[]> {
|
|
59
|
+
const s = this.session();
|
|
60
|
+
try {
|
|
61
|
+
let query: string;
|
|
62
|
+
let params: Record<string, any> = {};
|
|
63
|
+
|
|
64
|
+
if (scope) {
|
|
65
|
+
query = `MATCH (e:Entity)
|
|
66
|
+
WHERE e.scope = $scope
|
|
67
|
+
OPTIONAL MATCH (e)-[:HAS_TOKEN]->(t:Token)
|
|
68
|
+
RETURN e, collect(t.value) AS tokens
|
|
69
|
+
ORDER BY e.filename`;
|
|
70
|
+
params.scope = scope;
|
|
71
|
+
} else {
|
|
72
|
+
query = `MATCH (e:Entity)
|
|
73
|
+
OPTIONAL MATCH (e)-[:HAS_TOKEN]->(t:Token)
|
|
74
|
+
RETURN e, collect(t.value) AS tokens
|
|
75
|
+
ORDER BY e.filename`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const result = await s.run(query, params);
|
|
79
|
+
|
|
80
|
+
return result.records.map((r: any) => {
|
|
81
|
+
const entity = r.get("e");
|
|
82
|
+
const props = entity.properties ? (typeof entity.properties === "function" ? entity.properties() : entity.properties) : entity;
|
|
83
|
+
return {
|
|
84
|
+
id: props.id,
|
|
85
|
+
category: props.category || "file",
|
|
86
|
+
filename: props.filename,
|
|
87
|
+
stem: props.stem,
|
|
88
|
+
extension: props.extension || null,
|
|
89
|
+
tokens: r.get("tokens") || [],
|
|
90
|
+
path: props.path,
|
|
91
|
+
} as Entity;
|
|
92
|
+
});
|
|
93
|
+
} finally {
|
|
94
|
+
await s.close();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async recordResolution(
|
|
99
|
+
entityId: string,
|
|
100
|
+
query: string,
|
|
101
|
+
confidence: number
|
|
102
|
+
): Promise<void> {
|
|
103
|
+
const s = this.session();
|
|
104
|
+
try {
|
|
105
|
+
await s.run(
|
|
106
|
+
`MATCH (e:Entity {id: $entityId})
|
|
107
|
+
CREATE (r:Resolution {id: $resolutionId, query: $query, confidence: $confidence, at: $at})
|
|
108
|
+
MERGE (e)-[:RESOLVED_BY]->(r)`,
|
|
109
|
+
{
|
|
110
|
+
entityId,
|
|
111
|
+
resolutionId: `res-${entityId}-${Date.now()}`,
|
|
112
|
+
query,
|
|
113
|
+
confidence,
|
|
114
|
+
at: new Date().toISOString(),
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
} finally {
|
|
118
|
+
await s.close();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async clear(scope?: string): Promise<void> {
|
|
123
|
+
const s = this.session();
|
|
124
|
+
try {
|
|
125
|
+
if (scope) {
|
|
126
|
+
await s.run(
|
|
127
|
+
`MATCH (e:Entity {scope: $scope})
|
|
128
|
+
DETACH DELETE e`,
|
|
129
|
+
{ scope }
|
|
130
|
+
);
|
|
131
|
+
} else {
|
|
132
|
+
await s.run(
|
|
133
|
+
`MATCH (e:Entity)
|
|
134
|
+
DETACH DELETE e`
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
} finally {
|
|
138
|
+
await s.close();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { ZodSchema } from "zod";
|
|
2
|
+
import { GraphFlow } from "../../execution";
|
|
3
|
+
import { Neo4jDriverLike, Neo4jSessionLike } from "./driver";
|
|
4
|
+
|
|
5
|
+
export class Neo4jExecutionTracer {
|
|
6
|
+
private driver: Neo4jDriverLike;
|
|
7
|
+
private database?: string;
|
|
8
|
+
private unsubscribeFns: Array<() => void> = [];
|
|
9
|
+
private executionId: string;
|
|
10
|
+
|
|
11
|
+
private constructor(
|
|
12
|
+
driver: Neo4jDriverLike,
|
|
13
|
+
executionId: string,
|
|
14
|
+
database?: string
|
|
15
|
+
) {
|
|
16
|
+
this.driver = driver;
|
|
17
|
+
this.executionId = executionId;
|
|
18
|
+
this.database = database;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static attach<T extends ZodSchema>(
|
|
22
|
+
graph: GraphFlow<T>,
|
|
23
|
+
driver: Neo4jDriverLike,
|
|
24
|
+
executionId?: string,
|
|
25
|
+
database?: string
|
|
26
|
+
): Neo4jExecutionTracer {
|
|
27
|
+
const id = executionId || crypto.randomUUID();
|
|
28
|
+
const tracer = new Neo4jExecutionTracer(driver, id, database);
|
|
29
|
+
|
|
30
|
+
const onStarted = (data: any) => {
|
|
31
|
+
tracer.onNodeStarted(data).catch(() => {});
|
|
32
|
+
};
|
|
33
|
+
graph.on("nodeStarted", onStarted);
|
|
34
|
+
tracer.unsubscribeFns.push(() => {
|
|
35
|
+
if (typeof (graph as any).removeListener === "function") {
|
|
36
|
+
(graph as any).removeListener("nodeStarted", onStarted);
|
|
37
|
+
} else if (typeof (graph as any).off === "function") {
|
|
38
|
+
(graph as any).off("nodeStarted", onStarted);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const onCompleted = (data: any) => {
|
|
43
|
+
tracer.onNodeCompleted(data).catch(() => {});
|
|
44
|
+
};
|
|
45
|
+
graph.on("nodeCompleted", onCompleted);
|
|
46
|
+
tracer.unsubscribeFns.push(() => {
|
|
47
|
+
if (typeof (graph as any).removeListener === "function") {
|
|
48
|
+
(graph as any).removeListener("nodeCompleted", onCompleted);
|
|
49
|
+
} else if (typeof (graph as any).off === "function") {
|
|
50
|
+
(graph as any).off("nodeCompleted", onCompleted);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const onError = (data: any) => {
|
|
55
|
+
tracer.onNodeError(data).catch(() => {});
|
|
56
|
+
};
|
|
57
|
+
graph.on("nodeError", onError);
|
|
58
|
+
tracer.unsubscribeFns.push(() => {
|
|
59
|
+
if (typeof (graph as any).removeListener === "function") {
|
|
60
|
+
(graph as any).removeListener("nodeError", onError);
|
|
61
|
+
} else if (typeof (graph as any).off === "function") {
|
|
62
|
+
(graph as any).off("nodeError", onError);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return tracer;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
getExecutionId(): string {
|
|
70
|
+
return this.executionId;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
detach(): void {
|
|
74
|
+
for (const fn of this.unsubscribeFns) {
|
|
75
|
+
fn();
|
|
76
|
+
}
|
|
77
|
+
this.unsubscribeFns = [];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async initExecution(
|
|
81
|
+
graphName: string,
|
|
82
|
+
startedAt: Date = new Date()
|
|
83
|
+
): Promise<void> {
|
|
84
|
+
const session = this.driver.session(this.database ? { database: this.database } : undefined);
|
|
85
|
+
try {
|
|
86
|
+
await session.run(
|
|
87
|
+
`MERGE (e:Execution {id: $id})
|
|
88
|
+
SET e.graphName = $graphName,
|
|
89
|
+
e.startedAt = $startedAt,
|
|
90
|
+
e.status = "running"`,
|
|
91
|
+
{ id: this.executionId, graphName, startedAt: startedAt.toISOString() }
|
|
92
|
+
);
|
|
93
|
+
} finally {
|
|
94
|
+
await session.close();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async completeExecution(
|
|
99
|
+
status: "completed" | "failed" = "completed",
|
|
100
|
+
completedAt: Date = new Date()
|
|
101
|
+
): Promise<void> {
|
|
102
|
+
const session = this.driver.session(this.database ? { database: this.database } : undefined);
|
|
103
|
+
try {
|
|
104
|
+
await session.run(
|
|
105
|
+
`MATCH (e:Execution {id: $id})
|
|
106
|
+
SET e.status = $status,
|
|
107
|
+
e.completedAt = $completedAt`,
|
|
108
|
+
{ id: this.executionId, status, completedAt: completedAt.toISOString() }
|
|
109
|
+
);
|
|
110
|
+
} finally {
|
|
111
|
+
await session.close();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private async onNodeStarted(data: {
|
|
116
|
+
name?: string;
|
|
117
|
+
payload?: { name?: string };
|
|
118
|
+
}): Promise<void> {
|
|
119
|
+
const name = data.name || data.payload?.name || "unknown";
|
|
120
|
+
const nodeRunId = `${this.executionId}-${name}`;
|
|
121
|
+
|
|
122
|
+
const session = this.driver.session(this.database ? { database: this.database } : undefined);
|
|
123
|
+
try {
|
|
124
|
+
await session.run(
|
|
125
|
+
`MATCH (e:Execution {id: $executionId})
|
|
126
|
+
MERGE (n:NodeRun {id: $nodeRunId})
|
|
127
|
+
SET n.name = $name,
|
|
128
|
+
n.startedAt = $startedAt,
|
|
129
|
+
n.status = "running"
|
|
130
|
+
MERGE (e)-[:HAS_RUN]->(n)`,
|
|
131
|
+
{
|
|
132
|
+
executionId: this.executionId,
|
|
133
|
+
nodeRunId,
|
|
134
|
+
name,
|
|
135
|
+
startedAt: new Date().toISOString(),
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
} finally {
|
|
139
|
+
await session.close();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private async onNodeCompleted(data: {
|
|
144
|
+
name?: string;
|
|
145
|
+
payload?: { name?: string; context?: any };
|
|
146
|
+
}): Promise<void> {
|
|
147
|
+
const name = data.name || data.payload?.name || "unknown";
|
|
148
|
+
const nodeRunId = `${this.executionId}-${name}`;
|
|
149
|
+
|
|
150
|
+
const session = this.driver.session(this.database ? { database: this.database } : undefined);
|
|
151
|
+
try {
|
|
152
|
+
await session.run(
|
|
153
|
+
`MATCH (n:NodeRun {id: $nodeRunId})
|
|
154
|
+
SET n.status = "completed",
|
|
155
|
+
n.completedAt = $completedAt`,
|
|
156
|
+
{
|
|
157
|
+
nodeRunId,
|
|
158
|
+
completedAt: new Date().toISOString(),
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
} finally {
|
|
162
|
+
await session.close();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private async onNodeError(data: {
|
|
167
|
+
name?: string;
|
|
168
|
+
payload?: { name?: string; error?: any };
|
|
169
|
+
}): Promise<void> {
|
|
170
|
+
const name = data.name || data.payload?.name || "unknown";
|
|
171
|
+
const nodeRunId = `${this.executionId}-${name}`;
|
|
172
|
+
const errorMessage =
|
|
173
|
+
data.payload?.error?.message ||
|
|
174
|
+
data.payload?.error?.toString() ||
|
|
175
|
+
"Unknown error";
|
|
176
|
+
|
|
177
|
+
const session = this.driver.session(this.database ? { database: this.database } : undefined);
|
|
178
|
+
try {
|
|
179
|
+
await session.run(
|
|
180
|
+
`MATCH (n:NodeRun {id: $nodeRunId})
|
|
181
|
+
SET n.status = "error",
|
|
182
|
+
n.error = $error,
|
|
183
|
+
n.completedAt = $completedAt`,
|
|
184
|
+
{
|
|
185
|
+
nodeRunId,
|
|
186
|
+
error: errorMessage,
|
|
187
|
+
completedAt: new Date().toISOString(),
|
|
188
|
+
}
|
|
189
|
+
);
|
|
190
|
+
} finally {
|
|
191
|
+
await session.close();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { IGraphMemoryAdapter, MemoryRelation } from "../../interfaces";
|
|
2
|
+
import {
|
|
3
|
+
BaseMemoryType,
|
|
4
|
+
CreateMemoryInput,
|
|
5
|
+
} from "../../types";
|
|
6
|
+
import { Neo4jDriverLike, Neo4jSessionLike } from "./driver";
|
|
7
|
+
|
|
8
|
+
export class Neo4jMemoryAdapter implements IGraphMemoryAdapter {
|
|
9
|
+
constructor(
|
|
10
|
+
private driver: Neo4jDriverLike,
|
|
11
|
+
private database?: string
|
|
12
|
+
) {}
|
|
13
|
+
|
|
14
|
+
private session(): Neo4jSessionLike {
|
|
15
|
+
return this.driver.session(this.database ? { database: this.database } : undefined);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async init(roomId?: string): Promise<void> {
|
|
19
|
+
if (!roomId) return;
|
|
20
|
+
const s = this.session();
|
|
21
|
+
try {
|
|
22
|
+
await s.run(
|
|
23
|
+
`MERGE (:Room {id: $roomId})`,
|
|
24
|
+
{ roomId }
|
|
25
|
+
);
|
|
26
|
+
} finally {
|
|
27
|
+
await s.close();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async createMemory(
|
|
32
|
+
input: CreateMemoryInput & { embedding?: number[] }
|
|
33
|
+
): Promise<BaseMemoryType | undefined> {
|
|
34
|
+
const memory: BaseMemoryType = {
|
|
35
|
+
id: input.id || crypto.randomUUID(),
|
|
36
|
+
content: input.content,
|
|
37
|
+
metadata: input.metadata,
|
|
38
|
+
embedding: input.embedding,
|
|
39
|
+
roomId: input.roomId,
|
|
40
|
+
createdAt: new Date(),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const s = this.session();
|
|
44
|
+
try {
|
|
45
|
+
await s.run(
|
|
46
|
+
`MERGE (r:Room {id: $roomId})
|
|
47
|
+
CREATE (m:Memory {
|
|
48
|
+
id: $id,
|
|
49
|
+
content: $content,
|
|
50
|
+
createdAt: $createdAt,
|
|
51
|
+
metadata: $metadata
|
|
52
|
+
})
|
|
53
|
+
MERGE (m)-[:IN_ROOM]->(r)
|
|
54
|
+
RETURN m`,
|
|
55
|
+
{
|
|
56
|
+
roomId: memory.roomId,
|
|
57
|
+
id: memory.id,
|
|
58
|
+
content: memory.content,
|
|
59
|
+
createdAt: memory.createdAt.toISOString(),
|
|
60
|
+
metadata: memory.metadata ? JSON.stringify(memory.metadata) : null,
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (memory.embedding) {
|
|
65
|
+
try {
|
|
66
|
+
await s.run(
|
|
67
|
+
`MATCH (m:Memory {id: $id})
|
|
68
|
+
SET m.embedding = $embedding`,
|
|
69
|
+
{ id: memory.id, embedding: memory.embedding }
|
|
70
|
+
);
|
|
71
|
+
} catch {
|
|
72
|
+
// vector index may not exist; embedding stored as best-effort
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return memory;
|
|
77
|
+
} finally {
|
|
78
|
+
await s.close();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async getMemoryById(
|
|
83
|
+
id: string,
|
|
84
|
+
roomId: string
|
|
85
|
+
): Promise<BaseMemoryType | null> {
|
|
86
|
+
const s = this.session();
|
|
87
|
+
try {
|
|
88
|
+
const result = await s.run(
|
|
89
|
+
`MATCH (m:Memory {id: $id})-[:IN_ROOM]->(:Room {id: $roomId})
|
|
90
|
+
RETURN m`,
|
|
91
|
+
{ id, roomId }
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
if (result.records.length === 0) return null;
|
|
95
|
+
return this.recordToMemory(result.records[0].get("m"));
|
|
96
|
+
} finally {
|
|
97
|
+
await s.close();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async getMemoryByIndex(
|
|
102
|
+
query: string,
|
|
103
|
+
options: { roomId: string; limit?: number }
|
|
104
|
+
): Promise<BaseMemoryType[]> {
|
|
105
|
+
const s = this.session();
|
|
106
|
+
try {
|
|
107
|
+
const result = await s.run(
|
|
108
|
+
`CALL db.index.fulltext.queryNodes("memory_content", $query)
|
|
109
|
+
YIELD node AS m, score
|
|
110
|
+
WHERE (m)-[:IN_ROOM]->(:Room {id: $roomId})
|
|
111
|
+
RETURN m, score
|
|
112
|
+
ORDER BY score DESC
|
|
113
|
+
LIMIT toInteger($limit)`,
|
|
114
|
+
{
|
|
115
|
+
query,
|
|
116
|
+
roomId: options.roomId,
|
|
117
|
+
limit: options.limit || 10,
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return result.records.map((r: any) => this.recordToMemory(r.get("m")));
|
|
122
|
+
} catch {
|
|
123
|
+
const result = await s.run(
|
|
124
|
+
`MATCH (m:Memory)-[:IN_ROOM]->(:Room {id: $roomId})
|
|
125
|
+
WHERE m.content CONTAINS $query
|
|
126
|
+
RETURN m
|
|
127
|
+
LIMIT toInteger($limit)`,
|
|
128
|
+
{
|
|
129
|
+
roomId: options.roomId,
|
|
130
|
+
query,
|
|
131
|
+
limit: options.limit || 10,
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
return result.records.map((r: any) => this.recordToMemory(r.get("m")));
|
|
135
|
+
} finally {
|
|
136
|
+
await s.close();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
async getAllMemories(roomId: string): Promise<BaseMemoryType[]> {
|
|
141
|
+
const s = this.session();
|
|
142
|
+
try {
|
|
143
|
+
const result = await s.run(
|
|
144
|
+
`MATCH (m:Memory)-[:IN_ROOM]->(:Room {id: $roomId})
|
|
145
|
+
RETURN m
|
|
146
|
+
ORDER BY m.createdAt DESC`,
|
|
147
|
+
{ roomId }
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
return result.records.map((r: any) => this.recordToMemory(r.get("m")));
|
|
151
|
+
} finally {
|
|
152
|
+
await s.close();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async clearMemoryById(id: string, roomId: string): Promise<void> {
|
|
157
|
+
const s = this.session();
|
|
158
|
+
try {
|
|
159
|
+
await s.run(
|
|
160
|
+
`MATCH (m:Memory {id: $id})-[:IN_ROOM]->(:Room {id: $roomId})
|
|
161
|
+
DETACH DELETE m`,
|
|
162
|
+
{ id, roomId }
|
|
163
|
+
);
|
|
164
|
+
} finally {
|
|
165
|
+
await s.close();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async clearAllMemories(): Promise<void> {
|
|
170
|
+
const s = this.session();
|
|
171
|
+
try {
|
|
172
|
+
await s.run(`MATCH (m:Memory) DETACH DELETE m`);
|
|
173
|
+
} finally {
|
|
174
|
+
await s.close();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async addRelation(relation: MemoryRelation): Promise<void> {
|
|
179
|
+
const s = this.session();
|
|
180
|
+
try {
|
|
181
|
+
await s.run(
|
|
182
|
+
`MATCH (from:Memory {id: $fromId})
|
|
183
|
+
MATCH (to:Memory {id: $toId})
|
|
184
|
+
MERGE (from)-[r:RELATES_TO {type: $type}]->(to)
|
|
185
|
+
SET r.weight = $weight,
|
|
186
|
+
r.createdAt = $createdAt`,
|
|
187
|
+
{
|
|
188
|
+
fromId: relation.fromId,
|
|
189
|
+
toId: relation.toId,
|
|
190
|
+
type: relation.type,
|
|
191
|
+
weight: relation.properties?.weight ?? 1,
|
|
192
|
+
createdAt: new Date().toISOString(),
|
|
193
|
+
}
|
|
194
|
+
);
|
|
195
|
+
} finally {
|
|
196
|
+
await s.close();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async removeRelation(
|
|
201
|
+
fromId: string,
|
|
202
|
+
toId: string,
|
|
203
|
+
type: string,
|
|
204
|
+
_roomId: string
|
|
205
|
+
): Promise<void> {
|
|
206
|
+
const s = this.session();
|
|
207
|
+
try {
|
|
208
|
+
await s.run(
|
|
209
|
+
`MATCH (from:Memory {id: $fromId})-[r:RELATES_TO {type: $type}]->(to:Memory {id: $toId})
|
|
210
|
+
DELETE r`,
|
|
211
|
+
{ fromId, toId, type }
|
|
212
|
+
);
|
|
213
|
+
} finally {
|
|
214
|
+
await s.close();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async getRelated(
|
|
219
|
+
id: string,
|
|
220
|
+
options: {
|
|
221
|
+
roomId: string;
|
|
222
|
+
relationTypes?: string[];
|
|
223
|
+
direction?: "in" | "out" | "both";
|
|
224
|
+
depth?: number;
|
|
225
|
+
limit?: number;
|
|
226
|
+
}
|
|
227
|
+
): Promise<Array<{ memory: BaseMemoryType; relationPath: string[] }>> {
|
|
228
|
+
const depth = options.depth ?? 1;
|
|
229
|
+
const direction = options.direction ?? "both";
|
|
230
|
+
const limit = options.limit ?? 10;
|
|
231
|
+
|
|
232
|
+
let directionPattern: string;
|
|
233
|
+
if (direction === "out") directionPattern = "(m:Memory)-[r:RELATES_TO]->(related)";
|
|
234
|
+
else if (direction === "in") directionPattern = "(m:Memory)<-[r:RELATES_TO]-(related)";
|
|
235
|
+
else directionPattern = "(m:Memory)-[r:RELATES_TO]-(related)";
|
|
236
|
+
|
|
237
|
+
let typeFilter = "";
|
|
238
|
+
if (options.relationTypes && options.relationTypes.length > 0) {
|
|
239
|
+
const types = options.relationTypes.map(t => `r.type = "${t}"`).join(" OR ");
|
|
240
|
+
typeFilter = `AND (${types})`;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const s = this.session();
|
|
244
|
+
try {
|
|
245
|
+
const query = depth === 1
|
|
246
|
+
? `MATCH ${directionPattern}
|
|
247
|
+
WHERE m.id = $id ${typeFilter}
|
|
248
|
+
RETURN DISTINCT related AS related, [] AS path
|
|
249
|
+
LIMIT toInteger($limit)`
|
|
250
|
+
: `MATCH (m:Memory {id: $id})
|
|
251
|
+
CALL {
|
|
252
|
+
MATCH (m)-[r:RELATES_TO*1..${depth}]-(related:Memory)
|
|
253
|
+
WHERE m.id <> related.id ${typeFilter ? `AND ${typeFilter.replace(/r\./g, "r0.")}` : ""}
|
|
254
|
+
RETURN DISTINCT related, [r IN r | r.type] AS path
|
|
255
|
+
}
|
|
256
|
+
RETURN related, path
|
|
257
|
+
LIMIT toInteger($limit)`;
|
|
258
|
+
|
|
259
|
+
const result = await s.run(query, { id, limit });
|
|
260
|
+
|
|
261
|
+
return result.records.map((r: any) => ({
|
|
262
|
+
memory: this.recordToMemory(r.get("related")),
|
|
263
|
+
relationPath: r.get("path") || [],
|
|
264
|
+
}));
|
|
265
|
+
} finally {
|
|
266
|
+
await s.close();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private recordToMemory(record: any): BaseMemoryType {
|
|
271
|
+
const props = record.properties ? (typeof record.properties === "function" ? record.properties() : record.properties) : record;
|
|
272
|
+
return {
|
|
273
|
+
id: props.id,
|
|
274
|
+
content: props.content,
|
|
275
|
+
metadata: props.metadata ? (typeof props.metadata === "string" ? JSON.parse(props.metadata) : props.metadata) : undefined,
|
|
276
|
+
embedding: props.embedding,
|
|
277
|
+
roomId: props.roomId,
|
|
278
|
+
createdAt: new Date(props.createdAt),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
}
|