@@ -16,6 +16,8 @@ import {
16
16
import { saveImageToLocal } from '../chat/util/FileUtils.ts' ;
17
17
import {
18
18
BedrockMessage ,
19
+ ImageContent ,
20
+ ImageInfo ,
19
21
TextContent ,
20
22
} from '../chat/util/BedrockMessageConvertor.ts' ;
21
23
@@ -59,7 +61,7 @@ export const invokeBedrockWithCallBack = async (
59
61
const url = getApiPrefix ( ) + '/converse' ;
60
62
let intervalId : ReturnType < typeof setInterval > ;
61
63
let completeMessage = '' ;
62
- const timeoutId = setTimeout ( ( ) => controller . abort ( ) , 20000 ) ;
64
+ const timeoutId = setTimeout ( ( ) => controller . abort ( ) , 60000 ) ;
63
65
fetch ( url ! , options )
64
66
. then ( response => {
65
67
return response . body ;
@@ -111,7 +113,7 @@ export const invokeBedrockWithCallBack = async (
111
113
if ( errorMsg . endsWith ( 'AbortError: Aborted' ) ) {
112
114
errorMsg = 'Timed out' ;
113
115
}
114
- if ( errorMsg . indexOf ( 'Unable to resolve host' ) ) {
116
+ if ( errorMsg . indexOf ( 'http' ) >= 0 ) {
115
117
errorMsg = 'Unable to resolve host' ;
116
118
}
117
119
const errorInfo = 'Request error: ' + errorMsg ;
@@ -122,16 +124,31 @@ export const invokeBedrockWithCallBack = async (
122
124
} else {
123
125
const prompt = ( messages [ messages . length - 1 ] . content [ 0 ] as TextContent )
124
126
. 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 ) ;
126
133
if ( imageRes . image . length > 0 ) {
127
134
const localFilePath = await saveImageToLocal ( imageRes . image ) ;
135
+ const imageSize = getImageSize ( ) . split ( 'x' ) [ 0 ] . trim ( ) ;
128
136
const usage : Usage = {
129
137
modelName : getImageModel ( ) . modelName ,
130
138
inputTokens : 0 ,
131
139
outputTokens : 0 ,
132
140
totalTokens : 0 ,
133
- imageCount : 1 ,
141
+ smallImageCount : 0 ,
142
+ imageCount : 0 ,
143
+ largeImageCount : 0 ,
134
144
} ;
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
+ }
135
152
if ( localFilePath ) {
136
153
callback ( `` , true , false , usage ) ;
137
154
}
@@ -143,7 +160,7 @@ export const invokeBedrockWithCallBack = async (
143
160
imageRes . error = 'Request timed out' ;
144
161
}
145
162
}
146
- if ( imageRes . error . indexOf ( 'Unable to resolve host' ) ) {
163
+ if ( imageRes . error . indexOf ( 'http' ) >= 0 ) {
147
164
imageRes . error = 'Request error: Unable to resolve host' ;
148
165
}
149
166
callback ( imageRes . error , true , true ) ;
@@ -210,7 +227,8 @@ export const requestUpgradeInfo = async (
210
227
211
228
export const genImage = async (
212
229
prompt : string ,
213
- controller : AbortController
230
+ controller : AbortController ,
231
+ image ?: ImageInfo
214
232
) : Promise < ImageRes > => {
215
233
if ( ! isConfigured ( ) ) {
216
234
return {
@@ -224,6 +242,7 @@ export const genImage = async (
224
242
const height = imageSize [ 1 ] . trim ( ) ;
225
243
const bodyObject = {
226
244
prompt : prompt ,
245
+ refImages : image ? [ image ] : undefined ,
227
246
modelId : getImageModel ( ) . modelId ,
228
247
region : getRegion ( ) ,
229
248
width : width ,
@@ -242,7 +261,7 @@ export const genImage = async (
242
261
} ;
243
262
244
263
try {
245
- const timeoutMs = parseInt ( width , 10 ) >= 1024 ? 90000 : 60000 ;
264
+ const timeoutMs = parseInt ( width , 10 ) >= 1024 ? 120000 : 90000 ;
246
265
const timeoutId = setTimeout ( ( ) => controller . abort ( ) , timeoutMs ) ;
247
266
const response = await fetch ( url , options ) ;
248
267
if ( ! response . ok ) {
0 commit comments