@falai/agent 1.2.8 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (522) hide show
  1. package/README.md +40 -886
  2. package/dist/adapters/MemoryAdapter.js +2 -2
  3. package/dist/adapters/MemoryAdapter.js.map +1 -1
  4. package/dist/adapters/MongoAdapter.js +2 -2
  5. package/dist/adapters/MongoAdapter.js.map +1 -1
  6. package/dist/adapters/OpenSearchAdapter.d.ts.map +1 -1
  7. package/dist/adapters/OpenSearchAdapter.js +9 -7
  8. package/dist/adapters/OpenSearchAdapter.js.map +1 -1
  9. package/dist/adapters/PostgreSQLAdapter.d.ts +14 -0
  10. package/dist/adapters/PostgreSQLAdapter.d.ts.map +1 -1
  11. package/dist/adapters/PostgreSQLAdapter.js +25 -9
  12. package/dist/adapters/PostgreSQLAdapter.js.map +1 -1
  13. package/dist/adapters/PrismaAdapter.js +5 -5
  14. package/dist/adapters/PrismaAdapter.js.map +1 -1
  15. package/dist/adapters/RedisAdapter.js +2 -2
  16. package/dist/adapters/RedisAdapter.js.map +1 -1
  17. package/dist/adapters/SQLiteAdapter.d.ts +17 -0
  18. package/dist/adapters/SQLiteAdapter.d.ts.map +1 -1
  19. package/dist/adapters/SQLiteAdapter.js +30 -11
  20. package/dist/adapters/SQLiteAdapter.js.map +1 -1
  21. package/dist/cjs/adapters/MemoryAdapter.js +2 -2
  22. package/dist/cjs/adapters/MemoryAdapter.js.map +1 -1
  23. package/dist/cjs/adapters/MongoAdapter.js +2 -2
  24. package/dist/cjs/adapters/MongoAdapter.js.map +1 -1
  25. package/dist/cjs/adapters/OpenSearchAdapter.d.ts.map +1 -1
  26. package/dist/cjs/adapters/OpenSearchAdapter.js +9 -7
  27. package/dist/cjs/adapters/OpenSearchAdapter.js.map +1 -1
  28. package/dist/cjs/adapters/PostgreSQLAdapter.d.ts +14 -0
  29. package/dist/cjs/adapters/PostgreSQLAdapter.d.ts.map +1 -1
  30. package/dist/cjs/adapters/PostgreSQLAdapter.js +25 -9
  31. package/dist/cjs/adapters/PostgreSQLAdapter.js.map +1 -1
  32. package/dist/cjs/adapters/PrismaAdapter.js +5 -5
  33. package/dist/cjs/adapters/PrismaAdapter.js.map +1 -1
  34. package/dist/cjs/adapters/RedisAdapter.js +2 -2
  35. package/dist/cjs/adapters/RedisAdapter.js.map +1 -1
  36. package/dist/cjs/adapters/SQLiteAdapter.d.ts +17 -0
  37. package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +1 -1
  38. package/dist/cjs/adapters/SQLiteAdapter.js +30 -11
  39. package/dist/cjs/adapters/SQLiteAdapter.js.map +1 -1
  40. package/dist/cjs/constants/index.d.ts +0 -9
  41. package/dist/cjs/constants/index.d.ts.map +1 -1
  42. package/dist/cjs/constants/index.js +2 -11
  43. package/dist/cjs/constants/index.js.map +1 -1
  44. package/dist/cjs/core/Agent.d.ts +119 -153
  45. package/dist/cjs/core/Agent.d.ts.map +1 -1
  46. package/dist/cjs/core/Agent.js +471 -324
  47. package/dist/cjs/core/Agent.js.map +1 -1
  48. package/dist/cjs/core/AutoChainExecutor.d.ts +107 -0
  49. package/dist/cjs/core/AutoChainExecutor.d.ts.map +1 -0
  50. package/dist/cjs/core/AutoChainExecutor.js +297 -0
  51. package/dist/cjs/core/AutoChainExecutor.js.map +1 -0
  52. package/dist/cjs/core/BranchEvaluator.d.ts +54 -0
  53. package/dist/cjs/core/BranchEvaluator.d.ts.map +1 -0
  54. package/dist/cjs/core/BranchEvaluator.js +130 -0
  55. package/dist/cjs/core/BranchEvaluator.js.map +1 -0
  56. package/dist/cjs/core/DirectiveBus.d.ts +88 -0
  57. package/dist/cjs/core/DirectiveBus.d.ts.map +1 -0
  58. package/dist/cjs/core/DirectiveBus.js +196 -0
  59. package/dist/cjs/core/DirectiveBus.js.map +1 -0
  60. package/dist/cjs/core/DirectiveChainTracker.d.ts +49 -0
  61. package/dist/cjs/core/DirectiveChainTracker.d.ts.map +1 -0
  62. package/dist/cjs/core/DirectiveChainTracker.js +121 -0
  63. package/dist/cjs/core/DirectiveChainTracker.js.map +1 -0
  64. package/dist/cjs/core/Flow.d.ts +186 -0
  65. package/dist/cjs/core/Flow.d.ts.map +1 -0
  66. package/dist/cjs/core/Flow.js +550 -0
  67. package/dist/cjs/core/Flow.js.map +1 -0
  68. package/dist/cjs/core/FlowRouter.d.ts +182 -0
  69. package/dist/cjs/core/FlowRouter.d.ts.map +1 -0
  70. package/dist/cjs/core/{RoutingEngine.js → FlowRouter.js} +323 -306
  71. package/dist/cjs/core/FlowRouter.js.map +1 -0
  72. package/dist/cjs/core/PersistenceManager.d.ts +2 -2
  73. package/dist/cjs/core/PersistenceManager.d.ts.map +1 -1
  74. package/dist/cjs/core/PersistenceManager.js +7 -7
  75. package/dist/cjs/core/PersistenceManager.js.map +1 -1
  76. package/dist/cjs/core/PromptComposer.d.ts +21 -8
  77. package/dist/cjs/core/PromptComposer.d.ts.map +1 -1
  78. package/dist/cjs/core/PromptComposer.js +182 -105
  79. package/dist/cjs/core/PromptComposer.js.map +1 -1
  80. package/dist/cjs/core/PromptSectionCache.d.ts +1 -1
  81. package/dist/cjs/core/PromptSectionCache.js +1 -1
  82. package/dist/cjs/core/ResponseEngine.d.ts +18 -8
  83. package/dist/cjs/core/ResponseEngine.d.ts.map +1 -1
  84. package/dist/cjs/core/ResponseEngine.js +38 -36
  85. package/dist/cjs/core/ResponseEngine.js.map +1 -1
  86. package/dist/cjs/core/ResponseModal.d.ts +73 -56
  87. package/dist/cjs/core/ResponseModal.d.ts.map +1 -1
  88. package/dist/cjs/core/ResponseModal.js +1191 -1014
  89. package/dist/cjs/core/ResponseModal.js.map +1 -1
  90. package/dist/cjs/core/ResponsePipeline.d.ts +124 -26
  91. package/dist/cjs/core/ResponsePipeline.d.ts.map +1 -1
  92. package/dist/cjs/core/ResponsePipeline.js +509 -136
  93. package/dist/cjs/core/ResponsePipeline.js.map +1 -1
  94. package/dist/cjs/core/SignalEvaluator.d.ts +86 -0
  95. package/dist/cjs/core/SignalEvaluator.d.ts.map +1 -0
  96. package/dist/cjs/core/SignalEvaluator.js +333 -0
  97. package/dist/cjs/core/SignalEvaluator.js.map +1 -0
  98. package/dist/cjs/core/SignalProcessor.d.ts +152 -0
  99. package/dist/cjs/core/SignalProcessor.d.ts.map +1 -0
  100. package/dist/cjs/core/SignalProcessor.js +562 -0
  101. package/dist/cjs/core/SignalProcessor.js.map +1 -0
  102. package/dist/cjs/core/Step.d.ts +43 -32
  103. package/dist/cjs/core/Step.d.ts.map +1 -1
  104. package/dist/cjs/core/Step.js +221 -126
  105. package/dist/cjs/core/Step.js.map +1 -1
  106. package/dist/cjs/core/StreamingToolExecutor.d.ts +2 -2
  107. package/dist/cjs/core/StreamingToolExecutor.d.ts.map +1 -1
  108. package/dist/cjs/core/StreamingToolExecutor.js.map +1 -1
  109. package/dist/cjs/core/ToolManager.d.ts +44 -13
  110. package/dist/cjs/core/ToolManager.d.ts.map +1 -1
  111. package/dist/cjs/core/ToolManager.js +174 -91
  112. package/dist/cjs/core/ToolManager.js.map +1 -1
  113. package/dist/cjs/core/createAgent.d.ts +35 -0
  114. package/dist/cjs/core/createAgent.d.ts.map +1 -0
  115. package/dist/cjs/core/createAgent.js +39 -0
  116. package/dist/cjs/core/createAgent.js.map +1 -0
  117. package/dist/cjs/core/flow-namespace.d.ts +49 -0
  118. package/dist/cjs/core/flow-namespace.d.ts.map +1 -0
  119. package/dist/cjs/core/flow-namespace.js +171 -0
  120. package/dist/cjs/core/flow-namespace.js.map +1 -0
  121. package/dist/cjs/index.d.ts +11 -14
  122. package/dist/cjs/index.d.ts.map +1 -1
  123. package/dist/cjs/index.js +18 -22
  124. package/dist/cjs/index.js.map +1 -1
  125. package/dist/cjs/providers/AnthropicProvider.d.ts +1 -1
  126. package/dist/cjs/providers/AnthropicProvider.js +1 -1
  127. package/dist/cjs/providers/GeminiProvider.d.ts +1 -1
  128. package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
  129. package/dist/cjs/providers/GeminiProvider.js +1 -1
  130. package/dist/cjs/providers/GeminiProvider.js.map +1 -1
  131. package/dist/cjs/providers/OpenAIProvider.d.ts +1 -1
  132. package/dist/cjs/providers/OpenAIProvider.d.ts.map +1 -1
  133. package/dist/cjs/providers/OpenAIProvider.js +1 -1
  134. package/dist/cjs/providers/OpenAIProvider.js.map +1 -1
  135. package/dist/cjs/types/agent.d.ts +183 -54
  136. package/dist/cjs/types/agent.d.ts.map +1 -1
  137. package/dist/cjs/types/agent.js +0 -6
  138. package/dist/cjs/types/agent.js.map +1 -1
  139. package/dist/cjs/types/ai.d.ts +3 -3
  140. package/dist/cjs/types/ai.d.ts.map +1 -1
  141. package/dist/cjs/types/errors.d.ts +15 -0
  142. package/dist/cjs/types/errors.d.ts.map +1 -0
  143. package/dist/cjs/types/errors.js +22 -0
  144. package/dist/cjs/types/errors.js.map +1 -0
  145. package/dist/cjs/types/flow.d.ts +513 -0
  146. package/dist/cjs/types/flow.d.ts.map +1 -0
  147. package/dist/cjs/types/{route.js → flow.js} +2 -2
  148. package/dist/cjs/types/flow.js.map +1 -0
  149. package/dist/cjs/types/index.d.ts +7 -6
  150. package/dist/cjs/types/index.d.ts.map +1 -1
  151. package/dist/cjs/types/index.js +6 -2
  152. package/dist/cjs/types/index.js.map +1 -1
  153. package/dist/cjs/types/persistence.d.ts +11 -7
  154. package/dist/cjs/types/persistence.d.ts.map +1 -1
  155. package/dist/cjs/types/routing.d.ts +1 -1
  156. package/dist/cjs/types/routing.d.ts.map +1 -1
  157. package/dist/cjs/types/session.d.ts +24 -23
  158. package/dist/cjs/types/session.d.ts.map +1 -1
  159. package/dist/cjs/types/signals.d.ts +248 -0
  160. package/dist/cjs/types/signals.d.ts.map +1 -0
  161. package/dist/cjs/types/signals.js +11 -0
  162. package/dist/cjs/types/signals.js.map +1 -0
  163. package/dist/cjs/types/template.d.ts +2 -8
  164. package/dist/cjs/types/template.d.ts.map +1 -1
  165. package/dist/cjs/types/tool.d.ts +36 -29
  166. package/dist/cjs/types/tool.d.ts.map +1 -1
  167. package/dist/cjs/types/tool.js +1 -1
  168. package/dist/cjs/types/tool.js.map +1 -1
  169. package/dist/cjs/utils/condition.d.ts +7 -1
  170. package/dist/cjs/utils/condition.d.ts.map +1 -1
  171. package/dist/cjs/utils/condition.js.map +1 -1
  172. package/dist/cjs/utils/id.d.ts +13 -5
  173. package/dist/cjs/utils/id.d.ts.map +1 -1
  174. package/dist/cjs/utils/id.js +24 -10
  175. package/dist/cjs/utils/id.js.map +1 -1
  176. package/dist/cjs/utils/index.d.ts +2 -2
  177. package/dist/cjs/utils/index.d.ts.map +1 -1
  178. package/dist/cjs/utils/index.js +7 -3
  179. package/dist/cjs/utils/index.js.map +1 -1
  180. package/dist/cjs/utils/session.d.ts +44 -5
  181. package/dist/cjs/utils/session.d.ts.map +1 -1
  182. package/dist/cjs/utils/session.js +197 -38
  183. package/dist/cjs/utils/session.js.map +1 -1
  184. package/dist/constants/index.d.ts +0 -9
  185. package/dist/constants/index.d.ts.map +1 -1
  186. package/dist/constants/index.js +3 -9
  187. package/dist/constants/index.js.map +1 -1
  188. package/dist/core/Agent.d.ts +119 -153
  189. package/dist/core/Agent.d.ts.map +1 -1
  190. package/dist/core/Agent.js +472 -325
  191. package/dist/core/Agent.js.map +1 -1
  192. package/dist/core/AutoChainExecutor.d.ts +107 -0
  193. package/dist/core/AutoChainExecutor.d.ts.map +1 -0
  194. package/dist/core/AutoChainExecutor.js +293 -0
  195. package/dist/core/AutoChainExecutor.js.map +1 -0
  196. package/dist/core/BranchEvaluator.d.ts +54 -0
  197. package/dist/core/BranchEvaluator.d.ts.map +1 -0
  198. package/dist/core/BranchEvaluator.js +126 -0
  199. package/dist/core/BranchEvaluator.js.map +1 -0
  200. package/dist/core/DirectiveBus.d.ts +88 -0
  201. package/dist/core/DirectiveBus.d.ts.map +1 -0
  202. package/dist/core/DirectiveBus.js +192 -0
  203. package/dist/core/DirectiveBus.js.map +1 -0
  204. package/dist/core/DirectiveChainTracker.d.ts +49 -0
  205. package/dist/core/DirectiveChainTracker.d.ts.map +1 -0
  206. package/dist/core/DirectiveChainTracker.js +117 -0
  207. package/dist/core/DirectiveChainTracker.js.map +1 -0
  208. package/dist/core/Flow.d.ts +186 -0
  209. package/dist/core/Flow.d.ts.map +1 -0
  210. package/dist/core/Flow.js +546 -0
  211. package/dist/core/Flow.js.map +1 -0
  212. package/dist/core/FlowRouter.d.ts +182 -0
  213. package/dist/core/FlowRouter.d.ts.map +1 -0
  214. package/dist/core/{RoutingEngine.js → FlowRouter.js} +322 -305
  215. package/dist/core/FlowRouter.js.map +1 -0
  216. package/dist/core/PersistenceManager.d.ts +2 -2
  217. package/dist/core/PersistenceManager.d.ts.map +1 -1
  218. package/dist/core/PersistenceManager.js +7 -7
  219. package/dist/core/PersistenceManager.js.map +1 -1
  220. package/dist/core/PromptComposer.d.ts +21 -8
  221. package/dist/core/PromptComposer.d.ts.map +1 -1
  222. package/dist/core/PromptComposer.js +183 -106
  223. package/dist/core/PromptComposer.js.map +1 -1
  224. package/dist/core/PromptSectionCache.d.ts +1 -1
  225. package/dist/core/PromptSectionCache.js +1 -1
  226. package/dist/core/ResponseEngine.d.ts +18 -8
  227. package/dist/core/ResponseEngine.d.ts.map +1 -1
  228. package/dist/core/ResponseEngine.js +38 -36
  229. package/dist/core/ResponseEngine.js.map +1 -1
  230. package/dist/core/ResponseModal.d.ts +73 -56
  231. package/dist/core/ResponseModal.d.ts.map +1 -1
  232. package/dist/core/ResponseModal.js +1193 -1016
  233. package/dist/core/ResponseModal.js.map +1 -1
  234. package/dist/core/ResponsePipeline.d.ts +124 -26
  235. package/dist/core/ResponsePipeline.d.ts.map +1 -1
  236. package/dist/core/ResponsePipeline.js +509 -137
  237. package/dist/core/ResponsePipeline.js.map +1 -1
  238. package/dist/core/SignalEvaluator.d.ts +86 -0
  239. package/dist/core/SignalEvaluator.d.ts.map +1 -0
  240. package/dist/core/SignalEvaluator.js +326 -0
  241. package/dist/core/SignalEvaluator.js.map +1 -0
  242. package/dist/core/SignalProcessor.d.ts +152 -0
  243. package/dist/core/SignalProcessor.d.ts.map +1 -0
  244. package/dist/core/SignalProcessor.js +555 -0
  245. package/dist/core/SignalProcessor.js.map +1 -0
  246. package/dist/core/Step.d.ts +43 -32
  247. package/dist/core/Step.d.ts.map +1 -1
  248. package/dist/core/Step.js +220 -126
  249. package/dist/core/Step.js.map +1 -1
  250. package/dist/core/StreamingToolExecutor.d.ts +2 -2
  251. package/dist/core/StreamingToolExecutor.d.ts.map +1 -1
  252. package/dist/core/StreamingToolExecutor.js.map +1 -1
  253. package/dist/core/ToolManager.d.ts +44 -13
  254. package/dist/core/ToolManager.d.ts.map +1 -1
  255. package/dist/core/ToolManager.js +174 -91
  256. package/dist/core/ToolManager.js.map +1 -1
  257. package/dist/core/createAgent.d.ts +35 -0
  258. package/dist/core/createAgent.d.ts.map +1 -0
  259. package/dist/core/createAgent.js +36 -0
  260. package/dist/core/createAgent.js.map +1 -0
  261. package/dist/core/flow-namespace.d.ts +49 -0
  262. package/dist/core/flow-namespace.d.ts.map +1 -0
  263. package/dist/core/flow-namespace.js +168 -0
  264. package/dist/core/flow-namespace.js.map +1 -0
  265. package/dist/index.d.ts +11 -14
  266. package/dist/index.d.ts.map +1 -1
  267. package/dist/index.js +9 -12
  268. package/dist/index.js.map +1 -1
  269. package/dist/providers/AnthropicProvider.d.ts +1 -1
  270. package/dist/providers/AnthropicProvider.js +1 -1
  271. package/dist/providers/GeminiProvider.d.ts +1 -1
  272. package/dist/providers/GeminiProvider.d.ts.map +1 -1
  273. package/dist/providers/GeminiProvider.js +1 -1
  274. package/dist/providers/GeminiProvider.js.map +1 -1
  275. package/dist/providers/OpenAIProvider.d.ts +1 -1
  276. package/dist/providers/OpenAIProvider.d.ts.map +1 -1
  277. package/dist/providers/OpenAIProvider.js +1 -1
  278. package/dist/providers/OpenAIProvider.js.map +1 -1
  279. package/dist/types/agent.d.ts +183 -54
  280. package/dist/types/agent.d.ts.map +1 -1
  281. package/dist/types/agent.js +0 -6
  282. package/dist/types/agent.js.map +1 -1
  283. package/dist/types/ai.d.ts +3 -3
  284. package/dist/types/ai.d.ts.map +1 -1
  285. package/dist/types/errors.d.ts +15 -0
  286. package/dist/types/errors.d.ts.map +1 -0
  287. package/dist/types/errors.js +18 -0
  288. package/dist/types/errors.js.map +1 -0
  289. package/dist/types/flow.d.ts +513 -0
  290. package/dist/types/flow.d.ts.map +1 -0
  291. package/dist/types/flow.js +5 -0
  292. package/dist/types/flow.js.map +1 -0
  293. package/dist/types/index.d.ts +7 -6
  294. package/dist/types/index.d.ts.map +1 -1
  295. package/dist/types/index.js +4 -1
  296. package/dist/types/index.js.map +1 -1
  297. package/dist/types/persistence.d.ts +11 -7
  298. package/dist/types/persistence.d.ts.map +1 -1
  299. package/dist/types/routing.d.ts +1 -1
  300. package/dist/types/routing.d.ts.map +1 -1
  301. package/dist/types/session.d.ts +24 -23
  302. package/dist/types/session.d.ts.map +1 -1
  303. package/dist/types/signals.d.ts +248 -0
  304. package/dist/types/signals.d.ts.map +1 -0
  305. package/dist/types/signals.js +10 -0
  306. package/dist/types/signals.js.map +1 -0
  307. package/dist/types/template.d.ts +2 -8
  308. package/dist/types/template.d.ts.map +1 -1
  309. package/dist/types/tool.d.ts +36 -29
  310. package/dist/types/tool.d.ts.map +1 -1
  311. package/dist/types/tool.js +1 -1
  312. package/dist/types/tool.js.map +1 -1
  313. package/dist/utils/condition.d.ts +7 -1
  314. package/dist/utils/condition.d.ts.map +1 -1
  315. package/dist/utils/condition.js.map +1 -1
  316. package/dist/utils/id.d.ts +13 -5
  317. package/dist/utils/id.d.ts.map +1 -1
  318. package/dist/utils/id.js +22 -9
  319. package/dist/utils/id.js.map +1 -1
  320. package/dist/utils/index.d.ts +2 -2
  321. package/dist/utils/index.d.ts.map +1 -1
  322. package/dist/utils/index.js +2 -2
  323. package/dist/utils/index.js.map +1 -1
  324. package/dist/utils/session.d.ts +44 -5
  325. package/dist/utils/session.d.ts.map +1 -1
  326. package/dist/utils/session.js +193 -37
  327. package/dist/utils/session.js.map +1 -1
  328. package/docs/README.md +22 -200
  329. package/docs/concepts/architecture.md +281 -0
  330. package/docs/concepts/directives.md +400 -0
  331. package/docs/concepts/pipeline.md +399 -0
  332. package/docs/guides/branching.md +263 -0
  333. package/docs/guides/compaction.md +163 -0
  334. package/docs/guides/conditions.md +167 -0
  335. package/docs/guides/error-handling.md +176 -0
  336. package/docs/guides/flow-control.md +409 -0
  337. package/docs/guides/instructions.md +210 -0
  338. package/docs/guides/persistence.md +182 -0
  339. package/docs/guides/streaming.md +137 -0
  340. package/docs/migration/README.md +14 -0
  341. package/docs/migration/route-to-flow.md +561 -0
  342. package/docs/migration/v1-to-v2.md +909 -0
  343. package/docs/reference/adapters.md +481 -0
  344. package/docs/reference/branches.md +241 -0
  345. package/docs/reference/create-agent.md +186 -0
  346. package/docs/reference/directive.md +243 -0
  347. package/docs/reference/errors.md +122 -0
  348. package/docs/reference/flow.md +238 -0
  349. package/docs/reference/instruction.md +177 -0
  350. package/docs/reference/pre-directive.md +131 -0
  351. package/docs/reference/providers.md +227 -0
  352. package/docs/reference/signals.md +356 -0
  353. package/docs/reference/step.md +339 -0
  354. package/docs/reference/tool.md +269 -0
  355. package/docs/start/01-install.md +81 -0
  356. package/docs/start/02-first-agent.md +196 -0
  357. package/docs/start/03-collect-data.md +222 -0
  358. package/docs/start/04-add-tools.md +276 -0
  359. package/docs/start/05-go-to-production.md +216 -0
  360. package/examples/01-quickstart.ts +20 -0
  361. package/examples/02-data-extraction.ts +90 -0
  362. package/examples/03-tools.ts +136 -0
  363. package/examples/04-instructions.ts +100 -0
  364. package/examples/05-branching.ts +140 -0
  365. package/examples/06-flow-control.ts +103 -0
  366. package/examples/07-streaming.ts +69 -0
  367. package/examples/08-persistence.ts +98 -0
  368. package/examples/09-signals.ts +144 -0
  369. package/examples/tsconfig.json +30 -0
  370. package/package.json +2 -1
  371. package/src/adapters/MemoryAdapter.ts +3 -3
  372. package/src/adapters/MongoAdapter.ts +3 -3
  373. package/src/adapters/OpenSearchAdapter.ts +10 -8
  374. package/src/adapters/PostgreSQLAdapter.ts +26 -10
  375. package/src/adapters/PrismaAdapter.ts +6 -6
  376. package/src/adapters/RedisAdapter.ts +3 -3
  377. package/src/adapters/SQLiteAdapter.ts +31 -12
  378. package/src/constants/index.ts +2 -10
  379. package/src/core/Agent.ts +585 -374
  380. package/src/core/AutoChainExecutor.ts +440 -0
  381. package/src/core/BranchEvaluator.ts +167 -0
  382. package/src/core/DirectiveBus.ts +248 -0
  383. package/src/core/DirectiveChainTracker.ts +144 -0
  384. package/src/core/Flow.ts +666 -0
  385. package/src/core/{RoutingEngine.ts → FlowRouter.ts} +385 -365
  386. package/src/core/PersistenceManager.ts +8 -8
  387. package/src/core/PromptComposer.ts +209 -140
  388. package/src/core/PromptSectionCache.ts +1 -1
  389. package/src/core/ResponseEngine.ts +61 -46
  390. package/src/core/ResponseModal.ts +1453 -1240
  391. package/src/core/ResponsePipeline.ts +655 -175
  392. package/src/core/SignalEvaluator.ts +420 -0
  393. package/src/core/SignalProcessor.ts +723 -0
  394. package/src/core/Step.ts +279 -176
  395. package/src/core/StreamingToolExecutor.ts +4 -4
  396. package/src/core/ToolManager.ts +200 -97
  397. package/src/core/createAgent.ts +40 -0
  398. package/src/core/flow-namespace.ts +219 -0
  399. package/src/index.ts +42 -36
  400. package/src/providers/AnthropicProvider.ts +2 -2
  401. package/src/providers/GeminiProvider.ts +2 -2
  402. package/src/providers/OpenAIProvider.ts +2 -2
  403. package/src/types/agent.ts +182 -53
  404. package/src/types/ai.ts +3 -3
  405. package/src/types/errors.ts +18 -0
  406. package/src/types/flow.ts +590 -0
  407. package/src/types/index.ts +43 -16
  408. package/src/types/persistence.ts +12 -8
  409. package/src/types/routing.ts +1 -1
  410. package/src/types/session.ts +26 -23
  411. package/src/types/signals.ts +321 -0
  412. package/src/types/template.ts +3 -11
  413. package/src/types/tool.ts +50 -42
  414. package/src/utils/condition.ts +13 -4
  415. package/src/utils/id.ts +27 -9
  416. package/src/utils/index.ts +6 -2
  417. package/src/utils/session.ts +238 -42
  418. package/dist/cjs/core/BatchExecutor.d.ts +0 -359
  419. package/dist/cjs/core/BatchExecutor.d.ts.map +0 -1
  420. package/dist/cjs/core/BatchExecutor.js +0 -861
  421. package/dist/cjs/core/BatchExecutor.js.map +0 -1
  422. package/dist/cjs/core/BatchPromptBuilder.d.ts +0 -89
  423. package/dist/cjs/core/BatchPromptBuilder.d.ts.map +0 -1
  424. package/dist/cjs/core/BatchPromptBuilder.js +0 -223
  425. package/dist/cjs/core/BatchPromptBuilder.js.map +0 -1
  426. package/dist/cjs/core/Route.d.ts +0 -180
  427. package/dist/cjs/core/Route.d.ts.map +0 -1
  428. package/dist/cjs/core/Route.js +0 -542
  429. package/dist/cjs/core/Route.js.map +0 -1
  430. package/dist/cjs/core/RoutingEngine.d.ts +0 -185
  431. package/dist/cjs/core/RoutingEngine.d.ts.map +0 -1
  432. package/dist/cjs/core/RoutingEngine.js.map +0 -1
  433. package/dist/cjs/types/route.d.ts +0 -336
  434. package/dist/cjs/types/route.d.ts.map +0 -1
  435. package/dist/cjs/types/route.js.map +0 -1
  436. package/dist/core/BatchExecutor.d.ts +0 -359
  437. package/dist/core/BatchExecutor.d.ts.map +0 -1
  438. package/dist/core/BatchExecutor.js +0 -856
  439. package/dist/core/BatchExecutor.js.map +0 -1
  440. package/dist/core/BatchPromptBuilder.d.ts +0 -89
  441. package/dist/core/BatchPromptBuilder.d.ts.map +0 -1
  442. package/dist/core/BatchPromptBuilder.js +0 -219
  443. package/dist/core/BatchPromptBuilder.js.map +0 -1
  444. package/dist/core/Route.d.ts +0 -180
  445. package/dist/core/Route.d.ts.map +0 -1
  446. package/dist/core/Route.js +0 -538
  447. package/dist/core/Route.js.map +0 -1
  448. package/dist/core/RoutingEngine.d.ts +0 -185
  449. package/dist/core/RoutingEngine.d.ts.map +0 -1
  450. package/dist/core/RoutingEngine.js.map +0 -1
  451. package/dist/types/route.d.ts +0 -336
  452. package/dist/types/route.d.ts.map +0 -1
  453. package/dist/types/route.js +0 -5
  454. package/dist/types/route.js.map +0 -1
  455. package/docs/CONTRIBUTING.md +0 -521
  456. package/docs/api/README.md +0 -3299
  457. package/docs/api/overview.md +0 -1410
  458. package/docs/architecture/data-extraction-flow.md +0 -360
  459. package/docs/architecture/multi-step-execution.md +0 -277
  460. package/docs/core/agent/README.md +0 -938
  461. package/docs/core/agent/context-management.md +0 -796
  462. package/docs/core/agent/rules-and-prohibitions.md +0 -113
  463. package/docs/core/agent/session-management.md +0 -693
  464. package/docs/core/ai-integration/prompt-composition.md +0 -355
  465. package/docs/core/ai-integration/providers.md +0 -515
  466. package/docs/core/ai-integration/response-processing.md +0 -433
  467. package/docs/core/conversation-flows/data-collection.md +0 -772
  468. package/docs/core/conversation-flows/route-dsl.md +0 -509
  469. package/docs/core/conversation-flows/routes.md +0 -249
  470. package/docs/core/conversation-flows/step-transitions.md +0 -731
  471. package/docs/core/conversation-flows/steps.md +0 -268
  472. package/docs/core/error-handling.md +0 -830
  473. package/docs/core/persistence/adapters.md +0 -255
  474. package/docs/core/persistence/session-storage.md +0 -656
  475. package/docs/core/routing/intelligent-routing.md +0 -470
  476. package/docs/core/tools/enhanced-tool.md +0 -186
  477. package/docs/core/tools/streaming-execution.md +0 -161
  478. package/docs/core/tools/tool-definition.md +0 -970
  479. package/docs/core/tools/tool-scoping.md +0 -819
  480. package/docs/guides/advanced-patterns/publishing.md +0 -186
  481. package/docs/guides/context-compaction.md +0 -96
  482. package/docs/guides/error-handling-patterns.md +0 -578
  483. package/docs/guides/getting-started/README.md +0 -795
  484. package/docs/guides/migration/README.md +0 -101
  485. package/docs/guides/migration/flexible-routing-conditions.md +0 -375
  486. package/docs/guides/migration/multi-step-execution.md +0 -393
  487. package/docs/guides/migration/response-modal-refactor.md +0 -518
  488. package/docs/guides/prompt-optimization.md +0 -164
  489. package/examples/advanced-patterns/context-compaction.ts +0 -223
  490. package/examples/advanced-patterns/knowledge-based-agent.ts +0 -735
  491. package/examples/advanced-patterns/persistent-onboarding.ts +0 -728
  492. package/examples/advanced-patterns/route-lifecycle-hooks.ts +0 -556
  493. package/examples/advanced-patterns/streaming-responses.ts +0 -656
  494. package/examples/ai-providers/anthropic-integration.ts +0 -388
  495. package/examples/ai-providers/openai-integration.ts +0 -228
  496. package/examples/condition-patterns/function-only-conditions.ts +0 -365
  497. package/examples/condition-patterns/mixed-array-conditions.ts +0 -477
  498. package/examples/condition-patterns/route-skipif-patterns.ts +0 -468
  499. package/examples/condition-patterns/step-skipif-patterns.ts +0 -0
  500. package/examples/condition-patterns/string-only-conditions.ts +0 -296
  501. package/examples/conversation-flows/completion-transitions.ts +0 -318
  502. package/examples/core-concepts/basic-agent.ts +0 -503
  503. package/examples/core-concepts/modern-streaming-api.ts +0 -309
  504. package/examples/core-concepts/schema-driven-extraction.ts +0 -332
  505. package/examples/core-concepts/session-management.ts +0 -494
  506. package/examples/integrations/database-integration.ts +0 -631
  507. package/examples/integrations/healthcare-integration.ts +0 -595
  508. package/examples/integrations/search-integration.ts +0 -530
  509. package/examples/integrations/server-session-management.ts +0 -307
  510. package/examples/persistence/custom-adapter.ts +0 -526
  511. package/examples/persistence/database-persistence.ts +0 -583
  512. package/examples/persistence/memory-sessions.ts +0 -495
  513. package/examples/persistence/prisma-schema.example.prisma +0 -74
  514. package/examples/persistence/redis-persistence.ts +0 -488
  515. package/examples/tools/basic-tools.ts +0 -765
  516. package/examples/tools/data-enrichment-tools.ts +0 -593
  517. package/examples/tools/enhanced-tool-metadata.ts +0 -268
  518. package/examples/tools/streaming-tool-execution.ts +0 -283
  519. package/src/core/BatchExecutor.ts +0 -1187
  520. package/src/core/BatchPromptBuilder.ts +0 -299
  521. package/src/core/Route.ts +0 -678
  522. package/src/types/route.ts +0 -392
@@ -0,0 +1,666 @@
1
+ /**
2
+ * Flow (Journey) DSL implementation
3
+ */
4
+
5
+ import type {
6
+ FlowOptions,
7
+ FlowRef,
8
+ StepOptions,
9
+ FlowLifecycleHooks,
10
+ StructuredSchema,
11
+ Instruction,
12
+ Term,
13
+ Tool,
14
+ TemplateContext,
15
+ ConditionEvaluationResult,
16
+ Directive,
17
+ } from "../types";
18
+ import type { SessionState } from "../types/session";
19
+ import type { StepResult } from "../types/flow";
20
+ import type { ConditionWhen, ConditionIf } from "../types/flow";
21
+
22
+ import { generateFlowId, logger } from "../utils";
23
+
24
+ import { Step, FlowConfigurationError } from "./Step";
25
+ import { Agent } from './Agent'
26
+
27
+ /**
28
+ * Represents a conversational flow/journey
29
+ */
30
+ export class Flow<TContext = unknown, TData = unknown> {
31
+ public readonly id: string;
32
+ public readonly title: string;
33
+ public readonly description?: string;
34
+ public readonly when?: ConditionWhen;
35
+ public readonly if?: ConditionIf<TContext, TData>;
36
+ private readonly _initialStep: Step<TContext, TData>;
37
+ public readonly responseOutputSchema?: StructuredSchema;
38
+ public readonly initialData?: Partial<TData>;
39
+ public readonly requiredFields?: (keyof TData)[];
40
+ public readonly optionalFields?: (keyof TData)[];
41
+ public readonly onComplete?: string;
42
+ public readonly reentrant: boolean;
43
+ public readonly hooks?: FlowLifecycleHooks<TContext, TData>;
44
+ public routingExtrasSchema?: StructuredSchema;
45
+ public instructions: Instruction<TContext, TData>[] = [];
46
+ public tools: Tool<TContext, TData>[] = [];
47
+ public knowledgeBase: Record<string, unknown> = {};
48
+
49
+ /** Get the initial (first) step of this flow. */
50
+ get initialStep(): Step<TContext, TData> {
51
+ return this._initialStep;
52
+ }
53
+
54
+ // Reference to parent agent for ToolManager access
55
+ private parentAgent?: Agent<TContext, TData>;
56
+
57
+ /**
58
+ * Tracks whether this flow has participated in at least one turn.
59
+ * Used to emit a DEBUG warning when `addStep` mutates the graph mid-session.
60
+ * @internal Set by the pipeline after the first turn involving this flow.
61
+ */
62
+ public _hasHandledTurn: boolean = false;
63
+
64
+ constructor(options: FlowOptions<TContext, TData>, parentAgent?: Agent<TContext, TData>) {
65
+ // Use provided ID or generate a deterministic one from the title
66
+ this.id = options.id || generateFlowId(options.title);
67
+ this.title = options.title;
68
+ this.description = options.description;
69
+ this.when = options.when;
70
+ this.if = options.if;
71
+
72
+ // Validate when/if split: functions belong on `if`, not `when`
73
+ if (this.when !== undefined) {
74
+ const whenValue = this.when as unknown;
75
+ if (typeof whenValue === 'function') {
76
+ throw new FlowConfigurationError(
77
+ `[FlowConfigurationError] Flow "${options.title}" has a function on "when": functions belong on "if" only. Move the function to the "if" field.`
78
+ );
79
+ }
80
+ if (Array.isArray(whenValue)) {
81
+ for (let i = 0; i < (whenValue as unknown[]).length; i++) {
82
+ if (typeof (whenValue as unknown[])[i] === 'function') {
83
+ throw new FlowConfigurationError(
84
+ `[FlowConfigurationError] Flow "${options.title}" has a function at "when[${i}]": functions belong on "if" only. Move the function to the "if" field.`
85
+ );
86
+ }
87
+ }
88
+ }
89
+ }
90
+
91
+ // Store reference to parent agent for ToolManager access
92
+ this.parentAgent = parentAgent;
93
+
94
+ // Handle steps: use first step as initial
95
+ let stepsToChain: StepOptions<TContext, TData>[] = [];
96
+
97
+ if (options.steps && options.steps.length > 0) {
98
+ stepsToChain = options.steps.slice(1);
99
+ this._initialStep = new Step<TContext, TData>(this.id, options.steps[0], this.parentAgent);
100
+ } else {
101
+ this._initialStep = new Step<TContext, TData>(this.id, undefined, this.parentAgent);
102
+ }
103
+
104
+ // Store flow configuration
105
+ this.routingExtrasSchema = options.routingExtrasSchema;
106
+ this.responseOutputSchema = options.responseOutputSchema;
107
+ this.initialData = options.initialData;
108
+ this.requiredFields = options.requiredFields;
109
+ this.optionalFields = options.optionalFields;
110
+ this.onComplete = options.onComplete;
111
+ this.reentrant = options.reentrant ?? false;
112
+ this.hooks = options.hooks;
113
+
114
+ // ─── onComplete cleanup: validate no conflict and desugar ─────────────
115
+ // If both top-level `onComplete` and `hooks.onComplete` are set, throw.
116
+ if (this.onComplete !== undefined && this.hooks?.onComplete !== undefined) {
117
+ throw new FlowConfigurationError(
118
+ `[FlowConfigurationError] Flow "${this.title}": both top-level \`onComplete\` and \`hooks.onComplete\` are set. ` +
119
+ `Use one or the other — top-level \`onComplete: string\` is sugar for \`hooks.onComplete = () => ({ goTo: '<id>' })\`.`
120
+ );
121
+ }
122
+
123
+ // Desugar: top-level `onComplete: '<flowId>'` → `hooks.onComplete = () => ({ goTo: '<flowId>' })`
124
+ if (this.onComplete !== undefined && !this.hooks?.onComplete) {
125
+ const targetFlowId = this.onComplete;
126
+ const desugaredHook = () => ({ goTo: targetFlowId });
127
+ if (this.hooks) {
128
+ (this.hooks).onComplete = desugaredHook;
129
+ } else {
130
+ (this as { hooks?: FlowLifecycleHooks<TContext, TData> }).hooks = { onComplete: desugaredHook };
131
+ }
132
+ }
133
+
134
+ // Initialize instructions from options
135
+ if (options.instructions) {
136
+ options.instructions.forEach((instruction) => {
137
+ this.createInstruction(instruction);
138
+ });
139
+ }
140
+
141
+ // Initialize tools from options
142
+ if (options.tools) {
143
+ options.tools.forEach((toolRef) => {
144
+ if (typeof toolRef === 'string') {
145
+ // Tool ID - try to resolve from ToolManager
146
+ if (this.parentAgent?.tool) {
147
+ const registeredTool = this.parentAgent.tool.find(toolRef);
148
+ if (registeredTool) {
149
+ this.createTool(registeredTool);
150
+ } else {
151
+ // Tool not found - log warning but don't fail
152
+ logger.warn(`[Flow] Tool ID '${toolRef}' not found in any scope for flow ${this.title}`);
153
+ }
154
+ } else {
155
+ logger.warn(`[Flow] No agent available to resolve tool ID '${toolRef}' for flow ${this.title}`);
156
+ }
157
+ } else {
158
+ // Inline tool object - validate and use directly
159
+ if (toolRef && toolRef.id && typeof toolRef.handler === 'function') {
160
+ this.createTool(toolRef);
161
+ } else {
162
+ logger.warn(`[Flow] Invalid inline tool object in flow ${this.title}:`, toolRef);
163
+ }
164
+ }
165
+ });
166
+ }
167
+
168
+ // Build sequential steps if provided
169
+ if (stepsToChain.length > 0) {
170
+ this.buildSequentialSteps(stepsToChain);
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Build a sequential step machine from an array of steps
176
+ * The last step in the array is the implicit terminus of the flow.
177
+ * @private
178
+ */
179
+ private buildSequentialSteps(
180
+ steps: Array<StepOptions<TContext, TData>>
181
+ ): void {
182
+ let currentStepResult: StepResult<TContext, TData> = this.initialStep.asStepResult();
183
+
184
+ for (const step of steps) {
185
+ currentStepResult = currentStepResult.nextStep(step);
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Imperatively add a step to this flow after construction.
191
+ *
192
+ * Applies the same construction-time validations as declarative `steps: [...]`
193
+ * registration (auto-step shape, `reply` exclusivity, `branches` validation,
194
+ * schema lookups). Connects the new step as a successor of the current last
195
+ * step in the flow.
196
+ *
197
+ * When called after the agent has handled a turn, emits a DEBUG-level warning
198
+ * that the flow graph is being mutated mid-session; the operation still succeeds.
199
+ *
200
+ * @param options - Step configuration
201
+ * @returns The newly created and registered Step
202
+ *
203
+ * **Validates: Requirements 8.7, 26.1–26.5**
204
+ */
205
+ addStep(options: StepOptions<TContext, TData>): Step<TContext, TData> {
206
+ // Warn if graph is being mutated mid-session
207
+ if (this._hasHandledTurn) {
208
+ logger.debug(
209
+ `[Flow] Flow "${this.title}" (${this.id}): addStep("${options.id || options.description || 'unnamed'}") called after the agent has handled a turn. ` +
210
+ `The flow graph is being mutated mid-session. The step will still be registered.`
211
+ );
212
+ }
213
+
214
+ // Find the current last step (terminal step with no transitions)
215
+ const lastStep = this.getLastStep();
216
+
217
+ // Create the new step via the last step's nextStep chain method.
218
+ // This applies the same Step constructor validations (auto-step shape,
219
+ // branches validation, when/if split checks) and connects the new step
220
+ // as a successor of the last step — identical to declarative chaining.
221
+ lastStep.nextStep(options);
222
+
223
+ // Retrieve the newly created Step instance from the last step's transitions
224
+ const transitions = lastStep.getTransitions();
225
+ const newStep = transitions[transitions.length - 1];
226
+
227
+ return newStep;
228
+ }
229
+
230
+ /**
231
+ * Find the last step in the sequential chain (the terminal step with no transitions).
232
+ * If multiple terminal steps exist (branching), returns the one visited last in BFS order.
233
+ * @private
234
+ */
235
+ private getLastStep(): Step<TContext, TData> {
236
+ const allSteps = this.getAllSteps();
237
+ // Find the last step with no outgoing transitions (terminal)
238
+ for (let i = allSteps.length - 1; i >= 0; i--) {
239
+ if (allSteps[i].getTransitions().length === 0) {
240
+ return allSteps[i];
241
+ }
242
+ }
243
+ // Fallback: if all steps have transitions (cycle), return the last in traversal order
244
+ return allSteps[allSteps.length - 1];
245
+ }
246
+
247
+ /**
248
+ * Evaluate when/if conditions using the v2 split logic.
249
+ * `if` (code predicate) evaluates first (free); `when` (AI) evaluates only when `if` passes.
250
+ * Both are combined with AND semantics.
251
+ */
252
+ async evaluateWhen(
253
+ templateContext: TemplateContext<TContext, TData>
254
+ ): Promise<ConditionEvaluationResult> {
255
+ // If neither `when` nor `if` is set, flow is always eligible
256
+ if (!this.when && !this.if) {
257
+ return {
258
+ programmaticResult: true,
259
+ aiContextStrings: [],
260
+ hasProgrammaticConditions: false,
261
+ };
262
+ }
263
+
264
+ // Evaluate `if` first (free, code-only)
265
+ if (this.if) {
266
+ const predicates = Array.isArray(this.if) ? this.if : [this.if];
267
+ for (const predicate of predicates) {
268
+ try {
269
+ const result = await predicate({
270
+ data: templateContext.data,
271
+ context: templateContext.context as TContext,
272
+ session: templateContext.session as SessionState<TData>,
273
+ history: templateContext.history || [],
274
+ });
275
+ if (!result) {
276
+ // `if` failed — short-circuit, don't evaluate `when`
277
+ return {
278
+ programmaticResult: false,
279
+ aiContextStrings: [],
280
+ hasProgrammaticConditions: true,
281
+ };
282
+ }
283
+ } catch (error) {
284
+ logger.warn(`[Flow] "if" predicate failed for flow "${this.title}":`, error);
285
+ return {
286
+ programmaticResult: false,
287
+ aiContextStrings: [],
288
+ hasProgrammaticConditions: true,
289
+ };
290
+ }
291
+ }
292
+ }
293
+
294
+ // `if` passed (or was absent) — now evaluate `when` (AI-evaluated strings)
295
+ if (this.when) {
296
+ const whenStrings = Array.isArray(this.when) ? this.when : [this.when];
297
+ return {
298
+ programmaticResult: true,
299
+ aiContextStrings: whenStrings,
300
+ hasProgrammaticConditions: !!this.if,
301
+ };
302
+ }
303
+
304
+ // Only `if` was set and it passed
305
+ return {
306
+ programmaticResult: true,
307
+ aiContextStrings: [],
308
+ hasProgrammaticConditions: true,
309
+ };
310
+ }
311
+
312
+ /**
313
+ * Create an instruction specific to this flow.
314
+ */
315
+ createInstruction(instruction: Instruction<TContext, TData>): this {
316
+ this.instructions.push({
317
+ ...instruction,
318
+ kind: instruction.kind || 'should' as const,
319
+ id: instruction.id || `instruction_${this.id}_${this.instructions.length}`,
320
+ enabled: instruction.enabled !== false, // Default to true
321
+ });
322
+ return this;
323
+ }
324
+
325
+ /**
326
+ * Register a tool for this flow
327
+ */
328
+ createTool(tool: Tool<TContext, TData>): this {
329
+ // Validate tool before adding
330
+ if (!tool || !tool.id || !tool.handler) {
331
+ throw new Error(`Invalid tool: must have id and handler properties`);
332
+ }
333
+
334
+ this.tools.push(tool);
335
+ return this;
336
+ }
337
+
338
+ /**
339
+ * Register multiple tools for this flow
340
+ */
341
+ registerTools(tools: Tool<TContext, TData>[]): this {
342
+ tools.forEach((tool) => this.createTool(tool));
343
+ return this;
344
+ }
345
+
346
+ /**
347
+ * Add a tool to this flow using the ToolManager API
348
+ * Creates and adds the tool to flow scope in one operation
349
+ */
350
+ addTool(
351
+ tool: Tool<TContext, TData>
352
+ ): this {
353
+ if (this.parentAgent && this.parentAgent.tool) {
354
+ // Use ToolManager to add to flow scope - no casting needed with unified interface
355
+ this.parentAgent.tool.addToFlow(this, tool);
356
+ } else {
357
+ // Fallback: add tool directly to flow tools
358
+ this.createTool(tool);
359
+ }
360
+ return this;
361
+ }
362
+
363
+ /**
364
+ * Get all instructions for this flow
365
+ */
366
+ getInstructions(): Instruction<TContext, TData>[] {
367
+ return [...this.instructions];
368
+ }
369
+
370
+ /**
371
+ * Get all terms for this flow
372
+ * @deprecated Flow-level terms removed in v2. Always returns empty array.
373
+ */
374
+ getTerms(): Term<TContext>[] {
375
+ return [];
376
+ }
377
+
378
+ /**
379
+ * Get all tools for this flow
380
+ */
381
+ getTools(): Tool<TContext, TData>[] {
382
+ return [...this.tools];
383
+ }
384
+
385
+ /**
386
+ * Get optional extras schema requested during routing
387
+ */
388
+ getRoutingExtrasSchema(): StructuredSchema | undefined {
389
+ return this.routingExtrasSchema;
390
+ }
391
+
392
+ /**
393
+ * Get optional structured response schema for this flow's message
394
+ */
395
+ getResponseOutputSchema(): StructuredSchema | undefined {
396
+ return this.responseOutputSchema;
397
+ }
398
+
399
+ getSteps(): Step<TContext, TData>[] {
400
+ return this.getAllSteps();
401
+ }
402
+
403
+ /**
404
+ * Get flow reference
405
+ */
406
+ getRef(): FlowRef {
407
+ return {
408
+ id: this.id,
409
+ };
410
+ }
411
+
412
+ /**
413
+ * Get all steps in this flow (via traversal from initial step)
414
+ */
415
+ getAllSteps(): Step<TContext, TData>[] {
416
+ const visited = new Set<string>();
417
+ const steps: Step<TContext, TData>[] = [];
418
+ const queue: Step<TContext, TData>[] = [this.initialStep];
419
+
420
+ while (queue.length > 0) {
421
+ const current = queue.shift()!;
422
+
423
+ if (visited.has(current.id)) {
424
+ continue;
425
+ }
426
+
427
+ visited.add(current.id);
428
+ steps.push(current);
429
+
430
+ // Add target steps from transitions
431
+ for (const nextStep of current.getTransitions()) {
432
+ if (nextStep && !visited.has(nextStep.id)) {
433
+ queue.push(nextStep);
434
+ }
435
+ }
436
+ }
437
+
438
+ return steps;
439
+ }
440
+
441
+ /**
442
+ * Get a specific step by ID
443
+ * @param stepId - The step ID to find
444
+ * @returns The step if found, undefined otherwise
445
+ */
446
+ getStep(stepId: string): Step<TContext, TData> | undefined {
447
+ const steps = this.getAllSteps();
448
+ return steps.find((step) => step.id === stepId);
449
+ }
450
+
451
+ /**
452
+ * Get a description of the flow structure for debugging
453
+ */
454
+ describe(): string {
455
+ const lines: string[] = [
456
+ `Flow: ${this.title}`,
457
+ `ID: ${this.id}`,
458
+ `Description: ${this.description || "N/A"}`,
459
+ `When: ${this.when ? (typeof this.when === 'string' ? this.when : Array.isArray(this.when) ? '[Array]' : '[Function]') : "None"}`,
460
+ `If: ${this.if ? '[Function]' : "None"}`,
461
+ "",
462
+ "Steps:",
463
+ ];
464
+
465
+ const steps = this.getAllSteps();
466
+ for (const step of steps) {
467
+ lines.push(
468
+ ` - ${step.id}${step.description ? `: ${step.description}` : ""}`
469
+ );
470
+
471
+ const transitions = step.getTransitions();
472
+ for (const transition of transitions) {
473
+ lines.push(
474
+ ` -> ${transition.id}${transition.description ? `: ${transition.description}` : ""
475
+ }`
476
+ );
477
+ }
478
+ }
479
+
480
+ return lines.join("\n");
481
+ }
482
+
483
+ /**
484
+ * Handle data updates for this flow, calling the onDataUpdate hook if configured
485
+ * @param data - New collected data
486
+ * @param previousCollected - Previously collected data
487
+ * @returns Modified data after hook processing, or original data if no hook
488
+ */
489
+ async handleDataUpdate(
490
+ data: Partial<TData>,
491
+ previousCollected: Partial<TData>
492
+ ): Promise<Partial<TData>> {
493
+ // Call flow-specific onDataUpdate hook if configured
494
+ if (this.hooks?.onDataUpdate) {
495
+ return await this.hooks.onDataUpdate(data, previousCollected);
496
+ }
497
+
498
+ // Return original data if no hook
499
+ return data;
500
+ }
501
+
502
+ /**
503
+ * Handle context updates for this flow, calling the onContextUpdate hook if configured
504
+ * @param newContext - New context
505
+ * @param previousContext - Previous context
506
+ */
507
+ async handleContextUpdate(
508
+ newContext: TContext,
509
+ previousContext: TContext
510
+ ): Promise<void> {
511
+ // Call flow-specific onContextUpdate hook if configured
512
+ if (this.hooks?.onContextUpdate) {
513
+ await this.hooks.onContextUpdate(newContext, previousContext);
514
+ }
515
+ }
516
+
517
+ /**
518
+ * Export flow configuration as FlowOptions for copying/cloning
519
+ * @returns FlowOptions that can be used to create a new flow with identical configuration
520
+ */
521
+ toOptions(): FlowOptions<TContext, TData> {
522
+ // Convert steps to StepOptions
523
+ const steps = this.getAllSteps()
524
+ .filter(step => step.id !== this.initialStep.id) // Exclude initial step
525
+ .map(step => ({
526
+ id: step.id,
527
+ description: step.description,
528
+ prompt: step.prompt,
529
+ tools: step.tools,
530
+ prepare: step.prepare,
531
+ finalize: step.finalize,
532
+ collect: step.collect,
533
+ skip: step.skip,
534
+ requires: step.requires,
535
+ when: step.when,
536
+ if: step.if,
537
+ instructions: step.getInstructions(),
538
+ }));
539
+
540
+ return {
541
+ id: this.id,
542
+ title: this.title,
543
+ description: this.description,
544
+ when: this.when,
545
+ if: this.if,
546
+ instructions: this.getInstructions(),
547
+ tools: this.getTools(),
548
+ routingExtrasSchema: this.routingExtrasSchema,
549
+ responseOutputSchema: this.responseOutputSchema,
550
+ requiredFields: this.requiredFields,
551
+ optionalFields: this.optionalFields,
552
+ initialData: this.initialData,
553
+ steps: steps.length > 0 ? steps : undefined,
554
+ onComplete: this.onComplete,
555
+ reentrant: this.reentrant,
556
+ hooks: this.hooks,
557
+ };
558
+ }
559
+
560
+ /**
561
+ * Check if this flow is complete based on the provided data
562
+ * @param data - Currently collected agent-level data
563
+ * @returns true if all required fields are present, false otherwise
564
+ *
565
+ * Note: Flows with no requiredFields (whether they have optionalFields or not)
566
+ * are never complete based on data — they complete when the last step finishes (implicit terminus).
567
+ */
568
+ isComplete(data: Partial<TData>): boolean {
569
+ // If flow has required fields, check if they're all collected
570
+ if (this.requiredFields && this.requiredFields.length > 0) {
571
+ return this.requiredFields.every(field => {
572
+ const value = data[field];
573
+ return value !== undefined && value !== null && value !== '';
574
+ });
575
+ }
576
+
577
+ // If flow has optional fields but no required fields, it's NOT complete
578
+ // Optional-only flows complete when the last step finishes
579
+ if (this.optionalFields && this.optionalFields.length > 0) {
580
+ return false;
581
+ }
582
+
583
+ // No required or optional fields - flow doesn't complete based on data
584
+ // It completes when the last step in the flow finishes (implicit terminus)
585
+ return false;
586
+ }
587
+
588
+ /**
589
+ * Get the list of missing required fields for this flow
590
+ * @param data - Currently collected agent-level data
591
+ * @returns Array of missing required field keys
592
+ */
593
+ getMissingRequiredFields(data: Partial<TData>): (keyof TData)[] {
594
+ if (!this.requiredFields || this.requiredFields.length === 0) {
595
+ return [];
596
+ }
597
+
598
+ return this.requiredFields.filter(field => {
599
+ const value = data[field];
600
+ return value === undefined || value === null || value === '';
601
+ });
602
+ }
603
+
604
+ /**
605
+ * Get the completion progress for this flow as a percentage
606
+ * @param data - Currently collected agent-level data
607
+ * @returns Completion progress as a number between 0 and 1
608
+ *
609
+ * Note: Must be consistent with isComplete() logic
610
+ */
611
+ getCompletionProgress(data: Partial<TData>): number {
612
+ // If flow has required fields, calculate progress
613
+ if (this.requiredFields && this.requiredFields.length > 0) {
614
+ const completedFields = this.requiredFields.filter(field => {
615
+ const value = data[field];
616
+ return value !== undefined && value !== null && value !== '';
617
+ });
618
+ return completedFields.length / this.requiredFields.length;
619
+ }
620
+
621
+ // If flow has optional fields but no required fields, it's NOT complete
622
+ // Optional-only flows complete when the last step finishes, progress is 0
623
+ if (this.optionalFields && this.optionalFields.length > 0) {
624
+ return 0;
625
+ }
626
+
627
+ // No required or optional fields - flow doesn't complete based on data
628
+ // Progress is 0 (completes when last step finishes)
629
+ return 0;
630
+ }
631
+
632
+ /**
633
+ * Evaluate the onComplete handler and return a Directive.
634
+ *
635
+ * In v2, top-level `onComplete` is desugared to `hooks.onComplete` at
636
+ * construction time. This method delegates to `hooks.onComplete` when set.
637
+ *
638
+ * @param session - Current session step
639
+ * @param context - Agent context
640
+ * @returns Directive or undefined if no transition
641
+ */
642
+ async evaluateOnComplete(
643
+ session: { data?: Partial<TData> },
644
+ context?: TContext
645
+ ): Promise<Directive<TContext, TData> | undefined> {
646
+ if (this.hooks?.onComplete) {
647
+ const hookCtx = {
648
+ context: context as TContext,
649
+ data: session.data ?? {} as Partial<TData>,
650
+ session: {} as SessionState<TData>,
651
+ history: [] as import("../types/history").Event[],
652
+ dispatch: () => { },
653
+ };
654
+ const result = await this.hooks.onComplete(hookCtx);
655
+ if (!result) return undefined;
656
+ return result;
657
+ }
658
+
659
+ // Fallback: if somehow onComplete is set but hooks.onComplete is not
660
+ if (this.onComplete) {
661
+ return { goTo: this.onComplete };
662
+ }
663
+
664
+ return undefined;
665
+ }
666
+ }