mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-16 15:35:31 +01:00
📂 refactor: Cleanup File Filtering Logic, Improve Validation (#10414)
* feat: add filterFilesByEndpointConfig to filter disabled file processing by provider * chore: explicit define of endpointFileConfig for better debugging * refactor: move `normalizeEndpointName` to data-provider as used app-wide * chore: remove overrideEndpoint from useFileHandling * refactor: improve endpoint file config selection * refactor: update filterFilesByEndpointConfig to accept structured parameters and improve endpoint file config handling * refactor: replace defaultFileConfig with getEndpointFileConfig for improved file configuration handling across components * test: add comprehensive unit tests for getEndpointFileConfig to validate endpoint configuration handling * refactor: streamline agent endpoint assignment and improve file filtering logic * feat: add error handling for disabled file uploads in endpoint configuration * refactor: update encodeAndFormat functions to accept structured parameters for provider and endpoint * refactor: streamline requestFiles handling in initializeAgent function * fix: getEndpointFileConfig partial config merging scenarios * refactor: enhance mergeWithDefault function to support document-supported providers with comprehensive MIME types * refactor: user-configured default file config in getEndpointFileConfig * fix: prevent file handling when endpoint is disabled and file is dragged to chat * refactor: move `getEndpointField` to `data-provider` and update usage across components and hooks * fix: prioritize endpointType based on agent.endpoint in file filtering logic * fix: prioritize agent.endpoint in file filtering logic and remove unnecessary endpointType defaulting
This commit is contained in:
parent
06c060b983
commit
2524d33362
62 changed files with 2352 additions and 290 deletions
|
|
@ -1,12 +1,11 @@
|
|||
import React, { useState, useMemo, useCallback } from 'react';
|
||||
import { useToastContext } from '@librechat/client';
|
||||
import { EModelEndpoint } from 'librechat-data-provider';
|
||||
import { Controller, useWatch, useFormContext } from 'react-hook-form';
|
||||
import { EModelEndpoint, getEndpointField } from 'librechat-data-provider';
|
||||
import type { AgentForm, AgentPanelProps, IconComponentTypes } from '~/common';
|
||||
import {
|
||||
removeFocusOutlines,
|
||||
processAgentOption,
|
||||
getEndpointField,
|
||||
defaultTextProps,
|
||||
validateEmail,
|
||||
getIconKey,
|
||||
|
|
|
|||
|
|
@ -6,9 +6,8 @@ import {
|
|||
EModelEndpoint,
|
||||
mergeFileConfig,
|
||||
AgentCapabilities,
|
||||
fileConfig as defaultFileConfig,
|
||||
getEndpointFileConfig,
|
||||
} from 'librechat-data-provider';
|
||||
import type { EndpointFileConfig } from 'librechat-data-provider';
|
||||
import type { ExtendedFile, AgentForm } from '~/common';
|
||||
import { useFileHandling, useLocalize, useLazyEffect } from '~/hooks';
|
||||
import FileRow from '~/components/Chat/Input/Files/FileRow';
|
||||
|
|
@ -30,12 +29,11 @@ export default function Files({
|
|||
const { watch } = useFormContext<AgentForm>();
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const [files, setFiles] = useState<Map<string, ExtendedFile>>(new Map());
|
||||
const { data: fileConfig = defaultFileConfig } = useGetFileConfig({
|
||||
const { data: fileConfig = null } = useGetFileConfig({
|
||||
select: (data) => mergeFileConfig(data),
|
||||
});
|
||||
const { abortUpload, handleFileChange } = useFileHandling({
|
||||
fileSetter: setFiles,
|
||||
overrideEndpoint: EModelEndpoint.agents,
|
||||
additionalMetadata: { agent_id, tool_resource },
|
||||
});
|
||||
|
||||
|
|
@ -51,9 +49,11 @@ export default function Files({
|
|||
|
||||
const codeChecked = watch(AgentCapabilities.execute_code);
|
||||
|
||||
const endpointFileConfig = fileConfig.endpoints[EModelEndpoint.agents] as
|
||||
| EndpointFileConfig
|
||||
| undefined;
|
||||
const endpointFileConfig = getEndpointFileConfig({
|
||||
fileConfig,
|
||||
endpoint: EModelEndpoint.agents,
|
||||
endpointType: EModelEndpoint.agents,
|
||||
});
|
||||
const isUploadDisabled = endpointFileConfig?.disabled ?? false;
|
||||
|
||||
if (isUploadDisabled) {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import {
|
|||
EModelEndpoint,
|
||||
EToolResources,
|
||||
mergeFileConfig,
|
||||
fileConfig as defaultFileConfig,
|
||||
getEndpointFileConfig,
|
||||
} from 'librechat-data-provider';
|
||||
import {
|
||||
HoverCard,
|
||||
|
|
@ -41,17 +41,15 @@ export default function FileContext({
|
|||
const { data: startupConfig } = useGetStartupConfig();
|
||||
const sharePointEnabled = startupConfig?.sharePointFilePickerEnabled;
|
||||
|
||||
const { data: fileConfig = defaultFileConfig } = useGetFileConfig({
|
||||
const { data: fileConfig = null } = useGetFileConfig({
|
||||
select: (data) => mergeFileConfig(data),
|
||||
});
|
||||
|
||||
const { handleFileChange } = useFileHandling({
|
||||
overrideEndpoint: EModelEndpoint.agents,
|
||||
additionalMetadata: { agent_id, tool_resource: EToolResources.context },
|
||||
fileSetter: setFiles,
|
||||
});
|
||||
const { handleSharePointFiles, isProcessing, downloadProgress } = useSharePointFileHandling({
|
||||
overrideEndpoint: EModelEndpoint.agents,
|
||||
additionalMetadata: { agent_id, tool_resource: EToolResources.file_search },
|
||||
fileSetter: setFiles,
|
||||
});
|
||||
|
|
@ -65,8 +63,12 @@ export default function FileContext({
|
|||
750,
|
||||
);
|
||||
|
||||
const endpointFileConfig = fileConfig.endpoints[EModelEndpoint.agents];
|
||||
const isUploadDisabled = endpointFileConfig.disabled ?? false;
|
||||
const endpointFileConfig = getEndpointFileConfig({
|
||||
fileConfig,
|
||||
endpoint: EModelEndpoint.agents,
|
||||
endpointType: EModelEndpoint.agents,
|
||||
});
|
||||
const isUploadDisabled = endpointFileConfig?.disabled ?? false;
|
||||
const handleSharePointFilesSelected = async (sharePointFiles: any[]) => {
|
||||
try {
|
||||
await handleSharePointFiles(sharePointFiles);
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import {
|
|||
EToolResources,
|
||||
mergeFileConfig,
|
||||
AgentCapabilities,
|
||||
fileConfig as defaultFileConfig,
|
||||
getEndpointFileConfig,
|
||||
} from 'librechat-data-provider';
|
||||
import type { ExtendedFile, AgentForm } from '~/common';
|
||||
import useSharePointFileHandling from '~/hooks/Files/useSharePointFileHandling';
|
||||
|
|
@ -38,18 +38,16 @@ export default function FileSearch({
|
|||
// Get startup configuration for SharePoint feature flag
|
||||
const { data: startupConfig } = useGetStartupConfig();
|
||||
|
||||
const { data: fileConfig = defaultFileConfig } = useGetFileConfig({
|
||||
const { data: fileConfig = null } = useGetFileConfig({
|
||||
select: (data) => mergeFileConfig(data),
|
||||
});
|
||||
|
||||
const { handleFileChange } = useFileHandling({
|
||||
overrideEndpoint: EModelEndpoint.agents,
|
||||
additionalMetadata: { agent_id, tool_resource: EToolResources.file_search },
|
||||
fileSetter: setFiles,
|
||||
});
|
||||
|
||||
const { handleSharePointFiles, isProcessing, downloadProgress } = useSharePointFileHandling({
|
||||
overrideEndpoint: EModelEndpoint.agents,
|
||||
additionalMetadata: { agent_id, tool_resource: EToolResources.file_search },
|
||||
fileSetter: setFiles,
|
||||
});
|
||||
|
|
@ -66,8 +64,12 @@ export default function FileSearch({
|
|||
|
||||
const fileSearchChecked = watch(AgentCapabilities.file_search);
|
||||
|
||||
const endpointFileConfig = fileConfig.endpoints[EModelEndpoint.agents];
|
||||
const isUploadDisabled = endpointFileConfig.disabled ?? false;
|
||||
const endpointFileConfig = getEndpointFileConfig({
|
||||
fileConfig,
|
||||
endpoint: EModelEndpoint.agents,
|
||||
endpointType: EModelEndpoint.agents,
|
||||
});
|
||||
const isUploadDisabled = endpointFileConfig?.disabled ?? false;
|
||||
|
||||
const sharePointEnabled = startupConfig?.sharePointFilePickerEnabled;
|
||||
const disabledUploadButton = isEphemeralAgent(agent_id) || fileSearchChecked === false;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { componentMapping } from '~/components/SidePanel/Parameters/components';
|
|||
import {
|
||||
alternateName,
|
||||
getSettingsKeys,
|
||||
getEndpointField,
|
||||
LocalStorageKeys,
|
||||
SettingDefinition,
|
||||
agentParamSettings,
|
||||
|
|
@ -14,9 +15,9 @@ import {
|
|||
import type * as t from 'librechat-data-provider';
|
||||
import type { AgentForm, AgentModelPanelProps, StringOption } from '~/common';
|
||||
import { useGetEndpointsQuery } from '~/data-provider';
|
||||
import { getEndpointField, cn } from '~/utils';
|
||||
import { useLocalize } from '~/hooks';
|
||||
import { Panel } from '~/common';
|
||||
import { cn } from '~/utils';
|
||||
|
||||
export default function ModelPanel({
|
||||
providers,
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
import { useState, useRef, useEffect } from 'react';
|
||||
import {
|
||||
EToolResources,
|
||||
mergeFileConfig,
|
||||
fileConfig as defaultFileConfig,
|
||||
} from 'librechat-data-provider';
|
||||
import type { AssistantsEndpoint, EndpointFileConfig } from 'librechat-data-provider';
|
||||
import { EToolResources, mergeFileConfig, getEndpointFileConfig } from 'librechat-data-provider';
|
||||
import type { AssistantsEndpoint } from 'librechat-data-provider';
|
||||
import type { ExtendedFile } from '~/common';
|
||||
import FileRow from '~/components/Chat/Input/Files/FileRow';
|
||||
import { useGetFileConfig } from '~/data-provider';
|
||||
|
|
@ -28,11 +24,10 @@ export default function CodeFiles({
|
|||
const { setFilesLoading } = useChatContext();
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const [files, setFiles] = useState<Map<string, ExtendedFile>>(new Map());
|
||||
const { data: fileConfig = defaultFileConfig } = useGetFileConfig({
|
||||
const { data: fileConfig = null } = useGetFileConfig({
|
||||
select: (data) => mergeFileConfig(data),
|
||||
});
|
||||
const { handleFileChange } = useFileHandling({
|
||||
overrideEndpoint: endpoint,
|
||||
additionalMetadata: { assistant_id, tool_resource },
|
||||
fileSetter: setFiles,
|
||||
});
|
||||
|
|
@ -43,7 +38,11 @@ export default function CodeFiles({
|
|||
}
|
||||
}, [_files]);
|
||||
|
||||
const endpointFileConfig = fileConfig.endpoints[endpoint] as EndpointFileConfig | undefined;
|
||||
const endpointFileConfig = getEndpointFileConfig({
|
||||
fileConfig,
|
||||
endpoint,
|
||||
endpointType: endpoint,
|
||||
});
|
||||
const isUploadDisabled = endpointFileConfig?.disabled ?? false;
|
||||
|
||||
if (isUploadDisabled) {
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ import { useState, useRef, useEffect } from 'react';
|
|||
import {
|
||||
mergeFileConfig,
|
||||
retrievalMimeTypes,
|
||||
fileConfig as defaultFileConfig,
|
||||
getEndpointFileConfig,
|
||||
} from 'librechat-data-provider';
|
||||
import type { AssistantsEndpoint, EndpointFileConfig } from 'librechat-data-provider';
|
||||
import type { AssistantsEndpoint } from 'librechat-data-provider';
|
||||
import type { ExtendedFile } from '~/common';
|
||||
import FileRow from '~/components/Chat/Input/Files/FileRow';
|
||||
import { useGetFileConfig } from '~/data-provider';
|
||||
|
|
@ -38,11 +38,10 @@ export default function Knowledge({
|
|||
const { setFilesLoading } = useChatContext();
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const [files, setFiles] = useState<Map<string, ExtendedFile>>(new Map());
|
||||
const { data: fileConfig = defaultFileConfig } = useGetFileConfig({
|
||||
const { data: fileConfig = null } = useGetFileConfig({
|
||||
select: (data) => mergeFileConfig(data),
|
||||
});
|
||||
const { handleFileChange } = useFileHandling({
|
||||
overrideEndpoint: endpoint,
|
||||
additionalMetadata: { assistant_id },
|
||||
fileSetter: setFiles,
|
||||
});
|
||||
|
|
@ -53,7 +52,11 @@ export default function Knowledge({
|
|||
}
|
||||
}, [_files]);
|
||||
|
||||
const endpointFileConfig = fileConfig.endpoints[endpoint] as EndpointFileConfig | undefined;
|
||||
const endpointFileConfig = getEndpointFileConfig({
|
||||
fileConfig,
|
||||
endpoint,
|
||||
endpointType: endpoint,
|
||||
});
|
||||
const isUploadDisabled = endpointFileConfig?.disabled ?? false;
|
||||
|
||||
if (isUploadDisabled) {
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import {
|
|||
mergeFileConfig,
|
||||
megabyte,
|
||||
isAssistantsEndpoint,
|
||||
getEndpointFileConfig,
|
||||
type TFile,
|
||||
} from 'librechat-data-provider';
|
||||
import { useFileMapContext, useChatContext } from '~/Providers';
|
||||
|
|
@ -86,7 +87,7 @@ export default function DataTable<TData, TValue>({ columns, data }: DataTablePro
|
|||
const fileMap = useFileMapContext();
|
||||
const { showToast } = useToastContext();
|
||||
const { setFiles, conversation } = useChatContext();
|
||||
const { data: fileConfig = defaultFileConfig } = useGetFileConfig({
|
||||
const { data: fileConfig = null } = useGetFileConfig({
|
||||
select: (data) => mergeFileConfig(data),
|
||||
});
|
||||
const { addFile } = useUpdateFiles(setFiles);
|
||||
|
|
@ -103,6 +104,7 @@ export default function DataTable<TData, TValue>({ columns, data }: DataTablePro
|
|||
|
||||
const fileData = fileMap[file.file_id];
|
||||
const endpoint = conversation.endpoint;
|
||||
const endpointType = conversation.endpointType;
|
||||
|
||||
if (!fileData.source) {
|
||||
return;
|
||||
|
|
@ -126,20 +128,31 @@ export default function DataTable<TData, TValue>({ columns, data }: DataTablePro
|
|||
});
|
||||
}
|
||||
|
||||
const { fileSizeLimit, supportedMimeTypes } =
|
||||
fileConfig.endpoints[endpoint] ?? fileConfig.endpoints.default;
|
||||
const endpointFileConfig = getEndpointFileConfig({
|
||||
fileConfig,
|
||||
endpoint,
|
||||
endpointType,
|
||||
});
|
||||
|
||||
if (fileData.bytes > fileSizeLimit) {
|
||||
if (endpointFileConfig.disabled === true) {
|
||||
showToast({
|
||||
message: localize('com_ui_attach_error_disabled'),
|
||||
status: 'error',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (fileData.bytes > (endpointFileConfig.fileSizeLimit ?? Number.MAX_SAFE_INTEGER)) {
|
||||
showToast({
|
||||
message: `${localize('com_ui_attach_error_size')} ${
|
||||
fileSizeLimit / megabyte
|
||||
(endpointFileConfig.fileSizeLimit ?? 0) / megabyte
|
||||
} MB (${endpoint})`,
|
||||
status: 'error',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!defaultFileConfig.checkType(file.type, supportedMimeTypes)) {
|
||||
if (!defaultFileConfig.checkType(file.type, endpointFileConfig.supportedMimeTypes ?? [])) {
|
||||
showToast({
|
||||
message: `${localize('com_ui_attach_error_type')} ${file.type} (${endpoint})`,
|
||||
status: 'error',
|
||||
|
|
@ -162,7 +175,7 @@ export default function DataTable<TData, TValue>({ columns, data }: DataTablePro
|
|||
metadata: fileData.metadata,
|
||||
});
|
||||
},
|
||||
[addFile, fileMap, conversation, localize, showToast, fileConfig.endpoints],
|
||||
[addFile, fileMap, conversation, localize, showToast, fileConfig],
|
||||
);
|
||||
|
||||
const filenameFilter = table.getColumn('filename')?.getFilterValue() as string;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import {
|
|||
excludedKeys,
|
||||
paramSettings,
|
||||
getSettingsKeys,
|
||||
getEndpointField,
|
||||
SettingDefinition,
|
||||
tConvoUpdateSchema,
|
||||
} from 'librechat-data-provider';
|
||||
|
|
@ -12,9 +13,9 @@ import type { TPreset } from 'librechat-data-provider';
|
|||
import { SaveAsPresetDialog } from '~/components/Endpoints';
|
||||
import { useSetIndexOptions, useLocalize } from '~/hooks';
|
||||
import { useGetEndpointsQuery } from '~/data-provider';
|
||||
import { getEndpointField, logger } from '~/utils';
|
||||
import { componentMapping } from './components';
|
||||
import { useChatContext } from '~/Providers';
|
||||
import { logger } from '~/utils';
|
||||
|
||||
export default function Parameters() {
|
||||
const localize = useLocalize();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { useState, useCallback, useMemo, memo } from 'react';
|
||||
import { getEndpointField } from 'librechat-data-provider';
|
||||
import { useUserKeyQuery } from 'librechat-data-provider/react-query';
|
||||
import { ResizableHandleAlt, ResizablePanel, useMediaQuery } from '@librechat/client';
|
||||
import type { TEndpointsConfig, TInterfaceConfig } from 'librechat-data-provider';
|
||||
|
|
@ -8,7 +9,7 @@ import { useLocalStorage, useLocalize } from '~/hooks';
|
|||
import { useGetEndpointsQuery } from '~/data-provider';
|
||||
import NavToggle from '~/components/Nav/NavToggle';
|
||||
import { useSidePanelContext } from '~/Providers';
|
||||
import { cn, getEndpointField } from '~/utils';
|
||||
import { cn } from '~/utils';
|
||||
import Nav from './Nav';
|
||||
|
||||
const defaultMinSize = 20;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue