Refactor reasoning placeholder clean-up

This commit is contained in:
Cohee
2025-01-27 21:56:15 +02:00
parent a5a8f6057b
commit 8b5e0df2d7
2 changed files with 14 additions and 10 deletions

View File

@@ -3194,10 +3194,6 @@ class StreamingProcessor {
this.#checkDomElements(messageId); this.#checkDomElements(messageId);
this.#updateMessageBlockVisibility(); this.#updateMessageBlockVisibility();
const currentTime = new Date(); const currentTime = new Date();
// Don't waste time calculating token count for streaming
const tokenCountText = (this.reasoning || '') + processedText;
const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(tokenCountText, 0) : 0;
const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);
chat[messageId]['mes'] = processedText; chat[messageId]['mes'] = processedText;
chat[messageId]['gen_started'] = this.timeStarted; chat[messageId]['gen_started'] = this.timeStarted;
chat[messageId]['gen_finished'] = currentTime; chat[messageId]['gen_finished'] = currentTime;
@@ -3214,6 +3210,10 @@ class StreamingProcessor {
} }
} }
// Don't waste time calculating token count for streaming
const tokenCountText = (this.reasoning || '') + processedText;
const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(tokenCountText, 0) : 0;
if (currentTokenCount) { if (currentTokenCount) {
chat[messageId]['extra']['token_count'] = currentTokenCount; chat[messageId]['extra']['token_count'] = currentTokenCount;
if (this.messageTokenCounterDom instanceof HTMLElement) { if (this.messageTokenCounterDom instanceof HTMLElement) {
@@ -3236,10 +3236,13 @@ class StreamingProcessor {
if (this.messageTextDom instanceof HTMLElement) { if (this.messageTextDom instanceof HTMLElement) {
this.messageTextDom.innerHTML = formattedText; this.messageTextDom.innerHTML = formattedText;
} }
const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);
if (this.messageTimerDom instanceof HTMLElement) { if (this.messageTimerDom instanceof HTMLElement) {
this.messageTimerDom.textContent = timePassed.timerValue; this.messageTimerDom.textContent = timePassed.timerValue;
this.messageTimerDom.title = timePassed.timerTitle; this.messageTimerDom.title = timePassed.timerTitle;
} }
this.setFirstSwipe(messageId); this.setFirstSwipe(messageId);
} }

View File

@@ -27,6 +27,7 @@ function getMessageFromJquery(element) {
*/ */
export class PromptReasoning { export class PromptReasoning {
static REASONING_PLACEHOLDER = '\u200B'; static REASONING_PLACEHOLDER = '\u200B';
static REASONING_PLACEHOLDER_REGEX = new RegExp(`${PromptReasoning.REASONING_PLACEHOLDER}$`);
constructor() { constructor() {
this.counter = 0; this.counter = 0;
@@ -121,8 +122,8 @@ function registerReasoningSlashCommands() {
callback: (_args, value) => { callback: (_args, value) => {
const messageId = !isNaN(Number(value)) ? Number(value) : chat.length - 1; const messageId = !isNaN(Number(value)) ? Number(value) : chat.length - 1;
const message = chat[messageId]; const message = chat[messageId];
const reasoning = message?.extra?.reasoning; const reasoning = String(message?.extra?.reasoning ?? '');
return reasoning !== PromptReasoning.REASONING_PLACEHOLDER ? reasoning : ''; return reasoning.replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, '');
}, },
})); }));
@@ -151,7 +152,7 @@ function registerReasoningSlashCommands() {
return ''; return '';
} }
message.extra.reasoning = String(value); message.extra.reasoning = String(value ?? '');
await saveChatConditional(); await saveChatConditional();
closeMessageEditor('reasoning'); closeMessageEditor('reasoning');
@@ -181,12 +182,12 @@ function setReasoningEventHandlers(){
return; return;
} }
const reasoning = message?.extra?.reasoning; const reasoning = String(message?.extra?.reasoning ?? '');
const chatElement = document.getElementById('chat'); const chatElement = document.getElementById('chat');
const textarea = document.createElement('textarea'); const textarea = document.createElement('textarea');
const reasoningBlock = messageBlock.find('.mes_reasoning'); const reasoningBlock = messageBlock.find('.mes_reasoning');
textarea.classList.add('reasoning_edit_textarea'); textarea.classList.add('reasoning_edit_textarea');
textarea.value = reasoning === PromptReasoning.REASONING_PLACEHOLDER ? '' : reasoning; textarea.value = reasoning.replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, '');
$(textarea).insertBefore(reasoningBlock); $(textarea).insertBefore(reasoningBlock);
if (!CSS.supports('field-sizing', 'content')) { if (!CSS.supports('field-sizing', 'content')) {
@@ -277,7 +278,7 @@ function setReasoningEventHandlers(){
$(document).on('pointerup', '.mes_reasoning_copy', async function () { $(document).on('pointerup', '.mes_reasoning_copy', async function () {
const { message } = getMessageFromJquery(this); const { message } = getMessageFromJquery(this);
const reasoning = message?.extra?.reasoning; const reasoning = String(message?.extra?.reasoning ?? '').replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, '');
if (!reasoning) { if (!reasoning) {
return; return;