Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🔧 fix: Azure Blob Integration and File Source References (#6575)
Ruben Talstrabc039cea2912
diff --git a/.gitignore b/.gitignoreindex c4477db921..bd3b596c81 100644--- a/.gitignore+++ b/.gitignore@@ -37,6 +37,10 @@ client/public/main.jsclient/public/main.js.mapclient/public/main.js.LICENSE.txt+# Azure Blob Storage Emulator (Azurite)+__azurite**+__blobstorage__/**/*+# Dependency directorys# Deployed apps should consider commenting these lines out:# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-gitdiff --git a/api/server/services/AppService.js b/api/server/services/AppService.jsindex baead97448..8715997126 100644--- a/api/server/services/AppService.js+++ b/api/server/services/AppService.js@@ -52,7 +52,7 @@ const AppService = async (app) => {if (fileStrategy === FileSources.firebase) {initializeFirebase();- } else if (fileStrategy === FileSources.azure) {+ } else if (fileStrategy === FileSources.azure_blob) {initializeAzureBlobService();} else if (fileStrategy === FileSources.s3) {initializeS3();diff --git a/api/server/services/Files/Azure/crud.js b/api/server/services/Files/Azure/crud.jsindex 638da34b27..cb52de8317 100644--- a/api/server/services/Files/Azure/crud.js+++ b/api/server/services/Files/Azure/crud.js@@ -1,11 +1,13 @@const fs = require('fs');const path = require('path');+const mime = require('mime');const axios = require('axios');const fetch = require('node-fetch');const { logger } = require('~/config');const { getAzureContainerClient } = require('./initialize');const defaultBasePath = 'images';+const { AZURE_STORAGE_PUBLIC_ACCESS = 'true', AZURE_CONTAINER_NAME = 'files' } = process.env;/*** Uploads a buffer to Azure Blob Storage.@@ -29,10 +31,9 @@ async function saveBufferToAzure({}) {try {const containerClient = getAzureContainerClient(containerName);+ const access = AZURE_STORAGE_PUBLIC_ACCESS?.toLowerCase() === 'true' ? 'blob' : undefined;// Create the container if it doesn't exist. This is done per operation.- await containerClient.createIfNotExists({- access: process.env.AZURE_STORAGE_PUBLIC_ACCESS ? 'blob' : undefined,- });+ await containerClient.createIfNotExists({ access });const blobPath = `${basePath}/${userId}/${fileName}`;const blockBlobClient = containerClient.getBlockBlobClient(blobPath);await blockBlobClient.uploadData(buffer);@@ -97,28 +98,83 @@ async function getAzureURL({ fileName, basePath = defaultBasePath, userId, conta* Deletes a blob from Azure Blob Storage.** @param {Object} params- * @param {string} params.fileName - The name of the file.- * @param {string} [params.basePath='images'] - The base folder where the file is stored.+ * @param {ServerRequest} params.req - The Express request object.+ * @param {MongoFile} params.file - The file object.+ */+async function deleteFileFromAzure(req, file) {+ try {+ const containerClient = getAzureContainerClient(AZURE_CONTAINER_NAME);+ const blobPath = file.filepath.split(`${AZURE_CONTAINER_NAME}/`)[1];+ if (!blobPath.includes(req.user.id)) {+ throw new Error('User ID not found in blob path');+ }+ const blockBlobClient = containerClient.getBlockBlobClient(blobPath);+ await blockBlobClient.delete();+ logger.debug('[deleteFileFromAzure] Blob deleted successfully from Azure Blob Storage');+ } catch (error) {+ logger.error('[deleteFileFromAzure] Error deleting blob:', error);+ if (error.statusCode === 404) {+ return;+ }+ throw error;+ }+}++/**+ * Streams a file from disk directly to Azure Blob Storage without loading+ * the entire file into memory.+ *+ * @param {Object} params* @param {string} params.userId - The user's id.+ * @param {string} params.filePath - The local file path to upload.+ * @param {string} params.fileName - The name of the file in Azure.+ * @param {string} [params.basePath='images'] - The base folder within the container.* @param {string} [params.containerName] - The Azure Blob container name.+ * @returns {Promise<string>} The URL of the uploaded blob.*/-async function deleteFileFromAzure({+async function streamFileToAzure({+ userId,+ filePath,fileName,basePath = defaultBasePath,- userId,containerName,}) {try {const containerClient = getAzureContainerClient(containerName);+ const access = AZURE_STORAGE_PUBLIC_ACCESS?.toLowerCase() === 'true' ? 'blob' : undefined;++ // Create the container if it doesn't exist+ await containerClient.createIfNotExists({ access });+const blobPath = `${basePath}/${userId}/${fileName}`;const blockBlobClient = containerClient.getBlockBlobClient(blobPath);- await blockBlobClient.delete();- logger.debug('[deleteFileFromAzure] Blob deleted successfully from Azure Blob Storage');++ // Get file size for proper content length+ const stats = await fs.promises.stat(filePath);++ // Create read stream from the file+ const fileStream = fs.createReadStream(filePath);++ const blobContentType = mime.getType(fileName);+ await blockBlobClient.uploadStream(+ fileStream,+ undefined, // Use default concurrency (5)+ undefined, // Use default buffer size (8MB)+ {+ blobHTTPHeaders: {+ blobContentType,+ },+ onProgress: (progress) => {+ logger.debug(+ `[streamFileToAzure] Upload progress: ${progress.loadedBytes} bytes of ${stats.size}`,+ );+ },+ },+ );++ return blockBlobClient.url;} catch (error) {- logger.error('[deleteFileFromAzure] Error deleting blob:', error.message);- if (error.statusCode === 404) {- return;- }+ logger.error('[streamFileToAzure] Error streaming file:', error);throw error;}}@@ -146,18 +202,19 @@ async function uploadFileToAzure({}) {try {const inputFilePath = file.path;- const inputBuffer = await fs.promises.readFile(inputFilePath);- const bytes = Buffer.byteLength(inputBuffer);+ const stats = await fs.promises.stat(inputFilePath);+ const bytes = stats.size;const userId = req.user.id;const fileName = `${file_id}__${path.basename(inputFilePath)}`;- const fileURL = await saveBufferToAzure({++ const fileURL = await streamFileToAzure({userId,- buffer: inputBuffer,+ filePath: inputFilePath,fileName,basePath,containerName,});- await fs.promises.unlink(inputFilePath);+return { filepath: fileURL, bytes };} catch (error) {logger.error('[uploadFileToAzure] Error uploading file:', error);diff --git a/api/server/services/Files/images/encode.js b/api/server/services/Files/images/encode.jsindex 759e363785..85d6513977 100644--- a/api/server/services/Files/images/encode.js+++ b/api/server/services/Files/images/encode.js@@ -37,7 +37,7 @@ const base64Only = new Set([EModelEndpoint.bedrock,]);-const blobStorageSources = new Set([FileSources.azure, FileSources.s3]);+const blobStorageSources = new Set([FileSources.azure_blob, FileSources.s3]);/*** Encodes and formats the given files.diff --git a/api/server/services/Files/process.js b/api/server/services/Files/process.jsindex 78a4976e2f..384955dabf 100644--- a/api/server/services/Files/process.js+++ b/api/server/services/Files/process.js@@ -492,7 +492,7 @@ const processAgentFileUpload = async ({ req, res, metadata }) => {let fileInfoMetadata;const entity_id = messageAttachment === true ? undefined : agent_id;-+ const basePath = mime.getType(file.originalname)?.startsWith('image') ? 'images' : 'uploads';if (tool_resource === EToolResources.execute_code) {const isCodeEnabled = await checkCapability(req, AgentCapabilities.execute_code);if (!isCodeEnabled) {@@ -532,7 +532,7 @@ const processAgentFileUpload = async ({ req, res, metadata }) => {images,filename,filepath: ocrFileURL,- } = await handleFileUpload({ req, file, file_id, entity_id: agent_id });+ } = await handleFileUpload({ req, file, file_id, entity_id: agent_id, basePath });const fileInfo = removeNullishValues({text,@@ -582,6 +582,7 @@ const processAgentFileUpload = async ({ req, res, metadata }) => {file,file_id,entity_id,+ basePath,});let filepath = _filepath;diff --git a/api/server/services/Files/strategies.js b/api/server/services/Files/strategies.jsindex d05ea03728..c6cfe77069 100644--- a/api/server/services/Files/strategies.js+++ b/api/server/services/Files/strategies.js@@ -211,6 +211,8 @@ const getStrategyFunctions = (fileSource) => {} else if (fileSource === FileSources.openai) {return openAIStrategy();} else if (fileSource === FileSources.azure) {+ return openAIStrategy();+ } else if (fileSource === FileSources.azure_blob) {return azureStrategy();} else if (fileSource === FileSources.vectordb) {return vectorStrategy();diff --git a/client/src/components/Chat/Input/Files/SourceIcon.tsx b/client/src/components/Chat/Input/Files/SourceIcon.tsxindex c3b2a4423c..677de452ff 100644--- a/client/src/components/Chat/Input/Files/SourceIcon.tsx+++ b/client/src/components/Chat/Input/Files/SourceIcon.tsx@@ -10,7 +10,8 @@ const sourceToEndpoint = {const sourceToClassname = {[FileSources.openai]: 'bg-white/75 dark:bg-black/65',- [FileSources.azure]: 'azure-bg-color opacity-85',+ [FileSources.azure]: 'azure-bg-color',+ [FileSources.azure_blob]: 'azure-bg-color',[FileSources.execute_code]: 'bg-black text-white opacity-85',[FileSources.text]: 'bg-blue-500 dark:bg-blue-900 opacity-85 text-white',[FileSources.vectordb]: 'bg-yellow-700 dark:bg-yellow-900 opacity-85 text-white',diff --git a/client/src/hooks/Endpoint/useSelectorEffects.ts b/client/src/hooks/Endpoint/useSelectorEffects.tsindex b9a245f2f7..ecfc51c309 100644--- a/client/src/hooks/Endpoint/useSelectorEffects.ts+++ b/client/src/hooks/Endpoint/useSelectorEffects.ts@@ -61,7 +61,7 @@ export default function useSelectorEffects({}const assistant = assistantsMap?.[endpoint ?? '']?.[assistant_id];if (assistant !== undefined) {- setOption('model')('');+ setOption('model')(assistant.model);setOption('assistant_id')(assistant_id);}}diff --git a/packages/data-provider/src/types/files.ts b/packages/data-provider/src/types/files.tsindex 78ef7781e8..2313af823b 100644--- a/packages/data-provider/src/types/files.ts+++ b/packages/data-provider/src/types/files.ts@@ -4,6 +4,7 @@ export enum FileSources {local = 'local',firebase = 'firebase',azure = 'azure',+ azure_blob = 'azure_blob',openai = 'openai',s3 = 's3',vectordb = 'vectordb',