add /getpromptentry command

4cf5d1535e72befb6509b77dd1d838f34d438dff

Succubyss <87207237+Succubyss@users.noreply.github.com>

1 files changed, +112 -0Ignore whitespace
public/scripts/slash-commands.js+112 -0
@@ -1699,6 +1699,49 @@ export function initDefaultSlashCommands() {
1699 helpString: 'Sets the model for the current API. Gets the current model name if no argument is provided.',1699 helpString: 'Sets the model for the current API. Gets the current model name if no argument is provided.',
1700 }));1700 }));
1701 SlashCommandParser.addCommandObject(SlashCommand.fromProps({1701 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
1702 name: 'getpromptentry',
1703 aliases: ['getpromptentries'],
1704 callback: getPromptEntryCallback,
1705 returns: 'true/false state of prompt(s)',
1706 namedArgumentList: [
1707 SlashCommandNamedArgument.fromProps({
1708 name: 'identifier',
1709 description: 'Prompt entry identifier(s) to retrieve',
1710 typeList: [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.LIST],
1711 acceptsMultiple: true,
1712 enumProvider: () =>
1713 promptManager.serviceSettings.prompts
1714 .map(prompt => prompt.identifier)
1715 .map(identifier => new SlashCommandEnumValue(identifier, identifier)),
1716 }),
1717 SlashCommandNamedArgument.fromProps({
1718 name: 'name',
1719 description: 'Prompt entry name(s) to retrieve',
1720 typeList: [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.LIST],
1721 acceptsMultiple: true,
1722 enumProvider: () =>
1723 promptManager.serviceSettings.prompts
1724 .map(prompt => prompt.name)
1725 .map(name => new SlashCommandEnumValue(name, name)),
1726 }),
1727 SlashCommandNamedArgument.fromProps({
1728 name: 'return',
1729 description: 'Whether the return will be simple, a list, or a dict.',
1730 typeList: [ARGUMENT_TYPE.STRING],
1731 defaultValue: 'simple',
1732 enumList: ['simple', 'list', 'dict'],
1733 }),
1734 ],
1735 helpString: `
1736 <div>
1737 Gets the state of the specified prompt entries.
1738 </div>
1739 <div>
1740 If <code>return</code> is <code>simple</code> (default) then the return will be a single value if only one value was retrieved; otherwise uses a dict (if the identifier parameter was used) or a list.
1741 </div>
1742 `,
1743 }));
1744 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
1702 name: 'setpromptentry',1745 name: 'setpromptentry',
1703 aliases: ['setpromptentries'],1746 aliases: ['setpromptentries'],
1704 callback: setPromptEntryCallback,1747 callback: setPromptEntryCallback,
@@ -3787,6 +3830,75 @@ function modelCallback(args, model) {
3787}3830}
37883831
3789/**3832/**
3833 * Gets the state of prompt entries (toggles) either via identifier/uuid or name.
3834 * @param {object} args Object containing arguments
3835 * @param {string} args.identifier Select prompt entry using an identifier (uuid)
3836 * @param {string} args.name Select prompt entry using name
3837 * @returns {Object} An object containing the states of the requested prompt entries
3838 */
3839function getPromptEntryCallback(args) {
3840 const promptManager = setupChatCompletionPromptManager(oai_settings);
3841 const prompts = promptManager.serviceSettings.prompts;
3842 let returnType = args.return ?? 'simple';
3843
3844 function parseArgs(arg) {
3845 // Arg is already an array
3846 if (Array.isArray(arg)) {
3847 return arg;
3848 }
3849 const list = [];
3850 try {
3851 // Arg is a JSON-stringified array
3852 const parsedArg = JSON.parse(arg);
3853 list.push(...Array.isArray(parsedArg) ? parsedArg : [arg]);
3854 } catch {
3855 // Arg is a string
3856 list.push(arg);
3857 }
3858 return list;
3859 }
3860
3861 let identifiersList = parseArgs(args.identifier);
3862 let nameList = parseArgs(args.name);
3863
3864 // Check if identifiers exists in prompt, else remove from list
3865 if (identifiersList.length !== 0) {
3866 identifiersList = identifiersList.filter(identifier => prompts.some(prompt => prompt.identifier === identifier));
3867 }
3868
3869 if (nameList.length !== 0) {
3870 nameList.forEach(name => {
3871 let identifiers = prompts
3872 .filter(entry => entry.name === name)
3873 .map(entry => entry.identifier);
3874 identifiersList = identifiersList.concat(identifiers);
3875 });
3876 }
3877
3878 // Get the state for each prompt entry
3879 let promptStates = new Map();
3880 identifiersList.forEach(identifier => {
3881 const promptOrderEntry = promptManager.getPromptOrderEntry(promptManager.activeCharacter, identifier);
3882 if (promptOrderEntry) {
3883 promptStates.set(identifier, promptOrderEntry.enabled);
3884 }
3885 });
3886
3887 // If return is simple (default) but more than one prompt state was retrieved, then change return type
3888 if (returnType === 'simple' && promptStates.size > 1) {
3889 returnType = args.identifier ? 'dict' : 'list';
3890 }
3891
3892 const result = (() => {
3893 if (returnType === 'list') return [...promptStates.values()];
3894 if (returnType === 'dict') return Object.fromEntries(promptStates);
3895 return [...promptStates.values()][0];
3896 })();
3897
3898 return result;
3899}
3900
3901/**
3790 * Sets state of prompt entries (toggles) either via identifier/uuid or name.3902 * Sets state of prompt entries (toggles) either via identifier/uuid or name.
3791 * @param {object} args Object containing arguments3903 * @param {object} args Object containing arguments
3792 * @param {string} args.identifier Select prompt entry using an identifier (uuid)3904 * @param {string} args.identifier Select prompt entry using an identifier (uuid)