Refactor reasoning placeholder clean-up
| @@ -3194,10 +3194,6 @@ class StreamingProcessor { | |||
| 3194 | this.#checkDomElements(messageId); | 3194 | this.#checkDomElements(messageId); |
| 3195 | this.#updateMessageBlockVisibility(); | 3195 | this.#updateMessageBlockVisibility(); |
| 3196 | const currentTime = new Date(); | 3196 | const currentTime = new Date(); |
| 3197 | // Don't waste time calculating token count for streaming | ||
| 3198 | const tokenCountText = (this.reasoning || '') + processedText; | ||
| 3199 | const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(tokenCountText, 0) : 0; | ||
| 3200 | const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount); | ||
| 3201 | chat[messageId]['mes'] = processedText; | 3197 | chat[messageId]['mes'] = processedText; |
| 3202 | chat[messageId]['gen_started'] = this.timeStarted; | 3198 | chat[messageId]['gen_started'] = this.timeStarted; |
| 3203 | chat[messageId]['gen_finished'] = currentTime; | 3199 | chat[messageId]['gen_finished'] = currentTime; |
| @@ -3214,6 +3210,10 @@ class StreamingProcessor { | |||
| 3214 | } | 3210 | } |
| 3215 | } | 3211 | } |
| 3216 | 3212 | ||
| 3213 | // Don't waste time calculating token count for streaming | ||
| 3214 | const tokenCountText = (this.reasoning || '') + processedText; | ||
| 3215 | const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(tokenCountText, 0) : 0; | ||
| 3216 | |||
| 3217 | if (currentTokenCount) { | 3217 | if (currentTokenCount) { |
| 3218 | chat[messageId]['extra']['token_count'] = currentTokenCount; | 3218 | chat[messageId]['extra']['token_count'] = currentTokenCount; |
| 3219 | if (this.messageTokenCounterDom instanceof HTMLElement) { | 3219 | if (this.messageTokenCounterDom instanceof HTMLElement) { |
| @@ -3236,10 +3236,13 @@ class StreamingProcessor { | |||
| 3236 | if (this.messageTextDom instanceof HTMLElement) { | 3236 | if (this.messageTextDom instanceof HTMLElement) { |
| 3237 | this.messageTextDom.innerHTML = formattedText; | 3237 | this.messageTextDom.innerHTML = formattedText; |
| 3238 | } | 3238 | } |
| 3239 | |||
| 3240 | const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount); | ||
| 3239 | if (this.messageTimerDom instanceof HTMLElement) { | 3241 | if (this.messageTimerDom instanceof HTMLElement) { |
| 3240 | this.messageTimerDom.textContent = timePassed.timerValue; | 3242 | this.messageTimerDom.textContent = timePassed.timerValue; |
| 3241 | this.messageTimerDom.title = timePassed.timerTitle; | 3243 | this.messageTimerDom.title = timePassed.timerTitle; |
| 3242 | } | 3244 | } |
| 3245 | |||
| 3243 | this.setFirstSwipe(messageId); | 3246 | this.setFirstSwipe(messageId); |
| 3244 | } | 3247 | } |
| 3245 | 3248 | ||
| @@ -27,6 +27,7 @@ function getMessageFromJquery(element) { | |||
| 27 | */ | 27 | */ |
| 28 | export class PromptReasoning { | 28 | export class PromptReasoning { |
| 29 | static REASONING_PLACEHOLDER = '\u200B'; | 29 | static REASONING_PLACEHOLDER = '\u200B'; |
| 30 | static REASONING_PLACEHOLDER_REGEX = new RegExp(`${PromptReasoning.REASONING_PLACEHOLDER}$`); | ||
| 30 | 31 | ||
| 31 | constructor() { | 32 | constructor() { |
| 32 | this.counter = 0; | 33 | this.counter = 0; |
| @@ -121,8 +122,8 @@ function registerReasoningSlashCommands() { | |||
| 121 | callback: (_args, value) => { | 122 | callback: (_args, value) => { |
| 122 | const messageId = !isNaN(Number(value)) ? Number(value) : chat.length - 1; | 123 | const messageId = !isNaN(Number(value)) ? Number(value) : chat.length - 1; |
| 123 | const message = chat[messageId]; | 124 | const message = chat[messageId]; |
| 124 | const reasoning = message?.extra?.reasoning; | 125 | const reasoning = String(message?.extra?.reasoning ?? ''); |
| 125 | return reasoning !== PromptReasoning.REASONING_PLACEHOLDER ? reasoning : ''; | 126 | return reasoning.replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, ''); |
| 126 | }, | 127 | }, |
| 127 | })); | 128 | })); |
| 128 | 129 | ||
| @@ -151,7 +152,7 @@ function registerReasoningSlashCommands() { | |||
| 151 | return ''; | 152 | return ''; |
| 152 | } | 153 | } |
| 153 | 154 | ||
| 154 | message.extra.reasoning = String(value); | 155 | message.extra.reasoning = String(value ?? ''); |
| 155 | await saveChatConditional(); | 156 | await saveChatConditional(); |
| 156 | 157 | ||
| 157 | closeMessageEditor('reasoning'); | 158 | closeMessageEditor('reasoning'); |
| @@ -181,12 +182,12 @@ function setReasoningEventHandlers(){ | |||
| 181 | return; | 182 | return; |
| 182 | } | 183 | } |
| 183 | 184 | ||
| 184 | const reasoning = message?.extra?.reasoning; | 185 | const reasoning = String(message?.extra?.reasoning ?? ''); |
| 185 | const chatElement = document.getElementById('chat'); | 186 | const chatElement = document.getElementById('chat'); |
| 186 | const textarea = document.createElement('textarea'); | 187 | const textarea = document.createElement('textarea'); |
| 187 | const reasoningBlock = messageBlock.find('.mes_reasoning'); | 188 | const reasoningBlock = messageBlock.find('.mes_reasoning'); |
| 188 | textarea.classList.add('reasoning_edit_textarea'); | 189 | textarea.classList.add('reasoning_edit_textarea'); |
| 189 | textarea.value = reasoning === PromptReasoning.REASONING_PLACEHOLDER ? '' : reasoning; | 190 | textarea.value = reasoning.replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, ''); |
| 190 | $(textarea).insertBefore(reasoningBlock); | 191 | $(textarea).insertBefore(reasoningBlock); |
| 191 | 192 | ||
| 192 | if (!CSS.supports('field-sizing', 'content')) { | 193 | if (!CSS.supports('field-sizing', 'content')) { |
| @@ -277,7 +278,7 @@ function setReasoningEventHandlers(){ | |||
| 277 | 278 | ||
| 278 | $(document).on('pointerup', '.mes_reasoning_copy', async function () { | 279 | $(document).on('pointerup', '.mes_reasoning_copy', async function () { |
| 279 | const { message } = getMessageFromJquery(this); | 280 | const { message } = getMessageFromJquery(this); |
| 280 | const reasoning = message?.extra?.reasoning; | 281 | const reasoning = String(message?.extra?.reasoning ?? '').replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, ''); |
| 281 | 282 | ||
| 282 | if (!reasoning) { | 283 | if (!reasoning) { |
| 283 | return; | 284 | return; |