### What problem does this PR solve? fix: Fixed an issue where the first message would be displayed when sending the second message #2625 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) - [ ] New Feature (non-breaking change which adds functionality) - [ ] Documentation Update - [ ] Refactoring - [ ] Performance Improvement - [ ] Other (please describe):
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import { EmptyConversationId, MessageType } from '@/constants/chat';
|
|
import { Message } from '@/interfaces/database/chat';
|
|
import { IMessage } from '@/pages/chat/interface';
|
|
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.role === MessageType.User
|
|
? `${MessageType.User}_${message.id}`
|
|
: `${MessageType.Assistant}_${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) => ({
|
|
...x,
|
|
id: buildMessageUuid(x),
|
|
})) ?? []
|
|
);
|
|
};
|
|
|
|
export const getConversationId = () => {
|
|
return uuid().replace(/-/g, '');
|
|
};
|