Add retroactive bookmarking

This commit is contained in:
Mike Weldon
2023-07-04 18:13:22 -07:00
parent 9a8e036ff9
commit d3c3614147
5 changed files with 33 additions and 9 deletions

View File

@@ -3302,6 +3302,7 @@
<div title="Copy" class="mes_copy fa-solid fa-copy "></div> <div title="Copy" class="mes_copy fa-solid fa-copy "></div>
</div> </div>
<div title="Open bookmark chat" class="mes_bookmark fa-solid fa-bookmark"></div> <div title="Open bookmark chat" class="mes_bookmark fa-solid fa-bookmark"></div>
<div title="Create bookmark" class="mes_create_bookmark fa-regular fa-code-branch"></div>
<div title="Edit" class="mes_edit fa-solid fa-pencil "></div> <div title="Edit" class="mes_edit fa-solid fa-pencil "></div>
</div> </div>
<div class="mes_edit_buttons"> <div class="mes_edit_buttons">

View File

@@ -100,7 +100,10 @@ import {
nai_settings, nai_settings,
} from "./scripts/nai-settings.js"; } from "./scripts/nai-settings.js";
import { showBookmarksButtons } from "./scripts/bookmarks.js"; import {
createNewBookmark,
showBookmarksButtons
} from "./scripts/bookmarks.js";
import { import {
horde_settings, horde_settings,
@@ -3815,7 +3818,7 @@ async function renamePastChats(newAvatar, newValue) {
} }
} }
async function saveChat(chat_name, withMetadata) { async function saveChat(chat_name, withMetadata, mesId) {
const metadata = { ...chat_metadata, ...(withMetadata || {}) }; const metadata = { ...chat_metadata, ...(withMetadata || {}) };
let file_name = chat_name ?? characters[this_chid].chat; let file_name = chat_name ?? characters[this_chid].chat;
characters[this_chid]['date_last_chat'] = Date.now(); characters[this_chid]['date_last_chat'] = Date.now();
@@ -3836,6 +3839,11 @@ async function saveChat(chat_name, withMetadata) {
} }
*/ */
}); });
const trimmed_chat = (mesId !== undefined && mesId >= 0 && mesId < chat.length)
? chat.slice(0, parseInt(mesId) + 1)
: chat;
var save_chat = [ var save_chat = [
{ {
user_name: name1, user_name: name1,
@@ -3843,7 +3851,7 @@ async function saveChat(chat_name, withMetadata) {
create_date: chat_create_date, create_date: chat_create_date,
chat_metadata: metadata, chat_metadata: metadata,
}, },
...chat, ...trimmed_chat,
]; ];
return jQuery.ajax({ return jQuery.ajax({
type: "POST", type: "POST",
@@ -7883,6 +7891,13 @@ $(document).ready(function () {
$("#load_select_chat_div").css("display", "block"); $("#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) {
createNewBookmark(selected_mes_id);
}
});
$(document).on("click", ".mes_stop", function () { $(document).on("click", ".mes_stop", function () {
if (streamingProcessor) { if (streamingProcessor) {
streamingProcessor.abortController.abort(); streamingProcessor.abortController.abort();

View File

@@ -32,6 +32,7 @@ import {
} from "./utils.js"; } from "./utils.js";
export { export {
createNewBookmark,
showBookmarksButtons, showBookmarksButtons,
} }
@@ -123,13 +124,14 @@ function showBookmarksButtons() {
} }
} }
async function createNewBookmark() { async function createNewBookmark(mesId) {
if (!chat.length) { if (!chat.length) {
toastr.warning('The chat is empty.', 'Bookmark creation failed'); toastr.warning('The chat is empty.', 'Bookmark creation failed');
return; return;
} }
const mesId = chat.length - 1; // Default to last message in chat if no mesId given.
mesId = mesId || chat.length - 1;
const lastMes = chat[mesId]; const lastMes = chat[mesId];
if (typeof lastMes.extra !== 'object') { if (typeof lastMes.extra !== 'object') {
@@ -155,9 +157,9 @@ async function createNewBookmark() {
const newMetadata = { main_chat: mainChat }; const newMetadata = { main_chat: mainChat };
if (selected_group) { if (selected_group) {
await saveGroupBookmarkChat(selected_group, name, newMetadata); await saveGroupBookmarkChat(selected_group, name, newMetadata, mesId);
} else { } else {
await saveChat(name, newMetadata); await saveChat(name, newMetadata, mesId);
} }
lastMes.extra['bookmark_link'] = name; lastMes.extra['bookmark_link'] = name;

View File

@@ -1440,7 +1440,7 @@ export async function importGroupChat(formData) {
}); });
} }
export async function saveGroupBookmarkChat(groupId, name, metadata) { export async function saveGroupBookmarkChat(groupId, name, metadata, mesId) {
const group = groups.find(x => x.id === groupId); const group = groups.find(x => x.id === groupId);
if (!group) { if (!group) {
@@ -1450,12 +1450,16 @@ export async function saveGroupBookmarkChat(groupId, name, metadata) {
group.past_metadata[name] = { ...chat_metadata, ...(metadata || {}) }; group.past_metadata[name] = { ...chat_metadata, ...(metadata || {}) };
group.chats.push(name); group.chats.push(name);
const trimmed_chat = (mesId !== undefined && mesId >= 0 && mesId < chat.length)
? chat.slice(0, parseInt(mesId) + 1)
: chat;
await editGroup(groupId, true); await editGroup(groupId, true);
await fetch("/savegroupchat", { await fetch("/savegroupchat", {
method: "POST", method: "POST",
headers: getRequestHeaders(), headers: getRequestHeaders(),
body: JSON.stringify({ id: name, chat: [...chat] }), body: JSON.stringify({ id: name, chat: [...trimmed_chat] }),
}); });
} }

View File

@@ -2470,6 +2470,7 @@ input[type="range"]::-webkit-slider-thumb {
.mes_buttons .mes_edit, .mes_buttons .mes_edit,
.mes_buttons .mes_bookmark, .mes_buttons .mes_bookmark,
.mes_buttons .mes_create_bookmark,
.extraMesButtonsHint, .extraMesButtonsHint,
.tagListHint, .tagListHint,
.extraMesButtons div { .extraMesButtons div {
@@ -2481,6 +2482,7 @@ input[type="range"]::-webkit-slider-thumb {
.mes_buttons .mes_edit:hover, .mes_buttons .mes_edit:hover,
.mes_buttons .mes_bookmark:hover, .mes_buttons .mes_bookmark:hover,
.mes_buttons .mes_create_bookmark:hover,
.extraMesButtonsHint:hover, .extraMesButtonsHint:hover,
.tagListHint:hover, .tagListHint:hover,
.extraMesButtons div:hover { .extraMesButtons div:hover {