(imagePath: string)
| 27 | * Extract visual language from an approved mockup PNG. |
| 28 | */ |
| 29 | export async function extractDesignLanguage(imagePath: string): Promise<ExtractedDesign> { |
| 30 | const apiKey = requireApiKey(); |
| 31 | const imageData = fs.readFileSync(imagePath).toString("base64"); |
| 32 | |
| 33 | const controller = new AbortController(); |
| 34 | const timeout = setTimeout(() => controller.abort(), 60_000); |
| 35 | |
| 36 | try { |
| 37 | const response = await fetch("https://api.openai.com/v1/chat/completions", { |
| 38 | method: "POST", |
| 39 | headers: { |
| 40 | "Authorization": `Bearer ${apiKey}`, |
| 41 | "Content-Type": "application/json", |
| 42 | }, |
| 43 | body: JSON.stringify({ |
| 44 | model: "gpt-4o", |
| 45 | messages: [{ |
| 46 | role: "user", |
| 47 | content: [ |
| 48 | { |
| 49 | type: "image_url", |
| 50 | image_url: { url: `data:image/png;base64,${imageData}` }, |
| 51 | }, |
| 52 | { |
| 53 | type: "text", |
| 54 | text: `Analyze this UI mockup and extract the design language. Return valid JSON only, no markdown: |
| 55 | |
| 56 | { |
| 57 | "colors": [{"name": "primary", "hex": "#...", "usage": "buttons, links"}, ...], |
| 58 | "typography": [{"role": "heading", "family": "...", "size": "...", "weight": "..."}, ...], |
| 59 | "spacing": ["8px base unit", "16px between sections", ...], |
| 60 | "layout": ["left-aligned content", "max-width 1200px", ...], |
| 61 | "mood": "one sentence describing the overall feel" |
| 62 | } |
| 63 | |
| 64 | Extract real values from what you see. Be specific about hex colors and font sizes.`, |
| 65 | }, |
| 66 | ], |
| 67 | }], |
| 68 | max_tokens: 800, |
| 69 | response_format: { type: "json_object" }, |
| 70 | }), |
| 71 | signal: controller.signal, |
| 72 | }); |
| 73 | |
| 74 | if (!response.ok) { |
| 75 | console.error(`Vision extraction failed (${response.status})`); |
| 76 | return defaultDesign(); |
| 77 | } |
| 78 | |
| 79 | const data = await response.json() as any; |
| 80 | const content = data.choices?.[0]?.message?.content?.trim() || ""; |
| 81 | return JSON.parse(content) as ExtractedDesign; |
| 82 | } catch (err: any) { |
| 83 | console.error(`Design extraction error: ${err.message}`); |
| 84 | return defaultDesign(); |
| 85 | } finally { |
| 86 | clearTimeout(timeout); |
no test coverage detected