import { readFile } from 'node:fs/promises' import { extname } from 'node:path' /** * Resolve repeatable `--reference-image` values into what the Market API takes: an http(s) URL * passes through untouched (the provider fetches it), a local file becomes a data URI. */ export function loadReferenceImages(refs: string[]): Promise { return Promise.all(refs.map(loadReferenceImage)) } async function loadReferenceImage(ref: string): Promise { if (ref.startsWith('http://') || ref.startsWith('https://')) return ref const data = await readFile(ref) return `data:${mimeFor(ref)};base64,${data.toString('base64')}` } function mimeFor(path: string): string { const ext = extname(path).toLowerCase() if (ext === '.png') return 'image/png' if (ext === '.jpg' || ext === '.jpeg') return 'image/jpeg' if (ext === '.webp') return 'image/webp' if (ext === '.gif') return 'image/gif' throw new Error(`Unsupported reference image type "${ext}": ${path}`) }