LibreChat/api/models/Conversation.js

174 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-02-06 14:05:02 -05:00
const mongoose = require('mongoose');
const mongoMeili = require('../lib/db/mongoMeili');
const { getMessages, deleteMessages } = require('./Message');
2023-02-06 14:05:02 -05:00
2023-03-13 21:59:25 -04:00
const convoSchema = mongoose.Schema(
{
conversationId: {
type: String,
unique: true,
required: true,
index: true,
meiliIndex: true
2023-03-13 21:59:25 -04:00
},
parentMessageId: {
type: String,
required: true
},
title: {
type: String,
default: 'New Chat',
meiliIndex: true
2023-03-13 21:59:25 -04:00
},
jailbreakConversationId: {
type: String,
default: null
2023-03-13 21:59:25 -04:00
},
conversationSignature: {
type: String,
default: null
2023-03-13 21:59:25 -04:00
},
clientId: {
type: String
},
invocationId: {
type: String
},
chatGptLabel: {
type: String,
default: null
2023-03-13 21:59:25 -04:00
},
promptPrefix: {
type: String,
default: null
2023-03-13 21:59:25 -04:00
},
model: {
type: String,
required: true
2023-03-13 21:59:25 -04:00
},
user: {
type: String
},
2023-03-13 21:59:25 -04:00
suggestions: [{ type: String }],
messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }]
2023-02-06 14:05:02 -05:00
},
2023-03-13 21:59:25 -04:00
{ timestamps: true }
);
2023-02-06 14:05:02 -05:00
convoSchema.plugin(mongoMeili, {
host: process.env.MEILI_HOST,
apiKey: process.env.MEILI_KEY,
indexName: 'convos', // Will get created automatically if it doesn't exist already
primaryKey: 'conversationId',
});
2023-02-06 14:05:02 -05:00
const Conversation =
mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
const getConvo = async (user, conversationId) => {
try {
return await Conversation.findOne({ user, conversationId }).exec();
} catch (error) {
console.log(error);
return { message: 'Error getting single conversation' };
}
};
2023-02-06 14:05:02 -05:00
module.exports = {
2023-03-17 19:58:13 -04:00
Conversation,
saveConvo: async (user, { conversationId, newConversationId, title, ...convo }) => {
try {
const messages = await getMessages({ conversationId });
2023-02-19 21:06:21 -05:00
const update = { ...convo, messages };
if (title) {
update.title = title;
2023-03-17 19:58:13 -04:00
update.user = user;
}
if (newConversationId) {
update.conversationId = newConversationId;
}
if (!update.jailbreakConversationId) {
update.jailbreakConversationId = null;
}
if (update.model !== 'chatgptCustom' && update.chatGptLabel && update.promptPrefix) {
console.log('Validation error: resetting chatgptCustom fields', update);
update.chatGptLabel = null;
update.promptPrefix = null;
}
2023-02-06 14:05:02 -05:00
return await Conversation.findOneAndUpdate(
2023-03-16 13:32:04 -04:00
{ conversationId: conversationId, user },
{ $set: update },
{ new: true, upsert: true }
).exec();
} catch (error) {
console.log(error);
return { message: 'Error saving conversation' };
}
2023-02-06 15:17:54 -05:00
},
updateConvo: async (user, { conversationId, ...update }) => {
try {
2023-03-17 19:58:13 -04:00
return await Conversation.findOneAndUpdate(
{ conversationId: conversationId, user },
update,
{
new: true
}
).exec();
} catch (error) {
console.log(error);
return { message: 'Error updating conversation' };
}
},
getConvosByPage: async (user, pageNumber = 1, pageSize = 12) => {
try {
2023-03-16 13:32:04 -04:00
const totalConvos = (await Conversation.countDocuments({ user })) || 1;
2023-03-15 04:05:14 +08:00
const totalPages = Math.ceil(totalConvos / pageSize);
2023-03-16 13:32:04 -04:00
const convos = await Conversation.find({ user })
.sort({ createdAt: -1, created: -1 })
2023-03-15 04:05:14 +08:00
.skip((pageNumber - 1) * pageSize)
.limit(pageSize)
.exec();
2023-03-05 14:41:50 -05:00
2023-03-15 04:05:14 +08:00
return { conversations: convos, pages: totalPages, pageNumber, pageSize };
} catch (error) {
console.log(error);
return { message: 'Error getting conversations' };
}
},
getConvosQueried: async (user, convoIds, pageNumber = 1, pageSize = 12) => {
try {
if (!convoIds || convoIds.length === 0) {
return { conversations: [], pages: 1, pageNumber, pageSize };
}
const promises = convoIds.map(convo => {
return Conversation.findOne({ user, conversationId: convo.conversationId}).exec();
});
const results = await Promise.all(promises);
const startIndex = (pageNumber - 1) * pageSize;
const convos = results.slice(startIndex, startIndex + pageSize);
const totalPages = Math.ceil(results.length / pageSize);
return { conversations: convos, pages: totalPages, pageNumber, pageSize };
} catch (error) {
console.log(error);
return { message: 'Error fetching conversations' };
}
},
getConvo,
getConvoTitle: async (user, conversationId) => {
try {
const convo = await getConvo(user, conversationId);
return convo.title;
} catch (error) {
console.log(error);
return { message: 'Error getting conversation title' };
}
2023-03-05 14:41:50 -05:00
},
deleteConvos: async (user, filter) => {
2023-03-17 19:58:13 -04:00
let deleteCount = await Conversation.deleteMany({ ...filter, user }).exec();
deleteCount.messages = await deleteMessages(filter);
return deleteCount;
}
2023-02-06 14:05:02 -05:00
};