@librechat/agents 2.4.322 → 3.0.0-rc10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +218 -0
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -0
  3. package/dist/cjs/common/enum.cjs +15 -5
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/events.cjs +10 -6
  6. package/dist/cjs/events.cjs.map +1 -1
  7. package/dist/cjs/graphs/Graph.cjs +309 -213
  8. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  9. package/dist/cjs/graphs/MultiAgentGraph.cjs +507 -0
  10. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
  11. package/dist/cjs/llm/anthropic/index.cjs +54 -9
  12. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  13. package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
  14. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +52 -6
  15. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  16. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +22 -2
  17. package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
  18. package/dist/cjs/llm/anthropic/utils/tools.cjs +29 -0
  19. package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -0
  20. package/dist/cjs/llm/google/index.cjs +144 -0
  21. package/dist/cjs/llm/google/index.cjs.map +1 -0
  22. package/dist/cjs/llm/google/utils/common.cjs +477 -0
  23. package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
  24. package/dist/cjs/llm/ollama/index.cjs +67 -0
  25. package/dist/cjs/llm/ollama/index.cjs.map +1 -0
  26. package/dist/cjs/llm/ollama/utils.cjs +158 -0
  27. package/dist/cjs/llm/ollama/utils.cjs.map +1 -0
  28. package/dist/cjs/llm/openai/index.cjs +422 -3
  29. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  30. package/dist/cjs/llm/openai/utils/index.cjs +672 -0
  31. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -0
  32. package/dist/cjs/llm/providers.cjs +15 -15
  33. package/dist/cjs/llm/providers.cjs.map +1 -1
  34. package/dist/cjs/llm/text.cjs +14 -3
  35. package/dist/cjs/llm/text.cjs.map +1 -1
  36. package/dist/cjs/llm/vertexai/index.cjs +330 -0
  37. package/dist/cjs/llm/vertexai/index.cjs.map +1 -0
  38. package/dist/cjs/main.cjs +11 -0
  39. package/dist/cjs/main.cjs.map +1 -1
  40. package/dist/cjs/run.cjs +137 -85
  41. package/dist/cjs/run.cjs.map +1 -1
  42. package/dist/cjs/stream.cjs +86 -52
  43. package/dist/cjs/stream.cjs.map +1 -1
  44. package/dist/cjs/tools/ToolNode.cjs +10 -4
  45. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  46. package/dist/cjs/tools/handlers.cjs +119 -13
  47. package/dist/cjs/tools/handlers.cjs.map +1 -1
  48. package/dist/cjs/tools/search/anthropic.cjs +40 -0
  49. package/dist/cjs/tools/search/anthropic.cjs.map +1 -0
  50. package/dist/cjs/tools/search/firecrawl.cjs +55 -9
  51. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
  52. package/dist/cjs/tools/search/format.cjs +6 -6
  53. package/dist/cjs/tools/search/format.cjs.map +1 -1
  54. package/dist/cjs/tools/search/rerankers.cjs +7 -29
  55. package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
  56. package/dist/cjs/tools/search/search.cjs +86 -16
  57. package/dist/cjs/tools/search/search.cjs.map +1 -1
  58. package/dist/cjs/tools/search/tool.cjs +4 -2
  59. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  60. package/dist/cjs/tools/search/utils.cjs +1 -1
  61. package/dist/cjs/tools/search/utils.cjs.map +1 -1
  62. package/dist/cjs/utils/events.cjs +31 -0
  63. package/dist/cjs/utils/events.cjs.map +1 -0
  64. package/dist/cjs/utils/title.cjs +57 -21
  65. package/dist/cjs/utils/title.cjs.map +1 -1
  66. package/dist/cjs/utils/tokens.cjs +54 -7
  67. package/dist/cjs/utils/tokens.cjs.map +1 -1
  68. package/dist/esm/agents/AgentContext.mjs +216 -0
  69. package/dist/esm/agents/AgentContext.mjs.map +1 -0
  70. package/dist/esm/common/enum.mjs +16 -6
  71. package/dist/esm/common/enum.mjs.map +1 -1
  72. package/dist/esm/events.mjs +10 -6
  73. package/dist/esm/events.mjs.map +1 -1
  74. package/dist/esm/graphs/Graph.mjs +311 -215
  75. package/dist/esm/graphs/Graph.mjs.map +1 -1
  76. package/dist/esm/graphs/MultiAgentGraph.mjs +505 -0
  77. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
  78. package/dist/esm/llm/anthropic/index.mjs +54 -9
  79. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  80. package/dist/esm/llm/anthropic/types.mjs.map +1 -1
  81. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +52 -6
  82. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  83. package/dist/esm/llm/anthropic/utils/message_outputs.mjs +22 -2
  84. package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
  85. package/dist/esm/llm/anthropic/utils/tools.mjs +27 -0
  86. package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -0
  87. package/dist/esm/llm/google/index.mjs +142 -0
  88. package/dist/esm/llm/google/index.mjs.map +1 -0
  89. package/dist/esm/llm/google/utils/common.mjs +471 -0
  90. package/dist/esm/llm/google/utils/common.mjs.map +1 -0
  91. package/dist/esm/llm/ollama/index.mjs +65 -0
  92. package/dist/esm/llm/ollama/index.mjs.map +1 -0
  93. package/dist/esm/llm/ollama/utils.mjs +155 -0
  94. package/dist/esm/llm/ollama/utils.mjs.map +1 -0
  95. package/dist/esm/llm/openai/index.mjs +421 -4
  96. package/dist/esm/llm/openai/index.mjs.map +1 -1
  97. package/dist/esm/llm/openai/utils/index.mjs +666 -0
  98. package/dist/esm/llm/openai/utils/index.mjs.map +1 -0
  99. package/dist/esm/llm/providers.mjs +5 -5
  100. package/dist/esm/llm/providers.mjs.map +1 -1
  101. package/dist/esm/llm/text.mjs +14 -3
  102. package/dist/esm/llm/text.mjs.map +1 -1
  103. package/dist/esm/llm/vertexai/index.mjs +328 -0
  104. package/dist/esm/llm/vertexai/index.mjs.map +1 -0
  105. package/dist/esm/main.mjs +6 -5
  106. package/dist/esm/main.mjs.map +1 -1
  107. package/dist/esm/run.mjs +138 -87
  108. package/dist/esm/run.mjs.map +1 -1
  109. package/dist/esm/stream.mjs +88 -55
  110. package/dist/esm/stream.mjs.map +1 -1
  111. package/dist/esm/tools/ToolNode.mjs +10 -4
  112. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  113. package/dist/esm/tools/handlers.mjs +119 -15
  114. package/dist/esm/tools/handlers.mjs.map +1 -1
  115. package/dist/esm/tools/search/anthropic.mjs +37 -0
  116. package/dist/esm/tools/search/anthropic.mjs.map +1 -0
  117. package/dist/esm/tools/search/firecrawl.mjs +55 -9
  118. package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
  119. package/dist/esm/tools/search/format.mjs +7 -7
  120. package/dist/esm/tools/search/format.mjs.map +1 -1
  121. package/dist/esm/tools/search/rerankers.mjs +7 -29
  122. package/dist/esm/tools/search/rerankers.mjs.map +1 -1
  123. package/dist/esm/tools/search/search.mjs +86 -16
  124. package/dist/esm/tools/search/search.mjs.map +1 -1
  125. package/dist/esm/tools/search/tool.mjs +4 -2
  126. package/dist/esm/tools/search/tool.mjs.map +1 -1
  127. package/dist/esm/tools/search/utils.mjs +1 -1
  128. package/dist/esm/tools/search/utils.mjs.map +1 -1
  129. package/dist/esm/utils/events.mjs +29 -0
  130. package/dist/esm/utils/events.mjs.map +1 -0
  131. package/dist/esm/utils/title.mjs +57 -22
  132. package/dist/esm/utils/title.mjs.map +1 -1
  133. package/dist/esm/utils/tokens.mjs +54 -8
  134. package/dist/esm/utils/tokens.mjs.map +1 -1
  135. package/dist/types/agents/AgentContext.d.ts +91 -0
  136. package/dist/types/common/enum.d.ts +17 -7
  137. package/dist/types/events.d.ts +5 -4
  138. package/dist/types/graphs/Graph.d.ts +64 -67
  139. package/dist/types/graphs/MultiAgentGraph.d.ts +47 -0
  140. package/dist/types/graphs/index.d.ts +1 -0
  141. package/dist/types/llm/anthropic/index.d.ts +11 -0
  142. package/dist/types/llm/anthropic/types.d.ts +9 -3
  143. package/dist/types/llm/anthropic/utils/message_inputs.d.ts +1 -1
  144. package/dist/types/llm/anthropic/utils/output_parsers.d.ts +4 -4
  145. package/dist/types/llm/anthropic/utils/tools.d.ts +3 -0
  146. package/dist/types/llm/google/index.d.ts +13 -0
  147. package/dist/types/llm/google/types.d.ts +32 -0
  148. package/dist/types/llm/google/utils/common.d.ts +19 -0
  149. package/dist/types/llm/google/utils/tools.d.ts +10 -0
  150. package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
  151. package/dist/types/llm/ollama/index.d.ts +7 -0
  152. package/dist/types/llm/ollama/utils.d.ts +7 -0
  153. package/dist/types/llm/openai/index.d.ts +82 -3
  154. package/dist/types/llm/openai/types.d.ts +10 -0
  155. package/dist/types/llm/openai/utils/index.d.ts +20 -0
  156. package/dist/types/llm/text.d.ts +1 -1
  157. package/dist/types/llm/vertexai/index.d.ts +293 -0
  158. package/dist/types/messages/reducer.d.ts +9 -0
  159. package/dist/types/run.d.ts +19 -12
  160. package/dist/types/stream.d.ts +10 -3
  161. package/dist/types/tools/CodeExecutor.d.ts +2 -2
  162. package/dist/types/tools/ToolNode.d.ts +1 -1
  163. package/dist/types/tools/handlers.d.ts +17 -4
  164. package/dist/types/tools/search/anthropic.d.ts +16 -0
  165. package/dist/types/tools/search/firecrawl.d.ts +15 -0
  166. package/dist/types/tools/search/rerankers.d.ts +0 -1
  167. package/dist/types/tools/search/types.d.ts +30 -9
  168. package/dist/types/types/graph.d.ts +129 -15
  169. package/dist/types/types/llm.d.ts +25 -10
  170. package/dist/types/types/run.d.ts +50 -8
  171. package/dist/types/types/stream.d.ts +16 -2
  172. package/dist/types/types/tools.d.ts +1 -1
  173. package/dist/types/utils/events.d.ts +6 -0
  174. package/dist/types/utils/title.d.ts +2 -1
  175. package/dist/types/utils/tokens.d.ts +24 -0
  176. package/package.json +41 -17
  177. package/src/agents/AgentContext.ts +315 -0
  178. package/src/common/enum.ts +15 -5
  179. package/src/events.ts +24 -13
  180. package/src/graphs/Graph.ts +495 -313
  181. package/src/graphs/MultiAgentGraph.ts +598 -0
  182. package/src/graphs/index.ts +2 -1
  183. package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
  184. package/src/llm/anthropic/index.ts +78 -13
  185. package/src/llm/anthropic/llm.spec.ts +491 -115
  186. package/src/llm/anthropic/types.ts +39 -3
  187. package/src/llm/anthropic/utils/message_inputs.ts +67 -11
  188. package/src/llm/anthropic/utils/message_outputs.ts +21 -2
  189. package/src/llm/anthropic/utils/output_parsers.ts +25 -6
  190. package/src/llm/anthropic/utils/tools.ts +29 -0
  191. package/src/llm/google/index.ts +218 -0
  192. package/src/llm/google/types.ts +43 -0
  193. package/src/llm/google/utils/common.ts +646 -0
  194. package/src/llm/google/utils/tools.ts +160 -0
  195. package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -0
  196. package/src/llm/ollama/index.ts +89 -0
  197. package/src/llm/ollama/utils.ts +193 -0
  198. package/src/llm/openai/index.ts +641 -14
  199. package/src/llm/openai/types.ts +24 -0
  200. package/src/llm/openai/utils/index.ts +912 -0
  201. package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
  202. package/src/llm/providers.ts +10 -9
  203. package/src/llm/text.ts +26 -7
  204. package/src/llm/vertexai/index.ts +360 -0
  205. package/src/messages/reducer.ts +80 -0
  206. package/src/run.ts +196 -116
  207. package/src/scripts/ant_web_search.ts +158 -0
  208. package/src/scripts/args.ts +12 -8
  209. package/src/scripts/cli4.ts +29 -21
  210. package/src/scripts/cli5.ts +29 -21
  211. package/src/scripts/code_exec.ts +54 -23
  212. package/src/scripts/code_exec_files.ts +48 -17
  213. package/src/scripts/code_exec_simple.ts +46 -27
  214. package/src/scripts/handoff-test.ts +135 -0
  215. package/src/scripts/image.ts +52 -20
  216. package/src/scripts/multi-agent-chain.ts +278 -0
  217. package/src/scripts/multi-agent-conditional.ts +220 -0
  218. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  219. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  220. package/src/scripts/multi-agent-parallel.ts +341 -0
  221. package/src/scripts/multi-agent-sequence.ts +212 -0
  222. package/src/scripts/multi-agent-supervisor.ts +362 -0
  223. package/src/scripts/multi-agent-test.ts +186 -0
  224. package/src/scripts/search.ts +1 -9
  225. package/src/scripts/simple.ts +25 -10
  226. package/src/scripts/test-custom-prompt-key.ts +145 -0
  227. package/src/scripts/test-handoff-input.ts +170 -0
  228. package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
  229. package/src/scripts/test-tools-before-handoff.ts +233 -0
  230. package/src/scripts/tools.ts +48 -18
  231. package/src/specs/anthropic.simple.test.ts +150 -34
  232. package/src/specs/azure.simple.test.ts +325 -0
  233. package/src/specs/openai.simple.test.ts +140 -33
  234. package/src/specs/openrouter.simple.test.ts +107 -0
  235. package/src/specs/prune.test.ts +4 -9
  236. package/src/specs/reasoning.test.ts +80 -44
  237. package/src/specs/token-memoization.test.ts +39 -0
  238. package/src/stream.test.ts +94 -0
  239. package/src/stream.ts +143 -61
  240. package/src/tools/ToolNode.ts +21 -7
  241. package/src/tools/handlers.ts +192 -18
  242. package/src/tools/search/anthropic.ts +51 -0
  243. package/src/tools/search/firecrawl.ts +69 -20
  244. package/src/tools/search/format.ts +6 -8
  245. package/src/tools/search/rerankers.ts +7 -40
  246. package/src/tools/search/search.ts +97 -16
  247. package/src/tools/search/tool.ts +5 -2
  248. package/src/tools/search/types.ts +30 -10
  249. package/src/tools/search/utils.ts +1 -1
  250. package/src/types/graph.ts +318 -103
  251. package/src/types/llm.ts +26 -12
  252. package/src/types/run.ts +56 -13
  253. package/src/types/stream.ts +22 -1
  254. package/src/types/tools.ts +16 -10
  255. package/src/utils/events.ts +32 -0
  256. package/src/utils/llmConfig.ts +19 -7
  257. package/src/utils/title.ts +104 -30
  258. package/src/utils/tokens.ts +69 -10
  259. package/dist/types/scripts/abort.d.ts +0 -1
  260. package/dist/types/scripts/args.d.ts +0 -6
  261. package/dist/types/scripts/caching.d.ts +0 -1
  262. package/dist/types/scripts/cli.d.ts +0 -1
  263. package/dist/types/scripts/cli2.d.ts +0 -1
  264. package/dist/types/scripts/cli3.d.ts +0 -1
  265. package/dist/types/scripts/cli4.d.ts +0 -1
  266. package/dist/types/scripts/cli5.d.ts +0 -1
  267. package/dist/types/scripts/code_exec.d.ts +0 -1
  268. package/dist/types/scripts/code_exec_files.d.ts +0 -1
  269. package/dist/types/scripts/code_exec_simple.d.ts +0 -1
  270. package/dist/types/scripts/content.d.ts +0 -1
  271. package/dist/types/scripts/empty_input.d.ts +0 -1
  272. package/dist/types/scripts/image.d.ts +0 -1
  273. package/dist/types/scripts/memory.d.ts +0 -1
  274. package/dist/types/scripts/search.d.ts +0 -1
  275. package/dist/types/scripts/simple.d.ts +0 -1
  276. package/dist/types/scripts/stream.d.ts +0 -1
  277. package/dist/types/scripts/thinking.d.ts +0 -1
  278. package/dist/types/scripts/tools.d.ts +0 -1
  279. package/dist/types/specs/spec.utils.d.ts +0 -1
@@ -0,0 +1,505 @@
1
+ import { z } from 'zod';
2
+ import { tool } from '@langchain/core/tools';
3
+ import { PromptTemplate } from '@langchain/core/prompts';
4
+ import { ToolMessage, getBufferString, HumanMessage } from '@langchain/core/messages';
5
+ import { getCurrentTaskInput, Command, Annotation, messagesStateReducer, StateGraph, END, START } from '@langchain/langgraph';
6
+ import { StandardGraph } from './Graph.mjs';
7
+ import { Constants } from '../common/enum.mjs';
8
+
9
+ /**
10
+ * MultiAgentGraph extends StandardGraph to support dynamic multi-agent workflows
11
+ * with handoffs, fan-in/fan-out, and other composable patterns.
12
+ *
13
+ * Key behavior:
14
+ * - Agents with ONLY handoff edges: Can dynamically route to any handoff destination
15
+ * - Agents with ONLY direct edges: Always follow their direct edges
16
+ * - Agents with BOTH: Use Command for exclusive routing (handoff OR direct, not both)
17
+ * - If handoff occurs: Only the handoff destination executes
18
+ * - If no handoff: Direct edges execute (potentially in parallel)
19
+ *
20
+ * This enables the common pattern where an agent either delegates (handoff)
21
+ * OR continues its workflow (direct edges), but not both simultaneously.
22
+ */
23
+ class MultiAgentGraph extends StandardGraph {
24
+ edges;
25
+ startingNodes = new Set();
26
+ directEdges = [];
27
+ handoffEdges = [];
28
+ constructor(input) {
29
+ super(input);
30
+ this.edges = input.edges;
31
+ this.categorizeEdges();
32
+ this.analyzeGraph();
33
+ this.createHandoffTools();
34
+ }
35
+ /**
36
+ * Categorize edges into handoff and direct types
37
+ */
38
+ categorizeEdges() {
39
+ for (const edge of this.edges) {
40
+ // Default behavior: edges with conditions or explicit 'handoff' type are handoff edges
41
+ // Edges with explicit 'direct' type or multi-destination without conditions are direct edges
42
+ if (edge.edgeType === 'direct') {
43
+ this.directEdges.push(edge);
44
+ }
45
+ else if (edge.edgeType === 'handoff' || edge.condition != null) {
46
+ this.handoffEdges.push(edge);
47
+ }
48
+ else {
49
+ // Default: single-to-single edges are handoff, single-to-multiple are direct
50
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
51
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
52
+ if (sources.length === 1 && destinations.length > 1) {
53
+ // Fan-out pattern defaults to direct
54
+ this.directEdges.push(edge);
55
+ }
56
+ else {
57
+ // Everything else defaults to handoff
58
+ this.handoffEdges.push(edge);
59
+ }
60
+ }
61
+ }
62
+ }
63
+ /**
64
+ * Analyze graph structure to determine starting nodes and connections
65
+ */
66
+ analyzeGraph() {
67
+ const hasIncomingEdge = new Set();
68
+ // Track all nodes that have incoming edges
69
+ for (const edge of this.edges) {
70
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
71
+ destinations.forEach((dest) => hasIncomingEdge.add(dest));
72
+ }
73
+ // Starting nodes are those without incoming edges
74
+ for (const agentId of this.agentContexts.keys()) {
75
+ if (!hasIncomingEdge.has(agentId)) {
76
+ this.startingNodes.add(agentId);
77
+ }
78
+ }
79
+ // If no starting nodes found, use the first agent
80
+ if (this.startingNodes.size === 0 && this.agentContexts.size > 0) {
81
+ this.startingNodes.add(this.agentContexts.keys().next().value);
82
+ }
83
+ }
84
+ /**
85
+ * Create handoff tools for agents based on handoff edges only
86
+ */
87
+ createHandoffTools() {
88
+ // Group handoff edges by source agent(s)
89
+ const handoffsByAgent = new Map();
90
+ // Only process handoff edges for tool creation
91
+ for (const edge of this.handoffEdges) {
92
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
93
+ sources.forEach((source) => {
94
+ if (!handoffsByAgent.has(source)) {
95
+ handoffsByAgent.set(source, []);
96
+ }
97
+ handoffsByAgent.get(source).push(edge);
98
+ });
99
+ }
100
+ // Create handoff tools for each agent
101
+ for (const [agentId, edges] of handoffsByAgent) {
102
+ const agentContext = this.agentContexts.get(agentId);
103
+ if (!agentContext)
104
+ continue;
105
+ // Create handoff tools for this agent's outgoing edges
106
+ const handoffTools = [];
107
+ for (const edge of edges) {
108
+ handoffTools.push(...this.createHandoffToolsForEdge(edge));
109
+ }
110
+ // Add handoff tools to the agent's existing tools
111
+ if (!agentContext.tools) {
112
+ agentContext.tools = [];
113
+ }
114
+ agentContext.tools.push(...handoffTools);
115
+ }
116
+ }
117
+ /**
118
+ * Create handoff tools for an edge (handles multiple destinations)
119
+ */
120
+ createHandoffToolsForEdge(edge) {
121
+ const tools = [];
122
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
123
+ /** If there's a condition, create a single conditional handoff tool */
124
+ if (edge.condition != null) {
125
+ const toolName = 'conditional_transfer';
126
+ const toolDescription = edge.description ?? 'Conditionally transfer control based on state';
127
+ /** Check if we have a prompt for handoff input */
128
+ const hasHandoffInput = edge.prompt != null && typeof edge.prompt === 'string';
129
+ const handoffInputDescription = hasHandoffInput ? edge.prompt : undefined;
130
+ const promptKey = edge.promptKey ?? 'instructions';
131
+ tools.push(tool(async (input, config) => {
132
+ const state = getCurrentTaskInput();
133
+ const toolCallId = config?.toolCall?.id ??
134
+ 'unknown';
135
+ /** Evaluated condition */
136
+ const result = edge.condition(state);
137
+ let destination;
138
+ if (typeof result === 'boolean') {
139
+ /** If true, use first destination; if false, don't transfer */
140
+ if (!result)
141
+ return null;
142
+ destination = destinations[0];
143
+ }
144
+ else if (typeof result === 'string') {
145
+ destination = result;
146
+ }
147
+ else {
148
+ /** Array of destinations - for now, use the first */
149
+ destination = Array.isArray(result) ? result[0] : destinations[0];
150
+ }
151
+ let content = `Conditionally transferred to ${destination}`;
152
+ if (hasHandoffInput &&
153
+ promptKey in input &&
154
+ input[promptKey] != null) {
155
+ content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
156
+ }
157
+ const toolMessage = new ToolMessage({
158
+ content,
159
+ name: toolName,
160
+ tool_call_id: toolCallId,
161
+ });
162
+ return new Command({
163
+ goto: destination,
164
+ update: { messages: state.messages.concat(toolMessage) },
165
+ graph: Command.PARENT,
166
+ });
167
+ }, {
168
+ name: toolName,
169
+ schema: hasHandoffInput
170
+ ? z.object({
171
+ [promptKey]: z
172
+ .string()
173
+ .optional()
174
+ .describe(handoffInputDescription),
175
+ })
176
+ : z.object({}),
177
+ description: toolDescription,
178
+ }));
179
+ }
180
+ else {
181
+ /** Create individual tools for each destination */
182
+ for (const destination of destinations) {
183
+ const toolName = `${Constants.LC_TRANSFER_TO_}${destination}`;
184
+ const toolDescription = edge.description ?? `Transfer control to agent '${destination}'`;
185
+ /** Check if we have a prompt for handoff input */
186
+ const hasHandoffInput = edge.prompt != null && typeof edge.prompt === 'string';
187
+ const handoffInputDescription = hasHandoffInput
188
+ ? edge.prompt
189
+ : undefined;
190
+ const promptKey = edge.promptKey ?? 'instructions';
191
+ tools.push(tool(async (input, config) => {
192
+ const toolCallId = config?.toolCall?.id ??
193
+ 'unknown';
194
+ let content = `Successfully transferred to ${destination}`;
195
+ if (hasHandoffInput &&
196
+ promptKey in input &&
197
+ input[promptKey] != null) {
198
+ content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
199
+ }
200
+ const toolMessage = new ToolMessage({
201
+ content,
202
+ name: toolName,
203
+ tool_call_id: toolCallId,
204
+ });
205
+ const state = getCurrentTaskInput();
206
+ return new Command({
207
+ goto: destination,
208
+ update: { messages: state.messages.concat(toolMessage) },
209
+ graph: Command.PARENT,
210
+ });
211
+ }, {
212
+ name: toolName,
213
+ schema: hasHandoffInput
214
+ ? z.object({
215
+ [promptKey]: z
216
+ .string()
217
+ .optional()
218
+ .describe(handoffInputDescription),
219
+ })
220
+ : z.object({}),
221
+ description: toolDescription,
222
+ }));
223
+ }
224
+ }
225
+ return tools;
226
+ }
227
+ /**
228
+ * Create a complete agent subgraph (similar to createReactAgent)
229
+ */
230
+ createAgentSubgraph(agentId) {
231
+ /** This is essentially the same as `createAgentNode` from `StandardGraph` */
232
+ return this.createAgentNode(agentId);
233
+ }
234
+ /**
235
+ * Create the multi-agent workflow with dynamic handoffs
236
+ */
237
+ createWorkflow() {
238
+ const StateAnnotation = Annotation.Root({
239
+ messages: Annotation({
240
+ reducer: (a, b) => {
241
+ if (!a.length) {
242
+ this.startIndex = a.length + b.length;
243
+ }
244
+ const result = messagesStateReducer(a, b);
245
+ this.messages = result;
246
+ return result;
247
+ },
248
+ default: () => [],
249
+ }),
250
+ /** Channel for passing filtered messages to agents when excludeResults is true */
251
+ agentMessages: Annotation({
252
+ /** Replaces state entirely */
253
+ reducer: (a, b) => b,
254
+ default: () => [],
255
+ }),
256
+ });
257
+ const builder = new StateGraph(StateAnnotation);
258
+ // Add all agents as complete subgraphs
259
+ for (const [agentId] of this.agentContexts) {
260
+ // Get all possible destinations for this agent
261
+ const handoffDestinations = new Set();
262
+ const directDestinations = new Set();
263
+ // Check handoff edges for destinations
264
+ for (const edge of this.handoffEdges) {
265
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
266
+ if (sources.includes(agentId) === true) {
267
+ const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
268
+ dests.forEach((dest) => handoffDestinations.add(dest));
269
+ }
270
+ }
271
+ // Check direct edges for destinations
272
+ for (const edge of this.directEdges) {
273
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
274
+ if (sources.includes(agentId) === true) {
275
+ const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
276
+ dests.forEach((dest) => directDestinations.add(dest));
277
+ }
278
+ }
279
+ /** Check if this agent has BOTH handoff and direct edges */
280
+ const hasHandoffEdges = handoffDestinations.size > 0;
281
+ const hasDirectEdges = directDestinations.size > 0;
282
+ const needsCommandRouting = hasHandoffEdges && hasDirectEdges;
283
+ /** Collect all possible destinations for this agent */
284
+ const allDestinations = new Set([
285
+ ...handoffDestinations,
286
+ ...directDestinations,
287
+ ]);
288
+ if (handoffDestinations.size > 0 || directDestinations.size === 0) {
289
+ allDestinations.add(END);
290
+ }
291
+ /** Agent subgraph (includes agent + tools) */
292
+ const agentSubgraph = this.createAgentSubgraph(agentId);
293
+ /** Wrapper function that handles agentMessages channel and conditional routing */
294
+ const agentWrapper = async (state) => {
295
+ let result;
296
+ if (state.agentMessages != null && state.agentMessages.length > 0) {
297
+ /**
298
+ * When using agentMessages (excludeResults=true), we need to update
299
+ * the token map to account for the new prompt message
300
+ */
301
+ const agentContext = this.agentContexts.get(agentId);
302
+ if (agentContext && agentContext.tokenCounter) {
303
+ // The agentMessages contains:
304
+ // 1. Filtered messages (0 to startIndex) - already have token counts
305
+ // 2. New prompt message - needs token counting
306
+ const freshTokenMap = {};
307
+ // Copy existing token counts for filtered messages (0 to startIndex)
308
+ for (let i = 0; i < this.startIndex; i++) {
309
+ const tokenCount = agentContext.indexTokenCountMap[i];
310
+ if (tokenCount !== undefined) {
311
+ freshTokenMap[i] = tokenCount;
312
+ }
313
+ }
314
+ // Calculate tokens only for the new prompt message (last message)
315
+ const promptMessageIndex = state.agentMessages.length - 1;
316
+ if (promptMessageIndex >= this.startIndex) {
317
+ const promptMessage = state.agentMessages[promptMessageIndex];
318
+ freshTokenMap[promptMessageIndex] =
319
+ agentContext.tokenCounter(promptMessage);
320
+ }
321
+ // Update the agent's token map with instructions added
322
+ agentContext.updateTokenMapWithInstructions(freshTokenMap);
323
+ }
324
+ /** Temporary state with messages replaced by `agentMessages` */
325
+ const transformedState = {
326
+ ...state,
327
+ messages: state.agentMessages,
328
+ };
329
+ result = await agentSubgraph.invoke(transformedState);
330
+ result = {
331
+ ...result,
332
+ /** Clear agentMessages for next agent */
333
+ agentMessages: [],
334
+ };
335
+ }
336
+ else {
337
+ result = await agentSubgraph.invoke(state);
338
+ }
339
+ /** If agent has both handoff and direct edges, use Command for exclusive routing */
340
+ if (needsCommandRouting) {
341
+ /** Check if a handoff occurred */
342
+ const lastMessage = result.messages[result.messages.length - 1];
343
+ if (lastMessage != null &&
344
+ lastMessage.getType() === 'tool' &&
345
+ typeof lastMessage.name === 'string' &&
346
+ lastMessage.name.startsWith(Constants.LC_TRANSFER_TO_)) {
347
+ /** Handoff occurred - extract destination and navigate there exclusively */
348
+ const handoffDest = lastMessage.name.replace(Constants.LC_TRANSFER_TO_, '');
349
+ return new Command({
350
+ update: result,
351
+ goto: handoffDest,
352
+ });
353
+ }
354
+ else {
355
+ /** No handoff - proceed with direct edges */
356
+ const directDests = Array.from(directDestinations);
357
+ if (directDests.length === 1) {
358
+ return new Command({
359
+ update: result,
360
+ goto: directDests[0],
361
+ });
362
+ }
363
+ else if (directDests.length > 1) {
364
+ /** Multiple direct destinations - they'll run in parallel */
365
+ return new Command({
366
+ update: result,
367
+ goto: directDests,
368
+ });
369
+ }
370
+ }
371
+ }
372
+ /** No special routing needed - return state normally */
373
+ return result;
374
+ };
375
+ /** Wrapped agent as a node with its possible destinations */
376
+ builder.addNode(agentId, agentWrapper, {
377
+ ends: Array.from(allDestinations),
378
+ });
379
+ }
380
+ // Add starting edges for all starting nodes
381
+ for (const startNode of this.startingNodes) {
382
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
383
+ /** @ts-ignore */
384
+ builder.addEdge(START, startNode);
385
+ }
386
+ /**
387
+ * Add direct edges for automatic transitions
388
+ * Group edges by destination to handle fan-in scenarios
389
+ */
390
+ const edgesByDestination = new Map();
391
+ for (const edge of this.directEdges) {
392
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
393
+ for (const destination of destinations) {
394
+ if (!edgesByDestination.has(destination)) {
395
+ edgesByDestination.set(destination, []);
396
+ }
397
+ edgesByDestination.get(destination).push(edge);
398
+ }
399
+ }
400
+ for (const [destination, edges] of edgesByDestination) {
401
+ /** Checks if this is a fan-in scenario with prompt instructions */
402
+ const edgesWithPrompt = edges.filter((edge) => edge.prompt != null && edge.prompt !== '');
403
+ if (edgesWithPrompt.length > 0) {
404
+ /**
405
+ * Single wrapper node for destination (Fan-in with prompt)
406
+ */
407
+ const wrapperNodeId = `fan_in_${destination}_prompt`;
408
+ /**
409
+ * First edge's `prompt`
410
+ * (they should all be the same for fan-in)
411
+ */
412
+ const prompt = edgesWithPrompt[0].prompt;
413
+ /**
414
+ * First edge's `excludeResults` flag
415
+ * (they should all be the same for fan-in)
416
+ */
417
+ const excludeResults = edgesWithPrompt[0].excludeResults;
418
+ builder.addNode(wrapperNodeId, async (state) => {
419
+ let promptText;
420
+ let effectiveExcludeResults = excludeResults;
421
+ if (typeof prompt === 'function') {
422
+ promptText = await prompt(state.messages, this.startIndex);
423
+ }
424
+ else if (prompt != null) {
425
+ if (prompt.includes('{results}')) {
426
+ const resultsMessages = state.messages.slice(this.startIndex);
427
+ const resultsString = getBufferString(resultsMessages);
428
+ const promptTemplate = PromptTemplate.fromTemplate(prompt);
429
+ const result = await promptTemplate.invoke({
430
+ results: resultsString,
431
+ });
432
+ promptText = result.value;
433
+ effectiveExcludeResults =
434
+ excludeResults !== false && promptText !== '';
435
+ }
436
+ else {
437
+ promptText = prompt;
438
+ }
439
+ }
440
+ if (promptText != null && promptText !== '') {
441
+ if (effectiveExcludeResults == null ||
442
+ effectiveExcludeResults === false) {
443
+ return {
444
+ messages: [new HumanMessage(promptText)],
445
+ };
446
+ }
447
+ /** When `excludeResults` is true, use agentMessages channel
448
+ * to pass filtered messages + prompt to the destination agent
449
+ */
450
+ const filteredMessages = state.messages.slice(0, this.startIndex);
451
+ return {
452
+ messages: [new HumanMessage(promptText)],
453
+ agentMessages: messagesStateReducer(filteredMessages, [
454
+ new HumanMessage(promptText),
455
+ ]),
456
+ };
457
+ }
458
+ /** No prompt needed, return empty update */
459
+ return {};
460
+ });
461
+ /** Add edges from all sources to the wrapper, then wrapper to destination */
462
+ for (const edge of edges) {
463
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
464
+ for (const source of sources) {
465
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
466
+ /** @ts-ignore */
467
+ builder.addEdge(source, wrapperNodeId);
468
+ }
469
+ }
470
+ /** Single edge from wrapper to destination */
471
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
472
+ /** @ts-ignore */
473
+ builder.addEdge(wrapperNodeId, destination);
474
+ }
475
+ else {
476
+ /** No prompt instructions, add direct edges (skip if source uses Command routing) */
477
+ for (const edge of edges) {
478
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
479
+ for (const source of sources) {
480
+ /** Check if this source node has both handoff and direct edges */
481
+ const sourceHandoffEdges = this.handoffEdges.filter((e) => {
482
+ const eSources = Array.isArray(e.from) ? e.from : [e.from];
483
+ return eSources.includes(source);
484
+ });
485
+ const sourceDirectEdges = this.directEdges.filter((e) => {
486
+ const eSources = Array.isArray(e.from) ? e.from : [e.from];
487
+ return eSources.includes(source);
488
+ });
489
+ /** Skip adding edge if source uses Command routing (has both types) */
490
+ if (sourceHandoffEdges.length > 0 && sourceDirectEdges.length > 0) {
491
+ continue;
492
+ }
493
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
494
+ /** @ts-ignore */
495
+ builder.addEdge(source, destination);
496
+ }
497
+ }
498
+ }
499
+ }
500
+ return builder.compile(this.compileOptions);
501
+ }
502
+ }
503
+
504
+ export { MultiAgentGraph };
505
+ //# sourceMappingURL=MultiAgentGraph.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MultiAgentGraph.mjs","sources":["../../../src/graphs/MultiAgentGraph.ts"],"sourcesContent":["import { z } from 'zod';\nimport { tool } from '@langchain/core/tools';\nimport { PromptTemplate } from '@langchain/core/prompts';\nimport {\n ToolMessage,\n HumanMessage,\n getBufferString,\n} from '@langchain/core/messages';\nimport {\n END,\n START,\n Command,\n StateGraph,\n Annotation,\n getCurrentTaskInput,\n messagesStateReducer,\n} from '@langchain/langgraph';\nimport type { ToolRunnableConfig } from '@langchain/core/tools';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport type * as t from '@/types';\nimport { StandardGraph } from './Graph';\nimport { Constants } from '@/common';\n\n/**\n * MultiAgentGraph extends StandardGraph to support dynamic multi-agent workflows\n * with handoffs, fan-in/fan-out, and other composable patterns.\n *\n * Key behavior:\n * - Agents with ONLY handoff edges: Can dynamically route to any handoff destination\n * - Agents with ONLY direct edges: Always follow their direct edges\n * - Agents with BOTH: Use Command for exclusive routing (handoff OR direct, not both)\n * - If handoff occurs: Only the handoff destination executes\n * - If no handoff: Direct edges execute (potentially in parallel)\n *\n * This enables the common pattern where an agent either delegates (handoff)\n * OR continues its workflow (direct edges), but not both simultaneously.\n */\nexport class MultiAgentGraph extends StandardGraph {\n private edges: t.GraphEdge[];\n private startingNodes: Set<string> = new Set();\n private directEdges: t.GraphEdge[] = [];\n private handoffEdges: t.GraphEdge[] = [];\n\n constructor(input: t.MultiAgentGraphInput) {\n super(input);\n this.edges = input.edges;\n this.categorizeEdges();\n this.analyzeGraph();\n this.createHandoffTools();\n }\n\n /**\n * Categorize edges into handoff and direct types\n */\n private categorizeEdges(): void {\n for (const edge of this.edges) {\n // Default behavior: edges with conditions or explicit 'handoff' type are handoff edges\n // Edges with explicit 'direct' type or multi-destination without conditions are direct edges\n if (edge.edgeType === 'direct') {\n this.directEdges.push(edge);\n } else if (edge.edgeType === 'handoff' || edge.condition != null) {\n this.handoffEdges.push(edge);\n } else {\n // Default: single-to-single edges are handoff, single-to-multiple are direct\n const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];\n const sources = Array.isArray(edge.from) ? edge.from : [edge.from];\n\n if (sources.length === 1 && destinations.length > 1) {\n // Fan-out pattern defaults to direct\n this.directEdges.push(edge);\n } else {\n // Everything else defaults to handoff\n this.handoffEdges.push(edge);\n }\n }\n }\n }\n\n /**\n * Analyze graph structure to determine starting nodes and connections\n */\n private analyzeGraph(): void {\n const hasIncomingEdge = new Set<string>();\n\n // Track all nodes that have incoming edges\n for (const edge of this.edges) {\n const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];\n destinations.forEach((dest) => hasIncomingEdge.add(dest));\n }\n\n // Starting nodes are those without incoming edges\n for (const agentId of this.agentContexts.keys()) {\n if (!hasIncomingEdge.has(agentId)) {\n this.startingNodes.add(agentId);\n }\n }\n\n // If no starting nodes found, use the first agent\n if (this.startingNodes.size === 0 && this.agentContexts.size > 0) {\n this.startingNodes.add(this.agentContexts.keys().next().value!);\n }\n }\n\n /**\n * Create handoff tools for agents based on handoff edges only\n */\n private createHandoffTools(): void {\n // Group handoff edges by source agent(s)\n const handoffsByAgent = new Map<string, t.GraphEdge[]>();\n\n // Only process handoff edges for tool creation\n for (const edge of this.handoffEdges) {\n const sources = Array.isArray(edge.from) ? edge.from : [edge.from];\n sources.forEach((source) => {\n if (!handoffsByAgent.has(source)) {\n handoffsByAgent.set(source, []);\n }\n handoffsByAgent.get(source)!.push(edge);\n });\n }\n\n // Create handoff tools for each agent\n for (const [agentId, edges] of handoffsByAgent) {\n const agentContext = this.agentContexts.get(agentId);\n if (!agentContext) continue;\n\n // Create handoff tools for this agent's outgoing edges\n const handoffTools: t.GenericTool[] = [];\n for (const edge of edges) {\n handoffTools.push(...this.createHandoffToolsForEdge(edge));\n }\n\n // Add handoff tools to the agent's existing tools\n if (!agentContext.tools) {\n agentContext.tools = [];\n }\n agentContext.tools.push(...handoffTools);\n }\n }\n\n /**\n * Create handoff tools for an edge (handles multiple destinations)\n */\n private createHandoffToolsForEdge(edge: t.GraphEdge): t.GenericTool[] {\n const tools: t.GenericTool[] = [];\n const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];\n\n /** If there's a condition, create a single conditional handoff tool */\n if (edge.condition != null) {\n const toolName = 'conditional_transfer';\n const toolDescription =\n edge.description ?? 'Conditionally transfer control based on state';\n\n /** Check if we have a prompt for handoff input */\n const hasHandoffInput =\n edge.prompt != null && typeof edge.prompt === 'string';\n const handoffInputDescription = hasHandoffInput ? edge.prompt : undefined;\n const promptKey = edge.promptKey ?? 'instructions';\n\n tools.push(\n tool(\n async (input: Record<string, unknown>, config) => {\n const state = getCurrentTaskInput() as t.BaseGraphState;\n const toolCallId =\n (config as ToolRunnableConfig | undefined)?.toolCall?.id ??\n 'unknown';\n\n /** Evaluated condition */\n const result = edge.condition!(state);\n let destination: string;\n\n if (typeof result === 'boolean') {\n /** If true, use first destination; if false, don't transfer */\n if (!result) return null;\n destination = destinations[0];\n } else if (typeof result === 'string') {\n destination = result;\n } else {\n /** Array of destinations - for now, use the first */\n destination = Array.isArray(result) ? result[0] : destinations[0];\n }\n\n let content = `Conditionally transferred to ${destination}`;\n if (\n hasHandoffInput &&\n promptKey in input &&\n input[promptKey] != null\n ) {\n content += `\\n\\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;\n }\n\n const toolMessage = new ToolMessage({\n content,\n name: toolName,\n tool_call_id: toolCallId,\n });\n\n return new Command({\n goto: destination,\n update: { messages: state.messages.concat(toolMessage) },\n graph: Command.PARENT,\n });\n },\n {\n name: toolName,\n schema: hasHandoffInput\n ? z.object({\n [promptKey]: z\n .string()\n .optional()\n .describe(handoffInputDescription as string),\n })\n : z.object({}),\n description: toolDescription,\n }\n )\n );\n } else {\n /** Create individual tools for each destination */\n for (const destination of destinations) {\n const toolName = `${Constants.LC_TRANSFER_TO_}${destination}`;\n const toolDescription =\n edge.description ?? `Transfer control to agent '${destination}'`;\n\n /** Check if we have a prompt for handoff input */\n const hasHandoffInput =\n edge.prompt != null && typeof edge.prompt === 'string';\n const handoffInputDescription = hasHandoffInput\n ? edge.prompt\n : undefined;\n const promptKey = edge.promptKey ?? 'instructions';\n\n tools.push(\n tool(\n async (input: Record<string, unknown>, config) => {\n const toolCallId =\n (config as ToolRunnableConfig | undefined)?.toolCall?.id ??\n 'unknown';\n\n let content = `Successfully transferred to ${destination}`;\n if (\n hasHandoffInput &&\n promptKey in input &&\n input[promptKey] != null\n ) {\n content += `\\n\\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;\n }\n\n const toolMessage = new ToolMessage({\n content,\n name: toolName,\n tool_call_id: toolCallId,\n });\n\n const state = getCurrentTaskInput() as t.BaseGraphState;\n\n return new Command({\n goto: destination,\n update: { messages: state.messages.concat(toolMessage) },\n graph: Command.PARENT,\n });\n },\n {\n name: toolName,\n schema: hasHandoffInput\n ? z.object({\n [promptKey]: z\n .string()\n .optional()\n .describe(handoffInputDescription as string),\n })\n : z.object({}),\n description: toolDescription,\n }\n )\n );\n }\n }\n\n return tools;\n }\n\n /**\n * Create a complete agent subgraph (similar to createReactAgent)\n */\n private createAgentSubgraph(agentId: string): t.CompiledAgentWorfklow {\n /** This is essentially the same as `createAgentNode` from `StandardGraph` */\n return this.createAgentNode(agentId);\n }\n\n /**\n * Create the multi-agent workflow with dynamic handoffs\n */\n override createWorkflow(): t.CompiledMultiAgentWorkflow {\n const StateAnnotation = Annotation.Root({\n messages: Annotation<BaseMessage[]>({\n reducer: (a, b) => {\n if (!a.length) {\n this.startIndex = a.length + b.length;\n }\n const result = messagesStateReducer(a, b);\n this.messages = result;\n return result;\n },\n default: () => [],\n }),\n /** Channel for passing filtered messages to agents when excludeResults is true */\n agentMessages: Annotation<BaseMessage[]>({\n /** Replaces state entirely */\n reducer: (a, b) => b,\n default: () => [],\n }),\n });\n\n const builder = new StateGraph(StateAnnotation);\n\n // Add all agents as complete subgraphs\n for (const [agentId] of this.agentContexts) {\n // Get all possible destinations for this agent\n const handoffDestinations = new Set<string>();\n const directDestinations = new Set<string>();\n\n // Check handoff edges for destinations\n for (const edge of this.handoffEdges) {\n const sources = Array.isArray(edge.from) ? edge.from : [edge.from];\n if (sources.includes(agentId) === true) {\n const dests = Array.isArray(edge.to) ? edge.to : [edge.to];\n dests.forEach((dest) => handoffDestinations.add(dest));\n }\n }\n\n // Check direct edges for destinations\n for (const edge of this.directEdges) {\n const sources = Array.isArray(edge.from) ? edge.from : [edge.from];\n if (sources.includes(agentId) === true) {\n const dests = Array.isArray(edge.to) ? edge.to : [edge.to];\n dests.forEach((dest) => directDestinations.add(dest));\n }\n }\n\n /** Check if this agent has BOTH handoff and direct edges */\n const hasHandoffEdges = handoffDestinations.size > 0;\n const hasDirectEdges = directDestinations.size > 0;\n const needsCommandRouting = hasHandoffEdges && hasDirectEdges;\n\n /** Collect all possible destinations for this agent */\n const allDestinations = new Set([\n ...handoffDestinations,\n ...directDestinations,\n ]);\n if (handoffDestinations.size > 0 || directDestinations.size === 0) {\n allDestinations.add(END);\n }\n\n /** Agent subgraph (includes agent + tools) */\n const agentSubgraph = this.createAgentSubgraph(agentId);\n\n /** Wrapper function that handles agentMessages channel and conditional routing */\n const agentWrapper = async (\n state: t.MultiAgentGraphState\n ): Promise<t.MultiAgentGraphState | Command> => {\n let result: t.MultiAgentGraphState;\n\n if (state.agentMessages != null && state.agentMessages.length > 0) {\n /**\n * When using agentMessages (excludeResults=true), we need to update\n * the token map to account for the new prompt message\n */\n const agentContext = this.agentContexts.get(agentId);\n if (agentContext && agentContext.tokenCounter) {\n // The agentMessages contains:\n // 1. Filtered messages (0 to startIndex) - already have token counts\n // 2. New prompt message - needs token counting\n\n const freshTokenMap: Record<string, number> = {};\n\n // Copy existing token counts for filtered messages (0 to startIndex)\n for (let i = 0; i < this.startIndex; i++) {\n const tokenCount = agentContext.indexTokenCountMap[i];\n if (tokenCount !== undefined) {\n freshTokenMap[i] = tokenCount;\n }\n }\n\n // Calculate tokens only for the new prompt message (last message)\n const promptMessageIndex = state.agentMessages.length - 1;\n if (promptMessageIndex >= this.startIndex) {\n const promptMessage = state.agentMessages[promptMessageIndex];\n freshTokenMap[promptMessageIndex] =\n agentContext.tokenCounter(promptMessage);\n }\n\n // Update the agent's token map with instructions added\n agentContext.updateTokenMapWithInstructions(freshTokenMap);\n }\n\n /** Temporary state with messages replaced by `agentMessages` */\n const transformedState: t.MultiAgentGraphState = {\n ...state,\n messages: state.agentMessages,\n };\n result = await agentSubgraph.invoke(transformedState);\n result = {\n ...result,\n /** Clear agentMessages for next agent */\n agentMessages: [],\n };\n } else {\n result = await agentSubgraph.invoke(state);\n }\n\n /** If agent has both handoff and direct edges, use Command for exclusive routing */\n if (needsCommandRouting) {\n /** Check if a handoff occurred */\n const lastMessage = result.messages[\n result.messages.length - 1\n ] as BaseMessage | null;\n if (\n lastMessage != null &&\n lastMessage.getType() === 'tool' &&\n typeof lastMessage.name === 'string' &&\n lastMessage.name.startsWith(Constants.LC_TRANSFER_TO_)\n ) {\n /** Handoff occurred - extract destination and navigate there exclusively */\n const handoffDest = lastMessage.name.replace(\n Constants.LC_TRANSFER_TO_,\n ''\n );\n return new Command({\n update: result,\n goto: handoffDest,\n });\n } else {\n /** No handoff - proceed with direct edges */\n const directDests = Array.from(directDestinations);\n if (directDests.length === 1) {\n return new Command({\n update: result,\n goto: directDests[0],\n });\n } else if (directDests.length > 1) {\n /** Multiple direct destinations - they'll run in parallel */\n return new Command({\n update: result,\n goto: directDests,\n });\n }\n }\n }\n\n /** No special routing needed - return state normally */\n return result;\n };\n\n /** Wrapped agent as a node with its possible destinations */\n builder.addNode(agentId, agentWrapper, {\n ends: Array.from(allDestinations),\n });\n }\n\n // Add starting edges for all starting nodes\n for (const startNode of this.startingNodes) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n builder.addEdge(START, startNode);\n }\n\n /**\n * Add direct edges for automatic transitions\n * Group edges by destination to handle fan-in scenarios\n */\n const edgesByDestination = new Map<string, t.GraphEdge[]>();\n\n for (const edge of this.directEdges) {\n const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];\n for (const destination of destinations) {\n if (!edgesByDestination.has(destination)) {\n edgesByDestination.set(destination, []);\n }\n edgesByDestination.get(destination)!.push(edge);\n }\n }\n\n for (const [destination, edges] of edgesByDestination) {\n /** Checks if this is a fan-in scenario with prompt instructions */\n const edgesWithPrompt = edges.filter(\n (edge) => edge.prompt != null && edge.prompt !== ''\n );\n\n if (edgesWithPrompt.length > 0) {\n /**\n * Single wrapper node for destination (Fan-in with prompt)\n */\n const wrapperNodeId = `fan_in_${destination}_prompt`;\n /**\n * First edge's `prompt`\n * (they should all be the same for fan-in)\n */\n const prompt = edgesWithPrompt[0].prompt;\n /**\n * First edge's `excludeResults` flag\n * (they should all be the same for fan-in)\n */\n const excludeResults = edgesWithPrompt[0].excludeResults;\n\n builder.addNode(wrapperNodeId, async (state: t.BaseGraphState) => {\n let promptText: string | undefined;\n let effectiveExcludeResults = excludeResults;\n\n if (typeof prompt === 'function') {\n promptText = await prompt(state.messages, this.startIndex);\n } else if (prompt != null) {\n if (prompt.includes('{results}')) {\n const resultsMessages = state.messages.slice(this.startIndex);\n const resultsString = getBufferString(resultsMessages);\n const promptTemplate = PromptTemplate.fromTemplate(prompt);\n const result = await promptTemplate.invoke({\n results: resultsString,\n });\n promptText = result.value;\n effectiveExcludeResults =\n excludeResults !== false && promptText !== '';\n } else {\n promptText = prompt;\n }\n }\n\n if (promptText != null && promptText !== '') {\n if (\n effectiveExcludeResults == null ||\n effectiveExcludeResults === false\n ) {\n return {\n messages: [new HumanMessage(promptText)],\n };\n }\n\n /** When `excludeResults` is true, use agentMessages channel\n * to pass filtered messages + prompt to the destination agent\n */\n const filteredMessages = state.messages.slice(0, this.startIndex);\n return {\n messages: [new HumanMessage(promptText)],\n agentMessages: messagesStateReducer(filteredMessages, [\n new HumanMessage(promptText),\n ]),\n };\n }\n\n /** No prompt needed, return empty update */\n return {};\n });\n\n /** Add edges from all sources to the wrapper, then wrapper to destination */\n for (const edge of edges) {\n const sources = Array.isArray(edge.from) ? edge.from : [edge.from];\n for (const source of sources) {\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n builder.addEdge(source, wrapperNodeId);\n }\n }\n\n /** Single edge from wrapper to destination */\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n builder.addEdge(wrapperNodeId, destination);\n } else {\n /** No prompt instructions, add direct edges (skip if source uses Command routing) */\n for (const edge of edges) {\n const sources = Array.isArray(edge.from) ? edge.from : [edge.from];\n for (const source of sources) {\n /** Check if this source node has both handoff and direct edges */\n const sourceHandoffEdges = this.handoffEdges.filter((e) => {\n const eSources = Array.isArray(e.from) ? e.from : [e.from];\n return eSources.includes(source);\n });\n const sourceDirectEdges = this.directEdges.filter((e) => {\n const eSources = Array.isArray(e.from) ? e.from : [e.from];\n return eSources.includes(source);\n });\n\n /** Skip adding edge if source uses Command routing (has both types) */\n if (sourceHandoffEdges.length > 0 && sourceDirectEdges.length > 0) {\n continue;\n }\n\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n builder.addEdge(source, destination);\n }\n }\n }\n }\n\n return builder.compile(this.compileOptions as unknown as never);\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAuBA;;;;;;;;;;;;;AAaG;AACG,MAAO,eAAgB,SAAQ,aAAa,CAAA;AACxC,IAAA,KAAK;AACL,IAAA,aAAa,GAAgB,IAAI,GAAG,EAAE;IACtC,WAAW,GAAkB,EAAE;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