illuma-agents 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/LICENSE +25 -21
  2. package/dist/cjs/agents/AgentContext.cjs +222 -0
  3. package/dist/cjs/agents/AgentContext.cjs.map +1 -0
  4. package/dist/cjs/common/enum.cjs +5 -4
  5. package/dist/cjs/common/enum.cjs.map +1 -1
  6. package/dist/cjs/events.cjs +7 -5
  7. package/dist/cjs/events.cjs.map +1 -1
  8. package/dist/cjs/graphs/Graph.cjs +328 -207
  9. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  10. package/dist/cjs/graphs/MultiAgentGraph.cjs +507 -0
  11. package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
  12. package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
  13. package/dist/cjs/llm/google/index.cjs.map +1 -1
  14. package/dist/cjs/llm/ollama/index.cjs.map +1 -1
  15. package/dist/cjs/llm/openai/index.cjs +35 -0
  16. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  17. package/dist/cjs/llm/openai/utils/index.cjs +3 -1
  18. package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
  19. package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
  20. package/dist/cjs/llm/providers.cjs +0 -2
  21. package/dist/cjs/llm/providers.cjs.map +1 -1
  22. package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
  23. package/dist/cjs/main.cjs +12 -1
  24. package/dist/cjs/main.cjs.map +1 -1
  25. package/dist/cjs/messages/cache.cjs +123 -0
  26. package/dist/cjs/messages/cache.cjs.map +1 -0
  27. package/dist/cjs/messages/content.cjs +53 -0
  28. package/dist/cjs/messages/content.cjs.map +1 -0
  29. package/dist/cjs/messages/format.cjs +17 -29
  30. package/dist/cjs/messages/format.cjs.map +1 -1
  31. package/dist/cjs/run.cjs +119 -74
  32. package/dist/cjs/run.cjs.map +1 -1
  33. package/dist/cjs/stream.cjs +77 -73
  34. package/dist/cjs/stream.cjs.map +1 -1
  35. package/dist/cjs/tools/Calculator.cjs +45 -0
  36. package/dist/cjs/tools/Calculator.cjs.map +1 -0
  37. package/dist/cjs/tools/CodeExecutor.cjs +22 -22
  38. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  39. package/dist/cjs/tools/ToolNode.cjs +5 -3
  40. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  41. package/dist/cjs/tools/handlers.cjs +20 -20
  42. package/dist/cjs/tools/handlers.cjs.map +1 -1
  43. package/dist/cjs/utils/events.cjs +31 -0
  44. package/dist/cjs/utils/events.cjs.map +1 -0
  45. package/dist/cjs/utils/handlers.cjs +70 -0
  46. package/dist/cjs/utils/handlers.cjs.map +1 -0
  47. package/dist/cjs/utils/tokens.cjs +54 -7
  48. package/dist/cjs/utils/tokens.cjs.map +1 -1
  49. package/dist/esm/agents/AgentContext.mjs +220 -0
  50. package/dist/esm/agents/AgentContext.mjs.map +1 -0
  51. package/dist/esm/common/enum.mjs +5 -4
  52. package/dist/esm/common/enum.mjs.map +1 -1
  53. package/dist/esm/events.mjs +7 -5
  54. package/dist/esm/events.mjs.map +1 -1
  55. package/dist/esm/graphs/Graph.mjs +330 -209
  56. package/dist/esm/graphs/Graph.mjs.map +1 -1
  57. package/dist/esm/graphs/MultiAgentGraph.mjs +505 -0
  58. package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
  59. package/dist/esm/llm/anthropic/index.mjs.map +1 -1
  60. package/dist/esm/llm/google/index.mjs.map +1 -1
  61. package/dist/esm/llm/ollama/index.mjs.map +1 -1
  62. package/dist/esm/llm/openai/index.mjs +35 -0
  63. package/dist/esm/llm/openai/index.mjs.map +1 -1
  64. package/dist/esm/llm/openai/utils/index.mjs +3 -1
  65. package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
  66. package/dist/esm/llm/openrouter/index.mjs.map +1 -1
  67. package/dist/esm/llm/providers.mjs +0 -2
  68. package/dist/esm/llm/providers.mjs.map +1 -1
  69. package/dist/esm/llm/vertexai/index.mjs.map +1 -1
  70. package/dist/esm/main.mjs +7 -2
  71. package/dist/esm/main.mjs.map +1 -1
  72. package/dist/esm/messages/cache.mjs +120 -0
  73. package/dist/esm/messages/cache.mjs.map +1 -0
  74. package/dist/esm/messages/content.mjs +51 -0
  75. package/dist/esm/messages/content.mjs.map +1 -0
  76. package/dist/esm/messages/format.mjs +18 -29
  77. package/dist/esm/messages/format.mjs.map +1 -1
  78. package/dist/esm/run.mjs +119 -74
  79. package/dist/esm/run.mjs.map +1 -1
  80. package/dist/esm/stream.mjs +77 -73
  81. package/dist/esm/stream.mjs.map +1 -1
  82. package/dist/esm/tools/Calculator.mjs +24 -0
  83. package/dist/esm/tools/Calculator.mjs.map +1 -0
  84. package/dist/esm/tools/CodeExecutor.mjs +22 -22
  85. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  86. package/dist/esm/tools/ToolNode.mjs +5 -3
  87. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  88. package/dist/esm/tools/handlers.mjs +20 -20
  89. package/dist/esm/tools/handlers.mjs.map +1 -1
  90. package/dist/esm/utils/events.mjs +29 -0
  91. package/dist/esm/utils/events.mjs.map +1 -0
  92. package/dist/esm/utils/handlers.mjs +68 -0
  93. package/dist/esm/utils/handlers.mjs.map +1 -0
  94. package/dist/esm/utils/tokens.mjs +54 -8
  95. package/dist/esm/utils/tokens.mjs.map +1 -1
  96. package/dist/types/agents/AgentContext.d.ts +94 -0
  97. package/dist/types/common/enum.d.ts +7 -5
  98. package/dist/types/events.d.ts +3 -3
  99. package/dist/types/graphs/Graph.d.ts +60 -66
  100. package/dist/types/graphs/MultiAgentGraph.d.ts +47 -0
  101. package/dist/types/graphs/index.d.ts +1 -0
  102. package/dist/types/index.d.ts +1 -0
  103. package/dist/types/llm/openai/index.d.ts +10 -0
  104. package/dist/types/messages/cache.d.ts +20 -0
  105. package/dist/types/messages/content.d.ts +7 -0
  106. package/dist/types/messages/format.d.ts +1 -7
  107. package/dist/types/messages/index.d.ts +2 -0
  108. package/dist/types/messages/reducer.d.ts +9 -0
  109. package/dist/types/run.d.ts +16 -10
  110. package/dist/types/stream.d.ts +4 -3
  111. package/dist/types/tools/Calculator.d.ts +8 -0
  112. package/dist/types/tools/ToolNode.d.ts +1 -1
  113. package/dist/types/tools/handlers.d.ts +9 -7
  114. package/dist/types/tools/search/tool.d.ts +4 -4
  115. package/dist/types/types/graph.d.ts +124 -11
  116. package/dist/types/types/llm.d.ts +13 -9
  117. package/dist/types/types/messages.d.ts +4 -0
  118. package/dist/types/types/run.d.ts +46 -8
  119. package/dist/types/types/stream.d.ts +3 -2
  120. package/dist/types/utils/events.d.ts +6 -0
  121. package/dist/types/utils/handlers.d.ts +34 -0
  122. package/dist/types/utils/index.d.ts +1 -0
  123. package/dist/types/utils/tokens.d.ts +24 -0
  124. package/package.json +162 -145
  125. package/src/agents/AgentContext.ts +323 -0
  126. package/src/common/enum.ts +177 -176
  127. package/src/events.ts +197 -191
  128. package/src/graphs/Graph.ts +1058 -846
  129. package/src/graphs/MultiAgentGraph.ts +598 -0
  130. package/src/graphs/index.ts +2 -1
  131. package/src/index.ts +25 -24
  132. package/src/llm/anthropic/index.ts +413 -413
  133. package/src/llm/google/index.ts +222 -222
  134. package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -88
  135. package/src/llm/ollama/index.ts +92 -92
  136. package/src/llm/openai/index.ts +894 -853
  137. package/src/llm/openai/utils/index.ts +920 -918
  138. package/src/llm/openrouter/index.ts +60 -60
  139. package/src/llm/providers.ts +55 -57
  140. package/src/llm/vertexai/index.ts +360 -360
  141. package/src/messages/cache.test.ts +461 -0
  142. package/src/messages/cache.ts +151 -0
  143. package/src/messages/content.test.ts +362 -0
  144. package/src/messages/content.ts +63 -0
  145. package/src/messages/format.ts +611 -625
  146. package/src/messages/formatAgentMessages.test.ts +1144 -917
  147. package/src/messages/index.ts +6 -4
  148. package/src/messages/reducer.ts +80 -0
  149. package/src/run.ts +447 -381
  150. package/src/scripts/abort.ts +157 -138
  151. package/src/scripts/ant_web_search.ts +158 -158
  152. package/src/scripts/cli.ts +172 -167
  153. package/src/scripts/cli2.ts +133 -125
  154. package/src/scripts/cli3.ts +184 -178
  155. package/src/scripts/cli4.ts +191 -184
  156. package/src/scripts/cli5.ts +191 -184
  157. package/src/scripts/code_exec.ts +213 -214
  158. package/src/scripts/code_exec_simple.ts +147 -129
  159. package/src/scripts/content.ts +138 -120
  160. package/src/scripts/handoff-test.ts +135 -0
  161. package/src/scripts/multi-agent-chain.ts +278 -0
  162. package/src/scripts/multi-agent-conditional.ts +220 -0
  163. package/src/scripts/multi-agent-document-review-chain.ts +197 -0
  164. package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
  165. package/src/scripts/multi-agent-parallel.ts +343 -0
  166. package/src/scripts/multi-agent-sequence.ts +212 -0
  167. package/src/scripts/multi-agent-supervisor.ts +364 -0
  168. package/src/scripts/multi-agent-test.ts +186 -0
  169. package/src/scripts/search.ts +146 -150
  170. package/src/scripts/simple.ts +225 -225
  171. package/src/scripts/stream.ts +140 -122
  172. package/src/scripts/test-custom-prompt-key.ts +145 -0
  173. package/src/scripts/test-handoff-input.ts +170 -0
  174. package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
  175. package/src/scripts/test-tools-before-handoff.ts +222 -0
  176. package/src/scripts/tools.ts +153 -155
  177. package/src/specs/agent-handoffs.test.ts +889 -0
  178. package/src/specs/anthropic.simple.test.ts +320 -317
  179. package/src/specs/azure.simple.test.ts +325 -316
  180. package/src/specs/openai.simple.test.ts +311 -316
  181. package/src/specs/openrouter.simple.test.ts +107 -0
  182. package/src/specs/prune.test.ts +758 -763
  183. package/src/specs/reasoning.test.ts +201 -165
  184. package/src/specs/thinking-prune.test.ts +769 -703
  185. package/src/specs/token-memoization.test.ts +39 -0
  186. package/src/stream.ts +664 -651
  187. package/src/tools/Calculator.test.ts +278 -0
  188. package/src/tools/Calculator.ts +25 -0
  189. package/src/tools/CodeExecutor.ts +220 -220
  190. package/src/tools/ToolNode.ts +170 -170
  191. package/src/tools/handlers.ts +341 -336
  192. package/src/types/graph.ts +372 -185
  193. package/src/types/llm.ts +141 -140
  194. package/src/types/messages.ts +4 -0
  195. package/src/types/run.ts +128 -89
  196. package/src/types/stream.ts +401 -400
  197. package/src/utils/events.ts +32 -0
  198. package/src/utils/handlers.ts +107 -0
  199. package/src/utils/index.ts +6 -5
  200. package/src/utils/llmConfig.ts +183 -183
  201. package/src/utils/tokens.ts +129 -70
  202. package/dist/types/scripts/abort.d.ts +0 -1
  203. package/dist/types/scripts/ant_web_search.d.ts +0 -1
  204. package/dist/types/scripts/args.d.ts +0 -7
  205. package/dist/types/scripts/caching.d.ts +0 -1
  206. package/dist/types/scripts/cli.d.ts +0 -1
  207. package/dist/types/scripts/cli2.d.ts +0 -1
  208. package/dist/types/scripts/cli3.d.ts +0 -1
  209. package/dist/types/scripts/cli4.d.ts +0 -1
  210. package/dist/types/scripts/cli5.d.ts +0 -1
  211. package/dist/types/scripts/code_exec.d.ts +0 -1
  212. package/dist/types/scripts/code_exec_files.d.ts +0 -1
  213. package/dist/types/scripts/code_exec_simple.d.ts +0 -1
  214. package/dist/types/scripts/content.d.ts +0 -1
  215. package/dist/types/scripts/empty_input.d.ts +0 -1
  216. package/dist/types/scripts/image.d.ts +0 -1
  217. package/dist/types/scripts/memory.d.ts +0 -1
  218. package/dist/types/scripts/search.d.ts +0 -1
  219. package/dist/types/scripts/simple.d.ts +0 -1
  220. package/dist/types/scripts/stream.d.ts +0 -1
  221. package/dist/types/scripts/thinking.d.ts +0 -1
  222. package/dist/types/scripts/tools.d.ts +0 -1
  223. package/dist/types/specs/spec.utils.d.ts +0 -1
  224. package/dist/types/tools/example.d.ts +0 -78
  225. package/src/tools/example.ts +0 -129
@@ -1,336 +1,341 @@
1
- /* eslint-disable no-console */
2
- // src/tools/handlers.ts
3
- import { nanoid } from 'nanoid';
4
- import { ToolMessage } from '@langchain/core/messages';
5
- import type { AnthropicWebSearchResultBlockParam } from '@/llm/anthropic/types';
6
- import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
7
- import type { Graph } from '@/graphs';
8
- import type * as t from '@/types';
9
- import {
10
- ToolCallTypes,
11
- ContentTypes,
12
- GraphEvents,
13
- StepTypes,
14
- Providers,
15
- Constants,
16
- } from '@/common';
17
- import {
18
- coerceAnthropicSearchResults,
19
- isAnthropicWebSearchResult,
20
- } from '@/tools/search/anthropic';
21
- import { formatResultsForLLM } from '@/tools/search/format';
22
- import { getMessageId } from '@/messages';
23
-
24
- export function handleToolCallChunks({
25
- graph,
26
- stepKey,
27
- toolCallChunks,
28
- }: {
29
- graph: Graph;
30
- stepKey: string;
31
- toolCallChunks: ToolCallChunk[];
32
- }): void {
33
- let prevStepId: string;
34
- let prevRunStep: t.RunStep | undefined;
35
- try {
36
- prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
37
- prevRunStep = graph.getRunStep(prevStepId);
38
- } catch {
39
- /** Edge Case: If no previous step exists, create a new message creation step */
40
- const message_id = getMessageId(stepKey, graph, true) ?? '';
41
- prevStepId = graph.dispatchRunStep(stepKey, {
42
- type: StepTypes.MESSAGE_CREATION,
43
- message_creation: {
44
- message_id,
45
- },
46
- });
47
- prevRunStep = graph.getRunStep(prevStepId);
48
- }
49
-
50
- const _stepId = graph.getStepIdByKey(stepKey, prevRunStep?.index);
51
-
52
- /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */
53
- const tool_calls: ToolCall[] | undefined =
54
- prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION
55
- ? []
56
- : undefined;
57
-
58
- /** Edge Case: `id` and `name` fields cannot be empty strings */
59
- for (const toolCallChunk of toolCallChunks) {
60
- if (toolCallChunk.name === '') {
61
- toolCallChunk.name = undefined;
62
- }
63
- if (toolCallChunk.id === '') {
64
- toolCallChunk.id = undefined;
65
- } else if (
66
- tool_calls != null &&
67
- toolCallChunk.id != null &&
68
- toolCallChunk.name != null
69
- ) {
70
- tool_calls.push({
71
- args: {},
72
- id: toolCallChunk.id,
73
- name: toolCallChunk.name,
74
- type: ToolCallTypes.TOOL_CALL,
75
- });
76
- }
77
- }
78
-
79
- let stepId: string = _stepId;
80
- const alreadyDispatched =
81
- prevRunStep?.type === StepTypes.MESSAGE_CREATION &&
82
- graph.messageStepHasToolCalls.has(prevStepId);
83
- if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {
84
- graph.dispatchMessageDelta(prevStepId, {
85
- content: [
86
- {
87
- type: ContentTypes.TEXT,
88
- text: '',
89
- tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),
90
- },
91
- ],
92
- });
93
- graph.messageStepHasToolCalls.set(prevStepId, true);
94
- stepId = graph.dispatchRunStep(stepKey, {
95
- type: StepTypes.TOOL_CALLS,
96
- tool_calls,
97
- });
98
- }
99
- graph.dispatchRunStepDelta(stepId, {
100
- type: StepTypes.TOOL_CALLS,
101
- tool_calls: toolCallChunks,
102
- });
103
- }
104
-
105
- export const handleToolCalls = (
106
- toolCalls?: ToolCall[],
107
- metadata?: Record<string, unknown>,
108
- graph?: Graph
109
- ): void => {
110
- if (!graph || !metadata) {
111
- console.warn(`Graph or metadata not found in ${event} event`);
112
- return;
113
- }
114
-
115
- if (!toolCalls) {
116
- return;
117
- }
118
-
119
- if (toolCalls.length === 0) {
120
- return;
121
- }
122
-
123
- const stepKey = graph.getStepKey(metadata);
124
-
125
- for (const tool_call of toolCalls) {
126
- const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;
127
- tool_call.id = toolCallId;
128
- if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {
129
- continue;
130
- }
131
-
132
- let prevStepId = '';
133
- let prevRunStep: t.RunStep | undefined;
134
- try {
135
- prevStepId = graph.getStepIdByKey(stepKey, graph.contentData.length - 1);
136
- prevRunStep = graph.getRunStep(prevStepId);
137
- } catch {
138
- // no previous step
139
- }
140
-
141
- const dispatchToolCallIds = (lastMessageStepId: string): void => {
142
- graph.dispatchMessageDelta(lastMessageStepId, {
143
- content: [
144
- {
145
- type: 'text',
146
- text: '',
147
- tool_call_ids: [toolCallId],
148
- },
149
- ],
150
- });
151
- };
152
- /* If the previous step exists and is a message creation */
153
- if (
154
- prevStepId &&
155
- prevRunStep &&
156
- prevRunStep.type === StepTypes.MESSAGE_CREATION
157
- ) {
158
- dispatchToolCallIds(prevStepId);
159
- graph.messageStepHasToolCalls.set(prevStepId, true);
160
- /* If the previous step doesn't exist or is not a message creation */
161
- } else if (
162
- !prevRunStep ||
163
- prevRunStep.type !== StepTypes.MESSAGE_CREATION
164
- ) {
165
- const messageId = getMessageId(stepKey, graph, true) ?? '';
166
- const stepId = graph.dispatchRunStep(stepKey, {
167
- type: StepTypes.MESSAGE_CREATION,
168
- message_creation: {
169
- message_id: messageId,
170
- },
171
- });
172
- dispatchToolCallIds(stepId);
173
- graph.messageStepHasToolCalls.set(prevStepId, true);
174
- }
175
-
176
- graph.dispatchRunStep(stepKey, {
177
- type: StepTypes.TOOL_CALLS,
178
- tool_calls: [tool_call],
179
- });
180
- }
181
- };
182
-
183
- export const toolResultTypes = new Set([
184
- // 'tool_use',
185
- // 'server_tool_use',
186
- // 'input_json_delta',
187
- 'tool_result',
188
- 'web_search_result',
189
- 'web_search_tool_result',
190
- ]);
191
-
192
- /**
193
- * Handles the result of a server tool call; in other words, a provider's built-in tool.
194
- * As of 2025-07-06, only Anthropic handles server tool calls with this pattern.
195
- */
196
- export function handleServerToolResult({
197
- content,
198
- metadata,
199
- graph,
200
- }: {
201
- content?: string | t.MessageContentComplex[];
202
- metadata?: Record<string, unknown>;
203
- graph: Graph;
204
- }): boolean {
205
- let skipHandling = false;
206
- if (metadata?.provider !== Providers.ANTHROPIC) {
207
- return skipHandling;
208
- }
209
- if (
210
- typeof content === 'string' ||
211
- content == null ||
212
- content.length === 0 ||
213
- (content.length === 1 &&
214
- (content[0] as t.ToolResultContent).tool_use_id == null)
215
- ) {
216
- return skipHandling;
217
- }
218
-
219
- for (const contentPart of content) {
220
- const toolUseId = (contentPart as t.ToolResultContent).tool_use_id;
221
- if (toolUseId == null || toolUseId === '') {
222
- continue;
223
- }
224
- const stepId = graph.toolCallStepIds.get(toolUseId);
225
- if (stepId == null || stepId === '') {
226
- console.warn(
227
- `Tool use ID ${toolUseId} not found in graph, cannot dispatch tool result.`
228
- );
229
- continue;
230
- }
231
- const runStep = graph.getRunStep(stepId);
232
- if (!runStep) {
233
- console.warn(
234
- `Run step for ${stepId} does not exist, cannot dispatch tool result.`
235
- );
236
- continue;
237
- } else if (runStep.type !== StepTypes.TOOL_CALLS) {
238
- console.warn(
239
- `Run step for ${stepId} is not a tool call step, cannot dispatch tool result.`
240
- );
241
- continue;
242
- }
243
-
244
- const toolCall =
245
- runStep.stepDetails.type === StepTypes.TOOL_CALLS
246
- ? (runStep.stepDetails.tool_calls?.find(
247
- (toolCall) => toolCall.id === toolUseId
248
- ) as ToolCall)
249
- : undefined;
250
-
251
- if (!toolCall) {
252
- continue;
253
- }
254
-
255
- if (
256
- contentPart.type === 'web_search_result' ||
257
- contentPart.type === 'web_search_tool_result'
258
- ) {
259
- handleAnthropicSearchResults({
260
- contentPart: contentPart as t.ToolResultContent,
261
- toolCall,
262
- metadata,
263
- graph,
264
- });
265
- }
266
-
267
- if (!skipHandling) {
268
- skipHandling = true;
269
- }
270
- }
271
-
272
- return skipHandling;
273
- }
274
-
275
- function handleAnthropicSearchResults({
276
- contentPart,
277
- toolCall,
278
- metadata,
279
- graph,
280
- }: {
281
- contentPart: t.ToolResultContent;
282
- toolCall: Partial<ToolCall>;
283
- metadata?: Record<string, unknown>;
284
- graph: Graph;
285
- }): void {
286
- if (!Array.isArray(contentPart.content)) {
287
- console.warn(
288
- `Expected content to be an array, got ${typeof contentPart.content}`
289
- );
290
- return;
291
- }
292
-
293
- if (!isAnthropicWebSearchResult(contentPart.content[0])) {
294
- console.warn(
295
- `Expected content to be an Anthropic web search result, got ${JSON.stringify(
296
- contentPart.content
297
- )}`
298
- );
299
- return;
300
- }
301
-
302
- const turn = graph.invokedToolIds?.size ?? 0;
303
- const searchResultData = coerceAnthropicSearchResults({
304
- turn,
305
- results: contentPart.content as AnthropicWebSearchResultBlockParam[],
306
- });
307
-
308
- const name = toolCall.name;
309
- const input = toolCall.args ?? {};
310
- const artifact = {
311
- [Constants.WEB_SEARCH]: searchResultData,
312
- };
313
- const { output: formattedOutput } = formatResultsForLLM(
314
- turn,
315
- searchResultData
316
- );
317
- const output = new ToolMessage({
318
- name,
319
- artifact,
320
- content: formattedOutput,
321
- tool_call_id: toolCall.id!,
322
- });
323
- const toolEndData: t.ToolEndData = {
324
- input,
325
- output,
326
- };
327
- graph.handlerRegistry
328
- ?.getHandler(GraphEvents.TOOL_END)
329
- ?.handle(GraphEvents.TOOL_END, toolEndData, metadata, graph);
330
-
331
- if (graph.invokedToolIds == null) {
332
- graph.invokedToolIds = new Set<string>();
333
- }
334
-
335
- graph.invokedToolIds.add(toolCall.id!);
336
- }
1
+ /* eslint-disable no-console */
2
+ // src/tools/handlers.ts
3
+ import { nanoid } from 'nanoid';
4
+ import { ToolMessage } from '@langchain/core/messages';
5
+ import type { AnthropicWebSearchResultBlockParam } from '@/llm/anthropic/types';
6
+ import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
7
+ import type { MultiAgentGraph, StandardGraph } from '@/graphs';
8
+ import type { AgentContext } from '@/agents/AgentContext';
9
+ import type * as t from '@/types';
10
+ import {
11
+ ToolCallTypes,
12
+ ContentTypes,
13
+ GraphEvents,
14
+ StepTypes,
15
+ Providers,
16
+ Constants,
17
+ } from '@/common';
18
+ import {
19
+ coerceAnthropicSearchResults,
20
+ isAnthropicWebSearchResult,
21
+ } from '@/tools/search/anthropic';
22
+ import { formatResultsForLLM } from '@/tools/search/format';
23
+ import { getMessageId } from '@/messages';
24
+
25
+ export async function handleToolCallChunks({
26
+ graph,
27
+ stepKey,
28
+ toolCallChunks,
29
+ }: {
30
+ graph: StandardGraph | MultiAgentGraph;
31
+ stepKey: string;
32
+ toolCallChunks: ToolCallChunk[];
33
+ }): Promise<void> {
34
+ let prevStepId: string;
35
+ let prevRunStep: t.RunStep | undefined;
36
+ try {
37
+ prevStepId = graph.getStepIdByKey(stepKey);
38
+ prevRunStep = graph.getRunStep(prevStepId);
39
+ } catch {
40
+ /** Edge Case: If no previous step exists, create a new message creation step */
41
+ const message_id = getMessageId(stepKey, graph, true) ?? '';
42
+ prevStepId = await graph.dispatchRunStep(stepKey, {
43
+ type: StepTypes.MESSAGE_CREATION,
44
+ message_creation: {
45
+ message_id,
46
+ },
47
+ });
48
+ prevRunStep = graph.getRunStep(prevStepId);
49
+ }
50
+
51
+ const _stepId = graph.getStepIdByKey(stepKey);
52
+
53
+ /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */
54
+ const tool_calls: ToolCall[] | undefined =
55
+ prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION
56
+ ? []
57
+ : undefined;
58
+
59
+ /** Edge Case: `id` and `name` fields cannot be empty strings */
60
+ for (const toolCallChunk of toolCallChunks) {
61
+ if (toolCallChunk.name === '') {
62
+ toolCallChunk.name = undefined;
63
+ }
64
+ if (toolCallChunk.id === '') {
65
+ toolCallChunk.id = undefined;
66
+ } else if (
67
+ tool_calls != null &&
68
+ toolCallChunk.id != null &&
69
+ toolCallChunk.name != null
70
+ ) {
71
+ tool_calls.push({
72
+ args: {},
73
+ id: toolCallChunk.id,
74
+ name: toolCallChunk.name,
75
+ type: ToolCallTypes.TOOL_CALL,
76
+ });
77
+ }
78
+ }
79
+
80
+ let stepId: string = _stepId;
81
+ const alreadyDispatched =
82
+ prevRunStep?.type === StepTypes.MESSAGE_CREATION &&
83
+ graph.messageStepHasToolCalls.has(prevStepId);
84
+ if (!alreadyDispatched && tool_calls?.length === toolCallChunks.length) {
85
+ await graph.dispatchMessageDelta(prevStepId, {
86
+ content: [
87
+ {
88
+ type: ContentTypes.TEXT,
89
+ text: '',
90
+ tool_call_ids: tool_calls.map((tc) => tc.id ?? ''),
91
+ },
92
+ ],
93
+ });
94
+ graph.messageStepHasToolCalls.set(prevStepId, true);
95
+ stepId = await graph.dispatchRunStep(stepKey, {
96
+ type: StepTypes.TOOL_CALLS,
97
+ tool_calls,
98
+ });
99
+ }
100
+ await graph.dispatchRunStepDelta(stepId, {
101
+ type: StepTypes.TOOL_CALLS,
102
+ tool_calls: toolCallChunks,
103
+ });
104
+ }
105
+
106
+ export const handleToolCalls = async (
107
+ toolCalls?: ToolCall[],
108
+ metadata?: Record<string, unknown>,
109
+ graph?: StandardGraph | MultiAgentGraph
110
+ ): Promise<void> => {
111
+ if (!graph || !metadata) {
112
+ console.warn(`Graph or metadata not found in ${event} event`);
113
+ return;
114
+ }
115
+
116
+ if (!toolCalls) {
117
+ return;
118
+ }
119
+
120
+ if (toolCalls.length === 0) {
121
+ return;
122
+ }
123
+
124
+ const stepKey = graph.getStepKey(metadata);
125
+
126
+ for (const tool_call of toolCalls) {
127
+ const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;
128
+ tool_call.id = toolCallId;
129
+ if (!toolCallId || graph.toolCallStepIds.has(toolCallId)) {
130
+ continue;
131
+ }
132
+
133
+ let prevStepId = '';
134
+ let prevRunStep: t.RunStep | undefined;
135
+ try {
136
+ prevStepId = graph.getStepIdByKey(stepKey);
137
+ prevRunStep = graph.getRunStep(prevStepId);
138
+ } catch {
139
+ // no previous step
140
+ }
141
+
142
+ const dispatchToolCallIds = async (
143
+ lastMessageStepId: string
144
+ ): Promise<void> => {
145
+ await graph.dispatchMessageDelta(lastMessageStepId, {
146
+ content: [
147
+ {
148
+ type: 'text',
149
+ text: '',
150
+ tool_call_ids: [toolCallId],
151
+ },
152
+ ],
153
+ });
154
+ };
155
+ /* If the previous step exists and is a message creation */
156
+ if (
157
+ prevStepId &&
158
+ prevRunStep &&
159
+ prevRunStep.type === StepTypes.MESSAGE_CREATION
160
+ ) {
161
+ await dispatchToolCallIds(prevStepId);
162
+ graph.messageStepHasToolCalls.set(prevStepId, true);
163
+ /* If the previous step doesn't exist or is not a message creation */
164
+ } else if (
165
+ !prevRunStep ||
166
+ prevRunStep.type !== StepTypes.MESSAGE_CREATION
167
+ ) {
168
+ const messageId = getMessageId(stepKey, graph, true) ?? '';
169
+ const stepId = await graph.dispatchRunStep(stepKey, {
170
+ type: StepTypes.MESSAGE_CREATION,
171
+ message_creation: {
172
+ message_id: messageId,
173
+ },
174
+ });
175
+ await dispatchToolCallIds(stepId);
176
+ graph.messageStepHasToolCalls.set(prevStepId, true);
177
+ }
178
+
179
+ await graph.dispatchRunStep(stepKey, {
180
+ type: StepTypes.TOOL_CALLS,
181
+ tool_calls: [tool_call],
182
+ });
183
+ }
184
+ };
185
+
186
+ export const toolResultTypes = new Set([
187
+ // 'tool_use',
188
+ // 'server_tool_use',
189
+ // 'input_json_delta',
190
+ 'tool_result',
191
+ 'web_search_result',
192
+ 'web_search_tool_result',
193
+ ]);
194
+
195
+ /**
196
+ * Handles the result of a server tool call; in other words, a provider's built-in tool.
197
+ * As of 2025-07-06, only Anthropic handles server tool calls with this pattern.
198
+ */
199
+ export async function handleServerToolResult({
200
+ graph,
201
+ content,
202
+ metadata,
203
+ agentContext,
204
+ }: {
205
+ graph: StandardGraph | MultiAgentGraph;
206
+ content?: string | t.MessageContentComplex[];
207
+ metadata?: Record<string, unknown>;
208
+ agentContext?: AgentContext;
209
+ }): Promise<boolean> {
210
+ let skipHandling = false;
211
+ if (agentContext?.provider !== Providers.ANTHROPIC) {
212
+ return skipHandling;
213
+ }
214
+ if (
215
+ typeof content === 'string' ||
216
+ content == null ||
217
+ content.length === 0 ||
218
+ (content.length === 1 &&
219
+ (content[0] as t.ToolResultContent).tool_use_id == null)
220
+ ) {
221
+ return skipHandling;
222
+ }
223
+
224
+ for (const contentPart of content) {
225
+ const toolUseId = (contentPart as t.ToolResultContent).tool_use_id;
226
+ if (toolUseId == null || toolUseId === '') {
227
+ continue;
228
+ }
229
+ const stepId = graph.toolCallStepIds.get(toolUseId);
230
+ if (stepId == null || stepId === '') {
231
+ console.warn(
232
+ `Tool use ID ${toolUseId} not found in graph, cannot dispatch tool result.`
233
+ );
234
+ continue;
235
+ }
236
+ const runStep = graph.getRunStep(stepId);
237
+ if (!runStep) {
238
+ console.warn(
239
+ `Run step for ${stepId} does not exist, cannot dispatch tool result.`
240
+ );
241
+ continue;
242
+ } else if (runStep.type !== StepTypes.TOOL_CALLS) {
243
+ console.warn(
244
+ `Run step for ${stepId} is not a tool call step, cannot dispatch tool result.`
245
+ );
246
+ continue;
247
+ }
248
+
249
+ const toolCall =
250
+ runStep.stepDetails.type === StepTypes.TOOL_CALLS
251
+ ? (runStep.stepDetails.tool_calls?.find(
252
+ (toolCall) => toolCall.id === toolUseId
253
+ ) as ToolCall)
254
+ : undefined;
255
+
256
+ if (!toolCall) {
257
+ continue;
258
+ }
259
+
260
+ if (
261
+ contentPart.type === 'web_search_result' ||
262
+ contentPart.type === 'web_search_tool_result'
263
+ ) {
264
+ await handleAnthropicSearchResults({
265
+ contentPart: contentPart as t.ToolResultContent,
266
+ toolCall,
267
+ metadata,
268
+ graph,
269
+ });
270
+ }
271
+
272
+ if (!skipHandling) {
273
+ skipHandling = true;
274
+ }
275
+ }
276
+
277
+ return skipHandling;
278
+ }
279
+
280
+ async function handleAnthropicSearchResults({
281
+ contentPart,
282
+ toolCall,
283
+ metadata,
284
+ graph,
285
+ }: {
286
+ contentPart: t.ToolResultContent;
287
+ toolCall: Partial<ToolCall>;
288
+ metadata?: Record<string, unknown>;
289
+ graph: StandardGraph | MultiAgentGraph;
290
+ }): Promise<void> {
291
+ if (!Array.isArray(contentPart.content)) {
292
+ console.warn(
293
+ `Expected content to be an array, got ${typeof contentPart.content}`
294
+ );
295
+ return;
296
+ }
297
+
298
+ if (!isAnthropicWebSearchResult(contentPart.content[0])) {
299
+ console.warn(
300
+ `Expected content to be an Anthropic web search result, got ${JSON.stringify(
301
+ contentPart.content
302
+ )}`
303
+ );
304
+ return;
305
+ }
306
+
307
+ const turn = graph.invokedToolIds?.size ?? 0;
308
+ const searchResultData = coerceAnthropicSearchResults({
309
+ turn,
310
+ results: contentPart.content as AnthropicWebSearchResultBlockParam[],
311
+ });
312
+
313
+ const name = toolCall.name;
314
+ const input = toolCall.args ?? {};
315
+ const artifact = {
316
+ [Constants.WEB_SEARCH]: searchResultData,
317
+ };
318
+ const { output: formattedOutput } = formatResultsForLLM(
319
+ turn,
320
+ searchResultData
321
+ );
322
+ const output = new ToolMessage({
323
+ name,
324
+ artifact,
325
+ content: formattedOutput,
326
+ tool_call_id: toolCall.id!,
327
+ });
328
+ const toolEndData: t.ToolEndData = {
329
+ input,
330
+ output,
331
+ };
332
+ await graph.handlerRegistry
333
+ ?.getHandler(GraphEvents.TOOL_END)
334
+ ?.handle(GraphEvents.TOOL_END, toolEndData, metadata, graph);
335
+
336
+ if (graph.invokedToolIds == null) {
337
+ graph.invokedToolIds = new Set<string>();
338
+ }
339
+
340
+ graph.invokedToolIds.add(toolCall.id!);
341
+ }