@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,721 +1,420 @@
1
- /* eslint-disable @typescript-eslint/no-use-before-define */
2
- import { _coerceToRunnable, Runnable, } from "@langchain/core/runnables";
3
- import { Graph as DrawableGraph, } from "@langchain/core/runnables/graph";
4
- import { z } from "zod";
5
- import { validate as isUuid } from "uuid";
1
+ import { InvalidUpdateError, NodeInterrupt, UnreachableNodeError } from "../errors.js";
2
+ import { CHECKPOINT_NAMESPACE_END, CHECKPOINT_NAMESPACE_SEPARATOR, END, START, TAG_HIDDEN, _isSend } from "../constants.js";
3
+ import { RunnableCallable, gatherIterator, gatherIteratorSync } from "../utils.js";
4
+ import { ChannelWrite, PASSTHROUGH } from "../pregel/write.js";
6
5
  import { PregelNode } from "../pregel/read.js";
6
+ import { isPregelLike } from "../pregel/utils/subgraph.js";
7
7
  import { Channel, Pregel } from "../pregel/index.js";
8
8
  import { EphemeralValue } from "../channels/ephemeral_value.js";
9
- import { ChannelWrite, PASSTHROUGH } from "../pregel/write.js";
10
- import { _isSend, CHECKPOINT_NAMESPACE_END, CHECKPOINT_NAMESPACE_SEPARATOR, END, START, TAG_HIDDEN, } from "../constants.js";
11
- import { gatherIterator, gatherIteratorSync, RunnableCallable, } from "../utils.js";
12
- import { InvalidUpdateError, NodeInterrupt, UnreachableNodeError, } from "../errors.js";
13
- import { isPregelLike } from "../pregel/utils/subgraph.js";
14
- export class Branch {
15
- constructor(options) {
16
- Object.defineProperty(this, "path", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: void 0
21
- });
22
- Object.defineProperty(this, "ends", {
23
- enumerable: true,
24
- configurable: true,
25
- writable: true,
26
- value: void 0
27
- });
28
- if (Runnable.isRunnable(options.path)) {
29
- this.path = options.path;
30
- }
31
- else {
32
- this.path = _coerceToRunnable(options.path).withConfig({ runName: `Branch` });
33
- }
34
- this.ends = Array.isArray(options.pathMap)
35
- ? options.pathMap.reduce((acc, n) => {
36
- acc[n] = n;
37
- return acc;
38
- }, {})
39
- : options.pathMap;
40
- }
41
- run(writer, reader) {
42
- return ChannelWrite.registerWriter(new RunnableCallable({
43
- name: "<branch_run>",
44
- trace: false,
45
- func: async (input, config) => {
46
- try {
47
- return await this._route(input, config, writer, reader);
48
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
49
- }
50
- catch (e) {
51
- // Detect & warn if NodeInterrupt is thrown in a conditional edge
52
- if (e.name === NodeInterrupt.unminifiable_name) {
53
- console.warn("[WARN]: 'NodeInterrupt' thrown in conditional edge. This is likely a bug in your graph implementation.\n" +
54
- "NodeInterrupt should only be thrown inside a node, not in edge conditions.");
55
- }
56
- throw e;
57
- }
58
- },
59
- }));
60
- }
61
- async _route(input, config, writer, reader
62
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
- ) {
64
- let result = await this.path.invoke(reader ? reader(config) : input, config);
65
- if (!Array.isArray(result)) {
66
- result = [result];
67
- }
68
- let destinations;
69
- if (this.ends) {
70
- destinations = result.map((r) => (_isSend(r) ? r : this.ends[r]));
71
- }
72
- else {
73
- destinations = result;
74
- }
75
- if (destinations.some((dest) => !dest)) {
76
- throw new Error("Branch condition returned unknown or null destination");
77
- }
78
- if (destinations.filter(_isSend).some((packet) => packet.node === END)) {
79
- throw new InvalidUpdateError("Cannot send a packet to the END node");
80
- }
81
- const writeResult = await writer(destinations, config);
82
- return writeResult ?? input;
83
- }
84
- }
85
- export class Graph {
86
- constructor() {
87
- Object.defineProperty(this, "nodes", {
88
- enumerable: true,
89
- configurable: true,
90
- writable: true,
91
- value: void 0
92
- });
93
- Object.defineProperty(this, "edges", {
94
- enumerable: true,
95
- configurable: true,
96
- writable: true,
97
- value: void 0
98
- });
99
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
- Object.defineProperty(this, "branches", {
101
- enumerable: true,
102
- configurable: true,
103
- writable: true,
104
- value: void 0
105
- });
106
- Object.defineProperty(this, "entryPoint", {
107
- enumerable: true,
108
- configurable: true,
109
- writable: true,
110
- value: void 0
111
- });
112
- Object.defineProperty(this, "compiled", {
113
- enumerable: true,
114
- configurable: true,
115
- writable: true,
116
- value: false
117
- });
118
- this.nodes = {};
119
- this.edges = new Set();
120
- this.branches = {};
121
- }
122
- warnIfCompiled(message) {
123
- if (this.compiled) {
124
- console.warn(message);
125
- }
126
- }
127
- get allEdges() {
128
- return this.edges;
129
- }
130
- addNode(...args) {
131
- function isMutlipleNodes(args) {
132
- return args.length >= 1 && typeof args[0] !== "string";
133
- }
134
- const nodes = (isMutlipleNodes(args) // eslint-disable-line no-nested-ternary
135
- ? Array.isArray(args[0])
136
- ? args[0]
137
- : Object.entries(args[0])
138
- : [[args[0], args[1], args[2]]]);
139
- if (nodes.length === 0) {
140
- throw new Error("No nodes provided in `addNode`");
141
- }
142
- for (const [key, action, options] of nodes) {
143
- for (const reservedChar of [
144
- CHECKPOINT_NAMESPACE_SEPARATOR,
145
- CHECKPOINT_NAMESPACE_END,
146
- ]) {
147
- if (key.includes(reservedChar)) {
148
- throw new Error(`"${reservedChar}" is a reserved character and is not allowed in node names.`);
149
- }
150
- }
151
- this.warnIfCompiled(`Adding a node to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
152
- if (key in this.nodes) {
153
- throw new Error(`Node \`${key}\` already present.`);
154
- }
155
- if (key === END) {
156
- throw new Error(`Node \`${key}\` is reserved.`);
157
- }
158
- const runnable = _coerceToRunnable(
159
- // Account for arbitrary state due to Send API
160
- action);
161
- this.nodes[key] = {
162
- runnable,
163
- metadata: options?.metadata,
164
- subgraphs: isPregelLike(runnable) ? [runnable] : options?.subgraphs,
165
- ends: options?.ends,
166
- };
167
- }
168
- return this;
169
- }
170
- addEdge(startKey, endKey) {
171
- this.warnIfCompiled(`Adding an edge to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
172
- if (startKey === END) {
173
- throw new Error("END cannot be a start node");
174
- }
175
- if (endKey === START) {
176
- throw new Error("START cannot be an end node");
177
- }
178
- if (Array.from(this.edges).some(([start]) => start === startKey) &&
179
- !("channels" in this)) {
180
- throw new Error(`Already found path for ${startKey}. For multiple edges, use StateGraph.`);
181
- }
182
- this.edges.add([startKey, endKey]);
183
- return this;
184
- }
185
- addConditionalEdges(source, path, pathMap) {
186
- const options = typeof source === "object" ? source : { source, path: path, pathMap };
187
- this.warnIfCompiled("Adding an edge to a graph that has already been compiled. This will not be reflected in the compiled graph.");
188
- if (!Runnable.isRunnable(options.path)) {
189
- const pathDisplayValues = Array.isArray(options.pathMap)
190
- ? options.pathMap.join(",")
191
- : Object.keys(options.pathMap ?? {}).join(",");
192
- options.path = _coerceToRunnable(options.path).withConfig({
193
- runName: `Branch<${options.source}${pathDisplayValues !== "" ? `,${pathDisplayValues}` : ""}>`.slice(0, 63),
194
- });
195
- }
196
- // find a name for condition
197
- const name = options.path.getName() === "RunnableLambda"
198
- ? "condition"
199
- : options.path.getName();
200
- // validate condition
201
- if (this.branches[options.source] && this.branches[options.source][name]) {
202
- throw new Error(`Condition \`${name}\` already present for node \`${source}\``);
203
- }
204
- // save it
205
- this.branches[options.source] ??= {};
206
- this.branches[options.source][name] = new Branch(options);
207
- return this;
208
- }
209
- /**
210
- * @deprecated use `addEdge(START, key)` instead
211
- */
212
- setEntryPoint(key) {
213
- this.warnIfCompiled("Setting the entry point of a graph that has already been compiled. This will not be reflected in the compiled graph.");
214
- return this.addEdge(START, key);
215
- }
216
- /**
217
- * @deprecated use `addEdge(key, END)` instead
218
- */
219
- setFinishPoint(key) {
220
- this.warnIfCompiled("Setting a finish point of a graph that has already been compiled. This will not be reflected in the compiled graph.");
221
- return this.addEdge(key, END);
222
- }
223
- compile({ checkpointer, interruptBefore, interruptAfter, name, } = {}) {
224
- // validate the graph
225
- this.validate([
226
- ...(Array.isArray(interruptBefore) ? interruptBefore : []),
227
- ...(Array.isArray(interruptAfter) ? interruptAfter : []),
228
- ]);
229
- // create empty compiled graph
230
- const compiled = new CompiledGraph({
231
- builder: this,
232
- checkpointer,
233
- interruptAfter,
234
- interruptBefore,
235
- autoValidate: false,
236
- nodes: {},
237
- channels: {
238
- [START]: new EphemeralValue(),
239
- [END]: new EphemeralValue(),
240
- },
241
- inputChannels: START,
242
- outputChannels: END,
243
- streamChannels: [],
244
- streamMode: "values",
245
- name,
246
- });
247
- // attach nodes, edges and branches
248
- for (const [key, node] of Object.entries(this.nodes)) {
249
- compiled.attachNode(key, node);
250
- }
251
- for (const [start, end] of this.edges) {
252
- compiled.attachEdge(start, end);
253
- }
254
- for (const [start, branches] of Object.entries(this.branches)) {
255
- for (const [name, branch] of Object.entries(branches)) {
256
- compiled.attachBranch(start, name, branch);
257
- }
258
- }
259
- return compiled.validate();
260
- }
261
- validate(interrupt) {
262
- // assemble sources
263
- const allSources = new Set([...this.allEdges].map(([src, _]) => src));
264
- for (const [start] of Object.entries(this.branches)) {
265
- allSources.add(start);
266
- }
267
- // validate sources
268
- for (const source of allSources) {
269
- if (source !== START && !(source in this.nodes)) {
270
- throw new Error(`Found edge starting at unknown node \`${source}\``);
271
- }
272
- }
273
- // assemble targets
274
- const allTargets = new Set([...this.allEdges].map(([_, target]) => target));
275
- for (const [start, branches] of Object.entries(this.branches)) {
276
- for (const branch of Object.values(branches)) {
277
- if (branch.ends != null) {
278
- for (const end of Object.values(branch.ends)) {
279
- allTargets.add(end);
280
- }
281
- }
282
- else {
283
- allTargets.add(END);
284
- for (const node of Object.keys(this.nodes)) {
285
- if (node !== start) {
286
- allTargets.add(node);
287
- }
288
- }
289
- }
290
- }
291
- }
292
- for (const node of Object.values(this.nodes)) {
293
- for (const target of node.ends ?? []) {
294
- allTargets.add(target);
295
- }
296
- }
297
- // validate targets
298
- for (const node of Object.keys(this.nodes)) {
299
- if (!allTargets.has(node)) {
300
- throw new UnreachableNodeError([
301
- `Node \`${node}\` is not reachable.`,
302
- "",
303
- "If you are returning Command objects from your node,",
304
- 'make sure you are passing names of potential destination nodes as an "ends" array',
305
- 'into ".addNode(..., { ends: ["node1", "node2"] })".',
306
- ].join("\n"), {
307
- lc_error_code: "UNREACHABLE_NODE",
308
- });
309
- }
310
- }
311
- for (const target of allTargets) {
312
- if (target !== END && !(target in this.nodes)) {
313
- throw new Error(`Found edge ending at unknown node \`${target}\``);
314
- }
315
- }
316
- // validate interrupts
317
- if (interrupt) {
318
- for (const node of interrupt) {
319
- if (!(node in this.nodes)) {
320
- throw new Error(`Interrupt node \`${node}\` is not present`);
321
- }
322
- }
323
- }
324
- this.compiled = true;
325
- }
326
- }
327
- export class CompiledGraph extends Pregel {
328
- constructor({ builder, ...rest }) {
329
- super(rest);
330
- Object.defineProperty(this, "builder", {
331
- enumerable: true,
332
- configurable: true,
333
- writable: true,
334
- value: void 0
335
- });
336
- this.builder = builder;
337
- }
338
- attachNode(key, node) {
339
- this.channels[key] = new EphemeralValue();
340
- this.nodes[key] = new PregelNode({
341
- channels: [],
342
- triggers: [],
343
- metadata: node.metadata,
344
- subgraphs: node.subgraphs,
345
- ends: node.ends,
346
- })
347
- .pipe(node.runnable)
348
- .pipe(new ChannelWrite([{ channel: key, value: PASSTHROUGH }], [TAG_HIDDEN]));
349
- this.streamChannels.push(key);
350
- }
351
- attachEdge(start, end) {
352
- if (end === END) {
353
- if (start === START) {
354
- throw new Error("Cannot have an edge from START to END");
355
- }
356
- this.nodes[start].writers.push(new ChannelWrite([{ channel: END, value: PASSTHROUGH }], [TAG_HIDDEN]));
357
- }
358
- else {
359
- this.nodes[end].triggers.push(start);
360
- this.nodes[end].channels.push(start);
361
- }
362
- }
363
- attachBranch(start, name, branch) {
364
- // add hidden start node
365
- if (start === START && !this.nodes[START]) {
366
- this.nodes[START] = Channel.subscribeTo(START, { tags: [TAG_HIDDEN] });
367
- }
368
- // attach branch writer
369
- this.nodes[start].pipe(branch.run((dests) => {
370
- const writes = dests.map((dest) => {
371
- if (_isSend(dest)) {
372
- return dest;
373
- }
374
- return {
375
- channel: dest === END ? END : `branch:${start}:${name}:${dest}`,
376
- value: PASSTHROUGH,
377
- };
378
- });
379
- return new ChannelWrite(writes, [TAG_HIDDEN]);
380
- }));
381
- // attach branch readers
382
- const ends = branch.ends
383
- ? Object.values(branch.ends)
384
- : Object.keys(this.nodes);
385
- for (const end of ends) {
386
- if (end !== END) {
387
- const channelName = `branch:${start}:${name}:${end}`;
388
- this.channels[channelName] =
389
- new EphemeralValue();
390
- this.nodes[end].triggers.push(channelName);
391
- this.nodes[end].channels.push(channelName);
392
- }
393
- }
394
- }
395
- /**
396
- * Returns a drawable representation of the computation graph.
397
- */
398
- async getGraphAsync(config) {
399
- const xray = config?.xray;
400
- const graph = new DrawableGraph();
401
- const startNodes = {
402
- [START]: graph.addNode({
403
- schema: z.any(),
404
- }, START),
405
- };
406
- const endNodes = {};
407
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
408
- let subgraphs = {};
409
- if (xray) {
410
- subgraphs = Object.fromEntries((await gatherIterator(this.getSubgraphsAsync())).filter(
411
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
412
- (x) => isCompiledGraph(x[1])));
413
- }
414
- function addEdge(start, end, label, conditional = false) {
415
- if (end === END && endNodes[END] === undefined) {
416
- endNodes[END] = graph.addNode({ schema: z.any() }, END);
417
- }
418
- if (startNodes[start] === undefined) {
419
- return;
420
- }
421
- if (endNodes[end] === undefined) {
422
- throw new Error(`End node ${end} not found!`);
423
- }
424
- return graph.addEdge(startNodes[start], endNodes[end], label !== end ? label : undefined, conditional);
425
- }
426
- for (const [key, nodeSpec] of Object.entries(this.builder.nodes)) {
427
- const displayKey = _escapeMermaidKeywords(key);
428
- const node = nodeSpec.runnable;
429
- const metadata = nodeSpec.metadata ?? {};
430
- if (this.interruptBefore?.includes(key) &&
431
- this.interruptAfter?.includes(key)) {
432
- metadata.__interrupt = "before,after";
433
- }
434
- else if (this.interruptBefore?.includes(key)) {
435
- metadata.__interrupt = "before";
436
- }
437
- else if (this.interruptAfter?.includes(key)) {
438
- metadata.__interrupt = "after";
439
- }
440
- if (xray) {
441
- const newXrayValue = typeof xray === "number" ? xray - 1 : xray;
442
- const drawableSubgraph = subgraphs[key] !== undefined
443
- ? await subgraphs[key].getGraphAsync({
444
- ...config,
445
- xray: newXrayValue,
446
- })
447
- : node.getGraph(config);
448
- drawableSubgraph.trimFirstNode();
449
- drawableSubgraph.trimLastNode();
450
- if (Object.keys(drawableSubgraph.nodes).length > 1) {
451
- const [e, s] = graph.extend(drawableSubgraph, displayKey);
452
- if (e === undefined) {
453
- throw new Error(`Could not extend subgraph "${key}" due to missing entrypoint.`);
454
- }
455
- // TODO: Remove default name once we stop supporting core 0.2.0
456
- // eslint-disable-next-line no-inner-declarations
457
- function _isRunnableInterface(
458
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
459
- thing) {
460
- return thing ? thing.lc_runnable : false;
461
- }
462
- // eslint-disable-next-line no-inner-declarations
463
- function _nodeDataStr(id, data) {
464
- if (id !== undefined && !isUuid(id)) {
465
- return id;
466
- }
467
- else if (_isRunnableInterface(data)) {
468
- try {
469
- let dataStr = data.getName();
470
- dataStr = dataStr.startsWith("Runnable")
471
- ? dataStr.slice("Runnable".length)
472
- : dataStr;
473
- return dataStr;
474
- }
475
- catch (error) {
476
- return data.getName();
477
- }
478
- }
479
- else {
480
- return data.name ?? "UnknownSchema";
481
- }
482
- }
483
- // TODO: Remove casts when we stop supporting core 0.2.0
484
- if (s !== undefined) {
485
- startNodes[displayKey] = {
486
- name: _nodeDataStr(s.id, s.data),
487
- ...s,
488
- };
489
- }
490
- endNodes[displayKey] = {
491
- name: _nodeDataStr(e.id, e.data),
492
- ...e,
493
- };
494
- }
495
- else {
496
- // TODO: Remove when we stop supporting core 0.2.0
497
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
498
- // @ts-ignore
499
- const newNode = graph.addNode(node, displayKey, metadata);
500
- startNodes[displayKey] = newNode;
501
- endNodes[displayKey] = newNode;
502
- }
503
- }
504
- else {
505
- // TODO: Remove when we stop supporting core 0.2.0
506
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
507
- // @ts-ignore
508
- const newNode = graph.addNode(node, displayKey, metadata);
509
- startNodes[displayKey] = newNode;
510
- endNodes[displayKey] = newNode;
511
- }
512
- }
513
- const sortedEdges = [...this.builder.allEdges].sort(([a], [b]) => {
514
- if (a < b) {
515
- return -1;
516
- }
517
- else if (b > a) {
518
- return 1;
519
- }
520
- else {
521
- return 0;
522
- }
523
- });
524
- for (const [start, end] of sortedEdges) {
525
- addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end));
526
- }
527
- for (const [start, branches] of Object.entries(this.builder.branches)) {
528
- const defaultEnds = {
529
- ...Object.fromEntries(Object.keys(this.builder.nodes)
530
- .filter((k) => k !== start)
531
- .map((k) => [_escapeMermaidKeywords(k), _escapeMermaidKeywords(k)])),
532
- [END]: END,
533
- };
534
- for (const branch of Object.values(branches)) {
535
- let ends;
536
- if (branch.ends !== undefined) {
537
- ends = branch.ends;
538
- }
539
- else {
540
- ends = defaultEnds;
541
- }
542
- for (const [label, end] of Object.entries(ends)) {
543
- addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end), label, true);
544
- }
545
- }
546
- }
547
- for (const [key, node] of Object.entries(this.builder.nodes)) {
548
- if (node.ends !== undefined) {
549
- for (const end of node.ends) {
550
- addEdge(_escapeMermaidKeywords(key), _escapeMermaidKeywords(end), undefined, true);
551
- }
552
- }
553
- }
554
- return graph;
555
- }
556
- /**
557
- * Returns a drawable representation of the computation graph.
558
- *
559
- * @deprecated Use getGraphAsync instead. The async method will be the default in the next minor core release.
560
- */
561
- getGraph(config) {
562
- const xray = config?.xray;
563
- const graph = new DrawableGraph();
564
- const startNodes = {
565
- [START]: graph.addNode({
566
- schema: z.any(),
567
- }, START),
568
- };
569
- const endNodes = {};
570
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
571
- let subgraphs = {};
572
- if (xray) {
573
- subgraphs = Object.fromEntries(gatherIteratorSync(this.getSubgraphs()).filter(
574
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
575
- (x) => isCompiledGraph(x[1])));
576
- }
577
- function addEdge(start, end, label, conditional = false) {
578
- if (end === END && endNodes[END] === undefined) {
579
- endNodes[END] = graph.addNode({ schema: z.any() }, END);
580
- }
581
- return graph.addEdge(startNodes[start], endNodes[end], label !== end ? label : undefined, conditional);
582
- }
583
- for (const [key, nodeSpec] of Object.entries(this.builder.nodes)) {
584
- const displayKey = _escapeMermaidKeywords(key);
585
- const node = nodeSpec.runnable;
586
- const metadata = nodeSpec.metadata ?? {};
587
- if (this.interruptBefore?.includes(key) &&
588
- this.interruptAfter?.includes(key)) {
589
- metadata.__interrupt = "before,after";
590
- }
591
- else if (this.interruptBefore?.includes(key)) {
592
- metadata.__interrupt = "before";
593
- }
594
- else if (this.interruptAfter?.includes(key)) {
595
- metadata.__interrupt = "after";
596
- }
597
- if (xray) {
598
- const newXrayValue = typeof xray === "number" ? xray - 1 : xray;
599
- const drawableSubgraph = subgraphs[key] !== undefined
600
- ? subgraphs[key].getGraph({
601
- ...config,
602
- xray: newXrayValue,
603
- })
604
- : node.getGraph(config);
605
- drawableSubgraph.trimFirstNode();
606
- drawableSubgraph.trimLastNode();
607
- if (Object.keys(drawableSubgraph.nodes).length > 1) {
608
- const [e, s] = graph.extend(drawableSubgraph, displayKey);
609
- if (e === undefined) {
610
- throw new Error(`Could not extend subgraph "${key}" due to missing entrypoint.`);
611
- }
612
- // TODO: Remove default name once we stop supporting core 0.2.0
613
- // eslint-disable-next-line no-inner-declarations
614
- function _isRunnableInterface(
615
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
616
- thing) {
617
- return thing ? thing.lc_runnable : false;
618
- }
619
- // eslint-disable-next-line no-inner-declarations
620
- function _nodeDataStr(id, data) {
621
- if (id !== undefined && !isUuid(id)) {
622
- return id;
623
- }
624
- else if (_isRunnableInterface(data)) {
625
- try {
626
- let dataStr = data.getName();
627
- dataStr = dataStr.startsWith("Runnable")
628
- ? dataStr.slice("Runnable".length)
629
- : dataStr;
630
- return dataStr;
631
- }
632
- catch (error) {
633
- return data.getName();
634
- }
635
- }
636
- else {
637
- return data.name ?? "UnknownSchema";
638
- }
639
- }
640
- // TODO: Remove casts when we stop supporting core 0.2.0
641
- if (s !== undefined) {
642
- startNodes[displayKey] = {
643
- name: _nodeDataStr(s.id, s.data),
644
- ...s,
645
- };
646
- }
647
- endNodes[displayKey] = {
648
- name: _nodeDataStr(e.id, e.data),
649
- ...e,
650
- };
651
- }
652
- else {
653
- // TODO: Remove when we stop supporting core 0.2.0
654
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
655
- // @ts-ignore
656
- const newNode = graph.addNode(node, displayKey, metadata);
657
- startNodes[displayKey] = newNode;
658
- endNodes[displayKey] = newNode;
659
- }
660
- }
661
- else {
662
- // TODO: Remove when we stop supporting core 0.2.0
663
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
664
- // @ts-ignore
665
- const newNode = graph.addNode(node, displayKey, metadata);
666
- startNodes[displayKey] = newNode;
667
- endNodes[displayKey] = newNode;
668
- }
669
- }
670
- const sortedEdges = [...this.builder.allEdges].sort(([a], [b]) => {
671
- if (a < b) {
672
- return -1;
673
- }
674
- else if (b > a) {
675
- return 1;
676
- }
677
- else {
678
- return 0;
679
- }
680
- });
681
- for (const [start, end] of sortedEdges) {
682
- addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end));
683
- }
684
- for (const [start, branches] of Object.entries(this.builder.branches)) {
685
- const defaultEnds = {
686
- ...Object.fromEntries(Object.keys(this.builder.nodes)
687
- .filter((k) => k !== start)
688
- .map((k) => [_escapeMermaidKeywords(k), _escapeMermaidKeywords(k)])),
689
- [END]: END,
690
- };
691
- for (const branch of Object.values(branches)) {
692
- let ends;
693
- if (branch.ends !== undefined) {
694
- ends = branch.ends;
695
- }
696
- else {
697
- ends = defaultEnds;
698
- }
699
- for (const [label, end] of Object.entries(ends)) {
700
- addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end), label, true);
701
- }
702
- }
703
- }
704
- return graph;
705
- }
706
- }
707
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ import { Runnable, _coerceToRunnable } from "@langchain/core/runnables";
10
+ import { Graph } from "@langchain/core/runnables/graph";
11
+ import { z } from "zod/v4";
12
+ import { validate } from "uuid";
13
+
14
+ //#region src/graph/graph.ts
15
+ var Branch = class {
16
+ path;
17
+ ends;
18
+ constructor(options) {
19
+ if (Runnable.isRunnable(options.path)) this.path = options.path;
20
+ else this.path = _coerceToRunnable(options.path).withConfig({ runName: `Branch` });
21
+ this.ends = Array.isArray(options.pathMap) ? options.pathMap.reduce((acc, n) => {
22
+ acc[n] = n;
23
+ return acc;
24
+ }, {}) : options.pathMap;
25
+ }
26
+ run(writer, reader) {
27
+ return ChannelWrite.registerWriter(new RunnableCallable({
28
+ name: "<branch_run>",
29
+ trace: false,
30
+ func: async (input, config) => {
31
+ try {
32
+ return await this._route(input, config, writer, reader);
33
+ } catch (e) {
34
+ if (e.name === NodeInterrupt.unminifiable_name) console.warn("[WARN]: 'NodeInterrupt' thrown in conditional edge. This is likely a bug in your graph implementation.\nNodeInterrupt should only be thrown inside a node, not in edge conditions.");
35
+ throw e;
36
+ }
37
+ }
38
+ }));
39
+ }
40
+ async _route(input, config, writer, reader) {
41
+ let result = await this.path.invoke(reader ? reader(config) : input, config);
42
+ if (!Array.isArray(result)) result = [result];
43
+ let destinations;
44
+ if (this.ends) destinations = result.map((r) => _isSend(r) ? r : this.ends[r]);
45
+ else destinations = result;
46
+ if (destinations.some((dest) => !dest)) throw new Error("Branch condition returned unknown or null destination");
47
+ if (destinations.filter(_isSend).some((packet) => packet.node === END)) throw new InvalidUpdateError("Cannot send a packet to the END node");
48
+ const writeResult = await writer(destinations, config);
49
+ return writeResult ?? input;
50
+ }
51
+ };
52
+ var Graph$1 = class {
53
+ nodes;
54
+ edges;
55
+ branches;
56
+ entryPoint;
57
+ compiled = false;
58
+ constructor() {
59
+ this.nodes = {};
60
+ this.edges = /* @__PURE__ */ new Set();
61
+ this.branches = {};
62
+ }
63
+ warnIfCompiled(message) {
64
+ if (this.compiled) console.warn(message);
65
+ }
66
+ get allEdges() {
67
+ return this.edges;
68
+ }
69
+ addNode(...args) {
70
+ function isMutlipleNodes(args$1) {
71
+ return args$1.length >= 1 && typeof args$1[0] !== "string";
72
+ }
73
+ const nodes = isMutlipleNodes(args) ? Array.isArray(args[0]) ? args[0] : Object.entries(args[0]) : [[
74
+ args[0],
75
+ args[1],
76
+ args[2]
77
+ ]];
78
+ if (nodes.length === 0) throw new Error("No nodes provided in `addNode`");
79
+ for (const [key, action, options] of nodes) {
80
+ 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.`);
81
+ this.warnIfCompiled(`Adding a node to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
82
+ if (key in this.nodes) throw new Error(`Node \`${key}\` already present.`);
83
+ if (key === END) throw new Error(`Node \`${key}\` is reserved.`);
84
+ const runnable = _coerceToRunnable(action);
85
+ this.nodes[key] = {
86
+ runnable,
87
+ metadata: options?.metadata,
88
+ subgraphs: isPregelLike(runnable) ? [runnable] : options?.subgraphs,
89
+ ends: options?.ends
90
+ };
91
+ }
92
+ return this;
93
+ }
94
+ addEdge(startKey, endKey) {
95
+ this.warnIfCompiled(`Adding an edge to a graph that has already been compiled. This will not be reflected in the compiled graph.`);
96
+ if (startKey === END) throw new Error("END cannot be a start node");
97
+ if (endKey === START) throw new Error("START cannot be an end node");
98
+ if (Array.from(this.edges).some(([start]) => start === startKey) && !("channels" in this)) throw new Error(`Already found path for ${startKey}. For multiple edges, use StateGraph.`);
99
+ this.edges.add([startKey, endKey]);
100
+ return this;
101
+ }
102
+ addConditionalEdges(source, path, pathMap) {
103
+ const options = typeof source === "object" ? source : {
104
+ source,
105
+ path,
106
+ pathMap
107
+ };
108
+ this.warnIfCompiled("Adding an edge to a graph that has already been compiled. This will not be reflected in the compiled graph.");
109
+ if (!Runnable.isRunnable(options.path)) {
110
+ const pathDisplayValues = Array.isArray(options.pathMap) ? options.pathMap.join(",") : Object.keys(options.pathMap ?? {}).join(",");
111
+ options.path = _coerceToRunnable(options.path).withConfig({ runName: `Branch<${options.source}${pathDisplayValues !== "" ? `,${pathDisplayValues}` : ""}>`.slice(0, 63) });
112
+ }
113
+ const name = options.path.getName() === "RunnableLambda" ? "condition" : options.path.getName();
114
+ if (this.branches[options.source] && this.branches[options.source][name]) throw new Error(`Condition \`${name}\` already present for node \`${source}\``);
115
+ this.branches[options.source] ??= {};
116
+ this.branches[options.source][name] = new Branch(options);
117
+ return this;
118
+ }
119
+ /**
120
+ * @deprecated use `addEdge(START, key)` instead
121
+ */
122
+ setEntryPoint(key) {
123
+ this.warnIfCompiled("Setting the entry point of a graph that has already been compiled. This will not be reflected in the compiled graph.");
124
+ return this.addEdge(START, key);
125
+ }
126
+ /**
127
+ * @deprecated use `addEdge(key, END)` instead
128
+ */
129
+ setFinishPoint(key) {
130
+ this.warnIfCompiled("Setting a finish point of a graph that has already been compiled. This will not be reflected in the compiled graph.");
131
+ return this.addEdge(key, END);
132
+ }
133
+ compile({ checkpointer, interruptBefore, interruptAfter, name } = {}) {
134
+ this.validate([...Array.isArray(interruptBefore) ? interruptBefore : [], ...Array.isArray(interruptAfter) ? interruptAfter : []]);
135
+ const compiled = new CompiledGraph({
136
+ builder: this,
137
+ checkpointer,
138
+ interruptAfter,
139
+ interruptBefore,
140
+ autoValidate: false,
141
+ nodes: {},
142
+ channels: {
143
+ [START]: new EphemeralValue(),
144
+ [END]: new EphemeralValue()
145
+ },
146
+ inputChannels: START,
147
+ outputChannels: END,
148
+ streamChannels: [],
149
+ streamMode: "values",
150
+ name
151
+ });
152
+ for (const [key, node] of Object.entries(this.nodes)) compiled.attachNode(key, node);
153
+ for (const [start, end] of this.edges) compiled.attachEdge(start, end);
154
+ for (const [start, branches] of Object.entries(this.branches)) for (const [name$1, branch] of Object.entries(branches)) compiled.attachBranch(start, name$1, branch);
155
+ return compiled.validate();
156
+ }
157
+ validate(interrupt) {
158
+ const allSources = new Set([...this.allEdges].map(([src, _]) => src));
159
+ for (const [start] of Object.entries(this.branches)) allSources.add(start);
160
+ for (const source of allSources) if (source !== START && !(source in this.nodes)) throw new Error(`Found edge starting at unknown node \`${source}\``);
161
+ const allTargets = new Set([...this.allEdges].map(([_, target]) => target));
162
+ for (const [start, branches] of Object.entries(this.branches)) for (const branch of Object.values(branches)) if (branch.ends != null) for (const end of Object.values(branch.ends)) allTargets.add(end);
163
+ else {
164
+ allTargets.add(END);
165
+ for (const node of Object.keys(this.nodes)) if (node !== start) allTargets.add(node);
166
+ }
167
+ for (const node of Object.values(this.nodes)) for (const target of node.ends ?? []) allTargets.add(target);
168
+ for (const node of Object.keys(this.nodes)) if (!allTargets.has(node)) throw new UnreachableNodeError([
169
+ `Node \`${node}\` is not reachable.`,
170
+ "",
171
+ "If you are returning Command objects from your node,",
172
+ "make sure you are passing names of potential destination nodes as an \"ends\" array",
173
+ "into \".addNode(..., { ends: [\"node1\", \"node2\"] })\"."
174
+ ].join("\n"), { lc_error_code: "UNREACHABLE_NODE" });
175
+ for (const target of allTargets) if (target !== END && !(target in this.nodes)) throw new Error(`Found edge ending at unknown node \`${target}\``);
176
+ if (interrupt) {
177
+ for (const node of interrupt) if (!(node in this.nodes)) throw new Error(`Interrupt node \`${node}\` is not present`);
178
+ }
179
+ this.compiled = true;
180
+ }
181
+ };
182
+ var CompiledGraph = class extends Pregel {
183
+ builder;
184
+ constructor({ builder,...rest }) {
185
+ super(rest);
186
+ this.builder = builder;
187
+ }
188
+ attachNode(key, node) {
189
+ this.channels[key] = new EphemeralValue();
190
+ this.nodes[key] = new PregelNode({
191
+ channels: [],
192
+ triggers: [],
193
+ metadata: node.metadata,
194
+ subgraphs: node.subgraphs,
195
+ ends: node.ends
196
+ }).pipe(node.runnable).pipe(new ChannelWrite([{
197
+ channel: key,
198
+ value: PASSTHROUGH
199
+ }], [TAG_HIDDEN]));
200
+ this.streamChannels.push(key);
201
+ }
202
+ attachEdge(start, end) {
203
+ if (end === END) {
204
+ if (start === START) throw new Error("Cannot have an edge from START to END");
205
+ this.nodes[start].writers.push(new ChannelWrite([{
206
+ channel: END,
207
+ value: PASSTHROUGH
208
+ }], [TAG_HIDDEN]));
209
+ } else {
210
+ this.nodes[end].triggers.push(start);
211
+ this.nodes[end].channels.push(start);
212
+ }
213
+ }
214
+ attachBranch(start, name, branch) {
215
+ if (start === START && !this.nodes[START]) this.nodes[START] = Channel.subscribeTo(START, { tags: [TAG_HIDDEN] });
216
+ this.nodes[start].pipe(branch.run((dests) => {
217
+ const writes = dests.map((dest) => {
218
+ if (_isSend(dest)) return dest;
219
+ return {
220
+ channel: dest === END ? END : `branch:${start}:${name}:${dest}`,
221
+ value: PASSTHROUGH
222
+ };
223
+ });
224
+ return new ChannelWrite(writes, [TAG_HIDDEN]);
225
+ }));
226
+ const ends = branch.ends ? Object.values(branch.ends) : Object.keys(this.nodes);
227
+ for (const end of ends) if (end !== END) {
228
+ const channelName = `branch:${start}:${name}:${end}`;
229
+ this.channels[channelName] = new EphemeralValue();
230
+ this.nodes[end].triggers.push(channelName);
231
+ this.nodes[end].channels.push(channelName);
232
+ }
233
+ }
234
+ /**
235
+ * Returns a drawable representation of the computation graph.
236
+ */
237
+ async getGraphAsync(config) {
238
+ const xray = config?.xray;
239
+ const graph = new Graph();
240
+ const startNodes = { [START]: graph.addNode({ schema: z.any() }, START) };
241
+ const endNodes = {};
242
+ let subgraphs = {};
243
+ if (xray) subgraphs = Object.fromEntries((await gatherIterator(this.getSubgraphsAsync())).filter((x) => isCompiledGraph(x[1])));
244
+ function addEdge(start, end, label, conditional = false) {
245
+ if (end === END && endNodes[END] === void 0) endNodes[END] = graph.addNode({ schema: z.any() }, END);
246
+ if (startNodes[start] === void 0) return;
247
+ if (endNodes[end] === void 0) throw new Error(`End node ${end} not found!`);
248
+ return graph.addEdge(startNodes[start], endNodes[end], label !== end ? label : void 0, conditional);
249
+ }
250
+ for (const [key, nodeSpec] of Object.entries(this.builder.nodes)) {
251
+ const displayKey = _escapeMermaidKeywords(key);
252
+ const node = nodeSpec.runnable;
253
+ const metadata = nodeSpec.metadata ?? {};
254
+ if (this.interruptBefore?.includes(key) && this.interruptAfter?.includes(key)) metadata.__interrupt = "before,after";
255
+ else if (this.interruptBefore?.includes(key)) metadata.__interrupt = "before";
256
+ else if (this.interruptAfter?.includes(key)) metadata.__interrupt = "after";
257
+ if (xray) {
258
+ const newXrayValue = typeof xray === "number" ? xray - 1 : xray;
259
+ const drawableSubgraph = subgraphs[key] !== void 0 ? await subgraphs[key].getGraphAsync({
260
+ ...config,
261
+ xray: newXrayValue
262
+ }) : node.getGraph(config);
263
+ drawableSubgraph.trimFirstNode();
264
+ drawableSubgraph.trimLastNode();
265
+ if (Object.keys(drawableSubgraph.nodes).length > 1) {
266
+ const [e, s] = graph.extend(drawableSubgraph, displayKey);
267
+ if (e === void 0) throw new Error(`Could not extend subgraph "${key}" due to missing entrypoint.`);
268
+ function _isRunnableInterface(thing) {
269
+ return thing ? thing.lc_runnable : false;
270
+ }
271
+ function _nodeDataStr(id, data) {
272
+ if (id !== void 0 && !validate(id)) return id;
273
+ else if (_isRunnableInterface(data)) try {
274
+ let dataStr = data.getName();
275
+ dataStr = dataStr.startsWith("Runnable") ? dataStr.slice(8) : dataStr;
276
+ return dataStr;
277
+ } catch (error) {
278
+ return data.getName();
279
+ }
280
+ else return data.name ?? "UnknownSchema";
281
+ }
282
+ if (s !== void 0) startNodes[displayKey] = {
283
+ name: _nodeDataStr(s.id, s.data),
284
+ ...s
285
+ };
286
+ endNodes[displayKey] = {
287
+ name: _nodeDataStr(e.id, e.data),
288
+ ...e
289
+ };
290
+ } else {
291
+ const newNode = graph.addNode(node, displayKey, metadata);
292
+ startNodes[displayKey] = newNode;
293
+ endNodes[displayKey] = newNode;
294
+ }
295
+ } else {
296
+ const newNode = graph.addNode(node, displayKey, metadata);
297
+ startNodes[displayKey] = newNode;
298
+ endNodes[displayKey] = newNode;
299
+ }
300
+ }
301
+ const sortedEdges = [...this.builder.allEdges].sort(([a], [b]) => {
302
+ if (a < b) return -1;
303
+ else if (b > a) return 1;
304
+ else return 0;
305
+ });
306
+ for (const [start, end] of sortedEdges) addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end));
307
+ for (const [start, branches] of Object.entries(this.builder.branches)) {
308
+ const defaultEnds = {
309
+ ...Object.fromEntries(Object.keys(this.builder.nodes).filter((k) => k !== start).map((k) => [_escapeMermaidKeywords(k), _escapeMermaidKeywords(k)])),
310
+ [END]: END
311
+ };
312
+ for (const branch of Object.values(branches)) {
313
+ let ends;
314
+ if (branch.ends !== void 0) ends = branch.ends;
315
+ else ends = defaultEnds;
316
+ for (const [label, end] of Object.entries(ends)) addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end), label, true);
317
+ }
318
+ }
319
+ for (const [key, node] of Object.entries(this.builder.nodes)) if (node.ends !== void 0) for (const end of node.ends) addEdge(_escapeMermaidKeywords(key), _escapeMermaidKeywords(end), void 0, true);
320
+ return graph;
321
+ }
322
+ /**
323
+ * Returns a drawable representation of the computation graph.
324
+ *
325
+ * @deprecated Use getGraphAsync instead. The async method will be the default in the next minor core release.
326
+ */
327
+ getGraph(config) {
328
+ const xray = config?.xray;
329
+ const graph = new Graph();
330
+ const startNodes = { [START]: graph.addNode({ schema: z.any() }, START) };
331
+ const endNodes = {};
332
+ let subgraphs = {};
333
+ if (xray) subgraphs = Object.fromEntries(gatherIteratorSync(this.getSubgraphs()).filter((x) => isCompiledGraph(x[1])));
334
+ function addEdge(start, end, label, conditional = false) {
335
+ if (end === END && endNodes[END] === void 0) endNodes[END] = graph.addNode({ schema: z.any() }, END);
336
+ return graph.addEdge(startNodes[start], endNodes[end], label !== end ? label : void 0, conditional);
337
+ }
338
+ for (const [key, nodeSpec] of Object.entries(this.builder.nodes)) {
339
+ const displayKey = _escapeMermaidKeywords(key);
340
+ const node = nodeSpec.runnable;
341
+ const metadata = nodeSpec.metadata ?? {};
342
+ if (this.interruptBefore?.includes(key) && this.interruptAfter?.includes(key)) metadata.__interrupt = "before,after";
343
+ else if (this.interruptBefore?.includes(key)) metadata.__interrupt = "before";
344
+ else if (this.interruptAfter?.includes(key)) metadata.__interrupt = "after";
345
+ if (xray) {
346
+ const newXrayValue = typeof xray === "number" ? xray - 1 : xray;
347
+ const drawableSubgraph = subgraphs[key] !== void 0 ? subgraphs[key].getGraph({
348
+ ...config,
349
+ xray: newXrayValue
350
+ }) : node.getGraph(config);
351
+ drawableSubgraph.trimFirstNode();
352
+ drawableSubgraph.trimLastNode();
353
+ if (Object.keys(drawableSubgraph.nodes).length > 1) {
354
+ const [e, s] = graph.extend(drawableSubgraph, displayKey);
355
+ if (e === void 0) throw new Error(`Could not extend subgraph "${key}" due to missing entrypoint.`);
356
+ function _isRunnableInterface(thing) {
357
+ return thing ? thing.lc_runnable : false;
358
+ }
359
+ function _nodeDataStr(id, data) {
360
+ if (id !== void 0 && !validate(id)) return id;
361
+ else if (_isRunnableInterface(data)) try {
362
+ let dataStr = data.getName();
363
+ dataStr = dataStr.startsWith("Runnable") ? dataStr.slice(8) : dataStr;
364
+ return dataStr;
365
+ } catch (error) {
366
+ return data.getName();
367
+ }
368
+ else return data.name ?? "UnknownSchema";
369
+ }
370
+ if (s !== void 0) startNodes[displayKey] = {
371
+ name: _nodeDataStr(s.id, s.data),
372
+ ...s
373
+ };
374
+ endNodes[displayKey] = {
375
+ name: _nodeDataStr(e.id, e.data),
376
+ ...e
377
+ };
378
+ } else {
379
+ const newNode = graph.addNode(node, displayKey, metadata);
380
+ startNodes[displayKey] = newNode;
381
+ endNodes[displayKey] = newNode;
382
+ }
383
+ } else {
384
+ const newNode = graph.addNode(node, displayKey, metadata);
385
+ startNodes[displayKey] = newNode;
386
+ endNodes[displayKey] = newNode;
387
+ }
388
+ }
389
+ const sortedEdges = [...this.builder.allEdges].sort(([a], [b]) => {
390
+ if (a < b) return -1;
391
+ else if (b > a) return 1;
392
+ else return 0;
393
+ });
394
+ for (const [start, end] of sortedEdges) addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end));
395
+ for (const [start, branches] of Object.entries(this.builder.branches)) {
396
+ const defaultEnds = {
397
+ ...Object.fromEntries(Object.keys(this.builder.nodes).filter((k) => k !== start).map((k) => [_escapeMermaidKeywords(k), _escapeMermaidKeywords(k)])),
398
+ [END]: END
399
+ };
400
+ for (const branch of Object.values(branches)) {
401
+ let ends;
402
+ if (branch.ends !== void 0) ends = branch.ends;
403
+ else ends = defaultEnds;
404
+ for (const [label, end] of Object.entries(ends)) addEdge(_escapeMermaidKeywords(start), _escapeMermaidKeywords(end), label, true);
405
+ }
406
+ }
407
+ return graph;
408
+ }
409
+ };
708
410
  function isCompiledGraph(x) {
709
- return (
710
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
711
- typeof x.attachNode === "function" &&
712
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
713
- typeof x.attachEdge === "function");
411
+ return typeof x.attachNode === "function" && typeof x.attachEdge === "function";
714
412
  }
715
413
  function _escapeMermaidKeywords(key) {
716
- if (key === "subgraph") {
717
- return `"${key}"`;
718
- }
719
- return key;
414
+ if (key === "subgraph") return `"${key}"`;
415
+ return key;
720
416
  }
417
+
418
+ //#endregion
419
+ export { Branch, CompiledGraph, Graph$1 as Graph };
721
420
  //# sourceMappingURL=graph.js.map