@mastra/react 0.0.0-unified-sidebar-20251010130811 → 0.0.0-vnext-20251104230439

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +162 -2
  2. package/dist/index.cjs +1818 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/{index.es.js → index.js} +782 -128
  5. package/dist/index.js.map +1 -0
  6. package/dist/react.css +719 -0
  7. package/dist/src/agent/hooks.d.ts +23 -6
  8. package/dist/src/agent/types.d.ts +1 -0
  9. package/dist/src/index.d.ts +1 -0
  10. package/dist/src/lib/ai-sdk/index.d.ts +1 -0
  11. package/dist/src/lib/ai-sdk/memory/resolveInitialMessages.d.ts +10 -0
  12. package/dist/src/lib/ai-sdk/memory/resolveInitialMessages.test.d.ts +1 -0
  13. package/dist/src/lib/ai-sdk/transformers/AISdkNetworkTransformer.d.ts +1 -0
  14. package/dist/src/lib/ai-sdk/transformers/AISdkNetworkTransformer.test.d.ts +1 -0
  15. package/dist/src/lib/ai-sdk/types.d.ts +7 -0
  16. package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.d.ts +10 -0
  17. package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.test.d.ts +1 -0
  18. package/dist/src/lib/ai-sdk/utils/toUIMessage.test.d.ts +1 -0
  19. package/dist/src/ui/Code/Code.d.ts +13 -0
  20. package/dist/src/ui/Code/highlight.d.ts +3 -0
  21. package/dist/src/ui/Code/index.d.ts +1 -0
  22. package/dist/src/ui/Entity/Entity.d.ts +13 -0
  23. package/dist/src/ui/Entity/Entity.stories.d.ts +22 -0
  24. package/dist/src/ui/Entity/Entry.d.ts +9 -0
  25. package/dist/src/ui/Entity/ToolApproval.d.ts +10 -0
  26. package/dist/src/ui/Entity/context.d.ts +10 -0
  27. package/dist/src/ui/Entity/index.d.ts +4 -0
  28. package/dist/src/ui/Entity/types.d.ts +1 -0
  29. package/dist/src/ui/Icon/Icon.d.ts +11 -0
  30. package/dist/src/ui/Icon/index.d.ts +1 -0
  31. package/dist/src/ui/IconButton/IconButton.d.ts +8 -0
  32. package/dist/src/ui/IconButton/IconButton.stories.d.ts +12 -0
  33. package/dist/src/ui/IconButton/index.d.ts +1 -0
  34. package/dist/src/ui/Icons/AgentIcon.d.ts +2 -0
  35. package/dist/src/ui/Icons/ToolsIcon.d.ts +2 -0
  36. package/dist/src/ui/Icons/WorkflowIcon.d.ts +2 -0
  37. package/dist/src/ui/Icons/index.d.ts +3 -0
  38. package/dist/src/ui/Message/Message.d.ts +25 -0
  39. package/dist/src/ui/Message/Message.stories.d.ts +13 -0
  40. package/dist/src/ui/Message/index.d.ts +1 -0
  41. package/dist/src/ui/Tooltip/Tooltip.d.ts +8 -0
  42. package/dist/src/ui/Tooltip/Tooltip.stories.d.ts +12 -0
  43. package/dist/src/ui/Tooltip/index.d.ts +1 -0
  44. package/dist/src/ui/index.d.ts +7 -0
  45. package/package.json +46 -11
  46. package/dist/index.cjs.js +0 -1109
  47. package/dist/index.cjs.js.map +0 -1
  48. package/dist/index.es.js.map +0 -1
package/dist/index.cjs ADDED
@@ -0,0 +1,1818 @@
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 lucideReact = require('lucide-react');
9
+ const tailwindMerge = require('tailwind-merge');
10
+ const hastUtilToJsxRuntime = require('hast-util-to-jsx-runtime');
11
+ const web = require('shiki/bundle/web');
12
+ const reactTooltip = require('@radix-ui/react-tooltip');
13
+
14
+ const MastraClientContext = react.createContext({});
15
+ const MastraClientProvider = ({ children, baseUrl, headers }) => {
16
+ const client = createMastraClient(baseUrl, headers);
17
+ return /* @__PURE__ */ jsxRuntime.jsx(MastraClientContext.Provider, { value: client, children });
18
+ };
19
+ const useMastraClient = () => react.useContext(MastraClientContext);
20
+ const createMastraClient = (baseUrl, mastraClientHeaders = {}) => {
21
+ return new clientJs.MastraClient({
22
+ baseUrl: baseUrl || "",
23
+ // only add the header if the baseUrl is not provided i.e it's a local dev environment
24
+ headers: !baseUrl ? { ...mastraClientHeaders, "x-mastra-dev-playground": "true" } : mastraClientHeaders
25
+ });
26
+ };
27
+
28
+ const MastraReactProvider = ({ children, baseUrl, headers }) => {
29
+ return /* @__PURE__ */ jsxRuntime.jsx(MastraClientProvider, { baseUrl, headers, children });
30
+ };
31
+
32
+ const mapWorkflowStreamChunkToWatchResult = (prev, chunk) => {
33
+ if (chunk.type === "workflow-start") {
34
+ return {
35
+ input: prev?.input,
36
+ status: "running",
37
+ steps: prev?.steps || {}
38
+ };
39
+ }
40
+ if (chunk.type === "workflow-canceled") {
41
+ return {
42
+ ...prev,
43
+ status: "canceled"
44
+ };
45
+ }
46
+ if (chunk.type === "workflow-finish") {
47
+ const finalStatus = chunk.payload.workflowStatus;
48
+ const prevSteps = prev?.steps ?? {};
49
+ const lastStep = Object.values(prevSteps).pop();
50
+ return {
51
+ ...prev,
52
+ status: chunk.payload.workflowStatus,
53
+ ...finalStatus === "success" && lastStep?.status === "success" ? { result: lastStep?.output } : finalStatus === "failed" && lastStep?.status === "failed" ? { error: lastStep?.error } : {}
54
+ };
55
+ }
56
+ const { stepCallId, stepName, ...newPayload } = chunk.payload ?? {};
57
+ const newSteps = {
58
+ ...prev?.steps,
59
+ [chunk.payload.id]: {
60
+ ...prev?.steps?.[chunk.payload.id],
61
+ ...newPayload
62
+ }
63
+ };
64
+ if (chunk.type === "workflow-step-start") {
65
+ return {
66
+ ...prev,
67
+ steps: newSteps
68
+ };
69
+ }
70
+ if (chunk.type === "workflow-step-suspended") {
71
+ const suspendedStepIds = Object.entries(newSteps).flatMap(
72
+ ([stepId, stepResult]) => {
73
+ if (stepResult?.status === "suspended") {
74
+ const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
75
+ return nestedPath ? [[stepId, ...nestedPath]] : [[stepId]];
76
+ }
77
+ return [];
78
+ }
79
+ );
80
+ return {
81
+ ...prev,
82
+ status: "suspended",
83
+ steps: newSteps,
84
+ suspendPayload: chunk.payload.suspendPayload,
85
+ suspended: suspendedStepIds
86
+ };
87
+ }
88
+ if (chunk.type === "workflow-step-waiting") {
89
+ return {
90
+ ...prev,
91
+ status: "waiting",
92
+ steps: newSteps
93
+ };
94
+ }
95
+ if (chunk.type === "workflow-step-result") {
96
+ return {
97
+ ...prev,
98
+ steps: newSteps
99
+ };
100
+ }
101
+ return prev;
102
+ };
103
+ const toUIMessage = ({ chunk, conversation, metadata }) => {
104
+ const result = [...conversation];
105
+ switch (chunk.type) {
106
+ case "tripwire": {
107
+ const newMessage = {
108
+ id: `tripwire-${chunk.runId + Date.now()}`,
109
+ role: "assistant",
110
+ parts: [
111
+ {
112
+ type: "text",
113
+ text: chunk.payload.tripwireReason
114
+ }
115
+ ],
116
+ metadata: {
117
+ ...metadata,
118
+ status: "warning"
119
+ }
120
+ };
121
+ return [...result, newMessage];
122
+ }
123
+ case "start": {
124
+ const newMessage = {
125
+ id: `start-${chunk.runId + Date.now()}`,
126
+ role: "assistant",
127
+ parts: [],
128
+ metadata
129
+ };
130
+ return [...result, newMessage];
131
+ }
132
+ case "text-start":
133
+ case "text-delta": {
134
+ const lastMessage = result[result.length - 1];
135
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
136
+ const parts = [...lastMessage.parts];
137
+ let textPartIndex = parts.findIndex((part) => part.type === "text");
138
+ if (chunk.type === "text-start") {
139
+ if (textPartIndex === -1) {
140
+ parts.push({
141
+ type: "text",
142
+ text: "",
143
+ state: "streaming",
144
+ providerMetadata: chunk.payload.providerMetadata
145
+ });
146
+ }
147
+ } else {
148
+ if (textPartIndex === -1) {
149
+ parts.push({
150
+ type: "text",
151
+ text: chunk.payload.text,
152
+ state: "streaming",
153
+ providerMetadata: chunk.payload.providerMetadata
154
+ });
155
+ } else {
156
+ const textPart = parts[textPartIndex];
157
+ if (textPart.type === "text") {
158
+ parts[textPartIndex] = {
159
+ ...textPart,
160
+ text: textPart.text + chunk.payload.text,
161
+ state: "streaming"
162
+ };
163
+ }
164
+ }
165
+ }
166
+ return [
167
+ ...result.slice(0, -1),
168
+ {
169
+ ...lastMessage,
170
+ parts
171
+ }
172
+ ];
173
+ }
174
+ case "reasoning-delta": {
175
+ const lastMessage = result[result.length - 1];
176
+ if (!lastMessage || lastMessage.role !== "assistant") {
177
+ const newMessage = {
178
+ id: `reasoning-${chunk.runId + Date.now()}`,
179
+ role: "assistant",
180
+ parts: [
181
+ {
182
+ type: "reasoning",
183
+ text: chunk.payload.text,
184
+ state: "streaming",
185
+ providerMetadata: chunk.payload.providerMetadata
186
+ }
187
+ ],
188
+ metadata
189
+ };
190
+ return [...result, newMessage];
191
+ }
192
+ const parts = [...lastMessage.parts];
193
+ let reasoningPartIndex = parts.findIndex((part) => part.type === "reasoning");
194
+ if (reasoningPartIndex === -1) {
195
+ parts.push({
196
+ type: "reasoning",
197
+ text: chunk.payload.text,
198
+ state: "streaming",
199
+ providerMetadata: chunk.payload.providerMetadata
200
+ });
201
+ } else {
202
+ const reasoningPart = parts[reasoningPartIndex];
203
+ if (reasoningPart.type === "reasoning") {
204
+ parts[reasoningPartIndex] = {
205
+ ...reasoningPart,
206
+ text: reasoningPart.text + chunk.payload.text,
207
+ state: "streaming"
208
+ };
209
+ }
210
+ }
211
+ return [
212
+ ...result.slice(0, -1),
213
+ {
214
+ ...lastMessage,
215
+ parts
216
+ }
217
+ ];
218
+ }
219
+ case "tool-call": {
220
+ const lastMessage = result[result.length - 1];
221
+ if (!lastMessage || lastMessage.role !== "assistant") {
222
+ const newMessage = {
223
+ id: `tool-call-${chunk.runId + Date.now()}`,
224
+ role: "assistant",
225
+ parts: [
226
+ {
227
+ type: "dynamic-tool",
228
+ toolName: chunk.payload.toolName,
229
+ toolCallId: chunk.payload.toolCallId,
230
+ state: "input-available",
231
+ input: chunk.payload.args,
232
+ callProviderMetadata: chunk.payload.providerMetadata
233
+ }
234
+ ],
235
+ metadata
236
+ };
237
+ return [...result, newMessage];
238
+ }
239
+ const parts = [...lastMessage.parts];
240
+ parts.push({
241
+ type: "dynamic-tool",
242
+ toolName: chunk.payload.toolName,
243
+ toolCallId: chunk.payload.toolCallId,
244
+ state: "input-available",
245
+ input: chunk.payload.args,
246
+ callProviderMetadata: chunk.payload.providerMetadata
247
+ });
248
+ return [
249
+ ...result.slice(0, -1),
250
+ {
251
+ ...lastMessage,
252
+ parts
253
+ }
254
+ ];
255
+ }
256
+ case "tool-error":
257
+ case "tool-result": {
258
+ const lastMessage = result[result.length - 1];
259
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
260
+ const parts = [...lastMessage.parts];
261
+ const toolPartIndex = parts.findIndex(
262
+ (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
263
+ );
264
+ if (toolPartIndex !== -1) {
265
+ const toolPart = parts[toolPartIndex];
266
+ if (toolPart.type === "dynamic-tool") {
267
+ if (chunk.type === "tool-result" && chunk.payload.isError || chunk.type === "tool-error") {
268
+ const error = chunk.type === "tool-error" ? chunk.payload.error : chunk.payload.result;
269
+ parts[toolPartIndex] = {
270
+ type: "dynamic-tool",
271
+ toolName: toolPart.toolName,
272
+ toolCallId: toolPart.toolCallId,
273
+ state: "output-error",
274
+ input: toolPart.input,
275
+ errorText: String(error),
276
+ callProviderMetadata: chunk.payload.providerMetadata
277
+ };
278
+ } else {
279
+ const isWorkflow = Boolean(chunk.payload.result?.result?.steps);
280
+ const isAgent = chunk?.from === "AGENT";
281
+ let output;
282
+ if (isWorkflow) {
283
+ output = chunk.payload.result?.result;
284
+ } else if (isAgent) {
285
+ output = parts[toolPartIndex].output ?? chunk.payload.result;
286
+ } else {
287
+ output = chunk.payload.result;
288
+ }
289
+ parts[toolPartIndex] = {
290
+ type: "dynamic-tool",
291
+ toolName: toolPart.toolName,
292
+ toolCallId: toolPart.toolCallId,
293
+ state: "output-available",
294
+ input: toolPart.input,
295
+ output,
296
+ callProviderMetadata: chunk.payload.providerMetadata
297
+ };
298
+ }
299
+ }
300
+ }
301
+ return [
302
+ ...result.slice(0, -1),
303
+ {
304
+ ...lastMessage,
305
+ parts
306
+ }
307
+ ];
308
+ }
309
+ case "tool-output": {
310
+ const lastMessage = result[result.length - 1];
311
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
312
+ const parts = [...lastMessage.parts];
313
+ const toolPartIndex = parts.findIndex(
314
+ (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
315
+ );
316
+ if (toolPartIndex !== -1) {
317
+ const toolPart = parts[toolPartIndex];
318
+ if (toolPart.type === "dynamic-tool") {
319
+ if (chunk.payload.output?.type?.startsWith("workflow-")) {
320
+ const existingWorkflowState = toolPart.output || {};
321
+ const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
322
+ existingWorkflowState,
323
+ chunk.payload.output
324
+ );
325
+ parts[toolPartIndex] = {
326
+ ...toolPart,
327
+ output: updatedWorkflowState
328
+ };
329
+ } else if (chunk.payload.output?.from === "AGENT" || chunk.payload.output?.from === "USER" && chunk.payload.output?.payload?.output?.type?.startsWith("workflow-")) {
330
+ return toUIMessageFromAgent(chunk.payload.output, conversation);
331
+ } else {
332
+ const currentOutput = toolPart.output || [];
333
+ const existingOutput = Array.isArray(currentOutput) ? currentOutput : [];
334
+ parts[toolPartIndex] = {
335
+ ...toolPart,
336
+ output: [...existingOutput, chunk.payload.output]
337
+ };
338
+ }
339
+ }
340
+ }
341
+ return [
342
+ ...result.slice(0, -1),
343
+ {
344
+ ...lastMessage,
345
+ parts
346
+ }
347
+ ];
348
+ }
349
+ case "source": {
350
+ const lastMessage = result[result.length - 1];
351
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
352
+ const parts = [...lastMessage.parts];
353
+ if (chunk.payload.sourceType === "url") {
354
+ parts.push({
355
+ type: "source-url",
356
+ sourceId: chunk.payload.id,
357
+ url: chunk.payload.url || "",
358
+ title: chunk.payload.title,
359
+ providerMetadata: chunk.payload.providerMetadata
360
+ });
361
+ } else if (chunk.payload.sourceType === "document") {
362
+ parts.push({
363
+ type: "source-document",
364
+ sourceId: chunk.payload.id,
365
+ mediaType: chunk.payload.mimeType || "application/octet-stream",
366
+ title: chunk.payload.title,
367
+ filename: chunk.payload.filename,
368
+ providerMetadata: chunk.payload.providerMetadata
369
+ });
370
+ }
371
+ return [
372
+ ...result.slice(0, -1),
373
+ {
374
+ ...lastMessage,
375
+ parts
376
+ }
377
+ ];
378
+ }
379
+ case "file": {
380
+ const lastMessage = result[result.length - 1];
381
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
382
+ const parts = [...lastMessage.parts];
383
+ let url;
384
+ if (typeof chunk.payload.data === "string") {
385
+ url = chunk.payload.base64 ? `data:${chunk.payload.mimeType};base64,${chunk.payload.data}` : `data:${chunk.payload.mimeType},${encodeURIComponent(chunk.payload.data)}`;
386
+ } else {
387
+ const base64 = btoa(String.fromCharCode(...chunk.payload.data));
388
+ url = `data:${chunk.payload.mimeType};base64,${base64}`;
389
+ }
390
+ parts.push({
391
+ type: "file",
392
+ mediaType: chunk.payload.mimeType,
393
+ url,
394
+ providerMetadata: chunk.payload.providerMetadata
395
+ });
396
+ return [
397
+ ...result.slice(0, -1),
398
+ {
399
+ ...lastMessage,
400
+ parts
401
+ }
402
+ ];
403
+ }
404
+ case "tool-call-approval": {
405
+ const lastMessage = result[result.length - 1];
406
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
407
+ const lastRequireApprovalMetadata = lastMessage.metadata?.mode === "stream" ? lastMessage.metadata?.requireApprovalMetadata : {};
408
+ return [
409
+ ...result.slice(0, -1),
410
+ {
411
+ ...lastMessage,
412
+ metadata: {
413
+ ...lastMessage.metadata,
414
+ mode: "stream",
415
+ requireApprovalMetadata: {
416
+ ...lastRequireApprovalMetadata,
417
+ [chunk.payload.toolCallId]: {
418
+ toolCallId: chunk.payload.toolCallId,
419
+ toolName: chunk.payload.toolName,
420
+ args: chunk.payload.args
421
+ }
422
+ }
423
+ }
424
+ }
425
+ ];
426
+ }
427
+ case "finish": {
428
+ const lastMessage = result[result.length - 1];
429
+ if (!lastMessage || lastMessage.role !== "assistant") return result;
430
+ const parts = lastMessage.parts.map((part) => {
431
+ if (typeof part === "object" && part !== null && "type" in part && "state" in part && part.state === "streaming") {
432
+ if (part.type === "text" || part.type === "reasoning") {
433
+ return { ...part, state: "done" };
434
+ }
435
+ }
436
+ return part;
437
+ });
438
+ return [
439
+ ...result.slice(0, -1),
440
+ {
441
+ ...lastMessage,
442
+ parts
443
+ }
444
+ ];
445
+ }
446
+ case "error": {
447
+ const newMessage = {
448
+ id: `error-${chunk.runId + Date.now()}`,
449
+ role: "assistant",
450
+ parts: [
451
+ {
452
+ type: "text",
453
+ text: typeof chunk.payload.error === "string" ? chunk.payload.error : JSON.stringify(chunk.payload.error)
454
+ }
455
+ ],
456
+ metadata: {
457
+ ...metadata,
458
+ status: "error"
459
+ }
460
+ };
461
+ return [...result, newMessage];
462
+ }
463
+ // For all other chunk types, return conversation unchanged
464
+ default:
465
+ return result;
466
+ }
467
+ };
468
+ const toUIMessageFromAgent = (chunk, conversation, metadata) => {
469
+ const lastMessage = conversation[conversation.length - 1];
470
+ if (!lastMessage || lastMessage.role !== "assistant") return conversation;
471
+ const parts = [...lastMessage.parts];
472
+ if (chunk.type === "text-delta") {
473
+ const agentChunk = chunk.payload;
474
+ const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
475
+ if (toolPartIndex === -1) return conversation;
476
+ const toolPart = parts[toolPartIndex];
477
+ const childMessages = toolPart?.output?.childMessages || [];
478
+ const lastChildMessage = childMessages[childMessages.length - 1];
479
+ const textMessage = { type: "text", content: (lastChildMessage?.content || "") + agentChunk.text };
480
+ const nextMessages = lastChildMessage?.type === "text" ? [...childMessages.slice(0, -1), textMessage] : [...childMessages, textMessage];
481
+ parts[toolPartIndex] = {
482
+ ...toolPart,
483
+ output: {
484
+ childMessages: nextMessages
485
+ }
486
+ };
487
+ } else if (chunk.type === "tool-call") {
488
+ const agentChunk = chunk.payload;
489
+ const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
490
+ if (toolPartIndex === -1) return conversation;
491
+ const toolPart = parts[toolPartIndex];
492
+ const childMessages = toolPart?.output?.childMessages || [];
493
+ parts[toolPartIndex] = {
494
+ ...toolPart,
495
+ output: {
496
+ ...toolPart?.output,
497
+ childMessages: [
498
+ ...childMessages,
499
+ {
500
+ type: "tool",
501
+ toolCallId: agentChunk.toolCallId,
502
+ toolName: agentChunk.toolName,
503
+ args: agentChunk.args
504
+ }
505
+ ]
506
+ }
507
+ };
508
+ } else if (chunk.type === "tool-output") {
509
+ const agentChunk = chunk.payload;
510
+ const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
511
+ if (toolPartIndex === -1) return conversation;
512
+ const toolPart = parts[toolPartIndex];
513
+ if (agentChunk?.output?.type?.startsWith("workflow-")) {
514
+ const childMessages = toolPart?.output?.childMessages || [];
515
+ const lastToolIndex = childMessages.length - 1;
516
+ const currentMessage = childMessages[lastToolIndex];
517
+ const actualExistingWorkflowState = currentMessage?.toolOutput || {};
518
+ const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(actualExistingWorkflowState, agentChunk.output);
519
+ if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
520
+ parts[toolPartIndex] = {
521
+ ...toolPart,
522
+ output: {
523
+ ...toolPart?.output,
524
+ childMessages: [
525
+ ...childMessages.slice(0, -1),
526
+ {
527
+ ...currentMessage,
528
+ toolOutput: { ...updatedWorkflowState, runId: agentChunk.output.runId }
529
+ }
530
+ ]
531
+ }
532
+ };
533
+ }
534
+ }
535
+ } else if (chunk.type === "tool-result") {
536
+ const agentChunk = chunk.payload;
537
+ const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
538
+ if (toolPartIndex === -1) return conversation;
539
+ const toolPart = parts[toolPartIndex];
540
+ const childMessages = toolPart?.output?.childMessages || [];
541
+ const lastToolIndex = childMessages.length - 1;
542
+ const isWorkflow = agentChunk?.toolName?.startsWith("workflow-");
543
+ if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
544
+ parts[toolPartIndex] = {
545
+ ...toolPart,
546
+ output: {
547
+ ...toolPart?.output,
548
+ childMessages: [
549
+ ...childMessages.slice(0, -1),
550
+ {
551
+ ...childMessages[lastToolIndex],
552
+ toolOutput: isWorkflow ? { ...agentChunk.result?.result, runId: agentChunk.result?.runId } : agentChunk.result
553
+ }
554
+ ]
555
+ }
556
+ };
557
+ }
558
+ }
559
+ return [
560
+ ...conversation.slice(0, -1),
561
+ {
562
+ ...lastMessage,
563
+ parts
564
+ }
565
+ ];
566
+ };
567
+
568
+ const toAssistantUIMessage = (message) => {
569
+ const extendedMessage = message;
570
+ const content = message.parts.map((part) => {
571
+ if (part.type === "text") {
572
+ return {
573
+ type: "text",
574
+ text: part.text,
575
+ metadata: message.metadata
576
+ };
577
+ }
578
+ if (part.type === "reasoning") {
579
+ return {
580
+ type: "reasoning",
581
+ text: part.text,
582
+ metadata: message.metadata
583
+ };
584
+ }
585
+ if (part.type === "source-url") {
586
+ return {
587
+ type: "source",
588
+ sourceType: "url",
589
+ id: part.sourceId,
590
+ url: part.url,
591
+ title: part.title,
592
+ metadata: message.metadata
593
+ };
594
+ }
595
+ if (part.type === "source-document") {
596
+ return {
597
+ type: "file",
598
+ filename: part.filename,
599
+ mimeType: part.mediaType,
600
+ data: "",
601
+ // Source documents don't have inline data
602
+ metadata: message.metadata
603
+ };
604
+ }
605
+ if (part.type === "file") {
606
+ const type = part.mediaType.includes("image/") ? "image" : "file";
607
+ if (type === "file") {
608
+ return {
609
+ type,
610
+ mimeType: part.mediaType,
611
+ data: part.url,
612
+ // Use URL as data source
613
+ metadata: message.metadata
614
+ };
615
+ }
616
+ if (type === "image") {
617
+ return {
618
+ type,
619
+ image: part.url,
620
+ metadata: message.metadata
621
+ };
622
+ }
623
+ }
624
+ if (part.type === "dynamic-tool") {
625
+ const baseToolCall = {
626
+ type: "tool-call",
627
+ toolCallId: part.toolCallId,
628
+ toolName: part.toolName,
629
+ argsText: JSON.stringify(part.input),
630
+ args: part.input,
631
+ metadata: message.metadata
632
+ };
633
+ if (part.state === "output-error" && "errorText" in part) {
634
+ return { ...baseToolCall, result: part.errorText, isError: true };
635
+ }
636
+ if ("output" in part) {
637
+ return { ...baseToolCall, result: part.output };
638
+ }
639
+ return baseToolCall;
640
+ }
641
+ if (part.type.startsWith("tool-") && part.state !== "input-available") {
642
+ const toolName = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.substring(5);
643
+ const baseToolCall = {
644
+ type: "tool-call",
645
+ toolCallId: "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : "",
646
+ toolName,
647
+ argsText: "input" in part ? JSON.stringify(part.input) : "{}",
648
+ args: "input" in part ? part.input : {},
649
+ metadata: message.metadata
650
+ };
651
+ if ("output" in part) {
652
+ return { ...baseToolCall, result: part.output };
653
+ } else if ("error" in part) {
654
+ return { ...baseToolCall, result: part.error, isError: true };
655
+ }
656
+ return baseToolCall;
657
+ }
658
+ return {
659
+ type: "text",
660
+ text: "",
661
+ metadata: message.metadata
662
+ };
663
+ });
664
+ let status;
665
+ if (message.role === "assistant" && content.length > 0) {
666
+ const hasStreamingParts = message.parts.some(
667
+ (part) => part.type === "text" && "state" in part && part.state === "streaming" || part.type === "reasoning" && "state" in part && part.state === "streaming"
668
+ );
669
+ const hasToolCalls = message.parts.some((part) => part.type === "dynamic-tool" || part.type.startsWith("tool-"));
670
+ const hasInputAvailableTools = message.parts.some(
671
+ (part) => part.type === "dynamic-tool" && part.state === "input-available"
672
+ );
673
+ const hasErrorTools = message.parts.some(
674
+ (part) => part.type === "dynamic-tool" && part.state === "output-error" || part.type.startsWith("tool-") && "error" in part
675
+ );
676
+ if (hasStreamingParts) {
677
+ status = { type: "running" };
678
+ } else if (hasInputAvailableTools && hasToolCalls) {
679
+ status = { type: "requires-action", reason: "tool-calls" };
680
+ } else if (hasErrorTools) {
681
+ status = { type: "incomplete", reason: "error" };
682
+ } else {
683
+ status = { type: "complete", reason: "stop" };
684
+ }
685
+ }
686
+ const threadMessage = {
687
+ role: message.role,
688
+ content,
689
+ id: message.id,
690
+ createdAt: extendedMessage.createdAt,
691
+ status,
692
+ attachments: extendedMessage.experimental_attachments
693
+ };
694
+ return threadMessage;
695
+ };
696
+
697
+ const resolveInitialMessages = (messages) => {
698
+ return messages.map((message) => {
699
+ const networkPart = message.parts.find(
700
+ (part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
701
+ );
702
+ if (networkPart && networkPart.type === "text") {
703
+ try {
704
+ const json = JSON.parse(networkPart.text);
705
+ if (json.isNetwork === true) {
706
+ const selectionReason = json.selectionReason || "";
707
+ const primitiveType = json.primitiveType || "";
708
+ const primitiveId = json.primitiveId || "";
709
+ const finalResult = json.finalResult;
710
+ const toolCalls = finalResult?.toolCalls || [];
711
+ const childMessages = [];
712
+ for (const toolCall of toolCalls) {
713
+ if (toolCall.type === "tool-call" && toolCall.payload) {
714
+ const toolCallId = toolCall.payload.toolCallId;
715
+ let toolResult;
716
+ for (const message2 of finalResult?.messages || []) {
717
+ for (const part of message2.content || []) {
718
+ if (typeof part === "object" && part.type === "tool-result" && part.toolCallId === toolCallId) {
719
+ toolResult = part;
720
+ break;
721
+ }
722
+ }
723
+ }
724
+ const isWorkflow = Boolean(toolResult?.result?.result?.steps);
725
+ childMessages.push({
726
+ type: "tool",
727
+ toolCallId: toolCall.payload.toolCallId,
728
+ toolName: toolCall.payload.toolName,
729
+ args: toolCall.payload.args,
730
+ toolOutput: isWorkflow ? toolResult?.result?.result : toolResult?.result
731
+ });
732
+ }
733
+ }
734
+ if (finalResult && finalResult.text) {
735
+ childMessages.push({
736
+ type: "text",
737
+ content: finalResult.text
738
+ });
739
+ }
740
+ const result = {
741
+ childMessages,
742
+ result: finalResult?.text || ""
743
+ };
744
+ console.log("json", json);
745
+ const nextMessage = {
746
+ role: "assistant",
747
+ parts: [
748
+ {
749
+ type: "dynamic-tool",
750
+ toolCallId: primitiveId,
751
+ toolName: primitiveId,
752
+ state: "output-available",
753
+ input: json.input,
754
+ output: result
755
+ }
756
+ ],
757
+ id: message.id,
758
+ metadata: {
759
+ ...message.metadata,
760
+ mode: "network",
761
+ selectionReason,
762
+ agentInput: json.input,
763
+ from: primitiveType === "agent" ? "AGENT" : "WORKFLOW"
764
+ }
765
+ };
766
+ return nextMessage;
767
+ }
768
+ } catch (error) {
769
+ return message;
770
+ }
771
+ }
772
+ return message;
773
+ });
774
+ };
775
+ const resolveToChildMessages = (messages) => {
776
+ const assistantMessage = messages.find((message) => message.role === "assistant");
777
+ if (!assistantMessage) return [];
778
+ const parts = assistantMessage.parts;
779
+ let childMessages = [];
780
+ for (const part of parts) {
781
+ const toolPart = part;
782
+ if (part.type.startsWith("tool-")) {
783
+ const toolName = part.type.substring("tool-".length);
784
+ const isWorkflow = toolName.startsWith("workflow-");
785
+ childMessages.push({
786
+ type: "tool",
787
+ toolCallId: toolPart.toolCallId,
788
+ toolName,
789
+ args: toolPart.input,
790
+ toolOutput: isWorkflow ? { ...toolPart.output?.result, runId: toolPart.output?.runId } : toolPart.output
791
+ });
792
+ }
793
+ if (part.type === "text") {
794
+ childMessages.push({
795
+ type: "text",
796
+ content: toolPart.text
797
+ });
798
+ }
799
+ }
800
+ return childMessages;
801
+ };
802
+
803
+ class AISdkNetworkTransformer {
804
+ transform({ chunk, conversation, metadata }) {
805
+ const newConversation = [...conversation];
806
+ if (chunk.type === "routing-agent-text-delta") {
807
+ return this.handleRoutingAgentConversation(chunk, newConversation);
808
+ }
809
+ if (chunk.type.startsWith("agent-execution-")) {
810
+ return this.handleAgentConversation(chunk, newConversation, metadata);
811
+ }
812
+ if (chunk.type.startsWith("workflow-execution-")) {
813
+ return this.handleWorkflowConversation(chunk, newConversation, metadata);
814
+ }
815
+ if (chunk.type.startsWith("tool-execution-")) {
816
+ return this.handleToolConversation(chunk, newConversation, metadata);
817
+ }
818
+ if (chunk.type === "network-execution-event-step-finish") {
819
+ const lastMessage = newConversation[newConversation.length - 1];
820
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
821
+ const agentChunk = chunk.payload;
822
+ const parts = [...lastMessage.parts];
823
+ const textPartIndex = parts.findIndex((part) => part.type === "text");
824
+ if (textPartIndex === -1) {
825
+ parts.push({
826
+ type: "text",
827
+ text: agentChunk.result,
828
+ state: "done"
829
+ });
830
+ return [
831
+ ...newConversation.slice(0, -1),
832
+ {
833
+ ...lastMessage,
834
+ parts
835
+ }
836
+ ];
837
+ }
838
+ const textPart = parts[textPartIndex];
839
+ if (textPart.type === "text") {
840
+ parts[textPartIndex] = {
841
+ ...textPart,
842
+ state: "done"
843
+ };
844
+ return [
845
+ ...newConversation.slice(0, -1),
846
+ {
847
+ ...lastMessage,
848
+ parts
849
+ }
850
+ ];
851
+ }
852
+ return newConversation;
853
+ }
854
+ return newConversation;
855
+ }
856
+ handleRoutingAgentConversation = (chunk, newConversation) => {
857
+ const lastMessage = newConversation[newConversation.length - 1];
858
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
859
+ const agentChunk = chunk.payload;
860
+ const parts = [...lastMessage.parts];
861
+ const textPartIndex = parts.findIndex((part) => part.type === "text");
862
+ if (textPartIndex === -1) {
863
+ parts.push({
864
+ type: "text",
865
+ text: agentChunk.text,
866
+ state: "streaming"
867
+ });
868
+ return [
869
+ ...newConversation.slice(0, -1),
870
+ {
871
+ ...lastMessage,
872
+ parts
873
+ }
874
+ ];
875
+ }
876
+ const textPart = parts[textPartIndex];
877
+ if (textPart.type === "text") {
878
+ parts[textPartIndex] = {
879
+ ...textPart,
880
+ text: textPart.text + agentChunk.text,
881
+ state: "streaming"
882
+ };
883
+ return [
884
+ ...newConversation.slice(0, -1),
885
+ {
886
+ ...lastMessage,
887
+ parts
888
+ }
889
+ ];
890
+ }
891
+ return newConversation;
892
+ };
893
+ handleAgentConversation = (chunk, newConversation, metadata) => {
894
+ if (chunk.type === "agent-execution-start") {
895
+ const primitiveId = chunk.payload?.args?.primitiveId;
896
+ const runId = chunk.payload.runId;
897
+ if (!primitiveId || !runId) return newConversation;
898
+ const newMessage = {
899
+ id: `agent-execution-start-${runId}-${Date.now()}`,
900
+ role: "assistant",
901
+ parts: [
902
+ {
903
+ type: "dynamic-tool",
904
+ toolName: primitiveId,
905
+ toolCallId: runId,
906
+ state: "input-available",
907
+ input: chunk.payload.args
908
+ }
909
+ ],
910
+ metadata: {
911
+ ...metadata,
912
+ selectionReason: chunk.payload?.args?.selectionReason || "",
913
+ agentInput: chunk.payload?.args?.task,
914
+ mode: "network",
915
+ from: "AGENT"
916
+ }
917
+ };
918
+ return [...newConversation, newMessage];
919
+ }
920
+ if (chunk.type === "agent-execution-end") {
921
+ const lastMessage = newConversation[newConversation.length - 1];
922
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
923
+ const parts = [...lastMessage.parts];
924
+ const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
925
+ if (toolPartIndex !== -1) {
926
+ const toolPart = parts[toolPartIndex];
927
+ if (toolPart.type === "dynamic-tool") {
928
+ const currentOutput = toolPart.output;
929
+ parts[toolPartIndex] = {
930
+ type: "dynamic-tool",
931
+ toolName: toolPart.toolName,
932
+ toolCallId: toolPart.toolCallId,
933
+ state: "output-available",
934
+ input: toolPart.input,
935
+ output: {
936
+ ...currentOutput,
937
+ result: currentOutput?.result || chunk.payload?.result || ""
938
+ }
939
+ };
940
+ }
941
+ }
942
+ return [
943
+ ...newConversation.slice(0, -1),
944
+ {
945
+ ...lastMessage,
946
+ parts
947
+ }
948
+ ];
949
+ }
950
+ if (chunk.type.startsWith("agent-execution-event-")) {
951
+ const lastMessage = newConversation[newConversation.length - 1];
952
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
953
+ const agentChunk = chunk.payload;
954
+ const parts = [...lastMessage.parts];
955
+ const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
956
+ if (toolPartIndex === -1) return newConversation;
957
+ const toolPart = parts[toolPartIndex];
958
+ if (agentChunk.type === "text-delta") {
959
+ const childMessages = toolPart?.output?.childMessages || [];
960
+ const lastChildMessage = childMessages[childMessages.length - 1];
961
+ const textMessage = { type: "text", content: (lastChildMessage?.content || "") + agentChunk.payload.text };
962
+ const nextMessages = lastChildMessage?.type === "text" ? [...childMessages.slice(0, -1), textMessage] : [...childMessages, textMessage];
963
+ parts[toolPartIndex] = {
964
+ ...toolPart,
965
+ output: {
966
+ childMessages: nextMessages
967
+ }
968
+ };
969
+ } else if (agentChunk.type === "tool-call") {
970
+ const childMessages = toolPart?.output?.childMessages || [];
971
+ parts[toolPartIndex] = {
972
+ ...toolPart,
973
+ output: {
974
+ ...toolPart?.output,
975
+ childMessages: [
976
+ ...childMessages,
977
+ {
978
+ type: "tool",
979
+ toolCallId: agentChunk.payload.toolCallId,
980
+ toolName: agentChunk.payload.toolName,
981
+ args: agentChunk.payload.args
982
+ }
983
+ ]
984
+ }
985
+ };
986
+ } else if (agentChunk.type === "tool-output") {
987
+ if (agentChunk.payload?.output?.type?.startsWith("workflow-")) {
988
+ const childMessages = toolPart?.output?.childMessages || [];
989
+ const lastToolIndex = childMessages.length - 1;
990
+ const currentMessage = childMessages[lastToolIndex];
991
+ const actualExistingWorkflowState = currentMessage?.toolOutput || {};
992
+ const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
993
+ actualExistingWorkflowState,
994
+ agentChunk.payload.output
995
+ );
996
+ if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
997
+ parts[toolPartIndex] = {
998
+ ...toolPart,
999
+ output: {
1000
+ ...toolPart?.output,
1001
+ childMessages: [
1002
+ ...childMessages.slice(0, -1),
1003
+ {
1004
+ ...currentMessage,
1005
+ toolOutput: updatedWorkflowState
1006
+ }
1007
+ ]
1008
+ }
1009
+ };
1010
+ }
1011
+ }
1012
+ } else if (agentChunk.type === "tool-result") {
1013
+ const childMessages = toolPart?.output?.childMessages || [];
1014
+ const lastToolIndex = childMessages.length - 1;
1015
+ const isWorkflow = Boolean(agentChunk.payload?.result?.result?.steps);
1016
+ if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
1017
+ parts[toolPartIndex] = {
1018
+ ...toolPart,
1019
+ output: {
1020
+ ...toolPart?.output,
1021
+ childMessages: [
1022
+ ...childMessages.slice(0, -1),
1023
+ {
1024
+ ...childMessages[lastToolIndex],
1025
+ toolOutput: isWorkflow ? agentChunk.payload.result.result : agentChunk.payload.result
1026
+ }
1027
+ ]
1028
+ }
1029
+ };
1030
+ }
1031
+ }
1032
+ return [
1033
+ ...newConversation.slice(0, -1),
1034
+ {
1035
+ ...lastMessage,
1036
+ parts
1037
+ }
1038
+ ];
1039
+ }
1040
+ return newConversation;
1041
+ };
1042
+ handleWorkflowConversation = (chunk, newConversation, metadata) => {
1043
+ if (chunk.type === "workflow-execution-start") {
1044
+ const primitiveId = chunk.payload?.args?.primitiveId;
1045
+ const runId = chunk.payload.runId;
1046
+ if (!primitiveId || !runId) return newConversation;
1047
+ let agentInput;
1048
+ try {
1049
+ agentInput = JSON.parse(chunk?.payload?.args?.prompt);
1050
+ } catch (e) {
1051
+ agentInput = chunk?.payload?.args?.prompt;
1052
+ }
1053
+ const newMessage = {
1054
+ id: `workflow-start-${runId}-${Date.now()}`,
1055
+ role: "assistant",
1056
+ parts: [
1057
+ {
1058
+ type: "dynamic-tool",
1059
+ toolName: primitiveId,
1060
+ toolCallId: runId,
1061
+ state: "input-available",
1062
+ input: chunk.payload.args
1063
+ }
1064
+ ],
1065
+ metadata: {
1066
+ ...metadata,
1067
+ selectionReason: chunk.payload?.args?.selectionReason || "",
1068
+ from: "WORKFLOW",
1069
+ mode: "network",
1070
+ agentInput
1071
+ }
1072
+ };
1073
+ return [...newConversation, newMessage];
1074
+ }
1075
+ if (chunk.type.startsWith("workflow-execution-event-")) {
1076
+ const lastMessage = newConversation[newConversation.length - 1];
1077
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
1078
+ const parts = [...lastMessage.parts];
1079
+ const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
1080
+ if (toolPartIndex === -1) return newConversation;
1081
+ const toolPart = parts[toolPartIndex];
1082
+ if (toolPart.type !== "dynamic-tool") return newConversation;
1083
+ const existingWorkflowState = toolPart.output || {};
1084
+ const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(existingWorkflowState, chunk.payload);
1085
+ parts[toolPartIndex] = {
1086
+ ...toolPart,
1087
+ output: updatedWorkflowState
1088
+ };
1089
+ return [
1090
+ ...newConversation.slice(0, -1),
1091
+ {
1092
+ ...lastMessage,
1093
+ parts
1094
+ }
1095
+ ];
1096
+ }
1097
+ return newConversation;
1098
+ };
1099
+ handleToolConversation = (chunk, newConversation, metadata) => {
1100
+ if (chunk.type === "tool-execution-start") {
1101
+ const { args: argsData } = chunk.payload;
1102
+ const lastMessage = newConversation[newConversation.length - 1];
1103
+ const nestedArgs = argsData.args || {};
1104
+ if (!lastMessage || lastMessage.role !== "assistant") {
1105
+ const newMessage = {
1106
+ id: `tool-start-${chunk.runId}-${Date.now()}`,
1107
+ role: "assistant",
1108
+ parts: [
1109
+ {
1110
+ type: "dynamic-tool",
1111
+ toolName: argsData.toolName || "unknown",
1112
+ toolCallId: argsData.toolCallId || "unknown",
1113
+ state: "input-available",
1114
+ input: nestedArgs
1115
+ }
1116
+ ],
1117
+ metadata: {
1118
+ ...metadata,
1119
+ selectionReason: metadata?.mode === "network" ? metadata.selectionReason || argsData.selectionReason : "",
1120
+ mode: "network",
1121
+ agentInput: nestedArgs
1122
+ }
1123
+ };
1124
+ return [...newConversation, newMessage];
1125
+ }
1126
+ const parts = [...lastMessage.parts];
1127
+ parts.push({
1128
+ type: "dynamic-tool",
1129
+ toolName: argsData.toolName || "unknown",
1130
+ toolCallId: argsData.toolCallId || "unknown",
1131
+ state: "input-available",
1132
+ input: nestedArgs
1133
+ });
1134
+ return [
1135
+ ...newConversation.slice(0, -1),
1136
+ {
1137
+ ...lastMessage,
1138
+ parts
1139
+ }
1140
+ ];
1141
+ }
1142
+ if (chunk.type === "tool-execution-end") {
1143
+ const lastMessage = newConversation[newConversation.length - 1];
1144
+ if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
1145
+ const parts = [...lastMessage.parts];
1146
+ const toolPartIndex = parts.findIndex(
1147
+ (part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
1148
+ );
1149
+ if (toolPartIndex !== -1) {
1150
+ const toolPart = parts[toolPartIndex];
1151
+ if (toolPart.type === "dynamic-tool") {
1152
+ const currentOutput = toolPart.output;
1153
+ parts[toolPartIndex] = {
1154
+ type: "dynamic-tool",
1155
+ toolName: toolPart.toolName,
1156
+ toolCallId: toolPart.toolCallId,
1157
+ state: "output-available",
1158
+ input: toolPart.input,
1159
+ output: currentOutput?.result || chunk.payload?.result || ""
1160
+ };
1161
+ }
1162
+ }
1163
+ return [
1164
+ ...newConversation.slice(0, -1),
1165
+ {
1166
+ ...lastMessage,
1167
+ parts
1168
+ }
1169
+ ];
1170
+ }
1171
+ return newConversation;
1172
+ };
1173
+ }
1174
+
1175
+ const fromCoreUserMessageToUIMessage = (coreUserMessage) => {
1176
+ const id = `user-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
1177
+ const parts = typeof coreUserMessage.content === "string" ? [
1178
+ {
1179
+ type: "text",
1180
+ text: coreUserMessage.content
1181
+ }
1182
+ ] : coreUserMessage.content.map((part) => {
1183
+ switch (part.type) {
1184
+ case "text": {
1185
+ return {
1186
+ type: "text",
1187
+ text: part.text
1188
+ };
1189
+ }
1190
+ case "image": {
1191
+ const url = typeof part.image === "string" ? part.image : part.image instanceof URL ? part.image.toString() : "";
1192
+ return {
1193
+ type: "file",
1194
+ mediaType: part.mimeType ?? "image/*",
1195
+ url
1196
+ };
1197
+ }
1198
+ case "file": {
1199
+ const url = typeof part.data === "string" ? part.data : part.data instanceof URL ? part.data.toString() : "";
1200
+ return {
1201
+ type: "file",
1202
+ mediaType: part.mimeType,
1203
+ url,
1204
+ ...part.filename !== void 0 ? { filename: part.filename } : {}
1205
+ };
1206
+ }
1207
+ default: {
1208
+ const exhaustiveCheck = part;
1209
+ throw new Error(`Unhandled content part type: ${exhaustiveCheck.type}`);
1210
+ }
1211
+ }
1212
+ });
1213
+ return {
1214
+ id,
1215
+ role: "user",
1216
+ parts
1217
+ };
1218
+ };
1219
+
1220
+ const useChat = ({ agentId, initializeMessages }) => {
1221
+ const _currentRunId = react.useRef(void 0);
1222
+ const _onChunk = react.useRef(void 0);
1223
+ const [messages, setMessages] = react.useState(
1224
+ () => resolveInitialMessages(initializeMessages?.() || [])
1225
+ );
1226
+ const [toolCallApprovals, setToolCallApprovals] = react.useState({});
1227
+ const baseClient = useMastraClient();
1228
+ const [isRunning, setIsRunning] = react.useState(false);
1229
+ const generate = async ({
1230
+ coreUserMessages,
1231
+ requestContext,
1232
+ threadId,
1233
+ modelSettings,
1234
+ signal,
1235
+ onFinish
1236
+ }) => {
1237
+ const {
1238
+ frequencyPenalty,
1239
+ presencePenalty,
1240
+ maxRetries,
1241
+ maxTokens,
1242
+ temperature,
1243
+ topK,
1244
+ topP,
1245
+ instructions,
1246
+ providerOptions,
1247
+ maxSteps
1248
+ } = modelSettings || {};
1249
+ setIsRunning(true);
1250
+ const clientWithAbort = new clientJs.MastraClient({
1251
+ ...baseClient.options,
1252
+ abortSignal: signal
1253
+ });
1254
+ const agent = clientWithAbort.getAgent(agentId);
1255
+ const response = await agent.generate({
1256
+ messages: coreUserMessages,
1257
+ runId: agentId,
1258
+ maxSteps,
1259
+ modelSettings: {
1260
+ frequencyPenalty,
1261
+ presencePenalty,
1262
+ maxRetries,
1263
+ maxOutputTokens: maxTokens,
1264
+ temperature,
1265
+ topK,
1266
+ topP
1267
+ },
1268
+ instructions,
1269
+ requestContext,
1270
+ ...threadId ? { threadId, resourceId: agentId } : {},
1271
+ providerOptions
1272
+ });
1273
+ setIsRunning(false);
1274
+ if (response && "uiMessages" in response.response && response.response.uiMessages) {
1275
+ onFinish?.(response.response.uiMessages);
1276
+ const mastraUIMessages = (response.response.uiMessages || []).map((message) => ({
1277
+ ...message,
1278
+ metadata: {
1279
+ mode: "generate"
1280
+ }
1281
+ }));
1282
+ setMessages((prev) => [...prev, ...mastraUIMessages]);
1283
+ }
1284
+ };
1285
+ const stream = async ({ coreUserMessages, requestContext, threadId, onChunk, modelSettings, signal }) => {
1286
+ const {
1287
+ frequencyPenalty,
1288
+ presencePenalty,
1289
+ maxRetries,
1290
+ maxTokens,
1291
+ temperature,
1292
+ topK,
1293
+ topP,
1294
+ instructions,
1295
+ providerOptions,
1296
+ maxSteps,
1297
+ requireToolApproval
1298
+ } = modelSettings || {};
1299
+ setIsRunning(true);
1300
+ const clientWithAbort = new clientJs.MastraClient({
1301
+ ...baseClient.options,
1302
+ abortSignal: signal
1303
+ });
1304
+ const agent = clientWithAbort.getAgent(agentId);
1305
+ const runId = agentId;
1306
+ const response = await agent.stream({
1307
+ messages: coreUserMessages,
1308
+ runId,
1309
+ maxSteps,
1310
+ modelSettings: {
1311
+ frequencyPenalty,
1312
+ presencePenalty,
1313
+ maxRetries,
1314
+ maxOutputTokens: maxTokens,
1315
+ temperature,
1316
+ topK,
1317
+ topP
1318
+ },
1319
+ instructions,
1320
+ requestContext,
1321
+ ...threadId ? { threadId, resourceId: agentId } : {},
1322
+ providerOptions,
1323
+ requireToolApproval
1324
+ });
1325
+ _onChunk.current = onChunk;
1326
+ _currentRunId.current = runId;
1327
+ await response.processDataStream({
1328
+ onChunk: async (chunk) => {
1329
+ setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
1330
+ onChunk?.(chunk);
1331
+ }
1332
+ });
1333
+ setIsRunning(false);
1334
+ };
1335
+ const network = async ({
1336
+ coreUserMessages,
1337
+ requestContext,
1338
+ threadId,
1339
+ onNetworkChunk,
1340
+ modelSettings,
1341
+ signal
1342
+ }) => {
1343
+ const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, maxSteps } = modelSettings || {};
1344
+ setIsRunning(true);
1345
+ const clientWithAbort = new clientJs.MastraClient({
1346
+ ...baseClient.options,
1347
+ abortSignal: signal
1348
+ });
1349
+ const agent = clientWithAbort.getAgent(agentId);
1350
+ const response = await agent.network({
1351
+ messages: coreUserMessages,
1352
+ maxSteps,
1353
+ modelSettings: {
1354
+ frequencyPenalty,
1355
+ presencePenalty,
1356
+ maxRetries,
1357
+ maxOutputTokens: maxTokens,
1358
+ temperature,
1359
+ topK,
1360
+ topP
1361
+ },
1362
+ runId: agentId,
1363
+ requestContext,
1364
+ ...threadId ? { thread: threadId, resourceId: agentId } : {}
1365
+ });
1366
+ const transformer = new AISdkNetworkTransformer();
1367
+ await response.processDataStream({
1368
+ onChunk: async (chunk) => {
1369
+ setMessages((prev) => transformer.transform({ chunk, conversation: prev, metadata: { mode: "network" } }));
1370
+ onNetworkChunk?.(chunk);
1371
+ }
1372
+ });
1373
+ setIsRunning(false);
1374
+ };
1375
+ const handleCancelRun = () => {
1376
+ setIsRunning(false);
1377
+ _currentRunId.current = void 0;
1378
+ _onChunk.current = void 0;
1379
+ };
1380
+ const approveToolCall = async (toolCallId) => {
1381
+ const onChunk = _onChunk.current;
1382
+ const currentRunId = _currentRunId.current;
1383
+ if (!currentRunId)
1384
+ return console.info("[approveToolCall] approveToolCall can only be called after a stream has started");
1385
+ setIsRunning(true);
1386
+ setToolCallApprovals((prev) => ({ ...prev, [toolCallId]: { status: "approved" } }));
1387
+ const agent = baseClient.getAgent(agentId);
1388
+ const response = await agent.approveToolCall({ runId: currentRunId, toolCallId });
1389
+ await response.processDataStream({
1390
+ onChunk: async (chunk) => {
1391
+ setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
1392
+ onChunk?.(chunk);
1393
+ }
1394
+ });
1395
+ setIsRunning(false);
1396
+ };
1397
+ const declineToolCall = async (toolCallId) => {
1398
+ const onChunk = _onChunk.current;
1399
+ const currentRunId = _currentRunId.current;
1400
+ if (!currentRunId)
1401
+ return console.info("[declineToolCall] declineToolCall can only be called after a stream has started");
1402
+ setIsRunning(true);
1403
+ setToolCallApprovals((prev) => ({ ...prev, [toolCallId]: { status: "declined" } }));
1404
+ const agent = baseClient.getAgent(agentId);
1405
+ const response = await agent.declineToolCall({ runId: currentRunId, toolCallId });
1406
+ await response.processDataStream({
1407
+ onChunk: async (chunk) => {
1408
+ setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
1409
+ onChunk?.(chunk);
1410
+ }
1411
+ });
1412
+ setIsRunning(false);
1413
+ };
1414
+ const sendMessage = async ({ mode = "stream", ...args }) => {
1415
+ const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
1416
+ const coreUserMessages = [nextMessage];
1417
+ if (args.coreUserMessages) {
1418
+ coreUserMessages.push(...args.coreUserMessages);
1419
+ }
1420
+ const uiMessages = coreUserMessages.map(fromCoreUserMessageToUIMessage);
1421
+ setMessages((s) => [...s, ...uiMessages]);
1422
+ if (mode === "generate") {
1423
+ await generate({ ...args, coreUserMessages });
1424
+ } else if (mode === "stream") {
1425
+ await stream({ ...args, coreUserMessages });
1426
+ } else if (mode === "network") {
1427
+ await network({ ...args, coreUserMessages });
1428
+ }
1429
+ };
1430
+ return {
1431
+ setMessages,
1432
+ sendMessage,
1433
+ isRunning,
1434
+ messages,
1435
+ approveToolCall,
1436
+ declineToolCall,
1437
+ cancelRun: handleCancelRun,
1438
+ toolCallApprovals
1439
+ };
1440
+ };
1441
+
1442
+ const EntityContext = react.createContext({
1443
+ expanded: false,
1444
+ setExpanded: () => {
1445
+ },
1446
+ variant: "initial",
1447
+ disabled: false
1448
+ });
1449
+ const EntityProvider = EntityContext.Provider;
1450
+ const useEntity = () => react.useContext(EntityContext);
1451
+
1452
+ const IconSizes = {
1453
+ sm: "mastra:[&>svg]:size-3",
1454
+ md: "mastra:[&>svg]:size-4",
1455
+ lg: "mastra:[&>svg]:size-5"
1456
+ };
1457
+ const Icon = ({ children, className, size = "md", ...props }) => {
1458
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || IconSizes[size], ...props, children });
1459
+ };
1460
+
1461
+ const Entity = ({
1462
+ className,
1463
+ variant = "initial",
1464
+ initialExpanded = false,
1465
+ disabled = false,
1466
+ ...props
1467
+ }) => {
1468
+ const [expanded, setExpanded] = react.useState(initialExpanded);
1469
+ return /* @__PURE__ */ jsxRuntime.jsx(EntityProvider, { value: { expanded, setExpanded, variant, disabled }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { className, ...props }) });
1470
+ };
1471
+ const EntityTriggerClass = tailwindMerge.twMerge(
1472
+ "mastra:aria-disabled:cursor-not-allowed mastra:aria-disabled:bg-surface5 mastra:aria-disabled:text-text3",
1473
+ "mastra:aria-expanded:rounded-b-none mastra:aria-expanded:border-b-0",
1474
+ "mastra:bg-surface3 mastra:text-text6 mastra:hover:bg-surface4 mastra:active:bg-surface5",
1475
+ "mastra:rounded-lg mastra:py-2 mastra:px-4 mastra:border mastra:border-border1",
1476
+ "mastra:cursor-pointer mastra:inline-flex mastra:items-center mastra:gap-1 mastra:font-mono"
1477
+ );
1478
+ const EntityTriggerVariantClasses = {
1479
+ agent: "mastra:[&_svg.mastra-icon]:text-accent1",
1480
+ workflow: "mastra:[&_svg.mastra-icon]:text-accent3",
1481
+ tool: "mastra:[&_svg.mastra-icon]:text-accent6",
1482
+ memory: "mastra:[&_svg.mastra-icon]:text-accent2",
1483
+ initial: "mastra:[&_svg.mastra-icon]:text-text3"
1484
+ };
1485
+ const EntityTrigger = ({ className, children, ...props }) => {
1486
+ const { expanded, setExpanded, variant, disabled } = useEntity();
1487
+ const handleClick = (e) => {
1488
+ if (disabled) return;
1489
+ setExpanded(!expanded);
1490
+ props?.onClick?.(e);
1491
+ };
1492
+ return /* @__PURE__ */ jsxRuntime.jsx(
1493
+ "button",
1494
+ {
1495
+ className: className || tailwindMerge.twMerge(EntityTriggerClass, !disabled && EntityTriggerVariantClasses[variant]),
1496
+ ...props,
1497
+ onClick: handleClick,
1498
+ "aria-expanded": expanded,
1499
+ "aria-disabled": disabled,
1500
+ children
1501
+ }
1502
+ );
1503
+ };
1504
+ const EntityContentClass = tailwindMerge.twMerge(
1505
+ "mastra:space-y-4",
1506
+ "mastra:rounded-lg mastra:rounded-tl-none mastra:p-4 mastra:border mastra:border-border1 mastra:-mt-[0.5px]",
1507
+ "mastra:bg-surface3 mastra:text-text6"
1508
+ );
1509
+ const EntityContent = ({ className, ...props }) => {
1510
+ const { expanded } = useEntity();
1511
+ if (!expanded) return null;
1512
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || EntityContentClass, ...props });
1513
+ };
1514
+ const EntityCaret = ({ className, ...props }) => {
1515
+ const { expanded } = useEntity();
1516
+ return /* @__PURE__ */ jsxRuntime.jsx(Icon, { children: /* @__PURE__ */ jsxRuntime.jsx(
1517
+ lucideReact.ChevronDownIcon,
1518
+ {
1519
+ className: tailwindMerge.twMerge(
1520
+ `mastra:text-text3 mastra:transition-transform mastra:duration-200 mastra:ease-in-out`,
1521
+ expanded ? "mastra:rotate-0" : "mastra:-rotate-90",
1522
+ className
1523
+ ),
1524
+ ...props
1525
+ }
1526
+ ) });
1527
+ };
1528
+
1529
+ const ToolApprovalClass = tailwindMerge.twMerge(
1530
+ "mastra:rounded-lg mastra:border mastra:border-border1 mastra:max-w-1/2 mastra:mt-2",
1531
+ "mastra:bg-surface3 mastra:text-text6"
1532
+ );
1533
+ const ToolApproval = ({ className, ...props }) => {
1534
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || ToolApprovalClass, ...props });
1535
+ };
1536
+ const ToolApprovalTitleClass = tailwindMerge.twMerge("mastra:text-text6 mastra:inline-flex mastra:items-center mastra:gap-1");
1537
+ const ToolApprovalTitle = ({ className, ...props }) => {
1538
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || ToolApprovalTitleClass, ...props });
1539
+ };
1540
+ const ToolApprovalHeaderClass = tailwindMerge.twMerge(
1541
+ "mastra:flex mastra:justify-between mastra:items-center mastra:gap-2",
1542
+ "mastra:border-b mastra:border-border1 mastra:px-4 mastra:py-2"
1543
+ );
1544
+ const ToolApprovalHeader = ({ className, ...props }) => {
1545
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || ToolApprovalHeaderClass, ...props });
1546
+ };
1547
+ const ToolApprovalContentClass = tailwindMerge.twMerge("mastra:text-text6 mastra:p-4");
1548
+ const ToolApprovalContent = ({ className, ...props }) => {
1549
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || ToolApprovalContentClass, ...props });
1550
+ };
1551
+ const ToolApprovalActionsClass = tailwindMerge.twMerge("mastra:flex mastra:gap-2 mastra:items-center");
1552
+ const ToolApprovalActions = ({ className, ...props }) => {
1553
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || ToolApprovalActionsClass, ...props });
1554
+ };
1555
+
1556
+ const EntryClass = "mastra:space-y-2";
1557
+ const Entry = ({ className, ...props }) => {
1558
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || EntryClass, ...props });
1559
+ };
1560
+ const EntryTitleClass = "mastra:font-mono mastra:text-sm mastra:text-text3";
1561
+ const EntryTitle = ({ className, as: Root = "h3", ...props }) => {
1562
+ return /* @__PURE__ */ jsxRuntime.jsx(Root, { className: className || EntryTitleClass, ...props });
1563
+ };
1564
+
1565
+ async function highlight(code, lang) {
1566
+ const out = await web.codeToHast(code, {
1567
+ lang,
1568
+ theme: "dracula-soft"
1569
+ });
1570
+ return hastUtilToJsxRuntime.toJsxRuntime(out, {
1571
+ Fragment: react.Fragment,
1572
+ jsx: jsxRuntime.jsx,
1573
+ jsxs: jsxRuntime.jsxs
1574
+ });
1575
+ }
1576
+
1577
+ const Tooltip = ({ children }) => {
1578
+ return /* @__PURE__ */ jsxRuntime.jsx(reactTooltip.TooltipProvider, { children: /* @__PURE__ */ jsxRuntime.jsx(reactTooltip.Root, { children }) });
1579
+ };
1580
+ const TooltipContentClass = "mastra:bg-surface4 mastra:text-text6 mastra mastra:rounded-lg mastra:py-1 mastra:px-2 mastra:text-xs mastra:border mastra:border-border1 mastra-tooltip-enter";
1581
+ const TooltipContent = ({ children, className, ...props }) => {
1582
+ return /* @__PURE__ */ jsxRuntime.jsx(reactTooltip.TooltipPortal, { children: /* @__PURE__ */ jsxRuntime.jsx(reactTooltip.TooltipContent, { className: className || TooltipContentClass, ...props, children }) });
1583
+ };
1584
+ const TooltipTrigger = (props) => {
1585
+ return /* @__PURE__ */ jsxRuntime.jsx(reactTooltip.TooltipTrigger, { ...props, asChild: true });
1586
+ };
1587
+
1588
+ const IconButtonClass = "mastra:text-text3 mastra:hover:text-text6 mastra:active:text-text6 mastra:hover:bg-surface4 mastra:active:bg-surface5 mastra:rounded-md mastra:cursor-pointer";
1589
+ const IconButton = ({ children, tooltip, size = "md", className, ...props }) => {
1590
+ return /* @__PURE__ */ jsxRuntime.jsxs(Tooltip, { children: [
1591
+ /* @__PURE__ */ jsxRuntime.jsx(TooltipTrigger, { children: /* @__PURE__ */ jsxRuntime.jsx(
1592
+ "button",
1593
+ {
1594
+ ...props,
1595
+ className: className || tailwindMerge.twMerge(IconButtonClass, size === "md" && "mastra:p-0.5", size === "lg" && "mastra:p-1"),
1596
+ children: /* @__PURE__ */ jsxRuntime.jsx(Icon, { size, children })
1597
+ }
1598
+ ) }),
1599
+ /* @__PURE__ */ jsxRuntime.jsx(TooltipContent, { children: tooltip })
1600
+ ] });
1601
+ };
1602
+
1603
+ const CodeBlockClass = "mastra:rounded-lg mastra:[&>pre]:p-4 mastra:overflow-hidden mastra:[&>pre]:!bg-surface4 mastra:[&>pre>code]:leading-5 mastra:relative";
1604
+ const CodeBlock = ({ code, language, className, cta }) => {
1605
+ const [nodes, setNodes] = react.useState(null);
1606
+ react.useLayoutEffect(() => {
1607
+ void highlight(code, language).then(setNodes);
1608
+ }, [language]);
1609
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: className || CodeBlockClass, children: [
1610
+ nodes ?? null,
1611
+ cta
1612
+ ] });
1613
+ };
1614
+ const CodeCopyButton = ({ code }) => {
1615
+ const [isCopied, setIsCopied] = react.useState(false);
1616
+ const handleCopy = () => {
1617
+ navigator.clipboard.writeText(code);
1618
+ setIsCopied(true);
1619
+ setTimeout(() => setIsCopied(false), 2e3);
1620
+ };
1621
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "mastra:absolute mastra:top-2 mastra:right-2", children: /* @__PURE__ */ jsxRuntime.jsx(IconButton, { tooltip: "Copy", onClick: handleCopy, children: isCopied ? /* @__PURE__ */ jsxRuntime.jsx(lucideReact.CheckIcon, {}) : /* @__PURE__ */ jsxRuntime.jsx(lucideReact.CopyIcon, {}) }) });
1622
+ };
1623
+
1624
+ const AgentIcon = ({ className, ...props }) => /* @__PURE__ */ jsxRuntime.jsxs(
1625
+ "svg",
1626
+ {
1627
+ width: "17",
1628
+ height: "16",
1629
+ viewBox: "0 0 17 16",
1630
+ fill: "none",
1631
+ xmlns: "http://www.w3.org/2000/svg",
1632
+ ...props,
1633
+ className: tailwindMerge.twMerge("mastra-icon", className),
1634
+ children: [
1635
+ /* @__PURE__ */ jsxRuntime.jsx(
1636
+ "path",
1637
+ {
1638
+ fillRule: "evenodd",
1639
+ clipRule: "evenodd",
1640
+ d: "M8.5 15C10.3565 15 12.137 14.2625 13.4497 12.9497C14.7625 11.637 15.5 9.85652 15.5 8C15.5 6.14348 14.7625 4.36301 13.4497 3.05025C12.137 1.7375 10.3565 1 8.5 1C6.64348 1 4.86301 1.7375 3.55025 3.05025C2.2375 4.36301 1.5 6.14348 1.5 8C1.5 9.85652 2.2375 11.637 3.55025 12.9497C4.86301 14.2625 6.64348 15 8.5 15ZM5.621 10.879L4.611 11.889C3.84179 11.1198 3.31794 10.1398 3.1057 9.07291C2.89346 8.00601 3.00236 6.90013 3.41864 5.89512C3.83491 4.89012 4.53986 4.03112 5.44434 3.42676C6.34881 2.8224 7.41219 2.49983 8.5 2.49983C9.58781 2.49983 10.6512 2.8224 11.5557 3.42676C12.4601 4.03112 13.1651 4.89012 13.5814 5.89512C13.9976 6.90013 14.1065 8.00601 13.8943 9.07291C13.6821 10.1398 13.1582 11.1198 12.389 11.889L11.379 10.879C11.1004 10.6003 10.7696 10.3792 10.4055 10.2284C10.0414 10.0776 9.6511 9.99995 9.257 10H7.743C7.3489 9.99995 6.95865 10.0776 6.59455 10.2284C6.23045 10.3792 5.89963 10.6003 5.621 10.879Z",
1641
+ fill: "currentColor"
1642
+ }
1643
+ ),
1644
+ /* @__PURE__ */ jsxRuntime.jsx(
1645
+ "path",
1646
+ {
1647
+ d: "M8.5 4C7.96957 4 7.46086 4.21071 7.08579 4.58579C6.71071 4.96086 6.5 5.46957 6.5 6V6.5C6.5 7.03043 6.71071 7.53914 7.08579 7.91421C7.46086 8.28929 7.96957 8.5 8.5 8.5C9.03043 8.5 9.53914 8.28929 9.91421 7.91421C10.2893 7.53914 10.5 7.03043 10.5 6.5V6C10.5 5.46957 10.2893 4.96086 9.91421 4.58579C9.53914 4.21071 9.03043 4 8.5 4Z",
1648
+ fill: "currentColor"
1649
+ }
1650
+ )
1651
+ ]
1652
+ }
1653
+ );
1654
+
1655
+ const ToolsIcon = ({ className, ...props }) => /* @__PURE__ */ jsxRuntime.jsx(
1656
+ "svg",
1657
+ {
1658
+ width: "17",
1659
+ height: "16",
1660
+ viewBox: "0 0 17 16",
1661
+ fill: "none",
1662
+ xmlns: "http://www.w3.org/2000/svg",
1663
+ ...props,
1664
+ className: tailwindMerge.twMerge("mastra-icon", className),
1665
+ children: /* @__PURE__ */ jsxRuntime.jsx(
1666
+ "path",
1667
+ {
1668
+ fillRule: "evenodd",
1669
+ clipRule: "evenodd",
1670
+ d: "M7.5605 1.42351C8.0791 0.904904 8.92215 0.906157 9.4395 1.42351L10.6922 2.67617C11.2108 3.19477 11.2095 4.03782 10.6922 4.55517L9.4395 5.80783C8.9209 6.32643 8.07785 6.32518 7.5605 5.80783L6.30784 4.55517C5.78923 4.03656 5.79049 3.19352 6.30784 2.67617L7.5605 1.42351ZM3.17618 5.80783C3.69478 5.28923 4.53782 5.29048 5.05517 5.80783L6.30784 7.0605C6.82644 7.5791 6.82519 8.42214 6.30784 8.93949L5.05517 10.1922C4.53657 10.7108 3.69353 10.7095 3.17618 10.1922L1.92351 8.93949C1.40491 8.42089 1.40616 7.57785 1.92351 7.0605L3.17618 5.80783ZM11.9448 5.80783C12.4634 5.28923 13.3065 5.29048 13.8238 5.80783L15.0765 7.0605C15.5951 7.5791 15.5938 8.42214 15.0765 8.93949L13.8238 10.1922C13.3052 10.7108 12.4622 10.7095 11.9448 10.1922L10.6922 8.93949C10.1736 8.42089 10.1748 7.57785 10.6922 7.0605L11.9448 5.80783ZM7.5605 10.1922C8.0791 9.67355 8.92215 9.67481 9.4395 10.1922L10.6922 11.4448C11.2108 11.9634 11.2095 12.8065 10.6922 13.3238L9.4395 14.5765C8.9209 15.0951 8.07785 15.0938 7.5605 14.5765L6.30784 13.3238C5.78923 12.8052 5.79049 11.9622 6.30784 11.4448L7.5605 10.1922Z",
1671
+ fill: "currentColor"
1672
+ }
1673
+ )
1674
+ }
1675
+ );
1676
+
1677
+ const WorkflowIcon = ({ className, ...props }) => /* @__PURE__ */ jsxRuntime.jsx(
1678
+ "svg",
1679
+ {
1680
+ width: "17",
1681
+ height: "16",
1682
+ viewBox: "0 0 17 16",
1683
+ fill: "none",
1684
+ xmlns: "http://www.w3.org/2000/svg",
1685
+ ...props,
1686
+ className: tailwindMerge.twMerge("mastra-icon", className),
1687
+ children: /* @__PURE__ */ jsxRuntime.jsx(
1688
+ "path",
1689
+ {
1690
+ fillRule: "evenodd",
1691
+ clipRule: "evenodd",
1692
+ d: "M6.24388 2.4018C6.24388 2.0394 6.53767 1.74561 6.90008 1.74561H10.0991C10.4614 1.74561 10.7553 2.0394 10.7553 2.4018V4.57546C10.7553 4.93787 10.4614 5.23166 10.0991 5.23166H9.31982V7.35469L10.0033 9.22664C9.90442 9.20146 9.80035 9.1761 9.6915 9.14986L9.62652 9.13422C9.30473 9.05687 8.92256 8.96501 8.61993 8.84491C8.5819 8.82981 8.54147 8.81292 8.49957 8.79391C8.45767 8.81292 8.41724 8.82981 8.3792 8.84491C8.07657 8.96501 7.6944 9.05687 7.37261 9.13422L7.30763 9.14986C7.19879 9.1761 7.09471 9.20146 6.99577 9.22664L7.67932 7.35469V5.23166H6.90008C6.53767 5.23166 6.24388 4.93787 6.24388 4.57546V2.4018ZM6.99577 9.22664C6.99577 9.22664 6.99578 9.22664 6.99577 9.22664L6.43283 10.7683H6.81806C7.18047 10.7683 7.47426 11.0622 7.47426 11.4245V13.5982C7.47426 13.9606 7.18047 14.2544 6.81806 14.2544H3.61909C3.25668 14.2544 2.96289 13.9606 2.96289 13.5982V11.4245C2.96289 11.0622 3.25668 10.7683 3.61909 10.7683H4.26617C4.2921 10.4663 4.32783 10.1494 4.37744 9.85171C4.43762 9.49063 4.52982 9.08135 4.68998 8.76102C4.93975 8.2615 5.44743 8.01751 5.7771 7.88788C6.14684 7.74249 6.57537 7.63889 6.92317 7.55505C7.24707 7.47696 7.49576 7.41679 7.67932 7.35469L6.99577 9.22664ZM6.43283 10.7683L6.99577 9.22664C6.75846 9.28705 6.55067 9.34646 6.37745 9.41458C6.22784 9.47341 6.1623 9.51712 6.14023 9.53254C6.09752 9.63631 6.04409 9.83055 5.99562 10.1214C5.96201 10.3231 5.93498 10.5439 5.91341 10.7683H6.43283ZM10.0033 9.22664L9.31982 7.35469C9.50338 7.41679 9.75206 7.47696 10.076 7.55505C10.4238 7.63889 10.8523 7.74249 11.2221 7.88788C11.5517 8.01751 12.0594 8.2615 12.3091 8.76102C12.4693 9.08135 12.5615 9.49063 12.6217 9.85171C12.6713 10.1494 12.707 10.4663 12.733 10.7683H13.38C13.7424 10.7683 14.0362 11.0622 14.0362 11.4245V13.5982C14.0362 13.9606 13.7424 14.2544 13.38 14.2544H10.1811C9.81867 14.2544 9.52488 13.9606 9.52488 13.5982V11.4245C9.52488 11.0622 9.81867 10.7683 10.1811 10.7683H10.5663L10.0033 9.22664ZM10.0033 9.22664L10.5663 10.7683H11.0857C11.0642 10.5439 11.0372 10.3231 11.0035 10.1214C10.9551 9.83055 10.9016 9.63631 10.8589 9.53254C10.8369 9.51712 10.7713 9.47341 10.6217 9.41458C10.4485 9.34646 10.2407 9.28705 10.0033 9.22664Z",
1693
+ fill: "currentColor"
1694
+ }
1695
+ )
1696
+ }
1697
+ );
1698
+
1699
+ const MessageClass = "mastra:flex mastra:flex-col mastra:w-full mastra:py-4 mastra:gap-2 mastra:group";
1700
+ const Message = ({ position, className, children, ...props }) => {
1701
+ return /* @__PURE__ */ jsxRuntime.jsx(
1702
+ "div",
1703
+ {
1704
+ className: className || tailwindMerge.twMerge(
1705
+ MessageClass,
1706
+ position === "left" ? "" : "mastra:items-end mastra:[&_.mastra-message-content]:bg-surface4 mastra:[&_.mastra-message-content]:px-4"
1707
+ ),
1708
+ ...props,
1709
+ children
1710
+ }
1711
+ );
1712
+ };
1713
+ const MessageContentClass = "mastra:max-w-4/5 mastra:py-2 mastra:text-text6 mastra:rounded-lg mastra-message-content mastra:text-md";
1714
+ const MessageContent = ({ children, className, isStreaming, ...props }) => {
1715
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: className || MessageContentClass, ...props, children: [
1716
+ children,
1717
+ isStreaming && /* @__PURE__ */ jsxRuntime.jsx(MessageStreaming, {})
1718
+ ] });
1719
+ };
1720
+ const MessageActionsClass = "mastra:gap-2 mastra:flex mastra:opacity-0 mastra:group-hover:opacity-100 mastra:group-focus-within:opacity-100 mastra:items-center";
1721
+ const MessageActions = ({ children, className, ...props }) => {
1722
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || MessageActionsClass, ...props, children });
1723
+ };
1724
+ const MessageUsagesClass = "mastra:flex mastra:gap-2 mastra:items-center";
1725
+ const MessageUsages = ({ children, className, ...props }) => {
1726
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || MessageUsagesClass, ...props, children });
1727
+ };
1728
+ const MessageUsageClass = "mastra:flex mastra:gap-2 mastra:items-center mastra:font-mono mastra:text-xs mastra:bg-surface3 mastra:rounded-lg mastra:px-2 mastra:py-1";
1729
+ const MessageUsage = ({ children, className, ...props }) => {
1730
+ return /* @__PURE__ */ jsxRuntime.jsx("dl", { className: className || MessageUsageClass, ...props, children });
1731
+ };
1732
+ const MessageUsageEntryClass = "mastra:text-text3 mastra:text-xs mastra:flex mastra:gap-1 mastra:items-center";
1733
+ const MessageUsageEntry = ({ children, className, ...props }) => {
1734
+ return /* @__PURE__ */ jsxRuntime.jsx("dt", { className: className || MessageUsageEntryClass, ...props, children });
1735
+ };
1736
+ const MessageUsageValueClass = "mastra:text-text6 mastra:text-xs";
1737
+ const MessageUsageValue = ({ children, className, ...props }) => {
1738
+ return /* @__PURE__ */ jsxRuntime.jsx("dd", { className: className || MessageUsageValueClass, ...props, children });
1739
+ };
1740
+ const MessageListClass = "mastra:overflow-y-auto mastra:h-full mastra-list";
1741
+ const MessageList = ({ children, className, ...props }) => {
1742
+ const listRef = react.useRef(null);
1743
+ react.useEffect(() => {
1744
+ const scrollToBottom = () => {
1745
+ if (!listRef.current) return;
1746
+ listRef.current.scrollTo({ top: listRef.current.scrollHeight, behavior: "smooth" });
1747
+ };
1748
+ requestAnimationFrame(scrollToBottom);
1749
+ });
1750
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { className: className || MessageListClass, ...props, ref: listRef, children });
1751
+ };
1752
+ const MessageStreamingClass = "mastra:inline-block mastra:w-[2px] mastra:h-[1em] mastra:bg-text5 mastra:ml-0.5 mastra:align-text-bottom mastra:animate-pulse";
1753
+ const MessageStreaming = ({ className, ...props }) => {
1754
+ return /* @__PURE__ */ jsxRuntime.jsx("span", { className: className || MessageStreamingClass, ...props });
1755
+ };
1756
+
1757
+ exports.AgentIcon = AgentIcon;
1758
+ exports.CodeBlock = CodeBlock;
1759
+ exports.CodeBlockClass = CodeBlockClass;
1760
+ exports.CodeCopyButton = CodeCopyButton;
1761
+ exports.Entity = Entity;
1762
+ exports.EntityCaret = EntityCaret;
1763
+ exports.EntityContent = EntityContent;
1764
+ exports.EntityContentClass = EntityContentClass;
1765
+ exports.EntityTrigger = EntityTrigger;
1766
+ exports.EntityTriggerClass = EntityTriggerClass;
1767
+ exports.EntityTriggerVariantClasses = EntityTriggerVariantClasses;
1768
+ exports.Entry = Entry;
1769
+ exports.EntryClass = EntryClass;
1770
+ exports.EntryTitle = EntryTitle;
1771
+ exports.EntryTitleClass = EntryTitleClass;
1772
+ exports.Icon = Icon;
1773
+ exports.IconButton = IconButton;
1774
+ exports.IconButtonClass = IconButtonClass;
1775
+ exports.IconSizes = IconSizes;
1776
+ exports.MastraReactProvider = MastraReactProvider;
1777
+ exports.Message = Message;
1778
+ exports.MessageActions = MessageActions;
1779
+ exports.MessageActionsClass = MessageActionsClass;
1780
+ exports.MessageClass = MessageClass;
1781
+ exports.MessageContent = MessageContent;
1782
+ exports.MessageContentClass = MessageContentClass;
1783
+ exports.MessageList = MessageList;
1784
+ exports.MessageListClass = MessageListClass;
1785
+ exports.MessageStreaming = MessageStreaming;
1786
+ exports.MessageStreamingClass = MessageStreamingClass;
1787
+ exports.MessageUsage = MessageUsage;
1788
+ exports.MessageUsageClass = MessageUsageClass;
1789
+ exports.MessageUsageEntry = MessageUsageEntry;
1790
+ exports.MessageUsageEntryClass = MessageUsageEntryClass;
1791
+ exports.MessageUsageValue = MessageUsageValue;
1792
+ exports.MessageUsageValueClass = MessageUsageValueClass;
1793
+ exports.MessageUsages = MessageUsages;
1794
+ exports.MessageUsagesClass = MessageUsagesClass;
1795
+ exports.ToolApproval = ToolApproval;
1796
+ exports.ToolApprovalActions = ToolApprovalActions;
1797
+ exports.ToolApprovalActionsClass = ToolApprovalActionsClass;
1798
+ exports.ToolApprovalClass = ToolApprovalClass;
1799
+ exports.ToolApprovalContent = ToolApprovalContent;
1800
+ exports.ToolApprovalContentClass = ToolApprovalContentClass;
1801
+ exports.ToolApprovalHeader = ToolApprovalHeader;
1802
+ exports.ToolApprovalHeaderClass = ToolApprovalHeaderClass;
1803
+ exports.ToolApprovalTitle = ToolApprovalTitle;
1804
+ exports.ToolApprovalTitleClass = ToolApprovalTitleClass;
1805
+ exports.ToolsIcon = ToolsIcon;
1806
+ exports.Tooltip = Tooltip;
1807
+ exports.TooltipContent = TooltipContent;
1808
+ exports.TooltipContentClass = TooltipContentClass;
1809
+ exports.TooltipTrigger = TooltipTrigger;
1810
+ exports.WorkflowIcon = WorkflowIcon;
1811
+ exports.mapWorkflowStreamChunkToWatchResult = mapWorkflowStreamChunkToWatchResult;
1812
+ exports.resolveToChildMessages = resolveToChildMessages;
1813
+ exports.toAssistantUIMessage = toAssistantUIMessage;
1814
+ exports.toUIMessage = toUIMessage;
1815
+ exports.useChat = useChat;
1816
+ exports.useEntity = useEntity;
1817
+ exports.useMastraClient = useMastraClient;
1818
+ //# sourceMappingURL=index.cjs.map