Allow overriding config.yaml values with env vars Closes #3520

f6fe5fea77b96b5d490b6af8aecab52059fdc4fb

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +13 -1Ignore whitespace
server.js+1 -1
@@ -261,7 +261,7 @@ app.use(responseTime());
261261
262262
263263/** @type {number} */
264264const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getConfigValue('port', DEFAULT_PORT);
265265/** @type {boolean} */
266266const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl;
267267/** @type {boolean} */
src/util.js+12 -0
@@ -21,6 +21,14 @@ import { LOG_LEVELS } from './constants.js';
2121let CACHED_CONFIG = null;
2222
2323/**
24+ * Converts a configuration key to an environment variable key.
25+ * @param {string} key Configuration key
26+ * @returns {string} Environment variable key
27+ * @example keyToEnv('extensions.models.speechToText') // 'SILLYTAVERN_EXTENSIONS_MODELS_SPEECHTOTEXT'
28+ */
29+export const keyToEnv = (key) => 'SILLYTAVERN_' + String(key).toUpperCase().replace(/\./g, '_');
30+
31+/**
2432 * Returns the config object from the config.yaml file.
2533 * @returns {object} Config object
2634 */
@@ -53,6 +61,10 @@ export function getConfig() {
5361 * @returns {any} Value for the given key
5462 */
5563export function getConfigValue(key, defaultValue = null) {
64+ const envKey = keyToEnv(key);
65+ if (envKey in process.env) {
66+ return process.env[envKey];
67+ }
5668 const config = getConfig();
5769 return _.get(config, key, defaultValue);
5870}