@mastra/ai-sdk 0.0.0-toolOptionTypes-20250917085558 → 0.0.0-top-level-fix-20251211111608
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 +821 -3
- package/README.md +125 -1
- package/dist/__tests__/__fixtures__/network.stream.d.ts +2329 -0
- package/dist/__tests__/__fixtures__/network.stream.d.ts.map +1 -0
- package/dist/chat-route.d.ts +101 -0
- package/dist/chat-route.d.ts.map +1 -0
- package/dist/convert-messages.d.ts +10 -0
- package/dist/convert-messages.d.ts.map +1 -0
- package/dist/convert-streams.d.ts +81 -0
- package/dist/convert-streams.d.ts.map +1 -0
- package/dist/helpers.d.ts +43 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/index.cjs +2029 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +13 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2022 -12
- package/dist/index.js.map +1 -1
- package/dist/middleware.d.ts +157 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/network-route.d.ts +75 -0
- package/dist/network-route.d.ts.map +1 -0
- package/dist/to-ai-sdk-format.d.ts +16 -0
- package/dist/to-ai-sdk-format.d.ts.map +1 -0
- package/dist/transformers.d.ts +259 -0
- package/dist/transformers.d.ts.map +1 -0
- package/dist/ui.cjs +16 -0
- package/dist/ui.cjs.map +1 -0
- package/dist/ui.d.ts +2 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +13 -0
- package/dist/ui.js.map +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/workflow-route.d.ts +77 -0
- package/dist/workflow-route.d.ts.map +1 -0
- package/package.json +28 -8
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @mastra/ai-sdk
|
|
2
2
|
|
|
3
|
-
`@mastra/ai-sdk`
|
|
3
|
+
The recommended way of using Mastra and AI SDK together is by installing the `@mastra/ai-sdk` package. `@mastra/ai-sdk` provides custom API routes and utilities for streaming Mastra agents in AI SDK-compatible formats. Including chat, workflow, and network route handlers, along with utilities and exported types for UI integrations.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -47,9 +47,133 @@ After defining a dynamic route with `:agentId` you can use the `useChat()` hook
|
|
|
47
47
|
|
|
48
48
|
```typescript
|
|
49
49
|
type MyMessage = {};
|
|
50
|
+
|
|
50
51
|
const { error, status, sendMessage, messages, regenerate, stop } = useChat<MyMessage>({
|
|
51
52
|
transport: new DefaultChatTransport({
|
|
52
53
|
api: 'http://localhost:4111/chat/weatherAgent',
|
|
53
54
|
}),
|
|
54
55
|
});
|
|
55
56
|
```
|
|
57
|
+
|
|
58
|
+
### Workflow route
|
|
59
|
+
|
|
60
|
+
Stream a workflow in AI SDK-compatible format.
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { workflowRoute } from '@mastra/ai-sdk';
|
|
64
|
+
|
|
65
|
+
export const mastra = new Mastra({
|
|
66
|
+
server: {
|
|
67
|
+
apiRoutes: [
|
|
68
|
+
workflowRoute({
|
|
69
|
+
path: '/workflow',
|
|
70
|
+
agent: 'weatherAgent',
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Network route
|
|
78
|
+
|
|
79
|
+
Stream agent networks (routing + nested agent/workflow/tool executions) in AI SDK-compatible format.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { networkRoute } from '@mastra/ai-sdk';
|
|
83
|
+
|
|
84
|
+
export const mastra = new Mastra({
|
|
85
|
+
server: {
|
|
86
|
+
apiRoutes: [
|
|
87
|
+
networkRoute({
|
|
88
|
+
path: '/network',
|
|
89
|
+
agent: 'weatherAgent',
|
|
90
|
+
}),
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Framework-agnostic handlers
|
|
97
|
+
|
|
98
|
+
For use outside of the Mastra server (e.g., Next.js App Router, Express), you can use the standalone handler functions directly. These handlers return a `ReadableStream` that you can wrap with `createUIMessageStreamResponse`:
|
|
99
|
+
|
|
100
|
+
### handleChatStream
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { handleChatStream } from '@mastra/ai-sdk';
|
|
104
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
105
|
+
import { mastra } from '@/src/mastra';
|
|
106
|
+
|
|
107
|
+
export async function POST(req: Request) {
|
|
108
|
+
const params = await req.json();
|
|
109
|
+
const stream = await handleChatStream({
|
|
110
|
+
mastra,
|
|
111
|
+
agentId: 'weatherAgent',
|
|
112
|
+
params,
|
|
113
|
+
});
|
|
114
|
+
return createUIMessageStreamResponse({ stream });
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### handleWorkflowStream
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { handleWorkflowStream } from '@mastra/ai-sdk';
|
|
122
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
123
|
+
import { mastra } from '@/src/mastra';
|
|
124
|
+
|
|
125
|
+
export async function POST(req: Request) {
|
|
126
|
+
const params = await req.json();
|
|
127
|
+
const stream = await handleWorkflowStream({
|
|
128
|
+
mastra,
|
|
129
|
+
workflowId: 'myWorkflow',
|
|
130
|
+
params,
|
|
131
|
+
});
|
|
132
|
+
return createUIMessageStreamResponse({ stream });
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### handleNetworkStream
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { handleNetworkStream } from '@mastra/ai-sdk';
|
|
140
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
141
|
+
import { mastra } from '@/src/mastra';
|
|
142
|
+
|
|
143
|
+
export async function POST(req: Request) {
|
|
144
|
+
const params = await req.json();
|
|
145
|
+
const stream = await handleNetworkStream({
|
|
146
|
+
mastra,
|
|
147
|
+
agentId: 'routingAgent',
|
|
148
|
+
params,
|
|
149
|
+
});
|
|
150
|
+
return createUIMessageStreamResponse({ stream });
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Manual transformation
|
|
155
|
+
|
|
156
|
+
If you have a raw Mastra `stream`, you can manually transform it to AI SDK UI message parts:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { toAISdkFormat } from '@mastra/ai-sdk';
|
|
160
|
+
import { createUIMessageStream, createUIMessageStreamResponse } from 'ai';
|
|
161
|
+
|
|
162
|
+
export async function POST(req: Request) {
|
|
163
|
+
const { messages } = await req.json();
|
|
164
|
+
const agent = mastra.getAgent('weatherAgent');
|
|
165
|
+
const stream = await agent.stream(messages);
|
|
166
|
+
|
|
167
|
+
// deduplicate messages https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages
|
|
168
|
+
const uiMessageStream = createUIMessageStream({
|
|
169
|
+
originalMessages: messages,
|
|
170
|
+
execute: async ({ writer }) => {
|
|
171
|
+
for await (const part of toAISdkFormat(stream, { from: 'agent' })!) {
|
|
172
|
+
writer.write(part);
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
178
|
+
}
|
|
179
|
+
```
|