2024-10-24 16:35:51 -04:00
|
|
|
'use client';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-11-15 12:18:17 -05:00
|
|
|
import type { Attachment, Message } from 'ai';
|
2025-03-04 18:00:14 -08:00
|
|
|
import { useChat } from '@ai-sdk/react';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { useState } from 'react';
|
2024-11-05 17:15:51 +03:00
|
|
|
import useSWR, { useSWRConfig } from 'swr';
|
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';
|
2025-01-28 18:51:04 +05:30
|
|
|
import { fetcher, generateUUID } from '@/lib/utils';
|
2025-02-13 08:25:57 -08:00
|
|
|
import { Artifact } from './artifact';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { MultimodalInput } from './multimodal-input';
|
2024-12-03 17:49:38 +03:00
|
|
|
import { Messages } from './messages';
|
2024-12-06 13:36:56 +03:00
|
|
|
import { VisibilityType } from './visibility-selector';
|
2025-02-13 08:25:57 -08:00
|
|
|
import { useArtifactSelector } from '@/hooks/use-artifact';
|
2025-02-03 20:33:15 +05:30
|
|
|
import { toast } from 'sonner';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
export function Chat({
|
|
|
|
|
id,
|
|
|
|
|
initialMessages,
|
2025-02-03 20:33:15 +05:30
|
|
|
selectedChatModel,
|
2024-12-06 13:36:56 +03:00
|
|
|
selectedVisibilityType,
|
|
|
|
|
isReadonly,
|
2024-10-11 18:00:22 +05:30
|
|
|
}: {
|
|
|
|
|
id: string;
|
|
|
|
|
initialMessages: Array<Message>;
|
2025-02-03 20:33:15 +05:30
|
|
|
selectedChatModel: string;
|
2024-12-06 13:36:56 +03:00
|
|
|
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,
|
2024-12-05 15:29:33 +03:00
|
|
|
reload,
|
2024-10-30 16:01:24 +05:30
|
|
|
} = useChat({
|
2024-12-05 15:29:33 +03:00
|
|
|
id,
|
2025-02-03 20:33:15 +05:30
|
|
|
body: { id, selectedChatModel: selectedChatModel },
|
2024-10-30 16:01:24 +05:30
|
|
|
initialMessages,
|
2024-12-19 17:05:04 +05:30
|
|
|
experimental_throttle: 100,
|
2025-01-28 18:51:04 +05:30
|
|
|
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
|
|
|
},
|
2025-03-04 18:00:14 -08:00
|
|
|
onError: () => {
|
2025-02-03 20:33:15 +05:30
|
|
|
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>>([]);
|
2025-02-13 08:25:57 -08:00
|
|
|
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">
|
2024-12-06 13:36:56 +03:00
|
|
|
<ChatHeader
|
|
|
|
|
chatId={id}
|
2025-02-03 20:33:15 +05:30
|
|
|
selectedModelId={selectedChatModel}
|
2024-12-06 13:36:56 +03:00
|
|
|
selectedVisibilityType={selectedVisibilityType}
|
|
|
|
|
isReadonly={isReadonly}
|
|
|
|
|
/>
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
<Messages
|
|
|
|
|
chatId={id}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
votes={votes}
|
|
|
|
|
messages={messages}
|
2024-12-05 15:29:33 +03:00
|
|
|
setMessages={setMessages}
|
|
|
|
|
reload={reload}
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly={isReadonly}
|
2025-02-13 08:25:57 -08:00
|
|
|
isArtifactVisible={isArtifactVisible}
|
2024-12-03 17:49:38 +03:00
|
|
|
/>
|
2024-11-06 15:21:28 +03:00
|
|
|
|
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">
|
2024-12-06 13:36:56 +03:00
|
|
|
{!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
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
<Artifact
|
2024-12-19 17:05:04 +05:30
|
|
|
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
|
|
|
);
|
|
|
|
|
}
|