import 'server-only'; // Reverse-engineer a textual style description from an image using // OpenAI's vision-capable chat model. Used by the admin "Analyze style" // button on custom-style commissions to turn the customer's uploaded // reference image into a paragraph the image-edit endpoint can read — // since /v1/images/edits only accepts ONE image (the pet subject), the // style has to travel as text on the OpenAI path. const VISION_MODEL = 'gpt-4o'; const SYSTEM_INSTRUCTIONS = [ 'You are an art critic helping another artist recreate a visual style.', 'You will be shown one image. Describe ITS VISUAL STYLE ONLY — do NOT describe what the subject is.', 'Cover, in this order, in 4–8 short sentences:', ' • Medium (oil, watercolor, gouache, ink, digital, photograph, mixed media, etc.) and apparent surface (canvas, paper texture, screen grain).', ' • Palette (dominant hues, saturation, warm/cool bias, contrast range).', ' • Brushwork / line quality / mark-making (loose, tight, impasto, dry-brush, scratchy, smooth, etc.).', ' • Lighting and atmosphere (hard, soft, directional, ambient, hazy, lit-from-within, etc.).', ' • Composition tendencies if obvious (cropped tight, vignetted, central, off-center).', ' • Mood the style projects (intimate, regal, playful, melancholic, monumental, etc.).', 'Be specific and concrete — names of techniques or movements ("ukiyo-e woodblock", "Basquiat-style neo-expressionist", "muted Edward Hopper realism") are great anchors when they fit.', 'Do not mention the subject. Do not reference what the image depicts. Style only.', 'Output the description as a single paragraph or short list — plain prose, no headers, no meta-commentary about your task.', ].join('\n'); /** Calls GPT-4o Vision against a publicly-fetchable image URL and * returns a trimmed style description. Throws on any failure — the * caller (analyze-style route) catches and returns a 500. * * Cost: ~$0.005–$0.01 per call on a typical CF Images-served upload * (small image → ~1–2k input tokens; output ~150–300 tokens). */ export async function describeImageStyle(imageUrl: string): Promise { const key = process.env.OPENAI_API_KEY; if (!key) throw new Error('OPENAI_API_KEY not set'); const res = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', headers: { Authorization: `Bearer ${key}`, 'content-type': 'application/json', }, body: JSON.stringify({ model: VISION_MODEL, // Low temperature so the description stays focused + reproducible; // a creative re-analyze is rarely what admin wants. temperature: 0.3, messages: [ { role: 'user', content: [ { type: 'text', text: SYSTEM_INSTRUCTIONS }, { type: 'image_url', image_url: { url: imageUrl } }, ], }, ], }), }); if (!res.ok) { const text = await res.text(); throw new Error(`OpenAI Vision ${res.status}: ${text.slice(0, 400)}`); } const json = (await res.json()) as { choices?: { message?: { content?: string } }[]; }; const content = json?.choices?.[0]?.message?.content?.trim(); if (!content) throw new Error('OpenAI Vision returned no content'); return content; }