Update frontend/src/components/ChatInterface.jsx
Browse files
frontend/src/components/ChatInterface.jsx
CHANGED
@@ -12,7 +12,9 @@ saveBotResponse, toLogin }) => {
|
|
12 |
const [streamingText, setStreamingText] = useState('');
|
13 |
const [isStreaming, setIsStreaming] = useState(false);
|
14 |
const [fullResponse, setFullResponse] = useState('');
|
15 |
-
|
|
|
|
|
16 |
const scrollToBottom = () => {
|
17 |
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
18 |
};
|
@@ -70,6 +72,8 @@ saveBotResponse, toLogin }) => {
|
|
70 |
|
71 |
const sendMessage = async (message) => {
|
72 |
try {
|
|
|
|
|
73 |
setIsLoading(true);
|
74 |
|
75 |
setMessages(prev => [...prev, { sender: 'user', text: message }]);
|
@@ -80,15 +84,33 @@ saveBotResponse, toLogin }) => {
|
|
80 |
method: 'POST',
|
81 |
headers: { 'Content-Type': 'application/json' },
|
82 |
credentials: 'include',
|
83 |
-
body: JSON.stringify({
|
|
|
|
|
|
|
84 |
});
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
if (!chatRes.ok) throw new Error(`Chat API error ${chatRes.status}`);
|
87 |
|
88 |
-
const { response: botResponse } =
|
89 |
|
90 |
setIsLoading(false);
|
91 |
-
|
92 |
setMessages(prev => [...prev, { sender: 'bot-streaming', text: '' }]);
|
93 |
|
94 |
streamResponse(botResponse);
|
@@ -108,6 +130,19 @@ saveBotResponse, toLogin }) => {
|
|
108 |
}
|
109 |
};
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
const handleSubmit = (e) => {
|
113 |
e.preventDefault();
|
@@ -124,7 +159,7 @@ const isMarkdown = (text) => {
|
|
124 |
};
|
125 |
return (
|
126 |
<div className="chat-container">
|
127 |
-
{messages.length === 0 ? (
|
128 |
<>
|
129 |
<div className="chat-header">
|
130 |
<h2 className="chat-title">Medic.ial</h2>
|
@@ -173,24 +208,34 @@ const isMarkdown = (text) => {
|
|
173 |
<h2 className="chat-title">Medic.ial</h2>
|
174 |
</div>
|
175 |
<div className="messages-container">
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
183 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
</div>
|
185 |
-
)
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
</div>
|
192 |
-
|
193 |
-
)}
|
194 |
|
195 |
{isLoading && (
|
196 |
<div className="message bot">
|
@@ -206,8 +251,8 @@ const isMarkdown = (text) => {
|
|
206 |
<textarea
|
207 |
value={inputMessage}
|
208 |
onChange={(e) => setInputMessage(e.target.value)}
|
209 |
-
placeholder="Tapez votre message ici..."
|
210 |
-
disabled={isLoading}
|
211 |
rows="1"
|
212 |
ref={textareaRef}
|
213 |
className="input-textarea"
|
@@ -222,8 +267,8 @@ const isMarkdown = (text) => {
|
|
222 |
e.target.style.height = `${e.target.scrollHeight}px`;
|
223 |
}}
|
224 |
/>
|
225 |
-
<button type="submit" disabled={isLoading || !inputMessage.trim()}>
|
226 |
-
|
227 |
<path d="M120-160v-240l320-80-320-80v-240l760 320-760 320Z"/>
|
228 |
</svg>
|
229 |
</button>
|
|
|
12 |
const [streamingText, setStreamingText] = useState('');
|
13 |
const [isStreaming, setIsStreaming] = useState(false);
|
14 |
const [fullResponse, setFullResponse] = useState('');
|
15 |
+
const [tokenLimitReached, setTokenLimitReached] = useState(false);
|
16 |
+
const [hasInteractionStarted, setHasInteractionStarted] = useState(false);
|
17 |
+
|
18 |
const scrollToBottom = () => {
|
19 |
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
20 |
};
|
|
|
72 |
|
73 |
const sendMessage = async (message) => {
|
74 |
try {
|
75 |
+
setHasInteractionStarted(true);
|
76 |
+
|
77 |
setIsLoading(true);
|
78 |
|
79 |
setMessages(prev => [...prev, { sender: 'user', text: message }]);
|
|
|
84 |
method: 'POST',
|
85 |
headers: { 'Content-Type': 'application/json' },
|
86 |
credentials: 'include',
|
87 |
+
body: JSON.stringify({
|
88 |
+
message,
|
89 |
+
conversation_id: activeConversationId
|
90 |
+
}),
|
91 |
});
|
92 |
|
93 |
+
const responseData = await chatRes.json();
|
94 |
+
|
95 |
+
// Vérifier si la limite de tokens est atteinte
|
96 |
+
if (responseData.error === 'token_limit_exceeded') {
|
97 |
+
setIsLoading(false);
|
98 |
+
setTokenLimitReached(true);
|
99 |
+
|
100 |
+
// Afficher un message à l'utilisateur
|
101 |
+
setMessages(prev => [...prev, {
|
102 |
+
sender: 'bot',
|
103 |
+
text: "⚠️ **Limite de taille de conversation atteinte**\n\nCette conversation est devenue trop longue. Pour continuer à discuter, veuillez créer une nouvelle conversation."
|
104 |
+
}]);
|
105 |
+
|
106 |
+
return;
|
107 |
+
}
|
108 |
+
|
109 |
if (!chatRes.ok) throw new Error(`Chat API error ${chatRes.status}`);
|
110 |
|
111 |
+
const { response: botResponse } = responseData;
|
112 |
|
113 |
setIsLoading(false);
|
|
|
114 |
setMessages(prev => [...prev, { sender: 'bot-streaming', text: '' }]);
|
115 |
|
116 |
streamResponse(botResponse);
|
|
|
130 |
}
|
131 |
};
|
132 |
|
133 |
+
const handleCreateNewConversation = () => {
|
134 |
+
onNewChat();
|
135 |
+
setTokenLimitReached(false);
|
136 |
+
setHasInteractionStarted(false);
|
137 |
+
|
138 |
+
};
|
139 |
+
useEffect(() => {
|
140 |
+
if (activeConversationId === null && messages.length === 0) {
|
141 |
+
setHasInteractionStarted(false);
|
142 |
+
}
|
143 |
+
}, [activeConversationId, messages]);
|
144 |
+
|
145 |
+
|
146 |
|
147 |
const handleSubmit = (e) => {
|
148 |
e.preventDefault();
|
|
|
159 |
};
|
160 |
return (
|
161 |
<div className="chat-container">
|
162 |
+
{messages.length === 0 && !hasInteractionStarted ? (
|
163 |
<>
|
164 |
<div className="chat-header">
|
165 |
<h2 className="chat-title">Medic.ial</h2>
|
|
|
208 |
<h2 className="chat-title">Medic.ial</h2>
|
209 |
</div>
|
210 |
<div className="messages-container">
|
211 |
+
{messages.map((msg, index) => {
|
212 |
+
if (msg.sender === 'bot-streaming') return null;
|
213 |
+
|
214 |
+
return (
|
215 |
+
<div key={index} className={`message ${msg.sender}`}>
|
216 |
+
<div className="message-content">
|
217 |
+
{isMarkdown(msg.text) ? <ReactMarkdown>{msg.text}</ReactMarkdown> : msg.text}
|
218 |
+
</div>
|
219 |
</div>
|
220 |
+
);
|
221 |
+
})}
|
222 |
+
{tokenLimitReached && (
|
223 |
+
<div className="token-limit-warning">
|
224 |
+
<button
|
225 |
+
className="new-conversation-button"
|
226 |
+
onClick={handleCreateNewConversation}
|
227 |
+
>
|
228 |
+
Démarrer une nouvelle conversation
|
229 |
+
</button>
|
230 |
</div>
|
231 |
+
)}
|
232 |
+
{isStreaming && (
|
233 |
+
<div className="message bot">
|
234 |
+
<div className="message-content streaming-message">
|
235 |
+
{isMarkdown(streamingText) ? <ReactMarkdown>{streamingText}</ReactMarkdown> : streamingText}
|
236 |
+
</div>
|
237 |
</div>
|
238 |
+
)}
|
|
|
239 |
|
240 |
{isLoading && (
|
241 |
<div className="message bot">
|
|
|
251 |
<textarea
|
252 |
value={inputMessage}
|
253 |
onChange={(e) => setInputMessage(e.target.value)}
|
254 |
+
placeholder={tokenLimitReached ? "Créez une nouvelle conversation pour continuer..." : "Tapez votre message ici..."}
|
255 |
+
disabled={isLoading || tokenLimitReached}
|
256 |
rows="1"
|
257 |
ref={textareaRef}
|
258 |
className="input-textarea"
|
|
|
267 |
e.target.style.height = `${e.target.scrollHeight}px`;
|
268 |
}}
|
269 |
/>
|
270 |
+
<button type="submit" disabled={isLoading || !inputMessage.trim() || tokenLimitReached}>
|
271 |
+
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#e3e3e3">
|
272 |
<path d="M120-160v-240l320-80-320-80v-240l760 320-760 320Z"/>
|
273 |
</svg>
|
274 |
</button>
|