From 8938ea1d721fcefc268d8aea75c505f255d0fca6 Mon Sep 17 00:00:00 2001 From: Stefan Daniel Schwarz Date: Wed, 23 Aug 2023 20:17:45 +0200 Subject: [PATCH] auto select presets --- public/scripts/instruct-mode.js | 72 ++++++++++++++++++++++++++++----- public/scripts/power-user.js | 15 ++++++- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/public/scripts/instruct-mode.js b/public/scripts/instruct-mode.js index bc75e266d..5881f5966 100644 --- a/public/scripts/instruct-mode.js +++ b/public/scripts/instruct-mode.js @@ -2,7 +2,10 @@ import { saveSettingsDebounced, substituteParams } from "../script.js"; import { selected_group } from "./group-chats.js"; -import { power_user } from "./power-user.js"; +import { + power_user, + context_presets, +} from "./power-user.js"; /** * @type {any[]} Instruct mode presets. @@ -69,6 +72,44 @@ function highlightDefaultPreset() { $('#instruct_set_default').toggleClass('default', power_user.default_instruct === power_user.instruct.preset); } +/** + * Select context template if not already selected. + * @param {string} preset Preset name. + */ +function selectContextPreset(preset) { + // If preset is not already selected, select it + if (power_user.context.preset !== preset) { + $('#context_presets').val(preset).trigger('change'); + toastr.info(`Context Template: preset "${preset}" auto-selected`); + + // If instruct mode is disabled, enable it + if (!power_user.instruct.enabled) { + $('#instruct_enabled').prop('checked', true).trigger('change'); + power_user.instruct.enabled = true; + toastr.info(`Instruct Mode enabled`); + } + } +} + +/** + * Select instruct preset if not already selected. + * @param {string} preset Preset name. + */ +export function selectInstructPreset(preset) { + // If preset is not already selected, select it + if (power_user.instruct.preset !== preset) { + $('#instruct_presets').val(preset).trigger('change'); + toastr.info(`Instruct Mode: preset "${preset}" auto-selected`); + + // If instruct mode is disabled, enable it + if (!power_user.instruct.enabled) { + $('#instruct_enabled').prop('checked', true).trigger('change'); + power_user.instruct.enabled = true; + toastr.info(`Instruct Mode enabled`); + } + } +} + /** * Automatically select instruct preset based on model id. * Otherwise, if default instruct preset is set, selects it. @@ -82,20 +123,17 @@ export function autoSelectInstructPreset(modelId) { } for (const preset of instruct_presets) { - // If activation regex is set, check if it matches the model id - if (preset.activation_regex) { + // If context template matches the preset name + if (power_user.context.preset === preset.name) { + selectInstructPreset(preset.name); + // Else if activation regex is set, check if it matches the model id + } else if (preset.activation_regex) { try { const regex = new RegExp(preset.activation_regex, 'i'); // Stop on first match so it won't cycle back and forth between presets if multiple regexes match if (regex.test(modelId)) { - // If preset is not already selected, select it - if (power_user.instruct.preset !== preset.name) { - $('#instruct_presets').val(preset.name).trigger('change'); - toastr.info(`Instruct mode: preset "${preset.name}" auto-selected`); - - return true; - } + selectInstructPreset(preset.name); } } catch { // If regex is invalid, ignore it @@ -259,6 +297,13 @@ jQuery(() => { saveSettingsDebounced(); }); + $('#instruct_enabled').on('change', function () { + // When instruct mode gets enabled, select context template matching selected instruct preset + if (power_user.instruct.enabled) { + $('#instruct_presets').trigger('change'); + } + }); + $('#instruct_presets').on('change', function () { const name = $(this).find(':selected').val(); const preset = instruct_presets.find(x => x.name === name); @@ -281,6 +326,13 @@ jQuery(() => { } }); + for (const context_preset of context_presets) { + // If context template matches the instruct preset + if (context_preset.name === name) { + selectContextPreset(name); + } + } + highlightDefaultPreset(); }); }); diff --git a/public/scripts/power-user.js b/public/scripts/power-user.js index 9ccbd7db9..77855ddec 100644 --- a/public/scripts/power-user.js +++ b/public/scripts/power-user.js @@ -20,7 +20,11 @@ import { groups, resetSelectedGroup, } from "./group-chats.js"; -import { loadInstructMode } from "./instruct-mode.js"; +import { + instruct_presets, + loadInstructMode, + selectInstructPreset, +} from "./instruct-mode.js"; import { registerSlashCommand } from "./slash-commands.js"; @@ -204,7 +208,7 @@ let power_user = { let themes = []; let movingUIPresets = []; -let context_presets = []; +export let context_presets = []; const storage_keys = { fast_ui_mode: "TavernAI_fast_ui_mode", @@ -931,6 +935,13 @@ function loadContextSettings() { } } }); + + for (const instruct_preset of instruct_presets) { + // If instruct preset matches the context template + if (instruct_preset.name === name) { + selectInstructPreset(name); + } + } }); }