Parse reasoning in multi-swipe swipes

0ea64050ff1e5a49d3a140ff3122bc59872e6574

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

2 files changed, +33 -9Showing whitespace changes
public/script.js+7 -9
@@ -271,7 +271,7 @@ import { initSettingsSearch } from './scripts/setting-search.js';
271271import { initBulkEdit } from './scripts/bulk-edit.js';
272272import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';
273273import { getContext } from './scripts/st-context.js';
274274import { extractReasoningFromData, initReasoning, parseReasoningInSwipes, PromptReasoning, ReasoningHandler, removeReasoningFromString, updateReasoningUI } from './scripts/reasoning.js';
275275import { accountStorage } from './scripts/util/AccountStorage.js';
276276
277277// API OBJECT FOR EXTERNAL WIRING
@@ -3346,7 +3346,7 @@ class StreamingProcessor {
33463346
33473347 if (Array.isArray(this.swipes) && this.swipes.length > 0) {
33483348 const message = chat[messageId];
33493349 const swipeInfoExtra = structuredClone(message.extra ?? {});
33503350 delete swipeInfoExtra.token_count;
33513351 delete swipeInfoExtra.reasoning;
33523352 delete swipeInfoExtra.reasoning_duration;
@@ -3356,9 +3356,8 @@ class StreamingProcessor {
33563356 gen_finished: message.gen_finished,
33573357 extra: swipeInfoExtra,
33583358 };
3359- const swipeInfoArray = [];
3359+ const swipeInfoArray = Array(this.swipes.length).fill().map(() => structuredClone(swipeInfo));
3360- swipeInfoArray.length = this.swipes.length;
3360+ parseReasoningInSwipes(this.swipes, swipeInfoArray, message.extra?.reasoning_duration);
3361- swipeInfoArray.fill(swipeInfo);
33623361 chat[messageId].swipes.push(...this.swipes);
33633362 chat[messageId].swipe_info.push(...swipeInfoArray);
33643363 }
@@ -6122,7 +6121,7 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
61226121 }
61236122
61246123 if (Array.isArray(swipes) && swipes.length > 0) {
61256124 const swipeInfoExtra = structuredClone(item.extra ?? {});
61266125 delete swipeInfoExtra.token_count;
61276126 delete swipeInfoExtra.reasoning;
61286127 delete swipeInfoExtra.reasoning_duration;
@@ -6132,9 +6131,8 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
61326131 gen_finished: item.gen_finished,
61336132 extra: swipeInfoExtra,
61346133 };
6135- const swipeInfoArray = [];
6134+ const swipeInfoArray = Array(swipes.length).fill().map(() => structuredClone(swipeInfo));
6136- swipeInfoArray.length = swipes.length;
6135+ parseReasoningInSwipes(swipes, swipeInfoArray, item.extra?.reasoning_duration);
6137- swipeInfoArray.fill(swipeInfo, 0, swipes.length);
61386136 item.swipes.push(...swipes);
61396137 item.swipe_info.push(...swipeInfoArray);
61406138 }
public/scripts/reasoning.js+26 -0
@@ -1048,6 +1048,32 @@ function parseReasoningFromString(str, { strict = true } = {}) {
10481048 }
10491049}
10501050
1051+/**
1052+ * Parse reasoning in an array of swipe strings if auto-parsing is enabled.
1053+ * @param {string[]} swipes Array of swipe strings
1054+ * @param {{extra: {reasoning: string, reasoning_duration: number}}[]} swipeInfoArray Array of swipe info objects
1055+ * @param {number?} duration Duration of the reasoning
1056+ */
1057+export function parseReasoningInSwipes(swipes, swipeInfoArray, duration) {
1058+ if (!power_user.reasoning.auto_parse) {
1059+ return;
1060+ }
1061+
1062+ // Something ain't right, don't parse
1063+ if (!Array.isArray(swipes) || !Array.isArray(swipeInfoArray) || swipes.length !== swipeInfoArray.length) {
1064+ return;
1065+ }
1066+
1067+ for (let index = 0; index < swipes.length; index++) {
1068+ const parsedReasoning = parseReasoningFromString(swipes[index]);
1069+ if (parsedReasoning) {
1070+ swipes[index] = parsedReasoning.content;
1071+ swipeInfoArray[index].extra.reasoning = parsedReasoning.reasoning;
1072+ swipeInfoArray[index].extra.reasoning_duration = duration;
1073+ }
1074+ }
1075+}
1076+
10511077function registerReasoningAppEvents() {
10521078 const eventHandler = (/** @type {number} */ idx) => {
10531079 if (!power_user.reasoning.auto_parse) {