2023-07-23 22:52:31 +02:00
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 ,
2023-07-23 22:52:31 +02:00
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' ;
2023-12-12 18:14:17 +01:00
import { registerSlashCommand } from './slash-commands.js' ;
2023-07-23 22:52:31 +02:00
import {
textgenerationwebui _preset _names ,
textgenerationwebui _presets ,
2023-12-03 16:03:19 +01:00
textgenerationwebui _settings as textgen _settings ,
2023-12-02 19:04:51 +01:00
} from './textgen-settings.js' ;
import { download , parseJsonFile , waitUntilCondition } from './utils.js' ;
2023-07-23 22:52:31 +02:00
const presetManagers = { } ;
2023-12-12 18:14:17 +01:00
/ * *
* Automatically select a preset for current API based on character or group name .
* /
2023-07-23 22:52:31 +02:00
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 = '' ) {
2023-08-20 22:29:43 +02:00
if ( ! apiId ) {
apiId = main _api == 'koboldhorde' ? 'kobold' : main _api ;
}
2023-07-23 22:52:31 +02:00
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 .
* /
2023-07-23 22:52:31 +02:00
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 ( ',' ) ) {
2023-07-23 22:52:31 +02:00
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
* /
2023-07-23 22:52:31 +02:00
findPreset ( name ) {
return $ ( this . select ) . find ( ` option:contains( ${ name } ) ` ) . val ( ) ;
}
2023-12-12 18:14:17 +01:00
/ * *
* Gets the selected preset value .
* @ returns { any } Selected preset value
* /
2023-07-23 22:52:31 +02:00
getSelectedPreset ( ) {
2023-12-02 19:04:51 +01:00
return $ ( this . select ) . find ( 'option:selected' ) . val ( ) ;
2023-07-23 22:52:31 +02:00
}
2023-12-12 18:14:17 +01:00
/ * *
* Gets the selected preset name .
* @ returns { string } Selected preset name
* /
2023-07-23 22:52:31 +02:00
getSelectedPresetName ( ) {
2023-12-02 19:04:51 +01:00
return $ ( this . select ) . find ( 'option:selected' ) . text ( ) ;
2023-07-23 22:52:31 +02:00
}
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' ) ;
2023-07-23 22:52:31 +02:00
}
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 ) ;
2023-07-23 22:52:31 +02:00
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 ( ) ;
2023-07-23 22:52:31 +02:00
const popupText = `
< h3 > Preset name : < / h 3 >
2023-08-22 22:03:55 +02:00
$ { ! 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 ;
}
2023-07-23 22:52:31 +02:00
await this . savePreset ( name ) ;
toastr . success ( 'Preset saved' ) ;
}
async savePreset ( name , settings ) {
2023-08-22 22:03:55 +02:00
const preset = settings ? ? this . getPresetSettings ( name ) ;
2023-12-02 19:04:51 +01:00
const res = await fetch ( '/api/presets/save' , {
method : 'POST' ,
2023-07-23 22:52:31 +02:00
headers : getRequestHeaders ( ) ,
2023-12-02 21:06:57 +01:00
body : JSON . stringify ( { preset , name , apiId : this . apiId } ) ,
2023-07-23 22:52:31 +02:00
} ) ;
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' :
2023-07-23 22:52:31 +02:00
presets = koboldai _settings ;
preset _names = koboldai _setting _names ;
break ;
2023-12-02 19:04:51 +01:00
case 'novel' :
2023-07-23 22:52:31 +02:00
presets = novelai _settings ;
preset _names = novelai _setting _names ;
break ;
2023-12-02 19:04:51 +01:00
case 'textgenerationwebui' :
2023-07-23 22:52:31 +02:00
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' :
2023-08-20 23:56:53 +02:00
presets = instruct _presets ;
preset _names = instruct _presets . map ( x => x . name ) ;
break ;
2023-07-23 22:52:31 +02:00
default :
console . warn ( ` Unknown API ID ${ this . apiId } ` ) ;
}
return { presets , preset _names } ;
}
2023-08-20 23:56:53 +02:00
isKeyedApi ( ) {
2023-12-02 19:04:51 +01:00
return this . apiId == 'textgenerationwebui' || this . apiId == 'context' || this . apiId == 'instruct' ;
2023-08-20 23:56:53 +02:00
}
isNonGenericApi ( ) {
2023-12-02 19:04:51 +01:00
return this . apiId == 'context' || this . apiId == 'instruct' ;
2023-08-20 23:56:53 +02:00
}
2023-07-23 22:52:31 +02:00
updateList ( name , preset ) {
const { presets , preset _names } = this . getPresetList ( ) ;
2023-08-20 23:56:53 +02:00
const presetExists = this . isKeyedApi ( ) ? preset _names . includes ( name ) : Object . keys ( preset _names ) . includes ( name ) ;
2023-07-23 22:52:31 +02:00
if ( presetExists ) {
2023-08-20 23:56:53 +02:00
if ( this . isKeyedApi ( ) ) {
2023-07-23 22:52:31 +02:00
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' ) ;
2023-07-23 22:52:31 +02:00
}
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' ) ;
2023-07-23 22:52:31 +02:00
}
}
else {
presets . push ( preset ) ;
const value = presets . length - 1 ;
2023-08-20 23:56:53 +02:00
if ( this . isKeyedApi ( ) ) {
2023-07-23 22:52:31 +02:00
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' ) ;
2023-07-23 22:52:31 +02:00
} 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' ) ;
2023-07-23 22:52:31 +02:00
}
}
}
2023-08-22 22:03:55 +02:00
getPresetSettings ( name ) {
2023-07-23 22:52:31 +02:00
function getSettingsByApiId ( apiId ) {
switch ( apiId ) {
2023-12-02 19:04:51 +01:00
case 'koboldhorde' :
case 'kobold' :
2023-07-23 22:52:31 +02:00
return kai _settings ;
2023-12-02 19:04:51 +01:00
case 'novel' :
2023-07-23 22:52:31 +02:00
return nai _settings ;
2023-12-02 19:04:51 +01:00
case 'textgenerationwebui' :
2023-12-03 16:03:19 +01:00
return textgen _settings ;
2023-12-02 19:04:51 +01:00
case 'context' : {
2023-10-20 07:56:17 +02:00
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
}
2023-07-23 22:52:31 +02:00
default :
console . warn ( ` Unknown API ID ${ apiId } ` ) ;
return { } ;
}
}
2023-08-16 20:43:38 +02:00
const filteredKeys = [
'preset' ,
'streaming_url' ,
'stopping_strings' ,
2023-08-24 20:23:35 +02:00
'can_use_tokenization' ,
'can_use_streaming' ,
2023-08-16 20:43:38 +02:00
'preset_settings_novel' ,
'streaming_novel' ,
'nai_preamble' ,
'model_novel' ,
2023-10-07 18:25:36 +02:00
'streaming_kobold' ,
2023-12-02 19:04:51 +01:00
'enabled' ,
2023-12-21 14:12:30 +01:00
'bind_to_context' ,
2023-10-26 20:22:00 +02:00
'seed' ,
2023-12-20 14:51:00 +01:00
'legacy_api' ,
2023-11-07 23:17:13 +01:00
'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' ,
2024-01-05 18:15:07 +01:00
'custom_model' ,
'bypass_status_check' ,
2024-02-25 21:47:07 +01:00
'infermaticai_model' ,
2024-03-07 10:55:08 +01:00
'dreamgen_model' ,
2024-02-25 21:47:07 +01:00
'openrouter_model' ,
2024-02-29 10:33:47 +01:00
'max_tokens_second' ,
2023-08-16 20:43:38 +02:00
] ;
2023-07-23 22:52:31 +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 ) ) {
2023-07-23 22:52:31 +02:00
delete settings [ key ] ;
}
}
2023-08-20 23:56:53 +02:00
if ( ! this . isNonGenericApi ( ) ) {
settings [ 'genamt' ] = amount _gen ;
settings [ 'max_length' ] = max _context ;
}
2023-07-23 22:52:31 +02:00
return settings ;
}
async deleteCurrentPreset ( ) {
2023-12-17 20:02:32 +01:00
const { preset _names , presets } = this . getPresetList ( ) ;
2023-07-23 22:52:31 +02:00
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 ( ) ;
2023-08-20 23:56:53 +02:00
if ( this . isKeyedApi ( ) ) {
2023-12-17 20:02:32 +01:00
const index = preset _names . indexOf ( nameToDelete ) ;
preset _names . splice ( index , 1 ) ;
presets . splice ( index , 1 ) ;
2023-07-23 22:52:31 +02:00
} 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' ) ;
}
2023-09-16 16:36:54 +02:00
const response = await fetch ( '/api/presets/delete' , {
2023-07-23 22:52:31 +02:00
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 } ) ,
} ) ;
2023-07-23 22:52:31 +02:00
if ( ! response . ok ) {
2023-12-03 16:16:33 +01:00
toastr . error ( 'Failed to restore default preset' ) ;
return ;
2023-07-23 22:52:31 +02:00
}
2023-12-03 16:16:33 +01:00
return await response . json ( ) ;
2023-07-23 22:52:31 +02:00
}
}
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-07-23 22:52:31 +02:00
2023-12-12 18:14:17 +01:00
export async function initPresetManager ( ) {
2023-07-23 22:52:31 +02:00
eventSource . on ( event _types . CHAT _CHANGED , autoSelectPreset ) ;
registerPresetManagers ( ) ;
2024-04-02 23:52:30 +02:00
registerSlashCommand ( 'preset' , presetCommandCallback , [ ] , '<span class="monospace">(name)</span> – sets a preset by name for the current API. Gets the current preset if no name is provided' , true , true ) ;
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' ) ;
2023-08-20 22:29:43 +02:00
const presetManager = getPresetManager ( apiId ) ;
2023-07-23 22:52:31 +02:00
if ( ! presetManager ) {
2023-08-20 22:29:43 +02:00
console . warn ( ` Preset Manager not found for API: ${ apiId } ` ) ;
2023-07-23 22:52:31 +02:00
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' ) ;
2023-08-20 22:29:43 +02:00
const presetManager = getPresetManager ( apiId ) ;
2023-07-23 22:52:31 +02:00
if ( ! presetManager ) {
2023-08-20 22:29:43 +02:00
console . warn ( ` Preset Manager not found for API: ${ apiId } ` ) ;
2023-07-23 22:52:31 +02:00
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' ) ;
2023-08-20 22:29:43 +02:00
const presetManager = getPresetManager ( apiId ) ;
2023-07-23 22:52:31 +02:00
if ( ! presetManager ) {
2023-08-20 22:29:43 +02:00
console . warn ( ` Preset Manager not found for API: ${ apiId } ` ) ;
2023-07-23 22:52:31 +02:00
return ;
}
2023-12-02 19:04:51 +01:00
const selected = $ ( presetManager . select ) . find ( 'option:selected' ) ;
2023-07-23 22:52:31 +02:00
const name = selected . text ( ) ;
2023-08-22 22:03:55 +02:00
const preset = presetManager . getPresetSettings ( name ) ;
2023-07-23 22:52:31 +02:00
const data = JSON . stringify ( preset , null , 4 ) ;
2023-12-02 19:04:51 +01:00
download ( data , ` ${ name } .json ` , 'application/json' ) ;
2023-07-23 22:52:31 +02:00
} ) ;
2023-12-02 19:04:51 +01:00
$ ( document ) . on ( 'click' , '[data-preset-manager-import]' , async function ( ) {
const apiId = $ ( this ) . data ( 'preset-manager-import' ) ;
2023-08-20 23:56:53 +02:00
$ ( ` [data-preset-manager-file=" ${ apiId } "] ` ) . trigger ( 'click' ) ;
2023-07-23 22:52:31 +02:00
} ) ;
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' ) ;
2023-08-20 23:56:53 +02:00
const presetManager = getPresetManager ( apiId ) ;
2023-07-23 22:52:31 +02:00
if ( ! presetManager ) {
2023-08-20 23:56:53 +02:00
console . warn ( ` Preset Manager not found for API: ${ apiId } ` ) ;
2023-07-23 22:52:31 +02:00
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' , '' ) ;
2023-07-23 22:52:31 +02:00
const data = await parseJsonFile ( file ) ;
2023-10-16 19:01:45 +02:00
const name = data ? . name ? ? fileName ;
data [ 'name' ] = name ;
2023-07-23 22:52:31 +02:00
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' ) ;
2023-08-20 22:29:43 +02:00
const presetManager = getPresetManager ( apiId ) ;
2023-07-23 22:52:31 +02:00
if ( ! presetManager ) {
2023-08-20 22:29:43 +02:00
console . warn ( ` Preset Manager not found for API: ${ apiId } ` ) ;
2023-07-23 22:52:31 +02:00
return ;
}
2023-08-26 14:52:23 +02:00
// 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 ) {
2023-08-26 14:52:23 +02:00
return ;
}
2023-07-23 22:52:31 +02:00
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' ) ;
}
2023-07-23 22:52:31 +02:00
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
}