Allow overriding config.yaml values with env vars

Closes #3520
This commit is contained in:
Cohee
2025-02-20 20:29:42 +02:00
parent 7ea2c5f8cf
commit f6fe5fea77
2 changed files with 13 additions and 1 deletions

View File

@@ -261,7 +261,7 @@ app.use(responseTime());
/** @type {number} */
const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getConfigValue('port', DEFAULT_PORT);
const server_port = cliArguments.port ?? getConfigValue('port', DEFAULT_PORT);
/** @type {boolean} */
const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl;
/** @type {boolean} */

View File

@@ -20,6 +20,14 @@ import { LOG_LEVELS } from './constants.js';
*/
let CACHED_CONFIG = null;
/**
* Converts a configuration key to an environment variable key.
* @param {string} key Configuration key
* @returns {string} Environment variable key
* @example keyToEnv('extensions.models.speechToText') // 'SILLYTAVERN_EXTENSIONS_MODELS_SPEECHTOTEXT'
*/
export const keyToEnv = (key) => 'SILLYTAVERN_' + String(key).toUpperCase().replace(/\./g, '_');
/**
* Returns the config object from the config.yaml file.
* @returns {object} Config object
@@ -53,6 +61,10 @@ export function getConfig() {
* @returns {any} Value for the given key
*/
export function getConfigValue(key, defaultValue = null) {
const envKey = keyToEnv(key);
if (envKey in process.env) {
return process.env[envKey];
}
const config = getConfig();
return _.get(config, key, defaultValue);
}