mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
feat: fully multipath and resubmit
This commit is contained in:
parent
4a39965b22
commit
a4d5f6a3f2
5 changed files with 197 additions and 89 deletions
|
|
@ -22,7 +22,7 @@ router.post('/gen_title', async (req, res) => {
|
||||||
: await titleConvo({
|
: await titleConvo({
|
||||||
model: convo?.model,
|
model: convo?.model,
|
||||||
message: firstMessage?.text,
|
message: firstMessage?.text,
|
||||||
// response: JSON.stringify(secondMessage?.text || '')
|
response: JSON.stringify(secondMessage?.text || '')
|
||||||
});
|
});
|
||||||
|
|
||||||
await saveConvo({
|
await saveConvo({
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ export default function TextChat({ messages }) {
|
||||||
const messageHandler = (data, currentState, currentMsg) => {
|
const messageHandler = (data, currentState, currentMsg) => {
|
||||||
const { messages, _currentMsg, message, sender } = currentState;
|
const { messages, _currentMsg, message, sender } = currentState;
|
||||||
|
|
||||||
dispatch(setMessages([...messages, currentMsg, { sender, text: data }]));
|
dispatch(setMessages([...messages, currentMsg, { sender, text: data, parentMessageId: currentMsg?.messageId, messageId: currentMsg?.messageId + '_' }]));
|
||||||
};
|
};
|
||||||
|
|
||||||
const createdHandler = (data, currentState, currentMsg) => {
|
const createdHandler = (data, currentState, currentMsg) => {
|
||||||
|
|
@ -152,11 +152,13 @@ export default function TextChat({ messages }) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is not a real messageId, it is used as placeholder before real messageId returned
|
||||||
|
const fakeMessageId = crypto.randomUUID();
|
||||||
const isCustomModel = model === 'chatgptCustom' || !initial[model];
|
const isCustomModel = model === 'chatgptCustom' || !initial[model];
|
||||||
const message = text.trim();
|
const message = text.trim();
|
||||||
const currentMsg = { sender: 'User', text: message, current: true, isCreatedByUser: true };
|
const currentMsg = { sender: 'User', text: message, current: true, isCreatedByUser: true, parentMessageId: convo.parentMessageId || '00000000-0000-0000-0000-000000000000', messageId: fakeMessageId };
|
||||||
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
||||||
const initialResponse = { sender, text: '' };
|
const initialResponse = { sender, text: '', parentMessageId: fakeMessageId };
|
||||||
|
|
||||||
dispatch(setSubmitState(true));
|
dispatch(setSubmitState(true));
|
||||||
dispatch(setMessages([...messages, currentMsg, initialResponse]));
|
dispatch(setMessages([...messages, currentMsg, initialResponse]));
|
||||||
|
|
@ -166,9 +168,7 @@ export default function TextChat({ messages }) {
|
||||||
convo,
|
convo,
|
||||||
isCustomModel,
|
isCustomModel,
|
||||||
message: {
|
message: {
|
||||||
sender: 'User',
|
...currentMsg,
|
||||||
text: message,
|
|
||||||
isCreatedByUser: true,
|
|
||||||
model,
|
model,
|
||||||
chatGptLabel,
|
chatGptLabel,
|
||||||
promptPrefix,
|
promptPrefix,
|
||||||
|
|
@ -193,7 +193,7 @@ export default function TextChat({ messages }) {
|
||||||
payload = {
|
payload = {
|
||||||
...payload,
|
...payload,
|
||||||
conversationId: convo.conversationId,
|
conversationId: convo.conversationId,
|
||||||
parentMessageId: convo.parentMessageId
|
parentMessageId: convo.parentMessageId || '00000000-0000-0000-0000-000000000000'
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,20 +4,59 @@ import { useSelector, useDispatch } from 'react-redux';
|
||||||
import GPTIcon from '../svg/GPTIcon';
|
import GPTIcon from '../svg/GPTIcon';
|
||||||
import BingIcon from '../svg/BingIcon';
|
import BingIcon from '../svg/BingIcon';
|
||||||
import HoverButtons from './HoverButtons';
|
import HoverButtons from './HoverButtons';
|
||||||
|
import SiblingSwitch from './SiblingSwitch';
|
||||||
import Spinner from '../svg/Spinner';
|
import Spinner from '../svg/Spinner';
|
||||||
import { setError } from '~/store/convoSlice';
|
import { setError } from '~/store/convoSlice';
|
||||||
import { setMessages } from '~/store/messageSlice';
|
import { setMessages } from '~/store/messageSlice';
|
||||||
import { setSubmitState, setSubmission } from '~/store/submitSlice';
|
import { setSubmitState, setSubmission } from '~/store/submitSlice';
|
||||||
import { setText } from '~/store/textSlice';
|
import { setText } from '~/store/textSlice';
|
||||||
|
import { setConversation } from '../../store/convoSlice';
|
||||||
|
|
||||||
|
const MultiMessage = ({
|
||||||
|
messageList,
|
||||||
|
messages,
|
||||||
|
scrollToBottom,
|
||||||
|
currentEditId,
|
||||||
|
setCurrentEditId
|
||||||
|
}) => {
|
||||||
|
const [siblingIdx, setSiblingIdx] = useState(0)
|
||||||
|
|
||||||
|
const setSiblingIdxRev = (value) => {
|
||||||
|
setSiblingIdx(messageList?.length - value - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!messageList?.length) return null;
|
||||||
|
|
||||||
|
if (siblingIdx >= messageList?.length) {
|
||||||
|
setSiblingIdx(0)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
return <Message
|
||||||
|
key={messageList[messageList.length - siblingIdx - 1].messageId}
|
||||||
|
message={messageList[messageList.length - siblingIdx - 1]}
|
||||||
|
messages={messages}
|
||||||
|
scrollToBottom={scrollToBottom}
|
||||||
|
currentEditId={currentEditId}
|
||||||
|
setCurrentEditId={setCurrentEditId}
|
||||||
|
|
||||||
|
siblingIdx={messageList.length - siblingIdx - 1}
|
||||||
|
siblingCount={messageList.length}
|
||||||
|
setSiblingIdx={setSiblingIdxRev}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
|
||||||
|
export { MultiMessage };
|
||||||
|
|
||||||
export default function Message({
|
export default function Message({
|
||||||
message,
|
message,
|
||||||
messages,
|
messages,
|
||||||
last = false,
|
|
||||||
scrollToBottom,
|
scrollToBottom,
|
||||||
edit,
|
currentEditId,
|
||||||
currentEditIdx,
|
setCurrentEditId,
|
||||||
enterEdit
|
siblingIdx,
|
||||||
|
siblingCount,
|
||||||
|
setSiblingIdx
|
||||||
}) {
|
}) {
|
||||||
const { isSubmitting, model, chatGptLabel, promptPrefix } = useSelector((state) => state.submit);
|
const { isSubmitting, model, chatGptLabel, promptPrefix } = useSelector((state) => state.submit);
|
||||||
const [abortScroll, setAbort] = useState(false);
|
const [abortScroll, setAbort] = useState(false);
|
||||||
|
|
@ -26,6 +65,9 @@ export default function Message({
|
||||||
const convo = useSelector((state) => state.convo);
|
const convo = useSelector((state) => state.convo);
|
||||||
const { initial } = useSelector((state) => state.models);
|
const { initial } = useSelector((state) => state.models);
|
||||||
const { error: convoError } = convo;
|
const { error: convoError } = convo;
|
||||||
|
const last = !message?.children?.length
|
||||||
|
|
||||||
|
const edit = message.messageId == currentEditId;
|
||||||
|
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
|
@ -38,10 +80,17 @@ export default function Message({
|
||||||
}
|
}
|
||||||
}, [isSubmitting, text, blinker, scrollToBottom, abortScroll]);
|
}, [isSubmitting, text, blinker, scrollToBottom, abortScroll]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (last)
|
||||||
|
dispatch(setConversation({parentMessageId: message?.messageId}))
|
||||||
|
}, [last, ])
|
||||||
|
|
||||||
if (sender === '') {
|
if (sender === '') {
|
||||||
return <Spinner />;
|
return <Spinner />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const enterEdit = (cancel) => setCurrentEditId(cancel?-1:message.messageId)
|
||||||
|
|
||||||
const handleWheel = () => {
|
const handleWheel = () => {
|
||||||
if (blinker) {
|
if (blinker) {
|
||||||
setAbort(true);
|
setAbort(true);
|
||||||
|
|
@ -105,51 +154,55 @@ export default function Message({
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is not a real messageId, it is used as placeholder before real messageId returned
|
||||||
|
const fakeMessageId = crypto.randomUUID();
|
||||||
const isCustomModel = model === 'chatgptCustom' || !initial[model];
|
const isCustomModel = model === 'chatgptCustom' || !initial[model];
|
||||||
const currentMsg = { ...message, sender: 'User', text: text.trim(), current: true, isCreatedByUser: true };
|
const currentMsg = { ...message, sender: 'User', text: text.trim(), current: true, isCreatedByUser: true, messageId: fakeMessageId };
|
||||||
console.log(model)
|
|
||||||
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
||||||
|
|
||||||
const initialResponse = { sender, text: '' };
|
const initialResponse = { sender, text: '', parentMessageId: fakeMessageId };
|
||||||
|
|
||||||
dispatch(setSubmitState(true));
|
dispatch(setSubmitState(true));
|
||||||
dispatch(setMessages([...messages.slice(0, currentEditIdx), currentMsg, initialResponse]));
|
dispatch(setMessages([...messages, currentMsg, initialResponse]));
|
||||||
dispatch(setText(''));
|
dispatch(setText(''));
|
||||||
|
|
||||||
const submission = {
|
const submission = {
|
||||||
isCustomModel,
|
isCustomModel,
|
||||||
message: {
|
message: {
|
||||||
...message,
|
...currentMsg,
|
||||||
text: text.trim(),
|
|
||||||
model,
|
model,
|
||||||
chatGptLabel,
|
chatGptLabel,
|
||||||
promptPrefix,
|
promptPrefix,
|
||||||
},
|
},
|
||||||
messages: messages.slice(0, currentEditIdx),
|
messages: messages,
|
||||||
currentMsg,
|
currentMsg,
|
||||||
initialResponse,
|
initialResponse,
|
||||||
sender,
|
sender,
|
||||||
};
|
};
|
||||||
console.log('User Input:', message);
|
console.log('User Input:', currentMsg?.text);
|
||||||
// handleSubmit(submission);
|
// handleSubmit(submission);
|
||||||
dispatch(setSubmission(submission));
|
dispatch(setSubmission(submission));
|
||||||
|
|
||||||
|
setSiblingIdx(siblingCount - 1)
|
||||||
enterEdit(true);
|
enterEdit(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<>
|
||||||
<div
|
<div
|
||||||
{...props}
|
{...props}
|
||||||
onWheel={handleWheel}
|
onWheel={handleWheel}
|
||||||
>
|
>
|
||||||
<div className="m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
<div className="relative m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
||||||
<strong className="relative flex w-[30px] flex-col items-end text-right text-xs md:text-sm">
|
|
||||||
|
<div className="relative flex w-[30px] flex-col items-end text-right text-xs md:text-sm">
|
||||||
{typeof icon === 'string' && icon.match(/[^\u0000-\u007F]+/) ? (
|
{typeof icon === 'string' && icon.match(/[^\u0000-\u007F]+/) ? (
|
||||||
<span className=" direction-rtl w-40 overflow-x-scroll">{icon}</span>
|
<span className=" direction-rtl w-40 overflow-x-scroll">{icon}</span>
|
||||||
) : (
|
) : (
|
||||||
icon
|
icon
|
||||||
)}
|
)}
|
||||||
</strong>
|
<SiblingSwitch siblingIdx={siblingIdx} siblingCount={siblingCount} setSiblingIdx={setSiblingIdx} />
|
||||||
|
</div>
|
||||||
<div className="relative flex w-[calc(100%-50px)] flex-col gap-1 whitespace-pre-wrap md:gap-3 lg:w-[calc(100%-115px)]">
|
<div className="relative flex w-[calc(100%-50px)] flex-col gap-1 whitespace-pre-wrap md:gap-3 lg:w-[calc(100%-115px)]">
|
||||||
<div className="flex flex-grow flex-col gap-3">
|
<div className="flex flex-grow flex-col gap-3">
|
||||||
{error ? (
|
{error ? (
|
||||||
|
|
@ -197,5 +250,13 @@ export default function Message({
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<MultiMessage
|
||||||
|
messageList={message.children}
|
||||||
|
messages={messages}
|
||||||
|
scrollToBottom={scrollToBottom}
|
||||||
|
currentEditId={currentEditId}
|
||||||
|
setCurrentEditId={setCurrentEditId}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
client/src/components/Messages/SiblingSwitch.jsx
Normal file
26
client/src/components/Messages/SiblingSwitch.jsx
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
export default function SiblingSwitch({
|
||||||
|
siblingIdx,
|
||||||
|
siblingCount,
|
||||||
|
setSiblingIdx
|
||||||
|
}) {
|
||||||
|
const previous = () => {
|
||||||
|
setSiblingIdx(siblingIdx - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const next = () => {
|
||||||
|
setSiblingIdx(siblingIdx + 1);
|
||||||
|
}
|
||||||
|
return siblingCount > 1 ? (
|
||||||
|
<div className="text-xs flex items-center justify-center gap-1 invisible absolute left-0 top-2 -ml-4 -translate-x-full group-hover:visible">
|
||||||
|
<button className="dark:text-white disabled:text-gray-300 dark:disabled:text-gray-400" onClick={previous} disabled={siblingIdx==0}>
|
||||||
|
<svg stroke="currentColor" fill="none" strokeWidth="1.5" viewBox="0 0 24 24" strokeLinecap="round" strokeLinejoin="round" className="h-3 w-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><polyline points="15 18 9 12 15 6"></polyline></svg>
|
||||||
|
</button>
|
||||||
|
<span className="flex-grow flex-shrink-0">{siblingIdx + 1}/{siblingCount}</span>
|
||||||
|
<button className="dark:text-white disabled:text-gray-300 dark:disabled:text-gray-400" onClick={next} disabled={siblingIdx==siblingCount-1}>
|
||||||
|
<svg stroke="currentColor" fill="none" strokeWidth="1.5" viewBox="0 0 24 24" strokeLinecap="round" strokeLinejoin="round" className="h-3 w-3" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><polyline points="9 18 15 12 9 6"></polyline></svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
):null;
|
||||||
|
}
|
||||||
|
|
@ -1,15 +1,17 @@
|
||||||
import React, { useEffect, useState, useRef } from 'react';
|
import React, { useEffect, useState, useRef, useMemo } from 'react';
|
||||||
import { CSSTransition } from 'react-transition-group';
|
import { CSSTransition } from 'react-transition-group';
|
||||||
import ScrollToBottom from './ScrollToBottom';
|
import ScrollToBottom from './ScrollToBottom';
|
||||||
import Message from './Message';
|
import { MultiMessage } from './Message';
|
||||||
|
import Conversation from '../Conversations/Conversation';
|
||||||
|
import { useSelector } from 'react-redux';
|
||||||
|
|
||||||
const Messages = ({ messages }) => {
|
const Messages = ({ messages }) => {
|
||||||
const [currentEditIdx, setCurrentEditIdx] = useState(-1)
|
const [currentEditId, setCurrentEditId] = useState(-1)
|
||||||
|
const { conversationId } = useSelector((state) => state.convo);
|
||||||
const [showScrollButton, setShowScrollButton] = useState(false);
|
const [showScrollButton, setShowScrollButton] = useState(false);
|
||||||
const scrollableRef = useRef(null);
|
const scrollableRef = useRef(null);
|
||||||
const messagesEndRef = useRef(null);
|
const messagesEndRef = useRef(null);
|
||||||
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timeoutId = setTimeout(() => {
|
const timeoutId = setTimeout(() => {
|
||||||
const scrollable = scrollableRef.current;
|
const scrollable = scrollableRef.current;
|
||||||
|
|
@ -22,6 +24,29 @@ const Messages = ({ messages }) => {
|
||||||
};
|
};
|
||||||
}, [messages]);
|
}, [messages]);
|
||||||
|
|
||||||
|
const messageTree = useMemo(() => buildTree(messages), [messages, ]);
|
||||||
|
|
||||||
|
function buildTree(messages) {
|
||||||
|
let messageMap = {};
|
||||||
|
let rootMessages = [];
|
||||||
|
|
||||||
|
// Traverse the messages array and store each element in messageMap.
|
||||||
|
messages.forEach(message => {
|
||||||
|
messageMap[message.messageId] = {...message, children: []};
|
||||||
|
|
||||||
|
if (message.parentMessageId === "00000000-0000-0000-0000-000000000000") {
|
||||||
|
rootMessages.push(messageMap[message.messageId]);
|
||||||
|
} else {
|
||||||
|
const parentMessage = messageMap[message.parentMessageId];
|
||||||
|
if (parentMessage) {
|
||||||
|
parentMessage.children.push(messageMap[message.messageId]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return rootMessages;
|
||||||
|
}
|
||||||
|
|
||||||
const scrollToBottom = () => {
|
const scrollToBottom = () => {
|
||||||
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
||||||
setShowScrollButton(false);
|
setShowScrollButton(false);
|
||||||
|
|
@ -59,18 +84,14 @@ const Messages = ({ messages }) => {
|
||||||
{/* <div className="flex-1 overflow-hidden"> */}
|
{/* <div className="flex-1 overflow-hidden"> */}
|
||||||
<div className="h-full dark:gpt-dark-gray">
|
<div className="h-full dark:gpt-dark-gray">
|
||||||
<div className="flex h-full flex-col items-center text-sm dark:gpt-dark-gray">
|
<div className="flex h-full flex-col items-center text-sm dark:gpt-dark-gray">
|
||||||
{messages.map((message, i) => (
|
<MultiMessage
|
||||||
<Message
|
key={conversationId} // avoid internal state mixture
|
||||||
key={i}
|
messageList={messageTree}
|
||||||
message={message}
|
|
||||||
messages={messages}
|
messages={messages}
|
||||||
last={i === messages.length - 1}
|
scrollToBottom={scrollToBottom}
|
||||||
scrollToBottom={i === messages.length - 1 ? scrollToBottom : null}
|
currentEditId={currentEditId}
|
||||||
edit={i===currentEditIdx}
|
setCurrentEditId={setCurrentEditId}
|
||||||
currentEditIdx={currentEditIdx}
|
|
||||||
enterEdit={(cancel) => setCurrentEditIdx(cancel?-1:i)}
|
|
||||||
/>
|
/>
|
||||||
))}
|
|
||||||
<CSSTransition
|
<CSSTransition
|
||||||
in={showScrollButton}
|
in={showScrollButton}
|
||||||
timeout={400}
|
timeout={400}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue