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
| @@ -5429,20 +5429,24 @@ async function promptItemize(itemizedPrompts, requestedMesId) { | ||
| 5429 | 5429 | await popup.show(); |
| 5430 | 5430 | } |
| 5431 | 5431 | |
| 5432 | 5432 | function setInContextMessages(lastmsgmsgInContextCount, type) { |
| 5433 | 5433 | $('#chat .mes').removeClass('lastInContext'); |
| 5434 | 5434 | |
| 5435 | 5435 | if (type === 'swipe' || type === 'regenerate' || type === 'continue') { |
| 5436 | 5436 | lastmsgmsgInContextCount++; |
| 5437 | 5437 | } |
| 5438 | 5438 | |
| 5439 | 5439 | const lastMessageBlock = $('#chat .mes:not([is_system="true"])').eq(-lastmsgmsgInContextCount); |
| 5440 | 5440 | lastMessageBlock.addClass('lastInContext'); |
| 5441 | 5441 | |
| 5442 | 5442 | if (lastMessageBlock.length === 0) { |
| 5443 | 5443 | const firstMessageId = getFirstDisplayedMessageId(); |
| 5444 | 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 | } |
| 5447 | 5451 | |
| 5448 | 5452 | /** |
| @@ -202,13 +202,7 @@ export function getLastMessageId({ exclude_swipe_in_propress = true, filter = nu | ||
| 202 | 202 | * @returns {number|null} The ID of the first message in the context |
| 203 | 203 | */ |
| 204 | 204 | function 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 | } |
| 213 | 207 | |
| 214 | 208 | /** |
| @@ -28,7 +28,7 @@ | ||
| 28 | 28 | <li><tt>{{lastUserMessage}}</tt> – <span data-i18n="help_macros_lastUser">the text of the latest user chat message.</span></li> |
| 29 | 29 | <li><tt>{{lastCharMessage}}</tt> – <span data-i18n="help_macros_lastChar">the text of the latest character chat message.</span></li> |
| 30 | 30 | <li><tt>{{lastMessageId}}</tt> – <span data-i18n="help_macros_21">index # of the latest chat message. Useful for slash command batching.</span></li> |
| 31 | 31 | <li><tt>{{firstIncludedMessageId}}</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> |
| 32 | 32 | <li><tt>{{firstDisplayedMessageId}}</tt> – <span data-i18n="help_macros_firstDisplayedMessageId">the ID of the first message loaded into the visible chat.</span></li> |
| 33 | 33 | <li><tt>{{currentSwipeId}}</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 | 34 | <li><tt>{{lastSwipeId}}</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> |