@mastra/react 0.1.0-beta.9 → 1.0.0-beta.25

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