mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-09-13 01:51:51 +00:00
* 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
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import type {
|
|
TConversation,
|
|
TPreset,
|
|
TEndpointsConfig,
|
|
EModelEndpoint,
|
|
} from 'librechat-data-provider';
|
|
import getLocalStorageItems from './getLocalStorageItems';
|
|
import { mapEndpoints } from './endpoints';
|
|
|
|
type TConvoSetup = Partial<TPreset> | Partial<TConversation>;
|
|
|
|
type TDefaultEndpoint = { convoSetup: TConvoSetup; endpointsConfig: TEndpointsConfig };
|
|
|
|
const getEndpointFromSetup = (convoSetup: TConvoSetup, endpointsConfig: TEndpointsConfig) => {
|
|
const { endpoint: targetEndpoint } = convoSetup || {};
|
|
if (targetEndpoint && endpointsConfig?.[targetEndpoint ?? '']) {
|
|
return targetEndpoint;
|
|
} else if (targetEndpoint) {
|
|
console.warn(`Illegal target endpoint ${targetEndpoint} ${endpointsConfig}`);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const getEndpointFromLocalStorage = (endpointsConfig: TEndpointsConfig) => {
|
|
try {
|
|
const { lastConversationSetup } = getLocalStorageItems();
|
|
const { endpoint } = lastConversationSetup;
|
|
const isDefaultConfig = Object.values(endpointsConfig ?? {})?.every((value) => !value);
|
|
|
|
if (isDefaultConfig && endpoint) {
|
|
return endpoint;
|
|
}
|
|
|
|
if (isDefaultConfig && endpoint) {
|
|
return endpoint;
|
|
}
|
|
|
|
return endpoint && endpointsConfig?.[endpoint ?? ''] ? endpoint : null;
|
|
} catch (error) {
|
|
console.error(error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const getDefinedEndpoint = (endpointsConfig: TEndpointsConfig) => {
|
|
const endpoints = mapEndpoints(endpointsConfig);
|
|
return endpoints.find((e) => Object.hasOwn(endpointsConfig ?? {}, e));
|
|
};
|
|
|
|
const getDefaultEndpoint = ({ convoSetup, endpointsConfig }: TDefaultEndpoint): EModelEndpoint => {
|
|
return (
|
|
getEndpointFromSetup(convoSetup, endpointsConfig) ||
|
|
getEndpointFromLocalStorage(endpointsConfig) ||
|
|
getDefinedEndpoint(endpointsConfig)
|
|
);
|
|
};
|
|
|
|
export default getDefaultEndpoint;
|