@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,795 +0,0 @@
|
|
|
1
|
-
# Getting Started with @falai/agent
|
|
2
|
-
|
|
3
|
-
Complete guide to building your first conversational AI agent in under 30 minutes.
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## 🎯 What You'll Build
|
|
8
|
-
|
|
9
|
-
By the end of this guide, you'll have a working AI agent that can:
|
|
10
|
-
|
|
11
|
-
- ✅ Understand natural language queries
|
|
12
|
-
- ✅ Extract structured data from conversations
|
|
13
|
-
- ✅ Maintain context across multiple turns
|
|
14
|
-
- ✅ Use tools to perform actions
|
|
15
|
-
- ✅ Handle complex conversation flows
|
|
16
|
-
- ✅ Execute multiple steps in a single LLM call (multi-step execution)
|
|
17
|
-
|
|
18
|
-
**Time estimate:** 15-30 minutes
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
## 📋 Prerequisites
|
|
23
|
-
|
|
24
|
-
### Required
|
|
25
|
-
|
|
26
|
-
- **Node.js 18+** or **Bun 1.0+**
|
|
27
|
-
- **API key** for an AI provider (Gemini, OpenAI, or Anthropic)
|
|
28
|
-
|
|
29
|
-
### Optional
|
|
30
|
-
|
|
31
|
-
- **Redis** (for session persistence)
|
|
32
|
-
- **Database** (PostgreSQL, MySQL, etc. for advanced persistence)
|
|
33
|
-
|
|
34
|
-
---
|
|
35
|
-
|
|
36
|
-
## 🚀 Quick Start (5 minutes)
|
|
37
|
-
|
|
38
|
-
### 1. Create Your Project
|
|
39
|
-
|
|
40
|
-
```bash
|
|
41
|
-
# Create a new directory
|
|
42
|
-
mkdir my-first-agent && cd my-first-agent
|
|
43
|
-
|
|
44
|
-
# Initialize with your package manager
|
|
45
|
-
bun init -y # or npm init -y
|
|
46
|
-
|
|
47
|
-
# Install @falai/agent
|
|
48
|
-
bun add @falai/agent
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### 2. Set Up Environment
|
|
52
|
-
|
|
53
|
-
Create a `.env` file:
|
|
54
|
-
|
|
55
|
-
```bash
|
|
56
|
-
# Choose one AI provider
|
|
57
|
-
GEMINI_API_KEY=your_gemini_api_key_here
|
|
58
|
-
# OPENAI_API_KEY=your_openai_api_key_here
|
|
59
|
-
# ANTHROPIC_API_KEY=your_anthropic_api_key_here
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
### 3. Create Your First Agent
|
|
63
|
-
|
|
64
|
-
Create `index.ts`:
|
|
65
|
-
|
|
66
|
-
```typescript
|
|
67
|
-
import { Agent, GeminiProvider } from "@falai/agent";
|
|
68
|
-
|
|
69
|
-
// Create AI provider
|
|
70
|
-
const provider = new GeminiProvider({
|
|
71
|
-
apiKey: process.env.GEMINI_API_KEY!,
|
|
72
|
-
model: "models/gemini-2.5-flash",
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
// Create your agent
|
|
76
|
-
const agent = new Agent({
|
|
77
|
-
name: "MyFirstAgent",
|
|
78
|
-
description: "A helpful AI assistant",
|
|
79
|
-
provider,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// Create a simple route with basic string condition
|
|
83
|
-
const generalRoute = agent.createRoute({
|
|
84
|
-
title: "General Help",
|
|
85
|
-
description: "Answers general questions",
|
|
86
|
-
when: ["User needs help or asks a question"], // Simple string condition
|
|
87
|
-
initialStep: {
|
|
88
|
-
prompt: "How can I help you today?",
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// Test your agent
|
|
93
|
-
async function main() {
|
|
94
|
-
const response = await agent.respond("Hello! Can you tell me about TypeScript?");
|
|
95
|
-
|
|
96
|
-
console.log("🤖 Agent:", response.message);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
main();
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### 4. Run Your Agent
|
|
103
|
-
|
|
104
|
-
```bash
|
|
105
|
-
# Run with Bun
|
|
106
|
-
bun run index.ts
|
|
107
|
-
|
|
108
|
-
# Or with Node.js + TypeScript
|
|
109
|
-
npx tsx index.ts
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
**Congratulations!** 🎉 You now have a working AI agent.
|
|
113
|
-
|
|
114
|
-
---
|
|
115
|
-
|
|
116
|
-
## 🏗️ Building a Data-Driven Agent (10 minutes)
|
|
117
|
-
|
|
118
|
-
Now let's build an agent that intelligently collects structured data:
|
|
119
|
-
|
|
120
|
-
### Define Your Data Schema
|
|
121
|
-
|
|
122
|
-
```typescript
|
|
123
|
-
// Define the data you want to collect
|
|
124
|
-
interface BookingData {
|
|
125
|
-
destination: string;
|
|
126
|
-
travelDate: string;
|
|
127
|
-
travelers: number;
|
|
128
|
-
budget: number;
|
|
129
|
-
}
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
### Create an Agent with Centralized Data Schema
|
|
133
|
-
|
|
134
|
-
```typescript
|
|
135
|
-
import { Agent, GeminiProvider } from "@falai/agent";
|
|
136
|
-
|
|
137
|
-
// Define data interface first
|
|
138
|
-
interface BookingData {
|
|
139
|
-
destination?: string;
|
|
140
|
-
travelDate?: string;
|
|
141
|
-
travelers?: number;
|
|
142
|
-
budget?: number;
|
|
143
|
-
estimatedPrice?: number;
|
|
144
|
-
availabilityChecked?: boolean;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// Create agent with centralized data schema first
|
|
148
|
-
const agent = new Agent<{}, BookingData>({
|
|
149
|
-
name: "TravelAgent",
|
|
150
|
-
provider: new GeminiProvider({
|
|
151
|
-
apiKey: process.env.GEMINI_API_KEY!,
|
|
152
|
-
}),
|
|
153
|
-
|
|
154
|
-
// Agent-level schema defines all possible data fields
|
|
155
|
-
schema: {
|
|
156
|
-
type: "object",
|
|
157
|
-
properties: {
|
|
158
|
-
destination: { type: "string", description: "Travel destination" },
|
|
159
|
-
travelDate: { type: "string", description: "Travel date" },
|
|
160
|
-
travelers: { type: "number", minimum: 1, maximum: 10 },
|
|
161
|
-
budget: { type: "number", description: "Budget in USD" },
|
|
162
|
-
},
|
|
163
|
-
required: ["destination", "travelers"],
|
|
164
|
-
},
|
|
165
|
-
|
|
166
|
-
// Agent-level data validation and enrichment
|
|
167
|
-
hooks: {
|
|
168
|
-
onDataUpdate: async (data, previousData) => {
|
|
169
|
-
// Auto-set budget range based on travelers
|
|
170
|
-
if (data.travelers && !data.budget) {
|
|
171
|
-
data.budget = data.travelers * 500; // Default $500 per person
|
|
172
|
-
}
|
|
173
|
-
return data;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
// Create booking tool using unified Tool interface - simple return value
|
|
179
|
-
agent.addTool({
|
|
180
|
-
id: "check_availability",
|
|
181
|
-
name: "Availability Checker",
|
|
182
|
-
description: "Check travel availability and pricing",
|
|
183
|
-
parameters: {
|
|
184
|
-
type: "object",
|
|
185
|
-
properties: {},
|
|
186
|
-
},
|
|
187
|
-
handler: async ({ context, data, updateData }) => {
|
|
188
|
-
// Simulate availability check using collected data
|
|
189
|
-
const available = Math.random() > 0.2;
|
|
190
|
-
const price = Math.floor(Math.random() * 1000) + 500;
|
|
191
|
-
|
|
192
|
-
// Update data with pricing information using helper method
|
|
193
|
-
await updateData({
|
|
194
|
-
estimatedPrice: price,
|
|
195
|
-
availabilityChecked: true
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// Return simple string value - unified interface supports both simple and complex returns
|
|
199
|
-
return available
|
|
200
|
-
? `✅ Available! Estimated cost: $${price} for ${data.travelers} travelers to ${data.destination}`
|
|
201
|
-
: "❌ Not available for those dates. Please try different dates.";
|
|
202
|
-
},
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
// Routes specify required fields instead of schemas
|
|
206
|
-
const bookingRoute = agent.createRoute({
|
|
207
|
-
title: "Travel Booking",
|
|
208
|
-
description: "Help users book travel",
|
|
209
|
-
when: ["User wants to book travel"],
|
|
210
|
-
requiredFields: ["destination", "travelDate", "travelers"], // Required for completion
|
|
211
|
-
optionalFields: ["budget"], // Nice to have but not required
|
|
212
|
-
|
|
213
|
-
initialStep: {
|
|
214
|
-
prompt: "I'd love to help you book a trip! Where would you like to go?",
|
|
215
|
-
collect: ["destination"],
|
|
216
|
-
},
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
// Build conversation flow with agent-level data awareness
|
|
220
|
-
const askDate = bookingRoute.initialStep.nextStep({
|
|
221
|
-
prompt: "When would you like to travel?",
|
|
222
|
-
collect: ["travelDate"],
|
|
223
|
-
requires: ["destination"], // Must have destination from agent data
|
|
224
|
-
skipIf: (data) => !!data.travelDate, // Skip if already collected
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
const askTravelers = askDate.nextStep({
|
|
228
|
-
prompt: "How many people are traveling?",
|
|
229
|
-
collect: ["travelers"],
|
|
230
|
-
requires: ["destination"], // Prerequisites from agent data
|
|
231
|
-
skipIf: (data) => data.travelers !== undefined,
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
const askBudget = askTravelers.nextStep({
|
|
235
|
-
prompt: "What's your budget for this trip?",
|
|
236
|
-
collect: ["budget"],
|
|
237
|
-
requires: ["destination", "travelers"],
|
|
238
|
-
skipIf: (data) => data.budget !== undefined,
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
const checkAndBook = askBudget.nextStep({
|
|
242
|
-
prompt: "Let me check availability for your trip.",
|
|
243
|
-
tools: ["check_availability"], // Reference tool by ID
|
|
244
|
-
requires: ["destination", "travelers"], // Minimum data needed
|
|
245
|
-
});
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
### Test the Agent-Level Data Collection
|
|
249
|
-
|
|
250
|
-
```typescript
|
|
251
|
-
async function testBookingAgent() {
|
|
252
|
-
// Create agent with automatic session management and agent-level schema
|
|
253
|
-
const sessionAgent = new Agent<{}, BookingData>({
|
|
254
|
-
name: "TravelAgent",
|
|
255
|
-
provider: new GeminiProvider({
|
|
256
|
-
apiKey: process.env.GEMINI_API_KEY!,
|
|
257
|
-
}),
|
|
258
|
-
sessionId: "user-alice", // Automatically manages this session
|
|
259
|
-
|
|
260
|
-
// Same agent-level schema and configuration
|
|
261
|
-
schema: agent.schema,
|
|
262
|
-
hooks: agent.hooks
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
// Add the same tool to the session agent
|
|
266
|
-
sessionAgent.addTool({
|
|
267
|
-
id: "check_availability",
|
|
268
|
-
name: "Availability Checker",
|
|
269
|
-
description: "Check travel availability and pricing",
|
|
270
|
-
handler: async ({ context, data, updateData }) => {
|
|
271
|
-
const available = Math.random() > 0.2;
|
|
272
|
-
const price = Math.floor(Math.random() * 1000) + 500;
|
|
273
|
-
await updateData({ estimatedPrice: price, availabilityChecked: true });
|
|
274
|
-
return {
|
|
275
|
-
data: available
|
|
276
|
-
? `✅ Available! Estimated cost: $${price} for ${data.travelers} travelers to ${data.destination}`
|
|
277
|
-
: "❌ Not available for those dates. Please try different dates.",
|
|
278
|
-
};
|
|
279
|
-
},
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
// Copy the route to the session agent
|
|
283
|
-
sessionAgent.createRoute(bookingRoute.options);
|
|
284
|
-
|
|
285
|
-
// User provides partial information - simple message API
|
|
286
|
-
const response1 = await sessionAgent.respond("I want to go to Paris");
|
|
287
|
-
|
|
288
|
-
console.log("Bot:", response1.message);
|
|
289
|
-
console.log("Agent data:", sessionAgent.getCollectedData()); // Agent-level data access
|
|
290
|
-
|
|
291
|
-
// User provides more details - session automatically maintained
|
|
292
|
-
const response2 = await sessionAgent.respond("Next Friday, 2 people, $2000 budget");
|
|
293
|
-
|
|
294
|
-
console.log("Bot:", response2.message);
|
|
295
|
-
console.log("Final agent data:", sessionAgent.getCollectedData());
|
|
296
|
-
|
|
297
|
-
// Check route completion
|
|
298
|
-
console.log("Route complete:", bookingRoute.isComplete(sessionAgent.getCollectedData()));
|
|
299
|
-
console.log("Progress:", Math.round(bookingRoute.getCompletionProgress(sessionAgent.getCollectedData()) * 100) + "%");
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
testBookingAgent();
|
|
303
|
-
|
|
304
|
-
// Advanced: Show different tool creation approaches
|
|
305
|
-
function demonstrateToolCreationMethods() {
|
|
306
|
-
// Method 1: Direct addition (simplest)
|
|
307
|
-
agent.addTool({
|
|
308
|
-
id: "simple_search",
|
|
309
|
-
description: "Basic search functionality",
|
|
310
|
-
handler: async ({ context, data }, args) => `Found results for: ${args.query}`
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
// Method 2: Registry for reuse
|
|
314
|
-
agent.tool.register({
|
|
315
|
-
id: "reusable_validator",
|
|
316
|
-
description: "Reusable validation tool",
|
|
317
|
-
handler: async ({ context, data }, args) => ({
|
|
318
|
-
data: "Validation complete",
|
|
319
|
-
success: true,
|
|
320
|
-
contextUpdate: { lastValidated: new Date() }
|
|
321
|
-
})
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
// Method 3: Pattern helpers
|
|
325
|
-
const enrichmentTool = agent.tool.createDataEnrichment({
|
|
326
|
-
id: "enrich_booking",
|
|
327
|
-
fields: ['destination', 'travelers'],
|
|
328
|
-
enricher: async (context, data) => ({
|
|
329
|
-
bookingCode: `${data.destination?.slice(0,3).toUpperCase()}-${data.travelers}`,
|
|
330
|
-
estimatedDuration: data.destination === 'Paris' ? '8 hours' : '12 hours'
|
|
331
|
-
})
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
agent.tool.register(enrichmentTool);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
demonstrateToolCreationMethods();
|
|
338
|
-
|
|
339
|
-
// Advanced: Tools as Step Lifecycle Hooks
|
|
340
|
-
function demonstrateLifecycleHooks() {
|
|
341
|
-
// Create tools for step lifecycle
|
|
342
|
-
agent.addTool({
|
|
343
|
-
id: "prepare_booking",
|
|
344
|
-
description: "Prepare booking context",
|
|
345
|
-
handler: async ({ context, data, updateContext }) => {
|
|
346
|
-
// Enrich context before AI response
|
|
347
|
-
await updateContext({
|
|
348
|
-
bookingStartTime: new Date().toISOString(),
|
|
349
|
-
userTier: 'premium'
|
|
350
|
-
});
|
|
351
|
-
return "Booking preparation complete"; // Simple return
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
agent.addTool({
|
|
356
|
-
id: "finalize_booking",
|
|
357
|
-
description: "Finalize booking process",
|
|
358
|
-
handler: async ({ context, data }) => {
|
|
359
|
-
// Process after AI response
|
|
360
|
-
const confirmationId = await bookingService.reserve(data);
|
|
361
|
-
return {
|
|
362
|
-
data: `Booking reserved with ID: ${confirmationId}`,
|
|
363
|
-
success: true,
|
|
364
|
-
contextUpdate: { lastBookingId: confirmationId }
|
|
365
|
-
}; // Complex ToolResult
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
// Use tools in step lifecycle
|
|
370
|
-
const bookingStep = agent.createRoute({
|
|
371
|
-
title: "Hotel Booking with Lifecycle",
|
|
372
|
-
steps: [{
|
|
373
|
-
id: "process_booking",
|
|
374
|
-
prompt: "Let me process your booking...",
|
|
375
|
-
prepare: "prepare_booking", // Tool executes before AI response
|
|
376
|
-
finalize: "finalize_booking", // Tool executes after AI response
|
|
377
|
-
collect: ["hotelName", "checkInDate"]
|
|
378
|
-
}]
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
console.log("Lifecycle hooks configured with tools");
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
demonstrateLifecycleHooks();
|
|
385
|
-
```
|
|
386
|
-
|
|
387
|
-
**Notice how the agent:**
|
|
388
|
-
|
|
389
|
-
- ✅ Automatically extracted destination from "Paris"
|
|
390
|
-
- ✅ Understood "Next Friday, 2 people, $2000 budget" as structured data
|
|
391
|
-
- ✅ Skipped asking for already-known information
|
|
392
|
-
- ✅ Used the ToolManager API to create and execute tools with simplified context
|
|
393
|
-
- ✅ Batched multiple steps together when data was available
|
|
394
|
-
|
|
395
|
-
---
|
|
396
|
-
|
|
397
|
-
## ⚡ Multi-Step Execution Benefits
|
|
398
|
-
|
|
399
|
-
@falai/agent automatically batches multiple steps together when their data requirements are satisfied, reducing LLM calls and improving conversation flow.
|
|
400
|
-
|
|
401
|
-
### How It Works
|
|
402
|
-
|
|
403
|
-
When a user provides information that satisfies multiple steps, they execute together:
|
|
404
|
-
|
|
405
|
-
```typescript
|
|
406
|
-
// Traditional approach: 3 separate turns
|
|
407
|
-
// Turn 1: "Which hotel?" → "Grand Hotel"
|
|
408
|
-
// Turn 2: "What date?" → "Friday"
|
|
409
|
-
// Turn 3: "How many guests?" → "2"
|
|
410
|
-
|
|
411
|
-
// With multi-step execution: 1 turn
|
|
412
|
-
const response = await agent.respond("Book Grand Hotel for 2 on Friday");
|
|
413
|
-
|
|
414
|
-
// All 3 steps execute in a single LLM call!
|
|
415
|
-
console.log(response.executedSteps);
|
|
416
|
-
// [
|
|
417
|
-
// { id: "ask-hotel", routeId: "booking" },
|
|
418
|
-
// { id: "ask-date", routeId: "booking" },
|
|
419
|
-
// { id: "ask-guests", routeId: "booking" }
|
|
420
|
-
// ]
|
|
421
|
-
|
|
422
|
-
console.log(response.stoppedReason);
|
|
423
|
-
// "route_complete"
|
|
424
|
-
```
|
|
425
|
-
|
|
426
|
-
### Benefits
|
|
427
|
-
|
|
428
|
-
1. **Fewer LLM Calls** - Multiple steps in one call reduces costs
|
|
429
|
-
2. **Better UX** - Users don't repeat information they've already provided
|
|
430
|
-
3. **Faster Responses** - Less back-and-forth means quicker completion
|
|
431
|
-
4. **Automatic** - No code changes needed, works with existing routes
|
|
432
|
-
|
|
433
|
-
### Seeing It In Action
|
|
434
|
-
|
|
435
|
-
```typescript
|
|
436
|
-
// Create a booking route with multiple steps
|
|
437
|
-
const bookingRoute = agent.createRoute({
|
|
438
|
-
title: "Hotel Booking",
|
|
439
|
-
requiredFields: ["hotel", "date", "guests"],
|
|
440
|
-
initialStep: {
|
|
441
|
-
prompt: "Which hotel?",
|
|
442
|
-
collect: ["hotel"],
|
|
443
|
-
skipIf: (data) => !!data.hotel,
|
|
444
|
-
},
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
const askDate = bookingRoute.initialStep.nextStep({
|
|
448
|
-
prompt: "What date?",
|
|
449
|
-
collect: ["date"],
|
|
450
|
-
skipIf: (data) => !!data.date,
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
const askGuests = askDate.nextStep({
|
|
454
|
-
prompt: "How many guests?",
|
|
455
|
-
collect: ["guests"],
|
|
456
|
-
skipIf: (data) => data.guests !== undefined,
|
|
457
|
-
});
|
|
458
|
-
|
|
459
|
-
// User provides all info at once
|
|
460
|
-
const response = await agent.respond(
|
|
461
|
-
"I want to book the Grand Hotel for 2 people next Friday"
|
|
462
|
-
);
|
|
463
|
-
|
|
464
|
-
// Pre-extraction captures all data
|
|
465
|
-
// All steps execute in one batch
|
|
466
|
-
// Route completes immediately!
|
|
467
|
-
|
|
468
|
-
console.log(response.message);
|
|
469
|
-
// "Perfect! I've booked the Grand Hotel for 2 guests on Friday."
|
|
470
|
-
|
|
471
|
-
console.log(response.isRouteComplete);
|
|
472
|
-
// true
|
|
473
|
-
```
|
|
474
|
-
|
|
475
|
-
### Understanding the Response
|
|
476
|
-
|
|
477
|
-
The response includes information about what executed:
|
|
478
|
-
|
|
479
|
-
```typescript
|
|
480
|
-
const response = await agent.respond("Book for 2 guests");
|
|
481
|
-
|
|
482
|
-
// Which steps ran
|
|
483
|
-
console.log(response.executedSteps);
|
|
484
|
-
// [{ id: "ask-guests", routeId: "booking" }]
|
|
485
|
-
|
|
486
|
-
// Why execution stopped
|
|
487
|
-
console.log(response.stoppedReason);
|
|
488
|
-
// "needs_input" - waiting for hotel and date
|
|
489
|
-
|
|
490
|
-
// Or if complete:
|
|
491
|
-
// "route_complete" - all steps finished
|
|
492
|
-
// "end_route" - reached END_ROUTE marker
|
|
493
|
-
```
|
|
494
|
-
|
|
495
|
-
---
|
|
496
|
-
|
|
497
|
-
## 🎯 Flexible Routing Conditions (5 minutes)
|
|
498
|
-
|
|
499
|
-
Learn how to create sophisticated routing logic with the new `ConditionTemplate` system:
|
|
500
|
-
|
|
501
|
-
### Simple String Conditions (Beginner)
|
|
502
|
-
|
|
503
|
-
Perfect for AI-driven routing decisions:
|
|
504
|
-
|
|
505
|
-
```typescript
|
|
506
|
-
// String conditions provide context to AI for routing
|
|
507
|
-
const supportRoute = agent.createRoute({
|
|
508
|
-
title: "Customer Support",
|
|
509
|
-
when: "User needs help or has a problem", // AI understands intent
|
|
510
|
-
initialStep: {
|
|
511
|
-
prompt: "I'm here to help! What can I assist you with?",
|
|
512
|
-
},
|
|
513
|
-
});
|
|
514
|
-
|
|
515
|
-
const feedbackRoute = agent.createRoute({
|
|
516
|
-
title: "Feedback Collection",
|
|
517
|
-
when: "User wants to leave feedback or a review", // AI context
|
|
518
|
-
initialStep: {
|
|
519
|
-
prompt: "I'd love to hear your feedback!",
|
|
520
|
-
},
|
|
521
|
-
});
|
|
522
|
-
```
|
|
523
|
-
|
|
524
|
-
### Function Conditions (Advanced)
|
|
525
|
-
|
|
526
|
-
For programmatic logic and precise control:
|
|
527
|
-
|
|
528
|
-
```typescript
|
|
529
|
-
interface UserContext {
|
|
530
|
-
userType: 'free' | 'premium' | 'enterprise';
|
|
531
|
-
loginCount: number;
|
|
532
|
-
lastActivity: Date;
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
const agent = new Agent<UserContext>({
|
|
536
|
-
name: "SmartAgent",
|
|
537
|
-
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY! }),
|
|
538
|
-
context: {
|
|
539
|
-
userType: 'free',
|
|
540
|
-
loginCount: 1,
|
|
541
|
-
lastActivity: new Date(),
|
|
542
|
-
},
|
|
543
|
-
});
|
|
544
|
-
|
|
545
|
-
// Function-only conditions for precise control
|
|
546
|
-
const premiumRoute = agent.createRoute({
|
|
547
|
-
title: "Premium Features",
|
|
548
|
-
when: (ctx) => ctx.context?.userType === 'premium', // Programmatic check
|
|
549
|
-
initialStep: {
|
|
550
|
-
prompt: "Welcome to premium features! What would you like to explore?",
|
|
551
|
-
},
|
|
552
|
-
});
|
|
553
|
-
|
|
554
|
-
const onboardingRoute = agent.createRoute({
|
|
555
|
-
title: "User Onboarding",
|
|
556
|
-
when: (ctx) => ctx.context?.loginCount <= 3, // New user logic
|
|
557
|
-
initialStep: {
|
|
558
|
-
prompt: "Welcome! Let me show you around.",
|
|
559
|
-
},
|
|
560
|
-
});
|
|
561
|
-
```
|
|
562
|
-
|
|
563
|
-
### Mixed Array Conditions (Expert)
|
|
564
|
-
|
|
565
|
-
Combine AI understanding with programmatic precision:
|
|
566
|
-
|
|
567
|
-
```typescript
|
|
568
|
-
interface SupportContext {
|
|
569
|
-
userTier: 'basic' | 'premium' | 'enterprise';
|
|
570
|
-
supportTickets: number;
|
|
571
|
-
accountAge: number; // days
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
interface SupportData {
|
|
575
|
-
issueType?: 'technical' | 'billing' | 'general';
|
|
576
|
-
priority?: 'low' | 'medium' | 'high';
|
|
577
|
-
previousAttempts?: number;
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
const agent = new Agent<SupportContext, SupportData>({
|
|
581
|
-
name: "SupportAgent",
|
|
582
|
-
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY! }),
|
|
583
|
-
});
|
|
584
|
-
|
|
585
|
-
// Mixed conditions: AI context + programmatic logic
|
|
586
|
-
const escalationRoute = agent.createRoute({
|
|
587
|
-
title: "Escalated Support",
|
|
588
|
-
when: [
|
|
589
|
-
"User is frustrated or needs urgent help", // AI context
|
|
590
|
-
(ctx) => ctx.data?.previousAttempts > 2, // Programmatic check
|
|
591
|
-
(ctx) => ctx.context?.userTier === 'enterprise' // Context check
|
|
592
|
-
],
|
|
593
|
-
skipIf: [
|
|
594
|
-
"Support system is under maintenance", // AI context
|
|
595
|
-
(ctx) => new Date().getHours() < 9 || new Date().getHours() > 17 // Outside hours
|
|
596
|
-
],
|
|
597
|
-
initialStep: {
|
|
598
|
-
prompt: "I understand this is urgent. Let me connect you with our senior support team.",
|
|
599
|
-
},
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
// Advanced step conditions
|
|
603
|
-
const technicalStep = escalationRoute.initialStep.nextStep({
|
|
604
|
-
when: [
|
|
605
|
-
"User has a technical issue that needs expert help", // AI context
|
|
606
|
-
(ctx) => ctx.data?.issueType === 'technical' // Data check
|
|
607
|
-
],
|
|
608
|
-
skipIf: (ctx) => ctx.data?.priority === 'low', // Skip low priority technical issues
|
|
609
|
-
prompt: "Let me get our technical expert to help you.",
|
|
610
|
-
collect: ["issueDescription"]
|
|
611
|
-
});
|
|
612
|
-
```
|
|
613
|
-
|
|
614
|
-
### Route SkipIf (Dynamic Exclusion)
|
|
615
|
-
|
|
616
|
-
Exclude routes from consideration based on conditions:
|
|
617
|
-
|
|
618
|
-
```typescript
|
|
619
|
-
const paymentRoute = agent.createRoute({
|
|
620
|
-
title: "Payment Processing",
|
|
621
|
-
when: ["User wants to make a payment or purchase"],
|
|
622
|
-
skipIf: [
|
|
623
|
-
"Payment system is temporarily unavailable", // AI context
|
|
624
|
-
(ctx) => ctx.context?.paymentSystemDown === true, // System check
|
|
625
|
-
(ctx) => ctx.data?.paymentBlocked === true // User-specific block
|
|
626
|
-
],
|
|
627
|
-
initialStep: {
|
|
628
|
-
prompt: "I'll help you with your payment.",
|
|
629
|
-
},
|
|
630
|
-
});
|
|
631
|
-
|
|
632
|
-
const maintenanceRoute = agent.createRoute({
|
|
633
|
-
title: "Maintenance Notice",
|
|
634
|
-
when: "User asks about system issues or downtime",
|
|
635
|
-
skipIf: (ctx) => ctx.context?.maintenanceMode !== true, // Only show during maintenance
|
|
636
|
-
initialStep: {
|
|
637
|
-
prompt: "We're currently performing scheduled maintenance. Service will resume shortly.",
|
|
638
|
-
},
|
|
639
|
-
});
|
|
640
|
-
```
|
|
641
|
-
|
|
642
|
-
### Testing Your Conditions
|
|
643
|
-
|
|
644
|
-
```typescript
|
|
645
|
-
async function testConditions() {
|
|
646
|
-
// Test with different contexts
|
|
647
|
-
const basicUser = { userTier: 'basic', supportTickets: 1, accountAge: 30 };
|
|
648
|
-
const premiumUser = { userTier: 'premium', supportTickets: 5, accountAge: 365 };
|
|
649
|
-
|
|
650
|
-
// Basic user - should get standard support
|
|
651
|
-
const response1 = await agent.respond("I need help", {
|
|
652
|
-
contextOverride: basicUser
|
|
653
|
-
});
|
|
654
|
-
console.log("Basic user route:", response1.session?.currentRoute?.title);
|
|
655
|
-
|
|
656
|
-
// Premium user with multiple tickets - should get escalated support
|
|
657
|
-
const response2 = await agent.respond("I'm having issues again", {
|
|
658
|
-
contextOverride: premiumUser
|
|
659
|
-
});
|
|
660
|
-
console.log("Premium user route:", response2.session?.currentRoute?.title);
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
testConditions();
|
|
664
|
-
```
|
|
665
|
-
|
|
666
|
-
**Key Benefits:**
|
|
667
|
-
|
|
668
|
-
- ✅ **Simple strings** for AI-driven routing decisions
|
|
669
|
-
- ✅ **Functions** for precise programmatic control
|
|
670
|
-
- ✅ **Arrays** to combine both approaches
|
|
671
|
-
- ✅ **Route skipIf** for dynamic exclusion
|
|
672
|
-
- ✅ **Context access** in all condition types
|
|
673
|
-
|
|
674
|
-
---
|
|
675
|
-
|
|
676
|
-
## 💾 Adding Session Persistence (5 minutes)
|
|
677
|
-
|
|
678
|
-
Make your agent remember conversations across sessions:
|
|
679
|
-
|
|
680
|
-
```typescript
|
|
681
|
-
import { MemoryAdapter } from "@falai/agent";
|
|
682
|
-
|
|
683
|
-
const agent = new Agent({
|
|
684
|
-
name: "PersistentAgent",
|
|
685
|
-
provider: new GeminiProvider({
|
|
686
|
-
apiKey: process.env.GEMINI_API_KEY!,
|
|
687
|
-
}),
|
|
688
|
-
// Add persistence
|
|
689
|
-
persistence: {
|
|
690
|
-
adapter: new MemoryAdapter(), // Or RedisAdapter, PrismaAdapter, etc.
|
|
691
|
-
},
|
|
692
|
-
});
|
|
693
|
-
|
|
694
|
-
// Create agent with automatic session management and persistence
|
|
695
|
-
const persistentAgent = new Agent({
|
|
696
|
-
name: "PersistentAgent",
|
|
697
|
-
provider: new GeminiProvider({
|
|
698
|
-
apiKey: process.env.GEMINI_API_KEY!,
|
|
699
|
-
}),
|
|
700
|
-
persistence: {
|
|
701
|
-
adapter: new MemoryAdapter(), // Or RedisAdapter, PrismaAdapter, etc.
|
|
702
|
-
},
|
|
703
|
-
sessionId: "user-123", // Automatically loads or creates this session
|
|
704
|
-
});
|
|
705
|
-
|
|
706
|
-
// Sessions are automatically saved and restored
|
|
707
|
-
const response1 = await persistentAgent.respond("Hello, my name is Alice");
|
|
708
|
-
// Session data automatically persisted
|
|
709
|
-
|
|
710
|
-
const response2 = await persistentAgent.respond("What's my name?");
|
|
711
|
-
console.log(response2.message); // Agent remembers: "Your name is Alice"
|
|
712
|
-
```
|
|
713
|
-
|
|
714
|
-
---
|
|
715
|
-
|
|
716
|
-
## 🎯 Next Steps
|
|
717
|
-
|
|
718
|
-
### Level 2: Core Concepts
|
|
719
|
-
|
|
720
|
-
- **[Schema-Driven Extraction](../../../examples/core-concepts/schema-driven-extraction.ts)** - Advanced data collection patterns
|
|
721
|
-
- **[Session Management](../../../examples/core-concepts/session-management.ts)** - Multi-turn conversations
|
|
722
|
-
- **[Modern Streaming API](../../../examples/core-concepts/modern-streaming-api.ts)** - Streaming with the modern API
|
|
723
|
-
|
|
724
|
-
### Level 3: Conversation Flows
|
|
725
|
-
|
|
726
|
-
- **[Completion Transitions](../../../examples/conversation-flows/completion-transitions.ts)** - Route completion and transitions
|
|
727
|
-
|
|
728
|
-
### Level 4: Advanced Features
|
|
729
|
-
|
|
730
|
-
- **[Knowledge-Based Agent](../../../examples/advanced-patterns/knowledge-based-agent.ts)** - Domain-specific knowledge bases
|
|
731
|
-
- **[Route Lifecycle Hooks](../../../examples/advanced-patterns/route-lifecycle-hooks.ts)** - Custom route behavior
|
|
732
|
-
|
|
733
|
-
### Level 5: Production
|
|
734
|
-
|
|
735
|
-
- **[Server Session Management](../../../examples/integrations/server-session-management.ts)** - Server-side sessions
|
|
736
|
-
- **[Database Persistence](../../../examples/persistence/custom-adapter.ts)** - Custom storage adapters
|
|
737
|
-
- **[Streaming Responses](../../../examples/advanced-patterns/streaming-responses.ts)** - Real-time UX
|
|
738
|
-
|
|
739
|
-
---
|
|
740
|
-
|
|
741
|
-
## 🆘 Troubleshooting
|
|
742
|
-
|
|
743
|
-
### Common Issues
|
|
744
|
-
|
|
745
|
-
**"API key not found"**
|
|
746
|
-
|
|
747
|
-
```bash
|
|
748
|
-
# Make sure your .env file exists and has the correct variable
|
|
749
|
-
echo "GEMINI_API_KEY=your_key_here" > .env
|
|
750
|
-
```
|
|
751
|
-
|
|
752
|
-
**"Module not found"**
|
|
753
|
-
|
|
754
|
-
```bash
|
|
755
|
-
# Reinstall dependencies
|
|
756
|
-
rm -rf node_modules package-lock.json
|
|
757
|
-
bun install # or npm install
|
|
758
|
-
```
|
|
759
|
-
|
|
760
|
-
**"Type errors"**
|
|
761
|
-
|
|
762
|
-
```typescript
|
|
763
|
-
// Make sure you're using TypeScript 5.3+
|
|
764
|
-
npx tsc --version
|
|
765
|
-
|
|
766
|
-
// Or use tsx for running TypeScript directly
|
|
767
|
-
npx tsx your-file.ts
|
|
768
|
-
```
|
|
769
|
-
|
|
770
|
-
**"Agent not responding"**
|
|
771
|
-
|
|
772
|
-
```typescript
|
|
773
|
-
// Check your API key is valid
|
|
774
|
-
curl "https://generativelanguage.googleapis.com/v1beta/models?key=YOUR_KEY"
|
|
775
|
-
|
|
776
|
-
// Verify your provider configuration
|
|
777
|
-
console.log("Provider:", agent.options.provider.name);
|
|
778
|
-
```
|
|
779
|
-
|
|
780
|
-
### Getting Help
|
|
781
|
-
|
|
782
|
-
- 📖 **[Full Documentation](../../README.md)** - Complete API reference
|
|
783
|
-
- 💬 **[Examples Directory](../../../examples/)** - Working code samples
|
|
784
|
-
- 🐛 **[GitHub Issues](https://github.com/falai-dev/agent/issues)** - Report bugs
|
|
785
|
-
- 💡 **[Discussions](https://github.com/falai-dev/agent/discussions)** - Ask questions
|
|
786
|
-
|
|
787
|
-
---
|
|
788
|
-
|
|
789
|
-
## 🎉 You're Done!
|
|
790
|
-
|
|
791
|
-
You now have the foundation to build sophisticated AI agents. The framework is designed to scale with your needs - from simple chatbots to complex, data-driven conversational applications.
|
|
792
|
-
|
|
793
|
-
**What's next?** Explore the examples directory to see more advanced patterns, or dive into the API documentation for detailed method references.
|
|
794
|
-
|
|
795
|
-
Happy building! 🚀
|