2025-09-21 11:02:31 -07:00
|
|
|
import { cookies } from "next/headers";
|
|
|
|
|
import { notFound, redirect } from "next/navigation";
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
import { auth } from "@/app/(auth)/auth";
|
|
|
|
|
import { Chat } from "@/components/chat";
|
|
|
|
|
import { DataStreamHandler } from "@/components/data-stream-handler";
|
|
|
|
|
import { DEFAULT_CHAT_MODEL } from "@/lib/ai/models";
|
|
|
|
|
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();
|
|
|
|
|
|
2025-04-25 23:40:15 -07:00
|
|
|
if (!session) {
|
2025-09-21 11:02:31 -07:00
|
|
|
redirect("/api/auth/guest");
|
2025-04-25 23:40:15 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (chat.visibility === "private") {
|
2025-04-25 23:40:15 -07:00
|
|
|
if (!session.user) {
|
2024-12-06 13:36:56 +03:00
|
|
|
return notFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-03 02:26:34 -07:00
|
|
|
const uiMessages = convertToUIMessages(messagesFromDb);
|
2025-03-16 18:42:29 -07:00
|
|
|
|
2024-10-24 16:35:51 -04:00
|
|
|
const cookieStore = await cookies();
|
2025-09-21 11:02:31 -07:00
|
|
|
const chatModelFromCookie = cookieStore.get("chat-model");
|
2025-02-03 20:33:15 +05:30
|
|
|
|
|
|
|
|
if (!chatModelFromCookie) {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Chat
|
2025-09-21 11:02:31 -07:00
|
|
|
autoResume={true}
|
2025-02-03 20:33:15 +05:30
|
|
|
id={chat.id}
|
2025-09-21 12:03:29 +01:00
|
|
|
initialChatModel={DEFAULT_CHAT_MODEL}
|
2025-09-21 11:02:31 -07:00
|
|
|
initialLastContext={chat.lastContext ?? undefined}
|
|
|
|
|
initialMessages={uiMessages}
|
2025-05-01 17:47:48 -07:00
|
|
|
initialVisibilityType={chat.visibility}
|
2025-02-03 20:33:15 +05:30
|
|
|
isReadonly={session?.user?.id !== chat.userId}
|
|
|
|
|
/>
|
2025-07-03 02:26:34 -07:00
|
|
|
<DataStreamHandler />
|
2025-02-03 20:33:15 +05:30
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-10-24 16:35:51 -04:00
|
|
|
|
|
|
|
|
return (
|
2024-12-20 23:07:23 +05:30
|
|
|
<>
|
|
|
|
|
<Chat
|
2025-09-21 11:02:31 -07:00
|
|
|
autoResume={true}
|
2024-12-20 23:07:23 +05:30
|
|
|
id={chat.id}
|
2025-09-21 12:03:29 +01:00
|
|
|
initialChatModel={chatModelFromCookie.value}
|
2025-09-21 11:02:31 -07:00
|
|
|
initialLastContext={chat.lastContext ?? undefined}
|
|
|
|
|
initialMessages={uiMessages}
|
2025-05-01 17:47:48 -07:00
|
|
|
initialVisibilityType={chat.visibility}
|
2024-12-20 23:07:23 +05:30
|
|
|
isReadonly={session?.user?.id !== chat.userId}
|
|
|
|
|
/>
|
2025-07-03 02:26:34 -07:00
|
|
|
<DataStreamHandler />
|
2024-12-20 23:07:23 +05:30
|
|
|
</>
|
2024-10-24 16:35:51 -04:00
|
|
|
);
|
2023-06-02 15:15:35 -04:00
|
|
|
}
|