From 1942656783983543bec78252f38eb0fa8b94ac94 Mon Sep 17 00:00:00 2001 From: Sergey Konovalov Date: Thu, 29 May 2025 03:40:40 +0300 Subject: [PATCH] [feature]Fix local.json replace; Change aiSettings.actions type --- DocService/sources/DocsCoServer.js | 9 +--- DocService/sources/ai/aiEngineWrapper.js | 8 ++-- DocService/sources/ai/aiProxyHandler.js | 61 +++++++++--------------- DocService/sources/routes/config.js | 6 ++- branding/info/js/ai-interface.js | 6 +-- 5 files changed, 36 insertions(+), 54 deletions(-) diff --git a/DocService/sources/DocsCoServer.js b/DocService/sources/DocsCoServer.js index cad34c18..e6cd4a38 100644 --- a/DocService/sources/DocsCoServer.js +++ b/DocService/sources/DocsCoServer.js @@ -3483,13 +3483,6 @@ exports.install = function(server, callbackFunction) { let [licenseInfo] = yield tenantManager.getTenantLicense(ctx); let pluginSettings = yield aiProxyHandler.getPluginSettings(ctx); - if (pluginSettings.actions) { - const tmp = pluginSettings.actions; - pluginSettings.actions = {}; - for (let i = 0; i < tmp.length; i++) { - pluginSettings.actions[tmp[i].id] = tmp[i]; - } - } sendData(ctx, conn, { type: 'license', license: { @@ -3987,6 +3980,8 @@ exports.install = function(server, callbackFunction) { ); }); }); + + void aiProxyHandler.getPluginSettings(operationContext.global); }; exports.setLicenseInfo = async function(globalCtx, data, original) { tenantManager.setDefLicense(data, original); diff --git a/DocService/sources/ai/aiEngineWrapper.js b/DocService/sources/ai/aiEngineWrapper.js index 60a20956..de809128 100644 --- a/DocService/sources/ai/aiEngineWrapper.js +++ b/DocService/sources/ai/aiEngineWrapper.js @@ -155,10 +155,10 @@ function loadInternalProviders() { */ function fillConfigObjects() { AI.Models = cfgAiApiModels; - for(let i = 0; i < cfgAiApiActions.length; i++) - { - if (cfgAiApiActions[i].model && AI.Actions[cfgAiApiActions[i].id]) { - AI.Actions[cfgAiApiActions[i].id].model = cfgAiApiActions[i].model; + for(let id in cfgAiApiActions) { + let action = cfgAiApiActions[id]; + if (action.model && AI.Actions[id]) { + AI.Actions[id].model = action.model; } } } diff --git a/DocService/sources/ai/aiProxyHandler.js b/DocService/sources/ai/aiProxyHandler.js index 8f760965..0a98860a 100644 --- a/DocService/sources/ai/aiProxyHandler.js +++ b/DocService/sources/ai/aiProxyHandler.js @@ -282,43 +282,23 @@ async function getPluginSettings(ctx) { const aiApi = config.get('aiSettings'); // Process providers and their models if configuration exists if (aiApi?.providers && typeof aiApi.providers === 'object') { - // Create an array of promises for each provider - // const providerPromises = aiApi.providers - // .filter(provider => provider.enable !== false || !provider.key || !provider.url) - // .map(provider => processProvider(ctx, provider)); - - // try { - // let providers = await Promise.allSettled(providerPromises); - // // providers = providers.filter(provider => provider.status === 'fulfilled' && provider.value && provider.value.name && provider.value.models?.length > 0); - // providers = providers.filter(provider => provider.status === 'fulfilled' && provider.value && provider.value.name); - - // const providerCount = providers.length; - // let totalModels = 0; - // // Convert providers array to object by provider name - // result.providers = {}; - // for(let i = 0; i < providers.length; i++) { - // const provider = providers[i].value; - // totalModels += provider.models.length; - // // result.models.push(...provider.modelsUI); - // delete provider.modelsUI;//todo remove - // //result.providers[provider.name] = provider; - // } - - // logger.info(`Successfully processed ${providerCount} providers with a total of ${totalModels} models`); - // } catch (error) { - // logger.error('Error resolving provider promises:', error); - // } - if (true) { - const providers = AI.serializeProviders(); - for (let i = 0; i < providers.length; i++) { - const provider = providers[i]; - const cfgProvider = aiApi.providers[provider.name]; - if (cfgProvider) { - //todo clone - provider.key = cfgProvider.key; - } - result.providers[provider.name] = provider; + const providers = AI.serializeProviders(); + for (let i = 0; i < providers.length; i++) { + const provider = providers[i]; + const cfgProvider = aiApi.providers[provider.name]; + if (cfgProvider) { + //todo clone + provider.key = cfgProvider.key; } + + try { + const providerProcessed = await processProvider(ctx, provider); + provider.models.push(...providerProcessed.models); + } catch (error) { + logger.warn('Error processing provider:', error); + } + + result.providers[provider.name] = provider; } } // Process AI actions @@ -330,10 +310,15 @@ async function getPluginSettings(ctx) { // Process AI actions if (aiApi?.actions && typeof aiApi.actions === 'object') { // result.actions = aiApi.actions; - result.actions = AI.ActionsGetSorted(); + const actionSoted = AI.ActionsGetSorted(); + result.actions = {}; + for (let i = 0; i < actionSoted.length; i++) { + const action = actionSoted[i]; + result.actions[action.id] = action; + } } result.version = aiApi.version; - // nodeCache.set(ctx.tenant, result); + nodeCache.set(ctx.tenant, result); } catch (error) { logger.error('Error retrieving AI models from config:', error); } diff --git a/DocService/sources/routes/config.js b/DocService/sources/routes/config.js index 9021ee26..561972fa 100644 --- a/DocService/sources/routes/config.js +++ b/DocService/sources/routes/config.js @@ -92,9 +92,13 @@ router.post('/', rawFileParser, async (req, res) => { ctx.logger.debug('Configuration backup not found: %s', backupError.stack); } if(!sampleFileStat){ + await cp(configPath, backupPath, {force: true, recursive: true}); + } + try { const oldConfig = JSON.parse(await readFile(configPath, {encoding: 'utf8'})); newConfig = {...oldConfig, ...newConfig}; - await cp(configPath, backupPath, {force: true, recursive: true}); + } catch (error) { + ctx.logger.debug('Configuration local.json not found: %s', error.stack); } const prettyConfig = JSON.stringify(newConfig, null, 2); await writeFile(configPath, prettyConfig, {encoding: 'utf8'}); diff --git a/branding/info/js/ai-interface.js b/branding/info/js/ai-interface.js index 94a123e6..b4aab95b 100644 --- a/branding/info/js/ai-interface.js +++ b/branding/info/js/ai-interface.js @@ -166,10 +166,8 @@ }, event.source); break; case 'onChangeAction': - for (let id in settings.actions) { - if (settings.actions[id].id == message.data.id) { - settings.actions[id].model = message.data.model; - } + if (settings.actions[message.data.id]) { + settings.actions[message.data.id].model = message.data.model; } break; case 'onOpenEditModal':