LibreChat/client/src/components/Messages/Content/Error.tsx
Ruben Talstra aae413cc71
🌎 i18n: React-i18next & i18next Integration (#5720)
* better i18n support an internationalization-framework.

* removed unused package

* auto sort for translation.json

* fixed tests with the new locales function

* added new CI actions from locize

* to use locize a mention in the README.md

* to use locize a mention in the README.md

* updated README.md and added TRANSLATION.md to the repo

* updated TRANSLATION.md badges

* updated README.md to go to the TRANSLATION.md when clicking on the Translation Progress badge

* updated TRANSLATION.md and added a new issue template.

* updated TRANSLATION.md and added a new issue template.

* updated issue template to add the iso code link.

* updated the new GitHub actions for `locize`

* updated label for new issue template --> i18n

* fixed type issue

* Fix eslint

* Fix eslint with key-spacing spacing

* fix: error type

* fix: handle undefined values in SortFilterHeader component

* fix: typing in Image component

* fix: handle optional promptGroup in PromptCard component

* fix: update localize function to accept string type and remove unnecessary JSX element

* fix: update localize function to enforce TranslationKeys type for better type safety

* fix: improve type safety and handle null values in Assistants component

* fix: enhance null checks for fileId in FilesListView component

* fix: localize 'Go back' button text in FilesListView component

* fix: update aria-label for menu buttons and add translation for 'Close Menu'

* docs: add Reasoning UI section for Chain-of-Thought AI models in README

* fix: enhance type safety by adding type for message in MultiMessage component

* fix: improve null checks and optional chaining in useAutoSave hook

* fix: improve handling of optional properties in cleanupPreset function

* fix: ensure isFetchingNextPage defaults to false and improve null checks for messages in Search component

* fix: enhance type safety and null checks in useBuildMessageTree hook

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
2025-02-09 12:05:31 -05:00

127 lines
4.5 KiB
TypeScript

// file deepcode ignore HardcodedNonCryptoSecret: No hardcoded secrets
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;
};
type TMessageLimit = {
max: number;
windowInMinutes: number;
};
type TTokenBalance = {
type: ViolationTypes | ErrorTypes;
balance: number;
tokenCost: number;
promptTokens: number;
prev_count: number;
violation_count: number;
date: Date;
generations?: TOpenAIMessage[];
};
type TExpiredKey = {
expiredAt: string;
endpoint: string;
};
type TGenericError = {
info: string;
};
const errorMessages = {
[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.INVALID_ACTION]: `com_error_${ErrorTypes.INVALID_ACTION}`,
[ErrorTypes.INVALID_REQUEST]: `com_error_${ErrorTypes.INVALID_REQUEST}`,
[ErrorTypes.NO_SYSTEM_MESSAGES]: `com_error_${ErrorTypes.NO_SYSTEM_MESSAGES}`,
[ErrorTypes.EXPIRED_USER_KEY]: (json: TExpiredKey, localize: LocalizeFunction) => {
const { expiredAt, endpoint } = json;
return localize('com_error_expired_user_key', { 0: endpoint, 1: expiredAt });
},
[ErrorTypes.INPUT_LENGTH]: (json: TGenericError, localize: LocalizeFunction) => {
const { info } = json;
return localize('com_error_input_length', { 0: info });
},
[ErrorTypes.GOOGLE_ERROR]: (json: TGenericError) => {
const { info } = json;
return info;
},
[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.',
concurrent: (json: TConcurrent) => {
const { limit } = json;
const plural = limit > 1 ? 's' : '';
return `Only ${limit} message${plural} at a time. Please allow any other responses to complete before sending another message, or wait one minute.`;
},
message_limit: (json: TMessageLimit) => {
const { max, windowInMinutes } = json;
const plural = max > 1 ? 's' : '';
return `You hit the message limit. You have a cap of ${max} message${plural} per ${
windowInMinutes > 1 ? `${windowInMinutes} minutes` : 'minute'
}.`;
},
token_balance: (json: TTokenBalance) => {
const { balance, tokenCost, promptTokens, generations } = json;
const message = `Insufficient Funds! Balance: ${balance}. Prompt tokens: ${promptTokens}. Cost: ${tokenCost}.`;
return (
<>
{message}
{generations && (
<>
<br />
<br />
</>
)}
{generations && (
<CodeBlock
lang="Generations"
error={true}
codeChildren={formatJSON(JSON.stringify(generations))}
/>
)}
</>
);
},
};
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}`;
if (!isJson(jsonString)) {
return defaultResponse;
}
const json = JSON.parse(jsonString);
const errorKey = json.code || json.type;
const keyExists = errorKey && errorMessages[errorKey];
if (keyExists && typeof errorMessages[errorKey] === 'function') {
return errorMessages[errorKey](json, localize);
} else if (keyExists && keyExists.startsWith(localizedErrorPrefix)) {
return localize(errorMessages[errorKey]);
} else if (keyExists) {
return errorMessages[errorKey];
} else {
return defaultResponse;
}
};
export default Error;