diff --git a/client/src/components/Main/TextChat.jsx b/client/src/components/Main/TextChat.jsx index 32b12cdf00..8d7db4e71a 100644 --- a/client/src/components/Main/TextChat.jsx +++ b/client/src/components/Main/TextChat.jsx @@ -10,15 +10,20 @@ import resetConvo from '~/utils/resetConvo'; import RegenerateIcon from '../svg/RegenerateIcon'; import StopGeneratingIcon from '../svg/StopGeneratingIcon'; import { useSelector, useDispatch } from 'react-redux'; -import { setConversation, setNewConvo, setError, refreshConversation } from '~/store/convoSlice'; +import { + setConversation, + setNewConvo, + setError, + refreshConversation +} from '~/store/convoSlice'; import { setMessages } from '~/store/messageSlice'; import { setSubmitState, setSubmission } from '~/store/submitSlice'; import { setText } from '~/store/textSlice'; -import { useMessageHandler } from '../../utils/handleSubmit' +import { useMessageHandler } from '../../utils/handleSubmit'; export default function TextChat({ messages }) { const [errorMessage, setErrorMessage] = useState(''); - const inputRef = useRef(null) + const inputRef = useRef(null); const isComposing = useRef(false); const dispatch = useDispatch(); const { user } = useSelector((state) => state.user); @@ -35,15 +40,39 @@ export default function TextChat({ messages }) { // auto focus to input, when enter a conversation. useEffect(() => { inputRef.current?.focus(); - }, [convo?.conversationId,]) + }, [convo?.conversationId]); const messageHandler = (data, currentState, currentMsg) => { + const { messages, _currentMsg, message, sender, isRegenerate } = currentState; if (isRegenerate) - dispatch(setMessages([...messages, { sender, text: data, parentMessageId: message?.overrideParentMessageId, messageId: message?.overrideParentMessageId + '_', submitting: true }])); + dispatch( + setMessages([ + ...messages, + { + sender, + text: data, + parentMessageId: message?.overrideParentMessageId, + messageId: message?.overrideParentMessageId + '_', + submitting: true + } + ]) + ); else - dispatch(setMessages([...messages, currentMsg, { sender, text: data, parentMessageId: currentMsg?.messageId, messageId: currentMsg?.messageId + '_', submitting: true }])); + dispatch( + setMessages([ + ...messages, + currentMsg, + { + sender, + text: data, + parentMessageId: currentMsg?.messageId, + messageId: currentMsg?.messageId + '_', + submitting: true + } + ]) + ); }; const createdHandler = (data, currentState, currentMsg) => { @@ -62,14 +91,8 @@ export default function TextChat({ messages }) { const { messages, _currentMsg, message, isCustomModel, sender, isRegenerate } = currentState; const { model, chatGptLabel, promptPrefix } = message; - if (isRegenerate) - dispatch( - setMessages([...messages, responseMessage,]) - ); - else - dispatch( - setMessages([...messages, requestMessage, responseMessage,]) - ); + if (isRegenerate) dispatch(setMessages([...messages, responseMessage])); + else dispatch(setMessages([...messages, requestMessage, responseMessage])); const isBing = model === 'bingai' || model === 'sydney'; @@ -101,12 +124,11 @@ export default function TextChat({ messages }) { latestMessage: null }) ); - } else if ( - model === 'bingai' - ) { + } else if (model === 'bingai') { console.log('Bing data:', data); const { title } = data; - const { conversationSignature, clientId, conversationId, invocationId } = responseMessage; + const { conversationSignature, clientId, conversationId, invocationId } = + responseMessage; dispatch( setConversation({ title, @@ -151,7 +173,7 @@ export default function TextChat({ messages }) { const errorResponse = { ...data, error: true, - parentMessageId: currentMsg?.messageId, + parentMessageId: currentMsg?.messageId }; setErrorMessage(data?.text); dispatch(setSubmitState(false)); @@ -161,7 +183,7 @@ export default function TextChat({ messages }) { return; }; const submitMessage = () => { - ask({ text }) + ask({ text }); }; useEffect(() => { @@ -171,8 +193,8 @@ export default function TextChat({ messages }) { } const currentState = submission; - let currentMsg = {...currentState.message}; - + let currentMsg = { ...currentState.message }; + const { server, payload } = createPayload(submission); const onMessage = (e) => { if (stopStream) { @@ -181,18 +203,18 @@ export default function TextChat({ messages }) { const data = JSON.parse(e.data); - // if (data.message) { - // messageHandler(text, currentState); - // } - if (data.final) { convoHandler(data, currentState, currentMsg); console.log('final', data); - } if (data.created) { + } + if (data.created) { currentMsg = data.message; createdHandler(data, currentState, currentMsg); } else { let text = data.text || data.response; + if (data.initial) { + console.log(data); + } if (data.message) { messageHandler(text, currentState, currentMsg); } @@ -230,13 +252,12 @@ export default function TextChat({ messages }) { }, [submission]); const handleRegenerate = () => { - if (latestMessage&&!latestMessage?.isCreatedByUser) - regenerate(latestMessage) - } + if (latestMessage && !latestMessage?.isCreatedByUser) regenerate(latestMessage); + }; const handleStopGenerating = () => { - stopGenerating() - } + stopGenerating(); + }; const handleKeyDown = (e) => { if (e.key === 'Enter' && !e.shiftKey) { @@ -244,8 +265,7 @@ export default function TextChat({ messages }) { } if (e.key === 'Enter' && !e.shiftKey) { - if (!isComposing.current) - submitMessage(); + if (!isComposing.current) submitMessage(); } }; @@ -258,14 +278,14 @@ export default function TextChat({ messages }) { return; } }; - + const handleCompositionStart = (e) => { - isComposing.current = true - } + isComposing.current = true; + }; const handleCompositionEnd = (e) => { isComposing.current = false; - } + }; const changeHandler = (e) => { const { value } = e.target; @@ -280,59 +300,73 @@ export default function TextChat({ messages }) { e.preventDefault(); dispatch(setError(false)); }; - isNotAppendable + + const placeholder = () => { + if (disabled && isSubmitting) { + return 'Choose another model or customize GPT again'; + } else if (!isSubmitting && latestMessage?.submitting) { + return 'Message in progress...'; + // } else if (latestMessage?.error) { + // return 'Error...'; + } else { + return ''; + } + }; + return ( -
-
+
+
- - {isSubmitting? - - :(latestMessage&&!latestMessage?.isCreatedByUser)? - - :null - } - -
- - - -
+ + {isSubmitting ? ( + + ) : latestMessage && !latestMessage?.isCreatedByUser ? ( + + ) : null} + +
+ + + +