import React, { useState, useRef, useEffect } from 'react'; import ReactMarkdown from 'react-markdown'; import Avatar from './Avatar.jsx'; import '../App.css'; const ChatInterface = ({ messages = [], setMessages = () => {}, onMessageSent = () => {}, toLogin }) => { const [inputMessage, setInputMessage] = useState(''); const [isLoading, setIsLoading] = useState(false); const messagesEndRef = useRef(null); const textareaRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(scrollToBottom, [messages]); const sendMessage = async (message) => { try { setIsLoading(true); onMessageSent(message); // 1. Récupération de l'embedding const embedRes = await fetch('/api/embed', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ texts: [message] }), }); if (!embedRes.ok) throw new Error(`Embed API error ${embedRes.status}`); const { embeddings } = await embedRes.json(); const userEmbedding = embeddings[0]; // 2. Appel à l'endpoint chat avec embedding const chatRes = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message, embedding: userEmbedding }), }); if (!chatRes.ok) throw new Error(`Chat API error ${chatRes.status}`); const { response: botResponse } = await chatRes.json(); setMessages(prev => [ ...prev, { sender: 'user', text: message }, { sender: 'bot', text: botResponse } ]); } catch (error) { console.error('Erreur:', error); setMessages(prev => [ ...prev, { sender: 'user', text: message }, { sender: 'bot', text: "Désolé, une erreur s'est produite. Veuillez réessayer. 👍" } ]); } finally { setIsLoading(false); } }; const handleSubmit = (e) => { e.preventDefault(); const txt = inputMessage.trim(); if (!txt) return; sendMessage(txt); setInputMessage(''); if (textareaRef.current) textareaRef.current.style.height = 'auto'; }; const isMarkdown = (text) => /[#*_>`-]/.test(text); return (
{messages.length === 0 ? ( <>

Medic.ial

Bonjour ! Comment puis-je vous aider aujourd'hui ? 🧑‍⚕️