@mastra/ai-sdk 0.0.5 → 0.1.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 CHANGED
@@ -1,5 +1,27 @@
1
1
  # @mastra/ai-sdk
2
2
 
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Breaking change to move the agent.streamVNext/generateVNext implementation to the default stream/generate. The old stream/generate have now been moved to streamLegacy and generateLegacy ([#8097](https://github.com/mastra-ai/mastra/pull/8097))
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [[`00cb6bd`](https://github.com/mastra-ai/mastra/commit/00cb6bdf78737c0fac14a5a0c7b532a11e38558a), [`869ba22`](https://github.com/mastra-ai/mastra/commit/869ba222e1d6b58fc1b65e7c9fd55ca4e01b8c2f), [`1b73665`](https://github.com/mastra-ai/mastra/commit/1b73665e8e23f5c09d49fcf3e7d709c75259259e), [`f7d7475`](https://github.com/mastra-ai/mastra/commit/f7d747507341aef60ed39e4b49318db1f86034a6), [`084b77b`](https://github.com/mastra-ai/mastra/commit/084b77b2955960e0190af8db3f77138aa83ed65c), [`a93ff84`](https://github.com/mastra-ai/mastra/commit/a93ff84b5e1af07ee236ac8873dac9b49aa5d501), [`bc5aacb`](https://github.com/mastra-ai/mastra/commit/bc5aacb646d468d325327e36117129f28cd13bf6), [`6b5af12`](https://github.com/mastra-ai/mastra/commit/6b5af12ce9e09066e0c32e821c203a6954498bea), [`bf60e4a`](https://github.com/mastra-ai/mastra/commit/bf60e4a89c515afd9570b7b79f33b95e7d07c397), [`d41aee5`](https://github.com/mastra-ai/mastra/commit/d41aee526d124e35f42720a08e64043229193679), [`e8fe13c`](https://github.com/mastra-ai/mastra/commit/e8fe13c4b4c255a42520127797ec394310f7c919), [`3ca833d`](https://github.com/mastra-ai/mastra/commit/3ca833dc994c38e3c9b4f9b4478a61cd8e07b32a), [`1edb8d1`](https://github.com/mastra-ai/mastra/commit/1edb8d1cfb963e72a12412990fb9170936c9904c), [`fbf6e32`](https://github.com/mastra-ai/mastra/commit/fbf6e324946332d0f5ed8930bf9d4d4479cefd7a), [`4753027`](https://github.com/mastra-ai/mastra/commit/4753027ee889288775c6958bdfeda03ff909af67)]:
12
+ - @mastra/core@0.20.0
13
+
14
+ ## 0.1.0-alpha.0
15
+
16
+ ### Minor Changes
17
+
18
+ - Breaking change to move the agent.streamVNext/generateVNext implementation to the default stream/generate. The old stream/generate have now been moved to streamLegacy and generateLegacy ([#8097](https://github.com/mastra-ai/mastra/pull/8097))
19
+
20
+ ### Patch Changes
21
+
22
+ - Updated dependencies [[`00cb6bd`](https://github.com/mastra-ai/mastra/commit/00cb6bdf78737c0fac14a5a0c7b532a11e38558a), [`869ba22`](https://github.com/mastra-ai/mastra/commit/869ba222e1d6b58fc1b65e7c9fd55ca4e01b8c2f), [`1b73665`](https://github.com/mastra-ai/mastra/commit/1b73665e8e23f5c09d49fcf3e7d709c75259259e), [`f7d7475`](https://github.com/mastra-ai/mastra/commit/f7d747507341aef60ed39e4b49318db1f86034a6), [`084b77b`](https://github.com/mastra-ai/mastra/commit/084b77b2955960e0190af8db3f77138aa83ed65c), [`a93ff84`](https://github.com/mastra-ai/mastra/commit/a93ff84b5e1af07ee236ac8873dac9b49aa5d501), [`bc5aacb`](https://github.com/mastra-ai/mastra/commit/bc5aacb646d468d325327e36117129f28cd13bf6), [`6b5af12`](https://github.com/mastra-ai/mastra/commit/6b5af12ce9e09066e0c32e821c203a6954498bea), [`bf60e4a`](https://github.com/mastra-ai/mastra/commit/bf60e4a89c515afd9570b7b79f33b95e7d07c397), [`d41aee5`](https://github.com/mastra-ai/mastra/commit/d41aee526d124e35f42720a08e64043229193679), [`e8fe13c`](https://github.com/mastra-ai/mastra/commit/e8fe13c4b4c255a42520127797ec394310f7c919), [`3ca833d`](https://github.com/mastra-ai/mastra/commit/3ca833dc994c38e3c9b4f9b4478a61cd8e07b32a), [`1edb8d1`](https://github.com/mastra-ai/mastra/commit/1edb8d1cfb963e72a12412990fb9170936c9904c), [`fbf6e32`](https://github.com/mastra-ai/mastra/commit/fbf6e324946332d0f5ed8930bf9d4d4479cefd7a), [`4753027`](https://github.com/mastra-ai/mastra/commit/4753027ee889288775c6958bdfeda03ff909af67)]:
23
+ - @mastra/core@0.20.0-alpha.0
24
+
3
25
  ## 0.0.5
4
26
 
5
27
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -124,7 +124,7 @@ function chatRoute({
124
124
  if (!agentObj) {
125
125
  throw new Error(`Agent ${agentToUse} not found`);
126
126
  }
127
- const result = await agentObj.streamVNext(messages, {
127
+ const result = await agentObj.stream(messages, {
128
128
  ...defaultOptions,
129
129
  ...rest,
130
130
  format: "aisdk"
@@ -1 +1 @@
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,EAA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,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.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"]}
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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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.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.stream<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.js CHANGED
@@ -122,7 +122,7 @@ function chatRoute({
122
122
  if (!agentObj) {
123
123
  throw new Error(`Agent ${agentToUse} not found`);
124
124
  }
125
- const result = await agentObj.streamVNext(messages, {
125
+ const result = await agentObj.stream(messages, {
126
126
  ...defaultOptions,
127
127
  ...rest,
128
128
  format: "aisdk"
package/dist/index.js.map CHANGED
@@ -1 +1 @@
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,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,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,kB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;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(Wokfl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AAuD;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.stream<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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/ai-sdk",
3
- "version": "0.0.5",
3
+ "version": "0.1.0",
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.1-0 <0.20.0-0",
25
+ "@mastra/core": ">=0.20.0-0 <0.21.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/types-builder": "0.0.18",
36
- "@mastra/core": "0.19.0",
37
- "@internal/lint": "0.0.43"
35
+ "@internal/lint": "0.0.45",
36
+ "@internal/types-builder": "0.0.20",
37
+ "@mastra/core": "0.20.0"
38
38
  },
39
39
  "keywords": [
40
40
  "mastra",