| @@ -7372,26 +7372,30 @@ export async function unshallowCharacter(characterId) { | ||
| 7372 | 7372 | } |
| 7373 | 7373 | |
| 7374 | 7374 | export async function getChat() { |
| 7375 | - //console.log('/api/chats/get -- entered for -- ' + characters[this_chid].name); | |
| 7376 | 7375 | try { |
| 7377 | 7376 | await unshallowCharacter(this_chid); |
| 7378 | 7377 | |
| 7379 | 7378 | const response = await $.ajaxfetch('/api/chats/get', { |
| 7380 | 7379 | typemethod: 'POST', |
| 7381 | - url: '/api/chats/get', | |
| 7380 | + headers: getRequestHeaders(), | |
| 7382 | - data: JSON.stringify({ | |
| 7381 | + cache: 'no-cache', | |
| 7382 | + body: JSON.stringify({ | |
| 7383 | 7383 | ch_name: characters[this_chid].name, |
| 7384 | 7384 | file_name: characters[this_chid].chat, |
| 7385 | 7385 | avatar_url: characters[this_chid].avatar, |
| 7386 | 7386 | }), |
| 7387 | - dataType: 'json', | |
| 7388 | - contentType: 'application/json', | |
| 7389 | 7387 | }); |
| 7390 | - if (Array.isArray(response) && response.length > 0) { | |
| 7388 | + | |
| 7389 | + if (!response.ok) { | |
| 7390 | + throw new Error('Chat could not be loaded'); | |
| 7391 | + } | |
| 7392 | + | |
| 7393 | + const data = await response.json(); | |
| 7394 | + if (Array.isArray(data) && data.length > 0) { | |
| 7391 | 7395 | /** @type {ChatHeader} */ |
| 7392 | 7396 | const chatHeader = responsedata.shift(); |
| 7393 | 7397 | chat_metadata = chatHeader?.chat_metadata ?? {}; |
| 7394 | 7398 | chat.splice(0, chat.length, ...responsedata); |
| 7395 | 7399 | chat.forEach(ensureMessageMediaIsArray); |
| 7396 | 7400 | } else { |
| 7397 | 7401 | // An empty/corrupted chat file |
| @@ -7402,15 +7406,15 @@ export async function getChat() { | ||
| 7402 | 7406 | chat_metadata.integrity = uuidv4(); |
| 7403 | 7407 | } |
| 7404 | 7408 | await getChatResult(); |
| 7405 | 7409 | eventSource.emit('chatLoaded'event_types.CHAT_LOADED, { detail: { id: this_chid, character: characters[this_chid] } }); |
| 7406 | 7410 | |
| 7407 | 7411 | // Focus on the textarea if not already focused on a visible text input |
| 7408 | - setTimeout(function () { | |
| 7412 | + delay(debounce_timeout.short).then(() => { | |
| 7409 | 7413 | if ($(document.activeElement).is('input:visible, textarea:visible')) { |
| 7410 | 7414 | return; |
| 7411 | 7415 | } |
| 7412 | 7416 | $('#send_textarea').trigger('click').trigger('focus'); |
| 7413 | 7417 | }, 200); |
| 7414 | 7418 | } catch (error) { |
| 7415 | 7419 | await getChatResult(); |
| 7416 | 7420 | console.log(error); |
| @@ -765,7 +765,7 @@ class PromptManager { | ||
| 765 | 765 | eventSource.on(event_types.CHATCOMPLETION_MODEL_CHANGED, () => this.renderDebounced()); |
| 766 | 766 | |
| 767 | 767 | // Re-render when the character changes. |
| 768 | 768 | eventSource.on('chatLoaded'event_types.CHAT_LOADED, (event) => { |
| 769 | 769 | this.handleCharacterSelected(event); |
| 770 | 770 | this.saveServiceSettings().then(() => this.renderDebounced()); |
| 771 | 771 | }); |
| @@ -16,6 +16,8 @@ export const event_types = { | ||
| 16 | 16 | MORE_MESSAGES_LOADED: 'more_messages_loaded', |
| 17 | 17 | IMPERSONATE_READY: 'impersonate_ready', |
| 18 | 18 | CHAT_CHANGED: 'chat_id_changed', |
| 19 | + // TODO: Naming convention is inconsistent with other events | |
| 20 | + CHAT_LOADED: 'chatLoaded', | |
| 19 | 21 | GENERATION_AFTER_COMMANDS: 'GENERATION_AFTER_COMMANDS', |
| 20 | 22 | GENERATION_STARTED: 'generation_started', |
| 21 | 23 | GENERATION_STOPPED: 'generation_stopped', |