@langchain/langgraph 1.0.12 → 1.0.14

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 (381) hide show
  1. package/dist/channels/any_value.cjs +46 -0
  2. package/dist/channels/any_value.cjs.map +1 -0
  3. package/dist/channels/any_value.d.cts +23 -0
  4. package/dist/channels/any_value.d.cts.map +1 -0
  5. package/dist/channels/any_value.d.ts +23 -0
  6. package/dist/channels/any_value.d.ts.map +1 -0
  7. package/dist/channels/any_value.js +46 -0
  8. package/dist/channels/any_value.js.map +1 -0
  9. package/dist/channels/base.cjs +101 -0
  10. package/dist/channels/base.cjs.map +1 -0
  11. package/dist/channels/base.d.cts +79 -0
  12. package/dist/channels/base.d.cts.map +1 -0
  13. package/dist/channels/base.d.ts +79 -0
  14. package/dist/channels/base.d.ts.map +1 -0
  15. package/dist/channels/base.js +97 -0
  16. package/dist/channels/base.js.map +1 -0
  17. package/dist/channels/binop.cjs +49 -0
  18. package/dist/channels/binop.cjs.map +1 -0
  19. package/dist/channels/binop.d.cts +22 -0
  20. package/dist/channels/binop.d.cts.map +1 -0
  21. package/dist/channels/binop.d.ts +22 -0
  22. package/dist/channels/binop.d.ts.map +1 -0
  23. package/dist/channels/binop.js +49 -0
  24. package/dist/channels/binop.js.map +1 -0
  25. package/dist/channels/dynamic_barrier_value.cjs +75 -0
  26. package/dist/channels/dynamic_barrier_value.cjs.map +1 -0
  27. package/dist/channels/dynamic_barrier_value.d.cts +40 -0
  28. package/dist/channels/dynamic_barrier_value.d.cts.map +1 -0
  29. package/dist/channels/dynamic_barrier_value.d.ts +40 -0
  30. package/dist/channels/dynamic_barrier_value.d.ts.map +1 -0
  31. package/dist/channels/dynamic_barrier_value.js +75 -0
  32. package/dist/channels/dynamic_barrier_value.js.map +1 -0
  33. package/dist/channels/ephemeral_value.cjs +46 -0
  34. package/dist/channels/ephemeral_value.cjs.map +1 -0
  35. package/dist/channels/ephemeral_value.d.cts +21 -0
  36. package/dist/channels/ephemeral_value.d.cts.map +1 -0
  37. package/dist/channels/ephemeral_value.d.ts +21 -0
  38. package/dist/channels/ephemeral_value.d.ts.map +1 -0
  39. package/dist/channels/ephemeral_value.js +46 -0
  40. package/dist/channels/ephemeral_value.js.map +1 -0
  41. package/dist/channels/index.cjs +21 -0
  42. package/dist/channels/index.d.cts +9 -0
  43. package/dist/channels/index.d.ts +9 -0
  44. package/dist/channels/index.js +10 -0
  45. package/dist/channels/last_value.cjs +98 -0
  46. package/dist/channels/last_value.cjs.map +1 -0
  47. package/dist/channels/last_value.d.cts +42 -0
  48. package/dist/channels/last_value.d.cts.map +1 -0
  49. package/dist/channels/last_value.d.ts +42 -0
  50. package/dist/channels/last_value.d.ts.map +1 -0
  51. package/dist/channels/last_value.js +97 -0
  52. package/dist/channels/last_value.js.map +1 -0
  53. package/dist/channels/named_barrier_value.cjs +116 -0
  54. package/dist/channels/named_barrier_value.cjs.map +1 -0
  55. package/dist/channels/named_barrier_value.d.cts +44 -0
  56. package/dist/channels/named_barrier_value.d.cts.map +1 -0
  57. package/dist/channels/named_barrier_value.d.ts +44 -0
  58. package/dist/channels/named_barrier_value.d.ts.map +1 -0
  59. package/dist/channels/named_barrier_value.js +114 -0
  60. package/dist/channels/named_barrier_value.js.map +1 -0
  61. package/dist/channels/topic.cjs +65 -0
  62. package/dist/channels/topic.cjs.map +1 -0
  63. package/dist/channels/topic.d.cts +32 -0
  64. package/dist/channels/topic.d.cts.map +1 -0
  65. package/dist/channels/topic.d.ts +32 -0
  66. package/dist/channels/topic.d.ts.map +1 -0
  67. package/dist/channels/topic.js +65 -0
  68. package/dist/channels/topic.js.map +1 -0
  69. package/dist/constants.cjs +406 -0
  70. package/dist/constants.cjs.map +1 -0
  71. package/dist/constants.d.cts +281 -0
  72. package/dist/constants.d.cts.map +1 -0
  73. package/dist/constants.d.ts +281 -0
  74. package/dist/constants.d.ts.map +1 -0
  75. package/dist/constants.js +362 -0
  76. package/dist/constants.js.map +1 -0
  77. package/dist/errors.cjs +163 -0
  78. package/dist/errors.cjs.map +1 -0
  79. package/dist/errors.d.cts +77 -0
  80. package/dist/errors.d.cts.map +1 -0
  81. package/dist/errors.d.ts +77 -0
  82. package/dist/errors.d.ts.map +1 -0
  83. package/dist/errors.js +146 -0
  84. package/dist/errors.js.map +1 -0
  85. package/dist/func/index.cjs +299 -0
  86. package/dist/func/index.cjs.map +1 -0
  87. package/dist/func/index.d.cts +288 -0
  88. package/dist/func/index.d.cts.map +1 -0
  89. package/dist/func/index.d.ts +288 -0
  90. package/dist/func/index.d.ts.map +1 -0
  91. package/dist/func/index.js +297 -0
  92. package/dist/func/index.js.map +1 -0
  93. package/dist/func/types.d.cts +62 -0
  94. package/dist/func/types.d.cts.map +1 -0
  95. package/dist/func/types.d.ts +62 -0
  96. package/dist/func/types.d.ts.map +1 -0
  97. package/dist/graph/annotation.cjs +101 -0
  98. package/dist/graph/annotation.cjs.map +1 -0
  99. package/dist/graph/annotation.d.cts +116 -0
  100. package/dist/graph/annotation.d.cts.map +1 -0
  101. package/dist/graph/annotation.d.ts +116 -0
  102. package/dist/graph/annotation.d.ts.map +1 -0
  103. package/dist/graph/annotation.js +99 -0
  104. package/dist/graph/annotation.js.map +1 -0
  105. package/dist/graph/graph.cjs +420 -0
  106. package/dist/graph/graph.cjs.map +1 -0
  107. package/dist/graph/graph.d.cts +108 -0
  108. package/dist/graph/graph.d.cts.map +1 -0
  109. package/dist/graph/graph.d.ts +108 -0
  110. package/dist/graph/graph.d.ts.map +1 -0
  111. package/dist/graph/graph.js +418 -0
  112. package/dist/graph/graph.js.map +1 -0
  113. package/dist/graph/index.cjs +5 -0
  114. package/dist/graph/index.js +7 -0
  115. package/dist/graph/message.cjs +97 -0
  116. package/dist/graph/message.cjs.map +1 -0
  117. package/dist/graph/message.d.cts +40 -0
  118. package/dist/graph/message.d.cts.map +1 -0
  119. package/dist/graph/message.d.ts +40 -0
  120. package/dist/graph/message.d.ts.map +1 -0
  121. package/dist/graph/message.js +94 -0
  122. package/dist/graph/message.js.map +1 -0
  123. package/dist/graph/messages_annotation.cjs +109 -0
  124. package/dist/graph/messages_annotation.cjs.map +1 -0
  125. package/dist/graph/messages_annotation.d.cts +112 -0
  126. package/dist/graph/messages_annotation.d.cts.map +1 -0
  127. package/dist/graph/messages_annotation.d.ts +112 -0
  128. package/dist/graph/messages_annotation.d.ts.map +1 -0
  129. package/dist/graph/messages_annotation.js +107 -0
  130. package/dist/graph/messages_annotation.js.map +1 -0
  131. package/dist/graph/state.cjs +494 -0
  132. package/dist/graph/state.cjs.map +1 -0
  133. package/dist/graph/state.d.cts +229 -0
  134. package/dist/graph/state.d.cts.map +1 -0
  135. package/dist/graph/state.d.ts +229 -0
  136. package/dist/graph/state.d.ts.map +1 -0
  137. package/dist/graph/state.js +493 -0
  138. package/dist/graph/state.js.map +1 -0
  139. package/dist/graph/zod/index.cjs +10 -0
  140. package/dist/graph/zod/index.d.cts +3 -0
  141. package/dist/graph/zod/index.d.ts +3 -0
  142. package/dist/graph/zod/index.js +5 -0
  143. package/dist/graph/zod/meta.cjs +144 -0
  144. package/dist/graph/zod/meta.cjs.map +1 -0
  145. package/dist/graph/zod/meta.d.cts +115 -0
  146. package/dist/graph/zod/meta.d.cts.map +1 -0
  147. package/dist/graph/zod/meta.d.ts +115 -0
  148. package/dist/graph/zod/meta.d.ts.map +1 -0
  149. package/dist/graph/zod/meta.js +141 -0
  150. package/dist/graph/zod/meta.js.map +1 -0
  151. package/dist/graph/zod/plugin.cjs +39 -0
  152. package/dist/graph/zod/plugin.cjs.map +1 -0
  153. package/dist/graph/zod/plugin.js +40 -0
  154. package/dist/graph/zod/plugin.js.map +1 -0
  155. package/dist/graph/zod/schema.cjs +98 -0
  156. package/dist/graph/zod/schema.cjs.map +1 -0
  157. package/dist/graph/zod/schema.d.cts +38 -0
  158. package/dist/graph/zod/schema.d.cts.map +1 -0
  159. package/dist/graph/zod/schema.d.ts +38 -0
  160. package/dist/graph/zod/schema.d.ts.map +1 -0
  161. package/dist/graph/zod/schema.js +94 -0
  162. package/dist/graph/zod/schema.js.map +1 -0
  163. package/dist/graph/zod/zod-registry.cjs +42 -0
  164. package/dist/graph/zod/zod-registry.cjs.map +1 -0
  165. package/dist/graph/zod/zod-registry.d.cts +49 -0
  166. package/dist/graph/zod/zod-registry.d.cts.map +1 -0
  167. package/dist/graph/zod/zod-registry.d.ts +49 -0
  168. package/dist/graph/zod/zod-registry.d.ts.map +1 -0
  169. package/dist/graph/zod/zod-registry.js +41 -0
  170. package/dist/graph/zod/zod-registry.js.map +1 -0
  171. package/dist/hash.cjs +265 -0
  172. package/dist/hash.cjs.map +1 -0
  173. package/dist/hash.js +263 -0
  174. package/dist/hash.js.map +1 -0
  175. package/dist/index.cjs +111 -0
  176. package/dist/index.cjs.map +1 -0
  177. package/dist/index.d.cts +26 -0
  178. package/dist/index.d.ts +26 -0
  179. package/dist/index.js +22 -0
  180. package/dist/index.js.map +1 -0
  181. package/dist/interrupt.cjs +79 -0
  182. package/dist/interrupt.cjs.map +1 -0
  183. package/dist/interrupt.d.cts +55 -0
  184. package/dist/interrupt.d.cts.map +1 -0
  185. package/dist/interrupt.d.ts +55 -0
  186. package/dist/interrupt.d.ts.map +1 -0
  187. package/dist/interrupt.js +79 -0
  188. package/dist/interrupt.js.map +1 -0
  189. package/dist/prebuilt/agentName.cjs +148 -0
  190. package/dist/prebuilt/agentName.cjs.map +1 -0
  191. package/dist/prebuilt/agentName.d.cts +42 -0
  192. package/dist/prebuilt/agentName.d.cts.map +1 -0
  193. package/dist/prebuilt/agentName.d.ts +42 -0
  194. package/dist/prebuilt/agentName.d.ts.map +1 -0
  195. package/dist/prebuilt/agentName.js +148 -0
  196. package/dist/prebuilt/agentName.js.map +1 -0
  197. package/dist/prebuilt/agent_executor.cjs +41 -0
  198. package/dist/prebuilt/agent_executor.cjs.map +1 -0
  199. package/dist/prebuilt/agent_executor.d.cts +58 -0
  200. package/dist/prebuilt/agent_executor.d.cts.map +1 -0
  201. package/dist/prebuilt/agent_executor.d.ts +58 -0
  202. package/dist/prebuilt/agent_executor.d.ts.map +1 -0
  203. package/dist/prebuilt/agent_executor.js +41 -0
  204. package/dist/prebuilt/agent_executor.js.map +1 -0
  205. package/dist/prebuilt/chat_agent_executor.cjs +62 -0
  206. package/dist/prebuilt/chat_agent_executor.cjs.map +1 -0
  207. package/dist/prebuilt/chat_agent_executor.d.cts +23 -0
  208. package/dist/prebuilt/chat_agent_executor.d.cts.map +1 -0
  209. package/dist/prebuilt/chat_agent_executor.d.ts +23 -0
  210. package/dist/prebuilt/chat_agent_executor.d.ts.map +1 -0
  211. package/dist/prebuilt/chat_agent_executor.js +62 -0
  212. package/dist/prebuilt/chat_agent_executor.js.map +1 -0
  213. package/dist/prebuilt/index.cjs +15 -0
  214. package/dist/prebuilt/index.d.cts +8 -0
  215. package/dist/prebuilt/index.d.ts +8 -0
  216. package/dist/prebuilt/index.js +8 -0
  217. package/dist/prebuilt/interrupt.d.cts +60 -0
  218. package/dist/prebuilt/interrupt.d.cts.map +1 -0
  219. package/dist/prebuilt/interrupt.d.ts +60 -0
  220. package/dist/prebuilt/interrupt.d.ts.map +1 -0
  221. package/dist/prebuilt/react_agent_executor.cjs +339 -0
  222. package/dist/prebuilt/react_agent_executor.cjs.map +1 -0
  223. package/dist/prebuilt/react_agent_executor.d.cts +211 -0
  224. package/dist/prebuilt/react_agent_executor.d.cts.map +1 -0
  225. package/dist/prebuilt/react_agent_executor.d.ts +211 -0
  226. package/dist/prebuilt/react_agent_executor.d.ts.map +1 -0
  227. package/dist/prebuilt/react_agent_executor.js +338 -0
  228. package/dist/prebuilt/react_agent_executor.js.map +1 -0
  229. package/dist/prebuilt/tool_executor.cjs +43 -0
  230. package/dist/prebuilt/tool_executor.cjs.map +1 -0
  231. package/dist/prebuilt/tool_executor.d.cts +40 -0
  232. package/dist/prebuilt/tool_executor.d.cts.map +1 -0
  233. package/dist/prebuilt/tool_executor.d.ts +40 -0
  234. package/dist/prebuilt/tool_executor.d.ts.map +1 -0
  235. package/dist/prebuilt/tool_executor.js +43 -0
  236. package/dist/prebuilt/tool_executor.js.map +1 -0
  237. package/dist/prebuilt/tool_node.cjs +247 -0
  238. package/dist/prebuilt/tool_node.cjs.map +1 -0
  239. package/dist/prebuilt/tool_node.d.cts +180 -0
  240. package/dist/prebuilt/tool_node.d.cts.map +1 -0
  241. package/dist/prebuilt/tool_node.d.ts +180 -0
  242. package/dist/prebuilt/tool_node.d.ts.map +1 -0
  243. package/dist/prebuilt/tool_node.js +246 -0
  244. package/dist/prebuilt/tool_node.js.map +1 -0
  245. package/dist/pregel/algo.cjs +565 -0
  246. package/dist/pregel/algo.cjs.map +1 -0
  247. package/dist/pregel/algo.d.cts +13 -0
  248. package/dist/pregel/algo.d.cts.map +1 -0
  249. package/dist/pregel/algo.d.ts +13 -0
  250. package/dist/pregel/algo.d.ts.map +1 -0
  251. package/dist/pregel/algo.js +560 -0
  252. package/dist/pregel/algo.js.map +1 -0
  253. package/dist/pregel/call.cjs +50 -0
  254. package/dist/pregel/call.cjs.map +1 -0
  255. package/dist/pregel/call.js +48 -0
  256. package/dist/pregel/call.js.map +1 -0
  257. package/dist/pregel/debug.cjs +172 -0
  258. package/dist/pregel/debug.cjs.map +1 -0
  259. package/dist/pregel/debug.js +166 -0
  260. package/dist/pregel/debug.js.map +1 -0
  261. package/dist/pregel/index.cjs +1163 -0
  262. package/dist/pregel/index.cjs.map +1 -0
  263. package/dist/pregel/index.d.cts +498 -0
  264. package/dist/pregel/index.d.cts.map +1 -0
  265. package/dist/pregel/index.d.ts +498 -0
  266. package/dist/pregel/index.d.ts.map +1 -0
  267. package/dist/pregel/index.js +1162 -0
  268. package/dist/pregel/index.js.map +1 -0
  269. package/dist/pregel/io.cjs +140 -0
  270. package/dist/pregel/io.cjs.map +1 -0
  271. package/dist/pregel/io.js +135 -0
  272. package/dist/pregel/io.js.map +1 -0
  273. package/dist/pregel/loop.cjs +545 -0
  274. package/dist/pregel/loop.cjs.map +1 -0
  275. package/dist/pregel/loop.js +545 -0
  276. package/dist/pregel/loop.js.map +1 -0
  277. package/dist/pregel/messages.cjs +107 -0
  278. package/dist/pregel/messages.cjs.map +1 -0
  279. package/dist/pregel/messages.js +107 -0
  280. package/dist/pregel/messages.js.map +1 -0
  281. package/dist/pregel/read.cjs +151 -0
  282. package/dist/pregel/read.cjs.map +1 -0
  283. package/dist/pregel/read.d.cts +45 -0
  284. package/dist/pregel/read.d.cts.map +1 -0
  285. package/dist/pregel/read.d.ts +45 -0
  286. package/dist/pregel/read.d.ts.map +1 -0
  287. package/dist/pregel/read.js +150 -0
  288. package/dist/pregel/read.js.map +1 -0
  289. package/dist/pregel/remote.cjs +341 -0
  290. package/dist/pregel/remote.cjs.map +1 -0
  291. package/dist/pregel/remote.d.cts +118 -0
  292. package/dist/pregel/remote.d.cts.map +1 -0
  293. package/dist/pregel/remote.d.ts +118 -0
  294. package/dist/pregel/remote.d.ts.map +1 -0
  295. package/dist/pregel/remote.js +341 -0
  296. package/dist/pregel/remote.js.map +1 -0
  297. package/dist/pregel/retry.cjs +91 -0
  298. package/dist/pregel/retry.cjs.map +1 -0
  299. package/dist/pregel/retry.js +91 -0
  300. package/dist/pregel/retry.js.map +1 -0
  301. package/dist/pregel/runnable_types.d.cts +43 -0
  302. package/dist/pregel/runnable_types.d.cts.map +1 -0
  303. package/dist/pregel/runnable_types.d.ts +43 -0
  304. package/dist/pregel/runnable_types.d.ts.map +1 -0
  305. package/dist/pregel/runner.cjs +228 -0
  306. package/dist/pregel/runner.cjs.map +1 -0
  307. package/dist/pregel/runner.js +228 -0
  308. package/dist/pregel/runner.js.map +1 -0
  309. package/dist/pregel/stream.cjs +197 -0
  310. package/dist/pregel/stream.cjs.map +1 -0
  311. package/dist/pregel/stream.js +195 -0
  312. package/dist/pregel/stream.js.map +1 -0
  313. package/dist/pregel/types.cjs +27 -0
  314. package/dist/pregel/types.cjs.map +1 -0
  315. package/dist/pregel/types.d.cts +440 -0
  316. package/dist/pregel/types.d.cts.map +1 -0
  317. package/dist/pregel/types.d.ts +440 -0
  318. package/dist/pregel/types.d.ts.map +1 -0
  319. package/dist/pregel/types.js +25 -0
  320. package/dist/pregel/types.js.map +1 -0
  321. package/dist/pregel/utils/config.cjs +121 -0
  322. package/dist/pregel/utils/config.cjs.map +1 -0
  323. package/dist/pregel/utils/config.d.cts +36 -0
  324. package/dist/pregel/utils/config.d.cts.map +1 -0
  325. package/dist/pregel/utils/config.d.ts +36 -0
  326. package/dist/pregel/utils/config.d.ts.map +1 -0
  327. package/dist/pregel/utils/config.js +115 -0
  328. package/dist/pregel/utils/config.js.map +1 -0
  329. package/dist/pregel/utils/index.cjs +102 -0
  330. package/dist/pregel/utils/index.cjs.map +1 -0
  331. package/dist/pregel/utils/index.d.cts +51 -0
  332. package/dist/pregel/utils/index.d.cts.map +1 -0
  333. package/dist/pregel/utils/index.d.ts +51 -0
  334. package/dist/pregel/utils/index.d.ts.map +1 -0
  335. package/dist/pregel/utils/index.js +96 -0
  336. package/dist/pregel/utils/index.js.map +1 -0
  337. package/dist/pregel/utils/subgraph.cjs +18 -0
  338. package/dist/pregel/utils/subgraph.cjs.map +1 -0
  339. package/dist/pregel/utils/subgraph.js +16 -0
  340. package/dist/pregel/utils/subgraph.js.map +1 -0
  341. package/dist/pregel/validate.cjs +45 -0
  342. package/dist/pregel/validate.cjs.map +1 -0
  343. package/dist/pregel/validate.js +44 -0
  344. package/dist/pregel/validate.js.map +1 -0
  345. package/dist/pregel/write.cjs +96 -0
  346. package/dist/pregel/write.cjs.map +1 -0
  347. package/dist/pregel/write.d.cts +31 -0
  348. package/dist/pregel/write.d.cts.map +1 -0
  349. package/dist/pregel/write.d.ts +31 -0
  350. package/dist/pregel/write.d.ts.map +1 -0
  351. package/dist/pregel/write.js +95 -0
  352. package/dist/pregel/write.js.map +1 -0
  353. package/dist/remote.cjs +3 -0
  354. package/dist/remote.d.cts +2 -0
  355. package/dist/remote.d.ts +2 -0
  356. package/dist/remote.js +3 -0
  357. package/dist/setup/async_local_storage.cjs +11 -0
  358. package/dist/setup/async_local_storage.cjs.map +1 -0
  359. package/dist/setup/async_local_storage.js +11 -0
  360. package/dist/setup/async_local_storage.js.map +1 -0
  361. package/dist/utils.cjs +86 -0
  362. package/dist/utils.cjs.map +1 -0
  363. package/dist/utils.d.cts +25 -0
  364. package/dist/utils.d.cts.map +1 -0
  365. package/dist/utils.d.ts +25 -0
  366. package/dist/utils.d.ts.map +1 -0
  367. package/dist/utils.js +80 -0
  368. package/dist/utils.js.map +1 -0
  369. package/dist/web.cjs +95 -0
  370. package/dist/web.d.cts +23 -0
  371. package/dist/web.d.ts +23 -0
  372. package/dist/web.js +15 -0
  373. package/dist/writer.cjs +14 -0
  374. package/dist/writer.cjs.map +1 -0
  375. package/dist/writer.d.cts +6 -0
  376. package/dist/writer.d.cts.map +1 -0
  377. package/dist/writer.d.ts +6 -0
  378. package/dist/writer.d.ts.map +1 -0
  379. package/dist/writer.js +14 -0
  380. package/dist/writer.js.map +1 -0
  381. package/package.json +25 -27
@@ -0,0 +1,180 @@
1
+ import { RunnableCallable } from "../utils.cjs";
2
+ import { Command, END } from "../constants.cjs";
3
+ import { MessagesAnnotation } from "../graph/messages_annotation.cjs";
4
+ import { RunnableConfig, RunnableToolLike } from "@langchain/core/runnables";
5
+ import { BaseMessage, ToolMessage } from "@langchain/core/messages";
6
+ import { DynamicTool, StructuredToolInterface } from "@langchain/core/tools";
7
+ import { ToolCall } from "@langchain/core/messages/tool";
8
+
9
+ //#region src/prebuilt/tool_node.d.ts
10
+ type ToolNodeOptions = {
11
+ name?: string;
12
+ tags?: string[];
13
+ handleToolErrors?: boolean;
14
+ };
15
+ /**
16
+ * A node that runs the tools requested in the last AIMessage. It can be used
17
+ * either in StateGraph with a "messages" key or in MessageGraph. If multiple
18
+ * tool calls are requested, they will be run in parallel. The output will be
19
+ * a list of ToolMessages, one for each tool call.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { ToolNode } from "@langchain/langgraph/prebuilt";
24
+ * import { tool } from "@langchain/core/tools";
25
+ * import { z } from "zod";
26
+ * import { AIMessage } from "@langchain/core/messages";
27
+ *
28
+ * const getWeather = tool((input) => {
29
+ * if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
30
+ * return "It's 60 degrees and foggy.";
31
+ * } else {
32
+ * return "It's 90 degrees and sunny.";
33
+ * }
34
+ * }, {
35
+ * name: "get_weather",
36
+ * description: "Call to get the current weather.",
37
+ * schema: z.object({
38
+ * location: z.string().describe("Location to get the weather for."),
39
+ * }),
40
+ * });
41
+ *
42
+ * const tools = [getWeather];
43
+ * const toolNode = new ToolNode(tools);
44
+ *
45
+ * const messageWithSingleToolCall = new AIMessage({
46
+ * content: "",
47
+ * tool_calls: [
48
+ * {
49
+ * name: "get_weather",
50
+ * args: { location: "sf" },
51
+ * id: "tool_call_id",
52
+ * type: "tool_call",
53
+ * }
54
+ * ]
55
+ * })
56
+ *
57
+ * await toolNode.invoke({ messages: [messageWithSingleToolCall] });
58
+ * // Returns tool invocation responses as:
59
+ * // { messages: ToolMessage[] }
60
+ * ```
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * import {
65
+ * StateGraph,
66
+ * MessagesAnnotation,
67
+ * } from "@langchain/langgraph";
68
+ * import { ToolNode } from "@langchain/langgraph/prebuilt";
69
+ * import { tool } from "@langchain/core/tools";
70
+ * import { z } from "zod";
71
+ * import { ChatAnthropic } from "@langchain/anthropic";
72
+ *
73
+ * const getWeather = tool((input) => {
74
+ * if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
75
+ * return "It's 60 degrees and foggy.";
76
+ * } else {
77
+ * return "It's 90 degrees and sunny.";
78
+ * }
79
+ * }, {
80
+ * name: "get_weather",
81
+ * description: "Call to get the current weather.",
82
+ * schema: z.object({
83
+ * location: z.string().describe("Location to get the weather for."),
84
+ * }),
85
+ * });
86
+ *
87
+ * const tools = [getWeather];
88
+ * const modelWithTools = new ChatAnthropic({
89
+ * model: "claude-3-haiku-20240307",
90
+ * temperature: 0
91
+ * }).bindTools(tools);
92
+ *
93
+ * const toolNodeForGraph = new ToolNode(tools)
94
+ *
95
+ * const shouldContinue = (state: typeof MessagesAnnotation.State) => {
96
+ * const { messages } = state;
97
+ * const lastMessage = messages[messages.length - 1];
98
+ * if ("tool_calls" in lastMessage && Array.isArray(lastMessage.tool_calls) && lastMessage.tool_calls?.length) {
99
+ * return "tools";
100
+ * }
101
+ * return "__end__";
102
+ * }
103
+ *
104
+ * const callModel = async (state: typeof MessagesAnnotation.State) => {
105
+ * const { messages } = state;
106
+ * const response = await modelWithTools.invoke(messages);
107
+ * return { messages: response };
108
+ * }
109
+ *
110
+ * const graph = new StateGraph(MessagesAnnotation)
111
+ * .addNode("agent", callModel)
112
+ * .addNode("tools", toolNodeForGraph)
113
+ * .addEdge("__start__", "agent")
114
+ * .addConditionalEdges("agent", shouldContinue)
115
+ * .addEdge("tools", "agent")
116
+ * .compile();
117
+ *
118
+ * const inputs = {
119
+ * messages: [{ role: "user", content: "what is the weather in SF?" }],
120
+ * };
121
+ *
122
+ * const stream = await graph.stream(inputs, {
123
+ * streamMode: "values",
124
+ * });
125
+ *
126
+ * for await (const { messages } of stream) {
127
+ * console.log(messages);
128
+ * }
129
+ * // Returns the messages in the state at each step of execution
130
+ * ```
131
+ */
132
+ declare class ToolNode<T = any> extends RunnableCallable<T, T> {
133
+ tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[];
134
+ handleToolErrors: boolean;
135
+ trace: boolean;
136
+ constructor(tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[], options?: ToolNodeOptions);
137
+ protected runTool(call: ToolCall, config: RunnableConfig): Promise<ToolMessage | Command>;
138
+ protected run(input: unknown, config: RunnableConfig): Promise<T>;
139
+ }
140
+ /**
141
+ * A conditional edge function that determines whether to route to a tools node or end the graph.
142
+ *
143
+ * This function is designed to be used as a conditional edge in a LangGraph state graph to implement
144
+ * the common pattern of checking if an AI message contains tool calls that need to be executed.
145
+ *
146
+ * @param state - The current state of the graph, which can be either:
147
+ * - An array of `BaseMessage` objects, where the last message is checked for tool calls
148
+ * - A state object conforming to `MessagesAnnotation.State`, which contains a `messages` array
149
+ *
150
+ * @returns A string indicating the next node to route to:
151
+ * - `"tools"` - If the last message contains tool calls that need to be executed
152
+ * - `END` - If there are no tool calls, indicating the graph should terminate
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * import { StateGraph, MessagesAnnotation, END, START } from "@langchain/langgraph";
157
+ * import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt";
158
+ *
159
+ * const graph = new StateGraph(MessagesAnnotation)
160
+ * .addNode("agent", agentNode)
161
+ * .addNode("tools", new ToolNode([searchTool, calculatorTool]))
162
+ * .addEdge(START, "agent")
163
+ * .addConditionalEdges("agent", toolsCondition, ["tools", END])
164
+ * .addEdge("tools", "agent")
165
+ * .compile();
166
+ * ```
167
+ *
168
+ * @remarks
169
+ * The function checks the last message in the state for the presence of `tool_calls`.
170
+ * If the message is an `AIMessage` with one or more tool calls, it returns `"tools"`,
171
+ * indicating that the graph should route to a tools node (typically a `ToolNode`) to
172
+ * execute those tool calls. Otherwise, it returns `END` to terminate the graph execution.
173
+ *
174
+ * This is a common pattern in agentic workflows where an AI model decides whether to
175
+ * use tools or provide a final response.
176
+ */
177
+ declare function toolsCondition(state: BaseMessage[] | typeof MessagesAnnotation.State): "tools" | typeof END;
178
+ //#endregion
179
+ export { ToolNode, ToolNodeOptions, toolsCondition };
180
+ //# sourceMappingURL=tool_node.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool_node.d.cts","names":["BaseMessage","ToolMessage","RunnableConfig","RunnableToolLike","DynamicTool","StructuredToolInterface","ToolCall","RunnableCallable","MessagesAnnotation","END","Command","ToolNodeOptions","ToolNode","T","Promise","toolsCondition","State"],"sources":["../../src/prebuilt/tool_node.d.ts"],"sourcesContent":["import { BaseMessage, ToolMessage } from \"@langchain/core/messages\";\nimport { RunnableConfig, RunnableToolLike } from \"@langchain/core/runnables\";\nimport { DynamicTool, StructuredToolInterface } from \"@langchain/core/tools\";\nimport type { ToolCall } from \"@langchain/core/messages/tool\";\nimport { RunnableCallable } from \"../utils.js\";\nimport { MessagesAnnotation } from \"../graph/messages_annotation.js\";\nimport { END, Command } from \"../constants.js\";\nexport type ToolNodeOptions = {\n name?: string;\n tags?: string[];\n handleToolErrors?: boolean;\n};\n/**\n * A node that runs the tools requested in the last AIMessage. It can be used\n * either in StateGraph with a \"messages\" key or in MessageGraph. If multiple\n * tool calls are requested, they will be run in parallel. The output will be\n * a list of ToolMessages, one for each tool call.\n *\n * @example\n * ```ts\n * import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n * import { tool } from \"@langchain/core/tools\";\n * import { z } from \"zod\";\n * import { AIMessage } from \"@langchain/core/messages\";\n *\n * const getWeather = tool((input) => {\n * if ([\"sf\", \"san francisco\"].includes(input.location.toLowerCase())) {\n * return \"It's 60 degrees and foggy.\";\n * } else {\n * return \"It's 90 degrees and sunny.\";\n * }\n * }, {\n * name: \"get_weather\",\n * description: \"Call to get the current weather.\",\n * schema: z.object({\n * location: z.string().describe(\"Location to get the weather for.\"),\n * }),\n * });\n *\n * const tools = [getWeather];\n * const toolNode = new ToolNode(tools);\n *\n * const messageWithSingleToolCall = new AIMessage({\n * content: \"\",\n * tool_calls: [\n * {\n * name: \"get_weather\",\n * args: { location: \"sf\" },\n * id: \"tool_call_id\",\n * type: \"tool_call\",\n * }\n * ]\n * })\n *\n * await toolNode.invoke({ messages: [messageWithSingleToolCall] });\n * // Returns tool invocation responses as:\n * // { messages: ToolMessage[] }\n * ```\n *\n * @example\n * ```ts\n * import {\n * StateGraph,\n * MessagesAnnotation,\n * } from \"@langchain/langgraph\";\n * import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n * import { tool } from \"@langchain/core/tools\";\n * import { z } from \"zod\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n *\n * const getWeather = tool((input) => {\n * if ([\"sf\", \"san francisco\"].includes(input.location.toLowerCase())) {\n * return \"It's 60 degrees and foggy.\";\n * } else {\n * return \"It's 90 degrees and sunny.\";\n * }\n * }, {\n * name: \"get_weather\",\n * description: \"Call to get the current weather.\",\n * schema: z.object({\n * location: z.string().describe(\"Location to get the weather for.\"),\n * }),\n * });\n *\n * const tools = [getWeather];\n * const modelWithTools = new ChatAnthropic({\n * model: \"claude-3-haiku-20240307\",\n * temperature: 0\n * }).bindTools(tools);\n *\n * const toolNodeForGraph = new ToolNode(tools)\n *\n * const shouldContinue = (state: typeof MessagesAnnotation.State) => {\n * const { messages } = state;\n * const lastMessage = messages[messages.length - 1];\n * if (\"tool_calls\" in lastMessage && Array.isArray(lastMessage.tool_calls) && lastMessage.tool_calls?.length) {\n * return \"tools\";\n * }\n * return \"__end__\";\n * }\n *\n * const callModel = async (state: typeof MessagesAnnotation.State) => {\n * const { messages } = state;\n * const response = await modelWithTools.invoke(messages);\n * return { messages: response };\n * }\n *\n * const graph = new StateGraph(MessagesAnnotation)\n * .addNode(\"agent\", callModel)\n * .addNode(\"tools\", toolNodeForGraph)\n * .addEdge(\"__start__\", \"agent\")\n * .addConditionalEdges(\"agent\", shouldContinue)\n * .addEdge(\"tools\", \"agent\")\n * .compile();\n *\n * const inputs = {\n * messages: [{ role: \"user\", content: \"what is the weather in SF?\" }],\n * };\n *\n * const stream = await graph.stream(inputs, {\n * streamMode: \"values\",\n * });\n *\n * for await (const { messages } of stream) {\n * console.log(messages);\n * }\n * // Returns the messages in the state at each step of execution\n * ```\n */\nexport declare class ToolNode<T = any> extends RunnableCallable<T, T> {\n tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[];\n handleToolErrors: boolean;\n trace: boolean;\n constructor(tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[], options?: ToolNodeOptions);\n protected runTool(call: ToolCall, config: RunnableConfig): Promise<ToolMessage | Command>;\n protected run(input: unknown, config: RunnableConfig): Promise<T>;\n}\n/**\n * A conditional edge function that determines whether to route to a tools node or end the graph.\n *\n * This function is designed to be used as a conditional edge in a LangGraph state graph to implement\n * the common pattern of checking if an AI message contains tool calls that need to be executed.\n *\n * @param state - The current state of the graph, which can be either:\n * - An array of `BaseMessage` objects, where the last message is checked for tool calls\n * - A state object conforming to `MessagesAnnotation.State`, which contains a `messages` array\n *\n * @returns A string indicating the next node to route to:\n * - `\"tools\"` - If the last message contains tool calls that need to be executed\n * - `END` - If there are no tool calls, indicating the graph should terminate\n *\n * @example\n * ```typescript\n * import { StateGraph, MessagesAnnotation, END, START } from \"@langchain/langgraph\";\n * import { ToolNode, toolsCondition } from \"@langchain/langgraph/prebuilt\";\n *\n * const graph = new StateGraph(MessagesAnnotation)\n * .addNode(\"agent\", agentNode)\n * .addNode(\"tools\", new ToolNode([searchTool, calculatorTool]))\n * .addEdge(START, \"agent\")\n * .addConditionalEdges(\"agent\", toolsCondition, [\"tools\", END])\n * .addEdge(\"tools\", \"agent\")\n * .compile();\n * ```\n *\n * @remarks\n * The function checks the last message in the state for the presence of `tool_calls`.\n * If the message is an `AIMessage` with one or more tool calls, it returns `\"tools\"`,\n * indicating that the graph should route to a tools node (typically a `ToolNode`) to\n * execute those tool calls. Otherwise, it returns `END` to terminate the graph execution.\n *\n * This is a common pattern in agentic workflows where an AI model decides whether to\n * use tools or provide a final response.\n */\nexport declare function toolsCondition(state: BaseMessage[] | typeof MessagesAnnotation.State): \"tools\" | typeof END;\n"],"mappings":";;;;;;;;;KAOYW,eAAAA;;EAAAA,IAAAA,CAAAA,EAAAA,MAAAA,EAAAA;EA0HSC,gBAAQ,CAAA,EAAA,OAAA;CAAA;;;;;;;;;;;;;;;;;;;;AA6C7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA7CqBA,0BAA0BL,iBAAiBM,GAAGA;UACvDR,0BAA0BD,cAAcD;;;sBAG5BE,0BAA0BD,cAAcD,+BAA+BQ;0BACnEL,kBAAkBJ,iBAAiBY,QAAQb,cAAcS;wCAC3CR,iBAAiBY,QAAQD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuC3CE,cAAAA,QAAsBf,uBAAuBQ,kBAAAA,CAAmBQ,yBAAyBP"}
@@ -0,0 +1,180 @@
1
+ import { RunnableCallable } from "../utils.js";
2
+ import { Command, END } from "../constants.js";
3
+ import { MessagesAnnotation } from "../graph/messages_annotation.js";
4
+ import { RunnableConfig, RunnableToolLike } from "@langchain/core/runnables";
5
+ import { BaseMessage, ToolMessage } from "@langchain/core/messages";
6
+ import { DynamicTool, StructuredToolInterface } from "@langchain/core/tools";
7
+ import { ToolCall } from "@langchain/core/messages/tool";
8
+
9
+ //#region src/prebuilt/tool_node.d.ts
10
+ type ToolNodeOptions = {
11
+ name?: string;
12
+ tags?: string[];
13
+ handleToolErrors?: boolean;
14
+ };
15
+ /**
16
+ * A node that runs the tools requested in the last AIMessage. It can be used
17
+ * either in StateGraph with a "messages" key or in MessageGraph. If multiple
18
+ * tool calls are requested, they will be run in parallel. The output will be
19
+ * a list of ToolMessages, one for each tool call.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { ToolNode } from "@langchain/langgraph/prebuilt";
24
+ * import { tool } from "@langchain/core/tools";
25
+ * import { z } from "zod";
26
+ * import { AIMessage } from "@langchain/core/messages";
27
+ *
28
+ * const getWeather = tool((input) => {
29
+ * if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
30
+ * return "It's 60 degrees and foggy.";
31
+ * } else {
32
+ * return "It's 90 degrees and sunny.";
33
+ * }
34
+ * }, {
35
+ * name: "get_weather",
36
+ * description: "Call to get the current weather.",
37
+ * schema: z.object({
38
+ * location: z.string().describe("Location to get the weather for."),
39
+ * }),
40
+ * });
41
+ *
42
+ * const tools = [getWeather];
43
+ * const toolNode = new ToolNode(tools);
44
+ *
45
+ * const messageWithSingleToolCall = new AIMessage({
46
+ * content: "",
47
+ * tool_calls: [
48
+ * {
49
+ * name: "get_weather",
50
+ * args: { location: "sf" },
51
+ * id: "tool_call_id",
52
+ * type: "tool_call",
53
+ * }
54
+ * ]
55
+ * })
56
+ *
57
+ * await toolNode.invoke({ messages: [messageWithSingleToolCall] });
58
+ * // Returns tool invocation responses as:
59
+ * // { messages: ToolMessage[] }
60
+ * ```
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * import {
65
+ * StateGraph,
66
+ * MessagesAnnotation,
67
+ * } from "@langchain/langgraph";
68
+ * import { ToolNode } from "@langchain/langgraph/prebuilt";
69
+ * import { tool } from "@langchain/core/tools";
70
+ * import { z } from "zod";
71
+ * import { ChatAnthropic } from "@langchain/anthropic";
72
+ *
73
+ * const getWeather = tool((input) => {
74
+ * if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
75
+ * return "It's 60 degrees and foggy.";
76
+ * } else {
77
+ * return "It's 90 degrees and sunny.";
78
+ * }
79
+ * }, {
80
+ * name: "get_weather",
81
+ * description: "Call to get the current weather.",
82
+ * schema: z.object({
83
+ * location: z.string().describe("Location to get the weather for."),
84
+ * }),
85
+ * });
86
+ *
87
+ * const tools = [getWeather];
88
+ * const modelWithTools = new ChatAnthropic({
89
+ * model: "claude-3-haiku-20240307",
90
+ * temperature: 0
91
+ * }).bindTools(tools);
92
+ *
93
+ * const toolNodeForGraph = new ToolNode(tools)
94
+ *
95
+ * const shouldContinue = (state: typeof MessagesAnnotation.State) => {
96
+ * const { messages } = state;
97
+ * const lastMessage = messages[messages.length - 1];
98
+ * if ("tool_calls" in lastMessage && Array.isArray(lastMessage.tool_calls) && lastMessage.tool_calls?.length) {
99
+ * return "tools";
100
+ * }
101
+ * return "__end__";
102
+ * }
103
+ *
104
+ * const callModel = async (state: typeof MessagesAnnotation.State) => {
105
+ * const { messages } = state;
106
+ * const response = await modelWithTools.invoke(messages);
107
+ * return { messages: response };
108
+ * }
109
+ *
110
+ * const graph = new StateGraph(MessagesAnnotation)
111
+ * .addNode("agent", callModel)
112
+ * .addNode("tools", toolNodeForGraph)
113
+ * .addEdge("__start__", "agent")
114
+ * .addConditionalEdges("agent", shouldContinue)
115
+ * .addEdge("tools", "agent")
116
+ * .compile();
117
+ *
118
+ * const inputs = {
119
+ * messages: [{ role: "user", content: "what is the weather in SF?" }],
120
+ * };
121
+ *
122
+ * const stream = await graph.stream(inputs, {
123
+ * streamMode: "values",
124
+ * });
125
+ *
126
+ * for await (const { messages } of stream) {
127
+ * console.log(messages);
128
+ * }
129
+ * // Returns the messages in the state at each step of execution
130
+ * ```
131
+ */
132
+ declare class ToolNode<T = any> extends RunnableCallable<T, T> {
133
+ tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[];
134
+ handleToolErrors: boolean;
135
+ trace: boolean;
136
+ constructor(tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[], options?: ToolNodeOptions);
137
+ protected runTool(call: ToolCall, config: RunnableConfig): Promise<ToolMessage | Command>;
138
+ protected run(input: unknown, config: RunnableConfig): Promise<T>;
139
+ }
140
+ /**
141
+ * A conditional edge function that determines whether to route to a tools node or end the graph.
142
+ *
143
+ * This function is designed to be used as a conditional edge in a LangGraph state graph to implement
144
+ * the common pattern of checking if an AI message contains tool calls that need to be executed.
145
+ *
146
+ * @param state - The current state of the graph, which can be either:
147
+ * - An array of `BaseMessage` objects, where the last message is checked for tool calls
148
+ * - A state object conforming to `MessagesAnnotation.State`, which contains a `messages` array
149
+ *
150
+ * @returns A string indicating the next node to route to:
151
+ * - `"tools"` - If the last message contains tool calls that need to be executed
152
+ * - `END` - If there are no tool calls, indicating the graph should terminate
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * import { StateGraph, MessagesAnnotation, END, START } from "@langchain/langgraph";
157
+ * import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt";
158
+ *
159
+ * const graph = new StateGraph(MessagesAnnotation)
160
+ * .addNode("agent", agentNode)
161
+ * .addNode("tools", new ToolNode([searchTool, calculatorTool]))
162
+ * .addEdge(START, "agent")
163
+ * .addConditionalEdges("agent", toolsCondition, ["tools", END])
164
+ * .addEdge("tools", "agent")
165
+ * .compile();
166
+ * ```
167
+ *
168
+ * @remarks
169
+ * The function checks the last message in the state for the presence of `tool_calls`.
170
+ * If the message is an `AIMessage` with one or more tool calls, it returns `"tools"`,
171
+ * indicating that the graph should route to a tools node (typically a `ToolNode`) to
172
+ * execute those tool calls. Otherwise, it returns `END` to terminate the graph execution.
173
+ *
174
+ * This is a common pattern in agentic workflows where an AI model decides whether to
175
+ * use tools or provide a final response.
176
+ */
177
+ declare function toolsCondition(state: BaseMessage[] | typeof MessagesAnnotation.State): "tools" | typeof END;
178
+ //#endregion
179
+ export { ToolNode, ToolNodeOptions, toolsCondition };
180
+ //# sourceMappingURL=tool_node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool_node.d.ts","names":["BaseMessage","ToolMessage","RunnableConfig","RunnableToolLike","DynamicTool","StructuredToolInterface","ToolCall","RunnableCallable","MessagesAnnotation","END","Command","ToolNodeOptions","ToolNode","T","Promise","toolsCondition","State"],"sources":["../../src/prebuilt/tool_node.d.ts"],"sourcesContent":["import { BaseMessage, ToolMessage } from \"@langchain/core/messages\";\nimport { RunnableConfig, RunnableToolLike } from \"@langchain/core/runnables\";\nimport { DynamicTool, StructuredToolInterface } from \"@langchain/core/tools\";\nimport type { ToolCall } from \"@langchain/core/messages/tool\";\nimport { RunnableCallable } from \"../utils.js\";\nimport { MessagesAnnotation } from \"../graph/messages_annotation.js\";\nimport { END, Command } from \"../constants.js\";\nexport type ToolNodeOptions = {\n name?: string;\n tags?: string[];\n handleToolErrors?: boolean;\n};\n/**\n * A node that runs the tools requested in the last AIMessage. It can be used\n * either in StateGraph with a \"messages\" key or in MessageGraph. If multiple\n * tool calls are requested, they will be run in parallel. The output will be\n * a list of ToolMessages, one for each tool call.\n *\n * @example\n * ```ts\n * import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n * import { tool } from \"@langchain/core/tools\";\n * import { z } from \"zod\";\n * import { AIMessage } from \"@langchain/core/messages\";\n *\n * const getWeather = tool((input) => {\n * if ([\"sf\", \"san francisco\"].includes(input.location.toLowerCase())) {\n * return \"It's 60 degrees and foggy.\";\n * } else {\n * return \"It's 90 degrees and sunny.\";\n * }\n * }, {\n * name: \"get_weather\",\n * description: \"Call to get the current weather.\",\n * schema: z.object({\n * location: z.string().describe(\"Location to get the weather for.\"),\n * }),\n * });\n *\n * const tools = [getWeather];\n * const toolNode = new ToolNode(tools);\n *\n * const messageWithSingleToolCall = new AIMessage({\n * content: \"\",\n * tool_calls: [\n * {\n * name: \"get_weather\",\n * args: { location: \"sf\" },\n * id: \"tool_call_id\",\n * type: \"tool_call\",\n * }\n * ]\n * })\n *\n * await toolNode.invoke({ messages: [messageWithSingleToolCall] });\n * // Returns tool invocation responses as:\n * // { messages: ToolMessage[] }\n * ```\n *\n * @example\n * ```ts\n * import {\n * StateGraph,\n * MessagesAnnotation,\n * } from \"@langchain/langgraph\";\n * import { ToolNode } from \"@langchain/langgraph/prebuilt\";\n * import { tool } from \"@langchain/core/tools\";\n * import { z } from \"zod\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n *\n * const getWeather = tool((input) => {\n * if ([\"sf\", \"san francisco\"].includes(input.location.toLowerCase())) {\n * return \"It's 60 degrees and foggy.\";\n * } else {\n * return \"It's 90 degrees and sunny.\";\n * }\n * }, {\n * name: \"get_weather\",\n * description: \"Call to get the current weather.\",\n * schema: z.object({\n * location: z.string().describe(\"Location to get the weather for.\"),\n * }),\n * });\n *\n * const tools = [getWeather];\n * const modelWithTools = new ChatAnthropic({\n * model: \"claude-3-haiku-20240307\",\n * temperature: 0\n * }).bindTools(tools);\n *\n * const toolNodeForGraph = new ToolNode(tools)\n *\n * const shouldContinue = (state: typeof MessagesAnnotation.State) => {\n * const { messages } = state;\n * const lastMessage = messages[messages.length - 1];\n * if (\"tool_calls\" in lastMessage && Array.isArray(lastMessage.tool_calls) && lastMessage.tool_calls?.length) {\n * return \"tools\";\n * }\n * return \"__end__\";\n * }\n *\n * const callModel = async (state: typeof MessagesAnnotation.State) => {\n * const { messages } = state;\n * const response = await modelWithTools.invoke(messages);\n * return { messages: response };\n * }\n *\n * const graph = new StateGraph(MessagesAnnotation)\n * .addNode(\"agent\", callModel)\n * .addNode(\"tools\", toolNodeForGraph)\n * .addEdge(\"__start__\", \"agent\")\n * .addConditionalEdges(\"agent\", shouldContinue)\n * .addEdge(\"tools\", \"agent\")\n * .compile();\n *\n * const inputs = {\n * messages: [{ role: \"user\", content: \"what is the weather in SF?\" }],\n * };\n *\n * const stream = await graph.stream(inputs, {\n * streamMode: \"values\",\n * });\n *\n * for await (const { messages } of stream) {\n * console.log(messages);\n * }\n * // Returns the messages in the state at each step of execution\n * ```\n */\nexport declare class ToolNode<T = any> extends RunnableCallable<T, T> {\n tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[];\n handleToolErrors: boolean;\n trace: boolean;\n constructor(tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[], options?: ToolNodeOptions);\n protected runTool(call: ToolCall, config: RunnableConfig): Promise<ToolMessage | Command>;\n protected run(input: unknown, config: RunnableConfig): Promise<T>;\n}\n/**\n * A conditional edge function that determines whether to route to a tools node or end the graph.\n *\n * This function is designed to be used as a conditional edge in a LangGraph state graph to implement\n * the common pattern of checking if an AI message contains tool calls that need to be executed.\n *\n * @param state - The current state of the graph, which can be either:\n * - An array of `BaseMessage` objects, where the last message is checked for tool calls\n * - A state object conforming to `MessagesAnnotation.State`, which contains a `messages` array\n *\n * @returns A string indicating the next node to route to:\n * - `\"tools\"` - If the last message contains tool calls that need to be executed\n * - `END` - If there are no tool calls, indicating the graph should terminate\n *\n * @example\n * ```typescript\n * import { StateGraph, MessagesAnnotation, END, START } from \"@langchain/langgraph\";\n * import { ToolNode, toolsCondition } from \"@langchain/langgraph/prebuilt\";\n *\n * const graph = new StateGraph(MessagesAnnotation)\n * .addNode(\"agent\", agentNode)\n * .addNode(\"tools\", new ToolNode([searchTool, calculatorTool]))\n * .addEdge(START, \"agent\")\n * .addConditionalEdges(\"agent\", toolsCondition, [\"tools\", END])\n * .addEdge(\"tools\", \"agent\")\n * .compile();\n * ```\n *\n * @remarks\n * The function checks the last message in the state for the presence of `tool_calls`.\n * If the message is an `AIMessage` with one or more tool calls, it returns `\"tools\"`,\n * indicating that the graph should route to a tools node (typically a `ToolNode`) to\n * execute those tool calls. Otherwise, it returns `END` to terminate the graph execution.\n *\n * This is a common pattern in agentic workflows where an AI model decides whether to\n * use tools or provide a final response.\n */\nexport declare function toolsCondition(state: BaseMessage[] | typeof MessagesAnnotation.State): \"tools\" | typeof END;\n"],"mappings":";;;;;;;;;KAOYW,eAAAA;;EAAAA,IAAAA,CAAAA,EAAAA,MAAAA,EAAAA;EA0HSC,gBAAQ,CAAA,EAAA,OAAA;CAAA;;;;;;;;;;;;;;;;;;;;AA6C7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA7CqBA,0BAA0BL,iBAAiBM,GAAGA;UACvDR,0BAA0BD,cAAcD;;;sBAG5BE,0BAA0BD,cAAcD,+BAA+BQ;0BACnEL,kBAAkBJ,iBAAiBY,QAAQb,cAAcS;wCAC3CR,iBAAiBY,QAAQD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuC3CE,cAAAA,QAAsBf,uBAAuBQ,kBAAAA,CAAmBQ,yBAAyBP"}
@@ -0,0 +1,246 @@
1
+ import { isGraphInterrupt } from "../errors.js";
2
+ import { Command, END, _isSend, isCommand } from "../constants.js";
3
+ import { RunnableCallable } from "../utils.js";
4
+ import { ToolMessage, isAIMessage, isBaseMessage } from "@langchain/core/messages";
5
+
6
+ //#region src/prebuilt/tool_node.ts
7
+ const isBaseMessageArray = (input) => Array.isArray(input) && input.every(isBaseMessage);
8
+ const isMessagesState = (input) => typeof input === "object" && input != null && "messages" in input && isBaseMessageArray(input.messages);
9
+ const isSendInput = (input) => typeof input === "object" && input != null && "lg_tool_call" in input;
10
+ /**
11
+ * A node that runs the tools requested in the last AIMessage. It can be used
12
+ * either in StateGraph with a "messages" key or in MessageGraph. If multiple
13
+ * tool calls are requested, they will be run in parallel. The output will be
14
+ * a list of ToolMessages, one for each tool call.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { ToolNode } from "@langchain/langgraph/prebuilt";
19
+ * import { tool } from "@langchain/core/tools";
20
+ * import { z } from "zod";
21
+ * import { AIMessage } from "@langchain/core/messages";
22
+ *
23
+ * const getWeather = tool((input) => {
24
+ * if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
25
+ * return "It's 60 degrees and foggy.";
26
+ * } else {
27
+ * return "It's 90 degrees and sunny.";
28
+ * }
29
+ * }, {
30
+ * name: "get_weather",
31
+ * description: "Call to get the current weather.",
32
+ * schema: z.object({
33
+ * location: z.string().describe("Location to get the weather for."),
34
+ * }),
35
+ * });
36
+ *
37
+ * const tools = [getWeather];
38
+ * const toolNode = new ToolNode(tools);
39
+ *
40
+ * const messageWithSingleToolCall = new AIMessage({
41
+ * content: "",
42
+ * tool_calls: [
43
+ * {
44
+ * name: "get_weather",
45
+ * args: { location: "sf" },
46
+ * id: "tool_call_id",
47
+ * type: "tool_call",
48
+ * }
49
+ * ]
50
+ * })
51
+ *
52
+ * await toolNode.invoke({ messages: [messageWithSingleToolCall] });
53
+ * // Returns tool invocation responses as:
54
+ * // { messages: ToolMessage[] }
55
+ * ```
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * import {
60
+ * StateGraph,
61
+ * MessagesAnnotation,
62
+ * } from "@langchain/langgraph";
63
+ * import { ToolNode } from "@langchain/langgraph/prebuilt";
64
+ * import { tool } from "@langchain/core/tools";
65
+ * import { z } from "zod";
66
+ * import { ChatAnthropic } from "@langchain/anthropic";
67
+ *
68
+ * const getWeather = tool((input) => {
69
+ * if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
70
+ * return "It's 60 degrees and foggy.";
71
+ * } else {
72
+ * return "It's 90 degrees and sunny.";
73
+ * }
74
+ * }, {
75
+ * name: "get_weather",
76
+ * description: "Call to get the current weather.",
77
+ * schema: z.object({
78
+ * location: z.string().describe("Location to get the weather for."),
79
+ * }),
80
+ * });
81
+ *
82
+ * const tools = [getWeather];
83
+ * const modelWithTools = new ChatAnthropic({
84
+ * model: "claude-3-haiku-20240307",
85
+ * temperature: 0
86
+ * }).bindTools(tools);
87
+ *
88
+ * const toolNodeForGraph = new ToolNode(tools)
89
+ *
90
+ * const shouldContinue = (state: typeof MessagesAnnotation.State) => {
91
+ * const { messages } = state;
92
+ * const lastMessage = messages[messages.length - 1];
93
+ * if ("tool_calls" in lastMessage && Array.isArray(lastMessage.tool_calls) && lastMessage.tool_calls?.length) {
94
+ * return "tools";
95
+ * }
96
+ * return "__end__";
97
+ * }
98
+ *
99
+ * const callModel = async (state: typeof MessagesAnnotation.State) => {
100
+ * const { messages } = state;
101
+ * const response = await modelWithTools.invoke(messages);
102
+ * return { messages: response };
103
+ * }
104
+ *
105
+ * const graph = new StateGraph(MessagesAnnotation)
106
+ * .addNode("agent", callModel)
107
+ * .addNode("tools", toolNodeForGraph)
108
+ * .addEdge("__start__", "agent")
109
+ * .addConditionalEdges("agent", shouldContinue)
110
+ * .addEdge("tools", "agent")
111
+ * .compile();
112
+ *
113
+ * const inputs = {
114
+ * messages: [{ role: "user", content: "what is the weather in SF?" }],
115
+ * };
116
+ *
117
+ * const stream = await graph.stream(inputs, {
118
+ * streamMode: "values",
119
+ * });
120
+ *
121
+ * for await (const { messages } of stream) {
122
+ * console.log(messages);
123
+ * }
124
+ * // Returns the messages in the state at each step of execution
125
+ * ```
126
+ */
127
+ var ToolNode = class extends RunnableCallable {
128
+ tools;
129
+ handleToolErrors = true;
130
+ trace = false;
131
+ constructor(tools, options) {
132
+ const { name, tags, handleToolErrors } = options ?? {};
133
+ super({
134
+ name,
135
+ tags,
136
+ func: (input, config) => this.run(input, config)
137
+ });
138
+ this.tools = tools;
139
+ this.handleToolErrors = handleToolErrors ?? this.handleToolErrors;
140
+ }
141
+ async runTool(call, config) {
142
+ const tool = this.tools.find((tool$1) => tool$1.name === call.name);
143
+ try {
144
+ if (tool === void 0) throw new Error(`Tool "${call.name}" not found.`);
145
+ const output = await tool.invoke({
146
+ ...call,
147
+ type: "tool_call"
148
+ }, config);
149
+ if (isBaseMessage(output) && output.getType() === "tool" || isCommand(output)) return output;
150
+ return new ToolMessage({
151
+ status: "success",
152
+ name: tool.name,
153
+ content: typeof output === "string" ? output : JSON.stringify(output),
154
+ tool_call_id: call.id
155
+ });
156
+ } catch (e) {
157
+ if (!this.handleToolErrors) throw e;
158
+ if (isGraphInterrupt(e)) throw e;
159
+ return new ToolMessage({
160
+ status: "error",
161
+ content: `Error: ${e.message}\n Please fix your mistakes.`,
162
+ name: call.name,
163
+ tool_call_id: call.id ?? ""
164
+ });
165
+ }
166
+ }
167
+ async run(input, config) {
168
+ let outputs;
169
+ if (isSendInput(input)) outputs = [await this.runTool(input.lg_tool_call, config)];
170
+ else {
171
+ let messages;
172
+ if (isBaseMessageArray(input)) messages = input;
173
+ else if (isMessagesState(input)) messages = input.messages;
174
+ else throw new Error("ToolNode only accepts BaseMessage[] or { messages: BaseMessage[] } as input.");
175
+ const toolMessageIds = new Set(messages.filter((msg) => msg.getType() === "tool").map((msg) => msg.tool_call_id));
176
+ let aiMessage;
177
+ for (let i = messages.length - 1; i >= 0; i -= 1) {
178
+ const message = messages[i];
179
+ if (isAIMessage(message)) {
180
+ aiMessage = message;
181
+ break;
182
+ }
183
+ }
184
+ if (aiMessage == null || !isAIMessage(aiMessage)) throw new Error("ToolNode only accepts AIMessages as input.");
185
+ outputs = await Promise.all(aiMessage.tool_calls?.filter((call) => call.id == null || !toolMessageIds.has(call.id)).map((call) => this.runTool(call, config)) ?? []);
186
+ }
187
+ if (!outputs.some(isCommand)) return Array.isArray(input) ? outputs : { messages: outputs };
188
+ const combinedOutputs = [];
189
+ let parentCommand = null;
190
+ for (const output of outputs) if (isCommand(output)) if (output.graph === Command.PARENT && Array.isArray(output.goto) && output.goto.every((send) => _isSend(send))) if (parentCommand) parentCommand.goto.push(...output.goto);
191
+ else parentCommand = new Command({
192
+ graph: Command.PARENT,
193
+ goto: output.goto
194
+ });
195
+ else combinedOutputs.push(output);
196
+ else combinedOutputs.push(Array.isArray(input) ? [output] : { messages: [output] });
197
+ if (parentCommand) combinedOutputs.push(parentCommand);
198
+ return combinedOutputs;
199
+ }
200
+ };
201
+ /**
202
+ * A conditional edge function that determines whether to route to a tools node or end the graph.
203
+ *
204
+ * This function is designed to be used as a conditional edge in a LangGraph state graph to implement
205
+ * the common pattern of checking if an AI message contains tool calls that need to be executed.
206
+ *
207
+ * @param state - The current state of the graph, which can be either:
208
+ * - An array of `BaseMessage` objects, where the last message is checked for tool calls
209
+ * - A state object conforming to `MessagesAnnotation.State`, which contains a `messages` array
210
+ *
211
+ * @returns A string indicating the next node to route to:
212
+ * - `"tools"` - If the last message contains tool calls that need to be executed
213
+ * - `END` - If there are no tool calls, indicating the graph should terminate
214
+ *
215
+ * @example
216
+ * ```typescript
217
+ * import { StateGraph, MessagesAnnotation, END, START } from "@langchain/langgraph";
218
+ * import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt";
219
+ *
220
+ * const graph = new StateGraph(MessagesAnnotation)
221
+ * .addNode("agent", agentNode)
222
+ * .addNode("tools", new ToolNode([searchTool, calculatorTool]))
223
+ * .addEdge(START, "agent")
224
+ * .addConditionalEdges("agent", toolsCondition, ["tools", END])
225
+ * .addEdge("tools", "agent")
226
+ * .compile();
227
+ * ```
228
+ *
229
+ * @remarks
230
+ * The function checks the last message in the state for the presence of `tool_calls`.
231
+ * If the message is an `AIMessage` with one or more tool calls, it returns `"tools"`,
232
+ * indicating that the graph should route to a tools node (typically a `ToolNode`) to
233
+ * execute those tool calls. Otherwise, it returns `END` to terminate the graph execution.
234
+ *
235
+ * This is a common pattern in agentic workflows where an AI model decides whether to
236
+ * use tools or provide a final response.
237
+ */
238
+ function toolsCondition(state) {
239
+ const message = Array.isArray(state) ? state[state.length - 1] : state.messages[state.messages.length - 1];
240
+ if (message !== void 0 && "tool_calls" in message && (message.tool_calls?.length ?? 0) > 0) return "tools";
241
+ else return END;
242
+ }
243
+
244
+ //#endregion
245
+ export { ToolNode, toolsCondition };
246
+ //# sourceMappingURL=tool_node.js.map