mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
wip: feat: abort messages and continue conversation
fix(addToCache.js): remove unused variables and parameters feat(addToCache.js): add message to cache with id, parentMessageId, role, and text fix(askOpenAI.js): remove parentMessageId parameter from addToCache call feat(MessageHandler.jsx): add latestMessage to store on cancel of submission, and generate messageId and parentMessageId for latestMessage
This commit is contained in:
parent
a81bd27b39
commit
a953fc9f2b
3 changed files with 25 additions and 22 deletions
|
|
@ -1,21 +1,16 @@
|
||||||
const Keyv = require('keyv');
|
const Keyv = require('keyv');
|
||||||
const { KeyvFile } = require('keyv-file');
|
const { KeyvFile } = require('keyv-file');
|
||||||
const crypto = require('crypto');
|
|
||||||
const { saveMessage } = require('../../../models');
|
const { saveMessage } = require('../../../models');
|
||||||
|
|
||||||
const addToCache = async ({
|
const addToCache = async ({ endpointOption, userMessage, latestMessage }) => {
|
||||||
endpointOption,
|
|
||||||
conversationId,
|
|
||||||
userMessage,
|
|
||||||
latestMessage,
|
|
||||||
parentMessageId
|
|
||||||
}) => {
|
|
||||||
try {
|
try {
|
||||||
const conversationsCache = new Keyv({
|
const conversationsCache = new Keyv({
|
||||||
store: new KeyvFile({ filename: './data/cache.json' }),
|
store: new KeyvFile({ filename: './data/cache.json' }),
|
||||||
namespace: 'chatgpt', // should be 'bing' for bing/sydney
|
namespace: 'chatgpt' // should be 'bing' for bing/sydney
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { conversationId, messageId, parentMessageId, text } = latestMessage;
|
||||||
|
|
||||||
let conversation = await conversationsCache.get(conversationId);
|
let conversation = await conversationsCache.get(conversationId);
|
||||||
// used to generate a title for the conversation if none exists
|
// used to generate a title for the conversation if none exists
|
||||||
// let isNewConversation = false;
|
// let isNewConversation = false;
|
||||||
|
|
@ -38,13 +33,13 @@ const addToCache = async ({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const messageId = crypto.randomUUID();
|
// const messageId = crypto.randomUUID();
|
||||||
|
|
||||||
let responseMessage = {
|
let responseMessage = {
|
||||||
id: messageId,
|
id: messageId,
|
||||||
parentMessageId,
|
parentMessageId,
|
||||||
role: roles(endpointOption),
|
role: roles(endpointOption),
|
||||||
message: latestMessage
|
message: text
|
||||||
};
|
};
|
||||||
|
|
||||||
await saveMessage({
|
await saveMessage({
|
||||||
|
|
@ -52,7 +47,7 @@ const addToCache = async ({
|
||||||
conversationId,
|
conversationId,
|
||||||
messageId,
|
messageId,
|
||||||
sender: responseMessage.role,
|
sender: responseMessage.role,
|
||||||
text: latestMessage
|
text
|
||||||
});
|
});
|
||||||
|
|
||||||
conversation.messages.push(userMessage, responseMessage);
|
conversation.messages.push(userMessage, responseMessage);
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ const { handleError, sendMessage, createOnProgress, handleText } = require('./ha
|
||||||
const abortControllers = new Map();
|
const abortControllers = new Map();
|
||||||
|
|
||||||
router.post('/abort', async (req, res) => {
|
router.post('/abort', async (req, res) => {
|
||||||
const { abortKey, latestMessage, parentMessageId } = req.body;
|
const { abortKey, latestMessage } = req.body;
|
||||||
console.log(`req.body`, req.body);
|
console.log(`req.body`, req.body);
|
||||||
if (!abortControllers.has(abortKey)) {
|
if (!abortControllers.has(abortKey)) {
|
||||||
return res.status(404).send('Request not found');
|
return res.status(404).send('Request not found');
|
||||||
|
|
@ -24,7 +24,7 @@ router.post('/abort', async (req, res) => {
|
||||||
abortController.abort();
|
abortController.abort();
|
||||||
abortControllers.delete(abortKey);
|
abortControllers.delete(abortKey);
|
||||||
console.log('Aborted request', abortKey, userMessage, endpointOption);
|
console.log('Aborted request', abortKey, userMessage, endpointOption);
|
||||||
await addToCache({ endpointOption, conversationId: abortKey, userMessage, latestMessage, parentMessageId });
|
await addToCache({ endpointOption, userMessage, latestMessage });
|
||||||
|
|
||||||
res.status(200).send('Aborted');
|
res.status(200).send('Aborted');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { useRecoilValue, useRecoilState, useResetRecoilState, useSetRecoilState
|
||||||
import { SSE } from '~/data-provider/sse.mjs';
|
import { SSE } from '~/data-provider/sse.mjs';
|
||||||
import createPayload from '~/data-provider/createPayload';
|
import createPayload from '~/data-provider/createPayload';
|
||||||
import { useAbortRequestWithMessage } from '~/data-provider';
|
import { useAbortRequestWithMessage } from '~/data-provider';
|
||||||
|
import { v4 } from 'uuid';
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
|
|
||||||
export default function MessageHandler() {
|
export default function MessageHandler() {
|
||||||
|
|
@ -13,6 +13,7 @@ export default function MessageHandler() {
|
||||||
const setConversation = useSetRecoilState(store.conversation);
|
const setConversation = useSetRecoilState(store.conversation);
|
||||||
const resetLatestMessage = useResetRecoilState(store.latestMessage);
|
const resetLatestMessage = useResetRecoilState(store.latestMessage);
|
||||||
const [lastResponse, setLastResponse] = useRecoilState(store.lastResponse);
|
const [lastResponse, setLastResponse] = useRecoilState(store.lastResponse);
|
||||||
|
const setLatestMessage = useSetRecoilState(store.latestMessage);
|
||||||
const setSubmission = useSetRecoilState(store.submission);
|
const setSubmission = useSetRecoilState(store.submission);
|
||||||
const [source, setSource] = useState(null);
|
const [source, setSource] = useState(null);
|
||||||
// const [abortKey, setAbortKey] = useState(null);
|
// const [abortKey, setAbortKey] = useState(null);
|
||||||
|
|
@ -50,6 +51,7 @@ export default function MessageHandler() {
|
||||||
|
|
||||||
const cancelHandler = (data, submission) => {
|
const cancelHandler = (data, submission) => {
|
||||||
const { messages, message, initialResponse, isRegenerate = false } = submission;
|
const { messages, message, initialResponse, isRegenerate = false } = submission;
|
||||||
|
const { text, messageId, parentMessageId } = data;
|
||||||
|
|
||||||
if (isRegenerate) {
|
if (isRegenerate) {
|
||||||
setMessages([
|
setMessages([
|
||||||
|
|
@ -63,14 +65,15 @@ export default function MessageHandler() {
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
} else {
|
} else {
|
||||||
|
console.log('cancelHandler, isRegenerate = false');
|
||||||
setMessages([
|
setMessages([
|
||||||
...messages,
|
...messages,
|
||||||
message,
|
message,
|
||||||
{
|
{
|
||||||
...initialResponse,
|
...initialResponse,
|
||||||
text: data,
|
text,
|
||||||
parentMessageId: message?.messageId,
|
parentMessageId: message?.messageId,
|
||||||
messageId: message?.messageId + '_'
|
messageId,
|
||||||
// cancelled: true
|
// cancelled: true
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
@ -78,6 +81,7 @@ export default function MessageHandler() {
|
||||||
setSource(null);
|
setSource(null);
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
setSubmission(null);
|
setSubmission(null);
|
||||||
|
setLatestMessage(data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -168,7 +172,14 @@ export default function MessageHandler() {
|
||||||
const { endpoint } = submission.conversation;
|
const { endpoint } = submission.conversation;
|
||||||
|
|
||||||
// splitting twice because the cursor may or may not be wrapped in a span
|
// splitting twice because the cursor may or may not be wrapped in a span
|
||||||
const latestMessage = lastResponse.split('█')[0].split('<span className="result-streaming">')[0];
|
const latestMessageText = lastResponse.split('█')[0].split('<span className="result-streaming">')[0];
|
||||||
|
const latestMessage = {
|
||||||
|
text: latestMessageText,
|
||||||
|
messageId: v4(),
|
||||||
|
parentMessageId: currentParent.messageId,
|
||||||
|
conversationId: currentParent.conversationId
|
||||||
|
};
|
||||||
|
|
||||||
fetch(`/api/ask/${endpoint}/abort`, {
|
fetch(`/api/ask/${endpoint}/abort`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -176,8 +187,7 @@ export default function MessageHandler() {
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
abortKey: currentParent.conversationId,
|
abortKey: currentParent.conversationId,
|
||||||
latestMessage,
|
latestMessage
|
||||||
parentMessageId: currentParent.messageId,
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
|
@ -195,8 +205,6 @@ export default function MessageHandler() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// events.oncancel = () => cancelHandler(latestResponseText, { ...submission, message });
|
|
||||||
|
|
||||||
const { server, payload } = createPayload(submission);
|
const { server, payload } = createPayload(submission);
|
||||||
|
|
||||||
const events = new SSE(server, {
|
const events = new SSE(server, {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue