Save parent bookmark reference to chat metadata

This commit is contained in:
SillyLossy
2023-04-14 22:43:02 +03:00
parent 0d09967485
commit c0eb8190c7
4 changed files with 48 additions and 23 deletions

View File

@@ -1438,8 +1438,8 @@
<div class="group_member_icon">
<div title="Move up" data-action="up" class="right_menu_button fa-solid fa-xl fa-chevron-up"></div>
<div title="Move down" data-action="down" class="right_menu_button fa-solid fa-xl fa-chevron-down"></div>
<div title="Remove from group" data-action="remove" class="right_menu_button fa-solid fa-xl fa-xmark"></div>
<div title="Add to group" data-action="add" class="right_menu_button fa-solid fa-xl fa-plus"></div>
<div title="Remove from group" data-action="remove" class="right_menu_button fa-solid fa-2xl fa-xmark"></div>
<div title="Add to group" data-action="add" class="right_menu_button fa-solid fa-2xl fa-plus"></div>
</div>
</div>
</div>
@@ -1780,7 +1780,7 @@
<div class="options-content">
<a id="option_back_to_main">
<div class="fa-lg fa-solid fa-left-long"></div>
<span>Back to main chat</span>
<span>Back to parent chat</span>
</a>
<a id="option_new_bookmark">
<div class="fa-lg fa-solid fa-bookmark"></div>

View File

@@ -2254,7 +2254,8 @@ function resultCheckStatusNovel() {
$("#api_button_novel").css("display", "inline-block");
}
async function saveChat(chat_name) {
async function saveChat(chat_name, withMetadata) {
const metadata = { ...chat_metadata, ...(withMetadata || {}) };
let file_name = chat_name ?? characters[this_chid].chat;
chat.forEach(function (item, i) {
if (item["is_group"]) {
@@ -2275,7 +2276,7 @@ async function saveChat(chat_name) {
user_name: default_user_name,
character_name: name2,
create_date: chat_create_date,
chat_metadata: chat_metadata,
chat_metadata: metadata,
},
...chat,
];

View File

@@ -7,6 +7,8 @@ import {
system_message_types,
this_chid,
openCharacterChat,
chat_metadata,
callPopup,
} from "../script.js";
import { selected_group } from "./group-chats.js";
@@ -36,27 +38,40 @@ async function getExistingChatNames() {
}
}
async function getBookmarkName(currentChat) {
async function getBookmarkName() {
const chatNames = await getExistingChatNames();
let mainChat = getMainChatName(currentChat);
let newChat = Date.now();
let friendlyName = '';
const popupText = `<h3>Enter the bookmark name:<h3>
<small>Using existing name will overwrite your bookmark chat.
<br>Leave empty to auto-generate.</small>`;
let name = await callPopup(popupText, 'input');
if (name === false) {
return null;
}
else if (name === '') {
for (let i = 0; i < 1000; i++) {
friendlyName = `${bookmarkNameToken}${i}`;
newChat = `${mainChat} ${friendlyName}`;
if (!chatNames.includes(newChat)) {
name = bookmarkNameToken + i;
if (!chatNames.includes(name)) {
break;
}
}
return { newChat, friendlyName };
}
function getMainChatName(currentChat) {
if (currentChat.includes(bookmarkNameToken)) {
currentChat = currentChat.substring(0, currentChat.lastIndexOf(bookmarkNameToken)).trim();
return name;
}
return currentChat;
function getMainChatName() {
if (chat_metadata) {
if (chat_metadata['main_chat']) {
return chat_metadata['main_chat'];
}
else if (characters[this_chid].chat && characters[this_chid].chat.includes(bookmarkNameToken)) {
const tokenIndex = characters[this_chid].chat.lastIndexOf(bookmarkNameToken);
chat_metadata['main_chat'] = characters[this_chid].chat.substring(0, tokenIndex).trim();
return chat_metadata['main_chat'];
}
}
return null;
}
function showBookmarksButtons() {
@@ -67,7 +82,7 @@ function showBookmarksButtons() {
$("#option_new_bookmark").hide();
}
// In main chat
else if (!characters[this_chid].chat.includes(bookmarkNameToken)) {
else if (!chat_metadata['main_chat']) {
$("#option_back_to_main").hide();
$("#option_new_bookmark").show();
@@ -91,10 +106,15 @@ $(document).ready(function () {
throw new Error();
}
let { newChat, friendlyName } = await getBookmarkName(characters[this_chid].chat);
let name = await getBookmarkName(characters[this_chid].chat);
saveChat(newChat);
let mainMessage = stringFormat(system_messages[system_message_types.BOOKMARK_CREATED].mes, newChat, friendlyName);
if (!name) {
return;
}
const newMetadata = { main_chat: characters[this_chid].chat };
saveChat(name, newMetadata);
let mainMessage = stringFormat(system_messages[system_message_types.BOOKMARK_CREATED].mes, name, name);
sendSystemMessage(system_message_types.BOOKMARK_CREATED, mainMessage);
saveChat();
});

View File

@@ -367,6 +367,10 @@ code {
column-gap: 10px;
}
.options-content a div:first-child {
min-width: 20px;
}
.options-content img {
width: calc(var(--mainFontSize) + .5rem);
margin-right: 5px;