| 1 | import type { ResolvedImageModelConfig } from '../types' |
| 2 | import { readRecord, readString } from './utils' |
| 3 | |
| 4 | const readPositiveInteger = (value: unknown): number | null => { |
| 5 | const number = Number(value) |
| 6 | return Number.isFinite(number) && number > 0 ? Math.floor(number) : null |
| 7 | } |
| 8 | |
| 9 | const readSizeOption = (value: unknown): string => { |
| 10 | if (typeof value === 'string') return value.trim() |
| 11 | const option = readRecord(value) |
| 12 | const explicit = |
| 13 | readString(option, 'value') || |
| 14 | readString(option, 'size') || |
| 15 | readString(option, 'imageSize') || |
| 16 | readString(option, 'image_size') |
| 17 | if (explicit) return explicit |
| 18 | const width = readPositiveInteger(option.width) |
| 19 | const height = readPositiveInteger(option.height) |
| 20 | return width && height ? `${width}x${height}` : '' |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Model settings may declare one fixed size or a list of supported values. Automatic generation |
| 25 | * always uses this provider/model default; only the manual image panel supplies a user selection. |
| 26 | */ |
| 27 | export const resolveConfiguredDefaultImageSize = (config: ResolvedImageModelConfig): string => { |
| 28 | const modelConfig = config.modelConfig |
| 29 | const configured = |
| 30 | readString(modelConfig, 'defaultSize') || |
| 31 | readString(modelConfig, 'default_size') || |
| 32 | readString(modelConfig, 'size') || |
| 33 | readString(modelConfig, 'imageSize') || |
| 34 | readString(modelConfig, 'image_size') |
| 35 | if (configured) return configured |
| 36 | |
| 37 | const width = readPositiveInteger(modelConfig.width) |
| 38 | const height = readPositiveInteger(modelConfig.height) |
| 39 | if (width && height) return `${width}x${height}` |
| 40 | |
| 41 | const numericSize = readPositiveInteger(modelConfig.size) |
| 42 | if (numericSize) return String(numericSize) |
| 43 | |
| 44 | for (const key of ['sizes', 'supportedSizes', 'aspectRatios', 'aspect_ratios', 'ratios']) { |
| 45 | const options = modelConfig[key] |
| 46 | if (!Array.isArray(options)) continue |
| 47 | const selected = options.map(readSizeOption).find(Boolean) |
| 48 | if (selected) return selected |
| 49 | } |
| 50 | return '' |
| 51 | } |
| 52 |