| 3787 | } | 3830 | } |
| 3788 | | 3831 | |
| 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 | */ |
| | 3839 | function 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 arguments | 3903 | * @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) |