2024-10-24 16:35:51 -04:00
|
|
|
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';
|
2024-10-24 16:35:51 -04:00
|
|
|
import { auth } from '@/app/(auth)/auth';
|
|
|
|
|
import { Chat as PreviewChat } from '@/components/custom/chat';
|
|
|
|
|
import { getChatById } from '@/db/queries';
|
|
|
|
|
import { Chat } from '@/db/schema';
|
|
|
|
|
import { convertToUIMessages } from '@/lib/utils';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
2024-10-23 12:21:30 -04:00
|
|
|
export default async function Page(props: { params: Promise<any> }) {
|
|
|
|
|
const params = await props.params;
|
2024-10-11 18:00:22 +05:30
|
|
|
const { id } = params;
|
|
|
|
|
const chatFromDb = await getChatById({ id });
|
|
|
|
|
|
|
|
|
|
if (!chatFromDb) {
|
|
|
|
|
notFound();
|
2023-06-16 21:52:01 +04:00
|
|
|
}
|
|
|
|
|
|
2024-10-11 18:00:22 +05:30
|
|
|
// type casting
|
|
|
|
|
const chat: Chat = {
|
|
|
|
|
...chatFromDb,
|
|
|
|
|
messages: convertToUIMessages(chatFromDb.messages as Array<CoreMessage>),
|
|
|
|
|
};
|
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-10-24 16:35:51 -04:00
|
|
|
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;
|
2024-10-24 16:35:51 -04:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<PreviewChat
|
|
|
|
|
id={chat.id}
|
|
|
|
|
initialMessages={chat.messages}
|
2024-10-30 16:01:24 +05:30
|
|
|
selectedModelId={selectedModelId}
|
2024-10-24 16:35:51 -04:00
|
|
|
/>
|
|
|
|
|
);
|
2023-06-02 15:15:35 -04:00
|
|
|
}
|