@mastra/client-js 0.0.0-bundle-recursion-20251030002519 → 0.0.0-bundle-version-20260121091645

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.
Files changed (58) hide show
  1. package/CHANGELOG.md +2119 -3
  2. package/README.md +11 -13
  3. package/dist/_types/@ai-sdk_ui-utils/dist/index.d.ts +820 -0
  4. package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +8511 -0
  5. package/dist/client.d.ts +111 -77
  6. package/dist/client.d.ts.map +1 -1
  7. package/dist/docs/README.md +33 -0
  8. package/dist/docs/SKILL.md +34 -0
  9. package/dist/docs/SOURCE_MAP.json +6 -0
  10. package/dist/docs/ai-sdk/01-reference.md +358 -0
  11. package/dist/docs/client-js/01-reference.md +1180 -0
  12. package/dist/docs/server/01-mastra-client.md +256 -0
  13. package/dist/docs/server/02-jwt.md +99 -0
  14. package/dist/docs/server/03-clerk.md +143 -0
  15. package/dist/docs/server/04-supabase.md +128 -0
  16. package/dist/docs/server/05-firebase.md +286 -0
  17. package/dist/docs/server/06-workos.md +201 -0
  18. package/dist/docs/server/07-auth0.md +233 -0
  19. package/dist/index.cjs +2108 -993
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.js +2106 -987
  22. package/dist/index.js.map +1 -1
  23. package/dist/resources/a2a.d.ts.map +1 -1
  24. package/dist/resources/agent-builder.d.ts +20 -75
  25. package/dist/resources/agent-builder.d.ts.map +1 -1
  26. package/dist/resources/agent.d.ts +63 -51
  27. package/dist/resources/agent.d.ts.map +1 -1
  28. package/dist/resources/base.d.ts.map +1 -1
  29. package/dist/resources/index.d.ts +2 -0
  30. package/dist/resources/index.d.ts.map +1 -1
  31. package/dist/resources/mcp-tool.d.ts +5 -5
  32. package/dist/resources/memory-thread.d.ts +31 -24
  33. package/dist/resources/memory-thread.d.ts.map +1 -1
  34. package/dist/resources/observability.d.ts +62 -19
  35. package/dist/resources/observability.d.ts.map +1 -1
  36. package/dist/resources/processor.d.ts +20 -0
  37. package/dist/resources/processor.d.ts.map +1 -0
  38. package/dist/resources/run.d.ts +210 -0
  39. package/dist/resources/run.d.ts.map +1 -0
  40. package/dist/resources/stored-agent.d.ts +26 -0
  41. package/dist/resources/stored-agent.d.ts.map +1 -0
  42. package/dist/resources/tool.d.ts +4 -4
  43. package/dist/resources/vector.d.ts +5 -5
  44. package/dist/resources/workflow.d.ts +27 -234
  45. package/dist/resources/workflow.d.ts.map +1 -1
  46. package/dist/tools.d.ts +2 -2
  47. package/dist/tools.d.ts.map +1 -1
  48. package/dist/types.d.ts +299 -99
  49. package/dist/types.d.ts.map +1 -1
  50. package/dist/utils/index.d.ts +32 -6
  51. package/dist/utils/index.d.ts.map +1 -1
  52. package/dist/utils/process-mastra-stream.d.ts +1 -1
  53. package/dist/utils/process-mastra-stream.d.ts.map +1 -1
  54. package/dist/utils/zod-to-json-schema.d.ts +9 -1
  55. package/dist/utils/zod-to-json-schema.d.ts.map +1 -1
  56. package/package.json +20 -18
  57. package/dist/resources/network-memory-thread.d.ts +0 -47
  58. package/dist/resources/network-memory-thread.d.ts.map +0 -1
@@ -0,0 +1,358 @@
1
+ # Ai sdk API Reference
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.
11
+
12
+ 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
+
14
+ This is useful when building custom streaming endpoints outside Mastra's provided route helpers such as [`chatRoute()`](https://mastra.ai/reference/ai-sdk/chat-route) or [`workflowRoute()`](https://mastra.ai/reference/ai-sdk/workflow-route).
15
+
16
+ ## Usage example
17
+
18
+ Next.js App Router example:
19
+
20
+ ```typescript title="app/api/chat/route.ts"
21
+ import { mastra } from "../../mastra";
22
+ import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
23
+ import { toAISdkStream } from "@mastra/ai-sdk";
24
+
25
+ export async function POST(req: Request) {
26
+ const { messages } = await req.json();
27
+ const myAgent = mastra.getAgent("weatherAgent");
28
+ const stream = await myAgent.stream(messages);
29
+
30
+ const uiMessageStream = createUIMessageStream({
31
+ originalMessages: messages,
32
+ execute: async ({ writer }) => {
33
+ for await (const part of toAISdkStream(stream, { from: "agent" })) {
34
+ await writer.write(part);
35
+ }
36
+ },
37
+ });
38
+
39
+ return createUIMessageStreamResponse({
40
+ stream: uiMessageStream,
41
+ });
42
+ }
43
+ ```
44
+
45
+ > **Note:**
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.
48
+
49
+ ## Parameters
50
+
51
+ The first parameter is the Mastra stream to convert. It can be one of:
52
+ - `MastraModelOutput` - An agent stream from `agent.stream()`
53
+ - `MastraAgentNetworkStream` - A network stream from `agent.network()`
54
+ - `MastraWorkflowStream` or `WorkflowRunOutput` - A workflow stream
55
+
56
+ The second parameter is an options object:
57
+
58
+ ## Examples
59
+
60
+ ### Converting a workflow stream
61
+
62
+ ```typescript title="app/api/workflow/route.ts" {13}
63
+ import { mastra } from "../../mastra";
64
+ import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
65
+ import { toAISdkStream } from "@mastra/ai-sdk";
66
+
67
+ export async function POST(req: Request) {
68
+ const { input } = await req.json();
69
+ const workflow = mastra.getWorkflow("myWorkflow");
70
+ const run = workflow.createRun();
71
+ const stream = await run.stream({ inputData: input });
72
+
73
+ const uiMessageStream = createUIMessageStream({
74
+ execute: async ({ writer }) => {
75
+ for await (const part of toAISdkStream(stream, { from: "workflow" })) {
76
+ await writer.write(part);
77
+ }
78
+ },
79
+ });
80
+
81
+ return createUIMessageStreamResponse({
82
+ stream: uiMessageStream,
83
+ });
84
+ }
85
+ ```
86
+
87
+ ### Converting a network stream
88
+
89
+ ```typescript title="app/api/network/route.ts" {12}
90
+ import { mastra } from "../../mastra";
91
+ import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
92
+ import { toAISdkStream } from "@mastra/ai-sdk";
93
+
94
+ export async function POST(req: Request) {
95
+ const { messages } = await req.json();
96
+ const routingAgent = mastra.getAgent("routingAgent");
97
+ const stream = await routingAgent.network(messages);
98
+
99
+ const uiMessageStream = createUIMessageStream({
100
+ execute: async ({ writer }) => {
101
+ for await (const part of toAISdkStream(stream, { from: "network" })) {
102
+ await writer.write(part);
103
+ }
104
+ },
105
+ });
106
+
107
+ return createUIMessageStreamResponse({
108
+ stream: uiMessageStream,
109
+ });
110
+ }
111
+ ```
112
+
113
+ ### Converting an agent stream with reasoning enabled
114
+
115
+ ```typescript title="app/api/reasoning/route.ts" {8-12,17-20}
116
+ import { mastra } from "../../mastra";
117
+ import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
118
+ import { toAISdkStream } from "@mastra/ai-sdk";
119
+
120
+ export async function POST(req: Request) {
121
+ const { messages } = await req.json();
122
+ const reasoningAgent = mastra.getAgent("reasoningAgent");
123
+ const stream = await reasoningAgent.stream(messages, {
124
+ providerOptions: {
125
+ openai: { reasoningEffort: "high" },
126
+ },
127
+ });
128
+
129
+ const uiMessageStream = createUIMessageStream({
130
+ originalMessages: messages,
131
+ execute: async ({ writer }) => {
132
+ for await (const part of toAISdkStream(stream, {
133
+ from: "agent",
134
+ sendReasoning: true,
135
+ })) {
136
+ await writer.write(part);
137
+ }
138
+ },
139
+ });
140
+
141
+ return createUIMessageStreamResponse({
142
+ stream: uiMessageStream,
143
+ });
144
+ }
145
+ ```
146
+
147
+ ### Using messageMetadata
148
+
149
+ ```typescript title="app/api/chat-with-metadata/route.ts" {13-19}
150
+ import { mastra } from "../../mastra";
151
+ import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
152
+ import { toAISdkStream } from "@mastra/ai-sdk";
153
+
154
+ export async function POST(req: Request) {
155
+ const { messages } = await req.json();
156
+ const myAgent = mastra.getAgent("weatherAgent");
157
+ const stream = await myAgent.stream(messages);
158
+
159
+ const uiMessageStream = createUIMessageStream({
160
+ originalMessages: messages,
161
+ execute: async ({ writer }) => {
162
+ for await (const part of toAISdkStream(stream, {
163
+ from: "agent",
164
+ messageMetadata: ({ part }) => ({
165
+ timestamp: Date.now(),
166
+ partType: part.type,
167
+ }),
168
+ })) {
169
+ await writer.write(part);
170
+ }
171
+ },
172
+ });
173
+
174
+ return createUIMessageStreamResponse({
175
+ stream: uiMessageStream,
176
+ });
177
+ }
178
+ ```
179
+
180
+ ### Client-side stream transformation
181
+
182
+ 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
+
184
+ ```typescript title="client-stream-to-ai-sdk.ts" {14-23,25-35}
185
+ import { MastraClient } from "@mastra/client-js";
186
+ import { createUIMessageStream } from "ai";
187
+ import { toAISdkStream } from "@mastra/ai-sdk";
188
+ import type { ChunkType, MastraModelOutput } from "@mastra/core/stream";
189
+
190
+ const client = new MastraClient({
191
+ baseUrl: "http://localhost:4111",
192
+ });
193
+
194
+ const agent = client.getAgent("weatherAgent");
195
+ const response = await agent.stream("What is the weather in Tokyo?");
196
+
197
+ // Convert the client SDK stream to a ReadableStream<ChunkType>
198
+ const chunkStream = new ReadableStream<ChunkType>({
199
+ async start(controller) {
200
+ await response.processDataStream({
201
+ onChunk: async (chunk) => {
202
+ controller.enqueue(chunk);
203
+ },
204
+ });
205
+ controller.close();
206
+ },
207
+ });
208
+
209
+ // Transform to AI SDK format
210
+ const uiMessageStream = createUIMessageStream({
211
+ execute: async ({ writer }) => {
212
+ for await (const part of toAISdkStream(
213
+ chunkStream as unknown as MastraModelOutput,
214
+ { from: "agent" }
215
+ )) {
216
+ await writer.write(part);
217
+ }
218
+ },
219
+ });
220
+
221
+ for await (const part of uiMessageStream) {
222
+ console.log(part);
223
+ }
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
+ ```