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';
271import { initBulkEdit } from './scripts/bulk-edit.js';271import { initBulkEdit } from './scripts/bulk-edit.js';
272import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';272import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';
273import { getContext } from './scripts/st-context.js';273import { getContext } from './scripts/st-context.js';
274import { extractReasoningFromData, initReasoning, PromptReasoning, ReasoningHandler, removeReasoningFromString, updateReasoningUI } from './scripts/reasoning.js';274import { extractReasoningFromData, initReasoning, parseReasoningInSwipes, PromptReasoning, ReasoningHandler, removeReasoningFromString, updateReasoningUI } from './scripts/reasoning.js';
275import { accountStorage } from './scripts/util/AccountStorage.js';275import { accountStorage } from './scripts/util/AccountStorage.js';
276276
277// API OBJECT FOR EXTERNAL WIRING277// API OBJECT FOR EXTERNAL WIRING
@@ -3346,7 +3346,7 @@ class StreamingProcessor {
33463346
3347 if (Array.isArray(this.swipes) && this.swipes.length > 0) {3347 if (Array.isArray(this.swipes) && this.swipes.length > 0) {
3348 const message = chat[messageId];3348 const message = chat[messageId];
3349 const swipeInfoExtra = structuredClone(message.extra);3349 const swipeInfoExtra = structuredClone(message.extra ?? {});
3350 delete swipeInfoExtra.token_count;3350 delete swipeInfoExtra.token_count;
3351 delete swipeInfoExtra.reasoning;3351 delete swipeInfoExtra.reasoning;
3352 delete swipeInfoExtra.reasoning_duration;3352 delete swipeInfoExtra.reasoning_duration;
@@ -3356,9 +3356,8 @@ class StreamingProcessor {
3356 gen_finished: message.gen_finished,3356 gen_finished: message.gen_finished,
3357 extra: swipeInfoExtra,3357 extra: swipeInfoExtra,
3358 };3358 };
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);
3362 chat[messageId].swipes.push(...this.swipes);3361 chat[messageId].swipes.push(...this.swipes);
3363 chat[messageId].swipe_info.push(...swipeInfoArray);3362 chat[messageId].swipe_info.push(...swipeInfoArray);
3364 }3363 }
@@ -6122,7 +6121,7 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
6122 }6121 }
61236122
6124 if (Array.isArray(swipes) && swipes.length > 0) {6123 if (Array.isArray(swipes) && swipes.length > 0) {
6125 const swipeInfoExtra = structuredClone(item.extra);6124 const swipeInfoExtra = structuredClone(item.extra ?? {});
6126 delete swipeInfoExtra.token_count;6125 delete swipeInfoExtra.token_count;
6127 delete swipeInfoExtra.reasoning;6126 delete swipeInfoExtra.reasoning;
6128 delete swipeInfoExtra.reasoning_duration;6127 delete swipeInfoExtra.reasoning_duration;
@@ -6132,9 +6131,8 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
6132 gen_finished: item.gen_finished,6131 gen_finished: item.gen_finished,
6133 extra: swipeInfoExtra,6132 extra: swipeInfoExtra,
6134 };6133 };
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);
6138 item.swipes.push(...swipes);6136 item.swipes.push(...swipes);
6139 item.swipe_info.push(...swipeInfoArray);6137 item.swipe_info.push(...swipeInfoArray);
6140 }6138 }
public/scripts/reasoning.js+26 -0
@@ -1048,6 +1048,32 @@ function parseReasoningFromString(str, { strict = true } = {}) {
1048 }1048 }
1049}1049}
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 */
1057export 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
1051function registerReasoningAppEvents() {1077function registerReasoningAppEvents() {
1052 const eventHandler = (/** @type {number} */ idx) => {1078 const eventHandler = (/** @type {number} */ idx) => {
1053 if (!power_user.reasoning.auto_parse) {1079 if (!power_user.reasoning.auto_parse) {