@@ -37,7 +37,7 @@ import { traceFetchPost } from "./fetchtext.js";
3737import { genaiscriptDebug } from "./debug.js" ;
3838import { OpenAIv2ResponsesChatCompletion } from "./openai-responses.js" ;
3939import type { LanguageModelInfo , RetryOptions , TranscriptionResult } from "./types.js" ;
40- import { resolveBufferLike } from "./bufferlike.js" ;
40+ import { BufferToBlob , resolveBufferLike } from "./bufferlike.js" ;
4141import { getConfigHeaders , OpenAIv1ChatCompletion } from "./openai-chatcompletion.js" ;
4242
4343const dbg = genaiscriptDebug ( "openai" ) ;
@@ -336,7 +336,7 @@ export async function OpenAIImageGeneration(
336336
337337 if ( isMultipart ) {
338338 // Use FormData for image uploads
339- body = new FormData ( ) ;
339+ const form = ( body = new FormData ( ) ) ;
340340
341341 // Add the image file
342342 const imageBuffer = await resolveBufferLike ( image ) ;
@@ -346,43 +346,43 @@ export async function OpenAIImageGeneration(
346346 error : serializeError ( new Error ( "Failed to resolve image buffer" ) ) ,
347347 } ;
348348 }
349- body . append ( "image" , new Blob ( [ imageBuffer ] , { type : "image/png" } ) , "image.png" ) ;
349+ form . append ( "image" , await BufferToBlob ( imageBuffer , "image/png" ) , "image.png" ) ;
350350
351351 // Add mask if provided (only for edit mode)
352352 if ( mode === "edit" && mask ) {
353353 const maskBuffer = await resolveBufferLike ( mask ) ;
354354 if ( maskBuffer ) {
355- body . append ( "mask" , new Blob ( [ maskBuffer ] , { type : "image/png" } ) , "mask.png" ) ;
355+ form . append ( "mask" , await BufferToBlob ( maskBuffer , "image/png" ) , "mask.png" ) ;
356356 }
357357 }
358358
359359 // Add model
360- body . append ( "model" , model ) ;
360+ form . append ( "model" , model ) ;
361361
362362 // Add prompt (required for edit mode)
363363 if ( mode === "edit" ) {
364- body . append ( "prompt" , prompt ) ;
364+ form . append ( "prompt" , prompt ) ;
365365 }
366366
367367 // Add processed parameters
368368 if ( shouldIncludeSize ) {
369- body . append ( "size" , processedParams . size ) ;
369+ form . append ( "size" , processedParams . size ) ;
370370 }
371371
372372 if ( shouldIncludeQuality ) {
373- body . append ( "quality" , processedParams . quality ) ;
373+ form . append ( "quality" , processedParams . quality ) ;
374374 }
375375
376376 if ( shouldIncludeStyle ) {
377- body . append ( "style" , processedParams . style ) ;
377+ form . append ( "style" , processedParams . style ) ;
378378 }
379379
380380 if ( shouldIncludeOutputFormat ) {
381- body . append ( "output_format" , processedParams . outputFormat ) ;
381+ form . append ( "output_format" , processedParams . outputFormat ) ;
382382 }
383383
384384 // Always request b64_json for response format
385- body . append ( "response_format" , "b64_json" ) ;
385+ if ( isDallE ) form . append ( "response_format" , "b64_json" ) ;
386386
387387 // Don't set Content-Type header for FormData, let the browser set it with boundary
388388 delete headers [ "Content-Type" ] ;
@@ -416,9 +416,7 @@ export async function OpenAIImageGeneration(
416416 }
417417
418418 headers [ "Content-Type" ] = "application/json" ;
419- body = JSON . stringify ( body ) ;
420419 }
421-
422420 dbg ( "%o" , {
423421 mode,
424422 endpoint,
@@ -440,13 +438,12 @@ export async function OpenAIImageGeneration(
440438 const freq = {
441439 method : "POST" ,
442440 headers,
443- body,
441+ body : isMultipart ? body : JSON . stringify ( body ) ,
444442 } ;
445443
446444 trace ?. itemValue ( `url` , `[${ url } ](${ url } )` ) ;
447- if ( ! isMultipart ) {
448- traceFetchPost ( trace , url , freq . headers , JSON . parse ( body ) ) ;
449- }
445+
446+ traceFetchPost ( trace , url , freq . headers , body ) ;
450447
451448 const res = await fetch ( url , freq as any ) ;
452449 dbg ( `response: %d %s` , res . status , res . statusText ) ;
0 commit comments