@mastra/ai-sdk 0.0.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.
@@ -0,0 +1,18 @@
1
+
2
+ > @mastra/ai-sdk@0.0.1-alpha.0 build /home/runner/work/mastra/mastra/client-sdks/ai-sdk
3
+ > tsup --config tsup.config.ts
4
+
5
+ CLI Building entry: src/index.ts
6
+ CLI Using tsconfig: tsconfig.json
7
+ CLI tsup v8.5.0
8
+ CLI Using tsup config: /home/runner/work/mastra/mastra/client-sdks/ai-sdk/tsup.config.ts
9
+ CLI Target: es2022
10
+ CLI Cleaning output folder
11
+ ESM Build start
12
+ CJS Build start
13
+ CJS dist/index.cjs 3.77 KB
14
+ CJS dist/index.cjs.map 7.61 KB
15
+ CJS ⚡️ Build success in 542ms
16
+ ESM dist/index.js 3.75 KB
17
+ ESM dist/index.js.map 7.59 KB
18
+ ESM ⚡️ Build success in 543ms
package/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # @mastra/ai-sdk
2
+
3
+ ## 0.0.1-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - [#7263](https://github.com/mastra-ai/mastra/pull/7263) [`9beaeff`](https://github.com/mastra-ai/mastra/commit/9beaeffa4a97b1d5fd01a7f8af8708b16067f67c) Thanks [@wardpeet](https://github.com/wardpeet)! - Create new `@mastra/ai-sdk` package to better support `useChat()`
8
+
9
+ - Updated dependencies [[`ab48c97`](https://github.com/mastra-ai/mastra/commit/ab48c979098ea571faf998a55d3a00e7acd7a715), [`ff89505`](https://github.com/mastra-ai/mastra/commit/ff895057c8c7e91a5535faef46c5e5391085ddfa), [`183dc95`](https://github.com/mastra-ai/mastra/commit/183dc95596f391b977bd1a2c050b8498dac74891), [`a1111e2`](https://github.com/mastra-ai/mastra/commit/a1111e24e705488adfe5e0a6f20c53bddf26cb22), [`61debef`](https://github.com/mastra-ai/mastra/commit/61debefd80ad3a7ed5737e19df6a23d40091689a), [`9beaeff`](https://github.com/mastra-ai/mastra/commit/9beaeffa4a97b1d5fd01a7f8af8708b16067f67c), [`9eee594`](https://github.com/mastra-ai/mastra/commit/9eee594e35e0ca2a650fcc33fa82009a142b9ed0), [`979912c`](https://github.com/mastra-ai/mastra/commit/979912cfd180aad53287cda08af771df26454e2c), [`7dcf4c0`](https://github.com/mastra-ai/mastra/commit/7dcf4c04f44d9345b1f8bc5d41eae3f11ac61611), [`ad78bfc`](https://github.com/mastra-ai/mastra/commit/ad78bfc4ea6a1fff140432bf4f638e01af7af668), [`0ce418a`](https://github.com/mastra-ai/mastra/commit/0ce418a1ccaa5e125d4483a9651b635046152569), [`8387952`](https://github.com/mastra-ai/mastra/commit/838795227b4edf758c84a2adf6f7fba206c27719), [`5eca5d2`](https://github.com/mastra-ai/mastra/commit/5eca5d2655788863ea0442a46c9ef5d3c6dbe0a8)]:
10
+ - @mastra/core@0.15.3-alpha.4
package/LICENSE.md ADDED
@@ -0,0 +1,15 @@
1
+ # Apache License 2.0
2
+
3
+ Copyright (c) 2025 Kepler Software, Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # @mastra/ai-sdk
2
+
3
+ `@mastra/ai-sdk` helps you setup custom API routes to more easily support [`useChat()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @mastra/ai-sdk
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ If you want to use dynamic agents you can use a path with `:agentId`.
14
+
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
+ });
27
+ ```
28
+
29
+ Or you can create a fixed route (i.e. `/chat`):
30
+
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
+ });
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
+ const { error, status, sendMessage, messages, regenerate, stop } = useChat<MyMessage>({
51
+ transport: new DefaultChatTransport({
52
+ api: 'http://localhost:4111/chat/weatherAgent',
53
+ }),
54
+ });
55
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,139 @@
1
+ 'use strict';
2
+
3
+ var server = require('@mastra/core/server');
4
+
5
+ // src/index.ts
6
+ function chatRoute({
7
+ path = "/chat/:agentId",
8
+ agent,
9
+ defaultOptions
10
+ }) {
11
+ if (!agent && !path.includes("/:agentId")) {
12
+ throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
13
+ }
14
+ return server.registerApiRoute(path, {
15
+ method: "POST",
16
+ openapi: {
17
+ summary: "Chat with an agent",
18
+ description: "Send messages to an agent and stream the response in the AI SDK format",
19
+ tags: ["ai-sdk"],
20
+ parameters: [
21
+ {
22
+ name: "agentId",
23
+ in: "path",
24
+ required: true,
25
+ description: "The ID of the agent to chat with",
26
+ schema: {
27
+ type: "string"
28
+ }
29
+ }
30
+ ],
31
+ requestBody: {
32
+ required: true,
33
+ content: {
34
+ "application/json": {
35
+ schema: {
36
+ type: "object",
37
+ properties: {
38
+ messages: {
39
+ type: "array",
40
+ description: "Array of messages in the conversation",
41
+ items: {
42
+ type: "object",
43
+ properties: {
44
+ role: {
45
+ type: "string",
46
+ enum: ["user", "assistant", "system"],
47
+ description: "The role of the message sender"
48
+ },
49
+ content: {
50
+ type: "string",
51
+ description: "The content of the message"
52
+ }
53
+ },
54
+ required: ["role", "content"]
55
+ }
56
+ }
57
+ },
58
+ required: ["messages"]
59
+ }
60
+ }
61
+ }
62
+ },
63
+ responses: {
64
+ "200": {
65
+ description: "Streaming response from the agent",
66
+ content: {
67
+ "text/plain": {
68
+ schema: {
69
+ type: "string",
70
+ description: "Server-sent events stream containing the agent response"
71
+ }
72
+ }
73
+ }
74
+ },
75
+ "400": {
76
+ description: "Bad request - invalid input",
77
+ content: {
78
+ "application/json": {
79
+ schema: {
80
+ type: "object",
81
+ properties: {
82
+ error: {
83
+ type: "string"
84
+ }
85
+ }
86
+ }
87
+ }
88
+ }
89
+ },
90
+ "404": {
91
+ description: "Agent not found",
92
+ content: {
93
+ "application/json": {
94
+ schema: {
95
+ type: "object",
96
+ properties: {
97
+ error: {
98
+ type: "string"
99
+ }
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+ },
107
+ handler: async (c) => {
108
+ const { messages, ...rest } = await c.req.json();
109
+ const mastra = c.get("mastra");
110
+ let agentToUse = agent;
111
+ if (!agent) {
112
+ const agentId = c.req.param("agentId");
113
+ agentToUse = agentId;
114
+ }
115
+ if (c.req.param("agentId") && agent) {
116
+ mastra.getLogger()?.warn(
117
+ `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
118
+ );
119
+ }
120
+ if (!agentToUse) {
121
+ throw new Error("Agent ID is required");
122
+ }
123
+ const agentObj = mastra.getAgent(agentToUse);
124
+ if (!agentObj) {
125
+ throw new Error(`Agent ${agentToUse} not found`);
126
+ }
127
+ const result = await agentObj.streamVNext(messages, {
128
+ ...defaultOptions,
129
+ ...rest,
130
+ format: "aisdk"
131
+ });
132
+ return result.toUIMessageStreamResponse();
133
+ }
134
+ });
135
+ }
136
+
137
+ exports.chatRoute = chatRoute;
138
+ //# sourceMappingURL=index.cjs.map
139
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["registerApiRoute"],"mappings":";;;;;AAuBO,SAAS,SAAA,CAGd;AAAA,EACA,IAAA,GAAO,gBAAA;AAAA,EACP,KAAA;AAAA,EACA;AACF,CAAA,EAAqF;AACnF,EAAA,IAAI,CAAC,KAAA,IAAS,CAAC,IAAA,CAAK,QAAA,CAAS,WAAW,CAAA,EAAG;AACzC,IAAA,MAAM,IAAI,MAAM,uFAAuF,CAAA;AAAA,EACzG;AAEA,EAAA,OAAOA,wBAAiB,IAAA,EAAM;AAAA,IAC5B,MAAA,EAAQ,MAAA;AAAA,IACR,OAAA,EAAS;AAAA,MACP,OAAA,EAAS,oBAAA;AAAA,MACT,WAAA,EAAa,wEAAA;AAAA,MACb,IAAA,EAAM,CAAC,QAAQ,CAAA;AAAA,MACf,UAAA,EAAY;AAAA,QACV;AAAA,UACE,IAAA,EAAM,SAAA;AAAA,UACN,EAAA,EAAI,MAAA;AAAA,UACJ,QAAA,EAAU,IAAA;AAAA,UACV,WAAA,EAAa,kCAAA;AAAA,UACb,MAAA,EAAQ;AAAA,YACN,IAAA,EAAM;AAAA;AACR;AACF,OACF;AAAA,MACA,WAAA,EAAa;AAAA,QACX,QAAA,EAAU,IAAA;AAAA,QACV,OAAA,EAAS;AAAA,UACP,kBAAA,EAAoB;AAAA,YAClB,MAAA,EAAQ;AAAA,cACN,IAAA,EAAM,QAAA;AAAA,cACN,UAAA,EAAY;AAAA,gBACV,QAAA,EAAU;AAAA,kBACR,IAAA,EAAM,OAAA;AAAA,kBACN,WAAA,EAAa,uCAAA;AAAA,kBACb,KAAA,EAAO;AAAA,oBACL,IAAA,EAAM,QAAA;AAAA,oBACN,UAAA,EAAY;AAAA,sBACV,IAAA,EAAM;AAAA,wBACJ,IAAA,EAAM,QAAA;AAAA,wBACN,IAAA,EAAM,CAAC,MAAA,EAAQ,WAA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AAQ,CAAA;AAE7B,MAAA,IAAI,UAAA,GAAiC,KAAA;AACrC,MAAA,IAAI,CAAC,KAAA,EAAO;AACV,QAAA,MAAM,OAAA,GAAU,CAAA,CAAE,GAAA,CAAI,KAAA,CAAM,SAAS,CAAA;AACrC,QAAA,UAAA,GAAa,OAAA;AAAA,MACf;AAEA,MAAA,IAAI,CAAA,CAAE,GAAA,CAAI,KAAA,CAAM,SAAS,KAAK,KAAA,EAAO;AACnC,QAAA,MAAA,CACG,WAAU,EACT,IAAA;AAAA,UACA,CAAA,qGAAA;AAAA,SACF;AAAA,MACJ;AAEA,MAAA,IAAI,CAAC,UAAA,EAAY;AACf,QAAA,MAAM,IAAI,MAAM,sBAAsB,CAAA;AAAA,MACxC;AAEA,MAAA,MAAM,QAAA,GAAW,MAAA,CAAO,QAAA,CAAS,UAAU,CAAA;AAC3C,MAAA,IAAI,CAAC,QAAA,EAAU;AACb,QAAA,MAAM,IAAI,KAAA,CAAM,CAAA,MAAA,EAAS,UAAU,CAAA,UAAA,CAAY,CAAA;AAAA,MACjD;AAEA,MAAA,MAAM,MAAA,GAAS,MAAM,QAAA,CAAS,WAAA,CAA+B,QAAA,EAAU;AAAA,QACrE,GAAG,cAAA;AAAA,QACH,GAAG,IAAA;AAAA,QACH,MAAA,EAAQ;AAAA,OACT,CAAA;AAED,MAAA,OAAO,OAAO,yBAAA,EAA0B;AAAA,IAC1C;AAAA,GACD,CAAA;AACH","file":"index.cjs","sourcesContent":["import type { AgentExecutionOptions } from '@mastra/core/agent';\nimport { registerApiRoute } from '@mastra/core/server';\nimport type { OutputSchema } from '@mastra/core/stream';\nimport type { JSONSchema7 } from 'json-schema';\nimport type { ZodSchema } from 'zod/v3';\nimport type { ZodAny } from 'zod/v4';\n\nexport type chatRouteOptions<\n OUTPUT extends OutputSchema | undefined = undefined,\n STRUCTURED_OUTPUT extends ZodSchema | ZodAny | JSONSchema7 | undefined = undefined,\n> = {\n defaultOptions?: AgentExecutionOptions<OUTPUT, STRUCTURED_OUTPUT, 'aisdk'>;\n} & (\n | {\n path: `${string}:agentId${string}`;\n agent?: never;\n }\n | {\n path: string;\n agent: string;\n }\n);\n\nexport function chatRoute<\n OUTPUT extends OutputSchema | undefined = undefined,\n STRUCTURED_OUTPUT extends ZodSchema | ZodAny | JSONSchema7 | undefined = undefined,\n>({\n path = '/chat/:agentId',\n agent,\n defaultOptions,\n}: chatRouteOptions<OUTPUT, STRUCTURED_OUTPUT>): ReturnType<typeof registerApiRoute> {\n if (!agent && !path.includes('/:agentId')) {\n throw new Error('Path must include :agentId to route to the correct agent or pass the agent explicitly');\n }\n\n return registerApiRoute(path, {\n method: 'POST',\n openapi: {\n summary: 'Chat with an agent',\n description: 'Send messages to an agent and stream the response in the AI SDK format',\n tags: ['ai-sdk'],\n parameters: [\n {\n name: 'agentId',\n in: 'path',\n required: true,\n description: 'The ID of the agent to chat with',\n schema: {\n type: 'string',\n },\n },\n ],\n requestBody: {\n required: true,\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n messages: {\n type: 'array',\n description: 'Array of messages in the conversation',\n items: {\n type: 'object',\n properties: {\n role: {\n type: 'string',\n enum: ['user', 'assistant', 'system'],\n description: 'The role of the message sender',\n },\n content: {\n type: 'string',\n description: 'The content of the message',\n },\n },\n required: ['role', 'content'],\n },\n },\n },\n required: ['messages'],\n },\n },\n },\n },\n responses: {\n '200': {\n description: 'Streaming response from the agent',\n content: {\n 'text/plain': {\n schema: {\n type: 'string',\n description: 'Server-sent events stream containing the agent response',\n },\n },\n },\n },\n '400': {\n description: 'Bad request - invalid input',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n error: {\n type: 'string',\n },\n },\n },\n },\n },\n },\n '404': {\n description: 'Agent not found',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n error: {\n type: 'string',\n },\n },\n },\n },\n },\n },\n },\n },\n handler: async c => {\n const { messages, ...rest } = await c.req.json();\n const mastra = c.get('mastra');\n\n let agentToUse: string | undefined = agent;\n if (!agent) {\n const agentId = c.req.param('agentId');\n agentToUse = agentId;\n }\n\n if (c.req.param('agentId') && agent) {\n mastra\n .getLogger()\n ?.warn(\n `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`,\n );\n }\n\n if (!agentToUse) {\n throw new Error('Agent ID is required');\n }\n\n const agentObj = mastra.getAgent(agentToUse);\n if (!agentObj) {\n throw new Error(`Agent ${agentToUse} not found`);\n }\n\n const result = await agentObj.streamVNext<any, any, 'aisdk'>(messages, {\n ...defaultOptions,\n ...rest,\n format: 'aisdk',\n });\n\n return result.toUIMessageStreamResponse();\n },\n });\n}\n"]}
@@ -0,0 +1,17 @@
1
+ import type { AgentExecutionOptions } from '@mastra/core/agent';
2
+ import { registerApiRoute } from '@mastra/core/server';
3
+ import type { OutputSchema } from '@mastra/core/stream';
4
+ import type { JSONSchema7 } from 'json-schema';
5
+ import type { ZodSchema } from 'zod/v3';
6
+ import type { ZodAny } from 'zod/v4';
7
+ export type chatRouteOptions<OUTPUT extends OutputSchema | undefined = undefined, STRUCTURED_OUTPUT extends ZodSchema | ZodAny | JSONSchema7 | undefined = undefined> = {
8
+ defaultOptions?: AgentExecutionOptions<OUTPUT, STRUCTURED_OUTPUT, 'aisdk'>;
9
+ } & ({
10
+ path: `${string}:agentId${string}`;
11
+ agent?: never;
12
+ } | {
13
+ path: string;
14
+ agent: string;
15
+ });
16
+ export declare function chatRoute<OUTPUT extends OutputSchema | undefined = undefined, STRUCTURED_OUTPUT extends ZodSchema | ZodAny | JSONSchema7 | undefined = undefined>({ path, agent, defaultOptions, }: chatRouteOptions<OUTPUT, STRUCTURED_OUTPUT>): ReturnType<typeof registerApiRoute>;
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACxC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,MAAM,MAAM,gBAAgB,CAC1B,MAAM,SAAS,YAAY,GAAG,SAAS,GAAG,SAAS,EACnD,iBAAiB,SAAS,SAAS,GAAG,MAAM,GAAG,WAAW,GAAG,SAAS,GAAG,SAAS,IAChF;IACF,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,iBAAiB,EAAE,OAAO,CAAC,CAAC;CAC5E,GAAG,CACA;IACE,IAAI,EAAE,GAAG,MAAM,WAAW,MAAM,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,KAAK,CAAC;CACf,GACD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf,CACJ,CAAC;AAEF,wBAAgB,SAAS,CACvB,MAAM,SAAS,YAAY,GAAG,SAAS,GAAG,SAAS,EACnD,iBAAiB,SAAS,SAAS,GAAG,MAAM,GAAG,WAAW,GAAG,SAAS,GAAG,SAAS,EAClF,EACA,IAAuB,EACvB,KAAK,EACL,cAAc,GACf,EAAE,gBAAgB,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAsInF"}
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
1
+ import { registerApiRoute } from '@mastra/core/server';
2
+
3
+ // src/index.ts
4
+ function chatRoute({
5
+ path = "/chat/:agentId",
6
+ agent,
7
+ defaultOptions
8
+ }) {
9
+ if (!agent && !path.includes("/:agentId")) {
10
+ throw new Error("Path must include :agentId to route to the correct agent or pass the agent explicitly");
11
+ }
12
+ return registerApiRoute(path, {
13
+ method: "POST",
14
+ openapi: {
15
+ summary: "Chat with an agent",
16
+ description: "Send messages to an agent and stream the response in the AI SDK format",
17
+ tags: ["ai-sdk"],
18
+ parameters: [
19
+ {
20
+ name: "agentId",
21
+ in: "path",
22
+ required: true,
23
+ description: "The ID of the agent to chat with",
24
+ schema: {
25
+ type: "string"
26
+ }
27
+ }
28
+ ],
29
+ requestBody: {
30
+ required: true,
31
+ content: {
32
+ "application/json": {
33
+ schema: {
34
+ type: "object",
35
+ properties: {
36
+ messages: {
37
+ type: "array",
38
+ description: "Array of messages in the conversation",
39
+ items: {
40
+ type: "object",
41
+ properties: {
42
+ role: {
43
+ type: "string",
44
+ enum: ["user", "assistant", "system"],
45
+ description: "The role of the message sender"
46
+ },
47
+ content: {
48
+ type: "string",
49
+ description: "The content of the message"
50
+ }
51
+ },
52
+ required: ["role", "content"]
53
+ }
54
+ }
55
+ },
56
+ required: ["messages"]
57
+ }
58
+ }
59
+ }
60
+ },
61
+ responses: {
62
+ "200": {
63
+ description: "Streaming response from the agent",
64
+ content: {
65
+ "text/plain": {
66
+ schema: {
67
+ type: "string",
68
+ description: "Server-sent events stream containing the agent response"
69
+ }
70
+ }
71
+ }
72
+ },
73
+ "400": {
74
+ description: "Bad request - invalid input",
75
+ content: {
76
+ "application/json": {
77
+ schema: {
78
+ type: "object",
79
+ properties: {
80
+ error: {
81
+ type: "string"
82
+ }
83
+ }
84
+ }
85
+ }
86
+ }
87
+ },
88
+ "404": {
89
+ description: "Agent not found",
90
+ content: {
91
+ "application/json": {
92
+ schema: {
93
+ type: "object",
94
+ properties: {
95
+ error: {
96
+ type: "string"
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+ }
103
+ }
104
+ },
105
+ handler: async (c) => {
106
+ const { messages, ...rest } = await c.req.json();
107
+ const mastra = c.get("mastra");
108
+ let agentToUse = agent;
109
+ if (!agent) {
110
+ const agentId = c.req.param("agentId");
111
+ agentToUse = agentId;
112
+ }
113
+ if (c.req.param("agentId") && agent) {
114
+ mastra.getLogger()?.warn(
115
+ `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`
116
+ );
117
+ }
118
+ if (!agentToUse) {
119
+ throw new Error("Agent ID is required");
120
+ }
121
+ const agentObj = mastra.getAgent(agentToUse);
122
+ if (!agentObj) {
123
+ throw new Error(`Agent ${agentToUse} not found`);
124
+ }
125
+ const result = await agentObj.streamVNext(messages, {
126
+ ...defaultOptions,
127
+ ...rest,
128
+ format: "aisdk"
129
+ });
130
+ return result.toUIMessageStreamResponse();
131
+ }
132
+ });
133
+ }
134
+
135
+ export { chatRoute };
136
+ //# sourceMappingURL=index.js.map
137
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAuBO,SAAS,SAAA,CAGd;AAAA,EACA,IAAA,GAAO,gBAAA;AAAA,EACP,KAAA;AAAA,EACA;AACF,CAAA,EAAqF;AACnF,EAAA,IAAI,CAAC,KAAA,IAAS,CAAC,IAAA,CAAK,QAAA,CAAS,WAAW,CAAA,EAAG;AACzC,IAAA,MAAM,IAAI,MAAM,uFAAuF,CAAA;AAAA,EACzG;AAEA,EAAA,OAAO,iBAAiB,IAAA,EAAM;AAAA,IAC5B,MAAA,EAAQ,MAAA;AAAA,IACR,OAAA,EAAS;AAAA,MACP,OAAA,EAAS,oBAAA;AAAA,MACT,WAAA,EAAa,wEAAA;AAAA,MACb,IAAA,EAAM,CAAC,QAAQ,CAAA;AAAA,MACf,UAAA,EAAY;AAAA,QACV;AAAA,UACE,IAAA,EAAM,SAAA;AAAA,UACN,EAAA,EAAI,MAAA;AAAA,UACJ,QAAA,EAAU,IAAA;AAAA,UACV,WAAA,EAAa,kCAAA;AAAA,UACb,MAAA,EAAQ;AAAA,YACN,IAAA,EAAM;AAAA;AACR;AACF,OACF;AAAA,MACA,WAAA,EAAa;AAAA,QACX,QAAA,EAAU,IAAA;AAAA,QACV,OAAA,EAAS;AAAA,UACP,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A,GACD,CAAA;AACH","file":"index.js","sourcesContent":["import type { AgentExecutionOptions } from '@mastra/core/agent';\nimport { registerApiRoute } from '@mastra/core/server';\nimport type { OutputSchema } from '@mastra/core/stream';\nimport type { JSONSchema7 } from 'json-schema';\nimport type { ZodSchema } from 'zod/v3';\nimport type { ZodAny } from 'zod/v4';\n\nexport type chatRouteOptions<\n OUTPUT extends OutputSchema | undefined = undefined,\n STRUCTURED_OUTPUT extends ZodSchema | ZodAny | JSONSchema7 | undefined = undefined,\n> = {\n defaultOptions?: AgentExecutionOptions<OUTPUT, STRUCTURED_OUTPUT, 'aisdk'>;\n} & (\n | {\n path: `${string}:agentId${string}`;\n agent?: never;\n }\n | {\n path: string;\n agent: string;\n }\n);\n\nexport function chatRoute<\n OUTPUT extends OutputSchema | undefined = undefined,\n STRUCTURED_OUTPUT extends ZodSchema | ZodAny | JSONSchema7 | undefined = undefined,\n>({\n path = '/chat/:agentId',\n agent,\n defaultOptions,\n}: chatRouteOptions<OUTPUT, STRUCTURED_OUTPUT>): ReturnType<typeof registerApiRoute> {\n if (!agent && !path.includes('/:agentId')) {\n throw new Error('Path must include :agentId to route to the correct agent or pass the agent explicitly');\n }\n\n return registerApiRoute(path, {\n method: 'POST',\n openapi: {\n summary: 'Chat with an agent',\n description: 'Send messages to an agent and stream the response in the AI SDK format',\n tags: ['ai-sdk'],\n parameters: [\n {\n name: 'agentId',\n in: 'path',\n required: true,\n description: 'The ID of the agent to chat with',\n schema: {\n type: 'string',\n },\n },\n ],\n requestBody: {\n required: true,\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n messages: {\n type: 'array',\n description: 'Array of messages in the conversation',\n items: {\n type: 'object',\n properties: {\n role: {\n type: 'string',\n enum: ['user', 'assistant', 'system'],\n description: 'The role of the message sender',\n },\n content: {\n type: 'string',\n description: 'The content of the message',\n },\n },\n required: ['role', 'content'],\n },\n },\n },\n required: ['messages'],\n },\n },\n },\n },\n responses: {\n '200': {\n description: 'Streaming response from the agent',\n content: {\n 'text/plain': {\n schema: {\n type: 'string',\n description: 'Server-sent events stream containing the agent response',\n },\n },\n },\n },\n '400': {\n description: 'Bad request - invalid input',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n error: {\n type: 'string',\n },\n },\n },\n },\n },\n },\n '404': {\n description: 'Agent not found',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n error: {\n type: 'string',\n },\n },\n },\n },\n },\n },\n },\n },\n handler: async c => {\n const { messages, ...rest } = await c.req.json();\n const mastra = c.get('mastra');\n\n let agentToUse: string | undefined = agent;\n if (!agent) {\n const agentId = c.req.param('agentId');\n agentToUse = agentId;\n }\n\n if (c.req.param('agentId') && agent) {\n mastra\n .getLogger()\n ?.warn(\n `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`,\n );\n }\n\n if (!agentToUse) {\n throw new Error('Agent ID is required');\n }\n\n const agentObj = mastra.getAgent(agentToUse);\n if (!agentObj) {\n throw new Error(`Agent ${agentToUse} not found`);\n }\n\n const result = await agentObj.streamVNext<any, any, 'aisdk'>(messages, {\n ...defaultOptions,\n ...rest,\n format: 'aisdk',\n });\n\n return result.toUIMessageStreamResponse();\n },\n });\n}\n"]}
@@ -0,0 +1,6 @@
1
+ import { createConfig } from '@internal/lint/eslint';
2
+
3
+ const config = await createConfig();
4
+
5
+ /** @type {import("eslint").Linter.Config[]} */
6
+ export default [...config.map(conf => ({ ...conf, ignores: [...(conf.ignores || []), '**/vitest.perf.config.ts'] }))];
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@mastra/ai-sdk",
3
+ "version": "0.0.1-alpha.0",
4
+ "description": "Adds custom API routes to be compatible with the AI SDK UI parts",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "import": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "require": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.cjs"
16
+ }
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "peerDependencies": {
21
+ "@mastra/core": ">=0.15.3-0 <0.16.0-0",
22
+ "zod": "^3.25.0 || ^4.0.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/json-schema": "^7.0.15",
26
+ "eslint": "^9.34.0",
27
+ "tsup": "^8.5.0",
28
+ "typescript": "^5.8.3",
29
+ "vitest": "^3.2.4",
30
+ "zod": "^3.25.67",
31
+ "@internal/lint": "0.0.34",
32
+ "@internal/types-builder": "0.0.9",
33
+ "@mastra/core": "0.15.3-alpha.4"
34
+ },
35
+ "keywords": [],
36
+ "author": "",
37
+ "license": "Apache-2.0",
38
+ "scripts": {
39
+ "lint": "eslint .",
40
+ "build": "tsup --config tsup.config.ts",
41
+ "test": "vitest run"
42
+ }
43
+ }
package/src/index.ts ADDED
@@ -0,0 +1,165 @@
1
+ import type { AgentExecutionOptions } from '@mastra/core/agent';
2
+ import { registerApiRoute } from '@mastra/core/server';
3
+ import type { OutputSchema } from '@mastra/core/stream';
4
+ import type { JSONSchema7 } from 'json-schema';
5
+ import type { ZodSchema } from 'zod/v3';
6
+ import type { ZodAny } from 'zod/v4';
7
+
8
+ export type chatRouteOptions<
9
+ OUTPUT extends OutputSchema | undefined = undefined,
10
+ STRUCTURED_OUTPUT extends ZodSchema | ZodAny | JSONSchema7 | undefined = undefined,
11
+ > = {
12
+ defaultOptions?: AgentExecutionOptions<OUTPUT, STRUCTURED_OUTPUT, 'aisdk'>;
13
+ } & (
14
+ | {
15
+ path: `${string}:agentId${string}`;
16
+ agent?: never;
17
+ }
18
+ | {
19
+ path: string;
20
+ agent: string;
21
+ }
22
+ );
23
+
24
+ export function chatRoute<
25
+ OUTPUT extends OutputSchema | undefined = undefined,
26
+ STRUCTURED_OUTPUT extends ZodSchema | ZodAny | JSONSchema7 | undefined = undefined,
27
+ >({
28
+ path = '/chat/:agentId',
29
+ agent,
30
+ defaultOptions,
31
+ }: chatRouteOptions<OUTPUT, STRUCTURED_OUTPUT>): ReturnType<typeof registerApiRoute> {
32
+ if (!agent && !path.includes('/:agentId')) {
33
+ throw new Error('Path must include :agentId to route to the correct agent or pass the agent explicitly');
34
+ }
35
+
36
+ return registerApiRoute(path, {
37
+ method: 'POST',
38
+ openapi: {
39
+ summary: 'Chat with an agent',
40
+ description: 'Send messages to an agent and stream the response in the AI SDK format',
41
+ tags: ['ai-sdk'],
42
+ parameters: [
43
+ {
44
+ name: 'agentId',
45
+ in: 'path',
46
+ required: true,
47
+ description: 'The ID of the agent to chat with',
48
+ schema: {
49
+ type: 'string',
50
+ },
51
+ },
52
+ ],
53
+ requestBody: {
54
+ required: true,
55
+ content: {
56
+ 'application/json': {
57
+ schema: {
58
+ type: 'object',
59
+ properties: {
60
+ messages: {
61
+ type: 'array',
62
+ description: 'Array of messages in the conversation',
63
+ items: {
64
+ type: 'object',
65
+ properties: {
66
+ role: {
67
+ type: 'string',
68
+ enum: ['user', 'assistant', 'system'],
69
+ description: 'The role of the message sender',
70
+ },
71
+ content: {
72
+ type: 'string',
73
+ description: 'The content of the message',
74
+ },
75
+ },
76
+ required: ['role', 'content'],
77
+ },
78
+ },
79
+ },
80
+ required: ['messages'],
81
+ },
82
+ },
83
+ },
84
+ },
85
+ responses: {
86
+ '200': {
87
+ description: 'Streaming response from the agent',
88
+ content: {
89
+ 'text/plain': {
90
+ schema: {
91
+ type: 'string',
92
+ description: 'Server-sent events stream containing the agent response',
93
+ },
94
+ },
95
+ },
96
+ },
97
+ '400': {
98
+ description: 'Bad request - invalid input',
99
+ content: {
100
+ 'application/json': {
101
+ schema: {
102
+ type: 'object',
103
+ properties: {
104
+ error: {
105
+ type: 'string',
106
+ },
107
+ },
108
+ },
109
+ },
110
+ },
111
+ },
112
+ '404': {
113
+ description: 'Agent not found',
114
+ content: {
115
+ 'application/json': {
116
+ schema: {
117
+ type: 'object',
118
+ properties: {
119
+ error: {
120
+ type: 'string',
121
+ },
122
+ },
123
+ },
124
+ },
125
+ },
126
+ },
127
+ },
128
+ },
129
+ handler: async c => {
130
+ const { messages, ...rest } = await c.req.json();
131
+ const mastra = c.get('mastra');
132
+
133
+ let agentToUse: string | undefined = agent;
134
+ if (!agent) {
135
+ const agentId = c.req.param('agentId');
136
+ agentToUse = agentId;
137
+ }
138
+
139
+ if (c.req.param('agentId') && agent) {
140
+ mastra
141
+ .getLogger()
142
+ ?.warn(
143
+ `Fixed agent ID was set together with an agentId path parameter. This can lead to unexpected behavior.`,
144
+ );
145
+ }
146
+
147
+ if (!agentToUse) {
148
+ throw new Error('Agent ID is required');
149
+ }
150
+
151
+ const agentObj = mastra.getAgent(agentToUse);
152
+ if (!agentObj) {
153
+ throw new Error(`Agent ${agentToUse} not found`);
154
+ }
155
+
156
+ const result = await agentObj.streamVNext<any, any, 'aisdk'>(messages, {
157
+ ...defaultOptions,
158
+ ...rest,
159
+ format: 'aisdk',
160
+ });
161
+
162
+ return result.toUIMessageStreamResponse();
163
+ },
164
+ });
165
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": ["./tsconfig.json", "../../tsconfig.build.json"],
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "**/*.test.ts", "src/**/*.mock.ts"]
9
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.node.json",
3
+ "include": ["src/**/*", "tsup.config.ts", "integration-tests/**/*"],
4
+ "exclude": ["node_modules", "integration-tests/node_modules", "src/example.ts", "**/*.test.ts"]
5
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { generateTypes } from '@internal/types-builder';
2
+ import { defineConfig } from 'tsup';
3
+
4
+ export default defineConfig({
5
+ entry: ['src/index.ts'],
6
+ format: ['esm', 'cjs'],
7
+ clean: true,
8
+ dts: false,
9
+ splitting: true,
10
+ treeshake: {
11
+ preset: 'smallest',
12
+ },
13
+ sourcemap: true,
14
+ onSuccess: async () => {
15
+ await generateTypes(process.cwd());
16
+ },
17
+ });
@@ -0,0 +1,8 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ environment: 'node',
6
+ include: ['src/**/*.test.ts'],
7
+ },
8
+ });