import type { UIMessageStreamOptions as UIMessageStreamOptionsV5 } from './_types/@internal_ai-sdk-v5/dist/index.d.ts'; import type { UIMessageStreamOptions as UIMessageStreamOptionsV6 } from './_types/@internal_ai-v6/dist/index.d.ts'; import type { AgentExecutionOptions } from '@mastra/core/agent'; import type { Mastra } from '@mastra/core/mastra'; import { registerApiRoute } from '@mastra/core/server'; import type { SupportedUIMessage, V5UIMessage, V5UIMessageStream, V6UIMessage, V6UIMessageStream } from './public-types.js'; /** * Scans a v6 UIMessage array for the most recent 'approval-responded' tool * part in the last trailing assistant message only. When found, splits the * composite approvalId ("${runId}::${toolCallId}") to recover the runId * needed for resumeStream. * * Only the last trailing assistant message is inspected so that approval * responses from earlier turns are never re-processed. Within that message, * parts are scanned in reverse so the decision the user just acted on wins * over any earlier 'approval-responded' parts that have not yet transitioned * to 'output-available'. * * Returns null when no approval response is present (normal chat turn). */ export declare function extractV6NativeApproval(messages: V6UIMessage[]): { resumeData: Record; runId: string; } | null; export type ChatStreamHandlerParams = AgentExecutionOptions & { messages: UI_MESSAGE[]; resumeData?: Record; /** The trigger for the request - sent by AI SDK's useChat hook */ trigger?: 'submit-message' | 'regenerate-message'; }; /** * Extracted from the second parameter of `Mastra.getAgentById` so the type * stays in sync with core automatically. */ export type AgentVersionOptions = NonNullable[1]>; export type ChatStreamHandlerOptions = { mastra: Mastra; agentId: string; agentVersion?: AgentVersionOptions; params: ChatStreamHandlerParams; defaultOptions?: AgentExecutionOptions; version?: 'v5' | 'v6'; sendStart?: boolean; sendFinish?: boolean; sendReasoning?: boolean; sendSources?: boolean; onError?: (error: unknown) => string; messageMetadata?: UI_MESSAGE extends V6UIMessage ? UIMessageStreamOptionsV6['messageMetadata'] : UI_MESSAGE extends V5UIMessage ? UIMessageStreamOptionsV5['messageMetadata'] : never; }; type ChatStreamHandlerOptionsV5 = Omit, 'version' | 'messageMetadata'> & { version?: 'v5'; messageMetadata?: UIMessageStreamOptionsV5['messageMetadata']; }; type ChatStreamHandlerOptionsV6 = Omit, 'version' | 'messageMetadata'> & { version: 'v6'; messageMetadata?: UIMessageStreamOptionsV6['messageMetadata']; }; /** * Framework-agnostic handler for streaming agent chat in AI SDK-compatible format. * Use this function directly when you need to handle chat streaming outside of Hono or Mastra's own apiRoutes feature. * * @example * ```ts * // Next.js App Router * import { handleChatStream } from '@mastra/ai-sdk'; * import { createUIMessageStreamResponse } from 'ai'; * import { mastra } from '@/src/mastra'; * * export async function POST(req: Request) { * const params = await req.json(); * const stream = await handleChatStream({ * mastra, * agentId: 'weatherAgent', * params, * }); * return createUIMessageStreamResponse({ stream }); * } * ``` */ export declare function handleChatStream(options: ChatStreamHandlerOptionsV5): Promise>; export declare function handleChatStream(options: ChatStreamHandlerOptionsV6): Promise>; export type chatRouteOptions = { defaultOptions?: AgentExecutionOptions; version?: 'v5' | 'v6'; agentVersion?: AgentVersionOptions; } & ({ path: `${string}:agentId${string}`; agent?: never; } | { path: string; agent: string; }) & { sendStart?: boolean; sendFinish?: boolean; sendReasoning?: boolean; sendSources?: boolean; onError?: (error: unknown) => string; }; /** * Creates a chat route handler for streaming agent conversations using the AI SDK format. * * This function registers an HTTP POST endpoint that accepts messages, executes an agent, and streams the response back to the client in AI SDK-compatible format. * * @param {chatRouteOptions} options - Configuration options for the chat route * @param {string} [options.path='/chat/:agentId'] - The route path. Include `:agentId` for dynamic routing * @param {string} [options.agent] - Fixed agent ID when not using dynamic routing * @param {AgentExecutionOptions} [options.defaultOptions] - Default options passed to agent execution * @param {boolean} [options.sendStart=true] - Whether to send start events in the stream * @param {boolean} [options.sendFinish=true] - Whether to send finish events in the stream * @param {boolean} [options.sendReasoning=false] - Whether to include reasoning steps in the stream * @param {boolean} [options.sendSources=false] - Whether to include source citations in the stream * @param {(error: unknown) => string} [options.onError] - Custom error serializer streamed to the client. When omitted, errors are passed through a default serializer that strips sensitive fields (e.g. `APICallError.requestBodyValues`, which holds the system prompt) before they reach the client. * * @returns {ReturnType} A registered API route handler * * @throws {Error} When path doesn't include `:agentId` and no fixed agent is specified * @throws {Error} When agent ID is missing at runtime * @throws {Error} When specified agent is not found in Mastra instance * * @example * // Dynamic agent routing * chatRoute({ * path: '/chat/:agentId', * }); * * @example * // Fixed agent with custom path * chatRoute({ * path: '/api/support-chat', * agent: 'support-agent', * defaultOptions: { * maxSteps: 5, * }, * }); * * @remarks * - The route handler expects a JSON body with a `messages` array * - Messages should follow the format: `{ role: 'user' | 'assistant' | 'system', content: string }` * - The response is a Server-Sent Events (SSE) stream compatible with AI SDK v5 * - If both `agent` and `:agentId` are present, a warning is logged and the fixed `agent` takes precedence * - Request context from the incoming request overrides `defaultOptions.requestContext` if both are present */ export declare function chatRoute({ path, agent, defaultOptions, version, agentVersion, sendStart, sendFinish, sendReasoning, sendSources, onError, }: chatRouteOptions): ReturnType; export {}; //# sourceMappingURL=chat-route.d.ts.map