mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add chat bookmarking
This commit is contained in:
61
public/scripts/bookmarks.js
Normal file
61
public/scripts/bookmarks.js
Normal file
@ -0,0 +1,61 @@
|
||||
import {
|
||||
characters,
|
||||
saveChat,
|
||||
sendSystemMessage,
|
||||
deleteLastMessage,
|
||||
token,
|
||||
system_messages,
|
||||
system_message_types,
|
||||
this_chid,
|
||||
} from "../script.js";
|
||||
import { selected_group } from "./group-chats.js";
|
||||
|
||||
import {
|
||||
stringFormat,
|
||||
} from "./utils.js";
|
||||
|
||||
async function getExistingChatNames() {
|
||||
const response = await fetch("/getallchatsofcharacter", {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
"X-CSRF-Token": token,
|
||||
},
|
||||
body: JSON.stringify({ avatar_url: characters[this_chid].avatar})
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
return Object.values(data).map(x => x.file_name.replace('.jsonl', ''));
|
||||
}
|
||||
}
|
||||
|
||||
async function getBookmarkName() {
|
||||
const chatNames = await getExistingChatNames();
|
||||
let newChat = Date.now();
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
newChat = `Bookmark - ${i}`;
|
||||
if (!chatNames.includes(newChat)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return newChat;
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
$('#option_new_bookmark').on('click', async function () {
|
||||
if (selected_group) {
|
||||
alert('Unsupported for groups');
|
||||
throw new Error('not yet implemented');
|
||||
}
|
||||
|
||||
let newChat = await getBookmarkName();
|
||||
|
||||
saveChat(newChat);
|
||||
let mainMessage = stringFormat(system_messages[system_message_types.BOOKMARK_CREATED].mes, newChat);
|
||||
sendSystemMessage(system_message_types.BOOKMARK_CREATED, mainMessage);
|
||||
saveChat();
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user