/** * UIMessage - AI SDK v6 Compatible UI Message Types * * Provides types and utilities for working with UI messages in chat applications. * Compatible with AI SDK v6's UIMessage format for seamless integration. */ /** * UI Message - The main message type for chat UIs. * Compatible with AI SDK v6's UIMessage format. */ export interface UIMessage { /** Unique identifier for the message */ id: string; /** Role of the message sender */ role: 'system' | 'user' | 'assistant'; /** Optional metadata */ metadata?: METADATA; /** Message parts for rendering */ parts: Array>; } /** Data types that can be used in UI message data parts */ export type UIDataTypes = Record; /** UI Tool definition */ export type UITool = { input: unknown; output: unknown | undefined; }; /** UI Tools map */ export type UITools = Record; /** All possible UI message part types */ export type UIMessagePart = TextUIPart | ReasoningUIPart | ToolUIPart | SourceUrlUIPart | SourceDocumentUIPart | FileUIPart | DataUIPart | StepStartUIPart; /** Text part of a message */ export interface TextUIPart { type: 'text'; text: string; state?: 'streaming' | 'done'; providerMetadata?: Record; } /** Reasoning part of a message */ export interface ReasoningUIPart { type: 'reasoning'; text: string; state?: 'streaming' | 'done'; providerMetadata?: Record; } /** Tool invocation part */ export interface ToolUIPart { type: 'tool'; toolInvocationId: string; toolName: keyof TOOLS & string; state: 'input-streaming' | 'input-available' | 'output-streaming' | 'output-available' | 'error'; input?: TOOLS[keyof TOOLS]['input']; output?: TOOLS[keyof TOOLS]['output']; error?: string; /** Whether this tool needs approval before execution */ needsApproval?: boolean; /** Approval status */ approvalStatus?: 'pending' | 'approved' | 'denied'; } /** Source URL part */ export interface SourceUrlUIPart { type: 'source-url'; sourceId: string; url: string; title?: string; providerMetadata?: Record; } /** Source document part */ export interface SourceDocumentUIPart { type: 'source-document'; sourceId: string; mediaType: string; title?: string; providerMetadata?: Record; } /** File part */ export interface FileUIPart { type: 'file'; mediaType: string; url?: string; data?: string; filename?: string; } /** Data part for custom data */ export interface DataUIPart { type: 'data'; dataType: keyof DATA_TYPES & string; data: DATA_TYPES[keyof DATA_TYPES]; } /** Step start marker */ export interface StepStartUIPart { type: 'step-start'; stepNumber: number; } export interface ModelMessage { role: 'system' | 'user' | 'assistant' | 'tool'; content: string | ModelMessageContent[]; name?: string; toolCallId?: string; toolCalls?: ToolCall[]; } export type ModelMessageContent = { type: 'text'; text: string; } | { type: 'image'; image: string | Uint8Array; mimeType?: string; } | { type: 'file'; data: string | Uint8Array; mimeType: string; } | { type: 'tool-call'; toolCallId: string; toolName: string; args: unknown; } | { type: 'tool-result'; toolCallId: string; toolName: string; result: unknown; isError?: boolean; }; export interface ToolCall { id: string; type: 'function'; function: { name: string; arguments: string; }; } /** * Convert UI messages to model messages for AI SDK functions. * * @example * ```typescript * const modelMessages = await convertToModelMessages(uiMessages); * const result = await generateText({ model, messages: modelMessages }); * ``` */ export declare function convertToModelMessages(messages: Array>, options?: { tools?: Record; ignoreIncompleteToolCalls?: boolean; }): Promise; /** * Convert model messages to UI messages. */ export declare function convertToUIMessages(messages: ModelMessage[], generateId?: () => string): UIMessage[]; /** * Validate UI messages array. */ export declare function validateUIMessages(messages: unknown[]): messages is UIMessage[]; /** * Safe validation that returns result object. */ export declare function safeValidateUIMessages(messages: unknown[]): { success: boolean; value?: UIMessage[]; error?: string; }; /** * Create a text UI message. */ export declare function createTextMessage(role: 'user' | 'assistant', text: string, id?: string): UIMessage; /** * Create a system message. */ export declare function createSystemMessage(text: string, id?: string): UIMessage; /** * Check if a message has pending tool approvals. */ export declare function hasPendingApprovals(message: UIMessage): boolean; /** * Get tool parts that need approval. */ export declare function getToolsNeedingApproval(message: UIMessage): ToolUIPart[]; /** * Create an approval response for a tool. */ export declare function createApprovalResponse(toolInvocationId: string, approved: boolean): { toolInvocationId: string; approved: boolean; }; export interface UIMessageStreamOptions { /** Called when a message is created */ onMessage?: (message: UIMessage) => void; /** Called when a part is added */ onPart?: (part: UIMessagePart) => void; /** Called on error */ onError?: (error: Error) => void; /** Called when stream completes */ onFinish?: (messages: UIMessage[]) => void; } /** * Create a UI message stream response for Next.js route handlers. * * @example * ```typescript * // In app/api/chat/route.ts * export async function POST(req: Request) { * const { messages } = await req.json(); * const result = await streamText({ model, messages }); * return toUIMessageStreamResponse(result); * } * ``` */ export declare function toUIMessageStreamResponse(stream: AsyncIterable, options?: { headers?: Record; status?: number; }): Response; /** * Pipe UI message stream to a Node.js response. */ export declare function pipeUIMessageStreamToResponse(stream: AsyncIterable, response: { write: (chunk: string) => void; end: () => void; }): void;