@mastra/client-js 1.2.0 → 1.2.1-alpha.0
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 +14 -0
- package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +10 -7
- package/dist/client.d.ts +11 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/docs/SKILL.md +32 -22
- package/dist/docs/{SOURCE_MAP.json → assets/SOURCE_MAP.json} +1 -1
- package/dist/docs/{server/07-auth0.md → references/docs-server-auth-auth0.md} +68 -79
- package/dist/docs/{server/03-clerk.md → references/docs-server-auth-clerk.md} +41 -52
- package/dist/docs/{server/05-firebase.md → references/docs-server-auth-firebase.md} +83 -97
- package/dist/docs/{server/02-jwt.md → references/docs-server-auth-jwt.md} +46 -35
- package/dist/docs/{server/04-supabase.md → references/docs-server-auth-supabase.md} +33 -44
- package/dist/docs/{server/06-workos.md → references/docs-server-auth-workos.md} +45 -56
- package/dist/docs/{server/01-mastra-client.md → references/docs-server-mastra-client.md} +32 -20
- package/dist/docs/{ai-sdk/01-reference.md → references/reference-ai-sdk-to-ai-sdk-stream.md} +27 -153
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-v4-messages.md +79 -0
- package/dist/docs/references/reference-ai-sdk-to-ai-sdk-v5-messages.md +73 -0
- package/dist/docs/references/reference-client-js-agents.md +438 -0
- package/dist/docs/references/reference-client-js-error-handling.md +16 -0
- package/dist/docs/references/reference-client-js-logs.md +24 -0
- package/dist/docs/references/reference-client-js-mastra-client.md +63 -0
- package/dist/docs/references/reference-client-js-memory.md +225 -0
- package/dist/docs/references/reference-client-js-observability.md +72 -0
- package/dist/docs/references/reference-client-js-telemetry.md +20 -0
- package/dist/docs/references/reference-client-js-tools.md +44 -0
- package/dist/docs/references/reference-client-js-vectors.md +79 -0
- package/dist/docs/references/reference-client-js-workflows.md +199 -0
- package/dist/index.cjs +18 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +18 -1
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +158 -27
- package/dist/types.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/docs/README.md +0 -33
- package/dist/docs/client-js/01-reference.md +0 -1180
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
> Documentation for the MastraAuthWorkos class, which authenticates Mastra applications using WorkOS authentication.
|
|
2
|
-
|
|
3
1
|
# MastraAuthWorkos Class
|
|
4
2
|
|
|
5
3
|
The `MastraAuthWorkos` class provides authentication for Mastra using WorkOS. It verifies incoming requests using WorkOS access tokens and integrates with the Mastra server using the `auth` option.
|
|
@@ -13,16 +11,14 @@ This example uses WorkOS authentication. Make sure to:
|
|
|
13
11
|
3. Configure your redirect URIs and allowed origins
|
|
14
12
|
4. Set up Organizations and configure user roles as needed
|
|
15
13
|
|
|
16
|
-
```env
|
|
14
|
+
```env
|
|
17
15
|
WORKOS_API_KEY=sk_live_...
|
|
18
16
|
WORKOS_CLIENT_ID=client_...
|
|
19
17
|
```
|
|
20
18
|
|
|
21
|
-
> **Note:**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
For detailed setup instructions, refer to the [WorkOS documentation](https://workos.com/docs) for your specific platform.
|
|
19
|
+
> **Note:** You can find your API key and Client ID in the WorkOS Dashboard under API Keys and Applications respectively.
|
|
20
|
+
>
|
|
21
|
+
> For detailed setup instructions, refer to the [WorkOS documentation](https://workos.com/docs) for your specific platform.
|
|
26
22
|
|
|
27
23
|
## Installation
|
|
28
24
|
|
|
@@ -36,7 +32,7 @@ npm install @mastra/auth-workos@latest
|
|
|
36
32
|
|
|
37
33
|
### Basic usage with environment variables
|
|
38
34
|
|
|
39
|
-
```typescript
|
|
35
|
+
```typescript
|
|
40
36
|
import { Mastra } from "@mastra/core";
|
|
41
37
|
import { MastraAuthWorkos } from "@mastra/auth-workos";
|
|
42
38
|
|
|
@@ -49,7 +45,7 @@ export const mastra = new Mastra({
|
|
|
49
45
|
|
|
50
46
|
### Custom configuration
|
|
51
47
|
|
|
52
|
-
```typescript
|
|
48
|
+
```typescript
|
|
53
49
|
import { Mastra } from "@mastra/core";
|
|
54
50
|
import { MastraAuthWorkos } from "@mastra/auth-workos";
|
|
55
51
|
|
|
@@ -76,7 +72,7 @@ By default, `MastraAuthWorkos` checks whether the authenticated user has an 'adm
|
|
|
76
72
|
|
|
77
73
|
To customize user authorization, provide a custom `authorizeUser` function:
|
|
78
74
|
|
|
79
|
-
```typescript
|
|
75
|
+
```typescript
|
|
80
76
|
import { MastraAuthWorkos } from "@mastra/auth-workos";
|
|
81
77
|
|
|
82
78
|
const workosAuth = new MastraAuthWorkos({
|
|
@@ -88,9 +84,7 @@ const workosAuth = new MastraAuthWorkos({
|
|
|
88
84
|
});
|
|
89
85
|
```
|
|
90
86
|
|
|
91
|
-
> **
|
|
92
|
-
|
|
93
|
-
Visit [MastraAuthWorkos](https://mastra.ai/reference/auth/workos) for all available configuration options.
|
|
87
|
+
> **Info:** Visit [MastraAuthWorkos](https://mastra.ai/reference/auth/workos) for all available configuration options.
|
|
94
88
|
|
|
95
89
|
## Client-side setup
|
|
96
90
|
|
|
@@ -108,7 +102,7 @@ npm install @workos-inc/node
|
|
|
108
102
|
|
|
109
103
|
After users complete the WorkOS authentication flow and return with an authorization code, exchange it for an access token:
|
|
110
104
|
|
|
111
|
-
```typescript
|
|
105
|
+
```typescript
|
|
112
106
|
import { WorkOS } from "@workos-inc/node";
|
|
113
107
|
|
|
114
108
|
const workos = new WorkOS(process.env.WORKOS_API_KEY);
|
|
@@ -127,15 +121,13 @@ export const authenticateWithWorkos = async (
|
|
|
127
121
|
};
|
|
128
122
|
```
|
|
129
123
|
|
|
130
|
-
> **Note:**
|
|
131
|
-
|
|
132
|
-
Refer to the [WorkOS User Management documentation](https://workos.com/docs/authkit/vanilla/nodejs) for more authentication methods and configuration options.
|
|
124
|
+
> **Note:** Refer to the [WorkOS User Management documentation](https://workos.com/docs/authkit/vanilla/nodejs) for more authentication methods and configuration options.
|
|
133
125
|
|
|
134
126
|
## Configuring `MastraClient`
|
|
135
127
|
|
|
136
128
|
When `auth` is enabled, all requests made with `MastraClient` must include a valid WorkOS access token in the `Authorization` header:
|
|
137
129
|
|
|
138
|
-
```typescript
|
|
130
|
+
```typescript
|
|
139
131
|
import { MastraClient } from "@mastra/client-js";
|
|
140
132
|
|
|
141
133
|
export const createMastraClient = (accessToken: string) => {
|
|
@@ -148,54 +140,51 @@ export const createMastraClient = (accessToken: string) => {
|
|
|
148
140
|
};
|
|
149
141
|
```
|
|
150
142
|
|
|
151
|
-
> **
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
143
|
+
> **Info:** The access token must be prefixed with `Bearer` in the Authorization header.
|
|
144
|
+
>
|
|
145
|
+
> Visit [Mastra Client SDK](https://mastra.ai/docs/server/mastra-client) for more configuration options.
|
|
156
146
|
|
|
157
147
|
### Making authenticated requests
|
|
158
148
|
|
|
159
149
|
Once `MastraClient` is configured with the WorkOS access token, you can send authenticated requests:
|
|
160
150
|
|
|
161
|
-
|
|
151
|
+
**React**:
|
|
162
152
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
153
|
+
```typescript
|
|
154
|
+
import { WorkOS } from '@workos-inc/node';
|
|
155
|
+
import { MastraClient } from '@mastra/client-js';
|
|
166
156
|
|
|
167
|
-
|
|
157
|
+
const workos = new WorkOS(process.env.WORKOS_API_KEY);
|
|
168
158
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
159
|
+
export const callMastraWithWorkos = async (code: string, clientId: string) => {
|
|
160
|
+
const authenticationResponse = await workos.userManagement.authenticateWithCode({
|
|
161
|
+
code,
|
|
162
|
+
clientId,
|
|
163
|
+
});
|
|
174
164
|
|
|
175
|
-
|
|
165
|
+
const token = authenticationResponse.accessToken;
|
|
176
166
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
167
|
+
const mastra = new MastraClient({
|
|
168
|
+
baseUrl: "http://localhost:4111",
|
|
169
|
+
headers: {
|
|
170
|
+
Authorization: `Bearer ${token}`,
|
|
171
|
+
},
|
|
172
|
+
});
|
|
183
173
|
|
|
184
|
-
|
|
185
|
-
|
|
174
|
+
const weatherAgent = mastra.getAgent("weatherAgent");
|
|
175
|
+
const response = await weatherAgent.generate("What's the weather like in Nairobi");
|
|
186
176
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
177
|
+
return response.text;
|
|
178
|
+
};
|
|
179
|
+
```
|
|
190
180
|
|
|
191
|
-
|
|
192
|
-
**curl:**
|
|
181
|
+
**cURL**:
|
|
193
182
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
183
|
+
```bash
|
|
184
|
+
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
|
|
185
|
+
-H "Content-Type: application/json" \
|
|
186
|
+
-H "Authorization: Bearer <your-workos-access-token>" \
|
|
187
|
+
-d '{
|
|
188
|
+
"messages": "Weather in London"
|
|
189
|
+
}'
|
|
190
|
+
```
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
> Learn how to set up and use the Mastra Client SDK
|
|
2
|
-
|
|
3
1
|
# Mastra Client SDK
|
|
4
2
|
|
|
5
3
|
The Mastra Client SDK provides a simple and type-safe interface for interacting with your [Mastra Server](https://mastra.ai/docs/server/mastra-server) from your client environment.
|
|
@@ -20,15 +18,35 @@ The Mastra Client SDK is designed for browser environments and uses the native `
|
|
|
20
18
|
|
|
21
19
|
To use the Mastra Client SDK, install the required dependencies:
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
**npm**:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
24
|
npm install @mastra/client-js@latest
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
**pnpm**:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pnpm add @mastra/client-js@latest
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**Yarn**:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
yarn add @mastra/client-js@latest
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Bun**:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
bun add @mastra/client-js@latest
|
|
43
|
+
```
|
|
44
|
+
|
|
27
45
|
### Initialize the `MastraClient`
|
|
28
46
|
|
|
29
47
|
Once initialized with a `baseUrl`, `MastraClient` exposes a type-safe interface for calling agents, tools, and workflows.
|
|
30
48
|
|
|
31
|
-
```typescript
|
|
49
|
+
```typescript
|
|
32
50
|
import { MastraClient } from "@mastra/client-js";
|
|
33
51
|
|
|
34
52
|
export const mastraClient = new MastraClient({
|
|
@@ -68,9 +86,7 @@ const testAgent = async () => {
|
|
|
68
86
|
};
|
|
69
87
|
```
|
|
70
88
|
|
|
71
|
-
> **
|
|
72
|
-
|
|
73
|
-
You can also call `.generate()` with an array of message objects that include `role` and `content`. Visit the [.generate() reference](https://mastra.ai/reference/client-js/agents#generate) for more information.
|
|
89
|
+
> **Info:** You can also call `.generate()` with an array of message objects that include `role` and `content`. Visit the [.generate() reference](https://mastra.ai/reference/client-js/agents) for more information.
|
|
74
90
|
|
|
75
91
|
## Streaming responses
|
|
76
92
|
|
|
@@ -96,15 +112,13 @@ const testAgent = async () => {
|
|
|
96
112
|
};
|
|
97
113
|
```
|
|
98
114
|
|
|
99
|
-
> **
|
|
100
|
-
|
|
101
|
-
You can also call `.stream()` with an array of message objects that include `role` and `content`. Visit the [.stream() reference](https://mastra.ai/reference/client-js/agents#stream) for more information.
|
|
115
|
+
> **Info:** You can also call `.stream()` with an array of message objects that include `role` and `content`. Visit the [.stream() reference](https://mastra.ai/reference/client-js/agents) for more information.
|
|
102
116
|
|
|
103
117
|
## Configuration options
|
|
104
118
|
|
|
105
119
|
`MastraClient` accepts optional parameters like `retries`, `backoffMs`, and `headers` to control request behavior. These parameters are useful for controlling retry behavior and including diagnostic metadata.
|
|
106
120
|
|
|
107
|
-
```typescript
|
|
121
|
+
```typescript
|
|
108
122
|
import { MastraClient } from "@mastra/client-js";
|
|
109
123
|
|
|
110
124
|
export const mastraClient = new MastraClient({
|
|
@@ -117,9 +131,7 @@ export const mastraClient = new MastraClient({
|
|
|
117
131
|
});
|
|
118
132
|
```
|
|
119
133
|
|
|
120
|
-
> **
|
|
121
|
-
|
|
122
|
-
Visit [MastraClient](https://mastra.ai/reference/client-js/mastra-client) for more configuration options.
|
|
134
|
+
> **Info:** Visit [MastraClient](https://mastra.ai/reference/client-js/mastra-client) for more configuration options.
|
|
123
135
|
|
|
124
136
|
## Adding request cancelling
|
|
125
137
|
|
|
@@ -127,7 +139,7 @@ Visit [MastraClient](https://mastra.ai/reference/client-js/mastra-client) for mo
|
|
|
127
139
|
|
|
128
140
|
Pass an `AbortSignal` to the client constructor to enable cancellation across all requests.
|
|
129
141
|
|
|
130
|
-
```typescript
|
|
142
|
+
```typescript
|
|
131
143
|
import { MastraClient } from "@mastra/client-js";
|
|
132
144
|
|
|
133
145
|
export const controller = new AbortController();
|
|
@@ -142,7 +154,7 @@ export const mastraClient = new MastraClient({
|
|
|
142
154
|
|
|
143
155
|
Calling `.abort()` will cancel any ongoing requests tied to that signal.
|
|
144
156
|
|
|
145
|
-
```typescript
|
|
157
|
+
```typescript
|
|
146
158
|
import { mastraClient, controller } from "lib/mastra-client";
|
|
147
159
|
|
|
148
160
|
const handleAbort = () => {
|
|
@@ -156,7 +168,7 @@ Define tools directly in client-side applications using the `createTool()` funct
|
|
|
156
168
|
|
|
157
169
|
This lets agents trigger browser-side functionality such as DOM manipulation, local storage access, or other Web APIs, enabling tool execution in the user's environment rather than on the server.
|
|
158
170
|
|
|
159
|
-
```typescript
|
|
171
|
+
```typescript
|
|
160
172
|
import { createTool } from "@mastra/client-js";
|
|
161
173
|
import { z } from "zod";
|
|
162
174
|
|
|
@@ -194,9 +206,9 @@ const handleClientTool = async () => {
|
|
|
194
206
|
|
|
195
207
|
### Client tool's agent
|
|
196
208
|
|
|
197
|
-
This is a standard Mastra [agent](
|
|
209
|
+
This is a standard Mastra [agent](https://mastra.ai/docs/agents/overview) configured to return hex color codes, intended to work with the browser-based client tool defined above.
|
|
198
210
|
|
|
199
|
-
```typescript
|
|
211
|
+
```typescript
|
|
200
212
|
import { Agent } from "@mastra/core/agent";
|
|
201
213
|
|
|
202
214
|
export const colorAgent = new Agent({
|
|
@@ -213,7 +225,7 @@ export const colorAgent = new Agent({
|
|
|
213
225
|
|
|
214
226
|
You can also use `MastraClient` in server-side environments such as API routes, serverless functions or actions. The usage will broadly remain the same but you may need to recreate the response to your client:
|
|
215
227
|
|
|
216
|
-
```typescript
|
|
228
|
+
```typescript
|
|
217
229
|
export async function action() {
|
|
218
230
|
const agent = mastraClient.getAgent("testAgent");
|
|
219
231
|
|
package/dist/docs/{ai-sdk/01-reference.md → references/reference-ai-sdk-to-ai-sdk-stream.md}
RENAMED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
> API reference for ai sdk - 3 entries
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Reference: toAISdkStream()
|
|
9
|
-
|
|
10
|
-
> API reference for toAISdkStream(), a function to convert Mastra streams to AI SDK-compatible streams.
|
|
1
|
+
# toAISdkStream()
|
|
11
2
|
|
|
12
3
|
Converts Mastra streams (agent, network, or workflow) to AI SDK-compatible streams. Use this function when you need to manually transform Mastra streams for use with AI SDK's `createUIMessageStream()` and `createUIMessageStreamResponse()`.
|
|
13
4
|
|
|
@@ -17,7 +8,7 @@ This is useful when building custom streaming endpoints outside Mastra's provide
|
|
|
17
8
|
|
|
18
9
|
Next.js App Router example:
|
|
19
10
|
|
|
20
|
-
```typescript
|
|
11
|
+
```typescript
|
|
21
12
|
import { mastra } from "../../mastra";
|
|
22
13
|
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
|
|
23
14
|
import { toAISdkStream } from "@mastra/ai-sdk";
|
|
@@ -42,24 +33,41 @@ export async function POST(req: Request) {
|
|
|
42
33
|
}
|
|
43
34
|
```
|
|
44
35
|
|
|
45
|
-
> **
|
|
46
|
-
|
|
47
|
-
Pass `messages` to `originalMessages` in `createUIMessageStream()` to avoid duplicated assistant messages in the UI. See [Troubleshooting: Repeated Assistant Messages](https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages) for details.
|
|
36
|
+
> **Tip:** Pass `messages` to `originalMessages` in `createUIMessageStream()` to avoid duplicated assistant messages in the UI. See [Troubleshooting: Repeated Assistant Messages](https://ai-sdk.dev/docs/troubleshooting/repeated-assistant-messages) for details.
|
|
48
37
|
|
|
49
38
|
## Parameters
|
|
50
39
|
|
|
51
40
|
The first parameter is the Mastra stream to convert. It can be one of:
|
|
41
|
+
|
|
52
42
|
- `MastraModelOutput` - An agent stream from `agent.stream()`
|
|
53
43
|
- `MastraAgentNetworkStream` - A network stream from `agent.network()`
|
|
54
44
|
- `MastraWorkflowStream` or `WorkflowRunOutput` - A workflow stream
|
|
55
45
|
|
|
56
46
|
The second parameter is an options object:
|
|
57
47
|
|
|
48
|
+
**from:** (`'agent' | 'network' | 'workflow'`): The type of Mastra stream being converted. (Default: `'agent'`)
|
|
49
|
+
|
|
50
|
+
**lastMessageId?:** (`string`): (Agent only) The ID of the last message in the conversation.
|
|
51
|
+
|
|
52
|
+
**sendStart?:** (`boolean`): (Agent only) Whether to send start events in the stream. (Default: `true`)
|
|
53
|
+
|
|
54
|
+
**sendFinish?:** (`boolean`): (Agent only) Whether to send finish events in the stream. (Default: `true`)
|
|
55
|
+
|
|
56
|
+
**sendReasoning?:** (`boolean`): (Agent only) Whether to include reasoning-delta chunks in the stream. Set to true to stream reasoning content from models that support extended thinking. (Default: `false`)
|
|
57
|
+
|
|
58
|
+
**sendSources?:** (`boolean`): (Agent only) Whether to include source citations in the output. (Default: `false`)
|
|
59
|
+
|
|
60
|
+
**includeTextStreamParts?:** (`boolean`): (Workflow only) Whether to include text stream parts in the output. (Default: `true`)
|
|
61
|
+
|
|
62
|
+
**messageMetadata?:** (`(options: { part: UIMessageStreamPart }) => Record<string, unknown> | undefined`): (Agent only) A function that receives the current stream part and returns metadata to attach to start and finish chunks.
|
|
63
|
+
|
|
64
|
+
**onError?:** (`(error: unknown) => string`): (Agent only) A function to handle errors during stream conversion. Receives the error and should return a string representation.
|
|
65
|
+
|
|
58
66
|
## Examples
|
|
59
67
|
|
|
60
68
|
### Converting a workflow stream
|
|
61
69
|
|
|
62
|
-
```typescript
|
|
70
|
+
```typescript
|
|
63
71
|
import { mastra } from "../../mastra";
|
|
64
72
|
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
|
|
65
73
|
import { toAISdkStream } from "@mastra/ai-sdk";
|
|
@@ -86,7 +94,7 @@ export async function POST(req: Request) {
|
|
|
86
94
|
|
|
87
95
|
### Converting a network stream
|
|
88
96
|
|
|
89
|
-
```typescript
|
|
97
|
+
```typescript
|
|
90
98
|
import { mastra } from "../../mastra";
|
|
91
99
|
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
|
|
92
100
|
import { toAISdkStream } from "@mastra/ai-sdk";
|
|
@@ -112,7 +120,7 @@ export async function POST(req: Request) {
|
|
|
112
120
|
|
|
113
121
|
### Converting an agent stream with reasoning enabled
|
|
114
122
|
|
|
115
|
-
```typescript
|
|
123
|
+
```typescript
|
|
116
124
|
import { mastra } from "../../mastra";
|
|
117
125
|
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
|
|
118
126
|
import { toAISdkStream } from "@mastra/ai-sdk";
|
|
@@ -146,7 +154,7 @@ export async function POST(req: Request) {
|
|
|
146
154
|
|
|
147
155
|
### Using messageMetadata
|
|
148
156
|
|
|
149
|
-
```typescript
|
|
157
|
+
```typescript
|
|
150
158
|
import { mastra } from "../../mastra";
|
|
151
159
|
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
|
|
152
160
|
import { toAISdkStream } from "@mastra/ai-sdk";
|
|
@@ -181,7 +189,7 @@ export async function POST(req: Request) {
|
|
|
181
189
|
|
|
182
190
|
If you're using the Mastra client SDK (`@mastra/client-js`) on the client side and want to convert streams to AI SDK format:
|
|
183
191
|
|
|
184
|
-
```typescript
|
|
192
|
+
```typescript
|
|
185
193
|
import { MastraClient } from "@mastra/client-js";
|
|
186
194
|
import { createUIMessageStream } from "ai";
|
|
187
195
|
import { toAISdkStream } from "@mastra/ai-sdk";
|
|
@@ -221,138 +229,4 @@ const uiMessageStream = createUIMessageStream({
|
|
|
221
229
|
for await (const part of uiMessageStream) {
|
|
222
230
|
console.log(part);
|
|
223
231
|
}
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
---
|
|
227
|
-
|
|
228
|
-
## Reference: toAISdkV4Messages()
|
|
229
|
-
|
|
230
|
-
> API reference for toAISdkV4Messages(), a function to convert Mastra messages to AI SDK v4 UI messages.
|
|
231
|
-
|
|
232
|
-
Converts messages from various input formats to AI SDK V4 UI message format. This function accepts messages in multiple formats (strings, AI SDK V4/V5 messages, Mastra DB messages, etc.) and normalizes them to the AI SDK V4 `UIMessage` format, which is suitable for use with AI SDK UI components like `useChat()`.
|
|
233
|
-
|
|
234
|
-
## Usage example
|
|
235
|
-
|
|
236
|
-
```typescript title="app/chat/page.tsx"
|
|
237
|
-
import { toAISdkV4Messages } from "@mastra/ai-sdk";
|
|
238
|
-
import { useChat } from "ai/react"; // AI SDK V4
|
|
239
|
-
|
|
240
|
-
// Stored messages from your database, memory or API
|
|
241
|
-
const storedMessages = [
|
|
242
|
-
{ id: "1", role: "user", parts: [{ type: "text", text: "Hello" }] },
|
|
243
|
-
{ id: "2", role: "assistant", parts: [{ type: "text", text: "Hi there!" }] }
|
|
244
|
-
];
|
|
245
|
-
|
|
246
|
-
export default function Chat() {
|
|
247
|
-
const { messages } = useChat({
|
|
248
|
-
initialMessages: toAISdkV4Messages(storedMessages)
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
return (
|
|
252
|
-
<div>
|
|
253
|
-
{messages.map((message) => (
|
|
254
|
-
<div key={message.id}>
|
|
255
|
-
{message.role}: {message.content}
|
|
256
|
-
</div>
|
|
257
|
-
))}
|
|
258
|
-
</div>
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
## Parameters
|
|
264
|
-
|
|
265
|
-
## Returns
|
|
266
|
-
|
|
267
|
-
Returns an array of AI SDK V4 `UIMessage` objects with the following structure:
|
|
268
|
-
|
|
269
|
-
## Examples
|
|
270
|
-
|
|
271
|
-
### Converting simple text messages
|
|
272
|
-
|
|
273
|
-
```typescript
|
|
274
|
-
import { toAISdkV4Messages } from "@mastra/ai-sdk";
|
|
275
|
-
|
|
276
|
-
const messages = toAISdkV4Messages(["Hello", "How can I help you today?"]);
|
|
277
|
-
// Returns array of UIMessage objects with user role and content string
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
### Loading messages with Mastra client
|
|
281
|
-
|
|
282
|
-
```typescript
|
|
283
|
-
import { MastraClient } from "@mastra/client-js";
|
|
284
|
-
import { toAISdkV4Messages } from "@mastra/ai-sdk";
|
|
285
|
-
|
|
286
|
-
const client = new MastraClient();
|
|
287
|
-
|
|
288
|
-
const { messages } = await client.listThreadMessages("thread-id", { agentId: "myAgent" });
|
|
289
|
-
const uiMessages = toAISdkV4Messages(messages);
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
---
|
|
293
|
-
|
|
294
|
-
## Reference: toAISdkV5Messages()
|
|
295
|
-
|
|
296
|
-
> API reference for toAISdkV5Messages(), a function to convert Mastra messages to AI SDK v5 UI messages.
|
|
297
|
-
|
|
298
|
-
Converts messages from various input formats to AI SDK V5 (and later) UI message format. This function accepts messages in multiple formats (strings, AI SDK V4/V5 messages, Mastra DB messages, etc.) and normalizes them to the AI SDK V5+ `UIMessage` format, which is suitable for use with AI SDK UI components like `useChat()`.
|
|
299
|
-
|
|
300
|
-
## Usage example
|
|
301
|
-
|
|
302
|
-
```typescript title="app/chat/page.tsx"
|
|
303
|
-
import { toAISdkV5Messages } from "@mastra/ai-sdk/ui";
|
|
304
|
-
import { useChat } from "ai/react";
|
|
305
|
-
|
|
306
|
-
// Stored messages from your database, memory or API
|
|
307
|
-
const storedMessages = [
|
|
308
|
-
{ id: "1", role: "user", content: "Hello", parts: [{ type: "text", text: "Hello" }] },
|
|
309
|
-
{ id: "2", role: "assistant", content: "Hi there!", parts: [{ type: "text", text: "Hi there!" }] }
|
|
310
|
-
];
|
|
311
|
-
|
|
312
|
-
export default function Chat() {
|
|
313
|
-
const { messages } = useChat({
|
|
314
|
-
initialMessages: toAISdkV5Messages(storedMessages)
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
return (
|
|
318
|
-
<div>
|
|
319
|
-
{messages.map((message) => (
|
|
320
|
-
<div key={message.id}>
|
|
321
|
-
{message.role}: {message.parts.map(part =>
|
|
322
|
-
part.type === "text" ? part.text : null
|
|
323
|
-
)}
|
|
324
|
-
</div>
|
|
325
|
-
))}
|
|
326
|
-
</div>
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
## Parameters
|
|
332
|
-
|
|
333
|
-
## Returns
|
|
334
|
-
|
|
335
|
-
Returns an array of AI SDK V5+ `UIMessage` objects with the following structure:
|
|
336
|
-
|
|
337
|
-
## Examples
|
|
338
|
-
|
|
339
|
-
### Converting simple text messages
|
|
340
|
-
|
|
341
|
-
```typescript
|
|
342
|
-
import { toAISdkV5Messages } from "@mastra/ai-sdk/ui";
|
|
343
|
-
|
|
344
|
-
const messages = toAISdkV5Messages(["Hello", "How can I help you today?"]);
|
|
345
|
-
// Returns array of UIMessage objects with user role
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
### Loading messages with Mastra client
|
|
349
|
-
|
|
350
|
-
```typescript
|
|
351
|
-
import { MastraClient } from "@mastra/client-js";
|
|
352
|
-
import { toAISdkV5Messages } from "@mastra/ai-sdk/ui";
|
|
353
|
-
|
|
354
|
-
const client = new MastraClient();
|
|
355
|
-
|
|
356
|
-
const { messages } = await client.listThreadMessages("thread-id", { agentId: "myAgent" });
|
|
357
|
-
const uiMessages = toAISdkV5Messages(messages);
|
|
358
232
|
```
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# toAISdkV4Messages()
|
|
2
|
+
|
|
3
|
+
Converts messages from various input formats to AI SDK V4 UI message format. This function accepts messages in multiple formats (strings, AI SDK V4/V5 messages, Mastra DB messages, etc.) and normalizes them to the AI SDK V4 `UIMessage` format, which is suitable for use with AI SDK UI components like `useChat()`.
|
|
4
|
+
|
|
5
|
+
## Usage example
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { toAISdkV4Messages } from "@mastra/ai-sdk";
|
|
9
|
+
import { useChat } from "ai/react"; // AI SDK V4
|
|
10
|
+
|
|
11
|
+
// Stored messages from your database, memory or API
|
|
12
|
+
const storedMessages = [
|
|
13
|
+
{ id: "1", role: "user", parts: [{ type: "text", text: "Hello" }] },
|
|
14
|
+
{ id: "2", role: "assistant", parts: [{ type: "text", text: "Hi there!" }] }
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
export default function Chat() {
|
|
18
|
+
const { messages } = useChat({
|
|
19
|
+
initialMessages: toAISdkV4Messages(storedMessages)
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<div>
|
|
24
|
+
{messages.map((message) => (
|
|
25
|
+
<div key={message.id}>
|
|
26
|
+
{message.role}: {message.content}
|
|
27
|
+
</div>
|
|
28
|
+
))}
|
|
29
|
+
</div>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Parameters
|
|
35
|
+
|
|
36
|
+
**messages:** (`MessageListInput`): Messages to convert. Can be a string, array of strings, a single message object, or an array of message objects in any supported format.
|
|
37
|
+
|
|
38
|
+
## Returns
|
|
39
|
+
|
|
40
|
+
Returns an array of AI SDK V4 `UIMessage` objects with the following structure:
|
|
41
|
+
|
|
42
|
+
**id:** (`string`): Unique message identifier.
|
|
43
|
+
|
|
44
|
+
**role:** (`'user' | 'assistant' | 'system'`): The role of the message sender.
|
|
45
|
+
|
|
46
|
+
**content:** (`string`): Text content of the message.
|
|
47
|
+
|
|
48
|
+
**parts:** (`UIMessagePart[]`): Array of UI parts including text, tool-invocation, file, reasoning, source, and step markers.
|
|
49
|
+
|
|
50
|
+
**createdAt:** (`Date`): Message creation timestamp.
|
|
51
|
+
|
|
52
|
+
**toolInvocations?:** (`ToolInvocation[]`): Array of tool invocations for assistant messages.
|
|
53
|
+
|
|
54
|
+
**experimental\_attachments?:** (`Attachment[]`): File attachments on the message.
|
|
55
|
+
|
|
56
|
+
**metadata?:** (`Record<string, unknown>`): Custom metadata attached to the message.
|
|
57
|
+
|
|
58
|
+
## Examples
|
|
59
|
+
|
|
60
|
+
### Converting simple text messages
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { toAISdkV4Messages } from "@mastra/ai-sdk";
|
|
64
|
+
|
|
65
|
+
const messages = toAISdkV4Messages(["Hello", "How can I help you today?"]);
|
|
66
|
+
// Returns array of UIMessage objects with user role and content string
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Loading messages with Mastra client
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { MastraClient } from "@mastra/client-js";
|
|
73
|
+
import { toAISdkV4Messages } from "@mastra/ai-sdk";
|
|
74
|
+
|
|
75
|
+
const client = new MastraClient();
|
|
76
|
+
|
|
77
|
+
const { messages } = await client.listThreadMessages("thread-id", { agentId: "myAgent" });
|
|
78
|
+
const uiMessages = toAISdkV4Messages(messages);
|
|
79
|
+
```
|