mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-28 05:06:13 +01:00
🔧 fix: Improve Endpoint Handling and Address Edge Cases (#1486)
* fix(TEndpointsConfig): resolve property access issues with typesafe helper function * fix: undefined or null endpoint edge case * refactor(mapEndpoints -> endpoints): renamed module to be more general for endpoint handling, wrote unit tests, export all helpers
This commit is contained in:
parent
42f2353509
commit
9864fc8700
24 changed files with 275 additions and 99 deletions
58
client/src/utils/endpoints.ts
Normal file
58
client/src/utils/endpoints.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import { defaultEndpoints } from 'librechat-data-provider';
|
||||
import type { EModelEndpoint, TEndpointsConfig, TConfig } from 'librechat-data-provider';
|
||||
|
||||
export const getEndpointsFilter = (endpointsConfig: TEndpointsConfig) => {
|
||||
const filter: Record<string, boolean> = {};
|
||||
if (!endpointsConfig) {
|
||||
return filter;
|
||||
}
|
||||
for (const key of Object.keys(endpointsConfig)) {
|
||||
filter[key] = !!endpointsConfig[key];
|
||||
}
|
||||
return filter;
|
||||
};
|
||||
|
||||
export const getAvailableEndpoints = (
|
||||
filter: Record<string, boolean>,
|
||||
endpointsConfig: TEndpointsConfig,
|
||||
) => {
|
||||
const defaultSet = new Set(defaultEndpoints);
|
||||
const availableEndpoints: EModelEndpoint[] = [];
|
||||
|
||||
for (const endpoint in endpointsConfig) {
|
||||
// Check if endpoint is in the filter or its type is in defaultEndpoints
|
||||
if (
|
||||
filter[endpoint] ||
|
||||
(endpointsConfig[endpoint]?.type &&
|
||||
defaultSet.has(endpointsConfig[endpoint]?.type as EModelEndpoint))
|
||||
) {
|
||||
availableEndpoints.push(endpoint as EModelEndpoint);
|
||||
}
|
||||
}
|
||||
|
||||
return availableEndpoints;
|
||||
};
|
||||
|
||||
export function getEndpointField<K extends keyof TConfig>(
|
||||
endpointsConfig: TEndpointsConfig | undefined,
|
||||
endpoint: EModelEndpoint | string | null | undefined,
|
||||
property: K,
|
||||
): TConfig[K] | undefined {
|
||||
if (!endpointsConfig || endpoint === null || endpoint === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const config = endpointsConfig[endpoint];
|
||||
if (!config) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return config[property];
|
||||
}
|
||||
|
||||
export function mapEndpoints(endpointsConfig: TEndpointsConfig) {
|
||||
const filter = getEndpointsFilter(endpointsConfig);
|
||||
return getAvailableEndpoints(filter, endpointsConfig).sort(
|
||||
(a, b) => (endpointsConfig?.[a]?.order ?? 0) - (endpointsConfig?.[b]?.order ?? 0),
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue