Merge pull request #3287 from SillyTavern/fix-first-included-message-id-macro Fix `{{firstIncludedMessageId}}` to not rely on DOM

a62fa3b073c7d547e023729682e8a5b607266d00

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
3 files changed, +9 -11Showing whitespace changes
public/script.js+7 -3
@@ -5429,20 +5429,24 @@ async function promptItemize(itemizedPrompts, requestedMesId) {
54295429 await popup.show();
54305430}
54315431
54325432function setInContextMessages(lastmsgmsgInContextCount, type) {
54335433 $('#chat .mes').removeClass('lastInContext');
54345434
54355435 if (type === 'swipe' || type === 'regenerate' || type === 'continue') {
54365436 lastmsgmsgInContextCount++;
54375437 }
54385438
54395439 const lastMessageBlock = $('#chat .mes:not([is_system="true"])').eq(-lastmsgmsgInContextCount);
54405440 lastMessageBlock.addClass('lastInContext');
54415441
54425442 if (lastMessageBlock.length === 0) {
54435443 const firstMessageId = getFirstDisplayedMessageId();
54445444 $(`#chat .mes[mesid="${firstMessageId}"`).addClass('lastInContext');
54455445 }
5446+
5447+ // Update last id to chat. No metadata save on purpose, gets hopefully saved via another call
5448+ const lastMessageId = Math.max(0, chat.length - msgInContextCount);
5449+ chat_metadata['lastInContextMessageId'] = lastMessageId;
54465450}
54475451
54485452/**
public/scripts/macros.js+1 -7
@@ -202,13 +202,7 @@ export function getLastMessageId({ exclude_swipe_in_propress = true, filter = nu
202202 * @returns {number|null} The ID of the first message in the context
203203 */
204204function getFirstIncludedMessageId() {
205- const index = Number(document.querySelector('.lastInContext')?.getAttribute('mesid'));
205+ return chat_metadata['lastInContextMessageId'];
206-
207- if (!isNaN(index) && index >= 0) {
208- return index;
209- }
210-
211- return null;
212206}
213207
214208/**
public/scripts/templates/macros.html+1 -1
@@ -28,7 +28,7 @@
2828 <li><tt>&lcub;&lcub;lastUserMessage&rcub;&rcub;</tt> – <span data-i18n="help_macros_lastUser">the text of the latest user chat message.</span></li>
2929 <li><tt>&lcub;&lcub;lastCharMessage&rcub;&rcub;</tt> – <span data-i18n="help_macros_lastChar">the text of the latest character chat message.</span></li>
3030 <li><tt>&lcub;&lcub;lastMessageId&rcub;&rcub;</tt> – <span data-i18n="help_macros_21">index # of the latest chat message. Useful for slash command batching.</span></li>
3131 <li><tt>&lcub;&lcub;firstIncludedMessageId&rcub;&rcub;</tt> – <span data-i18n="help_macros_22">the ID of the first message included in the context. Requires generation to be ranrun at least once in the current session. Will only be updated on generation.</span></li>
3232 <li><tt>&lcub;&lcub;firstDisplayedMessageId&rcub;&rcub;</tt> – <span data-i18n="help_macros_firstDisplayedMessageId">the ID of the first message loaded into the visible chat.</span></li>
3333 <li><tt>&lcub;&lcub;currentSwipeId&rcub;&rcub;</tt> – <span data-i18n="help_macros_23">the 1-based ID of the current swipe in the last chat message. Empty string if the last message is user or prompt-hidden.</span></li>
3434 <li><tt>&lcub;&lcub;lastSwipeId&rcub;&rcub;</tt> – <span data-i18n="help_macros_24">the number of swipes in the last chat message. Empty string if the last message is user or prompt-hidden.</span></li>