Add reasoning auto-parsing, always show reasoning prefix/suffix in Markdown, respect space trim preference

This commit is contained in:
Cohee
2025-01-29 21:38:08 +02:00
parent 15a3cfcb8a
commit d0abba23dc
4 changed files with 117 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import { chat, closeMessageEditor, saveChatConditional, saveSettingsDebounced, substituteParams, updateMessageBlock } from '../script.js';
import { chat, closeMessageEditor, event_types, eventSource, saveChatConditional, saveSettingsDebounced, substituteParams, updateMessageBlock } from '../script.js';
import { getRegexedString, regex_placement } from './extensions/regex/engine.js';
import { t } from './i18n.js';
import { MacrosParser } from './macros.js';
@ -8,7 +8,7 @@ import { SlashCommand } from './slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js';
import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
import { copyText } from './utils.js';
import { copyText, escapeRegex } from './utils.js';
/**
* Gets a message from a jQuery element.
@ -106,6 +106,12 @@ function loadReasoningSettings() {
power_user.reasoning.max_additions = Number($(this).val());
saveSettingsDebounced();
});
$('#reasoning_auto_parse').prop('checked', power_user.reasoning.auto_parse);
$('#reasoning_auto_parse').on('change', function () {
power_user.reasoning.auto_parse = !!$(this).prop('checked');
saveSettingsDebounced();
});
}
function registerReasoningSlashCommands() {
@ -290,9 +296,94 @@ function setReasoningEventHandlers(){
});
}
/**
* Parses reasoning from a string using the power user reasoning settings.
* @typedef {Object} ParsedReasoning
* @property {string} reasoning Reasoning block
* @property {string} content Message content
* @param {string} str Content of the message
* @returns {ParsedReasoning|null} Parsed reasoning block and message content
*/
function parseReasoningFromString(str) {
// Both prefix and suffix must be defined
if (!power_user.reasoning.prefix || !power_user.reasoning.suffix) {
return null;
}
try {
let reasoning = '';
const regex = new RegExp(`${escapeRegex(power_user.reasoning.prefix)}(.*?)${escapeRegex(power_user.reasoning.suffix)}`, 's');
const content = String(str).replace(regex, (_match, captureGroup) => {
reasoning = power_user.trim_spaces ? captureGroup.trim() : captureGroup;
return '';
});
return { reasoning, content };
} catch (error) {
console.error('[Reasoning] Error parsing reasoning block', error);
return null;
}
}
function registerReasoningAppEvents() {
eventSource.makeFirst(event_types.MESSAGE_RECEIVED, (/** @type {number} */ idx) => {
if (!power_user.reasoning.auto_parse) {
return;
}
console.debug('[Reasoning] Auto-parsing reasoning block for message', idx);
const message = chat[idx];
if (!message) {
console.warn('[Reasoning] Message not found', idx);
return null;
}
if (!message.mes || message.mes === '...') {
console.debug('[Reasoning] Message content is empty or a placeholder', idx);
return null;
}
const parsedReasoning = parseReasoningFromString(message.mes);
// No reasoning block found
if (!parsedReasoning) {
return;
}
// Make sure the message has an extra object
if (!message.extra || typeof message.extra !== 'object') {
message.extra = {};
}
const contentUpdated = !!parsedReasoning.reasoning || parsedReasoning.content !== message.mes;
// If reasoning was found, add it to the message
if (parsedReasoning.reasoning) {
message.extra.reasoning = parsedReasoning.reasoning;
}
// Update the message text if it was changed
if (parsedReasoning.content !== message.mes) {
message.mes = parsedReasoning.content;
}
// Find if a message already exists in DOM and must be updated
if (contentUpdated) {
const messageRendered = document.querySelector(`.mes[mesid="${idx}"]`) !== null;
if (messageRendered) {
console.debug('[Reasoning] Updating message block', idx);
updateMessageBlock(idx, message);
}
}
});
}
export function initReasoning() {
loadReasoningSettings();
setReasoningEventHandlers();
registerReasoningSlashCommands();
registerReasoningMacros();
registerReasoningAppEvents();
}