Optimize performance of getExistingChatNames

def10929d22578dcfb22babbb5a0cf77f471d6f6

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

3 files changed, +43 -31Ignore whitespace
public/script.js+1 -10
@@ -7417,16 +7417,7 @@ export async function openCharacterChat(file_name) {
7417 characters[this_chid]['chat'] = file_name;7417 characters[this_chid]['chat'] = file_name;
7418 chat.length = 0;7418 chat.length = 0;
7419 chat_metadata = {};7419 chat_metadata = {};
7420 try {7420 await getChat();
7421 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 }
7430 $('#selected_chat_pole').val(file_name);7421 $('#selected_chat_pole').val(file_name);
7431 await createOrEditCharacter(new CustomEvent('newChat'));7422 await createOrEditCharacter(new CustomEvent('newChat'));
7432}7423}
public/scripts/bookmarks.js+42 -17
@@ -14,7 +14,6 @@ import {
14} from '../script.js';14} from '../script.js';
15import { humanizedDateTime } from './RossAscends-mods.js';15import { humanizedDateTime } from './RossAscends-mods.js';
16import {16import {
17 getGroupPastChats,
18 group_activation_strategy,17 group_activation_strategy,
19 groups,18 groups,
20 openGroupById,19 openGroupById,
@@ -40,22 +39,42 @@ import {
4039
41const bookmarkNameToken = 'Checkpoint #';40const 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 */
43async function getExistingChatNames() {46async function getExistingChatNames() {
44 if (selected_group) {47 if (selected_group) {
45 const data = await getGroupPastChats(selected_group);48 const group = groups.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];
48 const response = await fetch('/api/characters/chats', {
49 method: 'POST',
50 headers: getRequestHeaders(),
51 body: JSON.stringify({ avatar_url: characters[this_chid].avatar }),
52 });
53
54 if (response.ok) {
55 const data = await response.json();
56 return Object.values(data).map(x => x.file_name.replace('.jsonl', ''));
57 }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
65 const response = await fetch('/api/characters/chats', {
66 method: 'POST',
67 headers: getRequestHeaders(),
68 body: JSON.stringify({ avatar_url: character.avatar, simple: true }),
69 });
70
71 if (response.ok) {
72 const data = await response.json();
73 const chats = Object.values(data).map(x => x.file_name.replace('.jsonl', ''));
74 return [...chats];
58 }75 }
76
77 return [];
59}78}
6079
61async function getBookmarkName({ isReplace = false, forceName = null } = {}) {80async function getBookmarkName({ isReplace = false, forceName = null } = {}) {
@@ -238,11 +257,17 @@ export function updateBookmarkDisplay(mes, newBookmarkLink = null) {
238257
239async function backToMainChat() {258async function backToMainChat() {
240 const mainChatName = getMainChatName();259 const mainChatName = getMainChatName();
241 if (selected_group) {260 const allChats = await getExistingChatNames();
242 await openGroupChat(selected_group, mainChatName);261
243 } else {262 if (allChats.includes(mainChatName)) {
244 await openCharacterChat(mainChatName);263 if (selected_group) {
264 await openGroupChat(selected_group, mainChatName);
265 } else {
266 await openCharacterChat(mainChatName);
267 }
268 return mainChatName;
245 }269 }
270
246 return null;271 return null;
247}272}
248273
public/scripts/group-chats.js+0 -4
@@ -266,10 +266,6 @@ export async function getGroupChat(groupId, reload = false) {
266 await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1), 'first_message');266 await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1), 'first_message');
267 }267 }
268 await saveGroupChat(groupId, false);268 await saveGroupChat(groupId, false);
269 await printMessages();
270 } else {
271 toastr.warning('Group chat file not found: ' + chat_id);
272 return;
273 }269 }
274 }270 }
275271