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
This commit is contained in:
Wolfsblvt
2025-02-09 08:26:51 +01:00
parent d48db9aded
commit 31f19d0d8a
2 changed files with 33 additions and 18 deletions

View File

@ -2506,8 +2506,8 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
const swipeMessage = chatElement.find(`[mesid="${chat.length - 1}"]`); const swipeMessage = chatElement.find(`[mesid="${chat.length - 1}"]`);
swipeMessage.attr('swipeid', params.swipeId); swipeMessage.attr('swipeid', params.swipeId);
swipeMessage.find('.mes_text').html(messageText).attr('title', title); swipeMessage.find('.mes_text').html(messageText).attr('title', title);
swipeMessage.find('.mes_reasoning').html(reasoning);
swipeMessage.find('.timestamp').text(timestamp).attr('title', `${params.extra.api} - ${params.extra.model}`); swipeMessage.find('.timestamp').text(timestamp).attr('title', `${params.extra.api} - ${params.extra.model}`);
updateReasoningUI(swipeMessage, { reset: true });
appendMediaToMessage(mes, swipeMessage); appendMediaToMessage(mes, swipeMessage);
if (power_user.timestamp_model_icon && params.extra?.api) { if (power_user.timestamp_model_icon && params.extra?.api) {
insertSVGIcon(swipeMessage, params.extra); insertSVGIcon(swipeMessage, params.extra);
@ -8722,7 +8722,7 @@ const swipe_right = () => {
// resets the timer // resets the timer
swipeMessage.find('.mes_timer').html(''); swipeMessage.find('.mes_timer').html('');
swipeMessage.find('.tokenCounterDisplay').text(''); swipeMessage.find('.tokenCounterDisplay').text('');
updateReasoningUI(swipeMessage); updateReasoningUI(swipeMessage, { reset: true });
} else { } else {
//console.log('showing previously generated swipe candidate, or "..."'); //console.log('showing previously generated swipe candidate, or "..."');
//console.log('onclick right swipe calling addOneMessage'); //console.log('onclick right swipe calling addOneMessage');

View File

@ -96,11 +96,11 @@ export function isHiddenReasoningModel() {
function isModelSupported(model) { function isModelSupported(model) {
for (const hiddenReasoningModel of hiddenReasoningModels) { for (const hiddenReasoningModel of hiddenReasoningModels) {
if (typeof model === 'string') { if (typeof hiddenReasoningModel === 'string') {
return hiddenReasoningModel === model; return hiddenReasoningModel === model;
} }
if (model.matchingFunc) { if (hiddenReasoningModel.func) {
return model.matchingFunc(model, hiddenReasoningModel); return hiddenReasoningModel.func(model, hiddenReasoningModel.name);
} }
} }
return false; return false;
@ -121,10 +121,12 @@ export function isHiddenReasoningModel() {
/** /**
* Updates the Reasoning UI for a specific message * Updates the Reasoning UI for a specific message
* @param {number|JQuery<HTMLElement>|HTMLElement} messageIdOrElement The message ID or the message element * @param {number|JQuery<HTMLElement>|HTMLElement} messageIdOrElement The message ID or the message element
* @param {Object} [options={}] - Optional arguments
* @param {boolean} [options.reset=false] - Whether to reset state, and not take the current mess properties (for example when swiping)
*/ */
export function updateReasoningUI(messageIdOrElement) { export function updateReasoningUI(messageIdOrElement, { reset = false } = {}) {
const handler = new ReasoningHandler(); const handler = new ReasoningHandler();
handler.initHandleMessage(messageIdOrElement); handler.initHandleMessage(messageIdOrElement, { reset });
} }
@ -185,8 +187,10 @@ export class ReasoningHandler {
* The state will always be either done/hidden or none. * The state will always be either done/hidden or none.
* *
* @param {number|JQuery<HTMLElement>|HTMLElement} messageIdOrElement - The message ID or the message element * @param {number|JQuery<HTMLElement>|HTMLElement} messageIdOrElement - The message ID or the message element
* @param {Object} [options={}] - Optional arguments
* @param {boolean} [options.reset=false] - Whether to reset state of the handler, and not take the current mess properties (for example when swiping)
*/ */
initHandleMessage(messageIdOrElement) { initHandleMessage(messageIdOrElement, { reset = false } = {}) {
/** @type {HTMLElement} */ /** @type {HTMLElement} */
const messageElement = typeof messageIdOrElement === 'number' const messageElement = typeof messageIdOrElement === 'number'
? document.querySelector(`#chat [mesid="${messageIdOrElement}"]`) ? document.querySelector(`#chat [mesid="${messageIdOrElement}"]`)
@ -197,7 +201,7 @@ export class ReasoningHandler {
if (isNaN(messageId)) return; if (isNaN(messageId)) return;
const extra = chat[messageId]['extra']; const extra = chat[messageId].extra;
if (extra.reasoning) { if (extra.reasoning) {
this.state = ReasoningState.Done; this.state = ReasoningState.Done;
@ -216,6 +220,15 @@ export class ReasoningHandler {
// Prefill main dom element, as message might not have been rendered yet // Prefill main dom element, as message might not have been rendered yet
this.messageDom = messageElement; this.messageDom = messageElement;
// Make sure reset correctly clears all relevant states
if (reset) {
this.state = this.#isHiddenReasoningModel ? ReasoningState.Thinking : ReasoningState.None;
this.reasoning = '';
this.initialTime = new Date();
this.startTime = null;
this.endTime = null;
}
this.updateDom(messageId); this.updateDom(messageId);
} }
@ -242,19 +255,21 @@ export class ReasoningHandler {
*/ */
updateReasoning(messageId, reasoning = null, { persist = false } = {}) { updateReasoning(messageId, reasoning = null, { persist = false } = {}) {
reasoning = reasoning ?? this.reasoning; reasoning = reasoning ?? this.reasoning;
const reasoningChanged = this.reasoning !== reasoning; reasoning = power_user.trim_spaces ? reasoning.trim() : reasoning;
// Ensure the chat extra exists
if (!chat[messageId].extra) {
chat[messageId].extra = {};
}
const extra = chat[messageId].extra;
const reasoningChanged = extra.reasoning !== reasoning;
this.reasoning = getRegexedString(reasoning ?? '', regex_placement.REASONING); this.reasoning = getRegexedString(reasoning ?? '', regex_placement.REASONING);
if (persist) { if (persist) {
// Ensure the chat extra exists
if (!chat[messageId]['extra']) {
chat[messageId]['extra'] = {};
}
// Build and save the reasoning data to message extras // Build and save the reasoning data to message extras
const extra = chat[messageId]['extra']; extra.reasoning = this.reasoning;
extra['reasoning'] = power_user.trim_spaces ? this.reasoning.trim() : this.reasoning; extra.reasoning_duration = this.getDuration();
extra['reasoning_duration'] = this.getDuration();
} }
return reasoningChanged; return reasoningChanged;