| 126 | } |
| 127 | |
| 128 | async function callFresh( |
| 129 | apiKey: string, |
| 130 | prompt: string, |
| 131 | ): Promise<{ responseId: string; imageData: string }> { |
| 132 | const controller = new AbortController(); |
| 133 | const timeout = setTimeout(() => controller.abort(), 240_000); |
| 134 | |
| 135 | try { |
| 136 | const response = await fetch("https://api.openai.com/v1/responses", { |
| 137 | method: "POST", |
| 138 | headers: { |
| 139 | "Authorization": `Bearer ${apiKey}`, |
| 140 | "Content-Type": "application/json", |
| 141 | }, |
| 142 | body: JSON.stringify({ |
| 143 | model: "gpt-4o", |
| 144 | input: prompt, |
| 145 | tools: [{ type: "image_generation", model: "gpt-image-2", size: "1536x1024", quality: "high" }], |
| 146 | }), |
| 147 | signal: controller.signal, |
| 148 | }); |
| 149 | |
| 150 | if (!response.ok) { |
| 151 | const error = await response.text(); |
| 152 | if (response.status === 403 && error.includes("organization must be verified")) { |
| 153 | throw new Error( |
| 154 | "OpenAI organization verification required.\n" |
| 155 | + "Go to https://platform.openai.com/settings/organization to verify.\n" |
| 156 | + "After verification, wait up to 15 minutes for access to propagate.", |
| 157 | ); |
| 158 | } |
| 159 | throw new Error(`API error (${response.status}): ${error.slice(0, 300)}`); |
| 160 | } |
| 161 | |
| 162 | const data = await response.json() as any; |
| 163 | const imageItem = data.output?.find((item: any) => item.type === "image_generation_call"); |
| 164 | |
| 165 | if (!imageItem?.result) { |
| 166 | throw new Error("No image data in fresh response"); |
| 167 | } |
| 168 | |
| 169 | return { responseId: data.id, imageData: imageItem.result }; |
| 170 | } finally { |
| 171 | clearTimeout(timeout); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | function buildAccumulatedPrompt(originalBrief: string, feedback: string[]): string { |
| 176 | // Cap to last 5 iterations to limit accumulation attack surface |