refactor: replace getCustomConfig with getAppConfig in STTService, TTSService, and related files

This commit is contained in:
Danny Avila 2025-08-18 01:27:47 -04:00
parent b0256510b5
commit c82c47ab6a
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
6 changed files with 37 additions and 40 deletions

View file

@ -87,12 +87,14 @@ const AppService = async () => {
const registration = config.registration ?? configDefaults.registration; const registration = config.registration ?? configDefaults.registration;
const interfaceConfig = await loadDefaultInterface(config, configDefaults); const interfaceConfig = await loadDefaultInterface(config, configDefaults);
const turnstileConfig = loadTurnstileConfig(config, configDefaults); const turnstileConfig = loadTurnstileConfig(config, configDefaults);
const speech = config.speech;
const defaultConfig = { const defaultConfig = {
ocr, ocr,
paths, paths,
config, config,
memory, memory,
speech,
balance, balance,
mcpConfig, mcpConfig,
webSearch, webSearch,

View file

@ -2,10 +2,10 @@ const axios = require('axios');
const fs = require('fs').promises; const fs = require('fs').promises;
const FormData = require('form-data'); const FormData = require('form-data');
const { Readable } = require('stream'); const { Readable } = require('stream');
const { logger } = require('@librechat/data-schemas');
const { genAzureEndpoint } = require('@librechat/api'); const { genAzureEndpoint } = require('@librechat/api');
const { extractEnvVariable, STTProviders } = require('librechat-data-provider'); const { extractEnvVariable, STTProviders } = require('librechat-data-provider');
const { getCustomConfig } = require('~/server/services/Config'); const { getAppConfig } = require('~/server/services/Config');
const { logger } = require('~/config');
/** /**
* Maps MIME types to their corresponding file extensions for audio files. * Maps MIME types to their corresponding file extensions for audio files.
@ -84,12 +84,7 @@ function getFileExtensionFromMime(mimeType) {
* @class * @class
*/ */
class STTService { class STTService {
/** constructor() {
* Creates an instance of STTService.
* @param {Object} customConfig - The custom configuration object.
*/
constructor(customConfig) {
this.customConfig = customConfig;
this.providerStrategies = { this.providerStrategies = {
[STTProviders.OPENAI]: this.openAIProvider, [STTProviders.OPENAI]: this.openAIProvider,
[STTProviders.AZURE_OPENAI]: this.azureOpenAIProvider, [STTProviders.AZURE_OPENAI]: this.azureOpenAIProvider,
@ -104,21 +99,20 @@ class STTService {
* @throws {Error} If the custom config is not found. * @throws {Error} If the custom config is not found.
*/ */
static async getInstance() { static async getInstance() {
const customConfig = await getCustomConfig(); return new STTService();
if (!customConfig) {
throw new Error('Custom config not found');
}
return new STTService(customConfig);
} }
/** /**
* Retrieves the configured STT provider and its schema. * Retrieves the configured STT provider and its schema.
* @param {ServerRequest} req - The request object.
* @returns {Promise<[string, Object]>} A promise that resolves to an array containing the provider name and its schema. * @returns {Promise<[string, Object]>} A promise that resolves to an array containing the provider name and its schema.
* @throws {Error} If no STT schema is set, multiple providers are set, or no provider is set. * @throws {Error} If no STT schema is set, multiple providers are set, or no provider is set.
*/ */
async getProviderSchema() { async getProviderSchema(req) {
const sttSchema = this.customConfig.speech.stt; const appConfig = await getAppConfig({
role: req?.user?.role,
});
const sttSchema = appConfig?.speech?.stt;
if (!sttSchema) { if (!sttSchema) {
throw new Error( throw new Error(
'No STT schema is set. Did you configure STT in the custom config (librechat.yaml)?', 'No STT schema is set. Did you configure STT in the custom config (librechat.yaml)?',
@ -274,7 +268,7 @@ class STTService {
* @param {Object} res - The response object. * @param {Object} res - The response object.
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async processTextToSpeech(req, res) { async processSpeechToText(req, res) {
if (!req.file) { if (!req.file) {
return res.status(400).json({ message: 'No audio file provided in the FormData' }); return res.status(400).json({ message: 'No audio file provided in the FormData' });
} }
@ -287,7 +281,7 @@ class STTService {
}; };
try { try {
const [provider, sttSchema] = await this.getProviderSchema(); const [provider, sttSchema] = await this.getProviderSchema(req);
const text = await this.sttRequest(provider, sttSchema, { audioBuffer, audioFile }); const text = await this.sttRequest(provider, sttSchema, { audioBuffer, audioFile });
res.json({ text }); res.json({ text });
} catch (error) { } catch (error) {
@ -297,7 +291,7 @@ class STTService {
try { try {
await fs.unlink(req.file.path); await fs.unlink(req.file.path);
logger.debug('[/speech/stt] Temp. audio upload file deleted'); logger.debug('[/speech/stt] Temp. audio upload file deleted');
} catch (error) { } catch {
logger.debug('[/speech/stt] Temp. audio upload file already deleted'); logger.debug('[/speech/stt] Temp. audio upload file already deleted');
} }
} }
@ -322,7 +316,7 @@ async function createSTTService() {
*/ */
async function speechToText(req, res) { async function speechToText(req, res) {
const sttService = await createSTTService(); const sttService = await createSTTService();
await sttService.processTextToSpeech(req, res); await sttService.processSpeechToText(req, res);
} }
module.exports = { speechToText }; module.exports = { speechToText };

View file

@ -1,9 +1,9 @@
const axios = require('axios'); const axios = require('axios');
const { logger } = require('@librechat/data-schemas');
const { genAzureEndpoint } = require('@librechat/api'); const { genAzureEndpoint } = require('@librechat/api');
const { extractEnvVariable, TTSProviders } = require('librechat-data-provider'); const { extractEnvVariable, TTSProviders } = require('librechat-data-provider');
const { getRandomVoiceId, createChunkProcessor, splitTextIntoChunks } = require('./streamAudio'); const { getRandomVoiceId, createChunkProcessor, splitTextIntoChunks } = require('./streamAudio');
const { getCustomConfig } = require('~/server/services/Config'); const { getAppConfig } = require('~/server/services/Config');
const { logger } = require('~/config');
/** /**
* Service class for handling Text-to-Speech (TTS) operations. * Service class for handling Text-to-Speech (TTS) operations.
@ -32,11 +32,7 @@ class TTSService {
* @throws {Error} If the custom config is not found. * @throws {Error} If the custom config is not found.
*/ */
static async getInstance() { static async getInstance() {
const customConfig = await getCustomConfig(); return new TTSService();
if (!customConfig) {
throw new Error('Custom config not found');
}
return new TTSService(customConfig);
} }
/** /**
@ -293,10 +289,13 @@ class TTSService {
return res.status(400).send('Missing text in request body'); return res.status(400).send('Missing text in request body');
} }
const appConfig = await getAppConfig({
role: req.user?.role,
});
try { try {
res.setHeader('Content-Type', 'audio/mpeg'); res.setHeader('Content-Type', 'audio/mpeg');
const provider = this.getProvider(); const provider = this.getProvider();
const ttsSchema = this.customConfig.speech.tts[provider]; const ttsSchema = appConfig?.speech?.tts?.[provider];
const voice = await this.getVoice(ttsSchema, requestVoice); const voice = await this.getVoice(ttsSchema, requestVoice);
if (input.length < 4096) { if (input.length < 4096) {

View file

@ -1,5 +1,5 @@
const { getCustomConfig } = require('~/server/services/Config'); const { logger } = require('@librechat/data-schemas');
const { logger } = require('~/config'); const { getAppConfig } = require('~/server/services/Config');
/** /**
* This function retrieves the speechTab settings from the custom configuration * This function retrieves the speechTab settings from the custom configuration
@ -15,26 +15,26 @@ const { logger } = require('~/config');
*/ */
async function getCustomConfigSpeech(req, res) { async function getCustomConfigSpeech(req, res) {
try { try {
const customConfig = await getCustomConfig(); const appConfig = await getAppConfig();
if (!customConfig) { if (!appConfig) {
return res.status(200).send({ return res.status(200).send({
message: 'not_found', message: 'not_found',
}); });
} }
const sttExternal = !!customConfig.speech?.stt; const sttExternal = !!appConfig.speech?.stt;
const ttsExternal = !!customConfig.speech?.tts; const ttsExternal = !!appConfig.speech?.tts;
let settings = { let settings = {
sttExternal, sttExternal,
ttsExternal, ttsExternal,
}; };
if (!customConfig.speech?.speechTab) { if (!appConfig.speech?.speechTab) {
return res.status(200).send(settings); return res.status(200).send(settings);
} }
const speechTab = customConfig.speech.speechTab; const speechTab = appConfig.speech.speechTab;
if (speechTab.advancedMode !== undefined) { if (speechTab.advancedMode !== undefined) {
settings.advancedMode = speechTab.advancedMode; settings.advancedMode = speechTab.advancedMode;

View file

@ -1,5 +1,5 @@
const { TTSProviders } = require('librechat-data-provider'); const { TTSProviders } = require('librechat-data-provider');
const { getCustomConfig } = require('~/server/services/Config'); const { getAppConfig } = require('~/server/services/Config');
const { getProvider } = require('./TTSService'); const { getProvider } = require('./TTSService');
/** /**
@ -14,13 +14,13 @@ const { getProvider } = require('./TTSService');
*/ */
async function getVoices(req, res) { async function getVoices(req, res) {
try { try {
const customConfig = await getCustomConfig(); const appConfig = await getAppConfig();
if (!customConfig || !customConfig?.speech?.tts) { if (!appConfig || !appConfig?.speech?.tts) {
throw new Error('Configuration or TTS schema is missing'); throw new Error('Configuration or TTS schema is missing');
} }
const ttsSchema = customConfig?.speech?.tts; const ttsSchema = appConfig?.speech?.tts;
const provider = await getProvider(ttsSchema); const provider = await getProvider(ttsSchema);
let voices; let voices;

View file

@ -45,6 +45,8 @@ export interface AppConfig {
turnstileConfig?: TCustomConfig['turnstile']; turnstileConfig?: TCustomConfig['turnstile'];
/** Balance configuration */ /** Balance configuration */
balance?: TCustomConfig['balance']; balance?: TCustomConfig['balance'];
/** Speech configuration */
speech?: TCustomConfig['speech'];
/** MCP server configuration */ /** MCP server configuration */
mcpConfig?: TCustomConfig['mcpServers'] | null; mcpConfig?: TCustomConfig['mcpServers'] | null;
/** File configuration */ /** File configuration */