2025-08-26 12:10:18 -04:00
|
|
|
import { EModelEndpoint, removeNullishValues } from 'librechat-data-provider';
|
2025-09-06 00:21:02 +09:00
|
|
|
import type { TCustomConfig, TEndpoint, TTransactionsConfig } from 'librechat-data-provider';
|
2025-08-26 12:10:18 -04:00
|
|
|
import type { AppConfig } from '~/types';
|
|
|
|
|
import { isEnabled, normalizeEndpointName } from '~/utils';
|
2025-09-06 00:21:02 +09:00
|
|
|
import { logger } from '@librechat/data-schemas';
|
2025-08-26 12:10:18 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves the balance configuration object
|
|
|
|
|
* */
|
|
|
|
|
export function getBalanceConfig(appConfig?: AppConfig): Partial<TCustomConfig['balance']> | null {
|
|
|
|
|
const isLegacyEnabled = isEnabled(process.env.CHECK_BALANCE);
|
|
|
|
|
const startBalance = process.env.START_BALANCE;
|
|
|
|
|
/** @type {} */
|
|
|
|
|
const config: Partial<TCustomConfig['balance']> = removeNullishValues({
|
|
|
|
|
enabled: isLegacyEnabled,
|
|
|
|
|
startBalance: startBalance != null && startBalance ? parseInt(startBalance, 10) : undefined,
|
|
|
|
|
});
|
|
|
|
|
if (!appConfig) {
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
return { ...config, ...(appConfig?.['balance'] ?? {}) };
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 00:21:02 +09:00
|
|
|
/**
|
|
|
|
|
* Retrieves the transactions configuration object
|
|
|
|
|
* */
|
|
|
|
|
export function getTransactionsConfig(appConfig?: AppConfig): TTransactionsConfig {
|
|
|
|
|
const defaultConfig: TTransactionsConfig = { enabled: true };
|
|
|
|
|
|
|
|
|
|
if (!appConfig) {
|
|
|
|
|
return defaultConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const transactionsConfig = appConfig?.['transactions'] ?? defaultConfig;
|
|
|
|
|
const balanceConfig = getBalanceConfig(appConfig);
|
|
|
|
|
|
|
|
|
|
// If balance is enabled but transactions are disabled, force transactions to be enabled
|
|
|
|
|
// and log a warning
|
|
|
|
|
if (balanceConfig?.enabled && !transactionsConfig.enabled) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
'Configuration warning: transactions.enabled=false is incompatible with balance.enabled=true. ' +
|
|
|
|
|
'Transactions will be enabled to ensure balance tracking works correctly.',
|
|
|
|
|
);
|
|
|
|
|
return { ...transactionsConfig, enabled: true };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return transactionsConfig;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 12:10:18 -04:00
|
|
|
export const getCustomEndpointConfig = ({
|
|
|
|
|
endpoint,
|
|
|
|
|
appConfig,
|
|
|
|
|
}: {
|
|
|
|
|
endpoint: string | EModelEndpoint;
|
|
|
|
|
appConfig?: AppConfig;
|
|
|
|
|
}): Partial<TEndpoint> | undefined => {
|
|
|
|
|
if (!appConfig) {
|
|
|
|
|
throw new Error(`Config not found for the ${endpoint} custom endpoint.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const customEndpoints = appConfig.endpoints?.[EModelEndpoint.custom] ?? [];
|
|
|
|
|
return customEndpoints.find(
|
|
|
|
|
(endpointConfig) => normalizeEndpointName(endpointConfig.name) === endpoint,
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function hasCustomUserVars(appConfig?: AppConfig): boolean {
|
|
|
|
|
const mcpServers = appConfig?.mcpConfig;
|
|
|
|
|
return Object.values(mcpServers ?? {}).some((server) => server.customUserVars);
|
|
|
|
|
}
|