2025-01-13 10:51:59 +08:00
|
|
|
import { EmptyConversationId } from '@/constants/chat';
|
2024-08-28 16:39:21 +08:00
|
|
|
import { Message } from '@/interfaces/database/chat';
|
|
|
|
|
import { IMessage } from '@/pages/chat/interface';
|
2024-12-10 16:46:47 +08:00
|
|
|
import { omit } from 'lodash';
|
2024-08-28 16:39:21 +08:00
|
|
|
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) {
|
2025-01-13 10:51:59 +08:00
|
|
|
return message.id;
|
2024-08-28 16:39:21 +08:00
|
|
|
}
|
|
|
|
|
return uuid();
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-30 17:53:30 +08:00
|
|
|
export const buildMessageListWithUuid = (messages?: Message[]) => {
|
|
|
|
|
return (
|
|
|
|
|
messages?.map((x: Message | IMessage) => ({
|
2024-12-10 16:46:47 +08:00
|
|
|
...omit(x, 'reference'),
|
2024-08-30 17:53:30 +08:00
|
|
|
id: buildMessageUuid(x),
|
|
|
|
|
})) ?? []
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-09-27 18:20:19 +08:00
|
|
|
|
|
|
|
|
export const getConversationId = () => {
|
|
|
|
|
return uuid().replace(/-/g, '');
|
|
|
|
|
};
|
2025-01-13 10:51:59 +08:00
|
|
|
|
|
|
|
|
// 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}`;
|
|
|
|
|
};
|