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.#updateMessageBlockVisibility();
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]['gen_started'] = this.timeStarted;
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) {
chat[messageId]['extra']['token_count'] = currentTokenCount;
if (this.messageTokenCounterDom instanceof HTMLElement) {
@@ -3236,10 +3236,13 @@ class StreamingProcessor {
if (this.messageTextDom instanceof HTMLElement) {
this.messageTextDom.innerHTML = formattedText;
}
const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);
if (this.messageTimerDom instanceof HTMLElement) {
this.messageTimerDom.textContent = timePassed.timerValue;
this.messageTimerDom.title = timePassed.timerTitle;
}
this.setFirstSwipe(messageId);
}

View File

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