@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,492 +1,342 @@
1
- import { isAIMessage, isBaseMessage, isToolMessage, SystemMessage, } from "@langchain/core/messages";
2
- import { Runnable, RunnableLambda, RunnableSequence, RunnableBinding, } from "@langchain/core/runnables";
3
- import { StateGraph, } from "../graph/index.js";
4
- import { ToolNode } from "./tool_node.js";
5
1
  import { Annotation } from "../graph/annotation.js";
2
+ import { END, START, Send } from "../constants.js";
3
+ import { StateGraph } from "../graph/state.js";
6
4
  import { messagesStateReducer } from "../graph/message.js";
7
- import { END, Send, START } from "../constants.js";
5
+ import "../graph/index.js";
6
+ import { ToolNode } from "./tool_node.js";
8
7
  import { withAgentName } from "./agentName.js";
8
+ import { Runnable, RunnableBinding, RunnableLambda, RunnableSequence } from "@langchain/core/runnables";
9
+ import { SystemMessage, isAIMessage, isBaseMessage, isToolMessage } from "@langchain/core/messages";
10
+
11
+ //#region src/prebuilt/react_agent_executor.ts
9
12
  function _convertMessageModifierToPrompt(messageModifier) {
10
- // Handle string or SystemMessage
11
- if (typeof messageModifier === "string" ||
12
- (isBaseMessage(messageModifier) && messageModifier._getType() === "system")) {
13
- return messageModifier;
14
- }
15
- // Handle callable function
16
- if (typeof messageModifier === "function") {
17
- return async (state) => messageModifier(state.messages);
18
- }
19
- // Handle Runnable
20
- if (Runnable.isRunnable(messageModifier)) {
21
- return RunnableLambda.from((state) => state.messages).pipe(messageModifier);
22
- }
23
- throw new Error(`Unexpected type for messageModifier: ${typeof messageModifier}`);
13
+ if (typeof messageModifier === "string" || isBaseMessage(messageModifier) && messageModifier._getType() === "system") return messageModifier;
14
+ if (typeof messageModifier === "function") return async (state) => messageModifier(state.messages);
15
+ if (Runnable.isRunnable(messageModifier)) return RunnableLambda.from((state) => state.messages).pipe(messageModifier);
16
+ throw new Error(`Unexpected type for messageModifier: ${typeof messageModifier}`);
24
17
  }
25
18
  const PROMPT_RUNNABLE_NAME = "prompt";
26
19
  function _getPromptRunnable(prompt) {
27
- let promptRunnable;
28
- if (prompt == null) {
29
- promptRunnable = RunnableLambda.from((state) => state.messages).withConfig({ runName: PROMPT_RUNNABLE_NAME });
30
- }
31
- else if (typeof prompt === "string") {
32
- const systemMessage = new SystemMessage(prompt);
33
- promptRunnable = RunnableLambda.from((state) => {
34
- return [systemMessage, ...(state.messages ?? [])];
35
- }).withConfig({ runName: PROMPT_RUNNABLE_NAME });
36
- }
37
- else if (isBaseMessage(prompt) && prompt._getType() === "system") {
38
- promptRunnable = RunnableLambda.from((state) => [prompt, ...state.messages]).withConfig({ runName: PROMPT_RUNNABLE_NAME });
39
- }
40
- else if (typeof prompt === "function") {
41
- promptRunnable = RunnableLambda.from(prompt).withConfig({
42
- runName: PROMPT_RUNNABLE_NAME,
43
- });
44
- }
45
- else if (Runnable.isRunnable(prompt)) {
46
- promptRunnable = prompt;
47
- }
48
- else {
49
- throw new Error(`Got unexpected type for 'prompt': ${typeof prompt}`);
50
- }
51
- return promptRunnable;
20
+ let promptRunnable;
21
+ if (prompt == null) promptRunnable = RunnableLambda.from((state) => state.messages).withConfig({ runName: PROMPT_RUNNABLE_NAME });
22
+ else if (typeof prompt === "string") {
23
+ const systemMessage = new SystemMessage(prompt);
24
+ promptRunnable = RunnableLambda.from((state) => {
25
+ return [systemMessage, ...state.messages ?? []];
26
+ }).withConfig({ runName: PROMPT_RUNNABLE_NAME });
27
+ } else if (isBaseMessage(prompt) && prompt._getType() === "system") promptRunnable = RunnableLambda.from((state) => [prompt, ...state.messages]).withConfig({ runName: PROMPT_RUNNABLE_NAME });
28
+ else if (typeof prompt === "function") promptRunnable = RunnableLambda.from(prompt).withConfig({ runName: PROMPT_RUNNABLE_NAME });
29
+ else if (Runnable.isRunnable(prompt)) promptRunnable = prompt;
30
+ else throw new Error(`Got unexpected type for 'prompt': ${typeof prompt}`);
31
+ return promptRunnable;
52
32
  }
53
33
  function isClientTool(tool) {
54
- return Runnable.isRunnable(tool);
34
+ return Runnable.isRunnable(tool);
55
35
  }
56
36
  function _getPrompt(prompt, stateModifier, messageModifier) {
57
- // Check if multiple modifiers exist
58
- const definedCount = [prompt, stateModifier, messageModifier].filter((x) => x != null).length;
59
- if (definedCount > 1) {
60
- throw new Error("Expected only one of prompt, stateModifier, or messageModifier, got multiple values");
61
- }
62
- let finalPrompt = prompt;
63
- if (stateModifier != null) {
64
- finalPrompt = stateModifier;
65
- }
66
- else if (messageModifier != null) {
67
- finalPrompt = _convertMessageModifierToPrompt(messageModifier);
68
- }
69
- return _getPromptRunnable(finalPrompt);
37
+ const definedCount = [
38
+ prompt,
39
+ stateModifier,
40
+ messageModifier
41
+ ].filter((x) => x != null).length;
42
+ if (definedCount > 1) throw new Error("Expected only one of prompt, stateModifier, or messageModifier, got multiple values");
43
+ let finalPrompt = prompt;
44
+ if (stateModifier != null) finalPrompt = stateModifier;
45
+ else if (messageModifier != null) finalPrompt = _convertMessageModifierToPrompt(messageModifier);
46
+ return _getPromptRunnable(finalPrompt);
70
47
  }
71
48
  function _isBaseChatModel(model) {
72
- return ("invoke" in model &&
73
- typeof model.invoke === "function" &&
74
- "_modelType" in model);
49
+ return "invoke" in model && typeof model.invoke === "function" && "_modelType" in model;
75
50
  }
76
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
77
51
  function _isConfigurableModel(model) {
78
- return ("_queuedMethodOperations" in model &&
79
- "_model" in model &&
80
- typeof model._model === "function");
52
+ return "_queuedMethodOperations" in model && "_model" in model && typeof model._model === "function";
81
53
  }
82
54
  function _isChatModelWithBindTools(llm) {
83
- if (!_isBaseChatModel(llm))
84
- return false;
85
- return "bindTools" in llm && typeof llm.bindTools === "function";
55
+ if (!_isBaseChatModel(llm)) return false;
56
+ return "bindTools" in llm && typeof llm.bindTools === "function";
86
57
  }
87
- export async function _shouldBindTools(llm, tools) {
88
- // If model is a RunnableSequence, find a RunnableBinding or BaseChatModel in its steps
89
- let model = llm;
90
- if (RunnableSequence.isRunnableSequence(model)) {
91
- model =
92
- model.steps.find((step) => RunnableBinding.isRunnableBinding(step) ||
93
- _isBaseChatModel(step) ||
94
- _isConfigurableModel(step)) || model;
95
- }
96
- if (_isConfigurableModel(model)) {
97
- model = await model._model();
98
- }
99
- // If not a RunnableBinding, we should bind tools
100
- if (!RunnableBinding.isRunnableBinding(model)) {
101
- return true;
102
- }
103
- let boundTools = (() => {
104
- // check if model.kwargs contain the tools key
105
- if (model.kwargs != null &&
106
- typeof model.kwargs === "object" &&
107
- "tools" in model.kwargs &&
108
- Array.isArray(model.kwargs.tools)) {
109
- return (model.kwargs.tools ?? null);
110
- }
111
- // Some models can bind the tools via `withConfig()` instead of `bind()`
112
- if (model.config != null &&
113
- typeof model.config === "object" &&
114
- "tools" in model.config &&
115
- Array.isArray(model.config.tools)) {
116
- return (model.config.tools ?? null);
117
- }
118
- return null;
119
- })();
120
- // google-style
121
- if (boundTools != null &&
122
- boundTools.length === 1 &&
123
- "functionDeclarations" in boundTools[0]) {
124
- boundTools = boundTools[0].functionDeclarations;
125
- }
126
- // If no tools in kwargs, we should bind tools
127
- if (boundTools == null)
128
- return true;
129
- // Check if tools count matches
130
- if (tools.length !== boundTools.length) {
131
- throw new Error("Number of tools in the model.bindTools() and tools passed to createReactAgent must match");
132
- }
133
- const toolNames = new Set(tools.flatMap((tool) => (isClientTool(tool) ? tool.name : [])));
134
- const boundToolNames = new Set();
135
- for (const boundTool of boundTools) {
136
- let boundToolName;
137
- // OpenAI-style tool
138
- if ("type" in boundTool && boundTool.type === "function") {
139
- boundToolName = boundTool.function.name;
140
- }
141
- // Anthropic or Google-style tool
142
- else if ("name" in boundTool) {
143
- boundToolName = boundTool.name;
144
- }
145
- // Bedrock-style tool
146
- else if ("toolSpec" in boundTool && "name" in boundTool.toolSpec) {
147
- boundToolName = boundTool.toolSpec.name;
148
- }
149
- // unknown tool type so we'll ignore it
150
- else {
151
- continue;
152
- }
153
- if (boundToolName) {
154
- boundToolNames.add(boundToolName);
155
- }
156
- }
157
- const missingTools = [...toolNames].filter((x) => !boundToolNames.has(x));
158
- if (missingTools.length > 0) {
159
- throw new Error(`Missing tools '${missingTools}' in the model.bindTools().` +
160
- `Tools in the model.bindTools() must match the tools passed to createReactAgent.`);
161
- }
162
- return false;
58
+ async function _shouldBindTools(llm, tools) {
59
+ let model = llm;
60
+ if (RunnableSequence.isRunnableSequence(model)) model = model.steps.find((step) => RunnableBinding.isRunnableBinding(step) || _isBaseChatModel(step) || _isConfigurableModel(step)) || model;
61
+ if (_isConfigurableModel(model)) model = await model._model();
62
+ if (!RunnableBinding.isRunnableBinding(model)) return true;
63
+ let boundTools = (() => {
64
+ if (model.kwargs != null && typeof model.kwargs === "object" && "tools" in model.kwargs && Array.isArray(model.kwargs.tools)) return model.kwargs.tools ?? null;
65
+ if (model.config != null && typeof model.config === "object" && "tools" in model.config && Array.isArray(model.config.tools)) return model.config.tools ?? null;
66
+ return null;
67
+ })();
68
+ if (boundTools != null && boundTools.length === 1 && "functionDeclarations" in boundTools[0]) boundTools = boundTools[0].functionDeclarations;
69
+ if (boundTools == null) return true;
70
+ if (tools.length !== boundTools.length) throw new Error("Number of tools in the model.bindTools() and tools passed to createReactAgent must match");
71
+ const toolNames = new Set(tools.flatMap((tool) => isClientTool(tool) ? tool.name : []));
72
+ const boundToolNames = /* @__PURE__ */ new Set();
73
+ for (const boundTool of boundTools) {
74
+ let boundToolName;
75
+ if ("type" in boundTool && boundTool.type === "function") boundToolName = boundTool.function.name;
76
+ else if ("name" in boundTool) boundToolName = boundTool.name;
77
+ else if ("toolSpec" in boundTool && "name" in boundTool.toolSpec) boundToolName = boundTool.toolSpec.name;
78
+ else continue;
79
+ if (boundToolName) boundToolNames.add(boundToolName);
80
+ }
81
+ const missingTools = [...toolNames].filter((x) => !boundToolNames.has(x));
82
+ if (missingTools.length > 0) throw new Error(`Missing tools '${missingTools}' in the model.bindTools().Tools in the model.bindTools() must match the tools passed to createReactAgent.`);
83
+ return false;
163
84
  }
164
85
  const _simpleBindTools = (llm, toolClasses) => {
165
- if (_isChatModelWithBindTools(llm)) {
166
- return llm.bindTools(toolClasses);
167
- }
168
- if (RunnableBinding.isRunnableBinding(llm) &&
169
- _isChatModelWithBindTools(llm.bound)) {
170
- const newBound = llm.bound.bindTools(toolClasses);
171
- if (RunnableBinding.isRunnableBinding(newBound)) {
172
- return new RunnableBinding({
173
- bound: newBound.bound,
174
- config: { ...llm.config, ...newBound.config },
175
- kwargs: { ...llm.kwargs, ...newBound.kwargs },
176
- configFactories: newBound.configFactories ?? llm.configFactories,
177
- });
178
- }
179
- return new RunnableBinding({
180
- bound: newBound,
181
- config: llm.config,
182
- kwargs: llm.kwargs,
183
- configFactories: llm.configFactories,
184
- });
185
- }
186
- return null;
86
+ if (_isChatModelWithBindTools(llm)) return llm.bindTools(toolClasses);
87
+ if (RunnableBinding.isRunnableBinding(llm) && _isChatModelWithBindTools(llm.bound)) {
88
+ const newBound = llm.bound.bindTools(toolClasses);
89
+ if (RunnableBinding.isRunnableBinding(newBound)) return new RunnableBinding({
90
+ bound: newBound.bound,
91
+ config: {
92
+ ...llm.config,
93
+ ...newBound.config
94
+ },
95
+ kwargs: {
96
+ ...llm.kwargs,
97
+ ...newBound.kwargs
98
+ },
99
+ configFactories: newBound.configFactories ?? llm.configFactories
100
+ });
101
+ return new RunnableBinding({
102
+ bound: newBound,
103
+ config: llm.config,
104
+ kwargs: llm.kwargs,
105
+ configFactories: llm.configFactories
106
+ });
107
+ }
108
+ return null;
187
109
  };
188
- export async function _bindTools(llm, toolClasses) {
189
- const model = _simpleBindTools(llm, toolClasses);
190
- if (model)
191
- return model;
192
- if (_isConfigurableModel(llm)) {
193
- const model = _simpleBindTools(await llm._model(), toolClasses);
194
- if (model)
195
- return model;
196
- }
197
- if (RunnableSequence.isRunnableSequence(llm)) {
198
- const modelStep = llm.steps.findIndex((step) => RunnableBinding.isRunnableBinding(step) ||
199
- _isBaseChatModel(step) ||
200
- _isConfigurableModel(step));
201
- if (modelStep >= 0) {
202
- const model = _simpleBindTools(llm.steps[modelStep], toolClasses);
203
- if (model) {
204
- const nextSteps = llm.steps.slice();
205
- nextSteps.splice(modelStep, 1, model);
206
- return RunnableSequence.from(nextSteps);
207
- }
208
- }
209
- }
210
- throw new Error(`llm ${llm} must define bindTools method.`);
110
+ async function _bindTools(llm, toolClasses) {
111
+ const model = _simpleBindTools(llm, toolClasses);
112
+ if (model) return model;
113
+ if (_isConfigurableModel(llm)) {
114
+ const model$1 = _simpleBindTools(await llm._model(), toolClasses);
115
+ if (model$1) return model$1;
116
+ }
117
+ if (RunnableSequence.isRunnableSequence(llm)) {
118
+ const modelStep = llm.steps.findIndex((step) => RunnableBinding.isRunnableBinding(step) || _isBaseChatModel(step) || _isConfigurableModel(step));
119
+ if (modelStep >= 0) {
120
+ const model$1 = _simpleBindTools(llm.steps[modelStep], toolClasses);
121
+ if (model$1) {
122
+ const nextSteps = llm.steps.slice();
123
+ nextSteps.splice(modelStep, 1, model$1);
124
+ return RunnableSequence.from(nextSteps);
125
+ }
126
+ }
127
+ }
128
+ throw new Error(`llm ${llm} must define bindTools method.`);
211
129
  }
212
- export async function _getModel(llm) {
213
- // If model is a RunnableSequence, find a RunnableBinding or BaseChatModel in its steps
214
- let model = llm;
215
- if (RunnableSequence.isRunnableSequence(model)) {
216
- model =
217
- model.steps.find((step) => RunnableBinding.isRunnableBinding(step) ||
218
- _isBaseChatModel(step) ||
219
- _isConfigurableModel(step)) || model;
220
- }
221
- if (_isConfigurableModel(model)) {
222
- model = await model._model();
223
- }
224
- // Get the underlying model from a RunnableBinding
225
- if (RunnableBinding.isRunnableBinding(model)) {
226
- model = model.bound;
227
- }
228
- if (!_isBaseChatModel(model)) {
229
- throw new Error(`Expected \`llm\` to be a ChatModel or RunnableBinding (e.g. llm.bind_tools(...)) with invoke() and generate() methods, got ${model.constructor.name}`);
230
- }
231
- return model;
130
+ async function _getModel(llm) {
131
+ let model = llm;
132
+ if (RunnableSequence.isRunnableSequence(model)) model = model.steps.find((step) => RunnableBinding.isRunnableBinding(step) || _isBaseChatModel(step) || _isConfigurableModel(step)) || model;
133
+ if (_isConfigurableModel(model)) model = await model._model();
134
+ if (RunnableBinding.isRunnableBinding(model)) model = model.bound;
135
+ if (!_isBaseChatModel(model)) throw new Error(`Expected \`llm\` to be a ChatModel or RunnableBinding (e.g. llm.bind_tools(...)) with invoke() and generate() methods, got ${model.constructor.name}`);
136
+ return model;
232
137
  }
233
- export const createReactAgentAnnotation = () => Annotation.Root({
234
- messages: Annotation({
235
- reducer: messagesStateReducer,
236
- default: () => [],
237
- }),
238
- structuredResponse: (Annotation),
239
- });
240
- const PreHookAnnotation = Annotation.Root({
241
- llmInputMessages: Annotation({
242
- reducer: (_, update) => messagesStateReducer([], update),
243
- default: () => [],
244
- }),
138
+ const createReactAgentAnnotation = () => Annotation.Root({
139
+ messages: Annotation({
140
+ reducer: messagesStateReducer,
141
+ default: () => []
142
+ }),
143
+ structuredResponse: Annotation
245
144
  });
145
+ const PreHookAnnotation = Annotation.Root({ llmInputMessages: Annotation({
146
+ reducer: (_, update) => messagesStateReducer([], update),
147
+ default: () => []
148
+ }) });
246
149
  /**
247
- * Creates a StateGraph agent that relies on a chat model utilizing tool calling.
248
- *
249
- * @example
250
- * ```ts
251
- * import { ChatOpenAI } from "@langchain/openai";
252
- * import { tool } from "@langchain/core/tools";
253
- * import { z } from "zod";
254
- * import { createReactAgent } from "@langchain/langgraph/prebuilt";
255
- *
256
- * const model = new ChatOpenAI({
257
- * model: "gpt-4o",
258
- * });
259
- *
260
- * const getWeather = tool((input) => {
261
- * if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
262
- * return "It's 60 degrees and foggy.";
263
- * } else {
264
- * return "It's 90 degrees and sunny.";
265
- * }
266
- * }, {
267
- * name: "get_weather",
268
- * description: "Call to get the current weather.",
269
- * schema: z.object({
270
- * location: z.string().describe("Location to get the weather for."),
271
- * })
272
- * })
273
- *
274
- * const agent = createReactAgent({ llm: model, tools: [getWeather] });
275
- *
276
- * const inputs = {
277
- * messages: [{ role: "user", content: "what is the weather in SF?" }],
278
- * };
279
- *
280
- * const stream = await agent.stream(inputs, { streamMode: "values" });
281
- *
282
- * for await (const { messages } of stream) {
283
- * console.log(messages);
284
- * }
285
- * // Returns the messages in the state at each step of execution
286
- * ```
287
- */
288
- export function createReactAgent(params) {
289
- const { llm, tools, messageModifier, stateModifier, prompt, stateSchema, contextSchema, checkpointSaver, checkpointer, interruptBefore, interruptAfter, store, responseFormat, preModelHook, postModelHook, name, description, version = "v1", includeAgentName, } = params;
290
- let toolClasses;
291
- let toolNode;
292
- if (!Array.isArray(tools)) {
293
- toolClasses = tools.tools;
294
- toolNode = tools;
295
- }
296
- else {
297
- toolClasses = tools;
298
- toolNode = new ToolNode(toolClasses.filter(isClientTool));
299
- }
300
- let cachedStaticModel = null;
301
- const _getStaticModel = async (llm) => {
302
- if (cachedStaticModel)
303
- return cachedStaticModel;
304
- let modelWithTools;
305
- if (await _shouldBindTools(llm, toolClasses)) {
306
- modelWithTools = await _bindTools(llm, toolClasses);
307
- }
308
- else {
309
- modelWithTools = llm;
310
- }
311
- const promptRunnable = _getPrompt(prompt, stateModifier, messageModifier);
312
- const modelRunnable = includeAgentName === "inline"
313
- ? withAgentName(modelWithTools, includeAgentName)
314
- : modelWithTools;
315
- cachedStaticModel = promptRunnable.pipe(modelRunnable);
316
- return cachedStaticModel;
317
- };
318
- const _getDynamicModel = async (llm, state, config) => {
319
- const model = await llm(state, config);
320
- return _getPrompt(prompt, stateModifier, messageModifier).pipe(includeAgentName === "inline"
321
- ? withAgentName(model, includeAgentName)
322
- : model);
323
- };
324
- // If any of the tools are configured to return_directly after running,
325
- // our graph needs to check if these were called
326
- const shouldReturnDirect = new Set(toolClasses
327
- .filter(isClientTool)
328
- .filter((tool) => "returnDirect" in tool && tool.returnDirect)
329
- .map((tool) => tool.name));
330
- function getModelInputState(state) {
331
- const { messages, llmInputMessages, ...rest } = state;
332
- if (llmInputMessages != null && llmInputMessages.length > 0) {
333
- return { messages: llmInputMessages, ...rest };
334
- }
335
- return { messages, ...rest };
336
- }
337
- const generateStructuredResponse = async (state, config) => {
338
- if (responseFormat == null) {
339
- throw new Error("Attempted to generate structured output with no passed response schema. Please contact us for help.");
340
- }
341
- const messages = [...state.messages];
342
- let modelWithStructuredOutput;
343
- const model = typeof llm === "function"
344
- ? await llm(state, config)
345
- : await _getModel(llm);
346
- if (!_isBaseChatModel(model)) {
347
- throw new Error(`Expected \`llm\` to be a ChatModel with .withStructuredOutput() method, got ${model.constructor.name}`);
348
- }
349
- if (typeof responseFormat === "object" && "schema" in responseFormat) {
350
- const { prompt, schema, ...options } = responseFormat;
351
- modelWithStructuredOutput = model.withStructuredOutput(schema, options);
352
- if (prompt != null) {
353
- messages.unshift(new SystemMessage({ content: prompt }));
354
- }
355
- }
356
- else {
357
- modelWithStructuredOutput = model.withStructuredOutput(responseFormat);
358
- }
359
- const response = await modelWithStructuredOutput.invoke(messages, config);
360
- return { structuredResponse: response };
361
- };
362
- const callModel = async (state, config) => {
363
- // NOTE: we're dynamically creating the model runnable here
364
- // to ensure that we can validate ConfigurableModel properly
365
- const modelRunnable = typeof llm === "function"
366
- ? await _getDynamicModel(llm, state, config)
367
- : await _getStaticModel(llm);
368
- // TODO: Auto-promote streaming.
369
- const response = (await modelRunnable.invoke(getModelInputState(state), config));
370
- // add agent name to the AIMessage
371
- // TODO: figure out if we can avoid mutating the message directly
372
- response.name = name;
373
- response.lc_kwargs.name = name;
374
- return { messages: [response] };
375
- };
376
- const schema = stateSchema ?? createReactAgentAnnotation();
377
- const workflow = new StateGraph(schema, contextSchema).addNode("tools", toolNode);
378
- if (!("messages" in workflow._schemaDefinition)) {
379
- throw new Error("Missing required `messages` key in state schema.");
380
- }
381
- const allNodeWorkflows = workflow;
382
- const conditionalMap = (map) => {
383
- return Object.fromEntries(Object.entries(map).filter(([_, v]) => v != null));
384
- };
385
- let entrypoint = "agent";
386
- let inputSchema;
387
- if (preModelHook != null) {
388
- allNodeWorkflows
389
- .addNode("pre_model_hook", preModelHook)
390
- .addEdge("pre_model_hook", "agent");
391
- entrypoint = "pre_model_hook";
392
- inputSchema = Annotation.Root({
393
- ...workflow._schemaDefinition,
394
- ...PreHookAnnotation.spec,
395
- });
396
- }
397
- else {
398
- entrypoint = "agent";
399
- }
400
- allNodeWorkflows
401
- .addNode("agent", callModel, { input: inputSchema })
402
- .addEdge(START, entrypoint);
403
- if (postModelHook != null) {
404
- allNodeWorkflows
405
- .addNode("post_model_hook", postModelHook)
406
- .addEdge("agent", "post_model_hook")
407
- .addConditionalEdges("post_model_hook", (state) => {
408
- const { messages } = state;
409
- const toolMessageIds = new Set(messages.filter(isToolMessage).map((msg) => msg.tool_call_id));
410
- let lastAiMessage;
411
- for (let i = messages.length - 1; i >= 0; i -= 1) {
412
- const message = messages[i];
413
- if (isAIMessage(message)) {
414
- lastAiMessage = message;
415
- break;
416
- }
417
- }
418
- const pendingToolCalls = lastAiMessage?.tool_calls?.filter((i) => i.id == null || !toolMessageIds.has(i.id)) ?? [];
419
- const lastMessage = messages.at(-1);
420
- if (pendingToolCalls.length > 0) {
421
- if (version === "v2") {
422
- return pendingToolCalls.map((toolCall) => new Send("tools", { ...state, lg_tool_call: toolCall }));
423
- }
424
- return "tools";
425
- }
426
- if (lastMessage && isToolMessage(lastMessage))
427
- return entrypoint;
428
- if (responseFormat != null)
429
- return "generate_structured_response";
430
- return END;
431
- }, conditionalMap({
432
- tools: "tools",
433
- [entrypoint]: entrypoint,
434
- generate_structured_response: responseFormat != null ? "generate_structured_response" : null,
435
- [END]: responseFormat != null ? null : END,
436
- }));
437
- }
438
- if (responseFormat !== undefined) {
439
- workflow
440
- .addNode("generate_structured_response", generateStructuredResponse)
441
- .addEdge("generate_structured_response", END);
442
- }
443
- if (postModelHook == null) {
444
- allNodeWorkflows.addConditionalEdges("agent", (state) => {
445
- const { messages } = state;
446
- const lastMessage = messages[messages.length - 1];
447
- // if there's no function call, we finish
448
- if (!isAIMessage(lastMessage) || !lastMessage.tool_calls?.length) {
449
- if (responseFormat != null)
450
- return "generate_structured_response";
451
- return END;
452
- }
453
- // there are function calls, we continue
454
- if (version === "v2") {
455
- return lastMessage.tool_calls.map((toolCall) => new Send("tools", { ...state, lg_tool_call: toolCall }));
456
- }
457
- return "tools";
458
- }, conditionalMap({
459
- tools: "tools",
460
- generate_structured_response: responseFormat != null ? "generate_structured_response" : null,
461
- [END]: responseFormat != null ? null : END,
462
- }));
463
- }
464
- if (shouldReturnDirect.size > 0) {
465
- allNodeWorkflows.addConditionalEdges("tools", (state) => {
466
- // Check the last consecutive tool calls
467
- for (let i = state.messages.length - 1; i >= 0; i -= 1) {
468
- const message = state.messages[i];
469
- if (!isToolMessage(message))
470
- break;
471
- // Check if this tool is configured to return directly
472
- if (message.name !== undefined &&
473
- shouldReturnDirect.has(message.name)) {
474
- return END;
475
- }
476
- }
477
- return entrypoint;
478
- }, conditionalMap({ [entrypoint]: entrypoint, [END]: END }));
479
- }
480
- else {
481
- allNodeWorkflows.addEdge("tools", entrypoint);
482
- }
483
- return allNodeWorkflows.compile({
484
- checkpointer: checkpointer ?? checkpointSaver,
485
- interruptBefore,
486
- interruptAfter,
487
- store,
488
- name,
489
- description,
490
- });
150
+ * @deprecated `createReactAgent` has been moved to {@link https://www.npmjs.com/package/langchain langchain} package.
151
+ * Update your import to `import { createAgent } from "langchain";`
152
+ *
153
+ * Creates a StateGraph agent that relies on a chat model utilizing tool calling.
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * import { ChatOpenAI } from "@langchain/openai";
158
+ * import { tool } from "@langchain/core/tools";
159
+ * import { z } from "zod";
160
+ * import { createReactAgent } from "@langchain/langgraph/prebuilt";
161
+ *
162
+ * const model = new ChatOpenAI({
163
+ * model: "gpt-4o",
164
+ * });
165
+ *
166
+ * const getWeather = tool((input) => {
167
+ * if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
168
+ * return "It's 60 degrees and foggy.";
169
+ * } else {
170
+ * return "It's 90 degrees and sunny.";
171
+ * }
172
+ * }, {
173
+ * name: "get_weather",
174
+ * description: "Call to get the current weather.",
175
+ * schema: z.object({
176
+ * location: z.string().describe("Location to get the weather for."),
177
+ * })
178
+ * })
179
+ *
180
+ * const agent = createReactAgent({ llm: model, tools: [getWeather] });
181
+ *
182
+ * const inputs = {
183
+ * messages: [{ role: "user", content: "what is the weather in SF?" }],
184
+ * };
185
+ *
186
+ * const stream = await agent.stream(inputs, { streamMode: "values" });
187
+ *
188
+ * for await (const { messages } of stream) {
189
+ * console.log(messages);
190
+ * }
191
+ * // Returns the messages in the state at each step of execution
192
+ * ```
193
+ */
194
+ function createReactAgent(params) {
195
+ const { llm, tools, messageModifier, stateModifier, prompt, stateSchema, contextSchema, checkpointSaver, checkpointer, interruptBefore, interruptAfter, store, responseFormat, preModelHook, postModelHook, name, description, version = "v1", includeAgentName } = params;
196
+ let toolClasses;
197
+ let toolNode;
198
+ if (!Array.isArray(tools)) {
199
+ toolClasses = tools.tools;
200
+ toolNode = tools;
201
+ } else {
202
+ toolClasses = tools;
203
+ toolNode = new ToolNode(toolClasses.filter(isClientTool));
204
+ }
205
+ let cachedStaticModel = null;
206
+ const _getStaticModel = async (llm$1) => {
207
+ if (cachedStaticModel) return cachedStaticModel;
208
+ let modelWithTools;
209
+ if (await _shouldBindTools(llm$1, toolClasses)) modelWithTools = await _bindTools(llm$1, toolClasses);
210
+ else modelWithTools = llm$1;
211
+ const promptRunnable = _getPrompt(prompt, stateModifier, messageModifier);
212
+ const modelRunnable = includeAgentName === "inline" ? withAgentName(modelWithTools, includeAgentName) : modelWithTools;
213
+ cachedStaticModel = promptRunnable.pipe(modelRunnable);
214
+ return cachedStaticModel;
215
+ };
216
+ const _getDynamicModel = async (llm$1, state, config) => {
217
+ const model = await llm$1(state, config);
218
+ return _getPrompt(prompt, stateModifier, messageModifier).pipe(includeAgentName === "inline" ? withAgentName(model, includeAgentName) : model);
219
+ };
220
+ const shouldReturnDirect = new Set(toolClasses.filter(isClientTool).filter((tool) => "returnDirect" in tool && tool.returnDirect).map((tool) => tool.name));
221
+ function getModelInputState(state) {
222
+ const { messages, llmInputMessages,...rest } = state;
223
+ if (llmInputMessages != null && llmInputMessages.length > 0) return {
224
+ messages: llmInputMessages,
225
+ ...rest
226
+ };
227
+ return {
228
+ messages,
229
+ ...rest
230
+ };
231
+ }
232
+ const generateStructuredResponse = async (state, config) => {
233
+ if (responseFormat == null) throw new Error("Attempted to generate structured output with no passed response schema. Please contact us for help.");
234
+ const messages = [...state.messages];
235
+ let modelWithStructuredOutput;
236
+ const model = typeof llm === "function" ? await llm(state, config) : await _getModel(llm);
237
+ if (!_isBaseChatModel(model)) throw new Error(`Expected \`llm\` to be a ChatModel with .withStructuredOutput() method, got ${model.constructor.name}`);
238
+ if (typeof responseFormat === "object" && "schema" in responseFormat) {
239
+ const { prompt: prompt$1, schema: schema$1,...options } = responseFormat;
240
+ modelWithStructuredOutput = model.withStructuredOutput(schema$1, options);
241
+ if (prompt$1 != null) messages.unshift(new SystemMessage({ content: prompt$1 }));
242
+ } else modelWithStructuredOutput = model.withStructuredOutput(responseFormat);
243
+ const response = await modelWithStructuredOutput.invoke(messages, config);
244
+ return { structuredResponse: response };
245
+ };
246
+ const callModel = async (state, config) => {
247
+ const modelRunnable = typeof llm === "function" ? await _getDynamicModel(llm, state, config) : await _getStaticModel(llm);
248
+ const response = await modelRunnable.invoke(getModelInputState(state), config);
249
+ response.name = name;
250
+ response.lc_kwargs.name = name;
251
+ return { messages: [response] };
252
+ };
253
+ const schema = stateSchema ?? createReactAgentAnnotation();
254
+ const workflow = new StateGraph(schema, contextSchema).addNode("tools", toolNode);
255
+ if (!("messages" in workflow._schemaDefinition)) throw new Error("Missing required `messages` key in state schema.");
256
+ const allNodeWorkflows = workflow;
257
+ const conditionalMap = (map) => {
258
+ return Object.fromEntries(Object.entries(map).filter(([_, v]) => v != null));
259
+ };
260
+ let entrypoint = "agent";
261
+ let inputSchema;
262
+ if (preModelHook != null) {
263
+ allNodeWorkflows.addNode("pre_model_hook", preModelHook).addEdge("pre_model_hook", "agent");
264
+ entrypoint = "pre_model_hook";
265
+ inputSchema = Annotation.Root({
266
+ ...workflow._schemaDefinition,
267
+ ...PreHookAnnotation.spec
268
+ });
269
+ } else entrypoint = "agent";
270
+ allNodeWorkflows.addNode("agent", callModel, { input: inputSchema }).addEdge(START, entrypoint);
271
+ if (postModelHook != null) allNodeWorkflows.addNode("post_model_hook", postModelHook).addEdge("agent", "post_model_hook").addConditionalEdges("post_model_hook", (state) => {
272
+ const { messages } = state;
273
+ const toolMessageIds = new Set(messages.filter(isToolMessage).map((msg) => msg.tool_call_id));
274
+ let lastAiMessage;
275
+ for (let i = messages.length - 1; i >= 0; i -= 1) {
276
+ const message = messages[i];
277
+ if (isAIMessage(message)) {
278
+ lastAiMessage = message;
279
+ break;
280
+ }
281
+ }
282
+ const pendingToolCalls = lastAiMessage?.tool_calls?.filter((i) => i.id == null || !toolMessageIds.has(i.id)) ?? [];
283
+ const lastMessage = messages.at(-1);
284
+ if (pendingToolCalls.length > 0) {
285
+ if (version === "v2") return pendingToolCalls.map((toolCall) => new Send("tools", {
286
+ ...state,
287
+ lg_tool_call: toolCall
288
+ }));
289
+ return "tools";
290
+ }
291
+ if (lastMessage && isToolMessage(lastMessage)) return entrypoint;
292
+ if (responseFormat != null) return "generate_structured_response";
293
+ return END;
294
+ }, conditionalMap({
295
+ tools: "tools",
296
+ [entrypoint]: entrypoint,
297
+ generate_structured_response: responseFormat != null ? "generate_structured_response" : null,
298
+ [END]: responseFormat != null ? null : END
299
+ }));
300
+ if (responseFormat !== void 0) workflow.addNode("generate_structured_response", generateStructuredResponse).addEdge("generate_structured_response", END);
301
+ if (postModelHook == null) allNodeWorkflows.addConditionalEdges("agent", (state) => {
302
+ const { messages } = state;
303
+ const lastMessage = messages[messages.length - 1];
304
+ if (!isAIMessage(lastMessage) || !lastMessage.tool_calls?.length) {
305
+ if (responseFormat != null) return "generate_structured_response";
306
+ return END;
307
+ }
308
+ if (version === "v2") return lastMessage.tool_calls.map((toolCall) => new Send("tools", {
309
+ ...state,
310
+ lg_tool_call: toolCall
311
+ }));
312
+ return "tools";
313
+ }, conditionalMap({
314
+ tools: "tools",
315
+ generate_structured_response: responseFormat != null ? "generate_structured_response" : null,
316
+ [END]: responseFormat != null ? null : END
317
+ }));
318
+ if (shouldReturnDirect.size > 0) allNodeWorkflows.addConditionalEdges("tools", (state) => {
319
+ for (let i = state.messages.length - 1; i >= 0; i -= 1) {
320
+ const message = state.messages[i];
321
+ if (!isToolMessage(message)) break;
322
+ if (message.name !== void 0 && shouldReturnDirect.has(message.name)) return END;
323
+ }
324
+ return entrypoint;
325
+ }, conditionalMap({
326
+ [entrypoint]: entrypoint,
327
+ [END]: END
328
+ }));
329
+ else allNodeWorkflows.addEdge("tools", entrypoint);
330
+ return allNodeWorkflows.compile({
331
+ checkpointer: checkpointer ?? checkpointSaver,
332
+ interruptBefore,
333
+ interruptAfter,
334
+ store,
335
+ name,
336
+ description
337
+ });
491
338
  }
339
+
340
+ //#endregion
341
+ export { createReactAgent, createReactAgentAnnotation };
492
342
  //# sourceMappingURL=react_agent_executor.js.map