import { UIMessage } from "ai";
//#region src/ai-chat-v5-migration.d.ts
/**
* AI SDK v5 Migration following https://jhak.im/blog/ai-sdk-migration-handling-previously-saved-messages
* Using exact types from the official AI SDK documentation
*/
/**
* AI SDK v5 Message Part types reference (from official AI SDK documentation)
*
* The migration logic below transforms legacy messages to match these official AI SDK v5 formats:
* - TextUIPart: { type: "text", text: string, state?: "streaming" | "done" }
* - ReasoningUIPart: { type: "reasoning", text: string, state?: "streaming" | "done", providerMetadata?: Record<string, unknown> }
* - FileUIPart: { type: "file", mediaType: string, filename?: string, url: string }
* - ToolUIPart: { type: `tool-${string}`, toolCallId: string, state: "input-streaming" | "input-available" | "output-available" | "output-error", input?: Record<string, unknown>, output?: unknown, errorText?: string, providerExecuted?: boolean }
*/
/**
* Tool invocation from v4 format
*/
type ToolInvocation = {
toolCallId: string;
toolName: string;
args: Record<string, unknown>;
result?: unknown;
state: "partial-call" | "call" | "result" | "error";
};
/**
* Legacy part from v4 format
*/
type LegacyPart = {
type: string;
text?: string;
url?: string;
data?: string;
mimeType?: string;
mediaType?: string;
filename?: string;
};
/**
* Legacy message format from AI SDK v4
*/
type LegacyMessage = {
id?: string;
role: string;
content: string;
reasoning?: string;
toolInvocations?: ToolInvocation[];
parts?: LegacyPart[];
[key: string]: unknown;
};
/**
* Corrupt content item
*/
type CorruptContentItem = {
type: string;
text: string;
};
/**
* Corrupted message format - has content as array instead of parts
*/
type CorruptArrayMessage = {
id?: string;
role: string;
content: CorruptContentItem[];
reasoning?: string;
toolInvocations?: ToolInvocation[];
[key: string]: unknown;
};
/**
* Union type for messages that could be in any format
*/
type MigratableMessage = LegacyMessage | CorruptArrayMessage | UIMessage;
/**
* Checks if a message is already in the UIMessage format (has parts array)
*/
declare function isUIMessage(message: unknown): message is UIMessage;
/**
* Input message that could be in any format - using unknown for flexibility
*/
type InputMessage = {
id?: string;
role?: string;
content?: unknown;
reasoning?: string;
toolInvocations?: unknown[];
parts?: unknown[];
[key: string]: unknown;
};
/**
* Automatic message transformer following the blog post pattern
* Handles comprehensive migration from AI SDK v4 to v5 format
* @param message - Message in any legacy format
* @param index - Index for ID generation fallback
* @returns UIMessage in v5 format
*/
declare function autoTransformMessage(
message: InputMessage,
index?: number
): UIMessage;
/**
* Legacy single message migration for backward compatibility
*/
declare function migrateToUIMessage(message: MigratableMessage): UIMessage;
/**
* Automatic message transformer for arrays following the blog post pattern
* @param messages - Array of messages in any format
* @returns Array of UIMessages in v5 format
*/
declare function autoTransformMessages(messages: unknown[]): UIMessage[];
/**
* Migrates an array of messages to UIMessage format (legacy compatibility)
* @param messages - Array of messages in old or new format
* @returns Array of UIMessages in the new format
*/
declare function migrateMessagesToUIFormat(
messages: MigratableMessage[]
): UIMessage[];
/**
* Checks if any messages in an array need migration
* @param messages - Array of messages to check
* @returns true if any messages are not in proper UIMessage format
*/
declare function needsMigration(messages: unknown[]): boolean;
/**
* Analyzes the corruption types in a message array for debugging
* @param messages - Array of messages to analyze
* @returns Statistics about corruption types found
*/
declare function analyzeCorruption(messages: unknown[]): {
total: number;
clean: number;
legacyString: number;
corruptArray: number;
unknown: number;
examples: {
legacyString?: unknown;
corruptArray?: unknown;
unknown?: unknown;
};
};
//#endregion
export {
CorruptArrayMessage,
LegacyMessage,
MigratableMessage,
analyzeCorruption,
autoTransformMessage,
autoTransformMessages,
isUIMessage,
migrateMessagesToUIFormat,
migrateToUIMessage,
needsMigration
};
//# sourceMappingURL=ai-chat-v5-migration.d.ts.map