@warlock.js/ai 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (490) hide show
  1. package/README.md +72 -0
  2. package/cjs/index.cjs +8625 -0
  3. package/cjs/index.cjs.map +1 -0
  4. package/esm/agent/agent-config.type.d.mts +160 -0
  5. package/esm/agent/agent-config.type.d.mts.map +1 -0
  6. package/esm/agent/agent-input-builder.mjs +111 -0
  7. package/esm/agent/agent-input-builder.mjs.map +1 -0
  8. package/esm/agent/agent-log-event.mjs +101 -0
  9. package/esm/agent/agent-log-event.mjs.map +1 -0
  10. package/esm/agent/agent-stream.d.mts +47 -0
  11. package/esm/agent/agent-stream.d.mts.map +1 -0
  12. package/esm/agent/agent-stream.mjs +82 -0
  13. package/esm/agent/agent-stream.mjs.map +1 -0
  14. package/esm/agent/agent-to-stream-event.mjs +86 -0
  15. package/esm/agent/agent-to-stream-event.mjs.map +1 -0
  16. package/esm/agent/agent.d.mts +38 -0
  17. package/esm/agent/agent.d.mts.map +1 -0
  18. package/esm/agent/agent.mjs +974 -0
  19. package/esm/agent/agent.mjs.map +1 -0
  20. package/esm/agent/index.d.mts +3 -0
  21. package/esm/agent/index.mjs +4 -0
  22. package/esm/agent/json-stream-guard.mjs +400 -0
  23. package/esm/agent/json-stream-guard.mjs.map +1 -0
  24. package/esm/ai.d.mts +64 -0
  25. package/esm/ai.d.mts.map +1 -0
  26. package/esm/ai.mjs +65 -0
  27. package/esm/ai.mjs.map +1 -0
  28. package/esm/config.d.mts +79 -0
  29. package/esm/config.d.mts.map +1 -0
  30. package/esm/config.mjs +43 -0
  31. package/esm/config.mjs.map +1 -0
  32. package/esm/contracts/agent/agent-options.type.d.mts +132 -0
  33. package/esm/contracts/agent/agent-options.type.d.mts.map +1 -0
  34. package/esm/contracts/agent/agent.contract.d.mts +92 -0
  35. package/esm/contracts/agent/agent.contract.d.mts.map +1 -0
  36. package/esm/contracts/agent/index.d.mts +2 -0
  37. package/esm/contracts/attachment.type.d.mts +100 -0
  38. package/esm/contracts/attachment.type.d.mts.map +1 -0
  39. package/esm/contracts/content-part.type.d.mts +55 -0
  40. package/esm/contracts/content-part.type.d.mts.map +1 -0
  41. package/esm/contracts/conversation-message.type.d.mts +51 -0
  42. package/esm/contracts/conversation-message.type.d.mts.map +1 -0
  43. package/esm/contracts/embedder.contract.d.mts +69 -0
  44. package/esm/contracts/embedder.contract.d.mts.map +1 -0
  45. package/esm/contracts/end.type.d.mts +53 -0
  46. package/esm/contracts/end.type.d.mts.map +1 -0
  47. package/esm/contracts/end.type.mjs +47 -0
  48. package/esm/contracts/end.type.mjs.map +1 -0
  49. package/esm/contracts/events/agent-events.type.d.mts +71 -0
  50. package/esm/contracts/events/agent-events.type.d.mts.map +1 -0
  51. package/esm/contracts/events/complete-event.type.d.mts +33 -0
  52. package/esm/contracts/events/complete-event.type.d.mts.map +1 -0
  53. package/esm/contracts/events/event-identity.type.d.mts +30 -0
  54. package/esm/contracts/events/event-identity.type.d.mts.map +1 -0
  55. package/esm/contracts/events/event-map.type.d.mts +80 -0
  56. package/esm/contracts/events/event-map.type.d.mts.map +1 -0
  57. package/esm/contracts/events/index.d.mts +7 -0
  58. package/esm/contracts/events/supervisor-events.type.d.mts +162 -0
  59. package/esm/contracts/events/supervisor-events.type.d.mts.map +1 -0
  60. package/esm/contracts/events/usage-event.type.d.mts +39 -0
  61. package/esm/contracts/events/usage-event.type.d.mts.map +1 -0
  62. package/esm/contracts/events/workflow-events.type.d.mts +51 -0
  63. package/esm/contracts/events/workflow-events.type.d.mts.map +1 -0
  64. package/esm/contracts/executable.contract.d.mts +25 -0
  65. package/esm/contracts/executable.contract.d.mts.map +1 -0
  66. package/esm/contracts/finish-reason.type.d.mts +17 -0
  67. package/esm/contracts/finish-reason.type.d.mts.map +1 -0
  68. package/esm/contracts/index.d.mts +63 -0
  69. package/esm/contracts/middleware/index.d.mts +3 -0
  70. package/esm/contracts/middleware/middleware-context.type.d.mts +99 -0
  71. package/esm/contracts/middleware/middleware-context.type.d.mts.map +1 -0
  72. package/esm/contracts/middleware/middleware-state.type.d.mts +34 -0
  73. package/esm/contracts/middleware/middleware-state.type.d.mts.map +1 -0
  74. package/esm/contracts/middleware/middleware.contract.d.mts +140 -0
  75. package/esm/contracts/middleware/middleware.contract.d.mts.map +1 -0
  76. package/esm/contracts/model-tool-call-request.type.d.mts +58 -0
  77. package/esm/contracts/model-tool-call-request.type.d.mts.map +1 -0
  78. package/esm/contracts/model.contract.d.mts +164 -0
  79. package/esm/contracts/model.contract.d.mts.map +1 -0
  80. package/esm/contracts/placeholders.type.d.mts +23 -0
  81. package/esm/contracts/placeholders.type.d.mts.map +1 -0
  82. package/esm/contracts/result/agent-result.type.d.mts +33 -0
  83. package/esm/contracts/result/agent-result.type.d.mts.map +1 -0
  84. package/esm/contracts/result/attempt-entry.type.d.mts +40 -0
  85. package/esm/contracts/result/attempt-entry.type.d.mts.map +1 -0
  86. package/esm/contracts/result/base-report.type.d.mts +137 -0
  87. package/esm/contracts/result/base-report.type.d.mts.map +1 -0
  88. package/esm/contracts/result/base-report.type.mjs +17 -0
  89. package/esm/contracts/result/base-report.type.mjs.map +1 -0
  90. package/esm/contracts/result/base-result.type.d.mts +26 -0
  91. package/esm/contracts/result/base-result.type.d.mts.map +1 -0
  92. package/esm/contracts/result/execute-result.type.d.mts +40 -0
  93. package/esm/contracts/result/execute-result.type.d.mts.map +1 -0
  94. package/esm/contracts/result/execution-report.type.d.mts +50 -0
  95. package/esm/contracts/result/execution-report.type.d.mts.map +1 -0
  96. package/esm/contracts/result/index.d.mts +14 -0
  97. package/esm/contracts/result/llm-trip.type.d.mts +36 -0
  98. package/esm/contracts/result/llm-trip.type.d.mts.map +1 -0
  99. package/esm/contracts/result/model-pricing.type.d.mts +51 -0
  100. package/esm/contracts/result/model-pricing.type.d.mts.map +1 -0
  101. package/esm/contracts/result/session-send-result.type.d.mts +29 -0
  102. package/esm/contracts/result/session-send-result.type.d.mts.map +1 -0
  103. package/esm/contracts/result/step-result.type.d.mts +43 -0
  104. package/esm/contracts/result/step-result.type.d.mts.map +1 -0
  105. package/esm/contracts/result/supervisor-result.type.d.mts +86 -0
  106. package/esm/contracts/result/supervisor-result.type.d.mts.map +1 -0
  107. package/esm/contracts/result/tool-call.type.d.mts +44 -0
  108. package/esm/contracts/result/tool-call.type.d.mts.map +1 -0
  109. package/esm/contracts/result/usage.type.d.mts +57 -0
  110. package/esm/contracts/result/usage.type.d.mts.map +1 -0
  111. package/esm/contracts/result/workflow-result.type.d.mts +45 -0
  112. package/esm/contracts/result/workflow-result.type.d.mts.map +1 -0
  113. package/esm/contracts/sdk-adapter.contract.d.mts +77 -0
  114. package/esm/contracts/sdk-adapter.contract.d.mts.map +1 -0
  115. package/esm/contracts/stream/stream-event.type.d.mts +68 -0
  116. package/esm/contracts/stream/stream-event.type.d.mts.map +1 -0
  117. package/esm/contracts/stream/stream.contract.d.mts +61 -0
  118. package/esm/contracts/stream/stream.contract.d.mts.map +1 -0
  119. package/esm/contracts/streaming-tool-guard-config.type.d.mts +78 -0
  120. package/esm/contracts/streaming-tool-guard-config.type.d.mts.map +1 -0
  121. package/esm/contracts/supervisor/ack-entry.type.d.mts +97 -0
  122. package/esm/contracts/supervisor/ack-entry.type.d.mts.map +1 -0
  123. package/esm/contracts/supervisor/classifier-context.type.d.mts +189 -0
  124. package/esm/contracts/supervisor/classifier-context.type.d.mts.map +1 -0
  125. package/esm/contracts/supervisor/dispatch-context.type.d.mts +235 -0
  126. package/esm/contracts/supervisor/dispatch-context.type.d.mts.map +1 -0
  127. package/esm/contracts/supervisor/evaluate-context.type.d.mts +124 -0
  128. package/esm/contracts/supervisor/evaluate-context.type.d.mts.map +1 -0
  129. package/esm/contracts/supervisor/index.d.mts +16 -0
  130. package/esm/contracts/supervisor/intent-entry.type.d.mts +254 -0
  131. package/esm/contracts/supervisor/intent-entry.type.d.mts.map +1 -0
  132. package/esm/contracts/supervisor/iteration-snapshot.type.d.mts +117 -0
  133. package/esm/contracts/supervisor/iteration-snapshot.type.d.mts.map +1 -0
  134. package/esm/contracts/supervisor/next.type.d.mts +25 -0
  135. package/esm/contracts/supervisor/next.type.d.mts.map +1 -0
  136. package/esm/contracts/supervisor/route-context.type.d.mts +91 -0
  137. package/esm/contracts/supervisor/route-context.type.d.mts.map +1 -0
  138. package/esm/contracts/supervisor/router-entry.type.d.mts +67 -0
  139. package/esm/contracts/supervisor/router-entry.type.d.mts.map +1 -0
  140. package/esm/contracts/supervisor/supervisor-config.type.d.mts +356 -0
  141. package/esm/contracts/supervisor/supervisor-config.type.d.mts.map +1 -0
  142. package/esm/contracts/supervisor/supervisor-execute-options.type.d.mts +90 -0
  143. package/esm/contracts/supervisor/supervisor-execute-options.type.d.mts.map +1 -0
  144. package/esm/contracts/supervisor/supervisor-input.type.d.mts +24 -0
  145. package/esm/contracts/supervisor/supervisor-input.type.d.mts.map +1 -0
  146. package/esm/contracts/supervisor/supervisor-snapshot.type.d.mts +49 -0
  147. package/esm/contracts/supervisor/supervisor-snapshot.type.d.mts.map +1 -0
  148. package/esm/contracts/supervisor/supervisor-stream-event.type.d.mts +16 -0
  149. package/esm/contracts/supervisor/supervisor-stream-event.type.d.mts.map +1 -0
  150. package/esm/contracts/supervisor/supervisor.contract.d.mts +126 -0
  151. package/esm/contracts/supervisor/supervisor.contract.d.mts.map +1 -0
  152. package/esm/contracts/system-prompt.contract.d.mts +118 -0
  153. package/esm/contracts/system-prompt.contract.d.mts.map +1 -0
  154. package/esm/contracts/tool.contract.d.mts +161 -0
  155. package/esm/contracts/tool.contract.d.mts.map +1 -0
  156. package/esm/contracts/workflow/index.d.mts +6 -0
  157. package/esm/contracts/workflow/next-step-result.type.d.mts +17 -0
  158. package/esm/contracts/workflow/next-step-result.type.d.mts.map +1 -0
  159. package/esm/contracts/workflow/retry-config.type.d.mts +19 -0
  160. package/esm/contracts/workflow/retry-config.type.d.mts.map +1 -0
  161. package/esm/contracts/workflow/step.contract.d.mts +189 -0
  162. package/esm/contracts/workflow/step.contract.d.mts.map +1 -0
  163. package/esm/contracts/workflow/workflow-context.type.d.mts +45 -0
  164. package/esm/contracts/workflow/workflow-context.type.d.mts.map +1 -0
  165. package/esm/contracts/workflow/workflow-snapshot.type.d.mts +24 -0
  166. package/esm/contracts/workflow/workflow-snapshot.type.d.mts.map +1 -0
  167. package/esm/contracts/workflow/workflow.contract.d.mts +142 -0
  168. package/esm/contracts/workflow/workflow.contract.d.mts.map +1 -0
  169. package/esm/errors/agent-cancelled-error.d.mts +49 -0
  170. package/esm/errors/agent-cancelled-error.d.mts.map +1 -0
  171. package/esm/errors/agent-cancelled-error.mjs +43 -0
  172. package/esm/errors/agent-cancelled-error.mjs.map +1 -0
  173. package/esm/errors/agent-execution-error.d.mts +32 -0
  174. package/esm/errors/agent-execution-error.d.mts.map +1 -0
  175. package/esm/errors/agent-execution-error.mjs +35 -0
  176. package/esm/errors/agent-execution-error.mjs.map +1 -0
  177. package/esm/errors/agent-max-trips-error.d.mts +45 -0
  178. package/esm/errors/agent-max-trips-error.d.mts.map +1 -0
  179. package/esm/errors/agent-max-trips-error.mjs +41 -0
  180. package/esm/errors/agent-max-trips-error.mjs.map +1 -0
  181. package/esm/errors/ai-error.d.mts +94 -0
  182. package/esm/errors/ai-error.d.mts.map +1 -0
  183. package/esm/errors/ai-error.mjs +68 -0
  184. package/esm/errors/ai-error.mjs.map +1 -0
  185. package/esm/errors/budget-exceeded-error.d.mts +41 -0
  186. package/esm/errors/budget-exceeded-error.d.mts.map +1 -0
  187. package/esm/errors/budget-exceeded-error.mjs +31 -0
  188. package/esm/errors/budget-exceeded-error.mjs.map +1 -0
  189. package/esm/errors/content-filter-error.d.mts +33 -0
  190. package/esm/errors/content-filter-error.d.mts.map +1 -0
  191. package/esm/errors/content-filter-error.mjs +28 -0
  192. package/esm/errors/content-filter-error.mjs.map +1 -0
  193. package/esm/errors/context-length-exceeded-error.d.mts +38 -0
  194. package/esm/errors/context-length-exceeded-error.d.mts.map +1 -0
  195. package/esm/errors/context-length-exceeded-error.mjs +31 -0
  196. package/esm/errors/context-length-exceeded-error.mjs.map +1 -0
  197. package/esm/errors/error-category.type.d.mts +30 -0
  198. package/esm/errors/error-category.type.d.mts.map +1 -0
  199. package/esm/errors/error-code.type.d.mts +19 -0
  200. package/esm/errors/error-code.type.d.mts.map +1 -0
  201. package/esm/errors/guardrail-violation-error.d.mts +55 -0
  202. package/esm/errors/guardrail-violation-error.d.mts.map +1 -0
  203. package/esm/errors/guardrail-violation-error.mjs +41 -0
  204. package/esm/errors/guardrail-violation-error.mjs.map +1 -0
  205. package/esm/errors/index.d.mts +29 -0
  206. package/esm/errors/index.mjs +29 -0
  207. package/esm/errors/invalid-request-error.d.mts +27 -0
  208. package/esm/errors/invalid-request-error.d.mts.map +1 -0
  209. package/esm/errors/invalid-request-error.mjs +31 -0
  210. package/esm/errors/invalid-request-error.mjs.map +1 -0
  211. package/esm/errors/max-iterations-error.d.mts +31 -0
  212. package/esm/errors/max-iterations-error.d.mts.map +1 -0
  213. package/esm/errors/max-iterations-error.mjs +32 -0
  214. package/esm/errors/max-iterations-error.mjs.map +1 -0
  215. package/esm/errors/max-steps-exceeded-error.d.mts +20 -0
  216. package/esm/errors/max-steps-exceeded-error.d.mts.map +1 -0
  217. package/esm/errors/max-steps-exceeded-error.mjs +21 -0
  218. package/esm/errors/max-steps-exceeded-error.mjs.map +1 -0
  219. package/esm/errors/provider-auth-error.d.mts +22 -0
  220. package/esm/errors/provider-auth-error.d.mts.map +1 -0
  221. package/esm/errors/provider-auth-error.mjs +26 -0
  222. package/esm/errors/provider-auth-error.mjs.map +1 -0
  223. package/esm/errors/provider-error.d.mts +38 -0
  224. package/esm/errors/provider-error.d.mts.map +1 -0
  225. package/esm/errors/provider-error.mjs +42 -0
  226. package/esm/errors/provider-error.mjs.map +1 -0
  227. package/esm/errors/provider-rate-limit-error.d.mts +34 -0
  228. package/esm/errors/provider-rate-limit-error.d.mts.map +1 -0
  229. package/esm/errors/provider-rate-limit-error.mjs +31 -0
  230. package/esm/errors/provider-rate-limit-error.mjs.map +1 -0
  231. package/esm/errors/provider-timeout-error.d.mts +22 -0
  232. package/esm/errors/provider-timeout-error.d.mts.map +1 -0
  233. package/esm/errors/provider-timeout-error.mjs +26 -0
  234. package/esm/errors/provider-timeout-error.mjs.map +1 -0
  235. package/esm/errors/quota-exceeded-error.d.mts +36 -0
  236. package/esm/errors/quota-exceeded-error.d.mts.map +1 -0
  237. package/esm/errors/quota-exceeded-error.mjs +40 -0
  238. package/esm/errors/quota-exceeded-error.mjs.map +1 -0
  239. package/esm/errors/routing-error.d.mts +23 -0
  240. package/esm/errors/routing-error.d.mts.map +1 -0
  241. package/esm/errors/routing-error.mjs +23 -0
  242. package/esm/errors/routing-error.mjs.map +1 -0
  243. package/esm/errors/schema-validation-error.d.mts +40 -0
  244. package/esm/errors/schema-validation-error.d.mts.map +1 -0
  245. package/esm/errors/schema-validation-error.mjs +37 -0
  246. package/esm/errors/schema-validation-error.mjs.map +1 -0
  247. package/esm/errors/step-failed-error.d.mts +20 -0
  248. package/esm/errors/step-failed-error.d.mts.map +1 -0
  249. package/esm/errors/step-failed-error.mjs +19 -0
  250. package/esm/errors/step-failed-error.mjs.map +1 -0
  251. package/esm/errors/supervisor-cancelled-error.d.mts +29 -0
  252. package/esm/errors/supervisor-cancelled-error.d.mts.map +1 -0
  253. package/esm/errors/supervisor-cancelled-error.mjs +29 -0
  254. package/esm/errors/supervisor-cancelled-error.mjs.map +1 -0
  255. package/esm/errors/supervisor-drift-error.d.mts +31 -0
  256. package/esm/errors/supervisor-drift-error.d.mts.map +1 -0
  257. package/esm/errors/supervisor-drift-error.mjs +30 -0
  258. package/esm/errors/supervisor-drift-error.mjs.map +1 -0
  259. package/esm/errors/supervisor-failed-error.d.mts +40 -0
  260. package/esm/errors/supervisor-failed-error.d.mts.map +1 -0
  261. package/esm/errors/supervisor-failed-error.mjs +43 -0
  262. package/esm/errors/supervisor-failed-error.mjs.map +1 -0
  263. package/esm/errors/supervisor-routing-error.d.mts +44 -0
  264. package/esm/errors/supervisor-routing-error.d.mts.map +1 -0
  265. package/esm/errors/supervisor-routing-error.mjs +39 -0
  266. package/esm/errors/supervisor-routing-error.mjs.map +1 -0
  267. package/esm/errors/tool-execution-error.d.mts +35 -0
  268. package/esm/errors/tool-execution-error.d.mts.map +1 -0
  269. package/esm/errors/tool-execution-error.mjs +32 -0
  270. package/esm/errors/tool-execution-error.mjs.map +1 -0
  271. package/esm/errors/workflow-cancelled-error.d.mts +23 -0
  272. package/esm/errors/workflow-cancelled-error.d.mts.map +1 -0
  273. package/esm/errors/workflow-cancelled-error.mjs +23 -0
  274. package/esm/errors/workflow-cancelled-error.mjs.map +1 -0
  275. package/esm/errors/workflow-drift-error.d.mts +25 -0
  276. package/esm/errors/workflow-drift-error.d.mts.map +1 -0
  277. package/esm/errors/workflow-drift-error.mjs +24 -0
  278. package/esm/errors/workflow-drift-error.mjs.map +1 -0
  279. package/esm/errors/workflow-error.d.mts +15 -0
  280. package/esm/errors/workflow-error.d.mts.map +1 -0
  281. package/esm/errors/workflow-error.mjs +18 -0
  282. package/esm/errors/workflow-error.mjs.map +1 -0
  283. package/esm/index.d.mts +137 -0
  284. package/esm/index.mjs +81 -0
  285. package/esm/middleware/builtins/budget.d.mts +103 -0
  286. package/esm/middleware/builtins/budget.d.mts.map +1 -0
  287. package/esm/middleware/builtins/budget.mjs +100 -0
  288. package/esm/middleware/builtins/budget.mjs.map +1 -0
  289. package/esm/middleware/builtins/guardrail.d.mts +92 -0
  290. package/esm/middleware/builtins/guardrail.d.mts.map +1 -0
  291. package/esm/middleware/builtins/guardrail.mjs +76 -0
  292. package/esm/middleware/builtins/guardrail.mjs.map +1 -0
  293. package/esm/middleware/builtins/semantic-cache.d.mts +106 -0
  294. package/esm/middleware/builtins/semantic-cache.d.mts.map +1 -0
  295. package/esm/middleware/builtins/semantic-cache.mjs +149 -0
  296. package/esm/middleware/builtins/semantic-cache.mjs.map +1 -0
  297. package/esm/middleware/helpers/compose.d.mts +42 -0
  298. package/esm/middleware/helpers/compose.d.mts.map +1 -0
  299. package/esm/middleware/helpers/compose.mjs +52 -0
  300. package/esm/middleware/helpers/compose.mjs.map +1 -0
  301. package/esm/middleware/helpers/for-tool.d.mts +55 -0
  302. package/esm/middleware/helpers/for-tool.d.mts.map +1 -0
  303. package/esm/middleware/helpers/for-tool.mjs +80 -0
  304. package/esm/middleware/helpers/for-tool.mjs.map +1 -0
  305. package/esm/middleware/helpers/index.d.mts +2 -0
  306. package/esm/middleware/index.d.mts +8 -0
  307. package/esm/middleware/index.mjs +10 -0
  308. package/esm/middleware/pipeline.d.mts +70 -0
  309. package/esm/middleware/pipeline.d.mts.map +1 -0
  310. package/esm/middleware/pipeline.mjs +100 -0
  311. package/esm/middleware/pipeline.mjs.map +1 -0
  312. package/esm/middleware/utils/extract-user-text.d.mts +33 -0
  313. package/esm/middleware/utils/extract-user-text.d.mts.map +1 -0
  314. package/esm/middleware/utils/extract-user-text.mjs +40 -0
  315. package/esm/middleware/utils/extract-user-text.mjs.map +1 -0
  316. package/esm/middleware/utils/index.d.mts +2 -0
  317. package/esm/middleware/utils/namespaced-state.d.mts +60 -0
  318. package/esm/middleware/utils/namespaced-state.d.mts.map +1 -0
  319. package/esm/middleware/utils/namespaced-state.mjs +60 -0
  320. package/esm/middleware/utils/namespaced-state.mjs.map +1 -0
  321. package/esm/mock/index.d.mts +4 -0
  322. package/esm/mock/index.mjs +5 -0
  323. package/esm/mock/mock-agent.d.mts +41 -0
  324. package/esm/mock/mock-agent.d.mts.map +1 -0
  325. package/esm/mock/mock-agent.mjs +39 -0
  326. package/esm/mock/mock-agent.mjs.map +1 -0
  327. package/esm/mock/mock-config.type.d.mts +31 -0
  328. package/esm/mock/mock-config.type.d.mts.map +1 -0
  329. package/esm/mock/mock-model.d.mts +94 -0
  330. package/esm/mock/mock-model.d.mts.map +1 -0
  331. package/esm/mock/mock-model.mjs +150 -0
  332. package/esm/mock/mock-model.mjs.map +1 -0
  333. package/esm/mock/mock-sdk.d.mts +25 -0
  334. package/esm/mock/mock-sdk.d.mts.map +1 -0
  335. package/esm/mock/mock-sdk.mjs +37 -0
  336. package/esm/mock/mock-sdk.mjs.map +1 -0
  337. package/esm/supervisor/as-tool.d.mts +45 -0
  338. package/esm/supervisor/as-tool.d.mts.map +1 -0
  339. package/esm/supervisor/as-tool.mjs +80 -0
  340. package/esm/supervisor/as-tool.mjs.map +1 -0
  341. package/esm/supervisor/cancellation.d.mts +12 -0
  342. package/esm/supervisor/cancellation.d.mts.map +1 -0
  343. package/esm/supervisor/cancellation.mjs +22 -0
  344. package/esm/supervisor/cancellation.mjs.map +1 -0
  345. package/esm/supervisor/decide.mjs +272 -0
  346. package/esm/supervisor/decide.mjs.map +1 -0
  347. package/esm/supervisor/emitter.d.mts +27 -0
  348. package/esm/supervisor/emitter.d.mts.map +1 -0
  349. package/esm/supervisor/emitter.mjs +47 -0
  350. package/esm/supervisor/emitter.mjs.map +1 -0
  351. package/esm/supervisor/entries.d.mts +141 -0
  352. package/esm/supervisor/entries.d.mts.map +1 -0
  353. package/esm/supervisor/entries.mjs +206 -0
  354. package/esm/supervisor/entries.mjs.map +1 -0
  355. package/esm/supervisor/execution.d.mts +637 -0
  356. package/esm/supervisor/execution.d.mts.map +1 -0
  357. package/esm/supervisor/execution.mjs +1868 -0
  358. package/esm/supervisor/execution.mjs.map +1 -0
  359. package/esm/supervisor/index.d.mts +11 -0
  360. package/esm/supervisor/index.mjs +12 -0
  361. package/esm/supervisor/router-prompt.d.mts +56 -0
  362. package/esm/supervisor/router-prompt.d.mts.map +1 -0
  363. package/esm/supervisor/router-prompt.mjs +52 -0
  364. package/esm/supervisor/router-prompt.mjs.map +1 -0
  365. package/esm/supervisor/signature.d.mts +28 -0
  366. package/esm/supervisor/signature.d.mts.map +1 -0
  367. package/esm/supervisor/signature.mjs +89 -0
  368. package/esm/supervisor/signature.mjs.map +1 -0
  369. package/esm/supervisor/snapshot.d.mts +54 -0
  370. package/esm/supervisor/snapshot.d.mts.map +1 -0
  371. package/esm/supervisor/snapshot.mjs +77 -0
  372. package/esm/supervisor/snapshot.mjs.map +1 -0
  373. package/esm/supervisor/supervisor-stream.d.mts +29 -0
  374. package/esm/supervisor/supervisor-stream.d.mts.map +1 -0
  375. package/esm/supervisor/supervisor-stream.mjs +89 -0
  376. package/esm/supervisor/supervisor-stream.mjs.map +1 -0
  377. package/esm/supervisor/supervisor.d.mts +27 -0
  378. package/esm/supervisor/supervisor.d.mts.map +1 -0
  379. package/esm/supervisor/supervisor.mjs +138 -0
  380. package/esm/supervisor/supervisor.mjs.map +1 -0
  381. package/esm/system-prompt/instruction.d.mts +52 -0
  382. package/esm/system-prompt/instruction.d.mts.map +1 -0
  383. package/esm/system-prompt/instruction.mjs +57 -0
  384. package/esm/system-prompt/instruction.mjs.map +1 -0
  385. package/esm/system-prompt/persona.d.mts +52 -0
  386. package/esm/system-prompt/persona.d.mts.map +1 -0
  387. package/esm/system-prompt/persona.mjs +57 -0
  388. package/esm/system-prompt/persona.mjs.map +1 -0
  389. package/esm/system-prompt/render-placeholders.d.mts +31 -0
  390. package/esm/system-prompt/render-placeholders.d.mts.map +1 -0
  391. package/esm/system-prompt/render-placeholders.mjs +58 -0
  392. package/esm/system-prompt/render-placeholders.mjs.map +1 -0
  393. package/esm/system-prompt/system-prompt.d.mts +117 -0
  394. package/esm/system-prompt/system-prompt.d.mts.map +1 -0
  395. package/esm/system-prompt/system-prompt.mjs +137 -0
  396. package/esm/system-prompt/system-prompt.mjs.map +1 -0
  397. package/esm/tool/index.d.mts +1 -0
  398. package/esm/tool/index.mjs +3 -0
  399. package/esm/tool/tool.d.mts +64 -0
  400. package/esm/tool/tool.d.mts.map +1 -0
  401. package/esm/tool/tool.mjs +184 -0
  402. package/esm/tool/tool.mjs.map +1 -0
  403. package/esm/utils/compute-cost.d.mts +58 -0
  404. package/esm/utils/compute-cost.d.mts.map +1 -0
  405. package/esm/utils/compute-cost.mjs +87 -0
  406. package/esm/utils/compute-cost.mjs.map +1 -0
  407. package/esm/utils/extract-json-payload.d.mts +33 -0
  408. package/esm/utils/extract-json-payload.d.mts.map +1 -0
  409. package/esm/utils/extract-json-payload.mjs +39 -0
  410. package/esm/utils/extract-json-payload.mjs.map +1 -0
  411. package/esm/utils/generate-run-id.d.mts +20 -0
  412. package/esm/utils/generate-run-id.d.mts.map +1 -0
  413. package/esm/utils/generate-run-id.mjs +23 -0
  414. package/esm/utils/generate-run-id.mjs.map +1 -0
  415. package/esm/utils/index.d.mts +9 -0
  416. package/esm/utils/index.mjs +11 -0
  417. package/esm/utils/json-schema.d.mts +57 -0
  418. package/esm/utils/json-schema.d.mts.map +1 -0
  419. package/esm/utils/json-schema.mjs +89 -0
  420. package/esm/utils/json-schema.mjs.map +1 -0
  421. package/esm/utils/prepare-attachment-part.d.mts +36 -0
  422. package/esm/utils/prepare-attachment-part.d.mts.map +1 -0
  423. package/esm/utils/prepare-attachment-part.mjs +140 -0
  424. package/esm/utils/prepare-attachment-part.mjs.map +1 -0
  425. package/esm/utils/resolve-attachment.d.mts +33 -0
  426. package/esm/utils/resolve-attachment.d.mts.map +1 -0
  427. package/esm/utils/resolve-attachment.mjs +73 -0
  428. package/esm/utils/resolve-attachment.mjs.map +1 -0
  429. package/esm/utils/safe-json-parse.d.mts +14 -0
  430. package/esm/utils/safe-json-parse.d.mts.map +1 -0
  431. package/esm/utils/safe-json-parse.mjs +22 -0
  432. package/esm/utils/safe-json-parse.mjs.map +1 -0
  433. package/esm/utils/stamp-report-lineage.d.mts +61 -0
  434. package/esm/utils/stamp-report-lineage.d.mts.map +1 -0
  435. package/esm/utils/stamp-report-lineage.mjs +47 -0
  436. package/esm/utils/stamp-report-lineage.mjs.map +1 -0
  437. package/esm/utils/token-count.d.mts +15 -0
  438. package/esm/utils/token-count.d.mts.map +1 -0
  439. package/esm/utils/token-count.mjs +18 -0
  440. package/esm/utils/token-count.mjs.map +1 -0
  441. package/esm/workflow/as-tool.mjs +55 -0
  442. package/esm/workflow/as-tool.mjs.map +1 -0
  443. package/esm/workflow/cancellation.mjs +44 -0
  444. package/esm/workflow/cancellation.mjs.map +1 -0
  445. package/esm/workflow/emitter.mjs +40 -0
  446. package/esm/workflow/emitter.mjs.map +1 -0
  447. package/esm/workflow/engine.mjs +362 -0
  448. package/esm/workflow/engine.mjs.map +1 -0
  449. package/esm/workflow/index.d.mts +3 -0
  450. package/esm/workflow/index.mjs +5 -0
  451. package/esm/workflow/retry.mjs +32 -0
  452. package/esm/workflow/retry.mjs.map +1 -0
  453. package/esm/workflow/router.mjs +56 -0
  454. package/esm/workflow/router.mjs.map +1 -0
  455. package/esm/workflow/run-scoped-emitter.mjs +32 -0
  456. package/esm/workflow/run-scoped-emitter.mjs.map +1 -0
  457. package/esm/workflow/signature.d.mts +7 -0
  458. package/esm/workflow/signature.d.mts.map +1 -0
  459. package/esm/workflow/signature.mjs +34 -0
  460. package/esm/workflow/signature.mjs.map +1 -0
  461. package/esm/workflow/snapshot.mjs +71 -0
  462. package/esm/workflow/snapshot.mjs.map +1 -0
  463. package/esm/workflow/state.mjs +37 -0
  464. package/esm/workflow/state.mjs.map +1 -0
  465. package/esm/workflow/step-runner.mjs +371 -0
  466. package/esm/workflow/step-runner.mjs.map +1 -0
  467. package/esm/workflow/step.d.mts +19 -0
  468. package/esm/workflow/step.d.mts.map +1 -0
  469. package/esm/workflow/step.mjs +37 -0
  470. package/esm/workflow/step.mjs.map +1 -0
  471. package/esm/workflow/workflow.d.mts +12 -0
  472. package/esm/workflow/workflow.d.mts.map +1 -0
  473. package/esm/workflow/workflow.mjs +116 -0
  474. package/esm/workflow/workflow.mjs.map +1 -0
  475. package/llms-full.txt +2293 -0
  476. package/llms.txt +20 -0
  477. package/package.json +37 -0
  478. package/skills/README.md +53 -0
  479. package/skills/ai-basics/SKILL.md +95 -0
  480. package/skills/attach-ai-middleware/SKILL.md +184 -0
  481. package/skills/define-ai-tool/SKILL.md +186 -0
  482. package/skills/embed-text/SKILL.md +105 -0
  483. package/skills/handle-ai-errors/SKILL.md +194 -0
  484. package/skills/log-ai-calls/SKILL.md +141 -0
  485. package/skills/persist-ai-data/SKILL.md +207 -0
  486. package/skills/pick-ai-provider/SKILL.md +164 -0
  487. package/skills/run-ai-agent/SKILL.md +267 -0
  488. package/skills/run-ai-workflow/SKILL.md +258 -0
  489. package/skills/run-supervisor/SKILL.md +309 -0
  490. package/skills/write-system-prompt/SKILL.md +129 -0
@@ -0,0 +1,56 @@
1
+ import { RoutingError } from "../errors/routing-error.mjs";
2
+ import "../errors/index.mjs";
3
+
4
+ //#region ../../@warlock.js/ai/src/workflow/router.ts
5
+ /**
6
+ * Resolve the next step to run after `step` completes. Tries
7
+ * step-level `nextStep` first, then workflow-level, then falls
8
+ * through (returns `undefined`, engine picks the next declared step).
9
+ *
10
+ * Throws `RoutingError` when either callback throws — routing is
11
+ * authoritative, so a broken router terminates the workflow instead
12
+ * of being retried.
13
+ */
14
+ async function resolveNextStep(params) {
15
+ const { step, definition, ctx } = params;
16
+ if (step.nextStep) {
17
+ let outcome;
18
+ try {
19
+ outcome = await step.nextStep(ctx);
20
+ } catch (err) {
21
+ throw new RoutingError(`workflow "${definition.name}": step "${step.name}" nextStep threw`, {
22
+ stepName: step.name,
23
+ cause: err
24
+ });
25
+ }
26
+ const mapped = mapNextStep(outcome);
27
+ if (mapped !== void 0) return mapped;
28
+ }
29
+ if (definition.nextStep) {
30
+ let outcome;
31
+ try {
32
+ outcome = await definition.nextStep(step.name, ctx);
33
+ } catch (err) {
34
+ throw new RoutingError(`workflow "${definition.name}": workflow-level nextStep threw after "${step.name}"`, {
35
+ stepName: step.name,
36
+ cause: err
37
+ });
38
+ }
39
+ const mapped = mapNextStep(outcome);
40
+ if (mapped !== void 0) return mapped;
41
+ }
42
+ }
43
+ function mapNextStep(outcome) {
44
+ if (!outcome) return void 0;
45
+ if ("end" in outcome && outcome.end === true) return "end";
46
+ if ("goto" in outcome && typeof outcome.goto === "string") return outcome.goto;
47
+ }
48
+ function nextDeclaredStep(definition, currentName) {
49
+ const idx = definition.steps.findIndex((s) => s.name === currentName);
50
+ if (idx === -1) return null;
51
+ return definition.steps[idx + 1]?.name ?? null;
52
+ }
53
+
54
+ //#endregion
55
+ export { mapNextStep, nextDeclaredStep, resolveNextStep };
56
+ //# sourceMappingURL=router.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/workflow/router.ts"],"sourcesContent":["import type { NextStepResult } from \"../contracts/workflow/next-step-result.type\";\nimport type { StepDefinition } from \"../contracts/workflow/step.contract\";\nimport type { WorkflowContext } from \"../contracts/workflow/workflow-context.type\";\nimport type { WorkflowDefinition } from \"../contracts/workflow/workflow.contract\";\nimport { RoutingError } from \"../errors\";\n\n/**\n * Resolve the next step to run after `step` completes. Tries\n * step-level `nextStep` first, then workflow-level, then falls\n * through (returns `undefined`, engine picks the next declared step).\n *\n * Throws `RoutingError` when either callback throws — routing is\n * authoritative, so a broken router terminates the workflow instead\n * of being retried.\n */\nexport async function resolveNextStep<T>(params: {\n step: StepDefinition;\n definition: WorkflowDefinition<any, T, any, any>;\n ctx: WorkflowContext;\n}): Promise<\"end\" | string | undefined> {\n const { step, definition, ctx } = params;\n\n if (step.nextStep) {\n let outcome: NextStepResult;\n try {\n outcome = await step.nextStep(ctx);\n } catch (err) {\n throw new RoutingError(\n `workflow \"${definition.name}\": step \"${step.name}\" nextStep threw`,\n { stepName: step.name, cause: err },\n );\n }\n const mapped = mapNextStep(outcome);\n if (mapped !== undefined) return mapped;\n }\n\n if (definition.nextStep) {\n let outcome: NextStepResult;\n try {\n outcome = await definition.nextStep(step.name, ctx);\n } catch (err) {\n throw new RoutingError(\n `workflow \"${definition.name}\": workflow-level nextStep threw after \"${step.name}\"`,\n { stepName: step.name, cause: err },\n );\n }\n const mapped = mapNextStep(outcome);\n if (mapped !== undefined) return mapped;\n }\n\n return undefined;\n}\n\nexport function mapNextStep(\n outcome: NextStepResult,\n): \"end\" | string | undefined {\n if (!outcome) return undefined;\n if (\"end\" in outcome && outcome.end === true) return \"end\";\n if (\"goto\" in outcome && typeof outcome.goto === \"string\")\n return outcome.goto;\n return undefined;\n}\n\nexport function nextDeclaredStep<T>(\n definition: WorkflowDefinition<any, T, any, any>,\n currentName: string,\n): string | null {\n const idx = definition.steps.findIndex(s => s.name === currentName);\n if (idx === -1) return null;\n return definition.steps[idx + 1]?.name ?? null;\n}\n"],"mappings":";;;;;;;;;;;;;AAeA,eAAsB,gBAAmB,QAID;CACtC,MAAM,EAAE,MAAM,YAAY,QAAQ;CAElC,IAAI,KAAK,UAAU;EACjB,IAAI;EACJ,IAAI;GACF,UAAU,MAAM,KAAK,SAAS,GAAG;EACnC,SAAS,KAAK;GACZ,MAAM,IAAI,aACR,aAAa,WAAW,KAAK,WAAW,KAAK,KAAK,mBAClD;IAAE,UAAU,KAAK;IAAM,OAAO;GAAI,CACpC;EACF;EACA,MAAM,SAAS,YAAY,OAAO;EAClC,IAAI,WAAW,QAAW,OAAO;CACnC;CAEA,IAAI,WAAW,UAAU;EACvB,IAAI;EACJ,IAAI;GACF,UAAU,MAAM,WAAW,SAAS,KAAK,MAAM,GAAG;EACpD,SAAS,KAAK;GACZ,MAAM,IAAI,aACR,aAAa,WAAW,KAAK,0CAA0C,KAAK,KAAK,IACjF;IAAE,UAAU,KAAK;IAAM,OAAO;GAAI,CACpC;EACF;EACA,MAAM,SAAS,YAAY,OAAO;EAClC,IAAI,WAAW,QAAW,OAAO;CACnC;AAGF;AAEA,SAAgB,YACd,SAC4B;CAC5B,IAAI,CAAC,SAAS,OAAO;CACrB,IAAI,SAAS,WAAW,QAAQ,QAAQ,MAAM,OAAO;CACrD,IAAI,UAAU,WAAW,OAAO,QAAQ,SAAS,UAC/C,OAAO,QAAQ;AAEnB;AAEA,SAAgB,iBACd,YACA,aACe;CACf,MAAM,MAAM,WAAW,MAAM,WAAU,MAAK,EAAE,SAAS,WAAW;CAClE,IAAI,QAAQ,IAAI,OAAO;CACvB,OAAO,WAAW,MAAM,MAAM,IAAI,QAAQ;AAC5C"}
@@ -0,0 +1,32 @@
1
+ //#region ../../@warlock.js/ai/src/workflow/run-scoped-emitter.ts
2
+ /**
3
+ * Bind a factory-scoped `WorkflowEmitter` to one run's identity.
4
+ *
5
+ * The engine and step-runner emit bare payloads through this sink;
6
+ * it injects `runId` / `rootRunId` once and delegates to the real
7
+ * three-tier emitter. This is the single place workflow run identity
8
+ * is stamped — the alternative (editing every `emitter.emit` call
9
+ * site) would be ~15 scattered edits and easy to miss one.
10
+ *
11
+ * `rootRunId === runId` for a standalone run; nested propagation
12
+ * (a child workflow inheriting an outer run's root) lands in a
13
+ * follow-up.
14
+ *
15
+ * @example
16
+ * // Inside runWorkflow(), once per execution:
17
+ * const emitter = runScopedEmitter(params.emitter, { runId, rootRunId: runId });
18
+ * emitter.emit("workflow.starting", { workflowName, input }, executionHandlers);
19
+ */
20
+ function runScopedEmitter(emitter, identity) {
21
+ return { emit(event, payload, executionHandlers) {
22
+ const fullPayload = {
23
+ ...payload,
24
+ ...identity
25
+ };
26
+ emitter.emit(event, fullPayload, executionHandlers);
27
+ } };
28
+ }
29
+
30
+ //#endregion
31
+ export { runScopedEmitter };
32
+ //# sourceMappingURL=run-scoped-emitter.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-scoped-emitter.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/workflow/run-scoped-emitter.ts"],"sourcesContent":["import type { EventIdentity } from \"../contracts/events/event-identity.type\";\nimport type { WorkflowEventMap } from \"../contracts/events/event-map.type\";\nimport type { WorkflowEventHandlers } from \"../contracts/workflow/workflow.contract\";\nimport type { WorkflowEmitter, WorkflowEventSink } from \"./emitter\";\n\n/**\n * Bind a factory-scoped `WorkflowEmitter` to one run's identity.\n *\n * The engine and step-runner emit bare payloads through this sink;\n * it injects `runId` / `rootRunId` once and delegates to the real\n * three-tier emitter. This is the single place workflow run identity\n * is stamped — the alternative (editing every `emitter.emit` call\n * site) would be ~15 scattered edits and easy to miss one.\n *\n * `rootRunId === runId` for a standalone run; nested propagation\n * (a child workflow inheriting an outer run's root) lands in a\n * follow-up.\n *\n * @example\n * // Inside runWorkflow(), once per execution:\n * const emitter = runScopedEmitter(params.emitter, { runId, rootRunId: runId });\n * emitter.emit(\"workflow.starting\", { workflowName, input }, executionHandlers);\n */\nexport function runScopedEmitter(\n emitter: WorkflowEmitter,\n identity: EventIdentity,\n): WorkflowEventSink {\n return {\n emit(event, payload, executionHandlers?: WorkflowEventHandlers) {\n const fullPayload = {\n ...payload,\n ...identity,\n } as WorkflowEventMap[typeof event];\n\n emitter.emit(event, fullPayload, executionHandlers);\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,iBACd,SACA,UACmB;CACnB,OAAO,EACL,KAAK,OAAO,SAAS,mBAA2C;EAC9D,MAAM,cAAc;GAClB,GAAG;GACH,GAAG;EACL;EAEA,QAAQ,KAAK,OAAO,aAAa,iBAAiB;CACpD,EACF;AACF"}
@@ -0,0 +1,7 @@
1
+ import { WorkflowDefinition } from "../contracts/workflow/workflow.contract.mjs";
2
+
3
+ //#region ../../@warlock.js/ai/src/workflow/signature.d.ts
4
+ declare function computeSignature<T>(definition: WorkflowDefinition<any, T, any, any>): string;
5
+ //#endregion
6
+ export { computeSignature };
7
+ //# sourceMappingURL=signature.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.d.mts","names":[],"sources":["../../../../../../@warlock.js/ai/src/workflow/signature.ts"],"mappings":";;;iBAoCgB,gBAAA,GAAA,CAAoB,UAAA,EAAY,kBAAkB,MAAM,CAAA"}
@@ -0,0 +1,34 @@
1
+ //#region ../../@warlock.js/ai/src/workflow/signature.ts
2
+ function stepFingerprint(step) {
3
+ const tag = step.parallel ? "parallel" : step.agent ? "agent" : step.run ? "run" : "empty";
4
+ const agentName = step.agent?.name;
5
+ return {
6
+ n: step.name,
7
+ t: tag,
8
+ a: agentName,
9
+ c: step.parallel?.map((child) => stepFingerprint(child)) ?? null
10
+ };
11
+ }
12
+ /**
13
+ * FNV-1a 32-bit hash — deterministic, no crypto dependency.
14
+ */
15
+ function hash(input) {
16
+ let h = 2166136261;
17
+ for (let i = 0; i < input.length; i++) {
18
+ h ^= input.charCodeAt(i);
19
+ h = h + ((h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24)) >>> 0;
20
+ }
21
+ return h.toString(16).padStart(8, "0");
22
+ }
23
+ function computeSignature(definition) {
24
+ const fingerprint = {
25
+ n: definition.name,
26
+ v: definition.version ?? null,
27
+ s: definition.steps.map((step) => stepFingerprint(step))
28
+ };
29
+ return hash(JSON.stringify(fingerprint));
30
+ }
31
+
32
+ //#endregion
33
+ export { computeSignature };
34
+ //# sourceMappingURL=signature.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/workflow/signature.ts"],"sourcesContent":["import type { StepDefinition } from \"../contracts/workflow/step.contract\";\nimport type { WorkflowDefinition } from \"../contracts/workflow/workflow.contract\";\n\nfunction stepFingerprint(step: StepDefinition): unknown {\n const tag = step.parallel\n ? \"parallel\"\n : step.agent\n ? \"agent\"\n : step.run\n ? \"run\"\n : \"empty\";\n\n const agentName = step.agent?.name;\n\n return {\n n: step.name,\n t: tag,\n a: agentName,\n c: step.parallel?.map(child => stepFingerprint(child)) ?? null,\n };\n}\n\n/**\n * FNV-1a 32-bit hash — deterministic, no crypto dependency.\n */\nfunction hash(input: string): string {\n let h = 0x811c9dc5;\n\n for (let i = 0; i < input.length; i++) {\n h ^= input.charCodeAt(i);\n h = (h + ((h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24))) >>> 0;\n }\n\n return h.toString(16).padStart(8, \"0\");\n}\n\nexport function computeSignature<T>(definition: WorkflowDefinition<any, T, any, any>): string {\n const fingerprint = {\n n: definition.name,\n v: definition.version ?? null,\n s: definition.steps.map(step => stepFingerprint(step)),\n };\n\n return hash(JSON.stringify(fingerprint));\n}\n"],"mappings":";AAGA,SAAS,gBAAgB,MAA+B;CACtD,MAAM,MAAM,KAAK,WACb,aACA,KAAK,QACH,UACA,KAAK,MACH,QACA;CAER,MAAM,YAAY,KAAK,OAAO;CAE9B,OAAO;EACL,GAAG,KAAK;EACR,GAAG;EACH,GAAG;EACH,GAAG,KAAK,UAAU,KAAI,UAAS,gBAAgB,KAAK,CAAC,KAAK;CAC5D;AACF;;;;AAKA,SAAS,KAAK,OAAuB;CACnC,IAAI,IAAI;CAER,KAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;EACrC,KAAK,MAAM,WAAW,CAAC;EACvB,IAAK,MAAM,KAAK,MAAM,KAAK,MAAM,KAAK,MAAM,KAAK,MAAM,KAAK,SAAU;CACxE;CAEA,OAAO,EAAE,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AACvC;AAEA,SAAgB,iBAAoB,YAA0D;CAC5F,MAAM,cAAc;EAClB,GAAG,WAAW;EACd,GAAG,WAAW,WAAW;EACzB,GAAG,WAAW,MAAM,KAAI,SAAQ,gBAAgB,IAAI,CAAC;CACvD;CAEA,OAAO,KAAK,KAAK,UAAU,WAAW,CAAC;AACzC"}
@@ -0,0 +1,71 @@
1
+ import { WorkflowError } from "../errors/workflow-error.mjs";
2
+ import { WorkflowDriftError } from "../errors/workflow-drift-error.mjs";
3
+ import "../errors/index.mjs";
4
+ import { resolveDefaultStore } from "../config.mjs";
5
+
6
+ //#region ../../@warlock.js/ai/src/workflow/snapshot.ts
7
+ /**
8
+ * Resolve the effective snapshot store: the workflow's own
9
+ * `snapshotStore` field wins; absent that, fall back to the global
10
+ * default set via `ai.config({ defaultStore })`.
11
+ */
12
+ function resolveSnapshotStore(definition) {
13
+ return definition.snapshotStore ?? resolveDefaultStore();
14
+ }
15
+ /**
16
+ * Write the current run's state to the configured `KVStore`. Returns
17
+ * an outcome object instead of throwing so the engine can surface
18
+ * persistence failures via events + logs without aborting the run.
19
+ * Callers decide whether a failed checkpoint is fatal.
20
+ *
21
+ * No-op (returns `{ ok: true }`) when the workflow has no store
22
+ * configured — the common in-memory test case.
23
+ */
24
+ async function persistSnapshot(params) {
25
+ const store = resolveSnapshotStore(params.definition);
26
+ if (!store) return { ok: true };
27
+ const snapshot = {
28
+ runId: params.runId,
29
+ workflowName: params.definition.name,
30
+ signature: params.signature,
31
+ version: params.definition.version,
32
+ input: params.input,
33
+ state: { ...params.state },
34
+ steps: { ...params.steps },
35
+ next: params.next,
36
+ status: params.status,
37
+ startedAt: params.startedAt,
38
+ savedAt: (/* @__PURE__ */ new Date()).toISOString()
39
+ };
40
+ try {
41
+ await store.set(params.runId, snapshot);
42
+ return { ok: true };
43
+ } catch (err) {
44
+ return {
45
+ ok: false,
46
+ error: err
47
+ };
48
+ }
49
+ }
50
+ /**
51
+ * Load a prior run's snapshot from the store and run the drift
52
+ * check. Throws `WorkflowError` when no snapshot exists, and
53
+ * `WorkflowDriftError` when the stored signature doesn't match the
54
+ * current definition (unless `force` is set).
55
+ */
56
+ async function loadSnapshotForResume(params) {
57
+ const store = resolveSnapshotStore(params.definition);
58
+ if (!store) throw new WorkflowError(`workflow "${params.definition.name}" has no store configured — set \`snapshotStore\` on the definition or call \`ai.config({ defaultStore })\` at boot before calling resume()`);
59
+ const snap = await store.get(params.runId) ?? null;
60
+ if (!snap) throw new WorkflowError(`workflow "${params.definition.name}": no snapshot for runId "${params.runId}"`);
61
+ if (!params.options?.force && snap.signature !== params.signature) throw new WorkflowDriftError(`workflow "${params.definition.name}" signature drift on resume`, {
62
+ savedSignature: snap.signature,
63
+ currentSignature: params.signature,
64
+ runId: params.runId
65
+ });
66
+ return snap;
67
+ }
68
+
69
+ //#endregion
70
+ export { loadSnapshotForResume, persistSnapshot };
71
+ //# sourceMappingURL=snapshot.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"snapshot.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/workflow/snapshot.ts"],"sourcesContent":["import { resolveDefaultStore } from \"../config\";\nimport type { StepSnapshot } from \"../contracts/result/step-result.type\";\nimport type { WorkflowSnapshot } from \"../contracts/workflow/workflow-snapshot.type\";\nimport type {\n WorkflowDefinition,\n WorkflowResumeOptions,\n} from \"../contracts/workflow/workflow.contract\";\nimport { WorkflowDriftError, WorkflowError } from \"../errors\";\n\n/**\n * Resolve the effective snapshot store: the workflow's own\n * `snapshotStore` field wins; absent that, fall back to the global\n * default set via `ai.config({ defaultStore })`.\n */\nfunction resolveSnapshotStore<T>(definition: WorkflowDefinition<any, T, any, any>) {\n return definition.snapshotStore ?? resolveDefaultStore();\n}\n\nexport type PersistParams<T> = {\n definition: WorkflowDefinition<any, T, any, any>;\n signature: string;\n runId: string;\n startedAt: string;\n input: unknown;\n state: Record<string, unknown>;\n steps: Record<string, StepSnapshot>;\n next: string | null;\n status: WorkflowSnapshot[\"status\"];\n};\n\nexport type PersistOutcome = { ok: true } | { ok: false; error: unknown };\n\n/**\n * Write the current run's state to the configured `KVStore`. Returns\n * an outcome object instead of throwing so the engine can surface\n * persistence failures via events + logs without aborting the run.\n * Callers decide whether a failed checkpoint is fatal.\n *\n * No-op (returns `{ ok: true }`) when the workflow has no store\n * configured — the common in-memory test case.\n */\nexport async function persistSnapshot<T>(\n params: PersistParams<T>,\n): Promise<PersistOutcome> {\n const store = resolveSnapshotStore(params.definition);\n\n if (!store) return { ok: true };\n\n const snapshot: WorkflowSnapshot = {\n runId: params.runId,\n workflowName: params.definition.name,\n signature: params.signature,\n version: params.definition.version,\n input: params.input,\n state: { ...params.state },\n steps: { ...params.steps },\n next: params.next,\n status: params.status,\n startedAt: params.startedAt,\n savedAt: new Date().toISOString(),\n };\n\n try {\n await store.set(params.runId, snapshot);\n return { ok: true };\n } catch (err) {\n return { ok: false, error: err };\n }\n}\n\n/**\n * Load a prior run's snapshot from the store and run the drift\n * check. Throws `WorkflowError` when no snapshot exists, and\n * `WorkflowDriftError` when the stored signature doesn't match the\n * current definition (unless `force` is set).\n */\nexport async function loadSnapshotForResume<T>(params: {\n definition: WorkflowDefinition<any, T, any, any>;\n signature: string;\n runId: string;\n options?: WorkflowResumeOptions;\n}): Promise<WorkflowSnapshot> {\n const store = resolveSnapshotStore(params.definition);\n\n if (!store) {\n throw new WorkflowError(\n `workflow \"${params.definition.name}\" has no store configured — set \\`snapshotStore\\` on the definition or call \\`ai.config({ defaultStore })\\` at boot before calling resume()`,\n );\n }\n\n const snap = (await store.get<WorkflowSnapshot>(params.runId)) ?? null;\n\n if (!snap) {\n throw new WorkflowError(\n `workflow \"${params.definition.name}\": no snapshot for runId \"${params.runId}\"`,\n );\n }\n\n if (!params.options?.force && snap.signature !== params.signature) {\n throw new WorkflowDriftError(\n `workflow \"${params.definition.name}\" signature drift on resume`,\n {\n savedSignature: snap.signature,\n currentSignature: params.signature,\n runId: params.runId,\n },\n );\n }\n\n return snap;\n}\n"],"mappings":";;;;;;;;;;;AAcA,SAAS,qBAAwB,YAAkD;CACjF,OAAO,WAAW,iBAAiB,oBAAoB;AACzD;;;;;;;;;;AAyBA,eAAsB,gBACpB,QACyB;CACzB,MAAM,QAAQ,qBAAqB,OAAO,UAAU;CAEpD,IAAI,CAAC,OAAO,OAAO,EAAE,IAAI,KAAK;CAE9B,MAAM,WAA6B;EACjC,OAAO,OAAO;EACd,cAAc,OAAO,WAAW;EAChC,WAAW,OAAO;EAClB,SAAS,OAAO,WAAW;EAC3B,OAAO,OAAO;EACd,OAAO,EAAE,GAAG,OAAO,MAAM;EACzB,OAAO,EAAE,GAAG,OAAO,MAAM;EACzB,MAAM,OAAO;EACb,QAAQ,OAAO;EACf,WAAW,OAAO;EAClB,0BAAS,IAAI,KAAK,GAAE,YAAY;CAClC;CAEA,IAAI;EACF,MAAM,MAAM,IAAI,OAAO,OAAO,QAAQ;EACtC,OAAO,EAAE,IAAI,KAAK;CACpB,SAAS,KAAK;EACZ,OAAO;GAAE,IAAI;GAAO,OAAO;EAAI;CACjC;AACF;;;;;;;AAQA,eAAsB,sBAAyB,QAKjB;CAC5B,MAAM,QAAQ,qBAAqB,OAAO,UAAU;CAEpD,IAAI,CAAC,OACH,MAAM,IAAI,cACR,aAAa,OAAO,WAAW,KAAK,4IACtC;CAGF,MAAM,OAAQ,MAAM,MAAM,IAAsB,OAAO,KAAK,KAAM;CAElE,IAAI,CAAC,MACH,MAAM,IAAI,cACR,aAAa,OAAO,WAAW,KAAK,4BAA4B,OAAO,MAAM,EAC/E;CAGF,IAAI,CAAC,OAAO,SAAS,SAAS,KAAK,cAAc,OAAO,WACtD,MAAM,IAAI,mBACR,aAAa,OAAO,WAAW,KAAK,8BACpC;EACE,gBAAgB,KAAK;EACrB,kBAAkB,OAAO;EACzB,OAAO,OAAO;CAChB,CACF;CAGF,OAAO;AACT"}
@@ -0,0 +1,37 @@
1
+ import { WorkflowError } from "../errors/workflow-error.mjs";
2
+ import "../errors/index.mjs";
3
+
4
+ //#region ../../@warlock.js/ai/src/workflow/state.ts
5
+ /**
6
+ * Deep-clone workflow state. Uses `structuredClone` — handles Date,
7
+ * Map, Set, ArrayBuffer, nested objects, arrays, primitives. Throws
8
+ * a typed `WorkflowError` on older runtimes (pre-Node-17) rather
9
+ * than silently falling back to a JSON round-trip that would drop
10
+ * non-serializable values like Dates without warning.
11
+ *
12
+ * Workflow state should stay serializable anyway (it round-trips
13
+ * through `KVStore` on every checkpoint). If `structuredClone` chokes
14
+ * on a value, that's a bug in the user's state — surface it.
15
+ */
16
+ function cloneState(value) {
17
+ if (typeof structuredClone !== "function") throw new WorkflowError("workflow state cloning requires `structuredClone` (Node 17+ or a modern browser)");
18
+ return structuredClone(value);
19
+ }
20
+ /**
21
+ * Recursively freeze `value` and every nested plain object / array so
22
+ * consumers of `ctx.steps[x].state` or `report.state` can't mutate
23
+ * historical snapshots. Already-frozen values are skipped.
24
+ */
25
+ function deepFreeze(value) {
26
+ if (value === null || typeof value !== "object") return value;
27
+ if (Object.isFrozen(value)) return value;
28
+ for (const key of Object.keys(value)) {
29
+ const child = value[key];
30
+ if (child && typeof child === "object") deepFreeze(child);
31
+ }
32
+ return Object.freeze(value);
33
+ }
34
+
35
+ //#endregion
36
+ export { cloneState, deepFreeze };
37
+ //# sourceMappingURL=state.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.mjs","names":[],"sources":["../../../../../../@warlock.js/ai/src/workflow/state.ts"],"sourcesContent":["import { WorkflowError } from \"../errors\";\n\n/**\n * Deep-clone workflow state. Uses `structuredClone` — handles Date,\n * Map, Set, ArrayBuffer, nested objects, arrays, primitives. Throws\n * a typed `WorkflowError` on older runtimes (pre-Node-17) rather\n * than silently falling back to a JSON round-trip that would drop\n * non-serializable values like Dates without warning.\n *\n * Workflow state should stay serializable anyway (it round-trips\n * through `KVStore` on every checkpoint). If `structuredClone` chokes\n * on a value, that's a bug in the user's state — surface it.\n */\nexport function cloneState<T>(value: T): T {\n if (typeof structuredClone !== \"function\") {\n throw new WorkflowError(\n \"workflow state cloning requires `structuredClone` (Node 17+ or a modern browser)\",\n );\n }\n\n return structuredClone(value);\n}\n\n/**\n * Recursively freeze `value` and every nested plain object / array so\n * consumers of `ctx.steps[x].state` or `report.state` can't mutate\n * historical snapshots. Already-frozen values are skipped.\n */\nexport function deepFreeze<T>(value: T): T {\n if (value === null || typeof value !== \"object\") return value;\n if (Object.isFrozen(value)) return value;\n\n for (const key of Object.keys(value as Record<string, unknown>)) {\n const child = (value as Record<string, unknown>)[key];\n if (child && typeof child === \"object\") deepFreeze(child);\n }\n\n return Object.freeze(value);\n}\n"],"mappings":";;;;;;;;;;;;;;;AAaA,SAAgB,WAAc,OAAa;CACzC,IAAI,OAAO,oBAAoB,YAC7B,MAAM,IAAI,cACR,kFACF;CAGF,OAAO,gBAAgB,KAAK;AAC9B;;;;;;AAOA,SAAgB,WAAc,OAAa;CACzC,IAAI,UAAU,QAAQ,OAAO,UAAU,UAAU,OAAO;CACxD,IAAI,OAAO,SAAS,KAAK,GAAG,OAAO;CAEnC,KAAK,MAAM,OAAO,OAAO,KAAK,KAAgC,GAAG;EAC/D,MAAM,QAAS,MAAkC;EACjD,IAAI,SAAS,OAAO,UAAU,UAAU,WAAW,KAAK;CAC1D;CAEA,OAAO,OAAO,OAAO,KAAK;AAC5B"}
@@ -0,0 +1,371 @@
1
+ import { AIError } from "../errors/ai-error.mjs";
2
+ import { WorkflowError } from "../errors/workflow-error.mjs";
3
+ import { SchemaValidationError } from "../errors/schema-validation-error.mjs";
4
+ import { StepFailedError } from "../errors/step-failed-error.mjs";
5
+ import { WorkflowCancelledError } from "../errors/workflow-cancelled-error.mjs";
6
+ import "../errors/index.mjs";
7
+ import { createCancelledError, sleep } from "./cancellation.mjs";
8
+ import { cloneState, deepFreeze } from "./state.mjs";
9
+ import { isAbortError, resolveBackoff, resolveRetryConfig } from "./retry.mjs";
10
+
11
+ //#region ../../@warlock.js/ai/src/workflow/step-runner.ts
12
+ /**
13
+ * Narrow an `executionResult` to an `AgentResult` when the step ran
14
+ * an agent. Custom `run` steps return arbitrary values, so the
15
+ * `type: "agent"` discriminant keeps us honest.
16
+ */
17
+ function asAgentResult(result) {
18
+ if (!result || typeof result !== "object") return void 0;
19
+ if (result.type !== "agent") return void 0;
20
+ return result;
21
+ }
22
+ /**
23
+ * Drive one step's full lifecycle — skip evaluation, parallel
24
+ * dispatch, retry loop around before → run|agent → output → after.
25
+ * Returns a mutable snapshot; the engine deep-freezes it before
26
+ * exposing.
27
+ */
28
+ async function executeStep(params) {
29
+ const { step, emitter, executionHandlers, logger, logModule, signal } = params;
30
+ const startedAt = (/* @__PURE__ */ new Date()).toISOString();
31
+ const stepStartPerf = performance.now();
32
+ params.step.on?.starting?.({ step: step.name });
33
+ emitter.emit("workflow.step.starting", { step: step.name }, executionHandlers);
34
+ logger.debug(logModule, "step.starting", `${step.name} step starting`, { step: step.name });
35
+ const stepState = cloneState(params.state);
36
+ try {
37
+ if (step.skip) {
38
+ if (await step.skip(params.buildContext({ state: stepState }))) {
39
+ const endedAt = (/* @__PURE__ */ new Date()).toISOString();
40
+ const duration = performance.now() - stepStartPerf;
41
+ emitter.emit("workflow.step.skipped", { step: step.name }, executionHandlers);
42
+ logger.debug(logModule, "step.skipped", `${step.name} step skipped`, { step: step.name });
43
+ return {
44
+ output: void 0,
45
+ skipped: true,
46
+ status: "skipped",
47
+ startedAt,
48
+ endedAt,
49
+ duration,
50
+ attempts: 0,
51
+ attemptHistory: [],
52
+ state: stepState
53
+ };
54
+ }
55
+ }
56
+ } catch (err) {
57
+ return buildFailedSnapshot(step, stepState, startedAt, stepStartPerf, 1, [failedAttempt(1, err, (/* @__PURE__ */ new Date()).toISOString(), performance.now())]);
58
+ }
59
+ if (step.parallel && step.parallel.length > 0) return runParallelStep({
60
+ ...params,
61
+ step,
62
+ stepState,
63
+ startedAt,
64
+ startPerf: stepStartPerf
65
+ });
66
+ const retryConfig = resolveRetryConfig(step, params.workflowDefaultRetry);
67
+ const attempts = [];
68
+ const totalAttempts = Math.max(1, retryConfig.attempts ?? 1);
69
+ let lastError;
70
+ let executionResult;
71
+ let output;
72
+ let succeeded = false;
73
+ for (let attempt = 1; attempt <= totalAttempts; attempt++) {
74
+ if (signal?.aborted) throw createCancelledError(signal);
75
+ const attemptStart = (/* @__PURE__ */ new Date()).toISOString();
76
+ const attemptStartPerf = performance.now();
77
+ try {
78
+ const attemptState = cloneState(params.state);
79
+ if (step.before) await step.before(params.buildContext({ state: attemptState }));
80
+ if (step.agent) {
81
+ const { prompt, ...agentOpts } = step.input ? await step.input(params.buildContext({ state: attemptState })) : { prompt: "" };
82
+ const result = await step.agent.execute(prompt, {
83
+ ...agentOpts,
84
+ signal
85
+ });
86
+ executionResult = result;
87
+ if (result.usage) {
88
+ params.usage.input += result.usage.input ?? 0;
89
+ params.usage.output += result.usage.output ?? 0;
90
+ params.usage.total += result.usage.total ?? 0;
91
+ }
92
+ if (result.error) throw result.error;
93
+ } else if (step.run) executionResult = await step.run(params.buildContext({ state: attemptState }));
94
+ if (step.output) {
95
+ const extracted = await step.output.extract(params.buildContext({
96
+ state: attemptState,
97
+ agentResult: executionResult
98
+ }));
99
+ output = await validateSchema(step.output.schema, extracted);
100
+ } else output = void 0;
101
+ if (step.after) await step.after(params.buildContext({
102
+ state: attemptState,
103
+ agentResult: executionResult
104
+ }));
105
+ Object.assign(stepState, attemptState);
106
+ attempts.push({
107
+ index: attempt,
108
+ startedAt: attemptStart,
109
+ endedAt: (/* @__PURE__ */ new Date()).toISOString(),
110
+ duration: performance.now() - attemptStartPerf,
111
+ status: "success"
112
+ });
113
+ succeeded = true;
114
+ break;
115
+ } catch (err) {
116
+ if (isAbortError(err) || err instanceof WorkflowCancelledError) throw createCancelledError(signal);
117
+ attempts.push({
118
+ index: attempt,
119
+ startedAt: attemptStart,
120
+ endedAt: (/* @__PURE__ */ new Date()).toISOString(),
121
+ duration: performance.now() - attemptStartPerf,
122
+ status: "failed",
123
+ error: toAIError(err)
124
+ });
125
+ lastError = err;
126
+ if (!(attempt < totalAttempts && (retryConfig.retryOn ? retryConfig.retryOn(err, attempt) !== false : true))) break;
127
+ emitter.emit("workflow.step.retrying", {
128
+ step: step.name,
129
+ attempt: attempt + 1,
130
+ totalAttempts,
131
+ lastError: err
132
+ }, params.executionHandlers);
133
+ step.on?.retrying?.({
134
+ step: step.name,
135
+ attempt: attempt + 1,
136
+ totalAttempts,
137
+ lastError: err
138
+ });
139
+ logger.warn(logModule, "step.retrying", `${step.name} step retrying`, {
140
+ step: step.name,
141
+ attempt: attempt + 1
142
+ });
143
+ retryConfig.onRetry?.(attempt + 1, err);
144
+ const delay = resolveBackoff(attempt, retryConfig.backoff);
145
+ if (delay > 0) await sleep(delay, signal);
146
+ }
147
+ }
148
+ const endedAt = (/* @__PURE__ */ new Date()).toISOString();
149
+ const duration = performance.now() - stepStartPerf;
150
+ if (!succeeded) {
151
+ const aiError = toAIError(lastError);
152
+ const stepError = new StepFailedError(`step "${step.name}" failed after ${attempts.length} attempt(s): ${aiError.message}`, {
153
+ stepName: step.name,
154
+ attempts: attempts.length,
155
+ cause: aiError
156
+ });
157
+ emitter.emit("workflow.step.failed", {
158
+ step: step.name,
159
+ error: stepError,
160
+ attempts: attempts.length
161
+ }, params.executionHandlers);
162
+ step.on?.failed?.({
163
+ step: step.name,
164
+ error: stepError,
165
+ attempts: attempts.length
166
+ });
167
+ logger.error(logModule, "step.failed", `${step.name} step failed`, {
168
+ step: step.name,
169
+ attempts: attempts.length,
170
+ code: stepError.code
171
+ });
172
+ const failedAgentResult = asAgentResult(executionResult);
173
+ return {
174
+ output: void 0,
175
+ skipped: false,
176
+ status: "failed",
177
+ startedAt,
178
+ endedAt,
179
+ duration,
180
+ attempts: attempts.length,
181
+ attemptHistory: attempts,
182
+ error: stepError,
183
+ state: stepState,
184
+ executionResult: executionResult && typeof executionResult === "object" ? executionResult : void 0,
185
+ agentReport: failedAgentResult?.report,
186
+ agentUsage: failedAgentResult?.usage
187
+ };
188
+ }
189
+ emitter.emit("workflow.step.completed", {
190
+ step: step.name,
191
+ output,
192
+ duration
193
+ }, params.executionHandlers);
194
+ step.on?.completed?.({
195
+ step: step.name,
196
+ output,
197
+ duration
198
+ });
199
+ logger.debug(logModule, "step.completed", "step completed", {
200
+ step: step.name,
201
+ duration
202
+ });
203
+ const completedAgentResult = asAgentResult(executionResult);
204
+ return {
205
+ output,
206
+ skipped: false,
207
+ status: "completed",
208
+ startedAt,
209
+ endedAt,
210
+ duration,
211
+ attempts: attempts.length,
212
+ attemptHistory: attempts,
213
+ state: stepState,
214
+ executionResult: executionResult && typeof executionResult === "object" ? executionResult : void 0,
215
+ agentReport: completedAgentResult?.report,
216
+ agentUsage: completedAgentResult?.usage
217
+ };
218
+ }
219
+ async function runParallelStep(params) {
220
+ const { step, emitter, executionHandlers, logger, logModule, signal } = params;
221
+ const sharedState = params.stepState;
222
+ const childSnapshots = {};
223
+ let firstError;
224
+ const results = await Promise.all((step.parallel ?? []).map(async (child) => {
225
+ const snap = await executeStep({
226
+ step: child,
227
+ state: sharedState,
228
+ emitter,
229
+ executionHandlers,
230
+ logger,
231
+ logModule,
232
+ signal,
233
+ buildContext: params.buildContext,
234
+ usage: params.usage
235
+ });
236
+ Object.assign(sharedState, snap.state);
237
+ return {
238
+ child,
239
+ snap
240
+ };
241
+ }));
242
+ for (const { child, snap } of results) {
243
+ childSnapshots[child.name] = finalizeSnapshot(snap);
244
+ if (snap.status === "failed" && !firstError && snap.error) firstError = snap.error;
245
+ }
246
+ const endedAt = (/* @__PURE__ */ new Date()).toISOString();
247
+ const duration = performance.now() - params.startPerf;
248
+ let output;
249
+ if (step.output) try {
250
+ const ctx = params.buildContext({ state: sharedState });
251
+ const ctxWithChildren = {
252
+ ...ctx,
253
+ steps: {
254
+ ...ctx.steps,
255
+ [step.name]: {
256
+ ...childSnapshots,
257
+ steps: childSnapshots,
258
+ status: firstError ? "failed" : "completed"
259
+ }
260
+ }
261
+ };
262
+ const extracted = await step.output.extract(ctxWithChildren);
263
+ output = await validateSchema(step.output.schema, extracted);
264
+ } catch (err) {
265
+ firstError = firstError ?? toAIError(err);
266
+ }
267
+ const status = firstError ? "failed" : "completed";
268
+ if (status === "completed") {
269
+ emitter.emit("workflow.step.completed", {
270
+ step: step.name,
271
+ output,
272
+ duration
273
+ }, executionHandlers);
274
+ step.on?.completed?.({
275
+ step: step.name,
276
+ output,
277
+ duration
278
+ });
279
+ } else {
280
+ emitter.emit("workflow.step.failed", {
281
+ step: step.name,
282
+ error: firstError,
283
+ attempts: 1
284
+ }, executionHandlers);
285
+ step.on?.failed?.({
286
+ step: step.name,
287
+ error: firstError,
288
+ attempts: 1
289
+ });
290
+ }
291
+ return {
292
+ output,
293
+ skipped: false,
294
+ status,
295
+ startedAt: params.startedAt,
296
+ endedAt,
297
+ duration,
298
+ attempts: 1,
299
+ attemptHistory: [],
300
+ error: firstError,
301
+ state: sharedState,
302
+ steps: childSnapshots
303
+ };
304
+ }
305
+ function finalizeSnapshot(snap) {
306
+ return Object.freeze({
307
+ output: snap.output,
308
+ skipped: snap.skipped,
309
+ status: snap.status,
310
+ startedAt: snap.startedAt,
311
+ endedAt: snap.endedAt,
312
+ duration: snap.duration,
313
+ attempts: snap.attempts,
314
+ attemptHistory: snap.attemptHistory,
315
+ error: snap.error,
316
+ state: deepFreeze(cloneState(snap.state)),
317
+ executionResult: snap.executionResult,
318
+ agentReport: snap.agentReport,
319
+ agentUsage: snap.agentUsage,
320
+ steps: snap.steps
321
+ });
322
+ }
323
+ function buildFailedSnapshot(step, state, startedAt, startPerf, attemptsCount, attemptHistory) {
324
+ const endedAt = (/* @__PURE__ */ new Date()).toISOString();
325
+ const duration = performance.now() - startPerf;
326
+ const lastErr = attemptHistory[attemptHistory.length - 1]?.error;
327
+ return {
328
+ output: void 0,
329
+ skipped: false,
330
+ status: "failed",
331
+ startedAt,
332
+ endedAt,
333
+ duration,
334
+ attempts: attemptsCount,
335
+ attemptHistory,
336
+ error: lastErr ? new StepFailedError(`step "${step.name}" skip threw: ${lastErr.message}`, {
337
+ stepName: step.name,
338
+ attempts: attemptsCount,
339
+ cause: lastErr
340
+ }) : new StepFailedError(`step "${step.name}" failed`, {
341
+ stepName: step.name,
342
+ attempts: attemptsCount
343
+ }),
344
+ state
345
+ };
346
+ }
347
+ function failedAttempt(index, err, startedAt, startPerf) {
348
+ return {
349
+ index,
350
+ startedAt,
351
+ endedAt: (/* @__PURE__ */ new Date()).toISOString(),
352
+ duration: performance.now() - startPerf,
353
+ status: "failed",
354
+ error: toAIError(err)
355
+ };
356
+ }
357
+ function toAIError(err) {
358
+ if (err instanceof AIError) return err;
359
+ if (err instanceof Error) return new WorkflowError(err.message, { cause: err });
360
+ return new WorkflowError(String(err));
361
+ }
362
+ async function validateSchema(schema, value) {
363
+ if (!schema) return value;
364
+ const result = await schema["~standard"].validate(value);
365
+ if ("issues" in result && result.issues) throw new SchemaValidationError("workflow step output failed schema validation", { issues: result.issues });
366
+ return result.value;
367
+ }
368
+
369
+ //#endregion
370
+ export { executeStep, finalizeSnapshot, toAIError };
371
+ //# sourceMappingURL=step-runner.mjs.map