@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.
- package/CHANGELOG.md +162 -2
- package/dist/index.cjs +1818 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.es.js → index.js} +782 -128
- package/dist/index.js.map +1 -0
- package/dist/react.css +719 -0
- package/dist/src/agent/hooks.d.ts +23 -6
- package/dist/src/agent/types.d.ts +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/lib/ai-sdk/index.d.ts +1 -0
- package/dist/src/lib/ai-sdk/memory/resolveInitialMessages.d.ts +10 -0
- package/dist/src/lib/ai-sdk/memory/resolveInitialMessages.test.d.ts +1 -0
- package/dist/src/lib/ai-sdk/transformers/AISdkNetworkTransformer.d.ts +1 -0
- package/dist/src/lib/ai-sdk/transformers/AISdkNetworkTransformer.test.d.ts +1 -0
- package/dist/src/lib/ai-sdk/types.d.ts +7 -0
- package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.d.ts +10 -0
- package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.test.d.ts +1 -0
- package/dist/src/lib/ai-sdk/utils/toUIMessage.test.d.ts +1 -0
- package/dist/src/ui/Code/Code.d.ts +13 -0
- package/dist/src/ui/Code/highlight.d.ts +3 -0
- package/dist/src/ui/Code/index.d.ts +1 -0
- package/dist/src/ui/Entity/Entity.d.ts +13 -0
- package/dist/src/ui/Entity/Entity.stories.d.ts +22 -0
- package/dist/src/ui/Entity/Entry.d.ts +9 -0
- package/dist/src/ui/Entity/ToolApproval.d.ts +10 -0
- package/dist/src/ui/Entity/context.d.ts +10 -0
- package/dist/src/ui/Entity/index.d.ts +4 -0
- package/dist/src/ui/Entity/types.d.ts +1 -0
- package/dist/src/ui/Icon/Icon.d.ts +11 -0
- package/dist/src/ui/Icon/index.d.ts +1 -0
- package/dist/src/ui/IconButton/IconButton.d.ts +8 -0
- package/dist/src/ui/IconButton/IconButton.stories.d.ts +12 -0
- package/dist/src/ui/IconButton/index.d.ts +1 -0
- package/dist/src/ui/Icons/AgentIcon.d.ts +2 -0
- package/dist/src/ui/Icons/ToolsIcon.d.ts +2 -0
- package/dist/src/ui/Icons/WorkflowIcon.d.ts +2 -0
- package/dist/src/ui/Icons/index.d.ts +3 -0
- package/dist/src/ui/Message/Message.d.ts +25 -0
- package/dist/src/ui/Message/Message.stories.d.ts +13 -0
- package/dist/src/ui/Message/index.d.ts +1 -0
- package/dist/src/ui/Tooltip/Tooltip.d.ts +8 -0
- package/dist/src/ui/Tooltip/Tooltip.stories.d.ts +12 -0
- package/dist/src/ui/Tooltip/index.d.ts +1 -0
- package/dist/src/ui/index.d.ts +7 -0
- package/package.json +46 -11
- package/dist/index.cjs.js +0 -1109
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.es.js.map +0 -1
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import { jsx } from 'react/jsx-runtime';
|
|
2
|
-
import { createContext, useContext, useState } from 'react';
|
|
1
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
2
|
+
import { createContext, useContext, useRef, useState, Fragment, useLayoutEffect, useEffect } from 'react';
|
|
3
3
|
import { MastraClient } from '@mastra/client-js';
|
|
4
|
-
import {
|
|
4
|
+
import { ChevronDownIcon, CheckIcon, CopyIcon } from 'lucide-react';
|
|
5
|
+
import { twMerge } from 'tailwind-merge';
|
|
6
|
+
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
7
|
+
import { codeToHast } from 'shiki/bundle/web';
|
|
8
|
+
import { TooltipProvider, Root, TooltipPortal, TooltipContent as TooltipContent$1, TooltipTrigger as TooltipTrigger$1 } from '@radix-ui/react-tooltip';
|
|
5
9
|
|
|
6
10
|
const MastraClientContext = createContext({});
|
|
7
11
|
const MastraClientProvider = ({ children, baseUrl, headers }) => {
|
|
@@ -245,6 +249,7 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
245
249
|
}
|
|
246
250
|
];
|
|
247
251
|
}
|
|
252
|
+
case "tool-error":
|
|
248
253
|
case "tool-result": {
|
|
249
254
|
const lastMessage = result[result.length - 1];
|
|
250
255
|
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
@@ -255,25 +260,35 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
255
260
|
if (toolPartIndex !== -1) {
|
|
256
261
|
const toolPart = parts[toolPartIndex];
|
|
257
262
|
if (toolPart.type === "dynamic-tool") {
|
|
258
|
-
if (chunk.payload.isError) {
|
|
263
|
+
if (chunk.type === "tool-result" && chunk.payload.isError || chunk.type === "tool-error") {
|
|
264
|
+
const error = chunk.type === "tool-error" ? chunk.payload.error : chunk.payload.result;
|
|
259
265
|
parts[toolPartIndex] = {
|
|
260
266
|
type: "dynamic-tool",
|
|
261
267
|
toolName: toolPart.toolName,
|
|
262
268
|
toolCallId: toolPart.toolCallId,
|
|
263
269
|
state: "output-error",
|
|
264
270
|
input: toolPart.input,
|
|
265
|
-
errorText: String(
|
|
271
|
+
errorText: String(error),
|
|
266
272
|
callProviderMetadata: chunk.payload.providerMetadata
|
|
267
273
|
};
|
|
268
274
|
} else {
|
|
269
275
|
const isWorkflow = Boolean(chunk.payload.result?.result?.steps);
|
|
276
|
+
const isAgent = chunk?.from === "AGENT";
|
|
277
|
+
let output;
|
|
278
|
+
if (isWorkflow) {
|
|
279
|
+
output = chunk.payload.result?.result;
|
|
280
|
+
} else if (isAgent) {
|
|
281
|
+
output = parts[toolPartIndex].output ?? chunk.payload.result;
|
|
282
|
+
} else {
|
|
283
|
+
output = chunk.payload.result;
|
|
284
|
+
}
|
|
270
285
|
parts[toolPartIndex] = {
|
|
271
286
|
type: "dynamic-tool",
|
|
272
287
|
toolName: toolPart.toolName,
|
|
273
288
|
toolCallId: toolPart.toolCallId,
|
|
274
289
|
state: "output-available",
|
|
275
290
|
input: toolPart.input,
|
|
276
|
-
output
|
|
291
|
+
output,
|
|
277
292
|
callProviderMetadata: chunk.payload.providerMetadata
|
|
278
293
|
};
|
|
279
294
|
}
|
|
@@ -307,6 +322,8 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
307
322
|
...toolPart,
|
|
308
323
|
output: updatedWorkflowState
|
|
309
324
|
};
|
|
325
|
+
} else if (chunk.payload.output?.from === "AGENT" || chunk.payload.output?.from === "USER" && chunk.payload.output?.payload?.output?.type?.startsWith("workflow-")) {
|
|
326
|
+
return toUIMessageFromAgent(chunk.payload.output, conversation);
|
|
310
327
|
} else {
|
|
311
328
|
const currentOutput = toolPart.output || [];
|
|
312
329
|
const existingOutput = Array.isArray(currentOutput) ? currentOutput : [];
|
|
@@ -380,15 +397,37 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
380
397
|
}
|
|
381
398
|
];
|
|
382
399
|
}
|
|
400
|
+
case "tool-call-approval": {
|
|
401
|
+
const lastMessage = result[result.length - 1];
|
|
402
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
403
|
+
const lastRequireApprovalMetadata = lastMessage.metadata?.mode === "stream" ? lastMessage.metadata?.requireApprovalMetadata : {};
|
|
404
|
+
return [
|
|
405
|
+
...result.slice(0, -1),
|
|
406
|
+
{
|
|
407
|
+
...lastMessage,
|
|
408
|
+
metadata: {
|
|
409
|
+
...lastMessage.metadata,
|
|
410
|
+
mode: "stream",
|
|
411
|
+
requireApprovalMetadata: {
|
|
412
|
+
...lastRequireApprovalMetadata,
|
|
413
|
+
[chunk.payload.toolCallId]: {
|
|
414
|
+
toolCallId: chunk.payload.toolCallId,
|
|
415
|
+
toolName: chunk.payload.toolName,
|
|
416
|
+
args: chunk.payload.args
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
];
|
|
422
|
+
}
|
|
383
423
|
case "finish": {
|
|
384
424
|
const lastMessage = result[result.length - 1];
|
|
385
425
|
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
386
426
|
const parts = lastMessage.parts.map((part) => {
|
|
387
|
-
if (part
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
return { ...part, state: "done" };
|
|
427
|
+
if (typeof part === "object" && part !== null && "type" in part && "state" in part && part.state === "streaming") {
|
|
428
|
+
if (part.type === "text" || part.type === "reasoning") {
|
|
429
|
+
return { ...part, state: "done" };
|
|
430
|
+
}
|
|
392
431
|
}
|
|
393
432
|
return part;
|
|
394
433
|
});
|
|
@@ -407,7 +446,7 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
407
446
|
parts: [
|
|
408
447
|
{
|
|
409
448
|
type: "text",
|
|
410
|
-
text: chunk.payload.error
|
|
449
|
+
text: typeof chunk.payload.error === "string" ? chunk.payload.error : JSON.stringify(chunk.payload.error)
|
|
411
450
|
}
|
|
412
451
|
],
|
|
413
452
|
metadata: {
|
|
@@ -422,6 +461,105 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
422
461
|
return result;
|
|
423
462
|
}
|
|
424
463
|
};
|
|
464
|
+
const toUIMessageFromAgent = (chunk, conversation, metadata) => {
|
|
465
|
+
const lastMessage = conversation[conversation.length - 1];
|
|
466
|
+
if (!lastMessage || lastMessage.role !== "assistant") return conversation;
|
|
467
|
+
const parts = [...lastMessage.parts];
|
|
468
|
+
if (chunk.type === "text-delta") {
|
|
469
|
+
const agentChunk = chunk.payload;
|
|
470
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
471
|
+
if (toolPartIndex === -1) return conversation;
|
|
472
|
+
const toolPart = parts[toolPartIndex];
|
|
473
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
474
|
+
const lastChildMessage = childMessages[childMessages.length - 1];
|
|
475
|
+
const textMessage = { type: "text", content: (lastChildMessage?.content || "") + agentChunk.text };
|
|
476
|
+
const nextMessages = lastChildMessage?.type === "text" ? [...childMessages.slice(0, -1), textMessage] : [...childMessages, textMessage];
|
|
477
|
+
parts[toolPartIndex] = {
|
|
478
|
+
...toolPart,
|
|
479
|
+
output: {
|
|
480
|
+
childMessages: nextMessages
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
} else if (chunk.type === "tool-call") {
|
|
484
|
+
const agentChunk = chunk.payload;
|
|
485
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
486
|
+
if (toolPartIndex === -1) return conversation;
|
|
487
|
+
const toolPart = parts[toolPartIndex];
|
|
488
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
489
|
+
parts[toolPartIndex] = {
|
|
490
|
+
...toolPart,
|
|
491
|
+
output: {
|
|
492
|
+
...toolPart?.output,
|
|
493
|
+
childMessages: [
|
|
494
|
+
...childMessages,
|
|
495
|
+
{
|
|
496
|
+
type: "tool",
|
|
497
|
+
toolCallId: agentChunk.toolCallId,
|
|
498
|
+
toolName: agentChunk.toolName,
|
|
499
|
+
args: agentChunk.args
|
|
500
|
+
}
|
|
501
|
+
]
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
} else if (chunk.type === "tool-output") {
|
|
505
|
+
const agentChunk = chunk.payload;
|
|
506
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
507
|
+
if (toolPartIndex === -1) return conversation;
|
|
508
|
+
const toolPart = parts[toolPartIndex];
|
|
509
|
+
if (agentChunk?.output?.type?.startsWith("workflow-")) {
|
|
510
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
511
|
+
const lastToolIndex = childMessages.length - 1;
|
|
512
|
+
const currentMessage = childMessages[lastToolIndex];
|
|
513
|
+
const actualExistingWorkflowState = currentMessage?.toolOutput || {};
|
|
514
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(actualExistingWorkflowState, agentChunk.output);
|
|
515
|
+
if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
|
|
516
|
+
parts[toolPartIndex] = {
|
|
517
|
+
...toolPart,
|
|
518
|
+
output: {
|
|
519
|
+
...toolPart?.output,
|
|
520
|
+
childMessages: [
|
|
521
|
+
...childMessages.slice(0, -1),
|
|
522
|
+
{
|
|
523
|
+
...currentMessage,
|
|
524
|
+
toolOutput: { ...updatedWorkflowState, runId: agentChunk.output.runId }
|
|
525
|
+
}
|
|
526
|
+
]
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
} else if (chunk.type === "tool-result") {
|
|
532
|
+
const agentChunk = chunk.payload;
|
|
533
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
534
|
+
if (toolPartIndex === -1) return conversation;
|
|
535
|
+
const toolPart = parts[toolPartIndex];
|
|
536
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
537
|
+
const lastToolIndex = childMessages.length - 1;
|
|
538
|
+
const isWorkflow = agentChunk?.toolName?.startsWith("workflow-");
|
|
539
|
+
if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
|
|
540
|
+
parts[toolPartIndex] = {
|
|
541
|
+
...toolPart,
|
|
542
|
+
output: {
|
|
543
|
+
...toolPart?.output,
|
|
544
|
+
childMessages: [
|
|
545
|
+
...childMessages.slice(0, -1),
|
|
546
|
+
{
|
|
547
|
+
...childMessages[lastToolIndex],
|
|
548
|
+
toolOutput: isWorkflow ? { ...agentChunk.result?.result, runId: agentChunk.result?.runId } : agentChunk.result
|
|
549
|
+
}
|
|
550
|
+
]
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return [
|
|
556
|
+
...conversation.slice(0, -1),
|
|
557
|
+
{
|
|
558
|
+
...lastMessage,
|
|
559
|
+
parts
|
|
560
|
+
}
|
|
561
|
+
];
|
|
562
|
+
};
|
|
425
563
|
|
|
426
564
|
const toAssistantUIMessage = (message) => {
|
|
427
565
|
const extendedMessage = message;
|
|
@@ -461,13 +599,23 @@ const toAssistantUIMessage = (message) => {
|
|
|
461
599
|
};
|
|
462
600
|
}
|
|
463
601
|
if (part.type === "file") {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
602
|
+
const type = part.mediaType.includes("image/") ? "image" : "file";
|
|
603
|
+
if (type === "file") {
|
|
604
|
+
return {
|
|
605
|
+
type,
|
|
606
|
+
mimeType: part.mediaType,
|
|
607
|
+
data: part.url,
|
|
608
|
+
// Use URL as data source
|
|
609
|
+
metadata: message.metadata
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
if (type === "image") {
|
|
613
|
+
return {
|
|
614
|
+
type,
|
|
615
|
+
image: part.url,
|
|
616
|
+
metadata: message.metadata
|
|
617
|
+
};
|
|
618
|
+
}
|
|
471
619
|
}
|
|
472
620
|
if (part.type === "dynamic-tool") {
|
|
473
621
|
const baseToolCall = {
|
|
@@ -542,9 +690,118 @@ const toAssistantUIMessage = (message) => {
|
|
|
542
690
|
return threadMessage;
|
|
543
691
|
};
|
|
544
692
|
|
|
693
|
+
const resolveInitialMessages = (messages) => {
|
|
694
|
+
return messages.map((message) => {
|
|
695
|
+
const networkPart = message.parts.find(
|
|
696
|
+
(part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
|
|
697
|
+
);
|
|
698
|
+
if (networkPart && networkPart.type === "text") {
|
|
699
|
+
try {
|
|
700
|
+
const json = JSON.parse(networkPart.text);
|
|
701
|
+
if (json.isNetwork === true) {
|
|
702
|
+
const selectionReason = json.selectionReason || "";
|
|
703
|
+
const primitiveType = json.primitiveType || "";
|
|
704
|
+
const primitiveId = json.primitiveId || "";
|
|
705
|
+
const finalResult = json.finalResult;
|
|
706
|
+
const toolCalls = finalResult?.toolCalls || [];
|
|
707
|
+
const childMessages = [];
|
|
708
|
+
for (const toolCall of toolCalls) {
|
|
709
|
+
if (toolCall.type === "tool-call" && toolCall.payload) {
|
|
710
|
+
const toolCallId = toolCall.payload.toolCallId;
|
|
711
|
+
let toolResult;
|
|
712
|
+
for (const message2 of finalResult?.messages || []) {
|
|
713
|
+
for (const part of message2.content || []) {
|
|
714
|
+
if (typeof part === "object" && part.type === "tool-result" && part.toolCallId === toolCallId) {
|
|
715
|
+
toolResult = part;
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
const isWorkflow = Boolean(toolResult?.result?.result?.steps);
|
|
721
|
+
childMessages.push({
|
|
722
|
+
type: "tool",
|
|
723
|
+
toolCallId: toolCall.payload.toolCallId,
|
|
724
|
+
toolName: toolCall.payload.toolName,
|
|
725
|
+
args: toolCall.payload.args,
|
|
726
|
+
toolOutput: isWorkflow ? toolResult?.result?.result : toolResult?.result
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
if (finalResult && finalResult.text) {
|
|
731
|
+
childMessages.push({
|
|
732
|
+
type: "text",
|
|
733
|
+
content: finalResult.text
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
const result = {
|
|
737
|
+
childMessages,
|
|
738
|
+
result: finalResult?.text || ""
|
|
739
|
+
};
|
|
740
|
+
console.log("json", json);
|
|
741
|
+
const nextMessage = {
|
|
742
|
+
role: "assistant",
|
|
743
|
+
parts: [
|
|
744
|
+
{
|
|
745
|
+
type: "dynamic-tool",
|
|
746
|
+
toolCallId: primitiveId,
|
|
747
|
+
toolName: primitiveId,
|
|
748
|
+
state: "output-available",
|
|
749
|
+
input: json.input,
|
|
750
|
+
output: result
|
|
751
|
+
}
|
|
752
|
+
],
|
|
753
|
+
id: message.id,
|
|
754
|
+
metadata: {
|
|
755
|
+
...message.metadata,
|
|
756
|
+
mode: "network",
|
|
757
|
+
selectionReason,
|
|
758
|
+
agentInput: json.input,
|
|
759
|
+
from: primitiveType === "agent" ? "AGENT" : "WORKFLOW"
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
return nextMessage;
|
|
763
|
+
}
|
|
764
|
+
} catch (error) {
|
|
765
|
+
return message;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return message;
|
|
769
|
+
});
|
|
770
|
+
};
|
|
771
|
+
const resolveToChildMessages = (messages) => {
|
|
772
|
+
const assistantMessage = messages.find((message) => message.role === "assistant");
|
|
773
|
+
if (!assistantMessage) return [];
|
|
774
|
+
const parts = assistantMessage.parts;
|
|
775
|
+
let childMessages = [];
|
|
776
|
+
for (const part of parts) {
|
|
777
|
+
const toolPart = part;
|
|
778
|
+
if (part.type.startsWith("tool-")) {
|
|
779
|
+
const toolName = part.type.substring("tool-".length);
|
|
780
|
+
const isWorkflow = toolName.startsWith("workflow-");
|
|
781
|
+
childMessages.push({
|
|
782
|
+
type: "tool",
|
|
783
|
+
toolCallId: toolPart.toolCallId,
|
|
784
|
+
toolName,
|
|
785
|
+
args: toolPart.input,
|
|
786
|
+
toolOutput: isWorkflow ? { ...toolPart.output?.result, runId: toolPart.output?.runId } : toolPart.output
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
if (part.type === "text") {
|
|
790
|
+
childMessages.push({
|
|
791
|
+
type: "text",
|
|
792
|
+
content: toolPart.text
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
return childMessages;
|
|
797
|
+
};
|
|
798
|
+
|
|
545
799
|
class AISdkNetworkTransformer {
|
|
546
800
|
transform({ chunk, conversation, metadata }) {
|
|
547
801
|
const newConversation = [...conversation];
|
|
802
|
+
if (chunk.type === "routing-agent-text-delta") {
|
|
803
|
+
return this.handleRoutingAgentConversation(chunk, newConversation);
|
|
804
|
+
}
|
|
548
805
|
if (chunk.type.startsWith("agent-execution-")) {
|
|
549
806
|
return this.handleAgentConversation(chunk, newConversation, metadata);
|
|
550
807
|
}
|
|
@@ -555,22 +812,80 @@ class AISdkNetworkTransformer {
|
|
|
555
812
|
return this.handleToolConversation(chunk, newConversation, metadata);
|
|
556
813
|
}
|
|
557
814
|
if (chunk.type === "network-execution-event-step-finish") {
|
|
558
|
-
const
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
815
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
816
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
817
|
+
const agentChunk = chunk.payload;
|
|
818
|
+
const parts = [...lastMessage.parts];
|
|
819
|
+
const textPartIndex = parts.findIndex((part) => part.type === "text");
|
|
820
|
+
if (textPartIndex === -1) {
|
|
821
|
+
parts.push({
|
|
822
|
+
type: "text",
|
|
823
|
+
text: agentChunk.result,
|
|
824
|
+
state: "done"
|
|
825
|
+
});
|
|
826
|
+
return [
|
|
827
|
+
...newConversation.slice(0, -1),
|
|
562
828
|
{
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
state: "done"
|
|
829
|
+
...lastMessage,
|
|
830
|
+
parts
|
|
566
831
|
}
|
|
567
|
-
]
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
832
|
+
];
|
|
833
|
+
}
|
|
834
|
+
const textPart = parts[textPartIndex];
|
|
835
|
+
if (textPart.type === "text") {
|
|
836
|
+
parts[textPartIndex] = {
|
|
837
|
+
...textPart,
|
|
838
|
+
state: "done"
|
|
839
|
+
};
|
|
840
|
+
return [
|
|
841
|
+
...newConversation.slice(0, -1),
|
|
842
|
+
{
|
|
843
|
+
...lastMessage,
|
|
844
|
+
parts
|
|
845
|
+
}
|
|
846
|
+
];
|
|
847
|
+
}
|
|
848
|
+
return newConversation;
|
|
571
849
|
}
|
|
572
850
|
return newConversation;
|
|
573
851
|
}
|
|
852
|
+
handleRoutingAgentConversation = (chunk, newConversation) => {
|
|
853
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
854
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
855
|
+
const agentChunk = chunk.payload;
|
|
856
|
+
const parts = [...lastMessage.parts];
|
|
857
|
+
const textPartIndex = parts.findIndex((part) => part.type === "text");
|
|
858
|
+
if (textPartIndex === -1) {
|
|
859
|
+
parts.push({
|
|
860
|
+
type: "text",
|
|
861
|
+
text: agentChunk.text,
|
|
862
|
+
state: "streaming"
|
|
863
|
+
});
|
|
864
|
+
return [
|
|
865
|
+
...newConversation.slice(0, -1),
|
|
866
|
+
{
|
|
867
|
+
...lastMessage,
|
|
868
|
+
parts
|
|
869
|
+
}
|
|
870
|
+
];
|
|
871
|
+
}
|
|
872
|
+
const textPart = parts[textPartIndex];
|
|
873
|
+
if (textPart.type === "text") {
|
|
874
|
+
parts[textPartIndex] = {
|
|
875
|
+
...textPart,
|
|
876
|
+
text: textPart.text + agentChunk.text,
|
|
877
|
+
state: "streaming"
|
|
878
|
+
};
|
|
879
|
+
return [
|
|
880
|
+
...newConversation.slice(0, -1),
|
|
881
|
+
{
|
|
882
|
+
...lastMessage,
|
|
883
|
+
parts
|
|
884
|
+
}
|
|
885
|
+
];
|
|
886
|
+
}
|
|
887
|
+
return newConversation;
|
|
888
|
+
};
|
|
574
889
|
handleAgentConversation = (chunk, newConversation, metadata) => {
|
|
575
890
|
if (chunk.type === "agent-execution-start") {
|
|
576
891
|
const primitiveId = chunk.payload?.args?.primitiveId;
|
|
@@ -853,92 +1168,63 @@ class AISdkNetworkTransformer {
|
|
|
853
1168
|
};
|
|
854
1169
|
}
|
|
855
1170
|
|
|
856
|
-
const
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
if (finalResult && finalResult.text) {
|
|
892
|
-
childMessages.push({
|
|
893
|
-
type: "text",
|
|
894
|
-
content: finalResult.text
|
|
895
|
-
});
|
|
896
|
-
}
|
|
897
|
-
const result = {
|
|
898
|
-
childMessages,
|
|
899
|
-
result: finalResult?.text || ""
|
|
900
|
-
};
|
|
901
|
-
console.log("json", json);
|
|
902
|
-
const nextMessage = {
|
|
903
|
-
role: "assistant",
|
|
904
|
-
parts: [
|
|
905
|
-
{
|
|
906
|
-
type: "dynamic-tool",
|
|
907
|
-
toolCallId: primitiveId,
|
|
908
|
-
toolName: primitiveId,
|
|
909
|
-
state: "output-available",
|
|
910
|
-
input: json.input,
|
|
911
|
-
output: result
|
|
912
|
-
}
|
|
913
|
-
],
|
|
914
|
-
id: message.id,
|
|
915
|
-
metadata: {
|
|
916
|
-
...message.metadata,
|
|
917
|
-
mode: "network",
|
|
918
|
-
selectionReason,
|
|
919
|
-
agentInput: json.input,
|
|
920
|
-
from: primitiveType === "agent" ? "AGENT" : "WORKFLOW"
|
|
921
|
-
}
|
|
922
|
-
};
|
|
923
|
-
return nextMessage;
|
|
924
|
-
}
|
|
925
|
-
} catch (error) {
|
|
926
|
-
return message;
|
|
1171
|
+
const fromCoreUserMessageToUIMessage = (coreUserMessage) => {
|
|
1172
|
+
const id = `user-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
1173
|
+
const parts = typeof coreUserMessage.content === "string" ? [
|
|
1174
|
+
{
|
|
1175
|
+
type: "text",
|
|
1176
|
+
text: coreUserMessage.content
|
|
1177
|
+
}
|
|
1178
|
+
] : coreUserMessage.content.map((part) => {
|
|
1179
|
+
switch (part.type) {
|
|
1180
|
+
case "text": {
|
|
1181
|
+
return {
|
|
1182
|
+
type: "text",
|
|
1183
|
+
text: part.text
|
|
1184
|
+
};
|
|
1185
|
+
}
|
|
1186
|
+
case "image": {
|
|
1187
|
+
const url = typeof part.image === "string" ? part.image : part.image instanceof URL ? part.image.toString() : "";
|
|
1188
|
+
return {
|
|
1189
|
+
type: "file",
|
|
1190
|
+
mediaType: part.mimeType ?? "image/*",
|
|
1191
|
+
url
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
1194
|
+
case "file": {
|
|
1195
|
+
const url = typeof part.data === "string" ? part.data : part.data instanceof URL ? part.data.toString() : "";
|
|
1196
|
+
return {
|
|
1197
|
+
type: "file",
|
|
1198
|
+
mediaType: part.mimeType,
|
|
1199
|
+
url,
|
|
1200
|
+
...part.filename !== void 0 ? { filename: part.filename } : {}
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
default: {
|
|
1204
|
+
const exhaustiveCheck = part;
|
|
1205
|
+
throw new Error(`Unhandled content part type: ${exhaustiveCheck.type}`);
|
|
927
1206
|
}
|
|
928
1207
|
}
|
|
929
|
-
return message;
|
|
930
1208
|
});
|
|
1209
|
+
return {
|
|
1210
|
+
id,
|
|
1211
|
+
role: "user",
|
|
1212
|
+
parts
|
|
1213
|
+
};
|
|
931
1214
|
};
|
|
932
1215
|
|
|
933
1216
|
const useChat = ({ agentId, initializeMessages }) => {
|
|
1217
|
+
const _currentRunId = useRef(void 0);
|
|
1218
|
+
const _onChunk = useRef(void 0);
|
|
934
1219
|
const [messages, setMessages] = useState(
|
|
935
1220
|
() => resolveInitialMessages(initializeMessages?.() || [])
|
|
936
1221
|
);
|
|
1222
|
+
const [toolCallApprovals, setToolCallApprovals] = useState({});
|
|
937
1223
|
const baseClient = useMastraClient();
|
|
938
1224
|
const [isRunning, setIsRunning] = useState(false);
|
|
939
1225
|
const generate = async ({
|
|
940
1226
|
coreUserMessages,
|
|
941
|
-
|
|
1227
|
+
requestContext,
|
|
942
1228
|
threadId,
|
|
943
1229
|
modelSettings,
|
|
944
1230
|
signal,
|
|
@@ -976,7 +1262,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
976
1262
|
topP
|
|
977
1263
|
},
|
|
978
1264
|
instructions,
|
|
979
|
-
|
|
1265
|
+
requestContext,
|
|
980
1266
|
...threadId ? { threadId, resourceId: agentId } : {},
|
|
981
1267
|
providerOptions
|
|
982
1268
|
});
|
|
@@ -992,7 +1278,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
992
1278
|
setMessages((prev) => [...prev, ...mastraUIMessages]);
|
|
993
1279
|
}
|
|
994
1280
|
};
|
|
995
|
-
const stream = async ({ coreUserMessages,
|
|
1281
|
+
const stream = async ({ coreUserMessages, requestContext, threadId, onChunk, modelSettings, signal }) => {
|
|
996
1282
|
const {
|
|
997
1283
|
frequencyPenalty,
|
|
998
1284
|
presencePenalty,
|
|
@@ -1003,7 +1289,8 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1003
1289
|
topP,
|
|
1004
1290
|
instructions,
|
|
1005
1291
|
providerOptions,
|
|
1006
|
-
maxSteps
|
|
1292
|
+
maxSteps,
|
|
1293
|
+
requireToolApproval
|
|
1007
1294
|
} = modelSettings || {};
|
|
1008
1295
|
setIsRunning(true);
|
|
1009
1296
|
const clientWithAbort = new MastraClient({
|
|
@@ -1011,9 +1298,10 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1011
1298
|
abortSignal: signal
|
|
1012
1299
|
});
|
|
1013
1300
|
const agent = clientWithAbort.getAgent(agentId);
|
|
1301
|
+
const runId = agentId;
|
|
1014
1302
|
const response = await agent.stream({
|
|
1015
1303
|
messages: coreUserMessages,
|
|
1016
|
-
runId
|
|
1304
|
+
runId,
|
|
1017
1305
|
maxSteps,
|
|
1018
1306
|
modelSettings: {
|
|
1019
1307
|
frequencyPenalty,
|
|
@@ -1025,19 +1313,16 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1025
1313
|
topP
|
|
1026
1314
|
},
|
|
1027
1315
|
instructions,
|
|
1028
|
-
|
|
1316
|
+
requestContext,
|
|
1029
1317
|
...threadId ? { threadId, resourceId: agentId } : {},
|
|
1030
|
-
providerOptions
|
|
1318
|
+
providerOptions,
|
|
1319
|
+
requireToolApproval
|
|
1031
1320
|
});
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
throw new Error("[Stream] No response body");
|
|
1035
|
-
}
|
|
1321
|
+
_onChunk.current = onChunk;
|
|
1322
|
+
_currentRunId.current = runId;
|
|
1036
1323
|
await response.processDataStream({
|
|
1037
1324
|
onChunk: async (chunk) => {
|
|
1038
|
-
|
|
1039
|
-
setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
|
|
1040
|
-
});
|
|
1325
|
+
setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
|
|
1041
1326
|
onChunk?.(chunk);
|
|
1042
1327
|
}
|
|
1043
1328
|
});
|
|
@@ -1045,7 +1330,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1045
1330
|
};
|
|
1046
1331
|
const network = async ({
|
|
1047
1332
|
coreUserMessages,
|
|
1048
|
-
|
|
1333
|
+
requestContext,
|
|
1049
1334
|
threadId,
|
|
1050
1335
|
onNetworkChunk,
|
|
1051
1336
|
modelSettings,
|
|
@@ -1071,30 +1356,399 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1071
1356
|
topP
|
|
1072
1357
|
},
|
|
1073
1358
|
runId: agentId,
|
|
1074
|
-
|
|
1359
|
+
requestContext,
|
|
1075
1360
|
...threadId ? { thread: threadId, resourceId: agentId } : {}
|
|
1076
1361
|
});
|
|
1077
1362
|
const transformer = new AISdkNetworkTransformer();
|
|
1078
1363
|
await response.processDataStream({
|
|
1079
1364
|
onChunk: async (chunk) => {
|
|
1080
|
-
|
|
1081
|
-
setMessages((prev) => transformer.transform({ chunk, conversation: prev, metadata: { mode: "network" } }));
|
|
1082
|
-
});
|
|
1365
|
+
setMessages((prev) => transformer.transform({ chunk, conversation: prev, metadata: { mode: "network" } }));
|
|
1083
1366
|
onNetworkChunk?.(chunk);
|
|
1084
1367
|
}
|
|
1085
1368
|
});
|
|
1086
1369
|
setIsRunning(false);
|
|
1087
1370
|
};
|
|
1371
|
+
const handleCancelRun = () => {
|
|
1372
|
+
setIsRunning(false);
|
|
1373
|
+
_currentRunId.current = void 0;
|
|
1374
|
+
_onChunk.current = void 0;
|
|
1375
|
+
};
|
|
1376
|
+
const approveToolCall = async (toolCallId) => {
|
|
1377
|
+
const onChunk = _onChunk.current;
|
|
1378
|
+
const currentRunId = _currentRunId.current;
|
|
1379
|
+
if (!currentRunId)
|
|
1380
|
+
return console.info("[approveToolCall] approveToolCall can only be called after a stream has started");
|
|
1381
|
+
setIsRunning(true);
|
|
1382
|
+
setToolCallApprovals((prev) => ({ ...prev, [toolCallId]: { status: "approved" } }));
|
|
1383
|
+
const agent = baseClient.getAgent(agentId);
|
|
1384
|
+
const response = await agent.approveToolCall({ runId: currentRunId, toolCallId });
|
|
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 declineToolCall = async (toolCallId) => {
|
|
1394
|
+
const onChunk = _onChunk.current;
|
|
1395
|
+
const currentRunId = _currentRunId.current;
|
|
1396
|
+
if (!currentRunId)
|
|
1397
|
+
return console.info("[declineToolCall] declineToolCall can only be called after a stream has started");
|
|
1398
|
+
setIsRunning(true);
|
|
1399
|
+
setToolCallApprovals((prev) => ({ ...prev, [toolCallId]: { status: "declined" } }));
|
|
1400
|
+
const agent = baseClient.getAgent(agentId);
|
|
1401
|
+
const response = await agent.declineToolCall({ runId: currentRunId, toolCallId });
|
|
1402
|
+
await response.processDataStream({
|
|
1403
|
+
onChunk: async (chunk) => {
|
|
1404
|
+
setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
|
|
1405
|
+
onChunk?.(chunk);
|
|
1406
|
+
}
|
|
1407
|
+
});
|
|
1408
|
+
setIsRunning(false);
|
|
1409
|
+
};
|
|
1410
|
+
const sendMessage = async ({ mode = "stream", ...args }) => {
|
|
1411
|
+
const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
|
|
1412
|
+
const coreUserMessages = [nextMessage];
|
|
1413
|
+
if (args.coreUserMessages) {
|
|
1414
|
+
coreUserMessages.push(...args.coreUserMessages);
|
|
1415
|
+
}
|
|
1416
|
+
const uiMessages = coreUserMessages.map(fromCoreUserMessageToUIMessage);
|
|
1417
|
+
setMessages((s) => [...s, ...uiMessages]);
|
|
1418
|
+
if (mode === "generate") {
|
|
1419
|
+
await generate({ ...args, coreUserMessages });
|
|
1420
|
+
} else if (mode === "stream") {
|
|
1421
|
+
await stream({ ...args, coreUserMessages });
|
|
1422
|
+
} else if (mode === "network") {
|
|
1423
|
+
await network({ ...args, coreUserMessages });
|
|
1424
|
+
}
|
|
1425
|
+
};
|
|
1088
1426
|
return {
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
generate,
|
|
1427
|
+
setMessages,
|
|
1428
|
+
sendMessage,
|
|
1092
1429
|
isRunning,
|
|
1093
1430
|
messages,
|
|
1094
|
-
|
|
1095
|
-
|
|
1431
|
+
approveToolCall,
|
|
1432
|
+
declineToolCall,
|
|
1433
|
+
cancelRun: handleCancelRun,
|
|
1434
|
+
toolCallApprovals
|
|
1435
|
+
};
|
|
1436
|
+
};
|
|
1437
|
+
|
|
1438
|
+
const EntityContext = createContext({
|
|
1439
|
+
expanded: false,
|
|
1440
|
+
setExpanded: () => {
|
|
1441
|
+
},
|
|
1442
|
+
variant: "initial",
|
|
1443
|
+
disabled: false
|
|
1444
|
+
});
|
|
1445
|
+
const EntityProvider = EntityContext.Provider;
|
|
1446
|
+
const useEntity = () => useContext(EntityContext);
|
|
1447
|
+
|
|
1448
|
+
const IconSizes = {
|
|
1449
|
+
sm: "mastra:[&>svg]:size-3",
|
|
1450
|
+
md: "mastra:[&>svg]:size-4",
|
|
1451
|
+
lg: "mastra:[&>svg]:size-5"
|
|
1452
|
+
};
|
|
1453
|
+
const Icon = ({ children, className, size = "md", ...props }) => {
|
|
1454
|
+
return /* @__PURE__ */ jsx("div", { className: className || IconSizes[size], ...props, children });
|
|
1455
|
+
};
|
|
1456
|
+
|
|
1457
|
+
const Entity = ({
|
|
1458
|
+
className,
|
|
1459
|
+
variant = "initial",
|
|
1460
|
+
initialExpanded = false,
|
|
1461
|
+
disabled = false,
|
|
1462
|
+
...props
|
|
1463
|
+
}) => {
|
|
1464
|
+
const [expanded, setExpanded] = useState(initialExpanded);
|
|
1465
|
+
return /* @__PURE__ */ jsx(EntityProvider, { value: { expanded, setExpanded, variant, disabled }, children: /* @__PURE__ */ jsx("div", { className, ...props }) });
|
|
1466
|
+
};
|
|
1467
|
+
const EntityTriggerClass = twMerge(
|
|
1468
|
+
"mastra:aria-disabled:cursor-not-allowed mastra:aria-disabled:bg-surface5 mastra:aria-disabled:text-text3",
|
|
1469
|
+
"mastra:aria-expanded:rounded-b-none mastra:aria-expanded:border-b-0",
|
|
1470
|
+
"mastra:bg-surface3 mastra:text-text6 mastra:hover:bg-surface4 mastra:active:bg-surface5",
|
|
1471
|
+
"mastra:rounded-lg mastra:py-2 mastra:px-4 mastra:border mastra:border-border1",
|
|
1472
|
+
"mastra:cursor-pointer mastra:inline-flex mastra:items-center mastra:gap-1 mastra:font-mono"
|
|
1473
|
+
);
|
|
1474
|
+
const EntityTriggerVariantClasses = {
|
|
1475
|
+
agent: "mastra:[&_svg.mastra-icon]:text-accent1",
|
|
1476
|
+
workflow: "mastra:[&_svg.mastra-icon]:text-accent3",
|
|
1477
|
+
tool: "mastra:[&_svg.mastra-icon]:text-accent6",
|
|
1478
|
+
memory: "mastra:[&_svg.mastra-icon]:text-accent2",
|
|
1479
|
+
initial: "mastra:[&_svg.mastra-icon]:text-text3"
|
|
1480
|
+
};
|
|
1481
|
+
const EntityTrigger = ({ className, children, ...props }) => {
|
|
1482
|
+
const { expanded, setExpanded, variant, disabled } = useEntity();
|
|
1483
|
+
const handleClick = (e) => {
|
|
1484
|
+
if (disabled) return;
|
|
1485
|
+
setExpanded(!expanded);
|
|
1486
|
+
props?.onClick?.(e);
|
|
1096
1487
|
};
|
|
1488
|
+
return /* @__PURE__ */ jsx(
|
|
1489
|
+
"button",
|
|
1490
|
+
{
|
|
1491
|
+
className: className || twMerge(EntityTriggerClass, !disabled && EntityTriggerVariantClasses[variant]),
|
|
1492
|
+
...props,
|
|
1493
|
+
onClick: handleClick,
|
|
1494
|
+
"aria-expanded": expanded,
|
|
1495
|
+
"aria-disabled": disabled,
|
|
1496
|
+
children
|
|
1497
|
+
}
|
|
1498
|
+
);
|
|
1499
|
+
};
|
|
1500
|
+
const EntityContentClass = twMerge(
|
|
1501
|
+
"mastra:space-y-4",
|
|
1502
|
+
"mastra:rounded-lg mastra:rounded-tl-none mastra:p-4 mastra:border mastra:border-border1 mastra:-mt-[0.5px]",
|
|
1503
|
+
"mastra:bg-surface3 mastra:text-text6"
|
|
1504
|
+
);
|
|
1505
|
+
const EntityContent = ({ className, ...props }) => {
|
|
1506
|
+
const { expanded } = useEntity();
|
|
1507
|
+
if (!expanded) return null;
|
|
1508
|
+
return /* @__PURE__ */ jsx("div", { className: className || EntityContentClass, ...props });
|
|
1509
|
+
};
|
|
1510
|
+
const EntityCaret = ({ className, ...props }) => {
|
|
1511
|
+
const { expanded } = useEntity();
|
|
1512
|
+
return /* @__PURE__ */ jsx(Icon, { children: /* @__PURE__ */ jsx(
|
|
1513
|
+
ChevronDownIcon,
|
|
1514
|
+
{
|
|
1515
|
+
className: twMerge(
|
|
1516
|
+
`mastra:text-text3 mastra:transition-transform mastra:duration-200 mastra:ease-in-out`,
|
|
1517
|
+
expanded ? "mastra:rotate-0" : "mastra:-rotate-90",
|
|
1518
|
+
className
|
|
1519
|
+
),
|
|
1520
|
+
...props
|
|
1521
|
+
}
|
|
1522
|
+
) });
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
const ToolApprovalClass = twMerge(
|
|
1526
|
+
"mastra:rounded-lg mastra:border mastra:border-border1 mastra:max-w-1/2 mastra:mt-2",
|
|
1527
|
+
"mastra:bg-surface3 mastra:text-text6"
|
|
1528
|
+
);
|
|
1529
|
+
const ToolApproval = ({ className, ...props }) => {
|
|
1530
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalClass, ...props });
|
|
1531
|
+
};
|
|
1532
|
+
const ToolApprovalTitleClass = twMerge("mastra:text-text6 mastra:inline-flex mastra:items-center mastra:gap-1");
|
|
1533
|
+
const ToolApprovalTitle = ({ className, ...props }) => {
|
|
1534
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalTitleClass, ...props });
|
|
1535
|
+
};
|
|
1536
|
+
const ToolApprovalHeaderClass = twMerge(
|
|
1537
|
+
"mastra:flex mastra:justify-between mastra:items-center mastra:gap-2",
|
|
1538
|
+
"mastra:border-b mastra:border-border1 mastra:px-4 mastra:py-2"
|
|
1539
|
+
);
|
|
1540
|
+
const ToolApprovalHeader = ({ className, ...props }) => {
|
|
1541
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalHeaderClass, ...props });
|
|
1542
|
+
};
|
|
1543
|
+
const ToolApprovalContentClass = twMerge("mastra:text-text6 mastra:p-4");
|
|
1544
|
+
const ToolApprovalContent = ({ className, ...props }) => {
|
|
1545
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalContentClass, ...props });
|
|
1546
|
+
};
|
|
1547
|
+
const ToolApprovalActionsClass = twMerge("mastra:flex mastra:gap-2 mastra:items-center");
|
|
1548
|
+
const ToolApprovalActions = ({ className, ...props }) => {
|
|
1549
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalActionsClass, ...props });
|
|
1550
|
+
};
|
|
1551
|
+
|
|
1552
|
+
const EntryClass = "mastra:space-y-2";
|
|
1553
|
+
const Entry = ({ className, ...props }) => {
|
|
1554
|
+
return /* @__PURE__ */ jsx("div", { className: className || EntryClass, ...props });
|
|
1555
|
+
};
|
|
1556
|
+
const EntryTitleClass = "mastra:font-mono mastra:text-sm mastra:text-text3";
|
|
1557
|
+
const EntryTitle = ({ className, as: Root = "h3", ...props }) => {
|
|
1558
|
+
return /* @__PURE__ */ jsx(Root, { className: className || EntryTitleClass, ...props });
|
|
1559
|
+
};
|
|
1560
|
+
|
|
1561
|
+
async function highlight(code, lang) {
|
|
1562
|
+
const out = await codeToHast(code, {
|
|
1563
|
+
lang,
|
|
1564
|
+
theme: "dracula-soft"
|
|
1565
|
+
});
|
|
1566
|
+
return toJsxRuntime(out, {
|
|
1567
|
+
Fragment,
|
|
1568
|
+
jsx,
|
|
1569
|
+
jsxs
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
const Tooltip = ({ children }) => {
|
|
1574
|
+
return /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsx(Root, { children }) });
|
|
1575
|
+
};
|
|
1576
|
+
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";
|
|
1577
|
+
const TooltipContent = ({ children, className, ...props }) => {
|
|
1578
|
+
return /* @__PURE__ */ jsx(TooltipPortal, { children: /* @__PURE__ */ jsx(TooltipContent$1, { className: className || TooltipContentClass, ...props, children }) });
|
|
1579
|
+
};
|
|
1580
|
+
const TooltipTrigger = (props) => {
|
|
1581
|
+
return /* @__PURE__ */ jsx(TooltipTrigger$1, { ...props, asChild: true });
|
|
1582
|
+
};
|
|
1583
|
+
|
|
1584
|
+
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";
|
|
1585
|
+
const IconButton = ({ children, tooltip, size = "md", className, ...props }) => {
|
|
1586
|
+
return /* @__PURE__ */ jsxs(Tooltip, { children: [
|
|
1587
|
+
/* @__PURE__ */ jsx(TooltipTrigger, { children: /* @__PURE__ */ jsx(
|
|
1588
|
+
"button",
|
|
1589
|
+
{
|
|
1590
|
+
...props,
|
|
1591
|
+
className: className || twMerge(IconButtonClass, size === "md" && "mastra:p-0.5", size === "lg" && "mastra:p-1"),
|
|
1592
|
+
children: /* @__PURE__ */ jsx(Icon, { size, children })
|
|
1593
|
+
}
|
|
1594
|
+
) }),
|
|
1595
|
+
/* @__PURE__ */ jsx(TooltipContent, { children: tooltip })
|
|
1596
|
+
] });
|
|
1597
|
+
};
|
|
1598
|
+
|
|
1599
|
+
const CodeBlockClass = "mastra:rounded-lg mastra:[&>pre]:p-4 mastra:overflow-hidden mastra:[&>pre]:!bg-surface4 mastra:[&>pre>code]:leading-5 mastra:relative";
|
|
1600
|
+
const CodeBlock = ({ code, language, className, cta }) => {
|
|
1601
|
+
const [nodes, setNodes] = useState(null);
|
|
1602
|
+
useLayoutEffect(() => {
|
|
1603
|
+
void highlight(code, language).then(setNodes);
|
|
1604
|
+
}, [language]);
|
|
1605
|
+
return /* @__PURE__ */ jsxs("div", { className: className || CodeBlockClass, children: [
|
|
1606
|
+
nodes ?? null,
|
|
1607
|
+
cta
|
|
1608
|
+
] });
|
|
1609
|
+
};
|
|
1610
|
+
const CodeCopyButton = ({ code }) => {
|
|
1611
|
+
const [isCopied, setIsCopied] = useState(false);
|
|
1612
|
+
const handleCopy = () => {
|
|
1613
|
+
navigator.clipboard.writeText(code);
|
|
1614
|
+
setIsCopied(true);
|
|
1615
|
+
setTimeout(() => setIsCopied(false), 2e3);
|
|
1616
|
+
};
|
|
1617
|
+
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, {}) }) });
|
|
1618
|
+
};
|
|
1619
|
+
|
|
1620
|
+
const AgentIcon = ({ className, ...props }) => /* @__PURE__ */ jsxs(
|
|
1621
|
+
"svg",
|
|
1622
|
+
{
|
|
1623
|
+
width: "17",
|
|
1624
|
+
height: "16",
|
|
1625
|
+
viewBox: "0 0 17 16",
|
|
1626
|
+
fill: "none",
|
|
1627
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
1628
|
+
...props,
|
|
1629
|
+
className: twMerge("mastra-icon", className),
|
|
1630
|
+
children: [
|
|
1631
|
+
/* @__PURE__ */ jsx(
|
|
1632
|
+
"path",
|
|
1633
|
+
{
|
|
1634
|
+
fillRule: "evenodd",
|
|
1635
|
+
clipRule: "evenodd",
|
|
1636
|
+
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",
|
|
1637
|
+
fill: "currentColor"
|
|
1638
|
+
}
|
|
1639
|
+
),
|
|
1640
|
+
/* @__PURE__ */ jsx(
|
|
1641
|
+
"path",
|
|
1642
|
+
{
|
|
1643
|
+
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",
|
|
1644
|
+
fill: "currentColor"
|
|
1645
|
+
}
|
|
1646
|
+
)
|
|
1647
|
+
]
|
|
1648
|
+
}
|
|
1649
|
+
);
|
|
1650
|
+
|
|
1651
|
+
const ToolsIcon = ({ className, ...props }) => /* @__PURE__ */ jsx(
|
|
1652
|
+
"svg",
|
|
1653
|
+
{
|
|
1654
|
+
width: "17",
|
|
1655
|
+
height: "16",
|
|
1656
|
+
viewBox: "0 0 17 16",
|
|
1657
|
+
fill: "none",
|
|
1658
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
1659
|
+
...props,
|
|
1660
|
+
className: twMerge("mastra-icon", className),
|
|
1661
|
+
children: /* @__PURE__ */ jsx(
|
|
1662
|
+
"path",
|
|
1663
|
+
{
|
|
1664
|
+
fillRule: "evenodd",
|
|
1665
|
+
clipRule: "evenodd",
|
|
1666
|
+
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",
|
|
1667
|
+
fill: "currentColor"
|
|
1668
|
+
}
|
|
1669
|
+
)
|
|
1670
|
+
}
|
|
1671
|
+
);
|
|
1672
|
+
|
|
1673
|
+
const WorkflowIcon = ({ className, ...props }) => /* @__PURE__ */ jsx(
|
|
1674
|
+
"svg",
|
|
1675
|
+
{
|
|
1676
|
+
width: "17",
|
|
1677
|
+
height: "16",
|
|
1678
|
+
viewBox: "0 0 17 16",
|
|
1679
|
+
fill: "none",
|
|
1680
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
1681
|
+
...props,
|
|
1682
|
+
className: twMerge("mastra-icon", className),
|
|
1683
|
+
children: /* @__PURE__ */ jsx(
|
|
1684
|
+
"path",
|
|
1685
|
+
{
|
|
1686
|
+
fillRule: "evenodd",
|
|
1687
|
+
clipRule: "evenodd",
|
|
1688
|
+
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",
|
|
1689
|
+
fill: "currentColor"
|
|
1690
|
+
}
|
|
1691
|
+
)
|
|
1692
|
+
}
|
|
1693
|
+
);
|
|
1694
|
+
|
|
1695
|
+
const MessageClass = "mastra:flex mastra:flex-col mastra:w-full mastra:py-4 mastra:gap-2 mastra:group";
|
|
1696
|
+
const Message = ({ position, className, children, ...props }) => {
|
|
1697
|
+
return /* @__PURE__ */ jsx(
|
|
1698
|
+
"div",
|
|
1699
|
+
{
|
|
1700
|
+
className: className || twMerge(
|
|
1701
|
+
MessageClass,
|
|
1702
|
+
position === "left" ? "" : "mastra:items-end mastra:[&_.mastra-message-content]:bg-surface4 mastra:[&_.mastra-message-content]:px-4"
|
|
1703
|
+
),
|
|
1704
|
+
...props,
|
|
1705
|
+
children
|
|
1706
|
+
}
|
|
1707
|
+
);
|
|
1708
|
+
};
|
|
1709
|
+
const MessageContentClass = "mastra:max-w-4/5 mastra:py-2 mastra:text-text6 mastra:rounded-lg mastra-message-content mastra:text-md";
|
|
1710
|
+
const MessageContent = ({ children, className, isStreaming, ...props }) => {
|
|
1711
|
+
return /* @__PURE__ */ jsxs("div", { className: className || MessageContentClass, ...props, children: [
|
|
1712
|
+
children,
|
|
1713
|
+
isStreaming && /* @__PURE__ */ jsx(MessageStreaming, {})
|
|
1714
|
+
] });
|
|
1715
|
+
};
|
|
1716
|
+
const MessageActionsClass = "mastra:gap-2 mastra:flex mastra:opacity-0 mastra:group-hover:opacity-100 mastra:group-focus-within:opacity-100 mastra:items-center";
|
|
1717
|
+
const MessageActions = ({ children, className, ...props }) => {
|
|
1718
|
+
return /* @__PURE__ */ jsx("div", { className: className || MessageActionsClass, ...props, children });
|
|
1719
|
+
};
|
|
1720
|
+
const MessageUsagesClass = "mastra:flex mastra:gap-2 mastra:items-center";
|
|
1721
|
+
const MessageUsages = ({ children, className, ...props }) => {
|
|
1722
|
+
return /* @__PURE__ */ jsx("div", { className: className || MessageUsagesClass, ...props, children });
|
|
1723
|
+
};
|
|
1724
|
+
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";
|
|
1725
|
+
const MessageUsage = ({ children, className, ...props }) => {
|
|
1726
|
+
return /* @__PURE__ */ jsx("dl", { className: className || MessageUsageClass, ...props, children });
|
|
1727
|
+
};
|
|
1728
|
+
const MessageUsageEntryClass = "mastra:text-text3 mastra:text-xs mastra:flex mastra:gap-1 mastra:items-center";
|
|
1729
|
+
const MessageUsageEntry = ({ children, className, ...props }) => {
|
|
1730
|
+
return /* @__PURE__ */ jsx("dt", { className: className || MessageUsageEntryClass, ...props, children });
|
|
1731
|
+
};
|
|
1732
|
+
const MessageUsageValueClass = "mastra:text-text6 mastra:text-xs";
|
|
1733
|
+
const MessageUsageValue = ({ children, className, ...props }) => {
|
|
1734
|
+
return /* @__PURE__ */ jsx("dd", { className: className || MessageUsageValueClass, ...props, children });
|
|
1735
|
+
};
|
|
1736
|
+
const MessageListClass = "mastra:overflow-y-auto mastra:h-full mastra-list";
|
|
1737
|
+
const MessageList = ({ children, className, ...props }) => {
|
|
1738
|
+
const listRef = useRef(null);
|
|
1739
|
+
useEffect(() => {
|
|
1740
|
+
const scrollToBottom = () => {
|
|
1741
|
+
if (!listRef.current) return;
|
|
1742
|
+
listRef.current.scrollTo({ top: listRef.current.scrollHeight, behavior: "smooth" });
|
|
1743
|
+
};
|
|
1744
|
+
requestAnimationFrame(scrollToBottom);
|
|
1745
|
+
});
|
|
1746
|
+
return /* @__PURE__ */ jsx("div", { className: className || MessageListClass, ...props, ref: listRef, children });
|
|
1747
|
+
};
|
|
1748
|
+
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";
|
|
1749
|
+
const MessageStreaming = ({ className, ...props }) => {
|
|
1750
|
+
return /* @__PURE__ */ jsx("span", { className: className || MessageStreamingClass, ...props });
|
|
1097
1751
|
};
|
|
1098
1752
|
|
|
1099
|
-
export { MastraReactProvider, mapWorkflowStreamChunkToWatchResult, toAssistantUIMessage, toUIMessage, useChat, useMastraClient };
|
|
1100
|
-
//# sourceMappingURL=index.
|
|
1753
|
+
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 };
|
|
1754
|
+
//# sourceMappingURL=index.js.map
|