mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-31 15:48:51 +01:00
server fix: code refactor
This commit is contained in:
parent
ea88e9b981
commit
dae0c2d5e3
4 changed files with 90 additions and 51 deletions
|
|
@ -111,23 +111,31 @@ const ask = async ({
|
|||
|
||||
console.log('BING RESPONSE', response);
|
||||
|
||||
userMessage.conversationSignature =
|
||||
endpointOption.conversationSignature || response.conversationSignature;
|
||||
userMessage.conversationId = response.conversationId || conversationId;
|
||||
userMessage.invocationId = endpointOption.invocationId;
|
||||
userMessage.messageId = response.details.requestId || userMessageId;
|
||||
if (!overrideParentMessageId) await saveBingMessage({ oldMessageId: userMessageId, ...userMessage });
|
||||
|
||||
// STEP1 update the convosation
|
||||
// Bing API will not use our conversationId at the first time,
|
||||
// so change the placeholder conversationId to the real one.
|
||||
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
|
||||
// but in this situation, don't change the conversationId, but create new convo.
|
||||
if (conversationId != userMessage.conversationId && isNewConversation)
|
||||
if (conversationId != response.conversationId && isNewConversation)
|
||||
await saveConvo(req?.session?.user?.username, {
|
||||
conversationId: conversationId,
|
||||
newConversationId: userMessage.conversationId
|
||||
newConversationId: response.conversationId || conversationId
|
||||
});
|
||||
conversationId = userMessage.conversationId;
|
||||
conversationId = response.conversationId || conversationId;
|
||||
|
||||
// STEP2 update the user message
|
||||
userMessage.conversationSignature =
|
||||
endpointOption.conversationSignature || response.conversationSignature;
|
||||
userMessage.conversationId = conversationId;
|
||||
userMessage.invocationId = endpointOption.invocationId;
|
||||
userMessage.messageId = response.details.requestId || userMessageId;
|
||||
|
||||
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
||||
if (!overrideParentMessageId) {
|
||||
const oldUserMessageId = userMessageId;
|
||||
userMessageId = response.details.requestId;
|
||||
await saveBingMessage({ ...userMessage, messageId: oldUserMessageId, newMessageId: userMessageId });
|
||||
}
|
||||
|
||||
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
|
||||
// delete response.response;
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ const ask = async ({
|
|||
req,
|
||||
res
|
||||
}) => {
|
||||
const { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage;
|
||||
let { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage;
|
||||
|
||||
const client = askClient;
|
||||
|
||||
|
|
@ -107,23 +107,22 @@ const ask = async ({
|
|||
gptResponse.text = gptResponse.response;
|
||||
console.log('CLIENT RESPONSE', gptResponse);
|
||||
|
||||
if (!gptResponse.parentMessageId) {
|
||||
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
||||
if (gptResponse.parentMessageId) {
|
||||
// If gptResponse has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
||||
if (!overrideParentMessageId) {
|
||||
const oldUserMessageId = userMessageId;
|
||||
userMessageId = gptResponse.parentMessageId;
|
||||
userMessage.messageId = userMessageId;
|
||||
await saveMessage({ ...userMessage, messageId: oldUserMessageId, newMessageId: userMessageId });
|
||||
}
|
||||
} else {
|
||||
delete gptResponse.response;
|
||||
}
|
||||
|
||||
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
||||
gptResponse.sender = endpointOption?.chatGptLabel || 'ChatGPT';
|
||||
// gptResponse.model = model;
|
||||
gptResponse.text = await handleText(gptResponse);
|
||||
// if (convo.chatGptLabel?.length > 0 && model === 'chatgptCustom') {
|
||||
// gptResponse.chatGptLabel = convo.chatGptLabel;
|
||||
// }
|
||||
|
||||
// if (convo.promptPrefix?.length > 0 && model === 'chatgptCustom') {
|
||||
// gptResponse.promptPrefix = convo.promptPrefix;
|
||||
// }
|
||||
|
||||
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
||||
|
||||
await saveMessage(gptResponse);
|
||||
await updateConvo(req?.session?.user?.username, {
|
||||
|
|
|
|||
|
|
@ -29,10 +29,13 @@ router.post('/', async (req, res) => {
|
|||
});
|
||||
|
||||
router.post('/delete', async (req, res) => {
|
||||
const { arg } = req.body;
|
||||
let filter = {};
|
||||
const { presetId } = req.body.arg || {};
|
||||
|
||||
if (presetId) filter = { presetId };
|
||||
|
||||
try {
|
||||
await deletePresets(req?.session?.user?.username, arg);
|
||||
await deletePresets(req?.session?.user?.username, filter);
|
||||
|
||||
const presets = (await getPresets(req?.session?.user?.username)).map(preset => {
|
||||
return preset.toObject();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue