chatbot-template/components/chat.tsx

126 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';
import { useState } from 'react';
2024-11-05 17:15:51 +03:00
import useSWR, { useSWRConfig } from 'swr';
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';
import { fetcher, generateUUID } from '@/lib/utils';
2024-10-11 18:00:22 +05:30
import { Artifact } from './artifact';
import { MultimodalInput } from './multimodal-input';
import { Messages } from './messages';
import { VisibilityType } from './visibility-selector';
import { useArtifactSelector } from '@/hooks/use-artifact';
import { toast } from 'sonner';
2024-10-11 18:00:22 +05:30
export function Chat({
id,
initialMessages,
selectedChatModel,
selectedVisibilityType,
isReadonly,
2024-10-11 18:00:22 +05:30
}: {
id: string;
initialMessages: Array<Message>;
selectedChatModel: string;
selectedVisibilityType: VisibilityType;
isReadonly: boolean;
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
} = useChat({
id,
body: { id, selectedChatModel: selectedChatModel },
2024-10-30 16:01:24 +05:30
initialMessages,
experimental_throttle: 100,
sendExtraMessageFields: true,
generateId: generateUUID,
2024-10-30 16:01:24 +05:30
onFinish: () => {
2024-11-05 17:15:51 +03:00
mutate('/api/history');
2024-10-30 16:01:24 +05:30
},
onError: (error) => {
toast.error('An error occured, please try again!');
},
2024-10-30 16:01:24 +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>>([]);
const isArtifactVisible = useArtifactSelector((state) => state.isVisible);
2024-10-11 18:00:22 +05:30
return (
2024-10-30 16:01:24 +05:30
<>
<div className="flex flex-col min-w-0 h-dvh bg-background">
<ChatHeader
chatId={id}
selectedModelId={selectedChatModel}
selectedVisibilityType={selectedVisibilityType}
isReadonly={isReadonly}
/>
2024-10-11 18:00:22 +05:30
<Messages
chatId={id}
isLoading={isLoading}
votes={votes}
messages={messages}
setMessages={setMessages}
reload={reload}
isReadonly={isReadonly}
isArtifactVisible={isArtifactVisible}
/>
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">
{!isReadonly && (
<MultimodalInput
chatId={id}
input={input}
setInput={setInput}
handleSubmit={handleSubmit}
isLoading={isLoading}
stop={stop}
attachments={attachments}
setAttachments={setAttachments}
messages={messages}
setMessages={setMessages}
append={append}
/>
)}
2024-10-30 16:01:24 +05:30
</form>
2024-10-11 18:00:22 +05:30
</div>
2024-10-30 16:01:24 +05:30
<Artifact
chatId={id}
input={input}
setInput={setInput}
handleSubmit={handleSubmit}
isLoading={isLoading}
stop={stop}
attachments={attachments}
setAttachments={setAttachments}
append={append}
messages={messages}
setMessages={setMessages}
reload={reload}
votes={votes}
isReadonly={isReadonly}
/>
2024-10-30 16:01:24 +05:30
</>
2024-10-11 18:00:22 +05:30
);
}