Merge pull request #2629 from SillyTavern/improve-swipe-commands Improve `/addswipe` and `/delswipe` slightly

478e1a6bb5a57e46cc8f63433742bcdd4b0bde53

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

Signed
1 files changed, +34 -8Ignore whitespace
public/scripts/slash-commands.js+34 -8
@@ -717,6 +717,7 @@ export function initDefaultSlashCommands() {
717 SlashCommandParser.addCommandObject(SlashCommand.fromProps({717 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
718 name: 'delswipe',718 name: 'delswipe',
719 callback: deleteSwipeCallback,719 callback: deleteSwipeCallback,
720 returns: 'the new, currently selected swipe id',
720 aliases: ['swipedel'],721 aliases: ['swipedel'],
721 unnamedArgumentList: [722 unnamedArgumentList: [
722 SlashCommandArgument.fromProps({723 SlashCommandArgument.fromProps({
@@ -912,13 +913,28 @@ export function initDefaultSlashCommands() {
912 SlashCommandParser.addCommandObject(SlashCommand.fromProps({913 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
913 name: 'addswipe',914 name: 'addswipe',
914 callback: addSwipeCallback,915 callback: addSwipeCallback,
916 returns: 'the new swipe id',
915 aliases: ['swipeadd'],917 aliases: ['swipeadd'],
918 namedArgumentList: [
919 SlashCommandNamedArgument.fromProps({
920 name: 'switch',
921 description: 'switch to the new swipe',
922 typeList: [ARGUMENT_TYPE.BOOLEAN],
923 enumList: commonEnumProviders.boolean()(),
924 }),
925 ],
916 unnamedArgumentList: [926 unnamedArgumentList: [
917 new SlashCommandArgument(927 new SlashCommandArgument(
918 'text', [ARGUMENT_TYPE.STRING], true,928 'text', [ARGUMENT_TYPE.STRING], true,
919 ),929 ),
920 ],930 ],
921 helpString: 'Adds a swipe to the last chat message.',931 helpString: `
932 <div>
933 Adds a swipe to the last chat message.
934 </div>
935 <div>
936 Use switch=true to switch to directly switch to the new swipe.
937 </div>`,
922 }));938 }));
923 SlashCommandParser.addCommandObject(SlashCommand.fromProps({939 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
924 name: 'stop',940 name: 'stop',
@@ -2154,8 +2170,11 @@ async function echoCallback(args, value) {
2154 }2170 }
2155}2171}
21562172
21572173/**
2158async function addSwipeCallback(_, arg) {2174 * @param {{switch?: string}} args - named arguments
2175 * @param {string} value - The swipe text to add (unnamed argument)
2176 */
2177async function addSwipeCallback(args, value) {
2159 const lastMessage = chat[chat.length - 1];2178 const lastMessage = chat[chat.length - 1];
21602179
2161 if (!lastMessage) {2180 if (!lastMessage) {
@@ -2163,7 +2182,7 @@ async function addSwipeCallback(_, arg) {
2163 return '';2182 return '';
2164 }2183 }
21652184
2166 if (!arg) {2185 if (!value) {
2167 console.warn('WARN: No argument provided for /addswipe command');2186 console.warn('WARN: No argument provided for /addswipe command');
2168 return '';2187 return '';
2169 }2188 }
@@ -2192,23 +2211,30 @@ async function addSwipeCallback(_, arg) {
2192 lastMessage.swipe_info = lastMessage.swipes.map(() => ({}));2211 lastMessage.swipe_info = lastMessage.swipes.map(() => ({}));
2193 }2212 }
21942213
2195 lastMessage.swipes.push(arg);2214 lastMessage.swipes.push(value);
2196 lastMessage.swipe_info.push({2215 lastMessage.swipe_info.push({
2197 send_date: getMessageTimeStamp(),2216 send_date: getMessageTimeStamp(),
2198 gen_started: null,2217 gen_started: null,
2199 gen_finished: null,2218 gen_finished: null,
2200 extra: {2219 extra: {
2201 bias: extractMessageBias(arg),2220 bias: extractMessageBias(value),
2202 gen_id: Date.now(),2221 gen_id: Date.now(),
2203 api: 'manual',2222 api: 'manual',
2204 model: 'slash command',2223 model: 'slash command',
2205 },2224 },
2206 });2225 });
22072226
2227 const newSwipeId = lastMessage.swipes.length - 1;
2228
2229 if (isTrueBoolean(args.switch)) {
2230 lastMessage.swipe_id = newSwipeId;
2231 lastMessage.mes = lastMessage.swipes[newSwipeId];
2232 }
2233
2208 await saveChatConditional();2234 await saveChatConditional();
2209 await reloadCurrentChat();2235 await reloadCurrentChat();
22102236
2211 return '';2237 return String(newSwipeId);
2212}2238}
22132239
2214async function deleteSwipeCallback(_, arg) {2240async function deleteSwipeCallback(_, arg) {
@@ -2244,7 +2270,7 @@ async function deleteSwipeCallback(_, arg) {
2244 await saveChatConditional();2270 await saveChatConditional();
2245 await reloadCurrentChat();2271 await reloadCurrentChat();
22462272
2247 return '';2273 return String(newSwipeId);
2248}2274}
22492275
2250async function askCharacter(args, text) {2276async function askCharacter(args, text) {