@falai/agent 1.2.8 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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/providers/AnthropicProvider.d.ts +1 -1
- package/dist/cjs/providers/AnthropicProvider.js +1 -1
- package/dist/cjs/providers/GeminiProvider.d.ts +1 -1
- package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/cjs/providers/GeminiProvider.js +1 -1
- package/dist/cjs/providers/GeminiProvider.js.map +1 -1
- package/dist/cjs/providers/OpenAIProvider.d.ts +1 -1
- package/dist/cjs/providers/OpenAIProvider.d.ts.map +1 -1
- package/dist/cjs/providers/OpenAIProvider.js +1 -1
- package/dist/cjs/providers/OpenAIProvider.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/providers/AnthropicProvider.d.ts +1 -1
- package/dist/providers/AnthropicProvider.js +1 -1
- package/dist/providers/GeminiProvider.d.ts +1 -1
- package/dist/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/providers/GeminiProvider.js +1 -1
- package/dist/providers/GeminiProvider.js.map +1 -1
- package/dist/providers/OpenAIProvider.d.ts +1 -1
- package/dist/providers/OpenAIProvider.d.ts.map +1 -1
- package/dist/providers/OpenAIProvider.js +1 -1
- package/dist/providers/OpenAIProvider.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 +22 -200
- 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 +14 -0
- package/docs/migration/route-to-flow.md +561 -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/providers/AnthropicProvider.ts +2 -2
- package/src/providers/GeminiProvider.ts +2 -2
- package/src/providers/OpenAIProvider.ts +2 -2
- 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
|
@@ -1,772 +0,0 @@
|
|
|
1
|
-
# Agent-Level Schema-Driven Data Collection
|
|
2
|
-
|
|
3
|
-
@falai/agent implements a powerful agent-level schema-first approach to data collection, enabling type-safe, structured information extraction from natural conversations across all routes. Unlike traditional route-specific data collection, this system centralizes data schemas at the agent level while allowing routes to specify completion requirements.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
The agent-level data collection system provides:
|
|
8
|
-
|
|
9
|
-
- **Centralized JSON Schema**: Define comprehensive data structures at the agent level
|
|
10
|
-
- **Cross-Route Data Sharing**: Data collected by any route is available to all routes
|
|
11
|
-
- **Route Completion Logic**: Routes complete when step flows reach `END_ROUTE`, with required fields acting as validation gates along the way
|
|
12
|
-
- **Type-Safe Extraction**: Automatic mapping from AI responses to typed data
|
|
13
|
-
- **Natural Conversations**: AI handles information gathering conversationally
|
|
14
|
-
- **Validation & Enrichment**: Agent-level lifecycle hooks for data processing
|
|
15
|
-
- **Session Persistence**: Data survives across conversation turns and route transitions
|
|
16
|
-
- **Batch Data Collection**: Multiple steps can collect data in a single LLM call
|
|
17
|
-
|
|
18
|
-
## Data Collection Across Batched Steps
|
|
19
|
-
|
|
20
|
-
When multiple steps execute in a single batch (requires `maxStepsPerBatch` > 1), data collection works across all steps simultaneously.
|
|
21
|
-
|
|
22
|
-
### How Batch Data Collection Works
|
|
23
|
-
|
|
24
|
-
1. **Gather collect fields** - All `collect` fields from all steps in the batch are combined
|
|
25
|
-
2. **Single LLM call** - The combined prompt instructs the LLM to extract all fields
|
|
26
|
-
3. **Extract from response** - All specified fields are extracted from the LLM response
|
|
27
|
-
4. **Validate against schema** - Collected data is validated against the agent schema
|
|
28
|
-
5. **Update session** - All collected values are merged into session data
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
// Batch with 3 steps, each collecting different fields
|
|
32
|
-
const batch = [
|
|
33
|
-
{ collect: ["name"] },
|
|
34
|
-
{ collect: ["email", "phone"] },
|
|
35
|
-
{ collect: ["preferences"] }
|
|
36
|
-
];
|
|
37
|
-
|
|
38
|
-
// Combined collection: ["name", "email", "phone", "preferences"]
|
|
39
|
-
// Single LLM response extracts all fields at once
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
### Pre-Extraction and Batch Determination
|
|
43
|
-
|
|
44
|
-
Pre-extraction happens **before** batch determination and directly impacts which steps can be batched:
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
// User message: "I'm John, email john@example.com, I prefer dark mode"
|
|
48
|
-
|
|
49
|
-
// Phase 1: Pre-extraction
|
|
50
|
-
const preExtracted = {
|
|
51
|
-
name: "John",
|
|
52
|
-
email: "john@example.com",
|
|
53
|
-
preferences: { theme: "dark" }
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
// Phase 2: Batch determination (with pre-extracted data merged)
|
|
57
|
-
// Step 1: collect: ["name"] → name exists → doesn't need input
|
|
58
|
-
// Step 2: collect: ["email"] → email exists → doesn't need input
|
|
59
|
-
// Step 3: collect: ["preferences"] → preferences exists → doesn't need input
|
|
60
|
-
// Result: All 3 steps batched together
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
### Pre-Extraction Configuration
|
|
64
|
-
|
|
65
|
-
Pre-extraction is automatic when routes define data fields:
|
|
66
|
-
|
|
67
|
-
```typescript
|
|
68
|
-
// Option 1: Route-level required fields
|
|
69
|
-
agent.createRoute({
|
|
70
|
-
title: "Booking",
|
|
71
|
-
requiredFields: ["hotel", "date", "guests"],
|
|
72
|
-
// Pre-extraction enabled automatically
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
// Option 2: Route-level optional fields
|
|
76
|
-
agent.createRoute({
|
|
77
|
-
title: "Booking",
|
|
78
|
-
optionalFields: ["specialRequests"],
|
|
79
|
-
// Pre-extraction enabled automatically
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// Option 3: Steps with collect arrays
|
|
83
|
-
agent.createRoute({
|
|
84
|
-
title: "Booking",
|
|
85
|
-
steps: [
|
|
86
|
-
{ collect: ["hotel"] }, // Pre-extraction enabled automatically
|
|
87
|
-
]
|
|
88
|
-
});
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### Batch Collection Example
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
const response = await agent.respond(
|
|
95
|
-
"I'm Alice, alice@example.com, and I want to book for 2 guests"
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
// Response includes all collected data
|
|
99
|
-
console.log(response.session.data);
|
|
100
|
-
// {
|
|
101
|
-
// name: "Alice",
|
|
102
|
-
// email: "alice@example.com",
|
|
103
|
-
// guests: 2
|
|
104
|
-
// }
|
|
105
|
-
|
|
106
|
-
// Shows which steps executed
|
|
107
|
-
console.log(response.executedSteps);
|
|
108
|
-
// [
|
|
109
|
-
// { id: "ask-name", routeId: "booking" },
|
|
110
|
-
// { id: "ask-email", routeId: "booking" },
|
|
111
|
-
// { id: "ask-guests", routeId: "booking" }
|
|
112
|
-
// ]
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
### Validation Across Batch
|
|
116
|
-
|
|
117
|
-
Data validation happens after collection for all fields:
|
|
118
|
-
|
|
119
|
-
```typescript
|
|
120
|
-
// Agent schema defines validation rules
|
|
121
|
-
const agent = new Agent({
|
|
122
|
-
schema: {
|
|
123
|
-
type: "object",
|
|
124
|
-
properties: {
|
|
125
|
-
email: { type: "string", format: "email" },
|
|
126
|
-
guests: { type: "number", minimum: 1, maximum: 10 }
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// If validation fails for any field:
|
|
132
|
-
const response = await agent.respond("Book for 100 guests");
|
|
133
|
-
|
|
134
|
-
// Response includes validation errors
|
|
135
|
-
if (response.stoppedReason === 'validation_error') {
|
|
136
|
-
console.log(response.error);
|
|
137
|
-
// {
|
|
138
|
-
// type: 'data_validation',
|
|
139
|
-
// message: 'Validation failed for 1 field(s): guests',
|
|
140
|
-
// details: [{ field: 'guests', message: 'Value exceeds maximum of 10' }]
|
|
141
|
-
// }
|
|
142
|
-
}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
### Partial Data Preservation
|
|
146
|
-
|
|
147
|
-
Even when validation fails, valid partial data is preserved:
|
|
148
|
-
|
|
149
|
-
```typescript
|
|
150
|
-
// User provides valid name but invalid email
|
|
151
|
-
const response = await agent.respond("I'm John, email: not-an-email");
|
|
152
|
-
|
|
153
|
-
// Valid data is still collected
|
|
154
|
-
console.log(response.session.data.name); // "John"
|
|
155
|
-
|
|
156
|
-
// Invalid data triggers validation error
|
|
157
|
-
console.log(response.stoppedReason); // "validation_error"
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
## Agent-Level Schema Definition
|
|
161
|
-
|
|
162
|
-
### Centralized Schema
|
|
163
|
-
|
|
164
|
-
```typescript
|
|
165
|
-
interface UserProfile {
|
|
166
|
-
name: string;
|
|
167
|
-
email: string;
|
|
168
|
-
age: number;
|
|
169
|
-
interests: string[];
|
|
170
|
-
preferences: {
|
|
171
|
-
notifications: boolean;
|
|
172
|
-
theme: "light" | "dark";
|
|
173
|
-
};
|
|
174
|
-
// Additional fields for other routes
|
|
175
|
-
supportTicketId?: string;
|
|
176
|
-
issueType?: string;
|
|
177
|
-
feedbackRating?: number;
|
|
178
|
-
subscriptionTier?: "free" | "premium" | "enterprise";
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Define schema at agent level
|
|
182
|
-
const agent = new Agent<{}, UserProfile>({
|
|
183
|
-
name: "User Management Agent",
|
|
184
|
-
provider: new OpenAIProvider({ apiKey: process.env.OPENAI_API_KEY }),
|
|
185
|
-
|
|
186
|
-
// Comprehensive agent-level schema
|
|
187
|
-
schema: {
|
|
188
|
-
type: "object",
|
|
189
|
-
properties: {
|
|
190
|
-
name: {
|
|
191
|
-
type: "string",
|
|
192
|
-
description: "The user's full name",
|
|
193
|
-
},
|
|
194
|
-
email: {
|
|
195
|
-
type: "string",
|
|
196
|
-
format: "email",
|
|
197
|
-
description: "Valid email address",
|
|
198
|
-
},
|
|
199
|
-
age: {
|
|
200
|
-
type: "number",
|
|
201
|
-
minimum: 13,
|
|
202
|
-
maximum: 120,
|
|
203
|
-
description: "Age in years",
|
|
204
|
-
},
|
|
205
|
-
interests: {
|
|
206
|
-
type: "array",
|
|
207
|
-
items: { type: "string" },
|
|
208
|
-
description: "List of user interests",
|
|
209
|
-
},
|
|
210
|
-
preferences: {
|
|
211
|
-
type: "object",
|
|
212
|
-
properties: {
|
|
213
|
-
notifications: { type: "boolean" },
|
|
214
|
-
theme: {
|
|
215
|
-
type: "string",
|
|
216
|
-
enum: ["light", "dark"],
|
|
217
|
-
},
|
|
218
|
-
},
|
|
219
|
-
},
|
|
220
|
-
supportTicketId: { type: "string" },
|
|
221
|
-
issueType: { type: "string" },
|
|
222
|
-
feedbackRating: { type: "number", minimum: 1, maximum: 5 },
|
|
223
|
-
subscriptionTier: { type: "string", enum: ["free", "premium", "enterprise"] }
|
|
224
|
-
},
|
|
225
|
-
required: ["name", "email"],
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
// Routes specify required fields instead of schemas
|
|
230
|
-
const profileRoute = agent.createRoute({
|
|
231
|
-
title: "User Profile Collection",
|
|
232
|
-
requiredFields: ["name", "email", "age"],
|
|
233
|
-
optionalFields: ["interests", "preferences"]
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
const supportRoute = agent.createRoute({
|
|
237
|
-
title: "Support Ticket",
|
|
238
|
-
requiredFields: ["name", "email", "issueType"],
|
|
239
|
-
optionalFields: ["supportTicketId"]
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
const feedbackRoute = agent.createRoute({
|
|
243
|
-
title: "Feedback Collection",
|
|
244
|
-
requiredFields: ["name", "email", "feedbackRating"],
|
|
245
|
-
optionalFields: ["subscriptionTier"]
|
|
246
|
-
});
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
### Advanced Schema Features
|
|
250
|
-
|
|
251
|
-
```typescript
|
|
252
|
-
const complexSchema = {
|
|
253
|
-
type: "object",
|
|
254
|
-
properties: {
|
|
255
|
-
// Basic types
|
|
256
|
-
name: { type: "string", minLength: 2, maxLength: 100 },
|
|
257
|
-
|
|
258
|
-
// Email validation
|
|
259
|
-
email: { type: "string", format: "email" },
|
|
260
|
-
|
|
261
|
-
// Number constraints
|
|
262
|
-
age: { type: "number", minimum: 0, maximum: 150 },
|
|
263
|
-
|
|
264
|
-
// Arrays with constraints
|
|
265
|
-
skills: {
|
|
266
|
-
type: "array",
|
|
267
|
-
items: { type: "string" },
|
|
268
|
-
minItems: 1,
|
|
269
|
-
maxItems: 10,
|
|
270
|
-
uniqueItems: true,
|
|
271
|
-
},
|
|
272
|
-
|
|
273
|
-
// Nested objects
|
|
274
|
-
address: {
|
|
275
|
-
type: "object",
|
|
276
|
-
properties: {
|
|
277
|
-
street: { type: "string" },
|
|
278
|
-
city: { type: "string" },
|
|
279
|
-
zipCode: { type: "string", pattern: "^\\d{5}(-\\d{4})?$" },
|
|
280
|
-
},
|
|
281
|
-
required: ["street", "city"],
|
|
282
|
-
},
|
|
283
|
-
|
|
284
|
-
// Enums
|
|
285
|
-
priority: {
|
|
286
|
-
type: "string",
|
|
287
|
-
enum: ["low", "medium", "high", "urgent"],
|
|
288
|
-
},
|
|
289
|
-
|
|
290
|
-
// Conditional schemas
|
|
291
|
-
contactMethod: { type: "string", enum: ["email", "phone", "sms"] },
|
|
292
|
-
},
|
|
293
|
-
|
|
294
|
-
// Cross-field validation
|
|
295
|
-
allOf: [
|
|
296
|
-
{
|
|
297
|
-
if: { properties: { contactMethod: { const: "phone" } } },
|
|
298
|
-
then: { required: ["phoneNumber"] },
|
|
299
|
-
},
|
|
300
|
-
],
|
|
301
|
-
|
|
302
|
-
required: ["name", "email"],
|
|
303
|
-
};
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
## Step-Level Data Collection
|
|
307
|
-
|
|
308
|
-
### Basic Collection with Agent-Level Data
|
|
309
|
-
|
|
310
|
-
```typescript
|
|
311
|
-
const profileRoute = agent
|
|
312
|
-
.createRoute({
|
|
313
|
-
title: "Profile Collection",
|
|
314
|
-
requiredFields: ["name", "email", "age"], // Required for route completion
|
|
315
|
-
optionalFields: ["interests"], // Optional but helpful
|
|
316
|
-
initialStep: {
|
|
317
|
-
prompt:
|
|
318
|
-
"Hi! I'm collecting some information to personalize your experience. What's your name?",
|
|
319
|
-
collect: ["name"], // Maps to agent schema field
|
|
320
|
-
},
|
|
321
|
-
})
|
|
322
|
-
.nextStep({
|
|
323
|
-
prompt: "Thanks {{name}}! What's your email address?",
|
|
324
|
-
collect: ["email"],
|
|
325
|
-
requires: ["name"], // Must have name before collecting email
|
|
326
|
-
})
|
|
327
|
-
.nextStep({
|
|
328
|
-
prompt: "What's your age?",
|
|
329
|
-
collect: ["age"],
|
|
330
|
-
requires: ["name", "email"],
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
// Route completes when the step flow naturally reaches its end
|
|
334
|
-
// Data is available to all other routes
|
|
335
|
-
```
|
|
336
|
-
|
|
337
|
-
### Multi-Field Collection
|
|
338
|
-
|
|
339
|
-
```typescript
|
|
340
|
-
const comprehensiveStep = {
|
|
341
|
-
prompt: `
|
|
342
|
-
Now I'd like to learn more about you. Please share:
|
|
343
|
-
- Your age
|
|
344
|
-
- Your main interests (comma-separated)
|
|
345
|
-
- Whether you'd like email notifications (yes/no)
|
|
346
|
-
- Your preferred theme (light/dark)
|
|
347
|
-
`,
|
|
348
|
-
collect: [
|
|
349
|
-
"age", // Single field from agent schema
|
|
350
|
-
"interests", // Array field from agent schema
|
|
351
|
-
"preferences.notifications", // Nested field (dot notation)
|
|
352
|
-
"preferences.theme", // Another nested field
|
|
353
|
-
],
|
|
354
|
-
requires: ["name", "email"], // Prerequisites from agent data
|
|
355
|
-
};
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
### Conditional Collection
|
|
359
|
-
|
|
360
|
-
```typescript
|
|
361
|
-
const conditionalCollection = {
|
|
362
|
-
prompt: "Would you like to set up notifications? (yes/no)",
|
|
363
|
-
collect: ["preferences.notifications"],
|
|
364
|
-
skipIf: (data) => data.preferences?.notifications !== undefined, // Skip if already collected
|
|
365
|
-
requires: ["name", "email"], // Prerequisites from agent data
|
|
366
|
-
};
|
|
367
|
-
```
|
|
368
|
-
|
|
369
|
-
### Cross-Route Data Sharing
|
|
370
|
-
|
|
371
|
-
With agent-level data collection, routes can share data seamlessly:
|
|
372
|
-
|
|
373
|
-
```typescript
|
|
374
|
-
// User starts with profile collection
|
|
375
|
-
const response1 = await agent.respond("Hi, I'm John Doe, email john@example.com");
|
|
376
|
-
// Agent data: { name: "John Doe", email: "john@example.com" }
|
|
377
|
-
|
|
378
|
-
// User switches to support - data is already available
|
|
379
|
-
const response2 = await agent.respond("Actually, I need help with a technical issue");
|
|
380
|
-
// Support route can access name and email, only needs to collect issue details
|
|
381
|
-
// Support route: 2/3 required fields already satisfied
|
|
382
|
-
|
|
383
|
-
// User provides issue details
|
|
384
|
-
const response3 = await agent.respond("My account won't sync properly");
|
|
385
|
-
// Support route completes: { name: "John Doe", email: "john@example.com", issueType: "technical" }
|
|
386
|
-
|
|
387
|
-
// Later, user wants to give feedback
|
|
388
|
-
const response4 = await agent.respond("I want to rate my support experience - 5 stars");
|
|
389
|
-
// Feedback route completes immediately: already has name, email, and now rating
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
## Data Validation & Processing
|
|
393
|
-
|
|
394
|
-
### Agent-Level Lifecycle Hooks
|
|
395
|
-
|
|
396
|
-
```typescript
|
|
397
|
-
const agent = new Agent<{}, UserProfile>({
|
|
398
|
-
name: "User Management Agent",
|
|
399
|
-
schema: { /* agent schema */ },
|
|
400
|
-
|
|
401
|
-
// Agent-level data validation (applies to all routes)
|
|
402
|
-
hooks: {
|
|
403
|
-
onDataUpdate: (newData, previousData) => {
|
|
404
|
-
// Cross-field validation using complete agent data
|
|
405
|
-
if (newData.email && newData.confirmEmail) {
|
|
406
|
-
if (newData.email !== newData.confirmEmail) {
|
|
407
|
-
throw new Error("Email addresses don't match");
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
// Data enrichment based on agent-level data
|
|
412
|
-
if (newData.name && !newData.displayName) {
|
|
413
|
-
newData.displayName = newData.name.split(" ")[0]; // First name only
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// Auto-set subscription tier based on email domain
|
|
417
|
-
if (newData.email && !newData.subscriptionTier) {
|
|
418
|
-
newData.subscriptionTier = newData.email.includes('@enterprise.com') ? 'enterprise' : 'free';
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// Validate against agent schema
|
|
422
|
-
const validation = agent.validateData(newData);
|
|
423
|
-
if (!validation.valid) {
|
|
424
|
-
throw new Error(`Data validation failed: ${validation.errors.map(e => e.message).join(', ')}`);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
return newData;
|
|
428
|
-
},
|
|
429
|
-
},
|
|
430
|
-
});
|
|
431
|
-
```
|
|
432
|
-
|
|
433
|
-
### Route-Level Hooks
|
|
434
|
-
|
|
435
|
-
```typescript
|
|
436
|
-
const routeWithHooks = agent.createRoute({
|
|
437
|
-
title: "Smart Collection",
|
|
438
|
-
|
|
439
|
-
hooks: {
|
|
440
|
-
onDataUpdate: async (newData, previousData) => {
|
|
441
|
-
// External validation
|
|
442
|
-
if (newData.email) {
|
|
443
|
-
const isValid = await validateEmailExternally(newData.email);
|
|
444
|
-
if (!isValid) {
|
|
445
|
-
throw new Error("Email validation failed");
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
// Data transformation
|
|
450
|
-
if (newData.interests && typeof newData.interests === "string") {
|
|
451
|
-
newData.interests = newData.interests.split(",").map((s) => s.trim());
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
return newData;
|
|
455
|
-
},
|
|
456
|
-
|
|
457
|
-
onContextUpdate: (newContext, previousContext) => {
|
|
458
|
-
// React to context changes that affect data collection
|
|
459
|
-
console.log("Context updated, may affect data validation rules");
|
|
460
|
-
},
|
|
461
|
-
},
|
|
462
|
-
});
|
|
463
|
-
```
|
|
464
|
-
|
|
465
|
-
## Data Flow & Persistence
|
|
466
|
-
|
|
467
|
-
### Session Data Management
|
|
468
|
-
|
|
469
|
-
```typescript
|
|
470
|
-
// Data flows through the session
|
|
471
|
-
const session = {
|
|
472
|
-
data: {
|
|
473
|
-
name: "John Doe",
|
|
474
|
-
email: "john@example.com",
|
|
475
|
-
age: 30,
|
|
476
|
-
},
|
|
477
|
-
dataByRoute: {
|
|
478
|
-
"user-profile": {
|
|
479
|
-
name: "John Doe",
|
|
480
|
-
email: "john@example.com",
|
|
481
|
-
age: 30,
|
|
482
|
-
},
|
|
483
|
-
preferences: {
|
|
484
|
-
theme: "dark",
|
|
485
|
-
notifications: true,
|
|
486
|
-
},
|
|
487
|
-
},
|
|
488
|
-
};
|
|
489
|
-
```
|
|
490
|
-
|
|
491
|
-
### Initial Data
|
|
492
|
-
|
|
493
|
-
```typescript
|
|
494
|
-
const routeWithInitialData = agent.createRoute({
|
|
495
|
-
title: "Resume Collection",
|
|
496
|
-
schema: userProfileSchema,
|
|
497
|
-
|
|
498
|
-
// Pre-populate known data
|
|
499
|
-
initialData: {
|
|
500
|
-
name: "John Doe", // From authentication
|
|
501
|
-
email: "john@example.com", // From user profile
|
|
502
|
-
preferences: {
|
|
503
|
-
theme: "dark", // From user settings
|
|
504
|
-
},
|
|
505
|
-
},
|
|
506
|
-
|
|
507
|
-
initialStep: {
|
|
508
|
-
prompt:
|
|
509
|
-
"Welcome back {{name}}! Let's update your profile. How old are you?",
|
|
510
|
-
collect: ["age"],
|
|
511
|
-
skipIf: (data) => data.age !== undefined, // Skip if already known
|
|
512
|
-
},
|
|
513
|
-
});
|
|
514
|
-
```
|
|
515
|
-
|
|
516
|
-
### Cross-Route Data Sharing
|
|
517
|
-
|
|
518
|
-
```typescript
|
|
519
|
-
// Route 1: Basic profile
|
|
520
|
-
const profileRoute = agent.createRoute({
|
|
521
|
-
title: "Basic Profile",
|
|
522
|
-
schema: {
|
|
523
|
-
/* basic fields */
|
|
524
|
-
},
|
|
525
|
-
});
|
|
526
|
-
|
|
527
|
-
// Route 2: Extended profile (can access data from Route 1)
|
|
528
|
-
const extendedRoute = agent.createRoute({
|
|
529
|
-
title: "Extended Profile",
|
|
530
|
-
schema: {
|
|
531
|
-
/* extended fields */
|
|
532
|
-
},
|
|
533
|
-
initialData: (session) => ({
|
|
534
|
-
// Access data from previous route
|
|
535
|
-
...session.dataByRoute?.["basic-profile"],
|
|
536
|
-
}),
|
|
537
|
-
});
|
|
538
|
-
```
|
|
539
|
-
|
|
540
|
-
## AI Response Processing
|
|
541
|
-
|
|
542
|
-
### Structured Response Schema
|
|
543
|
-
|
|
544
|
-
The system automatically builds response schemas for AI:
|
|
545
|
-
|
|
546
|
-
```typescript
|
|
547
|
-
// For a step collecting ["name", "email"]
|
|
548
|
-
const responseSchema = {
|
|
549
|
-
type: "object",
|
|
550
|
-
properties: {
|
|
551
|
-
message: { type: "string" }, // Standard response field
|
|
552
|
-
name: {
|
|
553
|
-
/* from route schema */
|
|
554
|
-
},
|
|
555
|
-
email: {
|
|
556
|
-
/* from route schema */
|
|
557
|
-
},
|
|
558
|
-
},
|
|
559
|
-
required: ["message"],
|
|
560
|
-
};
|
|
561
|
-
```
|
|
562
|
-
|
|
563
|
-
### Tool Integration
|
|
564
|
-
|
|
565
|
-
Data collection works seamlessly with tools:
|
|
566
|
-
|
|
567
|
-
```typescript
|
|
568
|
-
const toolEnhancedStep = {
|
|
569
|
-
prompt: "What's your location? I'll help you find nearby services.",
|
|
570
|
-
collect: ["location"],
|
|
571
|
-
tools: ["geolocation-tool"], // Tools can enrich collected data
|
|
572
|
-
finalize: async (context, data) => {
|
|
573
|
-
if (data.location) {
|
|
574
|
-
// Use collected location data
|
|
575
|
-
const services = await findNearbyServices(data.location);
|
|
576
|
-
// Update context or trigger next steps
|
|
577
|
-
}
|
|
578
|
-
},
|
|
579
|
-
};
|
|
580
|
-
```
|
|
581
|
-
|
|
582
|
-
## Error Handling & Recovery
|
|
583
|
-
|
|
584
|
-
### Validation Errors
|
|
585
|
-
|
|
586
|
-
```typescript
|
|
587
|
-
const robustRoute = agent.createRoute({
|
|
588
|
-
title: "Robust Collection",
|
|
589
|
-
|
|
590
|
-
hooks: {
|
|
591
|
-
onDataUpdate: (newData, previousData) => {
|
|
592
|
-
// Handle validation gracefully
|
|
593
|
-
try {
|
|
594
|
-
validateUserData(newData);
|
|
595
|
-
return newData;
|
|
596
|
-
} catch (error) {
|
|
597
|
-
// Provide user-friendly error recovery
|
|
598
|
-
if (error.message.includes("email")) {
|
|
599
|
-
newData.emailError = "Please provide a valid email address";
|
|
600
|
-
}
|
|
601
|
-
return { ...previousData, ...newData };
|
|
602
|
-
}
|
|
603
|
-
},
|
|
604
|
-
},
|
|
605
|
-
});
|
|
606
|
-
```
|
|
607
|
-
|
|
608
|
-
### Recovery Steps
|
|
609
|
-
|
|
610
|
-
```typescript
|
|
611
|
-
const recoveryRoute = agent
|
|
612
|
-
.createRoute({
|
|
613
|
-
title: "Error Recovery",
|
|
614
|
-
|
|
615
|
-
initialStep: {
|
|
616
|
-
prompt:
|
|
617
|
-
"I noticed there was an issue with the information provided. Let's try again.",
|
|
618
|
-
collect: ["correctedData"],
|
|
619
|
-
when: "if there are validation errors in session data",
|
|
620
|
-
},
|
|
621
|
-
})
|
|
622
|
-
.nextStep({
|
|
623
|
-
prompt:
|
|
624
|
-
"Thanks for the correction. Here's what I now have: {{correctedData}}",
|
|
625
|
-
requires: ["correctedData"],
|
|
626
|
-
});
|
|
627
|
-
```
|
|
628
|
-
|
|
629
|
-
## Advanced Patterns
|
|
630
|
-
|
|
631
|
-
### Progressive Collection
|
|
632
|
-
|
|
633
|
-
```typescript
|
|
634
|
-
const progressiveRoute = agent
|
|
635
|
-
.createRoute({
|
|
636
|
-
title: "Progressive Collection",
|
|
637
|
-
|
|
638
|
-
// Start with minimal required data
|
|
639
|
-
initialStep: {
|
|
640
|
-
prompt: "Let's get started! What's your name and email?",
|
|
641
|
-
collect: ["name", "email"],
|
|
642
|
-
},
|
|
643
|
-
|
|
644
|
-
// Add more fields as conversation progresses
|
|
645
|
-
})
|
|
646
|
-
.nextStep({
|
|
647
|
-
prompt: "Great! Now tell me about your interests and preferences.",
|
|
648
|
-
collect: ["interests", "preferences"],
|
|
649
|
-
requires: ["name", "email"],
|
|
650
|
-
skipIf: (data) => data.interests && data.preferences, // Skip if already have both
|
|
651
|
-
});
|
|
652
|
-
```
|
|
653
|
-
|
|
654
|
-
### Conditional Schema Expansion
|
|
655
|
-
|
|
656
|
-
```typescript
|
|
657
|
-
const dynamicRoute = agent
|
|
658
|
-
.createRoute({
|
|
659
|
-
title: "Dynamic Collection",
|
|
660
|
-
|
|
661
|
-
initialStep: {
|
|
662
|
-
prompt: "Are you a business user or individual?",
|
|
663
|
-
collect: ["userType"],
|
|
664
|
-
},
|
|
665
|
-
})
|
|
666
|
-
.branch([
|
|
667
|
-
{
|
|
668
|
-
name: "business",
|
|
669
|
-
step: {
|
|
670
|
-
prompt: "For business users, I need company information...",
|
|
671
|
-
collect: ["companyName", "industry", "companySize"],
|
|
672
|
-
requires: ["userType"],
|
|
673
|
-
},
|
|
674
|
-
},
|
|
675
|
-
{
|
|
676
|
-
name: "individual",
|
|
677
|
-
step: {
|
|
678
|
-
prompt: "For individual users, tell me about your interests...",
|
|
679
|
-
collect: ["personalInterests", "occupation"],
|
|
680
|
-
requires: ["userType"],
|
|
681
|
-
},
|
|
682
|
-
},
|
|
683
|
-
]);
|
|
684
|
-
```
|
|
685
|
-
|
|
686
|
-
## Best Practices
|
|
687
|
-
|
|
688
|
-
### Schema Design
|
|
689
|
-
|
|
690
|
-
1. **Start Simple**: Begin with required fields only
|
|
691
|
-
2. **Progressive Enhancement**: Add optional fields as needed
|
|
692
|
-
3. **Clear Descriptions**: Document field purposes in schema
|
|
693
|
-
4. **Validation Rules**: Use JSON Schema constraints effectively
|
|
694
|
-
|
|
695
|
-
### Collection Strategy
|
|
696
|
-
|
|
697
|
-
1. **Logical Order**: Collect prerequisite data first
|
|
698
|
-
2. **Smart Skipping**: Skip already-known information
|
|
699
|
-
3. **Natural Flow**: Make conversations feel organic
|
|
700
|
-
4. **Error Recovery**: Plan for validation failures
|
|
701
|
-
|
|
702
|
-
### Performance
|
|
703
|
-
|
|
704
|
-
1. **Minimal Schemas**: Only define needed fields
|
|
705
|
-
2. **Efficient Hooks**: Avoid expensive operations in data hooks
|
|
706
|
-
3. **Caching**: Cache external validation results
|
|
707
|
-
4. **Batch Updates**: Group related field collections
|
|
708
|
-
|
|
709
|
-
### Testing
|
|
710
|
-
|
|
711
|
-
```typescript
|
|
712
|
-
// Test data collection — classic signature
|
|
713
|
-
const mockSession = createSession();
|
|
714
|
-
|
|
715
|
-
// Or pre-populate fields with the partial state overload
|
|
716
|
-
const prepopulatedSession = createSession({
|
|
717
|
-
data: { name: "Test User", email: "test@example.com" },
|
|
718
|
-
});
|
|
719
|
-
|
|
720
|
-
const testData = {
|
|
721
|
-
name: "Test User",
|
|
722
|
-
email: "test@example.com",
|
|
723
|
-
};
|
|
724
|
-
|
|
725
|
-
// Simulate collection
|
|
726
|
-
const updatedSession = mergeCollected(mockSession, testData);
|
|
727
|
-
|
|
728
|
-
// Validate against schema
|
|
729
|
-
const isValid = validateAgainstSchema(testData, route.schema);
|
|
730
|
-
```
|
|
731
|
-
|
|
732
|
-
## Integration Examples
|
|
733
|
-
|
|
734
|
-
### Database Persistence
|
|
735
|
-
|
|
736
|
-
```typescript
|
|
737
|
-
const dbRoute = agent.createRoute({
|
|
738
|
-
title: "Database-Backed Collection",
|
|
739
|
-
|
|
740
|
-
hooks: {
|
|
741
|
-
onDataUpdate: async (newData, previousData) => {
|
|
742
|
-
// Auto-save to database
|
|
743
|
-
if (newData.email && newData.email !== previousData.email) {
|
|
744
|
-
await saveEmailToDatabase(newData.email);
|
|
745
|
-
}
|
|
746
|
-
return newData;
|
|
747
|
-
},
|
|
748
|
-
},
|
|
749
|
-
});
|
|
750
|
-
```
|
|
751
|
-
|
|
752
|
-
### External API Integration
|
|
753
|
-
|
|
754
|
-
```typescript
|
|
755
|
-
const apiRoute = agent.createRoute({
|
|
756
|
-
title: "API-Enhanced Collection",
|
|
757
|
-
|
|
758
|
-
hooks: {
|
|
759
|
-
onDataUpdate: async (newData, previousData) => {
|
|
760
|
-
// Enrich data with external APIs
|
|
761
|
-
if (newData.zipCode && !newData.city) {
|
|
762
|
-
const locationData = await geocodeZipCode(newData.zipCode);
|
|
763
|
-
newData.city = locationData.city;
|
|
764
|
-
newData.state = locationData.state;
|
|
765
|
-
}
|
|
766
|
-
return newData;
|
|
767
|
-
},
|
|
768
|
-
},
|
|
769
|
-
});
|
|
770
|
-
```
|
|
771
|
-
|
|
772
|
-
The schema-driven data collection system transforms natural conversations into structured, validated data while maintaining the flexibility and intelligence of AI-driven interactions.
|