Refactor /listinjects, deprecate its "format" arg
| @@ -1525,11 +1525,20 @@ export function initDefaultSlashCommands() { | ||
| 1525 | 1525 | name: 'listinjects', |
| 1526 | 1526 | callback: listInjectsCallback, |
| 1527 | 1527 | helpString: 'Lists all script injections for the current chat. Displays injects in a popup by default. Use the <code>format</code> argument to change the output format.', |
| 1528 | 1528 | returns: 'Optionalls the JSON object of script injections', |
| 1529 | 1529 | namedArgumentList: [ |
| 1530 | 1530 | SlashCommandNamedArgument.fromProps({ |
| 1531 | + name: 'return', | |
| 1532 | + description: 'The way how you want the return value to be provided', | |
| 1533 | + typeList: [ARGUMENT_TYPE.STRING], | |
| 1534 | + defaultValue: 'object', | |
| 1535 | + enumList: slashCommandReturnHelper.enumList({ allowPipe: false, allowObject: true, allowChat: true, allowPopup: true, allowTextVersion: false }), | |
| 1536 | + forceEnum: true, | |
| 1537 | + }), | |
| 1538 | + // TODO remove some day | |
| 1539 | + SlashCommandNamedArgument.fromProps({ | |
| 1531 | 1540 | name: 'format', |
| 1532 | 1541 | description: 'DEPRECATED - use "return" instead - output format', |
| 1533 | 1542 | typeList: [ARGUMENT_TYPE.STRING], |
| 1534 | 1543 | isRequired: true, |
| 1535 | 1544 | forceEnum: true, |
| @@ -1798,37 +1807,44 @@ function injectCallback(args, value) { | ||
| 1798 | 1807 | } |
| 1799 | 1808 | |
| 1800 | 1809 | async function listInjectsCallback(args) { |
| 1801 | - const type = String(args?.format).toLowerCase().trim(); | |
| 1810 | + /** @type {import('./slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */ | |
| 1802 | - if (!chat_metadata.script_injects || !Object.keys(chat_metadata.script_injects).length) { | |
| 1811 | + let returnType = args.return; | |
| 1803 | - type !== 'none' && toastr.info('No script injections for the current chat'); | |
| 1812 | + | |
| 1804 | - return JSON.stringify({}); | |
| 1813 | + // Old legacy return type handling | |
| 1805 | - } | |
| 1814 | + if (args.format) { | |
| 1806 | - | |
| 1815 | + toastr.warning(`Legacy argument 'format' with value '${args.format}' is deprecated. Please use 'return' instead. Routing to the correct return type...`, 'Deprecation warning'); | |
| 1807 | - const injects = Object.entries(chat_metadata.script_injects) | |
| 1816 | + const type = String(args?.format).toLowerCase().trim(); | |
| 1808 | - .map(([id, inject]) => { | |
| 1817 | + if (!chat_metadata.script_injects || !Object.keys(chat_metadata.script_injects).length) { | |
| 1809 | - const position = Object.entries(extension_prompt_types); | |
| 1818 | + type !== 'none' && toastr.info('No script injections for the current chat'); | |
| 1810 | - const positionName = position.find(([_, value]) => value === inject.position)?.[0] ?? 'unknown'; | |
| 1819 | + } | |
| 1811 | - return `* **${id}**: <code>${inject.value}</code> (${positionName}, depth: ${inject.depth}, scan: ${inject.scan ?? false}, role: ${inject.role ?? extension_prompt_roles.SYSTEM})`; | |
| 1820 | + switch (type) { | |
| 1812 | - }) | |
| 1821 | + case 'none': | |
| 1813 | - .join('\n'); | |
| 1822 | + returnType = 'none'; | |
| 1814 | - | |
| 1823 | + break; | |
| 1815 | - const converter = new showdown.Converter(); | |
| 1824 | + case 'chat': | |
| 1816 | - const messageText = `### Script injections:\n${injects}`; | |
| 1825 | + returnType = 'chat-html'; | |
| 1817 | - const htmlMessage = DOMPurify.sanitize(converter.makeHtml(messageText)); | |
| 1826 | + break; | |
| 1818 | - | |
| 1827 | + case 'popup': | |
| 1819 | - switch (type) { | |
| 1828 | + default: | |
| 1820 | - case 'none': | |
| 1829 | + returnType = 'popup-html'; | |
| 1821 | 1830 | break; |
| 1822 | - case 'chat': | |
| 1831 | + } | |
| 1823 | - sendSystemMessage(system_message_types.GENERIC, htmlMessage); | |
| 1824 | - break; | |
| 1825 | - case 'popup': | |
| 1826 | - default: | |
| 1827 | - await callGenericPopup(htmlMessage, POPUP_TYPE.TEXT); | |
| 1828 | - break; | |
| 1829 | 1832 | } |
| 1830 | 1833 | |
| 1831 | - return JSON.stringify(chat_metadata.script_injects); | |
| 1834 | + // Now the actual new return type handling | |
| 1835 | + | |
| 1836 | + const buildTextValue = (injects) => { | |
| 1837 | + const injectsStr = Object.entries(injects) | |
| 1838 | + .map(([id, inject]) => { | |
| 1839 | + const position = Object.entries(extension_prompt_types); | |
| 1840 | + const positionName = position.find(([_, value]) => value === inject.position)?.[0] ?? 'unknown'; | |
| 1841 | + return `* **${id}**: <code>${inject.value}</code> (${positionName}, depth: ${inject.depth}, scan: ${inject.scan ?? false}, role: ${inject.role ?? extension_prompt_roles.SYSTEM})`; | |
| 1842 | + }) | |
| 1843 | + .join('\n'); | |
| 1844 | + return `### Script injections:\n${injectsStr}`; | |
| 1845 | + }; | |
| 1846 | + | |
| 1847 | + return await slashCommandReturnHelper.doReturn(returnType ?? 'object', chat_metadata.script_injects, { objectToStringFunc: buildTextValue }); | |
| 1832 | 1848 | } |
| 1833 | 1849 | |
| 1834 | 1850 | /** |
| @@ -7,6 +7,9 @@ import { enumTypes, SlashCommandEnumValue } from './SlashCommandEnumValue.js'; | ||
| 7 | 7 | /** @typedef {'pipe'|'object'|'chat-html'|'chat-text'|'popup-html'|'popup-text'|'toast-html'|'toast-text'|'console'|'none'} SlashCommandReturnType */ |
| 8 | 8 | |
| 9 | 9 | export const slashCommandReturnHelper = { |
| 10 | + // Without this, VSCode formatter fucks up JS docs. Don't ask me why. | |
| 11 | + _: false, | |
| 12 | + | |
| 10 | 13 | /** |
| 11 | 14 | * Gets/creates the enum list of types of return relevant for a slash command |
| 12 | 15 | * |
| @@ -15,18 +18,19 @@ export const slashCommandReturnHelper = { | ||
| 15 | 18 | * @param {boolean} [options.allowObject=false] Allow option to return the value as an object |
| 16 | 19 | * @param {boolean} [options.allowChat=false] Allow option to return the value as a chat message |
| 17 | 20 | * @param {boolean} [options.allowPopup=false] Allow option to return the value as a popup |
| 21 | + * @param {boolean}[options.allowTextVersion=true] Used in combination with chat/popup/toast, some of them do not make sense for text versions, e.g.if you are building a HTML string anyway | |
| 18 | 22 | * @returns {SlashCommandEnumValue[]} The enum list |
| 19 | 23 | */ |
| 20 | 24 | enumList: ({ allowPipe = true, allowObject = false, allowChat = false, allowPopup = false, allowTextVersion = true } = {}) => [ |
| 21 | 25 | allowPipe && new SlashCommandEnumValue('pipe', 'Return to the pipe for the next command', enumTypes.name, '|'), |
| 22 | 26 | allowObject && new SlashCommandEnumValue('object', 'Return as an object to the pipe for the next command', enumTypes.variable, enumIcons.dictionary), |
| 23 | 27 | allowChat && new SlashCommandEnumValue('chat-html', 'Sending a chat message with the return value - Can display HTML', enumTypes.command, enumIcons.message), |
| 24 | 28 | allowChat && allowTextVersion && new SlashCommandEnumValue('chat-text', 'Sending a chat message with the return value - Will only display as text', enumTypes.qr, enumIcons.message), |
| 25 | 29 | allowPopup && new SlashCommandEnumValue('popup-html', 'Showing as a popup with the return value - Can display HTML', enumTypes.command, enumIcons.popup), |
| 26 | 30 | allowPopup && allowTextVersion && new SlashCommandEnumValue('popup-text', 'Showing as a popup with the return value - Will only display as text', enumTypes.qr, enumIcons.popup), |
| 27 | 31 | new SlashCommandEnumValue('toast-html', 'Show the return value as a toast notification - Can display HTML', enumTypes.command, 'ℹ️'), |
| 28 | 32 | allowTextVersion && new SlashCommandEnumValue('toast-text', 'Show the return value as a toast notification - Will only display as text', enumTypes.qr, 'ℹ️'), |
| 29 | 33 | new SlashCommandEnumValue('console', 'Log the return value (object, if it can be one) to the console', enumTypes.enum, '>'), |
| 30 | 34 | new SlashCommandEnumValue('none', 'No return value'), |
| 31 | 35 | ].filter(x => !!x), |
| 32 | 36 | |