Merge pull request #2803 from SillyTavern/small-bookmark-updates Update Checkpoints Feature (Expand tooltip and icons, add slash commands, refactoring)
Signed| @@ -5799,7 +5799,7 @@ | |||
| 5799 | <div title="Create branch" class="mes_button mes_create_branch fa-regular fa-code-branch" data-i18n="[title]Create Branch"></div> | 5799 | <div title="Create branch" class="mes_button mes_create_branch fa-regular fa-code-branch" data-i18n="[title]Create Branch"></div> |
| 5800 | <div title="Copy" class="mes_button mes_copy fa-solid fa-copy " data-i18n="[title]Copy"></div> | 5800 | <div title="Copy" class="mes_button mes_copy fa-solid fa-copy " data-i18n="[title]Copy"></div> |
| 5801 | </div> | 5801 | </div> |
| 5802 | <div title="Open checkpoint chat" class="mes_button mes_bookmark fa-solid fa-flag" data-i18n="[title]Open checkpoint chat"></div> | 5802 | <div data-tooltip="Click to open checkpoint chat Shift+Click to replace the existing checkpoint with a new one" class="mes_button mes_bookmark fa-solid fa-flag" data-i18n="[data-tooltip]Open checkpoint chat Shift+Click to replace the existing checkpoint with a new one"></div> |
| 5803 | <div title="Edit" class="mes_button mes_edit fa-solid fa-pencil " data-i18n="[title]Edit"></div> | 5803 | <div title="Edit" class="mes_button mes_edit fa-solid fa-pencil " data-i18n="[title]Edit"></div> |
| 5804 | </div> | 5804 | </div> |
| 5805 | <div class="mes_edit_buttons"> | 5805 | <div class="mes_edit_buttons"> |
| @@ -117,9 +117,9 @@ import { | |||
| 117 | } from './scripts/nai-settings.js'; | 117 | } from './scripts/nai-settings.js'; |
| 118 | 118 | ||
| 119 | import { | 119 | import { |
| 120 | createNewBookmark, | 120 | initBookmarks, |
| 121 | showBookmarksButtons, | 121 | showBookmarksButtons, |
| 122 | createBranch, | 122 | updateBookmarkDisplay, |
| 123 | } from './scripts/bookmarks.js'; | 123 | } from './scripts/bookmarks.js'; |
| 124 | 124 | ||
| 125 | import { | 125 | import { |
| @@ -558,8 +558,6 @@ export const system_message_types = { | |||
| 558 | GROUP: 'group', | 558 | GROUP: 'group', |
| 559 | EMPTY: 'empty', | 559 | EMPTY: 'empty', |
| 560 | GENERIC: 'generic', | 560 | GENERIC: 'generic', |
| 561 | BOOKMARK_CREATED: 'bookmark_created', | ||
| 562 | BOOKMARK_BACK: 'bookmark_back', | ||
| 563 | NARRATOR: 'narrator', | 561 | NARRATOR: 'narrator', |
| 564 | COMMENT: 'comment', | 562 | COMMENT: 'comment', |
| 565 | SLASH_COMMANDS: 'slash_commands', | 563 | SLASH_COMMANDS: 'slash_commands', |
| @@ -671,20 +669,6 @@ async function getSystemMessages() { | |||
| 671 | is_system: true, | 669 | is_system: true, |
| 672 | mes: 'Generic system message. User `text` parameter to override the contents', | 670 | mes: 'Generic system message. User `text` parameter to override the contents', |
| 673 | }, | 671 | }, |
| 674 | bookmark_created: { | ||
| 675 | name: systemUserName, | ||
| 676 | force_avatar: system_avatar, | ||
| 677 | is_user: false, | ||
| 678 | is_system: true, | ||
| 679 | mes: 'Checkpoint created! Click here to open the checkpoint chat: <a class="bookmark_link" file_name="{0}" href="javascript:void(null);">{1}</a>', | ||
| 680 | }, | ||
| 681 | bookmark_back: { | ||
| 682 | name: systemUserName, | ||
| 683 | force_avatar: system_avatar, | ||
| 684 | is_user: false, | ||
| 685 | is_system: true, | ||
| 686 | mes: 'Click here to return to the previous chat: <a class="bookmark_link" file_name="{0}" href="javascript:void(null);">Return</a>', | ||
| 687 | }, | ||
| 688 | welcome_prompt: { | 672 | welcome_prompt: { |
| 689 | name: systemUserName, | 673 | name: systemUserName, |
| 690 | force_avatar: system_avatar, | 674 | force_avatar: system_avatar, |
| @@ -954,6 +938,7 @@ async function firstLoadInit() { | |||
| 954 | initDynamicStyles(); | 938 | initDynamicStyles(); |
| 955 | initTags(); | 939 | initTags(); |
| 956 | initOpenai(); | 940 | initOpenai(); |
| 941 | initBookmarks(); | ||
| 957 | await getUserAvatars(true, user_avatar); | 942 | await getUserAvatars(true, user_avatar); |
| 958 | await getCharacters(); | 943 | await getCharacters(); |
| 959 | await getBackgrounds(); | 944 | await getBackgrounds(); |
| @@ -2128,6 +2113,7 @@ function getMessageFromTemplate({ | |||
| 2128 | tokenCount && mes.find('.tokenCounterDisplay').text(`${tokenCount}t`); | 2113 | tokenCount && mes.find('.tokenCounterDisplay').text(`${tokenCount}t`); |
| 2129 | title && mes.attr('title', title); | 2114 | title && mes.attr('title', title); |
| 2130 | timerValue && mes.find('.mes_timer').attr('title', timerTitle).text(timerValue); | 2115 | timerValue && mes.find('.mes_timer').attr('title', timerTitle).text(timerValue); |
| 2116 | bookmarkLink && updateBookmarkDisplay(mes); | ||
| 2131 | 2117 | ||
| 2132 | if (power_user.timestamp_model_icon && extra?.api) { | 2118 | if (power_user.timestamp_model_icon && extra?.api) { |
| 2133 | insertSVGIcon(mes, extra); | 2119 | insertSVGIcon(mes, extra); |
| @@ -8246,29 +8232,6 @@ function swipe_left() { // when we swipe left..but no generation. | |||
| 8246 | } | 8232 | } |
| 8247 | } | 8233 | } |
| 8248 | 8234 | ||
| 8249 | /** | ||
| 8250 | * Creates a new branch from the message with the given ID | ||
| 8251 | * @param {number} mesId Message ID | ||
| 8252 | * @returns {Promise<string>} Branch file name | ||
| 8253 | */ | ||
| 8254 | async function branchChat(mesId) { | ||
| 8255 | if (this_chid === undefined && !selected_group) { | ||
| 8256 | toastr.info('No character selected.', 'Branch creation aborted'); | ||
| 8257 | return; | ||
| 8258 | } | ||
| 8259 | |||
| 8260 | const fileName = await createBranch(mesId); | ||
| 8261 | await saveItemizedPrompts(fileName); | ||
| 8262 | |||
| 8263 | if (selected_group) { | ||
| 8264 | await openGroupChat(selected_group, fileName); | ||
| 8265 | } else { | ||
| 8266 | await openCharacterChat(fileName); | ||
| 8267 | } | ||
| 8268 | |||
| 8269 | return fileName; | ||
| 8270 | } | ||
| 8271 | |||
| 8272 | // when we click swipe right button | 8235 | // when we click swipe right button |
| 8273 | const swipe_right = () => { | 8236 | const swipe_right = () => { |
| 8274 | if (chat.length - 1 === Number(this_edit_mes_id)) { | 8237 | if (chat.length - 1 === Number(this_edit_mes_id)) { |
| @@ -10582,44 +10545,6 @@ jQuery(async function () { | |||
| 10582 | await duplicateCharacter(); | 10545 | await duplicateCharacter(); |
| 10583 | }); | 10546 | }); |
| 10584 | 10547 | ||
| 10585 | $(document).on('click', '.select_chat_block, .bookmark_link, .mes_bookmark', async function () { | ||
| 10586 | let file_name = $(this).hasClass('mes_bookmark') | ||
| 10587 | ? $(this).closest('.mes').attr('bookmark_link') | ||
| 10588 | : $(this).attr('file_name').replace('.jsonl', ''); | ||
| 10589 | |||
| 10590 | if (!file_name) { | ||
| 10591 | return; | ||
| 10592 | } | ||
| 10593 | |||
| 10594 | try { | ||
| 10595 | showLoader(); | ||
| 10596 | if (selected_group) { | ||
| 10597 | await openGroupChat(selected_group, file_name); | ||
| 10598 | } else { | ||
| 10599 | await openCharacterChat(file_name); | ||
| 10600 | } | ||
| 10601 | } finally { | ||
| 10602 | hideLoader(); | ||
| 10603 | } | ||
| 10604 | |||
| 10605 | $('#shadow_select_chat_popup').css('display', 'none'); | ||
| 10606 | $('#load_select_chat_div').css('display', 'block'); | ||
| 10607 | }); | ||
| 10608 | |||
| 10609 | $(document).on('click', '.mes_create_bookmark', async function () { | ||
| 10610 | var selected_mes_id = $(this).closest('.mes').attr('mesid'); | ||
| 10611 | if (selected_mes_id !== undefined) { | ||
| 10612 | createNewBookmark(selected_mes_id); | ||
| 10613 | } | ||
| 10614 | }); | ||
| 10615 | |||
| 10616 | $(document).on('click', '.mes_create_branch', async function () { | ||
| 10617 | var selected_mes_id = $(this).closest('.mes').attr('mesid'); | ||
| 10618 | if (selected_mes_id !== undefined) { | ||
| 10619 | branchChat(Number(selected_mes_id)); | ||
| 10620 | } | ||
| 10621 | }); | ||
| 10622 | |||
| 10623 | $(document).on('click', '.mes_stop', function () { | 10548 | $(document).on('click', '.mes_stop', function () { |
| 10624 | stopGeneration(); | 10549 | stopGeneration(); |
| 10625 | }); | 10550 | }); |
| @@ -6,7 +6,6 @@ import { | |||
| 6 | this_chid, | 6 | this_chid, |
| 7 | openCharacterChat, | 7 | openCharacterChat, |
| 8 | chat_metadata, | 8 | chat_metadata, |
| 9 | callPopup, | ||
| 10 | getRequestHeaders, | 9 | getRequestHeaders, |
| 11 | getThumbnailUrl, | 10 | getThumbnailUrl, |
| 12 | getCharacters, | 11 | getCharacters, |
| @@ -24,19 +23,21 @@ import { | |||
| 24 | saveGroupBookmarkChat, | 23 | saveGroupBookmarkChat, |
| 25 | selected_group, | 24 | selected_group, |
| 26 | } from './group-chats.js'; | 25 | } from './group-chats.js'; |
| 26 | import { hideLoader, showLoader } from './loader.js'; | ||
| 27 | import { getLastMessageId } from './macros.js'; | ||
| 27 | import { Popup } from './popup.js'; | 28 | import { Popup } from './popup.js'; |
| 29 | import { SlashCommand } from './slash-commands/SlashCommand.js'; | ||
| 30 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; | ||
| 31 | import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js'; | ||
| 32 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; | ||
| 28 | import { createTagMapFromList } from './tags.js'; | 33 | import { createTagMapFromList } from './tags.js'; |
| 34 | import { renderTemplateAsync } from './templates.js'; | ||
| 29 | 35 | ||
| 30 | import { | 36 | import { |
| 31 | delay, | ||
| 32 | getUniqueName, | 37 | getUniqueName, |
| 38 | isTrueBoolean, | ||
| 33 | } from './utils.js'; | 39 | } from './utils.js'; |
| 34 | 40 | ||
| 35 | export { | ||
| 36 | createNewBookmark, | ||
| 37 | showBookmarksButtons, | ||
| 38 | }; | ||
| 39 | |||
| 40 | const bookmarkNameToken = 'Checkpoint #'; | 41 | const bookmarkNameToken = 'Checkpoint #'; |
| 41 | 42 | ||
| 42 | async function getExistingChatNames() { | 43 | async function getExistingChatNames() { |
| @@ -57,16 +58,13 @@ async function getExistingChatNames() { | |||
| 57 | } | 58 | } |
| 58 | } | 59 | } |
| 59 | 60 | ||
| 60 | async function getBookmarkName() { | 61 | async function getBookmarkName({ isReplace = false, forceName = null } = {}) { |
| 61 | const chatNames = await getExistingChatNames(); | 62 | const chatNames = await getExistingChatNames(); |
| 62 | const popupText = `<h3>Enter the checkpoint name:<h3> | ||
| 63 | <small>Leave empty to auto-generate.</small>`; | ||
| 64 | let name = await callPopup(popupText, 'input'); | ||
| 65 | 63 | ||
| 66 | if (name === false) { | 64 | const body = await renderTemplateAsync('createCheckpoint', { isReplace: isReplace }); |
| 67 | return null; | 65 | let name = forceName ?? await Popup.show.input('Create Checkpoint', body); |
| 68 | } | 66 | // Special handling for confirmed empty input (=> auto-generate name) |
| 69 | else if (name === '') { | 67 | if (name === '') { |
| 70 | for (let i = chatNames.length; i < 1000; i++) { | 68 | for (let i = chatNames.length; i < 1000; i++) { |
| 71 | name = bookmarkNameToken + i; | 69 | name = bookmarkNameToken + i; |
| 72 | if (!chatNames.includes(name)) { | 70 | if (!chatNames.includes(name)) { |
| @@ -74,6 +72,9 @@ async function getBookmarkName() { | |||
| 74 | } | 72 | } |
| 75 | } | 73 | } |
| 76 | } | 74 | } |
| 75 | if (!name) { | ||
| 76 | return null; | ||
| 77 | } | ||
| 77 | 78 | ||
| 78 | return `${name} - ${humanizedDateTime()}`; | 79 | return `${name} - ${humanizedDateTime()}`; |
| 79 | } | 80 | } |
| @@ -96,7 +97,7 @@ function getMainChatName() { | |||
| 96 | return null; | 97 | return null; |
| 97 | } | 98 | } |
| 98 | 99 | ||
| 99 | function showBookmarksButtons() { | 100 | export function showBookmarksButtons() { |
| 100 | try { | 101 | try { |
| 101 | if (selected_group) { | 102 | if (selected_group) { |
| 102 | $('#option_convert_to_group').hide(); | 103 | $('#option_convert_to_group').hide(); |
| @@ -131,9 +132,10 @@ async function saveBookmarkMenu() { | |||
| 131 | return; | 132 | return; |
| 132 | } | 133 | } |
| 133 | 134 | ||
| 134 | return createNewBookmark(chat.length - 1); | 135 | return await createNewBookmark(chat.length - 1); |
| 135 | } | 136 | } |
| 136 | 137 | ||
| 138 | // Export is used by Timelines extension. Do not remove. | ||
| 137 | export async function createBranch(mesId) { | 139 | export async function createBranch(mesId) { |
| 138 | if (!chat.length) { | 140 | if (!chat.length) { |
| 139 | toastr.warning('The chat is empty.', 'Branch creation failed'); | 141 | toastr.warning('The chat is empty.', 'Branch creation failed'); |
| @@ -167,20 +169,26 @@ export async function createBranch(mesId) { | |||
| 167 | return name; | 169 | return name; |
| 168 | } | 170 | } |
| 169 | 171 | ||
| 170 | async function createNewBookmark(mesId) { | 172 | /** |
| 173 | * Creates a new bookmark for a message. | ||
| 174 | * | ||
| 175 | * @param {number} mesId - The ID of the message. | ||
| 176 | * @param {Object} [options={}] - Optional parameters. | ||
| 177 | * @param {string?} [options.forceName=null] - The name to force for the bookmark. | ||
| 178 | * @returns {Promise<string?>} - A promise that resolves to the bookmark name when the bookmark is created. | ||
| 179 | */ | ||
| 180 | export async function createNewBookmark(mesId, { forceName = null } = {}) { | ||
| 171 | if (this_chid === undefined && !selected_group) { | 181 | if (this_chid === undefined && !selected_group) { |
| 172 | toastr.info('No character selected.', 'Checkpoint creation aborted'); | 182 | toastr.info('No character selected.', 'Create Checkpoint'); |
| 173 | return; | 183 | return null; |
| 174 | } | 184 | } |
| 175 | |||
| 176 | if (!chat.length) { | 185 | if (!chat.length) { |
| 177 | toastr.warning('The chat is empty.', 'Checkpoint creation failed'); | 186 | toastr.warning('The chat is empty.', 'Create Checkpoint'); |
| 178 | return; | 187 | return null; |
| 179 | } | 188 | } |
| 180 | 189 | if (!chat[mesId]) { | |
| 181 | if (mesId < 0 || mesId >= chat.length) { | 190 | toastr.warning('Invalid message ID.', 'Create Checkpoint'); |
| 182 | toastr.warning('Invalid message ID.', 'Checkpoint creation failed'); | 191 | return null; |
| 183 | return; | ||
| 184 | } | 192 | } |
| 185 | 193 | ||
| 186 | const lastMes = chat[mesId]; | 194 | const lastMes = chat[mesId]; |
| @@ -189,19 +197,11 @@ async function createNewBookmark(mesId) { | |||
| 189 | lastMes.extra = {}; | 197 | lastMes.extra = {}; |
| 190 | } | 198 | } |
| 191 | 199 | ||
| 192 | if (lastMes.extra.bookmark_link) { | 200 | const isReplace = lastMes.extra.bookmark_link; |
| 193 | const confirm = await callPopup('Checkpoint for the last message already exists. Would you like to replace it?', 'confirm'); | ||
| 194 | |||
| 195 | if (!confirm) { | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | await delay(250); | ||
| 201 | let name = await getBookmarkName(); | ||
| 202 | 201 | ||
| 202 | let name = await getBookmarkName({ isReplace: isReplace, forceName: forceName }); | ||
| 203 | if (!name) { | 203 | if (!name) { |
| 204 | return; | 204 | return null; |
| 205 | } | 205 | } |
| 206 | 206 | ||
| 207 | const mainChat = selected_group ? groups?.find(x => x.id == selected_group)?.chat_id : characters[this_chid].chat; | 207 | const mainChat = selected_group ? groups?.find(x => x.id == selected_group)?.chat_id : characters[this_chid].chat; |
| @@ -215,10 +215,25 @@ async function createNewBookmark(mesId) { | |||
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | lastMes.extra['bookmark_link'] = name; | 217 | lastMes.extra['bookmark_link'] = name; |
| 218 | $(`.mes[mesid="${mesId}"]`).attr('bookmark_link', name); | 218 | |
| 219 | const mes = $(`.mes[mesid="${mesId}"]`); | ||
| 220 | updateBookmarkDisplay(mes, name); | ||
| 219 | 221 | ||
| 220 | await saveChatConditional(); | 222 | await saveChatConditional(); |
| 221 | toastr.success('Click the flag icon in the last message to open the checkpoint chat.', 'Checkpoint created', { timeOut: 10000 }); | 223 | toastr.success('Click the flag icon next to the message to open the checkpoint chat.', 'Create Checkpoint', { timeOut: 10000 }); |
| 224 | return name; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | /** | ||
| 229 | * Updates the display of the bookmark on a chat message. | ||
| 230 | * @param {JQuery<HTMLElement>} mes - The message element | ||
| 231 | * @param {string?} [newBookmarkLink=null] - The new bookmark link (optional) | ||
| 232 | */ | ||
| 233 | export function updateBookmarkDisplay(mes, newBookmarkLink = null) { | ||
| 234 | newBookmarkLink && mes.attr('bookmark_link', newBookmarkLink); | ||
| 235 | const bookmarkFlag = mes.find('.mes_bookmark'); | ||
| 236 | bookmarkFlag.attr('title', `Checkpoint\n${mes.attr('bookmark_link')}\n\n${bookmarkFlag.data('tooltip')}`); | ||
| 222 | } | 237 | } |
| 223 | 238 | ||
| 224 | async function backToMainChat() { | 239 | async function backToMainChat() { |
| @@ -231,10 +246,13 @@ async function backToMainChat() { | |||
| 231 | } else { | 246 | } else { |
| 232 | await openCharacterChat(mainChatName); | 247 | await openCharacterChat(mainChatName); |
| 233 | } | 248 | } |
| 249 | return mainChatName; | ||
| 234 | } | 250 | } |
| 251 | |||
| 252 | return null; | ||
| 235 | } | 253 | } |
| 236 | 254 | ||
| 237 | async function convertSoloToGroupChat() { | 255 | export async function convertSoloToGroupChat() { |
| 238 | if (selected_group) { | 256 | if (selected_group) { |
| 239 | console.log('Already in group. No need for conversion'); | 257 | console.log('Already in group. No need for conversion'); |
| 240 | return; | 258 | return; |
| @@ -261,6 +279,7 @@ async function convertSoloToGroupChat() { | |||
| 261 | const activationStrategy = group_activation_strategy.NATURAL; | 279 | const activationStrategy = group_activation_strategy.NATURAL; |
| 262 | const allowSelfResponses = false; | 280 | const allowSelfResponses = false; |
| 263 | const favChecked = character.fav || character.fav == 'true'; | 281 | const favChecked = character.fav || character.fav == 'true'; |
| 282 | /** @type {any} */ | ||
| 264 | const metadata = Object.assign({}, chat_metadata); | 283 | const metadata = Object.assign({}, chat_metadata); |
| 265 | delete metadata.main_chat; | 284 | delete metadata.main_chat; |
| 266 | 285 | ||
| @@ -351,8 +370,288 @@ async function convertSoloToGroupChat() { | |||
| 351 | toastr.success('The chat has been successfully converted!'); | 370 | toastr.success('The chat has been successfully converted!'); |
| 352 | } | 371 | } |
| 353 | 372 | ||
| 354 | jQuery(function () { | 373 | /** |
| 374 | * Creates a new branch from the message with the given ID | ||
| 375 | * @param {number} mesId Message ID | ||
| 376 | * @returns {Promise<string?>} Branch file name | ||
| 377 | */ | ||
| 378 | export async function branchChat(mesId) { | ||
| 379 | if (this_chid === undefined && !selected_group) { | ||
| 380 | toastr.info('No character selected.', 'Create Branch'); | ||
| 381 | return null; | ||
| 382 | } | ||
| 383 | |||
| 384 | const fileName = await createBranch(mesId); | ||
| 385 | await saveItemizedPrompts(fileName); | ||
| 386 | |||
| 387 | if (selected_group) { | ||
| 388 | await openGroupChat(selected_group, fileName); | ||
| 389 | } else { | ||
| 390 | await openCharacterChat(fileName); | ||
| 391 | } | ||
| 392 | |||
| 393 | return fileName; | ||
| 394 | } | ||
| 395 | |||
| 396 | function registerBookmarksSlashCommands() { | ||
| 397 | /** | ||
| 398 | * Validates a message ID. (Is a number, exists as a message) | ||
| 399 | * | ||
| 400 | * @param {number} mesId - The message ID to validate. | ||
| 401 | * @param {string} context - The context of the slash command. Will be used as the title of any toasts. | ||
| 402 | * @returns {boolean} - Returns true if the message ID is valid, otherwise false. | ||
| 403 | */ | ||
| 404 | function validateMessageId(mesId, context) { | ||
| 405 | if (isNaN(mesId)) { | ||
| 406 | toastr.warning('Invalid message ID was provided', context); | ||
| 407 | return false; | ||
| 408 | } | ||
| 409 | if (!chat[mesId]) { | ||
| 410 | toastr.warning(`Message for id ${mesId} not found`, context); | ||
| 411 | return false; | ||
| 412 | } | ||
| 413 | return true; | ||
| 414 | } | ||
| 415 | |||
| 416 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | ||
| 417 | name: 'branch-create', | ||
| 418 | returns: 'Name of the new branch', | ||
| 419 | callback: async (args, text) => { | ||
| 420 | const mesId = Number(args.mesId ?? text ?? getLastMessageId()); | ||
| 421 | if (!validateMessageId(mesId, 'Create Branch')) return ''; | ||
| 422 | |||
| 423 | const branchName = await branchChat(mesId); | ||
| 424 | return branchName ?? ''; | ||
| 425 | }, | ||
| 426 | unnamedArgumentList: [ | ||
| 427 | SlashCommandArgument.fromProps({ | ||
| 428 | description: 'Message ID', | ||
| 429 | typeList: [ARGUMENT_TYPE.NUMBER], | ||
| 430 | enumProvider: commonEnumProviders.messages(), | ||
| 431 | }), | ||
| 432 | ], | ||
| 433 | helpString: ` | ||
| 434 | <div> | ||
| 435 | Create a new branch from the selected message. If no message id is provided, will use the last message. | ||
| 436 | </div> | ||
| 437 | <div> | ||
| 438 | Creating a branch will automatically choose a name for the branch.<br /> | ||
| 439 | After creating the branch, the branch chat will be automatically opened. | ||
| 440 | </div> | ||
| 441 | <div> | ||
| 442 | Use Checkpoints and <code>/checkpoint-create</code> instead if you do not want to jump to the new chat. | ||
| 443 | </div>`, | ||
| 444 | })); | ||
| 445 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | ||
| 446 | name: 'checkpoint-create', | ||
| 447 | returns: 'Name of the new checkpoint', | ||
| 448 | callback: async (args, text) => { | ||
| 449 | const mesId = Number(args.mesId ?? getLastMessageId()); | ||
| 450 | if (!validateMessageId(mesId, 'Create Checkpoint')) return ''; | ||
| 451 | |||
| 452 | if (typeof text !== 'string') { | ||
| 453 | toastr.warning('Checkpoint name must be a string or empty', 'Create Checkpoint'); | ||
| 454 | return ''; | ||
| 455 | } | ||
| 456 | |||
| 457 | const checkPointName = await createNewBookmark(mesId, { forceName: text }); | ||
| 458 | return checkPointName ?? ''; | ||
| 459 | }, | ||
| 460 | namedArgumentList: [ | ||
| 461 | SlashCommandNamedArgument.fromProps({ | ||
| 462 | name: 'mesId', | ||
| 463 | description: 'Message ID', | ||
| 464 | typeList: [ARGUMENT_TYPE.NUMBER], | ||
| 465 | enumProvider: commonEnumProviders.messages(), | ||
| 466 | }), | ||
| 467 | ], | ||
| 468 | unnamedArgumentList: [ | ||
| 469 | SlashCommandArgument.fromProps({ | ||
| 470 | description: 'Checkpoint name', | ||
| 471 | typeList: [ARGUMENT_TYPE.STRING], | ||
| 472 | }), | ||
| 473 | ], | ||
| 474 | helpString: ` | ||
| 475 | <div> | ||
| 476 | Create a new checkpoint for the selected message with the provided name. If no message id is provided, will use the last message.<br /> | ||
| 477 | Leave the checkpoint name empty to auto-generate one. | ||
| 478 | </div> | ||
| 479 | <div> | ||
| 480 | A created checkpoint will be permanently linked with the message.<br /> | ||
| 481 | If a checkpoint already exists, the link to it will be overwritten.<br /> | ||
| 482 | After creating the checkpoint, the checkpoint chat can be opened with the checkpoint flag, | ||
| 483 | using the <code>/go</code> command with the checkpoint name or the <code>/checkpoint-go</code> command on the message. | ||
| 484 | </div> | ||
| 485 | <div> | ||
| 486 | Use Branches and <code>/branch-create</code> instead if you do want to jump to the new chat. | ||
| 487 | </div> | ||
| 488 | <div> | ||
| 489 | <strong>Example:</strong> | ||
| 490 | <ul> | ||
| 491 | <li> | ||
| 492 | <pre><code>/checkpoint-create mes={{lastCharMessage}} Checkpoint for char reply | /setvar key=rememberCheckpoint {{pipe}}</code></pre> | ||
| 493 | Will create a new checkpoint to the latest message of the current character, and save it as a local variable for future use. | ||
| 494 | </li> | ||
| 495 | </ul> | ||
| 496 | </div>`, | ||
| 497 | })); | ||
| 498 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | ||
| 499 | name: 'checkpoint-go', | ||
| 500 | returns: 'Name of the checkpoint', | ||
| 501 | callback: async (args, text) => { | ||
| 502 | const mesId = Number(args.mesId ?? text ?? getLastMessageId()); | ||
| 503 | if (!validateMessageId(mesId, 'Open Checkpoint')) return ''; | ||
| 504 | |||
| 505 | const checkPointName = chat[mesId].extra?.bookmark_link; | ||
| 506 | if (!checkPointName) { | ||
| 507 | toastr.warning('No checkpoint is linked to the selected message', 'Open Checkpoint'); | ||
| 508 | return ''; | ||
| 509 | } | ||
| 510 | |||
| 511 | if (selected_group) { | ||
| 512 | await openGroupChat(selected_group, checkPointName); | ||
| 513 | } else { | ||
| 514 | await openCharacterChat(checkPointName); | ||
| 515 | } | ||
| 516 | |||
| 517 | return checkPointName; | ||
| 518 | }, | ||
| 519 | unnamedArgumentList: [ | ||
| 520 | SlashCommandArgument.fromProps({ | ||
| 521 | description: 'Message ID', | ||
| 522 | typeList: [ARGUMENT_TYPE.NUMBER], | ||
| 523 | enumProvider: commonEnumProviders.messages(), | ||
| 524 | }), | ||
| 525 | ], | ||
| 526 | helpString: ` | ||
| 527 | <div> | ||
| 528 | Open the checkpoint linked to the selected message. If no message id is provided, will use the last message. | ||
| 529 | </div> | ||
| 530 | <div> | ||
| 531 | Use <code>/checkpoint-get</code> if you want to make sure that the selected message has a checkpoint. | ||
| 532 | </div>`, | ||
| 533 | })); | ||
| 534 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | ||
| 535 | name: 'checkpoint-exit', | ||
| 536 | returns: 'The name of the chat exited to. Returns an empty string if not in a checkpoint chat.', | ||
| 537 | callback: async () => { | ||
| 538 | const mainChat = await backToMainChat(); | ||
| 539 | return mainChat ?? ''; | ||
| 540 | }, | ||
| 541 | helpString: 'Exit the checkpoint chat.<br />If not in a checkpoint chat, returns empty string.', | ||
| 542 | })); | ||
| 543 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | ||
| 544 | name: 'checkpoint-parent', | ||
| 545 | returns: 'Name of the parent chat for this checkpoint', | ||
| 546 | callback: async () => { | ||
| 547 | const mainChatName = getMainChatName(); | ||
| 548 | return mainChatName ?? ''; | ||
| 549 | }, | ||
| 550 | helpString: 'Get the name of the parent chat for this checkpoint.<br />If not in a checkpoint chat, returns empty string.', | ||
| 551 | })); | ||
| 552 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | ||
| 553 | name: 'checkpoint-get', | ||
| 554 | returns: 'Name of the chat', | ||
| 555 | callback: async (args, text) => { | ||
| 556 | const mesId = Number(args.mesId ?? text ?? getLastMessageId()); | ||
| 557 | if (!validateMessageId(mesId, 'Get Checkpoint')) return ''; | ||
| 558 | |||
| 559 | const checkPointName = chat[mesId].extra?.bookmark_link; | ||
| 560 | return checkPointName ?? ''; | ||
| 561 | }, | ||
| 562 | unnamedArgumentList: [ | ||
| 563 | SlashCommandArgument.fromProps({ | ||
| 564 | description: 'Message ID', | ||
| 565 | typeList: [ARGUMENT_TYPE.NUMBER], | ||
| 566 | enumProvider: commonEnumProviders.messages(), | ||
| 567 | }), | ||
| 568 | ], | ||
| 569 | helpString: ` | ||
| 570 | <div> | ||
| 571 | Get the name of the checkpoint linked to the selected message. If no message id is provided, will use the last message.<br /> | ||
| 572 | If no checkpoint is linked, the result will be empty. | ||
| 573 | </div>`, | ||
| 574 | })); | ||
| 575 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ | ||
| 576 | name: 'checkpoint-list', | ||
| 577 | returns: 'JSON array of all existing checkpoints in this chat, as an array', | ||
| 578 | /** @param {{links?: string}} args @returns {Promise<string>} */ | ||
| 579 | callback: async (args, _) => { | ||
| 580 | const result = Object.entries(chat) | ||
| 581 | .filter(([_, message]) => message.extra?.bookmark_link) | ||
| 582 | .map(([mesId, message]) => isTrueBoolean(args.links) ? message.extra.bookmark_link : Number(mesId)); | ||
| 583 | return JSON.stringify(result); | ||
| 584 | }, | ||
| 585 | namedArgumentList: [ | ||
| 586 | SlashCommandNamedArgument.fromProps({ | ||
| 587 | name: 'links', | ||
| 588 | description: 'Get a list of all links / chat names of the checkpoints, instead of the message ids', | ||
| 589 | typeList: [ARGUMENT_TYPE.BOOLEAN], | ||
| 590 | enumList: commonEnumProviders.boolean('trueFalse')(), | ||
| 591 | defaultValue: 'false', | ||
| 592 | }), | ||
| 593 | ], | ||
| 594 | helpString: ` | ||
| 595 | <div> | ||
| 596 | List all existing checkpoints in this chat. | ||
| 597 | </div> | ||
| 598 | <div> | ||
| 599 | Returns a list of all message ids that have a checkpoint, or all checkpoint links if <code>links</code> is set to <code>true</code>.<br /> | ||
| 600 | The value will be a JSON array. | ||
| 601 | </div>`, | ||
| 602 | })); | ||
| 603 | } | ||
| 604 | |||
| 605 | export function initBookmarks() { | ||
| 355 | $('#option_new_bookmark').on('click', saveBookmarkMenu); | 606 | $('#option_new_bookmark').on('click', saveBookmarkMenu); |
| 356 | $('#option_back_to_main').on('click', backToMainChat); | 607 | $('#option_back_to_main').on('click', backToMainChat); |
| 357 | $('#option_convert_to_group').on('click', convertSoloToGroupChat); | 608 | $('#option_convert_to_group').on('click', convertSoloToGroupChat); |
| 609 | |||
| 610 | $(document).on('click', '.select_chat_block, .mes_bookmark', async function (e) { | ||
| 611 | // If shift is held down, we are not following the bookmark, but creating a new one | ||
| 612 | const mes = $(this).closest('.mes'); | ||
| 613 | if (e.shiftKey && mes.length) { | ||
| 614 | const selectedMesId = mes.attr('mesid'); | ||
| 615 | await createNewBookmark(Number(selectedMesId)); | ||
| 616 | return; | ||
| 617 | } | ||
| 618 | |||
| 619 | const fileName = $(this).hasClass('mes_bookmark') | ||
| 620 | ? $(this).closest('.mes').attr('bookmark_link') | ||
| 621 | : $(this).attr('file_name').replace('.jsonl', ''); | ||
| 622 | |||
| 623 | if (!fileName) { | ||
| 624 | return; | ||
| 625 | } | ||
| 626 | |||
| 627 | try { | ||
| 628 | showLoader(); | ||
| 629 | if (selected_group) { | ||
| 630 | await openGroupChat(selected_group, fileName); | ||
| 631 | } else { | ||
| 632 | await openCharacterChat(fileName); | ||
| 633 | } | ||
| 634 | } finally { | ||
| 635 | await hideLoader(); | ||
| 636 | } | ||
| 637 | |||
| 638 | $('#shadow_select_chat_popup').css('display', 'none'); | ||
| 639 | $('#load_select_chat_div').css('display', 'block'); | ||
| 358 | }); | 640 | }); |
| 641 | |||
| 642 | $(document).on('click', '.mes_create_bookmark', async function () { | ||
| 643 | const mesId = $(this).closest('.mes').attr('mesid'); | ||
| 644 | if (mesId !== undefined) { | ||
| 645 | await createNewBookmark(Number(mesId)); | ||
| 646 | } | ||
| 647 | }); | ||
| 648 | |||
| 649 | $(document).on('click', '.mes_create_branch', async function () { | ||
| 650 | const mesId = $(this).closest('.mes').attr('mesid'); | ||
| 651 | if (mesId !== undefined) { | ||
| 652 | await branchChat(Number(mesId)); | ||
| 653 | } | ||
| 654 | }); | ||
| 655 | |||
| 656 | registerBookmarksSlashCommands(); | ||
| 657 | } | ||
| @@ -358,10 +358,10 @@ function onRefineModeInput() { | |||
| 358 | */ | 358 | */ |
| 359 | async function captionCommandCallback(args, prompt) { | 359 | async function captionCommandCallback(args, prompt) { |
| 360 | const quiet = isTrueBoolean(args?.quiet); | 360 | const quiet = isTrueBoolean(args?.quiet); |
| 361 | const id = args?.id; | 361 | const mesId = args?.mesId ?? args?.id; |
| 362 | 362 | ||
| 363 | if (!isNaN(Number(id))) { | 363 | if (!isNaN(Number(mesId))) { |
| 364 | const message = getContext().chat[id]; | 364 | const message = getContext().chat[mesId]; |
| 365 | if (message?.extra?.image) { | 365 | if (message?.extra?.image) { |
| 366 | try { | 366 | try { |
| 367 | const fetchResult = await fetch(message.extra.image); | 367 | const fetchResult = await fetch(message.extra.image); |
| @@ -546,7 +546,7 @@ jQuery(async function () { | |||
| 546 | 'quiet', 'suppress sending a captioned message', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false', | 546 | 'quiet', 'suppress sending a captioned message', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false', |
| 547 | ), | 547 | ), |
| 548 | SlashCommandNamedArgument.fromProps({ | 548 | SlashCommandNamedArgument.fromProps({ |
| 549 | name: 'id', | 549 | name: 'mesId', |
| 550 | description: 'get image from a message with this ID', | 550 | description: 'get image from a message with this ID', |
| 551 | typeList: [ARGUMENT_TYPE.NUMBER], | 551 | typeList: [ARGUMENT_TYPE.NUMBER], |
| 552 | enumProvider: commonEnumProviders.messages(), | 552 | enumProvider: commonEnumProviders.messages(), |
| @@ -598,9 +598,20 @@ jQuery(async () => { | |||
| 598 | $(document).on('click', '.mes_translate', onMessageTranslateClick); | 598 | $(document).on('click', '.mes_translate', onMessageTranslateClick); |
| 599 | $('#translate_key_button').on('click', async () => { | 599 | $('#translate_key_button').on('click', async () => { |
| 600 | const optionText = $('#translation_provider option:selected').text(); | 600 | const optionText = $('#translation_provider option:selected').text(); |
| 601 | const key = await callGenericPopup(`<h3>${optionText} API Key</h3>`, POPUP_TYPE.INPUT); | 601 | const key = await callGenericPopup(`<h3>${optionText} API Key</h3>`, POPUP_TYPE.INPUT, '', { |
| 602 | customButtons: [{ | ||
| 603 | text: 'Remove Key', | ||
| 604 | appendAtEnd: true, | ||
| 605 | result: POPUP_RESULT.NEGATIVE, | ||
| 606 | action: async () => { | ||
| 607 | await writeSecret(extension_settings.translate.provider, ''); | ||
| 608 | toastr.success('API Key removed'); | ||
| 609 | $('#translate_key_button').toggleClass('success', !!secret_state[extension_settings.translate.provider]); | ||
| 610 | }, | ||
| 611 | }], | ||
| 612 | }); | ||
| 602 | 613 | ||
| 603 | if (key == false) { | 614 | if (!key) { |
| 604 | return; | 615 | return; |
| 605 | } | 616 | } |
| 606 | 617 | ||
| @@ -634,7 +645,7 @@ jQuery(async () => { | |||
| 634 | }], | 645 | }], |
| 635 | }); | 646 | }); |
| 636 | 647 | ||
| 637 | if (url == false || url == '') { | 648 | if (!url) { |
| 638 | return; | 649 | return; |
| 639 | } | 650 | } |
| 640 | 651 | ||
| @@ -77,14 +77,14 @@ class AzureTtsProvider { | |||
| 77 | result: POPUP_RESULT.NEGATIVE, | 77 | result: POPUP_RESULT.NEGATIVE, |
| 78 | action: async () => { | 78 | action: async () => { |
| 79 | await writeSecret(SECRET_KEYS.AZURE_TTS, ''); | 79 | await writeSecret(SECRET_KEYS.AZURE_TTS, ''); |
| 80 | $('#azure_tts_key').toggleClass('success', secret_state[SECRET_KEYS.AZURE_TTS]); | 80 | $('#azure_tts_key').toggleClass('success', !!secret_state[SECRET_KEYS.AZURE_TTS]); |
| 81 | toastr.success('API Key removed'); | 81 | toastr.success('API Key removed'); |
| 82 | await this.onRefreshClick(); | 82 | await this.onRefreshClick(); |
| 83 | }, | 83 | }, |
| 84 | }], | 84 | }], |
| 85 | }); | 85 | }); |
| 86 | 86 | ||
| 87 | if (key == false || key == '') { | 87 | if (!key) { |
| 88 | return; | 88 | return; |
| 89 | } | 89 | } |
| 90 | 90 | ||
| @@ -86,14 +86,14 @@ class OpenAICompatibleTtsProvider { | |||
| 86 | result: POPUP_RESULT.NEGATIVE, | 86 | result: POPUP_RESULT.NEGATIVE, |
| 87 | action: async () => { | 87 | action: async () => { |
| 88 | await writeSecret(SECRET_KEYS.CUSTOM_OPENAI_TTS, ''); | 88 | await writeSecret(SECRET_KEYS.CUSTOM_OPENAI_TTS, ''); |
| 89 | $('#openai_compatible_tts_key').toggleClass('success', secret_state[SECRET_KEYS.CUSTOM_OPENAI_TTS]); | 89 | $('#openai_compatible_tts_key').toggleClass('success', !!secret_state[SECRET_KEYS.CUSTOM_OPENAI_TTS]); |
| 90 | toastr.success('API Key removed'); | 90 | toastr.success('API Key removed'); |
| 91 | await this.onRefreshClick(); | 91 | await this.onRefreshClick(); |
| 92 | }, | 92 | }, |
| 93 | }], | 93 | }], |
| 94 | }); | 94 | }); |
| 95 | 95 | ||
| 96 | if (key == false || key == '') { | 96 | if (!key) { |
| 97 | return; | 97 | return; |
| 98 | } | 98 | } |
| 99 | 99 | ||
| @@ -83,6 +83,9 @@ const showPopupHelper = { | |||
| 83 | const content = PopupUtils.BuildTextWithHeader(header, text); | 83 | const content = PopupUtils.BuildTextWithHeader(header, text); |
| 84 | const popup = new Popup(content, POPUP_TYPE.INPUT, defaultValue, popupOptions); | 84 | const popup = new Popup(content, POPUP_TYPE.INPUT, defaultValue, popupOptions); |
| 85 | const value = await popup.show(); | 85 | const value = await popup.show(); |
| 86 | // Return values: If empty string, we explicitly handle that as returning that empty string as "success" provided. | ||
| 87 | // Otherwise, all non-truthy values (false, null, undefined) are treated as "cancel" and return null. | ||
| 88 | if (value === '') return ''; | ||
| 86 | return value ? String(value) : null; | 89 | return value ? String(value) : null; |
| 87 | }, | 90 | }, |
| 88 | 91 | ||
| @@ -0,0 +1,8 @@ | |||
| 1 | <div> | ||
| 2 | <span class="margin-right-10px">Enter Checkpoint Name:</span><small>(Leave empty to auto-generate)</small> | ||
| 3 | </div> | ||
| 4 | {{#if isReplace}} | ||
| 5 | <div class="m-t-1"> | ||
| 6 | <small>The currently existing checkpoint will be unlinked and replaced with the new checkpoint, but can still be found in the Chat Management.</small> | ||
| 7 | </div> | ||
| 8 | {{/if}} | ||
| @@ -3964,6 +3964,10 @@ input[type="range"]::-webkit-slider-thumb { | |||
| 3964 | display: inline-block; | 3964 | display: inline-block; |
| 3965 | } | 3965 | } |
| 3966 | 3966 | ||
| 3967 | .mes:not([bookmark_link='']) .mes_create_bookmark { | ||
| 3968 | display: none; | ||
| 3969 | } | ||
| 3970 | |||
| 3967 | .mes_edit_buttons { | 3971 | .mes_edit_buttons { |
| 3968 | display: none; | 3972 | display: none; |
| 3969 | flex-direction: row; | 3973 | flex-direction: row; |