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.
- package/LICENSE +25 -21
- package/dist/cjs/agents/AgentContext.cjs +222 -0
- package/dist/cjs/agents/AgentContext.cjs.map +1 -0
- package/dist/cjs/common/enum.cjs +5 -4
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/events.cjs +7 -5
- package/dist/cjs/events.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +328 -207
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +507 -0
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/ollama/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +35 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +3 -1
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/llm/openrouter/index.cjs.map +1 -1
- package/dist/cjs/llm/providers.cjs +0 -2
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +12 -1
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +123 -0
- package/dist/cjs/messages/cache.cjs.map +1 -0
- package/dist/cjs/messages/content.cjs +53 -0
- package/dist/cjs/messages/content.cjs.map +1 -0
- package/dist/cjs/messages/format.cjs +17 -29
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/run.cjs +119 -74
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/stream.cjs +77 -73
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/Calculator.cjs +45 -0
- package/dist/cjs/tools/Calculator.cjs.map +1 -0
- package/dist/cjs/tools/CodeExecutor.cjs +22 -22
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +5 -3
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +20 -20
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/utils/events.cjs +31 -0
- package/dist/cjs/utils/events.cjs.map +1 -0
- package/dist/cjs/utils/handlers.cjs +70 -0
- package/dist/cjs/utils/handlers.cjs.map +1 -0
- package/dist/cjs/utils/tokens.cjs +54 -7
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +220 -0
- package/dist/esm/agents/AgentContext.mjs.map +1 -0
- package/dist/esm/common/enum.mjs +5 -4
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/events.mjs +7 -5
- package/dist/esm/events.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +330 -209
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +505 -0
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -0
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/ollama/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +35 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +3 -1
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/llm/openrouter/index.mjs.map +1 -1
- package/dist/esm/llm/providers.mjs +0 -2
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +7 -2
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +120 -0
- package/dist/esm/messages/cache.mjs.map +1 -0
- package/dist/esm/messages/content.mjs +51 -0
- package/dist/esm/messages/content.mjs.map +1 -0
- package/dist/esm/messages/format.mjs +18 -29
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/run.mjs +119 -74
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/stream.mjs +77 -73
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/Calculator.mjs +24 -0
- package/dist/esm/tools/Calculator.mjs.map +1 -0
- package/dist/esm/tools/CodeExecutor.mjs +22 -22
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +5 -3
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +20 -20
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/utils/events.mjs +29 -0
- package/dist/esm/utils/events.mjs.map +1 -0
- package/dist/esm/utils/handlers.mjs +68 -0
- package/dist/esm/utils/handlers.mjs.map +1 -0
- package/dist/esm/utils/tokens.mjs +54 -8
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +94 -0
- package/dist/types/common/enum.d.ts +7 -5
- package/dist/types/events.d.ts +3 -3
- package/dist/types/graphs/Graph.d.ts +60 -66
- package/dist/types/graphs/MultiAgentGraph.d.ts +47 -0
- package/dist/types/graphs/index.d.ts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/llm/openai/index.d.ts +10 -0
- package/dist/types/messages/cache.d.ts +20 -0
- package/dist/types/messages/content.d.ts +7 -0
- package/dist/types/messages/format.d.ts +1 -7
- package/dist/types/messages/index.d.ts +2 -0
- package/dist/types/messages/reducer.d.ts +9 -0
- package/dist/types/run.d.ts +16 -10
- package/dist/types/stream.d.ts +4 -3
- package/dist/types/tools/Calculator.d.ts +8 -0
- package/dist/types/tools/ToolNode.d.ts +1 -1
- package/dist/types/tools/handlers.d.ts +9 -7
- package/dist/types/tools/search/tool.d.ts +4 -4
- package/dist/types/types/graph.d.ts +124 -11
- package/dist/types/types/llm.d.ts +13 -9
- package/dist/types/types/messages.d.ts +4 -0
- package/dist/types/types/run.d.ts +46 -8
- package/dist/types/types/stream.d.ts +3 -2
- package/dist/types/utils/events.d.ts +6 -0
- package/dist/types/utils/handlers.d.ts +34 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/tokens.d.ts +24 -0
- package/package.json +162 -145
- package/src/agents/AgentContext.ts +323 -0
- package/src/common/enum.ts +177 -176
- package/src/events.ts +197 -191
- package/src/graphs/Graph.ts +1058 -846
- package/src/graphs/MultiAgentGraph.ts +598 -0
- package/src/graphs/index.ts +2 -1
- package/src/index.ts +25 -24
- package/src/llm/anthropic/index.ts +413 -413
- package/src/llm/google/index.ts +222 -222
- package/src/llm/google/utils/zod_to_genai_parameters.ts +86 -88
- package/src/llm/ollama/index.ts +92 -92
- package/src/llm/openai/index.ts +894 -853
- package/src/llm/openai/utils/index.ts +920 -918
- package/src/llm/openrouter/index.ts +60 -60
- package/src/llm/providers.ts +55 -57
- package/src/llm/vertexai/index.ts +360 -360
- package/src/messages/cache.test.ts +461 -0
- package/src/messages/cache.ts +151 -0
- package/src/messages/content.test.ts +362 -0
- package/src/messages/content.ts +63 -0
- package/src/messages/format.ts +611 -625
- package/src/messages/formatAgentMessages.test.ts +1144 -917
- package/src/messages/index.ts +6 -4
- package/src/messages/reducer.ts +80 -0
- package/src/run.ts +447 -381
- package/src/scripts/abort.ts +157 -138
- package/src/scripts/ant_web_search.ts +158 -158
- package/src/scripts/cli.ts +172 -167
- package/src/scripts/cli2.ts +133 -125
- package/src/scripts/cli3.ts +184 -178
- package/src/scripts/cli4.ts +191 -184
- package/src/scripts/cli5.ts +191 -184
- package/src/scripts/code_exec.ts +213 -214
- package/src/scripts/code_exec_simple.ts +147 -129
- package/src/scripts/content.ts +138 -120
- package/src/scripts/handoff-test.ts +135 -0
- package/src/scripts/multi-agent-chain.ts +278 -0
- package/src/scripts/multi-agent-conditional.ts +220 -0
- package/src/scripts/multi-agent-document-review-chain.ts +197 -0
- package/src/scripts/multi-agent-hybrid-flow.ts +310 -0
- package/src/scripts/multi-agent-parallel.ts +343 -0
- package/src/scripts/multi-agent-sequence.ts +212 -0
- package/src/scripts/multi-agent-supervisor.ts +364 -0
- package/src/scripts/multi-agent-test.ts +186 -0
- package/src/scripts/search.ts +146 -150
- package/src/scripts/simple.ts +225 -225
- package/src/scripts/stream.ts +140 -122
- package/src/scripts/test-custom-prompt-key.ts +145 -0
- package/src/scripts/test-handoff-input.ts +170 -0
- package/src/scripts/test-multi-agent-list-handoff.ts +261 -0
- package/src/scripts/test-tools-before-handoff.ts +222 -0
- package/src/scripts/tools.ts +153 -155
- package/src/specs/agent-handoffs.test.ts +889 -0
- package/src/specs/anthropic.simple.test.ts +320 -317
- package/src/specs/azure.simple.test.ts +325 -316
- package/src/specs/openai.simple.test.ts +311 -316
- package/src/specs/openrouter.simple.test.ts +107 -0
- package/src/specs/prune.test.ts +758 -763
- package/src/specs/reasoning.test.ts +201 -165
- package/src/specs/thinking-prune.test.ts +769 -703
- package/src/specs/token-memoization.test.ts +39 -0
- package/src/stream.ts +664 -651
- package/src/tools/Calculator.test.ts +278 -0
- package/src/tools/Calculator.ts +25 -0
- package/src/tools/CodeExecutor.ts +220 -220
- package/src/tools/ToolNode.ts +170 -170
- package/src/tools/handlers.ts +341 -336
- package/src/types/graph.ts +372 -185
- package/src/types/llm.ts +141 -140
- package/src/types/messages.ts +4 -0
- package/src/types/run.ts +128 -89
- package/src/types/stream.ts +401 -400
- package/src/utils/events.ts +32 -0
- package/src/utils/handlers.ts +107 -0
- package/src/utils/index.ts +6 -5
- package/src/utils/llmConfig.ts +183 -183
- package/src/utils/tokens.ts +129 -70
- package/dist/types/scripts/abort.d.ts +0 -1
- package/dist/types/scripts/ant_web_search.d.ts +0 -1
- package/dist/types/scripts/args.d.ts +0 -7
- package/dist/types/scripts/caching.d.ts +0 -1
- package/dist/types/scripts/cli.d.ts +0 -1
- package/dist/types/scripts/cli2.d.ts +0 -1
- package/dist/types/scripts/cli3.d.ts +0 -1
- package/dist/types/scripts/cli4.d.ts +0 -1
- package/dist/types/scripts/cli5.d.ts +0 -1
- package/dist/types/scripts/code_exec.d.ts +0 -1
- package/dist/types/scripts/code_exec_files.d.ts +0 -1
- package/dist/types/scripts/code_exec_simple.d.ts +0 -1
- package/dist/types/scripts/content.d.ts +0 -1
- package/dist/types/scripts/empty_input.d.ts +0 -1
- package/dist/types/scripts/image.d.ts +0 -1
- package/dist/types/scripts/memory.d.ts +0 -1
- package/dist/types/scripts/search.d.ts +0 -1
- package/dist/types/scripts/simple.d.ts +0 -1
- package/dist/types/scripts/stream.d.ts +0 -1
- package/dist/types/scripts/thinking.d.ts +0 -1
- package/dist/types/scripts/tools.d.ts +0 -1
- package/dist/types/specs/spec.utils.d.ts +0 -1
- package/dist/types/tools/example.d.ts +0 -78
- package/src/tools/example.ts +0 -129
package/src/stream.ts
CHANGED
|
@@ -1,651 +1,664 @@
|
|
|
1
|
-
// src/stream.ts
|
|
2
|
-
import type { ChatOpenAIReasoningSummary } from '@langchain/openai';
|
|
3
|
-
import type { AIMessageChunk } from '@langchain/core/messages';
|
|
4
|
-
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
5
|
-
import type {
|
|
6
|
-
import type
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*
|
|
24
|
-
* @
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
chunk?.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
) {
|
|
237
|
-
return;
|
|
238
|
-
} else if (
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
type
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
reasoning_content
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
reasoning_content
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
reasoning_content
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
chunk.content
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (!
|
|
406
|
-
console.warn('No content
|
|
407
|
-
return;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
if (!
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
if (
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
contentParts[index]
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
const
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
if
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
};
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
1
|
+
// src/stream.ts
|
|
2
|
+
import type { ChatOpenAIReasoningSummary } from '@langchain/openai';
|
|
3
|
+
import type { AIMessageChunk } from '@langchain/core/messages';
|
|
4
|
+
import type { ToolCall } from '@langchain/core/messages/tool';
|
|
5
|
+
import type { AgentContext } from '@/agents/AgentContext';
|
|
6
|
+
import type { StandardGraph } from '@/graphs';
|
|
7
|
+
import type * as t from '@/types';
|
|
8
|
+
import {
|
|
9
|
+
ToolCallTypes,
|
|
10
|
+
ContentTypes,
|
|
11
|
+
GraphEvents,
|
|
12
|
+
StepTypes,
|
|
13
|
+
Providers,
|
|
14
|
+
} from '@/common';
|
|
15
|
+
import {
|
|
16
|
+
handleServerToolResult,
|
|
17
|
+
handleToolCallChunks,
|
|
18
|
+
handleToolCalls,
|
|
19
|
+
} from '@/tools/handlers';
|
|
20
|
+
import { getMessageId } from '@/messages';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Parses content to extract thinking sections enclosed in <think> tags using string operations
|
|
24
|
+
* @param content The content to parse
|
|
25
|
+
* @returns An object with separated text and thinking content
|
|
26
|
+
*/
|
|
27
|
+
function parseThinkingContent(content: string): {
|
|
28
|
+
text: string;
|
|
29
|
+
thinking: string;
|
|
30
|
+
} {
|
|
31
|
+
// If no think tags, return the original content as text
|
|
32
|
+
if (!content.includes('<think>')) {
|
|
33
|
+
return { text: content, thinking: '' };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let textResult = '';
|
|
37
|
+
const thinkingResult: string[] = [];
|
|
38
|
+
let position = 0;
|
|
39
|
+
|
|
40
|
+
while (position < content.length) {
|
|
41
|
+
const thinkStart = content.indexOf('<think>', position);
|
|
42
|
+
|
|
43
|
+
if (thinkStart === -1) {
|
|
44
|
+
// No more think tags, add the rest and break
|
|
45
|
+
textResult += content.slice(position);
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Add text before the think tag
|
|
50
|
+
textResult += content.slice(position, thinkStart);
|
|
51
|
+
|
|
52
|
+
const thinkEnd = content.indexOf('</think>', thinkStart);
|
|
53
|
+
if (thinkEnd === -1) {
|
|
54
|
+
// Malformed input, no closing tag
|
|
55
|
+
textResult += content.slice(thinkStart);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Add the thinking content
|
|
60
|
+
const thinkContent = content.slice(thinkStart + 7, thinkEnd);
|
|
61
|
+
thinkingResult.push(thinkContent);
|
|
62
|
+
|
|
63
|
+
// Move position to after the think tag
|
|
64
|
+
position = thinkEnd + 8; // 8 is the length of '</think>'
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
text: textResult.trim(),
|
|
69
|
+
thinking: thinkingResult.join('\n').trim(),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getNonEmptyValue(possibleValues: string[]): string | undefined {
|
|
74
|
+
for (const value of possibleValues) {
|
|
75
|
+
if (value && value.trim() !== '') {
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function getChunkContent({
|
|
83
|
+
chunk,
|
|
84
|
+
provider,
|
|
85
|
+
reasoningKey,
|
|
86
|
+
}: {
|
|
87
|
+
chunk?: Partial<AIMessageChunk>;
|
|
88
|
+
provider?: Providers;
|
|
89
|
+
reasoningKey: 'reasoning_content' | 'reasoning';
|
|
90
|
+
}): string | t.MessageContentComplex[] | undefined {
|
|
91
|
+
if (
|
|
92
|
+
(provider === Providers.OPENAI || provider === Providers.AZURE) &&
|
|
93
|
+
(
|
|
94
|
+
chunk?.additional_kwargs?.reasoning as
|
|
95
|
+
| Partial<ChatOpenAIReasoningSummary>
|
|
96
|
+
| undefined
|
|
97
|
+
)?.summary?.[0]?.text != null &&
|
|
98
|
+
((
|
|
99
|
+
chunk?.additional_kwargs?.reasoning as
|
|
100
|
+
| Partial<ChatOpenAIReasoningSummary>
|
|
101
|
+
| undefined
|
|
102
|
+
)?.summary?.[0]?.text?.length ?? 0) > 0
|
|
103
|
+
) {
|
|
104
|
+
return (
|
|
105
|
+
chunk?.additional_kwargs?.reasoning as
|
|
106
|
+
| Partial<ChatOpenAIReasoningSummary>
|
|
107
|
+
| undefined
|
|
108
|
+
)?.summary?.[0]?.text;
|
|
109
|
+
}
|
|
110
|
+
return (
|
|
111
|
+
((chunk?.additional_kwargs?.[reasoningKey] as string | undefined) ?? '') ||
|
|
112
|
+
chunk?.content
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export class ChatModelStreamHandler implements t.EventHandler {
|
|
117
|
+
async handle(
|
|
118
|
+
event: string,
|
|
119
|
+
data: t.StreamEventData,
|
|
120
|
+
metadata?: Record<string, unknown>,
|
|
121
|
+
graph?: StandardGraph
|
|
122
|
+
): Promise<void> {
|
|
123
|
+
if (!graph) {
|
|
124
|
+
throw new Error('Graph not found');
|
|
125
|
+
}
|
|
126
|
+
if (!graph.config) {
|
|
127
|
+
throw new Error('Config not found in graph');
|
|
128
|
+
}
|
|
129
|
+
if (!data.chunk) {
|
|
130
|
+
console.warn(`No chunk found in ${event} event`);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const agentContext = graph.getAgentContext(metadata);
|
|
135
|
+
|
|
136
|
+
const chunk = data.chunk as Partial<AIMessageChunk>;
|
|
137
|
+
const content = getChunkContent({
|
|
138
|
+
chunk,
|
|
139
|
+
reasoningKey: agentContext.reasoningKey,
|
|
140
|
+
provider: agentContext.provider,
|
|
141
|
+
});
|
|
142
|
+
const skipHandling = await handleServerToolResult({
|
|
143
|
+
graph,
|
|
144
|
+
content,
|
|
145
|
+
metadata,
|
|
146
|
+
agentContext,
|
|
147
|
+
});
|
|
148
|
+
if (skipHandling) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.handleReasoning(chunk, agentContext);
|
|
152
|
+
let hasToolCalls = false;
|
|
153
|
+
if (
|
|
154
|
+
chunk.tool_calls &&
|
|
155
|
+
chunk.tool_calls.length > 0 &&
|
|
156
|
+
chunk.tool_calls.every(
|
|
157
|
+
(tc) =>
|
|
158
|
+
tc.id != null && tc.id !== '' && tc.name != null && tc.name !== ''
|
|
159
|
+
)
|
|
160
|
+
) {
|
|
161
|
+
hasToolCalls = true;
|
|
162
|
+
await handleToolCalls(chunk.tool_calls, metadata, graph);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const hasToolCallChunks =
|
|
166
|
+
(chunk.tool_call_chunks && chunk.tool_call_chunks.length > 0) ?? false;
|
|
167
|
+
const isEmptyContent =
|
|
168
|
+
typeof content === 'undefined' ||
|
|
169
|
+
!content.length ||
|
|
170
|
+
(typeof content === 'string' && !content);
|
|
171
|
+
|
|
172
|
+
/** Set a preliminary message ID if found in empty chunk */
|
|
173
|
+
const isEmptyChunk = isEmptyContent && !hasToolCallChunks;
|
|
174
|
+
if (
|
|
175
|
+
isEmptyChunk &&
|
|
176
|
+
(chunk.id ?? '') !== '' &&
|
|
177
|
+
!graph.prelimMessageIdsByStepKey.has(chunk.id ?? '')
|
|
178
|
+
) {
|
|
179
|
+
const stepKey = graph.getStepKey(metadata);
|
|
180
|
+
graph.prelimMessageIdsByStepKey.set(stepKey, chunk.id ?? '');
|
|
181
|
+
} else if (isEmptyChunk) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const stepKey = graph.getStepKey(metadata);
|
|
186
|
+
|
|
187
|
+
if (
|
|
188
|
+
hasToolCallChunks &&
|
|
189
|
+
chunk.tool_call_chunks &&
|
|
190
|
+
chunk.tool_call_chunks.length &&
|
|
191
|
+
typeof chunk.tool_call_chunks[0]?.index === 'number'
|
|
192
|
+
) {
|
|
193
|
+
await handleToolCallChunks({
|
|
194
|
+
graph,
|
|
195
|
+
stepKey,
|
|
196
|
+
toolCallChunks: chunk.tool_call_chunks,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (isEmptyContent) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const message_id = getMessageId(stepKey, graph) ?? '';
|
|
205
|
+
if (message_id) {
|
|
206
|
+
await graph.dispatchRunStep(stepKey, {
|
|
207
|
+
type: StepTypes.MESSAGE_CREATION,
|
|
208
|
+
message_creation: {
|
|
209
|
+
message_id,
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const stepId = graph.getStepIdByKey(stepKey);
|
|
215
|
+
const runStep = graph.getRunStep(stepId);
|
|
216
|
+
if (!runStep) {
|
|
217
|
+
console.warn(`\n
|
|
218
|
+
==============================================================
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
Run step for ${stepId} does not exist, cannot dispatch delta event.
|
|
222
|
+
|
|
223
|
+
event: ${event}
|
|
224
|
+
stepId: ${stepId}
|
|
225
|
+
stepKey: ${stepKey}
|
|
226
|
+
message_id: ${message_id}
|
|
227
|
+
hasToolCalls: ${hasToolCalls}
|
|
228
|
+
hasToolCallChunks: ${hasToolCallChunks}
|
|
229
|
+
|
|
230
|
+
==============================================================
|
|
231
|
+
\n`);
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/* Note: tool call chunks may have non-empty content that matches the current tool chunk generation */
|
|
236
|
+
if (typeof content === 'string' && runStep.type === StepTypes.TOOL_CALLS) {
|
|
237
|
+
return;
|
|
238
|
+
} else if (
|
|
239
|
+
hasToolCallChunks &&
|
|
240
|
+
(chunk.tool_call_chunks?.some((tc) => tc.args === content) ?? false)
|
|
241
|
+
) {
|
|
242
|
+
return;
|
|
243
|
+
} else if (typeof content === 'string') {
|
|
244
|
+
if (agentContext.currentTokenType === ContentTypes.TEXT) {
|
|
245
|
+
await graph.dispatchMessageDelta(stepId, {
|
|
246
|
+
content: [
|
|
247
|
+
{
|
|
248
|
+
type: ContentTypes.TEXT,
|
|
249
|
+
text: content,
|
|
250
|
+
},
|
|
251
|
+
],
|
|
252
|
+
});
|
|
253
|
+
} else if (agentContext.currentTokenType === 'think_and_text') {
|
|
254
|
+
const { text, thinking } = parseThinkingContent(content);
|
|
255
|
+
if (thinking) {
|
|
256
|
+
await graph.dispatchReasoningDelta(stepId, {
|
|
257
|
+
content: [
|
|
258
|
+
{
|
|
259
|
+
type: ContentTypes.THINK,
|
|
260
|
+
think: thinking,
|
|
261
|
+
},
|
|
262
|
+
],
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
if (text) {
|
|
266
|
+
agentContext.currentTokenType = ContentTypes.TEXT;
|
|
267
|
+
agentContext.tokenTypeSwitch = 'content';
|
|
268
|
+
const newStepKey = graph.getStepKey(metadata);
|
|
269
|
+
const message_id = getMessageId(newStepKey, graph) ?? '';
|
|
270
|
+
await graph.dispatchRunStep(newStepKey, {
|
|
271
|
+
type: StepTypes.MESSAGE_CREATION,
|
|
272
|
+
message_creation: {
|
|
273
|
+
message_id,
|
|
274
|
+
},
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
const newStepId = graph.getStepIdByKey(newStepKey);
|
|
278
|
+
await graph.dispatchMessageDelta(newStepId, {
|
|
279
|
+
content: [
|
|
280
|
+
{
|
|
281
|
+
type: ContentTypes.TEXT,
|
|
282
|
+
text: text,
|
|
283
|
+
},
|
|
284
|
+
],
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
} else {
|
|
288
|
+
await graph.dispatchReasoningDelta(stepId, {
|
|
289
|
+
content: [
|
|
290
|
+
{
|
|
291
|
+
type: ContentTypes.THINK,
|
|
292
|
+
think: content,
|
|
293
|
+
},
|
|
294
|
+
],
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
} else if (
|
|
298
|
+
content.every((c) => c.type?.startsWith(ContentTypes.TEXT) ?? false)
|
|
299
|
+
) {
|
|
300
|
+
await graph.dispatchMessageDelta(stepId, {
|
|
301
|
+
content,
|
|
302
|
+
});
|
|
303
|
+
} else if (
|
|
304
|
+
content.every(
|
|
305
|
+
(c) =>
|
|
306
|
+
(c.type?.startsWith(ContentTypes.THINKING) ?? false) ||
|
|
307
|
+
(c.type?.startsWith(ContentTypes.REASONING) ?? false) ||
|
|
308
|
+
(c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false)
|
|
309
|
+
)
|
|
310
|
+
) {
|
|
311
|
+
await graph.dispatchReasoningDelta(stepId, {
|
|
312
|
+
content: content.map((c) => ({
|
|
313
|
+
type: ContentTypes.THINK,
|
|
314
|
+
think:
|
|
315
|
+
(c as t.ThinkingContentText).thinking ??
|
|
316
|
+
(c as Partial<t.GoogleReasoningContentText>).reasoning ??
|
|
317
|
+
(c as Partial<t.BedrockReasoningContentText>).reasoningText?.text ??
|
|
318
|
+
'',
|
|
319
|
+
})),
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
handleReasoning(
|
|
324
|
+
chunk: Partial<AIMessageChunk>,
|
|
325
|
+
agentContext: AgentContext
|
|
326
|
+
): void {
|
|
327
|
+
let reasoning_content = chunk.additional_kwargs?.[
|
|
328
|
+
agentContext.reasoningKey
|
|
329
|
+
] as string | Partial<ChatOpenAIReasoningSummary> | undefined;
|
|
330
|
+
if (
|
|
331
|
+
Array.isArray(chunk.content) &&
|
|
332
|
+
(chunk.content[0]?.type === ContentTypes.THINKING ||
|
|
333
|
+
chunk.content[0]?.type === ContentTypes.REASONING ||
|
|
334
|
+
chunk.content[0]?.type === ContentTypes.REASONING_CONTENT)
|
|
335
|
+
) {
|
|
336
|
+
reasoning_content = 'valid';
|
|
337
|
+
} else if (
|
|
338
|
+
(agentContext.provider === Providers.OPENAI ||
|
|
339
|
+
agentContext.provider === Providers.AZURE) &&
|
|
340
|
+
reasoning_content != null &&
|
|
341
|
+
typeof reasoning_content !== 'string' &&
|
|
342
|
+
reasoning_content.summary?.[0]?.text != null &&
|
|
343
|
+
reasoning_content.summary[0].text
|
|
344
|
+
) {
|
|
345
|
+
reasoning_content = 'valid';
|
|
346
|
+
}
|
|
347
|
+
if (
|
|
348
|
+
reasoning_content != null &&
|
|
349
|
+
reasoning_content !== '' &&
|
|
350
|
+
(chunk.content == null ||
|
|
351
|
+
chunk.content === '' ||
|
|
352
|
+
reasoning_content === 'valid')
|
|
353
|
+
) {
|
|
354
|
+
agentContext.currentTokenType = ContentTypes.THINK;
|
|
355
|
+
agentContext.tokenTypeSwitch = 'reasoning';
|
|
356
|
+
return;
|
|
357
|
+
} else if (
|
|
358
|
+
agentContext.tokenTypeSwitch === 'reasoning' &&
|
|
359
|
+
agentContext.currentTokenType !== ContentTypes.TEXT &&
|
|
360
|
+
((chunk.content != null && chunk.content !== '') ||
|
|
361
|
+
(chunk.tool_calls?.length ?? 0) > 0 ||
|
|
362
|
+
(chunk.tool_call_chunks?.length ?? 0) > 0)
|
|
363
|
+
) {
|
|
364
|
+
agentContext.currentTokenType = ContentTypes.TEXT;
|
|
365
|
+
agentContext.tokenTypeSwitch = 'content';
|
|
366
|
+
} else if (
|
|
367
|
+
chunk.content != null &&
|
|
368
|
+
typeof chunk.content === 'string' &&
|
|
369
|
+
chunk.content.includes('<think>') &&
|
|
370
|
+
chunk.content.includes('</think>')
|
|
371
|
+
) {
|
|
372
|
+
agentContext.currentTokenType = 'think_and_text';
|
|
373
|
+
agentContext.tokenTypeSwitch = 'content';
|
|
374
|
+
} else if (
|
|
375
|
+
chunk.content != null &&
|
|
376
|
+
typeof chunk.content === 'string' &&
|
|
377
|
+
chunk.content.includes('<think>')
|
|
378
|
+
) {
|
|
379
|
+
agentContext.currentTokenType = ContentTypes.THINK;
|
|
380
|
+
agentContext.tokenTypeSwitch = 'content';
|
|
381
|
+
} else if (
|
|
382
|
+
agentContext.lastToken != null &&
|
|
383
|
+
agentContext.lastToken.includes('</think>')
|
|
384
|
+
) {
|
|
385
|
+
agentContext.currentTokenType = ContentTypes.TEXT;
|
|
386
|
+
agentContext.tokenTypeSwitch = 'content';
|
|
387
|
+
}
|
|
388
|
+
if (typeof chunk.content !== 'string') {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
agentContext.lastToken = chunk.content;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function createContentAggregator(): t.ContentAggregatorResult {
|
|
396
|
+
const contentParts: Array<t.MessageContentComplex | undefined> = [];
|
|
397
|
+
const stepMap = new Map<string, t.RunStep>();
|
|
398
|
+
const toolCallIdMap = new Map<string, string>();
|
|
399
|
+
|
|
400
|
+
const updateContent = (
|
|
401
|
+
index: number,
|
|
402
|
+
contentPart?: t.MessageContentComplex,
|
|
403
|
+
finalUpdate = false
|
|
404
|
+
): void => {
|
|
405
|
+
if (!contentPart) {
|
|
406
|
+
console.warn('No content part found in \'updateContent\'');
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
const partType = contentPart.type ?? '';
|
|
410
|
+
if (!partType) {
|
|
411
|
+
console.warn('No content type found in content part');
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (!contentParts[index]) {
|
|
416
|
+
contentParts[index] = { type: partType };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (!partType.startsWith(contentParts[index]?.type ?? '')) {
|
|
420
|
+
console.warn('Content type mismatch');
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (
|
|
425
|
+
partType.startsWith(ContentTypes.TEXT) &&
|
|
426
|
+
ContentTypes.TEXT in contentPart &&
|
|
427
|
+
typeof contentPart.text === 'string'
|
|
428
|
+
) {
|
|
429
|
+
// TODO: update this!!
|
|
430
|
+
const currentContent = contentParts[index] as t.MessageDeltaUpdate;
|
|
431
|
+
const update: t.MessageDeltaUpdate = {
|
|
432
|
+
type: ContentTypes.TEXT,
|
|
433
|
+
text: (currentContent.text || '') + contentPart.text,
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
if (contentPart.tool_call_ids) {
|
|
437
|
+
update.tool_call_ids = contentPart.tool_call_ids;
|
|
438
|
+
}
|
|
439
|
+
contentParts[index] = update;
|
|
440
|
+
} else if (
|
|
441
|
+
partType.startsWith(ContentTypes.THINK) &&
|
|
442
|
+
ContentTypes.THINK in contentPart &&
|
|
443
|
+
typeof contentPart.think === 'string'
|
|
444
|
+
) {
|
|
445
|
+
const currentContent = contentParts[index] as t.ReasoningDeltaUpdate;
|
|
446
|
+
const update: t.ReasoningDeltaUpdate = {
|
|
447
|
+
type: ContentTypes.THINK,
|
|
448
|
+
think: (currentContent.think || '') + contentPart.think,
|
|
449
|
+
};
|
|
450
|
+
contentParts[index] = update;
|
|
451
|
+
} else if (
|
|
452
|
+
partType.startsWith(ContentTypes.AGENT_UPDATE) &&
|
|
453
|
+
ContentTypes.AGENT_UPDATE in contentPart &&
|
|
454
|
+
contentPart.agent_update != null
|
|
455
|
+
) {
|
|
456
|
+
const update: t.AgentUpdate = {
|
|
457
|
+
type: ContentTypes.AGENT_UPDATE,
|
|
458
|
+
agent_update: contentPart.agent_update,
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
contentParts[index] = update;
|
|
462
|
+
} else if (
|
|
463
|
+
partType === ContentTypes.IMAGE_URL &&
|
|
464
|
+
'image_url' in contentPart
|
|
465
|
+
) {
|
|
466
|
+
const currentContent = contentParts[index] as {
|
|
467
|
+
type: 'image_url';
|
|
468
|
+
image_url: string;
|
|
469
|
+
};
|
|
470
|
+
contentParts[index] = {
|
|
471
|
+
...currentContent,
|
|
472
|
+
};
|
|
473
|
+
} else if (
|
|
474
|
+
partType === ContentTypes.TOOL_CALL &&
|
|
475
|
+
'tool_call' in contentPart
|
|
476
|
+
) {
|
|
477
|
+
const incomingName = contentPart.tool_call.name;
|
|
478
|
+
const incomingId = contentPart.tool_call.id;
|
|
479
|
+
const toolCallArgs = (contentPart.tool_call as t.ToolCallPart).args;
|
|
480
|
+
|
|
481
|
+
// When we receive a tool call with a name, it's the complete tool call
|
|
482
|
+
// Consolidate with any previously accumulated args from chunks
|
|
483
|
+
const hasValidName = incomingName != null && incomingName !== '';
|
|
484
|
+
|
|
485
|
+
// Only process if incoming has a valid name (complete tool call)
|
|
486
|
+
// or if we're doing a final update with complete data
|
|
487
|
+
if (!hasValidName && !finalUpdate) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const existingContent = contentParts[index] as
|
|
492
|
+
| (Omit<t.ToolCallContent, 'tool_call'> & {
|
|
493
|
+
tool_call?: t.ToolCallPart;
|
|
494
|
+
})
|
|
495
|
+
| undefined;
|
|
496
|
+
|
|
497
|
+
/** When args are a valid object, they are likely already invoked */
|
|
498
|
+
let args =
|
|
499
|
+
finalUpdate ||
|
|
500
|
+
typeof existingContent?.tool_call?.args === 'object' ||
|
|
501
|
+
typeof toolCallArgs === 'object'
|
|
502
|
+
? contentPart.tool_call.args
|
|
503
|
+
: (existingContent?.tool_call?.args ?? '') + (toolCallArgs ?? '');
|
|
504
|
+
if (
|
|
505
|
+
finalUpdate &&
|
|
506
|
+
args == null &&
|
|
507
|
+
existingContent?.tool_call?.args != null
|
|
508
|
+
) {
|
|
509
|
+
args = existingContent.tool_call.args;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const id =
|
|
513
|
+
getNonEmptyValue([incomingId, existingContent?.tool_call?.id]) ?? '';
|
|
514
|
+
const name =
|
|
515
|
+
getNonEmptyValue([incomingName, existingContent?.tool_call?.name]) ??
|
|
516
|
+
'';
|
|
517
|
+
|
|
518
|
+
const newToolCall: ToolCall & t.PartMetadata = {
|
|
519
|
+
id,
|
|
520
|
+
name,
|
|
521
|
+
args,
|
|
522
|
+
type: ToolCallTypes.TOOL_CALL,
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
if (finalUpdate) {
|
|
526
|
+
newToolCall.progress = 1;
|
|
527
|
+
newToolCall.output = contentPart.tool_call.output;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
contentParts[index] = {
|
|
531
|
+
type: ContentTypes.TOOL_CALL,
|
|
532
|
+
tool_call: newToolCall,
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
const aggregateContent = ({
|
|
538
|
+
event,
|
|
539
|
+
data,
|
|
540
|
+
}: {
|
|
541
|
+
event: GraphEvents;
|
|
542
|
+
data:
|
|
543
|
+
| t.RunStep
|
|
544
|
+
| t.AgentUpdate
|
|
545
|
+
| t.MessageDeltaEvent
|
|
546
|
+
| t.RunStepDeltaEvent
|
|
547
|
+
| { result: t.ToolEndEvent };
|
|
548
|
+
}): void => {
|
|
549
|
+
if (event === GraphEvents.ON_RUN_STEP) {
|
|
550
|
+
const runStep = data as t.RunStep;
|
|
551
|
+
stepMap.set(runStep.id, runStep);
|
|
552
|
+
|
|
553
|
+
// Store tool call IDs if present
|
|
554
|
+
if (
|
|
555
|
+
runStep.stepDetails.type === StepTypes.TOOL_CALLS &&
|
|
556
|
+
runStep.stepDetails.tool_calls
|
|
557
|
+
) {
|
|
558
|
+
(runStep.stepDetails.tool_calls as ToolCall[]).forEach((toolCall) => {
|
|
559
|
+
const toolCallId = toolCall.id ?? '';
|
|
560
|
+
if ('id' in toolCall && toolCallId) {
|
|
561
|
+
toolCallIdMap.set(runStep.id, toolCallId);
|
|
562
|
+
}
|
|
563
|
+
const contentPart: t.MessageContentComplex = {
|
|
564
|
+
type: ContentTypes.TOOL_CALL,
|
|
565
|
+
tool_call: {
|
|
566
|
+
args: toolCall.args,
|
|
567
|
+
name: toolCall.name,
|
|
568
|
+
id: toolCallId,
|
|
569
|
+
},
|
|
570
|
+
};
|
|
571
|
+
|
|
572
|
+
updateContent(runStep.index, contentPart);
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
} else if (event === GraphEvents.ON_MESSAGE_DELTA) {
|
|
576
|
+
const messageDelta = data as t.MessageDeltaEvent;
|
|
577
|
+
const runStep = stepMap.get(messageDelta.id);
|
|
578
|
+
if (!runStep) {
|
|
579
|
+
console.warn('No run step or runId found for message delta event');
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
if (messageDelta.delta.content) {
|
|
584
|
+
const contentPart = Array.isArray(messageDelta.delta.content)
|
|
585
|
+
? messageDelta.delta.content[0]
|
|
586
|
+
: messageDelta.delta.content;
|
|
587
|
+
|
|
588
|
+
updateContent(runStep.index, contentPart);
|
|
589
|
+
}
|
|
590
|
+
} else if (
|
|
591
|
+
event === GraphEvents.ON_AGENT_UPDATE &&
|
|
592
|
+
(data as t.AgentUpdate | undefined)?.agent_update
|
|
593
|
+
) {
|
|
594
|
+
const contentPart = data as t.AgentUpdate | undefined;
|
|
595
|
+
if (!contentPart) {
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
updateContent(contentPart.agent_update.index, contentPart);
|
|
599
|
+
} else if (event === GraphEvents.ON_REASONING_DELTA) {
|
|
600
|
+
const reasoningDelta = data as t.ReasoningDeltaEvent;
|
|
601
|
+
const runStep = stepMap.get(reasoningDelta.id);
|
|
602
|
+
if (!runStep) {
|
|
603
|
+
console.warn('No run step or runId found for reasoning delta event');
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (reasoningDelta.delta.content) {
|
|
608
|
+
const contentPart = Array.isArray(reasoningDelta.delta.content)
|
|
609
|
+
? reasoningDelta.delta.content[0]
|
|
610
|
+
: reasoningDelta.delta.content;
|
|
611
|
+
|
|
612
|
+
updateContent(runStep.index, contentPart);
|
|
613
|
+
}
|
|
614
|
+
} else if (event === GraphEvents.ON_RUN_STEP_DELTA) {
|
|
615
|
+
const runStepDelta = data as t.RunStepDeltaEvent;
|
|
616
|
+
const runStep = stepMap.get(runStepDelta.id);
|
|
617
|
+
if (!runStep) {
|
|
618
|
+
console.warn('No run step or runId found for run step delta event');
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
if (
|
|
623
|
+
runStepDelta.delta.type === StepTypes.TOOL_CALLS &&
|
|
624
|
+
runStepDelta.delta.tool_calls
|
|
625
|
+
) {
|
|
626
|
+
runStepDelta.delta.tool_calls.forEach((toolCallDelta) => {
|
|
627
|
+
const toolCallId = toolCallIdMap.get(runStepDelta.id);
|
|
628
|
+
|
|
629
|
+
const contentPart: t.MessageContentComplex = {
|
|
630
|
+
type: ContentTypes.TOOL_CALL,
|
|
631
|
+
tool_call: {
|
|
632
|
+
args: toolCallDelta.args ?? '',
|
|
633
|
+
name: toolCallDelta.name,
|
|
634
|
+
id: toolCallId,
|
|
635
|
+
},
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
updateContent(runStep.index, contentPart);
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
} else if (event === GraphEvents.ON_RUN_STEP_COMPLETED) {
|
|
642
|
+
const { result } = data as unknown as { result: t.ToolEndEvent };
|
|
643
|
+
|
|
644
|
+
const { id: stepId } = result;
|
|
645
|
+
|
|
646
|
+
const runStep = stepMap.get(stepId);
|
|
647
|
+
if (!runStep) {
|
|
648
|
+
console.warn(
|
|
649
|
+
'No run step or runId found for completed tool call event'
|
|
650
|
+
);
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const contentPart: t.MessageContentComplex = {
|
|
655
|
+
type: ContentTypes.TOOL_CALL,
|
|
656
|
+
tool_call: result.tool_call,
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
updateContent(runStep.index, contentPart, true);
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
return { contentParts, aggregateContent, stepMap };
|
|
664
|
+
}
|