lovart3/app/(chat)/page.tsx
2025-12-25 23:08:33 +08:00

64 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { Suspense } from "react";
import { Chat } from "@/components/chat";
import { DataStreamHandler } from "@/components/data-stream-handler";
import { generateUUID } from "@/lib/utils";
import { auth } from "../(auth)/auth";
import { getDefaultAiModel, getEnabledAiModelsWithProvider } from "@/lib/db/queries";
export default function Page() {
return (
<Suspense fallback={<div className="flex h-dvh" />}>
<NewChatPage />
</Suspense>
);
}
async function NewChatPage() {
const id = generateUUID();
const [session, cookieStore] = await Promise.all([auth(), cookies()]);
// 如果用户未登录,重定向到登录页面
if (!session?.user) {
redirect("/login");
}
const modelIdFromCookie = cookieStore.get("chat-model");
// 如果没有保存的模型 ID,从数据库获取默认模型
let initialModelId = modelIdFromCookie?.value;
if (!initialModelId) {
// 尝试获取默认模型
const defaultModel = await getDefaultAiModel();
if (defaultModel) {
initialModelId = defaultModel.id;
} else {
// 如果没有设置默认模型,获取第一个启用的模型
const enabledModels = await getEnabledAiModelsWithProvider();
if (enabledModels.length > 0) {
initialModelId = enabledModels[0].model.id;
}
}
}
return (
<>
<Chat
autoResume={false}
id={id}
initialChatModel={initialModelId || ""}
initialMessages={[]}
initialVisibilityType="private"
isReadonly={false}
key={id}
userType={session?.user?.type}
/>
<DataStreamHandler />
</>
);
}