mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-22 19:30:15 +01:00
WIP: app.locals refactoring
WIP: appConfig fix: update memory configuration retrieval to use getAppConfig based on user role fix: update comment for AppConfig interface to clarify purpose
This commit is contained in:
parent
5a14ee9c6a
commit
b992fed16c
66 changed files with 706 additions and 366 deletions
|
|
@ -1,9 +1,10 @@
|
|||
import { primeResources } from './resources';
|
||||
import { logger } from '@librechat/data-schemas';
|
||||
import { EModelEndpoint, EToolResources, AgentCapabilities } from 'librechat-data-provider';
|
||||
import type { TAgentsEndpoint, TFile } from 'librechat-data-provider';
|
||||
import type { Request as ServerRequest } from 'express';
|
||||
import type { TFile } from 'librechat-data-provider';
|
||||
import type { TGetFiles } from './resources';
|
||||
import type { AppConfig } from '~/types';
|
||||
|
||||
// Mock logger
|
||||
jest.mock('@librechat/data-schemas', () => ({
|
||||
|
|
@ -14,6 +15,7 @@ jest.mock('@librechat/data-schemas', () => ({
|
|||
|
||||
describe('primeResources', () => {
|
||||
let mockReq: ServerRequest;
|
||||
let mockAppConfig: AppConfig;
|
||||
let mockGetFiles: jest.MockedFunction<TGetFiles>;
|
||||
let requestFileSet: Set<string>;
|
||||
|
||||
|
|
@ -32,6 +34,13 @@ describe('primeResources', () => {
|
|||
},
|
||||
} as unknown as ServerRequest;
|
||||
|
||||
// Setup mock appConfig
|
||||
mockAppConfig = {
|
||||
[EModelEndpoint.agents]: {
|
||||
capabilities: [AgentCapabilities.ocr],
|
||||
} as TAgentsEndpoint,
|
||||
} as AppConfig;
|
||||
|
||||
// Setup mock getFiles function
|
||||
mockGetFiles = jest.fn();
|
||||
|
||||
|
|
@ -65,6 +74,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments: undefined,
|
||||
|
|
@ -85,6 +95,7 @@ describe('primeResources', () => {
|
|||
describe('when OCR is disabled', () => {
|
||||
it('should not fetch OCR files even if tool_resources has OCR file_ids', async () => {
|
||||
(mockReq.app as ServerRequest['app']).locals[EModelEndpoint.agents].capabilities = [];
|
||||
(mockAppConfig[EModelEndpoint.agents] as TAgentsEndpoint).capabilities = [];
|
||||
|
||||
const tool_resources = {
|
||||
[EToolResources.ocr]: {
|
||||
|
|
@ -94,6 +105,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments: undefined,
|
||||
|
|
@ -129,6 +141,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -158,6 +171,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -189,6 +203,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -220,6 +235,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -250,6 +266,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -291,6 +308,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -342,6 +360,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -399,6 +418,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -450,6 +470,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -492,6 +513,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -560,6 +582,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -618,6 +641,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -671,6 +695,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -724,6 +749,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -764,6 +790,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -838,6 +865,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -888,6 +916,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -906,6 +935,7 @@ describe('primeResources', () => {
|
|||
// The function should now handle rejected attachment promises gracefully
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments,
|
||||
|
|
@ -928,9 +958,11 @@ describe('primeResources', () => {
|
|||
describe('edge cases', () => {
|
||||
it('should handle missing app.locals gracefully', async () => {
|
||||
const reqWithoutLocals = {} as ServerRequest;
|
||||
const emptyAppConfig = {} as AppConfig;
|
||||
|
||||
const result = await primeResources({
|
||||
req: reqWithoutLocals,
|
||||
appConfig: emptyAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments: undefined,
|
||||
|
|
@ -950,6 +982,7 @@ describe('primeResources', () => {
|
|||
it('should handle undefined tool_resources', async () => {
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet,
|
||||
attachments: undefined,
|
||||
|
|
@ -982,6 +1015,7 @@ describe('primeResources', () => {
|
|||
|
||||
const result = await primeResources({
|
||||
req: mockReq,
|
||||
appConfig: mockAppConfig,
|
||||
getFiles: mockGetFiles,
|
||||
requestFileSet: emptyRequestFileSet,
|
||||
attachments,
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import type { AgentToolResources, TFile, AgentBaseResource } from 'librechat-dat
|
|||
import type { FilterQuery, QueryOptions, ProjectionType } from 'mongoose';
|
||||
import type { IMongoFile, IUser } from '@librechat/data-schemas';
|
||||
import type { Request as ServerRequest } from 'express';
|
||||
import type { AppConfig } from '~/types/';
|
||||
|
||||
/**
|
||||
* Function type for retrieving files from the database
|
||||
|
|
@ -134,7 +135,8 @@ const categorizeFileForToolResources = ({
|
|||
* 4. Prevents duplicate files across all sources
|
||||
*
|
||||
* @param params - Parameters object
|
||||
* @param params.req - Express request object containing app configuration
|
||||
* @param params.req - Express request object
|
||||
* @param params.appConfig - Application configuration object
|
||||
* @param params.getFiles - Function to retrieve files from database
|
||||
* @param params.requestFileSet - Set of file IDs from the current request
|
||||
* @param params.attachments - Promise resolving to array of attachment files
|
||||
|
|
@ -143,6 +145,7 @@ const categorizeFileForToolResources = ({
|
|||
*/
|
||||
export const primeResources = async ({
|
||||
req,
|
||||
appConfig,
|
||||
getFiles,
|
||||
requestFileSet,
|
||||
attachments: _attachments,
|
||||
|
|
@ -150,6 +153,7 @@ export const primeResources = async ({
|
|||
agentId,
|
||||
}: {
|
||||
req: ServerRequest & { user?: IUser };
|
||||
appConfig: AppConfig;
|
||||
requestFileSet: Set<string>;
|
||||
attachments: Promise<Array<TFile | null>> | undefined;
|
||||
tool_resources: AgentToolResources | undefined;
|
||||
|
|
@ -198,7 +202,7 @@ export const primeResources = async ({
|
|||
}
|
||||
}
|
||||
|
||||
const isOCREnabled = (req.app.locals?.[EModelEndpoint.agents]?.capabilities ?? []).includes(
|
||||
const isOCREnabled = (appConfig?.[EModelEndpoint.agents]?.capabilities ?? []).includes(
|
||||
AgentCapabilities.ocr,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { ErrorTypes, EModelEndpoint, mapModelToAzureConfig } from 'librechat-data-provider';
|
||||
import type {
|
||||
UserKeyValues,
|
||||
InitializeOpenAIOptionsParams,
|
||||
OpenAIOptionsResult,
|
||||
OpenAIConfigOptions,
|
||||
InitializeOpenAIOptionsParams,
|
||||
UserKeyValues,
|
||||
} from '~/types';
|
||||
import { createHandleLLMNewToken } from '~/utils/generators';
|
||||
import { getAzureCredentials } from '~/utils/azure';
|
||||
|
|
@ -21,6 +21,7 @@ import { getOpenAIConfig } from './llm';
|
|||
*/
|
||||
export const initializeOpenAI = async ({
|
||||
req,
|
||||
appConfig,
|
||||
overrideModel,
|
||||
endpointOption,
|
||||
overrideEndpoint,
|
||||
|
|
@ -71,7 +72,7 @@ export const initializeOpenAI = async ({
|
|||
};
|
||||
|
||||
const isAzureOpenAI = endpoint === EModelEndpoint.azureOpenAI;
|
||||
const azureConfig = isAzureOpenAI && req.app.locals[EModelEndpoint.azureOpenAI];
|
||||
const azureConfig = isAzureOpenAI && appConfig[EModelEndpoint.azureOpenAI];
|
||||
|
||||
if (isAzureOpenAI && azureConfig) {
|
||||
const { modelGroupMap, groupMap } = azureConfig;
|
||||
|
|
@ -142,8 +143,8 @@ export const initializeOpenAI = async ({
|
|||
|
||||
const options = getOpenAIConfig(apiKey, finalClientOptions, endpoint);
|
||||
|
||||
const openAIConfig = req.app.locals[EModelEndpoint.openAI];
|
||||
const allConfig = req.app.locals.all;
|
||||
const openAIConfig = appConfig[EModelEndpoint.openAI];
|
||||
const allConfig = appConfig.all;
|
||||
const azureRate = modelName?.includes('gpt-4') ? 30 : 17;
|
||||
|
||||
let streamRate: number | undefined;
|
||||
|
|
|
|||
|
|
@ -46,7 +46,12 @@ import * as fs from 'fs';
|
|||
import axios from 'axios';
|
||||
import type { Request as ExpressRequest } from 'express';
|
||||
import type { Readable } from 'stream';
|
||||
import type { MistralFileUploadResponse, MistralSignedUrlResponse, OCRResult } from '~/types';
|
||||
import type {
|
||||
MistralFileUploadResponse,
|
||||
MistralSignedUrlResponse,
|
||||
OCRResult,
|
||||
AppConfig,
|
||||
} from '~/types';
|
||||
import { logger as mockLogger } from '@librechat/data-schemas';
|
||||
import {
|
||||
uploadDocumentToMistral,
|
||||
|
|
@ -497,18 +502,17 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
// Use environment variable syntax to ensure loadAuthValues is called
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-medium',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
// Use environment variable syntax to ensure loadAuthValues is called
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-medium',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -517,6 +521,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const result = await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -599,17 +604,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user456' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-medium',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-medium',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/image.png',
|
||||
originalname: 'image.png',
|
||||
|
|
@ -618,6 +622,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const result = await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -698,17 +703,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${CUSTOM_API_KEY}',
|
||||
baseURL: '${CUSTOM_BASEURL}',
|
||||
mistralModel: '${CUSTOM_MODEL}',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${CUSTOM_API_KEY}',
|
||||
baseURL: '${CUSTOM_BASEURL}',
|
||||
mistralModel: '${CUSTOM_MODEL}',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
// Set environment variable for model
|
||||
process.env.CUSTOM_MODEL = 'mistral-large';
|
||||
|
||||
|
|
@ -720,6 +724,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const result = await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -790,18 +795,17 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
// Use environment variable syntax to ensure loadAuthValues is called
|
||||
apiKey: '${INVALID_FORMAT}', // Using valid env var format but with an invalid name
|
||||
baseURL: '${OCR_BASEURL}', // Using valid env var format
|
||||
mistralModel: 'mistral-ocr-latest', // Plain string value
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
// Use environment variable syntax to ensure loadAuthValues is called
|
||||
apiKey: '${INVALID_FORMAT}', // Using valid env var format but with an invalid name
|
||||
baseURL: '${OCR_BASEURL}', // Using valid env var format
|
||||
mistralModel: 'mistral-ocr-latest', // Plain string value
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -810,6 +814,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -845,16 +850,15 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: 'OCR_API_KEY',
|
||||
baseURL: 'OCR_BASEURL',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: 'OCR_API_KEY',
|
||||
baseURL: 'OCR_BASEURL',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -864,6 +868,7 @@ describe('MistralOCR Service', () => {
|
|||
await expect(
|
||||
uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
}),
|
||||
|
|
@ -931,17 +936,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: 'OCR_API_KEY',
|
||||
baseURL: 'OCR_BASEURL',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: 'OCR_API_KEY',
|
||||
baseURL: 'OCR_BASEURL',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'single-page.pdf',
|
||||
|
|
@ -950,6 +954,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const result = await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1019,18 +1024,17 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
// Direct values that should be used as-is, without variable substitution
|
||||
apiKey: 'actual-api-key-value',
|
||||
baseURL: 'https://direct-api-url.mistral.ai/v1',
|
||||
mistralModel: 'mistral-direct-model',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
// Direct values that should be used as-is, without variable substitution
|
||||
apiKey: 'actual-api-key-value',
|
||||
baseURL: 'https://direct-api-url.mistral.ai/v1',
|
||||
mistralModel: 'mistral-direct-model',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'direct-values.pdf',
|
||||
|
|
@ -1039,6 +1043,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const result = await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1133,18 +1138,17 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
// Empty string values - should fall back to defaults
|
||||
apiKey: '',
|
||||
baseURL: '',
|
||||
mistralModel: '',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
// Empty string values - should fall back to defaults
|
||||
apiKey: '',
|
||||
baseURL: '',
|
||||
mistralModel: '',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'empty-config.pdf',
|
||||
|
|
@ -1153,6 +1157,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const result = await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1276,17 +1281,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${AZURE_MISTRAL_OCR_API_KEY}',
|
||||
baseURL: 'https://endpoint.models.ai.azure.com/v1',
|
||||
mistralModel: 'mistral-ocr-2503',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${AZURE_MISTRAL_OCR_API_KEY}',
|
||||
baseURL: 'https://endpoint.models.ai.azure.com/v1',
|
||||
mistralModel: 'mistral-ocr-2503',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -1295,6 +1299,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1360,17 +1365,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user456' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: 'hardcoded-api-key-12345',
|
||||
baseURL: '${CUSTOM_OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: 'hardcoded-api-key-12345',
|
||||
baseURL: '${CUSTOM_OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -1379,6 +1383,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1484,17 +1489,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -1503,6 +1507,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1553,17 +1558,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -1573,6 +1577,7 @@ describe('MistralOCR Service', () => {
|
|||
await expect(
|
||||
uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
}),
|
||||
|
|
@ -1641,17 +1646,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -1661,6 +1665,7 @@ describe('MistralOCR Service', () => {
|
|||
// Should not throw even if delete fails
|
||||
const result = await uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1701,17 +1706,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -1721,6 +1725,7 @@ describe('MistralOCR Service', () => {
|
|||
await expect(
|
||||
uploadMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
}),
|
||||
|
|
@ -1775,17 +1780,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${OCR_API_KEY}',
|
||||
baseURL: '${OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/azure-file.pdf',
|
||||
originalname: 'azure-document.pdf',
|
||||
|
|
@ -1794,6 +1798,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const result = await uploadAzureMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1851,17 +1856,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user123' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: '${AZURE_MISTRAL_OCR_API_KEY}',
|
||||
baseURL: 'https://endpoint.models.ai.azure.com/v1',
|
||||
mistralModel: 'mistral-ocr-2503',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: '${AZURE_MISTRAL_OCR_API_KEY}',
|
||||
baseURL: 'https://endpoint.models.ai.azure.com/v1',
|
||||
mistralModel: 'mistral-ocr-2503',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -1870,6 +1874,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
await uploadAzureMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
@ -1915,17 +1920,16 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
const req = {
|
||||
user: { id: 'user456' },
|
||||
app: {
|
||||
locals: {
|
||||
ocr: {
|
||||
apiKey: 'hardcoded-api-key-12345',
|
||||
baseURL: '${CUSTOM_OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
},
|
||||
},
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const appConfig = {
|
||||
ocr: {
|
||||
apiKey: 'hardcoded-api-key-12345',
|
||||
baseURL: '${CUSTOM_OCR_BASEURL}',
|
||||
mistralModel: 'mistral-ocr-latest',
|
||||
},
|
||||
} as AppConfig;
|
||||
|
||||
const file = {
|
||||
path: '/tmp/upload/file.pdf',
|
||||
originalname: 'document.pdf',
|
||||
|
|
@ -1934,6 +1938,7 @@ describe('MistralOCR Service', () => {
|
|||
|
||||
await uploadAzureMistralOCR({
|
||||
req,
|
||||
appConfig,
|
||||
file,
|
||||
loadAuthValues: mockLoadAuthValues,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import type {
|
|||
MistralOCRUploadResult,
|
||||
MistralOCRError,
|
||||
OCRResultPage,
|
||||
AppConfig,
|
||||
OCRResult,
|
||||
OCRImage,
|
||||
} from '~/types';
|
||||
|
|
@ -42,14 +43,10 @@ interface GoogleServiceAccount {
|
|||
|
||||
/** Helper type for OCR request context */
|
||||
interface OCRContext {
|
||||
req: Pick<ServerRequest, 'user' | 'app'> & {
|
||||
req: Pick<ServerRequest, 'user'> & {
|
||||
user?: { id: string };
|
||||
app: {
|
||||
locals?: {
|
||||
ocr?: TCustomConfig['ocr'];
|
||||
};
|
||||
};
|
||||
};
|
||||
appConfig: AppConfig;
|
||||
file: Express.Multer.File;
|
||||
loadAuthValues: (params: {
|
||||
userId: string;
|
||||
|
|
@ -241,7 +238,7 @@ async function resolveConfigValue(
|
|||
* Loads authentication configuration from OCR config
|
||||
*/
|
||||
async function loadAuthConfig(context: OCRContext): Promise<AuthConfig> {
|
||||
const ocrConfig = context.req.app.locals?.ocr;
|
||||
const ocrConfig = context.appConfig?.ocr;
|
||||
const apiKeyConfig = ocrConfig?.apiKey || '';
|
||||
const baseURLConfig = ocrConfig?.baseURL || '';
|
||||
|
||||
|
|
@ -357,6 +354,7 @@ function createOCRError(error: unknown, baseMessage: string): Error {
|
|||
* @param params - The params object.
|
||||
* @param params.req - The request object from Express. It should have a `user` property with an `id`
|
||||
* representing the user
|
||||
* @param params.appConfig - Application configuration object
|
||||
* @param params.file - The file object, which is part of the request. The file object should
|
||||
* have a `mimetype` property that tells us the file type
|
||||
* @param params.loadAuthValues - Function to load authentication values
|
||||
|
|
@ -372,7 +370,7 @@ export const uploadMistralOCR = async (context: OCRContext): Promise<MistralOCRU
|
|||
const authConfig = await loadAuthConfig(context);
|
||||
apiKey = authConfig.apiKey;
|
||||
baseURL = authConfig.baseURL;
|
||||
const model = getModelConfig(context.req.app.locals?.ocr);
|
||||
const model = getModelConfig(context.appConfig?.ocr);
|
||||
|
||||
const mistralFile = await uploadDocumentToMistral({
|
||||
filePath: context.file.path,
|
||||
|
|
@ -430,6 +428,7 @@ export const uploadMistralOCR = async (context: OCRContext): Promise<MistralOCRU
|
|||
* @param params - The params object.
|
||||
* @param params.req - The request object from Express. It should have a `user` property with an `id`
|
||||
* representing the user
|
||||
* @param params.appConfig - Application configuration object
|
||||
* @param params.file - The file object, which is part of the request. The file object should
|
||||
* have a `mimetype` property that tells us the file type
|
||||
* @param params.loadAuthValues - Function to load authentication values
|
||||
|
|
@ -441,7 +440,7 @@ export const uploadAzureMistralOCR = async (
|
|||
): Promise<MistralOCRUploadResult> => {
|
||||
try {
|
||||
const { apiKey, baseURL } = await loadAuthConfig(context);
|
||||
const model = getModelConfig(context.req.app.locals?.ocr);
|
||||
const model = getModelConfig(context.appConfig?.ocr);
|
||||
|
||||
const buffer = fs.readFileSync(context.file.path);
|
||||
const base64 = buffer.toString('base64');
|
||||
|
|
@ -644,6 +643,7 @@ async function performGoogleVertexOCR({
|
|||
* @param params - The params object.
|
||||
* @param params.req - The request object from Express. It should have a `user` property with an `id`
|
||||
* representing the user
|
||||
* @param params.appConfig - Application configuration object
|
||||
* @param params.file - The file object, which is part of the request. The file object should
|
||||
* have a `mimetype` property that tells us the file type
|
||||
* @param params.loadAuthValues - Function to load authentication values
|
||||
|
|
@ -655,7 +655,7 @@ export const uploadGoogleVertexMistralOCR = async (
|
|||
): Promise<MistralOCRUploadResult> => {
|
||||
try {
|
||||
const { serviceAccount, accessToken } = await loadGoogleAuthConfig();
|
||||
const model = getModelConfig(context.req.app.locals?.ocr);
|
||||
const model = getModelConfig(context.appConfig?.ocr);
|
||||
|
||||
const buffer = fs.readFileSync(context.file.path);
|
||||
const base64 = buffer.toString('base64');
|
||||
|
|
|
|||
78
packages/api/src/types/config.ts
Normal file
78
packages/api/src/types/config.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import type {
|
||||
TEndpoint,
|
||||
FileSources,
|
||||
TAzureConfig,
|
||||
TCustomConfig,
|
||||
TMemoryConfig,
|
||||
EModelEndpoint,
|
||||
TAgentsEndpoint,
|
||||
TAssistantEndpoint,
|
||||
} from 'librechat-data-provider';
|
||||
|
||||
/**
|
||||
* Application configuration object
|
||||
* Based on the configuration defined in api/server/services/Config/getAppConfig.js
|
||||
*/
|
||||
export interface AppConfig {
|
||||
/** The main custom configuration */
|
||||
config: TCustomConfig;
|
||||
/** OCR configuration */
|
||||
ocr?: TCustomConfig['ocr'];
|
||||
/** File paths configuration */
|
||||
paths: {
|
||||
uploads: string;
|
||||
imageOutput: string;
|
||||
publicPath: string;
|
||||
[key: string]: string;
|
||||
};
|
||||
/** Memory configuration */
|
||||
memory?: TMemoryConfig;
|
||||
/** Web search configuration */
|
||||
webSearch?: TCustomConfig['webSearch'];
|
||||
/** File storage strategy ('local', 's3', 'firebase', 'azure_blob') */
|
||||
fileStrategy: FileSources.local | FileSources.s3 | FileSources.firebase | FileSources.azure_blob;
|
||||
/** Social login configurations */
|
||||
socialLogins: Array<unknown>;
|
||||
/** Admin-filtered tools */
|
||||
filteredTools?: string[];
|
||||
/** Admin-included tools */
|
||||
includedTools?: string[];
|
||||
/** Image output type configuration */
|
||||
imageOutputType: string;
|
||||
/** Interface configuration */
|
||||
interfaceConfig?: TCustomConfig['interface'];
|
||||
/** Turnstile configuration */
|
||||
turnstileConfig?: TCustomConfig['registration'];
|
||||
/** Balance configuration */
|
||||
balance?: TCustomConfig['balance'];
|
||||
/** MCP server configuration */
|
||||
mcpConfig?: TCustomConfig['mcpServers'] | null;
|
||||
/** File configuration */
|
||||
fileConfig?: TCustomConfig['fileConfig'];
|
||||
/** Secure image links configuration */
|
||||
secureImageLinks?: TCustomConfig['secureImageLinks'];
|
||||
/** Processed model specifications */
|
||||
modelSpecs?: TCustomConfig['modelSpecs'];
|
||||
/** OpenAI endpoint configuration */
|
||||
openAI?: TEndpoint;
|
||||
/** Google endpoint configuration */
|
||||
google?: TEndpoint;
|
||||
/** Bedrock endpoint configuration */
|
||||
bedrock?: TEndpoint;
|
||||
/** Anthropic endpoint configuration */
|
||||
anthropic?: TEndpoint;
|
||||
/** GPT plugins endpoint configuration */
|
||||
gptPlugins?: TEndpoint;
|
||||
/** Azure OpenAI endpoint configuration */
|
||||
azureOpenAI?: TAzureConfig;
|
||||
/** Assistants endpoint configuration */
|
||||
assistants?: TAssistantEndpoint;
|
||||
/** Azure assistants endpoint configuration */
|
||||
azureAssistants?: TAssistantEndpoint;
|
||||
/** Agents endpoint configuration */
|
||||
[EModelEndpoint.agents]?: TAgentsEndpoint;
|
||||
/** Global endpoint configuration */
|
||||
all?: TEndpoint;
|
||||
/** Any additional endpoint configurations */
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
export * from './config';
|
||||
export * from './azure';
|
||||
export * from './balance';
|
||||
export * from './events';
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import type { TEndpointOption, TAzureConfig, TEndpoint } from 'librechat-data-pr
|
|||
import type { BindToolsInput } from '@langchain/core/language_models/chat_models';
|
||||
import type { OpenAIClientOptions, Providers } from '@librechat/agents';
|
||||
import type { AzureOptions } from './azure';
|
||||
import type { AppConfig } from './config';
|
||||
|
||||
export type OpenAIParameters = z.infer<typeof openAISchema>;
|
||||
|
||||
|
|
@ -85,6 +86,7 @@ export type CheckUserKeyExpiryFunction = (expiresAt: string, endpoint: string) =
|
|||
*/
|
||||
export interface InitializeOpenAIOptionsParams {
|
||||
req: RequestData;
|
||||
appConfig: AppConfig;
|
||||
overrideModel?: string;
|
||||
overrideEndpoint?: string;
|
||||
endpointOption: Partial<TEndpointOption>;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue