From 27698fd024991e0b0b39b69f077b1da68fedf05a Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Wed, 3 Apr 2024 00:52:30 +0300 Subject: [PATCH] Add ability to get model name with /model --- public/script.js | 4 ++-- public/scripts/preset-manager.js | 2 +- public/scripts/slash-commands.js | 28 ++++++++++++++++------------ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/public/script.js b/public/script.js index 3a9d5f5bb..0d1633a6b 100644 --- a/public/script.js +++ b/public/script.js @@ -8616,10 +8616,10 @@ jQuery(async function () { registerSlashCommand('closechat', doCloseChat, [], '– closes the current chat', true, true); registerSlashCommand('panels', doTogglePanels, ['togglepanels'], '– toggle UI panels on/off', true, true); registerSlashCommand('forcesave', doForceSave, [], '– forces a save of the current chat and settings', true, true); - registerSlashCommand('instruct', selectInstructCallback, [], '(name) – selects instruct mode preset by name', true, true); + registerSlashCommand('instruct', selectInstructCallback, [], '(name) – selects instruct mode preset by name. Gets the current instruct if no name is provided', true, true); registerSlashCommand('instruct-on', enableInstructCallback, [], '– enables instruct mode', true, true); registerSlashCommand('instruct-off', disableInstructCallback, [], '– disables instruct mode', true, true); - registerSlashCommand('context', selectContextCallback, [], '(name) – selects context template by name', true, true); + registerSlashCommand('context', selectContextCallback, [], '(name) – selects context template by name. Gets the current template if no name is provided', true, true); registerSlashCommand('chat-manager', () => $('#option_select_chat').trigger('click'), ['chat-history', 'manage-chats'], '– opens the chat manager for the current character/group', true, true); setTimeout(function () { diff --git a/public/scripts/preset-manager.js b/public/scripts/preset-manager.js index ede6346d8..1a28f075c 100644 --- a/public/scripts/preset-manager.js +++ b/public/scripts/preset-manager.js @@ -470,7 +470,7 @@ async function waitForConnection() { export async function initPresetManager() { eventSource.on(event_types.CHAT_CHANGED, autoSelectPreset); registerPresetManagers(); - registerSlashCommand('preset', presetCommandCallback, [], '(name) – sets a preset by name for the current API', true, true); + registerSlashCommand('preset', presetCommandCallback, [], '(name) – sets a preset by name for the current API. Gets the current preset if no name is provided', true, true); $(document).on('click', '[data-preset-manager-update]', async function () { const apiId = $(this).data('preset-manager-update'); diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 70042dd3c..4aeff2b91 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -249,7 +249,7 @@ parser.addCommand('inject', injectCallback, [], 'id=inje parser.addCommand('listinjects', listInjectsCallback, [], ' – lists all script injections for the current chat.', true, true); parser.addCommand('flushinjects', flushInjectsCallback, [], ' – removes all script injections for the current chat.', true, true); parser.addCommand('tokens', (_, text) => getTokenCount(text), [], '(text) – counts the number of tokens in the text.', true, true); -parser.addCommand('model', modelCallback, [], '(model name) – sets the model for the current API.', true, true); +parser.addCommand('model', modelCallback, [], '(model name) – sets the model for the current API. Gets the current model name if no argument is provided.', true, true); registerVariableCommands(); const NARRATOR_NAME_KEY = 'narrator_name'; @@ -1634,16 +1634,10 @@ function setBackgroundCallback(_, bg) { /** * Sets a model for the current API. * @param {object} _ Unused - * @param {string} model Model name - * @returns {void} + * @param {string} model New model name + * @returns {string} New or existing model name */ function modelCallback(_, model) { - if (!model) { - return; - } - - console.log('Set model to ' + model); - const modelSelectMap = [ { id: 'model_togetherai_select', api: 'textgenerationwebui', type: textgen_types.TOGETHERAI }, { id: 'openrouter_model', api: 'textgenerationwebui', type: textgen_types.OPENROUTER }, @@ -1681,23 +1675,31 @@ function modelCallback(_, model) { if (!modelSelectItem) { toastr.info('Setting a model for your API is not supported or not implemented yet.'); - return; + return ''; } const modelSelectControl = document.getElementById(modelSelectItem); if (!(modelSelectControl instanceof HTMLSelectElement)) { toastr.error(`Model select control not found: ${main_api}[${apiSubType}]`); - return; + return ''; } const options = Array.from(modelSelectControl.options); if (!options.length) { toastr.warning('No model options found. Check your API settings.'); - return; + return ''; } + model = String(model || '').trim(); + + if (!model) { + return modelSelectControl.value; + } + + console.log('Set model to ' + model); + let newSelectedOption = null; const fuse = new Fuse(options, { keys: ['text', 'value'] }); @@ -1718,8 +1720,10 @@ function modelCallback(_, model) { modelSelectControl.value = newSelectedOption.value; $(modelSelectControl).trigger('change'); toastr.success(`Model set to "${newSelectedOption.text}"`); + return newSelectedOption.value; } else { toastr.warning(`No model found with name "${model}"`); + return ''; } }