@ai.ntellect/core 1.0.0 → 1.1.1
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 +6 -0
- package/AGENTS.md +50 -0
- package/ARCHITECTURE.md +58 -0
- package/BENCHMARK.md +38 -0
- package/CHANGELOG.md +224 -0
- package/README.md +363 -142
- package/agent/agent-workflow.ts +569 -0
- package/agent/agent.ts +183 -0
- package/agent/base/executor.ts +103 -0
- package/agent/base/index.ts +87 -0
- 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/agent/prompt-builder.ts +78 -0
- 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/agent/base/executor.d.ts +51 -0
- package/dist/agent/base/executor.d.ts.map +1 -0
- package/dist/agent/base/executor.js +69 -0
- package/dist/agent/base/executor.js.map +1 -0
- package/dist/agent/base/index.d.ts +30 -0
- package/dist/agent/base/index.d.ts.map +1 -0
- package/dist/agent/base/index.js +88 -0
- 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/agent/generic-executor.d.ts +68 -0
- 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 +19 -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/agent/prompt-builder.d.ts +35 -0
- package/dist/agent/prompt-builder.d.ts.map +1 -0
- package/dist/agent/prompt-builder.js +76 -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 +30 -0
- package/dist/execution/controller.d.ts.map +1 -0
- package/dist/execution/controller.js +80 -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/execution/event-manager.js +264 -0
- package/dist/execution/event-manager.js.map +1 -0
- package/dist/execution/index.d.ts +221 -0
- 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 +46 -0
- package/dist/execution/logger.d.ts.map +1 -0
- package/dist/execution/logger.js +69 -0
- package/dist/execution/logger.js.map +1 -0
- package/dist/execution/node.d.ts +65 -0
- package/dist/execution/node.d.ts.map +1 -0
- package/dist/execution/node.js +250 -0
- package/dist/execution/node.js.map +1 -0
- package/dist/execution/observer.d.ts +38 -0
- package/dist/execution/observer.d.ts.map +1 -0
- package/dist/execution/observer.js +111 -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 +35 -0
- package/dist/execution/visualizer.d.ts.map +1 -0
- package/dist/execution/visualizer.js +131 -0
- package/dist/execution/visualizer.js.map +1 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/index.d.ts +487 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/index.js +75 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/modules/agenda/adapters/node-cron/index.d.ts +17 -0
- package/dist/modules/agenda/adapters/node-cron/index.d.ts.map +1 -0
- package/dist/modules/agenda/adapters/node-cron/index.js +30 -0
- package/dist/modules/agenda/adapters/node-cron/index.js.map +1 -0
- package/dist/modules/agenda/index.d.ts +63 -0
- package/dist/modules/agenda/index.d.ts.map +1 -0
- package/dist/modules/agenda/index.js +141 -0
- package/dist/modules/agenda/index.js.map +1 -0
- 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/embedding/adapters/ai/index.d.ts +29 -0
- package/dist/modules/embedding/adapters/ai/index.d.ts.map +1 -0
- package/dist/modules/embedding/adapters/ai/index.js +58 -0
- package/dist/modules/embedding/adapters/ai/index.js.map +1 -0
- package/dist/modules/embedding/index.d.ts +36 -0
- package/dist/modules/embedding/index.d.ts.map +1 -0
- package/dist/modules/embedding/index.js +60 -0
- package/dist/modules/embedding/index.js.map +1 -0
- package/dist/modules/nlp/engine.d.ts +126 -0
- package/dist/modules/nlp/engine.d.ts.map +1 -0
- package/dist/modules/nlp/engine.js +299 -0
- package/dist/modules/nlp/engine.js.map +1 -0
- package/dist/modules/nlp/index.d.ts +27 -0
- package/dist/modules/nlp/index.d.ts.map +1 -0
- package/dist/modules/nlp/index.js +56 -0
- package/dist/modules/nlp/index.js.map +1 -0
- 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 +289 -0
- package/dist/types/agent.d.ts.map +1 -0
- package/dist/types/agent.js +78 -0
- package/dist/types/agent.js.map +1 -0
- package/dist/types/index.d.ts +343 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/generate-action-schema.d.ts +4 -0
- package/dist/utils/generate-action-schema.d.ts.map +1 -0
- package/dist/utils/generate-action-schema.js +46 -0
- package/dist/utils/generate-action-schema.js.map +1 -0
- package/dist/utils/header-builder.d.ts +12 -0
- package/dist/utils/header-builder.d.ts.map +1 -0
- package/dist/utils/header-builder.js +35 -0
- package/dist/utils/header-builder.js.map +1 -0
- 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/execution/controller.ts +84 -0
- package/execution/event-manager.ts +331 -0
- package/execution/index.ts +800 -0
- package/execution/logger.ts +70 -0
- package/execution/node.ts +315 -0
- package/execution/observer.ts +192 -0
- 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/execution/visualizer.ts +158 -0
- package/index.ts +55 -465
- package/interfaces/index.ts +597 -0
- package/modules/agenda/adapters/node-cron/index.ts +25 -0
- package/modules/agenda/index.ts +146 -0
- package/modules/cli/index.ts +580 -0
- package/modules/embedding/adapters/ai/index.ts +42 -0
- package/modules/embedding/index.ts +45 -0
- package/modules/nlp/engine.ts +324 -0
- package/modules/nlp/index.ts +45 -0
- package/package.json +81 -9
- 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/execution/controller.test.ts +236 -0
- package/test/execution/event-manager.test.ts +118 -0
- package/test/execution/index.test.ts +690 -0
- package/test/execution/node.test.ts +464 -0
- package/test/execution/observer.test.ts +393 -0
- 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/agenda/node-cron.test.ts +307 -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 +30 -108
- package/types/agent.ts +296 -0
- package/types/index.ts +387 -0
- package/utils/generate-action-schema.ts +47 -0
- package/utils/header-builder.ts +40 -0
- package/utils/logger.ts +40 -0
- package/package copy.json +0 -21
- package/types.ts +0 -62
- package/utils/executor.ts +0 -42
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { PetriNet } from "../../routing";
|
|
2
|
+
import { Session } from "../../routing/orchestrator";
|
|
3
|
+
import { IPetriCheckpointAdapter } from "../../routing/checkpoint-adapter";
|
|
4
|
+
import { Neo4jDriverLike, Neo4jSessionLike } from "./driver";
|
|
5
|
+
|
|
6
|
+
export class Neo4jPetriCheckpointAdapter implements IPetriCheckpointAdapter {
|
|
7
|
+
constructor(
|
|
8
|
+
private driver: Neo4jDriverLike,
|
|
9
|
+
private database?: string
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
private session(): Neo4jSessionLike {
|
|
13
|
+
return this.driver.session(this.database ? { database: this.database } : undefined);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async save(net: PetriNet, session?: Session): Promise<string> {
|
|
17
|
+
const checkpointId = `petri-cp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
18
|
+
|
|
19
|
+
const marking = (net as any).state.marking as Map<string, any[]>;
|
|
20
|
+
const serializedMarking: any[] = [];
|
|
21
|
+
for (const [pid, tokens] of marking) {
|
|
22
|
+
serializedMarking.push({
|
|
23
|
+
placeId: pid,
|
|
24
|
+
tokens: tokens.map((t: any) => ({ id: t.id, data: t.data, createdAt: t.createdAt })),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const s = this.session();
|
|
29
|
+
try {
|
|
30
|
+
await s.run(
|
|
31
|
+
`MERGE (s:PetriSession {id: $sessionId})
|
|
32
|
+
SET s.createdAt = $createdAt
|
|
33
|
+
WITH s
|
|
34
|
+
CREATE (c:Checkpoint {id: $checkpointId, timestamp: $timestamp, marking: $marking})
|
|
35
|
+
MERGE (s)-[:HAS_CHECKPOINT]->(c)`,
|
|
36
|
+
{
|
|
37
|
+
sessionId: session?.id || "anonymous",
|
|
38
|
+
createdAt: session?.createdAt ? new Date(session.createdAt).toISOString() : new Date().toISOString(),
|
|
39
|
+
checkpointId,
|
|
40
|
+
timestamp: Date.now(),
|
|
41
|
+
marking: JSON.stringify(serializedMarking),
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
if (session?.id) {
|
|
46
|
+
const history = (net as any).state.history || [];
|
|
47
|
+
for (const entry of history) {
|
|
48
|
+
await s.run(
|
|
49
|
+
`MATCH (s:PetriSession {id: $sessionId})
|
|
50
|
+
CREATE (f:TransitionFiring {id: $firingId, transitionId: $transitionId, seq: $seq, firedAt: $firedAt})
|
|
51
|
+
MERGE (s)-[:FIRED]->(f)`,
|
|
52
|
+
{
|
|
53
|
+
sessionId: session.id,
|
|
54
|
+
firingId: `firing-${session.id}-${entry.timestamp || Date.now()}-${Math.random().toString(36).substr(2, 6)}`,
|
|
55
|
+
transitionId: entry.transitionId || "unknown",
|
|
56
|
+
seq: entry.seq || 0,
|
|
57
|
+
firedAt: entry.timestamp ? new Date(entry.timestamp).toISOString() : new Date().toISOString(),
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return checkpointId;
|
|
64
|
+
} finally {
|
|
65
|
+
await s.close();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async load(checkpointId: string): Promise<{ net: PetriNet; session?: Session } | null> {
|
|
70
|
+
const s = this.session();
|
|
71
|
+
try {
|
|
72
|
+
const result = await s.run(
|
|
73
|
+
`MATCH (c:Checkpoint {id: $checkpointId})
|
|
74
|
+
OPTIONAL MATCH (c)<-[:HAS_CHECKPOINT]-(sess:PetriSession)
|
|
75
|
+
RETURN c, sess`,
|
|
76
|
+
{ checkpointId }
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
if (result.records.length === 0) return null;
|
|
80
|
+
|
|
81
|
+
const record = result.records[0];
|
|
82
|
+
const cp = record.get("c");
|
|
83
|
+
const sessRecord = record.get("sess");
|
|
84
|
+
|
|
85
|
+
if (!cp) return null;
|
|
86
|
+
|
|
87
|
+
const cpProps = cp.properties ? (typeof cp.properties === "function" ? cp.properties() : cp.properties) : cp;
|
|
88
|
+
const marking = JSON.parse(cpProps.marking || "[]");
|
|
89
|
+
|
|
90
|
+
const net = new PetriNet(cpProps.id || checkpointId);
|
|
91
|
+
const stateMarking = (net as any).state.marking as Map<string, any[]>;
|
|
92
|
+
|
|
93
|
+
for (const item of marking) {
|
|
94
|
+
stateMarking.set(
|
|
95
|
+
item.placeId,
|
|
96
|
+
item.tokens.map((t: any) => ({ ...t }))
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
(net as any).state.history = [];
|
|
101
|
+
|
|
102
|
+
let session: Session | undefined;
|
|
103
|
+
if (sessRecord) {
|
|
104
|
+
const sessProps = sessRecord.properties ? (typeof sessRecord.properties === "function" ? sessRecord.properties() : sessRecord.properties) : sessRecord;
|
|
105
|
+
session = {
|
|
106
|
+
id: sessProps.id || "restored",
|
|
107
|
+
petriNet: net,
|
|
108
|
+
context: {},
|
|
109
|
+
history: [],
|
|
110
|
+
createdAt: new Date(sessProps.createdAt || Date.now()),
|
|
111
|
+
traceId: `trace-restored-${Date.now()}`,
|
|
112
|
+
} as any;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return { net, session };
|
|
116
|
+
} finally {
|
|
117
|
+
await s.close();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async list(): Promise<Array<{ id: string; timestamp: number; sessionId?: string }>> {
|
|
122
|
+
const s = this.session();
|
|
123
|
+
try {
|
|
124
|
+
const result = await s.run(
|
|
125
|
+
`MATCH (c:Checkpoint)
|
|
126
|
+
OPTIONAL MATCH (c)<-[:HAS_CHECKPOINT]-(sess:PetriSession)
|
|
127
|
+
RETURN c.id AS id, c.timestamp AS timestamp, sess.id AS sessionId
|
|
128
|
+
ORDER BY c.timestamp DESC`
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
return result.records.map((r: any) => ({
|
|
132
|
+
id: r.get("id"),
|
|
133
|
+
timestamp: Number(r.get("timestamp")) || 0,
|
|
134
|
+
sessionId: r.get("sessionId") || undefined,
|
|
135
|
+
}));
|
|
136
|
+
} finally {
|
|
137
|
+
await s.close();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async delete(checkpointId: string): Promise<void> {
|
|
142
|
+
const s = this.session();
|
|
143
|
+
try {
|
|
144
|
+
await s.run(
|
|
145
|
+
`MATCH (c:Checkpoint {id: $checkpointId})
|
|
146
|
+
DETACH DELETE c`,
|
|
147
|
+
{ checkpointId }
|
|
148
|
+
);
|
|
149
|
+
} finally {
|
|
150
|
+
await s.close();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|