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