@ -420,6 +420,7 @@ const NEWAPIWG_IMAGE_RESOLUTION_PIXELS: Record<string, number> = {
"2K" : 2560 * 1440 ,
"2K" : 2560 * 1440 ,
"4K" : 3840 * 2160 ,
"4K" : 3840 * 2160 ,
} ;
} ;
const NEWAPIWG_SEEDREAM_MIN_IMAGE_PIXELS = NEWAPIWG_IMAGE_RESOLUTION_PIXELS [ "2K" ] ;
const REFERENCE_IMAGE_FETCH_TIMEOUT_MS = 30 _000 ;
const REFERENCE_IMAGE_FETCH_TIMEOUT_MS = 30 _000 ;
const MAX_REFERENCE_IMAGE_BYTES = 25 * 1024 * 1024 ;
const MAX_REFERENCE_IMAGE_BYTES = 25 * 1024 * 1024 ;
@ -493,12 +494,44 @@ function ceilToMultiple(value: number, multiple: number): number {
return Math . ceil ( value / multiple ) * multiple ;
return Math . ceil ( value / multiple ) * multiple ;
}
}
function resolveNewApiWGImageSize ( parameters : Record < string , unknown > | undefined ) : string {
function isSeedreamImageModel ( modelId : string ) : boolean {
return /seedream/i . test ( modelId ) ;
}
function parsePixelSize ( value : string ) : { width : number ; height : number } | null {
const match = value . match ( /^(\d+)x(\d+)$/i ) ;
if ( ! match ) return null ;
const width = Number ( match [ 1 ] ) ;
const height = Number ( match [ 2 ] ) ;
if ( ! Number . isFinite ( width ) || ! Number . isFinite ( height ) || width <= 0 || height <= 0 ) {
return null ;
}
return { width , height } ;
}
function pixelSizeForAspectRatio ( targetPixels : number , ratio : { width : number ; height : number } ) : string {
const width = ceilToMultiple ( Math . sqrt ( targetPixels * ratio . width / ratio . height ) , 8 ) ;
const height = ceilToMultiple ( Math . sqrt ( targetPixels * ratio . height / ratio . width ) , 8 ) ;
return ` ${ width } x ${ height } ` ;
}
function clampPixelSize ( size : string , minPixels : number ) : string {
if ( minPixels <= 0 ) return size ;
const parsed = parsePixelSize ( size ) ;
if ( ! parsed || parsed . width * parsed . height >= minPixels ) return size ;
return pixelSizeForAspectRatio ( minPixels , parsed ) ;
}
function resolveNewApiWGImageSize (
parameters : Record < string , unknown > | undefined ,
modelId : string
) : string {
const minPixels = isSeedreamImageModel ( modelId ) ? NEWAPIWG_SEEDREAM_MIN_IMAGE_PIXELS : 0 ;
const requestedSize =
const requestedSize =
getStringParameter ( parameters , "imageSize" ) ? ?
getStringParameter ( parameters , "imageSize" ) ? ?
getStringParameter ( parameters , "resolution" ) ;
getStringParameter ( parameters , "resolution" ) ;
if ( requestedSize ) {
if ( requestedSize ) {
if ( /^\d+x\d+$/i . test ( requestedSize ) ) return requestedSize ;
if ( /^\d+x\d+$/i . test ( requestedSize ) ) return clampPixelSize ( requestedSize , minPixels ) ;
const targetPixels = NEWAPIWG_IMAGE_RESOLUTION_PIXELS [ requestedSize ] ;
const targetPixels = NEWAPIWG_IMAGE_RESOLUTION_PIXELS [ requestedSize ] ;
if ( targetPixels ) {
if ( targetPixels ) {
@ -506,15 +539,13 @@ function resolveNewApiWGImageSize(parameters: Record<string, unknown> | undefine
getStringParameter ( parameters , "aspectRatio" ) ? ?
getStringParameter ( parameters , "aspectRatio" ) ? ?
getStringParameter ( parameters , "aspect_ratio" )
getStringParameter ( parameters , "aspect_ratio" )
) ;
) ;
const width = ceilToMultiple ( Math . sqrt ( targetPixels * ratio . width / ratio . height ) , 8 ) ;
return pixelSizeForAspectRatio ( Math . max ( targetPixels , minPixels ) , ratio ) ;
const height = ceilToMultiple ( Math . sqrt ( targetPixels * ratio . height / ratio . width ) , 8 ) ;
return ` ${ width } x ${ height } ` ;
}
}
}
}
const explicitSize = getStringParameter ( parameters , "size" ) ;
const explicitSize = getStringParameter ( parameters , "size" ) ;
if ( explicitSize ) return explicitSize ;
if ( explicitSize ) return clampPixelSize ( explicitSize , minPixels ) ;
return "1024x1024";
return clampPixelSize ( "1024x1024", minPixels ) ;
}
}
function isImagenGenerationModel ( modelId : string ) : boolean {
function isImagenGenerationModel ( modelId : string ) : boolean {
@ -940,7 +971,7 @@ export async function generateWithNewApiWG(
}
}
const endpoint = ` ${ normalizedBaseUrl } / ${ isVideo ? "video" : "images" } /generations ` ;
const endpoint = ` ${ normalizedBaseUrl } / ${ isVideo ? "video" : "images" } /generations ` ;
const imageSize = resolveNewApiWGImageSize ( input . parameters ) ;
const imageSize = resolveNewApiWGImageSize ( input . parameters , input . model . id ) ;
const imageParameters = normalizeNewApiWGImageGenerationParameters ( input . parameters ) ;
const imageParameters = normalizeNewApiWGImageGenerationParameters ( input . parameters ) ;
const rawDynamicInputs = dynamicInputsWithoutPrompt ( input ) ;
const rawDynamicInputs = dynamicInputsWithoutPrompt ( input ) ;
const normalizedVideoInputs = isVideo
const normalizedVideoInputs = isVideo