@leuduan/lagent 0.1.2 → 44.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 (353) hide show
  1. package/README.md +28 -15
  2. package/dist/chunks/{UC3FVVJ2.js → 2GCIWSOA.js} +2 -2
  3. package/dist/chunks/2P47AHNK.js +7 -0
  4. package/dist/chunks/3UMZMPWW.js +1 -0
  5. package/dist/chunks/5IRF47YB.js +1 -0
  6. package/dist/chunks/6DRCKZ2C.js +1 -0
  7. package/dist/chunks/6FRNHMCV.js +1 -0
  8. package/dist/chunks/7LI64H5X.js +17 -0
  9. package/dist/chunks/{SL2YLB2F.js → 7VAPNE3L.js} +1 -1
  10. package/dist/chunks/{BM3S37EZ.js → ASSQZH3F.js} +1 -1
  11. package/dist/chunks/B4FXK5ER.js +77 -0
  12. package/dist/chunks/{37YOGDGY.js → BBCW2FZK.js} +1 -1
  13. package/dist/chunks/BPRCULZH.js +1 -0
  14. package/dist/chunks/CANPZYTH.js +24 -0
  15. package/dist/chunks/CQEVREBT.js +1 -0
  16. package/dist/chunks/CUK7CGC7.js +1 -0
  17. package/dist/chunks/D4PQP5MA.js +3 -0
  18. package/dist/chunks/{CDQG7RKA.js → DDZ7YF26.js} +1 -1
  19. package/dist/chunks/EMRB4KCN.js +6 -0
  20. package/dist/chunks/EPYQ5M3B.js +3 -0
  21. package/dist/chunks/G44LG6VL.js +1 -0
  22. package/dist/chunks/GGSVIIWJ.js +2 -0
  23. package/dist/chunks/IAAEP6VL.js +30 -0
  24. package/dist/chunks/{AP6Q4EPT.js → JCTBX33H.js} +1 -1
  25. package/dist/chunks/JFX2MXBA.js +4 -0
  26. package/dist/chunks/JFZZXVWD.js +15 -0
  27. package/dist/chunks/KUZCL2SH.js +1 -0
  28. package/dist/chunks/MHC6VPB4.js +7 -0
  29. package/dist/chunks/MMNNZCBO.js +5 -0
  30. package/dist/chunks/{C5LYCXJG.js → MNJ4SRF7.js} +1 -1
  31. package/dist/chunks/N6IXG2VL.js +1 -0
  32. package/dist/chunks/OCBPT7KE.js +1 -0
  33. package/dist/chunks/OL5A6ITQ.js +1 -0
  34. package/dist/chunks/Q75FUN7D.js +2 -0
  35. package/dist/chunks/Q7S5LFGN.js +1 -0
  36. package/dist/chunks/RCIHV3KG.js +1 -0
  37. package/dist/chunks/S34ZA2R6.js +1 -0
  38. package/dist/chunks/SS3B5W34.js +12 -0
  39. package/dist/chunks/VYMT4TXD.js +1 -0
  40. package/dist/chunks/W57RW55W.js +1 -0
  41. package/dist/compositions/base.d.ts +11 -0
  42. package/dist/{presets → compositions}/codex.d.ts +12 -6
  43. package/dist/{presets/prompts.d.ts → compositions/codexPrompts.d.ts} +1 -1
  44. package/dist/context.d.ts +18 -10
  45. package/dist/index.d.ts +31 -23
  46. package/dist/index.js +1 -77
  47. package/dist/journal/catalog.d.ts +1 -1
  48. package/dist/journal/journal.d.ts +3 -3
  49. package/dist/journal/projection.d.ts +1 -1
  50. package/dist/journal/store.d.ts +3 -3
  51. package/dist/llm/index.d.ts +19 -13
  52. package/dist/llm/index.js +1 -0
  53. package/dist/llm/provider.d.ts +11 -0
  54. package/dist/loop/agent.d.ts +16 -132
  55. package/dist/loop/authorization.d.ts +41 -0
  56. package/dist/loop/auxiliary.d.ts +7 -3
  57. package/dist/loop/chunks.d.ts +3 -3
  58. package/dist/loop/completion.d.ts +17 -0
  59. package/dist/loop/errors.d.ts +6 -29
  60. package/dist/loop/hooks.d.ts +39 -6
  61. package/dist/loop/index.d.ts +7 -28
  62. package/dist/loop/runner.d.ts +134 -0
  63. package/dist/loop/runtime.d.ts +7 -16
  64. package/dist/loop/strategy.d.ts +12 -3
  65. package/dist/loop/{prepare.d.ts → surfacePrepare.d.ts} +2 -2
  66. package/dist/loop/tool.d.ts +11 -6
  67. package/dist/loop/toolCalls.d.ts +3 -10
  68. package/dist/plugin.d.ts +31 -34
  69. package/dist/plugin.js +1 -1
  70. package/dist/plugins/agentsMd.d.ts +24 -20
  71. package/dist/plugins/agentsMd.js +1 -1
  72. package/dist/plugins/ask/index.d.ts +2 -2
  73. package/dist/plugins/ask/index.js +1 -1
  74. package/dist/plugins/ask/service.d.ts +2 -2
  75. package/dist/plugins/commands/index.d.ts +5 -2
  76. package/dist/plugins/commands/index.js +1 -1
  77. package/dist/plugins/commands/output.d.ts +1 -1
  78. package/dist/plugins/commands/record.d.ts +31 -22
  79. package/dist/plugins/commands/service.d.ts +18 -11
  80. package/dist/plugins/commands/{background.d.ts → tools.d.ts} +2 -0
  81. package/dist/plugins/compaction/checkpoint.d.ts +4 -1
  82. package/dist/plugins/compaction/config.d.ts +3 -3
  83. package/dist/plugins/compaction/estimate.d.ts +9 -1
  84. package/dist/plugins/compaction/index.d.ts +87 -2
  85. package/dist/plugins/compaction/index.js +5 -5
  86. package/dist/plugins/compaction/{records.d.ts → record.d.ts} +36 -36
  87. package/dist/plugins/compaction/{log.d.ts → state.d.ts} +8 -1
  88. package/dist/plugins/dateAndBranch.js +1 -1
  89. package/dist/plugins/delegations/answers.d.ts +1 -1
  90. package/dist/plugins/delegations/assignmentKey.d.ts +1 -1
  91. package/dist/plugins/delegations/cleanup.d.ts +1 -1
  92. package/dist/plugins/delegations/index.js +1 -1
  93. package/dist/plugins/delegations/notices.d.ts +8 -8
  94. package/dist/plugins/delegations/obligations.d.ts +13 -13
  95. package/dist/plugins/delegations/record.d.ts +11 -10
  96. package/dist/plugins/delegations/request.d.ts +8 -5
  97. package/dist/plugins/delegations/role.d.ts +1 -1
  98. package/dist/plugins/delegations/service.d.ts +9 -8
  99. package/dist/plugins/delegations/{waitBook.d.ts → waitResolver.d.ts} +9 -9
  100. package/dist/plugins/diagnostics/digest.d.ts +1 -1
  101. package/dist/plugins/diagnostics/index.d.ts +1 -1
  102. package/dist/plugins/diagnostics/index.js +1 -1
  103. package/dist/plugins/fileTools/config.d.ts +10 -0
  104. package/dist/plugins/{tools → fileTools}/editFiles.d.ts +16 -27
  105. package/dist/plugins/fileTools/gitDiscovery.d.ts +9 -0
  106. package/dist/plugins/fileTools/index.d.ts +18 -0
  107. package/dist/plugins/fileTools/index.js +1 -0
  108. package/dist/plugins/fileTools/tools.d.ts +6 -0
  109. package/dist/plugins/historyRecall/history.d.ts +1 -1
  110. package/dist/plugins/historyRecall/index.js +1 -1
  111. package/dist/plugins/historyRecall/render.d.ts +1 -1
  112. package/dist/plugins/mcp/catalog.d.ts +1 -1
  113. package/dist/plugins/mcp/index.d.ts +2 -2
  114. package/dist/plugins/mcp/index.js +1 -1
  115. package/dist/plugins/mcp/mount.d.ts +2 -2
  116. package/dist/plugins/mcp/render.d.ts +1 -1
  117. package/dist/plugins/mcp/{tool.d.ts → tools.d.ts} +1 -1
  118. package/dist/plugins/{routeContext.d.ts → routeNotice.d.ts} +2 -2
  119. package/dist/plugins/routeNotice.js +1 -0
  120. package/dist/{sandbox → plugins/sandbox}/absent.d.ts +11 -10
  121. package/dist/{sandbox → plugins/sandbox}/index.d.ts +5 -2
  122. package/dist/plugins/sandbox/index.js +4 -0
  123. package/dist/{sandbox → plugins/sandbox}/mode.d.ts +5 -5
  124. package/dist/plugins/sandbox/policy.d.ts +163 -0
  125. package/dist/{sandbox → plugins/sandbox}/service.d.ts +23 -13
  126. package/dist/plugins/schedules/index.js +3 -3
  127. package/dist/plugins/schedules/record.d.ts +18 -18
  128. package/dist/plugins/schedules/service.d.ts +2 -2
  129. package/dist/plugins/skills/discovery.d.ts +38 -9
  130. package/dist/plugins/skills/index.d.ts +1 -1
  131. package/dist/plugins/skills/index.js +1 -1
  132. package/dist/plugins/subAgents/index.js +1 -1
  133. package/dist/plugins/subAgents/{fork.d.ts → tools.d.ts} +36 -1
  134. package/dist/plugins/threadDirs.d.ts +36 -0
  135. package/dist/plugins/threadDirs.js +1 -0
  136. package/dist/plugins/toolPolicy/index.d.ts +2 -2
  137. package/dist/plugins/toolPolicy/index.js +1 -1
  138. package/dist/plugins/truncateToolOutput.d.ts +14 -10
  139. package/dist/plugins/truncateToolOutput.js +6 -1
  140. package/dist/plugins/usage/index.d.ts +4 -2
  141. package/dist/plugins/usage/index.js +1 -1
  142. package/dist/plugins/usage/tracker.d.ts +1 -1
  143. package/dist/plugins/verify/index.d.ts +7 -55
  144. package/dist/plugins/verify/index.js +6 -1
  145. package/dist/plugins/verify/judgement.d.ts +16 -0
  146. package/dist/plugins/verify/record.d.ts +382 -0
  147. package/dist/plugins/verify/service.d.ts +7 -4
  148. package/dist/plugins/verify/state.d.ts +58 -0
  149. package/dist/plugins/webTools/index.d.ts +34 -0
  150. package/dist/plugins/webTools/index.js +1 -0
  151. package/dist/plugins/workflows/assignment.d.ts +6 -2
  152. package/dist/plugins/workflows/cleanup.d.ts +1 -1
  153. package/dist/plugins/workflows/definition.d.ts +2 -2
  154. package/dist/plugins/workflows/{bindings.d.ts → entryPlan.d.ts} +2 -2
  155. package/dist/plugins/workflows/{providers.d.ts → hostBindings.d.ts} +2 -2
  156. package/dist/plugins/workflows/index.d.ts +4 -4
  157. package/dist/plugins/workflows/index.js +10 -5
  158. package/dist/plugins/workflows/observations.d.ts +3 -3
  159. package/dist/plugins/workflows/participantModels.d.ts +28 -0
  160. package/dist/plugins/workflows/participants.d.ts +2 -2
  161. package/dist/plugins/workflows/policy.d.ts +2 -2
  162. package/dist/plugins/workflows/record.d.ts +51 -65
  163. package/dist/plugins/workflows/routing.d.ts +2 -2
  164. package/dist/plugins/workflows/service.d.ts +24 -24
  165. package/dist/plugins/workflows/{fold.d.ts → state.d.ts} +2 -2
  166. package/dist/plugins/workflows/stopCheck.d.ts +13 -0
  167. package/dist/plugins/workflows/tools.d.ts +13 -0
  168. package/dist/plugins/workflows/view.d.ts +2 -2
  169. package/dist/plugins/{workspaceContext.d.ts → workspaceNotice.d.ts} +5 -7
  170. package/dist/plugins/workspaceNotice.js +1 -0
  171. package/dist/{plugins/providers → providers}/anthropic.d.ts +6 -3
  172. package/dist/{plugins/providers → providers}/anthropicMessages.d.ts +2 -1
  173. package/dist/{plugins/providers → providers}/codex.d.ts +14 -3
  174. package/dist/{plugins/providers → providers}/deepseek.d.ts +6 -2
  175. package/dist/{plugins/providers → providers}/failure.d.ts +1 -1
  176. package/dist/{plugins/providers → providers}/gateway.d.ts +2 -1
  177. package/dist/providers/index.d.ts +18 -0
  178. package/dist/providers/index.js +6 -0
  179. package/dist/{plugins/providers → providers}/openaiChat.d.ts +13 -2
  180. package/dist/{plugins/providers → providers}/openaiResponses.d.ts +12 -3
  181. package/dist/{plugins/providers → providers}/opencodeCopilot.d.ts +2 -2
  182. package/dist/{plugins/providers → providers}/opencodeGo.d.ts +18 -7
  183. package/dist/{plugins/providers → providers}/shared.d.ts +9 -1
  184. package/dist/{plugins/providers → providers}/stream.d.ts +4 -2
  185. package/dist/{models/door.d.ts → records/index.d.ts} +8 -7
  186. package/dist/records/index.js +1 -0
  187. package/dist/{models/wire → records}/journal.d.ts +210 -14
  188. package/dist/{models/wire/primitives.d.ts → records/toolResult.d.ts} +1 -1
  189. package/dist/services/agents.d.ts +26 -1
  190. package/dist/services/confinement/files.d.ts +118 -0
  191. package/dist/services/confinement/service.d.ts +219 -0
  192. package/dist/services/core.d.ts +2 -8
  193. package/dist/{kit/extension.d.ts → services/extensionEvents.d.ts} +5 -4
  194. package/dist/services/journal.d.ts +1 -1
  195. package/dist/services/llm.d.ts +4 -2
  196. package/dist/{kit → services}/modes.d.ts +1 -1
  197. package/dist/services/plugins.d.ts +34 -0
  198. package/dist/services/{preset.d.ts → presets/definition.d.ts} +4 -4
  199. package/dist/services/{presets.d.ts → presets/installations.d.ts} +3 -3
  200. package/dist/services/prompts.d.ts +23 -5
  201. package/dist/services/{threadActivation.d.ts → threads/activation.d.ts} +3 -3
  202. package/dist/services/{admission.d.ts → threads/admission.d.ts} +3 -3
  203. package/dist/services/{birth.d.ts → threads/birth.d.ts} +9 -10
  204. package/dist/services/threads/index.d.ts +14 -0
  205. package/dist/services/{runEvents.d.ts → threads/runEvents.d.ts} +2 -2
  206. package/dist/services/{owner.d.ts → threads/runOwner.d.ts} +5 -4
  207. package/dist/services/{threads.d.ts → threads/service.d.ts} +9 -8
  208. package/dist/services/{thread.d.ts → threads/thread.d.ts} +8 -7
  209. package/dist/services/{threadView.d.ts → threads/view.d.ts} +7 -6
  210. package/dist/services/tools.d.ts +79 -2
  211. package/dist/{loop/opening.d.ts → state/birth.d.ts} +22 -2
  212. package/dist/{loop → state}/chain.d.ts +3 -3
  213. package/dist/state/confirmations.d.ts +64 -0
  214. package/dist/{loop → state}/continuation.d.ts +3 -2
  215. package/dist/{loop → state}/decide.d.ts +10 -2
  216. package/dist/state/dispatches.d.ts +54 -0
  217. package/dist/state/errors.d.ts +26 -0
  218. package/dist/{loop → state}/forks.d.ts +1 -1
  219. package/dist/{loop → state}/holds.d.ts +1 -1
  220. package/dist/{loop → state}/inbox.d.ts +2 -2
  221. package/dist/state/index.d.ts +31 -0
  222. package/dist/state/inputs.d.ts +58 -0
  223. package/dist/state/installations.d.ts +28 -0
  224. package/dist/state/prompt.d.ts +13 -0
  225. package/dist/state/recovery.d.ts +7 -0
  226. package/dist/{loop → state}/replace.d.ts +2 -2
  227. package/dist/{loop → state}/requests.d.ts +3 -3
  228. package/dist/{loop → state}/route.d.ts +10 -2
  229. package/dist/state/summary.d.ts +16 -0
  230. package/dist/{loop → state}/surface.d.ts +3 -3
  231. package/dist/state/surfaceRead.d.ts +20 -0
  232. package/dist/{loop/threadState.d.ts → state/thread.d.ts} +12 -11
  233. package/dist/state/turns.d.ts +87 -0
  234. package/dist/state/waits.d.ts +38 -0
  235. package/dist/{loop → state}/wake.d.ts +1 -1
  236. package/dist/stores/durableObject.js +1 -0
  237. package/dist/{plugins/store → stores}/memory.d.ts +2 -2
  238. package/dist/{plugins/store → stores}/sql.d.ts +2 -2
  239. package/dist/stores/sqlite.js +1 -0
  240. package/dist/sys/environment.d.ts +0 -5
  241. package/dist/sys/git.d.ts +0 -13
  242. package/dist/sys/paths.d.ts +0 -47
  243. package/dist/sys/spawn.d.ts +16 -6
  244. package/dist/testing/clock.d.ts +1 -1
  245. package/dist/testing/fakeLlm.d.ts +4 -2
  246. package/dist/testing/fedLlm.d.ts +3 -1
  247. package/dist/testing/helpers.d.ts +31 -13
  248. package/dist/testing/index.js +1 -1
  249. package/dist/testing/settlement.d.ts +1 -1
  250. package/package.json +36 -28
  251. package/dist/chunks/2O7ZGEIT.js +0 -1
  252. package/dist/chunks/2OSESBIL.js +0 -1
  253. package/dist/chunks/3LQHQRIU.js +0 -6
  254. package/dist/chunks/3QCW4WDJ.js +0 -1
  255. package/dist/chunks/4XVRUSCQ.js +0 -1
  256. package/dist/chunks/6ECI2GGV.js +0 -1
  257. package/dist/chunks/74HKUGRR.js +0 -1
  258. package/dist/chunks/ADHERZLW.js +0 -2
  259. package/dist/chunks/D7E6EWIE.js +0 -3
  260. package/dist/chunks/DRYAN665.js +0 -1
  261. package/dist/chunks/DXFXAYDN.js +0 -17
  262. package/dist/chunks/EGZI4XQX.js +0 -24
  263. package/dist/chunks/ENWFSU27.js +0 -41
  264. package/dist/chunks/G6RXC3AL.js +0 -1
  265. package/dist/chunks/G74AGRAJ.js +0 -15
  266. package/dist/chunks/GQGNVSKB.js +0 -1
  267. package/dist/chunks/IJ2WNMD5.js +0 -7
  268. package/dist/chunks/JPZFKSWT.js +0 -1
  269. package/dist/chunks/KESCTXGO.js +0 -5
  270. package/dist/chunks/L3N4FIEN.js +0 -1
  271. package/dist/chunks/NAOUTU7A.js +0 -4
  272. package/dist/chunks/NDIM7MEW.js +0 -1
  273. package/dist/chunks/O4PPOM5Z.js +0 -2
  274. package/dist/chunks/OVZFGILJ.js +0 -1
  275. package/dist/chunks/P5ZHHOOM.js +0 -6
  276. package/dist/chunks/QMMLQJBS.js +0 -1
  277. package/dist/chunks/RK3VP2Q4.js +0 -3
  278. package/dist/chunks/SGGMBSBA.js +0 -1
  279. package/dist/chunks/TOOIB6J2.js +0 -1
  280. package/dist/chunks/TYTQRLTX.js +0 -1
  281. package/dist/chunks/UCTEU62F.js +0 -1
  282. package/dist/chunks/UD3VJER6.js +0 -1
  283. package/dist/chunks/WUUKTSRT.js +0 -7
  284. package/dist/chunks/X65X4KP5.js +0 -7
  285. package/dist/chunks/YIR5MYGU.js +0 -1
  286. package/dist/chunks/YLNJXQ3Z.js +0 -1
  287. package/dist/kit/claim.d.ts +0 -13
  288. package/dist/kit/drain.d.ts +0 -10
  289. package/dist/kit/grants.d.ts +0 -9
  290. package/dist/kit/scope.d.ts +0 -32
  291. package/dist/kit/service.d.ts +0 -7
  292. package/dist/kit/strategy.d.ts +0 -10
  293. package/dist/kit/tool.d.ts +0 -72
  294. package/dist/llm/door.d.ts +0 -21
  295. package/dist/llm/door.js +0 -1
  296. package/dist/loop/projections.d.ts +0 -307
  297. package/dist/loop/state.d.ts +0 -70
  298. package/dist/loop/summary.d.ts +0 -3
  299. package/dist/models/config/agent.d.ts +0 -18
  300. package/dist/models/config/prompt.d.ts +0 -18
  301. package/dist/models/door.js +0 -1
  302. package/dist/models/index.d.ts +0 -7
  303. package/dist/plugins/commands/foreground.d.ts +0 -4
  304. package/dist/plugins/compaction/preset.d.ts +0 -86
  305. package/dist/plugins/compaction/shared.d.ts +0 -14
  306. package/dist/plugins/delegations/testing.d.ts +0 -16
  307. package/dist/plugins/providers/door.d.ts +0 -61
  308. package/dist/plugins/providers/door.js +0 -1
  309. package/dist/plugins/providers/index.d.ts +0 -6
  310. package/dist/plugins/routeContext.js +0 -1
  311. package/dist/plugins/store/durableObject.js +0 -1
  312. package/dist/plugins/store/sqlite.js +0 -1
  313. package/dist/plugins/subAgents/tool.d.ts +0 -38
  314. package/dist/plugins/tools/index.d.ts +0 -59
  315. package/dist/plugins/tools/index.js +0 -1
  316. package/dist/plugins/verify/facts.d.ts +0 -356
  317. package/dist/plugins/workflows/submit.d.ts +0 -8
  318. package/dist/plugins/workspaceContext.js +0 -1
  319. package/dist/presets/agents.d.ts +0 -7
  320. package/dist/presets/base.d.ts +0 -10
  321. package/dist/presets/index.d.ts +0 -4
  322. package/dist/sandbox/index.js +0 -4
  323. package/dist/sandbox/policy.d.ts +0 -86
  324. package/dist/services/launches.d.ts +0 -104
  325. package/dist/services/threadDirs.d.ts +0 -38
  326. package/dist/testing/sandbox.d.ts +0 -71
  327. /package/dist/chunks/{UKXMCHHT.js → FJLU4UH3.js} +0 -0
  328. /package/dist/chunks/{J5S4URAW.js → HK6ZSONF.js} +0 -0
  329. /package/dist/plugins/ask/{askQuestion.d.ts → tools.d.ts} +0 -0
  330. /package/dist/plugins/delegations/{waits.d.ts → waitContracts.d.ts} +0 -0
  331. /package/dist/plugins/{tools → fileTools}/fsRead.d.ts +0 -0
  332. /package/dist/plugins/{tools → fileTools}/ripgrep.d.ts +0 -0
  333. /package/dist/plugins/{tools → fileTools}/textEdit.d.ts +0 -0
  334. /package/dist/{util → plugins/mcp}/version.d.ts +0 -0
  335. /package/dist/{sandbox → plugins/sandbox}/client.d.ts +0 -0
  336. /package/dist/{sandbox → plugins/sandbox}/files.d.ts +0 -0
  337. /package/dist/{sandbox → plugins/sandbox}/protocol.d.ts +0 -0
  338. /package/dist/{sandbox → plugins/sandbox}/worker.d.ts +0 -0
  339. /package/dist/{sandbox → plugins/sandbox}/worker.js +0 -0
  340. /package/dist/plugins/skills/{tool.d.ts → tools.d.ts} +0 -0
  341. /package/dist/plugins/{tools → webTools}/html.d.ts +0 -0
  342. /package/dist/plugins/{tools → webTools}/network.d.ts +0 -0
  343. /package/dist/plugins/{tools → webTools}/web.d.ts +0 -0
  344. /package/dist/plugins/workflows/{work.d.ts → workStatus.d.ts} +0 -0
  345. /package/dist/{plugins/providers → providers}/codexAuth.d.ts +0 -0
  346. /package/dist/services/{contributions.d.ts → catalogContributions.d.ts} +0 -0
  347. /package/dist/{loop → state}/limits.d.ts +0 -0
  348. /package/dist/{plugins/store → stores}/durableObject.d.ts +0 -0
  349. /package/dist/{plugins/store → stores}/durableObjectConnection.d.ts +0 -0
  350. /package/dist/{plugins/store → stores}/sqlite.d.ts +0 -0
  351. /package/dist/{models → util}/jsonSchema.d.ts +0 -0
  352. /package/dist/{kit → util}/retries.d.ts +0 -0
  353. /package/dist/{kit → util}/timers.d.ts +0 -0
@@ -1,6 +1,11 @@
1
- import{e as Rn,f as An,m as le,q as Nn,r as He,s as ie}from"../../chunks/EGZI4XQX.js";import"../../chunks/UD3VJER6.js";import"../../chunks/SGGMBSBA.js";import"../../chunks/3QCW4WDJ.js";import"../../chunks/C5LYCXJG.js";import"../../chunks/J5S4URAW.js";import{d as Mn}from"../../chunks/G74AGRAJ.js";import{a as Tn}from"../../chunks/SL2YLB2F.js";import"../../chunks/JPZFKSWT.js";import"../../chunks/RK3VP2Q4.js";import"../../chunks/74HKUGRR.js";import"../../chunks/QMMLQJBS.js";import{a as Sn}from"../../chunks/YIR5MYGU.js";import"../../chunks/L3N4FIEN.js";import{a as _n,b as En}from"../../chunks/NAOUTU7A.js";import{a as Ke}from"../../chunks/UKXMCHHT.js";import{e as Ve}from"../../chunks/G6RXC3AL.js";import"../../chunks/GQGNVSKB.js";import"../../chunks/DXFXAYDN.js";import{b as te,e as qn}from"../../chunks/DRYAN665.js";import{d as xn}from"../../chunks/2OSESBIL.js";import{K as $n,X as On,a as ee,o as ne}from"../../chunks/YLNJXQ3Z.js";import{a as de,b as jn}from"../../chunks/DDANMY45.js";import{a as ue,d as U,e as N,f as Y,h as je}from"../../chunks/37YOGDGY.js";import{a as Be,i as yn,o as vn}from"../../chunks/KESCTXGO.js";import{e as kn}from"../../chunks/TYTQRLTX.js";import{k as Oe}from"../../chunks/D7E6EWIE.js";import{Service as Vi}from"@leuduan/cordis";import{z as y}from"zod";import{z as m}from"zod";var T="lagent:workflows",z=n=>n.requires.includes(T),P="submit_workflow_output",Pn="workflow_submission",x=m.string().min(1),Ft=m.discriminatedUnion("kind",[m.strictObject({kind:m.literal("input")}),m.strictObject({kind:m.literal("evidence"),provider:x,immutable:m.boolean(),detail:N}),m.strictObject({kind:m.literal("result"),result:x,participant:x.nullable(),decision:x.nullable(),mode:m.enum(["current","historical"])})]),xe=n=>n===null?null:n.kind==="evidence"?{...n,detail:Y(n.detail)}:{...n},Cn=m.strictObject({config:N}),zt=m.strictObject({participants:m.record(x,m.strictObject({configuration:m.strictObject({agent:x,route:kn,environment:vn,installations:m.array(yn)}).nullable(),checks:m.record(x,Cn)})),evidence:m.record(x,Cn)}),Ut=m.discriminatedUnion("context",[m.strictObject({context:m.literal("conversation"),id:x,participant:x,activation:x}),m.strictObject({context:m.literal("isolated"),id:x,participant:x,thread:x,task:x,generation:m.number().int().positive(),lifetime:m.enum(["instance","attempt"]),checks:m.array(x)})]),Jt=m.discriminatedUnion("kind",[m.strictObject({kind:m.literal("accepted"),submission:m.number().int().positive(),ending:m.number().int().positive(),output:N}),m.strictObject({kind:m.literal("ended"),settlement:m.enum(["failed","stopped","completed"]),detail:m.string(),candidate:m.strictObject({submission:m.number().int().positive(),output:N}).nullable(),report:m.string().nullable()}),m.strictObject({kind:m.literal("refused"),detail:m.string()}),m.strictObject({kind:m.literal("unfinished"),candidate:m.strictObject({submission:m.number().int().positive(),output:N}).nullable(),report:m.string().nullable()})]),Wn=m.strictObject({assignment:x,thread:x,task:x,generation:m.number().int().positive(),outcome:Jt}),In=m.discriminatedUnion("kind",[m.strictObject({kind:m.literal("opening")}),m.strictObject({kind:m.literal("routed"),result:x}),m.strictObject({kind:m.literal("decision"),decision:x})]),Ln=m.discriminatedUnion("op",[m.strictObject({op:m.literal("opened"),definition:x,canonical:m.record(m.string(),N),input:N,input_id:x,pins:zt}),m.strictObject({op:m.literal("entered"),transition:x,cause:In,step:x,attempt:x,ordinal:m.number().int().positive(),retires:m.array(x),inputs:m.record(x,Ft.nullable()),evidence:m.record(x,N),assignments:m.array(Ut)}),m.strictObject({op:m.literal("observed"),observations:m.array(Wn).min(1)}),m.strictObject({op:m.literal("stopped"),attempt:x,reason:m.string(),observations:m.array(Wn)}),m.strictObject({op:m.literal("completed"),transition:x,cause:In}),m.strictObject({op:m.literal("decided"),decision:x,operation_id:x,actor:m.string(),attempt:x,reason:m.enum(["gate","exhaustion","recovery"]),result:x.nullable(),verdict:m.enum(["approve","revise"]),option:x.nullable(),feedback:m.string().nullable(),restart:m.array(x)}).refine(n=>n.reason==="recovery"==(n.result===null),{message:"A recovery decision cites no result, and any other decision cites one"}),m.strictObject({op:m.literal("cancel-requested"),operation_id:x,actor:m.string(),reason:m.string()}),m.strictObject({op:m.literal("cancelled"),operation_id:x})]),L=te({plugin:"lagent:workflows",kind:"instance",version:6,policyState:!0,schema:Ln});function Dn(n,e,t,i){return Ln.parse({op:"opened",definition:n.id,canonical:n,input:e,input_id:t,pins:i})}var Fn=m.strictObject({assignment:x,output:N}),zn=n=>({[Pn]:Fn.parse(n)});function Se(n){if(n.name!==P||n.is_error||n.concludes_turn!==!0)return;let e=Fn.safeParse(n.metadata[Pn]);return e.success?e.data:void 0}var G=te({plugin:"lagent:workflows",kind:"reminder",version:1,schema:m.strictObject({assignment:x,epoch:m.number().int().positive()})}),Un=n=>encodeURIComponent(n),Jn=(n,e,t)=>`${n}#${Un(e)}:${t}`,Bn=(n,e)=>`${n}@${Un(e)}`,fe=(n,e)=>n.get(e)??1,Kn=n=>`${n}/result`,Qe=(n,e)=>`${n}/${e}`;function _e(n,e){switch(e.kind){case"opening":return`${n}#opening/next`;case"routed":return`${e.result}/next`;case"decision":return`${e.decision}/next`}}var Ee=n=>`${n}/input`,Vn=te({plugin:"lagent:workflows",kind:"transition",version:1,schema:m.strictObject({transition:x,assignment:x.nullable(),decision:x.nullable()})});var se=(n,e=new Set)=>Qn(n,t=>t.retired||e.has(t.revision));function Hn(n,e){let t=new Set;for(let i of n.attempts.values())if(i.seq<=e)for(let s of i.entered.retires)t.add(s);return Qn(n,i=>t.has(i.revision))}function Qn(n,e){let t=new Set;for(let i of n.results.values()){let s=n.attempts.get(i.attempt).entered.inputs,r=Object.values(s).some(o=>o?.kind==="result"&&o.mode==="current"&&t.has(o.result));(e(i)||r)&&t.add(i.revision)}return t}function qe(n,e){let t=e===null?void 0:n.decisions.get(e);if(t===void 0)return null;let{reason:i,verdict:s,option:r,feedback:o}=t;return{reason:i,verdict:s,option:r,feedback:o}}function Te(n,e,t){let i=e.inputs[t]??null;if(i===null)return null;if(i.kind==="input")return n.opened.input;if(i.kind==="evidence")return e.evidence[t];let s=n.results.get(i.result);return i.participant!==null?s.outputs[i.participant]:{outputs:s.outputs,decision:qe(n,i.decision)}}function Me(n,e,t){let{instance:i,step:s,cause:r}=t,o=e.steps[s];if(o===void 0)return{refused:`the definition has no step ${s}`};for(let{participant:v}of o.assignments)if(e.participants[v]?.context==="isolated"&&n.opened?.pins.participants[v]?.configuration==null)return{refused:`participant ${v} was not pinned when the instance opened: a new participant needs a new workflow id`};let c=(n.ordinals.get(s)??0)+1,a=Jn(i,s,c),u=_e(i,r),{evidence:d}=t,f=d?.attempt===a&&d.transition===u?d.inputs:new Map,l=Kt(n,s,se(n)),g=l===void 0?[]:[l.revision],p=se(n,new Set(g)),h={},w={},k=[];for(let[v,b]of Object.entries(o.inputs)){if(!("source"in b)){let{evidence:F,optional:Ue}=b,Q=f.get(v),Je=Dt=>({refused:`input ${v} (evidence ${F}): ${Dt}`});if(Q===void 0){k.push({input:v,provider:F});continue}if(Q.kind==="unavailable")return Je(`it could not be acquired: ${Q.reason}`);if(Q.kind==="absent"){if(!Ue)return Je("there is none");h[v]=null;continue}if(n.opened?.pins.evidence[F]===void 0)return Je("the instance recorded no pin for its provider");h[v]={kind:"evidence",provider:F,immutable:Q.immutable,detail:Q.detail},w[v]=Q.value;continue}if(b.source==="$input"){h[v]={kind:"input"};continue}let{step:j,participant:S=null}=b.source,A=F=>({refused:`input ${v} (${j}): ${F}`}),D=[...n.results.values()].filter(F=>F.step===j&&(S===null||Object.hasOwn(F.outputs,S))),C;if(b.mode==="historical")C=D.at(-1);else if(j!==s){if(C=D.filter(({revision:Ue})=>!p.has(Ue)).at(-1),C===void 0&&D.length>0)return A("its result is no longer current");let F=C===void 0?null:Bt(n,C,t.deciding);if(C!==void 0&&e.steps[j]?.gate===!0&&F?.verdict!=="approve")return A(`its current result ${C.revision} is not approved`)}if(C===void 0){if(!b.optional)return A("it has no accepted result");h[v]=null;continue}h[v]={kind:"result",result:C.revision,participant:S,decision:t.deciding?.result===C.revision?t.deciding.id:C.decision,mode:b.mode}}return k.length>0?{needs:{transition:u,attempt:a,step:s,inputs:k}}:{row:{op:"entered",transition:u,cause:r,step:s,attempt:a,ordinal:c,retires:g,inputs:h,evidence:w,assignments:o.assignments.map(({participant:v,checks:b})=>{let j=Bn(a,v),S=e.participants[v];if(S.context==="conversation")return{context:"conversation",id:j,participant:v,activation:t.activation};let A=fe(n.generations,v)+(t.deciding?.restart.includes(v)?1:0),D=T;return{context:"isolated",id:j,participant:v,thread:An({owner:D,instance:i,participant:v,attempt:S.lifetime==="attempt"?a:null,generation:A}),task:Rn({owner:D,instance:i,attempt:a,assignment:j,generation:A}),generation:A,lifetime:S.lifetime,checks:[...b]}})}}}var Bt=(n,e,t)=>t?.result===e.revision?t.decision:qe(n,e.decision);function Kt(n,e,t){return[...n.results.values()].filter(i=>i.step===e&&!t.has(i.revision)).at(-1)}function Ye(n,e,t){if(t===void 0||e===null)return!1;let i=n.calls.get(t),s=n.calls.get(e);return i!==void 0&&s!==void 0&&i>s}var Vt=()=>({instance:null,opened:null,attempts:new Map,current:null,results:new Map,decisions:new Map,operations:new Map,ordinals:new Map,counts:new Map,generations:new Map,observed:new Map,stop:null,cancellation:null,terminal:null,answer:ee.init(),calls:new Map,response:null,personAt:null}),_=n=>n.opened!==null&&n.terminal===null&&n.cancellation===null,q=je({key:"lagent:workflows/instance",onFork:"reset",init:Vt,apply(n,e){let t=L.read(e);if(t!==void 0)return ni(n,t,e),n;if(n.opened===null)return n;let i=n.answer;return n.answer=ee.apply(i,e),e.type==="inbox/spliced"&&Ht(n,e),e.type==="run/cause"&&Qt(n,e),e.type==="assistant/message"&&Xe(n,e),e.type==="tool/call"&&en(n,e),e.type==="input/message"&&(Yt(n,e),Gt(n,e)),e.type==="tool/result"&&Zt(n,e),Xt(n,i,e.seq),n}});function pe(n){return!_(n)||n.current===null?[]:n.current.assignments.filter(({state:e})=>e!=="accepted"&&e!=="superseded")}var Ae=n=>pe(n).filter(({context:e})=>e==="conversation");function Ge(n){return n.replacement===null?n.activation:n.replacement.queued.find(({person:e})=>e)?.id??null}var Ze=n=>Ae(n).filter(({state:e})=>e==="scheduled");function Ht(n,e){let{removed:t,inserted:i}=e.data;for(let s of Ze(n)){let{replacement:r}=s;if(r===null){t.includes(s.activation)&&(s.replacement={queued:[]});continue}let o=r.queued.filter(({id:c})=>!t.includes(c));r.queued.splice(0,r.queued.length,...o);for(let c of i)c.source===null&&r.queued.push({id:c.id,person:!1})}}function Qt(n,e){let{input_id:t,chain:i}=e.data;if(!(t===null||i!=="person"))for(let s of Ze(n)){let r=s.replacement?.queued.find(({id:o})=>o===t);r!==void 0&&(r.person=!0)}}function Yt(n,e){let{input_id:t}=e.data;if(t!==null){for(let i of Ze(n))if(Ge(i)===t)i.state="active",i.claimSeq=e.seq,i.replacement=null;else if(i.replacement!==null){let{queued:s}=i.replacement,r=s.findIndex(({id:o})=>o===t);r>=0&&s.splice(r,1)}}}function Xe(n,e){let{tool_calls:t}=e.data.response.message;e.data.interrupted||t.length===0||(n.response={seq:e.seq,ids:t.map(({call_id:i})=>i)},n.calls.clear())}function en(n,e){let{response:t}=n;if(t===null||!e.source_seqs.includes(t.seq))return;let i=t.ids.indexOf(e.data.call_id);i>=0&&n.calls.set(e.seq,i)}function Gt(n,e){if(e.data.source===null){n.personAt=e.seq;return}let t=G.readContribution(e);if(t===void 0)return;let i=Ae(n).find(({id:s})=>s===t.assignment);i!==void 0&&(i.reminded=t.epoch)}function Xn(n,e){return e.claimSeq===null?null:Math.max(e.claimSeq,n.personAt??0)}function Zt(n,e){let t=Se(e.data.result);if(t===void 0)return;let i=Ae(n).find(({id:s})=>s===t.assignment);i===void 0||i.state==="scheduled"||(i.state="candidate",i.candidate={output:t.output,seq:e.seq,call:e.source_seqs[0]??null},i.submitted=i.candidate)}function Xt(n,e,t){let{answer:i}=n;for(let s of Ae(n)){let{candidate:r}=s;if(r===null)continue;let o=i.status!=="none"&&i.seq===r.seq;o&&i.status==="pending"||(o&&i.status==="accepted"&&e.status==="pending"?(s.state="accepted",s.accepted=r,s.closed=t):s.state="active",s.candidate=null)}et(n)}function et(n){let e=n.current;if(e===null||e.result!==null||e.recovery!==null||!e.assignments.every(({state:i})=>i==="accepted"))return;let t=Math.max(...e.assignments.map(({accepted:i})=>i.seq));tt(n,e,t)}function Yn(n,e,t,i){let s=e.flatMap(a=>{let u=pe(n).find(({id:f})=>f===a.assignment);return u!==void 0&&u.context==="isolated"&&u.thread===a.thread&&u.task===a.task&&u.generation===a.generation?[{assignment:u,outcome:a.outcome}]:[]});for(let{assignment:a,outcome:u}of s)u.kind==="accepted"&&(a.state="accepted",a.accepted={output:u.output,seq:t,call:null},a.submitted=a.accepted,a.closed=t);let r=s.flatMap(({assignment:a,outcome:u})=>u.kind==="ended"||u.kind==="refused"?[{assignment:a,outcome:u}]:[]);if(r.length===0&&(et(n),i===null||!pe(n).some(({context:a})=>a==="isolated")))return;let o=new Map;for(let{assignment:a,outcome:u}of s)u.kind==="accepted"||u.kind==="refused"||(u.candidate!==null&&(a.submitted={output:u.candidate.output,seq:t,call:null}),u.report!==null&&o.set(a,u.report));let c=r.length>0?{cause:"failure",detail:r.map(({assignment:a,outcome:u})=>u.kind==="refused"?`${a.participant}: its task could not be started: ${u.detail}`:`${a.participant}: ${u.detail}`).join("; ")}:{cause:"stop",detail:`the user stopped it: ${i}`};nt(n,{...c,seq:t},a=>o.get(a)??null)}function ei(n,e){if(e.cause.kind!=="routed"||n.stop===null)return!1;let t=n.results.get(e.cause.result);return t!==void 0&&t.seq<=n.stop.seq}function nt(n,e,t){let i=n.current,s={},r={};for(let o of i.assignments){if(o.state==="accepted"||o.state==="superseded")continue;o.submitted!==null&&(s[o.participant]=o.submitted.output);let c=t(o);c!==null&&(r[o.participant]=c)}Re(n,e.seq),i.recovery={...e,candidates:s,reports:r,decision:null}}function tt(n,e,t){let i=Kn(e.entered.attempt);e.result=i,n.results.set(i,{revision:i,attempt:e.entered.attempt,step:e.entered.step,outputs:Object.fromEntries(e.assignments.map(({participant:s,accepted:r})=>[s,r.output])),seq:t,decision:null,routed:null,retired:!1})}function Re(n,e){for(let t of pe(n))t.state="superseded",t.candidate=null,t.closed=e}function Gn(n,e,t){let i=e.kind==="routed"?e.result:e.kind==="decision"?n.decisions.get(e.decision)?.result:void 0,s=i==null?void 0:n.results.get(i);s!==void 0&&(s.routed=t)}function Zn(n,e,t){let i=e.filter(({task:s,outcome:r})=>r.kind==="unfinished"||!n.observed.has(s));for(let s of i)s.outcome.kind!=="unfinished"&&n.observed.set(s.task,{...s,seq:t});return i}function ni(n,e,t){switch(e.op){case"opened":if(n.opened!==null)return;n.opened=e,n.instance=t.thread_id;return;case"entered":{if(!_(n)||n.attempts.has(e.attempt))return;Re(n,t.seq);for(let s of e.retires){let r=n.results.get(s);r!==void 0&&(r.retired=!0)}Gn(n,e.cause,e.transition),n.ordinals.set(e.step,e.ordinal),n.counts.set(e.step,(n.counts.get(e.step)??0)+1);let i={entered:e,seq:t.seq,assignments:e.assignments.map(s=>({id:s.id,participant:s.participant,context:s.context,...s.context==="conversation"?{thread:t.thread_id,task:null,generation:null,lifetime:null,checks:[],activation:s.activation}:{thread:s.thread,task:s.task,generation:s.generation,lifetime:s.lifetime,checks:s.checks,activation:s.task},state:"scheduled",replacement:null,claimSeq:null,candidate:null,submitted:null,accepted:null,reminded:null,closed:null})),result:null,recovery:null};if(n.attempts.set(e.attempt,i),n.current=i,i.assignments.length===0)tt(n,i,t.seq);else if(ei(n,e)&&i.assignments.some(({context:s})=>s==="isolated")){let s=`the user stopped it before its work began: ${n.stop.reason}`;nt(n,{cause:"stop",detail:s,seq:t.seq},()=>null)}return}case"observed":Yn(n,Zn(n,e.observations,t.seq),t.seq,null);return;case"stopped":if(!_(n)||n.current?.entered.attempt!==e.attempt)return;n.stop={seq:t.seq,reason:e.reason},Yn(n,Zn(n,e.observations,t.seq),t.seq,e.reason);return;case"completed":if(!_(n))return;Re(n,t.seq),Gn(n,e.cause,e.transition),n.terminal={kind:"completed",transition:e.transition};return;case"decided":{if(!_(n)||n.decisions.has(e.decision))return;if(e.result===null){let i=n.current?.entered.attempt===e.attempt?n.current.recovery:null;if(i===null||i.decision!==null)return;i.decision=e.decision}else{let i=n.results.get(e.result);if(i===void 0||i.decision!==null)return;i.decision=e.decision}n.decisions.set(e.decision,{...e,seq:t.seq}),n.operations.set(e.operation_id,e);for(let i of e.restart)n.generations.set(i,fe(n.generations,i)+1);n.counts.clear();return}case"cancel-requested":if(!_(n))return;Re(n,t.seq),n.cancellation=e,n.operations.set(e.operation_id,e);return;case"cancelled":n.terminal===null&&n.cancellation?.operation_id===e.operation_id&&(n.terminal={kind:"cancelled"});return}}function nn(n){let e=new Map;for(let t of n.attempts.values())for(let{participant:i,lifetime:s,thread:r,generation:o}of t.assignments)s==="instance"&&o===fe(n.generations,i)&&e.set(i,r);return e}function he(n){let e={};if(_(n)){for(let t of nn(n).keys())e[t]={context:"isolated",open:!1};for(let{participant:t,context:i}of pe(n))e[t]={context:i,open:!0}}return{id:n.opened.definition,step:_(n)?n.current?.entered.step??null:null,participants:e}}function me(n,e){let{id:t,step:i}=n;if(e===void 0||e.id!==t)return`definition ${t} is not registered`;let s=[],r=i===null?void 0:e.steps[i];i!==null&&r===void 0&&s.push(`its current step ${i} is gone`);for(let[o,{context:c,open:a}]of Object.entries(n.participants)){let u=a?"an open assignment":"a conversation of its own",d=e.participants[o];d===void 0?s.push(`participant ${o}, with ${u}, is gone`):d.context!==c?s.push(`participant ${o}, with ${u}, changed from ${c} to ${d.context}`):a&&r!==void 0&&!r.assignments.some(f=>f.participant===o)&&s.push(`step ${i} no longer assigns participant ${o}, whose assignment is open`)}return s.length===0?null:`definition ${t} no longer fits the instance: ${s.join("; ")}`}function it(n,e){if(n.opened!==null)return me(he(n),e)===null?e:void 0}function tn(n,e){return{outputs:(e.result===null?void 0:n.results.get(e.result))?.outputs??{},inputs:Object.fromEntries(Object.entries(e.entered.inputs).map(([i,s])=>[i,{value:Te(n,e.entered,i),citation:xe(s)}]))}}import{z as $}from"zod";import{z as W}from"zod";var ti=["title","description"],ii=["type","checks","error"],si={min_length:{props:["minimum"],sets:["minLength"]},max_length:{props:["maximum"],sets:["maxLength"]},length_equals:{props:["length"],sets:["minLength","maxLength"]},string_format:{props:["format","pattern"],sets:["pattern"]},greater_than:{props:["value","inclusive"],sets:["lower"]},less_than:{props:["value","inclusive"],sets:["upper"]},multiple_of:{props:["value"],sets:["multipleOf"]},number_format:{props:["format","type"],sets:["integer"]}},ri={string:["min_length","max_length","length_equals","string_format"],number:["greater_than","less_than","multiple_of","number_format"],array:["min_length","max_length","length_equals"]},ge=n=>n._zod.def,oi=W.string().min(0)._zod.def.checks[0]._zod.def.when,st=n=>n instanceof W.core.$ZodType,ai=n=>Number.isSafeInteger(n)&&n>=0;function rt(n,e){let t=[],i=[],s=(o,c)=>{if(!st(o)){t.push(`${c}: not a zod schema`);return}if(i.includes(o)){t.push(`${c}: a recursive schema`);return}i.push(o);try{return ci(o,c,s,t)}finally{i.pop()}},r=s(n,e);return{problems:t,schema:t.length===0?r:void 0}}function ci(n,e,t,i){let s=i.length,r=l=>{i.push(`${e}: ${l}`)},o={};for(let[l,g]of Object.entries(W.globalRegistry.get(n)??{}))ti.includes(l)?typeof g!="string"?r(`metadata "${l}" is not a string`):o[l]=g:r(`metadata "${l}" (only title and description are admitted)`);let c=ge(n),a=c.type,u=(...l)=>{for(let g of Object.keys(c))!ii.includes(g)&&!l.includes(g)&&r(`${String(a)} with "${g}"`)};if(c.coerce===!0){r("coercion");return}let d=ui(c,String(a),r),f=l=>{if(!(i.length>s||l===void 0))return Object.keys(o).length===0?l:l.meta(o)};switch(a){case"string":{u();let l=W.string();return d.minLength!==void 0&&(l=l.min(d.minLength)),d.maxLength!==void 0&&(l=l.max(d.maxLength)),d.pattern!==void 0&&(l=l.regex(new RegExp(d.pattern))),f(l)}case"number":{c.format==="safeint"&&c.check==="number_format"?(d.integer===!0&&r("the check number_format twice"),d.integer=!0,u("check","abort","format")):u();let l=d.integer===!0?W.int():W.number(),{lower:g,upper:p,multipleOf:h}=d;return g!==void 0&&(l=g.inclusive?l.gte(g.value):l.gt(g.value)),p!==void 0&&(l=p.inclusive?l.lte(p.value):l.lt(p.value)),h!==void 0&&(l=l.multipleOf(h)),f(l)}case"boolean":return u(),f(W.boolean());case"enum":{u("entries");let l=Object.entries(c.entries);if(!l.every(([,g])=>typeof g=="string"||typeof g=="number"&&Number.isFinite(g))){r("an enum with a value that is not a string or finite number");return}return f(W.enum(Object.fromEntries(l)))}case"literal":{u("values");let l=[...c.values];if(!l.every(g=>g===null||["string","boolean"].includes(typeof g)||typeof g=="number"&&Number.isFinite(g))){r("a literal that is not a JSON scalar");return}return f(W.literal(l))}case"object":{u("shape","catchall");let l=c.catchall;(!st(l)||ge(l).type!=="never")&&r("an object that is not strict (use z.strictObject: other objects strip or pass unknown keys)");let g={};for(let[p,h]of Object.entries(c.shape)){let w=t(h,`${e}.${p}`);w!==void 0&&(g[p]=w)}return f(W.strictObject(g))}case"array":{u("element");let l=t(c.element,`${e}[]`);if(l===void 0)return;let g=W.array(l);return d.minLength!==void 0&&(g=g.min(d.minLength)),d.maxLength!==void 0&&(g=g.max(d.maxLength)),f(g)}case"union":{u("options","discriminator","inclusive");let l=c.discriminator;c.inclusive===!1&&l===void 0&&r("an exclusive union");let g=c.options.map((w,k)=>t(w,`${e}|${k}`));if(g.some(w=>w===void 0)||i.length>s)return;let p=g;if(typeof l!="string")return f(W.union(p));let h=new Map;if(p.forEach((w,k)=>{let O=ge(w),v=O.type==="object"?O.shape[l]:void 0,b=v===void 0||!Object.hasOwn(O.shape,l)?void 0:ge(v),j=b?.type==="literal"?b.values:b?.type==="enum"?Object.values(b.entries):void 0;if(j===void 0){r(`branch ${k} of the discriminated union has no literal or enum ${l}`);return}for(let S of j){let A=h.get(S);A!==void 0?r(`branches ${A} and ${k} of the discriminated union share ${l} ${JSON.stringify(S)}`):h.set(S,k)}}),i.length>s)return;try{let w=W.discriminatedUnion(l,p);return w.safeParse(null),f(w)}catch(w){r(`a discriminated union zod refuses: ${w instanceof Error?w.message:String(w)}`);return}}case"nullable":case"optional":{u("innerType");let l=t(c.innerType,e);return f(l===void 0?void 0:a==="nullable"?l.nullable():l.optional())}default:r(`a ${String(a)} schema`);return}}function ui(n,e,t){let i={},s=new Set,r=ri[e]??[];for(let o of n.checks??[]){let c=o._zod.def,a=String(c.check),u=si[a];if(!r.includes(a)||u===void 0){t(a==="custom"?"a refinement":a==="overwrite"?"a normalizing check":`the check ${a}`);continue}for(let l of Object.keys(c))l==="when"&&c.when===oi||["check","error","abort",...u.props].includes(l)||t(`the check ${a} with "${l}"`);let d=u.sets.filter(l=>s.has(l));if(d.length>0){t(`a second check on ${d.join(" and ")} (${a}): one per node`);continue}for(let l of u.sets)s.add(l);let f=c.value;switch(a){case"min_length":case"max_length":case"length_equals":{let l=c[a==="min_length"?"minimum":a==="max_length"?"maximum":"length"];if(!ai(l)){t(`the check ${a} with ${String(l)}, not a non-negative integer`);break}a!=="max_length"&&(i.minLength=l),a!=="min_length"&&(i.maxLength=l);break}case"string_format":{let l=c.pattern;c.format!=="regex"||!(l instanceof RegExp)?t(`the string format ${String(c.format)}`):l.flags!==""?t(`a regex with flags "${l.flags}"`):i.pattern=l.source;break}case"greater_than":case"less_than":if(typeof f!="number"||!Number.isFinite(f)){t(`the check ${a} with ${String(f)}, not a finite number`);break}i[a==="greater_than"?"lower":"upper"]={value:f,inclusive:c.inclusive===!0};break;case"multiple_of":if(typeof f!="number"||!Number.isFinite(f)||f<=0){t(`the check multiple_of with ${String(f)}, not a positive finite number`);break}i.multipleOf=f;break;case"number_format":c.format!=="safeint"?t(`the number format ${String(c.format)}`):i.integer=!0;break}}return i}function re(n,e){if(e.length===0)return[n];let t=ge(n),[i,...s]=e;switch(t.type){case"nullable":case"optional":return re(t.innerType,e);case"union":return t.options.flatMap(r=>re(r,e));case"object":{let r=t.shape;return Object.hasOwn(r,i)?re(r[i],s):[]}case"array":return/^(0|[1-9]\d*)$/.test(i)?re(t.element,s):[];default:return[]}}var oe="$complete",J=class extends Error{workflow;problems;constructor(e,t){super(`Workflow ${e} is refused:
1
+ import{a as En,j as Ln,k as Dn,r as fe,v as Fn,w as He,x as re}from"../../chunks/CANPZYTH.js";import{c as Cn}from"../../chunks/VYMT4TXD.js";import"../../chunks/MNJ4SRF7.js";import{d as In}from"../../chunks/JFZZXVWD.js";import"../../chunks/N6IXG2VL.js";import"../../chunks/6DRCKZ2C.js";import"../../chunks/7LI64H5X.js";import"../../chunks/3UMZMPWW.js";import"../../chunks/34KW6VNQ.js";import"../../chunks/KUZCL2SH.js";import"../../chunks/CQEVREBT.js";import"../../chunks/HK6ZSONF.js";import{a as Pn}from"../../chunks/7VAPNE3L.js";import"../../chunks/EPYQ5M3B.js";import"../../chunks/5IRF47YB.js";import"../../chunks/OL5A6ITQ.js";import{b as An,c as Nn}from"../../chunks/JFX2MXBA.js";import{b as Mn,d as Rn}from"../../chunks/S34ZA2R6.js";import{b as se,e as Wn}from"../../chunks/6FRNHMCV.js";import"../../chunks/CUK7CGC7.js";import{a as te,s as ie,z as qn}from"../../chunks/BPRCULZH.js";import{a as Ve}from"../../chunks/FJLU4UH3.js";import{a as le,b as Tn}from"../../chunks/DDANMY45.js";import{c as U,d as A,e as G,g as ue,h as je}from"../../chunks/BBCW2FZK.js";import{a as H,b as jn,i as Sn,o as _n}from"../../chunks/MMNNZCBO.js";import{a as xn}from"../../chunks/G44LG6VL.js";import{f as xe}from"../../chunks/D4PQP5MA.js";import{Service as es}from"@leuduan/cordis";import{z as $}from"zod";import{z as m}from"zod";var T="lagent:workflows",F=n=>n.requires.includes(T),N="submit_workflow_output",Bn="workflow_submission",j=m.string().min(1),Yt=m.discriminatedUnion("kind",[m.strictObject({kind:m.literal("input")}),m.strictObject({kind:m.literal("evidence"),provider:j,immutable:m.boolean(),detail:A}),m.strictObject({kind:m.literal("result"),result:j,participant:j.nullable(),decision:j.nullable(),mode:m.enum(["current","historical"])})]),Se=n=>n===null?null:n.kind==="evidence"?{...n,detail:G(n.detail)}:{...n},zn=m.strictObject({config:A}),Gt=m.strictObject({participants:m.record(j,m.strictObject({configuration:m.strictObject({agent:j,route:xn,environment:_n,installations:m.array(Sn)}).nullable(),checks:m.record(j,zn)})),evidence:m.record(j,zn)}),Zt=m.discriminatedUnion("context",[m.strictObject({context:m.literal("conversation"),id:j,participant:j,activation:j}),m.strictObject({context:m.literal("isolated"),id:j,participant:j,thread:j,task:j,generation:m.number().int().positive(),lifetime:m.enum(["instance","attempt"]),checks:m.array(j)})]),Xt=m.discriminatedUnion("kind",[m.strictObject({kind:m.literal("accepted"),submission:m.number().int().positive(),ending:m.number().int().positive(),output:A}),m.strictObject({kind:m.literal("ended"),settlement:m.enum(["failed","stopped","completed"]),detail:m.string(),candidate:m.strictObject({submission:m.number().int().positive(),output:A}).nullable(),report:m.string().nullable()}),m.strictObject({kind:m.literal("refused"),detail:m.string()}),m.strictObject({kind:m.literal("unfinished"),candidate:m.strictObject({submission:m.number().int().positive(),output:A}).nullable(),report:m.string().nullable()})]),Un=m.strictObject({assignment:j,thread:j,task:j,generation:m.number().int().positive(),outcome:Xt}),Jn=m.discriminatedUnion("kind",[m.strictObject({kind:m.literal("opening")}),m.strictObject({kind:m.literal("routed"),result:j}),m.strictObject({kind:m.literal("decision"),decision:j})]),Kn=m.discriminatedUnion("op",[m.strictObject({op:m.literal("opened"),definition:j,canonical:m.record(m.string(),A),input:A,input_id:j,pins:Gt}),m.strictObject({op:m.literal("entered"),transition:j,cause:Jn,step:j,attempt:j,ordinal:m.number().int().positive(),retires:m.array(j),inputs:m.record(j,Yt.nullable()),evidence:m.record(j,A),assignments:m.array(Zt)}),m.strictObject({op:m.literal("observed"),observations:m.array(Un).min(1)}),m.strictObject({op:m.literal("stopped"),attempt:j,reason:m.string(),observations:m.array(Un)}),m.strictObject({op:m.literal("completed"),transition:j,cause:Jn}),m.strictObject({op:m.literal("decided"),decision:j,operation_id:j,actor:m.string(),attempt:j,reason:m.enum(["gate","exhaustion","recovery"]),result:j.nullable(),verdict:m.enum(["approve","revise"]),option:j.nullable(),feedback:m.string().nullable(),restart:m.array(j)}).refine(n=>n.reason==="recovery"==(n.result===null),{message:"A recovery decision cites no result, and any other decision cites one"}),m.strictObject({op:m.literal("cancel-requested"),operation_id:j,actor:m.string(),reason:m.string()}),m.strictObject({op:m.literal("cancelled"),operation_id:j})]),I=se({plugin:"lagent:workflows",kind:"instance",version:7,policyState:!0,schema:Kn});function Vn(n,e,t,i){return Kn.parse({op:"opened",definition:n.id,canonical:n,input:e,input_id:t,pins:i})}var Hn=m.strictObject({assignment:j,output:A}),Qn=n=>({[Bn]:Hn.parse(n)});function _e(n){if(n.name!==N||n.is_error||n.concludes_turn!==!0)return;let e=Hn.safeParse(n.metadata[Bn]);return e.success?e.data:void 0}var Z=se({plugin:"lagent:workflows",kind:"reminder",version:1,schema:m.strictObject({assignment:j,epoch:m.number().int().positive()})}),Yn=n=>encodeURIComponent(n),Gn=(n,e,t)=>`${n}#${Yn(e)}:${t}`,Zn=(n,e)=>`${n}@${Yn(e)}`,pe=(n,e)=>n.get(e)??1,Xn=n=>`${n}/result`,Qe=(n,e)=>`${n}/${e}`;function Ee(n,e){switch(e.kind){case"opening":return`${n}#opening/next`;case"routed":return`${e.result}/next`;case"decision":return`${e.decision}/next`}}var qe=n=>`${n}/input`,et=se({plugin:"lagent:workflows",kind:"transition",version:1,schema:m.strictObject({transition:j,assignment:j.nullable(),decision:j.nullable()})});var oe=(n,e=new Set)=>tt(n,t=>t.retired||e.has(t.revision));function nt(n,e){let t=new Set;for(let i of n.attempts.values())if(i.seq<=e)for(let s of i.entered.retires)t.add(s);return tt(n,i=>t.has(i.revision))}function tt(n,e){let t=new Set;for(let i of n.results.values()){let s=n.attempts.get(i.attempt).entered.inputs,r=Object.values(s).some(o=>o?.kind==="result"&&o.mode==="current"&&t.has(o.result));(e(i)||r)&&t.add(i.revision)}return t}function Te(n,e){let t=e===null?void 0:n.decisions.get(e);if(t===void 0)return null;let{reason:i,verdict:s,option:r,feedback:o}=t;return{reason:i,verdict:s,option:r,feedback:o}}function Me(n,e,t){let i=e.inputs[t]??null;if(i===null)return null;if(i.kind==="input")return n.opened.input;if(i.kind==="evidence")return e.evidence[t];let s=n.results.get(i.result);return i.participant!==null?s.outputs[i.participant]:{outputs:s.outputs,decision:Te(n,i.decision)}}function Re(n,e,t){let{instance:i,step:s,cause:r}=t,o=e.steps[s];if(o===void 0)return{refused:`the definition has no step ${s}`};for(let{participant:b}of o.assignments)if(e.participants[b]?.context==="isolated"&&n.opened?.pins.participants[b]?.configuration==null)return{refused:`participant ${b} was not pinned when the instance opened: a new participant needs a new workflow id`};let a=(n.ordinals.get(s)??0)+1,c=Gn(i,s,a),d=Ee(i,r),{evidence:u}=t,f=u?.attempt===c&&u.transition===d?u.inputs:new Map,l=ni(n,s,oe(n)),w=l===void 0?[]:[l.revision],p=oe(n,new Set(w)),h={},g={},O=[];for(let[b,k]of Object.entries(o.inputs)){if(!("source"in k)){let{evidence:D,optional:Be}=k,Y=f.get(b),Ke=Qt=>({refused:`input ${b} (evidence ${D}): ${Qt}`});if(Y===void 0){O.push({input:b,provider:D});continue}if(Y.kind==="unavailable")return Ke(`it could not be acquired: ${Y.reason}`);if(Y.kind==="absent"){if(!Be)return Ke("there is none");h[b]=null;continue}if(n.opened?.pins.evidence[D]===void 0)return Ke("the instance recorded no pin for its provider");h[b]={kind:"evidence",provider:D,immutable:Y.immutable,detail:Y.detail},g[b]=Y.value;continue}if(k.source==="$input"){h[b]={kind:"input"};continue}let{step:x,participant:S=null}=k.source,C=D=>({refused:`input ${b} (${x}): ${D}`}),L=[...n.results.values()].filter(D=>D.step===x&&(S===null||Object.hasOwn(D.outputs,S))),W;if(k.mode==="historical")W=L.at(-1);else if(x!==s){if(W=L.filter(({revision:Be})=>!p.has(Be)).at(-1),W===void 0&&L.length>0)return C("its result is no longer current");let D=W===void 0?null:ei(n,W,t.deciding);if(W!==void 0&&e.steps[x]?.gate===!0&&D?.verdict!=="approve")return C(`its current result ${W.revision} is not approved`)}if(W===void 0){if(!k.optional)return C("it has no accepted result");h[b]=null;continue}h[b]={kind:"result",result:W.revision,participant:S,decision:t.deciding?.result===W.revision?t.deciding.id:W.decision,mode:k.mode}}return O.length>0?{needs:{transition:d,attempt:c,step:s,inputs:O}}:{row:{op:"entered",transition:d,cause:r,step:s,attempt:c,ordinal:a,retires:w,inputs:h,evidence:g,assignments:o.assignments.map(({participant:b,checks:k})=>{let x=Zn(c,b),S=e.participants[b];if(S.context==="conversation")return{context:"conversation",id:x,participant:b,activation:t.activation};let C=pe(n.generations,b)+(t.deciding?.restart.includes(b)?1:0),L=T;return{context:"isolated",id:x,participant:b,thread:Dn({owner:L,instance:i,participant:b,attempt:S.lifetime==="attempt"?c:null,generation:C}),task:Ln({owner:L,instance:i,attempt:c,assignment:x,generation:C}),generation:C,lifetime:S.lifetime,checks:[...k]}})}}}var ei=(n,e,t)=>t?.result===e.revision?t.decision:Te(n,e.decision);function ni(n,e,t){return[...n.results.values()].filter(i=>i.step===e&&!t.has(i.revision)).at(-1)}function Ye(n,e,t){if(t===void 0||e===null)return!1;let i=n.calls.get(t),s=n.calls.get(e);return i!==void 0&&s!==void 0&&i>s}var ti=()=>({instance:null,opened:null,attempts:new Map,current:null,results:new Map,decisions:new Map,operations:new Map,ordinals:new Map,counts:new Map,generations:new Map,observed:new Map,stop:null,cancellation:null,terminal:null,answer:te.init(),calls:new Map,response:null,personAt:null}),_=n=>n.opened!==null&&n.terminal===null&&n.cancellation===null,q=je({key:"lagent:workflows/instance",onFork:"reset",init:ti,apply(n,e){let t=I.read(e);if(t!==void 0)return ui(n,t,e),n;if(n.opened===null)return n;let i=n.answer;return n.answer=te.apply(i,e),e.type==="inbox/spliced"&&ii(n,e),e.type==="run/cause"&&si(n,e),e.type==="assistant/message"&&Xe(n,e),e.type==="tool/call"&&en(n,e),e.type==="input/message"&&(ri(n,e),oi(n,e)),e.type==="tool/result"&&ai(n,e),ci(n,i,e.seq),n}});function he(n){return!_(n)||n.current===null?[]:n.current.assignments.filter(({state:e})=>e!=="accepted"&&e!=="superseded")}var Ae=n=>he(n).filter(({context:e})=>e==="conversation");function Ge(n){return n.replacement===null?n.activation:n.replacement.queued.find(({person:e})=>e)?.id??null}var Ze=n=>Ae(n).filter(({state:e})=>e==="scheduled");function ii(n,e){let{removed:t,inserted:i}=e.data;for(let s of Ze(n)){let{replacement:r}=s;if(r===null){t.includes(s.activation)&&(s.replacement={queued:[]});continue}let o=r.queued.filter(({id:a})=>!t.includes(a));r.queued.splice(0,r.queued.length,...o);for(let a of i)a.source===null&&r.queued.push({id:a.id,person:!1})}}function si(n,e){let{input_id:t,chain:i}=e.data;if(!(t===null||i!=="person"))for(let s of Ze(n)){let r=s.replacement?.queued.find(({id:o})=>o===t);r!==void 0&&(r.person=!0)}}function ri(n,e){let{input_id:t}=e.data;if(t!==null){for(let i of Ze(n))if(Ge(i)===t)i.state="active",i.claimSeq=e.seq,i.replacement=null;else if(i.replacement!==null){let{queued:s}=i.replacement,r=s.findIndex(({id:o})=>o===t);r>=0&&s.splice(r,1)}}}function Xe(n,e){let{tool_calls:t}=e.data.response.message;e.data.interrupted||t.length===0||(n.response={seq:e.seq,ids:t.map(({call_id:i})=>i)},n.calls.clear())}function en(n,e){let{response:t}=n;if(t===null||!e.source_seqs.includes(t.seq))return;let i=t.ids.indexOf(e.data.call_id);i>=0&&n.calls.set(e.seq,i)}function oi(n,e){if(e.data.source===null){n.personAt=e.seq;return}let t=Z.readContribution(e);if(t===void 0)return;let i=Ae(n).find(({id:s})=>s===t.assignment);i!==void 0&&(i.reminded=t.epoch)}function ot(n,e){return e.claimSeq===null?null:Math.max(e.claimSeq,n.personAt??0)}function ai(n,e){let t=_e(e.data.result);if(t===void 0)return;let i=Ae(n).find(({id:s})=>s===t.assignment);i===void 0||i.state==="scheduled"||(i.state="candidate",i.candidate={output:t.output,seq:e.seq,call:e.source_seqs[0]??null},i.submitted=i.candidate)}function ci(n,e,t){let{answer:i}=n;for(let s of Ae(n)){let{candidate:r}=s;if(r===null)continue;let o=i.status!=="none"&&i.seq===r.seq;o&&i.status==="pending"||(o&&i.status==="accepted"&&e.status==="pending"?(s.state="accepted",s.accepted=r,s.closed=t):s.state="active",s.candidate=null)}at(n)}function at(n){let e=n.current;if(e===null||e.result!==null||e.recovery!==null||!e.assignments.every(({state:i})=>i==="accepted"))return;let t=Math.max(...e.assignments.map(({accepted:i})=>i.seq));dt(n,e,t)}function it(n,e,t,i){let s=e.flatMap(c=>{let d=he(n).find(({id:f})=>f===c.assignment);return d!==void 0&&d.context==="isolated"&&d.thread===c.thread&&d.task===c.task&&d.generation===c.generation?[{assignment:d,outcome:c.outcome}]:[]});for(let{assignment:c,outcome:d}of s)d.kind==="accepted"&&(c.state="accepted",c.accepted={output:d.output,seq:t,call:null},c.submitted=c.accepted,c.closed=t);let r=s.flatMap(({assignment:c,outcome:d})=>d.kind==="ended"||d.kind==="refused"?[{assignment:c,outcome:d}]:[]);if(r.length===0&&(at(n),i===null||!he(n).some(({context:c})=>c==="isolated")))return;let o=new Map;for(let{assignment:c,outcome:d}of s)d.kind==="accepted"||d.kind==="refused"||(d.candidate!==null&&(c.submitted={output:d.candidate.output,seq:t,call:null}),d.report!==null&&o.set(c,d.report));let a=r.length>0?{cause:"failure",detail:r.map(({assignment:c,outcome:d})=>d.kind==="refused"?`${c.participant}: its task could not be started: ${d.detail}`:`${c.participant}: ${d.detail}`).join("; ")}:{cause:"stop",detail:`the user stopped it: ${i}`};ct(n,{...a,seq:t},c=>o.get(c)??null)}function di(n,e){if(e.cause.kind!=="routed"||n.stop===null)return!1;let t=n.results.get(e.cause.result);return t!==void 0&&t.seq<=n.stop.seq}function ct(n,e,t){let i=n.current,s={},r={};for(let o of i.assignments){if(o.state==="accepted"||o.state==="superseded")continue;o.submitted!==null&&(s[o.participant]=o.submitted.output);let a=t(o);a!==null&&(r[o.participant]=a)}Ce(n,e.seq),i.recovery={...e,candidates:s,reports:r,decision:null}}function dt(n,e,t){let i=Xn(e.entered.attempt);e.result=i,n.results.set(i,{revision:i,attempt:e.entered.attempt,step:e.entered.step,outputs:Object.fromEntries(e.assignments.map(({participant:s,accepted:r})=>[s,r.output])),seq:t,decision:null,routed:null,retired:!1})}function Ce(n,e){for(let t of he(n))t.state="superseded",t.candidate=null,t.closed=e}function st(n,e,t){let i=e.kind==="routed"?e.result:e.kind==="decision"?n.decisions.get(e.decision)?.result:void 0,s=i==null?void 0:n.results.get(i);s!==void 0&&(s.routed=t)}function rt(n,e,t){let i=e.filter(({task:s,outcome:r})=>r.kind==="unfinished"||!n.observed.has(s));for(let s of i)s.outcome.kind!=="unfinished"&&n.observed.set(s.task,{...s,seq:t});return i}function ui(n,e,t){switch(e.op){case"opened":if(n.opened!==null)return;n.opened=e,n.instance=t.thread_id;return;case"entered":{if(!_(n)||n.attempts.has(e.attempt))return;Ce(n,t.seq);for(let s of e.retires){let r=n.results.get(s);r!==void 0&&(r.retired=!0)}st(n,e.cause,e.transition),n.ordinals.set(e.step,e.ordinal),n.counts.set(e.step,(n.counts.get(e.step)??0)+1);let i={entered:e,seq:t.seq,assignments:e.assignments.map(s=>({id:s.id,participant:s.participant,context:s.context,...s.context==="conversation"?{thread:t.thread_id,task:null,generation:null,lifetime:null,checks:[],activation:s.activation}:{thread:s.thread,task:s.task,generation:s.generation,lifetime:s.lifetime,checks:s.checks,activation:s.task},state:"scheduled",replacement:null,claimSeq:null,candidate:null,submitted:null,accepted:null,reminded:null,closed:null})),result:null,recovery:null};if(n.attempts.set(e.attempt,i),n.current=i,i.assignments.length===0)dt(n,i,t.seq);else if(di(n,e)&&i.assignments.some(({context:s})=>s==="isolated")){let s=`the user stopped it before its work began: ${n.stop.reason}`;ct(n,{cause:"stop",detail:s,seq:t.seq},()=>null)}return}case"observed":it(n,rt(n,e.observations,t.seq),t.seq,null);return;case"stopped":if(!_(n)||n.current?.entered.attempt!==e.attempt)return;n.stop={seq:t.seq,reason:e.reason},it(n,rt(n,e.observations,t.seq),t.seq,e.reason);return;case"completed":if(!_(n))return;Ce(n,t.seq),st(n,e.cause,e.transition),n.terminal={kind:"completed",transition:e.transition};return;case"decided":{if(!_(n)||n.decisions.has(e.decision))return;if(e.result===null){let i=n.current?.entered.attempt===e.attempt?n.current.recovery:null;if(i===null||i.decision!==null)return;i.decision=e.decision}else{let i=n.results.get(e.result);if(i===void 0||i.decision!==null)return;i.decision=e.decision}n.decisions.set(e.decision,{...e,seq:t.seq}),n.operations.set(e.operation_id,e);for(let i of e.restart)n.generations.set(i,pe(n.generations,i)+1);n.counts.clear();return}case"cancel-requested":if(!_(n))return;Ce(n,t.seq),n.cancellation=e,n.operations.set(e.operation_id,e);return;case"cancelled":n.terminal===null&&n.cancellation?.operation_id===e.operation_id&&(n.terminal={kind:"cancelled"});return}}function nn(n){let e=new Map;for(let t of n.attempts.values())for(let{participant:i,lifetime:s,thread:r,generation:o}of t.assignments)s==="instance"&&o===pe(n.generations,i)&&e.set(i,r);return e}function me(n){let e={};if(_(n)){for(let t of nn(n).keys())e[t]={context:"isolated",open:!1};for(let{participant:t,context:i}of he(n))e[t]={context:i,open:!0}}return{id:n.opened.definition,step:_(n)?n.current?.entered.step??null:null,participants:e}}function ge(n,e){let{id:t,step:i}=n;if(e===void 0||e.id!==t)return`definition ${t} is not registered`;let s=[],r=i===null?void 0:e.steps[i];i!==null&&r===void 0&&s.push(`its current step ${i} is gone`);for(let[o,{context:a,open:c}]of Object.entries(n.participants)){let d=c?"an open assignment":"a conversation of its own",u=e.participants[o];u===void 0?s.push(`participant ${o}, with ${d}, is gone`):u.context!==a?s.push(`participant ${o}, with ${d}, changed from ${a} to ${u.context}`):c&&r!==void 0&&!r.assignments.some(f=>f.participant===o)&&s.push(`step ${i} no longer assigns participant ${o}, whose assignment is open`)}return s.length===0?null:`definition ${t} no longer fits the instance: ${s.join("; ")}`}function ut(n,e){if(n.opened!==null)return ge(me(n),e)===null?e:void 0}function tn(n,e){return{outputs:(e.result===null?void 0:n.results.get(e.result))?.outputs??{},inputs:Object.fromEntries(Object.entries(e.entered.inputs).map(([i,s])=>[i,{value:Me(n,e.entered,i),citation:Se(s)}]))}}import{z as v}from"zod";import{z as P}from"zod";var li=["title","description"],fi=["type","checks","error"],pi={min_length:{props:["minimum"],sets:["minLength"]},max_length:{props:["maximum"],sets:["maxLength"]},length_equals:{props:["length"],sets:["minLength","maxLength"]},string_format:{props:["format","pattern"],sets:["pattern"]},greater_than:{props:["value","inclusive"],sets:["lower"]},less_than:{props:["value","inclusive"],sets:["upper"]},multiple_of:{props:["value"],sets:["multipleOf"]},number_format:{props:["format","type"],sets:["integer"]}},hi={string:["min_length","max_length","length_equals","string_format"],number:["greater_than","less_than","multiple_of","number_format"],array:["min_length","max_length","length_equals"]},we=n=>n._zod.def,mi=P.string().min(0)._zod.def.checks[0]._zod.def.when,lt=n=>n instanceof P.core.$ZodType,gi=n=>Number.isSafeInteger(n)&&n>=0;function ft(n,e){let t=[],i=[],s=(o,a)=>{if(!lt(o)){t.push(`${a}: not a zod schema`);return}if(i.includes(o)){t.push(`${a}: a recursive schema`);return}i.push(o);try{return wi(o,a,s,t)}finally{i.pop()}},r=s(n,e);return{problems:t,schema:t.length===0?r:void 0}}function wi(n,e,t,i){let s=i.length,r=l=>{i.push(`${e}: ${l}`)},o={};for(let[l,w]of Object.entries(P.globalRegistry.get(n)??{}))li.includes(l)?typeof w!="string"?r(`metadata "${l}" is not a string`):o[l]=w:r(`metadata "${l}" (only title and description are admitted)`);let a=we(n),c=a.type,d=(...l)=>{for(let w of Object.keys(a))!fi.includes(w)&&!l.includes(w)&&r(`${String(c)} with "${w}"`)};if(a.coerce===!0){r("coercion");return}let u=bi(a,String(c),r),f=l=>{if(!(i.length>s||l===void 0))return Object.keys(o).length===0?l:l.meta(o)};switch(c){case"string":{d();let l=P.string();return u.minLength!==void 0&&(l=l.min(u.minLength)),u.maxLength!==void 0&&(l=l.max(u.maxLength)),u.pattern!==void 0&&(l=l.regex(new RegExp(u.pattern))),f(l)}case"number":{a.format==="safeint"&&a.check==="number_format"?(u.integer===!0&&r("the check number_format twice"),u.integer=!0,d("check","abort","format")):d();let l=u.integer===!0?P.int():P.number(),{lower:w,upper:p,multipleOf:h}=u;return w!==void 0&&(l=w.inclusive?l.gte(w.value):l.gt(w.value)),p!==void 0&&(l=p.inclusive?l.lte(p.value):l.lt(p.value)),h!==void 0&&(l=l.multipleOf(h)),f(l)}case"boolean":return d(),f(P.boolean());case"enum":{d("entries");let l=Object.entries(a.entries);if(!l.every(([,w])=>typeof w=="string"||typeof w=="number"&&Number.isFinite(w))){r("an enum with a value that is not a string or finite number");return}return f(P.enum(Object.fromEntries(l)))}case"literal":{d("values");let l=[...a.values];if(!l.every(w=>w===null||["string","boolean"].includes(typeof w)||typeof w=="number"&&Number.isFinite(w))){r("a literal that is not a JSON scalar");return}return f(P.literal(l))}case"object":{d("shape","catchall");let l=a.catchall;(!lt(l)||we(l).type!=="never")&&r("an object that is not strict (use z.strictObject: other objects strip or pass unknown keys)");let w={};for(let[p,h]of Object.entries(a.shape)){let g=t(h,`${e}.${p}`);g!==void 0&&(w[p]=g)}return f(P.strictObject(w))}case"array":{d("element");let l=t(a.element,`${e}[]`);if(l===void 0)return;let w=P.array(l);return u.minLength!==void 0&&(w=w.min(u.minLength)),u.maxLength!==void 0&&(w=w.max(u.maxLength)),f(w)}case"union":{d("options","discriminator","inclusive");let l=a.discriminator;a.inclusive===!1&&l===void 0&&r("an exclusive union");let w=a.options.map((g,O)=>t(g,`${e}|${O}`));if(w.some(g=>g===void 0)||i.length>s)return;let p=w;if(typeof l!="string")return f(P.union(p));let h=new Map;if(p.forEach((g,O)=>{let y=we(g),b=y.type==="object"?y.shape[l]:void 0,k=b===void 0||!Object.hasOwn(y.shape,l)?void 0:we(b),x=k?.type==="literal"?k.values:k?.type==="enum"?Object.values(k.entries):void 0;if(x===void 0){r(`branch ${O} of the discriminated union has no literal or enum ${l}`);return}for(let S of x){let C=h.get(S);C!==void 0?r(`branches ${C} and ${O} of the discriminated union share ${l} ${JSON.stringify(S)}`):h.set(S,O)}}),i.length>s)return;try{let g=P.discriminatedUnion(l,p);return g.safeParse(null),f(g)}catch(g){r(`a discriminated union zod refuses: ${g instanceof Error?g.message:String(g)}`);return}}case"nullable":case"optional":{d("innerType");let l=t(a.innerType,e);return f(l===void 0?void 0:c==="nullable"?l.nullable():l.optional())}default:r(`a ${String(c)} schema`);return}}function bi(n,e,t){let i={},s=new Set,r=hi[e]??[];for(let o of n.checks??[]){let a=o._zod.def,c=String(a.check),d=pi[c];if(!r.includes(c)||d===void 0){t(c==="custom"?"a refinement":c==="overwrite"?"a normalizing check":`the check ${c}`);continue}for(let l of Object.keys(a))l==="when"&&a.when===mi||["check","error","abort",...d.props].includes(l)||t(`the check ${c} with "${l}"`);let u=d.sets.filter(l=>s.has(l));if(u.length>0){t(`a second check on ${u.join(" and ")} (${c}): one per node`);continue}for(let l of d.sets)s.add(l);let f=a.value;switch(c){case"min_length":case"max_length":case"length_equals":{let l=a[c==="min_length"?"minimum":c==="max_length"?"maximum":"length"];if(!gi(l)){t(`the check ${c} with ${String(l)}, not a non-negative integer`);break}c!=="max_length"&&(i.minLength=l),c!=="min_length"&&(i.maxLength=l);break}case"string_format":{let l=a.pattern;a.format!=="regex"||!(l instanceof RegExp)?t(`the string format ${String(a.format)}`):l.flags!==""?t(`a regex with flags "${l.flags}"`):i.pattern=l.source;break}case"greater_than":case"less_than":if(typeof f!="number"||!Number.isFinite(f)){t(`the check ${c} with ${String(f)}, not a finite number`);break}i[c==="greater_than"?"lower":"upper"]={value:f,inclusive:a.inclusive===!0};break;case"multiple_of":if(typeof f!="number"||!Number.isFinite(f)||f<=0){t(`the check multiple_of with ${String(f)}, not a positive finite number`);break}i.multipleOf=f;break;case"number_format":a.format!=="safeint"?t(`the number format ${String(a.format)}`):i.integer=!0;break}}return i}function ae(n,e){if(e.length===0)return[n];let t=we(n),[i,...s]=e;switch(t.type){case"nullable":case"optional":return ae(t.innerType,e);case"union":return t.options.flatMap(r=>ae(r,e));case"object":{let r=t.shape;return Object.hasOwn(r,i)?ae(r[i],s):[]}case"array":return/^(0|[1-9]\d*)$/.test(i)?ae(t.element,s):[];default:return[]}}var ce="$complete",J=class extends Error{workflow;problems;constructor(e,t){super(`Workflow ${e} is refused:
2
2
  - ${t.join(`
3
- - `)}`),this.name="WorkflowDefinitionError",this.workflow=e,this.problems=t}},M=$.string().min(1),ot=$.custom(n=>n instanceof $.ZodType,{error:"Expected a zod schema"}),di=$.strictObject({id:M,input:ot,entry:M,prerequisites:$.array(M).default([]),participants:$.record(M,$.discriminatedUnion("context",[$.strictObject({context:$.literal("conversation")}),$.strictObject({context:$.literal("isolated"),lifetime:$.enum(["instance","attempt"]),role:M,model:$.strictObject({provider:M,model:M,thinkingEffort:Oe.optional(),configurable:$.boolean()})})])),steps:$.record(M,$.strictObject({inputs:$.record(M,$.union([$.strictObject({source:$.union([$.literal("$input"),$.strictObject({step:M,participant:M.optional()})]),mode:$.enum(["current","historical"]),optional:$.boolean()}),$.strictObject({evidence:M,optional:$.boolean()})])),assignments:$.array($.strictObject({participant:M,reads:$.array(M),prompt:$.string(),output:ot,checks:$.array(M).default([])})),gate:$.boolean(),options:$.array($.strictObject({name:M,on:$.array($.enum(["gate","recovery"])).min(1)})).default([]),restriction:M.nullable(),maxAttempts:$.number().int().positive(),edges:$.array($.strictObject({when:$.union([$.literal("otherwise"),$.array($.strictObject({field:$.string(),equals:$.union([$.string(),$.number(),$.boolean(),$.null()])}))]),to:M}))}))}),sn=new WeakMap,at=n=>typeof n=="object"&&n!==null&&sn.has(n);function Ne(n,e){let t=sn.get(n),i=e==="$input"?t?.input:t?.outputs.get(e.step)?.get(e.participant);if(i===void 0)throw new Error(`Workflow ${n.id} has no schema for ${JSON.stringify(e)}`);return i}function li(n){let e=di.safeParse(n),t=fi(n);if(!e.success)throw new J(t,e.error.issues.map(c=>`${c.path.map(String).join(".")||"(definition)"}: ${c.message}`));let i=hi(e.data);if(i.problems.length>0)throw new J(t,i.problems);let s=i.data,r=wi(s);if(r.length>0)throw new J(t,r);let o=ue(pi(s));return sn.set(o,{input:s.input,outputs:new Map(Object.entries(s.steps).map(([c,{assignments:a}])=>[c,new Map(a.map(({participant:u,output:d})=>[u,d]))]))}),o}var fi=n=>{let{id:e}=typeof n=="object"&&n!==null?n:{};return typeof e=="string"?e:"?"};function pi(n){let{input:e,participants:t,steps:i,...s}=n;return JSON.parse(JSON.stringify({...s,input:Ke(e),participants:t,steps:Object.fromEntries(Object.entries(i).map(([r,{assignments:o,...c}])=>[r,{...c,assignments:o.map(({output:a,...u})=>({...u,output:Ke(a)}))}]))}))}function hi(n){let e=[],t=(s,r)=>{let o=rt(s,r);return e.push(...o.problems),o.schema??s},i=Object.fromEntries(Object.entries(n.steps).map(([s,r])=>[s,{...r,assignments:r.assignments.map(o=>({...o,output:t(o.output,`steps.${s}: ${o.participant}'s output`)}))}]));return{problems:e,data:{...n,input:t(n.input,"input"),steps:i}}}var mi=["reason","verdict","option"];function ct(n){if(!n.startsWith("/"))return;let e=n.slice(1).split("/");if(!e.some(t=>/~(?![01])/.test(t)))return e.map(t=>t.replaceAll("~1","/").replaceAll("~0","~"))}function gi(n,e){let t=n;for(let i of ct(e)??[])if(Array.isArray(t)){if(!/^(0|[1-9]\d*)$/.test(i))return;t=t[Number(i)]}else if(typeof t=="object"&&t!==null&&Object.hasOwn(t,i))t=t[i];else return;return t}function we(n,e){let t=ut(n,e);if(t===void 0)throw new Error("No edge matched: a registered step always ends with otherwise");return t}var ut=(n,e)=>n.find(t=>t.when==="otherwise"||t.when.every(({field:i,equals:s})=>gi(e,i)===s)),rn=(n,e)=>({outputs:{},decision:{reason:"recovery",verdict:"revise",option:n,feedback:e}});function on(n,e){let t=(i,s)=>n.when!=="otherwise"&&n.when.some(r=>r.field===i&&r.equals===s);return!t("/decision/reason","recovery")||!t("/decision/option",e)?`its first matching edge does not guard /decision/reason = recovery and /decision/option = ${e}`:n.to===oe?"its first matching edge completes the instance, which a recovery cannot":null}function wi(n){let e=[],{steps:t,participants:i}=n,s=c=>Object.hasOwn(t,c),r=Object.entries(i).filter(([,c])=>c.context==="conversation");r.length>1&&e.push(`participants: more than one conversation participant (${r.map(([c])=>c).join(", ")})`),s(n.entry)||e.push(`entry: unknown step ${n.entry}`);let o=new Set;for(let c of n.prerequisites)o.has(c)&&e.push(`prerequisites: ${c} is listed twice`),o.add(c);for(let[c,a]of Object.entries(t)){let u=`steps.${c}`,d=h=>{e.push(`${u}: ${h}`)};c.startsWith("$")&&d('a step name may not start with "$"');for(let[h,w]of Object.entries(a.inputs)){if(!("source"in w)||w.source==="$input")continue;let{step:k,participant:O}=w.source;if(!s(k)){d(`input ${h} binds unknown step ${k}`);continue}O!==void 0&&!t[k].assignments.some(v=>v.participant===O)&&d(`input ${h} binds participant ${O}, which step ${k} does not assign`),k===c&&w.mode==="current"&&!w.optional&&d(`input ${h} requires the step's own current result, which entering the step retires`)}a.assignments.length===0&&!a.gate&&d("a step with no assignments is a pure gate and must set gate: true");let f=new Set;for(let h of a.assignments){let{participant:w}=h;Object.hasOwn(i,w)||d(`assigns unknown participant ${w}`),f.has(w)&&d(`assigns participant ${w} twice`),f.add(w),h.checks.length>0&&i[w]?.context==="conversation"&&d(`${w}'s assignment lists checks, which only an isolated participant's may`),new Set(h.checks).size!==h.checks.length&&d(`${w}'s assignment lists a check twice`);let k=new Set;for(let O of h.reads)Object.hasOwn(a.inputs,O)||d(`${w} reads unknown input ${O}`),k.has(O)&&d(`${w} reads input ${O} twice`),k.add(O)}let l=new Map;for(let h of a.options)l.has(h.name)&&d(`option ${h.name} is declared twice`),l.set(h.name,h.on),new Set(h.on).size!==h.on.length&&d(`option ${h.name} lists a reason twice`),h.on.includes("gate")&&!a.gate&&d(`option ${h.name} is declared on gate, but the step has no gate`);let g=a.options.some(h=>h.on.includes("recovery"));if(g){let h=a.assignments.every(({participant:w})=>i[w]?.context==="isolated");(a.assignments.length===0||!h)&&d("a recovery option needs every assignment of the step isolated: a mixed step recovers only by retry or cancel");for(let{name:w,on:k}of a.options){if(!k.includes("recovery"))continue;let O=ut(a.edges,rn(w,null)),v=O===void 0?"no edge matches it":on(O,w);v!==null&&d(`option ${w} on recovery: ${v}`)}}(a.edges.length===0||a.edges.at(-1).when!=="otherwise")&&d("the last edge must be otherwise");let p=new Map;a.edges.forEach((h,w)=>{let k=`edge ${w}`;if(h.to!==oe&&!s(h.to)&&d(`${k} targets unknown step ${h.to}`),h.when==="otherwise"){w!==a.edges.length-1&&d(`${k} is otherwise but not last`);return}if(h.when.length===0){d(`${k} has no conditions (use otherwise)`);return}let O=new Map;for(let{field:j,equals:S}of h.when){O.has(j)&&O.get(j)!==S&&d(`${k} tests ${j} for two different values`),O.set(j,S);let D=bi(j,S,a,f,l,g);D!==null&&d(`${k}: ${D}`)}let v=U([...O].sort(([j],[S])=>j<S?-1:j>S?1:0)),b=p.get(v);b!==void 0?d(`${k} repeats the predicate of edge ${b}`):p.set(v,w)})}if(s(n.entry)){let c=new Set([n.entry]),a=[n.entry];for(let u=a.shift();u!==void 0;u=a.shift())for(let{to:d}of t[u].edges)d!==oe&&s(d)&&!c.has(d)&&(c.add(d),a.push(d));for(let u of Object.keys(t))c.has(u)||e.push(`steps.${u}: not reachable from the entry ${n.entry}`)}return e}function bi(n,e,t,i,s,r){let o=ct(n);if(o===void 0)return`${n} is not a JSON Pointer`;let[c,a,...u]=o;if(c==="outputs"){if(a===void 0||!i.has(a))return`${n} names no participant the step assigns`;let d=t.assignments.find(l=>l.participant===a).output,f=re(d,u);return f.length===0?`${n} is not a field of ${a}'s output`:f.some(l=>$.safeParse(l,e).success)?null:`${n} can never equal ${JSON.stringify(e)}`}if(c==="decision"&&u.length===0&&mi.includes(a))switch(a){case"verdict":return e==="approve"||e==="revise"?null:`${n} can never equal ${JSON.stringify(e)}`;case"reason":return e==="exhaustion"?null:e==="gate"?t.gate?null:`${n} tests gate on a step without a gate`:e==="recovery"?r?null:`${n} tests recovery on a step without a recovery option`:`${n} can never equal ${JSON.stringify(e)}`;default:return e===null||typeof e=="string"&&s.has(e)?null:`${n} tests the undeclared option ${JSON.stringify(e)}`}return`${n} is not a routing field (/outputs/<participant>/..., /decision/reason, /decision/verdict or /decision/option)`}function dt(n,e){let t=[],i=(s,r,o)=>{t.push(`${s}: ${r} ${o} is not bound by the host`)};for(let s of n.prerequisites)e.prerequisites.has(s)||t.push(`prerequisites: ${s} is not bound by the host`);for(let[s,r]of Object.entries(n.participants))r.context==="isolated"&&!e.roles.has(r.role)&&i(`participants.${s}`,"role",r.role);for(let[s,{restriction:r,inputs:o,assignments:c}]of Object.entries(n.steps)){let a=`steps.${s}`;r!==null&&!e.restrictions.has(r)&&i(a,"restriction",r);for(let u of Object.values(o))"evidence"in u&&!e.evidence.has(u.evidence)&&i(a,"evidence provider",u.evidence);for(let{checks:u}of c)for(let d of u)e.checks.has(d)||i(a,"check provider",d)}return[...new Set(t)]}var ki=(n,e,t)=>(n.counts.get(t)??0)>=e.steps[t].maxAttempts;function ft(n){let e=n.current;if(!_(n)||e===null||e.result===null)return;let t=n.results.get(e.result);return t.routed===null&&t.decision===null?{attempt:e,result:t}:void 0}var lt={required:[],allowed:[]};function R(n,e){let t=n.current;if(_(n)&&t!==null&&t.recovery!==null&&t.recovery.decision===null)return yi(n,e,t);let i=ft(n);if(i===void 0)return null;let{attempt:s,result:r}=i,o=e.steps[r.step];if(o===void 0)return null;let c=(u,d)=>({id:Qe(s.entered.attempt,u),attempt:s.entered.attempt,reason:u,result:r.revision,subject:tn(n,s),actions:{cleanupPending:!1,verdicts:["approve","revise"],options:an(e,r.step,u),restart:{retry:lt,options:Object.fromEntries(an(e,r.step,u).map(f=>[f,lt]))}},detail:d});if(o.gate)return c("gate",{});let{to:a}=we(o.edges,{outputs:r.outputs,decision:null});return a!==oe&&ki(n,e,a)?c("exhaustion",{blocked:{from:r.step,to:a}}):null}var an=(n,e,t)=>(n.steps[e]?.options??[]).filter(({on:i})=>i.includes(t)).map(({name:i})=>i);function yi(n,e,t){let i=t.recovery,s=d=>{let f=e.participants[d];return f?.context==="isolated"&&f.lifetime==="instance"},r=d=>[...new Set(d)].sort(),o=r(t.assignments.map(({participant:d})=>d).filter(s)),c=r([...n.attempts.values()].flatMap(({assignments:d})=>d.filter(({context:f})=>f==="isolated").map(({participant:f})=>f))).filter(s),{step:a}=t.entered,u=an(e,a,"recovery");return{id:Qe(t.entered.attempt,"recovery"),attempt:t.entered.attempt,reason:"recovery",result:null,subject:tn(n,t),actions:{cleanupPending:!1,verdicts:["revise"],options:u,restart:{retry:{required:[],allowed:o},options:Object.fromEntries(u.map(d=>[d,{required:[],allowed:c}]))}},detail:{recovery:{cause:i.cause,detail:i.detail,candidates:i.candidates,reports:i.reports}}}}function cn(n,e){if(n.reason!=="recovery")return n;let{draining:t,unresolved:i}=e.cleanup,s=t.length>0||i.length>0,r=({allowed:c})=>({required:c.filter(a=>e.uncontinuable.has(a)),allowed:c}),{actions:o}=n;return{...n,actions:{...o,cleanupPending:s,verdicts:s?[]:o.verdicts,restart:{retry:r(o.restart.retry),options:Object.fromEntries(Object.entries(o.restart.options).map(([c,a])=>[c,r(a)]))}}}}function pt(n,e,t,i,s,r){let o=n.instance,c=_e(o,i);if(t===oe)return{kind:"transition",row:{op:"completed",transition:c,cause:i}};let a=Me(n,e,{instance:o,step:t,cause:i,activation:Ee(c),deciding:r,evidence:s});return"needs"in a?{kind:"needs",needs:a.needs}:"refused"in a?{kind:"refused",reason:a.refused}:{kind:"transition",row:a.row}}function Z(n,e,t){let i=ft(n);if(i===void 0||R(n,e)!==null)return{kind:"none"};let{result:s}=i,r=we(e.steps[s.step].edges,{outputs:s.outputs,decision:null});return pt(n,e,r.to,{kind:"routed",result:s.revision},t)}function ht(n,e,t,i,s){let{operationId:r,verdict:o,option:c,feedback:a}=t;if(new Set(t.restart).size!==t.restart.length)return{kind:"refused",reason:"the restart list names a participant twice"};let u=[...t.restart].sort(),d=n.operations.get(r);if(d!==void 0)return d.op==="decided"&&d.decision===t.decision&&d.verdict===o&&d.option===c&&d.feedback===a&&d.restart.length===u.length&&d.restart.every((S,A)=>S===u[A])?{kind:"replay"}:{kind:"refused",reason:`operation ${r} was already used for another request`};if(n.opened===null)return{kind:"refused",reason:"no workflow instance is open on this thread"};if(n.terminal!==null)return{kind:"refused",reason:`the instance is ${n.terminal.kind}`};if(n.cancellation!==null)return{kind:"refused",reason:"the instance is being cancelled"};if(e===void 0)return{kind:"refused",reason:"the instance cannot execute: no definition it can continue on is registered"};let f=R(n,e);if(f===null||f.id!==t.decision)return{kind:"refused",reason:`decision ${t.decision} is not pending`};let l=cn(f,i);if(l.actions.cleanupPending){let{draining:j,unresolved:S}=i.cleanup;return{kind:"refused",reason:`the recovery's cleanup is not established yet: ${[...S,...j].join("; ")}`}}if(!l.actions.verdicts.includes(o))return{kind:"refused",reason:`${o} is not offered on a ${l.reason} decision`};if(c!==null){if(o==="approve")return{kind:"refused",reason:"an option accompanies only revise"};if(!l.actions.options.includes(c))return{kind:"refused",reason:`option ${c} is not declared for a ${l.reason} decision`}}let g=c===null?l.actions.restart.retry:l.actions.restart.options[c],p=u.filter(j=>!g.allowed.includes(j));if(p.length>0)return{kind:"refused",reason:`this decision cannot restart ${p.join(", ")}`};let h=g.required.filter(j=>!u.includes(j));if(h.length>0)return{kind:"refused",reason:`${h.map(S=>`${S} (${i.uncontinuable.get(S)})`).join(", ")} cannot be continued, so this decision must restart ${h.length===1?"it":"them"}`};let w={reason:l.reason,verdict:o,option:c,feedback:a},k=n.attempts.get(l.attempt),O=e.steps[k.entered.step],v;if(l.result!==null)v=we(O.edges,{outputs:n.results.get(l.result).outputs,decision:w}).to;else if(c===null)v=k.entered.step;else{let j=we(O.edges,rn(c,a)),S=on(j,c);if(S!==null)return{kind:"refused",reason:`option ${c} cannot route a recovery: ${S}`};v=j.to}let b=pt(n,e,v,{kind:"decision",decision:l.id},s,{result:l.result,id:l.id,decision:w,restart:u});return b.kind==="needs"?b:b.kind==="refused"?{kind:"refused",reason:`the selected step ${v} cannot be entered: ${b.reason}`}:{kind:"rows",rows:[{op:"decided",decision:l.id,operation_id:r,actor:t.actor,attempt:l.attempt,reason:l.reason,result:l.result,verdict:o,option:c,feedback:a,restart:u},b.row]}}function mt(n,e){let t=n.operations.get(e.operationId);return t!==void 0?t.op==="cancel-requested"&&t.reason===e.reason?{kind:"replay"}:{kind:"refused",reason:`operation ${e.operationId} was already used for another request`}:n.opened===null?{kind:"refused",reason:"no workflow instance is open on this thread"}:n.terminal!==null?{kind:"refused",reason:`the instance is ${n.terminal.kind}`}:n.cancellation!==null?{kind:"refused",reason:`the instance is already being cancelled (${n.cancellation.operation_id})`}:{kind:"rows",rows:[{op:"cancel-requested",operation_id:e.operationId,actor:e.actor,reason:e.reason}]}}var gt=()=>({managed:!1,claims:new Map,submissions:new Map,accepted:new Map,reminded:new Map,calls:new Map,response:null,answer:ee.init()}),be=je({key:"lagent:workflows/participant",init:gt,apply(n,e){if(e.type==="thread/spawned"){if(e.data.fork!==null)return gt();let i=e.data.origin?.cause;return n.managed=i?.kind==="assignment"&&i.owner===T,n}if(!n.managed)return n;let t=n.answer;if(n.answer=ee.apply(t,e),e.type==="input/message"&&e.data.input_id!==null&&!n.claims.has(e.data.input_id)&&n.claims.set(e.data.input_id,e.seq),e.type==="input/message"){let i=G.readContribution(e);i!==void 0&&n.reminded.set(i.assignment,i.epoch)}if(e.type==="assistant/message"&&Xe(n,e),e.type==="tool/call"&&en(n,e),e.type==="tool/result"){let i=Se(e.data.result);i!==void 0&&n.submissions.set(e.seq,{...i,call:e.source_seqs[0]??null})}return e.type==="turn/end"&&t.status==="pending"&&n.answer.status==="accepted"&&n.accepted.set(n.answer.seq,e.seq),n}});function un(n,e,t){let{id:i,thread:s,task:r,generation:o}=n;if(r===null||o===null)throw new Error(`Assignment ${i} runs on the root`);let c=e.claims.get(r),a=[...e.submissions].filter(([p,h])=>c!==void 0&&p>c&&h.assignment===i),u=p=>({assignment:i,thread:s,task:r,generation:o,outcome:p});for(let[p,{output:h}]of a){let w=e.accepted.get(p);if(w!==void 0)return u({kind:"accepted",submission:p,ending:w,output:Y(h)})}let d=a.at(-1),f=d===void 0?null:{submission:d[0],output:Y(d[1].output)},l=t?.latestText??null,g=t?.settlement??null;switch(g?.kind){case void 0:return u({kind:"unfinished",candidate:f,report:l});case"failed":return u({kind:"ended",settlement:"failed",detail:g.why,candidate:f,report:l});case"stopped":{let p=t?.stop==null?"":` (stopped by ${t.stop.by==="person"?"the user":t.stop.by})`;return u({kind:"ended",settlement:"stopped",detail:`${g.reason}${p}`,candidate:f,report:l})}case"completed":return u({kind:"ended",settlement:"completed",detail:"it answered without its answer being an accepted submission of the assignment",candidate:f,report:g.output})}}function wt(n,e){let t=V(e).flatMap(r=>{if(!n.isLoaded(r.thread))return[];let o=le(n,T,{taskId:r.task,childId:r.thread});return[un(r,n.state(be,r.thread),o)]}),i=t.some(({outcome:r})=>r.kind==="ended"),s=t.filter(({outcome:r})=>i||r.kind!=="unfinished");return s.length===0?[]:[{op:"observed",observations:s}]}function bt(n,e,t,i){let s=V(e),r=s.find(({id:f})=>f===t);if(r===void 0)return null;let o={taskId:r.task,childId:r.thread};if(n.summary(o.childId)!==void 0&&(!n.isLoaded(o.childId)||le(n,T,o)!==null))return null;let c=s.flatMap(f=>{if(f===r||!n.isLoaded(f.thread))return[];let l=le(n,T,{taskId:f.task,childId:f.thread});return[un(f,n.state(be,f.thread),l)]}),{id:a,thread:u,generation:d}=r;return{op:"observed",observations:[{assignment:a,thread:u,task:o.taskId,generation:d,outcome:{kind:"refused",detail:i}},...c]}}function V(n){return n.opened===null||n.terminal!==null||n.cancellation!==null||n.current===null?[]:n.current.assignments.filter(({context:e,state:t,task:i})=>e==="isolated"&&t!=="accepted"&&t!=="superseded"&&!n.observed.has(i))}function kt(n,e,t,i){let s=e.current;if(!_(e)||s===null||s.recovery!==null)return null;let r=s.result===null?void 0:e.results.get(s.result),o=r!==void 0&&r.routed===null&&r.decision===null&&R(e,t)===null,c=V(e);if(!o&&c.length===0)return null;let a=c.flatMap(u=>{if(n.summary(u.thread)===void 0)return[];let d=le(n,T,{taskId:u.task,childId:u.thread});return[un(u,n.state(be,u.thread),d)]});return{op:"stopped",attempt:s.entered.attempt,reason:i,observations:a}}import{scopeOf as vi}from"@leuduan/cordis";import{z as Ce}from"zod";var ln=Ce.strictObject({allow:Ce.array(Ce.string()),denial:Ce.string().optional()}),yt=n=>n.source_seq,ae=(n,e)=>n!==null&&n.caller_thread_id===e&&n.cause.kind==="assignment"&&n.cause.owner===T;function ce(n,e){let t=n.journal.origins.callers(e).threads,i=t.at(-1);if(i===void 0)return;if(i.origin!==null)return{unreadable:"This thread's conversation root cannot be found, so its workflow cannot be read."};if(!z(i))return;let s=i.thread_id,r=n.journal.state(q,s);if(r.opened!==null){if(t.length===1)return{root:s,state:r,kind:"root"};for(let o=0;o<t.length-1;o+=1){let c=t[o];if(ae(c.origin,s)){let a=n.journal.state(be,c.thread_id);if(o===0)return{root:s,state:r,kind:"participant",thread:c.thread_id,facts:a};let u=yt(t[o-1].origin);return{root:s,state:r,kind:"descendant",source:{kind:"participant",thread:c.thread_id,facts:a,seq:u}}}if(o===t.length-2)return{root:s,state:r,kind:"descendant",source:{kind:"root",seq:yt(c.origin)}}}}}var $t=(n,e)=>_(n)&&n.current?.assignments.includes(e)===!0&&e.state!=="accepted"&&e.state!=="superseded";function B(n){if(!(!_(n)||n.current===null))return n.current.assignments.find(e=>e.context==="conversation"&&$t(n,e))}function $i(n,e){for(let t of[...n.attempts.values()].reverse()){let i=t.assignments.find(({context:s,claimSeq:r,closed:o})=>s==="conversation"&&r!==null&&r<=e&&(o===null||o>e));if(i!==void 0)return i}}function X(n,e,t,i){let s;for(let r of n.attempts.values())for(let o of r.assignments){if(o.thread!==e||o.task===null)continue;let c=t.claims.get(o.task);c===void 0||i!==void 0&&c>i||(s===void 0||c>s.claimed)&&(s={assignment:o,claimed:c})}return s?.assignment}function Oi(n,e){let t=e.task===null?void 0:n.claims.get(e.task);if(t===void 0)return!1;for(let i of n.accepted.keys())if(i>t&&n.submissions.get(i)?.assignment===e.id)return!0;return!1}function ji(n,e,t){let i=e.executable(n);if(n.cancellation!==null)return{kind:"coordination",why:"the workflow is being cancelled"};if(i===void 0)return{kind:"coordination",why:"the workflow cannot continue on the definition registered"};if(R(n,i)!==null)return{kind:"coordination",why:"a decision is pending, which freezes submission and changes"};let s=B(n);return s===void 0||s.claimSeq===null?{kind:"coordination",why:"no assignment of yours is active"}:t===void 0||t<s.claimSeq?{kind:"coordination",why:`the call was asked for before assignment ${s.id} began`}:Ot(n,i,e,s)}function dn(n,e,t,i){let s=e.executable(n);return n.cancellation!==null?{kind:"closed",why:"its workflow is being cancelled"}:s===void 0?{kind:"closed",why:"its workflow cannot continue on the definition registered"}:R(n,s)!==null?{kind:"closed",why:"a decision is pending on its workflow, which freezes its work"}:t===void 0?{kind:"closed",why:"no workflow assignment covers it"}:$t(n,t)?i!==null&&Oi(i,t)?{kind:"closed",why:`workflow assignment ${t.id} was accepted`}:Ot(n,s,e,t):{kind:"closed",why:`workflow assignment ${t.id} has ended`}}function Ot(n,e,t,i){let s=e.steps[n.current.entered.step].restriction;return{kind:"assignment",assignment:i,restriction:s===null?null:t.restrictions[s]}}function fn(n,e,t){let{state:i}=n;switch(n.kind){case"root":return ji(i,e,t);case"participant":return dn(i,e,X(i,n.thread,n.facts,t),n.facts);case"descendant":{let{source:s}=n;if(s.kind==="participant")return dn(i,e,X(i,s.thread,s.facts,s.seq),s.facts);let r=$i(i,s.seq);return r!==void 0?dn(i,e,r,null):{kind:"coordination",why:"it was started while its conversation coordinated"}}}}var H=n=>({decision:"deny",reason:n});function xi(n,e){if(n.kind==="root"){let r=B(n.state),o=r?.candidate?.call??null;return Ye(n.state,o,e)?r:void 0}if(n.kind!=="participant")return;let{facts:t}=n,{answer:i}=t,s=i.status==="pending"?t.submissions.get(i.seq):void 0;if(!(s===void 0||!Ye(t,s.call,e)))return X(n.state,n.thread,t,void 0)}function vt(n,e,t){if(!n.allow.includes(e))return H(n.denial??`${e} is not allowed while ${t}.`)}function Si(n,e,t,i){let s=i.name===P,r=()=>s?H("No workflow assignment of yours is active, so there is nothing to submit."):void 0;if(n.journal.summary(t)===void 0)return r();let o=ce(n,t);if(o===void 0)return r();if("unreadable"in o)return H(o.unreadable);if(o.state.terminal!==null)return r();let c=n.journal.state(ne,t).dispatchedCalls.get(i.call_id),a=xi(o,c);if(a!==void 0)return H(`Not run: this response already submitted assignment ${a.id}, and that submission is its answer, so the calls after it do not run.`);let u=fn(o,e,c);if(u.kind==="closed")return H(s?`Nothing can be submitted now: ${u.why}.`:`This thread works under a workflow, and no workflow authority covers this call: ${u.why}.`);if(u.kind==="coordination")return s?H(`Nothing can be submitted now: ${u.why}.`):vt(e.coordination,i.name,u.why);if(s){if(u.assignment.thread!==t)return H(`Only the participant working assignment ${u.assignment.id} may submit it.`);let d=i.arguments.assignmentId;return d===u.assignment.id?void 0:H(`Your active assignment is ${u.assignment.id}; ${JSON.stringify(d)} is not.`)}return u.restriction===null?void 0:vt(u.restriction,i.name,`assignment ${u.assignment.id} is active`)}function jt(n,e){n.on("lagent/tool/authorize",({toolCall:t,threadId:i})=>Si(n,e,i,t)),n.on("lagent/thread/activate",t=>{let i=n.journal.summary(t.id),s=i?.origin==null?void 0:n.journal.summary(i.origin.caller_thread_id),r=i===void 0?void 0:i.origin===null?i:s?.origin===null&&ae(i.origin,s.thread_id)?s:void 0;if(r===void 0||!z(r))return;let o=()=>{if(!n.journal.isLoaded(r.thread_id))return!1;let a=n.journal.state(q,r.thread_id);return a.opened===null||a.terminal!==null};if(o())return;let c=vi(t.ctx);t.ctx.effect(()=>{let a=n.tools.grant(c,[t.agentName],P),u=n.journal.subscribe(d=>{a===null||d.thread_id!==r.thread_id||!o()||(a(),a=null)});return()=>{u(),a?.()}})})}function ke(n,e,t){let i=[],s=[];for(let r of t){let o=r===e?"":`thread ${r}, `,c=Mn(n,r,{descendants:!1});i.push(...c.draining.map(a=>`${o}${a}`)),s.push(...c.unresolved.map(a=>`${o}${a}`));for(let a of He(n.journal,r))i.push(`${o}the stop of ${a.targets.length} agent task(s) has not finished`)}if(!t.has(e))for(let r of He(n.journal,e)){let o=r.targets.filter(({childId:c})=>t.has(c));o.length>0&&i.push(`the stop of ${o.length} agent task(s) has not finished`)}for(let r of ie(n.journal,e))t.has(r.childId)&&(r.state==="unavailable"?s.push(`agent ${r.childId}: its task cannot be read (${r.error})`):r.state==="open"?i.push(`agent ${r.childId} has not finished`):(r.draining||r.cleanupPending)&&i.push(`agent ${r.childId} is still being cleaned up`));return{draining:i,unresolved:s}}function We(n,e,t){let i=new Set;for(let s of n.subtree(e,{self:!0})){let{origin:r}=s,o=r!==null&&!ae(r,t)&&i.has(r.caller_thread_id);(s.thread_id===e||o)&&i.add(s.thread_id)}return i}function xt(n,e,t){let i=e.instance,s=new Map;if(i===null||e.opened===null||e.terminal!==null)return{cleanup:{draining:[],unresolved:[]},uncontinuable:s};let r=n.journal.summaryMap(),o=t.map(p=>`the start of assignment ${p}'s task is still in flight`);if(e.cancellation!==null){let p=n.journal.origins.subtree(i,{self:!0}).map(({thread_id:w})=>w),h=ke(n,i,new Set(p));return{cleanup:{draining:[...h.draining,...o],unresolved:h.unresolved},uncontinuable:s}}let c=e.current;if(c?.recovery==null||c.recovery.decision!==null)return{cleanup:{draining:[],unresolved:[]},uncontinuable:s};let a=new Set;for(let{context:p,thread:h}of c.assignments)if(p==="isolated")for(let{thread_id:w}of n.journal.origins.subtree(h,{self:!0}))a.add(w);let u=c.assignments.some(({context:p})=>p==="conversation");if(u)for(let p of We(n.journal.origins,i,i))a.add(p);let d=ke(n,i,a),f=[...d.draining,...o],l=[...d.unresolved];if(u){let{pending:p,runs:h}=n.journal.state(ne,i).requests,w=p===null?void 0:h.get(p.runId)?.requestedAt;w!==void 0&&w<=c.recovery.seq&&f.push(`the conversation's run ${p.runId}, under way when its assignment was superseded, has not ended`)}let g=n.get("delegations");for(let[p,h]of nn(e)){if(!r.has(h))continue;let w=O=>{f.push(`whether ${p}'s conversation can be continued is not known yet: ${O}`)};if(g===void 0){w("delegations is not mounted");continue}if(!n.journal.isLoaded(h)){w("it is not loaded");continue}let k=g.continuation(h);k!==null&&(k.kind==="refusal"?s.set(p,k.reason):k.kind==="wait"?f.push(`${p}: ${k.reason}`):l.push(`${p}: ${k.reason}`))}return{cleanup:{draining:f,unresolved:l},uncontinuable:s}}var _i="another participant's task in the same workflow attempt ended without acceptance";function pn(n,e,t){let i=e.instance,s=new Set(t.observations.flatMap(({task:c,outcome:a})=>a.kind==="ended"||a.kind==="refused"?[c]:[]));if(i===null||s.size===0)return[];let r=new Map(ie(n.journal,i).flatMap(c=>c.state!=="unavailable"&&(c.state==="open"||c.draining)?[[c.taskId,c]]:[])),o=V(e).flatMap(({task:c,thread:a})=>c===null||s.has(c)||r.get(c)?.childId!==a?[]:[{taskId:c,childId:a}]);return o.length===0?[]:[Nn(i,_i,o)]}import{z as Ie}from"zod";var Ei=Ie.strictObject({instance:Ie.string().min(1),status:Ie.enum(["assignment","decision","blocked","cleanup","ended"]),subject:Ie.string().nullable()}),qi=te({plugin:"lagent:workflows",kind:"presentation",version:1,schema:Ei});function Ti(n,e,t){let{state:i,facts:s,thread:r,root:o}=n;if(i.terminal!==null||i.cancellation!==null||i.current===null||e===void 0||R(i,e)!==null)return;let c=new Set(t.claimed.map(({id:u})=>u)),a=i.current.assignments.find(u=>u.thread===r&&u.state!=="accepted"&&u.state!=="superseded"&&(c.has(u.task)||X(i,r,s,void 0)===u));return a===void 0?void 0:{instance:o,status:"assignment",subject:a.id}}function Mi(n,e,t){let i=n.instance;if(n.terminal!==null)return{instance:i,status:"ended",subject:n.terminal.kind};if(n.cancellation!==null)return{instance:i,status:"cleanup",subject:null};if(e===void 0)return{instance:i,status:"blocked",subject:"its definition is not registered, or no longer fits where it stands"};let s=R(n,e);if(s!==null)return{instance:i,status:"decision",subject:s.id};let r=B(n);if(r!==void 0){if(r.state!=="scheduled")return{instance:i,status:"assignment",subject:r.id};let c=Ge(r);return t.claimed.some(({id:a})=>a===c)?{instance:i,status:"assignment",subject:r.id}:void 0}let o=Z(n,e);return o.kind==="refused"?{instance:i,status:"blocked",subject:o.reason}:void 0}function _t(n,e){let t=i=>{if(n.journal.summary(i)===void 0)return;let s=ce(n,i);return s===void 0||"unreadable"in s?void 0:s};qn(n,{event:qi,rank:2,current:i=>{let s=t(i.threadId);if(s===void 0)return;let r=e.executable(s.state);return s.kind==="participant"?Ti(s,r,i):s.kind==="root"?Mi(s.state,r,i):void 0},render:(i,s,{step:r})=>{let o=n.journal.state(q,i.instance),c=e.executable(o);if(r.threadId!==i.instance){let f=o.current.assignments.find(({id:l})=>l===i.subject);return Be("workflow",St(o,c,f,e),{attrs:{id:i.instance,status:i.status}})}let a=o.opened.definition,u=f=>Be("workflow",f,{attrs:{id:i.instance,status:i.status}}),d=`Until then you work under the coordination policy: ${Ai(e.coordination)}`;switch(i.status){case"assignment":{let f=B(o);return u(St(o,c,f,e))}case"decision":{let{recovery:f}=o.current;return u(f!==null?Ri(a,o.current,i.subject,d):`Workflow ${a}: step ${o.current.entered.step}'s result awaits a decision (${i.subject}). Nothing can be submitted or changed until it is decided, and chat is discussion only: the user decides. `+d)}case"blocked":return u(`Workflow ${a} cannot go on: ${i.subject}. ${d}`);case"cleanup":return u(`Workflow ${a} is being cancelled. ${d}`);case"ended":return s===null||s.status==="ended"?null:u(`Workflow ${a} ${i.subject==="completed"?"is complete":"was cancelled"}: its restrictions no longer apply, and this conversation goes on as ordinary chat.`)}}})}function Ri(n,e,t,i){let s=e.recovery,{step:r,ordinal:o}=e.entered,c=[`Workflow ${n}: attempt ${o} of step ${r} cannot finish (${s.cause==="stop"?"stopped":"failed"}: ${s.detail}), and awaits a recovery decision (${t}): a retry, a declared option, or cancelling. Nothing can be submitted or changed until it is decided, and chat is discussion only: the user decides. ${i}`],a=Et(s,null);return a.length>0&&c.push("","Unaccepted, shown to you as its coordinator only:",...a),c.join(`
4
- `)}function Et(n,e){let t=[],i=s=>e===null||s===e;for(let[s,r]of Object.entries(n.candidates))i(s)&&t.push(`- ${s}'s last submission, not accepted: ${JSON.stringify(r)}`);for(let[s,r]of Object.entries(n.reports))i(s)&&t.push(`- ${s}'s partial report, not accepted: ${r}`);return t}var Ai=n=>`${n.allow.length===0?"no tool may be called":`you may call only ${n.allow.join(", ")}`}; any other call is denied, and a listed tool still needs to be granted to you and allowed by the host's permissions.`,Ni=(n,e)=>`${n.allow.length===0?"no other tool may be called":`the only other tools you may call are ${n.allow.join(", ")}`}; any other call is denied, and a listed tool still needs to be granted to you and allowed by the host's permissions. ${P} for assignment ${e} stays available whatever the list says, subject to the host's permissions.`;function St(n,e,t,i){let s=n.current,{step:r,ordinal:o}=s.entered,c=e.steps[r],a=c.assignments.find(({participant:g})=>g===t.participant),u=[`Workflow ${e.id}, step ${r}, attempt ${o}: your assignment is ${t.id}.`,"",a.prompt];if(a.reads.length>0){u.push("","Inputs:");for(let g of a.reads){let p=s.entered.inputs[g]??null;u.push(`- ${g} (${Ci(n,p)}): ${JSON.stringify(Te(n,s.entered,g))}`)}}let d=Wi(n,s,t.participant);d!==null&&u.push("",d);let f=Ii(n,t);f.length>0&&(u.push("","Superseded since your previous assignment, so do not rely on them as you remember them:"),u.push(...f.map(g=>`- ${g}`))),u.push("",`Submit your output with ${P} ({ "assignmentId": "${t.id}", "output": ... }); the output must match this JSON Schema:`,JSON.stringify(a.output));let l=c.restriction===null?null:i.restrictions[c.restriction];return u.push("",l===null?`This step adds no restriction to your calls; ${P} for assignment ${t.id} is available, subject to the host's permissions.`:`While this assignment is active, ${Ni(l,t.id)}`,"","Status: the workflow is running; this step waits on your submission."),u.join(`
5
- `)}function Pe(n){return n==="approve"?"approved":"sent back"}function Ci(n,e){if(e===null)return"optional, nothing to bind";if(e.kind==="input")return"the workflow's input";if(e.kind==="evidence"){let o=e.immutable?"a snapshot":"live: it describes the state at this step's entry, which may have changed since";return`evidence from ${e.provider}, ${o}`}let t=n.results.get(e.result),i=e.participant===null?"result":`${e.participant}'s output`,s=e.decision===null?void 0:n.decisions.get(e.decision),r=s===void 0?"":`, ${Pe(s.verdict)}`;return`${i} of step ${t.step}, revision ${t.revision}, ${e.mode}${r}`}function Wi(n,e,t){let{cause:i}=e.entered;if(i.kind!=="decision")return null;let s=n.decisions.get(i.decision),r=n.attempts.get(s.attempt),o=s.option===null?"":` (${s.option})`,c=s.feedback===null?"":`, with this feedback: ${s.feedback}`,a=`This attempt was opened when step ${r.entered.step} was ${Pe(s.verdict)}${o} (decision ${s.decision})${c}.`;if(r.recovery===null)return a;let u=Et(r.recovery,t),d=`That attempt could not finish: ${r.recovery.detail}.`;return u.length===0?`${a} ${d}`:[`${a} ${d} What you left in it, not accepted:`,...u].join(`
6
- `)}function Ii(n,e){let t=null;for(let o of n.attempts.values()){if(o===n.current)break;for(let c of o.assignments)c.participant===e.participant&&(c.context==="isolated"?t=o.seq:c.claimSeq!==null&&(t=c.claimSeq))}if(t===null)return[];let i=se(n),s=Hn(n,t),r=[...n.results.values()].filter(({revision:o})=>!i.has(o));return[...i].filter(o=>!s.has(o)).map(o=>{let{step:c}=n.results.get(o),a=r.filter(u=>u.step===c).at(-1);return a===void 0?`${o} (step ${c}) is no longer current`:`${o} (step ${c}) is replaced by ${a.revision}`})}var Le=class{handle;inFlight=new Map;running=new Set;outcomes=new Map;reported=new Set;closing=!1;ctx;options;constructor(e,t){this.ctx=e,this.options=t}register(){let e=this.ctx.get("delegations");if(e===void 0)return()=>{};let t=e.registerOwner({name:T,authorize:(i,s)=>this.authorize(i,s),settled:i=>this.options.reconcile(i.callerThreadId)});return this.handle=t,()=>{this.handle===t&&(this.handle=void 0);for(let{controller:i}of this.inFlight.values())i.abort(new Error("The workflow owner was withdrawn"));t.dispose()}}changed(e){this.handle?.changed(e)}starting(e){return[...this.inFlight.values()].filter(t=>t.root===e).map(({assignment:t})=>t)}waiting(e){return[...this.outcomes.get(e)?.values()??[]].some(({kind:t})=>t==="wait")}held(e){return[...this.outcomes.get(e)?.values()??[]].some(({kind:t})=>t==="hold")}of(e){let t=this.outcomes.get(e);return t===void 0&&this.outcomes.set(e,t=new Map),t}states(e,t){let i=qt(this.ctx,e),s=new Map;for(let r of hn(t)){let o=i.has(r.task);s.set(r.id,o?{kind:"started"}:this.outcomes.get(e)?.get(r.id)??{kind:"starting"})}return s}dispatch(e,t){let i=this.ctx.journal.state(q,e),s=this.of(e);for(let r of s.keys())i.current?.assignments.some(o=>o.id===r)||s.delete(r);if(s.size===0&&this.outcomes.delete(e),!(this.closing||this.options.quarantined(e)!==null))for(let r of this.owed(e,i)){if(this.inFlight.has(r.task))continue;let o=this.outcomes.get(e)?.get(r.id);if(t==="capacity"&&!(o?.kind==="wait"&&o.on==="capacity"))continue;let c=new AbortController;this.inFlight.set(r.task,{root:e,assignment:r.id,controller:c});let a=this.start(e,r.id,c.signal).catch(u=>{this.of(e).set(r.id,{kind:"hold",reason:`assignment ${r.id} cannot start its task: ${de(u)}`}),this.ctx.reportError(u)}).finally(()=>{this.inFlight.delete(r.task),this.running.delete(a);let u=this.outcomes.get(e)?.get(r.id);this.options.ended(e,u===void 0),u!==void 0&&!(u.kind==="wait"&&u.on==="capacity")&&this.retryCapacity(e)});this.running.add(a)}}retryCapacity(e){if(![...this.outcomes.get(e)?.values()??[]].some(s=>s.kind==="wait"&&s.on==="capacity")||this.closing)return;let i=this.ctx.threads.retain(()=>[e],{lastRelease:"keep"}).then(s=>{try{this.dispatch(e,"capacity")}finally{s.release()}}).catch(s=>this.ctx.reportError(s)).finally(()=>this.running.delete(i));this.running.add(i)}async shutdown(){this.closing=!0,await Promise.allSettled([...this.running])}owed(e,t){let i=t.opened===null?void 0:this.options.executable(t);if(i===void 0||R(t,i)!==null)return[];let s=qt(this.ctx,e);return hn(t).filter(r=>!s.has(r.task))}async start(e,t,i){let s=await this.ctx.threads.retain(()=>[e],{lastRelease:"keep"});try{let r=this.ctx.journal.state(q,e),o=this.owed(e,r).find(d=>d.id===t);if(o===void 0)return;let c=this.handle;if(c===void 0){this.of(e).set(t,{kind:"hold",reason:"delegations, which starts isolated participants, is not mounted"});return}let a=this.request(r,o,i);if("hold"in a){this.of(e).set(t,{kind:"hold",reason:a.hold});return}let u=await c.start(a);u.kind==="refusal"?await this.options.refused(e,t,u.reason):this.settle(e,o,u)}finally{s.release()}}settle(e,t,i){let{id:s}=t;switch(i.kind){case"opened":case"discard":this.of(e).delete(s);return;case"wait":this.of(e).set(s,{kind:"wait",on:i.on,reason:i.reason});return;case"hold":this.of(e).set(s,{kind:"hold",reason:i.reason});return;case"conflict":case"vetoed":{let r=`assignment ${s} cannot start its task: ${i.reason}`;this.of(e).set(s,{kind:"hold",reason:r}),this.reported.has(r)||(this.reported.add(r),this.ctx.reportError(new Error(`Workflow ${e}: ${r}`)));return}}}request(e,t,i){let{opened:s}=e,r=this.options.executable(e),o=e.current.entered.step,{participant:c}=t,a=s.pins.participants[c];if(t.lifetime===null||a?.configuration==null)throw new Error(`Assignment ${t.id} names no isolated participant the instance pinned`);let u=[];for(let d of t.checks){let f=this.options.providers.problem("check",d,a.checks[d]);if(f!==null)return{hold:f};u.push(this.options.checks[d].owed({config:a.checks[d].config,owner:T,assignment:t.id,generation:t.generation,inputId:t.task}))}return{callerThreadId:e.instance,grantedSeq:e.current.seq,instance:e.instance,attempt:e.current.entered.attempt,assignment:t.id,participant:c,lifetime:t.lifetime,generation:t.generation,configuration:Pi(a,this.options.checks),task:`Workflow ${r.id}: assignment ${t.id} (step ${o}) is yours; its instructions come with your next step.`,...u.length===0?{}:{rows:Li(u)},signal:i}}authorize(e,t){let i=e.callerThreadId,s=this.options.quarantined(i);if(s!==null)return{kind:"hold",reason:`its workflow instance could not be repaired: ${s}`};if(t==="running"){let u=this.options.runnable(i);return u==="allow-all"?{kind:"allow"}:Array.isArray(u)?u.includes(e.taskId)?{kind:"allow"}:{kind:"hold",reason:`its workflow assignment ${e.assignment} does not run now`}:{kind:"hold",reason:`its workflow cannot be read: ${u.unavailable}`}}if(!this.ctx.journal.isLoaded(i))return{kind:"hold",reason:`its workflow's root ${i} is not loaded`};let r=this.ctx.journal.state(q,i);if(!_(r))return{kind:"discard",reason:"its workflow ended or is being cancelled"};let o=hn(r).find(({id:u})=>u===e.assignment);if(o===void 0||o.task!==e.taskId||o.thread!==e.childId||o.generation!==e.generation||r.current.seq!==e.grantedSeq)return{kind:"discard",reason:`workflow assignment ${e.assignment} is no longer open as this task`};let c=this.options.executable(r);if(c===void 0)return{kind:"hold",reason:"its workflow cannot continue on the definition registered"};if(R(r,c)!==null)return{kind:"hold",reason:"a decision is pending"};let a=r.opened.pins.participants[o.participant]?.checks??{};for(let u of o.checks){let d=this.options.providers.problem("check",u,a[u]);if(d!==null)return{kind:"hold",reason:d}}return{kind:"allow"}}};function hn(n){return!_(n)||n.current===null||n.current.recovery!==null?[]:n.current.assignments.filter(({context:e,state:t,task:i})=>e==="isolated"&&t!=="accepted"&&t!=="superseded"&&!n.observed.has(i))}function qt(n,e){return new Set(ie(n.journal,e).flatMap(t=>"taskId"in t?[t.taskId]:[]))}function Tt(n){let e=[];for(let t of n.attempts.values())for(let{task:i,state:s}of t.assignments){if(i===null)continue;let r=t===n.current&&s!=="accepted"&&s!=="superseded";(s==="accepted"||r)&&e.push(i)}return e}function Pi(n,e){let{agent:t,route:i,environment:s,installations:r}=n.configuration,o=new Set(Object.keys(n.checks).flatMap(c=>e[c]?.requires??[]));return{agent:t,route:i,environment:s,installations:r.map(({installation_id:c,preset_id:a,config:u,lifetime:d})=>({installationId:c,presetId:a,config:u,lifetime:d})),requires:[...o].sort()}}function Li(n){return e=>{let t=[];for(let i of n){let s=i(e);if($n(s))return s;t.push(...s)}return t}}import{z as Mt}from"zod";var mn=n=>typeof n=="function",Rt=n=>{if(typeof n!="object"||n===null)return!1;let e=n;return["unavailable","validate","pin","subscribe"].every(t=>mn(e[t]))},At=Mt.custom(n=>Rt(n)&&mn(n.owed)&&Array.isArray(n.requires),{error:"Expected a check provider binding"}),Nt=Mt.custom(n=>Rt(n)&&mn(n.acquire),{error:"Expected an evidence provider binding"}),De=class{disposed=new Set;inFlight=new Set;drained=new AbortController;bindings;constructor(e){this.bindings=e}watch(e){let t=["check","evidence"].flatMap(i=>Object.entries(this.table(i)).map(([s,r])=>r.subscribe(o=>{o==="disposed"&&this.dispose(ye(i,s)),e(i,s,o)})));return()=>{for(let i of t)i()}}problem(e,t,i){let s=this.table(e)[t],r=`${e} provider ${t}`;if(s===void 0)return`${r} is not bound`;if(this.disposed.has(ye(e,t)))return`${r} was disposed`;if(i===void 0)return`the instance recorded no pin for ${r}`;let o=s.validate(i.config);if(o!==null)return`${r} refuses the instance's pin: ${o}`;let c=s.unavailable();return c===null?null:`${r} is unavailable: ${c}`}async pin(e,t,i,s){let r=this.table(e)[t];if(r===void 0)return{refused:`${e} provider ${t} is not bound`};if(this.disposed.has(ye(e,t)))return{refused:`${e} provider ${t} was disposed`};let o=r.unavailable();if(o!==null)return{refused:`${e} provider ${t} is unavailable: ${o}`};let c;try{c=await this.tracked(ye(e,t),s,a=>r.pin(i,a))}catch(a){if(a instanceof K)throw a;c={refused:a instanceof Error?a.message:String(a)}}return"refused"in c?{refused:`${e} provider ${t} refuses this opening: ${c.refused}`}:{config:c.config}}async acquire(e,t,i){let s=this.problem("evidence",e,t);if(s!==null)return{kind:"unavailable",reason:s};let r=this.bindings.evidence[e];try{return await this.tracked(ye("evidence",e),i.signal,o=>r.acquire({...i,config:t.config,signal:o}))}catch(o){if(o instanceof K)throw o;return{kind:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async drain(){this.drained.abort(new Error("The workflows plugin is unloading")),await Promise.allSettled([...this.inFlight].map(({done:e})=>e))}table(e){return e==="check"?this.bindings.checks:this.bindings.evidence}dispose(e){this.disposed.add(e);for(let t of this.inFlight)t.key===e&&t.controller.abort(new Error(`Provider ${e} was disposed`))}async tracked(e,t,i){let s=new AbortController,r=AbortSignal.any([t,s.signal,this.drained.signal]);if(r.aborted)throw new K(r.reason);let o=i(r),c={key:e,controller:s,done:o};this.inFlight.add(c),o.then(()=>this.inFlight.delete(c),()=>this.inFlight.delete(c));try{return await Promise.race([o,Di(r)])}catch(a){throw r.aborted?new K(r.reason):a}}},K=class extends Error{constructor(e){super(`Cancelled: ${e instanceof Error?e.message:String(e)}`,{cause:e}),this.name="CancelledError"}},ye=(n,e)=>`${n}:${e}`,Di=n=>new Promise((e,t)=>{n.aborted?t(n.reason):n.addEventListener("abort",()=>t(n.reason),{once:!0})});import{z as gn}from"zod";function Fi(n,e,t){let{draining:i,unresolved:s}=ke(n,t,We(n.journal.origins,t,e.root));return[...i,...s]}var zi=gn.strictObject({assignmentId:gn.string().min(1).describe("The id of the assignment this output answers, as it was presented."),output:N.describe("The assignment's output, matching the JSON Schema it was presented with.")});function Ui(n,e){return _n({name:P,description:"Submit the output of your active workflow assignment. A valid submission ends your turn as your answer; it is accepted when that answer is.",parameters:zi,onInterrupted:"rerun",executionMode:"exclusive",execute:async({assignmentId:t,output:i},{threadId:s,callSeq:r})=>{let o=h=>({output:`Not submitted: ${h}`,is_error:!0}),c=ce(n,s);if(c===void 0||"unreadable"in c||!_(c.state))return o("no workflow assignment of yours is active.");let a=fn(c,e,r);if(a.kind!=="assignment")return o(`${a.why}.`);let{assignment:u}=a;if(u.thread!==s)return o(`only the participant working ${u.id} may submit it.`);if(u.id!==t)return o(`your active assignment is ${u.id}, not ${JSON.stringify(t)}.`);let d=Fi(n,c,s);if(d.length>0)return o(`work you started has not ended: ${d.join("; ")}. Wait for it to end, or stop it, then submit again.`);let{state:f}=c,l=e.executable(f),g=f.current.entered.step,p=Ne(l,{step:g,participant:u.participant}).safeParse(i);return p.success?{output:`Submitted the output of assignment ${u.id}. It is accepted when this answer is.`,metadata:zn({assignment:u.id,output:i}),concludes_turn:!0}:o(`the output does not match the assignment's schema: ${gn.prettifyError(p.error)}`)}})}function Ji(n,e,t,i){if(!_(n))return;let s=t.executable(n);if(s===void 0||R(n,s)!==null)return;if(e.kind==="root"){let c=B(n),a=c===void 0?null:Xn(n,c);return c===void 0||a===null?void 0:{assignment:c,epoch:a,submitted:c.candidate?.seq===i,reminded:c.reminded}}if(e.kind!=="participant")return;let{facts:r}=e,o=X(n,e.thread,r,void 0);if(!(o===void 0||o.state==="accepted"||o.state==="superseded")&&n.current.assignments.includes(o))return{assignment:o,epoch:r.claims.get(o.task),submitted:r.submissions.get(i)?.assignment===o.id,reminded:r.reminded.get(o.id)??null}}function Bi(n,e){let t=(i,s)=>{let r=ce(n,i);if(r===void 0||"unreadable"in r)return null;let o=Ji(r.state,r,e,s);return o===void 0?null:{kind:r.kind,assignment:o.assignment.id,epoch:o.epoch,submitted:o.submitted,reminded:o.reminded}};n.on("lagent/turn/stop",({threadId:i,answer:s,observe:r})=>{r(()=>JSON.stringify(t(i,s.seq)));let o=t(i,s.seq);if(o===null||o.submitted)return;let{assignment:c,epoch:a}=o;if(o.reminded===a)return o.kind==="root"?void 0:G.rejection(`Assignment ${c} was answered without its submission after the one reminder, so its task fails and the workflow's recovery decides what comes next.`,{assignment:c,epoch:a});let u=o.kind==="root"?"If you need the user first, reply again saying what you need; the assignment stays open until you submit.":"Another reply without a submission fails your task.";return{decision:"continue",message:G.contribution({content:`Your workflow assignment ${c} is still open, and a reply does not finish it: submit its output with ${P} ({ "assignmentId": "${c}", "output": ... }), matching the schema it was presented with. ${u}`},{assignment:c,epoch:a})}})}function Ct(n,e){En(n,Ui(n,e)),Bi(n,e)}function It(n,e,t){let i=n.opened;if(i===null)throw new Error("No workflow instance was opened on this thread");let s=me(he(n),e),r=s===null?e:void 0,o=r??i.canonical,c=se(n),a=[...n.results.values()].map(b=>({revision:b.revision,step:b.step,outputs:b.outputs,decision:qe(n,b.decision),current:!c.has(b.revision),inputs:Object.fromEntries(Object.entries(n.attempts.get(b.attempt).entered.inputs).map(([j,S])=>[j,xe(S)]))})),u=i.input,d=Object.fromEntries(Object.entries(i.pins.participants).flatMap(([b,{configuration:j}])=>j===null?[]:[[b,{provider:j.route.provider,model:j.route.model,thinkingEffort:j.route.thinking_effort}]]));if(n.terminal!==null)return{definition:i.definition,input:u,models:d,status:n.terminal.kind,step:null,attempt:null,assignments:[],results:a,pending:null,blocked:null,waitingOn:null};let f=n.current,l=R(n,o),g={definition:i.definition,input:u,models:d,step:f?.entered.step??null,attempt:f===null?null:{id:f.entered.attempt,ordinal:f.entered.ordinal},assignments:(f?.assignments??[]).map(({id:b,participant:j,state:S,thread:A,task:D,generation:C})=>({id:b,participant:j,state:S,thread:A,task:D,generation:C})),results:a};if(n.cancellation!==null){let{unresolved:b}=t.barrier.cleanup;return b.length>0?{...g,status:"blocked",pending:null,blocked:{reason:"cleanup",detail:b.join("; ")},waitingOn:"cleanup"}:{...g,status:"running",pending:null,blocked:null,waitingOn:"cleanup"}}if(r===void 0)return{...g,status:"blocked",pending:l===null?null:Wt(l),blocked:{reason:"missing-binding",detail:s},waitingOn:null};let p=[...t.starts.values()].flatMap(b=>b.kind==="hold"?[b.reason]:[]),h=[...Ki(n,r,t),...p];if(h.length>0)return{...g,status:"blocked",pending:l===null?null:Wt(l),blocked:{reason:"missing-binding",detail:h.join("; ")},waitingOn:null};if(l!==null){let b=cn(l,t.barrier);if(!b.actions.cleanupPending)return{...g,status:"waiting",pending:b,blocked:null,waitingOn:"decision"};let{unresolved:j}=t.barrier.cleanup;return j.length>0?{...g,status:"blocked",pending:b,blocked:{reason:"cleanup",detail:j.join("; ")},waitingOn:"cleanup"}:{...g,status:"waiting",pending:b,blocked:null,waitingOn:"cleanup"}}let w=Z(n,r),k=w.kind==="refused"?w.reason:w.kind==="needs"&&t.entryRefusal?.transition===w.needs.transition?t.entryRefusal.reason:null;if(k!==null)return{...g,status:"blocked",pending:null,blocked:{reason:"entry-refused",detail:k},waitingOn:null};let O=[...t.starts.values()],v=t.settled&&B(n)!==void 0?"person-input":O.some(({kind:b})=>b==="starting"||b==="wait")?"dispatch":O.length>0?"participant":null;return{...g,status:"running",pending:null,blocked:null,waitingOn:v}}function Ki(n,e,t){let i=[],s=n.current;if(s!==null){for(let{participant:c,checks:a,state:u}of s.assignments)if(!(u==="accepted"||u==="superseded"))for(let d of a){let f=t.provider({check:d,participant:c});f!==null&&i.push(f)}}let r=Z(n,e);if(r.kind==="needs")for(let{provider:c}of r.needs.inputs){let a=t.provider({evidence:c});a!==null&&i.push(a)}let o=r.kind==="needs"?r.needs.step:r.kind==="transition"&&r.row.op==="entered"?r.row.step:null;for(let{participant:c,checks:a}of o===null?[]:e.steps[o].assignments)for(let u of a){let d=t.provider({check:u,participant:c});d!==null&&i.push(d)}return[...new Set(i)]}var Wt=n=>({...n,actions:{...n.actions,verdicts:[],options:[],restart:{retry:{required:[],allowed:[]},options:{}}}});var Hi=y.strictObject({definitions:y.array(y.custom(at,{error:"Expected a definition made by defineWorkflow"})),restrictions:y.record(y.string().min(1),ln),coordination:ln,prerequisites:y.record(y.string().min(1),y.custom(n=>typeof n=="function",{error:"Expected a prerequisite check"})),roles:y.record(y.string().min(1),y.custom(n=>typeof n=="function",{error:"Expected a role binding"})),modelProviders:y.array(y.string().min(1)),checks:y.record(y.string().min(1),At),evidence:y.record(y.string().min(1),Nt)}),I=y.string().min(1),Qi=y.strictObject({id:I,inputId:I,definition:I,input:N,conversation:y.strictObject({environment:y.strictObject({cwd:y.string(),read_roots:y.array(y.string())}),installations:y.array(y.custom())}),models:y.record(I,y.strictObject({provider:I,model:I,thinkingEffort:Oe.nullable()})).optional(),signal:y.instanceof(AbortSignal).optional()}),Yi=y.strictObject({operationId:I,instanceId:I,decision:I,verdict:y.enum(["approve","revise"]),option:I.optional(),feedback:y.string().optional(),restart:y.array(I).optional()}),Gi=y.strictObject({operationId:I,instanceId:I,reason:y.string()}),Zi=3,$e=class extends Error{constructor(e){super(`The workflow changed while its Stop was prepared: ${e}`),this.name="StaleStopError"}},E=class extends Error{constructor(e){super(e),this.name="WorkflowRefusedError"}},Xi=3,Pt="stale-wake",Lt="A workflow opens only on a new root conversation, never on a fork or a delegated thread",es=3,wn=new AbortController().signal,ve="The workflow instance could not be repaired",Fe=Tn({key:"workflow",version:1,schema:y.strictObject({references:y.strictObject({id:y.string(),step:y.string().nullable(),participants:y.record(y.string(),y.strictObject({context:y.enum(["conversation","isolated"]),open:y.boolean()}))}),terminal:y.boolean(),checks:y.array(y.strictObject({thread:y.string(),check:y.string(),pin:y.strictObject({config:N}).nullable()})),runnable:y.array(y.string())}).nullable(),applies:z,compute(n,e){let t=n.state(q,e);return t.opened===null?null:{references:he(t),terminal:t.terminal!==null,checks:ts(t),runnable:t.cancellation===null?Tt(t):[]}}}),ze=class extends Vi{static inject=["threads","journal","tools","presets"];config;registered;entries;cleanups=new Map;cancelling=new Set;unrepaired=new Map;sends=new Set;passes=new Map;closing=!1;repairing=!1;participants;refreshed=Promise.resolve([]);providers;constraint;entryRefusals=new Map;constructor(e,t){super(e,"workflows"),this.config=Hi.parse(t);let{definitions:i,restrictions:s,prerequisites:r,roles:o,checks:c,evidence:a}=this.config,u={restrictions:new Set(Object.keys(s)),prerequisites:new Set(Object.keys(r)),roles:new Set(Object.keys(o)),checks:new Set(Object.keys(c)),evidence:new Set(Object.keys(a))};this.providers=new De({checks:c,evidence:a});let d=e.get("delegations")!==void 0,f=new Map;for(let p of i){let h=p.id;if(f.has(h))throw new J(h,["its id is registered twice"]);let w=dt(p,u);if(Object.values(p.participants).some(({context:O})=>O==="isolated")&&!d&&w.push("isolated participants run as delegation tasks: mount delegations before workflows"),w.length>0)throw new J(h,w);f.set(h,p)}this.registered=f;let l=Object.freeze([...this.config.modelProviders]);this.entries=ue([...f.values()].map(({id:p,input:h,participants:w})=>({id:p,input:h,participants:Object.fromEntries(Object.entries(w).flatMap(([k,O])=>O.context==="isolated"?[[k,{provider:O.model.provider,model:O.model.model,thinkingEffort:O.model.thinkingEffort??null,configurable:O.model.configurable}]]:[])),modelProviders:l}))),this.participants=new Le(e,{providers:this.providers,checks:c,executable:p=>this.executable(p),runnable:p=>this.runnable(p),quarantined:p=>this.quarantined(p),reconcile:p=>this.track(this.reconcile(p)),ended:(p,h)=>{(h||this.cancelling.has(p))&&this.track(this.reconcile(p))},refused:(p,h,w)=>this.refused(p,h,w)}),e.effect(()=>e.journal.register(q));let g={restrictions:s,coordination:this.config.coordination,executable:p=>this.executable(p)};_t(e,g),jt(e,g),Ct(e,g),this.refreshed=Fe.register(e).refreshed,e.effect(()=>e.journal.subscribe(p=>this.onRow(p))),e.effect(()=>e.threads.onRunEvent(({threadId:p})=>{this.cancelling.has(p)&&this.track(this.holding(p,()=>this.finishCleanup(p)))})),e.effect(()=>{let p=e.threads.registerOwner({owner:T,constraint:{applies:h=>this.governs(h),evaluate:h=>this.evaluate(h)},repair:()=>this.repair()});return this.constraint=p,()=>{this.constraint=void 0,p.dispose()}}),e.effect(()=>this.providers.watch(()=>this.providerChanged())),e.effect(()=>this.participants.register()),Sn(e,()=>this.shutdown())}catalog(){return this.entries}definition(e){return this.registered.get(e)}async opening(e){let t=Qi.parse(e),{id:i,inputId:s,definition:r,input:o,models:c={},signal:a=wn}=t,u=this.definition(r);if(u===void 0)throw new E(`No workflow ${r} is registered`);let d=Ne(u,"$input").safeParse(o);if(!d.success)throw new E(`The input does not match the workflow's input schema: ${y.prettifyError(d.error)}`);let f=this.proposed(t.conversation),l=await this.pins(u,f,c,a),g=Dn(u,o,s,l),p={...q.init(),instance:i,opened:g},{plan:h}=await this.withEvidence(i,l,k=>{let O=Me(p,u,{instance:i,step:u.entry,cause:{kind:"opening"},activation:s,...k===void 0?{}:{evidence:k}});return"needs"in O?{kind:"needs",needs:O.needs}:"refused"in O?{kind:"refused",reason:O.refused}:{kind:"row",...O}},a);if(h.kind==="refused")throw new E(`The entry step cannot be entered: ${h.reason}`);let w=[g,h.row];return k=>{if(k.kind==="send")throw new Error("Workflow opening rows only open a thread");let{threadId:O}=k,v=k.kind==="fork"?Lt:this.openingRefusal(u,{instance:i,inputId:s,conversation:f},k)??this.pinsProblem(l);return v!==null?{refuse:{message:v}}:w.map(b=>L.entry(b,{threadId:O}))}}proposed(e){try{let t=this.ctx.presets.installationsFor(e.installations,"creation").map(({preset_id:i,config:s},r)=>({preset_id:i,config:s,lifetime:e.installations[r].lifetime}));return{environment:bn(e.environment),installations:t}}catch(t){throw new E(`The proposed conversation is refused: ${de(t)}`)}}async pins(e,t,i,s){for(let[a,u]of Object.entries(i)){let d=e.participants[a];if(d===void 0)throw new E(`The workflow has no participant ${a}`);if(d.context==="conversation")throw new E(`Participant ${a} runs on the conversation's route: it takes no model choice (${u.provider}/${u.model})`);if(!d.model.configurable)throw new E(`Participant ${a} runs on the model its workflow fixes`)}let r=async(a,u,d,f)=>{let l=await this.providers.pin(a,u,d,s);if("refused"in l)throw new E(`${f}: ${l.refused}`);return l},o={};for(let[a,u]of Object.entries(e.participants)){let d=u.context==="isolated"?this.configuration(a,u,t,i[a]):null,f=d===null?t:{environment:d.environment,installations:d.installations.map(({preset_id:g,config:p,lifetime:h})=>({preset_id:g,config:p,lifetime:h}))},l={};for(let g of Object.values(e.steps))for(let p of g.assignments)if(p.participant===a)for(let h of p.checks)l[h]??=await r("check",h,f,`Participant ${a}`);o[a]={configuration:d,checks:l}}let c={};for(let a of Object.values(e.steps))for(let u of Object.values(a.inputs))"evidence"in u&&(c[u.evidence]??=await r("evidence",u.evidence,t,"Evidence"));return{participants:o,evidence:c}}configuration(e,t,i,s){let r=u=>{throw new E(`Participant ${e}: ${u}`)},{model:o}=t,c=s===void 0?{provider:o.provider,model:o.model,thinking_effort:o.thinkingEffort??null}:{provider:s.provider,model:s.model,thinking_effort:s.thinkingEffort};this.config.modelProviders.includes(c.provider)||r(`model provider ${c.provider} is not offered by the host`);let a=this.config.roles[t.role](i);if("refused"in a)return r(`role ${t.role} refuses this conversation: ${a.refused}`);for(let u of a.installations)(u.lifetime!=="defining"||u.installationId===void 0)&&r(`role ${t.role}'s installation of ${u.presetId} must be defining and name its id`);try{return{agent:a.agent,route:c,environment:bn(a.environment),installations:this.ctx.presets.initialInstallations(a.installations,a.agent)}}catch(u){return r(`role ${t.role} resolves a configuration that is refused: ${de(u)}`)}}openingRefusal(e,t,i){let{threadId:s,runId:r,inputId:o,opening:c}=i;if(s!==t.instance)return`The workflow opens on thread ${t.instance}, not ${s}`;if(c.origin!==null)return Lt;let{spec:a}=c;if(!a.requires.includes(T))return`The conversation must require ${T}`;if(o!==t.inputId)return`The conversation's first input must be ${t.inputId}`;let u=e.steps[e.entry].assignments.some(({participant:f})=>e.participants[f]?.context==="conversation");if(u&&r===null)return"The conversation's creation must wake it: its entry step is its own";if(!u&&r!==null)return"The conversation's creation must not wake it: its entry step is not its own, and nothing runs it merely to start participants";let d={environment:bn(a.environment),installations:a.installations.map(({preset_id:f,config:l,lifetime:g})=>({preset_id:f,config:l,lifetime:g}))};if(U(d)!==U(t.conversation))return"The conversation is not created as the workflow's opening was prepared for: its environment or installations differ";for(let f of e.prerequisites){let l=this.config.prerequisites[f](c);if(l!==null)return`Prerequisite ${f} is not met: ${l}`}return null}read(e){return this.view(e)}decide(e,t){let i=Yi.parse(e),{instanceId:s}=i,r={operationId:i.operationId,decision:i.decision,verdict:i.verdict,option:i.option??null,feedback:i.feedback??null,restart:i.restart??[],actor:t};return this.retaining(this.subtree(s),async()=>{this.refuseQuarantined(s);for(let o=0;o<Xi;o+=1){let{plan:c,evidence:a}=await this.prepareDecision(s,r);if(c.kind==="replay")return this.view(s);if(c.kind==="refused")throw new E(c.reason);let[u,d]=c.rows,f=this.transition(s,d,u),l=w=>f.rootWork||On(this.ctx.journal.state(ne,w).requests),g=l(s)?{releases:!0,chain:"person"}:void 0,p=await this.send(s,f,g,({threadId:w,runId:k})=>{let O=this.quarantined(w);if(O!==null)return{refuse:{message:`${ve}: ${O}`}};let v=this.planDecision(w,r,a);if(v.kind!=="rows")return{refuse:{message:v.kind==="refused"?v.reason:"already recorded"}};let b=this.entryProblem(w,v.rows);return b!==null?{refuse:{message:`the selected step cannot be entered: ${b}`}}:U(v.rows)!==U(c.rows)?{refuse:{message:"The instance changed while the decision was written: decide again"}}:l(w)!==(k!==null)?{refuse:{message:"A Stop changed while the decision was written",code:Pt}}:v.rows.map(j=>L.entry(j,{threadId:w}))});if(p.status==="queued")return this.view(s);if(p.status==="refused"&&p.refusal.code===Pt)continue;this.refuseQuarantined(s);let h=this.planDecision(s,r,a);if(h.kind==="replay")return this.view(s);throw h.kind==="refused"?new E(h.reason):new E(p.status==="refused"?p.refusal.message:`The input of decision ${r.decision} was already sent`)}throw new E("The conversation was stopped and resumed while deciding: decide again")})}cancel(e,t){let{operationId:i,instanceId:s,reason:r}=Gi.parse(e);return this.holding(s,async()=>{this.refuseQuarantined(s);let{value:o}=await this.ctx.journal.transact(s,()=>{let c=this.quarantined(s);if(c!==null)return{rows:[],value:`${ve}: ${c}`};let a=mt(this.state(s),{operationId:i,reason:r,actor:t});return{rows:a.kind==="rows"?a.rows.map(u=>L.entry(u,{threadId:s})):[],value:a.kind==="refused"?a.reason:null}});if(o!==null)throw new E(o);return await this.cleanUp(s),this.view(s)})}async stop(e,t,i){for(let s=0;;s+=1){let r=await this.ctx.threads.retain(()=>[e],{lastRelease:"keep"});try{let o=this.stopping(e),c=await this.ctx.threads.retain(()=>o.children,{lastRelease:"keep"});try{await i(a=>{if(a!==e)throw new Error(`A Stop of thread ${e} was written on ${a}`);return this.stopRows(e,t,o)});return}finally{c.release()}}catch(o){if(!(o instanceof $e)||s+1>=Zi)throw o}finally{r.release()}}}stopping(e){let t=this.ctx.journal.summary(e);if(t===void 0||!z(t)||this.quarantined(e)!==null)return{attempt:null,children:[]};let i=this.ctx.journal.state(q,e),s=V(i).map(({thread:r})=>r).filter(r=>this.ctx.journal.summary(r)!==void 0);return{attempt:i.current?.entered.attempt??null,children:s}}stopRows(e,t,i){let s=this.stopping(e);if(s.attempt!==i.attempt)throw new $e(`it met attempt ${s.attempt}`);let r=s.children.filter(a=>!this.ctx.journal.isLoaded(a));if(r.length>0)throw new $e(`${r.join(", ")} not loaded`);if(s.attempt===null||this.quarantined(e)!==null)return[];let o=this.ctx.journal.state(q,e),c=kt(this.ctx.journal,o,this.procedure(o),t);return c===null?[]:[L.entry(c,{threadId:e})]}state(e){let t=this.ctx.journal.state(q,e);if(t.opened===null)throw new E(`No workflow instance is open on thread ${e}`);return t}view(e){let t=this.state(e),{state:i}=this.ctx.threads.inspect(e,{basis:"catalog"}),{pins:s}=t.opened,r=It(t,this.definition(t.opened.definition),{barrier:this.barrier(e,t),starts:this.participants.states(e,t),settled:i==="idle"||i==="held",provider:o=>"check"in o?this.providers.problem("check",o.check,s.participants[o.participant]?.checks[o.check]):this.providers.problem("evidence",o.evidence,s.evidence[o.evidence]),entryRefusal:this.entryRefusals.get(e)??null});return ue(structuredClone(r))}barrier(e,t){return xt(this.ctx,t,this.participants.starting(e))}executable(e){return it(e,this.definition(e.opened.definition))}procedure(e){return this.executable(e)??e.opened.canonical}planDecision(e,t,i){let s=this.state(e);return ht(s,this.executable(s),t,this.barrier(e,s),i)}async prepareDecision(e,t){try{return await this.withEvidence(e,this.state(e).opened.pins,i=>this.planDecision(e,t,i),wn)}catch(i){throw i instanceof K?new E(`The decision's evidence: ${i.message}`):i}}owed(e,t){let i=this.ctx.journal.state(q,e);if(i.opened===null)return{kind:"none"};let s=this.executable(i);return s===void 0?{kind:"none"}:Z(i,s,t)}async advance(e){let t;try{let u=this.ctx.journal.state(q,e);if(u.opened===null)return;t=await this.withEvidence(e,u.opened.pins,d=>this.owed(e,d),wn)}catch(u){if(u instanceof K||u instanceof E)return;throw u}let{plan:i,evidence:s}=t;if(i.kind==="none")return;if(i.kind==="refused"){s!==void 0&&this.entryRefusals.set(e,{transition:s.transition,reason:i.reason});return}let{row:r}=i,o=this.transition(e,r,null),c=o.rootWork?{releases:!1,chain:"fresh"}:void 0,a=await this.send(e,o,c,()=>{let u=this.quarantined(e);if(u!==null)return{refuse:{message:`${ve}: ${u}`}};let d=this.owed(e,s);if(d.kind!=="transition"||U(d.row)!==U(r))return{refuse:{message:"The transition is no longer owed"}};let f=this.entryProblem(e,[r]);return f!==null?{refuse:{message:f}}:[L.entry(r,{threadId:e})]});a.status==="refused"&&this.entryRefusals.set(e,{transition:r.transition,reason:a.refusal.message})}entryProblem(e,t){let i=this.state(e),{pins:s}=i.opened,r=this.procedure(i);for(let o of t){if(o.op!=="entered")continue;let c=r.steps[o.step];for(let a of Object.values(c?.inputs??{})){if(!("evidence"in a))continue;let u=this.providers.problem("evidence",a.evidence,s.evidence[a.evidence]);if(u!==null)return u}for(let a of o.assignments){let{participant:u}=a;for(let d of a.context==="isolated"?a.checks:[]){let f=this.providers.problem("check",d,s.participants[u]?.checks[d]);if(f!==null)return f}}}return null}pinsProblem(e){for(let{checks:t}of Object.values(e.participants))for(let[i,s]of Object.entries(t)){let r=this.providers.problem("check",i,s);if(r!==null)return r}for(let[t,i]of Object.entries(e.evidence)){let s=this.providers.problem("evidence",t,i);if(s!==null)return s}return null}async withEvidence(e,t,i,s){let r;for(let o=0;o<es;o+=1){let c=i(r);if(c.kind!=="needs")return{plan:c,evidence:r};let{needs:a}=c,u=await Promise.all(a.inputs.map(async({input:d,provider:f})=>[d,await this.providers.acquire(f,t.evidence[f],{instance:e,attempt:a.attempt,step:a.step,input:d,signal:s})]));r={transition:a.transition,attempt:a.attempt,inputs:new Map(u)}}throw new E("The instance moved on each time its entry evidence was acquired: try again")}send(e,t,i,s){let r=this.ctx.threads.get(e);if(r===void 0)throw new Error(`Unknown thread: ${e}`);let{input:o}=t;return r.send(o.message,{id:o.id,source:o.source,target:"next-turn",...i===void 0?{}:{wake:i},rows:s})}transition(e,t,i){let s=this.state(e),r=this.executable(s),o=t.op==="entered"?t.assignments.find(({participant:d})=>r.participants[d]?.context==="conversation"):void 0,c=i===null?void 0:s.attempts.get(i.attempt)?.entered.step,a=[`Workflow ${r.id}:`];if(i!==null){let d=i.option===null?"":` (${i.option})`,f=i.feedback===null?"":`, with this feedback: ${i.feedback}`;a.push(`step ${c} ${Pe(i.verdict)}${d}${f}.`)}t.op==="completed"?a.push("The workflow is complete; this conversation goes on as ordinary chat."):o!==void 0?a.push(`Step ${t.step} is now yours, as assignment ${o.id}; its instructions come with your next step.`):t.assignments.length===0?a.push(`Step ${t.step} now waits for a decision.`):a.push(`Step ${t.step} is being worked by other participants.`);let u=Ee(t.transition);return{row:t,input:{id:u,message:{role:"developer",content:a.join(" ")},source:Vn.record({transition:t.transition,assignment:o?.id??null,decision:i?.decision??null})},rootWork:o!==void 0}}cleanUp(e){let t=this.cleanups.get(e);if(t!==void 0)return t;this.cancelling.add(e);let i=this.runCleanup(e).finally(()=>this.cleanups.delete(e));return this.cleanups.set(e,i),i}async runCleanup(e){if(this.quarantined(e)!==null)return;let{cancellation:t,terminal:i}=this.state(e);if(t===null||i!==null)return;let s=this.ctx.threads.get(e),r=`The workflow was cancelled: ${t.reason}`,o=this.ctx.get("delegations");await s.cancel(r,{intent:"cleanup",...o===void 0?{}:{rows:o.stopObligationRows(r)}}),await o?.retryStops(e);let c=this.ctx.get("commands");if(c!==void 0){let a=this.ctx.journal.origins.subtree(e,{self:!0});await Promise.all(a.map(({thread_id:u})=>c.stopAll({threadId:u},"its workflow was cancelled")))}await this.ctx.journal.flush(),await this.finishCleanup(e)}async finishCleanup(e){await this.ctx.journal.transact(e,()=>{let t={rows:[],value:void 0};if(this.quarantined(e)!==null)return t;let i=this.state(e);if(i.terminal!==null||i.cancellation===null)return this.cancelling.delete(e),t;let{state:s,operation:r}=this.ctx.threads.inspect(e,{basis:"catalog"});if(s!=="idle"&&s!=="held"||r!==null)return t;let{draining:o,unresolved:c}=this.barrier(e,i).cleanup;return o.length>0||c.length>0?t:{rows:[L.entry({op:"cancelled",operation_id:i.cancellation.operation_id},{threadId:e})],value:void 0}})}governs(e){if(z(e))return!0;if(e.origin===null)return!1;let t=this.ctx.journal.origins.callers(e.thread_id).threads.at(-1);return t!==void 0&&t.origin===null&&z(t)}evaluate(e){let t=this.ctx.journal.origins.callers(e).threads,i=t.at(-1),s=i===void 0?null:this.quarantined(i.thread_id);if(s!==null)return{kind:"hold",reason:`${ve}: ${s}`};let r=i===void 0?void 0:Fe.read(this.ctx.journal,i.thread_id);if(r?.status!=="available")return{kind:"hold",reason:`Its workflow instance is unreadable: ${r?.status==="unavailable"?r.reason:"its instance state has not been computed"}`};let o=r.value;if(o===null)return{kind:"allow"};if(o.terminal)return{kind:"allow"};let{references:c}=o,a=this.definition(c.id),u=me(c,a);if(u!==null)return{kind:"hold",reason:`Its workflow cannot continue: ${u}`};let d=t.find(({origin:f})=>ae(f,i.thread_id))?.thread_id??i.thread_id;for(let{thread:f,check:l,pin:g}of o.checks){if(f!==d)continue;let p=this.providers.problem("check",l,g??void 0);if(p!==null)return{kind:"hold",reason:`Its workflow's ${p}`}}return{kind:"allow"}}governedChanged(e){for(let{thread_id:t}of this.ctx.journal.origins.subtree(e,{self:!0}))this.constraint?.changed(t),this.participants.changed(t)}runnable(e){let t=Fe.read(this.ctx.journal,e);if(t.status==="unavailable")return{unavailable:t.reason};if(t.status==="not-applicable")return{unavailable:"its instance state has not been computed"};let i=t.value;return i===null?{unavailable:"no instance is open on it"}:i.terminal?"allow-all":i.runnable}providerChanged(){let e=new Set(this.entryRefusals.keys());this.entryRefusals.clear();for(let t of this.ctx.journal.summaries()){let i=Fe.read(this.ctx.journal,t.thread_id);if(i.status!=="available")continue;let s=i.value;s!==null&&s.checks.length>0&&(this.governedChanged(t.thread_id),e.add(t.thread_id)),this.ctx.journal.isLoaded(t.thread_id)&&this.participants.held(t.thread_id)&&e.add(t.thread_id)}for(let t of e)this.track(this.reconcile(t))}onRow(e){let t=this.ctx.journal.summary(e.thread_id);if(t===void 0||(z(t)&&t.origin===null&&(e.type==="ext"&&this.governedChanged(e.thread_id),(L.read(e)!==void 0||e.type==="turn/end")&&this.track(this.reconcile(e.thread_id))),!ns.has(e.type)))return;let i=this.ctx.journal.origins.callers(e.thread_id).threads.at(-1),s=i?.thread_id;s===void 0||!z(i)||(this.cancelling.has(s)?this.track(this.holding(s,()=>this.finishCleanup(s))):s!==e.thread_id&&this.ctx.journal.isLoaded(s)&&this.participants.waiting(s)&&this.track(this.reconcile(s)))}reconcile(e){if(this.closing||!this.repairing||this.quarantined(e)!==null)return Promise.resolve();let t=this.passes.get(e);if(t!==void 0)return t.again=!0,t.done;let i={again:!0,done:Promise.resolve()};return i.done=(async()=>{try{for(;i.again&&!this.closing;)i.again=!1,await this.holding(e,async()=>{let s=this.ctx.journal.state(q,e);if(!(s.opened===null||s.terminal!==null)){if(s.cancellation!==null){this.cancelling.has(e)&&await this.finishCleanup(e);return}await this.observe(e),await this.advance(e),this.participants.dispatch(e)}})}finally{this.passes.delete(e)}})(),this.passes.set(e,i),i.done}async refused(e,t,i){await this.retaining(this.subtree(e),()=>this.ctx.journal.transact(e,()=>{let s={rows:[],value:void 0};if(this.quarantined(e)!==null)return s;let r=this.state(e),o=bt(this.ctx.journal,r,t,i);if(o===null)return s;let c=o.observations[0].thread;return this.ctx.journal.summary(c)!==void 0&&this.ctx.get("delegations")?.continuation(c)?.kind!=="refusal"?s:{rows:[L.entry(o,{threadId:e}),...pn(this.ctx,r,o)],value:void 0}}))}async observe(e){let t=this.ctx.journal.state(q,e),i=[...new Set(V(t).map(({thread:r})=>r))].filter(r=>this.ctx.journal.summary(r)!==void 0);if(i.length===0)return;let s=await this.ctx.threads.retain(()=>i,{lastRelease:"keep"});try{await this.ctx.journal.transact(e,()=>{if(this.quarantined(e)!==null)return{rows:[],value:void 0};let r=this.state(e);return{rows:wt(this.ctx.journal,r).flatMap(c=>[L.entry(c,{threadId:e}),...pn(this.ctx,r,c)]),value:void 0}})}finally{s.release()}}track(e){let t=e.catch(i=>this.ctx.reportError(i)).finally(()=>this.sends.delete(t));this.sends.add(t)}async repair(){for(let t of await this.refreshed.catch(()=>[]))this.ctx.reportError(new Error(`The workflow instance of thread ${t} could not be read into the catalog`));this.repairing=!0;let e=this.ctx.journal.summaries().filter(t=>z(t)&&t.origin===null).map(t=>t.thread_id);for(let t of e)try{await(await this.holding(t,()=>{let s=this.ctx.journal.state(q,t);return s.opened!==null&&s.terminal===null&&s.cancellation!==null})?this.holding(t,()=>this.cleanUp(t)):this.reconcile(t))}catch(i){let s=jn(i);this.unrepaired.set(t,s),this.ctx.reportError(new Error(`The workflow instance of thread ${t} could not be repaired: ${s}`,{cause:i})),this.governedChanged(t)}}quarantined(e){return this.unrepaired.get(e)??null}refuseQuarantined(e){let t=this.quarantined(e);if(t!==null)throw new E(`${ve}: ${t}`)}holding(e,t){return this.retaining(()=>[e],t)}async retaining(e,t){let i=await this.ctx.threads.retain(e,{lastRelease:"keep"});try{return await t()}finally{i.release()}}subtree(e){return()=>this.ctx.journal.origins.subtree(e,{self:!0}).map(({thread_id:t})=>t)}async shutdown(){this.closing=!0,await Promise.allSettled([this.providers.drain(),this.participants.shutdown(),...this.sends,...this.cleanups.values(),...[...this.passes.values()].map(({done:e})=>e)])}},ns=new Set(["turn/end","run/settled","thread/cancel","inbox/spliced","ext"]),bn=n=>({cwd:Ve(n.cwd,"A conversation cwd"),read_roots:[...new Set(n.read_roots.map(e=>Ve(e,"A read root")))]});function ts(n){if(!_(n)||n.current===null)return[];let{pins:e}=n.opened;return n.current.assignments.flatMap(({participant:t,thread:i,checks:s,state:r})=>r==="accepted"||r==="superseded"?[]:s.map(o=>{let c=e.participants[t]?.checks[o];return{thread:i,check:o,pin:c===void 0?null:{config:Y(c.config)}}}))}var lo={name:"lagent:workflows",async apply(n,e){xn(n,"workflows"),await n.plugin(ze,e)}};export{P as SUBMIT_TOOL,T as WORKFLOWS_OWNER,J as WorkflowDefinitionError,E as WorkflowRefusedError,li as defineWorkflow,lo as workflows};
3
+ - `)}`),this.name="WorkflowDefinitionError",this.workflow=e,this.problems=t}},M=v.string().min(1),pt=v.custom(n=>n instanceof v.ZodType,{error:"Expected a zod schema"}),ki=v.strictObject({id:M,input:pt,entry:M,prerequisites:v.array(M).default([]),participants:v.record(M,v.discriminatedUnion("context",[v.strictObject({context:v.literal("conversation")}),v.strictObject({context:v.literal("isolated"),lifetime:v.enum(["instance","attempt"]),role:M,model:v.strictObject({provider:M,model:M,thinkingEffort:xe.optional(),configurable:v.boolean()})})])),steps:v.record(M,v.strictObject({inputs:v.record(M,v.union([v.strictObject({source:v.union([v.literal("$input"),v.strictObject({step:M,participant:M.optional()})]),mode:v.enum(["current","historical"]),optional:v.boolean()}),v.strictObject({evidence:M,optional:v.boolean()})])),assignments:v.array(v.strictObject({participant:M,reads:v.array(M),prompt:v.string(),output:pt,checks:v.array(M).default([])})),gate:v.boolean(),options:v.array(v.strictObject({name:M,on:v.array(v.enum(["gate","recovery"])).min(1)})).default([]),restriction:M.nullable(),maxAttempts:v.number().int().positive(),edges:v.array(v.strictObject({when:v.union([v.literal("otherwise"),v.array(v.strictObject({field:v.string(),equals:v.union([v.string(),v.number(),v.boolean(),v.null()])}))]),to:M}))}))}),sn=new WeakMap,ht=n=>typeof n=="object"&&n!==null&&sn.has(n);function Ne(n,e){let t=sn.get(n),i=e==="$input"?t?.input:t?.outputs.get(e.step)?.get(e.participant);if(i===void 0)throw new Error(`Workflow ${n.id} has no schema for ${JSON.stringify(e)}`);return i}function vi(n){let e=ki.safeParse(n),t=yi(n);if(!e.success)throw new J(t,e.error.issues.map(a=>`${a.path.map(String).join(".")||"(definition)"}: ${a.message}`));let i=Oi(e.data);if(i.problems.length>0)throw new J(t,i.problems);let s=i.data,r=Si(s);if(r.length>0)throw new J(t,r);let o=ue($i(s));return sn.set(o,{input:s.input,outputs:new Map(Object.entries(s.steps).map(([a,{assignments:c}])=>[a,new Map(c.map(({participant:d,output:u})=>[d,u]))]))}),o}var yi=n=>{let{id:e}=typeof n=="object"&&n!==null?n:{};return typeof e=="string"?e:"?"};function $i(n){let{input:e,participants:t,steps:i,...s}=n;return JSON.parse(JSON.stringify({...s,input:Ve(e),participants:t,steps:Object.fromEntries(Object.entries(i).map(([r,{assignments:o,...a}])=>[r,{...a,assignments:o.map(({output:c,...d})=>({...d,output:Ve(c)}))}]))}))}function Oi(n){let e=[],t=(s,r)=>{let o=ft(s,r);return e.push(...o.problems),o.schema??s},i=Object.fromEntries(Object.entries(n.steps).map(([s,r])=>[s,{...r,assignments:r.assignments.map(o=>({...o,output:t(o.output,`steps.${s}: ${o.participant}'s output`)}))}]));return{problems:e,data:{...n,input:t(n.input,"input"),steps:i}}}var xi=["reason","verdict","option"];function mt(n){if(!n.startsWith("/"))return;let e=n.slice(1).split("/");if(!e.some(t=>/~(?![01])/.test(t)))return e.map(t=>t.replaceAll("~1","/").replaceAll("~0","~"))}function ji(n,e){let t=n;for(let i of mt(e)??[])if(Array.isArray(t)){if(!/^(0|[1-9]\d*)$/.test(i))return;t=t[Number(i)]}else if(typeof t=="object"&&t!==null&&Object.hasOwn(t,i))t=t[i];else return;return t}function be(n,e){let t=gt(n,e);if(t===void 0)throw new Error("No edge matched: a registered step always ends with otherwise");return t}var gt=(n,e)=>n.find(t=>t.when==="otherwise"||t.when.every(({field:i,equals:s})=>ji(e,i)===s)),rn=(n,e)=>({outputs:{},decision:{reason:"recovery",verdict:"revise",option:n,feedback:e}});function on(n,e){let t=(i,s)=>n.when!=="otherwise"&&n.when.some(r=>r.field===i&&r.equals===s);return!t("/decision/reason","recovery")||!t("/decision/option",e)?`its first matching edge does not guard /decision/reason = recovery and /decision/option = ${e}`:n.to===ce?"its first matching edge completes the instance, which a recovery cannot":null}function Si(n){let e=[],{steps:t,participants:i}=n,s=a=>Object.hasOwn(t,a),r=Object.entries(i).filter(([,a])=>a.context==="conversation");r.length>1&&e.push(`participants: more than one conversation participant (${r.map(([a])=>a).join(", ")})`),s(n.entry)||e.push(`entry: unknown step ${n.entry}`);let o=new Set;for(let a of n.prerequisites)o.has(a)&&e.push(`prerequisites: ${a} is listed twice`),o.add(a);for(let[a,c]of Object.entries(t)){let d=`steps.${a}`,u=h=>{e.push(`${d}: ${h}`)};a.startsWith("$")&&u('a step name may not start with "$"');for(let[h,g]of Object.entries(c.inputs)){if(!("source"in g)||g.source==="$input")continue;let{step:O,participant:y}=g.source;if(!s(O)){u(`input ${h} binds unknown step ${O}`);continue}y!==void 0&&!t[O].assignments.some(b=>b.participant===y)&&u(`input ${h} binds participant ${y}, which step ${O} does not assign`),O===a&&g.mode==="current"&&!g.optional&&u(`input ${h} requires the step's own current result, which entering the step retires`)}c.assignments.length===0&&!c.gate&&u("a step with no assignments is a pure gate and must set gate: true");let f=new Set;for(let h of c.assignments){let{participant:g}=h;Object.hasOwn(i,g)||u(`assigns unknown participant ${g}`),f.has(g)&&u(`assigns participant ${g} twice`),f.add(g),h.checks.length>0&&i[g]?.context==="conversation"&&u(`${g}'s assignment lists checks, which only an isolated participant's may`),new Set(h.checks).size!==h.checks.length&&u(`${g}'s assignment lists a check twice`);let O=new Set;for(let y of h.reads)Object.hasOwn(c.inputs,y)||u(`${g} reads unknown input ${y}`),O.has(y)&&u(`${g} reads input ${y} twice`),O.add(y)}let l=new Map;for(let h of c.options)l.has(h.name)&&u(`option ${h.name} is declared twice`),l.set(h.name,h.on),new Set(h.on).size!==h.on.length&&u(`option ${h.name} lists a reason twice`),h.on.includes("gate")&&!c.gate&&u(`option ${h.name} is declared on gate, but the step has no gate`);let w=c.options.some(h=>h.on.includes("recovery"));if(w){let h=c.assignments.every(({participant:g})=>i[g]?.context==="isolated");(c.assignments.length===0||!h)&&u("a recovery option needs every assignment of the step isolated: a mixed step recovers only by retry or cancel");for(let{name:g,on:O}of c.options){if(!O.includes("recovery"))continue;let y=gt(c.edges,rn(g,null)),b=y===void 0?"no edge matches it":on(y,g);b!==null&&u(`option ${g} on recovery: ${b}`)}}(c.edges.length===0||c.edges.at(-1).when!=="otherwise")&&u("the last edge must be otherwise");let p=new Map;c.edges.forEach((h,g)=>{let O=`edge ${g}`;if(h.to!==ce&&!s(h.to)&&u(`${O} targets unknown step ${h.to}`),h.when==="otherwise"){g!==c.edges.length-1&&u(`${O} is otherwise but not last`);return}if(h.when.length===0){u(`${O} has no conditions (use otherwise)`);return}let y=new Map;for(let{field:x,equals:S}of h.when){y.has(x)&&y.get(x)!==S&&u(`${O} tests ${x} for two different values`),y.set(x,S);let L=_i(x,S,c,f,l,w);L!==null&&u(`${O}: ${L}`)}let b=U([...y].sort(([x],[S])=>x<S?-1:x>S?1:0)),k=p.get(b);k!==void 0?u(`${O} repeats the predicate of edge ${k}`):p.set(b,g)})}if(s(n.entry)){let a=new Set([n.entry]),c=[n.entry];for(let d=c.shift();d!==void 0;d=c.shift())for(let{to:u}of t[d].edges)u!==ce&&s(u)&&!a.has(u)&&(a.add(u),c.push(u));for(let d of Object.keys(t))a.has(d)||e.push(`steps.${d}: not reachable from the entry ${n.entry}`)}return e}function _i(n,e,t,i,s,r){let o=mt(n);if(o===void 0)return`${n} is not a JSON Pointer`;let[a,c,...d]=o;if(a==="outputs"){if(c===void 0||!i.has(c))return`${n} names no participant the step assigns`;let u=t.assignments.find(l=>l.participant===c).output,f=ae(u,d);return f.length===0?`${n} is not a field of ${c}'s output`:f.some(l=>v.safeParse(l,e).success)?null:`${n} can never equal ${JSON.stringify(e)}`}if(a==="decision"&&d.length===0&&xi.includes(c))switch(c){case"verdict":return e==="approve"||e==="revise"?null:`${n} can never equal ${JSON.stringify(e)}`;case"reason":return e==="exhaustion"?null:e==="gate"?t.gate?null:`${n} tests gate on a step without a gate`:e==="recovery"?r?null:`${n} tests recovery on a step without a recovery option`:`${n} can never equal ${JSON.stringify(e)}`;default:return e===null||typeof e=="string"&&s.has(e)?null:`${n} tests the undeclared option ${JSON.stringify(e)}`}return`${n} is not a routing field (/outputs/<participant>/..., /decision/reason, /decision/verdict or /decision/option)`}function wt(n,e){let t=[],i=(s,r,o)=>{t.push(`${s}: ${r} ${o} is not bound by the host`)};for(let s of n.prerequisites)e.prerequisites.has(s)||t.push(`prerequisites: ${s} is not bound by the host`);for(let[s,r]of Object.entries(n.participants))r.context==="isolated"&&!e.roles.has(r.role)&&i(`participants.${s}`,"role",r.role);for(let[s,{restriction:r,inputs:o,assignments:a}]of Object.entries(n.steps)){let c=`steps.${s}`;r!==null&&!e.restrictions.has(r)&&i(c,"restriction",r);for(let d of Object.values(o))"evidence"in d&&!e.evidence.has(d.evidence)&&i(c,"evidence provider",d.evidence);for(let{checks:d}of a)for(let u of d)e.checks.has(u)||i(c,"check provider",u)}return[...new Set(t)]}var Ei=(n,e,t)=>(n.counts.get(t)??0)>=e.steps[t].maxAttempts;function kt(n){let e=n.current;if(!_(n)||e===null||e.result===null)return;let t=n.results.get(e.result);return t.routed===null&&t.decision===null?{attempt:e,result:t}:void 0}var bt={required:[],allowed:[]};function R(n,e){let t=n.current;if(_(n)&&t!==null&&t.recovery!==null&&t.recovery.decision===null)return qi(n,e,t);let i=kt(n);if(i===void 0)return null;let{attempt:s,result:r}=i,o=e.steps[r.step];if(o===void 0)return null;let a=(d,u)=>({id:Qe(s.entered.attempt,d),attempt:s.entered.attempt,reason:d,result:r.revision,subject:tn(n,s),actions:{cleanupPending:!1,verdicts:["approve","revise"],options:an(e,r.step,d),restart:{retry:bt,options:Object.fromEntries(an(e,r.step,d).map(f=>[f,bt]))}},detail:u});if(o.gate)return a("gate",{});let{to:c}=be(o.edges,{outputs:r.outputs,decision:null});return c!==ce&&Ei(n,e,c)?a("exhaustion",{blocked:{from:r.step,to:c}}):null}var an=(n,e,t)=>(n.steps[e]?.options??[]).filter(({on:i})=>i.includes(t)).map(({name:i})=>i);function qi(n,e,t){let i=t.recovery,s=u=>{let f=e.participants[u];return f?.context==="isolated"&&f.lifetime==="instance"},r=u=>[...new Set(u)].sort(),o=r(t.assignments.map(({participant:u})=>u).filter(s)),a=r([...n.attempts.values()].flatMap(({assignments:u})=>u.filter(({context:f})=>f==="isolated").map(({participant:f})=>f))).filter(s),{step:c}=t.entered,d=an(e,c,"recovery");return{id:Qe(t.entered.attempt,"recovery"),attempt:t.entered.attempt,reason:"recovery",result:null,subject:tn(n,t),actions:{cleanupPending:!1,verdicts:["revise"],options:d,restart:{retry:{required:[],allowed:o},options:Object.fromEntries(d.map(u=>[u,{required:[],allowed:a}]))}},detail:{recovery:{cause:i.cause,detail:i.detail,candidates:i.candidates,reports:i.reports}}}}function cn(n,e){if(n.reason!=="recovery")return n;let{draining:t,unresolved:i}=e.cleanup,s=t.length>0||i.length>0,r=({allowed:a})=>({required:a.filter(c=>e.uncontinuable.has(c)),allowed:a}),{actions:o}=n;return{...n,actions:{...o,cleanupPending:s,verdicts:s?[]:o.verdicts,restart:{retry:r(o.restart.retry),options:Object.fromEntries(Object.entries(o.restart.options).map(([a,c])=>[a,r(c)]))}}}}function vt(n,e,t,i,s,r){let o=n.instance,a=Ee(o,i);if(t===ce)return{kind:"transition",row:{op:"completed",transition:a,cause:i}};let c=Re(n,e,{instance:o,step:t,cause:i,activation:qe(a),deciding:r,evidence:s});return"needs"in c?{kind:"needs",needs:c.needs}:"refused"in c?{kind:"refused",reason:c.refused}:{kind:"transition",row:c.row}}function X(n,e,t){let i=kt(n);if(i===void 0||R(n,e)!==null)return{kind:"none"};let{result:s}=i,r=be(e.steps[s.step].edges,{outputs:s.outputs,decision:null});return vt(n,e,r.to,{kind:"routed",result:s.revision},t)}function yt(n,e,t,i,s){let{operationId:r,verdict:o,option:a,feedback:c}=t;if(new Set(t.restart).size!==t.restart.length)return{kind:"refused",reason:"the restart list names a participant twice"};let d=[...t.restart].sort(),u=n.operations.get(r);if(u!==void 0)return u.op==="decided"&&u.decision===t.decision&&u.verdict===o&&u.option===a&&u.feedback===c&&u.restart.length===d.length&&u.restart.every((S,C)=>S===d[C])?{kind:"replay"}:{kind:"refused",reason:`operation ${r} was already used for another request`};if(n.opened===null)return{kind:"refused",reason:"no workflow instance is open on this thread"};if(n.terminal!==null)return{kind:"refused",reason:`the instance is ${n.terminal.kind}`};if(n.cancellation!==null)return{kind:"refused",reason:"the instance is being cancelled"};if(e===void 0)return{kind:"refused",reason:"the instance cannot execute: no definition it can continue on is registered"};let f=R(n,e);if(f===null||f.id!==t.decision)return{kind:"refused",reason:`decision ${t.decision} is not pending`};let l=cn(f,i);if(l.actions.cleanupPending){let{draining:x,unresolved:S}=i.cleanup;return{kind:"refused",reason:`the recovery's cleanup is not established yet: ${[...S,...x].join("; ")}`}}if(!l.actions.verdicts.includes(o))return{kind:"refused",reason:`${o} is not offered on a ${l.reason} decision`};if(a!==null){if(o==="approve")return{kind:"refused",reason:"an option accompanies only revise"};if(!l.actions.options.includes(a))return{kind:"refused",reason:`option ${a} is not declared for a ${l.reason} decision`}}let w=a===null?l.actions.restart.retry:l.actions.restart.options[a],p=d.filter(x=>!w.allowed.includes(x));if(p.length>0)return{kind:"refused",reason:`this decision cannot restart ${p.join(", ")}`};let h=w.required.filter(x=>!d.includes(x));if(h.length>0)return{kind:"refused",reason:`${h.map(S=>`${S} (${i.uncontinuable.get(S)})`).join(", ")} cannot be continued, so this decision must restart ${h.length===1?"it":"them"}`};let g={reason:l.reason,verdict:o,option:a,feedback:c},O=n.attempts.get(l.attempt),y=e.steps[O.entered.step],b;if(l.result!==null)b=be(y.edges,{outputs:n.results.get(l.result).outputs,decision:g}).to;else if(a===null)b=O.entered.step;else{let x=be(y.edges,rn(a,c)),S=on(x,a);if(S!==null)return{kind:"refused",reason:`option ${a} cannot route a recovery: ${S}`};b=x.to}let k=vt(n,e,b,{kind:"decision",decision:l.id},s,{result:l.result,id:l.id,decision:g,restart:d});return k.kind==="needs"?k:k.kind==="refused"?{kind:"refused",reason:`the selected step ${b} cannot be entered: ${k.reason}`}:{kind:"rows",rows:[{op:"decided",decision:l.id,operation_id:r,actor:t.actor,attempt:l.attempt,reason:l.reason,result:l.result,verdict:o,option:a,feedback:c,restart:d},k.row]}}function $t(n,e){let t=n.operations.get(e.operationId);return t!==void 0?t.op==="cancel-requested"&&t.reason===e.reason?{kind:"replay"}:{kind:"refused",reason:`operation ${e.operationId} was already used for another request`}:n.opened===null?{kind:"refused",reason:"no workflow instance is open on this thread"}:n.terminal!==null?{kind:"refused",reason:`the instance is ${n.terminal.kind}`}:n.cancellation!==null?{kind:"refused",reason:`the instance is already being cancelled (${n.cancellation.operation_id})`}:{kind:"rows",rows:[{op:"cancel-requested",operation_id:e.operationId,actor:e.actor,reason:e.reason}]}}var Ot=()=>({managed:!1,claims:new Map,submissions:new Map,accepted:new Map,reminded:new Map,calls:new Map,response:null,answer:te.init()}),ke=je({key:"lagent:workflows/participant",init:Ot,apply(n,e){if(e.type==="thread/spawned"){if(e.data.fork!==null)return Ot();let i=e.data.origin?.cause;return n.managed=i?.kind==="assignment"&&i.owner===T,n}if(!n.managed)return n;let t=n.answer;if(n.answer=te.apply(t,e),e.type==="input/message"&&e.data.input_id!==null&&!n.claims.has(e.data.input_id)&&n.claims.set(e.data.input_id,e.seq),e.type==="input/message"){let i=Z.readContribution(e);i!==void 0&&n.reminded.set(i.assignment,i.epoch)}if(e.type==="assistant/message"&&Xe(n,e),e.type==="tool/call"&&en(n,e),e.type==="tool/result"){let i=_e(e.data.result);i!==void 0&&n.submissions.set(e.seq,{...i,call:e.source_seqs[0]??null})}return e.type==="turn/end"&&t.status==="pending"&&n.answer.status==="accepted"&&n.accepted.set(n.answer.seq,e.seq),n}});function dn(n,e,t){let{id:i,thread:s,task:r,generation:o}=n;if(r===null||o===null)throw new Error(`Assignment ${i} runs on the root`);let a=e.claims.get(r),c=[...e.submissions].filter(([p,h])=>a!==void 0&&p>a&&h.assignment===i),d=p=>({assignment:i,thread:s,task:r,generation:o,outcome:p});for(let[p,{output:h}]of c){let g=e.accepted.get(p);if(g!==void 0)return d({kind:"accepted",submission:p,ending:g,output:G(h)})}let u=c.at(-1),f=u===void 0?null:{submission:u[0],output:G(u[1].output)},l=t?.latestText??null,w=t?.settlement??null;switch(w?.kind){case void 0:return d({kind:"unfinished",candidate:f,report:l});case"failed":return d({kind:"ended",settlement:"failed",detail:w.why,candidate:f,report:l});case"stopped":{let p=t?.stop==null?"":` (stopped by ${t.stop.by==="person"?"the user":t.stop.by})`;return d({kind:"ended",settlement:"stopped",detail:`${w.reason}${p}`,candidate:f,report:l})}case"completed":return d({kind:"ended",settlement:"completed",detail:"it answered without its answer being an accepted submission of the assignment",candidate:f,report:w.output})}}function xt(n,e){let t=Q(e).flatMap(r=>{if(!n.isLoaded(r.thread))return[];let o=fe(n,T,{taskId:r.task,childId:r.thread});return[dn(r,n.state(ke,r.thread),o)]}),i=t.some(({outcome:r})=>r.kind==="ended"),s=t.filter(({outcome:r})=>i||r.kind!=="unfinished");return s.length===0?[]:[{op:"observed",observations:s}]}function jt(n,e,t,i){let s=Q(e),r=s.find(({id:f})=>f===t);if(r===void 0)return null;let o={taskId:r.task,childId:r.thread};if(n.summary(o.childId)!==void 0&&(!n.isLoaded(o.childId)||fe(n,T,o)!==null))return null;let a=s.flatMap(f=>{if(f===r||!n.isLoaded(f.thread))return[];let l=fe(n,T,{taskId:f.task,childId:f.thread});return[dn(f,n.state(ke,f.thread),l)]}),{id:c,thread:d,generation:u}=r;return{op:"observed",observations:[{assignment:c,thread:d,task:o.taskId,generation:u,outcome:{kind:"refused",detail:i}},...a]}}function Q(n){return n.opened===null||n.terminal!==null||n.cancellation!==null||n.current===null?[]:n.current.assignments.filter(({context:e,state:t,task:i})=>e==="isolated"&&t!=="accepted"&&t!=="superseded"&&!n.observed.has(i))}function St(n,e,t,i){let s=e.current;if(!_(e)||s===null||s.recovery!==null)return null;let r=s.result===null?void 0:e.results.get(s.result),o=r!==void 0&&r.routed===null&&r.decision===null&&R(e,t)===null,a=Q(e);if(!o&&a.length===0)return null;let c=a.flatMap(d=>{if(n.summary(d.thread)===void 0)return[];let u=fe(n,T,{taskId:d.task,childId:d.thread});return[dn(d,n.state(ke,d.thread),u)]});return{op:"stopped",attempt:s.entered.attempt,reason:i,observations:c}}import{scopeOf as Ti}from"@leuduan/cordis";import{z as We}from"zod";var ln=We.strictObject({allow:We.array(We.string()),denial:We.string().optional()}),_t=n=>n.source_seq,de=(n,e)=>n!==null&&n.caller_thread_id===e&&n.cause.kind==="assignment"&&n.cause.owner===T;function ne(n,e){let t=n.journal.origins.callers(e).threads,i=t.at(-1);if(i===void 0)return;if(i.origin!==null)return{unreadable:"This thread's conversation root cannot be found, so its workflow cannot be read."};if(!F(i))return;let s=i.thread_id,r=n.journal.state(q,s);if(r.opened!==null){if(t.length===1)return{root:s,state:r,kind:"root"};for(let o=0;o<t.length-1;o+=1){let a=t[o];if(de(a.origin,s)){let c=n.journal.state(ke,a.thread_id);if(o===0)return{root:s,state:r,kind:"participant",thread:a.thread_id,facts:c};let d=_t(t[o-1].origin);return{root:s,state:r,kind:"descendant",source:{kind:"participant",thread:a.thread_id,facts:c,seq:d}}}if(o===t.length-2)return{root:s,state:r,kind:"descendant",source:{kind:"root",seq:_t(a.origin)}}}}}var qt=(n,e)=>_(n)&&n.current?.assignments.includes(e)===!0&&e.state!=="accepted"&&e.state!=="superseded";function K(n){if(!(!_(n)||n.current===null))return n.current.assignments.find(e=>e.context==="conversation"&&qt(n,e))}function Mi(n,e){for(let t of[...n.attempts.values()].reverse()){let i=t.assignments.find(({context:s,claimSeq:r,closed:o})=>s==="conversation"&&r!==null&&r<=e&&(o===null||o>e));if(i!==void 0)return i}}function ee(n,e,t,i){let s;for(let r of n.attempts.values())for(let o of r.assignments){if(o.thread!==e||o.task===null)continue;let a=t.claims.get(o.task);a===void 0||i!==void 0&&a>i||(s===void 0||a>s.claimed)&&(s={assignment:o,claimed:a})}return s?.assignment}function Ri(n,e){let t=e.task===null?void 0:n.claims.get(e.task);if(t===void 0)return!1;for(let i of n.accepted.keys())if(i>t&&n.submissions.get(i)?.assignment===e.id)return!0;return!1}function Ci(n,e,t){let i=e.executable(n);if(n.cancellation!==null)return{kind:"coordination",why:"the workflow is being cancelled"};if(i===void 0)return{kind:"coordination",why:"the workflow cannot continue on the definition registered"};if(R(n,i)!==null)return{kind:"coordination",why:"a decision is pending, which freezes submission and changes"};let s=K(n);return s===void 0||s.claimSeq===null?{kind:"coordination",why:"no assignment of yours is active"}:t===void 0||t<s.claimSeq?{kind:"coordination",why:`the call was asked for before assignment ${s.id} began`}:Tt(n,i,e,s)}function un(n,e,t,i){let s=e.executable(n);return n.cancellation!==null?{kind:"closed",why:"its workflow is being cancelled"}:s===void 0?{kind:"closed",why:"its workflow cannot continue on the definition registered"}:R(n,s)!==null?{kind:"closed",why:"a decision is pending on its workflow, which freezes its work"}:t===void 0?{kind:"closed",why:"no workflow assignment covers it"}:qt(n,t)?i!==null&&Ri(i,t)?{kind:"closed",why:`workflow assignment ${t.id} was accepted`}:Tt(n,s,e,t):{kind:"closed",why:`workflow assignment ${t.id} has ended`}}function Tt(n,e,t,i){let s=e.steps[n.current.entered.step].restriction;return{kind:"assignment",assignment:i,restriction:s===null?null:t.restrictions[s]}}function fn(n,e,t){let{state:i}=n;switch(n.kind){case"root":return Ci(i,e,t);case"participant":return un(i,e,ee(i,n.thread,n.facts,t),n.facts);case"descendant":{let{source:s}=n;if(s.kind==="participant")return un(i,e,ee(i,s.thread,s.facts,s.seq),s.facts);let r=Mi(i,s.seq);return r!==void 0?un(i,e,r,null):{kind:"coordination",why:"it was started while its conversation coordinated"}}}}var B=n=>({decision:"deny",reason:n});function Ai(n,e){if(n.kind==="root"){let r=K(n.state),o=r?.candidate?.call??null;return Ye(n.state,o,e)?r:void 0}if(n.kind!=="participant")return;let{facts:t}=n,{answer:i}=t,s=i.status==="pending"?t.submissions.get(i.seq):void 0;if(!(s===void 0||!Ye(t,s.call,e)))return ee(n.state,n.thread,t,void 0)}function Et(n,e,t){if(!n.allow.includes(e))return B(n.denial??`${e} is not allowed while ${t}.`)}function Ni(n,e,t,i,s){let r=i.name===N,o=()=>r?B("No workflow assignment of yours is active, so there is nothing to submit."):void 0;if(n.journal.summary(t)===void 0)return o();let a=ne(n,t);if(a===void 0)return o();if("unreadable"in a)return B(a.unreadable);if(a.state.terminal!==null)return o();let c=n.journal.state(ie,t),d=s===void 0?void 0:c.executions.get(s.requestSeq);if(s!==void 0&&(d===void 0||d.request.owner!==s.owner||d.request.candidate_seq!==s.candidateSeq||c.answerCheck.status!=="pending"||c.answerCheck.seq!==s.candidateSeq||d.request.operation.name!==i.name||JSON.stringify(d.request.operation.arguments)!==JSON.stringify(i.arguments)))return B("Runtime preparation no longer belongs to this pending workflow candidate.");let u=s?.requestSeq??c.dispatchedCalls.get(i.call_id),f=s===void 0?Ai(a,u):void 0;if(f!==void 0)return B(`Not run: this response already submitted assignment ${f.id}, and that submission is its answer, so the calls after it do not run.`);let l=fn(a,e,u);if(l.kind==="closed")return B(r?`Nothing can be submitted now: ${l.why}.`:`This thread works under a workflow, and no workflow authority covers this call: ${l.why}.`);if(l.kind==="coordination")return r?B(`Nothing can be submitted now: ${l.why}.`):Et(e.coordination,i.name,l.why);if(r){if(l.assignment.thread!==t)return B(`Only the participant working assignment ${l.assignment.id} may submit it.`);let w=i.arguments.assignmentId;return w===l.assignment.id?void 0:B(`Your active assignment is ${l.assignment.id}; ${JSON.stringify(w)} is not.`)}return l.restriction===null?void 0:Et(l.restriction,i.name,`assignment ${l.assignment.id} is active`)}function Mt(n,e){n.on("lagent/tool/authorize",({toolCall:t,threadId:i,origin:s})=>Ni(n,e,i,t,s)),n.on("lagent/thread/activate",t=>{let i=n.journal.summary(t.id),s=i?.origin==null?void 0:n.journal.summary(i.origin.caller_thread_id),r=i===void 0?void 0:i.origin===null?i:s?.origin===null&&de(i.origin,s.thread_id)?s:void 0;if(r===void 0||!F(r))return;let o=()=>{if(!n.journal.isLoaded(r.thread_id))return!1;let c=n.journal.state(q,r.thread_id);return c.opened===null||c.terminal!==null};if(o())return;let a=Ti(t.ctx);t.ctx.effect(()=>{let c=n.tools.grant(a,[t.agentName],N),d=n.journal.subscribe(u=>{c===null||u.thread_id!==r.thread_id||!o()||(c(),c=null)});return()=>{d(),c?.()}})})}function ve(n,e,t){let i=[],s=[];for(let r of t){let o=r===e?"":`thread ${r}, `,a=In(n,r,{descendants:!1});i.push(...a.draining.map(c=>`${o}${c}`)),s.push(...a.unresolved.map(c=>`${o}${c}`));for(let c of He(n.journal,r))i.push(`${o}the stop of ${c.targets.length} agent task(s) has not finished`)}if(!t.has(e))for(let r of He(n.journal,e)){let o=r.targets.filter(({childId:a})=>t.has(a));o.length>0&&i.push(`the stop of ${o.length} agent task(s) has not finished`)}for(let r of re(n.journal,e))t.has(r.childId)&&(r.state==="unavailable"?s.push(`agent ${r.childId}: its task cannot be read (${r.error})`):r.state==="open"?i.push(`agent ${r.childId} has not finished`):(r.draining||r.cleanupPending)&&i.push(`agent ${r.childId} is still being cleaned up`));return{draining:i,unresolved:s}}function Pe(n,e,t){let i=new Set;for(let s of n.subtree(e,{self:!0})){let{origin:r}=s,o=r!==null&&!de(r,t)&&i.has(r.caller_thread_id);(s.thread_id===e||o)&&i.add(s.thread_id)}return i}function Rt(n,e,t){let i=e.instance,s=new Map;if(i===null||e.opened===null||e.terminal!==null)return{cleanup:{draining:[],unresolved:[]},uncontinuable:s};let r=n.journal.summaryMap(),o=t.map(p=>`the start of assignment ${p}'s task is still in flight`);if(e.cancellation!==null){let p=n.journal.origins.subtree(i,{self:!0}).map(({thread_id:g})=>g),h=ve(n,i,new Set(p));return{cleanup:{draining:[...h.draining,...o],unresolved:h.unresolved},uncontinuable:s}}let a=e.current;if(a?.recovery==null||a.recovery.decision!==null)return{cleanup:{draining:[],unresolved:[]},uncontinuable:s};let c=new Set;for(let{context:p,thread:h}of a.assignments)if(p==="isolated")for(let{thread_id:g}of n.journal.origins.subtree(h,{self:!0}))c.add(g);let d=a.assignments.some(({context:p})=>p==="conversation");if(d)for(let p of Pe(n.journal.origins,i,i))c.add(p);let u=ve(n,i,c),f=[...u.draining,...o],l=[...u.unresolved];if(d){let{pending:p,runs:h}=n.journal.state(ie,i).requests,g=p===null?void 0:h.get(p.runId)?.requestedAt;g!==void 0&&g<=a.recovery.seq&&f.push(`the conversation's run ${p.runId}, under way when its assignment was superseded, has not ended`)}let w=n.get("delegations");for(let[p,h]of nn(e)){if(!r.has(h))continue;let g=y=>{f.push(`whether ${p}'s conversation can be continued is not known yet: ${y}`)};if(w===void 0){g("delegations is not mounted");continue}if(!n.journal.isLoaded(h)){g("it is not loaded");continue}let O=w.continuation(h);O!==null&&(O.kind==="refusal"?s.set(p,O.reason):O.kind==="wait"?f.push(`${p}: ${O.reason}`):l.push(`${p}: ${O.reason}`))}return{cleanup:{draining:f,unresolved:l},uncontinuable:s}}var Wi="another participant's task in the same workflow attempt ended without acceptance";function pn(n,e,t){let i=e.instance,s=new Set(t.observations.flatMap(({task:a,outcome:c})=>c.kind==="ended"||c.kind==="refused"?[a]:[]));if(i===null||s.size===0)return[];let r=new Map(re(n.journal,i).flatMap(a=>a.state!=="unavailable"&&(a.state==="open"||a.draining)?[[a.taskId,a]]:[])),o=Q(e).flatMap(({task:a,thread:c})=>a===null||s.has(a)||r.get(a)?.childId!==c?[]:[{taskId:a,childId:c}]);return o.length===0?[]:[Fn(i,Wi,o)]}import{z as Ie}from"zod";var hn=Ie.string().min(1),mn=Ie.record(hn,Ie.strictObject({provider:hn,model:hn,thinkingEffort:xe.nullable()}));function gn(n,e,t){let i=mn.safeParse(e);if(!i.success)throw new E(`The model choices are invalid: ${Ie.prettifyError(i.error)}`);let s=i.data;for(let[o,a]of Object.entries(s)){let c=n.participants[o];if(c===void 0)throw new E(`The workflow has no participant ${o}`);if(c.context==="conversation")throw new E(`Participant ${o} runs on the conversation's route: it takes no model choice (${a.provider}/${a.model})`);if(!c.model.configurable)throw new E(`Participant ${o} runs on the model its workflow fixes`)}let r={};for(let[o,a]of Object.entries(n.participants)){if(a.context!=="isolated")continue;let c=s[o],{model:d}=a,u=c===void 0?{provider:d.provider,model:d.model,thinkingEffort:d.thinkingEffort??null}:{provider:c.provider,model:c.model,thinkingEffort:c.thinkingEffort};if(!t.includes(u.provider))throw new E(`Participant ${o}: model provider ${u.provider} is not offered by the host`);r[o]=u}return r}var E=class extends Error{constructor(e){super(e),this.name="WorkflowRefusedError"}};import{z as Le}from"zod";var Pi=Le.strictObject({instance:Le.string().min(1),status:Le.enum(["assignment","decision","blocked","cleanup","ended"]),subject:Le.string().nullable()}),Ii=se({plugin:"lagent:workflows",kind:"presentation",version:1,schema:Pi});function Li(n,e,t){let{state:i,facts:s,thread:r,root:o}=n;if(i.terminal!==null||i.cancellation!==null||i.current===null||e===void 0||R(i,e)!==null)return;let a=new Set(t.claimed.map(({id:d})=>d)),c=i.current.assignments.find(d=>d.thread===r&&d.state!=="accepted"&&d.state!=="superseded"&&(a.has(d.task)||ee(i,r,s,void 0)===d));return c===void 0?void 0:{instance:o,status:"assignment",subject:c.id}}function Di(n,e,t){let i=n.instance;if(n.terminal!==null)return{instance:i,status:"ended",subject:n.terminal.kind};if(n.cancellation!==null)return{instance:i,status:"cleanup",subject:null};if(e===void 0)return{instance:i,status:"blocked",subject:"its definition is not registered, or no longer fits where it stands"};let s=R(n,e);if(s!==null)return{instance:i,status:"decision",subject:s.id};let r=K(n);if(r!==void 0){if(r.state!=="scheduled")return{instance:i,status:"assignment",subject:r.id};let a=Ge(r);return t.claimed.some(({id:c})=>c===a)?{instance:i,status:"assignment",subject:r.id}:void 0}let o=X(n,e);return o.kind==="refused"?{instance:i,status:"blocked",subject:o.reason}:void 0}function At(n,e){let t=i=>{if(n.journal.summary(i)===void 0)return;let s=ne(n,i);return s===void 0||"unreadable"in s?void 0:s};Wn(n,{event:Ii,rank:2,current:i=>{let s=t(i.threadId);if(s===void 0)return;let r=e.executable(s.state);return s.kind==="participant"?Li(s,r,i):s.kind==="root"?Di(s.state,r,i):void 0},render:(i,s,{step:r})=>{let o=n.journal.state(q,i.instance),a=e.executable(o);if(r.threadId!==i.instance){let f=o.current.assignments.find(({id:l})=>l===i.subject);return H("workflow",Ct(o,a,f,e),{attrs:{id:i.instance,status:i.status}})}let c=o.opened.definition,d=f=>H("workflow",f,{attrs:{id:i.instance,status:i.status}}),u=`Until then you work under the coordination policy: ${zi(e.coordination)}`;switch(i.status){case"assignment":{let f=K(o);return d(Ct(o,a,f,e))}case"decision":{let{recovery:f}=o.current;return d(f!==null?Fi(c,o.current,i.subject,u):`Workflow ${c}: step ${o.current.entered.step}'s result awaits a decision (${i.subject}). Nothing can be submitted or changed until it is decided, and chat is discussion only: the user decides. `+u)}case"blocked":return d(`Workflow ${c} cannot go on: ${i.subject}. ${u}`);case"cleanup":return d(`Workflow ${c} is being cancelled. ${u}`);case"ended":return s===null||s.status==="ended"?null:d(`Workflow ${c} ${i.subject==="completed"?"is complete":"was cancelled"}: its restrictions no longer apply, and this conversation goes on as ordinary chat.`)}}})}var wn=(n,e)=>JSON.stringify(n,null,e).replaceAll("<","\\u003c"),De=n=>jn(n,["workflow"]);function Fi(n,e,t,i){let s=e.recovery,{step:r,ordinal:o}=e.entered,a=[`Workflow ${n}: attempt ${o} of step ${r} cannot finish (${s.cause==="stop"?"stopped":"failed"}: ${De(s.detail)}), and awaits a recovery decision (${t}): a retry, a declared option, or cancelling. Nothing can be submitted or changed until it is decided, and chat is discussion only: the user decides. ${i}`],c=Nt(s,null);return c.length>0&&a.push("","Unaccepted, shown to you as its coordinator only:",...c),a.join(`
4
+ `)}function Nt(n,e){let t=[],i=s=>e===null||s===e;for(let[s,r]of Object.entries(n.candidates))i(s)&&t.push(`- ${s}'s last submission, not accepted: ${wn(r)}`);for(let[s,r]of Object.entries(n.reports))i(s)&&t.push(`- ${s}'s partial report, not accepted: ${De(r)}`);return t}var zi=n=>n.allow.length===0?"no tool may be called.":`you may call only ${n.allow.join(", ")}; any other call is denied.`,Ui=n=>n===null?`Tools: no step restriction; ${N} is available.`:`Tools: ${n.allow.length===0?`${N} only`:[...n.allow,N].join(", ")}; any other call is denied.`;function Ct(n,e,t,i){let s=n.current,{step:r,ordinal:o}=s.entered,a=e.steps[r],c=a.assignments.find(({participant:l})=>l===t.participant),d=[`Workflow ${e.id}, step ${r}, attempt ${o}. Submit with ${N}, assignmentId "${t.id}".`],u=Bi(n,s,t.participant);u!==null&&d.push("",u);for(let l of c.reads){let w=s.entered.inputs[l]??null,p=wn(Me(n,s.entered,l),2);d.push("",H("input",p,{namespace:"workflow",attrs:{name:l,citation:Ji(n,w)}}))}d.push("",H("output-schema",wn(c.output),{namespace:"workflow"})),d.push("",Ui(a.restriction===null?null:i.restrictions[a.restriction]));let f=Ki(n,t);return f.length>0&&(d.push("","Superseded since your previous assignment, so do not rely on them as you remember them:"),d.push(...f.map(l=>`- ${l}`))),[H("step-guidance",c.prompt,{namespace:"workflow"}),H("step-input",d.join(`
5
+ `),{namespace:"workflow"})].join(`
6
+
7
+ `)}function Wt({reason:n,verdict:e,option:t}){return n==="recovery"?t===null?"retried":"recovered":e==="approve"?"approved":"sent back"}function bn(n,e){let t=`step ${n} was ${Wt(e)}${e.option===null?"":` (${e.option})`}`;return e.feedback===null?`${t}.`:`${t}, with this feedback:
8
+ ${H("feedback",De(e.feedback),{namespace:"workflow"})}`}function Ji(n,e){if(e===null)return"optional, nothing to bind";if(e.kind==="input")return"the workflow's input";if(e.kind==="evidence"){let o=e.immutable?"a snapshot":"live: it describes the state at this step's entry, which may have changed since";return`evidence from ${e.provider}, ${o}`}let t=n.results.get(e.result),i=e.participant===null?"result":`${e.participant}'s output`,s=e.decision===null?void 0:n.decisions.get(e.decision),r=s===void 0?"":`, ${Wt(s)}`;return`${i} of step ${t.step}, revision ${t.revision}, ${e.mode}${r}`}function Bi(n,e,t){let{cause:i}=e.entered;if(i.kind!=="decision")return null;let s=n.decisions.get(i.decision),r=n.attempts.get(s.attempt),o=[`This attempt was opened when ${bn(r.entered.step,s)}`];if(r.recovery===null)return o.join(`
9
+ `);o.push(`That attempt could not finish: ${De(r.recovery.detail)}.`);let a=Nt(r.recovery,t);return a.length>0&&o.push("What you left in it, not accepted:",...a),o.join(`
10
+ `)}function Ki(n,e){let t=null;for(let o of n.attempts.values()){if(o===n.current)break;for(let a of o.assignments)a.participant===e.participant&&(a.context==="isolated"?t=o.seq:a.claimSeq!==null&&(t=a.claimSeq))}if(t===null)return[];let i=oe(n),s=nt(n,t),r=[...n.results.values()].filter(({revision:o})=>!i.has(o));return[...i].filter(o=>!s.has(o)).map(o=>{let{step:a}=n.results.get(o),c=r.filter(d=>d.step===a).at(-1);return c===void 0?`${o} (step ${a}) is no longer current`:`${o} (step ${a}) is replaced by ${c.revision}`})}var Fe=class{handle;inFlight=new Map;running=new Set;outcomes=new Map;reported=new Set;closing=!1;ctx;options;constructor(e,t){this.ctx=e,this.options=t}register(){let e=this.ctx.get("delegations");if(e===void 0)return()=>{};let t=e.registerOwner({name:T,authorize:(i,s)=>this.authorize(i,s),settled:i=>this.options.reconcile(i.callerThreadId)});return this.handle=t,()=>{this.handle===t&&(this.handle=void 0);for(let{controller:i}of this.inFlight.values())i.abort(new Error("The workflow owner was withdrawn"));t.dispose()}}changed(e){this.handle?.changed(e)}starting(e){return[...this.inFlight.values()].filter(t=>t.root===e).map(({assignment:t})=>t)}waiting(e){return[...this.outcomes.get(e)?.values()??[]].some(({kind:t})=>t==="wait")}held(e){return[...this.outcomes.get(e)?.values()??[]].some(({kind:t})=>t==="hold")}of(e){let t=this.outcomes.get(e);return t===void 0&&this.outcomes.set(e,t=new Map),t}states(e,t){let i=Pt(this.ctx,e),s=new Map;for(let r of kn(t)){let o=i.has(r.task);s.set(r.id,o?{kind:"started"}:this.outcomes.get(e)?.get(r.id)??{kind:"starting"})}return s}dispatch(e,t){let i=this.ctx.journal.state(q,e),s=this.of(e);for(let r of s.keys())i.current?.assignments.some(o=>o.id===r)||s.delete(r);if(s.size===0&&this.outcomes.delete(e),!(this.closing||this.options.quarantined(e)!==null))for(let r of this.owed(e,i)){if(this.inFlight.has(r.task))continue;let o=this.outcomes.get(e)?.get(r.id);if(t==="capacity"&&!(o?.kind==="wait"&&o.on==="capacity"))continue;let a=new AbortController;this.inFlight.set(r.task,{root:e,assignment:r.id,controller:a});let c=this.start(e,r.id,a.signal).catch(d=>{this.of(e).set(r.id,{kind:"hold",reason:`assignment ${r.id} cannot start its task: ${le(d)}`}),this.ctx.reportError(d)}).finally(()=>{this.inFlight.delete(r.task),this.running.delete(c);let d=this.outcomes.get(e)?.get(r.id);this.options.ended(e,d===void 0),d!==void 0&&!(d.kind==="wait"&&d.on==="capacity")&&this.retryCapacity(e)});this.running.add(c)}}retryCapacity(e){if(![...this.outcomes.get(e)?.values()??[]].some(s=>s.kind==="wait"&&s.on==="capacity")||this.closing)return;let i=this.ctx.threads.retain(()=>[e],{lastRelease:"keep"}).then(s=>{try{this.dispatch(e,"capacity")}finally{s.release()}}).catch(s=>this.ctx.reportError(s)).finally(()=>this.running.delete(i));this.running.add(i)}async shutdown(){this.closing=!0,await Promise.allSettled([...this.running])}owed(e,t){let i=t.opened===null?void 0:this.options.executable(t);if(i===void 0||R(t,i)!==null)return[];let s=Pt(this.ctx,e);return kn(t).filter(r=>!s.has(r.task))}async start(e,t,i){let s=await this.ctx.threads.retain(()=>[e],{lastRelease:"keep"});try{let r=this.ctx.journal.state(q,e),o=this.owed(e,r).find(u=>u.id===t);if(o===void 0)return;let a=this.handle;if(a===void 0){this.of(e).set(t,{kind:"hold",reason:"delegations, which starts isolated participants, is not mounted"});return}let c=this.request(r,o,i);if("hold"in c){this.of(e).set(t,{kind:"hold",reason:c.hold});return}let d=await a.start(c);d.kind==="refusal"?await this.options.refused(e,t,d.reason):this.settle(e,o,d)}finally{s.release()}}settle(e,t,i){let{id:s}=t;switch(i.kind){case"opened":case"discard":this.of(e).delete(s);return;case"wait":this.of(e).set(s,{kind:"wait",on:i.on,reason:i.reason});return;case"hold":this.of(e).set(s,{kind:"hold",reason:i.reason});return;case"conflict":case"vetoed":{let r=`assignment ${s} cannot start its task: ${i.reason}`;this.of(e).set(s,{kind:"hold",reason:r}),this.reported.has(r)||(this.reported.add(r),this.ctx.reportError(new Error(`Workflow ${e}: ${r}`)));return}}}request(e,t,i){let{opened:s}=e,r=this.options.executable(e),o=e.current.entered.step,{participant:a}=t,c=s.pins.participants[a];if(t.lifetime===null||c?.configuration==null)throw new Error(`Assignment ${t.id} names no isolated participant the instance pinned`);let d=[];for(let u of t.checks){let f=this.options.providers.problem("check",u,c.checks[u]);if(f!==null)return{hold:f};d.push(this.options.checks[u].owed({config:c.checks[u].config,owner:T,assignment:t.id,generation:t.generation,inputId:t.task}))}return{callerThreadId:e.instance,grantedSeq:e.current.seq,instance:e.instance,attempt:e.current.entered.attempt,assignment:t.id,participant:a,lifetime:t.lifetime,generation:t.generation,configuration:Vi(c,this.options.checks),task:`Workflow ${r.id}: assignment ${t.id} (step ${o}) is yours; its instructions come with your next step.`,...d.length===0?{}:{rows:Hi(d)},signal:i}}authorize(e,t){let i=e.callerThreadId,s=this.options.quarantined(i);if(s!==null)return{kind:"hold",reason:`its workflow instance could not be repaired: ${s}`};if(t==="running"){let d=this.options.runnable(i);return d==="allow-all"?{kind:"allow"}:Array.isArray(d)?d.includes(e.taskId)?{kind:"allow"}:{kind:"hold",reason:`its workflow assignment ${e.assignment} does not run now`}:{kind:"hold",reason:`its workflow cannot be read: ${d.unavailable}`}}if(!this.ctx.journal.isLoaded(i))return{kind:"hold",reason:`its workflow's root ${i} is not loaded`};let r=this.ctx.journal.state(q,i);if(!_(r))return{kind:"discard",reason:"its workflow ended or is being cancelled"};let o=kn(r).find(({id:d})=>d===e.assignment);if(o===void 0||o.task!==e.taskId||o.thread!==e.childId||o.generation!==e.generation||r.current.seq!==e.grantedSeq)return{kind:"discard",reason:`workflow assignment ${e.assignment} is no longer open as this task`};let a=this.options.executable(r);if(a===void 0)return{kind:"hold",reason:"its workflow cannot continue on the definition registered"};if(R(r,a)!==null)return{kind:"hold",reason:"a decision is pending"};let c=r.opened.pins.participants[o.participant]?.checks??{};for(let d of o.checks){let u=this.options.providers.problem("check",d,c[d]);if(u!==null)return{kind:"hold",reason:u}}return{kind:"allow"}}};function kn(n){return!_(n)||n.current===null||n.current.recovery!==null?[]:n.current.assignments.filter(({context:e,state:t,task:i})=>e==="isolated"&&t!=="accepted"&&t!=="superseded"&&!n.observed.has(i))}function Pt(n,e){return new Set(re(n.journal,e).flatMap(t=>"taskId"in t?[t.taskId]:[]))}function It(n){let e=[];for(let t of n.attempts.values())for(let{task:i,state:s}of t.assignments){if(i===null)continue;let r=t===n.current&&s!=="accepted"&&s!=="superseded";(s==="accepted"||r)&&e.push(i)}return e}function Vi(n,e){let{agent:t,route:i,environment:s,installations:r}=n.configuration,o=new Set(Object.keys(n.checks).flatMap(a=>e[a]?.requires??[]));return{agent:t,route:i,environment:s,installations:r.map(({installation_id:a,preset_id:c,config:d,lifetime:u})=>({installationId:a,presetId:c,config:d,lifetime:u})),requires:[...o].sort()}}function Hi(n){return e=>{let t=[];for(let i of n){let s=i(e);if(qn(s))return s;t.push(...s)}return t}}import{z as Lt}from"zod";var vn=n=>typeof n=="function",Dt=n=>{if(typeof n!="object"||n===null)return!1;let e=n;return["unavailable","validate","pin","subscribe"].every(t=>vn(e[t]))},Ft=Lt.custom(n=>Dt(n)&&vn(n.owed)&&Array.isArray(n.requires),{error:"Expected a check provider binding"}),zt=Lt.custom(n=>Dt(n)&&vn(n.acquire),{error:"Expected an evidence provider binding"}),ze=class{disposed=new Set;inFlight=new Set;drained=new AbortController;bindings;constructor(e){this.bindings=e}watch(e){let t=["check","evidence"].flatMap(i=>Object.entries(this.table(i)).map(([s,r])=>r.subscribe(o=>{o==="disposed"&&this.dispose(ye(i,s)),e(i,s,o)})));return()=>{for(let i of t)i()}}problem(e,t,i){let s=this.table(e)[t],r=`${e} provider ${t}`;if(s===void 0)return`${r} is not bound`;if(this.disposed.has(ye(e,t)))return`${r} was disposed`;if(i===void 0)return`the instance recorded no pin for ${r}`;let o=s.validate(i.config);if(o!==null)return`${r} refuses the instance's pin: ${o}`;let a=s.unavailable();return a===null?null:`${r} is unavailable: ${a}`}async pin(e,t,i,s){let r=this.table(e)[t];if(r===void 0)return{refused:`${e} provider ${t} is not bound`};if(this.disposed.has(ye(e,t)))return{refused:`${e} provider ${t} was disposed`};let o=r.unavailable();if(o!==null)return{refused:`${e} provider ${t} is unavailable: ${o}`};let a;try{a=await this.tracked(ye(e,t),s,c=>r.pin(i,c))}catch(c){if(c instanceof V)throw c;a={refused:c instanceof Error?c.message:String(c)}}return"refused"in a?{refused:`${e} provider ${t} refuses this opening: ${a.refused}`}:{config:a.config}}async acquire(e,t,i){let s=this.problem("evidence",e,t);if(s!==null)return{kind:"unavailable",reason:s};let r=this.bindings.evidence[e];try{return await this.tracked(ye("evidence",e),i.signal,o=>r.acquire({...i,config:t.config,signal:o}))}catch(o){if(o instanceof V)throw o;return{kind:"unavailable",reason:o instanceof Error?o.message:String(o)}}}async drain(){this.drained.abort(new Error("The workflows plugin is unloading")),await Promise.allSettled([...this.inFlight].map(({done:e})=>e))}table(e){return e==="check"?this.bindings.checks:this.bindings.evidence}dispose(e){this.disposed.add(e);for(let t of this.inFlight)t.key===e&&t.controller.abort(new Error(`Provider ${e} was disposed`))}async tracked(e,t,i){let s=new AbortController,r=AbortSignal.any([t,s.signal,this.drained.signal]);if(r.aborted)throw new V(r.reason);let o=i(r),a={key:e,controller:s,done:o};this.inFlight.add(a),o.then(()=>this.inFlight.delete(a),()=>this.inFlight.delete(a));try{return await Promise.race([o,Qi(r)])}catch(c){throw r.aborted?new V(r.reason):c}}},V=class extends Error{constructor(e){super(`Cancelled: ${e instanceof Error?e.message:String(e)}`,{cause:e}),this.name="CancelledError"}},ye=(n,e)=>`${n}:${e}`,Qi=n=>new Promise((e,t)=>{n.aborted?t(n.reason):n.addEventListener("abort",()=>t(n.reason),{once:!0})});function Yi(n,e,t,i){if(!_(n))return;let s=t.executable(n);if(s===void 0||R(n,s)!==null)return;if(e.kind==="root"){let a=K(n),c=a===void 0?null:ot(n,a);return a===void 0||c===null?void 0:{assignment:a,epoch:c,submitted:a.candidate?.seq===i,reminded:a.reminded}}if(e.kind!=="participant")return;let{facts:r}=e,o=ee(n,e.thread,r,void 0);if(!(o===void 0||o.state==="accepted"||o.state==="superseded")&&n.current.assignments.includes(o))return{assignment:o,epoch:r.claims.get(o.task),submitted:r.submissions.get(i)?.assignment===o.id,reminded:r.reminded.get(o.id)??null}}function Ut(n,e){let t=(i,s)=>{let r=ne(n,i);if(r===void 0||"unreadable"in r)return null;let o=Yi(r.state,r,e,s);return o===void 0?null:{kind:r.kind,assignment:o.assignment.id,epoch:o.epoch,submitted:o.submitted,reminded:o.reminded}};n.on("lagent/turn/stop",({threadId:i,answer:s,observe:r})=>{r(()=>JSON.stringify(t(i,s.seq)));let o=t(i,s.seq);if(o===null||o.submitted)return;let{assignment:a,epoch:c}=o;if(o.reminded===c)return o.kind==="root"?void 0:Z.rejection(`Assignment ${a} was answered without its submission after the one reminder, so its task fails and the workflow's recovery decides what comes next.`,{assignment:a,epoch:c});let d=o.kind==="root"?"If you need the user first, reply again saying what you need; the assignment stays open until you submit.":"Another reply without a submission fails your task.";return{decision:"continue",message:Z.contribution({content:`Your workflow assignment ${a} is still open, and a reply does not finish it: submit its output with ${N} ({ "assignmentId": "${a}", "output": ... }), matching the schema it was presented with. ${d}`},{assignment:a,epoch:c})}})}import{z as yn}from"zod";function Gi(n,e,t){let{draining:i,unresolved:s}=ve(n,t,Pe(n.journal.origins,t,e.root));return[...i,...s]}var Zi=yn.strictObject({assignmentId:yn.string().min(1).describe("The id of the assignment this output answers, as it was presented."),output:A.describe("The assignment's output, matching the JSON Schema it was presented with.")});function Jt(n,e){return An({name:N,description:"Submit the output of your active workflow assignment. A valid submission ends your turn as your answer; it is accepted when that answer is.",parameters:Zi,onInterrupted:"rerun",executionMode:"exclusive",execute:async({assignmentId:t,output:i},{threadId:s,callSeq:r})=>{let o=h=>({output:`Not submitted: ${h}`,is_error:!0}),a=ne(n,s);if(a===void 0||"unreadable"in a||!_(a.state))return o("no workflow assignment of yours is active.");let c=fn(a,e,r);if(c.kind!=="assignment")return o(`${c.why}.`);let{assignment:d}=c;if(d.thread!==s)return o(`only the participant working ${d.id} may submit it.`);if(d.id!==t)return o(`your active assignment is ${d.id}, not ${JSON.stringify(t)}.`);let u=Gi(n,a,s);if(u.length>0)return o(`work you started has not ended: ${u.join("; ")}. Wait for it to end, or stop it, then submit again.`);let{state:f}=a,l=e.executable(f),w=f.current.entered.step,p=Ne(l,{step:w,participant:d.participant}).safeParse(i);return p.success?{output:`Submitted the output of assignment ${d.id}. It is accepted when this answer is.`,metadata:Qn({assignment:d.id,output:i}),concludes_turn:!0}:o(`the output does not match the assignment's schema: ${yn.prettifyError(p.error)}`)}})}function Kt(n,e,t){let i=n.opened;if(i===null)throw new Error("No workflow instance was opened on this thread");let s=ge(me(n),e),r=s===null?e:void 0,o=r??i.canonical,a=oe(n),c=[...n.results.values()].map(k=>({revision:k.revision,step:k.step,outputs:k.outputs,decision:Te(n,k.decision),current:!a.has(k.revision),inputs:Object.fromEntries(Object.entries(n.attempts.get(k.attempt).entered.inputs).map(([x,S])=>[x,Se(S)]))})),d=i.input,u=Object.fromEntries(Object.entries(i.pins.participants).flatMap(([k,{configuration:x}])=>x===null?[]:[[k,{provider:x.route.provider,model:x.route.model,thinkingEffort:x.route.thinking_effort}]]));if(n.terminal!==null)return{definition:i.definition,input:d,models:u,status:n.terminal.kind,step:null,attempt:null,assignments:[],results:c,pending:null,blocked:null,waitingOn:null};let f=n.current,l=R(n,o),w={definition:i.definition,input:d,models:u,step:f?.entered.step??null,attempt:f===null?null:{id:f.entered.attempt,ordinal:f.entered.ordinal},assignments:(f?.assignments??[]).map(({id:k,participant:x,state:S,thread:C,task:L,generation:W})=>({id:k,participant:x,state:S,thread:C,task:L,generation:W})),results:c};if(n.cancellation!==null){let{unresolved:k}=t.barrier.cleanup;return k.length>0?{...w,status:"blocked",pending:null,blocked:{reason:"cleanup",detail:k.join("; ")},waitingOn:"cleanup"}:{...w,status:"running",pending:null,blocked:null,waitingOn:"cleanup"}}if(r===void 0)return{...w,status:"blocked",pending:l===null?null:Bt(l),blocked:{reason:"missing-binding",detail:s},waitingOn:null};let p=[...t.starts.values()].flatMap(k=>k.kind==="hold"?[k.reason]:[]),h=[...Xi(n,r,t),...p];if(h.length>0)return{...w,status:"blocked",pending:l===null?null:Bt(l),blocked:{reason:"missing-binding",detail:h.join("; ")},waitingOn:null};if(l!==null){let k=cn(l,t.barrier);if(!k.actions.cleanupPending)return{...w,status:"waiting",pending:k,blocked:null,waitingOn:"decision"};let{unresolved:x}=t.barrier.cleanup;return x.length>0?{...w,status:"blocked",pending:k,blocked:{reason:"cleanup",detail:x.join("; ")},waitingOn:"cleanup"}:{...w,status:"waiting",pending:k,blocked:null,waitingOn:"cleanup"}}let g=X(n,r),O=g.kind==="refused"?g.reason:g.kind==="needs"&&t.entryRefusal?.transition===g.needs.transition?t.entryRefusal.reason:null;if(O!==null)return{...w,status:"blocked",pending:null,blocked:{reason:"entry-refused",detail:O},waitingOn:null};let y=[...t.starts.values()],b=t.settled&&K(n)!==void 0?"person-input":y.some(({kind:k})=>k==="starting"||k==="wait")?"dispatch":y.length>0?"participant":null;return{...w,status:"running",pending:null,blocked:null,waitingOn:b}}function Xi(n,e,t){let i=[],s=n.current;if(s!==null){for(let{participant:a,checks:c,state:d}of s.assignments)if(!(d==="accepted"||d==="superseded"))for(let u of c){let f=t.provider({check:u,participant:a});f!==null&&i.push(f)}}let r=X(n,e);if(r.kind==="needs")for(let{provider:a}of r.needs.inputs){let c=t.provider({evidence:a});c!==null&&i.push(c)}let o=r.kind==="needs"?r.needs.step:r.kind==="transition"&&r.row.op==="entered"?r.row.step:null;for(let{participant:a,checks:c}of o===null?[]:e.steps[o].assignments)for(let d of c){let u=t.provider({check:d,participant:a});u!==null&&i.push(u)}return[...new Set(i)]}var Bt=n=>({...n,actions:{...n.actions,verdicts:[],options:[],restart:{retry:{required:[],allowed:[]},options:{}}}});var ns=$.strictObject({definitions:$.array($.custom(ht,{error:"Expected a definition made by defineWorkflow"})),restrictions:$.record($.string().min(1),ln),coordination:ln,prerequisites:$.record($.string().min(1),$.custom(n=>typeof n=="function",{error:"Expected a prerequisite check"})),roles:$.record($.string().min(1),$.custom(n=>typeof n=="function",{error:"Expected a role binding"})),modelProviders:$.array($.string().min(1)),checks:$.record($.string().min(1),Ft),evidence:$.record($.string().min(1),zt)}),z=$.string().min(1),ts=$.strictObject({id:z,inputId:z,definition:z,input:A,conversation:$.strictObject({environment:$.strictObject({cwd:$.string()}),installations:$.array($.custom())}),models:mn.optional(),signal:$.instanceof(AbortSignal).optional()}),is=$.strictObject({operationId:z,instanceId:z,decision:z,verdict:$.enum(["approve","revise"]),option:z.optional(),feedback:$.string().optional(),restart:$.array(z).optional()}),ss=$.strictObject({operationId:z,instanceId:z,reason:$.string()}),rs=3,Oe=class extends Error{constructor(e){super(`The workflow changed while its Stop was prepared: ${e}`),this.name="StaleStopError"}},os=3,Vt="stale-wake",Ht="A workflow opens only on a new root conversation, never on a fork or a delegated thread",as=3,$n=new AbortController().signal,$e="The workflow instance could not be repaired",Ue=Pn({key:"workflow",version:1,schema:$.strictObject({references:$.strictObject({id:$.string(),step:$.string().nullable(),participants:$.record($.string(),$.strictObject({context:$.enum(["conversation","isolated"]),open:$.boolean()}))}),terminal:$.boolean(),checks:$.array($.strictObject({thread:$.string(),check:$.string(),pin:$.strictObject({config:A}).nullable()})),runnable:$.array($.string())}).nullable(),applies:F,compute(n,e){let t=n.state(q,e);return t.opened===null?null:{references:me(t),terminal:t.terminal!==null,checks:ds(t),runnable:t.cancellation===null?It(t):[]}}}),Je=class extends es{static inject=["threads","journal","tools","presets"];config;registered;entries;cleanups=new Map;cancelling=new Set;unrepaired=new Map;sends=new Set;passes=new Map;closing=!1;repairing=!1;participants;refreshed=Promise.resolve([]);providers;constraint;entryRefusals=new Map;constructor(e,t){super(e,"workflows"),this.config=ns.parse(t);let{definitions:i,restrictions:s,prerequisites:r,roles:o,checks:a,evidence:c}=this.config,d={restrictions:new Set(Object.keys(s)),prerequisites:new Set(Object.keys(r)),roles:new Set(Object.keys(o)),checks:new Set(Object.keys(a)),evidence:new Set(Object.keys(c))};this.providers=new ze({checks:a,evidence:c});let u=e.get("delegations")!==void 0,f=new Map;for(let p of i){let h=p.id;if(f.has(h))throw new J(h,["its id is registered twice"]);let g=wt(p,d);if(Object.values(p.participants).some(({context:y})=>y==="isolated")&&!u&&g.push("isolated participants run as delegation tasks: mount delegations before workflows"),g.length>0)throw new J(h,g);f.set(h,p)}this.registered=f;let l=Object.freeze([...this.config.modelProviders]);this.entries=ue([...f.values()].map(({id:p,input:h,participants:g})=>({id:p,input:h,participants:Object.fromEntries(Object.entries(g).flatMap(([O,y])=>y.context==="isolated"?[[O,{provider:y.model.provider,model:y.model.model,thinkingEffort:y.model.thinkingEffort??null,configurable:y.model.configurable}]]:[])),modelProviders:l}))),this.participants=new Fe(e,{providers:this.providers,checks:a,executable:p=>this.executable(p),runnable:p=>this.runnable(p),quarantined:p=>this.quarantined(p),reconcile:p=>this.track(this.reconcile(p)),ended:(p,h)=>{(h||this.cancelling.has(p))&&this.track(this.reconcile(p))},refused:(p,h,g)=>this.refused(p,h,g)}),e.effect(()=>e.journal.register(q));let w={restrictions:s,coordination:this.config.coordination,executable:p=>this.executable(p)};At(e,w),Mt(e,w),Nn(e,Jt(e,w)),Ut(e,w),this.refreshed=Ue.register(e).refreshed,e.effect(()=>e.journal.subscribe(p=>this.onRow(p))),e.effect(()=>e.threads.onRunEvent(({threadId:p})=>{this.cancelling.has(p)&&this.track(this.holding(p,()=>this.finishCleanup(p)))})),e.effect(()=>{let p=e.threads.registerOwner({owner:T,constraint:{applies:h=>this.governs(h),evaluate:h=>this.evaluate(h)},repair:()=>this.repair()});return this.constraint=p,()=>{this.constraint=void 0,p.dispose()}}),e.effect(()=>this.providers.watch(()=>this.providerChanged())),e.effect(()=>this.participants.register()),Mn(e,()=>this.shutdown())}catalog(){return this.entries}definition(e){return this.registered.get(e)}async opening(e){let t=ts.parse(e),{id:i,inputId:s,definition:r,input:o,models:a={},signal:c=$n}=t,d=this.definition(r);if(d===void 0)throw new E(`No workflow ${r} is registered`);let u=Ne(d,"$input").safeParse(o);if(!u.success)throw new E(`The input does not match the workflow's input schema: ${$.prettifyError(u.error)}`);let f=this.proposed(t.conversation),l=gn(d,a,this.config.modelProviders),w=await this.pins(d,f,l,c),p=Vn(d,o,s,w),h={...q.init(),instance:i,opened:p},{plan:g}=await this.withEvidence(i,w,y=>{let b=Re(h,d,{instance:i,step:d.entry,cause:{kind:"opening"},activation:s,...y===void 0?{}:{evidence:y}});return"needs"in b?{kind:"needs",needs:b.needs}:"refused"in b?{kind:"refused",reason:b.refused}:{kind:"row",...b}},c);if(g.kind==="refused")throw new E(`The entry step cannot be entered: ${g.reason}`);let O=[p,g.row];return y=>{if(y.kind==="send")throw new Error("Workflow opening rows only open a thread");let{threadId:b}=y,k=y.kind==="fork"?Ht:this.openingRefusal(d,{instance:i,inputId:s,conversation:f},y)??this.pinsProblem(w);return k!==null?{refuse:{message:k}}:O.map(x=>I.entry(x,{threadId:b}))}}proposed(e){try{let t=this.ctx.presets.installationsFor(e.installations,"creation").map(({preset_id:i,config:s},r)=>({preset_id:i,config:s,lifetime:e.installations[r].lifetime}));return{environment:On(e.environment),installations:t}}catch(t){throw new E(`The proposed conversation is refused: ${le(t)}`)}}async pins(e,t,i,s){let r=async(c,d,u,f)=>{let l=await this.providers.pin(c,d,u,s);if("refused"in l)throw new E(`${f}: ${l.refused}`);return l},o={};for(let[c,d]of Object.entries(e.participants)){let u=d.context==="isolated"?this.configuration(c,d,t,i[c]):null,f=u===null?t:{environment:u.environment,installations:u.installations.map(({preset_id:w,config:p,lifetime:h})=>({preset_id:w,config:p,lifetime:h}))},l={};for(let w of Object.values(e.steps))for(let p of w.assignments)if(p.participant===c)for(let h of p.checks)l[h]??=await r("check",h,f,`Participant ${c}`);o[c]={configuration:u,checks:l}}let a={};for(let c of Object.values(e.steps))for(let d of Object.values(c.inputs))"evidence"in d&&(a[d.evidence]??=await r("evidence",d.evidence,t,"Evidence"));return{participants:o,evidence:a}}configuration(e,t,i,s){let r=c=>{throw new E(`Participant ${e}: ${c}`)},o={provider:s.provider,model:s.model,thinking_effort:s.thinkingEffort},a=this.config.roles[t.role](i);if("refused"in a)return r(`role ${t.role} refuses this conversation: ${a.refused}`);for(let c of a.installations)(c.lifetime!=="defining"||c.installationId===void 0)&&r(`role ${t.role}'s installation of ${c.presetId} must be defining and name its id`);try{return{agent:a.agent,route:o,environment:On(a.environment),installations:this.ctx.presets.initialInstallations(a.installations,a.agent)}}catch(c){return r(`role ${t.role} resolves a configuration that is refused: ${le(c)}`)}}openingRefusal(e,t,i){let{threadId:s,runId:r,inputId:o,opening:a}=i;if(s!==t.instance)return`The workflow opens on thread ${t.instance}, not ${s}`;if(a.origin!==null)return Ht;let{spec:c}=a;if(!c.requires.includes(T))return`The conversation must require ${T}`;if(o!==t.inputId)return`The conversation's first input must be ${t.inputId}`;let d=e.steps[e.entry].assignments.some(({participant:f})=>e.participants[f]?.context==="conversation");if(d&&r===null)return"The conversation's creation must wake it: its entry step is its own";if(!d&&r!==null)return"The conversation's creation must not wake it: its entry step is not its own, and nothing runs it merely to start participants";let u={environment:On(c.environment),installations:c.installations.map(({preset_id:f,config:l,lifetime:w})=>({preset_id:f,config:l,lifetime:w}))};if(U(u)!==U(t.conversation))return"The conversation is not created as the workflow's opening was prepared for: its environment or installations differ";for(let f of e.prerequisites){let l=this.config.prerequisites[f](a);if(l!==null)return`Prerequisite ${f} is not met: ${l}`}return null}read(e){return this.view(e)}decide(e,t){let i=is.parse(e),{instanceId:s}=i,r={operationId:i.operationId,decision:i.decision,verdict:i.verdict,option:i.option??null,feedback:i.feedback??null,restart:i.restart??[],actor:t};return this.retaining(this.subtree(s),async()=>{this.refuseQuarantined(s);for(let o=0;o<os;o+=1){let{plan:a,evidence:c}=await this.prepareDecision(s,r);if(a.kind==="replay")return this.view(s);if(a.kind==="refused")throw new E(a.reason);let[d,u]=a.rows,f=this.transition(s,u,d),l=g=>f.rootWork||En(this.ctx.journal.state(ie,g).requests),w=l(s)?{releases:!0,chain:"person"}:void 0,p=await this.send(s,f,w,({threadId:g,runId:O})=>{let y=this.quarantined(g);if(y!==null)return{refuse:{message:`${$e}: ${y}`}};let b=this.planDecision(g,r,c);if(b.kind!=="rows")return{refuse:{message:b.kind==="refused"?b.reason:"already recorded"}};let k=this.entryProblem(g,b.rows);return k!==null?{refuse:{message:`the selected step cannot be entered: ${k}`}}:U(b.rows)!==U(a.rows)?{refuse:{message:"The instance changed while the decision was written: decide again"}}:l(g)!==(O!==null)?{refuse:{message:"A Stop changed while the decision was written",code:Vt}}:b.rows.map(x=>I.entry(x,{threadId:g}))});if(p.status==="queued")return this.view(s);if(p.status==="refused"&&p.refusal.code===Vt)continue;this.refuseQuarantined(s);let h=this.planDecision(s,r,c);if(h.kind==="replay")return this.view(s);throw h.kind==="refused"?new E(h.reason):new E(p.status==="refused"?p.refusal.message:`The input of decision ${r.decision} was already sent`)}throw new E("The conversation was stopped and resumed while deciding: decide again")})}cancel(e,t){let{operationId:i,instanceId:s,reason:r}=ss.parse(e);return this.holding(s,async()=>{this.refuseQuarantined(s);let{value:o}=await this.ctx.journal.transact(s,()=>{let a=this.quarantined(s);if(a!==null)return{rows:[],value:`${$e}: ${a}`};let c=$t(this.state(s),{operationId:i,reason:r,actor:t});return{rows:c.kind==="rows"?c.rows.map(d=>I.entry(d,{threadId:s})):[],value:c.kind==="refused"?c.reason:null}});if(o!==null)throw new E(o);return await this.cleanUp(s),this.view(s)})}async stop(e,t,i){for(let s=0;;s+=1){let r=await this.ctx.threads.retain(()=>[e],{lastRelease:"keep"});try{let o=this.stopping(e),a=await this.ctx.threads.retain(()=>o.children,{lastRelease:"keep"});try{await i(c=>{if(c!==e)throw new Error(`A Stop of thread ${e} was written on ${c}`);return this.stopRows(e,t,o)});return}finally{a.release()}}catch(o){if(!(o instanceof Oe)||s+1>=rs)throw o}finally{r.release()}}}stopping(e){let t=this.ctx.journal.summary(e);if(t===void 0||!F(t)||this.quarantined(e)!==null)return{attempt:null,children:[]};let i=this.ctx.journal.state(q,e),s=Q(i).map(({thread:r})=>r).filter(r=>this.ctx.journal.summary(r)!==void 0);return{attempt:i.current?.entered.attempt??null,children:s}}stopRows(e,t,i){let s=this.stopping(e);if(s.attempt!==i.attempt)throw new Oe(`it met attempt ${s.attempt}`);let r=s.children.filter(c=>!this.ctx.journal.isLoaded(c));if(r.length>0)throw new Oe(`${r.join(", ")} not loaded`);if(s.attempt===null||this.quarantined(e)!==null)return[];let o=this.ctx.journal.state(q,e),a=St(this.ctx.journal,o,this.procedure(o),t);return a===null?[]:[I.entry(a,{threadId:e})]}state(e){let t=this.ctx.journal.state(q,e);if(t.opened===null)throw new E(`No workflow instance is open on thread ${e}`);return t}view(e){let t=this.state(e),{state:i}=this.ctx.threads.inspect(e,{basis:"catalog"}),{pins:s}=t.opened,r=Kt(t,this.definition(t.opened.definition),{barrier:this.barrier(e,t),starts:this.participants.states(e,t),settled:i==="idle"||i==="held",provider:o=>"check"in o?this.providers.problem("check",o.check,s.participants[o.participant]?.checks[o.check]):this.providers.problem("evidence",o.evidence,s.evidence[o.evidence]),entryRefusal:this.entryRefusals.get(e)??null});return ue(structuredClone(r))}barrier(e,t){return Rt(this.ctx,t,this.participants.starting(e))}executable(e){return ut(e,this.definition(e.opened.definition))}procedure(e){return this.executable(e)??e.opened.canonical}planDecision(e,t,i){let s=this.state(e);return yt(s,this.executable(s),t,this.barrier(e,s),i)}async prepareDecision(e,t){try{return await this.withEvidence(e,this.state(e).opened.pins,i=>this.planDecision(e,t,i),$n)}catch(i){throw i instanceof V?new E(`The decision's evidence: ${i.message}`):i}}owed(e,t){let i=this.ctx.journal.state(q,e);if(i.opened===null)return{kind:"none"};let s=this.executable(i);return s===void 0?{kind:"none"}:X(i,s,t)}async advance(e){let t;try{let d=this.ctx.journal.state(q,e);if(d.opened===null)return;t=await this.withEvidence(e,d.opened.pins,u=>this.owed(e,u),$n)}catch(d){if(d instanceof V||d instanceof E)return;throw d}let{plan:i,evidence:s}=t;if(i.kind==="none")return;if(i.kind==="refused"){s!==void 0&&this.entryRefusals.set(e,{transition:s.transition,reason:i.reason});return}let{row:r}=i,o=this.transition(e,r,null),a=o.rootWork?{releases:!1,chain:"fresh"}:void 0,c=await this.send(e,o,a,()=>{let d=this.quarantined(e);if(d!==null)return{refuse:{message:`${$e}: ${d}`}};let u=this.owed(e,s);if(u.kind!=="transition"||U(u.row)!==U(r))return{refuse:{message:"The transition is no longer owed"}};let f=this.entryProblem(e,[r]);return f!==null?{refuse:{message:f}}:[I.entry(r,{threadId:e})]});c.status==="refused"&&this.entryRefusals.set(e,{transition:r.transition,reason:c.refusal.message})}entryProblem(e,t){let i=this.state(e),{pins:s}=i.opened,r=this.procedure(i);for(let o of t){if(o.op!=="entered")continue;let a=r.steps[o.step];for(let c of Object.values(a?.inputs??{})){if(!("evidence"in c))continue;let d=this.providers.problem("evidence",c.evidence,s.evidence[c.evidence]);if(d!==null)return d}for(let c of o.assignments){let{participant:d}=c;for(let u of c.context==="isolated"?c.checks:[]){let f=this.providers.problem("check",u,s.participants[d]?.checks[u]);if(f!==null)return f}}}return null}pinsProblem(e){for(let{checks:t}of Object.values(e.participants))for(let[i,s]of Object.entries(t)){let r=this.providers.problem("check",i,s);if(r!==null)return r}for(let[t,i]of Object.entries(e.evidence)){let s=this.providers.problem("evidence",t,i);if(s!==null)return s}return null}async withEvidence(e,t,i,s){let r;for(let o=0;o<as;o+=1){let a=i(r);if(a.kind!=="needs")return{plan:a,evidence:r};let{needs:c}=a,d=await Promise.all(c.inputs.map(async({input:u,provider:f})=>[u,await this.providers.acquire(f,t.evidence[f],{instance:e,attempt:c.attempt,step:c.step,input:u,signal:s})]));r={transition:c.transition,attempt:c.attempt,inputs:new Map(d)}}throw new E("The instance moved on each time its entry evidence was acquired: try again")}send(e,t,i,s){let r=this.ctx.threads.get(e);if(r===void 0)throw new Error(`Unknown thread: ${e}`);let{input:o}=t;return r.send(o.message,{id:o.id,source:o.source,target:t.rootWork?"next-turn":"next-step",...i===void 0?{}:{wake:i},rows:s})}transition(e,t,i){let s=this.state(e),r=this.executable(s),o=t.op==="entered"?t.assignments.find(({participant:u})=>r.participants[u]?.context==="conversation"):void 0,a=`Workflow ${r.id}:`,c=u=>a+=`${a.endsWith(">")?`
11
+ `:" "}${u}`;if(i!==null){let u=s.attempts.get(i.attempt).entered.step;c(bn(u,i))}else t.cause.kind==="routed"&&c(`step ${s.results.get(t.cause.result).step}'s result was accepted.`);t.op==="completed"?c("The workflow completed."):o!==void 0?c(`Step ${t.step} was assigned to you as ${o.id}.`):t.assignments.length>0&&c(`Step ${t.step} was handed to other participants.`);let d=qe(t.transition);return{row:t,input:{id:d,message:{role:"developer",content:a},source:et.record({transition:t.transition,assignment:o?.id??null,decision:i?.decision??null})},rootWork:o!==void 0}}cleanUp(e){let t=this.cleanups.get(e);if(t!==void 0)return t;this.cancelling.add(e);let i=this.runCleanup(e).finally(()=>this.cleanups.delete(e));return this.cleanups.set(e,i),i}async runCleanup(e){if(this.quarantined(e)!==null)return;let{cancellation:t,terminal:i}=this.state(e);if(t===null||i!==null)return;let s=this.ctx.threads.get(e),r=`The workflow was cancelled: ${t.reason}`,o=this.ctx.get("delegations");await s.cancel(r,{intent:"cleanup",...o===void 0?{}:{rows:o.stopObligationRows(r)}}),await o?.retryStops(e);let a=this.ctx.get("commands");if(a!==void 0){let c=this.ctx.journal.origins.subtree(e,{self:!0});await Promise.all(c.map(({thread_id:d})=>a.stopAll({threadId:d},"its workflow was cancelled")))}await this.ctx.journal.flush(),await this.finishCleanup(e)}async finishCleanup(e){await this.ctx.journal.transact(e,()=>{let t={rows:[],value:void 0};if(this.quarantined(e)!==null)return t;let i=this.state(e);if(i.terminal!==null||i.cancellation===null)return this.cancelling.delete(e),t;let{state:s,operation:r}=this.ctx.threads.inspect(e,{basis:"catalog"});if(s!=="idle"&&s!=="held"||r!==null)return t;let{draining:o,unresolved:a}=this.barrier(e,i).cleanup;return o.length>0||a.length>0?t:{rows:[I.entry({op:"cancelled",operation_id:i.cancellation.operation_id},{threadId:e})],value:void 0}})}governs(e){if(F(e))return!0;if(e.origin===null)return!1;let t=this.ctx.journal.origins.callers(e.thread_id).threads.at(-1);return t!==void 0&&t.origin===null&&F(t)}evaluate(e){let t=this.ctx.journal.origins.callers(e).threads,i=t.at(-1),s=i===void 0?null:this.quarantined(i.thread_id);if(s!==null)return{kind:"hold",reason:`${$e}: ${s}`};let r=i===void 0?void 0:Ue.read(this.ctx.journal,i.thread_id);if(r?.status!=="available")return{kind:"hold",reason:`Its workflow instance is unreadable: ${r?.status==="unavailable"?r.reason:"its instance state has not been computed"}`};let o=r.value;if(o===null)return{kind:"allow"};if(o.terminal)return{kind:"allow"};let{references:a}=o,c=this.definition(a.id),d=ge(a,c);if(d!==null)return{kind:"hold",reason:`Its workflow cannot continue: ${d}`};let u=t.find(({origin:f})=>de(f,i.thread_id))?.thread_id??i.thread_id;for(let{thread:f,check:l,pin:w}of o.checks){if(f!==u)continue;let p=this.providers.problem("check",l,w??void 0);if(p!==null)return{kind:"hold",reason:`Its workflow's ${p}`}}return{kind:"allow"}}governedChanged(e){for(let{thread_id:t}of this.ctx.journal.origins.subtree(e,{self:!0}))this.constraint?.changed(t),this.participants.changed(t)}runnable(e){let t=Ue.read(this.ctx.journal,e);if(t.status==="unavailable")return{unavailable:t.reason};if(t.status==="not-applicable")return{unavailable:"its instance state has not been computed"};let i=t.value;return i===null?{unavailable:"no instance is open on it"}:i.terminal?"allow-all":i.runnable}providerChanged(){let e=new Set(this.entryRefusals.keys());this.entryRefusals.clear();for(let t of this.ctx.journal.summaries()){let i=Ue.read(this.ctx.journal,t.thread_id);if(i.status!=="available")continue;let s=i.value;s!==null&&s.checks.length>0&&(this.governedChanged(t.thread_id),e.add(t.thread_id)),this.ctx.journal.isLoaded(t.thread_id)&&this.participants.held(t.thread_id)&&e.add(t.thread_id)}for(let t of e)this.track(this.reconcile(t))}onRow(e){let t=this.ctx.journal.summary(e.thread_id);if(t===void 0||(F(t)&&t.origin===null&&(e.type==="ext"&&this.governedChanged(e.thread_id),(I.read(e)!==void 0||e.type==="turn/end")&&this.track(this.reconcile(e.thread_id))),!cs.has(e.type)))return;let i=this.ctx.journal.origins.callers(e.thread_id).threads.at(-1),s=i?.thread_id;s===void 0||!F(i)||(this.cancelling.has(s)?this.track(this.holding(s,()=>this.finishCleanup(s))):s!==e.thread_id&&this.ctx.journal.isLoaded(s)&&this.participants.waiting(s)&&this.track(this.reconcile(s)))}reconcile(e){if(this.closing||!this.repairing||this.quarantined(e)!==null)return Promise.resolve();let t=this.passes.get(e);if(t!==void 0)return t.again=!0,t.done;let i={again:!0,done:Promise.resolve()};return i.done=(async()=>{try{for(;i.again&&!this.closing;)i.again=!1,await this.holding(e,async()=>{let s=this.ctx.journal.state(q,e);if(!(s.opened===null||s.terminal!==null)){if(s.cancellation!==null){this.cancelling.has(e)&&await this.finishCleanup(e);return}await this.observe(e),await this.advance(e),this.participants.dispatch(e)}})}finally{this.passes.delete(e)}})(),this.passes.set(e,i),i.done}async refused(e,t,i){await this.retaining(this.subtree(e),()=>this.ctx.journal.transact(e,()=>{let s={rows:[],value:void 0};if(this.quarantined(e)!==null)return s;let r=this.state(e),o=jt(this.ctx.journal,r,t,i);if(o===null)return s;let a=o.observations[0].thread;return this.ctx.journal.summary(a)!==void 0&&this.ctx.get("delegations")?.continuation(a)?.kind!=="refusal"?s:{rows:[I.entry(o,{threadId:e}),...pn(this.ctx,r,o)],value:void 0}}))}async observe(e){let t=this.ctx.journal.state(q,e),i=[...new Set(Q(t).map(({thread:r})=>r))].filter(r=>this.ctx.journal.summary(r)!==void 0);if(i.length===0)return;let s=await this.ctx.threads.retain(()=>i,{lastRelease:"keep"});try{await this.ctx.journal.transact(e,()=>{if(this.quarantined(e)!==null)return{rows:[],value:void 0};let r=this.state(e);return{rows:xt(this.ctx.journal,r).flatMap(a=>[I.entry(a,{threadId:e}),...pn(this.ctx,r,a)]),value:void 0}})}finally{s.release()}}track(e){let t=e.catch(i=>this.ctx.reportError(i)).finally(()=>this.sends.delete(t));this.sends.add(t)}async repair(){for(let t of await this.refreshed.catch(()=>[]))this.ctx.reportError(new Error(`The workflow instance of thread ${t} could not be read into the catalog`));this.repairing=!0;let e=this.ctx.journal.summaries().filter(t=>F(t)&&t.origin===null).map(t=>t.thread_id);for(let t of e)try{await(await this.holding(t,()=>{let s=this.ctx.journal.state(q,t);return s.opened!==null&&s.terminal===null&&s.cancellation!==null})?this.holding(t,()=>this.cleanUp(t)):this.reconcile(t))}catch(i){let s=Tn(i);this.unrepaired.set(t,s),this.ctx.reportError(new Error(`The workflow instance of thread ${t} could not be repaired: ${s}`,{cause:i})),this.governedChanged(t)}}quarantined(e){return this.unrepaired.get(e)??null}refuseQuarantined(e){let t=this.quarantined(e);if(t!==null)throw new E(`${$e}: ${t}`)}holding(e,t){return this.retaining(()=>[e],t)}async retaining(e,t){let i=await this.ctx.threads.retain(e,{lastRelease:"keep"});try{return await t()}finally{i.release()}}subtree(e){return()=>this.ctx.journal.origins.subtree(e,{self:!0}).map(({thread_id:t})=>t)}async shutdown(){this.closing=!0,await Promise.allSettled([this.providers.drain(),this.participants.shutdown(),...this.sends,...this.cleanups.values(),...[...this.passes.values()].map(({done:e})=>e)])}},cs=new Set(["turn/end","run/settled","thread/cancel","inbox/spliced","ext"]),On=n=>({cwd:Cn(n.cwd,"A conversation cwd")});function ds(n){if(!_(n)||n.current===null)return[];let{pins:e}=n.opened;return n.current.assignments.flatMap(({participant:t,thread:i,checks:s,state:r})=>r==="accepted"||r==="superseded"?[]:s.map(o=>{let a=e.participants[t]?.checks[o];return{thread:i,check:o,pin:a===void 0?null:{config:G(a.config)}}}))}var _o={name:"lagent:workflows",async apply(n,e){Rn(n,"workflows"),await n.plugin(Je,e)}};export{N as SUBMIT_TOOL,T as WORKFLOWS_OWNER,J as WorkflowDefinitionError,E as WorkflowRefusedError,vi as defineWorkflow,gn as resolveParticipantModels,_o as workflows};
@@ -1,8 +1,8 @@
1
1
  import { type PluginJournal } from '../../journal/index.js';
2
- import { type AnswerCheck } from '../../loop/index.js';
3
- import type { Seq } from '../../models/index.js';
2
+ import { type AnswerCheck } from '../../state/index.js';
3
+ import type { Seq } from '../../records/journal.js';
4
4
  import { type AssignmentOutcome } from '../delegations/index.js';
5
- import { type AssignmentView, type InstanceState, type ResponseCalls } from './fold.js';
5
+ import { type AssignmentView, type InstanceState, type ResponseCalls } from './state.js';
6
6
  import { type InstanceRowInput, type Observation, type Submission } from './record.js';
7
7
  import type { CanonicalDefinition } from './definition.js';
8
8
  import type { ReadonlyState } from '../../util/foldState.js';
@@ -0,0 +1,28 @@
1
+ import { z } from 'zod';
2
+ import type { WorkflowDefinition } from './definition.js';
3
+ import type { InstanceView } from './view.js';
4
+ /** Complete models a starter chose for configurable isolated participants, by participant. */
5
+ export declare const ModelChoicesSchema: z.ZodRecord<z.ZodString, z.ZodObject<{
6
+ provider: z.ZodString;
7
+ model: z.ZodString;
8
+ thinkingEffort: z.ZodNullable<z.ZodEnum<{
9
+ low: "low";
10
+ high: "high";
11
+ none: "none";
12
+ medium: "medium";
13
+ max: "max";
14
+ }>>;
15
+ }, z.core.$strict>>;
16
+ /**
17
+ * Every isolated participant's model for an opening: the default its definition names (an omitted
18
+ * effort is `null`) or the starter's complete choice, which only a configurable participant takes.
19
+ * `choices` is the untrusted choice map as `OpeningRequest.models` takes it. Refused, with
20
+ * {@link WorkflowRefusedError}, are: a choice that fails the schema, one for an unknown, conversation or
21
+ * fixed-model participant, and a provider outside `allowedProviders`, a definition's default
22
+ * included. Nothing is read or recorded: the result is fresh and its inputs are untouched.
23
+ */
24
+ export declare function resolveParticipantModels(definition: WorkflowDefinition, choices: unknown, allowedProviders: readonly string[]): InstanceView['models'];
25
+ /** A request the workflow refuses, with why: nothing was recorded. */
26
+ export declare class WorkflowRefusedError extends Error {
27
+ constructor(reason: string);
28
+ }
@@ -1,7 +1,7 @@
1
1
  import type { Context } from '@leuduan/cordis';
2
2
  import type { WorkflowDefinition } from './definition.js';
3
- import { type InstanceState } from './fold.js';
4
- import type { CheckProvider, Providers } from './providers.js';
3
+ import { type InstanceState } from './state.js';
4
+ import type { CheckProvider, Providers } from './hostBindings.js';
5
5
  import type { ReadonlyState } from '../../util/foldState.js';
6
6
  /**
7
7
  * Where an open isolated assignment's start stands, as the instance's view shows it: its task is
@@ -1,8 +1,8 @@
1
1
  import { type Context } from '@leuduan/cordis';
2
2
  import { z } from 'zod';
3
- import type { Seq, ThreadOrigin } from '../../models/index.js';
3
+ import type { Seq, ThreadOrigin } from '../../records/journal.js';
4
4
  import type { WorkflowDefinition } from './definition.js';
5
- import { type AssignmentView, type InstanceState } from './fold.js';
5
+ import { type AssignmentView, type InstanceState } from './state.js';
6
6
  import { type ChildFacts } from './observations.js';
7
7
  import type { ReadonlyState } from '../../util/foldState.js';
8
8
  /**