fix: add Message Processing Throttling to Prevent Browser Crashes (#848)

This commit is contained in:
Anirban Kar 2024-12-21 02:13:29 +05:30 committed by GitHub
parent 80d9800aa0
commit df6925ac07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,6 +21,7 @@ import { debounce } from '~/utils/debounce';
import { useSettings } from '~/lib/hooks/useSettings'; import { useSettings } from '~/lib/hooks/useSettings';
import type { ProviderInfo } from '~/types/model'; import type { ProviderInfo } from '~/types/model';
import { useSearchParams } from '@remix-run/react'; import { useSearchParams } from '@remix-run/react';
import { createSampler } from '~/utils/sampler';
const toastAnimation = cssTransition({ const toastAnimation = cssTransition({
enter: 'animated fadeInRight', enter: 'animated fadeInRight',
@ -77,6 +78,24 @@ export function Chat() {
); );
} }
const processSampledMessages = createSampler(
(options: {
messages: Message[];
initialMessages: Message[];
isLoading: boolean;
parseMessages: (messages: Message[], isLoading: boolean) => void;
storeMessageHistory: (messages: Message[]) => Promise<void>;
}) => {
const { messages, initialMessages, isLoading, parseMessages, storeMessageHistory } = options;
parseMessages(messages, isLoading);
if (messages.length > initialMessages.length) {
storeMessageHistory(messages).catch((error) => toast.error(error.message));
}
},
50,
);
interface ChatProps { interface ChatProps {
initialMessages: Message[]; initialMessages: Message[];
storeMessageHistory: (messages: Message[]) => Promise<void>; storeMessageHistory: (messages: Message[]) => Promise<void>;
@ -169,11 +188,13 @@ export const ChatImpl = memo(
}, []); }, []);
useEffect(() => { useEffect(() => {
parseMessages(messages, isLoading); processSampledMessages({
messages,
if (messages.length > initialMessages.length) { initialMessages,
storeMessageHistory(messages).catch((error) => toast.error(error.message)); isLoading,
} parseMessages,
storeMessageHistory,
});
}, [messages, isLoading, parseMessages]); }, [messages, isLoading, parseMessages]);
const scrollTextArea = () => { const scrollTextArea = () => {