@mastra/ai-sdk 1.10.0 → 1.10.1-alpha.1
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/LICENSE.md +6 -4
- package/README.md +7 -261
- package/dist/__tests__/__fixtures__/network.stream.d.ts +684 -684
- package/dist/__tests__/__fixtures__/network.stream.d.ts.map +1 -1
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/transformers.d.ts +59 -59
- package/dist/transformers.d.ts.map +1 -1
- package/dist/utils.d.ts +2 -2
- package/dist/utils.d.ts.map +1 -1
- package/package.json +13 -14
- package/CHANGELOG.md +0 -2473
package/LICENSE.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
Portions of this software are licensed as follows:
|
|
2
2
|
|
|
3
|
-
- All content that resides under any directory named
|
|
3
|
+
- All content that resides under any directory named `ee/` within this
|
|
4
4
|
repository, including but not limited to:
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
|
|
5
|
+
- `@mastra/core/auth/ee`
|
|
6
|
+
- `@mastra/core/agent-builder/ee`
|
|
7
|
+
- `@mastra/editor/ee`
|
|
8
|
+
|
|
9
|
+
is licensed under the license defined in [`ee/LICENSE`](https://github.com/mastra-ai/mastra/blob/main/ee/LICENSE).
|
|
8
10
|
|
|
9
11
|
- All third-party components incorporated into the Mastra Software are
|
|
10
12
|
licensed under the original license provided by the owner of the
|
package/README.md
CHANGED
|
@@ -26,269 +26,15 @@ export const mastra = new Mastra({
|
|
|
26
26
|
});
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
## Documentation
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
export const mastra = new Mastra({
|
|
35
|
-
server: {
|
|
36
|
-
apiRoutes: [
|
|
37
|
-
chatRoute({
|
|
38
|
-
path: '/chat',
|
|
39
|
-
agent: 'weatherAgent',
|
|
40
|
-
}),
|
|
41
|
-
],
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
After defining a dynamic route with `:agentId` you can use the `useChat()` hook like so:
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
type MyMessage = {};
|
|
50
|
-
|
|
51
|
-
const { error, status, sendMessage, messages, regenerate, stop } = useChat<MyMessage>({
|
|
52
|
-
transport: new DefaultChatTransport({
|
|
53
|
-
api: 'http://localhost:4111/chat/weatherAgent',
|
|
54
|
-
}),
|
|
55
|
-
});
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
`chatRoute()` forwards the incoming request `AbortSignal` to `agent.stream()`. If the client disconnects, Mastra aborts the in-flight generation. If you need generation to continue and persist server-side after disconnect, build a custom route around `agent.stream()`, avoid passing the request signal, and call `consumeStream()` on the returned `MastraModelOutput`.
|
|
59
|
-
|
|
60
|
-
### Workflow route
|
|
61
|
-
|
|
62
|
-
Stream a workflow in AI SDK-compatible format.
|
|
63
|
-
|
|
64
|
-
```typescript
|
|
65
|
-
import { workflowRoute } from '@mastra/ai-sdk';
|
|
66
|
-
|
|
67
|
-
export const mastra = new Mastra({
|
|
68
|
-
server: {
|
|
69
|
-
apiRoutes: [
|
|
70
|
-
workflowRoute({
|
|
71
|
-
path: '/workflow',
|
|
72
|
-
agent: 'weatherAgent',
|
|
73
|
-
}),
|
|
74
|
-
],
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
### Network route
|
|
80
|
-
|
|
81
|
-
Stream agent networks (routing + nested agent/workflow/tool executions) in AI SDK-compatible format.
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
import { networkRoute } from '@mastra/ai-sdk';
|
|
85
|
-
|
|
86
|
-
export const mastra = new Mastra({
|
|
87
|
-
server: {
|
|
88
|
-
apiRoutes: [
|
|
89
|
-
networkRoute({
|
|
90
|
-
path: '/network',
|
|
91
|
-
agent: 'weatherAgent',
|
|
92
|
-
}),
|
|
93
|
-
],
|
|
94
|
-
},
|
|
95
|
-
});
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## Framework-agnostic handlers
|
|
99
|
-
|
|
100
|
-
For use outside the Mastra server (e.g., Next.js App Router, Express), you can use the standalone handler functions directly. These handlers return a compatibility `ReadableStream` that can be passed to AI SDK response helpers like `createUIMessageStreamResponse` and `pipeUIMessageStreamToResponse`:
|
|
101
|
-
|
|
102
|
-
### handleChatStream
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
import { handleChatStream } from '@mastra/ai-sdk';
|
|
106
|
-
import { createUIMessageStreamResponse } from 'ai';
|
|
107
|
-
import { mastra } from '@/src/mastra';
|
|
108
|
-
|
|
109
|
-
export async function POST(req: Request) {
|
|
110
|
-
const params = await req.json();
|
|
111
|
-
const stream = await handleChatStream({
|
|
112
|
-
mastra,
|
|
113
|
-
agentId: 'weatherAgent',
|
|
114
|
-
params,
|
|
115
|
-
});
|
|
116
|
-
return createUIMessageStreamResponse({ stream });
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### Smooth agent streams
|
|
121
|
-
|
|
122
|
-
Use the experimental `smoothStream()` transform to pace uneven provider deltas before they are converted to AI SDK UI chunks. The transform only reshapes text and reasoning deltas; tool calls, sources, metadata, and stream control events keep their ordering.
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
import { handleChatStream, smoothStream } from '@mastra/ai-sdk';
|
|
126
|
-
import { createUIMessageStreamResponse } from 'ai';
|
|
127
|
-
import { mastra } from '@/src/mastra';
|
|
128
|
-
|
|
129
|
-
export async function POST(req: Request) {
|
|
130
|
-
const params = await req.json();
|
|
131
|
-
const stream = await handleChatStream({
|
|
132
|
-
mastra,
|
|
133
|
-
agentId: 'weatherAgent',
|
|
134
|
-
params,
|
|
135
|
-
experimentalTransform: smoothStream({
|
|
136
|
-
delayInMs: 20,
|
|
137
|
-
chunking: 'word',
|
|
138
|
-
}),
|
|
139
|
-
});
|
|
31
|
+
- [AI SDK UI integration guide](https://mastra.ai/integrations/agentic-ui/ai-sdk-ui)
|
|
32
|
+
- [AI SDK reference](https://mastra.ai/reference/ai-sdk/overview)
|
|
140
33
|
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
The transform can also be configured once for a registered route:
|
|
146
|
-
|
|
147
|
-
```typescript
|
|
148
|
-
chatRoute({
|
|
149
|
-
path: '/chat',
|
|
150
|
-
agent: 'weatherAgent',
|
|
151
|
-
experimentalTransform: smoothStream({ delayInMs: 20 }),
|
|
152
|
-
});
|
|
153
|
-
```
|
|
34
|
+
## Changelog
|
|
154
35
|
|
|
155
|
-
|
|
36
|
+
See the [package changelog](https://github.com/mastra-ai/mastra/blob/main/client-sdks/ai-sdk/CHANGELOG.md) for version history and release notes.
|
|
156
37
|
|
|
157
|
-
|
|
38
|
+
## Support
|
|
158
39
|
|
|
159
|
-
|
|
160
|
-
const result = await agent.stream('Explain how rainbows form', {
|
|
161
|
-
experimentalTransform: smoothStream({ delayInMs: 20 }),
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
for await (const chunk of result.fullStream) {
|
|
165
|
-
// Text and reasoning deltas are smoothed; other chunks preserve their order.
|
|
166
|
-
}
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### handleWorkflowStream
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
import { handleWorkflowStream } from '@mastra/ai-sdk';
|
|
173
|
-
import { createUIMessageStreamResponse } from 'ai';
|
|
174
|
-
import { mastra } from '@/src/mastra';
|
|
175
|
-
|
|
176
|
-
export async function POST(req: Request) {
|
|
177
|
-
const params = await req.json();
|
|
178
|
-
const stream = await handleWorkflowStream({
|
|
179
|
-
mastra,
|
|
180
|
-
workflowId: 'myWorkflow',
|
|
181
|
-
params,
|
|
182
|
-
});
|
|
183
|
-
return createUIMessageStreamResponse({ stream });
|
|
184
|
-
}
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
### handleNetworkStream
|
|
188
|
-
|
|
189
|
-
Pass AI SDK `UIMessage[]` from your installed `ai` version so TypeScript can infer the correct stream overload.
|
|
190
|
-
|
|
191
|
-
Handlers keep the existing v5/default behavior. If your app is typed against `ai@6`, pass `version: 'v6'`.
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
import { handleNetworkStream } from '@mastra/ai-sdk';
|
|
195
|
-
import { createUIMessageStreamResponse, type UIMessage } from 'ai';
|
|
196
|
-
import { mastra } from '@/src/mastra';
|
|
197
|
-
|
|
198
|
-
export async function POST(req: Request) {
|
|
199
|
-
const params = (await req.json()) as { messages: UIMessage[] };
|
|
200
|
-
const stream = await handleNetworkStream({
|
|
201
|
-
mastra,
|
|
202
|
-
agentId: 'routingAgent',
|
|
203
|
-
version: 'v6',
|
|
204
|
-
params,
|
|
205
|
-
});
|
|
206
|
-
return createUIMessageStreamResponse({ stream });
|
|
207
|
-
}
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
## Agent versioning
|
|
211
|
-
|
|
212
|
-
All route handlers and standalone stream functions accept an optional `agentVersion` parameter to target a specific agent version. This requires the [Editor](https://mastra.ai/docs/editor/overview) to be configured.
|
|
213
|
-
|
|
214
|
-
Pass a version ID or resolve by status:
|
|
215
|
-
|
|
216
|
-
```typescript
|
|
217
|
-
chatRoute({
|
|
218
|
-
path: '/chat',
|
|
219
|
-
agent: 'weatherAgent',
|
|
220
|
-
agentVersion: { status: 'published' },
|
|
221
|
-
});
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
For route handlers (`chatRoute`, `networkRoute`), callers can also override the version at request time with query parameters: `?versionId=<id>` or `?status=draft|published`. Query parameters take precedence over the static `agentVersion` option.
|
|
225
|
-
|
|
226
|
-
The standalone handlers (`handleChatStream`, `handleNetworkStream`) accept `agentVersion` directly:
|
|
227
|
-
|
|
228
|
-
```typescript
|
|
229
|
-
const stream = await handleChatStream({
|
|
230
|
-
mastra,
|
|
231
|
-
agentId: 'weatherAgent',
|
|
232
|
-
agentVersion: { versionId: 'ver_abc123' },
|
|
233
|
-
params,
|
|
234
|
-
});
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
## Manual transformation
|
|
238
|
-
|
|
239
|
-
If you have a raw Mastra `stream`, you can manually transform it to AI SDK UI message parts:
|
|
240
|
-
|
|
241
|
-
Use `toAISdkStream` for both versions. If your app is typed against `ai@6`, pass `version: 'v6'`.
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
import { toAISdkStream } from '@mastra/ai-sdk';
|
|
245
|
-
import { createUIMessageStream, createUIMessageStreamResponse } from 'ai';
|
|
246
|
-
|
|
247
|
-
export async function POST(req: Request) {
|
|
248
|
-
const { messages } = await req.json();
|
|
249
|
-
const agent = mastra.getAgent('weatherAgent');
|
|
250
|
-
const stream = await agent.stream(messages);
|
|
251
|
-
|
|
252
|
-
// deduplicate messages https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages
|
|
253
|
-
const uiMessageStream = createUIMessageStream({
|
|
254
|
-
originalMessages: messages,
|
|
255
|
-
execute: async ({ writer }) => {
|
|
256
|
-
for await (const part of toAISdkStream(stream, { from: 'agent' })) {
|
|
257
|
-
writer.write(part);
|
|
258
|
-
}
|
|
259
|
-
},
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
263
|
-
}
|
|
264
|
-
```
|
|
265
|
-
|
|
266
|
-
For AI SDK v6, select the v6 stream contract explicitly:
|
|
267
|
-
|
|
268
|
-
```typescript
|
|
269
|
-
const uiMessageStream = createUIMessageStream({
|
|
270
|
-
originalMessages: messages,
|
|
271
|
-
execute: async ({ writer }) => {
|
|
272
|
-
for await (const part of toAISdkStream(stream, {
|
|
273
|
-
from: 'agent',
|
|
274
|
-
version: 'v6',
|
|
275
|
-
})) {
|
|
276
|
-
writer.write(part);
|
|
277
|
-
}
|
|
278
|
-
},
|
|
279
|
-
});
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
## Loading stored messages
|
|
283
|
-
|
|
284
|
-
Use `toAISdkMessages` from `@mastra/ai-sdk/ui` to convert stored Mastra messages for `useChat()` and other AI SDK UI hooks.
|
|
285
|
-
|
|
286
|
-
The helper keeps the existing v5/default behavior. If your app is typed against `ai@6`, pass `version: 'v6'`.
|
|
287
|
-
That uses the MessageList AI SDK v6 UI output path. MessageList input detection and ingestion remain unchanged.
|
|
288
|
-
|
|
289
|
-
```typescript
|
|
290
|
-
import { toAISdkMessages } from '@mastra/ai-sdk/ui';
|
|
291
|
-
|
|
292
|
-
const v5Messages = toAISdkMessages(storedMessages);
|
|
293
|
-
const v6Messages = toAISdkMessages(storedMessages, { version: 'v6' });
|
|
294
|
-
```
|
|
40
|
+
We have an [open community Discord](https://discord.gg/mastra-ai). Come and say hello and let us know if you have any questions or need any help getting things running.
|