mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
* chore: enhance logging for latest message actions in message components
* fix: Extract previous convoId from latest text in message helpers and process hooks
- Updated `useMessageHelpers` and `useMessageProcess` to extract `convoId` from the previous text key for improved message handling.
- Refactored `getLengthAndLastTenChars` to `getLengthAndLastNChars` for better flexibility in character length retrieval.
- Introduced `getLatestContentForKey` function to streamline content extraction from messages.
* chore: Enhance logging for clearing latest messages in conversation hooks
* refactor: Update message key formatting for improved URL parameter handling
- Modified `getLatestContentForKey` to change the format from `${text}-${i}` to `${text}&i=${i}` for better URL parameter structure.
- Adjusted `getTextKey` to increase character length retrieval from 12 to 16 in `getLengthAndLastNChars` for enhanced text processing.
* refactor: Simplify convoId extraction and enhance message formatting
- Updated `useMessageHelpers` and `useMessageProcess` to extract `convoId` using a new format for improved clarity.
- Refactored `getLatestContentForKey` to streamline content formatting and ensure consistent use of `Constants.COMMON_DIVIDER` for better message structure.
- Removed redundant length and last character extraction logic from `getLengthAndLastNChars` for cleaner code.
* chore: linting
* chore: Simplify pre-commit hook by removing unnecessary lines
98 lines
3.7 KiB
TypeScript
98 lines
3.7 KiB
TypeScript
import { useSetRecoilState } from 'recoil';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
import { QueryKeys, Constants, dataService } from 'librechat-data-provider';
|
|
import type { TConversation, TEndpointsConfig, TModelsConfig } from 'librechat-data-provider';
|
|
import { buildDefaultConvo, getDefaultEndpoint, getEndpointField, logger } from '~/utils';
|
|
import store from '~/store';
|
|
|
|
const useNavigateToConvo = (index = 0) => {
|
|
const navigate = useNavigate();
|
|
const queryClient = useQueryClient();
|
|
const clearAllConversations = store.useClearConvoState();
|
|
const setSubmission = useSetRecoilState(store.submissionByIndex(index));
|
|
const clearAllLatestMessages = store.useClearLatestMessages(`useNavigateToConvo ${index}`);
|
|
const { hasSetConversation, setConversation } = store.useCreateConversationAtom(index);
|
|
|
|
const fetchFreshData = async (conversation?: Partial<TConversation>) => {
|
|
const conversationId = conversation?.conversationId;
|
|
if (!conversationId) {
|
|
return;
|
|
}
|
|
try {
|
|
const data = await queryClient.fetchQuery([QueryKeys.conversation, conversationId], () =>
|
|
dataService.getConversationById(conversationId),
|
|
);
|
|
logger.log('conversation', 'Fetched fresh conversation data', data);
|
|
setConversation(data);
|
|
navigate(`/c/${conversationId ?? Constants.NEW_CONVO}`, { state: { focusChat: true } });
|
|
} catch (error) {
|
|
console.error('Error fetching conversation data on navigation', error);
|
|
if (conversation) {
|
|
setConversation(conversation as TConversation);
|
|
navigate(`/c/${conversationId}`, { state: { focusChat: true } });
|
|
}
|
|
}
|
|
};
|
|
|
|
const navigateToConvo = (
|
|
conversation?: TConversation | null,
|
|
options?: {
|
|
resetLatestMessage?: boolean;
|
|
currentConvoId?: string;
|
|
},
|
|
) => {
|
|
if (!conversation) {
|
|
logger.warn('conversation', 'Conversation not provided to `navigateToConvo`');
|
|
return;
|
|
}
|
|
const { resetLatestMessage = true, currentConvoId } = options || {};
|
|
logger.log('conversation', 'Navigating to conversation', conversation);
|
|
hasSetConversation.current = true;
|
|
setSubmission(null);
|
|
if (resetLatestMessage) {
|
|
logger.log('latest_message', 'Clearing all latest messages');
|
|
clearAllLatestMessages();
|
|
}
|
|
|
|
let convo = { ...conversation };
|
|
const endpointsConfig = queryClient.getQueryData<TEndpointsConfig>([QueryKeys.endpoints]);
|
|
if (!convo.endpoint || !endpointsConfig?.[convo.endpoint]) {
|
|
/* undefined/removed endpoint edge case */
|
|
const modelsConfig = queryClient.getQueryData<TModelsConfig>([QueryKeys.models]);
|
|
const defaultEndpoint = getDefaultEndpoint({
|
|
convoSetup: conversation,
|
|
endpointsConfig,
|
|
});
|
|
|
|
const endpointType = getEndpointField(endpointsConfig, defaultEndpoint, 'type');
|
|
if (!conversation.endpointType && endpointType) {
|
|
conversation.endpointType = endpointType;
|
|
}
|
|
|
|
const models = modelsConfig?.[defaultEndpoint ?? ''] ?? [];
|
|
|
|
convo = buildDefaultConvo({
|
|
models,
|
|
conversation,
|
|
endpoint: defaultEndpoint,
|
|
lastConversationSetup: conversation,
|
|
});
|
|
}
|
|
clearAllConversations(true);
|
|
queryClient.setQueryData([QueryKeys.messages, currentConvoId], []);
|
|
if (convo.conversationId !== Constants.NEW_CONVO && convo.conversationId) {
|
|
queryClient.invalidateQueries([QueryKeys.conversation, convo.conversationId]);
|
|
fetchFreshData(convo);
|
|
} else {
|
|
setConversation(convo);
|
|
navigate(`/c/${convo.conversationId ?? Constants.NEW_CONVO}`, { state: { focusChat: true } });
|
|
}
|
|
};
|
|
|
|
return {
|
|
navigateToConvo,
|
|
};
|
|
};
|
|
|
|
export default useNavigateToConvo;
|