Add format and scope arguments to /listvar
| @@ -1,5 +1,6 @@ | |||
| 1 | import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessage, system_message_types } from '../script.js'; | 1 | import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessage, system_message_types } from '../script.js'; |
| 2 | import { extension_settings, saveMetadataDebounced } from './extensions.js'; | 2 | import { extension_settings, saveMetadataDebounced } from './extensions.js'; |
| 3 | import { callGenericPopup, POPUP_TYPE } from './popup.js'; | ||
| 3 | import { executeSlashCommandsWithOptions } from './slash-commands.js'; | 4 | import { executeSlashCommandsWithOptions } from './slash-commands.js'; |
| 4 | import { SlashCommand } from './slash-commands/SlashCommand.js'; | 5 | import { SlashCommand } from './slash-commands/SlashCommand.js'; |
| 5 | import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js'; | 6 | import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js'; |
| @@ -303,24 +304,48 @@ export function replaceVariableMacros(input) { | |||
| 303 | return lines.join('\n'); | 304 | return lines.join('\n'); |
| 304 | } | 305 | } |
| 305 | 306 | ||
| 306 | function listVariablesCallback() { | 307 | async function listVariablesCallback(args) { |
| 308 | const type = String(args?.format || '').toLowerCase().trim() || 'popup'; | ||
| 309 | const scope = String(args?.scope || '').toLowerCase().trim() || 'all'; | ||
| 307 | if (!chat_metadata.variables) { | 310 | if (!chat_metadata.variables) { |
| 308 | chat_metadata.variables = {}; | 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 | const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables'; | 325 | const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables'; |
| 315 | const globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables'; | 326 | const globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables'; |
| 316 | const chatName = getCurrentChatId(); | 327 | const chatName = getCurrentChatId(); |
| 317 | 328 | ||
| 318 | const converter = new showdown.Converter(); | 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 | const htmlMessage = DOMPurify.sanitize(converter.makeHtml(message)); | 334 | const htmlMessage = DOMPurify.sanitize(converter.makeHtml(message)); |
| 321 | 335 | ||
| 336 | switch (type) { | ||
| 337 | case 'none': | ||
| 338 | break; | ||
| 339 | case 'chat': | ||
| 322 | sendSystemMessage(system_message_types.GENERIC, htmlMessage); | 340 | sendSystemMessage(system_message_types.GENERIC, htmlMessage); |
| 323 | return ''; | 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 | name: 'listvar', | 912 | name: 'listvar', |
| 888 | callback: listVariablesCallback, | 913 | callback: listVariablesCallback, |
| 889 | aliases: ['listchatvar'], | 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 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | 945 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 893 | name: 'setvar', | 946 | name: 'setvar', |