@@ -16,6 +16,8 @@ import {
1616import  {  saveImageToLocal  }  from  '../chat/util/FileUtils.ts' ; 
1717import  { 
1818  BedrockMessage , 
19+   ImageContent , 
20+   ImageInfo , 
1921  TextContent , 
2022}  from  '../chat/util/BedrockMessageConvertor.ts' ; 
2123
@@ -59,7 +61,7 @@ export const invokeBedrockWithCallBack = async (
5961    const  url  =  getApiPrefix ( )  +  '/converse' ; 
6062    let  intervalId : ReturnType < typeof  setInterval > ; 
6163    let  completeMessage  =  '' ; 
62-     const  timeoutId  =  setTimeout ( ( )  =>  controller . abort ( ) ,  20000 ) ; 
64+     const  timeoutId  =  setTimeout ( ( )  =>  controller . abort ( ) ,  60000 ) ; 
6365    fetch ( url ! ,  options ) 
6466      . then ( response  =>  { 
6567        return  response . body ; 
@@ -111,7 +113,7 @@ export const invokeBedrockWithCallBack = async (
111113          if  ( errorMsg . endsWith ( 'AbortError: Aborted' ) )  { 
112114            errorMsg  =  'Timed out' ; 
113115          } 
114-           if  ( errorMsg . indexOf ( 'Unable to resolve host'  ) )  { 
116+           if  ( errorMsg . indexOf ( 'http'  )   >=   0 )  { 
115117            errorMsg  =  'Unable to resolve host' ; 
116118          } 
117119          const  errorInfo  =  'Request error: '  +  errorMsg ; 
@@ -122,16 +124,31 @@ export const invokeBedrockWithCallBack = async (
122124  }  else  { 
123125    const  prompt  =  ( messages [ messages . length  -  1 ] . content [ 0 ]  as  TextContent ) 
124126      . text ; 
125-     const  imageRes  =  await  genImage ( prompt ,  controller ) ; 
127+     let  image : ImageInfo  |  undefined ; 
128+     if  ( messages [ messages . length  -  1 ] . content [ 1 ] )  { 
129+       image  =  ( messages [ messages . length  -  1 ] . content [ 1 ]  as  ImageContent ) . image ; 
130+     } 
131+ 
132+     const  imageRes  =  await  genImage ( prompt ,  controller ,  image ) ; 
126133    if  ( imageRes . image . length  >  0 )  { 
127134      const  localFilePath  =  await  saveImageToLocal ( imageRes . image ) ; 
135+       const  imageSize  =  getImageSize ( ) . split ( 'x' ) [ 0 ] . trim ( ) ; 
128136      const  usage : Usage  =  { 
129137        modelName : getImageModel ( ) . modelName , 
130138        inputTokens : 0 , 
131139        outputTokens : 0 , 
132140        totalTokens : 0 , 
133-         imageCount : 1 , 
141+         smallImageCount : 0 , 
142+         imageCount : 0 , 
143+         largeImageCount : 0 , 
134144      } ; 
145+       if  ( imageSize  ===  '512' )  { 
146+         usage . smallImageCount  =  1 ; 
147+       }  else  if  ( imageSize  ===  '1024' )  { 
148+         usage . imageCount  =  1 ; 
149+       }  else  if  ( imageSize  ===  '2048' )  { 
150+         usage . largeImageCount  =  1 ; 
151+       } 
135152      if  ( localFilePath )  { 
136153        callback ( ` ; 
137154      } 
@@ -143,7 +160,7 @@ export const invokeBedrockWithCallBack = async (
143160          imageRes . error  =  'Request timed out' ; 
144161        } 
145162      } 
146-       if  ( imageRes . error . indexOf ( 'Unable to resolve host'  ) )  { 
163+       if  ( imageRes . error . indexOf ( 'http'  )   >=   0 )  { 
147164        imageRes . error  =  'Request error: Unable to resolve host' ; 
148165      } 
149166      callback ( imageRes . error ,  true ,  true ) ; 
@@ -210,7 +227,8 @@ export const requestUpgradeInfo = async (
210227
211228export  const  genImage  =  async  ( 
212229  prompt : string , 
213-   controller : AbortController 
230+   controller : AbortController , 
231+   image ?: ImageInfo 
214232) : Promise < ImageRes >  =>  { 
215233  if  ( ! isConfigured ( ) )  { 
216234    return  { 
@@ -224,6 +242,7 @@ export const genImage = async (
224242  const  height  =  imageSize [ 1 ] . trim ( ) ; 
225243  const  bodyObject  =  { 
226244    prompt : prompt , 
245+     refImages : image  ? [ image ]  : undefined , 
227246    modelId : getImageModel ( ) . modelId , 
228247    region : getRegion ( ) , 
229248    width : width , 
@@ -242,7 +261,7 @@ export const genImage = async (
242261  } ; 
243262
244263  try  { 
245-     const  timeoutMs  =  parseInt ( width ,  10 )  >=  1024  ? 90000  : 60000 ; 
264+     const  timeoutMs  =  parseInt ( width ,  10 )  >=  1024  ? 120000  : 90000 ; 
246265    const  timeoutId  =  setTimeout ( ( )  =>  controller . abort ( ) ,  timeoutMs ) ; 
247266    const  response  =  await  fetch ( url ,  options ) ; 
248267    if  ( ! response . ok )  { 
0 commit comments