93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
import "server-only";
|
|
|
|
import type { DiscountableImageGenConfigLike } from "@/lib/ai/image-model-pricing";
|
|
import {
|
|
getImageCreditCostFromConfig,
|
|
getImageModelCreditDiscountLabel,
|
|
} from "@/lib/ai/image-model-pricing";
|
|
import { isImageGenerationModelDescriptor } from "@/lib/ai/model-classification";
|
|
import { getEnabledAiModelsWithProvider } from "@/lib/db/queries";
|
|
|
|
const STYLE_PRO_SUPPORTED_PROVIDER_TYPES = new Set([
|
|
"gemini",
|
|
"grsai-gpt-image",
|
|
"alibaba-image",
|
|
"bfl-image",
|
|
"ruxa-image",
|
|
"fal-image",
|
|
"openai-image",
|
|
"stepfun-image",
|
|
"midjourney",
|
|
"runway",
|
|
"krea",
|
|
]);
|
|
|
|
export type StyleProModelEntry = Awaited<
|
|
ReturnType<typeof getEnabledAiModelsWithProvider>
|
|
>[number];
|
|
|
|
export type PublicStyleProModelOption = {
|
|
id: string;
|
|
modelId: string;
|
|
displayName: string;
|
|
avatarUrl: string | null;
|
|
providerName: string;
|
|
providerType: string;
|
|
isDefault: boolean;
|
|
unitCost: number;
|
|
creditDiscountLabel: string | null;
|
|
};
|
|
|
|
export function isStyleProImageModel(entry: StyleProModelEntry) {
|
|
const { model, provider } = entry;
|
|
if (!model.supportsImageGen) return false;
|
|
if (!STYLE_PRO_SUPPORTED_PROVIDER_TYPES.has(provider.type)) return false;
|
|
return isImageGenerationModelDescriptor({
|
|
providerType: provider.type,
|
|
modelId: model.modelId,
|
|
capabilities: model.capabilities,
|
|
supportsImageGen: model.supportsImageGen,
|
|
});
|
|
}
|
|
|
|
export async function getStyleProImageModelEntries(options?: {
|
|
includeHidden?: boolean;
|
|
}) {
|
|
const models = await getEnabledAiModelsWithProvider(
|
|
false,
|
|
options?.includeHidden === true
|
|
);
|
|
return models.filter(isStyleProImageModel);
|
|
}
|
|
|
|
export function findStyleProImageModelByRef(
|
|
entries: StyleProModelEntry[],
|
|
modelRef?: string | null
|
|
) {
|
|
const normalized = String(modelRef || "").trim();
|
|
if (!normalized) return null;
|
|
return (
|
|
entries.find(
|
|
({ model }) => model.id === normalized || model.modelId === normalized
|
|
) || null
|
|
);
|
|
}
|
|
|
|
export function toPublicStyleProModelOption(
|
|
entry: StyleProModelEntry
|
|
): PublicStyleProModelOption {
|
|
const imageGenConfig = (entry.model.imageGenConfig ||
|
|
null) as DiscountableImageGenConfigLike | null;
|
|
return {
|
|
id: entry.model.id,
|
|
modelId: entry.model.modelId,
|
|
displayName: entry.model.displayName,
|
|
avatarUrl: entry.model.avatarUrl || null,
|
|
providerName: entry.provider.name,
|
|
providerType: entry.provider.type,
|
|
isDefault: entry.model.isDefault,
|
|
unitCost: getImageCreditCostFromConfig(imageGenConfig),
|
|
creditDiscountLabel: getImageModelCreditDiscountLabel(imageGenConfig),
|
|
};
|
|
}
|