@mastra/ai-sdk 0.0.4 → 0.0.5

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 CHANGED
@@ -1,5 +1,27 @@
1
1
  # @mastra/ai-sdk
2
2
 
3
+ ## 0.0.5
4
+
5
+ ### Patch Changes
6
+
7
+ - Update peer deps ([#8154](https://github.com/mastra-ai/mastra/pull/8154))
8
+
9
+ - Add transformer to support workflows in useChat ([#7829](https://github.com/mastra-ai/mastra/pull/7829))
10
+
11
+ - Updated dependencies [[`dc099b4`](https://github.com/mastra-ai/mastra/commit/dc099b40fb31147ba3f362f98d991892033c4c67), [`504438b`](https://github.com/mastra-ai/mastra/commit/504438b961bde211071186bba63a842c4e3db879), [`b342a68`](https://github.com/mastra-ai/mastra/commit/b342a68e1399cf1ece9ba11bda112db89d21118c), [`a7243e2`](https://github.com/mastra-ai/mastra/commit/a7243e2e58762667a6e3921e755e89d6bb0a3282), [`7fceb0a`](https://github.com/mastra-ai/mastra/commit/7fceb0a327d678e812f90f5387c5bc4f38bd039e), [`303a9c0`](https://github.com/mastra-ai/mastra/commit/303a9c0d7dd58795915979f06a0512359e4532fb), [`df64f9e`](https://github.com/mastra-ai/mastra/commit/df64f9ef814916fff9baedd861c988084e7c41de), [`370f8a6`](https://github.com/mastra-ai/mastra/commit/370f8a6480faec70fef18d72e5f7538f27004301), [`809eea0`](https://github.com/mastra-ai/mastra/commit/809eea092fa80c3f69b9eaf078d843b57fd2a88e), [`683e5a1`](https://github.com/mastra-ai/mastra/commit/683e5a1466e48b686825b2c11f84680f296138e4), [`3679378`](https://github.com/mastra-ai/mastra/commit/3679378673350aa314741dc826f837b1984149bc), [`7775bc2`](https://github.com/mastra-ai/mastra/commit/7775bc20bb1ad1ab24797fb420e4f96c65b0d8ec), [`623ffaf`](https://github.com/mastra-ai/mastra/commit/623ffaf2d969e11e99a0224633cf7b5a0815c857), [`9fc1613`](https://github.com/mastra-ai/mastra/commit/9fc16136400186648880fd990119ac15f7c02ee4), [`61f62aa`](https://github.com/mastra-ai/mastra/commit/61f62aa31bc88fe4ddf8da6240dbcfbeb07358bd), [`db1891a`](https://github.com/mastra-ai/mastra/commit/db1891a4707443720b7cd8a260dc7e1d49b3609c), [`e8f379d`](https://github.com/mastra-ai/mastra/commit/e8f379d390efa264c4e0874f9ac0cf8839b07777), [`652066b`](https://github.com/mastra-ai/mastra/commit/652066bd1efc6bb6813ba950ed1d7573e8b7d9d4), [`3e292ba`](https://github.com/mastra-ai/mastra/commit/3e292ba00837886d5d68a34cbc0d9b703c991883), [`418c136`](https://github.com/mastra-ai/mastra/commit/418c1366843d88e491bca3f87763899ce855ca29), [`ea8d386`](https://github.com/mastra-ai/mastra/commit/ea8d386cd8c5593664515fd5770c06bf2aa980ef), [`67b0f00`](https://github.com/mastra-ai/mastra/commit/67b0f005b520335c71fb85cbaa25df4ce8484a81), [`c2a4919`](https://github.com/mastra-ai/mastra/commit/c2a4919ba6797d8bdb1509e02287496eef69303e), [`c84b7d0`](https://github.com/mastra-ai/mastra/commit/c84b7d093c4657772140cbfd2b15ef72f3315ed5), [`0130986`](https://github.com/mastra-ai/mastra/commit/0130986fc62d0edcc626dd593282661dbb9af141)]:
12
+ - @mastra/core@0.19.0
13
+
14
+ ## 0.0.5-alpha.0
15
+
16
+ ### Patch Changes
17
+
18
+ - Update peer deps ([#8154](https://github.com/mastra-ai/mastra/pull/8154))
19
+
20
+ - Add transformer to support workflows in useChat ([#7829](https://github.com/mastra-ai/mastra/pull/7829))
21
+
22
+ - Updated dependencies [[`504438b`](https://github.com/mastra-ai/mastra/commit/504438b961bde211071186bba63a842c4e3db879), [`a7243e2`](https://github.com/mastra-ai/mastra/commit/a7243e2e58762667a6e3921e755e89d6bb0a3282), [`7fceb0a`](https://github.com/mastra-ai/mastra/commit/7fceb0a327d678e812f90f5387c5bc4f38bd039e), [`df64f9e`](https://github.com/mastra-ai/mastra/commit/df64f9ef814916fff9baedd861c988084e7c41de), [`809eea0`](https://github.com/mastra-ai/mastra/commit/809eea092fa80c3f69b9eaf078d843b57fd2a88e), [`683e5a1`](https://github.com/mastra-ai/mastra/commit/683e5a1466e48b686825b2c11f84680f296138e4), [`3679378`](https://github.com/mastra-ai/mastra/commit/3679378673350aa314741dc826f837b1984149bc), [`7775bc2`](https://github.com/mastra-ai/mastra/commit/7775bc20bb1ad1ab24797fb420e4f96c65b0d8ec), [`db1891a`](https://github.com/mastra-ai/mastra/commit/db1891a4707443720b7cd8a260dc7e1d49b3609c), [`e8f379d`](https://github.com/mastra-ai/mastra/commit/e8f379d390efa264c4e0874f9ac0cf8839b07777), [`652066b`](https://github.com/mastra-ai/mastra/commit/652066bd1efc6bb6813ba950ed1d7573e8b7d9d4), [`ea8d386`](https://github.com/mastra-ai/mastra/commit/ea8d386cd8c5593664515fd5770c06bf2aa980ef), [`c2a4919`](https://github.com/mastra-ai/mastra/commit/c2a4919ba6797d8bdb1509e02287496eef69303e), [`0130986`](https://github.com/mastra-ai/mastra/commit/0130986fc62d0edcc626dd593282661dbb9af141)]:
23
+ - @mastra/core@0.19.0-alpha.1
24
+
3
25
  ## 0.0.4
4
26
 
5
27
  ### Patch Changes
@@ -0,0 +1,14 @@
1
+ import type { AgentExecutionOptions } from '@mastra/core/agent';
2
+ import { registerApiRoute } from '@mastra/core/server';
3
+ import type { OutputSchema } from '@mastra/core/stream';
4
+ export type chatRouteOptions<OUTPUT extends OutputSchema = undefined> = {
5
+ defaultOptions?: AgentExecutionOptions<OUTPUT, 'aisdk'>;
6
+ } & ({
7
+ path: `${string}:agentId${string}`;
8
+ agent?: never;
9
+ } | {
10
+ path: string;
11
+ agent: string;
12
+ });
13
+ export declare function chatRoute<OUTPUT extends OutputSchema = undefined>({ path, agent, defaultOptions, }: chatRouteOptions<OUTPUT>): ReturnType<typeof registerApiRoute>;
14
+ //# sourceMappingURL=chat-route.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat-route.d.ts","sourceRoot":"","sources":["../src/chat-route.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;AAExD,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IACtE,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzD,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,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,EAAE,EACjE,IAAuB,EACvB,KAAK,EACL,cAAc,GACf,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAsIhE"}
package/dist/index.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  var server = require('@mastra/core/server');
4
4
 
5
- // src/index.ts
5
+ // src/chat-route.ts
6
6
  function chatRoute({
7
7
  path = "/chat/:agentId",
8
8
  agent,
@@ -134,6 +134,105 @@ function chatRoute({
134
134
  });
135
135
  }
136
136
 
137
+ // src/to-ai-sdk-format.ts
138
+ function WokflowStreamToAISDKTransformer() {
139
+ const steps = {};
140
+ return new TransformStream({
141
+ start(controller) {
142
+ controller.enqueue({
143
+ data: JSON.stringify({
144
+ type: "start",
145
+ messageId: "1"
146
+ })
147
+ });
148
+ },
149
+ flush(controller) {
150
+ controller.enqueue({
151
+ data: JSON.stringify({
152
+ type: "finish"
153
+ })
154
+ });
155
+ controller.enqueue({
156
+ data: "[DONE]"
157
+ });
158
+ },
159
+ transform(chunk, controller) {
160
+ let workflowName = "";
161
+ if (chunk.type === "workflow-start") {
162
+ workflowName = chunk.payload.workflowId;
163
+ controller.enqueue({
164
+ data: JSON.stringify({
165
+ type: "data-workflow",
166
+ id: chunk.runId,
167
+ data: {
168
+ name: workflowName,
169
+ status: "running",
170
+ steps: {},
171
+ output: null
172
+ }
173
+ })
174
+ });
175
+ } else if (chunk.type === "workflow-step-start") {
176
+ steps[chunk.payload.id] = {
177
+ // TODO swap with name
178
+ name: chunk.payload.id,
179
+ status: chunk.payload.status,
180
+ input: chunk.payload.payload ?? null,
181
+ output: null
182
+ };
183
+ controller.enqueue({
184
+ data: JSON.stringify({
185
+ type: "data-workflow",
186
+ id: chunk.runId,
187
+ data: {
188
+ name: workflowName,
189
+ status: "running",
190
+ steps,
191
+ output: null
192
+ }
193
+ })
194
+ });
195
+ } else if (chunk.type === "workflow-step-result") {
196
+ steps[chunk.payload.id] = {
197
+ ...steps[chunk.payload.id],
198
+ status: chunk.payload.status,
199
+ output: chunk.payload.output ?? null
200
+ };
201
+ controller.enqueue({
202
+ data: JSON.stringify({
203
+ type: "data-workflow",
204
+ id: chunk.runId,
205
+ data: {
206
+ name: workflowName,
207
+ status: "running",
208
+ steps,
209
+ output: null
210
+ }
211
+ })
212
+ });
213
+ } else if (chunk.type === "workflow-finish") {
214
+ controller.enqueue({
215
+ data: JSON.stringify({
216
+ type: "data-workflow",
217
+ id: chunk.runId,
218
+ data: {
219
+ name: workflowName,
220
+ steps,
221
+ output: chunk.payload.output ?? null,
222
+ status: chunk.payload.workflowStatus
223
+ }
224
+ })
225
+ });
226
+ }
227
+ }
228
+ });
229
+ }
230
+ function toAISdkFormat(stream) {
231
+ return stream.pipeThrough(WokflowStreamToAISDKTransformer());
232
+ }
233
+
234
+ exports.WokflowStreamToAISDKTransformer = WokflowStreamToAISDKTransformer;
137
235
  exports.chatRoute = chatRoute;
236
+ exports.toAISdkFormat = toAISdkFormat;
138
237
  //# sourceMappingURL=index.cjs.map
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,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,CAA6B,QAAA,EAAU;AAAA,QACnE,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';\n\nexport type chatRouteOptions<OUTPUT extends OutputSchema = undefined> = {\n defaultOptions?: AgentExecutionOptions<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<OUTPUT extends OutputSchema = undefined>({\n path = '/chat/:agentId',\n agent,\n defaultOptions,\n}: chatRouteOptions<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<OUTPUT, 'aisdk'>(messages, {\n ...defaultOptions,\n ...rest,\n format: 'aisdk',\n });\n\n return result.toUIMessageStreamResponse();\n },\n });\n}\n"]}
1
+ {"version":3,"sources":["../src/chat-route.ts","../src/to-ai-sdk-format.ts"],"names":["registerApiRoute"],"mappings":";;;;;AAiBO,SAAS,SAAA,CAAmD;AAAA,EACjE,IAAA,GAAO,gBAAA;AAAA,EACP,KAAA;AAAA,EACA;AACF,CAAA,EAAkE;AAChE,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;A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Q;AAAA;AACxB,WAC2B;AAAA,SAC9B,CAAA;AAAA,MACH;AAAA,IACF;AAAA,GACD,CAAA;AACH;AAEO,SAAS,cAId,MAAA,EAAuD;AACvD,EAAA,OAAO,MAAA,CAAO,WAAA,CAAY,+BAAA,EAAiC,CAAA;AAC7D","file":"index.cjs","sourcesContent":["import type { AgentExecutionOptions } from '@mastra/core/agent';\nimport { registerApiRoute } from '@mastra/core/server';\nimport type { OutputSchema } from '@mastra/core/stream';\n\nexport type chatRouteOptions<OUTPUT extends OutputSchema = undefined> = {\n defaultOptions?: AgentExecutionOptions<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<OUTPUT extends OutputSchema = undefined>({\n path = '/chat/:agentId',\n agent,\n defaultOptions,\n}: chatRouteOptions<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<OUTPUT, 'aisdk'>(messages, {\n ...defaultOptions,\n ...rest,\n format: 'aisdk',\n });\n\n return result.toUIMessageStreamResponse();\n },\n });\n}\n","import type {\n ChunkType,\n MastraWorkflowStream,\n Step,\n WorkflowRunStatus,\n WorkflowStepStatus,\n} from '@mastra/core/workflows';\nimport type { ZodType } from 'zod';\n\ntype StepResult = {\n name: string;\n status: WorkflowStepStatus;\n input: Record<string, unknown> | null;\n output: Record<string, unknown> | null;\n};\n\nexport type WorkflowAiSDKType = {\n type: 'data-workflow';\n id: string;\n data: {\n name: string;\n status: WorkflowRunStatus;\n steps: Record<string, StepResult>;\n output: {\n usage: {\n inputTokens: number;\n outputTokens: number;\n totalTokens: number;\n };\n } | null;\n };\n};\n\nexport function WokflowStreamToAISDKTransformer() {\n const steps: Record<string, StepResult> = {};\n return new TransformStream<\n ChunkType,\n {\n data: string;\n }\n >({\n start(controller) {\n controller.enqueue({\n data: JSON.stringify({\n type: 'start',\n messageId: '1',\n }),\n });\n },\n flush(controller) {\n controller.enqueue({\n data: JSON.stringify({\n type: 'finish',\n }),\n });\n controller.enqueue({\n data: '[DONE]',\n });\n },\n transform(chunk, controller) {\n let workflowName = '';\n if (chunk.type === 'workflow-start') {\n // TODO swap with name\n workflowName = chunk.payload.workflowId;\n controller.enqueue({\n data: JSON.stringify({\n type: 'data-workflow',\n id: chunk.runId,\n data: {\n name: workflowName,\n status: 'running',\n steps: {} as Record<string, StepResult>,\n output: null,\n },\n } satisfies WorkflowAiSDKType),\n });\n } else if (chunk.type === 'workflow-step-start') {\n steps[chunk.payload.id] = {\n // TODO swap with name\n name: chunk.payload.id,\n status: chunk.payload.status,\n input: chunk.payload.payload ?? null,\n output: null,\n } satisfies StepResult;\n\n controller.enqueue({\n data: JSON.stringify({\n type: 'data-workflow',\n id: chunk.runId,\n data: {\n name: workflowName,\n status: 'running',\n steps,\n output: null,\n },\n } satisfies WorkflowAiSDKType),\n });\n } else if (chunk.type === 'workflow-step-result') {\n steps[chunk.payload.id] = {\n ...steps[chunk.payload.id]!,\n status: chunk.payload.status,\n output: chunk.payload.output ?? null,\n } satisfies StepResult;\n\n controller.enqueue({\n data: JSON.stringify({\n type: 'data-workflow',\n id: chunk.runId,\n data: {\n name: workflowName,\n status: 'running',\n steps,\n output: null,\n },\n } satisfies WorkflowAiSDKType),\n });\n } else if (chunk.type === 'workflow-finish') {\n controller.enqueue({\n data: JSON.stringify({\n type: 'data-workflow',\n id: chunk.runId,\n data: {\n name: workflowName,\n steps,\n output: chunk.payload.output ?? null,\n status: chunk.payload.workflowStatus,\n },\n } satisfies WorkflowAiSDKType),\n });\n }\n },\n });\n}\n\nexport function toAISdkFormat<\n TInput extends ZodType<any>,\n TOutput extends ZodType<any>,\n TSteps extends Step<string, any, any>[],\n>(stream: MastraWorkflowStream<TInput, TOutput, TSteps>) {\n return stream.pipeThrough(WokflowStreamToAISDKTransformer());\n}\n"]}
package/dist/index.d.ts CHANGED
@@ -1,14 +1,6 @@
1
- import type { AgentExecutionOptions } from '@mastra/core/agent';
2
- import { registerApiRoute } from '@mastra/core/server';
3
- import type { OutputSchema } from '@mastra/core/stream';
4
- export type chatRouteOptions<OUTPUT extends OutputSchema = undefined> = {
5
- defaultOptions?: AgentExecutionOptions<OUTPUT, 'aisdk'>;
6
- } & ({
7
- path: `${string}:agentId${string}`;
8
- agent?: never;
9
- } | {
10
- path: string;
11
- agent: string;
12
- });
13
- export declare function chatRoute<OUTPUT extends OutputSchema = undefined>({ path, agent, defaultOptions, }: chatRouteOptions<OUTPUT>): ReturnType<typeof registerApiRoute>;
1
+ export { chatRoute } from './chat-route.js';
2
+ export type { chatRouteOptions } from './chat-route.js';
3
+ export { toAISdkFormat } from './to-ai-sdk-format.js';
4
+ export { WokflowStreamToAISDKTransformer } from './to-ai-sdk-format.js';
5
+ export type { WorkflowAiSDKType } from './to-ai-sdk-format.js';
14
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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;AAExD,MAAM,MAAM,gBAAgB,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,IAAI;IACtE,cAAc,CAAC,EAAE,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACzD,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,CAAC,MAAM,SAAS,YAAY,GAAG,SAAS,EAAE,EACjE,IAAuB,EACvB,KAAK,EACL,cAAc,GACf,EAAE,gBAAgB,CAAC,MAAM,CAAC,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAsIhE"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,+BAA+B,EAAE,MAAM,oBAAoB,CAAC;AACrE,YAAY,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC"}
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { registerApiRoute } from '@mastra/core/server';
2
2
 
3
- // src/index.ts
3
+ // src/chat-route.ts
4
4
  function chatRoute({
5
5
  path = "/chat/:agentId",
6
6
  agent,
@@ -132,6 +132,103 @@ function chatRoute({
132
132
  });
133
133
  }
134
134
 
135
- export { chatRoute };
135
+ // src/to-ai-sdk-format.ts
136
+ function WokflowStreamToAISDKTransformer() {
137
+ const steps = {};
138
+ return new TransformStream({
139
+ start(controller) {
140
+ controller.enqueue({
141
+ data: JSON.stringify({
142
+ type: "start",
143
+ messageId: "1"
144
+ })
145
+ });
146
+ },
147
+ flush(controller) {
148
+ controller.enqueue({
149
+ data: JSON.stringify({
150
+ type: "finish"
151
+ })
152
+ });
153
+ controller.enqueue({
154
+ data: "[DONE]"
155
+ });
156
+ },
157
+ transform(chunk, controller) {
158
+ let workflowName = "";
159
+ if (chunk.type === "workflow-start") {
160
+ workflowName = chunk.payload.workflowId;
161
+ controller.enqueue({
162
+ data: JSON.stringify({
163
+ type: "data-workflow",
164
+ id: chunk.runId,
165
+ data: {
166
+ name: workflowName,
167
+ status: "running",
168
+ steps: {},
169
+ output: null
170
+ }
171
+ })
172
+ });
173
+ } else if (chunk.type === "workflow-step-start") {
174
+ steps[chunk.payload.id] = {
175
+ // TODO swap with name
176
+ name: chunk.payload.id,
177
+ status: chunk.payload.status,
178
+ input: chunk.payload.payload ?? null,
179
+ output: null
180
+ };
181
+ controller.enqueue({
182
+ data: JSON.stringify({
183
+ type: "data-workflow",
184
+ id: chunk.runId,
185
+ data: {
186
+ name: workflowName,
187
+ status: "running",
188
+ steps,
189
+ output: null
190
+ }
191
+ })
192
+ });
193
+ } else if (chunk.type === "workflow-step-result") {
194
+ steps[chunk.payload.id] = {
195
+ ...steps[chunk.payload.id],
196
+ status: chunk.payload.status,
197
+ output: chunk.payload.output ?? null
198
+ };
199
+ controller.enqueue({
200
+ data: JSON.stringify({
201
+ type: "data-workflow",
202
+ id: chunk.runId,
203
+ data: {
204
+ name: workflowName,
205
+ status: "running",
206
+ steps,
207
+ output: null
208
+ }
209
+ })
210
+ });
211
+ } else if (chunk.type === "workflow-finish") {
212
+ controller.enqueue({
213
+ data: JSON.stringify({
214
+ type: "data-workflow",
215
+ id: chunk.runId,
216
+ data: {
217
+ name: workflowName,
218
+ steps,
219
+ output: chunk.payload.output ?? null,
220
+ status: chunk.payload.workflowStatus
221
+ }
222
+ })
223
+ });
224
+ }
225
+ }
226
+ });
227
+ }
228
+ function toAISdkFormat(stream) {
229
+ return stream.pipeThrough(WokflowStreamToAISDKTransformer());
230
+ }
231
+
232
+ export { WokflowStreamToAISDKTransformer, chatRoute, toAISdkFormat };
136
233
  //# sourceMappingURL=index.js.map
137
234
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAiBO,SAAS,SAAA,CAAmD;AAAA,EACjE,IAAA,GAAO,gBAAA;AAAA,EACP,KAAA;AAAA,EACA;AACF,CAAA,EAAkE;AAChE,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,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AM,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,CAA6B,QAAA,EAAU;AAAA,QACnE,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.js","sourcesContent":["import type { AgentExecutionOptions } from '@mastra/core/agent';\nimport { registerApiRoute } from '@mastra/core/server';\nimport type { OutputSchema } from '@mastra/core/stream';\n\nexport type chatRouteOptions<OUTPUT extends OutputSchema = undefined> = {\n defaultOptions?: AgentExecutionOptions<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<OUTPUT extends OutputSchema = undefined>({\n path = '/chat/:agentId',\n agent,\n defaultOptions,\n}: chatRouteOptions<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<OUTPUT, 'aisdk'>(messages, {\n ...defaultOptions,\n ...rest,\n format: 'aisdk',\n });\n\n return result.toUIMessageStreamResponse();\n },\n });\n}\n"]}
1
+ {"version":3,"sources":["../src/chat-route.ts","../src/to-ai-sdk-format.ts"],"names":[],"mappings":";;;AAiBO,SAAS,SAAA,CAAmD;AAAA,EACjE,IAAA,GAAO,gBAAA;AAAA,EACP,KAAA;AAAA,EACA;AACF,CAAA,EAAkE;AAChE,EAAA,IAAI,CAAC,KAAA,IAAS,CAAC,IAAA,CAAK,QAAA,CAAS,WAAW,CAAA,EAAG;AACzC,IAA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CAAA;AAAA,UACzB,MAAA,EAAQ,MAAM,OAAA,CAAQ,MAAA;AAAA,UACtB,MAAA,EAAQ,KAAA,CAAM,OAAA,CAAQ,MAAA,IAAU;AAAA,SAClC;AAEA,QAAA,UAAA,CAAW,OAAA,CAAQ;AAAA,UACjB,IAAA,EAAM,KAAK,SAAA,CAAU;AAAA,YACnB,IAAA,EAAM,eAAA;AAAA,YACN,IAAI,KAAA,CAAM,KAAA;AAAA,YACV,IAAA,EAAM;AAAA,cACJ,IAAA,EAAM,YAAA;AAAA,cACN,MAAA,EAAQ,SAAA;AAAA,cACR,KAAA;AAAA,cACA,MAAA,EAAQ;AAAA;AACV,WAC2B;AAAA,SAC9B,CAAA;AAAA,MACH,CAAA,MAAA,IAAW,KAAA,CAAM,IAAA,KAAS,iBAAA,EAAmB;AAC3C,QAAA,UAAA,CAAW,OAAA,CAAQ;AAAA,UACjB,IAAA,EAAM,KAAK,SAAA,CAAU;AAAA,YACnB,IAAA,EAAM,eAAA;AAAA,YACN,IAAI,KAAA,CAAM,KAAA;AAAA,YACV,IAAA,EAAM;AAAA,cACJ,IAAA,EAAM,YAAA;AAAA,cACN,KAAA;AAAA,cACA,MAAA,EAAQ,KAAA,CAAM,OAAA,CAAQ,MAAA,IAAU,IAAA;AAAA,cAChC,MAAA,EAAQ,MAAM,OAAA,CAAQ;AAAA;AACxB,WAC2B;AAAA,SAC9B,CAAA;AAAA,MACH;AAAA,IACF;AAAA,GACD,CAAA;AACH;AAEO,SAAS,cAId,MAAA,EAAuD;AACvD,EAAA,OAAO,MAAA,CAAO,WAAA,CAAY,+BAAA,EAAiC,CAAA;AAC7D","file":"index.js","sourcesContent":["import type { AgentExecutionOptions } from '@mastra/core/agent';\nimport { registerApiRoute } from '@mastra/core/server';\nimport type { OutputSchema } from '@mastra/core/stream';\n\nexport type chatRouteOptions<OUTPUT extends OutputSchema = undefined> = {\n defaultOptions?: AgentExecutionOptions<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<OUTPUT extends OutputSchema = undefined>({\n path = '/chat/:agentId',\n agent,\n defaultOptions,\n}: chatRouteOptions<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<OUTPUT, 'aisdk'>(messages, {\n ...defaultOptions,\n ...rest,\n format: 'aisdk',\n });\n\n return result.toUIMessageStreamResponse();\n },\n });\n}\n","import type {\n ChunkType,\n MastraWorkflowStream,\n Step,\n WorkflowRunStatus,\n WorkflowStepStatus,\n} from '@mastra/core/workflows';\nimport type { ZodType } from 'zod';\n\ntype StepResult = {\n name: string;\n status: WorkflowStepStatus;\n input: Record<string, unknown> | null;\n output: Record<string, unknown> | null;\n};\n\nexport type WorkflowAiSDKType = {\n type: 'data-workflow';\n id: string;\n data: {\n name: string;\n status: WorkflowRunStatus;\n steps: Record<string, StepResult>;\n output: {\n usage: {\n inputTokens: number;\n outputTokens: number;\n totalTokens: number;\n };\n } | null;\n };\n};\n\nexport function WokflowStreamToAISDKTransformer() {\n const steps: Record<string, StepResult> = {};\n return new TransformStream<\n ChunkType,\n {\n data: string;\n }\n >({\n start(controller) {\n controller.enqueue({\n data: JSON.stringify({\n type: 'start',\n messageId: '1',\n }),\n });\n },\n flush(controller) {\n controller.enqueue({\n data: JSON.stringify({\n type: 'finish',\n }),\n });\n controller.enqueue({\n data: '[DONE]',\n });\n },\n transform(chunk, controller) {\n let workflowName = '';\n if (chunk.type === 'workflow-start') {\n // TODO swap with name\n workflowName = chunk.payload.workflowId;\n controller.enqueue({\n data: JSON.stringify({\n type: 'data-workflow',\n id: chunk.runId,\n data: {\n name: workflowName,\n status: 'running',\n steps: {} as Record<string, StepResult>,\n output: null,\n },\n } satisfies WorkflowAiSDKType),\n });\n } else if (chunk.type === 'workflow-step-start') {\n steps[chunk.payload.id] = {\n // TODO swap with name\n name: chunk.payload.id,\n status: chunk.payload.status,\n input: chunk.payload.payload ?? null,\n output: null,\n } satisfies StepResult;\n\n controller.enqueue({\n data: JSON.stringify({\n type: 'data-workflow',\n id: chunk.runId,\n data: {\n name: workflowName,\n status: 'running',\n steps,\n output: null,\n },\n } satisfies WorkflowAiSDKType),\n });\n } else if (chunk.type === 'workflow-step-result') {\n steps[chunk.payload.id] = {\n ...steps[chunk.payload.id]!,\n status: chunk.payload.status,\n output: chunk.payload.output ?? null,\n } satisfies StepResult;\n\n controller.enqueue({\n data: JSON.stringify({\n type: 'data-workflow',\n id: chunk.runId,\n data: {\n name: workflowName,\n status: 'running',\n steps,\n output: null,\n },\n } satisfies WorkflowAiSDKType),\n });\n } else if (chunk.type === 'workflow-finish') {\n controller.enqueue({\n data: JSON.stringify({\n type: 'data-workflow',\n id: chunk.runId,\n data: {\n name: workflowName,\n steps,\n output: chunk.payload.output ?? null,\n status: chunk.payload.workflowStatus,\n },\n } satisfies WorkflowAiSDKType),\n });\n }\n },\n });\n}\n\nexport function toAISdkFormat<\n TInput extends ZodType<any>,\n TOutput extends ZodType<any>,\n TSteps extends Step<string, any, any>[],\n>(stream: MastraWorkflowStream<TInput, TOutput, TSteps>) {\n return stream.pipeThrough(WokflowStreamToAISDKTransformer());\n}\n"]}
@@ -0,0 +1,32 @@
1
+ import type { ChunkType, MastraWorkflowStream, Step, WorkflowRunStatus, WorkflowStepStatus } from '@mastra/core/workflows';
2
+ import type { ZodType } from 'zod';
3
+ type StepResult = {
4
+ name: string;
5
+ status: WorkflowStepStatus;
6
+ input: Record<string, unknown> | null;
7
+ output: Record<string, unknown> | null;
8
+ };
9
+ export type WorkflowAiSDKType = {
10
+ type: 'data-workflow';
11
+ id: string;
12
+ data: {
13
+ name: string;
14
+ status: WorkflowRunStatus;
15
+ steps: Record<string, StepResult>;
16
+ output: {
17
+ usage: {
18
+ inputTokens: number;
19
+ outputTokens: number;
20
+ totalTokens: number;
21
+ };
22
+ } | null;
23
+ };
24
+ };
25
+ export declare function WokflowStreamToAISDKTransformer(): import("stream/web").TransformStream<ChunkType, {
26
+ data: string;
27
+ }>;
28
+ export declare function toAISdkFormat<TInput extends ZodType<any>, TOutput extends ZodType<any>, TSteps extends Step<string, any, any>[]>(stream: MastraWorkflowStream<TInput, TOutput, TSteps>): import("stream/web").ReadableStream<{
29
+ data: string;
30
+ }>;
31
+ export {};
32
+ //# sourceMappingURL=to-ai-sdk-format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"to-ai-sdk-format.d.ts","sourceRoot":"","sources":["../src/to-ai-sdk-format.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,oBAAoB,EACpB,IAAI,EACJ,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAEnC,KAAK,UAAU,GAAG;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,kBAAkB,CAAC;IAC3B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IACtC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;CACxC,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,eAAe,CAAC;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE;QACJ,IAAI,EAAE,MAAM,CAAC;QACb,MAAM,EAAE,iBAAiB,CAAC;QAC1B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;QAClC,MAAM,EAAE;YACN,KAAK,EAAE;gBACL,WAAW,EAAE,MAAM,CAAC;gBACpB,YAAY,EAAE,MAAM,CAAC;gBACrB,WAAW,EAAE,MAAM,CAAC;aACrB,CAAC;SACH,GAAG,IAAI,CAAC;KACV,CAAC;CACH,CAAC;AAEF,wBAAgB,+BAA+B;UAKnC,MAAM;GA8FjB;AAED,wBAAgB,aAAa,CAC3B,MAAM,SAAS,OAAO,CAAC,GAAG,CAAC,EAC3B,OAAO,SAAS,OAAO,CAAC,GAAG,CAAC,EAC5B,MAAM,SAAS,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,EAAE,EACvC,MAAM,EAAE,oBAAoB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC;UApG3C,MAAM;GAsGjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/ai-sdk",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Adds custom API routes to be compatible with the AI SDK UI parts",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -22,7 +22,7 @@
22
22
  "CHANGELOG.md"
23
23
  ],
24
24
  "peerDependencies": {
25
- "@mastra/core": ">=0.18.0-0 <0.19.0-0",
25
+ "@mastra/core": ">=0.18.1-0 <0.20.0-0",
26
26
  "zod": "^3.25.0 || ^4.0.0"
27
27
  },
28
28
  "devDependencies": {
@@ -32,9 +32,9 @@
32
32
  "typescript": "^5.8.3",
33
33
  "vitest": "^3.2.4",
34
34
  "zod": "^3.25.76",
35
- "@internal/lint": "0.0.42",
36
- "@mastra/core": "0.18.0",
37
- "@internal/types-builder": "0.0.17"
35
+ "@internal/types-builder": "0.0.18",
36
+ "@mastra/core": "0.19.0",
37
+ "@internal/lint": "0.0.43"
38
38
  },
39
39
  "keywords": [
40
40
  "mastra",