LibreChat/api/models/Conversation.js

119 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-02-06 14:05:02 -05:00
const mongoose = require('mongoose');
const { getMessages, deleteMessages } = require('./Message');
2023-02-06 14:05:02 -05:00
const convoSchema = mongoose.Schema({
conversationId: {
type: String,
unique: true,
required: true
},
parentMessageId: {
type: String,
required: true
},
title: {
type: String,
default: 'New Chat'
},
2023-03-08 19:47:23 -05:00
jailbreakConversationId: {
type: String
},
conversationSignature: {
2023-02-19 21:06:21 -05:00
type: String
},
clientId: {
2023-02-19 21:06:21 -05:00
type: String
},
invocationId: {
2023-02-19 21:06:21 -05:00
type: String
},
chatGptLabel: {
type: String
},
promptPrefix: {
type: String
},
2023-02-19 21:06:21 -05:00
model: {
type: String
},
2023-02-19 21:06:21 -05:00
suggestions: [{ type: String }],
2023-02-06 14:05:02 -05:00
messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }],
}, { 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 = {
2023-02-19 21:06:21 -05:00
saveConvo: async ({ conversationId, 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-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-05 14:41:50 -05:00
getConvos: async (pageNumber = 1, pageSize = 12) => {
try {
const skip = (pageNumber - 1) * pageSize;
// const limit = pageNumber * pageSize;
2023-03-05 14:41:50 -05:00
const conversations = await Conversation.find({})
.sort({ createdAt: -1 })
.skip(skip)
// .limit(limit)
.limit(pageSize)
.exec();
2023-03-05 14:41:50 -05:00
return conversations;
} 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-02-06 14:05:02 -05:00
};