Update frontend/src/components/ChatInterface.jsx
Browse files
frontend/src/components/ChatInterface.jsx
CHANGED
@@ -21,28 +21,49 @@ saveBotResponse, toLogin }) => {
|
|
21 |
|
22 |
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
const streamResponse = (response) => {
|
27 |
setIsStreaming(true);
|
28 |
setFullResponse(response);
|
29 |
setStreamingText('');
|
30 |
|
31 |
-
|
32 |
-
let
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
const streamInterval = setInterval(() => {
|
35 |
-
if (
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
} else {
|
39 |
clearInterval(streamInterval);
|
40 |
setIsStreaming(false);
|
41 |
|
42 |
-
setMessages(prev =>
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
}
|
45 |
-
},
|
46 |
|
47 |
return () => clearInterval(streamInterval);
|
48 |
};
|
@@ -162,7 +183,7 @@ saveBotResponse, toLogin }) => {
|
|
162 |
</div>
|
163 |
);
|
164 |
})}
|
165 |
-
|
166 |
<div className="message bot">
|
167 |
<div className="message-content streaming-message">
|
168 |
{isMarkdown(streamingText) ? <ReactMarkdown>{streamingText}</ReactMarkdown> : streamingText}
|
|
|
21 |
|
22 |
|
23 |
|
24 |
+
const streamResponse = (response) => {
|
|
|
|
|
25 |
setIsStreaming(true);
|
26 |
setFullResponse(response);
|
27 |
setStreamingText('');
|
28 |
|
29 |
+
// Garder une référence au message de streaming
|
30 |
+
let streamMessageId = Date.now().toString();
|
31 |
+
|
32 |
+
// Ajouter un message de streaming avec un ID unique
|
33 |
+
setMessages(prev => [...prev, {
|
34 |
+
sender: 'bot-streaming',
|
35 |
+
text: '',
|
36 |
+
id: streamMessageId
|
37 |
+
}]);
|
38 |
+
|
39 |
+
const totalCharacters = response.length;
|
40 |
+
let charCount = 0;
|
41 |
|
42 |
const streamInterval = setInterval(() => {
|
43 |
+
if (charCount < totalCharacters) {
|
44 |
+
charCount += 5;
|
45 |
+
const fragment = response.substring(0, charCount);
|
46 |
+
|
47 |
+
setMessages(prev =>
|
48 |
+
prev.map(msg =>
|
49 |
+
msg.id === streamMessageId ? { ...msg, text: fragment } : msg
|
50 |
+
)
|
51 |
+
);
|
52 |
+
|
53 |
+
setStreamingText(fragment);
|
54 |
} else {
|
55 |
clearInterval(streamInterval);
|
56 |
setIsStreaming(false);
|
57 |
|
58 |
+
setMessages(prev =>
|
59 |
+
prev.map(msg =>
|
60 |
+
msg.id === streamMessageId
|
61 |
+
? { sender: 'bot', text: response, id: streamMessageId }
|
62 |
+
: msg
|
63 |
+
)
|
64 |
+
);
|
65 |
}
|
66 |
+
}, 30);
|
67 |
|
68 |
return () => clearInterval(streamInterval);
|
69 |
};
|
|
|
183 |
</div>
|
184 |
);
|
185 |
})}
|
186 |
+
{isStreaming && (
|
187 |
<div className="message bot">
|
188 |
<div className="message-content streaming-message">
|
189 |
{isMarkdown(streamingText) ? <ReactMarkdown>{streamingText}</ReactMarkdown> : streamingText}
|