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