@mastra/react 0.0.0-refactor-agent-information-for-recomposable-ui-20251112151814 → 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 +198 -24
- package/dist/index.cjs +95 -103
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +95 -103
- 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 +12 -7
- 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,58 +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
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
## 0.0.22-alpha.0
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
### Patch Changes
|
|
10
13
|
|
|
11
|
-
-
|
|
12
|
-
|
|
14
|
+
- Updated dependencies []:
|
|
15
|
+
- @mastra/client-js@0.16.16-alpha.0
|
|
16
|
+
|
|
17
|
+
## 0.0.21
|
|
13
18
|
|
|
14
19
|
### Patch Changes
|
|
15
20
|
|
|
16
|
-
-
|
|
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
|
|
17
115
|
|
|
18
|
-
|
|
116
|
+
## 0.0.18-alpha.0
|
|
19
117
|
|
|
20
|
-
|
|
21
|
-
|
|
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
|
|
124
|
+
|
|
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
|
|
22
159
|
|
|
23
|
-
## 0.
|
|
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
|
|
24
184
|
|
|
25
185
|
### Patch Changes
|
|
26
186
|
|
|
27
187
|
- Updated dependencies []:
|
|
28
|
-
- @mastra/client-js@
|
|
188
|
+
- @mastra/client-js@0.16.7
|
|
29
189
|
|
|
30
|
-
## 0.
|
|
190
|
+
## 0.0.13-alpha.1
|
|
31
191
|
|
|
32
192
|
### Patch Changes
|
|
33
193
|
|
|
34
|
-
- Updated dependencies [
|
|
35
|
-
- @mastra/client-js@
|
|
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
|
|
36
203
|
|
|
37
|
-
## 0.
|
|
204
|
+
## 0.0.12
|
|
38
205
|
|
|
39
|
-
###
|
|
206
|
+
### Patch Changes
|
|
40
207
|
|
|
41
|
-
-
|
|
208
|
+
- Fix peerdependencies ([`eb7c1c8`](https://github.com/mastra-ai/mastra/commit/eb7c1c8c592d8fb16dfd250e337d9cdc73c8d5de))
|
|
42
209
|
|
|
43
|
-
-
|
|
210
|
+
- Updated dependencies [[`eb7c1c8`](https://github.com/mastra-ai/mastra/commit/eb7c1c8c592d8fb16dfd250e337d9cdc73c8d5de)]:
|
|
211
|
+
- @mastra/client-js@0.16.6
|
|
44
212
|
|
|
45
|
-
|
|
46
|
-
Made the return format of all methods that return db messages consistent. It's always `{ messages: MastraDBMessage[] }` now, and messages can be converted after that using `@mastra/ai-sdk/ui`'s `toAISdkV4/5Messages()` function
|
|
213
|
+
## 0.0.11
|
|
47
214
|
|
|
48
215
|
### Patch Changes
|
|
49
216
|
|
|
50
217
|
- Add tool call approval ([#8649](https://github.com/mastra-ai/mastra/pull/8649))
|
|
51
218
|
|
|
52
|
-
-
|
|
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))
|
|
53
227
|
|
|
54
|
-
- Updated dependencies [[`
|
|
55
|
-
- @mastra/client-js@
|
|
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
|
|
56
230
|
|
|
57
231
|
## 0.0.10
|
|
58
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
|
}
|
|
@@ -428,10 +442,11 @@ const toUIMessage = ({ chunk, conversation, metadata }) => {
|
|
|
428
442
|
const lastMessage = result[result.length - 1];
|
|
429
443
|
if (!lastMessage || lastMessage.role !== "assistant") return result;
|
|
430
444
|
const parts = lastMessage.parts.map((part) => {
|
|
431
|
-
if (
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
445
|
+
if (part.type === "text" && part.state === "streaming") {
|
|
446
|
+
return { ...part, state: "done" };
|
|
447
|
+
}
|
|
448
|
+
if (part.type === "reasoning" && part.state === "streaming") {
|
|
449
|
+
return { ...part, state: "done" };
|
|
435
450
|
}
|
|
436
451
|
return part;
|
|
437
452
|
});
|
|
@@ -603,23 +618,13 @@ const toAssistantUIMessage = (message) => {
|
|
|
603
618
|
};
|
|
604
619
|
}
|
|
605
620
|
if (part.type === "file") {
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
metadata: message.metadata
|
|
614
|
-
};
|
|
615
|
-
}
|
|
616
|
-
if (type === "image") {
|
|
617
|
-
return {
|
|
618
|
-
type,
|
|
619
|
-
image: part.url,
|
|
620
|
-
metadata: message.metadata
|
|
621
|
-
};
|
|
622
|
-
}
|
|
621
|
+
return {
|
|
622
|
+
type: "file",
|
|
623
|
+
mimeType: part.mediaType,
|
|
624
|
+
data: part.url,
|
|
625
|
+
// Use URL as data source
|
|
626
|
+
metadata: message.metadata
|
|
627
|
+
};
|
|
623
628
|
}
|
|
624
629
|
if (part.type === "dynamic-tool") {
|
|
625
630
|
const baseToolCall = {
|
|
@@ -655,6 +660,20 @@ const toAssistantUIMessage = (message) => {
|
|
|
655
660
|
}
|
|
656
661
|
return baseToolCall;
|
|
657
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
|
+
}
|
|
658
677
|
return {
|
|
659
678
|
type: "text",
|
|
660
679
|
text: "",
|
|
@@ -696,9 +715,7 @@ const toAssistantUIMessage = (message) => {
|
|
|
696
715
|
|
|
697
716
|
const resolveInitialMessages = (messages) => {
|
|
698
717
|
return messages.map((message) => {
|
|
699
|
-
const networkPart = message.parts.find(
|
|
700
|
-
(part) => typeof part === "object" && part !== null && "type" in part && part.type === "text" && "text" in part && typeof part.text === "string" && part.text.includes('"isNetwork":true')
|
|
701
|
-
);
|
|
718
|
+
const networkPart = message.parts.find((part) => part.type === "text" && part.text.includes('"isNetwork":true'));
|
|
702
719
|
if (networkPart && networkPart.type === "text") {
|
|
703
720
|
try {
|
|
704
721
|
const json = JSON.parse(networkPart.text);
|
|
@@ -741,7 +758,6 @@ const resolveInitialMessages = (messages) => {
|
|
|
741
758
|
childMessages,
|
|
742
759
|
result: finalResult?.text || ""
|
|
743
760
|
};
|
|
744
|
-
console.log("json", json);
|
|
745
761
|
const nextMessage = {
|
|
746
762
|
role: "assistant",
|
|
747
763
|
parts: [
|
|
@@ -769,6 +785,18 @@ const resolveInitialMessages = (messages) => {
|
|
|
769
785
|
return message;
|
|
770
786
|
}
|
|
771
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
|
+
}
|
|
772
800
|
return message;
|
|
773
801
|
});
|
|
774
802
|
};
|
|
@@ -1172,63 +1200,30 @@ class AISdkNetworkTransformer {
|
|
|
1172
1200
|
};
|
|
1173
1201
|
}
|
|
1174
1202
|
|
|
1175
|
-
const
|
|
1176
|
-
const
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
case "text": {
|
|
1185
|
-
return {
|
|
1186
|
-
type: "text",
|
|
1187
|
-
text: part.text
|
|
1188
|
-
};
|
|
1189
|
-
}
|
|
1190
|
-
case "image": {
|
|
1191
|
-
const url = typeof part.image === "string" ? part.image : part.image instanceof URL ? part.image.toString() : "";
|
|
1192
|
-
return {
|
|
1193
|
-
type: "file",
|
|
1194
|
-
mediaType: part.mimeType ?? "image/*",
|
|
1195
|
-
url
|
|
1196
|
-
};
|
|
1197
|
-
}
|
|
1198
|
-
case "file": {
|
|
1199
|
-
const url = typeof part.data === "string" ? part.data : part.data instanceof URL ? part.data.toString() : "";
|
|
1200
|
-
return {
|
|
1201
|
-
type: "file",
|
|
1202
|
-
mediaType: part.mimeType,
|
|
1203
|
-
url,
|
|
1204
|
-
...part.filename !== void 0 ? { filename: part.filename } : {}
|
|
1205
|
-
};
|
|
1206
|
-
}
|
|
1207
|
-
default: {
|
|
1208
|
-
const exhaustiveCheck = part;
|
|
1209
|
-
throw new Error(`Unhandled content part type: ${exhaustiveCheck.type}`);
|
|
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
|
+
}
|
|
1210
1212
|
}
|
|
1211
1213
|
}
|
|
1212
|
-
|
|
1213
|
-
return {
|
|
1214
|
-
id,
|
|
1215
|
-
role: "user",
|
|
1216
|
-
parts
|
|
1214
|
+
return void 0;
|
|
1217
1215
|
};
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
const
|
|
1221
|
-
const _currentRunId = react.useRef(void 0);
|
|
1216
|
+
const initialMessages = initializeMessages?.() || [];
|
|
1217
|
+
const initialRunId = extractRunIdFromMessages(initialMessages);
|
|
1218
|
+
const _currentRunId = react.useRef(initialRunId);
|
|
1222
1219
|
const _onChunk = react.useRef(void 0);
|
|
1223
|
-
const [messages, setMessages] = react.useState(
|
|
1224
|
-
() => resolveInitialMessages(initializeMessages?.() || [])
|
|
1225
|
-
);
|
|
1220
|
+
const [messages, setMessages] = react.useState(() => resolveInitialMessages(initialMessages));
|
|
1226
1221
|
const [toolCallApprovals, setToolCallApprovals] = react.useState({});
|
|
1227
1222
|
const baseClient = useMastraClient();
|
|
1228
1223
|
const [isRunning, setIsRunning] = react.useState(false);
|
|
1229
1224
|
const generate = async ({
|
|
1230
1225
|
coreUserMessages,
|
|
1231
|
-
|
|
1226
|
+
runtimeContext,
|
|
1232
1227
|
threadId,
|
|
1233
1228
|
modelSettings,
|
|
1234
1229
|
signal,
|
|
@@ -1254,7 +1249,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1254
1249
|
const agent = clientWithAbort.getAgent(agentId);
|
|
1255
1250
|
const response = await agent.generate({
|
|
1256
1251
|
messages: coreUserMessages,
|
|
1257
|
-
runId:
|
|
1252
|
+
runId: uuid.v4(),
|
|
1258
1253
|
maxSteps,
|
|
1259
1254
|
modelSettings: {
|
|
1260
1255
|
frequencyPenalty,
|
|
@@ -1266,8 +1261,8 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1266
1261
|
topP
|
|
1267
1262
|
},
|
|
1268
1263
|
instructions,
|
|
1269
|
-
|
|
1270
|
-
...threadId ? { threadId, resourceId: agentId } : {},
|
|
1264
|
+
runtimeContext,
|
|
1265
|
+
...threadId ? { threadId, resourceId: resourceId || agentId } : {},
|
|
1271
1266
|
providerOptions
|
|
1272
1267
|
});
|
|
1273
1268
|
setIsRunning(false);
|
|
@@ -1282,7 +1277,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1282
1277
|
setMessages((prev) => [...prev, ...mastraUIMessages]);
|
|
1283
1278
|
}
|
|
1284
1279
|
};
|
|
1285
|
-
const stream = async ({ coreUserMessages,
|
|
1280
|
+
const stream = async ({ coreUserMessages, runtimeContext, threadId, onChunk, modelSettings, signal }) => {
|
|
1286
1281
|
const {
|
|
1287
1282
|
frequencyPenalty,
|
|
1288
1283
|
presencePenalty,
|
|
@@ -1302,7 +1297,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1302
1297
|
abortSignal: signal
|
|
1303
1298
|
});
|
|
1304
1299
|
const agent = clientWithAbort.getAgent(agentId);
|
|
1305
|
-
const runId =
|
|
1300
|
+
const runId = uuid.v4();
|
|
1306
1301
|
const response = await agent.stream({
|
|
1307
1302
|
messages: coreUserMessages,
|
|
1308
1303
|
runId,
|
|
@@ -1317,8 +1312,8 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1317
1312
|
topP
|
|
1318
1313
|
},
|
|
1319
1314
|
instructions,
|
|
1320
|
-
|
|
1321
|
-
...threadId ? { threadId, resourceId: agentId } : {},
|
|
1315
|
+
runtimeContext,
|
|
1316
|
+
...threadId ? { threadId, resourceId: resourceId || agentId } : {},
|
|
1322
1317
|
providerOptions,
|
|
1323
1318
|
requireToolApproval
|
|
1324
1319
|
});
|
|
@@ -1334,7 +1329,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1334
1329
|
};
|
|
1335
1330
|
const network = async ({
|
|
1336
1331
|
coreUserMessages,
|
|
1337
|
-
|
|
1332
|
+
runtimeContext,
|
|
1338
1333
|
threadId,
|
|
1339
1334
|
onNetworkChunk,
|
|
1340
1335
|
modelSettings,
|
|
@@ -1347,6 +1342,7 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1347
1342
|
abortSignal: signal
|
|
1348
1343
|
});
|
|
1349
1344
|
const agent = clientWithAbort.getAgent(agentId);
|
|
1345
|
+
const runId = uuid.v4();
|
|
1350
1346
|
const response = await agent.network({
|
|
1351
1347
|
messages: coreUserMessages,
|
|
1352
1348
|
maxSteps,
|
|
@@ -1359,9 +1355,9 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1359
1355
|
topK,
|
|
1360
1356
|
topP
|
|
1361
1357
|
},
|
|
1362
|
-
runId
|
|
1363
|
-
|
|
1364
|
-
...threadId ? { thread: threadId, resourceId: agentId } : {}
|
|
1358
|
+
runId,
|
|
1359
|
+
runtimeContext,
|
|
1360
|
+
...threadId ? { thread: threadId, resourceId: resourceId || agentId } : {}
|
|
1365
1361
|
});
|
|
1366
1362
|
const transformer = new AISdkNetworkTransformer();
|
|
1367
1363
|
await response.processDataStream({
|
|
@@ -1413,18 +1409,14 @@ const useChat = ({ agentId, initializeMessages }) => {
|
|
|
1413
1409
|
};
|
|
1414
1410
|
const sendMessage = async ({ mode = "stream", ...args }) => {
|
|
1415
1411
|
const nextMessage = { role: "user", content: [{ type: "text", text: args.message }] };
|
|
1416
|
-
const
|
|
1417
|
-
|
|
1418
|
-
coreUserMessages.push(...args.coreUserMessages);
|
|
1419
|
-
}
|
|
1420
|
-
const uiMessages = coreUserMessages.map(fromCoreUserMessageToUIMessage);
|
|
1421
|
-
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 }] }]);
|
|
1422
1414
|
if (mode === "generate") {
|
|
1423
|
-
await generate({ ...args, coreUserMessages });
|
|
1415
|
+
await generate({ ...args, coreUserMessages: messages2 });
|
|
1424
1416
|
} else if (mode === "stream") {
|
|
1425
|
-
await stream({ ...args, coreUserMessages });
|
|
1417
|
+
await stream({ ...args, coreUserMessages: messages2 });
|
|
1426
1418
|
} else if (mode === "network") {
|
|
1427
|
-
await network({ ...args, coreUserMessages });
|
|
1419
|
+
await network({ ...args, coreUserMessages: messages2 });
|
|
1428
1420
|
}
|
|
1429
1421
|
};
|
|
1430
1422
|
return {
|