From 4f0921856f21bd8388a19246911965ee3d4f9fa9 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 17 Feb 2025 05:32:13 +0100 Subject: [PATCH 1/7] On char rename, update auxiliary connections - Move WI char lore (additional lorebooks) based on rename - Move character-bound Author's Note based on rename - Extend core `getCharFilename` to be able to take an avatarKey, instead of just uid --- public/script.js | 23 ++++++++++++++++++++++- public/scripts/utils.js | 15 ++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/public/script.js b/public/script.js index 9123ad32b..ebad7530b 100644 --- a/public/script.js +++ b/public/script.js @@ -493,6 +493,7 @@ export const event_types = { // TODO: Naming convention is inconsistent with other events CHARACTER_DELETED: 'characterDeleted', CHARACTER_DUPLICATED: 'character_duplicated', + CHARACTER_RENAMED: 'character_renamed', /** @deprecated The event is aliased to STREAM_TOKEN_RECEIVED. */ SMOOTH_STREAM_TOKEN_RECEIVED: 'stream_token_received', STREAM_TOKEN_RECEIVED: 'stream_token_received', @@ -6241,9 +6242,29 @@ export async function renameCharacter(name = null, { silent = false, renameChats const data = await response.json(); const newAvatar = data.avatar; - // Replace tags list + const oldName = getCharaFilename(null, { manualAvatarKey: oldAvatar }); + const newName = getCharaFilename(null, { manualAvatarKey: newAvatar }); + + // Replace other auxillery fields where was referenced by avatar key + // Tag List renameTagKey(oldAvatar, newAvatar); + // Addtional lore books + const charLore = world_info.charLore?.find(x => x.name == oldName); + if (charLore) { + charLore.name = newName; + saveSettingsDebounced(); + } + + // Char-bound Author's Notes + const charNote = extension_settings.note.chara?.find(x => x.name == oldName); + if (charNote) { + charNote.name = newName; + saveSettingsDebounced(); + } + + eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar); + // Reload characters list await getCharacters(); diff --git a/public/scripts/utils.js b/public/scripts/utils.js index b385a760a..00304daa6 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -994,13 +994,18 @@ export function getImageSizeFromDataURL(dataUrl) { }); } -export function getCharaFilename(chid) { +/** + * Gets the filename of the character avatar without extension + * @param {number?} [chid=null] - Character ID. If not provided, uses the current character ID + * @param {object} [options={}] - Options arguments + * @param {string?} [options.manualAvatarKey=null] - Manually take the following avatar key, instead of using the chid to determine the name + * @returns {string?} The filename of the character avatar without extension, or null if the character ID is invalid + */ +export function getCharaFilename(chid = null, { manualAvatarKey = null } = {}) { const context = getContext(); - const fileName = context.characters[chid ?? context.characterId]?.avatar; + const fileName = manualAvatarKey ?? context.characters[chid ?? context.characterId]?.avatar; - if (fileName) { - return fileName.replace(/\.[^/.]+$/, ''); - } + return fileName?.replace(/\.[^/.]+$/, '') ?? null; } /** From 1c0ca414b9ee073f47e8db230e6efe845b83a9f8 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 17 Feb 2025 05:34:28 +0100 Subject: [PATCH 2/7] Update "active character" field on char rename --- public/script.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/public/script.js b/public/script.js index ebad7530b..975f12065 100644 --- a/public/script.js +++ b/public/script.js @@ -6263,6 +6263,12 @@ export async function renameCharacter(name = null, { silent = false, renameChats saveSettingsDebounced(); } + // Update active character, if the current one was the currently active one + if (active_character === oldAvatar) { + active_character = newAvatar; + saveSettingsDebounced(); + } + eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar); // Reload characters list From 9f21f7771c288c6a22fa217c240748bfe565cb26 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 17 Feb 2025 07:28:59 +0100 Subject: [PATCH 3/7] Auto-load char/group resets if target is not found --- public/script.js | 16 +++++++++++++--- public/scripts/RossAscends-mods.js | 13 ++++++++++++- public/scripts/group-chats.js | 7 +++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/public/script.js b/public/script.js index 975f12065..753e6b7d3 100644 --- a/public/script.js +++ b/public/script.js @@ -1028,12 +1028,22 @@ export function setAnimationDuration(ms = null) { document.documentElement.style.setProperty('--animation-duration', `${animation_duration}ms`); } +/** + * Sets the currently active character + * @param {object|number|string} [entityOrKey] - An entity with id property (character, group, tag), or directly an id or tag key. If not provided, the active character is reset to `null`. + */ export function setActiveCharacter(entityOrKey) { - active_character = getTagKeyForEntity(entityOrKey); + active_character = entityOrKey ? getTagKeyForEntity(entityOrKey) : null; + if (active_character) active_group = null; } +/** + * Sets the currently active group. + * @param {object|number|string} [entityOrKey] - An entity with id property (character, group, tag), or directly an id or tag key. If not provided, the active group is reset to `null`. + */ export function setActiveGroup(entityOrKey) { - active_group = getTagKeyForEntity(entityOrKey); + active_group = entityOrKey ? getTagKeyForEntity(entityOrKey) : null; + if (active_group) active_character = null; } /** @@ -7597,7 +7607,7 @@ export function select_selected_character(chid) { // Hide the chat scenario button if we're peeking the group member defs $('#set_chat_scenario').toggle(!selected_group); - // Don't update the navbar name if we're peeking the group member defs + // Don't update the navbar name if we're peekinFg the group member defs if (!selected_group) { $('#rm_button_selected_ch').children('h2').text(display_name); } diff --git a/public/scripts/RossAscends-mods.js b/public/scripts/RossAscends-mods.js index 236955596..9d64b9cb2 100644 --- a/public/scripts/RossAscends-mods.js +++ b/public/scripts/RossAscends-mods.js @@ -287,10 +287,21 @@ async function RA_autoloadchat() { const selectedCharElement = $(`#rm_print_characters_block .character_select[chid="${active_character_id}"]`); applyTagsOnCharacterSelect.call(selectedCharElement); } + if (!active_character_id) { + setActiveCharacter(null); + saveSettingsDebounced(); + console.warn(`Currently active character with ID ${active_character} not found. Resetting to no active character.`); + } } if (active_group !== null && active_group !== undefined) { - await openGroupById(String(active_group)); + if (active_character) { + console.warn('Active character and active group are both set. Only active character will be loaded. Resetting active group.'); + setActiveGroup(null); + saveSettingsDebounced(); + } else { + const result = await openGroupById(String(active_group)); + } } // if the character list hadn't been loaded yet, try again. diff --git a/public/scripts/group-chats.js b/public/scripts/group-chats.js index 54f19e90e..0d0af4a3f 100644 --- a/public/scripts/group-chats.js +++ b/public/scripts/group-chats.js @@ -1664,12 +1664,12 @@ function updateFavButtonState(state) { export async function openGroupById(groupId) { if (isChatSaving) { toastr.info(t`Please wait until the chat is saved before switching characters.`, t`Your chat is still saving...`); - return; + return false; } if (!groups.find(x => x.id === groupId)) { console.log('Group not found', groupId); - return; + return false; } if (!is_send_press && !is_group_generating) { @@ -1686,8 +1686,11 @@ export async function openGroupById(groupId) { updateChatMetadata({}, true); chat.length = 0; await getGroupChat(groupId); + return true; } } + + return false; } function openCharacterDefinition(characterSelect) { From 7b65427236c2c281530b1e42457cc3f3df7f2ff3 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 17 Feb 2025 09:19:08 +0100 Subject: [PATCH 4/7] typo --- public/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index 753e6b7d3..f48aaf377 100644 --- a/public/script.js +++ b/public/script.js @@ -7607,7 +7607,7 @@ export function select_selected_character(chid) { // Hide the chat scenario button if we're peeking the group member defs $('#set_chat_scenario').toggle(!selected_group); - // Don't update the navbar name if we're peekinFg the group member defs + // Don't update the navbar name if we're peeking the group member defs if (!selected_group) { $('#rm_button_selected_ch').children('h2').text(display_name); } From 231068f72905195a6ef343c281071e2e50cd4f1e Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 17 Feb 2025 09:47:35 +0100 Subject: [PATCH 5/7] await character renamed event --- public/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index f48aaf377..d76c4810a 100644 --- a/public/script.js +++ b/public/script.js @@ -6279,7 +6279,7 @@ export async function renameCharacter(name = null, { silent = false, renameChats saveSettingsDebounced(); } - eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar); + await eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar); // Reload characters list await getCharacters(); From ab27b2981976f3e328b730d727c63c8b65cb3453 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 17 Feb 2025 09:58:37 +0100 Subject: [PATCH 6/7] cleanup group on auto load if name not found --- public/scripts/RossAscends-mods.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/public/scripts/RossAscends-mods.js b/public/scripts/RossAscends-mods.js index 9d64b9cb2..1c62a186d 100644 --- a/public/scripts/RossAscends-mods.js +++ b/public/scripts/RossAscends-mods.js @@ -301,6 +301,11 @@ async function RA_autoloadchat() { saveSettingsDebounced(); } else { const result = await openGroupById(String(active_group)); + if (!result) { + setActiveGroup(null); + saveSettingsDebounced(); + console.warn(`Currently active group with ID ${active_group} not found. Resetting to no active group.`); + } } } From 0cdc389794ba53fe924cdc82a806361562d43cc7 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:00:09 +0200 Subject: [PATCH 7/7] Fix type handling of active_character_id --- public/scripts/RossAscends-mods.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/public/scripts/RossAscends-mods.js b/public/scripts/RossAscends-mods.js index 1c62a186d..2702976a8 100644 --- a/public/scripts/RossAscends-mods.js +++ b/public/scripts/RossAscends-mods.js @@ -280,14 +280,13 @@ async function RA_autoloadchat() { // active character is the name, we should look it up in the character list and get the id if (active_character !== null && active_character !== undefined) { const active_character_id = characters.findIndex(x => getTagKeyForEntity(x) === active_character); - if (active_character_id !== null) { + if (active_character_id !== -1) { await selectCharacterById(String(active_character_id)); // Do a little tomfoolery to spoof the tag selector const selectedCharElement = $(`#rm_print_characters_block .character_select[chid="${active_character_id}"]`); applyTagsOnCharacterSelect.call(selectedCharElement); - } - if (!active_character_id) { + } else { setActiveCharacter(null); saveSettingsDebounced(); console.warn(`Currently active character with ID ${active_character} not found. Resetting to no active character.`);