@langchain/langgraph 0.4.9 → 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (505) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +1 -2
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/channels/any_value.cjs +44 -63
  5. package/dist/channels/any_value.cjs.map +1 -0
  6. package/dist/channels/any_value.d.cts +24 -0
  7. package/dist/channels/any_value.d.cts.map +1 -0
  8. package/dist/channels/any_value.d.ts +16 -11
  9. package/dist/channels/any_value.d.ts.map +1 -0
  10. package/dist/channels/any_value.js +42 -57
  11. package/dist/channels/any_value.js.map +1 -1
  12. package/dist/channels/base.cjs +93 -133
  13. package/dist/channels/base.cjs.map +1 -0
  14. package/dist/channels/base.d.cts +79 -0
  15. package/dist/channels/base.d.cts.map +1 -0
  16. package/dist/channels/base.d.ts +77 -73
  17. package/dist/channels/base.d.ts.map +1 -0
  18. package/dist/channels/base.js +90 -127
  19. package/dist/channels/base.js.map +1 -1
  20. package/dist/channels/binop.cjs +47 -77
  21. package/dist/channels/binop.cjs.map +1 -0
  22. package/dist/channels/binop.d.cts +22 -0
  23. package/dist/channels/binop.d.cts.map +1 -0
  24. package/dist/channels/binop.d.ts +17 -12
  25. package/dist/channels/binop.d.ts.map +1 -0
  26. package/dist/channels/binop.js +45 -71
  27. package/dist/channels/binop.js.map +1 -1
  28. package/dist/channels/dynamic_barrier_value.cjs +72 -208
  29. package/dist/channels/dynamic_barrier_value.cjs.map +1 -0
  30. package/dist/channels/dynamic_barrier_value.d.cts +42 -0
  31. package/dist/channels/dynamic_barrier_value.d.cts.map +1 -0
  32. package/dist/channels/dynamic_barrier_value.d.ts +20 -35
  33. package/dist/channels/dynamic_barrier_value.d.ts.map +1 -0
  34. package/dist/channels/dynamic_barrier_value.js +69 -200
  35. package/dist/channels/dynamic_barrier_value.js.map +1 -1
  36. package/dist/channels/ephemeral_value.cjs +44 -70
  37. package/dist/channels/ephemeral_value.cjs.map +1 -0
  38. package/dist/channels/ephemeral_value.d.cts +22 -0
  39. package/dist/channels/ephemeral_value.d.cts.map +1 -0
  40. package/dist/channels/ephemeral_value.d.ts +18 -12
  41. package/dist/channels/ephemeral_value.d.ts.map +1 -0
  42. package/dist/channels/ephemeral_value.js +43 -65
  43. package/dist/channels/ephemeral_value.js.map +1 -1
  44. package/dist/channels/index.cjs +21 -10
  45. package/dist/channels/index.d.cts +9 -0
  46. package/dist/channels/index.d.ts +9 -7
  47. package/dist/channels/index.js +10 -3
  48. package/dist/channels/last_value.cjs +90 -140
  49. package/dist/channels/last_value.cjs.map +1 -0
  50. package/dist/channels/last_value.d.cts +42 -0
  51. package/dist/channels/last_value.d.cts.map +1 -0
  52. package/dist/channels/last_value.d.ts +27 -23
  53. package/dist/channels/last_value.d.ts.map +1 -0
  54. package/dist/channels/last_value.js +87 -133
  55. package/dist/channels/last_value.js.map +1 -1
  56. package/dist/channels/named_barrier_value.cjs +114 -170
  57. package/dist/channels/named_barrier_value.cjs.map +1 -0
  58. package/dist/channels/named_barrier_value.d.cts +46 -0
  59. package/dist/channels/named_barrier_value.d.cts.map +1 -0
  60. package/dist/channels/named_barrier_value.d.ts +32 -29
  61. package/dist/channels/named_barrier_value.d.ts.map +1 -0
  62. package/dist/channels/named_barrier_value.js +111 -163
  63. package/dist/channels/named_barrier_value.js.map +1 -1
  64. package/dist/channels/topic.cjs +63 -96
  65. package/dist/channels/topic.cjs.map +1 -0
  66. package/dist/channels/topic.d.cts +32 -0
  67. package/dist/channels/topic.d.cts.map +1 -0
  68. package/dist/channels/topic.d.ts +28 -19
  69. package/dist/channels/topic.d.ts.map +1 -0
  70. package/dist/channels/topic.js +61 -90
  71. package/dist/channels/topic.js.map +1 -1
  72. package/dist/constants.cjs +376 -452
  73. package/dist/constants.cjs.map +1 -0
  74. package/dist/constants.d.cts +285 -0
  75. package/dist/constants.d.cts.map +1 -0
  76. package/dist/constants.d.ts +113 -148
  77. package/dist/constants.d.ts.map +1 -0
  78. package/dist/constants.js +336 -444
  79. package/dist/constants.js.map +1 -1
  80. package/dist/errors.cjs +151 -179
  81. package/dist/errors.cjs.map +1 -0
  82. package/dist/errors.d.cts +81 -0
  83. package/dist/errors.d.cts.map +1 -0
  84. package/dist/errors.d.ts +56 -46
  85. package/dist/errors.d.ts.map +1 -0
  86. package/dist/errors.js +140 -165
  87. package/dist/errors.js.map +1 -1
  88. package/dist/func/index.cjs +292 -290
  89. package/dist/func/index.cjs.map +1 -0
  90. package/dist/func/index.d.cts +293 -0
  91. package/dist/func/index.d.cts.map +1 -0
  92. package/dist/func/index.d.ts +86 -72
  93. package/dist/func/index.d.ts.map +1 -0
  94. package/dist/func/index.js +288 -284
  95. package/dist/func/index.js.map +1 -1
  96. package/dist/func/types.d.cts +64 -0
  97. package/dist/func/types.d.cts.map +1 -0
  98. package/dist/func/types.d.ts +24 -19
  99. package/dist/func/types.d.ts.map +1 -0
  100. package/dist/graph/annotation.cjs +96 -120
  101. package/dist/graph/annotation.cjs.map +1 -0
  102. package/dist/graph/annotation.d.cts +116 -0
  103. package/dist/graph/annotation.d.cts.map +1 -0
  104. package/dist/graph/annotation.d.ts +33 -34
  105. package/dist/graph/annotation.d.ts.map +1 -0
  106. package/dist/graph/annotation.js +91 -112
  107. package/dist/graph/annotation.js.map +1 -1
  108. package/dist/graph/graph.cjs +419 -723
  109. package/dist/graph/graph.cjs.map +1 -0
  110. package/dist/graph/graph.d.cts +133 -0
  111. package/dist/graph/graph.d.cts.map +1 -0
  112. package/dist/graph/graph.d.ts +124 -91
  113. package/dist/graph/graph.d.ts.map +1 -0
  114. package/dist/graph/graph.js +412 -713
  115. package/dist/graph/graph.js.map +1 -1
  116. package/dist/graph/index.cjs +4 -17
  117. package/dist/graph/index.js +6 -5
  118. package/dist/graph/message.cjs +59 -104
  119. package/dist/graph/message.cjs.map +1 -0
  120. package/dist/graph/message.d.cts +19 -0
  121. package/dist/graph/message.d.cts.map +1 -0
  122. package/dist/graph/message.d.ts +11 -10
  123. package/dist/graph/message.d.ts.map +1 -0
  124. package/dist/graph/message.js +56 -98
  125. package/dist/graph/message.js.map +1 -1
  126. package/dist/graph/messages_annotation.cjs +106 -106
  127. package/dist/graph/messages_annotation.cjs.map +1 -0
  128. package/dist/graph/messages_annotation.d.cts +111 -0
  129. package/dist/graph/messages_annotation.d.cts.map +1 -0
  130. package/dist/graph/messages_annotation.d.ts +18 -9
  131. package/dist/graph/messages_annotation.d.ts.map +1 -0
  132. package/dist/graph/messages_annotation.js +100 -100
  133. package/dist/graph/messages_annotation.js.map +1 -1
  134. package/dist/graph/state.cjs +475 -779
  135. package/dist/graph/state.cjs.map +1 -0
  136. package/dist/graph/state.d.cts +231 -0
  137. package/dist/graph/state.d.cts.map +1 -0
  138. package/dist/graph/state.d.ts +148 -138
  139. package/dist/graph/state.d.ts.map +1 -0
  140. package/dist/graph/state.js +469 -769
  141. package/dist/graph/state.js.map +1 -1
  142. package/dist/graph/zod/index.cjs +10 -21
  143. package/dist/graph/zod/index.d.cts +3 -0
  144. package/dist/graph/zod/index.d.ts +3 -3
  145. package/dist/graph/zod/index.js +4 -4
  146. package/dist/graph/zod/meta.cjs +142 -177
  147. package/dist/graph/zod/meta.cjs.map +1 -0
  148. package/dist/graph/zod/meta.d.cts +116 -0
  149. package/dist/graph/zod/meta.d.cts.map +1 -0
  150. package/dist/graph/zod/meta.d.ts +99 -97
  151. package/dist/graph/zod/meta.d.ts.map +1 -0
  152. package/dist/graph/zod/meta.js +136 -170
  153. package/dist/graph/zod/meta.js.map +1 -1
  154. package/dist/graph/zod/plugin.cjs +36 -39
  155. package/dist/graph/zod/plugin.cjs.map +1 -0
  156. package/dist/graph/zod/plugin.js +34 -35
  157. package/dist/graph/zod/plugin.js.map +1 -1
  158. package/dist/graph/zod/schema.cjs +82 -110
  159. package/dist/graph/zod/schema.cjs.map +1 -0
  160. package/dist/graph/zod/schema.d.cts +38 -0
  161. package/dist/graph/zod/schema.d.cts.map +1 -0
  162. package/dist/graph/zod/schema.d.ts +12 -6
  163. package/dist/graph/zod/schema.d.ts.map +1 -0
  164. package/dist/graph/zod/schema.js +77 -103
  165. package/dist/graph/zod/schema.js.map +1 -1
  166. package/dist/graph/zod/zod-registry.cjs +41 -47
  167. package/dist/graph/zod/zod-registry.cjs.map +1 -0
  168. package/dist/graph/zod/zod-registry.d.cts +51 -0
  169. package/dist/graph/zod/zod-registry.d.cts.map +1 -0
  170. package/dist/graph/zod/zod-registry.d.ts +34 -26
  171. package/dist/graph/zod/zod-registry.d.ts.map +1 -0
  172. package/dist/graph/zod/zod-registry.js +37 -41
  173. package/dist/graph/zod/zod-registry.js.map +1 -1
  174. package/dist/hash.cjs +205 -267
  175. package/dist/hash.cjs.map +1 -0
  176. package/dist/hash.js +205 -265
  177. package/dist/hash.js.map +1 -1
  178. package/dist/index.cjs +111 -33
  179. package/dist/index.cjs.map +1 -0
  180. package/dist/index.d.cts +26 -0
  181. package/dist/index.d.ts +26 -5
  182. package/dist/index.js +19 -7
  183. package/dist/index.js.map +1 -1
  184. package/dist/interrupt.cjs +79 -85
  185. package/dist/interrupt.cjs.map +1 -0
  186. package/dist/interrupt.d.cts +49 -0
  187. package/dist/interrupt.d.cts.map +1 -0
  188. package/dist/interrupt.d.ts +6 -1
  189. package/dist/interrupt.d.ts.map +1 -0
  190. package/dist/interrupt.js +76 -80
  191. package/dist/interrupt.js.map +1 -1
  192. package/dist/prebuilt/agentName.cjs +139 -172
  193. package/dist/prebuilt/agentName.cjs.map +1 -0
  194. package/dist/prebuilt/agentName.d.cts +42 -0
  195. package/dist/prebuilt/agentName.d.cts.map +1 -0
  196. package/dist/prebuilt/agentName.d.ts +13 -21
  197. package/dist/prebuilt/agentName.d.ts.map +1 -0
  198. package/dist/prebuilt/agentName.js +139 -168
  199. package/dist/prebuilt/agentName.js.map +1 -1
  200. package/dist/prebuilt/agent_executor.cjs +42 -80
  201. package/dist/prebuilt/agent_executor.cjs.map +1 -0
  202. package/dist/prebuilt/agent_executor.d.cts +57 -0
  203. package/dist/prebuilt/agent_executor.d.cts.map +1 -0
  204. package/dist/prebuilt/agent_executor.d.ts +47 -38
  205. package/dist/prebuilt/agent_executor.d.ts.map +1 -0
  206. package/dist/prebuilt/agent_executor.js +40 -75
  207. package/dist/prebuilt/agent_executor.js.map +1 -1
  208. package/dist/prebuilt/chat_agent_executor.cjs +66 -128
  209. package/dist/prebuilt/chat_agent_executor.cjs.map +1 -0
  210. package/dist/prebuilt/chat_agent_executor.d.cts +23 -0
  211. package/dist/prebuilt/chat_agent_executor.d.cts.map +1 -0
  212. package/dist/prebuilt/chat_agent_executor.d.ts +18 -10
  213. package/dist/prebuilt/chat_agent_executor.d.ts.map +1 -0
  214. package/dist/prebuilt/chat_agent_executor.js +63 -123
  215. package/dist/prebuilt/chat_agent_executor.js.map +1 -1
  216. package/dist/prebuilt/index.cjs +15 -18
  217. package/dist/prebuilt/index.d.cts +8 -0
  218. package/dist/prebuilt/index.d.ts +8 -8
  219. package/dist/prebuilt/index.js +8 -7
  220. package/dist/prebuilt/interrupt.d.cts +73 -0
  221. package/dist/prebuilt/interrupt.d.cts.map +1 -0
  222. package/dist/prebuilt/interrupt.d.ts +32 -15
  223. package/dist/prebuilt/interrupt.d.ts.map +1 -0
  224. package/dist/prebuilt/react_agent_executor.cjs +317 -473
  225. package/dist/prebuilt/react_agent_executor.cjs.map +1 -0
  226. package/dist/prebuilt/react_agent_executor.d.cts +229 -0
  227. package/dist/prebuilt/react_agent_executor.d.cts.map +1 -0
  228. package/dist/prebuilt/react_agent_executor.d.ts +171 -143
  229. package/dist/prebuilt/react_agent_executor.d.ts.map +1 -0
  230. package/dist/prebuilt/react_agent_executor.js +315 -465
  231. package/dist/prebuilt/react_agent_executor.js.map +1 -1
  232. package/dist/prebuilt/tool_executor.cjs +45 -67
  233. package/dist/prebuilt/tool_executor.cjs.map +1 -0
  234. package/dist/prebuilt/tool_executor.d.cts +42 -0
  235. package/dist/prebuilt/tool_executor.d.cts.map +1 -0
  236. package/dist/prebuilt/tool_executor.d.ts +30 -24
  237. package/dist/prebuilt/tool_executor.d.ts.map +1 -0
  238. package/dist/prebuilt/tool_executor.js +44 -63
  239. package/dist/prebuilt/tool_executor.js.map +1 -1
  240. package/dist/prebuilt/tool_node.cjs +213 -275
  241. package/dist/prebuilt/tool_node.cjs.map +1 -0
  242. package/dist/prebuilt/tool_node.d.cts +151 -0
  243. package/dist/prebuilt/tool_node.d.cts.map +1 -0
  244. package/dist/prebuilt/tool_node.d.ts +30 -17
  245. package/dist/prebuilt/tool_node.d.ts.map +1 -0
  246. package/dist/prebuilt/tool_node.js +209 -268
  247. package/dist/prebuilt/tool_node.js.map +1 -1
  248. package/dist/pregel/algo.cjs +551 -692
  249. package/dist/pregel/algo.cjs.map +1 -0
  250. package/dist/pregel/algo.d.cts +13 -0
  251. package/dist/pregel/algo.d.cts.map +1 -0
  252. package/dist/pregel/algo.d.ts +8 -38
  253. package/dist/pregel/algo.d.ts.map +1 -0
  254. package/dist/pregel/algo.js +546 -683
  255. package/dist/pregel/algo.js.map +1 -1
  256. package/dist/pregel/call.cjs +46 -42
  257. package/dist/pregel/call.cjs.map +1 -0
  258. package/dist/pregel/call.js +43 -37
  259. package/dist/pregel/call.js.map +1 -1
  260. package/dist/pregel/debug.cjs +153 -223
  261. package/dist/pregel/debug.cjs.map +1 -0
  262. package/dist/pregel/debug.js +152 -215
  263. package/dist/pregel/debug.js.map +1 -1
  264. package/dist/pregel/index.cjs +1138 -1604
  265. package/dist/pregel/index.cjs.map +1 -0
  266. package/dist/pregel/index.d.cts +536 -0
  267. package/dist/pregel/index.d.cts.map +1 -0
  268. package/dist/pregel/index.d.ts +460 -422
  269. package/dist/pregel/index.d.ts.map +1 -0
  270. package/dist/pregel/index.js +1129 -1592
  271. package/dist/pregel/index.js.map +1 -1
  272. package/dist/pregel/io.cjs +127 -234
  273. package/dist/pregel/io.cjs.map +1 -0
  274. package/dist/pregel/io.js +122 -225
  275. package/dist/pregel/io.js.map +1 -1
  276. package/dist/pregel/loop.cjs +542 -954
  277. package/dist/pregel/loop.cjs.map +1 -0
  278. package/dist/pregel/loop.js +539 -948
  279. package/dist/pregel/loop.js.map +1 -1
  280. package/dist/pregel/messages.cjs +104 -196
  281. package/dist/pregel/messages.cjs.map +1 -0
  282. package/dist/pregel/messages.js +102 -191
  283. package/dist/pregel/messages.js.map +1 -1
  284. package/dist/pregel/read.cjs +150 -280
  285. package/dist/pregel/read.cjs.map +1 -0
  286. package/dist/pregel/read.d.cts +51 -0
  287. package/dist/pregel/read.d.cts.map +1 -0
  288. package/dist/pregel/read.d.ts +48 -46
  289. package/dist/pregel/read.d.ts.map +1 -0
  290. package/dist/pregel/read.js +147 -274
  291. package/dist/pregel/read.js.map +1 -1
  292. package/dist/pregel/remote.cjs +339 -458
  293. package/dist/pregel/remote.cjs.map +1 -0
  294. package/dist/pregel/remote.d.cts +121 -0
  295. package/dist/pregel/remote.d.cts.map +1 -0
  296. package/dist/pregel/remote.d.ts +79 -69
  297. package/dist/pregel/remote.d.ts.map +1 -0
  298. package/dist/pregel/remote.js +337 -453
  299. package/dist/pregel/remote.js.map +1 -1
  300. package/dist/pregel/retry.cjs +87 -138
  301. package/dist/pregel/retry.cjs.map +1 -0
  302. package/dist/pregel/retry.js +83 -130
  303. package/dist/pregel/retry.js.map +1 -1
  304. package/dist/pregel/runnable_types.d.cts +49 -0
  305. package/dist/pregel/runnable_types.d.cts.map +1 -0
  306. package/dist/pregel/runnable_types.d.ts +47 -17
  307. package/dist/pregel/runnable_types.d.ts.map +1 -0
  308. package/dist/pregel/runner.cjs +222 -315
  309. package/dist/pregel/runner.cjs.map +1 -0
  310. package/dist/pregel/runner.js +219 -308
  311. package/dist/pregel/runner.js.map +1 -1
  312. package/dist/pregel/stream.cjs +89 -130
  313. package/dist/pregel/stream.cjs.map +1 -0
  314. package/dist/pregel/stream.js +87 -125
  315. package/dist/pregel/stream.js.map +1 -1
  316. package/dist/pregel/types.cjs +25 -62
  317. package/dist/pregel/types.cjs.map +1 -0
  318. package/dist/pregel/types.d.cts +440 -0
  319. package/dist/pregel/types.d.cts.map +1 -0
  320. package/dist/pregel/types.d.ts +381 -432
  321. package/dist/pregel/types.d.ts.map +1 -0
  322. package/dist/pregel/types.js +23 -57
  323. package/dist/pregel/types.js.map +1 -1
  324. package/dist/pregel/utils/config.cjs +101 -147
  325. package/dist/pregel/utils/config.cjs.map +1 -0
  326. package/dist/pregel/utils/config.d.cts +36 -0
  327. package/dist/pregel/utils/config.d.cts.map +1 -0
  328. package/dist/pregel/utils/config.d.ts +12 -9
  329. package/dist/pregel/utils/config.d.ts.map +1 -0
  330. package/dist/pregel/utils/config.js +99 -143
  331. package/dist/pregel/utils/config.js.map +1 -1
  332. package/dist/pregel/utils/index.cjs +87 -125
  333. package/dist/pregel/utils/index.cjs.map +1 -0
  334. package/dist/pregel/utils/index.d.cts +51 -0
  335. package/dist/pregel/utils/index.d.cts.map +1 -0
  336. package/dist/pregel/utils/index.d.ts +45 -61
  337. package/dist/pregel/utils/index.d.ts.map +1 -0
  338. package/dist/pregel/utils/index.js +86 -120
  339. package/dist/pregel/utils/index.js.map +1 -1
  340. package/dist/pregel/utils/subgraph.cjs +15 -26
  341. package/dist/pregel/utils/subgraph.cjs.map +1 -0
  342. package/dist/pregel/utils/subgraph.js +12 -21
  343. package/dist/pregel/utils/subgraph.js.map +1 -1
  344. package/dist/pregel/validate.cjs +42 -92
  345. package/dist/pregel/validate.cjs.map +1 -0
  346. package/dist/pregel/validate.js +39 -84
  347. package/dist/pregel/validate.js.map +1 -1
  348. package/dist/pregel/write.cjs +87 -137
  349. package/dist/pregel/write.cjs.map +1 -0
  350. package/dist/pregel/write.d.cts +35 -0
  351. package/dist/pregel/write.d.cts.map +1 -0
  352. package/dist/pregel/write.d.ts +27 -23
  353. package/dist/pregel/write.d.ts.map +1 -0
  354. package/dist/pregel/write.js +83 -131
  355. package/dist/pregel/write.js.map +1 -1
  356. package/dist/remote.cjs +3 -6
  357. package/dist/remote.d.cts +2 -0
  358. package/dist/remote.d.ts +2 -1
  359. package/dist/remote.js +3 -2
  360. package/dist/setup/async_local_storage.cjs +10 -7
  361. package/dist/setup/async_local_storage.cjs.map +1 -0
  362. package/dist/setup/async_local_storage.js +7 -2
  363. package/dist/setup/async_local_storage.js.map +1 -1
  364. package/dist/ui/index.cjs +4 -0
  365. package/dist/ui/index.d.cts +5 -0
  366. package/dist/ui/index.d.ts +5 -0
  367. package/dist/ui/index.js +3 -0
  368. package/dist/ui/stream.cjs +145 -0
  369. package/dist/ui/stream.cjs.map +1 -0
  370. package/dist/ui/stream.d.cts +25 -0
  371. package/dist/ui/stream.d.cts.map +1 -0
  372. package/dist/ui/stream.d.ts +25 -0
  373. package/dist/ui/stream.d.ts.map +1 -0
  374. package/dist/ui/stream.js +143 -0
  375. package/dist/ui/stream.js.map +1 -0
  376. package/dist/ui/types.infer.d.cts +53 -0
  377. package/dist/ui/types.infer.d.cts.map +1 -0
  378. package/dist/ui/types.infer.d.ts +53 -0
  379. package/dist/ui/types.infer.d.ts.map +1 -0
  380. package/dist/ui/types.message.d.cts +95 -0
  381. package/dist/ui/types.message.d.cts.map +1 -0
  382. package/dist/ui/types.message.d.ts +95 -0
  383. package/dist/ui/types.message.d.ts.map +1 -0
  384. package/dist/ui/types.schema.d.cts +228 -0
  385. package/dist/ui/types.schema.d.cts.map +1 -0
  386. package/dist/ui/types.schema.d.ts +228 -0
  387. package/dist/ui/types.schema.d.ts.map +1 -0
  388. package/dist/utils.cjs +77 -147
  389. package/dist/utils.cjs.map +1 -0
  390. package/dist/utils.d.cts +32 -0
  391. package/dist/utils.d.cts.map +1 -0
  392. package/dist/utils.d.ts +29 -25
  393. package/dist/utils.d.ts.map +1 -0
  394. package/dist/utils.js +73 -140
  395. package/dist/utils.js.map +1 -1
  396. package/dist/web.cjs +96 -55
  397. package/dist/web.d.cts +23 -0
  398. package/dist/web.d.ts +23 -14
  399. package/dist/web.js +15 -8
  400. package/dist/writer.cjs +15 -0
  401. package/dist/writer.cjs.map +1 -0
  402. package/dist/writer.d.cts +5 -0
  403. package/dist/writer.d.cts.map +1 -0
  404. package/dist/writer.d.ts +5 -0
  405. package/dist/writer.d.ts.map +1 -0
  406. package/dist/writer.js +14 -0
  407. package/dist/writer.js.map +1 -0
  408. package/package.json +90 -83
  409. package/dist/channels/index.js.map +0 -1
  410. package/dist/func/types.cjs +0 -15
  411. package/dist/func/types.js +0 -12
  412. package/dist/func/types.js.map +0 -1
  413. package/dist/graph/index.d.ts +0 -4
  414. package/dist/graph/index.js.map +0 -1
  415. package/dist/graph/message.test.cjs +0 -196
  416. package/dist/graph/message.test.d.ts +0 -1
  417. package/dist/graph/message.test.js +0 -194
  418. package/dist/graph/message.test.js.map +0 -1
  419. package/dist/graph/zod/index.js.map +0 -1
  420. package/dist/graph/zod/plugin.d.ts +0 -28
  421. package/dist/hash.d.ts +0 -2
  422. package/dist/prebuilt/index.js.map +0 -1
  423. package/dist/prebuilt/interrupt.cjs +0 -3
  424. package/dist/prebuilt/interrupt.js +0 -2
  425. package/dist/prebuilt/interrupt.js.map +0 -1
  426. package/dist/pregel/call.d.ts +0 -16
  427. package/dist/pregel/debug.d.ts +0 -41
  428. package/dist/pregel/debug.test.cjs +0 -258
  429. package/dist/pregel/debug.test.d.ts +0 -1
  430. package/dist/pregel/debug.test.js +0 -256
  431. package/dist/pregel/debug.test.js.map +0 -1
  432. package/dist/pregel/io.d.ts +0 -30
  433. package/dist/pregel/io.mapCommand.test.cjs +0 -150
  434. package/dist/pregel/io.mapCommand.test.d.ts +0 -1
  435. package/dist/pregel/io.mapCommand.test.js +0 -148
  436. package/dist/pregel/io.mapCommand.test.js.map +0 -1
  437. package/dist/pregel/loop.d.ts +0 -147
  438. package/dist/pregel/messages.d.ts +0 -30
  439. package/dist/pregel/messages.test.cjs +0 -369
  440. package/dist/pregel/messages.test.d.ts +0 -1
  441. package/dist/pregel/messages.test.js +0 -367
  442. package/dist/pregel/messages.test.js.map +0 -1
  443. package/dist/pregel/read.test.cjs +0 -194
  444. package/dist/pregel/read.test.d.ts +0 -1
  445. package/dist/pregel/read.test.js +0 -192
  446. package/dist/pregel/read.test.js.map +0 -1
  447. package/dist/pregel/retry.d.ts +0 -17
  448. package/dist/pregel/runnable_types.cjs +0 -3
  449. package/dist/pregel/runnable_types.js +0 -2
  450. package/dist/pregel/runnable_types.js.map +0 -1
  451. package/dist/pregel/runner.d.ts +0 -79
  452. package/dist/pregel/runner.test.cjs +0 -66
  453. package/dist/pregel/runner.test.d.ts +0 -1
  454. package/dist/pregel/runner.test.js +0 -64
  455. package/dist/pregel/runner.test.js.map +0 -1
  456. package/dist/pregel/stream.d.ts +0 -40
  457. package/dist/pregel/utils/config.test.cjs +0 -214
  458. package/dist/pregel/utils/config.test.d.ts +0 -1
  459. package/dist/pregel/utils/config.test.js +0 -212
  460. package/dist/pregel/utils/config.test.js.map +0 -1
  461. package/dist/pregel/utils/subgraph.d.ts +0 -4
  462. package/dist/pregel/utils/subgraph.test.cjs +0 -83
  463. package/dist/pregel/utils/subgraph.test.d.ts +0 -1
  464. package/dist/pregel/utils/subgraph.test.js +0 -81
  465. package/dist/pregel/utils/subgraph.test.js.map +0 -1
  466. package/dist/pregel/validate.d.ts +0 -16
  467. package/dist/pregel/validate.test.cjs +0 -220
  468. package/dist/pregel/validate.test.d.ts +0 -1
  469. package/dist/pregel/validate.test.js +0 -218
  470. package/dist/pregel/validate.test.js.map +0 -1
  471. package/dist/pregel/write.test.cjs +0 -181
  472. package/dist/pregel/write.test.d.ts +0 -1
  473. package/dist/pregel/write.test.js +0 -179
  474. package/dist/pregel/write.test.js.map +0 -1
  475. package/dist/remote.js.map +0 -1
  476. package/dist/setup/async_local_storage.d.ts +0 -1
  477. package/dist/web.js.map +0 -1
  478. package/index.cjs +0 -1
  479. package/index.d.cts +0 -1
  480. package/index.d.ts +0 -1
  481. package/index.js +0 -1
  482. package/prebuilt.cjs +0 -1
  483. package/prebuilt.d.cts +0 -1
  484. package/prebuilt.d.ts +0 -1
  485. package/prebuilt.js +0 -1
  486. package/pregel.cjs +0 -1
  487. package/pregel.d.cts +0 -1
  488. package/pregel.d.ts +0 -1
  489. package/pregel.js +0 -1
  490. package/remote.cjs +0 -1
  491. package/remote.d.cts +0 -1
  492. package/remote.d.ts +0 -1
  493. package/remote.js +0 -1
  494. package/web.cjs +0 -1
  495. package/web.d.cts +0 -1
  496. package/web.d.ts +0 -1
  497. package/web.js +0 -1
  498. package/zod/schema.cjs +0 -1
  499. package/zod/schema.d.cts +0 -1
  500. package/zod/schema.d.ts +0 -1
  501. package/zod/schema.js +0 -1
  502. package/zod.cjs +0 -1
  503. package/zod.d.cts +0 -1
  504. package/zod.d.ts +0 -1
  505. package/zod.js +0 -1
@@ -1,956 +1,547 @@
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
+ updatedChannels;
75
+ status = "pending";
76
+ tasks = {};
77
+ stream;
78
+ checkpointerPromises = [];
79
+ isNested;
80
+ _checkpointerChainedPromise = Promise.resolve();
81
+ store;
82
+ cache;
83
+ manager;
84
+ interruptAfter;
85
+ interruptBefore;
86
+ toInterrupt = [];
87
+ debug = false;
88
+ triggerToNodes;
89
+ get isResuming() {
90
+ let hasChannelVersions = false;
91
+ if (START in this.checkpoint.channel_versions) hasChannelVersions = true;
92
+ else for (const chan in this.checkpoint.channel_versions) if (Object.prototype.hasOwnProperty.call(this.checkpoint.channel_versions, chan)) {
93
+ hasChannelVersions = true;
94
+ break;
95
+ }
96
+ const configHasResumingFlag = this.config.configurable?.[CONFIG_KEY_RESUMING] !== void 0;
97
+ const configIsResuming = configHasResumingFlag && this.config.configurable?.[CONFIG_KEY_RESUMING];
98
+ const inputIsNullOrUndefined = this.input === null || this.input === void 0;
99
+ const inputIsCommandResuming = isCommand(this.input) && this.input.resume != null;
100
+ const inputIsResuming = this.input === INPUT_RESUMING;
101
+ 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;
102
+ return hasChannelVersions && (configIsResuming || inputIsNullOrUndefined || inputIsCommandResuming || inputIsResuming || runIdMatchesPrevious);
103
+ }
104
+ constructor(params) {
105
+ this.input = params.input;
106
+ this.checkpointer = params.checkpointer;
107
+ if (this.checkpointer !== void 0) this.checkpointerGetNextVersion = this.checkpointer.getNextVersion.bind(this.checkpointer);
108
+ else this.checkpointerGetNextVersion = increment;
109
+ this.checkpoint = params.checkpoint;
110
+ this.checkpointMetadata = params.checkpointMetadata;
111
+ this.checkpointPreviousVersions = params.checkpointPreviousVersions;
112
+ this.channels = params.channels;
113
+ this.checkpointPendingWrites = params.checkpointPendingWrites;
114
+ this.step = params.step;
115
+ this.stop = params.stop;
116
+ this.config = params.config;
117
+ this.checkpointConfig = params.checkpointConfig;
118
+ this.isNested = params.isNested;
119
+ this.manager = params.manager;
120
+ this.outputKeys = params.outputKeys;
121
+ this.streamKeys = params.streamKeys;
122
+ this.nodes = params.nodes;
123
+ this.skipDoneTasks = params.skipDoneTasks;
124
+ this.store = params.store;
125
+ this.cache = params.cache ? new AsyncBatchedCache(params.cache) : void 0;
126
+ this.stream = params.stream;
127
+ this.checkpointNamespace = params.checkpointNamespace;
128
+ this.prevCheckpointConfig = params.prevCheckpointConfig;
129
+ this.interruptAfter = params.interruptAfter;
130
+ this.interruptBefore = params.interruptBefore;
131
+ this.durability = params.durability;
132
+ this.debug = params.debug;
133
+ this.triggerToNodes = params.triggerToNodes;
134
+ }
135
+ static async initialize(params) {
136
+ let { config, stream } = params;
137
+ if (stream !== void 0 && config.configurable?.[CONFIG_KEY_STREAM] !== void 0) stream = createDuplexStream(stream, config.configurable[CONFIG_KEY_STREAM]);
138
+ const skipDoneTasks = config.configurable ? !("checkpoint_id" in config.configurable) : true;
139
+ const scratchpad = config.configurable?.[CONFIG_KEY_SCRATCHPAD];
140
+ if (config.configurable && scratchpad) {
141
+ if (scratchpad.subgraphCounter > 0) config = patchConfigurable(config, { [CONFIG_KEY_CHECKPOINT_NS]: [config.configurable[CONFIG_KEY_CHECKPOINT_NS], scratchpad.subgraphCounter.toString()].join(CHECKPOINT_NAMESPACE_SEPARATOR) });
142
+ scratchpad.subgraphCounter += 1;
143
+ }
144
+ const isNested = CONFIG_KEY_READ in (config.configurable ?? {});
145
+ if (!isNested && config.configurable?.checkpoint_ns !== void 0 && config.configurable?.checkpoint_ns !== "") config = patchConfigurable(config, {
146
+ checkpoint_ns: "",
147
+ checkpoint_id: void 0
148
+ });
149
+ let checkpointConfig = config;
150
+ 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] });
151
+ const checkpointNamespace = config.configurable?.checkpoint_ns?.split(CHECKPOINT_NAMESPACE_SEPARATOR) ?? [];
152
+ const saved = await params.checkpointer?.getTuple(checkpointConfig) ?? {
153
+ config,
154
+ checkpoint: emptyCheckpoint(),
155
+ metadata: {
156
+ source: "input",
157
+ step: -2,
158
+ parents: {}
159
+ },
160
+ pendingWrites: []
161
+ };
162
+ checkpointConfig = {
163
+ ...config,
164
+ ...saved.config,
165
+ configurable: {
166
+ checkpoint_ns: "",
167
+ ...config.configurable,
168
+ ...saved.config.configurable
169
+ }
170
+ };
171
+ const prevCheckpointConfig = saved.parentConfig;
172
+ const checkpoint = copyCheckpoint(saved.checkpoint);
173
+ const checkpointMetadata = { ...saved.metadata };
174
+ const checkpointPendingWrites = saved.pendingWrites ?? [];
175
+ const channels = emptyChannels(params.channelSpecs, checkpoint);
176
+ const step = (checkpointMetadata.step ?? 0) + 1;
177
+ const stop = step + (config.recursionLimit ?? DEFAULT_LOOP_LIMIT) + 1;
178
+ const checkpointPreviousVersions = { ...checkpoint.channel_versions };
179
+ const store = params.store ? new AsyncBatchedStore(params.store) : void 0;
180
+ if (store) await store.start();
181
+ return new PregelLoop({
182
+ input: params.input,
183
+ config,
184
+ checkpointer: params.checkpointer,
185
+ checkpoint,
186
+ checkpointMetadata,
187
+ checkpointConfig,
188
+ prevCheckpointConfig,
189
+ checkpointNamespace,
190
+ channels,
191
+ isNested,
192
+ manager: params.manager,
193
+ skipDoneTasks,
194
+ step,
195
+ stop,
196
+ checkpointPreviousVersions,
197
+ checkpointPendingWrites,
198
+ outputKeys: params.outputKeys ?? [],
199
+ streamKeys: params.streamKeys ?? [],
200
+ nodes: params.nodes,
201
+ stream,
202
+ store,
203
+ cache: params.cache,
204
+ interruptAfter: params.interruptAfter,
205
+ interruptBefore: params.interruptBefore,
206
+ durability: params.durability,
207
+ debug: params.debug,
208
+ triggerToNodes: params.triggerToNodes
209
+ });
210
+ }
211
+ _checkpointerPutAfterPrevious(input) {
212
+ this._checkpointerChainedPromise = this._checkpointerChainedPromise.then(() => {
213
+ return this.checkpointer?.put(input.config, input.checkpoint, input.metadata, input.newVersions);
214
+ });
215
+ this.checkpointerPromises.push(this._checkpointerChainedPromise);
216
+ }
217
+ /**
218
+ * Put writes for a task, to be read by the next tick.
219
+ * @param taskId
220
+ * @param writes
221
+ */
222
+ putWrites(taskId, writes) {
223
+ let writesCopy = writes;
224
+ if (writesCopy.length === 0) return;
225
+ if (writesCopy.every(([key]) => key in WRITES_IDX_MAP)) writesCopy = Array.from(new Map(writesCopy.map((w) => [w[0], w])).values());
226
+ this.checkpointPendingWrites = this.checkpointPendingWrites.filter((w) => w[0] !== taskId);
227
+ for (const [c, v] of writesCopy) this.checkpointPendingWrites.push([
228
+ taskId,
229
+ c,
230
+ v
231
+ ]);
232
+ const config = patchConfigurable(this.checkpointConfig, {
233
+ [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "",
234
+ [CONFIG_KEY_CHECKPOINT_ID]: this.checkpoint.id
235
+ });
236
+ if (this.durability !== "exit" && this.checkpointer != null) this.checkpointerPromises.push(this.checkpointer.putWrites(config, writesCopy, taskId));
237
+ if (this.tasks) this._outputWrites(taskId, writesCopy);
238
+ if (!writes.length || !this.cache || !this.tasks) return;
239
+ const task = this.tasks[taskId];
240
+ if (task == null || task.cache_key == null) return;
241
+ if (writes[0][0] === ERROR || writes[0][0] === INTERRUPT) return;
242
+ this.cache.set([{
243
+ key: [task.cache_key.ns, task.cache_key.key],
244
+ value: task.writes,
245
+ ttl: task.cache_key.ttl
246
+ }]);
247
+ }
248
+ _outputWrites(taskId, writes, cached = false) {
249
+ const task = this.tasks[taskId];
250
+ if (task !== void 0) {
251
+ if (task.config !== void 0 && (task.config.tags ?? []).includes(TAG_HIDDEN)) return;
252
+ if (writes.length > 0) {
253
+ if (writes[0][0] === INTERRUPT) {
254
+ if (task.path?.[0] === PUSH && task.path?.at(-1) === true) return;
255
+ const interruptWrites = writes.filter((w) => w[0] === INTERRUPT).flatMap((w) => w[1]);
256
+ this._emit([["updates", { [INTERRUPT]: interruptWrites }], ["values", { [INTERRUPT]: interruptWrites }]]);
257
+ } else if (writes[0][0] !== ERROR) this._emit(gatherIteratorSync(prefixGenerator(mapOutputUpdates(this.outputKeys, [[task, writes]], cached), "updates")));
258
+ }
259
+ if (!cached) this._emit(gatherIteratorSync(prefixGenerator(mapDebugTaskResults([[task, writes]], this.streamKeys), "tasks")));
260
+ }
261
+ }
262
+ async _matchCachedWrites() {
263
+ if (!this.cache) return [];
264
+ const matched = [];
265
+ const serializeKey = ([ns, key]) => {
266
+ return `ns:${ns.join(",")}|key:${key}`;
267
+ };
268
+ const keys = [];
269
+ const keyMap = {};
270
+ for (const task of Object.values(this.tasks)) if (task.cache_key != null && !task.writes.length) {
271
+ keys.push([task.cache_key.ns, task.cache_key.key]);
272
+ keyMap[serializeKey([task.cache_key.ns, task.cache_key.key])] = task;
273
+ }
274
+ if (keys.length === 0) return [];
275
+ const cache = await this.cache.get(keys);
276
+ for (const { key, value } of cache) {
277
+ const task = keyMap[serializeKey(key)];
278
+ if (task != null) {
279
+ task.writes.push(...value);
280
+ matched.push({
281
+ task,
282
+ result: value
283
+ });
284
+ }
285
+ }
286
+ return matched;
287
+ }
288
+ /**
289
+ * Execute a single iteration of the Pregel loop.
290
+ * Returns true if more iterations are needed.
291
+ * @param params
292
+ */
293
+ async tick(params) {
294
+ if (this.store && !this.store.isRunning) await this.store?.start();
295
+ const { inputKeys = [] } = params;
296
+ if (this.status !== "pending") throw new Error(`Cannot tick when status is no longer "pending". Current status: "${this.status}"`);
297
+ if (![INPUT_DONE, INPUT_RESUMING].includes(this.input)) await this._first(inputKeys);
298
+ else if (this.toInterrupt.length > 0) {
299
+ this.status = "interrupt_before";
300
+ throw new GraphInterrupt();
301
+ } else if (Object.values(this.tasks).every((task) => task.writes.length > 0)) {
302
+ const writes = Object.values(this.tasks).flatMap((t) => t.writes);
303
+ this.updatedChannels = _applyWrites(this.checkpoint, this.channels, Object.values(this.tasks), this.checkpointerGetNextVersion, this.triggerToNodes);
304
+ const valuesOutput = await gatherIterator(prefixGenerator(mapOutputValues(this.outputKeys, writes, this.channels), "values"));
305
+ this._emit(valuesOutput);
306
+ this.checkpointPendingWrites = [];
307
+ await this._putCheckpoint({ source: "loop" });
308
+ if (shouldInterrupt(this.checkpoint, this.interruptAfter, Object.values(this.tasks))) {
309
+ this.status = "interrupt_after";
310
+ throw new GraphInterrupt();
311
+ }
312
+ if (this.config.configurable?.[CONFIG_KEY_RESUMING] !== void 0) delete this.config.configurable?.[CONFIG_KEY_RESUMING];
313
+ } else return false;
314
+ if (this.step > this.stop) {
315
+ this.status = "out_of_steps";
316
+ return false;
317
+ }
318
+ const nextTasks = _prepareNextTasks(this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, this.config, true, {
319
+ step: this.step,
320
+ checkpointer: this.checkpointer,
321
+ isResuming: this.isResuming,
322
+ manager: this.manager,
323
+ store: this.store,
324
+ stream: this.stream,
325
+ triggerToNodes: this.triggerToNodes,
326
+ updatedChannels: this.updatedChannels
327
+ });
328
+ this.tasks = nextTasks;
329
+ 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")));
330
+ if (Object.values(this.tasks).length === 0) {
331
+ this.status = "done";
332
+ return false;
333
+ }
334
+ if (this.skipDoneTasks && this.checkpointPendingWrites.length > 0) {
335
+ for (const [tid, k, v] of this.checkpointPendingWrites) {
336
+ if (k === ERROR || k === INTERRUPT || k === RESUME) continue;
337
+ const task = Object.values(this.tasks).find((t) => t.id === tid);
338
+ if (task) task.writes.push([k, v]);
339
+ }
340
+ for (const task of Object.values(this.tasks)) if (task.writes.length > 0) this._outputWrites(task.id, task.writes, true);
341
+ }
342
+ if (Object.values(this.tasks).every((task) => task.writes.length > 0)) return this.tick({ inputKeys });
343
+ if (shouldInterrupt(this.checkpoint, this.interruptBefore, Object.values(this.tasks))) {
344
+ this.status = "interrupt_before";
345
+ throw new GraphInterrupt();
346
+ }
347
+ const debugOutput = await gatherIterator(prefixGenerator(mapDebugTasks(Object.values(this.tasks)), "tasks"));
348
+ this._emit(debugOutput);
349
+ return true;
350
+ }
351
+ async finishAndHandleError(error) {
352
+ if (this.durability === "exit" && (!this.isNested || typeof error !== "undefined" || this.checkpointNamespace.every((part) => !part.includes(CHECKPOINT_NAMESPACE_END)))) {
353
+ this._putCheckpoint(this.checkpointMetadata);
354
+ this._flushPendingWrites();
355
+ }
356
+ const suppress = this._suppressInterrupt(error);
357
+ if (suppress || error === void 0) this.output = readChannels(this.channels, this.outputKeys);
358
+ if (suppress) {
359
+ if (this.tasks !== void 0 && this.checkpointPendingWrites.length > 0 && Object.values(this.tasks).some((task) => task.writes.length > 0)) {
360
+ this.updatedChannels = _applyWrites(this.checkpoint, this.channels, Object.values(this.tasks), this.checkpointerGetNextVersion, this.triggerToNodes);
361
+ this._emit(gatherIteratorSync(prefixGenerator(mapOutputValues(this.outputKeys, Object.values(this.tasks).flatMap((t) => t.writes), this.channels), "values")));
362
+ }
363
+ if (isGraphInterrupt(error) && !error.interrupts.length) this._emit([["updates", { [INTERRUPT]: [] }], ["values", { [INTERRUPT]: [] }]]);
364
+ }
365
+ return suppress;
366
+ }
367
+ async acceptPush(task, writeIdx, call) {
368
+ if (this.interruptAfter?.length > 0 && shouldInterrupt(this.checkpoint, this.interruptAfter, [task])) {
369
+ this.toInterrupt.push(task);
370
+ return;
371
+ }
372
+ const pushed = _prepareSingleTask([
373
+ PUSH,
374
+ task.path ?? [],
375
+ writeIdx,
376
+ task.id,
377
+ call
378
+ ], this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, task.config ?? {}, true, {
379
+ step: this.step,
380
+ checkpointer: this.checkpointer,
381
+ manager: this.manager,
382
+ store: this.store,
383
+ stream: this.stream
384
+ });
385
+ if (!pushed) return;
386
+ if (this.interruptBefore?.length > 0 && shouldInterrupt(this.checkpoint, this.interruptBefore, [pushed])) {
387
+ this.toInterrupt.push(pushed);
388
+ return;
389
+ }
390
+ this._emit(gatherIteratorSync(prefixGenerator(mapDebugTasks([pushed]), "tasks")));
391
+ if (this.debug) printStepTasks(this.step, [pushed]);
392
+ this.tasks[pushed.id] = pushed;
393
+ if (this.skipDoneTasks) this._matchWrites({ [pushed.id]: pushed });
394
+ const tasks = await this._matchCachedWrites();
395
+ for (const { task: task$1 } of tasks) this._outputWrites(task$1.id, task$1.writes, true);
396
+ return pushed;
397
+ }
398
+ _suppressInterrupt(e) {
399
+ return isGraphInterrupt(e) && !this.isNested;
400
+ }
401
+ async _first(inputKeys) {
402
+ const { configurable } = this.config;
403
+ const scratchpad = configurable?.[CONFIG_KEY_SCRATCHPAD];
404
+ if (scratchpad && scratchpad.nullResume !== void 0) this.putWrites(NULL_TASK_ID, [[RESUME, scratchpad.nullResume]]);
405
+ if (isCommand(this.input)) {
406
+ const hasResume = this.input.resume != null;
407
+ if (this.input.resume != null && typeof this.input.resume === "object" && Object.keys(this.input.resume).every(isXXH3)) {
408
+ this.config.configurable ??= {};
409
+ this.config.configurable[CONFIG_KEY_RESUME_MAP] = this.input.resume;
410
+ }
411
+ if (hasResume && this.checkpointer == null) throw new Error("Cannot use Command(resume=...) without checkpointer");
412
+ const writes = {};
413
+ for (const [tid, key, value] of mapCommand(this.input, this.checkpointPendingWrites)) {
414
+ writes[tid] ??= [];
415
+ writes[tid].push([key, value]);
416
+ }
417
+ if (Object.keys(writes).length === 0) throw new EmptyInputError("Received empty Command input");
418
+ for (const [tid, ws] of Object.entries(writes)) this.putWrites(tid, ws);
419
+ }
420
+ const nullWrites = (this.checkpointPendingWrites ?? []).filter((w) => w[0] === NULL_TASK_ID).map((w) => w.slice(1));
421
+ if (nullWrites.length > 0) _applyWrites(this.checkpoint, this.channels, [{
422
+ name: INPUT,
423
+ writes: nullWrites,
424
+ triggers: []
425
+ }], this.checkpointerGetNextVersion, this.triggerToNodes);
426
+ const isCommandUpdateOrGoto = isCommand(this.input) && nullWrites.length > 0;
427
+ if (this.isResuming || isCommandUpdateOrGoto) {
428
+ for (const channelName in this.channels) {
429
+ if (!Object.prototype.hasOwnProperty.call(this.channels, channelName)) continue;
430
+ if (this.checkpoint.channel_versions[channelName] !== void 0) {
431
+ const version = this.checkpoint.channel_versions[channelName];
432
+ this.checkpoint.versions_seen[INTERRUPT] = {
433
+ ...this.checkpoint.versions_seen[INTERRUPT],
434
+ [channelName]: version
435
+ };
436
+ }
437
+ }
438
+ const valuesOutput = await gatherIterator(prefixGenerator(mapOutputValues(this.outputKeys, true, this.channels), "values"));
439
+ this._emit(valuesOutput);
440
+ }
441
+ if (this.isResuming) this.input = INPUT_RESUMING;
442
+ else if (isCommandUpdateOrGoto) {
443
+ await this._putCheckpoint({ source: "input" });
444
+ this.input = INPUT_DONE;
445
+ } else {
446
+ const inputWrites = await gatherIterator(mapInput(inputKeys, this.input));
447
+ if (inputWrites.length > 0) {
448
+ const discardTasks = _prepareNextTasks(this.checkpoint, this.checkpointPendingWrites, this.nodes, this.channels, this.config, true, { step: this.step });
449
+ this.updatedChannels = _applyWrites(this.checkpoint, this.channels, Object.values(discardTasks).concat([{
450
+ name: INPUT,
451
+ writes: inputWrites,
452
+ triggers: []
453
+ }]), this.checkpointerGetNextVersion, this.triggerToNodes);
454
+ await this._putCheckpoint({ source: "input" });
455
+ this.input = INPUT_DONE;
456
+ } else if (!(CONFIG_KEY_RESUMING in (this.config.configurable ?? {}))) throw new EmptyInputError(`Received no input writes for ${JSON.stringify(inputKeys, null, 2)}`);
457
+ else this.input = INPUT_DONE;
458
+ }
459
+ if (!this.isNested) this.config = patchConfigurable(this.config, { [CONFIG_KEY_RESUMING]: this.isResuming });
460
+ }
461
+ _emit(values) {
462
+ for (const [mode, payload] of values) {
463
+ if (this.stream.modes.has(mode)) this.stream.push([
464
+ this.checkpointNamespace,
465
+ mode,
466
+ payload
467
+ ]);
468
+ if ((mode === "checkpoints" || mode === "tasks") && this.stream.modes.has("debug")) {
469
+ const step = mode === "checkpoints" ? this.step - 1 : this.step;
470
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
471
+ const type = (() => {
472
+ if (mode === "checkpoints") return "checkpoint";
473
+ else if (typeof payload === "object" && payload != null && "result" in payload) return "task_result";
474
+ else return "task";
475
+ })();
476
+ this.stream.push([
477
+ this.checkpointNamespace,
478
+ "debug",
479
+ {
480
+ step,
481
+ type,
482
+ timestamp,
483
+ payload
484
+ }
485
+ ]);
486
+ }
487
+ }
488
+ }
489
+ _putCheckpoint(inputMetadata) {
490
+ const exiting = this.checkpointMetadata === inputMetadata;
491
+ const doCheckpoint = this.checkpointer != null && (this.durability !== "exit" || exiting);
492
+ const storeCheckpoint = (checkpoint) => {
493
+ this.prevCheckpointConfig = this.checkpointConfig?.configurable?.checkpoint_id ? this.checkpointConfig : void 0;
494
+ this.checkpointConfig = patchConfigurable(this.checkpointConfig, { [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "" });
495
+ const channelVersions = { ...this.checkpoint.channel_versions };
496
+ const newVersions = getNewChannelVersions(this.checkpointPreviousVersions, channelVersions);
497
+ this.checkpointPreviousVersions = channelVersions;
498
+ this._checkpointerPutAfterPrevious({
499
+ config: { ...this.checkpointConfig },
500
+ checkpoint: copyCheckpoint(checkpoint),
501
+ metadata: { ...this.checkpointMetadata },
502
+ newVersions
503
+ });
504
+ this.checkpointConfig = {
505
+ ...this.checkpointConfig,
506
+ configurable: {
507
+ ...this.checkpointConfig.configurable,
508
+ checkpoint_id: this.checkpoint.id
509
+ }
510
+ };
511
+ };
512
+ if (!exiting) this.checkpointMetadata = {
513
+ ...inputMetadata,
514
+ step: this.step,
515
+ parents: this.config.configurable?.[CONFIG_KEY_CHECKPOINT_MAP] ?? {}
516
+ };
517
+ this.checkpoint = createCheckpoint(this.checkpoint, doCheckpoint ? this.channels : void 0, this.step, exiting ? { id: this.checkpoint.id } : void 0);
518
+ if (doCheckpoint) storeCheckpoint(this.checkpoint);
519
+ if (!exiting) this.step += 1;
520
+ }
521
+ _flushPendingWrites() {
522
+ if (this.checkpointer == null) return;
523
+ if (this.checkpointPendingWrites.length === 0) return;
524
+ const config = patchConfigurable(this.checkpointConfig, {
525
+ [CONFIG_KEY_CHECKPOINT_NS]: this.config.configurable?.checkpoint_ns ?? "",
526
+ [CONFIG_KEY_CHECKPOINT_ID]: this.checkpoint.id
527
+ });
528
+ const byTask = {};
529
+ for (const [tid, key, value] of this.checkpointPendingWrites) {
530
+ byTask[tid] ??= [];
531
+ byTask[tid].push([key, value]);
532
+ }
533
+ for (const [tid, ws] of Object.entries(byTask)) this.checkpointerPromises.push(this.checkpointer.putWrites(config, ws, tid));
534
+ }
535
+ _matchWrites(tasks) {
536
+ for (const [tid, k, v] of this.checkpointPendingWrites) {
537
+ if (k === ERROR || k === INTERRUPT || k === RESUME) continue;
538
+ const task = Object.values(tasks).find((t) => t.id === tid);
539
+ if (task) task.writes.push([k, v]);
540
+ }
541
+ for (const task of Object.values(tasks)) if (task.writes.length > 0) this._outputWrites(task.id, task.writes, true);
542
+ }
543
+ };
544
+
545
+ //#endregion
546
+ export { PregelLoop };
956
547
  //# sourceMappingURL=loop.js.map