Refactor /listvar, deprecate its "format" arg - Update /listvar - Fix toasts not doing correct HMTL here

697b3b2034c20ea15e48002929b696f85bfb3a64

Wolfsblvt <wolfsblvt@gmail.com>

3 files changed, +50 -28Showing whitespace changes
public/scripts/slash-commands.js+4 -5
@@ -1524,21 +1524,21 @@ export function initDefaultSlashCommands() {
15241524 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
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>formatreturn</code> argument to change the outputreturn formattype.',
15281528 returns: 'Optionalls the JSON object of script injections',
15291529 namedArgumentList: [
15301530 SlashCommandNamedArgument.fromProps({
15311531 name: 'return',
15321532 description: 'The way how you want the return value to be provided',
15331533 typeList: [ARGUMENT_TYPE.STRING],
15341534 defaultValue: 'objectpopup-html',
15351535 enumList: slashCommandReturnHelper.enumList({ allowPipe: false, allowObject: true, allowChat: true, allowPopup: true, allowTextVersion: false }),
15361536 forceEnum: true,
15371537 }),
15381538 // TODO remove some day
15391539 SlashCommandNamedArgument.fromProps({
15401540 name: 'format',
15411541 description: '!!! DEPRECATED - use "return" instead -!!! output format)',
15421542 typeList: [ARGUMENT_TYPE.STRING],
15431543 isRequired: true,
15441544 forceEnum: true,
@@ -1832,7 +1832,6 @@ async function listInjectsCallback(args) {
18321832 }
18331833
18341834 // Now the actual new return type handling
1835-
18361835 const buildTextValue = (injects) => {
18371836 const injectsStr = Object.entries(injects)
18381837 .map(([id, inject]) => {
@@ -1844,7 +1843,7 @@ async function listInjectsCallback(args) {
18441843 return `### Script injections:\n${injectsStr}`;
18451844 };
18461845
18471846 return await slashCommandReturnHelper.doReturn(returnType ?? 'objectpopup-html', chat_metadata.script_injects, { objectToStringFunc: buildTextValue });
18481847}
18491848
18501849/**
public/scripts/slash-commands/SlashCommandReturnHelper.js+1 -1
@@ -58,7 +58,7 @@ export const slashCommandReturnHelper = {
5858
5959 if (type.startsWith('popup')) await callGenericPopup(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), POPUP_TYPE.TEXT);
6060 if (type.startsWith('chat')) sendSystemMessage(system_message_types.GENERIC, shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue));
6161 if (type.startsWith('toast')) toastr.info(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), null, { escapeHtml: !shouldHtml }); // Toastr handles HTML conversion internally already
6262
6363 return '';
6464 }
public/scripts/variables.js+45 -22
@@ -11,6 +11,7 @@ import { SlashCommandClosureResult } from './slash-commands/SlashCommandClosureR
1111import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
1212import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
1313import { PARSER_FLAG, SlashCommandParser } from './slash-commands/SlashCommandParser.js';
14+import { slashCommandReturnHelper } from './slash-commands/SlashCommandReturnHelper.js';
1415import { SlashCommandScope } from './slash-commands/SlashCommandScope.js';
1516import { isFalseBoolean, convertValueType, isTrueBoolean } from './utils.js';
1617
@@ -305,7 +306,31 @@ export function replaceVariableMacros(input) {
305306}
306307
307308async function listVariablesCallback(args) {
308- const type = String(args?.format || '').toLowerCase().trim() || 'popup';
309+ /** @type {import('./slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */
310+ let returnType = args.return;
311+
312+ // Old legacy return type handling
313+ if (args.format) {
314+ toastr.warning(`Legacy argument 'format' with value '${args.format}' is deprecated. Please use 'return' instead. Routing to the correct return type...`, 'Deprecation warning');
315+ const type = String(args?.format).toLowerCase().trim();
316+ if (!chat_metadata.script_injects || !Object.keys(chat_metadata.script_injects).length) {
317+ type !== 'none' && toastr.info('No script injections for the current chat');
318+ }
319+ switch (type) {
320+ case 'none':
321+ returnType = 'none';
322+ break;
323+ case 'chat':
324+ returnType = 'chat-html';
325+ break;
326+ case 'popup':
327+ default:
328+ returnType = 'popup-html';
329+ break;
330+ }
331+ }
332+
333+ // Now the actual new return type handling
309334 const scope = String(args?.scope || '').toLowerCase().trim() || 'all';
310335 if (!chat_metadata.variables) {
311336 chat_metadata.variables = {};
@@ -317,35 +342,24 @@ async function listVariablesCallback(args) {
317342 const localVariables = includeLocalVariables ? Object.entries(chat_metadata.variables).map(([name, value]) => `${name}: ${value}`) : [];
318343 const globalVariables = includeGlobalVariables ? Object.entries(extension_settings.variables.global).map(([name, value]) => `${name}: ${value}`) : [];
319344
320345 const jsonVariablesbuildTextValue = [(_) => {
321- ...Object.entries(chat_metadata.variables).map(x => ({ key: x[0], value: x[1], scope: 'local' })),
322- ...Object.entries(extension_settings.variables.global).map(x => ({ key: x[0], value: x[1], scope: 'global' })),
323- ];
324-
325346 const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables';
326347 const globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables';
327348 const chatName = getCurrentChatId();
328349
329- const converter = new showdown.Converter();
330350 const message = [
331351 includeLocalVariables ? `### Local variables (${chatName}):\n${localVariablesString}` : '',
332352 includeGlobalVariables ? `### Global variables:\n${globalVariablesString}` : '',
333353 ].filter(x => x).join('\n\n');
334- const htmlMessage = DOMPurify.sanitize(converter.makeHtml(message));
354+ return message;
355+ };
335356
336- switch (type) {
357+ const jsonVariables = [
337- case 'none':
358+ ...Object.entries(chat_metadata.variables).map(x => ({ key: x[0], value: x[1], scope: 'local' })),
338- break;
359+ ...Object.entries(extension_settings.variables.global).map(x => ({ key: x[0], value: x[1], scope: 'global' })),
339- case 'chat':
360+ ];
340- sendSystemMessage(system_message_types.GENERIC, htmlMessage);
341- break;
342- case 'popup':
343- default:
344- await callGenericPopup(htmlMessage, POPUP_TYPE.TEXT);
345- break;
346- }
347361
348- return JSON.stringify(jsonVariables);
362+ return await slashCommandReturnHelper.doReturn(returnType ?? 'popup-html', jsonVariables, { objectToStringFunc: buildTextValue });
349363}
350364
351365/**
@@ -910,7 +924,7 @@ export function registerVariableCommands() {
910924 name: 'listvar',
911925 callback: listVariablesCallback,
912926 aliases: ['listchatvar'],
913927 helpString: 'List registered chat variables. Displays variables in a popup by default. Use the <code>formatreturn</code> argument to change the outputreturn formattype.',
914928 returns: 'JSON list of local variables',
915929 namedArgumentList: [
916930 SlashCommandNamedArgument.fromProps({
@@ -927,8 +941,17 @@ export function registerVariableCommands() {
927941 ],
928942 }),
929943 SlashCommandNamedArgument.fromProps({
944+ name: 'return',
945+ description: 'The way how you want the return value to be provided',
946+ typeList: [ARGUMENT_TYPE.STRING],
947+ defaultValue: 'popup-html',
948+ enumList: slashCommandReturnHelper.enumList({ allowPipe: false, allowObject: true, allowChat: true, allowPopup: true, allowTextVersion: false }),
949+ forceEnum: true,
950+ }),
951+ // TODO remove some day
952+ SlashCommandNamedArgument.fromProps({
930953 name: 'format',
931- description: 'output format',
954+ description: '!!! DEPRECATED - use "return" instead !!! output format)',
932955 typeList: [ARGUMENT_TYPE.STRING],
933956 isRequired: true,
934957 forceEnum: true,