@@ -26,6 +26,7 @@ import {
2626} from "../../media-understanding/runtime.js" ;
2727import { getImageMetadata } from "../../media/media-services.js" ;
2828import { defaultRuntime } from "../../runtime.js" ;
29+ import { formatHumanList } from "../../shared/human-list.js" ;
2930import { runCommandWithRuntime } from "../cli-utils.js" ;
3031import { getModelsCommandSecretTargetIds } from "../command-secret-targets.js" ;
3132import { readInputFiles , writeOutputAsset } from "../media-output.js" ;
@@ -48,6 +49,8 @@ import {
4849
4950const IMAGE_OUTPUT_FORMATS = [ "png" , "jpeg" , "webp" ] as const ;
5051const IMAGE_BACKGROUNDS = [ "transparent" , "opaque" , "auto" ] as const ;
52+ const IMAGE_QUALITIES = [ "low" , "medium" , "high" , "auto" ] as const ;
53+ const IMAGE_OPENAI_MODERATIONS = [ "low" , "auto" ] as const ;
5154
5255async function runImageGenerate ( params : {
5356 capability : "image.generate" | "image.edit" ;
@@ -232,60 +235,20 @@ async function runImageDescribe(params: {
232235 } satisfies CapabilityEnvelope ;
233236}
234237
235- function normalizeImageOutputFormat (
238+ function normalizeImageOption < T extends string > (
236239 raw : string | undefined ,
237- ) : ImageGenerationOutputFormat | undefined {
240+ values : readonly T [ ] ,
241+ label : string ,
242+ ) : T | undefined {
238243 const normalized = normalizeLowercaseStringOrEmpty ( raw ) ;
239244 if ( ! normalized ) {
240245 return undefined ;
241246 }
242- if ( ( IMAGE_OUTPUT_FORMATS as readonly string [ ] ) . includes ( normalized ) ) {
243- return normalized as ImageGenerationOutputFormat ;
247+ const match = values . find ( ( value ) => value === normalized ) ;
248+ if ( match ) {
249+ return match ;
244250 }
245- throw new Error ( "--output-format must be one of png, jpeg, or webp" ) ;
246- }
247-
248- function normalizeImageBackground (
249- raw : string | undefined ,
250- label = "--background" ,
251- ) : ImageGenerationBackground | undefined {
252- const normalized = normalizeLowercaseStringOrEmpty ( raw ) ;
253- if ( ! normalized ) {
254- return undefined ;
255- }
256- if ( ( IMAGE_BACKGROUNDS as readonly string [ ] ) . includes ( normalized ) ) {
257- return normalized as ImageGenerationBackground ;
258- }
259- throw new Error ( `${ label } must be one of transparent, opaque, or auto` ) ;
260- }
261-
262- function normalizeImageQuality ( raw : string | undefined ) : ImageGenerationQuality | undefined {
263- const normalized = normalizeLowercaseStringOrEmpty ( raw ) ;
264- if ( ! normalized ) {
265- return undefined ;
266- }
267- if (
268- normalized === "low" ||
269- normalized === "medium" ||
270- normalized === "high" ||
271- normalized === "auto"
272- ) {
273- return normalized ;
274- }
275- throw new Error ( "--quality must be one of low, medium, high, or auto" ) ;
276- }
277-
278- function normalizeOpenAIModeration (
279- raw : string | undefined ,
280- ) : ImageGenerationOpenAIModeration | undefined {
281- const normalized = normalizeLowercaseStringOrEmpty ( raw ) ;
282- if ( ! normalized ) {
283- return undefined ;
284- }
285- if ( normalized === "low" || normalized === "auto" ) {
286- return normalized ;
287- }
288- throw new Error ( "--openai-moderation must be one of low or auto" ) ;
251+ throw new Error ( `${ label } must be one of ${ formatHumanList ( values ) } ` ) ;
289252}
290253
291254function resolveImageDescribeInput ( filePath : string ) : string {
@@ -322,14 +285,27 @@ function resolveImageGenerationOptions(opts: Record<string, unknown>, command: C
322285 size : opts . size as string | undefined ,
323286 aspectRatio : opts . aspectRatio as string | undefined ,
324287 resolution : opts . resolution as "1K" | "2K" | "4K" | undefined ,
325- outputFormat : normalizeImageOutputFormat ( opts . outputFormat as string | undefined ) ,
326- background : normalizeImageBackground ( opts . background as string | undefined ) ,
327- openaiBackground : normalizeImageBackground (
288+ outputFormat : normalizeImageOption (
289+ opts . outputFormat as string | undefined ,
290+ IMAGE_OUTPUT_FORMATS ,
291+ "--output-format" ,
292+ ) ,
293+ background : normalizeImageOption (
294+ opts . background as string | undefined ,
295+ IMAGE_BACKGROUNDS ,
296+ "--background" ,
297+ ) ,
298+ openaiBackground : normalizeImageOption (
328299 opts . openaiBackground as string | undefined ,
300+ IMAGE_BACKGROUNDS ,
329301 "--openai-background" ,
330302 ) ,
331- openaiModeration : normalizeOpenAIModeration ( opts . openaiModeration as string | undefined ) ,
332- quality : normalizeImageQuality ( opts . quality as string | undefined ) ,
303+ openaiModeration : normalizeImageOption (
304+ opts . openaiModeration as string | undefined ,
305+ IMAGE_OPENAI_MODERATIONS ,
306+ "--openai-moderation" ,
307+ ) ,
308+ quality : normalizeImageOption ( opts . quality as string | undefined , IMAGE_QUALITIES , "--quality" ) ,
333309 timeoutMs : parseOptionalTimeoutMs ( opts . timeoutMs as string | number | undefined ) ,
334310 output : opts . output as string | undefined ,
335311 } ;
0 commit comments