import { useState } from "react"; import { marked } from "marked"; import DOMPurify from "dompurify"; import BotIcon from "./icons/BotIcon"; import BrainIcon from "./icons/BrainIcon"; import UserIcon from "./icons/UserIcon"; import { MathJaxContext, MathJax } from "better-react-mathjax"; import "./Chat.css"; function render(text) { // Replace all instances of single backslashes before brackets with double backslashes // See https://github.com/markedjs/marked/issues/546 for more information. text = text.replace(/\\([\[\]\(\)])/g, "\\\\$1"); const result = DOMPurify.sanitize( marked.parse(text, { async: false, breaks: true, }), ); return result; } function Message({ role, content, answerIndex }) { const thinking = answerIndex !== undefined ? content.slice(0, answerIndex) : content; const answer = answerIndex !== undefined ? content.slice(answerIndex) : ""; const [showThinking, setShowThinking] = useState(false); const doneThinking = answerIndex === 0 || answer.length > 0; return (
{role === "assistant" ? ( <>
{answerIndex === 0 || thinking.length > 0 ? ( <> {thinking.length > 0 && (
{showThinking && ( )}
)} {doneThinking && ( )} ) : ( )}
) : ( <>

{content}

)}
); } export default function Chat({ messages }) { const empty = messages.length === 0; return (
{empty ? (
Ready!
) : ( messages.map((msg, i) => ) )}
); }