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 -11Showing whitespace changes
public/script.js+7 -3
@@ -5429,20 +5429,24 @@ async function promptItemize(itemizedPrompts, requestedMesId) {
5429 await popup.show();5429 await popup.show();
5430}5430}
54315431
5432function setInContextMessages(lastmsg, type) {5432function setInContextMessages(msgInContextCount, type) {
5433 $('#chat .mes').removeClass('lastInContext');5433 $('#chat .mes').removeClass('lastInContext');
54345434
5435 if (type === 'swipe' || type === 'regenerate' || type === 'continue') {5435 if (type === 'swipe' || type === 'regenerate' || type === 'continue') {
5436 lastmsg++;5436 msgInContextCount++;
5437 }5437 }
54385438
5439 const lastMessageBlock = $('#chat .mes:not([is_system="true"])').eq(-lastmsg);5439 const lastMessageBlock = $('#chat .mes:not([is_system="true"])').eq(-msgInContextCount);
5440 lastMessageBlock.addClass('lastInContext');5440 lastMessageBlock.addClass('lastInContext');
54415441
5442 if (lastMessageBlock.length === 0) {5442 if (lastMessageBlock.length === 0) {
5443 const firstMessageId = getFirstDisplayedMessageId();5443 const firstMessageId = getFirstDisplayedMessageId();
5444 $(`#chat .mes[mesid="${firstMessageId}"`).addClass('lastInContext');5444 $(`#chat .mes[mesid="${firstMessageId}"`).addClass('lastInContext');
5445 }5445 }
5446
5447 const lastMessageId = Math.max(0, chat.length - msgInContextCount);
5448 chat_metadata['lastInContextMessageId'] = lastMessageId;
5449 saveMetadataDebounced();
5446}5450}
54475451
5448/**5452/**
public/scripts/macros.js+1 -7
@@ -202,13 +202,7 @@ export function getLastMessageId({ exclude_swipe_in_propress = true, filter = nu
202 * @returns {number|null} The ID of the first message in the context202 * @returns {number|null} The ID of the first message in the context
203 */203 */
204function getFirstIncludedMessageId() {204function 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;
212}206}
213207
214/**208/**
public/scripts/templates/macros.html+1 -1
@@ -28,7 +28,7 @@
28 <li><tt>&lcub;&lcub;lastUserMessage&rcub;&rcub;</tt> – <span data-i18n="help_macros_lastUser">the text of the latest user chat message.</span></li>28 <li><tt>&lcub;&lcub;lastUserMessage&rcub;&rcub;</tt> – <span data-i18n="help_macros_lastUser">the text of the latest user chat message.</span></li>
29 <li><tt>&lcub;&lcub;lastCharMessage&rcub;&rcub;</tt> – <span data-i18n="help_macros_lastChar">the text of the latest character chat message.</span></li>29 <li><tt>&lcub;&lcub;lastCharMessage&rcub;&rcub;</tt> – <span data-i18n="help_macros_lastChar">the text of the latest character chat message.</span></li>
30 <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>30 <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>
31 <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 ran at least once in the current session.</span></li>31 <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 run at least once in the current session. Will only be updated on generation.</span></li>
32 <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>32 <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>
33 <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>33 <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>
34 <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>34 <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>