@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.
Files changed (847) hide show
  1. package/README.md +41 -34
  2. package/dist/cjs/core/Agent.d.ts +22 -378
  3. package/dist/cjs/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/core/Agent.js +104 -1178
  5. package/dist/cjs/core/Agent.js.map +1 -1
  6. package/dist/cjs/core/CompactionEngine.d.ts.map +1 -1
  7. package/dist/cjs/core/CompactionEngine.js +5 -3
  8. package/dist/cjs/core/CompactionEngine.js.map +1 -1
  9. package/dist/cjs/core/FlowSpec.d.ts +136 -0
  10. package/dist/cjs/core/FlowSpec.d.ts.map +1 -0
  11. package/dist/cjs/core/FlowSpec.js +522 -0
  12. package/dist/cjs/core/FlowSpec.js.map +1 -0
  13. package/dist/cjs/core/Migrate.d.ts +38 -0
  14. package/dist/cjs/core/Migrate.d.ts.map +1 -0
  15. package/dist/cjs/core/Migrate.js +270 -0
  16. package/dist/cjs/core/Migrate.js.map +1 -0
  17. package/dist/cjs/core/Prompt.d.ts +54 -0
  18. package/dist/cjs/core/Prompt.d.ts.map +1 -0
  19. package/dist/cjs/core/Prompt.js +143 -0
  20. package/dist/cjs/core/Prompt.js.map +1 -0
  21. package/dist/cjs/core/Runner.d.ts +160 -0
  22. package/dist/cjs/core/Runner.d.ts.map +1 -0
  23. package/dist/cjs/core/Runner.js +1131 -0
  24. package/dist/cjs/core/Runner.js.map +1 -0
  25. package/dist/cjs/core/Speak.d.ts +37 -0
  26. package/dist/cjs/core/Speak.d.ts.map +1 -0
  27. package/dist/cjs/core/Speak.js +364 -0
  28. package/dist/cjs/core/Speak.js.map +1 -0
  29. package/dist/cjs/core/Understand.d.ts +28 -0
  30. package/dist/cjs/core/Understand.d.ts.map +1 -0
  31. package/dist/cjs/core/Understand.js +353 -0
  32. package/dist/cjs/core/Understand.js.map +1 -0
  33. package/dist/cjs/core/contracts.d.ts +122 -0
  34. package/dist/cjs/core/contracts.d.ts.map +1 -0
  35. package/dist/cjs/core/contracts.js +11 -0
  36. package/dist/cjs/core/contracts.js.map +1 -0
  37. package/dist/cjs/core/falai.d.ts +57 -0
  38. package/dist/cjs/core/falai.d.ts.map +1 -0
  39. package/dist/cjs/core/falai.js +43 -0
  40. package/dist/cjs/core/falai.js.map +1 -0
  41. package/dist/cjs/core/predicate.d.ts +9 -0
  42. package/dist/cjs/core/predicate.d.ts.map +1 -0
  43. package/dist/cjs/core/predicate.js +58 -0
  44. package/dist/cjs/core/predicate.js.map +1 -0
  45. package/dist/cjs/index.d.ts +26 -31
  46. package/dist/cjs/index.d.ts.map +1 -1
  47. package/dist/cjs/index.js +46 -68
  48. package/dist/cjs/index.js.map +1 -1
  49. package/dist/cjs/persistence/MemoryStore.d.ts +15 -0
  50. package/dist/cjs/persistence/MemoryStore.d.ts.map +1 -0
  51. package/dist/cjs/persistence/MemoryStore.js +39 -0
  52. package/dist/cjs/persistence/MemoryStore.js.map +1 -0
  53. package/dist/cjs/persistence/MongoStore.d.ts +42 -0
  54. package/dist/cjs/persistence/MongoStore.d.ts.map +1 -0
  55. package/dist/cjs/persistence/MongoStore.js +60 -0
  56. package/dist/cjs/persistence/MongoStore.js.map +1 -0
  57. package/dist/cjs/persistence/OpenSearchStore.d.ts +86 -0
  58. package/dist/cjs/persistence/OpenSearchStore.d.ts.map +1 -0
  59. package/dist/cjs/persistence/OpenSearchStore.js +120 -0
  60. package/dist/cjs/persistence/OpenSearchStore.js.map +1 -0
  61. package/dist/cjs/persistence/PostgresStore.d.ts +41 -0
  62. package/dist/cjs/persistence/PostgresStore.d.ts.map +1 -0
  63. package/dist/cjs/persistence/PostgresStore.js +58 -0
  64. package/dist/cjs/persistence/PostgresStore.js.map +1 -0
  65. package/dist/cjs/persistence/PrismaStore.d.ts +65 -0
  66. package/dist/cjs/persistence/PrismaStore.d.ts.map +1 -0
  67. package/dist/cjs/persistence/PrismaStore.js +95 -0
  68. package/dist/cjs/persistence/PrismaStore.js.map +1 -0
  69. package/dist/cjs/persistence/RedisStore.d.ts +34 -0
  70. package/dist/cjs/persistence/RedisStore.d.ts.map +1 -0
  71. package/dist/cjs/persistence/RedisStore.js +61 -0
  72. package/dist/cjs/persistence/RedisStore.js.map +1 -0
  73. package/dist/cjs/persistence/SQLiteStore.d.ts +45 -0
  74. package/dist/cjs/persistence/SQLiteStore.d.ts.map +1 -0
  75. package/dist/cjs/persistence/SQLiteStore.js +74 -0
  76. package/dist/cjs/persistence/SQLiteStore.js.map +1 -0
  77. package/dist/cjs/persistence/sessionRow.d.ts +14 -0
  78. package/dist/cjs/persistence/sessionRow.d.ts.map +1 -0
  79. package/dist/cjs/persistence/sessionRow.js +50 -0
  80. package/dist/cjs/persistence/sessionRow.js.map +1 -0
  81. package/dist/cjs/providers/DeepSeekProvider.d.ts.map +1 -1
  82. package/dist/cjs/providers/DeepSeekProvider.js +8 -3
  83. package/dist/cjs/providers/DeepSeekProvider.js.map +1 -1
  84. package/dist/cjs/providers/GeminiProvider.d.ts +4 -3
  85. package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
  86. package/dist/cjs/providers/GeminiProvider.js +4 -3
  87. package/dist/cjs/providers/GeminiProvider.js.map +1 -1
  88. package/dist/cjs/providers/OpenAICompatibleProvider.d.ts +4 -0
  89. package/dist/cjs/providers/OpenAICompatibleProvider.d.ts.map +1 -1
  90. package/dist/cjs/providers/OpenAICompatibleProvider.js +2 -0
  91. package/dist/cjs/providers/OpenAICompatibleProvider.js.map +1 -1
  92. package/dist/cjs/providers/OpenRouterProvider.d.ts.map +1 -1
  93. package/dist/cjs/providers/OpenRouterProvider.js +2 -4
  94. package/dist/cjs/providers/OpenRouterProvider.js.map +1 -1
  95. package/dist/cjs/providers/ProviderAdapter.d.ts +1 -1
  96. package/dist/cjs/providers/ProviderAdapter.d.ts.map +1 -1
  97. package/dist/cjs/providers/ProviderAdapter.js +33 -10
  98. package/dist/cjs/providers/ProviderAdapter.js.map +1 -1
  99. package/dist/cjs/types/agent.d.ts +153 -383
  100. package/dist/cjs/types/agent.d.ts.map +1 -1
  101. package/dist/cjs/types/agent.js +1 -1
  102. package/dist/cjs/types/ai.d.ts +32 -1
  103. package/dist/cjs/types/ai.d.ts.map +1 -1
  104. package/dist/cjs/types/compaction.d.ts +3 -1
  105. package/dist/cjs/types/compaction.d.ts.map +1 -1
  106. package/dist/cjs/types/errors.d.ts +9 -12
  107. package/dist/cjs/types/errors.d.ts.map +1 -1
  108. package/dist/cjs/types/errors.js +14 -17
  109. package/dist/cjs/types/errors.js.map +1 -1
  110. package/dist/cjs/types/flow.d.ts +265 -513
  111. package/dist/cjs/types/flow.d.ts.map +1 -1
  112. package/dist/cjs/types/flow.js +7 -1
  113. package/dist/cjs/types/flow.js.map +1 -1
  114. package/dist/cjs/types/history.d.ts +7 -18
  115. package/dist/cjs/types/history.d.ts.map +1 -1
  116. package/dist/cjs/types/history.js.map +1 -1
  117. package/dist/cjs/types/index.d.ts +9 -15
  118. package/dist/cjs/types/index.d.ts.map +1 -1
  119. package/dist/cjs/types/index.js +4 -14
  120. package/dist/cjs/types/index.js.map +1 -1
  121. package/dist/cjs/types/session.d.ts +94 -64
  122. package/dist/cjs/types/session.d.ts.map +1 -1
  123. package/dist/cjs/types/session.js +5 -1
  124. package/dist/cjs/types/session.js.map +1 -1
  125. package/dist/cjs/types/tool.d.ts +37 -207
  126. package/dist/cjs/types/tool.d.ts.map +1 -1
  127. package/dist/cjs/types/tool.js +5 -14
  128. package/dist/cjs/types/tool.js.map +1 -1
  129. package/dist/cjs/utils/clock.d.ts +28 -0
  130. package/dist/cjs/utils/clock.d.ts.map +1 -0
  131. package/dist/cjs/utils/clock.js +64 -0
  132. package/dist/cjs/utils/clock.js.map +1 -0
  133. package/dist/cjs/utils/duration.d.ts +11 -0
  134. package/dist/cjs/utils/duration.d.ts.map +1 -0
  135. package/dist/cjs/utils/duration.js +31 -0
  136. package/dist/cjs/utils/duration.js.map +1 -0
  137. package/dist/cjs/utils/history.d.ts +4 -1
  138. package/dist/cjs/utils/history.d.ts.map +1 -1
  139. package/dist/cjs/utils/history.js +2 -2
  140. package/dist/cjs/utils/history.js.map +1 -1
  141. package/dist/cjs/utils/index.d.ts +4 -10
  142. package/dist/cjs/utils/index.d.ts.map +1 -1
  143. package/dist/cjs/utils/index.js +14 -61
  144. package/dist/cjs/utils/index.js.map +1 -1
  145. package/dist/cjs/utils/json.d.ts +2 -0
  146. package/dist/cjs/utils/json.d.ts.map +1 -1
  147. package/dist/cjs/utils/json.js +5 -0
  148. package/dist/cjs/utils/json.js.map +1 -1
  149. package/dist/cjs/utils/outcomes.d.ts +48 -0
  150. package/dist/cjs/utils/outcomes.d.ts.map +1 -0
  151. package/dist/cjs/utils/outcomes.js +51 -0
  152. package/dist/cjs/utils/outcomes.js.map +1 -0
  153. package/dist/cjs/utils/schema.d.ts +50 -0
  154. package/dist/cjs/utils/schema.d.ts.map +1 -0
  155. package/dist/cjs/utils/schema.js +138 -0
  156. package/dist/cjs/utils/schema.js.map +1 -0
  157. package/dist/cjs/utils/streamingMessage.d.ts +3 -2
  158. package/dist/cjs/utils/streamingMessage.d.ts.map +1 -1
  159. package/dist/cjs/utils/streamingMessage.js +38 -4
  160. package/dist/cjs/utils/streamingMessage.js.map +1 -1
  161. package/dist/cjs/utils/template.d.ts +13 -149
  162. package/dist/cjs/utils/template.d.ts.map +1 -1
  163. package/dist/cjs/utils/template.js +31 -363
  164. package/dist/cjs/utils/template.js.map +1 -1
  165. package/dist/cjs/utils/usage.d.ts +19 -0
  166. package/dist/cjs/utils/usage.d.ts.map +1 -0
  167. package/dist/cjs/utils/usage.js +35 -0
  168. package/dist/cjs/utils/usage.js.map +1 -0
  169. package/dist/core/Agent.d.ts +22 -378
  170. package/dist/core/Agent.d.ts.map +1 -1
  171. package/dist/core/Agent.js +107 -1181
  172. package/dist/core/Agent.js.map +1 -1
  173. package/dist/core/CompactionEngine.d.ts.map +1 -1
  174. package/dist/core/CompactionEngine.js +5 -3
  175. package/dist/core/CompactionEngine.js.map +1 -1
  176. package/dist/core/FlowSpec.d.ts +136 -0
  177. package/dist/core/FlowSpec.d.ts.map +1 -0
  178. package/dist/core/FlowSpec.js +516 -0
  179. package/dist/core/FlowSpec.js.map +1 -0
  180. package/dist/core/Migrate.d.ts +38 -0
  181. package/dist/core/Migrate.d.ts.map +1 -0
  182. package/dist/core/Migrate.js +264 -0
  183. package/dist/core/Migrate.js.map +1 -0
  184. package/dist/core/Prompt.d.ts +54 -0
  185. package/dist/core/Prompt.d.ts.map +1 -0
  186. package/dist/core/Prompt.js +133 -0
  187. package/dist/core/Prompt.js.map +1 -0
  188. package/dist/core/Runner.d.ts +160 -0
  189. package/dist/core/Runner.d.ts.map +1 -0
  190. package/dist/core/Runner.js +1127 -0
  191. package/dist/core/Runner.js.map +1 -0
  192. package/dist/core/Speak.d.ts +37 -0
  193. package/dist/core/Speak.d.ts.map +1 -0
  194. package/dist/core/Speak.js +360 -0
  195. package/dist/core/Speak.js.map +1 -0
  196. package/dist/core/Understand.d.ts +28 -0
  197. package/dist/core/Understand.d.ts.map +1 -0
  198. package/dist/core/Understand.js +349 -0
  199. package/dist/core/Understand.js.map +1 -0
  200. package/dist/core/contracts.d.ts +122 -0
  201. package/dist/core/contracts.d.ts.map +1 -0
  202. package/dist/core/contracts.js +10 -0
  203. package/dist/core/contracts.js.map +1 -0
  204. package/dist/core/falai.d.ts +57 -0
  205. package/dist/core/falai.d.ts.map +1 -0
  206. package/dist/core/falai.js +40 -0
  207. package/dist/core/falai.js.map +1 -0
  208. package/dist/core/predicate.d.ts +9 -0
  209. package/dist/core/predicate.d.ts.map +1 -0
  210. package/dist/core/predicate.js +54 -0
  211. package/dist/core/predicate.js.map +1 -0
  212. package/dist/index.d.ts +26 -31
  213. package/dist/index.d.ts.map +1 -1
  214. package/dist/index.js +19 -24
  215. package/dist/index.js.map +1 -1
  216. package/dist/persistence/MemoryStore.d.ts +15 -0
  217. package/dist/persistence/MemoryStore.d.ts.map +1 -0
  218. package/dist/persistence/MemoryStore.js +35 -0
  219. package/dist/persistence/MemoryStore.js.map +1 -0
  220. package/dist/persistence/MongoStore.d.ts +42 -0
  221. package/dist/persistence/MongoStore.d.ts.map +1 -0
  222. package/dist/persistence/MongoStore.js +56 -0
  223. package/dist/persistence/MongoStore.js.map +1 -0
  224. package/dist/persistence/OpenSearchStore.d.ts +86 -0
  225. package/dist/persistence/OpenSearchStore.d.ts.map +1 -0
  226. package/dist/persistence/OpenSearchStore.js +116 -0
  227. package/dist/persistence/OpenSearchStore.js.map +1 -0
  228. package/dist/persistence/PostgresStore.d.ts +41 -0
  229. package/dist/persistence/PostgresStore.d.ts.map +1 -0
  230. package/dist/persistence/PostgresStore.js +54 -0
  231. package/dist/persistence/PostgresStore.js.map +1 -0
  232. package/dist/persistence/PrismaStore.d.ts +65 -0
  233. package/dist/persistence/PrismaStore.d.ts.map +1 -0
  234. package/dist/persistence/PrismaStore.js +91 -0
  235. package/dist/persistence/PrismaStore.js.map +1 -0
  236. package/dist/persistence/RedisStore.d.ts +34 -0
  237. package/dist/persistence/RedisStore.d.ts.map +1 -0
  238. package/dist/persistence/RedisStore.js +57 -0
  239. package/dist/persistence/RedisStore.js.map +1 -0
  240. package/dist/persistence/SQLiteStore.d.ts +45 -0
  241. package/dist/persistence/SQLiteStore.d.ts.map +1 -0
  242. package/dist/persistence/SQLiteStore.js +70 -0
  243. package/dist/persistence/SQLiteStore.js.map +1 -0
  244. package/dist/persistence/sessionRow.d.ts +14 -0
  245. package/dist/persistence/sessionRow.d.ts.map +1 -0
  246. package/dist/persistence/sessionRow.js +45 -0
  247. package/dist/persistence/sessionRow.js.map +1 -0
  248. package/dist/providers/DeepSeekProvider.d.ts.map +1 -1
  249. package/dist/providers/DeepSeekProvider.js +8 -3
  250. package/dist/providers/DeepSeekProvider.js.map +1 -1
  251. package/dist/providers/GeminiProvider.d.ts +4 -3
  252. package/dist/providers/GeminiProvider.d.ts.map +1 -1
  253. package/dist/providers/GeminiProvider.js +4 -3
  254. package/dist/providers/GeminiProvider.js.map +1 -1
  255. package/dist/providers/OpenAICompatibleProvider.d.ts +4 -0
  256. package/dist/providers/OpenAICompatibleProvider.d.ts.map +1 -1
  257. package/dist/providers/OpenAICompatibleProvider.js +2 -0
  258. package/dist/providers/OpenAICompatibleProvider.js.map +1 -1
  259. package/dist/providers/OpenRouterProvider.d.ts.map +1 -1
  260. package/dist/providers/OpenRouterProvider.js +2 -4
  261. package/dist/providers/OpenRouterProvider.js.map +1 -1
  262. package/dist/providers/ProviderAdapter.d.ts +1 -1
  263. package/dist/providers/ProviderAdapter.d.ts.map +1 -1
  264. package/dist/providers/ProviderAdapter.js +34 -11
  265. package/dist/providers/ProviderAdapter.js.map +1 -1
  266. package/dist/types/agent.d.ts +153 -383
  267. package/dist/types/agent.d.ts.map +1 -1
  268. package/dist/types/agent.js +1 -1
  269. package/dist/types/ai.d.ts +32 -1
  270. package/dist/types/ai.d.ts.map +1 -1
  271. package/dist/types/compaction.d.ts +3 -1
  272. package/dist/types/compaction.d.ts.map +1 -1
  273. package/dist/types/errors.d.ts +9 -12
  274. package/dist/types/errors.d.ts.map +1 -1
  275. package/dist/types/errors.js +12 -15
  276. package/dist/types/errors.js.map +1 -1
  277. package/dist/types/flow.d.ts +265 -513
  278. package/dist/types/flow.d.ts.map +1 -1
  279. package/dist/types/flow.js +7 -1
  280. package/dist/types/flow.js.map +1 -1
  281. package/dist/types/history.d.ts +7 -18
  282. package/dist/types/history.d.ts.map +1 -1
  283. package/dist/types/history.js.map +1 -1
  284. package/dist/types/index.d.ts +9 -15
  285. package/dist/types/index.d.ts.map +1 -1
  286. package/dist/types/index.js +2 -7
  287. package/dist/types/index.js.map +1 -1
  288. package/dist/types/session.d.ts +94 -64
  289. package/dist/types/session.d.ts.map +1 -1
  290. package/dist/types/session.js +5 -1
  291. package/dist/types/session.js.map +1 -1
  292. package/dist/types/tool.d.ts +37 -207
  293. package/dist/types/tool.d.ts.map +1 -1
  294. package/dist/types/tool.js +6 -13
  295. package/dist/types/tool.js.map +1 -1
  296. package/dist/utils/clock.d.ts +28 -0
  297. package/dist/utils/clock.d.ts.map +1 -0
  298. package/dist/utils/clock.js +59 -0
  299. package/dist/utils/clock.js.map +1 -0
  300. package/dist/utils/duration.d.ts +11 -0
  301. package/dist/utils/duration.d.ts.map +1 -0
  302. package/dist/utils/duration.js +26 -0
  303. package/dist/utils/duration.js.map +1 -0
  304. package/dist/utils/history.d.ts +4 -1
  305. package/dist/utils/history.d.ts.map +1 -1
  306. package/dist/utils/history.js +2 -2
  307. package/dist/utils/history.js.map +1 -1
  308. package/dist/utils/index.d.ts +4 -10
  309. package/dist/utils/index.d.ts.map +1 -1
  310. package/dist/utils/index.js +4 -21
  311. package/dist/utils/index.js.map +1 -1
  312. package/dist/utils/json.d.ts +2 -0
  313. package/dist/utils/json.d.ts.map +1 -1
  314. package/dist/utils/json.js +4 -0
  315. package/dist/utils/json.js.map +1 -1
  316. package/dist/utils/outcomes.d.ts +48 -0
  317. package/dist/utils/outcomes.d.ts.map +1 -0
  318. package/dist/utils/outcomes.js +48 -0
  319. package/dist/utils/outcomes.js.map +1 -0
  320. package/dist/utils/schema.d.ts +50 -0
  321. package/dist/utils/schema.d.ts.map +1 -0
  322. package/dist/utils/schema.js +129 -0
  323. package/dist/utils/schema.js.map +1 -0
  324. package/dist/utils/streamingMessage.d.ts +3 -2
  325. package/dist/utils/streamingMessage.d.ts.map +1 -1
  326. package/dist/utils/streamingMessage.js +38 -4
  327. package/dist/utils/streamingMessage.js.map +1 -1
  328. package/dist/utils/template.d.ts +13 -149
  329. package/dist/utils/template.d.ts.map +1 -1
  330. package/dist/utils/template.js +28 -355
  331. package/dist/utils/template.js.map +1 -1
  332. package/dist/utils/usage.d.ts +19 -0
  333. package/dist/utils/usage.d.ts.map +1 -0
  334. package/dist/utils/usage.js +31 -0
  335. package/dist/utils/usage.js.map +1 -0
  336. package/docs/README.md +37 -19
  337. package/docs/concepts/architecture.md +117 -239
  338. package/docs/concepts/collection.md +170 -0
  339. package/docs/concepts/pipeline.md +132 -378
  340. package/docs/concepts/runs-and-waits.md +192 -0
  341. package/docs/guides/actions-and-events.md +276 -0
  342. package/docs/guides/branching.md +119 -208
  343. package/docs/guides/compaction.md +63 -158
  344. package/docs/guides/conditions.md +164 -128
  345. package/docs/guides/error-handling.md +168 -164
  346. package/docs/guides/flow-control.md +210 -349
  347. package/docs/guides/flows-from-json.md +224 -0
  348. package/docs/guides/instructions.md +125 -161
  349. package/docs/guides/persistence.md +182 -206
  350. package/docs/guides/streaming.md +50 -114
  351. package/docs/guides/testing.md +284 -0
  352. package/docs/guides/triggers.md +401 -0
  353. package/docs/migration/README.md +8 -15
  354. package/docs/migration/v1-to-v2.md +1 -1
  355. package/docs/migration/v2-3-to-v2-4.md +2 -2
  356. package/docs/migration/v2-6-to-v2-7.md +4 -4
  357. package/docs/migration/v3-to-v4.md +452 -0
  358. package/docs/reference/actions-events-conditions.md +396 -0
  359. package/docs/reference/agent.md +244 -0
  360. package/docs/reference/branches.md +75 -203
  361. package/docs/reference/errors.md +188 -144
  362. package/docs/reference/fields.md +125 -0
  363. package/docs/reference/flow-spec.md +248 -0
  364. package/docs/reference/flow.md +104 -192
  365. package/docs/reference/instruction.md +83 -137
  366. package/docs/reference/outcomes.md +273 -0
  367. package/docs/reference/providers.md +525 -302
  368. package/docs/reference/session.md +210 -0
  369. package/docs/reference/step.md +194 -312
  370. package/docs/reference/stores.md +496 -0
  371. package/docs/reference/tool.md +162 -231
  372. package/docs/reference/trigger.md +180 -0
  373. package/docs/rfc/v4-one-flow.md +477 -0
  374. package/docs/start/01-install.md +59 -44
  375. package/docs/start/02-first-agent.md +97 -147
  376. package/docs/start/03-collect-data.md +78 -183
  377. package/docs/start/04-add-tools.md +159 -227
  378. package/docs/start/05-go-to-production.md +167 -164
  379. package/examples/01-quickstart.ts +26 -16
  380. package/examples/02-fields.ts +75 -0
  381. package/examples/03-tools.ts +79 -119
  382. package/examples/04-instructions.ts +60 -87
  383. package/examples/05-branches.ts +78 -0
  384. package/examples/06-triggers-and-waits.ts +148 -0
  385. package/examples/07-streaming.ts +34 -60
  386. package/examples/08-store-and-migration.ts +97 -0
  387. package/examples/09-flows-from-json.ts +107 -0
  388. package/package.json +9 -6
  389. package/src/core/Agent.ts +116 -1512
  390. package/src/core/CompactionEngine.ts +7 -4
  391. package/src/core/FlowSpec.ts +712 -0
  392. package/src/core/Migrate.ts +256 -0
  393. package/src/core/Prompt.ts +156 -0
  394. package/src/core/Runner.ts +1181 -0
  395. package/src/core/Speak.ts +451 -0
  396. package/src/core/Understand.ts +422 -0
  397. package/src/core/contracts.ts +111 -0
  398. package/src/core/falai.ts +86 -0
  399. package/src/core/predicate.ts +56 -0
  400. package/src/index.ts +119 -147
  401. package/src/persistence/MemoryStore.ts +37 -0
  402. package/src/persistence/MongoStore.ts +89 -0
  403. package/src/persistence/OpenSearchStore.ts +153 -0
  404. package/src/persistence/PostgresStore.ts +89 -0
  405. package/src/persistence/PrismaStore.ts +127 -0
  406. package/src/persistence/RedisStore.ts +90 -0
  407. package/src/persistence/SQLiteStore.ts +103 -0
  408. package/src/persistence/sessionRow.ts +45 -0
  409. package/src/providers/DeepSeekProvider.ts +8 -3
  410. package/src/providers/GeminiProvider.ts +4 -3
  411. package/src/providers/OpenAICompatibleProvider.ts +6 -0
  412. package/src/providers/OpenRouterProvider.ts +2 -4
  413. package/src/providers/ProviderAdapter.ts +36 -8
  414. package/src/types/agent.ts +124 -397
  415. package/src/types/ai.ts +33 -1
  416. package/src/types/compaction.ts +3 -1
  417. package/src/types/errors.ts +13 -16
  418. package/src/types/flow.ts +249 -550
  419. package/src/types/history.ts +7 -20
  420. package/src/types/index.ts +87 -139
  421. package/src/types/session.ts +135 -70
  422. package/src/types/tool.ts +42 -267
  423. package/src/utils/clock.ts +70 -0
  424. package/src/utils/duration.ts +33 -0
  425. package/src/utils/history.ts +3 -2
  426. package/src/utils/index.ts +8 -66
  427. package/src/utils/json.ts +5 -0
  428. package/src/utils/outcomes.ts +56 -0
  429. package/src/utils/schema.ts +145 -0
  430. package/src/utils/streamingMessage.ts +34 -4
  431. package/src/utils/template.ts +32 -423
  432. package/src/utils/usage.ts +37 -0
  433. package/dist/adapters/MemoryAdapter.d.ts +0 -47
  434. package/dist/adapters/MemoryAdapter.d.ts.map +0 -1
  435. package/dist/adapters/MemoryAdapter.js +0 -204
  436. package/dist/adapters/MemoryAdapter.js.map +0 -1
  437. package/dist/adapters/MongoAdapter.d.ts +0 -97
  438. package/dist/adapters/MongoAdapter.d.ts.map +0 -1
  439. package/dist/adapters/MongoAdapter.js +0 -196
  440. package/dist/adapters/MongoAdapter.js.map +0 -1
  441. package/dist/adapters/OpenSearchAdapter.d.ts +0 -169
  442. package/dist/adapters/OpenSearchAdapter.d.ts.map +0 -1
  443. package/dist/adapters/OpenSearchAdapter.js +0 -471
  444. package/dist/adapters/OpenSearchAdapter.js.map +0 -1
  445. package/dist/adapters/PostgreSQLAdapter.d.ts +0 -85
  446. package/dist/adapters/PostgreSQLAdapter.d.ts.map +0 -1
  447. package/dist/adapters/PostgreSQLAdapter.js +0 -308
  448. package/dist/adapters/PostgreSQLAdapter.js.map +0 -1
  449. package/dist/adapters/PrismaAdapter.d.ts +0 -115
  450. package/dist/adapters/PrismaAdapter.d.ts.map +0 -1
  451. package/dist/adapters/PrismaAdapter.js +0 -406
  452. package/dist/adapters/PrismaAdapter.js.map +0 -1
  453. package/dist/adapters/RedisAdapter.d.ts +0 -72
  454. package/dist/adapters/RedisAdapter.d.ts.map +0 -1
  455. package/dist/adapters/RedisAdapter.js +0 -286
  456. package/dist/adapters/RedisAdapter.js.map +0 -1
  457. package/dist/adapters/SQLiteAdapter.d.ts +0 -86
  458. package/dist/adapters/SQLiteAdapter.d.ts.map +0 -1
  459. package/dist/adapters/SQLiteAdapter.js +0 -337
  460. package/dist/adapters/SQLiteAdapter.js.map +0 -1
  461. package/dist/adapters/index.d.ts +0 -17
  462. package/dist/adapters/index.d.ts.map +0 -1
  463. package/dist/adapters/index.js +0 -11
  464. package/dist/adapters/index.js.map +0 -1
  465. package/dist/adapters/sessionRow.d.ts +0 -22
  466. package/dist/adapters/sessionRow.d.ts.map +0 -1
  467. package/dist/adapters/sessionRow.js +0 -48
  468. package/dist/adapters/sessionRow.js.map +0 -1
  469. package/dist/cjs/adapters/MemoryAdapter.d.ts +0 -47
  470. package/dist/cjs/adapters/MemoryAdapter.d.ts.map +0 -1
  471. package/dist/cjs/adapters/MemoryAdapter.js +0 -208
  472. package/dist/cjs/adapters/MemoryAdapter.js.map +0 -1
  473. package/dist/cjs/adapters/MongoAdapter.d.ts +0 -97
  474. package/dist/cjs/adapters/MongoAdapter.d.ts.map +0 -1
  475. package/dist/cjs/adapters/MongoAdapter.js +0 -200
  476. package/dist/cjs/adapters/MongoAdapter.js.map +0 -1
  477. package/dist/cjs/adapters/OpenSearchAdapter.d.ts +0 -169
  478. package/dist/cjs/adapters/OpenSearchAdapter.d.ts.map +0 -1
  479. package/dist/cjs/adapters/OpenSearchAdapter.js +0 -475
  480. package/dist/cjs/adapters/OpenSearchAdapter.js.map +0 -1
  481. package/dist/cjs/adapters/PostgreSQLAdapter.d.ts +0 -85
  482. package/dist/cjs/adapters/PostgreSQLAdapter.d.ts.map +0 -1
  483. package/dist/cjs/adapters/PostgreSQLAdapter.js +0 -312
  484. package/dist/cjs/adapters/PostgreSQLAdapter.js.map +0 -1
  485. package/dist/cjs/adapters/PrismaAdapter.d.ts +0 -115
  486. package/dist/cjs/adapters/PrismaAdapter.d.ts.map +0 -1
  487. package/dist/cjs/adapters/PrismaAdapter.js +0 -410
  488. package/dist/cjs/adapters/PrismaAdapter.js.map +0 -1
  489. package/dist/cjs/adapters/RedisAdapter.d.ts +0 -72
  490. package/dist/cjs/adapters/RedisAdapter.d.ts.map +0 -1
  491. package/dist/cjs/adapters/RedisAdapter.js +0 -290
  492. package/dist/cjs/adapters/RedisAdapter.js.map +0 -1
  493. package/dist/cjs/adapters/SQLiteAdapter.d.ts +0 -86
  494. package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +0 -1
  495. package/dist/cjs/adapters/SQLiteAdapter.js +0 -341
  496. package/dist/cjs/adapters/SQLiteAdapter.js.map +0 -1
  497. package/dist/cjs/adapters/index.d.ts +0 -17
  498. package/dist/cjs/adapters/index.d.ts.map +0 -1
  499. package/dist/cjs/adapters/index.js +0 -21
  500. package/dist/cjs/adapters/index.js.map +0 -1
  501. package/dist/cjs/adapters/sessionRow.d.ts +0 -22
  502. package/dist/cjs/adapters/sessionRow.d.ts.map +0 -1
  503. package/dist/cjs/adapters/sessionRow.js +0 -52
  504. package/dist/cjs/adapters/sessionRow.js.map +0 -1
  505. package/dist/cjs/constants/index.d.ts +0 -1
  506. package/dist/cjs/constants/index.d.ts.map +0 -1
  507. package/dist/cjs/constants/index.js +0 -4
  508. package/dist/cjs/constants/index.js.map +0 -1
  509. package/dist/cjs/core/AutoChainExecutor.d.ts +0 -97
  510. package/dist/cjs/core/AutoChainExecutor.d.ts.map +0 -1
  511. package/dist/cjs/core/AutoChainExecutor.js +0 -288
  512. package/dist/cjs/core/AutoChainExecutor.js.map +0 -1
  513. package/dist/cjs/core/BranchEvaluator.d.ts +0 -55
  514. package/dist/cjs/core/BranchEvaluator.d.ts.map +0 -1
  515. package/dist/cjs/core/BranchEvaluator.js +0 -125
  516. package/dist/cjs/core/BranchEvaluator.js.map +0 -1
  517. package/dist/cjs/core/DirectiveChainTracker.d.ts +0 -49
  518. package/dist/cjs/core/DirectiveChainTracker.d.ts.map +0 -1
  519. package/dist/cjs/core/DirectiveChainTracker.js +0 -121
  520. package/dist/cjs/core/DirectiveChainTracker.js.map +0 -1
  521. package/dist/cjs/core/Events.d.ts +0 -26
  522. package/dist/cjs/core/Events.d.ts.map +0 -1
  523. package/dist/cjs/core/Events.js +0 -144
  524. package/dist/cjs/core/Events.js.map +0 -1
  525. package/dist/cjs/core/Flow.d.ts +0 -183
  526. package/dist/cjs/core/Flow.d.ts.map +0 -1
  527. package/dist/cjs/core/Flow.js +0 -551
  528. package/dist/cjs/core/Flow.js.map +0 -1
  529. package/dist/cjs/core/FlowRouter.d.ts +0 -183
  530. package/dist/cjs/core/FlowRouter.d.ts.map +0 -1
  531. package/dist/cjs/core/FlowRouter.js +0 -1047
  532. package/dist/cjs/core/FlowRouter.js.map +0 -1
  533. package/dist/cjs/core/PersistenceManager.d.ts +0 -114
  534. package/dist/cjs/core/PersistenceManager.d.ts.map +0 -1
  535. package/dist/cjs/core/PersistenceManager.js +0 -336
  536. package/dist/cjs/core/PersistenceManager.js.map +0 -1
  537. package/dist/cjs/core/PromptComposer.d.ts +0 -47
  538. package/dist/cjs/core/PromptComposer.d.ts.map +0 -1
  539. package/dist/cjs/core/PromptComposer.js +0 -397
  540. package/dist/cjs/core/PromptComposer.js.map +0 -1
  541. package/dist/cjs/core/PromptSectionCache.d.ts +0 -48
  542. package/dist/cjs/core/PromptSectionCache.d.ts.map +0 -1
  543. package/dist/cjs/core/PromptSectionCache.js +0 -108
  544. package/dist/cjs/core/PromptSectionCache.js.map +0 -1
  545. package/dist/cjs/core/ResponseEngine.d.ts +0 -43
  546. package/dist/cjs/core/ResponseEngine.d.ts.map +0 -1
  547. package/dist/cjs/core/ResponseEngine.js +0 -235
  548. package/dist/cjs/core/ResponseEngine.js.map +0 -1
  549. package/dist/cjs/core/ResponseGenerationError.d.ts +0 -30
  550. package/dist/cjs/core/ResponseGenerationError.d.ts.map +0 -1
  551. package/dist/cjs/core/ResponseGenerationError.js +0 -35
  552. package/dist/cjs/core/ResponseGenerationError.js.map +0 -1
  553. package/dist/cjs/core/ResponseModal.d.ts +0 -305
  554. package/dist/cjs/core/ResponseModal.d.ts.map +0 -1
  555. package/dist/cjs/core/ResponseModal.js +0 -1414
  556. package/dist/cjs/core/ResponseModal.js.map +0 -1
  557. package/dist/cjs/core/ResponsePipeline.d.ts +0 -220
  558. package/dist/cjs/core/ResponsePipeline.d.ts.map +0 -1
  559. package/dist/cjs/core/ResponsePipeline.js +0 -1040
  560. package/dist/cjs/core/ResponsePipeline.js.map +0 -1
  561. package/dist/cjs/core/SessionFinalizer.d.ts +0 -34
  562. package/dist/cjs/core/SessionFinalizer.d.ts.map +0 -1
  563. package/dist/cjs/core/SessionFinalizer.js +0 -88
  564. package/dist/cjs/core/SessionFinalizer.js.map +0 -1
  565. package/dist/cjs/core/SessionManager.d.ts +0 -112
  566. package/dist/cjs/core/SessionManager.d.ts.map +0 -1
  567. package/dist/cjs/core/SessionManager.js +0 -308
  568. package/dist/cjs/core/SessionManager.js.map +0 -1
  569. package/dist/cjs/core/SignalCoordinator.d.ts +0 -103
  570. package/dist/cjs/core/SignalCoordinator.d.ts.map +0 -1
  571. package/dist/cjs/core/SignalCoordinator.js +0 -207
  572. package/dist/cjs/core/SignalCoordinator.js.map +0 -1
  573. package/dist/cjs/core/SignalEvaluator.d.ts +0 -86
  574. package/dist/cjs/core/SignalEvaluator.d.ts.map +0 -1
  575. package/dist/cjs/core/SignalEvaluator.js +0 -319
  576. package/dist/cjs/core/SignalEvaluator.js.map +0 -1
  577. package/dist/cjs/core/SignalProcessor.d.ts +0 -152
  578. package/dist/cjs/core/SignalProcessor.d.ts.map +0 -1
  579. package/dist/cjs/core/SignalProcessor.js +0 -505
  580. package/dist/cjs/core/SignalProcessor.js.map +0 -1
  581. package/dist/cjs/core/Step.d.ts +0 -184
  582. package/dist/cjs/core/Step.d.ts.map +0 -1
  583. package/dist/cjs/core/Step.js +0 -599
  584. package/dist/cjs/core/Step.js.map +0 -1
  585. package/dist/cjs/core/StepLifecycle.d.ts +0 -43
  586. package/dist/cjs/core/StepLifecycle.d.ts.map +0 -1
  587. package/dist/cjs/core/StepLifecycle.js +0 -180
  588. package/dist/cjs/core/StepLifecycle.js.map +0 -1
  589. package/dist/cjs/core/StreamingToolExecutor.d.ts +0 -142
  590. package/dist/cjs/core/StreamingToolExecutor.d.ts.map +0 -1
  591. package/dist/cjs/core/StreamingToolExecutor.js +0 -490
  592. package/dist/cjs/core/StreamingToolExecutor.js.map +0 -1
  593. package/dist/cjs/core/ToolLoopExecutor.d.ts +0 -133
  594. package/dist/cjs/core/ToolLoopExecutor.d.ts.map +0 -1
  595. package/dist/cjs/core/ToolLoopExecutor.js +0 -568
  596. package/dist/cjs/core/ToolLoopExecutor.js.map +0 -1
  597. package/dist/cjs/core/ToolManager.d.ts +0 -250
  598. package/dist/cjs/core/ToolManager.d.ts.map +0 -1
  599. package/dist/cjs/core/ToolManager.js +0 -1104
  600. package/dist/cjs/core/ToolManager.js.map +0 -1
  601. package/dist/cjs/core/createAgent.d.ts +0 -35
  602. package/dist/cjs/core/createAgent.d.ts.map +0 -1
  603. package/dist/cjs/core/createAgent.js +0 -39
  604. package/dist/cjs/core/createAgent.js.map +0 -1
  605. package/dist/cjs/core/flow-namespace.d.ts +0 -64
  606. package/dist/cjs/core/flow-namespace.d.ts.map +0 -1
  607. package/dist/cjs/core/flow-namespace.js +0 -182
  608. package/dist/cjs/core/flow-namespace.js.map +0 -1
  609. package/dist/cjs/core/toolGates.d.ts +0 -24
  610. package/dist/cjs/core/toolGates.d.ts.map +0 -1
  611. package/dist/cjs/core/toolGates.js +0 -52
  612. package/dist/cjs/core/toolGates.js.map +0 -1
  613. package/dist/cjs/types/persistence.d.ts +0 -254
  614. package/dist/cjs/types/persistence.d.ts.map +0 -1
  615. package/dist/cjs/types/persistence.js +0 -7
  616. package/dist/cjs/types/persistence.js.map +0 -1
  617. package/dist/cjs/types/prompt-cache.d.ts +0 -15
  618. package/dist/cjs/types/prompt-cache.d.ts.map +0 -1
  619. package/dist/cjs/types/prompt-cache.js +0 -6
  620. package/dist/cjs/types/prompt-cache.js.map +0 -1
  621. package/dist/cjs/types/signals.d.ts +0 -263
  622. package/dist/cjs/types/signals.d.ts.map +0 -1
  623. package/dist/cjs/types/signals.js +0 -11
  624. package/dist/cjs/types/signals.js.map +0 -1
  625. package/dist/cjs/types/template.d.ts +0 -84
  626. package/dist/cjs/types/template.d.ts.map +0 -1
  627. package/dist/cjs/types/template.js +0 -3
  628. package/dist/cjs/types/template.js.map +0 -1
  629. package/dist/cjs/utils/condition.d.ts +0 -63
  630. package/dist/cjs/utils/condition.d.ts.map +0 -1
  631. package/dist/cjs/utils/condition.js +0 -239
  632. package/dist/cjs/utils/condition.js.map +0 -1
  633. package/dist/cjs/utils/event.d.ts +0 -6
  634. package/dist/cjs/utils/event.d.ts.map +0 -1
  635. package/dist/cjs/utils/event.js +0 -20
  636. package/dist/cjs/utils/event.js.map +0 -1
  637. package/dist/cjs/utils/id.d.ts +0 -33
  638. package/dist/cjs/utils/id.d.ts.map +0 -1
  639. package/dist/cjs/utils/id.js +0 -84
  640. package/dist/cjs/utils/id.js.map +0 -1
  641. package/dist/cjs/utils/serialize.d.ts +0 -36
  642. package/dist/cjs/utils/serialize.d.ts.map +0 -1
  643. package/dist/cjs/utils/serialize.js +0 -77
  644. package/dist/cjs/utils/serialize.js.map +0 -1
  645. package/dist/cjs/utils/session.d.ts +0 -124
  646. package/dist/cjs/utils/session.d.ts.map +0 -1
  647. package/dist/cjs/utils/session.js +0 -396
  648. package/dist/cjs/utils/session.js.map +0 -1
  649. package/dist/constants/index.d.ts +0 -2
  650. package/dist/constants/index.d.ts.map +0 -1
  651. package/dist/constants/index.js +0 -4
  652. package/dist/constants/index.js.map +0 -1
  653. package/dist/core/AutoChainExecutor.d.ts +0 -97
  654. package/dist/core/AutoChainExecutor.d.ts.map +0 -1
  655. package/dist/core/AutoChainExecutor.js +0 -284
  656. package/dist/core/AutoChainExecutor.js.map +0 -1
  657. package/dist/core/BranchEvaluator.d.ts +0 -55
  658. package/dist/core/BranchEvaluator.d.ts.map +0 -1
  659. package/dist/core/BranchEvaluator.js +0 -121
  660. package/dist/core/BranchEvaluator.js.map +0 -1
  661. package/dist/core/DirectiveChainTracker.d.ts +0 -49
  662. package/dist/core/DirectiveChainTracker.d.ts.map +0 -1
  663. package/dist/core/DirectiveChainTracker.js +0 -117
  664. package/dist/core/DirectiveChainTracker.js.map +0 -1
  665. package/dist/core/Events.d.ts +0 -26
  666. package/dist/core/Events.d.ts.map +0 -1
  667. package/dist/core/Events.js +0 -137
  668. package/dist/core/Events.js.map +0 -1
  669. package/dist/core/Flow.d.ts +0 -183
  670. package/dist/core/Flow.d.ts.map +0 -1
  671. package/dist/core/Flow.js +0 -547
  672. package/dist/core/Flow.js.map +0 -1
  673. package/dist/core/FlowRouter.d.ts +0 -183
  674. package/dist/core/FlowRouter.d.ts.map +0 -1
  675. package/dist/core/FlowRouter.js +0 -1043
  676. package/dist/core/FlowRouter.js.map +0 -1
  677. package/dist/core/PersistenceManager.d.ts +0 -114
  678. package/dist/core/PersistenceManager.d.ts.map +0 -1
  679. package/dist/core/PersistenceManager.js +0 -332
  680. package/dist/core/PersistenceManager.js.map +0 -1
  681. package/dist/core/PromptComposer.d.ts +0 -47
  682. package/dist/core/PromptComposer.d.ts.map +0 -1
  683. package/dist/core/PromptComposer.js +0 -393
  684. package/dist/core/PromptComposer.js.map +0 -1
  685. package/dist/core/PromptSectionCache.d.ts +0 -48
  686. package/dist/core/PromptSectionCache.d.ts.map +0 -1
  687. package/dist/core/PromptSectionCache.js +0 -104
  688. package/dist/core/PromptSectionCache.js.map +0 -1
  689. package/dist/core/ResponseEngine.d.ts +0 -43
  690. package/dist/core/ResponseEngine.d.ts.map +0 -1
  691. package/dist/core/ResponseEngine.js +0 -231
  692. package/dist/core/ResponseEngine.js.map +0 -1
  693. package/dist/core/ResponseGenerationError.d.ts +0 -30
  694. package/dist/core/ResponseGenerationError.d.ts.map +0 -1
  695. package/dist/core/ResponseGenerationError.js +0 -31
  696. package/dist/core/ResponseGenerationError.js.map +0 -1
  697. package/dist/core/ResponseModal.d.ts +0 -305
  698. package/dist/core/ResponseModal.d.ts.map +0 -1
  699. package/dist/core/ResponseModal.js +0 -1410
  700. package/dist/core/ResponseModal.js.map +0 -1
  701. package/dist/core/ResponsePipeline.d.ts +0 -220
  702. package/dist/core/ResponsePipeline.d.ts.map +0 -1
  703. package/dist/core/ResponsePipeline.js +0 -1035
  704. package/dist/core/ResponsePipeline.js.map +0 -1
  705. package/dist/core/SessionFinalizer.d.ts +0 -34
  706. package/dist/core/SessionFinalizer.d.ts.map +0 -1
  707. package/dist/core/SessionFinalizer.js +0 -84
  708. package/dist/core/SessionFinalizer.js.map +0 -1
  709. package/dist/core/SessionManager.d.ts +0 -112
  710. package/dist/core/SessionManager.d.ts.map +0 -1
  711. package/dist/core/SessionManager.js +0 -301
  712. package/dist/core/SessionManager.js.map +0 -1
  713. package/dist/core/SignalCoordinator.d.ts +0 -103
  714. package/dist/core/SignalCoordinator.d.ts.map +0 -1
  715. package/dist/core/SignalCoordinator.js +0 -203
  716. package/dist/core/SignalCoordinator.js.map +0 -1
  717. package/dist/core/SignalEvaluator.d.ts +0 -86
  718. package/dist/core/SignalEvaluator.d.ts.map +0 -1
  719. package/dist/core/SignalEvaluator.js +0 -312
  720. package/dist/core/SignalEvaluator.js.map +0 -1
  721. package/dist/core/SignalProcessor.d.ts +0 -152
  722. package/dist/core/SignalProcessor.d.ts.map +0 -1
  723. package/dist/core/SignalProcessor.js +0 -498
  724. package/dist/core/SignalProcessor.js.map +0 -1
  725. package/dist/core/Step.d.ts +0 -184
  726. package/dist/core/Step.d.ts.map +0 -1
  727. package/dist/core/Step.js +0 -594
  728. package/dist/core/Step.js.map +0 -1
  729. package/dist/core/StepLifecycle.d.ts +0 -43
  730. package/dist/core/StepLifecycle.d.ts.map +0 -1
  731. package/dist/core/StepLifecycle.js +0 -176
  732. package/dist/core/StepLifecycle.js.map +0 -1
  733. package/dist/core/StreamingToolExecutor.d.ts +0 -142
  734. package/dist/core/StreamingToolExecutor.d.ts.map +0 -1
  735. package/dist/core/StreamingToolExecutor.js +0 -483
  736. package/dist/core/StreamingToolExecutor.js.map +0 -1
  737. package/dist/core/ToolLoopExecutor.d.ts +0 -133
  738. package/dist/core/ToolLoopExecutor.d.ts.map +0 -1
  739. package/dist/core/ToolLoopExecutor.js +0 -564
  740. package/dist/core/ToolLoopExecutor.js.map +0 -1
  741. package/dist/core/ToolManager.d.ts +0 -250
  742. package/dist/core/ToolManager.d.ts.map +0 -1
  743. package/dist/core/ToolManager.js +0 -1098
  744. package/dist/core/ToolManager.js.map +0 -1
  745. package/dist/core/createAgent.d.ts +0 -35
  746. package/dist/core/createAgent.d.ts.map +0 -1
  747. package/dist/core/createAgent.js +0 -36
  748. package/dist/core/createAgent.js.map +0 -1
  749. package/dist/core/flow-namespace.d.ts +0 -64
  750. package/dist/core/flow-namespace.d.ts.map +0 -1
  751. package/dist/core/flow-namespace.js +0 -179
  752. package/dist/core/flow-namespace.js.map +0 -1
  753. package/dist/core/toolGates.d.ts +0 -24
  754. package/dist/core/toolGates.d.ts.map +0 -1
  755. package/dist/core/toolGates.js +0 -49
  756. package/dist/core/toolGates.js.map +0 -1
  757. package/dist/types/persistence.d.ts +0 -254
  758. package/dist/types/persistence.d.ts.map +0 -1
  759. package/dist/types/persistence.js +0 -6
  760. package/dist/types/persistence.js.map +0 -1
  761. package/dist/types/prompt-cache.d.ts +0 -15
  762. package/dist/types/prompt-cache.d.ts.map +0 -1
  763. package/dist/types/prompt-cache.js +0 -5
  764. package/dist/types/prompt-cache.js.map +0 -1
  765. package/dist/types/signals.d.ts +0 -263
  766. package/dist/types/signals.d.ts.map +0 -1
  767. package/dist/types/signals.js +0 -10
  768. package/dist/types/signals.js.map +0 -1
  769. package/dist/types/template.d.ts +0 -84
  770. package/dist/types/template.d.ts.map +0 -1
  771. package/dist/types/template.js +0 -2
  772. package/dist/types/template.js.map +0 -1
  773. package/dist/utils/condition.d.ts +0 -63
  774. package/dist/utils/condition.d.ts.map +0 -1
  775. package/dist/utils/condition.js +0 -230
  776. package/dist/utils/condition.js.map +0 -1
  777. package/dist/utils/event.d.ts +0 -6
  778. package/dist/utils/event.d.ts.map +0 -1
  779. package/dist/utils/event.js +0 -17
  780. package/dist/utils/event.js.map +0 -1
  781. package/dist/utils/id.d.ts +0 -33
  782. package/dist/utils/id.d.ts.map +0 -1
  783. package/dist/utils/id.js +0 -77
  784. package/dist/utils/id.js.map +0 -1
  785. package/dist/utils/serialize.d.ts +0 -36
  786. package/dist/utils/serialize.d.ts.map +0 -1
  787. package/dist/utils/serialize.js +0 -72
  788. package/dist/utils/serialize.js.map +0 -1
  789. package/dist/utils/session.d.ts +0 -124
  790. package/dist/utils/session.d.ts.map +0 -1
  791. package/dist/utils/session.js +0 -379
  792. package/dist/utils/session.js.map +0 -1
  793. package/docs/concepts/directives.md +0 -369
  794. package/docs/reference/adapters.md +0 -543
  795. package/docs/reference/create-agent.md +0 -216
  796. package/docs/reference/directive.md +0 -242
  797. package/docs/reference/signals.md +0 -368
  798. package/examples/02-data-extraction.ts +0 -90
  799. package/examples/05-branching.ts +0 -140
  800. package/examples/06-flow-control.ts +0 -103
  801. package/examples/08-persistence.ts +0 -98
  802. package/examples/09-signals.ts +0 -144
  803. package/src/adapters/MemoryAdapter.ts +0 -281
  804. package/src/adapters/MongoAdapter.ts +0 -341
  805. package/src/adapters/OpenSearchAdapter.ts +0 -693
  806. package/src/adapters/PostgreSQLAdapter.ts +0 -487
  807. package/src/adapters/PrismaAdapter.ts +0 -617
  808. package/src/adapters/RedisAdapter.ts +0 -439
  809. package/src/adapters/SQLiteAdapter.ts +0 -496
  810. package/src/adapters/index.ts +0 -43
  811. package/src/adapters/sessionRow.ts +0 -57
  812. package/src/constants/index.ts +0 -2
  813. package/src/core/AutoChainExecutor.ts +0 -397
  814. package/src/core/BranchEvaluator.ts +0 -161
  815. package/src/core/DirectiveChainTracker.ts +0 -144
  816. package/src/core/Events.ts +0 -164
  817. package/src/core/Flow.ts +0 -665
  818. package/src/core/FlowRouter.ts +0 -1540
  819. package/src/core/PersistenceManager.ts +0 -446
  820. package/src/core/PromptComposer.ts +0 -448
  821. package/src/core/PromptSectionCache.ts +0 -125
  822. package/src/core/ResponseEngine.ts +0 -338
  823. package/src/core/ResponseGenerationError.ts +0 -53
  824. package/src/core/ResponseModal.ts +0 -1902
  825. package/src/core/ResponsePipeline.ts +0 -1404
  826. package/src/core/SessionFinalizer.ts +0 -108
  827. package/src/core/SessionManager.ts +0 -372
  828. package/src/core/SignalCoordinator.ts +0 -263
  829. package/src/core/SignalEvaluator.ts +0 -404
  830. package/src/core/SignalProcessor.ts +0 -663
  831. package/src/core/Step.ts +0 -782
  832. package/src/core/StepLifecycle.ts +0 -242
  833. package/src/core/StreamingToolExecutor.ts +0 -609
  834. package/src/core/ToolLoopExecutor.ts +0 -749
  835. package/src/core/ToolManager.ts +0 -1379
  836. package/src/core/createAgent.ts +0 -40
  837. package/src/core/flow-namespace.ts +0 -227
  838. package/src/core/toolGates.ts +0 -72
  839. package/src/types/persistence.ts +0 -303
  840. package/src/types/prompt-cache.ts +0 -17
  841. package/src/types/signals.ts +0 -338
  842. package/src/types/template.ts +0 -98
  843. package/src/utils/condition.ts +0 -296
  844. package/src/utils/event.ts +0 -16
  845. package/src/utils/id.ts +0 -91
  846. package/src/utils/serialize.ts +0 -86
  847. package/src/utils/session.ts +0 -501
@@ -1,543 +1,295 @@
1
1
  /**
2
- * Flow/Journey DSL type definitions
3
- */
4
- import type { Tool } from "./tool.js";
5
- import type { StructuredSchema } from "./schema.js";
6
- import type { Instruction, HookContext, ExitReason } from "./agent.js";
7
- import type { Template } from "./template.js";
8
- /**
9
- * Code-evaluated condition predicate. Returns `true` to pass, `false` to fail.
10
- * May be async. Used on `if` fields (free to evaluate, no LLM cost).
11
- */
12
- export type ConditionPredicate<TContext = unknown, TData = unknown> = (ctx: {
13
- data: Partial<TData>;
14
- context: TContext;
15
- session: import("./session.js").SessionState<TData>;
16
- history: Event[];
17
- }) => boolean | Promise<boolean>;
18
- /**
19
- * The `if` field shape: a single predicate or an array of predicates (AND semantics).
20
- */
21
- export type ConditionIf<TContext = unknown, TData = unknown> = ConditionPredicate<TContext, TData> | ConditionPredicate<TContext, TData>[];
22
- /**
23
- * The `when` field shape: a single AI-evaluated string or array of strings.
24
- * Non-`!` entries use OR semantics. `!`-prefixed entries are OR exclusions:
25
- * any matching exclusion inhibits the condition. Functions are NOT allowed on
26
- * `when` — they belong on `if` only.
27
- */
28
- export type ConditionWhen = string | string[];
29
- import type { SessionState } from "./session.js";
30
- import type { Event } from "./history.js";
31
- /**
32
- * Reason why execution stopped.
33
- * Used to indicate the stopping condition for step execution.
2
+ * The authoring surface: fields, flows, steps, triggers, actions, conditions.
34
3
  *
35
- * v2 vocabulary — see Requirements 17.1–17.9.
4
+ * A Flow is a trigger plus an ordered list of steps. Everything here is a
5
+ * plain object literal, so the same shape round-trips through JSON (FlowSpec).
6
+ * Two generics thread through: `C` is the host's ambient context, passed on
7
+ * every turn; `D` is the data collected across all flows, inferred from the
8
+ * agent's fields with `InferData`.
36
9
  */
37
- export type StoppedReason = 'needs_input' | 'last_step' | 'completed' | 'aborted' | 'goto' | 'reset' | 'halt' | 'reply' | 'max_auto_steps' | 'prepare_error' | 'llm_error' | 'validation_error' | 'finalize_error';
10
+ import type { Run } from "./session.js";
11
+ /** A time span: `'30s'`, `'5m'`, `'24h'`, `'3d'`. */
12
+ export type Duration = `${number}${"s" | "m" | "h" | "d"}`;
13
+ /** How often a trigger may start a run for one session or anchor. */
14
+ export type Repeat = "once" | "always" | {
15
+ cooldown: Duration;
16
+ };
38
17
  /**
39
- * Result that a prepare/finalize hook may return to issue directives.
40
- * All fields are optional — returning void is also valid.
18
+ * Prompt text. `{{data.x}}`, `{{context.x}}` and `{{input.x}}` are filled in
19
+ * before the text is used.
41
20
  */
42
- export interface PrepareResult<TContext = unknown, TData = unknown> {
43
- /** Partial data to merge into session.data */
44
- dataUpdate?: Partial<TData>;
45
- /** Partial context to merge into the agent context */
46
- contextUpdate?: Partial<TContext>;
47
- /** If true, stop the auto-step chain and end the turn with `reply` */
48
- halt?: boolean;
49
- /** Verbatim reply to send when halting */
50
- reply?: string;
51
- /** Jump to a step within the current flow */
52
- goToStep?: string | {
53
- step: string;
54
- flow?: string;
55
- data?: Partial<TData>;
56
- reason?: string;
57
- };
58
- /** Jump to another flow */
59
- goTo?: string | {
60
- flow?: string;
61
- step?: string;
62
- data?: Partial<TData>;
63
- reason?: string;
64
- carry?: 'preserve' | 'reset';
65
- };
66
- /** Mark the current flow as complete */
67
- complete?: true | {
68
- next?: Directive<unknown, unknown>;
69
- reason?: string;
70
- };
71
- /**
72
- * Prompt lines appended to this turn's generation prompt. Pre-LLM
73
- * augmentation: honored by auto steps' prepare; other step kinds drop it
74
- * with a loud warning rather than silently ignoring it.
75
- */
76
- appendPrompt?: string[];
77
- /**
78
- * Extra tools made available for this turn only. Same auto-step-only
79
- * honoring as {@link appendPrompt}.
80
- */
81
- injectTools?: Tool[];
82
- }
21
+ export type Template = string;
83
22
  /**
84
- * A programmatic transition directive. The single shape any tool, hook, branch,
85
- * or signal handler returns to write state, redirect the conversation, or speak
86
- * verbatim.
87
- *
88
- * At most one position field (`goTo`, `goToStep`, `complete`, `abort`, `reset`)
89
- * may be set per Directive. Non-position fields (`reply`, `contextUpdate`,
90
- * `dataUpdate`) may accompany any position field.
91
- *
92
- * Pre-LLM augmentation fields (`appendPrompt`, `injectTools`, `halt`) are
93
- * one-turn-lifetime: they only take effect in pre-LLM hooks (`onEnter`,
94
- * `prepare`). When emitted from post-LLM hooks (`finalize`, `onComplete`) or
95
- * persisted to `session.pendingDirective`, these fields are ignored and a WARN
96
- * log is emitted. They are never serialized across turns.
23
+ * Where a step sends the run next: a step id, `'end'` (reserved; ends the
24
+ * run), a step with fields to clear first, or another flow to start.
97
25
  */
98
- export interface Directive<TContext = unknown, TData = unknown> {
99
- /** Jump to a flow (string) or a flow with options (object). */
100
- goTo?: string | {
101
- flow?: string;
102
- step?: string;
103
- data?: Partial<TData>;
104
- reason?: string;
105
- carry?: 'preserve' | 'reset';
106
- };
107
- /** Jump to a specific step, optionally in another flow. */
108
- goToStep?: string | {
109
- step: string;
110
- flow?: string;
111
- data?: Partial<TData>;
112
- reason?: string;
113
- };
114
- /** Mark the current flow as complete. */
115
- complete?: true | {
116
- next?: Directive<unknown, unknown>;
117
- reason?: string;
118
- };
119
- /** Abort the current flow. */
120
- abort?: string | {
121
- reason: string;
122
- clearSession?: boolean;
123
- };
124
- /** Reset the current flow (or jump to a step within it). */
125
- reset?: true | {
126
- step?: string;
127
- clearData?: boolean;
128
- reason?: string;
129
- };
130
- /** Verbatim reply text to send to the user. */
131
- reply?: string;
132
- /** Partial context update applied before the next turn. */
133
- contextUpdate?: Partial<TContext>;
134
- /** Partial data update applied before the next turn. */
135
- dataUpdate?: Partial<TData>;
136
- /**
137
- * Sentences to append to the system prompt for THIS turn only.
138
- * Wired through PromptComposer's per-turn appendage slot.
139
- *
140
- * Only meaningful in pre-LLM hooks (`onEnter`, `prepare`). Ignored with
141
- * a WARN log when emitted from post-LLM hooks or persisted.
142
- */
143
- appendPrompt?: string[];
144
- /**
145
- * Tools available for THIS turn only. Stacked on top of agent/flow/step
146
- * tool scopes via ToolManager's transient layer.
147
- *
148
- * Only meaningful in pre-LLM hooks (`onEnter`, `prepare`). Ignored with
149
- * a WARN log when emitted from post-LLM hooks or persisted.
150
- */
151
- injectTools?: Tool[];
26
+ export type Next<D = unknown> = string | {
27
+ step: string;
28
+ clear?: (keyof D & string)[];
29
+ } | {
30
+ flow: Template;
31
+ input?: unknown;
32
+ };
33
+ export type ScalarType = "string" | "number" | "integer" | "boolean";
34
+ export interface ScalarDef<T extends ScalarType = ScalarType> {
35
+ type: T;
36
+ description?: string;
37
+ enum?: readonly (string | number)[];
38
+ }
39
+ /** One collectable field, authored once on the agent. */
40
+ export interface FieldDef<T extends ScalarType = ScalarType> extends ScalarDef<T> {
41
+ /** How the AI should ask for this field when a step collects it. */
42
+ ask?: string;
152
43
  /**
153
- * If true, skip the LLM call entirely this turn.
154
- *
155
- * Co-validates with `reply`: when both are set, `reply` becomes the
156
- * assistant output (`stoppedReason: 'reply'`). When `halt` is true
157
- * without `reply`, the turn produces an empty assistant message
158
- * (`stoppedReason: 'halt'`).
159
- *
160
- * Only meaningful in pre-LLM hooks (`onEnter`, `prepare`). Ignored with
161
- * a WARN log when emitted from post-LLM hooks or persisted.
44
+ * Where the value may be harvested from. `'anywhere'` (default for strings
45
+ * and numbers): any lead message. `'asked'` (default for booleans): only the
46
+ * reply to the step that lists the field, so a stray "sim" never opens a gate.
162
47
  */
163
- halt?: boolean;
48
+ extract?: "anywhere" | "asked";
164
49
  }
165
- /**
166
- * Context passed to a `BranchPredicate` function.
167
- *
168
- * **Typing note:** `data` is `Partial<TData>` — predicates must null-check
169
- * any field not declared in the source step's `requires`. Fields covered by
170
- * `requires` are guaranteed present by the engine when the predicate runs;
171
- * everything else is `T | undefined`.
172
- */
173
- export interface BranchPredicateContext<TContext = unknown, TData = unknown> {
174
- /** Collected data (partial — null-check fields not in `requires`). */
175
- data: Partial<TData>;
176
- /** Agent-level context. */
177
- context: TContext;
178
- /** Full session state. */
179
- session: SessionState<TData>;
180
- /** Conversation history as events. */
181
- history: Event[];
50
+ export type FieldDefs = Record<string, FieldDef>;
51
+ /** One parameter of a host action. All parameters are required unless `optional: true`. */
52
+ export type ParamDef = (ScalarDef & {
53
+ optional?: true;
54
+ }) | {
55
+ type: "array";
56
+ items: ScalarDef;
57
+ description?: string;
58
+ optional?: true;
59
+ };
60
+ export type ParamDefs = Record<string, ParamDef>;
61
+ type Simplify<T> = {
62
+ [K in keyof T]: T[K];
63
+ };
64
+ type TypeValue<T extends ScalarType> = T extends "string" ? string : T extends "boolean" ? boolean : number;
65
+ type ScalarValue<Def extends ScalarDef> = Def extends {
66
+ enum: readonly (infer E)[];
67
+ } ? E : TypeValue<Def["type"]>;
68
+ type ParamValue<Def extends ParamDef> = Def extends {
69
+ type: "array";
70
+ items: infer I extends ScalarDef;
71
+ } ? ScalarValue<I>[] : Def extends ScalarDef ? ScalarValue<Def> : never;
72
+ type OptionalKeys<P extends ParamDefs> = {
73
+ [K in keyof P]: P[K] extends {
74
+ optional: true;
75
+ } ? K : never;
76
+ }[keyof P];
77
+ /** The collected-data type of a set of fields. Enums become literal unions. */
78
+ export type InferData<F extends FieldDefs> = Simplify<{
79
+ [K in keyof F]: ScalarValue<F[K]>;
80
+ }>;
81
+ /** The `with` shape of a host action, from its parameter definitions. */
82
+ export type InferParams<P extends ParamDefs> = Simplify<{
83
+ [K in Exclude<keyof P, OptionalKeys<P>>]: ParamValue<P[K]>;
84
+ } & {
85
+ [K in OptionalKeys<P>]?: ParamValue<P[K]>;
86
+ }>;
87
+ /** What a code predicate sees. `run` is the run about to move or start. */
88
+ export interface PredCtx<C = unknown, D = unknown, P = unknown> {
89
+ context: C;
90
+ data: Partial<D>;
91
+ input: P;
92
+ /** The run being judged. Absent only for an agent-level instruction while the idle speaker answers. */
93
+ run?: Run;
94
+ /** The host's reason the assistant cannot speak right now, when it cannot. */
95
+ silenced?: string;
96
+ now: Date;
182
97
  }
183
98
  /**
184
- * A code predicate for branch evaluation. Returns `true` to pass, `false`
185
- * to skip the entry. May be async.
186
- *
187
- * Predicates are evaluated **before** any AI condition (`when`) on the same
188
- * entry — code-first evaluation saves tokens when the predicate fails.
189
- */
190
- export type BranchPredicate<TContext = unknown, TData = unknown> = (ctx: BranchPredicateContext<TContext, TData>) => boolean | Promise<boolean>;
191
- /**
192
- * A single entry in a `BranchMap`. Evaluated in declaration order; the first
193
- * entry whose conditions pass wins.
194
- *
195
- * **Resolution rules for `then`:**
196
- * 1. String matching a step id in the current flow → enter that step.
197
- * 2. String matching a flow id/title in the agent → treated as
198
- * `applyDirective({ goTo: <string> })`.
199
- * 3. `Directive` object → applied via `applyDirective` directly.
200
- *
201
- * An entry with neither `when` nor `if` is an unconditional fallback and
202
- * is only legal as the **last** entry in the array.
99
+ * A named host condition, used by name (with its argument) in JSON specs.
100
+ * Declared as a method so a condition typed for one argument fits the map;
101
+ * the argument arrives from JSON unvalidated, so the check should test it.
203
102
  */
204
- export interface BranchEntry<TContext = unknown, TData = unknown> {
205
- /**
206
- * AI-evaluated condition. Non-`!` strings are OR alternatives; `!` strings
207
- * are OR exclusions where any match inhibits the branch.
208
- * Costs LLM tokens. Reuses the same machinery as `step.when`.
209
- * Only evaluated if `if` passes (or is absent) — code-first short-circuit.
210
- */
211
- when?: string | string[];
212
- /**
213
- * Code predicate. Function or array of functions (AND semantics).
214
- * Free to evaluate. When both `when` and `if` are set, `if` runs first;
215
- * `when` is only evaluated if all `if` predicates pass (token-saving).
216
- */
217
- if?: BranchPredicate<TContext, TData> | BranchPredicate<TContext, TData>[];
218
- /**
219
- * Where to go when this entry matches.
220
- * - **String:** step id in the current flow, or flow id/title for cross-flow.
221
- * - **Directive:** full programmatic transition (cross-flow with data,
222
- * complete, abort, reset, etc.).
223
- */
224
- then: string | Directive<TContext, TData>;
225
- /** Optional label for event traces and flow visualization. */
226
- label?: string;
103
+ export interface Condition<C = unknown, D = unknown, Arg = unknown> {
104
+ check(ctx: PredCtx<C, D>, arg: Arg): boolean;
227
105
  }
106
+ export type ConditionMap<C = unknown, D = unknown> = Record<string, Condition<C, D>>;
228
107
  /**
229
- * Array of branch entries evaluated in declaration order.
230
- * First entry whose conditions pass wins. This is the explicit, source-local
231
- * fork primitive — all possible paths from a step are visible in one list.
108
+ * The JSON form of a predicate. Every listed entry must hold. Built-ins:
109
+ * `equals` (fields equal the given values), `known` (fields are known),
110
+ * `silenced` (the host gate is closed). Any other key names one of the
111
+ * agent's conditions and carries its argument.
232
112
  */
233
- export type BranchMap<TContext = unknown, TData = unknown> = Array<BranchEntry<TContext, TData>>;
234
- /**
235
- * Reference to a flow
236
- */
237
- export interface FlowRef {
238
- /** Flow identifier */
239
- id: string;
113
+ export interface ConditionSpec<D = unknown> {
114
+ equals?: Partial<D>;
115
+ known?: (keyof D & string)[];
116
+ silenced?: boolean;
117
+ [condition: string]: unknown;
240
118
  }
119
+ /** A code predicate (free) or its JSON form. */
120
+ export type Pred<C = unknown, D = unknown, P = unknown> = ((ctx: PredCtx<C, D, P>) => boolean) | ConditionSpec<D>;
121
+ /** A fork judged while a step is asking: `when` by the AI, `if` by code. */
122
+ export type Branch<C = unknown, D = unknown> = {
123
+ then: Next<D>;
124
+ } & ({
125
+ when: string;
126
+ } | {
127
+ if: Pred<C, D>;
128
+ });
241
129
  /**
242
- * Reference to a step within a flow
130
+ * When a run starts. `repeat` defaults to `'once'` for message, mention and
131
+ * silence triggers and to `'always'` for events.
243
132
  */
244
- export interface StepRef {
245
- /** Step identifier */
246
- id: string;
247
- /** Flow this step belongs to */
248
- flowId: string;
133
+ export type Trigger<C = unknown, D = unknown> = {
134
+ repeat?: Repeat;
135
+ } & (
136
+ /** The lead asks for this; the run takes the conversation. `[]` = catch-all. */
137
+ {
138
+ message: string[];
139
+ if?: Pred<C, D>;
249
140
  }
250
- /**
251
- * Flow lifecycle hooks for managing flow-specific data and behavior.
252
- *
253
- * Pre-LLM hooks (`onEnter`) return `void | Directive`.
254
- * Post-LLM hooks (`onComplete`) return `void | Directive`.
255
- * Informational hooks (`onExit`) return `void`.
256
- * Data hooks (`onDataUpdate`, `onContextUpdate`) retain their v1 signatures.
257
- */
258
- export interface FlowLifecycleHooks<TContext = unknown, TData = unknown> {
259
- /**
260
- * Called when the flow is first entered.
261
- * May return a Directive to augment the prompt, inject tools, halt, or redirect.
262
- * Pre-LLM fields (`appendPrompt`, `injectTools`, `halt`) are honored here.
263
- */
264
- onEnter?: (ctx: HookContext<TContext, TData>) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
265
- /**
266
- * Called when the flow is exited. Informational only — cannot influence flow control.
267
- * Receives the reason the flow was exited.
268
- */
269
- onExit?: (ctx: HookContext<TContext, TData>, reason: ExitReason) => void | Promise<void>;
270
- /**
271
- * Called when the flow's required fields are satisfied or when a `complete` directive fires.
272
- * May return a Directive to chain into another flow or perform state writes.
273
- */
274
- onComplete?: (ctx: HookContext<TContext, TData>) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
275
- /**
276
- * Called after collected data is updated for this flow (from AI response or tool execution)
277
- * Useful for validation, enrichment, or persistence of flow-specific collected data
278
- * Return modified collected data or the same data to keep it unchanged
279
- *
280
- * Unlike Agent-level onDataUpdate, this only triggers for data changes in this specific flow.
281
- */
282
- onDataUpdate?: (data: Partial<TData>, previousCollected: Partial<TData>) => Partial<TData> | Promise<Partial<TData>>;
283
- /**
284
- * Called after context is updated via updateContext() when this flow is active
285
- * Useful for flow-specific context reactions, validation, or side effects
286
- *
287
- * Unlike Agent-level onContextUpdate, this only triggers when this specific flow is active.
288
- */
289
- onContextUpdate?: (newContext: TContext, previousContext: TContext) => void | Promise<void>;
141
+ /** The lead mentions this; the run reacts beside the conversation. `[]` + `if` = code-only. */
142
+ | {
143
+ mention: string[];
144
+ extract?: ParamDefs;
145
+ if?: Pred<C, D>;
290
146
  }
291
- /**
292
- * Options for creating a flow
293
- * @template TData - Type of data collected throughout the flow (inferred from schema)
294
- */
295
- export interface FlowOptions<TContext = unknown, TData = unknown> {
296
- /** Custom ID for the flow (optional - will generate deterministic ID from title if not provided) */
147
+ /** The lead has been quiet since the assistant last spoke. */
148
+ | {
149
+ silence: Duration;
150
+ if?: Pred<C, D>;
151
+ businessHours?: boolean;
152
+ }
153
+ /** The host reported an event; its payload is the run's `input`. */
154
+ | {
155
+ event: string;
156
+ if?: Pred<C, D>;
157
+ after?: Duration;
158
+ businessHours?: boolean;
159
+ });
160
+ /** A behavioural statement rendered into the prompt while it applies. */
161
+ export interface Instruction<C = unknown, D = unknown> {
297
162
  id?: string;
298
- /** Title of the flow */
299
- title: string;
300
- /** Description of what this flow accomplishes */
301
- description?: string;
302
- /**
303
- * AI-evaluated activation condition(s). Non-`!` strings are OR alternatives;
304
- * `!` strings are OR exclusions where any match inhibits activation.
305
- * Costs LLM tokens. Functions are NOT allowed here — use `if` for code predicates.
306
- */
307
- when?: ConditionWhen;
308
- /**
309
- * Code-evaluated activation condition(s). Function or array of functions (AND semantics).
310
- * Free to evaluate. When both `when` and `if` are set, `if` runs first;
311
- * `when` is only evaluated when `if` passes.
312
- */
313
- if?: ConditionIf<TContext, TData>;
314
- /**
315
- * Instructions for this flow.
316
- */
317
- instructions?: Instruction<TContext, TData>[];
318
- /** Tools available in this flow */
319
- tools?: (string | Tool<TContext, TData>)[];
320
- /** Optional: extractions the router may return (added to routing schema) */
321
- routingExtrasSchema?: StructuredSchema;
322
- /** Optional: structured response data for this flow's message generation */
323
- responseOutputSchema?: StructuredSchema;
324
- /**
325
- * Required fields for flow completion - must be valid keys from agent's TData type
326
- * Flow is considered complete when all required fields are present in agent data
327
- */
328
- requiredFields?: (keyof TData)[];
329
- /**
330
- * Optional fields that enhance the flow but aren't required for completion
331
- * Must be valid keys from agent's TData type
332
- */
333
- optionalFields?: (keyof TData)[];
334
- /**
335
- * Initial data to pre-populate when entering this flow
336
- * Useful for restoring sessions or pre-filling known information
337
- * Steps with skip conditions will be automatically bypassed if data is present
338
- * Now refers to agent-level data
339
- */
340
- initialData?: Partial<TData>;
341
- /**
342
- * Sequential steps for simple linear flows
343
- * If provided, automatically chains the steps from initialStep
344
- * The last step in the array is the implicit terminus of the flow
345
- * For complex flows with branching, build the step machine manually instead
346
- */
347
- steps?: StepOptions<TContext, TData>[];
348
- /**
349
- * Optional transition when the flow completes (last step finishes).
350
- *
351
- * Accepts a flow ID or title string — sugar for
352
- * `hooks.onComplete = () => ({ goTo: '<id>' })`.
353
- *
354
- * For dynamic completion logic, use `hooks.onComplete` instead.
355
- * Setting both `onComplete` and `hooks.onComplete` on the same flow
356
- * throws `FlowConfigurationError` at construction time.
357
- *
358
- * @example
359
- * // Simple string — transitions to "Collect Feedback" on completion
360
- * onComplete: "Collect Feedback"
361
- */
362
- onComplete?: string;
363
- /**
364
- * If true, this flow can be re-selected by the router after it has
365
- * completed in the current session — useful for "do another?" patterns
366
- * (re-book, re-search, repeat-task).
367
- *
368
- * Default: `false`. Once a flow completes (and `onComplete` is not set
369
- * or returns `undefined`), the flow is excluded from routing candidates
370
- * for the rest of the session unless `reentrant: true`.
371
- *
372
- * On re-entry, the engine clears every field declared in this flow's
373
- * `requiredFields` and `optionalFields` (so the flow starts fresh from
374
- * its initial step). Fields not declared as owned by this flow are
375
- * preserved in `session.data`.
376
- *
377
- * `onComplete` always wins over `reentrant`. If `onComplete` returns a
378
- * target flow, the session transitions there immediately on completion;
379
- * `reentrant` is consulted only when `onComplete` is absent or returns
380
- * `undefined`.
381
- */
382
- reentrant?: boolean;
383
- /**
384
- * Flow lifecycle hooks
385
- */
386
- hooks?: FlowLifecycleHooks<TContext, TData>;
163
+ /** `'must'` always, `'never'` a prohibition, `'should'` (default) a nudge. */
164
+ kind?: "must" | "never" | "should";
165
+ /** AI-judged activation, rendered into the prompt. */
166
+ when?: string | string[];
167
+ /** Code-judged activation, free. */
168
+ if?: Pred<C, D>;
169
+ prompt: Template;
387
170
  }
388
- /**
389
- * Step lifecycle hooks for managing step-specific behavior. Desugared onto the
390
- * equivalent top-level `prepare` / `finalize` fields by the Step constructor —
391
- * both spellings run through the same lifecycle machinery, and declaring both
392
- * runs them in sequence with their returns merged (Algorithm 4).
393
- *
394
- * `prepare` runs before the AI responds; `finalize` runs after the turn's
395
- * generation completes (before persistence). Both may return a Directive to
396
- * write state or redirect flow.
397
- */
398
- export interface StepLifecycleHooks<TContext = unknown, TData = unknown> {
399
- /**
400
- * Called pre-LLM (alongside any top-level `prepare`). May return a Directive
401
- * to write state or redirect flow.
402
- */
403
- prepare?: (context: TContext, data?: Partial<TData>) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
404
- /**
405
- * Called after generation (alongside any top-level `finalize`, before
406
- * persistence). May return a Directive to redirect flow or write state.
407
- */
408
- finalize?: (context: TContext, data?: Partial<TData>) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
171
+ /** The AI talks: a guideline, fields to collect, or both. */
172
+ export type TalkStep<C = unknown, D = unknown> = ({
173
+ prompt: Template;
174
+ collect?: (keyof D & string)[];
175
+ } | {
176
+ collect: (keyof D & string)[];
177
+ prompt?: Template;
178
+ }) & {
179
+ /** Per-flow wording for a field; the field's own `ask` is the default. */
180
+ ask?: Partial<Record<keyof D & string, string>>;
181
+ /** Times a field may be asked before it is skipped. Default 3. */
182
+ maxAsks?: number;
183
+ branches?: Branch<C, D>[];
184
+ tools?: string[];
185
+ instructions?: Instruction<C, D>[];
186
+ };
187
+ /** A fixed message goes out, verbatim. */
188
+ export interface SayStep {
189
+ say: Template;
190
+ media?: {
191
+ slug: string;
192
+ };
193
+ /** Send at most once per session. */
194
+ once?: boolean;
409
195
  }
410
- /**
411
- * Specification for a step transition
412
- */
413
- export interface StepOptions<TContext = unknown, TData = unknown> {
414
- /** Custom ID for this step (optional - will generate deterministic ID if not provided) */
415
- id?: string;
416
- /** Description of the transition */
417
- description?: string;
418
- /** Transition to a chat state with this description */
419
- prompt?: Template<TContext, TData>;
420
- /** Tools available for AI to call in this step (by ID reference or inline definition) */
421
- tools?: (string | Tool<TContext, TData>)[];
422
- /** Programmatic function or tool to run before AI responds */
423
- prepare?: string | Tool<TContext, TData> | ((context: TContext, data?: Partial<TData>) => void | PrepareResult<TContext, TData> | Promise<void | PrepareResult<TContext, TData>>);
424
- /** Programmatic function or tool to run after AI responds */
425
- finalize?: string | Tool<TContext, TData> | ((context: TContext, data?: Partial<TData>) => void | PrepareResult<TContext, TData> | Promise<void | PrepareResult<TContext, TData>>);
426
- /**
427
- * Fields to collect from the conversation in this step
428
- * These should match keys in the agent's TData schema
429
- */
430
- collect?: (keyof TData)[];
431
- /**
432
- * Code-evaluated skip condition. If evaluates to true, the step will be bypassed.
433
- * Function or array of functions (OR semantics) — if any returns true, step is skipped.
434
- * Only code predicates are allowed here (no AI strings).
435
- *
436
- * Renamed from v1 `skipIf` to clarify the if-only shape.
437
- */
438
- skip?: ConditionIf<TContext, TData>;
439
- /**
440
- * Required data fields that must be present before entering this step
441
- * If any required field is missing, step cannot be entered
442
- * Must be valid keys from agent's TData type
443
- */
444
- requires?: (keyof TData)[];
445
- /**
446
- * AI-evaluated activation condition(s). Non-`!` strings are OR alternatives;
447
- * `!` strings are OR exclusions where any match inhibits activation.
448
- * Costs LLM tokens. Functions are NOT allowed here — use `if` for code predicates.
449
- */
450
- when?: ConditionWhen;
451
- /**
452
- * Code-evaluated activation condition(s). Function or array of functions (AND semantics).
453
- * Free to evaluate. When both `when` and `if` are set, `if` runs first;
454
- * `when` is only evaluated when `if` passes.
455
- */
456
- if?: ConditionIf<TContext, TData>;
457
- /**
458
- * Instructions for this step. Replaces v1's `guidelines`.
459
- */
460
- instructions?: Instruction<TContext, TData>[];
461
- /**
462
- * If true, this step runs without an LLM call. Pre-LLM hooks
463
- * (`onEnter`, `prepare`) and branch resolution still execute.
464
- *
465
- * An auto-step **cannot** define `prompt`, `collect`, `tools`, or `finalize`.
466
- * Defining any of these will throw `FlowConfigurationError` at construction time.
467
- *
468
- * Use auto-steps for:
469
- * - Data enrichment between user-facing steps (CRM lookup, pricing calc).
470
- * - Deterministic decision points (pair with `branches`).
471
- * - Programmatic short-circuits (`prepare` returning `{ halt: true, reply }`).
472
- *
473
- * @default false
474
- */
475
- auto?: boolean;
476
- /**
477
- * Verbatim assistant output for this step. When set, the template is
478
- * rendered via the same engine as `prompt` and emitted as the assistant
479
- * message without invoking the LLM.
480
- *
481
- * A reply step **cannot** define `prompt`, `collect`, `tools`, `finalize`,
482
- * or `auto: true`. Defining any of these will throw `FlowConfigurationError`
483
- * at construction time.
484
- *
485
- * `onEnter` and `prepare` hooks fire normally before the reply is rendered.
486
- * If `prepare` returns a Directive with its own `reply` field, the
487
- * hook-emitted reply wins (last-emission-wins per Algorithm 4).
488
- *
489
- * After emission, `onExit` fires and `branches` are resolved for next step.
490
- * `stoppedReason: 'reply'`.
491
- *
492
- * Use reply steps for: confirmations, hand-offs, farewells, acks — anything
493
- * that doesn't need LLM reasoning.
494
- */
495
- reply?: Template<TContext, TData>;
496
- /**
497
- * Explicit source-local fork. An array of branch entries evaluated in
498
- * declaration order — the first entry whose conditions pass wins; its
499
- * `then` resolves to the next step or a Directive.
500
- *
501
- * Coexists with `nextStep`. If `branches` is absent or no entry matches,
502
- * resolution falls through to linear nextStep / AI step selection.
503
- *
504
- * Runs **after** the step's post-LLM phase (tool execution, `finalize`)
505
- * and **before** linear successor selection.
506
- */
507
- branches?: BranchMap<TContext, TData>;
508
- /**
509
- * Step lifecycle hooks — an alternative spelling of the top-level
510
- * `prepare` / `finalize` fields. Both spellings run when declared together;
511
- * returns are merged via Algorithm 4.
512
- */
513
- hooks?: StepLifecycleHooks<TContext, TData>;
196
+ /** The host does something. `with` is checked against the action's parameters. */
197
+ export interface DoStep<D = unknown> {
198
+ do: string;
199
+ with?: Record<string, unknown>;
200
+ /** Where to go when the action reports `failed`. Default: continue. */
201
+ onFail?: Next<D>;
514
202
  }
515
- /**
516
- * Specification for a branch in the conversation flow
517
- */
518
- export interface BranchSpec<TContext = unknown, TData = unknown> {
519
- /** User-friendly identifier for this branch (used as object key) */
203
+ /** Park for a while. `then` = the time passed; `else` = the lead replied first. */
204
+ export interface WaitStep<C = unknown, D = unknown> {
205
+ wait: Duration;
206
+ businessHours?: boolean;
207
+ else?: Next<D>;
208
+ branches?: Branch<C, D>[];
209
+ }
210
+ /** Park until an event. `then` = it came; `else` = `upTo` passed (default 30 days). */
211
+ export interface WaitEventStep<D = unknown> {
212
+ wait: {
213
+ event: string;
214
+ upTo?: Duration;
215
+ };
216
+ else?: Next<D>;
217
+ }
218
+ /** The code forks. `then` = true; `else` = false (default `'end'`). */
219
+ export interface IfStep<C = unknown, D = unknown> {
220
+ if: Pred<C, D>;
221
+ else?: Next<D>;
222
+ }
223
+ export interface StepBase<D = unknown> {
224
+ /** Required, unique inside the flow, never `'end'`. */
225
+ id: string;
226
+ label?: string;
227
+ then?: Next<D>;
228
+ /** Editor-only data the framework carries but never reads. */
229
+ ui?: Record<string, unknown>;
230
+ }
231
+ export type Step<C = unknown, D = unknown> = StepBase<D> & (TalkStep<C, D> | SayStep | DoStep<D> | WaitStep<C, D> | WaitEventStep<D> | IfStep<C, D>);
232
+ export interface Flow<C = unknown, D = unknown> {
233
+ id: string;
520
234
  name: string;
521
- /** Optional ID for this branch (auto-generated if not provided) */
522
- id?: string;
523
- /** Step configuration for this branch */
524
- step: StepOptions<TContext, TData>;
235
+ /** When this flow should be used; the AI reads it when routing. */
236
+ description?: string;
237
+ /** Absent or empty: the flow only starts when the host calls `start`. */
238
+ on?: Trigger<C, D>[];
239
+ /** `'session'` (default) or a host anchor name such as `'lead'`. */
240
+ anchor?: string;
241
+ /** Re-checked whenever the run moves. Default: the trigger's `if`. */
242
+ while?: Pred<C, D>;
243
+ clearOnStart?: (keyof D & string)[];
244
+ steps: Step<C, D>[];
245
+ /** What the run does after its last step. Default `'end'`. */
246
+ onEnd?: "end" | "stay" | "reset";
247
+ instructions?: Instruction<C, D>[];
248
+ tools?: string[];
525
249
  }
526
- /**
527
- * Result of a branch operation
528
- * Maps branch names to their respective step results for continued chaining
529
- */
530
- export interface BranchResult<TContext = unknown, TData = unknown> {
531
- [branchName: string]: StepResult<TContext, TData>;
250
+ export type ActionResult = {
251
+ ok: true;
252
+ detail?: string;
253
+ spoke?: true;
254
+ } | {
255
+ skipped: string;
256
+ } | {
257
+ failed: string;
258
+ } | {
259
+ defer: Duration;
260
+ detail: string;
261
+ };
262
+ /** What a host action sees. Handlers run at-least-once; make them idempotent on `key`. */
263
+ export interface ActionCtx<C = unknown, D = unknown> {
264
+ context: C;
265
+ data: Partial<D>;
266
+ input: unknown;
267
+ run: Run;
268
+ /** `${runId}:${stepId}:${visit}`; the same on a replay of the same input. */
269
+ key: string;
270
+ /** `${flowId}:${anchor}:${nonce}`; shared across a lead's sessions. */
271
+ dedupeKey: string;
272
+ silenced?: string;
273
+ now: Date;
274
+ /** Write collected data from inside the action. */
275
+ set(patch: Partial<D>): void;
532
276
  }
533
- /**
534
- * Result of a transition operation
535
- * Combines step reference with the ability to chain transitions and create branches
536
- */
537
- export interface StepResult<TContext = unknown, TData = unknown> extends StepRef {
538
- /** Allow chaining transitions */
539
- nextStep: (spec: StepOptions<TContext, TData>) => StepResult<TContext, TData>;
540
- /** Create multiple branches from this step */
541
- branch: (branches: BranchSpec<TContext, TData>[]) => BranchResult<TContext, TData>;
277
+ export interface Action<C = unknown, D = unknown, P = Record<string, unknown>> {
278
+ description?: string;
279
+ parameters: ParamDefs;
280
+ run(params: P, ctx: ActionCtx<C, D>): ActionResult | Promise<ActionResult>;
281
+ }
282
+ export type ActionMap<C = unknown, D = unknown> = Record<string, Action<C, D>>;
283
+ /** A host event the agent may react to. `P` is the payload type. */
284
+ export interface EventDef<P = unknown> {
285
+ /**
286
+ * `'inbound'` counts as the lead speaking (resolves reply waits);
287
+ * `'outbound'` as the assistant speaking (re-arms silence).
288
+ */
289
+ direction?: "inbound" | "outbound";
290
+ /** Phantom: the payload type, for inference. Never set at runtime. */
291
+ readonly payload?: P;
542
292
  }
293
+ export type EventMap = Record<string, EventDef>;
294
+ export {};
543
295
  //# sourceMappingURL=flow.d.ts.map