chatbot-template/components/chat.tsx

133 lines
3.2 KiB
TypeScript
Raw Normal View History

'use client';
2024-10-11 18:00:22 +05:30
2024-11-15 12:18:17 -05:00
import type { Attachment, Message } from 'ai';
import { useChat } from 'ai/react';
2024-10-30 16:01:24 +05:30
import { AnimatePresence } from 'framer-motion';
import { useState } from 'react';
2024-11-05 17:15:51 +03:00
import useSWR, { useSWRConfig } from 'swr';
import { useWindowSize } from 'usehooks-ts';
2024-10-11 18:00:22 +05:30
2024-11-15 10:14:25 -05:00
import { ChatHeader } from '@/components/chat-header';
2024-11-15 12:18:17 -05:00
import type { Vote } from '@/lib/db/schema';
2024-11-05 17:15:51 +03:00
import { fetcher } from '@/lib/utils';
2024-10-11 18:00:22 +05:30
2024-11-15 12:18:17 -05:00
import { Block, type UIBlock } from './block';
2024-11-07 02:40:29 +03:00
import { BlockStreamHandler } from './block-stream-handler';
import { MultimodalInput } from './multimodal-input';
import { Messages } from './messages';
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-11-05 17:15:51 +03:00
const { mutate } = useSWRConfig();
2024-10-30 16:01:24 +05:30
const {
messages,
setMessages,
handleSubmit,
input,
setInput,
append,
isLoading,
stop,
reload,
2024-10-30 16:01:24 +05:30
data: streamingData,
} = useChat({
id,
2024-10-30 16:01:24 +05:30
body: { id, modelId: selectedModelId },
initialMessages,
onFinish: () => {
2024-11-05 17:15:51 +03:00
mutate('/api/history');
2024-10-30 16:01:24 +05:30
},
});
const { width: windowWidth = 1920, height: windowHeight = 1080 } =
useWindowSize();
2024-11-07 02:40:29 +03:00
const [block, setBlock] = useState<UIBlock>({
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
2024-11-05 17:15:51 +03:00
const { data: votes } = useSWR<Array<Vote>>(
`/api/vote?chatId=${id}`,
2024-11-15 13:00:15 -05:00
fetcher,
2024-11-05 17:15:51 +03:00
);
2024-10-11 18:00:22 +05:30
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} />
2024-10-11 18:00:22 +05:30
<Messages
chatId={id}
block={block}
setBlock={setBlock}
isLoading={isLoading}
votes={votes}
messages={messages}
setMessages={setMessages}
reload={reload}
/>
2024-10-30 16:01:24 +05:30
<form className="flex mx-auto px-4 bg-background pb-4 md:pb-6 gap-2 w-full md:max-w-3xl">
<MultimodalInput
2024-11-05 17:15:51 +03:00
chatId={id}
2024-10-30 16:01:24 +05:30
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>
2024-11-15 12:18:17 -05:00
{block?.isVisible && (
2024-11-07 02:40:29 +03:00
<Block
2024-11-05 17:15:51 +03:00
chatId={id}
2024-10-30 16:01:24 +05:30
input={input}
setInput={setInput}
handleSubmit={handleSubmit}
isLoading={isLoading}
stop={stop}
attachments={attachments}
setAttachments={setAttachments}
append={append}
2024-11-07 02:40:29 +03:00
block={block}
setBlock={setBlock}
2024-10-30 16:01:24 +05:30
messages={messages}
setMessages={setMessages}
reload={reload}
2024-11-05 17:15:51 +03:00
votes={votes}
2024-10-30 16:01:24 +05:30
/>
)}
</AnimatePresence>
2024-11-07 02:40:29 +03:00
<BlockStreamHandler streamingData={streamingData} setBlock={setBlock} />
2024-10-30 16:01:24 +05:30
</>
2024-10-11 18:00:22 +05:30
);
}