const { CacheKeys } = require('librechat-data-provider');
const { loadDefaultModels, loadConfigModels } = require('~/server/services/Config');
const { getLogStores } = require('~/cache');

async function modelController(req, res) {
  const cache = getLogStores(CacheKeys.CONFIG_STORE);
  const cachedModelsConfig = await cache.get(CacheKeys.MODELS_CONFIG);
  if (cachedModelsConfig) {
    res.send(cachedModelsConfig);
    return;
  }
  const defaultModelsConfig = await loadDefaultModels();
  const customModelsConfig = await loadConfigModels();

  const modelConfig = { ...defaultModelsConfig, ...customModelsConfig };

  await cache.set(CacheKeys.MODELS_CONFIG, modelConfig);
  res.send(modelConfig);
}

module.exports = modelController;
