Optimize performance of getExistingChatNames
| @@ -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 { | ||
| 7421 | await getChat(); | 7420 | 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 | } |
| @@ -14,7 +14,6 @@ import { | |||
| 14 | } from '../script.js'; | 14 | } from '../script.js'; |
| 15 | import { humanizedDateTime } from './RossAscends-mods.js'; | 15 | import { humanizedDateTime } from './RossAscends-mods.js'; |
| 16 | import { | 16 | import { |
| 17 | getGroupPastChats, | ||
| 18 | group_activation_strategy, | 17 | group_activation_strategy, |
| 19 | groups, | 18 | groups, |
| 20 | openGroupById, | 19 | openGroupById, |
| @@ -40,22 +39,42 @@ import { | |||
| 40 | 39 | ||
| 41 | const bookmarkNameToken = 'Checkpoint #'; | 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 | async function getExistingChatNames() { | 46 | async 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]; |
| 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 | |||
| 48 | const response = await fetch('/api/characters/chats', { | 65 | const response = await fetch('/api/characters/chats', { |
| 49 | method: 'POST', | 66 | method: 'POST', |
| 50 | headers: getRequestHeaders(), | 67 | headers: getRequestHeaders(), |
| 51 | body: JSON.stringify({ avatar_url: characters[this_chid].avatar }), | 68 | body: JSON.stringify({ avatar_url: character.avatar, simple: true }), |
| 52 | }); | 69 | }); |
| 53 | 70 | ||
| 54 | if (response.ok) { | 71 | if (response.ok) { |
| 55 | const data = await response.json(); | 72 | const data = await response.json(); |
| 56 | return Object.values(data).map(x => x.file_name.replace('.jsonl', '')); | 73 | const chats = Object.values(data).map(x => x.file_name.replace('.jsonl', '')); |
| 57 | } | 74 | return [...chats]; |
| 58 | } | 75 | } |
| 76 | |||
| 77 | return []; | ||
| 59 | } | 78 | } |
| 60 | 79 | ||
| 61 | async function getBookmarkName({ isReplace = false, forceName = null } = {}) { | 80 | async function getBookmarkName({ isReplace = false, forceName = null } = {}) { |
| @@ -238,11 +257,17 @@ export function updateBookmarkDisplay(mes, newBookmarkLink = null) { | |||
| 238 | 257 | ||
| 239 | async function backToMainChat() { | 258 | async function backToMainChat() { |
| 240 | const mainChatName = getMainChatName(); | 259 | const mainChatName = getMainChatName(); |
| 260 | const allChats = await getExistingChatNames(); | ||
| 261 | |||
| 262 | if (allChats.includes(mainChatName)) { | ||
| 241 | if (selected_group) { | 263 | if (selected_group) { |
| 242 | await openGroupChat(selected_group, mainChatName); | 264 | await openGroupChat(selected_group, mainChatName); |
| 243 | } else { | 265 | } else { |
| 244 | await openCharacterChat(mainChatName); | 266 | await openCharacterChat(mainChatName); |
| 245 | } | 267 | } |
| 268 | return mainChatName; | ||
| 269 | } | ||
| 270 | |||
| 246 | return null; | 271 | return null; |
| 247 | } | 272 | } |
| 248 | 273 | ||
| @@ -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 | } |
| 275 | 271 | ||