Add chat bookmarking

This commit is contained in:
SillyLossy
2023-03-23 03:14:06 +02:00
parent a67e72ba1c
commit 5c537e6eb8
10 changed files with 129 additions and 13 deletions

View 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();
});
});