From 3b4e6f0b781d8f7812c8e98231bcf278ca0b6c5a Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 27 Aug 2023 23:20:43 +0300 Subject: [PATCH] Add debug functions menu --- public/index.html | 10 +++++++-- public/script.js | 2 ++ public/scripts/i18n.js | 9 +++++--- public/scripts/power-user.js | 35 ++++++++++++++++++++++++++++- public/scripts/templates/debug.html | 25 +++++++++++++++++++++ public/scripts/tokenizers.js | 6 ++--- public/style.css | 4 ++-- 7 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 public/scripts/templates/debug.html diff --git a/public/index.html b/public/index.html index 1c489eaea..de3dd0542 100644 --- a/public/index.html +++ b/public/index.html @@ -2961,8 +2961,14 @@ - diff --git a/public/script.js b/public/script.js index 66a8f0778..bb3c90c03 100644 --- a/public/script.js +++ b/public/script.js @@ -70,6 +70,7 @@ import { MAX_CONTEXT_DEFAULT, renderStoryString, sortEntitiesList, + registerDebugFunction, } from "./scripts/power-user.js"; import { @@ -6658,6 +6659,7 @@ window["SillyTavern"].getContext = function () { saveReply, registerSlashCommand: registerSlashCommand, registerHelper: registerExtensionHelper, + registedDebugFunction: registerDebugFunction, }; }; diff --git a/public/scripts/i18n.js b/public/scripts/i18n.js index 5e359fa97..afc3f3917 100644 --- a/public/scripts/i18n.js +++ b/public/scripts/i18n.js @@ -1,3 +1,4 @@ +import { registerDebugFunction } from "./power-user.js"; import { waitUntilCondition } from "./utils.js"; const storageKey = "language"; @@ -48,9 +49,9 @@ function getMissingTranslations() { console.table(uniqueMissingData); console.log(missingDataMap); -} -window["getMissingTranslations"] = getMissingTranslations; + toastr.success(`Found ${uniqueMissingData.length} missing translations. See browser console for details.`); +} export function applyLocale(root = document) { const overrideLanguage = localStorage.getItem("language"); @@ -106,7 +107,6 @@ function addLanguagesToDropdown() { jQuery(async () => { waitUntilCondition(() => !!localeData); - window["applyLocale"] = applyLocale; applyLocale(); addLanguagesToDropdown(); @@ -121,4 +121,7 @@ jQuery(async () => { location.reload(); }); + + registerDebugFunction('getMissingTranslations', 'Get missing translations', 'Detects missing localization data and dumps the data into the browser console.', getMissingTranslations); + registerDebugFunction('applyLocale', 'Apply locale', 'Reapplies the currently selected locale to the page.', applyLocale); }); diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index 0a3c768a3..312db1edc 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -13,7 +13,8 @@ import { getCurrentChatId, printCharacters, setCharacterId, - setEditedMessageId + setEditedMessageId, + renderTemplate, } from "../script.js"; import { isMobile, initMovingUI } from "./RossAscends-mods.js"; import { @@ -232,6 +233,7 @@ const storage_keys = { }; let browser_has_focus = true; +const debug_functions = []; function playMessageSound() { if (!power_user.play_message_sound) { @@ -653,6 +655,22 @@ async function applyMovingUIPreset(name) { loadMovingUIState() } +/** + * Register a function to be executed when the debug menu is opened. + * @param {string} functionId Unique ID for the function. + * @param {string} name Name of the function. + * @param {string} description Description of the function. + * @param {function} func Function to be executed. + */ +export function registerDebugFunction(functionId, name, description, func) { + debug_functions.push({ functionId, name, description, func }); +} + +function showDebugMenu() { + const template = renderTemplate('debug', { functions: debug_functions }); + callPopup(template, 'text', '', { wide: true, large: true }); +} + switchUiMode(); applyFontScale('forced'); applyThemeColor(); @@ -2113,6 +2131,21 @@ $(document).ready(() => { saveSettingsDebounced(); }); + $('#debug_menu').on('click', function () { + showDebugMenu(); + }); + + $(document).on('click', '#debug_table [data-debug-function]', function () { + const functionId = $(this).data('debug-function'); + const functionRecord = debug_functions.find(f => f.functionId === functionId); + + if (functionRecord) { + functionRecord.func(); + } else { + console.warn(`Debug function ${functionId} not found`); + } + }); + $(window).on('focus', function () { browser_has_focus = true; }); diff --git a/public/scripts/templates/debug.html b/public/scripts/templates/debug.html new file mode 100644 index 000000000..3e562cc45 --- /dev/null +++ b/public/scripts/templates/debug.html @@ -0,0 +1,25 @@ +

Debug Menu

+
+ Functions in this category are for advanced users only. Don't click anything if you're not sure about the consequences. +
+ + {{#each functions}} + {{#with this}} + + + + {{/with}} + {{/each}} +
+
+ {{this.name}} +
+
+ {{this.description}} +
+
+ +
+
diff --git a/public/scripts/tokenizers.js b/public/scripts/tokenizers.js index 0960ad87d..48ddcc11a 100644 --- a/public/scripts/tokenizers.js +++ b/public/scripts/tokenizers.js @@ -1,5 +1,5 @@ import { characters, main_api, nai_settings, online_status, this_chid } from "../script.js"; -import { power_user } from "./power-user.js"; +import { power_user, registerDebugFunction } from "./power-user.js"; import { chat_completion_sources, oai_settings } from "./openai.js"; import { groups, selected_group } from "./group-chats.js"; import { getStringHash } from "./utils.js"; @@ -59,13 +59,12 @@ async function resetTokenCache() { console.debug('Chat Completions: resetting token cache'); Object.keys(tokenCache).forEach(key => delete tokenCache[key]); await objectStore.removeItem('tokenCache'); + toastr.success('Token cache cleared. Please reload the chat to re-tokenize it.'); } catch (e) { console.log('Chat Completions: unable to reset token cache', e); } } -window['resetTokenCache'] = resetTokenCache; - function getTokenizerBestMatch() { if (main_api === 'novel') { if (nai_settings.model_novel.includes('krake') || nai_settings.model_novel.includes('euterpe')) { @@ -438,4 +437,5 @@ export function decodeTextTokens(tokenizerType, ids) { jQuery(async () => { await loadTokenCache(); + registerDebugFunction('resetTokenCache', 'Reset token cache', 'Purges the calculated token counts. Use this if you want to force a full re-tokenization of all chats or suspect the token counts are wrong.', resetTokenCache); }); diff --git a/public/style.css b/public/style.css index c6276a87e..2c5962004 100644 --- a/public/style.css +++ b/public/style.css @@ -1768,10 +1768,10 @@ grammarly-extension { top: 50%; transform: translateY(-50%); text-align: center; - box-shadow: 0px 0px 14px var(--black50a); + box-shadow: 0px 0px 14px var(--black70a); border: 1px solid var(--white30a); padding: 4px; - background-color: var(--black30a); + background-color: var(--black50a); border-radius: 10px; max-height: 90vh; max-height: 90svh;