@librechat/agents 3.0.0-rc8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +6 -2
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +23 -2
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/graphs/MultiAgentGraph.cjs +108 -17
  6. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  7. package/dist/cjs/instrumentation.cjs +21 -0
  8. package/dist/cjs/instrumentation.cjs.map +1 -0
  9. package/dist/cjs/llm/anthropic/index.cjs +21 -2
  10. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  11. package/dist/cjs/llm/google/index.cjs +3 -0
  12. package/dist/cjs/llm/google/index.cjs.map +1 -1
  13. package/dist/cjs/llm/google/utils/common.cjs +13 -0
  14. package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
  15. package/dist/cjs/llm/ollama/index.cjs +3 -0
  16. package/dist/cjs/llm/ollama/index.cjs.map +1 -1
  17. package/dist/cjs/llm/openai/index.cjs +18 -3
  18. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  19. package/dist/cjs/llm/openai/utils/index.cjs +6 -1
  20. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  21. package/dist/cjs/llm/openrouter/index.cjs +5 -1
  22. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  23. package/dist/cjs/llm/vertexai/index.cjs +1 -1
  24. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  25. package/dist/cjs/main.cjs +8 -2
  26. package/dist/cjs/main.cjs.map +1 -1
  27. package/dist/cjs/messages/cache.cjs +49 -0
  28. package/dist/cjs/messages/cache.cjs.map +1 -0
  29. package/dist/cjs/messages/content.cjs +53 -0
  30. package/dist/cjs/messages/content.cjs.map +1 -0
  31. package/dist/cjs/messages/core.cjs +5 -1
  32. package/dist/cjs/messages/core.cjs.map +1 -1
  33. package/dist/cjs/messages/format.cjs +50 -59
  34. package/dist/cjs/messages/format.cjs.map +1 -1
  35. package/dist/cjs/messages/prune.cjs +28 -0
  36. package/dist/cjs/messages/prune.cjs.map +1 -1
  37. package/dist/cjs/run.cjs +57 -5
  38. package/dist/cjs/run.cjs.map +1 -1
  39. package/dist/cjs/stream.cjs +7 -0
  40. package/dist/cjs/stream.cjs.map +1 -1
  41. package/dist/cjs/tools/ToolNode.cjs +2 -0
  42. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  43. package/dist/cjs/tools/search/firecrawl.cjs +3 -1
  44. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
  45. package/dist/cjs/tools/search/rerankers.cjs +8 -6
  46. package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
  47. package/dist/cjs/tools/search/search.cjs +5 -5
  48. package/dist/cjs/tools/search/search.cjs.map +1 -1
  49. package/dist/cjs/tools/search/serper-scraper.cjs +132 -0
  50. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -0
  51. package/dist/cjs/tools/search/tool.cjs +46 -9
  52. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  53. package/dist/cjs/utils/handlers.cjs +70 -0
  54. package/dist/cjs/utils/handlers.cjs.map +1 -0
  55. package/dist/cjs/utils/misc.cjs +8 -1
  56. package/dist/cjs/utils/misc.cjs.map +1 -1
  57. package/dist/cjs/utils/title.cjs +54 -25
  58. package/dist/cjs/utils/title.cjs.map +1 -1
  59. package/dist/esm/agents/AgentContext.mjs +6 -2
  60. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  61. package/dist/esm/graphs/Graph.mjs +23 -2
  62. package/dist/esm/graphs/Graph.mjs.map +1 -1
  63. package/dist/esm/graphs/MultiAgentGraph.mjs +108 -17
  64. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  65. package/dist/esm/instrumentation.mjs +19 -0
  66. package/dist/esm/instrumentation.mjs.map +1 -0
  67. package/dist/esm/llm/anthropic/index.mjs +21 -2
  68. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  69. package/dist/esm/llm/google/index.mjs +3 -0
  70. package/dist/esm/llm/google/index.mjs.map +1 -1
  71. package/dist/esm/llm/google/utils/common.mjs +13 -0
  72. package/dist/esm/llm/google/utils/common.mjs.map +1 -1
  73. package/dist/esm/llm/ollama/index.mjs +3 -0
  74. package/dist/esm/llm/ollama/index.mjs.map +1 -1
  75. package/dist/esm/llm/openai/index.mjs +18 -3
  76. package/dist/esm/llm/openai/index.mjs.map +1 -1
  77. package/dist/esm/llm/openai/utils/index.mjs +6 -1
  78. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  79. package/dist/esm/llm/openrouter/index.mjs +5 -1
  80. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  81. package/dist/esm/llm/vertexai/index.mjs +1 -1
  82. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  83. package/dist/esm/main.mjs +5 -2
  84. package/dist/esm/main.mjs.map +1 -1
  85. package/dist/esm/messages/cache.mjs +47 -0
  86. package/dist/esm/messages/cache.mjs.map +1 -0
  87. package/dist/esm/messages/content.mjs +51 -0
  88. package/dist/esm/messages/content.mjs.map +1 -0
  89. package/dist/esm/messages/core.mjs +5 -1
  90. package/dist/esm/messages/core.mjs.map +1 -1
  91. package/dist/esm/messages/format.mjs +50 -58
  92. package/dist/esm/messages/format.mjs.map +1 -1
  93. package/dist/esm/messages/prune.mjs +28 -0
  94. package/dist/esm/messages/prune.mjs.map +1 -1
  95. package/dist/esm/run.mjs +57 -5
  96. package/dist/esm/run.mjs.map +1 -1
  97. package/dist/esm/stream.mjs +7 -0
  98. package/dist/esm/stream.mjs.map +1 -1
  99. package/dist/esm/tools/ToolNode.mjs +2 -0
  100. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  101. package/dist/esm/tools/search/firecrawl.mjs +3 -1
  102. package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
  103. package/dist/esm/tools/search/rerankers.mjs +8 -6
  104. package/dist/esm/tools/search/rerankers.mjs.map +1 -1
  105. package/dist/esm/tools/search/search.mjs +5 -5
  106. package/dist/esm/tools/search/search.mjs.map +1 -1
  107. package/dist/esm/tools/search/serper-scraper.mjs +129 -0
  108. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -0
  109. package/dist/esm/tools/search/tool.mjs +46 -9
  110. package/dist/esm/tools/search/tool.mjs.map +1 -1
  111. package/dist/esm/utils/handlers.mjs +68 -0
  112. package/dist/esm/utils/handlers.mjs.map +1 -0
  113. package/dist/esm/utils/misc.mjs +8 -2
  114. package/dist/esm/utils/misc.mjs.map +1 -1
  115. package/dist/esm/utils/title.mjs +54 -25
  116. package/dist/esm/utils/title.mjs.map +1 -1
  117. package/dist/types/agents/AgentContext.d.ts +4 -1
  118. package/dist/types/graphs/MultiAgentGraph.d.ts +11 -1
  119. package/dist/types/llm/anthropic/index.d.ts +3 -0
  120. package/dist/types/llm/google/index.d.ts +1 -0
  121. package/dist/types/llm/ollama/index.d.ts +1 -0
  122. package/dist/types/llm/openai/index.d.ts +4 -0
  123. package/dist/types/llm/openrouter/index.d.ts +4 -2
  124. package/dist/types/llm/vertexai/index.d.ts +1 -1
  125. package/dist/types/messages/cache.d.ts +8 -0
  126. package/dist/types/messages/content.d.ts +7 -0
  127. package/dist/types/messages/format.d.ts +22 -25
  128. package/dist/types/messages/index.d.ts +2 -0
  129. package/dist/types/run.d.ts +2 -1
  130. package/dist/types/tools/search/firecrawl.d.ts +2 -1
  131. package/dist/types/tools/search/rerankers.d.ts +4 -1
  132. package/dist/types/tools/search/search.d.ts +1 -2
  133. package/dist/types/tools/search/serper-scraper.d.ts +59 -0
  134. package/dist/types/tools/search/tool.d.ts +25 -4
  135. package/dist/types/tools/search/types.d.ts +31 -1
  136. package/dist/types/types/graph.d.ts +3 -1
  137. package/dist/types/types/messages.d.ts +4 -0
  138. package/dist/types/utils/handlers.d.ts +34 -0
  139. package/dist/types/utils/index.d.ts +1 -0
  140. package/dist/types/utils/misc.d.ts +1 -0
  141. package/package.json +8 -2
  142. package/src/agents/AgentContext.ts +8 -0
  143. package/src/graphs/Graph.ts +31 -2
  144. package/src/graphs/MultiAgentGraph.ts +125 -18
  145. package/src/instrumentation.ts +22 -0
  146. package/src/llm/anthropic/index.ts +23 -2
  147. package/src/llm/anthropic/llm.spec.ts +1 -1
  148. package/src/llm/google/index.ts +4 -0
  149. package/src/llm/google/utils/common.ts +14 -0
  150. package/src/llm/ollama/index.ts +3 -0
  151. package/src/llm/openai/index.ts +17 -4
  152. package/src/llm/openai/utils/index.ts +7 -1
  153. package/src/llm/openrouter/index.ts +15 -6
  154. package/src/llm/vertexai/index.ts +2 -2
  155. package/src/messages/cache.test.ts +262 -0
  156. package/src/messages/cache.ts +56 -0
  157. package/src/messages/content.test.ts +362 -0
  158. package/src/messages/content.ts +63 -0
  159. package/src/messages/core.ts +5 -2
  160. package/src/messages/format.ts +65 -71
  161. package/src/messages/formatMessage.test.ts +418 -2
  162. package/src/messages/index.ts +2 -0
  163. package/src/messages/prune.ts +51 -0
  164. package/src/run.ts +82 -10
  165. package/src/scripts/ant_web_search.ts +1 -1
  166. package/src/scripts/handoff-test.ts +1 -1
  167. package/src/scripts/multi-agent-chain.ts +278 -0
  168. package/src/scripts/multi-agent-conditional.ts +4 -4
  169. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  170. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  171. package/src/scripts/multi-agent-parallel.ts +10 -8
  172. package/src/scripts/multi-agent-sequence.ts +3 -3
  173. package/src/scripts/multi-agent-supervisor.ts +5 -3
  174. package/src/scripts/multi-agent-test.ts +2 -2
  175. package/src/scripts/search.ts +5 -1
  176. package/src/scripts/simple.ts +8 -0
  177. package/src/scripts/test-custom-prompt-key.ts +4 -4
  178. package/src/scripts/test-handoff-input.ts +3 -3
  179. package/src/scripts/test-multi-agent-list-handoff.ts +2 -2
  180. package/src/scripts/tools.ts +4 -1
  181. package/src/specs/agent-handoffs.test.ts +889 -0
  182. package/src/stream.ts +9 -2
  183. package/src/tools/search/firecrawl.ts +5 -2
  184. package/src/tools/search/jina-reranker.test.ts +126 -0
  185. package/src/tools/search/rerankers.ts +11 -5
  186. package/src/tools/search/search.ts +6 -8
  187. package/src/tools/search/serper-scraper.ts +155 -0
  188. package/src/tools/search/tool.ts +49 -8
  189. package/src/tools/search/types.ts +46 -0
  190. package/src/types/graph.ts +6 -1
  191. package/src/types/messages.ts +4 -0
  192. package/src/utils/handlers.ts +107 -0
  193. package/src/utils/index.ts +2 -1
  194. package/src/utils/llmConfig.ts +35 -1
  195. package/src/utils/misc.ts +33 -21
  196. package/src/utils/title.ts +80 -40
  197. package/dist/types/scripts/ant_web_search.d.ts +0 -1
  198. package/dist/types/scripts/args.d.ts +0 -7
  199. package/dist/types/scripts/caching.d.ts +0 -1
  200. package/dist/types/scripts/cli.d.ts +0 -1
  201. package/dist/types/scripts/cli2.d.ts +0 -1
  202. package/dist/types/scripts/cli3.d.ts +0 -1
  203. package/dist/types/scripts/cli4.d.ts +0 -1
  204. package/dist/types/scripts/cli5.d.ts +0 -1
  205. package/dist/types/scripts/code_exec.d.ts +0 -1
  206. package/dist/types/scripts/code_exec_files.d.ts +0 -1
  207. package/dist/types/scripts/code_exec_simple.d.ts +0 -1
  208. package/dist/types/scripts/content.d.ts +0 -1
  209. package/dist/types/scripts/empty_input.d.ts +0 -1
  210. package/dist/types/scripts/handoff-test.d.ts +0 -1
  211. package/dist/types/scripts/image.d.ts +0 -1
  212. package/dist/types/scripts/memory.d.ts +0 -1
  213. package/dist/types/scripts/multi-agent-conditional.d.ts +0 -1
  214. package/dist/types/scripts/multi-agent-parallel.d.ts +0 -1
  215. package/dist/types/scripts/multi-agent-sequence.d.ts +0 -1
  216. package/dist/types/scripts/multi-agent-supervisor.d.ts +0 -1
  217. package/dist/types/scripts/multi-agent-test.d.ts +0 -1
  218. package/dist/types/scripts/search.d.ts +0 -1
  219. package/dist/types/scripts/simple.d.ts +0 -1
  220. package/dist/types/scripts/stream.d.ts +0 -1
  221. package/dist/types/scripts/test-custom-prompt-key.d.ts +0 -2
  222. package/dist/types/scripts/test-handoff-input.d.ts +0 -1
  223. package/dist/types/scripts/test-multi-agent-list-handoff.d.ts +0 -2
  224. package/dist/types/scripts/test-tools-before-handoff.d.ts +0 -1
  225. package/dist/types/scripts/thinking.d.ts +0 -1
  226. package/dist/types/scripts/tools.d.ts +0 -1
  227. package/dist/types/specs/spec.utils.d.ts +0 -1
  228. package/src/scripts/multi-agent-example-output.md +0 -110
  229. /package/dist/types/{scripts/abort.d.ts → instrumentation.d.ts} +0 -0
@@ -0,0 +1,310 @@
1
+ import { config } from 'dotenv';
2
+ config();
3
+
4
+ import { HumanMessage, BaseMessage } from '@langchain/core/messages';
5
+ import { Run } from '@/run';
6
+ import { Providers, GraphEvents, Constants } from '@/common';
7
+ import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
8
+ import { ToolEndHandler, ModelEndHandler } from '@/events';
9
+ import type * as t from '@/types';
10
+
11
+ const conversationHistory: BaseMessage[] = [];
12
+
13
+ /**
14
+ * Example of hybrid multi-agent system combining handoff and sequential patterns
15
+ *
16
+ * Graph structure:
17
+ * START -> primary_agent -> agent_b -> agent_c -> END
18
+ * |
19
+ * └─> standalone_agent -> END
20
+ *
21
+ * Because primary_agent has BOTH handoff and direct edges:
22
+ * - Uses Command-based routing for exclusive execution
23
+ * - The primary agent can either:
24
+ * 1. Handoff to standalone_agent (direct edge to agent_b is cancelled)
25
+ * 2. OR continue to agent_b -> agent_c (if no handoff occurs)
26
+ *
27
+ * This is automatic behavior when an agent has both edge types.
28
+ */
29
+ async function testHybridMultiAgent() {
30
+ console.log('Testing Hybrid Multi-Agent System (Sequential + Handoff)...\n');
31
+
32
+ // Define agents
33
+ const agents: t.AgentInputs[] = [
34
+ {
35
+ agentId: 'primary_agent',
36
+ provider: Providers.OPENAI,
37
+ clientOptions: {
38
+ modelName: 'gpt-4.1-mini',
39
+ apiKey: process.env.OPENAI_API_KEY,
40
+ },
41
+ instructions: `You are the Primary Agent in a hybrid workflow.
42
+
43
+ You have TWO options:
44
+ 1. If the request requires specialized expertise (complex analysis, deep technical knowledge, etc.),
45
+ use the "transfer_to_standalone_agent" tool to hand off to the Standalone Specialist
46
+ 2. If the request is straightforward and can be handled through standard processing,
47
+ just provide your initial response and it will automatically continue to Agent B
48
+
49
+ Be decisive - either handoff immediately or provide your response.
50
+ Start your response with "PRIMARY AGENT:" if you're handling it yourself.`,
51
+ maxContextTokens: 8000,
52
+ },
53
+ {
54
+ agentId: 'standalone_agent',
55
+ provider: Providers.OPENAI,
56
+ clientOptions: {
57
+ modelName: 'gpt-4.1',
58
+ apiKey: process.env.OPENAI_API_KEY,
59
+ },
60
+ instructions: `You are a Standalone Specialist Agent.
61
+ You only receive requests that require specialized expertise.
62
+
63
+ Provide a comprehensive, expert-level response to the request.
64
+ Start your response with "STANDALONE SPECIALIST:"
65
+ End with "Specialized analysis complete."`,
66
+ maxContextTokens: 8000,
67
+ },
68
+ {
69
+ agentId: 'agent_b',
70
+ provider: Providers.OPENAI,
71
+ clientOptions: {
72
+ modelName: 'gpt-4.1',
73
+ apiKey: process.env.OPENAI_API_KEY,
74
+ },
75
+ instructions: `You are Agent B in a sequential workflow.
76
+ You receive requests that the Primary Agent decided to handle through standard processing.
77
+
78
+ Your job is to:
79
+ 1. Build upon the Primary Agent's initial response
80
+ 2. Add additional processing or analysis (keep it brief, 2-3 sentences)
81
+ 3. Prepare the information for final processing by Agent C
82
+
83
+ Start your response with "AGENT B:" and end with "Passing to final processing..."`,
84
+ maxContextTokens: 8000,
85
+ },
86
+ {
87
+ agentId: 'agent_c',
88
+ provider: Providers.OPENAI,
89
+ clientOptions: {
90
+ modelName: 'gpt-4.1',
91
+ apiKey: process.env.OPENAI_API_KEY,
92
+ },
93
+ instructions: `You are Agent C, the final agent in the sequential workflow.
94
+
95
+ Your job is to:
96
+ 1. Review all previous processing from Primary Agent and Agent B
97
+ 2. Provide a final summary or conclusion
98
+ 3. Complete the standard workflow
99
+
100
+ Start your response with "AGENT C:" and end with "Standard workflow complete."`,
101
+ maxContextTokens: 8000,
102
+ },
103
+ ];
104
+
105
+ // Define edges combining handoff and direct patterns
106
+ const edges: t.GraphEdge[] = [
107
+ // Handoff edge: primary can transfer to standalone
108
+ {
109
+ from: 'primary_agent',
110
+ to: 'standalone_agent',
111
+ edgeType: 'handoff',
112
+ description: 'Transfer to standalone specialist for complex requests',
113
+ prompt: 'Specific instructions for the specialist',
114
+ },
115
+ // Direct edge - exclusive with handoffs (automatic when agent has both types)
116
+ {
117
+ from: 'primary_agent',
118
+ to: 'agent_b',
119
+ edgeType: 'direct',
120
+ description: 'Continue to Agent B only if no handoff occurs',
121
+ },
122
+ // Direct edge: agent_b automatically continues to agent_c
123
+ {
124
+ from: 'agent_b',
125
+ to: 'agent_c',
126
+ edgeType: 'direct',
127
+ description: 'Automatic progression from B to C',
128
+ },
129
+ ];
130
+
131
+ try {
132
+ // Test with different queries
133
+ const testQueries = [
134
+ {
135
+ query: 'What is the capital of France?',
136
+ expectedPath: 'sequential',
137
+ description: 'Simple query - should go through sequential flow',
138
+ },
139
+ // {
140
+ // query: 'Design a distributed microservices architecture for a real-time trading platform with sub-millisecond latency requirements',
141
+ // expectedPath: 'handoff',
142
+ // description: 'Complex query - should handoff to specialist',
143
+ // },
144
+ ];
145
+
146
+ const config = {
147
+ configurable: {
148
+ thread_id: 'hybrid-conversation-1',
149
+ },
150
+ streamMode: 'values',
151
+ version: 'v2' as const,
152
+ };
153
+
154
+ for (const test of testQueries) {
155
+ console.log(`\n${'='.repeat(70)}`);
156
+ console.log(`TEST: ${test.description}`);
157
+ console.log(`QUERY: "${test.query}"`);
158
+ console.log(`EXPECTED PATH: ${test.expectedPath}`);
159
+ console.log('='.repeat(70));
160
+
161
+ // Reset state
162
+ conversationHistory.length = 0;
163
+ conversationHistory.push(new HumanMessage(test.query));
164
+
165
+ // Create separate content aggregator for each test
166
+ const { contentParts, aggregateContent } = createContentAggregator();
167
+
168
+ // Track agent progression for this test
169
+ let currentAgent = '';
170
+ let handoffOccurred = false;
171
+
172
+ // Create custom handlers for this test
173
+ const customHandlers = {
174
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
175
+ [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
176
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
177
+ [GraphEvents.ON_RUN_STEP]: {
178
+ handle: (
179
+ event: GraphEvents.ON_RUN_STEP,
180
+ data: t.StreamEventData
181
+ ): void => {
182
+ const runStepData = data as any;
183
+ if (runStepData?.name) {
184
+ currentAgent = runStepData.name;
185
+ console.log(`\n[${currentAgent}] Processing...`);
186
+ }
187
+ aggregateContent({ event, data: data as t.RunStep });
188
+ },
189
+ },
190
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
191
+ handle: (
192
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
193
+ data: t.StreamEventData
194
+ ): void => {
195
+ const runStepData = data as any;
196
+ if (runStepData?.name) {
197
+ console.log(`✓ ${runStepData.name} completed`);
198
+ }
199
+ aggregateContent({
200
+ event,
201
+ data: data as unknown as { result: t.ToolEndEvent },
202
+ });
203
+ },
204
+ },
205
+ [GraphEvents.ON_MESSAGE_DELTA]: {
206
+ handle: (
207
+ event: GraphEvents.ON_MESSAGE_DELTA,
208
+ data: t.StreamEventData
209
+ ): void => {
210
+ // console.dir(data, { depth: null });
211
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
212
+ },
213
+ },
214
+ [GraphEvents.TOOL_START]: {
215
+ handle: (
216
+ _event: string,
217
+ data: t.StreamEventData,
218
+ metadata?: Record<string, unknown>
219
+ ): void => {
220
+ const toolData = data as any;
221
+ if (toolData?.name?.startsWith(Constants.LC_TRANSFER_TO_)) {
222
+ const specialist = toolData.name.replace(
223
+ Constants.LC_TRANSFER_TO_,
224
+ ''
225
+ );
226
+ console.log(`\n🔀 Transferring to ${specialist}...`);
227
+ handoffOccurred = true;
228
+ }
229
+ },
230
+ },
231
+ };
232
+
233
+ // Create a new run configuration for each test
234
+ const runConfig: t.RunConfig = {
235
+ runId: `hybrid-multi-agent-${test.expectedPath}-${Date.now()}`,
236
+ graphConfig: {
237
+ type: 'multi-agent',
238
+ agents,
239
+ edges,
240
+ },
241
+ customHandlers,
242
+ returnContent: true,
243
+ };
244
+
245
+ // Create and execute a new run for this test
246
+ const run = await Run.create(runConfig);
247
+
248
+ console.log('\nProcessing request...');
249
+
250
+ // Process with streaming
251
+ const inputs = {
252
+ messages: conversationHistory,
253
+ };
254
+
255
+ const finalContentParts = await run.processStream(inputs, config);
256
+ const finalMessages = run.getRunMessages();
257
+
258
+ if (finalMessages) {
259
+ conversationHistory.push(...finalMessages);
260
+ }
261
+
262
+ // Show path taken
263
+ console.log(`\n${'─'.repeat(70)}`);
264
+ console.log('PATH ANALYSIS:');
265
+ console.log(`- Query type: ${test.expectedPath}`);
266
+ console.log(`- Handoff occurred: ${handoffOccurred ? 'YES' : 'NO'}`);
267
+ console.log(
268
+ `- Sequential path runs: ${handoffOccurred ? 'NO (exclusive routing)' : 'YES'}`
269
+ );
270
+ console.log(
271
+ `- Result: ${
272
+ (test.expectedPath === 'handoff' &&
273
+ handoffOccurred &&
274
+ !test.query.includes('continue')) ||
275
+ (test.expectedPath === 'sequential' && !handoffOccurred)
276
+ ? '✅ CORRECT'
277
+ : '❌ INCORRECT'
278
+ }`
279
+ );
280
+ console.log('─'.repeat(70));
281
+
282
+ // Display the responses
283
+ const aiMessages = conversationHistory.filter(
284
+ (msg) => msg._getType() === 'ai'
285
+ );
286
+ console.log('\n--- Agent Responses ---');
287
+ aiMessages.forEach((msg, index) => {
288
+ console.log(`\nResponse ${index + 1}:`);
289
+ console.log(msg.content);
290
+ });
291
+ }
292
+
293
+ // Final summary
294
+ console.log(`\n${'='.repeat(70)}`);
295
+ console.log('HYBRID WORKFLOW TEST COMPLETE');
296
+ console.log('='.repeat(70));
297
+ console.log('\nThis test demonstrates automatic exclusive routing:');
298
+ console.log('- When an agent has BOTH handoff and direct edges');
299
+ console.log('- It uses Command-based routing for exclusive execution');
300
+ console.log('- Either handoff OR direct edges execute, never both');
301
+ console.log(
302
+ '\nThis prevents duplicate processing in delegation scenarios!'
303
+ );
304
+ } catch (error) {
305
+ console.error('Error in hybrid multi-agent test:', error);
306
+ }
307
+ }
308
+
309
+ // Run the test
310
+ testHybridMultiAgent();
@@ -2,11 +2,12 @@ import { config } from 'dotenv';
2
2
  config();
3
3
 
4
4
  import { HumanMessage, BaseMessage } from '@langchain/core/messages';
5
- import { Run } from '@/run';
6
- import { Providers, GraphEvents } from '@/common';
5
+ import type * as t from '@/types';
7
6
  import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
8
7
  import { ToolEndHandler, ModelEndHandler } from '@/events';
9
- import type * as t from '@/types';
8
+ import { Providers, GraphEvents } from '@/common';
9
+ import { sleep } from '@/utils/run';
10
+ import { Run } from '@/run';
10
11
 
11
12
  const conversationHistory: BaseMessage[] = [];
12
13
 
@@ -35,7 +36,7 @@ async function testParallelMultiAgent() {
35
36
  agentId: 'researcher',
36
37
  provider: Providers.ANTHROPIC,
37
38
  clientOptions: {
38
- modelName: 'claude-3-5-sonnet-latest',
39
+ modelName: 'claude-haiku-4-5',
39
40
  apiKey: process.env.ANTHROPIC_API_KEY,
40
41
  },
41
42
  instructions: `You are a research coordinator in a multi-agent analysis workflow. Your sole job is to:
@@ -59,7 +60,7 @@ async function testParallelMultiAgent() {
59
60
  agentId: 'analyst1',
60
61
  provider: Providers.ANTHROPIC,
61
62
  clientOptions: {
62
- modelName: 'claude-3-5-sonnet-latest',
63
+ modelName: 'claude-haiku-4-5',
63
64
  apiKey: process.env.ANTHROPIC_API_KEY,
64
65
  },
65
66
  instructions: `You are a FINANCIAL AND ECONOMIC ANALYST in a parallel analysis workflow.
@@ -82,7 +83,7 @@ async function testParallelMultiAgent() {
82
83
  agentId: 'analyst2',
83
84
  provider: Providers.ANTHROPIC,
84
85
  clientOptions: {
85
- modelName: 'claude-3-5-sonnet-latest',
86
+ modelName: 'claude-haiku-4-5',
86
87
  apiKey: process.env.ANTHROPIC_API_KEY,
87
88
  },
88
89
  instructions: `You are a TECHNICAL AND IMPLEMENTATION ANALYST in a parallel analysis workflow.
@@ -106,7 +107,7 @@ async function testParallelMultiAgent() {
106
107
  agentId: 'analyst3',
107
108
  provider: Providers.ANTHROPIC,
108
109
  clientOptions: {
109
- modelName: 'claude-3-5-sonnet-latest',
110
+ modelName: 'claude-haiku-4-5',
110
111
  apiKey: process.env.ANTHROPIC_API_KEY,
111
112
  },
112
113
  instructions: `You are a MARKET AND INDUSTRY ANALYST in a parallel analysis workflow.
@@ -130,7 +131,7 @@ async function testParallelMultiAgent() {
130
131
  agentId: 'summarizer',
131
132
  provider: Providers.ANTHROPIC,
132
133
  clientOptions: {
133
- modelName: 'claude-3-5-sonnet-latest',
134
+ modelName: 'claude-haiku-4-5',
134
135
  apiKey: process.env.ANTHROPIC_API_KEY,
135
136
  },
136
137
  instructions: `You are the SYNTHESIS AND SUMMARY EXPERT in a multi-agent workflow.
@@ -332,6 +333,7 @@ IMPORTANT: Each analyst must produce substantive analysis (200+ words), not just
332
333
  console.log('\n\nActive agents during execution:', activeAgents.size);
333
334
  console.log('Final content parts:', contentParts.length, 'parts');
334
335
  console.dir(contentParts, { depth: null });
336
+ await sleep(3000);
335
337
  } catch (error) {
336
338
  console.error('Error in parallel multi-agent test:', error);
337
339
  }
@@ -30,7 +30,7 @@ async function testSequentialMultiAgent() {
30
30
  agentId: 'agent_a',
31
31
  provider: Providers.ANTHROPIC,
32
32
  clientOptions: {
33
- modelName: 'claude-3-5-sonnet-latest',
33
+ modelName: 'claude-haiku-4-5',
34
34
  apiKey: process.env.ANTHROPIC_API_KEY,
35
35
  },
36
36
  instructions: `You are Agent A, the first agent in a sequential workflow.
@@ -46,7 +46,7 @@ async function testSequentialMultiAgent() {
46
46
  agentId: 'agent_b',
47
47
  provider: Providers.ANTHROPIC,
48
48
  clientOptions: {
49
- modelName: 'claude-3-5-sonnet-latest',
49
+ modelName: 'claude-haiku-4-5',
50
50
  apiKey: process.env.ANTHROPIC_API_KEY,
51
51
  },
52
52
  instructions: `You are Agent B, the second agent in a sequential workflow.
@@ -62,7 +62,7 @@ async function testSequentialMultiAgent() {
62
62
  agentId: 'agent_c',
63
63
  provider: Providers.ANTHROPIC,
64
64
  clientOptions: {
65
- modelName: 'claude-3-5-sonnet-latest',
65
+ modelName: 'claude-haiku-4-5',
66
66
  apiKey: process.env.ANTHROPIC_API_KEY,
67
67
  },
68
68
  instructions: `You are Agent C, the final agent in a sequential workflow.
@@ -2,11 +2,12 @@ import { config } from 'dotenv';
2
2
  config();
3
3
 
4
4
  import { HumanMessage, BaseMessage } from '@langchain/core/messages';
5
- import { Run } from '@/run';
6
- import { Providers, GraphEvents } from '@/common';
5
+ import type * as t from '@/types';
7
6
  import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
8
7
  import { ToolEndHandler, ModelEndHandler } from '@/events';
9
- import type * as t from '@/types';
8
+ import { Providers, GraphEvents } from '@/common';
9
+ import { sleep } from '@/utils/run';
10
+ import { Run } from '@/run';
10
11
 
11
12
  const conversationHistory: BaseMessage[] = [];
12
13
 
@@ -353,6 +354,7 @@ async function testSupervisorMultiAgent() {
353
354
  console.log(` transfer_to_legal_advisor`);
354
355
  console.log('─'.repeat(60));
355
356
  }
357
+ await sleep(3000);
356
358
  } catch (error) {
357
359
  console.error('Error in supervisor multi-agent test:', error);
358
360
  }
@@ -22,7 +22,7 @@ async function testMultiAgentHandoff() {
22
22
  agentId: 'flight_assistant',
23
23
  provider: Providers.ANTHROPIC,
24
24
  clientOptions: {
25
- modelName: 'claude-3-5-sonnet-latest',
25
+ modelName: 'claude-haiku-4-5',
26
26
  apiKey: process.env.ANTHROPIC_API_KEY,
27
27
  },
28
28
  instructions:
@@ -33,7 +33,7 @@ async function testMultiAgentHandoff() {
33
33
  agentId: 'hotel_assistant',
34
34
  provider: Providers.ANTHROPIC,
35
35
  clientOptions: {
36
- modelName: 'claude-3-5-sonnet-latest',
36
+ modelName: 'claude-haiku-4-5',
37
37
  apiKey: process.env.ANTHROPIC_API_KEY,
38
38
  },
39
39
  instructions:
@@ -77,7 +77,11 @@ async function testStandardStreaming(): Promise<void> {
77
77
  graphConfig: {
78
78
  type: 'standard',
79
79
  llmConfig,
80
- tools: [createSearchTool()],
80
+ tools: [
81
+ createSearchTool({
82
+ scraperProvider: 'serper',
83
+ }),
84
+ ],
81
85
  instructions:
82
86
  'You are a friendly AI assistant. Always address the user by their name.',
83
87
  // additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
@@ -18,6 +18,7 @@ import {
18
18
  import { GraphEvents, Providers, TitleMethod } from '@/common';
19
19
  import { getLLMConfig } from '@/utils/llmConfig';
20
20
  import { getArgs } from '@/scripts/args';
21
+ import { sleep } from '@/utils/run';
21
22
  import { Run } from '@/run';
22
23
 
23
24
  const conversationHistory: BaseMessage[] = [];
@@ -145,7 +146,9 @@ async function testStandardStreaming(): Promise<void> {
145
146
  });
146
147
 
147
148
  const config = {
149
+ runId: 'test-simple-script',
148
150
  configurable: {
151
+ user_id: 'user-123',
149
152
  thread_id: 'conversation-num-1',
150
153
  },
151
154
  streamMode: 'values',
@@ -177,6 +180,10 @@ async function testStandardStreaming(): Promise<void> {
177
180
  contentParts,
178
181
  // titleMethod: TitleMethod.STRUCTURED,
179
182
  chainOptions: {
183
+ configurable: {
184
+ user_id: 'user-123',
185
+ thread_id: 'conversation-num-1',
186
+ },
180
187
  callbacks: [
181
188
  {
182
189
  handleLLMEnd,
@@ -193,6 +200,7 @@ async function testStandardStreaming(): Promise<void> {
193
200
  console.log('Collected usage metadata:', collectedUsage);
194
201
  console.log('Generated Title:', titleResult);
195
202
  console.log('Collected title usage metadata:', collected);
203
+ await sleep(5000);
196
204
  }
197
205
 
198
206
  process.on('unhandledRejection', (reason, promise) => {
@@ -24,7 +24,7 @@ async function testCustomPromptKey() {
24
24
  agentId: 'supervisor',
25
25
  provider: Providers.ANTHROPIC,
26
26
  clientOptions: {
27
- modelName: 'claude-3-5-sonnet-latest',
27
+ modelName: 'claude-haiku-4-5',
28
28
  apiKey: process.env.ANTHROPIC_API_KEY,
29
29
  },
30
30
  instructions: `You are a Task Supervisor managing different agents:
@@ -41,7 +41,7 @@ async function testCustomPromptKey() {
41
41
  agentId: 'researcher',
42
42
  provider: Providers.ANTHROPIC,
43
43
  clientOptions: {
44
- modelName: 'claude-3-5-sonnet-latest',
44
+ modelName: 'claude-haiku-4-5',
45
45
  apiKey: process.env.ANTHROPIC_API_KEY,
46
46
  },
47
47
  instructions: `You are a Research Agent. You receive research queries to investigate.
@@ -52,7 +52,7 @@ async function testCustomPromptKey() {
52
52
  agentId: 'designer',
53
53
  provider: Providers.ANTHROPIC,
54
54
  clientOptions: {
55
- modelName: 'claude-3-5-sonnet-latest',
55
+ modelName: 'claude-haiku-4-5',
56
56
  apiKey: process.env.ANTHROPIC_API_KEY,
57
57
  },
58
58
  instructions: `You are a Design Agent. You receive design requirements to implement.
@@ -63,7 +63,7 @@ async function testCustomPromptKey() {
63
63
  agentId: 'coder',
64
64
  provider: Providers.ANTHROPIC,
65
65
  clientOptions: {
66
- modelName: 'claude-3-5-sonnet-latest',
66
+ modelName: 'claude-haiku-4-5',
67
67
  apiKey: process.env.ANTHROPIC_API_KEY,
68
68
  },
69
69
  instructions: `You are a Coding Agent. You receive technical specifications to implement.
@@ -84,7 +84,7 @@ async function testHandoffInput() {
84
84
  agentId: 'supervisor',
85
85
  provider: Providers.ANTHROPIC,
86
86
  clientOptions: {
87
- modelName: 'claude-3-5-sonnet-latest',
87
+ modelName: 'claude-haiku-4-5',
88
88
  apiKey: process.env.ANTHROPIC_API_KEY,
89
89
  },
90
90
  instructions: `You are a Task Supervisor. You have access to two specialist agents:
@@ -100,7 +100,7 @@ async function testHandoffInput() {
100
100
  agentId: 'analyst',
101
101
  provider: Providers.ANTHROPIC,
102
102
  clientOptions: {
103
- modelName: 'claude-3-5-sonnet-latest',
103
+ modelName: 'claude-haiku-4-5',
104
104
  apiKey: process.env.ANTHROPIC_API_KEY,
105
105
  },
106
106
  instructions: `You are a Data Analyst. Follow the supervisor's instructions carefully.
@@ -111,7 +111,7 @@ async function testHandoffInput() {
111
111
  agentId: 'writer',
112
112
  provider: Providers.ANTHROPIC,
113
113
  clientOptions: {
114
- modelName: 'claude-3-5-sonnet-latest',
114
+ modelName: 'claude-haiku-4-5',
115
115
  apiKey: process.env.ANTHROPIC_API_KEY,
116
116
  },
117
117
  instructions: `You are a Content Writer. Follow the supervisor's instructions carefully.
@@ -93,7 +93,7 @@ async function testSupervisorListHandoff() {
93
93
  const specialistConfig = {
94
94
  provider: Providers.ANTHROPIC,
95
95
  clientOptions: {
96
- modelName: 'claude-3-5-sonnet-latest',
96
+ modelName: 'claude-haiku-4-5',
97
97
  apiKey: process.env.ANTHROPIC_API_KEY,
98
98
  },
99
99
  instructions: `You are an Adaptive Specialist. Your agent ID indicates your role:
@@ -114,7 +114,7 @@ async function testSupervisorListHandoff() {
114
114
  agentId: 'supervisor',
115
115
  provider: Providers.ANTHROPIC,
116
116
  clientOptions: {
117
- modelName: 'claude-3-5-sonnet-latest',
117
+ modelName: 'claude-haiku-4-5',
118
118
  apiKey: process.env.ANTHROPIC_API_KEY,
119
119
  },
120
120
  instructions: `You are a Task Supervisor with access to 5 specialist agents:
@@ -126,7 +126,10 @@ async function testStandardStreaming(): Promise<void> {
126
126
  const inputs = {
127
127
  messages: conversationHistory,
128
128
  };
129
- const finalContentParts = await run.processStream(inputs, config);
129
+ const finalContentParts = await run.processStream(inputs, config, {
130
+ indexTokenCountMap: { 0: 35 },
131
+ maxContextTokens: 89000,
132
+ });
130
133
  const finalMessages = run.getRunMessages();
131
134
  if (finalMessages) {
132
135
  conversationHistory.push(...finalMessages);