//#region src/ai-chat-v5-migration.ts
/**
* Tool call state mapping for v4 to v5 migration
*/
const STATE_MAP = {
"partial-call": "input-streaming",
call: "input-available",
result: "output-available",
error: "output-error"
};
/**
* Checks if a message is already in the UIMessage format (has parts array)
*/
function isUIMessage(message) {
return typeof message === "object" && message !== null && "parts" in message && Array.isArray(message.parts);
}
/**
* Type guard to check if a message is in legacy format (content as string)
*/
function isLegacyMessage(message) {
return typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.role === "string" && typeof message.content === "string";
}
/**
* Type guard to check if a message has corrupted array content format
* Detects: {role: "user", content: [{type: "text", text: "..."}]}
*/
function isCorruptArrayMessage(message) {
return typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.role === "string" && Array.isArray(message.content) && !("parts" in message);
}
/**
* 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
*/
function autoTransformMessage(message, index = 0) {
if (isUIMessage(message)) return message;
const parts = [];
if (message.reasoning) parts.push({
type: "reasoning",
text: message.reasoning
});
if (message.toolInvocations && Array.isArray(message.toolInvocations)) message.toolInvocations.forEach((inv) => {
if (typeof inv === "object" && inv !== null && "toolName" in inv) {
const invObj = inv;
parts.push({
type: `tool-${invObj.toolName}`,
toolCallId: invObj.toolCallId,
state: STATE_MAP[invObj.state] || "input-available",
input: invObj.args,
output: invObj.result !== void 0 ? invObj.result : null
});
}
});
if (message.parts && Array.isArray(message.parts)) message.parts.forEach((part) => {
if (typeof part === "object" && part !== null && "type" in part) {
const partObj = part;
if (partObj.type === "file") parts.push({
type: "file",
url: partObj.url || (partObj.data ? `data:${partObj.mimeType || partObj.mediaType};base64,${partObj.data}` : void 0),
mediaType: partObj.mediaType || partObj.mimeType,
filename: partObj.filename
});
}
});
if (Array.isArray(message.content)) message.content.forEach((item) => {
if (typeof item === "object" && item !== null && "text" in item) {
const itemObj = item;
parts.push({
type: itemObj.type || "text",
text: itemObj.text || ""
});
}
});
if (!parts.length && message.content !== void 0) parts.push({
type: "text",
text: typeof message.content === "string" ? message.content : JSON.stringify(message.content)
});
if (!parts.length) parts.push({
type: "text",
text: typeof message === "string" ? message : JSON.stringify(message)
});
return {
id: message.id || `msg-${index}`,
role: message.role === "data" ? "system" : message.role || "user",
parts
};
}
/**
* Legacy single message migration for backward compatibility
*/
function migrateToUIMessage(message) {
return autoTransformMessage(message);
}
/**
* 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
*/
function autoTransformMessages(messages) {
return messages.map((msg, i) => autoTransformMessage(msg, i));
}
/**
* 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
*/
function migrateMessagesToUIFormat(messages) {
return autoTransformMessages(messages);
}
/**
* 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
*/
function needsMigration(messages) {
return messages.some((message) => {
if (isUIMessage(message)) return false;
if (isCorruptArrayMessage(message)) return true;
if (isLegacyMessage(message)) return true;
return true;
});
}
/**
* Analyzes the corruption types in a message array for debugging
* @param messages - Array of messages to analyze
* @returns Statistics about corruption types found
*/
function analyzeCorruption(messages) {
const stats = {
total: messages.length,
clean: 0,
legacyString: 0,
corruptArray: 0,
unknown: 0,
examples: {}
};
for (const message of messages) if (isUIMessage(message)) stats.clean++;
else if (isCorruptArrayMessage(message)) {
stats.corruptArray++;
if (!stats.examples.corruptArray) stats.examples.corruptArray = message;
} else if (isLegacyMessage(message)) {
stats.legacyString++;
if (!stats.examples.legacyString) stats.examples.legacyString = message;
} else {
stats.unknown++;
if (!stats.examples.unknown) stats.examples.unknown = message;
}
return stats;
}
//#endregion
export { migrateMessagesToUIFormat as a, isUIMessage as i, autoTransformMessage as n, migrateToUIMessage as o, autoTransformMessages as r, needsMigration as s, analyzeCorruption as t };
//# sourceMappingURL=ai-chat-v5-migration-DguhuLKF.js.map