@librechat/agents 3.2.31 → 3.2.33

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 (582) hide show
  1. package/dist/cjs/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/cjs/agents/AgentContext.cjs +844 -1046
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  4. package/dist/cjs/common/constants.cjs +13 -13
  5. package/dist/cjs/common/constants.cjs.map +1 -1
  6. package/dist/cjs/common/enum.cjs +233 -240
  7. package/dist/cjs/common/enum.cjs.map +1 -1
  8. package/dist/cjs/common/index.cjs +2 -0
  9. package/dist/cjs/events.cjs +121 -169
  10. package/dist/cjs/events.cjs.map +1 -1
  11. package/dist/cjs/graphs/Graph.cjs +1389 -1807
  12. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  13. package/dist/cjs/graphs/MultiAgentGraph.cjs +713 -945
  14. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  15. package/dist/cjs/graphs/index.cjs +2 -0
  16. package/dist/cjs/hitl/askUserQuestion.cjs +60 -62
  17. package/dist/cjs/hitl/askUserQuestion.cjs.map +1 -1
  18. package/dist/cjs/hitl/index.cjs +1 -0
  19. package/dist/cjs/hooks/HookRegistry.cjs +176 -202
  20. package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
  21. package/dist/cjs/hooks/createToolPolicyHook.cjs +71 -101
  22. package/dist/cjs/hooks/createToolPolicyHook.cjs.map +1 -1
  23. package/dist/cjs/hooks/createWorkspacePolicyHook.cjs +170 -273
  24. package/dist/cjs/hooks/createWorkspacePolicyHook.cjs.map +1 -1
  25. package/dist/cjs/hooks/executeHooks.cjs +227 -282
  26. package/dist/cjs/hooks/executeHooks.cjs.map +1 -1
  27. package/dist/cjs/hooks/index.cjs +6 -0
  28. package/dist/cjs/hooks/matchers.cjs +196 -230
  29. package/dist/cjs/hooks/matchers.cjs.map +1 -1
  30. package/dist/cjs/hooks/types.cjs +24 -24
  31. package/dist/cjs/hooks/types.cjs.map +1 -1
  32. package/dist/cjs/instrumentation.cjs +110 -137
  33. package/dist/cjs/instrumentation.cjs.map +1 -1
  34. package/dist/cjs/langchain/google-common.cjs +0 -3
  35. package/dist/cjs/langchain/index.cjs +80 -43
  36. package/dist/cjs/langchain/language_models/chat_models.cjs +0 -3
  37. package/dist/cjs/langchain/messages/tool.cjs +0 -3
  38. package/dist/cjs/langchain/messages.cjs +35 -18
  39. package/dist/cjs/langchain/openai.cjs +0 -3
  40. package/dist/cjs/langchain/prompts.cjs +5 -8
  41. package/dist/cjs/langchain/runnables.cjs +11 -10
  42. package/dist/cjs/langchain/tools.cjs +14 -11
  43. package/dist/cjs/langchain/utils/env.cjs +5 -8
  44. package/dist/cjs/langfuse.cjs +60 -79
  45. package/dist/cjs/langfuse.cjs.map +1 -1
  46. package/dist/cjs/langfuseToolOutputTracing.cjs +267 -399
  47. package/dist/cjs/langfuseToolOutputTracing.cjs.map +1 -1
  48. package/dist/cjs/llm/anthropic/index.cjs +432 -562
  49. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  50. package/dist/cjs/llm/anthropic/types.cjs +23 -47
  51. package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
  52. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +441 -696
  53. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  54. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +171 -252
  55. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
  56. package/dist/cjs/llm/anthropic/utils/output_parsers.cjs +2 -0
  57. package/dist/cjs/llm/anthropic/utils/tools.cjs +12 -26
  58. package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -1
  59. package/dist/cjs/llm/bedrock/index.cjs +195 -240
  60. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  61. package/dist/cjs/llm/bedrock/toolCache.cjs +84 -106
  62. package/dist/cjs/llm/bedrock/toolCache.cjs.map +1 -1
  63. package/dist/cjs/llm/bedrock/utils/index.cjs +2 -0
  64. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs +357 -620
  65. package/dist/cjs/llm/bedrock/utils/message_inputs.cjs.map +1 -1
  66. package/dist/cjs/llm/bedrock/utils/message_outputs.cjs +105 -149
  67. package/dist/cjs/llm/bedrock/utils/message_outputs.cjs.map +1 -1
  68. package/dist/cjs/llm/fake.cjs +86 -96
  69. package/dist/cjs/llm/fake.cjs.map +1 -1
  70. package/dist/cjs/llm/google/index.cjs +183 -237
  71. package/dist/cjs/llm/google/index.cjs.map +1 -1
  72. package/dist/cjs/llm/google/utils/common.cjs +398 -674
  73. package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
  74. package/dist/cjs/llm/google/utils/zod_to_genai_parameters.cjs +2 -0
  75. package/dist/cjs/llm/init.cjs +44 -53
  76. package/dist/cjs/llm/init.cjs.map +1 -1
  77. package/dist/cjs/llm/invoke.cjs +142 -182
  78. package/dist/cjs/llm/invoke.cjs.map +1 -1
  79. package/dist/cjs/llm/openai/index.cjs +991 -1276
  80. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  81. package/dist/cjs/llm/openai/utils/index.cjs +189 -316
  82. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  83. package/dist/cjs/llm/openrouter/index.cjs +102 -153
  84. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  85. package/dist/cjs/llm/openrouter/toolCache.cjs +35 -44
  86. package/dist/cjs/llm/openrouter/toolCache.cjs.map +1 -1
  87. package/dist/cjs/llm/providers.cjs +29 -37
  88. package/dist/cjs/llm/providers.cjs.map +1 -1
  89. package/dist/cjs/llm/request.cjs +20 -33
  90. package/dist/cjs/llm/request.cjs.map +1 -1
  91. package/dist/cjs/llm/vertexai/index.cjs +427 -453
  92. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  93. package/dist/cjs/main.cjs +547 -528
  94. package/dist/cjs/messages/anthropicToolCache.cjs +68 -119
  95. package/dist/cjs/messages/anthropicToolCache.cjs.map +1 -1
  96. package/dist/cjs/messages/cache.cjs +308 -401
  97. package/dist/cjs/messages/cache.cjs.map +1 -1
  98. package/dist/cjs/messages/content.cjs +36 -49
  99. package/dist/cjs/messages/content.cjs.map +1 -1
  100. package/dist/cjs/messages/contextPruning.cjs +112 -145
  101. package/dist/cjs/messages/contextPruning.cjs.map +1 -1
  102. package/dist/cjs/messages/contextPruningSettings.cjs +36 -46
  103. package/dist/cjs/messages/contextPruningSettings.cjs.map +1 -1
  104. package/dist/cjs/messages/core.cjs +256 -397
  105. package/dist/cjs/messages/core.cjs.map +1 -1
  106. package/dist/cjs/messages/format.cjs +904 -1382
  107. package/dist/cjs/messages/format.cjs.map +1 -1
  108. package/dist/cjs/messages/ids.cjs +16 -20
  109. package/dist/cjs/messages/ids.cjs.map +1 -1
  110. package/dist/cjs/messages/index.cjs +12 -0
  111. package/dist/cjs/messages/langchain.cjs +18 -18
  112. package/dist/cjs/messages/langchain.cjs.map +1 -1
  113. package/dist/cjs/messages/prune.cjs +1054 -1517
  114. package/dist/cjs/messages/prune.cjs.map +1 -1
  115. package/dist/cjs/messages/recency.cjs +77 -95
  116. package/dist/cjs/messages/recency.cjs.map +1 -1
  117. package/dist/cjs/messages/reducer.cjs +63 -78
  118. package/dist/cjs/messages/reducer.cjs.map +1 -1
  119. package/dist/cjs/messages/tools.cjs +51 -79
  120. package/dist/cjs/messages/tools.cjs.map +1 -1
  121. package/dist/cjs/openai/index.cjs +171 -217
  122. package/dist/cjs/openai/index.cjs.map +1 -1
  123. package/dist/cjs/responses/index.cjs +302 -391
  124. package/dist/cjs/responses/index.cjs.map +1 -1
  125. package/dist/cjs/run.cjs +903 -1113
  126. package/dist/cjs/run.cjs.map +1 -1
  127. package/dist/cjs/session/AgentSession.cjs +805 -986
  128. package/dist/cjs/session/AgentSession.cjs.map +1 -1
  129. package/dist/cjs/session/JsonlSessionStore.cjs +327 -410
  130. package/dist/cjs/session/JsonlSessionStore.cjs.map +1 -1
  131. package/dist/cjs/session/handlers.cjs +192 -208
  132. package/dist/cjs/session/handlers.cjs.map +1 -1
  133. package/dist/cjs/session/ids.cjs +9 -10
  134. package/dist/cjs/session/ids.cjs.map +1 -1
  135. package/dist/cjs/session/index.cjs +4 -0
  136. package/dist/cjs/session/messageSerialization.cjs +94 -156
  137. package/dist/cjs/session/messageSerialization.cjs.map +1 -1
  138. package/dist/cjs/splitStream.cjs +147 -206
  139. package/dist/cjs/splitStream.cjs.map +1 -1
  140. package/dist/cjs/stream.cjs +856 -1344
  141. package/dist/cjs/stream.cjs.map +1 -1
  142. package/dist/cjs/summarization/index.cjs +57 -101
  143. package/dist/cjs/summarization/index.cjs.map +1 -1
  144. package/dist/cjs/summarization/node.cjs +643 -796
  145. package/dist/cjs/summarization/node.cjs.map +1 -1
  146. package/dist/cjs/tools/BashExecutor.cjs +110 -136
  147. package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
  148. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +165 -245
  149. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -1
  150. package/dist/cjs/tools/Calculator.cjs +36 -57
  151. package/dist/cjs/tools/Calculator.cjs.map +1 -1
  152. package/dist/cjs/tools/CodeExecutor.cjs +126 -168
  153. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  154. package/dist/cjs/tools/CodeSessionFileSummary.cjs +36 -46
  155. package/dist/cjs/tools/CodeSessionFileSummary.cjs.map +1 -1
  156. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +459 -649
  157. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  158. package/dist/cjs/tools/ReadFile.cjs +17 -20
  159. package/dist/cjs/tools/ReadFile.cjs.map +1 -1
  160. package/dist/cjs/tools/SkillTool.cjs +26 -27
  161. package/dist/cjs/tools/SkillTool.cjs.map +1 -1
  162. package/dist/cjs/tools/SubagentTool.cjs +59 -61
  163. package/dist/cjs/tools/SubagentTool.cjs.map +1 -1
  164. package/dist/cjs/tools/ToolNode.cjs +2109 -2686
  165. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  166. package/dist/cjs/tools/ToolSearch.cjs +663 -825
  167. package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
  168. package/dist/cjs/tools/cloudflare/CloudflareBridgeRuntime.cjs +248 -340
  169. package/dist/cjs/tools/cloudflare/CloudflareBridgeRuntime.cjs.map +1 -1
  170. package/dist/cjs/tools/cloudflare/CloudflareProgrammaticToolCalling.cjs +170 -197
  171. package/dist/cjs/tools/cloudflare/CloudflareProgrammaticToolCalling.cjs.map +1 -1
  172. package/dist/cjs/tools/cloudflare/CloudflareSandboxExecutionEngine.cjs +425 -520
  173. package/dist/cjs/tools/cloudflare/CloudflareSandboxExecutionEngine.cjs.map +1 -1
  174. package/dist/cjs/tools/cloudflare/CloudflareSandboxTools.cjs +91 -124
  175. package/dist/cjs/tools/cloudflare/CloudflareSandboxTools.cjs.map +1 -1
  176. package/dist/cjs/tools/cloudflare/index.cjs +4 -0
  177. package/dist/cjs/tools/eagerEventExecution.cjs +75 -99
  178. package/dist/cjs/tools/eagerEventExecution.cjs.map +1 -1
  179. package/dist/cjs/tools/handlers.cjs +200 -262
  180. package/dist/cjs/tools/handlers.cjs.map +1 -1
  181. package/dist/cjs/tools/local/CompileCheckTool.cjs +150 -212
  182. package/dist/cjs/tools/local/CompileCheckTool.cjs.map +1 -1
  183. package/dist/cjs/tools/local/FileCheckpointer.cjs +77 -85
  184. package/dist/cjs/tools/local/FileCheckpointer.cjs.map +1 -1
  185. package/dist/cjs/tools/local/LocalCodingTools.cjs +763 -1022
  186. package/dist/cjs/tools/local/LocalCodingTools.cjs.map +1 -1
  187. package/dist/cjs/tools/local/LocalExecutionEngine.cjs +666 -941
  188. package/dist/cjs/tools/local/LocalExecutionEngine.cjs.map +1 -1
  189. package/dist/cjs/tools/local/LocalExecutionTools.cjs +49 -92
  190. package/dist/cjs/tools/local/LocalExecutionTools.cjs.map +1 -1
  191. package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +286 -354
  192. package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs.map +1 -1
  193. package/dist/cjs/tools/local/attachments.cjs +108 -165
  194. package/dist/cjs/tools/local/attachments.cjs.map +1 -1
  195. package/dist/cjs/tools/local/bashAst.cjs +99 -113
  196. package/dist/cjs/tools/local/bashAst.cjs.map +1 -1
  197. package/dist/cjs/tools/local/editStrategies.cjs +126 -169
  198. package/dist/cjs/tools/local/editStrategies.cjs.map +1 -1
  199. package/dist/cjs/tools/local/index.cjs +12 -0
  200. package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs +136 -218
  201. package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs.map +1 -1
  202. package/dist/cjs/tools/local/syntaxCheck.cjs +142 -161
  203. package/dist/cjs/tools/local/syntaxCheck.cjs.map +1 -1
  204. package/dist/cjs/tools/local/textEncoding.cjs +25 -23
  205. package/dist/cjs/tools/local/textEncoding.cjs.map +1 -1
  206. package/dist/cjs/tools/local/workspaceFS.cjs +38 -46
  207. package/dist/cjs/tools/local/workspaceFS.cjs.map +1 -1
  208. package/dist/cjs/tools/ptcTimeout.cjs +27 -47
  209. package/dist/cjs/tools/ptcTimeout.cjs.map +1 -1
  210. package/dist/cjs/tools/schema.cjs +24 -23
  211. package/dist/cjs/tools/schema.cjs.map +1 -1
  212. package/dist/cjs/tools/search/anthropic.cjs +24 -33
  213. package/dist/cjs/tools/search/anthropic.cjs.map +1 -1
  214. package/dist/cjs/tools/search/content.cjs +95 -137
  215. package/dist/cjs/tools/search/content.cjs.map +1 -1
  216. package/dist/cjs/tools/search/firecrawl.cjs +141 -172
  217. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
  218. package/dist/cjs/tools/search/format.cjs +128 -196
  219. package/dist/cjs/tools/search/format.cjs.map +1 -1
  220. package/dist/cjs/tools/search/highlights.cjs +165 -232
  221. package/dist/cjs/tools/search/highlights.cjs.map +1 -1
  222. package/dist/cjs/tools/search/index.cjs +2 -0
  223. package/dist/cjs/tools/search/rerankers.cjs +151 -174
  224. package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
  225. package/dist/cjs/tools/search/schema.cjs +40 -39
  226. package/dist/cjs/tools/search/schema.cjs.map +1 -1
  227. package/dist/cjs/tools/search/search.cjs +428 -530
  228. package/dist/cjs/tools/search/search.cjs.map +1 -1
  229. package/dist/cjs/tools/search/serper-scraper.cjs +106 -127
  230. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -1
  231. package/dist/cjs/tools/search/tavily-scraper.cjs +129 -181
  232. package/dist/cjs/tools/search/tavily-scraper.cjs.map +1 -1
  233. package/dist/cjs/tools/search/tavily-search.cjs +295 -359
  234. package/dist/cjs/tools/search/tavily-search.cjs.map +1 -1
  235. package/dist/cjs/tools/search/tool.cjs +260 -299
  236. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  237. package/dist/cjs/tools/search/utils.cjs +74 -117
  238. package/dist/cjs/tools/search/utils.cjs.map +1 -1
  239. package/dist/cjs/tools/skillCatalog.cjs +54 -72
  240. package/dist/cjs/tools/skillCatalog.cjs.map +1 -1
  241. package/dist/cjs/tools/streamedToolCallSeals.cjs +19 -36
  242. package/dist/cjs/tools/streamedToolCallSeals.cjs.map +1 -1
  243. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +612 -771
  244. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
  245. package/dist/cjs/tools/subagent/index.cjs +1 -0
  246. package/dist/cjs/tools/toolOutputReferences.cjs +523 -630
  247. package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
  248. package/dist/cjs/utils/callbacks.cjs +11 -21
  249. package/dist/cjs/utils/callbacks.cjs.map +1 -1
  250. package/dist/cjs/utils/errors.cjs +70 -95
  251. package/dist/cjs/utils/errors.cjs.map +1 -1
  252. package/dist/cjs/utils/events.cjs +32 -42
  253. package/dist/cjs/utils/events.cjs.map +1 -1
  254. package/dist/cjs/utils/graph.cjs +8 -12
  255. package/dist/cjs/utils/graph.cjs.map +1 -1
  256. package/dist/cjs/utils/handlers.cjs +60 -82
  257. package/dist/cjs/utils/handlers.cjs.map +1 -1
  258. package/dist/cjs/utils/index.cjs +9 -0
  259. package/dist/cjs/utils/llm.cjs +19 -27
  260. package/dist/cjs/utils/llm.cjs.map +1 -1
  261. package/dist/cjs/utils/misc.cjs +30 -46
  262. package/dist/cjs/utils/misc.cjs.map +1 -1
  263. package/dist/cjs/utils/run.cjs +50 -66
  264. package/dist/cjs/utils/run.cjs.map +1 -1
  265. package/dist/cjs/utils/schema.cjs +11 -19
  266. package/dist/cjs/utils/schema.cjs.map +1 -1
  267. package/dist/cjs/utils/title.cjs +71 -106
  268. package/dist/cjs/utils/title.cjs.map +1 -1
  269. package/dist/cjs/utils/tokens.cjs +186 -283
  270. package/dist/cjs/utils/tokens.cjs.map +1 -1
  271. package/dist/cjs/utils/truncation.cjs +95 -114
  272. package/dist/cjs/utils/truncation.cjs.map +1 -1
  273. package/dist/esm/agents/AgentContext.mjs +844 -1044
  274. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  275. package/dist/esm/common/constants.mjs +13 -11
  276. package/dist/esm/common/constants.mjs.map +1 -1
  277. package/dist/esm/common/enum.mjs +221 -238
  278. package/dist/esm/common/enum.mjs.map +1 -1
  279. package/dist/esm/common/index.mjs +3 -0
  280. package/dist/esm/events.mjs +121 -167
  281. package/dist/esm/events.mjs.map +1 -1
  282. package/dist/esm/graphs/Graph.mjs +1388 -1804
  283. package/dist/esm/graphs/Graph.mjs.map +1 -1
  284. package/dist/esm/graphs/MultiAgentGraph.mjs +713 -943
  285. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  286. package/dist/esm/graphs/index.mjs +3 -0
  287. package/dist/esm/hitl/askUserQuestion.mjs +60 -60
  288. package/dist/esm/hitl/askUserQuestion.mjs.map +1 -1
  289. package/dist/esm/hitl/index.mjs +2 -0
  290. package/dist/esm/hooks/HookRegistry.mjs +176 -200
  291. package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
  292. package/dist/esm/hooks/createToolPolicyHook.mjs +71 -99
  293. package/dist/esm/hooks/createToolPolicyHook.mjs.map +1 -1
  294. package/dist/esm/hooks/createWorkspacePolicyHook.mjs +170 -271
  295. package/dist/esm/hooks/createWorkspacePolicyHook.mjs.map +1 -1
  296. package/dist/esm/hooks/executeHooks.mjs +227 -280
  297. package/dist/esm/hooks/executeHooks.mjs.map +1 -1
  298. package/dist/esm/hooks/index.mjs +7 -0
  299. package/dist/esm/hooks/matchers.mjs +196 -228
  300. package/dist/esm/hooks/matchers.mjs.map +1 -1
  301. package/dist/esm/hooks/types.mjs +24 -22
  302. package/dist/esm/hooks/types.mjs.map +1 -1
  303. package/dist/esm/instrumentation.mjs +109 -132
  304. package/dist/esm/instrumentation.mjs.map +1 -1
  305. package/dist/esm/langchain/google-common.mjs +1 -2
  306. package/dist/esm/langchain/index.mjs +5 -5
  307. package/dist/esm/langchain/language_models/chat_models.mjs +1 -2
  308. package/dist/esm/langchain/messages/tool.mjs +1 -2
  309. package/dist/esm/langchain/messages.mjs +2 -2
  310. package/dist/esm/langchain/openai.mjs +1 -2
  311. package/dist/esm/langchain/prompts.mjs +2 -2
  312. package/dist/esm/langchain/runnables.mjs +2 -2
  313. package/dist/esm/langchain/tools.mjs +2 -2
  314. package/dist/esm/langchain/utils/env.mjs +2 -2
  315. package/dist/esm/langfuse.mjs +60 -76
  316. package/dist/esm/langfuse.mjs.map +1 -1
  317. package/dist/esm/langfuseToolOutputTracing.mjs +267 -395
  318. package/dist/esm/langfuseToolOutputTracing.mjs.map +1 -1
  319. package/dist/esm/llm/anthropic/index.mjs +432 -559
  320. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  321. package/dist/esm/llm/anthropic/types.mjs +23 -45
  322. package/dist/esm/llm/anthropic/types.mjs.map +1 -1
  323. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +439 -690
  324. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  325. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +171 -249
  326. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
  327. package/dist/esm/llm/anthropic/utils/output_parsers.mjs +3 -0
  328. package/dist/esm/llm/anthropic/utils/tools.mjs +12 -24
  329. package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -1
  330. package/dist/esm/llm/bedrock/index.mjs +195 -238
  331. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  332. package/dist/esm/llm/bedrock/toolCache.mjs +84 -104
  333. package/dist/esm/llm/bedrock/toolCache.mjs.map +1 -1
  334. package/dist/esm/llm/bedrock/utils/index.mjs +3 -0
  335. package/dist/esm/llm/bedrock/utils/message_inputs.mjs +357 -618
  336. package/dist/esm/llm/bedrock/utils/message_inputs.mjs.map +1 -1
  337. package/dist/esm/llm/bedrock/utils/message_outputs.mjs +105 -147
  338. package/dist/esm/llm/bedrock/utils/message_outputs.mjs.map +1 -1
  339. package/dist/esm/llm/fake.mjs +86 -94
  340. package/dist/esm/llm/fake.mjs.map +1 -1
  341. package/dist/esm/llm/google/index.mjs +183 -235
  342. package/dist/esm/llm/google/index.mjs.map +1 -1
  343. package/dist/esm/llm/google/utils/common.mjs +397 -666
  344. package/dist/esm/llm/google/utils/common.mjs.map +1 -1
  345. package/dist/esm/llm/google/utils/zod_to_genai_parameters.mjs +3 -0
  346. package/dist/esm/llm/init.mjs +44 -51
  347. package/dist/esm/llm/init.mjs.map +1 -1
  348. package/dist/esm/llm/invoke.mjs +142 -180
  349. package/dist/esm/llm/invoke.mjs.map +1 -1
  350. package/dist/esm/llm/openai/index.mjs +991 -1271
  351. package/dist/esm/llm/openai/index.mjs.map +1 -1
  352. package/dist/esm/llm/openai/utils/index.mjs +188 -312
  353. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  354. package/dist/esm/llm/openrouter/index.mjs +102 -151
  355. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  356. package/dist/esm/llm/openrouter/toolCache.mjs +35 -42
  357. package/dist/esm/llm/openrouter/toolCache.mjs.map +1 -1
  358. package/dist/esm/llm/providers.mjs +29 -34
  359. package/dist/esm/llm/providers.mjs.map +1 -1
  360. package/dist/esm/llm/request.mjs +20 -31
  361. package/dist/esm/llm/request.mjs.map +1 -1
  362. package/dist/esm/llm/vertexai/index.mjs +427 -449
  363. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  364. package/dist/esm/main.mjs +99 -87
  365. package/dist/esm/messages/anthropicToolCache.mjs +68 -117
  366. package/dist/esm/messages/anthropicToolCache.mjs.map +1 -1
  367. package/dist/esm/messages/cache.mjs +308 -399
  368. package/dist/esm/messages/cache.mjs.map +1 -1
  369. package/dist/esm/messages/content.mjs +36 -47
  370. package/dist/esm/messages/content.mjs.map +1 -1
  371. package/dist/esm/messages/contextPruning.mjs +112 -143
  372. package/dist/esm/messages/contextPruning.mjs.map +1 -1
  373. package/dist/esm/messages/contextPruningSettings.mjs +36 -44
  374. package/dist/esm/messages/contextPruningSettings.mjs.map +1 -1
  375. package/dist/esm/messages/core.mjs +254 -393
  376. package/dist/esm/messages/core.mjs.map +1 -1
  377. package/dist/esm/messages/format.mjs +902 -1378
  378. package/dist/esm/messages/format.mjs.map +1 -1
  379. package/dist/esm/messages/ids.mjs +16 -18
  380. package/dist/esm/messages/ids.mjs.map +1 -1
  381. package/dist/esm/messages/index.mjs +13 -0
  382. package/dist/esm/messages/langchain.mjs +18 -16
  383. package/dist/esm/messages/langchain.mjs.map +1 -1
  384. package/dist/esm/messages/prune.mjs +1053 -1514
  385. package/dist/esm/messages/prune.mjs.map +1 -1
  386. package/dist/esm/messages/recency.mjs +77 -93
  387. package/dist/esm/messages/recency.mjs.map +1 -1
  388. package/dist/esm/messages/reducer.mjs +63 -76
  389. package/dist/esm/messages/reducer.mjs.map +1 -1
  390. package/dist/esm/messages/tools.mjs +49 -75
  391. package/dist/esm/messages/tools.mjs.map +1 -1
  392. package/dist/esm/openai/index.mjs +170 -215
  393. package/dist/esm/openai/index.mjs.map +1 -1
  394. package/dist/esm/responses/index.mjs +301 -389
  395. package/dist/esm/responses/index.mjs.map +1 -1
  396. package/dist/esm/run.mjs +903 -1111
  397. package/dist/esm/run.mjs.map +1 -1
  398. package/dist/esm/session/AgentSession.mjs +806 -985
  399. package/dist/esm/session/AgentSession.mjs.map +1 -1
  400. package/dist/esm/session/JsonlSessionStore.mjs +326 -407
  401. package/dist/esm/session/JsonlSessionStore.mjs.map +1 -1
  402. package/dist/esm/session/handlers.mjs +192 -206
  403. package/dist/esm/session/handlers.mjs.map +1 -1
  404. package/dist/esm/session/ids.mjs +9 -8
  405. package/dist/esm/session/ids.mjs.map +1 -1
  406. package/dist/esm/session/index.mjs +5 -0
  407. package/dist/esm/session/messageSerialization.mjs +94 -154
  408. package/dist/esm/session/messageSerialization.mjs.map +1 -1
  409. package/dist/esm/splitStream.mjs +147 -204
  410. package/dist/esm/splitStream.mjs.map +1 -1
  411. package/dist/esm/stream.mjs +854 -1341
  412. package/dist/esm/stream.mjs.map +1 -1
  413. package/dist/esm/summarization/index.mjs +57 -99
  414. package/dist/esm/summarization/index.mjs.map +1 -1
  415. package/dist/esm/summarization/node.mjs +640 -790
  416. package/dist/esm/summarization/node.mjs.map +1 -1
  417. package/dist/esm/tools/BashExecutor.mjs +103 -129
  418. package/dist/esm/tools/BashExecutor.mjs.map +1 -1
  419. package/dist/esm/tools/BashProgrammaticToolCalling.mjs +162 -239
  420. package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -1
  421. package/dist/esm/tools/Calculator.mjs +34 -36
  422. package/dist/esm/tools/Calculator.mjs.map +1 -1
  423. package/dist/esm/tools/CodeExecutor.mjs +123 -164
  424. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  425. package/dist/esm/tools/CodeSessionFileSummary.mjs +36 -44
  426. package/dist/esm/tools/CodeSessionFileSummary.mjs.map +1 -1
  427. package/dist/esm/tools/ProgrammaticToolCalling.mjs +454 -644
  428. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  429. package/dist/esm/tools/ReadFile.mjs +17 -18
  430. package/dist/esm/tools/ReadFile.mjs.map +1 -1
  431. package/dist/esm/tools/SkillTool.mjs +26 -25
  432. package/dist/esm/tools/SkillTool.mjs.map +1 -1
  433. package/dist/esm/tools/SubagentTool.mjs +59 -59
  434. package/dist/esm/tools/SubagentTool.mjs.map +1 -1
  435. package/dist/esm/tools/ToolNode.mjs +2107 -2684
  436. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  437. package/dist/esm/tools/ToolSearch.mjs +659 -804
  438. package/dist/esm/tools/ToolSearch.mjs.map +1 -1
  439. package/dist/esm/tools/cloudflare/CloudflareBridgeRuntime.mjs +248 -338
  440. package/dist/esm/tools/cloudflare/CloudflareBridgeRuntime.mjs.map +1 -1
  441. package/dist/esm/tools/cloudflare/CloudflareProgrammaticToolCalling.mjs +170 -195
  442. package/dist/esm/tools/cloudflare/CloudflareProgrammaticToolCalling.mjs.map +1 -1
  443. package/dist/esm/tools/cloudflare/CloudflareSandboxExecutionEngine.mjs +424 -517
  444. package/dist/esm/tools/cloudflare/CloudflareSandboxExecutionEngine.mjs.map +1 -1
  445. package/dist/esm/tools/cloudflare/CloudflareSandboxTools.mjs +91 -122
  446. package/dist/esm/tools/cloudflare/CloudflareSandboxTools.mjs.map +1 -1
  447. package/dist/esm/tools/cloudflare/index.mjs +5 -0
  448. package/dist/esm/tools/eagerEventExecution.mjs +75 -96
  449. package/dist/esm/tools/eagerEventExecution.mjs.map +1 -1
  450. package/dist/esm/tools/handlers.mjs +200 -260
  451. package/dist/esm/tools/handlers.mjs.map +1 -1
  452. package/dist/esm/tools/local/CompileCheckTool.mjs +150 -210
  453. package/dist/esm/tools/local/CompileCheckTool.mjs.map +1 -1
  454. package/dist/esm/tools/local/FileCheckpointer.mjs +77 -83
  455. package/dist/esm/tools/local/FileCheckpointer.mjs.map +1 -1
  456. package/dist/esm/tools/local/LocalCodingTools.mjs +760 -1017
  457. package/dist/esm/tools/local/LocalCodingTools.mjs.map +1 -1
  458. package/dist/esm/tools/local/LocalExecutionEngine.mjs +663 -936
  459. package/dist/esm/tools/local/LocalExecutionEngine.mjs.map +1 -1
  460. package/dist/esm/tools/local/LocalExecutionTools.mjs +49 -90
  461. package/dist/esm/tools/local/LocalExecutionTools.mjs.map +1 -1
  462. package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +283 -349
  463. package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs.map +1 -1
  464. package/dist/esm/tools/local/attachments.mjs +108 -163
  465. package/dist/esm/tools/local/attachments.mjs.map +1 -1
  466. package/dist/esm/tools/local/bashAst.mjs +99 -111
  467. package/dist/esm/tools/local/bashAst.mjs.map +1 -1
  468. package/dist/esm/tools/local/editStrategies.mjs +126 -167
  469. package/dist/esm/tools/local/editStrategies.mjs.map +1 -1
  470. package/dist/esm/tools/local/index.mjs +13 -0
  471. package/dist/esm/tools/local/resolveLocalExecutionTools.mjs +136 -216
  472. package/dist/esm/tools/local/resolveLocalExecutionTools.mjs.map +1 -1
  473. package/dist/esm/tools/local/syntaxCheck.mjs +138 -155
  474. package/dist/esm/tools/local/syntaxCheck.mjs.map +1 -1
  475. package/dist/esm/tools/local/textEncoding.mjs +25 -21
  476. package/dist/esm/tools/local/textEncoding.mjs.map +1 -1
  477. package/dist/esm/tools/local/workspaceFS.mjs +38 -44
  478. package/dist/esm/tools/local/workspaceFS.mjs.map +1 -1
  479. package/dist/esm/tools/ptcTimeout.mjs +27 -42
  480. package/dist/esm/tools/ptcTimeout.mjs.map +1 -1
  481. package/dist/esm/tools/schema.mjs +24 -21
  482. package/dist/esm/tools/schema.mjs.map +1 -1
  483. package/dist/esm/tools/search/anthropic.mjs +24 -31
  484. package/dist/esm/tools/search/anthropic.mjs.map +1 -1
  485. package/dist/esm/tools/search/content.mjs +93 -116
  486. package/dist/esm/tools/search/content.mjs.map +1 -1
  487. package/dist/esm/tools/search/firecrawl.mjs +139 -169
  488. package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
  489. package/dist/esm/tools/search/format.mjs +128 -194
  490. package/dist/esm/tools/search/format.mjs.map +1 -1
  491. package/dist/esm/tools/search/highlights.mjs +165 -230
  492. package/dist/esm/tools/search/highlights.mjs.map +1 -1
  493. package/dist/esm/tools/search/index.mjs +3 -0
  494. package/dist/esm/tools/search/rerankers.mjs +149 -168
  495. package/dist/esm/tools/search/rerankers.mjs.map +1 -1
  496. package/dist/esm/tools/search/schema.mjs +39 -37
  497. package/dist/esm/tools/search/schema.mjs.map +1 -1
  498. package/dist/esm/tools/search/search.mjs +426 -528
  499. package/dist/esm/tools/search/search.mjs.map +1 -1
  500. package/dist/esm/tools/search/serper-scraper.mjs +104 -124
  501. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -1
  502. package/dist/esm/tools/search/tavily-scraper.mjs +127 -178
  503. package/dist/esm/tools/search/tavily-scraper.mjs.map +1 -1
  504. package/dist/esm/tools/search/tavily-search.mjs +293 -357
  505. package/dist/esm/tools/search/tavily-search.mjs.map +1 -1
  506. package/dist/esm/tools/search/tool.mjs +259 -297
  507. package/dist/esm/tools/search/tool.mjs.map +1 -1
  508. package/dist/esm/tools/search/utils.mjs +74 -115
  509. package/dist/esm/tools/search/utils.mjs.map +1 -1
  510. package/dist/esm/tools/skillCatalog.mjs +54 -70
  511. package/dist/esm/tools/skillCatalog.mjs.map +1 -1
  512. package/dist/esm/tools/streamedToolCallSeals.mjs +19 -31
  513. package/dist/esm/tools/streamedToolCallSeals.mjs.map +1 -1
  514. package/dist/esm/tools/subagent/SubagentExecutor.mjs +612 -768
  515. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
  516. package/dist/esm/tools/subagent/index.mjs +2 -0
  517. package/dist/esm/tools/toolOutputReferences.mjs +523 -624
  518. package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
  519. package/dist/esm/utils/callbacks.mjs +11 -19
  520. package/dist/esm/utils/callbacks.mjs.map +1 -1
  521. package/dist/esm/utils/errors.mjs +70 -93
  522. package/dist/esm/utils/errors.mjs.map +1 -1
  523. package/dist/esm/utils/events.mjs +32 -40
  524. package/dist/esm/utils/events.mjs.map +1 -1
  525. package/dist/esm/utils/graph.mjs +8 -10
  526. package/dist/esm/utils/graph.mjs.map +1 -1
  527. package/dist/esm/utils/handlers.mjs +60 -80
  528. package/dist/esm/utils/handlers.mjs.map +1 -1
  529. package/dist/esm/utils/index.mjs +10 -0
  530. package/dist/esm/utils/llm.mjs +19 -25
  531. package/dist/esm/utils/llm.mjs.map +1 -1
  532. package/dist/esm/utils/misc.mjs +30 -44
  533. package/dist/esm/utils/misc.mjs.map +1 -1
  534. package/dist/esm/utils/run.mjs +50 -64
  535. package/dist/esm/utils/run.mjs.map +1 -1
  536. package/dist/esm/utils/schema.mjs +11 -17
  537. package/dist/esm/utils/schema.mjs.map +1 -1
  538. package/dist/esm/utils/title.mjs +71 -104
  539. package/dist/esm/utils/title.mjs.map +1 -1
  540. package/dist/esm/utils/tokens.mjs +186 -281
  541. package/dist/esm/utils/tokens.mjs.map +1 -1
  542. package/dist/esm/utils/truncation.mjs +95 -112
  543. package/dist/esm/utils/truncation.mjs.map +1 -1
  544. package/dist/types/messages/format.d.ts +5 -0
  545. package/dist/types/tools/search/tool.d.ts +17 -0
  546. package/dist/types/tools/search/types.d.ts +4 -0
  547. package/package.json +11 -17
  548. package/src/llm/anthropic/llm.spec.ts +36 -0
  549. package/src/llm/anthropic/utils/message_inputs.ts +45 -3
  550. package/src/llm/anthropic/utils/message_outputs.ts +6 -2
  551. package/src/llm/anthropic/utils/streaming-tool-input.test.ts +186 -0
  552. package/src/messages/cache.test.ts +122 -0
  553. package/src/messages/cache.ts +25 -1
  554. package/src/messages/format.ts +9 -0
  555. package/src/messages/formatAgentMessages.skills.test.ts +100 -0
  556. package/src/tools/search/highlights.ts +9 -1
  557. package/src/tools/search/search.ts +41 -3
  558. package/src/tools/search/source-processing.test.ts +373 -0
  559. package/src/tools/search/tool.ts +22 -2
  560. package/src/tools/search/types.ts +4 -0
  561. package/dist/cjs/langchain/google-common.cjs.map +0 -1
  562. package/dist/cjs/langchain/index.cjs.map +0 -1
  563. package/dist/cjs/langchain/language_models/chat_models.cjs.map +0 -1
  564. package/dist/cjs/langchain/messages/tool.cjs.map +0 -1
  565. package/dist/cjs/langchain/messages.cjs.map +0 -1
  566. package/dist/cjs/langchain/openai.cjs.map +0 -1
  567. package/dist/cjs/langchain/prompts.cjs.map +0 -1
  568. package/dist/cjs/langchain/runnables.cjs.map +0 -1
  569. package/dist/cjs/langchain/tools.cjs.map +0 -1
  570. package/dist/cjs/langchain/utils/env.cjs.map +0 -1
  571. package/dist/cjs/main.cjs.map +0 -1
  572. package/dist/esm/langchain/google-common.mjs.map +0 -1
  573. package/dist/esm/langchain/index.mjs.map +0 -1
  574. package/dist/esm/langchain/language_models/chat_models.mjs.map +0 -1
  575. package/dist/esm/langchain/messages/tool.mjs.map +0 -1
  576. package/dist/esm/langchain/messages.mjs.map +0 -1
  577. package/dist/esm/langchain/openai.mjs.map +0 -1
  578. package/dist/esm/langchain/prompts.mjs.map +0 -1
  579. package/dist/esm/langchain/runnables.mjs.map +0 -1
  580. package/dist/esm/langchain/tools.mjs.map +0 -1
  581. package/dist/esm/langchain/utils/env.mjs.map +0 -1
  582. package/dist/esm/main.mjs.map +0 -1
@@ -1,947 +1,717 @@
1
- import { tool } from '@langchain/core/tools';
2
- import { PromptTemplate } from '@langchain/core/prompts';
3
- import { ToolMessage, AIMessage, getBufferString, HumanMessage } from '@langchain/core/messages';
4
- import { getCurrentTaskInput, Command, Annotation, messagesStateReducer, StateGraph, END, START } from '@langchain/langgraph';
5
- import { StandardGraph } from './Graph.mjs';
6
- import { Constants } from '../common/enum.mjs';
7
-
1
+ import "../common/enum.mjs";
2
+ import "../common/index.mjs";
3
+ import { StandardGraph } from "./Graph.mjs";
4
+ import { AIMessage, HumanMessage, ToolMessage, getBufferString } from "@langchain/core/messages";
5
+ import { PromptTemplate } from "@langchain/core/prompts";
6
+ import { Annotation, Command, END, START, StateGraph, getCurrentTaskInput, messagesStateReducer } from "@langchain/langgraph";
7
+ import { tool } from "@langchain/core/tools";
8
+ //#region src/graphs/MultiAgentGraph.ts
8
9
  /** Pattern to extract instructions from transfer ToolMessage content */
9
10
  const HANDOFF_INSTRUCTIONS_PATTERN = /(?:Instructions?|Context):\s*(.+)/is;
10
11
  /**
11
- * MultiAgentGraph extends StandardGraph to support dynamic multi-agent workflows
12
- * with handoffs, fan-in/fan-out, and other composable patterns.
13
- *
14
- * Key behavior:
15
- * - Agents with ONLY handoff edges: Can dynamically route to any handoff destination
16
- * - Agents with ONLY direct edges: Always follow their direct edges
17
- * - Agents with BOTH: Use Command for exclusive routing (handoff OR direct, not both)
18
- * - If handoff occurs: Only the handoff destination executes
19
- * - If no handoff: Direct edges execute (potentially in parallel)
20
- *
21
- * This enables the common pattern where an agent either delegates (handoff)
22
- * OR continues its workflow (direct edges), but not both simultaneously.
23
- */
24
- class MultiAgentGraph extends StandardGraph {
25
- edges;
26
- startingNodes = new Set();
27
- directEdges = [];
28
- handoffEdges = [];
29
- /**
30
- * Map of agentId to parallel group info.
31
- * Contains groupId (incrementing number reflecting execution order) for agents in parallel groups.
32
- * Sequential agents (not in any parallel group) have undefined entry.
33
- *
34
- * Example for: researcher -> [analyst1, analyst2, analyst3] -> summarizer
35
- * - researcher: undefined (sequential, order 0)
36
- * - analyst1, analyst2, analyst3: { groupId: 1 } (parallel group, order 1)
37
- * - summarizer: undefined (sequential, order 2)
38
- */
39
- agentParallelGroups = new Map();
40
- constructor(input) {
41
- super(input);
42
- this.edges = input.edges;
43
- this.validateEdgeAgents();
44
- this.categorizeEdges();
45
- this.analyzeGraph();
46
- this.createHandoffTools();
47
- }
48
- /**
49
- * Fails fast when an edge references an agent that is not in
50
- * `agentContexts`. Without this check, the underlying LangGraph
51
- * `StateGraph.compile()` would throw the opaque
52
- * `Found edge ending at unknown node "<id>"` error after graph
53
- * construction — far from the true root cause.
54
- *
55
- * This catches the common misuse of passing `edges` into a multi-agent
56
- * config without also passing the corresponding sub-agent configs in
57
- * `agents` (e.g. a host that forgot to pre-load handoff targets).
58
- */
59
- validateEdgeAgents() {
60
- const known = new Set(this.agentContexts.keys());
61
- const unknown = new Set();
62
- for (const edge of this.edges) {
63
- const participants = [
64
- ...(Array.isArray(edge.from) ? edge.from : [edge.from]),
65
- ...(Array.isArray(edge.to) ? edge.to : [edge.to]),
66
- ];
67
- for (const id of participants) {
68
- if (typeof id === 'string' && !known.has(id)) {
69
- unknown.add(id);
70
- }
71
- }
72
- }
73
- if (unknown.size === 0) {
74
- return;
75
- }
76
- const missing = Array.from(unknown)
77
- .map((id) => `"${id}"`)
78
- .join(', ');
79
- throw new Error(`MultiAgentGraph: edges reference agent(s) not present in agents: [${missing}]. ` +
80
- 'Ensure every agent referenced by an edge is also included in the `agents` array, ' +
81
- 'or filter orphaned edges before constructing the graph.');
82
- }
83
- /**
84
- * Categorize edges into handoff and direct types
85
- */
86
- categorizeEdges() {
87
- for (const edge of this.edges) {
88
- // Default behavior: edges with conditions or explicit 'handoff' type are handoff edges
89
- // Edges with explicit 'direct' type or multi-destination without conditions are direct edges
90
- if (edge.edgeType === 'direct') {
91
- this.directEdges.push(edge);
92
- }
93
- else if (edge.edgeType === 'handoff' || edge.condition != null) {
94
- this.handoffEdges.push(edge);
95
- }
96
- else {
97
- // Default: single-to-single edges are handoff, single-to-multiple are direct
98
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
99
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
100
- if (sources.length === 1 && destinations.length > 1) {
101
- // Fan-out pattern defaults to direct
102
- this.directEdges.push(edge);
103
- }
104
- else {
105
- // Everything else defaults to handoff
106
- this.handoffEdges.push(edge);
107
- }
108
- }
109
- }
110
- }
111
- /**
112
- * Analyze graph structure to determine starting nodes and connections
113
- */
114
- analyzeGraph() {
115
- const hasIncomingEdge = new Set();
116
- // Track all nodes that have incoming edges
117
- for (const edge of this.edges) {
118
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
119
- destinations.forEach((dest) => hasIncomingEdge.add(dest));
120
- }
121
- // Starting nodes are those without incoming edges
122
- for (const agentId of this.agentContexts.keys()) {
123
- if (!hasIncomingEdge.has(agentId)) {
124
- this.startingNodes.add(agentId);
125
- }
126
- }
127
- // If no starting nodes found, use the first agent
128
- if (this.startingNodes.size === 0 && this.agentContexts.size > 0) {
129
- this.startingNodes.add(this.agentContexts.keys().next().value);
130
- }
131
- // Determine if graph has parallel execution capability
132
- this.computeParallelCapability();
133
- }
134
- /**
135
- * Compute parallel groups by traversing the graph in execution order.
136
- * Assigns incrementing group IDs that reflect the sequential order of execution.
137
- *
138
- * For: researcher -> [analyst1, analyst2, analyst3] -> summarizer
139
- * - researcher: no group (first sequential node)
140
- * - analyst1, analyst2, analyst3: groupId 1 (first parallel group)
141
- * - summarizer: no group (next sequential node)
142
- *
143
- * This allows frontend to render in order:
144
- * Row 0: researcher
145
- * Row 1: [analyst1, analyst2, analyst3] (grouped)
146
- * Row 2: summarizer
147
- */
148
- computeParallelCapability() {
149
- let groupCounter = 1; // Start at 1, 0 reserved for "no group"
150
- // Check 1: Multiple starting nodes means parallel from the start (group 1)
151
- if (this.startingNodes.size > 1) {
152
- for (const agentId of this.startingNodes) {
153
- this.agentParallelGroups.set(agentId, groupCounter);
154
- }
155
- groupCounter++;
156
- }
157
- // Check 2: Traverse direct edges in order to find fan-out patterns
158
- // Build a simple execution order by following edges from starting nodes
159
- const visited = new Set();
160
- const queue = [...this.startingNodes];
161
- while (queue.length > 0) {
162
- const current = queue.shift();
163
- if (visited.has(current))
164
- continue;
165
- visited.add(current);
166
- // Find direct edges from this node
167
- for (const edge of this.directEdges) {
168
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
169
- if (!sources.includes(current))
170
- continue;
171
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
172
- // Fan-out: multiple destinations = parallel group
173
- if (destinations.length > 1) {
174
- for (const dest of destinations) {
175
- // Only set if not already in a group (first group wins)
176
- if (!this.agentParallelGroups.has(dest)) {
177
- this.agentParallelGroups.set(dest, groupCounter);
178
- }
179
- if (!visited.has(dest)) {
180
- queue.push(dest);
181
- }
182
- }
183
- groupCounter++;
184
- }
185
- else {
186
- // Single destination - add to queue for traversal
187
- for (const dest of destinations) {
188
- if (!visited.has(dest)) {
189
- queue.push(dest);
190
- }
191
- }
192
- }
193
- }
194
- // Also follow handoff edges for traversal (but they don't create parallel groups)
195
- for (const edge of this.handoffEdges) {
196
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
197
- if (!sources.includes(current))
198
- continue;
199
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
200
- for (const dest of destinations) {
201
- if (!visited.has(dest)) {
202
- queue.push(dest);
203
- }
204
- }
205
- }
206
- }
207
- }
208
- /**
209
- * Get the parallel group ID for an agent, if any.
210
- * Returns undefined if the agent is not part of a parallel group.
211
- * Group IDs are incrementing numbers reflecting execution order.
212
- */
213
- getParallelGroupId(agentId) {
214
- return this.agentParallelGroups.get(agentId);
215
- }
216
- /**
217
- * Override to indicate this is a multi-agent graph.
218
- * Enables agentId to be included in RunStep for frontend agent labeling.
219
- */
220
- isMultiAgentGraph() {
221
- return true;
222
- }
223
- /**
224
- * Override base class method to provide parallel group IDs for run steps.
225
- */
226
- getParallelGroupIdForAgent(agentId) {
227
- return this.agentParallelGroups.get(agentId);
228
- }
229
- /**
230
- * Create handoff tools for agents based on handoff edges only
231
- */
232
- createHandoffTools() {
233
- // Group handoff edges by source agent(s)
234
- const handoffsByAgent = new Map();
235
- // Only process handoff edges for tool creation
236
- for (const edge of this.handoffEdges) {
237
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
238
- sources.forEach((source) => {
239
- if (!handoffsByAgent.has(source)) {
240
- handoffsByAgent.set(source, []);
241
- }
242
- handoffsByAgent.get(source).push(edge);
243
- });
244
- }
245
- // Create handoff tools for each agent
246
- for (const [agentId, edges] of handoffsByAgent) {
247
- const agentContext = this.agentContexts.get(agentId);
248
- if (!agentContext)
249
- continue;
250
- // Create handoff tools for this agent's outgoing edges
251
- const handoffTools = [];
252
- const sourceAgentName = agentContext.name ?? agentId;
253
- for (const edge of edges) {
254
- handoffTools.push(...this.createHandoffToolsForEdge(edge, agentId, sourceAgentName));
255
- }
256
- if (!agentContext.graphTools) {
257
- agentContext.graphTools = [];
258
- }
259
- agentContext.graphTools.push(...handoffTools);
260
- }
261
- }
262
- /**
263
- * Create handoff tools for an edge (handles multiple destinations)
264
- * @param edge - The graph edge defining the handoff
265
- * @param sourceAgentId - The ID of the agent that will perform the handoff
266
- * @param sourceAgentName - The human-readable name of the source agent
267
- */
268
- createHandoffToolsForEdge(edge, sourceAgentId, sourceAgentName) {
269
- const tools = [];
270
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
271
- /** If there's a condition, create a single conditional handoff tool */
272
- if (edge.condition != null) {
273
- const toolName = 'conditional_transfer';
274
- const toolDescription = edge.description ?? 'Conditionally transfer control based on state';
275
- /** Check if we have a prompt for handoff input */
276
- const hasHandoffInput = edge.prompt != null && typeof edge.prompt === 'string';
277
- const handoffInputDescription = hasHandoffInput ? edge.prompt : undefined;
278
- const promptKey = edge.promptKey ?? 'instructions';
279
- tools.push(tool(async (rawInput, config) => {
280
- const input = rawInput;
281
- const state = getCurrentTaskInput();
282
- const toolCallId = config?.toolCall?.id ??
283
- 'unknown';
284
- /** Evaluated condition */
285
- const result = edge.condition(state);
286
- let destination;
287
- if (typeof result === 'boolean') {
288
- /** If true, use first destination; if false, don't transfer */
289
- if (!result)
290
- return null;
291
- destination = destinations[0];
292
- }
293
- else if (typeof result === 'string') {
294
- destination = result;
295
- }
296
- else {
297
- /** Array of destinations - for now, use the first */
298
- destination = Array.isArray(result) ? result[0] : destinations[0];
299
- }
300
- let content = `Conditionally transferred to ${destination}`;
301
- if (hasHandoffInput &&
302
- promptKey in input &&
303
- input[promptKey] != null) {
304
- content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
305
- }
306
- const toolMessage = new ToolMessage({
307
- content,
308
- name: toolName,
309
- tool_call_id: toolCallId,
310
- additional_kwargs: {
311
- /** Store destination for programmatic access in handoff detection */
312
- handoff_destination: destination,
313
- /** Store source agent name for receiving agent to know who handed off */
314
- handoff_source_name: sourceAgentName,
315
- },
316
- });
317
- return new Command({
318
- goto: destination,
319
- update: { messages: state.messages.concat(toolMessage) },
320
- graph: Command.PARENT,
321
- });
322
- }, {
323
- name: toolName,
324
- schema: hasHandoffInput
325
- ? {
326
- type: 'object',
327
- properties: {
328
- [promptKey]: {
329
- type: 'string',
330
- description: handoffInputDescription,
331
- },
332
- },
333
- required: [],
334
- }
335
- : { type: 'object', properties: {}, required: [] },
336
- description: toolDescription,
337
- }));
338
- }
339
- else {
340
- /** Create individual tools for each destination */
341
- for (const destination of destinations) {
342
- const toolName = `${Constants.LC_TRANSFER_TO_}${destination}`;
343
- const toolDescription = edge.description ?? `Transfer control to agent '${destination}'`;
344
- /** Check if we have a prompt for handoff input */
345
- const hasHandoffInput = edge.prompt != null && typeof edge.prompt === 'string';
346
- const handoffInputDescription = hasHandoffInput
347
- ? edge.prompt
348
- : undefined;
349
- const promptKey = edge.promptKey ?? 'instructions';
350
- tools.push(tool(async (rawInput, config) => {
351
- const input = rawInput;
352
- const toolCallId = config?.toolCall?.id ??
353
- 'unknown';
354
- let content = `Successfully transferred to ${destination}`;
355
- if (hasHandoffInput &&
356
- promptKey in input &&
357
- input[promptKey] != null) {
358
- content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
359
- }
360
- const toolMessage = new ToolMessage({
361
- content,
362
- name: toolName,
363
- tool_call_id: toolCallId,
364
- additional_kwargs: {
365
- /** Store source agent name for receiving agent to know who handed off */
366
- handoff_source_name: sourceAgentName,
367
- },
368
- });
369
- const state = getCurrentTaskInput();
370
- /**
371
- * For parallel handoff support:
372
- * Build messages that include ONLY this tool call's context.
373
- * This prevents errors when LLM calls multiple transfers simultaneously -
374
- * each destination gets a valid AIMessage with matching tool_call and tool_result.
375
- *
376
- * Strategy:
377
- * 1. Find the AIMessage containing this tool call
378
- * 2. Create a filtered AIMessage with ONLY this tool_call
379
- * 3. Include all messages before the AIMessage plus the filtered pair
380
- */
381
- const messages = state.messages;
382
- let filteredMessages = messages;
383
- let aiMessageIndex = -1;
384
- /** Find the AIMessage containing this tool call */
385
- for (let i = messages.length - 1; i >= 0; i--) {
386
- const msg = messages[i];
387
- if (msg.getType() === 'ai') {
388
- const aiMsg = msg;
389
- const hasThisCall = aiMsg.tool_calls?.some((tc) => tc.id === toolCallId);
390
- if (hasThisCall === true) {
391
- aiMessageIndex = i;
392
- break;
393
- }
394
- }
395
- }
396
- if (aiMessageIndex >= 0) {
397
- const originalAiMsg = messages[aiMessageIndex];
398
- const thisToolCall = originalAiMsg.tool_calls?.find((tc) => tc.id === toolCallId);
399
- if (thisToolCall != null &&
400
- (originalAiMsg.tool_calls?.length ?? 0) > 1) {
401
- /**
402
- * Multiple tool calls - create filtered AIMessage with ONLY this call.
403
- * This ensures valid message structure for parallel handoffs.
404
- */
405
- const filteredAiMsg = new AIMessage({
406
- content: originalAiMsg.content,
407
- tool_calls: [thisToolCall],
408
- id: originalAiMsg.id,
409
- });
410
- filteredMessages = [
411
- ...messages.slice(0, aiMessageIndex),
412
- filteredAiMsg,
413
- toolMessage,
414
- ];
415
- }
416
- else {
417
- /** Single tool call - use messages as-is */
418
- filteredMessages = messages.concat(toolMessage);
419
- }
420
- }
421
- else {
422
- /** Fallback - append tool message */
423
- filteredMessages = messages.concat(toolMessage);
424
- }
425
- return new Command({
426
- goto: destination,
427
- update: { messages: filteredMessages },
428
- graph: Command.PARENT,
429
- });
430
- }, {
431
- name: toolName,
432
- schema: hasHandoffInput
433
- ? {
434
- type: 'object',
435
- properties: {
436
- [promptKey]: {
437
- type: 'string',
438
- description: handoffInputDescription,
439
- },
440
- },
441
- required: [],
442
- }
443
- : { type: 'object', properties: {}, required: [] },
444
- description: toolDescription,
445
- }));
446
- }
447
- }
448
- return tools;
449
- }
450
- /**
451
- * Create a complete agent subgraph (similar to createReactAgent)
452
- */
453
- createAgentSubgraph(agentId) {
454
- /** This is essentially the same as `createAgentNode` from `StandardGraph` */
455
- return this.createAgentNode(agentId);
456
- }
457
- /**
458
- * Detects if the current agent is receiving a handoff and processes the messages accordingly.
459
- * Returns filtered messages with the transfer tool call/message removed, plus any instructions,
460
- * source agent, and parallel sibling information extracted from the transfer.
461
- *
462
- * Supports both single handoffs (last message is the transfer) and parallel handoffs
463
- * (multiple transfer ToolMessages, need to find the one targeting this agent).
464
- *
465
- * @param messages - Current state messages
466
- * @param agentId - The agent ID to check for handoff reception
467
- * @returns Object with filtered messages, extracted instructions, source agent, and parallel siblings
468
- */
469
- processHandoffReception(messages, agentId) {
470
- if (messages.length === 0)
471
- return null;
472
- /**
473
- * Search for a transfer ToolMessage targeting this agent.
474
- * For parallel handoffs, multiple transfer messages may exist - find ours.
475
- * Search backwards from the end to find the most recent transfer to this agent.
476
- */
477
- let toolMessage = null;
478
- let toolMessageIndex = -1;
479
- for (let i = messages.length - 1; i >= 0; i--) {
480
- const msg = messages[i];
481
- if (msg.getType() !== 'tool')
482
- continue;
483
- const candidateMsg = msg;
484
- const toolName = candidateMsg.name;
485
- if (typeof toolName !== 'string')
486
- continue;
487
- /** Check for standard transfer pattern */
488
- const isTransferMessage = toolName.startsWith(Constants.LC_TRANSFER_TO_);
489
- const isConditionalTransfer = toolName === 'conditional_transfer';
490
- if (!isTransferMessage && !isConditionalTransfer)
491
- continue;
492
- /** Extract destination from tool name or additional_kwargs */
493
- let destinationAgent = null;
494
- if (isTransferMessage) {
495
- destinationAgent = toolName.replace(Constants.LC_TRANSFER_TO_, '');
496
- }
497
- else if (isConditionalTransfer) {
498
- const handoffDest = candidateMsg.additional_kwargs.handoff_destination;
499
- destinationAgent = typeof handoffDest === 'string' ? handoffDest : null;
500
- }
501
- /** Check if this transfer targets our agent */
502
- if (destinationAgent === agentId) {
503
- toolMessage = candidateMsg;
504
- toolMessageIndex = i;
505
- break;
506
- }
507
- }
508
- /** No transfer targeting this agent found */
509
- if (toolMessage === null || toolMessageIndex < 0)
510
- return null;
511
- /** Extract instructions from the ToolMessage content */
512
- const contentStr = typeof toolMessage.content === 'string'
513
- ? toolMessage.content
514
- : JSON.stringify(toolMessage.content);
515
- const instructionsMatch = contentStr.match(HANDOFF_INSTRUCTIONS_PATTERN);
516
- const instructions = instructionsMatch?.[1]?.trim() ?? null;
517
- /** Extract source agent name from additional_kwargs */
518
- const handoffSourceName = toolMessage.additional_kwargs.handoff_source_name;
519
- const sourceAgentName = typeof handoffSourceName === 'string' ? handoffSourceName : null;
520
- /** Extract parallel siblings (set by ToolNode for parallel handoffs) */
521
- const rawSiblings = toolMessage.additional_kwargs.handoff_parallel_siblings;
522
- const siblingIds = Array.isArray(rawSiblings)
523
- ? rawSiblings.filter((s) => typeof s === 'string')
524
- : [];
525
- /** Convert IDs to display names */
526
- const parallelSiblings = siblingIds.map((id) => {
527
- const ctx = this.agentContexts.get(id);
528
- return ctx?.name ?? id;
529
- });
530
- /** Get the tool_call_id to find and filter the AI message's tool call */
531
- const toolCallId = toolMessage.tool_call_id;
532
- /**
533
- * Collect all transfer tool_call_ids to filter out.
534
- * For parallel handoffs, we filter ALL transfer messages (not just ours)
535
- * to give the receiving agent a clean context without handoff noise.
536
- */
537
- const transferToolCallIds = new Set([toolCallId]);
538
- for (const msg of messages) {
539
- if (msg.getType() !== 'tool')
540
- continue;
541
- const tm = msg;
542
- const tName = tm.name;
543
- if (typeof tName !== 'string')
544
- continue;
545
- if (tName.startsWith(Constants.LC_TRANSFER_TO_) ||
546
- tName === 'conditional_transfer') {
547
- transferToolCallIds.add(tm.tool_call_id);
548
- }
549
- }
550
- /** Filter out all transfer messages */
551
- const filteredMessages = [];
552
- for (let i = 0; i < messages.length; i++) {
553
- const msg = messages[i];
554
- const msgType = msg.getType();
555
- /** Skip transfer ToolMessages */
556
- if (msgType === 'tool') {
557
- const tm = msg;
558
- if (transferToolCallIds.has(tm.tool_call_id)) {
559
- continue;
560
- }
561
- }
562
- if (msgType === 'ai') {
563
- /** Check if this AI message contains any transfer tool calls */
564
- const aiMsg = msg;
565
- const toolCalls = aiMsg.tool_calls;
566
- if (toolCalls && toolCalls.length > 0) {
567
- /** Filter out all transfer tool calls */
568
- const remainingToolCalls = toolCalls.filter((tc) => tc.id == null || !transferToolCallIds.has(tc.id));
569
- const hasTransferCalls = remainingToolCalls.length < toolCalls.length;
570
- if (hasTransferCalls) {
571
- if (remainingToolCalls.length > 0 ||
572
- (typeof aiMsg.content === 'string' && aiMsg.content.trim())) {
573
- /** Keep the message but without transfer tool calls */
574
- const filteredAiMsg = new AIMessage({
575
- content: aiMsg.content,
576
- tool_calls: remainingToolCalls,
577
- id: aiMsg.id,
578
- });
579
- filteredMessages.push(filteredAiMsg);
580
- }
581
- /** If no remaining content or tool calls, skip this message entirely */
582
- continue;
583
- }
584
- }
585
- }
586
- /** Keep all other messages */
587
- filteredMessages.push(msg);
588
- }
589
- return {
590
- filteredMessages,
591
- instructions,
592
- sourceAgentName,
593
- parallelSiblings,
594
- };
595
- }
596
- /**
597
- * Create the multi-agent workflow with dynamic handoffs
598
- */
599
- createWorkflow() {
600
- const StateAnnotation = Annotation.Root({
601
- messages: Annotation({
602
- reducer: (a, b) => {
603
- if (!this.messages.length) {
604
- this.startIndex = a.length + b.length;
605
- }
606
- const result = messagesStateReducer(a, b);
607
- this.messages = result;
608
- return result;
609
- },
610
- default: () => [],
611
- }),
612
- /** Channel for passing filtered messages to agents when excludeResults is true */
613
- agentMessages: Annotation({
614
- /** Replaces state entirely */
615
- reducer: (a, b) => b,
616
- default: () => [],
617
- }),
618
- });
619
- const builder = new StateGraph(StateAnnotation);
620
- // Add all agents as complete subgraphs
621
- for (const [agentId] of this.agentContexts) {
622
- // Get all possible destinations for this agent
623
- const handoffDestinations = new Set();
624
- const directDestinations = new Set();
625
- // Check handoff edges for destinations
626
- for (const edge of this.handoffEdges) {
627
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
628
- if (sources.includes(agentId) === true) {
629
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
630
- dests.forEach((dest) => handoffDestinations.add(dest));
631
- }
632
- }
633
- // Check direct edges for destinations
634
- for (const edge of this.directEdges) {
635
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
636
- if (sources.includes(agentId) === true) {
637
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
638
- dests.forEach((dest) => directDestinations.add(dest));
639
- }
640
- }
641
- /** Check if this agent has BOTH handoff and direct edges */
642
- const hasHandoffEdges = handoffDestinations.size > 0;
643
- const hasDirectEdges = directDestinations.size > 0;
644
- const needsCommandRouting = hasHandoffEdges && hasDirectEdges;
645
- /** Collect all possible destinations for this agent */
646
- const allDestinations = new Set([
647
- ...handoffDestinations,
648
- ...directDestinations,
649
- ]);
650
- if (handoffDestinations.size > 0 || directDestinations.size === 0) {
651
- allDestinations.add(END);
652
- }
653
- /** Agent subgraph (includes agent + tools) */
654
- const agentSubgraph = this.createAgentSubgraph(agentId);
655
- /** Wrapper function that handles agentMessages channel, handoff reception, and conditional routing */
656
- const agentWrapper = async (state, config) => {
657
- let result;
658
- /**
659
- * Check if this agent is receiving a handoff.
660
- * If so, filter out the transfer messages and inject instructions as preamble.
661
- * This prevents the receiving agent from seeing the transfer as "completed work"
662
- * and prematurely producing an end token.
663
- */
664
- const handoffContext = this.processHandoffReception(state.messages, agentId);
665
- if (handoffContext !== null) {
666
- const { filteredMessages, instructions, sourceAgentName, parallelSiblings, } = handoffContext;
667
- /**
668
- * Set handoff context on the receiving agent.
669
- * Uses pre-computed graph position for depth and parallel info.
670
- */
671
- const agentContext = this.agentContexts.get(agentId);
672
- if (agentContext &&
673
- sourceAgentName != null &&
674
- sourceAgentName !== '') {
675
- agentContext.setHandoffContext(sourceAgentName, parallelSiblings);
676
- }
677
- /** Build messages for the receiving agent */
678
- let messagesForAgent = filteredMessages;
679
- /**
680
- * If there are instructions, inject them as a HumanMessage to
681
- * ground the receiving agent.
682
- *
683
- * When the last filtered message is a ToolMessage (e.g. from a
684
- * non-handoff tool the router called before handing off), a
685
- * synthetic AIMessage is inserted first to satisfy the
686
- * tool → assistant role ordering required by chat APIs. Without
687
- * this bridge, appending a HumanMessage directly after a
688
- * ToolMessage causes "400 Unexpected role 'user' after role
689
- * 'tool'" errors (see issue #54).
690
- */
691
- const hasInstructions = instructions !== null && instructions !== '';
692
- if (hasInstructions) {
693
- const lastMsg = filteredMessages.length > 0
694
- ? filteredMessages[filteredMessages.length - 1]
695
- : null;
696
- if (lastMsg != null && lastMsg.getType() === 'tool') {
697
- messagesForAgent = [
698
- ...filteredMessages,
699
- new AIMessage(`[Processed tool result and transferring to ${agentId}]`),
700
- new HumanMessage(instructions),
701
- ];
702
- }
703
- else {
704
- messagesForAgent = [
705
- ...filteredMessages,
706
- new HumanMessage(instructions),
707
- ];
708
- }
709
- }
710
- /** Update token map if we have a token counter */
711
- if (agentContext?.tokenCounter && hasInstructions) {
712
- const freshTokenMap = {};
713
- for (let i = 0; i < Math.min(filteredMessages.length, this.startIndex); i++) {
714
- const tokenCount = agentContext.indexTokenCountMap[i];
715
- if (tokenCount !== undefined) {
716
- freshTokenMap[i] = tokenCount;
717
- }
718
- }
719
- /** Add tokens for the bridge AIMessage + instructions HumanMessage */
720
- for (let i = filteredMessages.length; i < messagesForAgent.length; i++) {
721
- freshTokenMap[i] = agentContext.tokenCounter(messagesForAgent[i]);
722
- }
723
- agentContext.updateTokenMapWithInstructions(freshTokenMap);
724
- }
725
- const transformedState = {
726
- ...state,
727
- messages: messagesForAgent,
728
- };
729
- result = await agentSubgraph.invoke(transformedState, config);
730
- result = {
731
- ...result,
732
- agentMessages: [],
733
- };
734
- }
735
- else if (state.agentMessages != null &&
736
- state.agentMessages.length > 0) {
737
- /**
738
- * When using agentMessages (excludeResults=true), we need to update
739
- * the token map to account for the new prompt message
740
- */
741
- const agentContext = this.agentContexts.get(agentId);
742
- if (agentContext && agentContext.tokenCounter) {
743
- /** The agentMessages contains:
744
- * 1. Filtered messages (0 to startIndex) - already have token counts
745
- * 2. New prompt message - needs token counting
746
- */
747
- const freshTokenMap = {};
748
- /** Copy existing token counts for filtered messages (0 to startIndex) */
749
- for (let i = 0; i < this.startIndex; i++) {
750
- const tokenCount = agentContext.indexTokenCountMap[i];
751
- if (tokenCount !== undefined) {
752
- freshTokenMap[i] = tokenCount;
753
- }
754
- }
755
- /** Calculate tokens only for the new prompt message (last message) */
756
- const promptMessageIndex = state.agentMessages.length - 1;
757
- if (promptMessageIndex >= this.startIndex) {
758
- const promptMessage = state.agentMessages[promptMessageIndex];
759
- freshTokenMap[promptMessageIndex] =
760
- agentContext.tokenCounter(promptMessage);
761
- }
762
- /** Update the agent's token map with instructions added */
763
- agentContext.updateTokenMapWithInstructions(freshTokenMap);
764
- }
765
- /** Temporary state with messages replaced by `agentMessages` */
766
- const transformedState = {
767
- ...state,
768
- messages: state.agentMessages,
769
- };
770
- result = await agentSubgraph.invoke(transformedState, config);
771
- result = {
772
- ...result,
773
- /** Clear agentMessages for next agent */
774
- agentMessages: [],
775
- };
776
- }
777
- else {
778
- result = await agentSubgraph.invoke(state, config);
779
- }
780
- /** If agent has both handoff and direct edges, use Command for exclusive routing */
781
- if (needsCommandRouting) {
782
- /** Check if a handoff occurred */
783
- const lastMessage = result.messages[result.messages.length - 1];
784
- if (lastMessage != null &&
785
- lastMessage.getType() === 'tool' &&
786
- typeof lastMessage.name === 'string' &&
787
- lastMessage.name.startsWith(Constants.LC_TRANSFER_TO_)) {
788
- /** Handoff occurred - extract destination and navigate there exclusively */
789
- const handoffDest = lastMessage.name.replace(Constants.LC_TRANSFER_TO_, '');
790
- return new Command({
791
- update: result,
792
- goto: handoffDest,
793
- });
794
- }
795
- else {
796
- /** No handoff - proceed with direct edges */
797
- const directDests = Array.from(directDestinations);
798
- if (directDests.length === 1) {
799
- return new Command({
800
- update: result,
801
- goto: directDests[0],
802
- });
803
- }
804
- else if (directDests.length > 1) {
805
- /** Multiple direct destinations - they'll run in parallel */
806
- return new Command({
807
- update: result,
808
- goto: directDests,
809
- });
810
- }
811
- }
812
- }
813
- /** No special routing needed - return state normally */
814
- return result;
815
- };
816
- /** Wrapped agent as a node with its possible destinations */
817
- builder.addNode(agentId, agentWrapper, {
818
- ends: Array.from(allDestinations),
819
- });
820
- }
821
- // Add starting edges for all starting nodes
822
- for (const startNode of this.startingNodes) {
823
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
824
- /** @ts-ignore */
825
- builder.addEdge(START, startNode);
826
- }
827
- /**
828
- * Add direct edges for automatic transitions
829
- * Group edges by destination to handle fan-in scenarios
830
- */
831
- const edgesByDestination = new Map();
832
- for (const edge of this.directEdges) {
833
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
834
- for (const destination of destinations) {
835
- if (!edgesByDestination.has(destination)) {
836
- edgesByDestination.set(destination, []);
837
- }
838
- edgesByDestination.get(destination).push(edge);
839
- }
840
- }
841
- for (const [destination, edges] of edgesByDestination) {
842
- /** Checks if this is a fan-in scenario with prompt instructions */
843
- const edgesWithPrompt = edges.filter((edge) => edge.prompt != null && edge.prompt !== '');
844
- if (edgesWithPrompt.length > 0) {
845
- /**
846
- * Single wrapper node for destination (Fan-in with prompt)
847
- */
848
- const wrapperNodeId = `fan_in_${destination}_prompt`;
849
- /**
850
- * First edge's `prompt`
851
- * (they should all be the same for fan-in)
852
- */
853
- const prompt = edgesWithPrompt[0].prompt;
854
- /**
855
- * First edge's `excludeResults` flag
856
- * (they should all be the same for fan-in)
857
- */
858
- const excludeResults = edgesWithPrompt[0].excludeResults;
859
- builder.addNode(wrapperNodeId, async (state) => {
860
- let promptText;
861
- let effectiveExcludeResults = excludeResults;
862
- if (typeof prompt === 'function') {
863
- promptText = await prompt(state.messages, this.startIndex);
864
- }
865
- else if (prompt != null) {
866
- if (prompt.includes('{results}')) {
867
- const resultsMessages = state.messages.slice(this.startIndex);
868
- const resultsString = getBufferString(resultsMessages);
869
- const promptTemplate = PromptTemplate.fromTemplate(prompt);
870
- const result = await promptTemplate.invoke({
871
- results: resultsString,
872
- });
873
- promptText = result.value;
874
- effectiveExcludeResults =
875
- excludeResults !== false && promptText !== '';
876
- }
877
- else {
878
- promptText = prompt;
879
- }
880
- }
881
- if (promptText != null && promptText !== '') {
882
- if (effectiveExcludeResults == null ||
883
- effectiveExcludeResults === false) {
884
- return {
885
- messages: [new HumanMessage(promptText)],
886
- };
887
- }
888
- /** When `excludeResults` is true, use agentMessages channel
889
- * to pass filtered messages + prompt to the destination agent
890
- */
891
- const filteredMessages = state.messages.slice(0, this.startIndex);
892
- const promptMessage = new HumanMessage(promptText);
893
- return {
894
- messages: [promptMessage],
895
- agentMessages: messagesStateReducer(filteredMessages, [
896
- promptMessage,
897
- ]),
898
- };
899
- }
900
- /** No prompt needed, return empty update */
901
- return {};
902
- });
903
- /** Add edges from all sources to the wrapper, then wrapper to destination */
904
- for (const edge of edges) {
905
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
906
- for (const source of sources) {
907
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
908
- /** @ts-ignore */
909
- builder.addEdge(source, wrapperNodeId);
910
- }
911
- }
912
- /** Single edge from wrapper to destination */
913
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
914
- /** @ts-ignore */
915
- builder.addEdge(wrapperNodeId, destination);
916
- }
917
- else {
918
- /** No prompt instructions, add direct edges (skip if source uses Command routing) */
919
- for (const edge of edges) {
920
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
921
- for (const source of sources) {
922
- /** Check if this source node has both handoff and direct edges */
923
- const sourceHandoffEdges = this.handoffEdges.filter((e) => {
924
- const eSources = Array.isArray(e.from) ? e.from : [e.from];
925
- return eSources.includes(source);
926
- });
927
- const sourceDirectEdges = this.directEdges.filter((e) => {
928
- const eSources = Array.isArray(e.from) ? e.from : [e.from];
929
- return eSources.includes(source);
930
- });
931
- /** Skip adding edge if source uses Command routing (has both types) */
932
- if (sourceHandoffEdges.length > 0 && sourceDirectEdges.length > 0) {
933
- continue;
934
- }
935
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
936
- /** @ts-ignore */
937
- builder.addEdge(source, destination);
938
- }
939
- }
940
- }
941
- }
942
- return builder.compile(this.compileOptions);
943
- }
944
- }
945
-
12
+ * MultiAgentGraph extends StandardGraph to support dynamic multi-agent workflows
13
+ * with handoffs, fan-in/fan-out, and other composable patterns.
14
+ *
15
+ * Key behavior:
16
+ * - Agents with ONLY handoff edges: Can dynamically route to any handoff destination
17
+ * - Agents with ONLY direct edges: Always follow their direct edges
18
+ * - Agents with BOTH: Use Command for exclusive routing (handoff OR direct, not both)
19
+ * - If handoff occurs: Only the handoff destination executes
20
+ * - If no handoff: Direct edges execute (potentially in parallel)
21
+ *
22
+ * This enables the common pattern where an agent either delegates (handoff)
23
+ * OR continues its workflow (direct edges), but not both simultaneously.
24
+ */
25
+ var MultiAgentGraph = class extends StandardGraph {
26
+ edges;
27
+ startingNodes = /* @__PURE__ */ new Set();
28
+ directEdges = [];
29
+ handoffEdges = [];
30
+ /**
31
+ * Map of agentId to parallel group info.
32
+ * Contains groupId (incrementing number reflecting execution order) for agents in parallel groups.
33
+ * Sequential agents (not in any parallel group) have undefined entry.
34
+ *
35
+ * Example for: researcher -> [analyst1, analyst2, analyst3] -> summarizer
36
+ * - researcher: undefined (sequential, order 0)
37
+ * - analyst1, analyst2, analyst3: { groupId: 1 } (parallel group, order 1)
38
+ * - summarizer: undefined (sequential, order 2)
39
+ */
40
+ agentParallelGroups = /* @__PURE__ */ new Map();
41
+ constructor(input) {
42
+ super(input);
43
+ this.edges = input.edges;
44
+ this.validateEdgeAgents();
45
+ this.categorizeEdges();
46
+ this.analyzeGraph();
47
+ this.createHandoffTools();
48
+ }
49
+ /**
50
+ * Fails fast when an edge references an agent that is not in
51
+ * `agentContexts`. Without this check, the underlying LangGraph
52
+ * `StateGraph.compile()` would throw the opaque
53
+ * `Found edge ending at unknown node "<id>"` error after graph
54
+ * construction — far from the true root cause.
55
+ *
56
+ * This catches the common misuse of passing `edges` into a multi-agent
57
+ * config without also passing the corresponding sub-agent configs in
58
+ * `agents` (e.g. a host that forgot to pre-load handoff targets).
59
+ */
60
+ validateEdgeAgents() {
61
+ const known = new Set(this.agentContexts.keys());
62
+ const unknown = /* @__PURE__ */ new Set();
63
+ for (const edge of this.edges) {
64
+ const participants = [...Array.isArray(edge.from) ? edge.from : [edge.from], ...Array.isArray(edge.to) ? edge.to : [edge.to]];
65
+ for (const id of participants) if (typeof id === "string" && !known.has(id)) unknown.add(id);
66
+ }
67
+ if (unknown.size === 0) return;
68
+ const missing = Array.from(unknown).map((id) => `"${id}"`).join(", ");
69
+ throw new Error(`MultiAgentGraph: edges reference agent(s) not present in agents: [${missing}]. Ensure every agent referenced by an edge is also included in the \`agents\` array, or filter orphaned edges before constructing the graph.`);
70
+ }
71
+ /**
72
+ * Categorize edges into handoff and direct types
73
+ */
74
+ categorizeEdges() {
75
+ for (const edge of this.edges) if (edge.edgeType === "direct") this.directEdges.push(edge);
76
+ else if (edge.edgeType === "handoff" || edge.condition != null) this.handoffEdges.push(edge);
77
+ else {
78
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
79
+ if ((Array.isArray(edge.from) ? edge.from : [edge.from]).length === 1 && destinations.length > 1) this.directEdges.push(edge);
80
+ else this.handoffEdges.push(edge);
81
+ }
82
+ }
83
+ /**
84
+ * Analyze graph structure to determine starting nodes and connections
85
+ */
86
+ analyzeGraph() {
87
+ const hasIncomingEdge = /* @__PURE__ */ new Set();
88
+ for (const edge of this.edges) (Array.isArray(edge.to) ? edge.to : [edge.to]).forEach((dest) => hasIncomingEdge.add(dest));
89
+ for (const agentId of this.agentContexts.keys()) if (!hasIncomingEdge.has(agentId)) this.startingNodes.add(agentId);
90
+ if (this.startingNodes.size === 0 && this.agentContexts.size > 0) this.startingNodes.add(this.agentContexts.keys().next().value);
91
+ this.computeParallelCapability();
92
+ }
93
+ /**
94
+ * Compute parallel groups by traversing the graph in execution order.
95
+ * Assigns incrementing group IDs that reflect the sequential order of execution.
96
+ *
97
+ * For: researcher -> [analyst1, analyst2, analyst3] -> summarizer
98
+ * - researcher: no group (first sequential node)
99
+ * - analyst1, analyst2, analyst3: groupId 1 (first parallel group)
100
+ * - summarizer: no group (next sequential node)
101
+ *
102
+ * This allows frontend to render in order:
103
+ * Row 0: researcher
104
+ * Row 1: [analyst1, analyst2, analyst3] (grouped)
105
+ * Row 2: summarizer
106
+ */
107
+ computeParallelCapability() {
108
+ let groupCounter = 1;
109
+ if (this.startingNodes.size > 1) {
110
+ for (const agentId of this.startingNodes) this.agentParallelGroups.set(agentId, groupCounter);
111
+ groupCounter++;
112
+ }
113
+ const visited = /* @__PURE__ */ new Set();
114
+ const queue = [...this.startingNodes];
115
+ while (queue.length > 0) {
116
+ const current = queue.shift();
117
+ if (visited.has(current)) continue;
118
+ visited.add(current);
119
+ for (const edge of this.directEdges) {
120
+ if (!(Array.isArray(edge.from) ? edge.from : [edge.from]).includes(current)) continue;
121
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
122
+ if (destinations.length > 1) {
123
+ for (const dest of destinations) {
124
+ if (!this.agentParallelGroups.has(dest)) this.agentParallelGroups.set(dest, groupCounter);
125
+ if (!visited.has(dest)) queue.push(dest);
126
+ }
127
+ groupCounter++;
128
+ } else for (const dest of destinations) if (!visited.has(dest)) queue.push(dest);
129
+ }
130
+ for (const edge of this.handoffEdges) {
131
+ if (!(Array.isArray(edge.from) ? edge.from : [edge.from]).includes(current)) continue;
132
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
133
+ for (const dest of destinations) if (!visited.has(dest)) queue.push(dest);
134
+ }
135
+ }
136
+ }
137
+ /**
138
+ * Get the parallel group ID for an agent, if any.
139
+ * Returns undefined if the agent is not part of a parallel group.
140
+ * Group IDs are incrementing numbers reflecting execution order.
141
+ */
142
+ getParallelGroupId(agentId) {
143
+ return this.agentParallelGroups.get(agentId);
144
+ }
145
+ /**
146
+ * Override to indicate this is a multi-agent graph.
147
+ * Enables agentId to be included in RunStep for frontend agent labeling.
148
+ */
149
+ isMultiAgentGraph() {
150
+ return true;
151
+ }
152
+ /**
153
+ * Override base class method to provide parallel group IDs for run steps.
154
+ */
155
+ getParallelGroupIdForAgent(agentId) {
156
+ return this.agentParallelGroups.get(agentId);
157
+ }
158
+ /**
159
+ * Create handoff tools for agents based on handoff edges only
160
+ */
161
+ createHandoffTools() {
162
+ const handoffsByAgent = /* @__PURE__ */ new Map();
163
+ for (const edge of this.handoffEdges) (Array.isArray(edge.from) ? edge.from : [edge.from]).forEach((source) => {
164
+ if (!handoffsByAgent.has(source)) handoffsByAgent.set(source, []);
165
+ handoffsByAgent.get(source).push(edge);
166
+ });
167
+ for (const [agentId, edges] of handoffsByAgent) {
168
+ const agentContext = this.agentContexts.get(agentId);
169
+ if (!agentContext) continue;
170
+ const handoffTools = [];
171
+ const sourceAgentName = agentContext.name ?? agentId;
172
+ for (const edge of edges) handoffTools.push(...this.createHandoffToolsForEdge(edge, agentId, sourceAgentName));
173
+ if (!agentContext.graphTools) agentContext.graphTools = [];
174
+ agentContext.graphTools.push(...handoffTools);
175
+ }
176
+ }
177
+ /**
178
+ * Create handoff tools for an edge (handles multiple destinations)
179
+ * @param edge - The graph edge defining the handoff
180
+ * @param sourceAgentId - The ID of the agent that will perform the handoff
181
+ * @param sourceAgentName - The human-readable name of the source agent
182
+ */
183
+ createHandoffToolsForEdge(edge, sourceAgentId, sourceAgentName) {
184
+ const tools = [];
185
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
186
+ /** If there's a condition, create a single conditional handoff tool */
187
+ if (edge.condition != null) {
188
+ const toolName = "conditional_transfer";
189
+ const toolDescription = edge.description ?? "Conditionally transfer control based on state";
190
+ /** Check if we have a prompt for handoff input */
191
+ const hasHandoffInput = edge.prompt != null && typeof edge.prompt === "string";
192
+ const handoffInputDescription = hasHandoffInput ? edge.prompt : void 0;
193
+ const promptKey = edge.promptKey ?? "instructions";
194
+ tools.push(tool(async (rawInput, config) => {
195
+ const input = rawInput;
196
+ const state = getCurrentTaskInput();
197
+ const toolCallId = config?.toolCall?.id ?? "unknown";
198
+ /** Evaluated condition */
199
+ const result = edge.condition(state);
200
+ let destination;
201
+ if (typeof result === "boolean") {
202
+ /** If true, use first destination; if false, don't transfer */
203
+ if (!result) return null;
204
+ destination = destinations[0];
205
+ } else if (typeof result === "string") destination = result;
206
+ else
207
+ /** Array of destinations - for now, use the first */
208
+ destination = Array.isArray(result) ? result[0] : destinations[0];
209
+ let content = `Conditionally transferred to ${destination}`;
210
+ if (hasHandoffInput && promptKey in input && input[promptKey] != null) content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
211
+ const toolMessage = new ToolMessage({
212
+ content,
213
+ name: toolName,
214
+ tool_call_id: toolCallId,
215
+ additional_kwargs: {
216
+ /** Store destination for programmatic access in handoff detection */
217
+ handoff_destination: destination,
218
+ /** Store source agent name for receiving agent to know who handed off */
219
+ handoff_source_name: sourceAgentName
220
+ }
221
+ });
222
+ return new Command({
223
+ goto: destination,
224
+ update: { messages: state.messages.concat(toolMessage) },
225
+ graph: Command.PARENT
226
+ });
227
+ }, {
228
+ name: toolName,
229
+ schema: hasHandoffInput ? {
230
+ type: "object",
231
+ properties: { [promptKey]: {
232
+ type: "string",
233
+ description: handoffInputDescription
234
+ } },
235
+ required: []
236
+ } : {
237
+ type: "object",
238
+ properties: {},
239
+ required: []
240
+ },
241
+ description: toolDescription
242
+ }));
243
+ } else
244
+ /** Create individual tools for each destination */
245
+ for (const destination of destinations) {
246
+ const toolName = `lc_transfer_to_${destination}`;
247
+ const toolDescription = edge.description ?? `Transfer control to agent '${destination}'`;
248
+ /** Check if we have a prompt for handoff input */
249
+ const hasHandoffInput = edge.prompt != null && typeof edge.prompt === "string";
250
+ const handoffInputDescription = hasHandoffInput ? edge.prompt : void 0;
251
+ const promptKey = edge.promptKey ?? "instructions";
252
+ tools.push(tool(async (rawInput, config) => {
253
+ const input = rawInput;
254
+ const toolCallId = config?.toolCall?.id ?? "unknown";
255
+ let content = `Successfully transferred to ${destination}`;
256
+ if (hasHandoffInput && promptKey in input && input[promptKey] != null) content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
257
+ const toolMessage = new ToolMessage({
258
+ content,
259
+ name: toolName,
260
+ tool_call_id: toolCallId,
261
+ additional_kwargs: {
262
+ /** Store source agent name for receiving agent to know who handed off */
263
+ handoff_source_name: sourceAgentName }
264
+ });
265
+ /**
266
+ * For parallel handoff support:
267
+ * Build messages that include ONLY this tool call's context.
268
+ * This prevents errors when LLM calls multiple transfers simultaneously -
269
+ * each destination gets a valid AIMessage with matching tool_call and tool_result.
270
+ *
271
+ * Strategy:
272
+ * 1. Find the AIMessage containing this tool call
273
+ * 2. Create a filtered AIMessage with ONLY this tool_call
274
+ * 3. Include all messages before the AIMessage plus the filtered pair
275
+ */
276
+ const messages = getCurrentTaskInput().messages;
277
+ let filteredMessages = messages;
278
+ let aiMessageIndex = -1;
279
+ /** Find the AIMessage containing this tool call */
280
+ for (let i = messages.length - 1; i >= 0; i--) {
281
+ const msg = messages[i];
282
+ if (msg.getType() === "ai") {
283
+ if (msg.tool_calls?.some((tc) => tc.id === toolCallId) === true) {
284
+ aiMessageIndex = i;
285
+ break;
286
+ }
287
+ }
288
+ }
289
+ if (aiMessageIndex >= 0) {
290
+ const originalAiMsg = messages[aiMessageIndex];
291
+ const thisToolCall = originalAiMsg.tool_calls?.find((tc) => tc.id === toolCallId);
292
+ if (thisToolCall != null && (originalAiMsg.tool_calls?.length ?? 0) > 1) {
293
+ /**
294
+ * Multiple tool calls - create filtered AIMessage with ONLY this call.
295
+ * This ensures valid message structure for parallel handoffs.
296
+ */
297
+ const filteredAiMsg = new AIMessage({
298
+ content: originalAiMsg.content,
299
+ tool_calls: [thisToolCall],
300
+ id: originalAiMsg.id
301
+ });
302
+ filteredMessages = [
303
+ ...messages.slice(0, aiMessageIndex),
304
+ filteredAiMsg,
305
+ toolMessage
306
+ ];
307
+ } else
308
+ /** Single tool call - use messages as-is */
309
+ filteredMessages = messages.concat(toolMessage);
310
+ } else
311
+ /** Fallback - append tool message */
312
+ filteredMessages = messages.concat(toolMessage);
313
+ return new Command({
314
+ goto: destination,
315
+ update: { messages: filteredMessages },
316
+ graph: Command.PARENT
317
+ });
318
+ }, {
319
+ name: toolName,
320
+ schema: hasHandoffInput ? {
321
+ type: "object",
322
+ properties: { [promptKey]: {
323
+ type: "string",
324
+ description: handoffInputDescription
325
+ } },
326
+ required: []
327
+ } : {
328
+ type: "object",
329
+ properties: {},
330
+ required: []
331
+ },
332
+ description: toolDescription
333
+ }));
334
+ }
335
+ return tools;
336
+ }
337
+ /**
338
+ * Create a complete agent subgraph (similar to createReactAgent)
339
+ */
340
+ createAgentSubgraph(agentId) {
341
+ /** This is essentially the same as `createAgentNode` from `StandardGraph` */
342
+ return this.createAgentNode(agentId);
343
+ }
344
+ /**
345
+ * Detects if the current agent is receiving a handoff and processes the messages accordingly.
346
+ * Returns filtered messages with the transfer tool call/message removed, plus any instructions,
347
+ * source agent, and parallel sibling information extracted from the transfer.
348
+ *
349
+ * Supports both single handoffs (last message is the transfer) and parallel handoffs
350
+ * (multiple transfer ToolMessages, need to find the one targeting this agent).
351
+ *
352
+ * @param messages - Current state messages
353
+ * @param agentId - The agent ID to check for handoff reception
354
+ * @returns Object with filtered messages, extracted instructions, source agent, and parallel siblings
355
+ */
356
+ processHandoffReception(messages, agentId) {
357
+ if (messages.length === 0) return null;
358
+ /**
359
+ * Search for a transfer ToolMessage targeting this agent.
360
+ * For parallel handoffs, multiple transfer messages may exist - find ours.
361
+ * Search backwards from the end to find the most recent transfer to this agent.
362
+ */
363
+ let toolMessage = null;
364
+ let toolMessageIndex = -1;
365
+ for (let i = messages.length - 1; i >= 0; i--) {
366
+ const msg = messages[i];
367
+ if (msg.getType() !== "tool") continue;
368
+ const candidateMsg = msg;
369
+ const toolName = candidateMsg.name;
370
+ if (typeof toolName !== "string") continue;
371
+ /** Check for standard transfer pattern */
372
+ const isTransferMessage = toolName.startsWith("lc_transfer_to_");
373
+ const isConditionalTransfer = toolName === "conditional_transfer";
374
+ if (!isTransferMessage && !isConditionalTransfer) continue;
375
+ /** Extract destination from tool name or additional_kwargs */
376
+ let destinationAgent = null;
377
+ if (isTransferMessage) destinationAgent = toolName.replace("lc_transfer_to_", "");
378
+ else if (isConditionalTransfer) {
379
+ const handoffDest = candidateMsg.additional_kwargs.handoff_destination;
380
+ destinationAgent = typeof handoffDest === "string" ? handoffDest : null;
381
+ }
382
+ /** Check if this transfer targets our agent */
383
+ if (destinationAgent === agentId) {
384
+ toolMessage = candidateMsg;
385
+ toolMessageIndex = i;
386
+ break;
387
+ }
388
+ }
389
+ /** No transfer targeting this agent found */
390
+ if (toolMessage === null || toolMessageIndex < 0) return null;
391
+ const instructions = (typeof toolMessage.content === "string" ? toolMessage.content : JSON.stringify(toolMessage.content)).match(HANDOFF_INSTRUCTIONS_PATTERN)?.[1]?.trim() ?? null;
392
+ /** Extract source agent name from additional_kwargs */
393
+ const handoffSourceName = toolMessage.additional_kwargs.handoff_source_name;
394
+ const sourceAgentName = typeof handoffSourceName === "string" ? handoffSourceName : null;
395
+ /** Extract parallel siblings (set by ToolNode for parallel handoffs) */
396
+ const rawSiblings = toolMessage.additional_kwargs.handoff_parallel_siblings;
397
+ /** Convert IDs to display names */
398
+ const parallelSiblings = (Array.isArray(rawSiblings) ? rawSiblings.filter((s) => typeof s === "string") : []).map((id) => {
399
+ return this.agentContexts.get(id)?.name ?? id;
400
+ });
401
+ /** Get the tool_call_id to find and filter the AI message's tool call */
402
+ const toolCallId = toolMessage.tool_call_id;
403
+ /**
404
+ * Collect all transfer tool_call_ids to filter out.
405
+ * For parallel handoffs, we filter ALL transfer messages (not just ours)
406
+ * to give the receiving agent a clean context without handoff noise.
407
+ */
408
+ const transferToolCallIds = new Set([toolCallId]);
409
+ for (const msg of messages) {
410
+ if (msg.getType() !== "tool") continue;
411
+ const tm = msg;
412
+ const tName = tm.name;
413
+ if (typeof tName !== "string") continue;
414
+ if (tName.startsWith("lc_transfer_to_") || tName === "conditional_transfer") transferToolCallIds.add(tm.tool_call_id);
415
+ }
416
+ /** Filter out all transfer messages */
417
+ const filteredMessages = [];
418
+ for (let i = 0; i < messages.length; i++) {
419
+ const msg = messages[i];
420
+ const msgType = msg.getType();
421
+ /** Skip transfer ToolMessages */
422
+ if (msgType === "tool") {
423
+ const tm = msg;
424
+ if (transferToolCallIds.has(tm.tool_call_id)) continue;
425
+ }
426
+ if (msgType === "ai") {
427
+ /** Check if this AI message contains any transfer tool calls */
428
+ const aiMsg = msg;
429
+ const toolCalls = aiMsg.tool_calls;
430
+ if (toolCalls && toolCalls.length > 0) {
431
+ /** Filter out all transfer tool calls */
432
+ const remainingToolCalls = toolCalls.filter((tc) => tc.id == null || !transferToolCallIds.has(tc.id));
433
+ if (remainingToolCalls.length < toolCalls.length) {
434
+ if (remainingToolCalls.length > 0 || typeof aiMsg.content === "string" && aiMsg.content.trim()) {
435
+ /** Keep the message but without transfer tool calls */
436
+ const filteredAiMsg = new AIMessage({
437
+ content: aiMsg.content,
438
+ tool_calls: remainingToolCalls,
439
+ id: aiMsg.id
440
+ });
441
+ filteredMessages.push(filteredAiMsg);
442
+ }
443
+ /** If no remaining content or tool calls, skip this message entirely */
444
+ continue;
445
+ }
446
+ }
447
+ }
448
+ /** Keep all other messages */
449
+ filteredMessages.push(msg);
450
+ }
451
+ return {
452
+ filteredMessages,
453
+ instructions,
454
+ sourceAgentName,
455
+ parallelSiblings
456
+ };
457
+ }
458
+ /**
459
+ * Create the multi-agent workflow with dynamic handoffs
460
+ */
461
+ createWorkflow() {
462
+ const builder = new StateGraph(Annotation.Root({
463
+ messages: Annotation({
464
+ reducer: (a, b) => {
465
+ if (!this.messages.length) this.startIndex = a.length + b.length;
466
+ const result = messagesStateReducer(a, b);
467
+ this.messages = result;
468
+ return result;
469
+ },
470
+ default: () => []
471
+ }),
472
+ /** Channel for passing filtered messages to agents when excludeResults is true */
473
+ agentMessages: Annotation({
474
+ /** Replaces state entirely */
475
+ reducer: (a, b) => b,
476
+ default: () => []
477
+ })
478
+ }));
479
+ for (const [agentId] of this.agentContexts) {
480
+ const handoffDestinations = /* @__PURE__ */ new Set();
481
+ const directDestinations = /* @__PURE__ */ new Set();
482
+ for (const edge of this.handoffEdges) if ((Array.isArray(edge.from) ? edge.from : [edge.from]).includes(agentId) === true) (Array.isArray(edge.to) ? edge.to : [edge.to]).forEach((dest) => handoffDestinations.add(dest));
483
+ for (const edge of this.directEdges) if ((Array.isArray(edge.from) ? edge.from : [edge.from]).includes(agentId) === true) (Array.isArray(edge.to) ? edge.to : [edge.to]).forEach((dest) => directDestinations.add(dest));
484
+ /** Check if this agent has BOTH handoff and direct edges */
485
+ const hasHandoffEdges = handoffDestinations.size > 0;
486
+ const hasDirectEdges = directDestinations.size > 0;
487
+ const needsCommandRouting = hasHandoffEdges && hasDirectEdges;
488
+ /** Collect all possible destinations for this agent */
489
+ const allDestinations = new Set([...handoffDestinations, ...directDestinations]);
490
+ if (handoffDestinations.size > 0 || directDestinations.size === 0) allDestinations.add(END);
491
+ /** Agent subgraph (includes agent + tools) */
492
+ const agentSubgraph = this.createAgentSubgraph(agentId);
493
+ /** Wrapper function that handles agentMessages channel, handoff reception, and conditional routing */
494
+ const agentWrapper = async (state, config) => {
495
+ let result;
496
+ /**
497
+ * Check if this agent is receiving a handoff.
498
+ * If so, filter out the transfer messages and inject instructions as preamble.
499
+ * This prevents the receiving agent from seeing the transfer as "completed work"
500
+ * and prematurely producing an end token.
501
+ */
502
+ const handoffContext = this.processHandoffReception(state.messages, agentId);
503
+ if (handoffContext !== null) {
504
+ const { filteredMessages, instructions, sourceAgentName, parallelSiblings } = handoffContext;
505
+ /**
506
+ * Set handoff context on the receiving agent.
507
+ * Uses pre-computed graph position for depth and parallel info.
508
+ */
509
+ const agentContext = this.agentContexts.get(agentId);
510
+ if (agentContext && sourceAgentName != null && sourceAgentName !== "") agentContext.setHandoffContext(sourceAgentName, parallelSiblings);
511
+ /** Build messages for the receiving agent */
512
+ let messagesForAgent = filteredMessages;
513
+ /**
514
+ * If there are instructions, inject them as a HumanMessage to
515
+ * ground the receiving agent.
516
+ *
517
+ * When the last filtered message is a ToolMessage (e.g. from a
518
+ * non-handoff tool the router called before handing off), a
519
+ * synthetic AIMessage is inserted first to satisfy the
520
+ * tool assistant role ordering required by chat APIs. Without
521
+ * this bridge, appending a HumanMessage directly after a
522
+ * ToolMessage causes "400 Unexpected role 'user' after role
523
+ * 'tool'" errors (see issue #54).
524
+ */
525
+ const hasInstructions = instructions !== null && instructions !== "";
526
+ if (hasInstructions) {
527
+ const lastMsg = filteredMessages.length > 0 ? filteredMessages[filteredMessages.length - 1] : null;
528
+ if (lastMsg != null && lastMsg.getType() === "tool") messagesForAgent = [
529
+ ...filteredMessages,
530
+ new AIMessage(`[Processed tool result and transferring to ${agentId}]`),
531
+ new HumanMessage(instructions)
532
+ ];
533
+ else messagesForAgent = [...filteredMessages, new HumanMessage(instructions)];
534
+ }
535
+ /** Update token map if we have a token counter */
536
+ if (agentContext?.tokenCounter && hasInstructions) {
537
+ const freshTokenMap = {};
538
+ for (let i = 0; i < Math.min(filteredMessages.length, this.startIndex); i++) {
539
+ const tokenCount = agentContext.indexTokenCountMap[i];
540
+ if (tokenCount !== void 0) freshTokenMap[i] = tokenCount;
541
+ }
542
+ /** Add tokens for the bridge AIMessage + instructions HumanMessage */
543
+ for (let i = filteredMessages.length; i < messagesForAgent.length; i++) freshTokenMap[i] = agentContext.tokenCounter(messagesForAgent[i]);
544
+ agentContext.updateTokenMapWithInstructions(freshTokenMap);
545
+ }
546
+ const transformedState = {
547
+ ...state,
548
+ messages: messagesForAgent
549
+ };
550
+ result = await agentSubgraph.invoke(transformedState, config);
551
+ result = {
552
+ ...result,
553
+ agentMessages: []
554
+ };
555
+ } else if (state.agentMessages != null && state.agentMessages.length > 0) {
556
+ /**
557
+ * When using agentMessages (excludeResults=true), we need to update
558
+ * the token map to account for the new prompt message
559
+ */
560
+ const agentContext = this.agentContexts.get(agentId);
561
+ if (agentContext && agentContext.tokenCounter) {
562
+ /** The agentMessages contains:
563
+ * 1. Filtered messages (0 to startIndex) - already have token counts
564
+ * 2. New prompt message - needs token counting
565
+ */
566
+ const freshTokenMap = {};
567
+ /** Copy existing token counts for filtered messages (0 to startIndex) */
568
+ for (let i = 0; i < this.startIndex; i++) {
569
+ const tokenCount = agentContext.indexTokenCountMap[i];
570
+ if (tokenCount !== void 0) freshTokenMap[i] = tokenCount;
571
+ }
572
+ /** Calculate tokens only for the new prompt message (last message) */
573
+ const promptMessageIndex = state.agentMessages.length - 1;
574
+ if (promptMessageIndex >= this.startIndex) {
575
+ const promptMessage = state.agentMessages[promptMessageIndex];
576
+ freshTokenMap[promptMessageIndex] = agentContext.tokenCounter(promptMessage);
577
+ }
578
+ /** Update the agent's token map with instructions added */
579
+ agentContext.updateTokenMapWithInstructions(freshTokenMap);
580
+ }
581
+ /** Temporary state with messages replaced by `agentMessages` */
582
+ const transformedState = {
583
+ ...state,
584
+ messages: state.agentMessages
585
+ };
586
+ result = await agentSubgraph.invoke(transformedState, config);
587
+ result = {
588
+ ...result,
589
+ /** Clear agentMessages for next agent */
590
+ agentMessages: []
591
+ };
592
+ } else result = await agentSubgraph.invoke(state, config);
593
+ /** If agent has both handoff and direct edges, use Command for exclusive routing */
594
+ if (needsCommandRouting) {
595
+ /** Check if a handoff occurred */
596
+ const lastMessage = result.messages[result.messages.length - 1];
597
+ if (lastMessage != null && lastMessage.getType() === "tool" && typeof lastMessage.name === "string" && lastMessage.name.startsWith("lc_transfer_to_")) {
598
+ /** Handoff occurred - extract destination and navigate there exclusively */
599
+ const handoffDest = lastMessage.name.replace("lc_transfer_to_", "");
600
+ return new Command({
601
+ update: result,
602
+ goto: handoffDest
603
+ });
604
+ } else {
605
+ /** No handoff - proceed with direct edges */
606
+ const directDests = Array.from(directDestinations);
607
+ if (directDests.length === 1) return new Command({
608
+ update: result,
609
+ goto: directDests[0]
610
+ });
611
+ else if (directDests.length > 1)
612
+ /** Multiple direct destinations - they'll run in parallel */
613
+ return new Command({
614
+ update: result,
615
+ goto: directDests
616
+ });
617
+ }
618
+ }
619
+ /** No special routing needed - return state normally */
620
+ return result;
621
+ };
622
+ /** Wrapped agent as a node with its possible destinations */
623
+ builder.addNode(agentId, agentWrapper, { ends: Array.from(allDestinations) });
624
+ }
625
+ for (const startNode of this.startingNodes)
626
+ /** @ts-ignore */
627
+ builder.addEdge(START, startNode);
628
+ /**
629
+ * Add direct edges for automatic transitions
630
+ * Group edges by destination to handle fan-in scenarios
631
+ */
632
+ const edgesByDestination = /* @__PURE__ */ new Map();
633
+ for (const edge of this.directEdges) {
634
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
635
+ for (const destination of destinations) {
636
+ if (!edgesByDestination.has(destination)) edgesByDestination.set(destination, []);
637
+ edgesByDestination.get(destination).push(edge);
638
+ }
639
+ }
640
+ for (const [destination, edges] of edgesByDestination) {
641
+ /** Checks if this is a fan-in scenario with prompt instructions */
642
+ const edgesWithPrompt = edges.filter((edge) => edge.prompt != null && edge.prompt !== "");
643
+ if (edgesWithPrompt.length > 0) {
644
+ /**
645
+ * Single wrapper node for destination (Fan-in with prompt)
646
+ */
647
+ const wrapperNodeId = `fan_in_${destination}_prompt`;
648
+ /**
649
+ * First edge's `prompt`
650
+ * (they should all be the same for fan-in)
651
+ */
652
+ const prompt = edgesWithPrompt[0].prompt;
653
+ /**
654
+ * First edge's `excludeResults` flag
655
+ * (they should all be the same for fan-in)
656
+ */
657
+ const excludeResults = edgesWithPrompt[0].excludeResults;
658
+ builder.addNode(wrapperNodeId, async (state) => {
659
+ let promptText;
660
+ let effectiveExcludeResults = excludeResults;
661
+ if (typeof prompt === "function") promptText = await prompt(state.messages, this.startIndex);
662
+ else if (prompt != null) if (prompt.includes("{results}")) {
663
+ const resultsString = getBufferString(state.messages.slice(this.startIndex));
664
+ promptText = (await PromptTemplate.fromTemplate(prompt).invoke({ results: resultsString })).value;
665
+ effectiveExcludeResults = excludeResults !== false && promptText !== "";
666
+ } else promptText = prompt;
667
+ if (promptText != null && promptText !== "") {
668
+ if (effectiveExcludeResults == null || effectiveExcludeResults === false) return { messages: [new HumanMessage(promptText)] };
669
+ /** When `excludeResults` is true, use agentMessages channel
670
+ * to pass filtered messages + prompt to the destination agent
671
+ */
672
+ const filteredMessages = state.messages.slice(0, this.startIndex);
673
+ const promptMessage = new HumanMessage(promptText);
674
+ return {
675
+ messages: [promptMessage],
676
+ agentMessages: messagesStateReducer(filteredMessages, [promptMessage])
677
+ };
678
+ }
679
+ /** No prompt needed, return empty update */
680
+ return {};
681
+ });
682
+ /** Add edges from all sources to the wrapper, then wrapper to destination */
683
+ for (const edge of edges) {
684
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
685
+ for (const source of sources)
686
+ /** @ts-ignore */
687
+ builder.addEdge(source, wrapperNodeId);
688
+ }
689
+ /** Single edge from wrapper to destination */
690
+ /** @ts-ignore */
691
+ builder.addEdge(wrapperNodeId, destination);
692
+ } else
693
+ /** No prompt instructions, add direct edges (skip if source uses Command routing) */
694
+ for (const edge of edges) {
695
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
696
+ for (const source of sources) {
697
+ /** Check if this source node has both handoff and direct edges */
698
+ const sourceHandoffEdges = this.handoffEdges.filter((e) => {
699
+ return (Array.isArray(e.from) ? e.from : [e.from]).includes(source);
700
+ });
701
+ const sourceDirectEdges = this.directEdges.filter((e) => {
702
+ return (Array.isArray(e.from) ? e.from : [e.from]).includes(source);
703
+ });
704
+ /** Skip adding edge if source uses Command routing (has both types) */
705
+ if (sourceHandoffEdges.length > 0 && sourceDirectEdges.length > 0) continue;
706
+ /** @ts-ignore */
707
+ builder.addEdge(source, destination);
708
+ }
709
+ }
710
+ }
711
+ return builder.compile(this.compileOptions);
712
+ }
713
+ };
714
+ //#endregion
946
715
  export { MultiAgentGraph };
947
- //# sourceMappingURL=MultiAgentGraph.mjs.map
716
+
717
+ //# sourceMappingURL=MultiAgentGraph.mjs.map