const db = require('../models'); const FileDBApi = require('./file'); const crypto = require('crypto'); const Utils = require('../utils'); const Sequelize = db.Sequelize; const Op = Sequelize.Op; module.exports = class Model_catalogDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const model_catalog = await db.model_catalog.create( { id: data.id || undefined, model_name: data.model_name || null , provider: data.provider || null , capability: data.capability || null , context_window: data.context_window || null , is_available: data.is_available || false , discovered_at: data.discovered_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await model_catalog.setEndpoint( data.endpoint || null, { transaction, }); return model_catalog; } 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 model_catalogData = data.map((item, index) => ({ id: item.id || undefined, model_name: item.model_name || null , provider: item.provider || null , capability: item.capability || null , context_window: item.context_window || null , is_available: item.is_available || false , discovered_at: item.discovered_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const model_catalog = await db.model_catalog.bulkCreate(model_catalogData, { transaction }); // For each item created, replace relation files return model_catalog; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const model_catalog = await db.model_catalog.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.model_name !== undefined) updatePayload.model_name = data.model_name; if (data.provider !== undefined) updatePayload.provider = data.provider; if (data.capability !== undefined) updatePayload.capability = data.capability; if (data.context_window !== undefined) updatePayload.context_window = data.context_window; if (data.is_available !== undefined) updatePayload.is_available = data.is_available; if (data.discovered_at !== undefined) updatePayload.discovered_at = data.discovered_at; updatePayload.updatedById = currentUser.id; await model_catalog.update(updatePayload, {transaction}); if (data.endpoint !== undefined) { await model_catalog.setEndpoint( data.endpoint, { transaction } ); } return model_catalog; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const model_catalog = await db.model_catalog.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of model_catalog) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of model_catalog) { await record.destroy({transaction}); } }); return model_catalog; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const model_catalog = await db.model_catalog.findByPk(id, options); await model_catalog.update({ deletedBy: currentUser.id }, { transaction, }); await model_catalog.destroy({ transaction }); return model_catalog; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const model_catalog = await db.model_catalog.findOne( { where }, { transaction }, ); if (!model_catalog) { return model_catalog; } const output = model_catalog.get({plain: true}); output.curl_templates_model = await model_catalog.getCurl_templates_model({ transaction }); output.llm_requests_model = await model_catalog.getLlm_requests_model({ transaction }); output.endpoint = await model_catalog.getEndpoint({ transaction }); return output; } static async findAll( filter, options ) { 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.llm_endpoints, as: 'endpoint', where: filter.endpoint ? { [Op.or]: [ { id: { [Op.in]: filter.endpoint.split('|').map(term => Utils.uuid(term)) } }, { endpoint_name: { [Op.or]: filter.endpoint.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.model_name) { where = { ...where, [Op.and]: Utils.ilike( 'model_catalog', 'model_name', filter.model_name, ), }; } if (filter.provider) { where = { ...where, [Op.and]: Utils.ilike( 'model_catalog', 'provider', filter.provider, ), }; } if (filter.context_windowRange) { const [start, end] = filter.context_windowRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, context_window: { ...where.context_window, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, context_window: { ...where.context_window, [Op.lte]: end, }, }; } } if (filter.discovered_atRange) { const [start, end] = filter.discovered_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, discovered_at: { ...where.discovered_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, discovered_at: { ...where.discovered_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.capability) { where = { ...where, capability: filter.capability, }; } if (filter.is_available) { where = { ...where, is_available: filter.is_available, }; } 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.model_catalog.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, ) { let where = {}; if (query) { where = { [Op.or]: [ { ['id']: Utils.uuid(query) }, Utils.ilike( 'model_catalog', 'model_name', query, ), ], }; } const records = await db.model_catalog.findAll({ attributes: [ 'id', 'model_name' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['model_name', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.model_name, })); } };