Fix external image API failover image validation

This commit is contained in:
Your Name 2026-06-09 18:12:22 +08:00
parent eb15c7a33b
commit 23d284b994
2 changed files with 29 additions and 16 deletions

View File

@ -8,9 +8,7 @@ import {
authenticateExternalImageApiRequest,
buildExternalImageApiModelCandidates,
buildExternalImageApiKeyFingerprint,
ensureHostedImageMarkdown,
ensureHostedReferenceImageUrls,
extractGeneratedImagesFromMarkdown,
generateImagesWithFailover,
getRequesterIp,
resolveExternalImageApiOwnerUserId,
@ -310,16 +308,8 @@ export async function POST(request: Request) {
referenceImageUrls: payload.referenceImages || [],
});
const actualModel = generationResult.usedModel;
const rawMarkdown = generationResult.markdown;
const hostedMarkdown = await ensureHostedImageMarkdown(
rawMarkdown,
payload.imageSize || "2K",
);
const images = extractGeneratedImagesFromMarkdown(hostedMarkdown);
if (!images.length) {
throw new Error("模型返回结果里未解析到图片");
}
const hostedMarkdown = generationResult.markdown;
const images = generationResult.images;
const persistResult = await saveGeneratedImages(
hostedMarkdown,

View File

@ -24,6 +24,13 @@ type ExternalImageApiGenerateAttempt = {
error?: string;
};
type ExternalImageApiGeneratedImage = {
id: number;
originalUrl: string;
thumbnailUrl: string;
imageSize: string;
};
function normalizeText(value: unknown) {
return String(value || "").trim();
}
@ -635,7 +642,7 @@ export async function resolveExternalImageApiOwnerUserId(ownerEmail?: string | n
return created.id;
}
export async function generateImagesWithModel(params: {
export async function generateImageWithModel(params: {
modelDbId: string;
prompt: string;
negativePrompt?: string | null;
@ -698,12 +705,17 @@ export async function generateImagesWithFailover(params: {
aspectRatio?: string | null;
imageSize?: string | null;
referenceImageUrls?: string[];
}) {
}): Promise<{
markdown: string;
images: ExternalImageApiGeneratedImage[];
usedModel: ImageModelEntry;
attempts: ExternalImageApiGenerateAttempt[];
}> {
const attempts: ExternalImageApiGenerateAttempt[] = [];
for (const candidate of params.candidates) {
try {
const markdown = await generateImagesWithModel({
const markdown = await generateImageWithModel({
modelDbId: candidate.model.id,
prompt: params.prompt,
negativePrompt: params.negativePrompt || null,
@ -713,6 +725,16 @@ export async function generateImagesWithFailover(params: {
referenceImageUrls: params.referenceImageUrls || [],
});
const hostedMarkdown = await ensureHostedImageMarkdown(
markdown,
params.imageSize || "2K",
);
const images = extractGeneratedImagesFromMarkdown(hostedMarkdown);
if (!images.length) {
throw new Error("模型返回结果里未解析到图片");
}
attempts.push({
modelId: candidate.model.id,
modelRuntimeId: candidate.model.modelId,
@ -721,7 +743,8 @@ export async function generateImagesWithFailover(params: {
});
return {
markdown,
markdown: hostedMarkdown,
images,
usedModel: candidate,
attempts,
};