From b38413e92d5168dde2d7dbdb5131a86eed371c8b Mon Sep 17 00:00:00 2001 From: haddadrm <121486289+haddadrm@users.noreply.github.com> Date: Sat, 15 Feb 2025 15:24:24 +0400 Subject: [PATCH] Update config.ts resolve conflict --- src/config.ts | 111 ++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/src/config.ts b/src/config.ts index c0556b3..c1a80ed 100644 --- a/src/config.ts +++ b/src/config.ts @@ -10,15 +10,32 @@ interface Config { SIMILARITY_MEASURE: string; KEEP_ALIVE: string; }; - API_KEYS: { - OPENAI: string; - GROQ: string; - ANTHROPIC: string; - GEMINI: string; + MODELS: { + OPENAI: { + API_KEY: string; + }; + GROQ: { + API_KEY: string; + }; + ANTHROPIC: { + API_KEY: string; + }; + GEMINI: { + API_KEY: string; + }; + OLLAMA: { + API_URL: string; + }; + LMSTUDIO: { + API_URL: string; + }; + CUSTOM_OPENAI: { + API_URL: string; + API_KEY: string; + MODEL_NAME: string; + }; }; - API_ENDPOINTS: { - OLLAMA: string; - LMSTUDIO: string; + API_ENDPOINTS: { SEARXNG: string; }; } @@ -39,65 +56,63 @@ export const getSimilarityMeasure = () => export const getKeepAlive = () => loadConfig().GENERAL.KEEP_ALIVE; -export const getOpenaiApiKey = () => loadConfig().API_KEYS.OPENAI; +export const getOpenaiApiKey = () => loadConfig().MODELS.OPENAI.API_KEY; -export const getGroqApiKey = () => loadConfig().API_KEYS.GROQ; +export const getGroqApiKey = () => loadConfig().MODELS.GROQ.API_KEY; -export const getAnthropicApiKey = () => loadConfig().API_KEYS.ANTHROPIC; +export const getAnthropicApiKey = () => loadConfig().MODELS.ANTHROPIC.API_KEY; -export const getGeminiApiKey = () => loadConfig().API_KEYS.GEMINI; +export const getGeminiApiKey = () => loadConfig().MODELS.GEMINI.API_KEY; export const getSearxngApiEndpoint = () => process.env.SEARXNG_API_URL || loadConfig().API_ENDPOINTS.SEARXNG; -export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA; +export const getOllamaApiEndpoint = () => loadConfig().MODELS.OLLAMA.API_URL; -export const getLMStudioApiEndpoint = () => loadConfig().API_ENDPOINTS.LMSTUDIO; +export const getLMStudioApiEndpoint = () => loadConfig().MODELS.LMSTUDIO.API_URL; -export const updateConfig = (config: RecursivePartial) => { - const currentConfig = loadConfig(); +export const getCustomOpenaiApiKey = () => + loadConfig().MODELS.CUSTOM_OPENAI.API_KEY; - for (const key in currentConfig) { - if (!config[key]) config[key] = {}; +export const getCustomOpenaiApiUrl = () => + loadConfig().MODELS.CUSTOM_OPENAI.API_URL; - if (typeof currentConfig[key] === 'object' && currentConfig[key] !== null) { - for (const nestedKey in currentConfig[key]) { - if ( - !config[key][nestedKey] && - currentConfig[key][nestedKey] && - config[key][nestedKey] !== '' - ) { - config[key][nestedKey] = currentConfig[key][nestedKey]; - } +export const getCustomOpenaiModelName = () => + loadConfig().MODELS.CUSTOM_OPENAI.MODEL_NAME; + +const mergeConfigs = (current: any, update: any): any => { + if (typeof current !== 'object' || current === null) { + return update; + } + + const result = { ...current }; + + for (const key in update) { + if (Object.prototype.hasOwnProperty.call(update, key)) { + const updateValue = update[key]; + + if ( + typeof updateValue === 'object' && + updateValue !== null && + typeof result[key] === 'object' && + result[key] !== null + ) { + result[key] = mergeConfigs(result[key], updateValue); + } else if (updateValue !== undefined) { + result[key] = updateValue; } - } else if (currentConfig[key] && config[key] !== '') { - config[key] = currentConfig[key]; } } - /* + return result; +}; + export const updateConfig = (config: RecursivePartial) => { const currentConfig = loadConfig(); - - // Merge existing config with new values - const mergedConfig: RecursivePartial = { - GENERAL: { - ...currentConfig.GENERAL, - ...config.GENERAL, - }, - API_KEYS: { - ...currentConfig.API_KEYS, - ...config.API_KEYS, - }, - API_ENDPOINTS: { - ...currentConfig.API_ENDPOINTS, - ...config.API_ENDPOINTS, - }, - }; -*/ + const mergedConfig = mergeConfigs(currentConfig, config); fs.writeFileSync( path.join(__dirname, `../${configFileName}`), - toml.stringify(config), + toml.stringify(mergedConfig), ); };