chatbot-template/app/(chat)/chat/[id]/page.tsx

48 lines
1.2 KiB
TypeScript
Raw Normal View History

import { CoreMessage } from 'ai';
import { cookies } from 'next/headers';
import { notFound } from 'next/navigation';
2023-05-19 12:33:56 -04:00
2024-10-30 16:01:24 +05:30
import { DEFAULT_MODEL_NAME, models } from '@/ai/models';
import { auth } from '@/app/(auth)/auth';
2024-11-15 10:14:25 -05:00
import { Chat as PreviewChat } from '@/components/chat';
2024-11-15 10:13:21 -05:00
import { getChatById, getMessagesByChatId } from '@/lib/db/queries';
import { convertToUIMessages } from '@/lib/utils';
2024-10-11 18:00:22 +05:30
2024-11-15 12:18:17 -05:00
export default async function Page(props: { params: Promise<{ id: string }> }) {
2024-10-23 12:21:30 -04:00
const params = await props.params;
2024-10-11 18:00:22 +05:30
const { id } = params;
2024-11-05 17:15:51 +03:00
const chat = await getChatById({ id });
2024-10-11 18:00:22 +05:30
2024-11-05 17:15:51 +03:00
if (!chat) {
2024-10-11 18:00:22 +05:30
notFound();
2023-06-16 21:52:01 +04:00
}
2024-10-11 18:00:22 +05:30
const session = await auth();
if (!session || !session.user) {
return notFound();
}
2023-05-19 12:33:56 -04:00
2024-10-11 18:00:22 +05:30
if (session.user.id !== chat.userId) {
return notFound();
2023-06-16 15:28:43 -04:00
}
2024-10-11 18:00:22 +05:30
2024-11-05 17:15:51 +03:00
const messagesFromDb = await getMessagesByChatId({
id,
});
const cookieStore = await cookies();
2024-10-30 16:01:24 +05:30
const modelIdFromCookie = cookieStore.get('model-id')?.value;
const selectedModelId =
models.find((model) => model.id === modelIdFromCookie)?.id ||
DEFAULT_MODEL_NAME;
return (
<PreviewChat
id={chat.id}
2024-11-05 17:15:51 +03:00
initialMessages={convertToUIMessages(messagesFromDb)}
2024-10-30 16:01:24 +05:30
selectedModelId={selectedModelId}
/>
);
2023-06-02 15:15:35 -04:00
}