@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/src/core/Step.ts
CHANGED
|
@@ -5,19 +5,27 @@
|
|
|
5
5
|
import type {
|
|
6
6
|
StepRef,
|
|
7
7
|
StepOptions,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
BranchResult,
|
|
11
|
-
Guideline,
|
|
12
|
-
GuidelineMatch,
|
|
8
|
+
BranchMap,
|
|
9
|
+
Instruction,
|
|
13
10
|
Tool,
|
|
14
|
-
|
|
15
|
-
Event,
|
|
11
|
+
PrepareResult,
|
|
16
12
|
} from "../types";
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
13
|
+
import type { SessionState } from "../types/session";
|
|
14
|
+
import type { StepResult, BranchSpec, BranchResult } from "../types/flow";
|
|
15
|
+
import { ToolScope, Template, TemplateContext } from "../types";
|
|
16
|
+
import type { ConditionWhen, ConditionIf } from "../types/flow";
|
|
17
|
+
import { generateStepId, logger } from "../utils";
|
|
19
18
|
import { Agent } from './Agent'
|
|
20
|
-
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Error thrown when a step's configuration violates auto-step constraints.
|
|
22
|
+
*/
|
|
23
|
+
export class FlowConfigurationError extends Error {
|
|
24
|
+
constructor(message: string) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "FlowConfigurationError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
21
29
|
|
|
22
30
|
/**
|
|
23
31
|
* Represents a step within a route
|
|
@@ -25,50 +33,124 @@ import { END_ROUTE, END_ROUTE_ID } from "../constants";
|
|
|
25
33
|
export class Step<TContext = unknown, TData = unknown> {
|
|
26
34
|
public readonly id: string;
|
|
27
35
|
private nextSteps: Step<TContext, TData>[] = [];
|
|
28
|
-
private
|
|
29
|
-
public readonly
|
|
36
|
+
private _instructions: Instruction<TContext, TData>[] = [];
|
|
37
|
+
public readonly flowId: string;
|
|
30
38
|
public collect?: (keyof TData)[];
|
|
31
39
|
public description?: string;
|
|
32
|
-
public when?:
|
|
33
|
-
public
|
|
40
|
+
public when?: ConditionWhen;
|
|
41
|
+
public if?: ConditionIf<TContext, TData>;
|
|
42
|
+
public skip?: ConditionIf<TContext, TData>;
|
|
34
43
|
public requires?: (keyof TData)[];
|
|
35
44
|
public prompt?: Template<TContext, TData>;
|
|
45
|
+
public reply?: Template<TContext, TData>;
|
|
36
46
|
public prepare?:
|
|
37
47
|
| string
|
|
38
48
|
| Tool<TContext, TData>
|
|
39
|
-
| ((context: TContext, data?: Partial<TData>) => void | Promise<void>);
|
|
49
|
+
| ((context: TContext, data?: Partial<TData>) => void | PrepareResult | Promise<void | PrepareResult>);
|
|
40
50
|
public finalize?:
|
|
41
51
|
| string
|
|
42
52
|
| Tool<TContext, TData>
|
|
43
|
-
| ((context: TContext, data?: Partial<TData>) => void | Promise<void>);
|
|
53
|
+
| ((context: TContext, data?: Partial<TData>) => void | PrepareResult | Promise<void | PrepareResult>);
|
|
44
54
|
public tools?: (string | Tool<TContext, TData>)[];
|
|
45
55
|
|
|
56
|
+
private readonly _auto: boolean;
|
|
57
|
+
private readonly _branches?: BranchMap<TContext, TData>;
|
|
58
|
+
|
|
59
|
+
/** Whether this step runs without an LLM call (computational only). */
|
|
60
|
+
get auto(): boolean {
|
|
61
|
+
return this._auto;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Explicit source-local fork entries, if declared. */
|
|
65
|
+
get branches(): BranchMap<TContext, TData> | undefined {
|
|
66
|
+
return this._branches;
|
|
67
|
+
}
|
|
68
|
+
|
|
46
69
|
// Reference to parent agent for ToolManager access
|
|
47
|
-
private parentAgent?: Agent<TContext, TData>;
|
|
70
|
+
private parentAgent?: Agent<TContext, TData>;
|
|
48
71
|
|
|
49
72
|
constructor(
|
|
50
73
|
routeId: string,
|
|
51
74
|
options: StepOptions<TContext, TData> = {},
|
|
52
|
-
parentAgent?: Agent<TContext, TData>
|
|
75
|
+
parentAgent?: Agent<TContext, TData>
|
|
53
76
|
) {
|
|
54
77
|
// Use provided ID or generate a deterministic one
|
|
55
78
|
this.id = options.id || generateStepId(routeId, options.description);
|
|
56
|
-
this.
|
|
79
|
+
this.flowId = routeId;
|
|
57
80
|
this.description = options.description;
|
|
58
81
|
|
|
82
|
+
this._auto = options.auto ?? false;
|
|
83
|
+
this._branches = options.branches;
|
|
59
84
|
this.collect = options.collect;
|
|
60
|
-
this.
|
|
85
|
+
this.skip = options.skip;
|
|
61
86
|
this.requires = options.requires;
|
|
62
87
|
this.prompt = options.prompt;
|
|
88
|
+
this.reply = options.reply;
|
|
63
89
|
this.when = options.when;
|
|
90
|
+
this.if = options.if;
|
|
64
91
|
this.prepare = options.prepare;
|
|
65
92
|
this.finalize = options.finalize;
|
|
66
93
|
this.tools = options.tools;
|
|
67
94
|
|
|
68
|
-
//
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
95
|
+
// Validate when/if split: functions belong on `if`, not `when`
|
|
96
|
+
if (this.when !== undefined) {
|
|
97
|
+
const whenValue = this.when;
|
|
98
|
+
if (typeof whenValue === 'function') {
|
|
99
|
+
throw new FlowConfigurationError(
|
|
100
|
+
`[FlowConfigurationError] Step "${this.id}" has a function on "when": functions belong on "if" only. Move the function to the "if" field.`
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
if (Array.isArray(whenValue)) {
|
|
104
|
+
for (let i = 0; i < whenValue.length; i++) {
|
|
105
|
+
if (typeof whenValue[i] === 'function') {
|
|
106
|
+
throw new FlowConfigurationError(
|
|
107
|
+
`[FlowConfigurationError] Step "${this.id}" has a function at "when[${i}]": functions belong on "if" only. Move the function to the "if" field.`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Validate auto-step shape: auto-steps cannot define prompt, collect, tools, or finalize
|
|
115
|
+
if (this._auto) {
|
|
116
|
+
const violatingFields: string[] = [];
|
|
117
|
+
if (this.prompt != null) violatingFields.push('prompt');
|
|
118
|
+
if (this.collect != null && this.collect.length > 0) violatingFields.push('collect');
|
|
119
|
+
if (this.tools != null && this.tools.length > 0) violatingFields.push('tools');
|
|
120
|
+
if (this.finalize != null) violatingFields.push('finalize');
|
|
121
|
+
|
|
122
|
+
if (violatingFields.length > 0) {
|
|
123
|
+
throw new FlowConfigurationError(
|
|
124
|
+
`[FlowConfigurationError] Auto-step "${this.id}" cannot define: ${violatingFields.join(', ')}. Auto-steps run without an LLM call and must not declare prompt, collect, tools, or finalize. Remove these fields or set auto: false.`
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Validate reply-step shape: reply steps cannot define prompt, collect, tools, finalize, or auto: true
|
|
130
|
+
if (this.reply != null) {
|
|
131
|
+
const conflictingFields: string[] = [];
|
|
132
|
+
if (this.prompt != null) conflictingFields.push('prompt');
|
|
133
|
+
if (this.collect != null && this.collect.length > 0) conflictingFields.push('collect');
|
|
134
|
+
if (this.tools != null && this.tools.length > 0) conflictingFields.push('tools');
|
|
135
|
+
if (this.finalize != null) conflictingFields.push('finalize');
|
|
136
|
+
if (this._auto) conflictingFields.push('auto: true');
|
|
137
|
+
|
|
138
|
+
if (conflictingFields.length > 0) {
|
|
139
|
+
throw new FlowConfigurationError(
|
|
140
|
+
`[FlowConfigurationError] Step "${this.id}" sets "reply" together with conflicting fields: ${conflictingFields.join(', ')}. A reply step skips the LLM call and cannot define prompt, collect, tools, finalize, or auto: true.`
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Validate branches shape
|
|
146
|
+
if (this._branches !== undefined) {
|
|
147
|
+
this.validateBranches(this._branches);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Initialize instructions from options
|
|
151
|
+
if (options.instructions) {
|
|
152
|
+
options.instructions.forEach((instruction) => {
|
|
153
|
+
this.addInstruction(instruction);
|
|
72
154
|
});
|
|
73
155
|
}
|
|
74
156
|
|
|
@@ -83,7 +165,7 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
83
165
|
configure(config: {
|
|
84
166
|
description?: string;
|
|
85
167
|
collect?: (keyof TData)[];
|
|
86
|
-
|
|
168
|
+
skip?: ConditionIf<TContext, TData>;
|
|
87
169
|
requires?: (keyof TData)[];
|
|
88
170
|
prompt?: Template<TContext, TData>;
|
|
89
171
|
prepare?:
|
|
@@ -104,8 +186,8 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
104
186
|
this.collect = config.collect;
|
|
105
187
|
}
|
|
106
188
|
|
|
107
|
-
if (config.
|
|
108
|
-
this.
|
|
189
|
+
if (config.skip !== undefined) {
|
|
190
|
+
this.skip = config.skip;
|
|
109
191
|
}
|
|
110
192
|
|
|
111
193
|
if (config.requires !== undefined) {
|
|
@@ -127,21 +209,6 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
127
209
|
return this;
|
|
128
210
|
}
|
|
129
211
|
|
|
130
|
-
/**
|
|
131
|
-
* Shortcut to end the current route
|
|
132
|
-
*
|
|
133
|
-
* @param options - Optional step options for the end step
|
|
134
|
-
* @returns Terminal step result
|
|
135
|
-
*/
|
|
136
|
-
endRoute(
|
|
137
|
-
options: Omit<StepOptions<TContext, TData>, "step"> = {}
|
|
138
|
-
): StepResult<TContext, TData> {
|
|
139
|
-
return this.nextStep({
|
|
140
|
-
...options,
|
|
141
|
-
step: END_ROUTE,
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
212
|
/**
|
|
146
213
|
* Create a transition from this step to another
|
|
147
214
|
*
|
|
@@ -149,25 +216,8 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
149
216
|
* @returns StepResult that supports chaining
|
|
150
217
|
*/
|
|
151
218
|
nextStep(spec: StepOptions<TContext, TData>): StepResult<TContext, TData> {
|
|
152
|
-
// Handle END_ROUTE
|
|
153
|
-
if (spec.step && typeof spec.step === "symbol" && spec.step === END_ROUTE) {
|
|
154
|
-
const endStep = new Step<TContext, TData>(this.routeId, {
|
|
155
|
-
...spec,
|
|
156
|
-
id: END_ROUTE_ID,
|
|
157
|
-
}, this.parentAgent);
|
|
158
|
-
this.nextSteps.push(endStep);
|
|
159
|
-
return this.createTerminalRef();
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Handle direct step reference
|
|
163
|
-
if (spec.step && typeof spec.step !== "symbol") {
|
|
164
|
-
// This is a bit tricky. We need to find the actual Step instance.
|
|
165
|
-
// For now, let's assume the user will provide a Step instance directly.
|
|
166
|
-
// This part might need to be revisited.
|
|
167
|
-
}
|
|
168
|
-
|
|
169
219
|
// Create new target step for prompt or tool
|
|
170
|
-
const targetStep = new Step<TContext, TData>(this.
|
|
220
|
+
const targetStep = new Step<TContext, TData>(this.flowId, spec, this.parentAgent);
|
|
171
221
|
this.nextSteps.push(targetStep);
|
|
172
222
|
|
|
173
223
|
return this.createStepRefWithTransition(targetStep.getRef(), targetStep);
|
|
@@ -191,7 +241,7 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
191
241
|
: branchSpec.step;
|
|
192
242
|
|
|
193
243
|
// Create a new step for this branch
|
|
194
|
-
const branchStep = new Step<TContext, TData>(this.
|
|
244
|
+
const branchStep = new Step<TContext, TData>(this.flowId, stepOptions, this.parentAgent);
|
|
195
245
|
// Add it to our transitions
|
|
196
246
|
this.nextSteps.push(branchStep);
|
|
197
247
|
// Create a step result for chaining
|
|
@@ -206,66 +256,22 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
206
256
|
}
|
|
207
257
|
|
|
208
258
|
/**
|
|
209
|
-
* Add
|
|
210
|
-
*/
|
|
211
|
-
addGuideline(guideline: Guideline<TContext, TData>): void {
|
|
212
|
-
this.guidelines.push(guideline);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Get guidelines for this step
|
|
259
|
+
* Add an instruction to this step.
|
|
217
260
|
*/
|
|
218
|
-
|
|
219
|
-
|
|
261
|
+
addInstruction(instruction: Instruction<TContext, TData>): void {
|
|
262
|
+
this._instructions.push({
|
|
263
|
+
...instruction,
|
|
264
|
+
kind: instruction.kind || 'should' as const,
|
|
265
|
+
id: instruction.id || `instruction_${this.id}_${this._instructions.length}`,
|
|
266
|
+
enabled: instruction.enabled !== false, // Default to true
|
|
267
|
+
});
|
|
220
268
|
}
|
|
221
269
|
|
|
222
270
|
/**
|
|
223
|
-
*
|
|
224
|
-
* Returns guidelines that should be active given the current context
|
|
271
|
+
* Get instructions for this step
|
|
225
272
|
*/
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
session?: SessionState<TData>,
|
|
229
|
-
history?: Event[]
|
|
230
|
-
): Promise<GuidelineMatch<TContext, TData>[]> {
|
|
231
|
-
const templateContext = { context, session, history, data: session?.data || {} };
|
|
232
|
-
const evaluator = createConditionEvaluator(templateContext);
|
|
233
|
-
const matches: GuidelineMatch<TContext, TData>[] = [];
|
|
234
|
-
|
|
235
|
-
for (const guideline of this.guidelines) {
|
|
236
|
-
// Skip disabled guidelines
|
|
237
|
-
if (guideline.enabled === false) {
|
|
238
|
-
continue;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (guideline.condition) {
|
|
242
|
-
const evaluation = await evaluator.evaluateCondition(guideline.condition, 'AND');
|
|
243
|
-
|
|
244
|
-
// Include guideline if:
|
|
245
|
-
// 1. No programmatic conditions (only strings) - always active
|
|
246
|
-
// 2. Programmatic conditions evaluate to true
|
|
247
|
-
if (!evaluation.hasProgrammaticConditions || evaluation.programmaticResult) {
|
|
248
|
-
const rationale = evaluation.aiContextStrings.length > 0
|
|
249
|
-
? `Condition met: ${evaluation.aiContextStrings.join(" AND ")}`
|
|
250
|
-
: evaluation.hasProgrammaticConditions
|
|
251
|
-
? "Programmatic condition evaluated to true"
|
|
252
|
-
: "Always active (no conditions)";
|
|
253
|
-
|
|
254
|
-
matches.push({
|
|
255
|
-
guideline,
|
|
256
|
-
rationale
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
} else {
|
|
260
|
-
// No condition means always active
|
|
261
|
-
matches.push({
|
|
262
|
-
guideline,
|
|
263
|
-
rationale: "Always active (no conditions)"
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
return matches;
|
|
273
|
+
getInstructions(): Instruction<TContext, TData>[] {
|
|
274
|
+
return [...this._instructions];
|
|
269
275
|
}
|
|
270
276
|
|
|
271
277
|
/**
|
|
@@ -356,7 +362,7 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
356
362
|
if (!this.parentAgent?.tool) {
|
|
357
363
|
// Fallback to local resolution if no ToolManager available
|
|
358
364
|
const resolved = this.resolveTools();
|
|
359
|
-
return resolved.find(tool => tool.id === toolId || tool.
|
|
365
|
+
return resolved.find(tool => tool.id === toolId || tool.id === toolId);
|
|
360
366
|
}
|
|
361
367
|
|
|
362
368
|
// Use ToolManager to find the tool with proper scope resolution
|
|
@@ -419,7 +425,7 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
419
425
|
const resolvedIds = resolved.map(tool => tool.id);
|
|
420
426
|
const missing = toolIds.filter(id => !resolvedIds.includes(id));
|
|
421
427
|
const found = toolIds.filter(id => resolvedIds.includes(id));
|
|
422
|
-
|
|
428
|
+
|
|
423
429
|
return {
|
|
424
430
|
valid: missing.length === 0,
|
|
425
431
|
missing,
|
|
@@ -440,58 +446,123 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
440
446
|
}
|
|
441
447
|
|
|
442
448
|
/**
|
|
443
|
-
* Evaluate when
|
|
449
|
+
* Evaluate when/if conditions using the v2 split logic.
|
|
450
|
+
* `if` (code predicate) evaluates first (free); `when` (AI) evaluates only when `if` passes.
|
|
451
|
+
* Both are combined with AND semantics.
|
|
444
452
|
*/
|
|
445
453
|
async evaluateWhen(
|
|
446
454
|
templateContext: TemplateContext<TContext, TData>
|
|
447
|
-
): Promise<{
|
|
448
|
-
shouldActivate: boolean;
|
|
455
|
+
): Promise<{
|
|
456
|
+
shouldActivate: boolean;
|
|
449
457
|
aiContextStrings: string[];
|
|
450
458
|
hasProgrammaticConditions: boolean;
|
|
451
459
|
}> {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
460
|
+
// If neither `when` nor `if` is set, step is always eligible
|
|
461
|
+
if (!this.when && !this.if) {
|
|
462
|
+
return {
|
|
463
|
+
shouldActivate: true,
|
|
464
|
+
aiContextStrings: [],
|
|
465
|
+
hasProgrammaticConditions: false
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// Evaluate `if` first (free, code-only)
|
|
470
|
+
if (this.if) {
|
|
471
|
+
const predicates = Array.isArray(this.if) ? this.if : [this.if];
|
|
472
|
+
for (const predicate of predicates) {
|
|
473
|
+
try {
|
|
474
|
+
const result = await predicate({
|
|
475
|
+
data: templateContext.data,
|
|
476
|
+
context: templateContext.context as TContext,
|
|
477
|
+
session: templateContext.session as SessionState<TData>,
|
|
478
|
+
history: templateContext.history || [],
|
|
479
|
+
});
|
|
480
|
+
if (!result) {
|
|
481
|
+
// `if` failed — short-circuit, don't bother with `when`
|
|
482
|
+
return {
|
|
483
|
+
shouldActivate: false,
|
|
484
|
+
aiContextStrings: [],
|
|
485
|
+
hasProgrammaticConditions: true
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
} catch (error) {
|
|
489
|
+
logger.warn(`[Step] "if" predicate failed for step "${this.id}":`, error);
|
|
490
|
+
return {
|
|
491
|
+
shouldActivate: false,
|
|
492
|
+
aiContextStrings: [],
|
|
493
|
+
hasProgrammaticConditions: true
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// `if` passed (or was absent) — now evaluate `when` (AI-evaluated strings)
|
|
500
|
+
if (this.when) {
|
|
501
|
+
const whenStrings = Array.isArray(this.when) ? this.when : [this.when];
|
|
502
|
+
// `when` strings are handed to the AI — return them as aiContextStrings
|
|
503
|
+
// The programmatic result is true (strings don't fail programmatically;
|
|
504
|
+
// they're scored by the AI at routing time)
|
|
505
|
+
return {
|
|
506
|
+
shouldActivate: true,
|
|
507
|
+
aiContextStrings: whenStrings,
|
|
508
|
+
hasProgrammaticConditions: !!this.if
|
|
457
509
|
};
|
|
458
510
|
}
|
|
459
511
|
|
|
460
|
-
|
|
461
|
-
const result = await evaluator.evaluateCondition(this.when, 'AND');
|
|
462
|
-
|
|
512
|
+
// Only `if` was set and it passed
|
|
463
513
|
return {
|
|
464
|
-
shouldActivate:
|
|
465
|
-
aiContextStrings:
|
|
466
|
-
hasProgrammaticConditions:
|
|
514
|
+
shouldActivate: true,
|
|
515
|
+
aiContextStrings: [],
|
|
516
|
+
hasProgrammaticConditions: true
|
|
467
517
|
};
|
|
468
518
|
}
|
|
469
519
|
|
|
470
520
|
/**
|
|
471
|
-
* Evaluate
|
|
521
|
+
* Evaluate the skip condition (if-only shape — code predicates, OR semantics).
|
|
522
|
+
* Returns true if the step should be skipped.
|
|
472
523
|
*/
|
|
473
|
-
async
|
|
524
|
+
async evaluateSkip(
|
|
474
525
|
templateContext: TemplateContext<TContext, TData>
|
|
475
|
-
): Promise<{
|
|
476
|
-
shouldSkip: boolean;
|
|
526
|
+
): Promise<{
|
|
527
|
+
shouldSkip: boolean;
|
|
477
528
|
aiContextStrings: string[];
|
|
478
529
|
hasProgrammaticConditions: boolean;
|
|
479
530
|
}> {
|
|
480
|
-
if (!this.
|
|
481
|
-
return {
|
|
482
|
-
shouldSkip: false,
|
|
483
|
-
aiContextStrings: [],
|
|
484
|
-
hasProgrammaticConditions: false
|
|
531
|
+
if (!this.skip) {
|
|
532
|
+
return {
|
|
533
|
+
shouldSkip: false,
|
|
534
|
+
aiContextStrings: [],
|
|
535
|
+
hasProgrammaticConditions: false
|
|
485
536
|
};
|
|
486
537
|
}
|
|
487
538
|
|
|
488
|
-
const
|
|
489
|
-
|
|
490
|
-
|
|
539
|
+
const predicates = Array.isArray(this.skip) ? this.skip : [this.skip];
|
|
540
|
+
// OR semantics: if ANY predicate returns true, skip
|
|
541
|
+
for (const predicate of predicates) {
|
|
542
|
+
try {
|
|
543
|
+
const result = await predicate({
|
|
544
|
+
data: templateContext.data,
|
|
545
|
+
context: templateContext.context as TContext,
|
|
546
|
+
session: templateContext.session as SessionState<TData>,
|
|
547
|
+
history: templateContext.history || [],
|
|
548
|
+
});
|
|
549
|
+
if (result) {
|
|
550
|
+
return {
|
|
551
|
+
shouldSkip: true,
|
|
552
|
+
aiContextStrings: [],
|
|
553
|
+
hasProgrammaticConditions: true
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
} catch (error) {
|
|
557
|
+
logger.warn(`[Step] "skip" predicate failed for step "${this.id}":`, error);
|
|
558
|
+
// On error, default to not skipping (safe fallback)
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
491
562
|
return {
|
|
492
|
-
shouldSkip:
|
|
493
|
-
aiContextStrings:
|
|
494
|
-
hasProgrammaticConditions:
|
|
563
|
+
shouldSkip: false,
|
|
564
|
+
aiContextStrings: [],
|
|
565
|
+
hasProgrammaticConditions: true
|
|
495
566
|
};
|
|
496
567
|
}
|
|
497
568
|
|
|
@@ -511,7 +582,7 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
511
582
|
getRef(): StepRef {
|
|
512
583
|
return {
|
|
513
584
|
id: this.id,
|
|
514
|
-
|
|
585
|
+
flowId: this.flowId,
|
|
515
586
|
};
|
|
516
587
|
}
|
|
517
588
|
|
|
@@ -530,30 +601,6 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
530
601
|
stepInstance.nextStep(spec),
|
|
531
602
|
branch: (branches: BranchSpec<TContext, TData>[]) =>
|
|
532
603
|
stepInstance.branch(branches),
|
|
533
|
-
endRoute: (options?: Omit<StepOptions<TContext, TData>, "step">) =>
|
|
534
|
-
stepInstance.endRoute(options),
|
|
535
|
-
};
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
/**
|
|
539
|
-
* Create a terminal step reference (for END_ROUTE)
|
|
540
|
-
*/
|
|
541
|
-
private createTerminalRef(): StepResult<TContext, TData> {
|
|
542
|
-
const terminalRef: StepRef = {
|
|
543
|
-
id: END_ROUTE_ID,
|
|
544
|
-
routeId: this.routeId,
|
|
545
|
-
};
|
|
546
|
-
|
|
547
|
-
return {
|
|
548
|
-
...terminalRef,
|
|
549
|
-
nextStep: () => {
|
|
550
|
-
throw new Error("Cannot transition from END_ROUTE step");
|
|
551
|
-
},
|
|
552
|
-
branch: () => {
|
|
553
|
-
throw new Error("Cannot branch from END_ROUTE step");
|
|
554
|
-
},
|
|
555
|
-
endRoute: (options?: Omit<StepOptions<TContext, TData>, "step">) =>
|
|
556
|
-
this.endRoute(options),
|
|
557
604
|
};
|
|
558
605
|
}
|
|
559
606
|
|
|
@@ -566,8 +613,6 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
566
613
|
nextStep: (spec: StepOptions<TContext, TData>) => this.nextStep(spec),
|
|
567
614
|
branch: (branches: BranchSpec<TContext, TData>[]) =>
|
|
568
615
|
this.branch(branches),
|
|
569
|
-
endRoute: (options?: Omit<StepOptions<TContext, TData>, "step">) =>
|
|
570
|
-
this.endRoute(options),
|
|
571
616
|
};
|
|
572
617
|
}
|
|
573
618
|
|
|
@@ -579,15 +624,73 @@ export class Step<TContext = unknown, TData = unknown> {
|
|
|
579
624
|
return {
|
|
580
625
|
id: this.id,
|
|
581
626
|
description: this.description,
|
|
627
|
+
auto: this._auto,
|
|
628
|
+
branches: this._branches,
|
|
582
629
|
prompt: this.prompt,
|
|
630
|
+
reply: this.reply,
|
|
583
631
|
tools: this.tools,
|
|
584
632
|
prepare: this.prepare,
|
|
585
633
|
finalize: this.finalize,
|
|
586
634
|
collect: this.collect,
|
|
587
|
-
|
|
635
|
+
skip: this.skip,
|
|
588
636
|
requires: this.requires,
|
|
589
637
|
when: this.when,
|
|
590
|
-
|
|
638
|
+
if: this.if,
|
|
639
|
+
instructions: this.getInstructions(),
|
|
591
640
|
};
|
|
592
641
|
}
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Validate the branches array at construction time.
|
|
645
|
+
* Checks:
|
|
646
|
+
* - Non-empty array
|
|
647
|
+
* - Unconditional entries (no `when` and no `if`) only legal as the last entry
|
|
648
|
+
* - Directive `then` values: at most one position field, no empty `goTo: {}`
|
|
649
|
+
*/
|
|
650
|
+
private validateBranches(branches: BranchMap<TContext, TData>): void {
|
|
651
|
+
if (branches.length === 0) {
|
|
652
|
+
throw new FlowConfigurationError(
|
|
653
|
+
`[FlowConfigurationError] Empty branches array on step "${this.id}": branches must contain at least one entry. Add branch entries or remove the branches field.`
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
const POSITION_FIELDS = ['goTo', 'goToStep', 'complete', 'abort', 'reset'] as const;
|
|
658
|
+
|
|
659
|
+
for (let i = 0; i < branches.length; i++) {
|
|
660
|
+
const entry = branches[i];
|
|
661
|
+
const isLast = i === branches.length - 1;
|
|
662
|
+
|
|
663
|
+
// Non-last entry without `when` or `if` is dead code — later entries are unreachable
|
|
664
|
+
if (!isLast && !entry.when && !entry.if) {
|
|
665
|
+
throw new FlowConfigurationError(
|
|
666
|
+
`[FlowConfigurationError] Dead-code branch at index ${i}: branches[${i}] has neither "when" nor "if" and is not the last entry. Entries after index ${i} are unreachable. Move the unconditional entry to the end or add a condition.`
|
|
667
|
+
);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// Validate Directive `then` values
|
|
671
|
+
if (entry.then && typeof entry.then === 'object') {
|
|
672
|
+
const directive = entry.then;
|
|
673
|
+
|
|
674
|
+
// Check for multiple position fields
|
|
675
|
+
const setPositionFields = POSITION_FIELDS.filter(
|
|
676
|
+
(field) => directive[field] !== undefined
|
|
677
|
+
);
|
|
678
|
+
if (setPositionFields.length > 1) {
|
|
679
|
+
throw new FlowConfigurationError(
|
|
680
|
+
`[FlowConfigurationError] Multiple position fields in branches[${i}].then: Directive sets ${setPositionFields.join(', ')}. At most one position field is allowed per Directive. Remove all but one.`
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// Check for empty goTo: {}
|
|
685
|
+
if (directive.goTo !== undefined && typeof directive.goTo === 'object' && directive.goTo !== null) {
|
|
686
|
+
const goToObj = directive.goTo as Record<string, unknown>;
|
|
687
|
+
if (Object.keys(goToObj).length === 0) {
|
|
688
|
+
throw new FlowConfigurationError(
|
|
689
|
+
`[FlowConfigurationError] Empty goTo in branches[${i}].then: Directive has "goTo: {}" with no flow target. Provide at least a flow id or title.`
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
593
696
|
}
|
|
@@ -16,7 +16,7 @@ import type {
|
|
|
16
16
|
ToolExecutionUpdate,
|
|
17
17
|
ToolExecutionResult,
|
|
18
18
|
ToolContext,
|
|
19
|
-
|
|
19
|
+
Tool,
|
|
20
20
|
TrackedTool,
|
|
21
21
|
} from "../types/tool";
|
|
22
22
|
|
|
@@ -116,7 +116,7 @@ export class StreamingToolExecutor<TContext = unknown, TData = unknown> {
|
|
|
116
116
|
*/
|
|
117
117
|
addTool(
|
|
118
118
|
toolCall: ToolCallRequest,
|
|
119
|
-
tool:
|
|
119
|
+
tool: Tool<TContext, TData>
|
|
120
120
|
): void {
|
|
121
121
|
if (this.discarded) {
|
|
122
122
|
log.warn(`[StreamingToolExecutor] Executor discarded, ignoring tool: ${toolCall.toolName}`);
|
|
@@ -312,7 +312,7 @@ export class StreamingToolExecutor<TContext = unknown, TData = unknown> {
|
|
|
312
312
|
*/
|
|
313
313
|
private normalizeResult(
|
|
314
314
|
result: unknown,
|
|
315
|
-
tool:
|
|
315
|
+
tool: Tool<TContext, TData>
|
|
316
316
|
): ToolExecutionResult {
|
|
317
317
|
if (
|
|
318
318
|
result &&
|
|
@@ -346,7 +346,7 @@ export class StreamingToolExecutor<TContext = unknown, TData = unknown> {
|
|
|
346
346
|
*/
|
|
347
347
|
private applyResultTruncation(
|
|
348
348
|
result: ToolExecutionResult,
|
|
349
|
-
tool:
|
|
349
|
+
tool: Tool<TContext, TData>
|
|
350
350
|
): ToolExecutionResult {
|
|
351
351
|
const maxChars = tool.maxResultSizeChars;
|
|
352
352
|
if (maxChars == null || maxChars <= 0 || result.data == null) {
|