@illuma-ai/agents 1.4.0-alpha.6 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (800) hide show
  1. package/README.md +62 -0
  2. package/dist/cjs/agents/AgentContext.cjs +274 -67
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  4. package/dist/cjs/common/enum.cjs +44 -13
  5. package/dist/cjs/common/enum.cjs.map +1 -1
  6. package/dist/cjs/graphs/Graph.cjs +182 -5
  7. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  8. package/dist/cjs/graphs/MultiAgentGraph.cjs +152 -1167
  9. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  10. package/dist/cjs/hooks/HookRegistry.cjs +162 -0
  11. package/dist/cjs/hooks/HookRegistry.cjs.map +1 -0
  12. package/dist/cjs/hooks/executeHooks.cjs +276 -0
  13. package/dist/cjs/hooks/executeHooks.cjs.map +1 -0
  14. package/dist/cjs/hooks/matchers.cjs +256 -0
  15. package/dist/cjs/hooks/matchers.cjs.map +1 -0
  16. package/dist/cjs/hooks/types.cjs +27 -0
  17. package/dist/cjs/hooks/types.cjs.map +1 -0
  18. package/dist/cjs/langchain/google-common.cjs +3 -0
  19. package/dist/cjs/langchain/google-common.cjs.map +1 -0
  20. package/dist/cjs/langchain/index.cjs +86 -0
  21. package/dist/cjs/langchain/index.cjs.map +1 -0
  22. package/dist/cjs/langchain/language_models/chat_models.cjs +3 -0
  23. package/dist/cjs/langchain/language_models/chat_models.cjs.map +1 -0
  24. package/dist/cjs/langchain/messages/tool.cjs +3 -0
  25. package/dist/cjs/langchain/messages/tool.cjs.map +1 -0
  26. package/dist/cjs/langchain/messages.cjs +51 -0
  27. package/dist/cjs/langchain/messages.cjs.map +1 -0
  28. package/dist/cjs/langchain/openai.cjs +3 -0
  29. package/dist/cjs/langchain/openai.cjs.map +1 -0
  30. package/dist/cjs/langchain/prompts.cjs +11 -0
  31. package/dist/cjs/langchain/prompts.cjs.map +1 -0
  32. package/dist/cjs/langchain/runnables.cjs +19 -0
  33. package/dist/cjs/langchain/runnables.cjs.map +1 -0
  34. package/dist/cjs/langchain/tools.cjs +23 -0
  35. package/dist/cjs/langchain/tools.cjs.map +1 -0
  36. package/dist/cjs/langchain/utils/env.cjs +11 -0
  37. package/dist/cjs/langchain/utils/env.cjs.map +1 -0
  38. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +5 -1
  39. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  40. package/dist/cjs/llm/bedrock/cacheSupport.cjs +55 -0
  41. package/dist/cjs/llm/bedrock/cacheSupport.cjs.map +1 -0
  42. package/dist/cjs/llm/bedrock/index.cjs +61 -33
  43. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  44. package/dist/cjs/llm/openai/index.cjs +0 -3
  45. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  46. package/dist/cjs/llm/openai/utils/index.cjs +27 -10
  47. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  48. package/dist/cjs/main.cjs +178 -127
  49. package/dist/cjs/main.cjs.map +1 -1
  50. package/dist/cjs/messages/cache.cjs +89 -0
  51. package/dist/cjs/messages/cache.cjs.map +1 -1
  52. package/dist/cjs/messages/contextPruning.cjs +156 -0
  53. package/dist/cjs/messages/contextPruning.cjs.map +1 -0
  54. package/dist/cjs/messages/contextPruningSettings.cjs +53 -0
  55. package/dist/cjs/messages/contextPruningSettings.cjs.map +1 -0
  56. package/dist/cjs/messages/format.cjs +144 -20
  57. package/dist/cjs/messages/format.cjs.map +1 -1
  58. package/dist/cjs/messages/prune.cjs +505 -4
  59. package/dist/cjs/messages/prune.cjs.map +1 -1
  60. package/dist/cjs/run.cjs +141 -1
  61. package/dist/cjs/run.cjs.map +1 -1
  62. package/dist/cjs/tools/BashExecutor.cjs +235 -0
  63. package/dist/cjs/tools/BashExecutor.cjs.map +1 -0
  64. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +297 -0
  65. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -0
  66. package/dist/cjs/tools/CodeExecutor.cjs +44 -47
  67. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  68. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +16 -11
  69. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  70. package/dist/cjs/tools/ReadFile.cjs +44 -0
  71. package/dist/cjs/tools/ReadFile.cjs.map +1 -0
  72. package/dist/cjs/tools/SkillTool.cjs +51 -0
  73. package/dist/cjs/tools/SkillTool.cjs.map +1 -0
  74. package/dist/cjs/tools/SubagentTool.cjs +93 -0
  75. package/dist/cjs/tools/SubagentTool.cjs.map +1 -0
  76. package/dist/cjs/tools/ToolNode.cjs +450 -24
  77. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  78. package/dist/cjs/tools/search/search.cjs +11 -3
  79. package/dist/cjs/tools/search/search.cjs.map +1 -1
  80. package/dist/cjs/tools/search/tavily-scraper.cjs +189 -0
  81. package/dist/cjs/tools/search/tavily-scraper.cjs.map +1 -0
  82. package/dist/cjs/tools/search/tavily-search.cjs +372 -0
  83. package/dist/cjs/tools/search/tavily-search.cjs.map +1 -0
  84. package/dist/cjs/tools/search/tool.cjs +28 -4
  85. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  86. package/dist/cjs/tools/search/utils.cjs +10 -3
  87. package/dist/cjs/tools/search/utils.cjs.map +1 -1
  88. package/dist/cjs/tools/skillCatalog.cjs +84 -0
  89. package/dist/cjs/tools/skillCatalog.cjs.map +1 -0
  90. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +512 -0
  91. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -0
  92. package/dist/cjs/tools/toolOutputReferences.cjs +670 -0
  93. package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -0
  94. package/dist/cjs/types/agent-cache.cjs +53 -0
  95. package/dist/cjs/types/agent-cache.cjs.map +1 -0
  96. package/dist/cjs/types/graph.cjs.map +1 -1
  97. package/dist/cjs/utils/truncation.cjs +135 -0
  98. package/dist/cjs/utils/truncation.cjs.map +1 -0
  99. package/dist/esm/agents/AgentContext.mjs +274 -67
  100. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  101. package/dist/esm/common/enum.mjs +44 -12
  102. package/dist/esm/common/enum.mjs.map +1 -1
  103. package/dist/esm/graphs/Graph.mjs +182 -5
  104. package/dist/esm/graphs/Graph.mjs.map +1 -1
  105. package/dist/esm/graphs/MultiAgentGraph.mjs +155 -1170
  106. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  107. package/dist/esm/hooks/HookRegistry.mjs +160 -0
  108. package/dist/esm/hooks/HookRegistry.mjs.map +1 -0
  109. package/dist/esm/hooks/executeHooks.mjs +273 -0
  110. package/dist/esm/hooks/executeHooks.mjs.map +1 -0
  111. package/dist/esm/hooks/matchers.mjs +251 -0
  112. package/dist/esm/hooks/matchers.mjs.map +1 -0
  113. package/dist/esm/hooks/types.mjs +25 -0
  114. package/dist/esm/hooks/types.mjs.map +1 -0
  115. package/dist/esm/langchain/google-common.mjs +2 -0
  116. package/dist/esm/langchain/google-common.mjs.map +1 -0
  117. package/dist/esm/langchain/index.mjs +5 -0
  118. package/dist/esm/langchain/language_models/chat_models.mjs +2 -0
  119. package/dist/esm/langchain/language_models/chat_models.mjs.map +1 -0
  120. package/dist/esm/langchain/messages/tool.mjs +2 -0
  121. package/dist/esm/langchain/messages/tool.mjs.map +1 -0
  122. package/dist/esm/langchain/messages.mjs +2 -0
  123. package/dist/esm/langchain/messages.mjs.map +1 -0
  124. package/dist/esm/langchain/openai.mjs +2 -0
  125. package/dist/esm/langchain/openai.mjs.map +1 -0
  126. package/dist/esm/langchain/prompts.mjs +2 -0
  127. package/dist/esm/langchain/prompts.mjs.map +1 -0
  128. package/dist/esm/langchain/runnables.mjs +2 -0
  129. package/dist/esm/langchain/runnables.mjs.map +1 -0
  130. package/dist/esm/langchain/tools.mjs +2 -0
  131. package/dist/esm/langchain/tools.mjs.map +1 -0
  132. package/dist/esm/langchain/utils/env.mjs +2 -0
  133. package/dist/esm/langchain/utils/env.mjs.map +1 -0
  134. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +5 -1
  135. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  136. package/dist/esm/llm/bedrock/cacheSupport.mjs +52 -0
  137. package/dist/esm/llm/bedrock/cacheSupport.mjs.map +1 -0
  138. package/dist/esm/llm/bedrock/index.mjs +61 -34
  139. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  140. package/dist/esm/llm/openai/index.mjs +0 -3
  141. package/dist/esm/llm/openai/index.mjs.map +1 -1
  142. package/dist/esm/llm/openai/utils/index.mjs +27 -10
  143. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  144. package/dist/esm/main.mjs +21 -27
  145. package/dist/esm/main.mjs.map +1 -1
  146. package/dist/esm/messages/cache.mjs +89 -0
  147. package/dist/esm/messages/cache.mjs.map +1 -1
  148. package/dist/esm/messages/contextPruning.mjs +154 -0
  149. package/dist/esm/messages/contextPruning.mjs.map +1 -0
  150. package/dist/esm/messages/contextPruningSettings.mjs +50 -0
  151. package/dist/esm/messages/contextPruningSettings.mjs.map +1 -0
  152. package/dist/esm/messages/format.mjs +136 -12
  153. package/dist/esm/messages/format.mjs.map +1 -1
  154. package/dist/esm/messages/prune.mjs +504 -7
  155. package/dist/esm/messages/prune.mjs.map +1 -1
  156. package/dist/esm/run.mjs +141 -1
  157. package/dist/esm/run.mjs.map +1 -1
  158. package/dist/esm/tools/BashExecutor.mjs +227 -0
  159. package/dist/esm/tools/BashExecutor.mjs.map +1 -0
  160. package/dist/esm/tools/BashProgrammaticToolCalling.mjs +288 -0
  161. package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -0
  162. package/dist/esm/tools/CodeExecutor.mjs +44 -48
  163. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  164. package/dist/esm/tools/ProgrammaticToolCalling.mjs +17 -12
  165. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  166. package/dist/esm/tools/ReadFile.mjs +39 -0
  167. package/dist/esm/tools/ReadFile.mjs.map +1 -0
  168. package/dist/esm/tools/SkillTool.mjs +46 -0
  169. package/dist/esm/tools/SkillTool.mjs.map +1 -0
  170. package/dist/esm/tools/SubagentTool.mjs +86 -0
  171. package/dist/esm/tools/SubagentTool.mjs.map +1 -0
  172. package/dist/esm/tools/ToolNode.mjs +452 -26
  173. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  174. package/dist/esm/tools/search/search.mjs +11 -3
  175. package/dist/esm/tools/search/search.mjs.map +1 -1
  176. package/dist/esm/tools/search/tavily-scraper.mjs +186 -0
  177. package/dist/esm/tools/search/tavily-scraper.mjs.map +1 -0
  178. package/dist/esm/tools/search/tavily-search.mjs +370 -0
  179. package/dist/esm/tools/search/tavily-search.mjs.map +1 -0
  180. package/dist/esm/tools/search/tool.mjs +28 -4
  181. package/dist/esm/tools/search/tool.mjs.map +1 -1
  182. package/dist/esm/tools/search/utils.mjs +10 -3
  183. package/dist/esm/tools/search/utils.mjs.map +1 -1
  184. package/dist/esm/tools/skillCatalog.mjs +82 -0
  185. package/dist/esm/tools/skillCatalog.mjs.map +1 -0
  186. package/dist/esm/tools/subagent/SubagentExecutor.mjs +506 -0
  187. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -0
  188. package/dist/esm/tools/toolOutputReferences.mjs +662 -0
  189. package/dist/esm/tools/toolOutputReferences.mjs.map +1 -0
  190. package/dist/esm/types/agent-cache.mjs +51 -0
  191. package/dist/esm/types/agent-cache.mjs.map +1 -0
  192. package/dist/esm/types/graph.mjs.map +1 -1
  193. package/dist/esm/utils/truncation.mjs +128 -0
  194. package/dist/esm/utils/truncation.mjs.map +1 -0
  195. package/dist/types/agents/AgentContext.d.ts +101 -8
  196. package/dist/types/common/enum.d.ts +39 -12
  197. package/dist/types/common/index.d.ts +0 -1
  198. package/dist/types/graphs/Graph.d.ts +43 -0
  199. package/dist/types/graphs/MultiAgentGraph.d.ts +26 -150
  200. package/dist/types/graphs/index.d.ts +0 -1
  201. package/dist/types/hooks/HookRegistry.d.ts +56 -0
  202. package/dist/types/hooks/executeHooks.d.ts +79 -0
  203. package/dist/types/hooks/index.d.ts +6 -0
  204. package/dist/types/hooks/matchers.d.ts +95 -0
  205. package/dist/types/hooks/types.d.ts +320 -0
  206. package/dist/types/index.d.ts +9 -9
  207. package/dist/types/langchain/google-common.d.ts +1 -0
  208. package/dist/types/langchain/index.d.ts +8 -0
  209. package/dist/types/langchain/language_models/chat_models.d.ts +1 -0
  210. package/dist/types/langchain/messages/tool.d.ts +1 -0
  211. package/dist/types/langchain/messages.d.ts +2 -0
  212. package/dist/types/langchain/openai.d.ts +1 -0
  213. package/dist/types/langchain/prompts.d.ts +1 -0
  214. package/dist/types/langchain/runnables.d.ts +2 -0
  215. package/dist/types/langchain/tools.d.ts +2 -0
  216. package/dist/types/langchain/utils/env.d.ts +1 -0
  217. package/dist/types/llm/bedrock/cacheSupport.d.ts +35 -0
  218. package/dist/types/llm/bedrock/index.d.ts +54 -1
  219. package/dist/types/messages/contextPruning.d.ts +42 -0
  220. package/dist/types/messages/contextPruningSettings.d.ts +44 -0
  221. package/dist/types/messages/format.d.ts +9 -1
  222. package/dist/types/messages/index.d.ts +2 -0
  223. package/dist/types/messages/prune.d.ts +91 -1
  224. package/dist/types/run.d.ts +2 -0
  225. package/dist/types/tools/BashExecutor.d.ts +76 -0
  226. package/dist/types/tools/BashProgrammaticToolCalling.d.ts +72 -0
  227. package/dist/types/tools/CodeExecutor.d.ts +8 -26
  228. package/dist/types/tools/ReadFile.d.ts +28 -0
  229. package/dist/types/tools/SkillTool.d.ts +40 -0
  230. package/dist/types/tools/SubagentTool.d.ts +36 -0
  231. package/dist/types/tools/ToolNode.d.ts +77 -5
  232. package/dist/types/tools/search/tavily-scraper.d.ts +19 -0
  233. package/dist/types/tools/search/tavily-search.d.ts +4 -0
  234. package/dist/types/tools/search/types.d.ts +99 -5
  235. package/dist/types/tools/search/utils.d.ts +2 -2
  236. package/dist/types/tools/skillCatalog.d.ts +19 -0
  237. package/dist/types/tools/subagent/SubagentExecutor.d.ts +137 -0
  238. package/dist/types/tools/subagent/index.d.ts +2 -0
  239. package/dist/types/tools/subagent/types.d.ts +84 -0
  240. package/dist/types/tools/toolOutputReferences.d.ts +236 -0
  241. package/dist/types/types/agent-cache.d.ts +70 -0
  242. package/dist/types/types/graph.d.ts +162 -22
  243. package/dist/types/types/index.d.ts +3 -0
  244. package/dist/types/types/messages.d.ts +26 -0
  245. package/dist/types/types/run.d.ts +22 -0
  246. package/dist/types/types/skill.d.ts +9 -0
  247. package/dist/types/types/tools.d.ts +111 -0
  248. package/dist/types/utils/index.d.ts +1 -3
  249. package/dist/types/utils/truncation.d.ts +70 -0
  250. package/package.json +57 -17
  251. package/src/agents/AgentContext.js.map +1 -0
  252. package/src/agents/AgentContext.test.js.map +1 -0
  253. package/src/agents/AgentContext.ts +321 -78
  254. package/src/agents/__tests__/AgentContext.cacheTtl.live.test.ts +259 -0
  255. package/src/agents/__tests__/AgentContext.crossAgentTier1.live.test.ts +264 -0
  256. package/src/agents/__tests__/AgentContext.crossUserCache.live.test.ts +342 -0
  257. package/src/agents/__tests__/AgentContext.test.js.map +1 -0
  258. package/src/agents/__tests__/AgentContext.test.ts +632 -0
  259. package/src/agents/__tests__/resolveStructuredOutputMode.test.js.map +1 -0
  260. package/src/common/__tests__/enum.test.ts +7 -17
  261. package/src/common/enum.js.map +1 -0
  262. package/src/common/enum.ts +43 -12
  263. package/src/common/index.js.map +1 -0
  264. package/src/common/index.ts +0 -1
  265. package/src/events.js.map +1 -0
  266. package/src/graphs/Graph.js.map +1 -0
  267. package/src/graphs/Graph.ts +222 -2
  268. package/src/graphs/MultiAgentGraph.js.map +1 -0
  269. package/src/graphs/MultiAgentGraph.ts +154 -1466
  270. package/src/graphs/__tests__/MultiAgentGraph.test.ts +91 -0
  271. package/src/graphs/__tests__/structured-output.integration.test.js.map +1 -0
  272. package/src/graphs/__tests__/structured-output.test.js.map +1 -0
  273. package/src/graphs/contextManagement.e2e.test.js.map +1 -0
  274. package/src/graphs/contextManagement.test.js.map +1 -0
  275. package/src/graphs/handoffValidation.test.js.map +1 -0
  276. package/src/graphs/index.js.map +1 -0
  277. package/src/graphs/index.ts +0 -1
  278. package/src/hooks/HookRegistry.ts +208 -0
  279. package/src/hooks/__tests__/HookRegistry.test.ts +190 -0
  280. package/src/hooks/__tests__/compactHooks.test.ts +214 -0
  281. package/src/hooks/__tests__/executeHooks.test.ts +1013 -0
  282. package/src/hooks/__tests__/integration.test.ts +337 -0
  283. package/src/hooks/__tests__/matchers.test.ts +238 -0
  284. package/src/hooks/__tests__/toolHooks.test.ts +665 -0
  285. package/src/hooks/executeHooks.ts +375 -0
  286. package/src/hooks/index.ts +57 -0
  287. package/src/hooks/matchers.ts +280 -0
  288. package/src/hooks/types.ts +404 -0
  289. package/src/index.js.map +1 -0
  290. package/src/index.ts +15 -24
  291. package/src/instrumentation.js.map +1 -0
  292. package/src/langchain/google-common.ts +1 -0
  293. package/src/langchain/index.ts +8 -0
  294. package/src/langchain/language_models/chat_models.ts +1 -0
  295. package/src/langchain/messages/tool.ts +5 -0
  296. package/src/langchain/messages.ts +21 -0
  297. package/src/langchain/openai.ts +1 -0
  298. package/src/langchain/prompts.ts +1 -0
  299. package/src/langchain/runnables.ts +7 -0
  300. package/src/langchain/tools.ts +8 -0
  301. package/src/langchain/utils/env.ts +1 -0
  302. package/src/llm/anthropic/index.js.map +1 -0
  303. package/src/llm/anthropic/types.js.map +1 -0
  304. package/src/llm/anthropic/utils/message_inputs.js.map +1 -0
  305. package/src/llm/anthropic/utils/message_inputs.ts +10 -1
  306. package/src/llm/anthropic/utils/message_outputs.js.map +1 -0
  307. package/src/llm/anthropic/utils/output_parsers.js.map +1 -0
  308. package/src/llm/anthropic/utils/server-tool-inputs.test.ts +436 -0
  309. package/src/llm/anthropic/utils/tools.js.map +1 -0
  310. package/src/llm/bedrock/__tests__/bedrock-caching.test.js.map +1 -0
  311. package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +166 -18
  312. package/src/llm/bedrock/cacheSupport.test.ts +99 -0
  313. package/src/llm/bedrock/cacheSupport.ts +53 -0
  314. package/src/llm/bedrock/index.js.map +1 -0
  315. package/src/llm/bedrock/index.ts +116 -41
  316. package/src/llm/bedrock/types.js.map +1 -0
  317. package/src/llm/bedrock/utils/index.js.map +1 -0
  318. package/src/llm/bedrock/utils/message_inputs.js.map +1 -0
  319. package/src/llm/bedrock/utils/message_outputs.js.map +1 -0
  320. package/src/llm/fake.js.map +1 -0
  321. package/src/llm/google/index.js.map +1 -0
  322. package/src/llm/google/types.js.map +1 -0
  323. package/src/llm/google/utils/common.js.map +1 -0
  324. package/src/llm/google/utils/tools.js.map +1 -0
  325. package/src/llm/google/utils/zod_to_genai_parameters.js.map +1 -0
  326. package/src/llm/openai/index.js.map +1 -0
  327. package/src/llm/openai/types.js.map +1 -0
  328. package/src/llm/openai/utils/index.js.map +1 -0
  329. package/src/llm/openai/utils/index.ts +31 -14
  330. package/src/llm/openai/utils/isReasoningModel.test.js.map +1 -0
  331. package/src/llm/openrouter/index.js.map +1 -0
  332. package/src/llm/openrouter/reasoning.test.js.map +1 -0
  333. package/src/llm/providers.js.map +1 -0
  334. package/src/llm/text.js.map +1 -0
  335. package/src/llm/vertexai/index.js.map +1 -0
  336. package/src/messages/__tests__/contextPruning.test.ts +228 -0
  337. package/src/messages/__tests__/tools.test.js.map +1 -0
  338. package/src/messages/cache.js.map +1 -0
  339. package/src/messages/cache.test.js.map +1 -0
  340. package/src/messages/cache.test.ts +62 -24
  341. package/src/messages/cache.ts +112 -0
  342. package/src/messages/content.js.map +1 -0
  343. package/src/messages/content.test.js.map +1 -0
  344. package/src/messages/contextPruning.ts +191 -0
  345. package/src/messages/contextPruningSettings.ts +90 -0
  346. package/src/messages/core.js.map +1 -0
  347. package/src/messages/ensureThinkingBlock.test.js.map +1 -0
  348. package/src/messages/format.js.map +1 -0
  349. package/src/messages/format.ts +164 -12
  350. package/src/messages/formatAgentMessages.skills.test.ts +413 -0
  351. package/src/messages/formatAgentMessages.test.js.map +1 -0
  352. package/src/messages/formatAgentMessages.tools.test.js.map +1 -0
  353. package/src/messages/formatMessage.test.js.map +1 -0
  354. package/src/messages/ids.js.map +1 -0
  355. package/src/messages/index.js.map +1 -0
  356. package/src/messages/index.ts +2 -0
  357. package/src/messages/labelContentByAgent.test.js.map +1 -0
  358. package/src/messages/prune.js.map +1 -0
  359. package/src/messages/prune.ts +661 -4
  360. package/src/messages/reducer.js.map +1 -0
  361. package/src/messages/shiftIndexTokenCountMap.test.js.map +1 -0
  362. package/src/messages/summarize.js.map +1 -0
  363. package/src/messages/summarize.test.js.map +1 -0
  364. package/src/messages/tools.js.map +1 -0
  365. package/src/mockStream.js.map +1 -0
  366. package/src/prompts/collab.js.map +1 -0
  367. package/src/prompts/index.js.map +1 -0
  368. package/src/prompts/taskmanager.js.map +1 -0
  369. package/src/run.js.map +1 -0
  370. package/src/run.ts +155 -1
  371. package/src/schemas/index.js.map +1 -0
  372. package/src/schemas/schema-preparation.test.js.map +1 -0
  373. package/src/schemas/validate.js.map +1 -0
  374. package/src/schemas/validate.test.js.map +1 -0
  375. package/src/scripts/abort.js.map +1 -0
  376. package/src/scripts/ant_web_search.js.map +1 -0
  377. package/src/scripts/ant_web_search_edge_case.js.map +1 -0
  378. package/src/scripts/ant_web_search_error_edge_case.js.map +1 -0
  379. package/src/scripts/args.js.map +1 -0
  380. package/src/scripts/bedrock-cache-debug.js.map +1 -0
  381. package/src/scripts/bedrock-content-aggregation-test.js.map +1 -0
  382. package/src/scripts/bedrock-merge-test.js.map +1 -0
  383. package/src/scripts/bedrock-parallel-tools-test.js.map +1 -0
  384. package/src/scripts/caching.js.map +1 -0
  385. package/src/scripts/cli.js.map +1 -0
  386. package/src/scripts/cli2.js.map +1 -0
  387. package/src/scripts/cli3.js.map +1 -0
  388. package/src/scripts/cli4.js.map +1 -0
  389. package/src/scripts/cli5.js.map +1 -0
  390. package/src/scripts/code_exec.js.map +1 -0
  391. package/src/scripts/code_exec_files.js.map +1 -0
  392. package/src/scripts/code_exec_multi_session.js.map +1 -0
  393. package/src/scripts/code_exec_ptc.js.map +1 -0
  394. package/src/scripts/code_exec_session.js.map +1 -0
  395. package/src/scripts/code_exec_simple.js.map +1 -0
  396. package/src/scripts/content.js.map +1 -0
  397. package/src/scripts/empty_input.js.map +1 -0
  398. package/src/scripts/handoff-test.js.map +1 -0
  399. package/src/scripts/image.js.map +1 -0
  400. package/src/scripts/memory.js.map +1 -0
  401. package/src/scripts/multi-agent-chain.js.map +1 -0
  402. package/src/scripts/multi-agent-chain.ts +2 -2
  403. package/src/scripts/multi-agent-conditional.js.map +1 -0
  404. package/src/scripts/multi-agent-document-review-chain.js.map +1 -0
  405. package/src/scripts/multi-agent-document-review-chain.ts +2 -2
  406. package/src/scripts/multi-agent-hybrid-flow.js.map +1 -0
  407. package/src/scripts/multi-agent-hybrid-flow.ts +4 -4
  408. package/src/scripts/multi-agent-parallel-start.js.map +1 -0
  409. package/src/scripts/multi-agent-parallel.js.map +1 -0
  410. package/src/scripts/multi-agent-parallel.ts +3 -3
  411. package/src/scripts/multi-agent-sequence.js.map +1 -0
  412. package/src/scripts/multi-agent-sequence.ts +3 -3
  413. package/src/scripts/multi-agent-subagent.ts +246 -0
  414. package/src/scripts/multi-agent-supervisor.js.map +1 -0
  415. package/src/scripts/multi-agent-supervisor.ts +5 -5
  416. package/src/scripts/multi-agent-test.js.map +1 -0
  417. package/src/scripts/parallel-asymmetric-tools-test.js.map +1 -0
  418. package/src/scripts/parallel-full-metadata-test.js.map +1 -0
  419. package/src/scripts/parallel-tools-test.js.map +1 -0
  420. package/src/scripts/poc-multi-agent-comprehensive.ts +8 -8
  421. package/src/scripts/programmatic_exec.js.map +1 -0
  422. package/src/scripts/programmatic_exec_agent.js.map +1 -0
  423. package/src/scripts/search.js.map +1 -0
  424. package/src/scripts/sequential-full-metadata-test.js.map +1 -0
  425. package/src/scripts/sequential-full-metadata-test.ts +2 -2
  426. package/src/scripts/simple.js.map +1 -0
  427. package/src/scripts/single-agent-metadata-test.js.map +1 -0
  428. package/src/scripts/stream.js.map +1 -0
  429. package/src/scripts/subagent-event-driven-debug.ts +190 -0
  430. package/src/scripts/subagent-tools-debug.ts +160 -0
  431. package/src/scripts/test-custom-prompt-key.js.map +1 -0
  432. package/src/scripts/test-custom-prompt-key.ts +3 -3
  433. package/src/scripts/test-handoff-input.js.map +1 -0
  434. package/src/scripts/test-handoff-input.ts +1 -1
  435. package/src/scripts/test-handoff-preamble.js.map +1 -0
  436. package/src/scripts/test-handoff-steering.js.map +1 -0
  437. package/src/scripts/test-handoff-steering.ts +3 -3
  438. package/src/scripts/test-multi-agent-list-handoff.js.map +1 -0
  439. package/src/scripts/test-multi-agent-list-handoff.ts +1 -1
  440. package/src/scripts/test-parallel-agent-labeling.js.map +1 -0
  441. package/src/scripts/test-parallel-agent-labeling.ts +3 -3
  442. package/src/scripts/test-parallel-handoffs.js.map +1 -0
  443. package/src/scripts/test-parallel-handoffs.ts +2 -2
  444. package/src/scripts/test-thinking-handoff-bedrock.js.map +1 -0
  445. package/src/scripts/test-thinking-handoff-bedrock.ts +1 -1
  446. package/src/scripts/test-thinking-handoff.js.map +1 -0
  447. package/src/scripts/test-thinking-handoff.ts +1 -1
  448. package/src/scripts/test-thinking-to-thinking-handoff-bedrock.js.map +1 -0
  449. package/src/scripts/test-thinking-to-thinking-handoff-bedrock.ts +1 -1
  450. package/src/scripts/test-tool-before-handoff-role-order.js.map +1 -0
  451. package/src/scripts/test-tool-before-handoff-role-order.ts +1 -1
  452. package/src/scripts/test-tools-before-handoff.js.map +1 -0
  453. package/src/scripts/test-tools-before-handoff.ts +1 -1
  454. package/src/scripts/test_code_api.js.map +1 -0
  455. package/src/scripts/thinking-bedrock.js.map +1 -0
  456. package/src/scripts/thinking-vertexai.js.map +1 -0
  457. package/src/scripts/thinking.js.map +1 -0
  458. package/src/scripts/tool_search.js.map +1 -0
  459. package/src/scripts/tools.js.map +1 -0
  460. package/src/specs/agent-handoffs-bedrock.integration.test.js.map +1 -0
  461. package/src/specs/agent-handoffs.test.js.map +1 -0
  462. package/src/specs/agent-handoffs.test.ts +26 -483
  463. package/src/specs/anthropic.simple.test.js.map +1 -0
  464. package/src/specs/anthropic.simple.test.ts +61 -0
  465. package/src/specs/azure.simple.test.js.map +1 -0
  466. package/src/specs/cache.simple.test.js.map +1 -0
  467. package/src/specs/custom-event-await.test.js.map +1 -0
  468. package/src/specs/deepseek.simple.test.js.map +1 -0
  469. package/src/specs/emergency-prune.test.js.map +1 -0
  470. package/src/specs/moonshot.simple.test.js.map +1 -0
  471. package/src/specs/multi-agent-summarization.test.ts +396 -0
  472. package/src/specs/observability.integration.test.js.map +1 -0
  473. package/src/specs/openai.simple.test.js.map +1 -0
  474. package/src/specs/openrouter.simple.test.js.map +1 -0
  475. package/src/specs/prune.orphans.test.ts +248 -0
  476. package/src/specs/prune.test.js.map +1 -0
  477. package/src/specs/prune.test.ts +104 -16
  478. package/src/specs/reasoning.test.js.map +1 -0
  479. package/src/specs/spec.utils.js.map +1 -0
  480. package/src/specs/thinking-handoff.test.js.map +1 -0
  481. package/src/specs/thinking-handoff.test.ts +19 -19
  482. package/src/specs/thinking-prune.test.js.map +1 -0
  483. package/src/specs/token-distribution-edge-case.test.js.map +1 -0
  484. package/src/specs/token-memoization.test.js.map +1 -0
  485. package/src/specs/tokens.test.js.map +1 -0
  486. package/src/specs/tool-error.test.js.map +1 -0
  487. package/src/splitStream.js.map +1 -0
  488. package/src/splitStream.test.js.map +1 -0
  489. package/src/stream.js.map +1 -0
  490. package/src/stream.test.js.map +1 -0
  491. package/src/test/mockTools.js.map +1 -0
  492. package/src/tools/BashExecutor.ts +281 -0
  493. package/src/tools/BashProgrammaticToolCalling.ts +397 -0
  494. package/src/tools/BrowserTools.js.map +1 -0
  495. package/src/tools/Calculator.js.map +1 -0
  496. package/src/tools/Calculator.test.js.map +1 -0
  497. package/src/tools/CodeExecutor.js.map +1 -0
  498. package/src/tools/CodeExecutor.ts +62 -54
  499. package/src/tools/ProgrammaticToolCalling.js.map +1 -0
  500. package/src/tools/ProgrammaticToolCalling.ts +29 -14
  501. package/src/tools/ReadFile.ts +39 -0
  502. package/src/tools/SkillTool.ts +46 -0
  503. package/src/tools/StreamingToolCallBuffer.js.map +1 -0
  504. package/src/tools/SubagentTool.ts +100 -0
  505. package/src/tools/ToolNode.js.map +1 -0
  506. package/src/tools/ToolNode.ts +548 -26
  507. package/src/tools/ToolSearch.js.map +1 -0
  508. package/src/tools/__tests__/BashExecutor.test.ts +49 -0
  509. package/src/tools/__tests__/BrowserTools.test.js.map +1 -0
  510. package/src/tools/__tests__/CodeExecutor.test.ts +37 -36
  511. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.js.map +1 -0
  512. package/src/tools/__tests__/ProgrammaticToolCalling.test.js.map +1 -0
  513. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +60 -0
  514. package/src/tools/__tests__/ReadFile.test.ts +44 -0
  515. package/src/tools/__tests__/SkillTool.test.ts +442 -0
  516. package/src/tools/__tests__/StreamingToolCallBuffer.test.js.map +1 -0
  517. package/src/tools/__tests__/SubagentExecutor.test.ts +1148 -0
  518. package/src/tools/__tests__/SubagentTool.test.ts +149 -0
  519. package/src/tools/__tests__/ToolApproval.test.js.map +1 -0
  520. package/src/tools/__tests__/ToolNode.outputReferences.test.ts +1438 -0
  521. package/src/tools/__tests__/ToolNode.recovery.test.js.map +1 -0
  522. package/src/tools/__tests__/ToolNode.session.test.js.map +1 -0
  523. package/src/tools/__tests__/ToolSearch.integration.test.js.map +1 -0
  524. package/src/tools/__tests__/ToolSearch.test.js.map +1 -0
  525. package/src/tools/__tests__/annotateMessagesForLLM.test.ts +479 -0
  526. package/src/tools/__tests__/handlers.test.js.map +1 -0
  527. package/src/tools/__tests__/skillCatalog.test.ts +161 -0
  528. package/src/tools/__tests__/subagentHooks.test.ts +210 -0
  529. package/src/tools/__tests__/toolOutputReferences.test.ts +415 -0
  530. package/src/tools/__tests__/truncation-recovery.integration.test.js.map +1 -0
  531. package/src/tools/handlers.js.map +1 -0
  532. package/src/tools/schema.js.map +1 -0
  533. package/src/tools/search/anthropic.js.map +1 -0
  534. package/src/tools/search/content.js.map +1 -0
  535. package/src/tools/search/content.test.js.map +1 -0
  536. package/src/tools/search/firecrawl.js.map +1 -0
  537. package/src/tools/search/format.js.map +1 -0
  538. package/src/tools/search/highlights.js.map +1 -0
  539. package/src/tools/search/index.js.map +1 -0
  540. package/src/tools/search/jina-reranker.test.js.map +1 -0
  541. package/src/tools/search/rerankers.js.map +1 -0
  542. package/src/tools/search/schema.js.map +1 -0
  543. package/src/tools/search/search.js.map +1 -0
  544. package/src/tools/search/search.ts +12 -2
  545. package/src/tools/search/serper-scraper.js.map +1 -0
  546. package/src/tools/search/tavily-scraper.ts +235 -0
  547. package/src/tools/search/tavily-search.ts +424 -0
  548. package/src/tools/search/tavily.test.ts +965 -0
  549. package/src/tools/search/test.js.map +1 -0
  550. package/src/tools/search/tool.js.map +1 -0
  551. package/src/tools/search/tool.ts +36 -2
  552. package/src/tools/search/types.js.map +1 -0
  553. package/src/tools/search/types.ts +133 -8
  554. package/src/tools/search/utils.js.map +1 -0
  555. package/src/tools/search/utils.ts +13 -5
  556. package/src/tools/skillCatalog.ts +126 -0
  557. package/src/tools/subagent/SubagentExecutor.ts +676 -0
  558. package/src/tools/subagent/index.ts +13 -0
  559. package/src/tools/subagent/types.test.ts +70 -0
  560. package/src/tools/subagent/types.ts +115 -0
  561. package/src/tools/toolOutputReferences.ts +825 -0
  562. package/src/types/agent-cache.ts +73 -0
  563. package/src/types/graph.js.map +1 -0
  564. package/src/types/graph.test.js.map +1 -0
  565. package/src/types/graph.ts +171 -20
  566. package/src/types/index.js.map +1 -0
  567. package/src/types/index.ts +3 -0
  568. package/src/types/llm.js.map +1 -0
  569. package/src/types/messages.js.map +1 -0
  570. package/src/types/messages.ts +27 -0
  571. package/src/types/run.js.map +1 -0
  572. package/src/types/run.ts +22 -0
  573. package/src/types/skill.ts +11 -0
  574. package/src/types/stream.js.map +1 -0
  575. package/src/types/tools.js.map +1 -0
  576. package/src/types/tools.ts +118 -0
  577. package/src/utils/__tests__/truncation.test.ts +66 -0
  578. package/src/utils/contextAnalytics.js.map +1 -0
  579. package/src/utils/contextAnalytics.test.js.map +1 -0
  580. package/src/utils/events.js.map +1 -0
  581. package/src/utils/graph.js.map +1 -0
  582. package/src/utils/handlers.js.map +1 -0
  583. package/src/utils/index.js.map +1 -0
  584. package/src/utils/index.ts +1 -3
  585. package/src/utils/llm.js.map +1 -0
  586. package/src/utils/llmConfig.js.map +1 -0
  587. package/src/utils/logging.js.map +1 -0
  588. package/src/utils/misc.js.map +1 -0
  589. package/src/utils/run.js.map +1 -0
  590. package/src/utils/schema.js.map +1 -0
  591. package/src/utils/title.js.map +1 -0
  592. package/src/utils/tokens.js.map +1 -0
  593. package/src/utils/toonFormat.js.map +1 -0
  594. package/src/utils/truncation.ts +154 -0
  595. package/dist/cjs/common/spawnPath.cjs +0 -104
  596. package/dist/cjs/common/spawnPath.cjs.map +0 -1
  597. package/dist/cjs/content/ArtifactStore.cjs +0 -579
  598. package/dist/cjs/content/ArtifactStore.cjs.map +0 -1
  599. package/dist/cjs/content/ContentStore.cjs +0 -638
  600. package/dist/cjs/content/ContentStore.cjs.map +0 -1
  601. package/dist/cjs/content/contentAnalyzer.cjs +0 -91
  602. package/dist/cjs/content/contentAnalyzer.cjs.map +0 -1
  603. package/dist/cjs/content/index.cjs +0 -20
  604. package/dist/cjs/content/index.cjs.map +0 -1
  605. package/dist/cjs/content/mcpAutoCache.cjs +0 -115
  606. package/dist/cjs/content/mcpAutoCache.cjs.map +0 -1
  607. package/dist/cjs/graphs/HandoffRegistry.cjs +0 -143
  608. package/dist/cjs/graphs/HandoffRegistry.cjs.map +0 -1
  609. package/dist/cjs/providers/a2a/A2ACapabilityProvider.cjs +0 -288
  610. package/dist/cjs/providers/a2a/A2ACapabilityProvider.cjs.map +0 -1
  611. package/dist/cjs/providers/a2a/client.cjs +0 -92
  612. package/dist/cjs/providers/a2a/client.cjs.map +0 -1
  613. package/dist/cjs/providers/a2a/config.cjs +0 -38
  614. package/dist/cjs/providers/a2a/config.cjs.map +0 -1
  615. package/dist/cjs/providers/capabilityNaming.cjs +0 -43
  616. package/dist/cjs/providers/capabilityNaming.cjs.map +0 -1
  617. package/dist/cjs/providers/mcp/MCPCapabilityProvider.cjs +0 -244
  618. package/dist/cjs/providers/mcp/MCPCapabilityProvider.cjs.map +0 -1
  619. package/dist/cjs/providers/mcp/config.cjs +0 -42
  620. package/dist/cjs/providers/mcp/config.cjs.map +0 -1
  621. package/dist/cjs/providers/mcp/transport.cjs +0 -65
  622. package/dist/cjs/providers/mcp/transport.cjs.map +0 -1
  623. package/dist/cjs/providers/tools-server/ToolsServerCapabilityProvider.cjs +0 -128
  624. package/dist/cjs/providers/tools-server/ToolsServerCapabilityProvider.cjs.map +0 -1
  625. package/dist/cjs/providers/types.cjs +0 -51
  626. package/dist/cjs/providers/types.cjs.map +0 -1
  627. package/dist/cjs/tools/artifacts/schema.cjs +0 -86
  628. package/dist/cjs/tools/artifacts/schema.cjs.map +0 -1
  629. package/dist/cjs/tools/artifacts/tool.cjs +0 -219
  630. package/dist/cjs/tools/artifacts/tool.cjs.map +0 -1
  631. package/dist/cjs/tools/fileSearch/formatter.cjs +0 -93
  632. package/dist/cjs/tools/fileSearch/formatter.cjs.map +0 -1
  633. package/dist/cjs/tools/fileSearch/ragClient.cjs +0 -102
  634. package/dist/cjs/tools/fileSearch/ragClient.cjs.map +0 -1
  635. package/dist/cjs/tools/fileSearch/schema.cjs +0 -18
  636. package/dist/cjs/tools/fileSearch/schema.cjs.map +0 -1
  637. package/dist/cjs/tools/fileSearch/tool.cjs +0 -155
  638. package/dist/cjs/tools/fileSearch/tool.cjs.map +0 -1
  639. package/dist/cjs/tools/proxyTool.cjs +0 -102
  640. package/dist/cjs/tools/proxyTool.cjs.map +0 -1
  641. package/dist/cjs/utils/childAgentContext.cjs +0 -242
  642. package/dist/cjs/utils/childAgentContext.cjs.map +0 -1
  643. package/dist/cjs/utils/credentials.cjs +0 -142
  644. package/dist/cjs/utils/credentials.cjs.map +0 -1
  645. package/dist/cjs/utils/httpClient.cjs +0 -74
  646. package/dist/cjs/utils/httpClient.cjs.map +0 -1
  647. package/dist/cjs/utils/toolManifest.cjs +0 -100
  648. package/dist/cjs/utils/toolManifest.cjs.map +0 -1
  649. package/dist/esm/common/spawnPath.mjs +0 -95
  650. package/dist/esm/common/spawnPath.mjs.map +0 -1
  651. package/dist/esm/content/ArtifactStore.mjs +0 -576
  652. package/dist/esm/content/ArtifactStore.mjs.map +0 -1
  653. package/dist/esm/content/ContentStore.mjs +0 -635
  654. package/dist/esm/content/ContentStore.mjs.map +0 -1
  655. package/dist/esm/content/contentAnalyzer.mjs +0 -87
  656. package/dist/esm/content/contentAnalyzer.mjs.map +0 -1
  657. package/dist/esm/content/index.mjs +0 -5
  658. package/dist/esm/content/mcpAutoCache.mjs +0 -111
  659. package/dist/esm/content/mcpAutoCache.mjs.map +0 -1
  660. package/dist/esm/graphs/HandoffRegistry.mjs +0 -141
  661. package/dist/esm/graphs/HandoffRegistry.mjs.map +0 -1
  662. package/dist/esm/providers/a2a/A2ACapabilityProvider.mjs +0 -281
  663. package/dist/esm/providers/a2a/A2ACapabilityProvider.mjs.map +0 -1
  664. package/dist/esm/providers/a2a/client.mjs +0 -88
  665. package/dist/esm/providers/a2a/client.mjs.map +0 -1
  666. package/dist/esm/providers/a2a/config.mjs +0 -35
  667. package/dist/esm/providers/a2a/config.mjs.map +0 -1
  668. package/dist/esm/providers/capabilityNaming.mjs +0 -39
  669. package/dist/esm/providers/capabilityNaming.mjs.map +0 -1
  670. package/dist/esm/providers/mcp/MCPCapabilityProvider.mjs +0 -240
  671. package/dist/esm/providers/mcp/MCPCapabilityProvider.mjs.map +0 -1
  672. package/dist/esm/providers/mcp/config.mjs +0 -39
  673. package/dist/esm/providers/mcp/config.mjs.map +0 -1
  674. package/dist/esm/providers/mcp/transport.mjs +0 -63
  675. package/dist/esm/providers/mcp/transport.mjs.map +0 -1
  676. package/dist/esm/providers/tools-server/ToolsServerCapabilityProvider.mjs +0 -126
  677. package/dist/esm/providers/tools-server/ToolsServerCapabilityProvider.mjs.map +0 -1
  678. package/dist/esm/providers/types.mjs +0 -51
  679. package/dist/esm/providers/types.mjs.map +0 -1
  680. package/dist/esm/tools/artifacts/schema.mjs +0 -79
  681. package/dist/esm/tools/artifacts/schema.mjs.map +0 -1
  682. package/dist/esm/tools/artifacts/tool.mjs +0 -213
  683. package/dist/esm/tools/artifacts/tool.mjs.map +0 -1
  684. package/dist/esm/tools/fileSearch/formatter.mjs +0 -90
  685. package/dist/esm/tools/fileSearch/formatter.mjs.map +0 -1
  686. package/dist/esm/tools/fileSearch/ragClient.mjs +0 -98
  687. package/dist/esm/tools/fileSearch/ragClient.mjs.map +0 -1
  688. package/dist/esm/tools/fileSearch/schema.mjs +0 -15
  689. package/dist/esm/tools/fileSearch/schema.mjs.map +0 -1
  690. package/dist/esm/tools/fileSearch/tool.mjs +0 -152
  691. package/dist/esm/tools/fileSearch/tool.mjs.map +0 -1
  692. package/dist/esm/tools/proxyTool.mjs +0 -100
  693. package/dist/esm/tools/proxyTool.mjs.map +0 -1
  694. package/dist/esm/utils/childAgentContext.mjs +0 -237
  695. package/dist/esm/utils/childAgentContext.mjs.map +0 -1
  696. package/dist/esm/utils/credentials.mjs +0 -135
  697. package/dist/esm/utils/credentials.mjs.map +0 -1
  698. package/dist/esm/utils/httpClient.mjs +0 -70
  699. package/dist/esm/utils/httpClient.mjs.map +0 -1
  700. package/dist/esm/utils/toolManifest.mjs +0 -96
  701. package/dist/esm/utils/toolManifest.mjs.map +0 -1
  702. package/dist/types/common/spawnPath.d.ts +0 -59
  703. package/dist/types/content/ArtifactStore.d.ts +0 -223
  704. package/dist/types/content/ContentStore.d.ts +0 -140
  705. package/dist/types/content/contentAnalyzer.d.ts +0 -38
  706. package/dist/types/content/index.d.ts +0 -24
  707. package/dist/types/content/mcpAutoCache.d.ts +0 -89
  708. package/dist/types/content/types.d.ts +0 -75
  709. package/dist/types/graphs/HandoffRegistry.d.ts +0 -97
  710. package/dist/types/providers/a2a/A2ACapabilityProvider.d.ts +0 -89
  711. package/dist/types/providers/a2a/client.d.ts +0 -47
  712. package/dist/types/providers/a2a/config.d.ts +0 -18
  713. package/dist/types/providers/a2a/index.d.ts +0 -6
  714. package/dist/types/providers/a2a/types.d.ts +0 -173
  715. package/dist/types/providers/capabilityNaming.d.ts +0 -25
  716. package/dist/types/providers/index.d.ts +0 -12
  717. package/dist/types/providers/mcp/MCPCapabilityProvider.d.ts +0 -54
  718. package/dist/types/providers/mcp/config.d.ts +0 -20
  719. package/dist/types/providers/mcp/index.d.ts +0 -5
  720. package/dist/types/providers/mcp/transport.d.ts +0 -18
  721. package/dist/types/providers/mcp/types.d.ts +0 -112
  722. package/dist/types/providers/tools-server/ToolsServerCapabilityProvider.d.ts +0 -59
  723. package/dist/types/providers/tools-server/index.d.ts +0 -1
  724. package/dist/types/providers/types.d.ts +0 -184
  725. package/dist/types/tools/artifacts/index.d.ts +0 -3
  726. package/dist/types/tools/artifacts/schema.d.ts +0 -63
  727. package/dist/types/tools/artifacts/tool.d.ts +0 -16
  728. package/dist/types/tools/artifacts/types.d.ts +0 -127
  729. package/dist/types/tools/fileSearch/formatter.d.ts +0 -25
  730. package/dist/types/tools/fileSearch/index.d.ts +0 -5
  731. package/dist/types/tools/fileSearch/ragClient.d.ts +0 -32
  732. package/dist/types/tools/fileSearch/schema.d.ts +0 -13
  733. package/dist/types/tools/fileSearch/tool.d.ts +0 -18
  734. package/dist/types/tools/fileSearch/types.d.ts +0 -139
  735. package/dist/types/tools/proxyTool.d.ts +0 -62
  736. package/dist/types/utils/childAgentContext.d.ts +0 -99
  737. package/dist/types/utils/credentials.d.ts +0 -77
  738. package/dist/types/utils/httpClient.d.ts +0 -46
  739. package/dist/types/utils/toolManifest.d.ts +0 -49
  740. package/src/common/__tests__/spawnPath.test.ts +0 -110
  741. package/src/common/spawnPath.ts +0 -101
  742. package/src/content/ArtifactStore.ts +0 -782
  743. package/src/content/ContentStore.ts +0 -753
  744. package/src/content/contentAnalyzer.ts +0 -105
  745. package/src/content/index.ts +0 -51
  746. package/src/content/mcpAutoCache.ts +0 -185
  747. package/src/content/types.ts +0 -82
  748. package/src/graphs/HandoffRegistry.ts +0 -199
  749. package/src/graphs/__tests__/HandoffRegistry.test.ts +0 -410
  750. package/src/graphs/__tests__/multi-agent-delegate.test.ts +0 -458
  751. package/src/graphs/__tests__/multi-agent-edges.test.ts +0 -276
  752. package/src/graphs/__tests__/multi-agent-nested-subgraph.test.ts +0 -221
  753. package/src/graphs/handoffValidation.test.ts +0 -353
  754. package/src/providers/__tests__/ToolsServerCapabilityProvider.integration.spec.ts +0 -79
  755. package/src/providers/__tests__/ToolsServerCapabilityProvider.test.ts +0 -271
  756. package/src/providers/__tests__/types.test.ts +0 -64
  757. package/src/providers/a2a/A2ACapabilityProvider.ts +0 -384
  758. package/src/providers/a2a/__tests__/A2ACapabilityProvider.integration.spec.ts +0 -345
  759. package/src/providers/a2a/__tests__/A2ACapabilityProvider.test.ts +0 -460
  760. package/src/providers/a2a/client.ts +0 -115
  761. package/src/providers/a2a/config.ts +0 -40
  762. package/src/providers/a2a/index.ts +0 -29
  763. package/src/providers/a2a/types.ts +0 -191
  764. package/src/providers/capabilityNaming.ts +0 -42
  765. package/src/providers/index.ts +0 -68
  766. package/src/providers/mcp/MCPCapabilityProvider.ts +0 -345
  767. package/src/providers/mcp/__tests__/MCPCapabilityProvider.integration.spec.ts +0 -386
  768. package/src/providers/mcp/__tests__/MCPCapabilityProvider.test.ts +0 -371
  769. package/src/providers/mcp/config.ts +0 -45
  770. package/src/providers/mcp/index.ts +0 -21
  771. package/src/providers/mcp/transport.ts +0 -76
  772. package/src/providers/mcp/types.ts +0 -139
  773. package/src/providers/tools-server/ToolsServerCapabilityProvider.ts +0 -249
  774. package/src/providers/tools-server/index.ts +0 -1
  775. package/src/providers/types.ts +0 -204
  776. package/src/scripts/test-bedrock-handoff-autonomous.ts +0 -267
  777. package/src/scripts/test-handoff-preamble.ts +0 -278
  778. package/src/specs/agent-handoffs-bedrock.integration.test.ts +0 -415
  779. package/src/tools/artifacts/__tests__/tool.test.ts +0 -259
  780. package/src/tools/artifacts/index.ts +0 -33
  781. package/src/tools/artifacts/schema.ts +0 -99
  782. package/src/tools/artifacts/tool.ts +0 -289
  783. package/src/tools/artifacts/types.ts +0 -162
  784. package/src/tools/fileSearch/__tests__/tool.test.ts +0 -261
  785. package/src/tools/fileSearch/formatter.ts +0 -129
  786. package/src/tools/fileSearch/index.ts +0 -23
  787. package/src/tools/fileSearch/ragClient.ts +0 -137
  788. package/src/tools/fileSearch/schema.ts +0 -19
  789. package/src/tools/fileSearch/tool.ts +0 -207
  790. package/src/tools/fileSearch/types.ts +0 -149
  791. package/src/tools/proxyTool.ts +0 -166
  792. package/src/utils/__tests__/childAgentContext.test.ts +0 -217
  793. package/src/utils/__tests__/credentials.test.ts +0 -130
  794. package/src/utils/__tests__/httpClient.test.ts +0 -75
  795. package/src/utils/__tests__/toolManifest.test.ts +0 -116
  796. package/src/utils/childAgentContext.ts +0 -259
  797. package/src/utils/credentials.ts +0 -157
  798. package/src/utils/httpClient.ts +0 -92
  799. package/src/utils/toolManifest.ts +0 -109
  800. /package/dist/esm/{content → langchain}/index.mjs.map +0 -0
@@ -1,52 +1,33 @@
1
1
  import { tool } from '@langchain/core/tools';
2
2
  import { PromptTemplate } from '@langchain/core/prompts';
3
- import { ToolMessage, AIMessage, HumanMessage, getBufferString, SystemMessage } from '@langchain/core/messages';
4
- import { getCurrentTaskInput, Command, Annotation, messagesStateReducer, StateGraph, START, END } from '@langchain/langgraph';
5
- import '../messages/core.mjs';
6
- import 'nanoid';
7
- import { EdgeType, Constants, DEFAULT_HANDOFF_MAX_RESULT_CHARS, GraphEvents } from '../common/enum.mjs';
8
- import { buildSpawnPath, spawnPathDepth } from '../common/spawnPath.mjs';
9
- import '../tools/approval/constants.mjs';
10
- import '../utils/toonFormat.mjs';
11
- import { summarize, createEmergencySummary } from '../messages/summarize.mjs';
3
+ import { ToolMessage, AIMessage, getBufferString, HumanMessage } from '@langchain/core/messages';
4
+ import { getCurrentTaskInput, Command, Annotation, messagesStateReducer, StateGraph, END, START } from '@langchain/langgraph';
12
5
  import { StandardGraph } from './Graph.mjs';
13
- import { safeDispatchCustomEvent } from '../utils/events.mjs';
14
- import { mlog, mwarn } from '../utils/logging.mjs';
15
- import { prepareHandoffMessages, prepareIsolatedChildMessages } from '../utils/childAgentContext.mjs';
6
+ import { EdgeType, Constants } from '../common/enum.mjs';
7
+ import '../tools/approval/constants.mjs';
16
8
  import { getApprovalGateNodeId, createApprovalGateNode } from '../nodes/ApprovalGateNode.mjs';
17
9
 
18
- // HandoffRegistry no longer needed — handoff tools use synchronous
19
- // browser-tool callback pattern (spawn → wait → return result)
20
10
  /** Pattern to extract instructions from transfer ToolMessage content */
21
- const TRANSFER_INSTRUCTIONS_PATTERN = /(?:Instructions?|Context):\s*(.+)/is;
11
+ const HANDOFF_INSTRUCTIONS_PATTERN = /(?:Instructions?|Context):\s*(.+)/is;
22
12
  /**
23
13
  * MultiAgentGraph extends StandardGraph to support dynamic multi-agent workflows
24
14
  * with handoffs, fan-in/fan-out, and other composable patterns.
25
15
  *
26
16
  * Key behavior:
27
- * - Agents with ONLY transfer edges: Can dynamically route to any transfer destination
28
- * - Agents with ONLY sequence edges: Always follow their sequence edges
29
- * - Agents with BOTH: Use Command for exclusive routing (transfer OR sequence, not both)
30
- * - If transfer occurs: Only the transfer destination executes
31
- * - If no transfer: Sequence edges execute (potentially in parallel)
17
+ * - Agents with ONLY handoff edges: Can dynamically route to any handoff destination
18
+ * - Agents with ONLY direct edges: Always follow their direct edges
19
+ * - Agents with BOTH: Use Command for exclusive routing (handoff OR direct, not both)
20
+ * - If handoff occurs: Only the handoff destination executes
21
+ * - If no handoff: Direct edges execute (potentially in parallel)
32
22
  *
33
- * This enables the common pattern where an agent either transfers (one-way)
34
- * OR continues its workflow (sequence edges), but not both simultaneously.
23
+ * This enables the common pattern where an agent either delegates (handoff)
24
+ * OR continues its workflow (direct edges), but not both simultaneously.
35
25
  */
36
26
  class MultiAgentGraph extends StandardGraph {
37
27
  edges;
38
28
  startingNodes = new Set();
39
- sequenceEdges = [];
40
- transferEdges = [];
29
+ directEdges = [];
41
30
  handoffEdges = [];
42
- /**
43
- * Lazily populated registry of compiled subgraphs, keyed by agentId.
44
- * Handoff tools are created in the constructor but reference subgraphs
45
- * that are only created in createWorkflow(). This Map bridges that gap —
46
- * tools capture the Map reference in their closure, and createWorkflow()
47
- * populates it before any tool invocation occurs.
48
- */
49
- subgraphRegistry = new Map();
50
31
  /**
51
32
  * Map of agentId to parallel group info.
52
33
  * Contains groupId (incrementing number reflecting execution order) for agents in parallel groups.
@@ -58,63 +39,76 @@ class MultiAgentGraph extends StandardGraph {
58
39
  * - summarizer: undefined (sequential, order 2)
59
40
  */
60
41
  agentParallelGroups = new Map();
61
- /**
62
- * Tracks the ID of the last agent that produced output.
63
- * Used by auto-continuation to know which agent's context to preserve after handoff.
64
- */
65
- lastActiveAgentId;
66
- /**
67
- * Registry for async handoff execution.
68
- * Enables autonomous orchestration: spawn children non-blocking,
69
- * orchestrator stays alive to reason and collect results when ready.
70
- */
71
- // HandoffRegistry removed — handoff tools are synchronous (callback pattern)
72
- /**
73
- * When set, the graph routes START to this agent instead of the default starting nodes.
74
- * Enables multi-turn resumption: follow-up messages go to the agent that last handled
75
- * the conversation rather than restarting from the root/router agent.
76
- */
77
- resumeFromAgentId;
78
42
  constructor(input) {
79
43
  super(input);
80
44
  this.edges = input.edges;
81
- this.resumeFromAgentId = input.resumeFromAgentId;
45
+ this.validateEdgeAgents();
82
46
  this.categorizeEdges();
83
47
  this.analyzeGraph();
84
- this.createTransferTools();
85
48
  this.createHandoffTools();
86
- mlog(`[MultiAgentGraph] Constructor complete: ${this.agentContexts.size} agents, ${this.edges.length} edges`);
87
49
  }
88
50
  /**
89
- * Categorize edges into handoff, transfer, and sequence types
51
+ * Fails fast when an edge references an agent that is not in
52
+ * `agentContexts`. Without this check, the underlying LangGraph
53
+ * `StateGraph.compile()` would throw the opaque
54
+ * `Found edge ending at unknown node "<id>"` error after graph
55
+ * construction — far from the true root cause.
56
+ *
57
+ * This catches the common misuse of passing `edges` into a multi-agent
58
+ * config without also passing the corresponding sub-agent configs in
59
+ * `agents` (e.g. a host that forgot to pre-load handoff targets).
90
60
  */
91
- categorizeEdges() {
61
+ validateEdgeAgents() {
62
+ const known = new Set(this.agentContexts.keys());
63
+ const unknown = new Set();
92
64
  for (const edge of this.edges) {
93
- if (edge.edgeType === EdgeType.HANDOFF) {
94
- this.handoffEdges.push(edge);
65
+ const participants = [
66
+ ...(Array.isArray(edge.from) ? edge.from : [edge.from]),
67
+ ...(Array.isArray(edge.to) ? edge.to : [edge.to]),
68
+ ];
69
+ for (const id of participants) {
70
+ if (typeof id === 'string' && !known.has(id)) {
71
+ unknown.add(id);
72
+ }
95
73
  }
96
- else if (edge.edgeType === EdgeType.SEQUENCE) {
97
- this.sequenceEdges.push(edge);
74
+ }
75
+ if (unknown.size === 0) {
76
+ return;
77
+ }
78
+ const missing = Array.from(unknown)
79
+ .map((id) => `"${id}"`)
80
+ .join(', ');
81
+ throw new Error(`MultiAgentGraph: edges reference agent(s) not present in agents: [${missing}]. ` +
82
+ 'Ensure every agent referenced by an edge is also included in the `agents` array, ' +
83
+ 'or filter orphaned edges before constructing the graph.');
84
+ }
85
+ /**
86
+ * Categorize edges into handoff and direct types
87
+ */
88
+ categorizeEdges() {
89
+ for (const edge of this.edges) {
90
+ // Default behavior: edges with conditions or explicit 'handoff' type are handoff edges
91
+ // Edges with explicit 'direct' type or multi-destination without conditions are direct edges
92
+ if (edge.edgeType === EdgeType.DIRECT) {
93
+ this.directEdges.push(edge);
98
94
  }
99
- else if (edge.edgeType === EdgeType.TRANSFER ||
100
- edge.condition != null) {
101
- this.transferEdges.push(edge);
95
+ else if (edge.edgeType === EdgeType.HANDOFF || edge.condition != null) {
96
+ this.handoffEdges.push(edge);
102
97
  }
103
98
  else {
104
- // Default: single-to-single edges are transfer, single-to-multiple are sequence
99
+ // Default: single-to-single edges are handoff, single-to-multiple are direct
105
100
  const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
106
101
  const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
107
102
  if (sources.length === 1 && destinations.length > 1) {
108
- // Fan-out pattern defaults to sequence
109
- this.sequenceEdges.push(edge);
103
+ // Fan-out pattern defaults to direct
104
+ this.directEdges.push(edge);
110
105
  }
111
106
  else {
112
- // Everything else defaults to transfer
113
- this.transferEdges.push(edge);
107
+ // Everything else defaults to handoff
108
+ this.handoffEdges.push(edge);
114
109
  }
115
110
  }
116
111
  }
117
- mlog(`[MultiAgentGraph] Edge categorization: ${this.handoffEdges.length} handoff, ${this.transferEdges.length} transfer, ${this.sequenceEdges.length} sequence (of ${this.edges.length} total)`);
118
112
  }
119
113
  /**
120
114
  * Analyze graph structure to determine starting nodes and connections
@@ -136,7 +130,6 @@ class MultiAgentGraph extends StandardGraph {
136
130
  if (this.startingNodes.size === 0 && this.agentContexts.size > 0) {
137
131
  this.startingNodes.add(this.agentContexts.keys().next().value);
138
132
  }
139
- mlog(`[MultiAgentGraph] Starting nodes identified: [${Array.from(this.startingNodes).join(', ')}]`);
140
133
  // Determine if graph has parallel execution capability
141
134
  this.computeParallelCapability();
142
135
  }
@@ -172,8 +165,8 @@ class MultiAgentGraph extends StandardGraph {
172
165
  if (visited.has(current))
173
166
  continue;
174
167
  visited.add(current);
175
- // Find sequence edges from this node
176
- for (const edge of this.sequenceEdges) {
168
+ // Find direct edges from this node
169
+ for (const edge of this.directEdges) {
177
170
  const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
178
171
  if (!sources.includes(current))
179
172
  continue;
@@ -200,8 +193,8 @@ class MultiAgentGraph extends StandardGraph {
200
193
  }
201
194
  }
202
195
  }
203
- // Also follow transfer and handoff edges for traversal (they don't create parallel groups)
204
- for (const edge of [...this.transferEdges, ...this.handoffEdges]) {
196
+ // Also follow handoff edges for traversal (but they don't create parallel groups)
197
+ for (const edge of this.handoffEdges) {
205
198
  const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
206
199
  if (!sources.includes(current))
207
200
  continue;
@@ -222,14 +215,6 @@ class MultiAgentGraph extends StandardGraph {
222
215
  getParallelGroupId(agentId) {
223
216
  return this.agentParallelGroups.get(agentId);
224
217
  }
225
- /**
226
- * Returns the ID of the last agent that produced output.
227
- * Used by auto-continuation to determine which agent's context to preserve
228
- * when a response is truncated after an agent handoff.
229
- */
230
- getLastActiveAgentId() {
231
- return this.lastActiveAgentId;
232
- }
233
218
  /**
234
219
  * Override to indicate this is a multi-agent graph.
235
220
  * Enables agentId to be included in RunStep for frontend agent labeling.
@@ -244,59 +229,45 @@ class MultiAgentGraph extends StandardGraph {
244
229
  return this.agentParallelGroups.get(agentId);
245
230
  }
246
231
  /**
247
- * Create transfer tools for agents based on transfer edges only.
248
- * Transfer tools return Command for one-way routing — parent exits, child takes over.
232
+ * Create handoff tools for agents based on handoff edges only
249
233
  */
250
- createTransferTools() {
251
- const transfersByAgent = new Map();
252
- for (const edge of this.transferEdges) {
234
+ createHandoffTools() {
235
+ // Group handoff edges by source agent(s)
236
+ const handoffsByAgent = new Map();
237
+ // Only process handoff edges for tool creation
238
+ for (const edge of this.handoffEdges) {
253
239
  const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
254
240
  sources.forEach((source) => {
255
- if (!transfersByAgent.has(source)) {
256
- transfersByAgent.set(source, []);
241
+ if (!handoffsByAgent.has(source)) {
242
+ handoffsByAgent.set(source, []);
257
243
  }
258
- transfersByAgent.get(source).push(edge);
244
+ handoffsByAgent.get(source).push(edge);
259
245
  });
260
246
  }
261
- for (const [agentId, edges] of transfersByAgent) {
247
+ // Create handoff tools for each agent
248
+ for (const [agentId, edges] of handoffsByAgent) {
262
249
  const agentContext = this.agentContexts.get(agentId);
263
250
  if (!agentContext)
264
251
  continue;
265
- const transferTools = [];
252
+ // Create handoff tools for this agent's outgoing edges
253
+ const handoffTools = [];
266
254
  const sourceAgentName = agentContext.name ?? agentId;
267
255
  for (const edge of edges) {
268
- transferTools.push(...this.createTransferToolsForEdge(edge, agentId, sourceAgentName));
256
+ handoffTools.push(...this.createHandoffToolsForEdge(edge, agentId, sourceAgentName));
269
257
  }
270
258
  if (!agentContext.graphTools) {
271
259
  agentContext.graphTools = [];
272
260
  }
273
- agentContext.graphTools.push(...transferTools);
274
- mlog(`[MultiAgentGraph] Transfer tools for "${agentId}": [${transferTools.map((t) => t.name).join(', ')}]`);
275
- // Inject orchestration guidance for agents with transfer tools
276
- const childDescs = edges.flatMap((e) => {
277
- const dests = Array.isArray(e.to) ? e.to : [e.to];
278
- return dests.map((d) => {
279
- const ctx = this.agentContexts.get(d);
280
- const name = ctx?.name ?? d;
281
- const desc = ctx?.description ? ` — ${ctx.description}` : '';
282
- return `${name}${desc}`;
283
- });
284
- });
285
- const guidance = this.buildOrchestratorGuidance(childDescs, transferTools.length);
286
- const existing = agentContext.additionalInstructions ?? '';
287
- agentContext.additionalInstructions = existing
288
- ? `${existing}\n\n${guidance}`
289
- : guidance;
261
+ agentContext.graphTools.push(...handoffTools);
290
262
  }
291
263
  }
292
264
  /**
293
- * Create transfer tools for an edge (handles multiple destinations).
294
- * Transfer tools return Command for one-way routing parent exits, child takes over.
295
- * @param edge - The graph edge defining the transfer
296
- * @param sourceAgentId - The ID of the agent that will perform the transfer
265
+ * Create handoff tools for an edge (handles multiple destinations)
266
+ * @param edge - The graph edge defining the handoff
267
+ * @param sourceAgentId - The ID of the agent that will perform the handoff
297
268
  * @param sourceAgentName - The human-readable name of the source agent
298
269
  */
299
- createTransferToolsForEdge(edge, sourceAgentId, sourceAgentName) {
270
+ createHandoffToolsForEdge(edge, sourceAgentId, sourceAgentName) {
300
271
  const tools = [];
301
272
  const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
302
273
  /** If there's a condition, create a single conditional handoff tool */
@@ -304,10 +275,8 @@ class MultiAgentGraph extends StandardGraph {
304
275
  const toolName = 'conditional_transfer';
305
276
  const toolDescription = edge.description ?? 'Conditionally transfer control based on state';
306
277
  /** Check if we have a prompt for handoff input */
307
- const hasTransferInput = edge.prompt != null && typeof edge.prompt === 'string';
308
- const transferInputDescription = hasTransferInput
309
- ? edge.prompt
310
- : undefined;
278
+ const hasHandoffInput = edge.prompt != null && typeof edge.prompt === 'string';
279
+ const handoffInputDescription = hasHandoffInput ? edge.prompt : undefined;
311
280
  const promptKey = edge.promptKey ?? 'instructions';
312
281
  tools.push(tool(async (rawInput, config) => {
313
282
  const input = rawInput;
@@ -331,7 +300,7 @@ class MultiAgentGraph extends StandardGraph {
331
300
  destination = Array.isArray(result) ? result[0] : destinations[0];
332
301
  }
333
302
  let content = `Conditionally transferred to ${destination}`;
334
- if (hasTransferInput &&
303
+ if (hasHandoffInput &&
335
304
  promptKey in input &&
336
305
  input[promptKey] != null) {
337
306
  content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
@@ -354,13 +323,13 @@ class MultiAgentGraph extends StandardGraph {
354
323
  });
355
324
  }, {
356
325
  name: toolName,
357
- schema: hasTransferInput
326
+ schema: hasHandoffInput
358
327
  ? {
359
328
  type: 'object',
360
329
  properties: {
361
330
  [promptKey]: {
362
331
  type: 'string',
363
- description: transferInputDescription,
332
+ description: handoffInputDescription,
364
333
  },
365
334
  },
366
335
  required: [],
@@ -373,12 +342,10 @@ class MultiAgentGraph extends StandardGraph {
373
342
  /** Create individual tools for each destination */
374
343
  for (const destination of destinations) {
375
344
  const toolName = `${Constants.LC_TRANSFER_TO_}${destination}`;
376
- const destContext = this.agentContexts.get(destination);
377
- const toolDescription = edge.description ??
378
- this.buildDefaultTransferDescription(destContext, destination);
345
+ const toolDescription = edge.description ?? `Transfer control to agent '${destination}'`;
379
346
  /** Check if we have a prompt for handoff input */
380
- const hasTransferInput = edge.prompt != null && typeof edge.prompt === 'string';
381
- const transferInputDescription = hasTransferInput
347
+ const hasHandoffInput = edge.prompt != null && typeof edge.prompt === 'string';
348
+ const handoffInputDescription = hasHandoffInput
382
349
  ? edge.prompt
383
350
  : undefined;
384
351
  const promptKey = edge.promptKey ?? 'instructions';
@@ -387,7 +354,7 @@ class MultiAgentGraph extends StandardGraph {
387
354
  const toolCallId = config?.toolCall?.id ??
388
355
  'unknown';
389
356
  let content = `Successfully transferred to ${destination}`;
390
- if (hasTransferInput &&
357
+ if (hasHandoffInput &&
391
358
  promptKey in input &&
392
359
  input[promptKey] != null) {
393
360
  content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
@@ -464,13 +431,13 @@ class MultiAgentGraph extends StandardGraph {
464
431
  });
465
432
  }, {
466
433
  name: toolName,
467
- schema: hasTransferInput
434
+ schema: hasHandoffInput
468
435
  ? {
469
436
  type: 'object',
470
437
  properties: {
471
438
  [promptKey]: {
472
439
  type: 'string',
473
- description: transferInputDescription,
440
+ description: handoffInputDescription,
474
441
  },
475
442
  },
476
443
  required: [],
@@ -482,630 +449,12 @@ class MultiAgentGraph extends StandardGraph {
482
449
  }
483
450
  return tools;
484
451
  }
485
- /**
486
- * Builds orchestration guidance injected into the system message of agents
487
- * that have handoff or transfer tools (i.e., orchestrator agents).
488
- *
489
- * Implements two orchestration primitives:
490
- * - Execution bias guidance injected into the system prompt
491
- * - Multi-round autonomous execution for dependent tasks
492
- *
493
- * Handoff tools are synchronous (browser-tool callback pattern): spawn the
494
- * child, await completion, return the real text as the tool output. Parallel
495
- * handoff tool calls in one turn run concurrently via LangGraph's ToolNode,
496
- * so independent children run in parallel without explicit orchestration.
497
- *
498
- * @param childDescs - Display names (with optional descriptions) of child agents
499
- * @param toolCount - Number of handoff/transfer tools available
500
- */
501
- buildOrchestratorGuidance(childDescs, toolCount) {
502
- return [
503
- '## Agent Orchestration',
504
- '',
505
- `You have ${toolCount} specialist agent(s) available for delegation:`,
506
- ...childDescs.map((d) => `- ${d}`),
507
- '',
508
- '## Execution Bias',
509
- 'If the user asks you to do the work, start doing it in the same turn.',
510
- 'Use a real tool call or concrete action first when the task is actionable; do not stop at a plan or promise-to-act reply.',
511
- 'Commentary-only turns are incomplete when tools are available and the next action is clear.',
512
- 'If the work will take multiple steps or a while to finish, send one short progress update before or while acting.',
513
- '',
514
- '## How Delegation Works',
515
- 'Each handoff tool call spawns a sub-agent, waits for it to complete, and returns the real result directly — like a function call.',
516
- 'Independent tasks MAY be called in parallel (multiple handoff tool calls in one turn). They run concurrently and all results return together.',
517
- 'Dependent tasks MUST be sequential: call one agent, get the result, then call the next agent using that real data.',
518
- '',
519
- '## Agent Isolation',
520
- "Sub-agents CANNOT see your conversation or the user's original message. They ONLY see what you write in the `instructions` parameter.",
521
- "When writing instructions, include ALL data the agent needs. Copy exact values from the user's message — email addresses, names, URLs, dates, numbers.",
522
- 'When delegating follow-up work, include the real data from prior agent results directly in the instructions text.',
523
- 'Do NOT re-delegate a task that was already completed. If you have the data, pass it directly.',
524
- ].join('\n');
525
- }
526
- /**
527
- * Builds subagent context instructions injected into child agents that are
528
- * handoff destinations. This tells the child agent it is a subagent with
529
- * a focused task.
530
- *
531
- * @param orchestratorName - Display name of the parent orchestrator agent
532
- */
533
- buildChildAgentContext(orchestratorName) {
534
- return [
535
- '# Subagent Context',
536
- '',
537
- `You are a **subagent** spawned by the ${orchestratorName} for a specific task.`,
538
- '',
539
- '## Your Role',
540
- "- Complete this task. That's your entire purpose.",
541
- `- You are NOT the ${orchestratorName}. Don't try to be.`,
542
- '',
543
- '## Rules',
544
- '1. **Stay focused** - Do your assigned task, nothing else',
545
- `2. **Complete the task** - Your final message will be automatically reported to the ${orchestratorName}`,
546
- "3. **Don't initiate** - No heartbeats, no proactive actions, no side quests",
547
- "4. **Be ephemeral** - You may be terminated after task completion. That's fine.",
548
- '',
549
- '## Output Format',
550
- 'When complete, your final response should include:',
551
- '- What you accomplished or found',
552
- `- Any relevant details the ${orchestratorName} should know`,
553
- '- Keep it concise but informative',
554
- '',
555
- "## What You DON'T Do",
556
- `- NO user conversations (that's ${orchestratorName}'s job)`,
557
- '- NO external messages (email, tweets, etc.) unless explicitly tasked with a specific recipient/channel',
558
- '- NO cron jobs or persistent state',
559
- `- NO pretending to be the ${orchestratorName}`,
560
- ].join('\n');
561
- }
562
- /**
563
- * Builds a meaningful default description for a transfer tool when no explicit
564
- * edge.description is provided. Uses the destination agent's name and description
565
- * so the LLM can make informed routing decisions.
566
- * @param destContext - AgentContext of the destination agent (may be undefined)
567
- * @param destinationId - Raw agent ID (fallback when context unavailable)
568
- */
569
- buildDefaultTransferDescription(destContext, destinationId) {
570
- const displayName = destContext?.name ?? destinationId;
571
- const agentDescription = destContext?.description;
572
- if (agentDescription != null && agentDescription !== '') {
573
- return `Transfer to "${displayName}": ${agentDescription}`;
574
- }
575
- return `Transfer control to "${displayName}"`;
576
- }
577
- /**
578
- * Create handoff tools for agents based on handoff edges.
579
- * Handoff tools invoke child agent subgraphs inline and return the result
580
- * as a string to the parent agent's context. Unlike transfer tools (which
581
- * return Command for one-way routing), handoff tools execute the child,
582
- * extract the final text, and return it within the parent's agent loop.
583
- *
584
- * This enables the supervisor pattern: parent calls child → gets result → thinks → calls another.
585
- */
586
- createHandoffTools() {
587
- const handoffsByAgent = new Map();
588
- for (const edge of this.handoffEdges) {
589
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
590
- sources.forEach((source) => {
591
- if (!handoffsByAgent.has(source)) {
592
- handoffsByAgent.set(source, []);
593
- }
594
- handoffsByAgent.get(source).push(edge);
595
- });
596
- }
597
- for (const [agentId, edges] of handoffsByAgent) {
598
- const agentContext = this.agentContexts.get(agentId);
599
- if (!agentContext)
600
- continue;
601
- const handoffTools = [];
602
- for (const edge of edges) {
603
- handoffTools.push(...this.createHandoffToolsForEdge(edge, agentId));
604
- }
605
- if (!agentContext.graphTools) {
606
- agentContext.graphTools = [];
607
- }
608
- agentContext.graphTools.push(...handoffTools);
609
- // No collect_results tool needed — handoff tools use the browser-tool
610
- // callback pattern: spawn child, wait for completion, return real result.
611
- // The LLM naturally gets child results as tool return values.
612
- mlog(`[MultiAgentGraph] Handoff tools for "${agentId}": [${handoffTools.map((t) => t.name).join(', ')}]`);
613
- // Inject autonomous orchestration guidance for agents with handoff tools.
614
- const childDescs = edges.flatMap((e) => {
615
- const dests = Array.isArray(e.to) ? e.to : [e.to];
616
- return dests.map((d) => {
617
- const ctx = this.agentContexts.get(d);
618
- const name = ctx?.name ?? d;
619
- const desc = ctx?.description ? ` — ${ctx.description}` : '';
620
- return `${name}${desc}`;
621
- });
622
- });
623
- const orchestrationGuidance = this.buildOrchestratorGuidance(childDescs, handoffTools.length);
624
- const existing = agentContext.additionalInstructions ?? '';
625
- agentContext.additionalInstructions = existing
626
- ? `${existing}\n\n${orchestrationGuidance}`
627
- : orchestrationGuidance;
628
- // Inject subagent context into each child/destination agent.
629
- // This tells child agents they are subagents with a focused task — stay focused,
630
- // execute (don't plan), and return results to the orchestrator.
631
- const orchestratorName = agentContext.name ?? agentId;
632
- const childAgentContext = this.buildChildAgentContext(orchestratorName);
633
- for (const edge of edges) {
634
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
635
- for (const destId of dests) {
636
- const destCtx = this.agentContexts.get(destId);
637
- if (!destCtx)
638
- continue;
639
- const existingChild = destCtx.additionalInstructions ?? '';
640
- // Avoid duplicate injection if agent is destination of multiple edges
641
- if (existingChild.includes('# Subagent Context'))
642
- continue;
643
- destCtx.additionalInstructions = existingChild
644
- ? `${existingChild}\n\n${childAgentContext}`
645
- : childAgentContext;
646
- }
647
- }
648
- }
649
- }
650
- /**
651
- * Create handoff tools for an edge (handles multiple destinations).
652
- * Each handoff tool spawns the child agent's compiled subgraph asynchronously
653
- * and returns immediately. The orchestrator uses `collect_results` to retrieve
654
- * outputs and `check_agents` to monitor status — a push-based autonomous
655
- * orchestration pattern.
656
- *
657
- * @param edge - The graph edge defining the handoff
658
- * @param sourceAgentId - The ID of the parent/supervisor agent
659
- */
660
- createHandoffToolsForEdge(edge, sourceAgentId) {
661
- const tools = [];
662
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
663
- const maxResultChars = edge.maxResultChars ?? DEFAULT_HANDOFF_MAX_RESULT_CHARS;
664
- for (const destination of destinations) {
665
- const toolName = `${Constants.LC_HANDOFF_TO_}${destination}`;
666
- const destContext = this.agentContexts.get(destination);
667
- const toolDescription = edge.description ??
668
- this.buildDefaultHandoffDescription(destContext, destination);
669
- /**
670
- * Always include an instructions parameter so the orchestrator can
671
- * pass scoped task descriptions to each child agent. Without this,
672
- * the child gets no context about what to do.
673
- */
674
- const promptKey = edge.promptKey ?? 'instructions';
675
- const destDesc = destContext?.description;
676
- const promptInputDescription = edge.prompt ??
677
- (destDesc
678
- ? `Task instructions for this agent (${destDesc}). Describe exactly what it should do.`
679
- : 'Specific task instructions for this agent. Describe exactly what it should do and what data to use.');
680
- /** Capture registry reference — Map populated in createWorkflow() */
681
- const subgraphReg = this.subgraphRegistry;
682
- tools.push(tool(async (rawInput, config) => {
683
- const input = rawInput;
684
- const subgraph = subgraphReg.get(destination);
685
- if (!subgraph) {
686
- throw new Error(`Handoff target "${destination}" subgraph not found in registry. ` +
687
- 'This is a bug: createWorkflow() should have populated the subgraph registry.');
688
- }
689
- /**
690
- * Per-spawn unique key = the orchestrator's tool_call.id.
691
- * LangChain's ToolNode passes `config.toolCall.id` to the tool
692
- * function; this is the same id the frontend sees on the parent's
693
- * handoff content part, so the UI can match each AgentHandoff
694
- * indicator to its own sidebar task without collision when the
695
- * same agent is invoked multiple times.
696
- */
697
- const toolCallCfg = config?.toolCall;
698
- const spawnKey = toolCallCfg?.id ??
699
- `${destination}_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`;
700
- /**
701
- * Hierarchical spawnPath: parent's spawnPath (from metadata) + this spawnKey.
702
- * Root invocations have empty parentSpawnPath. Threaded through childConfig
703
- * so nested handoffs/sequences inherit the full ancestry.
704
- * See docs/multi-agent-nesting-architecture.md §4.
705
- */
706
- const parentMetadata = config?.metadata;
707
- const parentSpawnPath = typeof parentMetadata?.spawnPath === 'string'
708
- ? parentMetadata.spawnPath
709
- : '';
710
- const childSpawnPath = buildSpawnPath(parentSpawnPath, spawnKey);
711
- const childDepth = spawnPathDepth(childSpawnPath);
712
- /**
713
- * Child agent message construction — three modes:
714
- *
715
- * 1. Default (isolated-session pattern): Child gets ONLY the orchestrator's
716
- * task instructions as a single HumanMessage. No parent conversation
717
- * leaks. Orchestrator controls exactly what context the child sees.
718
- *
719
- * 2. Passthrough (edge.passthrough = true): Child gets the full parent
720
- * conversation + orchestrator's instructions appended. Use this when
721
- * the child needs the full user context (e.g., a transfer-like handoff).
722
- *
723
- * 3. Fallback: If no instructions provided AND not passthrough, child
724
- * gets the agent's description as its task.
725
- */
726
- const taskDescription = promptKey in input && input[promptKey] != null
727
- ? String(input[promptKey])
728
- : '';
729
- let childMessages;
730
- if (edge.passthrough) {
731
- // Passthrough: full parent context + instructions appended
732
- const state = getCurrentTaskInput();
733
- childMessages = MultiAgentGraph.prepareHandoffMessages([
734
- ...state.messages,
735
- ]);
736
- if (taskDescription) {
737
- childMessages.push(new HumanMessage(taskDescription));
738
- }
739
- }
740
- else {
741
- // Default: isolated — only orchestrator's instructions
742
- const fallbackTask = destContext?.description ?? 'Complete your assigned task.';
743
- childMessages = [
744
- new HumanMessage(taskDescription || fallbackTask),
745
- ];
746
- }
747
- const childState = {
748
- messages: childMessages,
749
- };
750
- const childContext = this.agentContexts.get(destination);
751
- const destName = destContext?.name ?? destination;
752
- mlog(`[MultiAgentGraph] Handoff "${sourceAgentId}" -> "${destination}" SPAWN (async)\n` +
753
- ` messages: ${childMessages.length}\n` +
754
- ` childTools: ${childContext?.tools?.length ?? 0} instances\n` +
755
- ` childToolDefs: ${childContext?.toolDefinitions?.length ?? 0} definitions`);
756
- /**
757
- * Dispatch transition BEFORE spawning the child subgraph so that
758
- * callbacks.js sets multiAgentTrace.isMultiAgent = true before the
759
- * child's ON_RUN_STEP events fire. spawnKey lets the UI create a
760
- * distinct sidebar task for this specific invocation.
761
- */
762
- mlog(`[MultiAgentGraph] Handoff SPAWN "${sourceAgentId}" -> "${destination}" spawnKey=${spawnKey}`);
763
- await safeDispatchCustomEvent(GraphEvents.ON_AGENT_TRANSITION, {
764
- sourceAgentId: sourceAgentId,
765
- sourceAgentName: this.agentContexts.get(sourceAgentId)?.name ?? sourceAgentId,
766
- destinationAgentId: destination,
767
- destinationAgentName: destName,
768
- edgeType: EdgeType.HANDOFF,
769
- timestamp: Date.now(),
770
- spawnKey,
771
- spawnPath: childSpawnPath,
772
- parentSpawnPath: parentSpawnPath || null,
773
- spawnDepth: childDepth,
774
- }, config);
775
- /**
776
- * Child events need to carry spawnKey so callbacks.js can route
777
- * them to the correct child aggregator. LangChain propagates
778
- * `metadata` and `tags` from the parent config to all descendants,
779
- * so everything dispatched inside subgraph.invoke will have
780
- * metadata.spawnKey populated on the event's runtime metadata.
781
- */
782
- const childConfig = {
783
- ...(config ?? {}),
784
- metadata: {
785
- ...(config?.metadata ?? {}),
786
- spawnKey,
787
- spawnAgentId: destination,
788
- /** Hierarchical identity — see spawnPath.ts */
789
- spawnPath: childSpawnPath,
790
- parentSpawnPath: parentSpawnPath || null,
791
- spawnDepth: childDepth,
792
- },
793
- tags: [
794
- ...(config?.tags ?? []),
795
- `spawn:${spawnKey}`,
796
- `depth:${childDepth}`,
797
- ],
798
- };
799
- /**
800
- * Callback pattern: spawn child, WAIT for completion, return real
801
- * result. The parent naturally sees child results in its tool
802
- * return, so no manual "collect_results" step is needed.
803
- *
804
- * Parallelism still works: when the LLM emits multiple handoff
805
- * tool calls in one response, LangGraph runs all tool functions
806
- * concurrently. Each waits for its child. All results land in
807
- * the LLM's next turn together.
808
- */
809
- const spawnedAt = Date.now();
810
- try {
811
- const result = await subgraph.invoke(childState, childConfig);
812
- const durationMs = Date.now() - spawnedAt;
813
- const resultText = MultiAgentGraph.extractHandoffResult(result.messages, destination);
814
- const truncated = MultiAgentGraph.truncateHandoffResult(resultText, maxResultChars);
815
- mlog(`[MultiAgentGraph] Handoff COMPLETED "${sourceAgentId}" -> "${destination}" ` +
816
- `spawnKey=${spawnKey} (${durationMs}ms, ${truncated.length} chars)`);
817
- /** Dispatch completion event for UI update — carries spawnKey so
818
- * the frontend can mark the correct sidebar task as completed. */
819
- safeDispatchCustomEvent(GraphEvents.ON_AGENT_TRANSITION, {
820
- sourceAgentId: destination,
821
- sourceAgentName: destName,
822
- destinationAgentId: sourceAgentId,
823
- destinationAgentName: this.agentContexts.get(sourceAgentId)?.name ??
824
- sourceAgentId,
825
- edgeType: EdgeType.HANDOFF,
826
- timestamp: Date.now(),
827
- isCompletion: true,
828
- durationMs,
829
- resultLength: truncated.length,
830
- spawnKey,
831
- spawnPath: childSpawnPath,
832
- parentSpawnPath: parentSpawnPath || null,
833
- spawnDepth: childDepth,
834
- }, config).catch(() => {
835
- /* best-effort event dispatch */
836
- });
837
- return truncated;
838
- }
839
- catch (err) {
840
- const durationMs = Date.now() - spawnedAt;
841
- const errMsg = err instanceof Error ? err.message : String(err);
842
- // EPIPE from console.debug is non-fatal
843
- if (errMsg.includes('EPIPE')) {
844
- mwarn(`[MultiAgentGraph] Child "${destination}" hit EPIPE (non-fatal) spawnKey=${spawnKey}`);
845
- safeDispatchCustomEvent(GraphEvents.ON_AGENT_TRANSITION, {
846
- sourceAgentId: destination,
847
- sourceAgentName: destName,
848
- destinationAgentId: sourceAgentId,
849
- destinationAgentName: this.agentContexts.get(sourceAgentId)?.name ??
850
- sourceAgentId,
851
- edgeType: EdgeType.HANDOFF,
852
- timestamp: Date.now(),
853
- isCompletion: true,
854
- durationMs,
855
- spawnKey,
856
- spawnPath: childSpawnPath,
857
- parentSpawnPath: parentSpawnPath || null,
858
- spawnDepth: childDepth,
859
- }, config).catch(() => {
860
- /* best-effort */
861
- });
862
- return `Agent "${destName}" completed but output was lost due to stream closure.`;
863
- }
864
- console.error(`[MultiAgentGraph] Handoff FAILED "${sourceAgentId}" -> "${destination}" ` +
865
- `spawnKey=${spawnKey} (${durationMs}ms): ${errMsg}`);
866
- safeDispatchCustomEvent(GraphEvents.ON_AGENT_TRANSITION, {
867
- sourceAgentId: destination,
868
- sourceAgentName: destName,
869
- destinationAgentId: sourceAgentId,
870
- destinationAgentName: this.agentContexts.get(sourceAgentId)?.name ??
871
- sourceAgentId,
872
- edgeType: EdgeType.HANDOFF,
873
- timestamp: Date.now(),
874
- isCompletion: true,
875
- durationMs,
876
- spawnKey,
877
- spawnPath: childSpawnPath,
878
- parentSpawnPath: parentSpawnPath || null,
879
- spawnDepth: childDepth,
880
- error: errMsg,
881
- }, config).catch(() => {
882
- /* best-effort */
883
- });
884
- return `Agent "${destName}" failed after ${durationMs}ms: ${errMsg}`;
885
- }
886
- }, {
887
- name: toolName,
888
- schema: {
889
- type: 'object',
890
- properties: {
891
- [promptKey]: {
892
- type: 'string',
893
- description: promptInputDescription,
894
- },
895
- },
896
- required: [promptKey],
897
- },
898
- description: toolDescription,
899
- }));
900
- }
901
- return tools;
902
- }
903
- /**
904
- * Extract the final text result from a child agent's output messages.
905
- * Walks backwards to find the last AIMessage with text content.
906
- * Handles both string content and array content (multi-modal messages).
907
- * @param messages - The child agent's output messages
908
- * @param agentId - The child agent ID (for fallback message)
909
- */
910
- static extractHandoffResult(messages, agentId) {
911
- for (let i = messages.length - 1; i >= 0; i--) {
912
- const msg = messages[i];
913
- if (msg.getType() !== 'ai')
914
- continue;
915
- const content = msg.content;
916
- if (typeof content === 'string' && content.trim()) {
917
- return content.trim();
918
- }
919
- /** Handle array content (multi-modal messages with text blocks) */
920
- if (Array.isArray(content)) {
921
- const textParts = content
922
- .filter((block) => typeof block === 'object' &&
923
- block !== null &&
924
- 'type' in block &&
925
- block.type === 'text' &&
926
- 'text' in block &&
927
- typeof block.text === 'string')
928
- .map((block) => block.text);
929
- const text = textParts.join('\n').trim();
930
- if (text)
931
- return text;
932
- }
933
- }
934
- return `[Agent "${agentId}" completed but produced no text output]`;
935
- }
936
- /**
937
- * Truncate handoff result using head/tail strategy (60/40 split).
938
- * Preserves the beginning (key findings) and end (conclusions).
939
- * Matches the TaskTool.truncateResult pattern used by host orchestrators.
940
- * @param result - The full result text
941
- * @param maxChars - Maximum allowed characters
942
- */
943
- static truncateHandoffResult(result, maxChars) {
944
- if (!result || result.length <= maxChars) {
945
- return result;
946
- }
947
- const truncationNotice = '\n\n[... handoff output truncated — middle section omitted to fit parent context ...]\n\n';
948
- const available = maxChars - truncationNotice.length;
949
- if (available <= 0) {
950
- return result.substring(0, maxChars);
951
- }
952
- const headSize = Math.floor(available * 0.6);
953
- const tailSize = available - headSize;
954
- return (result.substring(0, headSize) +
955
- truncationNotice +
956
- result.substring(result.length - tailSize));
957
- }
958
- /**
959
- * Build a meaningful default description for a handoff tool.
960
- * @param destContext - AgentContext of the destination agent
961
- * @param destinationId - Raw agent ID (fallback)
962
- */
963
- buildDefaultHandoffDescription(destContext, destinationId) {
964
- const displayName = destContext?.name ?? destinationId;
965
- const agentDescription = destContext?.description;
966
- if (agentDescription != null && agentDescription !== '') {
967
- return `Hand off task to "${displayName}": ${agentDescription}. The agent will execute and return its result.`;
968
- }
969
- return `Hand off task to "${displayName}" and receive its result.`;
970
- }
971
452
  /**
972
453
  * Create a complete agent subgraph (similar to createReactAgent)
973
454
  */
974
455
  createAgentSubgraph(agentId) {
975
- /**
976
- * Scoped subgraph build for handoff targets.
977
- *
978
- * If the handoff target has outgoing sequence/transfer edges (e.g. a
979
- * "researcher" agent with its own sequence `[researcher → prod_assistant]`),
980
- * we compile a mini-StateGraph containing the agent and all agents reachable
981
- * from it via non-handoff edges. This way, when the parent hands off to
982
- * researcher via `subgraph.invoke()`, the nested sequence runs to completion
983
- * before the result is returned to the parent.
984
- *
985
- * Fast path: if no downstream agents are reachable, fall back to the
986
- * previous single-node behavior (`createAgentNode`).
987
- *
988
- * See docs/multi-agent-nesting-architecture.md §6.
989
- */
990
- const reachable = this.computeReachableViaNonHandoff(agentId);
991
- if (reachable.size === 1) {
992
- return this.createAgentNode(agentId);
993
- }
994
- mlog(`[MultiAgentGraph] Scoped subgraph for "${agentId}": ${reachable.size} nodes [${Array.from(reachable).join(', ')}]`);
995
- return this.buildScopedSubgraph(agentId, reachable);
996
- }
997
- /**
998
- * BFS from `rootAgentId` across sequence + transfer edges (NOT handoff edges).
999
- * Returns the set of agents reachable in this agent's "local workflow".
1000
- */
1001
- computeReachableViaNonHandoff(rootAgentId) {
1002
- const reachable = new Set([rootAgentId]);
1003
- const queue = [rootAgentId];
1004
- const localEdges = [...this.sequenceEdges, ...this.transferEdges];
1005
- while (queue.length > 0) {
1006
- const current = queue.shift();
1007
- for (const edge of localEdges) {
1008
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1009
- if (!sources.includes(current))
1010
- continue;
1011
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
1012
- for (const dest of dests) {
1013
- if (!reachable.has(dest) && this.agentContexts.has(dest)) {
1014
- reachable.add(dest);
1015
- queue.push(dest);
1016
- }
1017
- }
1018
- }
1019
- }
1020
- return reachable;
1021
- }
1022
- /**
1023
- * Build a compiled scoped StateGraph containing `agentIds` as nodes, rooted
1024
- * at `rootAgentId`. Linear sequence edges where both endpoints are in scope
1025
- * are wired directly; nodes with no outgoing in-scope edges route to END.
1026
- *
1027
- * Each node is wrapped around the per-agent `createAgentNode` compiled
1028
- * workflow (agent + tools loop) to preserve isolated tool context.
1029
- */
1030
- buildScopedSubgraph(rootAgentId, agentIds) {
1031
- const StateAnnotation = Annotation.Root({
1032
- messages: Annotation({
1033
- reducer: messagesStateReducer,
1034
- default: () => [],
1035
- }),
1036
- });
1037
- const builder = new StateGraph(StateAnnotation);
1038
- // Precompile each scoped agent's inner workflow and wrap as a node.
1039
- //
1040
- // Two different isolation strategies depending on position:
1041
- //
1042
- // • ROOT node (the handoff target itself): receives the parent
1043
- // orchestrator's handoff frame. Use `prepareHandoffMessages` — drops
1044
- // orphaned tool_use, compacts paired tool calls, guarantees trailing
1045
- // HumanMessage for Bedrock/VertexAI compatibility. The root needs
1046
- // orchestrator context because it's responding to the handoff.
1047
- //
1048
- // • DOWNSTREAM nodes (sequence targets of the root): run as FULLY
1049
- // ISOLATED child sessions. They receive only:
1050
- // [original user request, synthetic HumanMessage describing what
1051
- // the upstream agent produced and asking them to act]
1052
- // No raw tool_use / tool_result blocks from the upstream agent —
1053
- // prevents schema confusion when a downstream agent sees noisy
1054
- // upstream context and produces malformed tool_use JSON.
1055
- //
1056
- // Each wrapper returns only the DELTA (new messages produced by the
1057
- // inner invoke), not the prepared input — otherwise messagesStateReducer
1058
- // would double-append the synthetic instruction into the scoped state.
1059
- for (const aid of agentIds) {
1060
- const inner = this.createAgentNode(aid);
1061
- const isRoot = aid === rootAgentId;
1062
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1063
- builder.addNode(aid, async (state, config) => {
1064
- const prepared = isRoot
1065
- ? MultiAgentGraph.prepareHandoffMessages(state.messages)
1066
- : MultiAgentGraph.prepareIsolatedChildMessages(state.messages);
1067
- mlog(`[MultiAgentGraph] scoped node "${aid}" entering (isRoot=${isRoot}, stateMessages=${state.messages.length}, prepared=${prepared.length})`);
1068
- const result = await inner.invoke({ ...state, messages: prepared }, config);
1069
- // Return only the messages the inner node appended beyond its input,
1070
- // so messagesStateReducer doesn't duplicate the synthetic wrapper
1071
- // prompt into the scoped state.
1072
- const delta = result.messages.length > prepared.length
1073
- ? result.messages.slice(prepared.length)
1074
- : result.messages;
1075
- return { messages: delta };
1076
- });
1077
- }
1078
- // START → root
1079
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1080
- // @ts-ignore — LangGraph string typing is too strict for dynamic agent ids
1081
- builder.addEdge(START, rootAgentId);
1082
- // Wire sequence edges in scope (linear chain support)
1083
- const hasOutgoing = new Set();
1084
- for (const edge of this.sequenceEdges) {
1085
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1086
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
1087
- for (const source of sources) {
1088
- if (!agentIds.has(source))
1089
- continue;
1090
- for (const dest of dests) {
1091
- if (!agentIds.has(dest))
1092
- continue;
1093
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1094
- // @ts-ignore
1095
- builder.addEdge(source, dest);
1096
- hasOutgoing.add(source);
1097
- }
1098
- }
1099
- }
1100
- // Leaves (no outgoing in-scope edges) route to END
1101
- for (const aid of agentIds) {
1102
- if (!hasOutgoing.has(aid)) {
1103
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1104
- // @ts-ignore
1105
- builder.addEdge(aid, END);
1106
- }
1107
- }
1108
- return builder.compile(this.compileOptions);
456
+ /** This is essentially the same as `createAgentNode` from `StandardGraph` */
457
+ return this.createAgentNode(agentId);
1109
458
  }
1110
459
  /**
1111
460
  * Detects if the current agent is receiving a handoff and processes the messages accordingly.
@@ -1119,23 +468,7 @@ class MultiAgentGraph extends StandardGraph {
1119
468
  * @param agentId - The agent ID to check for handoff reception
1120
469
  * @returns Object with filtered messages, extracted instructions, source agent, and parallel siblings
1121
470
  */
1122
- /**
1123
- * Prepare messages for a handoff child agent. See
1124
- * {@link prepareHandoffMessagesUtil} for the full implementation and
1125
- * semantics — this static method is a thin delegate preserved for
1126
- * backward compatibility with existing call sites and unit tests.
1127
- */
1128
- static prepareHandoffMessages(messages) {
1129
- return prepareHandoffMessages(messages);
1130
- }
1131
- /**
1132
- * Build an isolated message context for a downstream scoped-subgraph
1133
- * node. See {@link prepareIsolatedChildMessagesUtil} for details.
1134
- */
1135
- static prepareIsolatedChildMessages(messages) {
1136
- return prepareIsolatedChildMessages(messages);
1137
- }
1138
- processTransferReception(messages, agentId) {
471
+ processHandoffReception(messages, agentId) {
1139
472
  if (messages.length === 0)
1140
473
  return null;
1141
474
  /**
@@ -1164,9 +497,8 @@ class MultiAgentGraph extends StandardGraph {
1164
497
  destinationAgent = toolName.replace(Constants.LC_TRANSFER_TO_, '');
1165
498
  }
1166
499
  else if (isConditionalTransfer) {
1167
- const transferDest = candidateMsg.additional_kwargs.handoff_destination;
1168
- destinationAgent =
1169
- typeof transferDest === 'string' ? transferDest : null;
500
+ const handoffDest = candidateMsg.additional_kwargs.handoff_destination;
501
+ destinationAgent = typeof handoffDest === 'string' ? handoffDest : null;
1170
502
  }
1171
503
  /** Check if this transfer targets our agent */
1172
504
  if (destinationAgent === agentId) {
@@ -1182,7 +514,7 @@ class MultiAgentGraph extends StandardGraph {
1182
514
  const contentStr = typeof toolMessage.content === 'string'
1183
515
  ? toolMessage.content
1184
516
  : JSON.stringify(toolMessage.content);
1185
- const instructionsMatch = contentStr.match(TRANSFER_INSTRUCTIONS_PATTERN);
517
+ const instructionsMatch = contentStr.match(HANDOFF_INSTRUCTIONS_PATTERN);
1186
518
  const instructions = instructionsMatch?.[1]?.trim() ?? null;
1187
519
  /** Extract source agent name from additional_kwargs */
1188
520
  const handoffSourceName = toolMessage.additional_kwargs.handoff_source_name;
@@ -1240,13 +572,9 @@ class MultiAgentGraph extends StandardGraph {
1240
572
  if (hasTransferCalls) {
1241
573
  if (remainingToolCalls.length > 0 ||
1242
574
  (typeof aiMsg.content === 'string' && aiMsg.content.trim())) {
1243
- /** Keep the message but without transfer tool calls.
1244
- * Trim trailing whitespace to prevent Bedrock validation errors. */
1245
- const trimmedContent = typeof aiMsg.content === 'string'
1246
- ? aiMsg.content.trimEnd()
1247
- : aiMsg.content;
575
+ /** Keep the message but without transfer tool calls */
1248
576
  const filteredAiMsg = new AIMessage({
1249
- content: trimmedContent,
577
+ content: aiMsg.content,
1250
578
  tool_calls: remainingToolCalls,
1251
579
  id: aiMsg.id,
1252
580
  });
@@ -1260,107 +588,15 @@ class MultiAgentGraph extends StandardGraph {
1260
588
  /** Keep all other messages */
1261
589
  filteredMessages.push(msg);
1262
590
  }
1263
- /**
1264
- * Flatten tool call/result pairs into text summaries for handoff.
1265
- *
1266
- * When agent A uses tools and then hands off to agent B, agent B may not
1267
- * have the same tools configured. Providers like Bedrock require toolConfig
1268
- * when tool_use/tool_result blocks are in the message history. Converting
1269
- * tool interactions to text summaries avoids this and reduces context bloat.
1270
- *
1271
- * Strategy: Walk through messages and merge each (AIMessage-with-tool_calls +
1272
- * following ToolMessages) group into a single AIMessage containing the original
1273
- * text plus a textual summary of the tool interaction. This preserves proper
1274
- * message role ordering (human/assistant alternation).
1275
- */
1276
- const compactedMessages = [];
1277
- for (let i = 0; i < filteredMessages.length; i++) {
1278
- const msg = filteredMessages[i];
1279
- const msgType = msg.getType();
1280
- if (msgType === 'ai') {
1281
- const aiMsg = msg;
1282
- const toolCalls = aiMsg.tool_calls;
1283
- if (toolCalls && toolCalls.length > 0) {
1284
- /** Extract text content from the AIMessage */
1285
- const textContent = typeof aiMsg.content === 'string'
1286
- ? aiMsg.content
1287
- : Array.isArray(aiMsg.content)
1288
- ? aiMsg.content
1289
- .filter((b) => typeof b === 'object' &&
1290
- b.type === 'text' &&
1291
- 'text' in b)
1292
- .map((b) => b.text)
1293
- .join('\n')
1294
- : '';
1295
- /** Collect tool_call_ids so we can match following ToolMessages */
1296
- const callIds = new Set(toolCalls.map((tc) => tc.id).filter(Boolean));
1297
- /** Build summary of what tools were called */
1298
- const callSummaries = toolCalls.map((tc) => `[Called "${tc.name}"]`);
1299
- /** Consume following ToolMessages that belong to this AI message */
1300
- const resultSummaries = [];
1301
- while (i + 1 < filteredMessages.length) {
1302
- const next = filteredMessages[i + 1];
1303
- if (next.getType() !== 'tool')
1304
- break;
1305
- const toolMsg = next;
1306
- if (!callIds.has(toolMsg.tool_call_id))
1307
- break;
1308
- /** Extract and summarize the tool result */
1309
- const rawContent = typeof toolMsg.content === 'string'
1310
- ? toolMsg.content
1311
- : Array.isArray(toolMsg.content)
1312
- ? toolMsg.content
1313
- .filter((b) => typeof b === 'object' &&
1314
- 'text' in b &&
1315
- typeof b.text ===
1316
- 'string')
1317
- .map((b) => b.text)
1318
- .join('\n')
1319
- : JSON.stringify(toolMsg.content);
1320
- const summary = rawContent.length > 500
1321
- ? rawContent.split('\n')[0] + ' [truncated for handoff]'
1322
- : rawContent;
1323
- resultSummaries.push(`[Tool "${toolMsg.name}" returned: ${summary}]`);
1324
- i++; // Skip this ToolMessage in the outer loop
1325
- }
1326
- /** Merge everything into a single AIMessage */
1327
- const parts = [
1328
- textContent,
1329
- ...callSummaries,
1330
- ...resultSummaries,
1331
- ].filter(Boolean);
1332
- /** Bedrock rejects messages with trailing whitespace */
1333
- const mergedContent = (parts.join('\n') || '[Agent processed tools]').trimEnd();
1334
- compactedMessages.push(new AIMessage({
1335
- content: mergedContent,
1336
- id: aiMsg.id,
1337
- }));
1338
- continue;
1339
- }
1340
- }
1341
- /** Skip orphaned ToolMessages (their AI parent was already handled above,
1342
- * or they belong to a transfer that was already filtered out) */
1343
- if (msgType === 'tool') {
1344
- continue;
1345
- }
1346
- /** Trim trailing whitespace on AI messages to prevent Bedrock validation errors */
1347
- if (msgType === 'ai' &&
1348
- typeof msg.content === 'string' &&
1349
- msg.content !== msg.content.trimEnd()) {
1350
- compactedMessages.push(new AIMessage({ content: msg.content.trimEnd(), id: msg.id }));
1351
- continue;
1352
- }
1353
- compactedMessages.push(msg);
1354
- }
1355
591
  return {
1356
- filteredMessages: compactedMessages,
592
+ filteredMessages,
1357
593
  instructions,
1358
594
  sourceAgentName,
1359
595
  parallelSiblings,
1360
596
  };
1361
597
  }
1362
598
  /**
1363
- * Create the multi-agent workflow with handoffs, transfers, and sequences
599
+ * Create the multi-agent workflow with dynamic handoffs
1364
600
  */
1365
601
  createWorkflow() {
1366
602
  const StateAnnotation = Annotation.Root({
@@ -1383,119 +619,53 @@ class MultiAgentGraph extends StandardGraph {
1383
619
  }),
1384
620
  });
1385
621
  const builder = new StateGraph(StateAnnotation);
1386
- /**
1387
- * Identify agents that are ONLY handoff destinations (not transfer/sequence
1388
- * destinations and not starting nodes). These agents are invoked inline via
1389
- * subgraph.invoke() inside handoff tools — they must NOT be added as
1390
- * top-level nodes in the parent graph because LangGraph validates that all
1391
- * nodes are reachable from START via edges.
1392
- */
1393
- const handoffOnlyDestinations = new Set();
1394
- const transferOrSequenceDestinations = new Set();
1395
- for (const edge of this.handoffEdges) {
1396
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
1397
- dests.forEach((d) => handoffOnlyDestinations.add(d));
1398
- }
1399
- for (const edge of [...this.transferEdges, ...this.sequenceEdges]) {
1400
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
1401
- dests.forEach((d) => transferOrSequenceDestinations.add(d));
1402
- }
1403
- // Remove agents that are also transfer/sequence destinations or starting nodes
1404
- for (const d of transferOrSequenceDestinations) {
1405
- handoffOnlyDestinations.delete(d);
1406
- }
1407
- for (const startNode of this.startingNodes) {
1408
- handoffOnlyDestinations.delete(startNode);
1409
- }
1410
- /**
1411
- * Nested-sequence expansion: for each handoff-only target, its downstream
1412
- * sequence/transfer agents MUST also become handoff-only — they exist only
1413
- * inside the target's scoped subgraph, not at top level. Without this,
1414
- * those downstream nodes would be added as top-level orphans and LangGraph
1415
- * would fail compilation (UNREACHABLE_NODE).
1416
- *
1417
- * See docs/multi-agent-nesting-architecture.md §6.
1418
- */
1419
- const nestedHandoffOnly = new Set();
1420
- for (const target of handoffOnlyDestinations) {
1421
- const reachable = this.computeReachableViaNonHandoff(target);
1422
- for (const agent of reachable) {
1423
- if (agent === target)
1424
- continue;
1425
- // Skip if this agent is legitimately a top-level starting node
1426
- if (this.startingNodes.has(agent))
1427
- continue;
1428
- nestedHandoffOnly.add(agent);
1429
- }
1430
- }
1431
- for (const agent of nestedHandoffOnly) {
1432
- handoffOnlyDestinations.add(agent);
1433
- }
1434
- if (nestedHandoffOnly.size > 0) {
1435
- mlog(`[MultiAgentGraph] Nested handoff-only (scoped subgraph downstream): [${Array.from(nestedHandoffOnly).join(', ')}]`);
1436
- }
1437
- if (handoffOnlyDestinations.size > 0) {
1438
- mlog(`[MultiAgentGraph] Handoff-only children (subgraph only, no top-level node): [${Array.from(handoffOnlyDestinations).join(', ')}]`);
1439
- }
1440
- // Add agents as nodes — skip handoff-only children (they exist as subgraphs only)
622
+ // Add all agents as complete subgraphs
1441
623
  for (const [agentId] of this.agentContexts) {
1442
624
  // Get all possible destinations for this agent
1443
- const transferDestinations = new Set();
1444
- const sequenceDestinations = new Set();
1445
- // Check transfer edges for destinations
1446
- for (const edge of this.transferEdges) {
625
+ const handoffDestinations = new Set();
626
+ const directDestinations = new Set();
627
+ // Check handoff edges for destinations
628
+ for (const edge of this.handoffEdges) {
1447
629
  const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1448
630
  if (sources.includes(agentId) === true) {
1449
631
  const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
1450
- dests.forEach((dest) => transferDestinations.add(dest));
632
+ dests.forEach((dest) => handoffDestinations.add(dest));
1451
633
  }
1452
634
  }
1453
- // Check sequence edges for destinations
1454
- for (const edge of this.sequenceEdges) {
635
+ // Check direct edges for destinations
636
+ for (const edge of this.directEdges) {
1455
637
  const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1456
638
  if (sources.includes(agentId) === true) {
1457
639
  const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
1458
- dests.forEach((dest) => sequenceDestinations.add(dest));
640
+ dests.forEach((dest) => directDestinations.add(dest));
1459
641
  }
1460
642
  }
1461
- /** Check if this agent has BOTH transfer and sequence edges */
1462
- const hasTransferEdges = transferDestinations.size > 0;
1463
- const hasSequenceEdges = sequenceDestinations.size > 0;
1464
- const needsCommandRouting = hasTransferEdges && hasSequenceEdges;
643
+ /** Check if this agent has BOTH handoff and direct edges */
644
+ const hasHandoffEdges = handoffDestinations.size > 0;
645
+ const hasDirectEdges = directDestinations.size > 0;
646
+ const needsCommandRouting = hasHandoffEdges && hasDirectEdges;
1465
647
  /** Collect all possible destinations for this agent */
1466
648
  const allDestinations = new Set([
1467
- ...transferDestinations,
1468
- ...sequenceDestinations,
649
+ ...handoffDestinations,
650
+ ...directDestinations,
1469
651
  ]);
1470
- if (transferDestinations.size > 0 || sequenceDestinations.size === 0) {
652
+ if (handoffDestinations.size > 0 || directDestinations.size === 0) {
1471
653
  allDestinations.add(END);
1472
654
  }
1473
655
  /** Agent subgraph (includes agent + tools) */
1474
656
  const agentSubgraph = this.createAgentSubgraph(agentId);
1475
- /** Register subgraph for handoff tools (lazy reference resolution) */
1476
- this.subgraphRegistry.set(agentId, agentSubgraph);
1477
- /**
1478
- * Handoff-only children are invoked inline via subgraph.invoke() — they
1479
- * don't need a top-level node. Adding them would cause LangGraph to reject
1480
- * the graph because no edge routes to them (UNREACHABLE_NODE).
1481
- */
1482
- if (handoffOnlyDestinations.has(agentId)) {
1483
- continue;
1484
- }
1485
657
  /** Wrapper function that handles agentMessages channel, handoff reception, and conditional routing */
1486
658
  const agentWrapper = async (state, config) => {
1487
- mlog(`[MultiAgentGraph] Agent "${agentId}" wrapper ENTRY (messages: ${state.messages.length}, needsCommandRouting: ${needsCommandRouting})`);
1488
659
  let result;
1489
660
  /**
1490
- * Check if this agent is receiving a transfer.
661
+ * Check if this agent is receiving a handoff.
1491
662
  * If so, filter out the transfer messages and inject instructions as preamble.
1492
663
  * This prevents the receiving agent from seeing the transfer as "completed work"
1493
664
  * and prematurely producing an end token.
1494
665
  */
1495
- const transferContext = this.processTransferReception(state.messages, agentId);
1496
- if (transferContext !== null) {
1497
- const { filteredMessages, instructions, sourceAgentName, parallelSiblings, } = transferContext;
1498
- mlog(`[MultiAgentGraph] Agent "${agentId}" receiving transfer from "${sourceAgentName}" (instructions: ${instructions != null}, parallelSiblings: ${parallelSiblings.length})`);
666
+ const handoffContext = this.processHandoffReception(state.messages, agentId);
667
+ if (handoffContext !== null) {
668
+ const { filteredMessages, instructions, sourceAgentName, parallelSiblings, } = handoffContext;
1499
669
  /**
1500
670
  * Set handoff context on the receiving agent.
1501
671
  * Uses pre-computed graph position for depth and parallel info.
@@ -1609,124 +779,24 @@ class MultiAgentGraph extends StandardGraph {
1609
779
  else {
1610
780
  result = await agentSubgraph.invoke(state, config);
1611
781
  }
1612
- /** Track the last agent that produced output for continuation support */
1613
- this.lastActiveAgentId = agentId;
1614
- mlog(`[MultiAgentGraph] Agent "${agentId}" wrapper EXIT (result messages: ${result.messages.length})`);
1615
- /** If agent has both transfer and sequence edges, use Command for exclusive routing */
782
+ /** If agent has both handoff and direct edges, use Command for exclusive routing */
1616
783
  if (needsCommandRouting) {
1617
- /** Check if a transfer occurred */
784
+ /** Check if a handoff occurred */
1618
785
  const lastMessage = result.messages[result.messages.length - 1];
1619
786
  if (lastMessage != null &&
1620
787
  lastMessage.getType() === 'tool' &&
1621
788
  typeof lastMessage.name === 'string' &&
1622
789
  lastMessage.name.startsWith(Constants.LC_TRANSFER_TO_)) {
1623
- /** Transfer occurred - extract destination and navigate there exclusively */
1624
- const transferDest = lastMessage.name.replace(Constants.LC_TRANSFER_TO_, '');
1625
- mlog(`[MultiAgentGraph] Command routing: "${agentId}" -> transfer to "${transferDest}" (sequence edges skipped: [${Array.from(sequenceDestinations).join(', ')}])`);
1626
- /** Validate destination agent exists */
1627
- if (!this.agentContexts.has(transferDest)) {
1628
- const availableAgents = Array.from(this.agentContexts.keys()).join(', ');
1629
- console.error(`[MultiAgentGraph] Transfer to non-existent agent "${transferDest}". Available: ${availableAgents}`);
1630
- /** Return error to model so it can self-correct */
1631
- const errorMsg = new ToolMessage({
1632
- content: `Transfer failed: agent "${transferDest}" does not exist. Available agents: ${availableAgents}. Please choose a valid agent to transfer to.`,
1633
- tool_call_id: lastMessage.tool_call_id,
1634
- name: lastMessage.name,
1635
- });
1636
- errorMsg.status = 'error';
1637
- return {
1638
- messages: [...result.messages, errorMsg],
1639
- };
1640
- }
1641
- /** Pre-handoff context compaction: if receiving agent has smaller budget */
1642
- const receiverContext = this.agentContexts.get(transferDest);
1643
- const senderContext = this.agentContexts.get(agentId);
1644
- if (receiverContext?.maxContextTokens != null &&
1645
- senderContext?.tokenCounter != null &&
1646
- receiverContext.maxContextTokens > 0) {
1647
- let currentSize = 0;
1648
- for (const msg of result.messages) {
1649
- currentSize += senderContext.tokenCounter(msg);
1650
- }
1651
- const receiverBudget = receiverContext.maxContextTokens;
1652
- if (currentSize > receiverBudget * 0.7) {
1653
- mwarn(`[MultiAgentGraph] Pre-handoff compaction: context (${currentSize} tokens) exceeds ` +
1654
- `70% of receiver "${transferDest}" budget (${receiverBudget} tokens)`);
1655
- /** Generate handoff briefing */
1656
- const senderName = senderContext.name ?? agentId;
1657
- if (senderContext.summarizeCallback) {
1658
- try {
1659
- const briefingResult = await summarize(result.messages, async (prompt, _maxTokens) => senderContext.summarizeCallback([
1660
- new HumanMessage(prompt),
1661
- ]), {
1662
- tokenCounter: senderContext.tokenCounter,
1663
- summaryBudget: Math.floor(receiverBudget * 0.2),
1664
- isMultiAgent: true,
1665
- agentWorkflowState: {
1666
- currentAgentId: transferDest,
1667
- agentChain: [agentId, transferDest],
1668
- pendingAgents: [],
1669
- },
1670
- });
1671
- const briefingMsg = new SystemMessage(`[Handoff Briefing from "${senderName}"]\n${briefingResult.summary}`);
1672
- /** Replace messages with briefing + last 3 messages */
1673
- const keepCount = Math.min(3, result.messages.length);
1674
- result = {
1675
- ...result,
1676
- messages: [
1677
- briefingMsg,
1678
- ...result.messages.slice(result.messages.length - keepCount),
1679
- ],
1680
- };
1681
- console.info(`[MultiAgentGraph] Pre-handoff compaction complete: ${currentSize} tokens → briefing + ${keepCount} messages`);
1682
- }
1683
- catch (compactErr) {
1684
- console.error('[MultiAgentGraph] Pre-handoff compaction failed:', compactErr);
1685
- /** Continue without compaction — let receiver handle the overflow */
1686
- }
1687
- }
1688
- else {
1689
- /** No summary callback — use emergency summary */
1690
- const emergencySummary = createEmergencySummary(result.messages);
1691
- const briefingMsg = new SystemMessage(`[Handoff Briefing from "${senderName}" — Emergency]\n${emergencySummary}`);
1692
- const keepCount = Math.min(3, result.messages.length);
1693
- result = {
1694
- ...result,
1695
- messages: [
1696
- briefingMsg,
1697
- ...result.messages.slice(result.messages.length - keepCount),
1698
- ],
1699
- };
1700
- }
1701
- }
1702
- }
1703
- await safeDispatchCustomEvent(GraphEvents.ON_AGENT_TRANSITION, {
1704
- sourceAgentId: agentId,
1705
- sourceAgentName: this.agentContexts.get(agentId)?.name ?? agentId,
1706
- destinationAgentId: transferDest,
1707
- destinationAgentName: this.agentContexts.get(transferDest)?.name ?? transferDest,
1708
- edgeType: EdgeType.TRANSFER,
1709
- timestamp: Date.now(),
1710
- }, config);
790
+ /** Handoff occurred - extract destination and navigate there exclusively */
791
+ const handoffDest = lastMessage.name.replace(Constants.LC_TRANSFER_TO_, '');
1711
792
  return new Command({
1712
793
  update: result,
1713
- goto: transferDest,
794
+ goto: handoffDest,
1714
795
  });
1715
796
  }
1716
797
  else {
1717
- /** No transfer - proceed with sequence edges */
1718
- mlog(`[MultiAgentGraph] Command routing: "${agentId}" -> no transfer, following sequence edges: [${Array.from(sequenceDestinations).join(', ')}]`);
1719
- const directDests = Array.from(sequenceDestinations);
1720
- for (const dest of directDests) {
1721
- await safeDispatchCustomEvent(GraphEvents.ON_AGENT_TRANSITION, {
1722
- sourceAgentId: agentId,
1723
- sourceAgentName: this.agentContexts.get(agentId)?.name ?? agentId,
1724
- destinationAgentId: dest,
1725
- destinationAgentName: this.agentContexts.get(dest)?.name ?? dest,
1726
- edgeType: EdgeType.SEQUENCE,
1727
- timestamp: Date.now(),
1728
- }, config);
1729
- }
798
+ /** No handoff - proceed with direct edges */
799
+ const directDests = Array.from(directDestinations);
1730
800
  if (directDests.length === 1) {
1731
801
  return new Command({
1732
802
  update: result,
@@ -1742,30 +812,7 @@ class MultiAgentGraph extends StandardGraph {
1742
812
  }
1743
813
  }
1744
814
  }
1745
- /**
1746
- * No Command routing needed — dispatch ON_AGENT_TRANSITION for all
1747
- * destinations so callbacks.js can register child agents for event
1748
- * isolation BEFORE they start streaming.
1749
- */
1750
- const allDests = new Set([
1751
- ...transferDestinations,
1752
- ...sequenceDestinations,
1753
- ]);
1754
- if (allDests.size > 0) {
1755
- const edgeType = hasTransferEdges
1756
- ? EdgeType.TRANSFER
1757
- : EdgeType.SEQUENCE;
1758
- for (const dest of allDests) {
1759
- await safeDispatchCustomEvent(GraphEvents.ON_AGENT_TRANSITION, {
1760
- sourceAgentId: agentId,
1761
- sourceAgentName: this.agentContexts.get(agentId)?.name ?? agentId,
1762
- destinationAgentId: dest,
1763
- destinationAgentName: this.agentContexts.get(dest)?.name ?? dest,
1764
- edgeType,
1765
- timestamp: Date.now(),
1766
- }, config);
1767
- }
1768
- }
815
+ /** No special routing needed - return state normally */
1769
816
  return result;
1770
817
  };
1771
818
  /** Wrapped agent as a node with its possible destinations */
@@ -1773,85 +820,36 @@ class MultiAgentGraph extends StandardGraph {
1773
820
  ends: Array.from(allDestinations),
1774
821
  });
1775
822
  }
1776
- /**
1777
- * Add starting edges from START to entry agent(s).
1778
- *
1779
- * Multi-turn resumption: when `resumeFromAgentId` is set and refers to a
1780
- * valid agent in this graph, START routes exclusively to that agent so
1781
- * follow-up messages continue where the previous turn left off.
1782
- *
1783
- * Default behavior (no resume): static edges to all starting nodes,
1784
- * preserving parallel execution for graphs with multiple entry points.
1785
- */
1786
- const validResumeAgent = this.resumeFromAgentId != null &&
1787
- this.agentContexts.has(this.resumeFromAgentId);
1788
- if (validResumeAgent) {
1789
- const resumeAgentId = this.resumeFromAgentId;
1790
- mlog(`[MultiAgentGraph] Multi-turn resumption: routing START → "${resumeAgentId}" (skipping default starting nodes: [${Array.from(this.startingNodes).join(', ')}])`);
1791
- /**
1792
- * Build route map containing both the resume agent and default starting
1793
- * nodes. This is required by LangGraph — all possible destinations must
1794
- * be declared even if the router always picks one.
1795
- */
1796
- const allPossibleStarts = new Set([...this.startingNodes, resumeAgentId]);
1797
- const routeMap = {};
1798
- for (const nodeId of allPossibleStarts) {
1799
- routeMap[nodeId] = nodeId;
1800
- }
1801
- builder.addConditionalEdges(START, () => resumeAgentId, routeMap);
1802
- }
1803
- else {
1804
- if (this.resumeFromAgentId != null) {
1805
- mwarn(`[MultiAgentGraph] resumeFromAgentId "${this.resumeFromAgentId}" not found in graph — falling back to default starting nodes`);
1806
- }
1807
- for (const startNode of this.startingNodes) {
1808
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1809
- /** @ts-ignore */
1810
- builder.addEdge(START, startNode);
1811
- }
823
+ // Add starting edges for all starting nodes
824
+ for (const startNode of this.startingNodes) {
825
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
826
+ /** @ts-ignore */
827
+ builder.addEdge(START, startNode);
1812
828
  }
1813
829
  /**
1814
- * Add approval gate nodes for sequence edges with approvalGate config.
1815
- * Gates are inserted between source and destination agents.
1816
- * They ALWAYS fire regardless of ExecutionContext.
830
+ * Add approval-gate nodes for sequence edges that declare an approvalGate
831
+ * config (Ranger-only feature, not present upstream). Gates fire
832
+ * regardless of ExecutionContext and sit between source and destination.
1817
833
  */
1818
834
  const gatedEdges = new Set();
1819
- for (const edge of this.sequenceEdges) {
1820
- if (!edge.approvalGate) {
835
+ for (const edge of this.directEdges) {
836
+ if (!edge.approvalGate)
1821
837
  continue;
1822
- }
1823
838
  const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1824
839
  const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
1825
840
  for (const source of sources) {
1826
841
  for (const dest of destinations) {
1827
842
  const gateNodeId = getApprovalGateNodeId(edge.approvalGate.gateId);
1828
- const onDeny = edge.approvalGate.onDeny ?? 'stop';
1829
- // Add the gate node
1830
843
  const gateNode = createApprovalGateNode(edge.approvalGate, source, dest);
1831
844
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1832
845
  /** @ts-ignore */
1833
846
  builder.addNode(gateNodeId, gateNode);
1834
- // Wire: source → gate
1835
847
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1836
848
  /** @ts-ignore */
1837
849
  builder.addEdge(source, gateNodeId);
1838
- // Wire: gate → destination (always, since approval is handled
1839
- // by the interrupt/resume mechanism — if denied, the host
1840
- // can choose not to resume, or resume with approved=false
1841
- // and the gate returns empty state)
1842
- if (onDeny === 'skip') {
1843
- // Conditional edge: approved → destination, denied → END
1844
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1845
- /** @ts-ignore */
1846
- builder.addEdge(gateNodeId, dest);
1847
- }
1848
- else {
1849
- // Direct edge to destination — denial stops via non-resume or
1850
- // the host terminates the graph
1851
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1852
- /** @ts-ignore */
1853
- builder.addEdge(gateNodeId, dest);
1854
- }
850
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
851
+ /** @ts-ignore */
852
+ builder.addEdge(gateNodeId, dest);
1855
853
  }
1856
854
  }
1857
855
  gatedEdges.add(edge);
@@ -1859,26 +857,14 @@ class MultiAgentGraph extends StandardGraph {
1859
857
  /**
1860
858
  * Add sequence edges for automatic transitions
1861
859
  * Group edges by destination to handle fan-in scenarios
1862
- * Skip edges that have approval gates (already handled above)
860
+ * (Skip edges that already have an approval gate above.)
1863
861
  */
1864
862
  const edgesByDestination = new Map();
1865
- for (const edge of this.sequenceEdges) {
1866
- if (gatedEdges.has(edge)) {
1867
- continue;
1868
- }
1869
- /**
1870
- * Skip sequence edges where either endpoint lives only inside a scoped
1871
- * handoff subgraph. Those edges are wired inside `buildScopedSubgraph`,
1872
- * not at the top level — adding them here would reference non-existent
1873
- * top-level nodes and fail compilation.
1874
- */
1875
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1876
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
1877
- const anyEndpointHandoffOnly = [...sources, ...dests].some((n) => handoffOnlyDestinations.has(n));
1878
- if (anyEndpointHandoffOnly) {
863
+ for (const edge of this.directEdges) {
864
+ if (gatedEdges.has(edge))
1879
865
  continue;
1880
- }
1881
- for (const destination of dests) {
866
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
867
+ for (const destination of destinations) {
1882
868
  if (!edgesByDestination.has(destination)) {
1883
869
  edgesByDestination.set(destination, []);
1884
870
  }
@@ -1965,18 +951,17 @@ class MultiAgentGraph extends StandardGraph {
1965
951
  for (const edge of edges) {
1966
952
  const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1967
953
  for (const source of sources) {
1968
- /** Check if this source node has both transfer and sequence edges */
1969
- const sourceTransferEdges = this.transferEdges.filter((e) => {
954
+ /** Check if this source node has both handoff and direct edges */
955
+ const sourceHandoffEdges = this.handoffEdges.filter((e) => {
1970
956
  const eSources = Array.isArray(e.from) ? e.from : [e.from];
1971
957
  return eSources.includes(source);
1972
958
  });
1973
- const sourceSequenceEdges = this.sequenceEdges.filter((e) => {
959
+ const sourceDirectEdges = this.directEdges.filter((e) => {
1974
960
  const eSources = Array.isArray(e.from) ? e.from : [e.from];
1975
961
  return eSources.includes(source);
1976
962
  });
1977
963
  /** Skip adding edge if source uses Command routing (has both types) */
1978
- if (sourceTransferEdges.length > 0 &&
1979
- sourceSequenceEdges.length > 0) {
964
+ if (sourceHandoffEdges.length > 0 && sourceDirectEdges.length > 0) {
1980
965
  continue;
1981
966
  }
1982
967
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment