const db = require('../models'); const FileDBApi = require('./file'); const crypto = require('crypto'); const Utils = require('../utils'); const Sequelize = db.Sequelize; const Op = Sequelize.Op; function buildOwnedConversationWhere(currentUser) { if (!currentUser || !currentUser.id) { return {}; } return { userId: currentUser.id, }; } function mergeWhere(left, right) { const hasLeft = left && Object.keys(left).length; const hasRight = right && Object.keys(right).length; if (!hasLeft) { return right || {}; } if (!hasRight) { return left || {}; } return { [Op.and]: [left, right], }; } module.exports = class MessagesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const messages = await db.messages.create( { id: data.id || undefined, role: data.role || null , content: data.content || null , content_markdown: data.content_markdown || null , tool_name: data.tool_name || null , tool_call_json: data.tool_call_json || null , tool_result_json: data.tool_result_json || null , delivery_status: data.delivery_status || null , sent_at: data.sent_at || null , completed_at: data.completed_at || null , sequence: data.sequence || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await messages.setConversation( data.conversation || null, { transaction, }); await messages.setAuthor_user( data.author_user || null, { transaction, }); return messages; } static async bulkImport(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; // Prepare data - wrapping individual data transformations in a map() method const messagesData = data.map((item, index) => ({ id: item.id || undefined, role: item.role || null , content: item.content || null , content_markdown: item.content_markdown || null , tool_name: item.tool_name || null , tool_call_json: item.tool_call_json || null , tool_result_json: item.tool_result_json || null , delivery_status: item.delivery_status || null , sent_at: item.sent_at || null , completed_at: item.completed_at || null , sequence: item.sequence || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const messages = await db.messages.bulkCreate(messagesData, { transaction }); // For each item created, replace relation files return messages; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const messages = await db.messages.findOne({ where: { id, }, include: [ { model: db.conversations, as: 'conversation', where: buildOwnedConversationWhere(currentUser), required: true, }, ], transaction, }); if (!messages) { throw new Error('Message not found.'); } const updatePayload = {}; if (data.role !== undefined) updatePayload.role = data.role; if (data.content !== undefined) updatePayload.content = data.content; if (data.content_markdown !== undefined) updatePayload.content_markdown = data.content_markdown; if (data.tool_name !== undefined) updatePayload.tool_name = data.tool_name; if (data.tool_call_json !== undefined) updatePayload.tool_call_json = data.tool_call_json; if (data.tool_result_json !== undefined) updatePayload.tool_result_json = data.tool_result_json; if (data.delivery_status !== undefined) updatePayload.delivery_status = data.delivery_status; if (data.sent_at !== undefined) updatePayload.sent_at = data.sent_at; if (data.completed_at !== undefined) updatePayload.completed_at = data.completed_at; if (data.sequence !== undefined) updatePayload.sequence = data.sequence; updatePayload.updatedById = currentUser.id; await messages.update(updatePayload, {transaction}); if (data.conversation !== undefined) { await messages.setConversation( data.conversation, { transaction } ); } if (data.author_user !== undefined) { await messages.setAuthor_user( data.author_user, { transaction } ); } return messages; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const messages = await db.messages.findAll({ where: { id: { [Op.in]: ids, }, }, include: [ { model: db.conversations, as: 'conversation', where: buildOwnedConversationWhere(currentUser), required: true, }, ], transaction, }); if (messages.length !== ids.length) { throw new Error('One or more messages were not found.'); } await db.sequelize.transaction(async (transaction) => { for (const record of messages) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of messages) { await record.destroy({transaction}); } }); return messages; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const messages = await db.messages.findOne({ where: { id, }, include: [ { model: db.conversations, as: 'conversation', where: buildOwnedConversationWhere(currentUser), required: true, }, ], transaction, }); if (!messages) { throw new Error('Message not found.'); } await messages.update({ deletedBy: currentUser.id }, { transaction, }); await messages.destroy({ transaction }); return messages; } static async findBy(where, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const messages = await db.messages.findOne( { where, include: [ { model: db.conversations, as: 'conversation', where: buildOwnedConversationWhere(currentUser), required: true, }, ], transaction, }, ); if (!messages) { return messages; } const output = messages.get({plain: true}); output.attachments_message = await messages.getAttachments_message({ transaction }); output.usage_events_message = await messages.getUsage_events_message({ transaction }); output.conversation = await messages.getConversation({ transaction }); output.author_user = await messages.getAuthor_user({ transaction }); return output; } static async findAll( filter, options ) { const currentUser = (options && options.currentUser) || { id: null }; const limit = filter.limit || 0; let offset = 0; let where = {}; const currentPage = +filter.page; offset = currentPage * limit; const orderBy = null; const transaction = (options && options.transaction) || undefined; let include = [ { model: db.conversations, as: 'conversation', where: mergeWhere( filter.conversation ? { [Op.or]: [ { id: { [Op.in]: filter.conversation.split('|').map(term => Utils.uuid(term)) } }, { title: { [Op.or]: filter.conversation.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, buildOwnedConversationWhere(currentUser), ), required: true, }, { model: db.users, as: 'author_user', where: filter.author_user ? { [Op.or]: [ { id: { [Op.in]: filter.author_user.split('|').map(term => Utils.uuid(term)) } }, { firstName: { [Op.or]: filter.author_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.content) { where = { ...where, [Op.and]: Utils.ilike( 'messages', 'content', filter.content, ), }; } if (filter.content_markdown) { where = { ...where, [Op.and]: Utils.ilike( 'messages', 'content_markdown', filter.content_markdown, ), }; } if (filter.tool_name) { where = { ...where, [Op.and]: Utils.ilike( 'messages', 'tool_name', filter.tool_name, ), }; } if (filter.tool_call_json) { where = { ...where, [Op.and]: Utils.ilike( 'messages', 'tool_call_json', filter.tool_call_json, ), }; } if (filter.tool_result_json) { where = { ...where, [Op.and]: Utils.ilike( 'messages', 'tool_result_json', filter.tool_result_json, ), }; } if (filter.sent_atRange) { const [start, end] = filter.sent_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, sent_at: { ...where.sent_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, sent_at: { ...where.sent_at, [Op.lte]: end, }, }; } } if (filter.completed_atRange) { const [start, end] = filter.completed_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, completed_at: { ...where.completed_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, completed_at: { ...where.completed_at, [Op.lte]: end, }, }; } } if (filter.sequenceRange) { const [start, end] = filter.sequenceRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, sequence: { ...where.sequence, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, sequence: { ...where.sequence, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.role) { where = { ...where, role: filter.role, }; } if (filter.delivery_status) { where = { ...where, delivery_status: filter.delivery_status, }; } if (filter.createdAtRange) { const [start, end] = filter.createdAtRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, ['createdAt']: { ...where.createdAt, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, ['createdAt']: { ...where.createdAt, [Op.lte]: end, }, }; } } } const queryOptions = { where, include, distinct: true, order: filter.field && filter.sort ? [[filter.field, filter.sort]] : [['createdAt', 'desc']], transaction: options?.transaction, logging: console.log }; if (!options?.countOnly) { queryOptions.limit = limit ? Number(limit) : undefined; queryOptions.offset = offset ? Number(offset) : undefined; } try { const { rows, count } = await db.messages.findAndCountAll(queryOptions); return { rows: options?.countOnly ? [] : rows, count: count }; } catch (error) { console.error('Error executing query:', error); throw error; } } static async findAllAutocomplete(query, limit, offset, options) { const currentUser = (options && options.currentUser) || { id: null }; let where = {}; if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike( 'messages', 'content', query, ), ], }; } const records = await db.messages.findAll({ attributes: [ 'id', 'content' ], where, include: [ { model: db.conversations, as: 'conversation', where: buildOwnedConversationWhere(currentUser), required: true, attributes: [], }, ], limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['content', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.content, })); } };