refactor: implement custom endpoints configuration and streamline endpoint loading logic

This commit is contained in:
Danny Avila 2025-08-18 16:58:05 -04:00
parent 240e3bd59e
commit 5eef6ea9e8
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
12 changed files with 100 additions and 88 deletions

View file

@ -0,0 +1,56 @@
import { EModelEndpoint, extractEnvVariable } from 'librechat-data-provider';
import type { TCustomEndpoints, TEndpoint, TConfig } from 'librechat-data-provider';
import type { TCustomEndpointsConfig } from '~/types/endpoints';
import { isUserProvided, normalizeEndpointName } from '~/utils';
/**
* Load config endpoints from the cached configuration object
* @param customEndpointsConfig - The configuration object
*/
export function loadCustomEndpointsConfig(
customEndpoints?: TCustomEndpoints,
): TCustomEndpointsConfig | undefined {
if (!customEndpoints) {
return;
}
const customEndpointsConfig: TCustomEndpointsConfig = {};
if (Array.isArray(customEndpoints)) {
const filteredEndpoints = customEndpoints.filter(
(endpoint) =>
endpoint.baseURL &&
endpoint.apiKey &&
endpoint.name &&
endpoint.models &&
(endpoint.models.fetch || endpoint.models.default),
);
for (let i = 0; i < filteredEndpoints.length; i++) {
const endpoint = filteredEndpoints[i] as TEndpoint;
const {
baseURL,
apiKey,
name: configName,
iconURL,
modelDisplayLabel,
customParams,
} = endpoint;
const name = normalizeEndpointName(configName);
const resolvedApiKey = extractEnvVariable(apiKey ?? '');
const resolvedBaseURL = extractEnvVariable(baseURL ?? '');
customEndpointsConfig[name] = {
type: EModelEndpoint.custom,
userProvide: isUserProvided(resolvedApiKey),
userProvideURL: isUserProvided(resolvedBaseURL),
customParams: customParams as TConfig['customParams'],
modelDisplayLabel,
iconURL,
};
}
}
return customEndpointsConfig;
}

View file

@ -0,0 +1 @@
export * from './config';

View file

@ -1,2 +1,3 @@
export * from './custom';
export * from './google';
export * from './openai';

View file

@ -6,6 +6,7 @@ import type {
TMemoryConfig,
EModelEndpoint,
TAgentsEndpoint,
TCustomEndpoints,
TAssistantEndpoint,
} from 'librechat-data-provider';
@ -78,9 +79,9 @@ export interface AppConfig {
azureAssistants?: TAssistantEndpoint;
/** Agents endpoint configuration */
[EModelEndpoint.agents]?: TAgentsEndpoint;
/** Custom endpoints configuration */
[EModelEndpoint.custom]?: TCustomEndpoints;
/** Global endpoint configuration */
all?: TEndpoint;
/** Any additional endpoint configurations */
[key: string]: unknown;
};
}

View file

@ -0,0 +1,3 @@
import type { TConfig } from 'librechat-data-provider';
export type TCustomEndpointsConfig = Partial<{ [key: string]: Omit<TConfig, 'order'> }>;

View file

@ -1,6 +1,7 @@
export * from './config';
export * from './azure';
export * from './balance';
export * from './endpoints';
export * from './events';
export * from './error';
export * from './google';