Merge pull request #3345 from SillyTavern/woo-yeah Better reasoning

3b8fd6f62f1afc410cb451acbe7e52dfc85724e1

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

Signed
15 files changed, +616 -75Showing whitespace changes
public/index.html+51 -6
@@ -1977,11 +1977,11 @@
19771977 </span>
19781978 </div>
19791979 </div>
19801980 <div class="range-block" data-source="makersuite,deepseek,openrouter">
19811981 <label for="openai_show_thoughts" class="checkbox_label widthFreeExpand">
19821982 <input id="openai_show_thoughts" type="checkbox" />
19831983 <span>
19841984 <span data-i18n="Show model thoughtsreasoning">Show model thoughtsreasoning</span>
19851985 <i class="opacity50p fa-solid fa-circle-info" title="Gemini 2.0 Thinking / DeepSeek Reasoner"></i>
19861986 </span>
19871987 </label>
@@ -3800,6 +3800,39 @@
38003800 </div>
38013801 </div>
38023802 <div>
3803+ <h4 class="standoutHeader">
3804+ <span data-i18n="Reasoning">Reasoning</span>
3805+ </h4>
3806+ <div>
3807+ <label class="checkbox_label" for="reasoning_add_to_prompts" title="Add existing reasoning blocks to prompts. To add a new reasoning block, use the message edit menu." data-i18n="[title]reasoning_add_to_prompts">
3808+ <input id="reasoning_add_to_prompts" type="checkbox" />
3809+ <small data-i18n="Add Reasoning to Prompts">
3810+ Add Reasoning to Prompts
3811+ </small>
3812+ </label>
3813+ <div class="flex-container">
3814+ <div class="flex1" title="Inserted before the reasoning content." data-i18n="[title]reasoning_prefix">
3815+ <small data-i18n="Prefix">Prefix</small>
3816+ <textarea id="reasoning_prefix" class="text_pole textarea_compact autoSetHeight"></textarea>
3817+ </div>
3818+ <div class="flex1" title="Inserted after the reasoning content." data-i18n="[title]reasoning_suffix">
3819+ <small data-i18n="Suffix">Suffix</small>
3820+ <textarea id="reasoning_suffix" class="text_pole textarea_compact autoSetHeight"></textarea>
3821+ </div>
3822+ </div>
3823+ <div class="flex-container">
3824+ <div class="flex1" title="Inserted between the reasoning and the message content." data-i18n="[title]reasoning_separator">
3825+ <small data-i18n="Separator">Separator</small>
3826+ <textarea id="reasoning_separator" class="text_pole textarea_compact autoSetHeight"></textarea>
3827+ </div>
3828+ <div class="flex1" title="Maximum number of reasoning blocks to be added per prompt, counting from the last message." data-i18n="[title]reasoning_max_additions">
3829+ <small data-i18n="Max Additions">Max Additions</small>
3830+ <input id="reasoning_max_additions" class="text_pole textarea_compact" type="number" min="0" max="999"></textarea>
3831+ </div>
3832+ </div>
3833+ </div>
3834+ </div>
3835+ <div>
38033836 <h4 class="standoutHeader" data-i18n="Miscellaneous">Miscellaneous</h4>
38043837 <div>
38053838 <small>
@@ -6221,14 +6254,26 @@
62216254 <div class="mes_edit_buttons">
62226255 <div class="mes_edit_done menu_button fa-solid fa-check" title="Confirm" data-i18n="[title]Confirm"></div>
62236256 <div class="mes_edit_copy menu_button fa-solid fa-copy" title="Copy this message" data-i18n="[title]Copy this message"></div>
62246257 <div class="mes_edit_deletemes_edit_add_reasoning menu_button fa-solid fa-trash-canlightbulb" title="DeleteAdd thisa messagereasoning block" data-i18n="[title]DeleteAdd thisa messagereasoning block"></div>
6225- </div>
6258+ <div class="mes_edit_delete menu_button fa-solid fa-trash-can" title="Delete this message" data-i18n="[title]Delete this message"></div>
62266259 <div class="mes_edit_up menu_button fa-solid fa-chevron-up " title="Move message up" data-i18n="[title]Move message up"></div>
62276260 <div class="mes_edit_down menu_button fa-solid fa-chevron-down" title="Move message down" data-i18n="[title]Move message down"></div>
6228- </div>
62296261 <div class="mes_edit_cancel menu_button fa-solid fa-xmark" title="Cancel" data-i18n="[title]Cancel"></div>
62306262 </div>
62316263 </div>
6264+ <details class="mes_reasoning_details">
6265+ <summary class="mes_reasoning_summary">
6266+ <span data-i18n="Reasoning">Reasoning</span>
6267+ <div class="mes_reasoning_actions">
6268+ <div class="mes_reasoning_edit_done mes_button fa-solid fa-check" title="Confirm" data-i18n="[title]Confirmedit"></div>
6269+ <div class="mes_reasoning_edit_cancel mes_button fa-solid fa-xmark" title="Cancel edit" data-i18n="[title]Cancel edit"></div>
6270+ <div class="mes_reasoning_edit mes_button fa-solid fa-pencil" title="Edit reasoning" data-i18n="[title]Edit reasoning"></div>
6271+ <div class="mes_reasoning_copy mes_button fa-solid fa-copy" title="Copy reasoning" data-i18n="[title]Copy reasoning"></div>
6272+ <div class="mes_reasoning_delete mes_button fa-solid fa-trash-can" title="Remove reasoning" data-i18n="[title]Remove reasoning"></div>
6273+ </div>
6274+ </summary>
6275+ <div class="mes_reasoning"></div>
6276+ </details>
62326277 <div class="mes_text"></div>
62336278 <div class="mes_img_container">
62346279 <div class="mes_img_controls">
public/locales/fr-fr.json+1 -1
@@ -1385,7 +1385,7 @@
13851385 "enable_functions_desc_1": "Autorise l'utilisation",
13861386 "enable_functions_desc_2": "outils de fonction",
13871387 "enable_functions_desc_3": "Peut être utilisé par diverses extensions pour fournir des fonctionnalités supplémentaires.",
13881388 "Show model thoughtsreasoning": "Afficher les pensées du modèle",
13891389 "Display the model's internal thoughts in the response.": "Afficher les pensées internes du modèle dans la réponse.",
13901390 "Confirm token parsing with": "Confirmer l'analyse des tokens avec",
13911391 "openai_logit_bias_no_items": "Aucun élément",
public/locales/zh-cn.json+1 -1
@@ -266,7 +266,7 @@
266266 "Use system prompt": "使用系统提示词",
267267 "Merges_all_system_messages_desc_1": "合并所有系统消息,直到第一条具有非系统角色的消息,然后通过",
268268 "Merges_all_system_messages_desc_2": "字段发送。",
269269 "Show model thoughtsreasoning": "展示思维链",
270270 "Display the model's internal thoughts in the response.": "展示模型在回复时的内部思维链。",
271271 "Assistant Prefill": "AI预填",
272272 "Expand the editor": "展开编辑器",
public/locales/zh-tw.json+1 -1
@@ -2357,7 +2357,7 @@
23572357 "Forbid": "禁止",
23582358 "Aphrodite only. Determines the order of samplers. Skew is always applied post-softmax, so it's not included here.": "僅限 Aphrodite 使用。決定採樣器的順序。偏移總是在 softmax 後應用,因此不包括在此。",
23592359 "Aphrodite only. Determines the order of samplers.": "僅限 Aphrodite 使用。決定採樣器的順序。",
23602360 "Show model thoughtsreasoning": "顯示模型思維鏈",
23612361 "Display the model's internal thoughts in the response.": "在回應中顯示模型的思維鏈(內部思考過程)。",
23622362 "Generic (OpenAI-compatible) [LM Studio, LiteLLM, etc.]": "通用(兼容 OpenAI)[LM Studio, LiteLLM 等]",
23632363 "Model ID (optional)": "模型 ID(可選)",
public/script.js+100 -26
@@ -267,6 +267,7 @@ import { initSettingsSearch } from './scripts/setting-search.js';
267267import { initBulkEdit } from './scripts/bulk-edit.js';
268268import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';
269269import { getContext } from './scripts/st-context.js';
270+import { initReasoning, PromptReasoning } from './scripts/reasoning.js';
270271
271272// API OBJECT FOR EXTERNAL WIRING
272273globalThis.SillyTavern = {
@@ -982,6 +983,7 @@ async function firstLoadInit() {
982983 initServerHistory();
983984 initSettingsSearch();
984985 initBulkEdit();
986+ initReasoning();
985987 await initScrapers();
986988 doDailyExtensionUpdatesCheck();
987989 await hideLoader();
@@ -2200,6 +2202,7 @@ function getMessageFromTemplate({
22002202 isUser,
22012203 avatarImg,
22022204 bias,
2205+ reasoning,
22032206 isSystem,
22042207 title,
22052208 timerValue,
@@ -2224,6 +2227,7 @@ function getMessageFromTemplate({
22242227 mes.find('.avatar img').attr('src', avatarImg);
22252228 mes.find('.ch_name .name_text').text(characterName);
22262229 mes.find('.mes_bias').html(bias);
2230+ mes.find('.mes_reasoning').html(reasoning);
22272231 mes.find('.timestamp').text(timestamp).attr('title', `${extra?.api ? extra.api + ' - ' : ''}${extra?.model ?? ''}`);
22282232 mes.find('.mesIDDisplay').text(`#${mesId}`);
22292233 tokenCount && mes.find('.tokenCounterDisplay').text(`${tokenCount}t`);
@@ -2238,10 +2242,16 @@ function getMessageFromTemplate({
22382242 return mes;
22392243}
22402244
2245+/**
2246+ * Re-renders a message block with updated content.
2247+ * @param {number} messageId Message ID
2248+ * @param {object} message Message object
2249+ */
22412250export function updateMessageBlock(messageId, message) {
22422251 const messageElement = $(`#chat [mesid="${messageId}"]`);
22432252 const text = message?.extra?.display_text ?? message.mes;
22442253 messageElement.find('.mes_text').html(messageFormatting(text, message.name, message.is_system, message.is_user, messageId));
2254+ messageElement.find('.mes_reasoning').html(messageFormatting(message.extra?.reasoning ?? '', '', false, false, -1));
22452255 addCopyToCodeBlocks(messageElement);
22462256 appendMediaToMessage(message, messageElement);
22472257}
@@ -2400,6 +2410,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
24002410 sanitizerOverrides,
24012411 );
24022412 const bias = messageFormatting(mes.extra?.bias ?? '', '', false, false, -1);
2413+ const reasoning = messageFormatting(mes.extra?.reasoning ?? '', '', false, false, -1);
24032414 let bookmarkLink = mes?.extra?.bookmark_link ?? '';
24042415
24052416 let params = {
@@ -2409,6 +2420,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
24092420 isUser: mes.is_user,
24102421 avatarImg: avatarImg,
24112422 bias: bias,
2423+ reasoning: reasoning,
24122424 isSystem: isSystem,
24132425 title: title,
24142426 bookmarkLink: bookmarkLink,
@@ -2468,6 +2480,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
24682480 const swipeMessage = chatElement.find(`[mesid="${chat.length - 1}"]`);
24692481 swipeMessage.attr('swipeid', params.swipeId);
24702482 swipeMessage.find('.mes_text').html(messageText).attr('title', title);
2483+ swipeMessage.find('.mes_reasoning').html(reasoning);
24712484 swipeMessage.find('.timestamp').text(timestamp).attr('title', `${params.extra.api} - ${params.extra.model}`);
24722485 appendMediaToMessage(mes, swipeMessage);
24732486 if (power_user.timestamp_model_icon && params.extra?.api) {
@@ -3078,6 +3091,7 @@ class StreamingProcessor {
30783091 this.messageTextDom = null;
30793092 this.messageTimerDom = null;
30803093 this.messageTokenCounterDom = null;
3094+ this.messageReasoningDom = null;
30813095 /** @type {HTMLTextAreaElement} */
30823096 this.sendTextarea = document.querySelector('#send_textarea');
30833097 this.type = type;
@@ -3093,6 +3107,7 @@ class StreamingProcessor {
30933107 /** @type {import('./scripts/logprobs.js').TokenLogprobs[]} */
30943108 this.messageLogprobs = [];
30953109 this.toolCalls = [];
3110+ this.reasoning = '';
30963111 }
30973112
30983113 #checkDomElements(messageId) {
@@ -3101,6 +3116,7 @@ class StreamingProcessor {
31013116 this.messageTextDom = this.messageDom?.querySelector('.mes_text');
31023117 this.messageTimerDom = this.messageDom?.querySelector('.mes_timer');
31033118 this.messageTokenCounterDom = this.messageDom?.querySelector('.tokenCounterDisplay');
3119+ this.messageReasoningDom = this.messageDom?.querySelector('.mes_reasoning');
31043120 }
31053121 }
31063122
@@ -3178,18 +3194,27 @@ class StreamingProcessor {
31783194 this.#checkDomElements(messageId);
31793195 this.#updateMessageBlockVisibility();
31803196 const currentTime = new Date();
3181- // Don't waste time calculating token count for streaming
3182- const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0;
3183- const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);
31843197 chat[messageId]['mes'] = processedText;
31853198 chat[messageId]['gen_started'] = this.timeStarted;
31863199 chat[messageId]['gen_finished'] = currentTime;
31873200
3188- if (currentTokenCount) {
31893201 if (!chat[messageId]['extra']) {
31903202 chat[messageId]['extra'] = {};
31913203 }
31923204
3205+ if (this.reasoning) {
3206+ chat[messageId]['extra']['reasoning'] = this.reasoning;
3207+ if (this.messageReasoningDom instanceof HTMLElement) {
3208+ const formattedReasoning = messageFormatting(this.reasoning, '', false, false, -1);
3209+ this.messageReasoningDom.innerHTML = formattedReasoning;
3210+ }
3211+ }
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) {
31933218 chat[messageId]['extra']['token_count'] = currentTokenCount;
31943219 if (this.messageTokenCounterDom instanceof HTMLElement) {
31953220 this.messageTokenCounterDom.textContent = `${currentTokenCount}t`;
@@ -3211,10 +3236,13 @@ class StreamingProcessor {
32113236 if (this.messageTextDom instanceof HTMLElement) {
32123237 this.messageTextDom.innerHTML = formattedText;
32133238 }
3239+
3240+ const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);
32143241 if (this.messageTimerDom instanceof HTMLElement) {
32153242 this.messageTimerDom.textContent = timePassed.timerValue;
32163243 this.messageTimerDom.title = timePassed.timerTitle;
32173244 }
3245+
32183246 this.setFirstSwipe(messageId);
32193247 }
32203248
@@ -3321,7 +3349,7 @@ class StreamingProcessor {
33213349 }
33223350
33233351 /**
33243352 * @returns {Generator<{ text: string, swipes: string[], logprobs: import('./scripts/logprobs.js').TokenLogprobs, toolCalls: any[], state: any }, void, void>}
33253353 */
33263354 *nullStreamingGeneration() {
33273355 throw new Error('Generation function for streaming is not hooked up');
@@ -3343,7 +3371,7 @@ class StreamingProcessor {
33433371 try {
33443372 const sw = new Stopwatch(1000 / power_user.streaming_fps);
33453373 const timestamps = [];
33463374 for await (const { text, swipes, logprobs, toolCalls, state } of this.generator()) {
33473375 timestamps.push(Date.now());
33483376 if (this.isStopped) {
33493377 return;
@@ -3355,6 +3383,7 @@ class StreamingProcessor {
33553383 if (logprobs) {
33563384 this.messageLogprobs.push(...(Array.isArray(logprobs) ? logprobs : [logprobs]));
33573385 }
3386+ this.reasoning = state?.reasoning ?? '';
33583387 await eventSource.emit(event_types.STREAM_TOKEN_RECEIVED, text);
33593388 await sw.tick(() => this.onProgressStreaming(this.messageId, this.continueMessage + text));
33603389 }
@@ -3821,6 +3850,14 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
38213850 coreChat.pop();
38223851 }
38233852
3853+ const reasoning = new PromptReasoning();
3854+ for (let i = coreChat.length - 1; i >= 0; i--) {
3855+ if (reasoning.isLimitReached()) {
3856+ break;
3857+ }
3858+ coreChat[i] = { ...coreChat[i], mes: reasoning.addToMessage(coreChat[i].mes, coreChat[i].extra?.reasoning) };
3859+ }
3860+
38243861 coreChat = await Promise.all(coreChat.map(async (chatItem, index) => {
38253862 let message = chatItem.mes;
38263863 let regexType = chatItem.is_user ? regex_placement.USER_INPUT : regex_placement.AI_OUTPUT;
@@ -4742,6 +4779,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
47424779 //const getData = await response.json();
47434780 let getMessage = extractMessageFromData(data);
47444781 let title = extractTitleFromData(data);
4782+ let reasoning = extractReasoningFromData(data);
47454783 kobold_horde_model = title;
47464784
47474785 const swipes = extractMultiSwipes(data, type);
@@ -4768,10 +4806,10 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
47684806 else {
47694807 // Without streaming we'll be having a full message on continuation. Treat it as a last chunk.
47704808 if (originalType !== 'continue') {
47714809 ({ type, getMessage } = await saveReply(type, getMessage, false, title, swipes, reasoning));
47724810 }
47734811 else {
47744812 ({ type, getMessage } = await saveReply('appendFinal', getMessage, false, title, swipes, reasoning));
47754813 }
47764814
47774815 // This relies on `saveReply` having been called to add the message to the chat, so it must be last.
@@ -5659,7 +5697,6 @@ function extractMessageFromData(data) {
56595697 return data;
56605698 }
56615699
5662- function getTextContext() {
56635700 switch (main_api) {
56645701 case 'kobold':
56655702 return data.results[0].text;
@@ -5676,14 +5713,24 @@ function extractMessageFromData(data) {
56765713 }
56775714}
56785715
5679- const content = getTextContext();
5716+/**
5680-
5717+ * Extracts the reasoning from the response data.
5681- if (main_api === 'openai' && oai_settings.chat_completion_source === chat_completion_sources.DEEPSEEK && oai_settings.show_thoughts) {
5718+ * @param {object} data Response data
5682- const thoughts = data?.choices?.[0]?.message?.reasoning_content ?? '';
5719+ * @returns {string} Extracted reasoning
5683- return [thoughts, content].filter(x => x).join('\n\n');
5720+ */
5721+function extractReasoningFromData(data) {
5722+ if (main_api === 'openai' && oai_settings.show_thoughts) {
5723+ switch (oai_settings.chat_completion_source) {
5724+ case chat_completion_sources.DEEPSEEK:
5725+ return data?.choices?.[0]?.message?.reasoning_content ?? '';
5726+ case chat_completion_sources.OPENROUTER:
5727+ return data?.choices?.[0]?.message?.reasoning ?? '';
5728+ case chat_completion_sources.MAKERSUITE:
5729+ return data?.responseContent?.parts?.filter(part => part.thought)?.map(part => part.text)?.join('\n\n') ?? '';
5730+ }
56845731 }
56855732
56865733 return content'';
56875734}
56885735
56895736/**
@@ -5866,7 +5913,7 @@ export function cleanUpMessage(getMessage, isImpersonate, isContinue, displayInc
58665913 return getMessage;
58675914}
58685915
58695916export async function saveReply(type, getMessage, fromStreaming, title, swipes, reasoning) {
58705917 if (type != 'append' && type != 'continue' && type != 'appendFinal' && chat.length && (chat[chat.length - 1]['swipe_id'] === undefined ||
58715918 chat[chat.length - 1]['is_user'])) {
58725919 type = 'normal';
@@ -5891,8 +5938,10 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes)
58915938 chat[chat.length - 1]['send_date'] = getMessageTimeStamp();
58925939 chat[chat.length - 1]['extra']['api'] = getGeneratingApi();
58935940 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
5941+ chat[chat.length - 1]['extra']['reasoning'] = reasoning;
58945942 if (power_user.message_token_count_enabled) {
58955943 chat[chat.lengthconst -tokenCountText 1]['extra'][= (reasoning || 'token_count'] =) await+ getTokenCountAsync(chat[chat.length - 1]['mes'], 0);
5944+ chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
58965945 }
58975946 const chat_id = (chat.length - 1);
58985947 await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id);
@@ -5911,8 +5960,10 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes)
59115960 chat[chat.length - 1]['send_date'] = getMessageTimeStamp();
59125961 chat[chat.length - 1]['extra']['api'] = getGeneratingApi();
59135962 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
5963+ chat[chat.length - 1]['extra']['reasoning'] += reasoning;
59145964 if (power_user.message_token_count_enabled) {
59155965 chat[chat.lengthconst -tokenCountText 1]['extra'][= (reasoning || 'token_count'] =) await+ getTokenCountAsync(chat[chat.length - 1]['mes'], 0);
5966+ chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
59165967 }
59175968 const chat_id = (chat.length - 1);
59185969 await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id);
@@ -5928,8 +5979,10 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes)
59285979 chat[chat.length - 1]['send_date'] = getMessageTimeStamp();
59295980 chat[chat.length - 1]['extra']['api'] = getGeneratingApi();
59305981 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
5982+ chat[chat.length - 1]['extra']['reasoning'] += reasoning;
59315983 if (power_user.message_token_count_enabled) {
59325984 chat[chat.lengthconst -tokenCountText 1]['extra'][= (reasoning || 'token_count'] =) await+ getTokenCountAsync(chat[chat.length - 1]['mes'], 0);
5985+ chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
59335986 }
59345987 const chat_id = (chat.length - 1);
59355988 await eventSource.emit(event_types.MESSAGE_RECEIVED, chat_id);
@@ -5945,6 +5998,7 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes)
59455998 chat[chat.length - 1]['send_date'] = getMessageTimeStamp();
59465999 chat[chat.length - 1]['extra']['api'] = getGeneratingApi();
59476000 chat[chat.length - 1]['extra']['model'] = getGeneratingModel();
6001+ chat[chat.length - 1]['extra']['reasoning'] = reasoning;
59486002 if (power_user.trim_spaces) {
59496003 getMessage = getMessage.trim();
59506004 }
@@ -5954,7 +6008,8 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes)
59546008 chat[chat.length - 1]['gen_finished'] = generationFinished;
59556009
59566010 if (power_user.message_token_count_enabled) {
59576011 chat[chat.lengthconst -tokenCountText 1]['extra'][= (reasoning || 'token_count'] =) await+ getTokenCountAsync(chat[chat.length - 1]['mes'], 0);
6012+ chat[chat.length - 1]['extra']['token_count'] = await getTokenCountAsync(tokenCountText, 0);
59586013 }
59596014
59606015 if (selected_group) {
@@ -7997,11 +8052,25 @@ function updateEditArrowClasses() {
79978052 }
79988053}
79998054
8000-function closeMessageEditor() {
8055+/**
8056+ * Closes the message editor.
8057+ * @param {'message'|'reasoning'|'all'} what What to close. Default is 'all'.
8058+ */
8059+export function closeMessageEditor(what = 'all') {
8060+ if (what === 'message' || what === 'all') {
80018061 if (this_edit_mes_id) {
80028062 $(`#chat .mes[mesid="${this_edit_mes_id}"] .mes_edit_cancel`).click();
80038063 }
80048064 }
8065+ if (what === 'reasoning' || what === 'all') {
8066+ document.querySelectorAll('.reasoning_edit_textarea').forEach((el) => {
8067+ const cancelButton = el.closest('.mes')?.querySelector('.mes_reasoning_edit_cancel');
8068+ if (cancelButton instanceof HTMLElement) {
8069+ cancelButton.click();
8070+ }
8071+ });
8072+ }
8073+}
80058074
80068075export function setGenerationProgress(progress) {
80078076 if (!progress) {
@@ -8483,7 +8552,8 @@ function swipe_left() { // when we swipe left..but no generation.
84838552 }
84848553
84858554 const swipeMessage = $('#chat').find(`[mesid="${chat.length - 1}"]`);
84868555 const tokenCounttokenCountText = await getTokenCountAsync(chat[chat.length - 1]?.mes,extra?.reasoning 0|| '') + chat[chat.length - 1].mes;
8556+ const tokenCount = await getTokenCountAsync(tokenCountText, 0);
84878557 chat[chat.length - 1]['extra']['token_count'] = tokenCount;
84888558 swipeMessage.find('.tokenCounterDisplay').text(`${tokenCount}t`);
84898559 }
@@ -8647,6 +8717,7 @@ const swipe_right = () => {
86478717 // resets the timer
86488718 swipeMessage.find('.mes_timer').html('');
86498719 swipeMessage.find('.tokenCounterDisplay').text('');
8720+ swipeMessage.find('.mes_reasoning').html('');
86508721 } else {
86518722 //console.log('showing previously generated swipe candidate, or "..."');
86528723 //console.log('onclick right swipe calling addOneMessage');
@@ -8657,7 +8728,8 @@ const swipe_right = () => {
86578728 chat[chat.length - 1].extra = {};
86588729 }
86598730
86608731 const tokenCounttokenCountText = await getTokenCountAsync(chat[chat.length - 1]?.mes,extra?.reasoning 0|| '') + chat[chat.length - 1].mes;
8732+ const tokenCount = await getTokenCountAsync(tokenCountText, 0);
86618733 chat[chat.length - 1]['extra']['token_count'] = tokenCount;
86628734 swipeMessage.find('.tokenCounterDisplay').text(`${tokenCount}t`);
86638735 }
@@ -9459,7 +9531,8 @@ function addDebugFunctions() {
94599531 message.extra = {};
94609532 }
94619533
9462- message.extra.token_count = await getTokenCountAsync(message.mes, 0);
9534+ const tokenCountText = (message?.extra?.reasoning || '') + message.mes;
9535+ message.extra.token_count = await getTokenCountAsync(tokenCountText, 0);
94639536 }
94649537
94659538 await saveChatConditional();
@@ -11228,14 +11301,15 @@ jQuery(async function () {
1122811301
1122911302 $(document).keyup(function (e) {
1123011303 if (e.key === 'Escape') {
1123111304 const isEditVisible = $('#curEditTextarea').is(':visible') || $('.reasoning_edit_textarea').length > 0;
1123211305 if (isEditVisible && power_user.auto_save_msg_edits === false) {
1123311306 closeMessageEditor('all');
1123411307 $('#send_textarea').focus();
1123511308 return;
1123611309 }
1123711310 if (isEditVisible && power_user.auto_save_msg_edits === true) {
1123811311 $(`#chat .mes[mesid="${this_edit_mes_id}"] .mes_edit_done`).click();
11312+ closeMessageEditor('reasoning');
1123911313 $('#send_textarea').focus();
1124011314 return;
1124111315 }
public/scripts/kai-settings.js+1 -1
@@ -188,7 +188,7 @@ export async function generateKoboldWithStreaming(generate_data, signal) {
188188 if (data?.token) {
189189 text += data.token;
190190 }
191191 yield { text, swipes: [], toolCalls: [], state: {} };
192192 }
193193 };
194194}
public/scripts/nai-settings.js+1 -1
@@ -746,7 +746,7 @@ export async function generateNovelWithStreaming(generate_data, signal) {
746746 text += data.token;
747747 }
748748
749749 yield { text, swipes: [], logprobs: parseNovelAILogprobs(data.logprobs), toolCalls: [], state: {} };
750750 }
751751 };
752752}
public/scripts/openai.js+15 -9
@@ -2095,7 +2095,7 @@ async function sendOpenAIRequest(type, messages, signal) {
20952095 let text = '';
20962096 const swipes = [];
20972097 const toolCalls = [];
20982098 const state = { reasoning: '' };
20992099 while (true) {
21002100 const { done, value } = await reader.read();
21012101 if (done) return;
@@ -2113,7 +2113,7 @@ async function sendOpenAIRequest(type, messages, signal) {
21132113
21142114 ToolManager.parseToolCalls(toolCalls, parsed);
21152115
21162116 yield { text, swipes: swipes, logprobs: parseChatCompletionLogprobs(parsed), toolCalls: toolCalls, state: state };
21172117 }
21182118 };
21192119 }
@@ -2150,16 +2150,22 @@ function getStreamingReply(data, state) {
21502150 if (oai_settings.chat_completion_source === chat_completion_sources.CLAUDE) {
21512151 return data?.delta?.text || '';
21522152 } else if (oai_settings.chat_completion_source === chat_completion_sources.MAKERSUITE) {
2153- return data?.candidates?.[0]?.content?.parts?.filter(x => oai_settings.show_thoughts || !x.thought)?.map(x => x.text)?.filter(x => x)?.join('\n\n') || '';
2153+ if (oai_settings.show_thoughts) {
2154+ state.reasoning += (data?.candidates?.[0]?.content?.parts?.filter(x => x.thought)?.map(x => x.text)?.[0] || '');
2155+ }
2156+ return data?.candidates?.[0]?.content?.parts?.filter(x => !x.thought)?.map(x => x.text)?.[0] || '';
21542157 } else if (oai_settings.chat_completion_source === chat_completion_sources.COHERE) {
21552158 return data?.delta?.message?.content?.text || data?.delta?.message?.tool_plan || '';
21562159 } else if (oai_settings.chat_completion_source === chat_completion_sources.DEEPSEEK) {
2157- const hadThoughts = state.hadThoughts;
2160+ if (oai_settings.show_thoughts) {
21582161 const thoughts state.reasoning += (data.choices?.filter(x => oai_settings.show_thoughts || !x?.delta?.reasoning_content)?.[0]?.delta?.reasoning_content || '');
2159- const content = data.choices?.[0]?.delta?.content || '';
2162+ }
2160- state.hadThoughts = !!thoughts;
2163+ return data.choices?.[0]?.delta?.content || '';
2161- const separator = hadThoughts && !thoughts ? '\n\n' : '';
2164+ } else if (oai_settings.chat_completion_source === chat_completion_sources.OPENROUTER) {
2162- return [thoughts, separator, content].filter(x => x).join('\n\n');
2165+ if (oai_settings.show_thoughts) {
2166+ state.reasoning += (data.choices?.filter(x => x?.delta?.reasoning)?.[0]?.delta?.reasoning || '');
2167+ }
2168+ return data.choices?.[0]?.delta?.content ?? data.choices?.[0]?.message?.content ?? data.choices?.[0]?.text ?? '';
21632169 } else {
21642170 return data.choices?.[0]?.delta?.content ?? data.choices?.[0]?.message?.content ?? data.choices?.[0]?.text ?? '';
21652171 }
public/scripts/power-user.js+8 -0
@@ -253,6 +253,14 @@ let power_user = {
253253 content: 'Write {{char}}\'s next reply in a fictional chat between {{char}} and {{user}}.',
254254 },
255255
256+ reasoning: {
257+ add_to_prompts: false,
258+ prefix: '<think>\n',
259+ suffix: '\n</think>',
260+ separator: '\n\n',
261+ max_additions: 1,
262+ },
263+
256264 personas: {},
257265 default_persona: null,
258266 persona_descriptions: {},
public/scripts/reasoning.js+297 -0
@@ -0,0 +1,297 @@
1+import { chat, closeMessageEditor, saveChatConditional, saveSettingsDebounced, substituteParams, updateMessageBlock } from '../script.js';
2+import { t } from './i18n.js';
3+import { MacrosParser } from './macros.js';
4+import { Popup } from './popup.js';
5+import { power_user } from './power-user.js';
6+import { SlashCommand } from './slash-commands/SlashCommand.js';
7+import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
8+import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js';
9+import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
10+import { copyText } from './utils.js';
11+
12+/**
13+ * Gets a message from a jQuery element.
14+ * @param {Element} element
15+ * @returns {{messageId: number, message: object, messageBlock: JQuery<HTMLElement>}}
16+ */
17+function getMessageFromJquery(element) {
18+ const messageBlock = $(element).closest('.mes');
19+ const messageId = Number(messageBlock.attr('mesid'));
20+ const message = chat[messageId];
21+ return { messageId: messageId, message, messageBlock };
22+}
23+
24+/**
25+ * Helper class for adding reasoning to messages.
26+ * Keeps track of the number of reasoning additions.
27+ */
28+export class PromptReasoning {
29+ static REASONING_PLACEHOLDER = '\u200B';
30+ static REASONING_PLACEHOLDER_REGEX = new RegExp(`${PromptReasoning.REASONING_PLACEHOLDER}$`);
31+
32+ constructor() {
33+ this.counter = 0;
34+ }
35+
36+ /**
37+ * Checks if the limit of reasoning additions has been reached.
38+ * @returns {boolean} True if the limit of reasoning additions has been reached, false otherwise.
39+ */
40+ isLimitReached() {
41+ if (!power_user.reasoning.add_to_prompts) {
42+ return true;
43+ }
44+
45+ return this.counter >= power_user.reasoning.max_additions;
46+ }
47+
48+ /**
49+ * Add reasoning to a message according to the power user settings.
50+ * @param {string} content Message content
51+ * @param {string} reasoning Message reasoning
52+ * @returns {string} Message content with reasoning
53+ */
54+ addToMessage(content, reasoning) {
55+ // Disabled or reached limit of additions
56+ if (!power_user.reasoning.add_to_prompts || this.counter >= power_user.reasoning.max_additions) {
57+ return content;
58+ }
59+
60+ // No reasoning provided or a placeholder
61+ if (!reasoning || reasoning === PromptReasoning.REASONING_PLACEHOLDER) {
62+ return content;
63+ }
64+
65+ // Increment the counter
66+ this.counter++;
67+
68+ // Substitute macros in variable parts
69+ const prefix = substituteParams(power_user.reasoning.prefix || '');
70+ const separator = substituteParams(power_user.reasoning.separator || '');
71+ const suffix = substituteParams(power_user.reasoning.suffix || '');
72+
73+ // Combine parts with reasoning and content
74+ return `${prefix}${reasoning}${suffix}${separator}${content}`;
75+ }
76+}
77+
78+function loadReasoningSettings() {
79+ $('#reasoning_add_to_prompts').prop('checked', power_user.reasoning.add_to_prompts);
80+ $('#reasoning_add_to_prompts').on('change', function () {
81+ power_user.reasoning.add_to_prompts = !!$(this).prop('checked');
82+ saveSettingsDebounced();
83+ });
84+
85+ $('#reasoning_prefix').val(power_user.reasoning.prefix);
86+ $('#reasoning_prefix').on('input', function () {
87+ power_user.reasoning.prefix = String($(this).val());
88+ saveSettingsDebounced();
89+ });
90+
91+ $('#reasoning_suffix').val(power_user.reasoning.suffix);
92+ $('#reasoning_suffix').on('input', function () {
93+ power_user.reasoning.suffix = String($(this).val());
94+ saveSettingsDebounced();
95+ });
96+
97+ $('#reasoning_separator').val(power_user.reasoning.separator);
98+ $('#reasoning_separator').on('input', function () {
99+ power_user.reasoning.separator = String($(this).val());
100+ saveSettingsDebounced();
101+ });
102+
103+ $('#reasoning_max_additions').val(power_user.reasoning.max_additions);
104+ $('#reasoning_max_additions').on('input', function () {
105+ power_user.reasoning.max_additions = Number($(this).val());
106+ saveSettingsDebounced();
107+ });
108+}
109+
110+function registerReasoningSlashCommands() {
111+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
112+ name: 'reasoning-get',
113+ returns: ARGUMENT_TYPE.STRING,
114+ helpString: t`Get the contents of a reasoning block of a message. Returns an empty string if the message does not have a reasoning block.`,
115+ unnamedArgumentList: [
116+ SlashCommandArgument.fromProps({
117+ description: 'Message ID. If not provided, the message ID of the last message is used.',
118+ typeList: ARGUMENT_TYPE.NUMBER,
119+ enumProvider: commonEnumProviders.messages(),
120+ }),
121+ ],
122+ callback: (_args, value) => {
123+ const messageId = !isNaN(Number(value)) ? Number(value) : chat.length - 1;
124+ const message = chat[messageId];
125+ const reasoning = String(message?.extra?.reasoning ?? '');
126+ return reasoning.replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, '');
127+ },
128+ }));
129+
130+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
131+ name: 'reasoning-set',
132+ returns: ARGUMENT_TYPE.STRING,
133+ helpString: t`Set the reasoning block of a message. Returns the reasoning block content.`,
134+ namedArgumentList: [
135+ SlashCommandNamedArgument.fromProps({
136+ name: 'at',
137+ description: 'Message ID. If not provided, the message ID of the last message is used.',
138+ typeList: ARGUMENT_TYPE.NUMBER,
139+ enumProvider: commonEnumProviders.messages(),
140+ }),
141+ ],
142+ unnamedArgumentList: [
143+ SlashCommandArgument.fromProps({
144+ description: 'Reasoning block content.',
145+ typeList: ARGUMENT_TYPE.STRING,
146+ }),
147+ ],
148+ callback: async (args, value) => {
149+ const messageId = !isNaN(Number(args[0])) ? Number(args[0]) : chat.length - 1;
150+ const message = chat[messageId];
151+ if (!message?.extra) {
152+ return '';
153+ }
154+
155+ message.extra.reasoning = String(value ?? '');
156+ await saveChatConditional();
157+
158+ closeMessageEditor('reasoning');
159+ updateMessageBlock(messageId, message);
160+ return message.extra.reasoning;
161+ },
162+ }));
163+}
164+
165+function registerReasoningMacros() {
166+ MacrosParser.registerMacro('reasoningPrefix', () => power_user.reasoning.prefix, t`Reasoning Prefix`);
167+ MacrosParser.registerMacro('reasoningSuffix', () => power_user.reasoning.suffix, t`Reasoning Suffix`);
168+ MacrosParser.registerMacro('reasoningSeparator', () => power_user.reasoning.separator, t`Reasoning Separator`);
169+}
170+
171+function setReasoningEventHandlers(){
172+ $(document).on('click', '.mes_reasoning_copy', (e) => {
173+ e.stopPropagation();
174+ e.preventDefault();
175+ });
176+
177+ $(document).on('click', '.mes_reasoning_edit', function (e) {
178+ e.stopPropagation();
179+ e.preventDefault();
180+ const { message, messageBlock } = getMessageFromJquery(this);
181+ if (!message?.extra) {
182+ return;
183+ }
184+
185+ const reasoning = String(message?.extra?.reasoning ?? '');
186+ const chatElement = document.getElementById('chat');
187+ const textarea = document.createElement('textarea');
188+ const reasoningBlock = messageBlock.find('.mes_reasoning');
189+ textarea.classList.add('reasoning_edit_textarea');
190+ textarea.value = reasoning.replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, '');
191+ $(textarea).insertBefore(reasoningBlock);
192+
193+ if (!CSS.supports('field-sizing', 'content')) {
194+ const resetHeight = function () {
195+ const scrollTop = chatElement.scrollTop;
196+ textarea.style.height = '0px';
197+ textarea.style.height = `${textarea.scrollHeight}px`;
198+ chatElement.scrollTop = scrollTop;
199+ };
200+
201+ textarea.addEventListener('input', resetHeight);
202+ resetHeight();
203+ }
204+
205+ textarea.focus();
206+ textarea.setSelectionRange(textarea.value.length, textarea.value.length);
207+
208+ const textareaRect = textarea.getBoundingClientRect();
209+ const chatRect = chatElement.getBoundingClientRect();
210+
211+ // Scroll if textarea bottom is below visible area
212+ if (textareaRect.bottom > chatRect.bottom) {
213+ const scrollOffset = textareaRect.bottom - chatRect.bottom;
214+ chatElement.scrollTop += scrollOffset;
215+ }
216+ });
217+
218+ $(document).on('click', '.mes_reasoning_edit_done', async function (e) {
219+ e.stopPropagation();
220+ e.preventDefault();
221+ const { message, messageId, messageBlock } = getMessageFromJquery(this);
222+ if (!message?.extra) {
223+ return;
224+ }
225+
226+ const textarea = messageBlock.find('.reasoning_edit_textarea');
227+ const reasoning = String(textarea.val());
228+ message.extra.reasoning = reasoning;
229+ await saveChatConditional();
230+ updateMessageBlock(messageId, message);
231+ textarea.remove();
232+ });
233+
234+ $(document).on('click', '.mes_reasoning_edit_cancel', function (e) {
235+ e.stopPropagation();
236+ e.preventDefault();
237+
238+ const { messageBlock } = getMessageFromJquery(this);
239+ const textarea = messageBlock.find('.reasoning_edit_textarea');
240+ textarea.remove();
241+ });
242+
243+ $(document).on('click', '.mes_edit_add_reasoning', async function () {
244+ const { message, messageId } = getMessageFromJquery(this);
245+ if (!message?.extra) {
246+ return;
247+ }
248+
249+ if (message.extra.reasoning) {
250+ toastr.info(t`Reasoning already exists.`, t`Edit Message`);
251+ return;
252+ }
253+
254+ message.extra.reasoning = PromptReasoning.REASONING_PLACEHOLDER;
255+ await saveChatConditional();
256+ closeMessageEditor();
257+ updateMessageBlock(messageId, message);
258+ });
259+
260+ $(document).on('click', '.mes_reasoning_delete', async function (e) {
261+ e.stopPropagation();
262+ e.preventDefault();
263+
264+ const confirm = await Popup.show.confirm(t`Are you sure you want to clear the reasoning?`, t`Visible message contents will stay intact.`);
265+
266+ if (!confirm) {
267+ return;
268+ }
269+
270+ const { message, messageId } = getMessageFromJquery(this);
271+ if (!message?.extra) {
272+ return;
273+ }
274+ message.extra.reasoning = '';
275+ await saveChatConditional();
276+ updateMessageBlock(messageId, message);
277+ });
278+
279+ $(document).on('pointerup', '.mes_reasoning_copy', async function () {
280+ const { message } = getMessageFromJquery(this);
281+ const reasoning = String(message?.extra?.reasoning ?? '').replace(PromptReasoning.REASONING_PLACEHOLDER_REGEX, '');
282+
283+ if (!reasoning) {
284+ return;
285+ }
286+
287+ await copyText(reasoning);
288+ toastr.info(t`Copied!`, '', { timeOut: 2000 });
289+ });
290+}
291+
292+export function initReasoning() {
293+ loadReasoningSettings();
294+ setReasoningEventHandlers();
295+ registerReasoningSlashCommands();
296+ registerReasoningMacros();
297+}
public/scripts/sse-stream.js+15 -0
@@ -235,6 +235,21 @@ async function* parseStreamData(json) {
235235 }
236236 return;
237237 }
238+ else if (typeof json.choices[0].delta.reasoning === 'string' && json.choices[0].delta.reasoning.length > 0) {
239+ for (let j = 0; j < json.choices[0].delta.reasoning.length; j++) {
240+ const str = json.choices[0].delta.reasoning[j];
241+ const isLastSymbol = j === json.choices[0].delta.reasoning.length - 1;
242+ const choiceClone = structuredClone(json.choices[0]);
243+ choiceClone.delta.reasoning = str;
244+ choiceClone.delta.content = isLastSymbol ? choiceClone.delta.content : '';
245+ const choices = [choiceClone];
246+ yield {
247+ data: { ...json, choices },
248+ chunk: str,
249+ };
250+ }
251+ return;
252+ }
238253 else if (typeof json.choices[0].delta.content === 'string' && json.choices[0].delta.content.length > 0) {
239254 for (let j = 0; j < json.choices[0].delta.content.length; j++) {
240255 const str = json.choices[0].delta.content[j];
public/scripts/st-context.js+4 -0
@@ -1,6 +1,7 @@
11import {
22 activateSendButtons,
33 addOneMessage,
4+ appendMediaToMessage,
45 callPopup,
56 characters,
67 chat,
@@ -41,6 +42,7 @@ import {
4142 substituteParamsExtended,
4243 this_chid,
4344 updateChatMetadata,
45+ updateMessageBlock,
4446} from '../script.js';
4547import {
4648 extension_settings,
@@ -171,6 +173,8 @@ export function getContext() {
171173 getCharacters,
172174 uuidv4,
173175 humanizedDateTime,
176+ updateMessageBlock,
177+ appendMediaToMessage,
174178 };
175179}
176180
public/scripts/textgen-settings.js+2 -1
@@ -986,6 +986,7 @@ export async function generateTextGenWithStreaming(generate_data, signal) {
986986 let logprobs = null;
987987 const swipes = [];
988988 const toolCalls = [];
989+ const state = {};
989990 while (true) {
990991 const { done, value } = await reader.read();
991992 if (done) return;
@@ -1004,7 +1005,7 @@ export async function generateTextGenWithStreaming(generate_data, signal) {
10041005 logprobs = parseTextgenLogprobs(newText, data.choices?.[0]?.logprobs || data?.completion_probabilities);
10051006 }
10061007
10071008 yield { text, swipes, logprobs, toolCalls, state };
10081009 }
10091010 };
10101011}
public/style.css+106 -21
@@ -292,36 +292,44 @@ input[type='checkbox']:focus-visible {
292292 filter: grayscale(25%);
293293}
294294
295295.mes_text table {,
296+.mes_reasoning table {
296297 border-spacing: 0;
297298 border-collapse: collapse;
298299 margin-bottom: 10px;
299300}
300301
301302.mes_text td,
302303.mes_text th {,
304+.mes_reasoning td,
305+.mes_reasoning th {
303306 border: 1px solid;
304307 border-collapse: collapse;
305308 padding: 0.25em;
306309}
307310
308311.mes_text p {,
312+.mes_reasoning p {
309313 margin-top: 0;
310314 margin-bottom: 10px;
311315}
312316
313317.mes_text li tt {,
318+.mes_reasoning li tt {
314319 display: inline-block;
315320}
316321
317322.mes_text ol,
318323.mes_text ul {,
324+.mes_reasoning ol,
325+.mes_reasoning ul {
319326 margin-top: 5px;
320327 margin-bottom: 5px;
321328}
322329
323330.mes_text br,
324331.mes_bias br {,
332+.mes_reasoning br {
325333 content: ' ';
326334}
327335
@@ -332,25 +340,83 @@ input[type='checkbox']:focus-visible {
332340 color: var(--SmartThemeQuoteColor);
333341}
334342
343+.mes_reasoning {
344+ display: block;
345+ border: 1px solid var(--SmartThemeBorderColor);
346+ background-color: var(--black30a);
347+ border-radius: 5px;
348+ padding: 5px;
349+ margin: 5px 0;
350+ overflow-y: auto;
351+}
352+
353+.mes_reasoning_summary {
354+ cursor: pointer;
355+ position: relative;
356+ margin: 2px;
357+}
358+
359+@supports not selector(:has(*)) {
360+ .mes_reasoning_details {
361+ display: none !important;
362+ }
363+}
364+
365+.mes_bias:empty,
366+.mes_reasoning:empty,
367+.mes_reasoning_details:has(.mes_reasoning:empty),
368+.mes_block:has(.edit_textarea) .mes_reasoning_details,
369+.mes_reasoning_details:not([open]) .mes_reasoning_actions,
370+.mes_reasoning_details:has(.reasoning_edit_textarea) .mes_reasoning,
371+.mes_reasoning_details:not(:has(.reasoning_edit_textarea)) .mes_reasoning_actions .mes_button.mes_reasoning_edit_done,
372+.mes_reasoning_details:not(:has(.reasoning_edit_textarea)) .mes_reasoning_actions .mes_button.mes_reasoning_edit_cancel,
373+.mes_reasoning_details:has(.reasoning_edit_textarea) .mes_reasoning_actions .mes_button:not(.mes_reasoning_edit_done, .mes_reasoning_edit_cancel) {
374+ display: none;
375+}
376+
377+.mes_reasoning_actions {
378+ position: absolute;
379+ right: 0;
380+ top: 0;
381+
382+ display: flex;
383+ gap: 4px;
384+ flex-wrap: nowrap;
385+ justify-content: flex-end;
386+ transition: all 200ms;
387+ overflow-x: hidden;
388+ padding: 1px;
389+}
390+
391+.mes_reasoning_summary>span {
392+ margin-left: 0.5em;
393+}
394+
335395.mes_text i,
336396.mes_text em {,
397+.mes_reasoning i,
398+.mes_reasoning em {
337399 color: var(--SmartThemeEmColor);
338400}
339401
340402.mes_text u {,
403+.mes_reasoning u {
341404 color: var(--SmartThemeUnderlineColor);
342405}
343406
344407.mes_text q {,
408+.mes_reasoning q {
345409 color: var(--SmartThemeQuoteColor);
346410}
347411
348412.mes_text font[color] em,
349413.mes_text font[color] i {,
350- color: inherit;
414+.mes_text font[color] u,
351-}
415+.mes_text font[color] q,
352-
416+.mes_reasoning font[color] em,
353417.mes_textmes_reasoning font[color] q {i,
418+.mes_reasoning font[color] u,
419+.mes_reasoning font[color] q {
354420 color: inherit;
355421}
356422
@@ -358,7 +424,8 @@ input[type='checkbox']:focus-visible {
358424 display: block;
359425}
360426
361427.mes_text blockquote {,
428+.mes_reasoning blockquote {
362429 border-left: 3px solid var(--SmartThemeQuoteColor);
363430 padding-left: 10px;
364431 background-color: var(--black30a);
@@ -368,18 +435,24 @@ input[type='checkbox']:focus-visible {
368435.mes_text strong em,
369436.mes_text strong,
370437.mes_text h2,
371438.mes_text h1 {,
439+.mes_reasoning strong em,
440+.mes_reasoning strong,
441+.mes_reasoning h2,
442+.mes_reasoning h1 {
372443 font-weight: bold;
373444}
374445
375446.mes_text pre code {,
447+.mes_reasoning pre code {
376448 position: relative;
377449 display: block;
378450 overflow-x: auto;
379451 padding: 1em;
380452}
381453
382454.mes_text img:not(.mes_img) {,
455+.mes_reasoning img:not(.mes_img) {
383456 max-width: 100%;
384457 max-height: var(--doc-height);
385458}
@@ -1022,6 +1095,11 @@ body .panelControlBar {
10221095 /*only affects bubblechat to make it sit nicely at the bottom*/
10231096}
10241097
1098+.last_mes:has(.mes_text:empty):has(.mes_reasoning_details[open]) .mes_reasoning:not(:empty) {
1099+ margin-bottom: 30px;
1100+}
1101+
1102+.last_mes .mes_reasoning,
10251103.last_mes .mes_text {
10261104 padding-right: 30px;
10271105}
@@ -1235,14 +1313,18 @@ body.swipeAllMessages .mes:not(.last_mes) .swipes-counter {
12351313 overflow-y: clip;
12361314}
12371315
12381316.mes_text {,
1317+.mes_reasoning {
12391318 font-weight: 500;
12401319 line-height: calc(var(--mainFontSize) + .5rem);
1320+ max-width: 100%;
1321+ overflow-wrap: anywhere;
1322+}
1323+
1324+.mes_text {
12411325 padding-left: 0;
12421326 padding-top: 5px;
12431327 padding-bottom: 5px;
1244- max-width: 100%;
1245- overflow-wrap: anywhere;
12461328}
12471329
12481330br {
@@ -4150,10 +4232,12 @@ input[type="range"]::-webkit-slider-thumb {
41504232 align-items: center;
41514233}
41524234
4235+.mes_reasoning_edit_cancel,
41534236.mes_edit_cancel.menu_button {
41544237 background-color: var(--crimson70a);
41554238}
41564239
4240+.mes_reasoning_edit_done,
41574241.mes_edit_done.menu_button {
41584242 background-color: var(--okGreen70a);
41594243}
@@ -4162,6 +4246,7 @@ input[type="range"]::-webkit-slider-thumb {
41624246 opacity: 1;
41634247}
41644248
4249+.reasoning_edit_textarea,
41654250.edit_textarea {
41664251 padding: 5px;
41674252 margin: 0;
src/endpoints/backends/chat-completions.js+13 -7
@@ -289,6 +289,7 @@ async function sendMakerSuiteRequest(request, response) {
289289 const model = String(request.body.model);
290290 const stream = Boolean(request.body.stream);
291291 const showThoughts = Boolean(request.body.show_thoughts);
292+ const isThinking = model.includes('thinking');
292293
293294 const generationConfig = {
294295 stopSequences: request.body.stop,
@@ -329,6 +330,12 @@ async function sendMakerSuiteRequest(request, response) {
329330 body.systemInstruction = prompt.system_instruction;
330331 }
331332
333+ if (isThinking && showThoughts) {
334+ generationConfig.thinkingConfig = {
335+ includeThoughts: true,
336+ };
337+ }
338+
332339 return body;
333340 }
334341
@@ -342,7 +349,6 @@ async function sendMakerSuiteRequest(request, response) {
342349 controller.abort();
343350 });
344351
345- const isThinking = model.includes('thinking');
346352 const apiVersion = isThinking ? 'v1alpha' : 'v1beta';
347353 const responseType = (stream ? 'streamGenerateContent' : 'generateContent');
348354
@@ -387,11 +393,7 @@ async function sendMakerSuiteRequest(request, response) {
387393 const responseContent = candidates[0].content ?? candidates[0].output;
388394 console.log('Google AI Studio response:', responseContent);
389395
390- if (Array.isArray(responseContent?.parts) && isThinking && !showThoughts) {
396+ const responseText = typeof responseContent === 'string' ? responseContent : responseContent?.parts?.filter(part => !part.thought)?.map(part => part.text)?.join('\n\n');
391- responseContent.parts = responseContent.parts.filter(part => !part.thought);
392- }
393-
394- const responseText = typeof responseContent === 'string' ? responseContent : responseContent?.parts?.map(part => part.text)?.join('\n\n');
395397 if (!responseText) {
396398 let message = 'Google AI Studio Candidate text empty';
397399 console.log(message, generateResponseJson);
@@ -399,7 +401,7 @@ async function sendMakerSuiteRequest(request, response) {
399401 }
400402
401403 // Wrap it back to OAI format
402404 const reply = { choices: [{ 'message': { 'content': responseText } }], responseContent };
403405 return response.send(reply);
404406 }
405407 } catch (error) {
@@ -996,6 +998,10 @@ router.post('/generate', jsonParser, function (request, response) {
996998 bodyParams['route'] = 'fallback';
997999 }
9981000
1001+ if (request.body.show_thoughts) {
1002+ bodyParams['include_reasoning'] = true;
1003+ }
1004+
9991005 let cachingAtDepth = getConfigValue('claude.cachingAtDepth', -1);
10001006 if (Number.isInteger(cachingAtDepth) && cachingAtDepth >= 0 && request.body.model?.startsWith('anthropic/claude-3')) {
10011007 cachingAtDepthForOpenRouterClaude(request.body.messages, cachingAtDepth);