mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* 👁️ feat: Add Azure Mistral OCR strategy and endpoint integration This commit introduces a new OCR strategy named 'azure_mistral_ocr', allowing the use of a Mistral OCR endpoint deployed on Azure. The configuration, schemas, and file upload strategies have been updated to support this integration, enabling seamless OCR processing via Azure-hosted Mistral services. * 🗑️ chore: Clean up .gitignore by removing commented-out uncommon directory name * chore: remove unused vars * refactor: Move createAxiosInstance to packages/api/utils and update imports - Removed the createAxiosInstance function from the config module and relocated it to a new utils module for better organization. - Updated import paths in relevant files to reflect the new location of createAxiosInstance. - Added tests for createAxiosInstance to ensure proper functionality and proxy configuration handling. * chore: move axios helpers to packages/api - Added logAxiosError function to @librechat/api for centralized error logging. - Updated imports across various files to use the new logAxiosError function. - Removed the old axios.js utility file as it is no longer needed. * chore: Update Jest moduleNameMapper for improved path resolution - Added a new mapping for '~/' to resolve module paths in Jest configuration, enhancing import handling for the project. * feat: Implement Mistral OCR API integration in TS * chore: Update MistralOCR tests based on new imports * fix: Enhance MistralOCR configuration handling and tests - Introduced helper functions for resolving configuration values from environment variables or hardcoded settings. - Updated the uploadMistralOCR and uploadAzureMistralOCR functions to utilize the new configuration resolution logic. - Improved test cases to ensure correct behavior when mixing environment variables and hardcoded values. - Mocked file upload and signed URL responses in tests to validate functionality without external dependencies. * feat: Enhance MistralOCR functionality with improved configuration and error handling - Introduced helper functions for loading authentication configuration and resolving values from environment variables. - Updated uploadMistralOCR and uploadAzureMistralOCR functions to utilize the new configuration logic. - Added utility functions for processing OCR results and creating error messages. - Improved document type determination and result aggregation for better OCR processing. * refactor: Reorganize OCR type imports in Mistral CRUD file - Moved OCRResult, OCRResultPage, and OCRImage imports to a more logical grouping for better readability and maintainability. * feat: Add file exports to API and create files index * chore: Update OCR types for enhanced structure and clarity - Redesigned OCRImage interface to include mandatory fields and improved naming conventions. - Added PageDimensions interface for better representation of page metrics. - Updated OCRResultPage to include dimensions and mandatory images array. - Refined OCRResult to include document annotation and usage information. * refactor: use TS counterpart of uploadOCR methods * ci: Update MistralOCR tests to reflect new OCR result structure * chore: Bump version of @librechat/api to 1.2.3 in package.json and package-lock.json * chore: Update CONFIG_VERSION to 1.2.8 * chore: remove unused sendEvent function from config module (now imported from '@librechat/api') * chore: remove MistralOCR service files and tests (now in '@librechat/api') * ci: update logger import in ModelService tests to use @librechat/data-schemas --------- Co-authored-by: arthurolivierfortin <arthurolivier.fortin@gmail.com>
195 lines
6 KiB
JavaScript
195 lines
6 KiB
JavaScript
const axios = require('axios');
|
|
const { logAxiosError } = require('@librechat/api');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const { TokenExchangeMethodEnum } = require('librechat-data-provider');
|
|
const { handleOAuthToken } = require('~/models/Token');
|
|
const { decryptV2 } = require('~/server/utils/crypto');
|
|
|
|
/**
|
|
* Processes the access tokens and stores them in the database.
|
|
* @param {object} tokenData
|
|
* @param {string} tokenData.access_token
|
|
* @param {number} tokenData.expires_in
|
|
* @param {string} [tokenData.refresh_token]
|
|
* @param {number} [tokenData.refresh_token_expires_in]
|
|
* @param {object} metadata
|
|
* @param {string} metadata.userId
|
|
* @param {string} metadata.identifier
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function processAccessTokens(tokenData, { userId, identifier }) {
|
|
const { access_token, expires_in = 3600, refresh_token, refresh_token_expires_in } = tokenData;
|
|
if (!access_token) {
|
|
logger.error('Access token not found: ', tokenData);
|
|
throw new Error('Access token not found');
|
|
}
|
|
await handleOAuthToken({
|
|
identifier,
|
|
token: access_token,
|
|
expiresIn: expires_in,
|
|
userId,
|
|
});
|
|
|
|
if (refresh_token != null) {
|
|
logger.debug('Processing refresh token');
|
|
await handleOAuthToken({
|
|
token: refresh_token,
|
|
type: 'oauth_refresh',
|
|
userId,
|
|
identifier: `${identifier}:refresh`,
|
|
expiresIn: refresh_token_expires_in ?? null,
|
|
});
|
|
}
|
|
logger.debug('Access tokens processed');
|
|
}
|
|
|
|
/**
|
|
* Refreshes the access token using the refresh token.
|
|
* @param {object} fields
|
|
* @param {string} fields.userId - The ID of the user.
|
|
* @param {string} fields.client_url - The URL of the OAuth provider.
|
|
* @param {string} fields.identifier - The identifier for the token.
|
|
* @param {string} fields.refresh_token - The refresh token to use.
|
|
* @param {string} fields.token_exchange_method - The token exchange method ('default_post' or 'basic_auth_header').
|
|
* @param {string} fields.encrypted_oauth_client_id - The client ID for the OAuth provider.
|
|
* @param {string} fields.encrypted_oauth_client_secret - The client secret for the OAuth provider.
|
|
* @returns {Promise<{
|
|
* access_token: string,
|
|
* expires_in: number,
|
|
* refresh_token?: string,
|
|
* refresh_token_expires_in?: number,
|
|
* }>}
|
|
*/
|
|
const refreshAccessToken = async ({
|
|
userId,
|
|
client_url,
|
|
identifier,
|
|
refresh_token,
|
|
token_exchange_method,
|
|
encrypted_oauth_client_id,
|
|
encrypted_oauth_client_secret,
|
|
}) => {
|
|
try {
|
|
const oauth_client_id = await decryptV2(encrypted_oauth_client_id);
|
|
const oauth_client_secret = await decryptV2(encrypted_oauth_client_secret);
|
|
|
|
const headers = {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
Accept: 'application/json',
|
|
};
|
|
|
|
const params = new URLSearchParams({
|
|
grant_type: 'refresh_token',
|
|
refresh_token,
|
|
});
|
|
|
|
if (token_exchange_method === TokenExchangeMethodEnum.BasicAuthHeader) {
|
|
const basicAuth = Buffer.from(`${oauth_client_id}:${oauth_client_secret}`).toString('base64');
|
|
headers['Authorization'] = `Basic ${basicAuth}`;
|
|
} else {
|
|
params.append('client_id', oauth_client_id);
|
|
params.append('client_secret', oauth_client_secret);
|
|
}
|
|
|
|
const response = await axios({
|
|
method: 'POST',
|
|
url: client_url,
|
|
headers,
|
|
data: params.toString(),
|
|
});
|
|
await processAccessTokens(response.data, {
|
|
userId,
|
|
identifier,
|
|
});
|
|
logger.debug(`Access token refreshed successfully for ${identifier}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
const message = 'Error refreshing OAuth tokens';
|
|
throw new Error(
|
|
logAxiosError({
|
|
message,
|
|
error,
|
|
}),
|
|
);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Handles the OAuth callback and exchanges the authorization code for tokens.
|
|
* @param {object} fields
|
|
* @param {string} fields.code - The authorization code returned by the provider.
|
|
* @param {string} fields.userId - The ID of the user.
|
|
* @param {string} fields.identifier - The identifier for the token.
|
|
* @param {string} fields.client_url - The URL of the OAuth provider.
|
|
* @param {string} fields.redirect_uri - The redirect URI for the OAuth provider.
|
|
* @param {string} fields.token_exchange_method - The token exchange method ('default_post' or 'basic_auth_header').
|
|
* @param {string} fields.encrypted_oauth_client_id - The client ID for the OAuth provider.
|
|
* @param {string} fields.encrypted_oauth_client_secret - The client secret for the OAuth provider.
|
|
* @returns {Promise<{
|
|
* access_token: string,
|
|
* expires_in: number,
|
|
* refresh_token?: string,
|
|
* refresh_token_expires_in?: number,
|
|
* }>}
|
|
*/
|
|
const getAccessToken = async ({
|
|
code,
|
|
userId,
|
|
identifier,
|
|
client_url,
|
|
redirect_uri,
|
|
token_exchange_method,
|
|
encrypted_oauth_client_id,
|
|
encrypted_oauth_client_secret,
|
|
}) => {
|
|
const oauth_client_id = await decryptV2(encrypted_oauth_client_id);
|
|
const oauth_client_secret = await decryptV2(encrypted_oauth_client_secret);
|
|
|
|
const headers = {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
Accept: 'application/json',
|
|
};
|
|
|
|
const params = new URLSearchParams({
|
|
code,
|
|
grant_type: 'authorization_code',
|
|
redirect_uri,
|
|
});
|
|
|
|
if (token_exchange_method === TokenExchangeMethodEnum.BasicAuthHeader) {
|
|
const basicAuth = Buffer.from(`${oauth_client_id}:${oauth_client_secret}`).toString('base64');
|
|
headers['Authorization'] = `Basic ${basicAuth}`;
|
|
} else {
|
|
params.append('client_id', oauth_client_id);
|
|
params.append('client_secret', oauth_client_secret);
|
|
}
|
|
|
|
try {
|
|
const response = await axios({
|
|
method: 'POST',
|
|
url: client_url,
|
|
headers,
|
|
data: params.toString(),
|
|
});
|
|
|
|
await processAccessTokens(response.data, {
|
|
userId,
|
|
identifier,
|
|
});
|
|
logger.debug(`Access tokens successfully created for ${identifier}`);
|
|
return response.data;
|
|
} catch (error) {
|
|
const message = 'Error exchanging OAuth code';
|
|
throw new Error(
|
|
logAxiosError({
|
|
message,
|
|
error,
|
|
}),
|
|
);
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
getAccessToken,
|
|
refreshAccessToken,
|
|
};
|