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