LibreChat/client/src/utils/cleanupPreset.ts
Danny Avila 467df0f07a
🎭 feat: Override Custom Endpoint Schema with Specified Params Endpoint (#11788)
* 🔧 refactor: Simplify payload parsing and enhance getSaveOptions logic

- Removed unused bedrockInputSchema from payloadParser, streamlining the function.
- Updated payloadParser to handle optional chaining for model parameters.
- Enhanced getSaveOptions to ensure runOptions defaults to an empty object if parsing fails, improving robustness.
- Adjusted the assignment of maxContextTokens to use the instance variable for consistency.

* 🔧 fix: Update maxContextTokens assignment logic in initializeAgent function

- Enhanced the maxContextTokens assignment to allow for user-defined values, ensuring it defaults to a calculated value only when not provided or invalid. This change improves flexibility in agent initialization.

* 🧪 test: Add unit tests for initializeAgent function

- Introduced comprehensive unit tests for the initializeAgent function, focusing on maxContextTokens behavior.
- Tests cover scenarios for user-defined values, fallback calculations, and edge cases such as zero and negative values, enhancing overall test coverage and reliability of agent initialization logic.

* refactor: default params Endpoint Configuration Handling

- Integrated `getEndpointsConfig` to fetch endpoint configurations, allowing for dynamic handling of `defaultParamsEndpoint`.
- Updated `buildEndpointOption` to pass `defaultParamsEndpoint` to `parseCompactConvo`, ensuring correct parameter handling based on endpoint type.
- Added comprehensive unit tests for `buildDefaultConvo` and `cleanupPreset` to validate behavior with `defaultParamsEndpoint`, covering various scenarios and edge cases.
- Refactored related hooks and utility functions to support the new configuration structure, improving overall flexibility and maintainability.

* refactor: Centralize defaultParamsEndpoint retrieval

- Introduced `getDefaultParamsEndpoint` function to streamline the retrieval of `defaultParamsEndpoint` across various hooks and middleware.
- Updated multiple files to utilize the new function, enhancing code consistency and maintainability.
- Removed redundant logic for fetching `defaultParamsEndpoint`, simplifying the codebase.
2026-02-13 23:04:51 -05:00

56 lines
1.8 KiB
TypeScript

import { parseConvo } from 'librechat-data-provider';
import type { TPreset } from 'librechat-data-provider';
type UIPreset = Partial<TPreset> & { presetOverride?: Partial<TPreset> };
type TCleanupPreset = {
preset?: UIPreset;
defaultParamsEndpoint?: string | null;
};
const cleanupPreset = ({ preset: _preset, defaultParamsEndpoint }: TCleanupPreset): TPreset => {
const { endpoint, endpointType } = _preset ?? ({} as UIPreset);
if (endpoint == null || endpoint === '') {
console.error(`Unknown endpoint ${endpoint}`, _preset);
return {
endpoint: null,
presetId: _preset?.presetId ?? null,
title: _preset?.title ?? 'New Preset',
};
}
const { presetOverride = {}, ...rest } = _preset ?? {};
const preset = { ...rest, ...presetOverride };
// Handle deprecated chatGptLabel field
// If both chatGptLabel and modelLabel exist, prioritize modelLabel and remove chatGptLabel
// If only chatGptLabel exists, migrate it to modelLabel
if (preset.chatGptLabel && preset.modelLabel) {
// Both exist: prioritize modelLabel, remove chatGptLabel
delete preset.chatGptLabel;
} else if (preset.chatGptLabel && !preset.modelLabel) {
// Only chatGptLabel exists: migrate to modelLabel
preset.modelLabel = preset.chatGptLabel;
delete preset.chatGptLabel;
} else if ('chatGptLabel' in preset) {
// chatGptLabel exists but is empty/falsy: remove it
delete preset.chatGptLabel;
}
const parsedPreset = parseConvo({
/* @ts-ignore: endpoint can be a custom defined name */
endpoint,
endpointType,
conversation: preset,
defaultParamsEndpoint,
});
return {
presetId: _preset?.presetId ?? null,
...parsedPreset,
endpoint,
endpointType,
title: _preset?.title ?? 'New Preset',
} as TPreset;
};
export default cleanupPreset;