@langchain/langgraph 0.4.8 → 1.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (494) hide show
  1. package/CHANGELOG.md +7 -2
  2. package/README.md +1 -2
  3. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  4. package/dist/channels/any_value.d.cts +26 -0
  5. package/dist/channels/any_value.d.cts.map +1 -0
  6. package/dist/channels/any_value.d.ts +16 -9
  7. package/dist/channels/any_value.d.ts.map +1 -0
  8. package/dist/channels/base.cjs +97 -133
  9. package/dist/channels/base.cjs.map +1 -0
  10. package/dist/channels/base.d.cts +73 -0
  11. package/dist/channels/base.d.cts.map +1 -0
  12. package/dist/channels/base.d.ts +73 -74
  13. package/dist/channels/base.d.ts.map +1 -0
  14. package/dist/channels/base.js +94 -127
  15. package/dist/channels/base.js.map +1 -1
  16. package/dist/channels/binop.cjs +47 -77
  17. package/dist/channels/binop.cjs.map +1 -0
  18. package/dist/channels/binop.d.cts +22 -0
  19. package/dist/channels/binop.d.cts.map +1 -0
  20. package/dist/channels/binop.d.ts +17 -12
  21. package/dist/channels/binop.d.ts.map +1 -0
  22. package/dist/channels/binop.js +45 -71
  23. package/dist/channels/binop.js.map +1 -1
  24. package/dist/channels/dynamic_barrier_value.d.cts +43 -0
  25. package/dist/channels/dynamic_barrier_value.d.cts.map +1 -0
  26. package/dist/channels/dynamic_barrier_value.d.ts +20 -34
  27. package/dist/channels/dynamic_barrier_value.d.ts.map +1 -0
  28. package/dist/channels/ephemeral_value.cjs +46 -70
  29. package/dist/channels/ephemeral_value.cjs.map +1 -0
  30. package/dist/channels/ephemeral_value.d.cts +23 -0
  31. package/dist/channels/ephemeral_value.d.cts.map +1 -0
  32. package/dist/channels/ephemeral_value.d.ts +18 -11
  33. package/dist/channels/ephemeral_value.d.ts.map +1 -0
  34. package/dist/channels/ephemeral_value.js +45 -65
  35. package/dist/channels/ephemeral_value.js.map +1 -1
  36. package/dist/channels/index.cjs +2 -10
  37. package/dist/channels/index.js +4 -3
  38. package/dist/channels/last_value.cjs +91 -140
  39. package/dist/channels/last_value.cjs.map +1 -0
  40. package/dist/channels/last_value.d.cts +30 -0
  41. package/dist/channels/last_value.d.cts.map +1 -0
  42. package/dist/channels/last_value.d.ts +15 -23
  43. package/dist/channels/last_value.d.ts.map +1 -0
  44. package/dist/channels/last_value.js +88 -133
  45. package/dist/channels/last_value.js.map +1 -1
  46. package/dist/channels/named_barrier_value.cjs +114 -170
  47. package/dist/channels/named_barrier_value.cjs.map +1 -0
  48. package/dist/channels/named_barrier_value.d.cts +33 -0
  49. package/dist/channels/named_barrier_value.d.cts.map +1 -0
  50. package/dist/channels/named_barrier_value.d.ts +19 -29
  51. package/dist/channels/named_barrier_value.d.ts.map +1 -0
  52. package/dist/channels/named_barrier_value.js +112 -163
  53. package/dist/channels/named_barrier_value.js.map +1 -1
  54. package/dist/channels/topic.cjs +63 -96
  55. package/dist/channels/topic.cjs.map +1 -0
  56. package/dist/channels/topic.d.cts +26 -0
  57. package/dist/channels/topic.d.cts.map +1 -0
  58. package/dist/channels/topic.d.ts +21 -18
  59. package/dist/channels/topic.d.ts.map +1 -0
  60. package/dist/channels/topic.js +61 -90
  61. package/dist/channels/topic.js.map +1 -1
  62. package/dist/constants.cjs +373 -452
  63. package/dist/constants.cjs.map +1 -0
  64. package/dist/constants.d.cts +284 -0
  65. package/dist/constants.d.cts.map +1 -0
  66. package/dist/constants.d.ts +112 -148
  67. package/dist/constants.d.ts.map +1 -0
  68. package/dist/constants.js +333 -444
  69. package/dist/constants.js.map +1 -1
  70. package/dist/errors.cjs +150 -179
  71. package/dist/errors.cjs.map +1 -0
  72. package/dist/errors.d.cts +80 -0
  73. package/dist/errors.d.cts.map +1 -0
  74. package/dist/errors.d.ts +55 -46
  75. package/dist/errors.d.ts.map +1 -0
  76. package/dist/errors.js +139 -165
  77. package/dist/errors.js.map +1 -1
  78. package/dist/func/index.cjs +289 -290
  79. package/dist/func/index.cjs.map +1 -0
  80. package/dist/func/index.d.cts +289 -0
  81. package/dist/func/index.d.cts.map +1 -0
  82. package/dist/func/index.d.ts +81 -71
  83. package/dist/func/index.d.ts.map +1 -0
  84. package/dist/func/index.js +285 -284
  85. package/dist/func/index.js.map +1 -1
  86. package/dist/func/types.d.cts +64 -0
  87. package/dist/func/types.d.cts.map +1 -0
  88. package/dist/func/types.d.ts +24 -19
  89. package/dist/func/types.d.ts.map +1 -0
  90. package/dist/graph/annotation.cjs +96 -120
  91. package/dist/graph/annotation.cjs.map +1 -0
  92. package/dist/graph/annotation.d.cts +116 -0
  93. package/dist/graph/annotation.d.cts.map +1 -0
  94. package/dist/graph/annotation.d.ts +33 -34
  95. package/dist/graph/annotation.d.ts.map +1 -0
  96. package/dist/graph/annotation.js +91 -112
  97. package/dist/graph/annotation.js.map +1 -1
  98. package/dist/graph/graph.cjs +419 -723
  99. package/dist/graph/graph.cjs.map +1 -0
  100. package/dist/graph/graph.d.cts +131 -0
  101. package/dist/graph/graph.d.cts.map +1 -0
  102. package/dist/graph/graph.d.ts +122 -91
  103. package/dist/graph/graph.d.ts.map +1 -0
  104. package/dist/graph/graph.js +412 -713
  105. package/dist/graph/graph.js.map +1 -1
  106. package/dist/graph/index.cjs +4 -17
  107. package/dist/graph/index.js +6 -5
  108. package/dist/graph/message.cjs +59 -104
  109. package/dist/graph/message.cjs.map +1 -0
  110. package/dist/graph/message.d.cts +19 -0
  111. package/dist/graph/message.d.cts.map +1 -0
  112. package/dist/graph/message.d.ts +11 -10
  113. package/dist/graph/message.d.ts.map +1 -0
  114. package/dist/graph/message.js +56 -98
  115. package/dist/graph/message.js.map +1 -1
  116. package/dist/graph/messages_annotation.cjs +106 -106
  117. package/dist/graph/messages_annotation.cjs.map +1 -0
  118. package/dist/graph/messages_annotation.d.cts +111 -0
  119. package/dist/graph/messages_annotation.d.cts.map +1 -0
  120. package/dist/graph/messages_annotation.d.ts +18 -9
  121. package/dist/graph/messages_annotation.d.ts.map +1 -0
  122. package/dist/graph/messages_annotation.js +100 -100
  123. package/dist/graph/messages_annotation.js.map +1 -1
  124. package/dist/graph/state.cjs +476 -777
  125. package/dist/graph/state.cjs.map +1 -0
  126. package/dist/graph/state.d.cts +216 -0
  127. package/dist/graph/state.d.cts.map +1 -0
  128. package/dist/graph/state.d.ts +129 -134
  129. package/dist/graph/state.d.ts.map +1 -0
  130. package/dist/graph/state.js +470 -768
  131. package/dist/graph/state.js.map +1 -1
  132. package/dist/graph/zod/index.cjs +10 -21
  133. package/dist/graph/zod/index.d.cts +3 -0
  134. package/dist/graph/zod/index.d.ts +3 -3
  135. package/dist/graph/zod/index.js +4 -4
  136. package/dist/graph/zod/meta.cjs +142 -177
  137. package/dist/graph/zod/meta.cjs.map +1 -0
  138. package/dist/graph/zod/meta.d.cts +116 -0
  139. package/dist/graph/zod/meta.d.cts.map +1 -0
  140. package/dist/graph/zod/meta.d.ts +99 -97
  141. package/dist/graph/zod/meta.d.ts.map +1 -0
  142. package/dist/graph/zod/meta.js +136 -170
  143. package/dist/graph/zod/meta.js.map +1 -1
  144. package/dist/graph/zod/plugin.cjs +36 -39
  145. package/dist/graph/zod/plugin.cjs.map +1 -0
  146. package/dist/graph/zod/plugin.js +34 -35
  147. package/dist/graph/zod/plugin.js.map +1 -1
  148. package/dist/graph/zod/schema.cjs +82 -110
  149. package/dist/graph/zod/schema.cjs.map +1 -0
  150. package/dist/graph/zod/schema.d.cts +38 -0
  151. package/dist/graph/zod/schema.d.cts.map +1 -0
  152. package/dist/graph/zod/schema.d.ts +12 -6
  153. package/dist/graph/zod/schema.d.ts.map +1 -0
  154. package/dist/graph/zod/schema.js +77 -103
  155. package/dist/graph/zod/schema.js.map +1 -1
  156. package/dist/graph/zod/zod-registry.cjs +41 -47
  157. package/dist/graph/zod/zod-registry.cjs.map +1 -0
  158. package/dist/graph/zod/zod-registry.d.cts +51 -0
  159. package/dist/graph/zod/zod-registry.d.cts.map +1 -0
  160. package/dist/graph/zod/zod-registry.d.ts +34 -26
  161. package/dist/graph/zod/zod-registry.d.ts.map +1 -0
  162. package/dist/graph/zod/zod-registry.js +37 -41
  163. package/dist/graph/zod/zod-registry.js.map +1 -1
  164. package/dist/hash.cjs +205 -267
  165. package/dist/hash.cjs.map +1 -0
  166. package/dist/hash.js +205 -265
  167. package/dist/hash.js.map +1 -1
  168. package/dist/index.cjs +110 -33
  169. package/dist/index.cjs.map +1 -0
  170. package/dist/index.d.cts +25 -0
  171. package/dist/index.d.ts +25 -5
  172. package/dist/index.js +18 -7
  173. package/dist/index.js.map +1 -1
  174. package/dist/interrupt.cjs +79 -85
  175. package/dist/interrupt.cjs.map +1 -0
  176. package/dist/interrupt.d.cts +49 -0
  177. package/dist/interrupt.d.cts.map +1 -0
  178. package/dist/interrupt.d.ts +6 -1
  179. package/dist/interrupt.d.ts.map +1 -0
  180. package/dist/interrupt.js +76 -80
  181. package/dist/interrupt.js.map +1 -1
  182. package/dist/prebuilt/agentName.cjs +139 -172
  183. package/dist/prebuilt/agentName.cjs.map +1 -0
  184. package/dist/prebuilt/agentName.d.cts +42 -0
  185. package/dist/prebuilt/agentName.d.cts.map +1 -0
  186. package/dist/prebuilt/agentName.d.ts +13 -21
  187. package/dist/prebuilt/agentName.d.ts.map +1 -0
  188. package/dist/prebuilt/agentName.js +139 -168
  189. package/dist/prebuilt/agentName.js.map +1 -1
  190. package/dist/prebuilt/agent_executor.cjs +42 -80
  191. package/dist/prebuilt/agent_executor.cjs.map +1 -0
  192. package/dist/prebuilt/agent_executor.d.cts +57 -0
  193. package/dist/prebuilt/agent_executor.d.cts.map +1 -0
  194. package/dist/prebuilt/agent_executor.d.ts +46 -37
  195. package/dist/prebuilt/agent_executor.d.ts.map +1 -0
  196. package/dist/prebuilt/agent_executor.js +40 -75
  197. package/dist/prebuilt/agent_executor.js.map +1 -1
  198. package/dist/prebuilt/chat_agent_executor.cjs +66 -128
  199. package/dist/prebuilt/chat_agent_executor.cjs.map +1 -0
  200. package/dist/prebuilt/chat_agent_executor.d.cts +23 -0
  201. package/dist/prebuilt/chat_agent_executor.d.cts.map +1 -0
  202. package/dist/prebuilt/chat_agent_executor.d.ts +18 -10
  203. package/dist/prebuilt/chat_agent_executor.d.ts.map +1 -0
  204. package/dist/prebuilt/chat_agent_executor.js +63 -123
  205. package/dist/prebuilt/chat_agent_executor.js.map +1 -1
  206. package/dist/prebuilt/index.cjs +15 -18
  207. package/dist/prebuilt/index.d.cts +8 -0
  208. package/dist/prebuilt/index.d.ts +8 -8
  209. package/dist/prebuilt/index.js +8 -7
  210. package/dist/prebuilt/interrupt.d.cts +73 -0
  211. package/dist/prebuilt/interrupt.d.cts.map +1 -0
  212. package/dist/prebuilt/interrupt.d.ts +32 -15
  213. package/dist/prebuilt/interrupt.d.ts.map +1 -0
  214. package/dist/prebuilt/react_agent_executor.cjs +317 -473
  215. package/dist/prebuilt/react_agent_executor.cjs.map +1 -0
  216. package/dist/prebuilt/react_agent_executor.d.cts +229 -0
  217. package/dist/prebuilt/react_agent_executor.d.cts.map +1 -0
  218. package/dist/prebuilt/react_agent_executor.d.ts +171 -143
  219. package/dist/prebuilt/react_agent_executor.d.ts.map +1 -0
  220. package/dist/prebuilt/react_agent_executor.js +315 -465
  221. package/dist/prebuilt/react_agent_executor.js.map +1 -1
  222. package/dist/prebuilt/tool_executor.cjs +45 -67
  223. package/dist/prebuilt/tool_executor.cjs.map +1 -0
  224. package/dist/prebuilt/tool_executor.d.cts +42 -0
  225. package/dist/prebuilt/tool_executor.d.cts.map +1 -0
  226. package/dist/prebuilt/tool_executor.d.ts +30 -24
  227. package/dist/prebuilt/tool_executor.d.ts.map +1 -0
  228. package/dist/prebuilt/tool_executor.js +44 -63
  229. package/dist/prebuilt/tool_executor.js.map +1 -1
  230. package/dist/prebuilt/tool_node.cjs +213 -275
  231. package/dist/prebuilt/tool_node.cjs.map +1 -0
  232. package/dist/prebuilt/tool_node.d.cts +151 -0
  233. package/dist/prebuilt/tool_node.d.cts.map +1 -0
  234. package/dist/prebuilt/tool_node.d.ts +30 -17
  235. package/dist/prebuilt/tool_node.d.ts.map +1 -0
  236. package/dist/prebuilt/tool_node.js +209 -268
  237. package/dist/prebuilt/tool_node.js.map +1 -1
  238. package/dist/pregel/algo.cjs +521 -692
  239. package/dist/pregel/algo.cjs.map +1 -0
  240. package/dist/pregel/algo.d.cts +13 -0
  241. package/dist/pregel/algo.d.cts.map +1 -0
  242. package/dist/pregel/algo.d.ts +8 -38
  243. package/dist/pregel/algo.d.ts.map +1 -0
  244. package/dist/pregel/algo.js +516 -683
  245. package/dist/pregel/algo.js.map +1 -1
  246. package/dist/pregel/call.cjs +46 -42
  247. package/dist/pregel/call.cjs.map +1 -0
  248. package/dist/pregel/call.js +43 -37
  249. package/dist/pregel/call.js.map +1 -1
  250. package/dist/pregel/debug.cjs +153 -223
  251. package/dist/pregel/debug.cjs.map +1 -0
  252. package/dist/pregel/debug.js +152 -215
  253. package/dist/pregel/debug.js.map +1 -1
  254. package/dist/pregel/index.cjs +1134 -1604
  255. package/dist/pregel/index.cjs.map +1 -0
  256. package/dist/pregel/index.d.cts +534 -0
  257. package/dist/pregel/index.d.cts.map +1 -0
  258. package/dist/pregel/index.d.ts +458 -422
  259. package/dist/pregel/index.d.ts.map +1 -0
  260. package/dist/pregel/index.js +1125 -1592
  261. package/dist/pregel/index.js.map +1 -1
  262. package/dist/pregel/io.cjs +127 -234
  263. package/dist/pregel/io.cjs.map +1 -0
  264. package/dist/pregel/io.js +122 -225
  265. package/dist/pregel/io.js.map +1 -1
  266. package/dist/pregel/loop.cjs +539 -954
  267. package/dist/pregel/loop.cjs.map +1 -0
  268. package/dist/pregel/loop.js +536 -948
  269. package/dist/pregel/loop.js.map +1 -1
  270. package/dist/pregel/messages.cjs +104 -196
  271. package/dist/pregel/messages.cjs.map +1 -0
  272. package/dist/pregel/messages.js +102 -191
  273. package/dist/pregel/messages.js.map +1 -1
  274. package/dist/pregel/read.cjs +150 -280
  275. package/dist/pregel/read.cjs.map +1 -0
  276. package/dist/pregel/read.d.cts +51 -0
  277. package/dist/pregel/read.d.cts.map +1 -0
  278. package/dist/pregel/read.d.ts +48 -46
  279. package/dist/pregel/read.d.ts.map +1 -0
  280. package/dist/pregel/read.js +147 -274
  281. package/dist/pregel/read.js.map +1 -1
  282. package/dist/pregel/remote.cjs +339 -458
  283. package/dist/pregel/remote.cjs.map +1 -0
  284. package/dist/pregel/remote.d.cts +121 -0
  285. package/dist/pregel/remote.d.cts.map +1 -0
  286. package/dist/pregel/remote.d.ts +79 -69
  287. package/dist/pregel/remote.d.ts.map +1 -0
  288. package/dist/pregel/remote.js +337 -453
  289. package/dist/pregel/remote.js.map +1 -1
  290. package/dist/pregel/retry.cjs +87 -138
  291. package/dist/pregel/retry.cjs.map +1 -0
  292. package/dist/pregel/retry.js +83 -130
  293. package/dist/pregel/retry.js.map +1 -1
  294. package/dist/pregel/runnable_types.d.cts +25 -0
  295. package/dist/pregel/runnable_types.d.cts.map +1 -0
  296. package/dist/pregel/runnable_types.d.ts +22 -16
  297. package/dist/pregel/runnable_types.d.ts.map +1 -0
  298. package/dist/pregel/runner.cjs +222 -315
  299. package/dist/pregel/runner.cjs.map +1 -0
  300. package/dist/pregel/runner.js +219 -308
  301. package/dist/pregel/runner.js.map +1 -1
  302. package/dist/pregel/stream.cjs +89 -130
  303. package/dist/pregel/stream.cjs.map +1 -0
  304. package/dist/pregel/stream.js +87 -125
  305. package/dist/pregel/stream.js.map +1 -1
  306. package/dist/pregel/types.cjs +25 -62
  307. package/dist/pregel/types.cjs.map +1 -0
  308. package/dist/pregel/types.d.cts +437 -0
  309. package/dist/pregel/types.d.cts.map +1 -0
  310. package/dist/pregel/types.d.ts +377 -431
  311. package/dist/pregel/types.d.ts.map +1 -0
  312. package/dist/pregel/types.js +23 -57
  313. package/dist/pregel/types.js.map +1 -1
  314. package/dist/pregel/utils/config.cjs +100 -147
  315. package/dist/pregel/utils/config.cjs.map +1 -0
  316. package/dist/pregel/utils/config.d.cts +36 -0
  317. package/dist/pregel/utils/config.d.cts.map +1 -0
  318. package/dist/pregel/utils/config.d.ts +12 -9
  319. package/dist/pregel/utils/config.d.ts.map +1 -0
  320. package/dist/pregel/utils/config.js +98 -143
  321. package/dist/pregel/utils/config.js.map +1 -1
  322. package/dist/pregel/utils/index.cjs +87 -125
  323. package/dist/pregel/utils/index.cjs.map +1 -0
  324. package/dist/pregel/utils/index.d.cts +51 -0
  325. package/dist/pregel/utils/index.d.cts.map +1 -0
  326. package/dist/pregel/utils/index.d.ts +45 -61
  327. package/dist/pregel/utils/index.d.ts.map +1 -0
  328. package/dist/pregel/utils/index.js +86 -120
  329. package/dist/pregel/utils/index.js.map +1 -1
  330. package/dist/pregel/utils/subgraph.cjs +15 -26
  331. package/dist/pregel/utils/subgraph.cjs.map +1 -0
  332. package/dist/pregel/utils/subgraph.js +12 -21
  333. package/dist/pregel/utils/subgraph.js.map +1 -1
  334. package/dist/pregel/validate.cjs +42 -92
  335. package/dist/pregel/validate.cjs.map +1 -0
  336. package/dist/pregel/validate.js +39 -84
  337. package/dist/pregel/validate.js.map +1 -1
  338. package/dist/pregel/write.cjs +87 -137
  339. package/dist/pregel/write.cjs.map +1 -0
  340. package/dist/pregel/write.d.cts +35 -0
  341. package/dist/pregel/write.d.cts.map +1 -0
  342. package/dist/pregel/write.d.ts +27 -23
  343. package/dist/pregel/write.d.ts.map +1 -0
  344. package/dist/pregel/write.js +83 -131
  345. package/dist/pregel/write.js.map +1 -1
  346. package/dist/remote.cjs +3 -6
  347. package/dist/remote.d.cts +2 -0
  348. package/dist/remote.d.ts +2 -1
  349. package/dist/remote.js +3 -2
  350. package/dist/setup/async_local_storage.cjs +10 -7
  351. package/dist/setup/async_local_storage.cjs.map +1 -0
  352. package/dist/setup/async_local_storage.js +7 -2
  353. package/dist/setup/async_local_storage.js.map +1 -1
  354. package/dist/ui/index.cjs +4 -0
  355. package/dist/ui/index.d.cts +5 -0
  356. package/dist/ui/index.d.ts +5 -0
  357. package/dist/ui/index.js +3 -0
  358. package/dist/ui/stream.cjs +145 -0
  359. package/dist/ui/stream.cjs.map +1 -0
  360. package/dist/ui/stream.d.cts +25 -0
  361. package/dist/ui/stream.d.cts.map +1 -0
  362. package/dist/ui/stream.d.ts +25 -0
  363. package/dist/ui/stream.d.ts.map +1 -0
  364. package/dist/ui/stream.js +143 -0
  365. package/dist/ui/stream.js.map +1 -0
  366. package/dist/ui/types.infer.d.cts +53 -0
  367. package/dist/ui/types.infer.d.cts.map +1 -0
  368. package/dist/ui/types.infer.d.ts +53 -0
  369. package/dist/ui/types.infer.d.ts.map +1 -0
  370. package/dist/ui/types.message.d.cts +95 -0
  371. package/dist/ui/types.message.d.cts.map +1 -0
  372. package/dist/ui/types.message.d.ts +95 -0
  373. package/dist/ui/types.message.d.ts.map +1 -0
  374. package/dist/ui/types.schema.d.cts +228 -0
  375. package/dist/ui/types.schema.d.cts.map +1 -0
  376. package/dist/ui/types.schema.d.ts +228 -0
  377. package/dist/ui/types.schema.d.ts.map +1 -0
  378. package/dist/utils.cjs +77 -147
  379. package/dist/utils.cjs.map +1 -0
  380. package/dist/utils.d.cts +32 -0
  381. package/dist/utils.d.cts.map +1 -0
  382. package/dist/utils.d.ts +29 -25
  383. package/dist/utils.d.ts.map +1 -0
  384. package/dist/utils.js +73 -140
  385. package/dist/utils.js.map +1 -1
  386. package/dist/web.cjs +97 -55
  387. package/dist/web.d.cts +23 -0
  388. package/dist/web.d.ts +23 -14
  389. package/dist/web.js +15 -8
  390. package/package.json +83 -86
  391. package/dist/channels/any_value.cjs +0 -65
  392. package/dist/channels/any_value.js +0 -61
  393. package/dist/channels/any_value.js.map +0 -1
  394. package/dist/channels/dynamic_barrier_value.cjs +0 -212
  395. package/dist/channels/dynamic_barrier_value.js +0 -207
  396. package/dist/channels/dynamic_barrier_value.js.map +0 -1
  397. package/dist/channels/index.d.ts +0 -7
  398. package/dist/channels/index.js.map +0 -1
  399. package/dist/func/types.cjs +0 -15
  400. package/dist/func/types.js +0 -12
  401. package/dist/func/types.js.map +0 -1
  402. package/dist/graph/index.d.ts +0 -4
  403. package/dist/graph/index.js.map +0 -1
  404. package/dist/graph/message.test.cjs +0 -196
  405. package/dist/graph/message.test.d.ts +0 -1
  406. package/dist/graph/message.test.js +0 -194
  407. package/dist/graph/message.test.js.map +0 -1
  408. package/dist/graph/zod/index.js.map +0 -1
  409. package/dist/graph/zod/plugin.d.ts +0 -28
  410. package/dist/hash.d.ts +0 -2
  411. package/dist/prebuilt/index.js.map +0 -1
  412. package/dist/prebuilt/interrupt.cjs +0 -3
  413. package/dist/prebuilt/interrupt.js +0 -2
  414. package/dist/prebuilt/interrupt.js.map +0 -1
  415. package/dist/pregel/call.d.ts +0 -16
  416. package/dist/pregel/debug.d.ts +0 -41
  417. package/dist/pregel/debug.test.cjs +0 -258
  418. package/dist/pregel/debug.test.d.ts +0 -1
  419. package/dist/pregel/debug.test.js +0 -256
  420. package/dist/pregel/debug.test.js.map +0 -1
  421. package/dist/pregel/io.d.ts +0 -30
  422. package/dist/pregel/io.mapCommand.test.cjs +0 -150
  423. package/dist/pregel/io.mapCommand.test.d.ts +0 -1
  424. package/dist/pregel/io.mapCommand.test.js +0 -148
  425. package/dist/pregel/io.mapCommand.test.js.map +0 -1
  426. package/dist/pregel/loop.d.ts +0 -147
  427. package/dist/pregel/messages.d.ts +0 -30
  428. package/dist/pregel/messages.test.cjs +0 -369
  429. package/dist/pregel/messages.test.d.ts +0 -1
  430. package/dist/pregel/messages.test.js +0 -367
  431. package/dist/pregel/messages.test.js.map +0 -1
  432. package/dist/pregel/read.test.cjs +0 -194
  433. package/dist/pregel/read.test.d.ts +0 -1
  434. package/dist/pregel/read.test.js +0 -192
  435. package/dist/pregel/read.test.js.map +0 -1
  436. package/dist/pregel/retry.d.ts +0 -17
  437. package/dist/pregel/runnable_types.cjs +0 -3
  438. package/dist/pregel/runnable_types.js +0 -2
  439. package/dist/pregel/runnable_types.js.map +0 -1
  440. package/dist/pregel/runner.d.ts +0 -79
  441. package/dist/pregel/runner.test.cjs +0 -66
  442. package/dist/pregel/runner.test.d.ts +0 -1
  443. package/dist/pregel/runner.test.js +0 -64
  444. package/dist/pregel/runner.test.js.map +0 -1
  445. package/dist/pregel/stream.d.ts +0 -40
  446. package/dist/pregel/utils/config.test.cjs +0 -214
  447. package/dist/pregel/utils/config.test.d.ts +0 -1
  448. package/dist/pregel/utils/config.test.js +0 -212
  449. package/dist/pregel/utils/config.test.js.map +0 -1
  450. package/dist/pregel/utils/subgraph.d.ts +0 -4
  451. package/dist/pregel/utils/subgraph.test.cjs +0 -83
  452. package/dist/pregel/utils/subgraph.test.d.ts +0 -1
  453. package/dist/pregel/utils/subgraph.test.js +0 -81
  454. package/dist/pregel/utils/subgraph.test.js.map +0 -1
  455. package/dist/pregel/validate.d.ts +0 -16
  456. package/dist/pregel/validate.test.cjs +0 -220
  457. package/dist/pregel/validate.test.d.ts +0 -1
  458. package/dist/pregel/validate.test.js +0 -218
  459. package/dist/pregel/validate.test.js.map +0 -1
  460. package/dist/pregel/write.test.cjs +0 -181
  461. package/dist/pregel/write.test.d.ts +0 -1
  462. package/dist/pregel/write.test.js +0 -179
  463. package/dist/pregel/write.test.js.map +0 -1
  464. package/dist/remote.js.map +0 -1
  465. package/dist/setup/async_local_storage.d.ts +0 -1
  466. package/dist/web.js.map +0 -1
  467. package/index.cjs +0 -1
  468. package/index.d.cts +0 -1
  469. package/index.d.ts +0 -1
  470. package/index.js +0 -1
  471. package/prebuilt.cjs +0 -1
  472. package/prebuilt.d.cts +0 -1
  473. package/prebuilt.d.ts +0 -1
  474. package/prebuilt.js +0 -1
  475. package/pregel.cjs +0 -1
  476. package/pregel.d.cts +0 -1
  477. package/pregel.d.ts +0 -1
  478. package/pregel.js +0 -1
  479. package/remote.cjs +0 -1
  480. package/remote.d.cts +0 -1
  481. package/remote.d.ts +0 -1
  482. package/remote.js +0 -1
  483. package/web.cjs +0 -1
  484. package/web.d.cts +0 -1
  485. package/web.d.ts +0 -1
  486. package/web.js +0 -1
  487. package/zod/schema.cjs +0 -1
  488. package/zod/schema.d.cts +0 -1
  489. package/zod/schema.d.ts +0 -1
  490. package/zod/schema.js +0 -1
  491. package/zod.cjs +0 -1
  492. package/zod.d.cts +0 -1
  493. package/zod.d.ts +0 -1
  494. package/zod.js +0 -1
@@ -1,795 +1,497 @@
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 } 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
+ constructor(fields, contextSchema) {
109
+ super();
110
+ if (isZodStateGraphArgsWithStateSchema(fields)) {
111
+ const stateDef = this._metaRegistry.getChannelsForSchema(fields.state);
112
+ const inputDef = fields.input != null ? this._metaRegistry.getChannelsForSchema(fields.input) : stateDef;
113
+ const outputDef = fields.output != null ? this._metaRegistry.getChannelsForSchema(fields.output) : stateDef;
114
+ this._schemaDefinition = stateDef;
115
+ this._schemaRuntimeDefinition = fields.state;
116
+ this._inputDefinition = inputDef;
117
+ this._inputRuntimeDefinition = fields.input ?? PartialStateSchema;
118
+ this._outputDefinition = outputDef;
119
+ this._outputRuntimeDefinition = fields.output ?? fields.state;
120
+ } else if (isInteropZodObject(fields)) {
121
+ const stateDef = this._metaRegistry.getChannelsForSchema(fields);
122
+ this._schemaDefinition = stateDef;
123
+ this._schemaRuntimeDefinition = fields;
124
+ this._inputDefinition = stateDef;
125
+ this._inputRuntimeDefinition = PartialStateSchema;
126
+ this._outputDefinition = stateDef;
127
+ this._outputRuntimeDefinition = fields;
128
+ } else if (isStateGraphArgsWithInputOutputSchemas(fields)) {
129
+ this._schemaDefinition = fields.input.spec;
130
+ this._inputDefinition = fields.input.spec;
131
+ this._outputDefinition = fields.output.spec;
132
+ } else if (isStateGraphArgsWithStateSchema(fields)) {
133
+ this._schemaDefinition = fields.stateSchema.spec;
134
+ this._inputDefinition = fields.input?.spec ?? this._schemaDefinition;
135
+ this._outputDefinition = fields.output?.spec ?? this._schemaDefinition;
136
+ } else if (isStateDefinition(fields) || isAnnotationRoot(fields)) {
137
+ const spec = isAnnotationRoot(fields) ? fields.spec : fields;
138
+ this._schemaDefinition = spec;
139
+ } else if (isStateGraphArgs(fields)) {
140
+ const spec = _getChannels(fields.channels);
141
+ this._schemaDefinition = spec;
142
+ } else throw new Error("Invalid StateGraph input. Make sure to pass a valid Annotation.Root or Zod schema.");
143
+ this._inputDefinition ??= this._schemaDefinition;
144
+ this._outputDefinition ??= this._schemaDefinition;
145
+ this._addSchema(this._schemaDefinition);
146
+ this._addSchema(this._inputDefinition);
147
+ this._addSchema(this._outputDefinition);
148
+ if (isInteropZodObject(contextSchema)) this._configRuntimeSchema = contextSchema;
149
+ }
150
+ get allEdges() {
151
+ return new Set([...this.edges, ...Array.from(this.waitingEdges).flatMap(([starts, end]) => starts.map((start) => [start, end]))]);
152
+ }
153
+ _addSchema(stateDefinition) {
154
+ if (this._schemaDefinitions.has(stateDefinition)) return;
155
+ this._schemaDefinitions.set(stateDefinition, stateDefinition);
156
+ for (const [key, val] of Object.entries(stateDefinition)) {
157
+ let channel;
158
+ if (typeof val === "function") channel = val();
159
+ else channel = val;
160
+ if (this.channels[key] !== void 0) {
161
+ if (this.channels[key] !== channel) {
162
+ if (channel.lc_graph_name !== "LastValue") throw new Error(`Channel "${key}" already exists with a different type.`);
163
+ }
164
+ } else this.channels[key] = channel;
165
+ }
166
+ }
167
+ addNode(...args) {
168
+ function isMultipleNodes(args$1) {
169
+ return args$1.length >= 1 && typeof args$1[0] !== "string";
170
+ }
171
+ const nodes = isMultipleNodes(args) ? Array.isArray(args[0]) ? args[0] : Object.entries(args[0]).map(([key, action]) => [
172
+ key,
173
+ action,
174
+ action[Symbol.for("langgraph.state.node")] ?? void 0
175
+ ]) : [[
176
+ args[0],
177
+ args[1],
178
+ args[2]
179
+ ]];
180
+ if (nodes.length === 0) throw new Error("No nodes provided in `addNode`");
181
+ for (const [key, action, options] of nodes) {
182
+ 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.`);
183
+ 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.`);
184
+ this.warnIfCompiled(`Adding a node to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
185
+ if (key in this.nodes) throw new Error(`Node \`${key}\` already present.`);
186
+ if (key === END || key === START) throw new Error(`Node \`${key}\` is reserved.`);
187
+ let inputSpec = this._schemaDefinition;
188
+ if (options?.input !== void 0) {
189
+ if (isInteropZodObject(options.input)) inputSpec = this._metaRegistry.getChannelsForSchema(options.input);
190
+ else if (options.input.spec !== void 0) inputSpec = options.input.spec;
191
+ }
192
+ if (inputSpec !== void 0) this._addSchema(inputSpec);
193
+ let runnable;
194
+ if (Runnable.isRunnable(action)) runnable = action;
195
+ else if (typeof action === "function") runnable = new RunnableCallable({
196
+ func: action,
197
+ name: key,
198
+ trace: false
199
+ });
200
+ else runnable = _coerceToRunnable(action);
201
+ let cachePolicy = options?.cachePolicy;
202
+ if (typeof cachePolicy === "boolean") cachePolicy = cachePolicy ? {} : void 0;
203
+ const nodeSpec = {
204
+ runnable,
205
+ retryPolicy: options?.retryPolicy,
206
+ cachePolicy,
207
+ metadata: options?.metadata,
208
+ input: inputSpec ?? this._schemaDefinition,
209
+ subgraphs: isPregelLike(runnable) ? [runnable] : options?.subgraphs,
210
+ ends: options?.ends,
211
+ defer: options?.defer
212
+ };
213
+ this.nodes[key] = nodeSpec;
214
+ }
215
+ return this;
216
+ }
217
+ addEdge(startKey, endKey) {
218
+ if (typeof startKey === "string") return super.addEdge(startKey, endKey);
219
+ 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.");
220
+ for (const start of startKey) {
221
+ if (start === END) throw new Error("END cannot be a start node");
222
+ if (!Object.keys(this.nodes).some((node) => node === start)) throw new Error(`Need to add a node named "${start}" first`);
223
+ }
224
+ if (endKey === END) throw new Error("END cannot be an end node");
225
+ if (!Object.keys(this.nodes).some((node) => node === endKey)) throw new Error(`Need to add a node named "${endKey}" first`);
226
+ this.waitingEdges.add([startKey, endKey]);
227
+ return this;
228
+ }
229
+ addSequence(nodes) {
230
+ const parsedNodes = Array.isArray(nodes) ? nodes : Object.entries(nodes).map(([key, action]) => [
231
+ key,
232
+ action,
233
+ action[Symbol.for("langgraph.state.node")] ?? void 0
234
+ ]);
235
+ if (parsedNodes.length === 0) throw new Error("Sequence requires at least one node.");
236
+ let previousNode;
237
+ for (const [key, action, options] of parsedNodes) {
238
+ if (key in this.nodes) throw new Error(`Node names must be unique: node with the name "${key}" already exists.`);
239
+ const validKey = key;
240
+ this.addNode(validKey, action, options);
241
+ if (previousNode != null) this.addEdge(previousNode, validKey);
242
+ previousNode = validKey;
243
+ }
244
+ return this;
245
+ }
246
+ compile({ checkpointer, store, cache, interruptBefore, interruptAfter, name, description } = {}) {
247
+ this.validate([...Array.isArray(interruptBefore) ? interruptBefore : [], ...Array.isArray(interruptAfter) ? interruptAfter : []]);
248
+ const outputKeys = Object.keys(this._schemaDefinitions.get(this._outputDefinition));
249
+ const outputChannels = outputKeys.length === 1 && outputKeys[0] === ROOT ? ROOT : outputKeys;
250
+ const streamKeys = Object.keys(this.channels);
251
+ const streamChannels = streamKeys.length === 1 && streamKeys[0] === ROOT ? ROOT : streamKeys;
252
+ const compiled = new CompiledStateGraph({
253
+ builder: this,
254
+ checkpointer,
255
+ interruptAfter,
256
+ interruptBefore,
257
+ autoValidate: false,
258
+ nodes: {},
259
+ channels: {
260
+ ...this.channels,
261
+ [START]: new EphemeralValue()
262
+ },
263
+ inputChannels: START,
264
+ outputChannels,
265
+ streamChannels,
266
+ streamMode: "updates",
267
+ store,
268
+ cache,
269
+ name,
270
+ description
271
+ });
272
+ compiled.attachNode(START);
273
+ for (const [key, node] of Object.entries(this.nodes)) compiled.attachNode(key, node);
274
+ compiled.attachBranch(START, SELF, _getControlBranch(), { withReader: false });
275
+ for (const [key] of Object.entries(this.nodes)) compiled.attachBranch(key, SELF, _getControlBranch(), { withReader: false });
276
+ for (const [start, end] of this.edges) compiled.attachEdge(start, end);
277
+ for (const [starts, end] of this.waitingEdges) compiled.attachEdge(starts, end);
278
+ for (const [start, branches] of Object.entries(this.branches)) for (const [name$1, branch] of Object.entries(branches)) compiled.attachBranch(start, name$1, branch);
279
+ return compiled.validate();
280
+ }
281
+ };
451
282
  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;
283
+ const channels = {};
284
+ for (const [name, val] of Object.entries(schema)) if (name === ROOT) channels[name] = getChannel(val);
285
+ else channels[name] = getChannel(val);
286
+ return channels;
463
287
  }
464
288
  /**
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
- }
289
+ * Final result from building and compiling a {@link StateGraph}.
290
+ * Should not be instantiated directly, only using the StateGraph `.compile()`
291
+ * instance method.
292
+ */
293
+ var CompiledStateGraph = class extends CompiledGraph {
294
+ /**
295
+ * The description of the compiled graph.
296
+ * This is used by the supervisor agent to describe the handoff to the agent.
297
+ */
298
+ description;
299
+ /** @internal */
300
+ _metaRegistry = schemaMetaRegistry;
301
+ constructor({ description,...rest }) {
302
+ super(rest);
303
+ this.description = description;
304
+ }
305
+ attachNode(key, node) {
306
+ let outputKeys;
307
+ if (key === START) outputKeys = Object.entries(this.builder._schemaDefinitions.get(this.builder._inputDefinition)).map(([k]) => k);
308
+ else outputKeys = Object.keys(this.builder.channels);
309
+ function _getRoot(input) {
310
+ if (isCommand(input)) {
311
+ if (input.graph === Command.PARENT) return null;
312
+ return input._updateAsTuples();
313
+ } else if (Array.isArray(input) && input.length > 0 && input.some((i) => isCommand(i))) {
314
+ const updates = [];
315
+ for (const i of input) if (isCommand(i)) {
316
+ if (i.graph === Command.PARENT) continue;
317
+ updates.push(...i._updateAsTuples());
318
+ } else updates.push([ROOT, i]);
319
+ return updates;
320
+ } else if (input != null) return [[ROOT, input]];
321
+ return null;
322
+ }
323
+ const nodeKey = key;
324
+ function _getUpdates(input) {
325
+ if (!input) return null;
326
+ else if (isCommand(input)) {
327
+ if (input.graph === Command.PARENT) return null;
328
+ return input._updateAsTuples().filter(([k]) => outputKeys.includes(k));
329
+ } else if (Array.isArray(input) && input.length > 0 && input.some(isCommand)) {
330
+ const updates = [];
331
+ for (const item of input) if (isCommand(item)) {
332
+ if (item.graph === Command.PARENT) continue;
333
+ updates.push(...item._updateAsTuples().filter(([k]) => outputKeys.includes(k)));
334
+ } else {
335
+ const itemUpdates = _getUpdates(item);
336
+ if (itemUpdates) updates.push(...itemUpdates ?? []);
337
+ }
338
+ return updates;
339
+ } else if (typeof input === "object" && !Array.isArray(input)) return Object.entries(input).filter(([k]) => outputKeys.includes(k));
340
+ else {
341
+ const typeofInput = Array.isArray(input) ? "array" : typeof input;
342
+ 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" });
343
+ }
344
+ }
345
+ const stateWriteEntries = [{
346
+ value: PASSTHROUGH,
347
+ mapper: new RunnableCallable({
348
+ func: outputKeys.length && outputKeys[0] === ROOT ? _getRoot : _getUpdates,
349
+ trace: false,
350
+ recurse: false
351
+ })
352
+ }];
353
+ if (key === START) this.nodes[key] = new PregelNode({
354
+ tags: [TAG_HIDDEN],
355
+ triggers: [START],
356
+ channels: [START],
357
+ writers: [new ChannelWrite(stateWriteEntries, [TAG_HIDDEN])]
358
+ });
359
+ else {
360
+ const inputDefinition = node?.input ?? this.builder._schemaDefinition;
361
+ const inputValues = Object.fromEntries(Object.keys(this.builder._schemaDefinitions.get(inputDefinition)).map((k) => [k, k]));
362
+ const isSingleInput = Object.keys(inputValues).length === 1 && ROOT in inputValues;
363
+ const branchChannel = `branch:to:${key}`;
364
+ this.channels[branchChannel] = node?.defer ? new LastValueAfterFinish() : new EphemeralValue(false);
365
+ this.nodes[key] = new PregelNode({
366
+ triggers: [branchChannel],
367
+ channels: isSingleInput ? Object.keys(inputValues) : inputValues,
368
+ writers: [new ChannelWrite(stateWriteEntries, [TAG_HIDDEN])],
369
+ mapper: isSingleInput ? void 0 : (input) => {
370
+ return Object.fromEntries(Object.entries(input).filter(([k]) => k in inputValues));
371
+ },
372
+ bound: node?.runnable,
373
+ metadata: node?.metadata,
374
+ retryPolicy: node?.retryPolicy,
375
+ cachePolicy: node?.cachePolicy,
376
+ subgraphs: node?.subgraphs,
377
+ ends: node?.ends
378
+ });
379
+ }
380
+ }
381
+ attachEdge(starts, end) {
382
+ if (end === END) return;
383
+ if (typeof starts === "string") this.nodes[starts].writers.push(new ChannelWrite([{
384
+ channel: `branch:to:${end}`,
385
+ value: null
386
+ }], [TAG_HIDDEN]));
387
+ else if (Array.isArray(starts)) {
388
+ const channelName = `join:${starts.join("+")}:${end}`;
389
+ this.channels[channelName] = this.builder.nodes[end].defer ? new NamedBarrierValueAfterFinish(new Set(starts)) : new NamedBarrierValue(new Set(starts));
390
+ this.nodes[end].triggers.push(channelName);
391
+ for (const start of starts) this.nodes[start].writers.push(new ChannelWrite([{
392
+ channel: channelName,
393
+ value: start
394
+ }], [TAG_HIDDEN]));
395
+ }
396
+ }
397
+ attachBranch(start, _, branch, options = { withReader: true }) {
398
+ const branchWriter = async (packets, config) => {
399
+ const filteredPackets = packets.filter((p) => p !== END);
400
+ if (!filteredPackets.length) return;
401
+ const writes = filteredPackets.map((p) => {
402
+ if (_isSend(p)) return p;
403
+ return {
404
+ channel: p === END ? p : `branch:to:${p}`,
405
+ value: start
406
+ };
407
+ });
408
+ await ChannelWrite.doWrite({
409
+ ...config,
410
+ tags: (config.tags ?? []).concat([TAG_HIDDEN])
411
+ }, writes);
412
+ };
413
+ this.nodes[start].writers.push(branch.run(branchWriter, options.withReader ? (config) => ChannelRead.doRead(config, this.streamChannels ?? this.outputChannels, true) : void 0));
414
+ }
415
+ async _validateInput(input) {
416
+ if (input == null) return input;
417
+ const schema = (() => {
418
+ const input$1 = this.builder._inputRuntimeDefinition;
419
+ const schema$1 = this.builder._schemaRuntimeDefinition;
420
+ const apply = (schema$2) => {
421
+ if (schema$2 == null) return void 0;
422
+ return this._metaRegistry.getExtendedChannelSchemas(schema$2, { withReducerSchema: true });
423
+ };
424
+ if (isInteropZodObject(input$1)) return apply(input$1);
425
+ if (input$1 === PartialStateSchema) return interopZodObjectPartial(apply(schema$1));
426
+ return void 0;
427
+ })();
428
+ if (isCommand(input)) {
429
+ const parsedInput = input;
430
+ if (input.update && schema != null) parsedInput.update = interopParse(schema, input.update);
431
+ return parsedInput;
432
+ }
433
+ if (schema != null) return interopParse(schema, input);
434
+ return input;
435
+ }
436
+ async _validateContext(config) {
437
+ const configSchema = this.builder._configRuntimeSchema;
438
+ if (isInteropZodObject(configSchema)) interopParse(configSchema, config);
439
+ return config;
440
+ }
441
+ };
699
442
  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)));
443
+ 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
444
  }
706
445
  function isAnnotationRoot(obj) {
707
- return (typeof obj === "object" &&
708
- obj !== null &&
709
- "lc_graph_name" in obj &&
710
- obj.lc_graph_name === "AnnotationRoot");
446
+ return typeof obj === "object" && obj !== null && "lc_graph_name" in obj && obj.lc_graph_name === "AnnotationRoot";
711
447
  }
712
448
  function isStateGraphArgs(obj) {
713
- return (typeof obj === "object" &&
714
- obj !== null &&
715
- obj.channels !== undefined);
449
+ return typeof obj === "object" && obj !== null && obj.channels !== void 0;
716
450
  }
717
451
  function isStateGraphArgsWithStateSchema(obj) {
718
- return (typeof obj === "object" &&
719
- obj !== null &&
720
- obj.stateSchema !== undefined);
452
+ return typeof obj === "object" && obj !== null && obj.stateSchema !== void 0;
721
453
  }
722
454
  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);
455
+ return typeof obj === "object" && obj !== null && obj.stateSchema === void 0 && obj.input !== void 0 && obj.output !== void 0;
729
456
  }
730
457
  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;
458
+ if (typeof value !== "object" || value == null) return false;
459
+ if (!("state" in value) || !isInteropZodObject(value.state)) return false;
460
+ if ("input" in value && !isInteropZodObject(value.input)) return false;
461
+ if ("output" in value && !isInteropZodObject(value.output)) return false;
462
+ return true;
744
463
  }
745
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
746
464
  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;
465
+ if (_isSend(value)) return [value];
466
+ const commands = [];
467
+ if (isCommand(value)) commands.push(value);
468
+ else if (Array.isArray(value)) commands.push(...value.filter(isCommand));
469
+ const destinations = [];
470
+ for (const command of commands) {
471
+ if (command.graph === Command.PARENT) throw new ParentCommand(command);
472
+ if (_isSend(command.goto)) destinations.push(command.goto);
473
+ else if (typeof command.goto === "string") destinations.push(command.goto);
474
+ else if (Array.isArray(command.goto)) destinations.push(...command.goto);
475
+ }
476
+ return destinations;
775
477
  }
776
478
  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
- });
479
+ const CONTROL_BRANCH_PATH = new RunnableCallable({
480
+ func: _controlBranch,
481
+ tags: [TAG_HIDDEN],
482
+ trace: false,
483
+ recurse: false,
484
+ name: "<control_branch>"
485
+ });
486
+ return new Branch({ path: CONTROL_BRANCH_PATH });
788
487
  }
789
- export function typedNode(_state, _options) {
790
- return (func, options) => {
791
- Object.assign(func, { [Symbol.for("langgraph.state.node")]: options });
792
- return func;
793
- };
488
+ function typedNode(_state, _options) {
489
+ return (func, options) => {
490
+ Object.assign(func, { [Symbol.for("langgraph.state.node")]: options });
491
+ return func;
492
+ };
794
493
  }
494
+
495
+ //#endregion
496
+ export { CompiledStateGraph, StateGraph, typedNode };
795
497
  //# sourceMappingURL=state.js.map