57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { cookies } from "next/headers";
|
||
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()]);
|
||
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 />
|
||
</>
|
||
);
|
||
}
|