Merge pull request #3564 from SillyTavern/rename-past-chats-event Add `RENAMED_PAST_CHAT` event

cf14b75091e9bab8ab55dedf7cc9f45f66ca2fdc

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

Signed
1 files changed, +35 -10Ignore whitespace
public/script.js+35 -10
@@ -500,6 +500,7 @@ export const event_types = {
500500 CHARACTER_DELETED: 'characterDeleted',
501501 CHARACTER_DUPLICATED: 'character_duplicated',
502502 CHARACTER_RENAMED: 'character_renamed',
503+ CHARACTER_RENAMED_IN_PAST_CHAT: 'character_renamed_in_past_chat',
503504 /** @deprecated The event is aliased to STREAM_TOKEN_RECEIVED. */
504505 SMOOTH_STREAM_TOKEN_RECEIVED: 'stream_token_received',
505506 STREAM_TOKEN_RECEIVED: 'stream_token_received',
@@ -6270,6 +6271,23 @@ export function setSendButtonState(value) {
62706271 is_send_press = value;
62716272}
62726273
6274+/**
6275+ * Renames the currently selected character, updating relevant references and optionally renaming past chats.
6276+ *
6277+ * If no name is provided, a popup prompts for a new name. If the new name matches the current name,
6278+ * the renaming process is aborted. The function sends a request to the server to rename the character
6279+ * and handles updates to other related fields such as tags, lore, and author notes.
6280+ *
6281+ * If the renaming is successful, the character list is reloaded and the renamed character is selected.
6282+ * Optionally, past chats can be renamed to reflect the new character name.
6283+ *
6284+ * @param {string?} [name=null] - The new name for the character. If not provided, a popup will prompt for it.
6285+ * @param {object} [options] - Additional options.
6286+ * @param {boolean} [options.silent=false] - If true, suppresses popups and warnings.
6287+ * @param {boolean?} [options.renameChats=null] - If true, renames past chats to reflect the new character name.
6288+ * @returns {Promise<boolean>} - Returns true if the character was successfully renamed, false otherwise.
6289+ */
6290+
62736291export async function renameCharacter(name = null, { silent = false, renameChats = null } = {}) {
62746292 if (!name && silent) {
62756293 toastr.warning(t`No character name provided.`, t`Rename Character`);
@@ -6353,13 +6371,18 @@ export async function renameCharacter(name = null, { silent = false, renameChats
63536371
63546372 // Also rename as a group member
63556373 await renameGroupMember(oldAvatar, newAvatar, newValue);
63566374 const renamePastChatsConfirm = renameChats !== null ? renameChats
6357- : silent ? false : await callPopup(`<h3>Character renamed!</h3>
6375+ ? renameChats
6358- <p>Past chats will still contain the old character name. Would you like to update the character name in previous chats as well?</p>
6376+ : silent
6359- <i><b>Sprites folder (if any) should be renamed manually.</b></i>`, 'confirm');
6377+ ? false
6378+ : await Popup.show.confirm(
6379+ t`Character renamed!`,
6380+ `<p>${t`Past chats will still contain the old character name. Would you like to update the character name in previous chats as well?`}</p>
6381+ <i><b>${t`Sprites folder (if any) should be renamed manually.`}</b></i>`,
6382+ ) == POPUP_RESULT.AFFIRMATIVE;
63606383
63616384 if (renamePastChatsConfirm) {
63626385 await renamePastChats(oldAvatar, newAvatar, newValue);
63636386 await reloadCurrentChat();
63646387 toastr.success(t`Character renamed and past chats updated!`, t`Rename Character`);
63656388 } else {
@@ -6376,7 +6399,7 @@ export async function renameCharacter(name = null, { silent = false, renameChats
63766399 }
63776400 catch (error) {
63786401 // Reloading to prevent data corruption
63796402 if (!silent) await callPopupPopup.show.text(t`Rename Character`, t`Something went wrong. The page will be reloaded.`, 'text');
63806403 else toastr.error(t`Something went wrong. The page will be reloaded.`, t`Rename Character`);
63816404
63826405 console.log('Renaming character error:', error);
@@ -6387,7 +6410,7 @@ export async function renameCharacter(name = null, { silent = false, renameChats
63876410 return true;
63886411}
63896412
63906413async function renamePastChats(oldAvatar, newAvatar, newValuenewName) {
63916414 const pastChats = await getPastCharacterChats();
63926415
63936416 for (const { file_name } of pastChats) {
@@ -6397,7 +6420,7 @@ async function renamePastChats(newAvatar, newValue) {
63976420 method: 'POST',
63986421 headers: getRequestHeaders(),
63996422 body: JSON.stringify({
64006423 ch_name: newValuenewName,
64016424 file_name: fileNameWithoutExtension,
64026425 avatar_url: newAvatar,
64036426 }),
@@ -6413,15 +6436,17 @@ async function renamePastChats(newAvatar, newValue) {
64136436 }
64146437
64156438 if (message.name !== undefined) {
64166439 message.name = newValuenewName;
64176440 }
64186441 }
64196442
6443+ await eventSource.emit(event_types.CHARACTER_RENAMED_IN_PAST_CHAT, currentChat, oldAvatar, newAvatar);
6444+
64206445 const saveChatResponse = await fetch('/api/chats/save', {
64216446 method: 'POST',
64226447 headers: getRequestHeaders(),
64236448 body: JSON.stringify({
64246449 ch_name: newValuenewName,
64256450 file_name: fileNameWithoutExtension,
64266451 chat: currentChat,
64276452 avatar_url: newAvatar,