@librechat/agents 2.4.322 → 3.0.0-rc2

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 (266) 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 +14 -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 -212
  8. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  9. package/dist/cjs/graphs/MultiAgentGraph.cjs +422 -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 +389 -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 +120 -81
  41. package/dist/cjs/run.cjs.map +1 -1
  42. package/dist/cjs/stream.cjs +85 -51
  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 +15 -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 -214
  75. package/dist/esm/graphs/Graph.mjs.map +1 -1
  76. package/dist/esm/graphs/MultiAgentGraph.mjs +420 -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 +388 -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 +121 -83
  108. package/dist/esm/run.mjs.map +1 -1
  109. package/dist/esm/stream.mjs +87 -54
  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 +15 -6
  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 +37 -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 +72 -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/scripts/ant_web_search.d.ts +1 -0
  161. package/dist/types/scripts/args.d.ts +2 -1
  162. package/dist/types/scripts/handoff-test.d.ts +1 -0
  163. package/dist/types/scripts/multi-agent-conditional.d.ts +1 -0
  164. package/dist/types/scripts/multi-agent-parallel.d.ts +1 -0
  165. package/dist/types/scripts/multi-agent-sequence.d.ts +1 -0
  166. package/dist/types/scripts/multi-agent-supervisor.d.ts +1 -0
  167. package/dist/types/scripts/multi-agent-test.d.ts +1 -0
  168. package/dist/types/scripts/test-custom-prompt-key.d.ts +2 -0
  169. package/dist/types/scripts/test-handoff-input.d.ts +2 -0
  170. package/dist/types/scripts/test-multi-agent-list-handoff.d.ts +2 -0
  171. package/dist/types/stream.d.ts +10 -3
  172. package/dist/types/tools/CodeExecutor.d.ts +2 -2
  173. package/dist/types/tools/ToolNode.d.ts +1 -1
  174. package/dist/types/tools/handlers.d.ts +17 -4
  175. package/dist/types/tools/search/anthropic.d.ts +16 -0
  176. package/dist/types/tools/search/firecrawl.d.ts +15 -0
  177. package/dist/types/tools/search/rerankers.d.ts +0 -1
  178. package/dist/types/tools/search/types.d.ts +30 -9
  179. package/dist/types/types/graph.d.ts +129 -15
  180. package/dist/types/types/llm.d.ts +24 -10
  181. package/dist/types/types/run.d.ts +46 -8
  182. package/dist/types/types/stream.d.ts +16 -2
  183. package/dist/types/types/tools.d.ts +1 -1
  184. package/dist/types/utils/events.d.ts +6 -0
  185. package/dist/types/utils/title.d.ts +2 -1
  186. package/dist/types/utils/tokens.d.ts +24 -0
  187. package/package.json +37 -17
  188. package/src/agents/AgentContext.ts +315 -0
  189. package/src/common/enum.ts +14 -5
  190. package/src/events.ts +24 -13
  191. package/src/graphs/Graph.ts +495 -312
  192. package/src/graphs/MultiAgentGraph.ts +498 -0
  193. package/src/graphs/index.ts +2 -1
  194. package/src/llm/anthropic/Jacob_Lee_Resume_2023.pdf +0 -0
  195. package/src/llm/anthropic/index.ts +78 -13
  196. package/src/llm/anthropic/llm.spec.ts +491 -115
  197. package/src/llm/anthropic/types.ts +39 -3
  198. package/src/llm/anthropic/utils/message_inputs.ts +67 -11
  199. package/src/llm/anthropic/utils/message_outputs.ts +21 -2
  200. package/src/llm/anthropic/utils/output_parsers.ts +25 -6
  201. package/src/llm/anthropic/utils/tools.ts +29 -0
  202. package/src/llm/google/index.ts +218 -0
  203. package/src/llm/google/types.ts +43 -0
  204. package/src/llm/google/utils/common.ts +646 -0
  205. package/src/llm/google/utils/tools.ts +160 -0
  206. package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -0
  207. package/src/llm/ollama/index.ts +89 -0
  208. package/src/llm/ollama/utils.ts +193 -0
  209. package/src/llm/openai/index.ts +600 -14
  210. package/src/llm/openai/types.ts +24 -0
  211. package/src/llm/openai/utils/index.ts +912 -0
  212. package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
  213. package/src/llm/providers.ts +10 -9
  214. package/src/llm/text.ts +26 -7
  215. package/src/llm/vertexai/index.ts +360 -0
  216. package/src/messages/reducer.ts +80 -0
  217. package/src/run.ts +181 -112
  218. package/src/scripts/ant_web_search.ts +158 -0
  219. package/src/scripts/args.ts +12 -8
  220. package/src/scripts/cli4.ts +29 -21
  221. package/src/scripts/cli5.ts +29 -21
  222. package/src/scripts/code_exec.ts +54 -23
  223. package/src/scripts/code_exec_files.ts +48 -17
  224. package/src/scripts/code_exec_simple.ts +46 -27
  225. package/src/scripts/handoff-test.ts +135 -0
  226. package/src/scripts/image.ts +52 -20
  227. package/src/scripts/multi-agent-conditional.ts +220 -0
  228. package/src/scripts/multi-agent-example-output.md +110 -0
  229. package/src/scripts/multi-agent-parallel.ts +341 -0
  230. package/src/scripts/multi-agent-sequence.ts +212 -0
  231. package/src/scripts/multi-agent-supervisor.ts +361 -0
  232. package/src/scripts/multi-agent-test.ts +186 -0
  233. package/src/scripts/search.ts +1 -9
  234. package/src/scripts/simple.ts +25 -10
  235. package/src/scripts/test-custom-prompt-key.ts +145 -0
  236. package/src/scripts/test-handoff-input.ts +110 -0
  237. package/src/scripts/test-multi-agent-list-handoff.ts +258 -0
  238. package/src/scripts/tools.ts +48 -18
  239. package/src/specs/anthropic.simple.test.ts +150 -34
  240. package/src/specs/azure.simple.test.ts +325 -0
  241. package/src/specs/openai.simple.test.ts +140 -33
  242. package/src/specs/openrouter.simple.test.ts +107 -0
  243. package/src/specs/prune.test.ts +4 -9
  244. package/src/specs/reasoning.test.ts +80 -44
  245. package/src/specs/token-memoization.test.ts +39 -0
  246. package/src/stream.test.ts +94 -0
  247. package/src/stream.ts +139 -60
  248. package/src/tools/ToolNode.ts +21 -7
  249. package/src/tools/handlers.ts +192 -18
  250. package/src/tools/search/anthropic.ts +51 -0
  251. package/src/tools/search/firecrawl.ts +69 -20
  252. package/src/tools/search/format.ts +6 -8
  253. package/src/tools/search/rerankers.ts +7 -40
  254. package/src/tools/search/search.ts +97 -16
  255. package/src/tools/search/tool.ts +5 -2
  256. package/src/tools/search/types.ts +30 -10
  257. package/src/tools/search/utils.ts +1 -1
  258. package/src/types/graph.ts +315 -103
  259. package/src/types/llm.ts +25 -12
  260. package/src/types/run.ts +51 -13
  261. package/src/types/stream.ts +22 -1
  262. package/src/types/tools.ts +16 -10
  263. package/src/utils/events.ts +32 -0
  264. package/src/utils/llmConfig.ts +19 -7
  265. package/src/utils/title.ts +104 -30
  266. package/src/utils/tokens.ts +69 -10
@@ -0,0 +1,498 @@
1
+ import { z } from 'zod';
2
+ import { tool } from '@langchain/core/tools';
3
+ import {
4
+ ToolMessage,
5
+ HumanMessage,
6
+ getBufferString,
7
+ } from '@langchain/core/messages';
8
+ import { ChatPromptTemplate } from '@langchain/core/prompts';
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
+
23
+ /**
24
+ * MultiAgentGraph extends StandardGraph to support dynamic multi-agent workflows
25
+ * with handoffs, fan-in/fan-out, and other composable patterns
26
+ */
27
+ export class MultiAgentGraph extends StandardGraph {
28
+ private edges: t.GraphEdge[];
29
+ private startingNodes: Set<string> = new Set();
30
+ private directEdges: t.GraphEdge[] = [];
31
+ private handoffEdges: t.GraphEdge[] = [];
32
+
33
+ constructor(input: t.MultiAgentGraphInput) {
34
+ super(input);
35
+ this.edges = input.edges;
36
+ this.categorizeEdges();
37
+ this.analyzeGraph();
38
+ this.createHandoffTools();
39
+ }
40
+
41
+ /**
42
+ * Categorize edges into handoff and direct types
43
+ */
44
+ private categorizeEdges(): void {
45
+ for (const edge of this.edges) {
46
+ // Default behavior: edges with conditions or explicit 'handoff' type are handoff edges
47
+ // Edges with explicit 'direct' type or multi-destination without conditions are direct edges
48
+ if (edge.edgeType === 'direct') {
49
+ this.directEdges.push(edge);
50
+ } else if (edge.edgeType === 'handoff' || edge.condition != null) {
51
+ this.handoffEdges.push(edge);
52
+ } else {
53
+ // Default: single-to-single edges are handoff, single-to-multiple are direct
54
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
55
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
56
+
57
+ if (sources.length === 1 && destinations.length > 1) {
58
+ // Fan-out pattern defaults to direct
59
+ this.directEdges.push(edge);
60
+ } else {
61
+ // Everything else defaults to handoff
62
+ this.handoffEdges.push(edge);
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Analyze graph structure to determine starting nodes and connections
70
+ */
71
+ private analyzeGraph(): void {
72
+ const hasIncomingEdge = new Set<string>();
73
+
74
+ // Track all nodes that have incoming edges
75
+ for (const edge of this.edges) {
76
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
77
+ destinations.forEach((dest) => hasIncomingEdge.add(dest));
78
+ }
79
+
80
+ // Starting nodes are those without incoming edges
81
+ for (const agentId of this.agentContexts.keys()) {
82
+ if (!hasIncomingEdge.has(agentId)) {
83
+ this.startingNodes.add(agentId);
84
+ }
85
+ }
86
+
87
+ // If no starting nodes found, use the first agent
88
+ if (this.startingNodes.size === 0 && this.agentContexts.size > 0) {
89
+ this.startingNodes.add(this.agentContexts.keys().next().value!);
90
+ }
91
+ }
92
+
93
+ /**
94
+ * Create handoff tools for agents based on handoff edges only
95
+ */
96
+ private createHandoffTools(): void {
97
+ // Group handoff edges by source agent(s)
98
+ const handoffsByAgent = new Map<string, t.GraphEdge[]>();
99
+
100
+ // Only process handoff edges for tool creation
101
+ for (const edge of this.handoffEdges) {
102
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
103
+ sources.forEach((source) => {
104
+ if (!handoffsByAgent.has(source)) {
105
+ handoffsByAgent.set(source, []);
106
+ }
107
+ handoffsByAgent.get(source)!.push(edge);
108
+ });
109
+ }
110
+
111
+ // Create handoff tools for each agent
112
+ for (const [agentId, edges] of handoffsByAgent) {
113
+ const agentContext = this.agentContexts.get(agentId);
114
+ if (!agentContext) continue;
115
+
116
+ // Create handoff tools for this agent's outgoing edges
117
+ const handoffTools: t.GenericTool[] = [];
118
+ for (const edge of edges) {
119
+ handoffTools.push(...this.createHandoffToolsForEdge(edge));
120
+ }
121
+
122
+ // Add handoff tools to the agent's existing tools
123
+ if (!agentContext.tools) {
124
+ agentContext.tools = [];
125
+ }
126
+ agentContext.tools.push(...handoffTools);
127
+
128
+ // Update tool map
129
+ for (const tool of handoffTools) {
130
+ if (!agentContext.toolMap) {
131
+ agentContext.toolMap = new Map();
132
+ }
133
+ agentContext.toolMap.set(tool.name, tool);
134
+ }
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Create handoff tools for an edge (handles multiple destinations)
140
+ */
141
+ private createHandoffToolsForEdge(edge: t.GraphEdge): t.GenericTool[] {
142
+ const tools: t.GenericTool[] = [];
143
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
144
+
145
+ /** If there's a condition, create a single conditional handoff tool */
146
+ if (edge.condition != null) {
147
+ const toolName = 'conditional_transfer';
148
+ const toolDescription =
149
+ edge.description ?? 'Conditionally transfer control based on state';
150
+
151
+ /** Check if we have a prompt for handoff input */
152
+ const hasHandoffInput =
153
+ edge.prompt != null && typeof edge.prompt === 'string';
154
+ const handoffInputDescription = hasHandoffInput ? edge.prompt : undefined;
155
+ const promptKey = edge.promptKey ?? 'instructions';
156
+
157
+ tools.push(
158
+ tool(
159
+ async (input: Record<string, unknown>, config) => {
160
+ const state = getCurrentTaskInput() as t.BaseGraphState;
161
+ const toolCallId =
162
+ (config as ToolRunnableConfig | undefined)?.toolCall?.id ??
163
+ 'unknown';
164
+
165
+ /** Evaluated condition */
166
+ const result = edge.condition!(state);
167
+ let destination: string;
168
+
169
+ if (typeof result === 'boolean') {
170
+ /** If true, use first destination; if false, don't transfer */
171
+ if (!result) return null;
172
+ destination = destinations[0];
173
+ } else if (typeof result === 'string') {
174
+ destination = result;
175
+ } else {
176
+ /** Array of destinations - for now, use the first */
177
+ destination = Array.isArray(result) ? result[0] : destinations[0];
178
+ }
179
+
180
+ let content = `Conditionally transferred to ${destination}`;
181
+ if (
182
+ hasHandoffInput &&
183
+ promptKey in input &&
184
+ input[promptKey] != null
185
+ ) {
186
+ content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
187
+ }
188
+
189
+ const toolMessage = new ToolMessage({
190
+ content,
191
+ name: toolName,
192
+ tool_call_id: toolCallId,
193
+ });
194
+
195
+ return new Command({
196
+ goto: destination,
197
+ update: { messages: state.messages.concat(toolMessage) },
198
+ graph: Command.PARENT,
199
+ });
200
+ },
201
+ {
202
+ name: toolName,
203
+ schema: hasHandoffInput
204
+ ? z.object({
205
+ [promptKey]: z
206
+ .string()
207
+ .optional()
208
+ .describe(handoffInputDescription as string),
209
+ })
210
+ : z.object({}),
211
+ description: toolDescription,
212
+ }
213
+ )
214
+ );
215
+ } else {
216
+ /** Create individual tools for each destination */
217
+ for (const destination of destinations) {
218
+ const toolName = `transfer_to_${destination}`;
219
+ const toolDescription =
220
+ edge.description ?? `Transfer control to agent '${destination}'`;
221
+
222
+ /** Check if we have a prompt for handoff input */
223
+ const hasHandoffInput =
224
+ edge.prompt != null && typeof edge.prompt === 'string';
225
+ const handoffInputDescription = hasHandoffInput
226
+ ? edge.prompt
227
+ : undefined;
228
+ const promptKey = edge.promptKey ?? 'instructions';
229
+
230
+ tools.push(
231
+ tool(
232
+ async (input: Record<string, unknown>, config) => {
233
+ const toolCallId =
234
+ (config as ToolRunnableConfig | undefined)?.toolCall?.id ??
235
+ 'unknown';
236
+
237
+ let content = `Successfully transferred to ${destination}`;
238
+ if (
239
+ hasHandoffInput &&
240
+ promptKey in input &&
241
+ input[promptKey] != null
242
+ ) {
243
+ content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
244
+ }
245
+
246
+ const toolMessage = new ToolMessage({
247
+ content,
248
+ name: toolName,
249
+ tool_call_id: toolCallId,
250
+ });
251
+
252
+ const state = getCurrentTaskInput() as t.BaseGraphState;
253
+
254
+ return new Command({
255
+ goto: destination,
256
+ update: { messages: state.messages.concat(toolMessage) },
257
+ graph: Command.PARENT,
258
+ });
259
+ },
260
+ {
261
+ name: toolName,
262
+ schema: hasHandoffInput
263
+ ? z.object({
264
+ [promptKey]: z
265
+ .string()
266
+ .optional()
267
+ .describe(handoffInputDescription as string),
268
+ })
269
+ : z.object({}),
270
+ description: toolDescription,
271
+ }
272
+ )
273
+ );
274
+ }
275
+ }
276
+
277
+ return tools;
278
+ }
279
+
280
+ /**
281
+ * Create a complete agent subgraph (similar to createReactAgent)
282
+ */
283
+ private createAgentSubgraph(agentId: string): t.CompiledAgentWorfklow {
284
+ /** This is essentially the same as `createAgentNode` from `StandardGraph` */
285
+ return this.createAgentNode(agentId);
286
+ }
287
+
288
+ /**
289
+ * Create the multi-agent workflow with dynamic handoffs
290
+ */
291
+ override createWorkflow(): t.CompiledMultiAgentWorkflow {
292
+ const StateAnnotation = Annotation.Root({
293
+ messages: Annotation<BaseMessage[]>({
294
+ reducer: (a, b) => {
295
+ if (!a.length) {
296
+ this.startIndex = a.length + b.length;
297
+ }
298
+ const result = messagesStateReducer(a, b);
299
+ this.messages = result;
300
+ return result;
301
+ },
302
+ default: () => [],
303
+ }),
304
+ /** Channel for passing filtered messages to agents when excludeResults is true */
305
+ agentMessages: Annotation<BaseMessage[]>({
306
+ /** Replaces state entirely */
307
+ reducer: (a, b) => b,
308
+ default: () => [],
309
+ }),
310
+ });
311
+
312
+ const builder = new StateGraph(StateAnnotation);
313
+
314
+ // Add all agents as complete subgraphs
315
+ for (const [agentId] of this.agentContexts) {
316
+ // Get all possible destinations for this agent
317
+ const handoffDestinations = new Set<string>();
318
+ const directDestinations = new Set<string>();
319
+
320
+ // Check handoff edges for destinations
321
+ for (const edge of this.handoffEdges) {
322
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
323
+ if (sources.includes(agentId) === true) {
324
+ const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
325
+ dests.forEach((dest) => handoffDestinations.add(dest));
326
+ }
327
+ }
328
+
329
+ // Check direct edges for destinations
330
+ for (const edge of this.directEdges) {
331
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
332
+ if (sources.includes(agentId) === true) {
333
+ const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
334
+ dests.forEach((dest) => directDestinations.add(dest));
335
+ }
336
+ }
337
+
338
+ /** If agent has handoff destinations, add END to possible ends
339
+ * If agent only has direct destinations, it naturally ends without explicit END
340
+ */
341
+ const destinations = new Set([...handoffDestinations]);
342
+ if (handoffDestinations.size > 0 || directDestinations.size === 0) {
343
+ destinations.add(END);
344
+ }
345
+
346
+ /** Agent subgraph (includes agent + tools) */
347
+ const agentSubgraph = this.createAgentSubgraph(agentId);
348
+
349
+ /** Wrapper function that handles agentMessages channel */
350
+ const agentWrapper = async (
351
+ state: t.MultiAgentGraphState
352
+ ): Promise<t.MultiAgentGraphState> => {
353
+ if (state.agentMessages != null && state.agentMessages.length > 0) {
354
+ /** Temporary state with messages replaced by `agentMessages` */
355
+ const transformedState: t.MultiAgentGraphState = {
356
+ ...state,
357
+ messages: state.agentMessages,
358
+ };
359
+ const result = await agentSubgraph.invoke(transformedState);
360
+ return {
361
+ ...result,
362
+ /** Clear agentMessages for next agent */
363
+ agentMessages: [],
364
+ };
365
+ } else {
366
+ return await agentSubgraph.invoke(state);
367
+ }
368
+ };
369
+
370
+ /** Wrapped agent as a node with its possible destinations */
371
+ builder.addNode(agentId, agentWrapper, {
372
+ ends: Array.from(destinations),
373
+ });
374
+ }
375
+
376
+ // Add starting edges for all starting nodes
377
+ for (const startNode of this.startingNodes) {
378
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
379
+ /** @ts-ignore */
380
+ builder.addEdge(START, startNode);
381
+ }
382
+
383
+ /**
384
+ * Add direct edges for automatic transitions
385
+ * Group edges by destination to handle fan-in scenarios
386
+ */
387
+ const edgesByDestination = new Map<string, t.GraphEdge[]>();
388
+
389
+ for (const edge of this.directEdges) {
390
+ const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
391
+ for (const destination of destinations) {
392
+ if (!edgesByDestination.has(destination)) {
393
+ edgesByDestination.set(destination, []);
394
+ }
395
+ edgesByDestination.get(destination)!.push(edge);
396
+ }
397
+ }
398
+
399
+ for (const [destination, edges] of edgesByDestination) {
400
+ /** Checks if this is a fan-in scenario with prompt instructions */
401
+ const edgesWithPrompt = edges.filter(
402
+ (edge) => edge.prompt != null && edge.prompt !== ''
403
+ );
404
+
405
+ if (edgesWithPrompt.length > 0) {
406
+ /**
407
+ * Single wrapper node for destination (Fan-in with prompt)
408
+ */
409
+ const wrapperNodeId = `fan_in_${destination}_prompt`;
410
+ /**
411
+ * First edge's `prompt`
412
+ * (they should all be the same for fan-in)
413
+ */
414
+ const prompt = edgesWithPrompt[0].prompt;
415
+ /**
416
+ * First edge's `excludeResults` flag
417
+ * (they should all be the same for fan-in)
418
+ */
419
+ const excludeResults = edgesWithPrompt[0].excludeResults;
420
+
421
+ builder.addNode(wrapperNodeId, async (state: t.BaseGraphState) => {
422
+ let promptText: string | undefined;
423
+ let effectiveExcludeResults = excludeResults;
424
+
425
+ if (typeof prompt === 'function') {
426
+ promptText = prompt(state.messages, this.startIndex);
427
+ } else if (prompt != null) {
428
+ if (prompt.includes('{results}')) {
429
+ const resultsMessages = state.messages.slice(this.startIndex);
430
+ const resultsString = getBufferString(resultsMessages);
431
+ const promptTemplate = ChatPromptTemplate.fromTemplate(prompt);
432
+ const formattedPromptValue = await promptTemplate.invoke({
433
+ results: resultsString,
434
+ });
435
+ promptText = formattedPromptValue.messages[0].content.toString();
436
+ effectiveExcludeResults =
437
+ excludeResults !== false && promptText !== '';
438
+ } else {
439
+ promptText = prompt;
440
+ }
441
+ }
442
+
443
+ if (promptText != null && promptText !== '') {
444
+ if (
445
+ effectiveExcludeResults == null ||
446
+ effectiveExcludeResults === false
447
+ ) {
448
+ return {
449
+ messages: [new HumanMessage(promptText)],
450
+ };
451
+ }
452
+
453
+ /** When `excludeResults` is true, use agentMessages channel
454
+ * to pass filtered messages + prompt to the destination agent
455
+ */
456
+ const filteredMessages = state.messages.slice(0, this.startIndex);
457
+ return {
458
+ messages: [new HumanMessage(promptText)],
459
+ agentMessages: messagesStateReducer(filteredMessages, [
460
+ new HumanMessage(promptText),
461
+ ]),
462
+ };
463
+ }
464
+
465
+ /** No prompt needed, return empty update */
466
+ return {};
467
+ });
468
+
469
+ /** Add edges from all sources to the wrapper, then wrapper to destination */
470
+ for (const edge of edges) {
471
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
472
+ for (const source of sources) {
473
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
474
+ /** @ts-ignore */
475
+ builder.addEdge(source, wrapperNodeId);
476
+ }
477
+ }
478
+
479
+ /** Single edge from wrapper to destination */
480
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
481
+ /** @ts-ignore */
482
+ builder.addEdge(wrapperNodeId, destination);
483
+ } else {
484
+ /** No prompt instructions, add direct edges */
485
+ for (const edge of edges) {
486
+ const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
487
+ for (const source of sources) {
488
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
489
+ /** @ts-ignore */
490
+ builder.addEdge(source, destination);
491
+ }
492
+ }
493
+ }
494
+ }
495
+
496
+ return builder.compile(this.compileOptions as unknown as never);
497
+ }
498
+ }
@@ -1 +1,2 @@
1
- export * from './Graph';
1
+ export * from './Graph';
2
+ export * from './MultiAgentGraph';
@@ -9,6 +9,7 @@ import type {
9
9
  } from '@langchain/core/messages';
10
10
  import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
11
11
  import type { AnthropicInput } from '@langchain/anthropic';
12
+ import type { Anthropic } from '@anthropic-ai/sdk';
12
13
  import type {
13
14
  AnthropicMessageCreateParams,
14
15
  AnthropicStreamingMessageCreateParams,
@@ -18,6 +19,7 @@ import type {
18
19
  } from '@/llm/anthropic/types';
19
20
  import { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';
20
21
  import { _convertMessagesToAnthropicPayload } from './utils/message_inputs';
22
+ import { handleToolChoice } from './utils/tools';
21
23
  import { TextStream } from '@/llm/text';
22
24
 
23
25
  function _toolsInParams(
@@ -120,6 +122,13 @@ export type CustomAnthropicInput = AnthropicInput & {
120
122
  _lc_stream_delay?: number;
121
123
  } & BaseChatModelParams;
122
124
 
125
+ /**
126
+ * A type representing additional parameters that can be passed to the
127
+ * Anthropic API.
128
+ */
129
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
130
+ type Kwargs = Record<string, any>;
131
+
123
132
  export class CustomAnthropic extends ChatAnthropicMessages {
124
133
  _lc_stream_delay: number;
125
134
  private message_start: AnthropicMessageStartEvent | undefined;
@@ -128,9 +137,68 @@ export class CustomAnthropic extends ChatAnthropicMessages {
128
137
  private emitted_usage?: boolean;
129
138
  constructor(fields?: CustomAnthropicInput) {
130
139
  super(fields);
140
+ this.resetTokenEvents();
131
141
  this._lc_stream_delay = fields?._lc_stream_delay ?? 25;
132
142
  }
133
143
 
144
+ /**
145
+ * Get the parameters used to invoke the model
146
+ */
147
+ override invocationParams(
148
+ options?: this['ParsedCallOptions']
149
+ ): Omit<
150
+ AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams,
151
+ 'messages'
152
+ > &
153
+ Kwargs {
154
+ const tool_choice:
155
+ | Anthropic.Messages.ToolChoiceAuto
156
+ | Anthropic.Messages.ToolChoiceAny
157
+ | Anthropic.Messages.ToolChoiceTool
158
+ | undefined = handleToolChoice(options?.tool_choice);
159
+
160
+ if (this.thinking.type === 'enabled') {
161
+ if (this.topK !== -1 && (this.topK as number | undefined) != null) {
162
+ throw new Error('topK is not supported when thinking is enabled');
163
+ }
164
+ if (this.topP !== -1 && (this.topP as number | undefined) != null) {
165
+ throw new Error('topP is not supported when thinking is enabled');
166
+ }
167
+ if (
168
+ this.temperature !== 1 &&
169
+ (this.temperature as number | undefined) != null
170
+ ) {
171
+ throw new Error(
172
+ 'temperature is not supported when thinking is enabled'
173
+ );
174
+ }
175
+
176
+ return {
177
+ model: this.model,
178
+ stop_sequences: options?.stop ?? this.stopSequences,
179
+ stream: this.streaming,
180
+ max_tokens: this.maxTokens,
181
+ tools: this.formatStructuredToolToAnthropic(options?.tools),
182
+ tool_choice,
183
+ thinking: this.thinking,
184
+ ...this.invocationKwargs,
185
+ };
186
+ }
187
+ return {
188
+ model: this.model,
189
+ temperature: this.temperature,
190
+ top_k: this.topK,
191
+ top_p: this.topP,
192
+ stop_sequences: options?.stop ?? this.stopSequences,
193
+ stream: this.streaming,
194
+ max_tokens: this.maxTokens,
195
+ tools: this.formatStructuredToolToAnthropic(options?.tools),
196
+ tool_choice,
197
+ thinking: this.thinking,
198
+ ...this.invocationKwargs,
199
+ };
200
+ }
201
+
134
202
  /**
135
203
  * Get stream usage as returned by this client's API response.
136
204
  * @returns The stream usage object.
@@ -209,6 +277,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
209
277
  options: this['ParsedCallOptions'],
210
278
  runManager?: CallbackManagerForLLMRun
211
279
  ): AsyncGenerator<ChatGenerationChunk> {
280
+ this.resetTokenEvents();
212
281
  const params = this.invocationParams(options);
213
282
  const formattedMessages = _convertMessagesToAnthropicPayload(messages);
214
283
  const payload = {
@@ -221,16 +290,9 @@ export class CustomAnthropic extends ChatAnthropicMessages {
221
290
  !_documentsInParams(payload) &&
222
291
  !_thinkingInParams(payload);
223
292
 
224
- const stream = await this.createStreamWithRetry(
225
- {
226
- ...params,
227
- ...formattedMessages,
228
- stream: true,
229
- },
230
- {
231
- headers: options.headers,
232
- }
233
- );
293
+ const stream = await this.createStreamWithRetry(payload, {
294
+ headers: options.headers,
295
+ });
234
296
 
235
297
  const shouldStreamUsage = this.streamUsage ?? options.streamUsage;
236
298
 
@@ -263,7 +325,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
263
325
  if (
264
326
  !tokenType ||
265
327
  tokenType === 'input' ||
266
- (token === '' && usageMetadata)
328
+ (token === '' && (usageMetadata != null || chunk.id != null))
267
329
  ) {
268
330
  const generationChunk = this.createGenerationChunk({
269
331
  token,
@@ -290,10 +352,13 @@ export class CustomAnthropic extends ChatAnthropicMessages {
290
352
  maxChunkSize: 8,
291
353
  });
292
354
 
293
- const generator = textStream.generateText();
355
+ const generator = textStream.generateText(options.signal);
294
356
  try {
295
357
  let emittedUsage = false;
296
358
  for await (const currentToken of generator) {
359
+ if ((options.signal as AbortSignal | undefined)?.aborted === true) {
360
+ break;
361
+ }
297
362
  const newChunk = cloneChunk(currentToken, tokenType, chunk);
298
363
 
299
364
  const generationChunk = this.createGenerationChunk({
@@ -309,7 +374,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
309
374
  yield generationChunk;
310
375
 
311
376
  await runManager?.handleLLMNewToken(
312
- token,
377
+ currentToken,
313
378
  undefined,
314
379
  undefined,
315
380
  undefined,