"use client"; import { useState, useRef, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { SendIcon } from "lucide-react"; import { useLocalStorage } from "react-use"; import { MODELS } from "@/lib/providers"; import { Settings } from "@/components/editor/ask-ai/settings"; interface Message { id: string; role: "user" | "assistant"; content: string; timestamp: Date; } export function ChatInterface() { const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); const [isLoading, setIsLoading] = useState(false); const [provider, setProvider] = useLocalStorage("provider", "auto"); const [model, setModel] = useLocalStorage("model", MODELS[0].value); const [openProvider, setOpenProvider] = useState(false); const scrollAreaRef = useRef(null); // Auto-scroll to bottom when messages change useEffect(() => { if (scrollAreaRef.current) { scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight; } }, [messages]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!input.trim() || isLoading) return; // Add user message const userMessage: Message = { id: Date.now().toString(), role: "user", content: input, timestamp: new Date(), }; setMessages((prev) => [...prev, userMessage]); setInput(""); setIsLoading(true); try { // Add assistant message placeholder const assistantMessageId = (Date.now() + 1).toString(); const assistantMessage: Message = { id: assistantMessageId, role: "assistant", content: "", timestamp: new Date(), }; setMessages((prev) => [...prev, assistantMessage]); // Call AI API const response = await fetch("/api/ask-ai", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ prompt: input, provider, model, }), }); if (!response.ok) { throw new Error("Failed to get response from AI"); } const reader = response.body?.getReader(); const decoder = new TextDecoder(); if (reader) { let assistantResponse = ""; while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value); assistantResponse += chunk; // Update the assistant message with the new content setMessages((prev) => prev.map((msg) => msg.id === assistantMessageId ? { ...msg, content: assistantResponse } : msg ) ); } } } catch (error) { console.error("Error:", error); setMessages((prev) => prev.map((msg) => msg.id === (Date.now() + 1).toString() ? { ...msg, content: "Sorry, I encountered an error." } : msg ) ); } finally { setIsLoading(false); } }; return (

Chat with AI

Ask anything and get AI-powered responses

{messages.length === 0 ? (
AI

How can I help you today?

Ask me anything about web development, design, or get help with your project.

) : (
{messages.map((message) => (
{message.role === "assistant" && ( AI )}

{message.content}

{message.role === "user" && ( U )}
))} {isLoading && (
AI
)}
)}