From 2baea084a17d7226e22432ae0a5d58c77cfc42a8 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 17 Mar 2024 02:45:22 +0200 Subject: [PATCH] Add lastCharMessage and lastUserMessage macros --- public/scripts/macros.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/public/scripts/macros.js b/public/scripts/macros.js index 5e6b37b7b..e8954f874 100644 --- a/public/scripts/macros.js +++ b/public/scripts/macros.js @@ -46,6 +46,42 @@ function getLastMessage() { return ''; } +/** + * Returns the last message from the user. + * @returns {string} The last message from the user. + */ +function getLastUserMessage() { + if (!Array.isArray(chat) || chat.length === 0) { + return ''; + } + + for (let i = chat.length - 1; i >= 0; i--) { + if (chat[i].is_user && !chat[i].is_system) { + return chat[i].mes; + } + } + + return ''; +} + +/** + * Returns the last message from the bot. + * @returns {string} The last message from the bot. + */ +function getLastCharMessage() { + if (!Array.isArray(chat) || chat.length === 0) { + return ''; + } + + for (let i = chat.length - 1; i >= 0; i--) { + if (!chat[i].is_user && !chat[i].is_system) { + return chat[i].mes; + } + } + + return ''; +} + /** * Returns the ID of the last swipe. * @returns {string} The 1-based ID of the last swipe @@ -238,6 +274,8 @@ export function evaluateMacros(content, env) { content = content.replace(/{{maxPrompt}}/gi, () => String(getMaxContextSize())); content = content.replace(/{{lastMessage}}/gi, () => getLastMessage()); content = content.replace(/{{lastMessageId}}/gi, () => getLastMessageId()); + content = content.replace(/{{lastUserMessage}}/gi, () => getLastUserMessage()); + content = content.replace(/{{lastCharMessage}}/gi, () => getLastCharMessage()); content = content.replace(/{{firstIncludedMessageId}}/gi, () => getFirstIncludedMessageId()); content = content.replace(/{{lastSwipeId}}/gi, () => getLastSwipeId()); content = content.replace(/{{currentSwipeId}}/gi, () => getCurrentSwipeId());