2023-02-05 15:29:35 -05:00
|
|
|
import React, { useState } from 'react';
|
2023-02-06 21:17:46 -05:00
|
|
|
import SubmitButton from './SubmitButton';
|
2023-02-08 15:26:42 -05:00
|
|
|
import Regenerate from './Regenerate';
|
2023-02-13 15:58:35 -05:00
|
|
|
import ModelMenu from './ModelMenu';
|
2023-02-11 12:54:02 -05:00
|
|
|
import Footer from './Footer';
|
2023-02-06 13:27:28 -05:00
|
|
|
import TextareaAutosize from 'react-textarea-autosize';
|
2023-02-07 10:26:19 -05:00
|
|
|
import handleSubmit from '~/utils/handleSubmit';
|
2023-02-07 00:05:00 -05:00
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
2023-02-08 15:26:42 -05:00
|
|
|
import { setConversation, setError } from '~/store/convoSlice';
|
2023-02-07 10:26:19 -05:00
|
|
|
import { setMessages } from '~/store/messageSlice';
|
2023-02-07 16:22:35 -05:00
|
|
|
import { setSubmitState } from '~/store/submitSlice';
|
2023-02-08 09:59:01 -05:00
|
|
|
import { setText } from '~/store/textSlice';
|
2023-02-05 15:29:35 -05:00
|
|
|
|
2023-02-13 13:32:54 -05:00
|
|
|
export default function TextChat({ messages }) {
|
2023-02-11 11:37:20 -05:00
|
|
|
const [errorMessage, setErrorMessage] = useState('');
|
2023-02-07 00:05:00 -05:00
|
|
|
const dispatch = useDispatch();
|
2023-02-07 09:41:54 -05:00
|
|
|
const convo = useSelector((state) => state.convo);
|
2023-03-03 15:52:06 -05:00
|
|
|
const { isSubmitting, disabled, model, chatGptLabel, promptPrefix } = useSelector((state) => state.submit);
|
2023-02-08 09:59:01 -05:00
|
|
|
const { text } = useSelector((state) => state.text);
|
2023-02-08 15:26:42 -05:00
|
|
|
const { error } = convo;
|
2023-02-07 00:05:00 -05:00
|
|
|
|
2023-02-06 21:17:46 -05:00
|
|
|
const submitMessage = () => {
|
2023-02-21 19:40:28 -05:00
|
|
|
if (error) {
|
2023-02-08 15:26:42 -05:00
|
|
|
dispatch(setError(false));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 16:22:35 -05:00
|
|
|
if (!!isSubmitting || text.trim() === '') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dispatch(setSubmitState(true));
|
2023-02-13 21:15:28 -05:00
|
|
|
const message = text.trim();
|
|
|
|
|
const currentMsg = { sender: 'User', text: message, current: true };
|
2023-03-03 15:52:06 -05:00
|
|
|
const sender = model === 'chatgptCustom' ? chatGptLabel : model
|
|
|
|
|
const initialResponse = { sender, text: '' };
|
2023-02-07 16:22:35 -05:00
|
|
|
dispatch(setMessages([...messages, currentMsg, initialResponse]));
|
2023-02-08 09:59:01 -05:00
|
|
|
dispatch(setText(''));
|
2023-02-06 21:17:46 -05:00
|
|
|
const messageHandler = (data) => {
|
2023-03-03 15:52:06 -05:00
|
|
|
dispatch(setMessages([...messages, currentMsg, { sender, text: data }]));
|
2023-02-05 19:41:24 -05:00
|
|
|
};
|
2023-02-25 09:06:26 -05:00
|
|
|
const convoHandler = (data) => {
|
|
|
|
|
dispatch(
|
2023-03-03 15:52:06 -05:00
|
|
|
setMessages([
|
|
|
|
|
...messages,
|
|
|
|
|
currentMsg,
|
|
|
|
|
{ sender, text: data.text || data.response }
|
|
|
|
|
])
|
2023-02-25 09:06:26 -05:00
|
|
|
);
|
2023-02-05 15:29:35 -05:00
|
|
|
|
2023-02-25 09:06:26 -05:00
|
|
|
if (
|
|
|
|
|
model !== 'bingai' &&
|
|
|
|
|
convo.conversationId === null &&
|
|
|
|
|
convo.parentMessageId === null
|
|
|
|
|
) {
|
|
|
|
|
const { title, conversationId, id } = data;
|
|
|
|
|
dispatch(
|
|
|
|
|
setConversation({
|
|
|
|
|
title,
|
|
|
|
|
conversationId,
|
|
|
|
|
parentMessageId: id,
|
|
|
|
|
conversationSignature: null,
|
|
|
|
|
clientId: null,
|
2023-03-04 17:39:06 -05:00
|
|
|
invocationId: null,
|
|
|
|
|
chatGptLabel: model === 'chatgptCustom' ? chatGptLabel : null,
|
|
|
|
|
promptPrefix: model === 'chatgptCustom' ? promptPrefix : null,
|
2023-02-25 09:06:26 -05:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
} else if (
|
|
|
|
|
model === 'bingai' &&
|
|
|
|
|
convo.conversationId === null &&
|
|
|
|
|
convo.invocationId === null
|
|
|
|
|
) {
|
|
|
|
|
const { title, conversationSignature, clientId, conversationId, invocationId } = data;
|
|
|
|
|
dispatch(
|
|
|
|
|
setConversation({
|
|
|
|
|
title,
|
|
|
|
|
conversationSignature,
|
|
|
|
|
clientId,
|
|
|
|
|
conversationId,
|
|
|
|
|
invocationId,
|
|
|
|
|
parentMessageId: null
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-02-24 23:16:19 -05:00
|
|
|
|
2023-02-07 16:22:35 -05:00
|
|
|
dispatch(setSubmitState(false));
|
2023-02-06 21:17:46 -05:00
|
|
|
};
|
2023-02-08 00:02:29 -05:00
|
|
|
|
2023-02-25 09:06:26 -05:00
|
|
|
// 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));
|
|
|
|
|
// };
|
|
|
|
|
|
2023-02-08 09:15:47 -05:00
|
|
|
const errorHandler = (event) => {
|
|
|
|
|
console.log('Error:', event);
|
2023-02-08 00:02:29 -05:00
|
|
|
const errorResponse = {
|
|
|
|
|
...initialResponse,
|
2023-02-08 09:15:47 -05:00
|
|
|
text: `An error occurred. Please try again in a few moments.\n\nError message: ${event.data}`,
|
2023-02-08 00:02:29 -05:00
|
|
|
error: true
|
|
|
|
|
};
|
2023-02-11 11:37:20 -05:00
|
|
|
setErrorMessage(event.data);
|
2023-02-08 00:02:29 -05:00
|
|
|
dispatch(setSubmitState(false));
|
2023-02-08 15:26:42 -05:00
|
|
|
dispatch(setMessages([...messages.slice(0, -2), currentMsg, errorResponse]));
|
2023-02-13 21:15:28 -05:00
|
|
|
dispatch(setText(message));
|
2023-02-08 15:26:42 -05:00
|
|
|
dispatch(setError(true));
|
2023-02-08 00:02:29 -05:00
|
|
|
return;
|
|
|
|
|
};
|
2023-02-13 21:15:28 -05:00
|
|
|
const submission = {
|
|
|
|
|
model,
|
|
|
|
|
text: message,
|
|
|
|
|
convo,
|
|
|
|
|
messageHandler,
|
|
|
|
|
convoHandler,
|
2023-03-03 15:52:06 -05:00
|
|
|
errorHandler,
|
|
|
|
|
chatGptLabel,
|
|
|
|
|
promptPrefix
|
2023-02-13 21:15:28 -05:00
|
|
|
};
|
|
|
|
|
console.log('User Input:', message);
|
|
|
|
|
handleSubmit(submission);
|
2023-02-05 15:29:35 -05:00
|
|
|
};
|
|
|
|
|
|
2023-02-08 08:27:23 -05:00
|
|
|
const handleKeyDown = (e) => {
|
|
|
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleKeyUp = (e) => {
|
2023-02-04 20:48:33 -05:00
|
|
|
if (e.key === 'Enter' && e.shiftKey) {
|
2023-02-22 21:30:48 -05:00
|
|
|
return console.log('Enter + Shift');
|
2023-02-04 20:48:33 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-22 21:30:48 -05:00
|
|
|
if (isSubmitting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-08 08:27:23 -05:00
|
|
|
|
2023-02-22 21:30:48 -05:00
|
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
2023-02-06 21:17:46 -05:00
|
|
|
submitMessage();
|
2023-02-04 20:48:33 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-07 16:22:35 -05:00
|
|
|
const changeHandler = (e) => {
|
|
|
|
|
const { value } = e.target;
|
|
|
|
|
if (isSubmitting && (value === '' || value === '\n')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-08 09:59:01 -05:00
|
|
|
dispatch(setText(value));
|
2023-02-07 16:22:35 -05:00
|
|
|
};
|
|
|
|
|
|
2023-02-11 10:22:15 -05:00
|
|
|
const tryAgain = (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dispatch(setError(false));
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-04 20:48:33 -05:00
|
|
|
return (
|
2023-02-08 09:15:47 -05:00
|
|
|
<div className="md:bg-vert-light-gradient dark:md:bg-vert-dark-gradient absolute bottom-0 left-0 w-full border-t bg-white dark:border-white/20 dark:bg-gray-800 md:border-t-0 md:border-transparent md:!bg-transparent md:dark:border-transparent">
|
2023-02-06 13:27:28 -05:00
|
|
|
<form className="stretch mx-2 flex flex-row gap-3 pt-2 last:mb-2 md:last:mb-6 lg:mx-auto lg:max-w-3xl lg:pt-6">
|
|
|
|
|
<div className="relative flex h-full flex-1 md:flex-col">
|
|
|
|
|
<div className="ml-1 mt-1.5 flex justify-center gap-0 md:m-auto md:mb-2 md:w-full md:gap-2" />
|
2023-02-21 19:40:28 -05:00
|
|
|
{error ? (
|
2023-02-11 10:22:15 -05:00
|
|
|
<Regenerate
|
|
|
|
|
submitMessage={submitMessage}
|
|
|
|
|
tryAgain={tryAgain}
|
2023-02-11 11:37:20 -05:00
|
|
|
errorMessage={errorMessage}
|
2023-02-11 10:22:15 -05:00
|
|
|
/>
|
2023-02-08 15:26:42 -05:00
|
|
|
) : (
|
2023-03-03 15:52:06 -05:00
|
|
|
<div
|
|
|
|
|
className={`relative flex w-full flex-grow flex-col rounded-md border border-black/10 ${
|
|
|
|
|
disabled ? 'bg-gray-100' : 'bg-white'
|
|
|
|
|
} py-2 shadow-[0_0_10px_rgba(0,0,0,0.10)] dark:border-gray-900/50 ${
|
|
|
|
|
disabled ? 'dark:bg-gray-900' : 'dark:bg-gray-700'
|
|
|
|
|
} dark:text-white dark:shadow-[0_0_15px_rgba(0,0,0,0.10)] md:py-3 md:pl-4`}
|
|
|
|
|
>
|
2023-02-13 15:58:35 -05:00
|
|
|
<ModelMenu />
|
2023-02-08 15:26:42 -05:00
|
|
|
<TextareaAutosize
|
|
|
|
|
tabIndex="0"
|
|
|
|
|
// style={{maxHeight: '200px', height: '24px', overflowY: 'hidden'}}
|
|
|
|
|
rows="1"
|
|
|
|
|
value={text}
|
|
|
|
|
onKeyUp={handleKeyUp}
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
onChange={changeHandler}
|
2023-03-03 15:52:06 -05:00
|
|
|
placeholder={disabled ? 'Choose another model or customize GPT again' : ''}
|
|
|
|
|
disabled={disabled}
|
2023-02-22 22:42:22 -05:00
|
|
|
className="m-0 h-auto max-h-52 resize-none overflow-auto border-0 bg-transparent p-0 pl-9 pr-8 leading-6 focus:outline-none focus:ring-0 focus-visible:ring-0 dark:bg-transparent md:pl-8"
|
2023-02-08 15:26:42 -05:00
|
|
|
/>
|
|
|
|
|
<SubmitButton submitMessage={submitMessage} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2023-02-06 13:27:28 -05:00
|
|
|
</div>
|
|
|
|
|
</form>
|
2023-02-11 12:54:02 -05:00
|
|
|
<Footer />
|
2023-02-06 13:27:28 -05:00
|
|
|
</div>
|
2023-02-04 20:48:33 -05:00
|
|
|
);
|
2023-02-20 21:52:08 -05:00
|
|
|
}
|