import React, { useState } from 'react'; import SubmitButton from './SubmitButton'; import Regenerate from './Regenerate'; import ModelMenu from '../Models/ModelMenu'; import Footer from './Footer'; import TextareaAutosize from 'react-textarea-autosize'; import handleSubmit from '~/utils/handleSubmit'; import { useSelector, useDispatch } from 'react-redux'; import { setConversation, setError } from '~/store/convoSlice'; import { setMessages } from '~/store/messageSlice'; import { setSubmitState } from '~/store/submitSlice'; import { setText } from '~/store/textSlice'; export default function TextChat({ messages }) { const [errorMessage, setErrorMessage] = useState(''); const dispatch = useDispatch(); const convo = useSelector((state) => state.convo); const { initial } = useSelector((state) => state.models); const { isSubmitting, disabled, model, chatGptLabel, promptPrefix } = useSelector( (state) => state.submit ); const { text } = useSelector((state) => state.text); const { error } = convo; const isCustomModel = model === 'chatgptCustom' || !initial[model]; const submitMessage = () => { if (error) { dispatch(setError(false)); } if (!!isSubmitting || text.trim() === '') { return; } dispatch(setSubmitState(true)); const message = text.trim(); const currentMsg = { sender: 'User', text: message, current: true }; const sender = model === 'chatgptCustom' ? chatGptLabel : model; const initialResponse = { sender, text: '' }; dispatch(setMessages([...messages, currentMsg, initialResponse])); dispatch(setText('')); const messageHandler = (data) => { dispatch(setMessages([...messages, currentMsg, { sender, text: data }])); }; const convoHandler = (data) => { dispatch( setMessages([...messages, currentMsg, { sender, text: data.text || data.response }]) ); if ( model !== 'bingai' && convo.conversationId === null && convo.parentMessageId === null ) { const { title, conversationId, id } = data; dispatch( setConversation({ title, conversationId, parentMessageId: id, jailbreakConversationId: null, conversationSignature: null, clientId: null, invocationId: null, chatGptLabel: model === isCustomModel ? chatGptLabel : null, promptPrefix: model === isCustomModel ? promptPrefix : null }) ); } else if ( model === 'bingai' && convo.conversationId === null && convo.invocationId === null ) { const { title, jailbreakConversationId, conversationSignature, clientId, conversationId, invocationId } = data; dispatch( setConversation({ title, jailbreakConversationId, conversationSignature, clientId, conversationId, invocationId, parentMessageId: null }) ); } dispatch(setSubmitState(false)); }; // const convoHandler = (data) => { // const { conversationId, id, invocationId } = data; // const conversationData = { // title: data.title, // conversationId, // parentMessageId: // model !== 'bingai' && !convo.conversationId && !convo.parentMessageId ? id : null, // conversationSignature: // model === 'bingai' && !convo.conversationId ? data.conversationSignature : null, // clientId: model === 'bingai' && !convo.conversationId ? data.clientId : null, // // invocationId: model === 'bingai' && !convo.conversationId ? data.invocationId : null // invocationId: invocationId ? invocationId : null // }; // dispatch(setMessages([...messages, currentMsg, { sender: model, text: data.text || data.response }])); // dispatch(setConversation(conversationData)); // dispatch(setSubmitState(false)); // }; const errorHandler = (event) => { console.log('Error:', event); const errorResponse = { ...initialResponse, text: `An error occurred. Please try again in a few moments.\n\nError message: ${event.data}`, error: true }; setErrorMessage(event.data); dispatch(setSubmitState(false)); dispatch(setMessages([...messages.slice(0, -2), currentMsg, errorResponse])); dispatch(setText(message)); dispatch(setError(true)); return; }; const submission = { model, text: message, convo, messageHandler, convoHandler, errorHandler, chatGptLabel, promptPrefix }; console.log('User Input:', message); handleSubmit(submission); }; const handleKeyDown = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); } }; const handleKeyUp = (e) => { if (e.key === 'Enter' && e.shiftKey) { return console.log('Enter + Shift'); } if (isSubmitting) { return; } if (e.key === 'Enter' && !e.shiftKey) { submitMessage(); } }; const changeHandler = (e) => { const { value } = e.target; if (isSubmitting && (value === '' || value === '\n')) { return; } dispatch(setText(value)); }; const tryAgain = (e) => { e.preventDefault(); dispatch(setError(false)); }; return (
{error ? ( ) : (
)}
); }