@langchain/langgraph 0.4.8 → 1.0.0-alpha.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 (494) hide show
  1. package/CHANGELOG.md +7 -2
  2. package/README.md +1 -2
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/channels/any_value.d.cts +26 -0
  5. package/dist/channels/any_value.d.cts.map +1 -0
  6. package/dist/channels/any_value.d.ts +16 -9
  7. package/dist/channels/any_value.d.ts.map +1 -0
  8. package/dist/channels/base.cjs +97 -133
  9. package/dist/channels/base.cjs.map +1 -0
  10. package/dist/channels/base.d.cts +73 -0
  11. package/dist/channels/base.d.cts.map +1 -0
  12. package/dist/channels/base.d.ts +73 -74
  13. package/dist/channels/base.d.ts.map +1 -0
  14. package/dist/channels/base.js +94 -127
  15. package/dist/channels/base.js.map +1 -1
  16. package/dist/channels/binop.cjs +47 -77
  17. package/dist/channels/binop.cjs.map +1 -0
  18. package/dist/channels/binop.d.cts +22 -0
  19. package/dist/channels/binop.d.cts.map +1 -0
  20. package/dist/channels/binop.d.ts +17 -12
  21. package/dist/channels/binop.d.ts.map +1 -0
  22. package/dist/channels/binop.js +45 -71
  23. package/dist/channels/binop.js.map +1 -1
  24. package/dist/channels/dynamic_barrier_value.d.cts +43 -0
  25. package/dist/channels/dynamic_barrier_value.d.cts.map +1 -0
  26. package/dist/channels/dynamic_barrier_value.d.ts +20 -34
  27. package/dist/channels/dynamic_barrier_value.d.ts.map +1 -0
  28. package/dist/channels/ephemeral_value.cjs +46 -70
  29. package/dist/channels/ephemeral_value.cjs.map +1 -0
  30. package/dist/channels/ephemeral_value.d.cts +23 -0
  31. package/dist/channels/ephemeral_value.d.cts.map +1 -0
  32. package/dist/channels/ephemeral_value.d.ts +18 -11
  33. package/dist/channels/ephemeral_value.d.ts.map +1 -0
  34. package/dist/channels/ephemeral_value.js +45 -65
  35. package/dist/channels/ephemeral_value.js.map +1 -1
  36. package/dist/channels/index.cjs +2 -10
  37. package/dist/channels/index.js +4 -3
  38. package/dist/channels/last_value.cjs +91 -140
  39. package/dist/channels/last_value.cjs.map +1 -0
  40. package/dist/channels/last_value.d.cts +30 -0
  41. package/dist/channels/last_value.d.cts.map +1 -0
  42. package/dist/channels/last_value.d.ts +15 -23
  43. package/dist/channels/last_value.d.ts.map +1 -0
  44. package/dist/channels/last_value.js +88 -133
  45. package/dist/channels/last_value.js.map +1 -1
  46. package/dist/channels/named_barrier_value.cjs +114 -170
  47. package/dist/channels/named_barrier_value.cjs.map +1 -0
  48. package/dist/channels/named_barrier_value.d.cts +33 -0
  49. package/dist/channels/named_barrier_value.d.cts.map +1 -0
  50. package/dist/channels/named_barrier_value.d.ts +19 -29
  51. package/dist/channels/named_barrier_value.d.ts.map +1 -0
  52. package/dist/channels/named_barrier_value.js +112 -163
  53. package/dist/channels/named_barrier_value.js.map +1 -1
  54. package/dist/channels/topic.cjs +63 -96
  55. package/dist/channels/topic.cjs.map +1 -0
  56. package/dist/channels/topic.d.cts +26 -0
  57. package/dist/channels/topic.d.cts.map +1 -0
  58. package/dist/channels/topic.d.ts +21 -18
  59. package/dist/channels/topic.d.ts.map +1 -0
  60. package/dist/channels/topic.js +61 -90
  61. package/dist/channels/topic.js.map +1 -1
  62. package/dist/constants.cjs +373 -452
  63. package/dist/constants.cjs.map +1 -0
  64. package/dist/constants.d.cts +284 -0
  65. package/dist/constants.d.cts.map +1 -0
  66. package/dist/constants.d.ts +112 -148
  67. package/dist/constants.d.ts.map +1 -0
  68. package/dist/constants.js +333 -444
  69. package/dist/constants.js.map +1 -1
  70. package/dist/errors.cjs +150 -179
  71. package/dist/errors.cjs.map +1 -0
  72. package/dist/errors.d.cts +80 -0
  73. package/dist/errors.d.cts.map +1 -0
  74. package/dist/errors.d.ts +55 -46
  75. package/dist/errors.d.ts.map +1 -0
  76. package/dist/errors.js +139 -165
  77. package/dist/errors.js.map +1 -1
  78. package/dist/func/index.cjs +289 -290
  79. package/dist/func/index.cjs.map +1 -0
  80. package/dist/func/index.d.cts +289 -0
  81. package/dist/func/index.d.cts.map +1 -0
  82. package/dist/func/index.d.ts +81 -71
  83. package/dist/func/index.d.ts.map +1 -0
  84. package/dist/func/index.js +285 -284
  85. package/dist/func/index.js.map +1 -1
  86. package/dist/func/types.d.cts +64 -0
  87. package/dist/func/types.d.cts.map +1 -0
  88. package/dist/func/types.d.ts +24 -19
  89. package/dist/func/types.d.ts.map +1 -0
  90. package/dist/graph/annotation.cjs +96 -120
  91. package/dist/graph/annotation.cjs.map +1 -0
  92. package/dist/graph/annotation.d.cts +116 -0
  93. package/dist/graph/annotation.d.cts.map +1 -0
  94. package/dist/graph/annotation.d.ts +33 -34
  95. package/dist/graph/annotation.d.ts.map +1 -0
  96. package/dist/graph/annotation.js +91 -112
  97. package/dist/graph/annotation.js.map +1 -1
  98. package/dist/graph/graph.cjs +419 -723
  99. package/dist/graph/graph.cjs.map +1 -0
  100. package/dist/graph/graph.d.cts +131 -0
  101. package/dist/graph/graph.d.cts.map +1 -0
  102. package/dist/graph/graph.d.ts +122 -91
  103. package/dist/graph/graph.d.ts.map +1 -0
  104. package/dist/graph/graph.js +412 -713
  105. package/dist/graph/graph.js.map +1 -1
  106. package/dist/graph/index.cjs +4 -17
  107. package/dist/graph/index.js +6 -5
  108. package/dist/graph/message.cjs +59 -104
  109. package/dist/graph/message.cjs.map +1 -0
  110. package/dist/graph/message.d.cts +19 -0
  111. package/dist/graph/message.d.cts.map +1 -0
  112. package/dist/graph/message.d.ts +11 -10
  113. package/dist/graph/message.d.ts.map +1 -0
  114. package/dist/graph/message.js +56 -98
  115. package/dist/graph/message.js.map +1 -1
  116. package/dist/graph/messages_annotation.cjs +106 -106
  117. package/dist/graph/messages_annotation.cjs.map +1 -0
  118. package/dist/graph/messages_annotation.d.cts +111 -0
  119. package/dist/graph/messages_annotation.d.cts.map +1 -0
  120. package/dist/graph/messages_annotation.d.ts +18 -9
  121. package/dist/graph/messages_annotation.d.ts.map +1 -0
  122. package/dist/graph/messages_annotation.js +100 -100
  123. package/dist/graph/messages_annotation.js.map +1 -1
  124. package/dist/graph/state.cjs +476 -777
  125. package/dist/graph/state.cjs.map +1 -0
  126. package/dist/graph/state.d.cts +216 -0
  127. package/dist/graph/state.d.cts.map +1 -0
  128. package/dist/graph/state.d.ts +129 -134
  129. package/dist/graph/state.d.ts.map +1 -0
  130. package/dist/graph/state.js +470 -768
  131. package/dist/graph/state.js.map +1 -1
  132. package/dist/graph/zod/index.cjs +10 -21
  133. package/dist/graph/zod/index.d.cts +3 -0
  134. package/dist/graph/zod/index.d.ts +3 -3
  135. package/dist/graph/zod/index.js +4 -4
  136. package/dist/graph/zod/meta.cjs +142 -177
  137. package/dist/graph/zod/meta.cjs.map +1 -0
  138. package/dist/graph/zod/meta.d.cts +116 -0
  139. package/dist/graph/zod/meta.d.cts.map +1 -0
  140. package/dist/graph/zod/meta.d.ts +99 -97
  141. package/dist/graph/zod/meta.d.ts.map +1 -0
  142. package/dist/graph/zod/meta.js +136 -170
  143. package/dist/graph/zod/meta.js.map +1 -1
  144. package/dist/graph/zod/plugin.cjs +36 -39
  145. package/dist/graph/zod/plugin.cjs.map +1 -0
  146. package/dist/graph/zod/plugin.js +34 -35
  147. package/dist/graph/zod/plugin.js.map +1 -1
  148. package/dist/graph/zod/schema.cjs +82 -110
  149. package/dist/graph/zod/schema.cjs.map +1 -0
  150. package/dist/graph/zod/schema.d.cts +38 -0
  151. package/dist/graph/zod/schema.d.cts.map +1 -0
  152. package/dist/graph/zod/schema.d.ts +12 -6
  153. package/dist/graph/zod/schema.d.ts.map +1 -0
  154. package/dist/graph/zod/schema.js +77 -103
  155. package/dist/graph/zod/schema.js.map +1 -1
  156. package/dist/graph/zod/zod-registry.cjs +41 -47
  157. package/dist/graph/zod/zod-registry.cjs.map +1 -0
  158. package/dist/graph/zod/zod-registry.d.cts +51 -0
  159. package/dist/graph/zod/zod-registry.d.cts.map +1 -0
  160. package/dist/graph/zod/zod-registry.d.ts +34 -26
  161. package/dist/graph/zod/zod-registry.d.ts.map +1 -0
  162. package/dist/graph/zod/zod-registry.js +37 -41
  163. package/dist/graph/zod/zod-registry.js.map +1 -1
  164. package/dist/hash.cjs +205 -267
  165. package/dist/hash.cjs.map +1 -0
  166. package/dist/hash.js +205 -265
  167. package/dist/hash.js.map +1 -1
  168. package/dist/index.cjs +110 -33
  169. package/dist/index.cjs.map +1 -0
  170. package/dist/index.d.cts +25 -0
  171. package/dist/index.d.ts +25 -5
  172. package/dist/index.js +18 -7
  173. package/dist/index.js.map +1 -1
  174. package/dist/interrupt.cjs +79 -85
  175. package/dist/interrupt.cjs.map +1 -0
  176. package/dist/interrupt.d.cts +49 -0
  177. package/dist/interrupt.d.cts.map +1 -0
  178. package/dist/interrupt.d.ts +6 -1
  179. package/dist/interrupt.d.ts.map +1 -0
  180. package/dist/interrupt.js +76 -80
  181. package/dist/interrupt.js.map +1 -1
  182. package/dist/prebuilt/agentName.cjs +139 -172
  183. package/dist/prebuilt/agentName.cjs.map +1 -0
  184. package/dist/prebuilt/agentName.d.cts +42 -0
  185. package/dist/prebuilt/agentName.d.cts.map +1 -0
  186. package/dist/prebuilt/agentName.d.ts +13 -21
  187. package/dist/prebuilt/agentName.d.ts.map +1 -0
  188. package/dist/prebuilt/agentName.js +139 -168
  189. package/dist/prebuilt/agentName.js.map +1 -1
  190. package/dist/prebuilt/agent_executor.cjs +42 -80
  191. package/dist/prebuilt/agent_executor.cjs.map +1 -0
  192. package/dist/prebuilt/agent_executor.d.cts +57 -0
  193. package/dist/prebuilt/agent_executor.d.cts.map +1 -0
  194. package/dist/prebuilt/agent_executor.d.ts +46 -37
  195. package/dist/prebuilt/agent_executor.d.ts.map +1 -0
  196. package/dist/prebuilt/agent_executor.js +40 -75
  197. package/dist/prebuilt/agent_executor.js.map +1 -1
  198. package/dist/prebuilt/chat_agent_executor.cjs +66 -128
  199. package/dist/prebuilt/chat_agent_executor.cjs.map +1 -0
  200. package/dist/prebuilt/chat_agent_executor.d.cts +23 -0
  201. package/dist/prebuilt/chat_agent_executor.d.cts.map +1 -0
  202. package/dist/prebuilt/chat_agent_executor.d.ts +18 -10
  203. package/dist/prebuilt/chat_agent_executor.d.ts.map +1 -0
  204. package/dist/prebuilt/chat_agent_executor.js +63 -123
  205. package/dist/prebuilt/chat_agent_executor.js.map +1 -1
  206. package/dist/prebuilt/index.cjs +15 -18
  207. package/dist/prebuilt/index.d.cts +8 -0
  208. package/dist/prebuilt/index.d.ts +8 -8
  209. package/dist/prebuilt/index.js +8 -7
  210. package/dist/prebuilt/interrupt.d.cts +73 -0
  211. package/dist/prebuilt/interrupt.d.cts.map +1 -0
  212. package/dist/prebuilt/interrupt.d.ts +32 -15
  213. package/dist/prebuilt/interrupt.d.ts.map +1 -0
  214. package/dist/prebuilt/react_agent_executor.cjs +317 -473
  215. package/dist/prebuilt/react_agent_executor.cjs.map +1 -0
  216. package/dist/prebuilt/react_agent_executor.d.cts +229 -0
  217. package/dist/prebuilt/react_agent_executor.d.cts.map +1 -0
  218. package/dist/prebuilt/react_agent_executor.d.ts +171 -143
  219. package/dist/prebuilt/react_agent_executor.d.ts.map +1 -0
  220. package/dist/prebuilt/react_agent_executor.js +315 -465
  221. package/dist/prebuilt/react_agent_executor.js.map +1 -1
  222. package/dist/prebuilt/tool_executor.cjs +45 -67
  223. package/dist/prebuilt/tool_executor.cjs.map +1 -0
  224. package/dist/prebuilt/tool_executor.d.cts +42 -0
  225. package/dist/prebuilt/tool_executor.d.cts.map +1 -0
  226. package/dist/prebuilt/tool_executor.d.ts +30 -24
  227. package/dist/prebuilt/tool_executor.d.ts.map +1 -0
  228. package/dist/prebuilt/tool_executor.js +44 -63
  229. package/dist/prebuilt/tool_executor.js.map +1 -1
  230. package/dist/prebuilt/tool_node.cjs +213 -275
  231. package/dist/prebuilt/tool_node.cjs.map +1 -0
  232. package/dist/prebuilt/tool_node.d.cts +151 -0
  233. package/dist/prebuilt/tool_node.d.cts.map +1 -0
  234. package/dist/prebuilt/tool_node.d.ts +30 -17
  235. package/dist/prebuilt/tool_node.d.ts.map +1 -0
  236. package/dist/prebuilt/tool_node.js +209 -268
  237. package/dist/prebuilt/tool_node.js.map +1 -1
  238. package/dist/pregel/algo.cjs +521 -692
  239. package/dist/pregel/algo.cjs.map +1 -0
  240. package/dist/pregel/algo.d.cts +13 -0
  241. package/dist/pregel/algo.d.cts.map +1 -0
  242. package/dist/pregel/algo.d.ts +8 -38
  243. package/dist/pregel/algo.d.ts.map +1 -0
  244. package/dist/pregel/algo.js +516 -683
  245. package/dist/pregel/algo.js.map +1 -1
  246. package/dist/pregel/call.cjs +46 -42
  247. package/dist/pregel/call.cjs.map +1 -0
  248. package/dist/pregel/call.js +43 -37
  249. package/dist/pregel/call.js.map +1 -1
  250. package/dist/pregel/debug.cjs +153 -223
  251. package/dist/pregel/debug.cjs.map +1 -0
  252. package/dist/pregel/debug.js +152 -215
  253. package/dist/pregel/debug.js.map +1 -1
  254. package/dist/pregel/index.cjs +1134 -1604
  255. package/dist/pregel/index.cjs.map +1 -0
  256. package/dist/pregel/index.d.cts +534 -0
  257. package/dist/pregel/index.d.cts.map +1 -0
  258. package/dist/pregel/index.d.ts +458 -422
  259. package/dist/pregel/index.d.ts.map +1 -0
  260. package/dist/pregel/index.js +1125 -1592
  261. package/dist/pregel/index.js.map +1 -1
  262. package/dist/pregel/io.cjs +127 -234
  263. package/dist/pregel/io.cjs.map +1 -0
  264. package/dist/pregel/io.js +122 -225
  265. package/dist/pregel/io.js.map +1 -1
  266. package/dist/pregel/loop.cjs +539 -954
  267. package/dist/pregel/loop.cjs.map +1 -0
  268. package/dist/pregel/loop.js +536 -948
  269. package/dist/pregel/loop.js.map +1 -1
  270. package/dist/pregel/messages.cjs +104 -196
  271. package/dist/pregel/messages.cjs.map +1 -0
  272. package/dist/pregel/messages.js +102 -191
  273. package/dist/pregel/messages.js.map +1 -1
  274. package/dist/pregel/read.cjs +150 -280
  275. package/dist/pregel/read.cjs.map +1 -0
  276. package/dist/pregel/read.d.cts +51 -0
  277. package/dist/pregel/read.d.cts.map +1 -0
  278. package/dist/pregel/read.d.ts +48 -46
  279. package/dist/pregel/read.d.ts.map +1 -0
  280. package/dist/pregel/read.js +147 -274
  281. package/dist/pregel/read.js.map +1 -1
  282. package/dist/pregel/remote.cjs +339 -458
  283. package/dist/pregel/remote.cjs.map +1 -0
  284. package/dist/pregel/remote.d.cts +121 -0
  285. package/dist/pregel/remote.d.cts.map +1 -0
  286. package/dist/pregel/remote.d.ts +79 -69
  287. package/dist/pregel/remote.d.ts.map +1 -0
  288. package/dist/pregel/remote.js +337 -453
  289. package/dist/pregel/remote.js.map +1 -1
  290. package/dist/pregel/retry.cjs +87 -138
  291. package/dist/pregel/retry.cjs.map +1 -0
  292. package/dist/pregel/retry.js +83 -130
  293. package/dist/pregel/retry.js.map +1 -1
  294. package/dist/pregel/runnable_types.d.cts +25 -0
  295. package/dist/pregel/runnable_types.d.cts.map +1 -0
  296. package/dist/pregel/runnable_types.d.ts +22 -16
  297. package/dist/pregel/runnable_types.d.ts.map +1 -0
  298. package/dist/pregel/runner.cjs +222 -315
  299. package/dist/pregel/runner.cjs.map +1 -0
  300. package/dist/pregel/runner.js +219 -308
  301. package/dist/pregel/runner.js.map +1 -1
  302. package/dist/pregel/stream.cjs +89 -130
  303. package/dist/pregel/stream.cjs.map +1 -0
  304. package/dist/pregel/stream.js +87 -125
  305. package/dist/pregel/stream.js.map +1 -1
  306. package/dist/pregel/types.cjs +25 -62
  307. package/dist/pregel/types.cjs.map +1 -0
  308. package/dist/pregel/types.d.cts +437 -0
  309. package/dist/pregel/types.d.cts.map +1 -0
  310. package/dist/pregel/types.d.ts +377 -431
  311. package/dist/pregel/types.d.ts.map +1 -0
  312. package/dist/pregel/types.js +23 -57
  313. package/dist/pregel/types.js.map +1 -1
  314. package/dist/pregel/utils/config.cjs +100 -147
  315. package/dist/pregel/utils/config.cjs.map +1 -0
  316. package/dist/pregel/utils/config.d.cts +36 -0
  317. package/dist/pregel/utils/config.d.cts.map +1 -0
  318. package/dist/pregel/utils/config.d.ts +12 -9
  319. package/dist/pregel/utils/config.d.ts.map +1 -0
  320. package/dist/pregel/utils/config.js +98 -143
  321. package/dist/pregel/utils/config.js.map +1 -1
  322. package/dist/pregel/utils/index.cjs +87 -125
  323. package/dist/pregel/utils/index.cjs.map +1 -0
  324. package/dist/pregel/utils/index.d.cts +51 -0
  325. package/dist/pregel/utils/index.d.cts.map +1 -0
  326. package/dist/pregel/utils/index.d.ts +45 -61
  327. package/dist/pregel/utils/index.d.ts.map +1 -0
  328. package/dist/pregel/utils/index.js +86 -120
  329. package/dist/pregel/utils/index.js.map +1 -1
  330. package/dist/pregel/utils/subgraph.cjs +15 -26
  331. package/dist/pregel/utils/subgraph.cjs.map +1 -0
  332. package/dist/pregel/utils/subgraph.js +12 -21
  333. package/dist/pregel/utils/subgraph.js.map +1 -1
  334. package/dist/pregel/validate.cjs +42 -92
  335. package/dist/pregel/validate.cjs.map +1 -0
  336. package/dist/pregel/validate.js +39 -84
  337. package/dist/pregel/validate.js.map +1 -1
  338. package/dist/pregel/write.cjs +87 -137
  339. package/dist/pregel/write.cjs.map +1 -0
  340. package/dist/pregel/write.d.cts +35 -0
  341. package/dist/pregel/write.d.cts.map +1 -0
  342. package/dist/pregel/write.d.ts +27 -23
  343. package/dist/pregel/write.d.ts.map +1 -0
  344. package/dist/pregel/write.js +83 -131
  345. package/dist/pregel/write.js.map +1 -1
  346. package/dist/remote.cjs +3 -6
  347. package/dist/remote.d.cts +2 -0
  348. package/dist/remote.d.ts +2 -1
  349. package/dist/remote.js +3 -2
  350. package/dist/setup/async_local_storage.cjs +10 -7
  351. package/dist/setup/async_local_storage.cjs.map +1 -0
  352. package/dist/setup/async_local_storage.js +7 -2
  353. package/dist/setup/async_local_storage.js.map +1 -1
  354. package/dist/ui/index.cjs +4 -0
  355. package/dist/ui/index.d.cts +5 -0
  356. package/dist/ui/index.d.ts +5 -0
  357. package/dist/ui/index.js +3 -0
  358. package/dist/ui/stream.cjs +145 -0
  359. package/dist/ui/stream.cjs.map +1 -0
  360. package/dist/ui/stream.d.cts +25 -0
  361. package/dist/ui/stream.d.cts.map +1 -0
  362. package/dist/ui/stream.d.ts +25 -0
  363. package/dist/ui/stream.d.ts.map +1 -0
  364. package/dist/ui/stream.js +143 -0
  365. package/dist/ui/stream.js.map +1 -0
  366. package/dist/ui/types.infer.d.cts +53 -0
  367. package/dist/ui/types.infer.d.cts.map +1 -0
  368. package/dist/ui/types.infer.d.ts +53 -0
  369. package/dist/ui/types.infer.d.ts.map +1 -0
  370. package/dist/ui/types.message.d.cts +95 -0
  371. package/dist/ui/types.message.d.cts.map +1 -0
  372. package/dist/ui/types.message.d.ts +95 -0
  373. package/dist/ui/types.message.d.ts.map +1 -0
  374. package/dist/ui/types.schema.d.cts +228 -0
  375. package/dist/ui/types.schema.d.cts.map +1 -0
  376. package/dist/ui/types.schema.d.ts +228 -0
  377. package/dist/ui/types.schema.d.ts.map +1 -0
  378. package/dist/utils.cjs +77 -147
  379. package/dist/utils.cjs.map +1 -0
  380. package/dist/utils.d.cts +32 -0
  381. package/dist/utils.d.cts.map +1 -0
  382. package/dist/utils.d.ts +29 -25
  383. package/dist/utils.d.ts.map +1 -0
  384. package/dist/utils.js +73 -140
  385. package/dist/utils.js.map +1 -1
  386. package/dist/web.cjs +97 -55
  387. package/dist/web.d.cts +23 -0
  388. package/dist/web.d.ts +23 -14
  389. package/dist/web.js +15 -8
  390. package/package.json +83 -86
  391. package/dist/channels/any_value.cjs +0 -65
  392. package/dist/channels/any_value.js +0 -61
  393. package/dist/channels/any_value.js.map +0 -1
  394. package/dist/channels/dynamic_barrier_value.cjs +0 -212
  395. package/dist/channels/dynamic_barrier_value.js +0 -207
  396. package/dist/channels/dynamic_barrier_value.js.map +0 -1
  397. package/dist/channels/index.d.ts +0 -7
  398. package/dist/channels/index.js.map +0 -1
  399. package/dist/func/types.cjs +0 -15
  400. package/dist/func/types.js +0 -12
  401. package/dist/func/types.js.map +0 -1
  402. package/dist/graph/index.d.ts +0 -4
  403. package/dist/graph/index.js.map +0 -1
  404. package/dist/graph/message.test.cjs +0 -196
  405. package/dist/graph/message.test.d.ts +0 -1
  406. package/dist/graph/message.test.js +0 -194
  407. package/dist/graph/message.test.js.map +0 -1
  408. package/dist/graph/zod/index.js.map +0 -1
  409. package/dist/graph/zod/plugin.d.ts +0 -28
  410. package/dist/hash.d.ts +0 -2
  411. package/dist/prebuilt/index.js.map +0 -1
  412. package/dist/prebuilt/interrupt.cjs +0 -3
  413. package/dist/prebuilt/interrupt.js +0 -2
  414. package/dist/prebuilt/interrupt.js.map +0 -1
  415. package/dist/pregel/call.d.ts +0 -16
  416. package/dist/pregel/debug.d.ts +0 -41
  417. package/dist/pregel/debug.test.cjs +0 -258
  418. package/dist/pregel/debug.test.d.ts +0 -1
  419. package/dist/pregel/debug.test.js +0 -256
  420. package/dist/pregel/debug.test.js.map +0 -1
  421. package/dist/pregel/io.d.ts +0 -30
  422. package/dist/pregel/io.mapCommand.test.cjs +0 -150
  423. package/dist/pregel/io.mapCommand.test.d.ts +0 -1
  424. package/dist/pregel/io.mapCommand.test.js +0 -148
  425. package/dist/pregel/io.mapCommand.test.js.map +0 -1
  426. package/dist/pregel/loop.d.ts +0 -147
  427. package/dist/pregel/messages.d.ts +0 -30
  428. package/dist/pregel/messages.test.cjs +0 -369
  429. package/dist/pregel/messages.test.d.ts +0 -1
  430. package/dist/pregel/messages.test.js +0 -367
  431. package/dist/pregel/messages.test.js.map +0 -1
  432. package/dist/pregel/read.test.cjs +0 -194
  433. package/dist/pregel/read.test.d.ts +0 -1
  434. package/dist/pregel/read.test.js +0 -192
  435. package/dist/pregel/read.test.js.map +0 -1
  436. package/dist/pregel/retry.d.ts +0 -17
  437. package/dist/pregel/runnable_types.cjs +0 -3
  438. package/dist/pregel/runnable_types.js +0 -2
  439. package/dist/pregel/runnable_types.js.map +0 -1
  440. package/dist/pregel/runner.d.ts +0 -79
  441. package/dist/pregel/runner.test.cjs +0 -66
  442. package/dist/pregel/runner.test.d.ts +0 -1
  443. package/dist/pregel/runner.test.js +0 -64
  444. package/dist/pregel/runner.test.js.map +0 -1
  445. package/dist/pregel/stream.d.ts +0 -40
  446. package/dist/pregel/utils/config.test.cjs +0 -214
  447. package/dist/pregel/utils/config.test.d.ts +0 -1
  448. package/dist/pregel/utils/config.test.js +0 -212
  449. package/dist/pregel/utils/config.test.js.map +0 -1
  450. package/dist/pregel/utils/subgraph.d.ts +0 -4
  451. package/dist/pregel/utils/subgraph.test.cjs +0 -83
  452. package/dist/pregel/utils/subgraph.test.d.ts +0 -1
  453. package/dist/pregel/utils/subgraph.test.js +0 -81
  454. package/dist/pregel/utils/subgraph.test.js.map +0 -1
  455. package/dist/pregel/validate.d.ts +0 -16
  456. package/dist/pregel/validate.test.cjs +0 -220
  457. package/dist/pregel/validate.test.d.ts +0 -1
  458. package/dist/pregel/validate.test.js +0 -218
  459. package/dist/pregel/validate.test.js.map +0 -1
  460. package/dist/pregel/write.test.cjs +0 -181
  461. package/dist/pregel/write.test.d.ts +0 -1
  462. package/dist/pregel/write.test.js +0 -179
  463. package/dist/pregel/write.test.js.map +0 -1
  464. package/dist/remote.js.map +0 -1
  465. package/dist/setup/async_local_storage.d.ts +0 -1
  466. package/dist/web.js.map +0 -1
  467. package/index.cjs +0 -1
  468. package/index.d.cts +0 -1
  469. package/index.d.ts +0 -1
  470. package/index.js +0 -1
  471. package/prebuilt.cjs +0 -1
  472. package/prebuilt.d.cts +0 -1
  473. package/prebuilt.d.ts +0 -1
  474. package/prebuilt.js +0 -1
  475. package/pregel.cjs +0 -1
  476. package/pregel.d.cts +0 -1
  477. package/pregel.d.ts +0 -1
  478. package/pregel.js +0 -1
  479. package/remote.cjs +0 -1
  480. package/remote.d.cts +0 -1
  481. package/remote.d.ts +0 -1
  482. package/remote.js +0 -1
  483. package/web.cjs +0 -1
  484. package/web.d.cts +0 -1
  485. package/web.d.ts +0 -1
  486. package/web.js +0 -1
  487. package/zod/schema.cjs +0 -1
  488. package/zod/schema.d.cts +0 -1
  489. package/zod/schema.d.ts +0 -1
  490. package/zod/schema.js +0 -1
  491. package/zod.cjs +0 -1
  492. package/zod.d.cts +0 -1
  493. package/zod.d.ts +0 -1
  494. package/zod.js +0 -1
@@ -1,956 +1,544 @@
1
- import { copyCheckpoint, emptyCheckpoint, AsyncBatchedStore, WRITES_IDX_MAP, BaseCache, } from "@langchain/langgraph-checkpoint";
2
- import { createCheckpoint, emptyChannels, } from "../channels/base.js";
3
- import { isCommand, CHECKPOINT_NAMESPACE_SEPARATOR, CONFIG_KEY_CHECKPOINT_MAP, CONFIG_KEY_READ, CONFIG_KEY_RESUMING, CONFIG_KEY_STREAM, ERROR, INPUT, INTERRUPT, NULL_TASK_ID, RESUME, TAG_HIDDEN, PUSH, CONFIG_KEY_SCRATCHPAD, CONFIG_KEY_CHECKPOINT_NS, CHECKPOINT_NAMESPACE_END, CONFIG_KEY_CHECKPOINT_ID, CONFIG_KEY_RESUME_MAP, START, } from "../constants.js";
4
- import { _applyWrites, _prepareNextTasks, _prepareSingleTask, increment, shouldInterrupt, } from "./algo.js";
5
- import { gatherIterator, gatherIteratorSync, prefixGenerator, } from "../utils.js";
6
- import { mapCommand, mapInput, mapOutputUpdates, mapOutputValues, readChannels, } from "./io.js";
7
- import { EmptyInputError, GraphInterrupt, isGraphInterrupt, } from "../errors.js";
1
+ import { EmptyInputError, GraphInterrupt, isGraphInterrupt } from "../errors.js";
2
+ import { createCheckpoint, emptyChannels } from "../channels/base.js";
3
+ import { CHECKPOINT_NAMESPACE_END, CHECKPOINT_NAMESPACE_SEPARATOR, CONFIG_KEY_CHECKPOINT_ID, CONFIG_KEY_CHECKPOINT_MAP, CONFIG_KEY_CHECKPOINT_NS, CONFIG_KEY_READ, CONFIG_KEY_RESUME_MAP, CONFIG_KEY_RESUMING, CONFIG_KEY_SCRATCHPAD, CONFIG_KEY_STREAM, ERROR, INPUT, INTERRUPT, NULL_TASK_ID, PUSH, RESUME, START, TAG_HIDDEN, isCommand } from "../constants.js";
4
+ import { gatherIterator, gatherIteratorSync, prefixGenerator } from "../utils.js";
5
+ import { isXXH3 } from "../hash.js";
6
+ import { mapCommand, mapInput, mapOutputUpdates, mapOutputValues, readChannels } from "./io.js";
8
7
  import { getNewChannelVersions, patchConfigurable } from "./utils/index.js";
9
- import { mapDebugTasks, mapDebugCheckpoint, mapDebugTaskResults, printStepTasks, } from "./debug.js";
8
+ import { _applyWrites, _prepareNextTasks, _prepareSingleTask, increment, shouldInterrupt } from "./algo.js";
9
+ import { mapDebugCheckpoint, mapDebugTaskResults, mapDebugTasks, printStepTasks } from "./debug.js";
10
10
  import { IterableReadableWritableStream } from "./stream.js";
11
- import { isXXH3 } from "../hash.js";
11
+ import { AsyncBatchedStore, BaseCache, WRITES_IDX_MAP, copyCheckpoint, emptyCheckpoint } from "@langchain/langgraph-checkpoint";
12
+
13
+ //#region src/pregel/loop.ts
12
14
  const INPUT_DONE = Symbol.for("INPUT_DONE");
13
15
  const INPUT_RESUMING = Symbol.for("INPUT_RESUMING");
14
16
  const DEFAULT_LOOP_LIMIT = 25;
15
17
  function createDuplexStream(...streams) {
16
- return new IterableReadableWritableStream({
17
- passthroughFn: (value) => {
18
- for (const stream of streams) {
19
- if (stream.modes.has(value[1])) {
20
- stream.push(value);
21
- }
22
- }
23
- },
24
- modes: new Set(streams.flatMap((s) => Array.from(s.modes))),
25
- });
26
- }
27
- class AsyncBatchedCache extends BaseCache {
28
- constructor(cache) {
29
- super();
30
- Object.defineProperty(this, "cache", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: void 0
35
- });
36
- Object.defineProperty(this, "queue", {
37
- enumerable: true,
38
- configurable: true,
39
- writable: true,
40
- value: Promise.resolve()
41
- });
42
- this.cache = cache;
43
- }
44
- async get(keys) {
45
- return this.enqueueOperation("get", keys);
46
- }
47
- async set(pairs) {
48
- return this.enqueueOperation("set", pairs);
49
- }
50
- async clear(namespaces) {
51
- return this.enqueueOperation("clear", namespaces);
52
- }
53
- async stop() {
54
- await this.queue;
55
- }
56
- enqueueOperation(type, ...args) {
57
- const newPromise = this.queue.then(() => {
58
- // @ts-expect-error Tuple type warning
59
- return this.cache[type](...args);
60
- });
61
- this.queue = newPromise.then(() => void 0, () => void 0);
62
- return newPromise;
63
- }
64
- }
65
- export class PregelLoop {
66
- get isResuming() {
67
- let hasChannelVersions = false;
68
- if (START in this.checkpoint.channel_versions) {
69
- // For common channels, we can short-circuit the check
70
- hasChannelVersions = true;
71
- }
72
- else {
73
- for (const chan in this.checkpoint.channel_versions) {
74
- if (Object.prototype.hasOwnProperty.call(this.checkpoint.channel_versions, chan)) {
75
- hasChannelVersions = true;
76
- break;
77
- }
78
- }
79
- }
80
- const configHasResumingFlag = this.config.configurable?.[CONFIG_KEY_RESUMING] !== undefined;
81
- const configIsResuming = configHasResumingFlag && this.config.configurable?.[CONFIG_KEY_RESUMING];
82
- const inputIsNullOrUndefined = this.input === null || this.input === undefined;
83
- const inputIsCommandResuming = isCommand(this.input) && this.input.resume != null;
84
- const inputIsResuming = this.input === INPUT_RESUMING;
85
- const runIdMatchesPrevious = !this.isNested &&
86
- this.config.metadata?.run_id !== undefined &&
87
- this.checkpointMetadata?.run_id !== undefined &&
88
- this.config.metadata.run_id ===
89
- this.checkpointMetadata?.run_id;
90
- return (hasChannelVersions &&
91
- (configIsResuming ||
92
- inputIsNullOrUndefined ||
93
- inputIsCommandResuming ||
94
- inputIsResuming ||
95
- runIdMatchesPrevious));
96
- }
97
- constructor(params) {
98
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
- Object.defineProperty(this, "input", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: void 0
104
- });
105
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
106
- Object.defineProperty(this, "output", {
107
- enumerable: true,
108
- configurable: true,
109
- writable: true,
110
- value: void 0
111
- });
112
- Object.defineProperty(this, "config", {
113
- enumerable: true,
114
- configurable: true,
115
- writable: true,
116
- value: void 0
117
- });
118
- Object.defineProperty(this, "checkpointer", {
119
- enumerable: true,
120
- configurable: true,
121
- writable: true,
122
- value: void 0
123
- });
124
- Object.defineProperty(this, "checkpointerGetNextVersion", {
125
- enumerable: true,
126
- configurable: true,
127
- writable: true,
128
- value: void 0
129
- });
130
- Object.defineProperty(this, "channels", {
131
- enumerable: true,
132
- configurable: true,
133
- writable: true,
134
- value: void 0
135
- });
136
- Object.defineProperty(this, "checkpoint", {
137
- enumerable: true,
138
- configurable: true,
139
- writable: true,
140
- value: void 0
141
- });
142
- Object.defineProperty(this, "checkpointIdSaved", {
143
- enumerable: true,
144
- configurable: true,
145
- writable: true,
146
- value: void 0
147
- });
148
- Object.defineProperty(this, "checkpointConfig", {
149
- enumerable: true,
150
- configurable: true,
151
- writable: true,
152
- value: void 0
153
- });
154
- Object.defineProperty(this, "checkpointMetadata", {
155
- enumerable: true,
156
- configurable: true,
157
- writable: true,
158
- value: void 0
159
- });
160
- Object.defineProperty(this, "checkpointNamespace", {
161
- enumerable: true,
162
- configurable: true,
163
- writable: true,
164
- value: void 0
165
- });
166
- Object.defineProperty(this, "checkpointPendingWrites", {
167
- enumerable: true,
168
- configurable: true,
169
- writable: true,
170
- value: []
171
- });
172
- Object.defineProperty(this, "checkpointPreviousVersions", {
173
- enumerable: true,
174
- configurable: true,
175
- writable: true,
176
- value: void 0
177
- });
178
- Object.defineProperty(this, "step", {
179
- enumerable: true,
180
- configurable: true,
181
- writable: true,
182
- value: void 0
183
- });
184
- Object.defineProperty(this, "stop", {
185
- enumerable: true,
186
- configurable: true,
187
- writable: true,
188
- value: void 0
189
- });
190
- Object.defineProperty(this, "durability", {
191
- enumerable: true,
192
- configurable: true,
193
- writable: true,
194
- value: void 0
195
- });
196
- Object.defineProperty(this, "outputKeys", {
197
- enumerable: true,
198
- configurable: true,
199
- writable: true,
200
- value: void 0
201
- });
202
- Object.defineProperty(this, "streamKeys", {
203
- enumerable: true,
204
- configurable: true,
205
- writable: true,
206
- value: void 0
207
- });
208
- Object.defineProperty(this, "nodes", {
209
- enumerable: true,
210
- configurable: true,
211
- writable: true,
212
- value: void 0
213
- });
214
- Object.defineProperty(this, "skipDoneTasks", {
215
- enumerable: true,
216
- configurable: true,
217
- writable: true,
218
- value: void 0
219
- });
220
- Object.defineProperty(this, "prevCheckpointConfig", {
221
- enumerable: true,
222
- configurable: true,
223
- writable: true,
224
- value: void 0
225
- });
226
- Object.defineProperty(this, "status", {
227
- enumerable: true,
228
- configurable: true,
229
- writable: true,
230
- value: "pending"
231
- });
232
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
233
- Object.defineProperty(this, "tasks", {
234
- enumerable: true,
235
- configurable: true,
236
- writable: true,
237
- value: {}
238
- });
239
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
240
- Object.defineProperty(this, "stream", {
241
- enumerable: true,
242
- configurable: true,
243
- writable: true,
244
- value: void 0
245
- });
246
- Object.defineProperty(this, "checkpointerPromises", {
247
- enumerable: true,
248
- configurable: true,
249
- writable: true,
250
- value: []
251
- });
252
- Object.defineProperty(this, "isNested", {
253
- enumerable: true,
254
- configurable: true,
255
- writable: true,
256
- value: void 0
257
- });
258
- Object.defineProperty(this, "_checkpointerChainedPromise", {
259
- enumerable: true,
260
- configurable: true,
261
- writable: true,
262
- value: Promise.resolve()
263
- });
264
- Object.defineProperty(this, "store", {
265
- enumerable: true,
266
- configurable: true,
267
- writable: true,
268
- value: void 0
269
- });
270
- Object.defineProperty(this, "cache", {
271
- enumerable: true,
272
- configurable: true,
273
- writable: true,
274
- value: void 0
275
- });
276
- Object.defineProperty(this, "manager", {
277
- enumerable: true,
278
- configurable: true,
279
- writable: true,
280
- value: void 0
281
- });
282
- Object.defineProperty(this, "interruptAfter", {
283
- enumerable: true,
284
- configurable: true,
285
- writable: true,
286
- value: void 0
287
- });
288
- Object.defineProperty(this, "interruptBefore", {
289
- enumerable: true,
290
- configurable: true,
291
- writable: true,
292
- value: void 0
293
- });
294
- Object.defineProperty(this, "toInterrupt", {
295
- enumerable: true,
296
- configurable: true,
297
- writable: true,
298
- value: []
299
- });
300
- Object.defineProperty(this, "debug", {
301
- enumerable: true,
302
- configurable: true,
303
- writable: true,
304
- value: false
305
- });
306
- Object.defineProperty(this, "triggerToNodes", {
307
- enumerable: true,
308
- configurable: true,
309
- writable: true,
310
- value: void 0
311
- });
312
- this.input = params.input;
313
- this.checkpointer = params.checkpointer;
314
- // TODO: if managed values no longer needs graph we can replace with
315
- // managed_specs, channel_specs
316
- if (this.checkpointer !== undefined) {
317
- this.checkpointerGetNextVersion = this.checkpointer.getNextVersion.bind(this.checkpointer);
318
- }
319
- else {
320
- this.checkpointerGetNextVersion = increment;
321
- }
322
- this.checkpoint = params.checkpoint;
323
- this.checkpointMetadata = params.checkpointMetadata;
324
- this.checkpointPreviousVersions = params.checkpointPreviousVersions;
325
- this.channels = params.channels;
326
- this.checkpointPendingWrites = params.checkpointPendingWrites;
327
- this.step = params.step;
328
- this.stop = params.stop;
329
- this.config = params.config;
330
- this.checkpointConfig = params.checkpointConfig;
331
- this.isNested = params.isNested;
332
- this.manager = params.manager;
333
- this.outputKeys = params.outputKeys;
334
- this.streamKeys = params.streamKeys;
335
- this.nodes = params.nodes;
336
- this.skipDoneTasks = params.skipDoneTasks;
337
- this.store = params.store;
338
- this.cache = params.cache ? new AsyncBatchedCache(params.cache) : undefined;
339
- this.stream = params.stream;
340
- this.checkpointNamespace = params.checkpointNamespace;
341
- this.prevCheckpointConfig = params.prevCheckpointConfig;
342
- this.interruptAfter = params.interruptAfter;
343
- this.interruptBefore = params.interruptBefore;
344
- this.durability = params.durability;
345
- this.debug = params.debug;
346
- this.triggerToNodes = params.triggerToNodes;
347
- }
348
- static async initialize(params) {
349
- let { config, stream } = params;
350
- if (stream !== undefined &&
351
- config.configurable?.[CONFIG_KEY_STREAM] !== undefined) {
352
- stream = createDuplexStream(stream, config.configurable[CONFIG_KEY_STREAM]);
353
- }
354
- const skipDoneTasks = config.configurable
355
- ? !("checkpoint_id" in config.configurable)
356
- : true;
357
- const scratchpad = config.configurable?.[CONFIG_KEY_SCRATCHPAD];
358
- if (config.configurable && scratchpad) {
359
- if (scratchpad.subgraphCounter > 0) {
360
- config = patchConfigurable(config, {
361
- [CONFIG_KEY_CHECKPOINT_NS]: [
362
- config.configurable[CONFIG_KEY_CHECKPOINT_NS],
363
- scratchpad.subgraphCounter.toString(),
364
- ].join(CHECKPOINT_NAMESPACE_SEPARATOR),
365
- });
366
- }
367
- scratchpad.subgraphCounter += 1;
368
- }
369
- const isNested = CONFIG_KEY_READ in (config.configurable ?? {});
370
- if (!isNested &&
371
- config.configurable?.checkpoint_ns !== undefined &&
372
- config.configurable?.checkpoint_ns !== "") {
373
- config = patchConfigurable(config, {
374
- checkpoint_ns: "",
375
- checkpoint_id: undefined,
376
- });
377
- }
378
- let checkpointConfig = config;
379
- if (config.configurable?.[CONFIG_KEY_CHECKPOINT_MAP] !== undefined &&
380
- config.configurable?.[CONFIG_KEY_CHECKPOINT_MAP]?.[config.configurable?.checkpoint_ns]) {
381
- checkpointConfig = patchConfigurable(config, {
382
- checkpoint_id: config.configurable[CONFIG_KEY_CHECKPOINT_MAP][config.configurable?.checkpoint_ns],
383
- });
384
- }
385
- const checkpointNamespace = config.configurable?.checkpoint_ns?.split(CHECKPOINT_NAMESPACE_SEPARATOR) ?? [];
386
- const saved = (await params.checkpointer?.getTuple(checkpointConfig)) ?? {
387
- config,
388
- checkpoint: emptyCheckpoint(),
389
- metadata: { source: "input", step: -2, parents: {} },
390
- pendingWrites: [],
391
- };
392
- checkpointConfig = {
393
- ...config,
394
- ...saved.config,
395
- configurable: {
396
- checkpoint_ns: "",
397
- ...config.configurable,
398
- ...saved.config.configurable,
399
- },
400
- };
401
- const prevCheckpointConfig = saved.parentConfig;
402
- const checkpoint = copyCheckpoint(saved.checkpoint);
403
- const checkpointMetadata = { ...saved.metadata };
404
- const checkpointPendingWrites = saved.pendingWrites ?? [];
405
- const channels = emptyChannels(params.channelSpecs, checkpoint);
406
- const step = (checkpointMetadata.step ?? 0) + 1;
407
- const stop = step + (config.recursionLimit ?? DEFAULT_LOOP_LIMIT) + 1;
408
- const checkpointPreviousVersions = { ...checkpoint.channel_versions };
409
- const store = params.store
410
- ? new AsyncBatchedStore(params.store)
411
- : undefined;
412
- if (store) {
413
- // Start the store. This is a batch store, so it will run continuously
414
- await store.start();
415
- }
416
- return new PregelLoop({
417
- input: params.input,
418
- config,
419
- checkpointer: params.checkpointer,
420
- checkpoint,
421
- checkpointMetadata,
422
- checkpointConfig,
423
- prevCheckpointConfig,
424
- checkpointNamespace,
425
- channels,
426
- isNested,
427
- manager: params.manager,
428
- skipDoneTasks,
429
- step,
430
- stop,
431
- checkpointPreviousVersions,
432
- checkpointPendingWrites,
433
- outputKeys: params.outputKeys ?? [],
434
- streamKeys: params.streamKeys ?? [],
435
- nodes: params.nodes,
436
- stream,
437
- store,
438
- cache: params.cache,
439
- interruptAfter: params.interruptAfter,
440
- interruptBefore: params.interruptBefore,
441
- durability: params.durability,
442
- debug: params.debug,
443
- triggerToNodes: params.triggerToNodes,
444
- });
445
- }
446
- _checkpointerPutAfterPrevious(input) {
447
- this._checkpointerChainedPromise = this._checkpointerChainedPromise.then(() => {
448
- return this.checkpointer?.put(input.config, input.checkpoint, input.metadata, input.newVersions);
449
- });
450
- this.checkpointerPromises.push(this._checkpointerChainedPromise);
451
- }
452
- /**
453
- * Put writes for a task, to be read by the next tick.
454
- * @param taskId
455
- * @param writes
456
- */
457
- putWrites(taskId, writes) {
458
- let writesCopy = writes;
459
- if (writesCopy.length === 0)
460
- return;
461
- // deduplicate writes to special channels, last write wins
462
- if (writesCopy.every(([key]) => key in WRITES_IDX_MAP)) {
463
- writesCopy = Array.from(new Map(writesCopy.map((w) => [w[0], w])).values());
464
- }
465
- // remove existing writes for this task
466
- this.checkpointPendingWrites = this.checkpointPendingWrites.filter((w) => w[0] !== taskId);
467
- // save writes
468
- for (const [c, v] of writesCopy) {
469
- this.checkpointPendingWrites.push([taskId, c, v]);
470
- }
471
- const config = patchConfigurable(this.checkpointConfig, {
472
- [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "",
473
- [CONFIG_KEY_CHECKPOINT_ID]: this.checkpoint.id,
474
- });
475
- if (this.durability !== "exit" && this.checkpointer != null) {
476
- this.checkpointerPromises.push(this.checkpointer.putWrites(config, writesCopy, taskId));
477
- }
478
- if (this.tasks) {
479
- this._outputWrites(taskId, writesCopy);
480
- }
481
- if (!writes.length || !this.cache || !this.tasks) {
482
- return;
483
- }
484
- // only cache tasks with a cache key
485
- const task = this.tasks[taskId];
486
- if (task == null || task.cache_key == null) {
487
- return;
488
- }
489
- // only cache successful tasks
490
- if (writes[0][0] === ERROR || writes[0][0] === INTERRUPT) {
491
- return;
492
- }
493
- void this.cache.set([
494
- {
495
- key: [task.cache_key.ns, task.cache_key.key],
496
- value: task.writes,
497
- ttl: task.cache_key.ttl,
498
- },
499
- ]);
500
- }
501
- _outputWrites(taskId, writes, cached = false) {
502
- const task = this.tasks[taskId];
503
- if (task !== undefined) {
504
- if (task.config !== undefined &&
505
- (task.config.tags ?? []).includes(TAG_HIDDEN)) {
506
- return;
507
- }
508
- if (writes.length > 0) {
509
- if (writes[0][0] === INTERRUPT) {
510
- // in `algo.ts` we append a bool to the task path to indicate
511
- // whether or not a call was present. If so, we don't emit the
512
- // the interrupt as it'll be emitted by the parent.
513
- if (task.path?.[0] === PUSH && task.path?.at(-1) === true)
514
- return;
515
- const interruptWrites = writes
516
- .filter((w) => w[0] === INTERRUPT)
517
- .flatMap((w) => w[1]);
518
- this._emit([
519
- ["updates", { [INTERRUPT]: interruptWrites }],
520
- ["values", { [INTERRUPT]: interruptWrites }],
521
- ]);
522
- }
523
- else if (writes[0][0] !== ERROR) {
524
- this._emit(gatherIteratorSync(prefixGenerator(mapOutputUpdates(this.outputKeys, [[task, writes]], cached), "updates")));
525
- }
526
- }
527
- if (!cached) {
528
- this._emit(gatherIteratorSync(prefixGenerator(mapDebugTaskResults([[task, writes]], this.streamKeys), "tasks")));
529
- }
530
- }
531
- }
532
- async _matchCachedWrites() {
533
- if (!this.cache)
534
- return [];
535
- const matched = [];
536
- const serializeKey = ([ns, key]) => {
537
- return `ns:${ns.join(",")}|key:${key}`;
538
- };
539
- const keys = [];
540
- const keyMap = {};
541
- for (const task of Object.values(this.tasks)) {
542
- if (task.cache_key != null && !task.writes.length) {
543
- keys.push([task.cache_key.ns, task.cache_key.key]);
544
- keyMap[serializeKey([task.cache_key.ns, task.cache_key.key])] = task;
545
- }
546
- }
547
- if (keys.length === 0)
548
- return [];
549
- const cache = await this.cache.get(keys);
550
- for (const { key, value } of cache) {
551
- const task = keyMap[serializeKey(key)];
552
- if (task != null) {
553
- // update the task with the cached writes
554
- task.writes.push(...value);
555
- matched.push({ task, result: value });
556
- }
557
- }
558
- return matched;
559
- }
560
- /**
561
- * Execute a single iteration of the Pregel loop.
562
- * Returns true if more iterations are needed.
563
- * @param params
564
- */
565
- async tick(params) {
566
- if (this.store && !this.store.isRunning) {
567
- await this.store?.start();
568
- }
569
- const { inputKeys = [] } = params;
570
- if (this.status !== "pending") {
571
- throw new Error(`Cannot tick when status is no longer "pending". Current status: "${this.status}"`);
572
- }
573
- if (![INPUT_DONE, INPUT_RESUMING].includes(this.input)) {
574
- await this._first(inputKeys);
575
- }
576
- else if (this.toInterrupt.length > 0) {
577
- this.status = "interrupt_before";
578
- throw new GraphInterrupt();
579
- }
580
- else if (Object.values(this.tasks).every((task) => task.writes.length > 0)) {
581
- // finish superstep
582
- const writes = Object.values(this.tasks).flatMap((t) => t.writes);
583
- // All tasks have finished
584
- _applyWrites(this.checkpoint, this.channels, Object.values(this.tasks), this.checkpointerGetNextVersion, this.triggerToNodes);
585
- // produce values output
586
- const valuesOutput = await gatherIterator(prefixGenerator(mapOutputValues(this.outputKeys, writes, this.channels), "values"));
587
- this._emit(valuesOutput);
588
- // clear pending writes
589
- this.checkpointPendingWrites = [];
590
- await this._putCheckpoint({ source: "loop" });
591
- // after execution, check if we should interrupt
592
- if (shouldInterrupt(this.checkpoint, this.interruptAfter, Object.values(this.tasks))) {
593
- this.status = "interrupt_after";
594
- throw new GraphInterrupt();
595
- }
596
- // unset resuming flag
597
- if (this.config.configurable?.[CONFIG_KEY_RESUMING] !== undefined) {
598
- delete this.config.configurable?.[CONFIG_KEY_RESUMING];
599
- }
600
- }
601
- else {
602
- return false;
603
- }
604
- if (this.step > this.stop) {
605
- this.status = "out_of_steps";
606
- return false;
607
- }
608
- const nextTasks = _prepareNextTasks(this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, this.config, true, {
609
- step: this.step,
610
- checkpointer: this.checkpointer,
611
- isResuming: this.isResuming,
612
- manager: this.manager,
613
- store: this.store,
614
- stream: this.stream,
615
- });
616
- this.tasks = nextTasks;
617
- // Produce debug output
618
- if (this.checkpointer) {
619
- this._emit(await gatherIterator(prefixGenerator(mapDebugCheckpoint(this.checkpointConfig, this.channels, this.streamKeys, this.checkpointMetadata, Object.values(this.tasks), this.checkpointPendingWrites, this.prevCheckpointConfig, this.outputKeys), "checkpoints")));
620
- }
621
- if (Object.values(this.tasks).length === 0) {
622
- this.status = "done";
623
- return false;
624
- }
625
- // if there are pending writes from a previous loop, apply them
626
- if (this.skipDoneTasks && this.checkpointPendingWrites.length > 0) {
627
- for (const [tid, k, v] of this.checkpointPendingWrites) {
628
- if (k === ERROR || k === INTERRUPT || k === RESUME) {
629
- continue;
630
- }
631
- const task = Object.values(this.tasks).find((t) => t.id === tid);
632
- if (task) {
633
- task.writes.push([k, v]);
634
- }
635
- }
636
- for (const task of Object.values(this.tasks)) {
637
- if (task.writes.length > 0) {
638
- this._outputWrites(task.id, task.writes, true);
639
- }
640
- }
641
- }
642
- // if all tasks have finished, re-tick
643
- if (Object.values(this.tasks).every((task) => task.writes.length > 0)) {
644
- return this.tick({ inputKeys });
645
- }
646
- // Before execution, check if we should interrupt
647
- if (shouldInterrupt(this.checkpoint, this.interruptBefore, Object.values(this.tasks))) {
648
- this.status = "interrupt_before";
649
- throw new GraphInterrupt();
650
- }
651
- // Produce debug output
652
- const debugOutput = await gatherIterator(prefixGenerator(mapDebugTasks(Object.values(this.tasks)), "tasks"));
653
- this._emit(debugOutput);
654
- return true;
655
- }
656
- async finishAndHandleError(error) {
657
- // persist current checkpoint and writes
658
- if (this.durability === "exit" &&
659
- // if it's a top graph
660
- (!this.isNested ||
661
- // or a nested graph with error or interrupt
662
- typeof error !== "undefined" ||
663
- // or a nested graph with checkpointer: true
664
- this.checkpointNamespace.every((part) => !part.includes(CHECKPOINT_NAMESPACE_END)))) {
665
- this._putCheckpoint(this.checkpointMetadata);
666
- this._flushPendingWrites();
667
- }
668
- const suppress = this._suppressInterrupt(error);
669
- if (suppress || error === undefined) {
670
- this.output = readChannels(this.channels, this.outputKeys);
671
- }
672
- if (suppress) {
673
- // emit one last "values" event, with pending writes applied
674
- if (this.tasks !== undefined &&
675
- this.checkpointPendingWrites.length > 0 &&
676
- Object.values(this.tasks).some((task) => task.writes.length > 0)) {
677
- _applyWrites(this.checkpoint, this.channels, Object.values(this.tasks), this.checkpointerGetNextVersion, this.triggerToNodes);
678
- this._emit(gatherIteratorSync(prefixGenerator(mapOutputValues(this.outputKeys, Object.values(this.tasks).flatMap((t) => t.writes), this.channels), "values")));
679
- }
680
- // Emit INTERRUPT event
681
- if (isGraphInterrupt(error) && !error.interrupts.length) {
682
- this._emit([
683
- ["updates", { [INTERRUPT]: [] }],
684
- ["values", { [INTERRUPT]: [] }],
685
- ]);
686
- }
687
- }
688
- return suppress;
689
- }
690
- async acceptPush(task, writeIdx, call) {
691
- if (this.interruptAfter?.length > 0 &&
692
- shouldInterrupt(this.checkpoint, this.interruptAfter, [task])) {
693
- this.toInterrupt.push(task);
694
- return;
695
- }
696
- const pushed = _prepareSingleTask([PUSH, task.path ?? [], writeIdx, task.id, call], this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, task.config ?? {}, true, {
697
- step: this.step,
698
- checkpointer: this.checkpointer,
699
- manager: this.manager,
700
- store: this.store,
701
- stream: this.stream,
702
- });
703
- if (!pushed)
704
- return;
705
- if (this.interruptBefore?.length > 0 &&
706
- shouldInterrupt(this.checkpoint, this.interruptBefore, [pushed])) {
707
- this.toInterrupt.push(pushed);
708
- return;
709
- }
710
- this._emit(gatherIteratorSync(prefixGenerator(mapDebugTasks([pushed]), "tasks")));
711
- if (this.debug)
712
- printStepTasks(this.step, [pushed]);
713
- this.tasks[pushed.id] = pushed;
714
- if (this.skipDoneTasks)
715
- this._matchWrites({ [pushed.id]: pushed });
716
- const tasks = await this._matchCachedWrites();
717
- for (const { task } of tasks) {
718
- this._outputWrites(task.id, task.writes, true);
719
- }
720
- return pushed;
721
- }
722
- _suppressInterrupt(e) {
723
- return isGraphInterrupt(e) && !this.isNested;
724
- }
725
- async _first(inputKeys) {
726
- /*
727
- * Resuming from previous checkpoint requires
728
- * - finding a previous checkpoint
729
- * - receiving null input (outer graph) or RESUMING flag (subgraph)
730
- */
731
- const { configurable } = this.config;
732
- // take resume value from parent
733
- const scratchpad = configurable?.[CONFIG_KEY_SCRATCHPAD];
734
- if (scratchpad && scratchpad.nullResume !== undefined) {
735
- this.putWrites(NULL_TASK_ID, [[RESUME, scratchpad.nullResume]]);
736
- }
737
- // map command to writes
738
- if (isCommand(this.input)) {
739
- const hasResume = this.input.resume != null;
740
- if (this.input.resume != null &&
741
- typeof this.input.resume === "object" &&
742
- Object.keys(this.input.resume).every(isXXH3)) {
743
- this.config.configurable ??= {};
744
- this.config.configurable[CONFIG_KEY_RESUME_MAP] = this.input.resume;
745
- }
746
- if (hasResume && this.checkpointer == null) {
747
- throw new Error("Cannot use Command(resume=...) without checkpointer");
748
- }
749
- const writes = {};
750
- // group writes by task id
751
- for (const [tid, key, value] of mapCommand(this.input, this.checkpointPendingWrites)) {
752
- writes[tid] ??= [];
753
- writes[tid].push([key, value]);
754
- }
755
- if (Object.keys(writes).length === 0) {
756
- throw new EmptyInputError("Received empty Command input");
757
- }
758
- // save writes
759
- for (const [tid, ws] of Object.entries(writes)) {
760
- this.putWrites(tid, ws);
761
- }
762
- }
763
- // apply null writes
764
- const nullWrites = (this.checkpointPendingWrites ?? [])
765
- .filter((w) => w[0] === NULL_TASK_ID)
766
- .map((w) => w.slice(1));
767
- if (nullWrites.length > 0) {
768
- _applyWrites(this.checkpoint, this.channels, [
769
- {
770
- name: INPUT,
771
- writes: nullWrites,
772
- triggers: [],
773
- },
774
- ], this.checkpointerGetNextVersion, this.triggerToNodes);
775
- }
776
- const isCommandUpdateOrGoto = isCommand(this.input) && nullWrites.length > 0;
777
- if (this.isResuming || isCommandUpdateOrGoto) {
778
- for (const channelName in this.channels) {
779
- if (!Object.prototype.hasOwnProperty.call(this.channels, channelName))
780
- continue;
781
- if (this.checkpoint.channel_versions[channelName] !== undefined) {
782
- const version = this.checkpoint.channel_versions[channelName];
783
- this.checkpoint.versions_seen[INTERRUPT] = {
784
- ...this.checkpoint.versions_seen[INTERRUPT],
785
- [channelName]: version,
786
- };
787
- }
788
- }
789
- // produce values output
790
- const valuesOutput = await gatherIterator(prefixGenerator(mapOutputValues(this.outputKeys, true, this.channels), "values"));
791
- this._emit(valuesOutput);
792
- }
793
- if (this.isResuming) {
794
- this.input = INPUT_RESUMING;
795
- }
796
- else if (isCommandUpdateOrGoto) {
797
- // we need to create a new checkpoint for Command(update=...) or Command(goto=...)
798
- // in case the result of Command(goto=...) is an interrupt.
799
- // If not done, the checkpoint containing the interrupt will be lost.
800
- await this._putCheckpoint({ source: "input" });
801
- this.input = INPUT_DONE;
802
- }
803
- else {
804
- // map inputs to channel updates
805
- const inputWrites = await gatherIterator(mapInput(inputKeys, this.input));
806
- if (inputWrites.length > 0) {
807
- const discardTasks = _prepareNextTasks(this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, this.config, true, { step: this.step });
808
- _applyWrites(this.checkpoint, this.channels, Object.values(discardTasks).concat([
809
- {
810
- name: INPUT,
811
- writes: inputWrites,
812
- triggers: [],
813
- },
814
- ]), this.checkpointerGetNextVersion, this.triggerToNodes);
815
- // save input checkpoint
816
- await this._putCheckpoint({ source: "input" });
817
- this.input = INPUT_DONE;
818
- }
819
- else if (!(CONFIG_KEY_RESUMING in (this.config.configurable ?? {}))) {
820
- throw new EmptyInputError(`Received no input writes for ${JSON.stringify(inputKeys, null, 2)}`);
821
- }
822
- else {
823
- // done with input
824
- this.input = INPUT_DONE;
825
- }
826
- }
827
- if (!this.isNested) {
828
- this.config = patchConfigurable(this.config, {
829
- [CONFIG_KEY_RESUMING]: this.isResuming,
830
- });
831
- }
832
- }
833
- _emit(values) {
834
- for (const [mode, payload] of values) {
835
- if (this.stream.modes.has(mode)) {
836
- this.stream.push([this.checkpointNamespace, mode, payload]);
837
- }
838
- // debug mode is a "checkpoints" or "tasks" wrapped in an object
839
- // TODO: consider deprecating this in 1.x
840
- if ((mode === "checkpoints" || mode === "tasks") &&
841
- this.stream.modes.has("debug")) {
842
- const step = mode === "checkpoints" ? this.step - 1 : this.step;
843
- const timestamp = new Date().toISOString();
844
- const type = (() => {
845
- if (mode === "checkpoints") {
846
- return "checkpoint";
847
- }
848
- else if (typeof payload === "object" &&
849
- payload != null &&
850
- "result" in payload) {
851
- return "task_result";
852
- }
853
- else {
854
- return "task";
855
- }
856
- })();
857
- this.stream.push([
858
- this.checkpointNamespace,
859
- "debug",
860
- { step, type, timestamp, payload },
861
- ]);
862
- }
863
- }
864
- }
865
- _putCheckpoint(inputMetadata) {
866
- const exiting = this.checkpointMetadata === inputMetadata;
867
- const doCheckpoint = this.checkpointer != null && (this.durability !== "exit" || exiting);
868
- const storeCheckpoint = (checkpoint) => {
869
- // store the previous checkpoint config for debug events
870
- this.prevCheckpointConfig = this.checkpointConfig?.configurable
871
- ?.checkpoint_id
872
- ? this.checkpointConfig
873
- : undefined;
874
- // child graphs keep at most one checkpoint per parent checkpoint
875
- // this is achieved by writing child checkpoints as progress is made
876
- // (so that error recovery / resuming from interrupt don't lose work)
877
- // but doing so always with an id equal to that of the parent checkpoint
878
- this.checkpointConfig = patchConfigurable(this.checkpointConfig, {
879
- [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "",
880
- });
881
- const channelVersions = { ...this.checkpoint.channel_versions };
882
- const newVersions = getNewChannelVersions(this.checkpointPreviousVersions, channelVersions);
883
- this.checkpointPreviousVersions = channelVersions;
884
- // save it, without blocking
885
- // if there's a previous checkpoint save in progress, wait for it
886
- // ensuring checkpointers receive checkpoints in order
887
- void this._checkpointerPutAfterPrevious({
888
- config: { ...this.checkpointConfig },
889
- checkpoint: copyCheckpoint(checkpoint),
890
- metadata: { ...this.checkpointMetadata },
891
- newVersions,
892
- });
893
- this.checkpointConfig = {
894
- ...this.checkpointConfig,
895
- configurable: {
896
- ...this.checkpointConfig.configurable,
897
- checkpoint_id: this.checkpoint.id,
898
- },
899
- };
900
- };
901
- if (!exiting) {
902
- this.checkpointMetadata = {
903
- ...inputMetadata,
904
- step: this.step,
905
- parents: this.config.configurable?.[CONFIG_KEY_CHECKPOINT_MAP] ?? {},
906
- };
907
- }
908
- // create new checkpoint
909
- this.checkpoint = createCheckpoint(this.checkpoint, doCheckpoint ? this.channels : undefined, this.step, exiting ? { id: this.checkpoint.id } : undefined);
910
- // Bail if no checkpointer
911
- if (doCheckpoint)
912
- storeCheckpoint(this.checkpoint);
913
- if (!exiting) {
914
- // increment step
915
- this.step += 1;
916
- }
917
- }
918
- _flushPendingWrites() {
919
- if (this.checkpointer == null)
920
- return;
921
- if (this.checkpointPendingWrites.length === 0)
922
- return;
923
- // patch config
924
- const config = patchConfigurable(this.checkpointConfig, {
925
- [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "",
926
- [CONFIG_KEY_CHECKPOINT_ID]: this.checkpoint.id,
927
- });
928
- // group writes by task id
929
- const byTask = {};
930
- for (const [tid, key, value] of this.checkpointPendingWrites) {
931
- byTask[tid] ??= [];
932
- byTask[tid].push([key, value]);
933
- }
934
- // submit writes to checkpointer
935
- for (const [tid, ws] of Object.entries(byTask)) {
936
- this.checkpointerPromises.push(this.checkpointer.putWrites(config, ws, tid));
937
- }
938
- }
939
- _matchWrites(tasks) {
940
- for (const [tid, k, v] of this.checkpointPendingWrites) {
941
- if (k === ERROR || k === INTERRUPT || k === RESUME) {
942
- continue;
943
- }
944
- const task = Object.values(tasks).find((t) => t.id === tid);
945
- if (task) {
946
- task.writes.push([k, v]);
947
- }
948
- }
949
- for (const task of Object.values(tasks)) {
950
- if (task.writes.length > 0) {
951
- this._outputWrites(task.id, task.writes, true);
952
- }
953
- }
954
- }
18
+ return new IterableReadableWritableStream({
19
+ passthroughFn: (value) => {
20
+ for (const stream of streams) if (stream.modes.has(value[1])) stream.push(value);
21
+ },
22
+ modes: new Set(streams.flatMap((s) => Array.from(s.modes)))
23
+ });
955
24
  }
25
+ var AsyncBatchedCache = class extends BaseCache {
26
+ cache;
27
+ queue = Promise.resolve();
28
+ constructor(cache) {
29
+ super();
30
+ this.cache = cache;
31
+ }
32
+ async get(keys) {
33
+ return this.enqueueOperation("get", keys);
34
+ }
35
+ async set(pairs) {
36
+ return this.enqueueOperation("set", pairs);
37
+ }
38
+ async clear(namespaces) {
39
+ return this.enqueueOperation("clear", namespaces);
40
+ }
41
+ async stop() {
42
+ await this.queue;
43
+ }
44
+ enqueueOperation(type, ...args) {
45
+ const newPromise = this.queue.then(() => {
46
+ return this.cache[type](...args);
47
+ });
48
+ this.queue = newPromise.then(() => void 0, () => void 0);
49
+ return newPromise;
50
+ }
51
+ };
52
+ var PregelLoop = class PregelLoop {
53
+ input;
54
+ output;
55
+ config;
56
+ checkpointer;
57
+ checkpointerGetNextVersion;
58
+ channels;
59
+ checkpoint;
60
+ checkpointIdSaved;
61
+ checkpointConfig;
62
+ checkpointMetadata;
63
+ checkpointNamespace;
64
+ checkpointPendingWrites = [];
65
+ checkpointPreviousVersions;
66
+ step;
67
+ stop;
68
+ durability;
69
+ outputKeys;
70
+ streamKeys;
71
+ nodes;
72
+ skipDoneTasks;
73
+ prevCheckpointConfig;
74
+ status = "pending";
75
+ tasks = {};
76
+ stream;
77
+ checkpointerPromises = [];
78
+ isNested;
79
+ _checkpointerChainedPromise = Promise.resolve();
80
+ store;
81
+ cache;
82
+ manager;
83
+ interruptAfter;
84
+ interruptBefore;
85
+ toInterrupt = [];
86
+ debug = false;
87
+ triggerToNodes;
88
+ get isResuming() {
89
+ let hasChannelVersions = false;
90
+ if (START in this.checkpoint.channel_versions) hasChannelVersions = true;
91
+ else for (const chan in this.checkpoint.channel_versions) if (Object.prototype.hasOwnProperty.call(this.checkpoint.channel_versions, chan)) {
92
+ hasChannelVersions = true;
93
+ break;
94
+ }
95
+ const configHasResumingFlag = this.config.configurable?.[CONFIG_KEY_RESUMING] !== void 0;
96
+ const configIsResuming = configHasResumingFlag && this.config.configurable?.[CONFIG_KEY_RESUMING];
97
+ const inputIsNullOrUndefined = this.input === null || this.input === void 0;
98
+ const inputIsCommandResuming = isCommand(this.input) && this.input.resume != null;
99
+ const inputIsResuming = this.input === INPUT_RESUMING;
100
+ const runIdMatchesPrevious = !this.isNested && this.config.metadata?.run_id !== void 0 && this.checkpointMetadata?.run_id !== void 0 && this.config.metadata.run_id === this.checkpointMetadata?.run_id;
101
+ return hasChannelVersions && (configIsResuming || inputIsNullOrUndefined || inputIsCommandResuming || inputIsResuming || runIdMatchesPrevious);
102
+ }
103
+ constructor(params) {
104
+ this.input = params.input;
105
+ this.checkpointer = params.checkpointer;
106
+ if (this.checkpointer !== void 0) this.checkpointerGetNextVersion = this.checkpointer.getNextVersion.bind(this.checkpointer);
107
+ else this.checkpointerGetNextVersion = increment;
108
+ this.checkpoint = params.checkpoint;
109
+ this.checkpointMetadata = params.checkpointMetadata;
110
+ this.checkpointPreviousVersions = params.checkpointPreviousVersions;
111
+ this.channels = params.channels;
112
+ this.checkpointPendingWrites = params.checkpointPendingWrites;
113
+ this.step = params.step;
114
+ this.stop = params.stop;
115
+ this.config = params.config;
116
+ this.checkpointConfig = params.checkpointConfig;
117
+ this.isNested = params.isNested;
118
+ this.manager = params.manager;
119
+ this.outputKeys = params.outputKeys;
120
+ this.streamKeys = params.streamKeys;
121
+ this.nodes = params.nodes;
122
+ this.skipDoneTasks = params.skipDoneTasks;
123
+ this.store = params.store;
124
+ this.cache = params.cache ? new AsyncBatchedCache(params.cache) : void 0;
125
+ this.stream = params.stream;
126
+ this.checkpointNamespace = params.checkpointNamespace;
127
+ this.prevCheckpointConfig = params.prevCheckpointConfig;
128
+ this.interruptAfter = params.interruptAfter;
129
+ this.interruptBefore = params.interruptBefore;
130
+ this.durability = params.durability;
131
+ this.debug = params.debug;
132
+ this.triggerToNodes = params.triggerToNodes;
133
+ }
134
+ static async initialize(params) {
135
+ let { config, stream } = params;
136
+ if (stream !== void 0 && config.configurable?.[CONFIG_KEY_STREAM] !== void 0) stream = createDuplexStream(stream, config.configurable[CONFIG_KEY_STREAM]);
137
+ const skipDoneTasks = config.configurable ? !("checkpoint_id" in config.configurable) : true;
138
+ const scratchpad = config.configurable?.[CONFIG_KEY_SCRATCHPAD];
139
+ if (config.configurable && scratchpad) {
140
+ if (scratchpad.subgraphCounter > 0) config = patchConfigurable(config, { [CONFIG_KEY_CHECKPOINT_NS]: [config.configurable[CONFIG_KEY_CHECKPOINT_NS], scratchpad.subgraphCounter.toString()].join(CHECKPOINT_NAMESPACE_SEPARATOR) });
141
+ scratchpad.subgraphCounter += 1;
142
+ }
143
+ const isNested = CONFIG_KEY_READ in (config.configurable ?? {});
144
+ if (!isNested && config.configurable?.checkpoint_ns !== void 0 && config.configurable?.checkpoint_ns !== "") config = patchConfigurable(config, {
145
+ checkpoint_ns: "",
146
+ checkpoint_id: void 0
147
+ });
148
+ let checkpointConfig = config;
149
+ if (config.configurable?.[CONFIG_KEY_CHECKPOINT_MAP] !== void 0 && config.configurable?.[CONFIG_KEY_CHECKPOINT_MAP]?.[config.configurable?.checkpoint_ns]) checkpointConfig = patchConfigurable(config, { checkpoint_id: config.configurable[CONFIG_KEY_CHECKPOINT_MAP][config.configurable?.checkpoint_ns] });
150
+ const checkpointNamespace = config.configurable?.checkpoint_ns?.split(CHECKPOINT_NAMESPACE_SEPARATOR) ?? [];
151
+ const saved = await params.checkpointer?.getTuple(checkpointConfig) ?? {
152
+ config,
153
+ checkpoint: emptyCheckpoint(),
154
+ metadata: {
155
+ source: "input",
156
+ step: -2,
157
+ parents: {}
158
+ },
159
+ pendingWrites: []
160
+ };
161
+ checkpointConfig = {
162
+ ...config,
163
+ ...saved.config,
164
+ configurable: {
165
+ checkpoint_ns: "",
166
+ ...config.configurable,
167
+ ...saved.config.configurable
168
+ }
169
+ };
170
+ const prevCheckpointConfig = saved.parentConfig;
171
+ const checkpoint = copyCheckpoint(saved.checkpoint);
172
+ const checkpointMetadata = { ...saved.metadata };
173
+ const checkpointPendingWrites = saved.pendingWrites ?? [];
174
+ const channels = emptyChannels(params.channelSpecs, checkpoint);
175
+ const step = (checkpointMetadata.step ?? 0) + 1;
176
+ const stop = step + (config.recursionLimit ?? DEFAULT_LOOP_LIMIT) + 1;
177
+ const checkpointPreviousVersions = { ...checkpoint.channel_versions };
178
+ const store = params.store ? new AsyncBatchedStore(params.store) : void 0;
179
+ if (store) await store.start();
180
+ return new PregelLoop({
181
+ input: params.input,
182
+ config,
183
+ checkpointer: params.checkpointer,
184
+ checkpoint,
185
+ checkpointMetadata,
186
+ checkpointConfig,
187
+ prevCheckpointConfig,
188
+ checkpointNamespace,
189
+ channels,
190
+ isNested,
191
+ manager: params.manager,
192
+ skipDoneTasks,
193
+ step,
194
+ stop,
195
+ checkpointPreviousVersions,
196
+ checkpointPendingWrites,
197
+ outputKeys: params.outputKeys ?? [],
198
+ streamKeys: params.streamKeys ?? [],
199
+ nodes: params.nodes,
200
+ stream,
201
+ store,
202
+ cache: params.cache,
203
+ interruptAfter: params.interruptAfter,
204
+ interruptBefore: params.interruptBefore,
205
+ durability: params.durability,
206
+ debug: params.debug,
207
+ triggerToNodes: params.triggerToNodes
208
+ });
209
+ }
210
+ _checkpointerPutAfterPrevious(input) {
211
+ this._checkpointerChainedPromise = this._checkpointerChainedPromise.then(() => {
212
+ return this.checkpointer?.put(input.config, input.checkpoint, input.metadata, input.newVersions);
213
+ });
214
+ this.checkpointerPromises.push(this._checkpointerChainedPromise);
215
+ }
216
+ /**
217
+ * Put writes for a task, to be read by the next tick.
218
+ * @param taskId
219
+ * @param writes
220
+ */
221
+ putWrites(taskId, writes) {
222
+ let writesCopy = writes;
223
+ if (writesCopy.length === 0) return;
224
+ if (writesCopy.every(([key]) => key in WRITES_IDX_MAP)) writesCopy = Array.from(new Map(writesCopy.map((w) => [w[0], w])).values());
225
+ this.checkpointPendingWrites = this.checkpointPendingWrites.filter((w) => w[0] !== taskId);
226
+ for (const [c, v] of writesCopy) this.checkpointPendingWrites.push([
227
+ taskId,
228
+ c,
229
+ v
230
+ ]);
231
+ const config = patchConfigurable(this.checkpointConfig, {
232
+ [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "",
233
+ [CONFIG_KEY_CHECKPOINT_ID]: this.checkpoint.id
234
+ });
235
+ if (this.durability !== "exit" && this.checkpointer != null) this.checkpointerPromises.push(this.checkpointer.putWrites(config, writesCopy, taskId));
236
+ if (this.tasks) this._outputWrites(taskId, writesCopy);
237
+ if (!writes.length || !this.cache || !this.tasks) return;
238
+ const task = this.tasks[taskId];
239
+ if (task == null || task.cache_key == null) return;
240
+ if (writes[0][0] === ERROR || writes[0][0] === INTERRUPT) return;
241
+ this.cache.set([{
242
+ key: [task.cache_key.ns, task.cache_key.key],
243
+ value: task.writes,
244
+ ttl: task.cache_key.ttl
245
+ }]);
246
+ }
247
+ _outputWrites(taskId, writes, cached = false) {
248
+ const task = this.tasks[taskId];
249
+ if (task !== void 0) {
250
+ if (task.config !== void 0 && (task.config.tags ?? []).includes(TAG_HIDDEN)) return;
251
+ if (writes.length > 0) {
252
+ if (writes[0][0] === INTERRUPT) {
253
+ if (task.path?.[0] === PUSH && task.path?.at(-1) === true) return;
254
+ const interruptWrites = writes.filter((w) => w[0] === INTERRUPT).flatMap((w) => w[1]);
255
+ this._emit([["updates", { [INTERRUPT]: interruptWrites }], ["values", { [INTERRUPT]: interruptWrites }]]);
256
+ } else if (writes[0][0] !== ERROR) this._emit(gatherIteratorSync(prefixGenerator(mapOutputUpdates(this.outputKeys, [[task, writes]], cached), "updates")));
257
+ }
258
+ if (!cached) this._emit(gatherIteratorSync(prefixGenerator(mapDebugTaskResults([[task, writes]], this.streamKeys), "tasks")));
259
+ }
260
+ }
261
+ async _matchCachedWrites() {
262
+ if (!this.cache) return [];
263
+ const matched = [];
264
+ const serializeKey = ([ns, key]) => {
265
+ return `ns:${ns.join(",")}|key:${key}`;
266
+ };
267
+ const keys = [];
268
+ const keyMap = {};
269
+ for (const task of Object.values(this.tasks)) if (task.cache_key != null && !task.writes.length) {
270
+ keys.push([task.cache_key.ns, task.cache_key.key]);
271
+ keyMap[serializeKey([task.cache_key.ns, task.cache_key.key])] = task;
272
+ }
273
+ if (keys.length === 0) return [];
274
+ const cache = await this.cache.get(keys);
275
+ for (const { key, value } of cache) {
276
+ const task = keyMap[serializeKey(key)];
277
+ if (task != null) {
278
+ task.writes.push(...value);
279
+ matched.push({
280
+ task,
281
+ result: value
282
+ });
283
+ }
284
+ }
285
+ return matched;
286
+ }
287
+ /**
288
+ * Execute a single iteration of the Pregel loop.
289
+ * Returns true if more iterations are needed.
290
+ * @param params
291
+ */
292
+ async tick(params) {
293
+ if (this.store && !this.store.isRunning) await this.store?.start();
294
+ const { inputKeys = [] } = params;
295
+ if (this.status !== "pending") throw new Error(`Cannot tick when status is no longer "pending". Current status: "${this.status}"`);
296
+ if (![INPUT_DONE, INPUT_RESUMING].includes(this.input)) await this._first(inputKeys);
297
+ else if (this.toInterrupt.length > 0) {
298
+ this.status = "interrupt_before";
299
+ throw new GraphInterrupt();
300
+ } else if (Object.values(this.tasks).every((task) => task.writes.length > 0)) {
301
+ const writes = Object.values(this.tasks).flatMap((t) => t.writes);
302
+ _applyWrites(this.checkpoint, this.channels, Object.values(this.tasks), this.checkpointerGetNextVersion, this.triggerToNodes);
303
+ const valuesOutput = await gatherIterator(prefixGenerator(mapOutputValues(this.outputKeys, writes, this.channels), "values"));
304
+ this._emit(valuesOutput);
305
+ this.checkpointPendingWrites = [];
306
+ await this._putCheckpoint({ source: "loop" });
307
+ if (shouldInterrupt(this.checkpoint, this.interruptAfter, Object.values(this.tasks))) {
308
+ this.status = "interrupt_after";
309
+ throw new GraphInterrupt();
310
+ }
311
+ if (this.config.configurable?.[CONFIG_KEY_RESUMING] !== void 0) delete this.config.configurable?.[CONFIG_KEY_RESUMING];
312
+ } else return false;
313
+ if (this.step > this.stop) {
314
+ this.status = "out_of_steps";
315
+ return false;
316
+ }
317
+ const nextTasks = _prepareNextTasks(this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, this.config, true, {
318
+ step: this.step,
319
+ checkpointer: this.checkpointer,
320
+ isResuming: this.isResuming,
321
+ manager: this.manager,
322
+ store: this.store,
323
+ stream: this.stream
324
+ });
325
+ this.tasks = nextTasks;
326
+ if (this.checkpointer) this._emit(await gatherIterator(prefixGenerator(mapDebugCheckpoint(this.checkpointConfig, this.channels, this.streamKeys, this.checkpointMetadata, Object.values(this.tasks), this.checkpointPendingWrites, this.prevCheckpointConfig, this.outputKeys), "checkpoints")));
327
+ if (Object.values(this.tasks).length === 0) {
328
+ this.status = "done";
329
+ return false;
330
+ }
331
+ if (this.skipDoneTasks && this.checkpointPendingWrites.length > 0) {
332
+ for (const [tid, k, v] of this.checkpointPendingWrites) {
333
+ if (k === ERROR || k === INTERRUPT || k === RESUME) continue;
334
+ const task = Object.values(this.tasks).find((t) => t.id === tid);
335
+ if (task) task.writes.push([k, v]);
336
+ }
337
+ for (const task of Object.values(this.tasks)) if (task.writes.length > 0) this._outputWrites(task.id, task.writes, true);
338
+ }
339
+ if (Object.values(this.tasks).every((task) => task.writes.length > 0)) return this.tick({ inputKeys });
340
+ if (shouldInterrupt(this.checkpoint, this.interruptBefore, Object.values(this.tasks))) {
341
+ this.status = "interrupt_before";
342
+ throw new GraphInterrupt();
343
+ }
344
+ const debugOutput = await gatherIterator(prefixGenerator(mapDebugTasks(Object.values(this.tasks)), "tasks"));
345
+ this._emit(debugOutput);
346
+ return true;
347
+ }
348
+ async finishAndHandleError(error) {
349
+ if (this.durability === "exit" && (!this.isNested || typeof error !== "undefined" || this.checkpointNamespace.every((part) => !part.includes(CHECKPOINT_NAMESPACE_END)))) {
350
+ this._putCheckpoint(this.checkpointMetadata);
351
+ this._flushPendingWrites();
352
+ }
353
+ const suppress = this._suppressInterrupt(error);
354
+ if (suppress || error === void 0) this.output = readChannels(this.channels, this.outputKeys);
355
+ if (suppress) {
356
+ if (this.tasks !== void 0 && this.checkpointPendingWrites.length > 0 && Object.values(this.tasks).some((task) => task.writes.length > 0)) {
357
+ _applyWrites(this.checkpoint, this.channels, Object.values(this.tasks), this.checkpointerGetNextVersion, this.triggerToNodes);
358
+ this._emit(gatherIteratorSync(prefixGenerator(mapOutputValues(this.outputKeys, Object.values(this.tasks).flatMap((t) => t.writes), this.channels), "values")));
359
+ }
360
+ if (isGraphInterrupt(error) && !error.interrupts.length) this._emit([["updates", { [INTERRUPT]: [] }], ["values", { [INTERRUPT]: [] }]]);
361
+ }
362
+ return suppress;
363
+ }
364
+ async acceptPush(task, writeIdx, call) {
365
+ if (this.interruptAfter?.length > 0 && shouldInterrupt(this.checkpoint, this.interruptAfter, [task])) {
366
+ this.toInterrupt.push(task);
367
+ return;
368
+ }
369
+ const pushed = _prepareSingleTask([
370
+ PUSH,
371
+ task.path ?? [],
372
+ writeIdx,
373
+ task.id,
374
+ call
375
+ ], this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, task.config ?? {}, true, {
376
+ step: this.step,
377
+ checkpointer: this.checkpointer,
378
+ manager: this.manager,
379
+ store: this.store,
380
+ stream: this.stream
381
+ });
382
+ if (!pushed) return;
383
+ if (this.interruptBefore?.length > 0 && shouldInterrupt(this.checkpoint, this.interruptBefore, [pushed])) {
384
+ this.toInterrupt.push(pushed);
385
+ return;
386
+ }
387
+ this._emit(gatherIteratorSync(prefixGenerator(mapDebugTasks([pushed]), "tasks")));
388
+ if (this.debug) printStepTasks(this.step, [pushed]);
389
+ this.tasks[pushed.id] = pushed;
390
+ if (this.skipDoneTasks) this._matchWrites({ [pushed.id]: pushed });
391
+ const tasks = await this._matchCachedWrites();
392
+ for (const { task: task$1 } of tasks) this._outputWrites(task$1.id, task$1.writes, true);
393
+ return pushed;
394
+ }
395
+ _suppressInterrupt(e) {
396
+ return isGraphInterrupt(e) && !this.isNested;
397
+ }
398
+ async _first(inputKeys) {
399
+ const { configurable } = this.config;
400
+ const scratchpad = configurable?.[CONFIG_KEY_SCRATCHPAD];
401
+ if (scratchpad && scratchpad.nullResume !== void 0) this.putWrites(NULL_TASK_ID, [[RESUME, scratchpad.nullResume]]);
402
+ if (isCommand(this.input)) {
403
+ const hasResume = this.input.resume != null;
404
+ if (this.input.resume != null && typeof this.input.resume === "object" && Object.keys(this.input.resume).every(isXXH3)) {
405
+ this.config.configurable ??= {};
406
+ this.config.configurable[CONFIG_KEY_RESUME_MAP] = this.input.resume;
407
+ }
408
+ if (hasResume && this.checkpointer == null) throw new Error("Cannot use Command(resume=...) without checkpointer");
409
+ const writes = {};
410
+ for (const [tid, key, value] of mapCommand(this.input, this.checkpointPendingWrites)) {
411
+ writes[tid] ??= [];
412
+ writes[tid].push([key, value]);
413
+ }
414
+ if (Object.keys(writes).length === 0) throw new EmptyInputError("Received empty Command input");
415
+ for (const [tid, ws] of Object.entries(writes)) this.putWrites(tid, ws);
416
+ }
417
+ const nullWrites = (this.checkpointPendingWrites ?? []).filter((w) => w[0] === NULL_TASK_ID).map((w) => w.slice(1));
418
+ if (nullWrites.length > 0) _applyWrites(this.checkpoint, this.channels, [{
419
+ name: INPUT,
420
+ writes: nullWrites,
421
+ triggers: []
422
+ }], this.checkpointerGetNextVersion, this.triggerToNodes);
423
+ const isCommandUpdateOrGoto = isCommand(this.input) && nullWrites.length > 0;
424
+ if (this.isResuming || isCommandUpdateOrGoto) {
425
+ for (const channelName in this.channels) {
426
+ if (!Object.prototype.hasOwnProperty.call(this.channels, channelName)) continue;
427
+ if (this.checkpoint.channel_versions[channelName] !== void 0) {
428
+ const version = this.checkpoint.channel_versions[channelName];
429
+ this.checkpoint.versions_seen[INTERRUPT] = {
430
+ ...this.checkpoint.versions_seen[INTERRUPT],
431
+ [channelName]: version
432
+ };
433
+ }
434
+ }
435
+ const valuesOutput = await gatherIterator(prefixGenerator(mapOutputValues(this.outputKeys, true, this.channels), "values"));
436
+ this._emit(valuesOutput);
437
+ }
438
+ if (this.isResuming) this.input = INPUT_RESUMING;
439
+ else if (isCommandUpdateOrGoto) {
440
+ await this._putCheckpoint({ source: "input" });
441
+ this.input = INPUT_DONE;
442
+ } else {
443
+ const inputWrites = await gatherIterator(mapInput(inputKeys, this.input));
444
+ if (inputWrites.length > 0) {
445
+ const discardTasks = _prepareNextTasks(this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, this.config, true, { step: this.step });
446
+ _applyWrites(this.checkpoint, this.channels, Object.values(discardTasks).concat([{
447
+ name: INPUT,
448
+ writes: inputWrites,
449
+ triggers: []
450
+ }]), this.checkpointerGetNextVersion, this.triggerToNodes);
451
+ await this._putCheckpoint({ source: "input" });
452
+ this.input = INPUT_DONE;
453
+ } else if (!(CONFIG_KEY_RESUMING in (this.config.configurable ?? {}))) throw new EmptyInputError(`Received no input writes for ${JSON.stringify(inputKeys, null, 2)}`);
454
+ else this.input = INPUT_DONE;
455
+ }
456
+ if (!this.isNested) this.config = patchConfigurable(this.config, { [CONFIG_KEY_RESUMING]: this.isResuming });
457
+ }
458
+ _emit(values) {
459
+ for (const [mode, payload] of values) {
460
+ if (this.stream.modes.has(mode)) this.stream.push([
461
+ this.checkpointNamespace,
462
+ mode,
463
+ payload
464
+ ]);
465
+ if ((mode === "checkpoints" || mode === "tasks") && this.stream.modes.has("debug")) {
466
+ const step = mode === "checkpoints" ? this.step - 1 : this.step;
467
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
468
+ const type = (() => {
469
+ if (mode === "checkpoints") return "checkpoint";
470
+ else if (typeof payload === "object" && payload != null && "result" in payload) return "task_result";
471
+ else return "task";
472
+ })();
473
+ this.stream.push([
474
+ this.checkpointNamespace,
475
+ "debug",
476
+ {
477
+ step,
478
+ type,
479
+ timestamp,
480
+ payload
481
+ }
482
+ ]);
483
+ }
484
+ }
485
+ }
486
+ _putCheckpoint(inputMetadata) {
487
+ const exiting = this.checkpointMetadata === inputMetadata;
488
+ const doCheckpoint = this.checkpointer != null && (this.durability !== "exit" || exiting);
489
+ const storeCheckpoint = (checkpoint) => {
490
+ this.prevCheckpointConfig = this.checkpointConfig?.configurable?.checkpoint_id ? this.checkpointConfig : void 0;
491
+ this.checkpointConfig = patchConfigurable(this.checkpointConfig, { [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "" });
492
+ const channelVersions = { ...this.checkpoint.channel_versions };
493
+ const newVersions = getNewChannelVersions(this.checkpointPreviousVersions, channelVersions);
494
+ this.checkpointPreviousVersions = channelVersions;
495
+ this._checkpointerPutAfterPrevious({
496
+ config: { ...this.checkpointConfig },
497
+ checkpoint: copyCheckpoint(checkpoint),
498
+ metadata: { ...this.checkpointMetadata },
499
+ newVersions
500
+ });
501
+ this.checkpointConfig = {
502
+ ...this.checkpointConfig,
503
+ configurable: {
504
+ ...this.checkpointConfig.configurable,
505
+ checkpoint_id: this.checkpoint.id
506
+ }
507
+ };
508
+ };
509
+ if (!exiting) this.checkpointMetadata = {
510
+ ...inputMetadata,
511
+ step: this.step,
512
+ parents: this.config.configurable?.[CONFIG_KEY_CHECKPOINT_MAP] ?? {}
513
+ };
514
+ this.checkpoint = createCheckpoint(this.checkpoint, doCheckpoint ? this.channels : void 0, this.step, exiting ? { id: this.checkpoint.id } : void 0);
515
+ if (doCheckpoint) storeCheckpoint(this.checkpoint);
516
+ if (!exiting) this.step += 1;
517
+ }
518
+ _flushPendingWrites() {
519
+ if (this.checkpointer == null) return;
520
+ if (this.checkpointPendingWrites.length === 0) return;
521
+ const config = patchConfigurable(this.checkpointConfig, {
522
+ [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "",
523
+ [CONFIG_KEY_CHECKPOINT_ID]: this.checkpoint.id
524
+ });
525
+ const byTask = {};
526
+ for (const [tid, key, value] of this.checkpointPendingWrites) {
527
+ byTask[tid] ??= [];
528
+ byTask[tid].push([key, value]);
529
+ }
530
+ for (const [tid, ws] of Object.entries(byTask)) this.checkpointerPromises.push(this.checkpointer.putWrites(config, ws, tid));
531
+ }
532
+ _matchWrites(tasks) {
533
+ for (const [tid, k, v] of this.checkpointPendingWrites) {
534
+ if (k === ERROR || k === INTERRUPT || k === RESUME) continue;
535
+ const task = Object.values(tasks).find((t) => t.id === tid);
536
+ if (task) task.writes.push([k, v]);
537
+ }
538
+ for (const task of Object.values(tasks)) if (task.writes.length > 0) this._outputWrites(task.id, task.writes, true);
539
+ }
540
+ };
541
+
542
+ //#endregion
543
+ export { PregelLoop };
956
544
  //# sourceMappingURL=loop.js.map