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 = {
500 CHARACTER_DELETED: 'characterDeleted',500 CHARACTER_DELETED: 'characterDeleted',
501 CHARACTER_DUPLICATED: 'character_duplicated',501 CHARACTER_DUPLICATED: 'character_duplicated',
502 CHARACTER_RENAMED: 'character_renamed',502 CHARACTER_RENAMED: 'character_renamed',
503 CHARACTER_RENAMED_IN_PAST_CHAT: 'character_renamed_in_past_chat',
503 /** @deprecated The event is aliased to STREAM_TOKEN_RECEIVED. */504 /** @deprecated The event is aliased to STREAM_TOKEN_RECEIVED. */
504 SMOOTH_STREAM_TOKEN_RECEIVED: 'stream_token_received',505 SMOOTH_STREAM_TOKEN_RECEIVED: 'stream_token_received',
505 STREAM_TOKEN_RECEIVED: 'stream_token_received',506 STREAM_TOKEN_RECEIVED: 'stream_token_received',
@@ -6270,6 +6271,23 @@ export function setSendButtonState(value) {
6270 is_send_press = value;6271 is_send_press = value;
6271}6272}
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
6273export async function renameCharacter(name = null, { silent = false, renameChats = null } = {}) {6291export async function renameCharacter(name = null, { silent = false, renameChats = null } = {}) {
6274 if (!name && silent) {6292 if (!name && silent) {
6275 toastr.warning(t`No character name provided.`, t`Rename Character`);6293 toastr.warning(t`No character name provided.`, t`Rename Character`);
@@ -6353,13 +6371,18 @@ export async function renameCharacter(name = null, { silent = false, renameChats
63536371
6354 // Also rename as a group member6372 // Also rename as a group member
6355 await renameGroupMember(oldAvatar, newAvatar, newValue);6373 await renameGroupMember(oldAvatar, newAvatar, newValue);
6356 const renamePastChatsConfirm = renameChats !== null ? renameChats6374 const renamePastChatsConfirm = renameChats !== null
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
6361 if (renamePastChatsConfirm) {6384 if (renamePastChatsConfirm) {
6362 await renamePastChats(newAvatar, newValue);6385 await renamePastChats(oldAvatar, newAvatar, newValue);
6363 await reloadCurrentChat();6386 await reloadCurrentChat();
6364 toastr.success(t`Character renamed and past chats updated!`, t`Rename Character`);6387 toastr.success(t`Character renamed and past chats updated!`, t`Rename Character`);
6365 } else {6388 } else {
@@ -6376,7 +6399,7 @@ export async function renameCharacter(name = null, { silent = false, renameChats
6376 }6399 }
6377 catch (error) {6400 catch (error) {
6378 // Reloading to prevent data corruption6401 // Reloading to prevent data corruption
6379 if (!silent) await callPopup(t`Something went wrong. The page will be reloaded.`, 'text');6402 if (!silent) await Popup.show.text(t`Rename Character`, t`Something went wrong. The page will be reloaded.`);
6380 else toastr.error(t`Something went wrong. The page will be reloaded.`, t`Rename Character`);6403 else toastr.error(t`Something went wrong. The page will be reloaded.`, t`Rename Character`);
63816404
6382 console.log('Renaming character error:', error);6405 console.log('Renaming character error:', error);
@@ -6387,7 +6410,7 @@ export async function renameCharacter(name = null, { silent = false, renameChats
6387 return true;6410 return true;
6388}6411}
63896412
6390async function renamePastChats(newAvatar, newValue) {6413async function renamePastChats(oldAvatar, newAvatar, newName) {
6391 const pastChats = await getPastCharacterChats();6414 const pastChats = await getPastCharacterChats();
63926415
6393 for (const { file_name } of pastChats) {6416 for (const { file_name } of pastChats) {
@@ -6397,7 +6420,7 @@ async function renamePastChats(newAvatar, newValue) {
6397 method: 'POST',6420 method: 'POST',
6398 headers: getRequestHeaders(),6421 headers: getRequestHeaders(),
6399 body: JSON.stringify({6422 body: JSON.stringify({
6400 ch_name: newValue,6423 ch_name: newName,
6401 file_name: fileNameWithoutExtension,6424 file_name: fileNameWithoutExtension,
6402 avatar_url: newAvatar,6425 avatar_url: newAvatar,
6403 }),6426 }),
@@ -6413,15 +6436,17 @@ async function renamePastChats(newAvatar, newValue) {
6413 }6436 }
64146437
6415 if (message.name !== undefined) {6438 if (message.name !== undefined) {
6416 message.name = newValue;6439 message.name = newName;
6417 }6440 }
6418 }6441 }
64196442
6443 await eventSource.emit(event_types.CHARACTER_RENAMED_IN_PAST_CHAT, currentChat, oldAvatar, newAvatar);
6444
6420 const saveChatResponse = await fetch('/api/chats/save', {6445 const saveChatResponse = await fetch('/api/chats/save', {
6421 method: 'POST',6446 method: 'POST',
6422 headers: getRequestHeaders(),6447 headers: getRequestHeaders(),
6423 body: JSON.stringify({6448 body: JSON.stringify({
6424 ch_name: newValue,6449 ch_name: newName,
6425 file_name: fileNameWithoutExtension,6450 file_name: fileNameWithoutExtension,
6426 chat: currentChat,6451 chat: currentChat,
6427 avatar_url: newAvatar,6452 avatar_url: newAvatar,