Handle auto parsing reasoning during streaming - Add function to handle auto parsing reasoning from the streamed message during streaming - Only works when the reasoning prefix is EXACTLY at the beginning of the message - Tried to keep this lightweight, no regex parsing, remembering the index, so it's simple string splicing - Add utility function that trims a string only if `trim_spaces` is enabled

9590127bae282e31bcca5fe5c475a4426a080bc0

Wolfsblvt <wolfsblvt@gmail.com>

3 files changed, +82 -13Ignore whitespace
public/script.js+2 -1
@@ -3223,6 +3223,7 @@ class StreamingProcessor {
32233223
32243224 // Update reasoning
32253225 await this.reasoningHandler.process(messageId, mesChanged);
3226+ processedText = chat[messageId]['mes'];
32263227
32273228 // Token count update.
32283229 const tokenCountText = this.reasoningHandler.reasoning + processedText;
@@ -3373,7 +3374,7 @@ class StreamingProcessor {
33733374 this.messageLogprobs.push(...(Array.isArray(logprobs) ? logprobs : [logprobs]));
33743375 }
33753376 // Get the updated reasoning string into the handler
33763377 this.reasoningHandler.updateReasoning(this.messageId, state?.reasoning ?? '');
33773378 await eventSource.emit(event_types.STREAM_TOKEN_RECEIVED, text);
33783379 await sw.tick(async () => await this.onProgressStreaming(this.messageId, this.continueMessage + text));
33793380 }
public/scripts/reasoning.js+66 -11
@@ -13,7 +13,7 @@ import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '
1313import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js';
1414import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
1515import { textgen_types, textgenerationwebui_settings } from './textgen-settings.js';
1616import { copyText, escapeRegex, isFalseBoolean, setDatasetProperty, trimSpaces } from './utils.js';
1717
1818/**
1919 * Gets a message from a jQuery element.
@@ -129,7 +129,12 @@ export const ReasoningState = {
129129 * This class is used inside the {@link StreamingProcessor} to manage reasoning states and UI updates.
130130 */
131131export class ReasoningHandler {
132+ /** @type {boolean} True if the model supports reasoning, but hides the reasoning output */
132133 #isHiddenReasoningModel;
134+ /** @type {boolean} True if the handler is currently handling a manual parse of reasoning blocks */
135+ #isParsingReasoning = false;
136+ /** @type {number?} When reasoning is being parsed manually, and the reasoning has ended, this will be the index at which the actual messages starts */
137+ #parsingReasoningMesStartIndex = null;
133138
134139 /**
135140 * @param {Date?} [timeStarted=null] - When the generation started
@@ -147,7 +152,6 @@ export class ReasoningHandler {
147152 /** @type {Date} Initial starting time of the generation */
148153 this.initialTime = timeStarted ?? new Date();
149154
150- /** @type {boolean} True if the model supports reasoning, but hides the reasoning output */
151155 this.#isHiddenReasoningModel = isHiddenReasoningModel();
152156
153157 // Cached DOM elements for reasoning
@@ -237,18 +241,19 @@ export class ReasoningHandler {
237241 * Updates the reasoning text/string for a message.
238242 *
239243 * @param {number} messageId - The ID of the message to update
240244 * @param {string?} [reasoning=null] - The reasoning text to update - If null or empty, uses the current reasoning
241245 * @param {Object} [options={}] - Optional arguments
242246 * @param {boolean} [options.persist=false] - Whether to persist the reasoning to the message object
247+ * @param {boolean} [options.allowReset=false] - Whether to allow empty reasoning provided to reset the reasoning, instead of just taking the existing one
243248 * @returns {boolean} - Returns true if the reasoning was changed, otherwise false
244249 */
245250 updateReasoning(messageId, reasoning = null, { persist = false, allowReset = false } = {}) {
246251 if (messageId == -1 || !chat[messageId]) {
247252 return false;
248253 }
249254
250255 reasoning = allowReset ? reasoning ?? this.reasoning : reasoning || this.reasoning;
251256 reasoning = power_user.trim_spaces ? reasoning.trimtrimSpaces() : reasoning);
252257
253258 // Ensure the chat extra exists
254259 if (!chat[messageId].extra) {
@@ -279,7 +284,10 @@ export class ReasoningHandler {
279284 * @returns {Promise<void>}
280285 */
281286 async process(messageId, mesChanged) {
282- if (!this.reasoning && !this.#isHiddenReasoningModel) return;
287+ mesChanged = this.#autoParseReasoningFromMessage(messageId, mesChanged);
288+
289+ if (!this.reasoning && !this.#isHiddenReasoningModel)
290+ return;
283291
284292 // Ensure reasoning string is updated and regexes are applied correctly
285293 const reasoningChanged = this.updateReasoning(messageId, null, { persist: true });
@@ -294,6 +302,53 @@ export class ReasoningHandler {
294302 }
295303 }
296304
305+ #autoParseReasoningFromMessage(messageId, mesChanged) {
306+ if (!power_user.reasoning.auto_parse)
307+ return;
308+ if (!power_user.reasoning.prefix || !power_user.reasoning.suffix)
309+ return mesChanged;
310+
311+ /** @type {{ mes: string, [key: string]: any}} */
312+ const message = chat[messageId];
313+ if (!message) return mesChanged;
314+
315+ // If we are done with reasoning parse, we just split the message correctly so the reasoning doesn't show up inside of it.
316+ if (this.#parsingReasoningMesStartIndex) {
317+ message.mes = trimSpaces(message.mes.slice(this.#parsingReasoningMesStartIndex));
318+ return mesChanged;
319+ }
320+
321+ if (this.state === ReasoningState.None) {
322+ // If streamed message starts with the opening, cut it out and put all inside reasoning
323+ if (message.mes.startsWith(power_user.reasoning.prefix) && message.mes.length > power_user.reasoning.prefix.length) {
324+ this.#isParsingReasoning = true;
325+
326+ // Manually set starting state here, as we might already have received the ending suffix
327+ this.state = ReasoningState.Thinking;
328+ this.startTime = this.initialTime;
329+ }
330+ }
331+
332+ if (!this.#isParsingReasoning)
333+ return mesChanged;
334+
335+ // If we are in manual parsing mode, all currently streaming mes tokens will go the the reasoning block
336+ const originalMes = message.mes;
337+ this.reasoning = originalMes.slice(power_user.reasoning.prefix.length);
338+ message.mes = '';
339+
340+ // If the reasoning contains the ending suffix, we cut that off and continue as message streaming
341+ if (this.reasoning.includes(power_user.reasoning.suffix)) {
342+ this.reasoning = this.reasoning.slice(0, this.reasoning.indexOf(power_user.reasoning.suffix));
343+ this.#parsingReasoningMesStartIndex = originalMes.indexOf(power_user.reasoning.suffix) + power_user.reasoning.suffix.length;
344+ message.mes = trimSpaces(originalMes.slice(this.#parsingReasoningMesStartIndex));
345+ this.#isParsingReasoning = false;
346+ }
347+
348+ // Only return the original mesChanged value if we haven't cut off the complete message
349+ return message.mes.length ? mesChanged : false;
350+ }
351+
297352 /**
298353 * Completes the reasoning process for a message.
299354 *
@@ -338,7 +393,7 @@ export class ReasoningHandler {
338393 setDatasetProperty(this.messageReasoningDetailsDom, 'state', this.state);
339394
340395 // Update the reasoning message
341396 const reasoning = power_user.trim_spaces ? this.reasoning.trimtrimSpaces() : this.reasoning);
342397 const displayReasoning = messageFormatting(reasoning, '', false, false, messageId, {}, true);
343398 this.messageReasoningContentDom.innerHTML = displayReasoning;
344399
@@ -838,9 +893,9 @@ function parseReasoningFromString(str) {
838893 return '';
839894 });
840895
841896 if (didReplace && power_user.trim_spaces) {
842897 reasoning = reasoning.trimtrimSpaces(reasoning);
843898 content = content.trimtrimSpaces(content);
844899 }
845900
846901 return { reasoning, content };
public/scripts/utils.js+14 -1
@@ -8,7 +8,7 @@ import {
88import { getContext } from './extensions.js';
99import { characters, getRequestHeaders, this_chid } from '../script.js';
1010import { isMobile } from './RossAscends-mods.js';
1111import { collapseNewlines, power_user } from './power-user.js';
1212import { debounce_timeout } from './constants.js';
1313import { Popup, POPUP_RESULT, POPUP_TYPE } from './popup.js';
1414import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
@@ -677,6 +677,19 @@ export function sortByCssOrder(a, b) {
677677}
678678
679679/**
680+ * Trims leading and trailing whitespace from the input string based on a configuration setting.
681+ * @param {string} input - The string to be trimmed
682+ * @returns {string} The trimmed string if trimming is enabled; otherwise, returns the original string
683+ */
684+
685+export function trimSpaces(input) {
686+ if (!input || typeof input !== 'string') {
687+ return input;
688+ }
689+ return power_user.trim_spaces ? input.trim() : input;
690+}
691+
692+/**
680693 * Trims a string to the end of a nearest sentence.
681694 * @param {string} input The string to trim.
682695 * @returns {string} The trimmed string.