mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Bookmarks support for groups
This commit is contained in:
@@ -3611,7 +3611,7 @@ async function saveMetadata() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveChatConditional() {
|
export async function saveChatConditional() {
|
||||||
if (selected_group) {
|
if (selected_group) {
|
||||||
await saveGroupChat(selected_group, true);
|
await saveGroupChat(selected_group, true);
|
||||||
}
|
}
|
||||||
@@ -4272,10 +4272,9 @@ $(document).ready(function () {
|
|||||||
|
|
||||||
//open the history view again after 100ms
|
//open the history view again after 100ms
|
||||||
//hide option popup menu
|
//hide option popup menu
|
||||||
setTimeout(function () {
|
await delay(100);
|
||||||
$("#option_select_chat").click();
|
$("#option_select_chat").click();
|
||||||
$("#options").hide();
|
$("#options").hide();
|
||||||
}, 100);
|
|
||||||
}
|
}
|
||||||
if (popup_type == "del_ch") {
|
if (popup_type == "del_ch") {
|
||||||
console.log(
|
console.log(
|
||||||
|
@@ -12,9 +12,17 @@ import {
|
|||||||
getThumbnailUrl,
|
getThumbnailUrl,
|
||||||
getCharacters,
|
getCharacters,
|
||||||
chat,
|
chat,
|
||||||
|
saveChatConditional,
|
||||||
} from "../script.js";
|
} from "../script.js";
|
||||||
import { humanizedDateTime } from "./RossAscends-mods.js";
|
import { humanizedDateTime } from "./RossAscends-mods.js";
|
||||||
import { group_activation_strategy, groups, selected_group } from "./group-chats.js";
|
import {
|
||||||
|
getGroupPastChats,
|
||||||
|
group_activation_strategy,
|
||||||
|
groups,
|
||||||
|
openGroupChat,
|
||||||
|
saveGroupBookmarkChat,
|
||||||
|
selected_group,
|
||||||
|
} from "./group-chats.js";
|
||||||
import { createTagMapFromList } from "./tags.js";
|
import { createTagMapFromList } from "./tags.js";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -30,30 +38,34 @@ export {
|
|||||||
const bookmarkNameToken = 'Bookmark #';
|
const bookmarkNameToken = 'Bookmark #';
|
||||||
|
|
||||||
async function getExistingChatNames() {
|
async function getExistingChatNames() {
|
||||||
const response = await fetch("/getallchatsofcharacter", {
|
if (selected_group) {
|
||||||
method: 'POST',
|
const data = await getGroupPastChats(selected_group);
|
||||||
headers: getRequestHeaders(),
|
return data.map(x => x.file_name);
|
||||||
body: JSON.stringify({ avatar_url: characters[this_chid].avatar })
|
} else {
|
||||||
});
|
const response = await fetch("/getallchatsofcharacter", {
|
||||||
|
method: 'POST',
|
||||||
|
headers: getRequestHeaders(),
|
||||||
|
body: JSON.stringify({ avatar_url: characters[this_chid].avatar })
|
||||||
|
});
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
return Object.values(data).map(x => x.file_name.replace('.jsonl', ''));
|
return Object.values(data).map(x => x.file_name.replace('.jsonl', ''));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getBookmarkName() {
|
async function getBookmarkName() {
|
||||||
const chatNames = await getExistingChatNames();
|
const chatNames = await getExistingChatNames();
|
||||||
const popupText = `<h3>Enter the bookmark name:<h3>
|
const popupText = `<h3>Enter the bookmark name:<h3>
|
||||||
<small>Using existing name will overwrite your bookmark chat.
|
<small>Leave empty to auto-generate.</small>`;
|
||||||
<br>Leave empty to auto-generate.</small>`;
|
|
||||||
let name = await callPopup(popupText, 'input');
|
let name = await callPopup(popupText, 'input');
|
||||||
|
|
||||||
if (name === false) {
|
if (name === false) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
else if (name === '') {
|
else if (name === '') {
|
||||||
for (let i = 0; i < 1000; i++) {
|
for (let i = chatNames.length; i < 1000; i++) {
|
||||||
name = bookmarkNameToken + i;
|
name = bookmarkNameToken + i;
|
||||||
if (!chatNames.includes(name)) {
|
if (!chatNames.includes(name)) {
|
||||||
break;
|
break;
|
||||||
@@ -61,7 +73,7 @@ async function getBookmarkName() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return name;
|
return `${name} - ${humanizedDateTime()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMainChatName() {
|
function getMainChatName() {
|
||||||
@@ -69,6 +81,10 @@ function getMainChatName() {
|
|||||||
if (chat_metadata['main_chat']) {
|
if (chat_metadata['main_chat']) {
|
||||||
return chat_metadata['main_chat'];
|
return chat_metadata['main_chat'];
|
||||||
}
|
}
|
||||||
|
// groups didn't support bookmarks before chat metadata was introduced
|
||||||
|
else if (selected_group) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
else if (characters[this_chid].chat && characters[this_chid].chat.includes(bookmarkNameToken)) {
|
else if (characters[this_chid].chat && characters[this_chid].chat.includes(bookmarkNameToken)) {
|
||||||
const tokenIndex = characters[this_chid].chat.lastIndexOf(bookmarkNameToken);
|
const tokenIndex = characters[this_chid].chat.lastIndexOf(bookmarkNameToken);
|
||||||
chat_metadata['main_chat'] = characters[this_chid].chat.substring(0, tokenIndex).trim();
|
chat_metadata['main_chat'] = characters[this_chid].chat.substring(0, tokenIndex).trim();
|
||||||
@@ -80,24 +96,24 @@ function getMainChatName() {
|
|||||||
|
|
||||||
function showBookmarksButtons() {
|
function showBookmarksButtons() {
|
||||||
try {
|
try {
|
||||||
// In groups or without an active chat
|
if (selected_group) {
|
||||||
if (selected_group || !characters[this_chid].chat) {
|
|
||||||
$("#option_back_to_main").hide();
|
|
||||||
$("#option_new_bookmark").hide();
|
|
||||||
$("#option_convert_to_group").hide();
|
$("#option_convert_to_group").hide();
|
||||||
}
|
} else {
|
||||||
// In main chat
|
|
||||||
else if (!chat_metadata['main_chat']) {
|
|
||||||
$("#option_back_to_main").hide();
|
|
||||||
$("#option_new_bookmark").show();
|
|
||||||
$("#option_convert_to_group").show();
|
$("#option_convert_to_group").show();
|
||||||
|
|
||||||
}
|
}
|
||||||
// In bookmark chat
|
|
||||||
else {
|
if (chat_metadata['main_chat']) {
|
||||||
|
// In bookmark chat
|
||||||
$("#option_back_to_main").show();
|
$("#option_back_to_main").show();
|
||||||
$("#option_new_bookmark").show();
|
$("#option_new_bookmark").show();
|
||||||
$("#option_convert_to_group").show();
|
} else if (!selected_group && !characters[this_chid].chat) {
|
||||||
|
// No chat recorded on character
|
||||||
|
$("#option_back_to_main").hide();
|
||||||
|
$("#option_new_bookmark").hide();
|
||||||
|
} else {
|
||||||
|
// In main chat
|
||||||
|
$("#option_back_to_main").hide();
|
||||||
|
$("#option_new_bookmark").show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
@@ -108,30 +124,36 @@ function showBookmarksButtons() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createNewBookmark() {
|
async function createNewBookmark() {
|
||||||
if (selected_group) {
|
let name = await getBookmarkName();
|
||||||
alert('Chat bookmarks unsupported for groups');
|
|
||||||
throw new Error();
|
|
||||||
}
|
|
||||||
|
|
||||||
let name = await getBookmarkName(characters[this_chid].chat);
|
|
||||||
|
|
||||||
if (!name) {
|
if (!name) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const newMetadata = { main_chat: characters[this_chid].chat };
|
const mainChat = selected_group ? groups?.find(x => x.id == selected_group)?.chat_id : characters[this_chid].chat;
|
||||||
saveChat(name, newMetadata);
|
const newMetadata = { main_chat: mainChat };
|
||||||
|
|
||||||
|
if (selected_group) {
|
||||||
|
await saveGroupBookmarkChat(selected_group, name, newMetadata);
|
||||||
|
} else {
|
||||||
|
await saveChat(name, newMetadata);
|
||||||
|
}
|
||||||
|
|
||||||
let mainMessage = stringFormat(system_messages[system_message_types.BOOKMARK_CREATED].mes, name, name);
|
let mainMessage = stringFormat(system_messages[system_message_types.BOOKMARK_CREATED].mes, name, name);
|
||||||
sendSystemMessage(system_message_types.BOOKMARK_CREATED, mainMessage);
|
sendSystemMessage(system_message_types.BOOKMARK_CREATED, mainMessage);
|
||||||
saveChat();
|
await saveChatConditional();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function backToMainChat() {
|
async function backToMainChat() {
|
||||||
const mainChatName = getMainChatName(characters[this_chid].chat);
|
const mainChatName = getMainChatName();
|
||||||
const allChats = await getExistingChatNames();
|
const allChats = await getExistingChatNames();
|
||||||
|
|
||||||
if (allChats.includes(mainChatName)) {
|
if (allChats.includes(mainChatName)) {
|
||||||
openCharacterChat(mainChatName);
|
if (selected_group) {
|
||||||
|
await openGroupChat(selected_group, mainChatName);
|
||||||
|
} else {
|
||||||
|
await openCharacterChat(mainChatName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,6 +180,7 @@ async function convertSoloToGroupChat() {
|
|||||||
const allowSelfResponses = false;
|
const allowSelfResponses = false;
|
||||||
const favChecked = character.fav == 'true';
|
const favChecked = character.fav == 'true';
|
||||||
const metadata = Object.assign({}, chat_metadata);
|
const metadata = Object.assign({}, chat_metadata);
|
||||||
|
delete metadata.main_chat;
|
||||||
|
|
||||||
const createGroupResponse = await fetch("/creategroup", {
|
const createGroupResponse = await fetch("/creategroup", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
|
@@ -1181,6 +1181,25 @@ export async function deleteGroupChat(groupId, chatId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function saveGroupBookmarkChat(groupId, name, metadata) {
|
||||||
|
const group = groups.find(x => x.id === groupId);
|
||||||
|
|
||||||
|
if (!group) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
group.past_metadata[name] = { ...chat_metadata, ...(metadata || {}) };
|
||||||
|
group.chats.push(name);
|
||||||
|
|
||||||
|
await editGroup(groupId, true);
|
||||||
|
|
||||||
|
await fetch("/savegroupchat", {
|
||||||
|
method: "POST",
|
||||||
|
headers: getRequestHeaders(),
|
||||||
|
body: JSON.stringify({ id: name, chat: [...chat] }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
$(document).ready(() => {
|
$(document).ready(() => {
|
||||||
$(document).on("click", ".group_select", selectGroup);
|
$(document).on("click", ".group_select", selectGroup);
|
||||||
$("#rm_group_filter").on("input", filterGroupMembers);
|
$("#rm_group_filter").on("input", filterGroupMembers);
|
||||||
|
Reference in New Issue
Block a user