Add format and scope arguments to /listvar

b61344185c4d9a29793938daea7fbc54cb0b0d0d

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +80 -27Ignore whitespace
public/scripts/variables.js+80 -27
@@ -1,5 +1,6 @@
1import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessage, system_message_types } from '../script.js';1import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessage, system_message_types } from '../script.js';
2import { extension_settings, saveMetadataDebounced } from './extensions.js';2import { extension_settings, saveMetadataDebounced } from './extensions.js';
3import { callGenericPopup, POPUP_TYPE } from './popup.js';
3import { executeSlashCommandsWithOptions } from './slash-commands.js';4import { executeSlashCommandsWithOptions } from './slash-commands.js';
4import { SlashCommand } from './slash-commands/SlashCommand.js';5import { SlashCommand } from './slash-commands/SlashCommand.js';
5import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js';6import { 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}
305306
306function listVariablesCallback() {307async 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 }
310313
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 ];
313324
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();
317328
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));
321335
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}
325350
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',
@@ -1276,16 +1329,16 @@ export function registerVariableCommands() {
1276 }),1329 }),
1277 new SlashCommandNamedArgument(1330 new SlashCommandNamedArgument(
1278 'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [1331 'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [
1279 new SlashCommandEnumValue('gt', 'a > b'),1332 new SlashCommandEnumValue('gt', 'a > b'),
1280 new SlashCommandEnumValue('gte', 'a >= b'),1333 new SlashCommandEnumValue('gte', 'a >= b'),
1281 new SlashCommandEnumValue('lt', 'a < b'),1334 new SlashCommandEnumValue('lt', 'a < b'),
1282 new SlashCommandEnumValue('lte', 'a <= b'),1335 new SlashCommandEnumValue('lte', 'a <= b'),
1283 new SlashCommandEnumValue('eq', 'a == b'),1336 new SlashCommandEnumValue('eq', 'a == b'),
1284 new SlashCommandEnumValue('neq', 'a !== b'),1337 new SlashCommandEnumValue('neq', 'a !== b'),
1285 new SlashCommandEnumValue('not', '!a'),1338 new SlashCommandEnumValue('not', '!a'),
1286 new SlashCommandEnumValue('in', 'a includes b'),1339 new SlashCommandEnumValue('in', 'a includes b'),
1287 new SlashCommandEnumValue('nin', 'a not includes b'),1340 new SlashCommandEnumValue('nin', 'a not includes b'),
1288 ],1341 ],
1289 ),1342 ),
1290 new SlashCommandNamedArgument(1343 new SlashCommandNamedArgument(
1291 'else', 'command to execute if not true', [ARGUMENT_TYPE.CLOSURE, ARGUMENT_TYPE.SUBCOMMAND], false,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 new SlashCommandNamedArgument(1408 new SlashCommandNamedArgument(
1356 'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [1409 'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [
1357 new SlashCommandEnumValue('gt', 'a > b'),1410 new SlashCommandEnumValue('gt', 'a > b'),
1358 new SlashCommandEnumValue('gte', 'a >= b'),1411 new SlashCommandEnumValue('gte', 'a >= b'),
1359 new SlashCommandEnumValue('lt', 'a < b'),1412 new SlashCommandEnumValue('lt', 'a < b'),
1360 new SlashCommandEnumValue('lte', 'a <= b'),1413 new SlashCommandEnumValue('lte', 'a <= b'),
1361 new SlashCommandEnumValue('eq', 'a == b'),1414 new SlashCommandEnumValue('eq', 'a == b'),
1362 new SlashCommandEnumValue('neq', 'a !== b'),1415 new SlashCommandEnumValue('neq', 'a !== b'),
1363 new SlashCommandEnumValue('not', '!a'),1416 new SlashCommandEnumValue('not', '!a'),
1364 new SlashCommandEnumValue('in', 'a includes b'),1417 new SlashCommandEnumValue('in', 'a includes b'),
1365 new SlashCommandEnumValue('nin', 'a not includes b'),1418 new SlashCommandEnumValue('nin', 'a not includes b'),
1366 ],1419 ],
1367 ),1420 ),
1368 new SlashCommandNamedArgument(1421 new SlashCommandNamedArgument(
1369 'guard', 'disable loop iteration limit', [ARGUMENT_TYPE.STRING], false, false, null, commonEnumProviders.boolean('onOff')(),1422 'guard', 'disable loop iteration limit', [ARGUMENT_TYPE.STRING], false, false, null, commonEnumProviders.boolean('onOff')(),