@@ -1878,25 +1878,9 @@ export const POST: RequestHandler = async ({ request }) => {
18781878 const imageCost = response . cost ?? 0 ;
18791879 log ( `Image generation cost: $${ imageCost } ` , startTime ) ;
18801880
1881- const image = response . data ?. [ 0 ] ;
1882- if ( ! image ?. b64_json && ! image ?. url ) {
1883- throw new Error ( 'No image data returned details: ' + JSON . stringify ( image ) ) ;
1884- }
1885-
1886- let buffer : Buffer ;
1887- let mimeType = 'image/png' ;
1888-
1889- if ( image . b64_json ) {
1890- buffer = Buffer . from ( image . b64_json , 'base64' ) ;
1891- } else if ( image . url ) {
1892- // Fallback download
1893- const imgRes = await fetch ( image . url ) ;
1894- const arrayBuffer = await imgRes . arrayBuffer ( ) ;
1895- buffer = Buffer . from ( arrayBuffer ) ;
1896- const contentType = imgRes . headers . get ( 'content-type' ) ;
1897- if ( contentType ) mimeType = contentType ;
1898- } else {
1899- throw new Error ( 'No image data' ) ;
1881+ const responseImages = response . data ?? [ ] ;
1882+ if ( ! Array . isArray ( responseImages ) || responseImages . length === 0 ) {
1883+ throw new Error ( 'No image data returned details: ' + JSON . stringify ( response . data ) ) ;
19001884 }
19011885
19021886 // Ensure upload dir exists
@@ -1905,37 +1889,76 @@ export const POST: RequestHandler = async ({ request }) => {
19051889 mkdirSync ( UPLOAD_DIR , { recursive : true } ) ;
19061890 }
19071891
1908- const storageId = generateId ( ) ;
1909- const extension = ( mimeType . split ( '/' ) [ 1 ] || 'png' ) . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) || 'png' ;
1910- const filename = `${ storageId } .${ extension } ` ;
1911- const filepath = join ( UPLOAD_DIR , filename ) ;
1892+ const generatedImages : Array < { url : string ; storage_id : string ; fileName ?: string } > = [ ] ;
19121893
1913- // Prevent path traversal
1914- if ( ! resolve ( filepath ) . startsWith ( resolve ( UPLOAD_DIR ) ) ) {
1915- throw new Error ( 'Invalid file path' ) ;
1916- }
1894+ for ( const [ index , image ] of responseImages . entries ( ) ) {
1895+ if ( ! image ?. b64_json && ! image ?. url ) {
1896+ console . warn ( 'Skipping image without data:' , image ) ;
1897+ continue ;
1898+ }
19171899
1918- writeFileSync ( filepath , buffer ) ;
1900+ let buffer : Buffer ;
1901+ let mimeType = 'image/png' ;
1902+
1903+ if ( image . b64_json ) {
1904+ buffer = Buffer . from ( image . b64_json , 'base64' ) ;
1905+ } else if ( image . url ) {
1906+ // Fallback download
1907+ const imgRes = await fetch ( image . url ) ;
1908+ const arrayBuffer = await imgRes . arrayBuffer ( ) ;
1909+ buffer = Buffer . from ( arrayBuffer ) ;
1910+ const contentType = imgRes . headers . get ( 'content-type' ) ;
1911+ if ( contentType ) mimeType = contentType ;
1912+ } else {
1913+ continue ;
1914+ }
19191915
1920- await db . insert ( storage ) . values ( {
1921- id : storageId ,
1922- userId,
1923- filename,
1924- mimeType,
1925- size : buffer . byteLength ,
1926- path : filepath ,
1927- createdAt : new Date ( ) ,
1928- } ) ;
1916+ const storageId = generateId ( ) ;
1917+ const extension =
1918+ ( mimeType . split ( '/' ) [ 1 ] || 'png' ) . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) || 'png' ;
1919+ const filename = `${ storageId } .${ extension } ` ;
1920+ const filepath = join ( UPLOAD_DIR , filename ) ;
1921+
1922+ // Prevent path traversal
1923+ if ( ! resolve ( filepath ) . startsWith ( resolve ( UPLOAD_DIR ) ) ) {
1924+ throw new Error ( 'Invalid file path' ) ;
1925+ }
1926+
1927+ writeFileSync ( filepath , buffer ) ;
1928+
1929+ await db . insert ( storage ) . values ( {
1930+ id : storageId ,
1931+ userId,
1932+ filename,
1933+ mimeType,
1934+ size : buffer . byteLength ,
1935+ path : filepath ,
1936+ createdAt : new Date ( ) ,
1937+ } ) ;
1938+
1939+ const imageUrl = `/api/storage/${ storageId } ` ;
1940+ generatedImages . push ( {
1941+ url : imageUrl ,
1942+ storage_id : storageId ,
1943+ fileName : `generated-image-${ index + 1 } .${ extension } ` ,
1944+ } ) ;
1945+ }
1946+
1947+ if ( generatedImages . length === 0 ) {
1948+ throw new Error ( 'No valid image data returned from API' ) ;
1949+ }
19291950
1930- const imageUrl = `/api/storage/${ storageId } ` ;
1931- const markdownContent = `` ;
1951+ const markdownContent = generatedImages
1952+ . map ( ( img , idx ) => `` )
1953+ . join ( '\n\n' ) ;
19321954
19331955 await db
19341956 . update ( messages )
19351957 . set ( {
19361958 content : markdownContent ,
19371959 contentHtml : null ,
19381960 tokenCount : 0 ,
1961+ images : generatedImages ,
19391962 costUsd : imageCost ,
19401963 } )
19411964 . where ( eq ( messages . id , assistantMessageId ) ) ;
0 commit comments