@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,795 +1,495 @@
1
- /* eslint-disable @typescript-eslint/no-use-before-define */
2
- import { _coerceToRunnable, Runnable } from "@langchain/core/runnables";
3
- import { interopParse, interopZodObjectPartial, isInteropZodObject, } from "@langchain/core/utils/types";
1
+ import { InvalidUpdateError, ParentCommand } from "../errors.js";
4
2
  import { isBaseChannel } from "../channels/base.js";
5
- import { CompiledGraph, Graph, Branch, } from "./graph.js";
6
- import { ChannelWrite, PASSTHROUGH, } from "../pregel/write.js";
7
- import { ChannelRead, PregelNode } from "../pregel/read.js";
8
- import { NamedBarrierValue, NamedBarrierValueAfterFinish, } from "../channels/named_barrier_value.js";
9
- import { EphemeralValue } from "../channels/ephemeral_value.js";
3
+ import { LastValueAfterFinish } from "../channels/last_value.js";
4
+ import { getChannel } from "./annotation.js";
5
+ import { CHECKPOINT_NAMESPACE_END, CHECKPOINT_NAMESPACE_SEPARATOR, Command, END, SELF, START, TAG_HIDDEN, _isSend, isCommand, isInterrupted } from "../constants.js";
10
6
  import { RunnableCallable } from "../utils.js";
11
- import { isCommand, _isSend, CHECKPOINT_NAMESPACE_END, CHECKPOINT_NAMESPACE_SEPARATOR, Command, END, SELF, START, TAG_HIDDEN, } from "../constants.js";
12
- import { InvalidUpdateError, ParentCommand } from "../errors.js";
13
- import { getChannel, } from "./annotation.js";
7
+ import { ChannelWrite, PASSTHROUGH } from "../pregel/write.js";
8
+ import { ChannelRead, PregelNode } from "../pregel/read.js";
14
9
  import { isPregelLike } from "../pregel/utils/subgraph.js";
15
- import { LastValueAfterFinish } from "../channels/last_value.js";
16
- import { schemaMetaRegistry, } from "./zod/meta.js";
10
+ import { EphemeralValue } from "../channels/ephemeral_value.js";
11
+ import { Branch, CompiledGraph, Graph } from "./graph.js";
12
+ import { NamedBarrierValue, NamedBarrierValueAfterFinish } from "../channels/named_barrier_value.js";
13
+ import { schemaMetaRegistry } from "./zod/meta.js";
14
+ import { Runnable, _coerceToRunnable } from "@langchain/core/runnables";
15
+ import { interopParse, interopZodObjectPartial, isInteropZodObject } from "@langchain/core/utils/types";
16
+
17
+ //#region src/graph/state.ts
17
18
  const ROOT = "__root__";
18
19
  const PartialStateSchema = Symbol.for("langgraph.state.partial");
19
20
  /**
20
- * A graph whose nodes communicate by reading and writing to a shared state.
21
- * Each node takes a defined `State` as input and returns a `Partial<State>`.
22
- *
23
- * Each state key can optionally be annotated with a reducer function that
24
- * will be used to aggregate the values of that key received from multiple nodes.
25
- * The signature of a reducer function is (left: Value, right: UpdateValue) => Value.
26
- *
27
- * See {@link Annotation} for more on defining state.
28
- *
29
- * After adding nodes and edges to your graph, you must call `.compile()` on it before
30
- * you can use it.
31
- *
32
- * @example
33
- * ```ts
34
- * import {
35
- * type BaseMessage,
36
- * AIMessage,
37
- * HumanMessage,
38
- * } from "@langchain/core/messages";
39
- * import { StateGraph, Annotation } from "@langchain/langgraph";
40
- *
41
- * // Define a state with a single key named "messages" that will
42
- * // combine a returned BaseMessage or arrays of BaseMessages
43
- * const StateAnnotation = Annotation.Root({
44
- * sentiment: Annotation<string>,
45
- * messages: Annotation<BaseMessage[]>({
46
- * reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
47
- * if (Array.isArray(right)) {
48
- * return left.concat(right);
49
- * }
50
- * return left.concat([right]);
51
- * },
52
- * default: () => [],
53
- * }),
54
- * });
55
- *
56
- * const graphBuilder = new StateGraph(StateAnnotation);
57
- *
58
- * // A node in the graph that returns an object with a "messages" key
59
- * // will update the state by combining the existing value with the returned one.
60
- * const myNode = (state: typeof StateAnnotation.State) => {
61
- * return {
62
- * messages: [new AIMessage("Some new response")],
63
- * sentiment: "positive",
64
- * };
65
- * };
66
- *
67
- * const graph = graphBuilder
68
- * .addNode("myNode", myNode)
69
- * .addEdge("__start__", "myNode")
70
- * .addEdge("myNode", "__end__")
71
- * .compile();
72
- *
73
- * await graph.invoke({ messages: [new HumanMessage("how are you?")] });
74
- *
75
- * // {
76
- * // messages: [HumanMessage("how are you?"), AIMessage("Some new response")],
77
- * // sentiment: "positive",
78
- * // }
79
- * ```
80
- */
81
- export class StateGraph extends Graph {
82
- constructor(fields, contextSchema) {
83
- super();
84
- Object.defineProperty(this, "channels", {
85
- enumerable: true,
86
- configurable: true,
87
- writable: true,
88
- value: {}
89
- });
90
- // TODO: this doesn't dedupe edges as in py, so worth fixing at some point
91
- Object.defineProperty(this, "waitingEdges", {
92
- enumerable: true,
93
- configurable: true,
94
- writable: true,
95
- value: new Set()
96
- });
97
- /** @internal */
98
- Object.defineProperty(this, "_schemaDefinition", {
99
- enumerable: true,
100
- configurable: true,
101
- writable: true,
102
- value: void 0
103
- });
104
- /** @internal */
105
- Object.defineProperty(this, "_schemaRuntimeDefinition", {
106
- enumerable: true,
107
- configurable: true,
108
- writable: true,
109
- value: void 0
110
- });
111
- /** @internal */
112
- Object.defineProperty(this, "_inputDefinition", {
113
- enumerable: true,
114
- configurable: true,
115
- writable: true,
116
- value: void 0
117
- });
118
- /** @internal */
119
- Object.defineProperty(this, "_inputRuntimeDefinition", {
120
- enumerable: true,
121
- configurable: true,
122
- writable: true,
123
- value: void 0
124
- });
125
- /** @internal */
126
- Object.defineProperty(this, "_outputDefinition", {
127
- enumerable: true,
128
- configurable: true,
129
- writable: true,
130
- value: void 0
131
- });
132
- /** @internal */
133
- Object.defineProperty(this, "_outputRuntimeDefinition", {
134
- enumerable: true,
135
- configurable: true,
136
- writable: true,
137
- value: void 0
138
- });
139
- /**
140
- * Map schemas to managed values
141
- * @internal
142
- */
143
- Object.defineProperty(this, "_schemaDefinitions", {
144
- enumerable: true,
145
- configurable: true,
146
- writable: true,
147
- value: new Map()
148
- });
149
- /** @internal */
150
- Object.defineProperty(this, "_metaRegistry", {
151
- enumerable: true,
152
- configurable: true,
153
- writable: true,
154
- value: schemaMetaRegistry
155
- });
156
- /** @internal Used only for typing. */
157
- Object.defineProperty(this, "_configSchema", {
158
- enumerable: true,
159
- configurable: true,
160
- writable: true,
161
- value: void 0
162
- });
163
- /** @internal */
164
- Object.defineProperty(this, "_configRuntimeSchema", {
165
- enumerable: true,
166
- configurable: true,
167
- writable: true,
168
- value: void 0
169
- });
170
- if (isZodStateGraphArgsWithStateSchema(fields)) {
171
- const stateDef = this._metaRegistry.getChannelsForSchema(fields.state);
172
- const inputDef = fields.input != null
173
- ? this._metaRegistry.getChannelsForSchema(fields.input)
174
- : stateDef;
175
- const outputDef = fields.output != null
176
- ? this._metaRegistry.getChannelsForSchema(fields.output)
177
- : stateDef;
178
- this._schemaDefinition = stateDef;
179
- this._schemaRuntimeDefinition = fields.state;
180
- this._inputDefinition = inputDef;
181
- this._inputRuntimeDefinition = fields.input ?? PartialStateSchema;
182
- this._outputDefinition = outputDef;
183
- this._outputRuntimeDefinition = fields.output ?? fields.state;
184
- }
185
- else if (isInteropZodObject(fields)) {
186
- const stateDef = this._metaRegistry.getChannelsForSchema(fields);
187
- this._schemaDefinition = stateDef;
188
- this._schemaRuntimeDefinition = fields;
189
- this._inputDefinition = stateDef;
190
- this._inputRuntimeDefinition = PartialStateSchema;
191
- this._outputDefinition = stateDef;
192
- this._outputRuntimeDefinition = fields;
193
- }
194
- else if (isStateGraphArgsWithInputOutputSchemas(fields)) {
195
- this._schemaDefinition = fields.input.spec;
196
- this._inputDefinition = fields.input.spec;
197
- this._outputDefinition = fields.output.spec;
198
- }
199
- else if (isStateGraphArgsWithStateSchema(fields)) {
200
- this._schemaDefinition = fields.stateSchema.spec;
201
- this._inputDefinition = (fields.input?.spec ??
202
- this._schemaDefinition);
203
- this._outputDefinition = (fields.output?.spec ??
204
- this._schemaDefinition);
205
- }
206
- else if (isStateDefinition(fields) || isAnnotationRoot(fields)) {
207
- const spec = isAnnotationRoot(fields) ? fields.spec : fields;
208
- this._schemaDefinition = spec;
209
- }
210
- else if (isStateGraphArgs(fields)) {
211
- const spec = _getChannels(fields.channels);
212
- this._schemaDefinition = spec;
213
- }
214
- else {
215
- throw new Error("Invalid StateGraph input. Make sure to pass a valid Annotation.Root or Zod schema.");
216
- }
217
- this._inputDefinition ??= this._schemaDefinition;
218
- this._outputDefinition ??= this._schemaDefinition;
219
- this._addSchema(this._schemaDefinition);
220
- this._addSchema(this._inputDefinition);
221
- this._addSchema(this._outputDefinition);
222
- if (isInteropZodObject(contextSchema)) {
223
- this._configRuntimeSchema = contextSchema;
224
- }
225
- }
226
- get allEdges() {
227
- return new Set([
228
- ...this.edges,
229
- ...Array.from(this.waitingEdges).flatMap(([starts, end]) => starts.map((start) => [start, end])),
230
- ]);
231
- }
232
- _addSchema(stateDefinition) {
233
- if (this._schemaDefinitions.has(stateDefinition)) {
234
- return;
235
- }
236
- // TODO: Support managed values
237
- this._schemaDefinitions.set(stateDefinition, stateDefinition);
238
- for (const [key, val] of Object.entries(stateDefinition)) {
239
- let channel;
240
- if (typeof val === "function") {
241
- channel = val();
242
- }
243
- else {
244
- channel = val;
245
- }
246
- if (this.channels[key] !== undefined) {
247
- if (this.channels[key] !== channel) {
248
- if (channel.lc_graph_name !== "LastValue") {
249
- throw new Error(`Channel "${key}" already exists with a different type.`);
250
- }
251
- }
252
- }
253
- else {
254
- this.channels[key] = channel;
255
- }
256
- }
257
- }
258
- addNode(...args) {
259
- function isMultipleNodes(args) {
260
- return args.length >= 1 && typeof args[0] !== "string";
261
- }
262
- const nodes = (isMultipleNodes(args) // eslint-disable-line no-nested-ternary
263
- ? Array.isArray(args[0])
264
- ? args[0]
265
- : Object.entries(args[0]).map(([key, action]) => [
266
- key,
267
- action,
268
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
269
- action[Symbol.for("langgraph.state.node")] ?? undefined,
270
- ])
271
- : [[args[0], args[1], args[2]]]);
272
- if (nodes.length === 0) {
273
- throw new Error("No nodes provided in `addNode`");
274
- }
275
- for (const [key, action, options] of nodes) {
276
- if (key in this.channels) {
277
- throw new Error(`${key} is already being used as a state attribute (a.k.a. a channel), cannot also be used as a node name.`);
278
- }
279
- for (const reservedChar of [
280
- CHECKPOINT_NAMESPACE_SEPARATOR,
281
- CHECKPOINT_NAMESPACE_END,
282
- ]) {
283
- if (key.includes(reservedChar)) {
284
- throw new Error(`"${reservedChar}" is a reserved character and is not allowed in node names.`);
285
- }
286
- }
287
- this.warnIfCompiled(`Adding a node to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
288
- if (key in this.nodes) {
289
- throw new Error(`Node \`${key}\` already present.`);
290
- }
291
- if (key === END || key === START) {
292
- throw new Error(`Node \`${key}\` is reserved.`);
293
- }
294
- let inputSpec = this._schemaDefinition;
295
- if (options?.input !== undefined) {
296
- if (isInteropZodObject(options.input)) {
297
- inputSpec = this._metaRegistry.getChannelsForSchema(options.input);
298
- }
299
- else if (options.input.spec !== undefined) {
300
- inputSpec = options.input.spec;
301
- }
302
- }
303
- if (inputSpec !== undefined) {
304
- this._addSchema(inputSpec);
305
- }
306
- let runnable;
307
- if (Runnable.isRunnable(action)) {
308
- runnable = action;
309
- }
310
- else if (typeof action === "function") {
311
- runnable = new RunnableCallable({
312
- func: action,
313
- name: key,
314
- trace: false,
315
- });
316
- }
317
- else {
318
- runnable = _coerceToRunnable(action);
319
- }
320
- let cachePolicy = options?.cachePolicy;
321
- if (typeof cachePolicy === "boolean") {
322
- cachePolicy = cachePolicy ? {} : undefined;
323
- }
324
- const nodeSpec = {
325
- runnable: runnable,
326
- retryPolicy: options?.retryPolicy,
327
- cachePolicy,
328
- metadata: options?.metadata,
329
- input: inputSpec ?? this._schemaDefinition,
330
- subgraphs: isPregelLike(runnable)
331
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
332
- [runnable]
333
- : options?.subgraphs,
334
- ends: options?.ends,
335
- defer: options?.defer,
336
- };
337
- this.nodes[key] = nodeSpec;
338
- }
339
- return this;
340
- }
341
- addEdge(startKey, endKey) {
342
- if (typeof startKey === "string") {
343
- return super.addEdge(startKey, endKey);
344
- }
345
- if (this.compiled) {
346
- console.warn("Adding an edge to a graph that has already been compiled. This will " +
347
- "not be reflected in the compiled graph.");
348
- }
349
- for (const start of startKey) {
350
- if (start === END) {
351
- throw new Error("END cannot be a start node");
352
- }
353
- if (!Object.keys(this.nodes).some((node) => node === start)) {
354
- throw new Error(`Need to add a node named "${start}" first`);
355
- }
356
- }
357
- if (endKey === END) {
358
- throw new Error("END cannot be an end node");
359
- }
360
- if (!Object.keys(this.nodes).some((node) => node === endKey)) {
361
- throw new Error(`Need to add a node named "${endKey}" first`);
362
- }
363
- this.waitingEdges.add([startKey, endKey]);
364
- return this;
365
- }
366
- addSequence(nodes) {
367
- const parsedNodes = Array.isArray(nodes)
368
- ? nodes
369
- : Object.entries(nodes).map(([key, action]) => [
370
- key,
371
- action,
372
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
373
- action[Symbol.for("langgraph.state.node")] ?? undefined,
374
- ]);
375
- if (parsedNodes.length === 0) {
376
- throw new Error("Sequence requires at least one node.");
377
- }
378
- let previousNode;
379
- for (const [key, action, options] of parsedNodes) {
380
- if (key in this.nodes) {
381
- throw new Error(`Node names must be unique: node with the name "${key}" already exists.`);
382
- }
383
- const validKey = key;
384
- this.addNode(validKey, action, options);
385
- if (previousNode != null) {
386
- this.addEdge(previousNode, validKey);
387
- }
388
- previousNode = validKey;
389
- }
390
- return this;
391
- }
392
- compile({ checkpointer, store, cache, interruptBefore, interruptAfter, name, description, } = {}) {
393
- // validate the graph
394
- this.validate([
395
- ...(Array.isArray(interruptBefore) ? interruptBefore : []),
396
- ...(Array.isArray(interruptAfter) ? interruptAfter : []),
397
- ]);
398
- // prepare output channels
399
- const outputKeys = Object.keys(this._schemaDefinitions.get(this._outputDefinition));
400
- const outputChannels = outputKeys.length === 1 && outputKeys[0] === ROOT ? ROOT : outputKeys;
401
- const streamKeys = Object.keys(this.channels);
402
- const streamChannels = streamKeys.length === 1 && streamKeys[0] === ROOT ? ROOT : streamKeys;
403
- // create empty compiled graph
404
- const compiled = new CompiledStateGraph({
405
- builder: this,
406
- checkpointer,
407
- interruptAfter,
408
- interruptBefore,
409
- autoValidate: false,
410
- nodes: {},
411
- channels: {
412
- ...this.channels,
413
- [START]: new EphemeralValue(),
414
- },
415
- inputChannels: START,
416
- outputChannels,
417
- streamChannels,
418
- streamMode: "updates",
419
- store,
420
- cache,
421
- name,
422
- description,
423
- });
424
- // attach nodes, edges and branches
425
- compiled.attachNode(START);
426
- for (const [key, node] of Object.entries(this.nodes)) {
427
- compiled.attachNode(key, node);
428
- }
429
- compiled.attachBranch(START, SELF, _getControlBranch(), {
430
- withReader: false,
431
- });
432
- for (const [key] of Object.entries(this.nodes)) {
433
- compiled.attachBranch(key, SELF, _getControlBranch(), {
434
- withReader: false,
435
- });
436
- }
437
- for (const [start, end] of this.edges) {
438
- compiled.attachEdge(start, end);
439
- }
440
- for (const [starts, end] of this.waitingEdges) {
441
- compiled.attachEdge(starts, end);
442
- }
443
- for (const [start, branches] of Object.entries(this.branches)) {
444
- for (const [name, branch] of Object.entries(branches)) {
445
- compiled.attachBranch(start, name, branch);
446
- }
447
- }
448
- return compiled.validate();
449
- }
450
- }
21
+ * A graph whose nodes communicate by reading and writing to a shared state.
22
+ * Each node takes a defined `State` as input and returns a `Partial<State>`.
23
+ *
24
+ * Each state key can optionally be annotated with a reducer function that
25
+ * will be used to aggregate the values of that key received from multiple nodes.
26
+ * The signature of a reducer function is (left: Value, right: UpdateValue) => Value.
27
+ *
28
+ * See {@link Annotation} for more on defining state.
29
+ *
30
+ * After adding nodes and edges to your graph, you must call `.compile()` on it before
31
+ * you can use it.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import {
36
+ * type BaseMessage,
37
+ * AIMessage,
38
+ * HumanMessage,
39
+ * } from "@langchain/core/messages";
40
+ * import { StateGraph, Annotation } from "@langchain/langgraph";
41
+ *
42
+ * // Define a state with a single key named "messages" that will
43
+ * // combine a returned BaseMessage or arrays of BaseMessages
44
+ * const StateAnnotation = Annotation.Root({
45
+ * sentiment: Annotation<string>,
46
+ * messages: Annotation<BaseMessage[]>({
47
+ * reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
48
+ * if (Array.isArray(right)) {
49
+ * return left.concat(right);
50
+ * }
51
+ * return left.concat([right]);
52
+ * },
53
+ * default: () => [],
54
+ * }),
55
+ * });
56
+ *
57
+ * const graphBuilder = new StateGraph(StateAnnotation);
58
+ *
59
+ * // A node in the graph that returns an object with a "messages" key
60
+ * // will update the state by combining the existing value with the returned one.
61
+ * const myNode = (state: typeof StateAnnotation.State) => {
62
+ * return {
63
+ * messages: [new AIMessage("Some new response")],
64
+ * sentiment: "positive",
65
+ * };
66
+ * };
67
+ *
68
+ * const graph = graphBuilder
69
+ * .addNode("myNode", myNode)
70
+ * .addEdge("__start__", "myNode")
71
+ * .addEdge("myNode", "__end__")
72
+ * .compile();
73
+ *
74
+ * await graph.invoke({ messages: [new HumanMessage("how are you?")] });
75
+ *
76
+ * // {
77
+ * // messages: [HumanMessage("how are you?"), AIMessage("Some new response")],
78
+ * // sentiment: "positive",
79
+ * // }
80
+ * ```
81
+ */
82
+ var StateGraph = class extends Graph {
83
+ channels = {};
84
+ waitingEdges = /* @__PURE__ */ new Set();
85
+ /** @internal */
86
+ _schemaDefinition;
87
+ /** @internal */
88
+ _schemaRuntimeDefinition;
89
+ /** @internal */
90
+ _inputDefinition;
91
+ /** @internal */
92
+ _inputRuntimeDefinition;
93
+ /** @internal */
94
+ _outputDefinition;
95
+ /** @internal */
96
+ _outputRuntimeDefinition;
97
+ /**
98
+ * Map schemas to managed values
99
+ * @internal
100
+ */
101
+ _schemaDefinitions = /* @__PURE__ */ new Map();
102
+ /** @internal */
103
+ _metaRegistry = schemaMetaRegistry;
104
+ /** @internal Used only for typing. */
105
+ _configSchema;
106
+ /** @internal */
107
+ _configRuntimeSchema;
108
+ /** @internal */
109
+ _interrupt;
110
+ /** @internal */
111
+ _writer;
112
+ constructor(fields, contextSchema) {
113
+ super();
114
+ if (isZodStateGraphArgsWithStateSchema(fields)) {
115
+ const stateDef = this._metaRegistry.getChannelsForSchema(fields.state);
116
+ const inputDef = fields.input != null ? this._metaRegistry.getChannelsForSchema(fields.input) : stateDef;
117
+ const outputDef = fields.output != null ? this._metaRegistry.getChannelsForSchema(fields.output) : stateDef;
118
+ this._schemaDefinition = stateDef;
119
+ this._schemaRuntimeDefinition = fields.state;
120
+ this._inputDefinition = inputDef;
121
+ this._inputRuntimeDefinition = fields.input ?? PartialStateSchema;
122
+ this._outputDefinition = outputDef;
123
+ this._outputRuntimeDefinition = fields.output ?? fields.state;
124
+ } else if (isInteropZodObject(fields)) {
125
+ const stateDef = this._metaRegistry.getChannelsForSchema(fields);
126
+ this._schemaDefinition = stateDef;
127
+ this._schemaRuntimeDefinition = fields;
128
+ this._inputDefinition = stateDef;
129
+ this._inputRuntimeDefinition = PartialStateSchema;
130
+ this._outputDefinition = stateDef;
131
+ this._outputRuntimeDefinition = fields;
132
+ } else if (isStateGraphArgsWithInputOutputSchemas(fields)) {
133
+ this._schemaDefinition = fields.input.spec;
134
+ this._inputDefinition = fields.input.spec;
135
+ this._outputDefinition = fields.output.spec;
136
+ } else if (isStateGraphArgsWithStateSchema(fields)) {
137
+ this._schemaDefinition = fields.stateSchema.spec;
138
+ this._inputDefinition = fields.input?.spec ?? this._schemaDefinition;
139
+ this._outputDefinition = fields.output?.spec ?? this._schemaDefinition;
140
+ } else if (isStateDefinition(fields) || isAnnotationRoot(fields)) {
141
+ const spec = isAnnotationRoot(fields) ? fields.spec : fields;
142
+ this._schemaDefinition = spec;
143
+ } else if (isStateGraphArgs(fields)) {
144
+ const spec = _getChannels(fields.channels);
145
+ this._schemaDefinition = spec;
146
+ } else throw new Error("Invalid StateGraph input. Make sure to pass a valid Annotation.Root or Zod schema.");
147
+ this._inputDefinition ??= this._schemaDefinition;
148
+ this._outputDefinition ??= this._schemaDefinition;
149
+ this._addSchema(this._schemaDefinition);
150
+ this._addSchema(this._inputDefinition);
151
+ this._addSchema(this._outputDefinition);
152
+ function isOptions(options) {
153
+ return typeof options === "object" && options != null && !("spec" in options) && !isInteropZodObject(options);
154
+ }
155
+ if (isOptions(contextSchema)) {
156
+ if (isInteropZodObject(contextSchema.context)) this._configRuntimeSchema = contextSchema.context;
157
+ } else if (isInteropZodObject(contextSchema)) this._configRuntimeSchema = contextSchema;
158
+ }
159
+ get allEdges() {
160
+ return new Set([...this.edges, ...Array.from(this.waitingEdges).flatMap(([starts, end]) => starts.map((start) => [start, end]))]);
161
+ }
162
+ _addSchema(stateDefinition) {
163
+ if (this._schemaDefinitions.has(stateDefinition)) return;
164
+ this._schemaDefinitions.set(stateDefinition, stateDefinition);
165
+ for (const [key, val] of Object.entries(stateDefinition)) {
166
+ let channel;
167
+ if (typeof val === "function") channel = val();
168
+ else channel = val;
169
+ if (this.channels[key] !== void 0) {
170
+ if (this.channels[key] !== channel) {
171
+ if (channel.lc_graph_name !== "LastValue") throw new Error(`Channel "${key}" already exists with a different type.`);
172
+ }
173
+ } else this.channels[key] = channel;
174
+ }
175
+ }
176
+ addNode(...args) {
177
+ function isMultipleNodes(args$1) {
178
+ return args$1.length >= 1 && typeof args$1[0] !== "string";
179
+ }
180
+ const nodes = isMultipleNodes(args) ? Array.isArray(args[0]) ? args[0] : Object.entries(args[0]).map(([key, action]) => [key, action]) : [[
181
+ args[0],
182
+ args[1],
183
+ args[2]
184
+ ]];
185
+ if (nodes.length === 0) throw new Error("No nodes provided in `addNode`");
186
+ for (const [key, action, options] of nodes) {
187
+ if (key in this.channels) throw new Error(`${key} is already being used as a state attribute (a.k.a. a channel), cannot also be used as a node name.`);
188
+ for (const reservedChar of [CHECKPOINT_NAMESPACE_SEPARATOR, CHECKPOINT_NAMESPACE_END]) if (key.includes(reservedChar)) throw new Error(`"${reservedChar}" is a reserved character and is not allowed in node names.`);
189
+ this.warnIfCompiled(`Adding a node to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
190
+ if (key in this.nodes) throw new Error(`Node \`${key}\` already present.`);
191
+ if (key === END || key === START) throw new Error(`Node \`${key}\` is reserved.`);
192
+ let inputSpec = this._schemaDefinition;
193
+ if (options?.input !== void 0) {
194
+ if (isInteropZodObject(options.input)) inputSpec = this._metaRegistry.getChannelsForSchema(options.input);
195
+ else if (options.input.spec !== void 0) inputSpec = options.input.spec;
196
+ }
197
+ if (inputSpec !== void 0) this._addSchema(inputSpec);
198
+ let runnable;
199
+ if (Runnable.isRunnable(action)) runnable = action;
200
+ else if (typeof action === "function") runnable = new RunnableCallable({
201
+ func: action,
202
+ name: key,
203
+ trace: false
204
+ });
205
+ else runnable = _coerceToRunnable(action);
206
+ let cachePolicy = options?.cachePolicy;
207
+ if (typeof cachePolicy === "boolean") cachePolicy = cachePolicy ? {} : void 0;
208
+ const nodeSpec = {
209
+ runnable,
210
+ retryPolicy: options?.retryPolicy,
211
+ cachePolicy,
212
+ metadata: options?.metadata,
213
+ input: inputSpec ?? this._schemaDefinition,
214
+ subgraphs: isPregelLike(runnable) ? [runnable] : options?.subgraphs,
215
+ ends: options?.ends,
216
+ defer: options?.defer
217
+ };
218
+ this.nodes[key] = nodeSpec;
219
+ }
220
+ return this;
221
+ }
222
+ addEdge(startKey, endKey) {
223
+ if (typeof startKey === "string") return super.addEdge(startKey, endKey);
224
+ if (this.compiled) console.warn("Adding an edge to a graph that has already been compiled. This will not be reflected in the compiled graph.");
225
+ for (const start of startKey) {
226
+ if (start === END) throw new Error("END cannot be a start node");
227
+ if (!Object.keys(this.nodes).some((node) => node === start)) throw new Error(`Need to add a node named "${start}" first`);
228
+ }
229
+ if (endKey === END) throw new Error("END cannot be an end node");
230
+ if (!Object.keys(this.nodes).some((node) => node === endKey)) throw new Error(`Need to add a node named "${endKey}" first`);
231
+ this.waitingEdges.add([startKey, endKey]);
232
+ return this;
233
+ }
234
+ addSequence(nodes) {
235
+ const parsedNodes = Array.isArray(nodes) ? nodes : Object.entries(nodes);
236
+ if (parsedNodes.length === 0) throw new Error("Sequence requires at least one node.");
237
+ let previousNode;
238
+ for (const [key, action, options] of parsedNodes) {
239
+ if (key in this.nodes) throw new Error(`Node names must be unique: node with the name "${key}" already exists.`);
240
+ const validKey = key;
241
+ this.addNode(validKey, action, options);
242
+ if (previousNode != null) this.addEdge(previousNode, validKey);
243
+ previousNode = validKey;
244
+ }
245
+ return this;
246
+ }
247
+ compile({ checkpointer, store, cache, interruptBefore, interruptAfter, name, description } = {}) {
248
+ this.validate([...Array.isArray(interruptBefore) ? interruptBefore : [], ...Array.isArray(interruptAfter) ? interruptAfter : []]);
249
+ const outputKeys = Object.keys(this._schemaDefinitions.get(this._outputDefinition));
250
+ const outputChannels = outputKeys.length === 1 && outputKeys[0] === ROOT ? ROOT : outputKeys;
251
+ const streamKeys = Object.keys(this.channels);
252
+ const streamChannels = streamKeys.length === 1 && streamKeys[0] === ROOT ? ROOT : streamKeys;
253
+ const compiled = new CompiledStateGraph({
254
+ builder: this,
255
+ checkpointer,
256
+ interruptAfter,
257
+ interruptBefore,
258
+ autoValidate: false,
259
+ nodes: {},
260
+ channels: {
261
+ ...this.channels,
262
+ [START]: new EphemeralValue()
263
+ },
264
+ inputChannels: START,
265
+ outputChannels,
266
+ streamChannels,
267
+ streamMode: "updates",
268
+ store,
269
+ cache,
270
+ name,
271
+ description
272
+ });
273
+ compiled.attachNode(START);
274
+ for (const [key, node] of Object.entries(this.nodes)) compiled.attachNode(key, node);
275
+ compiled.attachBranch(START, SELF, _getControlBranch(), { withReader: false });
276
+ for (const [key] of Object.entries(this.nodes)) compiled.attachBranch(key, SELF, _getControlBranch(), { withReader: false });
277
+ for (const [start, end] of this.edges) compiled.attachEdge(start, end);
278
+ for (const [starts, end] of this.waitingEdges) compiled.attachEdge(starts, end);
279
+ for (const [start, branches] of Object.entries(this.branches)) for (const [name$1, branch] of Object.entries(branches)) compiled.attachBranch(start, name$1, branch);
280
+ return compiled.validate();
281
+ }
282
+ };
451
283
  function _getChannels(schema) {
452
- const channels = {};
453
- for (const [name, val] of Object.entries(schema)) {
454
- if (name === ROOT) {
455
- channels[name] = getChannel(val);
456
- }
457
- else {
458
- const key = name;
459
- channels[name] = getChannel(val);
460
- }
461
- }
462
- return channels;
284
+ const channels = {};
285
+ for (const [name, val] of Object.entries(schema)) if (name === ROOT) channels[name] = getChannel(val);
286
+ else channels[name] = getChannel(val);
287
+ return channels;
463
288
  }
464
289
  /**
465
- * Final result from building and compiling a {@link StateGraph}.
466
- * Should not be instantiated directly, only using the StateGraph `.compile()`
467
- * instance method.
468
- */
469
- export class CompiledStateGraph extends CompiledGraph {
470
- constructor({ description, ...rest }) {
471
- super(rest);
472
- /**
473
- * The description of the compiled graph.
474
- * This is used by the supervisor agent to describe the handoff to the agent.
475
- */
476
- Object.defineProperty(this, "description", {
477
- enumerable: true,
478
- configurable: true,
479
- writable: true,
480
- value: void 0
481
- });
482
- /** @internal */
483
- Object.defineProperty(this, "_metaRegistry", {
484
- enumerable: true,
485
- configurable: true,
486
- writable: true,
487
- value: schemaMetaRegistry
488
- });
489
- this.description = description;
490
- }
491
- attachNode(key, node) {
492
- let outputKeys;
493
- if (key === START) {
494
- // Get input schema keys excluding managed values
495
- outputKeys = Object.entries(this.builder._schemaDefinitions.get(this.builder._inputDefinition)).map(([k]) => k);
496
- }
497
- else {
498
- outputKeys = Object.keys(this.builder.channels);
499
- }
500
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
501
- function _getRoot(input) {
502
- if (isCommand(input)) {
503
- if (input.graph === Command.PARENT) {
504
- return null;
505
- }
506
- return input._updateAsTuples();
507
- }
508
- else if (Array.isArray(input) &&
509
- input.length > 0 &&
510
- input.some((i) => isCommand(i))) {
511
- const updates = [];
512
- for (const i of input) {
513
- if (isCommand(i)) {
514
- if (i.graph === Command.PARENT) {
515
- continue;
516
- }
517
- updates.push(...i._updateAsTuples());
518
- }
519
- else {
520
- updates.push([ROOT, i]);
521
- }
522
- }
523
- return updates;
524
- }
525
- else if (input != null) {
526
- return [[ROOT, input]];
527
- }
528
- return null;
529
- }
530
- // to avoid name collision below
531
- const nodeKey = key;
532
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
533
- function _getUpdates(input) {
534
- if (!input) {
535
- return null;
536
- }
537
- else if (isCommand(input)) {
538
- if (input.graph === Command.PARENT) {
539
- return null;
540
- }
541
- return input._updateAsTuples().filter(([k]) => outputKeys.includes(k));
542
- }
543
- else if (Array.isArray(input) &&
544
- input.length > 0 &&
545
- input.some(isCommand)) {
546
- const updates = [];
547
- for (const item of input) {
548
- if (isCommand(item)) {
549
- if (item.graph === Command.PARENT) {
550
- continue;
551
- }
552
- updates.push(...item._updateAsTuples().filter(([k]) => outputKeys.includes(k)));
553
- }
554
- else {
555
- const itemUpdates = _getUpdates(item);
556
- if (itemUpdates) {
557
- updates.push(...(itemUpdates ?? []));
558
- }
559
- }
560
- }
561
- return updates;
562
- }
563
- else if (typeof input === "object" && !Array.isArray(input)) {
564
- return Object.entries(input).filter(([k]) => outputKeys.includes(k));
565
- }
566
- else {
567
- const typeofInput = Array.isArray(input) ? "array" : typeof input;
568
- throw new InvalidUpdateError(`Expected node "${nodeKey.toString()}" to return an object or an array containing at least one Command object, received ${typeofInput}`, {
569
- lc_error_code: "INVALID_GRAPH_NODE_RETURN_VALUE",
570
- });
571
- }
572
- }
573
- const stateWriteEntries = [
574
- {
575
- value: PASSTHROUGH,
576
- mapper: new RunnableCallable({
577
- func: outputKeys.length && outputKeys[0] === ROOT
578
- ? _getRoot
579
- : _getUpdates,
580
- trace: false,
581
- recurse: false,
582
- }),
583
- },
584
- ];
585
- // add node and output channel
586
- if (key === START) {
587
- this.nodes[key] = new PregelNode({
588
- tags: [TAG_HIDDEN],
589
- triggers: [START],
590
- channels: [START],
591
- writers: [new ChannelWrite(stateWriteEntries, [TAG_HIDDEN])],
592
- });
593
- }
594
- else {
595
- const inputDefinition = node?.input ?? this.builder._schemaDefinition;
596
- const inputValues = Object.fromEntries(Object.keys(this.builder._schemaDefinitions.get(inputDefinition)).map((k) => [k, k]));
597
- const isSingleInput = Object.keys(inputValues).length === 1 && ROOT in inputValues;
598
- const branchChannel = `branch:to:${key}`;
599
- this.channels[branchChannel] = node?.defer
600
- ? new LastValueAfterFinish()
601
- : new EphemeralValue(false);
602
- this.nodes[key] = new PregelNode({
603
- triggers: [branchChannel],
604
- // read state keys
605
- channels: isSingleInput ? Object.keys(inputValues) : inputValues,
606
- // publish to state keys
607
- writers: [new ChannelWrite(stateWriteEntries, [TAG_HIDDEN])],
608
- mapper: isSingleInput
609
- ? undefined
610
- : // eslint-disable-next-line @typescript-eslint/no-explicit-any
611
- (input) => {
612
- return Object.fromEntries(Object.entries(input).filter(([k]) => k in inputValues));
613
- },
614
- bound: node?.runnable,
615
- metadata: node?.metadata,
616
- retryPolicy: node?.retryPolicy,
617
- cachePolicy: node?.cachePolicy,
618
- subgraphs: node?.subgraphs,
619
- ends: node?.ends,
620
- });
621
- }
622
- }
623
- attachEdge(starts, end) {
624
- if (end === END)
625
- return;
626
- if (typeof starts === "string") {
627
- this.nodes[starts].writers.push(new ChannelWrite([{ channel: `branch:to:${end}`, value: null }], [TAG_HIDDEN]));
628
- }
629
- else if (Array.isArray(starts)) {
630
- const channelName = `join:${starts.join("+")}:${end}`;
631
- // register channel
632
- this.channels[channelName] = this.builder.nodes[end].defer
633
- ? new NamedBarrierValueAfterFinish(new Set(starts))
634
- : new NamedBarrierValue(new Set(starts));
635
- // subscribe to channel
636
- this.nodes[end].triggers.push(channelName);
637
- // publish to channel
638
- for (const start of starts) {
639
- this.nodes[start].writers.push(new ChannelWrite([{ channel: channelName, value: start }], [TAG_HIDDEN]));
640
- }
641
- }
642
- }
643
- attachBranch(start, _, branch, options = { withReader: true }) {
644
- const branchWriter = async (packets, config) => {
645
- const filteredPackets = packets.filter((p) => p !== END);
646
- if (!filteredPackets.length)
647
- return;
648
- const writes = filteredPackets.map((p) => {
649
- if (_isSend(p))
650
- return p;
651
- return { channel: p === END ? p : `branch:to:${p}`, value: start };
652
- });
653
- await ChannelWrite.doWrite({ ...config, tags: (config.tags ?? []).concat([TAG_HIDDEN]) }, writes);
654
- };
655
- // attach branch publisher
656
- this.nodes[start].writers.push(branch.run(branchWriter,
657
- // reader
658
- options.withReader
659
- ? (config) => ChannelRead.doRead(config, this.streamChannels ?? this.outputChannels, true)
660
- : undefined));
661
- }
662
- async _validateInput(input) {
663
- if (input == null)
664
- return input;
665
- const schema = (() => {
666
- const input = this.builder._inputRuntimeDefinition;
667
- const schema = this.builder._schemaRuntimeDefinition;
668
- const apply = (schema) => {
669
- if (schema == null)
670
- return undefined;
671
- return this._metaRegistry.getExtendedChannelSchemas(schema, {
672
- withReducerSchema: true,
673
- });
674
- };
675
- if (isInteropZodObject(input))
676
- return apply(input);
677
- if (input === PartialStateSchema) {
678
- return interopZodObjectPartial(apply(schema));
679
- }
680
- return undefined;
681
- })();
682
- if (isCommand(input)) {
683
- const parsedInput = input;
684
- if (input.update && schema != null)
685
- parsedInput.update = interopParse(schema, input.update);
686
- return parsedInput;
687
- }
688
- if (schema != null)
689
- return interopParse(schema, input);
690
- return input;
691
- }
692
- async _validateContext(config) {
693
- const configSchema = this.builder._configRuntimeSchema;
694
- if (isInteropZodObject(configSchema))
695
- interopParse(configSchema, config);
696
- return config;
697
- }
698
- }
290
+ * Final result from building and compiling a {@link StateGraph}.
291
+ * Should not be instantiated directly, only using the StateGraph `.compile()`
292
+ * instance method.
293
+ */
294
+ var CompiledStateGraph = class extends CompiledGraph {
295
+ /**
296
+ * The description of the compiled graph.
297
+ * This is used by the supervisor agent to describe the handoff to the agent.
298
+ */
299
+ description;
300
+ /** @internal */
301
+ _metaRegistry = schemaMetaRegistry;
302
+ constructor({ description,...rest }) {
303
+ super(rest);
304
+ this.description = description;
305
+ }
306
+ attachNode(key, node) {
307
+ let outputKeys;
308
+ if (key === START) outputKeys = Object.entries(this.builder._schemaDefinitions.get(this.builder._inputDefinition)).map(([k]) => k);
309
+ else outputKeys = Object.keys(this.builder.channels);
310
+ function _getRoot(input) {
311
+ if (isCommand(input)) {
312
+ if (input.graph === Command.PARENT) return null;
313
+ return input._updateAsTuples();
314
+ } else if (Array.isArray(input) && input.length > 0 && input.some((i) => isCommand(i))) {
315
+ const updates = [];
316
+ for (const i of input) if (isCommand(i)) {
317
+ if (i.graph === Command.PARENT) continue;
318
+ updates.push(...i._updateAsTuples());
319
+ } else updates.push([ROOT, i]);
320
+ return updates;
321
+ } else if (input != null) return [[ROOT, input]];
322
+ return null;
323
+ }
324
+ const nodeKey = key;
325
+ function _getUpdates(input) {
326
+ if (!input) return null;
327
+ else if (isCommand(input)) {
328
+ if (input.graph === Command.PARENT) return null;
329
+ return input._updateAsTuples().filter(([k]) => outputKeys.includes(k));
330
+ } else if (Array.isArray(input) && input.length > 0 && input.some(isCommand)) {
331
+ const updates = [];
332
+ for (const item of input) if (isCommand(item)) {
333
+ if (item.graph === Command.PARENT) continue;
334
+ updates.push(...item._updateAsTuples().filter(([k]) => outputKeys.includes(k)));
335
+ } else {
336
+ const itemUpdates = _getUpdates(item);
337
+ if (itemUpdates) updates.push(...itemUpdates ?? []);
338
+ }
339
+ return updates;
340
+ } else if (typeof input === "object" && !Array.isArray(input)) return Object.entries(input).filter(([k]) => outputKeys.includes(k));
341
+ else {
342
+ const typeofInput = Array.isArray(input) ? "array" : typeof input;
343
+ throw new InvalidUpdateError(`Expected node "${nodeKey.toString()}" to return an object or an array containing at least one Command object, received ${typeofInput}`, { lc_error_code: "INVALID_GRAPH_NODE_RETURN_VALUE" });
344
+ }
345
+ }
346
+ const stateWriteEntries = [{
347
+ value: PASSTHROUGH,
348
+ mapper: new RunnableCallable({
349
+ func: outputKeys.length && outputKeys[0] === ROOT ? _getRoot : _getUpdates,
350
+ trace: false,
351
+ recurse: false
352
+ })
353
+ }];
354
+ if (key === START) this.nodes[key] = new PregelNode({
355
+ tags: [TAG_HIDDEN],
356
+ triggers: [START],
357
+ channels: [START],
358
+ writers: [new ChannelWrite(stateWriteEntries, [TAG_HIDDEN])]
359
+ });
360
+ else {
361
+ const inputDefinition = node?.input ?? this.builder._schemaDefinition;
362
+ const inputValues = Object.fromEntries(Object.keys(this.builder._schemaDefinitions.get(inputDefinition)).map((k) => [k, k]));
363
+ const isSingleInput = Object.keys(inputValues).length === 1 && ROOT in inputValues;
364
+ const branchChannel = `branch:to:${key}`;
365
+ this.channels[branchChannel] = node?.defer ? new LastValueAfterFinish() : new EphemeralValue(false);
366
+ this.nodes[key] = new PregelNode({
367
+ triggers: [branchChannel],
368
+ channels: isSingleInput ? Object.keys(inputValues) : inputValues,
369
+ writers: [new ChannelWrite(stateWriteEntries, [TAG_HIDDEN])],
370
+ mapper: isSingleInput ? void 0 : (input) => {
371
+ return Object.fromEntries(Object.entries(input).filter(([k]) => k in inputValues));
372
+ },
373
+ bound: node?.runnable,
374
+ metadata: node?.metadata,
375
+ retryPolicy: node?.retryPolicy,
376
+ cachePolicy: node?.cachePolicy,
377
+ subgraphs: node?.subgraphs,
378
+ ends: node?.ends
379
+ });
380
+ }
381
+ }
382
+ attachEdge(starts, end) {
383
+ if (end === END) return;
384
+ if (typeof starts === "string") this.nodes[starts].writers.push(new ChannelWrite([{
385
+ channel: `branch:to:${end}`,
386
+ value: null
387
+ }], [TAG_HIDDEN]));
388
+ else if (Array.isArray(starts)) {
389
+ const channelName = `join:${starts.join("+")}:${end}`;
390
+ this.channels[channelName] = this.builder.nodes[end].defer ? new NamedBarrierValueAfterFinish(new Set(starts)) : new NamedBarrierValue(new Set(starts));
391
+ this.nodes[end].triggers.push(channelName);
392
+ for (const start of starts) this.nodes[start].writers.push(new ChannelWrite([{
393
+ channel: channelName,
394
+ value: start
395
+ }], [TAG_HIDDEN]));
396
+ }
397
+ }
398
+ attachBranch(start, _, branch, options = { withReader: true }) {
399
+ const branchWriter = async (packets, config) => {
400
+ const filteredPackets = packets.filter((p) => p !== END);
401
+ if (!filteredPackets.length) return;
402
+ const writes = filteredPackets.map((p) => {
403
+ if (_isSend(p)) return p;
404
+ return {
405
+ channel: p === END ? p : `branch:to:${p}`,
406
+ value: start
407
+ };
408
+ });
409
+ await ChannelWrite.doWrite({
410
+ ...config,
411
+ tags: (config.tags ?? []).concat([TAG_HIDDEN])
412
+ }, writes);
413
+ };
414
+ this.nodes[start].writers.push(branch.run(branchWriter, options.withReader ? (config) => ChannelRead.doRead(config, this.streamChannels ?? this.outputChannels, true) : void 0));
415
+ }
416
+ async _validateInput(input) {
417
+ if (input == null) return input;
418
+ const schema = (() => {
419
+ const input$1 = this.builder._inputRuntimeDefinition;
420
+ const schema$1 = this.builder._schemaRuntimeDefinition;
421
+ const apply = (schema$2) => {
422
+ if (schema$2 == null) return void 0;
423
+ return this._metaRegistry.getExtendedChannelSchemas(schema$2, { withReducerSchema: true });
424
+ };
425
+ if (isInteropZodObject(input$1)) return apply(input$1);
426
+ if (input$1 === PartialStateSchema) return interopZodObjectPartial(apply(schema$1));
427
+ return void 0;
428
+ })();
429
+ if (isCommand(input)) {
430
+ const parsedInput = input;
431
+ if (input.update && schema != null) parsedInput.update = interopParse(schema, input.update);
432
+ return parsedInput;
433
+ }
434
+ if (schema != null) return interopParse(schema, input);
435
+ return input;
436
+ }
437
+ isInterrupted(input) {
438
+ return isInterrupted(input);
439
+ }
440
+ async _validateContext(config) {
441
+ const configSchema = this.builder._configRuntimeSchema;
442
+ if (isInteropZodObject(configSchema)) interopParse(configSchema, config);
443
+ return config;
444
+ }
445
+ };
699
446
  function isStateDefinition(obj) {
700
- return (typeof obj === "object" &&
701
- obj !== null &&
702
- !Array.isArray(obj) &&
703
- Object.keys(obj).length > 0 &&
704
- Object.values(obj).every((v) => typeof v === "function" || isBaseChannel(v)));
447
+ return typeof obj === "object" && obj !== null && !Array.isArray(obj) && Object.keys(obj).length > 0 && Object.values(obj).every((v) => typeof v === "function" || isBaseChannel(v));
705
448
  }
706
449
  function isAnnotationRoot(obj) {
707
- return (typeof obj === "object" &&
708
- obj !== null &&
709
- "lc_graph_name" in obj &&
710
- obj.lc_graph_name === "AnnotationRoot");
450
+ return typeof obj === "object" && obj !== null && "lc_graph_name" in obj && obj.lc_graph_name === "AnnotationRoot";
711
451
  }
712
452
  function isStateGraphArgs(obj) {
713
- return (typeof obj === "object" &&
714
- obj !== null &&
715
- obj.channels !== undefined);
453
+ return typeof obj === "object" && obj !== null && obj.channels !== void 0;
716
454
  }
717
455
  function isStateGraphArgsWithStateSchema(obj) {
718
- return (typeof obj === "object" &&
719
- obj !== null &&
720
- obj.stateSchema !== undefined);
456
+ return typeof obj === "object" && obj !== null && obj.stateSchema !== void 0;
721
457
  }
722
458
  function isStateGraphArgsWithInputOutputSchemas(obj) {
723
- return (typeof obj === "object" &&
724
- obj !== null &&
725
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
726
- obj.stateSchema === undefined &&
727
- obj.input !== undefined &&
728
- obj.output !== undefined);
459
+ return typeof obj === "object" && obj !== null && obj.stateSchema === void 0 && obj.input !== void 0 && obj.output !== void 0;
729
460
  }
730
461
  function isZodStateGraphArgsWithStateSchema(value) {
731
- if (typeof value !== "object" || value == null) {
732
- return false;
733
- }
734
- if (!("state" in value) || !isInteropZodObject(value.state)) {
735
- return false;
736
- }
737
- if ("input" in value && !isInteropZodObject(value.input)) {
738
- return false;
739
- }
740
- if ("output" in value && !isInteropZodObject(value.output)) {
741
- return false;
742
- }
743
- return true;
462
+ if (typeof value !== "object" || value == null) return false;
463
+ if (!("state" in value) || !isInteropZodObject(value.state)) return false;
464
+ if ("input" in value && !isInteropZodObject(value.input)) return false;
465
+ if ("output" in value && !isInteropZodObject(value.output)) return false;
466
+ return true;
744
467
  }
745
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
746
468
  function _controlBranch(value) {
747
- if (_isSend(value)) {
748
- return [value];
749
- }
750
- const commands = [];
751
- if (isCommand(value)) {
752
- commands.push(value);
753
- }
754
- else if (Array.isArray(value)) {
755
- commands.push(...value.filter(isCommand));
756
- }
757
- const destinations = [];
758
- for (const command of commands) {
759
- if (command.graph === Command.PARENT) {
760
- throw new ParentCommand(command);
761
- }
762
- if (_isSend(command.goto)) {
763
- destinations.push(command.goto);
764
- }
765
- else if (typeof command.goto === "string") {
766
- destinations.push(command.goto);
767
- }
768
- else {
769
- if (Array.isArray(command.goto)) {
770
- destinations.push(...command.goto);
771
- }
772
- }
773
- }
774
- return destinations;
469
+ if (_isSend(value)) return [value];
470
+ const commands = [];
471
+ if (isCommand(value)) commands.push(value);
472
+ else if (Array.isArray(value)) commands.push(...value.filter(isCommand));
473
+ const destinations = [];
474
+ for (const command of commands) {
475
+ if (command.graph === Command.PARENT) throw new ParentCommand(command);
476
+ if (_isSend(command.goto)) destinations.push(command.goto);
477
+ else if (typeof command.goto === "string") destinations.push(command.goto);
478
+ else if (Array.isArray(command.goto)) destinations.push(...command.goto);
479
+ }
480
+ return destinations;
775
481
  }
776
482
  function _getControlBranch() {
777
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
778
- const CONTROL_BRANCH_PATH = new RunnableCallable({
779
- func: _controlBranch,
780
- tags: [TAG_HIDDEN],
781
- trace: false,
782
- recurse: false,
783
- name: "<control_branch>",
784
- });
785
- return new Branch({
786
- path: CONTROL_BRANCH_PATH,
787
- });
788
- }
789
- export function typedNode(_state, _options) {
790
- return (func, options) => {
791
- Object.assign(func, { [Symbol.for("langgraph.state.node")]: options });
792
- return func;
793
- };
483
+ const CONTROL_BRANCH_PATH = new RunnableCallable({
484
+ func: _controlBranch,
485
+ tags: [TAG_HIDDEN],
486
+ trace: false,
487
+ recurse: false,
488
+ name: "<control_branch>"
489
+ });
490
+ return new Branch({ path: CONTROL_BRANCH_PATH });
794
491
  }
492
+
493
+ //#endregion
494
+ export { CompiledStateGraph, StateGraph };
795
495
  //# sourceMappingURL=state.js.map