diff --git a/client/src/components/Messages/Content/Error/ProviderError.tsx b/client/src/components/Messages/Content/Error/ProviderError.tsx index 5dddc0237b..5ac0e23ed0 100644 --- a/client/src/components/Messages/Content/Error/ProviderError.tsx +++ b/client/src/components/Messages/Content/Error/ProviderError.tsx @@ -135,9 +135,12 @@ export function UnclassifiedError({ json, text, message }: UnclassifiedErrorProp const prose = stripLangChainTroubleshootingUrl(withoutPayload).trim() || payloadProse; if ( prose != null && - /^This turn exceeds the configured (attachment count|total attachment size|extracted document text) limit \(/.test( + (/^This turn exceeds the configured (attachment count|total attachment size|extracted document text) limit \(/.test( prose, - ) + ) || + /^The attached file ".*" is over the [\d.]+ MB limit for a file sent to this model\./.test( + prose, + )) ) { return ( { expect(screen.getByText(detail)).toBeVisible(); }); + it('does not blame the provider for a file over the size limit for its model', () => { + const detail = + 'The attached file "big.csv" is over the 4.5 MB limit for a file sent to this model. Remove it or attach a smaller file, then try again.'; + renderError(detail, providerMessage); + expect(screen.getByText(catalog.com_error_attachment_limit)).toBeInTheDocument(); + expect(screen.queryByText('OpenAI could not complete this request.')).not.toBeInTheDocument(); + expect(screen.getByText(detail)).toBeVisible(); + }); + it.each(['attachment count', 'total attachment size', 'extracted document text'])( 'does not blame the provider for the local %s limit', (label) => { diff --git a/packages/api/src/agents/attachments.ts b/packages/api/src/agents/attachments.ts index 3fce5885af..313f884f11 100644 --- a/packages/api/src/agents/attachments.ts +++ b/packages/api/src/agents/attachments.ts @@ -11,6 +11,7 @@ import type { TokenCountFn } from '~/utils/text'; import type { ServerRequest } from '~/types'; import { filterFilesByEndpointRuntimeConfig } from '~/files/filter'; import { AGENT_ATTACHMENT_LIMIT_EXCEEDED } from './errors'; +import { AttachmentSizeError } from '~/files/encode/utils'; import { countTokens } from '~/utils/tokenizer'; import { extractFileContext } from '~/files'; @@ -235,8 +236,12 @@ export class AgentAttachmentPolicyError extends Error { export function isAgentAttachmentLimitError( error: unknown, -): error is AgentAttachmentLimitError | AgentAttachmentPolicyError { - return error instanceof AgentAttachmentLimitError || error instanceof AgentAttachmentPolicyError; +): error is AgentAttachmentLimitError | AgentAttachmentPolicyError | AttachmentSizeError { + return ( + error instanceof AgentAttachmentLimitError || + error instanceof AgentAttachmentPolicyError || + error instanceof AttachmentSizeError + ); } export function collectAgentAttachmentStats( diff --git a/packages/api/src/files/encode/document.spec.ts b/packages/api/src/files/encode/document.spec.ts index 745968d33e..f88ca144e5 100644 --- a/packages/api/src/files/encode/document.spec.ts +++ b/packages/api/src/files/encode/document.spec.ts @@ -14,6 +14,7 @@ import { encodeAndFormatDocuments, dedupeDocumentNames } from './document'; jest.mock('~/files/validation', () => ({ validatePdf: jest.fn(), validateBedrockDocument: jest.fn(), + getBedrockDocumentLimit: jest.requireActual('~/files/validation').getBedrockDocumentLimit, })); /** Mock the utils module */ @@ -22,10 +23,16 @@ jest.mock('./utils', () => ({ getConfiguredFileSizeLimit: jest.fn(), skipMissingAttachment: jest.requireActual('./utils').skipMissingAttachment, AttachmentObjectNotFoundError: jest.requireActual('./utils').AttachmentObjectNotFoundError, + AttachmentSizeError: jest.requireActual('./utils').AttachmentSizeError, })); import { validatePdf, validateBedrockDocument } from '~/files/validation'; -import { getFileStream, getConfiguredFileSizeLimit, AttachmentObjectNotFoundError } from './utils'; +import { + getFileStream, + AttachmentSizeError, + getConfiguredFileSizeLimit, + AttachmentObjectNotFoundError, +} from './utils'; import { Types } from 'mongoose'; const mockedValidatePdf = validatePdf as jest.MockedFunction; @@ -574,6 +581,104 @@ describe('encodeAndFormatDocuments - fileConfig integration', () => { }); }); + it('refuses a Bedrock document over the limit from its recorded size, without reading it', async () => { + const req = createMockRequest() as ServerRequest; + const file = createMockFile(8); + file.type = 'text/csv'; + + await expect( + encodeAndFormatDocuments( + req, + [file], + { provider: Providers.BEDROCK }, + mockStrategyFunctions, + ), + ).rejects.toBeInstanceOf(AttachmentSizeError); + expect(mockedGetFileStream).not.toHaveBeenCalled(); + expect(mockedGetConfiguredFileSizeLimit).toHaveBeenCalledWith( + req, + expect.objectContaining({ explicit: true }), + ); + }); + + it('bounds the Bedrock read at the limit and reports an object larger than its record', async () => { + const req = createMockRequest() as ServerRequest; + const file = createMockFile(1); + file.type = 'text/csv'; + const limit = mbToBytes(4.5); + mockedGetFileStream.mockRejectedValue( + new AttachmentSizeError(file.file_id, 'data.csv', limit), + ); + + await expect( + encodeAndFormatDocuments( + req, + [file], + { provider: Providers.BEDROCK }, + mockStrategyFunctions, + ), + ).rejects.toBeInstanceOf(AttachmentSizeError); + expect(mockedGetFileStream.mock.calls[0][5]).toBe(limit); + }); + + it('reads a Bedrock document up to a limit the operator set above the provider default', async () => { + const req = createMockRequest(10, Providers.BEDROCK) as ServerRequest; + const file = createMockFile(8); + file.type = 'text/csv'; + mockedGetFileStream.mockResolvedValue({ + file, + content: Buffer.from('a,b').toString('base64'), + metadata: file, + }); + mockedValidateBedrockDocument.mockResolvedValue({ isValid: true }); + + const result = await encodeAndFormatDocuments( + req, + [file], + { provider: Providers.BEDROCK }, + mockStrategyFunctions, + ); + expect(result.documents).toHaveLength(1); + expect(mockedGetFileStream.mock.calls[0][5]).toBe(mbToBytes(10)); + }); + + it('leaves out a replayed Bedrock document that is gone, whatever size was recorded', async () => { + const req = createMockRequest() as ServerRequest; + const file = createMockFile(8); + file.type = 'text/csv'; + const onMissingFile = jest.fn(); + mockedGetFileStream.mockRejectedValue(new AttachmentObjectNotFoundError(file.file_id)); + + const result = await encodeAndFormatDocuments( + req, + [file], + { provider: Providers.BEDROCK, onMissingFile }, + mockStrategyFunctions, + ); + expect(result.documents).toHaveLength(0); + expect(onMissingFile).toHaveBeenCalledWith(file); + expect(mockedGetFileStream.mock.calls[0][5]).toBe(mbToBytes(4.5)); + }); + + it('leaves reads for other providers unbounded', async () => { + const req = createMockRequest() as ServerRequest; + const file = createMockFile(3); + mockedGetFileStream.mockResolvedValue({ + file, + content: Buffer.from('%PDF-1.4 test').toString('base64'), + metadata: file, + }); + mockedValidatePdf.mockResolvedValue({ isValid: true }); + + await encodeAndFormatDocuments( + req, + [file], + { provider: Providers.ANTHROPIC }, + mockStrategyFunctions, + ); + expect(mockedGetFileStream.mock.calls[0][5]).toBeUndefined(); + }); + it('should format Bedrock CSV document', async () => { const req = createMockRequest() as ServerRequest; const file = createMockDocFile(1, 'text/csv', 'data.csv'); @@ -706,7 +811,7 @@ describe('encodeAndFormatDocuments - fileConfig integration', () => { it('should reject Bedrock document when validation fails', async () => { const req = createMockRequest() as ServerRequest; - const file = createMockDocFile(5, 'text/csv', 'big.csv'); + const file = createMockDocFile(3, 'text/csv', 'big.csv'); const mockContent = Buffer.from('large-csv-content').toString('base64'); mockedGetFileStream.mockResolvedValue({ diff --git a/packages/api/src/files/encode/document.ts b/packages/api/src/files/encode/document.ts index 4a6b2b2da1..db53ce1d14 100644 --- a/packages/api/src/files/encode/document.ts +++ b/packages/api/src/files/encode/document.ts @@ -22,8 +22,13 @@ import { CONTENT_TRAVERSAL_MAX_NODES, ContentTraversalLimitError, } from '~/protection/adapters/nested'; -import { getFileStream, getConfiguredFileSizeLimit, skipMissingAttachment } from './utils'; -import { validatePdf, validateBedrockDocument } from '~/files/validation'; +import { + getFileStream, + AttachmentSizeError, + skipMissingAttachment, + getConfiguredFileSizeLimit, +} from './utils'; +import { validatePdf, validateBedrockDocument, getBedrockDocumentLimit } from '~/files/validation'; import { someReachableAgent } from '~/agents/traversal'; import { runGuardedEncode } from './memoryGuard'; @@ -345,18 +350,49 @@ export async function encodeAndFormatDocuments( return result; } - const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, { provider, endpoint }); + /** For Bedrock only a limit the operator set counts, so the inherited 512 MB cannot stand in for it. */ + const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, { + provider, + endpoint, + explicit: isBedrock, + }); + + /** Bedrock's limit per document, known before anything is read. Other providers read unbounded. */ + const readLimits = processableFiles.map((file) => + isBedrock ? getBedrockDocumentLimit(file.type, configuredFileSizeLimit, model) : undefined, + ); + /** + * A file attached this turn is refused from its recorded size, before storage is opened. Replayed + * history skips this: an object that is gone has to be reported missing and left out, and the + * bounded read still refuses one that is present. + */ + for (const [index, file] of onMissingFile ? [] : processableFiles.entries()) { + const limit = readLimits[index]; + if (limit != null && (file.bytes ?? 0) > limit) { + throw new AttachmentSizeError(file.file_id, file.filename, limit); + } + } const results = await Promise.allSettled( - processableFiles.map((file) => + processableFiles.map((file, index) => runGuardedEncode(file.bytes ?? 0, () => - getFileStream(req, file, encodingMethods, getStrategyFunctions), + getFileStream( + req, + file, + encodingMethods, + getStrategyFunctions, + undefined, + readLimits[index], + ), ), ), ); for (const [index, settledResult] of results.entries()) { if (settledResult.status === 'rejected') { + if (settledResult.reason instanceof AttachmentSizeError) { + throw settledResult.reason; + } if (skipMissingAttachment(settledResult.reason, processableFiles[index], onMissingFile)) { continue; } diff --git a/packages/api/src/files/encode/utils.spec.ts b/packages/api/src/files/encode/utils.spec.ts index e56c464f73..5d506e9577 100644 --- a/packages/api/src/files/encode/utils.spec.ts +++ b/packages/api/src/files/encode/utils.spec.ts @@ -5,12 +5,14 @@ import { audioMimeTypes } from 'librechat-data-provider'; import type { ServerRequest } from '~/types'; import { AttachmentObjectNotFoundError, + AttachmentSizeError, appendMissingAttachmentsNote, createMissingAttachmentCollector, replayMessageAttachments, skipMissingAttachment, getAudioFormat, getFileStream, + getConfiguredFileSizeLimit, isConfiguredProviderMediaType, } from './utils'; import { outboundRequestBudget } from '~/utils/timeouts'; @@ -69,6 +71,41 @@ describe('getFileStream', () => { ).resolves.toMatchObject({ content: Buffer.from('data').toString('base64') }); }); + it('reads an object that fits its byte limit', async () => { + const getDownloadStream = jest.fn().mockResolvedValue(Readable.from(Buffer.from('data'))); + + await expect( + getFileStream({} as ServerRequest, file, {}, () => ({ getDownloadStream }), undefined, 4), + ).resolves.toMatchObject({ content: Buffer.from('data').toString('base64') }); + }); + + it('stops reading at the byte limit and never downloads the rest', async () => { + let produced = 0; + const stream = Readable.from( + (function* chunks() { + for (let i = 0; i < 1000; i++) { + produced++; + yield Buffer.alloc(1024); + } + })(), + ); + const getDownloadStream = jest.fn().mockResolvedValue(stream); + + const read = getFileStream( + {} as ServerRequest, + { ...file, filename: 'big.csv' }, + {}, + () => ({ getDownloadStream }), + undefined, + 4096, + ); + + await expect(read).rejects.toBeInstanceOf(AttachmentSizeError); + await expect(read).rejects.toMatchObject({ status: 413, limit: 4096 }); + expect(stream.destroyed).toBe(true); + expect(produced).toBeLessThan(40); + }); + it.each([ '', 'https://minio.example.com/librechat/uploads/user%201/image%20one.png?X-Amz-Credential=secret&X-Amz-Signature=signed', @@ -193,6 +230,33 @@ describe('getFileStream with a read budget', () => { const reqWith = (fileConfig: unknown): ServerRequest => ({ config: fileConfig === undefined ? undefined : { fileConfig } }) as unknown as ServerRequest; +describe('getConfiguredFileSizeLimit', () => { + const params = { provider: Providers.BEDROCK }; + + it('is undefined without any fileConfig', () => { + expect(getConfiguredFileSizeLimit(reqWith(undefined), params)).toBeUndefined(); + }); + + it('reports the inherited default as a limit unless only an explicit one is asked for', () => { + const req = reqWith({ endpoints: { default: { fileLimit: 3 } } }); + expect(getConfiguredFileSizeLimit(req, params)).toBe(512 * 1024 * 1024); + expect(getConfiguredFileSizeLimit(req, { ...params, explicit: true })).toBeUndefined(); + }); + + it.each([ + ['its own endpoint', { endpoints: { [Providers.BEDROCK]: { fileSizeLimit: 10 } } }], + ['the default endpoint', { endpoints: { default: { fileSizeLimit: 10 } } }], + ])('returns a limit the operator set on %s', (_name, fileConfig) => { + const req = reqWith(fileConfig); + expect(getConfiguredFileSizeLimit(req, { ...params, explicit: true })).toBe(10 * 1024 * 1024); + }); + + it('keeps a disabled endpoint at zero', () => { + const req = reqWith({ endpoints: { [Providers.BEDROCK]: { disabled: true } } }); + expect(getConfiguredFileSizeLimit(req, { ...params, explicit: true })).toBe(0); + }); +}); + describe('isConfiguredProviderMediaType', () => { const params = { provider: Providers.OPENAI, endpoint: 'MyGateway' }; diff --git a/packages/api/src/files/encode/utils.ts b/packages/api/src/files/encode/utils.ts index 564cf33f74..dc522d0cf9 100644 --- a/packages/api/src/files/encode/utils.ts +++ b/packages/api/src/files/encode/utils.ts @@ -24,6 +24,28 @@ export class AttachmentObjectNotFoundError extends Error { } } +/** + * One attachment is larger than the limit for the model it is going to. The client recognizes the + * opening words (`ProviderError.tsx`) and shows this under its localized attachment-limit headline. + */ +export class AttachmentSizeError extends Error { + readonly code = 'AGENT_ATTACHMENT_LIMIT_EXCEEDED'; + readonly status = 413; + readonly statusCode = 413; + + constructor( + readonly fileId: string | undefined, + filename: string | undefined, + readonly limit: number, + ) { + const megabytes = Number((limit / (1024 * 1024)).toFixed(1)); + super( + `The attached file "${filename ?? 'attachment'}" is over the ${megabytes} MB limit for a file sent to this model. Remove it or attach a smaller file, then try again.`, + ); + this.name = 'AttachmentSizeError'; + } +} + export function isAttachmentObjectNotFoundError( error: unknown, ): error is AttachmentObjectNotFoundError { @@ -154,6 +176,10 @@ export function isStorageNotFoundError(error: unknown): boolean { * @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 + * @param params.explicit - Return a limit only when the operator set one. Merging fills every + * endpoint with the built-in default, so without this any `fileConfig` section yields a limit and + * a caller that prefers it over a provider's own limit never applies the provider's. A limit set + * to exactly the built-in default reads as inherited. * @returns The configured file size limit in bytes, or undefined if not configured */ export const getConfiguredFileSizeLimit = ( @@ -161,18 +187,23 @@ export const getConfiguredFileSizeLimit = ( params: { provider: Providers; endpoint?: string; + explicit?: boolean; }, ): number | undefined => { if (!req.config?.fileConfig) { return undefined; } - const { provider, endpoint } = params; + const { provider, endpoint, explicit } = params; const fileConfig = mergeFileConfig(req.config.fileConfig); const endpointConfig = getEndpointFileConfig({ fileConfig, endpoint: endpoint ?? provider, }); - return endpointConfig?.fileSizeLimit; + const limit = endpointConfig?.fileSizeLimit; + if (explicit && limit === getEndpointFileConfig({}).fileSizeLimit) { + return undefined; + } + return limit; }; /** @@ -288,14 +319,37 @@ type ReadBudget = Pick, 'signal' | 'tim * @param budget - Wall-clock budget for the whole read. The strategy receives its signal, and a * stream still open when the signal aborts is destroyed, so a stalled read ends with an * `OutboundTimeoutError` instead of holding the caller. Without one the read is unbounded. + * @param maxBytes - The most the stored object may hold. The read stops and the stream is destroyed + * as soon as it yields more, so an object larger than its record never reaches memory in full. * @returns Processed file with content and metadata, or null if no download reference exists */ +/** Collects a stream, giving up at the first chunk that takes it past `maxBytes`. */ +async function readUpTo( + stream: AsyncIterable, + maxBytes: number, + tooLarge: () => Error, +): Promise { + const chunks: Buffer[] = []; + let size = 0; + // Leaving the loop by throwing destroys the stream, so the rest is never downloaded. + for await (const chunk of stream) { + const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk as Uint8Array); + size += bytes.length; + if (size > maxBytes) { + throw tooLarge(); + } + chunks.push(bytes); + } + return Buffer.concat(chunks, size); +} + export async function getFileStream( req: ServerRequest, file: T, encodingMethods: Record, getStrategyFunctions: (source: string) => StrategyFunctions, budget?: ReadBudget, + maxBytes?: number, ): Promise | null> { if (!file?.filepath && !file?.storageKey) { return null; @@ -312,9 +366,13 @@ export async function getFileStream { + return new AttachmentSizeError(file.file_id, file.filename, maxBytes); + }); const content = buffer.toString('base64'); buffer = null; diff --git a/packages/api/src/files/validation.spec.ts b/packages/api/src/files/validation.spec.ts index 97c8ee5697..6aa3468036 100644 --- a/packages/api/src/files/validation.spec.ts +++ b/packages/api/src/files/validation.spec.ts @@ -1,6 +1,12 @@ import { Providers } from '@librechat/agents'; import { mbToBytes } from 'librechat-data-provider'; -import { validatePdf, validateBedrockDocument, validateVideo, validateAudio } from './validation'; +import { + validatePdf, + validateVideo, + validateAudio, + validateBedrockDocument, + getBedrockDocumentLimit, +} from './validation'; describe('PDF Validation with fileConfig.endpoints.*.fileSizeLimit', () => { /** Helper to create a PDF buffer with valid header */ @@ -380,6 +386,21 @@ describe('PDF Validation with fileConfig.endpoints.*.fileSizeLimit', () => { }); }); + describe('getBedrockDocumentLimit', () => { + const claude4 = 'anthropic.claude-sonnet-4-20250514-v1:0'; + + it('is 4.5MB by default and 32MB for a type the model exempts', () => { + expect(getBedrockDocumentLimit('text/csv')).toBe(mbToBytes(4.5)); + expect(getBedrockDocumentLimit('application/pdf', undefined, claude4)).toBe(mbToBytes(32)); + expect(getBedrockDocumentLimit('text/csv', undefined, claude4)).toBe(mbToBytes(4.5)); + }); + + it('gives way to a limit the operator set, lower or higher', () => { + expect(getBedrockDocumentLimit('text/csv', mbToBytes(2))).toBe(mbToBytes(2)); + expect(getBedrockDocumentLimit('text/csv', mbToBytes(10))).toBe(mbToBytes(10)); + }); + }); + describe('validateBedrockDocument - non-PDF types', () => { it('should accept CSV within 4.5MB limit', async () => { const fileSize = 2 * 1024 * 1024; diff --git a/packages/api/src/files/validation.ts b/packages/api/src/files/validation.ts index 0bde847b87..a9adf92d7f 100644 --- a/packages/api/src/files/validation.ts +++ b/packages/api/src/files/validation.ts @@ -181,6 +181,22 @@ const isExemptFromBedrockDocLimit = (model?: string, mimeType?: string): boolean return false; }; +/** + * The size limit for one Bedrock document of this type on this model: the operator's limit when + * one is set, otherwise Bedrock's own. Known before anything is read, so it also bounds the read. + */ +export function getBedrockDocumentLimit( + mimeType: string, + configuredFileSizeLimit?: number, + model?: string, +): number { + /** Default 4.5 MB; exempt models (Claude 4+ PDF, Nova PDF/DOCX) take the 32 MB request bound */ + const providerLimit = isExemptFromBedrockDocLimit(model, mimeType) + ? mbToBytes(32) + : mbToBytes(4.5); + return configuredFileSizeLimit ?? providerLimit; +} + /** * Validates a document against Bedrock size limits. The default limit is 4.5 MB, * but Claude 4+ (PDF) and Nova (PDF/DOCX) models are exempt per AWS docs. @@ -200,10 +216,7 @@ export async function validateBedrockDocument( model?: string, ): Promise { try { - const exempt = isExemptFromBedrockDocLimit(model, mimeType); - /** Default 4.5 MB; exempt models (Claude 4+ PDF, Nova PDF/DOCX) default to 32 MB when unconfigured */ - const providerLimit = exempt ? mbToBytes(32) : mbToBytes(4.5); - const effectiveLimit = configuredFileSizeLimit ?? providerLimit; + const effectiveLimit = getBedrockDocumentLimit(mimeType, configuredFileSizeLimit, model); if (fileSize > effectiveLimit) { const limitMB = (effectiveLimit / (1024 * 1024)).toFixed(1);