LibreChat/api/server/services/Files/Audio/getCustomConfigSpeech.js
Marco Beretta 62881fee54
🔧 fix: handle missing custom config speech (#3790)
* feat: Update speech settings retrieval logic to handle missing custom configuration

This commit updates the logic in the Speech component and the getCustomConfigSpeech function to handle the case where the custom configuration is missing. Previously, if no custom configuration was found, an error would occur. Now, the code checks for the presence of the custom configuration and returns a message indicating that no custom configuration was found. This improves the robustness of the application and provides a better user experience.

* refactor: changed response message when no custom config is found
2024-08-27 06:09:04 -04:00

66 lines
1.9 KiB
JavaScript

const getCustomConfig = require('~/server/services/Config/getCustomConfig');
const { logger } = require('~/config');
/**
* This function retrieves the speechTab settings from the custom configuration
* It first fetches the custom configuration
* Then, it checks if the custom configuration and the speechTab schema exist
* If they do, it sends the speechTab settings as a JSON response
* If they don't, it throws an error
*
* @param {Object} req - The request object
* @param {Object} res - The response object
* @returns {Promise<void>}
* @throws {Error} - If the custom configuration or the speechTab schema is missing, an error is thrown
*/
async function getCustomConfigSpeech(req, res) {
try {
const customConfig = await getCustomConfig();
if (!customConfig) {
return res.status(200).send({
message: 'not_found',
});
}
const sttExternal = !!customConfig.speech?.stt;
const ttsExternal = !!customConfig.speech?.tts;
let settings = {
sttExternal,
ttsExternal,
};
if (!customConfig.speech?.speechTab) {
return res.status(200).send(settings);
}
const speechTab = customConfig.speech.speechTab;
if (speechTab.advancedMode !== undefined) {
settings.advancedMode = speechTab.advancedMode;
}
if (speechTab.speechToText) {
for (const key in speechTab.speechToText) {
if (speechTab.speechToText[key] !== undefined) {
settings[key] = speechTab.speechToText[key];
}
}
}
if (speechTab.textToSpeech) {
for (const key in speechTab.textToSpeech) {
if (speechTab.textToSpeech[key] !== undefined) {
settings[key] = speechTab.textToSpeech[key];
}
}
}
return res.status(200).send(settings);
} catch (error) {
logger.error('Failed to get custom config speech settings:', error);
res.status(500).send('Internal Server Error');
}
}
module.exports = getCustomConfigSpeech;