diff --git a/public/scripts/macros.js b/public/scripts/macros.js index cbc38ded4..7ce1a373c 100644 --- a/public/scripts/macros.js +++ b/public/scripts/macros.js @@ -211,6 +211,21 @@ function getFirstIncludedMessageId() { return null; } +/** + * Returns the ID of the first displayed message in the chat. + * + * @returns {number|null} The ID of the first displayed message + */ +function getFirstDisplayedMessageId() { + const mesId = Number(document.querySelector('#chat .mes')?.getAttribute('mesid')); + + if (!isNaN(mesId) && mesId >= 0) { + return mesId; + } + + return null; +} + /** * Returns the last message in the chat * @@ -467,6 +482,7 @@ export function evaluateMacros(content, env, postProcessFn) { { regex: /{{lastUserMessage}}/gi, replace: () => getLastUserMessage() }, { regex: /{{lastCharMessage}}/gi, replace: () => getLastCharMessage() }, { regex: /{{firstIncludedMessageId}}/gi, replace: () => String(getFirstIncludedMessageId() ?? '') }, + { regex: /{{firstDisplayedMessageId}}/gi, replace: () => String(getFirstDisplayedMessageId() ?? '') }, { regex: /{{lastSwipeId}}/gi, replace: () => String(getLastSwipeId() ?? '') }, { regex: /{{currentSwipeId}}/gi, replace: () => String(getCurrentSwipeId() ?? '') }, { regex: /{{reverse:(.+?)}}/gi, replace: (_, str) => Array.from(str).reverse().join('') }, diff --git a/public/scripts/templates/macros.html b/public/scripts/templates/macros.html index 7490596d3..97740073f 100644 --- a/public/scripts/templates/macros.html +++ b/public/scripts/templates/macros.html @@ -29,6 +29,7 @@
  • {{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.
  • +
  • {{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.
  • {{reverse:(content)}}reverses the content of the macro.