2025-10-06 17:30:16 -04:00
|
|
|
import getStream from 'get-stream';
|
2025-11-07 10:57:15 -05:00
|
|
|
import { Providers } from '@librechat/agents';
|
|
|
|
|
import { FileSources, mergeFileConfig } 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 { 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
|
|
|
|
|
* @returns The configured file size limit in bytes, or undefined if not configured
|
|
|
|
|
*/
|
|
|
|
|
export const getConfiguredFileSizeLimit = (
|
|
|
|
|
req: ServerRequest,
|
|
|
|
|
provider: Providers,
|
|
|
|
|
): number | undefined => {
|
|
|
|
|
if (!req.config?.fileConfig) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
const fileConfig = mergeFileConfig(req.config.fileConfig);
|
|
|
|
|
const endpointConfig = fileConfig.endpoints[provider] ?? fileConfig.endpoints.default;
|
|
|
|
|
return endpointConfig?.fileSizeLimit;
|
|
|
|
|
};
|
2025-10-06 17:30:16 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Processes a file by downloading and encoding it to base64
|
|
|
|
|
* @param req - Express request object
|
|
|
|
|
* @param file - File object to process
|
|
|
|
|
* @param encodingMethods - Cache of encoding methods by source
|
|
|
|
|
* @param getStrategyFunctions - Function to get strategy functions for a source
|
|
|
|
|
* @returns Processed file with content and metadata, or null if filepath missing
|
|
|
|
|
*/
|
|
|
|
|
export async function getFileStream(
|
2025-11-07 10:57:15 -05:00
|
|
|
req: ServerRequest,
|
2025-10-06 17:30:16 -04:00
|
|
|
file: IMongoFile,
|
|
|
|
|
encodingMethods: Record<string, StrategyFunctions>,
|
|
|
|
|
getStrategyFunctions: (source: string) => StrategyFunctions,
|
|
|
|
|
): Promise<ProcessedFile | null> {
|
|
|
|
|
if (!file?.filepath) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const source = file.source ?? FileSources.local;
|
|
|
|
|
if (!encodingMethods[source]) {
|
|
|
|
|
encodingMethods[source] = getStrategyFunctions(source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { getDownloadStream } = encodingMethods[source];
|
|
|
|
|
const stream = await getDownloadStream(req, file.filepath);
|
|
|
|
|
const buffer = await getStream.buffer(stream);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
file,
|
|
|
|
|
content: buffer.toString('base64'),
|
|
|
|
|
metadata: {
|
|
|
|
|
file_id: file.file_id,
|
|
|
|
|
temp_file_id: file.temp_file_id,
|
|
|
|
|
filepath: file.filepath,
|
|
|
|
|
source: file.source,
|
|
|
|
|
filename: file.filename,
|
|
|
|
|
type: file.type,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|