import { useState, useRef, useEffect } from "react"; import { useNavigate, useParams } from "react-router"; import { motion, AnimatePresence } from "motion/react"; import { ChevronLeft, Send, Sparkles, Lightbulb, User, Bot, } from "lucide-react"; import { Button } from "../components/ui/button"; import { Input } from "../components/ui/input"; import { Card } from "../components/ui/card"; import { sendChatMessage, getChatHistory } from "../services/api"; interface Message { id: string; role: "user" | "assistant"; content: string; timestamp: Date; } export function AIChatPage() { const navigate = useNavigate(); const { reportId } = useParams(); const [messages, setMessages] = useState([ { id: "1", role: "assistant", content: "您好!我是您的孕期健康助手。您可以向我询问报告中的任何指标,我会结合医学知识为您详细解答。", timestamp: new Date(), }, ]); const [input, setInput] = useState(""); const [isTyping, setIsTyping] = useState(false); const [quickQuestions, setQuickQuestions] = useState([ "这个指标偏高对宝宝有影响吗?", "我需要补充什么营养?", "下次检查需要注意什么?", "这种情况多久会恢复正常?", ]); const messagesEndRef = useRef(null); // Load chat history on mount useEffect(() => { if (!reportId) return; getChatHistory(reportId).then((data) => { if (data.messages.length > 0) { setMessages( data.messages.map((m, i) => ({ id: String(i), role: m.role as "user" | "assistant", content: m.content, timestamp: new Date(m.timestamp), })) ); } if (data.quick_questions.length > 0) { setQuickQuestions(data.quick_questions); } }).catch(() => {/* keep defaults */}); }, [reportId]); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }; useEffect(() => { scrollToBottom(); }, [messages]); const handleSend = async () => { if (!input.trim() || !reportId) return; const userMessage: Message = { id: Date.now().toString(), role: "user", content: input, timestamp: new Date(), }; setMessages((prev) => [...prev, userMessage]); const question = input; setInput(""); setIsTyping(true); try { const result = await sendChatMessage(reportId, question); const aiResponse: Message = { id: (Date.now() + 1).toString(), role: "assistant", content: result.ai_message.content, timestamp: new Date(result.ai_message.timestamp), }; setMessages((prev) => [...prev, aiResponse]); } catch { const errorMsg: Message = { id: (Date.now() + 1).toString(), role: "assistant", content: "抱歉,AI 服务暂时不可用,请稍后重试。", timestamp: new Date(), }; setMessages((prev) => [...prev, errorMsg]); } finally { setIsTyping(false); } }; const handleQuickQuestion = (question: string) => { setInput(question); }; return (
{/* 顶部导航 */}

AI 健康顾问

智能解答,随时陪伴

{/* 消息列表 */}
{messages.map((message) => (
{message.role === "user" ? ( ) : ( )}

{message.content}

{message.timestamp.toLocaleTimeString("zh-CN", { hour: "2-digit", minute: "2-digit", })}

))}
{isTyping && (
)}
{/* 快捷提问 */} {messages.length <= 1 && (

试试这样问:

{quickQuestions.map((question, index) => ( ))}
)} {/* 输入框 */}
setInput(e.target.value)} onKeyPress={(e) => e.key === "Enter" && handleSend()} placeholder="输入您的问题..." className="flex-1 rounded-full border-gray-200 focus:border-morandi-200" />
); }