@redplanethq/sdk 0.1.17 → 1.2.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/CHANGELOG.md +1657 -0
- package/LICENSE.md +30 -0
- package/README.md +159 -183
- package/dist/__tests__/__fixtures__/network.stream.d.ts +2329 -0
- package/dist/__tests__/__fixtures__/network.stream.d.ts.map +1 -0
- package/dist/_types/@ai-sdk_provider/dist/index.d.ts +1725 -0
- package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +10332 -0
- package/dist/_types/@internal_ai-v6/dist/index.d.ts +13493 -0
- package/dist/chat-route.d.ts +117 -0
- package/dist/chat-route.d.ts.map +1 -0
- package/dist/chunk-3YJ25IUJ.js +79 -0
- package/dist/chunk-3YJ25IUJ.js.map +1 -0
- package/dist/chunk-DGUM43GV.js +10 -0
- package/dist/chunk-DGUM43GV.js.map +1 -0
- package/dist/chunk-HDI4ZP7N.js +79 -0
- package/dist/chunk-HDI4ZP7N.js.map +1 -0
- package/dist/chunk-HMOX24BQ.js +439 -0
- package/dist/chunk-HMOX24BQ.js.map +1 -0
- package/dist/chunk-IV3RKEE4.js +439 -0
- package/dist/chunk-IV3RKEE4.js.map +1 -0
- package/dist/chunk-JEQ2X3Z6.cjs +12 -0
- package/dist/chunk-JEQ2X3Z6.cjs.map +1 -0
- package/dist/chunk-PEZAMDZJ.cjs +84 -0
- package/dist/chunk-PEZAMDZJ.cjs.map +1 -0
- package/dist/chunk-VJW66KXQ.cjs +441 -0
- package/dist/chunk-VJW66KXQ.cjs.map +1 -0
- package/dist/chunk-WFARZCDO.cjs +84 -0
- package/dist/chunk-WFARZCDO.cjs.map +1 -0
- package/dist/chunk-X72VW7EY.cjs +441 -0
- package/dist/chunk-X72VW7EY.cjs.map +1 -0
- package/dist/convert-messages.d.ts +90 -0
- package/dist/convert-messages.d.ts.map +1 -0
- package/dist/convert-streams.d.ts +59 -0
- package/dist/convert-streams.d.ts.map +1 -0
- package/dist/helpers.d.ts +59 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/index.cjs +14385 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +14 -1245
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14289 -804
- package/dist/index.js.map +1 -1
- package/dist/middleware.d.ts +159 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/network-route.d.ts +84 -0
- package/dist/network-route.d.ts.map +1 -0
- package/dist/public-types.d.ts +9 -0
- package/dist/public-types.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/token-APYSY3BW-CBIBV5F2.js +61 -0
- package/dist/token-APYSY3BW-CBIBV5F2.js.map +1 -0
- package/dist/token-APYSY3BW-INR55PKP.js +61 -0
- package/dist/token-APYSY3BW-INR55PKP.js.map +1 -0
- package/dist/token-APYSY3BW-M2K6HVOG.cjs +63 -0
- package/dist/token-APYSY3BW-M2K6HVOG.cjs.map +1 -0
- package/dist/token-APYSY3BW-Y5VD4L7Z.cjs +63 -0
- package/dist/token-APYSY3BW-Y5VD4L7Z.cjs.map +1 -0
- package/dist/token-util-RMHT2CPJ-3LCBPXJ6.cjs +10 -0
- package/dist/token-util-RMHT2CPJ-3LCBPXJ6.cjs.map +1 -0
- package/dist/token-util-RMHT2CPJ-4YTBAZ23.cjs +10 -0
- package/dist/token-util-RMHT2CPJ-4YTBAZ23.cjs.map +1 -0
- package/dist/token-util-RMHT2CPJ-HCMQIDTV.js +8 -0
- package/dist/token-util-RMHT2CPJ-HCMQIDTV.js.map +1 -0
- package/dist/token-util-RMHT2CPJ-IOKQM5O3.js +8 -0
- package/dist/token-util-RMHT2CPJ-IOKQM5O3.js.map +1 -0
- package/dist/transformers.d.ts +315 -0
- package/dist/transformers.d.ts.map +1 -0
- package/dist/ui.cjs +15 -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 +12 -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 +93 -0
- package/dist/workflow-route.d.ts.map +1 -0
- package/package.json +65 -31
- package/ui.d.ts +1 -0
- package/LICENSE +0 -44
- package/dist/index.d.mts +0 -1245
- package/dist/index.mjs +0 -822
- package/dist/index.mjs.map +0 -1
package/LICENSE.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Portions of this software are licensed as follows:
|
|
2
|
+
|
|
3
|
+
- All content that resides under any directory named "ee/" within this
|
|
4
|
+
repository, including but not limited to:
|
|
5
|
+
- `packages/core/src/auth/ee/`
|
|
6
|
+
- `packages/server/src/server/auth/ee/`
|
|
7
|
+
is licensed under the license defined in `ee/LICENSE`.
|
|
8
|
+
|
|
9
|
+
- All third-party components incorporated into the Mastra Software are
|
|
10
|
+
licensed under the original license provided by the owner of the
|
|
11
|
+
applicable component.
|
|
12
|
+
|
|
13
|
+
- Content outside of the above-mentioned directories or restrictions is
|
|
14
|
+
available under the "Apache License 2.0" as defined below.
|
|
15
|
+
|
|
16
|
+
# Apache License 2.0
|
|
17
|
+
|
|
18
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
19
|
+
|
|
20
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
21
|
+
you may not use this file except in compliance with the License.
|
|
22
|
+
You may obtain a copy of the License at
|
|
23
|
+
|
|
24
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
25
|
+
|
|
26
|
+
Unless required by applicable law or agreed to in writing, software
|
|
27
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
28
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
29
|
+
See the License for the specific language governing permissions and
|
|
30
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,228 +1,204 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mastra/ai-sdk
|
|
2
2
|
|
|
3
|
-
The
|
|
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
|
-
##
|
|
6
|
-
|
|
7
|
-
The Core integration system uses a CLI-based approach where each integration is a command-line tool that responds to specific events. This makes integrations portable, testable, and easy to debug.
|
|
8
|
-
|
|
9
|
-
### Integration Event Types
|
|
10
|
-
|
|
11
|
-
Each integration CLI handles 5 core event types:
|
|
12
|
-
|
|
13
|
-
#### 1. `spec`
|
|
14
|
-
|
|
15
|
-
Returns the integration's metadata and configuration.
|
|
16
|
-
|
|
17
|
-
**Usage:**
|
|
5
|
+
## Installation
|
|
18
6
|
|
|
19
7
|
```bash
|
|
20
|
-
|
|
8
|
+
npm install @mastra/ai-sdk
|
|
21
9
|
```
|
|
22
10
|
|
|
23
|
-
|
|
11
|
+
## Usage
|
|
24
12
|
|
|
25
|
-
|
|
13
|
+
If you want to use dynamic agents you can use a path with `:agentId`.
|
|
26
14
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
15
|
+
```typescript
|
|
16
|
+
import { chatRoute } from '@mastra/ai-sdk';
|
|
17
|
+
|
|
18
|
+
export const mastra = new Mastra({
|
|
19
|
+
server: {
|
|
20
|
+
apiRoutes: [
|
|
21
|
+
chatRoute({
|
|
22
|
+
path: '/chat/:agentId',
|
|
23
|
+
}),
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
});
|
|
33
27
|
```
|
|
34
28
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
#### 3. `identify`
|
|
29
|
+
Or you can create a fixed route (i.e. `/chat`):
|
|
38
30
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
31
|
+
```typescript
|
|
32
|
+
import { chatRoute } from '@mastra/ai-sdk';
|
|
33
|
+
|
|
34
|
+
export const mastra = new Mastra({
|
|
35
|
+
server: {
|
|
36
|
+
apiRoutes: [
|
|
37
|
+
chatRoute({
|
|
38
|
+
path: '/chat',
|
|
39
|
+
agent: 'weatherAgent',
|
|
40
|
+
}),
|
|
41
|
+
],
|
|
42
|
+
},
|
|
43
|
+
});
|
|
45
44
|
```
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
#### 4. `process`
|
|
50
|
-
|
|
51
|
-
Handles webhook events and returns activity data.
|
|
46
|
+
After defining a dynamic route with `:agentId` you can use the `useChat()` hook like so:
|
|
52
47
|
|
|
53
|
-
|
|
48
|
+
```typescript
|
|
49
|
+
type MyMessage = {};
|
|
54
50
|
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
const { error, status, sendMessage, messages, regenerate, stop } = useChat<MyMessage>({
|
|
52
|
+
transport: new DefaultChatTransport({
|
|
53
|
+
api: 'http://localhost:4111/chat/weatherAgent',
|
|
54
|
+
}),
|
|
55
|
+
});
|
|
57
56
|
```
|
|
58
57
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
#### 5. `sync`
|
|
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`.
|
|
62
59
|
|
|
63
|
-
|
|
60
|
+
### Workflow route
|
|
64
61
|
|
|
65
|
-
|
|
62
|
+
Stream a workflow in AI SDK-compatible format.
|
|
66
63
|
|
|
67
|
-
```
|
|
68
|
-
|
|
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
|
+
});
|
|
69
77
|
```
|
|
70
78
|
|
|
71
|
-
|
|
79
|
+
### Network route
|
|
72
80
|
|
|
73
|
-
|
|
81
|
+
Stream agent networks (routing + nested agent/workflow/tool executions) in AI SDK-compatible format.
|
|
74
82
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
```
|
|
83
97
|
|
|
84
|
-
|
|
98
|
+
## Framework-agnostic handlers
|
|
85
99
|
|
|
86
|
-
|
|
87
|
-
npm install @Core/core-sdk
|
|
88
|
-
```
|
|
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`:
|
|
89
101
|
|
|
90
|
-
|
|
102
|
+
### handleChatStream
|
|
91
103
|
|
|
92
104
|
```typescript
|
|
93
|
-
import {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return this.handleSetup(eventPayload);
|
|
106
|
-
case 'PROCESS':
|
|
107
|
-
return this.handleProcess(eventPayload);
|
|
108
|
-
case 'IDENTIFY':
|
|
109
|
-
return this.handleIdentify(eventPayload);
|
|
110
|
-
case 'SYNC':
|
|
111
|
-
return this.handleSync(eventPayload);
|
|
112
|
-
default:
|
|
113
|
-
throw new Error(`Unknown event type: ${eventPayload.event}`);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
protected async getSpec(): Promise<Spec> {
|
|
118
|
-
return {
|
|
119
|
-
name: 'My Integration',
|
|
120
|
-
key: 'my-integration',
|
|
121
|
-
description: 'Integration with My Service',
|
|
122
|
-
icon: 'https://example.com/icon.png',
|
|
123
|
-
auth: {
|
|
124
|
-
OAuth2: {
|
|
125
|
-
token_url: 'https://api.example.com/oauth/token',
|
|
126
|
-
authorization_url: 'https://api.example.com/oauth/authorize',
|
|
127
|
-
scopes: ['read', 'write'],
|
|
128
|
-
},
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
private async handleSetup(
|
|
134
|
-
eventPayload: IntegrationEventPayload,
|
|
135
|
-
): Promise<any> {
|
|
136
|
-
// Process OAuth response and return tokens to save
|
|
137
|
-
const { code } = eventPayload.eventBody;
|
|
138
|
-
// Exchange code for tokens...
|
|
139
|
-
return {
|
|
140
|
-
access_token: 'token',
|
|
141
|
-
refresh_token: 'refresh_token',
|
|
142
|
-
expires_at: Date.now() + 3600000,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
private async handleProcess(
|
|
147
|
-
eventPayload: IntegrationEventPayload,
|
|
148
|
-
): Promise<any> {
|
|
149
|
-
// Handle webhook events
|
|
150
|
-
const { eventData } = eventPayload.eventBody;
|
|
151
|
-
// Process event and return activity...
|
|
152
|
-
return {
|
|
153
|
-
type: 'message',
|
|
154
|
-
user: 'user123',
|
|
155
|
-
content: 'Hello world',
|
|
156
|
-
timestamp: new Date(),
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
private async handleIdentify(
|
|
161
|
-
eventPayload: IntegrationEventPayload,
|
|
162
|
-
): Promise<any> {
|
|
163
|
-
// Extract account ID from webhook
|
|
164
|
-
const { team_id } = eventPayload.eventBody;
|
|
165
|
-
return { id: team_id };
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
private async handleSync(
|
|
169
|
-
eventPayload: IntegrationEventPayload,
|
|
170
|
-
): Promise<any> {
|
|
171
|
-
// Perform scheduled sync
|
|
172
|
-
const { config } = eventPayload;
|
|
173
|
-
// Fetch data since last sync...
|
|
174
|
-
return {
|
|
175
|
-
activities: [
|
|
176
|
-
/* activity data */
|
|
177
|
-
],
|
|
178
|
-
state: { last_sync: new Date().toISOString() },
|
|
179
|
-
};
|
|
180
|
-
}
|
|
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 });
|
|
181
117
|
}
|
|
182
|
-
|
|
183
|
-
// CLI entry point
|
|
184
|
-
const integration = new MyIntegration();
|
|
185
|
-
integration.parse();
|
|
186
118
|
```
|
|
187
119
|
|
|
188
|
-
|
|
120
|
+
### handleWorkflowStream
|
|
189
121
|
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
|
|
122
|
+
```typescript
|
|
123
|
+
import { handleWorkflowStream } from '@mastra/ai-sdk';
|
|
124
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
125
|
+
import { mastra } from '@/src/mastra';
|
|
126
|
+
|
|
127
|
+
export async function POST(req: Request) {
|
|
128
|
+
const params = await req.json();
|
|
129
|
+
const stream = await handleWorkflowStream({
|
|
130
|
+
mastra,
|
|
131
|
+
workflowId: 'myWorkflow',
|
|
132
|
+
params,
|
|
133
|
+
});
|
|
134
|
+
return createUIMessageStreamResponse({ stream });
|
|
135
|
+
}
|
|
193
136
|
```
|
|
194
137
|
|
|
195
|
-
###
|
|
138
|
+
### handleNetworkStream
|
|
196
139
|
|
|
197
|
-
|
|
140
|
+
Pass AI SDK `UIMessage[]` from your installed `ai` version so TypeScript can infer the correct stream overload.
|
|
198
141
|
|
|
199
|
-
|
|
200
|
-
- **JSON input/output handling** for all event types
|
|
201
|
-
- **Error handling** with proper exit codes
|
|
202
|
-
- **Consistent message formatting** for all responses
|
|
142
|
+
Handlers keep the existing v5/default behavior. If your app is typed against `ai@6`, pass `version: 'v6'`.
|
|
203
143
|
|
|
204
|
-
|
|
144
|
+
```typescript
|
|
145
|
+
import { handleNetworkStream } from '@mastra/ai-sdk';
|
|
146
|
+
import { createUIMessageStreamResponse, type UIMessage } from 'ai';
|
|
147
|
+
import { mastra } from '@/src/mastra';
|
|
148
|
+
|
|
149
|
+
export async function POST(req: Request) {
|
|
150
|
+
const params = (await req.json()) as { messages: UIMessage[] };
|
|
151
|
+
const stream = await handleNetworkStream({
|
|
152
|
+
mastra,
|
|
153
|
+
agentId: 'routingAgent',
|
|
154
|
+
version: 'v6',
|
|
155
|
+
params,
|
|
156
|
+
});
|
|
157
|
+
return createUIMessageStreamResponse({ stream });
|
|
158
|
+
}
|
|
159
|
+
```
|
|
205
160
|
|
|
206
|
-
|
|
161
|
+
## Manual transformation
|
|
207
162
|
|
|
208
|
-
|
|
209
|
-
# Test spec
|
|
210
|
-
node dist/index.js spec
|
|
163
|
+
If you have a raw Mastra `stream`, you can manually transform it to AI SDK UI message parts:
|
|
211
164
|
|
|
212
|
-
|
|
213
|
-
node dist/index.js setup --event-body '{"code":"test"}' --integration-definition '{}'
|
|
165
|
+
Use `toAISdkStream` for both versions. If your app is typed against `ai@6`, pass `version: 'v6'`.
|
|
214
166
|
|
|
215
|
-
|
|
216
|
-
|
|
167
|
+
```typescript
|
|
168
|
+
import { toAISdkStream } from '@mastra/ai-sdk';
|
|
169
|
+
import { createUIMessageStream, createUIMessageStreamResponse } from 'ai';
|
|
170
|
+
|
|
171
|
+
export async function POST(req: Request) {
|
|
172
|
+
const { messages } = await req.json();
|
|
173
|
+
const agent = mastra.getAgent('weatherAgent');
|
|
174
|
+
const stream = await agent.stream(messages);
|
|
175
|
+
|
|
176
|
+
// deduplicate messages https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages
|
|
177
|
+
const uiMessageStream = createUIMessageStream({
|
|
178
|
+
originalMessages: messages,
|
|
179
|
+
execute: async ({ writer }) => {
|
|
180
|
+
for await (const part of toAISdkStream(stream, { from: 'agent' })) {
|
|
181
|
+
writer.write(part);
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
return createUIMessageStreamResponse({ stream: uiMessageStream });
|
|
187
|
+
}
|
|
217
188
|
```
|
|
218
189
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
1. **Always validate input data** before processing
|
|
222
|
-
2. **Handle errors gracefully** with meaningful error messages
|
|
223
|
-
3. **Use consistent data structures** for activities
|
|
224
|
-
4. **Include proper timestamps** in all activity data
|
|
225
|
-
5. **Store minimal state** for sync operations
|
|
226
|
-
6. **Test all event types** thoroughly
|
|
190
|
+
For AI SDK v6, select the v6 stream contract explicitly:
|
|
227
191
|
|
|
228
|
-
|
|
192
|
+
```typescript
|
|
193
|
+
const uiMessageStream = createUIMessageStream({
|
|
194
|
+
originalMessages: messages,
|
|
195
|
+
execute: async ({ writer }) => {
|
|
196
|
+
for await (const part of toAISdkStream(stream, {
|
|
197
|
+
from: 'agent',
|
|
198
|
+
version: 'v6',
|
|
199
|
+
})) {
|
|
200
|
+
writer.write(part);
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
```
|