illuma-agents 1.0.2 → 1.0.4

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 (225) hide show
  1. package/LICENSE +25 -21
  2. package/dist/cjs/agents/AgentContext.cjs +222 -0
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -0
  4. package/dist/cjs/common/enum.cjs +5 -4
  5. package/dist/cjs/common/enum.cjs.map +1 -1
  6. package/dist/cjs/events.cjs +7 -5
  7. package/dist/cjs/events.cjs.map +1 -1
  8. package/dist/cjs/graphs/Graph.cjs +328 -207
  9. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  10. package/dist/cjs/graphs/MultiAgentGraph.cjs +507 -0
  11. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
  12. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  13. package/dist/cjs/llm/google/index.cjs.map +1 -1
  14. package/dist/cjs/llm/ollama/index.cjs.map +1 -1
  15. package/dist/cjs/llm/openai/index.cjs +35 -0
  16. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  17. package/dist/cjs/llm/openai/utils/index.cjs +3 -1
  18. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  19. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  20. package/dist/cjs/llm/providers.cjs +0 -2
  21. package/dist/cjs/llm/providers.cjs.map +1 -1
  22. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  23. package/dist/cjs/main.cjs +12 -1
  24. package/dist/cjs/main.cjs.map +1 -1
  25. package/dist/cjs/messages/cache.cjs +123 -0
  26. package/dist/cjs/messages/cache.cjs.map +1 -0
  27. package/dist/cjs/messages/content.cjs +53 -0
  28. package/dist/cjs/messages/content.cjs.map +1 -0
  29. package/dist/cjs/messages/format.cjs +17 -29
  30. package/dist/cjs/messages/format.cjs.map +1 -1
  31. package/dist/cjs/run.cjs +119 -74
  32. package/dist/cjs/run.cjs.map +1 -1
  33. package/dist/cjs/stream.cjs +77 -73
  34. package/dist/cjs/stream.cjs.map +1 -1
  35. package/dist/cjs/tools/Calculator.cjs +45 -0
  36. package/dist/cjs/tools/Calculator.cjs.map +1 -0
  37. package/dist/cjs/tools/CodeExecutor.cjs +22 -22
  38. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  39. package/dist/cjs/tools/ToolNode.cjs +5 -3
  40. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  41. package/dist/cjs/tools/handlers.cjs +20 -20
  42. package/dist/cjs/tools/handlers.cjs.map +1 -1
  43. package/dist/cjs/utils/events.cjs +31 -0
  44. package/dist/cjs/utils/events.cjs.map +1 -0
  45. package/dist/cjs/utils/handlers.cjs +70 -0
  46. package/dist/cjs/utils/handlers.cjs.map +1 -0
  47. package/dist/cjs/utils/tokens.cjs +54 -7
  48. package/dist/cjs/utils/tokens.cjs.map +1 -1
  49. package/dist/esm/agents/AgentContext.mjs +220 -0
  50. package/dist/esm/agents/AgentContext.mjs.map +1 -0
  51. package/dist/esm/common/enum.mjs +5 -4
  52. package/dist/esm/common/enum.mjs.map +1 -1
  53. package/dist/esm/events.mjs +7 -5
  54. package/dist/esm/events.mjs.map +1 -1
  55. package/dist/esm/graphs/Graph.mjs +330 -209
  56. package/dist/esm/graphs/Graph.mjs.map +1 -1
  57. package/dist/esm/graphs/MultiAgentGraph.mjs +505 -0
  58. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
  59. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  60. package/dist/esm/llm/google/index.mjs.map +1 -1
  61. package/dist/esm/llm/ollama/index.mjs.map +1 -1
  62. package/dist/esm/llm/openai/index.mjs +35 -0
  63. package/dist/esm/llm/openai/index.mjs.map +1 -1
  64. package/dist/esm/llm/openai/utils/index.mjs +3 -1
  65. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  66. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  67. package/dist/esm/llm/providers.mjs +0 -2
  68. package/dist/esm/llm/providers.mjs.map +1 -1
  69. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  70. package/dist/esm/main.mjs +7 -2
  71. package/dist/esm/main.mjs.map +1 -1
  72. package/dist/esm/messages/cache.mjs +120 -0
  73. package/dist/esm/messages/cache.mjs.map +1 -0
  74. package/dist/esm/messages/content.mjs +51 -0
  75. package/dist/esm/messages/content.mjs.map +1 -0
  76. package/dist/esm/messages/format.mjs +18 -29
  77. package/dist/esm/messages/format.mjs.map +1 -1
  78. package/dist/esm/run.mjs +119 -74
  79. package/dist/esm/run.mjs.map +1 -1
  80. package/dist/esm/stream.mjs +77 -73
  81. package/dist/esm/stream.mjs.map +1 -1
  82. package/dist/esm/tools/Calculator.mjs +24 -0
  83. package/dist/esm/tools/Calculator.mjs.map +1 -0
  84. package/dist/esm/tools/CodeExecutor.mjs +22 -22
  85. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  86. package/dist/esm/tools/ToolNode.mjs +5 -3
  87. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  88. package/dist/esm/tools/handlers.mjs +20 -20
  89. package/dist/esm/tools/handlers.mjs.map +1 -1
  90. package/dist/esm/utils/events.mjs +29 -0
  91. package/dist/esm/utils/events.mjs.map +1 -0
  92. package/dist/esm/utils/handlers.mjs +68 -0
  93. package/dist/esm/utils/handlers.mjs.map +1 -0
  94. package/dist/esm/utils/tokens.mjs +54 -8
  95. package/dist/esm/utils/tokens.mjs.map +1 -1
  96. package/dist/types/agents/AgentContext.d.ts +94 -0
  97. package/dist/types/common/enum.d.ts +7 -5
  98. package/dist/types/events.d.ts +3 -3
  99. package/dist/types/graphs/Graph.d.ts +60 -66
  100. package/dist/types/graphs/MultiAgentGraph.d.ts +47 -0
  101. package/dist/types/graphs/index.d.ts +1 -0
  102. package/dist/types/index.d.ts +1 -0
  103. package/dist/types/llm/openai/index.d.ts +10 -0
  104. package/dist/types/messages/cache.d.ts +20 -0
  105. package/dist/types/messages/content.d.ts +7 -0
  106. package/dist/types/messages/format.d.ts +1 -7
  107. package/dist/types/messages/index.d.ts +2 -0
  108. package/dist/types/messages/reducer.d.ts +9 -0
  109. package/dist/types/run.d.ts +16 -10
  110. package/dist/types/stream.d.ts +4 -3
  111. package/dist/types/tools/Calculator.d.ts +8 -0
  112. package/dist/types/tools/ToolNode.d.ts +1 -1
  113. package/dist/types/tools/handlers.d.ts +9 -7
  114. package/dist/types/tools/search/tool.d.ts +4 -4
  115. package/dist/types/types/graph.d.ts +124 -11
  116. package/dist/types/types/llm.d.ts +13 -9
  117. package/dist/types/types/messages.d.ts +4 -0
  118. package/dist/types/types/run.d.ts +46 -8
  119. package/dist/types/types/stream.d.ts +3 -2
  120. package/dist/types/utils/events.d.ts +6 -0
  121. package/dist/types/utils/handlers.d.ts +34 -0
  122. package/dist/types/utils/index.d.ts +1 -0
  123. package/dist/types/utils/tokens.d.ts +24 -0
  124. package/package.json +162 -145
  125. package/src/agents/AgentContext.ts +323 -0
  126. package/src/common/enum.ts +177 -176
  127. package/src/events.ts +197 -191
  128. package/src/graphs/Graph.ts +1058 -846
  129. package/src/graphs/MultiAgentGraph.ts +598 -0
  130. package/src/graphs/index.ts +2 -1
  131. package/src/index.ts +25 -24
  132. package/src/llm/anthropic/index.ts +413 -413
  133. package/src/llm/google/index.ts +222 -222
  134. package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -88
  135. package/src/llm/ollama/index.ts +92 -92
  136. package/src/llm/openai/index.ts +894 -853
  137. package/src/llm/openai/utils/index.ts +920 -918
  138. package/src/llm/openrouter/index.ts +60 -60
  139. package/src/llm/providers.ts +55 -57
  140. package/src/llm/vertexai/index.ts +360 -360
  141. package/src/messages/cache.test.ts +461 -0
  142. package/src/messages/cache.ts +151 -0
  143. package/src/messages/content.test.ts +362 -0
  144. package/src/messages/content.ts +63 -0
  145. package/src/messages/format.ts +611 -625
  146. package/src/messages/formatAgentMessages.test.ts +1144 -917
  147. package/src/messages/index.ts +6 -4
  148. package/src/messages/reducer.ts +80 -0
  149. package/src/run.ts +447 -381
  150. package/src/scripts/abort.ts +157 -138
  151. package/src/scripts/ant_web_search.ts +158 -158
  152. package/src/scripts/cli.ts +172 -167
  153. package/src/scripts/cli2.ts +133 -125
  154. package/src/scripts/cli3.ts +184 -178
  155. package/src/scripts/cli4.ts +191 -184
  156. package/src/scripts/cli5.ts +191 -184
  157. package/src/scripts/code_exec.ts +213 -214
  158. package/src/scripts/code_exec_simple.ts +147 -129
  159. package/src/scripts/content.ts +138 -120
  160. package/src/scripts/handoff-test.ts +135 -0
  161. package/src/scripts/multi-agent-chain.ts +278 -0
  162. package/src/scripts/multi-agent-conditional.ts +220 -0
  163. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  164. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  165. package/src/scripts/multi-agent-parallel.ts +343 -0
  166. package/src/scripts/multi-agent-sequence.ts +212 -0
  167. package/src/scripts/multi-agent-supervisor.ts +364 -0
  168. package/src/scripts/multi-agent-test.ts +186 -0
  169. package/src/scripts/search.ts +146 -150
  170. package/src/scripts/simple.ts +225 -225
  171. package/src/scripts/stream.ts +140 -122
  172. package/src/scripts/test-custom-prompt-key.ts +145 -0
  173. package/src/scripts/test-handoff-input.ts +170 -0
  174. package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
  175. package/src/scripts/test-tools-before-handoff.ts +222 -0
  176. package/src/scripts/tools.ts +153 -155
  177. package/src/specs/agent-handoffs.test.ts +889 -0
  178. package/src/specs/anthropic.simple.test.ts +320 -317
  179. package/src/specs/azure.simple.test.ts +325 -316
  180. package/src/specs/openai.simple.test.ts +311 -316
  181. package/src/specs/openrouter.simple.test.ts +107 -0
  182. package/src/specs/prune.test.ts +758 -763
  183. package/src/specs/reasoning.test.ts +201 -165
  184. package/src/specs/thinking-prune.test.ts +769 -703
  185. package/src/specs/token-memoization.test.ts +39 -0
  186. package/src/stream.ts +664 -651
  187. package/src/tools/Calculator.test.ts +278 -0
  188. package/src/tools/Calculator.ts +25 -0
  189. package/src/tools/CodeExecutor.ts +220 -220
  190. package/src/tools/ToolNode.ts +170 -170
  191. package/src/tools/handlers.ts +341 -336
  192. package/src/types/graph.ts +372 -185
  193. package/src/types/llm.ts +141 -140
  194. package/src/types/messages.ts +4 -0
  195. package/src/types/run.ts +128 -89
  196. package/src/types/stream.ts +401 -400
  197. package/src/utils/events.ts +32 -0
  198. package/src/utils/handlers.ts +107 -0
  199. package/src/utils/index.ts +6 -5
  200. package/src/utils/llmConfig.ts +183 -183
  201. package/src/utils/tokens.ts +129 -70
  202. package/dist/types/scripts/abort.d.ts +0 -1
  203. package/dist/types/scripts/ant_web_search.d.ts +0 -1
  204. package/dist/types/scripts/args.d.ts +0 -7
  205. package/dist/types/scripts/caching.d.ts +0 -1
  206. package/dist/types/scripts/cli.d.ts +0 -1
  207. package/dist/types/scripts/cli2.d.ts +0 -1
  208. package/dist/types/scripts/cli3.d.ts +0 -1
  209. package/dist/types/scripts/cli4.d.ts +0 -1
  210. package/dist/types/scripts/cli5.d.ts +0 -1
  211. package/dist/types/scripts/code_exec.d.ts +0 -1
  212. package/dist/types/scripts/code_exec_files.d.ts +0 -1
  213. package/dist/types/scripts/code_exec_simple.d.ts +0 -1
  214. package/dist/types/scripts/content.d.ts +0 -1
  215. package/dist/types/scripts/empty_input.d.ts +0 -1
  216. package/dist/types/scripts/image.d.ts +0 -1
  217. package/dist/types/scripts/memory.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/thinking.d.ts +0 -1
  222. package/dist/types/scripts/tools.d.ts +0 -1
  223. package/dist/types/specs/spec.utils.d.ts +0 -1
  224. package/dist/types/tools/example.d.ts +0 -78
  225. package/src/tools/example.ts +0 -129
@@ -0,0 +1,261 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { config } from 'dotenv';
4
+ config();
5
+
6
+ import { HumanMessage, BaseMessage } from '@langchain/core/messages';
7
+ import { Run } from '@/run';
8
+ import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
9
+ import { Providers, GraphEvents, Constants } from '@/common';
10
+ import { ToolEndHandler, ModelEndHandler } from '@/events';
11
+ import type * as t from '@/types';
12
+
13
+ const conversationHistory: BaseMessage[] = [];
14
+
15
+ /**
16
+ * Test supervisor-based multi-agent system using a single edge with multiple destinations
17
+ *
18
+ * Instead of creating 5 separate edges, we use one edge with an array of destinations
19
+ * This should create handoff tools for all 5 specialists from a single edge definition
20
+ */
21
+ async function testSupervisorListHandoff() {
22
+ console.log('Testing Supervisor with List-Based Handoff Edge...\n');
23
+
24
+ // Set up content aggregator
25
+ const { contentParts, aggregateContent } = createContentAggregator();
26
+
27
+ // Track which specialist role was selected
28
+ let selectedRole = '';
29
+
30
+ // Create custom handlers
31
+ const customHandlers = {
32
+ [GraphEvents.TOOL_END]: new ToolEndHandler(),
33
+ [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
34
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
35
+ [GraphEvents.ON_RUN_STEP]: {
36
+ handle: (
37
+ event: GraphEvents.ON_RUN_STEP,
38
+ data: t.StreamEventData
39
+ ): void => {
40
+ const runStepData = data as any;
41
+ if (runStepData?.name) {
42
+ console.log(`\n[${runStepData.name}] Processing...`);
43
+ }
44
+ aggregateContent({ event, data: data as t.RunStep });
45
+ },
46
+ },
47
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
48
+ handle: (
49
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
50
+ data: t.StreamEventData
51
+ ): void => {
52
+ aggregateContent({
53
+ event,
54
+ data: data as unknown as { result: t.ToolEndEvent },
55
+ });
56
+ },
57
+ },
58
+ [GraphEvents.ON_MESSAGE_DELTA]: {
59
+ handle: (
60
+ event: GraphEvents.ON_MESSAGE_DELTA,
61
+ data: t.StreamEventData
62
+ ): void => {
63
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
64
+ },
65
+ },
66
+ [GraphEvents.TOOL_START]: {
67
+ handle: (
68
+ _event: string,
69
+ data: t.StreamEventData,
70
+ metadata?: Record<string, unknown>
71
+ ): void => {
72
+ const toolData = data as any;
73
+ if (toolData?.name?.startsWith(Constants.LC_TRANSFER_TO_)) {
74
+ const specialist = toolData.name.replace(
75
+ Constants.LC_TRANSFER_TO_,
76
+ ''
77
+ );
78
+ console.log(`\nšŸ”€ Transferring to ${specialist}...`);
79
+ selectedRole = specialist;
80
+ }
81
+ },
82
+ },
83
+ };
84
+
85
+ // Function to create the graph with a single edge to multiple specialists
86
+ function createSupervisorGraphWithListEdge(): t.RunConfig {
87
+ console.log(`\nCreating graph with supervisor and 5 specialist agents.`);
88
+ console.log(
89
+ 'Using a SINGLE edge with multiple destinations (list-based handoff).\n'
90
+ );
91
+
92
+ // Define the adaptive specialist configuration that will be reused
93
+ const specialistConfig = {
94
+ provider: Providers.ANTHROPIC,
95
+ clientOptions: {
96
+ modelName: 'claude-haiku-4-5',
97
+ apiKey: process.env.ANTHROPIC_API_KEY,
98
+ },
99
+ instructions: `You are an Adaptive Specialist. Your agent ID indicates your role:
100
+
101
+ - data_analyst: Focus on statistical analysis, metrics, ML evaluation, A/B testing
102
+ - security_expert: Focus on cybersecurity, vulnerability assessment, compliance
103
+ - product_designer: Focus on UX/UI design, user research, accessibility
104
+ - devops_engineer: Focus on CI/CD, infrastructure, cloud platforms, monitoring
105
+ - legal_advisor: Focus on licensing, privacy laws, contracts, regulatory compliance
106
+
107
+ The supervisor will provide specific instructions. Follow them while maintaining your expert perspective.`,
108
+ maxContextTokens: 8000,
109
+ };
110
+
111
+ // Create the graph with supervisor and all 5 specialists
112
+ const agents: t.AgentInputs[] = [
113
+ {
114
+ agentId: 'supervisor',
115
+ provider: Providers.ANTHROPIC,
116
+ clientOptions: {
117
+ modelName: 'claude-haiku-4-5',
118
+ apiKey: process.env.ANTHROPIC_API_KEY,
119
+ },
120
+ instructions: `You are a Task Supervisor with access to 5 specialist agents:
121
+ 1. transfer_to_data_analyst - For statistical analysis and metrics
122
+ 2. transfer_to_security_expert - For cybersecurity and vulnerability assessment
123
+ 3. transfer_to_product_designer - For UX/UI design
124
+ 4. transfer_to_devops_engineer - For infrastructure and deployment
125
+ 5. transfer_to_legal_advisor - For compliance and licensing
126
+
127
+ Your role is to:
128
+ 1. Analyze the incoming request
129
+ 2. Decide which specialist is best suited
130
+ 3. Use the appropriate transfer tool (e.g., transfer_to_data_analyst)
131
+ 4. Provide specific instructions to guide their work
132
+
133
+ Be specific about what you need from the specialist.`,
134
+ maxContextTokens: 8000,
135
+ },
136
+ // Include all 5 specialists with the same adaptive configuration
137
+ {
138
+ agentId: 'data_analyst',
139
+ ...specialistConfig,
140
+ },
141
+ {
142
+ agentId: 'security_expert',
143
+ ...specialistConfig,
144
+ },
145
+ {
146
+ agentId: 'product_designer',
147
+ ...specialistConfig,
148
+ },
149
+ {
150
+ agentId: 'devops_engineer',
151
+ ...specialistConfig,
152
+ },
153
+ {
154
+ agentId: 'legal_advisor',
155
+ ...specialistConfig,
156
+ },
157
+ ];
158
+
159
+ // Create a SINGLE edge from supervisor to ALL 5 specialists using a list
160
+ const edges: t.GraphEdge[] = [
161
+ {
162
+ from: 'supervisor',
163
+ to: [
164
+ 'data_analyst',
165
+ 'security_expert',
166
+ 'product_designer',
167
+ 'devops_engineer',
168
+ 'legal_advisor',
169
+ ],
170
+ description:
171
+ 'Transfer to appropriate specialist based on task requirements',
172
+ edgeType: 'handoff',
173
+ },
174
+ ];
175
+
176
+ return {
177
+ runId: `supervisor-list-handoff-${Date.now()}`,
178
+ graphConfig: {
179
+ type: 'multi-agent',
180
+ agents,
181
+ edges,
182
+ },
183
+ customHandlers,
184
+ returnContent: true,
185
+ };
186
+ }
187
+
188
+ try {
189
+ // Test with different queries
190
+ const testQueries = [
191
+ // 'How can we analyze user engagement metrics to improve our product?',
192
+ // 'What security measures should we implement for our new API?',
193
+ // 'Can you help design a better onboarding flow for our mobile app?',
194
+ // 'We need to set up a CI/CD pipeline for our microservices.',
195
+ 'What are the legal implications of using GPL-licensed code in our product?',
196
+ ];
197
+
198
+ const config = {
199
+ configurable: {
200
+ thread_id: 'supervisor-list-handoff-1',
201
+ },
202
+ streamMode: 'values',
203
+ version: 'v2' as const,
204
+ };
205
+
206
+ for (const query of testQueries) {
207
+ console.log(`\n${'='.repeat(60)}`);
208
+ console.log(`USER QUERY: "${query}"`);
209
+ console.log('='.repeat(60));
210
+
211
+ // Reset conversation
212
+ conversationHistory.length = 0;
213
+ conversationHistory.push(new HumanMessage(query));
214
+
215
+ // Create graph with supervisor having a single edge to multiple specialists
216
+ const runConfig = createSupervisorGraphWithListEdge();
217
+ const run = await Run.create(runConfig);
218
+
219
+ console.log('Processing request...');
220
+
221
+ // Process with streaming
222
+ const inputs = {
223
+ messages: conversationHistory,
224
+ };
225
+
226
+ const finalContentParts = await run.processStream(inputs, config);
227
+ const finalMessages = run.getRunMessages();
228
+
229
+ if (finalMessages) {
230
+ conversationHistory.push(...finalMessages);
231
+ }
232
+
233
+ // Show summary
234
+ console.log(`\n${'─'.repeat(60)}`);
235
+ console.log(`Graph structure:`);
236
+ console.log(`- Agents: 6 total (supervisor + 5 specialists)`);
237
+ console.log(`- Edges: 1 edge with multiple destinations`);
238
+ console.log(
239
+ `- Edge type: handoff (creates individual tools for each destination)`
240
+ );
241
+ console.log(
242
+ `- Result: Supervisor has 5 handoff tools from a single edge`
243
+ );
244
+ console.log('─'.repeat(60));
245
+ }
246
+
247
+ // Final summary
248
+ console.log(`\n${'='.repeat(60)}`);
249
+ console.log('TEST COMPLETE');
250
+ console.log('='.repeat(60));
251
+ console.log('\nThis test demonstrates that a single edge with multiple');
252
+ console.log('destinations in the "to" field creates individual handoff');
253
+ console.log('tools for each destination agent, achieving the same result');
254
+ console.log('as creating separate edges for each specialist.');
255
+ } catch (error) {
256
+ console.error('Error in supervisor list handoff test:', error);
257
+ }
258
+ }
259
+
260
+ // Run the test
261
+ testSupervisorListHandoff();
@@ -0,0 +1,222 @@
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 } 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
+ * Test edge case: Agent performs 2 web searches before handing off
15
+ *
16
+ * This tests how the system behaves when an agent with handoff capabilities
17
+ * uses tools before transferring control to another agent.
18
+ */
19
+ async function testToolsBeforeHandoff() {
20
+ console.log('Testing Tools Before Handoff Edge Case...\n');
21
+
22
+ // Set up content aggregator
23
+ const { contentParts, aggregateContent } = createContentAggregator();
24
+
25
+ // Track tool calls and handoffs
26
+ let toolCallCount = 0;
27
+ let handoffOccurred = false;
28
+
29
+ // Create custom handlers
30
+ const customHandlers = {
31
+ [GraphEvents.TOOL_END]: new ToolEndHandler(undefined, (name?: string) => {
32
+ console.log(`\nāœ… Tool completed: ${name}`);
33
+ return true;
34
+ }),
35
+ [GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
36
+ [GraphEvents.CHAT_MODEL_STREAM]: new ChatModelStreamHandler(),
37
+ [GraphEvents.ON_RUN_STEP]: {
38
+ handle: (
39
+ event: GraphEvents.ON_RUN_STEP,
40
+ data: t.StreamEventData
41
+ ): void => {
42
+ const runStepData = data as any;
43
+ if (runStepData?.name) {
44
+ console.log(`\n[${runStepData.name}] Processing...`);
45
+ }
46
+ aggregateContent({ event, data: data as t.RunStep });
47
+ },
48
+ },
49
+ [GraphEvents.ON_RUN_STEP_COMPLETED]: {
50
+ handle: (
51
+ event: GraphEvents.ON_RUN_STEP_COMPLETED,
52
+ data: t.StreamEventData
53
+ ): void => {
54
+ aggregateContent({
55
+ event,
56
+ data: data as unknown as { result: t.ToolEndEvent },
57
+ });
58
+ },
59
+ },
60
+ [GraphEvents.ON_MESSAGE_DELTA]: {
61
+ handle: (
62
+ event: GraphEvents.ON_MESSAGE_DELTA,
63
+ data: t.StreamEventData
64
+ ): void => {
65
+ // console.log('====== ON_MESSAGE_DELTA ======');
66
+ console.dir(data, { depth: null });
67
+ aggregateContent({ event, data: data as t.MessageDeltaEvent });
68
+ },
69
+ },
70
+ [GraphEvents.TOOL_START]: {
71
+ handle: (
72
+ _event: string,
73
+ data: t.StreamEventData,
74
+ metadata?: Record<string, unknown>
75
+ ): void => {
76
+ const toolData = data as any;
77
+ console.log(`\nšŸ”§ Tool started:`);
78
+ console.dir({ toolData, metadata }, { depth: null });
79
+
80
+ if (toolData?.output?.name?.includes('transfer_to_')) {
81
+ handoffOccurred = true;
82
+ const specialist = toolData.name.replace('transfer_to_', '');
83
+ console.log(`\nšŸ”€ Handoff initiated to: ${specialist}`);
84
+ }
85
+ },
86
+ },
87
+ };
88
+
89
+ // Create the graph with research agent and report writer
90
+ function createGraphWithToolsAndHandoff(): t.RunConfig {
91
+ const agents: t.AgentInputs[] = [
92
+ {
93
+ agentId: 'research_coordinator',
94
+ provider: Providers.OPENAI,
95
+ clientOptions: {
96
+ modelName: 'gpt-4.1-mini',
97
+ apiKey: process.env.OPENAI_API_KEY,
98
+ },
99
+ tools: [],
100
+ instructions: `You are a Research Coordinator with access to a report writer specialist.
101
+
102
+ Your workflow MUST follow these steps IN ORDER:
103
+ 1. FIRST: Write an initial response acknowledging the request
104
+ - Explain what you understand about the topic
105
+ - Provide any general knowledge you have
106
+ 2. FINALLY: Transfer to the report writer
107
+ - Provide the report writer with a summary of the information
108
+
109
+ CRITICAL: You MUST write your initial response before transferring to the report writer.`,
110
+ maxContextTokens: 8000,
111
+ },
112
+ {
113
+ agentId: 'report_writer',
114
+ provider: Providers.OPENAI,
115
+ clientOptions: {
116
+ modelName: 'gpt-5-mini',
117
+ apiKey: process.env.OPENAI_API_KEY,
118
+ },
119
+ instructions: `You are a Report Writer specialist. Your role is to:
120
+ 1. Receive research findings from the Research Coordinator
121
+ 2. Create a well-structured, comprehensive report
122
+ 3. Include all key findings from the research
123
+ 4. Format the report with clear sections and bullet points
124
+ 5. Add a brief executive summary at the beginning
125
+
126
+ Focus on clarity, completeness, and professional presentation.`,
127
+ maxContextTokens: 8000,
128
+ },
129
+ ];
130
+
131
+ // Create edge from research coordinator to report writer
132
+ const edges: t.GraphEdge[] = [
133
+ {
134
+ from: 'research_coordinator',
135
+ to: 'report_writer',
136
+ description: 'Transfer to report writer after completing research',
137
+ edgeType: 'handoff',
138
+ },
139
+ ];
140
+
141
+ return {
142
+ runId: `tools-before-handoff-${Date.now()}`,
143
+ graphConfig: {
144
+ type: 'multi-agent',
145
+ agents,
146
+ edges,
147
+ },
148
+ customHandlers,
149
+ returnContent: true,
150
+ };
151
+ }
152
+
153
+ try {
154
+ // Single test query that requires handoff to report writer
155
+ const query = `Tell me about quantum computing developments,
156
+ including major breakthroughs and commercial applications.
157
+ I need a comprehensive report.`;
158
+
159
+ console.log('='.repeat(60));
160
+ console.log(`USER QUERY: "${query}"`);
161
+ console.log('='.repeat(60));
162
+
163
+ // Create the graph
164
+ const runConfig = createGraphWithToolsAndHandoff();
165
+ const run = await Run.create(runConfig);
166
+
167
+ console.log('\nExpected behavior:');
168
+ console.log('1. Research Coordinator writes initial response');
169
+ console.log('2. Research Coordinator hands off to Report Writer');
170
+ console.log('3. Report Writer creates final report\n');
171
+
172
+ // Process with streaming
173
+ conversationHistory.push(new HumanMessage(query));
174
+ const inputs = {
175
+ messages: conversationHistory,
176
+ };
177
+
178
+ const config = {
179
+ configurable: {
180
+ thread_id: 'tools-handoff-test-1',
181
+ },
182
+ streamMode: 'values',
183
+ version: 'v2' as const,
184
+ };
185
+
186
+ const finalContentParts = await run.processStream(inputs, config);
187
+ const finalMessages = run.getRunMessages();
188
+
189
+ if (finalMessages) {
190
+ conversationHistory.push(...finalMessages);
191
+ }
192
+
193
+ // Show results summary
194
+ console.log(`\n${'─'.repeat(60)}`);
195
+ console.log('EDGE CASE TEST RESULTS:');
196
+ console.log('─'.repeat(60));
197
+ console.log(`Tool calls before handoff: ${toolCallCount}`);
198
+ console.log(`Expected tool calls: 0 (no web search available)`);
199
+ console.log(`Handoff occurred: ${handoffOccurred ? 'Yes āœ…' : 'No āŒ'}`);
200
+ console.log(`Test status: ${handoffOccurred ? 'PASSED āœ…' : 'FAILED āŒ'}`);
201
+ console.log('─'.repeat(60));
202
+
203
+ // Display conversation history
204
+ console.log('\nConversation History:');
205
+ console.log('─'.repeat(60));
206
+ conversationHistory.forEach((msg, idx) => {
207
+ const role = msg.constructor.name.replace('Message', '');
208
+ console.log(`\n[${idx}] ${role}:`);
209
+ if (typeof msg.content === 'string') {
210
+ console.log(
211
+ msg.content.substring(0, 200) +
212
+ (msg.content.length > 200 ? '...' : '')
213
+ );
214
+ }
215
+ });
216
+ } catch (error) {
217
+ console.error('Error in tools-before-handoff test:', error);
218
+ }
219
+ }
220
+
221
+ // Run the test
222
+ testToolsBeforeHandoff();