diff --git a/api/models/Config.js b/api/models/Config.js index 57192a461e..bee45603ff 100644 --- a/api/models/Config.js +++ b/api/models/Config.js @@ -49,16 +49,16 @@ const configSchema = mongoose.Schema( ); // Instance method -ConfigSchema.methods.incrementCount = function () { +configSchema.methods.incrementCount = function () { this.startupCounts += 1; }; // Static methods -ConfigSchema.statics.findByTag = async function (tag) { +configSchema.statics.findByTag = async function (tag) { return await this.findOne({ tag }); }; -ConfigSchema.statics.updateByTag = async function (tag, update) { +configSchema.statics.updateByTag = async function (tag, update) { return await this.findOneAndUpdate({ tag }, update, { new: true }); }; diff --git a/api/models/Conversation.js b/api/models/Conversation.js index bc25b3f57d..953e42528a 100644 --- a/api/models/Conversation.js +++ b/api/models/Conversation.js @@ -1,70 +1,6 @@ -const mongoose = require('mongoose'); -const mongoMeili = require('../lib/db/mongoMeili'); +const { Conversation } = require('./schema/'); const { getMessages, deleteMessages } = require('./Message'); -const convoSchema = mongoose.Schema( - { - conversationId: { - type: String, - unique: true, - required: true, - index: true, - meiliIndex: true - }, - parentMessageId: { - type: String, - required: true - }, - title: { - type: String, - default: 'New Chat', - meiliIndex: true - }, - jailbreakConversationId: { - type: String, - default: null - }, - conversationSignature: { - type: String, - default: null - }, - clientId: { - type: String - }, - invocationId: { - type: String - }, - chatGptLabel: { - type: String, - default: null - }, - promptPrefix: { - type: String, - default: null - }, - model: { - type: String, - required: true - }, - user: { - type: String - }, - suggestions: [{ type: String }], - messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }] - }, - { timestamps: true } -); - -// 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' -// }); - -const Conversation = - mongoose.models.Conversation || mongoose.model('Conversation', convoSchema); - const getConvo = async (user, conversationId) => { try { return await Conversation.findOne({ user, conversationId }).exec(); @@ -147,7 +83,7 @@ module.exports = { // will handle a syncing solution soon const deletedConvoIds = []; - convoIds.forEach((convo) => + convoIds.forEach(convo => promises.push( Conversation.findOne({ user, @@ -182,7 +118,7 @@ module.exports = { pageNumber, pageSize, // will handle a syncing solution soon - filter: new Set(deletedConvoIds), + filter: new Set(deletedConvoIds) }; } catch (error) { console.log(error); diff --git a/api/models/Message.js b/api/models/Message.js index cb6c531291..b2422a4c4e 100644 --- a/api/models/Message.js +++ b/api/models/Message.js @@ -1,71 +1,5 @@ -const mongoose = require('mongoose'); -const mongoMeili = require('../lib/db/mongoMeili'); - -const messageSchema = mongoose.Schema({ - messageId: { - type: String, - unique: true, - required: true, - index: true, - meiliIndex: true - }, - conversationId: { - type: String, - required: true, - meiliIndex: true - }, - conversationSignature: { - type: String, - // required: true - }, - clientId: { - type: String, - }, - invocationId: { - type: String, - }, - parentMessageId: { - type: String, - // required: true - }, - sender: { - type: String, - required: true, - meiliIndex: true - }, - text: { - type: String, - required: true, - meiliIndex: true - }, - isCreatedByUser: { - type: Boolean, - required: true, - default: false - }, - error: { - type: Boolean, - default: false - }, - _meiliIndex: { - type: Boolean, - required: false, - select: false, - default: false - } -}, { timestamps: true }); - -// messageSchema.plugin(mongoMeili, { -// host: process.env.MEILI_HOST, -// apiKey: process.env.MEILI_KEY, -// indexName: 'messages', // Will get created automatically if it doesn't exist already -// primaryKey: 'messageId', -// }); - -const Message = mongoose.models.Message || mongoose.model('Message', messageSchema); - +const { Message } = require('./schema/'); module.exports = { - messageSchema, Message, saveMessage: async ({ messageId, conversationId, parentMessageId, sender, text, isCreatedByUser=false, error }) => { try { diff --git a/api/models/schema/convoSchema.js b/api/models/schema/convoSchema.js new file mode 100644 index 0000000000..01f85e3b15 --- /dev/null +++ b/api/models/schema/convoSchema.js @@ -0,0 +1,53 @@ +const mongoose = require('mongoose'); +module.exports = mongoose.Schema( + { + conversationId: { + type: String, + unique: true, + required: true, + index: true, + meiliIndex: true + }, + parentMessageId: { + type: String, + required: true + }, + title: { + type: String, + default: 'New Chat', + meiliIndex: true + }, + jailbreakConversationId: { + type: String, + default: null + }, + conversationSignature: { + type: String, + default: null + }, + clientId: { + type: String + }, + invocationId: { + type: String + }, + chatGptLabel: { + type: String, + default: null + }, + promptPrefix: { + type: String, + default: null + }, + model: { + type: String, + required: true + }, + user: { + type: String + }, + suggestions: [{ type: String }], + messages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Message' }] + }, + { timestamps: true } +); \ No newline at end of file diff --git a/api/models/schema/index.js b/api/models/schema/index.js new file mode 100644 index 0000000000..53efd98616 --- /dev/null +++ b/api/models/schema/index.js @@ -0,0 +1,46 @@ +const mongoose = require('mongoose'); +const convoSchema = require('./convoSchema'); +const messageSchema = require('./messageSchema'); +const { MeiliSearch } = require('meilisearch'); +const mongoMeili = require('../../lib/db/mongoMeili'); + +(async () => { + try { + const client = new MeiliSearch({ + host: process.env.MEILI_HOST, + apiKey: process.env.MEILI_KEY + }); + + const { status } = await client.health(); + console.log(`Meilisearch: ${status}`); + const result = status === 'available' && !!process.env.SEARCH; + + if (!result) { + throw new Error('Meilisearch not available'); + } + + 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' + }); + + messageSchema.plugin(mongoMeili, { + host: process.env.MEILI_HOST, + apiKey: process.env.MEILI_KEY, + indexName: 'messages', + primaryKey: 'messageId' + }); + + } catch (error) { + console.log('Meilisearch error, search will be disabled'); + console.error(error); + } +})(); + +const Conversation = +mongoose.models.Conversation || mongoose.model('Conversation', convoSchema); +const Message = mongoose.models.Message || mongoose.model('Message', messageSchema); + +module.exports = { Conversation, Message }; diff --git a/api/models/schema/messageSchema.js b/api/models/schema/messageSchema.js new file mode 100644 index 0000000000..3a2214d314 --- /dev/null +++ b/api/models/schema/messageSchema.js @@ -0,0 +1,54 @@ +const mongoose = require('mongoose'); +module.exports = mongoose.Schema({ + messageId: { + type: String, + unique: true, + required: true, + index: true, + meiliIndex: true + }, + conversationId: { + type: String, + required: true, + meiliIndex: true + }, + conversationSignature: { + type: String, + // required: true + }, + clientId: { + type: String, + }, + invocationId: { + type: String, + }, + parentMessageId: { + type: String, + // required: true + }, + sender: { + type: String, + required: true, + meiliIndex: true + }, + text: { + type: String, + required: true, + meiliIndex: true + }, + isCreatedByUser: { + type: Boolean, + required: true, + default: false + }, + error: { + type: Boolean, + default: false + }, + _meiliIndex: { + type: Boolean, + required: false, + select: false, + default: false + } +}, { timestamps: true }); \ No newline at end of file diff --git a/api/server/routes/search.js b/api/server/routes/search.js index 25515325c4..8122ad4939 100644 --- a/api/server/routes/search.js +++ b/api/server/routes/search.js @@ -54,7 +54,7 @@ router.get('/', async function (req, res) { const result = await getConvosQueried(user, sortedHits, pageNumber); cache.set(q, result.cache); delete result.cache; - result.messages = messages.filter(message => !result.filter.has(message.conversationId)); + result.messages = messages.filter((message) => !result.filter.has(message.conversationId)); // console.log(result, messages.length); res.status(200).send(result); } catch (error) { @@ -81,17 +81,17 @@ router.get('/test', async function (req, res) { router.get('/enable', async function (req, res) { let result = false; - const client = new MeiliSearch({ - host: process.env.MEILI_HOST, - apiKey: process.env.MEILI_KEY - }); - try { + const client = new MeiliSearch({ + host: process.env.MEILI_HOST, + apiKey: process.env.MEILI_KEY + }); + const { status } = await client.health(); console.log(`Meilisearch: ${status}`); result = status === 'available' && !!process.env.SEARCH; return res.send(result); - } catch(error) { + } catch (error) { console.error(error); return res.send(false); }