mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 18:00:15 +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
|
|
@ -40,7 +40,6 @@ jest.mock('@librechat/data-schemas', () => ({
|
|||
|
||||
jest.mock('~/utils', () => ({
|
||||
isEnabled: jest.fn((value) => value === 'true'),
|
||||
normalizeEndpointName: jest.fn((name) => name),
|
||||
}));
|
||||
|
||||
describe('getTransactionsConfig', () => {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
import { logger } from '@librechat/data-schemas';
|
||||
import { EModelEndpoint, removeNullishValues } from 'librechat-data-provider';
|
||||
import {
|
||||
EModelEndpoint,
|
||||
removeNullishValues,
|
||||
normalizeEndpointName,
|
||||
} from 'librechat-data-provider';
|
||||
import type { TCustomConfig, TEndpoint, TTransactionsConfig } from 'librechat-data-provider';
|
||||
import type { AppConfig } from '@librechat/data-schemas';
|
||||
import { isEnabled, normalizeEndpointName } from '~/utils';
|
||||
import { isEnabled } from '~/utils';
|
||||
|
||||
/**
|
||||
* Retrieves the balance configuration object
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { EModelEndpoint, extractEnvVariable } from 'librechat-data-provider';
|
||||
import { EModelEndpoint, extractEnvVariable, normalizeEndpointName } from 'librechat-data-provider';
|
||||
import type { TCustomEndpoints, TEndpoint, TConfig } from 'librechat-data-provider';
|
||||
import type { TCustomEndpointsConfig } from '~/types/endpoints';
|
||||
import { isUserProvided, normalizeEndpointName } from '~/utils';
|
||||
import { isUserProvided } from '~/utils';
|
||||
|
||||
/**
|
||||
* Load config endpoints from the cached configuration object
|
||||
|
|
|
|||
|
|
@ -9,16 +9,19 @@ import { validateAudio } from '~/files/validation';
|
|||
* Encodes and formats audio files for different providers
|
||||
* @param req - The request object
|
||||
* @param files - Array of audio files
|
||||
* @param provider - The provider to format for (currently only google is supported)
|
||||
* @param params - Object containing provider and optional endpoint
|
||||
* @param params.provider - The provider to format for (currently only google is supported)
|
||||
* @param params.endpoint - Optional endpoint name for file config lookup
|
||||
* @param getStrategyFunctions - Function to get strategy functions
|
||||
* @returns Promise that resolves to audio and file metadata
|
||||
*/
|
||||
export async function encodeAndFormatAudios(
|
||||
req: ServerRequest,
|
||||
files: IMongoFile[],
|
||||
provider: Providers,
|
||||
params: { provider: Providers; endpoint?: string },
|
||||
getStrategyFunctions: (source: string) => StrategyFunctions,
|
||||
): Promise<AudioResult> {
|
||||
const { provider, endpoint } = params;
|
||||
if (!files?.length) {
|
||||
return { audios: [], files: [] };
|
||||
}
|
||||
|
|
@ -54,7 +57,10 @@ export async function encodeAndFormatAudios(
|
|||
const audioBuffer = Buffer.from(content, 'base64');
|
||||
|
||||
/** Extract configured file size limit from fileConfig for this endpoint */
|
||||
const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, provider);
|
||||
const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, {
|
||||
provider,
|
||||
endpoint,
|
||||
});
|
||||
|
||||
const validation = await validateAudio(
|
||||
audioBuffer,
|
||||
|
|
|
|||
|
|
@ -31,14 +31,16 @@ describe('encodeAndFormatDocuments - fileConfig integration', () => {
|
|||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
/** Default mock implementation for getConfiguredFileSizeLimit */
|
||||
mockedGetConfiguredFileSizeLimit.mockImplementation((req, provider) => {
|
||||
mockedGetConfiguredFileSizeLimit.mockImplementation((req, params) => {
|
||||
if (!req.config?.fileConfig) {
|
||||
return undefined;
|
||||
}
|
||||
const { provider, endpoint } = params;
|
||||
const lookupKey = endpoint ?? provider;
|
||||
const fileConfig = req.config.fileConfig;
|
||||
const endpoints = fileConfig.endpoints;
|
||||
if (endpoints?.[provider]) {
|
||||
const limit = endpoints[provider].fileSizeLimit;
|
||||
if (endpoints?.[lookupKey]) {
|
||||
const limit = endpoints[lookupKey].fileSizeLimit;
|
||||
return limit !== undefined ? mbToBytes(limit) : undefined;
|
||||
}
|
||||
if (endpoints?.default) {
|
||||
|
|
|
|||
|
|
@ -14,16 +14,20 @@ import { validatePdf } from '~/files/validation';
|
|||
* Processes and encodes document files for various providers
|
||||
* @param req - Express request object
|
||||
* @param files - Array of file objects to process
|
||||
* @param provider - The provider name
|
||||
* @param params - Object containing provider, endpoint, and other options
|
||||
* @param params.provider - The provider name
|
||||
* @param params.endpoint - Optional endpoint name for file config lookup
|
||||
* @param params.useResponsesApi - Whether to use responses API format
|
||||
* @param getStrategyFunctions - Function to get strategy functions
|
||||
* @returns Promise that resolves to documents and file metadata
|
||||
*/
|
||||
export async function encodeAndFormatDocuments(
|
||||
req: ServerRequest,
|
||||
files: IMongoFile[],
|
||||
{ provider, useResponsesApi }: { provider: Providers; useResponsesApi?: boolean },
|
||||
params: { provider: Providers; endpoint?: string; useResponsesApi?: boolean },
|
||||
getStrategyFunctions: (source: string) => StrategyFunctions,
|
||||
): Promise<DocumentResult> {
|
||||
const { provider, endpoint, useResponsesApi } = params;
|
||||
if (!files?.length) {
|
||||
return { documents: [], files: [] };
|
||||
}
|
||||
|
|
@ -68,7 +72,10 @@ export async function encodeAndFormatDocuments(
|
|||
const pdfBuffer = Buffer.from(content, 'base64');
|
||||
|
||||
/** Extract configured file size limit from fileConfig for this endpoint */
|
||||
const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, provider);
|
||||
const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, {
|
||||
provider,
|
||||
endpoint,
|
||||
});
|
||||
|
||||
const validation = await validatePdf(
|
||||
pdfBuffer,
|
||||
|
|
|
|||
|
|
@ -1,24 +1,33 @@
|
|||
import getStream from 'get-stream';
|
||||
import { Providers } from '@librechat/agents';
|
||||
import { FileSources, mergeFileConfig } from 'librechat-data-provider';
|
||||
import { FileSources, mergeFileConfig, getEndpointFileConfig } from 'librechat-data-provider';
|
||||
import type { IMongoFile } from '@librechat/data-schemas';
|
||||
import type { ServerRequest, StrategyFunctions, ProcessedFile } from '~/types';
|
||||
|
||||
/**
|
||||
* Extracts the configured file size limit for a specific provider from fileConfig
|
||||
* @param req - The server request object containing config
|
||||
* @param provider - The provider to get the limit for
|
||||
* @param params - Object containing provider and optional endpoint
|
||||
* @param params.provider - The provider to get the limit for
|
||||
* @param params.endpoint - Optional endpoint name for lookup
|
||||
* @returns The configured file size limit in bytes, or undefined if not configured
|
||||
*/
|
||||
export const getConfiguredFileSizeLimit = (
|
||||
req: ServerRequest,
|
||||
provider: Providers,
|
||||
params: {
|
||||
provider: Providers;
|
||||
endpoint?: string;
|
||||
},
|
||||
): number | undefined => {
|
||||
if (!req.config?.fileConfig) {
|
||||
return undefined;
|
||||
}
|
||||
const { provider, endpoint } = params;
|
||||
const fileConfig = mergeFileConfig(req.config.fileConfig);
|
||||
const endpointConfig = fileConfig.endpoints[provider] ?? fileConfig.endpoints.default;
|
||||
const endpointConfig = getEndpointFileConfig({
|
||||
fileConfig,
|
||||
endpoint: endpoint ?? provider,
|
||||
});
|
||||
return endpointConfig?.fileSizeLimit;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -9,16 +9,19 @@ import { validateVideo } from '~/files/validation';
|
|||
* Encodes and formats video files for different providers
|
||||
* @param req - The request object
|
||||
* @param files - Array of video files
|
||||
* @param provider - The provider to format for
|
||||
* @param params - Object containing provider and optional endpoint
|
||||
* @param params.provider - The provider to format for
|
||||
* @param params.endpoint - Optional endpoint name for file config lookup
|
||||
* @param getStrategyFunctions - Function to get strategy functions
|
||||
* @returns Promise that resolves to videos and file metadata
|
||||
*/
|
||||
export async function encodeAndFormatVideos(
|
||||
req: ServerRequest,
|
||||
files: IMongoFile[],
|
||||
provider: Providers,
|
||||
params: { provider: Providers; endpoint?: string },
|
||||
getStrategyFunctions: (source: string) => StrategyFunctions,
|
||||
): Promise<VideoResult> {
|
||||
const { provider, endpoint } = params;
|
||||
if (!files?.length) {
|
||||
return { videos: [], files: [] };
|
||||
}
|
||||
|
|
@ -54,7 +57,10 @@ export async function encodeAndFormatVideos(
|
|||
const videoBuffer = Buffer.from(content, 'base64');
|
||||
|
||||
/** Extract configured file size limit from fileConfig for this endpoint */
|
||||
const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, provider);
|
||||
const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, {
|
||||
provider,
|
||||
endpoint,
|
||||
});
|
||||
|
||||
const validation = await validateVideo(
|
||||
videoBuffer,
|
||||
|
|
|
|||
692
packages/api/src/files/filter.spec.ts
Normal file
692
packages/api/src/files/filter.spec.ts
Normal file
|
|
@ -0,0 +1,692 @@
|
|||
import { Types } from 'mongoose';
|
||||
import { Providers } from '@librechat/agents';
|
||||
import { EModelEndpoint } from 'librechat-data-provider';
|
||||
import type { IMongoFile } from '@librechat/data-schemas';
|
||||
import type { ServerRequest } from '~/types';
|
||||
import { filterFilesByEndpointConfig } from './filter';
|
||||
|
||||
describe('filterFilesByEndpointConfig', () => {
|
||||
/** Helper to create a mock file */
|
||||
const createMockFile = (filename: string): IMongoFile =>
|
||||
({
|
||||
_id: new Types.ObjectId(),
|
||||
user: new Types.ObjectId(),
|
||||
file_id: new Types.ObjectId().toString(),
|
||||
filename,
|
||||
type: 'application/pdf',
|
||||
bytes: 1024,
|
||||
object: 'file',
|
||||
usage: 0,
|
||||
source: 'test',
|
||||
filepath: `/test/${filename}`,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
}) as unknown as IMongoFile;
|
||||
|
||||
describe('when files are disabled for endpoint', () => {
|
||||
it('should return empty array when endpoint has disabled: true', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test1.pdf'), createMockFile('test2.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array when default endpoint has disabled: true and provider not found', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
default: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array for disabled Anthropic endpoint', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.ANTHROPIC]: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('doc.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.ANTHROPIC,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array for disabled Google endpoint', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.GOOGLE]: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('video.mp4')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.GOOGLE,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when files are enabled for endpoint', () => {
|
||||
it('should return all files when endpoint has disabled: false', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
disabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test1.pdf'), createMockFile('test2.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual(files);
|
||||
});
|
||||
|
||||
it('should return all files when endpoint config exists but disabled is not set', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
fileSizeLimit: 10,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual(files);
|
||||
});
|
||||
|
||||
it('should return all files when no fileConfig exists', () => {
|
||||
const req = {} as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test1.pdf'), createMockFile('test2.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual(files);
|
||||
});
|
||||
|
||||
it('should return all files when endpoint not in config and no default', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.ANTHROPIC]: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/** OpenAI not configured, should use base defaults which allow files */
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual(files);
|
||||
});
|
||||
});
|
||||
|
||||
describe('custom endpoint configuration', () => {
|
||||
it('should use direct endpoint lookup when endpointType is custom', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
ollama: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should use normalized endpoint lookup for custom endpoints', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
ollama: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/** Test with non-normalized endpoint name (e.g., "Ollama" vs "ollama") */
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'Ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should fallback to "custom" config when specific custom endpoint not found', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[EModelEndpoint.custom]: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'unknownCustomEndpoint',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return files when custom endpoint has disabled: false', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
ollama: {
|
||||
disabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test1.pdf'), createMockFile('test2.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual(files);
|
||||
});
|
||||
|
||||
it('should use agents config as fallback for custom endpoints', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[EModelEndpoint.agents]: {
|
||||
disabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/**
|
||||
* Lookup order for custom endpoint: explicitConfig -> custom -> agents -> default
|
||||
* Should find and use agents config
|
||||
*/
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual(files);
|
||||
});
|
||||
|
||||
it('should fallback to default when agents is not configured for custom endpoint', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
/** Only default configured, no agents or custom */
|
||||
default: {
|
||||
disabled: false,
|
||||
fileLimit: 10,
|
||||
fileSizeLimit: 20,
|
||||
totalSizeLimit: 50,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/**
|
||||
* Lookup order: explicitConfig -> custom -> agents -> default
|
||||
* Since none of first three exist, should fall back to default
|
||||
*/
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual(files);
|
||||
});
|
||||
|
||||
it('should use default when agents is not configured for custom endpoint', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
/** NO agents config - should skip to default */
|
||||
default: {
|
||||
disabled: false,
|
||||
fileLimit: 15,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/**
|
||||
* Lookup order: explicitConfig -> custom -> agents -> default
|
||||
* Since agents is not configured, should fall back to default
|
||||
*/
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual(files);
|
||||
});
|
||||
|
||||
it('should block files when agents is disabled for unconfigured custom endpoint', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
agents: {
|
||||
disabled: true,
|
||||
},
|
||||
default: {
|
||||
disabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/**
|
||||
* Lookup order: explicitConfig -> custom -> agents -> default
|
||||
* Should use agents config which is disabled: true
|
||||
*/
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should prioritize specific custom endpoint over generic custom config', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[EModelEndpoint.custom]: {
|
||||
disabled: false,
|
||||
},
|
||||
ollama: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/** Should use ollama config (disabled: true), not custom config (disabled: false) */
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should handle case-insensitive custom endpoint names', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
ollama: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/** Test various case combinations */
|
||||
const result1 = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'OLLAMA',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
const result2 = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'OlLaMa',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result1).toEqual([]);
|
||||
expect(result2).toEqual([]);
|
||||
});
|
||||
|
||||
it('should work without endpointType for standard endpoints but require it for custom', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
disabled: true,
|
||||
},
|
||||
ollama: {
|
||||
disabled: true,
|
||||
},
|
||||
default: {
|
||||
disabled: false,
|
||||
fileLimit: 10,
|
||||
fileSizeLimit: 20,
|
||||
totalSizeLimit: 50,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
/** Standard endpoint works without endpointType */
|
||||
const openaiResult = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
expect(openaiResult).toEqual([]);
|
||||
|
||||
/** Custom endpoint with endpointType uses specific config */
|
||||
const customWithTypeResult = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'ollama',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
expect(customWithTypeResult).toEqual([]);
|
||||
|
||||
/** Custom endpoint without endpointType tries direct lookup, falls back to default */
|
||||
const customWithoutTypeResult = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'unknownCustom',
|
||||
});
|
||||
expect(customWithoutTypeResult).toEqual(files);
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
it('should return empty array when files input is undefined', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
disabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files: undefined,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array when files input is empty array', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
disabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files: [],
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should handle custom provider strings', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
customProvider: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('test.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: 'customProvider',
|
||||
endpointType: EModelEndpoint.custom,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('bypass scenarios from bug report', () => {
|
||||
it('should block files when switching from enabled to disabled endpoint', () => {
|
||||
/**
|
||||
* Scenario: User attaches files under Anthropic (enabled),
|
||||
* then switches to OpenAI (disabled)
|
||||
*/
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
disabled: true,
|
||||
},
|
||||
[Providers.ANTHROPIC]: {
|
||||
disabled: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [createMockFile('document.pdf')];
|
||||
|
||||
/** Files were attached under Anthropic */
|
||||
const anthropicResult = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.ANTHROPIC,
|
||||
});
|
||||
expect(anthropicResult).toEqual(files);
|
||||
|
||||
/** User switches to OpenAI - files should be filtered out */
|
||||
const openaiResult = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
expect(openaiResult).toEqual([]);
|
||||
});
|
||||
|
||||
it('should prevent drag-and-drop bypass by filtering at agent initialization', () => {
|
||||
/**
|
||||
* Scenario: User drags file into disabled endpoint
|
||||
* Server processes it but filter should remove it
|
||||
*/
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const draggedFiles = [createMockFile('dragged.pdf')];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files: draggedFiles,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('should filter multiple files when endpoint disabled', () => {
|
||||
const req = {
|
||||
config: {
|
||||
fileConfig: {
|
||||
endpoints: {
|
||||
[Providers.OPENAI]: {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
const files = [
|
||||
createMockFile('file1.pdf'),
|
||||
createMockFile('file2.pdf'),
|
||||
createMockFile('file3.pdf'),
|
||||
];
|
||||
|
||||
const result = filterFilesByEndpointConfig(req, {
|
||||
files,
|
||||
endpoint: Providers.OPENAI,
|
||||
});
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
44
packages/api/src/files/filter.ts
Normal file
44
packages/api/src/files/filter.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { getEndpointFileConfig, mergeFileConfig } from 'librechat-data-provider';
|
||||
import type { IMongoFile } from '@librechat/data-schemas';
|
||||
import type { ServerRequest } from '~/types';
|
||||
|
||||
/**
|
||||
* Filters out files if the endpoint/provider has file uploads disabled
|
||||
* @param req - The server request object containing config
|
||||
* @param params - Object containing files, endpoint, and endpointType
|
||||
* @param params.files - Array of processed file documents from MongoDB
|
||||
* @param params.endpoint - The endpoint name to check configuration for
|
||||
* @param params.endpointType - The endpoint type to check configuration for
|
||||
* @returns Filtered array of files (empty if disabled)
|
||||
*/
|
||||
export function filterFilesByEndpointConfig(
|
||||
req: ServerRequest,
|
||||
params: {
|
||||
files: IMongoFile[] | undefined;
|
||||
endpoint?: string | null;
|
||||
endpointType?: string | null;
|
||||
},
|
||||
): IMongoFile[] {
|
||||
const { files, endpoint, endpointType } = params;
|
||||
|
||||
if (!files || files.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const fileConfig = mergeFileConfig(req.config?.fileConfig);
|
||||
const endpointFileConfig = getEndpointFileConfig({
|
||||
fileConfig,
|
||||
endpoint,
|
||||
endpointType,
|
||||
});
|
||||
|
||||
/**
|
||||
* If endpoint has files explicitly disabled, filter out all files
|
||||
* Only filter if disabled is explicitly set to true
|
||||
*/
|
||||
if (endpointFileConfig?.disabled === true) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
export * from './audio';
|
||||
export * from './context';
|
||||
export * from './encode';
|
||||
export * from './filter';
|
||||
export * from './mistral/crud';
|
||||
export * from './ocr';
|
||||
export * from './parse';
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { Providers } from '@librechat/agents';
|
||||
import { AuthType } from 'librechat-data-provider';
|
||||
|
||||
/**
|
||||
|
|
@ -49,11 +48,3 @@ export function optionalChainWithEmptyCheck(
|
|||
}
|
||||
return values[values.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize the endpoint name to system-expected value.
|
||||
* @param name
|
||||
*/
|
||||
export function normalizeEndpointName(name = ''): string {
|
||||
return name.toLowerCase() === Providers.OLLAMA ? Providers.OLLAMA : name;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue