@falai/agent 3.4.5 → 4.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -34
- package/dist/cjs/core/Agent.d.ts +22 -378
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +104 -1178
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/CompactionEngine.d.ts.map +1 -1
- package/dist/cjs/core/CompactionEngine.js +5 -3
- package/dist/cjs/core/CompactionEngine.js.map +1 -1
- package/dist/cjs/core/FlowSpec.d.ts +136 -0
- package/dist/cjs/core/FlowSpec.d.ts.map +1 -0
- package/dist/cjs/core/FlowSpec.js +522 -0
- package/dist/cjs/core/FlowSpec.js.map +1 -0
- package/dist/cjs/core/Migrate.d.ts +38 -0
- package/dist/cjs/core/Migrate.d.ts.map +1 -0
- package/dist/cjs/core/Migrate.js +270 -0
- package/dist/cjs/core/Migrate.js.map +1 -0
- package/dist/cjs/core/Prompt.d.ts +54 -0
- package/dist/cjs/core/Prompt.d.ts.map +1 -0
- package/dist/cjs/core/Prompt.js +143 -0
- package/dist/cjs/core/Prompt.js.map +1 -0
- package/dist/cjs/core/Runner.d.ts +160 -0
- package/dist/cjs/core/Runner.d.ts.map +1 -0
- package/dist/cjs/core/Runner.js +1131 -0
- package/dist/cjs/core/Runner.js.map +1 -0
- package/dist/cjs/core/Speak.d.ts +37 -0
- package/dist/cjs/core/Speak.d.ts.map +1 -0
- package/dist/cjs/core/Speak.js +364 -0
- package/dist/cjs/core/Speak.js.map +1 -0
- package/dist/cjs/core/Understand.d.ts +28 -0
- package/dist/cjs/core/Understand.d.ts.map +1 -0
- package/dist/cjs/core/Understand.js +353 -0
- package/dist/cjs/core/Understand.js.map +1 -0
- package/dist/cjs/core/contracts.d.ts +122 -0
- package/dist/cjs/core/contracts.d.ts.map +1 -0
- package/dist/cjs/core/contracts.js +11 -0
- package/dist/cjs/core/contracts.js.map +1 -0
- package/dist/cjs/core/falai.d.ts +57 -0
- package/dist/cjs/core/falai.d.ts.map +1 -0
- package/dist/cjs/core/falai.js +43 -0
- package/dist/cjs/core/falai.js.map +1 -0
- package/dist/cjs/core/predicate.d.ts +9 -0
- package/dist/cjs/core/predicate.d.ts.map +1 -0
- package/dist/cjs/core/predicate.js +58 -0
- package/dist/cjs/core/predicate.js.map +1 -0
- package/dist/cjs/index.d.ts +26 -31
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +46 -68
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/persistence/MemoryStore.d.ts +15 -0
- package/dist/cjs/persistence/MemoryStore.d.ts.map +1 -0
- package/dist/cjs/persistence/MemoryStore.js +39 -0
- package/dist/cjs/persistence/MemoryStore.js.map +1 -0
- package/dist/cjs/persistence/MongoStore.d.ts +42 -0
- package/dist/cjs/persistence/MongoStore.d.ts.map +1 -0
- package/dist/cjs/persistence/MongoStore.js +60 -0
- package/dist/cjs/persistence/MongoStore.js.map +1 -0
- package/dist/cjs/persistence/OpenSearchStore.d.ts +86 -0
- package/dist/cjs/persistence/OpenSearchStore.d.ts.map +1 -0
- package/dist/cjs/persistence/OpenSearchStore.js +120 -0
- package/dist/cjs/persistence/OpenSearchStore.js.map +1 -0
- package/dist/cjs/persistence/PostgresStore.d.ts +41 -0
- package/dist/cjs/persistence/PostgresStore.d.ts.map +1 -0
- package/dist/cjs/persistence/PostgresStore.js +58 -0
- package/dist/cjs/persistence/PostgresStore.js.map +1 -0
- package/dist/cjs/persistence/PrismaStore.d.ts +65 -0
- package/dist/cjs/persistence/PrismaStore.d.ts.map +1 -0
- package/dist/cjs/persistence/PrismaStore.js +95 -0
- package/dist/cjs/persistence/PrismaStore.js.map +1 -0
- package/dist/cjs/persistence/RedisStore.d.ts +34 -0
- package/dist/cjs/persistence/RedisStore.d.ts.map +1 -0
- package/dist/cjs/persistence/RedisStore.js +61 -0
- package/dist/cjs/persistence/RedisStore.js.map +1 -0
- package/dist/cjs/persistence/SQLiteStore.d.ts +45 -0
- package/dist/cjs/persistence/SQLiteStore.d.ts.map +1 -0
- package/dist/cjs/persistence/SQLiteStore.js +74 -0
- package/dist/cjs/persistence/SQLiteStore.js.map +1 -0
- package/dist/cjs/persistence/sessionRow.d.ts +14 -0
- package/dist/cjs/persistence/sessionRow.d.ts.map +1 -0
- package/dist/cjs/persistence/sessionRow.js +50 -0
- package/dist/cjs/persistence/sessionRow.js.map +1 -0
- package/dist/cjs/providers/DeepSeekProvider.d.ts.map +1 -1
- package/dist/cjs/providers/DeepSeekProvider.js +8 -3
- package/dist/cjs/providers/DeepSeekProvider.js.map +1 -1
- package/dist/cjs/providers/GeminiProvider.d.ts +4 -3
- package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/cjs/providers/GeminiProvider.js +4 -3
- package/dist/cjs/providers/GeminiProvider.js.map +1 -1
- package/dist/cjs/providers/OpenAICompatibleProvider.d.ts +4 -0
- package/dist/cjs/providers/OpenAICompatibleProvider.d.ts.map +1 -1
- package/dist/cjs/providers/OpenAICompatibleProvider.js +2 -0
- package/dist/cjs/providers/OpenAICompatibleProvider.js.map +1 -1
- package/dist/cjs/providers/OpenRouterProvider.d.ts.map +1 -1
- package/dist/cjs/providers/OpenRouterProvider.js +2 -4
- package/dist/cjs/providers/OpenRouterProvider.js.map +1 -1
- package/dist/cjs/providers/ProviderAdapter.d.ts +1 -1
- package/dist/cjs/providers/ProviderAdapter.d.ts.map +1 -1
- package/dist/cjs/providers/ProviderAdapter.js +33 -10
- package/dist/cjs/providers/ProviderAdapter.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +153 -383
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/agent.js +1 -1
- package/dist/cjs/types/ai.d.ts +32 -1
- package/dist/cjs/types/ai.d.ts.map +1 -1
- package/dist/cjs/types/compaction.d.ts +3 -1
- package/dist/cjs/types/compaction.d.ts.map +1 -1
- package/dist/cjs/types/errors.d.ts +9 -12
- package/dist/cjs/types/errors.d.ts.map +1 -1
- package/dist/cjs/types/errors.js +14 -17
- package/dist/cjs/types/errors.js.map +1 -1
- package/dist/cjs/types/flow.d.ts +265 -513
- package/dist/cjs/types/flow.d.ts.map +1 -1
- package/dist/cjs/types/flow.js +7 -1
- package/dist/cjs/types/flow.js.map +1 -1
- package/dist/cjs/types/history.d.ts +7 -18
- package/dist/cjs/types/history.d.ts.map +1 -1
- package/dist/cjs/types/history.js.map +1 -1
- package/dist/cjs/types/index.d.ts +9 -15
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js +4 -14
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/session.d.ts +94 -64
- package/dist/cjs/types/session.d.ts.map +1 -1
- package/dist/cjs/types/session.js +5 -1
- package/dist/cjs/types/session.js.map +1 -1
- package/dist/cjs/types/tool.d.ts +37 -207
- package/dist/cjs/types/tool.d.ts.map +1 -1
- package/dist/cjs/types/tool.js +5 -14
- package/dist/cjs/types/tool.js.map +1 -1
- package/dist/cjs/utils/clock.d.ts +28 -0
- package/dist/cjs/utils/clock.d.ts.map +1 -0
- package/dist/cjs/utils/clock.js +64 -0
- package/dist/cjs/utils/clock.js.map +1 -0
- package/dist/cjs/utils/duration.d.ts +11 -0
- package/dist/cjs/utils/duration.d.ts.map +1 -0
- package/dist/cjs/utils/duration.js +31 -0
- package/dist/cjs/utils/duration.js.map +1 -0
- package/dist/cjs/utils/history.d.ts +4 -1
- package/dist/cjs/utils/history.d.ts.map +1 -1
- package/dist/cjs/utils/history.js +2 -2
- package/dist/cjs/utils/history.js.map +1 -1
- package/dist/cjs/utils/index.d.ts +4 -10
- package/dist/cjs/utils/index.d.ts.map +1 -1
- package/dist/cjs/utils/index.js +14 -61
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/json.d.ts +2 -0
- package/dist/cjs/utils/json.d.ts.map +1 -1
- package/dist/cjs/utils/json.js +5 -0
- package/dist/cjs/utils/json.js.map +1 -1
- package/dist/cjs/utils/outcomes.d.ts +48 -0
- package/dist/cjs/utils/outcomes.d.ts.map +1 -0
- package/dist/cjs/utils/outcomes.js +51 -0
- package/dist/cjs/utils/outcomes.js.map +1 -0
- package/dist/cjs/utils/schema.d.ts +50 -0
- package/dist/cjs/utils/schema.d.ts.map +1 -0
- package/dist/cjs/utils/schema.js +138 -0
- package/dist/cjs/utils/schema.js.map +1 -0
- package/dist/cjs/utils/streamingMessage.d.ts +3 -2
- package/dist/cjs/utils/streamingMessage.d.ts.map +1 -1
- package/dist/cjs/utils/streamingMessage.js +38 -4
- package/dist/cjs/utils/streamingMessage.js.map +1 -1
- package/dist/cjs/utils/template.d.ts +13 -149
- package/dist/cjs/utils/template.d.ts.map +1 -1
- package/dist/cjs/utils/template.js +31 -363
- package/dist/cjs/utils/template.js.map +1 -1
- package/dist/cjs/utils/usage.d.ts +19 -0
- package/dist/cjs/utils/usage.d.ts.map +1 -0
- package/dist/cjs/utils/usage.js +35 -0
- package/dist/cjs/utils/usage.js.map +1 -0
- package/dist/core/Agent.d.ts +22 -378
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +107 -1181
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/CompactionEngine.d.ts.map +1 -1
- package/dist/core/CompactionEngine.js +5 -3
- package/dist/core/CompactionEngine.js.map +1 -1
- package/dist/core/FlowSpec.d.ts +136 -0
- package/dist/core/FlowSpec.d.ts.map +1 -0
- package/dist/core/FlowSpec.js +516 -0
- package/dist/core/FlowSpec.js.map +1 -0
- package/dist/core/Migrate.d.ts +38 -0
- package/dist/core/Migrate.d.ts.map +1 -0
- package/dist/core/Migrate.js +264 -0
- package/dist/core/Migrate.js.map +1 -0
- package/dist/core/Prompt.d.ts +54 -0
- package/dist/core/Prompt.d.ts.map +1 -0
- package/dist/core/Prompt.js +133 -0
- package/dist/core/Prompt.js.map +1 -0
- package/dist/core/Runner.d.ts +160 -0
- package/dist/core/Runner.d.ts.map +1 -0
- package/dist/core/Runner.js +1127 -0
- package/dist/core/Runner.js.map +1 -0
- package/dist/core/Speak.d.ts +37 -0
- package/dist/core/Speak.d.ts.map +1 -0
- package/dist/core/Speak.js +360 -0
- package/dist/core/Speak.js.map +1 -0
- package/dist/core/Understand.d.ts +28 -0
- package/dist/core/Understand.d.ts.map +1 -0
- package/dist/core/Understand.js +349 -0
- package/dist/core/Understand.js.map +1 -0
- package/dist/core/contracts.d.ts +122 -0
- package/dist/core/contracts.d.ts.map +1 -0
- package/dist/core/contracts.js +10 -0
- package/dist/core/contracts.js.map +1 -0
- package/dist/core/falai.d.ts +57 -0
- package/dist/core/falai.d.ts.map +1 -0
- package/dist/core/falai.js +40 -0
- package/dist/core/falai.js.map +1 -0
- package/dist/core/predicate.d.ts +9 -0
- package/dist/core/predicate.d.ts.map +1 -0
- package/dist/core/predicate.js +54 -0
- package/dist/core/predicate.js.map +1 -0
- package/dist/index.d.ts +26 -31
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -24
- package/dist/index.js.map +1 -1
- package/dist/persistence/MemoryStore.d.ts +15 -0
- package/dist/persistence/MemoryStore.d.ts.map +1 -0
- package/dist/persistence/MemoryStore.js +35 -0
- package/dist/persistence/MemoryStore.js.map +1 -0
- package/dist/persistence/MongoStore.d.ts +42 -0
- package/dist/persistence/MongoStore.d.ts.map +1 -0
- package/dist/persistence/MongoStore.js +56 -0
- package/dist/persistence/MongoStore.js.map +1 -0
- package/dist/persistence/OpenSearchStore.d.ts +86 -0
- package/dist/persistence/OpenSearchStore.d.ts.map +1 -0
- package/dist/persistence/OpenSearchStore.js +116 -0
- package/dist/persistence/OpenSearchStore.js.map +1 -0
- package/dist/persistence/PostgresStore.d.ts +41 -0
- package/dist/persistence/PostgresStore.d.ts.map +1 -0
- package/dist/persistence/PostgresStore.js +54 -0
- package/dist/persistence/PostgresStore.js.map +1 -0
- package/dist/persistence/PrismaStore.d.ts +65 -0
- package/dist/persistence/PrismaStore.d.ts.map +1 -0
- package/dist/persistence/PrismaStore.js +91 -0
- package/dist/persistence/PrismaStore.js.map +1 -0
- package/dist/persistence/RedisStore.d.ts +34 -0
- package/dist/persistence/RedisStore.d.ts.map +1 -0
- package/dist/persistence/RedisStore.js +57 -0
- package/dist/persistence/RedisStore.js.map +1 -0
- package/dist/persistence/SQLiteStore.d.ts +45 -0
- package/dist/persistence/SQLiteStore.d.ts.map +1 -0
- package/dist/persistence/SQLiteStore.js +70 -0
- package/dist/persistence/SQLiteStore.js.map +1 -0
- package/dist/persistence/sessionRow.d.ts +14 -0
- package/dist/persistence/sessionRow.d.ts.map +1 -0
- package/dist/persistence/sessionRow.js +45 -0
- package/dist/persistence/sessionRow.js.map +1 -0
- package/dist/providers/DeepSeekProvider.d.ts.map +1 -1
- package/dist/providers/DeepSeekProvider.js +8 -3
- package/dist/providers/DeepSeekProvider.js.map +1 -1
- package/dist/providers/GeminiProvider.d.ts +4 -3
- package/dist/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/providers/GeminiProvider.js +4 -3
- package/dist/providers/GeminiProvider.js.map +1 -1
- package/dist/providers/OpenAICompatibleProvider.d.ts +4 -0
- package/dist/providers/OpenAICompatibleProvider.d.ts.map +1 -1
- package/dist/providers/OpenAICompatibleProvider.js +2 -0
- package/dist/providers/OpenAICompatibleProvider.js.map +1 -1
- package/dist/providers/OpenRouterProvider.d.ts.map +1 -1
- package/dist/providers/OpenRouterProvider.js +2 -4
- package/dist/providers/OpenRouterProvider.js.map +1 -1
- package/dist/providers/ProviderAdapter.d.ts +1 -1
- package/dist/providers/ProviderAdapter.d.ts.map +1 -1
- package/dist/providers/ProviderAdapter.js +34 -11
- package/dist/providers/ProviderAdapter.js.map +1 -1
- package/dist/types/agent.d.ts +153 -383
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/agent.js +1 -1
- package/dist/types/ai.d.ts +32 -1
- package/dist/types/ai.d.ts.map +1 -1
- package/dist/types/compaction.d.ts +3 -1
- package/dist/types/compaction.d.ts.map +1 -1
- package/dist/types/errors.d.ts +9 -12
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +12 -15
- package/dist/types/errors.js.map +1 -1
- package/dist/types/flow.d.ts +265 -513
- package/dist/types/flow.d.ts.map +1 -1
- package/dist/types/flow.js +7 -1
- package/dist/types/flow.js.map +1 -1
- package/dist/types/history.d.ts +7 -18
- package/dist/types/history.d.ts.map +1 -1
- package/dist/types/history.js.map +1 -1
- package/dist/types/index.d.ts +9 -15
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +2 -7
- package/dist/types/index.js.map +1 -1
- package/dist/types/session.d.ts +94 -64
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/session.js +5 -1
- package/dist/types/session.js.map +1 -1
- package/dist/types/tool.d.ts +37 -207
- package/dist/types/tool.d.ts.map +1 -1
- package/dist/types/tool.js +6 -13
- package/dist/types/tool.js.map +1 -1
- package/dist/utils/clock.d.ts +28 -0
- package/dist/utils/clock.d.ts.map +1 -0
- package/dist/utils/clock.js +59 -0
- package/dist/utils/clock.js.map +1 -0
- package/dist/utils/duration.d.ts +11 -0
- package/dist/utils/duration.d.ts.map +1 -0
- package/dist/utils/duration.js +26 -0
- package/dist/utils/duration.js.map +1 -0
- package/dist/utils/history.d.ts +4 -1
- package/dist/utils/history.d.ts.map +1 -1
- package/dist/utils/history.js +2 -2
- package/dist/utils/history.js.map +1 -1
- package/dist/utils/index.d.ts +4 -10
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +4 -21
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/json.d.ts +2 -0
- package/dist/utils/json.d.ts.map +1 -1
- package/dist/utils/json.js +4 -0
- package/dist/utils/json.js.map +1 -1
- package/dist/utils/outcomes.d.ts +48 -0
- package/dist/utils/outcomes.d.ts.map +1 -0
- package/dist/utils/outcomes.js +48 -0
- package/dist/utils/outcomes.js.map +1 -0
- package/dist/utils/schema.d.ts +50 -0
- package/dist/utils/schema.d.ts.map +1 -0
- package/dist/utils/schema.js +129 -0
- package/dist/utils/schema.js.map +1 -0
- package/dist/utils/streamingMessage.d.ts +3 -2
- package/dist/utils/streamingMessage.d.ts.map +1 -1
- package/dist/utils/streamingMessage.js +38 -4
- package/dist/utils/streamingMessage.js.map +1 -1
- package/dist/utils/template.d.ts +13 -149
- package/dist/utils/template.d.ts.map +1 -1
- package/dist/utils/template.js +28 -355
- package/dist/utils/template.js.map +1 -1
- package/dist/utils/usage.d.ts +19 -0
- package/dist/utils/usage.d.ts.map +1 -0
- package/dist/utils/usage.js +31 -0
- package/dist/utils/usage.js.map +1 -0
- package/docs/README.md +37 -19
- package/docs/concepts/architecture.md +117 -239
- package/docs/concepts/collection.md +170 -0
- package/docs/concepts/pipeline.md +132 -378
- package/docs/concepts/runs-and-waits.md +192 -0
- package/docs/guides/actions-and-events.md +276 -0
- package/docs/guides/branching.md +119 -208
- package/docs/guides/compaction.md +63 -158
- package/docs/guides/conditions.md +164 -128
- package/docs/guides/error-handling.md +168 -164
- package/docs/guides/flow-control.md +210 -349
- package/docs/guides/flows-from-json.md +224 -0
- package/docs/guides/instructions.md +125 -161
- package/docs/guides/persistence.md +182 -206
- package/docs/guides/streaming.md +50 -114
- package/docs/guides/testing.md +284 -0
- package/docs/guides/triggers.md +401 -0
- package/docs/migration/README.md +8 -15
- package/docs/migration/v1-to-v2.md +1 -1
- package/docs/migration/v2-3-to-v2-4.md +2 -2
- package/docs/migration/v2-6-to-v2-7.md +4 -4
- package/docs/migration/v3-to-v4.md +452 -0
- package/docs/reference/actions-events-conditions.md +396 -0
- package/docs/reference/agent.md +244 -0
- package/docs/reference/branches.md +75 -203
- package/docs/reference/errors.md +188 -144
- package/docs/reference/fields.md +125 -0
- package/docs/reference/flow-spec.md +248 -0
- package/docs/reference/flow.md +104 -192
- package/docs/reference/instruction.md +83 -137
- package/docs/reference/outcomes.md +273 -0
- package/docs/reference/providers.md +525 -302
- package/docs/reference/session.md +210 -0
- package/docs/reference/step.md +194 -312
- package/docs/reference/stores.md +496 -0
- package/docs/reference/tool.md +162 -231
- package/docs/reference/trigger.md +180 -0
- package/docs/rfc/v4-one-flow.md +477 -0
- package/docs/start/01-install.md +59 -44
- package/docs/start/02-first-agent.md +97 -147
- package/docs/start/03-collect-data.md +78 -183
- package/docs/start/04-add-tools.md +159 -227
- package/docs/start/05-go-to-production.md +167 -164
- package/examples/01-quickstart.ts +26 -16
- package/examples/02-fields.ts +75 -0
- package/examples/03-tools.ts +79 -119
- package/examples/04-instructions.ts +60 -87
- package/examples/05-branches.ts +78 -0
- package/examples/06-triggers-and-waits.ts +148 -0
- package/examples/07-streaming.ts +34 -60
- package/examples/08-store-and-migration.ts +97 -0
- package/examples/09-flows-from-json.ts +107 -0
- package/package.json +9 -6
- package/src/core/Agent.ts +116 -1512
- package/src/core/CompactionEngine.ts +7 -4
- package/src/core/FlowSpec.ts +712 -0
- package/src/core/Migrate.ts +256 -0
- package/src/core/Prompt.ts +156 -0
- package/src/core/Runner.ts +1181 -0
- package/src/core/Speak.ts +451 -0
- package/src/core/Understand.ts +422 -0
- package/src/core/contracts.ts +111 -0
- package/src/core/falai.ts +86 -0
- package/src/core/predicate.ts +56 -0
- package/src/index.ts +119 -147
- package/src/persistence/MemoryStore.ts +37 -0
- package/src/persistence/MongoStore.ts +89 -0
- package/src/persistence/OpenSearchStore.ts +153 -0
- package/src/persistence/PostgresStore.ts +89 -0
- package/src/persistence/PrismaStore.ts +127 -0
- package/src/persistence/RedisStore.ts +90 -0
- package/src/persistence/SQLiteStore.ts +103 -0
- package/src/persistence/sessionRow.ts +45 -0
- package/src/providers/DeepSeekProvider.ts +8 -3
- package/src/providers/GeminiProvider.ts +4 -3
- package/src/providers/OpenAICompatibleProvider.ts +6 -0
- package/src/providers/OpenRouterProvider.ts +2 -4
- package/src/providers/ProviderAdapter.ts +36 -8
- package/src/types/agent.ts +124 -397
- package/src/types/ai.ts +33 -1
- package/src/types/compaction.ts +3 -1
- package/src/types/errors.ts +13 -16
- package/src/types/flow.ts +249 -550
- package/src/types/history.ts +7 -20
- package/src/types/index.ts +87 -139
- package/src/types/session.ts +135 -70
- package/src/types/tool.ts +42 -267
- package/src/utils/clock.ts +70 -0
- package/src/utils/duration.ts +33 -0
- package/src/utils/history.ts +3 -2
- package/src/utils/index.ts +8 -66
- package/src/utils/json.ts +5 -0
- package/src/utils/outcomes.ts +56 -0
- package/src/utils/schema.ts +145 -0
- package/src/utils/streamingMessage.ts +34 -4
- package/src/utils/template.ts +32 -423
- package/src/utils/usage.ts +37 -0
- package/dist/adapters/MemoryAdapter.d.ts +0 -47
- package/dist/adapters/MemoryAdapter.d.ts.map +0 -1
- package/dist/adapters/MemoryAdapter.js +0 -204
- package/dist/adapters/MemoryAdapter.js.map +0 -1
- package/dist/adapters/MongoAdapter.d.ts +0 -97
- package/dist/adapters/MongoAdapter.d.ts.map +0 -1
- package/dist/adapters/MongoAdapter.js +0 -196
- package/dist/adapters/MongoAdapter.js.map +0 -1
- package/dist/adapters/OpenSearchAdapter.d.ts +0 -169
- package/dist/adapters/OpenSearchAdapter.d.ts.map +0 -1
- package/dist/adapters/OpenSearchAdapter.js +0 -471
- package/dist/adapters/OpenSearchAdapter.js.map +0 -1
- package/dist/adapters/PostgreSQLAdapter.d.ts +0 -85
- package/dist/adapters/PostgreSQLAdapter.d.ts.map +0 -1
- package/dist/adapters/PostgreSQLAdapter.js +0 -308
- package/dist/adapters/PostgreSQLAdapter.js.map +0 -1
- package/dist/adapters/PrismaAdapter.d.ts +0 -115
- package/dist/adapters/PrismaAdapter.d.ts.map +0 -1
- package/dist/adapters/PrismaAdapter.js +0 -406
- package/dist/adapters/PrismaAdapter.js.map +0 -1
- package/dist/adapters/RedisAdapter.d.ts +0 -72
- package/dist/adapters/RedisAdapter.d.ts.map +0 -1
- package/dist/adapters/RedisAdapter.js +0 -286
- package/dist/adapters/RedisAdapter.js.map +0 -1
- package/dist/adapters/SQLiteAdapter.d.ts +0 -86
- package/dist/adapters/SQLiteAdapter.d.ts.map +0 -1
- package/dist/adapters/SQLiteAdapter.js +0 -337
- package/dist/adapters/SQLiteAdapter.js.map +0 -1
- package/dist/adapters/index.d.ts +0 -17
- package/dist/adapters/index.d.ts.map +0 -1
- package/dist/adapters/index.js +0 -11
- package/dist/adapters/index.js.map +0 -1
- package/dist/adapters/sessionRow.d.ts +0 -22
- package/dist/adapters/sessionRow.d.ts.map +0 -1
- package/dist/adapters/sessionRow.js +0 -48
- package/dist/adapters/sessionRow.js.map +0 -1
- package/dist/cjs/adapters/MemoryAdapter.d.ts +0 -47
- package/dist/cjs/adapters/MemoryAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/MemoryAdapter.js +0 -208
- package/dist/cjs/adapters/MemoryAdapter.js.map +0 -1
- package/dist/cjs/adapters/MongoAdapter.d.ts +0 -97
- package/dist/cjs/adapters/MongoAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/MongoAdapter.js +0 -200
- package/dist/cjs/adapters/MongoAdapter.js.map +0 -1
- package/dist/cjs/adapters/OpenSearchAdapter.d.ts +0 -169
- package/dist/cjs/adapters/OpenSearchAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/OpenSearchAdapter.js +0 -475
- package/dist/cjs/adapters/OpenSearchAdapter.js.map +0 -1
- package/dist/cjs/adapters/PostgreSQLAdapter.d.ts +0 -85
- package/dist/cjs/adapters/PostgreSQLAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/PostgreSQLAdapter.js +0 -312
- package/dist/cjs/adapters/PostgreSQLAdapter.js.map +0 -1
- package/dist/cjs/adapters/PrismaAdapter.d.ts +0 -115
- package/dist/cjs/adapters/PrismaAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/PrismaAdapter.js +0 -410
- package/dist/cjs/adapters/PrismaAdapter.js.map +0 -1
- package/dist/cjs/adapters/RedisAdapter.d.ts +0 -72
- package/dist/cjs/adapters/RedisAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/RedisAdapter.js +0 -290
- package/dist/cjs/adapters/RedisAdapter.js.map +0 -1
- package/dist/cjs/adapters/SQLiteAdapter.d.ts +0 -86
- package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +0 -1
- package/dist/cjs/adapters/SQLiteAdapter.js +0 -341
- package/dist/cjs/adapters/SQLiteAdapter.js.map +0 -1
- package/dist/cjs/adapters/index.d.ts +0 -17
- package/dist/cjs/adapters/index.d.ts.map +0 -1
- package/dist/cjs/adapters/index.js +0 -21
- package/dist/cjs/adapters/index.js.map +0 -1
- package/dist/cjs/adapters/sessionRow.d.ts +0 -22
- package/dist/cjs/adapters/sessionRow.d.ts.map +0 -1
- package/dist/cjs/adapters/sessionRow.js +0 -52
- package/dist/cjs/adapters/sessionRow.js.map +0 -1
- package/dist/cjs/constants/index.d.ts +0 -1
- package/dist/cjs/constants/index.d.ts.map +0 -1
- package/dist/cjs/constants/index.js +0 -4
- package/dist/cjs/constants/index.js.map +0 -1
- package/dist/cjs/core/AutoChainExecutor.d.ts +0 -97
- package/dist/cjs/core/AutoChainExecutor.d.ts.map +0 -1
- package/dist/cjs/core/AutoChainExecutor.js +0 -288
- package/dist/cjs/core/AutoChainExecutor.js.map +0 -1
- package/dist/cjs/core/BranchEvaluator.d.ts +0 -55
- package/dist/cjs/core/BranchEvaluator.d.ts.map +0 -1
- package/dist/cjs/core/BranchEvaluator.js +0 -125
- package/dist/cjs/core/BranchEvaluator.js.map +0 -1
- package/dist/cjs/core/DirectiveChainTracker.d.ts +0 -49
- package/dist/cjs/core/DirectiveChainTracker.d.ts.map +0 -1
- package/dist/cjs/core/DirectiveChainTracker.js +0 -121
- package/dist/cjs/core/DirectiveChainTracker.js.map +0 -1
- package/dist/cjs/core/Events.d.ts +0 -26
- package/dist/cjs/core/Events.d.ts.map +0 -1
- package/dist/cjs/core/Events.js +0 -144
- package/dist/cjs/core/Events.js.map +0 -1
- package/dist/cjs/core/Flow.d.ts +0 -183
- package/dist/cjs/core/Flow.d.ts.map +0 -1
- package/dist/cjs/core/Flow.js +0 -551
- package/dist/cjs/core/Flow.js.map +0 -1
- package/dist/cjs/core/FlowRouter.d.ts +0 -183
- package/dist/cjs/core/FlowRouter.d.ts.map +0 -1
- package/dist/cjs/core/FlowRouter.js +0 -1047
- package/dist/cjs/core/FlowRouter.js.map +0 -1
- package/dist/cjs/core/PersistenceManager.d.ts +0 -114
- package/dist/cjs/core/PersistenceManager.d.ts.map +0 -1
- package/dist/cjs/core/PersistenceManager.js +0 -336
- package/dist/cjs/core/PersistenceManager.js.map +0 -1
- package/dist/cjs/core/PromptComposer.d.ts +0 -47
- package/dist/cjs/core/PromptComposer.d.ts.map +0 -1
- package/dist/cjs/core/PromptComposer.js +0 -397
- package/dist/cjs/core/PromptComposer.js.map +0 -1
- package/dist/cjs/core/PromptSectionCache.d.ts +0 -48
- package/dist/cjs/core/PromptSectionCache.d.ts.map +0 -1
- package/dist/cjs/core/PromptSectionCache.js +0 -108
- package/dist/cjs/core/PromptSectionCache.js.map +0 -1
- package/dist/cjs/core/ResponseEngine.d.ts +0 -43
- package/dist/cjs/core/ResponseEngine.d.ts.map +0 -1
- package/dist/cjs/core/ResponseEngine.js +0 -235
- package/dist/cjs/core/ResponseEngine.js.map +0 -1
- package/dist/cjs/core/ResponseGenerationError.d.ts +0 -30
- package/dist/cjs/core/ResponseGenerationError.d.ts.map +0 -1
- package/dist/cjs/core/ResponseGenerationError.js +0 -35
- package/dist/cjs/core/ResponseGenerationError.js.map +0 -1
- package/dist/cjs/core/ResponseModal.d.ts +0 -305
- package/dist/cjs/core/ResponseModal.d.ts.map +0 -1
- package/dist/cjs/core/ResponseModal.js +0 -1414
- package/dist/cjs/core/ResponseModal.js.map +0 -1
- package/dist/cjs/core/ResponsePipeline.d.ts +0 -220
- package/dist/cjs/core/ResponsePipeline.d.ts.map +0 -1
- package/dist/cjs/core/ResponsePipeline.js +0 -1040
- package/dist/cjs/core/ResponsePipeline.js.map +0 -1
- package/dist/cjs/core/SessionFinalizer.d.ts +0 -34
- package/dist/cjs/core/SessionFinalizer.d.ts.map +0 -1
- package/dist/cjs/core/SessionFinalizer.js +0 -88
- package/dist/cjs/core/SessionFinalizer.js.map +0 -1
- package/dist/cjs/core/SessionManager.d.ts +0 -112
- package/dist/cjs/core/SessionManager.d.ts.map +0 -1
- package/dist/cjs/core/SessionManager.js +0 -308
- package/dist/cjs/core/SessionManager.js.map +0 -1
- package/dist/cjs/core/SignalCoordinator.d.ts +0 -103
- package/dist/cjs/core/SignalCoordinator.d.ts.map +0 -1
- package/dist/cjs/core/SignalCoordinator.js +0 -207
- package/dist/cjs/core/SignalCoordinator.js.map +0 -1
- package/dist/cjs/core/SignalEvaluator.d.ts +0 -86
- package/dist/cjs/core/SignalEvaluator.d.ts.map +0 -1
- package/dist/cjs/core/SignalEvaluator.js +0 -319
- package/dist/cjs/core/SignalEvaluator.js.map +0 -1
- package/dist/cjs/core/SignalProcessor.d.ts +0 -152
- package/dist/cjs/core/SignalProcessor.d.ts.map +0 -1
- package/dist/cjs/core/SignalProcessor.js +0 -505
- package/dist/cjs/core/SignalProcessor.js.map +0 -1
- package/dist/cjs/core/Step.d.ts +0 -184
- package/dist/cjs/core/Step.d.ts.map +0 -1
- package/dist/cjs/core/Step.js +0 -599
- package/dist/cjs/core/Step.js.map +0 -1
- package/dist/cjs/core/StepLifecycle.d.ts +0 -43
- package/dist/cjs/core/StepLifecycle.d.ts.map +0 -1
- package/dist/cjs/core/StepLifecycle.js +0 -180
- package/dist/cjs/core/StepLifecycle.js.map +0 -1
- package/dist/cjs/core/StreamingToolExecutor.d.ts +0 -142
- package/dist/cjs/core/StreamingToolExecutor.d.ts.map +0 -1
- package/dist/cjs/core/StreamingToolExecutor.js +0 -490
- package/dist/cjs/core/StreamingToolExecutor.js.map +0 -1
- package/dist/cjs/core/ToolLoopExecutor.d.ts +0 -133
- package/dist/cjs/core/ToolLoopExecutor.d.ts.map +0 -1
- package/dist/cjs/core/ToolLoopExecutor.js +0 -568
- package/dist/cjs/core/ToolLoopExecutor.js.map +0 -1
- package/dist/cjs/core/ToolManager.d.ts +0 -250
- package/dist/cjs/core/ToolManager.d.ts.map +0 -1
- package/dist/cjs/core/ToolManager.js +0 -1104
- package/dist/cjs/core/ToolManager.js.map +0 -1
- package/dist/cjs/core/createAgent.d.ts +0 -35
- package/dist/cjs/core/createAgent.d.ts.map +0 -1
- package/dist/cjs/core/createAgent.js +0 -39
- package/dist/cjs/core/createAgent.js.map +0 -1
- package/dist/cjs/core/flow-namespace.d.ts +0 -64
- package/dist/cjs/core/flow-namespace.d.ts.map +0 -1
- package/dist/cjs/core/flow-namespace.js +0 -182
- package/dist/cjs/core/flow-namespace.js.map +0 -1
- package/dist/cjs/core/toolGates.d.ts +0 -24
- package/dist/cjs/core/toolGates.d.ts.map +0 -1
- package/dist/cjs/core/toolGates.js +0 -52
- package/dist/cjs/core/toolGates.js.map +0 -1
- package/dist/cjs/types/persistence.d.ts +0 -254
- package/dist/cjs/types/persistence.d.ts.map +0 -1
- package/dist/cjs/types/persistence.js +0 -7
- package/dist/cjs/types/persistence.js.map +0 -1
- package/dist/cjs/types/prompt-cache.d.ts +0 -15
- package/dist/cjs/types/prompt-cache.d.ts.map +0 -1
- package/dist/cjs/types/prompt-cache.js +0 -6
- package/dist/cjs/types/prompt-cache.js.map +0 -1
- package/dist/cjs/types/signals.d.ts +0 -263
- package/dist/cjs/types/signals.d.ts.map +0 -1
- package/dist/cjs/types/signals.js +0 -11
- package/dist/cjs/types/signals.js.map +0 -1
- package/dist/cjs/types/template.d.ts +0 -84
- package/dist/cjs/types/template.d.ts.map +0 -1
- package/dist/cjs/types/template.js +0 -3
- package/dist/cjs/types/template.js.map +0 -1
- package/dist/cjs/utils/condition.d.ts +0 -63
- package/dist/cjs/utils/condition.d.ts.map +0 -1
- package/dist/cjs/utils/condition.js +0 -239
- package/dist/cjs/utils/condition.js.map +0 -1
- package/dist/cjs/utils/event.d.ts +0 -6
- package/dist/cjs/utils/event.d.ts.map +0 -1
- package/dist/cjs/utils/event.js +0 -20
- package/dist/cjs/utils/event.js.map +0 -1
- package/dist/cjs/utils/id.d.ts +0 -33
- package/dist/cjs/utils/id.d.ts.map +0 -1
- package/dist/cjs/utils/id.js +0 -84
- package/dist/cjs/utils/id.js.map +0 -1
- package/dist/cjs/utils/serialize.d.ts +0 -36
- package/dist/cjs/utils/serialize.d.ts.map +0 -1
- package/dist/cjs/utils/serialize.js +0 -77
- package/dist/cjs/utils/serialize.js.map +0 -1
- package/dist/cjs/utils/session.d.ts +0 -124
- package/dist/cjs/utils/session.d.ts.map +0 -1
- package/dist/cjs/utils/session.js +0 -396
- package/dist/cjs/utils/session.js.map +0 -1
- package/dist/constants/index.d.ts +0 -2
- package/dist/constants/index.d.ts.map +0 -1
- package/dist/constants/index.js +0 -4
- package/dist/constants/index.js.map +0 -1
- package/dist/core/AutoChainExecutor.d.ts +0 -97
- package/dist/core/AutoChainExecutor.d.ts.map +0 -1
- package/dist/core/AutoChainExecutor.js +0 -284
- package/dist/core/AutoChainExecutor.js.map +0 -1
- package/dist/core/BranchEvaluator.d.ts +0 -55
- package/dist/core/BranchEvaluator.d.ts.map +0 -1
- package/dist/core/BranchEvaluator.js +0 -121
- package/dist/core/BranchEvaluator.js.map +0 -1
- package/dist/core/DirectiveChainTracker.d.ts +0 -49
- package/dist/core/DirectiveChainTracker.d.ts.map +0 -1
- package/dist/core/DirectiveChainTracker.js +0 -117
- package/dist/core/DirectiveChainTracker.js.map +0 -1
- package/dist/core/Events.d.ts +0 -26
- package/dist/core/Events.d.ts.map +0 -1
- package/dist/core/Events.js +0 -137
- package/dist/core/Events.js.map +0 -1
- package/dist/core/Flow.d.ts +0 -183
- package/dist/core/Flow.d.ts.map +0 -1
- package/dist/core/Flow.js +0 -547
- package/dist/core/Flow.js.map +0 -1
- package/dist/core/FlowRouter.d.ts +0 -183
- package/dist/core/FlowRouter.d.ts.map +0 -1
- package/dist/core/FlowRouter.js +0 -1043
- package/dist/core/FlowRouter.js.map +0 -1
- package/dist/core/PersistenceManager.d.ts +0 -114
- package/dist/core/PersistenceManager.d.ts.map +0 -1
- package/dist/core/PersistenceManager.js +0 -332
- package/dist/core/PersistenceManager.js.map +0 -1
- package/dist/core/PromptComposer.d.ts +0 -47
- package/dist/core/PromptComposer.d.ts.map +0 -1
- package/dist/core/PromptComposer.js +0 -393
- package/dist/core/PromptComposer.js.map +0 -1
- package/dist/core/PromptSectionCache.d.ts +0 -48
- package/dist/core/PromptSectionCache.d.ts.map +0 -1
- package/dist/core/PromptSectionCache.js +0 -104
- package/dist/core/PromptSectionCache.js.map +0 -1
- package/dist/core/ResponseEngine.d.ts +0 -43
- package/dist/core/ResponseEngine.d.ts.map +0 -1
- package/dist/core/ResponseEngine.js +0 -231
- package/dist/core/ResponseEngine.js.map +0 -1
- package/dist/core/ResponseGenerationError.d.ts +0 -30
- package/dist/core/ResponseGenerationError.d.ts.map +0 -1
- package/dist/core/ResponseGenerationError.js +0 -31
- package/dist/core/ResponseGenerationError.js.map +0 -1
- package/dist/core/ResponseModal.d.ts +0 -305
- package/dist/core/ResponseModal.d.ts.map +0 -1
- package/dist/core/ResponseModal.js +0 -1410
- package/dist/core/ResponseModal.js.map +0 -1
- package/dist/core/ResponsePipeline.d.ts +0 -220
- package/dist/core/ResponsePipeline.d.ts.map +0 -1
- package/dist/core/ResponsePipeline.js +0 -1035
- package/dist/core/ResponsePipeline.js.map +0 -1
- package/dist/core/SessionFinalizer.d.ts +0 -34
- package/dist/core/SessionFinalizer.d.ts.map +0 -1
- package/dist/core/SessionFinalizer.js +0 -84
- package/dist/core/SessionFinalizer.js.map +0 -1
- package/dist/core/SessionManager.d.ts +0 -112
- package/dist/core/SessionManager.d.ts.map +0 -1
- package/dist/core/SessionManager.js +0 -301
- package/dist/core/SessionManager.js.map +0 -1
- package/dist/core/SignalCoordinator.d.ts +0 -103
- package/dist/core/SignalCoordinator.d.ts.map +0 -1
- package/dist/core/SignalCoordinator.js +0 -203
- package/dist/core/SignalCoordinator.js.map +0 -1
- package/dist/core/SignalEvaluator.d.ts +0 -86
- package/dist/core/SignalEvaluator.d.ts.map +0 -1
- package/dist/core/SignalEvaluator.js +0 -312
- package/dist/core/SignalEvaluator.js.map +0 -1
- package/dist/core/SignalProcessor.d.ts +0 -152
- package/dist/core/SignalProcessor.d.ts.map +0 -1
- package/dist/core/SignalProcessor.js +0 -498
- package/dist/core/SignalProcessor.js.map +0 -1
- package/dist/core/Step.d.ts +0 -184
- package/dist/core/Step.d.ts.map +0 -1
- package/dist/core/Step.js +0 -594
- package/dist/core/Step.js.map +0 -1
- package/dist/core/StepLifecycle.d.ts +0 -43
- package/dist/core/StepLifecycle.d.ts.map +0 -1
- package/dist/core/StepLifecycle.js +0 -176
- package/dist/core/StepLifecycle.js.map +0 -1
- package/dist/core/StreamingToolExecutor.d.ts +0 -142
- package/dist/core/StreamingToolExecutor.d.ts.map +0 -1
- package/dist/core/StreamingToolExecutor.js +0 -483
- package/dist/core/StreamingToolExecutor.js.map +0 -1
- package/dist/core/ToolLoopExecutor.d.ts +0 -133
- package/dist/core/ToolLoopExecutor.d.ts.map +0 -1
- package/dist/core/ToolLoopExecutor.js +0 -564
- package/dist/core/ToolLoopExecutor.js.map +0 -1
- package/dist/core/ToolManager.d.ts +0 -250
- package/dist/core/ToolManager.d.ts.map +0 -1
- package/dist/core/ToolManager.js +0 -1098
- package/dist/core/ToolManager.js.map +0 -1
- package/dist/core/createAgent.d.ts +0 -35
- package/dist/core/createAgent.d.ts.map +0 -1
- package/dist/core/createAgent.js +0 -36
- package/dist/core/createAgent.js.map +0 -1
- package/dist/core/flow-namespace.d.ts +0 -64
- package/dist/core/flow-namespace.d.ts.map +0 -1
- package/dist/core/flow-namespace.js +0 -179
- package/dist/core/flow-namespace.js.map +0 -1
- package/dist/core/toolGates.d.ts +0 -24
- package/dist/core/toolGates.d.ts.map +0 -1
- package/dist/core/toolGates.js +0 -49
- package/dist/core/toolGates.js.map +0 -1
- package/dist/types/persistence.d.ts +0 -254
- package/dist/types/persistence.d.ts.map +0 -1
- package/dist/types/persistence.js +0 -6
- package/dist/types/persistence.js.map +0 -1
- package/dist/types/prompt-cache.d.ts +0 -15
- package/dist/types/prompt-cache.d.ts.map +0 -1
- package/dist/types/prompt-cache.js +0 -5
- package/dist/types/prompt-cache.js.map +0 -1
- package/dist/types/signals.d.ts +0 -263
- package/dist/types/signals.d.ts.map +0 -1
- package/dist/types/signals.js +0 -10
- package/dist/types/signals.js.map +0 -1
- package/dist/types/template.d.ts +0 -84
- package/dist/types/template.d.ts.map +0 -1
- package/dist/types/template.js +0 -2
- package/dist/types/template.js.map +0 -1
- package/dist/utils/condition.d.ts +0 -63
- package/dist/utils/condition.d.ts.map +0 -1
- package/dist/utils/condition.js +0 -230
- package/dist/utils/condition.js.map +0 -1
- package/dist/utils/event.d.ts +0 -6
- package/dist/utils/event.d.ts.map +0 -1
- package/dist/utils/event.js +0 -17
- package/dist/utils/event.js.map +0 -1
- package/dist/utils/id.d.ts +0 -33
- package/dist/utils/id.d.ts.map +0 -1
- package/dist/utils/id.js +0 -77
- package/dist/utils/id.js.map +0 -1
- package/dist/utils/serialize.d.ts +0 -36
- package/dist/utils/serialize.d.ts.map +0 -1
- package/dist/utils/serialize.js +0 -72
- package/dist/utils/serialize.js.map +0 -1
- package/dist/utils/session.d.ts +0 -124
- package/dist/utils/session.d.ts.map +0 -1
- package/dist/utils/session.js +0 -379
- package/dist/utils/session.js.map +0 -1
- package/docs/concepts/directives.md +0 -369
- package/docs/reference/adapters.md +0 -543
- package/docs/reference/create-agent.md +0 -216
- package/docs/reference/directive.md +0 -242
- package/docs/reference/signals.md +0 -368
- package/examples/02-data-extraction.ts +0 -90
- package/examples/05-branching.ts +0 -140
- package/examples/06-flow-control.ts +0 -103
- package/examples/08-persistence.ts +0 -98
- package/examples/09-signals.ts +0 -144
- package/src/adapters/MemoryAdapter.ts +0 -281
- package/src/adapters/MongoAdapter.ts +0 -341
- package/src/adapters/OpenSearchAdapter.ts +0 -693
- package/src/adapters/PostgreSQLAdapter.ts +0 -487
- package/src/adapters/PrismaAdapter.ts +0 -617
- package/src/adapters/RedisAdapter.ts +0 -439
- package/src/adapters/SQLiteAdapter.ts +0 -496
- package/src/adapters/index.ts +0 -43
- package/src/adapters/sessionRow.ts +0 -57
- package/src/constants/index.ts +0 -2
- package/src/core/AutoChainExecutor.ts +0 -397
- package/src/core/BranchEvaluator.ts +0 -161
- package/src/core/DirectiveChainTracker.ts +0 -144
- package/src/core/Events.ts +0 -164
- package/src/core/Flow.ts +0 -665
- package/src/core/FlowRouter.ts +0 -1540
- package/src/core/PersistenceManager.ts +0 -446
- package/src/core/PromptComposer.ts +0 -448
- package/src/core/PromptSectionCache.ts +0 -125
- package/src/core/ResponseEngine.ts +0 -338
- package/src/core/ResponseGenerationError.ts +0 -53
- package/src/core/ResponseModal.ts +0 -1902
- package/src/core/ResponsePipeline.ts +0 -1404
- package/src/core/SessionFinalizer.ts +0 -108
- package/src/core/SessionManager.ts +0 -372
- package/src/core/SignalCoordinator.ts +0 -263
- package/src/core/SignalEvaluator.ts +0 -404
- package/src/core/SignalProcessor.ts +0 -663
- package/src/core/Step.ts +0 -782
- package/src/core/StepLifecycle.ts +0 -242
- package/src/core/StreamingToolExecutor.ts +0 -609
- package/src/core/ToolLoopExecutor.ts +0 -749
- package/src/core/ToolManager.ts +0 -1379
- package/src/core/createAgent.ts +0 -40
- package/src/core/flow-namespace.ts +0 -227
- package/src/core/toolGates.ts +0 -72
- package/src/types/persistence.ts +0 -303
- package/src/types/prompt-cache.ts +0 -17
- package/src/types/signals.ts +0 -338
- package/src/types/template.ts +0 -98
- package/src/utils/condition.ts +0 -296
- package/src/utils/event.ts +0 -16
- package/src/utils/id.ts +0 -91
- package/src/utils/serialize.ts +0 -86
- package/src/utils/session.ts +0 -501
package/docs/reference/tool.md
CHANGED
|
@@ -1,84 +1,44 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: "Tool"
|
|
3
|
-
description: "
|
|
3
|
+
description: "A typed function the model may call while it speaks; it returns a value for the model and data for the session, and never moves the run."
|
|
4
4
|
type: reference
|
|
5
|
-
order:
|
|
5
|
+
order: 9
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
# Tool
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
A tool is a function the model may call in the middle of phrasing a reply: look up a price, check a calendar, book a room. The model picks the tool and its arguments; your handler runs; the result goes back to the model; the model answers. A tool returns `{ value?, data? }` and nothing else. `value` is what the model reads. `data` is written into the session's collected fields. Movement between steps belongs to the flow, never to a tool.
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
`Tool.id` is the sole identifier.
|
|
15
|
-
|
|
16
|
-
Since v2.4 the generic defaults are `unknown` (previously `any`) on `Tool`, `ToolContext`, `ToolResult`, and `ToolHandler`. Pass explicit type parameters — or let inference flow from `createAgent`'s `schema` — to get typed `ctx.data` and `ctx.context`; untyped tool code that relied on implicit `any` needs explicit generics or a type guard.
|
|
12
|
+
Source: `src/types/tool.ts`, `src/core/Speak.ts`, `src/core/Runner.ts` (`speakRequest`), `src/types/agent.ts` (`maxToolLoops`).
|
|
17
13
|
|
|
18
14
|
## Signature
|
|
19
15
|
|
|
20
|
-
```
|
|
21
|
-
interface Tool<
|
|
22
|
-
// Identity
|
|
16
|
+
```ts fragment
|
|
17
|
+
interface Tool<C = unknown, D = unknown> {
|
|
23
18
|
id: string;
|
|
24
19
|
description?: string;
|
|
25
|
-
parameters?:
|
|
26
|
-
|
|
27
|
-
// Handler
|
|
28
|
-
handler: ToolHandler<TContext, TData, TResult>;
|
|
20
|
+
parameters?: StructuredSchema;
|
|
21
|
+
handler(args: Record<string, unknown>, ctx: ToolCtx<C, D>): ToolResult<D> | Promise<ToolResult<D>>;
|
|
29
22
|
|
|
30
|
-
|
|
31
|
-
isReadOnly?(input
|
|
32
|
-
|
|
33
|
-
isDestructive?(input?: Record<string, unknown>): boolean;
|
|
34
|
-
interruptBehavior?(): 'cancel' | 'block';
|
|
23
|
+
isConcurrencySafe?(input: Record<string, unknown>): boolean;
|
|
24
|
+
isReadOnly?(input: Record<string, unknown>): boolean;
|
|
25
|
+
isDestructive?(input: Record<string, unknown>): boolean;
|
|
35
26
|
maxResultSizeChars?: number;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
input: Record<string, unknown>,
|
|
39
|
-
context: ToolContext<TContext, TData>,
|
|
40
|
-
): Promise<ToolValidationResult> | ToolValidationResult;
|
|
41
|
-
|
|
42
|
-
checkPermissions?(
|
|
43
|
-
input: Record<string, unknown>,
|
|
44
|
-
context: ToolContext<TContext, TData>,
|
|
45
|
-
): Promise<ToolPermissionResult> | ToolPermissionResult;
|
|
27
|
+
validateInput?(input: Record<string, unknown>, ctx: ToolCtx<C, D>): ToolValidationResult | Promise<ToolValidationResult>;
|
|
28
|
+
checkPermissions?(input: Record<string, unknown>, ctx: ToolCtx<C, D>): ToolPermissionResult | Promise<ToolPermissionResult>;
|
|
46
29
|
}
|
|
47
30
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
| ToolResult<TResult, TContext, TData>;
|
|
55
|
-
|
|
56
|
-
interface ToolContext<TContext, TData> {
|
|
57
|
-
context: TContext;
|
|
58
|
-
data: Partial<TData>;
|
|
59
|
-
history: Event[];
|
|
60
|
-
step?: StepRef;
|
|
61
|
-
metadata?: Record<string, unknown>;
|
|
62
|
-
|
|
63
|
-
updateContext(updates: Partial<TContext>): Promise<void>;
|
|
64
|
-
updateData(updates: Partial<TData>): Promise<void>;
|
|
65
|
-
getField<K extends keyof TData>(key: K): TData[K] | undefined;
|
|
66
|
-
setField<K extends keyof TData>(key: K, value: TData[K]): Promise<void>;
|
|
67
|
-
hasField<K extends keyof TData>(key: K): boolean;
|
|
68
|
-
|
|
69
|
-
/** Imperative redirection — emit a directive mid-handler. */
|
|
70
|
-
dispatch(directive: Directive<TContext, TData>): void;
|
|
31
|
+
interface ToolCtx<C = unknown, D = unknown> {
|
|
32
|
+
context: C;
|
|
33
|
+
data: Partial<D>;
|
|
34
|
+
history: History;
|
|
35
|
+
run?: Run;
|
|
36
|
+
now: Date;
|
|
71
37
|
}
|
|
72
38
|
|
|
73
|
-
interface ToolResult<
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
dataUpdate?: Partial<TData>;
|
|
77
|
-
success?: boolean;
|
|
78
|
-
error?: string;
|
|
79
|
-
meta?: Record<string, unknown>;
|
|
80
|
-
/** Declarative redirection — emit a directive on return. */
|
|
81
|
-
directive?: Directive<TContext, TData>;
|
|
39
|
+
interface ToolResult<D = unknown> {
|
|
40
|
+
value?: unknown;
|
|
41
|
+
data?: Partial<D>;
|
|
82
42
|
}
|
|
83
43
|
|
|
84
44
|
interface ToolValidationResult {
|
|
@@ -90,191 +50,162 @@ interface ToolValidationResult {
|
|
|
90
50
|
interface ToolPermissionResult {
|
|
91
51
|
allowed: boolean;
|
|
92
52
|
reason?: string;
|
|
93
|
-
canOverride?: boolean;
|
|
94
53
|
}
|
|
95
54
|
```
|
|
96
55
|
|
|
97
|
-
##
|
|
98
|
-
|
|
99
|
-
### `Tool`
|
|
100
|
-
|
|
101
|
-
| Field | Type | Required | Default | Notes |
|
|
102
|
-
|-------|------|----------|---------|-------|
|
|
103
|
-
| `id` | `string` | yes | — | Unique identifier. The AI references this name when calling the tool. There is no separate `name` field. |
|
|
104
|
-
| `handler` | `ToolHandler` | yes | — | The function the AI invokes. Receives `ctx` and optional `args`. |
|
|
105
|
-
| `description` | `string` | no | — | Free-form description for AI tool discovery. |
|
|
106
|
-
| `parameters` | `unknown` | no | — | Argument schema (provider-specific shape; pass through to the LLM). |
|
|
107
|
-
| `isReadOnly` | `(input?) => boolean` | no | — | Returns `true` when the call has no side effects. Enables result caching and concurrency. |
|
|
108
|
-
| `isConcurrencySafe` | `(input?) => boolean` | no | — | Returns `true` when this call may run in parallel with other concurrent-safe calls. |
|
|
109
|
-
| `isDestructive` | `(input?) => boolean` | no | — | Returns `true` for irreversible operations. Surfaces to confirmation UIs. |
|
|
110
|
-
| `interruptBehavior` | `() => 'cancel' \| 'block'` | no | `'cancel'` | How the tool reacts to abort signals. `'block'` waits for natural completion. |
|
|
111
|
-
| `maxResultSizeChars` | `number` | no | — | Truncation cap for the serialized result, before history compaction. |
|
|
112
|
-
| `validateInput` | `(input, ctx) => ToolValidationResult` | no | — | Pre-execution input check. May return `correctedInput` to repair the call. |
|
|
113
|
-
| `checkPermissions` | `(input, ctx) => ToolPermissionResult` | no | — | Pre-execution gate. When `allowed: false`, the handler is **not** invoked. |
|
|
114
|
-
|
|
115
|
-
### `ToolContext`
|
|
116
|
-
|
|
117
|
-
| Field | Type | Notes |
|
|
118
|
-
|-------|------|-------|
|
|
119
|
-
| `context` | `TContext` | Ambient app data (user, env, services). |
|
|
120
|
-
| `data` | `Partial<TData>` | Everything collected so far across the conversation. |
|
|
121
|
-
| `history` | `Event[]` | Native multi-turn history (read-only). |
|
|
122
|
-
| `step` | `StepRef \| undefined` | Identifies the current flow/step when the tool runs inside a flow. |
|
|
123
|
-
| `metadata` | `Record<string, unknown> \| undefined` | Free-form per-call metadata. |
|
|
124
|
-
| `updateContext` | `(updates) => Promise<void>` | Shallow-merge into `context`. Triggers context lifecycle hooks. |
|
|
125
|
-
| `updateData` | `(updates) => Promise<void>` | Shallow-merge into `data`. Triggers data lifecycle hooks. |
|
|
126
|
-
| `getField` / `setField` / `hasField` | `(key) => …` | Typed accessors over `data`. |
|
|
127
|
-
| `dispatch` | `(directive) => void` | Imperative directive emit. May be called multiple times; emissions are merged by Algorithm 4 alongside other tool/hook directives this turn. |
|
|
128
|
-
|
|
129
|
-
### `ToolResult`
|
|
130
|
-
|
|
131
|
-
| Field | Type | Notes |
|
|
132
|
-
|-------|------|-------|
|
|
133
|
-
| `data` | `TResultData` | The value the AI sees as the tool result. |
|
|
134
|
-
| `contextUpdate` | `Partial<TContext>` | Shallow-merged into `context` after the call. |
|
|
135
|
-
| `dataUpdate` | `Partial<TData>` | Shallow-merged into `data` after the call. |
|
|
136
|
-
| `success` | `boolean` | When `false`, the executor treats this as a failed call and surfaces `error`. |
|
|
137
|
-
| `error` | `string` | Failure message when `success === false`. |
|
|
138
|
-
| `meta` | `Record<string, unknown>` | Free-form metadata (stored on the tool event). |
|
|
139
|
-
| `directive` | `Directive` | Declarative redirection. Equivalent to calling `ctx.dispatch(directive)` once. |
|
|
140
|
-
|
|
141
|
-
### `ToolValidationResult`
|
|
142
|
-
|
|
143
|
-
| Field | Type | Notes |
|
|
144
|
-
|-------|------|-------|
|
|
145
|
-
| `valid` | `boolean` | `false` blocks execution and surfaces `error` to the AI. |
|
|
146
|
-
| `error` | `string` | Why validation failed. |
|
|
147
|
-
| `correctedInput` | `Record<string, unknown>` | When present, the executor retries with this input instead of the original. |
|
|
148
|
-
|
|
149
|
-
### `ToolPermissionResult`
|
|
150
|
-
|
|
151
|
-
| Field | Type | Notes |
|
|
152
|
-
|-------|------|-------|
|
|
153
|
-
| `allowed` | `boolean` | `false` blocks execution; the handler is never called. |
|
|
154
|
-
| `reason` | `string` | Why permission was denied. |
|
|
155
|
-
| `canOverride` | `boolean` | Hint to UIs: the user may grant a one-time override. |
|
|
156
|
-
|
|
157
|
-
## Examples
|
|
158
|
-
|
|
159
|
-
### 1. Imperative redirection with `ctx.dispatch`
|
|
160
|
-
|
|
161
|
-
A tool that runs mid-flow and decides the rest of the turn is moot — for example, an eligibility check that fails and should jump straight to a denial flow.
|
|
162
|
-
|
|
163
|
-
```typescript
|
|
164
|
-
import type { Tool } from "@falai/agent";
|
|
165
|
-
|
|
166
|
-
type Ctx = { userId: string };
|
|
167
|
-
type Data = { country: string };
|
|
168
|
-
|
|
169
|
-
export const checkEligibility: Tool<Ctx, Data, { ok: boolean }> = {
|
|
170
|
-
id: "check_eligibility",
|
|
171
|
-
description: "Verify the user can proceed with booking.",
|
|
172
|
-
isReadOnly: () => true,
|
|
173
|
-
async handler(ctx) {
|
|
174
|
-
const ok = await isEligible(ctx.context.userId, ctx.data.country);
|
|
175
|
-
|
|
176
|
-
if (!ok) {
|
|
177
|
-
// Imperative: stop reasoning, jump to the denial flow.
|
|
178
|
-
ctx.dispatch({
|
|
179
|
-
goTo: "denial",
|
|
180
|
-
reply: "Sorry — you're not eligible for this service.",
|
|
181
|
-
});
|
|
182
|
-
return { ok: false };
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return { ok: true };
|
|
186
|
-
},
|
|
187
|
-
};
|
|
188
|
-
```
|
|
56
|
+
## Tool fields
|
|
189
57
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
The
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (!ok) {
|
|
203
|
-
return {
|
|
204
|
-
data: { ok: false },
|
|
205
|
-
directive: {
|
|
206
|
-
goTo: "denial",
|
|
207
|
-
reply: "Sorry — you're not eligible for this service.",
|
|
208
|
-
},
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return { data: { ok: true } };
|
|
213
|
-
},
|
|
214
|
-
};
|
|
215
|
-
```
|
|
58
|
+
| Field | Type | Default | Meaning |
|
|
59
|
+
|---|---|---|---|
|
|
60
|
+
| `id` | `string` | required | The name the model calls, and the name a `tools: [...]` list uses to point at this tool. Ids are not checked for uniqueness: two tools with one id are both sent to the provider, and the first one's handler runs for any call under that id. |
|
|
61
|
+
| `description` | `string` | none | What the tool does and when to use it, for the model. |
|
|
62
|
+
| `parameters` | `StructuredSchema` | none | A JSON Schema **object** for `args`, passed to the provider as given: `{ type: "object", properties: { … }, required: [ … ] }`. An action's shorthand map (`{ cidade: { type: "string" } }`) is not one, and `f.agent()` rejects it — most providers accept the malformed declaration and simply never call the tool. |
|
|
63
|
+
| `handler` | `(args, ctx) => ToolResult \| Promise<ToolResult>` | required | Your code. Returning nothing counts as `{}`. |
|
|
64
|
+
| `isReadOnly` | `(input) => boolean` | none | The call only reads. Used as the fallback for `isConcurrencySafe`. |
|
|
65
|
+
| `isConcurrencySafe` | `(input) => boolean` | falls back to `isReadOnly`, then `false` | The call may run in parallel with other safe calls of the same round. |
|
|
66
|
+
| `isDestructive` | `(input) => boolean` | none | The call cannot be undone. A destructive call never runs in parallel. |
|
|
67
|
+
| `maxResultSizeChars` | `number` | none (no cut) | Longest `value` the model gets. Beyond it, the text is cut and ends with `[truncated: N chars total, showing the first M]`. |
|
|
68
|
+
| `validateInput` | `(input, ctx) => ToolValidationResult` | none | Runs first. On `valid: false` the handler is skipped and the model reads `{"error":"Validation failed: <error>","correctedInput":…}`. |
|
|
69
|
+
| `checkPermissions` | `(input, ctx) => ToolPermissionResult` | none | Runs after validation. On `allowed: false` the handler is skipped and the model reads `{"error":"Permission denied: <reason>"}`. |
|
|
216
70
|
|
|
217
|
-
|
|
71
|
+
## ToolCtx fields
|
|
218
72
|
|
|
219
|
-
|
|
73
|
+
| Field | Type | Meaning |
|
|
74
|
+
|---|---|---|
|
|
75
|
+
| `context` | `C` | The host context of this turn. |
|
|
76
|
+
| `data` | `Partial<D>` | The session's fields at the start of the speak call, plus every `data` patch earlier tool rounds of this same call returned. |
|
|
77
|
+
| `history` | `History` | The history the model sees: the host's history for this turn, plus the assistant and tool items of earlier rounds in this call. |
|
|
78
|
+
| `run` | `Run \| undefined` | The run whose talk step is speaking. Absent when the idle speaker is the one calling. |
|
|
79
|
+
| `now` | `Date` | The agent's clock. |
|
|
220
80
|
|
|
221
|
-
|
|
222
|
-
export const bookHotel: Tool<Ctx, Data, { id: string }> = {
|
|
223
|
-
id: "book_hotel",
|
|
224
|
-
description: "Reserve a hotel for the collected dates.",
|
|
225
|
-
isDestructive: () => true,
|
|
226
|
-
isConcurrencySafe: () => false,
|
|
227
|
-
maxResultSizeChars: 2_000,
|
|
228
|
-
|
|
229
|
-
validateInput(input) {
|
|
230
|
-
if (typeof input.nights !== "number" || input.nights < 1) {
|
|
231
|
-
return { valid: false, error: "`nights` must be a positive integer." };
|
|
232
|
-
}
|
|
233
|
-
return { valid: true };
|
|
234
|
-
},
|
|
81
|
+
## ToolResult fields
|
|
235
82
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
return { allowed: true };
|
|
241
|
-
},
|
|
83
|
+
| Field | Type | What happens |
|
|
84
|
+
|---|---|---|
|
|
85
|
+
| `value` | `unknown` | Serialized for the model: a string as is, anything else through `JSON.stringify`, `undefined` as `{"ok":true}`. Cut at `maxResultSizeChars` when set. |
|
|
86
|
+
| `data` | `Partial<D>` | Merged into the session's fields when the turn settles, as given: no type coercion, no `enum` check. A field a `collect` step is waiting for counts as known once a tool writes it, so the step can end without the customer saying it. |
|
|
242
87
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
88
|
+
## Which tools the model sees
|
|
89
|
+
|
|
90
|
+
The list is an allow-list of tool ids, resolved once per speak call:
|
|
91
|
+
|
|
92
|
+
| Speaker | List used |
|
|
93
|
+
|---|---|
|
|
94
|
+
| A talk step | `step.tools`, else `flow.tools`, else every tool on the agent. |
|
|
95
|
+
| The idle speaker | `idle.tools`, else every tool on the agent. |
|
|
96
|
+
|
|
97
|
+
An empty list (`tools: []`) means no tools. Every name in a list must be a registered tool id; `validateFlow` throws `FlowConfigurationError` for a step or flow list, and the agent constructor throws for `idle.tools`.
|
|
98
|
+
|
|
99
|
+
The agent's `maxToolLoops` (default 5, from `src/types/agent.ts`) caps the rounds. `maxToolLoops: 0` sends no tools at all, whatever the lists say.
|
|
100
|
+
|
|
101
|
+
## Rounds
|
|
102
|
+
|
|
103
|
+
One speak call is a loop of provider rounds:
|
|
104
|
+
|
|
105
|
+
1. Each of the first `maxToolLoops` rounds offers the tools. The model may answer, call tools, or both.
|
|
106
|
+
2. When it calls tools, each call goes through the gates below and its result becomes a `tool` history item. The model is asked again with that history.
|
|
107
|
+
3. When it calls no tools, the loop ends and its message is the reply.
|
|
108
|
+
4. After `maxToolLoops` rounds with calls, one more round runs with no tools and a "wrap up" section, so a message always comes back.
|
|
109
|
+
|
|
110
|
+
Every round is one model call and counts in `TurnResult.llmCalls`. A text turn therefore costs one understand call plus one call per speak round: up to `maxToolLoops` rounds with tools and one to wrap up, so six speak calls with the default, seven model calls in all. A round that fails at the provider, or a final message that is empty, defers the talk step — or ends the run, when no wait can fix what failed: see [Outcomes](./outcomes.md), the `provider-*` codes. Rounds that already completed still count in `TurnResult.usage`.
|
|
111
|
+
|
|
112
|
+
Within a round, consecutive calls that are safe (not destructive, and `isConcurrencySafe` true; when `isConcurrencySafe` is not defined, `isReadOnly` true) run together with `Promise.all`; any other call runs alone, in order. `data` patches merge in call order, not in the order the calls finished. Field values the model reports in its structured reply are checked and coerced; tool `data` is not.
|
|
113
|
+
|
|
114
|
+
## Gates, in order
|
|
115
|
+
|
|
116
|
+
| Situation | What the model reads | Handler runs |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| The id is not in this call's list | `{"error":"Tool \"x\" is not available."}` | no |
|
|
119
|
+
| `validateInput` returns `valid: false` | `{"error":"Validation failed: <error or 'invalid input'>","correctedInput":…}` | no |
|
|
120
|
+
| `checkPermissions` returns `allowed: false` | `{"error":"Permission denied: <reason or 'not allowed'>"}` | no |
|
|
121
|
+
| The handler throws | `{"error":"<message>"}` | yes, and failed |
|
|
122
|
+
| The handler returns | `value`, serialized | yes |
|
|
123
|
+
|
|
124
|
+
Nothing a tool does reaches the host as an exception. The speak call never throws for a tool.
|
|
125
|
+
|
|
126
|
+
## Tool rounds in the history
|
|
127
|
+
|
|
128
|
+
Inside a call, a round is recorded as one assistant item with `tool_calls` (ids `call-<round>-<index>`, any text the model wrote beside the calls as `content`, else `null`) followed by one `tool` item per call. These items live for the duration of the speak call; the framework returns `messages[]`, not history, so your own history is what you save.
|
|
129
|
+
|
|
130
|
+
The exported `ToolCall` type belongs to the event-style history the conversion helpers (`eventsToHistory`, `historyToEvents`) read and write:
|
|
131
|
+
|
|
132
|
+
```ts fragment
|
|
133
|
+
interface ToolCall<TArgs = unknown, TResult = unknown> {
|
|
134
|
+
tool_id: string;
|
|
135
|
+
arguments: TArgs;
|
|
136
|
+
result: { data: TResult; meta?: Record<string, unknown> };
|
|
137
|
+
}
|
|
252
138
|
```
|
|
253
139
|
|
|
254
|
-
|
|
140
|
+
It is a record of a call that already happened, for hosts that store history as events. The framework does not build it during a turn.
|
|
141
|
+
|
|
142
|
+
## Streaming
|
|
143
|
+
|
|
144
|
+
`agent.turnStream()` streams every round's text as it arrives. Tools run between rounds exactly as above; the last chunk carries the same `TurnResult`. Text the model writes beside a tool call reaches the stream but not the final message.
|
|
145
|
+
|
|
146
|
+
## Example
|
|
255
147
|
|
|
256
|
-
|
|
148
|
+
```ts
|
|
149
|
+
import { falai, GeminiProvider, type DataOf, type Tool } from "@falai/agent";
|
|
257
150
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
151
|
+
const f = falai().fields({
|
|
152
|
+
cidade: { type: "string", ask: "Pergunte em qual cidade a pessoa quer ficar." },
|
|
153
|
+
reserva: { type: "string", description: "Código da reserva" },
|
|
154
|
+
});
|
|
261
155
|
|
|
262
|
-
|
|
156
|
+
// The smallest tool that works: one argument in, one value out. `value` is what the model reads back.
|
|
157
|
+
const preco: Tool = {
|
|
158
|
+
id: "preco",
|
|
159
|
+
description: "Preço da diária em uma cidade.",
|
|
160
|
+
parameters: { type: "object", properties: { cidade: { type: "string" } }, required: ["cidade"] },
|
|
161
|
+
handler: () => ({ value: { precoNoite: 420 } }),
|
|
162
|
+
};
|
|
263
163
|
|
|
264
|
-
|
|
164
|
+
type Data = DataOf<typeof f>;
|
|
265
165
|
|
|
266
|
-
|
|
166
|
+
// A bigger one: it checks its input, never runs in parallel, and writes a field.
|
|
167
|
+
const reservar: Tool<undefined, Data> = {
|
|
168
|
+
id: "reservar",
|
|
169
|
+
description: "Confirma a reserva e devolve o código.",
|
|
170
|
+
parameters: { type: "object", properties: { cidade: { type: "string" } }, required: ["cidade"] },
|
|
171
|
+
isDestructive: () => true,
|
|
172
|
+
validateInput: (args) =>
|
|
173
|
+
typeof args.cidade === "string" && args.cidade.length > 0
|
|
174
|
+
? { valid: true }
|
|
175
|
+
: { valid: false, error: "Informe a cidade." },
|
|
176
|
+
handler: (_args, ctx) => {
|
|
177
|
+
const codigo = `RES-${ctx.now.getTime().toString(36).toUpperCase()}`;
|
|
178
|
+
// `data` lands in the session: the `confirma` step ends once `reserva` is known.
|
|
179
|
+
return { value: { codigo }, data: { reserva: codigo } };
|
|
180
|
+
},
|
|
181
|
+
};
|
|
267
182
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
183
|
+
const agent = f.agent({
|
|
184
|
+
name: "Concierge",
|
|
185
|
+
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY ?? "", model: "gemini-2.5-flash" }),
|
|
186
|
+
tools: [preco, reservar],
|
|
187
|
+
flows: [
|
|
188
|
+
f.flow({
|
|
189
|
+
id: "hospedagem",
|
|
190
|
+
name: "Hospedagem",
|
|
191
|
+
on: [{ message: ["quer reservar um quarto"] }],
|
|
192
|
+
steps: [
|
|
193
|
+
{ id: "onde", collect: ["cidade"] },
|
|
194
|
+
// Only this step may call `preco`.
|
|
195
|
+
{ id: "oferta", prompt: "Apresente o preço da diária. Pergunte se pode reservar.", tools: ["preco"] },
|
|
196
|
+
{ id: "confirma", prompt: "Se a pessoa confirmou, reserve e informe o código.", collect: ["reserva"], tools: ["reservar"] },
|
|
197
|
+
],
|
|
198
|
+
}),
|
|
199
|
+
],
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const r = await agent.turn({ sessionId: "demo", message: "Quero um quarto em Curitiba" });
|
|
203
|
+
console.log(r.messages[0]?.text, r.llmCalls);
|
|
204
|
+
```
|
|
272
205
|
|
|
273
|
-
##
|
|
206
|
+
## See also
|
|
274
207
|
|
|
275
|
-
- [Add tools](../start/04-add-tools.md)
|
|
276
|
-
- [
|
|
277
|
-
- [
|
|
278
|
-
- [
|
|
279
|
-
- [Flow control](../guides/flow-control.md) — recipes for redirecting from tools and hooks.
|
|
280
|
-
- [Errors](./errors.md) — `ToolExecutionError` format contract.
|
|
208
|
+
- [Add tools](../start/04-add-tools.md): tools the model calls versus actions the flow runs.
|
|
209
|
+
- [Actions, events, conditions](./actions-events-conditions.md): the `do` step's side of the same line.
|
|
210
|
+
- [Streaming](../guides/streaming.md): tool rounds inside `turnStream`.
|
|
211
|
+
- [Agent](./agent.md): `tools`, `maxToolLoops` and `idle`.
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Trigger"
|
|
3
|
+
description: "The Trigger union kind by kind, Repeat and its default per kind, and the trigger key, run id, dedupe key and wake key each kind mints."
|
|
4
|
+
type: reference
|
|
5
|
+
order: 4
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Trigger
|
|
9
|
+
|
|
10
|
+
A trigger says when a run of a flow starts. There are four: the customer asks for the flow (`message`), the customer mentions something (`mention`), the customer goes quiet (`silence`), or something happens in the host (`event`). A flow with no trigger starts only by hand, with `turn({ start })`, or from another flow's `then: { flow }`. Every trigger may carry `repeat` and a code `if`.
|
|
11
|
+
|
|
12
|
+
## Signature
|
|
13
|
+
|
|
14
|
+
```ts fragment
|
|
15
|
+
type Trigger<C, D> = { repeat?: Repeat } & (
|
|
16
|
+
| { message: string[]; if?: Pred<C, D> }
|
|
17
|
+
| { mention: string[]; extract?: ParamDefs; if?: Pred<C, D> }
|
|
18
|
+
| { silence: Duration; if?: Pred<C, D>; businessHours?: boolean }
|
|
19
|
+
| { event: string; if?: Pred<C, D>; after?: Duration; businessHours?: boolean }
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
type Repeat = "once" | "always" | { cooldown: Duration };
|
|
23
|
+
|
|
24
|
+
type TriggerKind = "message" | "mention" | "silence" | "event" | "start" | "flow";
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`TriggerKind` is what `run.trigger.kind` records. It has two values no trigger has: `'start'` for `turn({ start })` and `'flow'` for a run another flow started.
|
|
28
|
+
|
|
29
|
+
## The four triggers
|
|
30
|
+
|
|
31
|
+
### message
|
|
32
|
+
|
|
33
|
+
| Field | Type | Default | Meaning |
|
|
34
|
+
|---|---|---|---|
|
|
35
|
+
| `message` | `string[]` | required | Phrases that describe what the customer asks for. The model scores the flow against the message. `[]` is the catch-all: it is never scored, and it starts only when no other message flow scores 40 or more. |
|
|
36
|
+
| `if` | `Pred<C, D>` | none | Code gate. The flow is not offered to the model when it is false. |
|
|
37
|
+
| `repeat` | `Repeat` | `'once'` | |
|
|
38
|
+
|
|
39
|
+
The run takes the conversation: it becomes the asker and holds the floor — only one run asks at a time.
|
|
40
|
+
|
|
41
|
+
### mention
|
|
42
|
+
|
|
43
|
+
| Field | Type | Default | Meaning |
|
|
44
|
+
|---|---|---|---|
|
|
45
|
+
| `mention` | `string[]` | required | Phrases that describe what the customer brings up. The model answers true or false. `[]` with `if` is a code-only detector. |
|
|
46
|
+
| `extract` | `ParamDefs` | none | Values the model pulls from the message when it says true. They become the run's `input`, read as `{{input.x}}` in templates and `ctx.input` in code. |
|
|
47
|
+
| `if` | `Pred<C, D>` | none | Code gate, judged at start with `extract` in hand as `input`. |
|
|
48
|
+
| `repeat` | `Repeat` | `'once'` | |
|
|
49
|
+
|
|
50
|
+
The run starts beside the conversation. It is meant for `do` and `say` steps: it does not get routed to. A talk step in it behaves like any talk step and takes the floor.
|
|
51
|
+
|
|
52
|
+
### silence
|
|
53
|
+
|
|
54
|
+
| Field | Type | Default | Meaning |
|
|
55
|
+
|---|---|---|---|
|
|
56
|
+
| `silence` | `Duration` | required | How long the customer has been quiet since the assistant last spoke. |
|
|
57
|
+
| `if` | `Pred<C, D>` | none | Judged when the wake is armed and again when it fires. |
|
|
58
|
+
| `businessHours` | `boolean` | `false` | Snap the wake forward with the agent's `businessHours`. |
|
|
59
|
+
| `repeat` | `Repeat` | `'once'` | |
|
|
60
|
+
|
|
61
|
+
### event
|
|
62
|
+
|
|
63
|
+
| Field | Type | Default | Meaning |
|
|
64
|
+
|---|---|---|---|
|
|
65
|
+
| `event` | `string` | required | The event's name in the agent's `events`. |
|
|
66
|
+
| `after` | `Duration` | none | Park the run this long before its first step. |
|
|
67
|
+
| `if` | `Pred<C, D>` | none | Judged when the event arrives, with the payload as `input`. |
|
|
68
|
+
| `businessHours` | `boolean` | `false` | Snap the `after` wake forward. |
|
|
69
|
+
| `repeat` | `Repeat` | `'always'` | |
|
|
70
|
+
|
|
71
|
+
The event's `payload` is the run's `input`.
|
|
72
|
+
|
|
73
|
+
## Repeat
|
|
74
|
+
|
|
75
|
+
| Value | Meaning |
|
|
76
|
+
|---|---|
|
|
77
|
+
| `'once'` | One run per flow and anchor, ever. A second match is skipped with `code: 'already-claimed'`. |
|
|
78
|
+
| `'always'` | A run per input. The claim carries the trigger key, so only a replay of the same input is skipped. |
|
|
79
|
+
| `{ cooldown: '7d' }` | A run, then none until the cooldown passes (`code: 'cooldown'`). The claim is refreshed on each start. |
|
|
80
|
+
|
|
81
|
+
Default: `'once'` for `message`, `mention` and `silence`; `'always'` for `event`. Runs started by `turn({ start })` or by another flow are `'always'`.
|
|
82
|
+
|
|
83
|
+
## Keys
|
|
84
|
+
|
|
85
|
+
Every key is deterministic: the same input against the same session mints the same key.
|
|
86
|
+
|
|
87
|
+
| Kind | Trigger key | Run id |
|
|
88
|
+
|---|---|---|
|
|
89
|
+
| message, mention | the message `id`; without one, its `at`; without that, the clock's now as ISO | `${flowId}#${triggerKey}` |
|
|
90
|
+
| silence | `session.lastAssistantAt` in milliseconds since the epoch, as a string | `${flowId}#${ms}` |
|
|
91
|
+
| event | the `key` passed with `turn({ event })` | `${flowId}#${key}` |
|
|
92
|
+
| start | `start.key` | `${flowId}#${key}` |
|
|
93
|
+
| flow | the parent step's key `${parentRunId}:${stepId}:${visit}` | `${flowId}#${parentRunId}:${stepId}:${visit}` |
|
|
94
|
+
|
|
95
|
+
Pass a real message `id` on every message turn. Only `id` is checked against the session's last 50 inputs; without it a replay is not detected, and without `at` as well the key is the clock's now, so the replay mints new keys.
|
|
96
|
+
|
|
97
|
+
**Dedupe key.** `${flowId}:${anchor}:${nonce}`. The nonce is the trigger key when `repeat` is `'always'` and empty otherwise. It is written to `session.claims` when the run starts, given to actions as `ctx.dedupeKey`, and returned in `started[]`. The last 50 `'always'` claims per flow and anchor are kept; `'once'` and cooldown claims are never pruned. The host may pass claims from the customer's other sessions in `turn({ claims })`; they count the same.
|
|
98
|
+
|
|
99
|
+
**Wake for `after`.** `${runId}:start:${atMs}`, where `atMs` is the fire time in milliseconds after `businessHours` snapping. The run is returned in `started[]` at once with the outcome `code: 'awaiting-trigger'`. At the wake it enters its first step. A second event for the same flow and anchor while it is parked replaces it: the parked run ends with reason `'replaced'`.
|
|
100
|
+
|
|
101
|
+
**Wake for silence.** `silence:${flowId}:${sessionId}:${lastAssistantAtMs}`. Armed at the end of every turn in which the assistant spoke and the customer has not written since, for every silence flow passing `if` and `repeat`; the entry's `replaces` names the previous silence wake. At fire time it is honoured only while `session.lastAssistantAt` still equals that timestamp and the customer has not written since (`code: 'silence-broken'` otherwise).
|
|
102
|
+
|
|
103
|
+
## Behaviour
|
|
104
|
+
|
|
105
|
+
**How message flows are chosen.** On a message turn, the eligible flows are those with a non-empty `message` list passing `if` and `repeat`, in agent order.
|
|
106
|
+
- One eligible flow and no run asking: it starts without being scored. The understand call is skipped altogether when there is nothing else to judge: no mention flows, no `when` branches, no unknown `'anywhere'` field.
|
|
107
|
+
- Several: the understand call scores each from 0 to 100. With no run asking, the top flow starts if it scores 40 or more; otherwise the first eligible `message: []` flow starts, if there is one.
|
|
108
|
+
- A run is asking: it keeps the floor unless another flow scores at least 15 above it and at least 40. Then that flow starts, or its suspended run resumes, and the asker is suspended.
|
|
109
|
+
- The catch-all `message: []` is never scored. It obeys `if` and `repeat` like any trigger, so with the default `'once'` it fires once per session. When nothing starts and no run is asking, the idle speaker answers.
|
|
110
|
+
|
|
111
|
+
**Mentions.** A non-empty `mention` list reaches the understand call on every message turn while `repeat` allows. When the model says true, the run starts with `extract` values as `input`; they arrive as the model returned them, with null and empty values dropped, and are not coerced. `mention: []` skips the model: the run goes through the start order on every message turn, so `if` and `repeat` decide.
|
|
112
|
+
|
|
113
|
+
**Where `if` runs.** With a draft run (`stepId: null`) for message eligibility and silence arming; with the real run about to start otherwise. `ctx.input` is the event payload or the mention's extract; `ctx.run` is the run; `ctx.silenced` is the host's reason, when any.
|
|
114
|
+
|
|
115
|
+
**Skip reasons** land in `TurnResult.skipped` with the flow, anchor and trigger key: `code: 'already-claimed'`, `code: 'cooldown'`, `code: 'already-running'` (one live run per flow and anchor, here or in another of the customer's sessions), `code: 'hop-limit'` (hop 5), `code: 'flow-gone'` (`start` or `{ flow }` named a flow that does not exist). A trigger whose `if` is false is not listed.
|
|
116
|
+
|
|
117
|
+
## Example
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { falai, GeminiProvider } from "@falai/agent";
|
|
121
|
+
|
|
122
|
+
interface Ctx {
|
|
123
|
+
lead: { dono: "ia" | "humano"; etapa: string };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const f = falai<Ctx>().fields({
|
|
127
|
+
nome: { type: "string", ask: "Pergunte o nome." },
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const agent = f.agent({
|
|
131
|
+
name: "Ana",
|
|
132
|
+
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY ?? "", model: "gemini-2.5-flash" }),
|
|
133
|
+
events: { entrou_na_etapa: f.event<{ etapa: string }>() },
|
|
134
|
+
conditions: { naEtapa: f.condition((ctx, etapa: string) => ctx.context.lead.etapa === etapa) },
|
|
135
|
+
actions: { avisar: f.action({ parameters: { texto: { type: "string" } }, run: () => ({ ok: true }) }) },
|
|
136
|
+
flows: [
|
|
137
|
+
f.flow({
|
|
138
|
+
id: "triagem",
|
|
139
|
+
name: "Triagem",
|
|
140
|
+
on: [{ message: ["quer saber como funciona", "pede um orçamento"], repeat: "always" }],
|
|
141
|
+
steps: [{ id: "quem", collect: ["nome"] }],
|
|
142
|
+
}),
|
|
143
|
+
f.flow({
|
|
144
|
+
id: "concorrente",
|
|
145
|
+
name: "Falou de concorrente",
|
|
146
|
+
on: [{ mention: ["cita ou compara com um concorrente"], extract: { trecho: { type: "string" } } }],
|
|
147
|
+
steps: [{ id: "avisa", do: "avisar", with: { texto: 'Falou de concorrente: "{{input.trecho}}"' } }],
|
|
148
|
+
}),
|
|
149
|
+
f.flow({
|
|
150
|
+
id: "retomar",
|
|
151
|
+
name: "Retomar quem sumiu",
|
|
152
|
+
on: [{ silence: "24h", businessHours: true, if: ({ context }) => context.lead.dono === "ia" }],
|
|
153
|
+
steps: [{ id: "p1", prompt: "Retome a conversa de forma leve e pergunte se ainda faz sentido." }],
|
|
154
|
+
}),
|
|
155
|
+
f.flow({
|
|
156
|
+
id: "proposta",
|
|
157
|
+
name: "Acompanhar proposta",
|
|
158
|
+
on: [{ event: "entrou_na_etapa", after: "1h", if: { naEtapa: "proposta" } }],
|
|
159
|
+
steps: [{ id: "fala", prompt: "Pergunte se a proposta chegou bem." }],
|
|
160
|
+
}),
|
|
161
|
+
f.flow({
|
|
162
|
+
id: "boas-vindas",
|
|
163
|
+
name: "Boas-vindas",
|
|
164
|
+
steps: [{ id: "oi", say: "Oi! Vi que você se cadastrou. Posso ajudar em algo?" }],
|
|
165
|
+
}),
|
|
166
|
+
],
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const context: Ctx = { lead: { dono: "ia", etapa: "proposta" } };
|
|
170
|
+
const r = await agent.turn({ sessionId: "s1", context, start: { flow: "boas-vindas", key: "signup:456" } });
|
|
171
|
+
console.log(r.started[0]?.runId, r.messages[0]?.key); // 'boas-vindas#signup:456', 'boas-vindas#signup:456:oi:1'
|
|
172
|
+
console.log(r.schedule.map((s) => s.key)); // [ 'silence:retomar:s1:<ms>' ]: the assistant spoke, so the silence wake is armed
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## See also
|
|
176
|
+
|
|
177
|
+
- [Triggers guide](../guides/triggers.md) for choosing a trigger
|
|
178
|
+
- [Flow](flow.md) for `anchor`, `while` and the start order
|
|
179
|
+
- [Runs and waits](../concepts/runs-and-waits.md) for the full key table and the floor
|
|
180
|
+
- [Session](session.md) for `Run.trigger` and `Session.claims`
|