Skip to content

Commit 297fd9d

Browse files
committed
🔧 fix: S3 Download Stream with Key Extraction and Blob Storage Encoding for Vision (#6557)
1 parent 9f3bf97 commit 297fd9d

File tree

3 files changed

+79
-9
lines changed

3 files changed

+79
-9
lines changed

‎api/server/services/Files/S3/crud.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,41 @@ async function uploadFileToS3({ req, file, file_id, basePath = defaultBasePath }
135135
}
136136
}
137137

138+
/**
139+
* Extracts the S3 key from a full S3 URL.
140+
*
141+
* @param {string} s3Url - The full S3 URL
142+
* @returns {string} The S3 key
143+
*/
144+
function extractKeyFromS3Url(s3Url) {
145+
try {
146+
// Parse the URL
147+
const url = new URL(s3Url);
148+
// Extract the path from the URL, removing the leading slash
149+
let key = url.pathname.substring(1);
150+
151+
return key;
152+
} catch (error) {
153+
throw new Error(`Failed to extract key from S3 URL: ${error.message}`);
154+
}
155+
}
156+
138157
/**
139158
* Retrieves a readable stream for a file stored in S3.
140159
*
160+
* @param {ServerRequest} req - Server request object.
141161
* @param {string} filePath - The S3 key of the file.
142162
* @returns {Promise<NodeJS.ReadableStream>}
143163
*/
144-
async function getS3FileStream(filePath) {
145-
const params = { Bucket: bucketName, Key: filePath };
164+
async function getS3FileStream(_req, filePath) {
146165
try {
166+
const Key = extractKeyFromS3Url(filePath);
167+
const params = { Bucket: bucketName, Key };
147168
const s3 = initializeS3();
148169
const data = await s3.send(new GetObjectCommand(params));
149170
return data.Body; // Returns a Node.js ReadableStream.
150171
} catch (error) {
151-
logger.error('[getS3FileStream] Error retrieving S3 file stream:', error.message);
172+
logger.error('[getS3FileStream] Error retrieving S3 file stream:', error);
152173
throw error;
153174
}
154175
}

‎api/server/services/Files/images/encode.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ const base64Only = new Set([
3737
EModelEndpoint.bedrock,
3838
]);
3939

40+
const blobStorageSources = new Set([FileSources.azure, FileSources.s3]);
41+
4042
/**
4143
* Encodes and formats the given files.
4244
* @param {Express.Request} req - The request object.
4345
* @param {Array<MongoFile>} files - The array of files to encode and format.
4446
* @param {EModelEndpoint} [endpoint] - Optional: The endpoint for the image.
4547
* @param {string} [mode] - Optional: The endpoint mode for the image.
46-
* @returns {Promise<Object>} - A promise that resolves to the result object containing the encoded images and file details.
48+
* @returns {Promise<{ text: string; files: MongoFile[]; image_urls: MessageContentImageUrl[] }>} - A promise that resolves to the result object containing the encoded images and file details.
4749
*/
4850
async function encodeAndFormat(req, files, endpoint, mode) {
4951
const promises = [];
52+
/** @type {Record<FileSources, Pick<ReturnType<typeof getStrategyFunctions>, 'prepareImagePayload' | 'getDownloadStream'>>} */
5053
const encodingMethods = {};
54+
/** @type {{ text: string; files: MongoFile[]; image_urls: MessageContentImageUrl[] }} */
5155
const result = {
5256
text: '',
5357
files: [],
@@ -59,6 +63,7 @@ async function encodeAndFormat(req, files, endpoint, mode) {
5963
}
6064

6165
for (let file of files) {
66+
/** @type {FileSources} */
6267
const source = file.source ?? FileSources.local;
6368
if (source === FileSources.text && file.text) {
6469
result.text += `${!result.text ? 'Attached document(s):\n```md' : '\n\n---\n\n'}# "${file.filename}"\n${file.text}\n`;
@@ -70,18 +75,51 @@ async function encodeAndFormat(req, files, endpoint, mode) {
7075
}
7176

7277
if (!encodingMethods[source]) {
73-
const { prepareImagePayload } = getStrategyFunctions(source);
78+
const { prepareImagePayload, getDownloadStream } = getStrategyFunctions(source);
7479
if (!prepareImagePayload) {
7580
throw new Error(`Encoding function not implemented for ${source}`);
7681
}
7782

78-
encodingMethods[source] = prepareImagePayload;
83+
encodingMethods[source] = { prepareImagePayload, getDownloadStream };
7984
}
8085

81-
const preparePayload = encodingMethods[source];
86+
const preparePayload = encodingMethods[source].prepareImagePayload;
87+
/* We need to fetch the image and convert it to base64 if we are using S3/Azure Blob storage. */
88+
if (blobStorageSources.has(source)) {
89+
try {
90+
const downloadStream = encodingMethods[source].getDownloadStream;
91+
const stream = await downloadStream(req, file.filepath);
92+
const streamPromise = new Promise((resolve, reject) => {
93+
/** @type {Uint8Array[]} */
94+
const chunks = [];
95+
stream.on('readable', () => {
96+
let chunk;
97+
while (null !== (chunk = stream.read())) {
98+
chunks.push(chunk);
99+
}
100+
});
101+
102+
stream.on('end', () => {
103+
const buffer = Buffer.concat(chunks);
104+
const base64Data = buffer.toString('base64');
105+
resolve(base64Data);
106+
});
107+
stream.on('error', (error) => {
108+
reject(error);
109+
});
110+
});
111+
const base64Data = await streamPromise;
112+
promises.push([file, base64Data]);
113+
} catch (error) {
114+
logger.error(
115+
`Error processing blob storage file stream for ${file.name} base64 payload:`,
116+
error,
117+
);
118+
continue;
119+
}
82120

83-
/* Google & Anthropic don't support passing URLs to payload */
84-
if (source !== FileSources.local && base64Only.has(endpoint)) {
121+
/* Google & Anthropic don't support passing URLs to payload */
122+
} else if (source !== FileSources.local && base64Only.has(endpoint)) {
85123
const [_file, imageURL] = await preparePayload(req, file);
86124
promises.push([_file, await fetchImageToBase64(imageURL)]);
87125
continue;

‎api/typedefs.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,12 @@
403403
* @memberof typedefs
404404
*/
405405

406+
/**
407+
* @exports MessageContentImageUrl
408+
* @typedef {import('librechat-data-provider').Agents.MessageContentImageUrl} MessageContentImageUrl
409+
* @memberof typedefs
410+
*/
411+
406412
/** Prompts */
407413
/**
408414
* @exports TPrompt
@@ -759,6 +765,11 @@
759765
* @typedef {import('mongoose').Schema} MongooseSchema
760766
* @memberof typedefs
761767
*/
768+
/**
769+
* @exports MongoFile
770+
* @typedef {import('@librechat/data-schemas').IMongoFile} MongoFile
771+
* @memberof typedefs
772+
*/
762773

763774
/**
764775
* @exports ObjectId

0 commit comments

Comments
 (0)