From 1b4d955aec57f9c8c52c927ceb91939fdfb3c3e3 Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Tue, 21 Nov 2023 23:33:20 +0200
Subject: [PATCH 1/3] Add swipe id validation for /delswipe
---
public/scripts/slash-commands.js | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js
index 0c19dab16..7bb7dbddb 100644
--- a/public/scripts/slash-commands.js
+++ b/public/scripts/slash-commands.js
@@ -176,6 +176,12 @@ async function deleteSwipeCallback(_, arg) {
}
const swipeId = arg && !isNaN(Number(arg)) ? (Number(arg) - 1) : lastMessage.swipe_id;
+
+ if (swipeId < 0 || swipeId >= lastMessage.swipes.length) {
+ toastr.warning(`Invalid swipe ID: ${swipeId + 1}`);
+ return;
+ }
+
lastMessage.swipes.splice(swipeId, 1);
if (Array.isArray(lastMessage.swipe_info) && lastMessage.swipe_info.length) {
From 4b78ddbc8a5890c5d9862b44ddcedb6abcb31bbc Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Wed, 22 Nov 2023 00:39:17 +0200
Subject: [PATCH 2/3] First steps in slash command piping
---
public/scripts/slash-commands.js | 31 +++++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js
index 7bb7dbddb..5586f1a0c 100644
--- a/public/scripts/slash-commands.js
+++ b/public/scripts/slash-commands.js
@@ -84,7 +84,7 @@ class SlashCommandParser {
const key = match[1];
const value = match[2];
// Remove the quotes around the value, if any
- argObj[key] = value.replace(/(^")|("$)/g, '');
+ argObj[key] = substituteParams(value.replace(/(^")|("$)/g, ''));
}
// Match unnamed argument
@@ -157,11 +157,36 @@ parser.addCommand('memberup', moveGroupMemberUpCallback, ['upmember'], '(member index or name) – moves a group member down in the group chat list', true, true);
parser.addCommand('peek', peekCallback, [], '(message index or range) – shows a group member character card without switching chats', true, true);
parser.addCommand('delswipe', deleteSwipeCallback, [], '(optional 1-based id) – deletes a swipe from the last chat message. If swipe id not provided - deletes the current swipe.', true, true);
+parser.addCommand('echo', echoCallback, [], '(text) – echoes the text to toast message. Useful for pipes debugging.', true, true);
+parser.addCommand('gen', generateCallback, [], '(prompt) – generates text using the provided prompt and passes it to the next command through the pipe.', true, true);
const NARRATOR_NAME_KEY = 'narrator_name';
const NARRATOR_NAME_DEFAULT = 'System';
export const COMMENT_NAME_DEFAULT = 'Note';
+async function generateCallback(_, arg) {
+ if (!arg) {
+ console.warn('WARN: No argument provided for /gen command');
+ return;
+ }
+
+ // Prevent generate recursion
+ $('#send_textarea').val('');
+
+ const result = await generateQuietPrompt(arg, false, false, '');
+ return result;
+}
+
+async function echoCallback(_, arg) {
+ if (!arg) {
+ console.warn('WARN: No argument provided for /echo command');
+ return;
+ }
+
+ toastr.info(arg);
+ return arg;
+}
+
async function deleteSwipeCallback(_, arg) {
const lastMessage = chat[chat.length - 1];
@@ -895,6 +920,7 @@ async function executeSlashCommands(text) {
const linesToRemove = [];
let interrupt = false;
+ let pipeResult = '';
for (let index = 0; index < lines.length; index++) {
const trimmedLine = lines[index].trim();
@@ -914,7 +940,8 @@ async function executeSlashCommands(text) {
}
console.debug('Slash command executing:', result);
- await result.command.callback(result.args, result.value);
+ const unnamedArg = pipeResult || result.value;
+ pipeResult = await result.command.callback(result.args, unnamedArg);
if (result.command.interruptsGeneration) {
interrupt = true;
From e5f7b0b5c76e8157ddc7e4e4728e42bb65f42382 Mon Sep 17 00:00:00 2001
From: Cohee <18619528+Cohee1207@users.noreply.github.com>
Date: Wed, 22 Nov 2023 00:43:33 +0200
Subject: [PATCH 3/3] Use explicit unnamed argument first if exists
---
public/scripts/slash-commands.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js
index 5586f1a0c..e35be81f8 100644
--- a/public/scripts/slash-commands.js
+++ b/public/scripts/slash-commands.js
@@ -940,7 +940,7 @@ async function executeSlashCommands(text) {
}
console.debug('Slash command executing:', result);
- const unnamedArg = pipeResult || result.value;
+ const unnamedArg = result.value || pipeResult;
pipeResult = await result.command.callback(result.args, unnamedArg);
if (result.command.interruptsGeneration) {