@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
51 lines (36 loc) • 1.68 kB
text/typescript
import { useCallback, useMemo } from 'react';
import { useChatStore } from '@/store/chat';
import { threadSelectors } from '@/store/chat/selectors';
import { SendMessageParams } from '@/types/message';
export type UseSendMessageParams = Pick<
SendMessageParams,
'onlyAddUserMessage' | 'isWelcomeQuestion'
>;
export const useSendThreadMessage = () => {
const [sendMessage, updateInputMessage] = useChatStore((s) => [
s.sendThreadMessage,
s.updateThreadInputMessage,
]);
const isSendButtonDisabledByMessage = useChatStore(threadSelectors.isSendButtonDisabledByMessage);
const canSend = !isSendButtonDisabledByMessage;
const send = useCallback((params: UseSendMessageParams = {}) => {
const store = useChatStore.getState();
if (threadSelectors.isThreadAIGenerating(store)) return;
const isSendButtonDisabledByMessage = threadSelectors.isSendButtonDisabledByMessage(
useChatStore.getState(),
);
const canSend = !isSendButtonDisabledByMessage;
if (!canSend) return;
// if there is no message and no image, then we should not send the message
if (!store.threadInputMessage) return;
sendMessage({ message: store.threadInputMessage, ...params });
updateInputMessage('');
// const hasSystemRole = agentSelectors.hasSystemRole(useAgentStore.getState());
// const agentSetting = useAgentStore.getState().agentSettingInstance;
// // if there is a system role, then we need to use agent setting instance to autocomplete agent meta
// if (hasSystemRole && !!agentSetting) {
// agentSetting.autocompleteAllMeta();
// }
}, []);
return useMemo(() => ({ canSend, send }), [canSend]);
};