@langchain/langgraph 1.0.7 → 1.0.12

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 (383) hide show
  1. package/package.json +7 -7
  2. package/CHANGELOG.md +0 -286
  3. package/dist/_virtual/rolldown_runtime.cjs +0 -25
  4. package/dist/channels/any_value.cjs +0 -46
  5. package/dist/channels/any_value.cjs.map +0 -1
  6. package/dist/channels/any_value.d.cts +0 -24
  7. package/dist/channels/any_value.d.cts.map +0 -1
  8. package/dist/channels/any_value.d.ts +0 -24
  9. package/dist/channels/any_value.d.ts.map +0 -1
  10. package/dist/channels/any_value.js +0 -46
  11. package/dist/channels/any_value.js.map +0 -1
  12. package/dist/channels/base.cjs +0 -102
  13. package/dist/channels/base.cjs.map +0 -1
  14. package/dist/channels/base.d.cts +0 -79
  15. package/dist/channels/base.d.cts.map +0 -1
  16. package/dist/channels/base.d.ts +0 -79
  17. package/dist/channels/base.d.ts.map +0 -1
  18. package/dist/channels/base.js +0 -97
  19. package/dist/channels/base.js.map +0 -1
  20. package/dist/channels/binop.cjs +0 -49
  21. package/dist/channels/binop.cjs.map +0 -1
  22. package/dist/channels/binop.d.cts +0 -22
  23. package/dist/channels/binop.d.cts.map +0 -1
  24. package/dist/channels/binop.d.ts +0 -22
  25. package/dist/channels/binop.d.ts.map +0 -1
  26. package/dist/channels/binop.js +0 -49
  27. package/dist/channels/binop.js.map +0 -1
  28. package/dist/channels/dynamic_barrier_value.cjs +0 -76
  29. package/dist/channels/dynamic_barrier_value.cjs.map +0 -1
  30. package/dist/channels/dynamic_barrier_value.d.cts +0 -42
  31. package/dist/channels/dynamic_barrier_value.d.cts.map +0 -1
  32. package/dist/channels/dynamic_barrier_value.d.ts +0 -42
  33. package/dist/channels/dynamic_barrier_value.d.ts.map +0 -1
  34. package/dist/channels/dynamic_barrier_value.js +0 -76
  35. package/dist/channels/dynamic_barrier_value.js.map +0 -1
  36. package/dist/channels/ephemeral_value.cjs +0 -46
  37. package/dist/channels/ephemeral_value.cjs.map +0 -1
  38. package/dist/channels/ephemeral_value.d.cts +0 -22
  39. package/dist/channels/ephemeral_value.d.cts.map +0 -1
  40. package/dist/channels/ephemeral_value.d.ts +0 -22
  41. package/dist/channels/ephemeral_value.d.ts.map +0 -1
  42. package/dist/channels/ephemeral_value.js +0 -46
  43. package/dist/channels/ephemeral_value.js.map +0 -1
  44. package/dist/channels/index.cjs +0 -21
  45. package/dist/channels/index.d.cts +0 -9
  46. package/dist/channels/index.d.ts +0 -9
  47. package/dist/channels/index.js +0 -10
  48. package/dist/channels/last_value.cjs +0 -98
  49. package/dist/channels/last_value.cjs.map +0 -1
  50. package/dist/channels/last_value.d.cts +0 -44
  51. package/dist/channels/last_value.d.cts.map +0 -1
  52. package/dist/channels/last_value.d.ts +0 -44
  53. package/dist/channels/last_value.d.ts.map +0 -1
  54. package/dist/channels/last_value.js +0 -97
  55. package/dist/channels/last_value.js.map +0 -1
  56. package/dist/channels/named_barrier_value.cjs +0 -118
  57. package/dist/channels/named_barrier_value.cjs.map +0 -1
  58. package/dist/channels/named_barrier_value.d.cts +0 -46
  59. package/dist/channels/named_barrier_value.d.cts.map +0 -1
  60. package/dist/channels/named_barrier_value.d.ts +0 -46
  61. package/dist/channels/named_barrier_value.d.ts.map +0 -1
  62. package/dist/channels/named_barrier_value.js +0 -116
  63. package/dist/channels/named_barrier_value.js.map +0 -1
  64. package/dist/channels/topic.cjs +0 -65
  65. package/dist/channels/topic.cjs.map +0 -1
  66. package/dist/channels/topic.d.cts +0 -32
  67. package/dist/channels/topic.d.cts.map +0 -1
  68. package/dist/channels/topic.d.ts +0 -32
  69. package/dist/channels/topic.d.ts.map +0 -1
  70. package/dist/channels/topic.js +0 -65
  71. package/dist/channels/topic.js.map +0 -1
  72. package/dist/constants.cjs +0 -406
  73. package/dist/constants.cjs.map +0 -1
  74. package/dist/constants.d.cts +0 -285
  75. package/dist/constants.d.cts.map +0 -1
  76. package/dist/constants.d.ts +0 -285
  77. package/dist/constants.d.ts.map +0 -1
  78. package/dist/constants.js +0 -362
  79. package/dist/constants.js.map +0 -1
  80. package/dist/errors.cjs +0 -163
  81. package/dist/errors.cjs.map +0 -1
  82. package/dist/errors.d.cts +0 -81
  83. package/dist/errors.d.cts.map +0 -1
  84. package/dist/errors.d.ts +0 -81
  85. package/dist/errors.d.ts.map +0 -1
  86. package/dist/errors.js +0 -146
  87. package/dist/errors.js.map +0 -1
  88. package/dist/func/index.cjs +0 -301
  89. package/dist/func/index.cjs.map +0 -1
  90. package/dist/func/index.d.cts +0 -293
  91. package/dist/func/index.d.cts.map +0 -1
  92. package/dist/func/index.d.ts +0 -293
  93. package/dist/func/index.d.ts.map +0 -1
  94. package/dist/func/index.js +0 -298
  95. package/dist/func/index.js.map +0 -1
  96. package/dist/func/types.d.cts +0 -64
  97. package/dist/func/types.d.cts.map +0 -1
  98. package/dist/func/types.d.ts +0 -64
  99. package/dist/func/types.d.ts.map +0 -1
  100. package/dist/graph/annotation.cjs +0 -101
  101. package/dist/graph/annotation.cjs.map +0 -1
  102. package/dist/graph/annotation.d.cts +0 -116
  103. package/dist/graph/annotation.d.cts.map +0 -1
  104. package/dist/graph/annotation.d.ts +0 -116
  105. package/dist/graph/annotation.d.ts.map +0 -1
  106. package/dist/graph/annotation.js +0 -99
  107. package/dist/graph/annotation.js.map +0 -1
  108. package/dist/graph/graph.cjs +0 -423
  109. package/dist/graph/graph.cjs.map +0 -1
  110. package/dist/graph/graph.d.cts +0 -132
  111. package/dist/graph/graph.d.cts.map +0 -1
  112. package/dist/graph/graph.d.ts +0 -132
  113. package/dist/graph/graph.d.ts.map +0 -1
  114. package/dist/graph/graph.js +0 -420
  115. package/dist/graph/graph.js.map +0 -1
  116. package/dist/graph/index.cjs +0 -5
  117. package/dist/graph/index.js +0 -7
  118. package/dist/graph/message.cjs +0 -99
  119. package/dist/graph/message.cjs.map +0 -1
  120. package/dist/graph/message.d.cts +0 -40
  121. package/dist/graph/message.d.cts.map +0 -1
  122. package/dist/graph/message.d.ts +0 -40
  123. package/dist/graph/message.d.ts.map +0 -1
  124. package/dist/graph/message.js +0 -95
  125. package/dist/graph/message.js.map +0 -1
  126. package/dist/graph/messages_annotation.cjs +0 -110
  127. package/dist/graph/messages_annotation.cjs.map +0 -1
  128. package/dist/graph/messages_annotation.d.cts +0 -112
  129. package/dist/graph/messages_annotation.d.cts.map +0 -1
  130. package/dist/graph/messages_annotation.d.ts +0 -112
  131. package/dist/graph/messages_annotation.d.ts.map +0 -1
  132. package/dist/graph/messages_annotation.js +0 -107
  133. package/dist/graph/messages_annotation.js.map +0 -1
  134. package/dist/graph/state.cjs +0 -501
  135. package/dist/graph/state.cjs.map +0 -1
  136. package/dist/graph/state.d.cts +0 -236
  137. package/dist/graph/state.d.cts.map +0 -1
  138. package/dist/graph/state.d.ts +0 -236
  139. package/dist/graph/state.d.ts.map +0 -1
  140. package/dist/graph/state.js +0 -499
  141. package/dist/graph/state.js.map +0 -1
  142. package/dist/graph/zod/index.cjs +0 -10
  143. package/dist/graph/zod/index.d.cts +0 -3
  144. package/dist/graph/zod/index.d.ts +0 -3
  145. package/dist/graph/zod/index.js +0 -5
  146. package/dist/graph/zod/meta.cjs +0 -145
  147. package/dist/graph/zod/meta.cjs.map +0 -1
  148. package/dist/graph/zod/meta.d.cts +0 -116
  149. package/dist/graph/zod/meta.d.cts.map +0 -1
  150. package/dist/graph/zod/meta.d.ts +0 -116
  151. package/dist/graph/zod/meta.d.ts.map +0 -1
  152. package/dist/graph/zod/meta.js +0 -141
  153. package/dist/graph/zod/meta.js.map +0 -1
  154. package/dist/graph/zod/plugin.cjs +0 -41
  155. package/dist/graph/zod/plugin.cjs.map +0 -1
  156. package/dist/graph/zod/plugin.js +0 -41
  157. package/dist/graph/zod/plugin.js.map +0 -1
  158. package/dist/graph/zod/schema.cjs +0 -99
  159. package/dist/graph/zod/schema.cjs.map +0 -1
  160. package/dist/graph/zod/schema.d.cts +0 -38
  161. package/dist/graph/zod/schema.d.cts.map +0 -1
  162. package/dist/graph/zod/schema.d.ts +0 -38
  163. package/dist/graph/zod/schema.d.ts.map +0 -1
  164. package/dist/graph/zod/schema.js +0 -94
  165. package/dist/graph/zod/schema.js.map +0 -1
  166. package/dist/graph/zod/zod-registry.cjs +0 -43
  167. package/dist/graph/zod/zod-registry.cjs.map +0 -1
  168. package/dist/graph/zod/zod-registry.d.cts +0 -51
  169. package/dist/graph/zod/zod-registry.d.cts.map +0 -1
  170. package/dist/graph/zod/zod-registry.d.ts +0 -51
  171. package/dist/graph/zod/zod-registry.d.ts.map +0 -1
  172. package/dist/graph/zod/zod-registry.js +0 -41
  173. package/dist/graph/zod/zod-registry.js.map +0 -1
  174. package/dist/hash.cjs +0 -271
  175. package/dist/hash.cjs.map +0 -1
  176. package/dist/hash.js +0 -269
  177. package/dist/hash.js.map +0 -1
  178. package/dist/index.cjs +0 -112
  179. package/dist/index.cjs.map +0 -1
  180. package/dist/index.d.cts +0 -26
  181. package/dist/index.d.ts +0 -26
  182. package/dist/index.js +0 -22
  183. package/dist/index.js.map +0 -1
  184. package/dist/interrupt.cjs +0 -82
  185. package/dist/interrupt.cjs.map +0 -1
  186. package/dist/interrupt.d.cts +0 -58
  187. package/dist/interrupt.d.cts.map +0 -1
  188. package/dist/interrupt.d.ts +0 -58
  189. package/dist/interrupt.d.ts.map +0 -1
  190. package/dist/interrupt.js +0 -81
  191. package/dist/interrupt.js.map +0 -1
  192. package/dist/prebuilt/agentName.cjs +0 -153
  193. package/dist/prebuilt/agentName.cjs.map +0 -1
  194. package/dist/prebuilt/agentName.d.cts +0 -42
  195. package/dist/prebuilt/agentName.d.cts.map +0 -1
  196. package/dist/prebuilt/agentName.d.ts +0 -42
  197. package/dist/prebuilt/agentName.d.ts.map +0 -1
  198. package/dist/prebuilt/agentName.js +0 -152
  199. package/dist/prebuilt/agentName.js.map +0 -1
  200. package/dist/prebuilt/agent_executor.cjs +0 -44
  201. package/dist/prebuilt/agent_executor.cjs.map +0 -1
  202. package/dist/prebuilt/agent_executor.d.cts +0 -58
  203. package/dist/prebuilt/agent_executor.d.cts.map +0 -1
  204. package/dist/prebuilt/agent_executor.d.ts +0 -58
  205. package/dist/prebuilt/agent_executor.d.ts.map +0 -1
  206. package/dist/prebuilt/agent_executor.js +0 -44
  207. package/dist/prebuilt/agent_executor.js.map +0 -1
  208. package/dist/prebuilt/chat_agent_executor.cjs +0 -68
  209. package/dist/prebuilt/chat_agent_executor.cjs.map +0 -1
  210. package/dist/prebuilt/chat_agent_executor.d.cts +0 -23
  211. package/dist/prebuilt/chat_agent_executor.d.cts.map +0 -1
  212. package/dist/prebuilt/chat_agent_executor.d.ts +0 -23
  213. package/dist/prebuilt/chat_agent_executor.d.ts.map +0 -1
  214. package/dist/prebuilt/chat_agent_executor.js +0 -67
  215. package/dist/prebuilt/chat_agent_executor.js.map +0 -1
  216. package/dist/prebuilt/index.cjs +0 -15
  217. package/dist/prebuilt/index.d.cts +0 -8
  218. package/dist/prebuilt/index.d.ts +0 -8
  219. package/dist/prebuilt/index.js +0 -8
  220. package/dist/prebuilt/interrupt.d.cts +0 -61
  221. package/dist/prebuilt/interrupt.d.cts.map +0 -1
  222. package/dist/prebuilt/interrupt.d.ts +0 -61
  223. package/dist/prebuilt/interrupt.d.ts.map +0 -1
  224. package/dist/prebuilt/react_agent_executor.cjs +0 -344
  225. package/dist/prebuilt/react_agent_executor.cjs.map +0 -1
  226. package/dist/prebuilt/react_agent_executor.d.cts +0 -230
  227. package/dist/prebuilt/react_agent_executor.d.cts.map +0 -1
  228. package/dist/prebuilt/react_agent_executor.d.ts +0 -230
  229. package/dist/prebuilt/react_agent_executor.d.ts.map +0 -1
  230. package/dist/prebuilt/react_agent_executor.js +0 -342
  231. package/dist/prebuilt/react_agent_executor.js.map +0 -1
  232. package/dist/prebuilt/tool_executor.cjs +0 -48
  233. package/dist/prebuilt/tool_executor.cjs.map +0 -1
  234. package/dist/prebuilt/tool_executor.d.cts +0 -42
  235. package/dist/prebuilt/tool_executor.d.cts.map +0 -1
  236. package/dist/prebuilt/tool_executor.d.ts +0 -42
  237. package/dist/prebuilt/tool_executor.d.ts.map +0 -1
  238. package/dist/prebuilt/tool_executor.js +0 -47
  239. package/dist/prebuilt/tool_executor.js.map +0 -1
  240. package/dist/prebuilt/tool_node.cjs +0 -248
  241. package/dist/prebuilt/tool_node.cjs.map +0 -1
  242. package/dist/prebuilt/tool_node.d.cts +0 -182
  243. package/dist/prebuilt/tool_node.d.cts.map +0 -1
  244. package/dist/prebuilt/tool_node.d.ts +0 -182
  245. package/dist/prebuilt/tool_node.d.ts.map +0 -1
  246. package/dist/prebuilt/tool_node.js +0 -246
  247. package/dist/prebuilt/tool_node.js.map +0 -1
  248. package/dist/pregel/algo.cjs +0 -573
  249. package/dist/pregel/algo.cjs.map +0 -1
  250. package/dist/pregel/algo.d.cts +0 -13
  251. package/dist/pregel/algo.d.cts.map +0 -1
  252. package/dist/pregel/algo.d.ts +0 -13
  253. package/dist/pregel/algo.d.ts.map +0 -1
  254. package/dist/pregel/algo.js +0 -567
  255. package/dist/pregel/algo.js.map +0 -1
  256. package/dist/pregel/call.cjs +0 -53
  257. package/dist/pregel/call.cjs.map +0 -1
  258. package/dist/pregel/call.js +0 -50
  259. package/dist/pregel/call.js.map +0 -1
  260. package/dist/pregel/debug.cjs +0 -175
  261. package/dist/pregel/debug.cjs.map +0 -1
  262. package/dist/pregel/debug.js +0 -169
  263. package/dist/pregel/debug.js.map +0 -1
  264. package/dist/pregel/index.cjs +0 -1170
  265. package/dist/pregel/index.cjs.map +0 -1
  266. package/dist/pregel/index.d.cts +0 -546
  267. package/dist/pregel/index.d.cts.map +0 -1
  268. package/dist/pregel/index.d.ts +0 -546
  269. package/dist/pregel/index.d.ts.map +0 -1
  270. package/dist/pregel/index.js +0 -1168
  271. package/dist/pregel/index.js.map +0 -1
  272. package/dist/pregel/io.cjs +0 -140
  273. package/dist/pregel/io.cjs.map +0 -1
  274. package/dist/pregel/io.js +0 -135
  275. package/dist/pregel/io.js.map +0 -1
  276. package/dist/pregel/loop.cjs +0 -548
  277. package/dist/pregel/loop.cjs.map +0 -1
  278. package/dist/pregel/loop.js +0 -547
  279. package/dist/pregel/loop.js.map +0 -1
  280. package/dist/pregel/messages.cjs +0 -108
  281. package/dist/pregel/messages.cjs.map +0 -1
  282. package/dist/pregel/messages.js +0 -107
  283. package/dist/pregel/messages.js.map +0 -1
  284. package/dist/pregel/read.cjs +0 -152
  285. package/dist/pregel/read.cjs.map +0 -1
  286. package/dist/pregel/read.d.cts +0 -51
  287. package/dist/pregel/read.d.cts.map +0 -1
  288. package/dist/pregel/read.d.ts +0 -51
  289. package/dist/pregel/read.d.ts.map +0 -1
  290. package/dist/pregel/read.js +0 -150
  291. package/dist/pregel/read.js.map +0 -1
  292. package/dist/pregel/remote.cjs +0 -349
  293. package/dist/pregel/remote.cjs.map +0 -1
  294. package/dist/pregel/remote.d.cts +0 -123
  295. package/dist/pregel/remote.d.cts.map +0 -1
  296. package/dist/pregel/remote.d.ts +0 -123
  297. package/dist/pregel/remote.d.ts.map +0 -1
  298. package/dist/pregel/remote.js +0 -348
  299. package/dist/pregel/remote.js.map +0 -1
  300. package/dist/pregel/retry.cjs +0 -92
  301. package/dist/pregel/retry.cjs.map +0 -1
  302. package/dist/pregel/retry.js +0 -92
  303. package/dist/pregel/retry.js.map +0 -1
  304. package/dist/pregel/runnable_types.d.cts +0 -49
  305. package/dist/pregel/runnable_types.d.cts.map +0 -1
  306. package/dist/pregel/runnable_types.d.ts +0 -49
  307. package/dist/pregel/runnable_types.d.ts.map +0 -1
  308. package/dist/pregel/runner.cjs +0 -228
  309. package/dist/pregel/runner.cjs.map +0 -1
  310. package/dist/pregel/runner.js +0 -228
  311. package/dist/pregel/runner.js.map +0 -1
  312. package/dist/pregel/stream.cjs +0 -199
  313. package/dist/pregel/stream.cjs.map +0 -1
  314. package/dist/pregel/stream.js +0 -196
  315. package/dist/pregel/stream.js.map +0 -1
  316. package/dist/pregel/types.cjs +0 -27
  317. package/dist/pregel/types.cjs.map +0 -1
  318. package/dist/pregel/types.d.cts +0 -453
  319. package/dist/pregel/types.d.cts.map +0 -1
  320. package/dist/pregel/types.d.ts +0 -453
  321. package/dist/pregel/types.d.ts.map +0 -1
  322. package/dist/pregel/types.js +0 -25
  323. package/dist/pregel/types.js.map +0 -1
  324. package/dist/pregel/utils/config.cjs +0 -122
  325. package/dist/pregel/utils/config.cjs.map +0 -1
  326. package/dist/pregel/utils/config.d.cts +0 -36
  327. package/dist/pregel/utils/config.d.cts.map +0 -1
  328. package/dist/pregel/utils/config.d.ts +0 -36
  329. package/dist/pregel/utils/config.d.ts.map +0 -1
  330. package/dist/pregel/utils/config.js +0 -115
  331. package/dist/pregel/utils/config.js.map +0 -1
  332. package/dist/pregel/utils/index.cjs +0 -103
  333. package/dist/pregel/utils/index.cjs.map +0 -1
  334. package/dist/pregel/utils/index.d.cts +0 -51
  335. package/dist/pregel/utils/index.d.cts.map +0 -1
  336. package/dist/pregel/utils/index.d.ts +0 -51
  337. package/dist/pregel/utils/index.d.ts.map +0 -1
  338. package/dist/pregel/utils/index.js +0 -97
  339. package/dist/pregel/utils/index.js.map +0 -1
  340. package/dist/pregel/utils/subgraph.cjs +0 -19
  341. package/dist/pregel/utils/subgraph.cjs.map +0 -1
  342. package/dist/pregel/utils/subgraph.js +0 -17
  343. package/dist/pregel/utils/subgraph.js.map +0 -1
  344. package/dist/pregel/validate.cjs +0 -45
  345. package/dist/pregel/validate.cjs.map +0 -1
  346. package/dist/pregel/validate.js +0 -44
  347. package/dist/pregel/validate.js.map +0 -1
  348. package/dist/pregel/write.cjs +0 -97
  349. package/dist/pregel/write.cjs.map +0 -1
  350. package/dist/pregel/write.d.cts +0 -35
  351. package/dist/pregel/write.d.cts.map +0 -1
  352. package/dist/pregel/write.d.ts +0 -35
  353. package/dist/pregel/write.d.ts.map +0 -1
  354. package/dist/pregel/write.js +0 -95
  355. package/dist/pregel/write.js.map +0 -1
  356. package/dist/remote.cjs +0 -3
  357. package/dist/remote.d.cts +0 -2
  358. package/dist/remote.d.ts +0 -2
  359. package/dist/remote.js +0 -3
  360. package/dist/setup/async_local_storage.cjs +0 -12
  361. package/dist/setup/async_local_storage.cjs.map +0 -1
  362. package/dist/setup/async_local_storage.js +0 -11
  363. package/dist/setup/async_local_storage.js.map +0 -1
  364. package/dist/utils.cjs +0 -88
  365. package/dist/utils.cjs.map +0 -1
  366. package/dist/utils.d.cts +0 -32
  367. package/dist/utils.d.cts.map +0 -1
  368. package/dist/utils.d.ts +0 -32
  369. package/dist/utils.d.ts.map +0 -1
  370. package/dist/utils.js +0 -81
  371. package/dist/utils.js.map +0 -1
  372. package/dist/web.cjs +0 -96
  373. package/dist/web.d.cts +0 -23
  374. package/dist/web.d.ts +0 -23
  375. package/dist/web.js +0 -15
  376. package/dist/writer.cjs +0 -15
  377. package/dist/writer.cjs.map +0 -1
  378. package/dist/writer.d.cts +0 -7
  379. package/dist/writer.d.cts.map +0 -1
  380. package/dist/writer.d.ts +0 -7
  381. package/dist/writer.d.ts.map +0 -1
  382. package/dist/writer.js +0 -14
  383. package/dist/writer.js.map +0 -1
@@ -1,182 +0,0 @@
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
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
133
- declare class ToolNode<T = any> extends RunnableCallable<T, T> {
134
- tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[];
135
- handleToolErrors: boolean;
136
- trace: boolean;
137
- constructor(tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[], options?: ToolNodeOptions);
138
- protected runTool(call: ToolCall, config: RunnableConfig): Promise<ToolMessage | Command>;
139
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
140
- protected run(input: unknown, config: RunnableConfig): Promise<T>;
141
- }
142
- /**
143
- * A conditional edge function that determines whether to route to a tools node or end the graph.
144
- *
145
- * This function is designed to be used as a conditional edge in a LangGraph state graph to implement
146
- * the common pattern of checking if an AI message contains tool calls that need to be executed.
147
- *
148
- * @param state - The current state of the graph, which can be either:
149
- * - An array of `BaseMessage` objects, where the last message is checked for tool calls
150
- * - A state object conforming to `MessagesAnnotation.State`, which contains a `messages` array
151
- *
152
- * @returns A string indicating the next node to route to:
153
- * - `"tools"` - If the last message contains tool calls that need to be executed
154
- * - `END` - If there are no tool calls, indicating the graph should terminate
155
- *
156
- * @example
157
- * ```typescript
158
- * import { StateGraph, MessagesAnnotation, END, START } from "@langchain/langgraph";
159
- * import { ToolNode, toolsCondition } from "@langchain/langgraph/prebuilt";
160
- *
161
- * const graph = new StateGraph(MessagesAnnotation)
162
- * .addNode("agent", agentNode)
163
- * .addNode("tools", new ToolNode([searchTool, calculatorTool]))
164
- * .addEdge(START, "agent")
165
- * .addConditionalEdges("agent", toolsCondition, ["tools", END])
166
- * .addEdge("tools", "agent")
167
- * .compile();
168
- * ```
169
- *
170
- * @remarks
171
- * The function checks the last message in the state for the presence of `tool_calls`.
172
- * If the message is an `AIMessage` with one or more tool calls, it returns `"tools"`,
173
- * indicating that the graph should route to a tools node (typically a `ToolNode`) to
174
- * execute those tool calls. Otherwise, it returns `END` to terminate the graph execution.
175
- *
176
- * This is a common pattern in agentic workflows where an AI model decides whether to
177
- * use tools or provide a final response.
178
- */
179
- declare function toolsCondition(state: BaseMessage[] | typeof MessagesAnnotation.State): "tools" | typeof END;
180
- //#endregion
181
- export { ToolNode, ToolNodeOptions, toolsCondition };
182
- //# sourceMappingURL=tool_node.d.ts.map
@@ -1 +0,0 @@
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 */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\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 // eslint-disable-next-line @typescript-eslint/no-explicit-any\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;EA2HSC,gBAAQ,CAAA,EAAA,OAAA;CAAA;;;;;;;;;;;;;;;;;;;;AA8C7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cA9CqBA,0BAA0BL,iBAAiBM,GAAGA;UACvDR,0BAA0BD,cAAcD;;;sBAG5BE,0BAA0BD,cAAcD,+BAA+BQ;0BACnEL,kBAAkBJ,iBAAiBY,QAAQb,cAAcS;;wCAE3CR,iBAAiBY,QAAQD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuC3CE,cAAAA,QAAsBf,uBAAuBQ,kBAAAA,CAAmBQ,yBAAyBP"}
@@ -1,246 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"tool_node.js","names":["tool","e: any","outputs: (ToolMessage | Command)[]","messages: BaseMessage[]","toolMessageIds: Set<string>","aiMessage: AIMessage | undefined","combinedOutputs: (\n | { messages: BaseMessage[] }\n | BaseMessage[]\n | Command\n )[]","parentCommand: Command | null"],"sources":["../../src/prebuilt/tool_node.ts"],"sourcesContent":["import {\n BaseMessage,\n ToolMessage,\n AIMessage,\n isBaseMessage,\n isAIMessage,\n} 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 { isGraphInterrupt } from \"../errors.js\";\nimport { END, isCommand, Command, _isSend, Send } from \"../constants.js\";\n\nexport type ToolNodeOptions = {\n name?: string;\n tags?: string[];\n handleToolErrors?: boolean;\n};\n\nconst isBaseMessageArray = (input: unknown): input is BaseMessage[] =>\n Array.isArray(input) && input.every(isBaseMessage);\n\nconst isMessagesState = (\n input: unknown\n): input is { messages: BaseMessage[] } =>\n typeof input === \"object\" &&\n input != null &&\n \"messages\" in input &&\n isBaseMessageArray(input.messages);\n\nconst isSendInput = (input: unknown): input is { lg_tool_call: ToolCall } =>\n typeof input === \"object\" && input != null && \"lg_tool_call\" in input;\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 */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport class ToolNode<T = any> extends RunnableCallable<T, T> {\n tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[];\n\n handleToolErrors = true;\n\n trace = false;\n\n constructor(\n tools: (StructuredToolInterface | DynamicTool | RunnableToolLike)[],\n options?: ToolNodeOptions\n ) {\n const { name, tags, handleToolErrors } = options ?? {};\n super({ name, tags, func: (input, config) => this.run(input, config) });\n this.tools = tools;\n this.handleToolErrors = handleToolErrors ?? this.handleToolErrors;\n }\n\n protected async runTool(\n call: ToolCall,\n config: RunnableConfig\n ): Promise<ToolMessage | Command> {\n const tool = this.tools.find((tool) => tool.name === call.name);\n try {\n if (tool === undefined) {\n throw new Error(`Tool \"${call.name}\" not found.`);\n }\n const output = await tool.invoke({ ...call, type: \"tool_call\" }, config);\n\n if (\n (isBaseMessage(output) && output.getType() === \"tool\") ||\n isCommand(output)\n ) {\n return output as ToolMessage | Command;\n }\n\n return new ToolMessage({\n status: \"success\",\n name: tool.name,\n content: typeof output === \"string\" ? output : JSON.stringify(output),\n tool_call_id: call.id!,\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n if (!this.handleToolErrors) throw e;\n\n if (isGraphInterrupt(e)) {\n // `NodeInterrupt` errors are a breakpoint to bring a human into the loop.\n // As such, they are not recoverable by the agent and shouldn't be fed\n // back. Instead, re-throw these errors even when `handleToolErrors = true`.\n throw e;\n }\n\n return new ToolMessage({\n status: \"error\",\n content: `Error: ${e.message}\\n Please fix your mistakes.`,\n name: call.name,\n tool_call_id: call.id ?? \"\",\n });\n }\n }\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n protected async run(input: unknown, config: RunnableConfig): Promise<T> {\n let outputs: (ToolMessage | Command)[];\n\n if (isSendInput(input)) {\n outputs = [await this.runTool(input.lg_tool_call, config)];\n } else {\n let messages: BaseMessage[];\n if (isBaseMessageArray(input)) {\n messages = input;\n } else if (isMessagesState(input)) {\n messages = input.messages;\n } else {\n throw new Error(\n \"ToolNode only accepts BaseMessage[] or { messages: BaseMessage[] } as input.\"\n );\n }\n\n const toolMessageIds: Set<string> = new Set(\n messages\n .filter((msg) => msg.getType() === \"tool\")\n .map((msg) => (msg as ToolMessage).tool_call_id)\n );\n\n let aiMessage: AIMessage | undefined;\n for (let i = messages.length - 1; i >= 0; i -= 1) {\n const message = messages[i];\n if (isAIMessage(message)) {\n aiMessage = message;\n break;\n }\n }\n\n if (aiMessage == null || !isAIMessage(aiMessage)) {\n throw new Error(\"ToolNode only accepts AIMessages as input.\");\n }\n\n outputs = await Promise.all(\n aiMessage.tool_calls\n ?.filter((call) => call.id == null || !toolMessageIds.has(call.id))\n .map((call) => this.runTool(call, config)) ?? []\n );\n }\n\n // Preserve existing behavior for non-command tool outputs for backwards compatibility\n if (!outputs.some(isCommand)) {\n return (Array.isArray(input) ? outputs : { messages: outputs }) as T;\n }\n\n // Handle mixed Command and non-Command outputs\n const combinedOutputs: (\n | { messages: BaseMessage[] }\n | BaseMessage[]\n | Command\n )[] = [];\n let parentCommand: Command | null = null;\n\n for (const output of outputs) {\n if (isCommand(output)) {\n if (\n output.graph === Command.PARENT &&\n Array.isArray(output.goto) &&\n output.goto.every((send) => _isSend(send))\n ) {\n if (parentCommand) {\n (parentCommand.goto as Send[]).push(...(output.goto as Send[]));\n } else {\n parentCommand = new Command({\n graph: Command.PARENT,\n goto: output.goto,\n });\n }\n } else {\n combinedOutputs.push(output);\n }\n } else {\n combinedOutputs.push(\n Array.isArray(input) ? [output] : { messages: [output] }\n );\n }\n }\n\n if (parentCommand) {\n combinedOutputs.push(parentCommand);\n }\n\n return combinedOutputs as T;\n }\n}\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 function toolsCondition(\n state: BaseMessage[] | typeof MessagesAnnotation.State\n): \"tools\" | typeof END {\n const message = Array.isArray(state)\n ? state[state.length - 1]\n : state.messages[state.messages.length - 1];\n\n if (\n message !== undefined &&\n \"tool_calls\" in message &&\n ((message as AIMessage).tool_calls?.length ?? 0) > 0\n ) {\n return \"tools\";\n } else {\n return END;\n }\n}\n"],"mappings":";;;;;;AAqBA,MAAM,sBAAsB,UAC1B,MAAM,QAAQ,UAAU,MAAM,MAAM;AAEtC,MAAM,mBACJ,UAEA,OAAO,UAAU,YACjB,SAAS,QACT,cAAc,SACd,mBAAmB,MAAM;AAE3B,MAAM,eAAe,UACnB,OAAO,UAAU,YAAY,SAAS,QAAQ,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwHlE,IAAa,WAAb,cAAuC,iBAAuB;CAC5D;CAEA,mBAAmB;CAEnB,QAAQ;CAER,YACE,OACA,SACA;EACA,MAAM,EAAE,MAAM,MAAM,qBAAqB,WAAW;AACpD,QAAM;GAAE;GAAM;GAAM,OAAO,OAAO,WAAW,KAAK,IAAI,OAAO;;AAC7D,OAAK,QAAQ;AACb,OAAK,mBAAmB,oBAAoB,KAAK;;CAGnD,MAAgB,QACd,MACA,QACgC;EAChC,MAAM,OAAO,KAAK,MAAM,MAAM,WAASA,OAAK,SAAS,KAAK;AAC1D,MAAI;AACF,OAAI,SAAS,OACX,OAAM,IAAI,MAAM,SAAS,KAAK,KAAK;GAErC,MAAM,SAAS,MAAM,KAAK,OAAO;IAAE,GAAG;IAAM,MAAM;MAAe;AAEjE,OACG,cAAc,WAAW,OAAO,cAAc,UAC/C,UAAU,QAEV,QAAO;AAGT,UAAO,IAAI,YAAY;IACrB,QAAQ;IACR,MAAM,KAAK;IACX,SAAS,OAAO,WAAW,WAAW,SAAS,KAAK,UAAU;IAC9D,cAAc,KAAK;;WAGdC,GAAQ;AACf,OAAI,CAAC,KAAK,iBAAkB,OAAM;AAElC,OAAI,iBAAiB,GAInB,OAAM;AAGR,UAAO,IAAI,YAAY;IACrB,QAAQ;IACR,SAAS,UAAU,EAAE,QAAQ;IAC7B,MAAM,KAAK;IACX,cAAc,KAAK,MAAM;;;;CAM/B,MAAgB,IAAI,OAAgB,QAAoC;EACtE,IAAIC;AAEJ,MAAI,YAAY,OACd,WAAU,CAAC,MAAM,KAAK,QAAQ,MAAM,cAAc;OAC7C;GACL,IAAIC;AACJ,OAAI,mBAAmB,OACrB,YAAW;YACF,gBAAgB,OACzB,YAAW,MAAM;OAEjB,OAAM,IAAI,MACR;GAIJ,MAAMC,iBAA8B,IAAI,IACtC,SACG,QAAQ,QAAQ,IAAI,cAAc,QAClC,KAAK,QAAS,IAAoB;GAGvC,IAAIC;AACJ,QAAK,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,KAAK,GAAG;IAChD,MAAM,UAAU,SAAS;AACzB,QAAI,YAAY,UAAU;AACxB,iBAAY;AACZ;;;AAIJ,OAAI,aAAa,QAAQ,CAAC,YAAY,WACpC,OAAM,IAAI,MAAM;AAGlB,aAAU,MAAM,QAAQ,IACtB,UAAU,YACN,QAAQ,SAAS,KAAK,MAAM,QAAQ,CAAC,eAAe,IAAI,KAAK,KAC9D,KAAK,SAAS,KAAK,QAAQ,MAAM,YAAY;;AAKpD,MAAI,CAAC,QAAQ,KAAK,WAChB,QAAQ,MAAM,QAAQ,SAAS,UAAU,EAAE,UAAU;EAIvD,MAAMC,kBAIA;EACN,IAAIC,gBAAgC;AAEpC,OAAK,MAAM,UAAU,QACnB,KAAI,UAAU,QACZ,KACE,OAAO,UAAU,QAAQ,UACzB,MAAM,QAAQ,OAAO,SACrB,OAAO,KAAK,OAAO,SAAS,QAAQ,OAEpC,KAAI,cACF,CAAC,cAAc,KAAgB,KAAK,GAAI,OAAO;MAE/C,iBAAgB,IAAI,QAAQ;GAC1B,OAAO,QAAQ;GACf,MAAM,OAAO;;MAIjB,iBAAgB,KAAK;MAGvB,iBAAgB,KACd,MAAM,QAAQ,SAAS,CAAC,UAAU,EAAE,UAAU,CAAC;AAKrD,MAAI,cACF,iBAAgB,KAAK;AAGvB,SAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCX,SAAgB,eACd,OACsB;CACtB,MAAM,UAAU,MAAM,QAAQ,SAC1B,MAAM,MAAM,SAAS,KACrB,MAAM,SAAS,MAAM,SAAS,SAAS;AAE3C,KACE,YAAY,UACZ,gBAAgB,YACd,QAAsB,YAAY,UAAU,KAAK,EAEnD,QAAO;KAEP,QAAO"}