Merge pull request #2629 from SillyTavern/improve-swipe-commands Improve `/addswipe` and `/delswipe` slightly
Signed| @@ -717,6 +717,7 @@ export function initDefaultSlashCommands() { | ||
| 717 | 717 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 718 | 718 | name: 'delswipe', |
| 719 | 719 | callback: deleteSwipeCallback, |
| 720 | + returns: 'the new, currently selected swipe id', | |
| 720 | 721 | aliases: ['swipedel'], |
| 721 | 722 | unnamedArgumentList: [ |
| 722 | 723 | SlashCommandArgument.fromProps({ |
| @@ -912,13 +913,28 @@ export function initDefaultSlashCommands() { | ||
| 912 | 913 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 913 | 914 | name: 'addswipe', |
| 914 | 915 | callback: addSwipeCallback, |
| 916 | + returns: 'the new swipe id', | |
| 915 | 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 | 926 | unnamedArgumentList: [ |
| 917 | 927 | new SlashCommandArgument( |
| 918 | 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 | 939 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 924 | 940 | name: 'stop', |
| @@ -2154,8 +2170,11 @@ async function echoCallback(args, value) { | ||
| 2154 | 2170 | } |
| 2155 | 2171 | } |
| 2156 | 2172 | |
| 2157 | - | |
| 2173 | +/** | |
| 2158 | -async function addSwipeCallback(_, arg) { | |
| 2174 | + * @param {{switch?: string}} args - named arguments | |
| 2175 | + * @param {string} value - The swipe text to add (unnamed argument) | |
| 2176 | + */ | |
| 2177 | +async function addSwipeCallback(args, value) { | |
| 2159 | 2178 | const lastMessage = chat[chat.length - 1]; |
| 2160 | 2179 | |
| 2161 | 2180 | if (!lastMessage) { |
| @@ -2163,7 +2182,7 @@ async function addSwipeCallback(_, arg) { | ||
| 2163 | 2182 | return ''; |
| 2164 | 2183 | } |
| 2165 | 2184 | |
| 2166 | 2185 | if (!argvalue) { |
| 2167 | 2186 | console.warn('WARN: No argument provided for /addswipe command'); |
| 2168 | 2187 | return ''; |
| 2169 | 2188 | } |
| @@ -2192,23 +2211,30 @@ async function addSwipeCallback(_, arg) { | ||
| 2192 | 2211 | lastMessage.swipe_info = lastMessage.swipes.map(() => ({})); |
| 2193 | 2212 | } |
| 2194 | 2213 | |
| 2195 | 2214 | lastMessage.swipes.push(argvalue); |
| 2196 | 2215 | lastMessage.swipe_info.push({ |
| 2197 | 2216 | send_date: getMessageTimeStamp(), |
| 2198 | 2217 | gen_started: null, |
| 2199 | 2218 | gen_finished: null, |
| 2200 | 2219 | extra: { |
| 2201 | 2220 | bias: extractMessageBias(argvalue), |
| 2202 | 2221 | gen_id: Date.now(), |
| 2203 | 2222 | api: 'manual', |
| 2204 | 2223 | model: 'slash command', |
| 2205 | 2224 | }, |
| 2206 | 2225 | }); |
| 2207 | 2226 | |
| 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 | 2234 | await saveChatConditional(); |
| 2209 | 2235 | await reloadCurrentChat(); |
| 2210 | 2236 | |
| 2211 | 2237 | return ''String(newSwipeId); |
| 2212 | 2238 | } |
| 2213 | 2239 | |
| 2214 | 2240 | async function deleteSwipeCallback(_, arg) { |
| @@ -2244,7 +2270,7 @@ async function deleteSwipeCallback(_, arg) { | ||
| 2244 | 2270 | await saveChatConditional(); |
| 2245 | 2271 | await reloadCurrentChat(); |
| 2246 | 2272 | |
| 2247 | 2273 | return ''String(newSwipeId); |
| 2248 | 2274 | } |
| 2249 | 2275 | |
| 2250 | 2276 | async function askCharacter(args, text) { |