mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
fix: convo resets on model change
This commit is contained in:
parent
796d8031e8
commit
918f2fecb6
4 changed files with 64 additions and 40 deletions
|
|
@ -1,18 +1,17 @@
|
||||||
import React, { useEffect, useRef, useState } from 'react';
|
import React, { useEffect, useRef, useState } from 'react';
|
||||||
import { SSE } from '~/utils/sse';
|
import { SSE } from '~/utils/sse';
|
||||||
import axios from 'axios';
|
|
||||||
import SubmitButton from './SubmitButton';
|
import SubmitButton from './SubmitButton';
|
||||||
import Regenerate from './Regenerate';
|
import Regenerate from './Regenerate';
|
||||||
import ModelMenu from '../Models/ModelMenu';
|
import ModelMenu from '../Models/ModelMenu';
|
||||||
import Footer from './Footer';
|
import Footer from './Footer';
|
||||||
import TextareaAutosize from 'react-textarea-autosize';
|
import TextareaAutosize from 'react-textarea-autosize';
|
||||||
import handleSubmit from '~/utils/handleSubmit';
|
import createPayload from '~/utils/createPayload';
|
||||||
|
import resetConvo from '~/utils/resetConvo';
|
||||||
import { useSelector, useDispatch } from 'react-redux';
|
import { useSelector, useDispatch } from 'react-redux';
|
||||||
import { setConversation, setError, refreshConversation } from '~/store/convoSlice';
|
import { setConversation, setError, refreshConversation } 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 manualSWR from '~/utils/fetchers';
|
|
||||||
|
|
||||||
export default function TextChat({ messages }) {
|
export default function TextChat({ messages }) {
|
||||||
const [errorMessage, setErrorMessage] = useState('');
|
const [errorMessage, setErrorMessage] = useState('');
|
||||||
|
|
@ -25,7 +24,6 @@ export default function TextChat({ messages }) {
|
||||||
useSelector((state) => state.submit);
|
useSelector((state) => state.submit);
|
||||||
const { text } = useSelector((state) => state.text);
|
const { text } = useSelector((state) => state.text);
|
||||||
const { error } = convo;
|
const { error } = convo;
|
||||||
const genTitle = manualSWR(`/api/convos/gen_title`, 'post');
|
|
||||||
|
|
||||||
// auto focus to input, when enter a conversation.
|
// auto focus to input, when enter a conversation.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -157,8 +155,12 @@ export default function TextChat({ messages }) {
|
||||||
const fakeMessageId = crypto.randomUUID();
|
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, parentMessageId: convo.parentMessageId || '00000000-0000-0000-0000-000000000000', messageId: fakeMessageId };
|
|
||||||
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
||||||
|
let parentMessageId = convo.parentMessageId || '00000000-0000-0000-0000-000000000000';
|
||||||
|
if (resetConvo(messages, sender)) {
|
||||||
|
parentMessageId = '00000000-0000-0000-0000-000000000000';
|
||||||
|
}
|
||||||
|
const currentMsg = { sender: 'User', text: message, current: true, isCreatedByUser: true, parentMessageId , messageId: fakeMessageId };
|
||||||
const initialResponse = { sender, text: '', parentMessageId: fakeMessageId, submitting: true };
|
const initialResponse = { sender, text: '', parentMessageId: fakeMessageId, submitting: true };
|
||||||
|
|
||||||
dispatch(setSubmitState(true));
|
dispatch(setSubmitState(true));
|
||||||
|
|
@ -184,38 +186,6 @@ export default function TextChat({ messages }) {
|
||||||
dispatch(setSubmission(submission));
|
dispatch(setSubmission(submission));
|
||||||
};
|
};
|
||||||
|
|
||||||
const createPayload = ({ convo, message }) => {
|
|
||||||
const endpoint = `/api/ask`;
|
|
||||||
let payload = { ...message };
|
|
||||||
const { model } = message
|
|
||||||
|
|
||||||
if (!payload.conversationId)
|
|
||||||
if (convo?.conversationId && convo?.parentMessageId) {
|
|
||||||
payload = {
|
|
||||||
...payload,
|
|
||||||
conversationId: convo.conversationId,
|
|
||||||
parentMessageId: convo.parentMessageId || '00000000-0000-0000-0000-000000000000'
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
const isBing = model === 'bingai' || model === 'sydney';
|
|
||||||
if (isBing && convo?.conversationId) {
|
|
||||||
payload = {
|
|
||||||
...payload,
|
|
||||||
jailbreakConversationId: convo.jailbreakConversationId,
|
|
||||||
conversationId: convo.conversationId,
|
|
||||||
conversationSignature: convo.conversationSignature,
|
|
||||||
clientId: convo.clientId,
|
|
||||||
invocationId: convo.invocationId
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let server = endpoint;
|
|
||||||
server = model === 'bingai' ? server + '/bing' : server;
|
|
||||||
server = model === 'sydney' ? server + '/sydney' : server;
|
|
||||||
return { server, payload };
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (Object.keys(submission).length === 0) {
|
if (Object.keys(submission).length === 0) {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,9 @@ export default function ModelMenu() {
|
||||||
}, [model]);
|
}, [model]);
|
||||||
|
|
||||||
const onChange = (value, custom = false) => {
|
const onChange = (value, custom = false) => {
|
||||||
|
// Set new conversation
|
||||||
|
dispatch(setNewConvo());
|
||||||
|
dispatch(setSubmission({}));
|
||||||
// if (custom) {
|
// if (custom) {
|
||||||
// mutate();
|
// mutate();
|
||||||
// }
|
// }
|
||||||
|
|
@ -89,9 +92,7 @@ export default function ModelMenu() {
|
||||||
dispatch(setCustomModel(null));
|
dispatch(setCustomModel(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set new conversation
|
|
||||||
dispatch(setNewConvo());
|
|
||||||
dispatch(setSubmission({}));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onOpenChange = (open) => {
|
const onOpenChange = (open) => {
|
||||||
|
|
|
||||||
31
client/src/utils/createPayload.js
Normal file
31
client/src/utils/createPayload.js
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
export default function createPayload({ convo, message }) {
|
||||||
|
const endpoint = `/api/ask`;
|
||||||
|
let payload = { ...message };
|
||||||
|
const { model } = message;
|
||||||
|
|
||||||
|
if (!payload.conversationId)
|
||||||
|
if (convo?.conversationId && convo?.parentMessageId) {
|
||||||
|
payload = {
|
||||||
|
...payload,
|
||||||
|
conversationId: convo.conversationId,
|
||||||
|
parentMessageId: convo.parentMessageId || '00000000-0000-0000-0000-000000000000'
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const isBing = model === 'bingai' || model === 'sydney';
|
||||||
|
if (isBing && convo?.conversationId) {
|
||||||
|
payload = {
|
||||||
|
...payload,
|
||||||
|
jailbreakConversationId: convo.jailbreakConversationId,
|
||||||
|
conversationId: convo.conversationId,
|
||||||
|
conversationSignature: convo.conversationSignature,
|
||||||
|
clientId: convo.clientId,
|
||||||
|
invocationId: convo.invocationId
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let server = endpoint;
|
||||||
|
server = model === 'bingai' ? server + '/bing' : server;
|
||||||
|
server = model === 'sydney' ? server + '/sydney' : server;
|
||||||
|
return { server, payload };
|
||||||
|
};
|
||||||
22
client/src/utils/resetConvo.js
Normal file
22
client/src/utils/resetConvo.js
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
export default function resetConvo(messages, sender) {
|
||||||
|
if (messages.length === 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let modelMessages = messages.filter((message) => !message.isCreatedByUser);
|
||||||
|
let lastModel = modelMessages[modelMessages.length - 1].sender;
|
||||||
|
if (lastModel !== sender) {
|
||||||
|
console.log(
|
||||||
|
'Model change! Reseting convo. Original messages: ',
|
||||||
|
messages,
|
||||||
|
'filtered messages: ',
|
||||||
|
modelMessages,
|
||||||
|
'last model: ',
|
||||||
|
lastModel,
|
||||||
|
'sender: ',
|
||||||
|
sender
|
||||||
|
);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue