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