7 changed files with 113 additions and 25 deletions
@ -0,0 +1,52 @@ |
|||||
|
const IMAGE_COUNT_PARAMETER_NAMES = [ |
||||
|
"batchSize", |
||||
|
"imageCount", |
||||
|
"numImages", |
||||
|
"num_images", |
||||
|
"numOutputs", |
||||
|
"num_outputs", |
||||
|
"count", |
||||
|
]; |
||||
|
|
||||
|
export function positiveInteger(value: unknown): number | null { |
||||
|
const numeric = typeof value === "number" |
||||
|
? value |
||||
|
: typeof value === "string" && value.trim().length > 0 |
||||
|
? Number(value) |
||||
|
: NaN; |
||||
|
return Number.isInteger(numeric) && numeric > 0 ? numeric : null; |
||||
|
} |
||||
|
|
||||
|
export function mergeConfiguredParameters( |
||||
|
base: Record<string, unknown> | undefined, |
||||
|
configured: Record<string, unknown> |
||||
|
): Record<string, unknown> { |
||||
|
const parameters = { ...(base ?? {}) }; |
||||
|
for (const [key, value] of Object.entries(configured)) { |
||||
|
if (value !== undefined && value !== null) { |
||||
|
parameters[key] = parameters[key] ?? value; |
||||
|
} |
||||
|
} |
||||
|
return parameters; |
||||
|
} |
||||
|
|
||||
|
export function resolveImageGenerationCount( |
||||
|
parameters: Record<string, unknown> | undefined, |
||||
|
nodeImageCount: number | undefined |
||||
|
): number { |
||||
|
for (const name of IMAGE_COUNT_PARAMETER_NAMES) { |
||||
|
const value = positiveInteger(parameters?.[name]); |
||||
|
if (value !== null) return value; |
||||
|
} |
||||
|
return positiveInteger(nodeImageCount) ?? 1; |
||||
|
} |
||||
|
|
||||
|
export function mergeImageGenerationParameters( |
||||
|
base: Record<string, unknown> | undefined, |
||||
|
configured: Record<string, unknown>, |
||||
|
imageCount: number |
||||
|
): Record<string, unknown> { |
||||
|
const parameters = mergeConfiguredParameters(base, configured); |
||||
|
parameters.batchSize = positiveInteger(parameters.batchSize) ?? imageCount; |
||||
|
return parameters; |
||||
|
} |
||||
Loading…
Reference in new issue