@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
@@ -0,0 +1,590 @@
1
+ /**
2
+ * Flow/Journey DSL type definitions
3
+ */
4
+
5
+ import type { Tool } from "./tool";
6
+ import type { StructuredSchema } from "./schema";
7
+ import type { Instruction, HookContext, ExitReason } from "./agent";
8
+ import type { Template } from "./template";
9
+
10
+ // ─── Condition types (v2 when/if split) ──────────────────────────────────────
11
+
12
+ /**
13
+ * Code-evaluated condition predicate. Returns `true` to pass, `false` to fail.
14
+ * May be async. Used on `if` fields (free to evaluate, no LLM cost).
15
+ */
16
+ export type ConditionPredicate<TContext = unknown, TData = unknown> = (
17
+ ctx: { data: Partial<TData>; context: TContext; session: import("./session").SessionState<TData>; history: Event[] }
18
+ ) => boolean | Promise<boolean>;
19
+
20
+ /**
21
+ * The `if` field shape: a single predicate or an array of predicates (AND semantics).
22
+ */
23
+ export type ConditionIf<TContext = unknown, TData = unknown> =
24
+ | ConditionPredicate<TContext, TData>
25
+ | ConditionPredicate<TContext, TData>[];
26
+
27
+ /**
28
+ * The `when` field shape: a single AI-evaluated string or array of strings (AND semantics).
29
+ * Functions are NOT allowed on `when` — they belong on `if` only.
30
+ */
31
+ export type ConditionWhen = string | string[];
32
+ import type { SessionState } from "./session";
33
+ import type { Event } from "./history";
34
+
35
+ /**
36
+ * Reason why execution stopped.
37
+ * Used to indicate the stopping condition for step execution.
38
+ *
39
+ * v2 vocabulary — see Requirements 17.1–17.9.
40
+ */
41
+ export type StoppedReason =
42
+ | 'needs_input' // Waiting for user input
43
+ | 'last_step' // Flow ended because the last step had no successor
44
+ | 'completed' // Flow ended because an explicit `complete` directive fired
45
+ | 'aborted' // Conversation aborted via `abort` directive
46
+ | 'goto' // Turn ended because a goTo/goToStep directive redirected
47
+ | 'reset' // Turn ended because a `reset` directive fired
48
+ | 'halt' // Pre-LLM halt directive — turn ends without LLM call
49
+ | 'reply' // Turn ended because a verbatim reply was emitted (no LLM call)
50
+ | 'max_auto_steps' // Auto-step chain exceeded maxAutoStepsPerTurn cap
51
+ | 'prepare_error' // Error in prepare hook
52
+ | 'llm_error' // Error during LLM call
53
+ | 'validation_error' // Error validating collected data
54
+ | 'finalize_error'; // Error in finalize hook (non-fatal, logged)
55
+
56
+ /**
57
+ * Result that a prepare/finalize hook may return to issue directives.
58
+ * All fields are optional — returning void is also valid.
59
+ */
60
+ export interface PrepareResult {
61
+ /** Partial data to merge into session.data */
62
+ dataUpdate?: Record<string, unknown>;
63
+ /** Partial context to merge into the agent context */
64
+ contextUpdate?: Record<string, unknown>;
65
+ /** If true, stop the auto-step chain and end the turn with `reply` */
66
+ halt?: boolean;
67
+ /** Verbatim reply to send when halting */
68
+ reply?: string;
69
+ /** Jump to a step within the current flow */
70
+ goToStep?: string;
71
+ /** Jump to another flow */
72
+ goTo?: string;
73
+ /** Mark the current flow as complete */
74
+ complete?: boolean;
75
+ }
76
+
77
+
78
+
79
+ // ─── Branch types ────────────────────────────────────────────────────────────
80
+
81
+ /**
82
+ * A programmatic transition directive. Encodes where to go and what state
83
+ * writes to apply. Used as the `then` target of a `BranchEntry` when the
84
+ * destination is more complex than a simple step/flow id string.
85
+ *
86
+ * At most one position field (`goTo`, `goToStep`, `complete`, `abort`, `reset`)
87
+ * may be set per Directive. Non-position fields (`reply`, `contextUpdate`,
88
+ * `dataUpdate`) may accompany any position field.
89
+ */
90
+ export interface Directive<TContext = unknown, TData = unknown> {
91
+ /** Jump to a flow (string) or a flow with options (object). */
92
+ goTo?: string | { flow?: string; step?: string; data?: Partial<TData>; reason?: string; carry?: 'preserve' | 'reset'; };
93
+ /** Jump to a specific step, optionally in another flow. */
94
+ goToStep?: string | { step: string; flow?: string; data?: Partial<TData>; reason?: string; };
95
+ /** Mark the current flow as complete. */
96
+ complete?: true | { next?: Directive<TContext, TData>; reason?: string; };
97
+ /** Abort the current flow. */
98
+ abort?: string | { reason: string; clearSession?: boolean; };
99
+ /** Reset the current flow (or jump to a step within it). */
100
+ reset?: true | { step?: string; clearData?: boolean; reason?: string; };
101
+ /** Verbatim reply text to send to the user. */
102
+ reply?: string;
103
+ /** Partial context update applied before the next turn. */
104
+ contextUpdate?: Partial<TContext>;
105
+ /** Partial data update applied before the next turn. */
106
+ dataUpdate?: Partial<TData>;
107
+ }
108
+
109
+ /**
110
+ * PreDirective — what `prepare` hooks and `onEnter` hooks return.
111
+ * Adds prompt/tool shaping that only makes sense BEFORE the LLM call this turn.
112
+ * Extends Directive — anything a Directive can do, a PreDirective can do too.
113
+ *
114
+ * NOT persistable: contains live Tool references and a halt flag that
115
+ * has no meaning across turns. This type is transient (one-turn lifetime).
116
+ */
117
+ export interface PreDirective<TContext = unknown, TData = unknown>
118
+ extends Directive<TContext, TData> {
119
+ /**
120
+ * Sentences to append to the system prompt for THIS turn only.
121
+ * Wired through PromptComposer's per-turn appendage slot.
122
+ */
123
+ appendPrompt?: string[];
124
+
125
+ /**
126
+ * Tools available for THIS turn only. Stacked on top of agent/flow/step
127
+ * tool scopes via ToolManager's transient layer.
128
+ */
129
+ injectTools?: Array<Tool<TContext, TData>>;
130
+
131
+ /**
132
+ * If true, skip the LLM call entirely this turn.
133
+ *
134
+ * Co-validates with `reply`: when both are set, `reply` becomes the
135
+ * assistant output (`stoppedReason: 'reply'`). When `halt` is true
136
+ * without `reply`, the turn produces an empty assistant message
137
+ * (`stoppedReason: 'halt'`).
138
+ */
139
+ halt?: boolean;
140
+ }
141
+
142
+ /**
143
+ * Context passed to a `BranchPredicate` function.
144
+ *
145
+ * **Typing note:** `data` is `Partial<TData>` — predicates must null-check
146
+ * any field not declared in the source step's `requires`. Fields covered by
147
+ * `requires` are guaranteed present by the engine when the predicate runs;
148
+ * everything else is `T | undefined`.
149
+ */
150
+ export interface BranchPredicateContext<TContext = unknown, TData = unknown> {
151
+ /** Collected data (partial — null-check fields not in `requires`). */
152
+ data: Partial<TData>;
153
+ /** Agent-level context. */
154
+ context: TContext;
155
+ /** Full session state. */
156
+ session: SessionState<TData>;
157
+ /** Conversation history as events. */
158
+ history: Event[];
159
+ }
160
+
161
+ /**
162
+ * A code predicate for branch evaluation. Returns `true` to pass, `false`
163
+ * to skip the entry. May be async.
164
+ *
165
+ * Predicates are evaluated **before** any AI condition (`when`) on the same
166
+ * entry — code-first evaluation saves tokens when the predicate fails.
167
+ */
168
+ export type BranchPredicate<TContext = unknown, TData = unknown> = (
169
+ ctx: BranchPredicateContext<TContext, TData>,
170
+ ) => boolean | Promise<boolean>;
171
+
172
+ /**
173
+ * A single entry in a `BranchMap`. Evaluated in declaration order; the first
174
+ * entry whose conditions pass wins.
175
+ *
176
+ * **Resolution rules for `then`:**
177
+ * 1. String matching a step id in the current flow → enter that step.
178
+ * 2. String matching a flow id/title in the agent → treated as
179
+ * `applyDirective({ goTo: <string> })`.
180
+ * 3. `Directive` object → applied via `applyDirective` directly.
181
+ *
182
+ * An entry with neither `when` nor `if` is an unconditional fallback and
183
+ * is only legal as the **last** entry in the array.
184
+ */
185
+ export interface BranchEntry<TContext = unknown, TData = unknown> {
186
+ /**
187
+ * AI-evaluated condition. String or array of strings (AND semantics).
188
+ * Costs LLM tokens. Reuses the same machinery as `step.when`.
189
+ * Only evaluated if `if` passes (or is absent) — code-first short-circuit.
190
+ */
191
+ when?: string | string[];
192
+
193
+ /**
194
+ * Code predicate. Function or array of functions (AND semantics).
195
+ * Free to evaluate. When both `when` and `if` are set, `if` runs first;
196
+ * `when` is only evaluated if all `if` predicates pass (token-saving).
197
+ */
198
+ if?: BranchPredicate<TContext, TData> | BranchPredicate<TContext, TData>[];
199
+
200
+ /**
201
+ * Where to go when this entry matches.
202
+ * - **String:** step id in the current flow, or flow id/title for cross-flow.
203
+ * - **Directive:** full programmatic transition (cross-flow with data,
204
+ * complete, abort, reset, etc.).
205
+ */
206
+ then: string | Directive<TContext, TData>;
207
+
208
+ /** Optional label for event traces and flow visualization. */
209
+ label?: string;
210
+ }
211
+
212
+ /**
213
+ * Array of branch entries evaluated in declaration order.
214
+ * First entry whose conditions pass wins. This is the explicit, source-local
215
+ * fork primitive — all possible paths from a step are visible in one list.
216
+ */
217
+ export type BranchMap<TContext = unknown, TData = unknown> = Array<BranchEntry<TContext, TData>>;
218
+
219
+ // ─── End branch types ────────────────────────────────────────────────────────
220
+
221
+ /**
222
+ * Reference to a flow
223
+ */
224
+ export interface FlowRef {
225
+ /** Flow identifier */
226
+ id: string;
227
+ }
228
+
229
+ /**
230
+ * Reference to a step within a flow
231
+ */
232
+ export interface StepRef {
233
+ /** Step identifier */
234
+ id: string;
235
+ /** Flow this step belongs to */
236
+ flowId: string;
237
+ }
238
+
239
+
240
+
241
+ /**
242
+ * Flow lifecycle hooks for managing flow-specific data and behavior.
243
+ *
244
+ * Pre-LLM hooks (`onEnter`) return `void | PreDirective`.
245
+ * Post-LLM hooks (`onComplete`) return `void | Directive`.
246
+ * Informational hooks (`onExit`) return `void`.
247
+ * Data hooks (`onDataUpdate`, `onContextUpdate`) retain their v1 signatures.
248
+ */
249
+ export interface FlowLifecycleHooks<TContext = unknown, TData = unknown> {
250
+ /**
251
+ * Called when the flow is first entered.
252
+ * May return a PreDirective to augment the prompt, inject tools, or halt.
253
+ */
254
+ onEnter?: (
255
+ ctx: HookContext<TContext, TData>
256
+ ) => void | PreDirective<TContext, TData> | Promise<void | PreDirective<TContext, TData>>;
257
+
258
+ /**
259
+ * Called when the flow is exited. Informational only — cannot influence flow control.
260
+ * Receives the reason the flow was exited.
261
+ */
262
+ onExit?: (
263
+ ctx: HookContext<TContext, TData>,
264
+ reason: ExitReason
265
+ ) => void | Promise<void>;
266
+
267
+ /**
268
+ * Called when the flow's required fields are satisfied or when a `complete` directive fires.
269
+ * May return a Directive to chain into another flow or perform state writes.
270
+ */
271
+ onComplete?: (
272
+ ctx: HookContext<TContext, TData>
273
+ ) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
274
+
275
+ /**
276
+ * Called after collected data is updated for this flow (from AI response or tool execution)
277
+ * Useful for validation, enrichment, or persistence of flow-specific collected data
278
+ * Return modified collected data or the same data to keep it unchanged
279
+ *
280
+ * Unlike Agent-level onDataUpdate, this only triggers for data changes in this specific flow.
281
+ */
282
+ onDataUpdate?: (
283
+ data: Partial<TData>,
284
+ previousCollected: Partial<TData>
285
+ ) => Partial<TData> | Promise<Partial<TData>>;
286
+
287
+ /**
288
+ * Called after context is updated via updateContext() when this flow is active
289
+ * Useful for flow-specific context reactions, validation, or side effects
290
+ *
291
+ * Unlike Agent-level onContextUpdate, this only triggers when this specific flow is active.
292
+ */
293
+ onContextUpdate?: (
294
+ newContext: TContext,
295
+ previousContext: TContext
296
+ ) => void | Promise<void>;
297
+ }
298
+
299
+
300
+
301
+ /**
302
+ * Options for creating a flow
303
+ * @template TData - Type of data collected throughout the flow (inferred from schema)
304
+ */
305
+ export interface FlowOptions<TContext = unknown, TData = unknown> {
306
+ /** Custom ID for the flow (optional - will generate deterministic ID from title if not provided) */
307
+ id?: string;
308
+ /** Title of the flow */
309
+ title: string;
310
+ /** Description of what this flow accomplishes */
311
+ description?: string;
312
+
313
+ /**
314
+ * AI-evaluated activation condition(s). String or array of strings (AND semantics).
315
+ * Costs LLM tokens. Functions are NOT allowed here — use `if` for code predicates.
316
+ */
317
+ when?: ConditionWhen;
318
+ /**
319
+ * Code-evaluated activation condition(s). Function or array of functions (AND semantics).
320
+ * Free to evaluate. When both `when` and `if` are set, `if` runs first;
321
+ * `when` is only evaluated when `if` passes.
322
+ */
323
+ if?: ConditionIf<TContext, TData>;
324
+ /**
325
+ * Instructions for this flow.
326
+ */
327
+ instructions?: Instruction<TContext, TData>[];
328
+ /** Tools available in this flow */
329
+ tools?: (string | Tool<TContext, TData>)[];
330
+
331
+ /** Optional: extractions the router may return (added to routing schema) */
332
+ routingExtrasSchema?: StructuredSchema;
333
+ /** Optional: structured response data for this flow's message generation */
334
+ responseOutputSchema?: StructuredSchema;
335
+ /**
336
+ * Required fields for flow completion - must be valid keys from agent's TData type
337
+ * Flow is considered complete when all required fields are present in agent data
338
+ */
339
+ requiredFields?: (keyof TData)[];
340
+ /**
341
+ * Optional fields that enhance the flow but aren't required for completion
342
+ * Must be valid keys from agent's TData type
343
+ */
344
+ optionalFields?: (keyof TData)[];
345
+ /**
346
+ * Initial data to pre-populate when entering this flow
347
+ * Useful for restoring sessions or pre-filling known information
348
+ * Steps with skip conditions will be automatically bypassed if data is present
349
+ * Now refers to agent-level data
350
+ */
351
+ initialData?: Partial<TData>;
352
+ /**
353
+ * Sequential steps for simple linear flows
354
+ * If provided, automatically chains the steps from initialStep
355
+ * The last step in the array is the implicit terminus of the flow
356
+ * For complex flows with branching, build the step machine manually instead
357
+ */
358
+ steps?: StepOptions<TContext, TData>[];
359
+
360
+ /**
361
+ * Optional transition when the flow completes (last step finishes).
362
+ *
363
+ * Accepts a flow ID or title string — sugar for
364
+ * `hooks.onComplete = () => ({ goTo: '<id>' })`.
365
+ *
366
+ * For dynamic completion logic, use `hooks.onComplete` instead.
367
+ * Setting both `onComplete` and `hooks.onComplete` on the same flow
368
+ * throws `FlowConfigurationError` at construction time.
369
+ *
370
+ * @example
371
+ * // Simple string — transitions to "Collect Feedback" on completion
372
+ * onComplete: "Collect Feedback"
373
+ */
374
+ onComplete?: string;
375
+ /**
376
+ * If true, this flow can be re-selected by the router after it has
377
+ * completed in the current session — useful for "do another?" patterns
378
+ * (re-book, re-search, repeat-task).
379
+ *
380
+ * Default: `false`. Once a flow completes (and `onComplete` is not set
381
+ * or returns `undefined`), the flow is excluded from routing candidates
382
+ * for the rest of the session unless `reentrant: true`.
383
+ *
384
+ * On re-entry, the engine clears every field declared in this flow's
385
+ * `requiredFields` and `optionalFields` (so the flow starts fresh from
386
+ * its initial step). Fields not declared as owned by this flow are
387
+ * preserved in `session.data`.
388
+ *
389
+ * `onComplete` always wins over `reentrant`. If `onComplete` returns a
390
+ * target flow, the session transitions there immediately on completion;
391
+ * `reentrant` is consulted only when `onComplete` is absent or returns
392
+ * `undefined`.
393
+ */
394
+ reentrant?: boolean;
395
+ /**
396
+ * Flow lifecycle hooks
397
+ */
398
+ hooks?: FlowLifecycleHooks<TContext, TData>;
399
+ }
400
+
401
+ /**
402
+ * Step lifecycle hooks for managing step-specific behavior.
403
+ *
404
+ * Pre-LLM hooks (`onEnter`, `prepare`) return `void | PreDirective`.
405
+ * Post-LLM hooks (`finalize`) return `void | Directive`.
406
+ * Informational hooks (`onExit`) return `void`.
407
+ */
408
+ export interface StepLifecycleHooks<TContext = unknown, TData = unknown> {
409
+ /**
410
+ * Called on step entry. May return a PreDirective to augment the prompt,
411
+ * inject tools, or halt.
412
+ */
413
+ onEnter?: (
414
+ ctx: HookContext<TContext, TData>
415
+ ) => void | PreDirective<TContext, TData> | Promise<void | PreDirective<TContext, TData>>;
416
+
417
+ /**
418
+ * Called when the step is exited. Informational only — cannot influence flow control.
419
+ * Receives the reason the step was exited.
420
+ */
421
+ onExit?: (
422
+ ctx: HookContext<TContext, TData>,
423
+ reason: ExitReason
424
+ ) => void | Promise<void>;
425
+
426
+ /**
427
+ * Called pre-LLM. May return a PreDirective to augment the prompt,
428
+ * inject tools, or halt the LLM call.
429
+ */
430
+ prepare?: (
431
+ ctx: HookContext<TContext, TData>
432
+ ) => void | PreDirective<TContext, TData> | Promise<void | PreDirective<TContext, TData>>;
433
+
434
+ /**
435
+ * Called post-LLM. May return a Directive to redirect flow, write state,
436
+ * or emit a verbatim reply.
437
+ */
438
+ finalize?: (
439
+ ctx: HookContext<TContext, TData>
440
+ ) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
441
+ }
442
+
443
+ /**
444
+ * Specification for a step transition
445
+ */
446
+ export interface StepOptions<TContext = unknown, TData = unknown> {
447
+ /** Custom ID for this step (optional - will generate deterministic ID if not provided) */
448
+ id?: string;
449
+ /** Description of the transition */
450
+ description?: string;
451
+ /** Transition to a chat state with this description */
452
+ prompt?: Template<TContext, TData>;
453
+ /** Tools available for AI to call in this step (by ID reference or inline definition) */
454
+ tools?: (string | Tool<TContext, TData>)[];
455
+ /** Programmatic function or tool to run before AI responds */
456
+ prepare?:
457
+ | string
458
+ | Tool<TContext, TData>
459
+ | ((context: TContext, data?: Partial<TData>) => void | PrepareResult | Promise<void | PrepareResult>);
460
+ /** Programmatic function or tool to run after AI responds */
461
+ finalize?:
462
+ | string
463
+ | Tool<TContext, TData>
464
+ | ((context: TContext, data?: Partial<TData>) => void | PrepareResult | Promise<void | PrepareResult>);
465
+
466
+ /**
467
+ * Fields to collect from the conversation in this step
468
+ * These should match keys in the agent's TData schema
469
+ */
470
+ collect?: (keyof TData)[];
471
+ /**
472
+ * Code-evaluated skip condition. If evaluates to true, the step will be bypassed.
473
+ * Function or array of functions (OR semantics) — if any returns true, step is skipped.
474
+ * Only code predicates are allowed here (no AI strings).
475
+ *
476
+ * Renamed from v1 `skipIf` to clarify the if-only shape.
477
+ */
478
+ skip?: ConditionIf<TContext, TData>;
479
+ /**
480
+ * Required data fields that must be present before entering this step
481
+ * If any required field is missing, step cannot be entered
482
+ * Must be valid keys from agent's TData type
483
+ */
484
+ requires?: (keyof TData)[];
485
+ /**
486
+ * AI-evaluated activation condition(s). String or array of strings (AND semantics).
487
+ * Costs LLM tokens. Functions are NOT allowed here — use `if` for code predicates.
488
+ */
489
+ when?: ConditionWhen;
490
+ /**
491
+ * Code-evaluated activation condition(s). Function or array of functions (AND semantics).
492
+ * Free to evaluate. When both `when` and `if` are set, `if` runs first;
493
+ * `when` is only evaluated when `if` passes.
494
+ */
495
+ if?: ConditionIf<TContext, TData>;
496
+ /**
497
+ * Instructions for this step. Replaces v1's `guidelines`.
498
+ */
499
+ instructions?: Instruction<TContext, TData>[];
500
+
501
+ /**
502
+ * If true, this step runs without an LLM call. Pre-LLM hooks
503
+ * (`onEnter`, `prepare`) and branch resolution still execute.
504
+ *
505
+ * An auto-step **cannot** define `prompt`, `collect`, `tools`, or `finalize`.
506
+ * Defining any of these will throw `FlowConfigurationError` at construction time.
507
+ *
508
+ * Use auto-steps for:
509
+ * - Data enrichment between user-facing steps (CRM lookup, pricing calc).
510
+ * - Deterministic decision points (pair with `branches`).
511
+ * - Programmatic short-circuits (`prepare` returning `{ halt: true, reply }`).
512
+ *
513
+ * @default false
514
+ */
515
+ auto?: boolean;
516
+
517
+ /**
518
+ * Verbatim assistant output for this step. When set, the template is
519
+ * rendered via the same engine as `prompt` and emitted as the assistant
520
+ * message without invoking the LLM.
521
+ *
522
+ * A reply step **cannot** define `prompt`, `collect`, `tools`, `finalize`,
523
+ * or `auto: true`. Defining any of these will throw `FlowConfigurationError`
524
+ * at construction time.
525
+ *
526
+ * `onEnter` and `prepare` hooks fire normally before the reply is rendered.
527
+ * If `prepare` returns a `PreDirective` with its own `reply` field, the
528
+ * hook-emitted reply wins (last-emission-wins per Algorithm 4).
529
+ *
530
+ * After emission, `onExit` fires and `branches` are resolved for next step.
531
+ * `stoppedReason: 'reply'`.
532
+ *
533
+ * Use reply steps for: confirmations, hand-offs, farewells, acks — anything
534
+ * that doesn't need LLM reasoning.
535
+ */
536
+ reply?: Template<TContext, TData>;
537
+
538
+ /**
539
+ * Explicit source-local fork. An array of branch entries evaluated in
540
+ * declaration order — the first entry whose conditions pass wins; its
541
+ * `then` resolves to the next step or a Directive.
542
+ *
543
+ * Coexists with `nextStep`. If `branches` is absent or no entry matches,
544
+ * resolution falls through to linear nextStep / AI step selection.
545
+ *
546
+ * Runs **after** the step's post-LLM phase (tool execution, `finalize`)
547
+ * and **before** linear successor selection.
548
+ */
549
+ branches?: BranchMap<TContext, TData>;
550
+
551
+ /**
552
+ * Step lifecycle hooks for managing step-specific behavior.
553
+ * Provides onEnter, onExit, prepare, and finalize hooks that receive HookContext.
554
+ */
555
+ hooks?: StepLifecycleHooks<TContext, TData>;
556
+ }
557
+
558
+ /**
559
+ * Specification for a branch in the conversation flow
560
+ */
561
+ export interface BranchSpec<TContext = unknown, TData = unknown> {
562
+ /** User-friendly identifier for this branch (used as object key) */
563
+ name: string;
564
+ /** Optional ID for this branch (auto-generated if not provided) */
565
+ id?: string;
566
+ /** Step configuration for this branch */
567
+ step: StepOptions<TContext, TData>;
568
+ }
569
+
570
+ /**
571
+ * Result of a branch operation
572
+ * Maps branch names to their respective step results for continued chaining
573
+ */
574
+ export interface BranchResult<TContext = unknown, TData = unknown> {
575
+ [branchName: string]: StepResult<TContext, TData>;
576
+ }
577
+
578
+ /**
579
+ * Result of a transition operation
580
+ * Combines step reference with the ability to chain transitions and create branches
581
+ */
582
+ export interface StepResult<TContext = unknown, TData = unknown>
583
+ extends StepRef {
584
+ /** Allow chaining transitions */
585
+ nextStep: (spec: StepOptions<TContext, TData>) => StepResult<TContext, TData>;
586
+ /** Create multiple branches from this step */
587
+ branch: (
588
+ branches: BranchSpec<TContext, TData>[]
589
+ ) => BranchResult<TContext, TData>;
590
+ }
@@ -7,14 +7,17 @@ export type {
7
7
  AgentOptions,
8
8
  AgentCompactionConfig,
9
9
  Term,
10
- Guideline,
11
- GuidelineMatch,
10
+ Instruction,
11
+ ScopedInstructions,
12
+ AppliedInstruction,
12
13
  AgentResponseStreamChunk,
13
14
  AgentResponse,
14
15
  ContextLifecycleHooks,
15
16
  ContextProvider,
16
17
  ValidationError,
17
18
  ValidationResult,
19
+ HookContext,
20
+ ExitReason,
18
21
  } from "./agent";
19
22
  export { CompositionMode } from "./agent";
20
23
 
@@ -40,23 +43,42 @@ export type {
40
43
  export { EventKind, MessageRole } from "./history";
41
44
  export * from "./history";
42
45
 
43
- // Route types
46
+ // Flow types
44
47
  export type {
45
- RouteOptions,
46
- RouteRef,
48
+ FlowOptions,
49
+ FlowRef,
47
50
  StepRef,
48
51
  StepOptions,
49
- BranchSpec,
50
- StepResult,
51
- BranchResult,
52
- RouteCompletionHandler,
53
- RouteTransitionConfig,
54
- RouteLifecycleHooks,
55
- } from "./route";
56
- export * from "./route";
52
+ FlowLifecycleHooks,
53
+ StepLifecycleHooks,
54
+ Directive,
55
+ PreDirective,
56
+ BranchEntry,
57
+ BranchMap,
58
+ BranchPredicate,
59
+ BranchPredicateContext,
60
+ ConditionPredicate,
61
+ ConditionIf,
62
+ ConditionWhen,
63
+ StoppedReason,
64
+ PrepareResult,
65
+ } from "./flow";
57
66
 
58
67
  // Session types
59
- export type { SessionState, PendingTransition } from "./session";
68
+ export type { SessionState } from "./session";
69
+
70
+ // Signals types (canonical source)
71
+ export type {
72
+ Signal,
73
+ SignalContext,
74
+ SignalDirective,
75
+ SignalPredicate,
76
+ SignalPredicateContext,
77
+ SignalFiring,
78
+ SignalSchema,
79
+ SignalsState,
80
+ SignalTriggerState,
81
+ } from "./signals";
60
82
 
61
83
  // Tool types
62
84
  export type {
@@ -65,7 +87,6 @@ export type {
65
87
  ToolResult,
66
88
  ToolHandler,
67
89
  ToolExecutionResult,
68
- EnhancedTool,
69
90
  ToolValidationResult,
70
91
  ToolPermissionResult,
71
92
  ToolCallRequest,
@@ -129,9 +150,15 @@ export * from "./persistence";
129
150
  export type {
130
151
  Template,
131
152
  TemplateContext,
132
- ConditionTemplate,
133
153
  ConditionEvaluationResult
134
154
  } from "./template";
155
+
156
+ // Error types
157
+ export { NotImplementedError } from "./errors";
158
+
159
+ // Internal — ConditionTemplate is NOT exported from the public surface in v2.
160
+ // It remains internally for the condition evaluator utility.
161
+
135
162
  export {
136
163
  ConditionEvaluator,
137
164
  createConditionEvaluator,