From 8ed31669dc18dd1a2fb7b4dac39b113e139133d0 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Mon, 9 Jun 2025 23:14:04 +0300 Subject: [PATCH] Fix support serverSettings --- sdkjs-plugins/content/ai/config.json | 2 +- sdkjs-plugins/content/ai/scripts/code.js | 10 ++++++++++ sdkjs-plugins/content/ai/scripts/engine/engine.js | 11 +++++++++-- .../content/ai/scripts/engine/local_storage.js | 6 +++++- .../content/ai/scripts/engine/providers/base.js | 2 ++ .../content/ai/scripts/engine/register.js | 15 ++++++++++----- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/sdkjs-plugins/content/ai/config.json b/sdkjs-plugins/content/ai/config.json index 8115dc14..35d19921 100644 --- a/sdkjs-plugins/content/ai/config.json +++ b/sdkjs-plugins/content/ai/config.json @@ -46,7 +46,7 @@ "type" : "background", "initDataType" : "none", "buttons" : [], - "events" : ["onContextMenuShow", "onContextMenuClick", "onToolbarMenuClick"], + "events" : ["onAIPluginSettings", "onContextMenuShow", "onContextMenuClick", "onToolbarMenuClick"], "store": { "background": { diff --git a/sdkjs-plugins/content/ai/scripts/code.js b/sdkjs-plugins/content/ai/scripts/code.js index a226756d..d156147c 100644 --- a/sdkjs-plugins/content/ai/scripts/code.js +++ b/sdkjs-plugins/content/ai/scripts/code.js @@ -91,6 +91,16 @@ async function GetOldCustomFunctions() { } window.Asc.plugin.init = async function() { + // Check server settings + if (window.Asc.plugin.info.aiPluginSettings) { + try { + AI.serverSettings = JSON.parse(window.Asc.plugin.info.aiPluginSettings); + } catch (e) { + AI.serverSettings = null; + } + delete window.Asc.plugin.info.aiPluginSettings; + } + await initWithTranslate(1 << 1); clearChatState(); diff --git a/sdkjs-plugins/content/ai/scripts/engine/engine.js b/sdkjs-plugins/content/ai/scripts/engine/engine.js index e3c99ac3..e8344f96 100644 --- a/sdkjs-plugins/content/ai/scripts/engine/engine.js +++ b/sdkjs-plugins/content/ai/scripts/engine/engine.js @@ -86,7 +86,14 @@ "data" : request.body }) } - message.url = AI.PROXY_URL; + if (AI.serverSettings){ + message.url = AI.serverSettings.proxy; + request["headers"] = { + "Authorization" : "Bearer " + Asc.plugin.info.jwt, + } + } else { + message.url = AI.PROXY_URL; + } } } @@ -136,7 +143,7 @@ body[i] = bodyPr[i]; } - return provider.isUseProxy(); + return provider.isUseProxy() || AI.serverSettings; }; AI._getEndpointUrl = function(_provider, endpoint, model) { diff --git a/sdkjs-plugins/content/ai/scripts/engine/local_storage.js b/sdkjs-plugins/content/ai/scripts/engine/local_storage.js index 6c81b3e0..4d405d26 100644 --- a/sdkjs-plugins/content/ai/scripts/engine/local_storage.js +++ b/sdkjs-plugins/content/ai/scripts/engine/local_storage.js @@ -58,7 +58,11 @@ AI.Storage.load = function() { let obj = null; try { - obj = JSON.parse(window.localStorage.getItem(localStorageKey)); + if (AI.serverSettings) { + obj = AI.serverSettings; + } else { + obj = JSON.parse(window.localStorage.getItem(localStorageKey)); + } } catch (e) { obj = AI.DEFAULT_SERVER_SETTINGS; diff --git a/sdkjs-plugins/content/ai/scripts/engine/providers/base.js b/sdkjs-plugins/content/ai/scripts/engine/providers/base.js index 000166f6..c4d93673 100644 --- a/sdkjs-plugins/content/ai/scripts/engine/providers/base.js +++ b/sdkjs-plugins/content/ai/scripts/engine/providers/base.js @@ -244,4 +244,6 @@ }; + AI.serverSettings = null; + })(); diff --git a/sdkjs-plugins/content/ai/scripts/engine/register.js b/sdkjs-plugins/content/ai/scripts/engine/register.js index 9753cb41..ed16c401 100644 --- a/sdkjs-plugins/content/ai/scripts/engine/register.js +++ b/sdkjs-plugins/content/ai/scripts/engine/register.js @@ -1,5 +1,8 @@ function registerButtons(window, undefined) { + window.AI = window.AI || {}; + var AI = window.AI; + function getToolBarButtonIcons(icon) { return "resources/icons/%theme-type%(light|dark)/big/" + icon + "%scale%(default).png"; } @@ -472,7 +475,7 @@ function registerButtons(window, undefined) window.buttonMainToolbar = buttonMainToolbar; window.getToolBarButtonIcons = getToolBarButtonIcons; - if (true) + if (!AI.serverSettings) { let button1 = new Asc.ButtonToolbar(buttonMainToolbar); button1.text = "Settings"; @@ -548,8 +551,6 @@ function registerButtons(window, undefined) } // register actions - window.AI = window.AI || {}; - var AI = window.AI; AI.ActionType = { Chat : "Chat", @@ -629,7 +630,11 @@ function registerButtons(window, undefined) let obj = null; try { - obj = JSON.parse(window.localStorage.getItem(actions_key)); + if (AI.serverSettings) { + obj = AI.serverSettings.actions; + } else { + obj = JSON.parse(window.localStorage.getItem(actions_key)); + } } catch (e) { @@ -657,5 +662,5 @@ function registerButtons(window, undefined) } }; - AI.ActionsLoad(); + AI.ActionsLoad(); }