/* BioReveal — AI chat sidekick. * * Floating button on the Reveal page → slides in a chat panel. * Each user message → POST /api/chat with the patient's full intake + * generated protocol as system context, plus the running conversation. * Claude (or rules engine) answers as a smart-friend-with-clinical-training. */ const SUGGESTED_QUESTIONS = [ "Why did you pick this dose for me?", "What if I miss a dose?", "Can I stack creatine on top?", "What time of day should I inject?", "How will I know if it's not working?", "What's the cheapest version of this protocol?", ]; const ChatSidekick = ({ profile }) => { const [open, setOpen] = React.useState(false); const [messages, setMessages] = React.useState([ { role: "assistant", content: "Hey — I'm the AI clinician for your protocol. Ask me anything: dosing, side effects, lifestyle adjustments, what to do if something feels off. I have your full intake + the analysis you just got." }, ]); const [input, setInput] = React.useState(""); const [sending, setSending] = React.useState(false); const scrollRef = React.useRef(null); const inputRef = React.useRef(null); React.useEffect(() => { if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight; }, [messages, sending]); React.useEffect(() => { if (open && inputRef.current) inputRef.current.focus(); }, [open]); const send = async (text) => { const t = (text ?? input).trim(); if (!t || sending) return; setInput(""); setSending(true); const newMessages = [...messages, { role: "user", content: t }]; setMessages(newMessages); try { const res = await fetch("/api/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ intake: profile.intake, protocol: profile.protocol, messages: newMessages.map(m => ({ role: m.role, content: m.content })), }), }); const data = await res.json(); const reply = data.reply || data.error || "(no response)"; setMessages([...newMessages, { role: "assistant", content: reply, engine: data.engine }]); } catch (e) { setMessages([...newMessages, { role: "assistant", content: `Network error: ${e.message}. Try again.` }]); } finally { setSending(false); } }; const onKey = (e) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); send(); } }; return ( <> {/* Floating button */} {!open && ( )} {/* Slide-in panel */} {open && (