@mastra/react 0.0.0-remove-unused-model-providers-api-20251030210744 → 0.0.0-safe-stringify-telemetry-20251205024938
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 +219 -6
- package/dist/index.cjs +89 -96
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +89 -96
- package/dist/index.js.map +1 -1
- package/dist/react.css +1 -1
- package/dist/src/agent/hooks.d.ts +4 -3
- package/dist/src/lib/ai-sdk/types.d.ts +10 -0
- package/package.json +11 -3
- package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.d.ts +0 -10
- package/dist/src/lib/ai-sdk/utils/fromCoreUserMessageToUIMessage.test.d.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,19 +1,232 @@
|
|
|
1
1
|
# @mastra/react-hooks
|
|
2
2
|
|
|
3
|
-
## 0.0.0-
|
|
3
|
+
## 0.0.0-safe-stringify-telemetry-20251205024938
|
|
4
4
|
|
|
5
|
-
###
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`5de72e1`](https://github.com/mastra-ai/mastra/commit/5de72e1a3fa51b860e7e0a606b66cf6f97fc100c), [`2b62302`](https://github.com/mastra-ai/mastra/commit/2b623027a9d65c1dbc963bf651e9e6a9d09da1fa)]:
|
|
8
|
+
- @mastra/client-js@0.0.0-safe-stringify-telemetry-20251205024938
|
|
9
|
+
|
|
10
|
+
## 0.0.22-alpha.0
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Updated dependencies []:
|
|
15
|
+
- @mastra/client-js@0.16.16-alpha.0
|
|
16
|
+
|
|
17
|
+
## 0.0.21
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Configurable resourceId in react useChat ([#10561](https://github.com/mastra-ai/mastra/pull/10561))
|
|
22
|
+
|
|
23
|
+
- fix(agent): persist messages before tool suspension ([#10542](https://github.com/mastra-ai/mastra/pull/10542))
|
|
24
|
+
|
|
25
|
+
Fixes issues where thread and messages were not saved before suspension when tools require approval or call suspend() during execution. This caused conversation history to be lost if users refreshed during tool approval or suspension.
|
|
26
|
+
|
|
27
|
+
**Backend changes (@mastra/core):**
|
|
28
|
+
- Add assistant messages to messageList immediately after LLM execution
|
|
29
|
+
- Flush messages synchronously before suspension to persist state
|
|
30
|
+
- Create thread if it doesn't exist before flushing
|
|
31
|
+
- Add metadata helpers to persist and remove tool approval state
|
|
32
|
+
- Pass saveQueueManager and memory context through workflow for immediate persistence
|
|
33
|
+
|
|
34
|
+
**Frontend changes (@mastra/react):**
|
|
35
|
+
- Extract runId from pending approvals to enable resumption after refresh
|
|
36
|
+
- Convert `pendingToolApprovals` (DB format) to `requireApprovalMetadata` (runtime format)
|
|
37
|
+
- Handle both `dynamic-tool` and `tool-{NAME}` part types for approval state
|
|
38
|
+
- Change runId from hardcoded `agentId` to unique `uuid()`
|
|
39
|
+
|
|
40
|
+
**UI changes (@mastra/playground-ui):**
|
|
41
|
+
- Handle tool calls awaiting approval in message initialization
|
|
42
|
+
- Convert approval metadata format when loading initial messages
|
|
43
|
+
|
|
44
|
+
Fixes #9745, #9906
|
|
45
|
+
|
|
46
|
+
- Updated dependencies []:
|
|
47
|
+
- @mastra/client-js@0.16.15
|
|
48
|
+
|
|
49
|
+
## 0.0.21-alpha.0
|
|
50
|
+
|
|
51
|
+
### Patch Changes
|
|
52
|
+
|
|
53
|
+
- Configurable resourceId in react useChat ([#10561](https://github.com/mastra-ai/mastra/pull/10561))
|
|
54
|
+
|
|
55
|
+
- fix(agent): persist messages before tool suspension ([#10542](https://github.com/mastra-ai/mastra/pull/10542))
|
|
56
|
+
|
|
57
|
+
Fixes issues where thread and messages were not saved before suspension when tools require approval or call suspend() during execution. This caused conversation history to be lost if users refreshed during tool approval or suspension.
|
|
58
|
+
|
|
59
|
+
**Backend changes (@mastra/core):**
|
|
60
|
+
- Add assistant messages to messageList immediately after LLM execution
|
|
61
|
+
- Flush messages synchronously before suspension to persist state
|
|
62
|
+
- Create thread if it doesn't exist before flushing
|
|
63
|
+
- Add metadata helpers to persist and remove tool approval state
|
|
64
|
+
- Pass saveQueueManager and memory context through workflow for immediate persistence
|
|
65
|
+
|
|
66
|
+
**Frontend changes (@mastra/react):**
|
|
67
|
+
- Extract runId from pending approvals to enable resumption after refresh
|
|
68
|
+
- Convert `pendingToolApprovals` (DB format) to `requireApprovalMetadata` (runtime format)
|
|
69
|
+
- Handle both `dynamic-tool` and `tool-{NAME}` part types for approval state
|
|
70
|
+
- Change runId from hardcoded `agentId` to unique `uuid()`
|
|
71
|
+
|
|
72
|
+
**UI changes (@mastra/playground-ui):**
|
|
73
|
+
- Handle tool calls awaiting approval in message initialization
|
|
74
|
+
- Convert approval metadata format when loading initial messages
|
|
75
|
+
|
|
76
|
+
Fixes #9745, #9906
|
|
77
|
+
|
|
78
|
+
- Updated dependencies []:
|
|
79
|
+
- @mastra/client-js@0.16.15-alpha.0
|
|
80
|
+
|
|
81
|
+
## 0.0.20
|
|
82
|
+
|
|
83
|
+
### Patch Changes
|
|
84
|
+
|
|
85
|
+
- Updated dependencies []:
|
|
86
|
+
- @mastra/client-js@0.16.14
|
|
87
|
+
|
|
88
|
+
## 0.0.20-alpha.0
|
|
89
|
+
|
|
90
|
+
### Patch Changes
|
|
91
|
+
|
|
92
|
+
- Updated dependencies []:
|
|
93
|
+
- @mastra/client-js@0.16.14-alpha.0
|
|
94
|
+
|
|
95
|
+
## 0.0.19
|
|
96
|
+
|
|
97
|
+
### Patch Changes
|
|
98
|
+
|
|
99
|
+
- Updated dependencies []:
|
|
100
|
+
- @mastra/client-js@0.16.13
|
|
101
|
+
|
|
102
|
+
## 0.0.19-alpha.0
|
|
103
|
+
|
|
104
|
+
### Patch Changes
|
|
105
|
+
|
|
106
|
+
- Updated dependencies []:
|
|
107
|
+
- @mastra/client-js@0.16.13-alpha.0
|
|
108
|
+
|
|
109
|
+
## 0.0.18
|
|
110
|
+
|
|
111
|
+
### Patch Changes
|
|
112
|
+
|
|
113
|
+
- Updated dependencies [[`91fa2b0`](https://github.com/mastra-ai/mastra/commit/91fa2b0cf077561d835baef8519a1422f1decd55)]:
|
|
114
|
+
- @mastra/client-js@0.16.12
|
|
115
|
+
|
|
116
|
+
## 0.0.18-alpha.0
|
|
117
|
+
|
|
118
|
+
### Patch Changes
|
|
119
|
+
|
|
120
|
+
- Updated dependencies [[`91fa2b0`](https://github.com/mastra-ai/mastra/commit/91fa2b0cf077561d835baef8519a1422f1decd55)]:
|
|
121
|
+
- @mastra/client-js@0.16.12-alpha.0
|
|
122
|
+
|
|
123
|
+
## 0.0.17
|
|
6
124
|
|
|
7
|
-
|
|
125
|
+
### Patch Changes
|
|
126
|
+
|
|
127
|
+
- Updated dependencies []:
|
|
128
|
+
- @mastra/client-js@0.16.11
|
|
129
|
+
|
|
130
|
+
## 0.0.17-alpha.0
|
|
131
|
+
|
|
132
|
+
### Patch Changes
|
|
133
|
+
|
|
134
|
+
- Updated dependencies []:
|
|
135
|
+
- @mastra/client-js@0.16.11-alpha.0
|
|
136
|
+
|
|
137
|
+
## 0.0.16
|
|
138
|
+
|
|
139
|
+
### Patch Changes
|
|
140
|
+
|
|
141
|
+
- Updated dependencies [[`082393c`](https://github.com/mastra-ai/mastra/commit/082393c002f3751113a48da28e4c0ed3db9824d7)]:
|
|
142
|
+
- @mastra/client-js@0.16.10
|
|
143
|
+
|
|
144
|
+
## 0.0.16-alpha.0
|
|
145
|
+
|
|
146
|
+
### Patch Changes
|
|
147
|
+
|
|
148
|
+
- Updated dependencies [[`082393c`](https://github.com/mastra-ai/mastra/commit/082393c002f3751113a48da28e4c0ed3db9824d7)]:
|
|
149
|
+
- @mastra/client-js@0.16.10-alpha.0
|
|
150
|
+
|
|
151
|
+
## 0.0.15
|
|
152
|
+
|
|
153
|
+
### Patch Changes
|
|
154
|
+
|
|
155
|
+
- update peerdeps ([`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc))
|
|
156
|
+
|
|
157
|
+
- Updated dependencies [[`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc), [`23c2614`](https://github.com/mastra-ai/mastra/commit/23c26140fdbf04b8c59e8d7d52106d67dad962ec), [`139588d`](https://github.com/mastra-ai/mastra/commit/139588df7755c9111a3060f72a789c1a8c95e091), [`186b29b`](https://github.com/mastra-ai/mastra/commit/186b29bd51ac1dcc24ad3825fdb7207a6d6391a6)]:
|
|
158
|
+
- @mastra/client-js@0.16.9
|
|
159
|
+
|
|
160
|
+
## 0.0.15-alpha.0
|
|
161
|
+
|
|
162
|
+
### Patch Changes
|
|
163
|
+
|
|
164
|
+
- update peerdeps ([`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc))
|
|
165
|
+
|
|
166
|
+
- Updated dependencies [[`5ca1cca`](https://github.com/mastra-ai/mastra/commit/5ca1ccac61ffa7141e6d9fa8f22d3ad4d03bf5dc), [`23c2614`](https://github.com/mastra-ai/mastra/commit/23c26140fdbf04b8c59e8d7d52106d67dad962ec), [`139588d`](https://github.com/mastra-ai/mastra/commit/139588df7755c9111a3060f72a789c1a8c95e091), [`186b29b`](https://github.com/mastra-ai/mastra/commit/186b29bd51ac1dcc24ad3825fdb7207a6d6391a6)]:
|
|
167
|
+
- @mastra/client-js@0.16.9-alpha.0
|
|
168
|
+
|
|
169
|
+
## 0.0.14
|
|
170
|
+
|
|
171
|
+
### Patch Changes
|
|
172
|
+
|
|
173
|
+
- Updated dependencies [[`b55bbce`](https://github.com/mastra-ai/mastra/commit/b55bbce89404d35fdd967012dd503fae343d4c2d), [`e742d37`](https://github.com/mastra-ai/mastra/commit/e742d371f24ef8059670cc05e9aee308eac068b9)]:
|
|
174
|
+
- @mastra/client-js@0.16.8
|
|
175
|
+
|
|
176
|
+
## 0.0.14-alpha.0
|
|
177
|
+
|
|
178
|
+
### Patch Changes
|
|
179
|
+
|
|
180
|
+
- Updated dependencies [[`b55bbce`](https://github.com/mastra-ai/mastra/commit/b55bbce89404d35fdd967012dd503fae343d4c2d), [`e742d37`](https://github.com/mastra-ai/mastra/commit/e742d371f24ef8059670cc05e9aee308eac068b9)]:
|
|
181
|
+
- @mastra/client-js@0.16.8-alpha.0
|
|
182
|
+
|
|
183
|
+
## 0.0.13
|
|
184
|
+
|
|
185
|
+
### Patch Changes
|
|
186
|
+
|
|
187
|
+
- Updated dependencies []:
|
|
188
|
+
- @mastra/client-js@0.16.7
|
|
189
|
+
|
|
190
|
+
## 0.0.13-alpha.1
|
|
191
|
+
|
|
192
|
+
### Patch Changes
|
|
193
|
+
|
|
194
|
+
- Updated dependencies []:
|
|
195
|
+
- @mastra/client-js@0.16.7-alpha.1
|
|
196
|
+
|
|
197
|
+
## 0.0.13-alpha.0
|
|
198
|
+
|
|
199
|
+
### Patch Changes
|
|
200
|
+
|
|
201
|
+
- Updated dependencies []:
|
|
202
|
+
- @mastra/client-js@0.16.7-alpha.0
|
|
203
|
+
|
|
204
|
+
## 0.0.12
|
|
205
|
+
|
|
206
|
+
### Patch Changes
|
|
207
|
+
|
|
208
|
+
- Fix peerdependencies ([`eb7c1c8`](https://github.com/mastra-ai/mastra/commit/eb7c1c8c592d8fb16dfd250e337d9cdc73c8d5de))
|
|
209
|
+
|
|
210
|
+
- Updated dependencies [[`eb7c1c8`](https://github.com/mastra-ai/mastra/commit/eb7c1c8c592d8fb16dfd250e337d9cdc73c8d5de)]:
|
|
211
|
+
- @mastra/client-js@0.16.6
|
|
212
|
+
|
|
213
|
+
## 0.0.11
|
|
8
214
|
|
|
9
215
|
### Patch Changes
|
|
10
216
|
|
|
11
217
|
- Add tool call approval ([#8649](https://github.com/mastra-ai/mastra/pull/8649))
|
|
12
218
|
|
|
13
|
-
-
|
|
219
|
+
- Updated dependencies [[`5df9cce`](https://github.com/mastra-ai/mastra/commit/5df9cce1a753438413f64c11eeef8f845745c2a8), [`2060766`](https://github.com/mastra-ai/mastra/commit/20607667bf78ea104cca3e15dfb93ae0b62c9d18)]:
|
|
220
|
+
- @mastra/client-js@0.16.5
|
|
221
|
+
|
|
222
|
+
## 0.0.11-alpha.0
|
|
223
|
+
|
|
224
|
+
### Patch Changes
|
|
225
|
+
|
|
226
|
+
- Add tool call approval ([#8649](https://github.com/mastra-ai/mastra/pull/8649))
|
|
14
227
|
|
|
15
|
-
- Updated dependencies [[`
|
|
16
|
-
- @mastra/client-js@0.
|
|
228
|
+
- Updated dependencies [[`5df9cce`](https://github.com/mastra-ai/mastra/commit/5df9cce1a753438413f64c11eeef8f845745c2a8), [`2060766`](https://github.com/mastra-ai/mastra/commit/20607667bf78ea104cca3e15dfb93ae0b62c9d18)]:
|
|
229
|
+
- @mastra/client-js@0.16.5-alpha.0
|
|
17
230
|
|
|
18
231
|
## 0.0.10
|
|
19
232
|
|
package/dist/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5
5
|
const jsxRuntime = require('react/jsx-runtime');
|
|
6
6
|
const react = require('react');
|
|
7
7
|
const clientJs = require('@mastra/client-js');
|
|
8
|
+
const uuid = require('@lukeed/uuid');
|
|
8
9
|
const lucideReact = require('lucide-react');
|
|
9
10
|
const tailwindMerge = require('tailwind-merge');
|
|
10
11
|
const hastUtilToJsxRuntime = require('hast-util-to-jsx-runtime');
|
|
@@ -259,17 +260,19 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
259
260
|
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
260
261
|
const parts = [...lastMessage.parts];
|
|
261
262
|
const toolPartIndex = parts.findIndex(
|
|
262
|
-
(part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
263
|
+
(part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
263
264
|
);
|
|
264
265
|
if (toolPartIndex !== -1) {
|
|
265
266
|
const toolPart = parts[toolPartIndex];
|
|
266
|
-
if (toolPart.type === "dynamic-tool") {
|
|
267
|
+
if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
|
|
268
|
+
const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
|
|
269
|
+
const toolCallId = toolPart.toolCallId;
|
|
267
270
|
if (chunk.type === "tool-result" && chunk.payload.isError || chunk.type === "tool-error") {
|
|
268
271
|
const error = chunk.type === "tool-error" ? chunk.payload.error : chunk.payload.result;
|
|
269
272
|
parts[toolPartIndex] = {
|
|
270
273
|
type: "dynamic-tool",
|
|
271
|
-
toolName
|
|
272
|
-
toolCallId
|
|
274
|
+
toolName,
|
|
275
|
+
toolCallId,
|
|
273
276
|
state: "output-error",
|
|
274
277
|
input: toolPart.input,
|
|
275
278
|
errorText: String(error),
|
|
@@ -288,8 +291,8 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
288
291
|
}
|
|
289
292
|
parts[toolPartIndex] = {
|
|
290
293
|
type: "dynamic-tool",
|
|
291
|
-
toolName
|
|
292
|
-
toolCallId
|
|
294
|
+
toolName,
|
|
295
|
+
toolCallId,
|
|
293
296
|
state: "output-available",
|
|
294
297
|
input: toolPart.input,
|
|
295
298
|
output,
|
|
@@ -311,11 +314,14 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
311
314
|
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
312
315
|
const parts = [...lastMessage.parts];
|
|
313
316
|
const toolPartIndex = parts.findIndex(
|
|
314
|
-
(part) => part.type === "dynamic-tool" && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
317
|
+
(part) => (part.type === "dynamic-tool" || typeof part.type === "string" && part.type.startsWith("tool-")) && "toolCallId" in part && part.toolCallId === chunk.payload.toolCallId
|
|
315
318
|
);
|
|
316
319
|
if (toolPartIndex !== -1) {
|
|
317
320
|
const toolPart = parts[toolPartIndex];
|
|
318
|
-
if (toolPart.type === "dynamic-tool") {
|
|
321
|
+
if (toolPart.type === "dynamic-tool" || typeof toolPart.type === "string" && toolPart.type.startsWith("tool-")) {
|
|
322
|
+
const toolName = "toolName" in toolPart && typeof toolPart.toolName === "string" ? toolPart.toolName : typeof toolPart.type === "string" && toolPart.type.startsWith("tool-") ? toolPart.type.substring(5) : "";
|
|
323
|
+
const toolCallId = toolPart.toolCallId;
|
|
324
|
+
const input = toolPart.input;
|
|
319
325
|
if (chunk.payload.output?.type?.startsWith("workflow-")) {
|
|
320
326
|
const existingWorkflowState = toolPart.output || {};
|
|
321
327
|
const updatedWorkflowState = mapWorkflowStreamChunkToWatchResult(
|
|
@@ -323,7 +329,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
323
329
|
chunk.payload.output
|
|
324
330
|
);
|
|
325
331
|
parts[toolPartIndex] = {
|
|
326
|
-
|
|
332
|
+
type: "dynamic-tool",
|
|
333
|
+
toolName,
|
|
334
|
+
toolCallId,
|
|
335
|
+
state: "input-streaming",
|
|
336
|
+
input,
|
|
327
337
|
output: updatedWorkflowState
|
|
328
338
|
};
|
|
329
339
|
} else if (chunk.payload.output?.from === "AGENT" || chunk.payload.output?.from === "USER" && chunk.payload.output?.payload?.output?.type?.startsWith("workflow-")) {
|
|
@@ -332,7 +342,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
332
342
|
const currentOutput = toolPart.output || [];
|
|
333
343
|
const existingOutput = Array.isArray(currentOutput) ? currentOutput : [];
|
|
334
344
|
parts[toolPartIndex] = {
|
|
335
|
-
|
|
345
|
+
type: "dynamic-tool",
|
|
346
|
+
toolName,
|
|
347
|
+
toolCallId,
|
|
348
|
+
state: "input-streaming",
|
|
349
|
+
input,
|
|
336
350
|
output: [...existingOutput, chunk.payload.output]
|
|
337
351
|
};
|
|
338
352
|
}
|
|
@@ -604,23 +618,13 @@ const toAssistantUIMessage = (message) => {
|
|
|
604
618
|
};
|
|
605
619
|
}
|
|
606
620
|
if (part.type === "file") {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
metadata: message.metadata
|
|
615
|
-
};
|
|
616
|
-
}
|
|
617
|
-
if (type === "image") {
|
|
618
|
-
return {
|
|
619
|
-
type,
|
|
620
|
-
image: part.url,
|
|
621
|
-
metadata: message.metadata
|
|
622
|
-
};
|
|
623
|
-
}
|
|
621
|
+
return {
|
|
622
|
+
type: "file",
|
|
623
|
+
mimeType: part.mediaType,
|
|
624
|
+
data: part.url,
|
|
625
|
+
// Use URL as data source
|
|
626
|
+
metadata: message.metadata
|
|
627
|
+
};
|
|
624
628
|
}
|
|
625
629
|
if (part.type === "dynamic-tool") {
|
|
626
630
|
const baseToolCall = {
|
|
@@ -656,6 +660,20 @@ const toAssistantUIMessage = (message) => {
|
|
|
656
660
|
}
|
|
657
661
|
return baseToolCall;
|
|
658
662
|
}
|
|
663
|
+
const requireApprovalMetadata = extendedMessage.metadata?.requireApprovalMetadata;
|
|
664
|
+
const partToolCallId = "toolCallId" in part && typeof part.toolCallId === "string" ? part.toolCallId : void 0;
|
|
665
|
+
const suspensionData = partToolCallId ? requireApprovalMetadata?.[partToolCallId] : void 0;
|
|
666
|
+
if (suspensionData) {
|
|
667
|
+
const toolName = "toolName" in part && typeof part.toolName === "string" ? part.toolName : part.type.startsWith("tool-") ? part.type.substring(5) : "";
|
|
668
|
+
return {
|
|
669
|
+
type: "tool-call",
|
|
670
|
+
toolCallId: partToolCallId,
|
|
671
|
+
toolName,
|
|
672
|
+
argsText: "input" in part ? JSON.stringify(part.input) : "{}",
|
|
673
|
+
args: "input" in part ? part.input : {},
|
|
674
|
+
metadata: extendedMessage.metadata
|
|
675
|
+
};
|
|
676
|
+
}
|
|
659
677
|
return {
|
|
660
678
|
type: "text",
|
|
661
679
|
text: "",
|
|
@@ -740,7 +758,6 @@ const resolveInitialMessages = (messages) => {
|
|
|
740
758
|
childMessages,
|
|
741
759
|
result: finalResult?.text || ""
|
|
742
760
|
};
|
|
743
|
-
console.log("json", json);
|
|
744
761
|
const nextMessage = {
|
|
745
762
|
role: "assistant",
|
|
746
763
|
parts: [
|
|
@@ -768,6 +785,18 @@ const resolveInitialMessages = (messages) => {
|
|
|
768
785
|
return message;
|
|
769
786
|
}
|
|
770
787
|
}
|
|
788
|
+
const extendedMessage = message;
|
|
789
|
+
const pendingToolApprovals = extendedMessage.metadata?.pendingToolApprovals;
|
|
790
|
+
if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
|
|
791
|
+
return {
|
|
792
|
+
...message,
|
|
793
|
+
metadata: {
|
|
794
|
+
...message.metadata,
|
|
795
|
+
mode: "stream",
|
|
796
|
+
requireApprovalMetadata: pendingToolApprovals
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
}
|
|
771
800
|
return message;
|
|
772
801
|
});
|
|
773
802
|
};
|
|
@@ -1171,63 +1200,30 @@ class AISdkNetworkTransformer {
|
|
|
1171
1200
|
};
|
|
1172
1201
|
}
|
|
1173
1202
|
|
|
1174
|
-
const
|
|
1175
|
-
const
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
case "text": {
|
|
1184
|
-
return {
|
|
1185
|
-
type: "text",
|
|
1186
|
-
text: part.text
|
|
1187
|
-
};
|
|
1188
|
-
}
|
|
1189
|
-
case "image": {
|
|
1190
|
-
const url = typeof part.image === "string" ? part.image : part.image instanceof URL ? part.image.toString() : "";
|
|
1191
|
-
return {
|
|
1192
|
-
type: "file",
|
|
1193
|
-
mediaType: part.mimeType ?? "image/*",
|
|
1194
|
-
url
|
|
1195
|
-
};
|
|
1196
|
-
}
|
|
1197
|
-
case "file": {
|
|
1198
|
-
const url = typeof part.data === "string" ? part.data : part.data instanceof URL ? part.data.toString() : "";
|
|
1199
|
-
return {
|
|
1200
|
-
type: "file",
|
|
1201
|
-
mediaType: part.mimeType,
|
|
1202
|
-
url,
|
|
1203
|
-
...part.filename !== void 0 ? { filename: part.filename } : {}
|
|
1204
|
-
};
|
|
1205
|
-
}
|
|
1206
|
-
default: {
|
|
1207
|
-
const exhaustiveCheck = part;
|
|
1208
|
-
throw new Error(`Unhandled content part type: ${exhaustiveCheck.type}`);
|
|
1203
|
+
const useChat = ({ agentId, resourceId, initializeMessages }) => {
|
|
1204
|
+
const extractRunIdFromMessages = (messages2) => {
|
|
1205
|
+
for (const message of messages2) {
|
|
1206
|
+
const pendingToolApprovals = message.metadata?.pendingToolApprovals;
|
|
1207
|
+
if (pendingToolApprovals && typeof pendingToolApprovals === "object") {
|
|
1208
|
+
const suspensionData = Object.values(pendingToolApprovals)[0];
|
|
1209
|
+
if (suspensionData?.runId) {
|
|
1210
|
+
return suspensionData.runId;
|
|
1211
|
+
}
|
|
1209
1212
|
}
|
|
1210
1213
|
}
|
|
1211
|
-
|
|
1212
|
-
return {
|
|
1213
|
-
id,
|
|
1214
|
-
role: "user",
|
|
1215
|
-
parts
|
|
1214
|
+
return void 0;
|
|
1216
1215
|
};
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
const
|
|
1220
|
-
const _currentRunId = react.useRef(void 0);
|
|
1216
|
+
const initialMessages = initializeMessages?.() || [];
|
|
1217
|
+
const initialRunId = extractRunIdFromMessages(initialMessages);
|
|
1218
|
+
const _currentRunId = react.useRef(initialRunId);
|
|
1221
1219
|
const _onChunk = react.useRef(void 0);
|
|
1222
|
-
const [messages, setMessages] = react.useState(
|
|
1223
|
-
() => resolveInitialMessages(initializeMessages?.() || [])
|
|
1224
|
-
);
|
|
1220
|
+
const [messages, setMessages] = react.useState(() => resolveInitialMessages(initialMessages));
|
|
1225
1221
|
const [toolCallApprovals, setToolCallApprovals] = react.useState({});
|
|
1226
1222
|
const baseClient = useMastraClient();
|
|
1227
1223
|
const [isRunning, setIsRunning] = react.useState(false);
|
|
1228
1224
|
const generate = async ({
|
|
1229
1225
|
coreUserMessages,
|
|
1230
|
-
|
|
1226
|
+
runtimeContext,
|
|
1231
1227
|
threadId,
|
|
1232
1228
|
modelSettings,
|
|
1233
1229
|
signal,
|
|
@@ -1253,7 +1249,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1253
1249
|
const agent = clientWithAbort.getAgent(agentId);
|
|
1254
1250
|
const response = await agent.generate({
|
|
1255
1251
|
messages: coreUserMessages,
|
|
1256
|
-
runId:
|
|
1252
|
+
runId: uuid.v4(),
|
|
1257
1253
|
maxSteps,
|
|
1258
1254
|
modelSettings: {
|
|
1259
1255
|
frequencyPenalty,
|
|
@@ -1265,8 +1261,8 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1265
1261
|
topP
|
|
1266
1262
|
},
|
|
1267
1263
|
instructions,
|
|
1268
|
-
|
|
1269
|
-
...threadId ? { threadId, resourceId: agentId } : {},
|
|
1264
|
+
runtimeContext,
|
|
1265
|
+
...threadId ? { threadId, resourceId: resourceId || agentId } : {},
|
|
1270
1266
|
providerOptions
|
|
1271
1267
|
});
|
|
1272
1268
|
setIsRunning(false);
|
|
@@ -1281,7 +1277,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1281
1277
|
setMessages((prev) => [...prev, ...mastraUIMessages]);
|
|
1282
1278
|
}
|
|
1283
1279
|
};
|
|
1284
|
-
const stream = async ({ coreUserMessages,
|
|
1280
|
+
const stream = async ({ coreUserMessages, runtimeContext, threadId, onChunk, modelSettings, signal }) => {
|
|
1285
1281
|
const {
|
|
1286
1282
|
frequencyPenalty,
|
|
1287
1283
|
presencePenalty,
|
|
@@ -1301,7 +1297,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1301
1297
|
abortSignal: signal
|
|
1302
1298
|
});
|
|
1303
1299
|
const agent = clientWithAbort.getAgent(agentId);
|
|
1304
|
-
const runId =
|
|
1300
|
+
const runId = uuid.v4();
|
|
1305
1301
|
const response = await agent.stream({
|
|
1306
1302
|
messages: coreUserMessages,
|
|
1307
1303
|
runId,
|
|
@@ -1316,8 +1312,8 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1316
1312
|
topP
|
|
1317
1313
|
},
|
|
1318
1314
|
instructions,
|
|
1319
|
-
|
|
1320
|
-
...threadId ? { threadId, resourceId: agentId } : {},
|
|
1315
|
+
runtimeContext,
|
|
1316
|
+
...threadId ? { threadId, resourceId: resourceId || agentId } : {},
|
|
1321
1317
|
providerOptions,
|
|
1322
1318
|
requireToolApproval
|
|
1323
1319
|
});
|
|
@@ -1333,7 +1329,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1333
1329
|
};
|
|
1334
1330
|
const network = async ({
|
|
1335
1331
|
coreUserMessages,
|
|
1336
|
-
|
|
1332
|
+
runtimeContext,
|
|
1337
1333
|
threadId,
|
|
1338
1334
|
onNetworkChunk,
|
|
1339
1335
|
modelSettings,
|
|
@@ -1346,6 +1342,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1346
1342
|
abortSignal: signal
|
|
1347
1343
|
});
|
|
1348
1344
|
const agent = clientWithAbort.getAgent(agentId);
|
|
1345
|
+
const runId = uuid.v4();
|
|
1349
1346
|
const response = await agent.network({
|
|
1350
1347
|
messages: coreUserMessages,
|
|
1351
1348
|
maxSteps,
|
|
@@ -1358,9 +1355,9 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1358
1355
|
topK,
|
|
1359
1356
|
topP
|
|
1360
1357
|
},
|
|
1361
|
-
runId
|
|
1362
|
-
|
|
1363
|
-
...threadId ? { thread: threadId, resourceId: agentId } : {}
|
|
1358
|
+
runId,
|
|
1359
|
+
runtimeContext,
|
|
1360
|
+
...threadId ? { thread: threadId, resourceId: resourceId || agentId } : {}
|
|
1364
1361
|
});
|
|
1365
1362
|
const transformer = new AISdkNetworkTransformer();
|
|
1366
1363
|
await response.processDataStream({
|
|
@@ -1412,18 +1409,14 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1412
1409
|
};
|
|
1413
1410
|
const sendMessage = async ({ mode = "stream", ...args }) => {
|
|
1414
1411
|
const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
|
|
1415
|
-
const
|
|
1416
|
-
|
|
1417
|
-
coreUserMessages.push(...args.coreUserMessages);
|
|
1418
|
-
}
|
|
1419
|
-
const uiMessages = coreUserMessages.map(fromCoreUserMessageToUIMessage);
|
|
1420
|
-
setMessages((s) => [...s, ...uiMessages]);
|
|
1412
|
+
const messages2 = args.coreUserMessages ? [nextMessage, ...args.coreUserMessages] : [nextMessage];
|
|
1413
|
+
setMessages((s) => [...s, { role: "user", parts: [{ type: "text", text: args.message }] }]);
|
|
1421
1414
|
if (mode === "generate") {
|
|
1422
|
-
await generate({ ...args, coreUserMessages });
|
|
1415
|
+
await generate({ ...args, coreUserMessages: messages2 });
|
|
1423
1416
|
} else if (mode === "stream") {
|
|
1424
|
-
await stream({ ...args, coreUserMessages });
|
|
1417
|
+
await stream({ ...args, coreUserMessages: messages2 });
|
|
1425
1418
|
} else if (mode === "network") {
|
|
1426
|
-
await network({ ...args, coreUserMessages });
|
|
1419
|
+
await network({ ...args, coreUserMessages: messages2 });
|
|
1427
1420
|
}
|
|
1428
1421
|
};
|
|
1429
1422
|
return {
|