mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 09:50:15 +01:00
* refactor: remove legacy max_tokens setting for vision models in OpenAIClient (intended for gpt-4-preview) * refactor: streamline capability checks in loadAgentTools function, still allow actions if tools are disabled * fix: enhance error handling for token limits in AnthropicClient and update error message in translations * feat: append timestamp to cloned agent names for better identification * chore: update @librechat/agents dependency to version 2.3.94 * refactor: remove clearDraft helper from useSubmitMessage and centralize draft clearing logic to SSE handling, helps prevent user message loss if logout occurs * refactor: increase debounce time for clearDraft function to improve auto-save performance
99 lines
3.2 KiB
TypeScript
99 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() {
|
|
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;
|
|
const clientTimestamp = new Date().toISOString();
|
|
|
|
ask({
|
|
text: data.text,
|
|
overrideConvoId: appendIndex(rootIndex, overrideConvoId),
|
|
overrideUserMessageId: appendIndex(rootIndex, overrideUserMessageId),
|
|
clientTimestamp,
|
|
});
|
|
|
|
if (hasAdded) {
|
|
askAdditional(
|
|
{
|
|
text: data.text,
|
|
overrideConvoId: appendIndex(addedIndex, overrideConvoId),
|
|
overrideUserMessageId: appendIndex(addedIndex, overrideUserMessageId),
|
|
clientTimestamp,
|
|
},
|
|
{ overrideMessages: rootMessages },
|
|
);
|
|
}
|
|
methods.reset();
|
|
},
|
|
[
|
|
ask,
|
|
methods,
|
|
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 };
|
|
}
|