SillyTavern/public/scripts/bookmarks.js

684 lines
24 KiB
JavaScript
Raw Normal View History

2023-07-20 19:32:15 +02:00
import {
characters,
saveChat,
system_messages,
system_message_types,
this_chid,
openCharacterChat,
chat_metadata,
getRequestHeaders,
getThumbnailUrl,
getCharacters,
chat,
saveChatConditional,
saveItemizedPrompts,
2023-12-02 19:04:51 +01:00
} from '../script.js';
import { humanizedDateTime, getMessageTimeStamp } from './RossAscends-mods.js';
2023-07-20 19:32:15 +02:00
import {
getGroupPastChats,
group_activation_strategy,
groups,
2023-08-19 20:44:15 +02:00
openGroupById,
2023-07-20 19:32:15 +02:00
openGroupChat,
saveGroupBookmarkChat,
selected_group,
2023-12-02 19:04:51 +01:00
} from './group-chats.js';
2024-09-08 00:34:03 +02:00
import { hideLoader, showLoader } from './loader.js';
import { getLastMessageId } from './macros.js';
import { Popup } from './popup.js';
import { SlashCommand } from './slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js';
import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js';
import { SlashCommandParser } from './slash-commands/SlashCommandParser.js';
2023-12-02 19:04:51 +01:00
import { createTagMapFromList } from './tags.js';
2023-07-20 19:32:15 +02:00
import {
getUniqueName,
2023-12-02 19:04:51 +01:00
} from './utils.js';
2023-07-20 19:32:15 +02:00
2023-12-03 02:11:14 +01:00
const bookmarkNameToken = 'Checkpoint #';
2023-07-20 19:32:15 +02:00
async function getExistingChatNames() {
if (selected_group) {
const data = await getGroupPastChats(selected_group);
return data.map(x => x.file_name);
} else {
const response = await fetch('/api/characters/chats', {
2023-07-20 19:32:15 +02:00
method: 'POST',
headers: getRequestHeaders(),
2023-12-02 21:06:57 +01:00
body: JSON.stringify({ avatar_url: characters[this_chid].avatar }),
2023-07-20 19:32:15 +02:00
});
if (response.ok) {
const data = await response.json();
return Object.values(data).map(x => x.file_name.replace('.jsonl', ''));
}
}
}
async function getBookmarkName({ forceName = null } = {}) {
2023-07-20 19:32:15 +02:00
const chatNames = await getExistingChatNames();
let name = forceName || await Popup.show.input('Create Checkpoint', '<span class="margin-right-10px">Enter Checkpoint Name:</span><small>(Leave empty to auto-generate)</small>');
2024-09-08 00:06:19 +02:00
if (name === null) {
2023-07-20 19:32:15 +02:00
return null;
}
else if (name === '') {
for (let i = chatNames.length; i < 1000; i++) {
name = bookmarkNameToken + i;
if (!chatNames.includes(name)) {
break;
}
}
}
return `${name} - ${humanizedDateTime()}`;
}
function getMainChatName() {
if (chat_metadata) {
if (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)) {
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;
}
export function showBookmarksButtons() {
2023-07-20 19:32:15 +02:00
try {
if (selected_group) {
2023-12-02 19:04:51 +01:00
$('#option_convert_to_group').hide();
2023-07-20 19:32:15 +02:00
} else {
2023-12-02 19:04:51 +01:00
$('#option_convert_to_group').show();
2023-07-20 19:32:15 +02:00
}
if (chat_metadata['main_chat']) {
// In bookmark chat
2023-12-02 19:04:51 +01:00
$('#option_back_to_main').show();
$('#option_new_bookmark').show();
2023-07-20 19:32:15 +02:00
} else if (!selected_group && !characters[this_chid].chat) {
// No chat recorded on character
2023-12-02 19:04:51 +01:00
$('#option_back_to_main').hide();
$('#option_new_bookmark').hide();
2023-07-20 19:32:15 +02:00
} else {
// In main chat
2023-12-02 19:04:51 +01:00
$('#option_back_to_main').hide();
$('#option_new_bookmark').show();
2023-07-20 19:32:15 +02:00
}
}
catch {
2023-12-02 19:04:51 +01:00
$('#option_back_to_main').hide();
$('#option_new_bookmark').hide();
$('#option_convert_to_group').hide();
2023-07-20 19:32:15 +02:00
}
}
async function saveBookmarkMenu() {
if (!chat.length) {
2023-12-03 02:11:14 +01:00
toastr.warning('The chat is empty.', 'Checkpoint creation failed');
2023-07-20 19:32:15 +02:00
return;
}
return await createNewBookmark(chat.length - 1);
2023-07-20 19:32:15 +02:00
}
2024-09-08 13:33:52 +02:00
// Export is used by Timelines extension. Do not remove.
export async function createBranch(mesId) {
if (!chat.length) {
2023-09-21 06:40:38 +02:00
toastr.warning('The chat is empty.', 'Branch creation failed');
return;
}
if (mesId < 0 || mesId >= chat.length) {
2023-09-21 06:40:38 +02:00
toastr.warning('Invalid message ID.', 'Branch creation failed');
return;
}
const lastMes = chat[mesId];
const mainChat = selected_group ? groups?.find(x => x.id == selected_group)?.chat_id : characters[this_chid].chat;
const newMetadata = { main_chat: mainChat };
2023-12-02 20:11:06 +01:00
let name = `Branch #${mesId} - ${humanizedDateTime()}`;
if (selected_group) {
await saveGroupBookmarkChat(selected_group, name, newMetadata, mesId);
} else {
await saveChat(name, newMetadata, mesId);
}
// append to branches list if it exists
// otherwise create it
if (typeof lastMes.extra !== 'object') {
lastMes.extra = {};
}
if (typeof lastMes.extra['branches'] !== 'object') {
lastMes.extra['branches'] = [];
}
lastMes.extra['branches'].push(name);
return name;
}
/**
* Creates a new bookmark for a message.
*
* @param {number} mesId - The ID of the message.
* @param {Object} [options={}] - Optional parameters.
* @param {string?} [options.forceName=null] - The name to force for the bookmark.
* @returns {Promise<string?>} - A promise that resolves to the bookmark name when the bookmark is created.
*/
export async function createNewBookmark(mesId, { forceName = null } = {}) {
if (this_chid === undefined && !selected_group) {
toastr.info('No character selected.', 'Create Checkpoint');
return null;
}
2023-07-20 19:32:15 +02:00
if (!chat.length) {
toastr.warning('The chat is empty.', 'Create Checkpoint');
return null;
2023-07-20 19:32:15 +02:00
}
if (!chat[mesId]) {
toastr.warning('Invalid message ID.', 'Create Checkpoint');
return null;
2023-07-20 19:32:15 +02:00
}
const lastMes = chat[mesId];
if (typeof lastMes.extra !== 'object') {
lastMes.extra = {};
}
if (lastMes.extra.bookmark_link && !forceName) {
2024-09-08 00:06:19 +02:00
const confirm = await Popup.show.confirm('Replace Checkpoint', 'Checkpoint for the last message already exists.<br />Would you like to replace it?');
2023-07-20 19:32:15 +02:00
if (!confirm) {
return null;
2023-07-20 19:32:15 +02:00
}
}
let name = await getBookmarkName({ forceName: forceName });
2023-07-20 19:32:15 +02:00
if (!name) {
return null;
2023-07-20 19:32:15 +02:00
}
const mainChat = selected_group ? groups?.find(x => x.id == selected_group)?.chat_id : characters[this_chid].chat;
const newMetadata = { main_chat: mainChat };
await saveItemizedPrompts(name);
2023-07-20 19:32:15 +02:00
if (selected_group) {
await saveGroupBookmarkChat(selected_group, name, newMetadata, mesId);
} else {
await saveChat(name, newMetadata, mesId);
}
lastMes.extra['bookmark_link'] = name;
const mes = $(`.mes[mesid="${mesId}"]`);
2024-09-08 00:34:03 +02:00
updateBookmarkDisplay(mes, name);
2023-07-20 19:32:15 +02:00
await saveChatConditional();
toastr.success('Click the flag icon next to the message to open the checkpoint chat.', 'Create Checkpoint', { timeOut: 10000 });
return name;
2023-07-20 19:32:15 +02:00
}
2024-09-08 00:34:03 +02:00
/**
* Updates the display of the bookmark on a chat message.
* @param {JQuery<HTMLElement>} mes - The message element
* @param {string?} [newBookmarkLink=null] - The new bookmark link (optional)
*/
export function updateBookmarkDisplay(mes, newBookmarkLink = null) {
newBookmarkLink && mes.attr('bookmark_link', newBookmarkLink);
mes.find('.mes_bookmark').attr('title', `Checkpoint\n${mes.attr('bookmark_link')}\n\n${mes.find('.mes_bookmark').data('tooltip')}`);
}
2023-07-20 19:32:15 +02:00
async function backToMainChat() {
const mainChatName = getMainChatName();
const allChats = await getExistingChatNames();
if (allChats.includes(mainChatName)) {
if (selected_group) {
await openGroupChat(selected_group, mainChatName);
} else {
await openCharacterChat(mainChatName);
}
return mainChatName;
2023-07-20 19:32:15 +02:00
}
return null;
2023-07-20 19:32:15 +02:00
}
export async function convertSoloToGroupChat() {
2023-07-20 19:32:15 +02:00
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 confirm = await Popup.show.confirm('Convert to group chat', 'Are you sure you want to convert this chat to a group chat?<br />This cannot be reverted.');
2023-12-17 20:14:16 +01:00
if (!confirm) {
return;
}
2023-07-20 19:32:15 +02:00
const character = characters[this_chid];
// Populate group required fields
const name = getUniqueName(`Group: ${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 || character.fav == 'true';
/** @type {any} */
2023-07-20 19:32:15 +02:00
const metadata = Object.assign({}, chat_metadata);
delete metadata.main_chat;
2023-12-06 18:40:58 +01:00
const createGroupResponse = await fetch('/api/groups/create', {
2023-12-02 19:04:51 +01:00
method: 'POST',
2023-07-20 19:32:15 +02:00
headers: getRequestHeaders(),
body: JSON.stringify({
name: name,
members: members,
avatar_url: avatar,
allow_self_responses: activationStrategy,
activation_strategy: allowSelfResponses,
disabled_members: [],
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
2023-12-02 19:04:51 +01:00
createTagMapFromList('#tagList', group.id);
2023-07-20 19:32:15 +02:00
// 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: getMessageTimeStamp(),
2023-12-02 21:06:57 +01:00
extra: { type: system_message_types.GROUP },
2023-07-20 19:32:15 +02:00
};
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 || message.extra?.type === system_message_types.NARRATOR || message.force_avatar !== undefined) {
continue;
}
// Set force fields for solo character
message.name = character.name;
message.original_avatar = character.avatar;
message.force_avatar = getThumbnailUrl('avatar', character.avatar);
// Allow regens of a single message in group
if (typeof message.extra !== 'object') {
message.extra = { gen_id: genIdFirst + index };
}
}
// Save group chat
2023-12-06 04:34:52 +01:00
const createChatResponse = await fetch('/api/chats/group/save', {
2023-12-02 19:04:51 +01:00
method: 'POST',
2023-07-20 19:32:15 +02:00
headers: getRequestHeaders(),
body: JSON.stringify({ id: chatName, chat: groupChat }),
});
if (!createChatResponse.ok) {
console.error('Group chat creation unsuccessful');
toastr.error('Group chat creation unsuccessful');
2023-07-20 19:32:15 +02:00
return;
}
// Click on the freshly selected group to open it
2023-08-19 20:44:15 +02:00
await openGroupById(group.id);
2023-07-20 19:32:15 +02:00
toastr.success('The chat has been successfully converted!');
}
2024-09-08 00:34:03 +02:00
/**
* Creates a new branch from the message with the given ID
* @param {number} mesId Message ID
* @returns {Promise<string?>} Branch file name
2024-09-08 00:34:03 +02:00
*/
export async function branchChat(mesId) {
2024-09-08 00:34:03 +02:00
if (this_chid === undefined && !selected_group) {
toastr.info('No character selected.', 'Create Branch');
return null;
2024-09-08 00:34:03 +02:00
}
const fileName = await createBranch(mesId);
await saveItemizedPrompts(fileName);
if (selected_group) {
await openGroupChat(selected_group, fileName);
} else {
await openCharacterChat(fileName);
}
return fileName;
}
function registerBookmarksSlashCommands() {
/**
* Validates a message ID. (Is a number, exists as a message)
*
* @param {number} mesId - The message ID to validate.
* @param {string} context - The context of the slash command. Will be used as the title of any toasts.
* @returns {boolean} - Returns true if the message ID is valid, otherwise false.
*/
function validateMessageId(mesId, context) {
if (isNaN(mesId)) {
toastr.warning('Invalid message ID was provided', context);
return false;
}
if (!chat[mesId]) {
toastr.warning(`Message for id ${mesId} not found`, context);
return false;
}
return true;
}
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'branch-create',
returns: 'Name of the new branch',
callback: async (args, text) => {
const mesId = Number(args.mesId ?? text ?? getLastMessageId());
if (!validateMessageId(mesId, 'Create Branch')) return '';
const branchName = await branchChat(mesId);
return branchName ?? '';
},
namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'mes',
description: 'Message ID',
typeList: [ARGUMENT_TYPE.NUMBER],
enumProvider: commonEnumProviders.messages(),
}),
],
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'Message ID',
typeList: [ARGUMENT_TYPE.NUMBER],
enumProvider: commonEnumProviders.messages(),
}),
],
helpString: `
<div>
Create a new branch from the selected message. If no message id is provided, will use the last message.
</div>
<div>
Creating a branch will automatically choose a name for the branch.<br />
After creating the branch, the branch chat will be automatically opened.
</div>
<div>
Use Checkpoints and <code>/checkpoint-create</code> instead if you do not want to jump to the new chat.
</div>`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'checkpoint-create',
returns: 'Name of the new checkpoint',
callback: async (args, text) => {
const mesId = Number(args.mesId ?? getLastMessageId());
if (!validateMessageId(mesId, 'Create Checkpoint')) return '';
if (!text || typeof text !== 'string') {
toastr.warning('Checkpoint name must be provided', 'Create Checkpoint');
return '';
}
const checkPointName = await createNewBookmark(mesId, { forceName: text });
return checkPointName ?? '';
},
namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'mes',
description: 'Message ID',
typeList: [ARGUMENT_TYPE.NUMBER],
enumProvider: commonEnumProviders.messages(),
}),
],
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'Checkpoint name',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
}),
],
helpString: `
<div>
Create a new checkpoint for the selected message with the provided name. If no message id is provided, will use the last message.
</div>
<div>
A created checkpoint will be permanently linked with the message.<br />
If a checkpoint already exists, the link to it will be overwritten.<br />
After creating the checkpoint, the checkpoint chat can be opened with the checkpoint flag,
using the <code>/go</code> command with the checkpoint name or the <code>/checkpoint-go</code> command on the message.
</div>
<div>
Use Branches and <code>/branch-create</code> instead if you do want to jump to the new chat.
</div>
<div>
<strong>Example:</strong>
<ul>
<li>
<pre><code>/checkpoint-create mes={{lastCharMessage}} Checkpoint for char reply | /setvar key=rememberCheckpoint {{pipe}}</code></pre>
Will create a new checkpoint to the latest message of the current character, and save it as a local variable for future use.
</li>
</ul>
</div>`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'checkpoint-go',
returns: 'Name of the checkpoint',
callback: async (args, text) => {
const mesId = Number(args.mesId ?? text ?? getLastMessageId());
if (!validateMessageId(mesId, 'Open Checkpoint')) return '';
const checkPointName = chat[mesId].extra?.bookmark_link;
if (!checkPointName) {
toastr.warning('No checkpoint is linked to the selected message', 'Open Checkpoint');
return '';
}
if (selected_group) {
await openGroupChat(selected_group, checkPointName);
} else {
await openCharacterChat(checkPointName);
}
return checkPointName;
},
namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'mes',
description: 'Message ID',
typeList: [ARGUMENT_TYPE.NUMBER],
enumProvider: commonEnumProviders.messages(),
}),
],
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'Message ID',
typeList: [ARGUMENT_TYPE.NUMBER],
enumProvider: commonEnumProviders.messages(),
}),
],
helpString: `
<div>
Open the checkpoint linked to the selected message. If no message id is provided, will use the last message.
</div>
<div>
Use <code>/checkpoint-get</code> if you want to make sure that the selected message has a checkpoint.
</div>`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'checkpoint-exit',
returns: 'The name of the chat exited to. Returns null if not in a checkpoint chat.',
callback: async () => {
const mainChat = await backToMainChat();
return mainChat ?? '';
},
helpString: 'Exit the checkpoint chat.<br />If not in a checkpoint chat, returns null.',
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'checkpoint-parent',
returns: 'Name of the parent chat for this checkpoint',
callback: async () => {
const mainChatName = getMainChatName();
return mainChatName ?? '';
},
helpString: 'Get the name of the parent chat for this checkpoint. If not in a checkpoint chat, returns null.',
}))
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'checkpoint-get',
returns: 'Name of the chat',
callback: async (args, text) => {
const mesId = Number(args.mesId ?? text ?? getLastMessageId());
if (!validateMessageId(mesId, 'Get Checkpoint')) return '';
const checkPointName = chat[mesId].extra?.bookmark_link;
return checkPointName ?? '';
},
namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'mes',
description: 'Message ID',
typeList: [ARGUMENT_TYPE.NUMBER],
enumProvider: commonEnumProviders.messages(),
}),
],
unnamedArgumentList: [
SlashCommandArgument.fromProps({
description: 'Message ID',
typeList: [ARGUMENT_TYPE.NUMBER],
enumProvider: commonEnumProviders.messages(),
}),
],
helpString: `
<div>
Get the name of the checkpoint linked to the selected message. If no message id is provided, will use the last message.<br />
If no checkpoint is linked, the result will be empty.
</div>`,
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'checkpoint-list',
returns: 'JSON array of all existing checkpoints in this chat, as an array',
/** @param {{links?: string}} args @returns {Promise<string>} */
callback: async (args, _) => {
const result = [];
for (const mesId in chat) {
if (chat[mesId].extra?.bookmark_link) {
result.push(args.links ? chat[mesId].extra.bookmark_link : Number(mesId));
}
}
return JSON.stringify(result);
},
namedArgumentList: [
SlashCommandNamedArgument.fromProps({
name: 'links',
description: 'Get a list of all links / chat names of the checkpoints, instead of the message ids',
typeList: [ARGUMENT_TYPE.BOOLEAN],
enumList: commonEnumProviders.boolean('trueFalse')(),
defaultValue: 'false',
}),
],
helpString: `
<div>
List all existing checkpoints in this chat.
</div>
<div>
Returns a list of all message ids that have a checkpoint, or all checkpoint links if <code>links</code> is set to <code>true</code>.<br />
The value will be a JSON array.
</div>`,
}));
}
export function initBookmarks() {
2023-07-20 19:32:15 +02:00
$('#option_new_bookmark').on('click', saveBookmarkMenu);
$('#option_back_to_main').on('click', backToMainChat);
$('#option_convert_to_group').on('click', convertSoloToGroupChat);
2024-09-08 00:34:03 +02:00
$(document).on('click', '.select_chat_block, .bookmark_link, .mes_bookmark', async function (e) {
// If shift is held down, we are not following the bookmark, but creating a new one
if (e.shiftKey) {
var selectedMesId = $(this).closest('.mes').attr('mesid');
await createNewBookmark(Number(selectedMesId));
2024-09-08 00:34:03 +02:00
return;
}
let file_name = $(this).hasClass('mes_bookmark')
? $(this).closest('.mes').attr('bookmark_link')
: $(this).attr('file_name').replace('.jsonl', '');
if (!file_name) {
return;
}
try {
showLoader();
if (selected_group) {
await openGroupChat(selected_group, file_name);
} else {
await openCharacterChat(file_name);
}
} finally {
await hideLoader();
2024-09-08 00:34:03 +02:00
}
$('#shadow_select_chat_popup').css('display', 'none');
$('#load_select_chat_div').css('display', 'block');
});
$(document).on('click', '.mes_create_bookmark', async function () {
var selected_mes_id = $(this).closest('.mes').attr('mesid');
if (selected_mes_id !== undefined) {
await createNewBookmark(Number(selected_mes_id));
2024-09-08 00:34:03 +02:00
}
});
$(document).on('click', '.mes_create_branch', async function () {
var selected_mes_id = $(this).closest('.mes').attr('mesid');
if (selected_mes_id !== undefined) {
await branchChat(Number(selected_mes_id));
2024-09-08 00:34:03 +02:00
}
});
registerBookmarksSlashCommands();
}