UNPKG

@mastra/ai-sdk

Version:

Adds custom API routes to be compatible with the AI SDK UI parts

1 lines 7.42 kB
{"version":3,"sources":["../src/index.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;AAAA,wBACJ,IAAA,EAAM,QAAA;AAAA,wBACN,IAAA,EAAM,CAAC,MAAA,EAAQ,WAAA,EAA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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,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"]}