2025-10-06 17:30:16 -04:00
|
|
|
import { Providers } from '@librechat/agents';
|
🪨 feat: AWS Bedrock Document Uploads (#11912)
* feat: add aws bedrock upload to provider support
* chore: address copilot comments
* feat: add shared Bedrock document format types and MIME mapping
Bedrock Converse API accepts 9 document formats beyond PDF. Add
BedrockDocumentFormat union type, MIME-to-format mapping, and helpers
in data-provider so both client and backend can reference them.
* refactor: generalize Bedrock PDF validation to support all document types
Rename validateBedrockPdf to validateBedrockDocument with MIME-aware
logic: 4.5MB hard limit applies to all types, PDF header check only
runs for application/pdf. Adds test coverage for non-PDF documents.
* feat: support all Bedrock document formats in encoding pipeline
Widen file type gates to accept csv, doc, docx, xls, xlsx, html, txt,
md for Bedrock. Uses shared MIME-to-format map instead of hardcoded
'pdf'. Other providers' PDF-only paths remain unchanged.
* feat: expand Bedrock file upload UI to accept all document types
Add 'image_document_extended' upload type for Bedrock with accept
filters for all 9 supported formats. Update drag-and-drop validation
to use isBedrockDocumentType helper.
* fix: route Bedrock document types through provider pipeline
2026-02-23 19:32:44 -08:00
|
|
|
import {
|
|
|
|
|
isOpenAILikeProvider,
|
|
|
|
|
isBedrockDocumentType,
|
|
|
|
|
bedrockDocumentFormats,
|
|
|
|
|
isDocumentSupportedProvider,
|
|
|
|
|
} from 'librechat-data-provider';
|
2025-10-06 17:30:16 -04:00
|
|
|
import type { IMongoFile } from '@librechat/data-schemas';
|
2025-11-07 10:57:15 -05:00
|
|
|
import type {
|
|
|
|
|
AnthropicDocumentBlock,
|
|
|
|
|
StrategyFunctions,
|
|
|
|
|
DocumentResult,
|
|
|
|
|
ServerRequest,
|
|
|
|
|
} from '~/types';
|
🪨 feat: AWS Bedrock Document Uploads (#11912)
* feat: add aws bedrock upload to provider support
* chore: address copilot comments
* feat: add shared Bedrock document format types and MIME mapping
Bedrock Converse API accepts 9 document formats beyond PDF. Add
BedrockDocumentFormat union type, MIME-to-format mapping, and helpers
in data-provider so both client and backend can reference them.
* refactor: generalize Bedrock PDF validation to support all document types
Rename validateBedrockPdf to validateBedrockDocument with MIME-aware
logic: 4.5MB hard limit applies to all types, PDF header check only
runs for application/pdf. Adds test coverage for non-PDF documents.
* feat: support all Bedrock document formats in encoding pipeline
Widen file type gates to accept csv, doc, docx, xls, xlsx, html, txt,
md for Bedrock. Uses shared MIME-to-format map instead of hardcoded
'pdf'. Other providers' PDF-only paths remain unchanged.
* feat: expand Bedrock file upload UI to accept all document types
Add 'image_document_extended' upload type for Bedrock with accept
filters for all 9 supported formats. Update drag-and-drop validation
to use isBedrockDocumentType helper.
* fix: route Bedrock document types through provider pipeline
2026-02-23 19:32:44 -08:00
|
|
|
import { validatePdf, validateBedrockDocument } from '~/files/validation';
|
2025-11-07 10:57:15 -05:00
|
|
|
import { getFileStream, getConfiguredFileSizeLimit } from './utils';
|
2025-10-06 17:30:16 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Processes and encodes document files for various providers
|
|
|
|
|
* @param req - Express request object
|
|
|
|
|
* @param files - Array of file objects to process
|
2025-11-10 19:05:30 -05:00
|
|
|
* @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
|
2025-10-06 17:30:16 -04:00
|
|
|
* @param getStrategyFunctions - Function to get strategy functions
|
|
|
|
|
* @returns Promise that resolves to documents and file metadata
|
|
|
|
|
*/
|
|
|
|
|
export async function encodeAndFormatDocuments(
|
2025-11-07 10:57:15 -05:00
|
|
|
req: ServerRequest,
|
2025-10-06 17:30:16 -04:00
|
|
|
files: IMongoFile[],
|
2025-11-10 19:05:30 -05:00
|
|
|
params: { provider: Providers; endpoint?: string; useResponsesApi?: boolean },
|
2025-10-06 17:30:16 -04:00
|
|
|
getStrategyFunctions: (source: string) => StrategyFunctions,
|
|
|
|
|
): Promise<DocumentResult> {
|
2025-11-10 19:05:30 -05:00
|
|
|
const { provider, endpoint, useResponsesApi } = params;
|
2025-10-06 17:30:16 -04:00
|
|
|
if (!files?.length) {
|
|
|
|
|
return { documents: [], files: [] };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const encodingMethods: Record<string, StrategyFunctions> = {};
|
|
|
|
|
const result: DocumentResult = { documents: [], files: [] };
|
|
|
|
|
|
🪨 feat: AWS Bedrock Document Uploads (#11912)
* feat: add aws bedrock upload to provider support
* chore: address copilot comments
* feat: add shared Bedrock document format types and MIME mapping
Bedrock Converse API accepts 9 document formats beyond PDF. Add
BedrockDocumentFormat union type, MIME-to-format mapping, and helpers
in data-provider so both client and backend can reference them.
* refactor: generalize Bedrock PDF validation to support all document types
Rename validateBedrockPdf to validateBedrockDocument with MIME-aware
logic: 4.5MB hard limit applies to all types, PDF header check only
runs for application/pdf. Adds test coverage for non-PDF documents.
* feat: support all Bedrock document formats in encoding pipeline
Widen file type gates to accept csv, doc, docx, xls, xlsx, html, txt,
md for Bedrock. Uses shared MIME-to-format map instead of hardcoded
'pdf'. Other providers' PDF-only paths remain unchanged.
* feat: expand Bedrock file upload UI to accept all document types
Add 'image_document_extended' upload type for Bedrock with accept
filters for all 9 supported formats. Update drag-and-drop validation
to use isBedrockDocumentType helper.
* fix: route Bedrock document types through provider pipeline
2026-02-23 19:32:44 -08:00
|
|
|
const isBedrock = provider === Providers.BEDROCK;
|
|
|
|
|
const isDocSupported = isDocumentSupportedProvider(provider);
|
|
|
|
|
|
|
|
|
|
const documentFiles = files.filter((file) => {
|
|
|
|
|
if (isBedrock && isBedrockDocumentType(file.type)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return file.type === 'application/pdf' || file.type?.startsWith('application/');
|
|
|
|
|
});
|
2025-10-06 17:30:16 -04:00
|
|
|
|
|
|
|
|
if (!documentFiles.length) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const results = await Promise.allSettled(
|
|
|
|
|
documentFiles.map((file) => {
|
🪨 feat: AWS Bedrock Document Uploads (#11912)
* feat: add aws bedrock upload to provider support
* chore: address copilot comments
* feat: add shared Bedrock document format types and MIME mapping
Bedrock Converse API accepts 9 document formats beyond PDF. Add
BedrockDocumentFormat union type, MIME-to-format mapping, and helpers
in data-provider so both client and backend can reference them.
* refactor: generalize Bedrock PDF validation to support all document types
Rename validateBedrockPdf to validateBedrockDocument with MIME-aware
logic: 4.5MB hard limit applies to all types, PDF header check only
runs for application/pdf. Adds test coverage for non-PDF documents.
* feat: support all Bedrock document formats in encoding pipeline
Widen file type gates to accept csv, doc, docx, xls, xlsx, html, txt,
md for Bedrock. Uses shared MIME-to-format map instead of hardcoded
'pdf'. Other providers' PDF-only paths remain unchanged.
* feat: expand Bedrock file upload UI to accept all document types
Add 'image_document_extended' upload type for Bedrock with accept
filters for all 9 supported formats. Update drag-and-drop validation
to use isBedrockDocumentType helper.
* fix: route Bedrock document types through provider pipeline
2026-02-23 19:32:44 -08:00
|
|
|
const isProcessable = isBedrock
|
|
|
|
|
? isBedrockDocumentType(file.type)
|
|
|
|
|
: file.type === 'application/pdf' && isDocSupported;
|
|
|
|
|
if (!isProcessable) {
|
2025-10-06 17:30:16 -04:00
|
|
|
return Promise.resolve(null);
|
|
|
|
|
}
|
|
|
|
|
return getFileStream(req, file, encodingMethods, getStrategyFunctions);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for (const settledResult of results) {
|
|
|
|
|
if (settledResult.status === 'rejected') {
|
|
|
|
|
console.error('Document processing failed:', settledResult.reason);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const processed = settledResult.value;
|
|
|
|
|
if (!processed) continue;
|
|
|
|
|
|
|
|
|
|
const { file, content, metadata } = processed;
|
|
|
|
|
|
|
|
|
|
if (!content || !file) {
|
|
|
|
|
if (metadata) result.files.push(metadata);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
🪨 feat: AWS Bedrock Document Uploads (#11912)
* feat: add aws bedrock upload to provider support
* chore: address copilot comments
* feat: add shared Bedrock document format types and MIME mapping
Bedrock Converse API accepts 9 document formats beyond PDF. Add
BedrockDocumentFormat union type, MIME-to-format mapping, and helpers
in data-provider so both client and backend can reference them.
* refactor: generalize Bedrock PDF validation to support all document types
Rename validateBedrockPdf to validateBedrockDocument with MIME-aware
logic: 4.5MB hard limit applies to all types, PDF header check only
runs for application/pdf. Adds test coverage for non-PDF documents.
* feat: support all Bedrock document formats in encoding pipeline
Widen file type gates to accept csv, doc, docx, xls, xlsx, html, txt,
md for Bedrock. Uses shared MIME-to-format map instead of hardcoded
'pdf'. Other providers' PDF-only paths remain unchanged.
* feat: expand Bedrock file upload UI to accept all document types
Add 'image_document_extended' upload type for Bedrock with accept
filters for all 9 supported formats. Update drag-and-drop validation
to use isBedrockDocumentType helper.
* fix: route Bedrock document types through provider pipeline
2026-02-23 19:32:44 -08:00
|
|
|
const configuredFileSizeLimit = getConfiguredFileSizeLimit(req, { provider, endpoint });
|
|
|
|
|
const mimeType = file.type ?? '';
|
2025-11-07 10:57:15 -05:00
|
|
|
|
🪨 feat: AWS Bedrock Document Uploads (#11912)
* feat: add aws bedrock upload to provider support
* chore: address copilot comments
* feat: add shared Bedrock document format types and MIME mapping
Bedrock Converse API accepts 9 document formats beyond PDF. Add
BedrockDocumentFormat union type, MIME-to-format mapping, and helpers
in data-provider so both client and backend can reference them.
* refactor: generalize Bedrock PDF validation to support all document types
Rename validateBedrockPdf to validateBedrockDocument with MIME-aware
logic: 4.5MB hard limit applies to all types, PDF header check only
runs for application/pdf. Adds test coverage for non-PDF documents.
* feat: support all Bedrock document formats in encoding pipeline
Widen file type gates to accept csv, doc, docx, xls, xlsx, html, txt,
md for Bedrock. Uses shared MIME-to-format map instead of hardcoded
'pdf'. Other providers' PDF-only paths remain unchanged.
* feat: expand Bedrock file upload UI to accept all document types
Add 'image_document_extended' upload type for Bedrock with accept
filters for all 9 supported formats. Update drag-and-drop validation
to use isBedrockDocumentType helper.
* fix: route Bedrock document types through provider pipeline
2026-02-23 19:32:44 -08:00
|
|
|
if (isBedrock && isBedrockDocumentType(mimeType)) {
|
|
|
|
|
const fileBuffer = Buffer.from(content, 'base64');
|
|
|
|
|
const format = bedrockDocumentFormats[mimeType];
|
|
|
|
|
|
|
|
|
|
const validation = await validateBedrockDocument(
|
|
|
|
|
fileBuffer.length,
|
|
|
|
|
mimeType,
|
|
|
|
|
fileBuffer,
|
|
|
|
|
configuredFileSizeLimit,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!validation.isValid) {
|
|
|
|
|
throw new Error(`Document validation failed: ${validation.error}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sanitizedName = (file.filename || 'document')
|
|
|
|
|
.replace(/[^a-zA-Z0-9\s\-()[\]]/g, '_')
|
|
|
|
|
.slice(0, 200);
|
|
|
|
|
result.documents.push({
|
|
|
|
|
type: 'document',
|
|
|
|
|
document: {
|
|
|
|
|
name: sanitizedName,
|
|
|
|
|
format,
|
|
|
|
|
source: {
|
|
|
|
|
bytes: fileBuffer,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-11-10 19:05:30 -05:00
|
|
|
});
|
🪨 feat: AWS Bedrock Document Uploads (#11912)
* feat: add aws bedrock upload to provider support
* chore: address copilot comments
* feat: add shared Bedrock document format types and MIME mapping
Bedrock Converse API accepts 9 document formats beyond PDF. Add
BedrockDocumentFormat union type, MIME-to-format mapping, and helpers
in data-provider so both client and backend can reference them.
* refactor: generalize Bedrock PDF validation to support all document types
Rename validateBedrockPdf to validateBedrockDocument with MIME-aware
logic: 4.5MB hard limit applies to all types, PDF header check only
runs for application/pdf. Adds test coverage for non-PDF documents.
* feat: support all Bedrock document formats in encoding pipeline
Widen file type gates to accept csv, doc, docx, xls, xlsx, html, txt,
md for Bedrock. Uses shared MIME-to-format map instead of hardcoded
'pdf'. Other providers' PDF-only paths remain unchanged.
* feat: expand Bedrock file upload UI to accept all document types
Add 'image_document_extended' upload type for Bedrock with accept
filters for all 9 supported formats. Update drag-and-drop validation
to use isBedrockDocumentType helper.
* fix: route Bedrock document types through provider pipeline
2026-02-23 19:32:44 -08:00
|
|
|
result.files.push(metadata);
|
|
|
|
|
} else if (file.type === 'application/pdf' && isDocSupported) {
|
|
|
|
|
const pdfBuffer = Buffer.from(content, 'base64');
|
2025-11-07 10:57:15 -05:00
|
|
|
|
|
|
|
|
const validation = await validatePdf(
|
|
|
|
|
pdfBuffer,
|
|
|
|
|
pdfBuffer.length,
|
|
|
|
|
provider,
|
|
|
|
|
configuredFileSizeLimit,
|
|
|
|
|
);
|
2025-10-06 17:30:16 -04:00
|
|
|
|
|
|
|
|
if (!validation.isValid) {
|
|
|
|
|
throw new Error(`PDF validation failed: ${validation.error}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (provider === Providers.ANTHROPIC) {
|
2025-10-16 23:24:14 +03:00
|
|
|
const document: AnthropicDocumentBlock = {
|
2025-10-06 17:30:16 -04:00
|
|
|
type: 'document',
|
|
|
|
|
source: {
|
|
|
|
|
type: 'base64',
|
|
|
|
|
media_type: 'application/pdf',
|
|
|
|
|
data: content,
|
|
|
|
|
},
|
|
|
|
|
citations: { enabled: true },
|
2025-10-16 23:24:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (file.filename) {
|
|
|
|
|
document.context = `File: "${file.filename}"`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.documents.push(document);
|
2025-10-06 17:30:16 -04:00
|
|
|
} else if (useResponsesApi) {
|
|
|
|
|
result.documents.push({
|
|
|
|
|
type: 'input_file',
|
|
|
|
|
filename: file.filename,
|
|
|
|
|
file_data: `data:application/pdf;base64,${content}`,
|
|
|
|
|
});
|
|
|
|
|
} else if (provider === Providers.GOOGLE || provider === Providers.VERTEXAI) {
|
|
|
|
|
result.documents.push({
|
2025-11-19 15:31:05 -08:00
|
|
|
type: 'media',
|
2025-10-06 17:30:16 -04:00
|
|
|
mimeType: 'application/pdf',
|
|
|
|
|
data: content,
|
|
|
|
|
});
|
|
|
|
|
} else if (isOpenAILikeProvider(provider) && provider != Providers.AZURE) {
|
|
|
|
|
result.documents.push({
|
|
|
|
|
type: 'file',
|
|
|
|
|
file: {
|
|
|
|
|
filename: file.filename,
|
|
|
|
|
file_data: `data:application/pdf;base64,${content}`,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
result.files.push(metadata);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|