2023-06-11 00:23:23 +04:00
|
|
|
'use client'
|
|
|
|
|
|
2023-06-16 22:43:24 +02:00
|
|
|
import { useChat, type Message } from 'ai/react'
|
2023-06-11 15:39:04 +04:00
|
|
|
|
2023-06-13 22:16:54 +04:00
|
|
|
import { cn } from '@/lib/utils'
|
2023-06-11 00:23:23 +04:00
|
|
|
import { ChatList } from '@/components/chat-list'
|
|
|
|
|
import { ChatPanel } from '@/components/chat-panel'
|
2023-06-13 22:16:54 +04:00
|
|
|
import { EmptyScreen } from '@/components/empty-screen'
|
2023-06-15 15:27:41 +04:00
|
|
|
import { ChatScrollAnchor } from '@/components/chat-scroll-anchor'
|
2023-06-11 00:23:23 +04:00
|
|
|
|
2023-06-13 22:16:54 +04:00
|
|
|
export interface ChatProps extends React.ComponentProps<'div'> {
|
2023-06-11 00:23:23 +04:00
|
|
|
initialMessages?: Message[]
|
|
|
|
|
id?: string
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 21:52:01 +04:00
|
|
|
export function Chat({ id, initialMessages, className }: ChatProps) {
|
2023-06-13 22:16:54 +04:00
|
|
|
const { messages, append, reload, stop, isLoading, input, setInput } =
|
|
|
|
|
useChat({
|
|
|
|
|
initialMessages,
|
2023-06-15 11:31:03 +04:00
|
|
|
id,
|
|
|
|
|
body: {
|
|
|
|
|
id
|
|
|
|
|
}
|
2023-06-13 22:16:54 +04:00
|
|
|
})
|
2023-06-11 00:23:23 +04:00
|
|
|
|
|
|
|
|
return (
|
2023-06-13 22:16:54 +04:00
|
|
|
<>
|
|
|
|
|
<div className={cn('pb-[200px] pt-4 md:pt-10', className)}>
|
|
|
|
|
{messages.length ? (
|
2023-06-15 15:27:41 +04:00
|
|
|
<>
|
|
|
|
|
<ChatList messages={messages} />
|
|
|
|
|
<ChatScrollAnchor trackVisibility={isLoading} />
|
|
|
|
|
</>
|
2023-06-13 22:16:54 +04:00
|
|
|
) : (
|
|
|
|
|
<EmptyScreen setInput={setInput} />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2023-06-11 15:08:59 +04:00
|
|
|
<ChatPanel
|
|
|
|
|
id={id}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
stop={stop}
|
|
|
|
|
append={append}
|
|
|
|
|
reload={reload}
|
|
|
|
|
messages={messages}
|
2023-06-13 22:16:54 +04:00
|
|
|
input={input}
|
|
|
|
|
setInput={setInput}
|
2023-06-11 15:08:59 +04:00
|
|
|
/>
|
2023-06-13 22:16:54 +04:00
|
|
|
</>
|
2023-06-11 00:23:23 +04:00
|
|
|
)
|
|
|
|
|
}
|