@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,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";
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