2024-08-28 16:39:21 +08:00
|
|
|
import { EmptyConversationId, MessageType } from '@/constants/chat';
|
|
|
|
|
import { Message } from '@/interfaces/database/chat';
|
|
|
|
|
import { IMessage } from '@/pages/chat/interface';
|
|
|
|
|
import { v4 as uuid } from 'uuid';
|
2024-08-27 14:45:17 +08:00
|
|
|
|
|
|
|
|
export const isConversationIdExist = (conversationId: string) => {
|
|
|
|
|
return conversationId !== EmptyConversationId && conversationId !== '';
|
|
|
|
|
};
|
2024-08-28 16:39:21 +08:00
|
|
|
|
2024-08-29 11:24:27 +08:00
|
|
|
export const buildMessageUuid = (message: Partial<Message | IMessage>) => {
|
2024-08-28 16:39:21 +08:00
|
|
|
if ('id' in message && message.id) {
|
|
|
|
|
return message.role === MessageType.User
|
|
|
|
|
? `${MessageType.User}_${message.id}`
|
|
|
|
|
: `${MessageType.Assistant}_${message.id}`;
|
|
|
|
|
}
|
|
|
|
|
return uuid();
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-29 18:37:18 +08:00
|
|
|
export const getMessagePureId = (id?: string) => {
|
|
|
|
|
const strings = id?.split('_') ?? [];
|
2024-08-28 16:39:21 +08:00
|
|
|
if (strings.length > 0) {
|
|
|
|
|
return strings.at(-1);
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
};
|
2024-08-30 17:53:30 +08:00
|
|
|
|
|
|
|
|
export const buildMessageListWithUuid = (messages?: Message[]) => {
|
|
|
|
|
return (
|
|
|
|
|
messages?.map((x: Message | IMessage) => ({
|
|
|
|
|
...x,
|
|
|
|
|
id: buildMessageUuid(x),
|
|
|
|
|
})) ?? []
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-09-27 18:20:19 +08:00
|
|
|
|
|
|
|
|
export const getConversationId = () => {
|
|
|
|
|
return uuid().replace(/-/g, '');
|
|
|
|
|
};
|