Reasoning edit/parse updates 1. Parse reasoning on message edit 2. Reasoning edit follow 'auto-save' preference
| @@ -1,7 +1,7 @@ | |||
| 1 | import { | 1 | import { |
| 2 | moment, | 2 | moment, |
| 3 | } from '../lib.js'; | 3 | } from '../lib.js'; |
| 4 | import { chat, closeMessageEditor, event_types, eventSource, main_api, messageFormatting, saveChatConditional, saveSettingsDebounced, substituteParams, updateMessageBlock } from '../script.js'; | 4 | import { chat, closeMessageEditor, event_types, eventSource, main_api, messageFormatting, saveChatConditional, saveChatDebounced, saveSettingsDebounced, substituteParams, updateMessageBlock } from '../script.js'; |
| 5 | import { getRegexedString, regex_placement } from './extensions/regex/engine.js'; | 5 | import { getRegexedString, regex_placement } from './extensions/regex/engine.js'; |
| 6 | import { getCurrentLocale, t, translate } from './i18n.js'; | 6 | import { getCurrentLocale, t, translate } from './i18n.js'; |
| 7 | import { MacrosParser } from './macros.js'; | 7 | import { MacrosParser } from './macros.js'; |
| @@ -755,6 +755,17 @@ function registerReasoningMacros() { | |||
| 755 | } | 755 | } |
| 756 | 756 | ||
| 757 | function setReasoningEventHandlers() { | 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 | $(document).on('click', '.mes_reasoning_details', function (e) { | 769 | $(document).on('click', '.mes_reasoning_details', function (e) { |
| 759 | if (!e.target.closest('.mes_reasoning_actions') && !e.target.closest('.mes_reasoning_header')) { | 770 | if (!e.target.closest('.mes_reasoning_actions') && !e.target.closest('.mes_reasoning_header')) { |
| 760 | e.preventDefault(); | 771 | e.preventDefault(); |
| @@ -835,9 +846,7 @@ function setReasoningEventHandlers() { | |||
| 835 | } | 846 | } |
| 836 | 847 | ||
| 837 | const textarea = messageBlock.find('.reasoning_edit_textarea'); | 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 | await saveChatConditional(); | 850 | await saveChatConditional(); |
| 842 | updateMessageBlock(messageId, message); | 851 | updateMessageBlock(messageId, message); |
| 843 | textarea.remove(); | 852 | textarea.remove(); |
| @@ -917,6 +926,20 @@ function setReasoningEventHandlers() { | |||
| 917 | await copyText(reasoning); | 926 | await copyText(reasoning); |
| 918 | toastr.info(t`Copied!`, '', { timeOut: 2000 }); | 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 | function registerReasoningAppEvents() { | 998 | function registerReasoningAppEvents() { |
| 976 | eventSource.makeFirst(event_types.MESSAGE_RECEIVED, (/** @type {number} */ idx) => { | 999 | const eventHandler = (/** @type {number} */ idx) => { |
| 977 | if (!power_user.reasoning.auto_parse) { | 1000 | if (!power_user.reasoning.auto_parse) { |
| 978 | return; | 1001 | return; |
| 979 | } | 1002 | } |
| @@ -1029,7 +1052,11 @@ function registerReasoningAppEvents() { | |||
| 1029 | updateMessageBlock(idx, message); | 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 | export function initReasoning() { | 1062 | export function initReasoning() { |