mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add solo chat to group conversion
This commit is contained in:
@@ -9,10 +9,17 @@ import {
|
||||
chat_metadata,
|
||||
callPopup,
|
||||
getRequestHeaders,
|
||||
getThumbnailUrl,
|
||||
getCharacters,
|
||||
chat,
|
||||
} from "../script.js";
|
||||
import { selected_group } from "./group-chats.js";
|
||||
import { humanizedDateTime } from "./RossAscends-mods.js";
|
||||
import { group_activation_strategy, groups, selected_group } from "./group-chats.js";
|
||||
import { createTagMapFromList } from "./tags.js";
|
||||
|
||||
import {
|
||||
delay,
|
||||
getUniqueName,
|
||||
stringFormat,
|
||||
} from "./utils.js";
|
||||
|
||||
@@ -100,32 +107,147 @@ function showBookmarksButtons() {
|
||||
}
|
||||
}
|
||||
|
||||
async function createNewBookmark() {
|
||||
if (selected_group) {
|
||||
alert('Chat bookmarks unsupported for groups');
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
let name = await getBookmarkName(characters[this_chid].chat);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
async function backToMainChat() {
|
||||
const mainChatName = getMainChatName(characters[this_chid].chat);
|
||||
const allChats = await getExistingChatNames();
|
||||
|
||||
if (allChats.includes(mainChatName)) {
|
||||
openCharacterChat(mainChatName);
|
||||
}
|
||||
}
|
||||
|
||||
async function convertSoloToGroupChat() {
|
||||
if (selected_group) {
|
||||
console.log('Already in group. No need for conversion');
|
||||
return;
|
||||
}
|
||||
|
||||
if (this_chid === undefined) {
|
||||
console.log('Need to have a character selected');
|
||||
return;
|
||||
}
|
||||
|
||||
const character = characters[this_chid];
|
||||
|
||||
// Populate group required fields
|
||||
const name = getUniqueName(`Chat with ${character.name}`, y => groups.findIndex(x => x.name === y) !== -1);
|
||||
const avatar = getThumbnailUrl('avatar', character.avatar);
|
||||
const chatName = humanizedDateTime();
|
||||
const chats = [chatName];
|
||||
const members = [character.avatar];
|
||||
const activationStrategy = group_activation_strategy.NATURAL;
|
||||
const allowSelfResponses = false;
|
||||
const favChecked = character.fav == 'true';
|
||||
const metadata = Object.assign({}, chat_metadata);
|
||||
|
||||
const createGroupResponse = await fetch("/creategroup", {
|
||||
method: "POST",
|
||||
headers: getRequestHeaders(),
|
||||
body: JSON.stringify({
|
||||
name: name,
|
||||
members: members,
|
||||
avatar_url: avatar,
|
||||
allow_self_responses: activationStrategy,
|
||||
activation_strategy: allowSelfResponses,
|
||||
chat_metadata: metadata,
|
||||
fav: favChecked,
|
||||
chat_id: chatName,
|
||||
chats: chats,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!createGroupResponse.ok) {
|
||||
console.error('Group creation unsuccessful');
|
||||
return;
|
||||
}
|
||||
|
||||
const group = await createGroupResponse.json();
|
||||
|
||||
// Convert tags list and assign to group
|
||||
createTagMapFromList("#tagList", group.id);
|
||||
|
||||
// Update chars list
|
||||
await getCharacters();
|
||||
|
||||
// Convert chat to group format
|
||||
const groupChat = chat.slice();
|
||||
const genIdFirst = Date.now();
|
||||
|
||||
// Add something if the chat is empty
|
||||
if (groupChat.length === 0) {
|
||||
const newMessage = {
|
||||
...system_messages[system_message_types.GROUP],
|
||||
send_date: humanizedDateTime(),
|
||||
extra: { type: system_message_types.GROUP }
|
||||
};
|
||||
groupChat.push(newMessage);
|
||||
}
|
||||
|
||||
for (let index = 0; index < groupChat.length; index++) {
|
||||
const message = groupChat[index];
|
||||
|
||||
// Save group-chat marker
|
||||
if (index == 0) {
|
||||
message.is_group = true;
|
||||
}
|
||||
|
||||
// Skip messages we don't care about
|
||||
if (message.is_user || message.is_system) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set force fields for solo character
|
||||
message.name = character.name;
|
||||
message.original_avatar = character.avatar;
|
||||
message.force_avatar = getThumbnailUrl('avatar', character.avatar);
|
||||
message.is_name = true;
|
||||
|
||||
// Allow regens of a single message in group
|
||||
if (typeof message.extra !== 'object') {
|
||||
message.extra = { gen_id: genIdFirst + index };
|
||||
}
|
||||
}
|
||||
|
||||
// Save group chat
|
||||
const createChatResponse = await fetch("/savegroupchat", {
|
||||
method: "POST",
|
||||
headers: getRequestHeaders(),
|
||||
body: JSON.stringify({ id: chatName, chat: groupChat }),
|
||||
});
|
||||
|
||||
if (!createChatResponse.ok) {
|
||||
console.error('Group chat creation unsuccessful');
|
||||
return;
|
||||
}
|
||||
|
||||
// Click on the freshly selected group to open it
|
||||
$(`.group_select[grid="${group.id}"]`).click();
|
||||
|
||||
await delay(1);
|
||||
callPopup('The chat has been successfully converted!', 'text');
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
$('#option_new_bookmark').on('click', async function () {
|
||||
if (selected_group) {
|
||||
alert('Chat bookmarks unsupported for groups');
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
let name = await getBookmarkName(characters[this_chid].chat);
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
$('#option_back_to_main').on('click', async function () {
|
||||
const mainChatName = getMainChatName(characters[this_chid].chat);
|
||||
const allChats = await getExistingChatNames();
|
||||
|
||||
if (allChats.includes(mainChatName)) {
|
||||
openCharacterChat(mainChatName);
|
||||
}
|
||||
});
|
||||
$('#option_new_bookmark').on('click', createNewBookmark);
|
||||
$('#option_back_to_main').on('click', backToMainChat);
|
||||
$('#option_convert_to_group').on('click', convertSoloToGroupChat);
|
||||
});
|
||||
|
Reference in New Issue
Block a user