Skip to content

Commit

Permalink
Merge branch 'main' into feat/messages-feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rubentalstra authored Feb 15, 2025
2 parents 8bee34e + e3b5c59 commit 439b88a
Show file tree
Hide file tree
Showing 25 changed files with 349 additions and 181 deletions.
117 changes: 16 additions & 101 deletions client/src/hooks/Files/useFileHandling.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { v4 } from 'uuid';
import debounce from 'lodash/debounce';
import { useQueryClient } from '@tanstack/react-query';
import { useState, useEffect, useCallback, useRef, useMemo } from 'react';
import React, { useState, useEffect, useCallback, useRef, useMemo } from 'react';
import {
megabyte,
QueryKeys,
EModelEndpoint,
codeTypeMapping,
mergeFileConfig,
isAgentsEndpoint,
isAssistantsEndpoint,
Expand All @@ -16,14 +14,12 @@ import {
import type { TEndpointsConfig, TError } from 'librechat-data-provider';
import type { ExtendedFile, FileSetter } from '~/common';
import { useUploadFileMutation, useGetFileConfig } from '~/data-provider';
import useLocalize, { TranslationKeys } from '~/hooks/useLocalize';
import { useDelayedUploadToast } from './useDelayedUploadToast';
import { useToastContext } from '~/Providers/ToastContext';
import { useChatContext } from '~/Providers/ChatContext';
import useLocalize from '~/hooks/useLocalize';
import { logger, validateFiles } from '~/utils';
import useUpdateFiles from './useUpdateFiles';
import { logger } from '~/utils';

const { checkType } = defaultFileConfig;

type UseFileHandling = {
overrideEndpoint?: EModelEndpoint;
Expand Down Expand Up @@ -58,20 +54,11 @@ const useFileHandling = (params?: UseFileHandling) => {
[params?.overrideEndpoint, conversation?.endpointType, conversation?.endpoint],
);

const { fileLimit, fileSizeLimit, totalSizeLimit, supportedMimeTypes } = useMemo(
() =>
fileConfig?.endpoints[endpoint] ??
fileConfig?.endpoints.default ??
defaultFileConfig.endpoints[endpoint] ??
defaultFileConfig.endpoints.default,
[fileConfig, endpoint],
);

const displayToast = useCallback(() => {
if (errors.length > 1) {
// TODO: this should not be a dynamic localize input!!
const errorList = Array.from(new Set(errors))
.map((e, i) => `${i > 0 ? '• ' : ''}${localize(e) || e}\n`)
.map((e, i) => `${i > 0 ? '• ' : ''}${localize(e as TranslationKeys) || e}\n`)
.join('');
showToast({
message: errorList,
Expand All @@ -80,7 +67,7 @@ const useFileHandling = (params?: UseFileHandling) => {
});
} else if (errors.length === 1) {
// TODO: this should not be a dynamic localize input!!
const message = localize(errors[0]) || errors[0];
const message = localize(errors[0] as TranslationKeys) || errors[0];
showToast({
message,
status: 'error',
Expand Down Expand Up @@ -147,7 +134,7 @@ const useFileHandling = (params?: UseFileHandling) => {
const errorMessage =
error?.code === 'ERR_CANCELED'
? 'com_error_files_upload_canceled'
: error?.response?.data?.message ?? 'com_error_files_upload';
: (error?.response?.data?.message ?? 'com_error_files_upload');
setError(errorMessage);
},
},
Expand Down Expand Up @@ -228,87 +215,6 @@ const useFileHandling = (params?: UseFileHandling) => {
uploadFile.mutate(formData);
};

const validateFiles = useCallback(
(fileList: File[]) => {
const existingFiles = Array.from(files.values());
const incomingTotalSize = fileList.reduce((total, file) => total + file.size, 0);
if (incomingTotalSize === 0) {
setError('com_error_files_empty');
return false;
}
const currentTotalSize = existingFiles.reduce((total, file) => total + file.size, 0);

if (fileList.length + files.size > fileLimit) {
setError(`You can only upload up to ${fileLimit} files at a time.`);
return false;
}

for (let i = 0; i < fileList.length; i++) {
let originalFile = fileList[i];
let fileType = originalFile.type;
const extension = originalFile.name.split('.').pop() ?? '';
const knownCodeType = codeTypeMapping[extension];

// Infer MIME type for Known Code files when the type is empty or a mismatch
if (knownCodeType && (!fileType || fileType !== knownCodeType)) {
fileType = knownCodeType;
}

// Check if the file type is still empty after the extension check
if (!fileType) {
setError('Unable to determine file type for: ' + originalFile.name);
return false;
}

// Replace empty type with inferred type
if (originalFile.type !== fileType) {
const newFile = new File([originalFile], originalFile.name, { type: fileType });
originalFile = newFile;
fileList[i] = newFile;
}

if (!checkType(originalFile.type, supportedMimeTypes)) {
console.log(originalFile);
setError('Currently, unsupported file type: ' + originalFile.type);
return false;
}

if (originalFile.size >= fileSizeLimit) {
setError(`File size exceeds ${fileSizeLimit / megabyte} MB.`);
return false;
}
}

if (currentTotalSize + incomingTotalSize > totalSizeLimit) {
setError(`The total size of the files cannot exceed ${totalSizeLimit / megabyte} MB.`);
return false;
}

const combinedFilesInfo = [
...existingFiles.map(
(file) =>
`${file.file?.name ?? file.filename}-${file.size}-${
file.type?.split('/')[0] ?? 'file'
}`,
),
...fileList.map(
(file: File | undefined) =>
`${file?.name}-${file?.size}-${file?.type.split('/')[0] ?? 'file'}`,
),
];

const uniqueFilesSet = new Set(combinedFilesInfo);

if (uniqueFilesSet.size !== combinedFilesInfo.length) {
setError('com_error_files_dupe');
return false;
}

return true;
},
[files, fileLimit, fileSizeLimit, totalSizeLimit, supportedMimeTypes],
);

const loadImage = (extendedFile: ExtendedFile, preview: string) => {
const img = new Image();
img.onload = async () => {
Expand All @@ -332,7 +238,16 @@ const useFileHandling = (params?: UseFileHandling) => {
/* Validate files */
let filesAreValid: boolean;
try {
filesAreValid = validateFiles(fileList);
filesAreValid = validateFiles({
files,
fileList,
setError,
endpointFileConfig:
fileConfig?.endpoints[endpoint] ??
fileConfig?.endpoints.default ??
defaultFileConfig.endpoints[endpoint] ??
defaultFileConfig.endpoints.default,
});
} catch (error) {
console.error('file validation error', error);
setError('com_error_files_validation');
Expand Down
6 changes: 3 additions & 3 deletions client/src/locales/ar/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,10 @@
"com_nav_lang_arabic": "العربية",
"com_nav_lang_auto": "اكتشاف تلقائي",
"com_nav_lang_brazilian_portuguese": "Português Brasileiro",
"com_nav_lang_portuguese": "Português",
"com_nav_lang_chinese": "中文",
"com_nav_lang_estonian": "Eesti keel",
"com_nav_lang_dutch": "Nederlands",
"com_nav_lang_english": "English",
"com_nav_lang_estonian": "Eesti keel",
"com_nav_lang_finnish": "Suomi",
"com_nav_lang_french": "Français ",
"com_nav_lang_german": "Deutsch",
Expand All @@ -347,12 +346,13 @@
"com_nav_lang_japanese": "日本語",
"com_nav_lang_korean": "한국어",
"com_nav_lang_polish": "Polski",
"com_nav_lang_portuguese": "Português",
"com_nav_lang_russian": "Русский",
"com_nav_lang_spanish": "Español",
"com_nav_lang_swedish": "Svenska",
"com_nav_lang_traditional_chinese": "繁體中文",
"com_nav_lang_turkish": "Türkçe",
"com_nav_lang_vietnamese": "Tiếng Việt",
"com_nav_lang_traditional_chinese": "繁體中文",
"com_nav_language": "اللغة",
"com_nav_latex_parsing": "تحليل LaTeX في الرسائل (قد يؤثر على الأداء)",
"com_nav_log_out": "تسجيل الخروج",
Expand Down
6 changes: 3 additions & 3 deletions client/src/locales/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,10 @@
"com_nav_lang_arabic": "العربية",
"com_nav_lang_auto": "Automatisch erkennen",
"com_nav_lang_brazilian_portuguese": "Português Brasileiro",
"com_nav_lang_portuguese": "Português",
"com_nav_lang_chinese": "中文",
"com_nav_lang_estonian": "Eesti keel",
"com_nav_lang_dutch": "Nederlands",
"com_nav_lang_english": "English",
"com_nav_lang_estonian": "Eesti keel",
"com_nav_lang_finnish": "Suomi",
"com_nav_lang_french": "Français ",
"com_nav_lang_german": "Deutsch",
Expand All @@ -354,12 +353,13 @@
"com_nav_lang_japanese": "日本語",
"com_nav_lang_korean": "한국어",
"com_nav_lang_polish": "Polski",
"com_nav_lang_portuguese": "Português",
"com_nav_lang_russian": "Русский",
"com_nav_lang_spanish": "Español",
"com_nav_lang_swedish": "Svenska",
"com_nav_lang_traditional_chinese": "繁體中文",
"com_nav_lang_turkish": "Türkçe",
"com_nav_lang_vietnamese": "Tiếng Việt",
"com_nav_lang_traditional_chinese": "繁體中文",
"com_nav_language": "Sprache",
"com_nav_latex_parsing": "LaTeX in Nachrichten parsen (kann die Leistung beeinflussen)",
"com_nav_log_out": "Abmelden",
Expand Down
24 changes: 12 additions & 12 deletions client/src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,10 @@
"com_nav_lang_arabic": "العربية",
"com_nav_lang_auto": "Auto detect",
"com_nav_lang_brazilian_portuguese": "Português Brasileiro",
"com_nav_lang_portuguese": "Português",
"com_nav_lang_chinese": "中文",
"com_nav_lang_estonian": "Eesti keel",
"com_nav_lang_dutch": "Nederlands",
"com_nav_lang_english": "English",
"com_nav_lang_estonian": "Eesti keel",
"com_nav_lang_finnish": "Suomi",
"com_nav_lang_french": "Français ",
"com_nav_lang_german": "Deutsch",
Expand All @@ -359,6 +358,7 @@
"com_nav_lang_japanese": "日本語",
"com_nav_lang_korean": "한국어",
"com_nav_lang_polish": "Polski",
"com_nav_lang_portuguese": "Português",
"com_nav_lang_russian": "Русский",
"com_nav_lang_spanish": "Español",
"com_nav_lang_swedish": "Svenska",
Expand Down Expand Up @@ -587,6 +587,7 @@
"com_ui_duplication_processing": "Duplicating conversation...",
"com_ui_duplication_success": "Successfully duplicated conversation",
"com_ui_edit": "Edit",
"com_ui_empty_category": "-",
"com_ui_endpoint": "Endpoint",
"com_ui_endpoint_menu": "LLM Endpoint Menu",
"com_ui_endpoints_available": "Available Endpoints",
Expand All @@ -602,6 +603,7 @@
"com_ui_field_required": "This field is required",
"com_ui_filter_prompts": "Filter Prompts",
"com_ui_filter_prompts_name": "Filter prompts by name",
"com_ui_finance": "Finance",
"com_ui_fork": "Fork",
"com_ui_fork_all_target": "Include all to/from here",
"com_ui_fork_branches": "Include related branches",
Expand Down Expand Up @@ -630,6 +632,7 @@
"com_ui_happy_birthday": "It's my 1st birthday!",
"com_ui_hide_qr": "Hide QR Code",
"com_ui_host": "Host",
"com_ui_idea": "Ideas",
"com_ui_image_gen": "Image Gen",
"com_ui_import_conversation": "Import",
"com_ui_import_conversation_error": "There was an error importing your conversations",
Expand All @@ -648,12 +651,14 @@
"com_ui_librechat_code_api_title": "Run AI Code",
"com_ui_llm_menu": "LLM Menu",
"com_ui_llms_available": "Available LLMs",
"com_ui_loading": "Loading...",
"com_ui_locked": "Locked",
"com_ui_logo": "{{0}} Logo",
"com_ui_manage": "Manage",
"com_ui_max_tags": "Maximum number allowed is {{0}}, using latest values.",
"com_ui_mention": "Mention an endpoint, assistant, or preset to quickly switch to it",
"com_ui_min_tags": "Cannot remove more values, a minimum of {{0}} are required.",
"com_ui_misc": "Misc.",
"com_ui_model": "Model",
"com_ui_model_parameters": "Model Parameters",
"com_ui_more_info": "More info",
Expand Down Expand Up @@ -710,6 +715,7 @@
"com_ui_revoke_keys": "Revoke Keys",
"com_ui_revoke_keys_confirm": "Are you sure you want to revoke all keys?",
"com_ui_role_select": "Role",
"com_ui_roleplay": "Roleplay",
"com_ui_run_code": "Run Code",
"com_ui_run_code_error": "There was an error running the code",
"com_ui_save": "Save",
Expand Down Expand Up @@ -741,6 +747,7 @@
"com_ui_shared_link_delete_success": "Successfully deleted shared link",
"com_ui_shared_link_not_found": "Shared link not found",
"com_ui_shared_prompts": "Shared Prompts",
"com_ui_shop": "Shopping",
"com_ui_show_all": "Show All",
"com_ui_show_qr": "Show QR Code",
"com_ui_sign_in_to_domain": "Sign-in to {{0}}",
Expand All @@ -752,6 +759,7 @@
"com_ui_stop": "Stop",
"com_ui_storage": "Storage",
"com_ui_submit": "Submit",
"com_ui_teach_or_explain": "Learning",
"com_ui_temporary_chat": "Temporary Chat",
"com_ui_terms_and_conditions": "Terms and Conditions",
"com_ui_terms_of_service": "Terms of service",
Expand All @@ -760,6 +768,7 @@
"com_ui_token_exchange_method": "Token Exchange Method",
"com_ui_token_url": "Token URL",
"com_ui_tools": "Tools",
"com_ui_travel": "Travel",
"com_ui_unarchive": "Unarchive",
"com_ui_unarchive_error": "Failed to unarchive conversation",
"com_ui_unknown": "Unknown",
Expand All @@ -783,18 +792,9 @@
"com_ui_version_var": "Version {{0}}",
"com_ui_versions": "Versions",
"com_ui_view_source": "View source chat",
"com_ui_write": "Writing",
"com_ui_yes": "Yes",
"com_ui_zoom": "Zoom",
"com_user_message": "You",
"com_ui_loading": "Loading...",
"com_ui_finance": "Finance",
"com_ui_idea": "Ideas",
"com_ui_misc": "Misc.",
"com_ui_roleplay": "Roleplay",
"com_ui_shop": "Shopping",
"com_ui_teach_or_explain": "Learning",
"com_ui_travel": "Travel",
"com_ui_write": "Writing",
"com_ui_empty_category": "-",
"com_warning_resubmit_unsupported": "Resubmitting the AI message is not supported for this endpoint."
}
6 changes: 3 additions & 3 deletions client/src/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,10 @@
"com_nav_lang_arabic": "العربية",
"com_nav_lang_auto": "Detección automática",
"com_nav_lang_brazilian_portuguese": "Português Brasileiro",
"com_nav_lang_portuguese": "Português",
"com_nav_lang_chinese": "中文",
"com_nav_lang_estonian": "Eesti keel",
"com_nav_lang_dutch": "Nederlands",
"com_nav_lang_english": "English",
"com_nav_lang_estonian": "Eesti keel",
"com_nav_lang_finnish": "Suomi",
"com_nav_lang_french": "Français ",
"com_nav_lang_german": "Deutsch",
Expand All @@ -347,12 +346,13 @@
"com_nav_lang_japanese": "日本語",
"com_nav_lang_korean": "한국어",
"com_nav_lang_polish": "Polski",
"com_nav_lang_portuguese": "Português",
"com_nav_lang_russian": "Русский",
"com_nav_lang_spanish": "Español",
"com_nav_lang_swedish": "Svenska",
"com_nav_lang_traditional_chinese": "繁體中文",
"com_nav_lang_turkish": "Türkçe",
"com_nav_lang_vietnamese": "Tiếng Việt",
"com_nav_lang_traditional_chinese": "繁體中文",
"com_nav_language": "Idioma",
"com_nav_latex_parsing": "Analizar LaTeX en los mensajes (puede afectar el rendimiento)",
"com_nav_log_out": "Cerrar sesión",
Expand Down
Loading

0 comments on commit 439b88a

Please sign in to comment.