From 0ea0399ed19995b1ed499100bd319ed85ca49f86 Mon Sep 17 00:00:00 2001 From: valadaptive Date: Sat, 9 Dec 2023 18:39:19 -0500 Subject: [PATCH 1/3] Separate getStatus into Kobold/textgen versions This adds a bit of duplicate code for the time being, but ultimately makes the code less confusing because we only need to include the bits that are relevant to the specific API in each function. We can also remove API parameters that are useless depending on the endpoint. --- public/script.js | 75 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/public/script.js b/public/script.js index 64cb41752..3713d74e8 100644 --- a/public/script.js +++ b/public/script.js @@ -232,7 +232,6 @@ export { isStreamingEnabled, getThumbnailUrl, getStoppingStrings, - getStatus, reloadMarkdownProcessor, getCurrentChatId, chat, @@ -857,7 +856,7 @@ export async function clearItemizedPrompts() { } } -async function getStatus() { +async function getStatusKobold() { if (main_api == 'koboldhorde') { try { const hordeStatus = await checkHordeStatus(); @@ -870,7 +869,7 @@ async function getStatus() { return resultCheckStatus(); } - const url = main_api == 'textgenerationwebui' ? '/api/textgenerationwebui/status' : '/getstatus'; + const url = '/getstatus'; let endpoint = getAPIServerUrl(); @@ -886,18 +885,64 @@ async function getStatus() { body: JSON.stringify({ main_api, api_server: endpoint, - api_type: textgen_settings.type, - legacy_api: main_api == 'textgenerationwebui' ? - textgen_settings.legacy_api && - textgen_settings.type !== MANCER : - false, }), signal: abortStatusCheck.signal, }); const data = await response.json(); - if (main_api == 'textgenerationwebui' && textgen_settings.type === MANCER) { + + online_status = data?.result; + + if (!online_status) { + online_status = 'no_connection'; + } + + // Determine instruct mode preset + autoSelectInstructPreset(online_status); + + // determine if we can use stop sequence and streaming + setKoboldFlags(data.version, data.koboldVersion); + + // We didn't get a 200 status code, but the endpoint has an explanation. Which means it DID connect, but I digress. + if (online_status === 'no_connection' && data.response) { + toastr.error(data.response, 'API Error', { timeOut: 5000, preventDuplicates: true }); + } + } catch (err) { + console.error('Error getting status', err); + online_status = 'no_connection'; + } + + return resultCheckStatus(); +} + +async function getStatusTextgen() { + const url = '/api/textgenerationwebui/status'; + + let endpoint = getAPIServerUrl(); + + if (!endpoint) { + console.warn('No endpoint for status check'); + return; + } + + try { + const response = await fetch(url, { + method: 'POST', + headers: getRequestHeaders(), + body: JSON.stringify({ + api_server: endpoint, + api_type: textgen_settings.type, + legacy_api: + textgen_settings.legacy_api && + textgen_settings.type !== MANCER, + }), + signal: abortStatusCheck.signal, + }); + + const data = await response.json(); + + if (textgen_settings.type === MANCER) { online_status = textgen_settings.mancer_model; loadMancerModels(data?.data); } else { @@ -911,11 +956,6 @@ async function getStatus() { // Determine instruct mode preset autoSelectInstructPreset(online_status); - // determine if we can use stop sequence and streaming - if (main_api === 'kobold' || main_api === 'koboldhorde') { - setKoboldFlags(data.version, data.koboldVersion); - } - // We didn't get a 200 status code, but the endpoint has an explanation. Which means it DID connect, but I digress. if (online_status === 'no_connection' && data.response) { toastr.error(data.response, 'API Error', { timeOut: 5000, preventDuplicates: true }); @@ -943,6 +983,7 @@ export function resultCheckStatus() { stopStatusLoading(); } +// TODO(valadaptive): remove the usage of this function in the tokenizers code, then remove the function entirely export function getAPIServerUrl() { if (main_api == 'textgenerationwebui') { if (textgen_settings.type === MANCER) { @@ -5314,7 +5355,7 @@ function changeMainAPI() { } if (main_api == 'koboldhorde') { - getStatus(); + getStatusKobold(); getHordeModels(); } @@ -8268,7 +8309,7 @@ jQuery(async function () { main_api = 'kobold'; saveSettingsDebounced(); - getStatus(); + getStatusKobold(); } }); @@ -8304,7 +8345,7 @@ jQuery(async function () { startStatusLoading(); main_api = 'textgenerationwebui'; saveSettingsDebounced(); - getStatus(); + getStatusTextgen(); }); var button = $('#options_button'); From babb127aee992e4fecb03ae7fae12e80ba74ef47 Mon Sep 17 00:00:00 2001 From: valadaptive Date: Sat, 9 Dec 2023 18:41:51 -0500 Subject: [PATCH 2/3] Move NovelAI status functions over to the rest Have all the get(...)Status and event handler registrations in the same areas, rather than having the NovelAI ones far away. I want to eventually move all the API-specific stuff into separate modules, but this will make things cleaner for the time being. --- public/script.js | 68 ++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/public/script.js b/public/script.js index 3713d74e8..81bed0af0 100644 --- a/public/script.js +++ b/public/script.js @@ -968,6 +968,22 @@ async function getStatusTextgen() { return resultCheckStatus(); } +async function getStatusNovel() { + try { + const result = await loadNovelSubscriptionData(); + + if (!result) { + throw new Error('Could not load subscription data'); + } + + online_status = getNovelTier(); + } catch { + online_status = 'no_connection'; + } + + resultCheckStatus(); +} + export function startStatusLoading() { $('.api_loading').show(); $('.api_button').addClass('disabled'); @@ -6072,22 +6088,6 @@ export async function displayPastChats() { }); } -async function getStatusNovel() { - try { - const result = await loadNovelSubscriptionData(); - - if (!result) { - throw new Error('Could not load subscription data'); - } - - online_status = getNovelTier(); - } catch { - online_status = 'no_connection'; - } - - resultCheckStatus(); -} - function selectRightMenuWithAnimation(selectedMenuId) { const displayModes = { 'rm_group_chats_block': 'flex', @@ -8348,6 +8348,24 @@ jQuery(async function () { getStatusTextgen(); }); + $('#api_button_novel').on('click', async function (e) { + e.stopPropagation(); + const api_key_novel = String($('#api_key_novel').val()).trim(); + + if (api_key_novel.length) { + await writeSecret(SECRET_KEYS.NOVEL, api_key_novel); + } + + if (!secret_state[SECRET_KEYS.NOVEL]) { + console.log('No secret key saved for NovelAI'); + return; + } + + startStatusLoading(); + // Check near immediately rather than waiting for up to 90s + await getStatusNovel(); + }); + var button = $('#options_button'); var menu = $('#options'); @@ -9034,24 +9052,6 @@ jQuery(async function () { }); //Select chat - $('#api_button_novel').on('click', async function (e) { - e.stopPropagation(); - const api_key_novel = String($('#api_key_novel').val()).trim(); - - if (api_key_novel.length) { - await writeSecret(SECRET_KEYS.NOVEL, api_key_novel); - } - - if (!secret_state[SECRET_KEYS.NOVEL]) { - console.log('No secret key saved for NovelAI'); - return; - } - - startStatusLoading(); - // Check near immediately rather than waiting for up to 90s - await getStatusNovel(); - }); - //**************************CHARACTER IMPORT EXPORT*************************// $('#character_import_button').click(function () { $('#character_import_file').click(); From a23be7d78576308a09633a9297231ee992075e83 Mon Sep 17 00:00:00 2001 From: valadaptive Date: Sat, 9 Dec 2023 18:52:14 -0500 Subject: [PATCH 3/3] Clean up CSS for API "connect" buttons Instead of identifying each connect button by ID, we can just use the .api_button class. The .menu_button class *would* override it due to CSS cascade rules (specifically, declarations later in the stylesheet apply over ones that appear earlier), but the `.menu_button.api_button` selector has a higher *specificity* and hence works. --- public/style.css | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/public/style.css b/public/style.css index c79357667..38de605cb 100644 --- a/public/style.css +++ b/public/style.css @@ -1444,9 +1444,7 @@ select option:not(:checked) { display: block; } -#api_button:hover, -#api_button_novel:hover, -#api_button_textgenerationwebui:hover { +.menu_button.api_button:hover { background-color: var(--active); }