AxL95 commited on
Commit
dd8b5df
·
verified ·
1 Parent(s): 863ac53

Update frontend/src/components/ChatInterface.jsx

Browse files
frontend/src/components/ChatInterface.jsx CHANGED
@@ -3,7 +3,8 @@ import ReactMarkdown from 'react-markdown';
3
  import Avatar from './Avatar.jsx';
4
  import '../App.css';
5
 
6
- const ChatInterface = ({ messages = [], setMessages = () => {}, onMessageSent = () => {}, toLogin }) => {
 
7
  const [inputMessage, setInputMessage] = useState('');
8
  const [isLoading, setIsLoading] = useState(false);
9
  const messagesEndRef = useRef(null);
@@ -17,38 +18,40 @@ const ChatInterface = ({ messages = [], setMessages = () => {}, onMessageSent =
17
  const sendMessage = async (message) => {
18
  try {
19
  setIsLoading(true);
20
- onMessageSent(message);
21
-
22
- // 1. Récupération de l'embedding
23
- const embedRes = await fetch('/api/embed', {
24
- method: 'POST',
25
- headers: { 'Content-Type': 'application/json' },
26
- body: JSON.stringify({ texts: [message] }),
27
- });
28
- if (!embedRes.ok) throw new Error(`Embed API error ${embedRes.status}`);
29
- const { embeddings } = await embedRes.json();
30
- const userEmbedding = embeddings[0];
31
-
32
- // 2. Appel à l'endpoint chat avec embedding
33
  const chatRes = await fetch('/api/chat', {
34
  method: 'POST',
35
  headers: { 'Content-Type': 'application/json' },
36
- body: JSON.stringify({ message, embedding: userEmbedding }),
 
37
  });
 
38
  if (!chatRes.ok) throw new Error(`Chat API error ${chatRes.status}`);
 
39
  const { response: botResponse } = await chatRes.json();
40
-
41
- setMessages(prev => [
42
- ...prev,
43
- { sender: 'user', text: message },
44
- { sender: 'bot', text: botResponse }
45
- ]);
 
 
 
 
 
46
  } catch (error) {
47
  console.error('Erreur:', error);
48
- setMessages(prev => [
49
- ...prev,
50
  { sender: 'user', text: message },
51
- { sender: 'bot', text: "Désolé, une erreur s'est produite. Veuillez réessayer. 👍" }
52
  ]);
53
  } finally {
54
  setIsLoading(false);
 
3
  import Avatar from './Avatar.jsx';
4
  import '../App.css';
5
 
6
+ const ChatInterface = ({ messages = [], setMessages = () => {}, onMessageSent = () => {}, activeConversationId,
7
+ saveBotResponse, toLogin }) => {
8
  const [inputMessage, setInputMessage] = useState('');
9
  const [isLoading, setIsLoading] = useState(false);
10
  const messagesEndRef = useRef(null);
 
18
  const sendMessage = async (message) => {
19
  try {
20
  setIsLoading(true);
21
+
22
+ // Ajouter le message utilisateur à l'interface
23
+ setMessages(prev => [...prev, { sender: 'user', text: message }]);
24
+
25
+ // IMPORTANT: Obtenir l'ID de conversation mis à jour (nouvelle ou existante)
26
+ const updatedConversationId = await onMessageSent(message);
27
+
28
+ // Appel à l'API de chat
 
 
 
 
 
29
  const chatRes = await fetch('/api/chat', {
30
  method: 'POST',
31
  headers: { 'Content-Type': 'application/json' },
32
+ credentials: 'include',
33
+ body: JSON.stringify({ message }),
34
  });
35
+
36
  if (!chatRes.ok) throw new Error(`Chat API error ${chatRes.status}`);
37
+
38
  const { response: botResponse } = await chatRes.json();
39
+
40
+ // Ajouter la réponse du bot à l'interface
41
+ setMessages(prev => [...prev, { sender: 'bot', text: botResponse }]);
42
+
43
+ // Utiliser l'ID de conversation mis à jour
44
+ if (updatedConversationId) {
45
+ saveBotResponse(updatedConversationId, botResponse);
46
+ } else if (activeConversationId) {
47
+ saveBotResponse(activeConversationId, botResponse);
48
+ }
49
+
50
  } catch (error) {
51
  console.error('Erreur:', error);
52
+ setMessages(prev => [...prev,
 
53
  { sender: 'user', text: message },
54
+ { sender: 'bot', text: "Désolé, une erreur s'est produite. Veuillez réessayer." }
55
  ]);
56
  } finally {
57
  setIsLoading(false);