Execute profile commands callbacks directly
| @@ -1,12 +1,14 @@ | ||
| 1 | 1 | import { event_types, eventSource, main_api, saveSettingsDebounced } from '../../../script.js'; |
| 2 | 2 | import { extension_settings, renderExtensionTemplateAsync } from '../../extensions.js'; |
| 3 | 3 | import { callGenericPopup, Popup, POPUP_TYPE } from '../../popup.js'; |
| 4 | -import { executeSlashCommandsWithOptions } from '../../slash-commands.js'; | |
| 5 | 4 | import { SlashCommand } from '../../slash-commands/SlashCommand.js'; |
| 5 | +import { SlashCommandAbortController } from '../../slash-commands/SlashCommandAbortController.js'; | |
| 6 | 6 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js'; |
| 7 | 7 | import { commonEnumProviders, enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 8 | +import { SlashCommandDebugController } from '../../slash-commands/SlashCommandDebugController.js'; | |
| 8 | 9 | import { enumTypes, SlashCommandEnumValue } from '../../slash-commands/SlashCommandEnumValue.js'; |
| 9 | 10 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; |
| 11 | +import { SlashCommandScope } from '../../slash-commands/SlashCommandScope.js'; | |
| 10 | 12 | import { collapseSpaces, getUniqueName, isFalseBoolean, uuidv4 } from '../../utils.js'; |
| 11 | 13 | |
| 12 | 14 | const MODULE_NAME = 'connection-manager'; |
| @@ -91,6 +93,24 @@ class ConnectionManagerSpinner { | ||
| 91 | 93 | } |
| 92 | 94 | } |
| 93 | 95 | |
| 96 | +/** | |
| 97 | + * Get named arguments for the command callback. | |
| 98 | + * @param {object} [args] Additional named arguments | |
| 99 | + * @returns {object} Named arguments | |
| 100 | + */ | |
| 101 | +function getNamedArguments(args = {}) { | |
| 102 | + // None of the commands here use underscored args, but better safe than sorry | |
| 103 | + return { | |
| 104 | + _scope: new SlashCommandScope(), | |
| 105 | + _abortController: new SlashCommandAbortController(), | |
| 106 | + _debugController: new SlashCommandDebugController(), | |
| 107 | + _parserFlags: {}, | |
| 108 | + _hasUnnamedArgument: false, | |
| 109 | + quiet: 'true', | |
| 110 | + ...args, | |
| 111 | + }; | |
| 112 | +} | |
| 113 | + | |
| 94 | 114 | /** @type {() => SlashCommandEnumValue[]} */ |
| 95 | 115 | const profilesProvider = () => [ |
| 96 | 116 | new SlashCommandEnumValue(NONE), |
| @@ -112,8 +132,6 @@ const profilesProvider = () => [ | ||
| 112 | 132 | * @property {string} [tokenizer] Tokenizer |
| 113 | 133 | */ |
| 114 | 134 | |
| 115 | -const escapeArgument = (a) => a.replace(/"/g, '\\"').replace(/\|/g, '\\|'); | |
| 116 | - | |
| 117 | 135 | /** |
| 118 | 136 | * Finds the best match for the search value. |
| 119 | 137 | * @param {string} value Search value |
| @@ -149,15 +167,15 @@ async function readProfileFromCommands(mode, profile, cleanUp = false) { | ||
| 149 | 167 | const commands = mode === 'cc' ? CC_COMMANDS : TC_COMMANDS; |
| 150 | 168 | const opposingCommands = mode === 'cc' ? TC_COMMANDS : CC_COMMANDS; |
| 151 | 169 | for (const command of commands) { |
| 152 | - const commandText = `/${command} quiet=true`; | |
| 153 | 170 | try { |
| 154 | - const result = await executeSlashCommandsWithOptions(commandText, { handleParserErrors: false, handleExecutionErrors: false }); | |
| 171 | + const args = getNamedArguments(); | |
| 155 | - if (result.pipe) { | |
| 172 | + const result = await SlashCommandParser.commands[command].callback(args, ''); | |
| 156 | - profile[command] = result.pipe; | |
| 173 | + if (result) { | |
| 174 | + profile[command] = result; | |
| 157 | 175 | continue; |
| 158 | 176 | } |
| 159 | 177 | } catch (error) { |
| 160 | 178 | console.warn(`Failed to execute command: ${commandTextcommand}`, error); |
| 161 | 179 | } |
| 162 | 180 | } |
| 163 | 181 | |
| @@ -273,11 +291,11 @@ async function applyConnectionProfile(profile) { | ||
| 273 | 291 | if (!argument) { |
| 274 | 292 | continue; |
| 275 | 293 | } |
| 276 | - const commandText = `/${command} quiet=true ${escapeArgument(argument)}`; | |
| 277 | 294 | try { |
| 278 | - await executeSlashCommandsWithOptions(commandText, { handleParserErrors: false, handleExecutionErrors: false }); | |
| 295 | + const args = getNamedArguments(); | |
| 296 | + await SlashCommandParser.commands[command].callback(args, argument); | |
| 279 | 297 | } catch (error) { |
| 280 | 298 | console.error(`Failed to execute command: ${commandTextcommand} ${argument}`, error); |
| 281 | 299 | } |
| 282 | 300 | } |
| 283 | 301 | |