From 976be98dd755b1e2ff5bb8f3f9cceef5520f6c4e Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Mon, 19 Aug 2024 21:31:58 +0300 Subject: [PATCH] Move command registration --- public/script.js | 45 +------------------------------- public/scripts/slash-commands.js | 45 +++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/public/script.js b/public/script.js index 273852398..daea9cc50 100644 --- a/public/script.js +++ b/public/script.js @@ -212,7 +212,7 @@ import { selectContextPreset, } from './scripts/instruct-mode.js'; import { initLocales, t, translate } from './scripts/i18n.js'; -import { getAvailableTokenizers, getFriendlyTokenizerName, getTokenCount, getTokenCountAsync, getTokenizerModel, initTokenizers, saveTokenCache, selectTokenizer } from './scripts/tokenizers.js'; +import { getFriendlyTokenizerName, getTokenCount, getTokenCountAsync, getTokenizerModel, initTokenizers, saveTokenCache } from './scripts/tokenizers.js'; import { user_avatar, getUserAvatars, @@ -8451,27 +8451,6 @@ async function selectInstructCallback(_, name) { return foundName; } -async function selectTokenizerCallback(_, name) { - if (!name) { - return getAvailableTokenizers().find(tokenizer => tokenizer.tokenizerId === power_user.tokenizer)?.tokenizerKey ?? ''; - } - - const tokenizers = getAvailableTokenizers(); - const fuse = new Fuse(tokenizers, { keys: ['tokenizerKey', 'tokenizerName'] }); - const result = fuse.search(name); - - if (result.length === 0) { - toastr.warning(`Tokenizer "${name}" not found`); - return ''; - } - - /** @type {import('./scripts/tokenizers.js').Tokenizer} */ - const foundTokenizer = result[0].item; - selectTokenizer(foundTokenizer.tokenizerId); - - return foundTokenizer.tokenizerKey; -} - async function enableInstructCallback() { $('#instruct_enabled').prop('checked', true).trigger('change'); return ''; @@ -9116,28 +9095,6 @@ jQuery(async function () { `, })); - SlashCommandParser.addCommandObject(SlashCommand.fromProps({ - name: 'tokenizer', - callback: selectTokenizerCallback, - returns: 'current tokenizer', - unnamedArgumentList: [ - SlashCommandArgument.fromProps({ - description: 'tokenizer name', - typeList: [ARGUMENT_TYPE.STRING], - enumList: getAvailableTokenizers().map(tokenizer => - new SlashCommandEnumValue(tokenizer.tokenizerKey, tokenizer.tokenizerName, enumTypes.enum, enumIcons.default)), - }), - ], - helpString: ` -
- Selects tokenizer by name. Gets the current tokenizer if no name is provided. -
-
- Available tokenizers: -
${getAvailableTokenizers().map(t => t.tokenizerKey).join(', ')}
-
- ` - })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'instruct-on', callback: enableInstructCallback, diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index d9d27f0db..6dcac6a80 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -52,7 +52,7 @@ import { chat_completion_sources, oai_settings, setupChatCompletionPromptManager import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, setPersonaLockState, togglePersonaLock, user_avatar } from './personas.js'; import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js'; import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js'; -import { decodeTextTokens, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync } from './tokenizers.js'; +import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js'; import { debounce, delay, isFalseBoolean, isTrueBoolean, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js'; import { registerVariableCommands, resolveVariable } from './variables.js'; import { background_settings } from './backgrounds.js'; @@ -1558,6 +1558,28 @@ export function initDefaultSlashCommands() { `, })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'tokenizer', + callback: selectTokenizerCallback, + returns: 'current tokenizer', + unnamedArgumentList: [ + SlashCommandArgument.fromProps({ + description: 'tokenizer name', + typeList: [ARGUMENT_TYPE.STRING], + enumList: getAvailableTokenizers().map(tokenizer => + new SlashCommandEnumValue(tokenizer.tokenizerKey, tokenizer.tokenizerName, enumTypes.enum, enumIcons.default)), + }), + ], + helpString: ` +
+ Selects tokenizer by name. Gets the current tokenizer if no name is provided. +
+
+ Available tokenizers: +
${getAvailableTokenizers().map(t => t.tokenizerKey).join(', ')}
+
+ `, + })); registerVariableCommands(); } @@ -3562,6 +3584,27 @@ async function setApiUrlCallback({ api = null, connect = 'true' }, url) { return textgenerationwebui_settings.server_urls[type] ?? ''; } +async function selectTokenizerCallback(_, name) { + if (!name) { + return getAvailableTokenizers().find(tokenizer => tokenizer.tokenizerId === power_user.tokenizer)?.tokenizerKey ?? ''; + } + + const tokenizers = getAvailableTokenizers(); + const fuse = new Fuse(tokenizers, { keys: ['tokenizerKey', 'tokenizerName'] }); + const result = fuse.search(name); + + if (result.length === 0) { + toastr.warning(`Tokenizer "${name}" not found`); + return ''; + } + + /** @type {import('./tokenizers.js').Tokenizer} */ + const foundTokenizer = result[0].item; + selectTokenizer(foundTokenizer.tokenizerId); + + return foundTokenizer.tokenizerKey; +} + export let isExecutingCommandsFromChatInput = false; export let commandsFromChatInputAbortController;