2024-10-24 16:35:51 -04:00
|
|
|
'use client';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2025-09-08 21:02:06 +01:00
|
|
|
import { DefaultChatTransport, type LanguageModelUsage } from 'ai';
|
2025-03-04 18:00:14 -08:00
|
|
|
import { useChat } from '@ai-sdk/react';
|
2025-04-28 23:18:02 -07:00
|
|
|
import { useEffect, 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-09-07 00:04:51 +01:00
|
|
|
import { fetcher, fetchWithErrorHandlers, 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';
|
2025-04-03 00:28:36 -07:00
|
|
|
import type { VisibilityType } from './visibility-selector';
|
2025-02-13 08:25:57 -08:00
|
|
|
import { useArtifactSelector } from '@/hooks/use-artifact';
|
2025-04-03 00:28:36 -07:00
|
|
|
import { unstable_serialize } from 'swr/infinite';
|
|
|
|
|
import { getChatHistoryPaginationKey } from './sidebar-history';
|
2025-04-25 23:40:15 -07:00
|
|
|
import { toast } from './toast';
|
|
|
|
|
import type { Session } from 'next-auth';
|
2025-04-28 23:18:02 -07:00
|
|
|
import { useSearchParams } from 'next/navigation';
|
2025-05-01 17:47:48 -07:00
|
|
|
import { useChatVisibility } from '@/hooks/use-chat-visibility';
|
2025-05-07 16:02:53 -07:00
|
|
|
import { useAutoResume } from '@/hooks/use-auto-resume';
|
2025-05-13 19:01:28 -07:00
|
|
|
import { ChatSDKError } from '@/lib/errors';
|
2025-07-03 02:26:34 -07:00
|
|
|
import type { Attachment, ChatMessage } from '@/lib/types';
|
|
|
|
|
import { useDataStream } from './data-stream-provider';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
export function Chat({
|
|
|
|
|
id,
|
|
|
|
|
initialMessages,
|
2025-05-01 17:47:48 -07:00
|
|
|
initialChatModel,
|
|
|
|
|
initialVisibilityType,
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly,
|
2025-04-25 23:40:15 -07:00
|
|
|
session,
|
2025-05-01 12:36:52 -07:00
|
|
|
autoResume,
|
2024-10-11 18:00:22 +05:30
|
|
|
}: {
|
|
|
|
|
id: string;
|
2025-07-03 02:26:34 -07:00
|
|
|
initialMessages: ChatMessage[];
|
2025-05-01 17:47:48 -07:00
|
|
|
initialChatModel: string;
|
|
|
|
|
initialVisibilityType: VisibilityType;
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly: boolean;
|
2025-04-25 23:40:15 -07:00
|
|
|
session: Session;
|
2025-05-01 12:36:52 -07:00
|
|
|
autoResume: boolean;
|
2024-10-11 18:00:22 +05:30
|
|
|
}) {
|
2025-05-01 17:47:48 -07:00
|
|
|
const { visibilityType } = useChatVisibility({
|
|
|
|
|
chatId: id,
|
|
|
|
|
initialVisibilityType,
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
const { mutate } = useSWRConfig();
|
|
|
|
|
const { setDataStream } = useDataStream();
|
|
|
|
|
|
|
|
|
|
const [input, setInput] = useState<string>('');
|
2025-09-08 16:56:10 +02:00
|
|
|
const [usage, setUsage] = useState<LanguageModelUsage | undefined>(undefined);
|
2025-07-03 02:26:34 -07:00
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
const {
|
|
|
|
|
messages,
|
|
|
|
|
setMessages,
|
2025-07-03 02:26:34 -07:00
|
|
|
sendMessage,
|
2025-03-11 15:33:18 -07:00
|
|
|
status,
|
2024-10-30 16:01:24 +05:30
|
|
|
stop,
|
2025-07-03 02:26:34 -07:00
|
|
|
regenerate,
|
|
|
|
|
resumeStream,
|
|
|
|
|
} = useChat<ChatMessage>({
|
2024-12-05 15:29:33 +03:00
|
|
|
id,
|
2025-07-03 02:26:34 -07:00
|
|
|
messages: initialMessages,
|
2024-12-19 17:05:04 +05:30
|
|
|
experimental_throttle: 100,
|
2025-01-28 18:51:04 +05:30
|
|
|
generateId: generateUUID,
|
2025-07-03 02:26:34 -07:00
|
|
|
transport: new DefaultChatTransport({
|
|
|
|
|
api: '/api/chat',
|
|
|
|
|
fetch: fetchWithErrorHandlers,
|
|
|
|
|
prepareSendMessagesRequest({ messages, id, body }) {
|
|
|
|
|
return {
|
|
|
|
|
body: {
|
|
|
|
|
id,
|
|
|
|
|
message: messages.at(-1),
|
|
|
|
|
selectedChatModel: initialChatModel,
|
|
|
|
|
selectedVisibilityType: visibilityType,
|
|
|
|
|
...body,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
},
|
2025-04-26 01:09:01 -07:00
|
|
|
}),
|
2025-07-03 02:26:34 -07:00
|
|
|
onData: (dataPart) => {
|
|
|
|
|
setDataStream((ds) => (ds ? [...ds, dataPart] : []));
|
2025-09-08 16:56:10 +02:00
|
|
|
if (dataPart.type === 'data-usage') {
|
|
|
|
|
setUsage(dataPart.data);
|
|
|
|
|
}
|
2025-07-03 02:26:34 -07:00
|
|
|
},
|
2024-10-30 16:01:24 +05:30
|
|
|
onFinish: () => {
|
2025-04-03 00:28:36 -07:00
|
|
|
mutate(unstable_serialize(getChatHistoryPaginationKey));
|
2024-10-30 16:01:24 +05:30
|
|
|
},
|
2025-04-25 23:40:15 -07:00
|
|
|
onError: (error) => {
|
2025-05-13 19:01:28 -07:00
|
|
|
if (error instanceof ChatSDKError) {
|
|
|
|
|
toast({
|
|
|
|
|
type: 'error',
|
|
|
|
|
description: error.message,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-02-03 20:33:15 +05:30
|
|
|
},
|
2024-10-30 16:01:24 +05:30
|
|
|
});
|
|
|
|
|
|
2025-04-28 23:18:02 -07:00
|
|
|
const searchParams = useSearchParams();
|
|
|
|
|
const query = searchParams.get('query');
|
|
|
|
|
|
|
|
|
|
const [hasAppendedQuery, setHasAppendedQuery] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (query && !hasAppendedQuery) {
|
2025-07-03 02:26:34 -07:00
|
|
|
sendMessage({
|
|
|
|
|
role: 'user' as const,
|
|
|
|
|
parts: [{ type: 'text', text: query }],
|
2025-04-28 23:18:02 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setHasAppendedQuery(true);
|
|
|
|
|
window.history.replaceState({}, '', `/chat/${id}`);
|
|
|
|
|
}
|
2025-07-03 02:26:34 -07:00
|
|
|
}, [query, sendMessage, hasAppendedQuery, id]);
|
2025-04-28 23:18:02 -07:00
|
|
|
|
2024-11-05 17:15:51 +03:00
|
|
|
const { data: votes } = useSWR<Array<Vote>>(
|
2025-03-16 20:22:34 -07:00
|
|
|
messages.length >= 2 ? `/api/vote?chatId=${id}` : null,
|
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
|
|
|
|
2025-05-07 16:02:53 -07:00
|
|
|
useAutoResume({
|
|
|
|
|
autoResume,
|
|
|
|
|
initialMessages,
|
2025-07-03 02:26:34 -07:00
|
|
|
resumeStream,
|
2025-05-07 16:02:53 -07:00
|
|
|
setMessages,
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
return (
|
2024-10-30 16:01:24 +05:30
|
|
|
<>
|
2025-09-07 00:04:51 +01:00
|
|
|
<div className="flex flex-col min-w-0 h-dvh bg-background touch-pan-y overscroll-behavior-contain">
|
2024-12-06 13:36:56 +03:00
|
|
|
<ChatHeader
|
|
|
|
|
chatId={id}
|
2025-05-01 17:47:48 -07:00
|
|
|
selectedVisibilityType={initialVisibilityType}
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly={isReadonly}
|
2025-04-25 23:40:15 -07:00
|
|
|
session={session}
|
2024-12-06 13:36:56 +03:00
|
|
|
/>
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-12-03 17:49:38 +03:00
|
|
|
<Messages
|
|
|
|
|
chatId={id}
|
2025-03-11 15:33:18 -07:00
|
|
|
status={status}
|
2024-12-03 17:49:38 +03:00
|
|
|
votes={votes}
|
|
|
|
|
messages={messages}
|
2024-12-05 15:29:33 +03:00
|
|
|
setMessages={setMessages}
|
2025-07-03 02:26:34 -07:00
|
|
|
regenerate={regenerate}
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly={isReadonly}
|
2025-02-13 08:25:57 -08:00
|
|
|
isArtifactVisible={isArtifactVisible}
|
2025-09-07 00:04:51 +01:00
|
|
|
selectedModelId={initialChatModel}
|
2024-12-03 17:49:38 +03:00
|
|
|
/>
|
2024-11-06 15:21:28 +03:00
|
|
|
|
2025-09-07 00:04:51 +01:00
|
|
|
<div className="sticky bottom-0 flex gap-2 px-2 md:px-4 pb-3 md:pb-4 mx-auto w-full bg-background max-w-4xl z-[1] border-t-0">
|
2024-12-06 13:36:56 +03:00
|
|
|
{!isReadonly && (
|
|
|
|
|
<MultimodalInput
|
|
|
|
|
chatId={id}
|
|
|
|
|
input={input}
|
|
|
|
|
setInput={setInput}
|
2025-03-11 15:33:18 -07:00
|
|
|
status={status}
|
2024-12-06 13:36:56 +03:00
|
|
|
stop={stop}
|
|
|
|
|
attachments={attachments}
|
|
|
|
|
setAttachments={setAttachments}
|
|
|
|
|
messages={messages}
|
|
|
|
|
setMessages={setMessages}
|
2025-07-03 02:26:34 -07:00
|
|
|
sendMessage={sendMessage}
|
2025-05-01 17:47:48 -07:00
|
|
|
selectedVisibilityType={visibilityType}
|
2025-09-01 11:07:07 +01:00
|
|
|
selectedModelId={initialChatModel}
|
2025-09-08 16:56:10 +02:00
|
|
|
usage={usage}
|
2024-12-06 13:36:56 +03:00
|
|
|
/>
|
|
|
|
|
)}
|
2025-08-28 14:15:36 +01:00
|
|
|
</div>
|
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}
|
2025-03-11 15:33:18 -07:00
|
|
|
status={status}
|
2024-12-19 17:05:04 +05:30
|
|
|
stop={stop}
|
|
|
|
|
attachments={attachments}
|
|
|
|
|
setAttachments={setAttachments}
|
2025-07-03 02:26:34 -07:00
|
|
|
sendMessage={sendMessage}
|
2024-12-19 17:05:04 +05:30
|
|
|
messages={messages}
|
|
|
|
|
setMessages={setMessages}
|
2025-07-03 02:26:34 -07:00
|
|
|
regenerate={regenerate}
|
2024-12-19 17:05:04 +05:30
|
|
|
votes={votes}
|
|
|
|
|
isReadonly={isReadonly}
|
2025-05-01 17:47:48 -07:00
|
|
|
selectedVisibilityType={visibilityType}
|
2025-09-01 11:07:07 +01:00
|
|
|
selectedModelId={initialChatModel}
|
2024-12-19 17:05:04 +05:30
|
|
|
/>
|
2024-10-30 16:01:24 +05:30
|
|
|
</>
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|
|
|
|
|
}
|