fix(chat): split image and video model lists by mode
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-03-02 22:10:41 +08:00
parent abadcdaa18
commit eec91f3c6f

View File

@ -52,6 +52,14 @@ const VIDEO_PROVIDER_TYPES = new Set([
"fal-video", "fal-video",
]); ]);
function isVideoProviderType(providerType?: string | null) {
return VIDEO_PROVIDER_TYPES.has(String(providerType || ""));
}
function isImageGenerationModel(model: DynamicModel) {
return model.supportsImageGen === true && !isVideoProviderType(model.providerType);
}
function isNanoBananaProModel(model: { displayName?: string | null; modelId?: string | null }) { function isNanoBananaProModel(model: { displayName?: string | null; modelId?: string | null }) {
const displayName = String(model.displayName || "").toLowerCase(); const displayName = String(model.displayName || "").toLowerCase();
const modelId = String(model.modelId || "").toLowerCase(); const modelId = String(model.modelId || "").toLowerCase();
@ -237,21 +245,20 @@ function PureMultimodalInput({
const models = modelCatalog?.models || []; const models = modelCatalog?.models || [];
const nanoBananaImage = models.find( const nanoBananaImage = models.find(
(model) => (model) =>
model.supportsImageGen && isImageGenerationModel(model) &&
!VIDEO_PROVIDER_TYPES.has(model.providerType || "") &&
isNanoBananaProModel(model) isNanoBananaProModel(model)
); );
const imageMain = models.find( const imageMain = models.find(
(model) => model.supportsImageGen && !VIDEO_PROVIDER_TYPES.has(model.providerType || "") && !model.isSubModel (model) => isImageGenerationModel(model) && !model.isSubModel
); );
const imageFallback = models.find( const imageFallback = models.find(
(model) => model.supportsImageGen && !VIDEO_PROVIDER_TYPES.has(model.providerType || "") (model) => isImageGenerationModel(model)
); );
const videoMain = models.find( const videoMain = models.find(
(model) => VIDEO_PROVIDER_TYPES.has(model.providerType || "") && !model.isSubModel (model) => isVideoProviderType(model.providerType) && !model.isSubModel
); );
const videoFallback = models.find((model) => const videoFallback = models.find((model) =>
VIDEO_PROVIDER_TYPES.has(model.providerType || "") isVideoProviderType(model.providerType)
); );
return { return {
image: nanoBananaImage || imageMain || imageFallback || null, image: nanoBananaImage || imageMain || imageFallback || null,
@ -260,14 +267,14 @@ function PureMultimodalInput({
}, [modelCatalog?.models]); }, [modelCatalog?.models]);
const activeQuickMode = useMemo<"image" | "video" | null>(() => { const activeQuickMode = useMemo<"image" | "video" | null>(() => {
if (isVideoModel || VIDEO_PROVIDER_TYPES.has(currentSelectedModel?.providerType || "")) { if (isVideoModel || isVideoProviderType(currentSelectedModel?.providerType)) {
return "video"; return "video";
} }
if (isImageModel || currentSelectedModel?.supportsImageGen) { if (isImageModel || (currentSelectedModel && isImageGenerationModel(currentSelectedModel))) {
return "image"; return "image";
} }
return null; return null;
}, [currentSelectedModel?.providerType, currentSelectedModel?.supportsImageGen, isImageModel, isVideoModel]); }, [currentSelectedModel, isImageModel, isVideoModel]);
const switchComposerMode = useCallback( const switchComposerMode = useCallback(
(targetMode: "image" | "video") => { (targetMode: "image" | "video") => {
@ -1255,6 +1262,7 @@ function PureMultimodalInput({
selectedModelId={selectedModelId} selectedModelId={selectedModelId}
attachments={attachments} attachments={attachments}
avatarOnly={true} avatarOnly={true}
modeFilter="video"
/> />
</> </>
) : ( ) : (
@ -1269,6 +1277,7 @@ function PureMultimodalInput({
onModelChange={onModelChange} onModelChange={onModelChange}
selectedModelId={selectedModelId} selectedModelId={selectedModelId}
attachments={attachments} attachments={attachments}
modeFilter="image"
/> />
) : null} ) : null}
{isImageModel && onImageParamsChange && ( {isImageModel && onImageParamsChange && (
@ -1417,12 +1426,14 @@ function PureModelSelectorCompact({
attachments, attachments,
avatarOnly = false, avatarOnly = false,
readOnly = false, readOnly = false,
modeFilter = "all",
}: { }: {
selectedModelId: string; selectedModelId: string;
onModelChange?: (modelId: string) => void; onModelChange?: (modelId: string) => void;
attachments: Attachment[]; attachments: Attachment[];
avatarOnly?: boolean; avatarOnly?: boolean;
readOnly?: boolean; readOnly?: boolean;
modeFilter?: "all" | "image" | "video";
}) { }) {
const [optimisticModelId, setOptimisticModelId] = useState(selectedModelId); const [optimisticModelId, setOptimisticModelId] = useState(selectedModelId);
@ -1496,9 +1507,20 @@ function PureModelSelectorCompact({
providerName: "System", providerName: "System",
}; };
const modeFilteredModels = useMemo(() => {
if (modeFilter === "video") {
return dynamicModels.filter((model) => isVideoProviderType(model.providerType));
}
if (modeFilter === "image") {
return dynamicModels.filter((model) => isImageGenerationModel(model));
}
return dynamicModels;
}, [dynamicModels, modeFilter]);
// 合并模型列表(根据设置决定是否包含自动模型) // 合并模型列表(根据设置决定是否包含自动模型)
const showAutoModel = siteSettings?.showAutoModel ?? true; const showAutoModel = siteSettings?.showAutoModel ?? true;
const displayModels = showAutoModel ? [autoModel, ...dynamicModels] : dynamicModels; const canShowAutoModel = showAutoModel && modeFilter === "all";
const displayModels = canShowAutoModel ? [autoModel, ...modeFilteredModels] : modeFilteredModels;
useEffect(() => { useEffect(() => {
setOptimisticModelId(selectedModelId); setOptimisticModelId(selectedModelId);
@ -1510,10 +1532,12 @@ function PureModelSelectorCompact({
const currentExists = displayModels.some((m) => m.id === selectedModelId); const currentExists = displayModels.some((m) => m.id === selectedModelId);
if (currentExists) return; if (currentExists) return;
const fallbackImageModel =
modeFilteredModels.find((model) => isNanoBananaProModel(model)) ||
modeFilteredModels.find((model) => model.isDefault) ||
modeFilteredModels[0];
const fallbackModelId = const fallbackModelId =
dynamicModels.find((m) => m.isDefault)?.id ?? fallbackImageModel?.id ?? (canShowAutoModel ? "auto" : "");
dynamicModels[0]?.id ??
(showAutoModel ? "auto" : "");
if (!fallbackModelId) return; if (!fallbackModelId) return;
@ -1522,7 +1546,7 @@ function PureModelSelectorCompact({
startTransition(() => { startTransition(() => {
saveChatModelAsCookie(fallbackModelId); saveChatModelAsCookie(fallbackModelId);
}); });
}, [displayModels, dynamicModels, onModelChange, selectedModelId, showAutoModel]); }, [canShowAutoModel, displayModels, modeFilteredModels, onModelChange, selectedModelId]);
// 自动选择模型的逻辑 - 必须在所有条件 return 之前定义 // 自动选择模型的逻辑 - 必须在所有条件 return 之前定义
const selectBestModel = useCallback(() => { const selectBestModel = useCallback(() => {
@ -1760,6 +1784,9 @@ const ModelSelectorCompact = memo(
if (prevProps.readOnly !== nextProps.readOnly) { if (prevProps.readOnly !== nextProps.readOnly) {
return false; return false;
} }
if (prevProps.modeFilter !== nextProps.modeFilter) {
return false;
}
if (!equal(prevProps.attachments, nextProps.attachments)) { if (!equal(prevProps.attachments, nextProps.attachments)) {
return false; return false;
} }