More fixes on reasoning - Fix resetting reasoning on swipes - Fix not updating reasoning time/end on gen when trim spaces was enabled - Fix hidden model checking not working

31f19d0d8ae2386fe016472268d94a11b7dbc0b9

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +32 -17Showing whitespace changes
public/script.js+2 -2
@@ -2506,8 +2506,8 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
25062506 const swipeMessage = chatElement.find(`[mesid="${chat.length - 1}"]`);
25072507 swipeMessage.attr('swipeid', params.swipeId);
25082508 swipeMessage.find('.mes_text').html(messageText).attr('title', title);
2509- swipeMessage.find('.mes_reasoning').html(reasoning);
25102509 swipeMessage.find('.timestamp').text(timestamp).attr('title', `${params.extra.api} - ${params.extra.model}`);
2510+ updateReasoningUI(swipeMessage, { reset: true });
25112511 appendMediaToMessage(mes, swipeMessage);
25122512 if (power_user.timestamp_model_icon && params.extra?.api) {
25132513 insertSVGIcon(swipeMessage, params.extra);
@@ -8722,7 +8722,7 @@ const swipe_right = () => {
87228722 // resets the timer
87238723 swipeMessage.find('.mes_timer').html('');
87248724 swipeMessage.find('.tokenCounterDisplay').text('');
87258725 updateReasoningUI(swipeMessage, { reset: true });
87268726 } else {
87278727 //console.log('showing previously generated swipe candidate, or "..."');
87288728 //console.log('onclick right swipe calling addOneMessage');
public/scripts/reasoning.js+30 -15
@@ -96,11 +96,11 @@ export function isHiddenReasoningModel() {
9696
9797 function isModelSupported(model) {
9898 for (const hiddenReasoningModel of hiddenReasoningModels) {
9999 if (typeof modelhiddenReasoningModel === 'string') {
100100 return hiddenReasoningModel === model;
101101 }
102102 if (modelhiddenReasoningModel.matchingFuncfunc) {
103103 return modelhiddenReasoningModel.matchingFuncfunc(model, hiddenReasoningModel.name);
104104 }
105105 }
106106 return false;
@@ -121,10 +121,12 @@ export function isHiddenReasoningModel() {
121121/**
122122 * Updates the Reasoning UI for a specific message
123123 * @param {number|JQuery<HTMLElement>|HTMLElement} messageIdOrElement The message ID or the message element
124+ * @param {Object} [options={}] - Optional arguments
125+ * @param {boolean} [options.reset=false] - Whether to reset state, and not take the current mess properties (for example when swiping)
124126 */
125127export function updateReasoningUI(messageIdOrElement, { reset = false } = {}) {
126128 const handler = new ReasoningHandler();
127129 handler.initHandleMessage(messageIdOrElement, { reset });
128130}
129131
130132
@@ -185,8 +187,10 @@ export class ReasoningHandler {
185187 * The state will always be either done/hidden or none.
186188 *
187189 * @param {number|JQuery<HTMLElement>|HTMLElement} messageIdOrElement - The message ID or the message element
190+ * @param {Object} [options={}] - Optional arguments
191+ * @param {boolean} [options.reset=false] - Whether to reset state of the handler, and not take the current mess properties (for example when swiping)
188192 */
189- initHandleMessage(messageIdOrElement) {
193+ initHandleMessage(messageIdOrElement, { reset = false } = {}) {
190194 /** @type {HTMLElement} */
191195 const messageElement = typeof messageIdOrElement === 'number'
192196 ? document.querySelector(`#chat [mesid="${messageIdOrElement}"]`)
@@ -197,7 +201,7 @@ export class ReasoningHandler {
197201
198202 if (isNaN(messageId)) return;
199203
200204 const extra = chat[messageId]['.extra'];
201205
202206 if (extra.reasoning) {
203207 this.state = ReasoningState.Done;
@@ -216,6 +220,15 @@ export class ReasoningHandler {
216220 // Prefill main dom element, as message might not have been rendered yet
217221 this.messageDom = messageElement;
218222
223+ // Make sure reset correctly clears all relevant states
224+ if (reset) {
225+ this.state = this.#isHiddenReasoningModel ? ReasoningState.Thinking : ReasoningState.None;
226+ this.reasoning = '';
227+ this.initialTime = new Date();
228+ this.startTime = null;
229+ this.endTime = null;
230+ }
231+
219232 this.updateDom(messageId);
220233 }
221234
@@ -242,19 +255,21 @@ export class ReasoningHandler {
242255 */
243256 updateReasoning(messageId, reasoning = null, { persist = false } = {}) {
244257 reasoning = reasoning ?? this.reasoning;
245258 const reasoningChangedreasoning = thispower_user.trim_spaces ? reasoning.trim() !==: reasoning;
246- this.reasoning = getRegexedString(reasoning ?? '', regex_placement.REASONING);
247259
248- if (persist) {
249260 // Ensure the chat extra exists
250261 if (!chat[messageId]['.extra']) {
251262 chat[messageId]['.extra'] = {};
252263 }
264+ const extra = chat[messageId].extra;
253265
266+ const reasoningChanged = extra.reasoning !== reasoning;
267+ this.reasoning = getRegexedString(reasoning ?? '', regex_placement.REASONING);
268+
269+ if (persist) {
254270 // Build and save the reasoning data to message extras
255- const extra = chat[messageId]['extra'];
271+ extra.reasoning = this.reasoning;
256- extra['reasoning'] = power_user.trim_spaces ? this.reasoning.trim() : this.reasoning;
272+ extra.reasoning_duration = this.getDuration();
257- extra['reasoning_duration'] = this.getDuration();
258273 }
259274
260275 return reasoningChanged;