Add reasoning tokens to token count.

bfedf20db52c1e91ab5e141e12a93d2c220b982d

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +16 -8Showing whitespace changes
public/script.js+16 -8
@@ -3195,7 +3195,8 @@ class StreamingProcessor {
31953195 this.#updateMessageBlockVisibility();
31963196 const currentTime = new Date();
31973197 // Don't waste time calculating token count for streaming
3198- const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0;
3198+ const tokenCountText = (this.reasoning || '') + processedText;
3199+ const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(tokenCountText, 0) : 0;
31993200 const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);
32003201 chat[messageId]['mes'] = processedText;
32013202 chat[messageId]['gen_started'] = this.timeStarted;
@@ -5936,7 +5937,8 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
59365937 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
59375938 chat[chat.length - 1]['extra']['reasoning'] = reasoning;
59385939 if (power_user.message_token_count_enabled) {
59395940 chat[chat.lengthconst -tokenCountText 1]['extra'][= (reasoning || 'token_count'] =) await+ getTokenCountAsync(chat[chat.length - 1]['mes'], 0);
5941+ chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
59405942 }
59415943 const chat_id = (chat.length - 1);
59425944 await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id);
@@ -5957,7 +5959,8 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
59575959 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
59585960 chat[chat.length - 1]['extra']['reasoning'] += reasoning;
59595961 if (power_user.message_token_count_enabled) {
59605962 chat[chat.lengthconst -tokenCountText 1]['extra'][= (reasoning || 'token_count'] =) await+ getTokenCountAsync(chat[chat.length - 1]['mes'], 0);
5963+ chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
59615964 }
59625965 const chat_id = (chat.length - 1);
59635966 await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id);
@@ -5975,7 +5978,8 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
59755978 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
59765979 chat[chat.length - 1]['extra']['reasoning'] += reasoning;
59775980 if (power_user.message_token_count_enabled) {
59785981 chat[chat.lengthconst -tokenCountText 1]['extra'][= (reasoning || 'token_count'] =) await+ getTokenCountAsync(chat[chat.length - 1]['mes'], 0);
5982+ chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
59795983 }
59805984 const chat_id = (chat.length - 1);
59815985 await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id);
@@ -6001,7 +6005,8 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
60016005 chat[chat.length - 1]['gen_finished'] = generationFinished;
60026006
60036007 if (power_user.message_token_count_enabled) {
60046008 chat[chat.lengthconst -tokenCountText 1]['extra'][= (reasoning || 'token_count'] =) await+ getTokenCountAsync(chat[chat.length - 1]['mes'], 0);
6009+ chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
60056010 }
60066011
60076012 if (selected_group) {
@@ -8544,7 +8549,8 @@ function swipe_left() { // when we swipe left..but no generation.
85448549 }
85458550
85468551 const swipeMessage = $('#chat').find(`[mesid="${chat.length - 1}"]`);
85478552 const tokenCounttokenCountText = await getTokenCountAsync(chat[chat.length - 1]?.mes,extra?.reasoning 0|| '') + chat[chat.length - 1].mes;
8553+ const tokenCount = await getTokenCountAsync(tokenCountText, 0);
85488554 chat[chat.length - 1]['extra']['token_count'] = tokenCount;
85498555 swipeMessage.find('.tokenCounterDisplay').text(`${tokenCount}t`);
85508556 }
@@ -8719,7 +8725,8 @@ const swipe_right = () => {
87198725 chat[chat.length - 1].extra = {};
87208726 }
87218727
87228728 const tokenCounttokenCountText = await getTokenCountAsync(chat[chat.length - 1]?.mes,extra?.reasoning 0|| '') + chat[chat.length - 1].mes;
8729+ const tokenCount = await getTokenCountAsync(tokenCountText, 0);
87238730 chat[chat.length - 1]['extra']['token_count'] = tokenCount;
87248731 swipeMessage.find('.tokenCounterDisplay').text(`${tokenCount}t`);
87258732 }
@@ -9521,7 +9528,8 @@ function addDebugFunctions() {
95219528 message.extra = {};
95229529 }
95239530
9524- message.extra.token_count = await getTokenCountAsync(message.mes, 0);
9531+ const tokenCountText = (message?.extra?.reasoning || '') + message.mes;
9532+ message.extra.token_count = await getTokenCountAsync(tokenCountText, 0);
95259533 }
95269534
95279535 await saveChatConditional();