LibreChat/client/src/utils/buildDefaultConvo.ts
Danny Avila 9864fc8700
🔧 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
2024-01-04 10:17:15 -05:00

74 lines
2 KiB
TypeScript

import { parseConvo } from 'librechat-data-provider';
import getLocalStorageItems from './getLocalStorageItems';
import type { TConversation, EModelEndpoint } from 'librechat-data-provider';
const buildDefaultConvo = ({
conversation,
endpoint,
models,
lastConversationSetup,
}: {
conversation: TConversation;
endpoint: EModelEndpoint;
models: string[];
// TODO: fix this type as we should allow undefined
lastConversationSetup: TConversation;
}) => {
const { lastSelectedModel, lastSelectedTools, lastBingSettings } = getLocalStorageItems();
const { jailbreak, toneStyle } = lastBingSettings;
const endpointType = lastConversationSetup?.endpointType ?? conversation?.endpointType;
if (!endpoint) {
return {
...conversation,
endpointType,
endpoint,
};
}
const availableModels = models;
const model = lastConversationSetup?.model ?? lastSelectedModel?.[endpoint];
const secondaryModel =
endpoint === 'gptPlugins'
? lastConversationSetup?.agentOptions?.model ?? lastSelectedModel?.secondaryModel
: null;
let possibleModels: string[], secondaryModels: string[];
if (availableModels.includes(model)) {
possibleModels = [model, ...availableModels];
} else {
possibleModels = [...availableModels];
}
if (secondaryModel && availableModels.includes(secondaryModel)) {
secondaryModels = [secondaryModel, ...availableModels];
} else {
secondaryModels = [...availableModels];
}
const convo = parseConvo({
endpoint,
endpointType,
conversation: lastConversationSetup,
possibleValues: {
models: possibleModels,
secondaryModels,
},
});
const defaultConvo = {
...conversation,
...convo,
endpointType,
endpoint,
};
defaultConvo.tools = lastSelectedTools ?? defaultConvo.tools;
defaultConvo.jailbreak = jailbreak ?? defaultConvo.jailbreak;
defaultConvo.toneStyle = toneStyle ?? defaultConvo.toneStyle;
return defaultConvo;
};
export default buildDefaultConvo;