@mastra/react 0.0.2-alpha.1
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 +40 -0
- package/LICENSE.md +15 -0
- package/dist/index.cjs.js +865 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.es.js +856 -0
- package/dist/index.es.js.map +1 -0
- package/dist/src/agent/hooks.d.ts +32 -0
- package/dist/src/agent/types.d.ts +16 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/lib/ai-sdk/index.d.ts +2 -0
- package/dist/src/lib/ai-sdk/toNetworkUIMessage.d.ts +6 -0
- package/dist/src/lib/ai-sdk/toNetworkUIMessage.test.d.ts +1 -0
- package/dist/src/lib/ai-sdk/toUIMessage.d.ts +16 -0
- package/dist/src/lib/ai-sdk/toUIMessage.test.d.ts +1 -0
- package/dist/src/mastra-client-context.d.ts +9 -0
- package/dist/src/mastra-react-provider.d.ts +4 -0
- package/package.json +50 -0
|
@@ -0,0 +1,865 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
const react = require('react');
|
|
7
|
+
const clientJs = require('@mastra/client-js');
|
|
8
|
+
const reactDom = require('react-dom');
|
|
9
|
+
|
|
10
|
+
const MastraClientContext = react.createContext({});
|
|
11
|
+
const MastraClientProvider = ({ children, baseUrl, headers }) => {
|
|
12
|
+
const client = createMastraClient(baseUrl, headers);
|
|
13
|
+
return /* @__PURE__ */ jsxRuntime.jsx(MastraClientContext.Provider, { value: client, children });
|
|
14
|
+
};
|
|
15
|
+
const useMastraClient = () => react.useContext(MastraClientContext);
|
|
16
|
+
const createMastraClient = (baseUrl, mastraClientHeaders = {}) => {
|
|
17
|
+
return new clientJs.MastraClient({
|
|
18
|
+
baseUrl: baseUrl || "",
|
|
19
|
+
// only add the header if the baseUrl is not provided i.e it's a local dev environment
|
|
20
|
+
headers: !baseUrl ? { ...mastraClientHeaders, "x-mastra-dev-playground": "true" } : mastraClientHeaders
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const MastraReactProvider = ({ children, baseUrl, headers }) => {
|
|
25
|
+
return /* @__PURE__ */ jsxRuntime.jsx(MastraClientProvider, { baseUrl, headers, children });
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const useChat = ({ agentId, initializeMessages }) => {
|
|
29
|
+
const [messages, setMessages] = react.useState(initializeMessages || []);
|
|
30
|
+
const baseClient = useMastraClient();
|
|
31
|
+
const [isRunning, setIsRunning] = react.useState(false);
|
|
32
|
+
const streamVNext = async ({
|
|
33
|
+
coreUserMessages,
|
|
34
|
+
runtimeContext,
|
|
35
|
+
threadId,
|
|
36
|
+
onChunk,
|
|
37
|
+
modelSettings,
|
|
38
|
+
signal
|
|
39
|
+
}) => {
|
|
40
|
+
const {
|
|
41
|
+
frequencyPenalty,
|
|
42
|
+
presencePenalty,
|
|
43
|
+
maxRetries,
|
|
44
|
+
maxTokens,
|
|
45
|
+
temperature,
|
|
46
|
+
topK,
|
|
47
|
+
topP,
|
|
48
|
+
instructions,
|
|
49
|
+
providerOptions
|
|
50
|
+
} = modelSettings || {};
|
|
51
|
+
setIsRunning(true);
|
|
52
|
+
const clientWithAbort = new clientJs.MastraClient({
|
|
53
|
+
...baseClient.options,
|
|
54
|
+
abortSignal: signal
|
|
55
|
+
});
|
|
56
|
+
const agent = clientWithAbort.getAgent(agentId);
|
|
57
|
+
const response = await agent.streamVNext({
|
|
58
|
+
messages: coreUserMessages,
|
|
59
|
+
runId: agentId,
|
|
60
|
+
modelSettings: {
|
|
61
|
+
frequencyPenalty,
|
|
62
|
+
presencePenalty,
|
|
63
|
+
maxRetries,
|
|
64
|
+
maxOutputTokens: maxTokens,
|
|
65
|
+
temperature,
|
|
66
|
+
topK,
|
|
67
|
+
topP
|
|
68
|
+
},
|
|
69
|
+
instructions,
|
|
70
|
+
runtimeContext,
|
|
71
|
+
...threadId ? { threadId, resourceId: agentId } : {},
|
|
72
|
+
providerOptions
|
|
73
|
+
});
|
|
74
|
+
if (!response.body) {
|
|
75
|
+
setIsRunning(false);
|
|
76
|
+
throw new Error("[StreamVNext] No response body");
|
|
77
|
+
}
|
|
78
|
+
await response.processDataStream({
|
|
79
|
+
onChunk: (chunk) => {
|
|
80
|
+
reactDom.flushSync(() => {
|
|
81
|
+
setMessages((prev) => onChunk(chunk, prev));
|
|
82
|
+
});
|
|
83
|
+
return Promise.resolve();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
setIsRunning(false);
|
|
87
|
+
};
|
|
88
|
+
const network = async ({
|
|
89
|
+
coreUserMessages,
|
|
90
|
+
runtimeContext,
|
|
91
|
+
threadId,
|
|
92
|
+
onNetworkChunk,
|
|
93
|
+
modelSettings,
|
|
94
|
+
signal
|
|
95
|
+
}) => {
|
|
96
|
+
const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, maxSteps } = modelSettings || {};
|
|
97
|
+
setIsRunning(true);
|
|
98
|
+
const clientWithAbort = new clientJs.MastraClient({
|
|
99
|
+
...baseClient.options,
|
|
100
|
+
abortSignal: signal
|
|
101
|
+
});
|
|
102
|
+
const agent = clientWithAbort.getAgent(agentId);
|
|
103
|
+
const response = await agent.network({
|
|
104
|
+
messages: coreUserMessages,
|
|
105
|
+
maxSteps,
|
|
106
|
+
modelSettings: {
|
|
107
|
+
frequencyPenalty,
|
|
108
|
+
presencePenalty,
|
|
109
|
+
maxRetries,
|
|
110
|
+
maxOutputTokens: maxTokens,
|
|
111
|
+
temperature,
|
|
112
|
+
topK,
|
|
113
|
+
topP
|
|
114
|
+
},
|
|
115
|
+
runId: agentId,
|
|
116
|
+
runtimeContext,
|
|
117
|
+
...threadId ? { thread: threadId, resourceId: agentId } : {}
|
|
118
|
+
});
|
|
119
|
+
await response.processDataStream({
|
|
120
|
+
onChunk: (chunk) => {
|
|
121
|
+
reactDom.flushSync(() => {
|
|
122
|
+
setMessages((prev) => onNetworkChunk(chunk, prev));
|
|
123
|
+
});
|
|
124
|
+
return Promise.resolve();
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
setIsRunning(false);
|
|
128
|
+
};
|
|
129
|
+
return {
|
|
130
|
+
network,
|
|
131
|
+
streamVNext,
|
|
132
|
+
isRunning,
|
|
133
|
+
messages,
|
|
134
|
+
setMessages,
|
|
135
|
+
cancelRun: () => setIsRunning(false)
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const mapWorkflowStreamChunkToWatchResult = (prev, chunk) => {
|
|
140
|
+
if (chunk.type === "workflow-start") {
|
|
141
|
+
return {
|
|
142
|
+
...prev,
|
|
143
|
+
runId: chunk.runId,
|
|
144
|
+
eventTimestamp: /* @__PURE__ */ new Date(),
|
|
145
|
+
payload: {
|
|
146
|
+
...prev?.payload || {},
|
|
147
|
+
workflowState: {
|
|
148
|
+
...prev?.payload?.workflowState,
|
|
149
|
+
status: "running",
|
|
150
|
+
steps: {}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (chunk.type === "workflow-step-start") {
|
|
156
|
+
const current = prev?.payload?.workflowState?.steps?.[chunk.payload.id] || {};
|
|
157
|
+
return {
|
|
158
|
+
...prev,
|
|
159
|
+
payload: {
|
|
160
|
+
...prev.payload,
|
|
161
|
+
currentStep: {
|
|
162
|
+
id: chunk.payload.id,
|
|
163
|
+
...chunk.payload
|
|
164
|
+
},
|
|
165
|
+
workflowState: {
|
|
166
|
+
...prev?.payload?.workflowState,
|
|
167
|
+
steps: {
|
|
168
|
+
...prev?.payload?.workflowState?.steps,
|
|
169
|
+
[chunk.payload.id]: {
|
|
170
|
+
...current || {},
|
|
171
|
+
...chunk.payload
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
eventTimestamp: /* @__PURE__ */ new Date()
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
if (chunk.type === "workflow-step-suspended") {
|
|
180
|
+
const current = prev?.payload?.workflowState?.steps?.[chunk.payload.id] || {};
|
|
181
|
+
return {
|
|
182
|
+
...prev,
|
|
183
|
+
payload: {
|
|
184
|
+
...prev?.payload,
|
|
185
|
+
currentStep: {
|
|
186
|
+
id: chunk.payload.id,
|
|
187
|
+
...prev?.payload?.currentStep,
|
|
188
|
+
...chunk.payload
|
|
189
|
+
},
|
|
190
|
+
workflowState: {
|
|
191
|
+
...prev?.payload?.workflowState,
|
|
192
|
+
status: "suspended",
|
|
193
|
+
steps: {
|
|
194
|
+
...prev?.payload?.workflowState?.steps,
|
|
195
|
+
[chunk.payload.id]: {
|
|
196
|
+
...current || {},
|
|
197
|
+
...chunk.payload
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
eventTimestamp: /* @__PURE__ */ new Date()
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
if (chunk.type === "workflow-step-waiting") {
|
|
206
|
+
const current = prev?.payload?.workflowState?.steps?.[chunk.payload.id] || {};
|
|
207
|
+
return {
|
|
208
|
+
...prev,
|
|
209
|
+
payload: {
|
|
210
|
+
...prev?.payload,
|
|
211
|
+
currentStep: {
|
|
212
|
+
id: chunk.payload.id,
|
|
213
|
+
...prev?.payload?.currentStep || {},
|
|
214
|
+
...chunk.payload
|
|
215
|
+
},
|
|
216
|
+
workflowState: {
|
|
217
|
+
...prev?.payload?.workflowState,
|
|
218
|
+
status: "waiting",
|
|
219
|
+
steps: {
|
|
220
|
+
...prev?.payload?.workflowState?.steps,
|
|
221
|
+
[chunk.payload.id]: {
|
|
222
|
+
...current,
|
|
223
|
+
...chunk.payload
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
eventTimestamp: /* @__PURE__ */ new Date()
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
if (chunk.type === "workflow-step-result") {
|
|
232
|
+
const status = chunk.payload.status;
|
|
233
|
+
const current = prev?.payload?.workflowState?.steps?.[chunk.payload.id] || {};
|
|
234
|
+
const next = {
|
|
235
|
+
...prev,
|
|
236
|
+
payload: {
|
|
237
|
+
...prev?.payload,
|
|
238
|
+
currentStep: {
|
|
239
|
+
id: chunk.payload.id,
|
|
240
|
+
...prev?.payload?.currentStep || {},
|
|
241
|
+
...chunk.payload
|
|
242
|
+
},
|
|
243
|
+
workflowState: {
|
|
244
|
+
...prev?.payload?.workflowState,
|
|
245
|
+
status,
|
|
246
|
+
steps: {
|
|
247
|
+
...prev?.payload?.workflowState?.steps,
|
|
248
|
+
[chunk.payload.id]: {
|
|
249
|
+
...current,
|
|
250
|
+
...chunk.payload
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
eventTimestamp: /* @__PURE__ */ new Date()
|
|
256
|
+
};
|
|
257
|
+
return next;
|
|
258
|
+
}
|
|
259
|
+
if (chunk.type === "workflow-canceled") {
|
|
260
|
+
return {
|
|
261
|
+
...prev,
|
|
262
|
+
payload: {
|
|
263
|
+
...prev?.payload,
|
|
264
|
+
workflowState: {
|
|
265
|
+
...prev?.payload?.workflowState,
|
|
266
|
+
status: "canceled"
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
eventTimestamp: /* @__PURE__ */ new Date()
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
if (chunk.type === "workflow-finish") {
|
|
273
|
+
return {
|
|
274
|
+
...prev,
|
|
275
|
+
payload: {
|
|
276
|
+
...prev?.payload,
|
|
277
|
+
currentStep: void 0,
|
|
278
|
+
workflowState: {
|
|
279
|
+
...prev?.payload?.workflowState,
|
|
280
|
+
status: chunk.payload.workflowStatus
|
|
281
|
+
}
|
|
282
|
+
},
|
|
283
|
+
eventTimestamp: /* @__PURE__ */ new Date()
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
return prev;
|
|
287
|
+
};
|
|
288
|
+
const toUIMessage = ({
|
|
289
|
+
chunk,
|
|
290
|
+
conversation
|
|
291
|
+
}) => {
|
|
292
|
+
const result = [...conversation];
|
|
293
|
+
switch (chunk.type) {
|
|
294
|
+
case "start": {
|
|
295
|
+
const newMessage = {
|
|
296
|
+
id: chunk.runId,
|
|
297
|
+
role: "assistant",
|
|
298
|
+
parts: []
|
|
299
|
+
};
|
|
300
|
+
return [...result, newMessage];
|
|
301
|
+
}
|
|
302
|
+
case "text-start":
|
|
303
|
+
case "text-delta": {
|
|
304
|
+
const lastMessage = result[result.length - 1];
|
|
305
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
306
|
+
const parts = [...lastMessage.parts];
|
|
307
|
+
let textPartIndex = parts.findIndex((part) => part.type === "text");
|
|
308
|
+
if (chunk.type === "text-start") {
|
|
309
|
+
if (textPartIndex === -1) {
|
|
310
|
+
parts.push({
|
|
311
|
+
type: "text",
|
|
312
|
+
text: "",
|
|
313
|
+
state: "streaming",
|
|
314
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
} else {
|
|
318
|
+
if (textPartIndex === -1) {
|
|
319
|
+
parts.push({
|
|
320
|
+
type: "text",
|
|
321
|
+
text: chunk.payload.text,
|
|
322
|
+
state: "streaming",
|
|
323
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
324
|
+
});
|
|
325
|
+
} else {
|
|
326
|
+
const textPart = parts[textPartIndex];
|
|
327
|
+
if (textPart.type === "text") {
|
|
328
|
+
parts[textPartIndex] = {
|
|
329
|
+
...textPart,
|
|
330
|
+
text: textPart.text + chunk.payload.text,
|
|
331
|
+
state: "streaming"
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return [
|
|
337
|
+
...result.slice(0, -1),
|
|
338
|
+
{
|
|
339
|
+
...lastMessage,
|
|
340
|
+
parts
|
|
341
|
+
}
|
|
342
|
+
];
|
|
343
|
+
}
|
|
344
|
+
case "reasoning-delta": {
|
|
345
|
+
const lastMessage = result[result.length - 1];
|
|
346
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
347
|
+
const newMessage = {
|
|
348
|
+
id: chunk.runId,
|
|
349
|
+
role: "assistant",
|
|
350
|
+
parts: [
|
|
351
|
+
{
|
|
352
|
+
type: "reasoning",
|
|
353
|
+
text: chunk.payload.text,
|
|
354
|
+
state: "streaming",
|
|
355
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
356
|
+
}
|
|
357
|
+
]
|
|
358
|
+
};
|
|
359
|
+
return [...result, newMessage];
|
|
360
|
+
}
|
|
361
|
+
const parts = [...lastMessage.parts];
|
|
362
|
+
let reasoningPartIndex = parts.findIndex((part) => part.type === "reasoning");
|
|
363
|
+
if (reasoningPartIndex === -1) {
|
|
364
|
+
parts.push({
|
|
365
|
+
type: "reasoning",
|
|
366
|
+
text: chunk.payload.text,
|
|
367
|
+
state: "streaming",
|
|
368
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
369
|
+
});
|
|
370
|
+
} else {
|
|
371
|
+
const reasoningPart = parts[reasoningPartIndex];
|
|
372
|
+
if (reasoningPart.type === "reasoning") {
|
|
373
|
+
parts[reasoningPartIndex] = {
|
|
374
|
+
...reasoningPart,
|
|
375
|
+
text: reasoningPart.text + chunk.payload.text,
|
|
376
|
+
state: "streaming"
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return [
|
|
381
|
+
...result.slice(0, -1),
|
|
382
|
+
{
|
|
383
|
+
...lastMessage,
|
|
384
|
+
parts
|
|
385
|
+
}
|
|
386
|
+
];
|
|
387
|
+
}
|
|
388
|
+
case "tool-call": {
|
|
389
|
+
const lastMessage = result[result.length - 1];
|
|
390
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
391
|
+
const newMessage = {
|
|
392
|
+
id: chunk.runId,
|
|
393
|
+
role: "assistant",
|
|
394
|
+
parts: [
|
|
395
|
+
{
|
|
396
|
+
type: "dynamic-tool",
|
|
397
|
+
toolName: chunk.payload.toolName,
|
|
398
|
+
toolCallId: chunk.payload.toolCallId,
|
|
399
|
+
state: "input-available",
|
|
400
|
+
input: chunk.payload.args,
|
|
401
|
+
callProviderMetadata: chunk.payload.providerMetadata
|
|
402
|
+
}
|
|
403
|
+
]
|
|
404
|
+
};
|
|
405
|
+
return [...result, newMessage];
|
|
406
|
+
}
|
|
407
|
+
const parts = [...lastMessage.parts];
|
|
408
|
+
parts.push({
|
|
409
|
+
type: "dynamic-tool",
|
|
410
|
+
toolName: chunk.payload.toolName,
|
|
411
|
+
toolCallId: chunk.payload.toolCallId,
|
|
412
|
+
state: "input-available",
|
|
413
|
+
input: chunk.payload.args,
|
|
414
|
+
callProviderMetadata: chunk.payload.providerMetadata
|
|
415
|
+
});
|
|
416
|
+
return [
|
|
417
|
+
...result.slice(0, -1),
|
|
418
|
+
{
|
|
419
|
+
...lastMessage,
|
|
420
|
+
parts
|
|
421
|
+
}
|
|
422
|
+
];
|
|
423
|
+
}
|
|
424
|
+
case "tool-result": {
|
|
425
|
+
const lastMessage = result[result.length - 1];
|
|
426
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
427
|
+
const parts = [...lastMessage.parts];
|
|
428
|
+
const toolPartIndex = parts.findIndex(
|
|
429
|
+
(part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
430
|
+
);
|
|
431
|
+
if (toolPartIndex !== -1) {
|
|
432
|
+
const toolPart = parts[toolPartIndex];
|
|
433
|
+
if (toolPart.type === "dynamic-tool") {
|
|
434
|
+
if (chunk.payload.isError) {
|
|
435
|
+
parts[toolPartIndex] = {
|
|
436
|
+
type: "dynamic-tool",
|
|
437
|
+
toolName: toolPart.toolName,
|
|
438
|
+
toolCallId: toolPart.toolCallId,
|
|
439
|
+
state: "output-error",
|
|
440
|
+
input: toolPart.input,
|
|
441
|
+
errorText: String(chunk.payload.result),
|
|
442
|
+
callProviderMetadata: chunk.payload.providerMetadata
|
|
443
|
+
};
|
|
444
|
+
} else {
|
|
445
|
+
parts[toolPartIndex] = {
|
|
446
|
+
type: "dynamic-tool",
|
|
447
|
+
toolName: toolPart.toolName,
|
|
448
|
+
toolCallId: toolPart.toolCallId,
|
|
449
|
+
state: "output-available",
|
|
450
|
+
input: toolPart.input,
|
|
451
|
+
output: toolPart.output,
|
|
452
|
+
callProviderMetadata: chunk.payload.providerMetadata
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return [
|
|
458
|
+
...result.slice(0, -1),
|
|
459
|
+
{
|
|
460
|
+
...lastMessage,
|
|
461
|
+
parts
|
|
462
|
+
}
|
|
463
|
+
];
|
|
464
|
+
}
|
|
465
|
+
case "tool-output": {
|
|
466
|
+
const lastMessage = result[result.length - 1];
|
|
467
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
468
|
+
const parts = [...lastMessage.parts];
|
|
469
|
+
const toolPartIndex = parts.findIndex(
|
|
470
|
+
(part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
471
|
+
);
|
|
472
|
+
if (toolPartIndex !== -1) {
|
|
473
|
+
const toolPart = parts[toolPartIndex];
|
|
474
|
+
if (toolPart.type === "dynamic-tool") {
|
|
475
|
+
if (chunk.payload.output?.type?.startsWith("workflow-")) {
|
|
476
|
+
const currentOutput = toolPart.output || {};
|
|
477
|
+
const existingWorkflowState = currentOutput || {};
|
|
478
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
|
|
479
|
+
existingWorkflowState,
|
|
480
|
+
chunk.payload.output
|
|
481
|
+
);
|
|
482
|
+
parts[toolPartIndex] = {
|
|
483
|
+
...toolPart,
|
|
484
|
+
output: updatedWorkflowState
|
|
485
|
+
};
|
|
486
|
+
} else {
|
|
487
|
+
const currentOutput = toolPart.output || [];
|
|
488
|
+
const existingOutput = Array.isArray(currentOutput) ? currentOutput : [];
|
|
489
|
+
parts[toolPartIndex] = {
|
|
490
|
+
...toolPart,
|
|
491
|
+
output: [...existingOutput, chunk.payload.output]
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return [
|
|
497
|
+
...result.slice(0, -1),
|
|
498
|
+
{
|
|
499
|
+
...lastMessage,
|
|
500
|
+
parts
|
|
501
|
+
}
|
|
502
|
+
];
|
|
503
|
+
}
|
|
504
|
+
case "source": {
|
|
505
|
+
const lastMessage = result[result.length - 1];
|
|
506
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
507
|
+
const parts = [...lastMessage.parts];
|
|
508
|
+
if (chunk.payload.sourceType === "url") {
|
|
509
|
+
parts.push({
|
|
510
|
+
type: "source-url",
|
|
511
|
+
sourceId: chunk.payload.id,
|
|
512
|
+
url: chunk.payload.url || "",
|
|
513
|
+
title: chunk.payload.title,
|
|
514
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
515
|
+
});
|
|
516
|
+
} else if (chunk.payload.sourceType === "document") {
|
|
517
|
+
parts.push({
|
|
518
|
+
type: "source-document",
|
|
519
|
+
sourceId: chunk.payload.id,
|
|
520
|
+
mediaType: chunk.payload.mimeType || "application/octet-stream",
|
|
521
|
+
title: chunk.payload.title,
|
|
522
|
+
filename: chunk.payload.filename,
|
|
523
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
return [
|
|
527
|
+
...result.slice(0, -1),
|
|
528
|
+
{
|
|
529
|
+
...lastMessage,
|
|
530
|
+
parts
|
|
531
|
+
}
|
|
532
|
+
];
|
|
533
|
+
}
|
|
534
|
+
case "file": {
|
|
535
|
+
const lastMessage = result[result.length - 1];
|
|
536
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
537
|
+
const parts = [...lastMessage.parts];
|
|
538
|
+
let url;
|
|
539
|
+
if (typeof chunk.payload.data === "string") {
|
|
540
|
+
url = chunk.payload.base64 ? `data:${chunk.payload.mimeType};base64,${chunk.payload.data}` : `data:${chunk.payload.mimeType},${encodeURIComponent(chunk.payload.data)}`;
|
|
541
|
+
} else {
|
|
542
|
+
const base64 = btoa(String.fromCharCode(...chunk.payload.data));
|
|
543
|
+
url = `data:${chunk.payload.mimeType};base64,${base64}`;
|
|
544
|
+
}
|
|
545
|
+
parts.push({
|
|
546
|
+
type: "file",
|
|
547
|
+
mediaType: chunk.payload.mimeType,
|
|
548
|
+
url,
|
|
549
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
550
|
+
});
|
|
551
|
+
return [
|
|
552
|
+
...result.slice(0, -1),
|
|
553
|
+
{
|
|
554
|
+
...lastMessage,
|
|
555
|
+
parts
|
|
556
|
+
}
|
|
557
|
+
];
|
|
558
|
+
}
|
|
559
|
+
case "finish": {
|
|
560
|
+
const lastMessage = result[result.length - 1];
|
|
561
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
562
|
+
const parts = lastMessage.parts.map((part) => {
|
|
563
|
+
if (part.type === "text" && part.state === "streaming") {
|
|
564
|
+
return { ...part, state: "done" };
|
|
565
|
+
}
|
|
566
|
+
if (part.type === "reasoning" && part.state === "streaming") {
|
|
567
|
+
return { ...part, state: "done" };
|
|
568
|
+
}
|
|
569
|
+
return part;
|
|
570
|
+
});
|
|
571
|
+
return [
|
|
572
|
+
...result.slice(0, -1),
|
|
573
|
+
{
|
|
574
|
+
...lastMessage,
|
|
575
|
+
parts
|
|
576
|
+
}
|
|
577
|
+
];
|
|
578
|
+
}
|
|
579
|
+
case "error": {
|
|
580
|
+
return result;
|
|
581
|
+
}
|
|
582
|
+
// For all other chunk types, return conversation unchanged
|
|
583
|
+
default:
|
|
584
|
+
return result;
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
const toNetworkUIMessage = ({
|
|
589
|
+
chunk,
|
|
590
|
+
conversation
|
|
591
|
+
}) => {
|
|
592
|
+
const result = [...conversation];
|
|
593
|
+
if (chunk.type === "agent-execution-start" || chunk.type === "workflow-execution-start") {
|
|
594
|
+
const primitiveId = chunk.payload?.args?.primitiveId;
|
|
595
|
+
const runId = chunk.payload.runId;
|
|
596
|
+
if (!primitiveId || !runId) return result;
|
|
597
|
+
const newMessage = {
|
|
598
|
+
id: runId,
|
|
599
|
+
role: "assistant",
|
|
600
|
+
parts: [
|
|
601
|
+
{
|
|
602
|
+
type: "dynamic-tool",
|
|
603
|
+
toolName: primitiveId,
|
|
604
|
+
toolCallId: runId,
|
|
605
|
+
state: "input-available",
|
|
606
|
+
input: chunk.payload.args,
|
|
607
|
+
output: {
|
|
608
|
+
networkMetadata: {
|
|
609
|
+
selectionReason: chunk.payload?.args?.selectionReason || "",
|
|
610
|
+
from: chunk.type === "agent-execution-start" ? "AGENT" : "WORKFLOW"
|
|
611
|
+
},
|
|
612
|
+
result: void 0
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
]
|
|
616
|
+
};
|
|
617
|
+
return [...result, newMessage];
|
|
618
|
+
}
|
|
619
|
+
if (chunk.type.startsWith("agent-execution-event-")) {
|
|
620
|
+
const agentChunk = chunk.payload;
|
|
621
|
+
const lastMessage = result[result.length - 1];
|
|
622
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
623
|
+
const parts = [...lastMessage.parts];
|
|
624
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
625
|
+
if (toolPartIndex === -1) return result;
|
|
626
|
+
const toolPart = parts[toolPartIndex];
|
|
627
|
+
if (toolPart.type !== "dynamic-tool") return result;
|
|
628
|
+
if (agentChunk.type === "text-delta") {
|
|
629
|
+
const currentInput = toolPart.input;
|
|
630
|
+
const messages = currentInput?.messages || [];
|
|
631
|
+
const lastMessage2 = messages[messages.length - 1];
|
|
632
|
+
const nextMessages = lastMessage2?.type === "text" ? [
|
|
633
|
+
...messages.slice(0, -1),
|
|
634
|
+
{ type: "text", content: (lastMessage2?.content || "") + agentChunk.payload.text }
|
|
635
|
+
] : [...messages, { type: "text", content: agentChunk.payload.text }];
|
|
636
|
+
parts[toolPartIndex] = {
|
|
637
|
+
...toolPart,
|
|
638
|
+
input: {
|
|
639
|
+
...currentInput,
|
|
640
|
+
messages: nextMessages
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
} else if (agentChunk.type === "tool-call") {
|
|
644
|
+
const currentInput = toolPart.input;
|
|
645
|
+
const messages = currentInput?.messages || [];
|
|
646
|
+
parts[toolPartIndex] = {
|
|
647
|
+
...toolPart,
|
|
648
|
+
input: {
|
|
649
|
+
...currentInput,
|
|
650
|
+
messages: [
|
|
651
|
+
...messages,
|
|
652
|
+
{
|
|
653
|
+
type: "tool",
|
|
654
|
+
toolCallId: agentChunk.payload.toolCallId,
|
|
655
|
+
toolName: agentChunk.payload.toolName,
|
|
656
|
+
toolInput: agentChunk.payload.args
|
|
657
|
+
}
|
|
658
|
+
]
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
} else if (agentChunk.type === "tool-result") {
|
|
662
|
+
const currentInput = toolPart.input;
|
|
663
|
+
const messages = currentInput?.messages || [];
|
|
664
|
+
const lastToolIndex = messages.length - 1;
|
|
665
|
+
if (lastToolIndex >= 0 && messages[lastToolIndex]?.type === "tool") {
|
|
666
|
+
parts[toolPartIndex] = {
|
|
667
|
+
...toolPart,
|
|
668
|
+
input: {
|
|
669
|
+
...currentInput,
|
|
670
|
+
messages: [
|
|
671
|
+
...messages.slice(0, -1),
|
|
672
|
+
{
|
|
673
|
+
...messages[lastToolIndex],
|
|
674
|
+
toolOutput: agentChunk.payload.result
|
|
675
|
+
}
|
|
676
|
+
]
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
} else if (agentChunk.type === "tool-output") {
|
|
681
|
+
if (agentChunk.payload?.output?.type?.startsWith("workflow-")) {
|
|
682
|
+
const currentOutput = toolPart.output || {};
|
|
683
|
+
const existingWorkflowState = currentOutput.result || {};
|
|
684
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
|
|
685
|
+
existingWorkflowState,
|
|
686
|
+
agentChunk.payload.output
|
|
687
|
+
);
|
|
688
|
+
parts[toolPartIndex] = {
|
|
689
|
+
...toolPart,
|
|
690
|
+
output: {
|
|
691
|
+
networkMetadata: currentOutput.networkMetadata,
|
|
692
|
+
result: updatedWorkflowState
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
return [
|
|
698
|
+
...result.slice(0, -1),
|
|
699
|
+
{
|
|
700
|
+
...lastMessage,
|
|
701
|
+
parts
|
|
702
|
+
}
|
|
703
|
+
];
|
|
704
|
+
}
|
|
705
|
+
if (chunk.type.startsWith("workflow-execution-event-")) {
|
|
706
|
+
const workflowChunk = chunk.payload;
|
|
707
|
+
const lastMessage = result[result.length - 1];
|
|
708
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
709
|
+
const parts = [...lastMessage.parts];
|
|
710
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
711
|
+
if (toolPartIndex === -1) return result;
|
|
712
|
+
const toolPart = parts[toolPartIndex];
|
|
713
|
+
if (toolPart.type !== "dynamic-tool") return result;
|
|
714
|
+
const currentOutput = toolPart.output || {};
|
|
715
|
+
const existingWorkflowState = currentOutput.result || {};
|
|
716
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(existingWorkflowState, workflowChunk);
|
|
717
|
+
parts[toolPartIndex] = {
|
|
718
|
+
...toolPart,
|
|
719
|
+
output: {
|
|
720
|
+
networkMetadata: currentOutput.networkMetadata,
|
|
721
|
+
result: updatedWorkflowState
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
return [
|
|
725
|
+
...result.slice(0, -1),
|
|
726
|
+
{
|
|
727
|
+
...lastMessage,
|
|
728
|
+
parts
|
|
729
|
+
}
|
|
730
|
+
];
|
|
731
|
+
}
|
|
732
|
+
if (chunk.type === "tool-execution-start") {
|
|
733
|
+
const { args: argsData } = chunk.payload;
|
|
734
|
+
const lastMessage = result[result.length - 1];
|
|
735
|
+
const nestedArgs = argsData.args || {};
|
|
736
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
737
|
+
const newMessage = {
|
|
738
|
+
id: chunk.runId,
|
|
739
|
+
role: "assistant",
|
|
740
|
+
parts: [
|
|
741
|
+
{
|
|
742
|
+
type: "dynamic-tool",
|
|
743
|
+
toolName: argsData.toolName || "unknown",
|
|
744
|
+
toolCallId: argsData.toolCallId || "unknown",
|
|
745
|
+
state: "input-available",
|
|
746
|
+
input: nestedArgs,
|
|
747
|
+
output: {
|
|
748
|
+
networkMetadata: {
|
|
749
|
+
selectionReason: argsData.selectionReason || ""
|
|
750
|
+
},
|
|
751
|
+
result: void 0
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
]
|
|
755
|
+
};
|
|
756
|
+
return [...result, newMessage];
|
|
757
|
+
}
|
|
758
|
+
const parts = [...lastMessage.parts];
|
|
759
|
+
parts.push({
|
|
760
|
+
type: "dynamic-tool",
|
|
761
|
+
toolName: argsData.toolName || "unknown",
|
|
762
|
+
toolCallId: argsData.toolCallId || "unknown",
|
|
763
|
+
state: "input-available",
|
|
764
|
+
input: nestedArgs,
|
|
765
|
+
output: {
|
|
766
|
+
networkMetadata: {
|
|
767
|
+
selectionReason: argsData.selectionReason || ""
|
|
768
|
+
},
|
|
769
|
+
result: void 0
|
|
770
|
+
}
|
|
771
|
+
});
|
|
772
|
+
return [
|
|
773
|
+
...result.slice(0, -1),
|
|
774
|
+
{
|
|
775
|
+
...lastMessage,
|
|
776
|
+
parts
|
|
777
|
+
}
|
|
778
|
+
];
|
|
779
|
+
}
|
|
780
|
+
if (chunk.type === "tool-execution-end") {
|
|
781
|
+
const lastMessage = result[result.length - 1];
|
|
782
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
783
|
+
const parts = [...lastMessage.parts];
|
|
784
|
+
const toolPartIndex = parts.findIndex(
|
|
785
|
+
(part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
786
|
+
);
|
|
787
|
+
if (toolPartIndex !== -1) {
|
|
788
|
+
const toolPart = parts[toolPartIndex];
|
|
789
|
+
if (toolPart.type === "dynamic-tool") {
|
|
790
|
+
const currentOutput = toolPart.output;
|
|
791
|
+
parts[toolPartIndex] = {
|
|
792
|
+
type: "dynamic-tool",
|
|
793
|
+
toolName: toolPart.toolName,
|
|
794
|
+
toolCallId: toolPart.toolCallId,
|
|
795
|
+
state: "output-available",
|
|
796
|
+
input: toolPart.input,
|
|
797
|
+
output: {
|
|
798
|
+
networkMetadata: currentOutput?.networkMetadata,
|
|
799
|
+
result: chunk.payload.result
|
|
800
|
+
}
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
return [
|
|
805
|
+
...result.slice(0, -1),
|
|
806
|
+
{
|
|
807
|
+
...lastMessage,
|
|
808
|
+
parts
|
|
809
|
+
}
|
|
810
|
+
];
|
|
811
|
+
}
|
|
812
|
+
if (chunk.type === "agent-execution-end" || chunk.type === "workflow-execution-end") {
|
|
813
|
+
const lastMessage = result[result.length - 1];
|
|
814
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
815
|
+
const parts = [...lastMessage.parts];
|
|
816
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
817
|
+
if (toolPartIndex !== -1) {
|
|
818
|
+
const toolPart = parts[toolPartIndex];
|
|
819
|
+
if (toolPart.type === "dynamic-tool") {
|
|
820
|
+
const currentOutput = toolPart.output;
|
|
821
|
+
parts[toolPartIndex] = {
|
|
822
|
+
type: "dynamic-tool",
|
|
823
|
+
toolName: toolPart.toolName,
|
|
824
|
+
toolCallId: toolPart.toolCallId,
|
|
825
|
+
state: "output-available",
|
|
826
|
+
input: toolPart.input,
|
|
827
|
+
output: {
|
|
828
|
+
networkMetadata: currentOutput?.networkMetadata,
|
|
829
|
+
result: currentOutput?.result || chunk.payload?.result || ""
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
return [
|
|
835
|
+
...result.slice(0, -1),
|
|
836
|
+
{
|
|
837
|
+
...lastMessage,
|
|
838
|
+
parts
|
|
839
|
+
}
|
|
840
|
+
];
|
|
841
|
+
}
|
|
842
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
843
|
+
const newMessage = {
|
|
844
|
+
id: chunk.runId,
|
|
845
|
+
role: "assistant",
|
|
846
|
+
parts: [
|
|
847
|
+
{
|
|
848
|
+
type: "text",
|
|
849
|
+
text: chunk.payload?.result || "",
|
|
850
|
+
state: "done"
|
|
851
|
+
}
|
|
852
|
+
]
|
|
853
|
+
};
|
|
854
|
+
return [...result, newMessage];
|
|
855
|
+
}
|
|
856
|
+
return result;
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
exports.MastraReactProvider = MastraReactProvider;
|
|
860
|
+
exports.mapWorkflowStreamChunkToWatchResult = mapWorkflowStreamChunkToWatchResult;
|
|
861
|
+
exports.toNetworkUIMessage = toNetworkUIMessage;
|
|
862
|
+
exports.toUIMessage = toUIMessage;
|
|
863
|
+
exports.useChat = useChat;
|
|
864
|
+
exports.useMastraClient = useMastraClient;
|
|
865
|
+
//# sourceMappingURL=index.cjs.js.map
|