Fix {{firstIncludedMessageId}} to not rely on DOM Up until now, the {{firstIncludedMessageId}} macro relied on searching the DOM for the actual CSS class applied for the context line. Bad design, as the actual message was maybe not display, with display X messages enabled. - Use setInContextMessages() that sets the context line on generation to also update a chat metadata field - Utilize that field inside the macro - Update docs to clarify that this will only show the mesid that was relevant during last generation

7a2276c176bc7d99f0ab8ebb0c05d03d9d4c3984

Wolfsblvt <wolfsblvt@gmail.com>

3 files changed, +9 -11Ignore whitespace
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+ const lastMessageId = Math.max(0, chat.length - msgInContextCount);
5448+ chat_metadata['lastInContextMessageId'] = lastMessageId;
5449+ saveMetadataDebounced();
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>