@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,1092 +0,0 @@
1
- import { tool } from '@langchain/core/tools';
2
- import { PromptTemplate } from '@langchain/core/prompts';
3
- import { AIMessage, ToolMessage, HumanMessage, SystemMessage, getBufferString, } from '@langchain/core/messages';
4
- import { END, START, Command, StateGraph, Annotation, getCurrentTaskInput, messagesStateReducer, } from '@langchain/langgraph';
5
- import { summarize, createEmergencySummary } from '@/messages';
6
- import { StandardGraph } from './Graph';
7
- import { Constants } from '@/common';
8
- /** Pattern to extract instructions from transfer ToolMessage content */
9
- const HANDOFF_INSTRUCTIONS_PATTERN = /(?:Instructions?|Context):\s*(.+)/is;
10
- /**
11
- * MultiAgentGraph extends StandardGraph to support dynamic multi-agent workflows
12
- * with handoffs, fan-in/fan-out, and other composable patterns.
13
- *
14
- * Key behavior:
15
- * - Agents with ONLY handoff edges: Can dynamically route to any handoff destination
16
- * - Agents with ONLY direct edges: Always follow their direct edges
17
- * - Agents with BOTH: Use Command for exclusive routing (handoff OR direct, not both)
18
- * - If handoff occurs: Only the handoff destination executes
19
- * - If no handoff: Direct edges execute (potentially in parallel)
20
- *
21
- * This enables the common pattern where an agent either delegates (handoff)
22
- * OR continues its workflow (direct edges), but not both simultaneously.
23
- */
24
- export class MultiAgentGraph extends StandardGraph {
25
- edges;
26
- startingNodes = new Set();
27
- directEdges = [];
28
- handoffEdges = [];
29
- /**
30
- * Map of agentId to parallel group info.
31
- * Contains groupId (incrementing number reflecting execution order) for agents in parallel groups.
32
- * Sequential agents (not in any parallel group) have undefined entry.
33
- *
34
- * Example for: researcher -> [analyst1, analyst2, analyst3] -> summarizer
35
- * - researcher: undefined (sequential, order 0)
36
- * - analyst1, analyst2, analyst3: { groupId: 1 } (parallel group, order 1)
37
- * - summarizer: undefined (sequential, order 2)
38
- */
39
- agentParallelGroups = new Map();
40
- /**
41
- * Tracks the ID of the last agent that produced output.
42
- * Used by auto-continuation to know which agent's context to preserve after handoff.
43
- */
44
- lastActiveAgentId;
45
- constructor(input) {
46
- super(input);
47
- this.edges = input.edges;
48
- this.categorizeEdges();
49
- this.analyzeGraph();
50
- this.createHandoffTools();
51
- }
52
- /**
53
- * Categorize edges into handoff and direct types
54
- */
55
- categorizeEdges() {
56
- for (const edge of this.edges) {
57
- // Default behavior: edges with conditions or explicit 'handoff' type are handoff edges
58
- // Edges with explicit 'direct' type or multi-destination without conditions are direct edges
59
- if (edge.edgeType === 'direct') {
60
- this.directEdges.push(edge);
61
- }
62
- else if (edge.edgeType === 'handoff' || edge.condition != null) {
63
- this.handoffEdges.push(edge);
64
- }
65
- else {
66
- // Default: single-to-single edges are handoff, single-to-multiple are direct
67
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
68
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
69
- if (sources.length === 1 && destinations.length > 1) {
70
- // Fan-out pattern defaults to direct
71
- this.directEdges.push(edge);
72
- }
73
- else {
74
- // Everything else defaults to handoff
75
- this.handoffEdges.push(edge);
76
- }
77
- }
78
- }
79
- }
80
- /**
81
- * Analyze graph structure to determine starting nodes and connections
82
- */
83
- analyzeGraph() {
84
- const hasIncomingEdge = new Set();
85
- // Track all nodes that have incoming edges
86
- for (const edge of this.edges) {
87
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
88
- destinations.forEach((dest) => hasIncomingEdge.add(dest));
89
- }
90
- // Starting nodes are those without incoming edges
91
- for (const agentId of this.agentContexts.keys()) {
92
- if (!hasIncomingEdge.has(agentId)) {
93
- this.startingNodes.add(agentId);
94
- }
95
- }
96
- // If no starting nodes found, use the first agent
97
- if (this.startingNodes.size === 0 && this.agentContexts.size > 0) {
98
- this.startingNodes.add(this.agentContexts.keys().next().value);
99
- }
100
- // Determine if graph has parallel execution capability
101
- this.computeParallelCapability();
102
- }
103
- /**
104
- * Compute parallel groups by traversing the graph in execution order.
105
- * Assigns incrementing group IDs that reflect the sequential order of execution.
106
- *
107
- * For: researcher -> [analyst1, analyst2, analyst3] -> summarizer
108
- * - researcher: no group (first sequential node)
109
- * - analyst1, analyst2, analyst3: groupId 1 (first parallel group)
110
- * - summarizer: no group (next sequential node)
111
- *
112
- * This allows frontend to render in order:
113
- * Row 0: researcher
114
- * Row 1: [analyst1, analyst2, analyst3] (grouped)
115
- * Row 2: summarizer
116
- */
117
- computeParallelCapability() {
118
- let groupCounter = 1; // Start at 1, 0 reserved for "no group"
119
- // Check 1: Multiple starting nodes means parallel from the start (group 1)
120
- if (this.startingNodes.size > 1) {
121
- for (const agentId of this.startingNodes) {
122
- this.agentParallelGroups.set(agentId, groupCounter);
123
- }
124
- groupCounter++;
125
- }
126
- // Check 2: Traverse direct edges in order to find fan-out patterns
127
- // Build a simple execution order by following edges from starting nodes
128
- const visited = new Set();
129
- const queue = [...this.startingNodes];
130
- while (queue.length > 0) {
131
- const current = queue.shift();
132
- if (visited.has(current))
133
- continue;
134
- visited.add(current);
135
- // Find direct edges from this node
136
- for (const edge of this.directEdges) {
137
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
138
- if (!sources.includes(current))
139
- continue;
140
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
141
- // Fan-out: multiple destinations = parallel group
142
- if (destinations.length > 1) {
143
- for (const dest of destinations) {
144
- // Only set if not already in a group (first group wins)
145
- if (!this.agentParallelGroups.has(dest)) {
146
- this.agentParallelGroups.set(dest, groupCounter);
147
- }
148
- if (!visited.has(dest)) {
149
- queue.push(dest);
150
- }
151
- }
152
- groupCounter++;
153
- }
154
- else {
155
- // Single destination - add to queue for traversal
156
- for (const dest of destinations) {
157
- if (!visited.has(dest)) {
158
- queue.push(dest);
159
- }
160
- }
161
- }
162
- }
163
- // Also follow handoff edges for traversal (but they don't create parallel groups)
164
- for (const edge of this.handoffEdges) {
165
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
166
- if (!sources.includes(current))
167
- continue;
168
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
169
- for (const dest of destinations) {
170
- if (!visited.has(dest)) {
171
- queue.push(dest);
172
- }
173
- }
174
- }
175
- }
176
- }
177
- /**
178
- * Get the parallel group ID for an agent, if any.
179
- * Returns undefined if the agent is not part of a parallel group.
180
- * Group IDs are incrementing numbers reflecting execution order.
181
- */
182
- getParallelGroupId(agentId) {
183
- return this.agentParallelGroups.get(agentId);
184
- }
185
- /**
186
- * Returns the ID of the last agent that produced output.
187
- * Used by auto-continuation to determine which agent's context to preserve
188
- * when a response is truncated after an agent handoff.
189
- */
190
- getLastActiveAgentId() {
191
- return this.lastActiveAgentId;
192
- }
193
- /**
194
- * Override to indicate this is a multi-agent graph.
195
- * Enables agentId to be included in RunStep for frontend agent labeling.
196
- */
197
- isMultiAgentGraph() {
198
- return true;
199
- }
200
- /**
201
- * Override base class method to provide parallel group IDs for run steps.
202
- */
203
- getParallelGroupIdForAgent(agentId) {
204
- return this.agentParallelGroups.get(agentId);
205
- }
206
- /**
207
- * Create handoff tools for agents based on handoff edges only
208
- */
209
- createHandoffTools() {
210
- // Group handoff edges by source agent(s)
211
- const handoffsByAgent = new Map();
212
- // Only process handoff edges for tool creation
213
- for (const edge of this.handoffEdges) {
214
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
215
- sources.forEach((source) => {
216
- if (!handoffsByAgent.has(source)) {
217
- handoffsByAgent.set(source, []);
218
- }
219
- handoffsByAgent.get(source).push(edge);
220
- });
221
- }
222
- // Create handoff tools for each agent
223
- for (const [agentId, edges] of handoffsByAgent) {
224
- const agentContext = this.agentContexts.get(agentId);
225
- if (!agentContext)
226
- continue;
227
- // Create handoff tools for this agent's outgoing edges
228
- const handoffTools = [];
229
- const sourceAgentName = agentContext.name ?? agentId;
230
- for (const edge of edges) {
231
- handoffTools.push(...this.createHandoffToolsForEdge(edge, agentId, sourceAgentName));
232
- }
233
- if (!agentContext.graphTools) {
234
- agentContext.graphTools = [];
235
- }
236
- agentContext.graphTools.push(...handoffTools);
237
- }
238
- }
239
- /**
240
- * Create handoff tools for an edge (handles multiple destinations)
241
- * @param edge - The graph edge defining the handoff
242
- * @param sourceAgentId - The ID of the agent that will perform the handoff
243
- * @param sourceAgentName - The human-readable name of the source agent
244
- */
245
- createHandoffToolsForEdge(edge, sourceAgentId, sourceAgentName) {
246
- const tools = [];
247
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
248
- /** If there's a condition, create a single conditional handoff tool */
249
- if (edge.condition != null) {
250
- const toolName = 'conditional_transfer';
251
- const toolDescription = edge.description ?? 'Conditionally transfer control based on state';
252
- /** Check if we have a prompt for handoff input */
253
- const hasHandoffInput = edge.prompt != null && typeof edge.prompt === 'string';
254
- const handoffInputDescription = hasHandoffInput ? edge.prompt : undefined;
255
- const promptKey = edge.promptKey ?? 'instructions';
256
- tools.push(tool(async (rawInput, config) => {
257
- const input = rawInput;
258
- const state = getCurrentTaskInput();
259
- const toolCallId = config?.toolCall?.id ??
260
- 'unknown';
261
- /** Evaluated condition */
262
- const result = edge.condition(state);
263
- let destination;
264
- if (typeof result === 'boolean') {
265
- /** If true, use first destination; if false, don't transfer */
266
- if (!result)
267
- return null;
268
- destination = destinations[0];
269
- }
270
- else if (typeof result === 'string') {
271
- destination = result;
272
- }
273
- else {
274
- /** Array of destinations - for now, use the first */
275
- destination = Array.isArray(result) ? result[0] : destinations[0];
276
- }
277
- let content = `Conditionally transferred to ${destination}`;
278
- if (hasHandoffInput &&
279
- promptKey in input &&
280
- input[promptKey] != null) {
281
- content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
282
- }
283
- const toolMessage = new ToolMessage({
284
- content,
285
- name: toolName,
286
- tool_call_id: toolCallId,
287
- additional_kwargs: {
288
- /** Store destination for programmatic access in handoff detection */
289
- handoff_destination: destination,
290
- /** Store source agent name for receiving agent to know who handed off */
291
- handoff_source_name: sourceAgentName,
292
- },
293
- });
294
- return new Command({
295
- goto: destination,
296
- update: { messages: state.messages.concat(toolMessage) },
297
- graph: Command.PARENT,
298
- });
299
- }, {
300
- name: toolName,
301
- schema: hasHandoffInput
302
- ? {
303
- type: 'object',
304
- properties: {
305
- [promptKey]: {
306
- type: 'string',
307
- description: handoffInputDescription,
308
- },
309
- },
310
- required: [],
311
- }
312
- : { type: 'object', properties: {}, required: [] },
313
- description: toolDescription,
314
- }));
315
- }
316
- else {
317
- /** Create individual tools for each destination */
318
- for (const destination of destinations) {
319
- const toolName = `${Constants.LC_TRANSFER_TO_}${destination}`;
320
- const destContext = this.agentContexts.get(destination);
321
- const toolDescription = edge.description ??
322
- this.buildDefaultHandoffDescription(destContext, destination);
323
- /** Check if we have a prompt for handoff input */
324
- const hasHandoffInput = edge.prompt != null && typeof edge.prompt === 'string';
325
- const handoffInputDescription = hasHandoffInput
326
- ? edge.prompt
327
- : undefined;
328
- const promptKey = edge.promptKey ?? 'instructions';
329
- tools.push(tool(async (rawInput, config) => {
330
- const input = rawInput;
331
- const toolCallId = config?.toolCall?.id ??
332
- 'unknown';
333
- let content = `Successfully transferred to ${destination}`;
334
- if (hasHandoffInput &&
335
- promptKey in input &&
336
- input[promptKey] != null) {
337
- content += `\n\n${promptKey.charAt(0).toUpperCase() + promptKey.slice(1)}: ${input[promptKey]}`;
338
- }
339
- const toolMessage = new ToolMessage({
340
- content,
341
- name: toolName,
342
- tool_call_id: toolCallId,
343
- additional_kwargs: {
344
- /** Store source agent name for receiving agent to know who handed off */
345
- handoff_source_name: sourceAgentName,
346
- },
347
- });
348
- const state = getCurrentTaskInput();
349
- /**
350
- * For parallel handoff support:
351
- * Build messages that include ONLY this tool call's context.
352
- * This prevents errors when LLM calls multiple transfers simultaneously -
353
- * each destination gets a valid AIMessage with matching tool_call and tool_result.
354
- *
355
- * Strategy:
356
- * 1. Find the AIMessage containing this tool call
357
- * 2. Create a filtered AIMessage with ONLY this tool_call
358
- * 3. Include all messages before the AIMessage plus the filtered pair
359
- */
360
- const messages = state.messages;
361
- let filteredMessages = messages;
362
- let aiMessageIndex = -1;
363
- /** Find the AIMessage containing this tool call */
364
- for (let i = messages.length - 1; i >= 0; i--) {
365
- const msg = messages[i];
366
- if (msg.getType() === 'ai') {
367
- const aiMsg = msg;
368
- const hasThisCall = aiMsg.tool_calls?.some((tc) => tc.id === toolCallId);
369
- if (hasThisCall === true) {
370
- aiMessageIndex = i;
371
- break;
372
- }
373
- }
374
- }
375
- if (aiMessageIndex >= 0) {
376
- const originalAiMsg = messages[aiMessageIndex];
377
- const thisToolCall = originalAiMsg.tool_calls?.find((tc) => tc.id === toolCallId);
378
- if (thisToolCall != null &&
379
- (originalAiMsg.tool_calls?.length ?? 0) > 1) {
380
- /**
381
- * Multiple tool calls - create filtered AIMessage with ONLY this call.
382
- * This ensures valid message structure for parallel handoffs.
383
- */
384
- const filteredAiMsg = new AIMessage({
385
- content: originalAiMsg.content,
386
- tool_calls: [thisToolCall],
387
- id: originalAiMsg.id,
388
- });
389
- filteredMessages = [
390
- ...messages.slice(0, aiMessageIndex),
391
- filteredAiMsg,
392
- toolMessage,
393
- ];
394
- }
395
- else {
396
- /** Single tool call - use messages as-is */
397
- filteredMessages = messages.concat(toolMessage);
398
- }
399
- }
400
- else {
401
- /** Fallback - append tool message */
402
- filteredMessages = messages.concat(toolMessage);
403
- }
404
- return new Command({
405
- goto: destination,
406
- update: { messages: filteredMessages },
407
- graph: Command.PARENT,
408
- });
409
- }, {
410
- name: toolName,
411
- schema: hasHandoffInput
412
- ? {
413
- type: 'object',
414
- properties: {
415
- [promptKey]: {
416
- type: 'string',
417
- description: handoffInputDescription,
418
- },
419
- },
420
- required: [],
421
- }
422
- : { type: 'object', properties: {}, required: [] },
423
- description: toolDescription,
424
- }));
425
- }
426
- }
427
- return tools;
428
- }
429
- /**
430
- * Builds a meaningful default description for a handoff tool when no explicit
431
- * edge.description is provided. Uses the destination agent's name and description
432
- * so the LLM can make informed routing decisions.
433
- * @param destContext - AgentContext of the destination agent (may be undefined)
434
- * @param destinationId - Raw agent ID (fallback when context unavailable)
435
- */
436
- buildDefaultHandoffDescription(destContext, destinationId) {
437
- const displayName = destContext?.name ?? destinationId;
438
- const agentDescription = destContext?.description;
439
- if (agentDescription != null && agentDescription !== '') {
440
- return `Transfer to "${displayName}": ${agentDescription}`;
441
- }
442
- return `Transfer control to "${displayName}"`;
443
- }
444
- /**
445
- * Create a complete agent subgraph (similar to createReactAgent)
446
- */
447
- createAgentSubgraph(agentId) {
448
- /** This is essentially the same as `createAgentNode` from `StandardGraph` */
449
- return this.createAgentNode(agentId);
450
- }
451
- /**
452
- * Detects if the current agent is receiving a handoff and processes the messages accordingly.
453
- * Returns filtered messages with the transfer tool call/message removed, plus any instructions,
454
- * source agent, and parallel sibling information extracted from the transfer.
455
- *
456
- * Supports both single handoffs (last message is the transfer) and parallel handoffs
457
- * (multiple transfer ToolMessages, need to find the one targeting this agent).
458
- *
459
- * @param messages - Current state messages
460
- * @param agentId - The agent ID to check for handoff reception
461
- * @returns Object with filtered messages, extracted instructions, source agent, and parallel siblings
462
- */
463
- processHandoffReception(messages, agentId) {
464
- if (messages.length === 0)
465
- return null;
466
- /**
467
- * Search for a transfer ToolMessage targeting this agent.
468
- * For parallel handoffs, multiple transfer messages may exist - find ours.
469
- * Search backwards from the end to find the most recent transfer to this agent.
470
- */
471
- let toolMessage = null;
472
- let toolMessageIndex = -1;
473
- for (let i = messages.length - 1; i >= 0; i--) {
474
- const msg = messages[i];
475
- if (msg.getType() !== 'tool')
476
- continue;
477
- const candidateMsg = msg;
478
- const toolName = candidateMsg.name;
479
- if (typeof toolName !== 'string')
480
- continue;
481
- /** Check for standard transfer pattern */
482
- const isTransferMessage = toolName.startsWith(Constants.LC_TRANSFER_TO_);
483
- const isConditionalTransfer = toolName === 'conditional_transfer';
484
- if (!isTransferMessage && !isConditionalTransfer)
485
- continue;
486
- /** Extract destination from tool name or additional_kwargs */
487
- let destinationAgent = null;
488
- if (isTransferMessage) {
489
- destinationAgent = toolName.replace(Constants.LC_TRANSFER_TO_, '');
490
- }
491
- else if (isConditionalTransfer) {
492
- const handoffDest = candidateMsg.additional_kwargs.handoff_destination;
493
- destinationAgent = typeof handoffDest === 'string' ? handoffDest : null;
494
- }
495
- /** Check if this transfer targets our agent */
496
- if (destinationAgent === agentId) {
497
- toolMessage = candidateMsg;
498
- toolMessageIndex = i;
499
- break;
500
- }
501
- }
502
- /** No transfer targeting this agent found */
503
- if (toolMessage === null || toolMessageIndex < 0)
504
- return null;
505
- /** Extract instructions from the ToolMessage content */
506
- const contentStr = typeof toolMessage.content === 'string'
507
- ? toolMessage.content
508
- : JSON.stringify(toolMessage.content);
509
- const instructionsMatch = contentStr.match(HANDOFF_INSTRUCTIONS_PATTERN);
510
- const instructions = instructionsMatch?.[1]?.trim() ?? null;
511
- /** Extract source agent name from additional_kwargs */
512
- const handoffSourceName = toolMessage.additional_kwargs.handoff_source_name;
513
- const sourceAgentName = typeof handoffSourceName === 'string' ? handoffSourceName : null;
514
- /** Extract parallel siblings (set by ToolNode for parallel handoffs) */
515
- const rawSiblings = toolMessage.additional_kwargs.handoff_parallel_siblings;
516
- const siblingIds = Array.isArray(rawSiblings)
517
- ? rawSiblings.filter((s) => typeof s === 'string')
518
- : [];
519
- /** Convert IDs to display names */
520
- const parallelSiblings = siblingIds.map((id) => {
521
- const ctx = this.agentContexts.get(id);
522
- return ctx?.name ?? id;
523
- });
524
- /** Get the tool_call_id to find and filter the AI message's tool call */
525
- const toolCallId = toolMessage.tool_call_id;
526
- /**
527
- * Collect all transfer tool_call_ids to filter out.
528
- * For parallel handoffs, we filter ALL transfer messages (not just ours)
529
- * to give the receiving agent a clean context without handoff noise.
530
- */
531
- const transferToolCallIds = new Set([toolCallId]);
532
- for (const msg of messages) {
533
- if (msg.getType() !== 'tool')
534
- continue;
535
- const tm = msg;
536
- const tName = tm.name;
537
- if (typeof tName !== 'string')
538
- continue;
539
- if (tName.startsWith(Constants.LC_TRANSFER_TO_) ||
540
- tName === 'conditional_transfer') {
541
- transferToolCallIds.add(tm.tool_call_id);
542
- }
543
- }
544
- /** Filter out all transfer messages */
545
- const filteredMessages = [];
546
- for (let i = 0; i < messages.length; i++) {
547
- const msg = messages[i];
548
- const msgType = msg.getType();
549
- /** Skip transfer ToolMessages */
550
- if (msgType === 'tool') {
551
- const tm = msg;
552
- if (transferToolCallIds.has(tm.tool_call_id)) {
553
- continue;
554
- }
555
- }
556
- if (msgType === 'ai') {
557
- /** Check if this AI message contains any transfer tool calls */
558
- const aiMsg = msg;
559
- const toolCalls = aiMsg.tool_calls;
560
- if (toolCalls && toolCalls.length > 0) {
561
- /** Filter out all transfer tool calls */
562
- const remainingToolCalls = toolCalls.filter((tc) => tc.id == null || !transferToolCallIds.has(tc.id));
563
- const hasTransferCalls = remainingToolCalls.length < toolCalls.length;
564
- if (hasTransferCalls) {
565
- if (remainingToolCalls.length > 0 ||
566
- (typeof aiMsg.content === 'string' && aiMsg.content.trim())) {
567
- /** Keep the message but without transfer tool calls */
568
- const filteredAiMsg = new AIMessage({
569
- content: aiMsg.content,
570
- tool_calls: remainingToolCalls,
571
- id: aiMsg.id,
572
- });
573
- filteredMessages.push(filteredAiMsg);
574
- }
575
- /** If no remaining content or tool calls, skip this message entirely */
576
- continue;
577
- }
578
- }
579
- }
580
- /** Keep all other messages */
581
- filteredMessages.push(msg);
582
- }
583
- /**
584
- * Compact content_tool messages to reduce handoff context bloat.
585
- *
586
- * 1. ToolMessages: Verbose outputs (read with line-numbered code, write confirmations)
587
- * are summarized to their first line, preserving file awareness without full code.
588
- * 2. AIMessages: content_tool tool_calls with large args (write/edit with full file content)
589
- * have their args compacted to remove the bulk content field.
590
- */
591
- const compactedMessages = filteredMessages.map((msg) => {
592
- const msgType = msg.getType();
593
- /** Compact content_tool ToolMessage outputs */
594
- if (msgType === 'tool') {
595
- const toolMsg = msg;
596
- if (toolMsg.name !== 'content_tool')
597
- return msg;
598
- const content = typeof toolMsg.content === 'string'
599
- ? toolMsg.content
600
- : Array.isArray(toolMsg.content)
601
- ? toolMsg.content
602
- .filter((b) => typeof b === 'object' &&
603
- 'text' in b &&
604
- typeof b.text === 'string')
605
- .map((b) => b.text)
606
- .join('\n')
607
- : JSON.stringify(toolMsg.content);
608
- if (content.length > 500) {
609
- const firstLine = content.split('\n')[0];
610
- return new ToolMessage({
611
- content: firstLine + ' [content summarized for handoff]',
612
- name: toolMsg.name,
613
- tool_call_id: toolMsg.tool_call_id,
614
- status: toolMsg.status,
615
- });
616
- }
617
- return msg;
618
- }
619
- /** Compact content_tool AI message args (remove large content/old_str/new_str fields) */
620
- if (msgType === 'ai') {
621
- const aiMsg = msg;
622
- const toolCalls = aiMsg.tool_calls;
623
- if (!toolCalls || toolCalls.length === 0)
624
- return msg;
625
- const hasLargeContentToolArgs = toolCalls.some((tc) => tc.name === 'content_tool' &&
626
- typeof tc.args === 'object' &&
627
- typeof tc.args.content === 'string' &&
628
- tc.args.content.length >
629
- 200);
630
- if (hasLargeContentToolArgs) {
631
- const compactedToolCalls = toolCalls.map((tc) => {
632
- if (tc.name !== 'content_tool' || typeof tc.args !== 'object') {
633
- return tc;
634
- }
635
- const args = tc.args;
636
- const compacted = { action: args.action };
637
- if (args.content_id != null)
638
- compacted.content_id = args.content_id;
639
- if (args.name != null)
640
- compacted.name = args.name;
641
- if (args.content != null)
642
- compacted.content = '[content removed for handoff]';
643
- if (args.old_str != null)
644
- compacted.old_str = '[removed for handoff]';
645
- if (args.new_str != null)
646
- compacted.new_str = '[removed for handoff]';
647
- if (args.pattern != null)
648
- compacted.pattern = args.pattern;
649
- return { ...tc, args: compacted };
650
- });
651
- return new AIMessage({
652
- content: aiMsg.content,
653
- tool_calls: compactedToolCalls,
654
- id: aiMsg.id,
655
- });
656
- }
657
- }
658
- return msg;
659
- });
660
- return {
661
- filteredMessages: compactedMessages,
662
- instructions,
663
- sourceAgentName,
664
- parallelSiblings,
665
- };
666
- }
667
- /**
668
- * Create the multi-agent workflow with dynamic handoffs
669
- */
670
- createWorkflow() {
671
- const StateAnnotation = Annotation.Root({
672
- messages: Annotation({
673
- reducer: (a, b) => {
674
- if (!a.length) {
675
- this.startIndex = a.length + b.length;
676
- }
677
- const result = messagesStateReducer(a, b);
678
- this.messages = result;
679
- return result;
680
- },
681
- default: () => [],
682
- }),
683
- /** Channel for passing filtered messages to agents when excludeResults is true */
684
- agentMessages: Annotation({
685
- /** Replaces state entirely */
686
- reducer: (a, b) => b,
687
- default: () => [],
688
- }),
689
- });
690
- const builder = new StateGraph(StateAnnotation);
691
- // Add all agents as complete subgraphs
692
- for (const [agentId] of this.agentContexts) {
693
- // Get all possible destinations for this agent
694
- const handoffDestinations = new Set();
695
- const directDestinations = new Set();
696
- // Check handoff edges for destinations
697
- for (const edge of this.handoffEdges) {
698
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
699
- if (sources.includes(agentId) === true) {
700
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
701
- dests.forEach((dest) => handoffDestinations.add(dest));
702
- }
703
- }
704
- // Check direct edges for destinations
705
- for (const edge of this.directEdges) {
706
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
707
- if (sources.includes(agentId) === true) {
708
- const dests = Array.isArray(edge.to) ? edge.to : [edge.to];
709
- dests.forEach((dest) => directDestinations.add(dest));
710
- }
711
- }
712
- /** Check if this agent has BOTH handoff and direct edges */
713
- const hasHandoffEdges = handoffDestinations.size > 0;
714
- const hasDirectEdges = directDestinations.size > 0;
715
- const needsCommandRouting = hasHandoffEdges && hasDirectEdges;
716
- /** Collect all possible destinations for this agent */
717
- const allDestinations = new Set([
718
- ...handoffDestinations,
719
- ...directDestinations,
720
- ]);
721
- if (handoffDestinations.size > 0 || directDestinations.size === 0) {
722
- allDestinations.add(END);
723
- }
724
- /** Agent subgraph (includes agent + tools) */
725
- const agentSubgraph = this.createAgentSubgraph(agentId);
726
- /** Wrapper function that handles agentMessages channel, handoff reception, and conditional routing */
727
- const agentWrapper = async (state, config) => {
728
- let result;
729
- /**
730
- * Check if this agent is receiving a handoff.
731
- * If so, filter out the transfer messages and inject instructions as preamble.
732
- * This prevents the receiving agent from seeing the transfer as "completed work"
733
- * and prematurely producing an end token.
734
- */
735
- const handoffContext = this.processHandoffReception(state.messages, agentId);
736
- if (handoffContext !== null) {
737
- const { filteredMessages, instructions, sourceAgentName, parallelSiblings, } = handoffContext;
738
- /**
739
- * Set handoff context on the receiving agent.
740
- * Uses pre-computed graph position for depth and parallel info.
741
- */
742
- const agentContext = this.agentContexts.get(agentId);
743
- if (agentContext &&
744
- sourceAgentName != null &&
745
- sourceAgentName !== '') {
746
- agentContext.setHandoffContext(sourceAgentName, parallelSiblings);
747
- }
748
- /** Build messages for the receiving agent */
749
- let messagesForAgent = filteredMessages;
750
- /**
751
- * If there are instructions, inject them as a HumanMessage to
752
- * ground the receiving agent.
753
- *
754
- * When the last filtered message is a ToolMessage (e.g. from a
755
- * non-handoff tool the router called before handing off), a
756
- * synthetic AIMessage is inserted first to satisfy the
757
- * tool → assistant role ordering required by chat APIs. Without
758
- * this bridge, appending a HumanMessage directly after a
759
- * ToolMessage causes "400 Unexpected role 'user' after role
760
- * 'tool'" errors (see issue #54).
761
- */
762
- const hasInstructions = instructions !== null && instructions !== '';
763
- if (hasInstructions) {
764
- const lastMsg = filteredMessages.length > 0
765
- ? filteredMessages[filteredMessages.length - 1]
766
- : null;
767
- if (lastMsg != null && lastMsg.getType() === 'tool') {
768
- messagesForAgent = [
769
- ...filteredMessages,
770
- new AIMessage(`[Processed tool result and transferring to ${agentId}]`),
771
- new HumanMessage(instructions),
772
- ];
773
- }
774
- else {
775
- messagesForAgent = [
776
- ...filteredMessages,
777
- new HumanMessage(instructions),
778
- ];
779
- }
780
- }
781
- /** Update token map if we have a token counter */
782
- if (agentContext?.tokenCounter && hasInstructions) {
783
- const freshTokenMap = {};
784
- for (let i = 0; i < Math.min(filteredMessages.length, this.startIndex); i++) {
785
- const tokenCount = agentContext.indexTokenCountMap[i];
786
- if (tokenCount !== undefined) {
787
- freshTokenMap[i] = tokenCount;
788
- }
789
- }
790
- /** Add tokens for the bridge AIMessage + instructions HumanMessage */
791
- for (let i = filteredMessages.length; i < messagesForAgent.length; i++) {
792
- freshTokenMap[i] = agentContext.tokenCounter(messagesForAgent[i]);
793
- }
794
- agentContext.updateTokenMapWithInstructions(freshTokenMap);
795
- }
796
- const transformedState = {
797
- ...state,
798
- messages: messagesForAgent,
799
- };
800
- result = await agentSubgraph.invoke(transformedState, config);
801
- result = {
802
- ...result,
803
- agentMessages: [],
804
- };
805
- }
806
- else if (state.agentMessages != null &&
807
- state.agentMessages.length > 0) {
808
- /**
809
- * When using agentMessages (excludeResults=true), we need to update
810
- * the token map to account for the new prompt message
811
- */
812
- const agentContext = this.agentContexts.get(agentId);
813
- if (agentContext && agentContext.tokenCounter) {
814
- /** The agentMessages contains:
815
- * 1. Filtered messages (0 to startIndex) - already have token counts
816
- * 2. New prompt message - needs token counting
817
- */
818
- const freshTokenMap = {};
819
- /** Copy existing token counts for filtered messages (0 to startIndex) */
820
- for (let i = 0; i < this.startIndex; i++) {
821
- const tokenCount = agentContext.indexTokenCountMap[i];
822
- if (tokenCount !== undefined) {
823
- freshTokenMap[i] = tokenCount;
824
- }
825
- }
826
- /** Calculate tokens only for the new prompt message (last message) */
827
- const promptMessageIndex = state.agentMessages.length - 1;
828
- if (promptMessageIndex >= this.startIndex) {
829
- const promptMessage = state.agentMessages[promptMessageIndex];
830
- freshTokenMap[promptMessageIndex] =
831
- agentContext.tokenCounter(promptMessage);
832
- }
833
- /** Update the agent's token map with instructions added */
834
- agentContext.updateTokenMapWithInstructions(freshTokenMap);
835
- }
836
- /** Temporary state with messages replaced by `agentMessages` */
837
- const transformedState = {
838
- ...state,
839
- messages: state.agentMessages,
840
- };
841
- result = await agentSubgraph.invoke(transformedState, config);
842
- result = {
843
- ...result,
844
- /** Clear agentMessages for next agent */
845
- agentMessages: [],
846
- };
847
- }
848
- else {
849
- result = await agentSubgraph.invoke(state, config);
850
- }
851
- /** Track the last agent that produced output for continuation support */
852
- this.lastActiveAgentId = agentId;
853
- /** If agent has both handoff and direct edges, use Command for exclusive routing */
854
- if (needsCommandRouting) {
855
- /** Check if a handoff occurred */
856
- const lastMessage = result.messages[result.messages.length - 1];
857
- if (lastMessage != null &&
858
- lastMessage.getType() === 'tool' &&
859
- typeof lastMessage.name === 'string' &&
860
- lastMessage.name.startsWith(Constants.LC_TRANSFER_TO_)) {
861
- /** Handoff occurred - extract destination and navigate there exclusively */
862
- const handoffDest = lastMessage.name.replace(Constants.LC_TRANSFER_TO_, '');
863
- /** Validate destination agent exists */
864
- if (!this.agentContexts.has(handoffDest)) {
865
- const availableAgents = Array.from(this.agentContexts.keys()).join(', ');
866
- console.error(`[MultiAgentGraph] Handoff to non-existent agent "${handoffDest}". Available: ${availableAgents}`);
867
- /** Return error to model so it can self-correct */
868
- const errorMsg = new ToolMessage({
869
- content: `Transfer failed: agent "${handoffDest}" does not exist. Available agents: ${availableAgents}. Please choose a valid agent to transfer to.`,
870
- tool_call_id: lastMessage.tool_call_id,
871
- name: lastMessage.name,
872
- });
873
- errorMsg.status = 'error';
874
- return {
875
- messages: [...result.messages, errorMsg],
876
- };
877
- }
878
- /** Pre-handoff context compaction: if receiving agent has smaller budget */
879
- const receiverContext = this.agentContexts.get(handoffDest);
880
- const senderContext = this.agentContexts.get(agentId);
881
- if (receiverContext?.maxContextTokens != null &&
882
- senderContext?.tokenCounter != null &&
883
- receiverContext.maxContextTokens > 0) {
884
- let currentSize = 0;
885
- for (const msg of result.messages) {
886
- currentSize += senderContext.tokenCounter(msg);
887
- }
888
- const receiverBudget = receiverContext.maxContextTokens;
889
- if (currentSize > receiverBudget * 0.7) {
890
- console.warn(`[MultiAgentGraph] Pre-handoff compaction: context (${currentSize} tokens) exceeds ` +
891
- `70% of receiver "${handoffDest}" budget (${receiverBudget} tokens)`);
892
- /** Generate handoff briefing */
893
- const senderName = senderContext.name ?? agentId;
894
- if (senderContext.summarizeCallback) {
895
- try {
896
- const briefingResult = await summarize(result.messages, async (prompt, _maxTokens) => senderContext.summarizeCallback([new HumanMessage(prompt)]), {
897
- tokenCounter: senderContext.tokenCounter,
898
- summaryBudget: Math.floor(receiverBudget * 0.2),
899
- isMultiAgent: true,
900
- agentWorkflowState: {
901
- currentAgentId: handoffDest,
902
- agentChain: [agentId, handoffDest],
903
- pendingAgents: [],
904
- },
905
- });
906
- const briefingMsg = new SystemMessage(`[Handoff Briefing from "${senderName}"]\n${briefingResult.summary}`);
907
- /** Replace messages with briefing + last 3 messages */
908
- const keepCount = Math.min(3, result.messages.length);
909
- result = {
910
- ...result,
911
- messages: [
912
- briefingMsg,
913
- ...result.messages.slice(result.messages.length - keepCount),
914
- ],
915
- };
916
- console.info(`[MultiAgentGraph] Pre-handoff compaction complete: ${currentSize} tokens → briefing + ${keepCount} messages`);
917
- }
918
- catch (compactErr) {
919
- console.error('[MultiAgentGraph] Pre-handoff compaction failed:', compactErr);
920
- /** Continue without compaction — let receiver handle the overflow */
921
- }
922
- }
923
- else {
924
- /** No summary callback — use emergency summary */
925
- const emergencySummary = createEmergencySummary(result.messages);
926
- const briefingMsg = new SystemMessage(`[Handoff Briefing from "${senderName}" — Emergency]\n${emergencySummary}`);
927
- const keepCount = Math.min(3, result.messages.length);
928
- result = {
929
- ...result,
930
- messages: [
931
- briefingMsg,
932
- ...result.messages.slice(result.messages.length - keepCount),
933
- ],
934
- };
935
- }
936
- }
937
- }
938
- return new Command({
939
- update: result,
940
- goto: handoffDest,
941
- });
942
- }
943
- else {
944
- /** No handoff - proceed with direct edges */
945
- const directDests = Array.from(directDestinations);
946
- if (directDests.length === 1) {
947
- return new Command({
948
- update: result,
949
- goto: directDests[0],
950
- });
951
- }
952
- else if (directDests.length > 1) {
953
- /** Multiple direct destinations - they'll run in parallel */
954
- return new Command({
955
- update: result,
956
- goto: directDests,
957
- });
958
- }
959
- }
960
- }
961
- /** No special routing needed - return state normally */
962
- return result;
963
- };
964
- /** Wrapped agent as a node with its possible destinations */
965
- builder.addNode(agentId, agentWrapper, {
966
- ends: Array.from(allDestinations),
967
- });
968
- }
969
- // Add starting edges for all starting nodes
970
- for (const startNode of this.startingNodes) {
971
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
972
- /** @ts-ignore */
973
- builder.addEdge(START, startNode);
974
- }
975
- /**
976
- * Add direct edges for automatic transitions
977
- * Group edges by destination to handle fan-in scenarios
978
- */
979
- const edgesByDestination = new Map();
980
- for (const edge of this.directEdges) {
981
- const destinations = Array.isArray(edge.to) ? edge.to : [edge.to];
982
- for (const destination of destinations) {
983
- if (!edgesByDestination.has(destination)) {
984
- edgesByDestination.set(destination, []);
985
- }
986
- edgesByDestination.get(destination).push(edge);
987
- }
988
- }
989
- for (const [destination, edges] of edgesByDestination) {
990
- /** Checks if this is a fan-in scenario with prompt instructions */
991
- const edgesWithPrompt = edges.filter((edge) => edge.prompt != null && edge.prompt !== '');
992
- if (edgesWithPrompt.length > 0) {
993
- /**
994
- * Single wrapper node for destination (Fan-in with prompt)
995
- */
996
- const wrapperNodeId = `fan_in_${destination}_prompt`;
997
- /**
998
- * First edge's `prompt`
999
- * (they should all be the same for fan-in)
1000
- */
1001
- const prompt = edgesWithPrompt[0].prompt;
1002
- /**
1003
- * First edge's `excludeResults` flag
1004
- * (they should all be the same for fan-in)
1005
- */
1006
- const excludeResults = edgesWithPrompt[0].excludeResults;
1007
- builder.addNode(wrapperNodeId, async (state) => {
1008
- let promptText;
1009
- let effectiveExcludeResults = excludeResults;
1010
- if (typeof prompt === 'function') {
1011
- promptText = await prompt(state.messages, this.startIndex);
1012
- }
1013
- else if (prompt != null) {
1014
- if (prompt.includes('{results}')) {
1015
- const resultsMessages = state.messages.slice(this.startIndex);
1016
- const resultsString = getBufferString(resultsMessages);
1017
- const promptTemplate = PromptTemplate.fromTemplate(prompt);
1018
- const result = await promptTemplate.invoke({
1019
- results: resultsString,
1020
- });
1021
- promptText = result.value;
1022
- effectiveExcludeResults =
1023
- excludeResults !== false && promptText !== '';
1024
- }
1025
- else {
1026
- promptText = prompt;
1027
- }
1028
- }
1029
- if (promptText != null && promptText !== '') {
1030
- if (effectiveExcludeResults == null ||
1031
- effectiveExcludeResults === false) {
1032
- return {
1033
- messages: [new HumanMessage(promptText)],
1034
- };
1035
- }
1036
- /** When `excludeResults` is true, use agentMessages channel
1037
- * to pass filtered messages + prompt to the destination agent
1038
- */
1039
- const filteredMessages = state.messages.slice(0, this.startIndex);
1040
- return {
1041
- messages: [new HumanMessage(promptText)],
1042
- agentMessages: messagesStateReducer(filteredMessages, [
1043
- new HumanMessage(promptText),
1044
- ]),
1045
- };
1046
- }
1047
- /** No prompt needed, return empty update */
1048
- return {};
1049
- });
1050
- /** Add edges from all sources to the wrapper, then wrapper to destination */
1051
- for (const edge of edges) {
1052
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1053
- for (const source of sources) {
1054
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1055
- /** @ts-ignore */
1056
- builder.addEdge(source, wrapperNodeId);
1057
- }
1058
- }
1059
- /** Single edge from wrapper to destination */
1060
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1061
- /** @ts-ignore */
1062
- builder.addEdge(wrapperNodeId, destination);
1063
- }
1064
- else {
1065
- /** No prompt instructions, add direct edges (skip if source uses Command routing) */
1066
- for (const edge of edges) {
1067
- const sources = Array.isArray(edge.from) ? edge.from : [edge.from];
1068
- for (const source of sources) {
1069
- /** Check if this source node has both handoff and direct edges */
1070
- const sourceHandoffEdges = this.handoffEdges.filter((e) => {
1071
- const eSources = Array.isArray(e.from) ? e.from : [e.from];
1072
- return eSources.includes(source);
1073
- });
1074
- const sourceDirectEdges = this.directEdges.filter((e) => {
1075
- const eSources = Array.isArray(e.from) ? e.from : [e.from];
1076
- return eSources.includes(source);
1077
- });
1078
- /** Skip adding edge if source uses Command routing (has both types) */
1079
- if (sourceHandoffEdges.length > 0 && sourceDirectEdges.length > 0) {
1080
- continue;
1081
- }
1082
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1083
- /** @ts-ignore */
1084
- builder.addEdge(source, destination);
1085
- }
1086
- }
1087
- }
1088
- }
1089
- return builder.compile(this.compileOptions);
1090
- }
1091
- }
1092
- //# sourceMappingURL=MultiAgentGraph.js.map