diff --git a/public/script.js b/public/script.js index 2761dea29..a1142f365 100644 --- a/public/script.js +++ b/public/script.js @@ -1661,6 +1661,20 @@ function scrollChatToBottom() { } } +/** + * Returns the ID of the last message in the chat. + * @returns {string} The ID of the last message in the chat. + */ +function getLastMessageId() { + const index = chat?.length - 1; + + if (!isNaN(index) && index >= 0) { + return String(index); + } + + return ''; +} + /** * Substitutes {{macro}} parameters in a string. * @param {string} content - The string to substitute parameters in. @@ -1690,6 +1704,7 @@ function substituteParams(content, _name1, _name2, _original, _group) { content = content.replace(/{{char}}/gi, _name2); content = content.replace(/{{charIfNotGroup}}/gi, _group); content = content.replace(/{{group}}/gi, _group); + content = content.replace(/{{lastMessageId}}/gi, getLastMessageId()); content = content.replace(//gi, _name1); content = content.replace(//gi, _name2); @@ -1885,12 +1900,12 @@ export async function generateQuietPrompt(quiet_prompt, quietToLoud, skipWIAN) { }); } -function processCommands(message, type) { +async function processCommands(message, type) { if (type == "regenerate" || type == "swipe" || type == 'quiet') { return null; } - const result = executeSlashCommands(message); + const result = await executeSlashCommands(message); $("#send_textarea").val(result.newText).trigger('input'); // interrupt generation if the input was nothing but a command @@ -2405,7 +2420,7 @@ async function Generate(type, { automatic_trigger, force_name2, resolve, reject, message_already_generated = isImpersonate ? `${name1}: ` : `${name2}: `; - const interruptedByCommand = processCommands($("#send_textarea").val(), type); + const interruptedByCommand = await processCommands($("#send_textarea").val(), type); if (interruptedByCommand) { $("#send_textarea").val('').trigger('input'); @@ -4457,7 +4472,13 @@ function saveChatDebounced() { async function saveChat(chat_name, withMetadata, mesId) { const metadata = { ...chat_metadata, ...(withMetadata || {}) }; - let file_name = chat_name ?? characters[this_chid].chat; + let file_name = chat_name ?? characters[this_chid]?.chat; + + if (!file_name) { + console.warn('saveChat called without chat_name and no chat file found'); + return; + } + characters[this_chid]['date_last_chat'] = Date.now(); chat.forEach(function (item, i) { if (item["is_group"]) { diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 372106ab8..d74d882de 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -681,7 +681,7 @@ function setBackgroundCallback(_, bg) { } } -function executeSlashCommands(text) { +async function executeSlashCommands(text) { if (!text) { return false; } @@ -706,8 +706,12 @@ function executeSlashCommands(text) { continue; } + if (result.value && typeof result.value === 'string') { + result.value = substituteParams(result.value.trim()); + } + console.debug('Slash command executing:', result); - result.command.callback(result.args, result.value); + await result.command.callback(result.args, result.value); if (result.command.interruptsGeneration) { interrupt = true; diff --git a/public/scripts/templates/macros.html b/public/scripts/templates/macros.html index 8960a3a78..fcf500d8c 100644 --- a/public/scripts/templates/macros.html +++ b/public/scripts/templates/macros.html @@ -4,6 +4,7 @@ System-wide Replacement Macros:
  • {{char}} - the Character's name
  • {{input}} - the user input
  • {{// (note)}} - you can leave a note here, and the macro will be replaced with blank content. Not visible for the AI.
  • +
  • {{lastMessageId}} - index # of the latest chat message. Useful for slash command batching.
  • {{time}} - the current time
  • {{date}} - the current date
  • {{weekday}} - the current weekday