'use client'; import { motion, AnimatePresence } from 'framer-motion'; import { useState, useRef, useEffect } from 'react'; import { Send, Bot, User, FileCode, Sparkles } from 'lucide-react'; interface Message { id: string; role: 'user' | 'assistant'; content: string; sources?: string[]; } export default function ChatPage() { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [isLoading, setIsLoading] = 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() || isLoading) return; const userMessage: Message = { id: Date.now().toString(), role: 'user', content: input, }; setMessages((prev) => [...prev, userMessage]); setInput(''); setIsLoading(true); try { const response = await fetch('http://localhost:8000/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: input }), }); const data = await response.json(); const assistantMessage: Message = { id: (Date.now() + 1).toString(), role: 'assistant', content: data.answer || data.response || 'No response received', sources: data.sources || [], }; setMessages((prev) => [...prev, assistantMessage]); } catch (error) { setMessages((prev) => [ ...prev, { id: (Date.now() + 1).toString(), role: 'assistant', content: 'Error connecting to backend. Please ensure the API is running.', }, ]); } finally { setIsLoading(false); } }; const suggestions = [ 'Explain how authentication works', 'Find all database queries', 'What are the main entry points?', 'Show me the API endpoints', ]; return (
{/* Header */}
💬

Chat Mode

Ask questions about your codebase

{/* Messages */}
{messages.length === 0 ? (

Ask anything about your code

I can explain functions, find patterns, suggest improvements, and more.

{suggestions.map((suggestion, i) => ( setInput(suggestion)} className="px-4 py-2 bg-slate-900/60 backdrop-blur-xl border border-white/10 rounded-xl rounded-full text-sm text-slate-300 hover:border-sky-500/50 transition-colors" > {suggestion} ))}
) : ( {messages.map((message, i) => (
{message.role === 'user' ? : }
{/* Sources */} {message.sources && message.sources.length > 0 && (
{message.sources.map((source, i) => ( {source} ))}
)}

{message.content}

))}
)} {isLoading && (
)}
{/* Input */}
setInput(e.target.value)} placeholder="Ask about your codebase..." className="flex-1 bg-transparent border-none outline-none text-slate-200 placeholder:text-slate-500" disabled={isLoading} />
); }