fix(ai): validate models server-side and fix reasoning detection (#1443)
This commit is contained in:
parent
49ba1d5f14
commit
99cd8699c1
4 changed files with 21 additions and 9 deletions
|
|
@ -12,6 +12,7 @@ import { createResumableStreamContext } from "resumable-stream";
|
||||||
import { auth, type UserType } from "@/app/(auth)/auth";
|
import { auth, type UserType } from "@/app/(auth)/auth";
|
||||||
import { entitlementsByUserType } from "@/lib/ai/entitlements";
|
import { entitlementsByUserType } from "@/lib/ai/entitlements";
|
||||||
import { type RequestHints, systemPrompt } from "@/lib/ai/prompts";
|
import { type RequestHints, systemPrompt } from "@/lib/ai/prompts";
|
||||||
|
import { allowedModelIds } from "@/lib/ai/models";
|
||||||
import { getLanguageModel } from "@/lib/ai/providers";
|
import { getLanguageModel } from "@/lib/ai/providers";
|
||||||
import { createDocument } from "@/lib/ai/tools/create-document";
|
import { createDocument } from "@/lib/ai/tools/create-document";
|
||||||
import { getWeather } from "@/lib/ai/tools/get-weather";
|
import { getWeather } from "@/lib/ai/tools/get-weather";
|
||||||
|
|
@ -69,6 +70,10 @@ export async function POST(request: Request) {
|
||||||
return new ChatbotError("unauthorized:chat").toResponse();
|
return new ChatbotError("unauthorized:chat").toResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!allowedModelIds.has(selectedChatModel)) {
|
||||||
|
return new ChatbotError("bad_request:api").toResponse();
|
||||||
|
}
|
||||||
|
|
||||||
await checkIpRateLimit(ipAddress(request));
|
await checkIpRateLimit(ipAddress(request));
|
||||||
|
|
||||||
const userType: UserType = session.user.type;
|
const userType: UserType = session.user.type;
|
||||||
|
|
@ -134,8 +139,9 @@ export async function POST(request: Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const isReasoningModel =
|
const isReasoningModel =
|
||||||
selectedChatModel.includes("reasoning") ||
|
selectedChatModel.endsWith("-thinking") ||
|
||||||
selectedChatModel.includes("thinking");
|
(selectedChatModel.includes("reasoning") &&
|
||||||
|
!selectedChatModel.includes("non-reasoning"));
|
||||||
|
|
||||||
const modelMessages = await convertToModelMessages(uiMessages);
|
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) {
|
if (titlePromise) {
|
||||||
const title = await titlePromise;
|
const title = await titlePromise;
|
||||||
|
|
|
||||||
|
|
@ -110,13 +110,14 @@ const PurePreviewMessage = ({
|
||||||
|
|
||||||
if (type === "reasoning") {
|
if (type === "reasoning") {
|
||||||
const hasContent = part.text?.trim().length > 0;
|
const hasContent = part.text?.trim().length > 0;
|
||||||
const isStreaming = "state" in part && part.state === "streaming";
|
if (hasContent) {
|
||||||
if (hasContent || isStreaming) {
|
const isStreaming =
|
||||||
|
"state" in part && part.state === "streaming";
|
||||||
return (
|
return (
|
||||||
<MessageReasoning
|
<MessageReasoning
|
||||||
isLoading={isLoading || isStreaming}
|
isLoading={isLoading || isStreaming}
|
||||||
key={key}
|
key={key}
|
||||||
reasoning={part.text || ""}
|
reasoning={part.text}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,8 @@ export const chatModels: ChatModel[] = [
|
||||||
description: "Fast and cost-effective for simple tasks",
|
description: "Fast and cost-effective for simple tasks",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "openai/gpt-5.2",
|
id: "openai/gpt-5-mini",
|
||||||
name: "GPT-5.2",
|
name: "GPT-5 Mini",
|
||||||
provider: "openai",
|
provider: "openai",
|
||||||
description: "Most capable OpenAI model",
|
description: "Most capable OpenAI model",
|
||||||
},
|
},
|
||||||
|
|
@ -65,6 +65,8 @@ export const chatModels: ChatModel[] = [
|
||||||
];
|
];
|
||||||
|
|
||||||
// Group models by provider for UI
|
// Group models by provider for UI
|
||||||
|
export const allowedModelIds = new Set(chatModels.map((m) => m.id));
|
||||||
|
|
||||||
export const modelsByProvider = chatModels.reduce(
|
export const modelsByProvider = chatModels.reduce(
|
||||||
(acc, model) => {
|
(acc, model) => {
|
||||||
if (!acc[model.provider]) {
|
if (!acc[model.provider]) {
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,8 @@ export function getLanguageModel(modelId: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const isReasoningModel =
|
const isReasoningModel =
|
||||||
modelId.includes("reasoning") || modelId.endsWith("-thinking");
|
modelId.endsWith("-thinking") ||
|
||||||
|
(modelId.includes("reasoning") && !modelId.includes("non-reasoning"));
|
||||||
|
|
||||||
if (isReasoningModel) {
|
if (isReasoningModel) {
|
||||||
const gatewayModelId = modelId.replace(THINKING_SUFFIX_REGEX, "");
|
const gatewayModelId = modelId.replace(THINKING_SUFFIX_REGEX, "");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue