Fix: In order to distinguish the keys of a pair of messages, add a prefix to the id when rendering the message. #4409 (#4451)

### What problem does this PR solve?

Fix: In order to distinguish the keys of a pair of messages, add a
prefix to the id when rendering the message. #4409

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
balibabu
2025-01-13 10:51:59 +08:00
committed by GitHub
parent 95261f17f6
commit 2c7ba90cb4
6 changed files with 23 additions and 26 deletions

View File

@ -1,4 +1,4 @@
import { EmptyConversationId, MessageType } from '@/constants/chat';
import { EmptyConversationId } from '@/constants/chat';
import { Message } from '@/interfaces/database/chat';
import { IMessage } from '@/pages/chat/interface';
import { omit } from 'lodash';
@ -10,21 +10,11 @@ export const isConversationIdExist = (conversationId: string) => {
export const buildMessageUuid = (message: Partial<Message | IMessage>) => {
if ('id' in message && message.id) {
return message.role === MessageType.User
? `${MessageType.User}_${message.id}`
: `${MessageType.Assistant}_${message.id}`;
return message.id;
}
return uuid();
};
export const getMessagePureId = (id?: string) => {
const strings = id?.split('_') ?? [];
if (strings.length > 0) {
return strings.at(-1);
}
return id;
};
export const buildMessageListWithUuid = (messages?: Message[]) => {
return (
messages?.map((x: Message | IMessage) => ({
@ -37,3 +27,10 @@ export const buildMessageListWithUuid = (messages?: Message[]) => {
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}`;
};