"use client" import { useState, useRef, useEffect } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { MessageCircle, Send, Loader2, User, Bot } from "lucide-react" import { cn } from "@/lib/utils" interface Message { role: "user" | "assistant" content: string timestamp: Date } export default function ChatPage() { const [messages, setMessages] = useState([]) const [input, setInput] = useState("") const [loading, setLoading] = useState(false) const messagesEndRef = useRef(null) const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }) } useEffect(() => { scrollToBottom() }, [messages]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!input.trim() || loading) return const userMessage: Message = { role: "user", content: input.trim(), timestamp: new Date() } setMessages(prev => [...prev, userMessage]) setInput("") setLoading(true) try { const response = await fetch("/api/chat", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ prompt: userMessage.content }), }) const result = await response.json() if (response.ok) { const assistantMessage: Message = { role: "assistant", content: result.response, timestamp: new Date() } setMessages(prev => [...prev, assistantMessage]) } else { console.error("Chat failed:", result.error) const errorMessage: Message = { role: "assistant", content: "Sorry, I encountered an error. Please try again.", timestamp: new Date() } setMessages(prev => [...prev, errorMessage]) } } catch (error) { console.error("Chat error:", error) const errorMessage: Message = { role: "assistant", content: "Sorry, I couldn't connect to the chat service. Please try again.", timestamp: new Date() } setMessages(prev => [...prev, errorMessage]) } finally { setLoading(false) } } return (

Chat Assistant

Ask questions about your documents and get AI-powered answers

Chat Chat with AI about your indexed documents {/* Messages Area */}
{messages.length === 0 ? (

Start a conversation by asking a question!

I can help you find information in your documents.

) : ( <> {messages.map((message, index) => (
{message.role === "user" ? ( ) : ( )}

{message.content}

{message.timestamp.toLocaleTimeString()}

))} {loading && (
Thinking...
)}
)}
{/* Input Area */}
setInput(e.target.value)} placeholder="Ask a question about your documents..." disabled={loading} className="flex-1" />
) }