@falai/agent 3.4.5 → 4.0.0-alpha.10
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 +29 -378
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +113 -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 +573 -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 +149 -0
- package/dist/cjs/core/Prompt.js.map +1 -0
- package/dist/cjs/core/Runner.d.ts +171 -0
- package/dist/cjs/core/Runner.d.ts.map +1 -0
- package/dist/cjs/core/Runner.js +1158 -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 +373 -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 +357 -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 +11 -6
- 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/providers/ZaiProvider.d.ts +6 -4
- package/dist/cjs/providers/ZaiProvider.d.ts.map +1 -1
- package/dist/cjs/providers/ZaiProvider.js +6 -4
- package/dist/cjs/providers/ZaiProvider.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +163 -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/phrases.d.ts +25 -0
- package/dist/cjs/utils/phrases.d.ts.map +1 -0
- package/dist/cjs/utils/phrases.js +38 -0
- package/dist/cjs/utils/phrases.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 +22 -150
- package/dist/cjs/utils/template.d.ts.map +1 -1
- package/dist/cjs/utils/template.js +64 -359
- 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 +29 -378
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +116 -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 +567 -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 +139 -0
- package/dist/core/Prompt.js.map +1 -0
- package/dist/core/Runner.d.ts +171 -0
- package/dist/core/Runner.d.ts.map +1 -0
- package/dist/core/Runner.js +1154 -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 +369 -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 +353 -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 +11 -6
- 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/providers/ZaiProvider.d.ts +6 -4
- package/dist/providers/ZaiProvider.d.ts.map +1 -1
- package/dist/providers/ZaiProvider.js +6 -4
- package/dist/providers/ZaiProvider.js.map +1 -1
- package/dist/types/agent.d.ts +163 -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/phrases.d.ts +25 -0
- package/dist/utils/phrases.d.ts.map +1 -0
- package/dist/utils/phrases.js +35 -0
- package/dist/utils/phrases.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 +22 -150
- package/dist/utils/template.d.ts.map +1 -1
- package/dist/utils/template.js +61 -351
- 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 +170 -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 +457 -0
- package/docs/reference/actions-events-conditions.md +396 -0
- package/docs/reference/agent.md +248 -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 +200 -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 +181 -163
- 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 +149 -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 +11 -6
- package/src/core/Agent.ts +126 -1512
- package/src/core/CompactionEngine.ts +7 -4
- package/src/core/FlowSpec.ts +778 -0
- package/src/core/Migrate.ts +256 -0
- package/src/core/Prompt.ts +162 -0
- package/src/core/Runner.ts +1214 -0
- package/src/core/Speak.ts +460 -0
- package/src/core/Understand.ts +423 -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 +120 -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 +46 -13
- package/src/providers/ZaiProvider.ts +6 -4
- package/src/types/agent.ts +135 -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 +88 -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/phrases.ts +40 -0
- package/src/utils/schema.ts +145 -0
- package/src/utils/streamingMessage.ts +34 -4
- package/src/utils/template.ts +63 -418
- 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
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Flows from JSON"
|
|
3
|
+
description: "Store flows as rows, let an editor draw them or a model write them: FlowSpec is the same object as a Flow, with no functions in it."
|
|
4
|
+
type: guide
|
|
5
|
+
order: 11
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Flows from JSON
|
|
9
|
+
|
|
10
|
+
A flow written as JSON is a `FlowSpec`. `f.fromSpec(spec)` turns it into a typed flow, and the agent checks it when it is built.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { falai, GeminiProvider, type FlowSpec } from "@falai/agent";
|
|
14
|
+
|
|
15
|
+
const f = falai().fields({
|
|
16
|
+
nome: { type: "string", ask: "Pergunte o nome." },
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// A rule someone typed in a chat, stored as a row.
|
|
20
|
+
const concorrente: FlowSpec = {
|
|
21
|
+
id: "concorrente",
|
|
22
|
+
name: "Lead falou de concorrente",
|
|
23
|
+
on: [{ mention: ["o lead cita um concorrente"] }],
|
|
24
|
+
steps: [{ id: "tag", kind: "do", do: "add_tags", with: { tags: ["concorrente"] } }],
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const agent = f.agent({
|
|
28
|
+
name: "Ana",
|
|
29
|
+
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY ?? "", model: "gemini-2.5-flash" }),
|
|
30
|
+
actions: {
|
|
31
|
+
add_tags: f.action({ parameters: { tags: { type: "array", items: { type: "string" } } }, run: () => ({ ok: true }) }),
|
|
32
|
+
},
|
|
33
|
+
idle: { prompt: "Responda pela empresa, sem inventar preços." },
|
|
34
|
+
flows: [f.fromSpec(concorrente)],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const r = await agent.turn({ sessionId: "demo", message: "a Acme cobra metade disso" });
|
|
38
|
+
console.log(r.started.map((s) => s.flowId)); // [ 'concorrente' ]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The customer mentions a competitor. The understand call detects it, the `do` step tags the customer beside the conversation, and the idle speaker answers. The row is the flow: nothing here behaves differently from a flow written in code.
|
|
42
|
+
|
|
43
|
+
## The JSON form
|
|
44
|
+
|
|
45
|
+
A `FlowSpec` is a `Flow` with two differences: every step is flat and carries a `kind`, and every predicate is in its JSON form (`ConditionSpec`). There are no functions anywhere, so it survives `JSON.stringify`, a database column and a model's output.
|
|
46
|
+
|
|
47
|
+
```ts fragment
|
|
48
|
+
interface FlowSpec {
|
|
49
|
+
id: string;
|
|
50
|
+
name: string;
|
|
51
|
+
description?: string;
|
|
52
|
+
on?: TriggerSpec[]; // Trigger with `if` as a ConditionSpec
|
|
53
|
+
anchor?: string;
|
|
54
|
+
while?: ConditionSpec;
|
|
55
|
+
clearOnStart?: string[];
|
|
56
|
+
steps: StepSpec[]; // flat, each with `kind`
|
|
57
|
+
onEnd?: "end" | "stay" | "reset";
|
|
58
|
+
instructions?: InstructionSpec[]; // Instruction with `if` as a ConditionSpec
|
|
59
|
+
tools?: string[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type StepKind = "prompt" | "collect" | "say" | "do" | "wait" | "waitEvent" | "if";
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
How each step maps, from `src/core/FlowSpec.ts`:
|
|
66
|
+
|
|
67
|
+
| In code | `kind` | Rule |
|
|
68
|
+
|---|---|---|
|
|
69
|
+
| `{ prompt }` | `prompt` | a guideline alone |
|
|
70
|
+
| `{ collect, prompt? }` | `collect` | whenever there is a `collect` list, with or without a prompt beside it |
|
|
71
|
+
| `{ say, media?, once? }` | `say` | |
|
|
72
|
+
| `{ do, with?, onFail? }` | `do` | |
|
|
73
|
+
| `{ wait: "2d", else?, branches?, businessHours? }` | `wait` | `wait` is a duration string |
|
|
74
|
+
| `{ wait: { event, upTo? }, else? }` | `waitEvent` | `wait` is an object |
|
|
75
|
+
| `{ if, else? }` | `if` | `if` must be a `ConditionSpec` |
|
|
76
|
+
|
|
77
|
+
Everything else on a step (`id`, `label`, `then`, `ui`, `ask`, `maxAsks`, `branches`, `tools`, `instructions`) keeps its name. A `ConditionSpec` is an object where every key must hold: `equals` (fields equal these values), `known` (these fields are known), `silenced` (the host gate is closed), and any other key names one of the agent's conditions with its argument. See [conditions](./conditions.md).
|
|
78
|
+
|
|
79
|
+
## `fromSpec` and `toSpec`
|
|
80
|
+
|
|
81
|
+
`fromSpec(spec)` drops the `kind` from each step and returns a `Flow`. It treats `null` as "not set" for every optional value, because the generation schema below says null where the type says optional. The types it returns are a promise, not a proof: the field slugs and the action names inside came out of storage as plain strings, and `validateFlow` is what checks them. Use `f.fromSpec` from a bound toolkit so `C` and `D` are filled in.
|
|
82
|
+
|
|
83
|
+
`toSpec(flow)` goes the other way and never writes `null`, so `toSpec(fromSpec(spec))` is `spec` with its nulls dropped. It throws `FlowConfigurationError` when the flow carries a function predicate, because a function cannot be stored:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { falai, FlowConfigurationError, toSpec } from "@falai/agent";
|
|
87
|
+
|
|
88
|
+
const f = falai().fields({
|
|
89
|
+
nome: { type: "string", ask: "Pergunte o nome." },
|
|
90
|
+
confirmado: { type: "boolean", ask: "Pergunte se está tudo certo." },
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const ok = f.flow({
|
|
94
|
+
id: "triagem",
|
|
95
|
+
name: "Triagem",
|
|
96
|
+
on: [{ message: ["quer um orçamento"] }],
|
|
97
|
+
steps: [
|
|
98
|
+
{ id: "quem", prompt: "Descubra quem é.", collect: ["nome"] },
|
|
99
|
+
{ id: "confirma", collect: ["confirmado"] },
|
|
100
|
+
{ id: "gate", if: { equals: { confirmado: true } }, else: { step: "quem", clear: ["confirmado"] } },
|
|
101
|
+
],
|
|
102
|
+
});
|
|
103
|
+
console.log(JSON.stringify(toSpec(ok).steps[0])); // {"id":"quem","kind":"collect","collect":["nome"],"prompt":"Descubra quem é."}
|
|
104
|
+
|
|
105
|
+
const notStorable = f.flow({
|
|
106
|
+
id: "vip",
|
|
107
|
+
name: "VIP",
|
|
108
|
+
on: [{ message: ["quer atendimento vip"], if: ({ data }) => data.nome === "Ana" }],
|
|
109
|
+
steps: [{ id: "p", prompt: "Dê boas-vindas." }],
|
|
110
|
+
});
|
|
111
|
+
try {
|
|
112
|
+
toSpec(notStorable);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (error instanceof FlowConfigurationError) console.log(error.message);
|
|
115
|
+
// [FlowConfigurationError] flow "vip", trigger #1 if: is a function, which cannot be stored as JSON. Write it as a condition …
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## `validateFlow(spec, registries)`
|
|
120
|
+
|
|
121
|
+
`Registries` is the part of the agent's options a flow's names resolve against: `fields`, `actions`, `events`, `conditions`, `tools`. Run it when a row is saved, so the person who typed the rule sees the problem, not the customer. The agent runs it again on every flow at construction.
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { falai, FlowConfigurationError, validateFlow, type FlowSpec, type Registries } from "@falai/agent";
|
|
125
|
+
|
|
126
|
+
const f = falai().fields({
|
|
127
|
+
nome: { type: "string", ask: "Pergunte o nome." },
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const registries: Registries = {
|
|
131
|
+
fields: f.fields,
|
|
132
|
+
actions: {
|
|
133
|
+
notify: f.action({ parameters: { recipient: { type: "string" }, message: { type: "string" } }, run: () => ({ ok: true }) }),
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const spec: FlowSpec = {
|
|
138
|
+
id: "avisa",
|
|
139
|
+
name: "Avisa o dono",
|
|
140
|
+
on: [{ message: ["quer falar com uma pessoa"] }],
|
|
141
|
+
steps: [
|
|
142
|
+
{ id: "quem", kind: "collect", collect: ["nome"] },
|
|
143
|
+
{ id: "n", kind: "do", do: "notify", with: { recipient: "owner", message: "{{data.nome}} quer falar com alguém" } },
|
|
144
|
+
],
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
console.log(validateFlow(spec, registries).warnings); // []
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
validateFlow({ ...spec, steps: [{ id: "x", kind: "do", do: "send_email", with: {} }] }, registries);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
if (error instanceof FlowConfigurationError) console.log(error.message);
|
|
153
|
+
// [FlowConfigurationError] flow "avisa", step "x": unknown action "send_email". Register it in actions or fix the name.
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
It throws `FlowConfigurationError` on the first problem that would break at runtime:
|
|
158
|
+
|
|
159
|
+
- no flow id, no `steps` list, triggers with no steps
|
|
160
|
+
- a step with no id, the reserved id `"end"`, or a duplicate id
|
|
161
|
+
- an unknown field in `collect`, `ask`, `clearOnStart`, a `clear` list or an `equals`
|
|
162
|
+
- an `equals` value of the wrong type for its field (values are not coerced)
|
|
163
|
+
- an unknown action, or a `with` that misses a required parameter, gives one the wrong type, or names one the action does not have
|
|
164
|
+
- an unknown event in a trigger or a `wait`
|
|
165
|
+
- an unknown condition in any `if` or `while`; a malformed `equals`, `known` or `silenced`
|
|
166
|
+
- an unknown tool in `tools`
|
|
167
|
+
- a duration that does not parse, in `wait`, `upTo`, `silence`, `after` or `repeat.cooldown`
|
|
168
|
+
- a `then`, `else`, `onFail` or branch pointing at a step that does not exist
|
|
169
|
+
- a branch with neither `when` nor `if`
|
|
170
|
+
- an `if` step that jumps backward with no `else`
|
|
171
|
+
|
|
172
|
+
And it returns warnings for what runs but probably not as intended:
|
|
173
|
+
|
|
174
|
+
- a `then`, `else` or branch that jumps back to an earlier step without `clear`: the fields collected since stay known and those steps skip
|
|
175
|
+
- a `collect` step with no `prompt` and no `ask` on any of its fields: the model has nothing to go on
|
|
176
|
+
|
|
177
|
+
## Letting a model write a flow
|
|
178
|
+
|
|
179
|
+
`flowSpecSchema(registries)` is the JSON schema of a `FlowSpec` for this agent: field slugs, action names with their parameter schemas, event names and condition names are enums. Every object is closed and every property required, with `null` standing for "not set", so Gemini and OpenAI strict mode both accept it as a response schema. Steps are an `anyOf` discriminated by `kind`, one `do` variant per action.
|
|
180
|
+
|
|
181
|
+
Use it as the response schema of your own generation call, then validate. The schema shapes the answer; `validateFlow` proves it.
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { falai, flowSpecSchema, GeminiProvider, validateFlow, type FlowSpec, type Registries, type StructuredSchema } from "@falai/agent";
|
|
185
|
+
|
|
186
|
+
const f = falai().fields({
|
|
187
|
+
nome: { type: "string", ask: "Pergunte o nome." },
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const registries: Registries = {
|
|
191
|
+
fields: f.fields,
|
|
192
|
+
actions: {
|
|
193
|
+
notify: f.action({ parameters: { recipient: { type: "string" }, message: { type: "string" } }, run: () => ({ ok: true }) }),
|
|
194
|
+
add_tags: f.action({ parameters: { tags: { type: "array", items: { type: "string" } } }, run: () => ({ ok: true }) }),
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// Your call to a model, with any SDK. The schema is plain JSON Schema.
|
|
199
|
+
declare function generateJson<T>(prompt: string, schema: StructuredSchema): Promise<T>;
|
|
200
|
+
|
|
201
|
+
const spec = await generateJson<FlowSpec>(
|
|
202
|
+
"Write a flow, as JSON, for this request: 'quando o lead pedir para falar com uma pessoa, avise o dono e marque a tag humano'. Write the texts in Brazilian Portuguese.",
|
|
203
|
+
flowSpecSchema(registries),
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
const { warnings } = validateFlow(spec, registries); // throws FlowConfigurationError when the model wrote something the agent cannot run
|
|
207
|
+
console.log(spec.id, warnings);
|
|
208
|
+
|
|
209
|
+
const provider = new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY ?? "", model: "gemini-2.5-flash" });
|
|
210
|
+
const agent = f.agent({ name: "Ana", provider, ...registries, flows: [f.fromSpec(spec)] });
|
|
211
|
+
console.log(agent.options.flows?.length); // 1
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
The agent's own provider serves as the generator too: `provider.generateMessage<undefined, FlowSpec>({ prompt, history: [], context: undefined, parameters: { jsonSchema: flowSpecSchema(registries), schemaName: "flow" } })` and read `.structured`, as `examples/09-flows-from-json.ts` does.
|
|
215
|
+
|
|
216
|
+
Left out of the schema on purpose, because a model should not write them: `ui`, `tools`, step-level `instructions` and `ask`, a mention trigger's `extract`, and the `input` of `{ flow }`. A variant with an empty registry is left out too: with no actions there is no `do` step, with no events no `event` trigger and no `waitEvent`. Condition arguments are typed loosely (string, number, boolean or list of strings) because conditions carry no argument schema.
|
|
217
|
+
|
|
218
|
+
## Rows into the agent
|
|
219
|
+
|
|
220
|
+
The agent is immutable: `f.agent({ flows })` reads the flows once. Load the rows, `f.fromSpec` each one, build the agent, and serve every session with that one instance. When a row changes, build a new agent. A row that no longer validates fails the build with the message above, which is the moment to show it in the editor rather than at the customer's next message.
|
|
221
|
+
|
|
222
|
+
A run in a stored session that points at a flow you removed does not crash: it ends with `code: 'flow-gone'`. See [outcomes](../reference/outcomes.md).
|
|
223
|
+
|
|
224
|
+
See [the flow spec reference](../reference/flow-spec.md) for every type, and [triggers](./triggers.md) for what a `mention` with `extract` can do.
|
|
@@ -1,219 +1,183 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: "Instructions"
|
|
3
|
-
description: "
|
|
3
|
+
description: "Rules the model follows while it speaks: must, never and should, at agent, flow or step scope, switched on by the model or by code."
|
|
4
4
|
type: guide
|
|
5
|
-
order:
|
|
5
|
+
order: 6
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
# Instructions
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
An instruction is one sentence the model reads before it writes a reply. You put it on the agent, on a flow or on a step, and it applies while that scope is speaking.
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
```ts
|
|
13
|
+
import { falai, GeminiProvider } from "@falai/agent";
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
const f = falai().fields({});
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
const agent = f.agent({
|
|
18
|
+
name: "Bia",
|
|
19
|
+
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY ?? "", model: "gemini-2.5-flash" }),
|
|
20
|
+
instructions: [{ kind: "never", prompt: "Nunca invente preços ou prazos." }],
|
|
21
|
+
idle: { prompt: "Responda pela Loja Azul." },
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const r = await agent.turn({ sessionId: "demo", message: "quanto custa o plano pro?" });
|
|
25
|
+
console.log(r.messages[0]?.text);
|
|
26
|
+
```
|
|
17
27
|
|
|
18
|
-
|
|
19
|
-
import type { Instruction } from "@falai/agent";
|
|
28
|
+
The rule reaches every reply the agent phrases. Here no flow is running, so the reply comes from `idle` — the speaker that answers when nothing holds the floor. It costs no extra model call: it is text inside the speak call the turn already makes.
|
|
20
29
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
## The shape
|
|
31
|
+
|
|
32
|
+
`Instruction` is defined in `src/types/flow.ts`:
|
|
33
|
+
|
|
34
|
+
```ts fragment
|
|
35
|
+
interface Instruction<C, D> {
|
|
36
|
+
id?: string;
|
|
37
|
+
kind?: "must" | "never" | "should"; // default "should"
|
|
38
|
+
when?: string | string[]; // judged by the model, inside the speak call
|
|
39
|
+
if?: Pred<C, D>; // judged by code, before the call
|
|
40
|
+
prompt: Template; // the rule; {{data.x}}, {{context.x}} and {{input.x}} are filled in
|
|
41
|
+
}
|
|
27
42
|
```
|
|
28
43
|
|
|
29
|
-
|
|
44
|
+
| Field | What it does |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `kind` | `must` is always done, `never` is a prohibition, `should` is a nudge. Default `should`. |
|
|
47
|
+
| `when` | A condition in words. The model decides whether it holds, from the conversation. One string or a list; any one of the list is enough. |
|
|
48
|
+
| `if` | A code predicate: a function of `PredCtx`, or its JSON form (`{ equals }`, `{ known }`, `{ silenced }`, or a named condition). Free. |
|
|
49
|
+
| `prompt` | The rule itself. Templates render against the collected data, the host context and the run's input. |
|
|
50
|
+
| `id` | Yours. The framework carries it and never reads it. |
|
|
30
51
|
|
|
31
|
-
|
|
52
|
+
An instruction with neither `when` nor `if` always applies.
|
|
32
53
|
|
|
33
|
-
|
|
54
|
+
## Three scopes and the idle speaker
|
|
34
55
|
|
|
35
|
-
|
|
|
36
|
-
|
|
37
|
-
| `
|
|
38
|
-
| `
|
|
39
|
-
| `
|
|
56
|
+
| Where | Applies while |
|
|
57
|
+
|---|---|
|
|
58
|
+
| `agent.instructions` | any talk step speaks, and the idle speaker answers |
|
|
59
|
+
| `flow.instructions` | a talk step of that flow speaks |
|
|
60
|
+
| `step.instructions` | that talk step speaks |
|
|
61
|
+
| `idle.instructions` | the idle speaker answers (no run holds the floor) |
|
|
40
62
|
|
|
41
|
-
|
|
63
|
+
The speak call gets them in this order: agent, then flow, then step. For the idle speaker: agent, then idle. Only talk steps (`prompt` / `collect`) and the idle speaker phrase text, so only they carry instructions; a `say` step goes out verbatim and a `do` step never speaks.
|
|
42
64
|
|
|
43
|
-
|
|
65
|
+
```ts
|
|
66
|
+
import { falai, GeminiProvider } from "@falai/agent";
|
|
44
67
|
|
|
45
|
-
|
|
68
|
+
interface Ctx {
|
|
69
|
+
plano: "gratis" | "pro";
|
|
70
|
+
horaLocal: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const f = falai<Ctx>().fields({
|
|
74
|
+
duvida: { type: "string", ask: "Pergunte qual é a dúvida, em uma frase." },
|
|
75
|
+
});
|
|
46
76
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
77
|
+
const agent = f.agent({
|
|
78
|
+
name: "Bia",
|
|
79
|
+
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY ?? "", model: "gemini-2.5-flash" }),
|
|
80
|
+
instructions: [
|
|
81
|
+
{ kind: "never", prompt: "Nunca invente preços ou prazos." },
|
|
82
|
+
{ kind: "must", when: "a pessoa está irritada", prompt: "Reconheça o problema antes de explicar qualquer coisa." },
|
|
83
|
+
{ kind: "should", if: ({ context }) => context.horaLocal >= 18 || context.horaLocal < 9, prompt: "Avise que o suporte humano volta às 9h." },
|
|
84
|
+
],
|
|
50
85
|
flows: [
|
|
51
|
-
{
|
|
52
|
-
|
|
53
|
-
|
|
86
|
+
f.flow({
|
|
87
|
+
id: "duvidas",
|
|
88
|
+
name: "Dúvidas",
|
|
89
|
+
on: [{ message: [] }],
|
|
90
|
+
instructions: [{ kind: "should", prompt: "Responda em até três frases." }],
|
|
54
91
|
steps: [
|
|
92
|
+
{ id: "qual", collect: ["duvida"] },
|
|
55
93
|
{
|
|
56
|
-
id: "
|
|
57
|
-
|
|
94
|
+
id: "resposta",
|
|
95
|
+
prompt: "Responda a dúvida.",
|
|
96
|
+
instructions: [{ kind: "must", prompt: "Termine perguntando se ficou claro." }],
|
|
58
97
|
},
|
|
59
98
|
],
|
|
60
|
-
},
|
|
99
|
+
}),
|
|
61
100
|
],
|
|
62
101
|
});
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Reach narrows as you nest. Agent-scope instructions render on every turn for every flow. Flow-scope instructions render only while that flow is active. Step-scope instructions render only while that step is current. Conditions apply on top: `if` removes an instruction locally when its predicate fails, while `when` is rendered with the instruction so the model can decide whether the natural-language condition applies.
|
|
66
|
-
|
|
67
|
-
The composer renders the resolved set under a single `## Instructions` heading and tags each line with a scope caption so the model can read both *what* and *where from* in one pass:
|
|
68
|
-
|
|
69
|
-
| Scope | Caption | When it appears |
|
|
70
|
-
|-------|---------|-----------------|
|
|
71
|
-
| Agent | `[Always]` | Every turn. |
|
|
72
|
-
| Flow | `[In: <FlowTitle>]` | While the named flow is active. |
|
|
73
|
-
| Step | `[Step: <stepId>]` | While the named step is current. |
|
|
74
|
-
|
|
75
|
-
## Rendering format
|
|
76
|
-
|
|
77
|
-
Each eligible instruction lands in the prompt as a single bullet:
|
|
78
|
-
|
|
79
|
-
```
|
|
80
|
-
- [<kind>] [<scope>] <prompt> (apply only when: <when-clause> OR <when-clause>; do not apply when: <exclusion-clause>)
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
The condition suffix is omitted when `when` is not set. A composed block looks like this:
|
|
84
|
-
|
|
85
|
-
```
|
|
86
|
-
## Instructions
|
|
87
102
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
- [should] [In: Booking] Offer to compare two options before committing. (apply only when: the user is comparing hotel options)
|
|
91
|
-
- [must] [Step: payment] If the card is declined, never retry without confirmation.
|
|
103
|
+
const r = await agent.turn({ sessionId: "demo", context: { plano: "gratis", horaLocal: 21 }, message: "quanto custa?" });
|
|
104
|
+
console.log(r.messages[0]?.text);
|
|
92
105
|
```
|
|
93
106
|
|
|
94
|
-
|
|
107
|
+
On this turn the step `qual` speaks. Its prompt carries all three agent rules: the `never`, the `must` with its `when` appended for the model to judge, and the `should`, because it is 21h. The flow's three-sentence rule goes in beside them. The `resposta` step's rule waits for its own step.
|
|
95
108
|
|
|
96
|
-
## `
|
|
109
|
+
## `if` runs first, in code
|
|
97
110
|
|
|
98
|
-
|
|
111
|
+
Before the speak call, the framework drops every instruction whose `if` is false. This happens in code, with no model call. The predicate sees a `PredCtx`:
|
|
99
112
|
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
113
|
+
```ts fragment
|
|
114
|
+
interface PredCtx<C, D, P> {
|
|
115
|
+
context: C; // the host context passed to turn()
|
|
116
|
+
data: Partial<D>; // the collected data so far
|
|
117
|
+
input: P; // the speaking run's input; undefined for the idle speaker
|
|
118
|
+
run?: Run; // the speaking run; absent for the idle speaker
|
|
119
|
+
silenced?: string; // the host's reason the assistant cannot speak, when it cannot
|
|
120
|
+
now: Date;
|
|
107
121
|
}
|
|
108
|
-
// global → ins_validate_dates
|
|
109
|
-
// global → ins_no_unquoted_prices
|
|
110
|
-
// flow:Booking → ins_offer_two_options
|
|
111
122
|
```
|
|
112
123
|
|
|
113
|
-
|
|
124
|
+
The JSON form works too, and it is the only form a flow stored as JSON can carry (see [flows from JSON](./flows-from-json.md)):
|
|
114
125
|
|
|
115
|
-
|
|
126
|
+
```ts
|
|
127
|
+
import { falai, GeminiProvider } from "@falai/agent";
|
|
116
128
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
history: [{ role: "user", content: "I want to book a room." }],
|
|
120
|
-
})) {
|
|
121
|
-
if (chunk.done) {
|
|
122
|
-
console.log("rendered:", chunk.appliedInstructions);
|
|
123
|
-
}
|
|
129
|
+
interface Ctx {
|
|
130
|
+
lead: { tags: string[] };
|
|
124
131
|
}
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
`appliedInstructions` is empty on intermediate chunks and populated only when `done: true`.
|
|
128
|
-
|
|
129
|
-
## Recipe: agent-level absolutes
|
|
130
|
-
|
|
131
|
-
Two house rules every flow must respect:
|
|
132
132
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
name: "BookingBot",
|
|
136
|
-
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY! }),
|
|
137
|
-
instructions: [
|
|
138
|
-
{ id: "ins_validate_dates", kind: "must", prompt: "Validate dates are in the future before booking." },
|
|
139
|
-
{ id: "ins_no_unquoted", kind: "never", prompt: "Promise rates you have not looked up." },
|
|
140
|
-
],
|
|
141
|
-
flows: [/* ... */],
|
|
133
|
+
const f = falai<Ctx>().fields({
|
|
134
|
+
nome: { type: "string", ask: "Pergunte o nome." },
|
|
142
135
|
});
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
These render with caption `[Always]` on every turn, regardless of which flow is active.
|
|
146
|
-
|
|
147
|
-
## Recipe: flow-level conditional
|
|
148
|
-
|
|
149
|
-
A flow-scope nudge that only fires when the user is comparison shopping:
|
|
150
136
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
137
|
+
const agent = f.agent({
|
|
138
|
+
name: "Ana",
|
|
139
|
+
provider: new GeminiProvider({ apiKey: process.env.GEMINI_API_KEY ?? "", model: "gemini-2.5-flash" }),
|
|
140
|
+
conditions: {
|
|
141
|
+
tagsAny: f.condition((ctx, tags: string[]) => tags.some((t) => ctx.context.lead.tags.includes(t))),
|
|
142
|
+
},
|
|
154
143
|
instructions: [
|
|
155
|
-
{
|
|
156
|
-
|
|
157
|
-
kind: "should",
|
|
158
|
-
when: "User is comparing options or asking which is better",
|
|
159
|
-
prompt: "Offer to compare two options side by side before recommending one.",
|
|
160
|
-
},
|
|
144
|
+
{ kind: "should", if: { tagsAny: ["vip"] }, prompt: "Ofereça falar com o gerente." },
|
|
145
|
+
{ kind: "should", if: { known: ["nome"] }, prompt: "Chame a pessoa pelo nome." },
|
|
161
146
|
],
|
|
162
|
-
|
|
163
|
-
};
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
While `Booking` is active, the line renders with caption `[In: Booking]` only on turns where the AI condition resolves true. On other turns the entry stays out of the prompt and out of `appliedInstructions`.
|
|
167
|
-
|
|
168
|
-
## Recipe: step-level reminder
|
|
169
|
-
|
|
170
|
-
A `must` that scopes to a specific step, gated by a code condition:
|
|
147
|
+
idle: { prompt: "Responda pela empresa." },
|
|
148
|
+
});
|
|
171
149
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
id: "payment",
|
|
175
|
-
prompt: "Take payment.",
|
|
176
|
-
instructions: [
|
|
177
|
-
{
|
|
178
|
-
id: "ins_no_retry_on_decline",
|
|
179
|
-
kind: "must",
|
|
180
|
-
if: (ctx) => ctx.data.lastChargeStatus === "declined",
|
|
181
|
-
prompt: "If the card is declined, never retry without explicit user confirmation.",
|
|
182
|
-
},
|
|
183
|
-
],
|
|
184
|
-
};
|
|
150
|
+
const r = await agent.turn({ sessionId: "demo", context: { lead: { tags: ["vip"] } }, message: "oi" });
|
|
151
|
+
console.log(r.llmCalls); // 1
|
|
185
152
|
```
|
|
186
153
|
|
|
187
|
-
|
|
154
|
+
A named condition the agent does not register fails with a `FlowConfigurationError`. On a flow or a step it fails when you build the agent. On the agent itself it fails on the first turn that phrases a reply.
|
|
188
155
|
|
|
189
|
-
|
|
156
|
+
Use `if` for anything the code already knows: the plan, the hour, a tag, a field being known. Use `when` only for what needs the conversation to judge: tone, intent, a topic. One form never fires here: `{ silenced: true }` is always false on an instruction, because a silenced turn phrases nothing and reads no instruction at all. Put it on a trigger, an `if` step or a branch instead — those still run while the gate is closed.
|
|
190
157
|
|
|
191
|
-
|
|
158
|
+
## `when` goes to the model as text
|
|
192
159
|
|
|
193
|
-
|
|
194
|
-
import { test, expect } from "bun:test";
|
|
160
|
+
An instruction that survives `if` is rendered into one section of the speak prompt:
|
|
195
161
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
162
|
+
```text
|
|
163
|
+
## Instructions
|
|
164
|
+
- [never] [Always] Nunca invente preços ou prazos.
|
|
165
|
+
- [must] [Always] Reconheça o problema antes de explicar qualquer coisa. (apply only when: a pessoa está irritada)
|
|
166
|
+
- [should] [Always] Avise que o suporte humano volta às 9h.
|
|
167
|
+
- [should] [Always] Responda em até três frases.
|
|
168
|
+
```
|
|
202
169
|
|
|
203
|
-
|
|
204
|
-
const response = await agent.respond({
|
|
205
|
-
history: [{ role: "user", content: "Try again." }],
|
|
206
|
-
});
|
|
170
|
+
One line per instruction: the kind in brackets, the rule, and `when` appended as `(apply only when: …)`. A list of `when` strings is joined with ` OR `. Templates are filled in before rendering; an instruction whose prompt renders to nothing is left out.
|
|
207
171
|
|
|
208
|
-
|
|
209
|
-
expect(ids).toContain("ins_no_retry_on_decline");
|
|
210
|
-
});
|
|
211
|
-
```
|
|
172
|
+
Two consequences:
|
|
212
173
|
|
|
213
|
-
|
|
174
|
+
- Instructions reach the speak call only. The understand call (routing, mentions, extraction) never sees them. A turn that phrases nothing, because it is silenced or because a `say` step already answered, renders none of them and spends nothing on them.
|
|
175
|
+
- The framework does not report which `when` clauses the model judged true. If you need to see what reached the prompt, drive the agent with a scripted provider and read the prompt it received (see [testing](./testing.md)).
|
|
214
176
|
|
|
215
|
-
## Where
|
|
177
|
+
## Where instructions are not the tool
|
|
216
178
|
|
|
217
|
-
|
|
179
|
+
- A fixed sentence that must go out word for word is a `say` step, not a `must`.
|
|
180
|
+
- A decision the code can make is an `if` step or a branch, not a `should`. Movement belongs to the flow; see [flow control](./flow-control.md).
|
|
181
|
+
- Who the agent is and what it wants live in `persona` and `goal` on the agent; facts it should know live in `knowledgeBase`. Instructions are for how it behaves in a situation.
|
|
218
182
|
|
|
219
|
-
|
|
183
|
+
See [the instruction reference](../reference/instruction.md) for the type as exported, and [conditions](./conditions.md) for `when` versus `if` across triggers, branches and steps.
|