From 2e476bc742829d5d3b9360f764dbb327f650331a Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sun, 15 Mar 2026 20:22:25 +0200 Subject: [PATCH] Add reasoning-collapse, reasoning-expand, reasoning-toggle slash commands with range support (#5296) * Initial plan * Add reasoning-collapse, reasoning-expand, reasoning-toggle slash commands Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> * Add message range support to reasoning commands and allChatRange macro Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Cohee1207 <18619528+Cohee1207@users.noreply.github.com> --- package-lock.json | 13 ---- public/scripts/macros.js | 1 + .../scripts/macros/definitions/chat-macros.js | 12 +++ public/scripts/reasoning.js | 76 ++++++++++++++++++- 4 files changed, 88 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 217ba8220..66a60e6fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -549,7 +549,6 @@ "resolved": "https://registry.npmjs.org/@jimp/custom/-/custom-0.22.12.tgz", "integrity": "sha512-xcmww1O/JFP2MrlGUMd3Q78S3Qu6W3mYTXYuIqFq33EorgYHV/HqymHfXy9GjiCJ7OI+7lWx6nYFOzU7M4rd1Q==", "license": "MIT", - "peer": true, "dependencies": { "@jimp/core": "^0.22.12" } @@ -1011,7 +1010,6 @@ "resolved": "https://registry.npmjs.org/@jimp/plugin-resize/-/plugin-resize-1.6.0.tgz", "integrity": "sha512-uSUD1mqXN9i1SGSz5ov3keRZ7S9L32/mAQG08wUwZiEi5FpbV0K8A8l1zkazAIZi9IJzLlTauRNU41Mi8IF9fA==", "license": "MIT", - "peer": true, "dependencies": { "@jimp/core": "1.6.0", "@jimp/types": "1.6.0", @@ -1043,7 +1041,6 @@ "resolved": "https://registry.npmjs.org/@jimp/plugin-scale/-/plugin-scale-0.22.12.tgz", "integrity": "sha512-dghs92qM6MhHj0HrV2qAwKPMklQtjNpoYgAB94ysYpsXslhRTiPisueSIELRwZGEr0J0VUxpUY7HgJwlSIgGZw==", "license": "MIT", - "peer": true, "dependencies": { "@jimp/utils": "^0.22.12" }, @@ -1139,7 +1136,6 @@ "resolved": "https://registry.npmjs.org/@jimp/plugin-blit/-/plugin-blit-0.22.12.tgz", "integrity": "sha512-xslz2ZoFZOPLY8EZ4dC29m168BtDx95D6K80TzgUi8gqT7LY6CsajWO0FAxDwHz6h0eomHMfyGX0stspBrTKnQ==", "license": "MIT", - "peer": true, "dependencies": { "@jimp/utils": "^0.22.12" }, @@ -1176,7 +1172,6 @@ "resolved": "https://registry.npmjs.org/@jimp/plugin-color/-/plugin-color-0.22.12.tgz", "integrity": "sha512-xImhTE5BpS8xa+mAN6j4sMRWaUgUDLoaGHhJhpC+r7SKKErYDR0WQV4yCE4gP+N0gozD0F3Ka1LUSaMXrn7ZIA==", "license": "MIT", - "peer": true, "dependencies": { "@jimp/utils": "^0.22.12", "tinycolor2": "^1.6.0" @@ -1220,7 +1215,6 @@ "resolved": "https://registry.npmjs.org/@jimp/plugin-crop/-/plugin-crop-0.22.12.tgz", "integrity": "sha512-FNuUN0OVzRCozx8XSgP9MyLGMxNHHJMFt+LJuFjn1mu3k0VQxrzqbN06yIl46TVejhyAhcq5gLzqmSCHvlcBVw==", "license": "MIT", - "peer": true, "dependencies": { "@jimp/utils": "^0.22.12" }, @@ -1308,7 +1302,6 @@ "resolved": "https://registry.npmjs.org/@jimp/plugin-resize/-/plugin-resize-0.22.12.tgz", "integrity": "sha512-3NyTPlPbTnGKDIbaBgQ3HbE6wXbAlFfxHVERmrbqAi8R3r6fQPxpCauA8UVDnieg5eo04D0T8nnnNIX//i/sXg==", "license": "MIT", - "peer": true, "dependencies": { "@jimp/utils": "^0.22.12" }, @@ -1321,7 +1314,6 @@ "resolved": "https://registry.npmjs.org/@jimp/plugin-rotate/-/plugin-rotate-0.22.12.tgz", "integrity": "sha512-9YNEt7BPAFfTls2FGfKBVgwwLUuKqy+E8bDGGEsOqHtbuhbshVGxN2WMZaD4gh5IDWvR+emmmPPWGgaYNYt1gA==", "license": "MIT", - "peer": true, "dependencies": { "@jimp/utils": "^0.22.12" }, @@ -1922,7 +1914,6 @@ "integrity": "sha512-Crp6WY9aTYP3qPi2wGDo9iUe/rceX01UMhnF1jmwDcKCFM6cx7YhGP/Mpr3y9AASpfHixIG0E6azCcL5OcDHsQ==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@types/body-parser": "*", "@types/express-serve-static-core": "^4.17.33", @@ -2612,7 +2603,6 @@ "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.16.0.tgz", "integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==", "license": "MIT", - "peer": true, "bin": { "acorn": "bin/acorn" }, @@ -3232,7 +3222,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "baseline-browser-mapping": "^2.9.0", "caniuse-lite": "^1.0.30001759", @@ -4567,7 +4556,6 @@ "deprecated": "This version is no longer supported. Please see https://eslint.org/version-support for other options.", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.6.1", @@ -8125,7 +8113,6 @@ "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.18.0.tgz", "integrity": "sha512-PlXPeEWMXMZ7sPYOHqmDyCJzcfNrUr3fGNKtezX14ykXOEIvyK81d+qydx89KY5O71FKMPaQ2vBfBFI5NHR63A==", "license": "MIT", - "peer": true, "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", diff --git a/public/scripts/macros.js b/public/scripts/macros.js index b9b5aaf7f..d3d7a57dd 100644 --- a/public/scripts/macros.js +++ b/public/scripts/macros.js @@ -653,6 +653,7 @@ export function evaluateMacros(content, env, postProcessFn) { { regex: /{{firstDisplayedMessageId}}/gi, replace: () => String(getFirstDisplayedMessageId() ?? '') }, { regex: /{{lastSwipeId}}/gi, replace: () => String(getLastSwipeId() ?? '') }, { regex: /{{currentSwipeId}}/gi, replace: () => String(getCurrentSwipeId() ?? '') }, + { regex: /{{allChatRange}}/gi, replace: () => chat.length === 0 ? '' : `0-${chat.length - 1}` }, { regex: /{{reverse:(.+?)}}/gi, replace: (_, str) => Array.from(str).reverse().join('') }, { regex: /\{\{\/\/([\s\S]*?)\}\}/gm, replace: () => '' }, { regex: /{{time}}/gi, replace: () => moment().format('LT') }, diff --git a/public/scripts/macros/definitions/chat-macros.js b/public/scripts/macros/definitions/chat-macros.js index ebe8e9ece..0dd179eec 100644 --- a/public/scripts/macros/definitions/chat-macros.js +++ b/public/scripts/macros/definitions/chat-macros.js @@ -66,6 +66,18 @@ export function registerChatMacros() { returnType: MacroValueType.INTEGER, handler: () => String(getCurrentSwipeId() ?? ''), }); + + MacroRegistry.registerMacro('allChatRange', { + category: MacroCategory.CHAT, + description: 'Range of all message IDs in the chat (e.g. "0-10"). Empty string if the chat is empty.', + returns: 'Range string from 0 to last message ID, or empty string.', + handler: () => { + if (!Array.isArray(chat) || chat.length === 0) { + return ''; + } + return `0-${chat.length - 1}`; + }, + }); } function getLastMessageId({ exclude_swipe_in_propress = true, filter = null } = {}) { diff --git a/public/scripts/reasoning.js b/public/scripts/reasoning.js index 6dbb5d9f6..578337230 100644 --- a/public/scripts/reasoning.js +++ b/public/scripts/reasoning.js @@ -16,7 +16,7 @@ import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandE import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; import { textgen_types, textgenerationwebui_settings } from './textgen-settings.js'; import { applyStreamFadeIn } from './util/stream-fadein.js'; -import { copyText, escapeRegex, isFalseBoolean, isTrueBoolean, setDatasetProperty, trimSpaces } from './utils.js'; +import { copyText, escapeRegex, isFalseBoolean, isTrueBoolean, setDatasetProperty, stringToRange, trimSpaces } from './utils.js'; /** * @typedef {object} ReasoningTemplate @@ -1045,6 +1045,80 @@ function registerReasoningSlashCommands() { `, })); + + /** + * Gets the reasoning details elements for a message range. + * @param {string} value Unnamed argument value (message ID or range) + * @returns {JQuery|null} The reasoning details elements, or null if not found + */ + function getReasoningDetailsElements(value) { + const range = value ? stringToRange(String(value), 0, chat.length - 1) : { start: chat.length - 1, end: chat.length - 1 }; + if (!range) { + toastr.warning(t`Invalid message ID or range: ${value}`); + return null; + } + const selector = Array.from({ length: range.end - range.start + 1 }, (_, i) => + `#chat [mesid="${range.start + i}"] .mes_reasoning_details`, + ).join(','); + const details = $(selector); + if (details.length === 0) { + toastr.warning(t`No reasoning blocks found for the specified messages.`); + return null; + } + return details; + } + + const reasoningVisibilityArgs = [ + SlashCommandArgument.fromProps({ + description: 'Message ID or range (e.g. 0-10). If not provided, the last message is used.', + typeList: [ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.RANGE], + enumProvider: commonEnumProviders.messages(), + }), + ]; + + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'reasoning-collapse', + aliases: ['collapse-reasoning'], + helpString: t`Collapse the reasoning block of a message or range of messages.`, + unnamedArgumentList: reasoningVisibilityArgs, + callback: (_args, value) => { + const details = getReasoningDetailsElements(value); + if (details) details.removeAttr('open'); + return ''; + }, + })); + + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'reasoning-expand', + aliases: ['expand-reasoning'], + helpString: t`Expand the reasoning block of a message or range of messages.`, + unnamedArgumentList: reasoningVisibilityArgs, + callback: (_args, value) => { + const details = getReasoningDetailsElements(value); + if (details) details.attr('open', ''); + return ''; + }, + })); + + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'reasoning-toggle', + aliases: ['toggle-reasoning'], + helpString: t`Toggle the reasoning block of a message or range of messages. Expanded blocks will be collapsed, and collapsed blocks will be expanded.`, + unnamedArgumentList: reasoningVisibilityArgs, + callback: (_args, value) => { + const details = getReasoningDetailsElements(value); + if (!details) return ''; + details.each(function () { + const $el = $(this); + if ($el.attr('open') !== undefined) { + $el.removeAttr('open'); + } else { + $el.attr('open', ''); + } + }); + return ''; + }, + })); } function registerReasoningMacros() {