Merge pull request #3538 from SillyTavern/reasoning-parse-on-edit Reasoning edit/parse updates
Signed| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | import { |
| 2 | 2 | moment, |
| 3 | 3 | } from '../lib.js'; |
| 4 | 4 | import { chat, closeMessageEditor, event_types, eventSource, main_api, messageFormatting, saveChatConditional, saveChatDebounced, saveSettingsDebounced, substituteParams, updateMessageBlock } from '../script.js'; |
| 5 | 5 | import { getRegexedString, regex_placement } from './extensions/regex/engine.js'; |
| 6 | 6 | import { getCurrentLocale, t, translate } from './i18n.js'; |
| 7 | 7 | import { MacrosParser } from './macros.js'; |
| @@ -755,6 +755,17 @@ function registerReasoningMacros() { | ||
| 755 | 755 | } |
| 756 | 756 | |
| 757 | 757 | function setReasoningEventHandlers() { |
| 758 | + /** | |
| 759 | + * Updates the reasoning block of a message from a value. | |
| 760 | + * @param {object} message Message object | |
| 761 | + * @param {string} value Reasoning value | |
| 762 | + */ | |
| 763 | + function updateReasoningFromValue(message, value) { | |
| 764 | + const reasoning = getRegexedString(value, regex_placement.REASONING, { isEdit: true }); | |
| 765 | + message.extra.reasoning = reasoning; | |
| 766 | + message.extra.reasoning_type = message.extra.reasoning_type ? ReasoningType.Edited : ReasoningType.Manual; | |
| 767 | + } | |
| 768 | + | |
| 758 | 769 | $(document).on('click', '.mes_reasoning_details', function (e) { |
| 759 | 770 | if (!e.target.closest('.mes_reasoning_actions') && !e.target.closest('.mes_reasoning_header')) { |
| 760 | 771 | e.preventDefault(); |
| @@ -835,9 +846,7 @@ function setReasoningEventHandlers() { | ||
| 835 | 846 | } |
| 836 | 847 | |
| 837 | 848 | const textarea = messageBlock.find('.reasoning_edit_textarea'); |
| 838 | - const reasoning = getRegexedString(String(textarea.val()), regex_placement.REASONING, { isEdit: true }); | |
| 849 | + updateReasoningFromValue(message, String(textarea.val())); | |
| 839 | - message.extra.reasoning = reasoning; | |
| 840 | - message.extra.reasoning_type = message.extra.reasoning_type ? ReasoningType.Edited : ReasoningType.Manual; | |
| 841 | 850 | await saveChatConditional(); |
| 842 | 851 | updateMessageBlock(messageId, message); |
| 843 | 852 | textarea.remove(); |
| @@ -917,6 +926,20 @@ function setReasoningEventHandlers() { | ||
| 917 | 926 | await copyText(reasoning); |
| 918 | 927 | toastr.info(t`Copied!`, '', { timeOut: 2000 }); |
| 919 | 928 | }); |
| 929 | + | |
| 930 | + $(document).on('input', '.reasoning_edit_textarea', function () { | |
| 931 | + if (!power_user.auto_save_msg_edits) { | |
| 932 | + return; | |
| 933 | + } | |
| 934 | + | |
| 935 | + const { message } = getMessageFromJquery(this); | |
| 936 | + if (!message?.extra) { | |
| 937 | + return; | |
| 938 | + } | |
| 939 | + | |
| 940 | + updateReasoningFromValue(message, String($(this).val())); | |
| 941 | + saveChatDebounced(); | |
| 942 | + }); | |
| 920 | 943 | } |
| 921 | 944 | |
| 922 | 945 | /** |
| @@ -973,7 +996,7 @@ function parseReasoningFromString(str, { strict = true } = {}) { | ||
| 973 | 996 | } |
| 974 | 997 | |
| 975 | 998 | function registerReasoningAppEvents() { |
| 976 | 999 | eventSource.makeFirst(event_types.MESSAGE_RECEIVED,const eventHandler = (/** @type {number} */ idx) => { |
| 977 | 1000 | if (!power_user.reasoning.auto_parse) { |
| 978 | 1001 | return; |
| 979 | 1002 | } |
| @@ -1029,7 +1052,11 @@ function registerReasoningAppEvents() { | ||
| 1029 | 1052 | updateMessageBlock(idx, message); |
| 1030 | 1053 | } |
| 1031 | 1054 | } |
| 1032 | 1055 | }); |
| 1056 | + | |
| 1057 | + for (const event of [event_types.MESSAGE_RECEIVED, event_types.MESSAGE_UPDATED]) { | |
| 1058 | + eventSource.on(event, eventHandler); | |
| 1059 | + } | |
| 1033 | 1060 | } |
| 1034 | 1061 | |
| 1035 | 1062 | export function initReasoning() { |