@falai/agent 1.2.8 → 2.0.0

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 (499) 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/types/agent.d.ts +183 -54
  126. package/dist/cjs/types/agent.d.ts.map +1 -1
  127. package/dist/cjs/types/agent.js +0 -6
  128. package/dist/cjs/types/agent.js.map +1 -1
  129. package/dist/cjs/types/ai.d.ts +3 -3
  130. package/dist/cjs/types/ai.d.ts.map +1 -1
  131. package/dist/cjs/types/errors.d.ts +15 -0
  132. package/dist/cjs/types/errors.d.ts.map +1 -0
  133. package/dist/cjs/types/errors.js +22 -0
  134. package/dist/cjs/types/errors.js.map +1 -0
  135. package/dist/cjs/types/flow.d.ts +513 -0
  136. package/dist/cjs/types/flow.d.ts.map +1 -0
  137. package/dist/cjs/types/{route.js → flow.js} +2 -2
  138. package/dist/cjs/types/flow.js.map +1 -0
  139. package/dist/cjs/types/index.d.ts +7 -6
  140. package/dist/cjs/types/index.d.ts.map +1 -1
  141. package/dist/cjs/types/index.js +6 -2
  142. package/dist/cjs/types/index.js.map +1 -1
  143. package/dist/cjs/types/persistence.d.ts +11 -7
  144. package/dist/cjs/types/persistence.d.ts.map +1 -1
  145. package/dist/cjs/types/routing.d.ts +1 -1
  146. package/dist/cjs/types/routing.d.ts.map +1 -1
  147. package/dist/cjs/types/session.d.ts +24 -23
  148. package/dist/cjs/types/session.d.ts.map +1 -1
  149. package/dist/cjs/types/signals.d.ts +248 -0
  150. package/dist/cjs/types/signals.d.ts.map +1 -0
  151. package/dist/cjs/types/signals.js +11 -0
  152. package/dist/cjs/types/signals.js.map +1 -0
  153. package/dist/cjs/types/template.d.ts +2 -8
  154. package/dist/cjs/types/template.d.ts.map +1 -1
  155. package/dist/cjs/types/tool.d.ts +36 -29
  156. package/dist/cjs/types/tool.d.ts.map +1 -1
  157. package/dist/cjs/types/tool.js +1 -1
  158. package/dist/cjs/types/tool.js.map +1 -1
  159. package/dist/cjs/utils/condition.d.ts +7 -1
  160. package/dist/cjs/utils/condition.d.ts.map +1 -1
  161. package/dist/cjs/utils/condition.js.map +1 -1
  162. package/dist/cjs/utils/id.d.ts +13 -5
  163. package/dist/cjs/utils/id.d.ts.map +1 -1
  164. package/dist/cjs/utils/id.js +24 -10
  165. package/dist/cjs/utils/id.js.map +1 -1
  166. package/dist/cjs/utils/index.d.ts +2 -2
  167. package/dist/cjs/utils/index.d.ts.map +1 -1
  168. package/dist/cjs/utils/index.js +7 -3
  169. package/dist/cjs/utils/index.js.map +1 -1
  170. package/dist/cjs/utils/session.d.ts +44 -5
  171. package/dist/cjs/utils/session.d.ts.map +1 -1
  172. package/dist/cjs/utils/session.js +197 -38
  173. package/dist/cjs/utils/session.js.map +1 -1
  174. package/dist/constants/index.d.ts +0 -9
  175. package/dist/constants/index.d.ts.map +1 -1
  176. package/dist/constants/index.js +3 -9
  177. package/dist/constants/index.js.map +1 -1
  178. package/dist/core/Agent.d.ts +119 -153
  179. package/dist/core/Agent.d.ts.map +1 -1
  180. package/dist/core/Agent.js +472 -325
  181. package/dist/core/Agent.js.map +1 -1
  182. package/dist/core/AutoChainExecutor.d.ts +107 -0
  183. package/dist/core/AutoChainExecutor.d.ts.map +1 -0
  184. package/dist/core/AutoChainExecutor.js +293 -0
  185. package/dist/core/AutoChainExecutor.js.map +1 -0
  186. package/dist/core/BranchEvaluator.d.ts +54 -0
  187. package/dist/core/BranchEvaluator.d.ts.map +1 -0
  188. package/dist/core/BranchEvaluator.js +126 -0
  189. package/dist/core/BranchEvaluator.js.map +1 -0
  190. package/dist/core/DirectiveBus.d.ts +88 -0
  191. package/dist/core/DirectiveBus.d.ts.map +1 -0
  192. package/dist/core/DirectiveBus.js +192 -0
  193. package/dist/core/DirectiveBus.js.map +1 -0
  194. package/dist/core/DirectiveChainTracker.d.ts +49 -0
  195. package/dist/core/DirectiveChainTracker.d.ts.map +1 -0
  196. package/dist/core/DirectiveChainTracker.js +117 -0
  197. package/dist/core/DirectiveChainTracker.js.map +1 -0
  198. package/dist/core/Flow.d.ts +186 -0
  199. package/dist/core/Flow.d.ts.map +1 -0
  200. package/dist/core/Flow.js +546 -0
  201. package/dist/core/Flow.js.map +1 -0
  202. package/dist/core/FlowRouter.d.ts +182 -0
  203. package/dist/core/FlowRouter.d.ts.map +1 -0
  204. package/dist/core/{RoutingEngine.js → FlowRouter.js} +322 -305
  205. package/dist/core/FlowRouter.js.map +1 -0
  206. package/dist/core/PersistenceManager.d.ts +2 -2
  207. package/dist/core/PersistenceManager.d.ts.map +1 -1
  208. package/dist/core/PersistenceManager.js +7 -7
  209. package/dist/core/PersistenceManager.js.map +1 -1
  210. package/dist/core/PromptComposer.d.ts +21 -8
  211. package/dist/core/PromptComposer.d.ts.map +1 -1
  212. package/dist/core/PromptComposer.js +183 -106
  213. package/dist/core/PromptComposer.js.map +1 -1
  214. package/dist/core/PromptSectionCache.d.ts +1 -1
  215. package/dist/core/PromptSectionCache.js +1 -1
  216. package/dist/core/ResponseEngine.d.ts +18 -8
  217. package/dist/core/ResponseEngine.d.ts.map +1 -1
  218. package/dist/core/ResponseEngine.js +38 -36
  219. package/dist/core/ResponseEngine.js.map +1 -1
  220. package/dist/core/ResponseModal.d.ts +73 -56
  221. package/dist/core/ResponseModal.d.ts.map +1 -1
  222. package/dist/core/ResponseModal.js +1193 -1016
  223. package/dist/core/ResponseModal.js.map +1 -1
  224. package/dist/core/ResponsePipeline.d.ts +124 -26
  225. package/dist/core/ResponsePipeline.d.ts.map +1 -1
  226. package/dist/core/ResponsePipeline.js +509 -137
  227. package/dist/core/ResponsePipeline.js.map +1 -1
  228. package/dist/core/SignalEvaluator.d.ts +86 -0
  229. package/dist/core/SignalEvaluator.d.ts.map +1 -0
  230. package/dist/core/SignalEvaluator.js +326 -0
  231. package/dist/core/SignalEvaluator.js.map +1 -0
  232. package/dist/core/SignalProcessor.d.ts +152 -0
  233. package/dist/core/SignalProcessor.d.ts.map +1 -0
  234. package/dist/core/SignalProcessor.js +555 -0
  235. package/dist/core/SignalProcessor.js.map +1 -0
  236. package/dist/core/Step.d.ts +43 -32
  237. package/dist/core/Step.d.ts.map +1 -1
  238. package/dist/core/Step.js +220 -126
  239. package/dist/core/Step.js.map +1 -1
  240. package/dist/core/StreamingToolExecutor.d.ts +2 -2
  241. package/dist/core/StreamingToolExecutor.d.ts.map +1 -1
  242. package/dist/core/StreamingToolExecutor.js.map +1 -1
  243. package/dist/core/ToolManager.d.ts +44 -13
  244. package/dist/core/ToolManager.d.ts.map +1 -1
  245. package/dist/core/ToolManager.js +174 -91
  246. package/dist/core/ToolManager.js.map +1 -1
  247. package/dist/core/createAgent.d.ts +35 -0
  248. package/dist/core/createAgent.d.ts.map +1 -0
  249. package/dist/core/createAgent.js +36 -0
  250. package/dist/core/createAgent.js.map +1 -0
  251. package/dist/core/flow-namespace.d.ts +49 -0
  252. package/dist/core/flow-namespace.d.ts.map +1 -0
  253. package/dist/core/flow-namespace.js +168 -0
  254. package/dist/core/flow-namespace.js.map +1 -0
  255. package/dist/index.d.ts +11 -14
  256. package/dist/index.d.ts.map +1 -1
  257. package/dist/index.js +9 -12
  258. package/dist/index.js.map +1 -1
  259. package/dist/types/agent.d.ts +183 -54
  260. package/dist/types/agent.d.ts.map +1 -1
  261. package/dist/types/agent.js +0 -6
  262. package/dist/types/agent.js.map +1 -1
  263. package/dist/types/ai.d.ts +3 -3
  264. package/dist/types/ai.d.ts.map +1 -1
  265. package/dist/types/errors.d.ts +15 -0
  266. package/dist/types/errors.d.ts.map +1 -0
  267. package/dist/types/errors.js +18 -0
  268. package/dist/types/errors.js.map +1 -0
  269. package/dist/types/flow.d.ts +513 -0
  270. package/dist/types/flow.d.ts.map +1 -0
  271. package/dist/types/flow.js +5 -0
  272. package/dist/types/flow.js.map +1 -0
  273. package/dist/types/index.d.ts +7 -6
  274. package/dist/types/index.d.ts.map +1 -1
  275. package/dist/types/index.js +4 -1
  276. package/dist/types/index.js.map +1 -1
  277. package/dist/types/persistence.d.ts +11 -7
  278. package/dist/types/persistence.d.ts.map +1 -1
  279. package/dist/types/routing.d.ts +1 -1
  280. package/dist/types/routing.d.ts.map +1 -1
  281. package/dist/types/session.d.ts +24 -23
  282. package/dist/types/session.d.ts.map +1 -1
  283. package/dist/types/signals.d.ts +248 -0
  284. package/dist/types/signals.d.ts.map +1 -0
  285. package/dist/types/signals.js +10 -0
  286. package/dist/types/signals.js.map +1 -0
  287. package/dist/types/template.d.ts +2 -8
  288. package/dist/types/template.d.ts.map +1 -1
  289. package/dist/types/tool.d.ts +36 -29
  290. package/dist/types/tool.d.ts.map +1 -1
  291. package/dist/types/tool.js +1 -1
  292. package/dist/types/tool.js.map +1 -1
  293. package/dist/utils/condition.d.ts +7 -1
  294. package/dist/utils/condition.d.ts.map +1 -1
  295. package/dist/utils/condition.js.map +1 -1
  296. package/dist/utils/id.d.ts +13 -5
  297. package/dist/utils/id.d.ts.map +1 -1
  298. package/dist/utils/id.js +22 -9
  299. package/dist/utils/id.js.map +1 -1
  300. package/dist/utils/index.d.ts +2 -2
  301. package/dist/utils/index.d.ts.map +1 -1
  302. package/dist/utils/index.js +2 -2
  303. package/dist/utils/index.js.map +1 -1
  304. package/dist/utils/session.d.ts +44 -5
  305. package/dist/utils/session.d.ts.map +1 -1
  306. package/dist/utils/session.js +193 -37
  307. package/dist/utils/session.js.map +1 -1
  308. package/docs/README.md +15 -202
  309. package/docs/concepts/architecture.md +281 -0
  310. package/docs/concepts/directives.md +400 -0
  311. package/docs/concepts/pipeline.md +399 -0
  312. package/docs/guides/branching.md +263 -0
  313. package/docs/guides/compaction.md +163 -0
  314. package/docs/guides/conditions.md +167 -0
  315. package/docs/guides/error-handling.md +176 -0
  316. package/docs/guides/flow-control.md +409 -0
  317. package/docs/guides/instructions.md +210 -0
  318. package/docs/guides/persistence.md +182 -0
  319. package/docs/guides/streaming.md +137 -0
  320. package/docs/migration/README.md +15 -0
  321. package/docs/migration/route-to-flow.md +560 -0
  322. package/docs/migration/v1-to-v2.md +909 -0
  323. package/docs/reference/adapters.md +481 -0
  324. package/docs/reference/branches.md +241 -0
  325. package/docs/reference/create-agent.md +186 -0
  326. package/docs/reference/directive.md +243 -0
  327. package/docs/reference/errors.md +122 -0
  328. package/docs/reference/flow.md +238 -0
  329. package/docs/reference/instruction.md +177 -0
  330. package/docs/reference/pre-directive.md +131 -0
  331. package/docs/reference/providers.md +227 -0
  332. package/docs/reference/signals.md +356 -0
  333. package/docs/reference/step.md +339 -0
  334. package/docs/reference/tool.md +269 -0
  335. package/docs/start/01-install.md +81 -0
  336. package/docs/start/02-first-agent.md +196 -0
  337. package/docs/start/03-collect-data.md +222 -0
  338. package/docs/start/04-add-tools.md +276 -0
  339. package/docs/start/05-go-to-production.md +216 -0
  340. package/examples/01-quickstart.ts +20 -0
  341. package/examples/02-data-extraction.ts +90 -0
  342. package/examples/03-tools.ts +136 -0
  343. package/examples/04-instructions.ts +100 -0
  344. package/examples/05-branching.ts +140 -0
  345. package/examples/06-flow-control.ts +103 -0
  346. package/examples/07-streaming.ts +69 -0
  347. package/examples/08-persistence.ts +98 -0
  348. package/examples/09-signals.ts +144 -0
  349. package/examples/tsconfig.json +30 -0
  350. package/package.json +2 -1
  351. package/src/adapters/MemoryAdapter.ts +3 -3
  352. package/src/adapters/MongoAdapter.ts +3 -3
  353. package/src/adapters/OpenSearchAdapter.ts +10 -8
  354. package/src/adapters/PostgreSQLAdapter.ts +26 -10
  355. package/src/adapters/PrismaAdapter.ts +6 -6
  356. package/src/adapters/RedisAdapter.ts +3 -3
  357. package/src/adapters/SQLiteAdapter.ts +31 -12
  358. package/src/constants/index.ts +2 -10
  359. package/src/core/Agent.ts +585 -374
  360. package/src/core/AutoChainExecutor.ts +440 -0
  361. package/src/core/BranchEvaluator.ts +167 -0
  362. package/src/core/DirectiveBus.ts +248 -0
  363. package/src/core/DirectiveChainTracker.ts +144 -0
  364. package/src/core/Flow.ts +666 -0
  365. package/src/core/{RoutingEngine.ts → FlowRouter.ts} +385 -365
  366. package/src/core/PersistenceManager.ts +8 -8
  367. package/src/core/PromptComposer.ts +209 -140
  368. package/src/core/PromptSectionCache.ts +1 -1
  369. package/src/core/ResponseEngine.ts +61 -46
  370. package/src/core/ResponseModal.ts +1453 -1240
  371. package/src/core/ResponsePipeline.ts +655 -175
  372. package/src/core/SignalEvaluator.ts +420 -0
  373. package/src/core/SignalProcessor.ts +723 -0
  374. package/src/core/Step.ts +279 -176
  375. package/src/core/StreamingToolExecutor.ts +4 -4
  376. package/src/core/ToolManager.ts +200 -97
  377. package/src/core/createAgent.ts +40 -0
  378. package/src/core/flow-namespace.ts +219 -0
  379. package/src/index.ts +42 -36
  380. package/src/types/agent.ts +182 -53
  381. package/src/types/ai.ts +3 -3
  382. package/src/types/errors.ts +18 -0
  383. package/src/types/flow.ts +590 -0
  384. package/src/types/index.ts +43 -16
  385. package/src/types/persistence.ts +12 -8
  386. package/src/types/routing.ts +1 -1
  387. package/src/types/session.ts +26 -23
  388. package/src/types/signals.ts +321 -0
  389. package/src/types/template.ts +3 -11
  390. package/src/types/tool.ts +50 -42
  391. package/src/utils/condition.ts +13 -4
  392. package/src/utils/id.ts +27 -9
  393. package/src/utils/index.ts +6 -2
  394. package/src/utils/session.ts +238 -42
  395. package/dist/cjs/core/BatchExecutor.d.ts +0 -359
  396. package/dist/cjs/core/BatchExecutor.d.ts.map +0 -1
  397. package/dist/cjs/core/BatchExecutor.js +0 -861
  398. package/dist/cjs/core/BatchExecutor.js.map +0 -1
  399. package/dist/cjs/core/BatchPromptBuilder.d.ts +0 -89
  400. package/dist/cjs/core/BatchPromptBuilder.d.ts.map +0 -1
  401. package/dist/cjs/core/BatchPromptBuilder.js +0 -223
  402. package/dist/cjs/core/BatchPromptBuilder.js.map +0 -1
  403. package/dist/cjs/core/Route.d.ts +0 -180
  404. package/dist/cjs/core/Route.d.ts.map +0 -1
  405. package/dist/cjs/core/Route.js +0 -542
  406. package/dist/cjs/core/Route.js.map +0 -1
  407. package/dist/cjs/core/RoutingEngine.d.ts +0 -185
  408. package/dist/cjs/core/RoutingEngine.d.ts.map +0 -1
  409. package/dist/cjs/core/RoutingEngine.js.map +0 -1
  410. package/dist/cjs/types/route.d.ts +0 -336
  411. package/dist/cjs/types/route.d.ts.map +0 -1
  412. package/dist/cjs/types/route.js.map +0 -1
  413. package/dist/core/BatchExecutor.d.ts +0 -359
  414. package/dist/core/BatchExecutor.d.ts.map +0 -1
  415. package/dist/core/BatchExecutor.js +0 -856
  416. package/dist/core/BatchExecutor.js.map +0 -1
  417. package/dist/core/BatchPromptBuilder.d.ts +0 -89
  418. package/dist/core/BatchPromptBuilder.d.ts.map +0 -1
  419. package/dist/core/BatchPromptBuilder.js +0 -219
  420. package/dist/core/BatchPromptBuilder.js.map +0 -1
  421. package/dist/core/Route.d.ts +0 -180
  422. package/dist/core/Route.d.ts.map +0 -1
  423. package/dist/core/Route.js +0 -538
  424. package/dist/core/Route.js.map +0 -1
  425. package/dist/core/RoutingEngine.d.ts +0 -185
  426. package/dist/core/RoutingEngine.d.ts.map +0 -1
  427. package/dist/core/RoutingEngine.js.map +0 -1
  428. package/dist/types/route.d.ts +0 -336
  429. package/dist/types/route.d.ts.map +0 -1
  430. package/dist/types/route.js +0 -5
  431. package/dist/types/route.js.map +0 -1
  432. package/docs/CONTRIBUTING.md +0 -521
  433. package/docs/api/README.md +0 -3299
  434. package/docs/api/overview.md +0 -1410
  435. package/docs/architecture/data-extraction-flow.md +0 -360
  436. package/docs/architecture/multi-step-execution.md +0 -277
  437. package/docs/core/agent/README.md +0 -938
  438. package/docs/core/agent/context-management.md +0 -796
  439. package/docs/core/agent/rules-and-prohibitions.md +0 -113
  440. package/docs/core/agent/session-management.md +0 -693
  441. package/docs/core/ai-integration/prompt-composition.md +0 -355
  442. package/docs/core/ai-integration/providers.md +0 -515
  443. package/docs/core/ai-integration/response-processing.md +0 -433
  444. package/docs/core/conversation-flows/data-collection.md +0 -772
  445. package/docs/core/conversation-flows/route-dsl.md +0 -509
  446. package/docs/core/conversation-flows/routes.md +0 -249
  447. package/docs/core/conversation-flows/step-transitions.md +0 -731
  448. package/docs/core/conversation-flows/steps.md +0 -268
  449. package/docs/core/error-handling.md +0 -830
  450. package/docs/core/persistence/adapters.md +0 -255
  451. package/docs/core/persistence/session-storage.md +0 -656
  452. package/docs/core/routing/intelligent-routing.md +0 -470
  453. package/docs/core/tools/enhanced-tool.md +0 -186
  454. package/docs/core/tools/streaming-execution.md +0 -161
  455. package/docs/core/tools/tool-definition.md +0 -970
  456. package/docs/core/tools/tool-scoping.md +0 -819
  457. package/docs/guides/advanced-patterns/publishing.md +0 -186
  458. package/docs/guides/context-compaction.md +0 -96
  459. package/docs/guides/error-handling-patterns.md +0 -578
  460. package/docs/guides/getting-started/README.md +0 -795
  461. package/docs/guides/migration/README.md +0 -101
  462. package/docs/guides/migration/flexible-routing-conditions.md +0 -375
  463. package/docs/guides/migration/multi-step-execution.md +0 -393
  464. package/docs/guides/migration/response-modal-refactor.md +0 -518
  465. package/docs/guides/prompt-optimization.md +0 -164
  466. package/examples/advanced-patterns/context-compaction.ts +0 -223
  467. package/examples/advanced-patterns/knowledge-based-agent.ts +0 -735
  468. package/examples/advanced-patterns/persistent-onboarding.ts +0 -728
  469. package/examples/advanced-patterns/route-lifecycle-hooks.ts +0 -556
  470. package/examples/advanced-patterns/streaming-responses.ts +0 -656
  471. package/examples/ai-providers/anthropic-integration.ts +0 -388
  472. package/examples/ai-providers/openai-integration.ts +0 -228
  473. package/examples/condition-patterns/function-only-conditions.ts +0 -365
  474. package/examples/condition-patterns/mixed-array-conditions.ts +0 -477
  475. package/examples/condition-patterns/route-skipif-patterns.ts +0 -468
  476. package/examples/condition-patterns/step-skipif-patterns.ts +0 -0
  477. package/examples/condition-patterns/string-only-conditions.ts +0 -296
  478. package/examples/conversation-flows/completion-transitions.ts +0 -318
  479. package/examples/core-concepts/basic-agent.ts +0 -503
  480. package/examples/core-concepts/modern-streaming-api.ts +0 -309
  481. package/examples/core-concepts/schema-driven-extraction.ts +0 -332
  482. package/examples/core-concepts/session-management.ts +0 -494
  483. package/examples/integrations/database-integration.ts +0 -631
  484. package/examples/integrations/healthcare-integration.ts +0 -595
  485. package/examples/integrations/search-integration.ts +0 -530
  486. package/examples/integrations/server-session-management.ts +0 -307
  487. package/examples/persistence/custom-adapter.ts +0 -526
  488. package/examples/persistence/database-persistence.ts +0 -583
  489. package/examples/persistence/memory-sessions.ts +0 -495
  490. package/examples/persistence/prisma-schema.example.prisma +0 -74
  491. package/examples/persistence/redis-persistence.ts +0 -488
  492. package/examples/tools/basic-tools.ts +0 -765
  493. package/examples/tools/data-enrichment-tools.ts +0 -593
  494. package/examples/tools/enhanced-tool-metadata.ts +0 -268
  495. package/examples/tools/streaming-tool-execution.ts +0 -283
  496. package/src/core/BatchExecutor.ts +0 -1187
  497. package/src/core/BatchPromptBuilder.ts +0 -299
  498. package/src/core/Route.ts +0 -678
  499. package/src/types/route.ts +0 -392
package/src/core/Step.ts CHANGED
@@ -5,19 +5,27 @@
5
5
  import type {
6
6
  StepRef,
7
7
  StepOptions,
8
- StepResult,
9
- BranchSpec,
10
- BranchResult,
11
- Guideline,
12
- GuidelineMatch,
8
+ BranchMap,
9
+ Instruction,
13
10
  Tool,
14
- SessionState,
15
- Event,
11
+ PrepareResult,
16
12
  } from "../types";
17
- import { ToolScope, Template, ConditionTemplate, TemplateContext } from "../types";
18
- import { createConditionEvaluator, generateStepId, logger } from "../utils";
13
+ import type { SessionState } from "../types/session";
14
+ import type { StepResult, BranchSpec, BranchResult } from "../types/flow";
15
+ import { ToolScope, Template, TemplateContext } from "../types";
16
+ import type { ConditionWhen, ConditionIf } from "../types/flow";
17
+ import { generateStepId, logger } from "../utils";
19
18
  import { Agent } from './Agent'
20
- import { END_ROUTE, END_ROUTE_ID } from "../constants";
19
+
20
+ /**
21
+ * Error thrown when a step's configuration violates auto-step constraints.
22
+ */
23
+ export class FlowConfigurationError extends Error {
24
+ constructor(message: string) {
25
+ super(message);
26
+ this.name = "FlowConfigurationError";
27
+ }
28
+ }
21
29
 
22
30
  /**
23
31
  * Represents a step within a route
@@ -25,50 +33,124 @@ import { END_ROUTE, END_ROUTE_ID } from "../constants";
25
33
  export class Step<TContext = unknown, TData = unknown> {
26
34
  public readonly id: string;
27
35
  private nextSteps: Step<TContext, TData>[] = [];
28
- private guidelines: Guideline<TContext, TData>[] = [];
29
- public readonly routeId: string;
36
+ private _instructions: Instruction<TContext, TData>[] = [];
37
+ public readonly flowId: string;
30
38
  public collect?: (keyof TData)[];
31
39
  public description?: string;
32
- public when?: ConditionTemplate<TContext, TData>;
33
- public skipIf?: ConditionTemplate<TContext, TData>;
40
+ public when?: ConditionWhen;
41
+ public if?: ConditionIf<TContext, TData>;
42
+ public skip?: ConditionIf<TContext, TData>;
34
43
  public requires?: (keyof TData)[];
35
44
  public prompt?: Template<TContext, TData>;
45
+ public reply?: Template<TContext, TData>;
36
46
  public prepare?:
37
47
  | string
38
48
  | Tool<TContext, TData>
39
- | ((context: TContext, data?: Partial<TData>) => void | Promise<void>);
49
+ | ((context: TContext, data?: Partial<TData>) => void | PrepareResult | Promise<void | PrepareResult>);
40
50
  public finalize?:
41
51
  | string
42
52
  | Tool<TContext, TData>
43
- | ((context: TContext, data?: Partial<TData>) => void | Promise<void>);
53
+ | ((context: TContext, data?: Partial<TData>) => void | PrepareResult | Promise<void | PrepareResult>);
44
54
  public tools?: (string | Tool<TContext, TData>)[];
45
55
 
56
+ private readonly _auto: boolean;
57
+ private readonly _branches?: BranchMap<TContext, TData>;
58
+
59
+ /** Whether this step runs without an LLM call (computational only). */
60
+ get auto(): boolean {
61
+ return this._auto;
62
+ }
63
+
64
+ /** Explicit source-local fork entries, if declared. */
65
+ get branches(): BranchMap<TContext, TData> | undefined {
66
+ return this._branches;
67
+ }
68
+
46
69
  // Reference to parent agent for ToolManager access
47
- private parentAgent?: Agent<TContext, TData>;
70
+ private parentAgent?: Agent<TContext, TData>;
48
71
 
49
72
  constructor(
50
73
  routeId: string,
51
74
  options: StepOptions<TContext, TData> = {},
52
- parentAgent?: Agent<TContext, TData>
75
+ parentAgent?: Agent<TContext, TData>
53
76
  ) {
54
77
  // Use provided ID or generate a deterministic one
55
78
  this.id = options.id || generateStepId(routeId, options.description);
56
- this.routeId = routeId;
79
+ this.flowId = routeId;
57
80
  this.description = options.description;
58
81
 
82
+ this._auto = options.auto ?? false;
83
+ this._branches = options.branches;
59
84
  this.collect = options.collect;
60
- this.skipIf = options.skipIf;
85
+ this.skip = options.skip;
61
86
  this.requires = options.requires;
62
87
  this.prompt = options.prompt;
88
+ this.reply = options.reply;
63
89
  this.when = options.when;
90
+ this.if = options.if;
64
91
  this.prepare = options.prepare;
65
92
  this.finalize = options.finalize;
66
93
  this.tools = options.tools;
67
94
 
68
- // Initialize guidelines from options
69
- if (options.guidelines) {
70
- options.guidelines.forEach((guideline) => {
71
- this.addGuideline(guideline);
95
+ // Validate when/if split: functions belong on `if`, not `when`
96
+ if (this.when !== undefined) {
97
+ const whenValue = this.when;
98
+ if (typeof whenValue === 'function') {
99
+ throw new FlowConfigurationError(
100
+ `[FlowConfigurationError] Step "${this.id}" has a function on "when": functions belong on "if" only. Move the function to the "if" field.`
101
+ );
102
+ }
103
+ if (Array.isArray(whenValue)) {
104
+ for (let i = 0; i < whenValue.length; i++) {
105
+ if (typeof whenValue[i] === 'function') {
106
+ throw new FlowConfigurationError(
107
+ `[FlowConfigurationError] Step "${this.id}" has a function at "when[${i}]": functions belong on "if" only. Move the function to the "if" field.`
108
+ );
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ // Validate auto-step shape: auto-steps cannot define prompt, collect, tools, or finalize
115
+ if (this._auto) {
116
+ const violatingFields: string[] = [];
117
+ if (this.prompt != null) violatingFields.push('prompt');
118
+ if (this.collect != null && this.collect.length > 0) violatingFields.push('collect');
119
+ if (this.tools != null && this.tools.length > 0) violatingFields.push('tools');
120
+ if (this.finalize != null) violatingFields.push('finalize');
121
+
122
+ if (violatingFields.length > 0) {
123
+ throw new FlowConfigurationError(
124
+ `[FlowConfigurationError] Auto-step "${this.id}" cannot define: ${violatingFields.join(', ')}. Auto-steps run without an LLM call and must not declare prompt, collect, tools, or finalize. Remove these fields or set auto: false.`
125
+ );
126
+ }
127
+ }
128
+
129
+ // Validate reply-step shape: reply steps cannot define prompt, collect, tools, finalize, or auto: true
130
+ if (this.reply != null) {
131
+ const conflictingFields: string[] = [];
132
+ if (this.prompt != null) conflictingFields.push('prompt');
133
+ if (this.collect != null && this.collect.length > 0) conflictingFields.push('collect');
134
+ if (this.tools != null && this.tools.length > 0) conflictingFields.push('tools');
135
+ if (this.finalize != null) conflictingFields.push('finalize');
136
+ if (this._auto) conflictingFields.push('auto: true');
137
+
138
+ if (conflictingFields.length > 0) {
139
+ throw new FlowConfigurationError(
140
+ `[FlowConfigurationError] Step "${this.id}" sets "reply" together with conflicting fields: ${conflictingFields.join(', ')}. A reply step skips the LLM call and cannot define prompt, collect, tools, finalize, or auto: true.`
141
+ );
142
+ }
143
+ }
144
+
145
+ // Validate branches shape
146
+ if (this._branches !== undefined) {
147
+ this.validateBranches(this._branches);
148
+ }
149
+
150
+ // Initialize instructions from options
151
+ if (options.instructions) {
152
+ options.instructions.forEach((instruction) => {
153
+ this.addInstruction(instruction);
72
154
  });
73
155
  }
74
156
 
@@ -83,7 +165,7 @@ export class Step<TContext = unknown, TData = unknown> {
83
165
  configure(config: {
84
166
  description?: string;
85
167
  collect?: (keyof TData)[];
86
- skipIf?: ConditionTemplate<TContext, TData>;
168
+ skip?: ConditionIf<TContext, TData>;
87
169
  requires?: (keyof TData)[];
88
170
  prompt?: Template<TContext, TData>;
89
171
  prepare?:
@@ -104,8 +186,8 @@ export class Step<TContext = unknown, TData = unknown> {
104
186
  this.collect = config.collect;
105
187
  }
106
188
 
107
- if (config.skipIf !== undefined) {
108
- this.skipIf = config.skipIf;
189
+ if (config.skip !== undefined) {
190
+ this.skip = config.skip;
109
191
  }
110
192
 
111
193
  if (config.requires !== undefined) {
@@ -127,21 +209,6 @@ export class Step<TContext = unknown, TData = unknown> {
127
209
  return this;
128
210
  }
129
211
 
130
- /**
131
- * Shortcut to end the current route
132
- *
133
- * @param options - Optional step options for the end step
134
- * @returns Terminal step result
135
- */
136
- endRoute(
137
- options: Omit<StepOptions<TContext, TData>, "step"> = {}
138
- ): StepResult<TContext, TData> {
139
- return this.nextStep({
140
- ...options,
141
- step: END_ROUTE,
142
- });
143
- }
144
-
145
212
  /**
146
213
  * Create a transition from this step to another
147
214
  *
@@ -149,25 +216,8 @@ export class Step<TContext = unknown, TData = unknown> {
149
216
  * @returns StepResult that supports chaining
150
217
  */
151
218
  nextStep(spec: StepOptions<TContext, TData>): StepResult<TContext, TData> {
152
- // Handle END_ROUTE
153
- if (spec.step && typeof spec.step === "symbol" && spec.step === END_ROUTE) {
154
- const endStep = new Step<TContext, TData>(this.routeId, {
155
- ...spec,
156
- id: END_ROUTE_ID,
157
- }, this.parentAgent);
158
- this.nextSteps.push(endStep);
159
- return this.createTerminalRef();
160
- }
161
-
162
- // Handle direct step reference
163
- if (spec.step && typeof spec.step !== "symbol") {
164
- // This is a bit tricky. We need to find the actual Step instance.
165
- // For now, let's assume the user will provide a Step instance directly.
166
- // This part might need to be revisited.
167
- }
168
-
169
219
  // Create new target step for prompt or tool
170
- const targetStep = new Step<TContext, TData>(this.routeId, spec, this.parentAgent);
220
+ const targetStep = new Step<TContext, TData>(this.flowId, spec, this.parentAgent);
171
221
  this.nextSteps.push(targetStep);
172
222
 
173
223
  return this.createStepRefWithTransition(targetStep.getRef(), targetStep);
@@ -191,7 +241,7 @@ export class Step<TContext = unknown, TData = unknown> {
191
241
  : branchSpec.step;
192
242
 
193
243
  // Create a new step for this branch
194
- const branchStep = new Step<TContext, TData>(this.routeId, stepOptions, this.parentAgent);
244
+ const branchStep = new Step<TContext, TData>(this.flowId, stepOptions, this.parentAgent);
195
245
  // Add it to our transitions
196
246
  this.nextSteps.push(branchStep);
197
247
  // Create a step result for chaining
@@ -206,66 +256,22 @@ export class Step<TContext = unknown, TData = unknown> {
206
256
  }
207
257
 
208
258
  /**
209
- * Add a guideline specific to this step
210
- */
211
- addGuideline(guideline: Guideline<TContext, TData>): void {
212
- this.guidelines.push(guideline);
213
- }
214
-
215
- /**
216
- * Get guidelines for this step
259
+ * Add an instruction to this step.
217
260
  */
218
- getGuidelines(): Guideline<TContext, TData>[] {
219
- return [...this.guidelines];
261
+ addInstruction(instruction: Instruction<TContext, TData>): void {
262
+ this._instructions.push({
263
+ ...instruction,
264
+ kind: instruction.kind || 'should' as const,
265
+ id: instruction.id || `instruction_${this.id}_${this._instructions.length}`,
266
+ enabled: instruction.enabled !== false, // Default to true
267
+ });
220
268
  }
221
269
 
222
270
  /**
223
- * Evaluate and match active guidelines based on their conditions
224
- * Returns guidelines that should be active given the current context
271
+ * Get instructions for this step
225
272
  */
226
- async evaluateGuidelines(
227
- context?: TContext,
228
- session?: SessionState<TData>,
229
- history?: Event[]
230
- ): Promise<GuidelineMatch<TContext, TData>[]> {
231
- const templateContext = { context, session, history, data: session?.data || {} };
232
- const evaluator = createConditionEvaluator(templateContext);
233
- const matches: GuidelineMatch<TContext, TData>[] = [];
234
-
235
- for (const guideline of this.guidelines) {
236
- // Skip disabled guidelines
237
- if (guideline.enabled === false) {
238
- continue;
239
- }
240
-
241
- if (guideline.condition) {
242
- const evaluation = await evaluator.evaluateCondition(guideline.condition, 'AND');
243
-
244
- // Include guideline if:
245
- // 1. No programmatic conditions (only strings) - always active
246
- // 2. Programmatic conditions evaluate to true
247
- if (!evaluation.hasProgrammaticConditions || evaluation.programmaticResult) {
248
- const rationale = evaluation.aiContextStrings.length > 0
249
- ? `Condition met: ${evaluation.aiContextStrings.join(" AND ")}`
250
- : evaluation.hasProgrammaticConditions
251
- ? "Programmatic condition evaluated to true"
252
- : "Always active (no conditions)";
253
-
254
- matches.push({
255
- guideline,
256
- rationale
257
- });
258
- }
259
- } else {
260
- // No condition means always active
261
- matches.push({
262
- guideline,
263
- rationale: "Always active (no conditions)"
264
- });
265
- }
266
- }
267
-
268
- return matches;
273
+ getInstructions(): Instruction<TContext, TData>[] {
274
+ return [...this._instructions];
269
275
  }
270
276
 
271
277
  /**
@@ -356,7 +362,7 @@ export class Step<TContext = unknown, TData = unknown> {
356
362
  if (!this.parentAgent?.tool) {
357
363
  // Fallback to local resolution if no ToolManager available
358
364
  const resolved = this.resolveTools();
359
- return resolved.find(tool => tool.id === toolId || tool.name === toolId);
365
+ return resolved.find(tool => tool.id === toolId || tool.id === toolId);
360
366
  }
361
367
 
362
368
  // Use ToolManager to find the tool with proper scope resolution
@@ -419,7 +425,7 @@ export class Step<TContext = unknown, TData = unknown> {
419
425
  const resolvedIds = resolved.map(tool => tool.id);
420
426
  const missing = toolIds.filter(id => !resolvedIds.includes(id));
421
427
  const found = toolIds.filter(id => resolvedIds.includes(id));
422
-
428
+
423
429
  return {
424
430
  valid: missing.length === 0,
425
431
  missing,
@@ -440,58 +446,123 @@ export class Step<TContext = unknown, TData = unknown> {
440
446
  }
441
447
 
442
448
  /**
443
- * Evaluate when condition using ConditionEvaluator
449
+ * Evaluate when/if conditions using the v2 split logic.
450
+ * `if` (code predicate) evaluates first (free); `when` (AI) evaluates only when `if` passes.
451
+ * Both are combined with AND semantics.
444
452
  */
445
453
  async evaluateWhen(
446
454
  templateContext: TemplateContext<TContext, TData>
447
- ): Promise<{
448
- shouldActivate: boolean;
455
+ ): Promise<{
456
+ shouldActivate: boolean;
449
457
  aiContextStrings: string[];
450
458
  hasProgrammaticConditions: boolean;
451
459
  }> {
452
- if (!this.when) {
453
- return {
454
- shouldActivate: true,
455
- aiContextStrings: [],
456
- hasProgrammaticConditions: false
460
+ // If neither `when` nor `if` is set, step is always eligible
461
+ if (!this.when && !this.if) {
462
+ return {
463
+ shouldActivate: true,
464
+ aiContextStrings: [],
465
+ hasProgrammaticConditions: false
466
+ };
467
+ }
468
+
469
+ // Evaluate `if` first (free, code-only)
470
+ if (this.if) {
471
+ const predicates = Array.isArray(this.if) ? this.if : [this.if];
472
+ for (const predicate of predicates) {
473
+ try {
474
+ const result = await predicate({
475
+ data: templateContext.data,
476
+ context: templateContext.context as TContext,
477
+ session: templateContext.session as SessionState<TData>,
478
+ history: templateContext.history || [],
479
+ });
480
+ if (!result) {
481
+ // `if` failed — short-circuit, don't bother with `when`
482
+ return {
483
+ shouldActivate: false,
484
+ aiContextStrings: [],
485
+ hasProgrammaticConditions: true
486
+ };
487
+ }
488
+ } catch (error) {
489
+ logger.warn(`[Step] "if" predicate failed for step "${this.id}":`, error);
490
+ return {
491
+ shouldActivate: false,
492
+ aiContextStrings: [],
493
+ hasProgrammaticConditions: true
494
+ };
495
+ }
496
+ }
497
+ }
498
+
499
+ // `if` passed (or was absent) — now evaluate `when` (AI-evaluated strings)
500
+ if (this.when) {
501
+ const whenStrings = Array.isArray(this.when) ? this.when : [this.when];
502
+ // `when` strings are handed to the AI — return them as aiContextStrings
503
+ // The programmatic result is true (strings don't fail programmatically;
504
+ // they're scored by the AI at routing time)
505
+ return {
506
+ shouldActivate: true,
507
+ aiContextStrings: whenStrings,
508
+ hasProgrammaticConditions: !!this.if
457
509
  };
458
510
  }
459
511
 
460
- const evaluator = createConditionEvaluator(templateContext);
461
- const result = await evaluator.evaluateCondition(this.when, 'AND');
462
-
512
+ // Only `if` was set and it passed
463
513
  return {
464
- shouldActivate: result.programmaticResult,
465
- aiContextStrings: result.aiContextStrings,
466
- hasProgrammaticConditions: result.hasProgrammaticConditions
514
+ shouldActivate: true,
515
+ aiContextStrings: [],
516
+ hasProgrammaticConditions: true
467
517
  };
468
518
  }
469
519
 
470
520
  /**
471
- * Evaluate skipIf condition using ConditionEvaluator
521
+ * Evaluate the skip condition (if-only shape — code predicates, OR semantics).
522
+ * Returns true if the step should be skipped.
472
523
  */
473
- async evaluateSkipIf(
524
+ async evaluateSkip(
474
525
  templateContext: TemplateContext<TContext, TData>
475
- ): Promise<{
476
- shouldSkip: boolean;
526
+ ): Promise<{
527
+ shouldSkip: boolean;
477
528
  aiContextStrings: string[];
478
529
  hasProgrammaticConditions: boolean;
479
530
  }> {
480
- if (!this.skipIf) {
481
- return {
482
- shouldSkip: false,
483
- aiContextStrings: [],
484
- hasProgrammaticConditions: false
531
+ if (!this.skip) {
532
+ return {
533
+ shouldSkip: false,
534
+ aiContextStrings: [],
535
+ hasProgrammaticConditions: false
485
536
  };
486
537
  }
487
538
 
488
- const evaluator = createConditionEvaluator(templateContext);
489
- const result = await evaluator.evaluateCondition(this.skipIf, 'OR');
490
-
539
+ const predicates = Array.isArray(this.skip) ? this.skip : [this.skip];
540
+ // OR semantics: if ANY predicate returns true, skip
541
+ for (const predicate of predicates) {
542
+ try {
543
+ const result = await predicate({
544
+ data: templateContext.data,
545
+ context: templateContext.context as TContext,
546
+ session: templateContext.session as SessionState<TData>,
547
+ history: templateContext.history || [],
548
+ });
549
+ if (result) {
550
+ return {
551
+ shouldSkip: true,
552
+ aiContextStrings: [],
553
+ hasProgrammaticConditions: true
554
+ };
555
+ }
556
+ } catch (error) {
557
+ logger.warn(`[Step] "skip" predicate failed for step "${this.id}":`, error);
558
+ // On error, default to not skipping (safe fallback)
559
+ }
560
+ }
561
+
491
562
  return {
492
- shouldSkip: result.programmaticResult,
493
- aiContextStrings: result.aiContextStrings,
494
- hasProgrammaticConditions: result.hasProgrammaticConditions
563
+ shouldSkip: false,
564
+ aiContextStrings: [],
565
+ hasProgrammaticConditions: true
495
566
  };
496
567
  }
497
568
 
@@ -511,7 +582,7 @@ export class Step<TContext = unknown, TData = unknown> {
511
582
  getRef(): StepRef {
512
583
  return {
513
584
  id: this.id,
514
- routeId: this.routeId,
585
+ flowId: this.flowId,
515
586
  };
516
587
  }
517
588
 
@@ -530,30 +601,6 @@ export class Step<TContext = unknown, TData = unknown> {
530
601
  stepInstance.nextStep(spec),
531
602
  branch: (branches: BranchSpec<TContext, TData>[]) =>
532
603
  stepInstance.branch(branches),
533
- endRoute: (options?: Omit<StepOptions<TContext, TData>, "step">) =>
534
- stepInstance.endRoute(options),
535
- };
536
- }
537
-
538
- /**
539
- * Create a terminal step reference (for END_ROUTE)
540
- */
541
- private createTerminalRef(): StepResult<TContext, TData> {
542
- const terminalRef: StepRef = {
543
- id: END_ROUTE_ID,
544
- routeId: this.routeId,
545
- };
546
-
547
- return {
548
- ...terminalRef,
549
- nextStep: () => {
550
- throw new Error("Cannot transition from END_ROUTE step");
551
- },
552
- branch: () => {
553
- throw new Error("Cannot branch from END_ROUTE step");
554
- },
555
- endRoute: (options?: Omit<StepOptions<TContext, TData>, "step">) =>
556
- this.endRoute(options),
557
604
  };
558
605
  }
559
606
 
@@ -566,8 +613,6 @@ export class Step<TContext = unknown, TData = unknown> {
566
613
  nextStep: (spec: StepOptions<TContext, TData>) => this.nextStep(spec),
567
614
  branch: (branches: BranchSpec<TContext, TData>[]) =>
568
615
  this.branch(branches),
569
- endRoute: (options?: Omit<StepOptions<TContext, TData>, "step">) =>
570
- this.endRoute(options),
571
616
  };
572
617
  }
573
618
 
@@ -579,15 +624,73 @@ export class Step<TContext = unknown, TData = unknown> {
579
624
  return {
580
625
  id: this.id,
581
626
  description: this.description,
627
+ auto: this._auto,
628
+ branches: this._branches,
582
629
  prompt: this.prompt,
630
+ reply: this.reply,
583
631
  tools: this.tools,
584
632
  prepare: this.prepare,
585
633
  finalize: this.finalize,
586
634
  collect: this.collect,
587
- skipIf: this.skipIf,
635
+ skip: this.skip,
588
636
  requires: this.requires,
589
637
  when: this.when,
590
- guidelines: this.getGuidelines(),
638
+ if: this.if,
639
+ instructions: this.getInstructions(),
591
640
  };
592
641
  }
642
+
643
+ /**
644
+ * Validate the branches array at construction time.
645
+ * Checks:
646
+ * - Non-empty array
647
+ * - Unconditional entries (no `when` and no `if`) only legal as the last entry
648
+ * - Directive `then` values: at most one position field, no empty `goTo: {}`
649
+ */
650
+ private validateBranches(branches: BranchMap<TContext, TData>): void {
651
+ if (branches.length === 0) {
652
+ throw new FlowConfigurationError(
653
+ `[FlowConfigurationError] Empty branches array on step "${this.id}": branches must contain at least one entry. Add branch entries or remove the branches field.`
654
+ );
655
+ }
656
+
657
+ const POSITION_FIELDS = ['goTo', 'goToStep', 'complete', 'abort', 'reset'] as const;
658
+
659
+ for (let i = 0; i < branches.length; i++) {
660
+ const entry = branches[i];
661
+ const isLast = i === branches.length - 1;
662
+
663
+ // Non-last entry without `when` or `if` is dead code — later entries are unreachable
664
+ if (!isLast && !entry.when && !entry.if) {
665
+ throw new FlowConfigurationError(
666
+ `[FlowConfigurationError] Dead-code branch at index ${i}: branches[${i}] has neither "when" nor "if" and is not the last entry. Entries after index ${i} are unreachable. Move the unconditional entry to the end or add a condition.`
667
+ );
668
+ }
669
+
670
+ // Validate Directive `then` values
671
+ if (entry.then && typeof entry.then === 'object') {
672
+ const directive = entry.then;
673
+
674
+ // Check for multiple position fields
675
+ const setPositionFields = POSITION_FIELDS.filter(
676
+ (field) => directive[field] !== undefined
677
+ );
678
+ if (setPositionFields.length > 1) {
679
+ throw new FlowConfigurationError(
680
+ `[FlowConfigurationError] Multiple position fields in branches[${i}].then: Directive sets ${setPositionFields.join(', ')}. At most one position field is allowed per Directive. Remove all but one.`
681
+ );
682
+ }
683
+
684
+ // Check for empty goTo: {}
685
+ if (directive.goTo !== undefined && typeof directive.goTo === 'object' && directive.goTo !== null) {
686
+ const goToObj = directive.goTo as Record<string, unknown>;
687
+ if (Object.keys(goToObj).length === 0) {
688
+ throw new FlowConfigurationError(
689
+ `[FlowConfigurationError] Empty goTo in branches[${i}].then: Directive has "goTo: {}" with no flow target. Provide at least a flow id or title.`
690
+ );
691
+ }
692
+ }
693
+ }
694
+ }
695
+ }
593
696
  }
@@ -16,7 +16,7 @@ import type {
16
16
  ToolExecutionUpdate,
17
17
  ToolExecutionResult,
18
18
  ToolContext,
19
- EnhancedTool,
19
+ Tool,
20
20
  TrackedTool,
21
21
  } from "../types/tool";
22
22
 
@@ -116,7 +116,7 @@ export class StreamingToolExecutor<TContext = unknown, TData = unknown> {
116
116
  */
117
117
  addTool(
118
118
  toolCall: ToolCallRequest,
119
- tool: EnhancedTool<TContext, TData>
119
+ tool: Tool<TContext, TData>
120
120
  ): void {
121
121
  if (this.discarded) {
122
122
  log.warn(`[StreamingToolExecutor] Executor discarded, ignoring tool: ${toolCall.toolName}`);
@@ -312,7 +312,7 @@ export class StreamingToolExecutor<TContext = unknown, TData = unknown> {
312
312
  */
313
313
  private normalizeResult(
314
314
  result: unknown,
315
- tool: EnhancedTool<TContext, TData>
315
+ tool: Tool<TContext, TData>
316
316
  ): ToolExecutionResult {
317
317
  if (
318
318
  result &&
@@ -346,7 +346,7 @@ export class StreamingToolExecutor<TContext = unknown, TData = unknown> {
346
346
  */
347
347
  private applyResultTruncation(
348
348
  result: ToolExecutionResult,
349
- tool: EnhancedTool<TContext, TData>
349
+ tool: Tool<TContext, TData>
350
350
  ): ToolExecutionResult {
351
351
  const maxChars = tool.maxResultSizeChars;
352
352
  if (maxChars == null || maxChars <= 0 || result.data == null) {