LibreChat/api/models/Conversation.js

181 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-02-06 14:05:02 -05:00
const mongoose = require('mongoose');
2023-03-13 21:59:25 -04:00
const crypto = require('crypto');
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
},
parentMessageId: {
type: String,
required: true
},
title: {
type: String,
default: 'New Chat'
},
jailbreakConversationId: {
type: String
},
conversationSignature: {
type: String
},
clientId: {
type: String
},
invocationId: {
type: String
},
chatGptLabel: {
type: String
},
promptPrefix: {
type: String
},
model: {
type: String
},
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
const Conversation =
mongoose.models.Conversation || mongoose.model('Conversation', convoSchema);
const getConvo = async (conversationId) => {
try {
return await Conversation.findOne({ conversationId }).exec();
} catch (error) {
console.log(error);
return { message: 'Error getting single conversation' };
}
};
2023-02-06 14:05:02 -05:00
module.exports = {
saveConvo: async ({ 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;
}
if (newConversationId) {
update.conversationId = newConversationId;
}
if (!update.jailbreakConversationId)
update.jailbreakConversationId = null
2023-02-06 14:05:02 -05:00
return await Conversation.findOneAndUpdate(
{ conversationId },
{ $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 ({ conversationId, ...update }) => {
try {
2023-02-19 21:06:21 -05:00
return await Conversation.findOneAndUpdate({ conversationId }, update, {
new: true
}).exec();
} catch (error) {
console.log(error);
return { message: 'Error updating conversation' };
}
},
// getConvos: async () => await Conversation.find({}).sort({ createdAt: -1 }).exec(),
2023-03-15 04:05:14 +08:00
getConvosByPage: async (pageNumber = 1, pageSize = 12) => {
try {
const totalConvos = (await Conversation.countDocuments()) || 1;
2023-03-15 04:05:14 +08:00
const totalPages = Math.ceil(totalConvos / pageSize);
const convos = await Conversation.find()
.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' };
}
},
getConvo,
getConvoTitle: async (conversationId) => {
try {
const convo = await getConvo(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 (filter) => {
let deleteCount = await Conversation.deleteMany(filter).exec();
deleteCount.messages = await deleteMessages(filter);
return deleteCount;
2023-03-13 21:59:25 -04:00
},
migrateDb: async () => {
try {
const conversations = await Conversation.find({ model: null }).exec();
if (!conversations || conversations.length === 0)
return { message: '[Migrate] No conversations to migrate' };
2023-03-13 21:59:25 -04:00
for (let convo of conversations) {
const messages = await getMessages({
conversationId: convo.conversationId,
messageId: { $exists: false }
});
let model;
let oldId;
const promises = [];
2023-03-13 21:59:25 -04:00
messages.forEach((message, i) => {
const msgObj = message.toObject();
const newId = msgObj.id;
if (i === 0) {
message.parentMessageId = '00000000-0000-0000-0000-000000000000';
} else {
message.parentMessageId = oldId;
}
oldId = newId;
2023-03-13 21:59:25 -04:00
message.messageId = newId;
if (message.sender.toLowerCase() !== 'user' && !model) {
model = message.sender.toLowerCase();
}
if (message.sender.toLowerCase() === 'user') {
message.isCreatedByUser = true;
}
promises.push(message.save());
});
await Promise.all(promises);
await Conversation.findOneAndUpdate(
{ conversationId: convo.conversationId },
{ model },
2023-03-13 21:59:25 -04:00
{ new: true }
).exec();
}
try {
await mongoose.connection.db.collection('messages').dropIndex('id_1');
} catch (error) {
console.log("[Migrate] Index doesn't exist or already dropped");
2023-03-13 21:59:25 -04:00
}
} catch (error) {
console.log(error);
return { message: '[Migrate] Error migrating conversations' };
2023-03-13 21:59:25 -04:00
}
}
2023-02-06 14:05:02 -05:00
};