Null safety for streaming processor

c12a283efcbced3533cf882e6fd57ca4867ace42

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

1 files changed, +23 -8Showing whitespace changes
public/script.js+23 -8
@@ -2800,6 +2800,8 @@ class StreamingProcessor {
2800 this.messageDom = null;2800 this.messageDom = null;
2801 this.messageTextDom = null;2801 this.messageTextDom = null;
2802 this.messageTimerDom = null;2802 this.messageTimerDom = null;
2803 this.messageTokenCounterDom = null;
2804 /** @type {HTMLTextAreaElement} */
2803 this.sendTextarea = document.querySelector('#send_textarea');2805 this.sendTextarea = document.querySelector('#send_textarea');
2804 this.type = type;2806 this.type = type;
2805 this.force_name2 = force_name2;2807 this.force_name2 = force_name2;
@@ -2815,6 +2817,15 @@ class StreamingProcessor {
2815 this.messageLogprobs = [];2817 this.messageLogprobs = [];
2816 }2818 }
28172819
2820 #checkDomElements(messageId) {
2821 if (this.messageDom === null) {
2822 this.messageDom = document.querySelector(`#chat .mes[mesid="${messageId}"]`);
2823 this.messageTextDom = this.messageDom?.querySelector('.mes_text');
2824 this.messageTimerDom = this.messageDom?.querySelector('.mes_timer');
2825 this.messageTokenCounterDom = this.messageDom?.querySelector('.tokenCounterDisplay');
2826 }
2827 }
2828
2818 showMessageButtons(messageId) {2829 showMessageButtons(messageId) {
2819 if (messageId == -1) {2830 if (messageId == -1) {
2820 return;2831 return;
@@ -2843,9 +2854,7 @@ class StreamingProcessor {
2843 else {2854 else {
2844 await saveReply(this.type, text, true);2855 await saveReply(this.type, text, true);
2845 messageId = chat.length - 1;2856 messageId = chat.length - 1;
2846 this.messageDom = document.querySelector(`#chat .mes[mesid="${messageId}"]`);2857 this.#checkDomElements(messageId);
2847 this.messageTextDom = this.messageDom.querySelector('.mes_text');
2848 this.messageTimerDom = this.messageDom.querySelector('.mes_timer');
2849 this.showMessageButtons(messageId);2858 this.showMessageButtons(messageId);
2850 }2859 }
28512860
@@ -2881,9 +2890,9 @@ class StreamingProcessor {
2881 this.sendTextarea.dispatchEvent(new Event('input', { bubbles: true }));2890 this.sendTextarea.dispatchEvent(new Event('input', { bubbles: true }));
2882 }2891 }
2883 else {2892 else {
2884 let currentTime = new Date();2893 const currentTime = new Date();
2885 // Don't waste time calculating token count for streaming2894 // Don't waste time calculating token count for streaming
2886 let currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0;2895 const currentTokenCount = isFinal && power_user.message_token_count_enabled ? getTokenCount(processedText, 0) : 0;
2887 const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);2896 const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount);
2888 chat[messageId]['mes'] = processedText;2897 chat[messageId]['mes'] = processedText;
2889 chat[messageId]['gen_started'] = this.timeStarted;2898 chat[messageId]['gen_started'] = this.timeStarted;
@@ -2895,8 +2904,9 @@ class StreamingProcessor {
2895 }2904 }
28962905
2897 chat[messageId]['extra']['token_count'] = currentTokenCount;2906 chat[messageId]['extra']['token_count'] = currentTokenCount;
2898 const tokenCounter = $(`#chat .mes[mesid="${messageId}"] .tokenCounterDisplay`);2907 if (this.messageTokenCounterDom instanceof HTMLElement) {
2899 tokenCounter.text(`${currentTokenCount}t`);2908 this.messageTokenCounterDom.textContent = `${currentTokenCount}t`;
2909 }
2900 }2910 }
29012911
2902 if ((this.type == 'swipe' || this.type === 'continue') && Array.isArray(chat[messageId]['swipes'])) {2912 if ((this.type == 'swipe' || this.type === 'continue') && Array.isArray(chat[messageId]['swipes'])) {
@@ -2904,16 +2914,21 @@ class StreamingProcessor {
2904 chat[messageId]['swipe_info'][chat[messageId]['swipe_id']] = { 'send_date': chat[messageId]['send_date'], 'gen_started': chat[messageId]['gen_started'], 'gen_finished': chat[messageId]['gen_finished'], 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])) };2914 chat[messageId]['swipe_info'][chat[messageId]['swipe_id']] = { 'send_date': chat[messageId]['send_date'], 'gen_started': chat[messageId]['gen_started'], 'gen_finished': chat[messageId]['gen_finished'], 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])) };
2905 }2915 }
29062916
2907 let formattedText = messageFormatting(2917 const formattedText = messageFormatting(
2908 processedText,2918 processedText,
2909 chat[messageId].name,2919 chat[messageId].name,
2910 chat[messageId].is_system,2920 chat[messageId].is_system,
2911 chat[messageId].is_user,2921 chat[messageId].is_user,
2912 messageId,2922 messageId,
2913 );2923 );
2924 this.#checkDomElements(messageId);
2925 if (this.messageTextDom instanceof HTMLElement) {
2914 this.messageTextDom.innerHTML = formattedText;2926 this.messageTextDom.innerHTML = formattedText;
2927 }
2928 if (this.messageTimerDom instanceof HTMLElement) {
2915 this.messageTimerDom.textContent = timePassed.timerValue;2929 this.messageTimerDom.textContent = timePassed.timerValue;
2916 this.messageTimerDom.title = timePassed.timerTitle;2930 this.messageTimerDom.title = timePassed.timerTitle;
2931 }
2917 this.setFirstSwipe(messageId);2932 this.setFirstSwipe(messageId);
2918 }2933 }
29192934