@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
package/README.md
CHANGED
|
@@ -1,927 +1,81 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
|
|
3
|
-
#
|
|
3
|
+
# @falai/agent
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**Typed conversations where code stays in charge.**
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Define flows, steps, and tools in TypeScript; the framework calls the LLM only for the parts that need language โ routing, extraction, and generation.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
[](https://www.typescriptlang.org/)
|
|
9
|
+
[](https://www.npmjs.com/package/@falai/agent)
|
|
12
10
|
[](./LICENSE)
|
|
13
|
-
[](https://falai.dev)
|
|
15
|
-
|
|
16
|
-
[๐ Website](https://falai.dev) โข [Features](#-features) โข [Installation](#-installation) โข [Quick Start](#-quick-start) โข [Documentation](#-documentation) โข [Examples](#-examples)
|
|
11
|
+
[](https://www.typescriptlang.org/)
|
|
17
12
|
|
|
18
13
|
</div>
|
|
19
14
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
## โก The @falai/agent Difference
|
|
23
|
-
|
|
24
|
-
### Traditional AI Chat:
|
|
15
|
+
> The AI understands. The code is in control.
|
|
25
16
|
|
|
26
17
|
```typescript
|
|
27
|
-
|
|
28
|
-
// AI: "Sure! Which hotel would you like?" // ๐ Asked already!
|
|
29
|
-
// User: "Grand Hotel"
|
|
30
|
-
// AI: "How many guests?" // ๐ You just told me!
|
|
31
|
-
// User: "2 people"
|
|
32
|
-
// AI: "What date?" // ๐ I said Friday!
|
|
33
|
-
```
|
|
18
|
+
import { createAgent, GeminiProvider } from "@falai/agent";
|
|
34
19
|
|
|
35
|
-
|
|
20
|
+
const agent = createAgent({
|
|
21
|
+
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY! }),
|
|
22
|
+
schema: { type: "object", properties: { name: { type: "string" } } },
|
|
23
|
+
flows: [{
|
|
24
|
+
title: "Greet",
|
|
25
|
+
requiredFields: ["name"],
|
|
26
|
+
steps: [{ id: "ask_name", prompt: "What's your name?", collect: ["name"] }],
|
|
27
|
+
}],
|
|
28
|
+
});
|
|
36
29
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// AI: "Perfect! Booking confirmed for 2 guests at Grand Hotel on Friday!"
|
|
40
|
-
// โ
Extracted all data from one message
|
|
41
|
-
// โ
Skipped unnecessary steps
|
|
42
|
-
// โ
Completed immediately
|
|
30
|
+
const response = await agent.respond("Hi, I'm Alice");
|
|
31
|
+
console.log(response.message);
|
|
43
32
|
```
|
|
44
33
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
**Intelligent Pre-Extraction** - AI automatically captures ALL relevant data from user messages, then determines which step to start at based on what's missing.
|
|
48
|
-
|
|
49
|
-
---
|
|
50
|
-
|
|
51
|
-
## ๐ค Why @falai/agent?
|
|
52
|
-
|
|
53
|
-
After building production AI applications, we found existing solutions either:
|
|
54
|
-
|
|
55
|
-
- **Too unpredictable** - AI decides everything, including which tools to call (unreliable in production)
|
|
56
|
-
- **Too complex** - Heavy Python frameworks with massive dependencies
|
|
57
|
-
- **Too basic** - No structured data extraction or step management
|
|
58
|
-
|
|
59
|
-
@falai/agent gives you **predictable AI** - the creativity of LLMs with the reliability of code.
|
|
60
|
-
|
|
61
|
-
**The key insight:** Let AI do what it's good at (understanding intent, generating responses, extracting data), and let TypeScript handle the rest (step logic, tool execution, validation).
|
|
62
|
-
|
|
63
|
-
---
|
|
64
|
-
|
|
65
|
-
## ๐ Features
|
|
66
|
-
|
|
67
|
-
<table>
|
|
68
|
-
<tr>
|
|
69
|
-
<td width="50%">
|
|
70
|
-
|
|
71
|
-
### ๐ฏ **Developer Experience**
|
|
72
|
-
|
|
73
|
-
- **Fully Type-Safe** - Generic `Agent<TContext>` with complete inference
|
|
74
|
-
- **Fluent API** - Chainable methods for elegant code
|
|
75
|
-
- **Modular Design** - Use what you need, when you need it
|
|
76
|
-
|
|
77
|
-
</td>
|
|
78
|
-
<td width="50%">
|
|
79
|
-
|
|
80
|
-
### ๐ **Production Ready**
|
|
81
|
-
|
|
82
|
-
- **Robust Retry Logic** - Exponential backoff & backup models
|
|
83
|
-
- **AI Provider Strategy** - Pluggable backends (Claude, Gemini, OpenAI, OpenRouter)
|
|
84
|
-
- **Prompt Composition** - Sophisticated prompt building system
|
|
85
|
-
|
|
86
|
-
</td>
|
|
87
|
-
</tr>
|
|
88
|
-
<tr>
|
|
89
|
-
<td width="50%">
|
|
90
|
-
|
|
91
|
-
### ๐ค๏ธ **Data-Driven Conversations**
|
|
92
|
-
|
|
93
|
-
- **Intelligent Pre-Extraction** - Automatically extract data BEFORE entering steps
|
|
94
|
-
- **Schema-First Extraction** - Define data contracts with JSON Schema
|
|
95
|
-
- **Automatic Route Completion** - Routes complete when required fields are collected
|
|
96
|
-
- **Smart Step Skipping** - Skip steps whose data is already present
|
|
97
|
-
- **Always-On Routing** - Context-aware routing between different flows
|
|
98
|
-
|
|
99
|
-
</td>
|
|
100
|
-
<td width="50%">
|
|
101
|
-
|
|
102
|
-
### ๐ง **Tools & Data Integration**
|
|
34
|
+
## Primitives
|
|
103
35
|
|
|
104
|
-
|
|
105
|
-
- **Dynamic Tool Calling** - AI can call tools during streaming responses
|
|
106
|
-
- **Tool Result Processing** - Tools update context and collected data automatically
|
|
107
|
-
- **EnhancedTool Interface** - Optional concurrency, validation, and permission metadata
|
|
36
|
+
### Agent
|
|
108
37
|
|
|
109
|
-
|
|
110
|
-
</tr>
|
|
111
|
-
<tr>
|
|
112
|
-
<td width="50%">
|
|
38
|
+
**Bind schema to provider.**
|
|
113
39
|
|
|
114
|
-
|
|
40
|
+
The top-level object. Owns the schema, the provider, the flows, the tools, and the session. One agent serves many conversations.
|
|
115
41
|
|
|
116
|
-
|
|
117
|
-
- **Extensible Adapters** - Use built-in (Prisma, Redis, etc.) or create your own
|
|
118
|
-
- **Custom DB Support** - Integrate with your existing database schemas
|
|
42
|
+
### Flow
|
|
119
43
|
|
|
120
|
-
|
|
121
|
-
<td width="50%">
|
|
44
|
+
**Collect until done.**
|
|
122
45
|
|
|
123
|
-
|
|
46
|
+
A goal made of ordered steps. Declares which schema fields it needs; the engine completes the flow when every required field has been collected.
|
|
124
47
|
|
|
125
|
-
|
|
126
|
-
- **Context Awareness** - Router sees current progress and collected data
|
|
48
|
+
### Tool
|
|
127
49
|
|
|
128
|
-
|
|
129
|
-
</tr>
|
|
130
|
-
<tr>
|
|
131
|
-
<td width="50%">
|
|
50
|
+
**Run code, redirect flow.**
|
|
132
51
|
|
|
133
|
-
|
|
52
|
+
A typed function the AI can call. Receives a `ToolContext` with session data, can dispatch directives to redirect the conversation, and returns a typed result.
|
|
134
53
|
|
|
135
|
-
|
|
136
|
-
- **Streaming Tool Execution** - Tools begin executing as they arrive from the LLM stream with concurrency control
|
|
137
|
-
- **Context Compaction** - Automatic conversation history management when approaching token limits
|
|
138
|
-
- **Enhanced Tool Metadata** - Rich tool annotations for concurrency, permissions, validation, and result budgeting
|
|
139
|
-
- **Prompt Section Caching** - Memoize static prompt sections across turns for faster prompt generation with native history format
|
|
140
|
-
- **Lifecycle Hooks** - `prepare`/`finalize` functions or tools on steps, context/data update hooks
|
|
141
|
-
- **Sequential Steps** - Define linear conversation flows with `steps` array
|
|
142
|
-
- **Route Transitions** - Automatic flow transitions when routes complete
|
|
143
|
-
- **Smart Step Control** - `skipIf` and `requires` for data-driven flow control
|
|
54
|
+
## Where to go next
|
|
144
55
|
|
|
145
|
-
|
|
146
|
-
|
|
56
|
+
- **Build your first agent** โ [docs/start/01-install.md](./docs/start/01-install.md)
|
|
57
|
+
- **Explore the docs** โ [docs/](./docs/README.md)
|
|
58
|
+
- **Examples** โ [examples/](./examples/)
|
|
59
|
+
- **Migration from v1** โ [docs/migration/v1-to-v2.md](./docs/migration/v1-to-v2.md)
|
|
147
60
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
- **Guidelines & Rules** - Define agent behavior patterns and restrictions
|
|
151
|
-
- **Route-Specific Logic** - Different rules for different conversation contexts
|
|
152
|
-
- **Knowledge Base** - Structured information available to AI during responses
|
|
153
|
-
- **Session Step** - Track conversation progress across turns
|
|
154
|
-
|
|
155
|
-
</td>
|
|
156
|
-
</tr>
|
|
157
|
-
</table>
|
|
158
|
-
|
|
159
|
-
---
|
|
160
|
-
|
|
161
|
-
## ๐ฆ Installation
|
|
61
|
+
## Install
|
|
162
62
|
|
|
163
63
|
```bash
|
|
164
|
-
# Using bun (recommended)
|
|
165
64
|
bun add @falai/agent
|
|
166
|
-
|
|
167
|
-
# Using npm
|
|
65
|
+
# or
|
|
168
66
|
npm install @falai/agent
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
yarn add @falai/agent
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
> **Requirements:** Node.js 18+ or Bun 1.0+
|
|
175
|
-
|
|
176
|
-
---
|
|
177
|
-
|
|
178
|
-
## ๐ Quick Start
|
|
179
|
-
|
|
180
|
-
### Level 1: Your First Agent (30 seconds)
|
|
181
|
-
|
|
182
|
-
Create a minimal conversational agent:
|
|
183
|
-
|
|
184
|
-
```typescript
|
|
185
|
-
import {
|
|
186
|
-
Agent,
|
|
187
|
-
GeminiProvider,
|
|
188
|
-
} from "@falai/agent";
|
|
189
|
-
|
|
190
|
-
// Create your agent
|
|
191
|
-
const agent = new Agent({
|
|
192
|
-
name: "Assistant",
|
|
193
|
-
description: "A helpful assistant",
|
|
194
|
-
provider: new GeminiProvider({
|
|
195
|
-
apiKey: process.env.GEMINI_API_KEY!,
|
|
196
|
-
model: "models/gemini-2.5-flash",
|
|
197
|
-
}),
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
// Create a simple route with sequential steps
|
|
201
|
-
agent.createRoute({
|
|
202
|
-
title: "General Help",
|
|
203
|
-
description: "Answers user questions",
|
|
204
|
-
when: ["User needs help or asks a question"],
|
|
205
|
-
steps: [
|
|
206
|
-
{
|
|
207
|
-
id: "answer_question",
|
|
208
|
-
description: "Answer the user's question helpfully",
|
|
209
|
-
prompt: "Answer the user's question helpfully",
|
|
210
|
-
},
|
|
211
|
-
],
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
// Start chatting - simple message-based API
|
|
215
|
-
const response = await agent.respond("What can you do?");
|
|
216
|
-
|
|
217
|
-
console.log(response.message);
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
**That's it!** You now have a working conversational AI agent.
|
|
221
|
-
|
|
222
|
-
---
|
|
223
|
-
|
|
224
|
-
## ๐ฏ Advanced Routing with ConditionTemplate
|
|
225
|
-
|
|
226
|
-
### Flexible Route Conditions
|
|
227
|
-
|
|
228
|
-
Routes now support powerful condition patterns that combine AI context with programmatic logic:
|
|
229
|
-
|
|
230
|
-
```typescript
|
|
231
|
-
// String-only conditions (AI context for routing decisions)
|
|
232
|
-
agent.createRoute({
|
|
233
|
-
title: "Customer Support",
|
|
234
|
-
when: "User needs help with their account",
|
|
235
|
-
steps: [/* ... */]
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
// Function-only conditions (programmatic evaluation)
|
|
239
|
-
agent.createRoute({
|
|
240
|
-
title: "Premium Features",
|
|
241
|
-
when: (ctx) => ctx.data?.userType === 'premium',
|
|
242
|
-
steps: [/* ... */]
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
// Mixed array conditions (AI context + programmatic logic)
|
|
246
|
-
agent.createRoute({
|
|
247
|
-
title: "Booking Assistance",
|
|
248
|
-
when: [
|
|
249
|
-
"User wants to make a reservation", // AI context
|
|
250
|
-
(ctx) => ctx.data?.isLoggedIn === true // Programmatic check
|
|
251
|
-
],
|
|
252
|
-
steps: [/* ... */]
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
// Route skipIf - exclude routes dynamically
|
|
256
|
-
agent.createRoute({
|
|
257
|
-
title: "Payment Processing",
|
|
258
|
-
when: ["User wants to make a payment"],
|
|
259
|
-
skipIf: [
|
|
260
|
-
"Payment system is under maintenance", // AI context
|
|
261
|
-
(ctx) => ctx.data?.paymentBlocked === true // Programmatic check
|
|
262
|
-
],
|
|
263
|
-
steps: [/* ... */]
|
|
264
|
-
});
|
|
265
|
-
```
|
|
266
|
-
|
|
267
|
-
### Enhanced Step Conditions
|
|
268
|
-
|
|
269
|
-
Steps support the same flexible condition patterns:
|
|
270
|
-
|
|
271
|
-
```typescript
|
|
272
|
-
agent.createRoute({
|
|
273
|
-
title: "Order Process",
|
|
274
|
-
steps: [
|
|
275
|
-
{
|
|
276
|
-
id: "collect_items",
|
|
277
|
-
when: "User wants to add items to cart",
|
|
278
|
-
prompt: "What would you like to order?",
|
|
279
|
-
collect: ["items"]
|
|
280
|
-
},
|
|
281
|
-
{
|
|
282
|
-
id: "payment_step",
|
|
283
|
-
when: [
|
|
284
|
-
"Ready to process payment", // AI context
|
|
285
|
-
(ctx) => ctx.data?.items?.length > 0 // Programmatic check
|
|
286
|
-
],
|
|
287
|
-
skipIf: (ctx) => ctx.data?.paymentComplete === true,
|
|
288
|
-
prompt: "Let's process your payment",
|
|
289
|
-
tools: ["process_payment"]
|
|
290
|
-
},
|
|
291
|
-
{
|
|
292
|
-
id: "confirmation",
|
|
293
|
-
when: "Order is ready for confirmation",
|
|
294
|
-
skipIf: [
|
|
295
|
-
"Order already confirmed", // AI context
|
|
296
|
-
(ctx) => ctx.data?.orderConfirmed === true // Programmatic check
|
|
297
|
-
],
|
|
298
|
-
prompt: "Your order is confirmed!",
|
|
299
|
-
finalize: "send_confirmation_email"
|
|
300
|
-
}
|
|
301
|
-
]
|
|
302
|
-
});
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
### Dynamic Guidelines
|
|
306
|
-
|
|
307
|
-
Guidelines now support flexible conditions for context-aware behavior:
|
|
308
|
-
|
|
309
|
-
```typescript
|
|
310
|
-
// Add guidelines with mixed condition types
|
|
311
|
-
agent.addGuideline({
|
|
312
|
-
title: "Premium User Support",
|
|
313
|
-
condition: [
|
|
314
|
-
"User is asking for help", // AI context
|
|
315
|
-
(ctx) => ctx.data?.userType === 'premium' // Programmatic check
|
|
316
|
-
],
|
|
317
|
-
content: "Provide priority support with detailed explanations and offer direct phone support."
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
agent.addGuideline({
|
|
321
|
-
title: "Maintenance Mode",
|
|
322
|
-
condition: "System maintenance is active",
|
|
323
|
-
content: "Inform users about scheduled maintenance and provide estimated completion time."
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
// Function-only guideline for specific conditions
|
|
327
|
-
agent.addGuideline({
|
|
328
|
-
title: "High Value Customer",
|
|
329
|
-
condition: (ctx) => ctx.data?.totalSpent > 10000,
|
|
330
|
-
content: "Offer VIP treatment and exclusive deals."
|
|
331
|
-
});
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
**Key Benefits:**
|
|
335
|
-
|
|
336
|
-
- โ
**Hybrid Logic** - Combine AI understanding with programmatic precision
|
|
337
|
-
- โ
**Context Awareness** - AI sees string conditions for better routing decisions
|
|
338
|
-
- โ
**Performance** - Functions execute first, strings only used when needed
|
|
339
|
-
- โ
**Flexibility** - Use simple strings, functions, or arrays as needed
|
|
340
|
-
|
|
341
|
-
---
|
|
342
|
-
|
|
343
|
-
## ๐ง Advanced Step Configuration
|
|
344
|
-
|
|
345
|
-
### Simple Tool Creation
|
|
346
|
-
|
|
347
|
-
Create tools with minimal boilerplate using the unified Tool interface:
|
|
348
|
-
|
|
349
|
-
```typescript
|
|
350
|
-
// Create a simple tool with the unified interface
|
|
351
|
-
agent.addTool({
|
|
352
|
-
id: "validate_user",
|
|
353
|
-
name: "User Data Validator",
|
|
354
|
-
description: "Validate user data before processing",
|
|
355
|
-
parameters: { type: "object", properties: {} },
|
|
356
|
-
handler: async ({ context, data, updateData }) => {
|
|
357
|
-
// Validation logic with helper methods
|
|
358
|
-
if (!data.email?.includes("@")) {
|
|
359
|
-
throw new Error("Invalid email address");
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// Mark as validated using helper method
|
|
363
|
-
await updateData({ emailValidated: true });
|
|
364
|
-
|
|
365
|
-
return "User validation completed successfully";
|
|
366
|
-
},
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
// Use tools in conversation flows and step lifecycle
|
|
370
|
-
agent.createRoute({
|
|
371
|
-
title: "User Registration",
|
|
372
|
-
steps: [
|
|
373
|
-
{
|
|
374
|
-
id: "collect_info",
|
|
375
|
-
description: "Collect user information",
|
|
376
|
-
collect: ["name", "email"],
|
|
377
|
-
prompt: "Please provide your name and email.",
|
|
378
|
-
prepare: "validate_user", // Tool executes before AI response
|
|
379
|
-
tools: ["validate_user"], // Tool available during conversation
|
|
380
|
-
},
|
|
381
|
-
],
|
|
382
|
-
});
|
|
383
|
-
```
|
|
384
|
-
|
|
385
|
-
**Benefits:**
|
|
386
|
-
|
|
387
|
-
- โ
**Simple API** - Unified Tool interface with minimal complexity
|
|
388
|
-
- โ
**Type Safety** - Full TypeScript support with automatic inference
|
|
389
|
-
- โ
**Flexible Returns** - Return simple values or complex ToolResult objects
|
|
390
|
-
- โ
**Helper Methods** - Built-in context and data update utilities
|
|
391
|
-
- โ
**Lifecycle Integration** - Use tools as prepare/finalize hooks in steps
|
|
392
|
-
|
|
393
|
-
---
|
|
394
|
-
|
|
395
|
-
### Level 2: Data Extraction (The Real Power)
|
|
396
|
-
|
|
397
|
-
Now let's build an agent that intelligently collects structured data:
|
|
398
|
-
|
|
399
|
-
```typescript
|
|
400
|
-
import {
|
|
401
|
-
Agent,
|
|
402
|
-
OpenAIProvider,
|
|
403
|
-
} from "@falai/agent";
|
|
404
|
-
|
|
405
|
-
// 1๏ธโฃ Define the data you want to collect
|
|
406
|
-
interface HotelBookingData {
|
|
407
|
-
hotelName: string;
|
|
408
|
-
date: string;
|
|
409
|
-
guests: number;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// 2๏ธโฃ Create your agent with centralized data schema
|
|
413
|
-
const agent = new Agent<{}, HotelBookingData>({
|
|
414
|
-
name: "BookingBot",
|
|
415
|
-
description: "A hotel booking assistant that collects information.",
|
|
416
|
-
provider: new OpenAIProvider({
|
|
417
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
418
|
-
model: "gpt-4", // or your preferred model
|
|
419
|
-
}),
|
|
420
|
-
|
|
421
|
-
// Agent-level schema defines all possible data fields
|
|
422
|
-
schema: {
|
|
423
|
-
type: "object",
|
|
424
|
-
properties: {
|
|
425
|
-
hotelName: { type: "string", description: "The name of the hotel." },
|
|
426
|
-
date: { type: "string", description: "The desired booking date." },
|
|
427
|
-
guests: { type: "number", description: "The number of guests." },
|
|
428
|
-
},
|
|
429
|
-
required: ["hotelName", "date", "guests"],
|
|
430
|
-
},
|
|
431
|
-
|
|
432
|
-
// Agent-level data validation and enrichment
|
|
433
|
-
hooks: {
|
|
434
|
-
onDataUpdate: async (data, previousData) => {
|
|
435
|
-
// Auto-validate and enrich data
|
|
436
|
-
if (data.guests && data.guests > 10) {
|
|
437
|
-
throw new Error("Maximum 10 guests allowed");
|
|
438
|
-
}
|
|
439
|
-
return data;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
// 3๏ธโฃ Define a tool using the unified Tool interface
|
|
445
|
-
agent.addTool({
|
|
446
|
-
id: "book_hotel",
|
|
447
|
-
name: "Hotel Booking System",
|
|
448
|
-
description: "Books a hotel once all information is collected.",
|
|
449
|
-
parameters: { type: "object", properties: {} },
|
|
450
|
-
handler: async ({ context, data, updateContext }) => {
|
|
451
|
-
// Tool receives complete agent data with simplified context and helper methods
|
|
452
|
-
const bookingId = await hotelAPI.createBooking({
|
|
453
|
-
hotel: data.hotelName,
|
|
454
|
-
date: data.date,
|
|
455
|
-
guests: data.guests,
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
// Use helper method to update context
|
|
459
|
-
await updateContext({
|
|
460
|
-
lastBookingId: bookingId,
|
|
461
|
-
lastBookingDate: new Date().toISOString(),
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
return `Booking confirmed! Confirmation #${bookingId} for ${data.guests} guests at ${data.hotelName} on ${data.date}`;
|
|
465
|
-
},
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
// 4๏ธโฃ Create a route with required fields specification
|
|
469
|
-
agent.createRoute({
|
|
470
|
-
title: "Book Hotel",
|
|
471
|
-
description: "Guides the user through the hotel booking process.",
|
|
472
|
-
when: ["User wants to book a hotel"],
|
|
473
|
-
requiredFields: ["hotelName", "date", "guests"], // Required for route completion
|
|
474
|
-
|
|
475
|
-
// 5๏ธโฃ Define the flow to collect data step-by-step
|
|
476
|
-
steps: [
|
|
477
|
-
{
|
|
478
|
-
id: "ask_hotel",
|
|
479
|
-
description: "Ask which hotel they want to book",
|
|
480
|
-
prompt: "Which hotel would you like to book?",
|
|
481
|
-
collect: ["hotelName"],
|
|
482
|
-
skipIf: (data: Partial<HotelBookingData>) => !!data.hotelName,
|
|
483
|
-
},
|
|
484
|
-
{
|
|
485
|
-
id: "ask_date",
|
|
486
|
-
description: "Ask for the booking date",
|
|
487
|
-
prompt: "What date would you like to book for?",
|
|
488
|
-
collect: ["date"],
|
|
489
|
-
requires: ["hotelName"], // Prerequisites from agent data
|
|
490
|
-
skipIf: (data: Partial<HotelBookingData>) => !!data.date,
|
|
491
|
-
},
|
|
492
|
-
{
|
|
493
|
-
id: "ask_guests",
|
|
494
|
-
description: "Ask for the number of guests",
|
|
495
|
-
prompt: "How many guests will be staying?",
|
|
496
|
-
collect: ["guests"],
|
|
497
|
-
requires: ["hotelName", "date"], // Prerequisites from agent data
|
|
498
|
-
skipIf: (data: Partial<HotelBookingData>) => data.guests !== undefined,
|
|
499
|
-
},
|
|
500
|
-
{
|
|
501
|
-
id: "confirm_booking",
|
|
502
|
-
description: "Confirm and book the hotel",
|
|
503
|
-
prompt: "Let me confirm your booking details.",
|
|
504
|
-
tools: ["book_hotel"], // Reference tool by ID
|
|
505
|
-
requires: ["hotelName", "date", "guests"],
|
|
506
|
-
},
|
|
507
|
-
],
|
|
508
|
-
});
|
|
509
|
-
|
|
510
|
-
// 5๏ธโฃ Start conversing - simple message API
|
|
511
|
-
const response = await agent.respond("I want to book a room at the Grand Hotel for 2 people.");
|
|
512
|
-
|
|
513
|
-
// The agent sees that `hotelName` and `guests` are provided,
|
|
514
|
-
// skips the first and third steps, and only asks for the date.
|
|
515
|
-
console.log(response.message);
|
|
516
|
-
// Expected: "What date would you like to book for?"
|
|
67
|
+
# or
|
|
68
|
+
pnpm add @falai/agent
|
|
517
69
|
```
|
|
518
70
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
- โ
**Understand the Goal** - Route to the `Book Hotel` flow based on user intent.
|
|
522
|
-
- โ
**Extract Known Data** - Automatically pull `hotelName` and `guests` from the first message.
|
|
523
|
-
- โ
**Skip Unneeded Steps** - Use `skipIf` to bypass questions for data it already has.
|
|
524
|
-
- โ
**Collect Missing Data** - Intelligently ask only for the missing `date`.
|
|
525
|
-
- โ
**Execute Deterministically** - Call the `bookHotel` tool only when all required data is present.
|
|
526
|
-
|
|
527
|
-
This creates a flexible and natural conversation, guided by a clear data structure.
|
|
528
|
-
|
|
529
|
-
๐ **[See more examples โ](./examples/)** | **[Full tutorial โ](./docs/guides/getting-started/README.md)**
|
|
530
|
-
|
|
531
|
-
### โก Advanced Features
|
|
532
|
-
|
|
533
|
-
**Streaming responses** for real-time UX:
|
|
534
|
-
|
|
535
|
-
```typescript
|
|
536
|
-
for await (const chunk of agent.respondStream("Hello")) {
|
|
537
|
-
process.stdout.write(chunk.delta);
|
|
538
|
-
if (chunk.done) {
|
|
539
|
-
console.log("\nTool calls:", chunk.toolCalls);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
```
|
|
71
|
+
Requires Node 18+ or Bun 1.0+. Set a provider API key in your environment (for example `GEMINI_API_KEY`, `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, or `OPENROUTER_API_KEY`).
|
|
543
72
|
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
```typescript
|
|
547
|
-
import { userMessage, assistantMessage, historyToEvents } from "@falai/agent";
|
|
548
|
-
|
|
549
|
-
// Create history using helper functions
|
|
550
|
-
const history = [
|
|
551
|
-
userMessage("Hello, I need help booking a hotel"),
|
|
552
|
-
assistantMessage("I'd be happy to help! Which hotel would you like to book?"),
|
|
553
|
-
userMessage("The Grand Hotel please"),
|
|
554
|
-
];
|
|
555
|
-
|
|
556
|
-
// Pass history to agent
|
|
557
|
-
const response = await agent.respond("For 2 guests", { history });
|
|
558
|
-
|
|
559
|
-
// Or access session history
|
|
560
|
-
console.log(agent.session.history);
|
|
561
|
-
```
|
|
562
|
-
|
|
563
|
-
**Automatic session management** for multi-turn conversations:
|
|
564
|
-
|
|
565
|
-
```typescript
|
|
566
|
-
// Server-side: Create agent with sessionId
|
|
567
|
-
const agent = new Agent({
|
|
568
|
-
name: "Assistant",
|
|
569
|
-
provider: new OpenAIProvider({ apiKey: process.env.OPENAI_API_KEY }),
|
|
570
|
-
persistence: { adapter: new PrismaAdapter({ prisma }) },
|
|
571
|
-
sessionId: "user-123" // Automatically loads or creates session
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
// Simple conversation - no manual session management needed
|
|
575
|
-
const response = await agent.respond("Hello, how are you?");
|
|
576
|
-
console.log(response.message);
|
|
577
|
-
console.log(agent.session.id); // Session ID for client
|
|
578
|
-
```
|
|
579
|
-
|
|
580
|
-
**Automatic session persistence** with any adapter:
|
|
581
|
-
|
|
582
|
-
```typescript
|
|
583
|
-
import { PrismaAdapter } from "@falai/agent";
|
|
584
|
-
|
|
585
|
-
// Server endpoint - sessions managed automatically
|
|
586
|
-
app.post('/chat', async (req, res) => {
|
|
587
|
-
const { sessionId, message } = req.body;
|
|
588
|
-
|
|
589
|
-
const agent = new Agent({
|
|
590
|
-
name: "ChatBot",
|
|
591
|
-
provider: new OpenAIProvider({ apiKey: process.env.OPENAI_API_KEY }),
|
|
592
|
-
persistence: { adapter: new PrismaAdapter({ prisma }) },
|
|
593
|
-
sessionId // Automatically loads or creates this session
|
|
594
|
-
});
|
|
595
|
-
|
|
596
|
-
const response = await agent.respond(message);
|
|
597
|
-
|
|
598
|
-
res.json({
|
|
599
|
-
message: response.message,
|
|
600
|
-
sessionId: agent.session.id, // Return session ID to client
|
|
601
|
-
isComplete: response.isRouteComplete
|
|
602
|
-
});
|
|
603
|
-
});
|
|
604
|
-
```
|
|
605
|
-
|
|
606
|
-
๐ **[See full feature docs โ](./docs)**
|
|
607
|
-
|
|
608
|
-
---
|
|
609
|
-
|
|
610
|
-
## ๐ Documentation
|
|
611
|
-
|
|
612
|
-
๐ **[Complete Documentation Index โ](docs/README.md)** - Searchable index of all docs
|
|
613
|
-
|
|
614
|
-
### ๐ Getting Started
|
|
615
|
-
|
|
616
|
-
- **[Quick Start Guide](./docs/guides/getting-started/README.md)** - Build your first agent in 15 minutes
|
|
617
|
-
|
|
618
|
-
### ๐๏ธ Core Framework
|
|
619
|
-
|
|
620
|
-
- **[Agent Orchestration](./docs/core/agent/README.md)** - Agent lifecycle, configuration & hooks
|
|
621
|
-
- **[Context Management](./docs/core/agent/context-management.md)** - Dynamic context providers & updates
|
|
622
|
-
- **[Session Management](./docs/core/agent/session-management.md)** - Session persistence & state
|
|
623
|
-
|
|
624
|
-
### ๐ฌ Conversation Flows
|
|
625
|
-
|
|
626
|
-
- **[Routes](./docs/core/conversation-flows/routes.md)** - Route definition & lifecycle
|
|
627
|
-
- **[Steps](./docs/core/conversation-flows/steps.md)** - Step transitions & logic
|
|
628
|
-
|
|
629
|
-
### ๐๏ธ Architecture
|
|
630
|
-
|
|
631
|
-
- **[Multi-Step Execution](./docs/architecture/multi-step-execution.md)** - Batch execution architecture & flow
|
|
632
|
-
|
|
633
|
-
### ๐ค AI Integration
|
|
634
|
-
|
|
635
|
-
- **[AI Providers](./docs/core/ai-integration/providers.md)** - Gemini, OpenAI, Anthropic, OpenRouter
|
|
636
|
-
- **[Prompt Composition](./docs/core/ai-integration/prompt-composition/)** - How prompts are built
|
|
637
|
-
- **[Response Processing](./docs/core/ai-integration/response-processing/)** - Schema extraction & tool calls
|
|
638
|
-
|
|
639
|
-
### ๐ง Tools & Data
|
|
640
|
-
|
|
641
|
-
- **[Tool Definition](./docs/core/tools/tool-definition.md)** - Creating and configuring tools
|
|
642
|
-
- **[Tool Execution](./docs/core/tools/tool-execution.md)** - Dynamic tool calling and context updates
|
|
643
|
-
- **[Tool Scoping](./docs/core/tools/tool-scoping.md)** - Agent, route, and step-level tool management
|
|
644
|
-
- **[Streaming Tool Execution](./docs/core/tools/streaming-execution.md)** - Concurrency-controlled streaming execution
|
|
645
|
-
- **[EnhancedTool Reference](./docs/core/tools/enhanced-tool.md)** - Rich tool metadata interface
|
|
646
|
-
|
|
647
|
-
### ๐พ Persistence
|
|
648
|
-
|
|
649
|
-
- **[Session Storage](./docs/core/persistence/session-storage.md)** - Session persistence patterns
|
|
650
|
-
- **[Database Adapters](./docs/core/persistence/adapters.md)** - Built-in adapter configurations
|
|
651
|
-
|
|
652
|
-
### ๐ Advanced Guides
|
|
653
|
-
|
|
654
|
-
- **[Building Agents](./docs/guides/building-agents/)** - Complete agent construction patterns
|
|
655
|
-
- **[Advanced Patterns](./docs/guides/advanced-patterns/)** - Complex use cases & integrations
|
|
656
|
-
- **[Context Compaction](./docs/guides/context-compaction.md)** - Automatic conversation history management
|
|
657
|
-
- **[Prompt Optimization](./docs/guides/prompt-optimization.md)** - Prompt section caching and native history format
|
|
658
|
-
- **[Multi-Step Execution Migration](./docs/guides/migration/multi-step-execution.md)** - Migrating to batch execution
|
|
659
|
-
- **[API Reference](./docs/api/overview.md)** - Complete API documentation
|
|
660
|
-
|
|
661
|
-
---
|
|
662
|
-
|
|
663
|
-
## ๐ฏ Examples - By Domain
|
|
664
|
-
|
|
665
|
-
### ๐๏ธ Core Concepts
|
|
666
|
-
|
|
667
|
-
Fundamental patterns every agent needs:
|
|
668
|
-
|
|
669
|
-
- **[Basic Agent](./examples/core-concepts/basic-agent.ts)** - Minimal agent setup and configuration
|
|
670
|
-
- **[Schema-Driven Extraction](./examples/core-concepts/schema-driven-extraction.ts)** - Type-safe data collection with JSON Schema
|
|
671
|
-
- **[Session Management](./examples/core-concepts/session-management.ts)** - Multi-turn conversations with persistence
|
|
672
|
-
- **[Context Providers](./examples/core-concepts/context-providers.ts)** - Dynamic context fetching and updates
|
|
673
|
-
|
|
674
|
-
### ๐ฌ Conversation Flows
|
|
675
|
-
|
|
676
|
-
Building intelligent dialogue systems:
|
|
677
|
-
|
|
678
|
-
- **[Simple Route](./examples/conversation-flows/simple-route.ts)** - Basic route with linear step progression
|
|
679
|
-
- **[Data-Driven Flows](./examples/conversation-flows/data-driven-flows.ts)** - Conditional logic with skipIf and requires
|
|
680
|
-
- **[Conditional Branching](./examples/conversation-flows/conditional-branching.ts)** - AI-powered branching decisions
|
|
681
|
-
- **[Completion Transitions](./examples/conversation-flows/completion-transitions.ts)** - Route transitions when flows complete
|
|
682
|
-
|
|
683
|
-
### ๐ค AI Providers
|
|
684
|
-
|
|
685
|
-
Integrating different AI services:
|
|
686
|
-
|
|
687
|
-
- **[Gemini Integration](./examples/ai-providers/gemini-integration.ts)** - Google Gemini with advanced features
|
|
688
|
-
- **[OpenAI Integration](./examples/ai-providers/openai-integration.ts)** - GPT-4 and GPT-3.5 Turbo
|
|
689
|
-
- **[Anthropic Integration](./examples/ai-providers/anthropic-integration.ts)** - Claude with streaming and tool calling
|
|
690
|
-
- **[Custom Provider](./examples/ai-providers/custom-provider.ts)** - Build your own AI provider integration
|
|
691
|
-
|
|
692
|
-
### ๐พ Persistence
|
|
693
|
-
|
|
694
|
-
Session storage and data persistence:
|
|
695
|
-
|
|
696
|
-
- **[Memory Sessions](./examples/persistence/memory-sessions.ts)** - In-memory session management
|
|
697
|
-
- **[Redis Persistence](./examples/persistence/redis-persistence.ts)** - High-performance Redis storage
|
|
698
|
-
- **[Database Persistence](./examples/persistence/database-persistence.ts)** - SQL/NoSQL database integration
|
|
699
|
-
- **[Custom Adapter](./examples/persistence/custom-adapter.ts)** - Build custom persistence adapters
|
|
700
|
-
|
|
701
|
-
### ๐ง Tools
|
|
702
|
-
|
|
703
|
-
Tool creation and data manipulation:
|
|
704
|
-
|
|
705
|
-
- **[Basic Tools](./examples/tools/basic-tools.ts)** - Simple tool creation and execution
|
|
706
|
-
- **[Data Enrichment Tools](./examples/tools/data-enrichment-tools.ts)** - Tools that modify collected data
|
|
707
|
-
- **[Context Updating Tools](./examples/tools/context-updating-tools.ts)** - Tools that modify agent context
|
|
708
|
-
- **[Domain Scoped Tools](./examples/tools/domain-scoped-tools.ts)** - Tool security and access control
|
|
709
|
-
|
|
710
|
-
### ๐ Advanced Patterns
|
|
711
|
-
|
|
712
|
-
Complex use cases and integrations:
|
|
713
|
-
|
|
714
|
-
- **[Multi-Turn Conversations](./examples/advanced-patterns/multi-turn-conversations.ts)** - Complex dialogue flows with backtracking
|
|
715
|
-
- **[Streaming Responses](./examples/advanced-patterns/streaming-responses.ts)** - Real-time response streaming
|
|
716
|
-
- **[Route Lifecycle Hooks](./examples/advanced-patterns/route-lifecycle-hooks.ts)** - Custom route behavior
|
|
717
|
-
- **[Custom Response Schemas](./examples/advanced-patterns/custom-response-schemas.ts)** - Advanced schema patterns
|
|
718
|
-
|
|
719
|
-
### ๐ Integrations
|
|
720
|
-
|
|
721
|
-
External service integrations:
|
|
722
|
-
|
|
723
|
-
- **[Server Deployment](./examples/integrations/server-deployment.ts)** - HTTP API with WebSocket streaming
|
|
724
|
-
- **[Database Integration](./examples/integrations/database-integration.ts)** - Direct database access patterns
|
|
725
|
-
- **[Webhook Integration](./examples/integrations/webhook-integration.ts)** - HTTP webhook handling
|
|
726
|
-
- **[API Integration](./examples/integrations/api-integration.ts)** - External API calls and responses
|
|
727
|
-
|
|
728
|
-
๐ **[See all examples with detailed explanations โ](./examples/)**
|
|
729
|
-
|
|
730
|
-
---
|
|
731
|
-
|
|
732
|
-
## ๐๏ธ How It Works
|
|
733
|
-
|
|
734
|
-
`@falai/agent` uses a **schema-first, pipeline-driven architecture** with intelligent pre-extraction:
|
|
735
|
-
|
|
736
|
-
```
|
|
737
|
-
User Message + Session State
|
|
738
|
-
โ
|
|
739
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
740
|
-
โ 1. ROUTING + PRE-EXTRACTION โ
|
|
741
|
-
โ โข Evaluate routes (AI scoring) โ
|
|
742
|
-
โ โข Pre-extract data from message โ
|
|
743
|
-
โ โข Check route completion โ
|
|
744
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
745
|
-
โ
|
|
746
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
747
|
-
โ 2. SMART STEP SELECTION โ
|
|
748
|
-
โ โข Filter steps (skipIf, requires) โ
|
|
749
|
-
โ โข Skip steps with existing data โ
|
|
750
|
-
โ โข Select optimal next step โ
|
|
751
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
752
|
-
โ
|
|
753
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
754
|
-
โ 3. RESPONSE GENERATION โ
|
|
755
|
-
โ โข Build prompt with context โ
|
|
756
|
-
โ โข Generate AI response โ
|
|
757
|
-
โ โข Execute tools if needed โ
|
|
758
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
759
|
-
โ
|
|
760
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
761
|
-
โ 4. COMPLETION HANDLING โ
|
|
762
|
-
โ โข Auto-complete when data collected โ
|
|
763
|
-
โ โข Exclude completed routes โ
|
|
764
|
-
โ โข Generate completion message โ
|
|
765
|
-
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
766
|
-
โ
|
|
767
|
-
Response + Updated Session State
|
|
768
|
-
```
|
|
769
|
-
|
|
770
|
-
### Key Principles:
|
|
771
|
-
|
|
772
|
-
โ
**AI decides:** Route selection, data extraction, message generation, tool calling
|
|
773
|
-
โ
**Code decides:** Step flow control, route completion, lifecycle hooks, data validation
|
|
774
|
-
โ
**Result:** Efficient conversations that don't waste user time
|
|
775
|
-
|
|
776
|
-
### What Makes It Smart:
|
|
777
|
-
|
|
778
|
-
๐ฏ **Pre-Extraction** - Data extracted BEFORE entering steps (no repeated questions)
|
|
779
|
-
๐ **Auto-Completion** - Routes complete automatically when required fields are collected
|
|
780
|
-
๐ **Completion Protection** - Completed routes excluded from future selection
|
|
781
|
-
โก **Smart Skipping** - Steps bypassed if their data is already present
|
|
782
|
-
|
|
783
|
-
๐ **[Read the detailed architecture โ](./docs/architecture/data-extraction-flow.md)**
|
|
784
|
-
|
|
785
|
-
---
|
|
786
|
-
|
|
787
|
-
### 1๏ธโฃ General flowchart of @falai/agent
|
|
788
|
-
|
|
789
|
-
This is the **actual internal execution flow** when `agent.respond()` is called.
|
|
790
|
-
|
|
791
|
-
```mermaid
|
|
792
|
-
flowchart TD
|
|
793
|
-
A[User sends a message] --> B[Load session and state]
|
|
794
|
-
B --> C[Routing + Pre-Extraction]
|
|
795
|
-
|
|
796
|
-
C --> C1[AI evaluates available routes]
|
|
797
|
-
C --> C2[AI extracts schema data]
|
|
798
|
-
C --> C3[Updates data/context]
|
|
799
|
-
|
|
800
|
-
C --> D{Route found?}
|
|
801
|
-
D -- No --> D1[Generic response / fallback]
|
|
802
|
-
D -- Yes --> E{Is the route already complete?}
|
|
803
|
-
|
|
804
|
-
E -- Yes --> F[Execute finalize / tools]
|
|
805
|
-
F --> G[Completion message]
|
|
806
|
-
|
|
807
|
-
E -- No --> H[Smart Step Selection]
|
|
808
|
-
|
|
809
|
-
H --> H1[Filter steps: when]
|
|
810
|
-
H --> H2[Remove steps via skipIf]
|
|
811
|
-
H --> H3[Validate requires]
|
|
812
|
-
H --> H4[Select next step]
|
|
813
|
-
|
|
814
|
-
H4 --> I[Prepare hooks / tools]
|
|
815
|
-
I --> J[AI generates response]
|
|
816
|
-
J --> K{Tool called?}
|
|
817
|
-
|
|
818
|
-
K -- Yes --> K1[Execute tool]
|
|
819
|
-
K1 --> K2[Update data/context]
|
|
820
|
-
K2 --> L[Re-evaluate route]
|
|
821
|
-
|
|
822
|
-
K -- No --> L[Re-evaluate route]
|
|
823
|
-
|
|
824
|
-
L --> M{Required fields completed?}
|
|
825
|
-
M -- Yes --> F
|
|
826
|
-
M -- No --> N[Save session]
|
|
827
|
-
N --> O[Return response to user]
|
|
828
|
-
```
|
|
829
|
-
|
|
830
|
-
### ๐ง Mental model of the flow
|
|
831
|
-
|
|
832
|
-
* **AI decides**
|
|
833
|
-
โ intent, route, data extraction, tools
|
|
834
|
-
* **Code decides**
|
|
835
|
-
โ step order, skipping steps, completing routes
|
|
836
|
-
* **Nothing is random**
|
|
837
|
-
โ there is always a deterministic next step
|
|
838
|
-
|
|
839
|
-
---
|
|
840
|
-
|
|
841
|
-
## 2๏ธโฃ Data-Driven conversation flowchart (e.g. Booking)
|
|
842
|
-
|
|
843
|
-
This shows **why the framework does not ask repeated questions**.
|
|
844
|
-
|
|
845
|
-
```mermaid
|
|
846
|
-
flowchart TD
|
|
847
|
-
A[User: I want to book the Grand Hotel for 2 people] --> B[Pre-Extraction]
|
|
848
|
-
B --> C[Extracts: hotelName, guests]
|
|
849
|
-
C --> D{All required fields?}
|
|
850
|
-
|
|
851
|
-
D -- No --> E[Select next step]
|
|
852
|
-
E --> F{hotelName present?}
|
|
853
|
-
F -- Yes --> G[Skip step ask_hotel]
|
|
854
|
-
F -- No --> H[Ask hotel]
|
|
855
|
-
|
|
856
|
-
G --> I{date present?}
|
|
857
|
-
I -- No --> J[Ask date]
|
|
858
|
-
I -- Yes --> K[Skip step ask_date]
|
|
859
|
-
|
|
860
|
-
J --> L[User answers date]
|
|
861
|
-
L --> M[Extract date]
|
|
862
|
-
|
|
863
|
-
M --> N{All fields completed?}
|
|
864
|
-
N -- Yes --> O[Execute tool book_hotel]
|
|
865
|
-
O --> P[Confirmation message]
|
|
866
|
-
|
|
867
|
-
N -- No --> E
|
|
868
|
-
```
|
|
869
|
-
|
|
870
|
-
---
|
|
871
|
-
|
|
872
|
-
## ๐ Key concepts mapped in the flowchart
|
|
873
|
-
|
|
874
|
-
| README Concept | Where it appears |
|
|
875
|
-
| ----------------- | ------------------------------------ |
|
|
876
|
-
| Pre-Extraction | Before any step |
|
|
877
|
-
| skipIf | Smart Step Selection |
|
|
878
|
-
| requires | Validation before the step |
|
|
879
|
-
| requiredFields | Route completion check |
|
|
880
|
-
| Tools | During generation or finalize |
|
|
881
|
-
| Session-aware | State loaded at the beginning |
|
|
882
|
-
| Always-on routing | Route re-evaluated after each action |
|
|
883
|
-
|
|
884
|
-
---
|
|
885
|
-
|
|
886
|
-
## ๐ค Contributing
|
|
887
|
-
|
|
888
|
-
We welcome contributions! See our [Contributing Guide](./docs/CONTRIBUTING.md) for details on:
|
|
889
|
-
|
|
890
|
-
- ๐ Reporting bugs
|
|
891
|
-
- ๐ก Suggesting features
|
|
892
|
-
- ๐ Improving documentation
|
|
893
|
-
- ๐จ Submitting pull requests
|
|
894
|
-
|
|
895
|
-
## ๐ Inspired By
|
|
896
|
-
|
|
897
|
-
This framework draws inspiration from [**Parlant**](https://github.com/emcie-co/parlant) by Emcie Co., an excellent Python framework for conversational AI agents. We've adapted and enhanced these concepts for the TypeScript ecosystem with additional type safety and modern patterns.
|
|
898
|
-
|
|
899
|
-
---
|
|
900
|
-
|
|
901
|
-
## ๐ License
|
|
73
|
+
## License
|
|
902
74
|
|
|
903
75
|
MIT ยฉ 2025
|
|
904
76
|
|
|
905
|
-
---
|
|
906
|
-
|
|
907
77
|
<div align="center">
|
|
908
78
|
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
**Choose your path:**
|
|
912
|
-
|
|
913
|
-
๐ถ **New to AI agents?** โ [Quick Start Guide](./docs/guides/getting-started/README.md)
|
|
914
|
-
๐๏ธ **Building production app?** โ [Agent Architecture](./docs/core/agent/README.md)
|
|
915
|
-
๐ก **Have questions?** โ [Open a discussion](https://github.com/falai-dev/agent/discussions)
|
|
916
|
-
|
|
917
|
-
---
|
|
918
|
-
|
|
919
|
-
### โญ Star us on [GitHub](https://github.com/falai-dev/agent)
|
|
920
|
-
|
|
921
|
-
**Help us reach more developers building production AI!**
|
|
922
|
-
|
|
923
|
-
[Report Bug](https://github.com/falai-dev/agent/issues) โข [Request Feature](https://github.com/falai-dev/agent/issues) โข [Contribute](https://github.com/falai-dev/agent/pulls)
|
|
924
|
-
|
|
925
|
-
**Made with โค๏ธ for the community**
|
|
79
|
+
[falai.dev](https://falai.dev) ยท [GitHub](https://github.com/falai-dev/agent) ยท [Issues](https://github.com/falai-dev/agent/issues)
|
|
926
80
|
|
|
927
81
|
</div>
|