mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Parse reasoning in multi-swipe swipes
This commit is contained in:
@ -271,7 +271,7 @@ import { initSettingsSearch } from './scripts/setting-search.js';
|
||||
import { initBulkEdit } from './scripts/bulk-edit.js';
|
||||
import { deriveTemplatesFromChatTemplate } from './scripts/chat-templates.js';
|
||||
import { getContext } from './scripts/st-context.js';
|
||||
import { extractReasoningFromData, initReasoning, PromptReasoning, ReasoningHandler, removeReasoningFromString, updateReasoningUI } from './scripts/reasoning.js';
|
||||
import { extractReasoningFromData, initReasoning, parseReasoningInSwipes, PromptReasoning, ReasoningHandler, removeReasoningFromString, updateReasoningUI } from './scripts/reasoning.js';
|
||||
import { accountStorage } from './scripts/util/AccountStorage.js';
|
||||
|
||||
// API OBJECT FOR EXTERNAL WIRING
|
||||
@ -3346,7 +3346,7 @@ class StreamingProcessor {
|
||||
|
||||
if (Array.isArray(this.swipes) && this.swipes.length > 0) {
|
||||
const message = chat[messageId];
|
||||
const swipeInfoExtra = structuredClone(message.extra);
|
||||
const swipeInfoExtra = structuredClone(message.extra ?? {});
|
||||
delete swipeInfoExtra.token_count;
|
||||
delete swipeInfoExtra.reasoning;
|
||||
delete swipeInfoExtra.reasoning_duration;
|
||||
@ -3356,9 +3356,8 @@ class StreamingProcessor {
|
||||
gen_finished: message.gen_finished,
|
||||
extra: swipeInfoExtra,
|
||||
};
|
||||
const swipeInfoArray = [];
|
||||
swipeInfoArray.length = this.swipes.length;
|
||||
swipeInfoArray.fill(swipeInfo);
|
||||
const swipeInfoArray = Array(this.swipes.length).fill().map(() => structuredClone(swipeInfo));
|
||||
parseReasoningInSwipes(this.swipes, swipeInfoArray, message.extra?.reasoning_duration);
|
||||
chat[messageId].swipes.push(...this.swipes);
|
||||
chat[messageId].swipe_info.push(...swipeInfoArray);
|
||||
}
|
||||
@ -6122,7 +6121,7 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
|
||||
}
|
||||
|
||||
if (Array.isArray(swipes) && swipes.length > 0) {
|
||||
const swipeInfoExtra = structuredClone(item.extra);
|
||||
const swipeInfoExtra = structuredClone(item.extra ?? {});
|
||||
delete swipeInfoExtra.token_count;
|
||||
delete swipeInfoExtra.reasoning;
|
||||
delete swipeInfoExtra.reasoning_duration;
|
||||
@ -6132,9 +6131,8 @@ export async function saveReply(type, getMessage, fromStreaming, title, swipes,
|
||||
gen_finished: item.gen_finished,
|
||||
extra: swipeInfoExtra,
|
||||
};
|
||||
const swipeInfoArray = [];
|
||||
swipeInfoArray.length = swipes.length;
|
||||
swipeInfoArray.fill(swipeInfo, 0, swipes.length);
|
||||
const swipeInfoArray = Array(swipes.length).fill().map(() => structuredClone(swipeInfo));
|
||||
parseReasoningInSwipes(swipes, swipeInfoArray, item.extra?.reasoning_duration);
|
||||
item.swipes.push(...swipes);
|
||||
item.swipe_info.push(...swipeInfoArray);
|
||||
}
|
||||
|
@ -1048,6 +1048,32 @@ function parseReasoningFromString(str, { strict = true } = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse reasoning in an array of swipe strings if auto-parsing is enabled.
|
||||
* @param {string[]} swipes Array of swipe strings
|
||||
* @param {{extra: {reasoning: string, reasoning_duration: number}}[]} swipeInfoArray Array of swipe info objects
|
||||
* @param {number?} duration Duration of the reasoning
|
||||
*/
|
||||
export function parseReasoningInSwipes(swipes, swipeInfoArray, duration) {
|
||||
if (!power_user.reasoning.auto_parse) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Something ain't right, don't parse
|
||||
if (!Array.isArray(swipes) || !Array.isArray(swipeInfoArray) || swipes.length !== swipeInfoArray.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let index = 0; index < swipes.length; index++) {
|
||||
const parsedReasoning = parseReasoningFromString(swipes[index]);
|
||||
if (parsedReasoning) {
|
||||
swipes[index] = parsedReasoning.content;
|
||||
swipeInfoArray[index].extra.reasoning = parsedReasoning.reasoning;
|
||||
swipeInfoArray[index].extra.reasoning_duration = duration;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function registerReasoningAppEvents() {
|
||||
const eventHandler = (/** @type {number} */ idx) => {
|
||||
if (!power_user.reasoning.auto_parse) {
|
||||
|
Reference in New Issue
Block a user