( imagePath: string, repoRoot?: string, )
| 20 | * Generate a structured implementation prompt from an approved mockup. |
| 21 | */ |
| 22 | export async function generateDesignToCodePrompt( |
| 23 | imagePath: string, |
| 24 | repoRoot?: string, |
| 25 | ): Promise<DesignToCodeResult> { |
| 26 | const apiKey = requireApiKey(); |
| 27 | const imageData = fs.readFileSync(imagePath).toString("base64"); |
| 28 | |
| 29 | // Read DESIGN.md if available for additional context |
| 30 | const designConstraints = repoRoot ? readDesignConstraints(repoRoot) : null; |
| 31 | |
| 32 | const controller = new AbortController(); |
| 33 | const timeout = setTimeout(() => controller.abort(), 60_000); |
| 34 | |
| 35 | try { |
| 36 | const contextBlock = designConstraints |
| 37 | ? `\n\nExisting DESIGN.md (use these as constraints):\n${designConstraints}` |
| 38 | : ""; |
| 39 | |
| 40 | const response = await fetch("https://api.openai.com/v1/chat/completions", { |
| 41 | method: "POST", |
| 42 | headers: { |
| 43 | "Authorization": `Bearer ${apiKey}`, |
| 44 | "Content-Type": "application/json", |
| 45 | }, |
| 46 | body: JSON.stringify({ |
| 47 | model: "gpt-4o", |
| 48 | messages: [{ |
| 49 | role: "user", |
| 50 | content: [ |
| 51 | { |
| 52 | type: "image_url", |
| 53 | image_url: { url: `data:image/png;base64,${imageData}` }, |
| 54 | }, |
| 55 | { |
| 56 | type: "text", |
| 57 | text: `Analyze this approved UI mockup and generate a structured implementation prompt. Return valid JSON only: |
| 58 | |
| 59 | { |
| 60 | "implementationPrompt": "A detailed paragraph telling a developer exactly how to build this UI. Include specific CSS values, layout approach (flex/grid), component structure, and interaction behaviors. Reference the specific elements visible in the mockup.", |
| 61 | "colors": ["#hex - usage", ...], |
| 62 | "typography": ["role: family, size, weight", ...], |
| 63 | "layout": ["description of layout pattern", ...], |
| 64 | "components": ["component name - description", ...] |
| 65 | } |
| 66 | |
| 67 | Be specific about every visual detail: exact hex colors, font sizes in px, spacing values, border-radius, shadows. The developer should be able to implement this without looking at the mockup again.${contextBlock}`, |
| 68 | }, |
| 69 | ], |
| 70 | }], |
| 71 | max_tokens: 1000, |
| 72 | response_format: { type: "json_object" }, |
| 73 | }), |
| 74 | signal: controller.signal, |
| 75 | }); |
| 76 | |
| 77 | if (!response.ok) { |
| 78 | const error = await response.text(); |
| 79 | throw new Error(`API error (${response.status}): ${error.slice(0, 200)}`); |
no test coverage detected