chat-ui / src /lib /utils /tree /convertLegacyConversation.ts
nsarrazin's picture
nsarrazin HF staff
Use `uuid` for message ID generation instead of crypto method (#882)
d8e839e unverified
raw history blame
No virus
979 Bytes
import type { Conversation } from "$lib/types/Conversation";
import type { Message } from "$lib/types/Message";
import { v4 } from "uuid";
export function convertLegacyConversation(
conv: Pick<Conversation, "messages" | "rootMessageId" | "preprompt">
): Pick<Conversation, "messages" | "rootMessageId" | "preprompt"> {
if (conv.rootMessageId) return conv; // not a legacy conversation
if (conv.messages.length === 0) return conv; // empty conversation
const messages = [
{
from: "system",
content: conv.preprompt ?? "",
createdAt: new Date(),
updatedAt: new Date(),
id: v4(),
} satisfies Message,
...conv.messages,
];
const rootMessageId = messages[0].id;
const newMessages = messages.map((message, index) => {
return {
...message,
ancestors: messages.slice(0, index).map((m) => m.id),
children: index < messages.length - 1 ? [messages[index + 1].id] : [],
};
});
return {
...conv,
rootMessageId,
messages: newMessages,
};
}