| 35 | ]; |
| 36 | |
| 37 | async function generateMockup(brief: { name: string; prompt: string }) { |
| 38 | console.log(`\n${"=".repeat(60)}`); |
| 39 | console.log(`Generating: ${brief.name}`); |
| 40 | console.log(`${"=".repeat(60)}`); |
| 41 | |
| 42 | const startTime = Date.now(); |
| 43 | |
| 44 | const controller = new AbortController(); |
| 45 | const timeout = setTimeout(() => controller.abort(), 120_000); // 2 min timeout |
| 46 | |
| 47 | const response = await fetch("https://api.openai.com/v1/responses", { |
| 48 | method: "POST", |
| 49 | headers: { |
| 50 | "Authorization": `Bearer ${API_KEY}`, |
| 51 | "Content-Type": "application/json", |
| 52 | }, |
| 53 | body: JSON.stringify({ |
| 54 | model: "gpt-4o", |
| 55 | input: brief.prompt, |
| 56 | tools: [{ |
| 57 | type: "image_generation", |
| 58 | size: "1536x1024", |
| 59 | quality: "high" |
| 60 | }], |
| 61 | }), |
| 62 | signal: controller.signal, |
| 63 | }); |
| 64 | clearTimeout(timeout); |
| 65 | |
| 66 | if (!response.ok) { |
| 67 | const error = await response.text(); |
| 68 | console.error(`FAILED (${response.status}): ${error}`); |
| 69 | return null; |
| 70 | } |
| 71 | |
| 72 | const data = await response.json() as any; |
| 73 | const elapsed = ((Date.now() - startTime) / 1000).toFixed(1); |
| 74 | |
| 75 | // Find the image generation result in output |
| 76 | const imageItem = data.output?.find((item: any) => |
| 77 | item.type === "image_generation_call" |
| 78 | ); |
| 79 | |
| 80 | if (!imageItem?.result) { |
| 81 | console.error("No image data in response. Output types:", |
| 82 | data.output?.map((o: any) => o.type)); |
| 83 | console.error("Full response:", JSON.stringify(data, null, 2).slice(0, 500)); |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | const safeName = brief.name.replace(/[^a-zA-Z0-9_-]/g, "_"); |
| 88 | const outputPath = OUTPUT_DIR + "/" + safeName + ".png"; |
| 89 | const imageBuffer = Buffer.from(imageItem.result, "base64"); |
| 90 | fs.writeFileSync(outputPath, imageBuffer); |
| 91 | |
| 92 | console.log(`OK (${elapsed}s) → ${outputPath}`); |
| 93 | console.log(` Size: ${(imageBuffer.length / 1024).toFixed(0)} KB`); |
| 94 | console.log(` Usage: ${JSON.stringify(data.usage || {})}`); |