2023-02-06 14:05:02 -05:00
|
|
|
const mongoose = require('mongoose');
|
2023-03-13 21:59:25 -04:00
|
|
|
const crypto = require('crypto');
|
2023-02-07 16:22:35 -05:00
|
|
|
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);
|
|
|
|
|
|
2023-03-08 22:30:29 -05:00
|
|
|
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-03-15 00:54:50 +08:00
|
|
|
saveConvo: async ({ conversationId, newConversationId, title, ...convo }) => {
|
2023-02-08 00:02:29 -05:00
|
|
|
try {
|
|
|
|
|
const messages = await getMessages({ conversationId });
|
2023-02-19 21:06:21 -05:00
|
|
|
const update = { ...convo, messages };
|
2023-02-08 00:02:29 -05:00
|
|
|
if (title) {
|
|
|
|
|
update.title = title;
|
|
|
|
|
}
|
2023-03-15 00:54:50 +08:00
|
|
|
if (newConversationId) {
|
|
|
|
|
update.conversationId = newConversationId;
|
|
|
|
|
}
|
2023-03-15 00:47:17 +08:00
|
|
|
if (!update.jailbreakConversationId)
|
|
|
|
|
update.jailbreakConversationId = null
|
2023-02-06 14:05:02 -05:00
|
|
|
|
2023-02-08 00:02:29 -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
|
|
|
},
|
2023-02-11 10:22:15 -05:00
|
|
|
updateConvo: async ({ conversationId, ...update }) => {
|
|
|
|
|
try {
|
2023-02-19 21:06:21 -05:00
|
|
|
return await Conversation.findOneAndUpdate({ conversationId }, update, {
|
|
|
|
|
new: true
|
|
|
|
|
}).exec();
|
2023-02-11 10:22:15 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return { message: 'Error updating conversation' };
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-13 05:20:35 +08:00
|
|
|
// getConvos: async () => await Conversation.find({}).sort({ createdAt: -1 }).exec(),
|
2023-03-05 14:41:50 -05:00
|
|
|
getConvos: async (pageNumber = 1, pageSize = 12) => {
|
2023-03-07 14:22:33 -05:00
|
|
|
try {
|
|
|
|
|
const skip = (pageNumber - 1) * pageSize;
|
|
|
|
|
// const limit = pageNumber * pageSize;
|
2023-03-05 14:41:50 -05:00
|
|
|
|
2023-03-07 14:22:33 -05:00
|
|
|
const conversations = await Conversation.find({})
|
2023-03-13 05:20:35 +08:00
|
|
|
.sort({ createdAt: -1 })
|
2023-03-07 14:22:33 -05:00
|
|
|
.skip(skip)
|
|
|
|
|
// .limit(limit)
|
|
|
|
|
.limit(pageSize)
|
|
|
|
|
.exec();
|
2023-03-05 14:41:50 -05:00
|
|
|
|
2023-03-07 14:22:33 -05:00
|
|
|
return conversations;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return { message: 'Error getting conversations' };
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-08 22:30:29 -05:00
|
|
|
getConvo,
|
|
|
|
|
getConvoTitle: async (conversationId) => {
|
2023-03-07 14:22:33 -05:00
|
|
|
try {
|
2023-03-08 22:30:29 -05:00
|
|
|
const convo = await getConvo(conversationId);
|
|
|
|
|
return convo.title;
|
2023-03-07 14:22:33 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
2023-03-08 22:30:29 -05:00
|
|
|
return { message: 'Error getting conversation title' };
|
2023-03-07 14:22:33 -05:00
|
|
|
}
|
2023-03-05 14:41:50 -05:00
|
|
|
},
|
2023-02-07 16:22:35 -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: 'No conversations to migrate' };
|
|
|
|
|
|
|
|
|
|
for (let convo of conversations) {
|
|
|
|
|
const messages = await getMessages({
|
|
|
|
|
conversationId: convo.conversationId,
|
|
|
|
|
messageId: { $exists: false }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const promises = [];
|
|
|
|
|
let model;
|
|
|
|
|
let oldId;
|
|
|
|
|
messages.forEach((message, i) => {
|
|
|
|
|
const msgObj = message.toObject();
|
|
|
|
|
const newId = msgObj.id;
|
|
|
|
|
if (i === 0) {
|
|
|
|
|
message.parentMessageId = '00000000-0000-0000-0000-000000000000';
|
|
|
|
|
oldId = newId;
|
|
|
|
|
} else {
|
|
|
|
|
message.parentMessageId = oldId;
|
|
|
|
|
oldId = newId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
message.messageId = newId;
|
|
|
|
|
message.createdAt = message.created;
|
|
|
|
|
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, createdAt: convo.created },
|
|
|
|
|
{ new: true }
|
|
|
|
|
).exec();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await mongoose.connection.db.collection('messages').dropIndex('id_1');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log("Index doesn't exist or already dropped");
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return { message: 'Error migrating conversations' };
|
|
|
|
|
}
|
2023-02-07 16:22:35 -05:00
|
|
|
}
|
2023-02-06 14:05:02 -05:00
|
|
|
};
|