@falai/agent 1.2.8 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -886
- package/dist/adapters/MemoryAdapter.js +2 -2
- package/dist/adapters/MemoryAdapter.js.map +1 -1
- package/dist/adapters/MongoAdapter.js +2 -2
- package/dist/adapters/MongoAdapter.js.map +1 -1
- package/dist/adapters/OpenSearchAdapter.d.ts.map +1 -1
- package/dist/adapters/OpenSearchAdapter.js +9 -7
- package/dist/adapters/OpenSearchAdapter.js.map +1 -1
- package/dist/adapters/PostgreSQLAdapter.d.ts +14 -0
- package/dist/adapters/PostgreSQLAdapter.d.ts.map +1 -1
- package/dist/adapters/PostgreSQLAdapter.js +25 -9
- package/dist/adapters/PostgreSQLAdapter.js.map +1 -1
- package/dist/adapters/PrismaAdapter.js +5 -5
- package/dist/adapters/PrismaAdapter.js.map +1 -1
- package/dist/adapters/RedisAdapter.js +2 -2
- package/dist/adapters/RedisAdapter.js.map +1 -1
- package/dist/adapters/SQLiteAdapter.d.ts +17 -0
- package/dist/adapters/SQLiteAdapter.d.ts.map +1 -1
- package/dist/adapters/SQLiteAdapter.js +30 -11
- package/dist/adapters/SQLiteAdapter.js.map +1 -1
- package/dist/cjs/adapters/MemoryAdapter.js +2 -2
- package/dist/cjs/adapters/MemoryAdapter.js.map +1 -1
- package/dist/cjs/adapters/MongoAdapter.js +2 -2
- package/dist/cjs/adapters/MongoAdapter.js.map +1 -1
- package/dist/cjs/adapters/OpenSearchAdapter.d.ts.map +1 -1
- package/dist/cjs/adapters/OpenSearchAdapter.js +9 -7
- package/dist/cjs/adapters/OpenSearchAdapter.js.map +1 -1
- package/dist/cjs/adapters/PostgreSQLAdapter.d.ts +14 -0
- package/dist/cjs/adapters/PostgreSQLAdapter.d.ts.map +1 -1
- package/dist/cjs/adapters/PostgreSQLAdapter.js +25 -9
- package/dist/cjs/adapters/PostgreSQLAdapter.js.map +1 -1
- package/dist/cjs/adapters/PrismaAdapter.js +5 -5
- package/dist/cjs/adapters/PrismaAdapter.js.map +1 -1
- package/dist/cjs/adapters/RedisAdapter.js +2 -2
- package/dist/cjs/adapters/RedisAdapter.js.map +1 -1
- package/dist/cjs/adapters/SQLiteAdapter.d.ts +17 -0
- package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +1 -1
- package/dist/cjs/adapters/SQLiteAdapter.js +30 -11
- package/dist/cjs/adapters/SQLiteAdapter.js.map +1 -1
- package/dist/cjs/constants/index.d.ts +0 -9
- package/dist/cjs/constants/index.d.ts.map +1 -1
- package/dist/cjs/constants/index.js +2 -11
- package/dist/cjs/constants/index.js.map +1 -1
- package/dist/cjs/core/Agent.d.ts +119 -153
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +471 -324
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/AutoChainExecutor.d.ts +107 -0
- package/dist/cjs/core/AutoChainExecutor.d.ts.map +1 -0
- package/dist/cjs/core/AutoChainExecutor.js +297 -0
- package/dist/cjs/core/AutoChainExecutor.js.map +1 -0
- package/dist/cjs/core/BranchEvaluator.d.ts +54 -0
- package/dist/cjs/core/BranchEvaluator.d.ts.map +1 -0
- package/dist/cjs/core/BranchEvaluator.js +130 -0
- package/dist/cjs/core/BranchEvaluator.js.map +1 -0
- package/dist/cjs/core/DirectiveBus.d.ts +88 -0
- package/dist/cjs/core/DirectiveBus.d.ts.map +1 -0
- package/dist/cjs/core/DirectiveBus.js +196 -0
- package/dist/cjs/core/DirectiveBus.js.map +1 -0
- package/dist/cjs/core/DirectiveChainTracker.d.ts +49 -0
- package/dist/cjs/core/DirectiveChainTracker.d.ts.map +1 -0
- package/dist/cjs/core/DirectiveChainTracker.js +121 -0
- package/dist/cjs/core/DirectiveChainTracker.js.map +1 -0
- package/dist/cjs/core/Flow.d.ts +186 -0
- package/dist/cjs/core/Flow.d.ts.map +1 -0
- package/dist/cjs/core/Flow.js +550 -0
- package/dist/cjs/core/Flow.js.map +1 -0
- package/dist/cjs/core/FlowRouter.d.ts +182 -0
- package/dist/cjs/core/FlowRouter.d.ts.map +1 -0
- package/dist/cjs/core/{RoutingEngine.js → FlowRouter.js} +323 -306
- package/dist/cjs/core/FlowRouter.js.map +1 -0
- package/dist/cjs/core/PersistenceManager.d.ts +2 -2
- package/dist/cjs/core/PersistenceManager.d.ts.map +1 -1
- package/dist/cjs/core/PersistenceManager.js +7 -7
- package/dist/cjs/core/PersistenceManager.js.map +1 -1
- package/dist/cjs/core/PromptComposer.d.ts +21 -8
- package/dist/cjs/core/PromptComposer.d.ts.map +1 -1
- package/dist/cjs/core/PromptComposer.js +182 -105
- package/dist/cjs/core/PromptComposer.js.map +1 -1
- package/dist/cjs/core/PromptSectionCache.d.ts +1 -1
- package/dist/cjs/core/PromptSectionCache.js +1 -1
- package/dist/cjs/core/ResponseEngine.d.ts +18 -8
- package/dist/cjs/core/ResponseEngine.d.ts.map +1 -1
- package/dist/cjs/core/ResponseEngine.js +38 -36
- package/dist/cjs/core/ResponseEngine.js.map +1 -1
- package/dist/cjs/core/ResponseModal.d.ts +73 -56
- package/dist/cjs/core/ResponseModal.d.ts.map +1 -1
- package/dist/cjs/core/ResponseModal.js +1191 -1014
- package/dist/cjs/core/ResponseModal.js.map +1 -1
- package/dist/cjs/core/ResponsePipeline.d.ts +124 -26
- package/dist/cjs/core/ResponsePipeline.d.ts.map +1 -1
- package/dist/cjs/core/ResponsePipeline.js +509 -136
- package/dist/cjs/core/ResponsePipeline.js.map +1 -1
- package/dist/cjs/core/SignalEvaluator.d.ts +86 -0
- package/dist/cjs/core/SignalEvaluator.d.ts.map +1 -0
- package/dist/cjs/core/SignalEvaluator.js +333 -0
- package/dist/cjs/core/SignalEvaluator.js.map +1 -0
- package/dist/cjs/core/SignalProcessor.d.ts +152 -0
- package/dist/cjs/core/SignalProcessor.d.ts.map +1 -0
- package/dist/cjs/core/SignalProcessor.js +562 -0
- package/dist/cjs/core/SignalProcessor.js.map +1 -0
- package/dist/cjs/core/Step.d.ts +43 -32
- package/dist/cjs/core/Step.d.ts.map +1 -1
- package/dist/cjs/core/Step.js +221 -126
- package/dist/cjs/core/Step.js.map +1 -1
- package/dist/cjs/core/StreamingToolExecutor.d.ts +2 -2
- package/dist/cjs/core/StreamingToolExecutor.d.ts.map +1 -1
- package/dist/cjs/core/StreamingToolExecutor.js.map +1 -1
- package/dist/cjs/core/ToolManager.d.ts +44 -13
- package/dist/cjs/core/ToolManager.d.ts.map +1 -1
- package/dist/cjs/core/ToolManager.js +174 -91
- package/dist/cjs/core/ToolManager.js.map +1 -1
- package/dist/cjs/core/createAgent.d.ts +35 -0
- package/dist/cjs/core/createAgent.d.ts.map +1 -0
- package/dist/cjs/core/createAgent.js +39 -0
- package/dist/cjs/core/createAgent.js.map +1 -0
- package/dist/cjs/core/flow-namespace.d.ts +49 -0
- package/dist/cjs/core/flow-namespace.d.ts.map +1 -0
- package/dist/cjs/core/flow-namespace.js +171 -0
- package/dist/cjs/core/flow-namespace.js.map +1 -0
- package/dist/cjs/index.d.ts +11 -14
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +18 -22
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +183 -54
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/agent.js +0 -6
- package/dist/cjs/types/agent.js.map +1 -1
- package/dist/cjs/types/ai.d.ts +3 -3
- package/dist/cjs/types/ai.d.ts.map +1 -1
- package/dist/cjs/types/errors.d.ts +15 -0
- package/dist/cjs/types/errors.d.ts.map +1 -0
- package/dist/cjs/types/errors.js +22 -0
- package/dist/cjs/types/errors.js.map +1 -0
- package/dist/cjs/types/flow.d.ts +513 -0
- package/dist/cjs/types/flow.d.ts.map +1 -0
- package/dist/cjs/types/{route.js → flow.js} +2 -2
- package/dist/cjs/types/flow.js.map +1 -0
- package/dist/cjs/types/index.d.ts +7 -6
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js +6 -2
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/persistence.d.ts +11 -7
- package/dist/cjs/types/persistence.d.ts.map +1 -1
- package/dist/cjs/types/routing.d.ts +1 -1
- package/dist/cjs/types/routing.d.ts.map +1 -1
- package/dist/cjs/types/session.d.ts +24 -23
- package/dist/cjs/types/session.d.ts.map +1 -1
- package/dist/cjs/types/signals.d.ts +248 -0
- package/dist/cjs/types/signals.d.ts.map +1 -0
- package/dist/cjs/types/signals.js +11 -0
- package/dist/cjs/types/signals.js.map +1 -0
- package/dist/cjs/types/template.d.ts +2 -8
- package/dist/cjs/types/template.d.ts.map +1 -1
- package/dist/cjs/types/tool.d.ts +36 -29
- package/dist/cjs/types/tool.d.ts.map +1 -1
- package/dist/cjs/types/tool.js +1 -1
- package/dist/cjs/types/tool.js.map +1 -1
- package/dist/cjs/utils/condition.d.ts +7 -1
- package/dist/cjs/utils/condition.d.ts.map +1 -1
- package/dist/cjs/utils/condition.js.map +1 -1
- package/dist/cjs/utils/id.d.ts +13 -5
- package/dist/cjs/utils/id.d.ts.map +1 -1
- package/dist/cjs/utils/id.js +24 -10
- package/dist/cjs/utils/id.js.map +1 -1
- package/dist/cjs/utils/index.d.ts +2 -2
- package/dist/cjs/utils/index.d.ts.map +1 -1
- package/dist/cjs/utils/index.js +7 -3
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/session.d.ts +44 -5
- package/dist/cjs/utils/session.d.ts.map +1 -1
- package/dist/cjs/utils/session.js +197 -38
- package/dist/cjs/utils/session.js.map +1 -1
- package/dist/constants/index.d.ts +0 -9
- package/dist/constants/index.d.ts.map +1 -1
- package/dist/constants/index.js +3 -9
- package/dist/constants/index.js.map +1 -1
- package/dist/core/Agent.d.ts +119 -153
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +472 -325
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/AutoChainExecutor.d.ts +107 -0
- package/dist/core/AutoChainExecutor.d.ts.map +1 -0
- package/dist/core/AutoChainExecutor.js +293 -0
- package/dist/core/AutoChainExecutor.js.map +1 -0
- package/dist/core/BranchEvaluator.d.ts +54 -0
- package/dist/core/BranchEvaluator.d.ts.map +1 -0
- package/dist/core/BranchEvaluator.js +126 -0
- package/dist/core/BranchEvaluator.js.map +1 -0
- package/dist/core/DirectiveBus.d.ts +88 -0
- package/dist/core/DirectiveBus.d.ts.map +1 -0
- package/dist/core/DirectiveBus.js +192 -0
- package/dist/core/DirectiveBus.js.map +1 -0
- package/dist/core/DirectiveChainTracker.d.ts +49 -0
- package/dist/core/DirectiveChainTracker.d.ts.map +1 -0
- package/dist/core/DirectiveChainTracker.js +117 -0
- package/dist/core/DirectiveChainTracker.js.map +1 -0
- package/dist/core/Flow.d.ts +186 -0
- package/dist/core/Flow.d.ts.map +1 -0
- package/dist/core/Flow.js +546 -0
- package/dist/core/Flow.js.map +1 -0
- package/dist/core/FlowRouter.d.ts +182 -0
- package/dist/core/FlowRouter.d.ts.map +1 -0
- package/dist/core/{RoutingEngine.js → FlowRouter.js} +322 -305
- package/dist/core/FlowRouter.js.map +1 -0
- package/dist/core/PersistenceManager.d.ts +2 -2
- package/dist/core/PersistenceManager.d.ts.map +1 -1
- package/dist/core/PersistenceManager.js +7 -7
- package/dist/core/PersistenceManager.js.map +1 -1
- package/dist/core/PromptComposer.d.ts +21 -8
- package/dist/core/PromptComposer.d.ts.map +1 -1
- package/dist/core/PromptComposer.js +183 -106
- package/dist/core/PromptComposer.js.map +1 -1
- package/dist/core/PromptSectionCache.d.ts +1 -1
- package/dist/core/PromptSectionCache.js +1 -1
- package/dist/core/ResponseEngine.d.ts +18 -8
- package/dist/core/ResponseEngine.d.ts.map +1 -1
- package/dist/core/ResponseEngine.js +38 -36
- package/dist/core/ResponseEngine.js.map +1 -1
- package/dist/core/ResponseModal.d.ts +73 -56
- package/dist/core/ResponseModal.d.ts.map +1 -1
- package/dist/core/ResponseModal.js +1193 -1016
- package/dist/core/ResponseModal.js.map +1 -1
- package/dist/core/ResponsePipeline.d.ts +124 -26
- package/dist/core/ResponsePipeline.d.ts.map +1 -1
- package/dist/core/ResponsePipeline.js +509 -137
- package/dist/core/ResponsePipeline.js.map +1 -1
- package/dist/core/SignalEvaluator.d.ts +86 -0
- package/dist/core/SignalEvaluator.d.ts.map +1 -0
- package/dist/core/SignalEvaluator.js +326 -0
- package/dist/core/SignalEvaluator.js.map +1 -0
- package/dist/core/SignalProcessor.d.ts +152 -0
- package/dist/core/SignalProcessor.d.ts.map +1 -0
- package/dist/core/SignalProcessor.js +555 -0
- package/dist/core/SignalProcessor.js.map +1 -0
- package/dist/core/Step.d.ts +43 -32
- package/dist/core/Step.d.ts.map +1 -1
- package/dist/core/Step.js +220 -126
- package/dist/core/Step.js.map +1 -1
- package/dist/core/StreamingToolExecutor.d.ts +2 -2
- package/dist/core/StreamingToolExecutor.d.ts.map +1 -1
- package/dist/core/StreamingToolExecutor.js.map +1 -1
- package/dist/core/ToolManager.d.ts +44 -13
- package/dist/core/ToolManager.d.ts.map +1 -1
- package/dist/core/ToolManager.js +174 -91
- package/dist/core/ToolManager.js.map +1 -1
- package/dist/core/createAgent.d.ts +35 -0
- package/dist/core/createAgent.d.ts.map +1 -0
- package/dist/core/createAgent.js +36 -0
- package/dist/core/createAgent.js.map +1 -0
- package/dist/core/flow-namespace.d.ts +49 -0
- package/dist/core/flow-namespace.d.ts.map +1 -0
- package/dist/core/flow-namespace.js +168 -0
- package/dist/core/flow-namespace.js.map +1 -0
- package/dist/index.d.ts +11 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -12
- package/dist/index.js.map +1 -1
- package/dist/types/agent.d.ts +183 -54
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/agent.js +0 -6
- package/dist/types/agent.js.map +1 -1
- package/dist/types/ai.d.ts +3 -3
- package/dist/types/ai.d.ts.map +1 -1
- package/dist/types/errors.d.ts +15 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +18 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/flow.d.ts +513 -0
- package/dist/types/flow.d.ts.map +1 -0
- package/dist/types/flow.js +5 -0
- package/dist/types/flow.js.map +1 -0
- package/dist/types/index.d.ts +7 -6
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +4 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/persistence.d.ts +11 -7
- package/dist/types/persistence.d.ts.map +1 -1
- package/dist/types/routing.d.ts +1 -1
- package/dist/types/routing.d.ts.map +1 -1
- package/dist/types/session.d.ts +24 -23
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/signals.d.ts +248 -0
- package/dist/types/signals.d.ts.map +1 -0
- package/dist/types/signals.js +10 -0
- package/dist/types/signals.js.map +1 -0
- package/dist/types/template.d.ts +2 -8
- package/dist/types/template.d.ts.map +1 -1
- package/dist/types/tool.d.ts +36 -29
- package/dist/types/tool.d.ts.map +1 -1
- package/dist/types/tool.js +1 -1
- package/dist/types/tool.js.map +1 -1
- package/dist/utils/condition.d.ts +7 -1
- package/dist/utils/condition.d.ts.map +1 -1
- package/dist/utils/condition.js.map +1 -1
- package/dist/utils/id.d.ts +13 -5
- package/dist/utils/id.d.ts.map +1 -1
- package/dist/utils/id.js +22 -9
- package/dist/utils/id.js.map +1 -1
- package/dist/utils/index.d.ts +2 -2
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -2
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/session.d.ts +44 -5
- package/dist/utils/session.d.ts.map +1 -1
- package/dist/utils/session.js +193 -37
- package/dist/utils/session.js.map +1 -1
- package/docs/README.md +15 -202
- package/docs/concepts/architecture.md +281 -0
- package/docs/concepts/directives.md +400 -0
- package/docs/concepts/pipeline.md +399 -0
- package/docs/guides/branching.md +263 -0
- package/docs/guides/compaction.md +163 -0
- package/docs/guides/conditions.md +167 -0
- package/docs/guides/error-handling.md +176 -0
- package/docs/guides/flow-control.md +409 -0
- package/docs/guides/instructions.md +210 -0
- package/docs/guides/persistence.md +182 -0
- package/docs/guides/streaming.md +137 -0
- package/docs/migration/README.md +15 -0
- package/docs/migration/route-to-flow.md +560 -0
- package/docs/migration/v1-to-v2.md +909 -0
- package/docs/reference/adapters.md +481 -0
- package/docs/reference/branches.md +241 -0
- package/docs/reference/create-agent.md +186 -0
- package/docs/reference/directive.md +243 -0
- package/docs/reference/errors.md +122 -0
- package/docs/reference/flow.md +238 -0
- package/docs/reference/instruction.md +177 -0
- package/docs/reference/pre-directive.md +131 -0
- package/docs/reference/providers.md +227 -0
- package/docs/reference/signals.md +356 -0
- package/docs/reference/step.md +339 -0
- package/docs/reference/tool.md +269 -0
- package/docs/start/01-install.md +81 -0
- package/docs/start/02-first-agent.md +196 -0
- package/docs/start/03-collect-data.md +222 -0
- package/docs/start/04-add-tools.md +276 -0
- package/docs/start/05-go-to-production.md +216 -0
- package/examples/01-quickstart.ts +20 -0
- package/examples/02-data-extraction.ts +90 -0
- package/examples/03-tools.ts +136 -0
- package/examples/04-instructions.ts +100 -0
- package/examples/05-branching.ts +140 -0
- package/examples/06-flow-control.ts +103 -0
- package/examples/07-streaming.ts +69 -0
- package/examples/08-persistence.ts +98 -0
- package/examples/09-signals.ts +144 -0
- package/examples/tsconfig.json +30 -0
- package/package.json +2 -1
- package/src/adapters/MemoryAdapter.ts +3 -3
- package/src/adapters/MongoAdapter.ts +3 -3
- package/src/adapters/OpenSearchAdapter.ts +10 -8
- package/src/adapters/PostgreSQLAdapter.ts +26 -10
- package/src/adapters/PrismaAdapter.ts +6 -6
- package/src/adapters/RedisAdapter.ts +3 -3
- package/src/adapters/SQLiteAdapter.ts +31 -12
- package/src/constants/index.ts +2 -10
- package/src/core/Agent.ts +585 -374
- package/src/core/AutoChainExecutor.ts +440 -0
- package/src/core/BranchEvaluator.ts +167 -0
- package/src/core/DirectiveBus.ts +248 -0
- package/src/core/DirectiveChainTracker.ts +144 -0
- package/src/core/Flow.ts +666 -0
- package/src/core/{RoutingEngine.ts → FlowRouter.ts} +385 -365
- package/src/core/PersistenceManager.ts +8 -8
- package/src/core/PromptComposer.ts +209 -140
- package/src/core/PromptSectionCache.ts +1 -1
- package/src/core/ResponseEngine.ts +61 -46
- package/src/core/ResponseModal.ts +1453 -1240
- package/src/core/ResponsePipeline.ts +655 -175
- package/src/core/SignalEvaluator.ts +420 -0
- package/src/core/SignalProcessor.ts +723 -0
- package/src/core/Step.ts +279 -176
- package/src/core/StreamingToolExecutor.ts +4 -4
- package/src/core/ToolManager.ts +200 -97
- package/src/core/createAgent.ts +40 -0
- package/src/core/flow-namespace.ts +219 -0
- package/src/index.ts +42 -36
- package/src/types/agent.ts +182 -53
- package/src/types/ai.ts +3 -3
- package/src/types/errors.ts +18 -0
- package/src/types/flow.ts +590 -0
- package/src/types/index.ts +43 -16
- package/src/types/persistence.ts +12 -8
- package/src/types/routing.ts +1 -1
- package/src/types/session.ts +26 -23
- package/src/types/signals.ts +321 -0
- package/src/types/template.ts +3 -11
- package/src/types/tool.ts +50 -42
- package/src/utils/condition.ts +13 -4
- package/src/utils/id.ts +27 -9
- package/src/utils/index.ts +6 -2
- package/src/utils/session.ts +238 -42
- package/dist/cjs/core/BatchExecutor.d.ts +0 -359
- package/dist/cjs/core/BatchExecutor.d.ts.map +0 -1
- package/dist/cjs/core/BatchExecutor.js +0 -861
- package/dist/cjs/core/BatchExecutor.js.map +0 -1
- package/dist/cjs/core/BatchPromptBuilder.d.ts +0 -89
- package/dist/cjs/core/BatchPromptBuilder.d.ts.map +0 -1
- package/dist/cjs/core/BatchPromptBuilder.js +0 -223
- package/dist/cjs/core/BatchPromptBuilder.js.map +0 -1
- package/dist/cjs/core/Route.d.ts +0 -180
- package/dist/cjs/core/Route.d.ts.map +0 -1
- package/dist/cjs/core/Route.js +0 -542
- package/dist/cjs/core/Route.js.map +0 -1
- package/dist/cjs/core/RoutingEngine.d.ts +0 -185
- package/dist/cjs/core/RoutingEngine.d.ts.map +0 -1
- package/dist/cjs/core/RoutingEngine.js.map +0 -1
- package/dist/cjs/types/route.d.ts +0 -336
- package/dist/cjs/types/route.d.ts.map +0 -1
- package/dist/cjs/types/route.js.map +0 -1
- package/dist/core/BatchExecutor.d.ts +0 -359
- package/dist/core/BatchExecutor.d.ts.map +0 -1
- package/dist/core/BatchExecutor.js +0 -856
- package/dist/core/BatchExecutor.js.map +0 -1
- package/dist/core/BatchPromptBuilder.d.ts +0 -89
- package/dist/core/BatchPromptBuilder.d.ts.map +0 -1
- package/dist/core/BatchPromptBuilder.js +0 -219
- package/dist/core/BatchPromptBuilder.js.map +0 -1
- package/dist/core/Route.d.ts +0 -180
- package/dist/core/Route.d.ts.map +0 -1
- package/dist/core/Route.js +0 -538
- package/dist/core/Route.js.map +0 -1
- package/dist/core/RoutingEngine.d.ts +0 -185
- package/dist/core/RoutingEngine.d.ts.map +0 -1
- package/dist/core/RoutingEngine.js.map +0 -1
- package/dist/types/route.d.ts +0 -336
- package/dist/types/route.d.ts.map +0 -1
- package/dist/types/route.js +0 -5
- package/dist/types/route.js.map +0 -1
- package/docs/CONTRIBUTING.md +0 -521
- package/docs/api/README.md +0 -3299
- package/docs/api/overview.md +0 -1410
- package/docs/architecture/data-extraction-flow.md +0 -360
- package/docs/architecture/multi-step-execution.md +0 -277
- package/docs/core/agent/README.md +0 -938
- package/docs/core/agent/context-management.md +0 -796
- package/docs/core/agent/rules-and-prohibitions.md +0 -113
- package/docs/core/agent/session-management.md +0 -693
- package/docs/core/ai-integration/prompt-composition.md +0 -355
- package/docs/core/ai-integration/providers.md +0 -515
- package/docs/core/ai-integration/response-processing.md +0 -433
- package/docs/core/conversation-flows/data-collection.md +0 -772
- package/docs/core/conversation-flows/route-dsl.md +0 -509
- package/docs/core/conversation-flows/routes.md +0 -249
- package/docs/core/conversation-flows/step-transitions.md +0 -731
- package/docs/core/conversation-flows/steps.md +0 -268
- package/docs/core/error-handling.md +0 -830
- package/docs/core/persistence/adapters.md +0 -255
- package/docs/core/persistence/session-storage.md +0 -656
- package/docs/core/routing/intelligent-routing.md +0 -470
- package/docs/core/tools/enhanced-tool.md +0 -186
- package/docs/core/tools/streaming-execution.md +0 -161
- package/docs/core/tools/tool-definition.md +0 -970
- package/docs/core/tools/tool-scoping.md +0 -819
- package/docs/guides/advanced-patterns/publishing.md +0 -186
- package/docs/guides/context-compaction.md +0 -96
- package/docs/guides/error-handling-patterns.md +0 -578
- package/docs/guides/getting-started/README.md +0 -795
- package/docs/guides/migration/README.md +0 -101
- package/docs/guides/migration/flexible-routing-conditions.md +0 -375
- package/docs/guides/migration/multi-step-execution.md +0 -393
- package/docs/guides/migration/response-modal-refactor.md +0 -518
- package/docs/guides/prompt-optimization.md +0 -164
- package/examples/advanced-patterns/context-compaction.ts +0 -223
- package/examples/advanced-patterns/knowledge-based-agent.ts +0 -735
- package/examples/advanced-patterns/persistent-onboarding.ts +0 -728
- package/examples/advanced-patterns/route-lifecycle-hooks.ts +0 -556
- package/examples/advanced-patterns/streaming-responses.ts +0 -656
- package/examples/ai-providers/anthropic-integration.ts +0 -388
- package/examples/ai-providers/openai-integration.ts +0 -228
- package/examples/condition-patterns/function-only-conditions.ts +0 -365
- package/examples/condition-patterns/mixed-array-conditions.ts +0 -477
- package/examples/condition-patterns/route-skipif-patterns.ts +0 -468
- package/examples/condition-patterns/step-skipif-patterns.ts +0 -0
- package/examples/condition-patterns/string-only-conditions.ts +0 -296
- package/examples/conversation-flows/completion-transitions.ts +0 -318
- package/examples/core-concepts/basic-agent.ts +0 -503
- package/examples/core-concepts/modern-streaming-api.ts +0 -309
- package/examples/core-concepts/schema-driven-extraction.ts +0 -332
- package/examples/core-concepts/session-management.ts +0 -494
- package/examples/integrations/database-integration.ts +0 -631
- package/examples/integrations/healthcare-integration.ts +0 -595
- package/examples/integrations/search-integration.ts +0 -530
- package/examples/integrations/server-session-management.ts +0 -307
- package/examples/persistence/custom-adapter.ts +0 -526
- package/examples/persistence/database-persistence.ts +0 -583
- package/examples/persistence/memory-sessions.ts +0 -495
- package/examples/persistence/prisma-schema.example.prisma +0 -74
- package/examples/persistence/redis-persistence.ts +0 -488
- package/examples/tools/basic-tools.ts +0 -765
- package/examples/tools/data-enrichment-tools.ts +0 -593
- package/examples/tools/enhanced-tool-metadata.ts +0 -268
- package/examples/tools/streaming-tool-execution.ts +0 -283
- package/src/core/BatchExecutor.ts +0 -1187
- package/src/core/BatchPromptBuilder.ts +0 -299
- package/src/core/Route.ts +0 -678
- package/src/types/route.ts +0 -392
package/dist/cjs/core/Agent.js
CHANGED
|
@@ -4,12 +4,15 @@
|
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Agent = void 0;
|
|
7
|
-
const
|
|
7
|
+
const errors_1 = require("../types/errors");
|
|
8
|
+
const SignalProcessor_1 = require("./SignalProcessor");
|
|
9
|
+
const SignalEvaluator_1 = require("./SignalEvaluator");
|
|
8
10
|
const utils_1 = require("../utils");
|
|
9
|
-
const
|
|
11
|
+
const Flow_1 = require("./Flow");
|
|
12
|
+
const Step_1 = require("./Step");
|
|
10
13
|
const PersistenceManager_1 = require("./PersistenceManager");
|
|
11
14
|
const SessionManager_1 = require("./SessionManager");
|
|
12
|
-
const
|
|
15
|
+
const FlowRouter_1 = require("./FlowRouter");
|
|
13
16
|
const PromptSectionCache_1 = require("./PromptSectionCache");
|
|
14
17
|
const ResponseModal_1 = require("./ResponseModal");
|
|
15
18
|
const ToolManager_1 = require("./ToolManager");
|
|
@@ -25,14 +28,14 @@ class DataValidationError extends Error {
|
|
|
25
28
|
}
|
|
26
29
|
}
|
|
27
30
|
/**
|
|
28
|
-
* Error thrown when
|
|
31
|
+
* Error thrown when flow configuration is invalid
|
|
29
32
|
*/
|
|
30
|
-
class
|
|
31
|
-
constructor(
|
|
32
|
-
super(message || `
|
|
33
|
-
this.
|
|
33
|
+
class FlowConfigurationError extends Error {
|
|
34
|
+
constructor(flowTitle, invalidFields, message) {
|
|
35
|
+
super(message || `Flow configuration error in '${flowTitle}'`);
|
|
36
|
+
this.flowTitle = flowTitle;
|
|
34
37
|
this.invalidFields = invalidFields;
|
|
35
|
-
this.name = "
|
|
38
|
+
this.name = "FlowConfigurationError";
|
|
36
39
|
}
|
|
37
40
|
}
|
|
38
41
|
/**
|
|
@@ -43,13 +46,75 @@ class Agent {
|
|
|
43
46
|
constructor(options) {
|
|
44
47
|
this.options = options;
|
|
45
48
|
this._terms = [];
|
|
46
|
-
this.
|
|
49
|
+
this._instructions = [];
|
|
47
50
|
this._tools = [];
|
|
48
|
-
this.
|
|
49
|
-
this._rules = [];
|
|
50
|
-
this._prohibitions = [];
|
|
51
|
+
this._flows = [];
|
|
51
52
|
this._knowledgeBase = {};
|
|
52
53
|
this._collectedData = {};
|
|
54
|
+
this.maxAutoStepsPerTurn = options.maxAutoStepsPerTurn ?? 10;
|
|
55
|
+
this.maxDirectiveChain = options.maxDirectiveChain ?? 10;
|
|
56
|
+
// Validate routerMode reservation — only 'ai' is supported in v2.0
|
|
57
|
+
if (options.routerMode !== undefined && options.routerMode !== 'ai') {
|
|
58
|
+
throw new errors_1.NotImplementedError(`[NotImplementedError] routerMode "${String(options.routerMode)}" is not implemented: only "ai" is supported in v2.0. ` +
|
|
59
|
+
`Set routerMode to "ai" or omit the option.`);
|
|
60
|
+
}
|
|
61
|
+
// ─── Signal construction-time validation (Requirements 1.4, 1.5, 1.6, 1.9, 2.3) ───
|
|
62
|
+
const rawSignals = options.signals ?? [];
|
|
63
|
+
// Auto-generate stable ids for entries without `id`
|
|
64
|
+
for (let i = 0; i < rawSignals.length; i++) {
|
|
65
|
+
if (!rawSignals[i].id) {
|
|
66
|
+
rawSignals[i] = {
|
|
67
|
+
...rawSignals[i],
|
|
68
|
+
id: (0, utils_1.generateSignalId)(rawSignals[i].title, rawSignals[i].description, i),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Validate unique ids (Requirement 1.4)
|
|
73
|
+
const idCounts = new Map();
|
|
74
|
+
for (const signal of rawSignals) {
|
|
75
|
+
const id = signal.id;
|
|
76
|
+
idCounts.set(id, (idCounts.get(id) ?? 0) + 1);
|
|
77
|
+
}
|
|
78
|
+
const duplicateIds = [...idCounts.entries()]
|
|
79
|
+
.filter(([, count]) => count > 1)
|
|
80
|
+
.map(([id]) => id);
|
|
81
|
+
if (duplicateIds.length > 0) {
|
|
82
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] Duplicate signal ids: ${duplicateIds.join(', ')}. ` +
|
|
83
|
+
`Each signal must have a unique id.`);
|
|
84
|
+
}
|
|
85
|
+
// Validate signalBatchSize (positive integer when set)
|
|
86
|
+
if (options.signalBatchSize !== undefined) {
|
|
87
|
+
if (!Number.isInteger(options.signalBatchSize) ||
|
|
88
|
+
options.signalBatchSize <= 0) {
|
|
89
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] signalBatchSize must be a positive integer, got: ${options.signalBatchSize}.`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Validate each signal's configuration
|
|
93
|
+
for (const signal of rawSignals) {
|
|
94
|
+
// Requirement 1.5: cooldown without cooldownMs → debug warning, treat as 'always'
|
|
95
|
+
if (signal.behavior === 'cooldown' && signal.cooldownMs == null) {
|
|
96
|
+
utils_1.logger.debug(`[Agent] Signal "${signal.id}" has behavior 'cooldown' but no cooldownMs. Treating as 'always'.`);
|
|
97
|
+
signal.behavior = 'always';
|
|
98
|
+
}
|
|
99
|
+
// Requirement 1.9: validate extract schema is a JSON Schema object
|
|
100
|
+
if (signal.extract !== undefined) {
|
|
101
|
+
if (signal.extract === null ||
|
|
102
|
+
typeof signal.extract !== 'object' ||
|
|
103
|
+
Array.isArray(signal.extract)) {
|
|
104
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] Signal "${signal.id}" has an invalid extract schema. ` +
|
|
105
|
+
`Expected a JSON Schema object, got: ${typeof signal.extract}.`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
this._signals = rawSignals;
|
|
110
|
+
// Requirement 2.3: Only instantiate SignalProcessor when signals are present
|
|
111
|
+
if (rawSignals.length > 0) {
|
|
112
|
+
const evaluator = new SignalEvaluator_1.SignalEvaluator(options.provider);
|
|
113
|
+
this.signalProcessor = new SignalProcessor_1.SignalProcessor(rawSignals, options.provider, evaluator, { batchSize: options.signalBatchSize ?? 10 });
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
this.signalProcessor = undefined;
|
|
117
|
+
}
|
|
53
118
|
// Set log level based on debug option
|
|
54
119
|
if (options.debug) {
|
|
55
120
|
utils_1.logger.setLevel(utils_1.LoggerLevel.DEBUG);
|
|
@@ -81,10 +146,10 @@ class Agent {
|
|
|
81
146
|
this._currentSession = options.session;
|
|
82
147
|
// Initialize prompt section cache
|
|
83
148
|
this._promptSectionCache = new PromptSectionCache_1.PromptSectionCache(options.promptCache);
|
|
84
|
-
// Initialize
|
|
85
|
-
this._routingEngine = new
|
|
86
|
-
|
|
87
|
-
|
|
149
|
+
// Initialize flow router
|
|
150
|
+
this._routingEngine = new FlowRouter_1.FlowRouter({
|
|
151
|
+
flowSwitchMargin: options.flowSwitchMargin,
|
|
152
|
+
onFlowSwitch: () => this.invalidateFlowSections(),
|
|
88
153
|
promptSectionCache: this._promptSectionCache,
|
|
89
154
|
});
|
|
90
155
|
// Initialize ResponseModal for handling all response generation
|
|
@@ -122,28 +187,25 @@ class Agent {
|
|
|
122
187
|
this.createTerm(term);
|
|
123
188
|
});
|
|
124
189
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
190
|
+
// Initialize instructions (new unified form)
|
|
191
|
+
if (options.instructions) {
|
|
192
|
+
options.instructions.forEach((instruction) => {
|
|
193
|
+
this.createInstruction(instruction);
|
|
128
194
|
});
|
|
129
195
|
}
|
|
130
196
|
if (options.tools) {
|
|
131
197
|
options.tools.forEach((tool) => {
|
|
132
|
-
this.
|
|
198
|
+
this.addTool(tool);
|
|
133
199
|
});
|
|
134
200
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
139
|
-
if (options.prohibitions) {
|
|
140
|
-
this._prohibitions = [...options.prohibitions];
|
|
141
|
-
}
|
|
142
|
-
if (options.routes) {
|
|
143
|
-
options.routes.forEach((routeOptions) => {
|
|
144
|
-
this.createRoute(routeOptions);
|
|
201
|
+
if (options.flows) {
|
|
202
|
+
options.flows.forEach((flowOptions) => {
|
|
203
|
+
this.createFlow(flowOptions);
|
|
145
204
|
});
|
|
146
205
|
}
|
|
206
|
+
// Validate deferred branch `then` string references against the flow registry.
|
|
207
|
+
// This catches strings that don't match a local step id AND don't match any flow id/title.
|
|
208
|
+
this.validateBranchReferences();
|
|
147
209
|
// Initialize knowledge base
|
|
148
210
|
if (options.knowledgeBase) {
|
|
149
211
|
this._knowledgeBase = { ...options.knowledgeBase };
|
|
@@ -199,6 +261,66 @@ class Agent {
|
|
|
199
261
|
}
|
|
200
262
|
utils_1.logger.debug("[Agent] Schema validation passed");
|
|
201
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Walk every flow's steps and resolve deferred string `then` values in branches
|
|
266
|
+
* against the agent's flow registry. Strings that match neither a local step id
|
|
267
|
+
* nor any flow id/title throw FlowConfigurationError.
|
|
268
|
+
* @private
|
|
269
|
+
*/
|
|
270
|
+
validateBranchReferences() {
|
|
271
|
+
for (const flow of this._flows) {
|
|
272
|
+
this.validateFlowBranchReferences(flow);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Validate branch `then` string references for a single flow against the agent's
|
|
277
|
+
* flow registry. Throws FlowConfigurationError for unresolved references.
|
|
278
|
+
* @private
|
|
279
|
+
*/
|
|
280
|
+
validateFlowBranchReferences(flow) {
|
|
281
|
+
const steps = flow.getAllSteps();
|
|
282
|
+
const localStepIds = new Set(steps.map(s => s.id));
|
|
283
|
+
for (const step of steps) {
|
|
284
|
+
if (!step.branches)
|
|
285
|
+
continue;
|
|
286
|
+
for (const entry of step.branches) {
|
|
287
|
+
if (typeof entry.then !== 'string')
|
|
288
|
+
continue;
|
|
289
|
+
// Already matches a local step id — no deferred resolution needed
|
|
290
|
+
if (localStepIds.has(entry.then))
|
|
291
|
+
continue;
|
|
292
|
+
// Check against the agent's flow registry (id or title)
|
|
293
|
+
const matchesFlow = this._flows.some(f => f.id === entry.then || f.title === entry.then);
|
|
294
|
+
if (!matchesFlow) {
|
|
295
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] Unresolved branch target: "${entry.then}" in ${flow.id}.${step.id} does not match any step in the flow or any flow in the agent. ` +
|
|
296
|
+
`Fix the branch "then" value to reference a valid step id or flow id/title.`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Validate that every step's `collect` fields in a flow reference valid keys
|
|
303
|
+
* from the agent-level schema. Throws FlowConfigurationError at construction
|
|
304
|
+
* time if any collect field is not a valid schema key.
|
|
305
|
+
*
|
|
306
|
+
* This enforces Requirement 14.5: generic inference is preserved AND every
|
|
307
|
+
* `collect` field reference is a valid key of the inferred TData.
|
|
308
|
+
* @private
|
|
309
|
+
*/
|
|
310
|
+
validateFlowCollectFields(flow) {
|
|
311
|
+
const schemaKeys = Object.keys(this._schema.properties);
|
|
312
|
+
const schemaKeySet = new Set(schemaKeys);
|
|
313
|
+
const steps = flow.getAllSteps();
|
|
314
|
+
for (const step of steps) {
|
|
315
|
+
if (!step.collect || step.collect.length === 0)
|
|
316
|
+
continue;
|
|
317
|
+
const invalidFields = step.collect.filter(field => !schemaKeySet.has(String(field)));
|
|
318
|
+
if (invalidFields.length > 0) {
|
|
319
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] Step "${step.id}" in flow "${flow.title}" references invalid collect fields: ${invalidFields.map(f => String(f)).join(', ')}. ` +
|
|
320
|
+
`Must be valid keys from agent schema. Available fields: ${schemaKeys.join(', ')}.`);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
202
324
|
/**
|
|
203
325
|
* Validate data against the agent-level schema
|
|
204
326
|
*/
|
|
@@ -269,7 +391,7 @@ class Agent {
|
|
|
269
391
|
const validation = this.validateData(updates);
|
|
270
392
|
if (!validation.valid) {
|
|
271
393
|
const errorMessages = validation.errors.map(e => e.message).join(', ');
|
|
272
|
-
throw new DataValidationError(validation.errors, `Data validation failed: ${errorMessages}
|
|
394
|
+
throw new DataValidationError(validation.errors, `[DataValidationError] Data validation failed: fields [${errorMessages}] did not pass schema validation. Fix the offending values to match the declared schema.`);
|
|
273
395
|
}
|
|
274
396
|
// Log warnings if any
|
|
275
397
|
if (validation.warnings.length > 0) {
|
|
@@ -314,16 +436,16 @@ class Agent {
|
|
|
314
436
|
this.options.name = value;
|
|
315
437
|
}
|
|
316
438
|
/**
|
|
317
|
-
* Get agent
|
|
439
|
+
* Get agent persona
|
|
318
440
|
*/
|
|
319
|
-
get
|
|
320
|
-
return this.options.
|
|
441
|
+
get persona() {
|
|
442
|
+
return this.options.persona;
|
|
321
443
|
}
|
|
322
444
|
/**
|
|
323
|
-
* Set agent
|
|
445
|
+
* Set agent persona
|
|
324
446
|
*/
|
|
325
|
-
set
|
|
326
|
-
this.options.
|
|
447
|
+
set persona(value) {
|
|
448
|
+
this.options.persona = value;
|
|
327
449
|
}
|
|
328
450
|
/**
|
|
329
451
|
* Get agent goal
|
|
@@ -337,30 +459,6 @@ class Agent {
|
|
|
337
459
|
set goal(value) {
|
|
338
460
|
this.options.goal = value;
|
|
339
461
|
}
|
|
340
|
-
/**
|
|
341
|
-
* Get agent identity
|
|
342
|
-
*/
|
|
343
|
-
get identity() {
|
|
344
|
-
return this.options.identity;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Set agent identity
|
|
348
|
-
*/
|
|
349
|
-
set identity(value) {
|
|
350
|
-
this.options.identity = value;
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Get agent personality
|
|
354
|
-
*/
|
|
355
|
-
get personality() {
|
|
356
|
-
return this.options.personality;
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* Set agent personality
|
|
360
|
-
*/
|
|
361
|
-
set personality(value) {
|
|
362
|
-
this.options.personality = value;
|
|
363
|
-
}
|
|
364
462
|
/**
|
|
365
463
|
* Get whether debug mode is enabled
|
|
366
464
|
*/
|
|
@@ -387,29 +485,17 @@ class Agent {
|
|
|
387
485
|
this.options.provider = value;
|
|
388
486
|
}
|
|
389
487
|
/**
|
|
390
|
-
* Get the
|
|
391
|
-
*/
|
|
392
|
-
get compositionMode() {
|
|
393
|
-
return this.options.compositionMode ?? types_1.CompositionMode.FLUID;
|
|
394
|
-
}
|
|
395
|
-
/**
|
|
396
|
-
* Set the composition mode
|
|
397
|
-
*/
|
|
398
|
-
set compositionMode(value) {
|
|
399
|
-
this.options.compositionMode = value;
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Get the route switch margin
|
|
488
|
+
* Get the flow switch margin
|
|
403
489
|
* @default 15
|
|
404
490
|
*/
|
|
405
|
-
get
|
|
406
|
-
return this.options.
|
|
491
|
+
get flowSwitchMargin() {
|
|
492
|
+
return this.options.flowSwitchMargin ?? 15;
|
|
407
493
|
}
|
|
408
494
|
/**
|
|
409
|
-
* Set the
|
|
495
|
+
* Set the flow switch margin
|
|
410
496
|
*/
|
|
411
|
-
set
|
|
412
|
-
this.options.
|
|
497
|
+
set flowSwitchMargin(value) {
|
|
498
|
+
this.options.flowSwitchMargin = value;
|
|
413
499
|
}
|
|
414
500
|
/**
|
|
415
501
|
* Get the prompt section cache instance
|
|
@@ -417,19 +503,6 @@ class Agent {
|
|
|
417
503
|
get promptSectionCache() {
|
|
418
504
|
return this._promptSectionCache;
|
|
419
505
|
}
|
|
420
|
-
/**
|
|
421
|
-
* Get the maximum steps per batch
|
|
422
|
-
* @default 1
|
|
423
|
-
*/
|
|
424
|
-
get maxStepsPerBatch() {
|
|
425
|
-
return this.options.maxStepsPerBatch ?? 1;
|
|
426
|
-
}
|
|
427
|
-
/**
|
|
428
|
-
* Set the maximum steps per batch
|
|
429
|
-
*/
|
|
430
|
-
set maxStepsPerBatch(value) {
|
|
431
|
-
this.options.maxStepsPerBatch = value;
|
|
432
|
-
}
|
|
433
506
|
/**
|
|
434
507
|
* Get all terms
|
|
435
508
|
*/
|
|
@@ -437,10 +510,10 @@ class Agent {
|
|
|
437
510
|
return [...this._terms];
|
|
438
511
|
}
|
|
439
512
|
/**
|
|
440
|
-
* Get all
|
|
513
|
+
* Get all instructions
|
|
441
514
|
*/
|
|
442
|
-
get
|
|
443
|
-
return [...this.
|
|
515
|
+
get instructions() {
|
|
516
|
+
return [...this._instructions];
|
|
444
517
|
}
|
|
445
518
|
/**
|
|
446
519
|
* Get all tools
|
|
@@ -449,34 +522,10 @@ class Agent {
|
|
|
449
522
|
return [...this._tools];
|
|
450
523
|
}
|
|
451
524
|
/**
|
|
452
|
-
* Get all
|
|
453
|
-
*/
|
|
454
|
-
get routes() {
|
|
455
|
-
return [...this._routes];
|
|
456
|
-
}
|
|
457
|
-
/**
|
|
458
|
-
* Get agent-level rules
|
|
459
|
-
*/
|
|
460
|
-
get rules() {
|
|
461
|
-
return [...this._rules];
|
|
462
|
-
}
|
|
463
|
-
/**
|
|
464
|
-
* Set agent-level rules
|
|
465
|
-
*/
|
|
466
|
-
set rules(value) {
|
|
467
|
-
this._rules = [...value];
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* Get agent-level prohibitions
|
|
525
|
+
* Get all flows
|
|
471
526
|
*/
|
|
472
|
-
get
|
|
473
|
-
return [...this.
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* Set agent-level prohibitions
|
|
477
|
-
*/
|
|
478
|
-
set prohibitions(value) {
|
|
479
|
-
this._prohibitions = [...value];
|
|
527
|
+
get flows() {
|
|
528
|
+
return [...this._flows];
|
|
480
529
|
}
|
|
481
530
|
/**
|
|
482
531
|
* Get current schema
|
|
@@ -493,6 +542,13 @@ class Agent {
|
|
|
493
542
|
}
|
|
494
543
|
this._schema = value;
|
|
495
544
|
}
|
|
545
|
+
/**
|
|
546
|
+
* Get the configured signals.
|
|
547
|
+
*/
|
|
548
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
549
|
+
get signals() {
|
|
550
|
+
return this._signals;
|
|
551
|
+
}
|
|
496
552
|
/**
|
|
497
553
|
* Get the agent's knowledge base
|
|
498
554
|
*/
|
|
@@ -519,98 +575,38 @@ class Agent {
|
|
|
519
575
|
this._currentSession = value;
|
|
520
576
|
this._promptSectionCache.invalidateAll();
|
|
521
577
|
}
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
578
|
+
/**
|
|
579
|
+
* Get all flows
|
|
580
|
+
*/
|
|
581
|
+
getFlows() {
|
|
582
|
+
return this.flows;
|
|
583
|
+
}
|
|
525
584
|
/**
|
|
526
585
|
* Get all terms
|
|
527
|
-
* @deprecated Use `agent.terms` instead
|
|
528
586
|
*/
|
|
529
587
|
getTerms() {
|
|
530
588
|
return this.terms;
|
|
531
589
|
}
|
|
532
590
|
/**
|
|
533
591
|
* Get all tools
|
|
534
|
-
* @deprecated Use `agent.tools` instead
|
|
535
592
|
*/
|
|
536
593
|
getTools() {
|
|
537
594
|
return this.tools;
|
|
538
595
|
}
|
|
539
596
|
/**
|
|
540
|
-
* Get all
|
|
541
|
-
* @deprecated Use `agent.guidelines` instead
|
|
597
|
+
* Get all instructions
|
|
542
598
|
*/
|
|
543
|
-
|
|
544
|
-
return this.
|
|
599
|
+
getInstructions() {
|
|
600
|
+
return this.instructions;
|
|
545
601
|
}
|
|
546
602
|
/**
|
|
547
|
-
*
|
|
548
|
-
*
|
|
603
|
+
* Invalidate flow-dependent prompt cache sections.
|
|
604
|
+
* Called automatically when the active flow changes.
|
|
549
605
|
*/
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
* Get agent-level rules
|
|
555
|
-
* @deprecated Use `agent.rules` instead
|
|
556
|
-
*/
|
|
557
|
-
getRules() {
|
|
558
|
-
return this.rules;
|
|
559
|
-
}
|
|
560
|
-
/**
|
|
561
|
-
* Get agent-level prohibitions
|
|
562
|
-
* @deprecated Use `agent.prohibitions` instead
|
|
563
|
-
*/
|
|
564
|
-
getProhibitions() {
|
|
565
|
-
return this.prohibitions;
|
|
566
|
-
}
|
|
567
|
-
/**
|
|
568
|
-
* Get current schema
|
|
569
|
-
* @deprecated Use `agent.schema` instead
|
|
570
|
-
*/
|
|
571
|
-
getSchema() {
|
|
572
|
-
return this.schema;
|
|
573
|
-
}
|
|
574
|
-
/**
|
|
575
|
-
* Get the agent's knowledge base
|
|
576
|
-
* @deprecated Use `agent.knowledgeBase` instead
|
|
577
|
-
*/
|
|
578
|
-
getKnowledgeBase() {
|
|
579
|
-
return this.knowledgeBase;
|
|
580
|
-
}
|
|
581
|
-
/**
|
|
582
|
-
* Get the current session (if set)
|
|
583
|
-
* @deprecated Use `agent.currentSession` instead
|
|
584
|
-
*/
|
|
585
|
-
getCurrentSession() {
|
|
586
|
-
return this.currentSession;
|
|
587
|
-
}
|
|
588
|
-
/**
|
|
589
|
-
* Set the current session for convenience methods
|
|
590
|
-
* @deprecated Use `agent.currentSession = session` instead
|
|
591
|
-
* @param session - Session step to use for subsequent calls
|
|
592
|
-
*/
|
|
593
|
-
setCurrentSession(session) {
|
|
594
|
-
this.currentSession = session;
|
|
595
|
-
this._promptSectionCache.invalidateAll();
|
|
596
|
-
}
|
|
597
|
-
/**
|
|
598
|
-
* Clear the current session
|
|
599
|
-
* @deprecated Use `agent.currentSession = undefined` instead
|
|
600
|
-
*/
|
|
601
|
-
clearCurrentSession() {
|
|
602
|
-
this._currentSession = undefined;
|
|
603
|
-
this._promptSectionCache.invalidateAll();
|
|
604
|
-
}
|
|
605
|
-
/**
|
|
606
|
-
* Invalidate route-dependent prompt cache sections.
|
|
607
|
-
* Called automatically when the active route changes.
|
|
608
|
-
*/
|
|
609
|
-
invalidateRouteSections() {
|
|
610
|
-
this._promptSectionCache.invalidate('activeRoutes');
|
|
611
|
-
this._promptSectionCache.invalidate('routeRules');
|
|
612
|
-
this._promptSectionCache.invalidate('routeProhibitions');
|
|
613
|
-
this._promptSectionCache.invalidate('routeKnowledgeBase');
|
|
606
|
+
invalidateFlowSections() {
|
|
607
|
+
this._promptSectionCache.invalidate('activeFlows');
|
|
608
|
+
this._promptSectionCache.invalidate('flowKnowledgeBase');
|
|
609
|
+
this._promptSectionCache.invalidate('instructionsFlow');
|
|
614
610
|
}
|
|
615
611
|
/**
|
|
616
612
|
* Get the persistence manager (if configured)
|
|
@@ -634,28 +630,32 @@ class Agent {
|
|
|
634
630
|
// Core methods
|
|
635
631
|
// ---------------------------------------------------------------------------
|
|
636
632
|
/**
|
|
637
|
-
* Create a new
|
|
633
|
+
* Create a new flow (journey) using agent-level data type
|
|
638
634
|
*/
|
|
639
|
-
|
|
635
|
+
createFlow(options) {
|
|
640
636
|
// Validate that requiredFields exist in agent schema
|
|
641
637
|
if (options.requiredFields && this._schema?.properties) {
|
|
642
638
|
const invalidRequiredFields = options.requiredFields.filter(field => !(String(field) in this._schema.properties));
|
|
643
639
|
if (invalidRequiredFields.length > 0) {
|
|
644
|
-
throw new
|
|
645
|
-
`
|
|
640
|
+
throw new FlowConfigurationError(options.title, invalidRequiredFields.map(f => String(f)), `[FlowConfigurationError] Invalid required fields in flow "${options.title}": [${invalidRequiredFields.join(', ')}] are not declared in the agent schema. ` +
|
|
641
|
+
`Use valid schema keys. Available fields: ${Object.keys(this._schema.properties).join(', ')}.`);
|
|
646
642
|
}
|
|
647
643
|
}
|
|
648
644
|
// Validate that optionalFields exist in agent schema
|
|
649
645
|
if (options.optionalFields && this._schema?.properties) {
|
|
650
646
|
const invalidOptionalFields = options.optionalFields.filter(field => !(String(field) in this._schema.properties));
|
|
651
647
|
if (invalidOptionalFields.length > 0) {
|
|
652
|
-
throw new
|
|
653
|
-
`
|
|
648
|
+
throw new FlowConfigurationError(options.title, invalidOptionalFields.map(f => String(f)), `[FlowConfigurationError] Invalid optional fields in flow "${options.title}": [${invalidOptionalFields.join(', ')}] are not declared in the agent schema. ` +
|
|
649
|
+
`Use valid schema keys. Available fields: ${Object.keys(this._schema.properties).join(', ')}.`);
|
|
654
650
|
}
|
|
655
651
|
}
|
|
656
|
-
const
|
|
657
|
-
|
|
658
|
-
|
|
652
|
+
const flow = new Flow_1.Flow(options, this);
|
|
653
|
+
// Validate that step collect fields reference valid schema keys
|
|
654
|
+
if (this._schema?.properties) {
|
|
655
|
+
this.validateFlowCollectFields(flow);
|
|
656
|
+
}
|
|
657
|
+
this._flows.push(flow);
|
|
658
|
+
return flow;
|
|
659
659
|
}
|
|
660
660
|
/**
|
|
661
661
|
* Create a domain term for the glossary
|
|
@@ -665,20 +665,22 @@ class Agent {
|
|
|
665
665
|
return this;
|
|
666
666
|
}
|
|
667
667
|
/**
|
|
668
|
-
* Create
|
|
668
|
+
* Create an instruction (unified behavioral primitive).
|
|
669
669
|
*/
|
|
670
|
-
|
|
671
|
-
const
|
|
672
|
-
...
|
|
673
|
-
|
|
674
|
-
|
|
670
|
+
createInstruction(instruction) {
|
|
671
|
+
const instructionWithId = {
|
|
672
|
+
...instruction,
|
|
673
|
+
kind: instruction.kind || 'should',
|
|
674
|
+
id: instruction.id || `instruction_${this._instructions.length}`,
|
|
675
|
+
enabled: instruction.enabled !== false, // Default to true
|
|
675
676
|
};
|
|
676
|
-
this.
|
|
677
|
+
this._instructions.push(instructionWithId);
|
|
678
|
+
this._promptSectionCache.invalidate('instructionsGlobal');
|
|
677
679
|
return this;
|
|
678
680
|
}
|
|
679
681
|
/**
|
|
680
682
|
* Add a tool to the agent using the unified Tool interface
|
|
681
|
-
* Creates and adds the tool to agent scope in one operation
|
|
683
|
+
* Creates and adds the tool to agent scope in one operation
|
|
682
684
|
*/
|
|
683
685
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
684
686
|
addTool(tool) {
|
|
@@ -691,20 +693,6 @@ class Agent {
|
|
|
691
693
|
utils_1.logger.debug(`[Agent] Added tool to agent scope: ${tool.id}`);
|
|
692
694
|
return this;
|
|
693
695
|
}
|
|
694
|
-
/**
|
|
695
|
-
* Register a tool at the agent level (legacy method for backward compatibility)
|
|
696
|
-
* @deprecated Use addTool() with Tool interface instead
|
|
697
|
-
*/
|
|
698
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
699
|
-
createTool(tool) {
|
|
700
|
-
// Validate tool before adding
|
|
701
|
-
if (!tool || !tool.id || !tool.handler) {
|
|
702
|
-
throw new Error('Invalid tool: must have id and handler properties');
|
|
703
|
-
}
|
|
704
|
-
this._tools.push(tool);
|
|
705
|
-
utils_1.logger.debug(`[Agent] Created tool (legacy): ${tool.id}`);
|
|
706
|
-
return this;
|
|
707
|
-
}
|
|
708
696
|
/**
|
|
709
697
|
* Register multiple tools at the agent level
|
|
710
698
|
*/
|
|
@@ -722,7 +710,7 @@ class Agent {
|
|
|
722
710
|
}
|
|
723
711
|
/**
|
|
724
712
|
* Update the agent's context
|
|
725
|
-
* Triggers both agent-level and
|
|
713
|
+
* Triggers both agent-level and flow-specific onContextUpdate lifecycle hooks if configured
|
|
726
714
|
*/
|
|
727
715
|
async updateContext(updates) {
|
|
728
716
|
const previousContext = this._context;
|
|
@@ -731,12 +719,12 @@ class Agent {
|
|
|
731
719
|
...this._context,
|
|
732
720
|
...updates,
|
|
733
721
|
};
|
|
734
|
-
// Trigger
|
|
735
|
-
if (this._currentSession?.
|
|
736
|
-
const
|
|
737
|
-
if (
|
|
722
|
+
// Trigger flow-specific lifecycle hook if configured and session has current flow
|
|
723
|
+
if (this._currentSession?.currentFlow) {
|
|
724
|
+
const currentFlow = this._flows.find((r) => r.id === this._currentSession.currentFlow?.id);
|
|
725
|
+
if (currentFlow?.hooks?.onContextUpdate &&
|
|
738
726
|
previousContext !== undefined) {
|
|
739
|
-
await
|
|
727
|
+
await currentFlow.handleContextUpdate(this._context, previousContext);
|
|
740
728
|
}
|
|
741
729
|
}
|
|
742
730
|
// Trigger agent-level lifecycle hook if configured
|
|
@@ -746,10 +734,11 @@ class Agent {
|
|
|
746
734
|
// Invalidate context-dependent prompt cache sections
|
|
747
735
|
this._promptSectionCache.invalidate('agentMeta');
|
|
748
736
|
this._promptSectionCache.invalidate('knowledgeBase');
|
|
737
|
+
this._promptSectionCache.invalidate('instructionsGlobal');
|
|
749
738
|
}
|
|
750
739
|
/**
|
|
751
740
|
* Update collected data in session with lifecycle hook support
|
|
752
|
-
* Triggers both agent-level and
|
|
741
|
+
* Triggers both agent-level and flow-specific onDataUpdate lifecycle hooks if configured
|
|
753
742
|
* @internal
|
|
754
743
|
*/
|
|
755
744
|
async updateData(session, dataUpdate) {
|
|
@@ -759,11 +748,11 @@ class Agent {
|
|
|
759
748
|
...session.data,
|
|
760
749
|
...dataUpdate,
|
|
761
750
|
};
|
|
762
|
-
// Trigger
|
|
763
|
-
if (session.
|
|
764
|
-
const
|
|
765
|
-
if (
|
|
766
|
-
newCollected = await
|
|
751
|
+
// Trigger flow-specific lifecycle hook if configured and session has a current flow
|
|
752
|
+
if (session.currentFlow) {
|
|
753
|
+
const currentFlow = this._flows.find((r) => r.id === session.currentFlow?.id);
|
|
754
|
+
if (currentFlow?.hooks?.onDataUpdate) {
|
|
755
|
+
newCollected = await currentFlow.handleDataUpdate(newCollected, previousCollected);
|
|
767
756
|
}
|
|
768
757
|
}
|
|
769
758
|
// Trigger agent-level lifecycle hook if configured
|
|
@@ -808,10 +797,10 @@ class Agent {
|
|
|
808
797
|
return this.options;
|
|
809
798
|
}
|
|
810
799
|
/**
|
|
811
|
-
* Get
|
|
800
|
+
* Get flow router
|
|
812
801
|
* @internal Used by ResponseModal
|
|
813
802
|
*/
|
|
814
|
-
|
|
803
|
+
getFlowRouter() {
|
|
815
804
|
return this._routingEngine;
|
|
816
805
|
}
|
|
817
806
|
/**
|
|
@@ -821,51 +810,11 @@ class Agent {
|
|
|
821
810
|
getUpdateDataMethod() {
|
|
822
811
|
return this.updateData.bind(this);
|
|
823
812
|
}
|
|
824
|
-
/**
|
|
825
|
-
* Evaluate and match active guidelines based on their conditions
|
|
826
|
-
* Returns guidelines that should be active given the current context
|
|
827
|
-
*/
|
|
828
|
-
async evaluateGuidelines(context, session, history) {
|
|
829
|
-
const templateContext = { context, session, history, data: session?.data };
|
|
830
|
-
const evaluator = (0, utils_1.createConditionEvaluator)(templateContext);
|
|
831
|
-
const matches = [];
|
|
832
|
-
for (const guideline of this._guidelines) {
|
|
833
|
-
// Skip disabled guidelines
|
|
834
|
-
if (guideline.enabled === false) {
|
|
835
|
-
continue;
|
|
836
|
-
}
|
|
837
|
-
if (guideline.condition) {
|
|
838
|
-
const evaluation = await evaluator.evaluateCondition(guideline.condition, 'AND');
|
|
839
|
-
// Include guideline if:
|
|
840
|
-
// 1. No programmatic conditions (only strings) - always active
|
|
841
|
-
// 2. Programmatic conditions evaluate to true
|
|
842
|
-
if (!evaluation.hasProgrammaticConditions || evaluation.programmaticResult) {
|
|
843
|
-
const rationale = evaluation.aiContextStrings.length > 0
|
|
844
|
-
? `Condition met: ${evaluation.aiContextStrings.join(" AND ")}`
|
|
845
|
-
: evaluation.hasProgrammaticConditions
|
|
846
|
-
? "Programmatic condition evaluated to true"
|
|
847
|
-
: "Always active (no conditions)";
|
|
848
|
-
matches.push({
|
|
849
|
-
guideline,
|
|
850
|
-
rationale
|
|
851
|
-
});
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
else {
|
|
855
|
-
// No condition means always active
|
|
856
|
-
matches.push({
|
|
857
|
-
guideline,
|
|
858
|
-
rationale: "Always active (no conditions)"
|
|
859
|
-
});
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
return matches;
|
|
863
|
-
}
|
|
864
813
|
/**
|
|
865
814
|
* Execute a prepare or finalize function/tool
|
|
866
815
|
* @internal Used by ResponseModal
|
|
867
816
|
*/
|
|
868
|
-
async executePrepareFinalize(prepareOrFinalize, context, data,
|
|
817
|
+
async executePrepareFinalize(prepareOrFinalize, context, data, flow, step) {
|
|
869
818
|
if (!prepareOrFinalize)
|
|
870
819
|
return;
|
|
871
820
|
if (typeof prepareOrFinalize === "function") {
|
|
@@ -877,7 +826,7 @@ class Agent {
|
|
|
877
826
|
let tool;
|
|
878
827
|
if (typeof prepareOrFinalize === "string") {
|
|
879
828
|
// Tool ID - use ToolManager to find it across all scopes
|
|
880
|
-
tool = this.tool.find(prepareOrFinalize, undefined, step,
|
|
829
|
+
tool = this.tool.find(prepareOrFinalize, undefined, step, flow);
|
|
881
830
|
}
|
|
882
831
|
else {
|
|
883
832
|
// Tool object - validate it has required properties
|
|
@@ -924,7 +873,6 @@ class Agent {
|
|
|
924
873
|
}
|
|
925
874
|
/**
|
|
926
875
|
* Get collected data from current session or agent-level collected data
|
|
927
|
-
* @param routeId - Optional route ID to get data for (uses current route if not provided)
|
|
928
876
|
* @returns The collected data from the current session or agent-level data
|
|
929
877
|
*/
|
|
930
878
|
getData() {
|
|
@@ -932,64 +880,263 @@ class Agent {
|
|
|
932
880
|
this.syncSessionDataToCollectedData();
|
|
933
881
|
// If we have a current session, use session data
|
|
934
882
|
if (this._currentSession) {
|
|
935
|
-
// With agent-level data, all
|
|
936
|
-
// No need for
|
|
883
|
+
// With agent-level data, all flows share the same data structure
|
|
884
|
+
// No need for flow-specific data access
|
|
937
885
|
return (this._currentSession.data) || {};
|
|
938
886
|
}
|
|
939
887
|
// Otherwise, return agent-level collected data
|
|
940
888
|
return this.getCollectedData();
|
|
941
889
|
}
|
|
942
890
|
/**
|
|
943
|
-
*
|
|
944
|
-
* Sets
|
|
891
|
+
* Dispatch a directive (or a flow shorthand) into a session.
|
|
892
|
+
* Sets `pendingDirective` on the session without triggering a `respond()` call.
|
|
893
|
+
* The directive will be applied at the start of the next turn.
|
|
894
|
+
*
|
|
895
|
+
* String form desugars to `{ goTo: target }`.
|
|
945
896
|
*
|
|
946
|
-
* @param
|
|
947
|
-
* @param session - Session
|
|
948
|
-
* @
|
|
949
|
-
*
|
|
897
|
+
* @param target - Flow ID/title string (desugars to `{ goTo: target }`) or a full Directive
|
|
898
|
+
* @param session - Session to update (uses current session if not provided)
|
|
899
|
+
* @returns Updated session with `pendingDirective` set
|
|
900
|
+
*
|
|
901
|
+
* @throws FlowConfigurationError if the string target doesn't match any flow
|
|
902
|
+
* @throws FlowConfigurationError if the directive fails validation
|
|
903
|
+
*
|
|
904
|
+
* @example
|
|
905
|
+
* // String shorthand — desugars to { goTo: 'Feedback' }
|
|
906
|
+
* const updated = await agent.dispatch('Feedback', session);
|
|
950
907
|
*
|
|
951
908
|
* @example
|
|
952
|
-
* //
|
|
953
|
-
*
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
* }
|
|
958
|
-
*/
|
|
959
|
-
async nextStepRoute(routeIdOrTitle, session, condition, history) {
|
|
909
|
+
* // Full directive
|
|
910
|
+
* const updated = await agent.dispatch({ goTo: 'Billing', reply: 'Transferring you now.' }, session);
|
|
911
|
+
*/
|
|
912
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
913
|
+
async dispatch(target, session) {
|
|
960
914
|
const targetSession = session || this._currentSession;
|
|
961
915
|
if (!targetSession) {
|
|
962
|
-
throw new Error("No session provided and no current session available. Please provide a session to
|
|
963
|
-
}
|
|
964
|
-
//
|
|
965
|
-
const
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
916
|
+
throw new Error("No session provided and no current session available. Please provide a session to dispatch into.");
|
|
917
|
+
}
|
|
918
|
+
// Desugar string form to { goTo: target }
|
|
919
|
+
const directive = typeof target === 'string'
|
|
920
|
+
? { goTo: target }
|
|
921
|
+
: target;
|
|
922
|
+
// Validate the directive: check for multiple position fields, empty goTo, etc.
|
|
923
|
+
this.validateDirective(directive);
|
|
924
|
+
// If goTo is a string, validate it references a known flow
|
|
925
|
+
if (typeof directive.goTo === 'string') {
|
|
926
|
+
const flowTarget = directive.goTo;
|
|
927
|
+
const matchesFlow = this._flows.some(f => f.id === flowTarget || f.title === flowTarget);
|
|
928
|
+
if (!matchesFlow) {
|
|
929
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] Unknown flow: "${flowTarget}" does not match any flow id or title. ` +
|
|
930
|
+
`Available flows: ${this._flows.map(f => f.title).join(', ')}.`);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
else if (directive.goTo && typeof directive.goTo === 'object' && directive.goTo.flow) {
|
|
934
|
+
const flowTarget = directive.goTo.flow;
|
|
935
|
+
const matchesFlow = this._flows.some(f => f.id === flowTarget || f.title === flowTarget);
|
|
936
|
+
if (!matchesFlow) {
|
|
937
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] Unknown flow: "${flowTarget}" does not match any flow id or title. ` +
|
|
938
|
+
`Available flows: ${this._flows.map(f => f.title).join(', ')}.`);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
// Strip PreDirective-only fields before storing
|
|
942
|
+
const stripped = this.stripPreDirectiveFields(directive);
|
|
943
|
+
// Set pendingDirective on the session without applying it
|
|
978
944
|
const updatedSession = {
|
|
979
945
|
...targetSession,
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
946
|
+
pendingDirective: stripped,
|
|
947
|
+
metadata: {
|
|
948
|
+
...targetSession.metadata,
|
|
949
|
+
lastUpdatedAt: new Date(),
|
|
984
950
|
},
|
|
985
951
|
};
|
|
986
|
-
// Update current session if
|
|
952
|
+
// Update current session in place if no explicit session was passed
|
|
987
953
|
if (!session && this._currentSession) {
|
|
988
954
|
this._currentSession = updatedSession;
|
|
989
955
|
}
|
|
990
|
-
utils_1.logger.debug(`[Agent]
|
|
956
|
+
utils_1.logger.debug(`[Agent] Dispatched directive: pendingDirective set on session ${updatedSession.id}`);
|
|
957
|
+
return updatedSession;
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Apply a directive synchronously to a session without invoking `respond()`.
|
|
961
|
+
* Performs in-place application: updates flow/step position, merges state writes.
|
|
962
|
+
*
|
|
963
|
+
* This is the synchronous counterpart to `dispatch` — it applies immediately
|
|
964
|
+
* rather than deferring to the next turn.
|
|
965
|
+
*
|
|
966
|
+
* @param directive - The directive to apply
|
|
967
|
+
* @param session - The session to apply the directive to
|
|
968
|
+
* @returns The updated session with the directive applied
|
|
969
|
+
*/
|
|
970
|
+
applyDirective(directive, session) {
|
|
971
|
+
// Validate the directive
|
|
972
|
+
this.validateDirective(directive);
|
|
973
|
+
let updatedSession = { ...session };
|
|
974
|
+
const now = new Date();
|
|
975
|
+
// Apply state writes
|
|
976
|
+
if (directive.contextUpdate) {
|
|
977
|
+
// Context updates are applied to the agent, not the session
|
|
978
|
+
this._context = {
|
|
979
|
+
...this._context,
|
|
980
|
+
...directive.contextUpdate,
|
|
981
|
+
};
|
|
982
|
+
}
|
|
983
|
+
if (directive.dataUpdate) {
|
|
984
|
+
updatedSession = {
|
|
985
|
+
...updatedSession,
|
|
986
|
+
data: {
|
|
987
|
+
...updatedSession.data,
|
|
988
|
+
...directive.dataUpdate,
|
|
989
|
+
},
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
// Apply position control
|
|
993
|
+
if (directive.goTo) {
|
|
994
|
+
const flowTarget = typeof directive.goTo === 'string'
|
|
995
|
+
? directive.goTo
|
|
996
|
+
: directive.goTo.flow;
|
|
997
|
+
if (flowTarget) {
|
|
998
|
+
const targetFlow = this._flows.find(f => f.id === flowTarget || f.title === flowTarget);
|
|
999
|
+
if (targetFlow) {
|
|
1000
|
+
// Merge goTo.data if present
|
|
1001
|
+
if (typeof directive.goTo === 'object' && directive.goTo.data) {
|
|
1002
|
+
updatedSession = {
|
|
1003
|
+
...updatedSession,
|
|
1004
|
+
data: {
|
|
1005
|
+
...updatedSession.data,
|
|
1006
|
+
...directive.goTo.data,
|
|
1007
|
+
},
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
updatedSession = (0, utils_1.enterFlow)(updatedSession, targetFlow.id, targetFlow.title);
|
|
1011
|
+
// If a specific step is targeted
|
|
1012
|
+
if (typeof directive.goTo === 'object' && directive.goTo.step) {
|
|
1013
|
+
updatedSession = (0, utils_1.enterStep)(updatedSession, directive.goTo.step);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
else if (directive.goToStep) {
|
|
1019
|
+
const stepTarget = typeof directive.goToStep === 'string'
|
|
1020
|
+
? directive.goToStep
|
|
1021
|
+
: directive.goToStep.step;
|
|
1022
|
+
// Merge goToStep.data if present
|
|
1023
|
+
if (typeof directive.goToStep === 'object' && directive.goToStep.data) {
|
|
1024
|
+
updatedSession = {
|
|
1025
|
+
...updatedSession,
|
|
1026
|
+
data: {
|
|
1027
|
+
...updatedSession.data,
|
|
1028
|
+
...directive.goToStep.data,
|
|
1029
|
+
},
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
updatedSession = (0, utils_1.enterStep)(updatedSession, stepTarget);
|
|
1033
|
+
}
|
|
1034
|
+
else if (directive.complete) {
|
|
1035
|
+
updatedSession = (0, utils_1.completeCurrentFlow)(updatedSession);
|
|
1036
|
+
// If complete carries a chained directive, set it as pendingDirective
|
|
1037
|
+
if (typeof directive.complete === 'object' && directive.complete.next) {
|
|
1038
|
+
updatedSession = {
|
|
1039
|
+
...updatedSession,
|
|
1040
|
+
pendingDirective: directive.complete.next,
|
|
1041
|
+
};
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
else if (directive.abort) {
|
|
1045
|
+
const clearSession = typeof directive.abort === 'object'
|
|
1046
|
+
? directive.abort.clearSession !== false
|
|
1047
|
+
: true;
|
|
1048
|
+
if (clearSession) {
|
|
1049
|
+
updatedSession = {
|
|
1050
|
+
...updatedSession,
|
|
1051
|
+
currentFlow: undefined,
|
|
1052
|
+
currentStep: undefined,
|
|
1053
|
+
data: {},
|
|
1054
|
+
};
|
|
1055
|
+
}
|
|
1056
|
+
else {
|
|
1057
|
+
updatedSession = {
|
|
1058
|
+
...updatedSession,
|
|
1059
|
+
currentFlow: undefined,
|
|
1060
|
+
currentStep: undefined,
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
else if (directive.reset) {
|
|
1065
|
+
const currentFlowId = updatedSession.currentFlow?.id;
|
|
1066
|
+
const currentFlowTitle = updatedSession.currentFlow?.title;
|
|
1067
|
+
if (currentFlowId && currentFlowTitle) {
|
|
1068
|
+
// Clear data if requested
|
|
1069
|
+
if (typeof directive.reset === 'object' && directive.reset.clearData) {
|
|
1070
|
+
const currentFlow = this._flows.find(f => f.id === currentFlowId);
|
|
1071
|
+
if (currentFlow) {
|
|
1072
|
+
const ownedFields = [
|
|
1073
|
+
...(currentFlow.requiredFields || []),
|
|
1074
|
+
...(currentFlow.optionalFields || []),
|
|
1075
|
+
];
|
|
1076
|
+
updatedSession = (0, utils_1.completeCurrentFlow)(updatedSession, { clearOwnedFields: ownedFields });
|
|
1077
|
+
// Re-enter the same flow
|
|
1078
|
+
updatedSession = (0, utils_1.enterFlow)(updatedSession, currentFlowId, currentFlowTitle);
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
else {
|
|
1082
|
+
// Re-enter the flow from the beginning (or specified step)
|
|
1083
|
+
updatedSession = (0, utils_1.enterFlow)(updatedSession, currentFlowId, currentFlowTitle);
|
|
1084
|
+
}
|
|
1085
|
+
// If a specific step is targeted for reset
|
|
1086
|
+
if (typeof directive.reset === 'object' && directive.reset.step) {
|
|
1087
|
+
updatedSession = (0, utils_1.enterStep)(updatedSession, directive.reset.step);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
// Update metadata
|
|
1092
|
+
updatedSession = {
|
|
1093
|
+
...updatedSession,
|
|
1094
|
+
metadata: {
|
|
1095
|
+
...updatedSession.metadata,
|
|
1096
|
+
lastUpdatedAt: now,
|
|
1097
|
+
},
|
|
1098
|
+
};
|
|
991
1099
|
return updatedSession;
|
|
992
1100
|
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Validate a directive for structural correctness.
|
|
1103
|
+
* Throws FlowConfigurationError for invalid combinations.
|
|
1104
|
+
* @private
|
|
1105
|
+
*/
|
|
1106
|
+
validateDirective(directive) {
|
|
1107
|
+
// Check for multiple position fields
|
|
1108
|
+
const positionFields = ['goTo', 'goToStep', 'complete', 'abort', 'reset'];
|
|
1109
|
+
const setPositionFields = positionFields.filter(field => directive[field] !== undefined);
|
|
1110
|
+
if (setPositionFields.length > 1) {
|
|
1111
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] Multiple position fields: a Directive may set at most one position field. ` +
|
|
1112
|
+
`Found: ${setPositionFields.join(', ')}. Remove all but one.`);
|
|
1113
|
+
}
|
|
1114
|
+
// Check for empty goTo object
|
|
1115
|
+
if (directive.goTo && typeof directive.goTo === 'object') {
|
|
1116
|
+
const goToObj = directive.goTo;
|
|
1117
|
+
if (!goToObj.flow && !goToObj.step) {
|
|
1118
|
+
throw new Step_1.FlowConfigurationError(`[FlowConfigurationError] Empty goTo: "goTo" requires at least a "flow" field. ` +
|
|
1119
|
+
`Provide { goTo: { flow: '<id>' } } or use the string shorthand { goTo: '<id>' }.`);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Strip PreDirective-only fields (appendPrompt, injectTools, halt) from a directive.
|
|
1125
|
+
* These fields are transient (one-turn lifetime) and must not be persisted.
|
|
1126
|
+
* @private
|
|
1127
|
+
*/
|
|
1128
|
+
stripPreDirectiveFields(directive) {
|
|
1129
|
+
const raw = directive;
|
|
1130
|
+
if (!raw.appendPrompt && !raw.injectTools && raw.halt === undefined) {
|
|
1131
|
+
return directive;
|
|
1132
|
+
}
|
|
1133
|
+
const { appendPrompt, injectTools, halt, ...rest } = raw;
|
|
1134
|
+
if (appendPrompt || injectTools || halt !== undefined) {
|
|
1135
|
+
utils_1.logger.debug(`[Agent] Stripped PreDirective-only fields before storing pendingDirective: ` +
|
|
1136
|
+
`${[appendPrompt && 'appendPrompt', injectTools && 'injectTools', halt !== undefined && 'halt'].filter(Boolean).join(', ')}`);
|
|
1137
|
+
}
|
|
1138
|
+
return rest;
|
|
1139
|
+
}
|
|
993
1140
|
/**
|
|
994
1141
|
* Simplified respond method using SessionManager
|
|
995
1142
|
* Automatically manages conversation history through the session
|