Optimize performance of getExistingChatNames

def10929d22578dcfb22babbb5a0cf77f471d6f6

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +32 -20Showing whitespace changes
public/script.js+0 -9
@@ -7417,16 +7417,7 @@ export async function openCharacterChat(file_name) {
74177417 characters[this_chid]['chat'] = file_name;
74187418 chat.length = 0;
74197419 chat_metadata = {};
7420- try {
74217420 await getChat();
7422- if (chat.length === 0) {
7423- toastr.warning('Chat file not found: ' + file_name);
7424- return;
7425- }
7426- } catch (e) {
7427- toastr.warning('Chat file not found: ' + file_name);
7428- return;
7429- }
74307421 $('#selected_chat_pole').val(file_name);
74317422 await createOrEditCharacter(new CustomEvent('newChat'));
74327423}
public/scripts/bookmarks.js+32 -7
@@ -14,7 +14,6 @@ import {
1414} from '../script.js';
1515import { humanizedDateTime } from './RossAscends-mods.js';
1616import {
17- getGroupPastChats,
1817 group_activation_strategy,
1918 groups,
2019 openGroupById,
@@ -40,22 +39,42 @@ import {
4039
4140const bookmarkNameToken = 'Checkpoint #';
4241
42+/**
43+ * Gets the names of existing chats for the current character or group.
44+ * @returns {Promise<string[]>} - Returns a promise that resolves to an array of existing chat names.
45+ */
4346async function getExistingChatNames() {
4447 if (selected_group) {
4548 const datagroup = await getGroupPastChatsgroups.find(x => x.id == selected_group);
46- return data.map(x => x.file_name);
49+ if (group && Array.isArray(group.chats)) {
47- } else {
50+ return [...group.chats];
51+ }
52+
53+ return [];
54+ }
55+
56+ if (this_chid === undefined) {
57+ return [];
58+ }
59+
60+ const character = characters[this_chid];
61+ if (!character) {
62+ return [];
63+ }
64+
4865 const response = await fetch('/api/characters/chats', {
4966 method: 'POST',
5067 headers: getRequestHeaders(),
5168 body: JSON.stringify({ avatar_url: characters[this_chid]character.avatar, simple: true }),
5269 });
5370
5471 if (response.ok) {
5572 const data = await response.json();
5673 return const chats = Object.values(data).map(x => x.file_name.replace('.jsonl', ''));
57- }
74+ return [...chats];
5875 }
76+
77+ return [];
5978}
6079
6180async function getBookmarkName({ isReplace = false, forceName = null } = {}) {
@@ -238,11 +257,17 @@ export function updateBookmarkDisplay(mes, newBookmarkLink = null) {
238257
239258async function backToMainChat() {
240259 const mainChatName = getMainChatName();
260+ const allChats = await getExistingChatNames();
261+
262+ if (allChats.includes(mainChatName)) {
241263 if (selected_group) {
242264 await openGroupChat(selected_group, mainChatName);
243265 } else {
244266 await openCharacterChat(mainChatName);
245267 }
268+ return mainChatName;
269+ }
270+
246271 return null;
247272}
248273
public/scripts/group-chats.js+0 -4
@@ -266,10 +266,6 @@ export async function getGroupChat(groupId, reload = false) {
266266 await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1), 'first_message');
267267 }
268268 await saveGroupChat(groupId, false);
269- await printMessages();
270- } else {
271- toastr.warning('Group chat file not found: ' + chat_id);
272- return;
273269 }
274270 }
275271