diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index 5c3f8a432..877729843 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -1922,7 +1922,12 @@ export async function POST(request: Request) { // 简单回退:有图选图片模型,无图选默认模型 let bestModel = null; if (hasImage) { - bestModel = allModels.find(m => m.model.supportsImageGen && !m.model.isSubModel); + bestModel = allModels.find( + (m) => + m.model.supportsImageGen && + !m.model.isSubModel && + !isVideoProviderType(m.provider?.type), + ); if (isAutoTimeout) { smartAutoFriendlyMessage = buildAutoModeFallbackMessage({ text: autoTextForFallback, @@ -1948,10 +1953,14 @@ export async function POST(request: Request) { }); } if (!bestModel) { - bestModel = allModels.find(m => m.model.isDefault && !m.model.isSubModel); + bestModel = allModels.find( + (m) => m.model.isDefault && !m.model.isSubModel && !isVideoProviderType(m.provider?.type), + ); } if (!bestModel && allModels.length > 0) { - bestModel = allModels.find(m => !m.model.isSubModel) || allModels[0]; + bestModel = allModels.find( + (m) => !m.model.isSubModel && !isVideoProviderType(m.provider?.type), + ) || allModels.find((m) => !m.model.isSubModel) || allModels[0]; } if (shouldKeepSelectedModel) { @@ -1967,6 +1976,50 @@ export async function POST(request: Request) { } // ========== End Smart Auto Mode Logic ========== + if (shouldRunAgentConversation && !allowAgentVideoGeneration) { + const finalModelIsUuid = + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(finalModelId); + let finalProviderType: string | undefined; + + if (finalModelIsUuid) { + const finalModelConfig = await getAiModelById({ id: finalModelId }).catch(() => null); + if (finalModelConfig) { + finalProviderType = ( + await getAiProviderById({ id: finalModelConfig.providerId }).catch(() => null) + )?.type as string | undefined; + } + } + + if (isVideoProviderType(finalProviderType)) { + const allModels = await getEnabledAiModelsWithProvider(false); + const fallbackModel = + allModels.find( + (m) => + !m.model.isSubModel && + m.model.supportsImageGen && + !isVideoProviderType(m.provider?.type), + ) || + allModels.find( + (m) => + !m.model.isSubModel && + !isVideoProviderType(m.provider?.type), + ); + + if (fallbackModel) { + const hasImageInput = message.parts.some((part: any) => + part?.type === "file" || part?.type === "image" || part?.type === "image_url" + ); + smartAutoDetectedIntent = hasImageInput ? "image_edit" : "image_gen"; + finalModelId = fallbackModel.model.id; + smartAutoVideoPlan = undefined; + smartAutoRecommendedVideoParams = undefined; + console.log( + `[Smart Auto Mode] 🚫 Safety override: replace video model (${finalProviderType}) with ${fallbackModel.model.displayName}`, + ); + } + } + } + if ( shouldRunAgentConversation && !smartAutoNeedsClarification &&