Optimize performance of getExistingChatNames
| @@ -7417,16 +7417,7 @@ export async function openCharacterChat(file_name) { | ||
| 7417 | 7417 | characters[this_chid]['chat'] = file_name; |
| 7418 | 7418 | chat.length = 0; |
| 7419 | 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 | 7421 | $('#selected_chat_pole').val(file_name); |
| 7431 | 7422 | await createOrEditCharacter(new CustomEvent('newChat')); |
| 7432 | 7423 | } |
| @@ -14,7 +14,6 @@ import { | ||
| 14 | 14 | } from '../script.js'; |
| 15 | 15 | import { humanizedDateTime } from './RossAscends-mods.js'; |
| 16 | 16 | import { |
| 17 | - getGroupPastChats, | |
| 18 | 17 | group_activation_strategy, |
| 19 | 18 | groups, |
| 20 | 19 | openGroupById, |
| @@ -40,22 +39,42 @@ import { | ||
| 40 | 39 | |
| 41 | 40 | const bookmarkNameToken = 'Checkpoint #'; |
| 42 | 41 | |
| 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 | + */ | |
| 43 | 46 | async function getExistingChatNames() { |
| 44 | 47 | if (selected_group) { |
| 45 | 48 | 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]; | |
| 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 | } |
| 60 | 79 | |
| 61 | 80 | async function getBookmarkName({ isReplace = false, forceName = null } = {}) { |
| @@ -238,11 +257,17 @@ export function updateBookmarkDisplay(mes, newBookmarkLink = null) { | ||
| 238 | 257 | |
| 239 | 258 | async function backToMainChat() { |
| 240 | 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 | 263 | awaitif openCharacterChat(mainChatNameselected_group); { |
| 264 | + await openGroupChat(selected_group, mainChatName); | |
| 265 | + } else { | |
| 266 | + await openCharacterChat(mainChatName); | |
| 267 | + } | |
| 268 | + return mainChatName; | |
| 245 | 269 | } |
| 270 | + | |
| 246 | 271 | return null; |
| 247 | 272 | } |
| 248 | 273 | |
| @@ -266,10 +266,6 @@ export async function getGroupChat(groupId, reload = false) { | ||
| 266 | 266 | await eventSource.emit(event_types.CHARACTER_MESSAGE_RENDERED, (chat.length - 1), 'first_message'); |
| 267 | 267 | } |
| 268 | 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 | } |
| 275 | 271 | |