@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.
- package/README.md +62 -0
- package/dist/cjs/agents/AgentContext.cjs +274 -67
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +44 -13
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +182 -5
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +152 -1167
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs +162 -0
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -0
- package/dist/cjs/hooks/executeHooks.cjs +276 -0
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -0
- package/dist/cjs/hooks/matchers.cjs +256 -0
- package/dist/cjs/hooks/matchers.cjs.map +1 -0
- package/dist/cjs/hooks/types.cjs +27 -0
- package/dist/cjs/hooks/types.cjs.map +1 -0
- package/dist/cjs/langchain/google-common.cjs +3 -0
- package/dist/cjs/langchain/google-common.cjs.map +1 -0
- package/dist/cjs/langchain/index.cjs +86 -0
- package/dist/cjs/langchain/index.cjs.map +1 -0
- package/dist/cjs/langchain/language_models/chat_models.cjs +3 -0
- package/dist/cjs/langchain/language_models/chat_models.cjs.map +1 -0
- package/dist/cjs/langchain/messages/tool.cjs +3 -0
- package/dist/cjs/langchain/messages/tool.cjs.map +1 -0
- package/dist/cjs/langchain/messages.cjs +51 -0
- package/dist/cjs/langchain/messages.cjs.map +1 -0
- package/dist/cjs/langchain/openai.cjs +3 -0
- package/dist/cjs/langchain/openai.cjs.map +1 -0
- package/dist/cjs/langchain/prompts.cjs +11 -0
- package/dist/cjs/langchain/prompts.cjs.map +1 -0
- package/dist/cjs/langchain/runnables.cjs +19 -0
- package/dist/cjs/langchain/runnables.cjs.map +1 -0
- package/dist/cjs/langchain/tools.cjs +23 -0
- package/dist/cjs/langchain/tools.cjs.map +1 -0
- package/dist/cjs/langchain/utils/env.cjs +11 -0
- package/dist/cjs/langchain/utils/env.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +5 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/cacheSupport.cjs +55 -0
- package/dist/cjs/llm/bedrock/cacheSupport.cjs.map +1 -0
- package/dist/cjs/llm/bedrock/index.cjs +61 -33
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +0 -3
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +27 -10
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +178 -127
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +89 -0
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/contextPruning.cjs +156 -0
- package/dist/cjs/messages/contextPruning.cjs.map +1 -0
- package/dist/cjs/messages/contextPruningSettings.cjs +53 -0
- package/dist/cjs/messages/contextPruningSettings.cjs.map +1 -0
- package/dist/cjs/messages/format.cjs +144 -20
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/prune.cjs +505 -4
- package/dist/cjs/messages/prune.cjs.map +1 -1
- package/dist/cjs/run.cjs +141 -1
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +235 -0
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +297 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -0
- package/dist/cjs/tools/CodeExecutor.cjs +44 -47
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +16 -11
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ReadFile.cjs +44 -0
- package/dist/cjs/tools/ReadFile.cjs.map +1 -0
- package/dist/cjs/tools/SkillTool.cjs +51 -0
- package/dist/cjs/tools/SkillTool.cjs.map +1 -0
- package/dist/cjs/tools/SubagentTool.cjs +93 -0
- package/dist/cjs/tools/SubagentTool.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +450 -24
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs +11 -3
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tavily-scraper.cjs +189 -0
- package/dist/cjs/tools/search/tavily-scraper.cjs.map +1 -0
- package/dist/cjs/tools/search/tavily-search.cjs +372 -0
- package/dist/cjs/tools/search/tavily-search.cjs.map +1 -0
- package/dist/cjs/tools/search/tool.cjs +28 -4
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +10 -3
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/cjs/tools/skillCatalog.cjs +84 -0
- package/dist/cjs/tools/skillCatalog.cjs.map +1 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +512 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -0
- package/dist/cjs/tools/toolOutputReferences.cjs +670 -0
- package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -0
- package/dist/cjs/types/agent-cache.cjs +53 -0
- package/dist/cjs/types/agent-cache.cjs.map +1 -0
- package/dist/cjs/types/graph.cjs.map +1 -1
- package/dist/cjs/utils/truncation.cjs +135 -0
- package/dist/cjs/utils/truncation.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +274 -67
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +44 -12
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +182 -5
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +155 -1170
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/hooks/HookRegistry.mjs +160 -0
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -0
- package/dist/esm/hooks/executeHooks.mjs +273 -0
- package/dist/esm/hooks/executeHooks.mjs.map +1 -0
- package/dist/esm/hooks/matchers.mjs +251 -0
- package/dist/esm/hooks/matchers.mjs.map +1 -0
- package/dist/esm/hooks/types.mjs +25 -0
- package/dist/esm/hooks/types.mjs.map +1 -0
- package/dist/esm/langchain/google-common.mjs +2 -0
- package/dist/esm/langchain/google-common.mjs.map +1 -0
- package/dist/esm/langchain/index.mjs +5 -0
- package/dist/esm/langchain/language_models/chat_models.mjs +2 -0
- package/dist/esm/langchain/language_models/chat_models.mjs.map +1 -0
- package/dist/esm/langchain/messages/tool.mjs +2 -0
- package/dist/esm/langchain/messages/tool.mjs.map +1 -0
- package/dist/esm/langchain/messages.mjs +2 -0
- package/dist/esm/langchain/messages.mjs.map +1 -0
- package/dist/esm/langchain/openai.mjs +2 -0
- package/dist/esm/langchain/openai.mjs.map +1 -0
- package/dist/esm/langchain/prompts.mjs +2 -0
- package/dist/esm/langchain/prompts.mjs.map +1 -0
- package/dist/esm/langchain/runnables.mjs +2 -0
- package/dist/esm/langchain/runnables.mjs.map +1 -0
- package/dist/esm/langchain/tools.mjs +2 -0
- package/dist/esm/langchain/tools.mjs.map +1 -0
- package/dist/esm/langchain/utils/env.mjs +2 -0
- package/dist/esm/langchain/utils/env.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +5 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/bedrock/cacheSupport.mjs +52 -0
- package/dist/esm/llm/bedrock/cacheSupport.mjs.map +1 -0
- package/dist/esm/llm/bedrock/index.mjs +61 -34
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +0 -3
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +27 -10
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/main.mjs +21 -27
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +89 -0
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/contextPruning.mjs +154 -0
- package/dist/esm/messages/contextPruning.mjs.map +1 -0
- package/dist/esm/messages/contextPruningSettings.mjs +50 -0
- package/dist/esm/messages/contextPruningSettings.mjs.map +1 -0
- package/dist/esm/messages/format.mjs +136 -12
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/prune.mjs +504 -7
- package/dist/esm/messages/prune.mjs.map +1 -1
- package/dist/esm/run.mjs +141 -1
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +227 -0
- package/dist/esm/tools/BashExecutor.mjs.map +1 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +288 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -0
- package/dist/esm/tools/CodeExecutor.mjs +44 -48
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +17 -12
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ReadFile.mjs +39 -0
- package/dist/esm/tools/ReadFile.mjs.map +1 -0
- package/dist/esm/tools/SkillTool.mjs +46 -0
- package/dist/esm/tools/SkillTool.mjs.map +1 -0
- package/dist/esm/tools/SubagentTool.mjs +86 -0
- package/dist/esm/tools/SubagentTool.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +452 -26
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs +11 -3
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tavily-scraper.mjs +186 -0
- package/dist/esm/tools/search/tavily-scraper.mjs.map +1 -0
- package/dist/esm/tools/search/tavily-search.mjs +370 -0
- package/dist/esm/tools/search/tavily-search.mjs.map +1 -0
- package/dist/esm/tools/search/tool.mjs +28 -4
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +10 -3
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/esm/tools/skillCatalog.mjs +82 -0
- package/dist/esm/tools/skillCatalog.mjs.map +1 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +506 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -0
- package/dist/esm/tools/toolOutputReferences.mjs +662 -0
- package/dist/esm/tools/toolOutputReferences.mjs.map +1 -0
- package/dist/esm/types/agent-cache.mjs +51 -0
- package/dist/esm/types/agent-cache.mjs.map +1 -0
- package/dist/esm/types/graph.mjs.map +1 -1
- package/dist/esm/utils/truncation.mjs +128 -0
- package/dist/esm/utils/truncation.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +101 -8
- package/dist/types/common/enum.d.ts +39 -12
- package/dist/types/common/index.d.ts +0 -1
- package/dist/types/graphs/Graph.d.ts +43 -0
- package/dist/types/graphs/MultiAgentGraph.d.ts +26 -150
- package/dist/types/graphs/index.d.ts +0 -1
- package/dist/types/hooks/HookRegistry.d.ts +56 -0
- package/dist/types/hooks/executeHooks.d.ts +79 -0
- package/dist/types/hooks/index.d.ts +6 -0
- package/dist/types/hooks/matchers.d.ts +95 -0
- package/dist/types/hooks/types.d.ts +320 -0
- package/dist/types/index.d.ts +9 -9
- package/dist/types/langchain/google-common.d.ts +1 -0
- package/dist/types/langchain/index.d.ts +8 -0
- package/dist/types/langchain/language_models/chat_models.d.ts +1 -0
- package/dist/types/langchain/messages/tool.d.ts +1 -0
- package/dist/types/langchain/messages.d.ts +2 -0
- package/dist/types/langchain/openai.d.ts +1 -0
- package/dist/types/langchain/prompts.d.ts +1 -0
- package/dist/types/langchain/runnables.d.ts +2 -0
- package/dist/types/langchain/tools.d.ts +2 -0
- package/dist/types/langchain/utils/env.d.ts +1 -0
- package/dist/types/llm/bedrock/cacheSupport.d.ts +35 -0
- package/dist/types/llm/bedrock/index.d.ts +54 -1
- package/dist/types/messages/contextPruning.d.ts +42 -0
- package/dist/types/messages/contextPruningSettings.d.ts +44 -0
- package/dist/types/messages/format.d.ts +9 -1
- package/dist/types/messages/index.d.ts +2 -0
- package/dist/types/messages/prune.d.ts +91 -1
- package/dist/types/run.d.ts +2 -0
- package/dist/types/tools/BashExecutor.d.ts +76 -0
- package/dist/types/tools/BashProgrammaticToolCalling.d.ts +72 -0
- package/dist/types/tools/CodeExecutor.d.ts +8 -26
- package/dist/types/tools/ReadFile.d.ts +28 -0
- package/dist/types/tools/SkillTool.d.ts +40 -0
- package/dist/types/tools/SubagentTool.d.ts +36 -0
- package/dist/types/tools/ToolNode.d.ts +77 -5
- package/dist/types/tools/search/tavily-scraper.d.ts +19 -0
- package/dist/types/tools/search/tavily-search.d.ts +4 -0
- package/dist/types/tools/search/types.d.ts +99 -5
- package/dist/types/tools/search/utils.d.ts +2 -2
- package/dist/types/tools/skillCatalog.d.ts +19 -0
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +137 -0
- package/dist/types/tools/subagent/index.d.ts +2 -0
- package/dist/types/tools/subagent/types.d.ts +84 -0
- package/dist/types/tools/toolOutputReferences.d.ts +236 -0
- package/dist/types/types/agent-cache.d.ts +70 -0
- package/dist/types/types/graph.d.ts +162 -22
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/messages.d.ts +26 -0
- package/dist/types/types/run.d.ts +22 -0
- package/dist/types/types/skill.d.ts +9 -0
- package/dist/types/types/tools.d.ts +111 -0
- package/dist/types/utils/index.d.ts +1 -3
- package/dist/types/utils/truncation.d.ts +70 -0
- package/package.json +57 -17
- package/src/agents/AgentContext.js.map +1 -0
- package/src/agents/AgentContext.test.js.map +1 -0
- package/src/agents/AgentContext.ts +321 -78
- package/src/agents/__tests__/AgentContext.cacheTtl.live.test.ts +259 -0
- package/src/agents/__tests__/AgentContext.crossAgentTier1.live.test.ts +264 -0
- package/src/agents/__tests__/AgentContext.crossUserCache.live.test.ts +342 -0
- package/src/agents/__tests__/AgentContext.test.js.map +1 -0
- package/src/agents/__tests__/AgentContext.test.ts +632 -0
- package/src/agents/__tests__/resolveStructuredOutputMode.test.js.map +1 -0
- package/src/common/__tests__/enum.test.ts +7 -17
- package/src/common/enum.js.map +1 -0
- package/src/common/enum.ts +43 -12
- package/src/common/index.js.map +1 -0
- package/src/common/index.ts +0 -1
- package/src/events.js.map +1 -0
- package/src/graphs/Graph.js.map +1 -0
- package/src/graphs/Graph.ts +222 -2
- package/src/graphs/MultiAgentGraph.js.map +1 -0
- package/src/graphs/MultiAgentGraph.ts +154 -1466
- package/src/graphs/__tests__/MultiAgentGraph.test.ts +91 -0
- package/src/graphs/__tests__/structured-output.integration.test.js.map +1 -0
- package/src/graphs/__tests__/structured-output.test.js.map +1 -0
- package/src/graphs/contextManagement.e2e.test.js.map +1 -0
- package/src/graphs/contextManagement.test.js.map +1 -0
- package/src/graphs/handoffValidation.test.js.map +1 -0
- package/src/graphs/index.js.map +1 -0
- package/src/graphs/index.ts +0 -1
- package/src/hooks/HookRegistry.ts +208 -0
- package/src/hooks/__tests__/HookRegistry.test.ts +190 -0
- package/src/hooks/__tests__/compactHooks.test.ts +214 -0
- package/src/hooks/__tests__/executeHooks.test.ts +1013 -0
- package/src/hooks/__tests__/integration.test.ts +337 -0
- package/src/hooks/__tests__/matchers.test.ts +238 -0
- package/src/hooks/__tests__/toolHooks.test.ts +665 -0
- package/src/hooks/executeHooks.ts +375 -0
- package/src/hooks/index.ts +57 -0
- package/src/hooks/matchers.ts +280 -0
- package/src/hooks/types.ts +404 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +15 -24
- package/src/instrumentation.js.map +1 -0
- package/src/langchain/google-common.ts +1 -0
- package/src/langchain/index.ts +8 -0
- package/src/langchain/language_models/chat_models.ts +1 -0
- package/src/langchain/messages/tool.ts +5 -0
- package/src/langchain/messages.ts +21 -0
- package/src/langchain/openai.ts +1 -0
- package/src/langchain/prompts.ts +1 -0
- package/src/langchain/runnables.ts +7 -0
- package/src/langchain/tools.ts +8 -0
- package/src/langchain/utils/env.ts +1 -0
- package/src/llm/anthropic/index.js.map +1 -0
- package/src/llm/anthropic/types.js.map +1 -0
- package/src/llm/anthropic/utils/message_inputs.js.map +1 -0
- package/src/llm/anthropic/utils/message_inputs.ts +10 -1
- package/src/llm/anthropic/utils/message_outputs.js.map +1 -0
- package/src/llm/anthropic/utils/output_parsers.js.map +1 -0
- package/src/llm/anthropic/utils/server-tool-inputs.test.ts +436 -0
- package/src/llm/anthropic/utils/tools.js.map +1 -0
- package/src/llm/bedrock/__tests__/bedrock-caching.test.js.map +1 -0
- package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +166 -18
- package/src/llm/bedrock/cacheSupport.test.ts +99 -0
- package/src/llm/bedrock/cacheSupport.ts +53 -0
- package/src/llm/bedrock/index.js.map +1 -0
- package/src/llm/bedrock/index.ts +116 -41
- package/src/llm/bedrock/types.js.map +1 -0
- package/src/llm/bedrock/utils/index.js.map +1 -0
- package/src/llm/bedrock/utils/message_inputs.js.map +1 -0
- package/src/llm/bedrock/utils/message_outputs.js.map +1 -0
- package/src/llm/fake.js.map +1 -0
- package/src/llm/google/index.js.map +1 -0
- package/src/llm/google/types.js.map +1 -0
- package/src/llm/google/utils/common.js.map +1 -0
- package/src/llm/google/utils/tools.js.map +1 -0
- package/src/llm/google/utils/zod_to_genai_parameters.js.map +1 -0
- package/src/llm/openai/index.js.map +1 -0
- package/src/llm/openai/types.js.map +1 -0
- package/src/llm/openai/utils/index.js.map +1 -0
- package/src/llm/openai/utils/index.ts +31 -14
- package/src/llm/openai/utils/isReasoningModel.test.js.map +1 -0
- package/src/llm/openrouter/index.js.map +1 -0
- package/src/llm/openrouter/reasoning.test.js.map +1 -0
- package/src/llm/providers.js.map +1 -0
- package/src/llm/text.js.map +1 -0
- package/src/llm/vertexai/index.js.map +1 -0
- package/src/messages/__tests__/contextPruning.test.ts +228 -0
- package/src/messages/__tests__/tools.test.js.map +1 -0
- package/src/messages/cache.js.map +1 -0
- package/src/messages/cache.test.js.map +1 -0
- package/src/messages/cache.test.ts +62 -24
- package/src/messages/cache.ts +112 -0
- package/src/messages/content.js.map +1 -0
- package/src/messages/content.test.js.map +1 -0
- package/src/messages/contextPruning.ts +191 -0
- package/src/messages/contextPruningSettings.ts +90 -0
- package/src/messages/core.js.map +1 -0
- package/src/messages/ensureThinkingBlock.test.js.map +1 -0
- package/src/messages/format.js.map +1 -0
- package/src/messages/format.ts +164 -12
- package/src/messages/formatAgentMessages.skills.test.ts +413 -0
- package/src/messages/formatAgentMessages.test.js.map +1 -0
- package/src/messages/formatAgentMessages.tools.test.js.map +1 -0
- package/src/messages/formatMessage.test.js.map +1 -0
- package/src/messages/ids.js.map +1 -0
- package/src/messages/index.js.map +1 -0
- package/src/messages/index.ts +2 -0
- package/src/messages/labelContentByAgent.test.js.map +1 -0
- package/src/messages/prune.js.map +1 -0
- package/src/messages/prune.ts +661 -4
- package/src/messages/reducer.js.map +1 -0
- package/src/messages/shiftIndexTokenCountMap.test.js.map +1 -0
- package/src/messages/summarize.js.map +1 -0
- package/src/messages/summarize.test.js.map +1 -0
- package/src/messages/tools.js.map +1 -0
- package/src/mockStream.js.map +1 -0
- package/src/prompts/collab.js.map +1 -0
- package/src/prompts/index.js.map +1 -0
- package/src/prompts/taskmanager.js.map +1 -0
- package/src/run.js.map +1 -0
- package/src/run.ts +155 -1
- package/src/schemas/index.js.map +1 -0
- package/src/schemas/schema-preparation.test.js.map +1 -0
- package/src/schemas/validate.js.map +1 -0
- package/src/schemas/validate.test.js.map +1 -0
- package/src/scripts/abort.js.map +1 -0
- package/src/scripts/ant_web_search.js.map +1 -0
- package/src/scripts/ant_web_search_edge_case.js.map +1 -0
- package/src/scripts/ant_web_search_error_edge_case.js.map +1 -0
- package/src/scripts/args.js.map +1 -0
- package/src/scripts/bedrock-cache-debug.js.map +1 -0
- package/src/scripts/bedrock-content-aggregation-test.js.map +1 -0
- package/src/scripts/bedrock-merge-test.js.map +1 -0
- package/src/scripts/bedrock-parallel-tools-test.js.map +1 -0
- package/src/scripts/caching.js.map +1 -0
- package/src/scripts/cli.js.map +1 -0
- package/src/scripts/cli2.js.map +1 -0
- package/src/scripts/cli3.js.map +1 -0
- package/src/scripts/cli4.js.map +1 -0
- package/src/scripts/cli5.js.map +1 -0
- package/src/scripts/code_exec.js.map +1 -0
- package/src/scripts/code_exec_files.js.map +1 -0
- package/src/scripts/code_exec_multi_session.js.map +1 -0
- package/src/scripts/code_exec_ptc.js.map +1 -0
- package/src/scripts/code_exec_session.js.map +1 -0
- package/src/scripts/code_exec_simple.js.map +1 -0
- package/src/scripts/content.js.map +1 -0
- package/src/scripts/empty_input.js.map +1 -0
- package/src/scripts/handoff-test.js.map +1 -0
- package/src/scripts/image.js.map +1 -0
- package/src/scripts/memory.js.map +1 -0
- package/src/scripts/multi-agent-chain.js.map +1 -0
- package/src/scripts/multi-agent-chain.ts +2 -2
- package/src/scripts/multi-agent-conditional.js.map +1 -0
- package/src/scripts/multi-agent-document-review-chain.js.map +1 -0
- package/src/scripts/multi-agent-document-review-chain.ts +2 -2
- package/src/scripts/multi-agent-hybrid-flow.js.map +1 -0
- package/src/scripts/multi-agent-hybrid-flow.ts +4 -4
- package/src/scripts/multi-agent-parallel-start.js.map +1 -0
- package/src/scripts/multi-agent-parallel.js.map +1 -0
- package/src/scripts/multi-agent-parallel.ts +3 -3
- package/src/scripts/multi-agent-sequence.js.map +1 -0
- package/src/scripts/multi-agent-sequence.ts +3 -3
- package/src/scripts/multi-agent-subagent.ts +246 -0
- package/src/scripts/multi-agent-supervisor.js.map +1 -0
- package/src/scripts/multi-agent-supervisor.ts +5 -5
- package/src/scripts/multi-agent-test.js.map +1 -0
- package/src/scripts/parallel-asymmetric-tools-test.js.map +1 -0
- package/src/scripts/parallel-full-metadata-test.js.map +1 -0
- package/src/scripts/parallel-tools-test.js.map +1 -0
- package/src/scripts/poc-multi-agent-comprehensive.ts +8 -8
- package/src/scripts/programmatic_exec.js.map +1 -0
- package/src/scripts/programmatic_exec_agent.js.map +1 -0
- package/src/scripts/search.js.map +1 -0
- package/src/scripts/sequential-full-metadata-test.js.map +1 -0
- package/src/scripts/sequential-full-metadata-test.ts +2 -2
- package/src/scripts/simple.js.map +1 -0
- package/src/scripts/single-agent-metadata-test.js.map +1 -0
- package/src/scripts/stream.js.map +1 -0
- package/src/scripts/subagent-event-driven-debug.ts +190 -0
- package/src/scripts/subagent-tools-debug.ts +160 -0
- package/src/scripts/test-custom-prompt-key.js.map +1 -0
- package/src/scripts/test-custom-prompt-key.ts +3 -3
- package/src/scripts/test-handoff-input.js.map +1 -0
- package/src/scripts/test-handoff-input.ts +1 -1
- package/src/scripts/test-handoff-preamble.js.map +1 -0
- package/src/scripts/test-handoff-steering.js.map +1 -0
- package/src/scripts/test-handoff-steering.ts +3 -3
- package/src/scripts/test-multi-agent-list-handoff.js.map +1 -0
- package/src/scripts/test-multi-agent-list-handoff.ts +1 -1
- package/src/scripts/test-parallel-agent-labeling.js.map +1 -0
- package/src/scripts/test-parallel-agent-labeling.ts +3 -3
- package/src/scripts/test-parallel-handoffs.js.map +1 -0
- package/src/scripts/test-parallel-handoffs.ts +2 -2
- package/src/scripts/test-thinking-handoff-bedrock.js.map +1 -0
- package/src/scripts/test-thinking-handoff-bedrock.ts +1 -1
- package/src/scripts/test-thinking-handoff.js.map +1 -0
- package/src/scripts/test-thinking-handoff.ts +1 -1
- package/src/scripts/test-thinking-to-thinking-handoff-bedrock.js.map +1 -0
- package/src/scripts/test-thinking-to-thinking-handoff-bedrock.ts +1 -1
- package/src/scripts/test-tool-before-handoff-role-order.js.map +1 -0
- package/src/scripts/test-tool-before-handoff-role-order.ts +1 -1
- package/src/scripts/test-tools-before-handoff.js.map +1 -0
- package/src/scripts/test-tools-before-handoff.ts +1 -1
- package/src/scripts/test_code_api.js.map +1 -0
- package/src/scripts/thinking-bedrock.js.map +1 -0
- package/src/scripts/thinking-vertexai.js.map +1 -0
- package/src/scripts/thinking.js.map +1 -0
- package/src/scripts/tool_search.js.map +1 -0
- package/src/scripts/tools.js.map +1 -0
- package/src/specs/agent-handoffs-bedrock.integration.test.js.map +1 -0
- package/src/specs/agent-handoffs.test.js.map +1 -0
- package/src/specs/agent-handoffs.test.ts +26 -483
- package/src/specs/anthropic.simple.test.js.map +1 -0
- package/src/specs/anthropic.simple.test.ts +61 -0
- package/src/specs/azure.simple.test.js.map +1 -0
- package/src/specs/cache.simple.test.js.map +1 -0
- package/src/specs/custom-event-await.test.js.map +1 -0
- package/src/specs/deepseek.simple.test.js.map +1 -0
- package/src/specs/emergency-prune.test.js.map +1 -0
- package/src/specs/moonshot.simple.test.js.map +1 -0
- package/src/specs/multi-agent-summarization.test.ts +396 -0
- package/src/specs/observability.integration.test.js.map +1 -0
- package/src/specs/openai.simple.test.js.map +1 -0
- package/src/specs/openrouter.simple.test.js.map +1 -0
- package/src/specs/prune.orphans.test.ts +248 -0
- package/src/specs/prune.test.js.map +1 -0
- package/src/specs/prune.test.ts +104 -16
- package/src/specs/reasoning.test.js.map +1 -0
- package/src/specs/spec.utils.js.map +1 -0
- package/src/specs/thinking-handoff.test.js.map +1 -0
- package/src/specs/thinking-handoff.test.ts +19 -19
- package/src/specs/thinking-prune.test.js.map +1 -0
- package/src/specs/token-distribution-edge-case.test.js.map +1 -0
- package/src/specs/token-memoization.test.js.map +1 -0
- package/src/specs/tokens.test.js.map +1 -0
- package/src/specs/tool-error.test.js.map +1 -0
- package/src/splitStream.js.map +1 -0
- package/src/splitStream.test.js.map +1 -0
- package/src/stream.js.map +1 -0
- package/src/stream.test.js.map +1 -0
- package/src/test/mockTools.js.map +1 -0
- package/src/tools/BashExecutor.ts +281 -0
- package/src/tools/BashProgrammaticToolCalling.ts +397 -0
- package/src/tools/BrowserTools.js.map +1 -0
- package/src/tools/Calculator.js.map +1 -0
- package/src/tools/Calculator.test.js.map +1 -0
- package/src/tools/CodeExecutor.js.map +1 -0
- package/src/tools/CodeExecutor.ts +62 -54
- package/src/tools/ProgrammaticToolCalling.js.map +1 -0
- package/src/tools/ProgrammaticToolCalling.ts +29 -14
- package/src/tools/ReadFile.ts +39 -0
- package/src/tools/SkillTool.ts +46 -0
- package/src/tools/StreamingToolCallBuffer.js.map +1 -0
- package/src/tools/SubagentTool.ts +100 -0
- package/src/tools/ToolNode.js.map +1 -0
- package/src/tools/ToolNode.ts +548 -26
- package/src/tools/ToolSearch.js.map +1 -0
- package/src/tools/__tests__/BashExecutor.test.ts +49 -0
- package/src/tools/__tests__/BrowserTools.test.js.map +1 -0
- package/src/tools/__tests__/CodeExecutor.test.ts +37 -36
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.js.map +1 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.test.js.map +1 -0
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +60 -0
- package/src/tools/__tests__/ReadFile.test.ts +44 -0
- package/src/tools/__tests__/SkillTool.test.ts +442 -0
- package/src/tools/__tests__/StreamingToolCallBuffer.test.js.map +1 -0
- package/src/tools/__tests__/SubagentExecutor.test.ts +1148 -0
- package/src/tools/__tests__/SubagentTool.test.ts +149 -0
- package/src/tools/__tests__/ToolApproval.test.js.map +1 -0
- package/src/tools/__tests__/ToolNode.outputReferences.test.ts +1438 -0
- package/src/tools/__tests__/ToolNode.recovery.test.js.map +1 -0
- package/src/tools/__tests__/ToolNode.session.test.js.map +1 -0
- package/src/tools/__tests__/ToolSearch.integration.test.js.map +1 -0
- package/src/tools/__tests__/ToolSearch.test.js.map +1 -0
- package/src/tools/__tests__/annotateMessagesForLLM.test.ts +479 -0
- package/src/tools/__tests__/handlers.test.js.map +1 -0
- package/src/tools/__tests__/skillCatalog.test.ts +161 -0
- package/src/tools/__tests__/subagentHooks.test.ts +210 -0
- package/src/tools/__tests__/toolOutputReferences.test.ts +415 -0
- package/src/tools/__tests__/truncation-recovery.integration.test.js.map +1 -0
- package/src/tools/handlers.js.map +1 -0
- package/src/tools/schema.js.map +1 -0
- package/src/tools/search/anthropic.js.map +1 -0
- package/src/tools/search/content.js.map +1 -0
- package/src/tools/search/content.test.js.map +1 -0
- package/src/tools/search/firecrawl.js.map +1 -0
- package/src/tools/search/format.js.map +1 -0
- package/src/tools/search/highlights.js.map +1 -0
- package/src/tools/search/index.js.map +1 -0
- package/src/tools/search/jina-reranker.test.js.map +1 -0
- package/src/tools/search/rerankers.js.map +1 -0
- package/src/tools/search/schema.js.map +1 -0
- package/src/tools/search/search.js.map +1 -0
- package/src/tools/search/search.ts +12 -2
- package/src/tools/search/serper-scraper.js.map +1 -0
- package/src/tools/search/tavily-scraper.ts +235 -0
- package/src/tools/search/tavily-search.ts +424 -0
- package/src/tools/search/tavily.test.ts +965 -0
- package/src/tools/search/test.js.map +1 -0
- package/src/tools/search/tool.js.map +1 -0
- package/src/tools/search/tool.ts +36 -2
- package/src/tools/search/types.js.map +1 -0
- package/src/tools/search/types.ts +133 -8
- package/src/tools/search/utils.js.map +1 -0
- package/src/tools/search/utils.ts +13 -5
- package/src/tools/skillCatalog.ts +126 -0
- package/src/tools/subagent/SubagentExecutor.ts +676 -0
- package/src/tools/subagent/index.ts +13 -0
- package/src/tools/subagent/types.test.ts +70 -0
- package/src/tools/subagent/types.ts +115 -0
- package/src/tools/toolOutputReferences.ts +825 -0
- package/src/types/agent-cache.ts +73 -0
- package/src/types/graph.js.map +1 -0
- package/src/types/graph.test.js.map +1 -0
- package/src/types/graph.ts +171 -20
- package/src/types/index.js.map +1 -0
- package/src/types/index.ts +3 -0
- package/src/types/llm.js.map +1 -0
- package/src/types/messages.js.map +1 -0
- package/src/types/messages.ts +27 -0
- package/src/types/run.js.map +1 -0
- package/src/types/run.ts +22 -0
- package/src/types/skill.ts +11 -0
- package/src/types/stream.js.map +1 -0
- package/src/types/tools.js.map +1 -0
- package/src/types/tools.ts +118 -0
- package/src/utils/__tests__/truncation.test.ts +66 -0
- package/src/utils/contextAnalytics.js.map +1 -0
- package/src/utils/contextAnalytics.test.js.map +1 -0
- package/src/utils/events.js.map +1 -0
- package/src/utils/graph.js.map +1 -0
- package/src/utils/handlers.js.map +1 -0
- package/src/utils/index.js.map +1 -0
- package/src/utils/index.ts +1 -3
- package/src/utils/llm.js.map +1 -0
- package/src/utils/llmConfig.js.map +1 -0
- package/src/utils/logging.js.map +1 -0
- package/src/utils/misc.js.map +1 -0
- package/src/utils/run.js.map +1 -0
- package/src/utils/schema.js.map +1 -0
- package/src/utils/title.js.map +1 -0
- package/src/utils/tokens.js.map +1 -0
- package/src/utils/toonFormat.js.map +1 -0
- package/src/utils/truncation.ts +154 -0
- package/dist/cjs/common/spawnPath.cjs +0 -104
- package/dist/cjs/common/spawnPath.cjs.map +0 -1
- package/dist/cjs/content/ArtifactStore.cjs +0 -579
- package/dist/cjs/content/ArtifactStore.cjs.map +0 -1
- package/dist/cjs/content/ContentStore.cjs +0 -638
- package/dist/cjs/content/ContentStore.cjs.map +0 -1
- package/dist/cjs/content/contentAnalyzer.cjs +0 -91
- package/dist/cjs/content/contentAnalyzer.cjs.map +0 -1
- package/dist/cjs/content/index.cjs +0 -20
- package/dist/cjs/content/index.cjs.map +0 -1
- package/dist/cjs/content/mcpAutoCache.cjs +0 -115
- package/dist/cjs/content/mcpAutoCache.cjs.map +0 -1
- package/dist/cjs/graphs/HandoffRegistry.cjs +0 -143
- package/dist/cjs/graphs/HandoffRegistry.cjs.map +0 -1
- package/dist/cjs/providers/a2a/A2ACapabilityProvider.cjs +0 -288
- package/dist/cjs/providers/a2a/A2ACapabilityProvider.cjs.map +0 -1
- package/dist/cjs/providers/a2a/client.cjs +0 -92
- package/dist/cjs/providers/a2a/client.cjs.map +0 -1
- package/dist/cjs/providers/a2a/config.cjs +0 -38
- package/dist/cjs/providers/a2a/config.cjs.map +0 -1
- package/dist/cjs/providers/capabilityNaming.cjs +0 -43
- package/dist/cjs/providers/capabilityNaming.cjs.map +0 -1
- package/dist/cjs/providers/mcp/MCPCapabilityProvider.cjs +0 -244
- package/dist/cjs/providers/mcp/MCPCapabilityProvider.cjs.map +0 -1
- package/dist/cjs/providers/mcp/config.cjs +0 -42
- package/dist/cjs/providers/mcp/config.cjs.map +0 -1
- package/dist/cjs/providers/mcp/transport.cjs +0 -65
- package/dist/cjs/providers/mcp/transport.cjs.map +0 -1
- package/dist/cjs/providers/tools-server/ToolsServerCapabilityProvider.cjs +0 -128
- package/dist/cjs/providers/tools-server/ToolsServerCapabilityProvider.cjs.map +0 -1
- package/dist/cjs/providers/types.cjs +0 -51
- package/dist/cjs/providers/types.cjs.map +0 -1
- package/dist/cjs/tools/artifacts/schema.cjs +0 -86
- package/dist/cjs/tools/artifacts/schema.cjs.map +0 -1
- package/dist/cjs/tools/artifacts/tool.cjs +0 -219
- package/dist/cjs/tools/artifacts/tool.cjs.map +0 -1
- package/dist/cjs/tools/fileSearch/formatter.cjs +0 -93
- package/dist/cjs/tools/fileSearch/formatter.cjs.map +0 -1
- package/dist/cjs/tools/fileSearch/ragClient.cjs +0 -102
- package/dist/cjs/tools/fileSearch/ragClient.cjs.map +0 -1
- package/dist/cjs/tools/fileSearch/schema.cjs +0 -18
- package/dist/cjs/tools/fileSearch/schema.cjs.map +0 -1
- package/dist/cjs/tools/fileSearch/tool.cjs +0 -155
- package/dist/cjs/tools/fileSearch/tool.cjs.map +0 -1
- package/dist/cjs/tools/proxyTool.cjs +0 -102
- package/dist/cjs/tools/proxyTool.cjs.map +0 -1
- package/dist/cjs/utils/childAgentContext.cjs +0 -242
- package/dist/cjs/utils/childAgentContext.cjs.map +0 -1
- package/dist/cjs/utils/credentials.cjs +0 -142
- package/dist/cjs/utils/credentials.cjs.map +0 -1
- package/dist/cjs/utils/httpClient.cjs +0 -74
- package/dist/cjs/utils/httpClient.cjs.map +0 -1
- package/dist/cjs/utils/toolManifest.cjs +0 -100
- package/dist/cjs/utils/toolManifest.cjs.map +0 -1
- package/dist/esm/common/spawnPath.mjs +0 -95
- package/dist/esm/common/spawnPath.mjs.map +0 -1
- package/dist/esm/content/ArtifactStore.mjs +0 -576
- package/dist/esm/content/ArtifactStore.mjs.map +0 -1
- package/dist/esm/content/ContentStore.mjs +0 -635
- package/dist/esm/content/ContentStore.mjs.map +0 -1
- package/dist/esm/content/contentAnalyzer.mjs +0 -87
- package/dist/esm/content/contentAnalyzer.mjs.map +0 -1
- package/dist/esm/content/index.mjs +0 -5
- package/dist/esm/content/mcpAutoCache.mjs +0 -111
- package/dist/esm/content/mcpAutoCache.mjs.map +0 -1
- package/dist/esm/graphs/HandoffRegistry.mjs +0 -141
- package/dist/esm/graphs/HandoffRegistry.mjs.map +0 -1
- package/dist/esm/providers/a2a/A2ACapabilityProvider.mjs +0 -281
- package/dist/esm/providers/a2a/A2ACapabilityProvider.mjs.map +0 -1
- package/dist/esm/providers/a2a/client.mjs +0 -88
- package/dist/esm/providers/a2a/client.mjs.map +0 -1
- package/dist/esm/providers/a2a/config.mjs +0 -35
- package/dist/esm/providers/a2a/config.mjs.map +0 -1
- package/dist/esm/providers/capabilityNaming.mjs +0 -39
- package/dist/esm/providers/capabilityNaming.mjs.map +0 -1
- package/dist/esm/providers/mcp/MCPCapabilityProvider.mjs +0 -240
- package/dist/esm/providers/mcp/MCPCapabilityProvider.mjs.map +0 -1
- package/dist/esm/providers/mcp/config.mjs +0 -39
- package/dist/esm/providers/mcp/config.mjs.map +0 -1
- package/dist/esm/providers/mcp/transport.mjs +0 -63
- package/dist/esm/providers/mcp/transport.mjs.map +0 -1
- package/dist/esm/providers/tools-server/ToolsServerCapabilityProvider.mjs +0 -126
- package/dist/esm/providers/tools-server/ToolsServerCapabilityProvider.mjs.map +0 -1
- package/dist/esm/providers/types.mjs +0 -51
- package/dist/esm/providers/types.mjs.map +0 -1
- package/dist/esm/tools/artifacts/schema.mjs +0 -79
- package/dist/esm/tools/artifacts/schema.mjs.map +0 -1
- package/dist/esm/tools/artifacts/tool.mjs +0 -213
- package/dist/esm/tools/artifacts/tool.mjs.map +0 -1
- package/dist/esm/tools/fileSearch/formatter.mjs +0 -90
- package/dist/esm/tools/fileSearch/formatter.mjs.map +0 -1
- package/dist/esm/tools/fileSearch/ragClient.mjs +0 -98
- package/dist/esm/tools/fileSearch/ragClient.mjs.map +0 -1
- package/dist/esm/tools/fileSearch/schema.mjs +0 -15
- package/dist/esm/tools/fileSearch/schema.mjs.map +0 -1
- package/dist/esm/tools/fileSearch/tool.mjs +0 -152
- package/dist/esm/tools/fileSearch/tool.mjs.map +0 -1
- package/dist/esm/tools/proxyTool.mjs +0 -100
- package/dist/esm/tools/proxyTool.mjs.map +0 -1
- package/dist/esm/utils/childAgentContext.mjs +0 -237
- package/dist/esm/utils/childAgentContext.mjs.map +0 -1
- package/dist/esm/utils/credentials.mjs +0 -135
- package/dist/esm/utils/credentials.mjs.map +0 -1
- package/dist/esm/utils/httpClient.mjs +0 -70
- package/dist/esm/utils/httpClient.mjs.map +0 -1
- package/dist/esm/utils/toolManifest.mjs +0 -96
- package/dist/esm/utils/toolManifest.mjs.map +0 -1
- package/dist/types/common/spawnPath.d.ts +0 -59
- package/dist/types/content/ArtifactStore.d.ts +0 -223
- package/dist/types/content/ContentStore.d.ts +0 -140
- package/dist/types/content/contentAnalyzer.d.ts +0 -38
- package/dist/types/content/index.d.ts +0 -24
- package/dist/types/content/mcpAutoCache.d.ts +0 -89
- package/dist/types/content/types.d.ts +0 -75
- package/dist/types/graphs/HandoffRegistry.d.ts +0 -97
- package/dist/types/providers/a2a/A2ACapabilityProvider.d.ts +0 -89
- package/dist/types/providers/a2a/client.d.ts +0 -47
- package/dist/types/providers/a2a/config.d.ts +0 -18
- package/dist/types/providers/a2a/index.d.ts +0 -6
- package/dist/types/providers/a2a/types.d.ts +0 -173
- package/dist/types/providers/capabilityNaming.d.ts +0 -25
- package/dist/types/providers/index.d.ts +0 -12
- package/dist/types/providers/mcp/MCPCapabilityProvider.d.ts +0 -54
- package/dist/types/providers/mcp/config.d.ts +0 -20
- package/dist/types/providers/mcp/index.d.ts +0 -5
- package/dist/types/providers/mcp/transport.d.ts +0 -18
- package/dist/types/providers/mcp/types.d.ts +0 -112
- package/dist/types/providers/tools-server/ToolsServerCapabilityProvider.d.ts +0 -59
- package/dist/types/providers/tools-server/index.d.ts +0 -1
- package/dist/types/providers/types.d.ts +0 -184
- package/dist/types/tools/artifacts/index.d.ts +0 -3
- package/dist/types/tools/artifacts/schema.d.ts +0 -63
- package/dist/types/tools/artifacts/tool.d.ts +0 -16
- package/dist/types/tools/artifacts/types.d.ts +0 -127
- package/dist/types/tools/fileSearch/formatter.d.ts +0 -25
- package/dist/types/tools/fileSearch/index.d.ts +0 -5
- package/dist/types/tools/fileSearch/ragClient.d.ts +0 -32
- package/dist/types/tools/fileSearch/schema.d.ts +0 -13
- package/dist/types/tools/fileSearch/tool.d.ts +0 -18
- package/dist/types/tools/fileSearch/types.d.ts +0 -139
- package/dist/types/tools/proxyTool.d.ts +0 -62
- package/dist/types/utils/childAgentContext.d.ts +0 -99
- package/dist/types/utils/credentials.d.ts +0 -77
- package/dist/types/utils/httpClient.d.ts +0 -46
- package/dist/types/utils/toolManifest.d.ts +0 -49
- package/src/common/__tests__/spawnPath.test.ts +0 -110
- package/src/common/spawnPath.ts +0 -101
- package/src/content/ArtifactStore.ts +0 -782
- package/src/content/ContentStore.ts +0 -753
- package/src/content/contentAnalyzer.ts +0 -105
- package/src/content/index.ts +0 -51
- package/src/content/mcpAutoCache.ts +0 -185
- package/src/content/types.ts +0 -82
- package/src/graphs/HandoffRegistry.ts +0 -199
- package/src/graphs/__tests__/HandoffRegistry.test.ts +0 -410
- package/src/graphs/__tests__/multi-agent-delegate.test.ts +0 -458
- package/src/graphs/__tests__/multi-agent-edges.test.ts +0 -276
- package/src/graphs/__tests__/multi-agent-nested-subgraph.test.ts +0 -221
- package/src/graphs/handoffValidation.test.ts +0 -353
- package/src/providers/__tests__/ToolsServerCapabilityProvider.integration.spec.ts +0 -79
- package/src/providers/__tests__/ToolsServerCapabilityProvider.test.ts +0 -271
- package/src/providers/__tests__/types.test.ts +0 -64
- package/src/providers/a2a/A2ACapabilityProvider.ts +0 -384
- package/src/providers/a2a/__tests__/A2ACapabilityProvider.integration.spec.ts +0 -345
- package/src/providers/a2a/__tests__/A2ACapabilityProvider.test.ts +0 -460
- package/src/providers/a2a/client.ts +0 -115
- package/src/providers/a2a/config.ts +0 -40
- package/src/providers/a2a/index.ts +0 -29
- package/src/providers/a2a/types.ts +0 -191
- package/src/providers/capabilityNaming.ts +0 -42
- package/src/providers/index.ts +0 -68
- package/src/providers/mcp/MCPCapabilityProvider.ts +0 -345
- package/src/providers/mcp/__tests__/MCPCapabilityProvider.integration.spec.ts +0 -386
- package/src/providers/mcp/__tests__/MCPCapabilityProvider.test.ts +0 -371
- package/src/providers/mcp/config.ts +0 -45
- package/src/providers/mcp/index.ts +0 -21
- package/src/providers/mcp/transport.ts +0 -76
- package/src/providers/mcp/types.ts +0 -139
- package/src/providers/tools-server/ToolsServerCapabilityProvider.ts +0 -249
- package/src/providers/tools-server/index.ts +0 -1
- package/src/providers/types.ts +0 -204
- package/src/scripts/test-bedrock-handoff-autonomous.ts +0 -267
- package/src/scripts/test-handoff-preamble.ts +0 -278
- package/src/specs/agent-handoffs-bedrock.integration.test.ts +0 -415
- package/src/tools/artifacts/__tests__/tool.test.ts +0 -259
- package/src/tools/artifacts/index.ts +0 -33
- package/src/tools/artifacts/schema.ts +0 -99
- package/src/tools/artifacts/tool.ts +0 -289
- package/src/tools/artifacts/types.ts +0 -162
- package/src/tools/fileSearch/__tests__/tool.test.ts +0 -261
- package/src/tools/fileSearch/formatter.ts +0 -129
- package/src/tools/fileSearch/index.ts +0 -23
- package/src/tools/fileSearch/ragClient.ts +0 -137
- package/src/tools/fileSearch/schema.ts +0 -19
- package/src/tools/fileSearch/tool.ts +0 -207
- package/src/tools/fileSearch/types.ts +0 -149
- package/src/tools/proxyTool.ts +0 -166
- package/src/utils/__tests__/childAgentContext.test.ts +0 -217
- package/src/utils/__tests__/credentials.test.ts +0 -130
- package/src/utils/__tests__/httpClient.test.ts +0 -75
- package/src/utils/__tests__/toolManifest.test.ts +0 -116
- package/src/utils/childAgentContext.ts +0 -259
- package/src/utils/credentials.ts +0 -157
- package/src/utils/httpClient.ts +0 -92
- package/src/utils/toolManifest.ts +0 -109
- /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,
|
|
4
|
-
import { getCurrentTaskInput, Command, Annotation, messagesStateReducer, StateGraph,
|
|
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 {
|
|
14
|
-
import
|
|
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
|
|
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
|
|
28
|
-
* - Agents with ONLY
|
|
29
|
-
* - Agents with BOTH: Use Command for exclusive routing (
|
|
30
|
-
* - If
|
|
31
|
-
* - If no
|
|
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
|
|
34
|
-
* OR continues its workflow (
|
|
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
|
-
|
|
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.
|
|
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
|
-
*
|
|
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
|
-
|
|
61
|
+
validateEdgeAgents() {
|
|
62
|
+
const known = new Set(this.agentContexts.keys());
|
|
63
|
+
const unknown = new Set();
|
|
92
64
|
for (const edge of this.edges) {
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
97
|
-
|
|
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.
|
|
100
|
-
edge
|
|
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
|
|
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
|
|
109
|
-
this.
|
|
103
|
+
// Fan-out pattern defaults to direct
|
|
104
|
+
this.directEdges.push(edge);
|
|
110
105
|
}
|
|
111
106
|
else {
|
|
112
|
-
// Everything else defaults to
|
|
113
|
-
this.
|
|
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
|
|
176
|
-
for (const edge of this.
|
|
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
|
|
204
|
-
for (const edge of
|
|
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
|
|
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
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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 (!
|
|
256
|
-
|
|
241
|
+
if (!handoffsByAgent.has(source)) {
|
|
242
|
+
handoffsByAgent.set(source, []);
|
|
257
243
|
}
|
|
258
|
-
|
|
244
|
+
handoffsByAgent.get(source).push(edge);
|
|
259
245
|
});
|
|
260
246
|
}
|
|
261
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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(...
|
|
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
|
|
294
|
-
*
|
|
295
|
-
* @param
|
|
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
|
-
|
|
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
|
|
308
|
-
const
|
|
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 (
|
|
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:
|
|
326
|
+
schema: hasHandoffInput
|
|
358
327
|
? {
|
|
359
328
|
type: 'object',
|
|
360
329
|
properties: {
|
|
361
330
|
[promptKey]: {
|
|
362
331
|
type: 'string',
|
|
363
|
-
description:
|
|
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
|
|
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
|
|
381
|
-
const
|
|
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 (
|
|
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:
|
|
434
|
+
schema: hasHandoffInput
|
|
468
435
|
? {
|
|
469
436
|
type: 'object',
|
|
470
437
|
properties: {
|
|
471
438
|
[promptKey]: {
|
|
472
439
|
type: 'string',
|
|
473
|
-
description:
|
|
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
|
-
|
|
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
|
|
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(
|
|
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:
|
|
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
|
|
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
|
|
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
|
|
1444
|
-
const
|
|
1445
|
-
// Check
|
|
1446
|
-
for (const edge of this.
|
|
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) =>
|
|
632
|
+
dests.forEach((dest) => handoffDestinations.add(dest));
|
|
1451
633
|
}
|
|
1452
634
|
}
|
|
1453
|
-
// Check
|
|
1454
|
-
for (const edge of this.
|
|
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) =>
|
|
640
|
+
dests.forEach((dest) => directDestinations.add(dest));
|
|
1459
641
|
}
|
|
1460
642
|
}
|
|
1461
|
-
/** Check if this agent has BOTH
|
|
1462
|
-
const
|
|
1463
|
-
const
|
|
1464
|
-
const needsCommandRouting =
|
|
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
|
-
...
|
|
1468
|
-
...
|
|
649
|
+
...handoffDestinations,
|
|
650
|
+
...directDestinations,
|
|
1469
651
|
]);
|
|
1470
|
-
if (
|
|
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
|
|
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
|
|
1496
|
-
if (
|
|
1497
|
-
const { filteredMessages, instructions, sourceAgentName, parallelSiblings, } =
|
|
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
|
-
/**
|
|
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
|
|
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
|
-
/**
|
|
1624
|
-
const
|
|
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:
|
|
794
|
+
goto: handoffDest,
|
|
1714
795
|
});
|
|
1715
796
|
}
|
|
1716
797
|
else {
|
|
1717
|
-
/** No
|
|
1718
|
-
|
|
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
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
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
|
|
1815
|
-
*
|
|
1816
|
-
*
|
|
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.
|
|
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
|
-
//
|
|
1839
|
-
|
|
1840
|
-
|
|
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
|
|
860
|
+
* (Skip edges that already have an approval gate above.)
|
|
1863
861
|
*/
|
|
1864
862
|
const edgesByDestination = new Map();
|
|
1865
|
-
for (const edge of this.
|
|
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
|
|
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
|
|
1969
|
-
const
|
|
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
|
|
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 (
|
|
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
|