@illuma-ai/agents 1.1.20 → 1.1.22

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 (246) hide show
  1. package/dist/cjs/graphs/Graph.cjs +12 -1
  2. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  3. package/dist/cjs/graphs/MultiAgentGraph.cjs +85 -1
  4. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
  5. package/dist/cjs/llm/bedrock/index.cjs +14 -0
  6. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  7. package/dist/cjs/run.cjs +20 -9
  8. package/dist/cjs/run.cjs.map +1 -1
  9. package/dist/esm/graphs/Graph.mjs +12 -1
  10. package/dist/esm/graphs/Graph.mjs.map +1 -1
  11. package/dist/esm/graphs/MultiAgentGraph.mjs +85 -1
  12. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
  13. package/dist/esm/llm/bedrock/index.mjs +14 -0
  14. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  15. package/dist/esm/run.mjs +20 -9
  16. package/dist/esm/run.mjs.map +1 -1
  17. package/dist/types/graphs/MultiAgentGraph.d.ts +17 -0
  18. package/package.json +1 -1
  19. package/src/graphs/Graph.ts +12 -1
  20. package/src/graphs/MultiAgentGraph.ts +105 -1
  21. package/src/graphs/__tests__/multi-agent-delegate.test.ts +191 -0
  22. package/src/llm/bedrock/index.ts +17 -0
  23. package/src/run.ts +20 -11
  24. package/src/scripts/test-bedrock-handoff-autonomous.ts +231 -0
  25. package/src/agents/AgentContext.js +0 -782
  26. package/src/agents/AgentContext.test.js +0 -421
  27. package/src/agents/__tests__/AgentContext.test.js +0 -678
  28. package/src/agents/__tests__/resolveStructuredOutputMode.test.js +0 -117
  29. package/src/common/enum.js +0 -192
  30. package/src/common/index.js +0 -3
  31. package/src/events.js +0 -166
  32. package/src/graphs/Graph.js +0 -1857
  33. package/src/graphs/MultiAgentGraph.js +0 -1092
  34. package/src/graphs/__tests__/structured-output.integration.test.js +0 -624
  35. package/src/graphs/__tests__/structured-output.test.js +0 -144
  36. package/src/graphs/contextManagement.e2e.test.js +0 -718
  37. package/src/graphs/contextManagement.test.js +0 -485
  38. package/src/graphs/handoffValidation.test.js +0 -276
  39. package/src/graphs/index.js +0 -3
  40. package/src/index.js +0 -28
  41. package/src/instrumentation.js +0 -21
  42. package/src/llm/anthropic/index.js +0 -319
  43. package/src/llm/anthropic/types.js +0 -46
  44. package/src/llm/anthropic/utils/message_inputs.js +0 -627
  45. package/src/llm/anthropic/utils/message_outputs.js +0 -290
  46. package/src/llm/anthropic/utils/output_parsers.js +0 -89
  47. package/src/llm/anthropic/utils/tools.js +0 -25
  48. package/src/llm/bedrock/__tests__/bedrock-caching.test.js +0 -392
  49. package/src/llm/bedrock/index.js +0 -303
  50. package/src/llm/bedrock/types.js +0 -2
  51. package/src/llm/bedrock/utils/index.js +0 -6
  52. package/src/llm/bedrock/utils/message_inputs.js +0 -463
  53. package/src/llm/bedrock/utils/message_outputs.js +0 -269
  54. package/src/llm/fake.js +0 -92
  55. package/src/llm/google/index.js +0 -215
  56. package/src/llm/google/types.js +0 -12
  57. package/src/llm/google/utils/common.js +0 -670
  58. package/src/llm/google/utils/tools.js +0 -111
  59. package/src/llm/google/utils/zod_to_genai_parameters.js +0 -47
  60. package/src/llm/openai/index.js +0 -1033
  61. package/src/llm/openai/types.js +0 -2
  62. package/src/llm/openai/utils/index.js +0 -756
  63. package/src/llm/openai/utils/isReasoningModel.test.js +0 -79
  64. package/src/llm/openrouter/index.js +0 -261
  65. package/src/llm/openrouter/reasoning.test.js +0 -181
  66. package/src/llm/providers.js +0 -36
  67. package/src/llm/text.js +0 -65
  68. package/src/llm/vertexai/index.js +0 -402
  69. package/src/messages/__tests__/tools.test.js +0 -392
  70. package/src/messages/cache.js +0 -404
  71. package/src/messages/cache.test.js +0 -1167
  72. package/src/messages/content.js +0 -48
  73. package/src/messages/content.test.js +0 -314
  74. package/src/messages/core.js +0 -359
  75. package/src/messages/ensureThinkingBlock.test.js +0 -997
  76. package/src/messages/format.js +0 -973
  77. package/src/messages/formatAgentMessages.test.js +0 -2278
  78. package/src/messages/formatAgentMessages.tools.test.js +0 -362
  79. package/src/messages/formatMessage.test.js +0 -608
  80. package/src/messages/ids.js +0 -18
  81. package/src/messages/index.js +0 -9
  82. package/src/messages/labelContentByAgent.test.js +0 -725
  83. package/src/messages/prune.js +0 -438
  84. package/src/messages/reducer.js +0 -60
  85. package/src/messages/shiftIndexTokenCountMap.test.js +0 -63
  86. package/src/messages/summarize.js +0 -146
  87. package/src/messages/summarize.test.js +0 -332
  88. package/src/messages/tools.js +0 -90
  89. package/src/mockStream.js +0 -81
  90. package/src/prompts/collab.js +0 -7
  91. package/src/prompts/index.js +0 -3
  92. package/src/prompts/taskmanager.js +0 -58
  93. package/src/run.js +0 -427
  94. package/src/schemas/index.js +0 -3
  95. package/src/schemas/schema-preparation.test.js +0 -370
  96. package/src/schemas/validate.js +0 -314
  97. package/src/schemas/validate.test.js +0 -264
  98. package/src/scripts/abort.js +0 -127
  99. package/src/scripts/ant_web_search.js +0 -130
  100. package/src/scripts/ant_web_search_edge_case.js +0 -133
  101. package/src/scripts/ant_web_search_error_edge_case.js +0 -119
  102. package/src/scripts/args.js +0 -41
  103. package/src/scripts/bedrock-cache-debug.js +0 -186
  104. package/src/scripts/bedrock-content-aggregation-test.js +0 -195
  105. package/src/scripts/bedrock-merge-test.js +0 -80
  106. package/src/scripts/bedrock-parallel-tools-test.js +0 -150
  107. package/src/scripts/caching.js +0 -106
  108. package/src/scripts/cli.js +0 -152
  109. package/src/scripts/cli2.js +0 -119
  110. package/src/scripts/cli3.js +0 -163
  111. package/src/scripts/cli4.js +0 -165
  112. package/src/scripts/cli5.js +0 -165
  113. package/src/scripts/code_exec.js +0 -171
  114. package/src/scripts/code_exec_files.js +0 -180
  115. package/src/scripts/code_exec_multi_session.js +0 -185
  116. package/src/scripts/code_exec_ptc.js +0 -265
  117. package/src/scripts/code_exec_session.js +0 -217
  118. package/src/scripts/code_exec_simple.js +0 -120
  119. package/src/scripts/content.js +0 -111
  120. package/src/scripts/empty_input.js +0 -125
  121. package/src/scripts/handoff-test.js +0 -96
  122. package/src/scripts/image.js +0 -138
  123. package/src/scripts/memory.js +0 -83
  124. package/src/scripts/multi-agent-chain.js +0 -271
  125. package/src/scripts/multi-agent-conditional.js +0 -185
  126. package/src/scripts/multi-agent-document-review-chain.js +0 -171
  127. package/src/scripts/multi-agent-hybrid-flow.js +0 -264
  128. package/src/scripts/multi-agent-parallel-start.js +0 -214
  129. package/src/scripts/multi-agent-parallel.js +0 -346
  130. package/src/scripts/multi-agent-sequence.js +0 -184
  131. package/src/scripts/multi-agent-supervisor.js +0 -324
  132. package/src/scripts/multi-agent-test.js +0 -147
  133. package/src/scripts/parallel-asymmetric-tools-test.js +0 -202
  134. package/src/scripts/parallel-full-metadata-test.js +0 -176
  135. package/src/scripts/parallel-tools-test.js +0 -256
  136. package/src/scripts/programmatic_exec.js +0 -277
  137. package/src/scripts/programmatic_exec_agent.js +0 -168
  138. package/src/scripts/search.js +0 -118
  139. package/src/scripts/sequential-full-metadata-test.js +0 -143
  140. package/src/scripts/simple.js +0 -174
  141. package/src/scripts/single-agent-metadata-test.js +0 -152
  142. package/src/scripts/stream.js +0 -113
  143. package/src/scripts/test-custom-prompt-key.js +0 -132
  144. package/src/scripts/test-handoff-input.js +0 -143
  145. package/src/scripts/test-handoff-preamble.js +0 -227
  146. package/src/scripts/test-handoff-steering.js +0 -353
  147. package/src/scripts/test-multi-agent-list-handoff.js +0 -318
  148. package/src/scripts/test-parallel-agent-labeling.js +0 -253
  149. package/src/scripts/test-parallel-handoffs.js +0 -229
  150. package/src/scripts/test-thinking-handoff-bedrock.js +0 -132
  151. package/src/scripts/test-thinking-handoff.js +0 -132
  152. package/src/scripts/test-thinking-to-thinking-handoff-bedrock.js +0 -140
  153. package/src/scripts/test-tool-before-handoff-role-order.js +0 -223
  154. package/src/scripts/test-tools-before-handoff.js +0 -187
  155. package/src/scripts/test_code_api.js +0 -263
  156. package/src/scripts/thinking-bedrock.js +0 -128
  157. package/src/scripts/thinking-vertexai.js +0 -130
  158. package/src/scripts/thinking.js +0 -134
  159. package/src/scripts/tool_search.js +0 -114
  160. package/src/scripts/tools.js +0 -125
  161. package/src/specs/agent-handoffs-bedrock.integration.test.js +0 -280
  162. package/src/specs/agent-handoffs.test.js +0 -924
  163. package/src/specs/anthropic.simple.test.js +0 -287
  164. package/src/specs/azure.simple.test.js +0 -381
  165. package/src/specs/cache.simple.test.js +0 -282
  166. package/src/specs/custom-event-await.test.js +0 -148
  167. package/src/specs/deepseek.simple.test.js +0 -189
  168. package/src/specs/emergency-prune.test.js +0 -308
  169. package/src/specs/moonshot.simple.test.js +0 -237
  170. package/src/specs/observability.integration.test.js +0 -1337
  171. package/src/specs/openai.simple.test.js +0 -233
  172. package/src/specs/openrouter.simple.test.js +0 -202
  173. package/src/specs/prune.test.js +0 -733
  174. package/src/specs/reasoning.test.js +0 -144
  175. package/src/specs/spec.utils.js +0 -4
  176. package/src/specs/thinking-handoff.test.js +0 -486
  177. package/src/specs/thinking-prune.test.js +0 -600
  178. package/src/specs/token-distribution-edge-case.test.js +0 -246
  179. package/src/specs/token-memoization.test.js +0 -32
  180. package/src/specs/tokens.test.js +0 -49
  181. package/src/specs/tool-error.test.js +0 -139
  182. package/src/splitStream.js +0 -204
  183. package/src/splitStream.test.js +0 -504
  184. package/src/stream.js +0 -650
  185. package/src/stream.test.js +0 -225
  186. package/src/test/mockTools.js +0 -340
  187. package/src/tools/BrowserTools.js +0 -245
  188. package/src/tools/Calculator.js +0 -38
  189. package/src/tools/Calculator.test.js +0 -225
  190. package/src/tools/CodeExecutor.js +0 -233
  191. package/src/tools/ProgrammaticToolCalling.js +0 -602
  192. package/src/tools/StreamingToolCallBuffer.js +0 -179
  193. package/src/tools/ToolNode.js +0 -930
  194. package/src/tools/ToolSearch.js +0 -904
  195. package/src/tools/__tests__/BrowserTools.test.js +0 -306
  196. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.js +0 -276
  197. package/src/tools/__tests__/ProgrammaticToolCalling.test.js +0 -807
  198. package/src/tools/__tests__/StreamingToolCallBuffer.test.js +0 -175
  199. package/src/tools/__tests__/ToolApproval.test.js +0 -675
  200. package/src/tools/__tests__/ToolNode.recovery.test.js +0 -200
  201. package/src/tools/__tests__/ToolNode.session.test.js +0 -319
  202. package/src/tools/__tests__/ToolSearch.integration.test.js +0 -125
  203. package/src/tools/__tests__/ToolSearch.test.js +0 -812
  204. package/src/tools/__tests__/handlers.test.js +0 -799
  205. package/src/tools/__tests__/truncation-recovery.integration.test.js +0 -362
  206. package/src/tools/handlers.js +0 -306
  207. package/src/tools/schema.js +0 -25
  208. package/src/tools/search/anthropic.js +0 -34
  209. package/src/tools/search/content.js +0 -116
  210. package/src/tools/search/content.test.js +0 -133
  211. package/src/tools/search/firecrawl.js +0 -173
  212. package/src/tools/search/format.js +0 -198
  213. package/src/tools/search/highlights.js +0 -241
  214. package/src/tools/search/index.js +0 -3
  215. package/src/tools/search/jina-reranker.test.js +0 -106
  216. package/src/tools/search/rerankers.js +0 -165
  217. package/src/tools/search/schema.js +0 -102
  218. package/src/tools/search/search.js +0 -561
  219. package/src/tools/search/serper-scraper.js +0 -126
  220. package/src/tools/search/test.js +0 -129
  221. package/src/tools/search/tool.js +0 -453
  222. package/src/tools/search/types.js +0 -2
  223. package/src/tools/search/utils.js +0 -59
  224. package/src/types/graph.js +0 -24
  225. package/src/types/graph.test.js +0 -192
  226. package/src/types/index.js +0 -7
  227. package/src/types/llm.js +0 -2
  228. package/src/types/messages.js +0 -2
  229. package/src/types/run.js +0 -2
  230. package/src/types/stream.js +0 -2
  231. package/src/types/tools.js +0 -2
  232. package/src/utils/contextAnalytics.js +0 -79
  233. package/src/utils/contextAnalytics.test.js +0 -166
  234. package/src/utils/events.js +0 -26
  235. package/src/utils/graph.js +0 -11
  236. package/src/utils/handlers.js +0 -65
  237. package/src/utils/index.js +0 -10
  238. package/src/utils/llm.js +0 -21
  239. package/src/utils/llmConfig.js +0 -205
  240. package/src/utils/logging.js +0 -37
  241. package/src/utils/misc.js +0 -51
  242. package/src/utils/run.js +0 -69
  243. package/src/utils/schema.js +0 -21
  244. package/src/utils/title.js +0 -119
  245. package/src/utils/tokens.js +0 -92
  246. package/src/utils/toonFormat.js +0 -379
@@ -1,924 +0,0 @@
1
- // src/specs/agent-handoffs.test.ts
2
- import { DynamicStructuredTool } from '@langchain/core/tools';
3
- import { HumanMessage, ToolMessage } from '@langchain/core/messages';
4
- import { Providers, Constants } from '@/common';
5
- import { Run } from '@/run';
6
- /**
7
- * Helper to safely get tool name from tool object
8
- */
9
- const getToolName = (tool) => {
10
- return tool.name;
11
- };
12
- /**
13
- * Helper to safely get tool description from tool object
14
- */
15
- const getToolDescription = (tool) => {
16
- return tool.description;
17
- };
18
- /**
19
- * Helper to safely get tool schema from tool object
20
- */
21
- const getToolSchema = (tool) => {
22
- return tool.schema;
23
- };
24
- /**
25
- * Helper to find tool by name
26
- */
27
- const findToolByName = (tools, name) => {
28
- return tools?.find((tool) => getToolName(tool) === name);
29
- };
30
- /**
31
- * Test suite for Agent Handoffs feature
32
- *
33
- * Tests cover:
34
- * - Basic handoff between two agents
35
- * - Handoffs with custom descriptions
36
- * - Handoffs with prompts and prompt keys
37
- * - Sequential handoffs (A -> B -> C)
38
- * - Bidirectional handoffs (A <-> B)
39
- * - Multiple handoff options from single agent
40
- * - Handoff tool creation and execution
41
- * - Error cases and edge conditions
42
- */
43
- describe('Agent Handoffs Tests', () => {
44
- jest.setTimeout(30000);
45
- const createTestConfig = (agents, edges) => ({
46
- runId: `handoff-test-${Date.now()}-${Math.random()}`,
47
- graphConfig: {
48
- type: 'multi-agent',
49
- agents,
50
- edges,
51
- },
52
- returnContent: true,
53
- skipCleanup: true,
54
- });
55
- const createBasicAgent = (agentId, instructions) => ({
56
- agentId,
57
- provider: Providers.ANTHROPIC,
58
- clientOptions: {
59
- modelName: 'claude-haiku-4-5',
60
- apiKey: 'test-key',
61
- },
62
- instructions,
63
- maxContextTokens: 28000,
64
- });
65
- describe('Basic Handoff Tests', () => {
66
- it('should create handoff tool for agent with outgoing handoff edge', async () => {
67
- const agents = [
68
- createBasicAgent('agent_a', 'You are agent A'),
69
- createBasicAgent('agent_b', 'You are agent B'),
70
- ];
71
- const edges = [
72
- {
73
- from: 'agent_a',
74
- to: 'agent_b',
75
- edgeType: 'handoff',
76
- description: 'Transfer to agent B',
77
- },
78
- ];
79
- const run = await Run.create(createTestConfig(agents, edges));
80
- expect(run.Graph).toBeDefined();
81
- const agentAContext = run.Graph.agentContexts.get('agent_a');
82
- expect(agentAContext).toBeDefined();
83
- expect(agentAContext?.graphTools).toBeDefined();
84
- // Check that handoff tool was created
85
- const handoffTool = findToolByName(agentAContext?.graphTools, `${Constants.LC_TRANSFER_TO_}agent_b`);
86
- expect(handoffTool).toBeDefined();
87
- expect(getToolDescription(handoffTool)).toBe('Transfer to agent B');
88
- });
89
- it('should successfully handoff from agent A to agent B', async () => {
90
- const agents = [
91
- createBasicAgent('agent_a', 'You are agent A. Transfer to agent B.'),
92
- createBasicAgent('agent_b', 'You are agent B. Respond to the user.'),
93
- ];
94
- const edges = [
95
- {
96
- from: 'agent_a',
97
- to: 'agent_b',
98
- edgeType: 'handoff',
99
- description: 'Transfer to agent B when needed',
100
- },
101
- ];
102
- const run = await Run.create(createTestConfig(agents, edges));
103
- // Override models to simulate handoff behavior
104
- run.Graph?.overrideTestModel([
105
- 'Transferring to agent B', // Agent A response
106
- 'Hello from agent B', // Agent B response
107
- ], 10, [
108
- {
109
- id: 'tool_call_1',
110
- name: `${Constants.LC_TRANSFER_TO_}agent_b`,
111
- args: {},
112
- },
113
- ]);
114
- const messages = [new HumanMessage('Hello')];
115
- const config = {
116
- configurable: {
117
- thread_id: 'test-handoff-thread',
118
- },
119
- streamMode: 'values',
120
- version: 'v2',
121
- };
122
- await run.processStream({ messages }, config);
123
- const finalMessages = run.getRunMessages();
124
- expect(finalMessages).toBeDefined();
125
- expect(finalMessages.length).toBeGreaterThan(1);
126
- // Check for tool message indicating handoff
127
- const toolMessages = finalMessages.filter((msg) => msg.getType() === 'tool');
128
- const handoffToolMessage = toolMessages.find((msg) => msg.name === `${Constants.LC_TRANSFER_TO_}agent_b`);
129
- expect(handoffToolMessage).toBeDefined();
130
- expect(handoffToolMessage?.content).toContain('transferred to agent_b');
131
- });
132
- it('should not create handoff tool for agent without outgoing edges', async () => {
133
- const agents = [
134
- createBasicAgent('agent_a', 'You are agent A'),
135
- createBasicAgent('agent_b', 'You are agent B'),
136
- ];
137
- const edges = [
138
- {
139
- from: 'agent_a',
140
- to: 'agent_b',
141
- edgeType: 'handoff',
142
- },
143
- ];
144
- const run = await Run.create(createTestConfig(agents, edges));
145
- const agentBContext = run.Graph.agentContexts.get('agent_b');
146
- expect(agentBContext).toBeDefined();
147
- // Agent B should not have handoff tools (no outgoing edges)
148
- const handoffTools = agentBContext?.graphTools?.filter((tool) => {
149
- const name = getToolName(tool);
150
- return name?.startsWith(Constants.LC_TRANSFER_TO_) ?? false;
151
- });
152
- expect(handoffTools?.length ?? 0).toBe(0);
153
- });
154
- });
155
- describe('Bidirectional Handoffs', () => {
156
- it('should create handoff tools for both agents in bidirectional setup', async () => {
157
- const agents = [
158
- createBasicAgent('agent_a', 'You are agent A'),
159
- createBasicAgent('agent_b', 'You are agent B'),
160
- ];
161
- const edges = [
162
- {
163
- from: 'agent_a',
164
- to: 'agent_b',
165
- edgeType: 'handoff',
166
- description: 'Transfer to agent B',
167
- },
168
- {
169
- from: 'agent_b',
170
- to: 'agent_a',
171
- edgeType: 'handoff',
172
- description: 'Transfer to agent A',
173
- },
174
- ];
175
- const run = await Run.create(createTestConfig(agents, edges));
176
- const agentAContext = run.Graph.agentContexts.get('agent_a');
177
- const agentBContext = run.Graph.agentContexts.get('agent_b');
178
- // Agent A should have tool to transfer to B
179
- const agentAHandoffTool = findToolByName(agentAContext?.graphTools, `${Constants.LC_TRANSFER_TO_}agent_b`);
180
- expect(agentAHandoffTool).toBeDefined();
181
- // Agent B should have tool to transfer to A
182
- const agentBHandoffTool = findToolByName(agentBContext?.graphTools, `${Constants.LC_TRANSFER_TO_}agent_a`);
183
- expect(agentBHandoffTool).toBeDefined();
184
- });
185
- it('should handle handoff from A to B in bidirectional setup', async () => {
186
- const agents = [
187
- createBasicAgent('agent_a', 'You are agent A'),
188
- createBasicAgent('agent_b', 'You are agent B'),
189
- ];
190
- const edges = [
191
- {
192
- from: 'agent_a',
193
- to: 'agent_b',
194
- edgeType: 'handoff',
195
- },
196
- {
197
- from: 'agent_b',
198
- to: 'agent_a',
199
- edgeType: 'handoff',
200
- },
201
- ];
202
- const run = await Run.create(createTestConfig(agents, edges));
203
- // Simulate single handoff from A to B
204
- run.Graph?.overrideTestModel(['Transferring to B', 'Response from B'], 10, [
205
- {
206
- id: 'tool_call_1',
207
- name: `${Constants.LC_TRANSFER_TO_}agent_b`,
208
- args: {},
209
- },
210
- ]);
211
- const messages = [new HumanMessage('Start conversation')];
212
- const config = {
213
- configurable: {
214
- thread_id: 'test-bidirectional-thread',
215
- },
216
- streamMode: 'values',
217
- version: 'v2',
218
- };
219
- await run.processStream({ messages }, config);
220
- const finalMessages = run.getRunMessages();
221
- expect(finalMessages).toBeDefined();
222
- // Should have a handoff tool message
223
- const toolMessages = finalMessages.filter((msg) => msg.getType() === 'tool');
224
- const handoffMessage = toolMessages.find((msg) => msg.name === `${Constants.LC_TRANSFER_TO_}agent_b`);
225
- expect(handoffMessage).toBeDefined();
226
- });
227
- });
228
- describe('Sequential Handoffs (Chain)', () => {
229
- it('should create handoff tools for chain of agents A -> B -> C', async () => {
230
- const agents = [
231
- createBasicAgent('agent_a', 'You are agent A'),
232
- createBasicAgent('agent_b', 'You are agent B'),
233
- createBasicAgent('agent_c', 'You are agent C'),
234
- ];
235
- const edges = [
236
- {
237
- from: 'agent_a',
238
- to: 'agent_b',
239
- edgeType: 'handoff',
240
- description: 'Transfer to agent B',
241
- },
242
- {
243
- from: 'agent_b',
244
- to: 'agent_c',
245
- edgeType: 'handoff',
246
- description: 'Transfer to agent C',
247
- },
248
- ];
249
- const run = await Run.create(createTestConfig(agents, edges));
250
- const agentAContext = run.Graph.agentContexts.get('agent_a');
251
- const agentBContext = run.Graph.agentContexts.get('agent_b');
252
- const agentCContext = run.Graph.agentContexts.get('agent_c');
253
- // Agent A should have tool to transfer to B
254
- expect(findToolByName(agentAContext?.graphTools, `${Constants.LC_TRANSFER_TO_}agent_b`)).toBeDefined();
255
- // Agent B should have tool to transfer to C
256
- expect(findToolByName(agentBContext?.graphTools, `${Constants.LC_TRANSFER_TO_}agent_c`)).toBeDefined();
257
- // Agent C should have no handoff tools
258
- const agentCHandoffTools = agentCContext?.graphTools?.filter((tool) => {
259
- const name = getToolName(tool);
260
- return name?.startsWith(Constants.LC_TRANSFER_TO_) ?? false;
261
- });
262
- expect(agentCHandoffTools?.length ?? 0).toBe(0);
263
- });
264
- });
265
- describe('Multiple Handoff Options', () => {
266
- it('should create multiple handoff tools when agent has multiple outgoing edges', async () => {
267
- const agents = [
268
- createBasicAgent('router', 'You are a router agent'),
269
- createBasicAgent('agent_a', 'You are agent A'),
270
- createBasicAgent('agent_b', 'You are agent B'),
271
- createBasicAgent('agent_c', 'You are agent C'),
272
- ];
273
- const edges = [
274
- {
275
- from: 'router',
276
- to: 'agent_a',
277
- edgeType: 'handoff',
278
- description: 'Transfer to agent A for task A',
279
- },
280
- {
281
- from: 'router',
282
- to: 'agent_b',
283
- edgeType: 'handoff',
284
- description: 'Transfer to agent B for task B',
285
- },
286
- {
287
- from: 'router',
288
- to: 'agent_c',
289
- edgeType: 'handoff',
290
- description: 'Transfer to agent C for task C',
291
- },
292
- ];
293
- const run = await Run.create(createTestConfig(agents, edges));
294
- const routerContext = run.Graph.agentContexts.get('router');
295
- expect(routerContext).toBeDefined();
296
- // Router should have 3 handoff tools
297
- const handoffTools = routerContext?.graphTools?.filter((tool) => {
298
- const name = getToolName(tool);
299
- return name?.startsWith(Constants.LC_TRANSFER_TO_) ?? false;
300
- });
301
- expect(handoffTools?.length).toBe(3);
302
- // Verify each tool exists
303
- expect(findToolByName(handoffTools, `${Constants.LC_TRANSFER_TO_}agent_a`)).toBeDefined();
304
- expect(findToolByName(handoffTools, `${Constants.LC_TRANSFER_TO_}agent_b`)).toBeDefined();
305
- expect(findToolByName(handoffTools, `${Constants.LC_TRANSFER_TO_}agent_c`)).toBeDefined();
306
- });
307
- it('should route to correct agent based on handoff tool used', async () => {
308
- const agents = [
309
- createBasicAgent('router', 'You are a router'),
310
- createBasicAgent('agent_a', 'You are agent A'),
311
- createBasicAgent('agent_b', 'You are agent B'),
312
- ];
313
- const edges = [
314
- {
315
- from: 'router',
316
- to: 'agent_a',
317
- edgeType: 'handoff',
318
- description: 'Transfer to agent A',
319
- },
320
- {
321
- from: 'router',
322
- to: 'agent_b',
323
- edgeType: 'handoff',
324
- description: 'Transfer to agent B',
325
- },
326
- ];
327
- const run = await Run.create(createTestConfig(agents, edges));
328
- // Router chooses agent_b
329
- run.Graph?.overrideTestModel(['Routing to agent B', 'Hello from agent B'], 10, [
330
- {
331
- id: 'tool_call_1',
332
- name: `${Constants.LC_TRANSFER_TO_}agent_b`,
333
- args: {},
334
- },
335
- ]);
336
- const messages = [new HumanMessage('Route this message')];
337
- const config = {
338
- configurable: {
339
- thread_id: 'test-routing-thread',
340
- },
341
- streamMode: 'values',
342
- version: 'v2',
343
- };
344
- await run.processStream({ messages }, config);
345
- const finalMessages = run.getRunMessages();
346
- const toolMessages = finalMessages.filter((msg) => msg.getType() === 'tool');
347
- // Should have handoff to agent_b, not agent_a
348
- const handoffToB = toolMessages.find((msg) => msg.name === `${Constants.LC_TRANSFER_TO_}agent_b`);
349
- expect(handoffToB).toBeDefined();
350
- const handoffToA = toolMessages.find((msg) => msg.name === `${Constants.LC_TRANSFER_TO_}agent_a`);
351
- expect(handoffToA).toBeUndefined();
352
- });
353
- });
354
- describe('Handoffs with Prompts', () => {
355
- it('should create handoff tool with prompt parameter when prompt is specified', async () => {
356
- const agents = [
357
- createBasicAgent('agent_a', 'You are agent A'),
358
- createBasicAgent('agent_b', 'You are agent B'),
359
- ];
360
- const edges = [
361
- {
362
- from: 'agent_a',
363
- to: 'agent_b',
364
- edgeType: 'handoff',
365
- description: 'Transfer to agent B with instructions',
366
- prompt: 'Provide specific instructions for agent B',
367
- promptKey: 'instructions',
368
- },
369
- ];
370
- const run = await Run.create(createTestConfig(agents, edges));
371
- const agentAContext = run.Graph.agentContexts.get('agent_a');
372
- const handoffTool = findToolByName(agentAContext?.graphTools, `${Constants.LC_TRANSFER_TO_}agent_b`);
373
- expect(handoffTool).toBeDefined();
374
- // Tool should accept parameters (schema should be defined)
375
- expect(getToolSchema(handoffTool)).toBeDefined();
376
- });
377
- it('should use default promptKey when not specified', async () => {
378
- const agents = [
379
- createBasicAgent('agent_a', 'You are agent A'),
380
- createBasicAgent('agent_b', 'You are agent B'),
381
- ];
382
- const edges = [
383
- {
384
- from: 'agent_a',
385
- to: 'agent_b',
386
- edgeType: 'handoff',
387
- prompt: 'Instructions for handoff',
388
- // promptKey not specified, should default to 'instructions'
389
- },
390
- ];
391
- const run = await Run.create(createTestConfig(agents, edges));
392
- const agentAContext = run.Graph.agentContexts.get('agent_a');
393
- const handoffTool = findToolByName(agentAContext?.graphTools, `${Constants.LC_TRANSFER_TO_}agent_b`);
394
- expect(handoffTool).toBeDefined();
395
- expect(getToolSchema(handoffTool)).toBeDefined();
396
- });
397
- it('should include prompt content in handoff tool message', async () => {
398
- const agents = [
399
- createBasicAgent('agent_a', 'You are agent A'),
400
- createBasicAgent('agent_b', 'You are agent B'),
401
- ];
402
- const edges = [
403
- {
404
- from: 'agent_a',
405
- to: 'agent_b',
406
- edgeType: 'handoff',
407
- description: 'Transfer to agent B',
408
- prompt: 'Additional context for agent B',
409
- promptKey: 'context',
410
- },
411
- ];
412
- const run = await Run.create(createTestConfig(agents, edges));
413
- run.Graph?.overrideTestModel(['Transferring with context'], 10, [
414
- {
415
- id: 'tool_call_1',
416
- name: `${Constants.LC_TRANSFER_TO_}agent_b`,
417
- args: { context: 'User needs help with booking' },
418
- },
419
- ]);
420
- const messages = [new HumanMessage('Help me')];
421
- const config = {
422
- configurable: {
423
- thread_id: 'test-prompt-thread',
424
- },
425
- streamMode: 'values',
426
- version: 'v2',
427
- };
428
- await run.processStream({ messages }, config);
429
- const finalMessages = run.getRunMessages();
430
- const toolMessages = finalMessages.filter((msg) => msg.getType() === 'tool');
431
- const handoffMessage = toolMessages.find((msg) => msg.name === `${Constants.LC_TRANSFER_TO_}agent_b`);
432
- expect(handoffMessage).toBeDefined();
433
- // Tool message should contain the prompt key and value
434
- expect(handoffMessage?.content).toContain('Context:');
435
- });
436
- });
437
- describe('Edge Cases and Error Handling', () => {
438
- it('should handle self-referential edge gracefully', async () => {
439
- const agents = [
440
- createBasicAgent('agent_a', 'You are agent A'),
441
- ];
442
- const edges = [
443
- {
444
- from: 'agent_a',
445
- to: 'agent_a',
446
- edgeType: 'handoff',
447
- description: 'Self-handoff (should be allowed but unusual)',
448
- },
449
- ];
450
- // Should not throw during creation
451
- expect(async () => {
452
- await Run.create(createTestConfig(agents, edges));
453
- }).not.toThrow();
454
- });
455
- it('should handle empty edges array', async () => {
456
- const agents = [
457
- createBasicAgent('agent_a', 'You are agent A'),
458
- createBasicAgent('agent_b', 'You are agent B'),
459
- ];
460
- const edges = [];
461
- const run = await Run.create(createTestConfig(agents, edges));
462
- expect(run.Graph).toBeDefined();
463
- // Agents should have no handoff tools
464
- const agentAContext = run.Graph.agentContexts.get('agent_a');
465
- const handoffTools = agentAContext?.graphTools?.filter((tool) => {
466
- const name = getToolName(tool);
467
- return name?.startsWith(Constants.LC_TRANSFER_TO_) ?? false;
468
- });
469
- expect(handoffTools?.length ?? 0).toBe(0);
470
- });
471
- it('should start from first agent when no edges are defined', async () => {
472
- const agents = [
473
- createBasicAgent('agent_a', 'You are agent A'),
474
- createBasicAgent('agent_b', 'You are agent B'),
475
- ];
476
- const edges = [];
477
- const run = await Run.create(createTestConfig(agents, edges));
478
- run.Graph?.overrideTestModel(['Response from first agent'], 10);
479
- const messages = [new HumanMessage('Hello')];
480
- const config = {
481
- configurable: {
482
- thread_id: 'test-no-edges-thread',
483
- },
484
- streamMode: 'values',
485
- version: 'v2',
486
- };
487
- await run.processStream({ messages }, config);
488
- const finalMessages = run.getRunMessages();
489
- expect(finalMessages).toBeDefined();
490
- expect(finalMessages.length).toBeGreaterThan(0);
491
- });
492
- it('should handle agents with existing tools alongside handoff tools', async () => {
493
- const customTool = new DynamicStructuredTool({
494
- name: 'custom_tool',
495
- description: 'A custom tool',
496
- schema: { type: 'object', properties: {}, required: [] },
497
- func: async () => 'Tool result',
498
- });
499
- const agents = [
500
- {
501
- ...createBasicAgent('agent_a', 'You are agent A'),
502
- tools: [customTool],
503
- },
504
- createBasicAgent('agent_b', 'You are agent B'),
505
- ];
506
- const edges = [
507
- {
508
- from: 'agent_a',
509
- to: 'agent_b',
510
- edgeType: 'handoff',
511
- description: 'Transfer to agent B',
512
- },
513
- ];
514
- const run = await Run.create(createTestConfig(agents, edges));
515
- const agentAContext = run.Graph.agentContexts.get('agent_a');
516
- // Agent A should have custom tool in tools and handoff tool in graphTools
517
- expect(findToolByName(agentAContext?.tools, 'custom_tool')).toBeDefined();
518
- expect(findToolByName(agentAContext?.graphTools, `${Constants.LC_TRANSFER_TO_}agent_b`)).toBeDefined();
519
- });
520
- });
521
- describe('Graph Structure Analysis', () => {
522
- it('should correctly identify starting nodes with no incoming edges', async () => {
523
- const agents = [
524
- createBasicAgent('agent_a', 'Starting agent'),
525
- createBasicAgent('agent_b', 'Middle agent'),
526
- createBasicAgent('agent_c', 'End agent'),
527
- ];
528
- const edges = [
529
- {
530
- from: 'agent_a',
531
- to: 'agent_b',
532
- edgeType: 'handoff',
533
- },
534
- {
535
- from: 'agent_b',
536
- to: 'agent_c',
537
- edgeType: 'handoff',
538
- },
539
- ];
540
- const run = await Run.create(createTestConfig(agents, edges));
541
- // agent_a should be the starting node (no incoming edges)
542
- expect(run.Graph).toBeDefined();
543
- // This is internal behavior, but we can test via execution
544
- run.Graph?.overrideTestModel(['Response from agent A'], 10);
545
- const messages = [new HumanMessage('Start')];
546
- const config = {
547
- configurable: {
548
- thread_id: 'test-starting-node-thread',
549
- },
550
- streamMode: 'values',
551
- version: 'v2',
552
- };
553
- // Should start from agent_a
554
- await run.processStream({ messages }, config);
555
- const finalMessages = run.getRunMessages();
556
- expect(finalMessages).toBeDefined();
557
- });
558
- it('should handle multiple starting nodes (parallel entry points)', async () => {
559
- const agents = [
560
- createBasicAgent('agent_a', 'Starting agent A'),
561
- createBasicAgent('agent_b', 'Starting agent B'),
562
- createBasicAgent('agent_c', 'Shared destination'),
563
- ];
564
- const edges = [
565
- {
566
- from: 'agent_a',
567
- to: 'agent_c',
568
- edgeType: 'handoff',
569
- },
570
- {
571
- from: 'agent_b',
572
- to: 'agent_c',
573
- edgeType: 'handoff',
574
- },
575
- ];
576
- // Both agent_a and agent_b have no incoming edges, so both are starting nodes
577
- const run = await Run.create(createTestConfig(agents, edges));
578
- expect(run.Graph).toBeDefined();
579
- });
580
- });
581
- describe('Tool Call Before Handoff (Issue #54)', () => {
582
- it('should complete handoff when router calls a non-handoff tool in the same turn', async () => {
583
- /**
584
- * Reproduces the bug from issue #54:
585
- * When a router calls a regular tool AND a handoff tool in the same turn,
586
- * the filtered messages for the receiving agent end with a ToolMessage.
587
- * Previously, instructions were appended as a HumanMessage (tool → user),
588
- * which many APIs reject. The fix injects instructions into the last
589
- * ToolMessage instead.
590
- */
591
- const customTool = new DynamicStructuredTool({
592
- name: 'list_upload_sessions',
593
- description: 'List available upload sessions',
594
- schema: { type: 'object', properties: {}, required: [] },
595
- func: async () => JSON.stringify({ sessions: [{ id: 'sess_1', status: 'ready' }] }),
596
- });
597
- const agents = [
598
- {
599
- ...createBasicAgent('router', 'You are a router'),
600
- tools: [customTool],
601
- toolMap: new Map([['list_upload_sessions', customTool]]),
602
- },
603
- createBasicAgent('data_analyst', 'You are a data analyst'),
604
- ];
605
- const edges = [
606
- {
607
- from: 'router',
608
- to: 'data_analyst',
609
- edgeType: 'handoff',
610
- description: 'Transfer to data analyst',
611
- prompt: 'Instructions for the analyst about what to analyze',
612
- promptKey: 'instructions',
613
- },
614
- ];
615
- const run = await Run.create(createTestConfig(agents, edges));
616
- /**
617
- * Simulate router calling list_upload_sessions AND handoff in the same turn.
618
- * The first model response includes both tool calls.
619
- * The second model response is the data_analyst's reply.
620
- */
621
- run.Graph?.overrideTestModel([
622
- 'Checking available sessions and transferring to analyst',
623
- 'Here is my analysis of the available sessions',
624
- ], 10, [
625
- {
626
- id: 'tool_call_1',
627
- name: 'list_upload_sessions',
628
- args: {},
629
- },
630
- {
631
- id: 'tool_call_2',
632
- name: `${Constants.LC_TRANSFER_TO_}data_analyst`,
633
- args: { instructions: 'Analyze the upload session data' },
634
- },
635
- ]);
636
- const messages = [
637
- new HumanMessage('Check my upload sessions and analyze them'),
638
- ];
639
- const config = {
640
- configurable: {
641
- thread_id: 'test-tool-before-handoff-thread',
642
- },
643
- streamMode: 'values',
644
- version: 'v2',
645
- };
646
- /**
647
- * This should complete without error. Before the fix, the receiving
648
- * agent would get an invalid tool → user message sequence.
649
- */
650
- await run.processStream({ messages }, config);
651
- const finalMessages = run.getRunMessages();
652
- expect(finalMessages).toBeDefined();
653
- expect(finalMessages.length).toBeGreaterThan(1);
654
- /** Verify that the handoff occurred */
655
- const toolMessages = finalMessages.filter((msg) => msg.getType() === 'tool');
656
- const handoffMessage = toolMessages.find((msg) => msg.name === `${Constants.LC_TRANSFER_TO_}data_analyst`);
657
- expect(handoffMessage).toBeDefined();
658
- /** Verify the flow completed (agent B responded) */
659
- const aiMessages = finalMessages.filter((msg) => msg.getType() === 'ai');
660
- expect(aiMessages.length).toBeGreaterThanOrEqual(1);
661
- });
662
- });
663
- describe('Handoff Tool Naming', () => {
664
- it('should use correct naming convention for handoff tools', async () => {
665
- const agents = [
666
- createBasicAgent('flight_assistant', 'You handle flights'),
667
- createBasicAgent('hotel_assistant', 'You handle hotels'),
668
- ];
669
- const edges = [
670
- {
671
- from: 'flight_assistant',
672
- to: 'hotel_assistant',
673
- edgeType: 'handoff',
674
- description: 'Transfer to hotel booking',
675
- },
676
- ];
677
- const run = await Run.create(createTestConfig(agents, edges));
678
- const flightContext = run.Graph.agentContexts.get('flight_assistant');
679
- const handoffTool = findToolByName(flightContext?.graphTools, `${Constants.LC_TRANSFER_TO_}hotel_assistant`);
680
- expect(handoffTool).toBeDefined();
681
- expect(getToolName(handoffTool)).toBe(`${Constants.LC_TRANSFER_TO_}hotel_assistant`);
682
- });
683
- it('should preserve agent ID format in tool names', async () => {
684
- const agents = [
685
- createBasicAgent('agent_with_underscores', 'Agent with underscores'),
686
- createBasicAgent('AgentWithCamelCase', 'Agent with camel case'),
687
- ];
688
- const edges = [
689
- {
690
- from: 'agent_with_underscores',
691
- to: 'AgentWithCamelCase',
692
- edgeType: 'handoff',
693
- },
694
- ];
695
- const run = await Run.create(createTestConfig(agents, edges));
696
- const agentContext = run.Graph.agentContexts.get('agent_with_underscores');
697
- const handoffTool = findToolByName(agentContext?.graphTools, `${Constants.LC_TRANSFER_TO_}AgentWithCamelCase`);
698
- expect(handoffTool).toBeDefined();
699
- expect(getToolName(handoffTool)).toBe(`${Constants.LC_TRANSFER_TO_}AgentWithCamelCase`);
700
- });
701
- });
702
- describe('Handoff Tool Description Enrichment', () => {
703
- it('should use agent name and description when edge.description is not provided', async () => {
704
- const agents = [
705
- createBasicAgent('supervisor', 'You are a supervisor'),
706
- {
707
- ...createBasicAgent('data_analyst', 'You analyze data'),
708
- name: 'Data Analyst',
709
- description: 'Analyzes datasets, creates charts, and provides statistical insights',
710
- },
711
- ];
712
- const edges = [
713
- {
714
- from: 'supervisor',
715
- to: 'data_analyst',
716
- edgeType: 'handoff',
717
- // No description provided - should auto-generate from agent name + description
718
- },
719
- ];
720
- const run = await Run.create(createTestConfig(agents, edges));
721
- const supervisorContext = run.Graph.agentContexts.get('supervisor');
722
- const handoffTool = findToolByName(supervisorContext?.tools, `${Constants.LC_TRANSFER_TO_}data_analyst`);
723
- expect(handoffTool).toBeDefined();
724
- const description = getToolDescription(handoffTool);
725
- expect(description).toContain('Data Analyst');
726
- expect(description).toContain('Analyzes datasets');
727
- });
728
- it('should use agent name without description when only name is available', async () => {
729
- const agents = [
730
- createBasicAgent('supervisor', 'You are a supervisor'),
731
- {
732
- ...createBasicAgent('writer', 'You write things'),
733
- name: 'Content Writer',
734
- // No description field
735
- },
736
- ];
737
- const edges = [
738
- {
739
- from: 'supervisor',
740
- to: 'writer',
741
- edgeType: 'handoff',
742
- },
743
- ];
744
- const run = await Run.create(createTestConfig(agents, edges));
745
- const supervisorContext = run.Graph.agentContexts.get('supervisor');
746
- const handoffTool = findToolByName(supervisorContext?.tools, `${Constants.LC_TRANSFER_TO_}writer`);
747
- expect(handoffTool).toBeDefined();
748
- const description = getToolDescription(handoffTool);
749
- expect(description).toContain('Content Writer');
750
- });
751
- it('should prefer explicit edge.description over auto-generated description', async () => {
752
- const agents = [
753
- createBasicAgent('supervisor', 'You are a supervisor'),
754
- {
755
- ...createBasicAgent('agent_b', 'You do things'),
756
- name: 'Agent B',
757
- description: 'This is agent B description',
758
- },
759
- ];
760
- const edges = [
761
- {
762
- from: 'supervisor',
763
- to: 'agent_b',
764
- edgeType: 'handoff',
765
- description: 'Custom handoff description that takes priority',
766
- },
767
- ];
768
- const run = await Run.create(createTestConfig(agents, edges));
769
- const supervisorContext = run.Graph.agentContexts.get('supervisor');
770
- const handoffTool = findToolByName(supervisorContext?.tools, `${Constants.LC_TRANSFER_TO_}agent_b`);
771
- expect(handoffTool).toBeDefined();
772
- const description = getToolDescription(handoffTool);
773
- expect(description).toBe('Custom handoff description that takes priority');
774
- });
775
- it('should generate meaningful descriptions for multiple handoff targets', async () => {
776
- const agents = [
777
- createBasicAgent('router', 'You route requests'),
778
- {
779
- ...createBasicAgent('sales', 'Handle sales'),
780
- name: 'Sales Agent',
781
- description: 'Handles product pricing, quotes, and purchase orders',
782
- },
783
- {
784
- ...createBasicAgent('support', 'Handle support'),
785
- name: 'Support Agent',
786
- description: 'Troubleshoots technical issues and handles bug reports',
787
- },
788
- {
789
- ...createBasicAgent('billing', 'Handle billing'),
790
- name: 'Billing Agent',
791
- description: 'Manages invoices, payments, and subscription changes',
792
- },
793
- ];
794
- const edges = [
795
- { from: 'router', to: 'sales', edgeType: 'handoff' },
796
- { from: 'router', to: 'support', edgeType: 'handoff' },
797
- { from: 'router', to: 'billing', edgeType: 'handoff' },
798
- ];
799
- const run = await Run.create(createTestConfig(agents, edges));
800
- const routerContext = run.Graph.agentContexts.get('router');
801
- const salesTool = findToolByName(routerContext?.tools, `${Constants.LC_TRANSFER_TO_}sales`);
802
- const supportTool = findToolByName(routerContext?.tools, `${Constants.LC_TRANSFER_TO_}support`);
803
- const billingTool = findToolByName(routerContext?.tools, `${Constants.LC_TRANSFER_TO_}billing`);
804
- // Each tool should have a meaningful description from the target agent
805
- expect(getToolDescription(salesTool)).toContain('pricing');
806
- expect(getToolDescription(supportTool)).toContain('Troubleshoots');
807
- expect(getToolDescription(billingTool)).toContain('invoices');
808
- });
809
- it('should store description in AgentContext', async () => {
810
- const agents = [
811
- {
812
- ...createBasicAgent('agent_a', 'Instructions for A'),
813
- name: 'Agent Alpha',
814
- description: 'Handles alpha tasks',
815
- },
816
- ];
817
- const edges = [];
818
- const run = await Run.create(createTestConfig(agents, edges));
819
- const context = run.Graph.agentContexts.get('agent_a');
820
- expect(context).toBeDefined();
821
- expect(context?.name).toBe('Agent Alpha');
822
- expect(context?.description).toBe('Handles alpha tasks');
823
- });
824
- });
825
- describe('Last Active Agent Tracking', () => {
826
- it('should return undefined for getLastActiveAgentId before any agent runs', async () => {
827
- const agents = [
828
- createBasicAgent('agent_a', 'You are agent A'),
829
- createBasicAgent('agent_b', 'You are agent B'),
830
- ];
831
- const edges = [
832
- { from: 'agent_a', to: 'agent_b', edgeType: 'handoff' },
833
- ];
834
- const run = await Run.create(createTestConfig(agents, edges));
835
- // Before running, lastActiveAgentId should be undefined
836
- expect(run.getLastActiveAgentId()).toBeUndefined();
837
- });
838
- it('should return undefined for single-agent graph', async () => {
839
- const agents = [
840
- createBasicAgent('agent_a', 'You are agent A'),
841
- ];
842
- const config = {
843
- runId: `single-agent-test-${Date.now()}`,
844
- graphConfig: {
845
- type: 'standard',
846
- agents,
847
- },
848
- returnContent: true,
849
- };
850
- const run = await Run.create(config);
851
- // Single-agent graph does not have getLastActiveAgentId
852
- expect(run.getLastActiveAgentId()).toBeUndefined();
853
- });
854
- it('should track last active agent after handoff execution', async () => {
855
- const agents = [
856
- createBasicAgent('agent_a', 'You are agent A. Transfer to B.'),
857
- createBasicAgent('agent_b', 'You are agent B.'),
858
- ];
859
- const edges = [
860
- {
861
- from: 'agent_a',
862
- to: 'agent_b',
863
- edgeType: 'handoff',
864
- description: 'Transfer to B',
865
- },
866
- ];
867
- const run = await Run.create(createTestConfig(agents, edges));
868
- run.Graph?.overrideTestModel(['Transferring to B', 'Hello from B'], 10, [
869
- {
870
- id: 'tool_call_handoff',
871
- name: `${Constants.LC_TRANSFER_TO_}agent_b`,
872
- args: {},
873
- },
874
- ]);
875
- const messages = [new HumanMessage('Hello')];
876
- const config = {
877
- configurable: { thread_id: 'last-active-test' },
878
- streamMode: 'values',
879
- version: 'v2',
880
- };
881
- await run.processStream({ messages }, config);
882
- // After handoff A -> B, last active agent should be B
883
- expect(run.getLastActiveAgentId()).toBe('agent_b');
884
- });
885
- });
886
- describe('Content Tool Compaction in Handoff', () => {
887
- it('should compact verbose content_tool ToolMessages during handoff', async () => {
888
- const agents = [
889
- createBasicAgent('agent_a', 'You are agent A'),
890
- createBasicAgent('agent_b', 'You are agent B'),
891
- ];
892
- const edges = [
893
- { from: 'agent_a', to: 'agent_b', edgeType: 'handoff' },
894
- ];
895
- const run = await Run.create(createTestConfig(agents, edges));
896
- const { MultiAgentGraph } = await import('@/graphs/MultiAgentGraph');
897
- if (run.Graph instanceof MultiAgentGraph) {
898
- // Build a mock message chain with a verbose content_tool ToolMessage
899
- const _verboseReadOutput = '"Dashboard.tsx" (content_id: abc123)\nShowing lines 1-200 of 200 total.\n---\n' +
900
- Array.from({ length: 200 }, (_, i) => ` ${i + 1} | const x${i} = ${i};`).join('\n') +
901
- '\n---';
902
- const _messages = [
903
- new HumanMessage('Read the file'),
904
- // Simulate AI calling content_tool then transfer
905
- // The processHandoffReception is private, so we test indirectly via the graph execution
906
- ];
907
- // Verify that a ToolMessage with content_tool name and > 500 chars would be compacted
908
- const longContent = 'x'.repeat(600);
909
- const toolMsg = new ToolMessage({
910
- content: `First line summary\n${longContent}`,
911
- name: 'content_tool',
912
- tool_call_id: 'test-tc-1',
913
- status: 'success',
914
- });
915
- // The compaction logic truncates to first line + suffix
916
- const firstLine = `First line summary\n${longContent}`.split('\n')[0];
917
- expect(firstLine).toBe('First line summary');
918
- // ToolMessage content is > 500 chars so it should be compacted
919
- expect(toolMsg.content.length).toBeGreaterThan(500);
920
- }
921
- });
922
- });
923
- });
924
- //# sourceMappingURL=agent-handoffs.test.js.map