@mastra/ai-sdk 0.0.0-scorer-agentnames-conditional-20250926065249 → 0.0.0-scorers-logs-20251208093427
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/CHANGELOG.md +671 -3
- package/README.md +125 -1
- package/dist/__tests__/__fixtures__/network.stream.d.ts +2329 -0
- package/dist/__tests__/__fixtures__/network.stream.d.ts.map +1 -0
- package/dist/chat-route.d.ts +101 -0
- package/dist/chat-route.d.ts.map +1 -0
- package/dist/convert-messages.d.ts +10 -0
- package/dist/convert-messages.d.ts.map +1 -0
- package/dist/convert-streams.d.ts +81 -0
- package/dist/convert-streams.d.ts.map +1 -0
- package/dist/helpers.d.ts +37 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/index.cjs +1545 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +11 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1539 -12
- package/dist/index.js.map +1 -1
- package/dist/network-route.d.ts +75 -0
- package/dist/network-route.d.ts.map +1 -0
- package/dist/to-ai-sdk-format.d.ts +16 -0
- package/dist/to-ai-sdk-format.d.ts.map +1 -0
- package/dist/transformers.d.ts +259 -0
- package/dist/transformers.d.ts.map +1 -0
- package/dist/ui.cjs +16 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.ts +2 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +13 -0
- package/dist/ui.js.map +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/workflow-route.d.ts +77 -0
- package/dist/workflow-route.d.ts.map +1 -0
- package/package.json +25 -7
package/dist/index.js
CHANGED
|
@@ -1,10 +1,1295 @@
|
|
|
1
1
|
import { registerApiRoute } from '@mastra/core/server';
|
|
2
|
+
import { createUIMessageStream, createUIMessageStreamResponse } from 'ai';
|
|
3
|
+
import { DefaultGeneratedFile, DefaultGeneratedFileWithType } from '@mastra/core/stream';
|
|
2
4
|
|
|
3
|
-
// src/
|
|
5
|
+
// src/chat-route.ts
|
|
6
|
+
|
|
7
|
+
// src/utils.ts
|
|
8
|
+
var isDataChunkType = (chunk) => {
|
|
9
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("data-");
|
|
10
|
+
};
|
|
11
|
+
var isMastraTextStreamChunk = (chunk) => {
|
|
12
|
+
return chunk && typeof chunk === "object" && "type" in chunk && typeof chunk.type === "string" && [
|
|
13
|
+
"text-start",
|
|
14
|
+
"text-delta",
|
|
15
|
+
"text-end",
|
|
16
|
+
"reasoning-start",
|
|
17
|
+
"reasoning-delta",
|
|
18
|
+
"reasoning-end",
|
|
19
|
+
"file",
|
|
20
|
+
"source",
|
|
21
|
+
"tool-input-start",
|
|
22
|
+
"tool-input-delta",
|
|
23
|
+
"tool-call",
|
|
24
|
+
"tool-result",
|
|
25
|
+
"tool-error",
|
|
26
|
+
"error",
|
|
27
|
+
"start-step",
|
|
28
|
+
"finish-step",
|
|
29
|
+
"start",
|
|
30
|
+
"finish",
|
|
31
|
+
"abort",
|
|
32
|
+
"tool-input-end",
|
|
33
|
+
"raw"
|
|
34
|
+
].includes(chunk.type);
|
|
35
|
+
};
|
|
36
|
+
function safeParseErrorObject(obj) {
|
|
37
|
+
if (typeof obj !== "object" || obj === null) {
|
|
38
|
+
return String(obj);
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const stringified = JSON.stringify(obj);
|
|
42
|
+
if (stringified === "{}") {
|
|
43
|
+
return String(obj);
|
|
44
|
+
}
|
|
45
|
+
return stringified;
|
|
46
|
+
} catch {
|
|
47
|
+
return String(obj);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
var isAgentExecutionDataChunkType = (chunk) => {
|
|
51
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("agent-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
|
|
52
|
+
};
|
|
53
|
+
var isWorkflowExecutionDataChunkType = (chunk) => {
|
|
54
|
+
return chunk && typeof chunk === "object" && "type" in chunk && chunk.type?.startsWith("workflow-execution-event-") && "payload" in chunk && typeof chunk.payload === "object" && "type" in chunk.payload && chunk.payload.type?.startsWith("data-");
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/helpers.ts
|
|
58
|
+
function convertMastraChunkToAISDKv5({
|
|
59
|
+
chunk,
|
|
60
|
+
mode = "stream"
|
|
61
|
+
}) {
|
|
62
|
+
switch (chunk.type) {
|
|
63
|
+
case "start":
|
|
64
|
+
return {
|
|
65
|
+
type: "start"
|
|
66
|
+
};
|
|
67
|
+
case "step-start":
|
|
68
|
+
const { messageId: _messageId, ...rest } = chunk.payload;
|
|
69
|
+
return {
|
|
70
|
+
type: "start-step",
|
|
71
|
+
request: rest.request,
|
|
72
|
+
warnings: rest.warnings || []
|
|
73
|
+
};
|
|
74
|
+
case "raw":
|
|
75
|
+
return {
|
|
76
|
+
type: "raw",
|
|
77
|
+
rawValue: chunk.payload
|
|
78
|
+
};
|
|
79
|
+
case "finish": {
|
|
80
|
+
return {
|
|
81
|
+
type: "finish",
|
|
82
|
+
finishReason: chunk.payload.stepResult.reason,
|
|
83
|
+
totalUsage: chunk.payload.output.usage
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
case "reasoning-start":
|
|
87
|
+
return {
|
|
88
|
+
type: "reasoning-start",
|
|
89
|
+
id: chunk.payload.id,
|
|
90
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
91
|
+
};
|
|
92
|
+
case "reasoning-delta":
|
|
93
|
+
return {
|
|
94
|
+
type: "reasoning-delta",
|
|
95
|
+
id: chunk.payload.id,
|
|
96
|
+
text: chunk.payload.text,
|
|
97
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
98
|
+
};
|
|
99
|
+
case "reasoning-signature":
|
|
100
|
+
throw new Error('AISDKv5 chunk type "reasoning-signature" not supported');
|
|
101
|
+
// return {
|
|
102
|
+
// type: 'reasoning-signature' as const,
|
|
103
|
+
// id: chunk.payload.id,
|
|
104
|
+
// signature: chunk.payload.signature,
|
|
105
|
+
// };
|
|
106
|
+
case "redacted-reasoning":
|
|
107
|
+
throw new Error('AISDKv5 chunk type "redacted-reasoning" not supported');
|
|
108
|
+
// return {
|
|
109
|
+
// type: 'redacted-reasoning',
|
|
110
|
+
// id: chunk.payload.id,
|
|
111
|
+
// data: chunk.payload.data,
|
|
112
|
+
// };
|
|
113
|
+
case "reasoning-end":
|
|
114
|
+
return {
|
|
115
|
+
type: "reasoning-end",
|
|
116
|
+
id: chunk.payload.id,
|
|
117
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
118
|
+
};
|
|
119
|
+
case "source":
|
|
120
|
+
if (chunk.payload.sourceType === "url") {
|
|
121
|
+
return {
|
|
122
|
+
type: "source",
|
|
123
|
+
sourceType: "url",
|
|
124
|
+
id: chunk.payload.id,
|
|
125
|
+
url: chunk.payload.url,
|
|
126
|
+
title: chunk.payload.title,
|
|
127
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
128
|
+
};
|
|
129
|
+
} else {
|
|
130
|
+
return {
|
|
131
|
+
type: "source",
|
|
132
|
+
sourceType: "document",
|
|
133
|
+
id: chunk.payload.id,
|
|
134
|
+
mediaType: chunk.payload.mimeType,
|
|
135
|
+
title: chunk.payload.title,
|
|
136
|
+
filename: chunk.payload.filename,
|
|
137
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
case "file":
|
|
141
|
+
if (mode === "generate") {
|
|
142
|
+
return {
|
|
143
|
+
type: "file",
|
|
144
|
+
file: new DefaultGeneratedFile({
|
|
145
|
+
data: chunk.payload.data,
|
|
146
|
+
mediaType: chunk.payload.mimeType
|
|
147
|
+
})
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
type: "file",
|
|
152
|
+
file: new DefaultGeneratedFileWithType({
|
|
153
|
+
data: chunk.payload.data,
|
|
154
|
+
mediaType: chunk.payload.mimeType
|
|
155
|
+
})
|
|
156
|
+
};
|
|
157
|
+
case "tool-call":
|
|
158
|
+
return {
|
|
159
|
+
type: "tool-call",
|
|
160
|
+
toolCallId: chunk.payload.toolCallId,
|
|
161
|
+
providerMetadata: chunk.payload.providerMetadata,
|
|
162
|
+
providerExecuted: chunk.payload.providerExecuted,
|
|
163
|
+
toolName: chunk.payload.toolName,
|
|
164
|
+
input: chunk.payload.args
|
|
165
|
+
};
|
|
166
|
+
case "tool-call-approval":
|
|
167
|
+
return {
|
|
168
|
+
type: "data-tool-call-approval",
|
|
169
|
+
id: chunk.payload.toolCallId,
|
|
170
|
+
data: {
|
|
171
|
+
runId: chunk.runId,
|
|
172
|
+
toolCallId: chunk.payload.toolCallId,
|
|
173
|
+
toolName: chunk.payload.toolName,
|
|
174
|
+
args: chunk.payload.args
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
case "tool-call-suspended":
|
|
178
|
+
return {
|
|
179
|
+
type: "data-tool-call-suspended",
|
|
180
|
+
id: chunk.payload.toolCallId,
|
|
181
|
+
data: {
|
|
182
|
+
runId: chunk.runId,
|
|
183
|
+
toolCallId: chunk.payload.toolCallId,
|
|
184
|
+
toolName: chunk.payload.toolName,
|
|
185
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
case "tool-call-input-streaming-start":
|
|
189
|
+
return {
|
|
190
|
+
type: "tool-input-start",
|
|
191
|
+
id: chunk.payload.toolCallId,
|
|
192
|
+
toolName: chunk.payload.toolName,
|
|
193
|
+
dynamic: !!chunk.payload.dynamic,
|
|
194
|
+
providerMetadata: chunk.payload.providerMetadata,
|
|
195
|
+
providerExecuted: chunk.payload.providerExecuted
|
|
196
|
+
};
|
|
197
|
+
case "tool-call-input-streaming-end":
|
|
198
|
+
return {
|
|
199
|
+
type: "tool-input-end",
|
|
200
|
+
id: chunk.payload.toolCallId,
|
|
201
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
202
|
+
};
|
|
203
|
+
case "tool-call-delta":
|
|
204
|
+
return {
|
|
205
|
+
type: "tool-input-delta",
|
|
206
|
+
id: chunk.payload.toolCallId,
|
|
207
|
+
delta: chunk.payload.argsTextDelta,
|
|
208
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
209
|
+
};
|
|
210
|
+
case "step-finish": {
|
|
211
|
+
const { request: _request, providerMetadata, ...rest2 } = chunk.payload.metadata;
|
|
212
|
+
return {
|
|
213
|
+
type: "finish-step",
|
|
214
|
+
response: {
|
|
215
|
+
id: chunk.payload.id || "",
|
|
216
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
217
|
+
modelId: rest2.modelId || "",
|
|
218
|
+
...rest2
|
|
219
|
+
},
|
|
220
|
+
usage: chunk.payload.output.usage,
|
|
221
|
+
finishReason: chunk.payload.stepResult.reason,
|
|
222
|
+
providerMetadata
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
case "text-delta":
|
|
226
|
+
return {
|
|
227
|
+
type: "text-delta",
|
|
228
|
+
id: chunk.payload.id,
|
|
229
|
+
text: chunk.payload.text,
|
|
230
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
231
|
+
};
|
|
232
|
+
case "text-end":
|
|
233
|
+
return {
|
|
234
|
+
type: "text-end",
|
|
235
|
+
id: chunk.payload.id,
|
|
236
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
237
|
+
};
|
|
238
|
+
case "text-start":
|
|
239
|
+
return {
|
|
240
|
+
type: "text-start",
|
|
241
|
+
id: chunk.payload.id,
|
|
242
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
243
|
+
};
|
|
244
|
+
case "tool-result":
|
|
245
|
+
return {
|
|
246
|
+
type: "tool-result",
|
|
247
|
+
input: chunk.payload.args,
|
|
248
|
+
toolCallId: chunk.payload.toolCallId,
|
|
249
|
+
providerExecuted: chunk.payload.providerExecuted,
|
|
250
|
+
toolName: chunk.payload.toolName,
|
|
251
|
+
output: chunk.payload.result
|
|
252
|
+
// providerMetadata: chunk.payload.providerMetadata, // AI v5 types don't show this?
|
|
253
|
+
};
|
|
254
|
+
case "tool-error":
|
|
255
|
+
return {
|
|
256
|
+
type: "tool-error",
|
|
257
|
+
error: chunk.payload.error,
|
|
258
|
+
input: chunk.payload.args,
|
|
259
|
+
toolCallId: chunk.payload.toolCallId,
|
|
260
|
+
providerExecuted: chunk.payload.providerExecuted,
|
|
261
|
+
toolName: chunk.payload.toolName
|
|
262
|
+
// providerMetadata: chunk.payload.providerMetadata, // AI v5 types don't show this?
|
|
263
|
+
};
|
|
264
|
+
case "abort":
|
|
265
|
+
return {
|
|
266
|
+
type: "abort"
|
|
267
|
+
};
|
|
268
|
+
case "error":
|
|
269
|
+
return {
|
|
270
|
+
type: "error",
|
|
271
|
+
error: chunk.payload.error
|
|
272
|
+
};
|
|
273
|
+
case "object":
|
|
274
|
+
return {
|
|
275
|
+
type: "object",
|
|
276
|
+
object: chunk.object
|
|
277
|
+
};
|
|
278
|
+
case "tripwire":
|
|
279
|
+
return {
|
|
280
|
+
type: "data-tripwire",
|
|
281
|
+
data: {
|
|
282
|
+
tripwireReason: chunk.payload.tripwireReason
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
default:
|
|
286
|
+
if (chunk.type && "payload" in chunk && chunk.payload) {
|
|
287
|
+
return {
|
|
288
|
+
type: chunk.type,
|
|
289
|
+
...chunk.payload || {}
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
if ("type" in chunk && chunk.type?.startsWith("data-")) {
|
|
293
|
+
return chunk;
|
|
294
|
+
}
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function convertFullStreamChunkToUIMessageStream({
|
|
299
|
+
part,
|
|
300
|
+
messageMetadataValue,
|
|
301
|
+
sendReasoning,
|
|
302
|
+
sendSources,
|
|
303
|
+
onError,
|
|
304
|
+
sendStart,
|
|
305
|
+
sendFinish,
|
|
306
|
+
responseMessageId
|
|
307
|
+
}) {
|
|
308
|
+
const partType = part?.type;
|
|
309
|
+
switch (partType) {
|
|
310
|
+
case "text-start": {
|
|
311
|
+
return {
|
|
312
|
+
type: "text-start",
|
|
313
|
+
id: part.id,
|
|
314
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
case "text-delta": {
|
|
318
|
+
return {
|
|
319
|
+
type: "text-delta",
|
|
320
|
+
id: part.id,
|
|
321
|
+
delta: part.text,
|
|
322
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
case "text-end": {
|
|
326
|
+
return {
|
|
327
|
+
type: "text-end",
|
|
328
|
+
id: part.id,
|
|
329
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
case "reasoning-start": {
|
|
333
|
+
return {
|
|
334
|
+
type: "reasoning-start",
|
|
335
|
+
id: part.id,
|
|
336
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
case "reasoning-delta": {
|
|
340
|
+
if (sendReasoning) {
|
|
341
|
+
return {
|
|
342
|
+
type: "reasoning-delta",
|
|
343
|
+
id: part.id,
|
|
344
|
+
delta: part.text,
|
|
345
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
case "reasoning-end": {
|
|
351
|
+
return {
|
|
352
|
+
type: "reasoning-end",
|
|
353
|
+
id: part.id,
|
|
354
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
case "file": {
|
|
358
|
+
return {
|
|
359
|
+
type: "file",
|
|
360
|
+
mediaType: part.file.mediaType,
|
|
361
|
+
url: `data:${part.file.mediaType};base64,${part.file.base64}`
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
case "source": {
|
|
365
|
+
if (sendSources && part.sourceType === "url") {
|
|
366
|
+
return {
|
|
367
|
+
type: "source-url",
|
|
368
|
+
sourceId: part.id,
|
|
369
|
+
url: part.url,
|
|
370
|
+
title: part.title,
|
|
371
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
if (sendSources && part.sourceType === "document") {
|
|
375
|
+
return {
|
|
376
|
+
type: "source-document",
|
|
377
|
+
sourceId: part.id,
|
|
378
|
+
mediaType: part.mediaType,
|
|
379
|
+
title: part.title,
|
|
380
|
+
filename: part.filename,
|
|
381
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
case "tool-input-start": {
|
|
387
|
+
return {
|
|
388
|
+
type: "tool-input-start",
|
|
389
|
+
toolCallId: part.id,
|
|
390
|
+
toolName: part.toolName,
|
|
391
|
+
...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
|
|
392
|
+
...part.dynamic != null ? { dynamic: part.dynamic } : {}
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
case "tool-input-delta": {
|
|
396
|
+
return {
|
|
397
|
+
type: "tool-input-delta",
|
|
398
|
+
toolCallId: part.id,
|
|
399
|
+
inputTextDelta: part.delta
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
case "tool-call": {
|
|
403
|
+
return {
|
|
404
|
+
type: "tool-input-available",
|
|
405
|
+
toolCallId: part.toolCallId,
|
|
406
|
+
toolName: part.toolName,
|
|
407
|
+
input: part.input,
|
|
408
|
+
...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
|
|
409
|
+
...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
|
|
410
|
+
...part.dynamic != null ? { dynamic: part.dynamic } : {}
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
case "tool-result": {
|
|
414
|
+
return {
|
|
415
|
+
type: "tool-output-available",
|
|
416
|
+
toolCallId: part.toolCallId,
|
|
417
|
+
output: part.output,
|
|
418
|
+
...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
|
|
419
|
+
...part.dynamic != null ? { dynamic: part.dynamic } : {}
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
case "tool-output": {
|
|
423
|
+
if (part.output.from === "AGENT") {
|
|
424
|
+
return {
|
|
425
|
+
type: "tool-agent",
|
|
426
|
+
toolCallId: part.toolCallId,
|
|
427
|
+
payload: part.output
|
|
428
|
+
};
|
|
429
|
+
} else if (part.output.from === "WORKFLOW") {
|
|
430
|
+
return {
|
|
431
|
+
type: "tool-workflow",
|
|
432
|
+
toolCallId: part.toolCallId,
|
|
433
|
+
payload: part.output
|
|
434
|
+
};
|
|
435
|
+
} else if (part.output.from === "NETWORK") {
|
|
436
|
+
return {
|
|
437
|
+
type: "tool-network",
|
|
438
|
+
toolCallId: part.toolCallId,
|
|
439
|
+
payload: part.output
|
|
440
|
+
};
|
|
441
|
+
} else if (isDataChunkType(part.output)) {
|
|
442
|
+
if (!("data" in part.output)) {
|
|
443
|
+
throw new Error(
|
|
444
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
445
|
+
${JSON.stringify(part)}`
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
return part.output;
|
|
449
|
+
}
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
case "tool-error": {
|
|
453
|
+
return {
|
|
454
|
+
type: "tool-output-error",
|
|
455
|
+
toolCallId: part.toolCallId,
|
|
456
|
+
errorText: onError(part.error),
|
|
457
|
+
...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
|
|
458
|
+
...part.dynamic != null ? { dynamic: part.dynamic } : {}
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
case "error": {
|
|
462
|
+
return {
|
|
463
|
+
type: "error",
|
|
464
|
+
errorText: onError(part.error)
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
case "start-step": {
|
|
468
|
+
return { type: "start-step" };
|
|
469
|
+
}
|
|
470
|
+
case "finish-step": {
|
|
471
|
+
return { type: "finish-step" };
|
|
472
|
+
}
|
|
473
|
+
case "start": {
|
|
474
|
+
if (sendStart) {
|
|
475
|
+
return {
|
|
476
|
+
type: "start",
|
|
477
|
+
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {},
|
|
478
|
+
...responseMessageId != null ? { messageId: responseMessageId } : {}
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
case "finish": {
|
|
484
|
+
if (sendFinish) {
|
|
485
|
+
return {
|
|
486
|
+
type: "finish",
|
|
487
|
+
...messageMetadataValue != null ? { messageMetadata: messageMetadataValue } : {}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
case "abort": {
|
|
493
|
+
return part;
|
|
494
|
+
}
|
|
495
|
+
case "tool-input-end": {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
case "raw": {
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
default: {
|
|
502
|
+
if (isDataChunkType(part)) {
|
|
503
|
+
if (!("data" in part)) {
|
|
504
|
+
throw new Error(
|
|
505
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
506
|
+
${JSON.stringify(part)}`
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
return part;
|
|
510
|
+
}
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// src/transformers.ts
|
|
517
|
+
var PRIMITIVE_CACHE_SYMBOL = Symbol("primitive-cache");
|
|
518
|
+
function WorkflowStreamToAISDKTransformer({
|
|
519
|
+
includeTextStreamParts
|
|
520
|
+
} = {}) {
|
|
521
|
+
const bufferedWorkflows = /* @__PURE__ */ new Map();
|
|
522
|
+
return new TransformStream({
|
|
523
|
+
start(controller) {
|
|
524
|
+
controller.enqueue({
|
|
525
|
+
type: "start"
|
|
526
|
+
});
|
|
527
|
+
},
|
|
528
|
+
flush(controller) {
|
|
529
|
+
controller.enqueue({
|
|
530
|
+
type: "finish"
|
|
531
|
+
});
|
|
532
|
+
},
|
|
533
|
+
transform(chunk, controller) {
|
|
534
|
+
const transformed = transformWorkflow(chunk, bufferedWorkflows, false, includeTextStreamParts);
|
|
535
|
+
if (transformed) controller.enqueue(transformed);
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
function AgentNetworkToAISDKTransformer() {
|
|
540
|
+
const bufferedNetworks = /* @__PURE__ */ new Map();
|
|
541
|
+
return new TransformStream({
|
|
542
|
+
start(controller) {
|
|
543
|
+
controller.enqueue({
|
|
544
|
+
type: "start"
|
|
545
|
+
});
|
|
546
|
+
},
|
|
547
|
+
flush(controller) {
|
|
548
|
+
controller.enqueue({
|
|
549
|
+
type: "finish"
|
|
550
|
+
});
|
|
551
|
+
},
|
|
552
|
+
transform(chunk, controller) {
|
|
553
|
+
const transformed = transformNetwork(chunk, bufferedNetworks);
|
|
554
|
+
if (transformed) controller.enqueue(transformed);
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
function AgentStreamToAISDKTransformer({
|
|
559
|
+
lastMessageId,
|
|
560
|
+
sendStart,
|
|
561
|
+
sendFinish,
|
|
562
|
+
sendReasoning,
|
|
563
|
+
sendSources,
|
|
564
|
+
messageMetadata,
|
|
565
|
+
onError
|
|
566
|
+
}) {
|
|
567
|
+
let bufferedSteps = /* @__PURE__ */ new Map();
|
|
568
|
+
let tripwireOccurred = false;
|
|
569
|
+
let finishEventSent = false;
|
|
570
|
+
return new TransformStream({
|
|
571
|
+
transform(chunk, controller) {
|
|
572
|
+
if (chunk.type === "tripwire") {
|
|
573
|
+
tripwireOccurred = true;
|
|
574
|
+
}
|
|
575
|
+
if (chunk.type === "finish") {
|
|
576
|
+
finishEventSent = true;
|
|
577
|
+
}
|
|
578
|
+
const part = convertMastraChunkToAISDKv5({ chunk, mode: "stream" });
|
|
579
|
+
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
580
|
+
part,
|
|
581
|
+
sendReasoning,
|
|
582
|
+
sendSources,
|
|
583
|
+
messageMetadataValue: messageMetadata?.({ part }),
|
|
584
|
+
sendStart,
|
|
585
|
+
sendFinish,
|
|
586
|
+
responseMessageId: lastMessageId,
|
|
587
|
+
onError(error) {
|
|
588
|
+
return onError ? onError(error) : safeParseErrorObject(error);
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
if (transformedChunk) {
|
|
592
|
+
if (transformedChunk.type === "tool-agent") {
|
|
593
|
+
const payload = transformedChunk.payload;
|
|
594
|
+
const agentTransformed = transformAgent(payload, bufferedSteps);
|
|
595
|
+
if (agentTransformed) controller.enqueue(agentTransformed);
|
|
596
|
+
} else if (transformedChunk.type === "tool-workflow") {
|
|
597
|
+
const payload = transformedChunk.payload;
|
|
598
|
+
const workflowChunk = transformWorkflow(payload, bufferedSteps, true);
|
|
599
|
+
if (workflowChunk) controller.enqueue(workflowChunk);
|
|
600
|
+
} else if (transformedChunk.type === "tool-network") {
|
|
601
|
+
const payload = transformedChunk.payload;
|
|
602
|
+
const networkChunk = transformNetwork(payload, bufferedSteps, true);
|
|
603
|
+
if (networkChunk) controller.enqueue(networkChunk);
|
|
604
|
+
} else {
|
|
605
|
+
controller.enqueue(transformedChunk);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
},
|
|
609
|
+
flush(controller) {
|
|
610
|
+
if (tripwireOccurred && !finishEventSent && sendFinish) {
|
|
611
|
+
controller.enqueue({
|
|
612
|
+
type: "finish",
|
|
613
|
+
finishReason: "other"
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
function transformAgent(payload, bufferedSteps) {
|
|
620
|
+
let hasChanged = false;
|
|
621
|
+
switch (payload.type) {
|
|
622
|
+
case "start":
|
|
623
|
+
bufferedSteps.set(payload.runId, {
|
|
624
|
+
id: payload.payload.id,
|
|
625
|
+
object: null,
|
|
626
|
+
finishReason: null,
|
|
627
|
+
usage: null,
|
|
628
|
+
warnings: [],
|
|
629
|
+
text: "",
|
|
630
|
+
reasoning: [],
|
|
631
|
+
sources: [],
|
|
632
|
+
files: [],
|
|
633
|
+
toolCalls: [],
|
|
634
|
+
toolResults: [],
|
|
635
|
+
request: {},
|
|
636
|
+
response: {
|
|
637
|
+
id: "",
|
|
638
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
639
|
+
modelId: "",
|
|
640
|
+
messages: []
|
|
641
|
+
},
|
|
642
|
+
providerMetadata: void 0,
|
|
643
|
+
steps: [],
|
|
644
|
+
status: "running"
|
|
645
|
+
});
|
|
646
|
+
hasChanged = true;
|
|
647
|
+
break;
|
|
648
|
+
case "finish":
|
|
649
|
+
bufferedSteps.set(payload.runId, {
|
|
650
|
+
...bufferedSteps.get(payload.runId),
|
|
651
|
+
finishReason: payload.payload.stepResult.reason,
|
|
652
|
+
usage: payload.payload?.output?.usage,
|
|
653
|
+
warnings: payload.payload?.stepResult?.warnings,
|
|
654
|
+
steps: bufferedSteps.get(payload.runId).steps,
|
|
655
|
+
status: "finished"
|
|
656
|
+
});
|
|
657
|
+
hasChanged = true;
|
|
658
|
+
break;
|
|
659
|
+
case "text-delta":
|
|
660
|
+
const prevData = bufferedSteps.get(payload.runId);
|
|
661
|
+
bufferedSteps.set(payload.runId, {
|
|
662
|
+
...prevData,
|
|
663
|
+
text: `${prevData.text}${payload.payload.text}`
|
|
664
|
+
});
|
|
665
|
+
hasChanged = true;
|
|
666
|
+
break;
|
|
667
|
+
case "reasoning-delta":
|
|
668
|
+
bufferedSteps.set(payload.runId, {
|
|
669
|
+
...bufferedSteps.get(payload.runId),
|
|
670
|
+
reasoning: [...bufferedSteps.get(payload.runId).reasoning, payload.payload.text]
|
|
671
|
+
});
|
|
672
|
+
hasChanged = true;
|
|
673
|
+
break;
|
|
674
|
+
case "source":
|
|
675
|
+
bufferedSteps.set(payload.runId, {
|
|
676
|
+
...bufferedSteps.get(payload.runId),
|
|
677
|
+
sources: [...bufferedSteps.get(payload.runId).sources, payload.payload]
|
|
678
|
+
});
|
|
679
|
+
hasChanged = true;
|
|
680
|
+
break;
|
|
681
|
+
case "file":
|
|
682
|
+
bufferedSteps.set(payload.runId, {
|
|
683
|
+
...bufferedSteps.get(payload.runId),
|
|
684
|
+
files: [...bufferedSteps.get(payload.runId).files, payload.payload]
|
|
685
|
+
});
|
|
686
|
+
hasChanged = true;
|
|
687
|
+
break;
|
|
688
|
+
case "tool-call":
|
|
689
|
+
bufferedSteps.set(payload.runId, {
|
|
690
|
+
...bufferedSteps.get(payload.runId),
|
|
691
|
+
toolCalls: [...bufferedSteps.get(payload.runId).toolCalls, payload.payload]
|
|
692
|
+
});
|
|
693
|
+
hasChanged = true;
|
|
694
|
+
break;
|
|
695
|
+
case "tool-result":
|
|
696
|
+
bufferedSteps.set(payload.runId, {
|
|
697
|
+
...bufferedSteps.get(payload.runId),
|
|
698
|
+
toolResults: [...bufferedSteps.get(payload.runId).toolResults, payload.payload]
|
|
699
|
+
});
|
|
700
|
+
hasChanged = true;
|
|
701
|
+
break;
|
|
702
|
+
case "object-result":
|
|
703
|
+
bufferedSteps.set(payload.runId, {
|
|
704
|
+
...bufferedSteps.get(payload.runId),
|
|
705
|
+
object: payload.object
|
|
706
|
+
});
|
|
707
|
+
hasChanged = true;
|
|
708
|
+
break;
|
|
709
|
+
case "object":
|
|
710
|
+
bufferedSteps.set(payload.runId, {
|
|
711
|
+
...bufferedSteps.get(payload.runId),
|
|
712
|
+
object: payload.object
|
|
713
|
+
});
|
|
714
|
+
hasChanged = true;
|
|
715
|
+
break;
|
|
716
|
+
case "step-finish":
|
|
717
|
+
const currentRun = bufferedSteps.get(payload.runId);
|
|
718
|
+
const stepResult = {
|
|
719
|
+
...bufferedSteps.get(payload.runId),
|
|
720
|
+
stepType: currentRun.steps.length === 0 ? "initial" : "tool-result",
|
|
721
|
+
reasoningText: bufferedSteps.get(payload.runId).reasoning.join(""),
|
|
722
|
+
staticToolCalls: bufferedSteps.get(payload.runId).toolCalls.filter((part) => part.type === "tool-call" && part.payload?.dynamic === false),
|
|
723
|
+
dynamicToolCalls: bufferedSteps.get(payload.runId).toolCalls.filter((part) => part.type === "tool-call" && part.payload?.dynamic === true),
|
|
724
|
+
staticToolResults: bufferedSteps.get(payload.runId).toolResults.filter((part) => part.type === "tool-result" && part.payload?.dynamic === false),
|
|
725
|
+
dynamicToolResults: bufferedSteps.get(payload.runId).toolResults.filter((part) => part.type === "tool-result" && part.payload?.dynamic === true),
|
|
726
|
+
finishReason: payload.payload.stepResult.reason,
|
|
727
|
+
usage: payload.payload.output.usage,
|
|
728
|
+
warnings: payload.payload.stepResult.warnings || [],
|
|
729
|
+
response: {
|
|
730
|
+
id: payload.payload.id || "",
|
|
731
|
+
timestamp: payload.payload.metadata?.timestamp || /* @__PURE__ */ new Date(),
|
|
732
|
+
modelId: payload.payload.metadata?.modelId || payload.payload.metadata?.model || "",
|
|
733
|
+
...bufferedSteps.get(payload.runId).response,
|
|
734
|
+
messages: bufferedSteps.get(payload.runId).response.messages || []
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
bufferedSteps.set(payload.runId, {
|
|
738
|
+
...bufferedSteps.get(payload.runId),
|
|
739
|
+
usage: payload.payload.output.usage,
|
|
740
|
+
warnings: payload.payload.stepResult.warnings || [],
|
|
741
|
+
steps: [...bufferedSteps.get(payload.runId).steps, stepResult]
|
|
742
|
+
});
|
|
743
|
+
hasChanged = true;
|
|
744
|
+
break;
|
|
745
|
+
}
|
|
746
|
+
if (hasChanged) {
|
|
747
|
+
return {
|
|
748
|
+
type: "data-tool-agent",
|
|
749
|
+
id: payload.runId,
|
|
750
|
+
data: bufferedSteps.get(payload.runId)
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
return null;
|
|
754
|
+
}
|
|
755
|
+
function transformWorkflow(payload, bufferedWorkflows, isNested, includeTextStreamParts) {
|
|
756
|
+
switch (payload.type) {
|
|
757
|
+
case "workflow-start":
|
|
758
|
+
bufferedWorkflows.set(payload.runId, {
|
|
759
|
+
name: payload.payload.workflowId,
|
|
760
|
+
steps: {}
|
|
761
|
+
});
|
|
762
|
+
return {
|
|
763
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
764
|
+
id: payload.runId,
|
|
765
|
+
data: {
|
|
766
|
+
name: bufferedWorkflows.get(payload.runId).name,
|
|
767
|
+
status: "running",
|
|
768
|
+
steps: bufferedWorkflows.get(payload.runId).steps,
|
|
769
|
+
output: null
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
case "workflow-step-start": {
|
|
773
|
+
const current = bufferedWorkflows.get(payload.runId) || { name: "", steps: {} };
|
|
774
|
+
current.steps[payload.payload.id] = {
|
|
775
|
+
name: payload.payload.id,
|
|
776
|
+
status: payload.payload.status,
|
|
777
|
+
input: payload.payload.payload ?? null,
|
|
778
|
+
output: null,
|
|
779
|
+
suspendPayload: null,
|
|
780
|
+
resumePayload: null
|
|
781
|
+
};
|
|
782
|
+
bufferedWorkflows.set(payload.runId, current);
|
|
783
|
+
return {
|
|
784
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
785
|
+
id: payload.runId,
|
|
786
|
+
data: {
|
|
787
|
+
name: current.name,
|
|
788
|
+
status: "running",
|
|
789
|
+
steps: current.steps,
|
|
790
|
+
output: null
|
|
791
|
+
}
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
case "workflow-step-result": {
|
|
795
|
+
const current = bufferedWorkflows.get(payload.runId);
|
|
796
|
+
if (!current) return null;
|
|
797
|
+
current.steps[payload.payload.id] = {
|
|
798
|
+
...current.steps[payload.payload.id],
|
|
799
|
+
status: payload.payload.status,
|
|
800
|
+
output: payload.payload.output ?? null
|
|
801
|
+
};
|
|
802
|
+
return {
|
|
803
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
804
|
+
id: payload.runId,
|
|
805
|
+
data: {
|
|
806
|
+
name: current.name,
|
|
807
|
+
status: "running",
|
|
808
|
+
steps: current.steps,
|
|
809
|
+
output: null
|
|
810
|
+
}
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
case "workflow-step-suspended": {
|
|
814
|
+
const current = bufferedWorkflows.get(payload.runId);
|
|
815
|
+
if (!current) return null;
|
|
816
|
+
current.steps[payload.payload.id] = {
|
|
817
|
+
...current.steps[payload.payload.id],
|
|
818
|
+
status: payload.payload.status,
|
|
819
|
+
suspendPayload: payload.payload.suspendPayload ?? null,
|
|
820
|
+
resumePayload: payload.payload.resumePayload ?? null,
|
|
821
|
+
output: null
|
|
822
|
+
};
|
|
823
|
+
return {
|
|
824
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
825
|
+
id: payload.runId,
|
|
826
|
+
data: {
|
|
827
|
+
name: current.name,
|
|
828
|
+
status: "suspended",
|
|
829
|
+
steps: current.steps,
|
|
830
|
+
output: null
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
case "workflow-finish": {
|
|
835
|
+
const current = bufferedWorkflows.get(payload.runId);
|
|
836
|
+
if (!current) return null;
|
|
837
|
+
return {
|
|
838
|
+
type: isNested ? "data-tool-workflow" : "data-workflow",
|
|
839
|
+
id: payload.runId,
|
|
840
|
+
data: {
|
|
841
|
+
name: current.name,
|
|
842
|
+
steps: current.steps,
|
|
843
|
+
output: payload.payload.output ?? null,
|
|
844
|
+
status: payload.payload.workflowStatus
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
case "workflow-step-output": {
|
|
849
|
+
const output = payload.payload.output;
|
|
850
|
+
if (includeTextStreamParts && output && isMastraTextStreamChunk(output)) {
|
|
851
|
+
const part = convertMastraChunkToAISDKv5({ chunk: output, mode: "stream" });
|
|
852
|
+
const transformedChunk = convertFullStreamChunkToUIMessageStream({
|
|
853
|
+
part,
|
|
854
|
+
onError(error) {
|
|
855
|
+
return safeParseErrorObject(error);
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
return transformedChunk;
|
|
859
|
+
}
|
|
860
|
+
if (output && isDataChunkType(output)) {
|
|
861
|
+
if (!("data" in output)) {
|
|
862
|
+
throw new Error(
|
|
863
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
864
|
+
${JSON.stringify(output)}`
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
return output;
|
|
868
|
+
}
|
|
869
|
+
return null;
|
|
870
|
+
}
|
|
871
|
+
default: {
|
|
872
|
+
if (isDataChunkType(payload)) {
|
|
873
|
+
if (!("data" in payload)) {
|
|
874
|
+
throw new Error(
|
|
875
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
876
|
+
${JSON.stringify(payload)}`
|
|
877
|
+
);
|
|
878
|
+
}
|
|
879
|
+
return payload;
|
|
880
|
+
}
|
|
881
|
+
return null;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
function transformNetwork(payload, bufferedNetworks, isNested) {
|
|
886
|
+
switch (payload.type) {
|
|
887
|
+
case "routing-agent-start": {
|
|
888
|
+
if (!bufferedNetworks.has(payload.runId)) {
|
|
889
|
+
bufferedNetworks.set(payload.runId, {
|
|
890
|
+
name: payload.payload.networkId,
|
|
891
|
+
steps: [],
|
|
892
|
+
usage: null,
|
|
893
|
+
output: null
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
897
|
+
current.steps.push({
|
|
898
|
+
id: payload.payload.runId,
|
|
899
|
+
name: payload.payload.agentId,
|
|
900
|
+
status: "running",
|
|
901
|
+
iteration: payload.payload.inputData.iteration,
|
|
902
|
+
input: {
|
|
903
|
+
task: payload.payload.inputData.task,
|
|
904
|
+
threadId: payload.payload.inputData.threadId,
|
|
905
|
+
threadResourceId: payload.payload.inputData.threadResourceId
|
|
906
|
+
},
|
|
907
|
+
output: "",
|
|
908
|
+
task: null,
|
|
909
|
+
suspendPayload: null,
|
|
910
|
+
resumePayload: null,
|
|
911
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
912
|
+
});
|
|
913
|
+
return {
|
|
914
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
915
|
+
id: payload.runId,
|
|
916
|
+
data: {
|
|
917
|
+
name: bufferedNetworks.get(payload.runId).name,
|
|
918
|
+
status: "running",
|
|
919
|
+
usage: null,
|
|
920
|
+
steps: bufferedNetworks.get(payload.runId).steps,
|
|
921
|
+
output: null
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
case "routing-agent-text-start": {
|
|
926
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
927
|
+
if (!current) return null;
|
|
928
|
+
return {
|
|
929
|
+
type: "text-start",
|
|
930
|
+
id: payload.runId
|
|
931
|
+
};
|
|
932
|
+
}
|
|
933
|
+
case "routing-agent-text-delta": {
|
|
934
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
935
|
+
if (!current) return null;
|
|
936
|
+
return {
|
|
937
|
+
type: "text-delta",
|
|
938
|
+
id: payload.runId,
|
|
939
|
+
delta: payload.payload.text
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
case "agent-execution-start": {
|
|
943
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
944
|
+
if (!current) return null;
|
|
945
|
+
current.steps.push({
|
|
946
|
+
id: payload.payload.runId,
|
|
947
|
+
name: payload.payload.agentId,
|
|
948
|
+
status: "running",
|
|
949
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
950
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
951
|
+
output: null,
|
|
952
|
+
task: null,
|
|
953
|
+
suspendPayload: null,
|
|
954
|
+
resumePayload: null,
|
|
955
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
956
|
+
});
|
|
957
|
+
bufferedNetworks.set(payload.runId, current);
|
|
958
|
+
return {
|
|
959
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
960
|
+
id: payload.runId,
|
|
961
|
+
data: {
|
|
962
|
+
...current,
|
|
963
|
+
status: "running"
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
case "workflow-execution-start": {
|
|
968
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
969
|
+
if (!current) return null;
|
|
970
|
+
current.steps.push({
|
|
971
|
+
id: payload.payload.runId,
|
|
972
|
+
name: payload.payload.workflowId,
|
|
973
|
+
status: "running",
|
|
974
|
+
iteration: payload.payload.args?.iteration ?? 0,
|
|
975
|
+
input: { prompt: payload.payload.args?.prompt ?? "" },
|
|
976
|
+
output: null,
|
|
977
|
+
task: null,
|
|
978
|
+
suspendPayload: null,
|
|
979
|
+
resumePayload: null,
|
|
980
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
981
|
+
});
|
|
982
|
+
bufferedNetworks.set(payload.runId, current);
|
|
983
|
+
return {
|
|
984
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
985
|
+
id: payload.runId,
|
|
986
|
+
data: {
|
|
987
|
+
...current,
|
|
988
|
+
status: "running"
|
|
989
|
+
}
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
case "tool-execution-start": {
|
|
993
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
994
|
+
if (!current) return null;
|
|
995
|
+
current.steps.push({
|
|
996
|
+
id: payload.payload.args.toolCallId,
|
|
997
|
+
name: payload.payload.args?.toolName,
|
|
998
|
+
status: "running",
|
|
999
|
+
iteration: payload.payload.args?.iteration ? Number(payload.payload.args.iteration) : 0,
|
|
1000
|
+
task: {
|
|
1001
|
+
id: payload.payload.args?.toolName
|
|
1002
|
+
},
|
|
1003
|
+
input: payload.payload.args?.args || null,
|
|
1004
|
+
output: null,
|
|
1005
|
+
suspendPayload: null,
|
|
1006
|
+
resumePayload: null,
|
|
1007
|
+
[PRIMITIVE_CACHE_SYMBOL]: /* @__PURE__ */ new Map()
|
|
1008
|
+
});
|
|
1009
|
+
bufferedNetworks.set(payload.runId, current);
|
|
1010
|
+
return {
|
|
1011
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1012
|
+
id: payload.runId,
|
|
1013
|
+
data: {
|
|
1014
|
+
...current,
|
|
1015
|
+
status: "running"
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
case "agent-execution-end": {
|
|
1020
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1021
|
+
if (!current) return null;
|
|
1022
|
+
const stepId = payload.payload.runId;
|
|
1023
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1024
|
+
if (!step) {
|
|
1025
|
+
return null;
|
|
1026
|
+
}
|
|
1027
|
+
step.status = "success";
|
|
1028
|
+
step.output = payload.payload.result;
|
|
1029
|
+
return {
|
|
1030
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1031
|
+
id: payload.runId,
|
|
1032
|
+
data: {
|
|
1033
|
+
...current,
|
|
1034
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
1035
|
+
status: "running",
|
|
1036
|
+
output: payload.payload.result ?? current.output
|
|
1037
|
+
}
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
case "tool-execution-end": {
|
|
1041
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1042
|
+
if (!current) return null;
|
|
1043
|
+
const stepId = payload.payload.toolCallId;
|
|
1044
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1045
|
+
if (!step) {
|
|
1046
|
+
return null;
|
|
1047
|
+
}
|
|
1048
|
+
step.status = "success";
|
|
1049
|
+
step.output = payload.payload.result;
|
|
1050
|
+
return {
|
|
1051
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1052
|
+
id: payload.runId,
|
|
1053
|
+
data: {
|
|
1054
|
+
...current,
|
|
1055
|
+
status: "running",
|
|
1056
|
+
output: payload.payload.result ?? current.output
|
|
1057
|
+
}
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
case "workflow-execution-end": {
|
|
1061
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1062
|
+
if (!current) return null;
|
|
1063
|
+
const stepId = payload.payload.runId;
|
|
1064
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1065
|
+
if (!step) {
|
|
1066
|
+
return null;
|
|
1067
|
+
}
|
|
1068
|
+
step.status = "success";
|
|
1069
|
+
step.output = payload.payload.result;
|
|
1070
|
+
return {
|
|
1071
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1072
|
+
id: payload.runId,
|
|
1073
|
+
data: {
|
|
1074
|
+
...current,
|
|
1075
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
1076
|
+
status: "running",
|
|
1077
|
+
output: payload.payload.result ?? current.output
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
case "routing-agent-end": {
|
|
1082
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1083
|
+
if (!current) return null;
|
|
1084
|
+
const stepId = payload.payload.runId;
|
|
1085
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1086
|
+
if (!step) {
|
|
1087
|
+
return null;
|
|
1088
|
+
}
|
|
1089
|
+
step.status = "success";
|
|
1090
|
+
step.task = {
|
|
1091
|
+
id: payload.payload.primitiveId,
|
|
1092
|
+
type: payload.payload.primitiveType,
|
|
1093
|
+
name: payload.payload.task,
|
|
1094
|
+
reason: payload.payload.selectionReason
|
|
1095
|
+
};
|
|
1096
|
+
step.output = payload.payload.result;
|
|
1097
|
+
return {
|
|
1098
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1099
|
+
id: payload.runId,
|
|
1100
|
+
data: {
|
|
1101
|
+
...current,
|
|
1102
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
1103
|
+
output: payload.payload?.result ?? current.output
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
case "network-execution-event-step-finish": {
|
|
1108
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1109
|
+
if (!current) return null;
|
|
1110
|
+
return {
|
|
1111
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1112
|
+
id: payload.runId,
|
|
1113
|
+
data: {
|
|
1114
|
+
...current,
|
|
1115
|
+
status: "finished",
|
|
1116
|
+
output: payload.payload?.result ?? current.output
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
case "network-execution-event-finish": {
|
|
1121
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1122
|
+
if (!current) return null;
|
|
1123
|
+
return {
|
|
1124
|
+
type: isNested ? "data-tool-network" : "data-network",
|
|
1125
|
+
id: payload.runId,
|
|
1126
|
+
data: {
|
|
1127
|
+
...current,
|
|
1128
|
+
usage: payload.payload?.usage ?? current.usage,
|
|
1129
|
+
status: "finished",
|
|
1130
|
+
output: payload.payload?.result ?? current.output
|
|
1131
|
+
}
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
default: {
|
|
1135
|
+
if (payload.type.startsWith("agent-execution-event-")) {
|
|
1136
|
+
const stepId = payload.payload.runId;
|
|
1137
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1138
|
+
if (!current) return null;
|
|
1139
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1140
|
+
if (!step) {
|
|
1141
|
+
return null;
|
|
1142
|
+
}
|
|
1143
|
+
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1144
|
+
const result = transformAgent(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1145
|
+
if (result) {
|
|
1146
|
+
const { request, response, ...data } = result.data;
|
|
1147
|
+
step.task = data;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
if (payload.type.startsWith("workflow-execution-event-")) {
|
|
1151
|
+
const stepId = payload.payload.runId;
|
|
1152
|
+
const current = bufferedNetworks.get(payload.runId);
|
|
1153
|
+
if (!current) return null;
|
|
1154
|
+
const step = current.steps.find((step2) => step2.id === stepId);
|
|
1155
|
+
if (!step) {
|
|
1156
|
+
return null;
|
|
1157
|
+
}
|
|
1158
|
+
step[PRIMITIVE_CACHE_SYMBOL] = step[PRIMITIVE_CACHE_SYMBOL] || /* @__PURE__ */ new Map();
|
|
1159
|
+
const result = transformWorkflow(payload.payload, step[PRIMITIVE_CACHE_SYMBOL]);
|
|
1160
|
+
if (result && "data" in result) {
|
|
1161
|
+
const data = result.data;
|
|
1162
|
+
step.task = data;
|
|
1163
|
+
if (data.name && step.task) {
|
|
1164
|
+
step.task.id = data.name;
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
if (isDataChunkType(payload)) {
|
|
1169
|
+
if (!("data" in payload)) {
|
|
1170
|
+
throw new Error(
|
|
1171
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1172
|
+
${JSON.stringify(payload)}`
|
|
1173
|
+
);
|
|
1174
|
+
}
|
|
1175
|
+
const { type, data } = payload;
|
|
1176
|
+
return { type, data };
|
|
1177
|
+
}
|
|
1178
|
+
if (isAgentExecutionDataChunkType(payload)) {
|
|
1179
|
+
if (!("data" in payload.payload)) {
|
|
1180
|
+
throw new Error(
|
|
1181
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1182
|
+
${JSON.stringify(payload)}`
|
|
1183
|
+
);
|
|
1184
|
+
}
|
|
1185
|
+
const { type, data } = payload.payload;
|
|
1186
|
+
return { type, data };
|
|
1187
|
+
}
|
|
1188
|
+
if (isWorkflowExecutionDataChunkType(payload)) {
|
|
1189
|
+
if (!("data" in payload.payload)) {
|
|
1190
|
+
throw new Error(
|
|
1191
|
+
`UI Messages require a data property when using data- prefixed chunks
|
|
1192
|
+
${JSON.stringify(payload)}`
|
|
1193
|
+
);
|
|
1194
|
+
}
|
|
1195
|
+
const { type, data } = payload.payload;
|
|
1196
|
+
return { type, data };
|
|
1197
|
+
}
|
|
1198
|
+
return null;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
// src/convert-streams.ts
|
|
1204
|
+
function toAISdkV5Stream(stream, options = {
|
|
1205
|
+
from: "agent",
|
|
1206
|
+
sendStart: true,
|
|
1207
|
+
sendFinish: true
|
|
1208
|
+
}) {
|
|
1209
|
+
const from = options?.from;
|
|
1210
|
+
if (from === "workflow") {
|
|
1211
|
+
const includeTextStreamParts = options?.includeTextStreamParts ?? true;
|
|
1212
|
+
return stream.pipeThrough(
|
|
1213
|
+
WorkflowStreamToAISDKTransformer({ includeTextStreamParts })
|
|
1214
|
+
);
|
|
1215
|
+
}
|
|
1216
|
+
if (from === "network") {
|
|
1217
|
+
return stream.pipeThrough(AgentNetworkToAISDKTransformer());
|
|
1218
|
+
}
|
|
1219
|
+
const agentReadable = "fullStream" in stream ? stream.fullStream : stream;
|
|
1220
|
+
return agentReadable.pipeThrough(
|
|
1221
|
+
AgentStreamToAISDKTransformer({
|
|
1222
|
+
lastMessageId: options?.lastMessageId,
|
|
1223
|
+
sendStart: options?.sendStart,
|
|
1224
|
+
sendFinish: options?.sendFinish,
|
|
1225
|
+
sendReasoning: options?.sendReasoning,
|
|
1226
|
+
sendSources: options?.sendSources,
|
|
1227
|
+
messageMetadata: options?.messageMetadata,
|
|
1228
|
+
onError: options?.onError
|
|
1229
|
+
})
|
|
1230
|
+
);
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
// src/chat-route.ts
|
|
1234
|
+
async function handleChatStream({
|
|
1235
|
+
mastra,
|
|
1236
|
+
agentId,
|
|
1237
|
+
params,
|
|
1238
|
+
defaultOptions,
|
|
1239
|
+
sendStart = true,
|
|
1240
|
+
sendFinish = true,
|
|
1241
|
+
sendReasoning = false,
|
|
1242
|
+
sendSources = false
|
|
1243
|
+
}) {
|
|
1244
|
+
const { messages, resumeData, runId, requestContext, ...rest } = params;
|
|
1245
|
+
if (resumeData && !runId) {
|
|
1246
|
+
throw new Error("runId is required when resumeData is provided");
|
|
1247
|
+
}
|
|
1248
|
+
const agentObj = mastra.getAgentById(agentId);
|
|
1249
|
+
if (!agentObj) {
|
|
1250
|
+
throw new Error(`Agent ${agentId} not found`);
|
|
1251
|
+
}
|
|
1252
|
+
if (!Array.isArray(messages)) {
|
|
1253
|
+
throw new Error("Messages must be an array of UIMessage objects");
|
|
1254
|
+
}
|
|
1255
|
+
const mergedOptions = {
|
|
1256
|
+
...defaultOptions,
|
|
1257
|
+
...rest,
|
|
1258
|
+
...runId && { runId },
|
|
1259
|
+
requestContext: requestContext || defaultOptions?.requestContext
|
|
1260
|
+
};
|
|
1261
|
+
const result = resumeData ? await agentObj.resumeStream(resumeData, mergedOptions) : await agentObj.stream(messages, mergedOptions);
|
|
1262
|
+
let lastMessageId;
|
|
1263
|
+
if (messages.length) {
|
|
1264
|
+
const lastMessage = messages[messages.length - 1];
|
|
1265
|
+
if (lastMessage?.role === "assistant") {
|
|
1266
|
+
lastMessageId = lastMessage.id;
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
return createUIMessageStream({
|
|
1270
|
+
originalMessages: messages,
|
|
1271
|
+
execute: async ({ writer }) => {
|
|
1272
|
+
for await (const part of toAISdkV5Stream(result, {
|
|
1273
|
+
from: "agent",
|
|
1274
|
+
lastMessageId,
|
|
1275
|
+
sendStart,
|
|
1276
|
+
sendFinish,
|
|
1277
|
+
sendReasoning,
|
|
1278
|
+
sendSources
|
|
1279
|
+
})) {
|
|
1280
|
+
writer.write(part);
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
4
1285
|
function chatRoute({
|
|
5
1286
|
path = "/chat/:agentId",
|
|
6
1287
|
agent,
|
|
7
|
-
defaultOptions
|
|
1288
|
+
defaultOptions,
|
|
1289
|
+
sendStart = true,
|
|
1290
|
+
sendFinish = true,
|
|
1291
|
+
sendReasoning = false,
|
|
1292
|
+
sendSources = false
|
|
8
1293
|
}) {
|
|
9
1294
|
if (!agent && !path.includes("/:agentId")) {
|
|
10
1295
|
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
@@ -33,6 +1318,14 @@ function chatRoute({
|
|
|
33
1318
|
schema: {
|
|
34
1319
|
type: "object",
|
|
35
1320
|
properties: {
|
|
1321
|
+
resumeData: {
|
|
1322
|
+
type: "object",
|
|
1323
|
+
description: "Resume data for the agent"
|
|
1324
|
+
},
|
|
1325
|
+
runId: {
|
|
1326
|
+
type: "string",
|
|
1327
|
+
description: "The run ID required when resuming an agent execution"
|
|
1328
|
+
},
|
|
36
1329
|
messages: {
|
|
37
1330
|
type: "array",
|
|
38
1331
|
description: "Array of messages in the conversation",
|
|
@@ -103,8 +1396,9 @@ function chatRoute({
|
|
|
103
1396
|
}
|
|
104
1397
|
},
|
|
105
1398
|
handler: async (c) => {
|
|
106
|
-
const
|
|
1399
|
+
const params = await c.req.json();
|
|
107
1400
|
const mastra = c.get("mastra");
|
|
1401
|
+
const contextRequestContext = c.get("requestContext");
|
|
108
1402
|
let agentToUse = agent;
|
|
109
1403
|
if (!agent) {
|
|
110
1404
|
const agentId = c.req.param("agentId");
|
|
@@ -115,23 +1409,256 @@ function chatRoute({
|
|
|
115
1409
|
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
116
1410
|
);
|
|
117
1411
|
}
|
|
1412
|
+
if (contextRequestContext && defaultOptions?.requestContext) {
|
|
1413
|
+
mastra.getLogger()?.warn(`"requestContext" set in the route options will be overridden by the request's "requestContext".`);
|
|
1414
|
+
}
|
|
118
1415
|
if (!agentToUse) {
|
|
119
1416
|
throw new Error("Agent ID is required");
|
|
120
1417
|
}
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
1418
|
+
const uiMessageStream = await handleChatStream({
|
|
1419
|
+
mastra,
|
|
1420
|
+
agentId: agentToUse,
|
|
1421
|
+
params: {
|
|
1422
|
+
...params,
|
|
1423
|
+
requestContext: contextRequestContext || params.requestContext
|
|
1424
|
+
},
|
|
1425
|
+
defaultOptions,
|
|
1426
|
+
sendStart,
|
|
1427
|
+
sendFinish,
|
|
1428
|
+
sendReasoning,
|
|
1429
|
+
sendSources
|
|
1430
|
+
});
|
|
1431
|
+
return createUIMessageStreamResponse({
|
|
1432
|
+
stream: uiMessageStream
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
async function handleWorkflowStream({
|
|
1438
|
+
mastra,
|
|
1439
|
+
workflowId,
|
|
1440
|
+
params,
|
|
1441
|
+
includeTextStreamParts = true
|
|
1442
|
+
}) {
|
|
1443
|
+
const { runId, resourceId, inputData, resumeData, requestContext, ...rest } = params;
|
|
1444
|
+
const workflowObj = mastra.getWorkflowById(workflowId);
|
|
1445
|
+
if (!workflowObj) {
|
|
1446
|
+
throw new Error(`Workflow ${workflowId} not found`);
|
|
1447
|
+
}
|
|
1448
|
+
const run = await workflowObj.createRun({ runId, resourceId, ...rest });
|
|
1449
|
+
const stream = resumeData ? run.resumeStream({ resumeData, ...rest, requestContext }) : run.stream({ inputData, ...rest, requestContext });
|
|
1450
|
+
return createUIMessageStream({
|
|
1451
|
+
execute: async ({ writer }) => {
|
|
1452
|
+
for await (const part of toAISdkV5Stream(stream, { from: "workflow", includeTextStreamParts })) {
|
|
1453
|
+
writer.write(part);
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
});
|
|
1457
|
+
}
|
|
1458
|
+
function workflowRoute({
|
|
1459
|
+
path = "/api/workflows/:workflowId/stream",
|
|
1460
|
+
workflow,
|
|
1461
|
+
includeTextStreamParts = true
|
|
1462
|
+
}) {
|
|
1463
|
+
if (!workflow && !path.includes("/:workflowId")) {
|
|
1464
|
+
throw new Error("Path must include :workflowId to route to the correct workflow or pass the workflow explicitly");
|
|
1465
|
+
}
|
|
1466
|
+
return registerApiRoute(path, {
|
|
1467
|
+
method: "POST",
|
|
1468
|
+
openapi: {
|
|
1469
|
+
summary: "Stream a workflow in AI SDK format",
|
|
1470
|
+
description: "Starts a workflow run and streams events as AI SDK UIMessage chunks",
|
|
1471
|
+
tags: ["ai-sdk"],
|
|
1472
|
+
parameters: [
|
|
1473
|
+
{
|
|
1474
|
+
name: "workflowId",
|
|
1475
|
+
in: "path",
|
|
1476
|
+
required: true,
|
|
1477
|
+
description: "The ID of the workflow to stream",
|
|
1478
|
+
schema: { type: "string" }
|
|
1479
|
+
}
|
|
1480
|
+
],
|
|
1481
|
+
requestBody: {
|
|
1482
|
+
required: true,
|
|
1483
|
+
content: {
|
|
1484
|
+
"application/json": {
|
|
1485
|
+
schema: {
|
|
1486
|
+
type: "object",
|
|
1487
|
+
properties: {
|
|
1488
|
+
runId: { type: "string" },
|
|
1489
|
+
resourceId: { type: "string" },
|
|
1490
|
+
inputData: { type: "object", additionalProperties: true },
|
|
1491
|
+
resumeData: { type: "object", additionalProperties: true },
|
|
1492
|
+
requestContext: { type: "object", additionalProperties: true },
|
|
1493
|
+
tracingOptions: { type: "object", additionalProperties: true },
|
|
1494
|
+
step: { type: "string" }
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
},
|
|
1500
|
+
responses: {
|
|
1501
|
+
"200": {
|
|
1502
|
+
description: "Workflow UIMessage event stream",
|
|
1503
|
+
content: {
|
|
1504
|
+
"text/plain": {
|
|
1505
|
+
schema: { type: "string", description: "SSE stream" }
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
},
|
|
1511
|
+
handler: async (c) => {
|
|
1512
|
+
const params = await c.req.json();
|
|
1513
|
+
const mastra = c.get("mastra");
|
|
1514
|
+
const contextRequestContext = c.get("requestContext");
|
|
1515
|
+
let workflowToUse = workflow;
|
|
1516
|
+
if (!workflow) {
|
|
1517
|
+
const workflowId = c.req.param("workflowId");
|
|
1518
|
+
workflowToUse = workflowId;
|
|
124
1519
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
1520
|
+
if (c.req.param("workflowId") && workflow) {
|
|
1521
|
+
mastra.getLogger()?.warn(
|
|
1522
|
+
`Fixed workflow ID was set together with a workflowId path parameter. This can lead to unexpected behavior.`
|
|
1523
|
+
);
|
|
1524
|
+
}
|
|
1525
|
+
if (!workflowToUse) {
|
|
1526
|
+
throw new Error("Workflow ID is required");
|
|
1527
|
+
}
|
|
1528
|
+
if (contextRequestContext && params.requestContext) {
|
|
1529
|
+
mastra.getLogger()?.warn(
|
|
1530
|
+
`"requestContext" from the request body will be ignored because "requestContext" is already set in the route options.`
|
|
1531
|
+
);
|
|
1532
|
+
}
|
|
1533
|
+
const uiMessageStream = await handleWorkflowStream({
|
|
1534
|
+
mastra,
|
|
1535
|
+
workflowId: workflowToUse,
|
|
1536
|
+
params: {
|
|
1537
|
+
...params,
|
|
1538
|
+
requestContext: contextRequestContext || params.requestContext
|
|
1539
|
+
},
|
|
1540
|
+
includeTextStreamParts
|
|
129
1541
|
});
|
|
130
|
-
return
|
|
1542
|
+
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1543
|
+
}
|
|
1544
|
+
});
|
|
1545
|
+
}
|
|
1546
|
+
async function handleNetworkStream({
|
|
1547
|
+
mastra,
|
|
1548
|
+
agentId,
|
|
1549
|
+
params,
|
|
1550
|
+
defaultOptions
|
|
1551
|
+
}) {
|
|
1552
|
+
const { messages, ...rest } = params;
|
|
1553
|
+
const agentObj = mastra.getAgentById(agentId);
|
|
1554
|
+
if (!agentObj) {
|
|
1555
|
+
throw new Error(`Agent ${agentId} not found`);
|
|
1556
|
+
}
|
|
1557
|
+
const result = await agentObj.network(messages, {
|
|
1558
|
+
...defaultOptions,
|
|
1559
|
+
...rest
|
|
1560
|
+
});
|
|
1561
|
+
return createUIMessageStream({
|
|
1562
|
+
execute: async ({ writer }) => {
|
|
1563
|
+
for await (const part of toAISdkV5Stream(result, { from: "network" })) {
|
|
1564
|
+
writer.write(part);
|
|
1565
|
+
}
|
|
131
1566
|
}
|
|
132
1567
|
});
|
|
133
1568
|
}
|
|
1569
|
+
function networkRoute({
|
|
1570
|
+
path = "/network/:agentId",
|
|
1571
|
+
agent,
|
|
1572
|
+
defaultOptions
|
|
1573
|
+
}) {
|
|
1574
|
+
if (!agent && !path.includes("/:agentId")) {
|
|
1575
|
+
throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
|
|
1576
|
+
}
|
|
1577
|
+
return registerApiRoute(path, {
|
|
1578
|
+
method: "POST",
|
|
1579
|
+
openapi: {
|
|
1580
|
+
summary: "Execute an agent network and stream AI SDK events",
|
|
1581
|
+
description: "Routes a request to an agent network and streams UIMessage chunks in AI SDK format",
|
|
1582
|
+
tags: ["ai-sdk"],
|
|
1583
|
+
parameters: [
|
|
1584
|
+
{
|
|
1585
|
+
name: "agentId",
|
|
1586
|
+
in: "path",
|
|
1587
|
+
required: true,
|
|
1588
|
+
description: "The ID of the routing agent to execute as a network",
|
|
1589
|
+
schema: { type: "string" }
|
|
1590
|
+
}
|
|
1591
|
+
],
|
|
1592
|
+
requestBody: {
|
|
1593
|
+
required: true,
|
|
1594
|
+
content: {
|
|
1595
|
+
"application/json": {
|
|
1596
|
+
schema: {
|
|
1597
|
+
type: "object",
|
|
1598
|
+
properties: {
|
|
1599
|
+
messages: { type: "array", items: { type: "object" } },
|
|
1600
|
+
requestContext: { type: "object", additionalProperties: true },
|
|
1601
|
+
runId: { type: "string" },
|
|
1602
|
+
maxSteps: { type: "number" },
|
|
1603
|
+
threadId: { type: "string" },
|
|
1604
|
+
resourceId: { type: "string" },
|
|
1605
|
+
modelSettings: { type: "object", additionalProperties: true },
|
|
1606
|
+
tools: { type: "array", items: { type: "object" } }
|
|
1607
|
+
},
|
|
1608
|
+
required: ["messages"]
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
},
|
|
1613
|
+
responses: {
|
|
1614
|
+
"200": {
|
|
1615
|
+
description: "Streaming AI SDK UIMessage event stream for the agent network",
|
|
1616
|
+
content: { "text/plain": { schema: { type: "string", description: "SSE stream" } } }
|
|
1617
|
+
},
|
|
1618
|
+
"404": {
|
|
1619
|
+
description: "Agent not found",
|
|
1620
|
+
content: {
|
|
1621
|
+
"application/json": {
|
|
1622
|
+
schema: { type: "object", properties: { error: { type: "string" } } }
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
},
|
|
1628
|
+
handler: async (c) => {
|
|
1629
|
+
const params = await c.req.json();
|
|
1630
|
+
const mastra = c.get("mastra");
|
|
1631
|
+
let agentToUse = agent;
|
|
1632
|
+
if (!agent) {
|
|
1633
|
+
const agentId = c.req.param("agentId");
|
|
1634
|
+
agentToUse = agentId;
|
|
1635
|
+
}
|
|
1636
|
+
if (c.req.param("agentId") && agent) {
|
|
1637
|
+
mastra.getLogger()?.warn(
|
|
1638
|
+
`Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
|
|
1639
|
+
);
|
|
1640
|
+
}
|
|
1641
|
+
if (!agentToUse) {
|
|
1642
|
+
throw new Error("Agent ID is required");
|
|
1643
|
+
}
|
|
1644
|
+
const uiMessageStream = await handleNetworkStream({
|
|
1645
|
+
mastra,
|
|
1646
|
+
agentId: agentToUse,
|
|
1647
|
+
params,
|
|
1648
|
+
defaultOptions
|
|
1649
|
+
});
|
|
1650
|
+
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
1651
|
+
}
|
|
1652
|
+
});
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
// src/to-ai-sdk-format.ts
|
|
1656
|
+
function toAISdkFormat() {
|
|
1657
|
+
throw new Error(
|
|
1658
|
+
'toAISdkFormat() has been deprecated. Please use toAISdkStream() instead.\n\nMigration:\n import { toAISdkFormat } from "@mastra/ai-sdk";\n // Change to:\n import { toAISdkStream } from "@mastra/ai-sdk";\n\nThe function signature remains the same.'
|
|
1659
|
+
);
|
|
1660
|
+
}
|
|
134
1661
|
|
|
135
|
-
export { chatRoute };
|
|
1662
|
+
export { chatRoute, handleChatStream, handleNetworkStream, handleWorkflowStream, networkRoute, toAISdkFormat, toAISdkV5Stream as toAISdkStream, workflowRoute };
|
|
136
1663
|
//# sourceMappingURL=index.js.map
|
|
137
1664
|
//# sourceMappingURL=index.js.map
|