mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 18:00:15 +01:00
* fix: Update health endpoint URL * refactor: use Constants for saved tag default value, do not place Saved as first always * refactor: check trimmed currentText before appending parsedText in useSubmitMessage * refactor: move `scrollToEnd()` to `createdHandler` and increase delay before execution slightly * chore: Add back TypeScript linting rules for unnecessary conditions and strict boolean expressions * chore: Update librechat-data-provider package.json version to 0.7.4.0
96 lines
3.2 KiB
TypeScript
96 lines
3.2 KiB
TypeScript
import { v4 } from 'uuid';
|
|
import { useCallback } from 'react';
|
|
import { Constants } from 'librechat-data-provider';
|
|
import { useRecoilValue, useSetRecoilState } from 'recoil';
|
|
import { useChatContext, useChatFormContext, useAddedChatContext } from '~/Providers';
|
|
import { useAuthContext } from '~/hooks/AuthContext';
|
|
import { replaceSpecialVars } from '~/utils';
|
|
import store from '~/store';
|
|
|
|
const appendIndex = (index: number, value?: string) => {
|
|
if (!value) {
|
|
return value;
|
|
}
|
|
return `${value}${Constants.COMMON_DIVIDER}${index}`;
|
|
};
|
|
|
|
export default function useSubmitMessage(helpers?: { clearDraft?: () => void }) {
|
|
const { user } = useAuthContext();
|
|
const methods = useChatFormContext();
|
|
const { ask, index, getMessages, setMessages, latestMessage } = useChatContext();
|
|
const { addedIndex, ask: askAdditional, conversation: addedConvo } = useAddedChatContext();
|
|
|
|
const autoSendPrompts = useRecoilValue(store.autoSendPrompts);
|
|
const activeConvos = useRecoilValue(store.allConversationsSelector);
|
|
const setActivePrompt = useSetRecoilState(store.activePromptByIndex(index));
|
|
|
|
const submitMessage = useCallback(
|
|
(data?: { text: string }) => {
|
|
if (!data) {
|
|
return console.warn('No data provided to submitMessage');
|
|
}
|
|
const rootMessages = getMessages();
|
|
const isLatestInRootMessages = rootMessages?.some(
|
|
(message) => message?.messageId === latestMessage?.messageId,
|
|
);
|
|
if (!isLatestInRootMessages && latestMessage) {
|
|
setMessages([...(rootMessages || []), latestMessage]);
|
|
}
|
|
|
|
const hasAdded = addedIndex && activeConvos[addedIndex] && addedConvo;
|
|
const isNewMultiConvo =
|
|
hasAdded &&
|
|
activeConvos.every((convoId) => convoId === Constants.NEW_CONVO) &&
|
|
!rootMessages?.length;
|
|
const overrideConvoId = isNewMultiConvo ? v4() : undefined;
|
|
const overrideUserMessageId = hasAdded ? v4() : undefined;
|
|
const rootIndex = addedIndex - 1;
|
|
ask({
|
|
text: data.text,
|
|
overrideConvoId: appendIndex(rootIndex, overrideConvoId),
|
|
overrideUserMessageId: appendIndex(rootIndex, overrideUserMessageId),
|
|
});
|
|
if (hasAdded) {
|
|
askAdditional(
|
|
{
|
|
text: data.text,
|
|
overrideConvoId: appendIndex(addedIndex, overrideConvoId),
|
|
overrideUserMessageId: appendIndex(addedIndex, overrideUserMessageId),
|
|
},
|
|
{ overrideMessages: rootMessages },
|
|
);
|
|
}
|
|
methods.reset();
|
|
helpers?.clearDraft && helpers.clearDraft();
|
|
},
|
|
[
|
|
ask,
|
|
methods,
|
|
helpers,
|
|
addedIndex,
|
|
addedConvo,
|
|
setMessages,
|
|
getMessages,
|
|
activeConvos,
|
|
askAdditional,
|
|
latestMessage,
|
|
],
|
|
);
|
|
|
|
const submitPrompt = useCallback(
|
|
(text: string) => {
|
|
const parsedText = replaceSpecialVars({ text, user });
|
|
if (autoSendPrompts) {
|
|
submitMessage({ text: parsedText });
|
|
return;
|
|
}
|
|
|
|
const currentText = methods.getValues('text');
|
|
const newText = currentText?.trim()?.length > 1 ? `\n${parsedText}` : parsedText;
|
|
setActivePrompt(newText);
|
|
},
|
|
[autoSendPrompts, submitMessage, setActivePrompt, methods, user],
|
|
);
|
|
|
|
return { submitMessage, submitPrompt };
|
|
}
|