Merge branch 'parse-reasoning-command-args' into reasoning-parsing-streaming

994d69508b9aff6fc0e25b67f3c445a0a08a9ad4

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +40 -9Showing whitespace changes
public/scripts/reasoning.js+39 -9
@@ -10,7 +10,8 @@ import { Popup } from './popup.js';
1010import { power_user } from './power-user.js';
1111import { SlashCommand } from './slash-commands/SlashCommand.js';
1212import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
1313import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
14+import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js';
1415import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
1516import { textgen_types, textgenerationwebui_settings } from './textgen-settings.js';
1617import { copyText, escapeRegex, isFalseBoolean, setDatasetProperty, trimSpaces } from './utils.js';
@@ -678,7 +679,26 @@ function registerReasoningSlashCommands() {
678679 typeList: [ARGUMENT_TYPE.BOOLEAN],
679680 defaultValue: 'true',
680681 isRequired: false,
681682 enumProviderenumList: commonEnumProviders.boolean('trueFalse')(),
683+ }),
684+ SlashCommandNamedArgument.fromProps({
685+ name: 'return',
686+ description: 'Whether to return the parsed reasoning or the content without reasoning',
687+ typeList: [ARGUMENT_TYPE.STRING],
688+ defaultValue: 'reasoning',
689+ isRequired: false,
690+ enumList: [
691+ new SlashCommandEnumValue('reasoning', null, enumTypes.enum, enumIcons.reasoning),
692+ new SlashCommandEnumValue('content', null, enumTypes.enum, enumIcons.message),
693+ ],
694+ }),
695+ SlashCommandNamedArgument.fromProps({
696+ name: 'strict',
697+ description: 'Whether to require the reasoning block to be at the beginning of the string (excluding whitespaces).',
698+ typeList: [ARGUMENT_TYPE.BOOLEAN],
699+ defaultValue: 'true',
700+ isRequired: false,
701+ enumList: commonEnumProviders.boolean('trueFalse')(),
682702 }),
683703 ],
684704 unnamedArgumentList: [
@@ -688,19 +708,27 @@ function registerReasoningSlashCommands() {
688708 }),
689709 ],
690710 callback: (args, value) => {
691711 if (!value || typeof value !== 'string') {
692712 return '';
693713 }
694714
695715 if (!power_user.reasoning.prefix || !power_user.reasoning.suffix) {
696716 toastr.warning(t`Both prefix and suffix must be set in the Reasoning Formatting settings.`, t`Reasoning Parse`);
697717 return String(value);
718+ }
719+ if (typeof args.return !== 'string' || !['reasoning', 'content'].includes(args.return)) {
720+ toastr.warning(t`Invalid return type '${args.return}', defaulting to 'reasoning'.`, t`Reasoning Parse`);
698721 }
699722
700- const parsedReasoning = parseReasoningFromString(String(value));
723+ const returnMessage = args.return === 'content';
701724
725+ const parsedReasoning = parseReasoningFromString(value, { strict: !isFalseBoolean(String(args.strict ?? '')) });
702726 if (!parsedReasoning) {
703727 return returnMessage ? value : '';
728+ }
729+
730+ if (returnMessage) {
731+ return parsedReasoning.content;
704732 }
705733
706734 const applyRegex = !isFalseBoolean(String(args.regex ?? ''));
@@ -902,16 +930,18 @@ export function removeReasoningFromString(str) {
902930 * @property {string} reasoning Reasoning block
903931 * @property {string} content Message content
904932 * @param {string} str Content of the message
933+ * @param {Object} options Optional arguments
934+ * @param {boolean} [options.strict=true] Whether the reasoning block **has** to be at the beginning of the provided string (excluding whitespaces), or can be anywhere in it
905935 * @returns {ParsedReasoning|null} Parsed reasoning block and message content
906936 */
907937function parseReasoningFromString(str, { strict = true } = {}) {
908938 // Both prefix and suffix must be defined
909939 if (!power_user.reasoning.prefix || !power_user.reasoning.suffix) {
910940 return null;
911941 }
912942
913943 try {
914944 const regex = new RegExp(`${(strict ? '^\\s*?' : '')}${escapeRegex(power_user.reasoning.prefix)}(.*?)${escapeRegex(power_user.reasoning.suffix)}`, 's');
915945
916946 let didReplace = false;
917947 let reasoning = '';
public/scripts/slash-commands/SlashCommandCommonEnumsProvider.js+1 -0
@@ -34,6 +34,7 @@ export const enumIcons = {
3434 preset: '⚙️',
3535 file: '📄',
3636 message: '💬',
37+ reasoning: '💡',
3738 voice: '🎤',
3839 server: '🖥️',
3940 popup: '🗔',