@mastra/react 0.2.7-alpha.0 → 0.2.8-alpha.0
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 +26 -0
- package/LICENSE.md +15 -0
- package/dist/{src/agent → agent}/hooks.d.ts +9 -8
- package/dist/agent/hooks.d.ts.map +1 -0
- package/dist/{src/agent → agent}/types.d.ts +1 -0
- package/dist/agent/types.d.ts.map +1 -0
- package/dist/index.cjs +2764 -66
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2705 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/ai-sdk/index.d.ts +5 -0
- package/dist/lib/ai-sdk/index.d.ts.map +1 -0
- package/dist/{src/lib → lib}/ai-sdk/memory/resolveInitialMessages.d.ts +2 -1
- package/dist/lib/ai-sdk/memory/resolveInitialMessages.d.ts.map +1 -0
- package/dist/{src/lib → lib}/ai-sdk/transformers/AISdkNetworkTransformer.d.ts +4 -3
- package/dist/lib/ai-sdk/transformers/AISdkNetworkTransformer.d.ts.map +1 -0
- package/dist/lib/ai-sdk/transformers/types.d.ts +11 -0
- package/dist/lib/ai-sdk/transformers/types.d.ts.map +1 -0
- package/dist/{src/lib → lib}/ai-sdk/types.d.ts +3 -2
- package/dist/lib/ai-sdk/types.d.ts.map +1 -0
- package/dist/{src/lib → lib}/ai-sdk/utils/fromCoreUserMessageToUIMessage.d.ts +3 -2
- package/dist/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.d.ts.map +1 -0
- package/dist/{src/lib → lib}/ai-sdk/utils/toAssistantUIMessage.d.ts +3 -2
- package/dist/lib/ai-sdk/utils/toAssistantUIMessage.d.ts.map +1 -0
- package/dist/{src/lib → lib}/ai-sdk/utils/toUIMessage.d.ts +4 -3
- package/dist/lib/ai-sdk/utils/toUIMessage.d.ts.map +1 -0
- package/dist/{src/lib → lib}/use-mutation.d.ts +1 -0
- package/dist/lib/use-mutation.d.ts.map +1 -0
- package/dist/{src/mastra-client-context.d.ts → mastra-client-context.d.ts} +3 -1
- package/dist/mastra-client-context.d.ts.map +1 -0
- package/dist/{src/mastra-react-provider.d.ts → mastra-react-provider.d.ts} +2 -1
- package/dist/mastra-react-provider.d.ts.map +1 -0
- package/dist/react.css +184 -322
- package/dist/{src/ui → ui}/Code/Code.d.ts +2 -1
- package/dist/ui/Code/Code.d.ts.map +1 -0
- package/dist/ui/Code/highlight.d.ts +4 -0
- package/dist/ui/Code/highlight.d.ts.map +1 -0
- package/dist/ui/Code/index.d.ts +2 -0
- package/dist/ui/Code/index.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Entity/Entity.d.ts +2 -1
- package/dist/ui/Entity/Entity.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Entity/Entry.d.ts +2 -1
- package/dist/ui/Entity/Entry.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Entity/ToolApproval.d.ts +1 -0
- package/dist/ui/Entity/ToolApproval.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Entity/context.d.ts +4 -3
- package/dist/ui/Entity/context.d.ts.map +1 -0
- package/dist/ui/Entity/index.d.ts +5 -0
- package/dist/ui/Entity/index.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Entity/types.d.ts +1 -0
- package/dist/ui/Entity/types.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Icon/Icon.d.ts +2 -1
- package/dist/ui/Icon/Icon.d.ts.map +1 -0
- package/dist/ui/Icon/index.d.ts +2 -0
- package/dist/ui/Icon/index.d.ts.map +1 -0
- package/dist/{src/ui → ui}/IconButton/IconButton.d.ts +2 -1
- package/dist/ui/IconButton/IconButton.d.ts.map +1 -0
- package/dist/ui/IconButton/index.d.ts +2 -0
- package/dist/ui/IconButton/index.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Icons/AgentIcon.d.ts +2 -1
- package/dist/ui/Icons/AgentIcon.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Icons/ToolsIcon.d.ts +2 -1
- package/dist/ui/Icons/ToolsIcon.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Icons/WorkflowIcon.d.ts +2 -1
- package/dist/ui/Icons/WorkflowIcon.d.ts.map +1 -0
- package/dist/ui/Icons/index.d.ts +4 -0
- package/dist/ui/Icons/index.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Message/Message.d.ts +2 -1
- package/dist/ui/Message/Message.d.ts.map +1 -0
- package/dist/ui/Message/index.d.ts +2 -0
- package/dist/ui/Message/index.d.ts.map +1 -0
- package/dist/{src/ui → ui}/Tooltip/Tooltip.d.ts +2 -1
- package/dist/ui/Tooltip/Tooltip.d.ts.map +1 -0
- package/dist/ui/Tooltip/index.d.ts +2 -0
- package/dist/ui/Tooltip/index.d.ts.map +1 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.d.ts.map +1 -0
- package/dist/{src/workflows → workflows}/hooks.d.ts +5 -4
- package/dist/workflows/hooks.d.ts.map +1 -0
- package/dist/workflows/index.d.ts +3 -0
- package/dist/workflows/index.d.ts.map +1 -0
- package/dist/{src/workflows → workflows}/types.d.ts +4 -3
- package/dist/workflows/types.d.ts.map +1 -0
- package/dist/{src/workflows → workflows}/use-stream-workflow.d.ts +6 -5
- package/dist/workflows/use-stream-workflow.d.ts.map +1 -0
- package/package.json +14 -10
- package/dist/chunk-REDZDNFN-B3BKm5Q3.js +0 -249
- package/dist/chunk-REDZDNFN-B3BKm5Q3.js.map +0 -1
- package/dist/chunk-REDZDNFN-B6_m0Vw5.cjs +0 -251
- package/dist/chunk-REDZDNFN-B6_m0Vw5.cjs.map +0 -1
- package/dist/index-0ViTRi6s.cjs +0 -26374
- package/dist/index-0ViTRi6s.cjs.map +0 -1
- package/dist/index-BaK_Y6TP.cjs +0 -185
- package/dist/index-BaK_Y6TP.cjs.map +0 -1
- package/dist/index-C1OzXW5i.js +0 -180
- package/dist/index-C1OzXW5i.js.map +0 -1
- package/dist/index-D3vBmpgO.js +0 -26288
- package/dist/index-D3vBmpgO.js.map +0 -1
- package/dist/src/index.d.ts +0 -7
- package/dist/src/lib/ai-sdk/index.d.ts +0 -4
- package/dist/src/lib/ai-sdk/memory/resolveInitialMessages.test.d.ts +0 -1
- package/dist/src/lib/ai-sdk/transformers/AISdkNetworkTransformer.test.d.ts +0 -1
- package/dist/src/lib/ai-sdk/transformers/types.d.ts +0 -10
- package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.test.d.ts +0 -1
- package/dist/src/lib/ai-sdk/utils/toAssistantUIMessage.test.d.ts +0 -1
- package/dist/src/lib/ai-sdk/utils/toUIMessage.test.d.ts +0 -1
- package/dist/src/ui/Code/highlight.d.ts +0 -3
- package/dist/src/ui/Code/index.d.ts +0 -1
- package/dist/src/ui/Entity/Entity.stories.d.ts +0 -22
- package/dist/src/ui/Entity/index.d.ts +0 -4
- package/dist/src/ui/Icon/index.d.ts +0 -1
- package/dist/src/ui/IconButton/IconButton.stories.d.ts +0 -12
- package/dist/src/ui/IconButton/index.d.ts +0 -1
- package/dist/src/ui/Icons/index.d.ts +0 -3
- package/dist/src/ui/Message/Message.stories.d.ts +0 -13
- package/dist/src/ui/Message/index.d.ts +0 -1
- package/dist/src/ui/Tooltip/Tooltip.stories.d.ts +0 -12
- package/dist/src/ui/Tooltip/index.d.ts +0 -1
- package/dist/src/ui/index.d.ts +0 -7
- package/dist/src/workflows/index.d.ts +0 -2
- package/dist/token-6GSAFR2W-XRCSVUPZ-DHFPWxwn.cjs +0 -64
- package/dist/token-6GSAFR2W-XRCSVUPZ-DHFPWxwn.cjs.map +0 -1
- package/dist/token-6GSAFR2W-XRCSVUPZ-PJRK45Cx.js +0 -60
- package/dist/token-6GSAFR2W-XRCSVUPZ-PJRK45Cx.js.map +0 -1
- package/dist/token-util-NEHG7TUY-U7CX7GS4-B0wfFQ5e.cjs +0 -11
- package/dist/token-util-NEHG7TUY-U7CX7GS4-B0wfFQ5e.cjs.map +0 -1
- package/dist/token-util-NEHG7TUY-U7CX7GS4-C_BDhp1r.js +0 -7
- package/dist/token-util-NEHG7TUY-U7CX7GS4-C_BDhp1r.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2706 @@
|
|
|
1
|
-
|
|
1
|
+
import { MastraClient } from '@mastra/client-js';
|
|
2
|
+
import { createContext, useContext, useRef, useState, useEffect, useLayoutEffect, useCallback, Fragment } from 'react';
|
|
3
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
4
|
+
import { v4 } from '@lukeed/uuid';
|
|
5
|
+
import { formatStreamCompletionFeedback, formatCompletionFeedback } from '@mastra/core/loop';
|
|
6
|
+
import { ChevronDownIcon, CheckIcon, CopyIcon } from 'lucide-react';
|
|
7
|
+
import { twMerge } from 'tailwind-merge';
|
|
8
|
+
import { TooltipProvider, Root, TooltipPortal, TooltipContent as TooltipContent$1, TooltipTrigger as TooltipTrigger$1 } from '@radix-ui/react-tooltip';
|
|
9
|
+
import { toJsxRuntime } from 'hast-util-to-jsx-runtime';
|
|
10
|
+
import { codeToHast } from 'shiki/bundle/web';
|
|
11
|
+
import { RequestContext } from '@mastra/core/request-context';
|
|
12
|
+
|
|
13
|
+
// src/mastra-client-context.tsx
|
|
14
|
+
var MastraClientContext = createContext({});
|
|
15
|
+
var MastraClientProvider = ({ children, baseUrl, headers, apiPrefix }) => {
|
|
16
|
+
const client = createMastraClient(baseUrl, headers, apiPrefix);
|
|
17
|
+
return /* @__PURE__ */ jsx(MastraClientContext.Provider, { value: client, children });
|
|
18
|
+
};
|
|
19
|
+
var useMastraClient = () => useContext(MastraClientContext);
|
|
20
|
+
var IPV4_LOOPBACK_RE = /^127\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
|
|
21
|
+
var isIPv4Loopback = (hostname) => {
|
|
22
|
+
const m = IPV4_LOOPBACK_RE.exec(hostname);
|
|
23
|
+
if (!m) return false;
|
|
24
|
+
return +m[1] <= 255 && +m[2] <= 255 && +m[3] <= 255;
|
|
25
|
+
};
|
|
26
|
+
var isLocalUrl = (url) => {
|
|
27
|
+
if (!url) return true;
|
|
28
|
+
try {
|
|
29
|
+
const { hostname } = new URL(url);
|
|
30
|
+
return hostname === "localhost" || hostname.endsWith(".localhost") || isIPv4Loopback(hostname) || hostname === "::1" || hostname === "[::1]";
|
|
31
|
+
} catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var createMastraClient = (baseUrl, mastraClientHeaders = {}, apiPrefix) => {
|
|
36
|
+
return new MastraClient({
|
|
37
|
+
baseUrl: baseUrl || "",
|
|
38
|
+
headers: isLocalUrl(baseUrl) ? { ...mastraClientHeaders, "x-mastra-dev-playground": "true" } : mastraClientHeaders,
|
|
39
|
+
apiPrefix
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
var MastraReactProvider = ({ children, baseUrl, headers, apiPrefix }) => {
|
|
43
|
+
return /* @__PURE__ */ jsx(MastraClientProvider, { baseUrl, headers, apiPrefix, children });
|
|
44
|
+
};
|
|
45
|
+
var mapWorkflowStreamChunkToWatchResult = (prev, chunk) => {
|
|
46
|
+
if (chunk.type === "workflow-start") {
|
|
47
|
+
return {
|
|
48
|
+
input: prev?.input,
|
|
49
|
+
status: "running",
|
|
50
|
+
steps: prev?.steps || {}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (chunk.type === "workflow-canceled") {
|
|
54
|
+
return {
|
|
55
|
+
...prev,
|
|
56
|
+
status: "canceled"
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
if (chunk.type === "workflow-finish") {
|
|
60
|
+
const finalStatus = chunk.payload.workflowStatus;
|
|
61
|
+
const prevSteps = prev?.steps ?? {};
|
|
62
|
+
const lastStep = Object.values(prevSteps).pop();
|
|
63
|
+
return {
|
|
64
|
+
...prev,
|
|
65
|
+
status: chunk.payload.workflowStatus,
|
|
66
|
+
...finalStatus === "success" && lastStep?.status === "success" ? { result: lastStep?.output } : finalStatus === "failed" && lastStep?.status === "failed" ? { error: lastStep?.error } : finalStatus === "tripwire" && chunk.payload.tripwire ? { tripwire: chunk.payload.tripwire } : {}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const { stepCallId, stepName, ...newPayload } = chunk.payload ?? {};
|
|
70
|
+
const newSteps = {
|
|
71
|
+
...prev?.steps,
|
|
72
|
+
[chunk.payload.id]: {
|
|
73
|
+
...prev?.steps?.[chunk.payload.id],
|
|
74
|
+
...newPayload
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (chunk.type === "workflow-step-start") {
|
|
78
|
+
return {
|
|
79
|
+
...prev,
|
|
80
|
+
steps: newSteps
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if (chunk.type === "workflow-step-suspended") {
|
|
84
|
+
const suspendedStepIds = Object.entries(newSteps).flatMap(
|
|
85
|
+
([stepId, stepResult]) => {
|
|
86
|
+
if (stepResult?.status === "suspended") {
|
|
87
|
+
const nestedPath = stepResult?.suspendPayload?.__workflow_meta?.path;
|
|
88
|
+
return nestedPath ? [[stepId, ...nestedPath]] : [[stepId]];
|
|
89
|
+
}
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
return {
|
|
94
|
+
...prev,
|
|
95
|
+
status: "suspended",
|
|
96
|
+
steps: newSteps,
|
|
97
|
+
suspendPayload: chunk.payload.suspendPayload,
|
|
98
|
+
suspended: suspendedStepIds
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (chunk.type === "workflow-step-waiting") {
|
|
102
|
+
return {
|
|
103
|
+
...prev,
|
|
104
|
+
status: "waiting",
|
|
105
|
+
steps: newSteps
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (chunk.type === "workflow-step-progress") {
|
|
109
|
+
const progressSteps = {
|
|
110
|
+
...prev?.steps,
|
|
111
|
+
[chunk.payload.id]: {
|
|
112
|
+
...prev?.steps?.[chunk.payload.id],
|
|
113
|
+
foreachProgress: {
|
|
114
|
+
completedCount: chunk.payload.completedCount,
|
|
115
|
+
totalCount: chunk.payload.totalCount,
|
|
116
|
+
currentIndex: chunk.payload.currentIndex,
|
|
117
|
+
iterationStatus: chunk.payload.iterationStatus,
|
|
118
|
+
iterationOutput: chunk.payload.iterationOutput
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
return {
|
|
123
|
+
...prev,
|
|
124
|
+
steps: progressSteps
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (chunk.type === "workflow-step-result") {
|
|
128
|
+
return {
|
|
129
|
+
...prev,
|
|
130
|
+
steps: newSteps
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return prev;
|
|
134
|
+
};
|
|
135
|
+
var toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
136
|
+
const result = [...conversation];
|
|
137
|
+
if (chunk.type.startsWith("data-")) {
|
|
138
|
+
const lastMessage = result[result.length - 1];
|
|
139
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
140
|
+
const newMessage = {
|
|
141
|
+
id: `data-${chunk.runId}-${Date.now()}`,
|
|
142
|
+
role: "assistant",
|
|
143
|
+
parts: [
|
|
144
|
+
{
|
|
145
|
+
type: chunk.type,
|
|
146
|
+
data: "data" in chunk ? chunk.data : void 0
|
|
147
|
+
}
|
|
148
|
+
],
|
|
149
|
+
metadata
|
|
150
|
+
};
|
|
151
|
+
return [...result, newMessage];
|
|
152
|
+
}
|
|
153
|
+
const updatedMessage = {
|
|
154
|
+
...lastMessage,
|
|
155
|
+
parts: [
|
|
156
|
+
...lastMessage.parts,
|
|
157
|
+
{
|
|
158
|
+
type: chunk.type,
|
|
159
|
+
data: "data" in chunk ? chunk.data : void 0
|
|
160
|
+
}
|
|
161
|
+
]
|
|
162
|
+
};
|
|
163
|
+
return [...result.slice(0, -1), updatedMessage];
|
|
164
|
+
}
|
|
165
|
+
switch (chunk.type) {
|
|
166
|
+
case "tripwire": {
|
|
167
|
+
const newMessage = {
|
|
168
|
+
id: `tripwire-${chunk.runId + Date.now()}`,
|
|
169
|
+
role: "assistant",
|
|
170
|
+
parts: [
|
|
171
|
+
{
|
|
172
|
+
type: "text",
|
|
173
|
+
text: chunk.payload.reason
|
|
174
|
+
}
|
|
175
|
+
],
|
|
176
|
+
metadata: {
|
|
177
|
+
...metadata,
|
|
178
|
+
status: "tripwire",
|
|
179
|
+
tripwire: {
|
|
180
|
+
retry: chunk.payload.retry,
|
|
181
|
+
tripwirePayload: chunk.payload.metadata,
|
|
182
|
+
processorId: chunk.payload.processorId
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
return [...result, newMessage];
|
|
187
|
+
}
|
|
188
|
+
case "start": {
|
|
189
|
+
const newMessage = {
|
|
190
|
+
id: typeof chunk.payload.messageId === "string" ? chunk.payload.messageId : `start-${chunk.runId + Date.now()}`,
|
|
191
|
+
role: "assistant",
|
|
192
|
+
parts: [],
|
|
193
|
+
metadata
|
|
194
|
+
};
|
|
195
|
+
return [...result, newMessage];
|
|
196
|
+
}
|
|
197
|
+
case "text-start": {
|
|
198
|
+
const lastMessage = result[result.length - 1];
|
|
199
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
200
|
+
const textId = chunk.payload.id || `text-${Date.now()}`;
|
|
201
|
+
const newTextPart = {
|
|
202
|
+
type: "text",
|
|
203
|
+
text: "",
|
|
204
|
+
state: "streaming",
|
|
205
|
+
textId,
|
|
206
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
207
|
+
};
|
|
208
|
+
if (lastMessage.metadata?.completionResult) {
|
|
209
|
+
const newMessage = {
|
|
210
|
+
id: `start-${chunk.runId}-${Date.now()}`,
|
|
211
|
+
role: "assistant",
|
|
212
|
+
parts: [newTextPart],
|
|
213
|
+
metadata
|
|
214
|
+
};
|
|
215
|
+
return [...result, newMessage];
|
|
216
|
+
}
|
|
217
|
+
const parts = [...lastMessage.parts];
|
|
218
|
+
parts.push(newTextPart);
|
|
219
|
+
return [
|
|
220
|
+
...result.slice(0, -1),
|
|
221
|
+
{
|
|
222
|
+
...lastMessage,
|
|
223
|
+
parts
|
|
224
|
+
}
|
|
225
|
+
];
|
|
226
|
+
}
|
|
227
|
+
case "text-delta": {
|
|
228
|
+
const lastMessage = result[result.length - 1];
|
|
229
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
230
|
+
const parts = [...lastMessage.parts];
|
|
231
|
+
const textId = chunk.payload.id;
|
|
232
|
+
let textPartIndex = textId ? parts.findLastIndex((part) => part.type === "text" && part.textId === textId) : -1;
|
|
233
|
+
if (textPartIndex === -1) {
|
|
234
|
+
textPartIndex = parts.findLastIndex(
|
|
235
|
+
(part) => part.type === "text" && part.state === "streaming"
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
if (textPartIndex === -1) {
|
|
239
|
+
const newTextPart = {
|
|
240
|
+
type: "text",
|
|
241
|
+
text: chunk.payload.text,
|
|
242
|
+
state: "streaming",
|
|
243
|
+
textId,
|
|
244
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
245
|
+
};
|
|
246
|
+
parts.push(newTextPart);
|
|
247
|
+
} else {
|
|
248
|
+
const textPart = parts[textPartIndex];
|
|
249
|
+
if (textPart.type === "text") {
|
|
250
|
+
const extendedTextPart = textPart;
|
|
251
|
+
const updatedTextPart = {
|
|
252
|
+
...extendedTextPart,
|
|
253
|
+
text: extendedTextPart.text + chunk.payload.text,
|
|
254
|
+
state: "streaming"
|
|
255
|
+
};
|
|
256
|
+
parts[textPartIndex] = updatedTextPart;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return [
|
|
260
|
+
...result.slice(0, -1),
|
|
261
|
+
{
|
|
262
|
+
...lastMessage,
|
|
263
|
+
parts
|
|
264
|
+
}
|
|
265
|
+
];
|
|
266
|
+
}
|
|
267
|
+
case "reasoning-delta": {
|
|
268
|
+
const lastMessage = result[result.length - 1];
|
|
269
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
270
|
+
const newMessage = {
|
|
271
|
+
id: `reasoning-${chunk.runId + Date.now()}`,
|
|
272
|
+
role: "assistant",
|
|
273
|
+
parts: [
|
|
274
|
+
{
|
|
275
|
+
type: "reasoning",
|
|
276
|
+
text: chunk.payload.text,
|
|
277
|
+
state: "streaming",
|
|
278
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
279
|
+
}
|
|
280
|
+
],
|
|
281
|
+
metadata
|
|
282
|
+
};
|
|
283
|
+
return [...result, newMessage];
|
|
284
|
+
}
|
|
285
|
+
const parts = [...lastMessage.parts];
|
|
286
|
+
let reasoningPartIndex = parts.findIndex((part) => part.type === "reasoning");
|
|
287
|
+
if (reasoningPartIndex === -1) {
|
|
288
|
+
parts.push({
|
|
289
|
+
type: "reasoning",
|
|
290
|
+
text: chunk.payload.text,
|
|
291
|
+
state: "streaming",
|
|
292
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
293
|
+
});
|
|
294
|
+
} else {
|
|
295
|
+
const reasoningPart = parts[reasoningPartIndex];
|
|
296
|
+
if (reasoningPart.type === "reasoning") {
|
|
297
|
+
parts[reasoningPartIndex] = {
|
|
298
|
+
...reasoningPart,
|
|
299
|
+
text: reasoningPart.text + chunk.payload.text,
|
|
300
|
+
state: "streaming"
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return [
|
|
305
|
+
...result.slice(0, -1),
|
|
306
|
+
{
|
|
307
|
+
...lastMessage,
|
|
308
|
+
parts
|
|
309
|
+
}
|
|
310
|
+
];
|
|
311
|
+
}
|
|
312
|
+
case "tool-call": {
|
|
313
|
+
const lastMessage = result[result.length - 1];
|
|
314
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
315
|
+
const newMessage = {
|
|
316
|
+
id: `tool-call-${chunk.runId + Date.now()}`,
|
|
317
|
+
role: "assistant",
|
|
318
|
+
parts: [
|
|
319
|
+
{
|
|
320
|
+
type: "dynamic-tool",
|
|
321
|
+
toolName: chunk.payload.toolName,
|
|
322
|
+
toolCallId: chunk.payload.toolCallId,
|
|
323
|
+
state: "input-available",
|
|
324
|
+
input: chunk.payload.args,
|
|
325
|
+
callProviderMetadata: chunk.payload.providerMetadata
|
|
326
|
+
}
|
|
327
|
+
],
|
|
328
|
+
metadata
|
|
329
|
+
};
|
|
330
|
+
return [...result, newMessage];
|
|
331
|
+
}
|
|
332
|
+
const parts = [...lastMessage.parts];
|
|
333
|
+
parts.push({
|
|
334
|
+
type: "dynamic-tool",
|
|
335
|
+
toolName: chunk.payload.toolName,
|
|
336
|
+
toolCallId: chunk.payload.toolCallId,
|
|
337
|
+
state: "input-available",
|
|
338
|
+
input: chunk.payload.args,
|
|
339
|
+
callProviderMetadata: chunk.payload.providerMetadata
|
|
340
|
+
});
|
|
341
|
+
return [
|
|
342
|
+
...result.slice(0, -1),
|
|
343
|
+
{
|
|
344
|
+
...lastMessage,
|
|
345
|
+
parts
|
|
346
|
+
}
|
|
347
|
+
];
|
|
348
|
+
}
|
|
349
|
+
case "tool-error":
|
|
350
|
+
case "tool-result": {
|
|
351
|
+
const lastMessage = result[result.length - 1];
|
|
352
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
353
|
+
const parts = [...lastMessage.parts];
|
|
354
|
+
const toolPartIndex = parts.findIndex(
|
|
355
|
+
(part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
356
|
+
);
|
|
357
|
+
if (toolPartIndex !== -1) {
|
|
358
|
+
const toolPart = parts[toolPartIndex];
|
|
359
|
+
if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
|
|
360
|
+
const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
|
|
361
|
+
const toolCallId = toolPart.toolCallId;
|
|
362
|
+
if (chunk.type === "tool-result" && chunk.payload.isError || chunk.type === "tool-error") {
|
|
363
|
+
const error = chunk.type === "tool-error" ? chunk.payload.error : chunk.payload.result;
|
|
364
|
+
parts[toolPartIndex] = {
|
|
365
|
+
type: "dynamic-tool",
|
|
366
|
+
toolName,
|
|
367
|
+
toolCallId,
|
|
368
|
+
state: "output-error",
|
|
369
|
+
input: toolPart.input,
|
|
370
|
+
errorText: typeof error === "string" ? error : error instanceof Error ? error.message : error?.message ?? String(error),
|
|
371
|
+
callProviderMetadata: chunk.payload.providerMetadata
|
|
372
|
+
};
|
|
373
|
+
} else {
|
|
374
|
+
const isWorkflow = Boolean(chunk.payload.result?.result?.steps);
|
|
375
|
+
const isAgent = chunk?.from === "AGENT";
|
|
376
|
+
let output;
|
|
377
|
+
if (isWorkflow) {
|
|
378
|
+
output = chunk.payload.result?.result;
|
|
379
|
+
} else if (isAgent) {
|
|
380
|
+
output = parts[toolPartIndex].output ?? chunk.payload.result;
|
|
381
|
+
} else {
|
|
382
|
+
output = chunk.payload.result;
|
|
383
|
+
}
|
|
384
|
+
parts[toolPartIndex] = {
|
|
385
|
+
type: "dynamic-tool",
|
|
386
|
+
toolName,
|
|
387
|
+
toolCallId,
|
|
388
|
+
state: "output-available",
|
|
389
|
+
input: toolPart.input,
|
|
390
|
+
output,
|
|
391
|
+
callProviderMetadata: chunk.payload.providerMetadata
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return [
|
|
397
|
+
...result.slice(0, -1),
|
|
398
|
+
{
|
|
399
|
+
...lastMessage,
|
|
400
|
+
parts
|
|
401
|
+
}
|
|
402
|
+
];
|
|
403
|
+
}
|
|
404
|
+
case "tool-output": {
|
|
405
|
+
const lastMessage = result[result.length - 1];
|
|
406
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
407
|
+
const parts = [...lastMessage.parts];
|
|
408
|
+
const toolPartIndex = parts.findIndex(
|
|
409
|
+
(part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
410
|
+
);
|
|
411
|
+
if (toolPartIndex !== -1) {
|
|
412
|
+
const toolPart = parts[toolPartIndex];
|
|
413
|
+
if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
|
|
414
|
+
const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : typeof toolPart.type === "string" && toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
|
|
415
|
+
const toolCallId = toolPart.toolCallId;
|
|
416
|
+
const input = toolPart.input;
|
|
417
|
+
if (chunk.payload.output?.type?.startsWith("workflow-")) {
|
|
418
|
+
const existingWorkflowState = toolPart.output || {};
|
|
419
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
|
|
420
|
+
existingWorkflowState,
|
|
421
|
+
chunk.payload.output
|
|
422
|
+
);
|
|
423
|
+
parts[toolPartIndex] = {
|
|
424
|
+
type: "dynamic-tool",
|
|
425
|
+
toolName,
|
|
426
|
+
toolCallId,
|
|
427
|
+
state: "input-streaming",
|
|
428
|
+
input,
|
|
429
|
+
output: updatedWorkflowState
|
|
430
|
+
};
|
|
431
|
+
} else if (chunk.payload.output?.from === "AGENT" || chunk.payload.output?.from === "USER" && chunk.payload.output?.payload?.output?.type?.startsWith("workflow-")) {
|
|
432
|
+
return toUIMessageFromAgent(chunk.payload.output, conversation, metadata, toolCallId, toolName);
|
|
433
|
+
} else {
|
|
434
|
+
const currentOutput = toolPart.output || [];
|
|
435
|
+
const existingOutput = Array.isArray(currentOutput) ? currentOutput : [];
|
|
436
|
+
parts[toolPartIndex] = {
|
|
437
|
+
type: "dynamic-tool",
|
|
438
|
+
toolName,
|
|
439
|
+
toolCallId,
|
|
440
|
+
state: "input-streaming",
|
|
441
|
+
input,
|
|
442
|
+
output: [...existingOutput, chunk.payload.output]
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return [
|
|
448
|
+
...result.slice(0, -1),
|
|
449
|
+
{
|
|
450
|
+
...lastMessage,
|
|
451
|
+
parts
|
|
452
|
+
}
|
|
453
|
+
];
|
|
454
|
+
}
|
|
455
|
+
case "is-task-complete": {
|
|
456
|
+
if (chunk.payload.suppressFeedback) return result;
|
|
457
|
+
const feedback = formatStreamCompletionFeedback(
|
|
458
|
+
{
|
|
459
|
+
complete: chunk.payload.passed,
|
|
460
|
+
scorers: chunk.payload.results,
|
|
461
|
+
totalDuration: chunk.payload.duration,
|
|
462
|
+
timedOut: chunk.payload.timedOut,
|
|
463
|
+
completionReason: chunk.payload.reason
|
|
464
|
+
},
|
|
465
|
+
chunk.payload.maxIterationReached
|
|
466
|
+
);
|
|
467
|
+
const newMessage = {
|
|
468
|
+
id: `is-task-complete-${chunk.runId + Date.now()}`,
|
|
469
|
+
role: "assistant",
|
|
470
|
+
parts: [
|
|
471
|
+
{
|
|
472
|
+
type: "text",
|
|
473
|
+
text: feedback
|
|
474
|
+
}
|
|
475
|
+
],
|
|
476
|
+
metadata: {
|
|
477
|
+
...metadata,
|
|
478
|
+
completionResult: {
|
|
479
|
+
passed: chunk.payload.passed
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
return [...result, newMessage];
|
|
484
|
+
}
|
|
485
|
+
case "source": {
|
|
486
|
+
const lastMessage = result[result.length - 1];
|
|
487
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
488
|
+
const parts = [...lastMessage.parts];
|
|
489
|
+
if (chunk.payload.sourceType === "url") {
|
|
490
|
+
parts.push({
|
|
491
|
+
type: "source-url",
|
|
492
|
+
sourceId: chunk.payload.id,
|
|
493
|
+
url: chunk.payload.url || "",
|
|
494
|
+
title: chunk.payload.title,
|
|
495
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
496
|
+
});
|
|
497
|
+
} else if (chunk.payload.sourceType === "document") {
|
|
498
|
+
parts.push({
|
|
499
|
+
type: "source-document",
|
|
500
|
+
sourceId: chunk.payload.id,
|
|
501
|
+
mediaType: chunk.payload.mimeType || "application/octet-stream",
|
|
502
|
+
title: chunk.payload.title,
|
|
503
|
+
filename: chunk.payload.filename,
|
|
504
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
return [
|
|
508
|
+
...result.slice(0, -1),
|
|
509
|
+
{
|
|
510
|
+
...lastMessage,
|
|
511
|
+
parts
|
|
512
|
+
}
|
|
513
|
+
];
|
|
514
|
+
}
|
|
515
|
+
case "file": {
|
|
516
|
+
const lastMessage = result[result.length - 1];
|
|
517
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
518
|
+
const parts = [...lastMessage.parts];
|
|
519
|
+
let url;
|
|
520
|
+
if (typeof chunk.payload.data === "string") {
|
|
521
|
+
url = chunk.payload.base64 ? `data:${chunk.payload.mimeType};base64,${chunk.payload.data}` : `data:${chunk.payload.mimeType},${encodeURIComponent(chunk.payload.data)}`;
|
|
522
|
+
} else {
|
|
523
|
+
const base64 = btoa(String.fromCharCode(...chunk.payload.data));
|
|
524
|
+
url = `data:${chunk.payload.mimeType};base64,${base64}`;
|
|
525
|
+
}
|
|
526
|
+
parts.push({
|
|
527
|
+
type: "file",
|
|
528
|
+
mediaType: chunk.payload.mimeType,
|
|
529
|
+
url,
|
|
530
|
+
providerMetadata: chunk.payload.providerMetadata
|
|
531
|
+
});
|
|
532
|
+
return [
|
|
533
|
+
...result.slice(0, -1),
|
|
534
|
+
{
|
|
535
|
+
...lastMessage,
|
|
536
|
+
parts
|
|
537
|
+
}
|
|
538
|
+
];
|
|
539
|
+
}
|
|
540
|
+
case "tool-call-approval": {
|
|
541
|
+
const lastMessage = result[result.length - 1];
|
|
542
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
543
|
+
const lastRequireApprovalMetadata = lastMessage.metadata?.mode === "stream" ? lastMessage.metadata?.requireApprovalMetadata : {};
|
|
544
|
+
return [
|
|
545
|
+
...result.slice(0, -1),
|
|
546
|
+
{
|
|
547
|
+
...lastMessage,
|
|
548
|
+
metadata: {
|
|
549
|
+
...lastMessage.metadata,
|
|
550
|
+
mode: "stream",
|
|
551
|
+
requireApprovalMetadata: {
|
|
552
|
+
...lastRequireApprovalMetadata,
|
|
553
|
+
[chunk.payload.toolName]: {
|
|
554
|
+
toolCallId: chunk.payload.toolCallId,
|
|
555
|
+
toolName: chunk.payload.toolName,
|
|
556
|
+
args: chunk.payload.args
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
];
|
|
562
|
+
}
|
|
563
|
+
case "tool-call-suspended": {
|
|
564
|
+
const lastMessage = result[result.length - 1];
|
|
565
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
566
|
+
const lastSuspendedTools = lastMessage.metadata?.mode === "stream" ? lastMessage.metadata?.suspendedTools : {};
|
|
567
|
+
return [
|
|
568
|
+
...result.slice(0, -1),
|
|
569
|
+
{
|
|
570
|
+
...lastMessage,
|
|
571
|
+
metadata: {
|
|
572
|
+
...lastMessage.metadata,
|
|
573
|
+
mode: "stream",
|
|
574
|
+
suspendedTools: {
|
|
575
|
+
...lastSuspendedTools,
|
|
576
|
+
[chunk.payload.toolName]: {
|
|
577
|
+
toolCallId: chunk.payload.toolCallId,
|
|
578
|
+
toolName: chunk.payload.toolName,
|
|
579
|
+
args: chunk.payload.args,
|
|
580
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
];
|
|
586
|
+
}
|
|
587
|
+
case "finish": {
|
|
588
|
+
const lastMessage = result[result.length - 1];
|
|
589
|
+
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
590
|
+
const parts = lastMessage.parts.map((part) => {
|
|
591
|
+
if (typeof part === "object" && part !== null && "type" in part && "state" in part && part.state === "streaming") {
|
|
592
|
+
if (part.type === "text" || part.type === "reasoning") {
|
|
593
|
+
return { ...part, state: "done" };
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return part;
|
|
597
|
+
});
|
|
598
|
+
return [
|
|
599
|
+
...result.slice(0, -1),
|
|
600
|
+
{
|
|
601
|
+
...lastMessage,
|
|
602
|
+
parts
|
|
603
|
+
}
|
|
604
|
+
];
|
|
605
|
+
}
|
|
606
|
+
case "error": {
|
|
607
|
+
const newMessage = {
|
|
608
|
+
id: `error-${chunk.runId + Date.now()}`,
|
|
609
|
+
role: "assistant",
|
|
610
|
+
parts: [
|
|
611
|
+
{
|
|
612
|
+
type: "text",
|
|
613
|
+
text: typeof chunk.payload.error === "string" ? chunk.payload.error : JSON.stringify(chunk.payload.error)
|
|
614
|
+
}
|
|
615
|
+
],
|
|
616
|
+
metadata: {
|
|
617
|
+
...metadata,
|
|
618
|
+
status: "error"
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
return [...result, newMessage];
|
|
622
|
+
}
|
|
623
|
+
// For all other chunk types, return conversation unchanged
|
|
624
|
+
default:
|
|
625
|
+
return result;
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
var toUIMessageFromAgent = (chunk, conversation, metadata, parentToolCallId, parentToolName) => {
|
|
629
|
+
const lastMessage = conversation[conversation.length - 1];
|
|
630
|
+
if (!lastMessage || lastMessage.role !== "assistant") return conversation;
|
|
631
|
+
const parts = [...lastMessage.parts];
|
|
632
|
+
if (chunk.type === "text-delta") {
|
|
633
|
+
const agentChunk = chunk.payload;
|
|
634
|
+
const toolPartIndex = parts.findIndex(
|
|
635
|
+
(part) => part.type === "dynamic-tool" && (parentToolCallId && part.toolCallId === parentToolCallId || parentToolName && part.toolName === parentToolName)
|
|
636
|
+
);
|
|
637
|
+
if (toolPartIndex === -1) return conversation;
|
|
638
|
+
const toolPart = parts[toolPartIndex];
|
|
639
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
640
|
+
const lastChildMessage = childMessages[childMessages.length - 1];
|
|
641
|
+
const textMessage = { type: "text", content: (lastChildMessage?.content || "") + agentChunk.text };
|
|
642
|
+
const nextMessages = lastChildMessage?.type === "text" ? [...childMessages.slice(0, -1), textMessage] : [...childMessages, textMessage];
|
|
643
|
+
parts[toolPartIndex] = {
|
|
644
|
+
...toolPart,
|
|
645
|
+
output: {
|
|
646
|
+
childMessages: nextMessages
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
} else if (chunk.type === "tool-call") {
|
|
650
|
+
const agentChunk = chunk.payload;
|
|
651
|
+
const toolPartIndex = parts.findIndex(
|
|
652
|
+
(part) => part.type === "dynamic-tool" && (parentToolCallId && part.toolCallId === parentToolCallId || parentToolName && part.toolName === parentToolName)
|
|
653
|
+
);
|
|
654
|
+
if (toolPartIndex === -1) return conversation;
|
|
655
|
+
const toolPart = parts[toolPartIndex];
|
|
656
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
657
|
+
parts[toolPartIndex] = {
|
|
658
|
+
...toolPart,
|
|
659
|
+
output: {
|
|
660
|
+
...toolPart?.output,
|
|
661
|
+
childMessages: [
|
|
662
|
+
...childMessages,
|
|
663
|
+
{
|
|
664
|
+
type: "tool",
|
|
665
|
+
toolCallId: agentChunk.toolCallId,
|
|
666
|
+
toolName: agentChunk.toolName,
|
|
667
|
+
args: agentChunk.args
|
|
668
|
+
}
|
|
669
|
+
]
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
} else if (chunk.type === "tool-output") {
|
|
673
|
+
const agentChunk = chunk.payload;
|
|
674
|
+
const toolPartIndex = parts.findIndex(
|
|
675
|
+
(part) => part.type === "dynamic-tool" && (parentToolCallId && part.toolCallId === parentToolCallId || parentToolName && part.toolName === parentToolName)
|
|
676
|
+
);
|
|
677
|
+
if (toolPartIndex === -1) return conversation;
|
|
678
|
+
const toolPart = parts[toolPartIndex];
|
|
679
|
+
if (agentChunk?.output?.type?.startsWith("workflow-")) {
|
|
680
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
681
|
+
const lastToolIndex = childMessages.length - 1;
|
|
682
|
+
const currentMessage = childMessages[lastToolIndex];
|
|
683
|
+
const actualExistingWorkflowState = currentMessage?.toolOutput || {};
|
|
684
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(actualExistingWorkflowState, agentChunk.output);
|
|
685
|
+
if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
|
|
686
|
+
parts[toolPartIndex] = {
|
|
687
|
+
...toolPart,
|
|
688
|
+
output: {
|
|
689
|
+
...toolPart?.output,
|
|
690
|
+
childMessages: [
|
|
691
|
+
...childMessages.slice(0, -1),
|
|
692
|
+
{
|
|
693
|
+
...currentMessage,
|
|
694
|
+
toolOutput: { ...updatedWorkflowState, runId: agentChunk.output.runId }
|
|
695
|
+
}
|
|
696
|
+
]
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
} else if (chunk.type === "tool-result") {
|
|
702
|
+
const agentChunk = chunk.payload;
|
|
703
|
+
const toolPartIndex = parts.findIndex(
|
|
704
|
+
(part) => part.type === "dynamic-tool" && (parentToolCallId && part.toolCallId === parentToolCallId || parentToolName && part.toolName === parentToolName)
|
|
705
|
+
);
|
|
706
|
+
if (toolPartIndex === -1) return conversation;
|
|
707
|
+
const toolPart = parts[toolPartIndex];
|
|
708
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
709
|
+
const lastToolIndex = childMessages.length - 1;
|
|
710
|
+
const isWorkflow = agentChunk?.toolName?.startsWith("workflow-");
|
|
711
|
+
if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
|
|
712
|
+
parts[toolPartIndex] = {
|
|
713
|
+
...toolPart,
|
|
714
|
+
output: {
|
|
715
|
+
...toolPart?.output,
|
|
716
|
+
childMessages: [
|
|
717
|
+
...childMessages.slice(0, -1),
|
|
718
|
+
{
|
|
719
|
+
...childMessages[lastToolIndex],
|
|
720
|
+
toolOutput: isWorkflow ? { ...agentChunk.result?.result, runId: agentChunk.result?.runId } : agentChunk.result
|
|
721
|
+
}
|
|
722
|
+
]
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
return [
|
|
728
|
+
...conversation.slice(0, -1),
|
|
729
|
+
{
|
|
730
|
+
...lastMessage,
|
|
731
|
+
parts
|
|
732
|
+
}
|
|
733
|
+
];
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
// src/lib/ai-sdk/utils/toAssistantUIMessage.ts
|
|
737
|
+
var toAssistantUIMessage = (message) => {
|
|
738
|
+
const extendedMessage = message;
|
|
739
|
+
const content = message.parts.map((part) => {
|
|
740
|
+
if (part.type === "text") {
|
|
741
|
+
return {
|
|
742
|
+
type: "text",
|
|
743
|
+
text: part.text,
|
|
744
|
+
metadata: message.metadata
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
if (part.type === "reasoning") {
|
|
748
|
+
return {
|
|
749
|
+
type: "reasoning",
|
|
750
|
+
text: part.text,
|
|
751
|
+
metadata: message.metadata
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
if (part.type === "source-url") {
|
|
755
|
+
return {
|
|
756
|
+
type: "source",
|
|
757
|
+
sourceType: "url",
|
|
758
|
+
id: part.sourceId,
|
|
759
|
+
url: part.url,
|
|
760
|
+
title: part.title,
|
|
761
|
+
metadata: message.metadata
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
if (part.type === "source-document") {
|
|
765
|
+
return {
|
|
766
|
+
type: "file",
|
|
767
|
+
filename: part.filename,
|
|
768
|
+
mimeType: part.mediaType,
|
|
769
|
+
data: "",
|
|
770
|
+
// Source documents don't have inline data
|
|
771
|
+
metadata: message.metadata
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
if (part.type === "file") {
|
|
775
|
+
const type = part.mediaType.includes("image/") ? "image" : "file";
|
|
776
|
+
if (type === "file") {
|
|
777
|
+
return {
|
|
778
|
+
type,
|
|
779
|
+
mimeType: part.mediaType,
|
|
780
|
+
data: part.url,
|
|
781
|
+
// Use URL as data source
|
|
782
|
+
metadata: message.metadata
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
if (type === "image") {
|
|
786
|
+
return {
|
|
787
|
+
type,
|
|
788
|
+
image: part.url,
|
|
789
|
+
metadata: message.metadata
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
if (part.type === "dynamic-tool") {
|
|
794
|
+
const baseToolCall = {
|
|
795
|
+
type: "tool-call",
|
|
796
|
+
toolCallId: part.toolCallId,
|
|
797
|
+
toolName: part.toolName,
|
|
798
|
+
argsText: JSON.stringify(part.input),
|
|
799
|
+
args: part.input,
|
|
800
|
+
metadata: message.metadata
|
|
801
|
+
};
|
|
802
|
+
if (part.state === "output-error" && "errorText" in part) {
|
|
803
|
+
return { ...baseToolCall, result: part.errorText, isError: true };
|
|
804
|
+
}
|
|
805
|
+
if ("output" in part) {
|
|
806
|
+
return { ...baseToolCall, result: part.output };
|
|
807
|
+
}
|
|
808
|
+
return baseToolCall;
|
|
809
|
+
}
|
|
810
|
+
if (part.type.startsWith("tool-") && part.state !== "input-available") {
|
|
811
|
+
const toolName2 = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.substring(5);
|
|
812
|
+
const { suspendedToolRunId, ...cleanInput } = "input" in part ? part.input : {};
|
|
813
|
+
const baseToolCall = {
|
|
814
|
+
type: "tool-call",
|
|
815
|
+
toolCallId: "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : "",
|
|
816
|
+
toolName: toolName2,
|
|
817
|
+
argsText: JSON.stringify(cleanInput ?? {}),
|
|
818
|
+
args: cleanInput ?? {},
|
|
819
|
+
metadata: message.metadata
|
|
820
|
+
};
|
|
821
|
+
if ("output" in part) {
|
|
822
|
+
return { ...baseToolCall, result: part.output };
|
|
823
|
+
} else if ("error" in part) {
|
|
824
|
+
return { ...baseToolCall, result: part.error, isError: true };
|
|
825
|
+
}
|
|
826
|
+
return baseToolCall;
|
|
827
|
+
}
|
|
828
|
+
const toolName = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.startsWith("tool-") ? part.type.substring(5) : "";
|
|
829
|
+
const requireApprovalMetadata = extendedMessage.metadata?.requireApprovalMetadata;
|
|
830
|
+
const suspendedTools = extendedMessage.metadata?.suspendedTools;
|
|
831
|
+
const partToolCallId = "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : void 0;
|
|
832
|
+
const suspensionData = toolName ? requireApprovalMetadata?.[toolName] ?? suspendedTools?.[toolName] : void 0;
|
|
833
|
+
if (suspensionData) {
|
|
834
|
+
const { suspendedToolRunId, ...cleanInput } = "input" in part ? part.input : {};
|
|
835
|
+
return {
|
|
836
|
+
type: "tool-call",
|
|
837
|
+
toolCallId: partToolCallId,
|
|
838
|
+
toolName,
|
|
839
|
+
argsText: JSON.stringify(cleanInput ?? {}),
|
|
840
|
+
args: cleanInput,
|
|
841
|
+
metadata: extendedMessage.metadata
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
if (part.type.startsWith("data-")) {
|
|
845
|
+
return {
|
|
846
|
+
type: "data",
|
|
847
|
+
name: part.type.substring(5),
|
|
848
|
+
// Extract name from 'data-{name}'
|
|
849
|
+
data: part.data,
|
|
850
|
+
metadata: message.metadata
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
return {
|
|
854
|
+
type: "text",
|
|
855
|
+
text: "",
|
|
856
|
+
metadata: message.metadata
|
|
857
|
+
};
|
|
858
|
+
});
|
|
859
|
+
let status;
|
|
860
|
+
if (message.role === "assistant" && content.length > 0) {
|
|
861
|
+
const hasStreamingParts = message.parts.some(
|
|
862
|
+
(part) => part.type === "text" && "state" in part && part.state === "streaming" || part.type === "reasoning" && "state" in part && part.state === "streaming"
|
|
863
|
+
);
|
|
864
|
+
const hasToolCalls = message.parts.some((part) => part.type === "dynamic-tool" || part.type.startsWith("tool-"));
|
|
865
|
+
const hasInputAvailableTools = message.parts.some(
|
|
866
|
+
(part) => part.type === "dynamic-tool" && part.state === "input-available"
|
|
867
|
+
);
|
|
868
|
+
const hasErrorTools = message.parts.some(
|
|
869
|
+
(part) => part.type === "dynamic-tool" && part.state === "output-error" || part.type.startsWith("tool-") && "error" in part
|
|
870
|
+
);
|
|
871
|
+
if (hasStreamingParts) {
|
|
872
|
+
status = { type: "running" };
|
|
873
|
+
} else if (hasInputAvailableTools && hasToolCalls) {
|
|
874
|
+
status = { type: "requires-action", reason: "tool-calls" };
|
|
875
|
+
} else if (hasErrorTools) {
|
|
876
|
+
status = { type: "incomplete", reason: "error" };
|
|
877
|
+
} else {
|
|
878
|
+
status = { type: "complete", reason: "stop" };
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
const threadMessage = {
|
|
882
|
+
role: message.role,
|
|
883
|
+
content,
|
|
884
|
+
id: message.id,
|
|
885
|
+
createdAt: extendedMessage.createdAt,
|
|
886
|
+
status,
|
|
887
|
+
attachments: extendedMessage.experimental_attachments
|
|
888
|
+
};
|
|
889
|
+
return threadMessage;
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
// src/lib/ai-sdk/memory/resolveInitialMessages.ts
|
|
893
|
+
var resolveInitialMessages = (messages) => {
|
|
894
|
+
const messagesLength = messages.length;
|
|
895
|
+
return messages.map((message, index) => {
|
|
896
|
+
const networkPart = message.parts.find(
|
|
897
|
+
(part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
|
|
898
|
+
);
|
|
899
|
+
if (networkPart && networkPart.type === "text") {
|
|
900
|
+
try {
|
|
901
|
+
const json = JSON.parse(networkPart.text);
|
|
902
|
+
if (json.isNetwork === true) {
|
|
903
|
+
const selectionReason = json.selectionReason || "";
|
|
904
|
+
const primitiveType = json.primitiveType || "";
|
|
905
|
+
const primitiveId = json.primitiveId || "";
|
|
906
|
+
const finalResult = json.finalResult;
|
|
907
|
+
const messages2 = finalResult?.messages || [];
|
|
908
|
+
const childMessages = [];
|
|
909
|
+
const toolResultMap = /* @__PURE__ */ new Map();
|
|
910
|
+
for (const msg of messages2) {
|
|
911
|
+
if (Array.isArray(msg.content)) {
|
|
912
|
+
for (const part of msg.content) {
|
|
913
|
+
if (typeof part === "object" && part.type === "tool-result") {
|
|
914
|
+
toolResultMap.set(part.toolCallId, part);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
for (const msg of messages2) {
|
|
920
|
+
if (msg.type === "tool-call" && Array.isArray(msg.content)) {
|
|
921
|
+
for (const part of msg.content) {
|
|
922
|
+
if (typeof part === "object" && part.type === "tool-call") {
|
|
923
|
+
const toolCallContent = part;
|
|
924
|
+
const toolResult = toolResultMap.get(toolCallContent.toolCallId);
|
|
925
|
+
const isWorkflow = Boolean(toolResult?.result?.result?.steps);
|
|
926
|
+
childMessages.push({
|
|
927
|
+
type: "tool",
|
|
928
|
+
toolCallId: toolCallContent.toolCallId,
|
|
929
|
+
toolName: toolCallContent.toolName,
|
|
930
|
+
args: toolCallContent.args,
|
|
931
|
+
toolOutput: isWorkflow ? toolResult?.result?.result : toolResult?.result
|
|
932
|
+
});
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
if (finalResult && finalResult.text) {
|
|
938
|
+
childMessages.push({
|
|
939
|
+
type: "text",
|
|
940
|
+
content: finalResult.text
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
const result = primitiveType === "tool" ? finalResult?.result : {
|
|
944
|
+
childMessages,
|
|
945
|
+
result: finalResult?.text || ""
|
|
946
|
+
};
|
|
947
|
+
const nextMessage = {
|
|
948
|
+
role: "assistant",
|
|
949
|
+
parts: [
|
|
950
|
+
{
|
|
951
|
+
type: "dynamic-tool",
|
|
952
|
+
toolCallId: primitiveId,
|
|
953
|
+
toolName: primitiveId,
|
|
954
|
+
state: "output-available",
|
|
955
|
+
input: json.input,
|
|
956
|
+
output: result
|
|
957
|
+
}
|
|
958
|
+
],
|
|
959
|
+
id: message.id,
|
|
960
|
+
metadata: {
|
|
961
|
+
...message.metadata,
|
|
962
|
+
mode: "network",
|
|
963
|
+
selectionReason,
|
|
964
|
+
agentInput: json.input,
|
|
965
|
+
hasMoreMessages: index < messagesLength - 1,
|
|
966
|
+
from: primitiveType === "agent" ? "AGENT" : primitiveType === "tool" ? "TOOL" : "WORKFLOW"
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
return nextMessage;
|
|
970
|
+
}
|
|
971
|
+
} catch {
|
|
972
|
+
return message;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
const extendedMessage = message;
|
|
976
|
+
const pendingToolApprovals = extendedMessage.metadata?.pendingToolApprovals;
|
|
977
|
+
if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
|
|
978
|
+
return {
|
|
979
|
+
...message,
|
|
980
|
+
metadata: {
|
|
981
|
+
...message.metadata,
|
|
982
|
+
mode: "stream",
|
|
983
|
+
requireApprovalMetadata: pendingToolApprovals
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
const suspendedTools = extendedMessage.metadata?.suspendedTools;
|
|
988
|
+
if (suspendedTools && typeof suspendedTools === "object") {
|
|
989
|
+
return {
|
|
990
|
+
...message,
|
|
991
|
+
metadata: {
|
|
992
|
+
...message.metadata,
|
|
993
|
+
mode: "stream",
|
|
994
|
+
suspendedTools
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
}
|
|
998
|
+
return message;
|
|
999
|
+
})?.filter((message) => {
|
|
1000
|
+
const completionModes = ["generate", "stream", "network"];
|
|
1001
|
+
if (message.role === "assistant" && completionModes.includes(message?.metadata?.mode)) {
|
|
1002
|
+
const meta = message.metadata;
|
|
1003
|
+
if (meta?.isTaskCompleteResult?.suppressFeedback || meta?.completionResult?.suppressFeedback) {
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
return true;
|
|
1007
|
+
}
|
|
1008
|
+
return true;
|
|
1009
|
+
});
|
|
1010
|
+
};
|
|
1011
|
+
var resolveToChildMessages = (messages) => {
|
|
1012
|
+
const assistantMessage = messages.find((message) => message.role === "assistant");
|
|
1013
|
+
if (!assistantMessage) return [];
|
|
1014
|
+
const parts = assistantMessage.parts;
|
|
1015
|
+
let childMessages = [];
|
|
1016
|
+
for (const part of parts) {
|
|
1017
|
+
const toolPart = part;
|
|
1018
|
+
if (part.type.startsWith("tool-")) {
|
|
1019
|
+
const toolName = part.type.substring("tool-".length);
|
|
1020
|
+
const isWorkflow = toolName.startsWith("workflow-");
|
|
1021
|
+
childMessages.push({
|
|
1022
|
+
type: "tool",
|
|
1023
|
+
toolCallId: toolPart.toolCallId,
|
|
1024
|
+
toolName,
|
|
1025
|
+
args: toolPart.input,
|
|
1026
|
+
toolOutput: isWorkflow ? { ...toolPart.output?.result, runId: toolPart.output?.runId } : toolPart.output
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
if (part.type === "text") {
|
|
1030
|
+
childMessages.push({
|
|
1031
|
+
type: "text",
|
|
1032
|
+
content: toolPart.text
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
return childMessages;
|
|
1037
|
+
};
|
|
1038
|
+
var AISdkNetworkTransformer = class {
|
|
1039
|
+
transform({ chunk, conversation, metadata }) {
|
|
1040
|
+
const newConversation = [...conversation];
|
|
1041
|
+
if (chunk.type === "routing-agent-text-delta") {
|
|
1042
|
+
return this.handleRoutingAgentConversation(chunk, newConversation);
|
|
1043
|
+
}
|
|
1044
|
+
if (chunk.type.startsWith("agent-execution-")) {
|
|
1045
|
+
return this.handleAgentConversation(chunk, newConversation, metadata);
|
|
1046
|
+
}
|
|
1047
|
+
if (chunk.type.startsWith("workflow-execution-")) {
|
|
1048
|
+
return this.handleWorkflowConversation(chunk, newConversation, metadata);
|
|
1049
|
+
}
|
|
1050
|
+
if (chunk.type.startsWith("tool-execution-")) {
|
|
1051
|
+
return this.handleToolConversation(chunk, newConversation, metadata);
|
|
1052
|
+
}
|
|
1053
|
+
if (chunk.type === "network-validation-end") {
|
|
1054
|
+
if (chunk.payload.suppressFeedback) return newConversation;
|
|
1055
|
+
const feedback = formatCompletionFeedback(
|
|
1056
|
+
{
|
|
1057
|
+
complete: chunk.payload.passed,
|
|
1058
|
+
scorers: chunk.payload.results,
|
|
1059
|
+
totalDuration: chunk.payload.duration,
|
|
1060
|
+
timedOut: chunk.payload.timedOut,
|
|
1061
|
+
completionReason: chunk.payload.reason
|
|
1062
|
+
},
|
|
1063
|
+
chunk.payload.maxIterationReached
|
|
1064
|
+
);
|
|
1065
|
+
const newMessage = {
|
|
1066
|
+
id: `network-validation-end-${chunk.payload.runId}-${Date.now()}`,
|
|
1067
|
+
role: "assistant",
|
|
1068
|
+
parts: [
|
|
1069
|
+
{
|
|
1070
|
+
type: "text",
|
|
1071
|
+
text: feedback
|
|
1072
|
+
}
|
|
1073
|
+
],
|
|
1074
|
+
metadata: {
|
|
1075
|
+
...metadata,
|
|
1076
|
+
mode: "network",
|
|
1077
|
+
completionResult: {
|
|
1078
|
+
passed: chunk.payload.passed
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
return [...newConversation, newMessage];
|
|
1083
|
+
}
|
|
1084
|
+
if (chunk.type === "network-execution-event-step-finish") {
|
|
1085
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1086
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1087
|
+
const agentChunk = chunk.payload;
|
|
1088
|
+
const parts = [...lastMessage.parts];
|
|
1089
|
+
const textPartIndex = parts.findIndex((part) => part.type === "text");
|
|
1090
|
+
if (textPartIndex === -1) {
|
|
1091
|
+
parts.push({
|
|
1092
|
+
type: "text",
|
|
1093
|
+
text: agentChunk.result,
|
|
1094
|
+
state: "done"
|
|
1095
|
+
});
|
|
1096
|
+
return [
|
|
1097
|
+
...newConversation.slice(0, -1),
|
|
1098
|
+
{
|
|
1099
|
+
...lastMessage,
|
|
1100
|
+
parts
|
|
1101
|
+
}
|
|
1102
|
+
];
|
|
1103
|
+
}
|
|
1104
|
+
const textPart = parts[textPartIndex];
|
|
1105
|
+
if (textPart.type === "text") {
|
|
1106
|
+
parts[textPartIndex] = {
|
|
1107
|
+
...textPart,
|
|
1108
|
+
state: "done"
|
|
1109
|
+
};
|
|
1110
|
+
return [
|
|
1111
|
+
...newConversation.slice(0, -1),
|
|
1112
|
+
{
|
|
1113
|
+
...lastMessage,
|
|
1114
|
+
parts
|
|
1115
|
+
}
|
|
1116
|
+
];
|
|
1117
|
+
}
|
|
1118
|
+
return newConversation;
|
|
1119
|
+
}
|
|
1120
|
+
return newConversation;
|
|
1121
|
+
}
|
|
1122
|
+
handleRoutingAgentConversation = (chunk, newConversation) => {
|
|
1123
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1124
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1125
|
+
const agentChunk = chunk.payload;
|
|
1126
|
+
const parts = [...lastMessage.parts];
|
|
1127
|
+
const textPartIndex = parts.findIndex((part) => part.type === "text");
|
|
1128
|
+
if (textPartIndex === -1) {
|
|
1129
|
+
parts.push({
|
|
1130
|
+
type: "text",
|
|
1131
|
+
text: agentChunk.text,
|
|
1132
|
+
state: "streaming"
|
|
1133
|
+
});
|
|
1134
|
+
return [
|
|
1135
|
+
...newConversation.slice(0, -1),
|
|
1136
|
+
{
|
|
1137
|
+
...lastMessage,
|
|
1138
|
+
parts
|
|
1139
|
+
}
|
|
1140
|
+
];
|
|
1141
|
+
}
|
|
1142
|
+
const textPart = parts[textPartIndex];
|
|
1143
|
+
if (textPart.type === "text") {
|
|
1144
|
+
parts[textPartIndex] = {
|
|
1145
|
+
...textPart,
|
|
1146
|
+
text: textPart.text + agentChunk.text,
|
|
1147
|
+
state: "streaming"
|
|
1148
|
+
};
|
|
1149
|
+
return [
|
|
1150
|
+
...newConversation.slice(0, -1),
|
|
1151
|
+
{
|
|
1152
|
+
...lastMessage,
|
|
1153
|
+
parts
|
|
1154
|
+
}
|
|
1155
|
+
];
|
|
1156
|
+
}
|
|
1157
|
+
return newConversation;
|
|
1158
|
+
};
|
|
1159
|
+
handleAgentConversation = (chunk, newConversation, metadata) => {
|
|
1160
|
+
if (chunk.type === "agent-execution-start") {
|
|
1161
|
+
const primitiveId = chunk.payload?.args?.primitiveId;
|
|
1162
|
+
const runId = chunk.payload.runId;
|
|
1163
|
+
if (!primitiveId || !runId) return newConversation;
|
|
1164
|
+
const newMessage = {
|
|
1165
|
+
id: `agent-execution-start-${runId}-${Date.now()}`,
|
|
1166
|
+
role: "assistant",
|
|
1167
|
+
parts: [
|
|
1168
|
+
{
|
|
1169
|
+
type: "dynamic-tool",
|
|
1170
|
+
toolName: primitiveId,
|
|
1171
|
+
toolCallId: runId,
|
|
1172
|
+
state: "input-available",
|
|
1173
|
+
input: chunk.payload.args
|
|
1174
|
+
}
|
|
1175
|
+
],
|
|
1176
|
+
metadata: {
|
|
1177
|
+
...metadata,
|
|
1178
|
+
selectionReason: chunk.payload?.args?.selectionReason || "",
|
|
1179
|
+
agentInput: chunk.payload?.args?.task,
|
|
1180
|
+
mode: "network",
|
|
1181
|
+
from: "AGENT"
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
return [...newConversation, newMessage];
|
|
1185
|
+
}
|
|
1186
|
+
if (chunk.type === "agent-execution-approval") {
|
|
1187
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1188
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1189
|
+
const lastRequireApprovalMetadata = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.requireApprovalMetadata : {};
|
|
1190
|
+
return [
|
|
1191
|
+
...newConversation.slice(0, -1),
|
|
1192
|
+
{
|
|
1193
|
+
...lastMessage,
|
|
1194
|
+
metadata: {
|
|
1195
|
+
...lastMessage.metadata,
|
|
1196
|
+
mode: "network",
|
|
1197
|
+
requireApprovalMetadata: {
|
|
1198
|
+
...lastRequireApprovalMetadata,
|
|
1199
|
+
[chunk.payload.toolName]: {
|
|
1200
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1201
|
+
toolName: chunk.payload.toolName,
|
|
1202
|
+
args: chunk.payload.args,
|
|
1203
|
+
runId: chunk.payload.runId
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
];
|
|
1209
|
+
}
|
|
1210
|
+
if (chunk.type === "agent-execution-suspended") {
|
|
1211
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1212
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1213
|
+
const lastSuspendedTools = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.suspendedTools : {};
|
|
1214
|
+
return [
|
|
1215
|
+
...newConversation.slice(0, -1),
|
|
1216
|
+
{
|
|
1217
|
+
...lastMessage,
|
|
1218
|
+
metadata: {
|
|
1219
|
+
...lastMessage.metadata,
|
|
1220
|
+
mode: "network",
|
|
1221
|
+
suspendedTools: {
|
|
1222
|
+
...lastSuspendedTools,
|
|
1223
|
+
[chunk.payload.toolName]: {
|
|
1224
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1225
|
+
toolName: chunk.payload.toolName,
|
|
1226
|
+
args: chunk.payload.args,
|
|
1227
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
];
|
|
1233
|
+
}
|
|
1234
|
+
if (chunk.type === "agent-execution-end") {
|
|
1235
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1236
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1237
|
+
const parts = [...lastMessage.parts];
|
|
1238
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
1239
|
+
if (toolPartIndex !== -1) {
|
|
1240
|
+
const toolPart = parts[toolPartIndex];
|
|
1241
|
+
if (toolPart.type === "dynamic-tool") {
|
|
1242
|
+
const currentOutput = toolPart.output;
|
|
1243
|
+
parts[toolPartIndex] = {
|
|
1244
|
+
type: "dynamic-tool",
|
|
1245
|
+
toolName: toolPart.toolName,
|
|
1246
|
+
toolCallId: toolPart.toolCallId,
|
|
1247
|
+
state: "output-available",
|
|
1248
|
+
input: toolPart.input,
|
|
1249
|
+
output: {
|
|
1250
|
+
...currentOutput,
|
|
1251
|
+
result: currentOutput?.result || chunk.payload?.result || ""
|
|
1252
|
+
}
|
|
1253
|
+
};
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
return [
|
|
1257
|
+
...newConversation.slice(0, -1),
|
|
1258
|
+
{
|
|
1259
|
+
...lastMessage,
|
|
1260
|
+
parts
|
|
1261
|
+
}
|
|
1262
|
+
];
|
|
1263
|
+
}
|
|
1264
|
+
if (chunk.type.startsWith("agent-execution-event-")) {
|
|
1265
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1266
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1267
|
+
const agentChunk = chunk.payload;
|
|
1268
|
+
const parts = [...lastMessage.parts];
|
|
1269
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
1270
|
+
if (toolPartIndex === -1) return newConversation;
|
|
1271
|
+
const toolPart = parts[toolPartIndex];
|
|
1272
|
+
if (agentChunk.type === "text-delta") {
|
|
1273
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
1274
|
+
const lastChildMessage = childMessages[childMessages.length - 1];
|
|
1275
|
+
const textMessage = { type: "text", content: (lastChildMessage?.content || "") + agentChunk.payload.text };
|
|
1276
|
+
const nextMessages = lastChildMessage?.type === "text" ? [...childMessages.slice(0, -1), textMessage] : [...childMessages, textMessage];
|
|
1277
|
+
parts[toolPartIndex] = {
|
|
1278
|
+
...toolPart,
|
|
1279
|
+
output: {
|
|
1280
|
+
childMessages: nextMessages
|
|
1281
|
+
}
|
|
1282
|
+
};
|
|
1283
|
+
} else if (agentChunk.type === "tool-call") {
|
|
1284
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
1285
|
+
parts[toolPartIndex] = {
|
|
1286
|
+
...toolPart,
|
|
1287
|
+
output: {
|
|
1288
|
+
...toolPart?.output,
|
|
1289
|
+
childMessages: [
|
|
1290
|
+
...childMessages,
|
|
1291
|
+
{
|
|
1292
|
+
type: "tool",
|
|
1293
|
+
toolCallId: agentChunk.payload.toolCallId,
|
|
1294
|
+
toolName: agentChunk.payload.toolName,
|
|
1295
|
+
args: agentChunk.payload.args
|
|
1296
|
+
}
|
|
1297
|
+
]
|
|
1298
|
+
}
|
|
1299
|
+
};
|
|
1300
|
+
} else if (agentChunk.type === "tool-output") {
|
|
1301
|
+
if (agentChunk.payload?.output?.type?.startsWith("workflow-")) {
|
|
1302
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
1303
|
+
const lastToolIndex = childMessages.length - 1;
|
|
1304
|
+
const currentMessage = childMessages[lastToolIndex];
|
|
1305
|
+
const actualExistingWorkflowState = currentMessage?.toolOutput || {};
|
|
1306
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
|
|
1307
|
+
actualExistingWorkflowState,
|
|
1308
|
+
agentChunk.payload.output
|
|
1309
|
+
);
|
|
1310
|
+
if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
|
|
1311
|
+
parts[toolPartIndex] = {
|
|
1312
|
+
...toolPart,
|
|
1313
|
+
output: {
|
|
1314
|
+
...toolPart?.output,
|
|
1315
|
+
childMessages: [
|
|
1316
|
+
...childMessages.slice(0, -1),
|
|
1317
|
+
{
|
|
1318
|
+
...currentMessage,
|
|
1319
|
+
toolOutput: updatedWorkflowState
|
|
1320
|
+
}
|
|
1321
|
+
]
|
|
1322
|
+
}
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
} else if (agentChunk.type === "tool-result") {
|
|
1327
|
+
const childMessages = toolPart?.output?.childMessages || [];
|
|
1328
|
+
const lastToolIndex = childMessages.length - 1;
|
|
1329
|
+
const isWorkflow = Boolean(agentChunk.payload?.result?.result?.steps);
|
|
1330
|
+
if (lastToolIndex >= 0 && childMessages[lastToolIndex]?.type === "tool") {
|
|
1331
|
+
parts[toolPartIndex] = {
|
|
1332
|
+
...toolPart,
|
|
1333
|
+
output: {
|
|
1334
|
+
...toolPart?.output,
|
|
1335
|
+
childMessages: [
|
|
1336
|
+
...childMessages.slice(0, -1),
|
|
1337
|
+
{
|
|
1338
|
+
...childMessages[lastToolIndex],
|
|
1339
|
+
toolOutput: isWorkflow ? agentChunk.payload.result.result : agentChunk.payload.result
|
|
1340
|
+
}
|
|
1341
|
+
]
|
|
1342
|
+
}
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
return [
|
|
1347
|
+
...newConversation.slice(0, -1),
|
|
1348
|
+
{
|
|
1349
|
+
...lastMessage,
|
|
1350
|
+
parts
|
|
1351
|
+
}
|
|
1352
|
+
];
|
|
1353
|
+
}
|
|
1354
|
+
return newConversation;
|
|
1355
|
+
};
|
|
1356
|
+
handleWorkflowConversation = (chunk, newConversation, metadata) => {
|
|
1357
|
+
if (chunk.type === "workflow-execution-start") {
|
|
1358
|
+
const primitiveId = chunk.payload?.args?.primitiveId;
|
|
1359
|
+
const runId = chunk.payload.runId;
|
|
1360
|
+
if (!primitiveId || !runId) return newConversation;
|
|
1361
|
+
let agentInput;
|
|
1362
|
+
try {
|
|
1363
|
+
agentInput = JSON.parse(chunk?.payload?.args?.prompt);
|
|
1364
|
+
} catch {
|
|
1365
|
+
agentInput = chunk?.payload?.args?.prompt;
|
|
1366
|
+
}
|
|
1367
|
+
const newMessage = {
|
|
1368
|
+
id: `workflow-start-${runId}-${Date.now()}`,
|
|
1369
|
+
role: "assistant",
|
|
1370
|
+
parts: [
|
|
1371
|
+
{
|
|
1372
|
+
type: "dynamic-tool",
|
|
1373
|
+
toolName: primitiveId,
|
|
1374
|
+
toolCallId: runId,
|
|
1375
|
+
state: "input-available",
|
|
1376
|
+
input: chunk.payload.args
|
|
1377
|
+
}
|
|
1378
|
+
],
|
|
1379
|
+
metadata: {
|
|
1380
|
+
...metadata,
|
|
1381
|
+
selectionReason: chunk.payload?.args?.selectionReason || "",
|
|
1382
|
+
from: "WORKFLOW",
|
|
1383
|
+
mode: "network",
|
|
1384
|
+
agentInput
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
return [...newConversation, newMessage];
|
|
1388
|
+
}
|
|
1389
|
+
if (chunk.type === "workflow-execution-suspended") {
|
|
1390
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1391
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1392
|
+
const lastSuspendedTools = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.suspendedTools : {};
|
|
1393
|
+
return [
|
|
1394
|
+
...newConversation.slice(0, -1),
|
|
1395
|
+
{
|
|
1396
|
+
...lastMessage,
|
|
1397
|
+
metadata: {
|
|
1398
|
+
...lastMessage.metadata,
|
|
1399
|
+
mode: "network",
|
|
1400
|
+
suspendedTools: {
|
|
1401
|
+
...lastSuspendedTools,
|
|
1402
|
+
[chunk.payload.toolName]: {
|
|
1403
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1404
|
+
toolName: chunk.payload.toolName,
|
|
1405
|
+
args: chunk.payload.args,
|
|
1406
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
];
|
|
1412
|
+
}
|
|
1413
|
+
if (chunk.type.startsWith("workflow-execution-event-")) {
|
|
1414
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1415
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1416
|
+
const parts = [...lastMessage.parts];
|
|
1417
|
+
const toolPartIndex = parts.findIndex((part) => part.type === "dynamic-tool");
|
|
1418
|
+
if (toolPartIndex === -1) return newConversation;
|
|
1419
|
+
const toolPart = parts[toolPartIndex];
|
|
1420
|
+
if (toolPart.type !== "dynamic-tool") return newConversation;
|
|
1421
|
+
const existingWorkflowState = toolPart.output || {};
|
|
1422
|
+
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(existingWorkflowState, chunk.payload);
|
|
1423
|
+
parts[toolPartIndex] = {
|
|
1424
|
+
...toolPart,
|
|
1425
|
+
output: updatedWorkflowState
|
|
1426
|
+
};
|
|
1427
|
+
return [
|
|
1428
|
+
...newConversation.slice(0, -1),
|
|
1429
|
+
{
|
|
1430
|
+
...lastMessage,
|
|
1431
|
+
parts
|
|
1432
|
+
}
|
|
1433
|
+
];
|
|
1434
|
+
}
|
|
1435
|
+
return newConversation;
|
|
1436
|
+
};
|
|
1437
|
+
handleToolConversation = (chunk, newConversation, metadata) => {
|
|
1438
|
+
if (chunk.type === "tool-execution-start") {
|
|
1439
|
+
const { args: argsData } = chunk.payload;
|
|
1440
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1441
|
+
const nestedArgs = argsData.args || {};
|
|
1442
|
+
if (!lastMessage || lastMessage.role !== "assistant") {
|
|
1443
|
+
const newMessage = {
|
|
1444
|
+
id: `tool-start-${chunk.runId}-${Date.now()}`,
|
|
1445
|
+
role: "assistant",
|
|
1446
|
+
parts: [
|
|
1447
|
+
{
|
|
1448
|
+
type: "dynamic-tool",
|
|
1449
|
+
toolName: argsData.toolName || "unknown",
|
|
1450
|
+
toolCallId: argsData.toolCallId || "unknown",
|
|
1451
|
+
state: "input-available",
|
|
1452
|
+
input: nestedArgs
|
|
1453
|
+
}
|
|
1454
|
+
],
|
|
1455
|
+
metadata: {
|
|
1456
|
+
...metadata,
|
|
1457
|
+
selectionReason: metadata?.mode === "network" ? metadata.selectionReason || argsData.selectionReason : "",
|
|
1458
|
+
mode: "network",
|
|
1459
|
+
agentInput: nestedArgs
|
|
1460
|
+
}
|
|
1461
|
+
};
|
|
1462
|
+
return [...newConversation, newMessage];
|
|
1463
|
+
}
|
|
1464
|
+
const parts = [...lastMessage.parts];
|
|
1465
|
+
parts.push({
|
|
1466
|
+
type: "dynamic-tool",
|
|
1467
|
+
toolName: argsData.toolName || "unknown",
|
|
1468
|
+
toolCallId: argsData.toolCallId || "unknown",
|
|
1469
|
+
state: "input-available",
|
|
1470
|
+
input: nestedArgs
|
|
1471
|
+
});
|
|
1472
|
+
return [
|
|
1473
|
+
...newConversation.slice(0, -1),
|
|
1474
|
+
{
|
|
1475
|
+
...lastMessage,
|
|
1476
|
+
parts
|
|
1477
|
+
}
|
|
1478
|
+
];
|
|
1479
|
+
}
|
|
1480
|
+
if (chunk.type === "tool-execution-approval") {
|
|
1481
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1482
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1483
|
+
const lastRequireApprovalMetadata = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.requireApprovalMetadata : {};
|
|
1484
|
+
return [
|
|
1485
|
+
...newConversation.slice(0, -1),
|
|
1486
|
+
{
|
|
1487
|
+
...lastMessage,
|
|
1488
|
+
metadata: {
|
|
1489
|
+
...lastMessage.metadata,
|
|
1490
|
+
mode: "network",
|
|
1491
|
+
requireApprovalMetadata: {
|
|
1492
|
+
...lastRequireApprovalMetadata,
|
|
1493
|
+
[chunk.payload.toolName]: {
|
|
1494
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1495
|
+
toolName: chunk.payload.toolName,
|
|
1496
|
+
args: chunk.payload.args,
|
|
1497
|
+
runId: chunk.payload.runId
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
];
|
|
1503
|
+
}
|
|
1504
|
+
if (chunk.type === "tool-execution-suspended") {
|
|
1505
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1506
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1507
|
+
const lastSuspendedTools = lastMessage.metadata?.mode === "network" ? lastMessage.metadata?.suspendedTools : {};
|
|
1508
|
+
return [
|
|
1509
|
+
...newConversation.slice(0, -1),
|
|
1510
|
+
{
|
|
1511
|
+
...lastMessage,
|
|
1512
|
+
metadata: {
|
|
1513
|
+
...lastMessage.metadata,
|
|
1514
|
+
mode: "network",
|
|
1515
|
+
suspendedTools: {
|
|
1516
|
+
...lastSuspendedTools,
|
|
1517
|
+
[chunk.payload.toolName]: {
|
|
1518
|
+
toolCallId: chunk.payload.toolCallId,
|
|
1519
|
+
toolName: chunk.payload.toolName,
|
|
1520
|
+
args: chunk.payload.args,
|
|
1521
|
+
suspendPayload: chunk.payload.suspendPayload
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
];
|
|
1527
|
+
}
|
|
1528
|
+
if (chunk.type === "tool-execution-end") {
|
|
1529
|
+
const lastMessage = newConversation[newConversation.length - 1];
|
|
1530
|
+
if (!lastMessage || lastMessage.role !== "assistant") return newConversation;
|
|
1531
|
+
const parts = [...lastMessage.parts];
|
|
1532
|
+
const toolPartIndex = parts.findIndex(
|
|
1533
|
+
(part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
1534
|
+
);
|
|
1535
|
+
if (toolPartIndex !== -1) {
|
|
1536
|
+
const toolPart = parts[toolPartIndex];
|
|
1537
|
+
if (toolPart.type === "dynamic-tool") {
|
|
1538
|
+
const currentOutput = toolPart.output;
|
|
1539
|
+
parts[toolPartIndex] = {
|
|
1540
|
+
type: "dynamic-tool",
|
|
1541
|
+
toolName: toolPart.toolName,
|
|
1542
|
+
toolCallId: toolPart.toolCallId,
|
|
1543
|
+
state: "output-available",
|
|
1544
|
+
input: toolPart.input,
|
|
1545
|
+
output: currentOutput?.result || chunk.payload?.result || ""
|
|
1546
|
+
};
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
return [
|
|
1550
|
+
...newConversation.slice(0, -1),
|
|
1551
|
+
{
|
|
1552
|
+
...lastMessage,
|
|
1553
|
+
parts
|
|
1554
|
+
}
|
|
1555
|
+
];
|
|
1556
|
+
}
|
|
1557
|
+
return newConversation;
|
|
1558
|
+
};
|
|
1559
|
+
};
|
|
1560
|
+
|
|
1561
|
+
// src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.tsx
|
|
1562
|
+
var fromCoreUserMessageToUIMessage = (coreUserMessage) => {
|
|
1563
|
+
const id = `user-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
1564
|
+
const parts = typeof coreUserMessage.content === "string" ? [
|
|
1565
|
+
{
|
|
1566
|
+
type: "text",
|
|
1567
|
+
text: coreUserMessage.content
|
|
1568
|
+
}
|
|
1569
|
+
] : coreUserMessage.content.map((part) => {
|
|
1570
|
+
switch (part.type) {
|
|
1571
|
+
case "text": {
|
|
1572
|
+
return {
|
|
1573
|
+
type: "text",
|
|
1574
|
+
text: part.text
|
|
1575
|
+
};
|
|
1576
|
+
}
|
|
1577
|
+
case "image": {
|
|
1578
|
+
const url = typeof part.image === "string" ? part.image : part.image instanceof URL ? part.image.toString() : "";
|
|
1579
|
+
return {
|
|
1580
|
+
type: "file",
|
|
1581
|
+
mediaType: part.mimeType ?? "image/*",
|
|
1582
|
+
url
|
|
1583
|
+
};
|
|
1584
|
+
}
|
|
1585
|
+
case "file": {
|
|
1586
|
+
const url = typeof part.data === "string" ? part.data : part.data instanceof URL ? part.data.toString() : "";
|
|
1587
|
+
return {
|
|
1588
|
+
type: "file",
|
|
1589
|
+
mediaType: part.mimeType,
|
|
1590
|
+
url,
|
|
1591
|
+
...part.filename !== void 0 ? { filename: part.filename } : {}
|
|
1592
|
+
};
|
|
1593
|
+
}
|
|
1594
|
+
default: {
|
|
1595
|
+
const exhaustiveCheck = part;
|
|
1596
|
+
throw new Error(`Unhandled content part type: ${exhaustiveCheck.type}`);
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
});
|
|
1600
|
+
return {
|
|
1601
|
+
id,
|
|
1602
|
+
role: "user",
|
|
1603
|
+
parts
|
|
1604
|
+
};
|
|
1605
|
+
};
|
|
1606
|
+
|
|
1607
|
+
// src/agent/hooks.ts
|
|
1608
|
+
var extractRunIdFromMessages = (messages) => {
|
|
1609
|
+
for (const message of messages) {
|
|
1610
|
+
const pendingToolApprovals = message.metadata?.pendingToolApprovals;
|
|
1611
|
+
if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
|
|
1612
|
+
const suspensionData = Object.values(pendingToolApprovals)[0];
|
|
1613
|
+
if (suspensionData?.runId) {
|
|
1614
|
+
return suspensionData.runId;
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
return void 0;
|
|
1619
|
+
};
|
|
1620
|
+
var useChat = ({ agentId, resourceId, initialMessages }) => {
|
|
1621
|
+
const _currentRunId = useRef(void 0);
|
|
1622
|
+
const _onChunk = useRef(void 0);
|
|
1623
|
+
const _networkRunId = useRef(void 0);
|
|
1624
|
+
const _onNetworkChunk = useRef(void 0);
|
|
1625
|
+
const [messages, setMessages] = useState([]);
|
|
1626
|
+
const [toolCallApprovals, setToolCallApprovals] = useState({});
|
|
1627
|
+
const [networkToolCallApprovals, setNetworkToolCallApprovals] = useState({});
|
|
1628
|
+
const baseClient = useMastraClient();
|
|
1629
|
+
const [isRunning, setIsRunning] = useState(false);
|
|
1630
|
+
useEffect(() => {
|
|
1631
|
+
const formattedMessages = resolveInitialMessages(initialMessages || []);
|
|
1632
|
+
setMessages(formattedMessages);
|
|
1633
|
+
_currentRunId.current = extractRunIdFromMessages(formattedMessages);
|
|
1634
|
+
}, [initialMessages]);
|
|
1635
|
+
const generate = async ({
|
|
1636
|
+
coreUserMessages,
|
|
1637
|
+
requestContext,
|
|
1638
|
+
threadId,
|
|
1639
|
+
modelSettings,
|
|
1640
|
+
signal,
|
|
1641
|
+
onFinish,
|
|
1642
|
+
tracingOptions
|
|
1643
|
+
}) => {
|
|
1644
|
+
const {
|
|
1645
|
+
frequencyPenalty,
|
|
1646
|
+
presencePenalty,
|
|
1647
|
+
maxRetries,
|
|
1648
|
+
maxTokens,
|
|
1649
|
+
temperature,
|
|
1650
|
+
topK,
|
|
1651
|
+
topP,
|
|
1652
|
+
instructions,
|
|
1653
|
+
providerOptions,
|
|
1654
|
+
maxSteps,
|
|
1655
|
+
requireToolApproval
|
|
1656
|
+
} = modelSettings || {};
|
|
1657
|
+
setIsRunning(true);
|
|
1658
|
+
const clientWithAbort = new MastraClient({
|
|
1659
|
+
...baseClient.options,
|
|
1660
|
+
abortSignal: signal
|
|
1661
|
+
});
|
|
1662
|
+
const agent = clientWithAbort.getAgent(agentId);
|
|
1663
|
+
const runId = v4();
|
|
1664
|
+
_currentRunId.current = runId;
|
|
1665
|
+
const response = await agent.generate(coreUserMessages, {
|
|
1666
|
+
runId,
|
|
1667
|
+
maxSteps,
|
|
1668
|
+
modelSettings: {
|
|
1669
|
+
frequencyPenalty,
|
|
1670
|
+
presencePenalty,
|
|
1671
|
+
maxRetries,
|
|
1672
|
+
maxOutputTokens: maxTokens,
|
|
1673
|
+
temperature,
|
|
1674
|
+
topK,
|
|
1675
|
+
topP
|
|
1676
|
+
},
|
|
1677
|
+
instructions,
|
|
1678
|
+
requestContext,
|
|
1679
|
+
...threadId ? { memory: { thread: threadId, resource: resourceId || agentId } } : {},
|
|
1680
|
+
providerOptions,
|
|
1681
|
+
tracingOptions,
|
|
1682
|
+
requireToolApproval
|
|
1683
|
+
});
|
|
1684
|
+
if (response.finishReason === "suspended" && response.suspendPayload) {
|
|
1685
|
+
const { toolCallId, toolName, args } = response.suspendPayload;
|
|
1686
|
+
if (response.response?.uiMessages) {
|
|
1687
|
+
const mastraUIMessages = (response.response.uiMessages || []).map((message) => ({
|
|
1688
|
+
...message,
|
|
1689
|
+
metadata: {
|
|
1690
|
+
mode: "generate",
|
|
1691
|
+
requireApprovalMetadata: {
|
|
1692
|
+
[toolName]: {
|
|
1693
|
+
toolCallId,
|
|
1694
|
+
toolName,
|
|
1695
|
+
args
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
}));
|
|
1700
|
+
setMessages((prev) => [...prev, ...mastraUIMessages]);
|
|
1701
|
+
}
|
|
1702
|
+
setIsRunning(false);
|
|
1703
|
+
return;
|
|
1704
|
+
}
|
|
1705
|
+
setIsRunning(false);
|
|
1706
|
+
if (response && "uiMessages" in response.response && response.response.uiMessages) {
|
|
1707
|
+
void onFinish?.(response.response.uiMessages);
|
|
1708
|
+
const mastraUIMessages = (response.response.uiMessages || []).map((message) => ({
|
|
1709
|
+
...message,
|
|
1710
|
+
metadata: {
|
|
1711
|
+
mode: "generate"
|
|
1712
|
+
}
|
|
1713
|
+
}));
|
|
1714
|
+
setMessages((prev) => [...prev, ...mastraUIMessages]);
|
|
1715
|
+
}
|
|
1716
|
+
};
|
|
1717
|
+
const stream = async ({
|
|
1718
|
+
coreUserMessages,
|
|
1719
|
+
requestContext,
|
|
1720
|
+
threadId,
|
|
1721
|
+
onChunk,
|
|
1722
|
+
modelSettings,
|
|
1723
|
+
signal,
|
|
1724
|
+
tracingOptions
|
|
1725
|
+
}) => {
|
|
1726
|
+
const {
|
|
1727
|
+
frequencyPenalty,
|
|
1728
|
+
presencePenalty,
|
|
1729
|
+
maxRetries,
|
|
1730
|
+
maxTokens,
|
|
1731
|
+
temperature,
|
|
1732
|
+
topK,
|
|
1733
|
+
topP,
|
|
1734
|
+
instructions,
|
|
1735
|
+
providerOptions,
|
|
1736
|
+
maxSteps,
|
|
1737
|
+
requireToolApproval
|
|
1738
|
+
} = modelSettings || {};
|
|
1739
|
+
setIsRunning(true);
|
|
1740
|
+
const clientWithAbort = new MastraClient({
|
|
1741
|
+
...baseClient.options,
|
|
1742
|
+
abortSignal: signal
|
|
1743
|
+
});
|
|
1744
|
+
const agent = clientWithAbort.getAgent(agentId);
|
|
1745
|
+
const runId = v4();
|
|
1746
|
+
const response = await agent.stream(coreUserMessages, {
|
|
1747
|
+
runId,
|
|
1748
|
+
maxSteps,
|
|
1749
|
+
modelSettings: {
|
|
1750
|
+
frequencyPenalty,
|
|
1751
|
+
presencePenalty,
|
|
1752
|
+
maxRetries,
|
|
1753
|
+
maxOutputTokens: maxTokens,
|
|
1754
|
+
temperature,
|
|
1755
|
+
topK,
|
|
1756
|
+
topP
|
|
1757
|
+
},
|
|
1758
|
+
instructions,
|
|
1759
|
+
requestContext,
|
|
1760
|
+
...threadId ? { memory: { thread: threadId, resource: resourceId || agentId } } : {},
|
|
1761
|
+
providerOptions,
|
|
1762
|
+
requireToolApproval,
|
|
1763
|
+
tracingOptions
|
|
1764
|
+
});
|
|
1765
|
+
_onChunk.current = onChunk;
|
|
1766
|
+
_currentRunId.current = runId;
|
|
1767
|
+
await response.processDataStream({
|
|
1768
|
+
onChunk: async (chunk) => {
|
|
1769
|
+
setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
|
|
1770
|
+
void onChunk?.(chunk);
|
|
1771
|
+
}
|
|
1772
|
+
});
|
|
1773
|
+
setIsRunning(false);
|
|
1774
|
+
};
|
|
1775
|
+
const network = async ({
|
|
1776
|
+
coreUserMessages,
|
|
1777
|
+
requestContext,
|
|
1778
|
+
threadId,
|
|
1779
|
+
onNetworkChunk,
|
|
1780
|
+
modelSettings,
|
|
1781
|
+
signal,
|
|
1782
|
+
tracingOptions
|
|
1783
|
+
}) => {
|
|
1784
|
+
const { frequencyPenalty, presencePenalty, maxRetries, maxTokens, temperature, topK, topP, maxSteps } = modelSettings || {};
|
|
1785
|
+
setIsRunning(true);
|
|
1786
|
+
const clientWithAbort = new MastraClient({
|
|
1787
|
+
...baseClient.options,
|
|
1788
|
+
abortSignal: signal
|
|
1789
|
+
});
|
|
1790
|
+
const agent = clientWithAbort.getAgent(agentId);
|
|
1791
|
+
const runId = v4();
|
|
1792
|
+
const response = await agent.network(coreUserMessages, {
|
|
1793
|
+
maxSteps,
|
|
1794
|
+
modelSettings: {
|
|
1795
|
+
frequencyPenalty,
|
|
1796
|
+
presencePenalty,
|
|
1797
|
+
maxRetries,
|
|
1798
|
+
maxOutputTokens: maxTokens,
|
|
1799
|
+
temperature,
|
|
1800
|
+
topK,
|
|
1801
|
+
topP
|
|
1802
|
+
},
|
|
1803
|
+
runId,
|
|
1804
|
+
requestContext,
|
|
1805
|
+
...threadId ? { memory: { thread: threadId, resource: resourceId || agentId } } : {},
|
|
1806
|
+
tracingOptions
|
|
1807
|
+
});
|
|
1808
|
+
_onNetworkChunk.current = onNetworkChunk;
|
|
1809
|
+
_networkRunId.current = runId;
|
|
1810
|
+
const transformer = new AISdkNetworkTransformer();
|
|
1811
|
+
await response.processDataStream({
|
|
1812
|
+
onChunk: async (chunk) => {
|
|
1813
|
+
setMessages((prev) => transformer.transform({ chunk, conversation: prev, metadata: { mode: "network" } }));
|
|
1814
|
+
void onNetworkChunk?.(chunk);
|
|
1815
|
+
}
|
|
1816
|
+
});
|
|
1817
|
+
setIsRunning(false);
|
|
1818
|
+
};
|
|
1819
|
+
const handleCancelRun = () => {
|
|
1820
|
+
setIsRunning(false);
|
|
1821
|
+
_currentRunId.current = void 0;
|
|
1822
|
+
_onChunk.current = void 0;
|
|
1823
|
+
_networkRunId.current = void 0;
|
|
1824
|
+
_onNetworkChunk.current = void 0;
|
|
1825
|
+
};
|
|
1826
|
+
const approveToolCall = async (toolCallId) => {
|
|
1827
|
+
const onChunk = _onChunk.current;
|
|
1828
|
+
const currentRunId = _currentRunId.current;
|
|
1829
|
+
if (!currentRunId)
|
|
1830
|
+
return console.info("[approveToolCall] approveToolCall can only be called after a stream has started");
|
|
1831
|
+
setIsRunning(true);
|
|
1832
|
+
setToolCallApprovals((prev) => ({ ...prev, [toolCallId]: { status: "approved" } }));
|
|
1833
|
+
const agent = baseClient.getAgent(agentId);
|
|
1834
|
+
const response = await agent.approveToolCall({ runId: currentRunId, toolCallId });
|
|
1835
|
+
await response.processDataStream({
|
|
1836
|
+
onChunk: async (chunk) => {
|
|
1837
|
+
setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
|
|
1838
|
+
void onChunk?.(chunk);
|
|
1839
|
+
}
|
|
1840
|
+
});
|
|
1841
|
+
setIsRunning(false);
|
|
1842
|
+
};
|
|
1843
|
+
const declineToolCall = async (toolCallId) => {
|
|
1844
|
+
const onChunk = _onChunk.current;
|
|
1845
|
+
const currentRunId = _currentRunId.current;
|
|
1846
|
+
if (!currentRunId)
|
|
1847
|
+
return console.info("[declineToolCall] declineToolCall can only be called after a stream has started");
|
|
1848
|
+
setIsRunning(true);
|
|
1849
|
+
setToolCallApprovals((prev) => ({ ...prev, [toolCallId]: { status: "declined" } }));
|
|
1850
|
+
const agent = baseClient.getAgent(agentId);
|
|
1851
|
+
const response = await agent.declineToolCall({ runId: currentRunId, toolCallId });
|
|
1852
|
+
await response.processDataStream({
|
|
1853
|
+
onChunk: async (chunk) => {
|
|
1854
|
+
setMessages((prev) => toUIMessage({ chunk, conversation: prev, metadata: { mode: "stream" } }));
|
|
1855
|
+
void onChunk?.(chunk);
|
|
1856
|
+
}
|
|
1857
|
+
});
|
|
1858
|
+
setIsRunning(false);
|
|
1859
|
+
};
|
|
1860
|
+
const approveToolCallGenerate = async (toolCallId) => {
|
|
1861
|
+
const currentRunId = _currentRunId.current;
|
|
1862
|
+
if (!currentRunId)
|
|
1863
|
+
return console.info(
|
|
1864
|
+
"[approveToolCallGenerate] approveToolCallGenerate can only be called after a generate has started"
|
|
1865
|
+
);
|
|
1866
|
+
setIsRunning(true);
|
|
1867
|
+
setToolCallApprovals((prev) => ({ ...prev, [toolCallId]: { status: "approved" } }));
|
|
1868
|
+
const agent = baseClient.getAgent(agentId);
|
|
1869
|
+
const response = await agent.approveToolCallGenerate({ runId: currentRunId, toolCallId });
|
|
1870
|
+
if (response && "uiMessages" in response.response && response.response.uiMessages) {
|
|
1871
|
+
const mastraUIMessages = (response.response.uiMessages || []).map((message) => ({
|
|
1872
|
+
...message,
|
|
1873
|
+
metadata: {
|
|
1874
|
+
mode: "generate"
|
|
1875
|
+
}
|
|
1876
|
+
}));
|
|
1877
|
+
setMessages((prev) => [...prev, ...mastraUIMessages]);
|
|
1878
|
+
}
|
|
1879
|
+
setIsRunning(false);
|
|
1880
|
+
};
|
|
1881
|
+
const declineToolCallGenerate = async (toolCallId) => {
|
|
1882
|
+
const currentRunId = _currentRunId.current;
|
|
1883
|
+
if (!currentRunId)
|
|
1884
|
+
return console.info(
|
|
1885
|
+
"[declineToolCallGenerate] declineToolCallGenerate can only be called after a generate has started"
|
|
1886
|
+
);
|
|
1887
|
+
setIsRunning(true);
|
|
1888
|
+
setToolCallApprovals((prev) => ({ ...prev, [toolCallId]: { status: "declined" } }));
|
|
1889
|
+
const agent = baseClient.getAgent(agentId);
|
|
1890
|
+
const response = await agent.declineToolCallGenerate({ runId: currentRunId, toolCallId });
|
|
1891
|
+
if (response && "uiMessages" in response.response && response.response.uiMessages) {
|
|
1892
|
+
const mastraUIMessages = (response.response.uiMessages || []).map((message) => ({
|
|
1893
|
+
...message,
|
|
1894
|
+
metadata: {
|
|
1895
|
+
mode: "generate"
|
|
1896
|
+
}
|
|
1897
|
+
}));
|
|
1898
|
+
setMessages((prev) => [...prev, ...mastraUIMessages]);
|
|
1899
|
+
}
|
|
1900
|
+
setIsRunning(false);
|
|
1901
|
+
};
|
|
1902
|
+
const approveNetworkToolCall = async (toolName, runId) => {
|
|
1903
|
+
const onNetworkChunk = _onNetworkChunk.current;
|
|
1904
|
+
const networkRunId = runId || _networkRunId.current;
|
|
1905
|
+
if (!networkRunId)
|
|
1906
|
+
return console.info(
|
|
1907
|
+
"[approveNetworkToolCall] approveNetworkToolCall can only be called after a network stream has started"
|
|
1908
|
+
);
|
|
1909
|
+
setIsRunning(true);
|
|
1910
|
+
setNetworkToolCallApprovals((prev) => ({
|
|
1911
|
+
...prev,
|
|
1912
|
+
[runId ? `${runId}-${toolName}` : toolName]: { status: "approved" }
|
|
1913
|
+
}));
|
|
1914
|
+
const agent = baseClient.getAgent(agentId);
|
|
1915
|
+
const response = await agent.approveNetworkToolCall({ runId: networkRunId });
|
|
1916
|
+
const transformer = new AISdkNetworkTransformer();
|
|
1917
|
+
await response.processDataStream({
|
|
1918
|
+
onChunk: async (chunk) => {
|
|
1919
|
+
setMessages((prev) => transformer.transform({ chunk, conversation: prev, metadata: { mode: "network" } }));
|
|
1920
|
+
void onNetworkChunk?.(chunk);
|
|
1921
|
+
}
|
|
1922
|
+
});
|
|
1923
|
+
setIsRunning(false);
|
|
1924
|
+
};
|
|
1925
|
+
const declineNetworkToolCall = async (toolName, runId) => {
|
|
1926
|
+
const onNetworkChunk = _onNetworkChunk.current;
|
|
1927
|
+
const networkRunId = runId || _networkRunId.current;
|
|
1928
|
+
if (!networkRunId)
|
|
1929
|
+
return console.info(
|
|
1930
|
+
"[declineNetworkToolCall] declineNetworkToolCall can only be called after a network stream has started"
|
|
1931
|
+
);
|
|
1932
|
+
setIsRunning(true);
|
|
1933
|
+
setNetworkToolCallApprovals((prev) => ({
|
|
1934
|
+
...prev,
|
|
1935
|
+
[runId ? `${runId}-${toolName}` : toolName]: { status: "declined" }
|
|
1936
|
+
}));
|
|
1937
|
+
const agent = baseClient.getAgent(agentId);
|
|
1938
|
+
const response = await agent.declineNetworkToolCall({ runId: networkRunId });
|
|
1939
|
+
const transformer = new AISdkNetworkTransformer();
|
|
1940
|
+
await response.processDataStream({
|
|
1941
|
+
onChunk: async (chunk) => {
|
|
1942
|
+
setMessages((prev) => transformer.transform({ chunk, conversation: prev, metadata: { mode: "network" } }));
|
|
1943
|
+
void onNetworkChunk?.(chunk);
|
|
1944
|
+
}
|
|
1945
|
+
});
|
|
1946
|
+
setIsRunning(false);
|
|
1947
|
+
};
|
|
1948
|
+
const sendMessage = async ({ mode = "stream", ...args }) => {
|
|
1949
|
+
const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
|
|
1950
|
+
const coreUserMessages = [nextMessage];
|
|
1951
|
+
if (args.coreUserMessages) {
|
|
1952
|
+
coreUserMessages.push(...args.coreUserMessages);
|
|
1953
|
+
}
|
|
1954
|
+
const uiMessages = coreUserMessages.map(fromCoreUserMessageToUIMessage);
|
|
1955
|
+
setMessages((s) => [...s, ...uiMessages]);
|
|
1956
|
+
if (mode === "generate") {
|
|
1957
|
+
await generate({ ...args, coreUserMessages });
|
|
1958
|
+
} else if (mode === "stream") {
|
|
1959
|
+
await stream({ ...args, coreUserMessages });
|
|
1960
|
+
} else if (mode === "network") {
|
|
1961
|
+
await network({ ...args, coreUserMessages });
|
|
1962
|
+
}
|
|
1963
|
+
};
|
|
1964
|
+
return {
|
|
1965
|
+
setMessages,
|
|
1966
|
+
sendMessage,
|
|
1967
|
+
isRunning,
|
|
1968
|
+
messages,
|
|
1969
|
+
approveToolCall,
|
|
1970
|
+
declineToolCall,
|
|
1971
|
+
approveToolCallGenerate,
|
|
1972
|
+
declineToolCallGenerate,
|
|
1973
|
+
cancelRun: handleCancelRun,
|
|
1974
|
+
toolCallApprovals,
|
|
1975
|
+
approveNetworkToolCall,
|
|
1976
|
+
declineNetworkToolCall,
|
|
1977
|
+
networkToolCallApprovals
|
|
1978
|
+
};
|
|
1979
|
+
};
|
|
1980
|
+
var IconSizes = {
|
|
1981
|
+
sm: "mastra:[&>svg]:size-3",
|
|
1982
|
+
md: "mastra:[&>svg]:size-4",
|
|
1983
|
+
lg: "mastra:[&>svg]:size-5"
|
|
1984
|
+
};
|
|
1985
|
+
var Icon = ({ children, className, size = "md", ...props }) => {
|
|
1986
|
+
return /* @__PURE__ */ jsx("div", { className: className || IconSizes[size], ...props, children });
|
|
1987
|
+
};
|
|
1988
|
+
var EntityContext = createContext({
|
|
1989
|
+
expanded: false,
|
|
1990
|
+
setExpanded: () => {
|
|
1991
|
+
},
|
|
1992
|
+
variant: "initial",
|
|
1993
|
+
disabled: false
|
|
1994
|
+
});
|
|
1995
|
+
var EntityProvider = EntityContext.Provider;
|
|
1996
|
+
var useEntity = () => useContext(EntityContext);
|
|
1997
|
+
var Entity = ({
|
|
1998
|
+
className,
|
|
1999
|
+
variant = "initial",
|
|
2000
|
+
initialExpanded = false,
|
|
2001
|
+
disabled = false,
|
|
2002
|
+
...props
|
|
2003
|
+
}) => {
|
|
2004
|
+
const [expanded, setExpanded] = useState(initialExpanded);
|
|
2005
|
+
return /* @__PURE__ */ jsx(EntityProvider, { value: { expanded, setExpanded, variant, disabled }, children: /* @__PURE__ */ jsx("div", { className, ...props }) });
|
|
2006
|
+
};
|
|
2007
|
+
var EntityTriggerClass = twMerge(
|
|
2008
|
+
"mastra:aria-disabled:cursor-not-allowed mastra:aria-disabled:bg-surface5 mastra:aria-disabled:text-text3",
|
|
2009
|
+
"mastra:aria-expanded:rounded-b-none mastra:aria-expanded:border-b-0",
|
|
2010
|
+
"mastra:bg-surface3 mastra:text-text6 mastra:hover:bg-surface4 mastra:active:bg-surface5",
|
|
2011
|
+
"mastra:rounded-lg mastra:py-2 mastra:px-4 mastra:border mastra:border-border1",
|
|
2012
|
+
"mastra:cursor-pointer mastra:inline-flex mastra:items-center mastra:gap-1 mastra:font-mono"
|
|
2013
|
+
);
|
|
2014
|
+
var EntityTriggerVariantClasses = {
|
|
2015
|
+
agent: "mastra:[&_svg.mastra-icon]:text-accent1",
|
|
2016
|
+
workflow: "mastra:[&_svg.mastra-icon]:text-accent3",
|
|
2017
|
+
tool: "mastra:[&_svg.mastra-icon]:text-accent6",
|
|
2018
|
+
memory: "mastra:[&_svg.mastra-icon]:text-accent2",
|
|
2019
|
+
initial: "mastra:[&_svg.mastra-icon]:text-text3"
|
|
2020
|
+
};
|
|
2021
|
+
var EntityTrigger = ({ className, children, ...props }) => {
|
|
2022
|
+
const { expanded, setExpanded, variant, disabled } = useEntity();
|
|
2023
|
+
const handleClick = (e) => {
|
|
2024
|
+
if (disabled) return;
|
|
2025
|
+
setExpanded(!expanded);
|
|
2026
|
+
props?.onClick?.(e);
|
|
2027
|
+
};
|
|
2028
|
+
return /* @__PURE__ */ jsx(
|
|
2029
|
+
"button",
|
|
2030
|
+
{
|
|
2031
|
+
className: className || twMerge(EntityTriggerClass, !disabled && EntityTriggerVariantClasses[variant]),
|
|
2032
|
+
...props,
|
|
2033
|
+
onClick: handleClick,
|
|
2034
|
+
"aria-expanded": expanded,
|
|
2035
|
+
"aria-disabled": disabled,
|
|
2036
|
+
children
|
|
2037
|
+
}
|
|
2038
|
+
);
|
|
2039
|
+
};
|
|
2040
|
+
var EntityContentClass = twMerge(
|
|
2041
|
+
"mastra:space-y-4",
|
|
2042
|
+
"mastra:rounded-lg mastra:rounded-tl-none mastra:p-4 mastra:border mastra:border-border1 mastra:-mt-[0.5px]",
|
|
2043
|
+
"mastra:bg-surface3 mastra:text-text6"
|
|
2044
|
+
);
|
|
2045
|
+
var EntityContent = ({ className, ...props }) => {
|
|
2046
|
+
const { expanded } = useEntity();
|
|
2047
|
+
if (!expanded) return null;
|
|
2048
|
+
return /* @__PURE__ */ jsx("div", { className: className || EntityContentClass, ...props });
|
|
2049
|
+
};
|
|
2050
|
+
var EntityCaret = ({ className, ...props }) => {
|
|
2051
|
+
const { expanded } = useEntity();
|
|
2052
|
+
return /* @__PURE__ */ jsx(Icon, { children: /* @__PURE__ */ jsx(
|
|
2053
|
+
ChevronDownIcon,
|
|
2054
|
+
{
|
|
2055
|
+
className: twMerge(
|
|
2056
|
+
`mastra:text-text3 mastra:transition-transform mastra:duration-200 mastra:ease-in-out`,
|
|
2057
|
+
expanded ? "mastra:rotate-0" : "mastra:-rotate-90",
|
|
2058
|
+
className
|
|
2059
|
+
),
|
|
2060
|
+
...props
|
|
2061
|
+
}
|
|
2062
|
+
) });
|
|
2063
|
+
};
|
|
2064
|
+
var ToolApprovalClass = twMerge(
|
|
2065
|
+
"mastra:rounded-lg mastra:border mastra:border-border1 mastra:max-w-1/2 mastra:mt-2",
|
|
2066
|
+
"mastra:bg-surface3 mastra:text-text6"
|
|
2067
|
+
);
|
|
2068
|
+
var ToolApproval = ({ className, ...props }) => {
|
|
2069
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalClass, ...props });
|
|
2070
|
+
};
|
|
2071
|
+
var ToolApprovalTitleClass = twMerge("mastra:text-text6 mastra:inline-flex mastra:items-center mastra:gap-1");
|
|
2072
|
+
var ToolApprovalTitle = ({ className, ...props }) => {
|
|
2073
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalTitleClass, ...props });
|
|
2074
|
+
};
|
|
2075
|
+
var ToolApprovalHeaderClass = twMerge(
|
|
2076
|
+
"mastra:flex mastra:justify-between mastra:items-center mastra:gap-2",
|
|
2077
|
+
"mastra:border-b mastra:border-border1 mastra:px-4 mastra:py-2"
|
|
2078
|
+
);
|
|
2079
|
+
var ToolApprovalHeader = ({ className, ...props }) => {
|
|
2080
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalHeaderClass, ...props });
|
|
2081
|
+
};
|
|
2082
|
+
var ToolApprovalContentClass = twMerge("mastra:text-text6 mastra:p-4");
|
|
2083
|
+
var ToolApprovalContent = ({ className, ...props }) => {
|
|
2084
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalContentClass, ...props });
|
|
2085
|
+
};
|
|
2086
|
+
var ToolApprovalActionsClass = twMerge("mastra:flex mastra:gap-2 mastra:items-center");
|
|
2087
|
+
var ToolApprovalActions = ({ className, ...props }) => {
|
|
2088
|
+
return /* @__PURE__ */ jsx("div", { className: className || ToolApprovalActionsClass, ...props });
|
|
2089
|
+
};
|
|
2090
|
+
var EntryClass = "mastra:space-y-2";
|
|
2091
|
+
var Entry = ({ className, ...props }) => {
|
|
2092
|
+
return /* @__PURE__ */ jsx("div", { className: className || EntryClass, ...props });
|
|
2093
|
+
};
|
|
2094
|
+
var EntryTitleClass = "mastra:font-mono mastra:text-sm mastra:text-text3";
|
|
2095
|
+
var EntryTitle = ({ className, as: Root = "h3", ...props }) => {
|
|
2096
|
+
return /* @__PURE__ */ jsx(Root, { className: className || EntryTitleClass, ...props });
|
|
2097
|
+
};
|
|
2098
|
+
var Tooltip = ({ children }) => {
|
|
2099
|
+
return /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsx(Root, { children }) });
|
|
2100
|
+
};
|
|
2101
|
+
var 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";
|
|
2102
|
+
var TooltipContent = ({ children, className, ...props }) => {
|
|
2103
|
+
return /* @__PURE__ */ jsx(TooltipPortal, { children: /* @__PURE__ */ jsx(TooltipContent$1, { className: className || TooltipContentClass, ...props, children }) });
|
|
2104
|
+
};
|
|
2105
|
+
var TooltipTrigger = (props) => {
|
|
2106
|
+
return /* @__PURE__ */ jsx(TooltipTrigger$1, { ...props, asChild: true });
|
|
2107
|
+
};
|
|
2108
|
+
var 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";
|
|
2109
|
+
var IconButton = ({ children, tooltip, size = "md", className, ...props }) => {
|
|
2110
|
+
return /* @__PURE__ */ jsxs(Tooltip, { children: [
|
|
2111
|
+
/* @__PURE__ */ jsx(TooltipTrigger, { children: /* @__PURE__ */ jsx(
|
|
2112
|
+
"button",
|
|
2113
|
+
{
|
|
2114
|
+
...props,
|
|
2115
|
+
className: className || twMerge(IconButtonClass, size === "md" && "mastra:p-0.5", size === "lg" && "mastra:p-1"),
|
|
2116
|
+
children: /* @__PURE__ */ jsx(Icon, { size, children })
|
|
2117
|
+
}
|
|
2118
|
+
) }),
|
|
2119
|
+
/* @__PURE__ */ jsx(TooltipContent, { children: tooltip })
|
|
2120
|
+
] });
|
|
2121
|
+
};
|
|
2122
|
+
async function highlight(code, lang) {
|
|
2123
|
+
const out = await codeToHast(code, {
|
|
2124
|
+
lang,
|
|
2125
|
+
theme: "dracula-soft"
|
|
2126
|
+
});
|
|
2127
|
+
return toJsxRuntime(out, {
|
|
2128
|
+
Fragment,
|
|
2129
|
+
jsx: jsx,
|
|
2130
|
+
jsxs: jsxs
|
|
2131
|
+
});
|
|
2132
|
+
}
|
|
2133
|
+
var CodeBlockClass = "mastra:rounded-lg mastra:[&>pre]:p-4 mastra:overflow-hidden mastra:[&>pre]:!bg-surface4 mastra:[&>pre>code]:leading-5 mastra:relative";
|
|
2134
|
+
var CodeBlock = ({ code, language, className, cta }) => {
|
|
2135
|
+
const [nodes, setNodes] = useState(null);
|
|
2136
|
+
useLayoutEffect(() => {
|
|
2137
|
+
void highlight(code, language).then(setNodes);
|
|
2138
|
+
}, [language]);
|
|
2139
|
+
return /* @__PURE__ */ jsxs("div", { className: className || CodeBlockClass, children: [
|
|
2140
|
+
nodes ?? null,
|
|
2141
|
+
cta
|
|
2142
|
+
] });
|
|
2143
|
+
};
|
|
2144
|
+
var CodeCopyButton = ({ code }) => {
|
|
2145
|
+
const [isCopied, setIsCopied] = useState(false);
|
|
2146
|
+
const handleCopy = () => {
|
|
2147
|
+
void navigator.clipboard.writeText(code);
|
|
2148
|
+
setIsCopied(true);
|
|
2149
|
+
setTimeout(() => setIsCopied(false), 2e3);
|
|
2150
|
+
};
|
|
2151
|
+
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, {}) }) });
|
|
2152
|
+
};
|
|
2153
|
+
var AgentIcon = ({ className, ...props }) => /* @__PURE__ */ jsxs(
|
|
2154
|
+
"svg",
|
|
2155
|
+
{
|
|
2156
|
+
width: "17",
|
|
2157
|
+
height: "16",
|
|
2158
|
+
viewBox: "0 0 17 16",
|
|
2159
|
+
fill: "none",
|
|
2160
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
2161
|
+
...props,
|
|
2162
|
+
className: twMerge("mastra-icon", className),
|
|
2163
|
+
children: [
|
|
2164
|
+
/* @__PURE__ */ jsx(
|
|
2165
|
+
"path",
|
|
2166
|
+
{
|
|
2167
|
+
fillRule: "evenodd",
|
|
2168
|
+
clipRule: "evenodd",
|
|
2169
|
+
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",
|
|
2170
|
+
fill: "currentColor"
|
|
2171
|
+
}
|
|
2172
|
+
),
|
|
2173
|
+
/* @__PURE__ */ jsx(
|
|
2174
|
+
"path",
|
|
2175
|
+
{
|
|
2176
|
+
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",
|
|
2177
|
+
fill: "currentColor"
|
|
2178
|
+
}
|
|
2179
|
+
)
|
|
2180
|
+
]
|
|
2181
|
+
}
|
|
2182
|
+
);
|
|
2183
|
+
var ToolsIcon = ({ className, ...props }) => /* @__PURE__ */ jsx(
|
|
2184
|
+
"svg",
|
|
2185
|
+
{
|
|
2186
|
+
width: "17",
|
|
2187
|
+
height: "16",
|
|
2188
|
+
viewBox: "0 0 17 16",
|
|
2189
|
+
fill: "none",
|
|
2190
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
2191
|
+
...props,
|
|
2192
|
+
className: twMerge("mastra-icon", className),
|
|
2193
|
+
children: /* @__PURE__ */ jsx(
|
|
2194
|
+
"path",
|
|
2195
|
+
{
|
|
2196
|
+
fillRule: "evenodd",
|
|
2197
|
+
clipRule: "evenodd",
|
|
2198
|
+
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",
|
|
2199
|
+
fill: "currentColor"
|
|
2200
|
+
}
|
|
2201
|
+
)
|
|
2202
|
+
}
|
|
2203
|
+
);
|
|
2204
|
+
var WorkflowIcon = ({ className, ...props }) => /* @__PURE__ */ jsx(
|
|
2205
|
+
"svg",
|
|
2206
|
+
{
|
|
2207
|
+
width: "17",
|
|
2208
|
+
height: "16",
|
|
2209
|
+
viewBox: "0 0 17 16",
|
|
2210
|
+
fill: "none",
|
|
2211
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
2212
|
+
...props,
|
|
2213
|
+
className: twMerge("mastra-icon", className),
|
|
2214
|
+
children: /* @__PURE__ */ jsx(
|
|
2215
|
+
"path",
|
|
2216
|
+
{
|
|
2217
|
+
fillRule: "evenodd",
|
|
2218
|
+
clipRule: "evenodd",
|
|
2219
|
+
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",
|
|
2220
|
+
fill: "currentColor"
|
|
2221
|
+
}
|
|
2222
|
+
)
|
|
2223
|
+
}
|
|
2224
|
+
);
|
|
2225
|
+
var MessageClass = "mastra:flex mastra:flex-col mastra:w-full mastra:py-4 mastra:gap-2 mastra:group";
|
|
2226
|
+
var Message = ({ position, className, children, ...props }) => {
|
|
2227
|
+
return /* @__PURE__ */ jsx(
|
|
2228
|
+
"div",
|
|
2229
|
+
{
|
|
2230
|
+
className: className || twMerge(
|
|
2231
|
+
MessageClass,
|
|
2232
|
+
position === "left" ? "" : "mastra:items-end mastra:[&_.mastra-message-content]:bg-surface4 mastra:[&_.mastra-message-content]:px-4"
|
|
2233
|
+
),
|
|
2234
|
+
...props,
|
|
2235
|
+
children
|
|
2236
|
+
}
|
|
2237
|
+
);
|
|
2238
|
+
};
|
|
2239
|
+
var MessageContentClass = "mastra:max-w-4/5 mastra:py-2 mastra:text-text6 mastra:rounded-lg mastra-message-content mastra:text-md";
|
|
2240
|
+
var MessageContent = ({ children, className, isStreaming, ...props }) => {
|
|
2241
|
+
return /* @__PURE__ */ jsxs("div", { className: className || MessageContentClass, ...props, children: [
|
|
2242
|
+
children,
|
|
2243
|
+
isStreaming && /* @__PURE__ */ jsx(MessageStreaming, {})
|
|
2244
|
+
] });
|
|
2245
|
+
};
|
|
2246
|
+
var MessageActionsClass = "mastra:gap-2 mastra:flex mastra:opacity-0 mastra:group-hover:opacity-100 mastra:group-focus-within:opacity-100 mastra:items-center";
|
|
2247
|
+
var MessageActions = ({ children, className, ...props }) => {
|
|
2248
|
+
return /* @__PURE__ */ jsx("div", { className: className || MessageActionsClass, ...props, children });
|
|
2249
|
+
};
|
|
2250
|
+
var MessageUsagesClass = "mastra:flex mastra:gap-2 mastra:items-center";
|
|
2251
|
+
var MessageUsages = ({ children, className, ...props }) => {
|
|
2252
|
+
return /* @__PURE__ */ jsx("div", { className: className || MessageUsagesClass, ...props, children });
|
|
2253
|
+
};
|
|
2254
|
+
var 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";
|
|
2255
|
+
var MessageUsage = ({ children, className, ...props }) => {
|
|
2256
|
+
return /* @__PURE__ */ jsx("dl", { className: className || MessageUsageClass, ...props, children });
|
|
2257
|
+
};
|
|
2258
|
+
var MessageUsageEntryClass = "mastra:text-text3 mastra:text-xs mastra:flex mastra:gap-1 mastra:items-center";
|
|
2259
|
+
var MessageUsageEntry = ({ children, className, ...props }) => {
|
|
2260
|
+
return /* @__PURE__ */ jsx("dt", { className: className || MessageUsageEntryClass, ...props, children });
|
|
2261
|
+
};
|
|
2262
|
+
var MessageUsageValueClass = "mastra:text-text6 mastra:text-xs";
|
|
2263
|
+
var MessageUsageValue = ({ children, className, ...props }) => {
|
|
2264
|
+
return /* @__PURE__ */ jsx("dd", { className: className || MessageUsageValueClass, ...props, children });
|
|
2265
|
+
};
|
|
2266
|
+
var MessageListClass = "mastra:overflow-y-auto mastra:h-full mastra-list";
|
|
2267
|
+
var MessageList = ({ children, className, ...props }) => {
|
|
2268
|
+
const listRef = useRef(null);
|
|
2269
|
+
useEffect(() => {
|
|
2270
|
+
const scrollToBottom = () => {
|
|
2271
|
+
if (!listRef.current) return;
|
|
2272
|
+
listRef.current.scrollTo({ top: listRef.current.scrollHeight, behavior: "smooth" });
|
|
2273
|
+
};
|
|
2274
|
+
requestAnimationFrame(scrollToBottom);
|
|
2275
|
+
});
|
|
2276
|
+
return /* @__PURE__ */ jsx("div", { className: className || MessageListClass, ...props, ref: listRef, children });
|
|
2277
|
+
};
|
|
2278
|
+
var MessageStreamingClass = "mastra:inline-block mastra:w-[2px] mastra:h-[1em] mastra:bg-text5 mastra:ml-0.5 mastra:align-text-bottom mastra:animate-pulse";
|
|
2279
|
+
var MessageStreaming = ({ className, ...props }) => {
|
|
2280
|
+
return /* @__PURE__ */ jsx("span", { className: className || MessageStreamingClass, ...props });
|
|
2281
|
+
};
|
|
2282
|
+
function useMutation(mutationFn) {
|
|
2283
|
+
const [isPending, setIsPending] = useState(false);
|
|
2284
|
+
const [isSuccess, setIsSuccess] = useState(false);
|
|
2285
|
+
const [isError, setIsError] = useState(false);
|
|
2286
|
+
const [error, setError] = useState(null);
|
|
2287
|
+
const [data, setData] = useState(void 0);
|
|
2288
|
+
const mutationFnRef = useRef(mutationFn);
|
|
2289
|
+
mutationFnRef.current = mutationFn;
|
|
2290
|
+
const reset = useCallback(() => {
|
|
2291
|
+
setIsPending(false);
|
|
2292
|
+
setIsSuccess(false);
|
|
2293
|
+
setIsError(false);
|
|
2294
|
+
setError(null);
|
|
2295
|
+
setData(void 0);
|
|
2296
|
+
}, []);
|
|
2297
|
+
const mutateAsync = useCallback(async (variables) => {
|
|
2298
|
+
setIsPending(true);
|
|
2299
|
+
setIsSuccess(false);
|
|
2300
|
+
setIsError(false);
|
|
2301
|
+
setError(null);
|
|
2302
|
+
try {
|
|
2303
|
+
const result = await mutationFnRef.current(variables);
|
|
2304
|
+
setData(result);
|
|
2305
|
+
setIsSuccess(true);
|
|
2306
|
+
return result;
|
|
2307
|
+
} catch (err) {
|
|
2308
|
+
const typedError = err;
|
|
2309
|
+
setError(typedError);
|
|
2310
|
+
setIsError(true);
|
|
2311
|
+
throw err;
|
|
2312
|
+
} finally {
|
|
2313
|
+
setIsPending(false);
|
|
2314
|
+
}
|
|
2315
|
+
}, []);
|
|
2316
|
+
const mutate = useCallback(
|
|
2317
|
+
(variables) => {
|
|
2318
|
+
mutateAsync(variables).catch(() => {
|
|
2319
|
+
});
|
|
2320
|
+
},
|
|
2321
|
+
[mutateAsync]
|
|
2322
|
+
);
|
|
2323
|
+
return {
|
|
2324
|
+
mutate,
|
|
2325
|
+
mutateAsync,
|
|
2326
|
+
isPending,
|
|
2327
|
+
isSuccess,
|
|
2328
|
+
isError,
|
|
2329
|
+
error,
|
|
2330
|
+
data,
|
|
2331
|
+
reset
|
|
2332
|
+
};
|
|
2333
|
+
}
|
|
2334
|
+
function useStreamWorkflow({ debugMode, tracingOptions, onError }) {
|
|
2335
|
+
const client = useMastraClient();
|
|
2336
|
+
const [streamResult, setStreamResult] = useState({});
|
|
2337
|
+
const [isStreaming, setIsStreaming] = useState(false);
|
|
2338
|
+
const readerRef = useRef(null);
|
|
2339
|
+
const observerRef = useRef(null);
|
|
2340
|
+
const resumeStreamRef = useRef(null);
|
|
2341
|
+
const timeTravelStreamRef = useRef(null);
|
|
2342
|
+
const isMountedRef = useRef(true);
|
|
2343
|
+
useEffect(() => {
|
|
2344
|
+
isMountedRef.current = true;
|
|
2345
|
+
return () => {
|
|
2346
|
+
isMountedRef.current = false;
|
|
2347
|
+
if (readerRef.current) {
|
|
2348
|
+
try {
|
|
2349
|
+
readerRef.current.releaseLock();
|
|
2350
|
+
} catch {
|
|
2351
|
+
}
|
|
2352
|
+
readerRef.current = null;
|
|
2353
|
+
}
|
|
2354
|
+
if (observerRef.current) {
|
|
2355
|
+
try {
|
|
2356
|
+
observerRef.current.releaseLock();
|
|
2357
|
+
} catch {
|
|
2358
|
+
}
|
|
2359
|
+
observerRef.current = null;
|
|
2360
|
+
}
|
|
2361
|
+
if (resumeStreamRef.current) {
|
|
2362
|
+
try {
|
|
2363
|
+
resumeStreamRef.current.releaseLock();
|
|
2364
|
+
} catch {
|
|
2365
|
+
}
|
|
2366
|
+
resumeStreamRef.current = null;
|
|
2367
|
+
}
|
|
2368
|
+
if (timeTravelStreamRef.current) {
|
|
2369
|
+
try {
|
|
2370
|
+
timeTravelStreamRef.current.releaseLock();
|
|
2371
|
+
} catch {
|
|
2372
|
+
}
|
|
2373
|
+
timeTravelStreamRef.current = null;
|
|
2374
|
+
}
|
|
2375
|
+
};
|
|
2376
|
+
}, []);
|
|
2377
|
+
const handleStreamError = useCallback(
|
|
2378
|
+
(err, defaultMessage, setStreamingState) => {
|
|
2379
|
+
if (err instanceof TypeError) {
|
|
2380
|
+
return;
|
|
2381
|
+
}
|
|
2382
|
+
const error = err instanceof Error ? err : new Error(defaultMessage);
|
|
2383
|
+
onError?.(error, defaultMessage);
|
|
2384
|
+
setStreamingState?.(false);
|
|
2385
|
+
},
|
|
2386
|
+
[onError]
|
|
2387
|
+
);
|
|
2388
|
+
const handleWorkflowFinish = useCallback((value) => {
|
|
2389
|
+
if (value.type === "workflow-finish") {
|
|
2390
|
+
const streamStatus = value.payload?.workflowStatus;
|
|
2391
|
+
const metadata = value.payload?.metadata;
|
|
2392
|
+
setStreamResult((prev) => ({
|
|
2393
|
+
...prev,
|
|
2394
|
+
status: streamStatus
|
|
2395
|
+
}));
|
|
2396
|
+
if (streamStatus === "failed") {
|
|
2397
|
+
throw new Error(metadata?.errorMessage || "Workflow execution failed");
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
}, []);
|
|
2401
|
+
const streamWorkflow = useMutation(
|
|
2402
|
+
async ({ workflowId, runId, inputData, initialState, requestContext: playgroundRequestContext, perStep }) => {
|
|
2403
|
+
if (readerRef.current) {
|
|
2404
|
+
readerRef.current.releaseLock();
|
|
2405
|
+
}
|
|
2406
|
+
if (!isMountedRef.current) return;
|
|
2407
|
+
setIsStreaming(true);
|
|
2408
|
+
setStreamResult({ input: inputData });
|
|
2409
|
+
const requestContext = new RequestContext();
|
|
2410
|
+
Object.entries(playgroundRequestContext).forEach(([key, value]) => {
|
|
2411
|
+
requestContext.set(key, value);
|
|
2412
|
+
});
|
|
2413
|
+
const workflow = client.getWorkflow(workflowId);
|
|
2414
|
+
const run = await workflow.createRun({ runId });
|
|
2415
|
+
const stream = await run.stream({
|
|
2416
|
+
inputData,
|
|
2417
|
+
initialState,
|
|
2418
|
+
requestContext,
|
|
2419
|
+
closeOnSuspend: true,
|
|
2420
|
+
tracingOptions,
|
|
2421
|
+
perStep: perStep ?? debugMode
|
|
2422
|
+
});
|
|
2423
|
+
if (!stream) {
|
|
2424
|
+
return handleStreamError(new Error("No stream returned"), "No stream returned", setIsStreaming);
|
|
2425
|
+
}
|
|
2426
|
+
const reader = stream.getReader();
|
|
2427
|
+
readerRef.current = reader;
|
|
2428
|
+
try {
|
|
2429
|
+
while (true) {
|
|
2430
|
+
if (!isMountedRef.current) break;
|
|
2431
|
+
const { done, value } = await reader.read();
|
|
2432
|
+
if (done) break;
|
|
2433
|
+
if (isMountedRef.current) {
|
|
2434
|
+
setStreamResult((prev) => {
|
|
2435
|
+
const newResult = mapWorkflowStreamChunkToWatchResult(prev, value);
|
|
2436
|
+
return newResult;
|
|
2437
|
+
});
|
|
2438
|
+
if (value.type === "workflow-step-start") {
|
|
2439
|
+
setIsStreaming(true);
|
|
2440
|
+
}
|
|
2441
|
+
if (value.type === "workflow-step-suspended") {
|
|
2442
|
+
setIsStreaming(false);
|
|
2443
|
+
}
|
|
2444
|
+
if (value.type === "workflow-finish") {
|
|
2445
|
+
handleWorkflowFinish(value);
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
} catch (err) {
|
|
2450
|
+
handleStreamError(err, "Error streaming workflow");
|
|
2451
|
+
} finally {
|
|
2452
|
+
if (isMountedRef.current) {
|
|
2453
|
+
setIsStreaming(false);
|
|
2454
|
+
}
|
|
2455
|
+
if (readerRef.current) {
|
|
2456
|
+
readerRef.current.releaseLock();
|
|
2457
|
+
readerRef.current = null;
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
);
|
|
2462
|
+
const observeWorkflowStream = useMutation(
|
|
2463
|
+
async ({ workflowId, runId, storeRunResult }) => {
|
|
2464
|
+
if (observerRef.current) {
|
|
2465
|
+
observerRef.current.releaseLock();
|
|
2466
|
+
}
|
|
2467
|
+
if (!isMountedRef.current) return;
|
|
2468
|
+
setIsStreaming(true);
|
|
2469
|
+
setStreamResult(storeRunResult || {});
|
|
2470
|
+
if (storeRunResult?.status === "suspended") {
|
|
2471
|
+
setIsStreaming(false);
|
|
2472
|
+
return;
|
|
2473
|
+
}
|
|
2474
|
+
const workflow = client.getWorkflow(workflowId);
|
|
2475
|
+
const run = await workflow.createRun({ runId });
|
|
2476
|
+
const stream = await run.observeStream();
|
|
2477
|
+
if (!stream) {
|
|
2478
|
+
return handleStreamError(new Error("No stream returned"), "No stream returned", setIsStreaming);
|
|
2479
|
+
}
|
|
2480
|
+
const reader = stream.getReader();
|
|
2481
|
+
observerRef.current = reader;
|
|
2482
|
+
try {
|
|
2483
|
+
while (true) {
|
|
2484
|
+
if (!isMountedRef.current) break;
|
|
2485
|
+
const { done, value } = await reader.read();
|
|
2486
|
+
if (done) break;
|
|
2487
|
+
if (isMountedRef.current) {
|
|
2488
|
+
setStreamResult((prev) => {
|
|
2489
|
+
const newResult = mapWorkflowStreamChunkToWatchResult(prev, value);
|
|
2490
|
+
return newResult;
|
|
2491
|
+
});
|
|
2492
|
+
if (value.type === "workflow-step-start") {
|
|
2493
|
+
setIsStreaming(true);
|
|
2494
|
+
}
|
|
2495
|
+
if (value.type === "workflow-step-suspended") {
|
|
2496
|
+
setIsStreaming(false);
|
|
2497
|
+
}
|
|
2498
|
+
if (value.type === "workflow-finish") {
|
|
2499
|
+
handleWorkflowFinish(value);
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
} catch (err) {
|
|
2504
|
+
handleStreamError(err, "Error observing workflow");
|
|
2505
|
+
} finally {
|
|
2506
|
+
if (isMountedRef.current) {
|
|
2507
|
+
setIsStreaming(false);
|
|
2508
|
+
}
|
|
2509
|
+
if (observerRef.current) {
|
|
2510
|
+
observerRef.current.releaseLock();
|
|
2511
|
+
observerRef.current = null;
|
|
2512
|
+
}
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
);
|
|
2516
|
+
const resumeWorkflowStream = useMutation(
|
|
2517
|
+
async ({ workflowId, runId, step, resumeData, requestContext: playgroundRequestContext, perStep }) => {
|
|
2518
|
+
if (resumeStreamRef.current) {
|
|
2519
|
+
resumeStreamRef.current.releaseLock();
|
|
2520
|
+
}
|
|
2521
|
+
if (!isMountedRef.current) return;
|
|
2522
|
+
setIsStreaming(true);
|
|
2523
|
+
const workflow = client.getWorkflow(workflowId);
|
|
2524
|
+
const requestContext = new RequestContext();
|
|
2525
|
+
Object.entries(playgroundRequestContext).forEach(([key, value]) => {
|
|
2526
|
+
requestContext.set(key, value);
|
|
2527
|
+
});
|
|
2528
|
+
const run = await workflow.createRun({ runId });
|
|
2529
|
+
const stream = await run.resumeStream({
|
|
2530
|
+
step,
|
|
2531
|
+
resumeData,
|
|
2532
|
+
requestContext,
|
|
2533
|
+
tracingOptions,
|
|
2534
|
+
perStep: perStep ?? debugMode
|
|
2535
|
+
});
|
|
2536
|
+
if (!stream) {
|
|
2537
|
+
return handleStreamError(new Error("No stream returned"), "No stream returned", setIsStreaming);
|
|
2538
|
+
}
|
|
2539
|
+
const reader = stream.getReader();
|
|
2540
|
+
resumeStreamRef.current = reader;
|
|
2541
|
+
try {
|
|
2542
|
+
while (true) {
|
|
2543
|
+
if (!isMountedRef.current) break;
|
|
2544
|
+
const { done, value } = await reader.read();
|
|
2545
|
+
if (done) break;
|
|
2546
|
+
if (isMountedRef.current) {
|
|
2547
|
+
setStreamResult((prev) => {
|
|
2548
|
+
const newResult = mapWorkflowStreamChunkToWatchResult(prev, value);
|
|
2549
|
+
return newResult;
|
|
2550
|
+
});
|
|
2551
|
+
if (value.type === "workflow-step-start") {
|
|
2552
|
+
setIsStreaming(true);
|
|
2553
|
+
}
|
|
2554
|
+
if (value.type === "workflow-step-suspended") {
|
|
2555
|
+
setIsStreaming(false);
|
|
2556
|
+
}
|
|
2557
|
+
if (value.type === "workflow-finish") {
|
|
2558
|
+
handleWorkflowFinish(value);
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
} catch (err) {
|
|
2563
|
+
handleStreamError(err, "Error resuming workflow stream");
|
|
2564
|
+
} finally {
|
|
2565
|
+
if (isMountedRef.current) {
|
|
2566
|
+
setIsStreaming(false);
|
|
2567
|
+
}
|
|
2568
|
+
if (resumeStreamRef.current) {
|
|
2569
|
+
resumeStreamRef.current.releaseLock();
|
|
2570
|
+
resumeStreamRef.current = null;
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
}
|
|
2574
|
+
);
|
|
2575
|
+
const timeTravelWorkflowStream = useMutation(
|
|
2576
|
+
async ({ workflowId, requestContext: playgroundRequestContext, runId, perStep, ...params }) => {
|
|
2577
|
+
if (timeTravelStreamRef.current) {
|
|
2578
|
+
timeTravelStreamRef.current.releaseLock();
|
|
2579
|
+
}
|
|
2580
|
+
if (!isMountedRef.current) return;
|
|
2581
|
+
setIsStreaming(true);
|
|
2582
|
+
const workflow = client.getWorkflow(workflowId);
|
|
2583
|
+
const requestContext = new RequestContext();
|
|
2584
|
+
Object.entries(playgroundRequestContext).forEach(([key, value]) => {
|
|
2585
|
+
requestContext.set(key, value);
|
|
2586
|
+
});
|
|
2587
|
+
const run = await workflow.createRun({ runId });
|
|
2588
|
+
const stream = await run.timeTravelStream({
|
|
2589
|
+
...params,
|
|
2590
|
+
perStep: perStep ?? debugMode,
|
|
2591
|
+
requestContext,
|
|
2592
|
+
tracingOptions
|
|
2593
|
+
});
|
|
2594
|
+
if (!stream) {
|
|
2595
|
+
return handleStreamError(new Error("No stream returned"), "No stream returned", setIsStreaming);
|
|
2596
|
+
}
|
|
2597
|
+
const reader = stream.getReader();
|
|
2598
|
+
timeTravelStreamRef.current = reader;
|
|
2599
|
+
try {
|
|
2600
|
+
while (true) {
|
|
2601
|
+
if (!isMountedRef.current) break;
|
|
2602
|
+
const { done, value } = await reader.read();
|
|
2603
|
+
if (done) break;
|
|
2604
|
+
if (isMountedRef.current) {
|
|
2605
|
+
setStreamResult((prev) => {
|
|
2606
|
+
const newResult = mapWorkflowStreamChunkToWatchResult(prev, value);
|
|
2607
|
+
return newResult;
|
|
2608
|
+
});
|
|
2609
|
+
if (value.type === "workflow-step-start") {
|
|
2610
|
+
setIsStreaming(true);
|
|
2611
|
+
}
|
|
2612
|
+
if (value.type === "workflow-step-suspended") {
|
|
2613
|
+
setIsStreaming(false);
|
|
2614
|
+
}
|
|
2615
|
+
if (value.type === "workflow-finish") {
|
|
2616
|
+
handleWorkflowFinish(value);
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
} catch (err) {
|
|
2621
|
+
handleStreamError(err, "Error time traveling workflow stream");
|
|
2622
|
+
} finally {
|
|
2623
|
+
if (isMountedRef.current) {
|
|
2624
|
+
setIsStreaming(false);
|
|
2625
|
+
}
|
|
2626
|
+
if (timeTravelStreamRef.current) {
|
|
2627
|
+
timeTravelStreamRef.current.releaseLock();
|
|
2628
|
+
timeTravelStreamRef.current = null;
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
}
|
|
2632
|
+
);
|
|
2633
|
+
const closeStreamsAndReset = useCallback(() => {
|
|
2634
|
+
setIsStreaming(false);
|
|
2635
|
+
setStreamResult({});
|
|
2636
|
+
if (readerRef.current) {
|
|
2637
|
+
try {
|
|
2638
|
+
readerRef.current.releaseLock();
|
|
2639
|
+
} catch {
|
|
2640
|
+
}
|
|
2641
|
+
readerRef.current = null;
|
|
2642
|
+
}
|
|
2643
|
+
if (observerRef.current) {
|
|
2644
|
+
try {
|
|
2645
|
+
observerRef.current.releaseLock();
|
|
2646
|
+
} catch {
|
|
2647
|
+
}
|
|
2648
|
+
observerRef.current = null;
|
|
2649
|
+
}
|
|
2650
|
+
if (resumeStreamRef.current) {
|
|
2651
|
+
try {
|
|
2652
|
+
resumeStreamRef.current.releaseLock();
|
|
2653
|
+
} catch {
|
|
2654
|
+
}
|
|
2655
|
+
resumeStreamRef.current = null;
|
|
2656
|
+
}
|
|
2657
|
+
if (timeTravelStreamRef.current) {
|
|
2658
|
+
try {
|
|
2659
|
+
timeTravelStreamRef.current.releaseLock();
|
|
2660
|
+
} catch {
|
|
2661
|
+
}
|
|
2662
|
+
timeTravelStreamRef.current = null;
|
|
2663
|
+
}
|
|
2664
|
+
}, []);
|
|
2665
|
+
return {
|
|
2666
|
+
streamWorkflow,
|
|
2667
|
+
streamResult,
|
|
2668
|
+
isStreaming,
|
|
2669
|
+
observeWorkflowStream,
|
|
2670
|
+
closeStreamsAndReset,
|
|
2671
|
+
resumeWorkflowStream,
|
|
2672
|
+
timeTravelWorkflowStream
|
|
2673
|
+
};
|
|
2674
|
+
}
|
|
2675
|
+
|
|
2676
|
+
// src/workflows/hooks.ts
|
|
2677
|
+
function useCreateWorkflowRun() {
|
|
2678
|
+
const client = useMastraClient();
|
|
2679
|
+
return useMutation(async ({ workflowId, prevRunId }) => {
|
|
2680
|
+
try {
|
|
2681
|
+
const workflow = client.getWorkflow(workflowId);
|
|
2682
|
+
const { runId: newRunId } = await workflow.createRun({ runId: prevRunId });
|
|
2683
|
+
return { runId: newRunId };
|
|
2684
|
+
} catch (error) {
|
|
2685
|
+
console.error("Error creating workflow run:", error);
|
|
2686
|
+
throw error;
|
|
2687
|
+
}
|
|
2688
|
+
});
|
|
2689
|
+
}
|
|
2690
|
+
function useCancelWorkflowRun() {
|
|
2691
|
+
const client = useMastraClient();
|
|
2692
|
+
return useMutation(async ({ workflowId, runId }) => {
|
|
2693
|
+
try {
|
|
2694
|
+
const workflow = client.getWorkflow(workflowId);
|
|
2695
|
+
const run = await workflow.createRun({ runId });
|
|
2696
|
+
return run.cancelRun();
|
|
2697
|
+
} catch (error) {
|
|
2698
|
+
console.error("Error canceling workflow run:", error);
|
|
2699
|
+
throw error;
|
|
2700
|
+
}
|
|
2701
|
+
});
|
|
2702
|
+
}
|
|
2703
|
+
|
|
2704
|
+
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, useCancelWorkflowRun, useChat, useCreateWorkflowRun, useEntity, useMastraClient, useStreamWorkflow };
|
|
2
2705
|
//# sourceMappingURL=index.js.map
|
|
2706
|
+
//# sourceMappingURL=index.js.map
|