(
responseId: string,
)
| 246 | if (!data || typeof data !== "object") return null; |
| 247 | |
| 248 | let imageBase64: string | null = null; |
| 249 | let revisedPrompt: string | null = null; |
| 250 | |
| 251 | const visit = (value: any): void => { |
| 252 | if (!value || typeof value !== "object") return; |
| 253 | if ( |
| 254 | typeof value.partial_image_b64 === "string" && |
| 255 | value.partial_image_b64 |
| 256 | ) { |
| 257 | imageBase64 = value.partial_image_b64; |
| 258 | } |
| 259 | if (typeof value.revised_prompt === "string" && value.revised_prompt) { |
| 260 | revisedPrompt = value.revised_prompt; |
| 261 | } |
| 262 | if (Array.isArray(value)) { |
| 263 | for (const item of value) visit(item); |
| 264 | return; |
| 265 | } |
| 266 | for (const nested of Object.values(value)) { |
| 267 | if (nested && typeof nested === "object") visit(nested); |
| 268 | } |
| 269 | }; |
| 270 | |
| 271 | visit(data); |
| 272 | |
| 273 | return { |
| 274 | imageBase64, |
| 275 | revisedPrompt, |
| 276 | status: typeof data.status === "string" ? data.status : null, |
| 277 | responseId: typeof data.id === "string" ? data.id : responseId, |
| 278 | }; |
| 279 | } catch { |
| 280 | return null; |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | const streamResult = await readStreamResult(); |
| 285 | if ( |
| 286 | streamResult.imageBase64 || |
| 287 | !streamResult.responseId || |
| 288 | streamResult.status !== "in_progress" |
| 289 | ) { |
| 290 | return streamResult; |
| 291 | } |
| 292 | |
| 293 | let attempt = 0; |
| 294 | while (true) { |
| 295 | attempt += 1; |
| 296 | if (Date.now() >= deadlineAt) { |
no test coverage detected