mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 00:40:14 +01:00
feat(api): add support for saving messages to database
fix(api): change arrowParens prettier option to always fix(api): update addToCache to include endpointOption and latestMessage fix(api): update askOpenAI to include endpointOption in abortControllers fix(client): remove abortKey state and add currentParent state to MessageHandler
This commit is contained in:
parent
6246ffff1e
commit
a81bd27b39
5 changed files with 78 additions and 32 deletions
|
|
@ -1,31 +1,66 @@
|
|||
const Keyv = require('keyv');
|
||||
const { KeyvFile } = require('keyv-file');
|
||||
const crypto = require('crypto');
|
||||
const { saveMessage } = require('../../../models');
|
||||
|
||||
const addToCache = async ( { conversationId, parentMessageId }) => {
|
||||
const conversationsCache = new Keyv({
|
||||
store: new KeyvFile({ filename: './data/cache.json' })
|
||||
});
|
||||
const addToCache = async ({
|
||||
endpointOption,
|
||||
conversationId,
|
||||
userMessage,
|
||||
latestMessage,
|
||||
parentMessageId
|
||||
}) => {
|
||||
try {
|
||||
const conversationsCache = new Keyv({
|
||||
store: new KeyvFile({ filename: './data/cache.json' }),
|
||||
namespace: 'chatgpt', // should be 'bing' for bing/sydney
|
||||
});
|
||||
|
||||
let conversation = await conversationsCache.get(conversationId);
|
||||
let isNewConversation = false;
|
||||
if (!conversation) {
|
||||
conversation = {
|
||||
messages: [],
|
||||
createdAt: Date.now()
|
||||
let conversation = await conversationsCache.get(conversationId);
|
||||
// used to generate a title for the conversation if none exists
|
||||
// let isNewConversation = false;
|
||||
if (!conversation) {
|
||||
conversation = {
|
||||
messages: [],
|
||||
createdAt: Date.now()
|
||||
};
|
||||
// isNewConversation = true;
|
||||
}
|
||||
|
||||
// const shouldGenerateTitle = opts.shouldGenerateTitle && isNewConversation;
|
||||
|
||||
const roles = (options) => {
|
||||
const { endpoint } = options;
|
||||
if (endpoint === 'openAI') {
|
||||
return options?.chatGptLabel || 'ChatGPT';
|
||||
} else if (endpoint === 'bingAI') {
|
||||
return options?.jailbreak ? 'Sydney' : 'BingAI';
|
||||
}
|
||||
};
|
||||
isNewConversation = true;
|
||||
|
||||
const messageId = crypto.randomUUID();
|
||||
|
||||
let responseMessage = {
|
||||
id: messageId,
|
||||
parentMessageId,
|
||||
role: roles(endpointOption),
|
||||
message: latestMessage
|
||||
};
|
||||
|
||||
await saveMessage({
|
||||
...responseMessage,
|
||||
conversationId,
|
||||
messageId,
|
||||
sender: responseMessage.role,
|
||||
text: latestMessage
|
||||
});
|
||||
|
||||
conversation.messages.push(userMessage, responseMessage);
|
||||
|
||||
await conversationsCache.set(conversationId, conversation);
|
||||
} catch (error) {
|
||||
console.error('Trouble adding to cache', error);
|
||||
}
|
||||
|
||||
// const shouldGenerateTitle = opts.shouldGenerateTitle && isNewConversation;
|
||||
|
||||
const userMessage = {
|
||||
id: crypto.randomUUID(),
|
||||
parentMessageId,
|
||||
role: 'User',
|
||||
message
|
||||
};
|
||||
conversation.messages.push(userMessage);
|
||||
};
|
||||
|
||||
module.exports = { addToCache };
|
||||
module.exports = addToCache;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue