SillyTavern/public/scripts/preset-manager.js

659 lines
20 KiB
JavaScript
Raw Normal View History

import {
amount_gen,
callPopup,
characters,
eventSource,
event_types,
getRequestHeaders,
koboldai_setting_names,
koboldai_settings,
main_api,
max_context,
nai_settings,
novelai_setting_names,
novelai_settings,
2023-12-12 18:14:17 +01:00
online_status,
saveSettingsDebounced,
this_chid,
2023-12-02 19:04:51 +01:00
} from '../script.js';
import { groups, selected_group } from './group-chats.js';
import { instruct_presets } from './instruct-mode.js';
import { kai_settings } from './kai-settings.js';
import { context_presets, getContextSettings, power_user } from './power-user.js';
STscript Parser Rewrite (#1965) * set isForced to true on input * make floating auto-complete follow horizontal scrolling * add callable closure vars * changes to /let and /var for callable closures * fix error message * fix scope for closure arguments * if should return the pipe result from closures * use /run to call closures and no arguments on immediate closures * throw exception from QRs window-function if no match * when to show autocomplete vs info only * autocomplete positioning * autocomplete styling * add theming to autocomplete (theme, dark, light) * improve autocomplete show/hide logic and editor selection * use blur tint color instead of chat tint color and use blur setting * cleanup and docs * use scope macros for QR args * add enter to select autocomplete * fix no executor found * cleanup and comment * fix alias list in help string * fallback to empty string piped value if null or undefined * fix typo * blur textarea on ctrl+enter execute (and refocus after) * stop executeSlashCommand if parser throws * move /let and /var callbacks into functions * switch textarea to monospace when value starts with slash * add double pipe a pipe breaker * fix /? slash * remove some logging * add "/:name" as shorthand for "/run name" after all * move shit around * fix error message * use testRunShorthandEnd * use parseQuotedValue and parseValue to determine name for "/:" QR labels and set names can include spaces * add some adjustments to make autocomplete work properly some hint in there about "/:" would still be nice * add autocomplete style selector * only strip quotes from subcommand if they are at both ends * fix JSDoc * escaping * allow open quotes on dry run * throwing shit at the wall for /: autocomplete * escapes only for symbols * clean up autocomplete * improve performance * fix scope macros * remove unescaping of pipes * fix macros in scope copy * fix "/? slash" * don't run parser for getNameAt if text has not changed * fix options filter * re-enable blur listener * restore selection on non-replace select * fix for escaping first character of value * add support for {{pipe}} and {{var::}} closures * add index support to var macro * add scoped var macro to macro help * more escape fixes * reduce autocomplete render debounce * cleanup * restore old escape handling and parser flag for strict escaping * fix "no match" autocomplete message * add dummy commands for comments and parser flag * fix type annotations * somewhat safer macro replacements * fix autocomplete select on blank / "no match" * fix cutting off handled part in substitution * add parser flag REPLACE_GETVAR Replaces all {{getvar::}} and {{getglobalvar::}} macros with {{var::}}. Inserts a series of command executors before the command with the macros that: - save {{pipe}} to a var - call /getvar or /getglobalvar to get the variable used in the macro - call /let to save the retrieved variable - return the saved {{pipe}} value This helps to avoid double-substitutions when the var values contain text that could be interpreted as macros. * remove old parser * fix send on enter when no match * deal with pipes in quoted values (loose escaping) * add default parser flags to user settings * allow quoted values in unnamed argument * set parser flag without explicit state to "on" * add click hint on parser error toast * dirty more detailed cmd defs * remove name from unnamed arg * move autocomplete into class and floating with details * replace jQuery's trigger('input') on #send_textarea with native events because jQuery does not dispatch the native event * fix ctrl+space * fix arrow navigation * add comments * fix pointer block * add static fromProps * fix up dummy commands * migrate all commands to addCommandObject * remove commented comment command * fix alias in details * add range as argument type * switch to addCommandObject * switch to addCommandObject * fix height * fix floating details position on left * re-enable blur event * use auto width for full details on floating autocomplete * auto-size floating full details * fix typo * re-enable blur listener * don't prevent enter when selected item is fully typed out * add autocomplete details tooltips * add language to slash command examples * move makeItem into option and command and fix click select * use autocomplete parts in /? slash * fix alias formatting * add language to slash command examples * fix details position on initial input history * small screen styles * replace registerSlashCommand with detailed declarations * put name on first line * add missing returns * fix missing comma * fix alias display in autocomplete list * remove args from help string * move parser settings to its own section * jsdoc * hljs stscript lang * add hljs to autocomplete help examples * add missing import * apply autocomplete colors to stscript codeblocks (hljs) * add fromProps * cache autocomplete elements * towards generic autocomplete * remove unused imports * fix blanks * add return types * re-enable blur * fix blank check * Caption messages by id * add aborting command execution * fix return type * fix chat input font reset * add slash command progress indicator * add missing return * mark registerSlashCommand deprecated * why?? * separate abort logic for commands * remove parsing of quoted values from unnamed arg * add adjustable autocomplete width * revert stop button pulse * add progress and pause/abort to QR editor * add resize event on autocomplete width change * add key= argument to all get vars * refactoring * introduce NamedArgumentAsignment * add TODOs * refactoring * record start and end of named arg assignment * refactoring * prevent duplicate calls to show * refactoring * remove macro ac * add secondary autocomplete and enum descriptions * add syntax highlighting to QR editor * add enum descriptions to /while * add /let key=... to scope variable names * add unnamed argument assignment class and unnamed argument splitting * fix QR editor style * remove dash before autocomplete help text * add autocomplete for unnamed enums * fix remaining dom after holding backslash * fix for unnamed enums * fix autocomplete for /parser-flag * add parser-flag enum help * fix type annotations * fix autocomplete result for /: * add colored autocomplete type icons * collapse second line autocomplete help if empty * mark optional named args in autocomplete * fix when what * remove duplicate debug buttons * dispatch input on autocomplete select * prevent grow from editor syntax layer * add auto-adjust qr editor caret color * remove text-shadow from autocomplete * join value strings in /let and /var * add /abort syntax highlight * fix attempting secondary result when there is none * rename settings headers and split autocomplete / stscript * add parser flag tooltips * add tooltips to chat width stops * fix typo * return clone of help item * fix enum string * don't make optional notice for autocomplete arguments smaller * avoid scrollbar in chat input * add rudimentary macro autocomplete * strip macro from helptext * finally remove closure delimiters around root * cleanup * fix index stuff for removed closure delimiters * fix type hint * add child commands to progress indicator * include sub-separator in macro autocomplete * remove all mentions of interruptsGeneration and purge * remove unused imports * fix syntax highlight with newline at end of input * cleanup select pointer events * coalesce onProgress call * add regex to STscript syntax highlighting * fix closure end * fix autocomplete type icon alignment * adjustments for small screens * fix removing wrong element * add missing "at=" arg to /sys, /comment, /sendas * add font scale setting for autocomplete * add target=_blank for parser flag links * fix for searching enums * remove REGEXP_MODE from hljs just causes trouble * fix autocomplete in closures * fix typo * fix type hint * Get rid of scroll bar on load * Add type hint for /send name argument. Fix 'at' types * Add 'negative' arg hint to /sd command * reenable blur event * Allow /summarize to process any text * Compact layout of script toggles * Expand CSS by default * fix double ranger indicator and adjust to narrow container * make custom css input fill available vertical space * reduce scroll lag * use default cursor on scrollbar * Clean-up module loading in index.html * fix tab indent with hljs --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
2024-05-12 21:15:05 +02:00
import { SlashCommand } from './slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashCommandArgument.js';
import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
import {
textgenerationwebui_preset_names,
textgenerationwebui_presets,
textgenerationwebui_settings as textgen_settings,
2023-12-02 19:04:51 +01:00
} from './textgen-settings.js';
import { download, parseJsonFile, waitUntilCondition } from './utils.js';
const presetManagers = {};
2023-12-12 18:14:17 +01:00
/**
* Automatically select a preset for current API based on character or group name.
*/
function autoSelectPreset() {
const presetManager = getPresetManager();
if (!presetManager) {
console.debug(`Preset Manager not found for API: ${main_api}`);
return;
}
const name = selected_group ? groups.find(x => x.id == selected_group)?.name : characters[this_chid]?.name;
if (!name) {
console.debug(`Preset candidate not found for API: ${main_api}`);
return;
}
const preset = presetManager.findPreset(name);
const selectedPreset = presetManager.getSelectedPreset();
if (preset === selectedPreset) {
console.debug(`Preset already selected for API: ${main_api}, name: ${name}`);
return;
}
if (preset !== undefined && preset !== null) {
console.log(`Preset found for API: ${main_api}, name: ${name}`);
presetManager.selectPreset(preset);
}
}
2023-12-12 18:14:17 +01:00
/**
* Gets a preset manager by API id.
* @param {string} apiId API id
* @returns {PresetManager} Preset manager
*/
function getPresetManager(apiId = '') {
if (!apiId) {
apiId = main_api == 'koboldhorde' ? 'kobold' : main_api;
}
if (!Object.keys(presetManagers).includes(apiId)) {
return null;
}
return presetManagers[apiId];
}
2023-12-12 18:14:17 +01:00
/**
* Registers preset managers for all select elements with data-preset-manager-for attribute.
*/
function registerPresetManagers() {
$('select[data-preset-manager-for]').each((_, e) => {
2023-12-02 19:04:51 +01:00
const forData = $(e).data('preset-manager-for');
for (const apiId of forData.split(',')) {
console.debug(`Registering preset manager for API: ${apiId}`);
presetManagers[apiId] = new PresetManager($(e), apiId);
}
});
}
class PresetManager {
constructor(select, apiId) {
this.select = select;
this.apiId = apiId;
}
2023-12-12 18:14:17 +01:00
/**
* Gets all preset names.
* @returns {string[]} List of preset names
*/
getAllPresets() {
return $(this.select).find('option').map((_, el) => el.text).toArray();
}
/**
* Finds a preset by name.
* @param {string} name Preset name
* @returns {any} Preset value
*/
findPreset(name) {
return $(this.select).find('option').filter(function() {
return $(this).text() === name;
}).val();
}
2023-12-12 18:14:17 +01:00
/**
* Gets the selected preset value.
* @returns {any} Selected preset value
*/
getSelectedPreset() {
2023-12-02 19:04:51 +01:00
return $(this.select).find('option:selected').val();
}
2023-12-12 18:14:17 +01:00
/**
* Gets the selected preset name.
* @returns {string} Selected preset name
*/
getSelectedPresetName() {
2023-12-02 19:04:51 +01:00
return $(this.select).find('option:selected').text();
}
2023-12-12 18:14:17 +01:00
/**
* Selects a preset by option value.
* @param {string} value Preset option value
*/
selectPreset(value) {
$(this.select).find(`option[value=${value}]`).prop('selected', true);
$(this.select).val(value).trigger('change');
}
async updatePreset() {
2023-12-02 19:04:51 +01:00
const selected = $(this.select).find('option:selected');
2023-12-02 20:11:06 +01:00
console.log(selected);
if (selected.val() == 'gui') {
toastr.info('Cannot update GUI preset');
return;
}
const name = selected.text();
await this.savePreset(name);
toastr.success('Preset updated');
}
async savePresetAs() {
2023-12-03 16:16:33 +01:00
const inputValue = this.getSelectedPresetName();
const popupText = `
<h3>Preset name:</h3>
${!this.isNonGenericApi() ? '<h4>Hint: Use a character/group name to bind preset to a specific chat.</h4>' : ''}`;
2023-12-03 16:16:33 +01:00
const name = await callPopup(popupText, 'input', inputValue);
2023-08-16 20:43:38 +02:00
if (!name) {
console.log('Preset name not provided');
return;
}
await this.savePreset(name);
toastr.success('Preset saved');
}
async savePreset(name, settings) {
const preset = settings ?? this.getPresetSettings(name);
2023-12-02 19:04:51 +01:00
const res = await fetch('/api/presets/save', {
method: 'POST',
headers: getRequestHeaders(),
2023-12-02 21:06:57 +01:00
body: JSON.stringify({ preset, name, apiId: this.apiId }),
});
if (!res.ok) {
toastr.error('Failed to save preset');
}
const data = await res.json();
name = data.name;
this.updateList(name, preset);
}
getPresetList() {
let presets = [];
let preset_names = {};
switch (this.apiId) {
2023-12-02 19:04:51 +01:00
case 'koboldhorde':
case 'kobold':
presets = koboldai_settings;
preset_names = koboldai_setting_names;
break;
2023-12-02 19:04:51 +01:00
case 'novel':
presets = novelai_settings;
preset_names = novelai_setting_names;
break;
2023-12-02 19:04:51 +01:00
case 'textgenerationwebui':
presets = textgenerationwebui_presets;
preset_names = textgenerationwebui_preset_names;
break;
2023-12-02 19:04:51 +01:00
case 'context':
2023-08-26 12:09:47 +02:00
presets = context_presets;
preset_names = context_presets.map(x => x.name);
break;
2023-12-02 19:04:51 +01:00
case 'instruct':
presets = instruct_presets;
preset_names = instruct_presets.map(x => x.name);
break;
default:
console.warn(`Unknown API ID ${this.apiId}`);
}
return { presets, preset_names };
}
isKeyedApi() {
2023-12-02 19:04:51 +01:00
return this.apiId == 'textgenerationwebui' || this.apiId == 'context' || this.apiId == 'instruct';
}
isNonGenericApi() {
2023-12-02 19:04:51 +01:00
return this.apiId == 'context' || this.apiId == 'instruct';
}
updateList(name, preset) {
const { presets, preset_names } = this.getPresetList();
const presetExists = this.isKeyedApi() ? preset_names.includes(name) : Object.keys(preset_names).includes(name);
if (presetExists) {
if (this.isKeyedApi()) {
presets[preset_names.indexOf(name)] = preset;
$(this.select).find(`option[value="${name}"]`).prop('selected', true);
2023-12-02 19:04:51 +01:00
$(this.select).val(name).trigger('change');
}
else {
const value = preset_names[name];
presets[value] = preset;
$(this.select).find(`option[value="${value}"]`).prop('selected', true);
2023-12-02 19:04:51 +01:00
$(this.select).val(value).trigger('change');
}
}
else {
presets.push(preset);
const value = presets.length - 1;
if (this.isKeyedApi()) {
preset_names[value] = name;
const option = $('<option></option>', { value: name, text: name, selected: true });
$(this.select).append(option);
2023-12-02 19:04:51 +01:00
$(this.select).val(name).trigger('change');
} else {
preset_names[name] = value;
const option = $('<option></option>', { value: value, text: name, selected: true });
$(this.select).append(option);
2023-12-02 19:04:51 +01:00
$(this.select).val(value).trigger('change');
}
}
}
getPresetSettings(name) {
function getSettingsByApiId(apiId) {
switch (apiId) {
2023-12-02 19:04:51 +01:00
case 'koboldhorde':
case 'kobold':
return kai_settings;
2023-12-02 19:04:51 +01:00
case 'novel':
return nai_settings;
2023-12-02 19:04:51 +01:00
case 'textgenerationwebui':
return textgen_settings;
2023-12-02 19:04:51 +01:00
case 'context': {
const context_preset = getContextSettings();
2023-08-26 12:09:47 +02:00
context_preset['name'] = name || power_user.context.preset;
return context_preset;
2023-12-02 16:14:06 +01:00
}
2023-12-02 19:04:51 +01:00
case 'instruct': {
2023-09-26 08:53:04 +02:00
const instruct_preset = structuredClone(power_user.instruct);
2023-08-26 12:09:47 +02:00
instruct_preset['name'] = name || power_user.instruct.preset;
return instruct_preset;
2023-12-02 16:14:06 +01:00
}
default:
console.warn(`Unknown API ID ${apiId}`);
return {};
}
}
2023-08-16 20:43:38 +02:00
const filteredKeys = [
'preset',
'streaming_url',
'stopping_strings',
'can_use_tokenization',
'can_use_streaming',
2023-08-16 20:43:38 +02:00
'preset_settings_novel',
'streaming_novel',
'nai_preamble',
'model_novel',
'streaming_kobold',
2023-12-02 19:04:51 +01:00
'enabled',
'bind_to_context',
2023-10-26 20:22:00 +02:00
'seed',
2023-12-20 14:51:00 +01:00
'legacy_api',
'mancer_model',
2023-12-20 14:51:00 +01:00
'togetherai_model',
'ollama_model',
2024-05-03 00:40:40 +02:00
'vllm_model',
2024-03-01 22:02:43 +01:00
'aphrodite_model',
2023-12-20 14:51:00 +01:00
'server_urls',
'type',
'custom_model',
'bypass_status_check',
'infermaticai_model',
'dreamgen_model',
'openrouter_model',
'max_tokens_second',
2024-05-06 18:26:20 +02:00
'openrouter_providers',
2023-08-16 20:43:38 +02:00
];
const settings = Object.assign({}, getSettingsByApiId(this.apiId));
for (const key of filteredKeys) {
2023-12-02 16:21:57 +01:00
if (Object.hasOwn(settings, key)) {
delete settings[key];
}
}
if (!this.isNonGenericApi()) {
settings['genamt'] = amount_gen;
settings['max_length'] = max_context;
}
return settings;
}
async deleteCurrentPreset() {
const { preset_names, presets } = this.getPresetList();
const value = this.getSelectedPreset();
const nameToDelete = this.getSelectedPresetName();
if (value == 'gui') {
toastr.info('Cannot delete GUI preset');
return;
}
$(this.select).find(`option[value="${value}"]`).remove();
if (this.isKeyedApi()) {
const index = preset_names.indexOf(nameToDelete);
preset_names.splice(index, 1);
presets.splice(index, 1);
} else {
delete preset_names[nameToDelete];
}
if (Object.keys(preset_names).length) {
const nextPresetName = Object.keys(preset_names)[0];
const newValue = preset_names[nextPresetName];
$(this.select).find(`option[value="${newValue}"]`).attr('selected', true);
$(this.select).trigger('change');
}
const response = await fetch('/api/presets/delete', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ name: nameToDelete, apiId: this.apiId }),
});
2023-12-03 16:16:33 +01:00
return response.ok;
}
async getDefaultPreset(name) {
const response = await fetch('/api/presets/restore', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({ name, apiId: this.apiId }),
});
if (!response.ok) {
2023-12-03 16:16:33 +01:00
toastr.error('Failed to restore default preset');
return;
}
2023-12-03 16:16:33 +01:00
return await response.json();
}
}
2023-12-12 18:14:17 +01:00
/**
* Selects a preset by name for current API.
* @param {any} _ Named arguments
* @param {string} name Unnamed arguments
* @returns {Promise<string>} Selected or current preset name
*/
async function presetCommandCallback(_, name) {
const shouldReconnect = online_status !== 'no_connection';
const presetManager = getPresetManager();
const allPresets = presetManager.getAllPresets();
const currentPreset = presetManager.getSelectedPresetName();
if (!presetManager) {
console.debug(`Preset Manager not found for API: ${main_api}`);
return '';
}
if (!name) {
console.log('No name provided for /preset command, using current preset');
return currentPreset;
}
if (!Array.isArray(allPresets) || allPresets.length === 0) {
console.log(`No presets found for API: ${main_api}`);
return currentPreset;
}
// Find exact match
const exactMatch = allPresets.find(p => p.toLowerCase().trim() === name.toLowerCase().trim());
if (exactMatch) {
console.log('Found exact preset match', exactMatch);
if (currentPreset !== exactMatch) {
const presetValue = presetManager.findPreset(exactMatch);
if (presetValue) {
presetManager.selectPreset(presetValue);
shouldReconnect && await waitForConnection();
}
}
return exactMatch;
} else {
// Find fuzzy match
const fuse = new Fuse(allPresets);
const fuzzyMatch = fuse.search(name);
if (!fuzzyMatch.length) {
console.warn(`WARN: Preset found with name ${name}`);
return currentPreset;
}
const fuzzyPresetName = fuzzyMatch[0].item;
const fuzzyPresetValue = presetManager.findPreset(fuzzyPresetName);
if (fuzzyPresetValue) {
console.log('Found fuzzy preset match', fuzzyPresetName);
if (currentPreset !== fuzzyPresetName) {
presetManager.selectPreset(fuzzyPresetValue);
shouldReconnect && await waitForConnection();
}
}
return fuzzyPresetName;
}
}
/**
* Waits for API connection to be established.
*/
async function waitForConnection() {
try {
2024-01-18 15:36:26 +01:00
await waitUntilCondition(() => online_status !== 'no_connection', 10000, 100);
2023-12-12 18:14:17 +01:00
} catch {
console.log('Timeout waiting for API to connect');
}
}
2023-12-12 18:14:17 +01:00
export async function initPresetManager() {
eventSource.on(event_types.CHAT_CHANGED, autoSelectPreset);
registerPresetManagers();
STscript Parser Rewrite (#1965) * set isForced to true on input * make floating auto-complete follow horizontal scrolling * add callable closure vars * changes to /let and /var for callable closures * fix error message * fix scope for closure arguments * if should return the pipe result from closures * use /run to call closures and no arguments on immediate closures * throw exception from QRs window-function if no match * when to show autocomplete vs info only * autocomplete positioning * autocomplete styling * add theming to autocomplete (theme, dark, light) * improve autocomplete show/hide logic and editor selection * use blur tint color instead of chat tint color and use blur setting * cleanup and docs * use scope macros for QR args * add enter to select autocomplete * fix no executor found * cleanup and comment * fix alias list in help string * fallback to empty string piped value if null or undefined * fix typo * blur textarea on ctrl+enter execute (and refocus after) * stop executeSlashCommand if parser throws * move /let and /var callbacks into functions * switch textarea to monospace when value starts with slash * add double pipe a pipe breaker * fix /? slash * remove some logging * add "/:name" as shorthand for "/run name" after all * move shit around * fix error message * use testRunShorthandEnd * use parseQuotedValue and parseValue to determine name for "/:" QR labels and set names can include spaces * add some adjustments to make autocomplete work properly some hint in there about "/:" would still be nice * add autocomplete style selector * only strip quotes from subcommand if they are at both ends * fix JSDoc * escaping * allow open quotes on dry run * throwing shit at the wall for /: autocomplete * escapes only for symbols * clean up autocomplete * improve performance * fix scope macros * remove unescaping of pipes * fix macros in scope copy * fix "/? slash" * don't run parser for getNameAt if text has not changed * fix options filter * re-enable blur listener * restore selection on non-replace select * fix for escaping first character of value * add support for {{pipe}} and {{var::}} closures * add index support to var macro * add scoped var macro to macro help * more escape fixes * reduce autocomplete render debounce * cleanup * restore old escape handling and parser flag for strict escaping * fix "no match" autocomplete message * add dummy commands for comments and parser flag * fix type annotations * somewhat safer macro replacements * fix autocomplete select on blank / "no match" * fix cutting off handled part in substitution * add parser flag REPLACE_GETVAR Replaces all {{getvar::}} and {{getglobalvar::}} macros with {{var::}}. Inserts a series of command executors before the command with the macros that: - save {{pipe}} to a var - call /getvar or /getglobalvar to get the variable used in the macro - call /let to save the retrieved variable - return the saved {{pipe}} value This helps to avoid double-substitutions when the var values contain text that could be interpreted as macros. * remove old parser * fix send on enter when no match * deal with pipes in quoted values (loose escaping) * add default parser flags to user settings * allow quoted values in unnamed argument * set parser flag without explicit state to "on" * add click hint on parser error toast * dirty more detailed cmd defs * remove name from unnamed arg * move autocomplete into class and floating with details * replace jQuery's trigger('input') on #send_textarea with native events because jQuery does not dispatch the native event * fix ctrl+space * fix arrow navigation * add comments * fix pointer block * add static fromProps * fix up dummy commands * migrate all commands to addCommandObject * remove commented comment command * fix alias in details * add range as argument type * switch to addCommandObject * switch to addCommandObject * fix height * fix floating details position on left * re-enable blur event * use auto width for full details on floating autocomplete * auto-size floating full details * fix typo * re-enable blur listener * don't prevent enter when selected item is fully typed out * add autocomplete details tooltips * add language to slash command examples * move makeItem into option and command and fix click select * use autocomplete parts in /? slash * fix alias formatting * add language to slash command examples * fix details position on initial input history * small screen styles * replace registerSlashCommand with detailed declarations * put name on first line * add missing returns * fix missing comma * fix alias display in autocomplete list * remove args from help string * move parser settings to its own section * jsdoc * hljs stscript lang * add hljs to autocomplete help examples * add missing import * apply autocomplete colors to stscript codeblocks (hljs) * add fromProps * cache autocomplete elements * towards generic autocomplete * remove unused imports * fix blanks * add return types * re-enable blur * fix blank check * Caption messages by id * add aborting command execution * fix return type * fix chat input font reset * add slash command progress indicator * add missing return * mark registerSlashCommand deprecated * why?? * separate abort logic for commands * remove parsing of quoted values from unnamed arg * add adjustable autocomplete width * revert stop button pulse * add progress and pause/abort to QR editor * add resize event on autocomplete width change * add key= argument to all get vars * refactoring * introduce NamedArgumentAsignment * add TODOs * refactoring * record start and end of named arg assignment * refactoring * prevent duplicate calls to show * refactoring * remove macro ac * add secondary autocomplete and enum descriptions * add syntax highlighting to QR editor * add enum descriptions to /while * add /let key=... to scope variable names * add unnamed argument assignment class and unnamed argument splitting * fix QR editor style * remove dash before autocomplete help text * add autocomplete for unnamed enums * fix remaining dom after holding backslash * fix for unnamed enums * fix autocomplete for /parser-flag * add parser-flag enum help * fix type annotations * fix autocomplete result for /: * add colored autocomplete type icons * collapse second line autocomplete help if empty * mark optional named args in autocomplete * fix when what * remove duplicate debug buttons * dispatch input on autocomplete select * prevent grow from editor syntax layer * add auto-adjust qr editor caret color * remove text-shadow from autocomplete * join value strings in /let and /var * add /abort syntax highlight * fix attempting secondary result when there is none * rename settings headers and split autocomplete / stscript * add parser flag tooltips * add tooltips to chat width stops * fix typo * return clone of help item * fix enum string * don't make optional notice for autocomplete arguments smaller * avoid scrollbar in chat input * add rudimentary macro autocomplete * strip macro from helptext * finally remove closure delimiters around root * cleanup * fix index stuff for removed closure delimiters * fix type hint * add child commands to progress indicator * include sub-separator in macro autocomplete * remove all mentions of interruptsGeneration and purge * remove unused imports * fix syntax highlight with newline at end of input * cleanup select pointer events * coalesce onProgress call * add regex to STscript syntax highlighting * fix closure end * fix autocomplete type icon alignment * adjustments for small screens * fix removing wrong element * add missing "at=" arg to /sys, /comment, /sendas * add font scale setting for autocomplete * add target=_blank for parser flag links * fix for searching enums * remove REGEXP_MODE from hljs just causes trouble * fix autocomplete in closures * fix typo * fix type hint * Get rid of scroll bar on load * Add type hint for /send name argument. Fix 'at' types * Add 'negative' arg hint to /sd command * reenable blur event * Allow /summarize to process any text * Compact layout of script toggles * Expand CSS by default * fix double ranger indicator and adjust to narrow container * make custom css input fill available vertical space * reduce scroll lag * use default cursor on scrollbar * Clean-up module loading in index.html * fix tab indent with hljs --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
2024-05-12 21:15:05 +02:00
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'preset',
callback: presetCommandCallback,
returns: 'current preset',
namedArgumentList: [],
unnamedArgumentList: [
new SlashCommandArgument(
'name', [ARGUMENT_TYPE.STRING], false,
),
],
helpString: `
<div>
Sets a preset by name for the current API. Gets the current preset if no name is provided.
</div>
<div>
<strong>Example:</strong>
<ul>
<li>
<pre><code>/preset myPreset</code></pre>
</li>
<li>
<pre><code>/preset</code></pre>
</li>
</ul>
</div>
`,
}));
2023-12-12 18:14:17 +01:00
2023-12-02 19:04:51 +01:00
$(document).on('click', '[data-preset-manager-update]', async function () {
const apiId = $(this).data('preset-manager-update');
const presetManager = getPresetManager(apiId);
if (!presetManager) {
console.warn(`Preset Manager not found for API: ${apiId}`);
return;
}
await presetManager.updatePreset();
});
2023-12-02 19:04:51 +01:00
$(document).on('click', '[data-preset-manager-new]', async function () {
const apiId = $(this).data('preset-manager-new');
const presetManager = getPresetManager(apiId);
if (!presetManager) {
console.warn(`Preset Manager not found for API: ${apiId}`);
return;
}
await presetManager.savePresetAs();
});
2023-12-02 19:04:51 +01:00
$(document).on('click', '[data-preset-manager-export]', async function () {
const apiId = $(this).data('preset-manager-export');
const presetManager = getPresetManager(apiId);
if (!presetManager) {
console.warn(`Preset Manager not found for API: ${apiId}`);
return;
}
2023-12-02 19:04:51 +01:00
const selected = $(presetManager.select).find('option:selected');
const name = selected.text();
const preset = presetManager.getPresetSettings(name);
const data = JSON.stringify(preset, null, 4);
2023-12-02 19:04:51 +01:00
download(data, `${name}.json`, 'application/json');
});
2023-12-02 19:04:51 +01:00
$(document).on('click', '[data-preset-manager-import]', async function () {
const apiId = $(this).data('preset-manager-import');
$(`[data-preset-manager-file="${apiId}"]`).trigger('click');
});
2023-12-02 19:04:51 +01:00
$(document).on('change', '[data-preset-manager-file]', async function (e) {
const apiId = $(this).data('preset-manager-file');
const presetManager = getPresetManager(apiId);
if (!presetManager) {
console.warn(`Preset Manager not found for API: ${apiId}`);
return;
}
const file = e.target.files[0];
if (!file) {
return;
}
2023-10-16 19:01:45 +02:00
const fileName = file.name.replace('.json', '').replace('.settings', '');
const data = await parseJsonFile(file);
2023-10-16 19:01:45 +02:00
const name = data?.name ?? fileName;
data['name'] = name;
await presetManager.savePreset(name, data);
toastr.success('Preset imported');
e.target.value = null;
});
2023-12-02 19:04:51 +01:00
$(document).on('click', '[data-preset-manager-delete]', async function () {
const apiId = $(this).data('preset-manager-delete');
const presetManager = getPresetManager(apiId);
if (!presetManager) {
console.warn(`Preset Manager not found for API: ${apiId}`);
return;
}
// default context preset cannot be deleted
2023-12-02 19:04:51 +01:00
if (apiId == 'context' && power_user.default_context === power_user.context.preset) {
return;
}
const confirm = await callPopup('Delete the preset? This action is irreversible and your current settings will be overwritten.', 'confirm');
if (!confirm) {
return;
}
2023-12-03 16:16:33 +01:00
const result = await presetManager.deleteCurrentPreset();
if (result) {
toastr.success('Preset deleted');
} else {
toastr.warning('Preset was not deleted from server');
}
saveSettingsDebounced();
});
2023-12-03 16:16:33 +01:00
2023-12-12 18:14:17 +01:00
$(document).on('click', '[data-preset-manager-restore]', async function () {
2023-12-03 16:16:33 +01:00
const apiId = $(this).data('preset-manager-restore');
const presetManager = getPresetManager(apiId);
if (!presetManager) {
console.warn(`Preset Manager not found for API: ${apiId}`);
return;
}
const name = presetManager.getSelectedPresetName();
const data = await presetManager.getDefaultPreset(name);
if (name == 'gui') {
toastr.info('Cannot restore GUI preset');
return;
}
if (!data) {
return;
}
if (data.isDefault) {
if (Object.keys(data.preset).length === 0) {
toastr.error('Default preset cannot be restored');
return;
}
const confirm = await callPopup('<h3>Are you sure?</h3>Resetting a <b>default preset</b> will restore the default settings.', 'confirm');
if (!confirm) {
return;
}
await presetManager.deleteCurrentPreset();
await presetManager.savePreset(name, data.preset);
const option = presetManager.findPreset(name);
presetManager.selectPreset(option);
toastr.success('Default preset restored');
} else {
const confirm = await callPopup('<h3>Are you sure?</h3>Resetting a <b>custom preset</b> will restore to the last saved state.', 'confirm');
if (!confirm) {
return;
}
const option = presetManager.findPreset(name);
presetManager.selectPreset(option);
toastr.success('Preset restored');
}
});
2023-12-12 18:14:17 +01:00
}