2024-05-16 20:15:02 +08:00
|
|
|
import { MessageType } from '@/constants/chat';
|
2024-02-28 16:28:33 +08:00
|
|
|
import { IConversation, IReference } from '@/interfaces/database/chat';
|
2024-12-10 16:46:47 +08:00
|
|
|
import { isEmpty } from 'lodash';
|
2024-06-06 11:01:14 +08:00
|
|
|
import { EmptyConversationId } from './constants';
|
2024-06-07 19:27:27 +08:00
|
|
|
import { IMessage } from './interface';
|
2024-02-22 17:14:25 +08:00
|
|
|
|
2024-02-28 16:28:33 +08:00
|
|
|
export const isConversationIdExist = (conversationId: string) => {
|
2024-02-26 18:38:54 +08:00
|
|
|
return conversationId !== EmptyConversationId && conversationId !== '';
|
|
|
|
|
};
|
2024-02-28 16:28:33 +08:00
|
|
|
|
|
|
|
|
export const getDocumentIdsFromConversionReference = (data: IConversation) => {
|
|
|
|
|
const documentIds = data.reference.reduce(
|
|
|
|
|
(pre: Array<string>, cur: IReference) => {
|
|
|
|
|
cur.doc_aggs
|
2024-05-16 20:15:02 +08:00
|
|
|
?.map((x) => x.doc_id)
|
2024-02-28 16:28:33 +08:00
|
|
|
.forEach((x) => {
|
|
|
|
|
if (pre.every((y) => y !== x)) {
|
|
|
|
|
pre.push(x);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return pre;
|
|
|
|
|
},
|
|
|
|
|
[],
|
|
|
|
|
);
|
|
|
|
|
return documentIds.join(',');
|
|
|
|
|
};
|
2024-05-16 20:15:02 +08:00
|
|
|
|
|
|
|
|
export const buildMessageItemReference = (
|
2024-06-07 19:27:27 +08:00
|
|
|
conversation: { message: IMessage[]; reference: IReference[] },
|
2024-05-16 20:15:02 +08:00
|
|
|
message: IMessage,
|
|
|
|
|
) => {
|
|
|
|
|
const assistantMessages = conversation.message
|
|
|
|
|
?.filter((x) => x.role === MessageType.Assistant)
|
|
|
|
|
.slice(1);
|
|
|
|
|
const referenceIndex = assistantMessages.findIndex(
|
|
|
|
|
(x) => x.id === message.id,
|
|
|
|
|
);
|
2024-12-10 16:46:47 +08:00
|
|
|
const reference = !isEmpty(message?.reference)
|
2024-05-16 20:15:02 +08:00
|
|
|
? message?.reference
|
2024-12-10 16:46:47 +08:00
|
|
|
: (conversation?.reference ?? [])[referenceIndex];
|
2024-05-16 20:15:02 +08:00
|
|
|
|
2024-12-10 16:46:47 +08:00
|
|
|
return reference ?? { doc_aggs: [], chunks: [], total: 0 };
|
2024-05-16 20:15:02 +08:00
|
|
|
};
|
2024-12-10 18:58:06 +08:00
|
|
|
|
|
|
|
|
const oldReg = /(#{2}\d+\${2})/g;
|
|
|
|
|
|
2024-12-11 16:29:17 +08:00
|
|
|
// To be compatible with the old index matching mode
|
2024-12-10 18:58:06 +08:00
|
|
|
export const replaceTextByOldReg = (text: string) => {
|
|
|
|
|
return text.replace(oldReg, function (substring) {
|
2024-12-11 16:29:17 +08:00
|
|
|
return `~~${substring.slice(2, -2)}==`;
|
2024-12-10 18:58:06 +08:00
|
|
|
});
|
|
|
|
|
};
|