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-02-13 21:15:28 -05:00
|
|
|
const { isSubmitting, model } = 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-02-15 12:29:56 -05:00
|
|
|
const initialResponse = { sender: model, 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-02-15 12:29:56 -05:00
|
|
|
dispatch(setMessages([...messages, currentMsg, { sender: model, text: data }]));
|
2023-02-05 19:41:24 -05:00
|
|
|
};
|
2023-02-06 21:17:46 -05:00
|
|
|
const convoHandler = (data) => {
|
2023-02-12 22:57:59 -05:00
|
|
|
console.log('in convo handler');
|
2023-02-21 21:30:56 -05:00
|
|
|
if (model !== 'bingai' && convo.conversationId && convo.parentMessageId === null) {
|
2023-02-13 10:20:21 -05:00
|
|
|
const { title, conversationId, id } = data;
|
2023-02-20 21:16:40 -05:00
|
|
|
console.log('parentMessageId is null');
|
2023-02-13 10:20:21 -05:00
|
|
|
console.log('title, convoId, id', title, conversationId, id);
|
2023-02-20 21:52:08 -05:00
|
|
|
dispatch(
|
|
|
|
|
setConversation({
|
|
|
|
|
title,
|
|
|
|
|
conversationId,
|
|
|
|
|
parentMessageId: id,
|
|
|
|
|
conversationSignature: null,
|
|
|
|
|
clientId: null,
|
|
|
|
|
invocationId: null
|
|
|
|
|
})
|
|
|
|
|
);
|
2023-02-21 21:30:56 -05:00
|
|
|
} else if (model === 'bingai' && convo.invocationId === null) {
|
2023-02-20 21:16:40 -05:00
|
|
|
const { title, conversationSignature, clientId, conversationId, invocationId } = data;
|
|
|
|
|
console.log('convoSig is null');
|
|
|
|
|
console.log(
|
|
|
|
|
'bing res data',
|
|
|
|
|
title,
|
|
|
|
|
conversationSignature,
|
|
|
|
|
clientId,
|
|
|
|
|
conversationId,
|
|
|
|
|
invocationId
|
|
|
|
|
);
|
|
|
|
|
dispatch(
|
|
|
|
|
setConversation({
|
|
|
|
|
title,
|
|
|
|
|
conversationSignature,
|
|
|
|
|
clientId,
|
|
|
|
|
conversationId,
|
2023-02-20 21:52:08 -05:00
|
|
|
invocationId,
|
|
|
|
|
parentMessageId: null
|
2023-02-20 21:16:40 -05:00
|
|
|
})
|
|
|
|
|
);
|
2023-02-06 21:17:46 -05:00
|
|
|
}
|
2023-02-05 15:29:35 -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-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,
|
|
|
|
|
errorHandler
|
|
|
|
|
};
|
|
|
|
|
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-02-13 10:20:21 -05:00
|
|
|
<div className="relative flex w-full flex-grow flex-col rounded-md border border-black/10 bg-white py-2 shadow-[0_0_10px_rgba(0,0,0,0.10)] dark:border-gray-900/50 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}
|
|
|
|
|
placeholder=""
|
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
|
|
|
}
|