| import { |
| ChatVariableEnabledField, |
| 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, ''); |
| }; |
|
|
| |
| export const buildMessageUuidWithRole = ( |
| message: Partial<Message | IMessage>, |
| ) => { |
| return `${message.role}_${message.id}`; |
| }; |
|
|
| |
| |
|
|
| export const preprocessLaTeX = (content: string) => { |
| const blockProcessedContent = content.replace( |
| /\\\[([\s\S]*?)\\\]/g, |
| (_, equation) => `$$${equation}$$`, |
| ); |
| const inlineProcessedContent = blockProcessedContent.replace( |
| /\\\(([\s\S]*?)\\\)/g, |
| (_, equation) => `$${equation}$`, |
| ); |
| return inlineProcessedContent; |
| }; |
|
|
| export function replaceThinkToSection(text: string = '') { |
| const pattern = /<think>([\s\S]*?)<\/think>/g; |
|
|
| const result = text.replace(pattern, '<section class="think">$1</section>'); |
|
|
| return result; |
| } |
|
|
| export function setInitialChatVariableEnabledFieldValue( |
| field: ChatVariableEnabledField, |
| ) { |
| return field !== ChatVariableEnabledField.MaxTokensEnabled; |
| } |
|
|