2024-10-24 16:35:51 -04:00
|
|
|
'use client';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-10-24 16:35:51 -04:00
|
|
|
import { Attachment, Message } from 'ai';
|
|
|
|
|
import { useChat } from 'ai/react';
|
2024-10-30 16:01:24 +05:30
|
|
|
import { AnimatePresence } from 'framer-motion';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { useState } from 'react';
|
2024-11-01 15:31:54 +05:30
|
|
|
import { useWindowSize } from 'usehooks-ts';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-10-24 16:35:51 -04:00
|
|
|
import { ChatHeader } from '@/components/custom/chat-header';
|
|
|
|
|
import { Message as PreviewMessage } from '@/components/custom/message';
|
|
|
|
|
import { useScrollToBottom } from '@/components/custom/use-scroll-to-bottom';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
import { Canvas, UICanvas } from './canvas';
|
|
|
|
|
import { CanvasStreamHandler } from './canvas-stream-handler';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { MultimodalInput } from './multimodal-input';
|
|
|
|
|
import { Overview } from './overview';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
export function Chat({
|
|
|
|
|
id,
|
|
|
|
|
initialMessages,
|
2024-10-30 16:01:24 +05:30
|
|
|
selectedModelId,
|
2024-10-11 18:00:22 +05:30
|
|
|
}: {
|
|
|
|
|
id: string;
|
|
|
|
|
initialMessages: Array<Message>;
|
2024-10-30 16:01:24 +05:30
|
|
|
selectedModelId: string;
|
2024-10-11 18:00:22 +05:30
|
|
|
}) {
|
2024-10-30 16:01:24 +05:30
|
|
|
const {
|
|
|
|
|
messages,
|
|
|
|
|
setMessages,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
input,
|
|
|
|
|
setInput,
|
|
|
|
|
append,
|
|
|
|
|
isLoading,
|
|
|
|
|
stop,
|
|
|
|
|
data: streamingData,
|
|
|
|
|
} = useChat({
|
|
|
|
|
body: { id, modelId: selectedModelId },
|
|
|
|
|
initialMessages,
|
|
|
|
|
onFinish: () => {
|
|
|
|
|
window.history.replaceState({}, '', `/chat/${id}`);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-11-01 15:31:54 +05:30
|
|
|
const { width: windowWidth = 1920, height: windowHeight = 1080 } =
|
|
|
|
|
useWindowSize();
|
|
|
|
|
|
|
|
|
|
const [canvas, setCanvas] = useState<UICanvas>({
|
|
|
|
|
documentId: 'init',
|
|
|
|
|
content: '',
|
|
|
|
|
title: '',
|
|
|
|
|
status: 'idle',
|
|
|
|
|
isVisible: false,
|
|
|
|
|
boundingBox: {
|
|
|
|
|
top: windowHeight / 4,
|
|
|
|
|
left: windowWidth / 4,
|
|
|
|
|
width: 250,
|
|
|
|
|
height: 50,
|
|
|
|
|
},
|
|
|
|
|
});
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
const [messagesContainerRef, messagesEndRef] =
|
|
|
|
|
useScrollToBottom<HTMLDivElement>();
|
|
|
|
|
|
|
|
|
|
const [attachments, setAttachments] = useState<Array<Attachment>>([]);
|
|
|
|
|
|
|
|
|
|
return (
|
2024-10-30 16:01:24 +05:30
|
|
|
<>
|
|
|
|
|
<div className="flex flex-col min-w-0 h-dvh bg-background">
|
|
|
|
|
<ChatHeader selectedModelId={selectedModelId} />
|
|
|
|
|
<div
|
|
|
|
|
ref={messagesContainerRef}
|
2024-11-05 14:16:27 +03:00
|
|
|
className="flex flex-col min-w-0 gap-6 flex-1 overflow-y-scroll pt-4"
|
2024-10-30 16:01:24 +05:30
|
|
|
>
|
|
|
|
|
{messages.length === 0 && <Overview />}
|
2024-10-14 23:01:46 +05:30
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
{messages.map((message) => (
|
|
|
|
|
<PreviewMessage
|
|
|
|
|
key={message.id}
|
|
|
|
|
role={message.role}
|
|
|
|
|
content={message.content}
|
|
|
|
|
attachments={message.experimental_attachments}
|
|
|
|
|
toolInvocations={message.toolInvocations}
|
|
|
|
|
canvas={canvas}
|
|
|
|
|
setCanvas={setCanvas}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
<div
|
|
|
|
|
ref={messagesEndRef}
|
|
|
|
|
className="shrink-0 min-w-[24px] min-h-[24px]"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<form className="flex mx-auto px-4 bg-background pb-4 md:pb-6 gap-2 w-full md:max-w-3xl">
|
|
|
|
|
<MultimodalInput
|
|
|
|
|
input={input}
|
|
|
|
|
setInput={setInput}
|
|
|
|
|
handleSubmit={handleSubmit}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
stop={stop}
|
|
|
|
|
attachments={attachments}
|
|
|
|
|
setAttachments={setAttachments}
|
|
|
|
|
messages={messages}
|
|
|
|
|
setMessages={setMessages}
|
|
|
|
|
append={append}
|
|
|
|
|
/>
|
|
|
|
|
</form>
|
2024-10-11 18:00:22 +05:30
|
|
|
</div>
|
2024-10-30 16:01:24 +05:30
|
|
|
|
|
|
|
|
<AnimatePresence>
|
|
|
|
|
{canvas && canvas.isVisible && (
|
|
|
|
|
<Canvas
|
|
|
|
|
input={input}
|
|
|
|
|
setInput={setInput}
|
|
|
|
|
handleSubmit={handleSubmit}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
stop={stop}
|
|
|
|
|
attachments={attachments}
|
|
|
|
|
setAttachments={setAttachments}
|
|
|
|
|
append={append}
|
|
|
|
|
canvas={canvas}
|
|
|
|
|
setCanvas={setCanvas}
|
|
|
|
|
messages={messages}
|
|
|
|
|
setMessages={setMessages}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
|
|
|
|
|
|
|
|
|
<CanvasStreamHandler
|
|
|
|
|
streamingData={streamingData}
|
|
|
|
|
setCanvas={setCanvas}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|
|
|
|
|
}
|