import type { Command } from "commander"; import fs from "node:fs"; import path from "node:path"; import { getMaterialUri, getSessionFromCommand, syncToTOS } from "../services/cerevox"; import type { Session } from "cerevox"; import { createProgressSpinner } from "../utils/progress"; export const name = "image"; export const description = "Image command: create image"; export function register(program: Command): void { const parent = program.command("image").description("Create a new image; requires --prompt"); const allowedModels = [ "seedream", "seedream-pro", "seedream-5l", "banana", "banana2", "banana-pro", "wan", "wan-pro", ] as const; type AllowedModel = (typeof allowedModels)[number]; const allowedAspectRatios = [ "1:1", "3:4", "4:3", "16:9", "9:16", "2:3", "3:2", "21:9", "1:4", "4:1", "1:8", "8:1", ] as const; type AllowedAspectRatio = (typeof allowedAspectRatios)[number]; const allowedResolutions = ["1K", "2K", "4K"] as const; type AllowedResolution = (typeof allowedResolutions)[number]; const allowedTypes = ["default", "storyboard", "subject-turnaround"] as const; type AllowedType = (typeof allowedTypes)[number]; async function performImageGeneration( session: Session, { prompt, model, type, aspectRatio, resolution, refsList, output, }: { prompt: string; model?: AllowedModel; type: AllowedType; aspectRatio?: AllowedAspectRatio; resolution?: AllowedResolution; refsList: string[]; output?: string; } ): Promise { const referenceImages = await Promise.all( refsList.map(async (ref) => ({ url: await getMaterialUri(session, ref) })) ); const onProgress = createProgressSpinner("inferencing"); const payload: Record = { model: model || "seedream-5l", type, prompt, aspect_ratio: aspectRatio, resolution, reference_images: referenceImages, sequential_image_generation: "disabled", onProgress, }; const res = await (session.ai.generateImage as (arg: Record) => Promise)( payload ); if (res?.urls && Array.isArray(res.urls) && res.urls.length > 0) { try { const tosUrl = await syncToTOS(res.urls[0]); if (tosUrl) { res.url = tosUrl; res.urls[0] = tosUrl; } } catch {} } process.stdout.write("\n"); if (output) { const dir = process.cwd(); const url = typeof res?.url === "string" && res.url.length > 0 ? (res.url as string) : Array.isArray(res?.urls) && typeof res.urls[0] === "string" && res.urls[0].length > 0 ? (res.urls[0] as string) : undefined; if (!url) { process.stderr.write( "Cannot save --output because no image URL was returned. Please retry later or run without --output to inspect raw response.\n" ); process.exitCode = 1; console.log(res); return; } const response = await fetch(url); const buffer = Buffer.from(await response.arrayBuffer()); const filePath = path.resolve(dir, output); if (!fs.existsSync(path.dirname(filePath))) { fs.mkdirSync(path.dirname(filePath), { recursive: true }); } fs.writeFileSync(filePath, buffer); res.output = filePath; } console.log(res); } async function imageCreateAction( this: Command, opts: { prompt?: string; model?: string; type?: string; aspectRatio?: string; resolution?: string; refs?: string; output?: string; } ) { const session = getSessionFromCommand(this as unknown as Record); if (!session) { process.stderr.write("No active session\n"); return; } const prompt = typeof opts.prompt === "string" ? opts.prompt : undefined; if (!prompt || prompt.trim().length === 0) { process.stderr.write("Missing required option: --prompt\n"); process.exitCode = 1; return; } const model = typeof opts.model === "string" ? opts.model.trim() : undefined; if (model && !(allowedModels as readonly string[]).includes(model)) { process.stderr.write( `Invalid value for --model: ${model}. Allowed: ${allowedModels.join("|")}\n` ); process.exitCode = 1; return; } const modelArg: AllowedModel | undefined = (model ?? undefined) as AllowedModel | undefined; const type = typeof opts.type === "string" ? opts.type.trim() : "default"; if (!(allowedTypes as readonly string[]).includes(type)) { process.stderr.write( `Invalid value for --type: ${type}. Allowed: ${allowedTypes.join("|")}\n` ); process.exitCode = 1; return; } const typeArg: AllowedType = type as AllowedType; const aspectRatio = typeof opts.aspectRatio === "string" ? (opts.aspectRatio.trim() as AllowedAspectRatio) : undefined; if (aspectRatio && !(allowedAspectRatios as readonly string[]).includes(aspectRatio)) { process.stderr.write( `Invalid value for --aspectRatio: ${aspectRatio}. Allowed: ${allowedAspectRatios.join("|")}\n` ); process.exitCode = 1; return; } const resolution = typeof opts.resolution === "string" ? (opts.resolution.trim() as AllowedResolution) : undefined; if (resolution && !(allowedResolutions as readonly string[]).includes(resolution)) { process.stderr.write( `Invalid value for --resolution: ${resolution}. Allowed: ${allowedResolutions.join("|")}\n` ); process.exitCode = 1; return; } const refsList = typeof opts.refs === "string" && opts.refs.length > 0 ? opts.refs .split(",") .map((s) => s.trim()) .filter((s) => s.length > 0) : []; const output = typeof opts.output === "string" ? opts.output : undefined; await performImageGeneration(session, { prompt, model: modelArg, type: typeArg, aspectRatio, resolution, refsList, output, }); } // default action on `zerocut image` parent .option("--prompt ", "Text prompt for image generation (required)") .option("--model ", `Generator model: ${allowedModels.join("|")}`) .option("--type ", `Image type: ${allowedTypes.join("|")}`) .option("--aspectRatio ", `Aspect ratio: ${allowedAspectRatios.join("|")}`) .option("--resolution ", `Resolution: ${allowedResolutions.join("|")}`) .option("--refs ", "Comma-separated reference image paths/urls") .option("--output ", "Output file path") .action(imageCreateAction); // keep `image create` for compatibility parent .command("create") .description("Create a new image; requires --prompt") .option("--prompt ", "Text prompt for image generation (required)") .option("--model ", `Generator model: ${allowedModels.join("|")}`) .option("--type ", `Image type: ${allowedTypes.join("|")}`) .option("--aspectRatio ", `Aspect ratio: ${allowedAspectRatios.join("|")}`) .option("--resolution ", `Resolution: ${allowedResolutions.join("|")}`) .option("--refs ", "Comma-separated reference image paths/urls") .option("--output ", "Output file path") .action(imageCreateAction); // removed `image edit` }