@falai/agent 0.8.1 → 0.9.0-alpha-2
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 +332 -147
- package/dist/{adapters → cjs/src/adapters}/MemoryAdapter.d.ts +4 -4
- package/dist/cjs/src/adapters/MemoryAdapter.d.ts.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/MemoryAdapter.js +41 -21
- package/dist/cjs/src/adapters/MemoryAdapter.js.map +1 -0
- package/dist/{adapters → cjs/src/adapters}/MongoAdapter.d.ts +3 -3
- package/dist/cjs/src/adapters/MongoAdapter.d.ts.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/MongoAdapter.js +2 -1
- package/dist/cjs/src/adapters/MongoAdapter.js.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/OpenSearchAdapter.d.ts +3 -3
- package/dist/cjs/src/adapters/OpenSearchAdapter.d.ts.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/OpenSearchAdapter.js +10 -13
- package/dist/cjs/src/adapters/OpenSearchAdapter.js.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/PostgreSQLAdapter.d.ts +3 -3
- package/dist/cjs/src/adapters/PostgreSQLAdapter.d.ts.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/PostgreSQLAdapter.js +1 -1
- package/dist/cjs/src/adapters/PostgreSQLAdapter.js.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/PrismaAdapter.d.ts +3 -3
- package/dist/cjs/src/adapters/PrismaAdapter.d.ts.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/PrismaAdapter.js +35 -5
- package/dist/cjs/src/adapters/PrismaAdapter.js.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/RedisAdapter.d.ts +3 -3
- package/dist/cjs/src/adapters/RedisAdapter.d.ts.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/RedisAdapter.js +3 -2
- package/dist/cjs/src/adapters/RedisAdapter.js.map +1 -0
- package/dist/{adapters → cjs/src/adapters}/SQLiteAdapter.d.ts +3 -3
- package/dist/cjs/src/adapters/SQLiteAdapter.d.ts.map +1 -0
- package/dist/cjs/{adapters → src/adapters}/SQLiteAdapter.js +2 -1
- package/dist/cjs/src/adapters/SQLiteAdapter.js.map +1 -0
- package/dist/cjs/src/adapters/index.d.ts.map +1 -0
- package/dist/cjs/src/adapters/index.js.map +1 -0
- package/dist/cjs/src/constants/index.d.ts.map +1 -0
- package/dist/cjs/src/constants/index.js.map +1 -0
- package/dist/cjs/src/core/Agent.d.ts +223 -0
- package/dist/cjs/src/core/Agent.d.ts.map +1 -0
- package/dist/cjs/src/core/Agent.js +1660 -0
- package/dist/cjs/src/core/Agent.js.map +1 -0
- package/dist/cjs/src/core/Events.d.ts +26 -0
- package/dist/cjs/src/core/Events.d.ts.map +1 -0
- package/dist/cjs/src/core/Events.js +144 -0
- package/dist/cjs/src/core/Events.js.map +1 -0
- package/dist/{core → cjs/src/core}/PersistenceManager.d.ts +21 -19
- package/dist/cjs/src/core/PersistenceManager.d.ts.map +1 -0
- package/dist/cjs/{core → src/core}/PersistenceManager.js +73 -20
- package/dist/cjs/src/core/PersistenceManager.js.map +1 -0
- package/dist/cjs/src/core/PromptComposer.d.ts +27 -0
- package/dist/cjs/src/core/PromptComposer.d.ts.map +1 -0
- package/dist/cjs/src/core/PromptComposer.js +157 -0
- package/dist/cjs/src/core/PromptComposer.js.map +1 -0
- package/dist/cjs/src/core/ResponseEngine.d.ts +32 -0
- package/dist/cjs/src/core/ResponseEngine.d.ts.map +1 -0
- package/dist/cjs/src/core/ResponseEngine.js +84 -0
- package/dist/cjs/src/core/ResponseEngine.js.map +1 -0
- package/dist/cjs/src/core/ResponsePipeline.d.ts +171 -0
- package/dist/cjs/src/core/ResponsePipeline.d.ts.map +1 -0
- package/dist/cjs/src/core/ResponsePipeline.js +514 -0
- package/dist/cjs/src/core/ResponsePipeline.js.map +1 -0
- package/dist/cjs/src/core/Route.d.ts +145 -0
- package/dist/cjs/src/core/Route.d.ts.map +1 -0
- package/dist/cjs/src/core/Route.js +343 -0
- package/dist/cjs/src/core/Route.js.map +1 -0
- package/dist/cjs/src/core/RoutingEngine.d.ts +129 -0
- package/dist/cjs/src/core/RoutingEngine.d.ts.map +1 -0
- package/dist/cjs/{core → src/core}/RoutingEngine.js +215 -117
- package/dist/cjs/src/core/RoutingEngine.js.map +1 -0
- package/dist/cjs/src/core/SessionManager.d.ts +86 -0
- package/dist/cjs/src/core/SessionManager.d.ts.map +1 -0
- package/dist/cjs/src/core/SessionManager.js +217 -0
- package/dist/cjs/src/core/SessionManager.js.map +1 -0
- package/dist/cjs/src/core/Step.d.ts +96 -0
- package/dist/cjs/src/core/Step.d.ts.map +1 -0
- package/dist/cjs/src/core/Step.js +206 -0
- package/dist/cjs/src/core/Step.js.map +1 -0
- package/dist/cjs/src/core/ToolExecutor.d.ts +45 -0
- package/dist/cjs/src/core/ToolExecutor.d.ts.map +1 -0
- package/dist/cjs/{core → src/core}/ToolExecutor.js +30 -19
- package/dist/cjs/src/core/ToolExecutor.js.map +1 -0
- package/dist/{index.d.ts → cjs/src/index.d.ts} +7 -15
- package/dist/cjs/src/index.d.ts.map +1 -0
- package/dist/cjs/{index.js → src/index.js} +21 -19
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/cjs/{providers → src/providers}/AnthropicProvider.d.ts +1 -1
- package/dist/cjs/src/providers/AnthropicProvider.d.ts.map +1 -0
- package/dist/cjs/{providers → src/providers}/AnthropicProvider.js +54 -2
- package/dist/cjs/src/providers/AnthropicProvider.js.map +1 -0
- package/dist/{providers → cjs/src/providers}/GeminiProvider.d.ts +1 -1
- package/dist/cjs/src/providers/GeminiProvider.d.ts.map +1 -0
- package/dist/cjs/{providers → src/providers}/GeminiProvider.js +65 -0
- package/dist/cjs/src/providers/GeminiProvider.js.map +1 -0
- package/dist/cjs/{providers → src/providers}/OpenAIProvider.d.ts +1 -1
- package/dist/cjs/src/providers/OpenAIProvider.d.ts.map +1 -0
- package/dist/cjs/{providers → src/providers}/OpenAIProvider.js +70 -1
- package/dist/cjs/src/providers/OpenAIProvider.js.map +1 -0
- package/dist/{providers → cjs/src/providers}/OpenRouterProvider.d.ts +1 -1
- package/dist/cjs/src/providers/OpenRouterProvider.d.ts.map +1 -0
- package/dist/cjs/{providers → src/providers}/OpenRouterProvider.js +76 -0
- package/dist/cjs/src/providers/OpenRouterProvider.js.map +1 -0
- package/dist/cjs/src/providers/index.d.ts.map +1 -0
- package/dist/cjs/src/providers/index.js.map +1 -0
- package/dist/cjs/{types → src/types}/agent.d.ts +80 -41
- package/dist/cjs/src/types/agent.d.ts.map +1 -0
- package/dist/cjs/src/types/agent.js.map +1 -0
- package/dist/cjs/{types → src/types}/ai.d.ts +7 -0
- package/dist/cjs/src/types/ai.d.ts.map +1 -0
- package/dist/cjs/src/types/ai.js.map +1 -0
- package/dist/{types → cjs/src/types}/history.d.ts +76 -18
- package/dist/cjs/src/types/history.d.ts.map +1 -0
- package/dist/cjs/src/types/history.js +33 -0
- package/dist/cjs/src/types/history.js.map +1 -0
- package/dist/cjs/src/types/index.d.ts +20 -0
- package/dist/cjs/src/types/index.d.ts.map +1 -0
- package/dist/cjs/src/types/index.js +30 -0
- package/dist/cjs/src/types/index.js.map +1 -0
- package/dist/{types → cjs/src/types}/persistence.d.ts +38 -23
- package/dist/cjs/src/types/persistence.d.ts.map +1 -0
- package/dist/cjs/src/types/persistence.js.map +1 -0
- package/dist/cjs/src/types/route.d.ts +235 -0
- package/dist/cjs/src/types/route.d.ts.map +1 -0
- package/dist/cjs/{types → src/types}/route.js.map +1 -1
- package/dist/cjs/src/types/routing.d.ts.map +1 -0
- package/dist/{types → cjs/src/types}/routing.js.map +1 -1
- package/dist/cjs/src/types/schema.d.ts.map +1 -0
- package/dist/{types → cjs/src/types}/schema.js.map +1 -1
- package/dist/cjs/src/types/session.d.ts +65 -0
- package/dist/cjs/src/types/session.d.ts.map +1 -0
- package/dist/cjs/src/types/session.js +6 -0
- package/dist/cjs/src/types/session.js.map +1 -0
- package/dist/cjs/src/types/template.d.ts +30 -0
- package/dist/cjs/src/types/template.d.ts.map +1 -0
- package/dist/cjs/src/types/template.js +3 -0
- package/dist/cjs/src/types/template.js.map +1 -0
- package/dist/{types → cjs/src/types}/tool.d.ts +17 -13
- package/dist/cjs/src/types/tool.d.ts.map +1 -0
- package/dist/cjs/{types → src/types}/tool.js.map +1 -1
- package/dist/cjs/src/utils/clone.d.ts +8 -0
- package/dist/cjs/src/utils/clone.d.ts.map +1 -0
- package/dist/cjs/src/utils/clone.js +36 -0
- package/dist/cjs/src/utils/clone.js.map +1 -0
- package/dist/{utils → cjs/src/utils}/event.d.ts +1 -1
- package/dist/cjs/src/utils/event.d.ts.map +1 -0
- package/dist/cjs/{utils → src/utils}/event.js +2 -2
- package/dist/cjs/src/utils/event.js.map +1 -0
- package/dist/cjs/src/utils/history.d.ts +31 -0
- package/dist/cjs/src/utils/history.d.ts.map +1 -0
- package/dist/cjs/src/utils/history.js +128 -0
- package/dist/cjs/src/utils/history.js.map +1 -0
- package/dist/cjs/src/utils/id.d.ts.map +1 -0
- package/dist/cjs/src/utils/id.js.map +1 -0
- package/dist/cjs/src/utils/index.d.ts +13 -0
- package/dist/cjs/src/utils/index.d.ts.map +1 -0
- package/dist/cjs/src/utils/index.js +49 -0
- package/dist/cjs/src/utils/index.js.map +1 -0
- package/dist/cjs/src/utils/logger.d.ts.map +1 -0
- package/dist/cjs/src/utils/logger.js.map +1 -0
- package/dist/cjs/src/utils/retry.d.ts.map +1 -0
- package/dist/cjs/src/utils/retry.js.map +1 -0
- package/dist/cjs/src/utils/session.d.ts +51 -0
- package/dist/cjs/src/utils/session.d.ts.map +1 -0
- package/dist/cjs/{types → src/utils}/session.js +35 -32
- package/dist/cjs/src/utils/session.js.map +1 -0
- package/dist/cjs/src/utils/template.d.ts +107 -0
- package/dist/cjs/src/utils/template.d.ts.map +1 -0
- package/dist/cjs/src/utils/template.js +283 -0
- package/dist/cjs/src/utils/template.js.map +1 -0
- package/dist/{cjs → src}/adapters/MemoryAdapter.d.ts +4 -4
- package/dist/src/adapters/MemoryAdapter.d.ts.map +1 -0
- package/dist/{adapters → src/adapters}/MemoryAdapter.js +41 -21
- package/dist/src/adapters/MemoryAdapter.js.map +1 -0
- package/dist/{cjs → src}/adapters/MongoAdapter.d.ts +3 -3
- package/dist/src/adapters/MongoAdapter.d.ts.map +1 -0
- package/dist/{adapters → src/adapters}/MongoAdapter.js +2 -1
- package/dist/src/adapters/MongoAdapter.js.map +1 -0
- package/dist/{adapters → src/adapters}/OpenSearchAdapter.d.ts +3 -3
- package/dist/src/adapters/OpenSearchAdapter.d.ts.map +1 -0
- package/dist/{adapters → src/adapters}/OpenSearchAdapter.js +10 -13
- package/dist/src/adapters/OpenSearchAdapter.js.map +1 -0
- package/dist/{adapters → src/adapters}/PostgreSQLAdapter.d.ts +3 -3
- package/dist/src/adapters/PostgreSQLAdapter.d.ts.map +1 -0
- package/dist/{adapters → src/adapters}/PostgreSQLAdapter.js +1 -1
- package/dist/src/adapters/PostgreSQLAdapter.js.map +1 -0
- package/dist/{adapters → src/adapters}/PrismaAdapter.d.ts +3 -3
- package/dist/src/adapters/PrismaAdapter.d.ts.map +1 -0
- package/dist/{adapters → src/adapters}/PrismaAdapter.js +35 -5
- package/dist/src/adapters/PrismaAdapter.js.map +1 -0
- package/dist/{adapters → src/adapters}/RedisAdapter.d.ts +3 -3
- package/dist/src/adapters/RedisAdapter.d.ts.map +1 -0
- package/dist/{adapters → src/adapters}/RedisAdapter.js +3 -2
- package/dist/src/adapters/RedisAdapter.js.map +1 -0
- package/dist/{cjs → src}/adapters/SQLiteAdapter.d.ts +3 -3
- package/dist/src/adapters/SQLiteAdapter.d.ts.map +1 -0
- package/dist/{adapters → src/adapters}/SQLiteAdapter.js +2 -1
- package/dist/src/adapters/SQLiteAdapter.js.map +1 -0
- package/dist/src/adapters/index.js.map +1 -0
- package/dist/src/constants/index.js.map +1 -0
- package/dist/src/core/Agent.d.ts +223 -0
- package/dist/src/core/Agent.d.ts.map +1 -0
- package/dist/src/core/Agent.js +1656 -0
- package/dist/src/core/Agent.js.map +1 -0
- package/dist/src/core/Events.d.ts +26 -0
- package/dist/src/core/Events.d.ts.map +1 -0
- package/dist/src/core/Events.js +137 -0
- package/dist/src/core/Events.js.map +1 -0
- package/dist/{cjs → src}/core/PersistenceManager.d.ts +21 -19
- package/dist/src/core/PersistenceManager.d.ts.map +1 -0
- package/dist/{core → src/core}/PersistenceManager.js +71 -18
- package/dist/src/core/PersistenceManager.js.map +1 -0
- package/dist/src/core/PromptComposer.d.ts +27 -0
- package/dist/src/core/PromptComposer.d.ts.map +1 -0
- package/dist/src/core/PromptComposer.js +153 -0
- package/dist/src/core/PromptComposer.js.map +1 -0
- package/dist/src/core/ResponseEngine.d.ts +32 -0
- package/dist/src/core/ResponseEngine.d.ts.map +1 -0
- package/dist/src/core/ResponseEngine.js +80 -0
- package/dist/src/core/ResponseEngine.js.map +1 -0
- package/dist/src/core/ResponsePipeline.d.ts +171 -0
- package/dist/src/core/ResponsePipeline.d.ts.map +1 -0
- package/dist/src/core/ResponsePipeline.js +510 -0
- package/dist/src/core/ResponsePipeline.js.map +1 -0
- package/dist/src/core/Route.d.ts +145 -0
- package/dist/src/core/Route.d.ts.map +1 -0
- package/dist/src/core/Route.js +339 -0
- package/dist/src/core/Route.js.map +1 -0
- package/dist/src/core/RoutingEngine.d.ts +129 -0
- package/dist/src/core/RoutingEngine.d.ts.map +1 -0
- package/dist/{core → src/core}/RoutingEngine.js +211 -113
- package/dist/src/core/RoutingEngine.js.map +1 -0
- package/dist/src/core/SessionManager.d.ts +86 -0
- package/dist/src/core/SessionManager.d.ts.map +1 -0
- package/dist/src/core/SessionManager.js +213 -0
- package/dist/src/core/SessionManager.js.map +1 -0
- package/dist/src/core/Step.d.ts +96 -0
- package/dist/src/core/Step.d.ts.map +1 -0
- package/dist/src/core/Step.js +202 -0
- package/dist/src/core/Step.js.map +1 -0
- package/dist/src/core/ToolExecutor.d.ts +45 -0
- package/dist/src/core/ToolExecutor.d.ts.map +1 -0
- package/dist/src/core/ToolExecutor.js +80 -0
- package/dist/src/core/ToolExecutor.js.map +1 -0
- package/dist/{cjs → src}/index.d.ts +7 -15
- package/dist/src/index.d.ts.map +1 -0
- package/dist/{index.js → src/index.js} +6 -7
- package/dist/src/index.js.map +1 -0
- package/dist/{providers → src/providers}/AnthropicProvider.d.ts +1 -1
- package/dist/src/providers/AnthropicProvider.d.ts.map +1 -0
- package/dist/{providers → src/providers}/AnthropicProvider.js +54 -2
- package/dist/src/providers/AnthropicProvider.js.map +1 -0
- package/dist/{cjs → src}/providers/GeminiProvider.d.ts +1 -1
- package/dist/{cjs → src}/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/{providers → src/providers}/GeminiProvider.js +65 -0
- package/dist/src/providers/GeminiProvider.js.map +1 -0
- package/dist/{providers → src/providers}/OpenAIProvider.d.ts +1 -1
- package/dist/{cjs → src}/providers/OpenAIProvider.d.ts.map +1 -1
- package/dist/{providers → src/providers}/OpenAIProvider.js +70 -1
- package/dist/src/providers/OpenAIProvider.js.map +1 -0
- package/dist/{cjs → src}/providers/OpenRouterProvider.d.ts +1 -1
- package/dist/{cjs → src}/providers/OpenRouterProvider.d.ts.map +1 -1
- package/dist/{providers → src/providers}/OpenRouterProvider.js +76 -0
- package/dist/src/providers/OpenRouterProvider.js.map +1 -0
- package/dist/src/providers/index.js.map +1 -0
- package/dist/{types → src/types}/agent.d.ts +80 -41
- package/dist/src/types/agent.d.ts.map +1 -0
- package/dist/src/types/agent.js.map +1 -0
- package/dist/{types → src/types}/ai.d.ts +7 -0
- package/dist/src/types/ai.d.ts.map +1 -0
- package/dist/{cjs → src}/types/ai.js.map +1 -1
- package/dist/{cjs → src}/types/history.d.ts +76 -18
- package/dist/src/types/history.d.ts.map +1 -0
- package/dist/src/types/history.js +30 -0
- package/dist/src/types/history.js.map +1 -0
- package/dist/src/types/index.d.ts +20 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +10 -0
- package/dist/src/types/index.js.map +1 -0
- package/dist/{cjs → src}/types/persistence.d.ts +38 -23
- package/dist/src/types/persistence.d.ts.map +1 -0
- package/dist/src/types/persistence.js.map +1 -0
- package/dist/src/types/route.d.ts +235 -0
- package/dist/src/types/route.d.ts.map +1 -0
- package/dist/{types → src/types}/route.js.map +1 -1
- package/dist/src/types/session.d.ts +65 -0
- package/dist/src/types/session.d.ts.map +1 -0
- package/dist/src/types/session.js +5 -0
- package/dist/src/types/session.js.map +1 -0
- package/dist/src/types/template.d.ts +30 -0
- package/dist/src/types/template.d.ts.map +1 -0
- package/dist/src/types/template.js +2 -0
- package/dist/src/types/template.js.map +1 -0
- package/dist/{cjs → src}/types/tool.d.ts +17 -13
- package/dist/src/types/tool.d.ts.map +1 -0
- package/dist/{types → src/types}/tool.js.map +1 -1
- package/dist/src/utils/clone.d.ts +8 -0
- package/dist/src/utils/clone.d.ts.map +1 -0
- package/dist/src/utils/clone.js +33 -0
- package/dist/src/utils/clone.js.map +1 -0
- package/dist/{cjs → src}/utils/event.d.ts +1 -1
- package/dist/{cjs → src}/utils/event.d.ts.map +1 -1
- package/dist/{utils → src/utils}/event.js +1 -1
- package/dist/src/utils/event.js.map +1 -0
- package/dist/src/utils/history.d.ts +31 -0
- package/dist/src/utils/history.d.ts.map +1 -0
- package/dist/src/utils/history.js +121 -0
- package/dist/src/utils/history.js.map +1 -0
- package/dist/src/utils/id.js.map +1 -0
- package/dist/src/utils/index.d.ts +13 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/index.js +19 -0
- package/dist/src/utils/index.js.map +1 -0
- package/dist/src/utils/logger.js.map +1 -0
- package/dist/src/utils/retry.js.map +1 -0
- package/dist/src/utils/session.d.ts +51 -0
- package/dist/src/utils/session.d.ts.map +1 -0
- package/dist/{types → src/utils}/session.js +33 -32
- package/dist/src/utils/session.js.map +1 -0
- package/dist/src/utils/template.d.ts +107 -0
- package/dist/src/utils/template.d.ts.map +1 -0
- package/dist/src/utils/template.js +276 -0
- package/dist/src/utils/template.js.map +1 -0
- package/docs/README.md +174 -68
- package/docs/{API_REFERENCE.md → api/README.md} +925 -255
- package/docs/api/overview.md +952 -0
- package/docs/core/agent/README.md +787 -0
- package/docs/{CONTEXT_MANAGEMENT.md → core/agent/context-management.md} +175 -102
- package/docs/{ARCHITECTURE.md → core/agent/session-management.md} +117 -69
- package/docs/core/ai-integration/prompt-composition.md +220 -0
- package/docs/core/ai-integration/providers.md +515 -0
- package/docs/core/ai-integration/response-processing.md +176 -0
- package/docs/core/conversation-flows/data-collection.md +623 -0
- package/docs/core/conversation-flows/route-dsl.md +502 -0
- package/docs/core/conversation-flows/routes.md +117 -0
- package/docs/core/conversation-flows/step-transitions.md +595 -0
- package/docs/core/conversation-flows/steps.md +154 -0
- package/docs/{ADAPTERS.md → core/persistence/adapters.md} +1 -1
- package/docs/core/persistence/session-storage.md +644 -0
- package/docs/core/routing/intelligent-routing.md +348 -0
- package/docs/core/tools/tool-definition.md +346 -0
- package/docs/core/tools/tool-execution.md +815 -0
- package/docs/core/tools/tool-scoping.md +628 -0
- package/docs/guides/getting-started/README.md +406 -0
- package/examples/{company-qna-agent.ts → advanced-patterns/knowledge-based-agent.ts} +139 -95
- package/examples/{persistent-onboarding.ts → advanced-patterns/persistent-onboarding.ts} +244 -137
- package/examples/{rules-prohibitions.ts → advanced-patterns/route-lifecycle-hooks.ts} +130 -84
- package/examples/{streaming-agent.ts → advanced-patterns/streaming-responses.ts} +116 -90
- package/examples/ai-providers/anthropic-integration.ts +384 -0
- package/examples/{openai-agent.ts → ai-providers/openai-integration.ts} +57 -63
- package/examples/conversation-flows/completion-transitions.ts +277 -0
- package/examples/core-concepts/basic-agent.ts +443 -0
- package/examples/core-concepts/schema-driven-extraction.ts +305 -0
- package/examples/core-concepts/session-management.ts +406 -0
- package/examples/integrations/database-integration.ts +630 -0
- package/examples/integrations/healthcare-integration.ts +609 -0
- package/examples/{opensearch-persistence.ts → integrations/search-integration.ts} +199 -171
- package/examples/integrations/server-session-management.ts +307 -0
- package/examples/persistence/custom-adapter.ts +529 -0
- package/examples/{prisma-persistence.ts → persistence/database-persistence.ts} +215 -272
- package/examples/persistence/memory-sessions.ts +495 -0
- package/examples/{prisma-schema.example.prisma → persistence/prisma-schema.example.prisma} +1 -1
- package/examples/persistence/redis-persistence.ts +490 -0
- package/examples/tools/basic-tools.ts +561 -0
- package/examples/{extracted-data-modification.ts → tools/data-enrichment-tools.ts} +128 -117
- package/package.json +14 -10
- package/src/adapters/MemoryAdapter.ts +74 -46
- package/src/adapters/MongoAdapter.ts +33 -24
- package/src/adapters/OpenSearchAdapter.ts +41 -37
- package/src/adapters/PostgreSQLAdapter.ts +35 -24
- package/src/adapters/PrismaAdapter.ts +69 -27
- package/src/adapters/RedisAdapter.ts +38 -26
- package/src/adapters/SQLiteAdapter.ts +32 -22
- package/src/core/Agent.ts +1431 -526
- package/src/core/Events.ts +100 -112
- package/src/core/PersistenceManager.ts +103 -49
- package/src/core/PromptComposer.ts +158 -85
- package/src/core/ResponseEngine.ts +128 -46
- package/src/core/ResponsePipeline.ts +830 -0
- package/src/core/Route.ts +222 -53
- package/src/core/RoutingEngine.ts +345 -229
- package/src/core/SessionManager.ts +265 -0
- package/src/core/Step.ts +157 -67
- package/src/core/ToolExecutor.ts +52 -43
- package/src/index.ts +31 -37
- package/src/providers/AnthropicProvider.ts +71 -5
- package/src/providers/GeminiProvider.ts +83 -2
- package/src/providers/OpenAIProvider.ts +95 -3
- package/src/providers/OpenRouterProvider.ts +102 -2
- package/src/types/agent.ts +81 -46
- package/src/types/ai.ts +7 -0
- package/src/types/history.ts +91 -18
- package/src/types/index.ts +45 -7
- package/src/types/persistence.ts +45 -28
- package/src/types/route.ts +122 -57
- package/src/types/session.ts +20 -220
- package/src/types/template.ts +36 -0
- package/src/types/tool.ts +23 -19
- package/src/utils/clone.ts +36 -0
- package/src/utils/event.ts +1 -1
- package/src/utils/history.ts +143 -0
- package/src/utils/index.ts +53 -0
- package/src/utils/session.ts +204 -0
- package/src/utils/template.ts +335 -0
- package/dist/adapters/MemoryAdapter.d.ts.map +0 -1
- package/dist/adapters/MemoryAdapter.js.map +0 -1
- package/dist/adapters/MongoAdapter.d.ts.map +0 -1
- package/dist/adapters/MongoAdapter.js.map +0 -1
- package/dist/adapters/OpenSearchAdapter.d.ts.map +0 -1
- package/dist/adapters/OpenSearchAdapter.js.map +0 -1
- package/dist/adapters/PostgreSQLAdapter.d.ts.map +0 -1
- package/dist/adapters/PostgreSQLAdapter.js.map +0 -1
- package/dist/adapters/PrismaAdapter.d.ts.map +0 -1
- package/dist/adapters/PrismaAdapter.js.map +0 -1
- package/dist/adapters/RedisAdapter.d.ts.map +0 -1
- package/dist/adapters/RedisAdapter.js.map +0 -1
- package/dist/adapters/SQLiteAdapter.d.ts.map +0 -1
- package/dist/adapters/SQLiteAdapter.js.map +0 -1
- package/dist/adapters/index.d.ts.map +0 -1
- package/dist/adapters/index.js.map +0 -1
- package/dist/cjs/adapters/MemoryAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/MemoryAdapter.js.map +0 -1
- package/dist/cjs/adapters/MongoAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/MongoAdapter.js.map +0 -1
- package/dist/cjs/adapters/OpenSearchAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/OpenSearchAdapter.js.map +0 -1
- package/dist/cjs/adapters/PostgreSQLAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/PostgreSQLAdapter.js.map +0 -1
- package/dist/cjs/adapters/PrismaAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/PrismaAdapter.js.map +0 -1
- package/dist/cjs/adapters/RedisAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/RedisAdapter.js.map +0 -1
- package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/SQLiteAdapter.js.map +0 -1
- package/dist/cjs/adapters/index.js.map +0 -1
- package/dist/cjs/constants/index.js.map +0 -1
- package/dist/cjs/core/Agent.d.ts +0 -197
- package/dist/cjs/core/Agent.d.ts.map +0 -1
- package/dist/cjs/core/Agent.js +0 -966
- package/dist/cjs/core/Agent.js.map +0 -1
- package/dist/cjs/core/DomainRegistry.d.ts +0 -36
- package/dist/cjs/core/DomainRegistry.d.ts.map +0 -1
- package/dist/cjs/core/DomainRegistry.js +0 -72
- package/dist/cjs/core/DomainRegistry.js.map +0 -1
- package/dist/cjs/core/Events.d.ts +0 -41
- package/dist/cjs/core/Events.d.ts.map +0 -1
- package/dist/cjs/core/Events.js +0 -99
- package/dist/cjs/core/Events.js.map +0 -1
- package/dist/cjs/core/PersistenceManager.d.ts.map +0 -1
- package/dist/cjs/core/PersistenceManager.js.map +0 -1
- package/dist/cjs/core/PromptComposer.d.ts +0 -24
- package/dist/cjs/core/PromptComposer.d.ts.map +0 -1
- package/dist/cjs/core/PromptComposer.js +0 -127
- package/dist/cjs/core/PromptComposer.js.map +0 -1
- package/dist/cjs/core/ResponseEngine.d.ts +0 -14
- package/dist/cjs/core/ResponseEngine.d.ts.map +0 -1
- package/dist/cjs/core/ResponseEngine.js +0 -56
- package/dist/cjs/core/ResponseEngine.js.map +0 -1
- package/dist/cjs/core/Route.d.ts +0 -90
- package/dist/cjs/core/Route.d.ts.map +0 -1
- package/dist/cjs/core/Route.js +0 -203
- package/dist/cjs/core/Route.js.map +0 -1
- package/dist/cjs/core/RoutingEngine.d.ts +0 -109
- package/dist/cjs/core/RoutingEngine.d.ts.map +0 -1
- package/dist/cjs/core/RoutingEngine.js.map +0 -1
- package/dist/cjs/core/Step.d.ts +0 -72
- package/dist/cjs/core/Step.d.ts.map +0 -1
- package/dist/cjs/core/Step.js +0 -150
- package/dist/cjs/core/Step.js.map +0 -1
- package/dist/cjs/core/Tool.d.ts +0 -39
- package/dist/cjs/core/Tool.d.ts.map +0 -1
- package/dist/cjs/core/Tool.js +0 -34
- package/dist/cjs/core/Tool.js.map +0 -1
- package/dist/cjs/core/ToolExecutor.d.ts +0 -29
- package/dist/cjs/core/ToolExecutor.d.ts.map +0 -1
- package/dist/cjs/core/ToolExecutor.js.map +0 -1
- package/dist/cjs/core/Transition.d.ts +0 -32
- package/dist/cjs/core/Transition.d.ts.map +0 -1
- package/dist/cjs/core/Transition.js +0 -89
- package/dist/cjs/core/Transition.js.map +0 -1
- package/dist/cjs/index.d.ts.map +0 -1
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/providers/AnthropicProvider.d.ts.map +0 -1
- package/dist/cjs/providers/AnthropicProvider.js.map +0 -1
- package/dist/cjs/providers/GeminiProvider.js.map +0 -1
- package/dist/cjs/providers/OpenAIProvider.js.map +0 -1
- package/dist/cjs/providers/OpenRouterProvider.js.map +0 -1
- package/dist/cjs/providers/index.js.map +0 -1
- package/dist/cjs/types/agent.d.ts.map +0 -1
- package/dist/cjs/types/agent.js.map +0 -1
- package/dist/cjs/types/ai.d.ts.map +0 -1
- package/dist/cjs/types/history.d.ts.map +0 -1
- package/dist/cjs/types/history.js +0 -37
- package/dist/cjs/types/history.js.map +0 -1
- package/dist/cjs/types/index.d.ts +0 -12
- package/dist/cjs/types/index.d.ts.map +0 -1
- package/dist/cjs/types/index.js +0 -12
- package/dist/cjs/types/index.js.map +0 -1
- package/dist/cjs/types/persistence.d.ts.map +0 -1
- package/dist/cjs/types/persistence.js.map +0 -1
- package/dist/cjs/types/route.d.ts +0 -175
- package/dist/cjs/types/route.d.ts.map +0 -1
- package/dist/cjs/types/session.d.ts +0 -104
- package/dist/cjs/types/session.d.ts.map +0 -1
- package/dist/cjs/types/session.js.map +0 -1
- package/dist/cjs/types/tool.d.ts.map +0 -1
- package/dist/cjs/utils/event.js.map +0 -1
- package/dist/cjs/utils/id.js.map +0 -1
- package/dist/cjs/utils/logger.js.map +0 -1
- package/dist/cjs/utils/retry.js.map +0 -1
- package/dist/constants/index.d.ts.map +0 -1
- package/dist/constants/index.js.map +0 -1
- package/dist/core/Agent.d.ts +0 -197
- package/dist/core/Agent.d.ts.map +0 -1
- package/dist/core/Agent.js +0 -962
- package/dist/core/Agent.js.map +0 -1
- package/dist/core/DomainRegistry.d.ts +0 -36
- package/dist/core/DomainRegistry.d.ts.map +0 -1
- package/dist/core/DomainRegistry.js +0 -68
- package/dist/core/DomainRegistry.js.map +0 -1
- package/dist/core/Events.d.ts +0 -41
- package/dist/core/Events.d.ts.map +0 -1
- package/dist/core/Events.js +0 -94
- package/dist/core/Events.js.map +0 -1
- package/dist/core/PersistenceManager.d.ts.map +0 -1
- package/dist/core/PersistenceManager.js.map +0 -1
- package/dist/core/PromptComposer.d.ts +0 -24
- package/dist/core/PromptComposer.d.ts.map +0 -1
- package/dist/core/PromptComposer.js +0 -123
- package/dist/core/PromptComposer.js.map +0 -1
- package/dist/core/ResponseEngine.d.ts +0 -14
- package/dist/core/ResponseEngine.d.ts.map +0 -1
- package/dist/core/ResponseEngine.js +0 -52
- package/dist/core/ResponseEngine.js.map +0 -1
- package/dist/core/Route.d.ts +0 -90
- package/dist/core/Route.d.ts.map +0 -1
- package/dist/core/Route.js +0 -199
- package/dist/core/Route.js.map +0 -1
- package/dist/core/RoutingEngine.d.ts +0 -109
- package/dist/core/RoutingEngine.d.ts.map +0 -1
- package/dist/core/RoutingEngine.js.map +0 -1
- package/dist/core/Step.d.ts +0 -72
- package/dist/core/Step.d.ts.map +0 -1
- package/dist/core/Step.js +0 -146
- package/dist/core/Step.js.map +0 -1
- package/dist/core/Tool.d.ts +0 -39
- package/dist/core/Tool.d.ts.map +0 -1
- package/dist/core/Tool.js +0 -31
- package/dist/core/Tool.js.map +0 -1
- package/dist/core/ToolExecutor.d.ts +0 -29
- package/dist/core/ToolExecutor.d.ts.map +0 -1
- package/dist/core/ToolExecutor.js +0 -69
- package/dist/core/ToolExecutor.js.map +0 -1
- package/dist/core/Transition.d.ts +0 -32
- package/dist/core/Transition.d.ts.map +0 -1
- package/dist/core/Transition.js +0 -85
- package/dist/core/Transition.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/providers/AnthropicProvider.d.ts.map +0 -1
- package/dist/providers/AnthropicProvider.js.map +0 -1
- package/dist/providers/GeminiProvider.d.ts.map +0 -1
- package/dist/providers/GeminiProvider.js.map +0 -1
- package/dist/providers/OpenAIProvider.d.ts.map +0 -1
- package/dist/providers/OpenAIProvider.js.map +0 -1
- package/dist/providers/OpenRouterProvider.d.ts.map +0 -1
- package/dist/providers/OpenRouterProvider.js.map +0 -1
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js.map +0 -1
- package/dist/types/agent.d.ts.map +0 -1
- package/dist/types/agent.js.map +0 -1
- package/dist/types/ai.d.ts.map +0 -1
- package/dist/types/ai.js.map +0 -1
- package/dist/types/history.d.ts.map +0 -1
- package/dist/types/history.js +0 -34
- package/dist/types/history.js.map +0 -1
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -6
- package/dist/types/index.js.map +0 -1
- package/dist/types/persistence.d.ts.map +0 -1
- package/dist/types/persistence.js.map +0 -1
- package/dist/types/route.d.ts +0 -175
- package/dist/types/route.d.ts.map +0 -1
- package/dist/types/routing.d.ts.map +0 -1
- package/dist/types/schema.d.ts.map +0 -1
- package/dist/types/session.d.ts +0 -104
- package/dist/types/session.d.ts.map +0 -1
- package/dist/types/session.js.map +0 -1
- package/dist/types/tool.d.ts.map +0 -1
- package/dist/utils/event.d.ts.map +0 -1
- package/dist/utils/event.js.map +0 -1
- package/dist/utils/id.d.ts.map +0 -1
- package/dist/utils/id.js.map +0 -1
- package/dist/utils/logger.d.ts.map +0 -1
- package/dist/utils/logger.js.map +0 -1
- package/dist/utils/retry.d.ts.map +0 -1
- package/dist/utils/retry.js.map +0 -1
- package/docs/AGENT.md +0 -535
- package/docs/DOCS.md +0 -263
- package/docs/DOMAINS.md +0 -735
- package/docs/EXAMPLES.md +0 -467
- package/docs/GETTING_STARTED.md +0 -424
- package/docs/PERSISTENCE.md +0 -815
- package/docs/PROVIDERS.md +0 -612
- package/docs/ROUTES.md +0 -1085
- package/docs/STEPS.md +0 -883
- package/examples/business-onboarding.ts +0 -791
- package/examples/custom-database-persistence.ts +0 -574
- package/examples/declarative-agent.ts +0 -401
- package/examples/domain-scoping.ts +0 -366
- package/examples/healthcare-agent.ts +0 -511
- package/examples/redis-persistence.ts +0 -525
- package/examples/route-transitions.ts +0 -266
- package/examples/travel-agent.ts +0 -584
- package/src/core/DomainRegistry.ts +0 -80
- package/src/core/Tool.ts +0 -112
- package/src/core/Transition.ts +0 -115
- /package/dist/{adapters → cjs/src/adapters}/index.d.ts +0 -0
- /package/dist/cjs/{adapters → src/adapters}/index.js +0 -0
- /package/dist/cjs/{constants → src/constants}/index.d.ts +0 -0
- /package/dist/cjs/{constants → src/constants}/index.js +0 -0
- /package/dist/cjs/{providers → src/providers}/index.d.ts +0 -0
- /package/dist/cjs/{providers → src/providers}/index.js +0 -0
- /package/dist/cjs/{types → src/types}/agent.js +0 -0
- /package/dist/cjs/{types → src/types}/ai.js +0 -0
- /package/dist/cjs/{types → src/types}/persistence.js +0 -0
- /package/dist/cjs/{types → src/types}/route.js +0 -0
- /package/dist/cjs/{types → src/types}/routing.d.ts +0 -0
- /package/dist/cjs/{types → src/types}/routing.js +0 -0
- /package/dist/cjs/{types → src/types}/schema.d.ts +0 -0
- /package/dist/cjs/{types → src/types}/schema.js +0 -0
- /package/dist/cjs/{types → src/types}/tool.js +0 -0
- /package/dist/cjs/{utils → src/utils}/id.d.ts +0 -0
- /package/dist/cjs/{utils → src/utils}/id.js +0 -0
- /package/dist/cjs/{utils → src/utils}/logger.d.ts +0 -0
- /package/dist/cjs/{utils → src/utils}/logger.js +0 -0
- /package/dist/cjs/{utils → src/utils}/retry.d.ts +0 -0
- /package/dist/cjs/{utils → src/utils}/retry.js +0 -0
- /package/dist/{cjs → src}/adapters/index.d.ts +0 -0
- /package/dist/{cjs → src}/adapters/index.d.ts.map +0 -0
- /package/dist/{adapters → src/adapters}/index.js +0 -0
- /package/dist/{constants → src/constants}/index.d.ts +0 -0
- /package/dist/{cjs → src}/constants/index.d.ts.map +0 -0
- /package/dist/{constants → src/constants}/index.js +0 -0
- /package/dist/{providers → src/providers}/index.d.ts +0 -0
- /package/dist/{cjs → src}/providers/index.d.ts.map +0 -0
- /package/dist/{providers → src/providers}/index.js +0 -0
- /package/dist/{types → src/types}/agent.js +0 -0
- /package/dist/{types → src/types}/ai.js +0 -0
- /package/dist/{types → src/types}/persistence.js +0 -0
- /package/dist/{types → src/types}/route.js +0 -0
- /package/dist/{types → src/types}/routing.d.ts +0 -0
- /package/dist/{cjs → src}/types/routing.d.ts.map +0 -0
- /package/dist/{types → src/types}/routing.js +0 -0
- /package/dist/{cjs → src}/types/routing.js.map +0 -0
- /package/dist/{types → src/types}/schema.d.ts +0 -0
- /package/dist/{cjs → src}/types/schema.d.ts.map +0 -0
- /package/dist/{types → src/types}/schema.js +0 -0
- /package/dist/{cjs → src}/types/schema.js.map +0 -0
- /package/dist/{types → src/types}/tool.js +0 -0
- /package/dist/{utils → src/utils}/id.d.ts +0 -0
- /package/dist/{cjs → src}/utils/id.d.ts.map +0 -0
- /package/dist/{utils → src/utils}/id.js +0 -0
- /package/dist/{utils → src/utils}/logger.d.ts +0 -0
- /package/dist/{cjs → src}/utils/logger.d.ts.map +0 -0
- /package/dist/{utils → src/utils}/logger.js +0 -0
- /package/dist/{utils → src/utils}/retry.d.ts +0 -0
- /package/dist/{cjs → src}/utils/retry.d.ts.map +0 -0
- /package/dist/{utils → src/utils}/retry.js +0 -0
- /package/docs/{PUBLISHING.md → guides/advanced-patterns/publishing.md} +0 -0
|
@@ -1,19 +1,30 @@
|
|
|
1
1
|
# API Reference
|
|
2
2
|
|
|
3
|
-
Complete API documentation for `@falai/agent`.
|
|
3
|
+
Complete API documentation for `@falai/agent`. This framework provides a strongly-typed, modular agent architecture with AI-powered routing and schema-driven data collection.
|
|
4
|
+
|
|
5
|
+
## 📋 Documentation Structure
|
|
6
|
+
|
|
7
|
+
- **[Complete API Overview](./overview.md)** - Comprehensive reference for all classes, interfaces, and utilities
|
|
8
|
+
- **[Agent Architecture](../core/agent/README.md)** - Agent class, context management, and lifecycle
|
|
9
|
+
- **[AI Routing System](../core/routing/intelligent-routing.md)** - Intelligent route and step selection
|
|
10
|
+
- **[Route DSL](../core/conversation-flows/route-dsl.md)** - Declarative conversation flow design
|
|
11
|
+
- **[Data Collection](../core/conversation-flows/data-collection.md)** - Schema-driven data extraction
|
|
12
|
+
- **[Tool Execution](../core/tools/tool-execution.md)** - Dynamic tool calling and context updates
|
|
13
|
+
- **[Session Storage](../core/persistence/session-storage.md)** - Persistence and session management
|
|
14
|
+
- **[AI Providers](../core/ai-integration/providers.md)** - Provider integrations and configuration
|
|
4
15
|
|
|
5
16
|
---
|
|
6
17
|
|
|
7
18
|
## Core Classes
|
|
8
19
|
|
|
9
|
-
### `Agent<TContext>`
|
|
20
|
+
### `Agent<TContext, TData>`
|
|
10
21
|
|
|
11
|
-
Main agent class for managing conversational AI.
|
|
22
|
+
Main agent class for managing conversational AI with agent-level data collection.
|
|
12
23
|
|
|
13
24
|
#### Constructor
|
|
14
25
|
|
|
15
26
|
```typescript
|
|
16
|
-
new Agent<TContext>(options: AgentOptions<TContext>)
|
|
27
|
+
new Agent<TContext, TData>(options: AgentOptions<TContext, TData>)
|
|
17
28
|
```
|
|
18
29
|
|
|
19
30
|
See [Agent](./AGENT.md) for full details.
|
|
@@ -22,7 +33,38 @@ See [Agent](./AGENT.md) for full details.
|
|
|
22
33
|
|
|
23
34
|
##### `createRoute(options: RouteOptions): Route`
|
|
24
35
|
|
|
25
|
-
Creates a new conversation route.
|
|
36
|
+
Creates a new conversation route with required fields specification.
|
|
37
|
+
|
|
38
|
+
##### `getCollectedData(): Partial<TData>`
|
|
39
|
+
|
|
40
|
+
Gets the current agent-level collected data.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
const data = agent.getCollectedData();
|
|
44
|
+
console.log(data); // { customerName: "John", email: "john@example.com" }
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
##### `updateCollectedData(updates: Partial<TData>): Promise<void>`
|
|
48
|
+
|
|
49
|
+
Updates agent-level collected data and triggers validation.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
await agent.updateCollectedData({
|
|
53
|
+
customerId: "CUST-12345",
|
|
54
|
+
priority: "high"
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
##### `validateData(data: Partial<TData>): ValidationResult`
|
|
59
|
+
|
|
60
|
+
Validates data against the agent-level schema.
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const validation = agent.validateData({ email: "invalid-email" });
|
|
64
|
+
if (!validation.valid) {
|
|
65
|
+
console.log(validation.errors); // Detailed validation errors
|
|
66
|
+
}
|
|
67
|
+
```
|
|
26
68
|
|
|
27
69
|
##### `createTerm(term: Term): this`
|
|
28
70
|
|
|
@@ -32,21 +74,108 @@ Adds a domain glossary term. Returns `this` for chaining.
|
|
|
32
74
|
|
|
33
75
|
Adds a behavioral guideline. Returns `this` for chaining.
|
|
34
76
|
|
|
35
|
-
##### `
|
|
77
|
+
##### `respond(input: RespondInput<TContext>): Promise<RespondOutput>`
|
|
78
|
+
|
|
79
|
+
Generates an AI response with session step management, tool execution, data extraction, and intelligent routing.
|
|
80
|
+
|
|
81
|
+
**Enhanced Response Pipeline:**
|
|
82
|
+
|
|
83
|
+
1. **Tool Execution** - Execute tools if current step has `tool` (enriches context before AI response)
|
|
84
|
+
2. **Always-On Routing** - Score all routes, respect user intent to change direction
|
|
85
|
+
3. **Step Traversal** - Use `skipIf` and `requires` to determine next step
|
|
86
|
+
4. **Response Generation** - Build schema with `collect` fields, extract data
|
|
87
|
+
5. **Session Update** - Merge collected data into session step
|
|
88
|
+
|
|
89
|
+
**Tool Execution (Pre-Response):**
|
|
90
|
+
Tools execute before AI response generation, allowing them to:
|
|
91
|
+
|
|
92
|
+
- Enrich context with external data
|
|
93
|
+
- Update collected session data
|
|
94
|
+
- Perform business logic
|
|
95
|
+
- Access APIs and databases
|
|
96
|
+
|
|
97
|
+
This design enables more intelligent, context-aware responses.
|
|
98
|
+
|
|
99
|
+
##### `getKnowledgeBase(): Record<string, unknown>`
|
|
100
|
+
|
|
101
|
+
Gets the agent's knowledge base containing any JSON structure the AI should know.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const knowledge = agent.getKnowledgeBase();
|
|
105
|
+
// Returns the agent's knowledge base
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
##### `getRoutes(): Route<TContext, unknown>[]`
|
|
109
|
+
|
|
110
|
+
Gets all routes configured in the agent.
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const routes = agent.getRoutes();
|
|
114
|
+
// Returns array of all configured routes
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
##### `getTerms(): Term<TContext>[]`
|
|
118
|
+
|
|
119
|
+
Gets all terms configured in the agent.
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
const terms = agent.getTerms();
|
|
123
|
+
// Returns array of all configured terms
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
##### `getGuidelines(): Guideline<TContext>[]`
|
|
127
|
+
|
|
128
|
+
Gets all guidelines configured in the agent.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const guidelines = agent.getGuidelines();
|
|
132
|
+
// Returns array of all configured guidelines
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
````typescript
|
|
136
|
+
|
|
137
|
+
##### `getPersistenceManager(): PersistenceManager | undefined`
|
|
138
|
+
|
|
139
|
+
Gets the persistence manager if configured.
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
const persistence = agent.getPersistenceManager();
|
|
143
|
+
// Returns PersistenceManager instance or undefined if not configured
|
|
144
|
+
````
|
|
145
|
+
|
|
146
|
+
##### `hasPersistence(): boolean`
|
|
147
|
+
|
|
148
|
+
Checks if persistence is enabled.
|
|
36
149
|
|
|
37
|
-
|
|
150
|
+
```typescript
|
|
151
|
+
if (agent.hasPersistence()) {
|
|
152
|
+
// Persistence is configured
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
##### `nextStepRoute(routeIdOrTitle, session?, condition?, history?): Promise<SessionState>`
|
|
38
157
|
|
|
39
|
-
|
|
158
|
+
Manually transition to a different route by setting a pending transition that will be executed on the next `respond()` call.
|
|
40
159
|
|
|
41
|
-
|
|
160
|
+
```typescript
|
|
161
|
+
// Transition to feedback route after completion
|
|
162
|
+
const updatedSession = await agent.nextStepRoute(
|
|
163
|
+
"feedback-collection",
|
|
164
|
+
session
|
|
165
|
+
);
|
|
42
166
|
|
|
43
|
-
|
|
167
|
+
// Next respond() call will automatically transition
|
|
168
|
+
const response = await agent.respond({ history, session: updatedSession });
|
|
169
|
+
```
|
|
44
170
|
|
|
45
|
-
|
|
171
|
+
**Parameters:**
|
|
46
172
|
|
|
47
|
-
|
|
173
|
+
- `routeIdOrTitle`: Route ID or title to transition to
|
|
174
|
+
- `session`: Session step to update (uses current session if not provided)
|
|
175
|
+
- `condition`: Optional AI-evaluated condition for the transition
|
|
176
|
+
- `history`: Optional history for template context
|
|
48
177
|
|
|
49
|
-
|
|
178
|
+
**Returns:** Updated session with pending transition
|
|
50
179
|
|
|
51
180
|
##### `getData<TData>(routeId?): Partial<TData>`
|
|
52
181
|
|
|
@@ -159,87 +288,53 @@ interface RespondOutput {
|
|
|
159
288
|
- Automatically merges new collected data with existing session data
|
|
160
289
|
- **Per-route data preservation** - Collected data is organized by route ID, allowing users to switch routes without losing progress
|
|
161
290
|
|
|
162
|
-
**Example with
|
|
291
|
+
**Example with Automatic Session Management:**
|
|
163
292
|
|
|
164
293
|
```typescript
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
294
|
+
// Server endpoint with automatic session management
|
|
295
|
+
app.post('/chat', async (req, res) => {
|
|
296
|
+
const { sessionId, message } = req.body;
|
|
297
|
+
|
|
298
|
+
const agent = new Agent({
|
|
299
|
+
name: "Travel Agent",
|
|
300
|
+
provider: new OpenAIProvider({ apiKey: process.env.OPENAI_API_KEY }),
|
|
301
|
+
persistence: { adapter: new PrismaAdapter({ prisma }) },
|
|
302
|
+
sessionId // Automatically loads or creates this session
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const response = await agent.respond(message);
|
|
306
|
+
|
|
307
|
+
res.json({
|
|
308
|
+
message: response.message,
|
|
309
|
+
sessionId: agent.session.id,
|
|
310
|
+
isComplete: response.isRouteComplete
|
|
172
311
|
});
|
|
173
|
-
|
|
174
|
-
// Option 1: Set current session for convenience
|
|
175
|
-
agent.setCurrentSession(sessionStep);
|
|
176
|
-
|
|
177
|
-
const response = await agent.respond({
|
|
178
|
-
history,
|
|
179
|
-
// session: sessionStep, // No longer required!
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
// Use convenience methods without passing session
|
|
183
|
-
const data = agent.getData();
|
|
184
|
-
|
|
185
|
-
// Option 2: Still pass session explicitly if preferred
|
|
186
|
-
const response2 = await agent.respond({
|
|
187
|
-
history,
|
|
188
|
-
session: sessionStep,
|
|
189
312
|
});
|
|
190
313
|
```
|
|
191
314
|
|
|
192
|
-
**
|
|
315
|
+
**SessionManager API:**
|
|
193
316
|
|
|
194
317
|
```typescript
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
// Load from your custom database
|
|
198
|
-
const dbSession = await yourDb.sessions.findOne({ id: sessionId });
|
|
318
|
+
// Access session manager
|
|
319
|
+
const sessionManager = agent.session;
|
|
199
320
|
|
|
200
|
-
//
|
|
201
|
-
|
|
321
|
+
// Get or create session (works for existing, new, or auto-generated IDs)
|
|
322
|
+
await sessionManager.getOrCreate("user-123");
|
|
323
|
+
await sessionManager.getOrCreate(); // Auto-generates ID
|
|
202
324
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
currentRoute: {
|
|
207
|
-
id: dbSession.currentRoute,
|
|
208
|
-
title:
|
|
209
|
-
dbSession.collectedData?.currentRouteTitle || dbSession.currentRoute,
|
|
210
|
-
enteredAt: new Date(),
|
|
211
|
-
},
|
|
212
|
-
currentStep: dbSession.currentStep
|
|
213
|
-
? {
|
|
214
|
-
id: dbSession.currentStep,
|
|
215
|
-
description: dbSession.collectedData?.currentStepDescription,
|
|
216
|
-
enteredAt: new Date(),
|
|
217
|
-
}
|
|
218
|
-
: undefined,
|
|
219
|
-
data: dbSession.collectedData?.data || {},
|
|
220
|
-
routeHistory: dbSession.collectedData?.routeHistory || [],
|
|
221
|
-
metadata: {
|
|
222
|
-
sessionId: dbSession.id,
|
|
223
|
-
createdAt: dbSession.createdAt,
|
|
224
|
-
lastUpdatedAt: new Date(),
|
|
225
|
-
},
|
|
226
|
-
};
|
|
227
|
-
} else {
|
|
228
|
-
// Create new session
|
|
229
|
-
agentSession = createSession<YourDataType>({
|
|
230
|
-
sessionId: dbSession?.id || "new-session-id",
|
|
231
|
-
});
|
|
232
|
-
}
|
|
325
|
+
// Data access
|
|
326
|
+
const data = sessionManager.getData<FlightData>();
|
|
327
|
+
await sessionManager.setData({ destination: "Paris" });
|
|
233
328
|
|
|
234
|
-
//
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
});
|
|
329
|
+
// History management
|
|
330
|
+
await sessionManager.addMessage("user", "Hello");
|
|
331
|
+
const history = sessionManager.getHistory();
|
|
332
|
+
sessionManager.clearHistory();
|
|
239
333
|
|
|
240
|
-
//
|
|
241
|
-
await
|
|
242
|
-
|
|
334
|
+
// Session operations
|
|
335
|
+
await sessionManager.save(); // Manual save (auto-saves on addMessage)
|
|
336
|
+
await sessionManager.delete();
|
|
337
|
+
const newSession = await sessionManager.reset(true); // Preserve history
|
|
243
338
|
currentRoute: response.session?.currentRoute?.id,
|
|
244
339
|
currentStep: response.session?.currentStep?.id,
|
|
245
340
|
collectedData: {
|
|
@@ -255,7 +350,7 @@ await yourDb.sessions.update({
|
|
|
255
350
|
// Save message
|
|
256
351
|
await yourDb.messages.create({
|
|
257
352
|
sessionId: dbSession.id,
|
|
258
|
-
role: "
|
|
353
|
+
role: "assistant",
|
|
259
354
|
content: response.message,
|
|
260
355
|
route: response.session?.currentRoute?.id,
|
|
261
356
|
step: response.session?.currentStep?.id,
|
|
@@ -310,21 +405,21 @@ const onboardingRoute = agent.createRoute<OnboardingData>({
|
|
|
310
405
|
// Build steps with skipIf conditions
|
|
311
406
|
const welcome = onboardingRoute.initialStep.nextStep({
|
|
312
407
|
id: "ask_name",
|
|
313
|
-
|
|
408
|
+
prompt: "What's your name?",
|
|
314
409
|
collect: ["name"],
|
|
315
410
|
skipIf: (data) => !!data.name, // Skip if name already collected
|
|
316
411
|
});
|
|
317
412
|
|
|
318
413
|
const askEmail = welcome.nextStep({
|
|
319
414
|
id: "ask_email",
|
|
320
|
-
|
|
415
|
+
prompt: "What's your email?",
|
|
321
416
|
collect: ["email"],
|
|
322
417
|
skipIf: (data) => !!data.email, // Skip if email already collected
|
|
323
418
|
});
|
|
324
419
|
|
|
325
420
|
const complete = askEmail.nextStep({
|
|
326
421
|
id: "complete",
|
|
327
|
-
|
|
422
|
+
prompt: "All done! Thank you.",
|
|
328
423
|
});
|
|
329
424
|
|
|
330
425
|
complete.nextStep({ step: END_ROUTE });
|
|
@@ -342,17 +437,13 @@ if (response.isRouteComplete) {
|
|
|
342
437
|
return "Profile updated successfully!";
|
|
343
438
|
}
|
|
344
439
|
|
|
345
|
-
// Get route-specific data if needed
|
|
346
|
-
const onboardingData = agent.getDataForRoute("onboarding");
|
|
347
|
-
const bookingData = agent.getDataForRoute("booking");
|
|
348
|
-
|
|
349
440
|
return response.message;
|
|
350
441
|
```
|
|
351
442
|
|
|
352
443
|
**Important Notes:**
|
|
353
444
|
|
|
354
445
|
- `isRouteComplete` is `true` when the route reaches an `END_ROUTE` transition
|
|
355
|
-
- The `message`
|
|
446
|
+
- The `message` contains the completion message generated by the AI when `isRouteComplete` is `true`
|
|
356
447
|
- You should check `isRouteComplete` and handle completion appropriately
|
|
357
448
|
- If all steps are skipped (due to `skipIf` conditions), the route can complete immediately on entry
|
|
358
449
|
- Use `agent.getData(session)` to retrieve all collected data
|
|
@@ -462,7 +553,7 @@ for await (const chunk of agent.respondStream({ history })) {
|
|
|
462
553
|
// Save to database
|
|
463
554
|
await db.agentMessages.create({
|
|
464
555
|
sessionId: session.id,
|
|
465
|
-
role: "
|
|
556
|
+
role: "assistant",
|
|
466
557
|
content: fullMessage,
|
|
467
558
|
route: finalChunk.route?.title,
|
|
468
559
|
toolCalls: finalChunk.toolCalls || [],
|
|
@@ -485,9 +576,9 @@ Agent's description (readonly).
|
|
|
485
576
|
|
|
486
577
|
Agent's goal (readonly).
|
|
487
578
|
|
|
488
|
-
##### `
|
|
579
|
+
##### `identity(): Template<TContext> | undefined`
|
|
489
580
|
|
|
490
|
-
|
|
581
|
+
Agent's identity template (readonly).
|
|
491
582
|
|
|
492
583
|
---
|
|
493
584
|
|
|
@@ -504,9 +595,11 @@ interface RouteOptions<TData = unknown> {
|
|
|
504
595
|
id?: string; // Optional custom ID (deterministic ID generated from title if not provided)
|
|
505
596
|
title: string; // Route title
|
|
506
597
|
description?: string; // Route description
|
|
598
|
+
identity?: string; // Optional identity prompt defining the agent's role and persona for this route
|
|
599
|
+
personality?: Template; // Optional personality prompt defining the agent's communication style for this route
|
|
507
600
|
conditions?: string[]; // Conditions that activate this route
|
|
508
601
|
guidelines?: Guideline[]; // Initial guidelines for this route
|
|
509
|
-
|
|
602
|
+
terms?: Term[]; // Initial terms for the route's domain glossary
|
|
510
603
|
rules?: string[]; // Absolute rules the agent MUST follow in this route
|
|
511
604
|
prohibitions?: string[]; // Absolute prohibitions the agent MUST NEVER do in this route
|
|
512
605
|
|
|
@@ -524,14 +617,20 @@ interface RouteOptions<TData = unknown> {
|
|
|
524
617
|
// NEW: Configure the initial step
|
|
525
618
|
initialStep?: {
|
|
526
619
|
id?: string; // Custom ID for the initial step
|
|
527
|
-
|
|
620
|
+
prompt?: string; // Description for the initial step
|
|
528
621
|
collect?: string[]; // Fields to collect in the initial step
|
|
529
622
|
skipIf?: (data: Partial<TData>) => boolean; // Skip condition
|
|
530
623
|
requires?: string[]; // Required data prerequisites
|
|
531
624
|
};
|
|
532
625
|
|
|
533
626
|
// NEW: Sequential steps for simple linear flows
|
|
534
|
-
steps?:
|
|
627
|
+
steps?: StepOptions<unknown, TData>[];
|
|
628
|
+
/** Knowledge base specific to this route containing any JSON structure the AI should know */
|
|
629
|
+
knowledgeBase?: Record<string, unknown>;
|
|
630
|
+
/**
|
|
631
|
+
* Route lifecycle hooks
|
|
632
|
+
*/
|
|
633
|
+
hooks?: RouteLifecycleHooks<TContext, TData>;
|
|
535
634
|
}
|
|
536
635
|
```
|
|
537
636
|
|
|
@@ -552,9 +651,13 @@ Adds a guideline specific to this route. Returns `this` for chaining.
|
|
|
552
651
|
|
|
553
652
|
Returns all guidelines for this route.
|
|
554
653
|
|
|
555
|
-
##### `
|
|
654
|
+
##### `createTerm(term: Term): this`
|
|
655
|
+
|
|
656
|
+
Adds a term to the route's domain glossary. Returns `this` for chaining.
|
|
556
657
|
|
|
557
|
-
|
|
658
|
+
##### `getTerms(): Term[]`
|
|
659
|
+
|
|
660
|
+
Returns all terms in the route's domain glossary.
|
|
558
661
|
|
|
559
662
|
##### `getRules(): string[]`
|
|
560
663
|
|
|
@@ -564,15 +667,101 @@ Returns the rules that must be followed in this route.
|
|
|
564
667
|
|
|
565
668
|
Returns the prohibitions that must never be done in this route.
|
|
566
669
|
|
|
670
|
+
##### `getKnowledgeBase(): Record<string, unknown>`
|
|
671
|
+
|
|
672
|
+
Gets the route's knowledge base containing any JSON structure the AI should know.
|
|
673
|
+
|
|
674
|
+
```typescript
|
|
675
|
+
const knowledge = route.getKnowledgeBase();
|
|
676
|
+
// Returns the route-specific knowledge base
|
|
677
|
+
```
|
|
678
|
+
|
|
567
679
|
##### `getRef(): RouteRef`
|
|
568
680
|
|
|
569
681
|
Returns a reference to this route.
|
|
570
682
|
|
|
571
|
-
|
|
683
|
+
##### `getAllSteps(): Step<TContext, TData>[]`
|
|
684
|
+
|
|
685
|
+
Gets all steps in this route via traversal from the initial step.
|
|
686
|
+
|
|
687
|
+
```typescript
|
|
688
|
+
const steps = route.getAllSteps();
|
|
689
|
+
// Returns array of all steps in the route
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
##### `getStep(stepId: string): Step<TContext, TData> | undefined`
|
|
693
|
+
|
|
694
|
+
Gets a specific step by ID.
|
|
695
|
+
|
|
696
|
+
```typescript
|
|
697
|
+
const step = route.getStep("ask_destination");
|
|
698
|
+
// Returns the step with the specified ID or undefined
|
|
699
|
+
```
|
|
572
700
|
|
|
573
701
|
##### `describe(): string`
|
|
574
702
|
|
|
575
|
-
Generates a description of this route's structure.
|
|
703
|
+
Generates a description of this route's structure for debugging.
|
|
704
|
+
|
|
705
|
+
```typescript
|
|
706
|
+
const description = route.describe();
|
|
707
|
+
console.log(description);
|
|
708
|
+
// Output:
|
|
709
|
+
// Route: Book Flight
|
|
710
|
+
// ID: route_book_flight
|
|
711
|
+
// Description: N/A
|
|
712
|
+
// Conditions: None
|
|
713
|
+
//
|
|
714
|
+
// Steps:
|
|
715
|
+
// - step_ask_destination: Ask where to fly
|
|
716
|
+
// -> step_ask_dates: Ask about travel dates
|
|
717
|
+
// -> step_ask_passengers: How many passengers?
|
|
718
|
+
```
|
|
719
|
+
|
|
720
|
+
##### `handleDataUpdate(data, previousCollected): Promise<Partial<TData>>`
|
|
721
|
+
|
|
722
|
+
Handles data updates for this route, calling the onDataUpdate hook if configured. Returns modified data after hook processing, or original data if no hook.
|
|
723
|
+
|
|
724
|
+
```typescript
|
|
725
|
+
const updatedData = await route.handleDataUpdate(newData, previousData);
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
**Parameters:**
|
|
729
|
+
|
|
730
|
+
- `data`: New collected data
|
|
731
|
+
- `previousCollected`: Previously collected data
|
|
732
|
+
|
|
733
|
+
**Returns:** Modified data after hook processing
|
|
734
|
+
|
|
735
|
+
##### `handleContextUpdate(newContext, previousContext): Promise<void>`
|
|
736
|
+
|
|
737
|
+
Handles context updates for this route, calling the onContextUpdate hook if configured.
|
|
738
|
+
|
|
739
|
+
```typescript
|
|
740
|
+
await route.handleContextUpdate(newContext, previousContext);
|
|
741
|
+
```
|
|
742
|
+
|
|
743
|
+
**Parameters:**
|
|
744
|
+
|
|
745
|
+
- `newContext`: New context
|
|
746
|
+
- `previousContext`: Previous context
|
|
747
|
+
|
|
748
|
+
##### `evaluateOnComplete(session, context?): Promise<RouteTransitionConfig<TContext, TData> | undefined>`
|
|
749
|
+
|
|
750
|
+
Evaluates the onComplete handler and returns transition config.
|
|
751
|
+
|
|
752
|
+
```typescript
|
|
753
|
+
const transition = await route.evaluateOnComplete(
|
|
754
|
+
{ data: session.data },
|
|
755
|
+
context
|
|
756
|
+
);
|
|
757
|
+
|
|
758
|
+
if (transition) {
|
|
759
|
+
// Transition to next route
|
|
760
|
+
console.log(`Next route: ${transition.nextStep}`);
|
|
761
|
+
}
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
**Note:** Routes no longer have a `getData()` method. Use `agent.getData()` or `agent.getData(routeId)` instead.
|
|
576
765
|
|
|
577
766
|
#### Properties
|
|
578
767
|
|
|
@@ -596,6 +785,10 @@ Conditions that trigger this route (readonly).
|
|
|
596
785
|
|
|
597
786
|
Starting step of the route (readonly).
|
|
598
787
|
|
|
788
|
+
##### `hooks?: RouteLifecycleHooks<TContext, TData>`
|
|
789
|
+
|
|
790
|
+
Route lifecycle hooks for managing route-specific data and behavior (readonly).
|
|
791
|
+
|
|
599
792
|
---
|
|
600
793
|
|
|
601
794
|
### `Step`
|
|
@@ -604,13 +797,15 @@ Represents a step within a conversation route.
|
|
|
604
797
|
|
|
605
798
|
#### Methods
|
|
606
799
|
|
|
607
|
-
##### `nextStep(spec:
|
|
800
|
+
##### `nextStep(spec: StepOptions): StepResult`
|
|
608
801
|
|
|
609
802
|
Creates a transition from this step and returns a chainable result.
|
|
610
803
|
|
|
611
804
|
```typescript
|
|
612
|
-
interface
|
|
613
|
-
|
|
805
|
+
interface StepOptions<TData = unknown> {
|
|
806
|
+
id?: string; // step id
|
|
807
|
+
description?: string; // Step description
|
|
808
|
+
prompt?: string; // Transition to a chat interaction
|
|
614
809
|
tool?: ToolRef; // Transition to execute a tool
|
|
615
810
|
step?: StepRef | symbol; // Transition to specific step or END_ROUTE
|
|
616
811
|
|
|
@@ -627,18 +822,18 @@ interface TransitionSpec<TData = unknown> {
|
|
|
627
822
|
condition?: string;
|
|
628
823
|
}
|
|
629
824
|
|
|
630
|
-
interface
|
|
825
|
+
interface StepResult<TData = unknown> {
|
|
631
826
|
id: string; // Step identifier
|
|
632
827
|
routeId: string; // Route identifier
|
|
633
|
-
nextStep: (spec:
|
|
828
|
+
nextStep: (spec: StepOptions<TData>) => StepResult<TData>;
|
|
634
829
|
}
|
|
635
830
|
```
|
|
636
831
|
|
|
637
832
|
**Parameters:**
|
|
638
833
|
|
|
639
|
-
- `spec`: The transition specification (see `
|
|
834
|
+
- `spec`: The transition specification (see `StepOptions` above). Can include an optional `condition` property for AI-evaluated step selection guidance.
|
|
640
835
|
|
|
641
|
-
**Returns:** A `
|
|
836
|
+
**Returns:** A `StepResult` that includes the target step's reference (`id`, `routeId`) and a `nextStep` method for chaining additional transitions.
|
|
642
837
|
|
|
643
838
|
**Example:**
|
|
644
839
|
|
|
@@ -666,14 +861,14 @@ const flightRoute = agent.createRoute<FlightData>({
|
|
|
666
861
|
|
|
667
862
|
// Approach 1: Step-by-step with data extraction and text conditions
|
|
668
863
|
const askDestination = flightRoute.initialStep.nextStep({
|
|
669
|
-
|
|
864
|
+
prompt: "Ask where they want to fly",
|
|
670
865
|
collect: ["destination"],
|
|
671
866
|
skipIf: (data) => !!data.destination, // Skip if already have destination
|
|
672
867
|
condition: "Customer hasn't specified destination yet", // AI-evaluated condition
|
|
673
868
|
});
|
|
674
869
|
|
|
675
870
|
const askDates = askDestination.nextStep({
|
|
676
|
-
|
|
871
|
+
prompt: "Ask about travel dates",
|
|
677
872
|
collect: ["departureDate"],
|
|
678
873
|
skipIf: (data) => !!data.departureDate,
|
|
679
874
|
requires: ["destination"], // Must have destination first
|
|
@@ -681,7 +876,7 @@ const askDates = askDestination.nextStep({
|
|
|
681
876
|
});
|
|
682
877
|
|
|
683
878
|
const askPassengers = askDates.nextStep({
|
|
684
|
-
|
|
879
|
+
prompt: "How many passengers?",
|
|
685
880
|
collect: ["passengers"],
|
|
686
881
|
skipIf: (data) => !!data.passengers,
|
|
687
882
|
});
|
|
@@ -693,24 +888,77 @@ console.log(askDestination.routeId); // Route ID
|
|
|
693
888
|
// Approach 2: Fluent chaining for linear flows
|
|
694
889
|
flightRoute.initialStep
|
|
695
890
|
.nextStep({
|
|
696
|
-
|
|
891
|
+
prompt: "Extract travel details",
|
|
697
892
|
collect: ["destination", "departureDate", "passengers"],
|
|
698
893
|
})
|
|
699
894
|
.nextStep({
|
|
700
|
-
|
|
895
|
+
prompt: "Present available flights",
|
|
701
896
|
})
|
|
702
897
|
.nextStep({ step: END_ROUTE });
|
|
703
898
|
|
|
704
|
-
//
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
899
|
+
// Automatic session management
|
|
900
|
+
const agent = new Agent({
|
|
901
|
+
name: "Travel Agent",
|
|
902
|
+
provider: new OpenAIProvider({ apiKey: process.env.OPENAI_API_KEY }),
|
|
903
|
+
persistence: { adapter: new PrismaAdapter({ prisma }) },
|
|
904
|
+
sessionId: "user-123" // Automatically loads or creates session
|
|
905
|
+
});
|
|
906
|
+
|
|
907
|
+
const response = await agent.respond("I want to book a flight to Paris");
|
|
908
|
+
console.log(agent.session.getData<FlightData>()); // { destination: "Paris", ... }
|
|
708
909
|
```
|
|
709
910
|
|
|
710
911
|
##### `addGuideline(guideline: Guideline): void`
|
|
711
912
|
|
|
712
913
|
Adds a guideline specific to this step.
|
|
713
914
|
|
|
915
|
+
##### `getGuidelines(): Guideline<TContext>[]`
|
|
916
|
+
|
|
917
|
+
Gets all guidelines for this step.
|
|
918
|
+
|
|
919
|
+
```typescript
|
|
920
|
+
const guidelines = step.getGuidelines();
|
|
921
|
+
// Returns array of guidelines specific to this step
|
|
922
|
+
```
|
|
923
|
+
|
|
924
|
+
##### `getTransitions(): Step<TContext, TData>[]`
|
|
925
|
+
|
|
926
|
+
Gets all transitions from this step.
|
|
927
|
+
|
|
928
|
+
```typescript
|
|
929
|
+
const nextSteps = step.getTransitions();
|
|
930
|
+
// Returns array of possible next steps
|
|
931
|
+
```
|
|
932
|
+
|
|
933
|
+
##### `shouldSkip(data: Partial<TData>): boolean`
|
|
934
|
+
|
|
935
|
+
Checks if this step should be skipped based on collected data.
|
|
936
|
+
|
|
937
|
+
```typescript
|
|
938
|
+
if (step.shouldSkip(session.data)) {
|
|
939
|
+
// Skip this step
|
|
940
|
+
}
|
|
941
|
+
```
|
|
942
|
+
|
|
943
|
+
##### `hasRequires(data: Partial<TData>): boolean`
|
|
944
|
+
|
|
945
|
+
Checks if this step has all required data to proceed.
|
|
946
|
+
|
|
947
|
+
```typescript
|
|
948
|
+
if (step.hasRequires(session.data)) {
|
|
949
|
+
// Step can proceed
|
|
950
|
+
}
|
|
951
|
+
```
|
|
952
|
+
|
|
953
|
+
##### `asStepResult(): StepResult<TContext, TData>`
|
|
954
|
+
|
|
955
|
+
Creates a transition result for this step that supports chaining.
|
|
956
|
+
|
|
957
|
+
```typescript
|
|
958
|
+
const result = step.asStepResult();
|
|
959
|
+
// Returns StepResult with nextStep method for chaining
|
|
960
|
+
```
|
|
961
|
+
|
|
714
962
|
##### `configure(config): this`
|
|
715
963
|
|
|
716
964
|
Configure the step properties after creation. Useful for overriding initial step configuration. Returns `this` for chaining.
|
|
@@ -719,15 +967,16 @@ Configure the step properties after creation. Useful for overriding initial step
|
|
|
719
967
|
// Configure initial step after route creation
|
|
720
968
|
route.initialStep.configure({
|
|
721
969
|
description: "Welcome! Let's get started",
|
|
722
|
-
|
|
970
|
+
collect: ["name", "email"],
|
|
723
971
|
skipIf: (data) => !!data.name && !!data.email,
|
|
724
972
|
requires: [],
|
|
973
|
+
prompt: "Hello! Let's get started with your information.",
|
|
725
974
|
});
|
|
726
975
|
|
|
727
976
|
// Or configure any step
|
|
728
|
-
const askName = route.initialStep.nextStep({
|
|
977
|
+
const askName = route.initialStep.nextStep({ prompt: "Ask for name" });
|
|
729
978
|
askName.configure({
|
|
730
|
-
|
|
979
|
+
collect: ["firstName", "lastName"],
|
|
731
980
|
});
|
|
732
981
|
```
|
|
733
982
|
|
|
@@ -735,9 +984,10 @@ askName.configure({
|
|
|
735
984
|
|
|
736
985
|
- `config`: Configuration object with optional properties:
|
|
737
986
|
- `description?: string` - Step description
|
|
738
|
-
- `
|
|
987
|
+
- `collect?: string[]` - Fields to collect in this step
|
|
739
988
|
- `skipIf?: (data: Partial<TData>) => boolean` - Skip condition function
|
|
740
989
|
- `requires?: string[]` - Required data prerequisites
|
|
990
|
+
- `prompt?: Template<TContext, TData>` - Step prompt template
|
|
741
991
|
|
|
742
992
|
**Returns:** `this` for method chaining
|
|
743
993
|
|
|
@@ -757,59 +1007,13 @@ Step description (readonly).
|
|
|
757
1007
|
|
|
758
1008
|
---
|
|
759
1009
|
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
Registry for organizing agent tools and methods by domain.
|
|
763
|
-
|
|
764
|
-
#### Methods
|
|
765
|
-
|
|
766
|
-
##### `register<TDomain>(name: string, domain: TDomain): void`
|
|
767
|
-
|
|
768
|
-
Registers a new domain with its tools/methods. Throws error if domain name already exists.
|
|
769
|
-
|
|
770
|
-
##### `get<TDomain>(name: string): TDomain | undefined`
|
|
771
|
-
|
|
772
|
-
Gets a registered domain by name. Returns `undefined` if not found.
|
|
773
|
-
|
|
774
|
-
##### `has(name: string): boolean`
|
|
775
|
-
|
|
776
|
-
Checks if a domain is registered.
|
|
777
|
-
|
|
778
|
-
##### `all(): Record<string, Record<string, unknown>>`
|
|
779
|
-
|
|
780
|
-
Returns all registered domains as a single object.
|
|
781
|
-
|
|
782
|
-
##### `getFiltered(allowedNames?: string[]): Record<string, Record<string, unknown>>`
|
|
783
|
-
|
|
784
|
-
Returns filtered domains by names. If `allowedNames` is `undefined`, returns all domains.
|
|
785
|
-
|
|
786
|
-
**Example:**
|
|
787
|
-
|
|
788
|
-
```typescript
|
|
789
|
-
const registry = new DomainRegistry();
|
|
790
|
-
|
|
791
|
-
registry.register("payment", {
|
|
792
|
-
processPayment: async (amount: number) => {
|
|
793
|
-
/* ... */
|
|
794
|
-
},
|
|
795
|
-
});
|
|
796
|
-
|
|
797
|
-
registry.register("shipping", {
|
|
798
|
-
calculateShipping: (zipCode: string) => {
|
|
799
|
-
/* ... */
|
|
800
|
-
},
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
// Get specific domains
|
|
804
|
-
const filtered = registry.getFiltered(["payment"]); // Only payment domain
|
|
1010
|
+
---
|
|
805
1011
|
|
|
806
|
-
|
|
807
|
-
const all = registry.getFiltered(); // payment + shipping
|
|
808
|
-
```
|
|
1012
|
+
## Tool Execution
|
|
809
1013
|
|
|
810
|
-
|
|
1014
|
+
Tools provide a powerful way to execute custom logic, access external APIs, and enrich conversation context before AI response generation.
|
|
811
1015
|
|
|
812
|
-
|
|
1016
|
+
**See Also:** [TOOLS.md](./TOOLS.md) - Complete guide to tool execution, lifecycle, and best practices
|
|
813
1017
|
|
|
814
1018
|
---
|
|
815
1019
|
|
|
@@ -1077,6 +1281,192 @@ Marks a session as completed.
|
|
|
1077
1281
|
|
|
1078
1282
|
Marks a session as abandoned.
|
|
1079
1283
|
|
|
1284
|
+
##### `updateCollectedData(sessionId: string, collectedData: Record<string, unknown>): Promise<SessionData | null>`
|
|
1285
|
+
|
|
1286
|
+
Updates collected data in session.
|
|
1287
|
+
|
|
1288
|
+
##### `updateRouteStep(sessionId: string, route?: string, step?: string): Promise<SessionData | null>`
|
|
1289
|
+
|
|
1290
|
+
Updates current route and step in session.
|
|
1291
|
+
|
|
1292
|
+
##### `getUserMessages(userId?: string, limit?: number): Promise<MessageData[]>`
|
|
1293
|
+
|
|
1294
|
+
Gets messages for a user.
|
|
1295
|
+
|
|
1296
|
+
##### `deleteSession(sessionId: string): Promise<boolean>`
|
|
1297
|
+
|
|
1298
|
+
Deletes a session and all its messages.
|
|
1299
|
+
|
|
1300
|
+
##### `messageToEvent(message: MessageData): Event | undefined`
|
|
1301
|
+
|
|
1302
|
+
Helper: Convert message data to Event format.
|
|
1303
|
+
|
|
1304
|
+
##### `saveSessionState(sessionId: string, sessionStep: SessionState): Promise<SessionData | null>`
|
|
1305
|
+
|
|
1306
|
+
Saves SessionState to database by converting to SessionData.
|
|
1307
|
+
|
|
1308
|
+
```typescript
|
|
1309
|
+
const saved = await persistence.saveSessionState(sessionId, sessionState);
|
|
1310
|
+
```
|
|
1311
|
+
|
|
1312
|
+
##### `loadSessionState(sessionId: string): Promise<SessionState | null>`
|
|
1313
|
+
|
|
1314
|
+
Loads SessionState from database by converting from SessionData.
|
|
1315
|
+
|
|
1316
|
+
```typescript
|
|
1317
|
+
const sessionState = await persistence.loadSessionState(sessionId);
|
|
1318
|
+
```
|
|
1319
|
+
|
|
1320
|
+
##### `createSessionWithStep(options: CreateSessionOptions): Promise<{ sessionData: SessionData; sessionStep: SessionState }>`
|
|
1321
|
+
|
|
1322
|
+
Creates a new session with both SessionData and initialized SessionState.
|
|
1323
|
+
|
|
1324
|
+
```typescript
|
|
1325
|
+
const { sessionData, sessionStep } = await persistence.createSessionWithStep({
|
|
1326
|
+
userId: "user_123",
|
|
1327
|
+
agentName: "Travel Agent",
|
|
1328
|
+
initialData: { channel: "web" },
|
|
1329
|
+
});
|
|
1330
|
+
```
|
|
1331
|
+
|
|
1332
|
+
---
|
|
1333
|
+
|
|
1334
|
+
### `RoutingEngine<TContext, TData>`
|
|
1335
|
+
|
|
1336
|
+
Handles route and step selection logic for conversation orchestration.
|
|
1337
|
+
|
|
1338
|
+
#### Constructor
|
|
1339
|
+
|
|
1340
|
+
```typescript
|
|
1341
|
+
new RoutingEngine<TContext, TData>(options?: RoutingEngineOptions)
|
|
1342
|
+
|
|
1343
|
+
interface RoutingEngineOptions {
|
|
1344
|
+
allowRouteSwitch?: boolean; // Default: true
|
|
1345
|
+
switchThreshold?: number; // Default: 70 (0-100)
|
|
1346
|
+
maxCandidates?: number; // Default: 5
|
|
1347
|
+
}
|
|
1348
|
+
```
|
|
1349
|
+
|
|
1350
|
+
#### Methods
|
|
1351
|
+
|
|
1352
|
+
##### `decideRouteAndStep(params): Promise<RoutingDecision>`
|
|
1353
|
+
|
|
1354
|
+
Combines route selection and step selection into a single orchestrated decision.
|
|
1355
|
+
|
|
1356
|
+
**Parameters:**
|
|
1357
|
+
|
|
1358
|
+
- `routes`: Array of available routes
|
|
1359
|
+
- `session`: Current session state
|
|
1360
|
+
- `history`: Conversation history
|
|
1361
|
+
- `agentOptions`: Agent metadata
|
|
1362
|
+
- `provider`: AI provider for scoring
|
|
1363
|
+
- `context`: Agent context
|
|
1364
|
+
- `signal`: Optional abort signal
|
|
1365
|
+
|
|
1366
|
+
**Returns:** Routing decision with selected route, step, directives, and updated session
|
|
1367
|
+
|
|
1368
|
+
---
|
|
1369
|
+
|
|
1370
|
+
### `ResponseEngine<TContext>`
|
|
1371
|
+
|
|
1372
|
+
Builds prompts and response schemas for AI message generation.
|
|
1373
|
+
|
|
1374
|
+
#### Constructor
|
|
1375
|
+
|
|
1376
|
+
```typescript
|
|
1377
|
+
new ResponseEngine<TContext>();
|
|
1378
|
+
```
|
|
1379
|
+
|
|
1380
|
+
#### Methods
|
|
1381
|
+
|
|
1382
|
+
##### `responseSchemaForRoute(route, currentStep?): StructuredSchema`
|
|
1383
|
+
|
|
1384
|
+
Builds JSON schema for AI responses based on route and current step.
|
|
1385
|
+
|
|
1386
|
+
##### `buildResponsePrompt(params): Promise<string>`
|
|
1387
|
+
|
|
1388
|
+
Builds a comprehensive prompt for AI response generation including context, guidelines, and route information.
|
|
1389
|
+
|
|
1390
|
+
**Parameters:**
|
|
1391
|
+
|
|
1392
|
+
- `route`: Current route
|
|
1393
|
+
- `currentStep`: Current step in route
|
|
1394
|
+
- `rules`: Route-specific rules
|
|
1395
|
+
- `prohibitions`: Route-specific prohibitions
|
|
1396
|
+
- `directives`: Response directives
|
|
1397
|
+
- `history`: Conversation history
|
|
1398
|
+
- `lastMessage`: Last user message
|
|
1399
|
+
- `agentOptions`: Agent metadata
|
|
1400
|
+
- `context`: Agent context
|
|
1401
|
+
- `session`: Current session state
|
|
1402
|
+
|
|
1403
|
+
##### `buildFallbackPrompt(params): Promise<string>`
|
|
1404
|
+
|
|
1405
|
+
Builds a fallback prompt when no routes are configured.
|
|
1406
|
+
|
|
1407
|
+
---
|
|
1408
|
+
|
|
1409
|
+
### `ToolExecutor<TContext, TData>`
|
|
1410
|
+
|
|
1411
|
+
Executes tools with context and security enforcement.
|
|
1412
|
+
|
|
1413
|
+
#### Constructor
|
|
1414
|
+
|
|
1415
|
+
```typescript
|
|
1416
|
+
new ToolExecutor<TContext, TData>();
|
|
1417
|
+
```
|
|
1418
|
+
|
|
1419
|
+
#### Methods
|
|
1420
|
+
|
|
1421
|
+
##### `executeTool(params): Promise<ToolExecutionResult>`
|
|
1422
|
+
|
|
1423
|
+
Executes a single tool with domain security enforcement.
|
|
1424
|
+
|
|
1425
|
+
**Parameters:**
|
|
1426
|
+
|
|
1427
|
+
- `tool`: Tool reference to execute
|
|
1428
|
+
- `context`: Agent context
|
|
1429
|
+
- `updateContext`: Function to update context
|
|
1430
|
+
- `history`: Conversation history
|
|
1431
|
+
- `data`: Collected session data
|
|
1432
|
+
- `allowedDomains`: Array of allowed domain names
|
|
1433
|
+
|
|
1434
|
+
**Returns:** Tool execution result with success status and data
|
|
1435
|
+
|
|
1436
|
+
##### `executeTools(params): Promise<ToolExecutionResult[]>`
|
|
1437
|
+
|
|
1438
|
+
Executes multiple tools in sequence, stopping on first failure.
|
|
1439
|
+
|
|
1440
|
+
---
|
|
1441
|
+
|
|
1442
|
+
### `Events`
|
|
1443
|
+
|
|
1444
|
+
Utility functions for creating and adapting conversation events.
|
|
1445
|
+
|
|
1446
|
+
#### Functions
|
|
1447
|
+
|
|
1448
|
+
##### `adaptEvent(event): string`
|
|
1449
|
+
|
|
1450
|
+
Adapts an event for inclusion in AI prompts by transforming it into serializable format.
|
|
1451
|
+
|
|
1452
|
+
```typescript
|
|
1453
|
+
const promptText = adaptEvent(messageEvent);
|
|
1454
|
+
```
|
|
1455
|
+
|
|
1456
|
+
##### History Format
|
|
1457
|
+
|
|
1458
|
+
For conversation history, use the simplified `History` type instead of manually creating events. See the [History Format](#history-format) section below for details.
|
|
1459
|
+
|
|
1460
|
+
##### `createToolEvent(source, toolCalls, timestamp?): Event`
|
|
1461
|
+
|
|
1462
|
+
Creates a tool execution event.
|
|
1463
|
+
|
|
1464
|
+
```typescript
|
|
1465
|
+
const toolEvent = createToolEvent(MessageRole.AGENT, [
|
|
1466
|
+
{ tool_id: "search_flights", arguments: { from: "NYC", to: "LAX" }, result: { flights: [...] } }
|
|
1467
|
+
]);
|
|
1468
|
+
```
|
|
1469
|
+
|
|
1080
1470
|
---
|
|
1081
1471
|
|
|
1082
1472
|
### `PrismaAdapter`
|
|
@@ -1476,7 +1866,7 @@ interface MessageData {
|
|
|
1476
1866
|
id: string;
|
|
1477
1867
|
sessionId: string;
|
|
1478
1868
|
userId?: string;
|
|
1479
|
-
role: MessageRole; // "user" | "agent" | "system"
|
|
1869
|
+
role: MessageRole; // "user" | "assistant" | "agent" | "system"
|
|
1480
1870
|
content: string;
|
|
1481
1871
|
route?: string;
|
|
1482
1872
|
step?: string;
|
|
@@ -1532,15 +1922,45 @@ Adds domain glossary terms.
|
|
|
1532
1922
|
|
|
1533
1923
|
Adds guidelines section.
|
|
1534
1924
|
|
|
1535
|
-
##### `
|
|
1925
|
+
##### `addActiveRoutes(routes): this`
|
|
1926
|
+
|
|
1927
|
+
Adds active routes to the prompt.
|
|
1928
|
+
|
|
1929
|
+
##### `build(): string`
|
|
1930
|
+
|
|
1931
|
+
Builds the final prompt string.
|
|
1536
1932
|
|
|
1537
|
-
|
|
1933
|
+
---
|
|
1934
|
+
|
|
1935
|
+
### `PromptComposer`
|
|
1936
|
+
|
|
1937
|
+
Constructs prompts for AI generation.
|
|
1938
|
+
|
|
1939
|
+
**Note:** As of the latest version, many methods in `PromptComposer` are `async` to support function-based templates.
|
|
1940
|
+
|
|
1941
|
+
#### Methods
|
|
1942
|
+
|
|
1943
|
+
##### `addIdentity(name: string, description?: string, goal?: string): this`
|
|
1944
|
+
|
|
1945
|
+
Adds agent identity section.
|
|
1946
|
+
|
|
1947
|
+
##### `addContext(variables: ContextVariable[]): this`
|
|
1948
|
+
|
|
1949
|
+
Adds context variables.
|
|
1950
|
+
|
|
1951
|
+
##### `addGlossary(terms: Term[]): this`
|
|
1952
|
+
|
|
1953
|
+
Adds domain glossary terms.
|
|
1954
|
+
|
|
1955
|
+
##### `addGuidelinesForMessageGeneration(guidelines: GuidelineMatch[]): this`
|
|
1956
|
+
|
|
1957
|
+
Adds guidelines section.
|
|
1538
1958
|
|
|
1539
1959
|
##### `addActiveRoutes(routes): this`
|
|
1540
1960
|
|
|
1541
1961
|
Adds active routes to the prompt.
|
|
1542
1962
|
|
|
1543
|
-
##### `build(): string
|
|
1963
|
+
##### `build(): Promise<string>`
|
|
1544
1964
|
|
|
1545
1965
|
Builds the final prompt string.
|
|
1546
1966
|
|
|
@@ -1548,67 +1968,181 @@ Builds the final prompt string.
|
|
|
1548
1968
|
|
|
1549
1969
|
## Utility Functions
|
|
1550
1970
|
|
|
1551
|
-
### `
|
|
1971
|
+
### `Tool<TContext, TArgs, TResult, TData>`
|
|
1552
1972
|
|
|
1553
|
-
Defines a type-safe tool.
|
|
1973
|
+
Defines a type-safe tool using the Tool interface.
|
|
1554
1974
|
|
|
1555
1975
|
```typescript
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1976
|
+
interface Tool<
|
|
1977
|
+
TContext = unknown,
|
|
1978
|
+
TArgs extends unknown[] = unknown[],
|
|
1979
|
+
TResult = unknown,
|
|
1980
|
+
TData = unknown
|
|
1981
|
+
> {
|
|
1982
|
+
id: string; // Unique tool identifier
|
|
1983
|
+
description?: string; // Description for AI discovery
|
|
1984
|
+
parameters?: unknown; // JSON Schema for tool parameters
|
|
1985
|
+
handler: ToolHandler<TContext, TArgs, TResult, TData>; // Tool execution handler
|
|
1986
|
+
}
|
|
1987
|
+
```
|
|
1988
|
+
|
|
1989
|
+
**Example:**
|
|
1990
|
+
|
|
1991
|
+
```typescript
|
|
1992
|
+
const getTool: Tool<MyContext, [id: string], Data> = {
|
|
1993
|
+
id: "get_data",
|
|
1994
|
+
description: "Fetches data by ID",
|
|
1995
|
+
parameters: {
|
|
1996
|
+
type: "object",
|
|
1997
|
+
properties: {
|
|
1998
|
+
id: { type: "string", description: "The data ID to fetch" },
|
|
1999
|
+
},
|
|
2000
|
+
required: ["id"],
|
|
2001
|
+
},
|
|
2002
|
+
handler: async (toolContext, args) => {
|
|
2003
|
+
return { data: await fetchData(args.id) };
|
|
2004
|
+
},
|
|
2005
|
+
};
|
|
1565
2006
|
```
|
|
1566
2007
|
|
|
1567
|
-
|
|
2008
|
+
---
|
|
2009
|
+
|
|
2010
|
+
### `formatKnowledgeBase(data, title?, maxDepth?)`
|
|
2011
|
+
|
|
2012
|
+
Formats a JSON structure into readable markdown format for AI prompts. Handles nested objects, arrays, and primitive values.
|
|
2013
|
+
|
|
2014
|
+
```typescript
|
|
2015
|
+
formatKnowledgeBase(
|
|
2016
|
+
data: Record<string, unknown> | unknown,
|
|
2017
|
+
title?: string,
|
|
2018
|
+
maxDepth?: number
|
|
2019
|
+
): string
|
|
2020
|
+
```
|
|
2021
|
+
|
|
2022
|
+
**Parameters:**
|
|
2023
|
+
|
|
2024
|
+
- `data` - The JSON data to format
|
|
2025
|
+
- `title` - Optional title for the knowledge base section
|
|
2026
|
+
- `maxDepth` - Maximum nesting depth (default: 3)
|
|
2027
|
+
|
|
2028
|
+
**Returns:** Formatted markdown string
|
|
1568
2029
|
|
|
1569
2030
|
**Example:**
|
|
1570
2031
|
|
|
1571
2032
|
```typescript
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
2033
|
+
import { formatKnowledgeBase } from "@falai/agent";
|
|
2034
|
+
|
|
2035
|
+
const knowledge = {
|
|
2036
|
+
company: {
|
|
2037
|
+
name: "Acme Corp",
|
|
2038
|
+
products: ["Widget A", "Widget B"],
|
|
2039
|
+
locations: {
|
|
2040
|
+
headquarters: "NYC",
|
|
2041
|
+
branches: ["LA", "Chicago"],
|
|
2042
|
+
},
|
|
1576
2043
|
},
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
2044
|
+
};
|
|
2045
|
+
|
|
2046
|
+
const markdown = formatKnowledgeBase(knowledge, "Company Information");
|
|
2047
|
+
// Output:
|
|
2048
|
+
// ## Company Information
|
|
2049
|
+
//
|
|
2050
|
+
// - **name**: Acme Corp
|
|
2051
|
+
// - **products**:
|
|
2052
|
+
// - Widget A
|
|
2053
|
+
// - Widget B
|
|
2054
|
+
// - **locations**:
|
|
2055
|
+
// - **headquarters**: NYC
|
|
2056
|
+
// - **branches**:
|
|
2057
|
+
// - LA
|
|
2058
|
+
// - Chicago
|
|
1582
2059
|
```
|
|
1583
2060
|
|
|
1584
2061
|
---
|
|
1585
2062
|
|
|
1586
|
-
###
|
|
2063
|
+
### History Format
|
|
2064
|
+
|
|
2065
|
+
The agent accepts a simplified history format for conversation context. This format is easier to use than the internal Event structure.
|
|
1587
2066
|
|
|
1588
|
-
|
|
2067
|
+
#### Types
|
|
1589
2068
|
|
|
1590
2069
|
```typescript
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
2070
|
+
type Role = "user" | "assistant" | "tool" | "system";
|
|
2071
|
+
|
|
2072
|
+
type HistoryItem =
|
|
2073
|
+
| {
|
|
2074
|
+
role: "user";
|
|
2075
|
+
content: string;
|
|
2076
|
+
name?: string; // Optional participant name
|
|
2077
|
+
}
|
|
2078
|
+
| {
|
|
2079
|
+
role: "assistant";
|
|
2080
|
+
content: string | null;
|
|
2081
|
+
tool_calls?: Array<{
|
|
2082
|
+
id: string;
|
|
2083
|
+
name: string;
|
|
2084
|
+
arguments: Record<string, unknown>;
|
|
2085
|
+
}>;
|
|
2086
|
+
}
|
|
2087
|
+
| {
|
|
2088
|
+
role: "tool";
|
|
2089
|
+
tool_call_id: string;
|
|
2090
|
+
name: string;
|
|
2091
|
+
content: any;
|
|
2092
|
+
}
|
|
2093
|
+
| {
|
|
2094
|
+
role: "system";
|
|
2095
|
+
content: string;
|
|
2096
|
+
};
|
|
2097
|
+
|
|
2098
|
+
type History = HistoryItem[];
|
|
1597
2099
|
```
|
|
1598
2100
|
|
|
1599
2101
|
**Example:**
|
|
1600
2102
|
|
|
1601
2103
|
```typescript
|
|
1602
|
-
//
|
|
1603
|
-
|
|
2104
|
+
// Simple conversation history
|
|
2105
|
+
const history: History = [
|
|
2106
|
+
{
|
|
2107
|
+
role: "user",
|
|
2108
|
+
content: "Hello! Can you help me book a flight?",
|
|
2109
|
+
name: "Alice"
|
|
2110
|
+
},
|
|
2111
|
+
{
|
|
2112
|
+
role: "assistant",
|
|
2113
|
+
content: "I'd be happy to help you book a flight. Where would you like to go?"
|
|
2114
|
+
},
|
|
2115
|
+
{
|
|
2116
|
+
role: "user",
|
|
2117
|
+
content: "I want to go to Paris next Friday for 2 people.",
|
|
2118
|
+
name: "Alice"
|
|
2119
|
+
}
|
|
2120
|
+
];
|
|
1604
2121
|
|
|
1605
|
-
//
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
2122
|
+
// Assistant with tool calls
|
|
2123
|
+
const historyWithTools: History = [
|
|
2124
|
+
{
|
|
2125
|
+
role: "assistant",
|
|
2126
|
+
content: "I'll search for flights to Paris.",
|
|
2127
|
+
tool_calls: [
|
|
2128
|
+
{
|
|
2129
|
+
id: "search_flights_1",
|
|
2130
|
+
name: "search_flights",
|
|
2131
|
+
arguments: {
|
|
2132
|
+
destination: "Paris",
|
|
2133
|
+
date: "2025-10-18",
|
|
2134
|
+
passengers: 2
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
]
|
|
2138
|
+
},
|
|
2139
|
+
{
|
|
2140
|
+
role: "tool",
|
|
2141
|
+
tool_call_id: "search_flights_1",
|
|
2142
|
+
name: "search_flights",
|
|
2143
|
+
content: { flights: [...] } // Tool execution result
|
|
2144
|
+
}
|
|
2145
|
+
];
|
|
1612
2146
|
```
|
|
1613
2147
|
|
|
1614
2148
|
---
|
|
@@ -1619,7 +2153,7 @@ Creates a tool execution event.
|
|
|
1619
2153
|
|
|
1620
2154
|
```typescript
|
|
1621
2155
|
createToolEvent(
|
|
1622
|
-
source:
|
|
2156
|
+
source: MessageRole,
|
|
1623
2157
|
toolCalls: ToolCall[],
|
|
1624
2158
|
timestamp?: string // Optional: provide custom timestamp (ISO 8601 format)
|
|
1625
2159
|
): Event
|
|
@@ -1629,13 +2163,13 @@ createToolEvent(
|
|
|
1629
2163
|
|
|
1630
2164
|
```typescript
|
|
1631
2165
|
// With auto-generated timestamp
|
|
1632
|
-
createToolEvent(
|
|
2166
|
+
createToolEvent(MessageRole.AGENT, [
|
|
1633
2167
|
{ tool_id: "get_data", arguments: { id: "123" }, result: { data: {...} } }
|
|
1634
2168
|
]);
|
|
1635
2169
|
|
|
1636
2170
|
// With custom timestamp
|
|
1637
2171
|
createToolEvent(
|
|
1638
|
-
|
|
2172
|
+
MessageRole.AGENT,
|
|
1639
2173
|
[{ tool_id: "get_data", arguments: { id: "123" }, result: { data: {...} } }],
|
|
1640
2174
|
"2025-10-13T10:30:00Z"
|
|
1641
2175
|
);
|
|
@@ -1658,10 +2192,10 @@ adaptEvent(event: EmittedEvent): Event
|
|
|
1658
2192
|
### `Term`
|
|
1659
2193
|
|
|
1660
2194
|
```typescript
|
|
1661
|
-
interface Term {
|
|
1662
|
-
name:
|
|
1663
|
-
description:
|
|
1664
|
-
synonyms?:
|
|
2195
|
+
interface Term<TContext = unknown> {
|
|
2196
|
+
name: Template<TContext>;
|
|
2197
|
+
description: Template<TContext>;
|
|
2198
|
+
synonyms?: Template<TContext>[];
|
|
1665
2199
|
}
|
|
1666
2200
|
```
|
|
1667
2201
|
|
|
@@ -1670,10 +2204,10 @@ interface Term {
|
|
|
1670
2204
|
### `Guideline`
|
|
1671
2205
|
|
|
1672
2206
|
```typescript
|
|
1673
|
-
interface Guideline {
|
|
2207
|
+
interface Guideline<TContext = unknown> {
|
|
1674
2208
|
id?: string;
|
|
1675
|
-
condition?:
|
|
1676
|
-
action:
|
|
2209
|
+
condition?: Template<TContext>;
|
|
2210
|
+
action: Template<TContext>;
|
|
1677
2211
|
enabled?: boolean; // Default: true
|
|
1678
2212
|
tags?: string[];
|
|
1679
2213
|
tools?: ToolRef[];
|
|
@@ -1683,25 +2217,47 @@ interface Guideline {
|
|
|
1683
2217
|
|
|
1684
2218
|
---
|
|
1685
2219
|
|
|
1686
|
-
### `
|
|
2220
|
+
### `RouteLifecycleHooks<TContext, TData>`
|
|
2221
|
+
|
|
2222
|
+
Route lifecycle hooks for managing route-specific data and behavior.
|
|
1687
2223
|
|
|
1688
2224
|
```typescript
|
|
1689
|
-
interface
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
2225
|
+
interface RouteLifecycleHooks<TContext = unknown, TData = unknown> {
|
|
2226
|
+
/**
|
|
2227
|
+
* Called after collected data is updated for this route (from AI response or tool execution)
|
|
2228
|
+
* Useful for validation, enrichment, or persistence of route-specific collected data
|
|
2229
|
+
* Return modified collected data or the same data to keep it unchanged
|
|
2230
|
+
*
|
|
2231
|
+
* Unlike Agent-level onDataUpdate, this only triggers for data changes in this specific route.
|
|
2232
|
+
*/
|
|
2233
|
+
onDataUpdate?: (
|
|
2234
|
+
data: Partial<TData>,
|
|
2235
|
+
previousCollected: Partial<TData>
|
|
2236
|
+
) => Partial<TData> | Promise<Partial<TData>>;
|
|
2237
|
+
|
|
2238
|
+
/**
|
|
2239
|
+
* Called after context is updated via updateContext() when this route is active
|
|
2240
|
+
* Useful for route-specific context reactions, validation, or side effects
|
|
2241
|
+
*
|
|
2242
|
+
* Unlike Agent-level onContextUpdate, this only triggers when this specific route is active.
|
|
2243
|
+
*/
|
|
2244
|
+
onContextUpdate?: (
|
|
2245
|
+
newContext: TContext,
|
|
2246
|
+
previousContext: TContext
|
|
2247
|
+
) => void | Promise<void>;
|
|
1694
2248
|
}
|
|
1695
2249
|
```
|
|
1696
2250
|
|
|
1697
2251
|
---
|
|
1698
2252
|
|
|
2253
|
+
---
|
|
2254
|
+
|
|
1699
2255
|
### `Event`
|
|
1700
2256
|
|
|
1701
2257
|
```typescript
|
|
1702
2258
|
interface Event {
|
|
1703
2259
|
kind: EventKind;
|
|
1704
|
-
source:
|
|
2260
|
+
source: MessageRole;
|
|
1705
2261
|
data: MessageEventData | ToolEventData | StatusEventData;
|
|
1706
2262
|
creation_utc: string;
|
|
1707
2263
|
}
|
|
@@ -1712,9 +2268,10 @@ enum EventKind {
|
|
|
1712
2268
|
STATUS = "status",
|
|
1713
2269
|
}
|
|
1714
2270
|
|
|
1715
|
-
enum
|
|
1716
|
-
|
|
1717
|
-
|
|
2271
|
+
enum MessageRole {
|
|
2272
|
+
USER = "user",
|
|
2273
|
+
ASSISTANT = "assistant",
|
|
2274
|
+
AGENT = "agent",
|
|
1718
2275
|
SYSTEM = "system",
|
|
1719
2276
|
}
|
|
1720
2277
|
```
|
|
@@ -1743,6 +2300,39 @@ interface ToolResult<TReturn> {
|
|
|
1743
2300
|
|
|
1744
2301
|
---
|
|
1745
2302
|
|
|
2303
|
+
### `RoutingDecision`
|
|
2304
|
+
|
|
2305
|
+
Result of route and step selection process.
|
|
2306
|
+
|
|
2307
|
+
```typescript
|
|
2308
|
+
interface RoutingDecision {
|
|
2309
|
+
context: string;
|
|
2310
|
+
routes: Record<string, number>; // Route scores
|
|
2311
|
+
responseDirectives?: string[];
|
|
2312
|
+
extractions?: unknown;
|
|
2313
|
+
contextUpdate?: Record<string, unknown>;
|
|
2314
|
+
}
|
|
2315
|
+
```
|
|
2316
|
+
|
|
2317
|
+
---
|
|
2318
|
+
|
|
2319
|
+
### `ToolExecutionResult`
|
|
2320
|
+
|
|
2321
|
+
Result of tool execution.
|
|
2322
|
+
|
|
2323
|
+
```typescript
|
|
2324
|
+
interface ToolExecutionResult {
|
|
2325
|
+
toolName: string;
|
|
2326
|
+
success: boolean;
|
|
2327
|
+
data?: unknown;
|
|
2328
|
+
contextUpdate?: Record<string, unknown>;
|
|
2329
|
+
dataUpdate?: Record<string, unknown>;
|
|
2330
|
+
error?: string;
|
|
2331
|
+
}
|
|
2332
|
+
```
|
|
2333
|
+
|
|
2334
|
+
---
|
|
2335
|
+
|
|
1746
2336
|
## Types
|
|
1747
2337
|
|
|
1748
2338
|
### `SessionState<TData>`
|
|
@@ -1772,14 +2362,14 @@ interface SessionState<TData = Record<string, unknown>> {
|
|
|
1772
2362
|
* Data collected during the current route
|
|
1773
2363
|
* Convenience reference to dataByRoute[currentRoute.id]
|
|
1774
2364
|
*/
|
|
1775
|
-
data
|
|
2365
|
+
data?: Partial<TData>;
|
|
1776
2366
|
|
|
1777
2367
|
/**
|
|
1778
|
-
*
|
|
1779
|
-
*
|
|
1780
|
-
*
|
|
2368
|
+
* Data collected organized by route ID
|
|
2369
|
+
* Persists data when switching between routes
|
|
2370
|
+
* Allows resuming incomplete routes where they left off
|
|
1781
2371
|
*/
|
|
1782
|
-
dataByRoute
|
|
2372
|
+
dataByRoute?: Record<string, Partial<unknown>>;
|
|
1783
2373
|
|
|
1784
2374
|
/** History of routes visited in this session */
|
|
1785
2375
|
routeHistory: Array<{
|
|
@@ -1789,6 +2379,16 @@ interface SessionState<TData = Record<string, unknown>> {
|
|
|
1789
2379
|
completed: boolean;
|
|
1790
2380
|
}>;
|
|
1791
2381
|
|
|
2382
|
+
/**
|
|
2383
|
+
* Pending route transition after completion
|
|
2384
|
+
* Set when a route completes with onComplete handler
|
|
2385
|
+
*/
|
|
2386
|
+
pendingTransition?: {
|
|
2387
|
+
targetRouteId: string;
|
|
2388
|
+
condition?: string;
|
|
2389
|
+
reason: "route_complete" | "manual";
|
|
2390
|
+
};
|
|
2391
|
+
|
|
1792
2392
|
/** Session metadata */
|
|
1793
2393
|
metadata?: {
|
|
1794
2394
|
createdAt?: Date;
|
|
@@ -1802,9 +2402,10 @@ interface SessionState<TData = Record<string, unknown>> {
|
|
|
1802
2402
|
|
|
1803
2403
|
- **`id`** - Optional session identifier that persists across database operations
|
|
1804
2404
|
- **`data`** - Type-safe data collected via `schema` for the **current route**
|
|
1805
|
-
- **`dataByRoute`** - **
|
|
2405
|
+
- **`dataByRoute`** - **Per-route data map that preserves collected data when switching routes**
|
|
1806
2406
|
- **`currentRoute`** / **`currentStep`** - Track conversation position
|
|
1807
2407
|
- **`routeHistory`** - Full audit trail of route transitions
|
|
2408
|
+
- **`pendingTransition`** - Handles route completion transitions
|
|
1808
2409
|
- **`metadata`** - Custom data (timestamps, user info, etc.)
|
|
1809
2410
|
|
|
1810
2411
|
**Per-Route Data Preservation:**
|
|
@@ -1902,14 +2503,14 @@ console.log(session.currentStep?.id); // "ask_destination"
|
|
|
1902
2503
|
console.log(session.currentStep?.description); // "Ask where to fly"
|
|
1903
2504
|
```
|
|
1904
2505
|
|
|
1905
|
-
#### `
|
|
2506
|
+
#### `mergeCollected<TData>(session, data): SessionState<TData>`
|
|
1906
2507
|
|
|
1907
2508
|
Merges new collected data into session. Updates timestamps automatically.
|
|
1908
2509
|
|
|
1909
2510
|
**Example:**
|
|
1910
2511
|
|
|
1911
2512
|
```typescript
|
|
1912
|
-
session =
|
|
2513
|
+
session = mergeCollected(session, {
|
|
1913
2514
|
destination: "Paris",
|
|
1914
2515
|
departureDate: "2025-06-15",
|
|
1915
2516
|
});
|
|
@@ -2088,7 +2689,7 @@ Symbol marking the end of a conversation route. Use this when building routes to
|
|
|
2088
2689
|
import { END_ROUTE } from "@falai/agent";
|
|
2089
2690
|
|
|
2090
2691
|
const thankYou = askEmail.nextStep({
|
|
2091
|
-
|
|
2692
|
+
prompt: "Thank you for your information!",
|
|
2092
2693
|
});
|
|
2093
2694
|
|
|
2094
2695
|
// Mark the end of the route
|
|
@@ -2144,7 +2745,7 @@ enum BuiltInSection {
|
|
|
2144
2745
|
GLOSSARY = "glossary",
|
|
2145
2746
|
CONTEXT = "context",
|
|
2146
2747
|
GUIDELINES = "guidelines",
|
|
2147
|
-
|
|
2748
|
+
TOOLS
|
|
2148
2749
|
ACTIVE_ROUTES = "active_routes",
|
|
2149
2750
|
}
|
|
2150
2751
|
```
|
|
@@ -2165,23 +2766,92 @@ interface AiProvider {
|
|
|
2165
2766
|
}
|
|
2166
2767
|
```
|
|
2167
2768
|
|
|
2168
|
-
|
|
2769
|
+
---
|
|
2770
|
+
|
|
2771
|
+
## Template Utilities
|
|
2772
|
+
|
|
2773
|
+
### Dynamic Content with Templates
|
|
2169
2774
|
|
|
2170
|
-
|
|
2775
|
+
The framework supports dynamic content generation through a versatile `Template` system. A `Template` can be either a simple string with `{{variable}}` placeholders or a function that returns a string, allowing for more complex, context-aware logic.
|
|
2776
|
+
|
|
2777
|
+
Templates are evaluated at runtime with a `TemplateContext` object that provides access to the agent's state:
|
|
2171
2778
|
|
|
2172
2779
|
```typescript
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2780
|
+
interface TemplateContext<TContext = unknown, TData = unknown> {
|
|
2781
|
+
context?: TContext;
|
|
2782
|
+
session?: SessionState<TData>;
|
|
2783
|
+
history?: Event[];
|
|
2784
|
+
data?: Partial<TData>; // Convenience alias for session.data
|
|
2785
|
+
}
|
|
2786
|
+
```
|
|
2787
|
+
|
|
2788
|
+
#### `Template<TContext, TData>` Type
|
|
2181
2789
|
|
|
2182
|
-
|
|
2790
|
+
This is the core type for all dynamic content:
|
|
2791
|
+
|
|
2792
|
+
```typescript
|
|
2793
|
+
type Template<TContext = unknown, TData = unknown> =
|
|
2794
|
+
| string
|
|
2795
|
+
| ((params: TemplateContext<TContext, TData>) => string | Promise<string>);
|
|
2796
|
+
```
|
|
2183
2797
|
|
|
2184
|
-
|
|
2798
|
+
**String Templates:**
|
|
2799
|
+
|
|
2800
|
+
Simple strings with `{{variable}}` placeholders are rendered using values from the `context` object.
|
|
2801
|
+
|
|
2802
|
+
```typescript
|
|
2803
|
+
const agent = new Agent({
|
|
2804
|
+
identity: "I am {{name}}",
|
|
2805
|
+
context: { name: "HelperBot" },
|
|
2806
|
+
});
|
|
2807
|
+
```
|
|
2808
|
+
|
|
2809
|
+
**Function Templates:**
|
|
2810
|
+
|
|
2811
|
+
For more complex logic, you can provide a function that receives the `TemplateContext` and returns a string (or a `Promise<string>` for async operations).
|
|
2812
|
+
|
|
2813
|
+
```typescript
|
|
2814
|
+
const agent = new Agent({
|
|
2815
|
+
identity: ({ context }) => `I am here to help, ${context.user.name}`,
|
|
2816
|
+
context: { user: { name: "Alice" } },
|
|
2817
|
+
});
|
|
2818
|
+
```
|
|
2819
|
+
|
|
2820
|
+
#### `render(template, params)`
|
|
2821
|
+
|
|
2822
|
+
An internal async utility that resolves a `Template` to a string. You won't typically call this directly, but it powers the dynamic content generation throughout the framework.
|
|
2823
|
+
|
|
2824
|
+
**Supported in (Dynamic/Personalized):**
|
|
2825
|
+
|
|
2826
|
+
- **Agent**: `identity`
|
|
2827
|
+
- **Terms**: `name`, `description`, `synonyms`
|
|
2828
|
+
- **Guidelines**: `condition`, `action`
|
|
2829
|
+
- **Routes**: `conditions`, `rules`, `prohibitions`
|
|
2830
|
+
- **Steps/Transitions**: `prompt`, `condition`
|
|
2831
|
+
|
|
2832
|
+
**Not supported in (Static/Predictable):**
|
|
2833
|
+
|
|
2834
|
+
- Agent `name`, `goal`, `description`
|
|
2835
|
+
- Route `title`, `description`
|
|
2836
|
+
- Step `description`
|
|
2837
|
+
|
|
2838
|
+
**Example:**
|
|
2839
|
+
|
|
2840
|
+
```typescript
|
|
2841
|
+
const agent = new Agent({
|
|
2842
|
+
name: "Assistant", // Static
|
|
2843
|
+
identity: ({ context }) =>
|
|
2844
|
+
`I am here to help you, ${context.user.firstName}.`, // Dynamic
|
|
2845
|
+
context: {
|
|
2846
|
+
user: { firstName: "Alice", age: 30 },
|
|
2847
|
+
},
|
|
2848
|
+
});
|
|
2849
|
+
|
|
2850
|
+
agent.createGuideline({
|
|
2851
|
+
condition: ({ context }) => context.user.age > 18, // Dynamic condition
|
|
2852
|
+
action:
|
|
2853
|
+
"Be helpful to {{user.firstName}} and consider their age in responses", // Dynamic action
|
|
2854
|
+
});
|
|
2185
2855
|
```
|
|
2186
2856
|
|
|
2187
2857
|
---
|