Rework slash command enum values pt.2

- Fix jsconfig module resolution for imports in frontend scripts
- Add file with common slash command enum values
This commit is contained in:
Wolfsblvt 2024-06-17 03:30:52 +02:00
parent 34e8cf476a
commit 6f7ef25369
13 changed files with 661 additions and 351 deletions

View File

@ -3,6 +3,7 @@
"checkJs": true,
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"allowUmdGlobalAccess": true,
"allowSyntheticDefaultImports": true
},

View File

@ -38,6 +38,7 @@ const chara_note_position = {
function setNoteTextCommand(_, text) {
$('#extension_floating_prompt').val(text).trigger('input');
toastr.success('Author\'s Note text updated');
return '';
}
function setNoteDepthCommand(_, text) {
@ -50,6 +51,7 @@ function setNoteDepthCommand(_, text) {
$('#extension_floating_depth').val(Math.abs(value)).trigger('input');
toastr.success('Author\'s Note depth updated');
return '';
}
function setNoteIntervalCommand(_, text) {
@ -62,6 +64,7 @@ function setNoteIntervalCommand(_, text) {
$('#extension_floating_interval').val(Math.abs(value)).trigger('input');
toastr.success('Author\'s Note frequency updated');
return '';
}
function setNotePositionCommand(_, text) {
@ -79,6 +82,7 @@ function setNotePositionCommand(_, text) {
$(`input[name="extension_floating_position"][value="${position}"]`).prop('checked', true).trigger('input');
toastr.info('Author\'s Note position updated');
return '';
}
function updateSettings() {

View File

@ -95,7 +95,7 @@ function onLockBackgroundClick(e) {
if (!chatName) {
toastr.warning('Select a chat to lock the background for it');
return;
return '';
}
const relativeBgImage = getUrlParameter(this);
@ -103,6 +103,7 @@ function onLockBackgroundClick(e) {
saveBackgroundMetadata(relativeBgImage);
setCustomBackground();
highlightLockedBackground();
return '';
}
function onUnlockBackgroundClick(e) {
@ -110,6 +111,7 @@ function onUnlockBackgroundClick(e) {
removeBackgroundMetadata();
unsetCustomBackground();
highlightLockedBackground();
return '';
}
function hasCustomBackground() {
@ -319,7 +321,7 @@ async function autoBackgroundCommand() {
const options = bgTitles.map(x => ({ element: x, text: x.innerText.trim() })).filter(x => x.text.length > 0);
if (options.length == 0) {
toastr.warning('No backgrounds to choose from. Please upload some images to the "backgrounds" folder.');
return;
return '';
}
const list = options.map(option => `- ${option.text}`).join('\n');
@ -330,11 +332,12 @@ async function autoBackgroundCommand() {
if (bestMatch.length == 0) {
toastr.warning('No match found. Please try again.');
return;
return '';
}
console.debug('Automatically choosing background:', bestMatch);
bestMatch[0].item.element.click();
return '';
}
export async function getBackgrounds() {

View File

@ -70,6 +70,7 @@ import { saveLogprobsForActiveMessage } from './logprobs.js';
import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
import { SlashCommand } from './slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashCommandArgument.js';
import { SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
export {
openai_messages_count,
@ -4616,9 +4617,12 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
returns: 'current proxy',
namedArgumentList: [],
unnamedArgumentList: [
new SlashCommandArgument(
'name', [ARGUMENT_TYPE.STRING], true,
),
SlashCommandArgument.fromProps({
description: 'name',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
enumProvider: () => proxies.map(preset => new SlashCommandEnumValue(preset.name)),
}),
],
helpString: 'Sets a proxy preset by name.',
}));

View File

@ -2883,7 +2883,7 @@ function setAvgBG() {
return `rgba(${rNew.toFixed(0)}, ${gNew.toFixed(0)}, ${bNew.toFixed(0)}, 1)`;
}
return '';
}
async function setThemeCallback(_, text) {
@ -3887,9 +3887,11 @@ $(document).ready(() => {
name: 'random',
callback: doRandomChat,
unnamedArgumentList: [
new SlashCommandArgument(
'optional tag name', [ARGUMENT_TYPE.STRING], false,
),
SlashCommandArgument.fromProps({
description: 'optional tag name',
typeList: [ARGUMENT_TYPE.STRING],
enumProvider: () => tags.filter(tag => Object.values(tag_map).some(x => x.includes(tag.id))).map(tag => new SlashCommandEnumValue(tag.name)),
}),
],
helpString: 'Start a new chat with a random character. If an argument is provided, only considers characters that have the specified tag.',
}));

View File

@ -22,6 +22,7 @@ import { kai_settings } from './kai-settings.js';
import { context_presets, getContextSettings, power_user } from './power-user.js';
import { SlashCommand } from './slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument } from './slash-commands/SlashCommandArgument.js';
import { SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
import {
textgenerationwebui_preset_names,
@ -481,9 +482,11 @@ export async function initPresetManager() {
returns: 'current preset',
namedArgumentList: [],
unnamedArgumentList: [
new SlashCommandArgument(
'name', [ARGUMENT_TYPE.STRING], false,
),
SlashCommandArgument.fromProps({
description: 'name',
typeList: [ARGUMENT_TYPE.STRING],
enumProvider: () => getPresetManager().getAllPresets().map(preset => new SlashCommandEnumValue(preset)),
}),
],
helpString: `
<div>

View File

@ -42,7 +42,7 @@ import { PARSER_FLAG, SlashCommandParser } from './slash-commands/SlashCommandPa
import { SlashCommandParserError } from './slash-commands/SlashCommandParserError.js';
import { getMessageTimeStamp } from './RossAscends-mods.js';
import { hideChatMessageRange } from './chats.js';
import { getContext, saveMetadataDebounced } from './extensions.js';
import { extension_settings, getContext, saveMetadataDebounced } from './extensions.js';
import { getRegexedString, regex_placement } from './extensions/regex/engine.js';
import { findGroupMemberId, groups, is_group_generating, openGroupById, resetSelectedGroup, saveGroupChat, selected_group } from './group-chats.js';
import { chat_completion_sources, oai_settings } from './openai.js';

View File

@ -2,8 +2,6 @@ import { SlashCommandClosure } from './SlashCommandClosure.js';
import { SlashCommandEnumValue } from './SlashCommandEnumValue.js';
import { SlashCommandExecutor } from './SlashCommandExecutor.js';
/**@readonly*/
/**@enum {string}*/
export const ARGUMENT_TYPE = {
@ -18,20 +16,18 @@ export const ARGUMENT_TYPE = {
'DICTIONARY': 'dictionary',
};
export class SlashCommandArgument {
/**
* Creates an unnamed argument from a properties object.
* @param {Object} props
* @param {string} props.description description of the argument
* @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} props.typeList default: ARGUMENT_TYPE.STRING - list of accepted types (from ARGUMENT_TYPE)
* @param {boolean} [props.isRequired] default: false - whether the argument is required (false = optional argument)
* @param {boolean} [props.acceptsMultiple] default: false - whether argument accepts multiple values
* @param {string|SlashCommandClosure} [props.defaultValue] default value if no value is provided
* @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} [props.enumList] list of accepted values
* @param {(executor:SlashCommandExecutor)=>SlashCommandEnumValue[]} [props.enumProvider] function that returns auto complete options
* @param {boolean} [props.forceEnum] default: true - whether the input must match one of the enum values
* @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} [props.typeList=[ARGUMENT_TYPE.STRING]] default: ARGUMENT_TYPE.STRING - list of accepted types (from ARGUMENT_TYPE)
* @param {boolean} [props.isRequired=false] default: false - whether the argument is required (false = optional argument)
* @param {boolean} [props.acceptsMultiple=false] default: false - whether argument accepts multiple values
* @param {string|SlashCommandClosure} [props.defaultValue=null] default value if no value is provided
* @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} [props.enumList=[]] list of accepted values
* @param {(executor:SlashCommandExecutor)=>SlashCommandEnumValue[]} [props.enumProvider=null] function that returns auto complete options
* @param {boolean} [props.forceEnum=true] default: true - whether the input must match one of the enum values
*/
static fromProps(props) {
return new SlashCommandArgument(
@ -46,9 +42,6 @@ export class SlashCommandArgument {
);
}
/**@type {string}*/ description;
/**@type {ARGUMENT_TYPE[]}*/ typeList = [];
/**@type {boolean}*/ isRequired = false;
@ -58,7 +51,6 @@ export class SlashCommandArgument {
/**@type {(executor:SlashCommandExecutor)=>SlashCommandEnumValue[]}*/ enumProvider = null;
/**@type {boolean}*/ forceEnum = true;
/**
* @param {string} description
* @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} types
@ -84,22 +76,20 @@ export class SlashCommandArgument {
}
}
export class SlashCommandNamedArgument extends SlashCommandArgument {
/**
* Creates an unnamed argument from a properties object.
* @param {Object} props
* @param {string} props.name the argument's name
* @param {string[]} [props.aliasList] list of aliases
* @param {string} props.description description of the argument
* @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} props.typeList default: ARGUMENT_TYPE.STRING - list of accepted types (from ARGUMENT_TYPE)
* @param {boolean} [props.isRequired] default: false - whether the argument is required (false = optional argument)
* @param {boolean} [props.acceptsMultiple] default: false - whether argument accepts multiple values
* @param {string|SlashCommandClosure} [props.defaultValue] default value if no value is provided
* @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} [props.enumList] list of accepted values
* @param {(executor:SlashCommandExecutor)=>SlashCommandEnumValue[]} [props.enumProvider] function that returns auto complete options
* @param {boolean} [props.forceEnum] default: true - whether the input must match one of the enum values
* @param {string[]} [props.aliasList=[]] list of aliases
* @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} [props.typeList=[ARGUMENT_TYPE.STRING]] default: ARGUMENT_TYPE.STRING - list of accepted types (from ARGUMENT_TYPE)
* @param {boolean} [props.isRequired=false] default: false - whether the argument is required (false = optional argument)
* @param {boolean} [props.acceptsMultiple=false] default: false - whether argument accepts multiple values
* @param {string|SlashCommandClosure} [props.defaultValue=null] default value if no value is provided
* @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} [props.enumList=[]] list of accepted values
* @param {(executor:SlashCommandExecutor)=>SlashCommandEnumValue[]} [props.enumProvider=null] function that returns auto complete options
* @param {boolean} [props.forceEnum=true] default: true - whether the input must match one of the enum values
*/
static fromProps(props) {
return new SlashCommandNamedArgument(
@ -116,21 +106,20 @@ export class SlashCommandNamedArgument extends SlashCommandArgument {
);
}
/**@type {string}*/ name;
/**@type {string[]}*/ aliasList = [];
/**
* @param {string} name
* @param {string} description
* @param {ARGUMENT_TYPE|ARGUMENT_TYPE[]} types
* @param {string|SlashCommandClosure} defaultValue
* @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} enums
* @param {(executor:SlashCommandExecutor)=>SlashCommandEnumValue[]} enumProvider function that returns auto complete options
* @param {boolean} forceEnum
* @param {boolean} [isRequired=false]
* @param {boolean} [acceptsMultiple=false]
* @param {string|SlashCommandClosure} [defaultValue=null]
* @param {string|SlashCommandEnumValue|(string|SlashCommandEnumValue)[]} [enums=[]]
* @param {string[]} [aliases=[]]
* @param {(executor:SlashCommandExecutor)=>SlashCommandEnumValue[]} [enumProvider=null] function that returns auto complete options
* @param {boolean} [forceEnum=true]
*/
constructor(name, description, types, isRequired = false, acceptsMultiple = false, defaultValue = null, enums = [], aliases = [], enumProvider = null, forceEnum = true) {
super(description, types, isRequired, acceptsMultiple, defaultValue, enums, enumProvider, forceEnum);

View File

@ -93,7 +93,7 @@ export class SlashCommandClosure {
/**
*
* @returns Promise<SlashCommandClosureResult>
* @returns {Promise<SlashCommandClosureResult>}
*/
async execute() {
const closure = this.getCopy();

View File

@ -0,0 +1,84 @@
import { chat_metadata, characters, substituteParams } from "../../script.js";
import { extension_settings } from "../extensions.js";
import { groups } from "../group-chats.js";
import { searchCharByName, getTagsList, tags } from "../tags.js";
import { SlashCommandEnumValue } from "./SlashCommandEnumValue.js";
import { SlashCommandExecutor } from "./SlashCommandExecutor.js";
/**
* A collection of common enum providers
*
* Can be used on `SlashCommandNamedArgument` and `SlashCommandArgument` and their `enumProvider` property.
*/
export const commonEnumProviders = {
/**
* All possible variable names
*
* Can be filtered by `type` to only show global or local variables
*
* @param {...('global'|'local'|'scope'|'all')} type - The type of variables to include in the array. Can be 'all', 'global', or 'local'.
* @returns {() => SlashCommandEnumValue[]}
*/
variables: (...type) => () => {
const types = type.flat();
const isAll = types.includes('all');
return [
...isAll || types.includes('global') ? Object.keys(chat_metadata.variables).map(x => new SlashCommandEnumValue(x, null, 'variable', 'L')) : [],
...isAll || types.includes('local') ? Object.keys(extension_settings.variables.global).map(x => new SlashCommandEnumValue(x, null, 'variable', 'G')) : [],
...isAll || types.includes('scope') ? [].map(x => new SlashCommandEnumValue(x, null, 'variable', 'S')) : [], // TODO: Add scoped variables here, Lenny
]
},
/**
* All possible character and group names
*
* @returns {SlashCommandEnumValue[]}
*/
charName: () => [
...characters.map(it => new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
...groups.map(it => new SlashCommandEnumValue(it.name, null, 'variable', 'G')),
],
/**
* All possible tags for a given char/group entity
*
* @param {'all' | 'existing' | 'not-existing'} mode - Which types of tags to show
* @returns {() => SlashCommandEnumValue[]}
*/
tagsForChar: (mode) => (/** @type {SlashCommandExecutor} */ executor) => {
// Try to see if we can find the char during execution to filter down the tags list some more. Otherwise take all tags.
const key = searchCharByName(substituteParams(/**@type {string?}*/(executor.namedArgumentList.find(it => it.name == 'name')?.value)), { suppressLogging: true });
const assigned = key ? getTagsList(key) : [];
return tags.filter(it => !key || mode === 'all' || mode === 'existing' && assigned.includes(it) || mode === 'not-existing' && !assigned.includes(it))
.map(it => new SlashCommandEnumValue(it.name, it.title));
},
/**
* All existing worlds / lorebooks
*
* @returns {SlashCommandEnumValue[]}
*/
worlds: () => $('#world_info').children().toArray().map(x => new SlashCommandEnumValue(x.textContent)),
};
/**
* Get the unicode icon for the given enum value type
* @param {string} type The type of the enum value
* @returns {string} the unicode icon
*/
export function getEnumIconByValueType(type) {
// Remove nullable types definition to match type icon
type = type.replace(/\?$/, '');
switch (type) {
case 'boolean': return '🔲';
case 'string': return '📝';
case 'number': return '1⃣';
case 'array': return '📦';
case 'enum': return '📚';
case 'dictionary': return '📖';
case 'closure': return '🧩';
default: return '◊';
}
}

View File

@ -24,6 +24,7 @@ import { SlashCommand } from './slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
import { SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
import { SlashCommandExecutor } from './slash-commands/SlashCommandExecutor.js';
import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js';
export {
TAG_FOLDER_TYPES,
@ -469,6 +470,28 @@ export function getTagKeyForEntityElement(element) {
return undefined;
}
/**
* Gets the key for char/group by searching based on the name or avatar. If none can be found, a toastr will be shown and null returned.
* This function is mostly used in slash commands.
*
* @param {string?} [charName] The optionally provided char name
* @param {object} [options] - Optional arguments
* @param {boolean} [options.suppressLogging=false] - Whether to suppress the toastr warning
* @returns {string?} - The char/group key, or null if none found
*/
export function searchCharByName(charName, { suppressLogging = false } = {}) {
const entity = charName
? (characters.find(x => x.name === charName) || groups.find(x => x.name == charName))
: (selected_group ? groups.find(x => x.id == selected_group) : characters[this_chid]);
const key = getTagKeyForEntity(entity);
if (!key) {
if (!suppressLogging) toastr.warning(`Character ${charName} not found.`);
return null;
}
return key;
}
/**
* Adds a tag to a given entity
* @param {Tag} tag - The tag to add
@ -1543,23 +1566,6 @@ function printViewTagList(empty = true) {
}
function registerTagsSlashCommands() {
/**
* Gets the key for char/group for a slash command. If none can be found, a toastr will be shown and null returned.
* @param {string?} [charName] The optionally provided char name
* @returns {string?} - The char/group key, or null if none found
*/
function paraGetCharKey(charName, { suppressLogging = false } = {}) {
const entity = charName
? (characters.find(x => x.name === charName) || groups.find(x => x.name == charName))
: (selected_group ? groups.find(x => x.id == selected_group) : characters[this_chid]);
const key = getTagKeyForEntity(entity);
if (!key) {
if (!suppressLogging) toastr.warning(`Character ${charName} not found.`);
return null;
}
return key;
}
/**
* Gets a tag by its name. Optionally can create the tag if it does not exist.
* @param {string} tagName - The name of the tag
@ -1583,32 +1589,12 @@ function registerTagsSlashCommands() {
return tag;
}
/** A collection of enum providers used for the tag slash commands */
const enumProviders = {
/** Get a list of all possible character and group names */
charName: () => [
...characters.map(it => new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
...groups.map(it => new SlashCommandEnumValue(it.name, null, 'variable', 'G')),
],
/**
* Get A list of all possible tags for the given char/group entity
* @param {'all' | 'existing' | 'not-existing'} mode - Which types of tags to
*/
tagForChar: (mode) => (/** @type {SlashCommandExecutor} */ executor) => {
// Try to see if we can find the char during execution to filter down the tags list some more. Otherwise take all tags.
const key = paraGetCharKey(substituteParams(/**@type {string?}*/(executor.namedArgumentList.find(it => it.name == 'name')?.value)), { suppressLogging: true });
const assigned = key ? getTagsList(key) : [];
return tags.filter(it => !key || mode === 'all' || mode === 'existing' && assigned.includes(it) || mode === 'not-existing' && !assigned.includes(it))
.map(it => new SlashCommandEnumValue(it.name, it.title));
}
}
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'tag-add',
returns: 'true/false - Whether the tag was added or was assigned already',
/** @param {{name: string}} namedArgs @param {string} tagName @returns {string} */
callback: ({ name }, tagName) => {
const key = paraGetCharKey(name);
const key = searchCharByName(name);
if (!key) return 'false';
const tag = paraGetTag(tagName, { allowCreate: true });
if (!tag) return 'false';
@ -1621,14 +1607,14 @@ function registerTagsSlashCommands() {
description: 'Character name',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: '{{char}}',
enumProvider: enumProviders.charName,
enumProvider: commonEnumProviders.charName,
}),
],
unnamedArgumentList: [
SlashCommandArgument.fromProps({ description: 'tag name',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
enumProvider: enumProviders.tagForChar('not-existing'),
enumProvider: commonEnumProviders.tagsForChar('not-existing'),
forceEnum: false,
}),
],
@ -1653,7 +1639,7 @@ function registerTagsSlashCommands() {
returns: 'true/false - Whether the tag was removed or wasn\'t assigned already',
/** @param {{name: string}} namedArgs @param {string} tagName @returns {string} */
callback: ({ name }, tagName) => {
const key = paraGetCharKey(name);
const key = searchCharByName(name);
if (!key) return 'false';
const tag = paraGetTag(tagName);
if (!tag) return 'false';
@ -1666,7 +1652,7 @@ function registerTagsSlashCommands() {
description: 'Character name',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: '{{char}}',
enumProvider: enumProviders.charName,
enumProvider: commonEnumProviders.charName,
}),
],
unnamedArgumentList: [
@ -1674,7 +1660,7 @@ function registerTagsSlashCommands() {
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
/**@param {SlashCommandExecutor} executor */
enumProvider: enumProviders.tagForChar('existing'),
enumProvider: commonEnumProviders.tagsForChar('existing'),
}),
],
helpString: `
@ -1697,7 +1683,7 @@ function registerTagsSlashCommands() {
returns: 'true/false - Whether the given tag name is assigned to the character',
/** @param {{name: string}} namedArgs @param {string} tagName @returns {string} */
callback: ({ name }, tagName) => {
const key = paraGetCharKey(name);
const key = searchCharByName(name);
if (!key) return 'false';
const tag = paraGetTag(tagName);
if (!tag) return 'false';
@ -1709,7 +1695,7 @@ function registerTagsSlashCommands() {
description: 'Character name',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: '{{char}}',
enumProvider: enumProviders.charName,
enumProvider: commonEnumProviders.charName,
}),
],
unnamedArgumentList: [
@ -1718,7 +1704,7 @@ function registerTagsSlashCommands() {
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
/**@param {SlashCommandExecutor} executor */
enumProvider: enumProviders.tagForChar('all'),
enumProvider: commonEnumProviders.tagsForChar('all'),
}),
],
helpString: `
@ -1741,7 +1727,7 @@ function registerTagsSlashCommands() {
returns: 'Comma-separated list of all assigned tags',
/** @param {{name: string}} namedArgs @returns {string} */
callback: ({ name }) => {
const key = paraGetCharKey(name);
const key = searchCharByName(name);
if (!key) return '';
const tags = getTagsList(key);
return tags.map(x => x.name).join(', ');
@ -1752,7 +1738,7 @@ function registerTagsSlashCommands() {
description: 'Character name',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: '{{char}}',
enumProvider: enumProviders.charName,
enumProvider: commonEnumProviders.charName,
}),
],
helpString: `

View File

@ -1,16 +1,20 @@
import { chat_metadata, getCurrentChatId, saveSettingsDebounced, sendSystemMessage, system_message_types } from '../script.js';
import { extension_settings, saveMetadataDebounced } from './extensions.js';
import { executeSlashCommands, executeSlashCommandsWithOptions } from './slash-commands.js';
import { executeSlashCommandsWithOptions } from './slash-commands.js';
import { SlashCommand } from './slash-commands/SlashCommand.js';
import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js';
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
import { SlashCommandClosureResult } from './slash-commands/SlashCommandClosureResult.js';
import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js';
import { SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
import { PARSER_FLAG, SlashCommandParser } from './slash-commands/SlashCommandParser.js';
import { SlashCommandScope } from './slash-commands/SlashCommandScope.js';
import { isFalseBoolean } from './utils.js';
/** @typedef {import('./slash-commands/SlashCommandParser.js').NamedArguments} NamedArguments */
/** @typedef {import('./slash-commands/SlashCommand.js').UnnamedArguments} UnnamedArguments */
const MAX_LOOPS = 100;
function getLocalVariable(name, args = {}) {
@ -116,6 +120,7 @@ function setGlobalVariable(name, value, args = {}) {
extension_settings.variables.global[name] = value;
}
saveSettingsDebounced();
return value;
}
function addLocalVariable(name, value) {
@ -314,11 +319,12 @@ function listVariablesCallback() {
const htmlMessage = DOMPurify.sanitize(converter.makeHtml(message));
sendSystemMessage(system_message_types.GENERIC, htmlMessage);
return '';
}
/**
*
* @param {import('./slash-commands/SlashCommand.js').NamedArguments} args
* @param {NamedArguments} args
* @param {(string|SlashCommandClosure)[]} value
*/
async function whileCallback(args, value) {
@ -360,8 +366,8 @@ async function whileCallback(args, value) {
/**
*
* @param {import('./slash-commands/SlashCommand.js').NamedArguments} args
* @param {import('./slash-commands/SlashCommand.js').UnnamedArguments} value
* @param {NamedArguments} args
* @param {UnnamedArguments} value
* @returns
*/
async function timesCallback(args, value) {
@ -398,7 +404,7 @@ async function timesCallback(args, value) {
/**
*
* @param {import('./slash-commands/SlashCommand.js').NamedArguments} args
* @param {NamedArguments} args
* @param {(string|SlashCommandClosure)[]} value
*/
async function ifCallback(args, value) {
@ -765,13 +771,13 @@ function randValuesCallback(from, to, args) {
if (args.round == 'floor') {
return Math.floor(value);
}
return String(value);
return value;
}
/**
* Declare a new variable in the current scope.
* @param {{_scope:SlashCommandScope, key?:string}} args Named arguments.
* @param {String|[String, SlashCommandClosure]} value Name and optional value for the variable.
* @param {NamedArguments} args Named arguments.
* @param {string|SlashCommandClosure|(string|SlashCommandClosure)[]} value Name and optional value for the variable.
* @returns The variable's value
*/
function letCallback(args, value) {
@ -784,7 +790,9 @@ function letCallback(args, value) {
const val = value;
args._scope.letVariable(key, val);
return val;
} else if (value.includes(' ')) {
}
if (value instanceof SlashCommandClosure) throw new Error('/let unnamed argument does not support closures if no key is provided');
if (value.includes(' ')) {
const key = value.split(' ')[0];
const val = value.split(' ').slice(1).join(' ');
args._scope.letVariable(key, val);
@ -795,7 +803,7 @@ function letCallback(args, value) {
/**
* Set or retrieve a variable in the current scope or nearest ancestor scope.
* @param {{_hasUnnamedArgument:boolean, _scope:SlashCommandScope, key?:string, index?:string|number}} args Named arguments.
* @param {NamedArguments} args Named arguments.
* @param {string|SlashCommandClosure|(string|SlashCommandClosure)[]} value Name and optional value for the variable.
* @returns The variable's value
*/
@ -822,7 +830,7 @@ function varCallback(args, value) {
}
/**
* @param {import('./slash-commands/SlashCommand.js').NamedArguments} args
* @param {NamedArguments} args
* @param {SlashCommandClosure} value
* @returns {string}
*/
@ -834,8 +842,8 @@ function closureSerializeCallback(args, value) {
}
/**
* @param {import('./slash-commands/SlashCommand.js').NamedArguments} args
* @param {import('./slash-commands/SlashCommand.js').UnnamedArguments} value
* @param {NamedArguments} args
* @param {UnnamedArguments} value
* @returns {SlashCommandClosure}
*/
function closureDeserializeCallback(args, value) {
@ -846,17 +854,24 @@ function closureDeserializeCallback(args, value) {
}
export function registerVariableCommands() {
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'listvar',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'listvar',
callback: listVariablesCallback,
helpString: 'List registered chat variables.',
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'setvar',
callback: (args, value) => setLocalVariable(args.key || args.name, value, args),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'setvar',
callback: (args, value) => String(setLocalVariable(args.key || args.name, value, args)),
returns: 'the set variable value',
namedArgumentList: [
new SlashCommandNamedArgument(
'key', 'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('local'),
forceEnum: false,
}),
new SlashCommandNamedArgument(
'index', 'list index', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], false,
),
@ -880,21 +895,28 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'getvar',
callback: (args, value) => getLocalVariable(value, args),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'getvar',
callback: (args, value) => String(getLocalVariable(value, args)),
returns: 'the variable value',
namedArgumentList: [
new SlashCommandNamedArgument(
'key', 'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], false,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('local'),
}),
new SlashCommandNamedArgument(
'index', 'list index', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], false,
),
],
unnamedArgumentList: [
new SlashCommandArgument(
'key', [ARGUMENT_TYPE.VARIABLE_NAME], false,
),
SlashCommandArgument.fromProps({
description: 'key',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: false,
enumProvider: commonEnumProviders.variables('local'),
}),
],
helpString: `
<div>
@ -916,13 +938,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'addvar',
callback: (args, value) => addLocalVariable(args.key || args.name, value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'addvar',
callback: (args, value) => String(addLocalVariable(args.key || args.name, value)),
returns: 'the new variable value',
namedArgumentList: [
new SlashCommandNamedArgument(
'key', 'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('local'),
forceEnum: false,
}),
],
unnamedArgumentList: [
new SlashCommandArgument(
@ -943,13 +971,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'setglobalvar',
callback: (args, value) => setGlobalVariable(args.key || args.name, value, args),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'setglobalvar',
callback: (args, value) => String(setGlobalVariable(args.key || args.name, value, args)),
returns: 'the set global variable value',
namedArgumentList: [
new SlashCommandNamedArgument(
'key', 'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('global'),
forceEnum: false,
}),
new SlashCommandNamedArgument(
'index', 'list index', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], false,
),
@ -973,21 +1007,27 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'getglobalvar',
callback: (args, value) => getGlobalVariable(value, args),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'getglobalvar',
callback: (args, value) => String(getGlobalVariable(value, args)),
returns: 'global variable value',
namedArgumentList: [
new SlashCommandNamedArgument(
'key', 'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], false,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('global'),
}),
new SlashCommandNamedArgument(
'index', 'list index', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.STRING], false,
),
],
unnamedArgumentList: [
new SlashCommandArgument(
'key', [ARGUMENT_TYPE.VARIABLE_NAME], false,
),
SlashCommandArgument.fromProps({
description: 'key',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('global'),
}),
],
helpString: `
<div>
@ -1009,13 +1049,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'addglobalvar',
callback: (args, value) => addGlobalVariable(args.key || args.name, value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'addglobalvar',
callback: (args, value) => String(addGlobalVariable(args.key || args.name, value)),
returns: 'the new variable value',
namedArgumentList: [
new SlashCommandNamedArgument(
'key', 'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('global'),
forceEnum: false,
}),
],
unnamedArgumentList: [
new SlashCommandArgument(
@ -1036,13 +1082,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'incvar',
callback: (_, value) => incrementLocalVariable(value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'incvar',
callback: (_, value) => String(incrementLocalVariable(value)),
returns: 'the new variable value',
unnamedArgumentList: [
new SlashCommandArgument(
'key', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('local'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1058,13 +1110,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'decvar',
callback: (_, value) => decrementLocalVariable(value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'decvar',
callback: (_, value) => String(decrementLocalVariable(value)),
returns: 'the new variable value',
unnamedArgumentList: [
new SlashCommandArgument(
'key', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('local'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1080,13 +1138,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'incglobalvar',
callback: (_, value) => incrementGlobalVariable(value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'incglobalvar',
callback: (_, value) => String(incrementGlobalVariable(value)),
returns: 'the new variable value',
unnamedArgumentList: [
new SlashCommandArgument(
'key', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('global'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1102,13 +1166,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'decglobalvar',
callback: (_, value) => decrementGlobalVariable(value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'decglobalvar',
callback: (_, value) => String(decrementGlobalVariable(value)),
returns: 'the new variable value',
unnamedArgumentList: [
new SlashCommandArgument(
'key', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('global'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1124,7 +1194,8 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'if',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'if',
callback: ifCallback,
returns: 'result of the executed command ("then" or "else")',
namedArgumentList: [
@ -1136,16 +1207,16 @@ export function registerVariableCommands() {
),
new SlashCommandNamedArgument(
'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [
new SlashCommandEnumValue('gt', 'a > b'),
new SlashCommandEnumValue('gt', 'a > b'),
new SlashCommandEnumValue('gte', 'a >= b'),
new SlashCommandEnumValue('lt', 'a < b'),
new SlashCommandEnumValue('lt', 'a < b'),
new SlashCommandEnumValue('lte', 'a <= b'),
new SlashCommandEnumValue('eq', 'a == b'),
new SlashCommandEnumValue('eq', 'a == b'),
new SlashCommandEnumValue('neq', 'a !== b'),
new SlashCommandEnumValue('not', '!a'),
new SlashCommandEnumValue('in', 'a includes b'),
new SlashCommandEnumValue('nin', 'a not includes b'),
],
new SlashCommandEnumValue('in', 'a includes b'),
new SlashCommandEnumValue('nin', 'a not includes b'),
],
),
new SlashCommandNamedArgument(
'else', 'command to execute if not true', [ARGUMENT_TYPE.CLOSURE, ARGUMENT_TYPE.SUBCOMMAND], false,
@ -1191,7 +1262,8 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'while',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'while',
callback: whileCallback,
returns: 'result of the last executed command',
namedArgumentList: [
@ -1203,16 +1275,16 @@ export function registerVariableCommands() {
),
new SlashCommandNamedArgument(
'rule', 'comparison rule', [ARGUMENT_TYPE.STRING], true, false, null, [
new SlashCommandEnumValue('gt', 'a > b'),
new SlashCommandEnumValue('gt', 'a > b'),
new SlashCommandEnumValue('gte', 'a >= b'),
new SlashCommandEnumValue('lt', 'a < b'),
new SlashCommandEnumValue('lt', 'a < b'),
new SlashCommandEnumValue('lte', 'a <= b'),
new SlashCommandEnumValue('eq', 'a == b'),
new SlashCommandEnumValue('eq', 'a == b'),
new SlashCommandEnumValue('neq', 'a !== b'),
new SlashCommandEnumValue('not', '!a'),
new SlashCommandEnumValue('in', 'a includes b'),
new SlashCommandEnumValue('nin', 'a not includes b'),
],
new SlashCommandEnumValue('in', 'a includes b'),
new SlashCommandEnumValue('nin', 'a not includes b'),
],
),
new SlashCommandNamedArgument(
'guard', 'disable loop iteration limit', [ARGUMENT_TYPE.STRING], false, false, null, ['off'],
@ -1260,7 +1332,8 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'times',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'times',
callback: timesCallback,
returns: 'result of the last executed command',
namedArgumentList: [],
@ -1299,13 +1372,16 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'flushvar',
callback: (_, value) => deleteLocalVariable(value),
namedArgumentList: [],
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'flushvar',
callback: async (_, value) => deleteLocalVariable(value instanceof SlashCommandClosure ? (await value.execute())?.pipe : String(value)),
unnamedArgumentList: [
new SlashCommandArgument(
'key', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('local'),
}),
],
helpString: `
<div>
@ -1321,13 +1397,17 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'flushglobalvar',
callback: (_, value) => deleteGlobalVariable(value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'flushglobalvar',
callback: async (_, value) => deleteGlobalVariable(value instanceof SlashCommandClosure ? (await value.execute())?.pipe : String(value)),
namedArgumentList: [],
unnamedArgumentList: [
new SlashCommandArgument(
'key', [ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('global'),
}),
],
helpString: `
<div>
@ -1344,13 +1424,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'add',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'add',
callback: addValuesCallback,
returns: 'sum of the provided values',
unnamedArgumentList: [
new SlashCommandArgument(
'values', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true, true,
),
SlashCommandArgument.fromProps({
description: 'values to sum',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1367,16 +1453,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'mul',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'mul',
callback: (args, value) => mulValuesCallback(args, value),
result: 'product of the provided values',
returns: 'product of the provided values',
unnamedArgumentList: [
new SlashCommandArgument(
'values to multiply',
[ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
true,
true,
),
SlashCommandArgument.fromProps({
description: 'values to multiply',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1392,16 +1481,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'max',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'max',
callback: maxValuesCallback,
returns: 'maximum value of the set of values',
unnamedArgumentList: [
new SlashCommandArgument(
'values',
[ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
true,
true,
),
SlashCommandArgument.fromProps({
description: 'values to find the max',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1417,13 +1509,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'min',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'min',
callback: minValuesCallback,
returns: 'minimum value of the set of values',
unnamedArgumentList: [
new SlashCommandArgument(
'values', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true, true,
),
SlashCommandArgument.fromProps({
description: 'values to find the min',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1440,13 +1538,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'sub',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'sub',
callback: subValuesCallback,
returns: 'difference of the provided values',
unnamedArgumentList: [
new SlashCommandArgument(
'values', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true, true,
),
SlashCommandArgument.fromProps({
description: 'values to find the difference',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
acceptsMultiple: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1463,16 +1567,25 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'div',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'div',
callback: divValuesCallback,
returns: 'result of division',
unnamedArgumentList: [
new SlashCommandArgument(
'dividend', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
new SlashCommandArgument(
'divisor', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'dividend',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
SlashCommandArgument.fromProps({
description: 'divisor',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1489,16 +1602,25 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'mod',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'mod',
callback: modValuesCallback,
returns: 'result of modulo operation',
unnamedArgumentList: [
new SlashCommandArgument(
'dividend', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
new SlashCommandArgument(
'divisor', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'dividend',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
SlashCommandArgument.fromProps({
description: 'divisor',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1515,16 +1637,25 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'pow',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'pow',
callback: powValuesCallback,
returns: 'result of power operation',
unnamedArgumentList: [
new SlashCommandArgument(
'base', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
new SlashCommandArgument(
'exponent', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'base',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
SlashCommandArgument.fromProps({
description: 'exponent',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1541,13 +1672,18 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'sin',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'sin',
callback: sinValuesCallback,
returns: 'sine of the provided value',
unnamedArgumentList: [
new SlashCommandArgument(
'value', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1564,13 +1700,18 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'cos',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'cos',
callback: cosValuesCallback,
returns: 'cosine of the provided value',
unnamedArgumentList: [
new SlashCommandArgument(
'value', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1587,14 +1728,19 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'log',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'log',
callback: logValuesCallback,
returns: 'log of the provided value',
namedArgumentList: [],
unnamedArgumentList: [
new SlashCommandArgument(
'value', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1611,13 +1757,18 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'abs',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'abs',
callback: absValuesCallback,
returns: 'absolute value of the provided value',
unnamedArgumentList: [
new SlashCommandArgument(
'value', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1634,13 +1785,18 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'sqrt',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'sqrt',
callback: sqrtValuesCallback,
returns: 'square root of the provided value',
unnamedArgumentList: [
new SlashCommandArgument(
'value', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1657,13 +1813,18 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'round',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'round',
callback: roundValuesCallback,
returns: 'rounded value',
unnamedArgumentList: [
new SlashCommandArgument(
'value', [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1680,13 +1841,18 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'len',
callback: (_, value) => lenValuesCallback(value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'len',
callback: (_, value) => String(lenValuesCallback(value)),
returns: 'length of the provided value',
unnamedArgumentList: [
new SlashCommandArgument(
'value', [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.VARIABLE_NAME], true,
),
SlashCommandArgument.fromProps({
description: 'value',
typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.VARIABLE_NAME],
isRequired: true,
enumProvider: commonEnumProviders.variables('all'),
forceEnum: false,
}),
],
helpString: `
<div>
@ -1702,8 +1868,9 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'rand',
callback: (args, value) => randValuesCallback(Number(args.from ?? 0), Number(args.to ?? (value.length ? value : 1)), args),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'rand',
callback: (args, value) => String(randValuesCallback(Number(args.from ?? 0), Number(args.to ?? (value ? value : 1)), args)),
returns: 'random number',
namedArgumentList: [
new SlashCommandNamedArgument(
@ -1755,13 +1922,18 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'var',
callback: (args, value) => varCallback(args, value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'var',
callback: (/** @type {NamedArguments} */ args, value) => varCallback(args, value),
returns: 'the variable value',
namedArgumentList: [
new SlashCommandNamedArgument(
'key', 'variable name; forces setting the variable, even if no value is provided', [ARGUMENT_TYPE.VARIABLE_NAME], false,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name; forces setting the variable, even if no value is provided',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('scope'),
forceEnum: false,
}),
new SlashCommandNamedArgument(
'index',
'optional index for list or dictionary',
@ -1771,12 +1943,12 @@ export function registerVariableCommands() {
),
],
unnamedArgumentList: [
new SlashCommandArgument(
'variable name',
[ARGUMENT_TYPE.VARIABLE_NAME],
false, // isRequired
false, // acceptsMultiple
),
SlashCommandArgument.fromProps({
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('scope'),
forceEnum: false,
}),
new SlashCommandArgument(
'variable value',
[ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.BOOLEAN, ARGUMENT_TYPE.LIST, ARGUMENT_TYPE.DICTIONARY, ARGUMENT_TYPE.CLOSURE],
@ -1802,18 +1974,26 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'let',
callback: (args, value) => letCallback(args, value),
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'let',
callback: (/** @type {NamedArguments} */ args, value) => letCallback(args, value),
returns: 'the variable value',
namedArgumentList: [
new SlashCommandNamedArgument(
'key', 'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], false,
),
SlashCommandNamedArgument.fromProps({
name: 'key',
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('scope'),
forceEnum: false,
}),
],
unnamedArgumentList: [
new SlashCommandArgument(
'variable name', [ARGUMENT_TYPE.VARIABLE_NAME], false,
),
SlashCommandArgument.fromProps({
description: 'variable name',
typeList: [ARGUMENT_TYPE.VARIABLE_NAME],
enumProvider: commonEnumProviders.variables('scope'),
forceEnum: false,
}),
new SlashCommandArgument(
'variable value', [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.BOOLEAN, ARGUMENT_TYPE.LIST, ARGUMENT_TYPE.DICTIONARY, ARGUMENT_TYPE.CLOSURE],
),
@ -1839,16 +2019,18 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'closure-serialize',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'closure-serialize',
/**
*
* @param {import('./slash-commands/SlashCommand.js').NamedArguments} args
* @param {NamedArguments} args
* @param {SlashCommandClosure} value
* @returns {string}
*/
callback: (args, value)=>closureSerializeCallback(args, value),
callback: (args, value) => closureSerializeCallback(args, value),
unnamedArgumentList: [
SlashCommandArgument.fromProps({ description: 'the closure to serialize',
SlashCommandArgument.fromProps({
description: 'the closure to serialize',
typeList: [ARGUMENT_TYPE.CLOSURE],
isRequired: true,
}),
@ -1868,15 +2050,17 @@ export function registerVariableCommands() {
</div>
`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'closure-deserialize',
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'closure-deserialize',
/**
* @param {import('./slash-commands/SlashCommand.js').NamedArguments} args
* @param {import('./slash-commands/SlashCommand.js').UnnamedArguments} value
* @param {NamedArguments} args
* @param {UnnamedArguments} value
* @returns {SlashCommandClosure}
*/
callback: (args, value)=>closureDeserializeCallback(args, value),
callback: (args, value) => closureDeserializeCallback(args, value),
unnamedArgumentList: [
SlashCommandArgument.fromProps({ description: 'serialized closure',
SlashCommandArgument.fromProps({
description: 'serialized closure',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
}),

View File

@ -13,6 +13,8 @@ import { getRegexedString, regex_placement } from './extensions/regex/engine.js'
import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
import { SlashCommand } from './slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
import { SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
import { commonEnumProviders, getEnumIconByValueType } from './slash-commands/SlashCommandCommonEnumsProvider.js';
export {
world_info,
@ -698,6 +700,12 @@ function registerWorldInfoSlashCommands() {
return '';
}
/** A collection of local enum providers for this context of world info */
const localEnumProviders = {
/** All possible fields that can be set in a WI entry */
wiEntryFields: () => Object.entries(newEntryDefinition).map(([key, value]) => new SlashCommandEnumValue(key, `[${value.type}] default: ${(typeof value.default === 'string' ? `'${value.default}'` : value.default)}`, 'property', getEnumIconByValueType(value.type))),
};
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'world',
callback: onWorldInfoChange,
namedArgumentList: [
@ -709,9 +717,11 @@ function registerWorldInfoSlashCommands() {
),
],
unnamedArgumentList: [
new SlashCommandArgument(
'name', [ARGUMENT_TYPE.STRING], false,
),
SlashCommandArgument.fromProps({
description: 'world name',
typeList: [ARGUMENT_TYPE.STRING],
enumProvider: commonEnumProviders.worlds,
}),
],
helpString: `
<div>
@ -726,17 +736,26 @@ function registerWorldInfoSlashCommands() {
helpString: 'Get a name of the chat-bound lorebook or create a new one if was unbound, and pass it down the pipe.',
aliases: ['getchatlore', 'getchatwi'],
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'findentry',
aliases: ['findlore', 'findwi'],
returns: 'UID',
callback: findBookEntryCallback,
namedArgumentList: [
new SlashCommandNamedArgument(
'file', 'bookName', ARGUMENT_TYPE.STRING, true,
),
new SlashCommandNamedArgument(
'field', 'field value for fuzzy match (default: key)', ARGUMENT_TYPE.STRING, false, false, 'key',
),
SlashCommandNamedArgument.fromProps({
name: 'file',
description: 'book name',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
enumProvider: commonEnumProviders.loreBooks,
}),
SlashCommandNamedArgument.fromProps({
name: 'field',
description: 'field value for fuzzy match (default: key)',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: 'key',
enumList: localEnumProviders.wiEntryFields(),
}),
],
unnamedArgumentList: [
new SlashCommandArgument(
@ -762,12 +781,20 @@ function registerWorldInfoSlashCommands() {
callback: getEntryFieldCallback,
returns: 'field value',
namedArgumentList: [
new SlashCommandNamedArgument(
'file', 'bookName', ARGUMENT_TYPE.STRING, true,
),
new SlashCommandNamedArgument(
'field', 'field to retrieve (default: content)', ARGUMENT_TYPE.STRING, false, false, 'content',
),
SlashCommandNamedArgument.fromProps({
name: 'file',
description: 'book name',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
enumProvider: commonEnumProviders.loreBooks,
}),
SlashCommandNamedArgument.fromProps({
name: 'field',
description: 'field to retrieve (default: content)',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: 'content',
enumList: localEnumProviders.wiEntryFields(),
}),
],
unnamedArgumentList: [
new SlashCommandArgument(
@ -793,9 +820,13 @@ function registerWorldInfoSlashCommands() {
aliases: ['createlore', 'createwi'],
returns: 'UID of the new record',
namedArgumentList: [
new SlashCommandNamedArgument(
'file', 'book name', [ARGUMENT_TYPE.STRING], true,
),
SlashCommandNamedArgument.fromProps({
name: 'file',
description: 'book name',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
enumProvider: commonEnumProviders.loreBooks,
}),
new SlashCommandNamedArgument(
'key', 'record key', [ARGUMENT_TYPE.STRING], false,
),
@ -823,15 +854,23 @@ function registerWorldInfoSlashCommands() {
callback: setEntryFieldCallback,
aliases: ['setlorefield', 'setwifield'],
namedArgumentList: [
new SlashCommandNamedArgument(
'file', 'book name', [ARGUMENT_TYPE.STRING], true,
),
SlashCommandNamedArgument.fromProps({
name: 'file',
description: 'book name',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
enumProvider: commonEnumProviders.loreBooks,
}),
new SlashCommandNamedArgument(
'uid', 'record UID', [ARGUMENT_TYPE.STRING], true,
),
new SlashCommandNamedArgument(
'field', 'field name', [ARGUMENT_TYPE.STRING], true, false, 'content',
),
SlashCommandNamedArgument.fromProps({
name: 'field',
description: 'field name (default: content)',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: 'content',
enumList: localEnumProviders.wiEntryFields(),
}),
],
unnamedArgumentList: [
new SlashCommandArgument(
@ -2450,36 +2489,47 @@ function deleteWorldInfoEntry(data, uid) {
delete data.entries[uid];
}
const newEntryTemplate = {
key: [],
keysecondary: [],
comment: '',
content: '',
constant: false,
vectorized: false,
selective: true,
selectiveLogic: world_info_logic.AND_ANY,
addMemo: false,
order: 100,
position: 0,
disable: false,
excludeRecursion: false,
preventRecursion: false,
delayUntilRecursion: false,
probability: 100,
useProbability: true,
depth: DEFAULT_DEPTH,
group: '',
groupOverride: false,
groupWeight: DEFAULT_WEIGHT,
scanDepth: null,
caseSensitive: null,
matchWholeWords: null,
useGroupScoring: null,
automationId: '',
role: 0,
/**
* Definitions of types for new WI entries
*
* Use `newEntryTemplate` if you just need the template that contains default values
*
* @type {{[key: string]: { default: any, type: string }}}
*/
const newEntryDefinition = {
key: { default: [], type: 'array' },
keysecondary: { default: [], type: 'array' },
comment: { default: '', type: 'string' },
content: { default: '', type: 'string' },
constant: { default: false, type: 'boolean' },
vectorized: { default: false, type: 'boolean' },
selective: { default: true, type: 'boolean' },
selectiveLogic: { default: world_info_logic.AND_ANY, type: 'enum' },
addMemo: { default: false, type: 'boolean' },
order: { default: 100, type: 'number' },
position: { default: 0, type: 'number' },
disable: { default: false, type: 'boolean' },
excludeRecursion: { default: false, type: 'boolean' },
preventRecursion: { default: false, type: 'boolean' },
delayUntilRecursion: { default: false, type: 'boolean' },
probability: { default: 100, type: 'number' },
useProbability: { default: true, type: 'boolean' },
depth: { default: DEFAULT_DEPTH, type: 'number' },
group: { default: '', type: 'string' },
groupOverride: { default: false, type: 'boolean' },
groupWeight: { default: DEFAULT_WEIGHT, type: 'number' },
scanDepth: { default: null, type: 'number?' },
caseSensitive: { default: null, type: 'boolean?' },
matchWholeWords: { default: null, type: 'boolean?' },
useGroupScoring: { default: null, type: 'boolean?' },
automationId: { default: '', type: 'string' },
role: { default: 0, type: 'enum' },
};
const newEntryTemplate = Object.fromEntries(
Object.entries(newEntryDefinition).map(([key, value]) => [key, value.default])
);
function createWorldInfoEntry(_name, data) {
const newUid = getFreeWorldEntryUid(data);