chatbot-template/components/chat.tsx

181 lines
4.7 KiB
TypeScript
Raw Normal View History

'use client';
2024-10-11 18:00:22 +05:30
import type { Attachment, UIMessage } from 'ai';
2025-03-04 18:00:14 -08:00
import { useChat } from '@ai-sdk/react';
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-05-13 19:01:28 -07:00
import { fetcher, fetchWithErrorHandlers, generateUUID } from '@/lib/utils';
import { Artifact } from './artifact';
import { MultimodalInput } from './multimodal-input';
import { Messages } from './messages';
2025-04-03 00:28:36 -07:00
import type { VisibilityType } from './visibility-selector';
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';
import { useSearchParams } from 'next/navigation';
import { useChatVisibility } from '@/hooks/use-chat-visibility';
import { useAutoResume } from '@/hooks/use-auto-resume';
2025-05-13 19:01:28 -07:00
import { ChatSDKError } from '@/lib/errors';
2024-10-11 18:00:22 +05:30
export function Chat({
id,
initialMessages,
initialChatModel,
initialVisibilityType,
isReadonly,
2025-04-25 23:40:15 -07:00
session,
autoResume,
2024-10-11 18:00:22 +05:30
}: {
id: string;
initialMessages: Array<UIMessage>;
initialChatModel: string;
initialVisibilityType: VisibilityType;
isReadonly: boolean;
2025-04-25 23:40:15 -07:00
session: Session;
autoResume: boolean;
2024-10-11 18:00:22 +05:30
}) {
2024-11-05 17:15:51 +03:00
const { mutate } = useSWRConfig();
const { visibilityType } = useChatVisibility({
chatId: id,
initialVisibilityType,
});
2024-10-30 16:01:24 +05:30
const {
messages,
setMessages,
handleSubmit,
input,
setInput,
append,
status,
2024-10-30 16:01:24 +05:30
stop,
reload,
experimental_resume,
data,
2024-10-30 16:01:24 +05:30
} = useChat({
id,
2024-10-30 16:01:24 +05:30
initialMessages,
experimental_throttle: 100,
sendExtraMessageFields: true,
generateId: generateUUID,
2025-05-13 19:01:28 -07:00
fetch: fetchWithErrorHandlers,
experimental_prepareRequestBody: (body) => ({
id,
message: body.messages.at(-1),
selectedChatModel: initialChatModel,
selectedVisibilityType: visibilityType,
}),
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,
});
}
},
2024-10-30 16:01:24 +05:30
});
const searchParams = useSearchParams();
const query = searchParams.get('query');
const [hasAppendedQuery, setHasAppendedQuery] = useState(false);
useEffect(() => {
if (query && !hasAppendedQuery) {
append({
role: 'user',
content: query,
});
setHasAppendedQuery(true);
window.history.replaceState({}, '', `/chat/${id}`);
}
}, [query, append, hasAppendedQuery, id]);
2024-11-05 17:15:51 +03:00
const { data: votes } = useSWR<Array<Vote>>(
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>>([]);
const isArtifactVisible = useArtifactSelector((state) => state.isVisible);
2024-10-11 18:00:22 +05:30
useAutoResume({
autoResume,
initialMessages,
experimental_resume,
data,
setMessages,
});
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={initialChatModel}
selectedVisibilityType={initialVisibilityType}
isReadonly={isReadonly}
2025-04-25 23:40:15 -07:00
session={session}
/>
2024-10-11 18:00:22 +05:30
<Messages
chatId={id}
status={status}
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}
status={status}
stop={stop}
attachments={attachments}
setAttachments={setAttachments}
messages={messages}
setMessages={setMessages}
append={append}
selectedVisibilityType={visibilityType}
/>
)}
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}
status={status}
stop={stop}
attachments={attachments}
setAttachments={setAttachments}
append={append}
messages={messages}
setMessages={setMessages}
reload={reload}
votes={votes}
isReadonly={isReadonly}
selectedVisibilityType={visibilityType}
/>
2024-10-30 16:01:24 +05:30
</>
2024-10-11 18:00:22 +05:30
);
}