Merge pull request #2560 from SillyTavern/dupe-persona Dupe persona

a2661b2c486f2b509f096a3ce8e59fedaddbbb92

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

Signed
4 files changed, +82 -20Ignore whitespace
public/index.html+3 -0
@@ -6440,6 +6440,9 @@
64406440 <button class="menu_button set_default_persona" title="Select this as default persona for the new chats." data-i18n="[title]Select this as default persona for the new chats.">
64416441 <i class="fa-fw fa-solid fa-crown"></i>
64426442 </button>
6443+ <button class="menu_button duplicate_persona" title="Duplicate persona" data-i18n="[title]Duplicate persona">
6444+ <i class="fa-fw fa-solid fa-clone"></i>
6445+ </button>
64436446 <button class="menu_button delete_avatar" title="Delete persona" data-i18n="[title]Delete persona">
64446447 <i class="fa-fw fa-solid fa-trash-alt"></i>
64456448 </button>
public/scripts/personas.js+70 -15
@@ -1,5 +1,4 @@
11import {
2- callPopup,
32 characters,
43 chat,
54 chat_metadata,
@@ -22,7 +21,7 @@ import { PAGINATION_TEMPLATE, debounce, delay, download, ensureImageFormatSuppor
2221import { debounce_timeout } from './constants.js';
2322import { FILTER_TYPES, FilterHelper } from './filters.js';
2423import { selected_group } from './group-chats.js';
2524import { POPUP_RESULT, POPUP_TYPE, Popup } from './popup.js';
2625
2726let savePersonasPage = 0;
2827const GRID_STORAGE_KEY = 'Personas_GridView';
@@ -332,15 +331,14 @@ async function changeUserAvatar(e) {
332331 * @returns {Promise} Promise that resolves when the persona is set
333332 */
334333export async function createPersona(avatarId) {
335334 const personaName = await callPopupPopup.show.input('<h3>Enter a name for this persona:</h3>', 'Cancel if you\'re just uploading an avatar.', 'input', '');
336335
337336 if (!personaName) {
338337 console.debug('User cancelled creating a persona');
339338 return;
340339 }
341340
342- await delay(500);
341+ const personaDescription = await Popup.show.input('Enter a description for this persona:', 'You can always add or change it later.', '', { rows: 4 });
343- const personaDescription = await callPopup('<h3>Enter a description for this persona:</h3>You can always add or change it later.', 'input', '', { rows: 4 });
344342
345343 initPersona(avatarId, personaName, personaDescription);
346344 if (power_user.persona_show_notifications) {
@@ -349,7 +347,7 @@ export async function createPersona(avatarId) {
349347}
350348
351349async function createDummyPersona() {
352350 const personaName = await callPopupPopup.show.input('<h3>Enter a name for this persona:</h3>', 'input', ''null);
353351
354352 if (!personaName) {
355353 console.debug('User cancelled creating dummy persona');
@@ -508,15 +506,20 @@ async function bindUserNameToPersona(e) {
508506 return;
509507 }
510508
509+ let personaUnbind = false;
511510 const existingPersona = power_user.personas[avatarId];
512- const personaName = await callPopup('<h3>Enter a name for this persona:</h3>(If empty name is provided, this will unbind the name from this avatar)', 'input', existingPersona || '');
511+ const personaName = await Popup.show.input(
512+ 'Enter a name for this persona:',
513+ '(If empty name is provided, this will unbind the name from this avatar)',
514+ existingPersona || '',
515+ { onClose: (p) => { personaUnbind = p.value === '' && p.result === POPUP_RESULT.AFFIRMATIVE; } });
513516
514517 // If the user clicked cancel, don't do anything
515518 if (personaName === falsenull && !personaUnbind) {
516519 return;
517520 }
518521
519522 if (personaName && personaName.length > 0) {
520523 // If the user clicked ok and entered a name, bind the name to the persona
521524 console.log(`Binding persona ${avatarId} to name ${personaName}`);
522525 power_user.personas[avatarId] = personaName;
@@ -643,7 +646,12 @@ async function lockPersona() {
643646 );
644647 }
645648 power_user.personas[user_avatar] = name1;
646649 power_user.persona_descriptions[user_avatar] = { description: '', position: persona_description_positions.IN_PROMPT };
650+ description: '',
651+ position: persona_description_positions.IN_PROMPT,
652+ depth: DEFAULT_DEPTH,
653+ role: DEFAULT_ROLE,
654+ };
647655 }
648656
649657 chat_metadata['persona'] = user_avatar;
@@ -672,7 +680,7 @@ async function deleteUserAvatar(e) {
672680 return;
673681 }
674682
675683 const confirm = await callPopupPopup.show.confirm('<h3>Are you sure you want to delete this avatar?</h3>', 'All information associated with its linked persona will be lost.', 'confirm');
676684
677685 if (!confirm) {
678686 console.debug('User cancelled deleting avatar');
@@ -806,7 +814,7 @@ async function setDefaultPersona(e) {
806814 const personaName = power_user.personas[avatarId];
807815
808816 if (avatarId === currentDefault) {
809817 const confirm = await callPopupPopup.show.confirm('Are you sure you want to remove the default persona?', 'confirm'personaName);
810818
811819 if (!confirm) {
812820 console.debug('User cancelled removing default persona');
@@ -819,8 +827,7 @@ async function setDefaultPersona(e) {
819827 }
820828 delete power_user.default_persona;
821829 } else {
822830 const confirm = await callPopupPopup.show.confirm(`<h3>Are you sure you want to set "${personaName}" as the default persona?</h3>`, 'This name and avatar will be used for all new chats, as well as existing chats where the user persona is not locked.');
823- This name and avatar will be used for all new chats, as well as existing chats where the user persona is not locked.`, 'confirm');
824831
825832 if (!confirm) {
826833 console.debug('User cancelled setting default persona');
@@ -978,7 +985,7 @@ async function onPersonasRestoreInput(e) {
978985}
979986
980987async function syncUserNameToPersona() {
981988 const confirmation = await callPopupPopup.show.confirm(`<h3>'Are you sure?</h3>', `All user-sent messages in this chat will be attributed to ${name1}.`, 'confirm');
982989
983990 if (!confirmation) {
984991 return;
@@ -1001,6 +1008,42 @@ export function retriggerFirstMessageOnEmptyChat() {
10011008 }
10021009}
10031010
1011+/**
1012+ * Duplicates a persona.
1013+ * @param {string} avatarId
1014+ * @returns {Promise<void>}
1015+ */
1016+async function duplicatePersona(avatarId) {
1017+ const personaName = power_user.personas[avatarId];
1018+
1019+ if (!personaName) {
1020+ toastr.warning('Chosen avatar is not a persona');
1021+ return;
1022+ }
1023+
1024+ const confirm = await Popup.show.confirm('Are you sure you want to duplicate this persona?', personaName);
1025+
1026+ if (!confirm) {
1027+ console.debug('User cancelled duplicating persona');
1028+ return;
1029+ }
1030+
1031+ const newAvatarId = `${Date.now()}-${personaName.replace(/[^a-zA-Z0-9]/g, '')}.png`;
1032+ const descriptor = power_user.persona_descriptions[avatarId];
1033+
1034+ power_user.personas[newAvatarId] = personaName;
1035+ power_user.persona_descriptions[newAvatarId] = {
1036+ description: descriptor?.description ?? '',
1037+ position: descriptor?.position ?? persona_description_positions.IN_PROMPT,
1038+ depth: descriptor?.depth ?? DEFAULT_DEPTH,
1039+ role: descriptor?.role ?? DEFAULT_ROLE,
1040+ };
1041+
1042+ await uploadUserAvatar(getUserAvatar(avatarId), newAvatarId);
1043+ await getUserAvatars(true, newAvatarId);
1044+ saveSettingsDebounced();
1045+}
1046+
10041047export function initPersonas() {
10051048 $(document).on('click', '.bind_user_name', bindUserNameToPersona);
10061049 $(document).on('click', '.set_default_persona', setDefaultPersona);
@@ -1059,6 +1102,18 @@ export function initPersonas() {
10591102 $('#avatar_upload_file').trigger('click');
10601103 });
10611104
1105+ $(document).on('click', '#user_avatar_block .duplicate_persona', function (e) {
1106+ e.stopPropagation();
1107+ const avatarId = $(this).closest('.avatar-container').find('.avatar').attr('imgfile');
1108+
1109+ if (!avatarId) {
1110+ console.log('no imgfile');
1111+ return;
1112+ }
1113+
1114+ duplicatePersona(avatarId);
1115+ });
1116+
10621117 $(document).on('click', '#user_avatar_block .set_persona_image', function (e) {
10631118 e.stopPropagation();
10641119 const avatarId = $(this).closest('.avatar-container').find('.avatar').attr('imgfile');
public/scripts/popup.js+5 -5
@@ -73,8 +73,8 @@ const showPopupHelper = {
7373 /**
7474 * Asynchronously displays an input popup with the given header and text, and returns the user's input.
7575 *
7676 * @param {string?} header - The header text for the popup.
7777 * @param {string?} text - The main text for the popup.
7878 * @param {string} [defaultValue=''] - The default value for the input field.
7979 * @param {PopupOptions} [popupOptions={}] - Options for the popup.
8080 * @return {Promise<string?>} A Promise that resolves with the user's input.
@@ -591,15 +591,15 @@ class PopupUtils {
591591 /**
592592 * Builds popup content with header and text below
593593 *
594594 * @param {string?} header - The header to be added to the text
595595 * @param {string?} text - The main text content
596596 */
597597 static BuildTextWithHeader(header, text) {
598598 if (!header) {
599599 return text;
600600 }
601601 return `<h3>${header}</h3>
602- ${text}`;
602+ ${text ?? ''}`; // Convert no text to empty string
603603 }
604604}
605605
public/style.css+4 -0
@@ -3031,6 +3031,10 @@ grammarly-extension {
30313031 opacity: 1;
30323032}
30333033
3034+.avatar-container .avatar-buttons .menu_button {
3035+ padding: 3px;
3036+}
3037+
30343038/* Ross should be able to handle this later */
30353039/*.big-avatars .avatar-buttons{
30363040 justify-content: center;