|
|
|
@ -61,6 +61,15 @@ type PopiTaskResult = { |
|
|
|
cover?: string | null; |
|
|
|
coverThumb?: string | null; |
|
|
|
originImages?: string[] | null; |
|
|
|
width?: number | string | null; |
|
|
|
height?: number | string | null; |
|
|
|
widths?: Array<number | string | null> | null; |
|
|
|
heights?: Array<number | string | null> | null; |
|
|
|
dimensions?: unknown; |
|
|
|
imageWidth?: number | string | null; |
|
|
|
imageHeight?: number | string | null; |
|
|
|
imageWidths?: Array<number | string | null> | null; |
|
|
|
imageHeights?: Array<number | string | null> | null; |
|
|
|
}; |
|
|
|
|
|
|
|
type PopiTask = { |
|
|
|
@ -123,6 +132,48 @@ function collectStrings(values: unknown[]): string[] { |
|
|
|
return items; |
|
|
|
} |
|
|
|
|
|
|
|
function readPositiveNumber(value: unknown): number | null { |
|
|
|
const numberValue = typeof value === "number" |
|
|
|
? value |
|
|
|
: typeof value === "string" && value.trim().length > 0 |
|
|
|
? Number(value) |
|
|
|
: Number.NaN; |
|
|
|
return Number.isFinite(numberValue) && numberValue > 0 ? numberValue : null; |
|
|
|
} |
|
|
|
|
|
|
|
function readDimensionObject(value: unknown): { width: number; height: number } | null { |
|
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) return null; |
|
|
|
const record = value as Record<string, unknown>; |
|
|
|
const width = readPositiveNumber( |
|
|
|
record.width ?? record.w ?? record.imageWidth ?? record.originWidth ?? record.naturalWidth |
|
|
|
); |
|
|
|
const height = readPositiveNumber( |
|
|
|
record.height ?? record.h ?? record.imageHeight ?? record.originHeight ?? record.naturalHeight |
|
|
|
); |
|
|
|
return width && height ? { width, height } : null; |
|
|
|
} |
|
|
|
|
|
|
|
function readIndexedDimension(value: unknown, index: number): { width: number; height: number } | null { |
|
|
|
if (!value) return null; |
|
|
|
if (Array.isArray(value)) { |
|
|
|
return readDimensionObject(value[index]) ?? readIndexedDimension(value[0], index); |
|
|
|
} |
|
|
|
return readDimensionObject(value); |
|
|
|
} |
|
|
|
|
|
|
|
function readPopiImageDimensions(res: PopiTaskResult, index: number): { width: number; height: number } | null { |
|
|
|
const dimensions = readIndexedDimension(res.dimensions, index); |
|
|
|
if (dimensions) return dimensions; |
|
|
|
|
|
|
|
const indexedWidth = readPositiveNumber(res.widths?.[index] ?? res.imageWidths?.[index]); |
|
|
|
const indexedHeight = readPositiveNumber(res.heights?.[index] ?? res.imageHeights?.[index]); |
|
|
|
if (indexedWidth && indexedHeight) return { width: indexedWidth, height: indexedHeight }; |
|
|
|
|
|
|
|
const width = readPositiveNumber(res.width ?? res.imageWidth); |
|
|
|
const height = readPositiveNumber(res.height ?? res.imageHeight); |
|
|
|
return width && height ? { width, height } : null; |
|
|
|
} |
|
|
|
|
|
|
|
function referenceSubjectUrls(referenceSubjectList: unknown, type: ReferenceSubjectMediaType): string[] { |
|
|
|
if (!Array.isArray(referenceSubjectList)) return []; |
|
|
|
return referenceSubjectList.flatMap((item) => { |
|
|
|
@ -472,7 +523,7 @@ function extractOutputFromTask(task: PopiTask, mediaType: string): GenerationOut |
|
|
|
? { success: true, outputs: [{ type: "audio", data: "", url }] } |
|
|
|
: { success: false, error: "PopiServer audio task completed without output" }; |
|
|
|
} |
|
|
|
const images: { image: string; previewImg?: string }[] = []; |
|
|
|
const images: { image: string; previewImg?: string; dimensions?: { width: number; height: number } | null }[] = []; |
|
|
|
task.resultList?.forEach((res) => { |
|
|
|
const originImages = collectStrings(res?.originImages ?? []); |
|
|
|
const resultImages = collectStrings(res?.images ?? []); |
|
|
|
@ -484,7 +535,7 @@ function extractOutputFromTask(task: PopiTask, mediaType: string): GenerationOut |
|
|
|
const previewImg = |
|
|
|
thumbs[index] || |
|
|
|
(originImages.length > 0 && resultImage && resultImage !== image ? resultImage : undefined); |
|
|
|
images.push({ image, previewImg }); |
|
|
|
images.push({ image, previewImg, dimensions: readPopiImageDimensions(res, index) }); |
|
|
|
}); |
|
|
|
}); |
|
|
|
// task.images is the original input/reference image list in Popiserver task
|
|
|
|
@ -492,7 +543,16 @@ function extractOutputFromTask(task: PopiTask, mediaType: string): GenerationOut |
|
|
|
// not put generated images under resultList.
|
|
|
|
|
|
|
|
return images.length > 0 |
|
|
|
? { success: true, outputs: images.map((img) => ({ type: "image", data: "", url: img.image, previewImg: img.previewImg })) } |
|
|
|
? { |
|
|
|
success: true, |
|
|
|
outputs: images.map((img) => ({ |
|
|
|
type: "image", |
|
|
|
data: "", |
|
|
|
url: img.image, |
|
|
|
...(img.previewImg ? { previewImg: img.previewImg } : {}), |
|
|
|
...(img.dimensions ? { dimensions: img.dimensions } : {}), |
|
|
|
})), |
|
|
|
} |
|
|
|
: { success: false, error: "PopiServer image task completed without output" }; |
|
|
|
} |
|
|
|
|
|
|
|
|