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

4f0921856f21bd8388a19246911965ee3d4f9fa9

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +32 -6Ignore whitespace
public/script.js+22 -1
@@ -493,6 +493,7 @@ export const event_types = {
493493 // TODO: Naming convention is inconsistent with other events
494494 CHARACTER_DELETED: 'characterDeleted',
495495 CHARACTER_DUPLICATED: 'character_duplicated',
496+ CHARACTER_RENAMED: 'character_renamed',
496497 /** @deprecated The event is aliased to STREAM_TOKEN_RECEIVED. */
497498 SMOOTH_STREAM_TOKEN_RECEIVED: 'stream_token_received',
498499 STREAM_TOKEN_RECEIVED: 'stream_token_received',
@@ -6241,9 +6242,29 @@ export async function renameCharacter(name = null, { silent = false, renameChats
62416242 const data = await response.json();
62426243 const newAvatar = data.avatar;
62436244
6244- // Replace tags list
6245+ const oldName = getCharaFilename(null, { manualAvatarKey: oldAvatar });
6246+ const newName = getCharaFilename(null, { manualAvatarKey: newAvatar });
6247+
6248+ // Replace other auxillery fields where was referenced by avatar key
6249+ // Tag List
62456250 renameTagKey(oldAvatar, newAvatar);
62466251
6252+ // Addtional lore books
6253+ const charLore = world_info.charLore?.find(x => x.name == oldName);
6254+ if (charLore) {
6255+ charLore.name = newName;
6256+ saveSettingsDebounced();
6257+ }
6258+
6259+ // Char-bound Author's Notes
6260+ const charNote = extension_settings.note.chara?.find(x => x.name == oldName);
6261+ if (charNote) {
6262+ charNote.name = newName;
6263+ saveSettingsDebounced();
6264+ }
6265+
6266+ eventSource.emit(event_types.CHARACTER_RENAMED, oldAvatar, newAvatar);
6267+
62476268 // Reload characters list
62486269 await getCharacters();
62496270
public/scripts/utils.js+10 -5
@@ -994,13 +994,18 @@ export function getImageSizeFromDataURL(dataUrl) {
994994 });
995995}
996996
997-export function getCharaFilename(chid) {
997+/**
998+ * Gets the filename of the character avatar without extension
999+ * @param {number?} [chid=null] - Character ID. If not provided, uses the current character ID
1000+ * @param {object} [options={}] - Options arguments
1001+ * @param {string?} [options.manualAvatarKey=null] - Manually take the following avatar key, instead of using the chid to determine the name
1002+ * @returns {string?} The filename of the character avatar without extension, or null if the character ID is invalid
1003+ */
1004+export function getCharaFilename(chid = null, { manualAvatarKey = null } = {}) {
9981005 const context = getContext();
9991006 const fileName = manualAvatarKey ?? context.characters[chid ?? context.characterId]?.avatar;
10001007
1001- if (fileName) {
1008+ return fileName?.replace(/\.[^/.]+$/, '') ?? null;
1002- return fileName.replace(/\.[^/.]+$/, '');
1003- }
10041009}
10051010
10061011/**