Add {{currentSwipeId}} / {{lastSwipeId}} macros

This commit is contained in:
Cohee 2023-12-07 02:35:24 +02:00
parent 393a15cd81
commit f575e0d61d
2 changed files with 44 additions and 0 deletions

View File

@ -1981,6 +1981,46 @@ function getLastMessage() {
return '';
}
/**
* Returns the ID of the last swipe.
* @returns {string} The 1-based ID of the last swipe
*/
function getLastSwipeId() {
const index = chat?.length - 1;
if (!isNaN(index) && index >= 0) {
const swipes = chat[index].swipes;
if (!Array.isArray(swipes) || swipes.length === 0) {
return '';
}
return String(swipes.length);
}
return '';
}
/**
* Returns the ID of the current swipe.
* @returns {string} The 1-based ID of the current swipe.
*/
function getCurrentSwipeId() {
const index = chat?.length - 1;
if (!isNaN(index) && index >= 0) {
const swipeId = chat[index].swipe_id;
if (swipeId === undefined || isNaN(swipeId)) {
return '';
}
return String(swipeId + 1);
}
return '';
}
/**
* Substitutes {{macro}} parameters in a string.
* @param {string} content - The string to substitute parameters in.
@ -2030,6 +2070,8 @@ function substituteParams(content, _name1, _name2, _original, _group, _replaceCh
content = content.replace(/{{group}}/gi, _group);
content = content.replace(/{{lastMessage}}/gi, getLastMessage());
content = content.replace(/{{lastMessageId}}/gi, getLastMessageId());
content = content.replace(/{{lastSwipeId}}/gi, getLastSwipeId());
content = content.replace(/{{currentSwipeId}}/gi, getCurrentSwipeId());
content = content.replace(/<USER>/gi, _name1);
content = content.replace(/<BOT>/gi, _name2);

View File

@ -17,6 +17,8 @@
<li><tt>&lcub;&lcub;char&rcub;&rcub;</tt> the Character's name</li>
<li><tt>&lcub;&lcub;lastMessage&rcub;&rcub;</tt> - the text of the latest chat message.</li>
<li><tt>&lcub;&lcub;lastMessageId&rcub;&rcub;</tt> index # of the latest chat message. Useful for slash command batching.</li>
<li><tt>&lcub;&lcub;currentSwipeId&rcub;&rcub;</tt> the 1-based ID of the current swipe in the last chat message. Empty string if the last message is user or prompt-hidden.</li>
<li><tt>&lcub;&lcub;lastSwipeId&rcub;&rcub;</tt> the number of swipes in the last chat message. Empty string if the last message is user or prompt-hidden.</li>
<li><tt>&lcub;&lcub;// (note)&rcub;&rcub;</tt> you can leave a note here, and the macro will be replaced with blank content. Not visible for the AI.</li>
<li><tt>&lcub;&lcub;time&rcub;&rcub;</tt> the current time</li>
<li><tt>&lcub;&lcub;date&rcub;&rcub;</tt> the current date</li>