balibabu
Fix: In order to distinguish the keys of a pair of messages, add a prefix to the id when rendering the message. #4409 (#4451)
2052ec7
import { EmptyConversationId } from '@/constants/chat'; | |
import { Message } from '@/interfaces/database/chat'; | |
import { IMessage } from '@/pages/chat/interface'; | |
import { omit } from 'lodash'; | |
import { v4 as uuid } from 'uuid'; | |
export const isConversationIdExist = (conversationId: string) => { | |
return conversationId !== EmptyConversationId && conversationId !== ''; | |
}; | |
export const buildMessageUuid = (message: Partial<Message | IMessage>) => { | |
if ('id' in message && message.id) { | |
return message.id; | |
} | |
return uuid(); | |
}; | |
export const buildMessageListWithUuid = (messages?: Message[]) => { | |
return ( | |
messages?.map((x: Message | IMessage) => ({ | |
...omit(x, 'reference'), | |
id: buildMessageUuid(x), | |
})) ?? [] | |
); | |
}; | |
export const getConversationId = () => { | |
return uuid().replace(/-/g, ''); | |
}; | |
// When rendering each message, add a prefix to the id to ensure uniqueness. | |
export const buildMessageUuidWithRole = ( | |
message: Partial<Message | IMessage>, | |
) => { | |
return `${message.role}_${message.id}`; | |
}; | |