2024-07-20 01:51:59 -04:00
|
|
|
import { ContentTypes, Constants } from 'librechat-data-provider';
|
2024-05-30 18:39:21 -04:00
|
|
|
import type { TMessage } from 'librechat-data-provider';
|
|
|
|
|
|
2024-07-20 01:51:59 -04:00
|
|
|
export const getLengthAndLastTenChars = (str?: string): string => {
|
2024-08-22 17:09:05 -04:00
|
|
|
if (typeof str !== 'string' || str.length === 0) {
|
2024-07-20 01:51:59 -04:00
|
|
|
return '0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const length = str.length;
|
|
|
|
|
const lastTenChars = str.slice(-10);
|
|
|
|
|
return `${length}${lastTenChars}`;
|
2024-05-30 18:39:21 -04:00
|
|
|
};
|
|
|
|
|
|
2024-07-20 01:51:59 -04:00
|
|
|
export const getLatestText = (message?: TMessage | null, includeIndex?: boolean) => {
|
2024-05-30 18:39:21 -04:00
|
|
|
if (!message) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
if (message.text) {
|
|
|
|
|
return message.text;
|
|
|
|
|
}
|
2024-08-22 17:09:05 -04:00
|
|
|
if (message.content && message.content.length > 0) {
|
2024-05-30 18:39:21 -04:00
|
|
|
for (let i = message.content.length - 1; i >= 0; i--) {
|
|
|
|
|
const part = message.content[i];
|
2024-08-22 17:09:05 -04:00
|
|
|
if (
|
|
|
|
|
part.type === ContentTypes.TEXT &&
|
|
|
|
|
((part[ContentTypes.TEXT].value as string | undefined)?.length ?? 0) > 0
|
|
|
|
|
) {
|
2024-07-20 01:51:59 -04:00
|
|
|
const text = part[ContentTypes.TEXT].value;
|
2024-08-22 17:09:05 -04:00
|
|
|
if (includeIndex === true) {
|
2024-07-20 01:51:59 -04:00
|
|
|
return `${text}-${i}`;
|
|
|
|
|
} else {
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2024-05-30 18:39:21 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
};
|
2024-07-20 01:51:59 -04:00
|
|
|
|
|
|
|
|
export const getTextKey = (message?: TMessage | null, convoId?: string | null) => {
|
|
|
|
|
if (!message) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
const text = getLatestText(message, true);
|
2024-08-22 17:09:05 -04:00
|
|
|
return `${(message.messageId as string | null) ?? ''}${
|
2024-07-20 01:51:59 -04:00
|
|
|
Constants.COMMON_DIVIDER
|
2024-08-22 17:09:05 -04:00
|
|
|
}${getLengthAndLastTenChars(text)}${Constants.COMMON_DIVIDER}${
|
|
|
|
|
message.conversationId ?? convoId
|
|
|
|
|
}`;
|
2024-07-20 01:51:59 -04:00
|
|
|
};
|
2024-08-08 14:52:12 -04:00
|
|
|
|
2024-08-18 19:02:46 -04:00
|
|
|
export const scrollToEnd = (callback?: () => void) => {
|
2024-08-09 15:17:13 -04:00
|
|
|
const messagesEndElement = document.getElementById('messages-end');
|
|
|
|
|
if (messagesEndElement) {
|
|
|
|
|
messagesEndElement.scrollIntoView({ behavior: 'instant' });
|
2024-08-18 19:02:46 -04:00
|
|
|
if (callback) {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2024-08-09 15:17:13 -04:00
|
|
|
}
|
2024-08-08 14:52:12 -04:00
|
|
|
};
|