11import { GenerateAssistantResponseCommand } from '@aws/codewhisperer-streaming-client'
2+ import { THINKING_BUDGETS } from '../../constants'
23import type { AccountRepository } from '../../infrastructure/database/account-repository'
34import type { AccountManager } from '../../plugin/accounts'
45import type { KiroConfig } from '../../plugin/config'
56import { isPermanentError } from '../../plugin/health'
7+ import { imageCache } from '../../plugin/image-cache'
68import * as logger from '../../plugin/logger'
79import { transformToSdkRequest } from '../../plugin/request'
810import { createSdkClient } from '../../plugin/sdk-client'
11+ import { kiroDb } from '../../plugin/storage/sqlite'
912import { syncFromKiroCli } from '../../plugin/sync/kiro-cli'
1013import type { KiroAuthDetails , ManagedAccount , SdkPreparedRequest } from '../../plugin/types'
1114import { AccountSelector } from '../account/account-selector'
@@ -17,8 +20,17 @@ import { RetryStrategy } from './retry-strategy'
1720
1821type ToastFunction = ( message : string , variant : 'info' | 'warning' | 'success' | 'error' ) => void
1922
20- const KIRO_API_PATTERN = / ^ ( h t t p s ? : \/ \/ ) ? q \. [ a - z 0 - 9 - ] + \. a m a z o n a w s \. c o m /
23+ // Matches both the standard q.amazonaws.com endpoint and the Pro runtime.kiro.dev endpoint
24+ const KIRO_API_PATTERN =
25+ / ^ ( h t t p s ? : \/ \/ ) ? ( q \. [ a - z 0 - 9 - ] + \. a m a z o n a w s \. c o m | r u n t i m e \. [ a - z 0 - 9 - ] + \. k i r o \. d e v ) /
2126const REAUTH_FAILURE_COOLDOWN_MS = 60000
27+ const REAUTH_TIMEOUT_MS = 90_000
28+
29+ function extractSessionId ( headers : unknown ) : string | undefined {
30+ if ( ! headers ) return undefined
31+ const h = headers as Record < string , string >
32+ return h [ 'x-session-id' ] ?? h [ 'x-session-affinity' ]
33+ }
2234
2335export class RequestHandler {
2436 private accountSelector : AccountSelector
@@ -29,13 +41,13 @@ export class RequestHandler {
2941 private retryStrategy : RetryStrategy
3042 private reauthInFlight : Promise < boolean > | null = null
3143 private lastFailedReauthAt = 0
32- private static kiroRequestQueue : Promise < void > = Promise . resolve ( )
3344
3445 constructor (
3546 private accountManager : AccountManager ,
3647 private config : KiroConfig ,
3748 private repository : AccountRepository ,
38- private client ?: any
49+ private client ?: any ,
50+ private workspace = ''
3951 ) {
4052 this . accountSelector = new AccountSelector ( accountManager , config , syncFromKiroCli , repository )
4153 this . tokenRefresher = new TokenRefresher ( config , accountManager , syncFromKiroCli , repository )
@@ -52,43 +64,46 @@ export class RequestHandler {
5264 return fetch ( input , init )
5365 }
5466
55- return this . enqueueKiroRequest ( ( ) => this . handleKiroRequest ( url , init , showToast ) )
56- }
57-
58- private async enqueueKiroRequest < T > ( run : ( ) => Promise < T > ) : Promise < T > {
59- const previous = RequestHandler . kiroRequestQueue
60- let release ! : ( ) => void
61-
62- RequestHandler . kiroRequestQueue = new Promise ( ( resolve ) => {
63- release = resolve
64- } )
67+ const sessionId = extractSessionId ( init ?. headers )
6568
66- await previous . catch ( ( ) => { } )
67-
68- try {
69- return await run ( )
70- } finally {
71- release ( )
72- }
69+ return this . handleKiroRequest ( url , init , showToast , sessionId )
7370 }
7471
7572 private async handleKiroRequest (
7673 url : string ,
7774 init : any ,
78- showToast : ToastFunction
75+ showToast : ToastFunction ,
76+ sessionId ?: string
7977 ) : Promise < Response > {
8078 const body = init ?. body ? JSON . parse ( init . body ) : { }
8179 const model = this . extractModel ( url ) || body . model || 'claude-sonnet-4-5'
82- const think =
83- model . endsWith ( '-thinking' ) || ! ! body . providerOptions ?. thinkingConfig || ! ! body . thinkingConfig
84- const budget =
85- body . providerOptions ?. thinkingConfig ?. thinkingBudget ||
86- body . thinkingConfig ?. thinkingBudget ||
87- body . thinkingConfig ?. budget_tokens ||
88- 20000
80+
81+ // Resolve thinking mode + budget.
82+ //
83+ // Priority order:
84+ // 1. Model ID ends with '-thinking' → adaptive thinking, default budget
85+ // 2. providerOptions["kiro"].reasoningEffort → adaptive mode, effort-based budget
86+ // (OpenCode sends this when the user picks low/medium/high in the UI)
87+ // 3. providerOptions.thinkingConfig.thinkingBudget → explicit budget (legacy)
88+ //
89+ // Budget mapping (Kiro max = 200 000 tokens):
90+ // low → 10 000 | medium → 24 000 | high → 200 000 | default → 16 000
91+ const provOpts = body . providerOptions ?. [ 'kiro' ] ?? body . providerOptions ?? { }
92+ const reasoningEffort : string | undefined = provOpts . reasoningEffort
93+ const thinkingConfig = body . providerOptions ?. thinkingConfig
94+
95+ const think = model . endsWith ( '-thinking' ) || ! ! reasoningEffort || ! ! thinkingConfig
96+
97+ let uiEffort : 'low' | 'medium' | 'high' | 'default' = 'default'
98+ if ( reasoningEffort === 'low' ) uiEffort = 'low'
99+ else if ( reasoningEffort === 'medium' ) uiEffort = 'medium'
100+ else if ( reasoningEffort === 'high' ) uiEffort = 'high'
101+
102+ const budget : number = thinkingConfig ?. thinkingBudget || THINKING_BUDGETS [ uiEffort ]
89103
90104 let retry = 0
91105 let consecutiveNullAccounts = 0
106+ let forceNewConversation = false
92107 const retryContext = this . retryStrategy . createContext ( )
93108
94109 while ( true ) {
@@ -131,12 +146,28 @@ export class RequestHandler {
131146 continue
132147 }
133148
134- const sdkPrep = this . prepareSdkRequest ( init ?. body , model , auth , think , budget , showToast )
149+ const sdkPrep = this . prepareSdkRequest (
150+ body ,
151+ model ,
152+ auth ,
153+ think ,
154+ budget ,
155+ showToast ,
156+ uiEffort ,
157+ sessionId
158+ )
159+
160+ const histLen = ( sdkPrep . conversationState as any ) . history ?. length || 0
161+ const agentContId = ( sdkPrep . conversationState as any ) . agentContinuationId || 'none'
162+ logger . debug (
163+ `[REQ] convId=${ sdkPrep . conversationId } history=${ histLen } agentCont=${ agentContId } model=${ model } `
164+ )
135165
136166 const apiTimestamp = this . config . enable_log_api_request ? logger . getTimestamp ( ) : null
137167 if ( apiTimestamp ) {
138168 this . logSdkRequest ( sdkPrep , acc , apiTimestamp )
139169 }
170+
140171 try {
141172 const client = createSdkClient ( auth , sdkPrep . region , sdkPrep . effort )
142173 const command = new GenerateAssistantResponseCommand ( {
@@ -153,13 +184,20 @@ export class RequestHandler {
153184 this . handleSuccessfulRequest ( acc )
154185 this . usageTracker . syncUsage ( acc , auth )
155186
156- return await this . responseHandler . handleSdkSuccess (
187+ const result = await this . responseHandler . handleSdkSuccess (
157188 sdkResponse ,
158189 model ,
159190 sdkPrep . conversationId ,
160- sdkPrep . streaming
191+ sdkPrep . streaming ,
192+ sdkPrep . toolNameMapper ,
193+ think
161194 )
195+ logger . debug ( `[REQ] done convId=${ sdkPrep . conversationId } ` )
196+ return result
162197 } catch ( e : any ) {
198+ logger . warn (
199+ `[REQ] error convId=${ sdkPrep . conversationId } : ${ e ?. name || '' } ${ e ?. message ?. slice ( 0 , 200 ) || String ( e ) . slice ( 0 , 200 ) } `
200+ )
163201 const httpStatus = e ?. $metadata ?. httpStatusCode
164202
165203 if ( httpStatus ) {
@@ -180,20 +218,40 @@ export class RequestHandler {
180218 e ,
181219 mockResponse ,
182220 acc ,
183- { retry } ,
221+ { retry, excludedMs : retryContext . excludedMs } ,
184222 showToast
185223 )
186224
187225 if ( errorResult . shouldRetry ) {
188226 if ( errorResult . newContext ) {
189227 retry = errorResult . newContext . retry
228+ const sleptMs = ( errorResult . newContext . excludedMs ?? 0 ) - retryContext . excludedMs
229+ if ( sleptMs > 0 ) this . retryStrategy . markSleep ( retryContext , sleptMs )
190230 }
191231 if ( errorResult . switchAccount ) {
192232 continue
193233 }
194234 continue
195235 }
196236
237+ if ( httpStatus === 400 && e ?. name === 'ValidationException' && ! forceNewConversation ) {
238+ const { workspace, fingerprint } = sdkPrep . conversationKey
239+ kiroDb . deleteConversationId ( workspace , fingerprint )
240+ // The conversation is starting fresh — drop any carried-forward
241+ // images too so the new convId doesn't inherit stale state.
242+ imageCache . delete ( workspace , fingerprint )
243+ logger . warn (
244+ `[REQ] stale conversationId reset, retrying convId=${ sdkPrep . conversationId } `
245+ )
246+ forceNewConversation = true
247+ continue
248+ }
249+
250+ if ( this . allAccountsPermanentlyUnhealthy ( ) ) {
251+ const reauthed = await this . triggerReauth ( showToast )
252+ if ( reauthed ) continue
253+ }
254+
197255 throw new Error ( `Kiro Error: ${ httpStatus } ` )
198256 }
199257
@@ -221,30 +279,41 @@ export class RequestHandler {
221279 auth : KiroAuthDetails ,
222280 think : boolean ,
223281 budget : number ,
224- showToast ?: ( message : string , variant : 'info' | 'warning' | 'success' | 'error' ) => void
282+ showToast ?: ( message : string , variant : 'info' | 'warning' | 'success' | 'error' ) => void ,
283+ _uiEffort : 'low' | 'medium' | 'high' | 'default' = 'default' ,
284+ sessionId ?: string
225285 ) : SdkPreparedRequest {
226- return transformToSdkRequest ( body , model , auth , think , budget , showToast , {
227- effort : this . config . effort ,
228- autoEffortMapping : this . config . auto_effort_mapping
229- } )
286+ return transformToSdkRequest (
287+ body ,
288+ model ,
289+ auth ,
290+ think ,
291+ budget ,
292+ showToast ,
293+ this . workspace ,
294+ this . config . image_carry_forward ,
295+ sessionId ,
296+ { effort : this . config . effort , autoEffortMapping : this . config . auto_effort_mapping }
297+ )
230298 }
231299
232300 private handleSuccessfulRequest ( acc : ManagedAccount ) : void {
233- if ( acc . failCount && acc . failCount > 0 ) {
234- if ( ! isPermanentError ( acc . unhealthyReason ) ) {
235- acc . failCount = 0
236- acc . isHealthy = true
237- delete acc . unhealthyReason
238- delete acc . recoveryTime
239- this . repository . save ( acc ) . catch ( ( ) => { } )
240- }
301+ // Only write to DB if the account was actually degraded — avoids a
302+ // withDatabaseLock + full merge/dedup round-trip on every healthy request.
303+ if ( acc . failCount && acc . failCount > 0 && ! isPermanentError ( acc . unhealthyReason ) ) {
304+ acc . failCount = 0
305+ acc . isHealthy = true
306+ delete acc . unhealthyReason
307+ delete acc . recoveryTime
308+ this . repository . save ( acc ) . catch ( ( ) => { } )
241309 }
242310 }
243311
244312 private logSdkRequest ( prep : SdkPreparedRequest , acc : ManagedAccount , timestamp : string ) : void {
313+ this . logImageDiagnostic ( prep )
245314 logger . logApiRequest (
246315 {
247- url : `https://q. ${ prep . region } .amazonaws.com /generateAssistantResponse` ,
316+ url : `${ prep . endpoint } /generateAssistantResponse` ,
248317 method : 'POST' ,
249318 headers : { 'x-amzn-kiro-agent-mode' : 'vibe' } ,
250319 body : {
@@ -264,6 +333,32 @@ export class RequestHandler {
264333 )
265334 }
266335
336+ private logImageDiagnostic ( prep : SdkPreparedRequest ) : void {
337+ const kb = ( bytes : number ) : number => Math . round ( bytes / 1024 )
338+ const sumBytes = ( imgs : { source ?: { bytes ?: { byteLength ?: number } } } [ ] ) : number =>
339+ imgs . reduce ( ( n , im ) => n + ( im . source ?. bytes ?. byteLength ?? 0 ) , 0 )
340+
341+ const cmImgs = prep . conversationState . currentMessage ?. userInputMessage ?. images ?? [ ]
342+ const history = ( prep . conversationState as any ) . history ?? [ ]
343+ const histDetail : string [ ] = [ ]
344+ let histImgs = 0
345+ let histKb = 0
346+ for ( let i = 0 ; i < history . length ; i ++ ) {
347+ const imgs = history [ i ] ?. userInputMessage ?. images ?? [ ]
348+ if ( imgs . length === 0 ) continue
349+ const entryKb = kb ( sumBytes ( imgs ) )
350+ histDetail . push ( `i=${ i } :user:${ imgs . length } (${ entryKb } KB)` )
351+ histImgs += imgs . length
352+ histKb += entryKb
353+ }
354+
355+ const detail = histDetail . length ? ` detail=[${ histDetail . join ( ',' ) } ]` : ''
356+ logger . log (
357+ `[IMG] convId=${ prep . conversationId } cur=${ cmImgs . length } (${ kb ( sumBytes ( cmImgs ) ) } KB)` +
358+ ` hist=${ histImgs } /${ history . length } (${ histKb } KB)${ detail } `
359+ )
360+ }
361+
267362 private logSdkResponse ( prep : SdkPreparedRequest , timestamp : string ) : void {
268363 logger . logApiResponse (
269364 {
@@ -295,7 +390,7 @@ export class RequestHandler {
295390 if ( ! this . config . enable_log_api_request ) {
296391 logger . logApiError (
297392 {
298- url : `https://q. ${ prep . region } .amazonaws.com /generateAssistantResponse` ,
393+ url : `${ prep . endpoint } /generateAssistantResponse` ,
299394 method : 'POST' ,
300395 headers : { } ,
301396 body : null ,
@@ -327,9 +422,26 @@ export class RequestHandler {
327422 return this . reauthInFlight
328423 }
329424
425+ if ( ! kiroDb . acquireReauthLock ( ) ) {
426+ logger . warn ( 'Reauth lock held by another instance — polling for completion' )
427+ showToast ( 'Another session is re-authenticating. Please wait...' , 'info' )
428+ const deadline = Date . now ( ) + 10_000
429+ while ( Date . now ( ) < deadline ) {
430+ await this . sleep ( 1000 )
431+ if ( kiroDb . isReauthLockHeld ( ) ) continue
432+ this . repository . invalidateCache ( )
433+ const accounts = await this . repository . findAll ( )
434+ for ( const acc of accounts ) this . accountManager . addAccount ( acc )
435+ return this . hasUsableAccount ( accounts )
436+ }
437+ showToast ( 'Re-authentication timed out. Please try again.' , 'error' )
438+ return false
439+ }
440+
330441 this . reauthInFlight = this . performReauth ( showToast )
331442 const success = await this . reauthInFlight . finally ( ( ) => {
332443 this . reauthInFlight = null
444+ kiroDb . releaseReauthLock ( )
333445 } )
334446 if ( ! success ) this . lastFailedReauthAt = Date . now ( )
335447 return success
@@ -338,15 +450,31 @@ export class RequestHandler {
338450 private async performReauth ( showToast : ToastFunction ) : Promise < boolean > {
339451 try {
340452 showToast ( 'Session expired. Re-authenticating...' , 'warning' )
341- await this . client . provider . oauth . authorize ( {
342- path : { id : 'kiro' } ,
343- body : { method : 0 }
344- } )
453+ logger . warn ( 'Reauth: starting oauth flow' )
454+
455+ const withTimeout = < T > ( promise : Promise < T > , label : string ) : Promise < T > => {
456+ let timer : ReturnType < typeof setTimeout > | undefined
457+ return Promise . race ( [
458+ promise . finally ( ( ) => clearTimeout ( timer ) ) ,
459+ new Promise < T > (
460+ ( _ , reject ) =>
461+ ( timer = setTimeout (
462+ ( ) => reject ( new Error ( `Reauth timed out waiting for ${ label } ` ) ) ,
463+ REAUTH_TIMEOUT_MS
464+ ) )
465+ )
466+ ] )
467+ }
345468
346- await this . client . provider . oauth . callback ( {
347- path : { id : 'kiro' } ,
348- body : { method : 0 }
349- } )
469+ await withTimeout (
470+ this . client . provider . oauth . authorize ( { path : { id : 'kiro' } , body : { method : 0 } } ) ,
471+ 'oauth.authorize'
472+ )
473+
474+ await withTimeout (
475+ this . client . provider . oauth . callback ( { path : { id : 'kiro' } , body : { method : 0 } } ) ,
476+ 'oauth.callback'
477+ )
350478
351479 this . repository . invalidateCache ( )
352480 const accounts = await this . repository . findAll ( )
@@ -364,6 +492,12 @@ export class RequestHandler {
364492 return true
365493 } catch ( e ) {
366494 logger . error ( 'Re-auth failed' , e instanceof Error ? e : new Error ( String ( e ) ) )
495+ showToast (
496+ e instanceof Error && e . message . includes ( 'timed out' )
497+ ? 'Re-authentication timed out. Please try again.'
498+ : 'Re-authentication failed. Please try again.' ,
499+ 'error'
500+ )
367501 return false
368502 }
369503 }
0 commit comments