diff --git a/public/script.js b/public/script.js index 77fff912a..2da8844f6 100644 --- a/public/script.js +++ b/public/script.js @@ -7372,26 +7372,30 @@ export async function unshallowCharacter(characterId) { } export async function getChat() { - //console.log('/api/chats/get -- entered for -- ' + characters[this_chid].name); try { await unshallowCharacter(this_chid); - const response = await $.ajax({ - type: 'POST', - url: '/api/chats/get', - data: JSON.stringify({ + const response = await fetch('/api/chats/get', { + method: 'POST', + headers: getRequestHeaders(), + cache: 'no-cache', + body: JSON.stringify({ ch_name: characters[this_chid].name, file_name: characters[this_chid].chat, avatar_url: characters[this_chid].avatar, }), - dataType: 'json', - contentType: 'application/json', }); - if (Array.isArray(response) && response.length > 0) { + + if (!response.ok) { + throw new Error('Chat could not be loaded'); + } + + const data = await response.json(); + if (Array.isArray(data) && data.length > 0) { /** @type {ChatHeader} */ - const chatHeader = response.shift(); + const chatHeader = data.shift(); chat_metadata = chatHeader?.chat_metadata ?? {}; - chat.splice(0, chat.length, ...response); + chat.splice(0, chat.length, ...data); chat.forEach(ensureMessageMediaIsArray); } else { // An empty/corrupted chat file @@ -7402,15 +7406,15 @@ export async function getChat() { chat_metadata.integrity = uuidv4(); } await getChatResult(); - eventSource.emit('chatLoaded', { detail: { id: this_chid, character: characters[this_chid] } }); + eventSource.emit(event_types.CHAT_LOADED, { detail: { id: this_chid, character: characters[this_chid] } }); // Focus on the textarea if not already focused on a visible text input - setTimeout(function () { + delay(debounce_timeout.short).then(() => { if ($(document.activeElement).is('input:visible, textarea:visible')) { return; } $('#send_textarea').trigger('click').trigger('focus'); - }, 200); + }); } catch (error) { await getChatResult(); console.log(error); diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index d2ae0160d..3006e4b53 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -765,7 +765,7 @@ class PromptManager { eventSource.on(event_types.CHATCOMPLETION_MODEL_CHANGED, () => this.renderDebounced()); // Re-render when the character changes. - eventSource.on('chatLoaded', (event) => { + eventSource.on(event_types.CHAT_LOADED, (event) => { this.handleCharacterSelected(event); this.saveServiceSettings().then(() => this.renderDebounced()); }); diff --git a/public/scripts/events.js b/public/scripts/events.js index d2635101a..e4952d4ef 100644 --- a/public/scripts/events.js +++ b/public/scripts/events.js @@ -16,6 +16,8 @@ export const event_types = { MORE_MESSAGES_LOADED: 'more_messages_loaded', IMPERSONATE_READY: 'impersonate_ready', CHAT_CHANGED: 'chat_id_changed', + // TODO: Naming convention is inconsistent with other events + CHAT_LOADED: 'chatLoaded', GENERATION_AFTER_COMMANDS: 'GENERATION_AFTER_COMMANDS', GENERATION_STARTED: 'generation_started', GENERATION_STOPPED: 'generation_stopped',