'use client' import { useChat, type Message } from 'ai/react' import { cn } from '@/lib/utils' import { ChatList } from '@/components/chat-list' import { ChatPanel } from '@/components/chat-panel' import { EmptyScreen } from '@/components/empty-screen' import { ChatScrollAnchor } from '@/components/chat-scroll-anchor' import { useLocalStorage } from '@/lib/hooks/use-local-storage' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { useState } from 'react' import { Button } from './ui/button' import { Input } from './ui/input' import { toast } from 'react-hot-toast' import { usePathname, useRouter } from 'next/navigation' const IS_PREVIEW = process.env.VERCEL_ENV === 'preview' export interface ChatProps extends React.ComponentProps<'div'> { initialMessages?: Message[] id?: string } export function Chat({ id, initialMessages, className }: ChatProps) { const router = useRouter() const path = usePathname() const [previewToken, setPreviewToken] = useLocalStorage( 'ai-token', null ) const [previewTokenDialog, setPreviewTokenDialog] = useState(IS_PREVIEW) const [previewTokenInput, setPreviewTokenInput] = useState(previewToken ?? '') const { messages, append, reload, stop, isLoading, input, setInput } = useChat({ initialMessages, id, body: { id, previewToken }, onResponse(response) { if (response.status === 401) { toast.error(response.statusText) } }, onFinish() { if (!path.includes('chat')) { router.push(`/chat/${id}`, { shallow: true, scroll: false }) router.refresh() } } }) return ( <>
{messages.length ? ( <> ) : ( )}
Enter your OpenAI Key If you have not obtained your OpenAI API key, you can do so by{' '} signing up {' '} on the OpenAI website. This is only necessary for preview environments so that the open source community can test the app. The token will be saved to your browser's local storage under the name ai-token. setPreviewTokenInput(e.target.value)} /> ) }