Refactor prompt entry callback

This commit is contained in:
Cohee 2024-06-16 13:56:43 +03:00
parent 0fe579e782
commit 861decd5c9

View File

@ -116,7 +116,8 @@ 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')]
@ -327,7 +328,8 @@ 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: () => [
@ -376,7 +378,8 @@ 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,
@ -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
const getPromptOrderEntryState = (promptOrderEntry) => {
if (['toggle', 't', ''].includes(targetState.trim().toLowerCase())) { if (['toggle', 't', ''].includes(targetState.trim().toLowerCase())) {
identifiersList.forEach(promptID => { return !promptOrderEntry.enabled;
const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, promptID);
const counts = promptManager.tokenHandler.getCounts();
counts[promptID] = null;
promptOrderEntry.enabled = !promptOrderEntry.enabled;
});
} }
if (isTrueBoolean(targetState)) { if (isTrueBoolean(targetState)) {
identifiersList.forEach(promptID => { return true;
const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, promptID);
const counts = promptManager.tokenHandler.getCounts();
counts[promptID] = null;
promptOrderEntry.enabled = true;
});
} }
if (isFalseBoolean(targetState)) { if (isFalseBoolean(targetState)) {
return false;
}
return promptOrderEntry.enabled;
};
identifiersList.forEach(promptID => { identifiersList.forEach(promptID => {
const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, promptID); const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, promptID);
const counts = promptManager.tokenHandler.getCounts(); const counts = promptManager.tokenHandler.getCounts();
counts[promptID] = null; counts[promptID] = null;
promptOrderEntry.enabled = false; promptOrderEntry.enabled = getPromptOrderEntryState(promptOrderEntry);
}); });
}
// no need to render for each identifier // no need to render for each identifier
promptManager.render(); promptManager.render();
promptManager.saveServiceSettings(); promptManager.saveServiceSettings();