mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
feat: the first message will have a parentMessageId as 00000000-0000-0000-0000-000000000000 (in order not to create new convo when resubmit) feat: ask will return the userMessage as well, to send back the messageId TODO: comment out the title generation. TODO: bing version need to be test fix: never use the same messageId fix: never delete exist messages fix: connect response.parentMessageId to the userMessage.messageId fix: set default convo title as new Chat
87 lines
No EOL
2.1 KiB
JavaScript
87 lines
No EOL
2.1 KiB
JavaScript
const mongoose = require('mongoose');
|
|
|
|
const messageSchema = mongoose.Schema({
|
|
messageId: {
|
|
type: String,
|
|
unique: true,
|
|
required: true
|
|
},
|
|
conversationId: {
|
|
type: String,
|
|
required: true
|
|
},
|
|
conversationSignature: {
|
|
type: String,
|
|
// required: true
|
|
},
|
|
clientId: {
|
|
type: String,
|
|
},
|
|
invocationId: {
|
|
type: String,
|
|
},
|
|
parentMessageId: {
|
|
type: String,
|
|
// required: true
|
|
},
|
|
sender: {
|
|
type: String,
|
|
required: true
|
|
},
|
|
text: {
|
|
type: String,
|
|
required: true
|
|
},
|
|
isCreatedByUser: {
|
|
type: Boolean,
|
|
required: true,
|
|
default: false
|
|
}
|
|
}, { timestamps: true });
|
|
|
|
const Message = mongoose.models.Message || mongoose.model('Message', messageSchema);
|
|
|
|
module.exports = {
|
|
saveMessage: async ({ messageId, conversationId, parentMessageId, sender, text, isCreatedByUser=false }) => {
|
|
try {
|
|
await Message.findOneAndUpdate({ messageId }, {
|
|
conversationId,
|
|
parentMessageId,
|
|
sender,
|
|
text,
|
|
isCreatedByUser
|
|
}, { upsert: true, new: true });
|
|
return { messageId, conversationId, parentMessageId, sender, text, isCreatedByUser };
|
|
} catch (error) {
|
|
console.error(error);
|
|
return { message: 'Error saving message' };
|
|
}
|
|
},
|
|
deleteMessagesSince: async ({ messageId, conversationId }) => {
|
|
try {
|
|
message = await Message.findOne({ messageId }).exec()
|
|
|
|
if (message)
|
|
return await Message.find({ conversationId }).deleteMany({ createdAt: { $gt: message.createdAt } }).exec();
|
|
} catch (error) {
|
|
console.error(error);
|
|
return { message: 'Error deleting messages' };
|
|
}
|
|
},
|
|
getMessages: async (filter) => {
|
|
try {
|
|
return await Message.find(filter).sort({createdAt: 1}).exec()
|
|
} catch (error) {
|
|
console.error(error);
|
|
return { message: 'Error getting messages' };
|
|
}
|
|
},
|
|
deleteMessages: async (filter) => {
|
|
try {
|
|
return await Message.deleteMany(filter).exec()
|
|
} catch (error) {
|
|
console.error(error);
|
|
return { message: 'Error deleting messages' };
|
|
}
|
|
}
|
|
} |