/model: Better support for write-in controls

c76fc7d23c95ea663d22b594d23a965d33dd7a8c

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +37 -10Showing whitespace changes
public/scripts/slash-commands.js+37 -10
@@ -55,7 +55,7 @@ import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, setPersonaLockStat
55import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js';55import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js';
56import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js';56import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js';
57import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js';57import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js';
58import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';58import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';
59import { registerVariableCommands, resolveVariable } from './variables.js';59import { registerVariableCommands, resolveVariable } from './variables.js';
60import { background_settings } from './backgrounds.js';60import { background_settings } from './backgrounds.js';
61import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';61import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
@@ -3542,11 +3542,12 @@ function setBackgroundCallback(_, bg) {
3542 * Retrieves the available model options based on the currently selected main API and its subtype3542 * Retrieves the available model options based on the currently selected main API and its subtype
3543 * @param {boolean} quiet - Whether to suppress toasts3543 * @param {boolean} quiet - Whether to suppress toasts
3544 *3544 *
3545 * @returns {{control: HTMLSelectElement, options: HTMLOptionElement[]}?} An array of objects representing the available model options, or null if not supported3545 * @returns {{control: HTMLSelectElement|HTMLInputElement, options: HTMLOptionElement[]}?} An array of objects representing the available model options, or null if not supported
3546 */3546 */
3547function getModelOptions(quiet) {3547function getModelOptions(quiet) {
3548 const nullResult = { control: null, options: null };3548 const nullResult = { control: null, options: null };
3549 const modelSelectMap = [3549 const modelSelectMap = [
3550 { id: 'custom_model_textgenerationwebui', api: 'textgenerationwebui', type: textgen_types.OOBA },
3550 { id: 'model_togetherai_select', api: 'textgenerationwebui', type: textgen_types.TOGETHERAI },3551 { id: 'model_togetherai_select', api: 'textgenerationwebui', type: textgen_types.TOGETHERAI },
3551 { id: 'openrouter_model', api: 'textgenerationwebui', type: textgen_types.OPENROUTER },3552 { id: 'openrouter_model', api: 'textgenerationwebui', type: textgen_types.OPENROUTER },
3552 { id: 'model_infermaticai_select', api: 'textgenerationwebui', type: textgen_types.INFERMATICAI },3553 { id: 'model_infermaticai_select', api: 'textgenerationwebui', type: textgen_types.INFERMATICAI },
@@ -3563,7 +3564,7 @@ function getModelOptions(quiet) {
3563 { id: 'model_ai21_select', api: 'openai', type: chat_completion_sources.AI21 },3564 { id: 'model_ai21_select', api: 'openai', type: chat_completion_sources.AI21 },
3564 { id: 'model_google_select', api: 'openai', type: chat_completion_sources.MAKERSUITE },3565 { id: 'model_google_select', api: 'openai', type: chat_completion_sources.MAKERSUITE },
3565 { id: 'model_mistralai_select', api: 'openai', type: chat_completion_sources.MISTRALAI },3566 { id: 'model_mistralai_select', api: 'openai', type: chat_completion_sources.MISTRALAI },
3566 { id: 'model_custom_select', api: 'openai', type: chat_completion_sources.CUSTOM },3567 { id: 'custom_model_id', api: 'openai', type: chat_completion_sources.CUSTOM },
3567 { id: 'model_cohere_select', api: 'openai', type: chat_completion_sources.COHERE },3568 { id: 'model_cohere_select', api: 'openai', type: chat_completion_sources.COHERE },
3568 { id: 'model_perplexity_select', api: 'openai', type: chat_completion_sources.PERPLEXITY },3569 { id: 'model_perplexity_select', api: 'openai', type: chat_completion_sources.PERPLEXITY },
3569 { id: 'model_groq_select', api: 'openai', type: chat_completion_sources.GROQ },3570 { id: 'model_groq_select', api: 'openai', type: chat_completion_sources.GROQ },
@@ -3594,12 +3595,31 @@ function getModelOptions(quiet) {
35943595
3595 const modelSelectControl = document.getElementById(modelSelectItem);3596 const modelSelectControl = document.getElementById(modelSelectItem);
35963597
3597 if (!(modelSelectControl instanceof HTMLSelectElement)) {3598 if (!(modelSelectControl instanceof HTMLSelectElement) && !(modelSelectControl instanceof HTMLInputElement)) {
3598 !quiet && toastr.error(`Model select control not found: ${main_api}[${apiSubType}]`);3599 !quiet && toastr.error(`Model select control not found: ${main_api}[${apiSubType}]`);
3599 return nullResult;3600 return nullResult;
3600 }3601 }
36013602
3602 const options = Array.from(modelSelectControl.options).filter(x => x.value);3603 /**
3604 * Get options from a HTMLSelectElement or HTMLInputElement with a list.
3605 * @param {HTMLSelectElement | HTMLInputElement} control Control containing the options
3606 * @returns {HTMLOptionElement[]} Array of options
3607 */
3608 const getOptions = (control) => {
3609 if (control instanceof HTMLSelectElement) {
3610 return Array.from(control.options);
3611 }
3612
3613 const valueOption = new Option(control.value, control.value);
3614
3615 if (control instanceof HTMLInputElement && control.list instanceof HTMLDataListElement) {
3616 return [valueOption, ...Array.from(control.list.options)];
3617 }
3618
3619 return [valueOption];
3620 };
3621
3622 const options = getOptions(modelSelectControl).filter(x => x.value).filter(onlyUnique);
3603 return { control: modelSelectControl, options };3623 return { control: modelSelectControl, options };
3604}3624}
36053625
@@ -3618,11 +3638,6 @@ function modelCallback(args, model) {
3618 return '';3638 return '';
3619 }3639 }
36203640
3621 if (!options.length) {
3622 !quiet && toastr.warning('No model options found. Check your API settings.');
3623 return '';
3624 }
3625
3626 model = String(model || '').trim();3641 model = String(model || '').trim();
36273642
3628 if (!model) {3643 if (!model) {
@@ -3631,6 +3646,18 @@ function modelCallback(args, model) {
36313646
3632 console.log('Set model to ' + model);3647 console.log('Set model to ' + model);
36333648
3649 if (modelSelectControl instanceof HTMLInputElement) {
3650 modelSelectControl.value = model;
3651 $(modelSelectControl).trigger('input');
3652 !quiet && toastr.success(`Model set to "${model}"`);
3653 return model;
3654 }
3655
3656 if (!options.length) {
3657 !quiet && toastr.warning('No model options found. Check your API settings.');
3658 return '';
3659 }
3660
3634 let newSelectedOption = null;3661 let newSelectedOption = null;
36353662
3636 const fuse = new Fuse(options, { keys: ['text', 'value'] });3663 const fuse = new Fuse(options, { keys: ['text', 'value'] });