codacus commited on
Commit
df6925a
·
unverified ·
1 Parent(s): 80d9800

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

Browse files
Files changed (1) hide show
  1. app/components/chat/Chat.client.tsx +26 -5
app/components/chat/Chat.client.tsx CHANGED
@@ -21,6 +21,7 @@ import { debounce } from '~/utils/debounce';
21
  import { useSettings } from '~/lib/hooks/useSettings';
22
  import type { ProviderInfo } from '~/types/model';
23
  import { useSearchParams } from '@remix-run/react';
 
24
 
25
  const toastAnimation = cssTransition({
26
  enter: 'animated fadeInRight',
@@ -77,6 +78,24 @@ export function Chat() {
77
  );
78
  }
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  interface ChatProps {
81
  initialMessages: Message[];
82
  storeMessageHistory: (messages: Message[]) => Promise<void>;
@@ -169,11 +188,13 @@ export const ChatImpl = memo(
169
  }, []);
170
 
171
  useEffect(() => {
172
- parseMessages(messages, isLoading);
173
-
174
- if (messages.length > initialMessages.length) {
175
- storeMessageHistory(messages).catch((error) => toast.error(error.message));
176
- }
 
 
177
  }, [messages, isLoading, parseMessages]);
178
 
179
  const scrollTextArea = () => {
 
21
  import { useSettings } from '~/lib/hooks/useSettings';
22
  import type { ProviderInfo } from '~/types/model';
23
  import { useSearchParams } from '@remix-run/react';
24
+ import { createSampler } from '~/utils/sampler';
25
 
26
  const toastAnimation = cssTransition({
27
  enter: 'animated fadeInRight',
 
78
  );
79
  }
80
 
81
+ const processSampledMessages = createSampler(
82
+ (options: {
83
+ messages: Message[];
84
+ initialMessages: Message[];
85
+ isLoading: boolean;
86
+ parseMessages: (messages: Message[], isLoading: boolean) => void;
87
+ storeMessageHistory: (messages: Message[]) => Promise<void>;
88
+ }) => {
89
+ const { messages, initialMessages, isLoading, parseMessages, storeMessageHistory } = options;
90
+ parseMessages(messages, isLoading);
91
+
92
+ if (messages.length > initialMessages.length) {
93
+ storeMessageHistory(messages).catch((error) => toast.error(error.message));
94
+ }
95
+ },
96
+ 50,
97
+ );
98
+
99
  interface ChatProps {
100
  initialMessages: Message[];
101
  storeMessageHistory: (messages: Message[]) => Promise<void>;
 
188
  }, []);
189
 
190
  useEffect(() => {
191
+ processSampledMessages({
192
+ messages,
193
+ initialMessages,
194
+ isLoading,
195
+ parseMessages,
196
+ storeMessageHistory,
197
+ });
198
  }, [messages, isLoading, parseMessages]);
199
 
200
  const scrollTextArea = () => {