Add format and scope arguments to /listvar
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessage, system_message_types } from '../script.js'; |
| 2 | 2 | import { extension_settings, saveMetadataDebounced } from './extensions.js'; |
| 3 | +import { callGenericPopup, POPUP_TYPE } from './popup.js'; | |
| 3 | 4 | import { executeSlashCommandsWithOptions } from './slash-commands.js'; |
| 4 | 5 | import { SlashCommand } from './slash-commands/SlashCommand.js'; |
| 5 | 6 | import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js'; |
| @@ -303,24 +304,48 @@ export function replaceVariableMacros(input) { | ||
| 303 | 304 | return lines.join('\n'); |
| 304 | 305 | } |
| 305 | 306 | |
| 306 | 307 | async function listVariablesCallback(args) { |
| 308 | + const type = String(args?.format || '').toLowerCase().trim() || 'popup'; | |
| 309 | + const scope = String(args?.scope || '').toLowerCase().trim() || 'all'; | |
| 307 | 310 | if (!chat_metadata.variables) { |
| 308 | 311 | chat_metadata.variables = {}; |
| 309 | 312 | } |
| 310 | 313 | |
| 311 | - const localVariables = Object.entries(chat_metadata.variables).map(([name, value]) => `${name}: ${value}`); | |
| 314 | + const includeLocalVariables = scope === 'all' || scope === 'local'; | |
| 312 | - const globalVariables = Object.entries(extension_settings.variables.global).map(([name, value]) => `${name}: ${value}`); | |
| 315 | + const includeGlobalVariables = scope === 'all' || scope === 'global'; | |
| 316 | + | |
| 317 | + const localVariables = includeLocalVariables ? Object.entries(chat_metadata.variables).map(([name, value]) => `${name}: ${value}`) : []; | |
| 318 | + const globalVariables = includeGlobalVariables ? Object.entries(extension_settings.variables.global).map(([name, value]) => `${name}: ${value}`) : []; | |
| 319 | + | |
| 320 | + const jsonVariables = [ | |
| 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 | + ]; | |
| 313 | 324 | |
| 314 | 325 | const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables'; |
| 315 | 326 | const globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables'; |
| 316 | 327 | const chatName = getCurrentChatId(); |
| 317 | 328 | |
| 318 | 329 | const converter = new showdown.Converter(); |
| 319 | - const message = `### Local variables (${chatName}):\n${localVariablesString}\n\n### Global variables:\n${globalVariablesString}`; | |
| 330 | + const message = [ | |
| 331 | + includeLocalVariables ? `### Local variables (${chatName}):\n${localVariablesString}` : '', | |
| 332 | + includeGlobalVariables ? `### Global variables:\n${globalVariablesString}` : '', | |
| 333 | + ].filter(x => x).join('\n\n'); | |
| 320 | 334 | const htmlMessage = DOMPurify.sanitize(converter.makeHtml(message)); |
| 321 | 335 | |
| 322 | - sendSystemMessage(system_message_types.GENERIC, htmlMessage); | |
| 336 | + switch (type) { | |
| 323 | - return ''; | |
| 337 | + case 'none': | |
| 338 | + break; | |
| 339 | + case 'chat': | |
| 340 | + sendSystemMessage(system_message_types.GENERIC, htmlMessage); | |
| 341 | + break; | |
| 342 | + case 'popup': | |
| 343 | + default: | |
| 344 | + await callGenericPopup(htmlMessage, POPUP_TYPE.TEXT); | |
| 345 | + break; | |
| 346 | + } | |
| 347 | + | |
| 348 | + return JSON.stringify(jsonVariables); | |
| 324 | 349 | } |
| 325 | 350 | |
| 326 | 351 | /** |
| @@ -887,7 +912,35 @@ export function registerVariableCommands() { | ||
| 887 | 912 | name: 'listvar', |
| 888 | 913 | callback: listVariablesCallback, |
| 889 | 914 | aliases: ['listchatvar'], |
| 890 | - helpString: 'List registered chat variables.', | |
| 915 | + helpString: 'List registered chat variables. Displays variables in a popup by default. Use the <code>format</code> argument to change the output format.', | |
| 916 | + returns: 'JSON list of local variables', | |
| 917 | + namedArgumentList: [ | |
| 918 | + SlashCommandNamedArgument.fromProps({ | |
| 919 | + name: 'scope', | |
| 920 | + description: 'filter variables by scope', | |
| 921 | + typeList: [ARGUMENT_TYPE.STRING], | |
| 922 | + defaultValue: 'all', | |
| 923 | + isRequired: false, | |
| 924 | + forceEnum: true, | |
| 925 | + enumList: [ | |
| 926 | + new SlashCommandEnumValue('all', 'All variables', enumTypes.enum, enumIcons.variable), | |
| 927 | + new SlashCommandEnumValue('local', 'Local variables', enumTypes.enum, enumIcons.localVariable), | |
| 928 | + new SlashCommandEnumValue('global', 'Global variables', enumTypes.enum, enumIcons.globalVariable), | |
| 929 | + ], | |
| 930 | + }), | |
| 931 | + SlashCommandNamedArgument.fromProps({ | |
| 932 | + name: 'format', | |
| 933 | + description: 'output format', | |
| 934 | + typeList: [ARGUMENT_TYPE.STRING], | |
| 935 | + isRequired: true, | |
| 936 | + forceEnum: true, | |
| 937 | + enumList: [ | |
| 938 | + new SlashCommandEnumValue('popup', 'Show variables in a popup.', enumTypes.enum, enumIcons.default), | |
| 939 | + new SlashCommandEnumValue('chat', 'Post a system message to the chat.', enumTypes.enum, enumIcons.message), | |
| 940 | + new SlashCommandEnumValue('none', 'Just return the variables as a JSON list.', enumTypes.enum, enumIcons.array), | |
| 941 | + ], | |
| 942 | + }), | |
| 943 | + ], | |
| 891 | 944 | })); |
| 892 | 945 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 893 | 946 | name: 'setvar', |
| @@ -1276,16 +1329,16 @@ export function registerVariableCommands() { | ||
| 1276 | 1329 | }), |
| 1277 | 1330 | new SlashCommandNamedArgument( |
| 1278 | 1331 | 'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [ |
| 1279 | 1332 | new SlashCommandEnumValue('gt', 'a > b'), |
| 1280 | 1333 | new SlashCommandEnumValue('gte', 'a >= b'), |
| 1281 | 1334 | new SlashCommandEnumValue('lt', 'a < b'), |
| 1282 | 1335 | new SlashCommandEnumValue('lte', 'a <= b'), |
| 1283 | 1336 | new SlashCommandEnumValue('eq', 'a == b'), |
| 1284 | 1337 | new SlashCommandEnumValue('neq', 'a !== b'), |
| 1285 | 1338 | new SlashCommandEnumValue('not', '!a'), |
| 1286 | 1339 | new SlashCommandEnumValue('in', 'a includes b'), |
| 1287 | 1340 | new SlashCommandEnumValue('nin', 'a not includes b'), |
| 1288 | 1341 | ], |
| 1289 | 1342 | ), |
| 1290 | 1343 | new SlashCommandNamedArgument( |
| 1291 | 1344 | 'else', 'command to execute if not true', [ARGUMENT_TYPE.CLOSURE, ARGUMENT_TYPE.SUBCOMMAND], false, |
| @@ -1354,16 +1407,16 @@ export function registerVariableCommands() { | ||
| 1354 | 1407 | }), |
| 1355 | 1408 | new SlashCommandNamedArgument( |
| 1356 | 1409 | 'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [ |
| 1357 | 1410 | new SlashCommandEnumValue('gt', 'a > b'), |
| 1358 | 1411 | new SlashCommandEnumValue('gte', 'a >= b'), |
| 1359 | 1412 | new SlashCommandEnumValue('lt', 'a < b'), |
| 1360 | 1413 | new SlashCommandEnumValue('lte', 'a <= b'), |
| 1361 | 1414 | new SlashCommandEnumValue('eq', 'a == b'), |
| 1362 | 1415 | new SlashCommandEnumValue('neq', 'a !== b'), |
| 1363 | 1416 | new SlashCommandEnumValue('not', '!a'), |
| 1364 | 1417 | new SlashCommandEnumValue('in', 'a includes b'), |
| 1365 | 1418 | new SlashCommandEnumValue('nin', 'a not includes b'), |
| 1366 | 1419 | ], |
| 1367 | 1420 | ), |
| 1368 | 1421 | new SlashCommandNamedArgument( |
| 1369 | 1422 | 'guard', 'disable loop iteration limit', [ARGUMENT_TYPE.STRING], false, false, null, commonEnumProviders.boolean('onOff')(), |