"use client" import type { ModelMessage } from "ai" import { useState } from "react" export default function Page() { const [input, setInput] = useState("") const [messages, setMessages] = useState([]) const [isLoading, setIsLoading] = useState(false) const handleSendMessage = async () => { if (!input.trim() || isLoading) return const userMessage = { role: "user" as const, content: input } setMessages((currentMessages) => [...currentMessages, userMessage]) setInput("") setIsLoading(true) try { const response = await fetch("/api/chat", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ messages: [...messages, userMessage], }), }) const { messages: newMessages } = await response.json() setMessages((currentMessages) => [...currentMessages, ...newMessages]) } catch (error) { console.error("Error sending message:", error) } finally { setIsLoading(false) } } return (
{/* Header */}

Chat App

Chat with AI using Supermemory

{/* Messages Container */}
{messages.length === 0 && (
Start a conversation by typing a message below
)} {messages.map((message, index) => (
{message.role}
{typeof message.content === "string" ? message.content : message.content .filter((part) => part.type === "text") .map((part, partIndex) => (
{part.text}
))}
))} {isLoading && (
AI is thinking...
)}
{/* Input Container */}
setInput(event.target.value)} onKeyDown={(event) => { if (event.key === "Enter" && !event.shiftKey) { event.preventDefault() handleSendMessage() } }} placeholder="Type your message here..." disabled={isLoading} className="flex-1 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent bg-background text-foreground placeholder:text-muted-foreground disabled:opacity-50 disabled:cursor-not-allowed" />
) }