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