diff --git a/public/script.js b/public/script.js index 7a32c65dd..2bd680cd0 100644 --- a/public/script.js +++ b/public/script.js @@ -5429,20 +5429,24 @@ async function promptItemize(itemizedPrompts, requestedMesId) { await popup.show(); } -function setInContextMessages(lastmsg, type) { +function setInContextMessages(msgInContextCount, type) { $('#chat .mes').removeClass('lastInContext'); if (type === 'swipe' || type === 'regenerate' || type === 'continue') { - lastmsg++; + msgInContextCount++; } - const lastMessageBlock = $('#chat .mes:not([is_system="true"])').eq(-lastmsg); + const lastMessageBlock = $('#chat .mes:not([is_system="true"])').eq(-msgInContextCount); lastMessageBlock.addClass('lastInContext'); if (lastMessageBlock.length === 0) { const firstMessageId = getFirstDisplayedMessageId(); $(`#chat .mes[mesid="${firstMessageId}"`).addClass('lastInContext'); } + + const lastMessageId = Math.max(0, chat.length - msgInContextCount); + chat_metadata['lastInContextMessageId'] = lastMessageId; + saveMetadataDebounced(); } /** diff --git a/public/scripts/macros.js b/public/scripts/macros.js index 7ce1a373c..3464b7a64 100644 --- a/public/scripts/macros.js +++ b/public/scripts/macros.js @@ -202,13 +202,7 @@ export function getLastMessageId({ exclude_swipe_in_propress = true, filter = nu * @returns {number|null} The ID of the first message in the context */ function getFirstIncludedMessageId() { - const index = Number(document.querySelector('.lastInContext')?.getAttribute('mesid')); - - if (!isNaN(index) && index >= 0) { - return index; - } - - return null; + return chat_metadata['lastInContextMessageId']; } /** diff --git a/public/scripts/templates/macros.html b/public/scripts/templates/macros.html index 97740073f..50ca3e5fe 100644 --- a/public/scripts/templates/macros.html +++ b/public/scripts/templates/macros.html @@ -28,7 +28,7 @@
  • {{lastUserMessage}}the text of the latest user chat message.
  • {{lastCharMessage}}the text of the latest character chat message.
  • {{lastMessageId}}index # of the latest chat message. Useful for slash command batching.
  • -
  • {{firstIncludedMessageId}}the ID of the first message included in the context. Requires generation to be ran at least once in the current session.
  • +
  • {{firstIncludedMessageId}}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.
  • {{firstDisplayedMessageId}}the ID of the first message loaded into the visible chat.
  • {{currentSwipeId}}the 1-based ID of the current swipe in the last chat message. Empty string if the last message is user or prompt-hidden.
  • {{lastSwipeId}}the number of swipes in the last chat message. Empty string if the last message is user or prompt-hidden.