Skip to content

Commit fd1c96d

Browse files
committed
refactor(cli): consolidate image option validation
1 parent 7c790ee commit fd1c96d

2 files changed

Lines changed: 30 additions & 54 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2372,7 +2372,7 @@ src/cli/acp-cli.ts 7
23722372
src/cli/attach-cli.ts 3
23732373
src/cli/capability-cli/audio.ts 3
23742374
src/cli/capability-cli/embedding.ts 3
2375-
src/cli/capability-cli/image.ts 21
2375+
src/cli/capability-cli/image.ts 17
23762376
src/cli/capability-cli/model.ts 4
23772377
src/cli/capability-cli/shared.ts 6
23782378
src/cli/capability-cli/tts.ts 6

‎src/cli/capability-cli/image.ts‎

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "../../media-understanding/runtime.js";
2727
import { getImageMetadata } from "../../media/media-services.js";
2828
import { defaultRuntime } from "../../runtime.js";
29+
import { formatHumanList } from "../../shared/human-list.js";
2930
import { runCommandWithRuntime } from "../cli-utils.js";
3031
import { getModelsCommandSecretTargetIds } from "../command-secret-targets.js";
3132
import { readInputFiles, writeOutputAsset } from "../media-output.js";
@@ -48,6 +49,8 @@ import {
4849

4950
const IMAGE_OUTPUT_FORMATS = ["png", "jpeg", "webp"] as const;
5051
const 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

5255
async 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

291254
function 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

Comments
 (0)