mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-13 10:42:55 +01:00
Refactor prompt entry callback
This commit is contained in:
parent
0fe579e782
commit
861decd5c9
@ -116,12 +116,13 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
|||||||
aliases: ['background'],
|
aliases: ['background'],
|
||||||
returns: 'the current background',
|
returns: 'the current background',
|
||||||
unnamedArgumentList: [
|
unnamedArgumentList: [
|
||||||
SlashCommandArgument.fromProps({ description: 'filename',
|
SlashCommandArgument.fromProps({
|
||||||
|
description: 'filename',
|
||||||
typeList: [ARGUMENT_TYPE.STRING],
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
enumProvider: ()=>[...document.querySelectorAll('.bg_example')]
|
enumProvider: () => [...document.querySelectorAll('.bg_example')]
|
||||||
.map((it)=>new SlashCommandEnumValue(it.getAttribute('bgfile')))
|
.map((it) => new SlashCommandEnumValue(it.getAttribute('bgfile')))
|
||||||
.filter(it=>it.value?.length)
|
.filter(it => it.value?.length)
|
||||||
,
|
,
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
@ -327,12 +328,13 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
|||||||
name: 'go',
|
name: 'go',
|
||||||
callback: goToCharacterCallback,
|
callback: goToCharacterCallback,
|
||||||
unnamedArgumentList: [
|
unnamedArgumentList: [
|
||||||
SlashCommandArgument.fromProps({ description: 'name',
|
SlashCommandArgument.fromProps({
|
||||||
|
description: 'name',
|
||||||
typeList: [ARGUMENT_TYPE.STRING],
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
enumProvider: ()=>[
|
enumProvider: () => [
|
||||||
...characters.map(it=>new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
|
...characters.map(it => new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
|
||||||
...groups.map(it=>new SlashCommandEnumValue(it.name, null, 'variable', 'G')),
|
...groups.map(it => new SlashCommandEnumValue(it.name, null, 'variable', 'G')),
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
@ -376,11 +378,12 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
|||||||
name: 'ask',
|
name: 'ask',
|
||||||
callback: askCharacter,
|
callback: askCharacter,
|
||||||
namedArgumentList: [
|
namedArgumentList: [
|
||||||
SlashCommandNamedArgument.fromProps({ name: 'name',
|
SlashCommandNamedArgument.fromProps({
|
||||||
|
name: 'name',
|
||||||
description: 'character name',
|
description: 'character name',
|
||||||
typeList: [ARGUMENT_TYPE.STRING],
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
enumProvider: ()=>characters.map(it=>new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
|
enumProvider: () => characters.map(it => new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
unnamedArgumentList: [
|
unnamedArgumentList: [
|
||||||
@ -1218,7 +1221,8 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
unnamedArgumentList: [
|
unnamedArgumentList: [
|
||||||
SlashCommandArgument.fromProps({ description: 'Set entry/entries on or off',
|
SlashCommandArgument.fromProps({
|
||||||
|
description: 'Set entry/entries on or off',
|
||||||
typeList: [ARGUMENT_TYPE.STRING],
|
typeList: [ARGUMENT_TYPE.STRING],
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
acceptsMultiple: false,
|
acceptsMultiple: false,
|
||||||
@ -1226,7 +1230,7 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
|||||||
enumList: ['on', 'off', 'toggle'],
|
enumList: ['on', 'off', 'toggle'],
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
helpString: 'Sets the toggles for prompt entries. Toggle by default.',
|
helpString: 'Sets the specified prompt manager entry/entries on or off.',
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerVariableCommands();
|
registerVariableCommands();
|
||||||
@ -2863,29 +2867,23 @@ function setPromptEntryCallback(args, targetState) {
|
|||||||
const promptManager = setupChatCompletionPromptManager(oai_settings);
|
const promptManager = setupChatCompletionPromptManager(oai_settings);
|
||||||
const prompts = promptManager.serviceSettings.prompts;
|
const prompts = promptManager.serviceSettings.prompts;
|
||||||
|
|
||||||
let identifiersList = [];
|
function parseArgs(arg) {
|
||||||
// Check identifiers args
|
const list = [];
|
||||||
try {
|
try {
|
||||||
const parsedIdentifiers = JSON.parse(args.identifier);
|
const parsedArg = JSON.parse(arg);
|
||||||
identifiersList = identifiersList.concat(Array.isArray(parsedIdentifiers) ? parsedIdentifiers : [args.identifier]);
|
list.push(...Array.isArray(parsedArg) ? parsedArg : [arg]);
|
||||||
} catch {
|
} catch {
|
||||||
identifiersList.push(args.identifier);
|
list.push(arg);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let identifiersList = parseArgs(args.identifier);
|
||||||
|
let nameList = parseArgs(args.name);
|
||||||
|
|
||||||
// Check if identifiers exists in prompt, else remove from list
|
// Check if identifiers exists in prompt, else remove from list
|
||||||
if (identifiersList.length !== 0) {
|
if (identifiersList.length !== 0) {
|
||||||
identifiersList = identifiersList.filter(identifier => {
|
identifiersList = identifiersList.filter(identifier => prompts.some(prompt => prompt.identifier === identifier));
|
||||||
return prompts.some(prompt => prompt.identifier === identifier);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let nameList = [];
|
|
||||||
// Get list of names
|
|
||||||
try {
|
|
||||||
const parsedNames = JSON.parse(args.name);
|
|
||||||
nameList = nameList.concat(Array.isArray(parsedNames) ? parsedNames : [args.name]);
|
|
||||||
} catch {
|
|
||||||
nameList.push(args.name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nameList.length !== 0) {
|
if (nameList.length !== 0) {
|
||||||
@ -2900,38 +2898,36 @@ function setPromptEntryCallback(args, targetState) {
|
|||||||
identifiersList = identifiersList.concat(identifiers);
|
identifiersList = identifiersList.concat(identifiers);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove duplicates to allow consistent 'toggle'
|
// Remove duplicates to allow consistent 'toggle'
|
||||||
identifiersList = [...new Set(identifiersList)];
|
identifiersList = [...new Set(identifiersList)];
|
||||||
if (identifiersList.length === 0) return '';
|
if (identifiersList.length === 0) return '';
|
||||||
|
|
||||||
// logic adapted from PromptManager.js, handleToggle
|
// logic adapted from PromptManager.js, handleToggle
|
||||||
if (['toggle', 't', ''].includes(targetState.trim().toLowerCase())){
|
const getPromptOrderEntryState = (promptOrderEntry) => {
|
||||||
identifiersList.forEach(promptID => {
|
if (['toggle', 't', ''].includes(targetState.trim().toLowerCase())) {
|
||||||
const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, promptID);
|
return !promptOrderEntry.enabled;
|
||||||
const counts = promptManager.tokenHandler.getCounts();
|
}
|
||||||
|
|
||||||
counts[promptID] = null;
|
if (isTrueBoolean(targetState)) {
|
||||||
promptOrderEntry.enabled = !promptOrderEntry.enabled;
|
return true;
|
||||||
});
|
}
|
||||||
}
|
|
||||||
if (isTrueBoolean(targetState)) {
|
|
||||||
identifiersList.forEach(promptID => {
|
|
||||||
const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, promptID);
|
|
||||||
const counts = promptManager.tokenHandler.getCounts();
|
|
||||||
|
|
||||||
counts[promptID] = null;
|
if (isFalseBoolean(targetState)) {
|
||||||
promptOrderEntry.enabled = true;
|
return false;
|
||||||
});
|
}
|
||||||
}
|
|
||||||
if (isFalseBoolean(targetState)) {
|
return promptOrderEntry.enabled;
|
||||||
identifiersList.forEach(promptID => {
|
};
|
||||||
const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, promptID);
|
|
||||||
const counts = promptManager.tokenHandler.getCounts();
|
identifiersList.forEach(promptID => {
|
||||||
|
const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, promptID);
|
||||||
|
const counts = promptManager.tokenHandler.getCounts();
|
||||||
|
|
||||||
|
counts[promptID] = null;
|
||||||
|
promptOrderEntry.enabled = getPromptOrderEntryState(promptOrderEntry);
|
||||||
|
});
|
||||||
|
|
||||||
counts[promptID] = null;
|
|
||||||
promptOrderEntry.enabled = false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// no need to render for each identifier
|
// no need to render for each identifier
|
||||||
promptManager.render();
|
promptManager.render();
|
||||||
promptManager.saveServiceSettings();
|
promptManager.saveServiceSettings();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user