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();
|
||||
});
|
||||
});
|
||||
|
@@ -8,6 +8,8 @@ export {
|
||||
debounce,
|
||||
delay,
|
||||
isSubsetOf,
|
||||
incrementString,
|
||||
stringFormat,
|
||||
};
|
||||
|
||||
/// UTILS
|
||||
@@ -86,3 +88,22 @@ function debounce(func, timeout = 300) {
|
||||
|
||||
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
|
||||
const isSubsetOf = (a, b) => (Array.isArray(a) && Array.isArray(b)) ? b.every(val => a.includes(val)) : false;
|
||||
|
||||
function incrementString(str) {
|
||||
// Find the trailing number or it will match the empty string
|
||||
const count = str.match(/\d*$/);
|
||||
|
||||
// Take the substring up until where the integer was matched
|
||||
// Concatenate it to the matched count incremented by 1
|
||||
return str.substr(0, count.index) + (++count[0]);
|
||||
};
|
||||
|
||||
function stringFormat(format) {
|
||||
const args = Array.prototype.slice.call(arguments, 1);
|
||||
return format.replace(/{(\d+)}/g, function (match, number) {
|
||||
return typeof args[number] != 'undefined'
|
||||
? args[number]
|
||||
: match
|
||||
;
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user