Refactor /listinjects, deprecate its "format" arg

62fd450c592653fd256eaf631372cd05c72745ae

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +45 -25Showing whitespace changes
public/scripts/slash-commands.js+35 -19
@@ -1525,11 +1525,20 @@ export function initDefaultSlashCommands() {
15251525 name: 'listinjects',
15261526 callback: listInjectsCallback,
15271527 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.',
15281528 returns: 'Optionalls the JSON object of script injections',
15291529 namedArgumentList: [
15301530 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({
15311540 name: 'format',
15321541 description: 'DEPRECATED - use "return" instead - output format',
15331542 typeList: [ARGUMENT_TYPE.STRING],
15341543 isRequired: true,
15351544 forceEnum: true,
@@ -1798,37 +1807,44 @@ function injectCallback(args, value) {
17981807}
17991808
18001809async function listInjectsCallback(args) {
1810+ /** @type {import('./slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */
1811+ let returnType = args.return;
1812+
1813+ // Old legacy return type handling
1814+ if (args.format) {
1815+ toastr.warning(`Legacy argument 'format' with value '${args.format}' is deprecated. Please use 'return' instead. Routing to the correct return type...`, 'Deprecation warning');
18011816 const type = String(args?.format).toLowerCase().trim();
18021817 if (!chat_metadata.script_injects || !Object.keys(chat_metadata.script_injects).length) {
18031818 type !== 'none' && toastr.info('No script injections for the current chat');
1804- return JSON.stringify({});
18051819 }
1806-
1807- const injects = Object.entries(chat_metadata.script_injects)
1808- .map(([id, inject]) => {
1809- const position = Object.entries(extension_prompt_types);
1810- const positionName = position.find(([_, value]) => value === inject.position)?.[0] ?? 'unknown';
1811- return `* **${id}**: <code>${inject.value}</code> (${positionName}, depth: ${inject.depth}, scan: ${inject.scan ?? false}, role: ${inject.role ?? extension_prompt_roles.SYSTEM})`;
1812- })
1813- .join('\n');
1814-
1815- const converter = new showdown.Converter();
1816- const messageText = `### Script injections:\n${injects}`;
1817- const htmlMessage = DOMPurify.sanitize(converter.makeHtml(messageText));
1818-
18191820 switch (type) {
18201821 case 'none':
1822+ returnType = 'none';
18211823 break;
18221824 case 'chat':
1823- sendSystemMessage(system_message_types.GENERIC, htmlMessage);
1825+ returnType = 'chat-html';
18241826 break;
18251827 case 'popup':
18261828 default:
1827- await callGenericPopup(htmlMessage, POPUP_TYPE.TEXT);
1829+ returnType = 'popup-html';
18281830 break;
18291831 }
1832+ }
1833+
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+ };
18301846
1831- return JSON.stringify(chat_metadata.script_injects);
1847+ return await slashCommandReturnHelper.doReturn(returnType ?? 'object', chat_metadata.script_injects, { objectToStringFunc: buildTextValue });
18321848}
18331849
18341850/**
public/scripts/slash-commands/SlashCommandReturnHelper.js+10 -6
@@ -7,6 +7,9 @@ import { enumTypes, SlashCommandEnumValue } from './SlashCommandEnumValue.js';
77/** @typedef {'pipe'|'object'|'chat-html'|'chat-text'|'popup-html'|'popup-text'|'toast-html'|'toast-text'|'console'|'none'} SlashCommandReturnType */
88
99export const slashCommandReturnHelper = {
10+ // Without this, VSCode formatter fucks up JS docs. Don't ask me why.
11+ _: false,
12+
1013 /**
1114 * Gets/creates the enum list of types of return relevant for a slash command
1215 *
@@ -15,18 +18,19 @@ export const slashCommandReturnHelper = {
1518 * @param {boolean} [options.allowObject=false] Allow option to return the value as an object
1619 * @param {boolean} [options.allowChat=false] Allow option to return the value as a chat message
1720 * @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
1822 * @returns {SlashCommandEnumValue[]} The enum list
1923 */
2024 enumList: ({ allowPipe = true, allowObject = false, allowChat = false, allowPopup = false, allowTextVersion = true } = {}) => [
2125 allowPipe && new SlashCommandEnumValue('pipe', 'Return to the pipe for the next command', enumTypes.name, '|'),
2226 allowObject && new SlashCommandEnumValue('object', 'Return as an object to the pipe for the next command', enumTypes.variable, enumIcons.dictionary),
2327 allowChat && new SlashCommandEnumValue('chat-html', 'Sending a chat message with the return value - Can display HTML', enumTypes.command, enumIcons.message),
2428 allowChat && allowTextVersion && new SlashCommandEnumValue('chat-text', 'Sending a chat message with the return value - Will only display as text', enumTypes.qr, enumIcons.message),
2529 allowPopup && new SlashCommandEnumValue('popup-html', 'Showing as a popup with the return value - Can display HTML', enumTypes.command, enumIcons.popup),
2630 allowPopup && allowTextVersion && new SlashCommandEnumValue('popup-text', 'Showing as a popup with the return value - Will only display as text', enumTypes.qr, enumIcons.popup),
2731 new SlashCommandEnumValue('toast-html', 'Show the return value as a toast notification - Can display HTML', enumTypes.command, 'ℹ️'),
2832 allowTextVersion && new SlashCommandEnumValue('toast-text', 'Show the return value as a toast notification - Will only display as text', enumTypes.qr, 'ℹ️'),
2933 new SlashCommandEnumValue('console', 'Log the return value (object, if it can be one) to the console', enumTypes.enum, '>'),
3034 new SlashCommandEnumValue('none', 'No return value'),
3135 ].filter(x => !!x),
3236