fix(ai): validate models server-side and fix reasoning detection (#1443)

This commit is contained in:
dancer 2026-03-03 15:56:05 +00:00 committed by GitHub
parent 49ba1d5f14
commit 99cd8699c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 9 deletions

View file

@ -12,6 +12,7 @@ import { createResumableStreamContext } from "resumable-stream";
import { auth, type UserType } from "@/app/(auth)/auth";
import { entitlementsByUserType } from "@/lib/ai/entitlements";
import { type RequestHints, systemPrompt } from "@/lib/ai/prompts";
import { allowedModelIds } from "@/lib/ai/models";
import { getLanguageModel } from "@/lib/ai/providers";
import { createDocument } from "@/lib/ai/tools/create-document";
import { getWeather } from "@/lib/ai/tools/get-weather";
@ -69,6 +70,10 @@ export async function POST(request: Request) {
return new ChatbotError("unauthorized:chat").toResponse();
}
if (!allowedModelIds.has(selectedChatModel)) {
return new ChatbotError("bad_request:api").toResponse();
}
await checkIpRateLimit(ipAddress(request));
const userType: UserType = session.user.type;
@ -134,8 +139,9 @@ export async function POST(request: Request) {
}
const isReasoningModel =
selectedChatModel.includes("reasoning") ||
selectedChatModel.includes("thinking");
selectedChatModel.endsWith("-thinking") ||
(selectedChatModel.includes("reasoning") &&
!selectedChatModel.includes("non-reasoning"));
const modelMessages = await convertToModelMessages(uiMessages);
@ -174,7 +180,9 @@ export async function POST(request: Request) {
},
});
dataStream.merge(result.toUIMessageStream({ sendReasoning: true }));
dataStream.merge(
result.toUIMessageStream({ sendReasoning: isReasoningModel }),
);
if (titlePromise) {
const title = await titlePromise;