"use client" import { useState, useRef, useEffect } from "react" import type { ChatMessage } from "@/lib/types" import ReactMarkdown from "react-markdown" interface Props { messages: ChatMessage[] onSend: (answer: string) => void isWaiting: boolean } export default function ChatInterface({ messages, onSend, isWaiting }: Props) { const [input, setInput] = useState("") const endRef = useRef(null) useEffect(() => { endRef.current?.scrollIntoView({ behavior: "smooth" }) }, [messages]) const handleSend = () => { if (!input.trim() || isWaiting) return onSend(input) setInput("") } const renderAgentMessage = (content: string) => { const lines = content.split('\n') const options: string[] = [] const mainText: string[] = [] const optionRegex = /^[-*]?\s*([A-D])[\)\.]\s+(.*)/i; for (const line of lines) { const match = line.trim().match(optionRegex) if (match) { options.push(line.trim()) } else { mainText.push(line) } } return (
{mainText.join('\n')}
{options.length > 0 && (
{options.map((opt, i) => { const cleanOpt = opt.replace(/^[-*]?\s*/, '') const match = cleanOpt.match(/^([A-D])[\)\.]/i) const letter = match ? match[1] : cleanOpt return ( ) })}
)}
) } return (
{messages.map((m) => (
{m.role === "agent" && (
SF
)} {m.role === "challenge" ? (
Challenge
{m.content}
Submit your answer below
) : m.role === "system" ? (
{m.content}
) : m.role === "user" ? (
{m.content}
) : (
{renderAgentMessage(m.content)}
)}
))} {isWaiting && (
SF
)}