🧑‍💻 refactor: Display Client-facing Errors (#2476)

* fix(Google): allow presets to configure expected maxOutputTokens

* refactor: standardize client-facing errors

* refactor(checkUserKeyExpiry): pass endpoint instead of custom message

* feat(UserService): JSDocs and getUserKeyValues

* refactor: add NO_BASE_URL error type, make use of getUserKeyValues, throw user-specific errors

* ci: update tests with recent changes
This commit is contained in:
Danny Avila 2024-04-21 08:31:54 -04:00 committed by GitHub
parent 6db91978ca
commit c937b8cd07
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 343 additions and 149 deletions

View file

@ -25,10 +25,10 @@ export default function Settings({ conversation, setOption, models, readonly }:
const isGemini = model?.toLowerCase()?.includes('gemini');
const maxOutputTokensMax = isGemini
? google.maxOutputTokens.maxGeminiPro
? google.maxOutputTokens.maxGemini
: google.maxOutputTokens.max;
const maxOutputTokensDefault = isGemini
? google.maxOutputTokens.defaultGeminiPro
? google.maxOutputTokens.defaultGemini
: google.maxOutputTokens.default;
useEffect(

View file

@ -1,9 +1,13 @@
// file deepcode ignore HardcodedNonCryptoSecret: No hardcoded secrets
import { ViolationTypes } from 'librechat-data-provider';
import { ViolationTypes, ErrorTypes } from 'librechat-data-provider';
import type { TOpenAIMessage } from 'librechat-data-provider';
import type { LocalizeFunction } from '~/common';
import { formatJSON, extractJson, isJson } from '~/utils/json';
import useLocalize from '~/hooks/useLocalize';
import CodeBlock from './CodeBlock';
const localizedErrorPrefix = 'com_error';
type TConcurrent = {
limit: number;
};
@ -14,7 +18,7 @@ type TMessageLimit = {
};
type TTokenBalance = {
type: ViolationTypes;
type: ViolationTypes | ErrorTypes;
balance: number;
tokenCost: number;
promptTokens: number;
@ -24,14 +28,26 @@ type TTokenBalance = {
generations?: TOpenAIMessage[];
};
type TExpiredKey = {
expiredAt: string;
endpoint: string;
};
const errorMessages = {
ban: 'Your account has been temporarily banned due to violations of our service.',
[ErrorTypes.MODERATION]: 'com_error_moderation',
[ErrorTypes.NO_USER_KEY]: 'com_error_no_user_key',
[ErrorTypes.INVALID_USER_KEY]: 'com_error_invalid_user_key',
[ErrorTypes.NO_BASE_URL]: 'com_error_no_base_url',
[ErrorTypes.EXPIRED_USER_KEY]: (json: TExpiredKey, localize: LocalizeFunction) => {
const { expiredAt, endpoint } = json;
return localize('com_error_expired_user_key', endpoint, expiredAt);
},
[ViolationTypes.BAN]:
'Your account has been temporarily banned due to violations of our service.',
invalid_api_key:
'Invalid API key. Please check your API key and try again. You can do this by clicking on the model logo in the left corner of the textbox and selecting "Set Token" for the current selected endpoint. Thank you for your understanding.',
insufficient_quota:
'We apologize for any inconvenience caused. The default API key has reached its limit. To continue using this service, please set up your own API key. You can do this by clicking on the model logo in the left corner of the textbox and selecting "Set Token" for the current selected endpoint. Thank you for your understanding.',
moderation:
'It appears that the content submitted has been flagged by our moderation system for not aligning with our community guidelines. We\'re unable to proceed with this specific topic. If you have any other questions or topics you\'d like to explore, please edit your message, or create a new conversation.',
concurrent: (json: TConcurrent) => {
const { limit } = json;
const plural = limit > 1 ? 's' : '';
@ -69,6 +85,7 @@ const errorMessages = {
};
const Error = ({ text }: { text: string }) => {
const localize = useLocalize();
const jsonString = extractJson(text);
const errorMessage = text.length > 512 && !jsonString ? text.slice(0, 512) + '...' : text;
const defaultResponse = `Something went wrong. Here's the specific error message we encountered: ${errorMessage}`;
@ -82,7 +99,9 @@ const Error = ({ text }: { text: string }) => {
const keyExists = errorKey && errorMessages[errorKey];
if (keyExists && typeof errorMessages[errorKey] === 'function') {
return errorMessages[errorKey](json);
return errorMessages[errorKey](json, localize);
} else if (keyExists && keyExists.startsWith(localizedErrorPrefix)) {
return localize(errorMessages[errorKey]);
} else if (keyExists) {
return errorMessages[errorKey];
} else {