Merge pull request #2438 from SillyTavern/commands-send-at-supports-depth Various commands with 'at' support depth values

bf28ae07b338709d4201664aa8871270bae6372a

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +33 -8Ignore whitespace
public/scripts/slash-commands.js+33 -8
@@ -177,7 +177,7 @@ export function initDefaultSlashCommands() {
177177 ),
178178 SlashCommandNamedArgument.fromProps({
179179 name: 'at',
180- description: 'position to insert the message',
180+ description: 'position to insert the message (index-based, corresponding to message id). If not set, the message will be inserted at the end of the chat.\nNegative values are accepted and will work similarly to how \'depth\' usually works. For example, -1 will insert the message right before the last message in chat.',
181181 typeList: [ARGUMENT_TYPE.NUMBER],
182182 enumProvider: commonEnumProviders.messages({ allowIdAfter: true }),
183183 }),
@@ -220,7 +220,7 @@ export function initDefaultSlashCommands() {
220220 ),
221221 SlashCommandNamedArgument.fromProps({
222222 name: 'at',
223- description: 'position to insert the message',
223+ description: 'position to insert the message (index-based, corresponding to message id). If not set, the message will be inserted at the end of the chat.\nNegative values are accepted and will work similarly to how \'depth\' usually works. For example, -1 will insert the message right before the last message in chat.',
224224 typeList: [ARGUMENT_TYPE.NUMBER],
225225 enumProvider: commonEnumProviders.messages({ allowIdAfter: true }),
226226 }),
@@ -274,7 +274,7 @@ export function initDefaultSlashCommands() {
274274 ),
275275 SlashCommandNamedArgument.fromProps({
276276 name: 'at',
277- description: 'position to insert the message',
277+ description: 'position to insert the message (index-based, corresponding to message id). If not set, the message will be inserted at the end of the chat.\nNegative values are accepted and will work similarly to how \'depth\' usually works. For example, -1 will insert the message right before the last message in chat.',
278278 typeList: [ARGUMENT_TYPE.NUMBER],
279279 enumProvider: commonEnumProviders.messages({ allowIdAfter: true }),
280280 }),
@@ -459,7 +459,7 @@ export function initDefaultSlashCommands() {
459459 ),
460460 SlashCommandNamedArgument.fromProps({
461461 name: 'at',
462- description: 'position to insert the message',
462+ description: 'position to insert the message (index-based, corresponding to message id). If not set, the message will be inserted at the end of the chat.\nNegative values are accepted and will work similarly to how \'depth\' usually works. For example, -1 will insert the message right before the last message in chat.',
463463 typeList: [ARGUMENT_TYPE.NUMBER],
464464 enumProvider: commonEnumProviders.messages({ allowIdAfter: true }),
465465 }),
@@ -2490,7 +2490,14 @@ async function sendUserMessageCallback(args, text) {
24902490 text = text.trim();
24912491 const compact = isTrueBoolean(args?.compact);
24922492 const bias = extractMessageBias(text);
2493- const insertAt = Number(args?.at);
2493+
2494+ let insertAt = Number(args?.at);
2495+
2496+ // Convert possible depth parameter to index
2497+ if (!isNaN(insertAt) && (insertAt < 0 || insertAt === Number(-0))) {
2498+ // Negative value means going back from current chat length. (E.g.: 8 messages, Depth 1 means insert at index 7)
2499+ insertAt = chat.length + insertAt;
2500+ }
24942501
24952502 if ('name' in args) {
24962503 const name = args.name || '';
@@ -2799,7 +2806,13 @@ export async function sendMessageAs(args, text) {
27992806 },
28002807 }];
28012808
28022809 constlet insertAt = Number(args.at);
2810+
2811+ // Convert possible depth parameter to index
2812+ if (!isNaN(insertAt) && (insertAt < 0 || insertAt === Number(-0))) {
2813+ // Negative value means going back from current chat length. (E.g.: 8 messages, Depth 1 means insert at index 7)
2814+ insertAt = chat.length + insertAt;
2815+ }
28032816
28042817 if (!isNaN(insertAt) && insertAt >= 0 && insertAt <= chat.length) {
28052818 chat.splice(insertAt, 0, message);
@@ -2846,7 +2859,13 @@ export async function sendNarratorMessage(args, text) {
28462859 },
28472860 };
28482861
28492862 constlet insertAt = Number(args.at);
2863+
2864+ // Convert possible depth parameter to index
2865+ if (!isNaN(insertAt) && (insertAt < 0 || insertAt === Number(-0))) {
2866+ // Negative value means going back from current chat length. (E.g.: 8 messages, Depth 1 means insert at index 7)
2867+ insertAt = chat.length + insertAt;
2868+ }
28502869
28512870 if (!isNaN(insertAt) && insertAt >= 0 && insertAt <= chat.length) {
28522871 chat.splice(insertAt, 0, message);
@@ -2928,7 +2947,13 @@ async function sendCommentMessage(args, text) {
29282947 },
29292948 };
29302949
29312950 constlet insertAt = Number(args.at);
2951+
2952+ // Convert possible depth parameter to index
2953+ if (!isNaN(insertAt) && (insertAt < 0 || insertAt === Number(-0))) {
2954+ // Negative value means going back from current chat length. (E.g.: 8 messages, Depth 1 means insert at index 7)
2955+ insertAt = chat.length + insertAt;
2956+ }
29322957
29332958 if (!isNaN(insertAt) && insertAt >= 0 && insertAt <= chat.length) {
29342959 chat.splice(insertAt, 0, message);