Replace $.ajax with fetch (#4978)

8a32b72dfea21a678864c561cd74ae3c4caab82e

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

Signed
3 files changed, +20 -14Ignore whitespace
public/script.js+17 -13
@@ -7372,26 +7372,30 @@ export async function unshallowCharacter(characterId) {
73727372}
73737373
73747374export async function getChat() {
7375- //console.log('/api/chats/get -- entered for -- ' + characters[this_chid].name);
73767375 try {
73777376 await unshallowCharacter(this_chid);
73787377
73797378 const response = await $.ajaxfetch('/api/chats/get', {
73807379 typemethod: 'POST',
7381- url: '/api/chats/get',
7380+ headers: getRequestHeaders(),
7382- data: JSON.stringify({
7381+ cache: 'no-cache',
7382+ body: JSON.stringify({
73837383 ch_name: characters[this_chid].name,
73847384 file_name: characters[this_chid].chat,
73857385 avatar_url: characters[this_chid].avatar,
73867386 }),
7387- dataType: 'json',
7388- contentType: 'application/json',
73897387 });
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) {
73917395 /** @type {ChatHeader} */
73927396 const chatHeader = responsedata.shift();
73937397 chat_metadata = chatHeader?.chat_metadata ?? {};
73947398 chat.splice(0, chat.length, ...responsedata);
73957399 chat.forEach(ensureMessageMediaIsArray);
73967400 } else {
73977401 // An empty/corrupted chat file
@@ -7402,15 +7406,15 @@ export async function getChat() {
74027406 chat_metadata.integrity = uuidv4();
74037407 }
74047408 await getChatResult();
74057409 eventSource.emit('chatLoaded'event_types.CHAT_LOADED, { detail: { id: this_chid, character: characters[this_chid] } });
74067410
74077411 // Focus on the textarea if not already focused on a visible text input
7408- setTimeout(function () {
7412+ delay(debounce_timeout.short).then(() => {
74097413 if ($(document.activeElement).is('input:visible, textarea:visible')) {
74107414 return;
74117415 }
74127416 $('#send_textarea').trigger('click').trigger('focus');
74137417 }, 200);
74147418 } catch (error) {
74157419 await getChatResult();
74167420 console.log(error);
public/scripts/PromptManager.js+1 -1
@@ -765,7 +765,7 @@ class PromptManager {
765765 eventSource.on(event_types.CHATCOMPLETION_MODEL_CHANGED, () => this.renderDebounced());
766766
767767 // Re-render when the character changes.
768768 eventSource.on('chatLoaded'event_types.CHAT_LOADED, (event) => {
769769 this.handleCharacterSelected(event);
770770 this.saveServiceSettings().then(() => this.renderDebounced());
771771 });
public/scripts/events.js+2 -0
@@ -16,6 +16,8 @@ export const event_types = {
1616 MORE_MESSAGES_LOADED: 'more_messages_loaded',
1717 IMPERSONATE_READY: 'impersonate_ready',
1818 CHAT_CHANGED: 'chat_id_changed',
19+ // TODO: Naming convention is inconsistent with other events
20+ CHAT_LOADED: 'chatLoaded',
1921 GENERATION_AFTER_COMMANDS: 'GENERATION_AFTER_COMMANDS',
2022 GENERATION_STARTED: 'generation_started',
2123 GENERATION_STOPPED: 'generation_stopped',