@@ -45,7 +45,7 @@ export async function generateImages(
45
45
} ) ;
46
46
47
47
// Download all images concurrently
48
- const imageUrls = response . data . map ( image => image . url ) . filter ( url => url != null )
48
+ const imageUrls = response . data ? .map ( image => image . url ) . filter ( url => url != null ) ?? [ ]
49
49
const client = new gptscript . GPTScript ( )
50
50
const generatedImages = await Promise . all (
51
51
imageUrls . map ( async ( url : string ) => {
@@ -80,14 +80,21 @@ async function download(client: gptscript.GPTScript, imageUrl: string): Promise<
80
80
const response = await axios . get ( imageUrl , {
81
81
responseType : 'arraybuffer'
82
82
} )
83
- let content = Buffer . from ( response . data , 'binary' )
83
+
84
+ // Process the image data
85
+ const imageBuffer = Buffer . from ( response . data )
86
+
84
87
// Convert the image to webp format
85
- content = Buffer . from ( await sharp ( content ) . webp ( { quality : 100 } ) . toBuffer ( ) )
88
+ const webpBuffer = await sharp ( imageBuffer ) . webp ( { quality : 100 } ) . toBuffer ( )
86
89
87
90
// Generate a SHA-256 hash of the imageURL to use as the filename
88
91
const filePath = `generated_image_${ createHash ( 'sha256' ) . update ( imageUrl ) . digest ( 'hex' ) . substring ( 0 , 8 ) } .webp` ;
89
92
90
- await client . writeFileInWorkspace ( `${ threadId ? 'files/' : '' } ${ filePath } ` , content . buffer ) ;
93
+ // Write the file to workspace
94
+ await client . writeFileInWorkspace (
95
+ `${ threadId ? 'files/' : '' } ${ filePath } ` ,
96
+ new Uint8Array ( webpBuffer ) . buffer
97
+ ) ;
91
98
92
99
return filePath
93
100
}
0 commit comments