import { ChatVariableEnabledField, EmptyConversationId, } from '@/constants/chat'; import { IMessage, Message } from '@/interfaces/database/chat'; import { omit } from 'lodash'; import { v4 as uuid } from 'uuid'; import { citationMarkerReg, normalizeCitationDigits, parseCitationIndex, } from './citation-utils'; export const isConversationIdExist = (conversationId: string) => { return conversationId !== EmptyConversationId && conversationId !== ''; }; export const buildMessageUuid = (message: Partial) => { 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 generateConversationId = () => { return uuid().replace(/-/g, ''); }; // When rendering each message, add a prefix to the id to ensure uniqueness. export const buildMessageUuidWithRole = ( message: Partial, ) => { return `${message.role}_${message.id}`; }; // Preprocess LaTeX equations to be rendered by KaTeX // ref: https://github.com/remarkjs/react-markdown/issues/785 // // Delimiter matching: we only treat \] and \) as block/inline endings when they // are not part of a LaTeX command (e.g. \right], \big), \left)). Use a negative // lookbehind (? { const blockProcessedContent = content.replace( BLOCK_MATH_RE, (_, equation) => `$$${equation}$$`, ); const inlineProcessedContent = blockProcessedContent.replace( INLINE_MATH_RE, (_, equation) => `$${equation}$`, ); return inlineProcessedContent; }; export function replaceThinkToSection(text: string = '') { const pattern = /([\s\S]*?)<\/think>/g; const result = text.replace(pattern, '
$1
'); return result; } export function setInitialChatVariableEnabledFieldValue( field: ChatVariableEnabledField, ) { return field !== ChatVariableEnabledField.MaxTokensEnabled; } const ShowImageFields = ['image', 'table']; export function showImage(filed?: string) { return ShowImageFields.some((x) => x === filed); } export function setChatVariableEnabledFieldValuePage() { const variableCheckBoxFieldMap = Object.values( ChatVariableEnabledField, ).reduce>((pre, cur) => { pre[cur] = cur !== ChatVariableEnabledField.MaxTokensEnabled; return pre; }, {}); return variableCheckBoxFieldMap; } const oldReg = /(#{2}[0-9\u0660-\u0669\u06F0-\u06F9]+\${2})/g; export const currentReg = citationMarkerReg; export { normalizeCitationDigits, parseCitationIndex }; // To be compatible with the old index matching mode export const replaceTextByOldReg = (text: string) => { return text?.replace(oldReg, (substring: string) => { return `[ID:${substring.slice(2, -2)}]`; }); };