125 lines
3.7 KiB
TypeScript
125 lines
3.7 KiB
TypeScript
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 { LovartHome } from "@/components/home/lovart-home";
|
||
import { generateUUID } from "@/lib/utils";
|
||
import { auth } from "../(auth)/auth";
|
||
import {
|
||
getDefaultAiModel,
|
||
getEnabledAiModelsWithProvider,
|
||
getUserById,
|
||
getSiteSettings,
|
||
getSystemSetting,
|
||
} from "@/lib/db/queries";
|
||
import { isAiModelAgentAuto } from "@/lib/db/ai-model-flags";
|
||
|
||
export default function Page() {
|
||
return (
|
||
<Suspense fallback={<div className="flex h-dvh" />}>
|
||
<NewChatPage />
|
||
</Suspense>
|
||
);
|
||
}
|
||
|
||
async function NewChatPage() {
|
||
const id = generateUUID();
|
||
|
||
const [session, cookieStore, siteSettings, homeVariantSetting, surpriseCategoriesSetting] =
|
||
await Promise.all([
|
||
auth(),
|
||
cookies(),
|
||
getSiteSettings(),
|
||
getSystemSetting({ key: "home_variant" }).catch(() => undefined),
|
||
getSystemSetting({ key: "home_surprise_categories" }).catch(() => undefined),
|
||
]);
|
||
|
||
const homeVariant = homeVariantSetting?.value === "lovart" ? "lovart" : "classic";
|
||
const surpriseCategories = Array.isArray(surpriseCategoriesSetting?.value)
|
||
? surpriseCategoriesSetting?.value
|
||
.map((item: unknown) => String(item || "").trim())
|
||
.filter(Boolean)
|
||
: ["全部", "精选"];
|
||
|
||
if (!session?.user && homeVariant !== "lovart") {
|
||
redirect("/login");
|
||
}
|
||
|
||
if (homeVariant === "lovart") {
|
||
return (
|
||
<LovartHome
|
||
user={
|
||
session?.user
|
||
? {
|
||
id: session.user.id,
|
||
email: session.user.email,
|
||
name: session.user.name,
|
||
type: session.user.type,
|
||
}
|
||
: null
|
||
}
|
||
isAuthenticated={Boolean(session?.user)}
|
||
siteName={siteSettings.siteName || "AI Chatbot"}
|
||
siteLogo={siteSettings.siteLogo || siteSettings.siteLogoDark || ""}
|
||
surpriseCategories={surpriseCategories}
|
||
heroPromoText={siteSettings.lovartPromoText}
|
||
heroPromoActionText={siteSettings.lovartPromoActionText}
|
||
heroTitleText={siteSettings.lovartHeroTitle}
|
||
heroSubtitleText={siteSettings.lovartHeroSubtitle}
|
||
/>
|
||
);
|
||
}
|
||
|
||
const modelIdFromCookie = cookieStore.get("chat-model");
|
||
|
||
// 如果没有保存的模型 ID,从数据库获取默认模型
|
||
let initialModelId = modelIdFromCookie?.value;
|
||
|
||
if (!initialModelId) {
|
||
// 尝试获取默认模型
|
||
const defaultModel = await getDefaultAiModel();
|
||
|
||
const currentUser = session?.user?.id
|
||
? await getUserById({ id: session.user.id }).catch(() => undefined)
|
||
: undefined;
|
||
const allowHiddenModels =
|
||
session?.user?.type === "admin" || Boolean(currentUser?.isHighVip);
|
||
|
||
if (
|
||
defaultModel &&
|
||
!isAiModelAgentAuto(defaultModel) &&
|
||
(allowHiddenModels || !defaultModel.isHidden)
|
||
) {
|
||
initialModelId = defaultModel.id;
|
||
} else {
|
||
// 如果没有设置默认模型,获取第一个启用的模型
|
||
const enabledModels = await getEnabledAiModelsWithProvider(false, true);
|
||
const visibleModels = enabledModels.filter(
|
||
({ model }) =>
|
||
!isAiModelAgentAuto(model) &&
|
||
(allowHiddenModels || !model.isHidden)
|
||
);
|
||
if (visibleModels.length > 0) {
|
||
initialModelId = visibleModels[0].model.id;
|
||
}
|
||
}
|
||
}
|
||
|
||
return (
|
||
<>
|
||
<Chat
|
||
autoResume={false}
|
||
id={id}
|
||
initialChatModel={initialModelId || ""}
|
||
initialMessages={[]}
|
||
initialVisibilityType="private"
|
||
isReadonly={false}
|
||
key={id}
|
||
userType={session?.user?.type}
|
||
/>
|
||
<DataStreamHandler />
|
||
</>
|
||
);
|
||
}
|